1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Mailbox file locking.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1996 Christos Zoulas. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Christos Zoulas.
21 * 4. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef HAVE_AMALGAMATION
40 #include <sys/utsname.h>
44 #define APID_SZ 40 /* sufficient for 128 bits pids XXX nail.h */
45 #define CREATE_RETRIES 5 /* XXX nail.h */
46 #define DOTLOCK_RETRIES 15 /* XXX nail.h */
49 # define O_BITS (O_WRONLY | O_CREAT | O_TRUNC | O_EXCL | O_SYNC)
51 # define O_BITS (O_WRONLY | O_CREAT | O_TRUNC | O_EXCL)
54 /* TODO Allow safe setgid, optional: check on startup wether in receive mode,
55 * TODO start helper process that is setgid and only does dotlocking.
56 * TODO Approach two, also optional: use a configurable setgid dotlock prog */
57 #define GID_MAYBESET(P) \
58 do if (realgid != effectivegid && !_maybe_setgid(P, effectivegid)) {\
64 do if (realgid != effectivegid && setgid(realgid) == -1) {\
69 /* GID_*() helper: set the gid if the path is in the normal mail spool */
70 static bool_t
_maybe_setgid(char const *name
, gid_t gid
);
72 /* Check if we can write a lock file at all */
73 static int maildir_access(char const *fname
);
75 /* Create a unique file. O_EXCL does not really work over NFS so we follow
76 * the following trick (inspired by S.R. van den Berg):
77 * - make a mostly unique filename and try to create it
78 * - link the unique filename to our target
79 * - get the link count of the target
80 * - unlink the mostly unique filename
81 * - if the link count was 2, then we are ok; else we've failed */
82 static int create_exclusive(char const *fname
);
85 _maybe_setgid(char const *name
, gid_t gid
)
87 char const safepath
[] = MAILSPOOL
;
91 if (strncmp(name
, safepath
, sizeof(safepath
) -1) ||
92 strchr(name
+ sizeof(safepath
), '/') != NULL
)
95 rv
= (setgid(gid
) != -1);
101 maildir_access(char const *fname
)
107 i
= (int)strlen(fname
);
108 path
= ac_alloc(i
+ 1 +1);
109 memcpy(path
, fname
, i
+1);
110 p
= strrchr(path
, '/');
113 if (p
== NULL
|| *path
== '\0') {
117 i
= access(path
, R_OK
| W_OK
| X_OK
);
124 create_exclusive(char const *fname
) /* TODO revisit! */
126 char path
[PATH_MAX
], apid
[APID_SZ
], *hostname
;
132 int pid
, fd
, serrno
, cc
;
137 hostname
= ut
.nodename
;
140 /* We generate a semi-unique filename, from hostname.(pid ^ usec) */
141 if ((ptr
= strrchr(fname
, '/')) == NULL
)
146 snprintf(path
, sizeof path
, "%.*s.%s.%x",
147 (int)PTR2SIZE(ptr
- fname
), fname
, hostname
, (pid
^ (int)t
));
149 /* We try to create the unique filename */
150 for (ntries
= 0; ntries
< CREATE_RETRIES
; ++ntries
) {
152 fd
= open(path
, O_BITS
, 0);
156 snprintf(apid
, APID_SZ
, "%d", pid
);
157 write(fd
, apid
, strlen(apid
));
160 } else if (serrno
!= EEXIST
) {
166 /* We link the path to the name */
168 cc
= link(path
, fname
);
174 /* Note that we stat our own exclusively created name, not the
175 * destination, since the destination can be affected by others */
176 if (stat(path
, &st
) == -1) {
185 /* If the number of links was two (one for the unique file and one for
186 * the lock), we've won the race */
187 if (st
.st_nlink
!= 2) {
204 fcntl_lock(int fd
, enum flock_type ft
) /* TODO check callees */
211 case FLOCK_READ
: rv
= F_RDLCK
; break;
212 case FLOCK_WRITE
: rv
= F_WRLCK
; break;
214 case FLOCK_UNLOCK
: rv
= F_UNLCK
; break;
219 flp
.l_whence
= SEEK_SET
;
221 rv
= fcntl(fd
, F_SETLKW
, &flp
);
227 dot_lock(char const *fname
, int fd
, int pollival
, FILE *fp
, char const *msg
)
235 if (maildir_access(fname
) != 0)
240 sigaddset(&nset
, SIGHUP
);
241 sigaddset(&nset
, SIGINT
);
242 sigaddset(&nset
, SIGQUIT
);
243 sigaddset(&nset
, SIGTERM
);
244 sigaddset(&nset
, SIGTTIN
);
245 sigaddset(&nset
, SIGTTOU
);
246 sigaddset(&nset
, SIGTSTP
);
247 sigaddset(&nset
, SIGCHLD
);
249 snprintf(path
, sizeof(path
), "%s.lock", fname
);
251 for (i
= 0; i
< DOTLOCK_RETRIES
; ++i
) {
252 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
253 rv
= create_exclusive(path
);
255 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
260 fcntl_lock(fd
, FLOCK_UNLOCK
);
261 if (olderrno
!= EEXIST
)
264 if (fp
!= NULL
&& msg
!= NULL
)
268 if (pollival
== -1) {
274 fcntl_lock(fd
, FLOCK_WRITE
);
276 fprintf(stderr
, tr(71,
277 "%s seems a stale lock? Need to be removed by hand?\n"), path
);
284 dot_unlock(char const *fname
)
289 if (maildir_access(fname
) != 0)
292 snprintf(path
, sizeof(path
), "%s.lock", fname
);
300 /* vim:set fenc=utf-8:s-it-mode */