]>
git.ipfire.org Git - people/ms/dma.git/blob - util.c
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>
54 static char name
[HOST_NAME_MAX
+1];
55 static int initialized
= 0;
61 if (config
.mailname
== NULL
|| !*config
.mailname
)
64 if (config
.mailname
[0] == '/') {
66 * If the mailname looks like an absolute path,
71 fp
= fopen(config
.mailname
, "r");
75 s
= fgets(name
, sizeof(name
), fp
);
80 for (s
= name
; *s
!= 0 && (isalnum(*s
) || strchr("_.-", *s
)); ++s
)
90 snprintf(name
, sizeof(name
), "%s", config
.mailname
);
96 if (gethostname(name
, sizeof(name
)) != 0)
99 * gethostname() is allowed to truncate name without NUL-termination
100 * and at the same time not return an error.
102 name
[sizeof(name
) - 1] = 0;
104 for (s
= name
; *s
!= 0 && (isalnum(*s
) || strchr("_.-", *s
)); ++s
)
109 snprintf(name
, sizeof(name
), "unknown-hostname");
116 setlogident(const char *fmt
, ...)
120 snprintf(tag
, sizeof(tag
), "%s", logident_base
);
126 vsnprintf(sufx
, sizeof(sufx
), fmt
, ap
);
128 snprintf(tag
, sizeof(tag
), "%s[%s]", logident_base
, sufx
);
131 openlog(tag
, 0, LOG_MAIL
);
135 errlog(int exitcode
, const char *fmt
, ...)
139 char outs
[ERRMSG_SIZE
];
144 vsnprintf(outs
, sizeof(outs
), fmt
, ap
);
149 syslog(LOG_ERR
, "%s: %m", outs
);
150 fprintf(stderr
, "%s: %s: %s\n", getprogname(), outs
, strerror(oerrno
));
152 syslog(LOG_ERR
, "%m");
153 fprintf(stderr
, "%s: %s\n", getprogname(), strerror(oerrno
));
160 errlogx(int exitcode
, const char *fmt
, ...)
163 char outs
[ERRMSG_SIZE
];
168 vsnprintf(outs
, sizeof(outs
), fmt
, ap
);
173 syslog(LOG_ERR
, "%s", outs
);
174 fprintf(stderr
, "%s: %s\n", getprogname(), outs
);
176 syslog(LOG_ERR
, "Unknown error");
177 fprintf(stderr
, "%s: Unknown error\n", getprogname());
184 check_username(const char *name
, uid_t ckuid
)
190 pwd
= getpwnam(name
);
191 if (pwd
== NULL
|| pwd
->pw_uid
!= ckuid
)
193 snprintf(username
, sizeof(username
), "%s", name
);
203 if (check_username(getlogin(), useruid
))
205 if (check_username(getenv("LOGNAME"), useruid
))
207 if (check_username(getenv("USER"), useruid
))
209 pwd
= getpwuid(useruid
);
210 if (pwd
!= NULL
&& pwd
->pw_name
!= NULL
&& pwd
->pw_name
[0] != '\0') {
211 if (check_username(pwd
->pw_name
, useruid
))
214 snprintf(username
, sizeof(username
), "uid=%ld", (long)useruid
);
222 SLIST_FOREACH(t
, &tmpfs
, next
) {
227 static sigjmp_buf sigbuf
;
228 static int sigbuf_valid
;
231 sigalrm_handler(int signo
)
233 (void)signo
; /* so that gcc doesn't complain */
235 siglongjmp(sigbuf
, 1);
239 do_timeout(int timeout
, int dojmp
)
241 struct sigaction act
;
244 sigemptyset(&act
.sa_mask
);
248 act
.sa_handler
= sigalrm_handler
;
249 if (sigaction(SIGALRM
, &act
, NULL
) != 0)
250 syslog(LOG_WARNING
, "can not set signal handler: %m");
252 ret
= sigsetjmp(sigbuf
, 1);
255 /* else just programmed */
264 act
.sa_handler
= SIG_IGN
;
265 if (sigaction(SIGALRM
, &act
, NULL
) != 0)
266 syslog(LOG_WARNING
, "can not remove signal handler: %m");
274 open_locked(const char *fname
, int flags
, ...)
278 if (flags
& O_CREAT
) {
281 mode
= va_arg(ap
, int);
288 fd
= open(fname
, flags
, mode
);
291 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
299 return(open(fname
, flags
|O_EXLOCK
, mode
));
311 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
314 strcpy(str
, "(date fail)");
319 strprefixcmp(const char *str
, const char *prefix
)
321 return (strncasecmp(str
, prefix
, strlen(prefix
)));
330 rf
= open("/dev/urandom", O_RDONLY
);
332 rf
= open("/dev/random", O_RDONLY
);
334 if (!(rf
!= -1 && read(rf
, &seed
, sizeof(seed
)) == sizeof(seed
)))
335 seed
= (time(NULL
) ^ getpid()) + (uintptr_t)&seed
;