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.
11 #include <lib-lib/lib-lib.h>
13 #include <sys/utsname.h>
20 #define MAXLINKS 1024 /* maximum link depth */
22 # define LONG_STRING 1024
23 # define MAXLOCKATTEMPT 5
28 # define SETEGID setegid
30 # define SETEGID setgid
33 # define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK ? 1 : 0)
38 static int DotlockFlags;
39 static int Retry = MAXLOCKATTEMPT;
41 static char *Hostname;
48 static int dotlock_deference_symlink (char *, size_t, const char *);
49 static int dotlock_prepare (char *, ssize_t, const char *, int fd);
50 static int dotlock_check_stats (struct stat *, struct stat *);
51 static int dotlock_dispatch (const char *, int fd);
53 static int dotlock_init_privs (void);
54 static void usage (const char *);
56 static void dotlock_expand_link (char *, const char *, const char *);
57 static void BEGIN_PRIVILEGED (void);
58 static void END_PRIVILEGED (void);
60 /* These functions work on the current directory.
61 * Invoke dotlock_prepare () before and check their
65 static int dotlock_try (void);
66 static int dotlock_unlock (const char *);
67 static int dotlock_unlink (const char *);
68 static int dotlock_lock (const char *);
71 #define check_flags(a) \
72 if (a & (DL_FL_TRY|DL_FL_UNLOCK|DL_FL_UNLINK)) \
75 int main (int argc, char **argv)
79 struct utsname utsname;
81 /* first, drop privileges */
83 if (dotlock_init_privs () == -1)
87 /* determine the system's host name */
90 if (!(Hostname = strdup(utsname.nodename)))
92 if ((p = strchr (Hostname, '.')))
96 /* parse the command line options. */
99 while ((i = getopt (argc, argv, "dtfupr:")) != EOF) {
101 /* actions, mutually exclusive */
103 check_flags (DotlockFlags);
104 DotlockFlags |= DL_FL_TRY;
107 check_flags (DotlockFlags);
108 DotlockFlags |= DL_FL_UNLINK;
111 check_flags (DotlockFlags);
112 DotlockFlags |= DL_FL_UNLOCK;
117 DotlockFlags |= DL_FL_FORCE;
120 DotlockFlags |= DL_FL_USEPRIV;
123 DotlockFlags |= DL_FL_RETRY;
124 Retry = atoi (optarg);
132 if (optind == argc || Retry < 0)
135 return dotlock_dispatch (argv[optind], -1);
140 * Determine our effective group ID, and drop
145 * 0 - everything went fine
146 * -1 - we couldn't drop privileges.
151 static int dotlock_init_privs (void)
157 MailGid = getegid ();
159 if (SETEGID (UserGid) != 0)
168 static int dotlock_dispatch (const char *f, int fd)
170 char frealpath[_POSIX_PATH_MAX];
172 /* If dotlock_prepare () succeeds [return value == 0],
173 * realpath contains the basename of f, and we have
174 * successfully changed our working directory to
175 * `dirname $f`. Additionally, f has been opened for
176 * reading to verify that the user has at least read
177 * permissions on that file.
179 * For a more detailed explanation of all this, see the
180 * lengthy comment below.
183 if (dotlock_prepare (frealpath, sizeof (frealpath), f, fd) != 0)
186 /* Actually perform the locking operation. */
188 if (DotlockFlags & DL_FL_TRY)
189 return dotlock_try ();
190 else if (DotlockFlags & DL_FL_UNLOCK)
191 return dotlock_unlock (frealpath);
192 else if (DotlockFlags & DL_FL_UNLINK)
193 return dotlock_unlink (frealpath);
195 return dotlock_lock (frealpath);
202 * This function re-acquires the privileges we may have
203 * if the user told us to do so by giving the "-p"
204 * command line option.
206 * BEGIN_PRIVILEGES () won't return if an error occurs.
210 static void BEGIN_PRIVILEGED (void)
213 if (DotlockFlags & DL_FL_USEPRIV) {
214 if (SETEGID (MailGid) != 0) {
215 /* perror ("setegid"); */
225 * This function drops the group privileges we may have.
227 * END_PRIVILEGED () won't return if an error occurs.
231 static void END_PRIVILEGED (void)
234 if (DotlockFlags & DL_FL_USEPRIV) {
235 if (SETEGID (UserGid) != 0) {
236 /* perror ("setegid"); */
246 * This function doesn't return.
250 static void usage (const char *av0)
252 fprintf (stderr, "dotlock [Madmutt %s]\n", VERSION);
253 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n", av0);
258 "\n -u\t\tunlock" "\n -d\t\tunlink" "\n -p\t\tprivileged"
262 "\n -r <retries>\tRetry locking" "\n", stderr);
268 * Access checking: Let's avoid to lock other users' mail
269 * spool files if we aren't permitted to read them.
271 * Some simple-minded access (2) checking isn't sufficient
272 * here: The problem is that the user may give us a
273 * deeply nested path to a file which has the same name
274 * as the file he wants to lock, but different
275 * permissions, say, e.g.
276 * /tmp/lots/of/subdirs/var/spool/mail/root.
278 * He may then try to replace /tmp/lots/of/subdirs by a
279 * symbolic link to / after we have invoked access () to
280 * check the file's permissions. The lockfile we'd
281 * create or remove would then actually be
282 * /var/spool/mail/root.
284 * To avoid this attack, we proceed as follows:
286 * - First, follow symbolic links a la
287 * dotlock_deference_symlink ().
289 * - get the result's dirname.
291 * - chdir to this directory. If you can't, bail out.
293 * - try to open the file in question, only using its
294 * basename. If you can't, bail out.
296 * - fstat that file and compare the result to a
297 * subsequent lstat (only using the basename). If
298 * the comparison fails, bail out.
300 * dotlock_prepare () is invoked from main () directly
301 * after the command line parsing has been done.
305 * 0 - Evereything's fine. The program's new current
306 * directory is the contains the file to be locked.
307 * The string pointed to by bn contains the name of
308 * the file to be locked.
310 * -1 - Something failed. Don't continue.
315 static int dotlock_check_stats (struct stat *fsb, struct stat *lsb)
317 /* S_ISLNK (fsb->st_mode) should actually be impossible,
318 * but we may have mixed up the parameters somewhere.
322 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
325 if ((lsb->st_dev != fsb->st_dev) ||
326 (lsb->st_ino != fsb->st_ino) ||
327 (lsb->st_mode != fsb->st_mode) ||
328 (lsb->st_nlink != fsb->st_nlink) ||
329 (lsb->st_uid != fsb->st_uid) ||
330 (lsb->st_gid != fsb->st_gid) ||
331 (lsb->st_rdev != fsb->st_rdev) || (lsb->st_size != fsb->st_size)) {
332 /* something's fishy */
339 static int dotlock_prepare (char *bn, ssize_t l, const char *f, int _fd)
341 struct stat fsb, lsb;
342 char frealpath[_POSIX_PATH_MAX];
343 char *fbasename, *dirname;
348 if (dotlock_deference_symlink (frealpath, sizeof (frealpath), f) == -1)
351 if ((p = strrchr (frealpath, '/'))) {
357 fbasename = frealpath;
358 dirname = m_strdup(".");
361 if (m_strlen(fbasename) + 1 > l)
364 m_strcpy(bn, l, fbasename);
366 if (chdir (dirname) == -1)
371 else if ((fd = open (fbasename, O_RDONLY)) == -1)
374 r = fstat (fd, &fsb);
382 if (lstat (fbasename, &lsb) == -1)
385 if (dotlock_check_stats (&fsb, &lsb) == -1)
392 * Expand a symbolic link.
394 * This function expects newpath to have space for
395 * at least _POSIX_PATH_MAX characters.
400 dotlock_expand_link (char *newpath, const char *path, const char *flink)
402 const char *lb = NULL;
405 /* link is full path */
407 m_strcpy(newpath, _POSIX_PATH_MAX, flink);
411 if ((lb = strrchr (path, '/')) == NULL) {
412 /* no path in link */
413 m_strcpy(newpath, _POSIX_PATH_MAX, flink);
418 memcpy (newpath, path, len);
419 m_strcpy(newpath + len, _POSIX_PATH_MAX - len, flink);
424 * Deference a chain of symbolic links
426 * The final path is written to d.
430 static int dotlock_deference_symlink (char *d, size_t l, const char *path)
433 char frealpath[_POSIX_PATH_MAX];
434 const char *pathptr = path;
437 while (count++ < MAXLINKS) {
438 if (lstat (pathptr, &sb) == -1) {
439 /* perror (pathptr); */
443 if (S_ISLNK (sb.st_mode)) {
444 char linkfile[_POSIX_PATH_MAX];
445 char linkpath[_POSIX_PATH_MAX];
448 if ((len = readlink (pathptr, linkfile, sizeof (linkfile))) == -1) {
449 /* perror (pathptr); */
453 linkfile[len] = '\0';
454 dotlock_expand_link (linkpath, pathptr, linkfile);
455 m_strcpy(frealpath, sizeof(frealpath), linkpath);
462 m_strcpy(d, l, pathptr);
469 * realpath is assumed _not_ to be an absolute path to
470 * the file we are about to lock. Invoke
471 * dotlock_prepare () before using this function!
475 #define HARDMAXATTEMPTS 10
477 static int dotlock_lock (const char *frealpath)
479 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
480 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
481 ssize_t prev_size = 0;
488 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
489 frealpath, Hostname, (int) getpid ());
490 snprintf (lockfile, sizeof (lockfile), "%s.lock", frealpath);
495 unlink (nfslockfile);
497 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0) {
501 if (errno != EAGAIN) {
502 /* perror ("cannot open NFS lock file"); */
515 while (hard_count++ < HARDMAXATTEMPTS) {
518 link (nfslockfile, lockfile);
521 if (stat (nfslockfile, &sb) != 0) {
522 /* perror ("stat"); */
526 if (sb.st_nlink == 2)
530 prev_size = sb.st_size;
532 if (prev_size == sb.st_size && ++count > Retry) {
533 if (DotlockFlags & DL_FL_FORCE) {
543 unlink (nfslockfile);
549 prev_size = sb.st_size;
551 /* don't trust sleep (3) as it may be interrupted
552 * by users sending signals.
558 } while (time (NULL) == t);
562 unlink (nfslockfile);
572 * The same comment as for dotlock_lock () applies here.
576 static int dotlock_unlock (const char *frealpath)
578 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
581 snprintf (lockfile, sizeof (lockfile), "%s.lock", frealpath);
584 i = unlink (lockfile);
593 /* remove an empty file */
595 static int dotlock_unlink (const char *frealpath)
600 if (dotlock_lock (frealpath) != DL_EX_OK)
603 if ((i = lstat (frealpath, &lsb)) == 0 && lsb.st_size == 0)
606 dotlock_unlock (frealpath);
608 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
613 * Check if a file can be locked at all.
615 * The same comment as for dotlock_lock () applies here.
619 static int dotlock_try (void)
625 if (access (".", W_OK) == 0)
629 if (stat (".", &sb) == 0) {
630 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
631 return DL_EX_NEED_PRIVS;
635 return DL_EX_IMPOSSIBLE;