xref: /openbsd-src/libexec/mail.local/locking.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: locking.c,v 1.10 2011/01/10 21:00:50 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 1996-1998 Theo de Raadt <deraadt@theos.com>
5  * Copyright (c) 1996-1998 David Mazieres <dm@lcs.mit.edu>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the authors may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
22  * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <pwd.h>
35 #include <syslog.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include "pathnames.h"
44 #include "mail.local.h"
45 
46 static char lpath[MAXPATHLEN];
47 
48 void
49 rellock(void)
50 {
51 
52 	if (lpath[0])
53 		unlink(lpath);
54 }
55 
56 int
57 getlock(char *name, struct passwd *pw)
58 {
59 	struct stat sb, fsb;
60 	int lfd=-1;
61 	char buf[8*1024];
62 	int tries = 0;
63 
64 	(void)snprintf(lpath, sizeof lpath, "%s/%s.lock",
65 	    _PATH_MAILDIR, name);
66 
67 	if (stat(_PATH_MAILDIR, &sb) != -1 &&
68 	    (sb.st_mode & S_IWOTH) == S_IWOTH) {
69 		/*
70 		 * We have a writeable spool, deal with it as
71 		 * securely as possible.
72 		 */
73 		time_t ctim = -1;
74 
75 		seteuid(pw->pw_uid);
76 		if (lstat(lpath, &sb) != -1)
77 			ctim = sb.st_ctime;
78 		while (1) {
79 			/*
80 			 * Deal with existing user.lock files
81 			 * or directories or symbolic links that
82 			 * should not be here.
83 			 */
84 			if (readlink(lpath, buf, sizeof buf-1) != -1) {
85 				if (lstat(lpath, &sb) != -1 &&
86 				    S_ISLNK(sb.st_mode)) {
87 					seteuid(sb.st_uid);
88 					unlink(lpath);
89 					seteuid(pw->pw_uid);
90 				}
91 				goto again;
92 			}
93 			if ((lfd = open(lpath, O_CREAT|O_WRONLY|O_EXCL|O_EXLOCK,
94 			    S_IRUSR|S_IWUSR)) != -1)
95 				break;
96 again:
97 			if (tries > 10) {
98 				merr(NOTFATAL, "%s: %s", lpath,
99 				    strerror(errno));
100 				seteuid(0);
101 				return(-1);
102 			}
103 			if (tries > 9 &&
104 			    (lfd = open(lpath, O_WRONLY|O_EXLOCK, 0)) != -1) {
105 				if (fstat(lfd, &fsb) != -1 &&
106 				    lstat(lpath, &sb) != -1) {
107 					if (fsb.st_dev == sb.st_dev &&
108 					    fsb.st_ino == sb.st_ino &&
109 					    ctim == fsb.st_ctime ) {
110 						seteuid(fsb.st_uid);
111 						baditem(lpath);
112 						seteuid(pw->pw_uid);
113 					}
114 				}
115 			}
116 			sleep(1U << tries);
117 			tries++;
118 			continue;
119 		}
120 		seteuid(0);
121 	} else {
122 		/*
123 		 * Only root can write the spool directory.
124 		 */
125 		while (1) {
126 			if ((lfd = open(lpath, O_CREAT|O_WRONLY|O_EXCL,
127 			    S_IRUSR|S_IWUSR)) != -1)
128 				break;
129 			if (tries > 9) {
130 				merr(NOTFATAL, "%s: %s", lpath, strerror(errno));
131 				return(-1);
132 			}
133 			sleep(1U << tries);
134 			tries++;
135 		}
136 	}
137 	return(lfd);
138 }
139 
140 void
141 baditem(char *path)
142 {
143 	char npath[MAXPATHLEN];
144 	int fd;
145 
146 	if (unlink(path) == 0)
147 		return;
148 	snprintf(npath, sizeof npath, "%s/mailXXXXXXXXXX", _PATH_MAILDIR);
149 	if ((fd = mkstemp(npath)) == -1)
150 		return;
151 	close(fd);
152 	if (rename(path, npath) == -1)
153 		unlink(npath);
154 	else
155 		merr(NOTFATAL, "nasty spool item %s renamed to %s",
156 		    path, npath);
157 	/* XXX if we fail to rename, another attempt will happen later */
158 }
159 
160 void
161 merr(int isfatal, const char *fmt, ...)
162 {
163 	va_list ap;
164 
165 	va_start(ap, fmt);
166 	vsyslog(LOG_ERR, fmt, ap);
167 	va_end(ap);
168 	if (isfatal)
169 		exit(1);
170 }
171