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) strncpy (A,B,C), *(A+(C)-1)=0
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 int main (int argc, char **argv)
133 struct utsname utsname;
135 /* first, drop privileges */
137 if (dotlock_init_privs () == -1)
141 /* determine the system's host name */
144 if (!(Hostname = strdup (utsname.nodename))) /* __MEM_CHECKED__ */
146 if ((p = strchr (Hostname, '.')))
150 /* parse the command line options. */
153 while ((i = getopt (argc, argv, "dtfupr:")) != EOF) {
155 /* actions, mutually exclusive */
157 check_flags (DotlockFlags);
158 DotlockFlags |= DL_FL_TRY;
161 check_flags (DotlockFlags);
162 DotlockFlags |= DL_FL_UNLINK;
165 check_flags (DotlockFlags);
166 DotlockFlags |= DL_FL_UNLOCK;
171 DotlockFlags |= DL_FL_FORCE;
174 DotlockFlags |= DL_FL_USEPRIV;
177 DotlockFlags |= DL_FL_RETRY;
178 Retry = atoi (optarg);
186 if (optind == argc || Retry < 0)
189 return dotlock_dispatch (argv[optind], -1);
194 * Determine our effective group ID, and drop
199 * 0 - everything went fine
200 * -1 - we couldn't drop privileges.
205 static int dotlock_init_privs (void)
211 MailGid = getegid ();
213 if (SETEGID (UserGid) != 0)
222 #else /* DL_STANDALONE */
225 * This function is intended to be invoked from within
226 * mutt instead of mx.c's invoke_dotlock ().
229 int dotlock_invoke (const char *path, int fd, int flags, int retry)
234 DotlockFlags = flags;
236 if ((currdir = open (".", O_RDONLY)) == -1)
239 if (!(DotlockFlags & DL_FL_RETRY) || retry)
240 Retry = MAXLOCKATTEMPT;
244 r = dotlock_dispatch (path, fd);
252 #endif /* DL_STANDALONE */
255 static int dotlock_dispatch (const char *f, int fd)
257 char realpath[_POSIX_PATH_MAX];
259 /* If dotlock_prepare () succeeds [return value == 0],
260 * realpath contains the basename of f, and we have
261 * successfully changed our working directory to
262 * `dirname $f`. Additionally, f has been opened for
263 * reading to verify that the user has at least read
264 * permissions on that file.
266 * For a more detailed explanation of all this, see the
267 * lengthy comment below.
270 if (dotlock_prepare (realpath, sizeof (realpath), f, fd) != 0)
273 /* Actually perform the locking operation. */
275 if (DotlockFlags & DL_FL_TRY)
276 return dotlock_try ();
277 else if (DotlockFlags & DL_FL_UNLOCK)
278 return dotlock_unlock (realpath);
279 else if (DotlockFlags & DL_FL_UNLINK)
280 return dotlock_unlink (realpath);
282 return dotlock_lock (realpath);
289 * This function re-acquires the privileges we may have
290 * if the user told us to do so by giving the "-p"
291 * command line option.
293 * BEGIN_PRIVILEGES () won't return if an error occurs.
297 static void BEGIN_PRIVILEGED (void)
300 if (DotlockFlags & DL_FL_USEPRIV) {
301 if (SETEGID (MailGid) != 0) {
302 /* perror ("setegid"); */
312 * This function drops the group privileges we may have.
314 * END_PRIVILEGED () won't return if an error occurs.
318 static void END_PRIVILEGED (void)
321 if (DotlockFlags & DL_FL_USEPRIV) {
322 if (SETEGID (UserGid) != 0) {
323 /* perror ("setegid"); */
335 * This function doesn't return.
339 static void usage (const char *av0)
341 fprintf (stderr, "dotlock [Mutt-ng %s (%s)]\n", VERSION, ReleaseDate);
342 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n", av0);
347 "\n -u\t\tunlock" "\n -d\t\tunlink" "\n -p\t\tprivileged"
351 "\n -r <retries>\tRetry locking" "\n", stderr);
359 * Access checking: Let's avoid to lock other users' mail
360 * spool files if we aren't permitted to read them.
362 * Some simple-minded access (2) checking isn't sufficient
363 * here: The problem is that the user may give us a
364 * deeply nested path to a file which has the same name
365 * as the file he wants to lock, but different
366 * permissions, say, e.g.
367 * /tmp/lots/of/subdirs/var/spool/mail/root.
369 * He may then try to replace /tmp/lots/of/subdirs by a
370 * symbolic link to / after we have invoked access () to
371 * check the file's permissions. The lockfile we'd
372 * create or remove would then actually be
373 * /var/spool/mail/root.
375 * To avoid this attack, we proceed as follows:
377 * - First, follow symbolic links a la
378 * dotlock_deference_symlink ().
380 * - get the result's dirname.
382 * - chdir to this directory. If you can't, bail out.
384 * - try to open the file in question, only using its
385 * basename. If you can't, bail out.
387 * - fstat that file and compare the result to a
388 * subsequent lstat (only using the basename). If
389 * the comparison fails, bail out.
391 * dotlock_prepare () is invoked from main () directly
392 * after the command line parsing has been done.
396 * 0 - Evereything's fine. The program's new current
397 * directory is the contains the file to be locked.
398 * The string pointed to by bn contains the name of
399 * the file to be locked.
401 * -1 - Something failed. Don't continue.
406 static int dotlock_check_stats (struct stat *fsb, struct stat *lsb)
408 /* S_ISLNK (fsb->st_mode) should actually be impossible,
409 * but we may have mixed up the parameters somewhere.
413 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
416 if ((lsb->st_dev != fsb->st_dev) ||
417 (lsb->st_ino != fsb->st_ino) ||
418 (lsb->st_mode != fsb->st_mode) ||
419 (lsb->st_nlink != fsb->st_nlink) ||
420 (lsb->st_uid != fsb->st_uid) ||
421 (lsb->st_gid != fsb->st_gid) ||
422 (lsb->st_rdev != fsb->st_rdev) || (lsb->st_size != fsb->st_size)) {
423 /* something's fishy */
430 static int dotlock_prepare (char *bn, size_t l, const char *f, int _fd)
432 struct stat fsb, lsb;
433 char realpath[_POSIX_PATH_MAX];
434 char *basename, *dirname;
439 if (dotlock_deference_symlink (realpath, sizeof (realpath), f) == -1)
442 if ((p = strrchr (realpath, '/'))) {
452 if (strlen (basename) + 1 > l)
455 strfcpy (bn, basename, l);
457 if (chdir (dirname) == -1)
462 else if ((fd = open (basename, O_RDONLY)) == -1)
465 r = fstat (fd, &fsb);
473 if (lstat (basename, &lsb) == -1)
476 if (dotlock_check_stats (&fsb, &lsb) == -1)
483 * Expand a symbolic link.
485 * This function expects newpath to have space for
486 * at least _POSIX_PATH_MAX characters.
491 dotlock_expand_link (char *newpath, const char *path, const char *link)
493 const char *lb = NULL;
496 /* link is full path */
498 strfcpy (newpath, link, _POSIX_PATH_MAX);
502 if ((lb = strrchr (path, '/')) == NULL) {
503 /* no path in link */
504 strfcpy (newpath, link, _POSIX_PATH_MAX);
509 memcpy (newpath, path, len);
510 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
515 * Deference a chain of symbolic links
517 * The final path is written to d.
521 static int dotlock_deference_symlink (char *d, size_t l, const char *path)
524 char realpath[_POSIX_PATH_MAX];
525 const char *pathptr = path;
528 while (count++ < MAXLINKS) {
529 if (lstat (pathptr, &sb) == -1) {
530 /* perror (pathptr); */
534 if (S_ISLNK (sb.st_mode)) {
535 char linkfile[_POSIX_PATH_MAX];
536 char linkpath[_POSIX_PATH_MAX];
539 if ((len = readlink (pathptr, linkfile, sizeof (linkfile))) == -1) {
540 /* perror (pathptr); */
544 linkfile[len] = '\0';
545 dotlock_expand_link (linkpath, pathptr, linkfile);
546 strfcpy (realpath, linkpath, sizeof (realpath));
553 strfcpy (d, pathptr, l);
560 * realpath is assumed _not_ to be an absolute path to
561 * the file we are about to lock. Invoke
562 * dotlock_prepare () before using this function!
566 #define HARDMAXATTEMPTS 10
568 static int dotlock_lock (const char *realpath)
570 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
571 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
572 size_t prev_size = 0;
579 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
580 realpath, Hostname, (int) getpid ());
581 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
586 unlink (nfslockfile);
588 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0) {
592 if (errno != EAGAIN) {
593 /* perror ("cannot open NFS lock file"); */
606 while (hard_count++ < HARDMAXATTEMPTS) {
609 link (nfslockfile, lockfile);
612 if (stat (nfslockfile, &sb) != 0) {
613 /* perror ("stat"); */
617 if (sb.st_nlink == 2)
621 prev_size = sb.st_size;
623 if (prev_size == sb.st_size && ++count > Retry) {
624 if (DotlockFlags & DL_FL_FORCE) {
634 unlink (nfslockfile);
640 prev_size = sb.st_size;
642 /* don't trust sleep (3) as it may be interrupted
643 * by users sending signals.
649 } while (time (NULL) == t);
653 unlink (nfslockfile);
663 * The same comment as for dotlock_lock () applies here.
667 static int dotlock_unlock (const char *realpath)
669 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
672 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
675 i = unlink (lockfile);
684 /* remove an empty file */
686 static int dotlock_unlink (const char *realpath)
691 if (dotlock_lock (realpath) != DL_EX_OK)
694 if ((i = lstat (realpath, &lsb)) == 0 && lsb.st_size == 0)
697 dotlock_unlock (realpath);
699 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
704 * Check if a file can be locked at all.
706 * The same comment as for dotlock_lock () applies here.
710 static int dotlock_try (void)
716 if (access (".", W_OK) == 0)
720 if (stat (".", &sb) == 0) {
721 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
722 return DL_EX_NEED_PRIVS;
726 return DL_EX_IMPOSSIBLE;