2 * Copyright (c) 2008-2014, Simon Schubert <2@0x2c.org>.
3 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
6 * by Simon Schubert <2@0x2c.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/param.h>
57 #define HOST_NAME_MAX 255
59 static char name[HOST_NAME_MAX+1];
60 static int initialized = 0;
66 if (config.mailname == NULL || !*config.mailname)
69 if (config.mailname[0] == '/') {
71 * If the mailname looks like an absolute path,
76 fp = fopen(config.mailname, "r");
80 s = fgets(name, sizeof(name), fp);
85 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
95 snprintf(name, sizeof(name), "%s", config.mailname);
101 if (gethostname(name, sizeof(name)) != 0)
104 * gethostname() is allowed to truncate name without NUL-termination
105 * and at the same time not return an error.
107 name[sizeof(name) - 1] = 0;
109 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
114 snprintf(name, sizeof(name), "unknown-hostname");
121 setlogident(const char *fmt, ...)
125 snprintf(tag, sizeof(tag), "%s", logident_base);
131 vsnprintf(sufx, sizeof(sufx), fmt, ap);
133 snprintf(tag, sizeof(tag), "%s[%s]", logident_base, sufx);
136 openlog(tag, 0, LOG_MAIL);
140 errlog(int exitcode, const char *fmt, ...)
144 char outs[ERRMSG_SIZE];
149 vsnprintf(outs, sizeof(outs), fmt, ap);
155 syslog(LOG_ERR, "%s: %m", outs);
156 fprintf(stderr, "%s: %s: %s\n", getprogname(), outs, strerror(oerrno));
158 syslog(LOG_ERR, "%m");
159 fprintf(stderr, "%s: %s\n", getprogname(), strerror(oerrno));
166 errlogx(int exitcode, const char *fmt, ...)
169 char outs[ERRMSG_SIZE];
174 vsnprintf(outs, sizeof(outs), fmt, ap);
179 syslog(LOG_ERR, "%s", outs);
180 fprintf(stderr, "%s: %s\n", getprogname(), outs);
182 syslog(LOG_ERR, "Unknown error");
183 fprintf(stderr, "%s: Unknown error\n", getprogname());
190 check_username(const char *name, uid_t ckuid)
196 pwd = getpwnam(name);
197 if (pwd == NULL || pwd->pw_uid != ckuid)
199 snprintf(username, sizeof(username), "%s", name);
209 if (check_username(getlogin(), useruid))
211 if (check_username(getenv("LOGNAME"), useruid))
213 if (check_username(getenv("USER"), useruid))
215 pwd = getpwuid(useruid);
216 if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0] != '\0') {
217 if (check_username(pwd->pw_name, useruid))
220 snprintf(username, sizeof(username), "uid=%ld", (long)useruid);
228 SLIST_FOREACH(t, &tmpfs, next) {
233 static sigjmp_buf sigbuf;
234 static int sigbuf_valid;
237 sigalrm_handler(int signo)
239 (void)signo; /* so that gcc doesn't complain */
241 siglongjmp(sigbuf, 1);
245 do_timeout(int timeout, int dojmp)
247 struct sigaction act;
250 sigemptyset(&act.sa_mask);
254 act.sa_handler = sigalrm_handler;
255 if (sigaction(SIGALRM, &act, NULL) != 0)
256 syslog(LOG_WARNING, "can not set signal handler: %m");
258 ret = sigsetjmp(sigbuf, 1);
261 /* else just programmed */
270 act.sa_handler = SIG_IGN;
271 if (sigaction(SIGALRM, &act, NULL) != 0)
272 syslog(LOG_WARNING, "can not remove signal handler: %m");
280 open_locked(const char *fname, int flags, ...)
284 if (flags & O_CREAT) {
287 mode = va_arg(ap, int);
294 fd = open(fname, flags, mode);
297 if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
305 return(open(fname, flags|O_EXLOCK, mode));
317 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
320 strcpy(str, "(date fail)");
325 strprefixcmp(const char *str, const char *prefix)
327 return (strncasecmp(str, prefix, strlen(prefix)));
336 rf = open("/dev/urandom", O_RDONLY);
338 rf = open("/dev/random", O_RDONLY);
340 if (!(rf != -1 && read(rf, &seed, sizeof(seed)) == sizeof(seed)))
341 seed = (time(NULL) ^ getpid()) + (uintptr_t)&seed;