]>
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>
51 static char name
[MAXHOSTNAMELEN
+1];
52 static int initialized
= 0;
60 if (config
.mailname
!= NULL
&& config
.mailname
[0] != '\0') {
61 snprintf(name
, sizeof(name
), "%s", config
.mailname
);
65 if (config
.mailnamefile
!= NULL
&& config
.mailnamefile
[0] != '\0') {
66 fp
= fopen(config
.mailnamefile
, "r");
68 res
= fgets(name
, sizeof(name
), fp
);
73 (name
[len
- 1] == '\r' ||
74 name
[len
- 1] == '\n'))
76 if (name
[0] != '\0') {
83 if (gethostname(name
, sizeof(name
)) != 0)
84 strcpy(name
, "(unknown hostname)");
90 setlogident(const char *fmt
, ...)
99 if (vasprintf(&sufx
, fmt
, ap
) != -1 && sufx
!= NULL
) {
100 if (asprintf(&tag
, "%s[%s]", logident_base
, sufx
) == -1)
107 openlog(tag
!= NULL
? tag
: logident_base
, 0, LOG_MAIL
);
111 errlog(int exitcode
, const char *fmt
, ...)
119 if (vasprintf(&outs
, fmt
, ap
) == -1)
125 syslog(LOG_ERR
, "%s: %m", outs
);
126 fprintf(stderr
, "%s: %s: %s\n", getprogname(), outs
, strerror(oerrno
));
128 syslog(LOG_ERR
, "%m");
129 fprintf(stderr
, "%s: %s\n", getprogname(), strerror(oerrno
));
136 errlogx(int exitcode
, const char *fmt
, ...)
143 if (vasprintf(&outs
, fmt
, ap
) == -1)
149 syslog(LOG_ERR
, "%s", outs
);
150 fprintf(stderr
, "%s: %s\n", getprogname(), outs
);
152 syslog(LOG_ERR
, "Unknown error");
153 fprintf(stderr
, "%s: Unknown error\n", getprogname());
160 check_username(const char *name
, uid_t ckuid
)
166 pwd
= getpwnam(name
);
167 if (pwd
== NULL
|| pwd
->pw_uid
!= ckuid
)
180 username
= check_username(getlogin(), uid
);
181 if (username
!= NULL
)
183 username
= check_username(getenv("LOGNAME"), uid
);
184 if (username
!= NULL
)
186 username
= check_username(getenv("USER"), uid
);
187 if (username
!= NULL
)
190 if (pwd
!= NULL
&& pwd
->pw_name
!= NULL
&& pwd
->pw_name
[0] != '\0' &&
191 (u
= strdup(pwd
->pw_name
)) != NULL
) {
192 username
= check_username(u
, uid
);
193 if (username
!= NULL
)
198 if (asprintf(__DECONST(void *, &username
), "%ld", (long)uid
) != -1 &&
201 username
= "unknown-or-invalid-username";
209 SLIST_FOREACH(t
, &tmpfs
, next
) {
215 open_locked(const char *fname
, int flags
, ...)
219 if (flags
& O_CREAT
) {
222 mode
= va_arg(ap
, int);
229 fd
= open(fname
, flags
, mode
);
232 if (flock(fd
, LOCK_EX
|((flags
& O_NONBLOCK
)? LOCK_NB
: 0)) < 0) {
240 return(open(fname
, flags
|O_EXLOCK
, mode
));
252 error
= strftime(str
, sizeof(str
), "%a, %d %b %Y %T %z",
255 strcpy(str
, "(date fail)");
260 strprefixcmp(const char *str
, const char *prefix
)
262 return (strncasecmp(str
, prefix
, strlen(prefix
)));
271 rf
= open("/dev/urandom", O_RDONLY
);
273 rf
= open("/dev/random", O_RDONLY
);
275 if (!(rf
!= -1 && read(rf
, &seed
, sizeof(seed
)) == sizeof(seed
)))
276 seed
= (time(NULL
) ^ getpid()) + (uintptr_t)&seed
;