1*da07b1a3Smillert /* $OpenBSD: locking.c,v 1.15 2020/05/27 03:12:06 millert Exp $ */
209850d41Smillert
309850d41Smillert /*
409850d41Smillert * Copyright (c) 1996-1998 Theo de Raadt <deraadt@theos.com>
509850d41Smillert * Copyright (c) 1996-1998 David Mazieres <dm@lcs.mit.edu>
609850d41Smillert * All rights reserved.
709850d41Smillert *
809850d41Smillert * Redistribution and use in source and binary forms, with or without
909850d41Smillert * modification, are permitted provided that the following conditions
1009850d41Smillert * are met:
1109850d41Smillert * 1. Redistributions of source code must retain the above copyright
1209850d41Smillert * notice, this list of conditions and the following disclaimer.
1309850d41Smillert * 2. Redistributions in binary form must reproduce the above copyright
1409850d41Smillert * notice, this list of conditions and the following disclaimer in the
1509850d41Smillert * documentation and/or other materials provided with the distribution.
1609850d41Smillert * 3. The name of the authors may not be used to endorse or promote products
1709850d41Smillert * derived from this software without specific prior written permission.
1809850d41Smillert *
1909850d41Smillert * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
2009850d41Smillert * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
2109850d41Smillert * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
2209850d41Smillert * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2309850d41Smillert * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2409850d41Smillert * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
2509850d41Smillert * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2609850d41Smillert * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
2709850d41Smillert * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
2809850d41Smillert * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2909850d41Smillert */
3009850d41Smillert
31b9fc9a72Sderaadt #include <sys/types.h>
3209850d41Smillert #include <sys/stat.h>
3309850d41Smillert #include <fcntl.h>
3409850d41Smillert #include <pwd.h>
3509850d41Smillert #include <syslog.h>
3609850d41Smillert #include <unistd.h>
37b9fc9a72Sderaadt #include <limits.h>
3809850d41Smillert #include <errno.h>
3909850d41Smillert #include <stdio.h>
4070ef01f6Sdavid #include <stdlib.h>
4109850d41Smillert #include <string.h>
42e7beb4a7Smillert #include <stdarg.h>
4309850d41Smillert #include "pathnames.h"
4409850d41Smillert #include "mail.local.h"
4509850d41Smillert
46b9fc9a72Sderaadt static char lpath[PATH_MAX];
4709850d41Smillert
4809850d41Smillert void
rellock(void)495b48e80aSderaadt rellock(void)
5009850d41Smillert {
5109850d41Smillert
5209850d41Smillert if (lpath[0])
5309850d41Smillert unlink(lpath);
5409850d41Smillert }
5509850d41Smillert
5609850d41Smillert int
getlock(const char * name,struct passwd * pw)57c711e483Smillert getlock(const char *name, struct passwd *pw)
5809850d41Smillert {
59*da07b1a3Smillert struct stat sb;
6009850d41Smillert int lfd=-1;
6109850d41Smillert int tries = 0;
6209850d41Smillert
6309850d41Smillert (void)snprintf(lpath, sizeof lpath, "%s/%s.lock",
6409850d41Smillert _PATH_MAILDIR, name);
6509850d41Smillert
6609850d41Smillert if (stat(_PATH_MAILDIR, &sb) != -1 &&
6709850d41Smillert (sb.st_mode & S_IWOTH) == S_IWOTH) {
68*da07b1a3Smillert mwarn("%s: will not deliver to world-writable spool",
69*da07b1a3Smillert _PATH_MAILDIR);
7009850d41Smillert } else {
7109850d41Smillert /*
7209850d41Smillert * Only root can write the spool directory.
7309850d41Smillert */
7409850d41Smillert while (1) {
7509850d41Smillert if ((lfd = open(lpath, O_CREAT|O_WRONLY|O_EXCL,
7609850d41Smillert S_IRUSR|S_IWUSR)) != -1)
7709850d41Smillert break;
7809850d41Smillert if (tries > 9) {
79e1869fb3Smillert mwarn("%s: %s", lpath, strerror(errno));
8009850d41Smillert return(-1);
8109850d41Smillert }
82668b2622Sderaadt sleep(1U << tries);
8309850d41Smillert tries++;
8409850d41Smillert }
8509850d41Smillert }
8609850d41Smillert return(lfd);
8709850d41Smillert }
8809850d41Smillert
8909850d41Smillert void
mwarn(const char * fmt,...)90e1869fb3Smillert mwarn(const char *fmt, ...)
9109850d41Smillert {
9209850d41Smillert va_list ap;
93e7beb4a7Smillert
9409850d41Smillert va_start(ap, fmt);
9509850d41Smillert vsyslog(LOG_ERR, fmt, ap);
9609850d41Smillert va_end(ap);
97e1869fb3Smillert }
98e1869fb3Smillert
99e1869fb3Smillert void
merr(int eval,const char * fmt,...)100e1869fb3Smillert merr(int eval, const char *fmt, ...)
101e1869fb3Smillert {
102e1869fb3Smillert va_list ap;
103e1869fb3Smillert
104e1869fb3Smillert va_start(ap, fmt);
105e1869fb3Smillert vsyslog(LOG_ERR, fmt, ap);
106e1869fb3Smillert va_end(ap);
107e1869fb3Smillert exit(eval);
10809850d41Smillert }
109