2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/param.h>
55 #define HOST_NAME_MAX 255
57 static char name[HOST_NAME_MAX+1];
58 static int initialized = 0;
64 if (config.mailname == NULL || !*config.mailname)
67 if (config.mailname[0] == '/') {
69 * If the mailname looks like an absolute path,
74 fp = fopen(config.mailname, "r");
78 s = fgets(name, sizeof(name), fp);
83 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
93 snprintf(name, sizeof(name), "%s", config.mailname);
99 if (gethostname(name, sizeof(name)) != 0)
102 * gethostname() is allowed to truncate name without NUL-termination
103 * and at the same time not return an error.
105 name[sizeof(name) - 1] = 0;
107 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
112 snprintf(name, sizeof(name), "unknown-hostname");
119 setlogident(const char *fmt, ...)
123 snprintf(tag, sizeof(tag), "%s", logident_base);
129 vsnprintf(sufx, sizeof(sufx), fmt, ap);
131 snprintf(tag, sizeof(tag), "%s[%s]", logident_base, sufx);
134 openlog(tag, 0, LOG_MAIL);
138 errlog(int exitcode, const char *fmt, ...)
142 char outs[ERRMSG_SIZE];
147 vsnprintf(outs, sizeof(outs), fmt, ap);
153 syslog(LOG_ERR, "%s: %m", outs);
154 fprintf(stderr, "%s: %s: %s\n", getprogname(), outs, strerror(oerrno));
156 syslog(LOG_ERR, "%m");
157 fprintf(stderr, "%s: %s\n", getprogname(), strerror(oerrno));
164 errlogx(int exitcode, const char *fmt, ...)
167 char outs[ERRMSG_SIZE];
172 vsnprintf(outs, sizeof(outs), fmt, ap);
177 syslog(LOG_ERR, "%s", outs);
178 fprintf(stderr, "%s: %s\n", getprogname(), outs);
180 syslog(LOG_ERR, "Unknown error");
181 fprintf(stderr, "%s: Unknown error\n", getprogname());
188 check_username(const char *name, uid_t ckuid)
194 pwd = getpwnam(name);
195 if (pwd == NULL || pwd->pw_uid != ckuid)
197 snprintf(username, sizeof(username), "%s", name);
207 if (check_username(getlogin(), useruid))
209 if (check_username(getenv("LOGNAME"), useruid))
211 if (check_username(getenv("USER"), useruid))
213 pwd = getpwuid(useruid);
214 if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0] != '\0') {
215 if (check_username(pwd->pw_name, useruid))
218 snprintf(username, sizeof(username), "uid=%ld", (long)useruid);
226 SLIST_FOREACH(t, &tmpfs, next) {
231 static sigjmp_buf sigbuf;
232 static int sigbuf_valid;
235 sigalrm_handler(int signo)
237 (void)signo; /* so that gcc doesn't complain */
239 siglongjmp(sigbuf, 1);
243 do_timeout(int timeout, int dojmp)
245 struct sigaction act;
248 sigemptyset(&act.sa_mask);
252 act.sa_handler = sigalrm_handler;
253 if (sigaction(SIGALRM, &act, NULL) != 0)
254 syslog(LOG_WARNING, "can not set signal handler: %m");
256 ret = sigsetjmp(sigbuf, 1);
259 /* else just programmed */
268 act.sa_handler = SIG_IGN;
269 if (sigaction(SIGALRM, &act, NULL) != 0)
270 syslog(LOG_WARNING, "can not remove signal handler: %m");
278 open_locked(const char *fname, int flags, ...)
282 if (flags & O_CREAT) {
285 mode = va_arg(ap, int);
292 fd = open(fname, flags, mode);
295 if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
303 return(open(fname, flags|O_EXLOCK, mode));
315 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
318 strcpy(str, "(date fail)");
323 strprefixcmp(const char *str, const char *prefix)
325 return (strncasecmp(str, prefix, strlen(prefix)));
334 rf = open("/dev/urandom", O_RDONLY);
336 rf = open("/dev/random", O_RDONLY);
338 if (!(rf != -1 && read(rf, &seed, sizeof(seed)) == sizeof(seed)))
339 seed = (time(NULL) ^ getpid()) + (uintptr_t)&seed;