2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1998-2001,2007 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * This module either be compiled into Mutt, or it can be
22 * built as a separate program. For building it
23 * separately, define the DL_STANDALONE preprocessor
39 #include <sys/utsname.h>
45 #ifndef _POSIX_PATH_MAX
59 #define MAXLINKS 1024 /* maximum link depth */
63 # define LONG_STRING 1024
64 # define MAXLOCKATTEMPT 5
66 # define strfcpy(A,B,C) strncpy (A,B,C), *(A+(C)-1)=0
71 # define SETEGID setegid
73 # define SETEGID setgid
76 # define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK ? 1 : 0)
81 # ifndef HAVE_SNPRINTF
82 extern int snprintf (char *, size_t, const char *, ...);
85 #else /* DL_STANDALONE */
88 # error Do not try to compile dotlock as a mutt module when requiring egid switching!
94 #endif /* DL_STANDALONE */
96 static int DotlockFlags;
97 static int Retry = MAXLOCKATTEMPT;
100 static char *Hostname;
104 static gid_t UserGid;
105 static gid_t MailGid;
108 static int dotlock_deference_symlink (char *, size_t, const char *);
109 static int dotlock_prepare (char *, size_t, const char *, int fd);
110 static int dotlock_check_stats (struct stat *, struct stat *);
111 static int dotlock_dispatch (const char *, int fd);
114 static int dotlock_init_privs (void);
115 static void usage (const char *);
118 static void dotlock_expand_link (char *, const char *, const char *);
119 static void BEGIN_PRIVILEGED (void);
120 static void END_PRIVILEGED (void);
122 /* These functions work on the current directory.
123 * Invoke dotlock_prepare () before and check their
127 static int dotlock_try (void);
128 static int dotlock_unlock (const char *);
129 static int dotlock_unlink (const char *);
130 static int dotlock_lock (const char *);
135 #define check_flags(a) if (a & DL_FL_ACTIONS) usage (argv[0])
137 int main (int argc, char **argv)
141 struct utsname utsname;
143 /* first, drop privileges */
145 if (dotlock_init_privs () == -1)
149 /* determine the system's host name */
152 if (!(Hostname = strdup (utsname.nodename))) /* __MEM_CHECKED__ */
154 if ((p = strchr (Hostname, '.')))
158 /* parse the command line options. */
161 while ((i = getopt (argc, argv, "dtfupr:")) != EOF)
165 /* actions, mutually exclusive */
166 case 't': check_flags (DotlockFlags); DotlockFlags |= DL_FL_TRY; break;
167 case 'd': check_flags (DotlockFlags); DotlockFlags |= DL_FL_UNLINK; break;
168 case 'u': check_flags (DotlockFlags); DotlockFlags |= DL_FL_UNLOCK; break;
171 case 'f': DotlockFlags |= DL_FL_FORCE; break;
172 case 'p': DotlockFlags |= DL_FL_USEPRIV; break;
173 case 'r': DotlockFlags |= DL_FL_RETRY; Retry = atoi (optarg); break;
175 default: usage (argv[0]);
179 if (optind == argc || Retry < 0)
182 return dotlock_dispatch (argv[optind], -1);
187 * Determine our effective group ID, and drop
192 * 0 - everything went fine
193 * -1 - we couldn't drop privileges.
199 dotlock_init_privs (void)
205 MailGid = getegid ();
207 if (SETEGID (UserGid) != 0)
216 #else /* DL_STANDALONE */
219 * This function is intended to be invoked from within
220 * mutt instead of mx.c's invoke_dotlock ().
223 int dotlock_invoke (const char *path, int fd, int flags, int retry)
228 DotlockFlags = flags;
230 if ((currdir = open (".", O_RDONLY)) == -1)
233 if (!(DotlockFlags & DL_FL_RETRY) || retry)
234 Retry = MAXLOCKATTEMPT;
238 r = dotlock_dispatch (path, fd);
246 #endif /* DL_STANDALONE */
249 static int dotlock_dispatch (const char *f, int fd)
251 char realpath[_POSIX_PATH_MAX];
253 /* If dotlock_prepare () succeeds [return value == 0],
254 * realpath contains the basename of f, and we have
255 * successfully changed our working directory to
256 * `dirname $f`. Additionally, f has been opened for
257 * reading to verify that the user has at least read
258 * permissions on that file.
260 * For a more detailed explanation of all this, see the
261 * lengthy comment below.
264 if (dotlock_prepare (realpath, sizeof (realpath), f, fd) != 0)
267 /* Actually perform the locking operation. */
269 if (DotlockFlags & DL_FL_TRY)
270 return dotlock_try ();
271 else if (DotlockFlags & DL_FL_UNLOCK)
272 return dotlock_unlock (realpath);
273 else if (DotlockFlags & DL_FL_UNLINK)
274 return dotlock_unlink (realpath);
276 return dotlock_lock (realpath);
283 * This function re-acquires the privileges we may have
284 * if the user told us to do so by giving the "-p"
285 * command line option.
287 * BEGIN_PRIVILEGES () won't return if an error occurs.
292 BEGIN_PRIVILEGED (void)
295 if (DotlockFlags & DL_FL_USEPRIV)
297 if (SETEGID (MailGid) != 0)
299 /* perror ("setegid"); */
309 * This function drops the group privileges we may have.
311 * END_PRIVILEGED () won't return if an error occurs.
316 END_PRIVILEGED (void)
319 if (DotlockFlags & DL_FL_USEPRIV)
321 if (SETEGID (UserGid) != 0)
323 /* perror ("setegid"); */
335 * This function doesn't return.
340 usage (const char *av0)
342 fprintf (stderr, "dotlock [Mutt %s (%s)]\n", VERSION, ReleaseDate);
343 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n",
351 "\n -p\t\tprivileged"
355 "\n -r <retries>\tRetry locking"
364 * Access checking: Let's avoid to lock other users' mail
365 * spool files if we aren't permitted to read them.
367 * Some simple-minded access (2) checking isn't sufficient
368 * here: The problem is that the user may give us a
369 * deeply nested path to a file which has the same name
370 * as the file he wants to lock, but different
371 * permissions, say, e.g.
372 * /tmp/lots/of/subdirs/var/spool/mail/root.
374 * He may then try to replace /tmp/lots/of/subdirs by a
375 * symbolic link to / after we have invoked access () to
376 * check the file's permissions. The lockfile we'd
377 * create or remove would then actually be
378 * /var/spool/mail/root.
380 * To avoid this attack, we proceed as follows:
382 * - First, follow symbolic links a la
383 * dotlock_deference_symlink ().
385 * - get the result's dirname.
387 * - chdir to this directory. If you can't, bail out.
389 * - try to open the file in question, only using its
390 * basename. If you can't, bail out.
392 * - fstat that file and compare the result to a
393 * subsequent lstat (only using the basename). If
394 * the comparison fails, bail out.
396 * dotlock_prepare () is invoked from main () directly
397 * after the command line parsing has been done.
401 * 0 - Evereything's fine. The program's new current
402 * directory is the contains the file to be locked.
403 * The string pointed to by bn contains the name of
404 * the file to be locked.
406 * -1 - Something failed. Don't continue.
412 dotlock_check_stats (struct stat *fsb, struct stat *lsb)
414 /* S_ISLNK (fsb->st_mode) should actually be impossible,
415 * but we may have mixed up the parameters somewhere.
419 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
422 if ((lsb->st_dev != fsb->st_dev) ||
423 (lsb->st_ino != fsb->st_ino) ||
424 (lsb->st_mode != fsb->st_mode) ||
425 (lsb->st_nlink != fsb->st_nlink) ||
426 (lsb->st_uid != fsb->st_uid) ||
427 (lsb->st_gid != fsb->st_gid) ||
428 (lsb->st_rdev != fsb->st_rdev) ||
429 (lsb->st_size != fsb->st_size))
431 /* something's fishy */
439 dotlock_prepare (char *bn, size_t l, const char *f, int _fd)
441 struct stat fsb, lsb;
442 char realpath[_POSIX_PATH_MAX];
443 char *basename, *dirname;
448 if (dotlock_deference_symlink (realpath, sizeof (realpath), f) == -1)
451 if ((p = strrchr (realpath, '/')))
463 if (strlen (basename) + 1 > l)
466 strfcpy (bn, basename, l);
468 if (chdir (dirname) == -1)
473 else if ((fd = open (basename, O_RDONLY)) == -1)
476 r = fstat (fd, &fsb);
484 if (lstat (basename, &lsb) == -1)
487 if (dotlock_check_stats (&fsb, &lsb) == -1)
494 * Expand a symbolic link.
496 * This function expects newpath to have space for
497 * at least _POSIX_PATH_MAX characters.
502 dotlock_expand_link (char *newpath, const char *path, const char *link)
504 const char *lb = NULL;
507 /* link is full path */
510 strfcpy (newpath, link, _POSIX_PATH_MAX);
514 if ((lb = strrchr (path, '/')) == NULL)
516 /* no path in link */
517 strfcpy (newpath, link, _POSIX_PATH_MAX);
522 memcpy (newpath, path, len);
523 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
528 * Deference a chain of symbolic links
530 * The final path is written to d.
535 dotlock_deference_symlink (char *d, size_t l, const char *path)
538 char realpath[_POSIX_PATH_MAX];
539 const char *pathptr = path;
542 while (count++ < MAXLINKS)
544 if (lstat (pathptr, &sb) == -1)
546 /* perror (pathptr); */
550 if (S_ISLNK (sb.st_mode))
552 char linkfile[_POSIX_PATH_MAX];
553 char linkpath[_POSIX_PATH_MAX];
556 if ((len = readlink (pathptr, linkfile, sizeof (linkfile) - 1)) == -1)
558 /* perror (pathptr); */
562 linkfile[len] = '\0';
563 dotlock_expand_link (linkpath, pathptr, linkfile);
564 strfcpy (realpath, linkpath, sizeof (realpath));
571 strfcpy (d, pathptr, l);
578 * realpath is assumed _not_ to be an absolute path to
579 * the file we are about to lock. Invoke
580 * dotlock_prepare () before using this function!
584 #define HARDMAXATTEMPTS 10
587 dotlock_lock (const char *realpath)
589 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
590 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
591 size_t prev_size = 0;
598 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
599 realpath, Hostname, (int) getpid ());
600 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
605 unlink (nfslockfile);
607 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0)
614 /* perror ("cannot open NFS lock file"); */
627 while (hard_count++ < HARDMAXATTEMPTS)
631 link (nfslockfile, lockfile);
634 if (stat (nfslockfile, &sb) != 0)
636 /* perror ("stat"); */
640 if (sb.st_nlink == 2)
644 prev_size = sb.st_size;
646 if (prev_size == sb.st_size && ++count > Retry)
648 if (DotlockFlags & DL_FL_FORCE)
660 unlink (nfslockfile);
666 prev_size = sb.st_size;
668 /* don't trust sleep (3) as it may be interrupted
669 * by users sending signals.
675 } while (time (NULL) == t);
679 unlink (nfslockfile);
689 * The same comment as for dotlock_lock () applies here.
694 dotlock_unlock (const char *realpath)
696 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
699 snprintf (lockfile, sizeof (lockfile), "%s.lock",
703 i = unlink (lockfile);
712 /* remove an empty file */
715 dotlock_unlink (const char *realpath)
720 if (dotlock_lock (realpath) != DL_EX_OK)
723 if ((i = lstat (realpath, &lsb)) == 0 && lsb.st_size == 0)
726 dotlock_unlock (realpath);
728 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
733 * Check if a file can be locked at all.
735 * The same comment as for dotlock_lock () applies here.
746 if (access (".", W_OK) == 0)
750 if (stat (".", &sb) == 0)
752 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
753 return DL_EX_NEED_PRIVS;
757 return DL_EX_IMPOSSIBLE;