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) if (a & DL_FL_ACTIONS) usage (argv[0])
73 int main (int argc, char **argv)
77 struct utsname utsname;
79 /* first, drop privileges */
81 if (dotlock_init_privs () == -1)
85 /* determine the system's host name */
88 if (!(Hostname = strdup(utsname.nodename)))
90 if ((p = strchr (Hostname, '.')))
94 /* parse the command line options. */
97 while ((i = getopt (argc, argv, "dtfupr:")) != EOF) {
99 /* actions, mutually exclusive */
101 check_flags (DotlockFlags);
102 DotlockFlags |= DL_FL_TRY;
105 check_flags (DotlockFlags);
106 DotlockFlags |= DL_FL_UNLINK;
109 check_flags (DotlockFlags);
110 DotlockFlags |= DL_FL_UNLOCK;
115 DotlockFlags |= DL_FL_FORCE;
118 DotlockFlags |= DL_FL_USEPRIV;
121 DotlockFlags |= DL_FL_RETRY;
122 Retry = atoi (optarg);
130 if (optind == argc || Retry < 0)
133 return dotlock_dispatch (argv[optind], -1);
138 * Determine our effective group ID, and drop
143 * 0 - everything went fine
144 * -1 - we couldn't drop privileges.
149 static int dotlock_init_privs (void)
155 MailGid = getegid ();
157 if (SETEGID (UserGid) != 0)
166 static int dotlock_dispatch (const char *f, int fd)
168 char frealpath[_POSIX_PATH_MAX];
170 /* If dotlock_prepare () succeeds [return value == 0],
171 * realpath contains the basename of f, and we have
172 * successfully changed our working directory to
173 * `dirname $f`. Additionally, f has been opened for
174 * reading to verify that the user has at least read
175 * permissions on that file.
177 * For a more detailed explanation of all this, see the
178 * lengthy comment below.
181 if (dotlock_prepare (frealpath, sizeof (frealpath), f, fd) != 0)
184 /* Actually perform the locking operation. */
186 if (DotlockFlags & DL_FL_TRY)
187 return dotlock_try ();
188 else if (DotlockFlags & DL_FL_UNLOCK)
189 return dotlock_unlock (frealpath);
190 else if (DotlockFlags & DL_FL_UNLINK)
191 return dotlock_unlink (frealpath);
193 return dotlock_lock (frealpath);
200 * This function re-acquires the privileges we may have
201 * if the user told us to do so by giving the "-p"
202 * command line option.
204 * BEGIN_PRIVILEGES () won't return if an error occurs.
208 static void BEGIN_PRIVILEGED (void)
211 if (DotlockFlags & DL_FL_USEPRIV) {
212 if (SETEGID (MailGid) != 0) {
213 /* perror ("setegid"); */
223 * This function drops the group privileges we may have.
225 * END_PRIVILEGED () won't return if an error occurs.
229 static void END_PRIVILEGED (void)
232 if (DotlockFlags & DL_FL_USEPRIV) {
233 if (SETEGID (UserGid) != 0) {
234 /* perror ("setegid"); */
244 * This function doesn't return.
248 static void usage (const char *av0)
250 fprintf (stderr, "dotlock [Madmutt %s]\n", VERSION);
251 fprintf (stderr, "usage: %s [-t|-f|-u|-d] [-p] [-r <retries>] file\n", av0);
256 "\n -u\t\tunlock" "\n -d\t\tunlink" "\n -p\t\tprivileged"
260 "\n -r <retries>\tRetry locking" "\n", stderr);
266 * Access checking: Let's avoid to lock other users' mail
267 * spool files if we aren't permitted to read them.
269 * Some simple-minded access (2) checking isn't sufficient
270 * here: The problem is that the user may give us a
271 * deeply nested path to a file which has the same name
272 * as the file he wants to lock, but different
273 * permissions, say, e.g.
274 * /tmp/lots/of/subdirs/var/spool/mail/root.
276 * He may then try to replace /tmp/lots/of/subdirs by a
277 * symbolic link to / after we have invoked access () to
278 * check the file's permissions. The lockfile we'd
279 * create or remove would then actually be
280 * /var/spool/mail/root.
282 * To avoid this attack, we proceed as follows:
284 * - First, follow symbolic links a la
285 * dotlock_deference_symlink ().
287 * - get the result's dirname.
289 * - chdir to this directory. If you can't, bail out.
291 * - try to open the file in question, only using its
292 * basename. If you can't, bail out.
294 * - fstat that file and compare the result to a
295 * subsequent lstat (only using the basename). If
296 * the comparison fails, bail out.
298 * dotlock_prepare () is invoked from main () directly
299 * after the command line parsing has been done.
303 * 0 - Evereything's fine. The program's new current
304 * directory is the contains the file to be locked.
305 * The string pointed to by bn contains the name of
306 * the file to be locked.
308 * -1 - Something failed. Don't continue.
313 static int dotlock_check_stats (struct stat *fsb, struct stat *lsb)
315 /* S_ISLNK (fsb->st_mode) should actually be impossible,
316 * but we may have mixed up the parameters somewhere.
320 if (S_ISLNK (lsb->st_mode) || S_ISLNK (fsb->st_mode))
323 if ((lsb->st_dev != fsb->st_dev) ||
324 (lsb->st_ino != fsb->st_ino) ||
325 (lsb->st_mode != fsb->st_mode) ||
326 (lsb->st_nlink != fsb->st_nlink) ||
327 (lsb->st_uid != fsb->st_uid) ||
328 (lsb->st_gid != fsb->st_gid) ||
329 (lsb->st_rdev != fsb->st_rdev) || (lsb->st_size != fsb->st_size)) {
330 /* something's fishy */
337 static int dotlock_prepare (char *bn, ssize_t l, const char *f, int _fd)
339 struct stat fsb, lsb;
340 char frealpath[_POSIX_PATH_MAX];
341 char *fbasename, *dirname;
346 if (dotlock_deference_symlink (frealpath, sizeof (frealpath), f) == -1)
349 if ((p = strrchr (frealpath, '/'))) {
355 fbasename = frealpath;
356 dirname = m_strdup(".");
359 if (m_strlen(fbasename) + 1 > l)
362 m_strcpy(bn, l, fbasename);
364 if (chdir (dirname) == -1)
369 else if ((fd = open (fbasename, O_RDONLY)) == -1)
372 r = fstat (fd, &fsb);
380 if (lstat (fbasename, &lsb) == -1)
383 if (dotlock_check_stats (&fsb, &lsb) == -1)
390 * Expand a symbolic link.
392 * This function expects newpath to have space for
393 * at least _POSIX_PATH_MAX characters.
398 dotlock_expand_link (char *newpath, const char *path, const char *flink)
400 const char *lb = NULL;
403 /* link is full path */
405 m_strcpy(newpath, _POSIX_PATH_MAX, flink);
409 if ((lb = strrchr (path, '/')) == NULL) {
410 /* no path in link */
411 m_strcpy(newpath, _POSIX_PATH_MAX, flink);
416 memcpy (newpath, path, len);
417 m_strcpy(newpath + len, _POSIX_PATH_MAX - len, flink);
422 * Deference a chain of symbolic links
424 * The final path is written to d.
428 static int dotlock_deference_symlink (char *d, size_t l, const char *path)
431 char frealpath[_POSIX_PATH_MAX];
432 const char *pathptr = path;
435 while (count++ < MAXLINKS) {
436 if (lstat (pathptr, &sb) == -1) {
437 /* perror (pathptr); */
441 if (S_ISLNK (sb.st_mode)) {
442 char linkfile[_POSIX_PATH_MAX];
443 char linkpath[_POSIX_PATH_MAX];
446 if ((len = readlink (pathptr, linkfile, sizeof (linkfile))) == -1) {
447 /* perror (pathptr); */
451 linkfile[len] = '\0';
452 dotlock_expand_link (linkpath, pathptr, linkfile);
453 m_strcpy(frealpath, sizeof(frealpath), linkpath);
460 m_strcpy(d, l, pathptr);
467 * realpath is assumed _not_ to be an absolute path to
468 * the file we are about to lock. Invoke
469 * dotlock_prepare () before using this function!
473 #define HARDMAXATTEMPTS 10
475 static int dotlock_lock (const char *frealpath)
477 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
478 char nfslockfile[_POSIX_PATH_MAX + LONG_STRING];
479 ssize_t prev_size = 0;
486 snprintf (nfslockfile, sizeof (nfslockfile), "%s.%s.%d",
487 frealpath, Hostname, (int) getpid ());
488 snprintf (lockfile, sizeof (lockfile), "%s.lock", frealpath);
493 unlink (nfslockfile);
495 while ((fd = open (nfslockfile, O_WRONLY | O_EXCL | O_CREAT, 0)) < 0) {
499 if (errno != EAGAIN) {
500 /* perror ("cannot open NFS lock file"); */
513 while (hard_count++ < HARDMAXATTEMPTS) {
516 link (nfslockfile, lockfile);
519 if (stat (nfslockfile, &sb) != 0) {
520 /* perror ("stat"); */
524 if (sb.st_nlink == 2)
528 prev_size = sb.st_size;
530 if (prev_size == sb.st_size && ++count > Retry) {
531 if (DotlockFlags & DL_FL_FORCE) {
541 unlink (nfslockfile);
547 prev_size = sb.st_size;
549 /* don't trust sleep (3) as it may be interrupted
550 * by users sending signals.
556 } while (time (NULL) == t);
560 unlink (nfslockfile);
570 * The same comment as for dotlock_lock () applies here.
574 static int dotlock_unlock (const char *frealpath)
576 char lockfile[_POSIX_PATH_MAX + LONG_STRING];
579 snprintf (lockfile, sizeof (lockfile), "%s.lock", frealpath);
582 i = unlink (lockfile);
591 /* remove an empty file */
593 static int dotlock_unlink (const char *frealpath)
598 if (dotlock_lock (frealpath) != DL_EX_OK)
601 if ((i = lstat (frealpath, &lsb)) == 0 && lsb.st_size == 0)
604 dotlock_unlock (frealpath);
606 return (i == 0) ? DL_EX_OK : DL_EX_ERROR;
611 * Check if a file can be locked at all.
613 * The same comment as for dotlock_lock () applies here.
617 static int dotlock_try (void)
623 if (access (".", W_OK) == 0)
627 if (stat (".", &sb) == 0) {
628 if ((sb.st_mode & S_IWGRP) == S_IWGRP && sb.st_gid == MailGid)
629 return DL_EX_NEED_PRIVS;
633 return DL_EX_IMPOSSIBLE;