2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1998-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
12 * This module either be compiled into Mutt, or it can be
13 * built as a separate program. For building it
14 * separately, define the DL_STANDALONE preprocessor
30 #include <sys/utsname.h>
36 #ifndef _POSIX_PATH_MAX
37 #include <posix1_lim.h>
51 #define MAXLINKS 1024 /* maximum link depth */
55 # define LONG_STRING 1024
56 # define MAXLOCKATTEMPT 5
58 # define strfcpy(A,B,C) m_strcpy(A,C,B)
63 # define SETEGID setegid
65 # define SETEGID setgid
68 # define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK ? 1 : 0)
73 # ifndef HAVE_SNPRINTF
74 extern int snprintf (char *, size_t, const char *, ...);
77 #else /* DL_STANDALONE */
80 # error Do not try to compile dotlock as a mutt module when requiring egid switching!
86 #endif /* DL_STANDALONE */
88 static int DotlockFlags;
89 static int Retry = MAXLOCKATTEMPT;
92 static char *Hostname;
100 static int dotlock_deference_symlink (char *, size_t, const char *);
101 static int dotlock_prepare (char *, size_t, const char *, int fd);
102 static int dotlock_check_stats (struct stat *, struct stat *);
103 static int dotlock_dispatch (const char *, int fd);
106 static int dotlock_init_privs (void);
107 static void usage (const char *);
110 static void dotlock_expand_link (char *, const char *, const char *);
111 static void BEGIN_PRIVILEGED (void);
112 static void END_PRIVILEGED (void);
114 /* These functions work on the current directory.
115 * Invoke dotlock_prepare () before and check their
119 static int dotlock_try (void);
120 static int dotlock_unlock (const char *);
121 static int dotlock_unlink (const char *);
122 static int dotlock_lock (const char *);
127 #define check_flags(a) if (a & DL_FL_ACTIONS) usage (argv[0])
129 size_t m_strlen(const char* s) {
130 return (s ? strlen (s) : 0);
133 int main (int argc, char **argv)
137 struct utsname utsname;
139 /* first, drop privileges */
141 if (dotlock_init_privs () == -1)
145 /* determine the system's host name */
148 if (!(Hostname = strdup (utsname.nodename))) /* __MEM_CHECKED__ */
150 if ((p = strchr (Hostname, '.')))
154 /* parse the command line options. */
157 while ((i = getopt (argc, argv, "dtfupr:")) != EOF) {
159 /* actions, mutually exclusive */
161 check_flags (DotlockFlags);
162 DotlockFlags |= DL_FL_TRY;
165 check_flags (DotlockFlags);
166 DotlockFlags |= DL_FL_UNLINK;
169 check_flags (DotlockFlags);
170 DotlockFlags |= DL_FL_UNLOCK;
175 DotlockFlags |= DL_FL_FORCE;
178 DotlockFlags |= DL_FL_USEPRIV;
181 DotlockFlags |= DL_FL_RETRY;
182 Retry = atoi (optarg);
190 if (optind == argc || Retry < 0)
193 return dotlock_dispatch (argv[optind], -1);
198 * Determine our effective group ID, and drop
203 * 0 - everything went fine
204 * -1 - we couldn't drop privileges.
209 static int dotlock_init_privs (void)
215 MailGid = getegid ();
217 if (SETEGID (UserGid) != 0)
226 #else /* DL_STANDALONE */
229 * This function is intended to be invoked from within
230 * mutt instead of mx.c's invoke_dotlock ().
233 int dotlock_invoke (const char *path, int fd, int flags, int retry)
238 DotlockFlags = flags;
240 if ((currdir = open (".", O_RDONLY)) == -1)
243 if (!(DotlockFlags & DL_FL_RETRY) || retry)
244 Retry = MAXLOCKATTEMPT;
248 r = dotlock_dispatch (path, fd);
256 #endif /* DL_STANDALONE */
259 static int dotlock_dispatch (const char *f, int fd)
261 char realpath[_POSIX_PATH_MAX];
263 /* If dotlock_prepare () succeeds [return value == 0],
264 * realpath contains the basename of f, and we have
265 * successfully changed our working directory to
266 * `dirname $f`. Additionally, f has been opened for
267 * reading to verify that the user has at least read
268 * permissions on that file.
270 * For a more detailed explanation of all this, see the
271 * lengthy comment below.
274 if (dotlock_prepare (realpath, sizeof (realpath), f, fd) != 0)
277 /* Actually perform the locking operation. */
279 if (DotlockFlags & DL_FL_TRY)
280 return dotlock_try ();
281 else if (DotlockFlags & DL_FL_UNLOCK)
282 return dotlock_unlock (realpath);
283 else if (DotlockFlags & DL_FL_UNLINK)
284 return dotlock_unlink (realpath);
286 return dotlock_lock (realpath);
293 * This function re-acquires the privileges we may have
294 * if the user told us to do so by giving the "-p"
295 * command line option.
297 * BEGIN_PRIVILEGES () won't return if an error occurs.
301 static void BEGIN_PRIVILEGED (void)
304 if (DotlockFlags & DL_FL_USEPRIV) {
305 if (SETEGID (MailGid) != 0) {
306 /* perror ("setegid"); */
316 * This function drops the group privileges we may have.
318 * END_PRIVILEGED () won't return if an error occurs.
322 static void END_PRIVILEGED (void)
325 if (DotlockFlags & DL_FL_USEPRIV) {
326 if (SETEGID (UserGid) != 0) {
327 /* perror ("setegid"); */
339 * This function doesn't return.
343 static void usage (const char *av0)
345 fprintf (stderr, "dotlock [Mutt-ng %s (%s)]\n", VERSION, ReleaseDate);
346 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n", av0);
351 "\n -u\t\tunlock" "\n -d\t\tunlink" "\n -p\t\tprivileged"
355 "\n -r <retries>\tRetry locking" "\n", stderr);
363 * Access checking: Let's avoid to lock other users' mail
364 * spool files if we aren't permitted to read them.
366 * Some simple-minded access (2) checking isn't sufficient
367 * here: The problem is that the user may give us a
368 * deeply nested path to a file which has the same name
369 * as the file he wants to lock, but different
370 * permissions, say, e.g.
371 * /tmp/lots/of/subdirs/var/spool/mail/root.
373 * He may then try to replace /tmp/lots/of/subdirs by a
374 * symbolic link to / after we have invoked access () to
375 * check the file's permissions. The lockfile we'd
376 * create or remove would then actually be
377 * /var/spool/mail/root.
379 * To avoid this attack, we proceed as follows:
381 * - First, follow symbolic links a la
382 * dotlock_deference_symlink ().
384 * - get the result's dirname.
386 * - chdir to this directory. If you can't, bail out.
388 * - try to open the file in question, only using its
389 * basename. If you can't, bail out.
391 * - fstat that file and compare the result to a
392 * subsequent lstat (only using the basename). If
393 * the comparison fails, bail out.
395 * dotlock_prepare () is invoked from main () directly
396 * after the command line parsing has been done.
400 * 0 - Evereything's fine. The program's new current
401 * directory is the contains the file to be locked.
402 * The string pointed to by bn contains the name of
403 * the file to be locked.
405 * -1 - Something failed. Don't continue.
410 static int dotlock_check_stats (struct stat *fsb, struct stat *lsb)
412 /* S_ISLNK (fsb->st_mode) should actually be impossible,
413 * but we may have mixed up the parameters somewhere.
417 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
420 if ((lsb->st_dev != fsb->st_dev) ||
421 (lsb->st_ino != fsb->st_ino) ||
422 (lsb->st_mode != fsb->st_mode) ||
423 (lsb->st_nlink != fsb->st_nlink) ||
424 (lsb->st_uid != fsb->st_uid) ||
425 (lsb->st_gid != fsb->st_gid) ||
426 (lsb->st_rdev != fsb->st_rdev) || (lsb->st_size != fsb->st_size)) {
427 /* something's fishy */
434 static int dotlock_prepare (char *bn, size_t l, const char *f, int _fd)
436 struct stat fsb, lsb;
437 char realpath[_POSIX_PATH_MAX];
438 char *basename, *dirname;
443 if (dotlock_deference_symlink (realpath, sizeof (realpath), f) == -1)
446 if ((p = strrchr (realpath, '/'))) {
456 if (m_strlen(basename) + 1 > l)
459 strfcpy (bn, basename, l);
461 if (chdir (dirname) == -1)
466 else if ((fd = open (basename, O_RDONLY)) == -1)
469 r = fstat (fd, &fsb);
477 if (lstat (basename, &lsb) == -1)
480 if (dotlock_check_stats (&fsb, &lsb) == -1)
487 * Expand a symbolic link.
489 * This function expects newpath to have space for
490 * at least _POSIX_PATH_MAX characters.
495 dotlock_expand_link (char *newpath, const char *path, const char *link)
497 const char *lb = NULL;
500 /* link is full path */
502 strfcpy (newpath, link, _POSIX_PATH_MAX);
506 if ((lb = strrchr (path, '/')) == NULL) {
507 /* no path in link */
508 strfcpy (newpath, link, _POSIX_PATH_MAX);
513 memcpy (newpath, path, len);
514 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
519 * Deference a chain of symbolic links
521 * The final path is written to d.
525 static int dotlock_deference_symlink (char *d, size_t l, const char *path)
528 char realpath[_POSIX_PATH_MAX];
529 const char *pathptr = path;
532 while (count++ < MAXLINKS) {
533 if (lstat (pathptr, &sb) == -1) {
534 /* perror (pathptr); */
538 if (S_ISLNK (sb.st_mode)) {
539 char linkfile[_POSIX_PATH_MAX];
540 char linkpath[_POSIX_PATH_MAX];
543 if ((len = readlink (pathptr, linkfile, sizeof (linkfile))) == -1) {
544 /* perror (pathptr); */
548 linkfile[len] = '\0';
549 dotlock_expand_link (linkpath, pathptr, linkfile);
550 strfcpy (realpath, linkpath, sizeof (realpath));
557 strfcpy (d, pathptr, l);
564 * realpath is assumed _not_ to be an absolute path to
565 * the file we are about to lock. Invoke
566 * dotlock_prepare () before using this function!
570 #define HARDMAXATTEMPTS 10
572 static int dotlock_lock (const char *realpath)
574 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
575 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
576 size_t prev_size = 0;
583 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
584 realpath, Hostname, (int) getpid ());
585 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
590 unlink (nfslockfile);
592 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0) {
596 if (errno != EAGAIN) {
597 /* perror ("cannot open NFS lock file"); */
610 while (hard_count++ < HARDMAXATTEMPTS) {
613 link (nfslockfile, lockfile);
616 if (stat (nfslockfile, &sb) != 0) {
617 /* perror ("stat"); */
621 if (sb.st_nlink == 2)
625 prev_size = sb.st_size;
627 if (prev_size == sb.st_size && ++count > Retry) {
628 if (DotlockFlags & DL_FL_FORCE) {
638 unlink (nfslockfile);
644 prev_size = sb.st_size;
646 /* don't trust sleep (3) as it may be interrupted
647 * by users sending signals.
653 } while (time (NULL) == t);
657 unlink (nfslockfile);
667 * The same comment as for dotlock_lock () applies here.
671 static int dotlock_unlock (const char *realpath)
673 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
676 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
679 i = unlink (lockfile);
688 /* remove an empty file */
690 static int dotlock_unlink (const char *realpath)
695 if (dotlock_lock (realpath) != DL_EX_OK)
698 if ((i = lstat (realpath, &lsb)) == 0 && lsb.st_size == 0)
701 dotlock_unlock (realpath);
703 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
708 * Check if a file can be locked at all.
710 * The same comment as for dotlock_lock () applies here.
714 static int dotlock_try (void)
720 if (access (".", W_OK) == 0)
724 if (stat (".", &sb) == 0) {
725 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
726 return DL_EX_NEED_PRIVS;
730 return DL_EX_IMPOSSIBLE;