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