]> git.ipfire.org Git - people/ms/dma.git/blame - util.c
make ppa: force lower version number
[people/ms/dma.git] / util.c
CommitLineData
de13a881
SS
1/*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Simon 'corecode' Schubert <corecode@fs.ei.tum.de>.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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
32 * SUCH DAMAGE.
33 */
34
35#include <sys/param.h>
9b921550
SS
36#include <sys/file.h>
37
27905878 38#include <ctype.h>
de13a881
SS
39#include <errno.h>
40#include <fcntl.h>
41#include <netdb.h>
42#include <pwd.h>
a71122e7
SS
43#include <setjmp.h>
44#include <signal.h>
de13a881
SS
45#include <stdio.h>
46#include <syslog.h>
47#include <unistd.h>
48
49#include "dma.h"
50
51const char *
52hostname(void)
53{
d6240370 54 static char name[HOST_NAME_MAX+1];
3b322ad4 55 static int initialized = 0;
c3fb272b 56 char *s;
de13a881
SS
57
58 if (initialized)
59 return (name);
60
27905878
SS
61 if (config.mailname == NULL || !*config.mailname)
62 goto local;
63
64 if (config.mailname[0] == '/') {
65 /*
66 * If the mailname looks like an absolute path,
67 * treat it as a file.
68 */
69 FILE *fp;
27905878
SS
70
71 fp = fopen(config.mailname, "r");
72 if (fp == NULL)
73 goto local;
74
c3fb272b 75 s = fgets(name, sizeof(name), fp);
27905878 76 fclose(fp);
c3fb272b 77 if (s == NULL)
27905878
SS
78 goto local;
79
c3fb272b
SS
80 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
81 /* NOTHING */;
82 *s = 0;
27905878
SS
83
84 if (!*name)
85 goto local;
86
87 initialized = 1;
88 return (name);
89 } else {
a0c4afa6 90 snprintf(name, sizeof(name), "%s", config.mailname);
de13a881
SS
91 initialized = 1;
92 return (name);
93 }
27905878
SS
94
95local:
de13a881 96 if (gethostname(name, sizeof(name)) != 0)
c3fb272b 97 *name = 0;
d6240370
SS
98 /*
99 * gethostname() is allowed to truncate name without NUL-termination
100 * and at the same time not return an error.
101 */
102 name[sizeof(name) - 1] = 0;
c3fb272b
SS
103
104 for (s = name; *s != 0 && (isalnum(*s) || strchr("_.-", *s)); ++s)
105 /* NOTHING */;
106 *s = 0;
107
108 if (!*name)
109 snprintf(name, sizeof(name), "unknown-hostname");
110
de13a881 111 initialized = 1;
27905878 112 return (name);
de13a881
SS
113}
114
115void
116setlogident(const char *fmt, ...)
117{
42916656 118 static char tag[50];
de13a881 119
249ee966 120 snprintf(tag, sizeof(tag), "%s", logident_base);
de13a881
SS
121 if (fmt != NULL) {
122 va_list ap;
249ee966 123 char sufx[50];
de13a881
SS
124
125 va_start(ap, fmt);
249ee966 126 vsnprintf(sufx, sizeof(sufx), fmt, ap);
de13a881 127 va_end(ap);
249ee966 128 snprintf(tag, sizeof(tag), "%s[%s]", logident_base, sufx);
de13a881
SS
129 }
130 closelog();
249ee966 131 openlog(tag, 0, LOG_MAIL);
de13a881
SS
132}
133
134void
135errlog(int exitcode, const char *fmt, ...)
136{
137 int oerrno = errno;
138 va_list ap;
249ee966 139 char outs[ERRMSG_SIZE];
de13a881 140
249ee966 141 outs[0] = 0;
de13a881
SS
142 if (fmt != NULL) {
143 va_start(ap, fmt);
249ee966 144 vsnprintf(outs, sizeof(outs), fmt, ap);
de13a881
SS
145 va_end(ap);
146 }
147
249ee966 148 if (*outs != 0) {
de13a881
SS
149 syslog(LOG_ERR, "%s: %m", outs);
150 fprintf(stderr, "%s: %s: %s\n", getprogname(), outs, strerror(oerrno));
151 } else {
152 syslog(LOG_ERR, "%m");
153 fprintf(stderr, "%s: %s\n", getprogname(), strerror(oerrno));
154 }
155
156 exit(exitcode);
157}
158
159void
160errlogx(int exitcode, const char *fmt, ...)
161{
162 va_list ap;
249ee966 163 char outs[ERRMSG_SIZE];
de13a881 164
249ee966 165 outs[0] = 0;
de13a881
SS
166 if (fmt != NULL) {
167 va_start(ap, fmt);
249ee966 168 vsnprintf(outs, sizeof(outs), fmt, ap);
de13a881
SS
169 va_end(ap);
170 }
171
249ee966 172 if (*outs != 0) {
de13a881
SS
173 syslog(LOG_ERR, "%s", outs);
174 fprintf(stderr, "%s: %s\n", getprogname(), outs);
175 } else {
176 syslog(LOG_ERR, "Unknown error");
177 fprintf(stderr, "%s: Unknown error\n", getprogname());
178 }
179
180 exit(exitcode);
181}
182
249ee966 183static int
de13a881
SS
184check_username(const char *name, uid_t ckuid)
185{
186 struct passwd *pwd;
187
188 if (name == NULL)
249ee966 189 return (0);
de13a881
SS
190 pwd = getpwnam(name);
191 if (pwd == NULL || pwd->pw_uid != ckuid)
249ee966
SS
192 return (0);
193 snprintf(username, sizeof(username), "%s", name);
194 return (1);
de13a881
SS
195}
196
197void
198set_username(void)
199{
200 struct passwd *pwd;
de13a881 201
75fdf182
SS
202 useruid = getuid();
203 if (check_username(getlogin(), useruid))
de13a881 204 return;
75fdf182 205 if (check_username(getenv("LOGNAME"), useruid))
de13a881 206 return;
75fdf182 207 if (check_username(getenv("USER"), useruid))
de13a881 208 return;
75fdf182 209 pwd = getpwuid(useruid);
249ee966 210 if (pwd != NULL && pwd->pw_name != NULL && pwd->pw_name[0] != '\0') {
75fdf182 211 if (check_username(pwd->pw_name, useruid))
de13a881 212 return;
de13a881 213 }
75fdf182 214 snprintf(username, sizeof(username), "uid=%ld", (long)useruid);
de13a881
SS
215}
216
217void
218deltmp(void)
219{
220 struct stritem *t;
221
222 SLIST_FOREACH(t, &tmpfs, next) {
223 unlink(t->str);
224 }
225}
226
a71122e7
SS
227static sigjmp_buf sigbuf;
228static int sigbuf_valid;
229
230static void
231sigalrm_handler(int signo)
232{
233 (void)signo; /* so that gcc doesn't complain */
234 if (sigbuf_valid)
235 siglongjmp(sigbuf, 1);
236}
237
238int
239do_timeout(int timeout, int dojmp)
240{
241 struct sigaction act;
242 int ret = 0;
243
244 sigemptyset(&act.sa_mask);
245 act.sa_flags = 0;
246
247 if (timeout) {
248 act.sa_handler = sigalrm_handler;
249 if (sigaction(SIGALRM, &act, NULL) != 0)
250 syslog(LOG_WARNING, "can not set signal handler: %m");
251 if (dojmp) {
252 ret = sigsetjmp(sigbuf, 1);
253 if (ret)
254 goto disable;
255 /* else just programmed */
256 sigbuf_valid = 1;
257 }
258
259 alarm(timeout);
260 } else {
261disable:
262 alarm(0);
263
264 act.sa_handler = SIG_IGN;
265 if (sigaction(SIGALRM, &act, NULL) != 0)
266 syslog(LOG_WARNING, "can not remove signal handler: %m");
267 sigbuf_valid = 0;
268 }
269
270 return (ret);
271}
272
de13a881
SS
273int
274open_locked(const char *fname, int flags, ...)
275{
276 int mode = 0;
277
278 if (flags & O_CREAT) {
279 va_list ap;
280 va_start(ap, flags);
281 mode = va_arg(ap, int);
282 va_end(ap);
283 }
284
285#ifndef O_EXLOCK
286 int fd, save_errno;
287
288 fd = open(fname, flags, mode);
289 if (fd < 0)
290 return(fd);
291 if (flock(fd, LOCK_EX|((flags & O_NONBLOCK)? LOCK_NB: 0)) < 0) {
292 save_errno = errno;
293 close(fd);
294 errno = save_errno;
295 return(-1);
296 }
297 return(fd);
298#else
299 return(open(fname, flags|O_EXLOCK, mode));
300#endif
301}
302
303char *
304rfc822date(void)
305{
306 static char str[50];
307 size_t error;
308 time_t now;
309
310 now = time(NULL);
311 error = strftime(str, sizeof(str), "%a, %d %b %Y %T %z",
312 localtime(&now));
313 if (error == 0)
314 strcpy(str, "(date fail)");
315 return (str);
316}
317
318int
319strprefixcmp(const char *str, const char *prefix)
320{
321 return (strncasecmp(str, prefix, strlen(prefix)));
322}
323
ee613428
SS
324void
325init_random(void)
326{
327 unsigned int seed;
328 int rf;
329
330 rf = open("/dev/urandom", O_RDONLY);
331 if (rf == -1)
332 rf = open("/dev/random", O_RDONLY);
333
334 if (!(rf != -1 && read(rf, &seed, sizeof(seed)) == sizeof(seed)))
335 seed = (time(NULL) ^ getpid()) + (uintptr_t)&seed;
336
337 srandom(seed);
338
339 if (rf != -1)
340 close(rf);
341}