Home | History | Annotate | Line # | Download | only in newsyslog
newsyslog.c revision 1.21.4.2
      1  1.21.4.2       he /*	$NetBSD: newsyslog.c,v 1.21.4.2 2002/03/06 23:34:45 he Exp $	*/
      2      1.12  thorpej 
      3       1.1      cgd /*
      4  1.21.4.2       he  * Copyright (c) 1999, 2000 Andrew Doran <ad (at) NetBSD.org>
      5  1.21.4.2       he  * All rights reserved.
      6  1.21.4.2       he  *
      7  1.21.4.2       he  * Redistribution and use in source and binary forms, with or without
      8  1.21.4.2       he  * modification, are permitted provided that the following conditions
      9  1.21.4.2       he  * are met:
     10  1.21.4.2       he  * 1. Redistributions of source code must retain the above copyright
     11  1.21.4.2       he  *    notice, this list of conditions and the following disclaimer.
     12  1.21.4.2       he  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.21.4.2       he  *    notice, this list of conditions and the following disclaimer in the
     14  1.21.4.2       he  *    documentation and/or other materials provided with the distribution.
     15  1.21.4.2       he  *
     16  1.21.4.2       he  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.21.4.2       he  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.21.4.2       he  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.21.4.2       he  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.21.4.2       he  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.21.4.2       he  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.21.4.2       he  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.21.4.2       he  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.21.4.2       he  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.21.4.2       he  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.21.4.2       he  * SUCH DAMAGE.
     27  1.21.4.2       he  *
     28       1.1      cgd  */
     29       1.1      cgd 
     30       1.1      cgd /*
     31  1.21.4.2       he  * This file contains changes from the Open Software Foundation.
     32  1.21.4.2       he  */
     33       1.1      cgd 
     34  1.21.4.2       he /*
     35  1.21.4.2       he  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
     36  1.21.4.2       he  *
     37  1.21.4.2       he  * Permission to use, copy, modify, and distribute this software
     38  1.21.4.2       he  * and its documentation for any purpose and without fee is
     39  1.21.4.2       he  * hereby granted, provided that the above copyright notice
     40  1.21.4.2       he  * appear in all copies and that both that copyright notice and
     41  1.21.4.2       he  * this permission notice appear in supporting documentation,
     42  1.21.4.2       he  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
     43  1.21.4.2       he  * used in advertising or publicity pertaining to distribution
     44  1.21.4.2       he  * of the software without specific, written prior permission.
     45  1.21.4.2       he  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
     46  1.21.4.2       he  * the suitability of this software for any purpose.  It is
     47  1.21.4.2       he  * provided "as is" without express or implied warranty.
     48  1.21.4.2       he  *
     49  1.21.4.2       he  */
     50       1.1      cgd 
     51       1.1      cgd /*
     52  1.21.4.2       he  * newsyslog(8) - a program to roll over log files provided that specified
     53  1.21.4.2       he  * critera are met, optionally preserving a number of historical log files.
     54       1.1      cgd  */
     55       1.1      cgd 
     56      1.13    lukem #include <sys/cdefs.h>
     57       1.5  mycroft #ifndef lint
     58  1.21.4.2       he __RCSID("$NetBSD: newsyslog.c,v 1.21.4.2 2002/03/06 23:34:45 he Exp $");
     59       1.5  mycroft #endif /* not lint */
     60       1.1      cgd 
     61       1.1      cgd #include <sys/types.h>
     62       1.1      cgd #include <sys/time.h>
     63       1.1      cgd #include <sys/stat.h>
     64       1.1      cgd #include <sys/param.h>
     65       1.1      cgd #include <sys/wait.h>
     66       1.1      cgd 
     67      1.13    lukem #include <ctype.h>
     68      1.13    lukem #include <fcntl.h>
     69      1.13    lukem #include <grp.h>
     70      1.13    lukem #include <pwd.h>
     71      1.13    lukem #include <signal.h>
     72      1.13    lukem #include <stdio.h>
     73      1.13    lukem #include <stdlib.h>
     74  1.21.4.2       he #include <stdarg.h>
     75      1.13    lukem #include <string.h>
     76      1.14   kleink #include <time.h>
     77      1.13    lukem #include <unistd.h>
     78  1.21.4.2       he #include <errno.h>
     79  1.21.4.2       he #include <err.h>
     80  1.21.4.2       he #include <util.h>
     81  1.21.4.2       he #include <paths.h>
     82  1.21.4.2       he 
     83  1.21.4.2       he #include "pathnames.h"
     84  1.21.4.2       he 
     85  1.21.4.2       he #define	PRHDRINFO(x)	((void)(verbose ? printf x : 0))
     86  1.21.4.2       he #define	PRINFO(x)	((void)(verbose ? printf("  ") + printf x : 0))
     87  1.21.4.2       he 
     88  1.21.4.2       he #define	CE_COMPRESS	0x01	/* Compress the archived log files */
     89  1.21.4.2       he #define	CE_BINARY	0x02	/* Logfile is a binary file/non-syslog */
     90  1.21.4.2       he #define	CE_NOSIGNAL	0x04	/* Don't send a signal when trimmed */
     91  1.21.4.2       he #define	CE_CREATE	0x08	/* Create log file if none exists */
     92  1.21.4.2       he #define	CE_PLAIN0	0x10	/* Do not compress zero'th history file */
     93      1.13    lukem 
     94       1.1      cgd struct conf_entry {
     95  1.21.4.2       he 	uid_t	uid;			/* Owner of log */
     96  1.21.4.2       he 	gid_t	gid;			/* Group of log */
     97  1.21.4.2       he 	mode_t	mode;			/* File permissions */
     98  1.21.4.2       he 	int	numhist;		/* Number of historical logs to keep */
     99  1.21.4.2       he 	size_t	maxsize;		/* Maximum log size */
    100  1.21.4.2       he 	int	maxage;			/* Hours between log trimming */
    101  1.21.4.2       he 	time_t	trimat;			/* Specific trim time */
    102  1.21.4.2       he 	int	flags;			/* Flags (CE_*) */
    103  1.21.4.2       he 	int	signum;			/* Signal to send */
    104  1.21.4.2       he 	char	pidfile[MAXPATHLEN];	/* File containing PID to signal */
    105  1.21.4.2       he 	char	logfile[MAXPATHLEN];	/* Path to log file */
    106       1.1      cgd };
    107       1.1      cgd 
    108  1.21.4.2       he int	verbose;			/* Be verbose */
    109  1.21.4.2       he int	noaction;			/* Take no action */
    110  1.21.4.2       he int	nosignal;			/* Do not send signals */
    111  1.21.4.2       he char    hostname[MAXHOSTNAMELEN + 1];	/* Hostname, stripped of domain */
    112  1.21.4.2       he uid_t	myeuid;				/* EUID we are running with */
    113  1.21.4.2       he 
    114  1.21.4.2       he int	getsig(const char *);
    115  1.21.4.2       he int	isnumber(const char *);
    116  1.21.4.2       he int	main(int, char **);
    117  1.21.4.2       he int	parse_cfgline(struct conf_entry *, FILE *, size_t *);
    118  1.21.4.2       he time_t	parse_iso8601(char *);
    119  1.21.4.2       he time_t	parse_dwm(char *);
    120  1.21.4.2       he int	parse_userspec(const char *, struct passwd **, struct group **);
    121  1.21.4.2       he pid_t	readpidfile(const char *);
    122  1.21.4.2       he void	usage(void);
    123  1.21.4.2       he 
    124  1.21.4.2       he void	log_compress(struct conf_entry *, const char *);
    125  1.21.4.2       he void	log_create(struct conf_entry *);
    126  1.21.4.2       he void	log_examine(struct conf_entry *, int);
    127  1.21.4.2       he void	log_trim(struct conf_entry *);
    128  1.21.4.2       he void	log_trimmed(struct conf_entry *);
    129       1.1      cgd 
    130  1.21.4.2       he /*
    131  1.21.4.2       he  * Program entry point.
    132  1.21.4.2       he  */
    133      1.13    lukem int
    134  1.21.4.2       he main(int argc, char **argv)
    135       1.1      cgd {
    136  1.21.4.2       he 	struct conf_entry log;
    137  1.21.4.2       he 	FILE *fd;
    138  1.21.4.2       he 	char *p, *cfile;
    139  1.21.4.2       he 	int c, needroot, i, force;
    140  1.21.4.2       he 	size_t lineno;
    141  1.21.4.2       he 
    142  1.21.4.2       he 	force = 0;
    143  1.21.4.2       he 	needroot = 1;
    144  1.21.4.2       he 	cfile = _PATH_NEWSYSLOGCONF;
    145       1.1      cgd 
    146  1.21.4.2       he 	gethostname(hostname, sizeof (hostname));
    147  1.21.4.2       he 	hostname[sizeof (hostname) - 1] = '\0';
    148       1.8      jtc 
    149  1.21.4.2       he 	/* Truncate domain. */
    150  1.21.4.2       he 	if ((p = strchr(hostname, '.')) != NULL)
    151       1.8      jtc 		*p = '\0';
    152  1.21.4.2       he 
    153  1.21.4.2       he 	/* Parse command line options. */
    154  1.21.4.2       he 	while ((c = getopt(argc, argv, "f:nrsvF")) != -1) {
    155  1.21.4.2       he 		switch (c) {
    156  1.21.4.2       he 		case 'f':
    157  1.21.4.2       he 			cfile = optarg;
    158  1.21.4.2       he 			break;
    159  1.21.4.2       he 		case 'n':
    160  1.21.4.2       he 			noaction = 1;
    161  1.21.4.2       he 			verbose = 1;
    162  1.21.4.2       he 			break;
    163  1.21.4.2       he 		case 'r':
    164  1.21.4.2       he 			needroot = 0;
    165  1.21.4.2       he 			break;
    166  1.21.4.2       he 		case 's':
    167  1.21.4.2       he 			nosignal = 1;
    168  1.21.4.2       he 			break;
    169  1.21.4.2       he 		case 'v':
    170  1.21.4.2       he 			verbose = 1;
    171  1.21.4.2       he 			break;
    172  1.21.4.2       he 		case 'F':
    173  1.21.4.2       he 			force = 1;
    174  1.21.4.2       he 			break;
    175  1.21.4.2       he 		default:
    176  1.21.4.2       he 			usage();
    177  1.21.4.2       he 			/* NOTREACHED */
    178  1.21.4.2       he 		}
    179  1.21.4.2       he 	}
    180  1.21.4.2       he 
    181  1.21.4.2       he 	myeuid = geteuid();
    182  1.21.4.2       he 	if (needroot && myeuid != 0)
    183  1.21.4.2       he 		errx(EXIT_FAILURE, "must be run as root");
    184  1.21.4.2       he 
    185  1.21.4.2       he 	argc -= optind;
    186  1.21.4.2       he 	argv += optind;
    187  1.21.4.2       he 
    188  1.21.4.2       he 	if (strcmp(cfile, "-") == 0)
    189  1.21.4.2       he 		fd = stdin;
    190  1.21.4.2       he 	else if ((fd = fopen(cfile, "rt")) == NULL)
    191  1.21.4.2       he 		err(EXIT_FAILURE, "%s", cfile);
    192  1.21.4.2       he 
    193  1.21.4.2       he 	for (lineno = 0; !parse_cfgline(&log, fd, &lineno);) {
    194  1.21.4.2       he 		/*
    195  1.21.4.2       he 		 * If specific log files were specified, touch only
    196  1.21.4.2       he 		 * those.
    197  1.21.4.2       he 		 */
    198  1.21.4.2       he 		if (argc != 0) {
    199  1.21.4.2       he 			for (i = 0; i < argc; i++)
    200  1.21.4.2       he 				if (strcmp(log.logfile, argv[i]) == 0)
    201  1.21.4.2       he 					break;
    202  1.21.4.2       he 			if (i == argc)
    203  1.21.4.2       he 				continue;
    204  1.21.4.2       he 		}
    205  1.21.4.2       he 		log_examine(&log, force);
    206       1.8      jtc 	}
    207  1.21.4.2       he 
    208  1.21.4.2       he 	if (fd != stdin)
    209  1.21.4.2       he 		fclose(fd);
    210       1.1      cgd 
    211  1.21.4.2       he 	exit(EXIT_SUCCESS);
    212  1.21.4.2       he 	/* NOTREACHED */
    213      1.13    lukem }
    214       1.1      cgd 
    215  1.21.4.2       he /*
    216  1.21.4.2       he  * Parse a single line from the configuration file.
    217  1.21.4.2       he  */
    218  1.21.4.2       he int
    219  1.21.4.2       he parse_cfgline(struct conf_entry *log, FILE *fd, size_t *_lineno)
    220       1.1      cgd {
    221  1.21.4.2       he 	char *line, *q, **ap, *argv[10];
    222  1.21.4.2       he 	struct passwd *pw;
    223  1.21.4.2       he 	struct group *gr;
    224  1.21.4.2       he 	int nf, lineno, i, rv;
    225  1.21.4.2       he 
    226  1.21.4.2       he 	rv = -1;
    227  1.21.4.2       he 	line = NULL;
    228  1.21.4.2       he 
    229  1.21.4.2       he 	/* Place the white-space separated fields into an array. */
    230  1.21.4.2       he 	do {
    231  1.21.4.2       he 		if (line != NULL)
    232  1.21.4.2       he 			free(line);
    233  1.21.4.2       he 		if ((line = fparseln(fd, NULL, _lineno, NULL, 0)) == NULL)
    234  1.21.4.2       he 			return (rv);
    235  1.21.4.2       he 		lineno = (int)*_lineno;
    236  1.21.4.2       he 
    237  1.21.4.2       he 		for (ap = argv, nf = 0; (*ap = strsep(&line, " \t")) != NULL;)
    238  1.21.4.2       he 			if (**ap != '\0') {
    239  1.21.4.2       he 				if (++nf == sizeof (argv) / sizeof (argv[0])) {
    240  1.21.4.2       he 					warnx("config line %d: "
    241  1.21.4.2       he 					    "too many fields", lineno);
    242  1.21.4.2       he 					goto bad;
    243  1.21.4.2       he 				}
    244  1.21.4.2       he 				ap++;
    245  1.21.4.2       he 			}
    246  1.21.4.2       he 	} while (nf == 0);
    247  1.21.4.2       he 
    248  1.21.4.2       he 	if (nf < 6)
    249  1.21.4.2       he 		errx(EXIT_FAILURE, "config line %d: too few fields", lineno);
    250  1.21.4.2       he 
    251  1.21.4.2       he 	memset(log, 0, sizeof (*log));
    252  1.21.4.2       he 
    253  1.21.4.2       he 	/* logfile_name */
    254  1.21.4.2       he 	ap = argv;
    255  1.21.4.2       he 	strlcpy(log->logfile, *ap++, sizeof (log->logfile));
    256  1.21.4.2       he 	if (log->logfile[0] != '/')
    257  1.21.4.2       he 		errx(EXIT_FAILURE,
    258  1.21.4.2       he 		    "config line %d: logfile must have a full path", lineno);
    259  1.21.4.2       he 
    260  1.21.4.2       he 	/* owner:group */
    261  1.21.4.2       he 	if (strchr(*ap, ':') != NULL || strchr(*ap, '.') != NULL) {
    262  1.21.4.2       he 		if (parse_userspec(*ap++, &pw, &gr)) {
    263  1.21.4.2       he 			warnx("config line %d: unknown user/group", lineno);
    264  1.21.4.2       he 			goto bad;
    265  1.21.4.2       he 		}
    266  1.21.4.2       he 
    267  1.21.4.2       he 		/*
    268  1.21.4.2       he 		 * We may only change the file's owner as non-root.
    269  1.21.4.2       he 		 */
    270  1.21.4.2       he 		if (myeuid != 0) {
    271  1.21.4.2       he 			if (pw->pw_uid != myeuid)
    272  1.21.4.2       he 				errx(EXIT_FAILURE, "config line %d: user:group "
    273  1.21.4.2       he 				    "as non-root must match current user",
    274  1.21.4.2       he 				    lineno);
    275  1.21.4.2       he 			log->uid = (uid_t)-1;
    276  1.21.4.2       he 		} else
    277  1.21.4.2       he 			log->uid = pw->pw_uid;
    278  1.21.4.2       he 		log->gid = gr->gr_gid;
    279  1.21.4.2       he 		if (nf < 7)
    280  1.21.4.2       he 			errx(EXIT_FAILURE, "config line %d: too few fields",
    281  1.21.4.2       he 			    lineno);
    282  1.21.4.2       he 	} else if (myeuid != 0) {
    283  1.21.4.2       he 		log->uid = (uid_t)-1;
    284  1.21.4.2       he 		log->gid = getegid();
    285  1.21.4.2       he 	}
    286       1.1      cgd 
    287  1.21.4.2       he 	/* mode */
    288  1.21.4.2       he 	if (sscanf(*ap++, "%o", &i) != 1) {
    289  1.21.4.2       he 		warnx("config line %d: bad permissions", lineno);
    290  1.21.4.2       he 		goto bad;
    291  1.21.4.2       he 	}
    292  1.21.4.2       he 	log->mode = (mode_t)i;
    293       1.1      cgd 
    294  1.21.4.2       he 	/* count */
    295  1.21.4.2       he 	if (sscanf(*ap++, "%d", &log->numhist) != 1) {
    296  1.21.4.2       he 		warnx("config line %d: bad log count", lineno);
    297  1.21.4.2       he 		goto bad;
    298  1.21.4.2       he 	}
    299      1.20       ad 
    300  1.21.4.2       he 	/* size */
    301  1.21.4.2       he 	if (isdigit(**ap)) {
    302  1.21.4.2       he 		log->maxsize = (int)strtol(*ap, &q, 0);
    303  1.21.4.2       he 		if (*q != '\0') {
    304  1.21.4.2       he 			warnx("config line %d: bad log size", lineno);
    305  1.21.4.2       he 			goto bad;
    306  1.21.4.2       he 		}
    307  1.21.4.2       he 	} else if (**ap == '*')
    308  1.21.4.2       he 		log->maxsize = (size_t)-1;
    309  1.21.4.2       he 	else {
    310  1.21.4.2       he 		warnx("config line %d: bad log size", lineno);
    311  1.21.4.2       he 		goto bad;
    312  1.21.4.2       he 	}
    313  1.21.4.2       he 	ap++;
    314      1.11  thorpej 
    315  1.21.4.2       he 	/* when */
    316  1.21.4.2       he 	log->maxage = -1;
    317  1.21.4.2       he 	log->trimat = (time_t)-1;
    318  1.21.4.2       he 	q = *ap++;
    319  1.21.4.2       he 
    320  1.21.4.2       he 	if (strcmp(q, "*") != 0) {
    321  1.21.4.2       he 		if (isdigit(*q))
    322  1.21.4.2       he 			log->maxage = (int)strtol(q, &q, 10);
    323  1.21.4.2       he 
    324  1.21.4.2       he 		/*
    325  1.21.4.2       he 		 * One class of periodic interval specification can follow a
    326  1.21.4.2       he 		 * maximum age specification.  Handle it.
    327  1.21.4.2       he 		 */
    328  1.21.4.2       he 		if (*q == '@') {
    329  1.21.4.2       he 			log->trimat = parse_iso8601(q + 1);
    330  1.21.4.2       he 			if (log->trimat == (time_t)-1) {
    331  1.21.4.2       he 				warnx("config line %d: bad trim time", lineno);
    332  1.21.4.2       he 				goto bad;
    333  1.21.4.2       he 			}
    334  1.21.4.2       he 		} else if (*q == '$') {
    335  1.21.4.2       he 			if ((log->trimat = parse_dwm(q + 1)) == (time_t)-1) {
    336  1.21.4.2       he 				warnx("config line %d: bad trim time", lineno);
    337  1.21.4.2       he 				goto bad;
    338  1.21.4.2       he 			}
    339  1.21.4.2       he 		} else if (log->maxage == -1) {
    340  1.21.4.2       he 			warnx("config line %d: bad log age", lineno);
    341  1.21.4.2       he 			goto bad;
    342      1.21       ad 		}
    343      1.21       ad 	}
    344      1.21       ad 
    345  1.21.4.2       he 	/* flags */
    346  1.21.4.2       he 	log->flags = (nosignal ? CE_NOSIGNAL : 0);
    347  1.21.4.2       he 
    348  1.21.4.2       he 	for (q = *ap++; q != NULL && *q != '\0'; q++) {
    349  1.21.4.2       he 		switch (tolower(*q)) {
    350  1.21.4.2       he 		case 'b':
    351  1.21.4.2       he 			log->flags |= CE_BINARY;
    352  1.21.4.2       he 			break;
    353  1.21.4.2       he 		case 'c':
    354  1.21.4.2       he 			log->flags |= CE_CREATE;
    355  1.21.4.2       he 			break;
    356  1.21.4.2       he 		case 'n':
    357  1.21.4.2       he 			log->flags |= CE_NOSIGNAL;
    358  1.21.4.2       he 			break;
    359  1.21.4.2       he 		case 'p':
    360  1.21.4.2       he 			log->flags |= CE_PLAIN0;
    361  1.21.4.2       he 			break;
    362  1.21.4.2       he 		case 'z':
    363  1.21.4.2       he 			log->flags |= CE_COMPRESS;
    364  1.21.4.2       he 			break;
    365  1.21.4.2       he 		case '-':
    366  1.21.4.2       he 			break;
    367  1.21.4.2       he 		default:
    368  1.21.4.2       he 			warnx("config line %d: bad flags", lineno);
    369  1.21.4.2       he 			goto bad;
    370  1.21.4.2       he 		}
    371  1.21.4.2       he 	}
    372  1.21.4.2       he 
    373  1.21.4.2       he 	/* path_to_pidfile */
    374  1.21.4.2       he 	if (*ap != NULL && **ap == '/')
    375  1.21.4.2       he 		strlcpy(log->pidfile, *ap++, sizeof (log->pidfile));
    376  1.21.4.2       he 	else
    377  1.21.4.2       he 		log->pidfile[0] = '\0';
    378  1.21.4.2       he 
    379  1.21.4.2       he 	/* sigtype */
    380  1.21.4.2       he 	if (*ap != NULL) {
    381  1.21.4.2       he 		if ((log->signum = getsig(*ap++)) < 0) {
    382  1.21.4.2       he 			warnx("config line %d: bad signal type", lineno);
    383  1.21.4.2       he 			goto bad;
    384  1.21.4.2       he 		}
    385  1.21.4.2       he 	} else
    386  1.21.4.2       he 		log->signum = SIGHUP;
    387  1.21.4.2       he 
    388  1.21.4.2       he 	rv = 0;
    389  1.21.4.2       he 
    390  1.21.4.2       he bad:
    391  1.21.4.2       he 	free(line);
    392  1.21.4.2       he 	return (rv);
    393       1.1      cgd }
    394       1.1      cgd 
    395  1.21.4.2       he /*
    396  1.21.4.2       he  * Examine a log file.  If the trim conditions are met, call log_trim() to
    397  1.21.4.2       he  * trim the log file.
    398  1.21.4.2       he  */
    399  1.21.4.2       he void
    400  1.21.4.2       he log_examine(struct conf_entry *log, int force)
    401       1.1      cgd {
    402  1.21.4.2       he 	struct stat sb;
    403  1.21.4.2       he 	size_t size;
    404  1.21.4.2       he 	int age, trim;
    405  1.21.4.2       he 	char tmp[MAXPATHLEN];
    406  1.21.4.2       he 	const char *reason;
    407  1.21.4.2       he 	time_t now;
    408  1.21.4.2       he 
    409  1.21.4.2       he 	now = time(NULL);
    410  1.21.4.2       he 
    411  1.21.4.2       he 	PRHDRINFO(("\n%s <%d%s>: ", log->logfile, log->numhist,
    412  1.21.4.2       he 	    (log->flags & CE_COMPRESS) != 0 ? "Z" : ""));
    413  1.21.4.2       he 
    414  1.21.4.2       he 	/*
    415  1.21.4.2       he 	 * stat() the logfile.  If it doesn't exist and the `c' flag has
    416  1.21.4.2       he 	 * been specified, create it.  If it doesn't exist and the `c' flag
    417  1.21.4.2       he 	 * hasn't been specified, give up.
    418  1.21.4.2       he 	 */
    419  1.21.4.2       he 	if (stat(log->logfile, &sb) < 0) {
    420  1.21.4.2       he 		if (errno == ENOENT && (log->flags & CE_CREATE) != 0) {
    421  1.21.4.2       he 			PRHDRINFO(("creating; "));
    422  1.21.4.2       he 			if (!noaction)
    423  1.21.4.2       he 				log_create(log);
    424  1.21.4.2       he 			else {
    425  1.21.4.2       he 				PRHDRINFO(("can't proceed with `-n'\n"));
    426  1.21.4.2       he 				return;
    427  1.21.4.2       he 			}
    428  1.21.4.2       he 			if (stat(log->logfile, &sb))
    429  1.21.4.2       he 				err(EXIT_FAILURE, "%s", log->logfile);
    430  1.21.4.2       he 		} else if (errno == ENOENT) {
    431  1.21.4.2       he 			PRHDRINFO(("does not exist --> skip log\n"));
    432  1.21.4.2       he 			return;
    433  1.21.4.2       he 		} else if (errno != 0)
    434  1.21.4.2       he 			err(EXIT_FAILURE, "%s", log->logfile);
    435  1.21.4.2       he 	}
    436  1.21.4.2       he 
    437  1.21.4.2       he 	/* Size of the log file in kB. */
    438  1.21.4.2       he 	size = ((size_t)sb.st_blocks * S_BLKSIZE) >> 10;
    439  1.21.4.2       he 
    440  1.21.4.2       he 	/*
    441  1.21.4.2       he 	 * Get the age (expressed in hours) of the current log file with
    442  1.21.4.2       he 	 * respect to the newest historical log file.
    443  1.21.4.2       he 	 */
    444  1.21.4.2       he 	strlcpy(tmp, log->logfile, sizeof (tmp));
    445  1.21.4.2       he 	strlcat(tmp, ".0", sizeof (tmp));
    446  1.21.4.2       he 	if (stat(tmp, &sb) < 0) {
    447  1.21.4.2       he 		strlcat(tmp, ".gz", sizeof (tmp));
    448  1.21.4.2       he 		if (stat(tmp, &sb) < 0)
    449  1.21.4.2       he 			age = -1;
    450  1.21.4.2       he 		else
    451  1.21.4.2       he 			age = (int)(now - sb.st_mtime + 1800) / 3600;
    452  1.21.4.2       he 	} else
    453  1.21.4.2       he 		age = (int)(now - sb.st_mtime + 1800) / 3600;
    454  1.21.4.2       he 
    455  1.21.4.2       he 	/*
    456  1.21.4.2       he 	 * Examine the set of given trim conditions and if any one is met,
    457  1.21.4.2       he 	 * trim the log.
    458  1.21.4.2       he 	 *
    459  1.21.4.2       he 	 * Note: if `maxage' or `trimat' is used as a trim condition, we
    460  1.21.4.2       he 	 * need at least one historical log file to determine the `age' of
    461  1.21.4.2       he 	 * the active log file.  WRT `trimat', we will trim up to one hour
    462  1.21.4.2       he 	 * after the specific trim time has passed - we need to know if
    463  1.21.4.2       he 	 * we've trimmed to meet that condition with a previous invocation
    464  1.21.4.2       he 	 * of newsyslog(8).
    465  1.21.4.2       he 	 */
    466  1.21.4.2       he 	if (log->maxage >= 0 && (age >= log->maxage || age < 0)) {
    467  1.21.4.2       he 		trim = 1;
    468  1.21.4.2       he 		reason = "log age > interval";
    469  1.21.4.2       he 	} else if (size >= log->maxsize) {
    470  1.21.4.2       he 		trim = 1;
    471  1.21.4.2       he 		reason = "log size > size";
    472  1.21.4.2       he 	} else if (log->trimat != (time_t)-1 && now >= log->trimat &&
    473  1.21.4.2       he 		   (age == -1 || age > 1) &&
    474  1.21.4.2       he 		   difftime(now, log->trimat) < 60 * 60) {
    475  1.21.4.2       he 		trim = 1;
    476  1.21.4.2       he 		reason = "specific trim time";
    477  1.21.4.2       he 	} else {
    478  1.21.4.2       he 		trim = force;
    479  1.21.4.2       he 		reason = "trim forced";
    480  1.21.4.2       he 	}
    481  1.21.4.2       he 
    482  1.21.4.2       he 	if (trim) {
    483  1.21.4.2       he 		PRHDRINFO(("--> trim log (%s)\n", reason));
    484  1.21.4.2       he 		log_trim(log);
    485  1.21.4.2       he 	} else
    486  1.21.4.2       he 		PRHDRINFO(("--> skip log (trim conditions not met)\n"));
    487       1.1      cgd }
    488       1.1      cgd 
    489  1.21.4.2       he /*
    490  1.21.4.2       he  * Trim the specified log file.
    491  1.21.4.2       he  */
    492      1.13    lukem void
    493  1.21.4.2       he log_trim(struct conf_entry *log)
    494       1.1      cgd {
    495  1.21.4.2       he 	char file1[MAXPATHLEN], file2[MAXPATHLEN];
    496  1.21.4.2       he 	int i;
    497  1.21.4.2       he 	struct stat st;
    498  1.21.4.2       he 	pid_t pid;
    499  1.21.4.2       he 
    500  1.21.4.2       he 	/* Remove oldest historical log. */
    501  1.21.4.2       he 	snprintf(file1, sizeof (file1), "%s.%d", log->logfile,
    502  1.21.4.2       he 	    log->numhist - 1);
    503  1.21.4.2       he 
    504  1.21.4.2       he 	PRINFO(("rm -f %s\n", file1));
    505  1.21.4.2       he 	if (!noaction)
    506  1.21.4.2       he 		unlink(file1);
    507  1.21.4.2       he 	strlcat(file1, ".gz", sizeof (file1));
    508  1.21.4.2       he 	PRINFO(("rm -f %s\n", file1));
    509  1.21.4.2       he 	if (!noaction)
    510  1.21.4.2       he 		unlink(file1);
    511  1.21.4.2       he 
    512  1.21.4.2       he 	/* Move down log files. */
    513  1.21.4.2       he 	for (i = log->numhist - 1; i != 0; i--) {
    514  1.21.4.2       he 		snprintf(file1, sizeof (file1), "%s.%d", log->logfile, i - 1);
    515  1.21.4.2       he 		snprintf(file2, sizeof (file2), "%s.%d", log->logfile, i);
    516  1.21.4.2       he 
    517  1.21.4.2       he 		if (lstat(file1, &st) != 0) {
    518  1.21.4.2       he 			strlcat(file1, ".gz", sizeof (file1));
    519  1.21.4.2       he 			strlcat(file2, ".gz", sizeof (file2));
    520  1.21.4.2       he 			if (lstat(file1, &st) != 0)
    521  1.21.4.2       he 				continue;
    522  1.21.4.2       he 		}
    523  1.21.4.2       he 
    524  1.21.4.2       he 		PRINFO(("mv %s %s\n", file1, file2));
    525  1.21.4.2       he 		if (!noaction)
    526  1.21.4.2       he 			if (rename(file1, file2))
    527  1.21.4.2       he 				err(EXIT_FAILURE, "%s", file1);
    528  1.21.4.2       he 		PRINFO(("chmod %o %s\n", log->mode, file2));
    529  1.21.4.2       he 		if (!noaction)
    530  1.21.4.2       he 			if (chmod(file2, log->mode))
    531  1.21.4.2       he 				err(EXIT_FAILURE, "%s", file2);
    532  1.21.4.2       he 		PRINFO(("chown %d:%d %s\n", log->uid, log->gid,
    533  1.21.4.2       he 		    file2));
    534  1.21.4.2       he 		if (!noaction)
    535  1.21.4.2       he 			if (chown(file2, log->uid, log->gid))
    536  1.21.4.2       he 				err(EXIT_FAILURE, "%s", file2);
    537  1.21.4.2       he 	}
    538  1.21.4.2       he 
    539  1.21.4.2       he 	/*
    540  1.21.4.2       he 	 * If a historical log file isn't compressed, and 'z' has been
    541  1.21.4.2       he 	 * specified, compress it.  (This is convenient, but is also needed
    542  1.21.4.2       he 	 * if 'p' has been specified.)  It should be noted that gzip(1)
    543  1.21.4.2       he 	 * preserves file ownership and file mode.
    544  1.21.4.2       he 	 */
    545  1.21.4.2       he 	for (i = (log->flags & CE_PLAIN0) != 0; i < log->numhist; i++) {
    546  1.21.4.2       he 		snprintf(file1, sizeof (file1), "%s.%d", log->logfile, i);
    547  1.21.4.2       he 		if (lstat(file1, &st) != 0)
    548  1.21.4.2       he 			continue;
    549  1.21.4.2       he 		snprintf(file2, sizeof (file2), "%s.gz", file1);
    550  1.21.4.2       he 		if (lstat(file2, &st) == 0)
    551  1.21.4.2       he 			continue;
    552  1.21.4.2       he 		log_compress(log, file1);
    553  1.21.4.2       he 	}
    554  1.21.4.2       he 
    555  1.21.4.2       he 	log_trimmed(log);
    556  1.21.4.2       he 
    557  1.21.4.2       he 	/* Create the historical log file if we're maintaining history. */
    558  1.21.4.2       he 	if (log->numhist == 0) {
    559  1.21.4.2       he 		PRINFO(("rm -f %s\n", log->logfile));
    560  1.21.4.2       he 		if (!noaction)
    561  1.21.4.2       he 			if (unlink(log->logfile))
    562  1.21.4.2       he 				err(EXIT_FAILURE, "%s", log->logfile);
    563  1.21.4.2       he 	} else {
    564  1.21.4.2       he 		snprintf(file1, sizeof (file1), "%s.0", log->logfile);
    565  1.21.4.2       he 		PRINFO(("mv %s %s\n", log->logfile, file1));
    566  1.21.4.2       he 		if (!noaction)
    567  1.21.4.2       he 			if (rename(log->logfile, file1))
    568  1.21.4.2       he 				err(EXIT_FAILURE, "%s", log->logfile);
    569  1.21.4.2       he 	}
    570  1.21.4.2       he 
    571  1.21.4.2       he 	PRINFO(("(create new log)\n"));
    572  1.21.4.2       he 	log_create(log);
    573  1.21.4.2       he 	log_trimmed(log);
    574  1.21.4.2       he 
    575  1.21.4.2       he 	/* Set the correct permissions on the log. */
    576  1.21.4.2       he 	PRINFO(("chmod %o %s\n", log->mode, log->logfile));
    577  1.21.4.2       he 	if (!noaction)
    578  1.21.4.2       he 		if (chmod(log->logfile, log->mode))
    579  1.21.4.2       he 			err(EXIT_FAILURE, "%s", log->logfile);
    580  1.21.4.2       he 
    581  1.21.4.2       he 	/* Do we need to signal a daemon? */
    582  1.21.4.2       he 	if ((log->flags & CE_NOSIGNAL) == 0) {
    583  1.21.4.2       he 		if (log->pidfile[0] != '\0')
    584  1.21.4.2       he 			pid = readpidfile(log->pidfile);
    585  1.21.4.2       he 		else
    586  1.21.4.2       he 			pid = readpidfile(_PATH_SYSLOGDPID);
    587  1.21.4.2       he 
    588  1.21.4.2       he 		if (pid != (pid_t)-1) {
    589  1.21.4.2       he 			PRINFO(("kill -%s %lu\n",
    590  1.21.4.2       he 			    sys_signame[log->signum], (u_long)pid));
    591  1.21.4.2       he 			if (!noaction)
    592  1.21.4.2       he 				if (kill(pid, log->signum))
    593  1.21.4.2       he 					warn("kill");
    594  1.21.4.2       he 		}
    595  1.21.4.2       he 	}
    596  1.21.4.2       he 
    597  1.21.4.2       he 	/* If the newest historical log is to be compressed, do it here. */
    598  1.21.4.2       he 	if ((log->flags & (CE_PLAIN0 | CE_COMPRESS)) == CE_COMPRESS) {
    599  1.21.4.2       he 		snprintf(file1, sizeof (file1), "%s.0", log->logfile);
    600  1.21.4.2       he 		if ((log->flags & CE_NOSIGNAL) == 0) {
    601  1.21.4.2       he 			PRINFO(("sleep for 10 seconds before compressing...\n"));
    602  1.21.4.2       he 			sleep(10);
    603  1.21.4.2       he 		}
    604  1.21.4.2       he 		log_compress(log, file1);
    605  1.21.4.1       ad 	}
    606       1.1      cgd }
    607       1.1      cgd 
    608  1.21.4.2       he /*
    609  1.21.4.2       he  * Write an entry to the log file recording the fact that it was trimmed.
    610  1.21.4.2       he  */
    611  1.21.4.2       he void
    612  1.21.4.2       he log_trimmed(struct conf_entry *log)
    613       1.1      cgd {
    614  1.21.4.2       he 	FILE *fd;
    615  1.21.4.2       he 	time_t now;
    616  1.21.4.2       he 	char *daytime;
    617  1.21.4.2       he 
    618  1.21.4.2       he 	if ((log->flags & CE_BINARY) != 0)
    619  1.21.4.2       he 		return;
    620  1.21.4.2       he 	PRINFO(("(append rotation notice to %s)\n", log->logfile));
    621  1.21.4.2       he 	if (noaction)
    622  1.21.4.2       he 		return;
    623  1.21.4.2       he 
    624  1.21.4.2       he 	if ((fd = fopen(log->logfile, "at")) == NULL)
    625  1.21.4.2       he 		err(EXIT_FAILURE, "%s", log->logfile);
    626  1.21.4.2       he 
    627  1.21.4.2       he 	now = time(NULL);
    628  1.21.4.2       he 	daytime = ctime(&now) + 4;
    629  1.21.4.2       he 	daytime[15] = '\0';
    630  1.21.4.2       he 
    631  1.21.4.2       he 	fprintf(fd, "%s %s newsyslog[%lu]: log file turned over\n", daytime,
    632  1.21.4.2       he 	    hostname, (u_long)getpid());
    633  1.21.4.2       he 	fclose(fd);
    634       1.1      cgd }
    635       1.1      cgd 
    636  1.21.4.2       he /*
    637  1.21.4.2       he  * Create a new log file.
    638  1.21.4.2       he  */
    639  1.21.4.2       he void
    640  1.21.4.2       he log_create(struct conf_entry *log)
    641       1.1      cgd {
    642  1.21.4.2       he 	int fd;
    643       1.1      cgd 
    644  1.21.4.2       he 	if (noaction)
    645  1.21.4.2       he 		return;
    646  1.21.4.2       he 
    647  1.21.4.2       he 	if ((fd = creat(log->logfile, log->mode)) < 0)
    648  1.21.4.2       he 		err(EXIT_FAILURE, "%s", log->logfile);
    649  1.21.4.2       he 	if (fchown(fd, log->uid, log->gid) < 0)
    650  1.21.4.2       he 		err(EXIT_FAILURE, "%s", log->logfile);
    651  1.21.4.2       he 	close(fd);
    652       1.1      cgd }
    653       1.1      cgd 
    654  1.21.4.2       he /*
    655  1.21.4.2       he  * Fork off gzip(1) to compress a log file.  This routine takes an
    656  1.21.4.2       he  * additional string argument (the name of the file to compress): it is also
    657  1.21.4.2       he  * used to compress historical log files other than the newest.
    658  1.21.4.2       he  */
    659  1.21.4.2       he void
    660  1.21.4.2       he log_compress(struct conf_entry *log, const char *fn)
    661       1.1      cgd {
    662  1.21.4.2       he 	char tmp[MAXPATHLEN];
    663  1.21.4.2       he 
    664  1.21.4.2       he 	PRINFO(("gzip %s\n", fn));
    665  1.21.4.2       he 	if (!noaction) {
    666  1.21.4.2       he 		pid_t pid;
    667  1.21.4.2       he 		int status;
    668  1.21.4.2       he 
    669  1.21.4.2       he 		if ((pid = vfork()) < 0)
    670  1.21.4.2       he 			err(EXIT_FAILURE, "vfork");
    671  1.21.4.2       he 		else if (pid == 0) {
    672  1.21.4.2       he 			execl(_PATH_GZIP, "gzip", "-f", fn, NULL);
    673  1.21.4.2       he 			_exit(EXIT_FAILURE);
    674  1.21.4.2       he 		}
    675  1.21.4.2       he 		while (waitpid(pid, &status, 0) != pid);
    676  1.21.4.2       he 
    677  1.21.4.2       he 		if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0))
    678  1.21.4.2       he 			errx(EXIT_FAILURE, "gzip failed");
    679  1.21.4.2       he 	}
    680  1.21.4.2       he 
    681  1.21.4.2       he 	snprintf(tmp, sizeof (tmp), "%s.gz", fn);
    682  1.21.4.2       he 	PRINFO(("chown %d:%d %s\n", log->uid, log->gid, tmp));
    683  1.21.4.2       he 	if (!noaction)
    684  1.21.4.2       he 		if (chown(tmp, log->uid, log->gid))
    685  1.21.4.2       he 			err(EXIT_FAILURE, "%s", tmp);
    686       1.1      cgd }
    687       1.1      cgd 
    688  1.21.4.2       he /*
    689  1.21.4.2       he  * Display program usage information.
    690  1.21.4.2       he  */
    691  1.21.4.2       he void
    692  1.21.4.2       he usage(void)
    693       1.1      cgd {
    694       1.1      cgd 
    695  1.21.4.2       he 	fprintf(stderr,
    696  1.21.4.2       he 	    "usage: newsyslog [-nrsvF] [-f config-file] [file ...]\n");
    697  1.21.4.2       he 	exit(EXIT_FAILURE);
    698  1.21.4.2       he }
    699       1.1      cgd 
    700  1.21.4.2       he /*
    701  1.21.4.2       he  * Return non-zero if a string represents a decimal value.
    702  1.21.4.2       he  */
    703      1.13    lukem int
    704  1.21.4.2       he isnumber(const char *string)
    705       1.1      cgd {
    706  1.21.4.2       he 
    707  1.21.4.2       he 	while (isdigit(*string))
    708  1.21.4.2       he 		string++;
    709  1.21.4.2       he 
    710  1.21.4.2       he 	return (*string == '\0');
    711      1.21       ad }
    712      1.21       ad 
    713  1.21.4.2       he /*
    714  1.21.4.2       he  * Given a signal name, attempt to find the corresponding signal number.
    715  1.21.4.2       he  */
    716      1.21       ad int
    717  1.21.4.2       he getsig(const char *sig)
    718      1.21       ad {
    719  1.21.4.2       he 	char *p;
    720      1.21       ad 	int n;
    721      1.21       ad 
    722      1.21       ad 	if (isnumber(sig)) {
    723  1.21.4.2       he 		n = (int)strtol(sig, &p, 0);
    724  1.21.4.2       he 		if (p != '\0' || n < 0 || n >= NSIG)
    725      1.21       ad 			return (-1);
    726      1.21       ad 		return (n);
    727      1.21       ad 	}
    728      1.21       ad 
    729  1.21.4.2       he 	if (strncasecmp(sig, "SIG", 3) == 0)
    730      1.21       ad 		sig += 3;
    731  1.21.4.2       he 	for (n = 1; n < NSIG; n++)
    732  1.21.4.2       he 		if (strcasecmp(sys_signame[n], sig) == 0)
    733      1.21       ad 			return (n);
    734      1.21       ad 	return (-1);
    735  1.21.4.2       he }
    736  1.21.4.2       he 
    737  1.21.4.2       he /*
    738  1.21.4.2       he  * Given a path to a PID file, return the PID contained within.
    739  1.21.4.2       he  */
    740  1.21.4.2       he pid_t
    741  1.21.4.2       he readpidfile(const char *file)
    742  1.21.4.2       he {
    743  1.21.4.2       he 	FILE *fd;
    744  1.21.4.2       he 	char line[BUFSIZ];
    745  1.21.4.2       he 	pid_t pid;
    746  1.21.4.2       he 
    747  1.21.4.2       he #ifdef notyet
    748  1.21.4.2       he 	if (file[0] != '/')
    749  1.21.4.2       he 		snprintf(tmp, sizeof (tmp), "%s%s", _PATH_VARRUN, file);
    750  1.21.4.2       he 	else
    751  1.21.4.2       he 		strlcpy(tmp, file, sizeof (tmp));
    752  1.21.4.2       he #endif
    753  1.21.4.2       he 
    754  1.21.4.2       he 	if ((fd = fopen(file, "rt")) == NULL) {
    755  1.21.4.2       he 		warn("%s", file);
    756  1.21.4.2       he 		return (-1);
    757  1.21.4.2       he 	}
    758  1.21.4.2       he 
    759  1.21.4.2       he 	if (fgets(line, sizeof (line) - 1, fd) != NULL) {
    760  1.21.4.2       he 		line[sizeof (line) - 1] = '\0';
    761  1.21.4.2       he 		pid = (pid_t)strtol(line, NULL, 0);
    762  1.21.4.2       he 	} else {
    763  1.21.4.2       he 		warnx("unable to read %s", file);
    764  1.21.4.2       he 		pid = (pid_t)-1;
    765  1.21.4.2       he 	}
    766  1.21.4.2       he 
    767  1.21.4.2       he 	fclose(fd);
    768  1.21.4.2       he 	return (pid);
    769  1.21.4.2       he }
    770  1.21.4.2       he 
    771  1.21.4.2       he /*
    772  1.21.4.2       he  * Parse a user:group specification.
    773  1.21.4.2       he  *
    774  1.21.4.2       he  * XXX This is over the top for newsyslog(8).  It should be moved to libutil.
    775  1.21.4.2       he  */
    776  1.21.4.2       he int
    777  1.21.4.2       he parse_userspec(const char *name, struct passwd **pw, struct group **gr)
    778  1.21.4.2       he {
    779  1.21.4.2       he 	char buf[MAXLOGNAME * 2 + 2], *group;
    780  1.21.4.2       he 
    781  1.21.4.2       he 	strlcpy(buf, name, sizeof (buf));
    782  1.21.4.2       he 	*gr = NULL;
    783  1.21.4.2       he 
    784  1.21.4.2       he 	/*
    785  1.21.4.2       he 	 * Before attempting to use '.' as a separator, see if the whole
    786  1.21.4.2       he 	 * string resolves as a user name.
    787  1.21.4.2       he 	 */
    788  1.21.4.2       he 	if ((*pw = getpwnam(buf)) != NULL) {
    789  1.21.4.2       he 		*gr = getgrgid((*pw)->pw_gid);
    790  1.21.4.2       he 		return (0);
    791  1.21.4.2       he 	}
    792  1.21.4.2       he 
    793  1.21.4.2       he 	/* Split the user and group name. */
    794  1.21.4.2       he 	if ((group = strchr(buf, ':')) != NULL ||
    795  1.21.4.2       he 	    (group = strchr(buf, '.')) != NULL)
    796  1.21.4.2       he 		*group++ = '\0';
    797  1.21.4.2       he 
    798  1.21.4.2       he 	if (isnumber(buf))
    799  1.21.4.2       he 		*pw = getpwuid((uid_t)atoi(buf));
    800  1.21.4.2       he 	else
    801  1.21.4.2       he 		*pw = getpwnam(buf);
    802  1.21.4.2       he 
    803  1.21.4.2       he 	/*
    804  1.21.4.2       he 	 * Find the group.  If a group wasn't specified, use the user's
    805  1.21.4.2       he 	 * `natural' group.  We get to this point even if no user was found.
    806  1.21.4.2       he 	 * This is to allow the caller to get a better idea of what went
    807  1.21.4.2       he 	 * wrong, if anything.
    808  1.21.4.2       he 	 */
    809  1.21.4.2       he 	if (group == NULL || *group == '\0') {
    810  1.21.4.2       he 		if (*pw == NULL)
    811  1.21.4.2       he 			return (-1);
    812  1.21.4.2       he 		*gr = getgrgid((*pw)->pw_gid);
    813  1.21.4.2       he 	} else if (isnumber(group))
    814  1.21.4.2       he 		*gr = getgrgid((gid_t)atoi(group));
    815  1.21.4.2       he 	else
    816  1.21.4.2       he 		*gr = getgrnam(group);
    817  1.21.4.2       he 
    818  1.21.4.2       he 	return (*pw != NULL && *gr != NULL ? 0 : -1);
    819  1.21.4.2       he }
    820  1.21.4.2       he 
    821  1.21.4.2       he /*
    822  1.21.4.2       he  * Parse a cyclic time specification, the format is as follows:
    823  1.21.4.2       he  *
    824  1.21.4.2       he  *	[Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
    825  1.21.4.2       he  *
    826  1.21.4.2       he  * to rotate a log file cyclic at
    827  1.21.4.2       he  *
    828  1.21.4.2       he  *	- every day (D) within a specific hour (hh)	(hh = 0...23)
    829  1.21.4.2       he  *	- once a week (W) at a specific day (d)     OR	(d = 0..6, 0 = Sunday)
    830  1.21.4.2       he  *	- once a month (M) at a specific day (d)	(d = 1..31,l|L)
    831  1.21.4.2       he  *
    832  1.21.4.2       he  * We don't accept a timezone specification; missing fields are defaulted to
    833  1.21.4.2       he  * the current date but time zero.
    834  1.21.4.2       he  */
    835  1.21.4.2       he time_t
    836  1.21.4.2       he parse_dwm(char *s)
    837  1.21.4.2       he {
    838  1.21.4.2       he 	char *t;
    839  1.21.4.2       he 	struct tm tm, *tmp;
    840  1.21.4.2       he 	u_long ul;
    841  1.21.4.2       he 	time_t now;
    842  1.21.4.2       he 	static int mtab[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    843  1.21.4.2       he 	int wmseen, dseen, nd, save;
    844  1.21.4.2       he 
    845  1.21.4.2       he 	wmseen = 0;
    846  1.21.4.2       he 	dseen = 0;
    847  1.21.4.2       he 
    848  1.21.4.2       he 	now = time(NULL);
    849  1.21.4.2       he 	tmp = localtime(&now);
    850  1.21.4.2       he 	tm = *tmp;
    851  1.21.4.2       he 
    852  1.21.4.2       he 	/* Set no. of days per month */
    853  1.21.4.2       he 	nd = mtab[tm.tm_mon];
    854  1.21.4.2       he 
    855  1.21.4.2       he 	if (tm.tm_mon == 1 &&
    856  1.21.4.2       he 	    ((tm.tm_year + 1900) % 4 == 0) &&
    857  1.21.4.2       he 	    ((tm.tm_year + 1900) % 100 != 0) &&
    858  1.21.4.2       he 	    ((tm.tm_year + 1900) % 400 == 0))
    859  1.21.4.2       he 		nd++;	/* leap year, 29 days in february */
    860  1.21.4.2       he 	tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
    861  1.21.4.2       he 
    862  1.21.4.2       he 	for (;;) {
    863  1.21.4.2       he 		switch (*s) {
    864  1.21.4.2       he 		case 'D':
    865  1.21.4.2       he 			if (dseen)
    866  1.21.4.2       he 				return ((time_t)-1);
    867  1.21.4.2       he 			dseen++;
    868  1.21.4.2       he 			s++;
    869  1.21.4.2       he 			ul = strtoul(s, &t, 10);
    870  1.21.4.2       he 			if (ul < 0 || ul > 23)
    871  1.21.4.2       he 				return ((time_t)-1);
    872  1.21.4.2       he 			tm.tm_hour = ul;
    873  1.21.4.2       he 			break;
    874  1.21.4.2       he 
    875  1.21.4.2       he 		case 'W':
    876  1.21.4.2       he 			if (wmseen)
    877  1.21.4.2       he 				return (-1);
    878  1.21.4.2       he 			wmseen++;
    879  1.21.4.2       he 			s++;
    880  1.21.4.2       he 			ul = strtoul(s, &t, 10);
    881  1.21.4.2       he 			if (ul < 0 || ul > 6)
    882  1.21.4.2       he 				return (-1);
    883  1.21.4.2       he 			if (ul != tm.tm_wday) {
    884  1.21.4.2       he 				if (ul < tm.tm_wday) {
    885  1.21.4.2       he 					save = 6 - tm.tm_wday;
    886  1.21.4.2       he 					save += (ul + 1);
    887  1.21.4.2       he 				} else
    888  1.21.4.2       he 					save = ul - tm.tm_wday;
    889  1.21.4.2       he 				tm.tm_mday += save;
    890  1.21.4.2       he 
    891  1.21.4.2       he 				if (tm.tm_mday > nd) {
    892  1.21.4.2       he 					tm.tm_mon++;
    893  1.21.4.2       he 					tm.tm_mday = tm.tm_mday - nd;
    894  1.21.4.2       he 				}
    895  1.21.4.2       he 			}
    896  1.21.4.2       he 			break;
    897  1.21.4.2       he 
    898  1.21.4.2       he 		case 'M':
    899  1.21.4.2       he 			if (wmseen)
    900  1.21.4.2       he 				return (-1);
    901  1.21.4.2       he 			wmseen++;
    902  1.21.4.2       he 			s++;
    903  1.21.4.2       he 			if (tolower(*s) == 'l') {
    904  1.21.4.2       he 				tm.tm_mday = nd;
    905  1.21.4.2       he 				s++;
    906  1.21.4.2       he 				t = s;
    907  1.21.4.2       he 			} else {
    908  1.21.4.2       he 				ul = strtoul(s, &t, 10);
    909  1.21.4.2       he 				if (ul < 1 || ul > 31)
    910  1.21.4.2       he 					return (-1);
    911  1.21.4.2       he 
    912  1.21.4.2       he 				if (ul > nd)
    913  1.21.4.2       he 					return (-1);
    914  1.21.4.2       he 				tm.tm_mday = ul;
    915  1.21.4.2       he 			}
    916  1.21.4.2       he 			break;
    917  1.21.4.2       he 
    918  1.21.4.2       he 		default:
    919  1.21.4.2       he 			return (-1);
    920  1.21.4.2       he 			break;
    921  1.21.4.2       he 		}
    922  1.21.4.2       he 
    923  1.21.4.2       he 		if (*t == '\0' || isspace(*t))
    924  1.21.4.2       he 			break;
    925  1.21.4.2       he 		else
    926  1.21.4.2       he 			s = t;
    927  1.21.4.2       he 	}
    928  1.21.4.2       he 
    929  1.21.4.2       he 	return (mktime(&tm));
    930  1.21.4.2       he }
    931  1.21.4.2       he 
    932  1.21.4.2       he /*
    933  1.21.4.2       he  * Parse a limited subset of ISO 8601.  The specific format is as follows:
    934  1.21.4.2       he  *
    935  1.21.4.2       he  * [CC[YY[MM[DD]]]][THH[MM[SS]]]	(where `T' is the literal letter)
    936  1.21.4.2       he  *
    937  1.21.4.2       he  * We don't accept a timezone specification; missing fields (including
    938  1.21.4.2       he  * timezone) are defaulted to the current date but time zero.
    939  1.21.4.2       he  */
    940  1.21.4.2       he time_t
    941  1.21.4.2       he parse_iso8601(char *s)
    942  1.21.4.2       he {
    943  1.21.4.2       he 	char *t;
    944  1.21.4.2       he 	struct tm tm, *tmp;
    945  1.21.4.2       he 	u_long ul;
    946  1.21.4.2       he 	time_t now;
    947  1.21.4.2       he 
    948  1.21.4.2       he 	now = time(NULL);
    949  1.21.4.2       he 	tmp = localtime(&now);
    950  1.21.4.2       he 	tm = *tmp;
    951  1.21.4.2       he 
    952  1.21.4.2       he 	tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
    953  1.21.4.2       he 
    954  1.21.4.2       he 	ul = strtoul(s, &t, 10);
    955  1.21.4.2       he 	if (*t != '\0' && *t != 'T')
    956  1.21.4.2       he 		return ((time_t)-1);
    957  1.21.4.2       he 
    958  1.21.4.2       he 	/*
    959  1.21.4.2       he 	 * Now t points either to the end of the string (if no time was
    960  1.21.4.2       he 	 * provided) or to the letter `T' which separates date and time in
    961  1.21.4.2       he 	 * ISO 8601.  The pointer arithmetic is the same for either case.
    962  1.21.4.2       he 	 */
    963  1.21.4.2       he 	switch (t - s) {
    964  1.21.4.2       he 	case 8:
    965  1.21.4.2       he 		tm.tm_year = ((ul / 1000000) - 19) * 100;
    966  1.21.4.2       he 		ul = ul % 1000000;
    967  1.21.4.2       he 		/* FALLTHROUGH */
    968  1.21.4.2       he 	case 6:
    969  1.21.4.2       he 		tm.tm_year = tm.tm_year - (tm.tm_year % 100);
    970  1.21.4.2       he 		tm.tm_year += ul / 10000;
    971  1.21.4.2       he 		ul = ul % 10000;
    972  1.21.4.2       he 		/* FALLTHROUGH */
    973  1.21.4.2       he 	case 4:
    974  1.21.4.2       he 		tm.tm_mon = (ul / 100) - 1;
    975  1.21.4.2       he 		ul = ul % 100;
    976  1.21.4.2       he 		/* FALLTHROUGH */
    977  1.21.4.2       he 	case 2:
    978  1.21.4.2       he 		tm.tm_mday = ul;
    979  1.21.4.2       he 		/* FALLTHROUGH */
    980  1.21.4.2       he 	case 0:
    981  1.21.4.2       he 		break;
    982  1.21.4.2       he 	default:
    983  1.21.4.2       he 		return ((time_t)-1);
    984  1.21.4.2       he 	}
    985  1.21.4.2       he 
    986  1.21.4.2       he 	/* Sanity check */
    987  1.21.4.2       he 	if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12 ||
    988  1.21.4.2       he 	    tm.tm_mday < 1 || tm.tm_mday > 31)
    989  1.21.4.2       he 		return ((time_t)-1);
    990  1.21.4.2       he 
    991  1.21.4.2       he 	if (*t != '\0') {
    992  1.21.4.2       he 		s = ++t;
    993  1.21.4.2       he 		ul = strtoul(s, &t, 10);
    994  1.21.4.2       he 		if (*t != '\0' && !isspace(*t))
    995  1.21.4.2       he 			return ((time_t)-1);
    996  1.21.4.2       he 
    997  1.21.4.2       he 		switch (t - s) {
    998  1.21.4.2       he 		case 6:
    999  1.21.4.2       he 			tm.tm_sec = ul % 100;
   1000  1.21.4.2       he 			ul /= 100;
   1001  1.21.4.2       he 			/* FALLTHROUGH */
   1002  1.21.4.2       he 		case 4:
   1003  1.21.4.2       he 			tm.tm_min = ul % 100;
   1004  1.21.4.2       he 			ul /= 100;
   1005  1.21.4.2       he 			/* FALLTHROUGH */
   1006  1.21.4.2       he 		case 2:
   1007  1.21.4.2       he 			tm.tm_hour = ul;
   1008  1.21.4.2       he 			/* FALLTHROUGH */
   1009  1.21.4.2       he 		case 0:
   1010  1.21.4.2       he 			break;
   1011  1.21.4.2       he 		default:
   1012  1.21.4.2       he 			return ((time_t)-1);
   1013  1.21.4.2       he 		}
   1014  1.21.4.2       he 
   1015  1.21.4.2       he 		/* Sanity check */
   1016  1.21.4.2       he 		if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0 ||
   1017  1.21.4.2       he 		    tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
   1018  1.21.4.2       he 			return ((time_t)-1);
   1019  1.21.4.2       he 	}
   1020  1.21.4.2       he 
   1021  1.21.4.2       he 	return (mktime(&tm));
   1022       1.1      cgd }
   1023