xref: /netbsd-src/external/bsd/cron/dist/misc.c (revision 28e539bcbb08d579b5eecd47ecc37bea41fd4962)
1*28e539bcSchristos /*	$NetBSD: misc.c,v 1.5 2017/08/17 08:53:00 christos Exp $	*/
2032a4398Schristos 
30061c6a5Schristos /* Copyright 1988,1990,1993,1994 by Paul Vixie
40061c6a5Schristos  * All rights reserved
50061c6a5Schristos  */
60061c6a5Schristos 
70061c6a5Schristos /*
80061c6a5Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
90061c6a5Schristos  * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
100061c6a5Schristos  *
110061c6a5Schristos  * Permission to use, copy, modify, and distribute this software for any
120061c6a5Schristos  * purpose with or without fee is hereby granted, provided that the above
130061c6a5Schristos  * copyright notice and this permission notice appear in all copies.
140061c6a5Schristos  *
150061c6a5Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
160061c6a5Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
170061c6a5Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
180061c6a5Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
190061c6a5Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
200061c6a5Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
210061c6a5Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
220061c6a5Schristos  */
23032a4398Schristos #include <sys/cdefs.h>
240061c6a5Schristos #if !defined(lint) && !defined(LINT)
25032a4398Schristos #if 0
260061c6a5Schristos static char rcsid[] = "Id: misc.c,v 1.16 2004/01/23 18:56:43 vixie Exp";
27032a4398Schristos #else
28*28e539bcSchristos __RCSID("$NetBSD: misc.c,v 1.5 2017/08/17 08:53:00 christos Exp $");
29032a4398Schristos #endif
300061c6a5Schristos #endif
310061c6a5Schristos 
320061c6a5Schristos /* vix 26jan87 [RCS has the rest of the log]
330061c6a5Schristos  * vix 30dec86 [written]
340061c6a5Schristos  */
350061c6a5Schristos 
360061c6a5Schristos #include "cron.h"
370061c6a5Schristos #include <limits.h>
38032a4398Schristos #include <vis.h>
390061c6a5Schristos 
400061c6a5Schristos #if defined(SYSLOG) && defined(LOG_FILE)
410061c6a5Schristos # undef LOG_FILE
420061c6a5Schristos #endif
430061c6a5Schristos 
440061c6a5Schristos #if defined(LOG_DAEMON) && !defined(LOG_CRON)
450061c6a5Schristos # define LOG_CRON LOG_DAEMON
460061c6a5Schristos #endif
470061c6a5Schristos 
480061c6a5Schristos #ifndef FACILITY
490061c6a5Schristos #define FACILITY LOG_CRON
500061c6a5Schristos #endif
510061c6a5Schristos 
520061c6a5Schristos static int LogFD = ERR;
530061c6a5Schristos 
540061c6a5Schristos #if defined(SYSLOG)
550061c6a5Schristos static int syslog_open = FALSE;
560061c6a5Schristos #endif
570061c6a5Schristos 
58032a4398Schristos static void mkprint(char *, char *, size_t);
59032a4398Schristos 
600061c6a5Schristos /*
610061c6a5Schristos  * glue_strings is the overflow-safe equivalent of
620061c6a5Schristos  *		sprintf(buffer, "%s%c%s", a, separator, b);
630061c6a5Schristos  *
640061c6a5Schristos  * returns 1 on success, 0 on failure.  'buffer' MUST NOT be used if
650061c6a5Schristos  * glue_strings fails.
660061c6a5Schristos  */
670061c6a5Schristos int
glue_strings(char * buffer,size_t buffer_size,const char * a,const char * b,char separator)680061c6a5Schristos glue_strings(char *buffer, size_t buffer_size, const char *a, const char *b,
690061c6a5Schristos 	     char separator)
700061c6a5Schristos {
710061c6a5Schristos 	char *buf;
720061c6a5Schristos 	char *buf_end;
730061c6a5Schristos 
74032a4398Schristos 	if (buffer_size == 0)
750061c6a5Schristos 		return (0);
760061c6a5Schristos 	buf_end = buffer + buffer_size;
770061c6a5Schristos 	buf = buffer;
780061c6a5Schristos 
790061c6a5Schristos 	for ( /* nothing */; buf < buf_end && *a != '\0'; buf++, a++ )
800061c6a5Schristos 		*buf = *a;
810061c6a5Schristos 	if (buf == buf_end)
820061c6a5Schristos 		return (0);
830061c6a5Schristos 	if (separator != '/' || buf == buffer || buf[-1] != '/')
840061c6a5Schristos 		*buf++ = separator;
850061c6a5Schristos 	if (buf == buf_end)
860061c6a5Schristos 		return (0);
870061c6a5Schristos 	for ( /* nothing */; buf < buf_end && *b != '\0'; buf++, b++ )
880061c6a5Schristos 		*buf = *b;
890061c6a5Schristos 	if (buf == buf_end)
900061c6a5Schristos 		return (0);
910061c6a5Schristos 	*buf = '\0';
920061c6a5Schristos 	return (1);
930061c6a5Schristos }
940061c6a5Schristos 
950061c6a5Schristos int
strcmp_until(const char * left,const char * right,char until)960061c6a5Schristos strcmp_until(const char *left, const char *right, char until) {
970061c6a5Schristos 	while (*left && *left != until && *left == *right) {
980061c6a5Schristos 		left++;
990061c6a5Schristos 		right++;
1000061c6a5Schristos 	}
1010061c6a5Schristos 
1020061c6a5Schristos 	if ((*left=='\0' || *left == until) &&
1030061c6a5Schristos 	    (*right=='\0' || *right == until)) {
1040061c6a5Schristos 		return (0);
1050061c6a5Schristos 	}
1060061c6a5Schristos 	return (*left - *right);
1070061c6a5Schristos }
1080061c6a5Schristos 
109032a4398Schristos #ifdef notdef
1100061c6a5Schristos /* strdtb(s) - delete trailing blanks in string 's' and return new length
1110061c6a5Schristos  */
1120061c6a5Schristos int
strdtb(char * s)1130061c6a5Schristos strdtb(char *s) {
1140061c6a5Schristos 	char	*x = s;
1150061c6a5Schristos 
1160061c6a5Schristos 	/* scan forward to the null
1170061c6a5Schristos 	 */
1180061c6a5Schristos 	while (*x)
1190061c6a5Schristos 		x++;
1200061c6a5Schristos 
1210061c6a5Schristos 	/* scan backward to either the first character before the string,
1220061c6a5Schristos 	 * or the last non-blank in the string, whichever comes first.
1230061c6a5Schristos 	 */
1240061c6a5Schristos 	do	{x--;}
1250061c6a5Schristos 	while (x >= s && isspace((unsigned char)*x));
1260061c6a5Schristos 
1270061c6a5Schristos 	/* one character beyond where we stopped above is where the null
1280061c6a5Schristos 	 * goes.
1290061c6a5Schristos 	 */
1300061c6a5Schristos 	*++x = '\0';
1310061c6a5Schristos 
1320061c6a5Schristos 	/* the difference between the position of the null character and
1330061c6a5Schristos 	 * the position of the first character of the string is the length.
1340061c6a5Schristos 	 */
135032a4398Schristos 	return (int)(x - s);
1360061c6a5Schristos }
137032a4398Schristos #endif
1380061c6a5Schristos 
1390061c6a5Schristos int
set_debug_flags(const char * flags)1400061c6a5Schristos set_debug_flags(const char *flags) {
1410061c6a5Schristos 	/* debug flags are of the form    flag[,flag ...]
1420061c6a5Schristos 	 *
1430061c6a5Schristos 	 * if an error occurs, print a message to stdout and return FALSE.
1440061c6a5Schristos 	 * otherwise return TRUE after setting ERROR_FLAGS.
1450061c6a5Schristos 	 */
1460061c6a5Schristos 
1470061c6a5Schristos #if !DEBUGGING
1480061c6a5Schristos 
1490061c6a5Schristos 	printf("this program was compiled without debugging enabled\n");
1500061c6a5Schristos 	return (FALSE);
1510061c6a5Schristos 
1520061c6a5Schristos #else /* DEBUGGING */
1530061c6a5Schristos 
1540061c6a5Schristos 	const char *pc = flags;
1550061c6a5Schristos 
1560061c6a5Schristos 	DebugFlags = 0;
1570061c6a5Schristos 
1580061c6a5Schristos 	while (*pc) {
159032a4398Schristos 		const char	* const *test;
1600061c6a5Schristos 		int		mask;
1610061c6a5Schristos 
1620061c6a5Schristos 		/* try to find debug flag name in our list.
1630061c6a5Schristos 		 */
1640061c6a5Schristos 		for (test = DebugFlagNames, mask = 1;
1650061c6a5Schristos 		     *test != NULL && strcmp_until(*test, pc, ',');
1660061c6a5Schristos 		     test++, mask <<= 1)
167032a4398Schristos 			continue;
1680061c6a5Schristos 
1690061c6a5Schristos 		if (!*test) {
170032a4398Schristos 			warnx("unrecognized debug flag <%s> <%s>\n", flags, pc);
1710061c6a5Schristos 			return (FALSE);
1720061c6a5Schristos 		}
1730061c6a5Schristos 
1740061c6a5Schristos 		DebugFlags |= mask;
1750061c6a5Schristos 
1760061c6a5Schristos 		/* skip to the next flag
1770061c6a5Schristos 		 */
1780061c6a5Schristos 		while (*pc && *pc != ',')
1790061c6a5Schristos 			pc++;
1800061c6a5Schristos 		if (*pc == ',')
1810061c6a5Schristos 			pc++;
1820061c6a5Schristos 	}
1830061c6a5Schristos 
1840061c6a5Schristos 	if (DebugFlags) {
1850061c6a5Schristos 		int flag;
1860061c6a5Schristos 
187032a4398Schristos 		(void)fprintf(stderr, "debug flags enabled:");
1880061c6a5Schristos 
1890061c6a5Schristos 		for (flag = 0;  DebugFlagNames[flag];  flag++)
1900061c6a5Schristos 			if (DebugFlags & (1 << flag))
191032a4398Schristos 				(void)fprintf(stderr, " %s", DebugFlagNames[flag]);
192032a4398Schristos 		(void)fprintf(stderr, "\n");
1930061c6a5Schristos 	}
1940061c6a5Schristos 
1950061c6a5Schristos 	return (TRUE);
1960061c6a5Schristos 
1970061c6a5Schristos #endif /* DEBUGGING */
1980061c6a5Schristos }
1990061c6a5Schristos 
2000061c6a5Schristos void
set_cron_uid(void)2010061c6a5Schristos set_cron_uid(void) {
2020061c6a5Schristos #if defined(BSD) || defined(POSIX)
2030061c6a5Schristos 	if (seteuid(ROOT_UID) < OK) {
204032a4398Schristos 		err(ERROR_EXIT, "cannot seteuid");
2050061c6a5Schristos 	}
2060061c6a5Schristos #else
2070061c6a5Schristos 	if (setuid(ROOT_UID) < OK) {
208032a4398Schristos 		err(ERROR_EXIT, "cannot setuid");
2090061c6a5Schristos 	}
2100061c6a5Schristos #endif
2110061c6a5Schristos }
2120061c6a5Schristos 
2130061c6a5Schristos void
set_cron_cwd(void)2140061c6a5Schristos set_cron_cwd(void) {
2150061c6a5Schristos 	struct stat sb;
2160061c6a5Schristos 	struct group *grp = NULL;
2170061c6a5Schristos 
2180061c6a5Schristos #ifdef CRON_GROUP
2190061c6a5Schristos 	grp = getgrnam(CRON_GROUP);
2200061c6a5Schristos #endif
2210061c6a5Schristos 	/* first check for CRONDIR ("/var/cron" or some such)
2220061c6a5Schristos 	 */
223065057e6Schristos #ifdef ENABLE_FIX_DIRECTORIES
2240061c6a5Schristos 	if (stat(CRONDIR, &sb) < OK && errno == ENOENT) {
225032a4398Schristos 		warn("Cannot stat `%s'", CRONDIR);
2260061c6a5Schristos 		if (OK == mkdir(CRONDIR, 0710)) {
227032a4398Schristos 			(void)fprintf(stderr, "%s: created\n", CRONDIR);
228032a4398Schristos 			if (stat(CRONDIR, &sb) == -1)
229032a4398Schristos 				err(ERROR_EXIT, "cannot stat `%s'", CRONDIR);
2300061c6a5Schristos 		} else {
231032a4398Schristos 			err(ERROR_EXIT, "cannot create `%s'", CRONDIR);
2320061c6a5Schristos 		}
2330061c6a5Schristos 	}
2340061c6a5Schristos 	if (!S_ISDIR(sb.st_mode)) {
235032a4398Schristos 		errx(ERROR_EXIT, "`%s' is not a directory, bailing out.",
2360061c6a5Schristos 			CRONDIR);
2370061c6a5Schristos 	}
238065057e6Schristos #endif /* ENABLE_FIX_DIRECTORIES */
2390061c6a5Schristos 	if (chdir(CRONDIR) < OK) {
240032a4398Schristos 		err(ERROR_EXIT, "cannot chdir `%s', bailing out.\n", CRONDIR);
2410061c6a5Schristos 	}
2420061c6a5Schristos 
2430061c6a5Schristos 	/* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
2440061c6a5Schristos 	 */
245065057e6Schristos #ifdef ENABLE_FIX_DIRECTORIES
2460061c6a5Schristos 	if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) {
247032a4398Schristos 		warn("cannot stat `%s'", SPOOL_DIR);
2480061c6a5Schristos 		if (OK == mkdir(SPOOL_DIR, 0700)) {
249032a4398Schristos 			(void)fprintf(stderr, "%s: created\n", SPOOL_DIR);
250032a4398Schristos 			if (stat(SPOOL_DIR, &sb) == -1)
251032a4398Schristos 				err(ERROR_EXIT, "cannot stat `%s'", CRONDIR);
2520061c6a5Schristos 		} else {
253032a4398Schristos 			err(ERROR_EXIT, "cannot create `%s'", SPOOL_DIR);
2540061c6a5Schristos 		}
2550061c6a5Schristos 	}
256065057e6Schristos #else
257065057e6Schristos 	if (stat(SPOOL_DIR, &sb)) {
258065057e6Schristos 		err(ERROR_EXIT, "cannot stat `%s'", SPOOL_DIR);
259065057e6Schristos 	}
260065057e6Schristos #endif /* ENABLE_FIX_DIRECTORIES */
2610061c6a5Schristos 	if (!S_ISDIR(sb.st_mode)) {
262032a4398Schristos 		errx(ERROR_EXIT, "`%s' is not a directory, bailing out.",
2630061c6a5Schristos 			SPOOL_DIR);
2640061c6a5Schristos 	}
2650061c6a5Schristos 	if (grp != NULL) {
266065057e6Schristos 		if (sb.st_gid != grp->gr_gid) {
267065057e6Schristos #ifdef ENABLE_FIX_DIRECTORIES
268065057e6Schristos 			errx(ERROR_EXIT, "Bad group %d != %d for `%s'",
269065057e6Schristos 			    (int)sb.st_gid, (int)grp->gr_gid, SPOOL_DIR);
270065057e6Schristos #else
271032a4398Schristos 			if (chown(SPOOL_DIR, (uid_t)-1, grp->gr_gid) == -1)
272032a4398Schristos 			    err(ERROR_EXIT, "cannot chown `%s'", SPOOL_DIR);
273065057e6Schristos #endif
274065057e6Schristos 		}
2750061c6a5Schristos 		if (sb.st_mode != 01730)
276065057e6Schristos #ifdef ENABLE_FIX_DIRECTORIES
277065057e6Schristos 			errx(ERROR_EXIT, "Bad mode %#o != %#o for `%s'",
278065057e6Schristos 			    (int)sb.st_mode, 01730, SPOOL_DIR);
279065057e6Schristos #else
280032a4398Schristos 			if (chmod(SPOOL_DIR, 01730) == -1)
281032a4398Schristos 			    err(ERROR_EXIT, "cannot chmod `%s'", SPOOL_DIR);
282065057e6Schristos #endif
2830061c6a5Schristos 	}
2840061c6a5Schristos }
2850061c6a5Schristos 
2860061c6a5Schristos /* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
2870061c6a5Schristos  *	another daemon is already running, which we detect here.
2880061c6a5Schristos  *
2890061c6a5Schristos  * note: main() calls us twice; once before forking, once after.
2900061c6a5Schristos  *	we maintain static storage of the file pointer so that we
2910061c6a5Schristos  *	can rewrite our PID into _PATH_CRON_PID after the fork.
2920061c6a5Schristos  */
2930061c6a5Schristos void
acquire_daemonlock(int closeflag)2940061c6a5Schristos acquire_daemonlock(int closeflag) {
2950061c6a5Schristos 	static int fd = -1;
2960061c6a5Schristos 	char buf[3*MAX_FNAME];
2970061c6a5Schristos 	const char *pidfile;
2980061c6a5Schristos 	char *ep;
2990061c6a5Schristos 	long otherpid;
3000061c6a5Schristos 	ssize_t num;
3010061c6a5Schristos 
3020061c6a5Schristos 	if (closeflag) {
3030061c6a5Schristos 		/* close stashed fd for child so we don't leak it. */
3040061c6a5Schristos 		if (fd != -1) {
305032a4398Schristos 			(void)close(fd);
3060061c6a5Schristos 			fd = -1;
3070061c6a5Schristos 		}
3080061c6a5Schristos 		return;
3090061c6a5Schristos 	}
3100061c6a5Schristos 
3110061c6a5Schristos 	if (fd == -1) {
3120061c6a5Schristos 		pidfile = _PATH_CRON_PID;
3130061c6a5Schristos 		/* Initial mode is 0600 to prevent flock() race/DoS. */
3140061c6a5Schristos 		if ((fd = open(pidfile, O_RDWR|O_CREAT, 0600)) == -1) {
315*28e539bcSchristos 			log_itx("CRON", getpid(), "DEATH",
316032a4398Schristos 			    "can't open or create %s: %s",
3170061c6a5Schristos 			    pidfile, strerror(errno));
318*28e539bcSchristos 			exit(ERROR_EXIT);
3190061c6a5Schristos 		}
320065057e6Schristos 		/* fd must be > STDERR since we dup fd 0-2 to /dev/null */
321065057e6Schristos 		if (fd <= STDERR) {
322065057e6Schristos 			if (dup2(fd, STDERR + 1) < 0) {
323*28e539bcSchristos 				log_itx("CRON", getpid(), "DEATH",
324065057e6Schristos 				    "can't dup pid fd: %s", strerror(errno));
325065057e6Schristos  				exit(ERROR_EXIT);
326065057e6Schristos  			}
327065057e6Schristos 			close(fd);
328065057e6Schristos 			fd = STDERR + 1;
329065057e6Schristos 		}
3300061c6a5Schristos 
3310061c6a5Schristos 		if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
3320061c6a5Schristos 			int save_errno = errno;
3330061c6a5Schristos 
334032a4398Schristos 			memset(buf, 0, sizeof(buf));
3350061c6a5Schristos 			if ((num = read(fd, buf, sizeof(buf) - 1)) > 0 &&
3360061c6a5Schristos 			    (otherpid = strtol(buf, &ep, 10)) > 0 &&
3370061c6a5Schristos 			    ep != buf && *ep == '\n' && otherpid != LONG_MAX) {
338*28e539bcSchristos 				log_itx("CRON", getpid(), "DEATH",
3390061c6a5Schristos 				    "can't lock %s, otherpid may be %ld: %s",
3400061c6a5Schristos 				    pidfile, otherpid, strerror(save_errno));
3410061c6a5Schristos 			} else {
342*28e539bcSchristos 				log_itx("CRON", getpid(), "DEATH",
3430061c6a5Schristos 				    "can't lock %s, otherpid unknown: %s",
3440061c6a5Schristos 				    pidfile, strerror(save_errno));
3450061c6a5Schristos 			}
346*28e539bcSchristos 			exit(ERROR_EXIT);
3470061c6a5Schristos 		}
3480061c6a5Schristos 		(void) fchmod(fd, 0644);
3490061c6a5Schristos 		(void) fcntl(fd, F_SETFD, 1);
3500061c6a5Schristos 	}
3510061c6a5Schristos 
352032a4398Schristos 	(void)snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
3530061c6a5Schristos 	(void) lseek(fd, (off_t)0, SEEK_SET);
3540061c6a5Schristos 	num = write(fd, buf, strlen(buf));
3550061c6a5Schristos 	(void) ftruncate(fd, num);
3560061c6a5Schristos 
3570061c6a5Schristos 	/* abandon fd even though the file is open. we need to keep
3580061c6a5Schristos 	 * it open and locked, but we don't need the handles elsewhere.
3590061c6a5Schristos 	 */
3600061c6a5Schristos }
3610061c6a5Schristos 
3620061c6a5Schristos /* get_char(file) : like getc() but increment LineNumber on newlines
3630061c6a5Schristos  */
3640061c6a5Schristos int
get_char(FILE * file)3650061c6a5Schristos get_char(FILE *file) {
3660061c6a5Schristos 	int ch;
3670061c6a5Schristos 
3680061c6a5Schristos 	ch = getc(file);
3690061c6a5Schristos 	if (ch == '\n')
370032a4398Schristos 		Set_LineNum(LineNumber + 1);
3710061c6a5Schristos 	return (ch);
3720061c6a5Schristos }
3730061c6a5Schristos 
3740061c6a5Schristos /* unget_char(ch, file) : like ungetc but do LineNumber processing
3750061c6a5Schristos  */
3760061c6a5Schristos void
unget_char(int ch,FILE * file)3770061c6a5Schristos unget_char(int ch, FILE *file) {
378032a4398Schristos 	(void)ungetc(ch, file);
3790061c6a5Schristos 	if (ch == '\n')
380032a4398Schristos 		Set_LineNum(LineNumber - 1);
3810061c6a5Schristos }
3820061c6a5Schristos 
3830061c6a5Schristos /* get_string(str, max, file, termstr) : like fgets() but
3840061c6a5Schristos  *		(1) has terminator string which should include \n
3850061c6a5Schristos  *		(2) will always leave room for the null
3860061c6a5Schristos  *		(3) uses get_char() so LineNumber will be accurate
3870061c6a5Schristos  *		(4) returns EOF or terminating character, whichever
3880061c6a5Schristos  */
3890061c6a5Schristos int
get_string(char * string,int size,FILE * file,const char * terms)390032a4398Schristos get_string(char *string, int size, FILE *file, const char *terms) {
3910061c6a5Schristos 	int ch;
3920061c6a5Schristos 
3930061c6a5Schristos 	while (EOF != (ch = get_char(file)) && !strchr(terms, ch)) {
3940061c6a5Schristos 		if (size > 1) {
3950061c6a5Schristos 			*string++ = (char) ch;
3960061c6a5Schristos 			size--;
3970061c6a5Schristos 		}
3980061c6a5Schristos 	}
3990061c6a5Schristos 
4000061c6a5Schristos 	if (size > 0)
4010061c6a5Schristos 		*string = '\0';
4020061c6a5Schristos 
4030061c6a5Schristos 	return (ch);
4040061c6a5Schristos }
4050061c6a5Schristos 
4060061c6a5Schristos /* skip_comments(file) : read past comment (if any)
4070061c6a5Schristos  */
4080061c6a5Schristos void
skip_comments(FILE * file)4090061c6a5Schristos skip_comments(FILE *file) {
4100061c6a5Schristos 	int ch;
4110061c6a5Schristos 
4120061c6a5Schristos 	while (EOF != (ch = get_char(file))) {
4130061c6a5Schristos 		/* ch is now the first character of a line.
4140061c6a5Schristos 		 */
4150061c6a5Schristos 
4160061c6a5Schristos 		while (ch == ' ' || ch == '\t')
4170061c6a5Schristos 			ch = get_char(file);
4180061c6a5Schristos 
4190061c6a5Schristos 		if (ch == EOF)
4200061c6a5Schristos 			break;
4210061c6a5Schristos 
4220061c6a5Schristos 		/* ch is now the first non-blank character of a line.
4230061c6a5Schristos 		 */
4240061c6a5Schristos 
4250061c6a5Schristos 		if (ch != '\n' && ch != '#')
4260061c6a5Schristos 			break;
4270061c6a5Schristos 
4280061c6a5Schristos 		/* ch must be a newline or comment as first non-blank
4290061c6a5Schristos 		 * character on a line.
4300061c6a5Schristos 		 */
4310061c6a5Schristos 
4320061c6a5Schristos 		while (ch != '\n' && ch != EOF)
4330061c6a5Schristos 			ch = get_char(file);
4340061c6a5Schristos 
4350061c6a5Schristos 		/* ch is now the newline of a line which we're going to
4360061c6a5Schristos 		 * ignore.
4370061c6a5Schristos 		 */
4380061c6a5Schristos 	}
4390061c6a5Schristos 	if (ch != EOF)
4400061c6a5Schristos 		unget_char(ch, file);
4410061c6a5Schristos }
4420061c6a5Schristos 
4430061c6a5Schristos void
log_itx(const char * username,PID_T xpid,const char * event,const char * fmt,...)444*28e539bcSchristos log_itx(const char *username, PID_T xpid, const char *event, const char *fmt,
445*28e539bcSchristos     ...)
446*28e539bcSchristos {
447*28e539bcSchristos 	char *detail;
448*28e539bcSchristos 	va_list ap;
449*28e539bcSchristos 	va_start(ap, fmt);
450*28e539bcSchristos 	if (vasprintf(&detail, fmt, ap) == -1) {
451*28e539bcSchristos 		va_end(ap);
452*28e539bcSchristos 		return;
453*28e539bcSchristos 	}
454*28e539bcSchristos 	log_it(username, xpid, event, detail);
455*28e539bcSchristos 	free(detail);
456*28e539bcSchristos }
457*28e539bcSchristos 
458*28e539bcSchristos void
log_it(const char * username,PID_T xpid,const char * event,const char * detail)4590061c6a5Schristos log_it(const char *username, PID_T xpid, const char *event, const char *detail) {
4600061c6a5Schristos #if defined(LOG_FILE) || DEBUGGING
4610061c6a5Schristos 	PID_T pid = xpid;
4620061c6a5Schristos #endif
4630061c6a5Schristos #if defined(LOG_FILE)
4640061c6a5Schristos 	char *msg;
465*28e539bcSchristos 	int msglen;
4660061c6a5Schristos 	TIME_T now = time((TIME_T) 0);
4670061c6a5Schristos 	struct tm *t = localtime(&now);
4680061c6a5Schristos 
4690061c6a5Schristos 	if (LogFD < OK) {
4700061c6a5Schristos 		LogFD = open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0600);
4710061c6a5Schristos 		if (LogFD < OK) {
472032a4398Schristos 			warn("can't open log file `%s'", LOG_FILE);
4730061c6a5Schristos 		} else {
474032a4398Schristos 			(void) fcntl(LogFD, F_SETFD, FD_CLOEXEC);
4750061c6a5Schristos 		}
4760061c6a5Schristos 	}
4770061c6a5Schristos 
4780061c6a5Schristos 	/* we have to sprintf() it because fprintf() doesn't always write
4790061c6a5Schristos 	 * everything out in one chunk and this has to be atomically appended
4800061c6a5Schristos 	 * to the log file.
4810061c6a5Schristos 	 */
482*28e539bcSchristos 	msglen = asprintf(&msg,
483*28e539bcSchristos 	    "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n", username,
4840061c6a5Schristos 	    t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid,
4850061c6a5Schristos 	    event, detail);
486*28e539bcSchristos 	if (msglen == -1)
487*28e539bcSchristos 		return;
4880061c6a5Schristos 
489*28e539bcSchristos 	if (LogFD < OK || write(LogFD, msg, (size_t)msglen) < OK) {
490032a4398Schristos 		warn("can't write to log file");
491*28e539bcSchristos 		write(STDERR, msg, (size_t)msglen);
4920061c6a5Schristos 	}
4930061c6a5Schristos 
4940061c6a5Schristos 	free(msg);
4950061c6a5Schristos #endif /*LOG_FILE*/
4960061c6a5Schristos 
4970061c6a5Schristos #if defined(SYSLOG)
4980061c6a5Schristos 	if (!syslog_open) {
4990061c6a5Schristos # ifdef LOG_DAEMON
500032a4398Schristos 		openlog(getprogname(), LOG_PID, FACILITY);
5010061c6a5Schristos # else
502032a4398Schristos 		openlog(getprogname(), LOG_PID);
5030061c6a5Schristos # endif
5040061c6a5Schristos 		syslog_open = TRUE;		/* assume openlog success */
5050061c6a5Schristos 	}
5060061c6a5Schristos 
5070061c6a5Schristos 	syslog(LOG_INFO, "(%s) %s (%s)", username, event, detail);
5080061c6a5Schristos 
5090061c6a5Schristos #endif /*SYSLOG*/
5100061c6a5Schristos 
5110061c6a5Schristos #if DEBUGGING
5120061c6a5Schristos 	if (DebugFlags) {
513032a4398Schristos 		(void)fprintf(stderr, "log_it: (%s %ld) %s (%s)\n",
5140061c6a5Schristos 			username, (long)pid, event, detail);
5150061c6a5Schristos 	}
5160061c6a5Schristos #endif
5170061c6a5Schristos }
5180061c6a5Schristos 
5190061c6a5Schristos void
log_close(void)5200061c6a5Schristos log_close(void) {
5210061c6a5Schristos 	if (LogFD != ERR) {
522032a4398Schristos 		(void)close(LogFD);
5230061c6a5Schristos 		LogFD = ERR;
5240061c6a5Schristos 	}
5250061c6a5Schristos #if defined(SYSLOG)
5260061c6a5Schristos 	closelog();
5270061c6a5Schristos 	syslog_open = FALSE;
5280061c6a5Schristos #endif /*SYSLOG*/
5290061c6a5Schristos }
5300061c6a5Schristos 
5310061c6a5Schristos /* warning:
5320061c6a5Schristos  *	heavily ascii-dependent.
5330061c6a5Schristos  */
534032a4398Schristos static void
mkprint(char * dst,char * src,size_t len)535032a4398Schristos mkprint(char *dst, char *src, size_t len)
5360061c6a5Schristos {
537032a4398Schristos 	while(len > 0 && isblank((unsigned char) *src))
538032a4398Schristos 		len--, src++;
5390061c6a5Schristos 
540032a4398Schristos 	(void)strvisx(dst, src, len, VIS_TAB|VIS_NL);
5410061c6a5Schristos }
5420061c6a5Schristos 
5430061c6a5Schristos /* warning:
5440061c6a5Schristos  *	returns a pointer to malloc'd storage, you must call free yourself.
5450061c6a5Schristos  */
5460061c6a5Schristos char *
mkprints(char * src,size_t len)547032a4398Schristos mkprints(char *src, size_t len)
5480061c6a5Schristos {
5490061c6a5Schristos 	char *dst = malloc(len*4 + 1);
5500061c6a5Schristos 
5510061c6a5Schristos 	if (dst)
5520061c6a5Schristos 		mkprint(dst, src, len);
5530061c6a5Schristos 
5540061c6a5Schristos 	return (dst);
5550061c6a5Schristos }
5560061c6a5Schristos 
5570061c6a5Schristos #ifdef MAIL_DATE
5580061c6a5Schristos /* Sat, 27 Feb 1993 11:44:51 -0800 (CST)
5590061c6a5Schristos  * 1234567890123456789012345678901234567
5600061c6a5Schristos  */
5610061c6a5Schristos char *
arpadate(time_t * clock)562032a4398Schristos arpadate(time_t *clock)
5630061c6a5Schristos {
5640061c6a5Schristos 	time_t t = clock ? *clock : time((TIME_T) 0);
5650061c6a5Schristos 	struct tm tm = *localtime(&t);
5660061c6a5Schristos 	long gmtoff = get_gmtoff(&t, &tm);
5670061c6a5Schristos 	int hours = gmtoff / SECONDS_PER_HOUR;
5680061c6a5Schristos 	int minutes = (gmtoff - (hours * SECONDS_PER_HOUR)) / SECONDS_PER_MINUTE;
5690061c6a5Schristos 	static char ret[64];	/* zone name might be >3 chars */
5700061c6a5Schristos 
571032a4398Schristos 	if (minutes < 0)
572032a4398Schristos 		minutes = -minutes;
573032a4398Schristos 
574032a4398Schristos 	(void)strftime(ret, sizeof(ret), "%a, %e %b %Y %T ????? (%Z)", &tm);
575032a4398Schristos 	(void)snprintf(strchr(ret, '?'), "% .2d%.2d", hours, minutes);
576032a4398Schristos 	ret[sizeof(ret) - 1] = '\0';
577032a4398Schristos 	return ret;
5780061c6a5Schristos }
5790061c6a5Schristos #endif /*MAIL_DATE*/
5800061c6a5Schristos 
5810061c6a5Schristos size_t
strlens(const char * last,...)5820061c6a5Schristos strlens(const char *last, ...) {
5830061c6a5Schristos 	va_list ap;
5840061c6a5Schristos 	size_t ret = 0;
5850061c6a5Schristos 	const char *str;
5860061c6a5Schristos 
5870061c6a5Schristos 	va_start(ap, last);
5880061c6a5Schristos 	for (str = last; str != NULL; str = va_arg(ap, const char *))
5890061c6a5Schristos 		ret += strlen(str);
5900061c6a5Schristos 	va_end(ap);
5910061c6a5Schristos 	return (ret);
5920061c6a5Schristos }
5930061c6a5Schristos 
5940061c6a5Schristos /* Return the offset from GMT in seconds (algorithm taken from sendmail).
5950061c6a5Schristos  *
5960061c6a5Schristos  * warning:
5970061c6a5Schristos  *	clobbers the static storage space used by localtime() and gmtime().
5980061c6a5Schristos  *	If the local pointer is non-NULL it *must* point to a local copy.
5990061c6a5Schristos  */
6000061c6a5Schristos #ifndef HAVE_TM_GMTOFF
get_gmtoff(time_t * clock,struct tm * local)6010061c6a5Schristos long get_gmtoff(time_t *clock, struct tm *local)
6020061c6a5Schristos {
6030061c6a5Schristos 	struct tm gmt;
6040061c6a5Schristos 	long offset;
6050061c6a5Schristos 
6060061c6a5Schristos 	gmt = *gmtime(clock);
6070061c6a5Schristos 	if (local == NULL)
6080061c6a5Schristos 		local = localtime(clock);
6090061c6a5Schristos 
6100061c6a5Schristos 	offset = (local->tm_sec - gmt.tm_sec) +
6110061c6a5Schristos 	    ((local->tm_min - gmt.tm_min) * 60) +
6120061c6a5Schristos 	    ((local->tm_hour - gmt.tm_hour) * 3600);
6130061c6a5Schristos 
6140061c6a5Schristos 	/* Timezone may cause year rollover to happen on a different day. */
6150061c6a5Schristos 	if (local->tm_year < gmt.tm_year)
6160061c6a5Schristos 		offset -= 24 * 3600;
6170061c6a5Schristos 	else if (local->tm_year > gmt.tm_year)
6180061c6a5Schristos 		offset -= 24 * 3600;
6190061c6a5Schristos 	else if (local->tm_yday < gmt.tm_yday)
6200061c6a5Schristos 		offset -= 24 * 3600;
6210061c6a5Schristos 	else if (local->tm_yday > gmt.tm_yday)
6220061c6a5Schristos 		offset += 24 * 3600;
6230061c6a5Schristos 
6240061c6a5Schristos 	return (offset);
6250061c6a5Schristos }
6260061c6a5Schristos #endif /* HAVE_TM_GMTOFF */
627