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