2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1998-2000 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., 59 Temple Place - Suite 330, Boston, MA 02111, 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
46 #include <posix1_lim.h>
60 #define MAXLINKS 1024 /* maximum link depth */
64 # define LONG_STRING 1024
65 # define MAXLOCKATTEMPT 5
67 # define strfcpy(A,B,C) strncpy (A,B,C), *(A+(C)-1)=0
72 # define SETEGID setegid
74 # define SETEGID setgid
77 # define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK ? 1 : 0)
82 # ifndef HAVE_SNPRINTF
83 extern int snprintf (char *, size_t, const char *, ...);
86 #else /* DL_STANDALONE */
89 # error Do not try to compile dotlock as a mutt module when requiring egid switching!
95 #endif /* DL_STANDALONE */
97 static int DotlockFlags;
98 static int Retry = MAXLOCKATTEMPT;
101 static char *Hostname;
105 static gid_t UserGid;
106 static gid_t MailGid;
109 static int dotlock_deference_symlink (char *, size_t, const char *);
110 static int dotlock_prepare (char *, size_t, const char *, int fd);
111 static int dotlock_check_stats (struct stat *, struct stat *);
112 static int dotlock_dispatch (const char *, int fd);
115 static int dotlock_init_privs (void);
116 static void usage (const char *);
119 static void dotlock_expand_link (char *, const char *, const char *);
120 static void BEGIN_PRIVILEGED (void);
121 static void END_PRIVILEGED (void);
123 /* These functions work on the current directory.
124 * Invoke dotlock_prepare () before and check their
128 static int dotlock_try (void);
129 static int dotlock_unlock (const char *);
130 static int dotlock_unlink (const char *);
131 static int dotlock_lock (const char *);
136 #define check_flags(a) if (a & DL_FL_ACTIONS) usage (argv[0])
138 int main (int argc, char **argv)
142 struct utsname utsname;
144 /* first, drop privileges */
146 if (dotlock_init_privs () == -1)
150 /* determine the system's host name */
153 if (!(Hostname = strdup (utsname.nodename))) /* __MEM_CHECKED__ */
155 if ((p = strchr (Hostname, '.')))
159 /* parse the command line options. */
162 while ((i = getopt (argc, argv, "dtfupr:")) != EOF) {
164 /* actions, mutually exclusive */
166 check_flags (DotlockFlags);
167 DotlockFlags |= DL_FL_TRY;
170 check_flags (DotlockFlags);
171 DotlockFlags |= DL_FL_UNLINK;
174 check_flags (DotlockFlags);
175 DotlockFlags |= DL_FL_UNLOCK;
180 DotlockFlags |= DL_FL_FORCE;
183 DotlockFlags |= DL_FL_USEPRIV;
186 DotlockFlags |= DL_FL_RETRY;
187 Retry = atoi (optarg);
195 if (optind == argc || Retry < 0)
198 return dotlock_dispatch (argv[optind], -1);
203 * Determine our effective group ID, and drop
208 * 0 - everything went fine
209 * -1 - we couldn't drop privileges.
214 static int dotlock_init_privs (void)
220 MailGid = getegid ();
222 if (SETEGID (UserGid) != 0)
231 #else /* DL_STANDALONE */
234 * This function is intended to be invoked from within
235 * mutt instead of mx.c's invoke_dotlock ().
238 int dotlock_invoke (const char *path, int fd, int flags, int retry)
243 DotlockFlags = flags;
245 if ((currdir = open (".", O_RDONLY)) == -1)
248 if (!(DotlockFlags & DL_FL_RETRY) || retry)
249 Retry = MAXLOCKATTEMPT;
253 r = dotlock_dispatch (path, fd);
261 #endif /* DL_STANDALONE */
264 static int dotlock_dispatch (const char *f, int fd)
266 char realpath[_POSIX_PATH_MAX];
268 /* If dotlock_prepare () succeeds [return value == 0],
269 * realpath contains the basename of f, and we have
270 * successfully changed our working directory to
271 * `dirname $f`. Additionally, f has been opened for
272 * reading to verify that the user has at least read
273 * permissions on that file.
275 * For a more detailed explanation of all this, see the
276 * lengthy comment below.
279 if (dotlock_prepare (realpath, sizeof (realpath), f, fd) != 0)
282 /* Actually perform the locking operation. */
284 if (DotlockFlags & DL_FL_TRY)
285 return dotlock_try ();
286 else if (DotlockFlags & DL_FL_UNLOCK)
287 return dotlock_unlock (realpath);
288 else if (DotlockFlags & DL_FL_UNLINK)
289 return dotlock_unlink (realpath);
291 return dotlock_lock (realpath);
298 * This function re-acquires the privileges we may have
299 * if the user told us to do so by giving the "-p"
300 * command line option.
302 * BEGIN_PRIVILEGES () won't return if an error occurs.
306 static void BEGIN_PRIVILEGED (void)
309 if (DotlockFlags & DL_FL_USEPRIV) {
310 if (SETEGID (MailGid) != 0) {
311 /* perror ("setegid"); */
321 * This function drops the group privileges we may have.
323 * END_PRIVILEGED () won't return if an error occurs.
327 static void END_PRIVILEGED (void)
330 if (DotlockFlags & DL_FL_USEPRIV) {
331 if (SETEGID (UserGid) != 0) {
332 /* perror ("setegid"); */
344 * This function doesn't return.
348 static void usage (const char *av0)
350 fprintf (stderr, "dotlock [Mutt-ng %s (%s)]\n", VERSION, ReleaseDate);
351 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n", av0);
356 "\n -u\t\tunlock" "\n -d\t\tunlink" "\n -p\t\tprivileged"
360 "\n -r <retries>\tRetry locking" "\n", stderr);
368 * Access checking: Let's avoid to lock other users' mail
369 * spool files if we aren't permitted to read them.
371 * Some simple-minded access (2) checking isn't sufficient
372 * here: The problem is that the user may give us a
373 * deeply nested path to a file which has the same name
374 * as the file he wants to lock, but different
375 * permissions, say, e.g.
376 * /tmp/lots/of/subdirs/var/spool/mail/root.
378 * He may then try to replace /tmp/lots/of/subdirs by a
379 * symbolic link to / after we have invoked access () to
380 * check the file's permissions. The lockfile we'd
381 * create or remove would then actually be
382 * /var/spool/mail/root.
384 * To avoid this attack, we proceed as follows:
386 * - First, follow symbolic links a la
387 * dotlock_deference_symlink ().
389 * - get the result's dirname.
391 * - chdir to this directory. If you can't, bail out.
393 * - try to open the file in question, only using its
394 * basename. If you can't, bail out.
396 * - fstat that file and compare the result to a
397 * subsequent lstat (only using the basename). If
398 * the comparison fails, bail out.
400 * dotlock_prepare () is invoked from main () directly
401 * after the command line parsing has been done.
405 * 0 - Evereything's fine. The program's new current
406 * directory is the contains the file to be locked.
407 * The string pointed to by bn contains the name of
408 * the file to be locked.
410 * -1 - Something failed. Don't continue.
415 static int dotlock_check_stats (struct stat *fsb, struct stat *lsb)
417 /* S_ISLNK (fsb->st_mode) should actually be impossible,
418 * but we may have mixed up the parameters somewhere.
422 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
425 if ((lsb->st_dev != fsb->st_dev) ||
426 (lsb->st_ino != fsb->st_ino) ||
427 (lsb->st_mode != fsb->st_mode) ||
428 (lsb->st_nlink != fsb->st_nlink) ||
429 (lsb->st_uid != fsb->st_uid) ||
430 (lsb->st_gid != fsb->st_gid) ||
431 (lsb->st_rdev != fsb->st_rdev) || (lsb->st_size != fsb->st_size)) {
432 /* something's fishy */
439 static int 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, '/'))) {
461 if (strlen (basename) + 1 > l)
464 strfcpy (bn, basename, l);
466 if (chdir (dirname) == -1)
471 else if ((fd = open (basename, O_RDONLY)) == -1)
474 r = fstat (fd, &fsb);
482 if (lstat (basename, &lsb) == -1)
485 if (dotlock_check_stats (&fsb, &lsb) == -1)
492 * Expand a symbolic link.
494 * This function expects newpath to have space for
495 * at least _POSIX_PATH_MAX characters.
500 dotlock_expand_link (char *newpath, const char *path, const char *link)
502 const char *lb = NULL;
505 /* link is full path */
507 strfcpy (newpath, link, _POSIX_PATH_MAX);
511 if ((lb = strrchr (path, '/')) == NULL) {
512 /* no path in link */
513 strfcpy (newpath, link, _POSIX_PATH_MAX);
518 memcpy (newpath, path, len);
519 strfcpy (newpath + len, link, _POSIX_PATH_MAX - len);
524 * Deference a chain of symbolic links
526 * The final path is written to d.
530 static int dotlock_deference_symlink (char *d, size_t l, const char *path)
533 char realpath[_POSIX_PATH_MAX];
534 const char *pathptr = path;
537 while (count++ < MAXLINKS) {
538 if (lstat (pathptr, &sb) == -1) {
539 /* perror (pathptr); */
543 if (S_ISLNK (sb.st_mode)) {
544 char linkfile[_POSIX_PATH_MAX];
545 char linkpath[_POSIX_PATH_MAX];
548 if ((len = readlink (pathptr, linkfile, sizeof (linkfile))) == -1) {
549 /* perror (pathptr); */
553 linkfile[len] = '\0';
554 dotlock_expand_link (linkpath, pathptr, linkfile);
555 strfcpy (realpath, linkpath, sizeof (realpath));
562 strfcpy (d, pathptr, l);
569 * realpath is assumed _not_ to be an absolute path to
570 * the file we are about to lock. Invoke
571 * dotlock_prepare () before using this function!
575 #define HARDMAXATTEMPTS 10
577 static int dotlock_lock (const char *realpath)
579 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
580 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
581 size_t prev_size = 0;
588 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
589 realpath, Hostname, (int) getpid ());
590 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
595 unlink (nfslockfile);
597 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0) {
601 if (errno != EAGAIN) {
602 /* perror ("cannot open NFS lock file"); */
615 while (hard_count++ < HARDMAXATTEMPTS) {
618 link (nfslockfile, lockfile);
621 if (stat (nfslockfile, &sb) != 0) {
622 /* perror ("stat"); */
626 if (sb.st_nlink == 2)
630 prev_size = sb.st_size;
632 if (prev_size == sb.st_size && ++count > Retry) {
633 if (DotlockFlags & DL_FL_FORCE) {
643 unlink (nfslockfile);
649 prev_size = sb.st_size;
651 /* don't trust sleep (3) as it may be interrupted
652 * by users sending signals.
658 } while (time (NULL) == t);
662 unlink (nfslockfile);
672 * The same comment as for dotlock_lock () applies here.
676 static int dotlock_unlock (const char *realpath)
678 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
681 snprintf (lockfile, sizeof (lockfile), "%s.lock", realpath);
684 i = unlink (lockfile);
693 /* remove an empty file */
695 static int dotlock_unlink (const char *realpath)
700 if (dotlock_lock (realpath) != DL_EX_OK)
703 if ((i = lstat (realpath, &lsb)) == 0 && lsb.st_size == 0)
706 dotlock_unlock (realpath);
708 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
713 * Check if a file can be locked at all.
715 * The same comment as for dotlock_lock () applies here.
719 static int dotlock_try (void)
725 if (access (".", W_OK) == 0)
729 if (stat (".", &sb) == 0) {
730 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
731 return DL_EX_NEED_PRIVS;
735 return DL_EX_IMPOSSIBLE;