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