newsyslog.c revision 321263
1230972Srmh/*-
2230972Srmh * ------+---------+---------+-------- + --------+---------+---------+---------*
3 * This file includes significant modifications done by:
4 * Copyright (c) 2003, 2004  - Garance Alistair Drosehn <gad@FreeBSD.org>.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *   1. Redistributions of source code must retain the above copyright
11 *      notice, this list of conditions and the following disclaimer.
12 *   2. Redistributions in binary form must reproduce the above copyright
13 *      notice, this list of conditions and the following disclaimer in the
14 *      documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * ------+---------+---------+-------- + --------+---------+---------+---------*
29 */
30
31/*
32 * This file contains changes from the Open Software Foundation.
33 */
34
35/*
36 * Copyright 1988, 1989 by the Massachusetts Institute of Technology
37 *
38 * Permission to use, copy, modify, and distribute this software and its
39 * documentation for any purpose and without fee is hereby granted, provided
40 * that the above copyright notice appear in all copies and that both that
41 * copyright notice and this permission notice appear in supporting
42 * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
43 * used in advertising or publicity pertaining to distribution of the
44 * software without specific, written prior permission. M.I.T. and the M.I.T.
45 * S.I.P.B. make no representations about the suitability of this software
46 * for any purpose.  It is provided "as is" without express or implied
47 * warranty.
48 *
49 */
50
51/*
52 * newsyslog - roll over selected logs at the appropriate time, keeping the a
53 * specified number of backup files around.
54 */
55
56#include <sys/cdefs.h>
57__FBSDID("$FreeBSD: stable/10/usr.sbin/newsyslog/newsyslog.c 321263 2017-07-20 00:44:01Z ngie $");
58
59#define	OSF
60
61#include <sys/param.h>
62#include <sys/queue.h>
63#include <sys/stat.h>
64#include <sys/wait.h>
65
66#include <assert.h>
67#include <ctype.h>
68#include <err.h>
69#include <errno.h>
70#include <dirent.h>
71#include <fcntl.h>
72#include <fnmatch.h>
73#include <glob.h>
74#include <grp.h>
75#include <paths.h>
76#include <pwd.h>
77#include <signal.h>
78#include <stdio.h>
79#include <libgen.h>
80#include <stdlib.h>
81#include <string.h>
82#include <syslog.h>
83#include <time.h>
84#include <unistd.h>
85
86#include "pathnames.h"
87#include "extern.h"
88
89/*
90 * Compression suffixes
91 */
92#ifndef	COMPRESS_SUFFIX_GZ
93#define	COMPRESS_SUFFIX_GZ	".gz"
94#endif
95
96#ifndef	COMPRESS_SUFFIX_BZ2
97#define	COMPRESS_SUFFIX_BZ2	".bz2"
98#endif
99
100#ifndef	COMPRESS_SUFFIX_XZ
101#define	COMPRESS_SUFFIX_XZ	".xz"
102#endif
103
104#define	COMPRESS_SUFFIX_MAXLEN	MAX(MAX(sizeof(COMPRESS_SUFFIX_GZ),sizeof(COMPRESS_SUFFIX_BZ2)),sizeof(COMPRESS_SUFFIX_XZ))
105
106/*
107 * Compression types
108 */
109#define	COMPRESS_TYPES  4	/* Number of supported compression types */
110
111#define	COMPRESS_NONE	0
112#define	COMPRESS_GZIP	1
113#define	COMPRESS_BZIP2	2
114#define	COMPRESS_XZ	3
115
116/*
117 * Bit-values for the 'flags' parsed from a config-file entry.
118 */
119#define	CE_BINARY	0x0008	/* Logfile is in binary, do not add status */
120				/*    messages to logfile(s) when rotating. */
121#define	CE_NOSIGNAL	0x0010	/* There is no process to signal when */
122				/*    trimming this file. */
123#define	CE_TRIMAT	0x0020	/* trim file at a specific time. */
124#define	CE_GLOB		0x0040	/* name of the log is file name pattern. */
125#define	CE_SIGNALGROUP	0x0080	/* Signal a process-group instead of a single */
126				/*    process when trimming this file. */
127#define	CE_CREATE	0x0100	/* Create the log file if it does not exist. */
128#define	CE_NODUMP	0x0200	/* Set 'nodump' on newly created log file. */
129#define	CE_PID2CMD	0x0400	/* Replace PID file with a shell command.*/
130
131#define	CE_RFC5424	0x0800	/* Use RFC5424 format rotation message */
132
133#define	MIN_PID         5	/* Don't touch pids lower than this */
134#define	MAX_PID		99999	/* was lower, see /usr/include/sys/proc.h */
135
136#define	kbytes(size)  (((size) + 1023) >> 10)
137
138#define	DEFAULT_MARKER	"<default>"
139#define	DEBUG_MARKER	"<debug>"
140#define	INCLUDE_MARKER	"<include>"
141#define	DEFAULT_TIMEFNAME_FMT	"%Y%m%dT%H%M%S"
142
143#define	MAX_OLDLOGS 65536	/* Default maximum number of old logfiles */
144
145struct compress_types {
146	const char *flag;	/* Flag in configuration file */
147	const char *suffix;	/* Compression suffix */
148	const char *path;	/* Path to compression program */
149};
150
151static const struct compress_types compress_type[COMPRESS_TYPES] = {
152	{ "", "", "" },					/* no compression */
153	{ "Z", COMPRESS_SUFFIX_GZ, _PATH_GZIP },	/* gzip compression */
154	{ "J", COMPRESS_SUFFIX_BZ2, _PATH_BZIP2 },	/* bzip2 compression */
155	{ "X", COMPRESS_SUFFIX_XZ, _PATH_XZ }		/* xz compression */
156};
157
158struct conf_entry {
159	STAILQ_ENTRY(conf_entry) cf_nextp;
160	char *log;		/* Name of the log */
161	char *pid_cmd_file;		/* PID or command file */
162	char *r_reason;		/* The reason this file is being rotated */
163	int firstcreate;	/* Creating log for the first time (-C). */
164	int rotate;		/* Non-zero if this file should be rotated */
165	int fsize;		/* size found for the log file */
166	uid_t uid;		/* Owner of log */
167	gid_t gid;		/* Group of log */
168	int numlogs;		/* Number of logs to keep */
169	int trsize;		/* Size cutoff to trigger trimming the log */
170	int hours;		/* Hours between log trimming */
171	struct ptime_data *trim_at;	/* Specific time to do trimming */
172	unsigned int permissions;	/* File permissions on the log */
173	int flags;		/* CE_BINARY */
174	int compress;		/* Compression */
175	int sig;		/* Signal to send */
176	int def_cfg;		/* Using the <default> rule for this file */
177};
178
179struct sigwork_entry {
180	SLIST_ENTRY(sigwork_entry) sw_nextp;
181	int	 sw_signum;		/* the signal to send */
182	int	 sw_pidok;		/* true if pid value is valid */
183	pid_t	 sw_pid;		/* the process id from the PID file */
184	const char *sw_pidtype;		/* "daemon" or "process group" */
185	int	 sw_runcmd;		/* run command or send PID to signal */
186	char	 sw_fname[1];		/* file the PID was read from or shell cmd */
187};
188
189struct zipwork_entry {
190	SLIST_ENTRY(zipwork_entry) zw_nextp;
191	const struct conf_entry *zw_conf;	/* for chown/perm/flag info */
192	const struct sigwork_entry *zw_swork;	/* to know success of signal */
193	int	 zw_fsize;		/* size of the file to compress */
194	char	 zw_fname[1];		/* the file to compress */
195};
196
197struct include_entry {
198	STAILQ_ENTRY(include_entry) inc_nextp;
199	const char *file;	/* Name of file to process */
200};
201
202struct oldlog_entry {
203	char *fname;		/* Filename of the log file */
204	time_t t;		/* Parsed timestamp of the logfile */
205};
206
207typedef enum {
208	FREE_ENT, KEEP_ENT
209}	fk_entry;
210
211STAILQ_HEAD(cflist, conf_entry);
212static SLIST_HEAD(swlisthead, sigwork_entry) swhead =
213    SLIST_HEAD_INITIALIZER(swhead);
214static SLIST_HEAD(zwlisthead, zipwork_entry) zwhead =
215    SLIST_HEAD_INITIALIZER(zwhead);
216STAILQ_HEAD(ilist, include_entry);
217
218int dbg_at_times;		/* -D Show details of 'trim_at' code */
219
220static int archtodir = 0;	/* Archive old logfiles to other directory */
221static int createlogs;		/* Create (non-GLOB) logfiles which do not */
222				/*    already exist.  1=='for entries with */
223				/*    C flag', 2=='for all entries'. */
224int verbose = 0;		/* Print out what's going on */
225static int needroot = 1;	/* Root privs are necessary */
226int noaction = 0;		/* Don't do anything, just show it */
227static int norotate = 0;	/* Don't rotate */
228static int nosignal;		/* Do not send any signals */
229static int enforcepid = 0;	/* If PID file does not exist or empty, do nothing */
230static int force = 0;		/* Force the trim no matter what */
231static int rotatereq = 0;	/* -R = Always rotate the file(s) as given */
232				/*    on the command (this also requires   */
233				/*    that a list of files *are* given on  */
234				/*    the run command). */
235static char *requestor;		/* The name given on a -R request */
236static char *timefnamefmt = NULL;/* Use time based filenames instead of .0 */
237static char *archdirname;	/* Directory path to old logfiles archive */
238static char *destdir = NULL;	/* Directory to treat at root for logs */
239static const char *conf;	/* Configuration file to use */
240
241struct ptime_data *dbg_timenow;	/* A "timenow" value set via -D option */
242static struct ptime_data *timenow; /* The time to use for checking at-fields */
243
244#define	DAYTIME_LEN	16
245static char daytime[DAYTIME_LEN];/* The current time in human readable form,
246				  * used for rotation-tracking messages. */
247
248/* Another buffer to hold the current time in RFC5424 format. Fractional
249 * seconds are allowed by the RFC, but are not included in the
250 * rotation-tracking messages written by newsyslog and so are not accounted for
251 * in the length below.
252 */
253#define	DAYTIME_RFC5424_LEN	sizeof("YYYY-MM-DDTHH:MM:SS+00:00")
254static char daytime_rfc5424[DAYTIME_RFC5424_LEN];
255
256static char hostname[MAXHOSTNAMELEN]; /* hostname */
257
258static const char *path_syslogpid = _PATH_SYSLOGPID;
259
260static struct cflist *get_worklist(char **files);
261static void parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
262		    struct conf_entry *defconf_p, struct ilist *inclist);
263static void add_to_queue(const char *fname, struct ilist *inclist);
264static char *sob(char *p);
265static char *son(char *p);
266static int isnumberstr(const char *);
267static int isglobstr(const char *);
268static char *missing_field(char *p, char *errline);
269static void	 change_attrs(const char *, const struct conf_entry *);
270static const char *get_logfile_suffix(const char *logfile);
271static fk_entry	 do_entry(struct conf_entry *);
272static fk_entry	 do_rotate(const struct conf_entry *);
273static void	 do_sigwork(struct sigwork_entry *);
274static void	 do_zipwork(struct zipwork_entry *);
275static struct sigwork_entry *
276		 save_sigwork(const struct conf_entry *);
277static struct zipwork_entry *
278		 save_zipwork(const struct conf_entry *, const struct
279		    sigwork_entry *, int, const char *);
280static void	 set_swpid(struct sigwork_entry *, const struct conf_entry *);
281static int	 sizefile(const char *);
282static void expand_globs(struct cflist *work_p, struct cflist *glob_p);
283static void free_clist(struct cflist *list);
284static void free_entry(struct conf_entry *ent);
285static struct conf_entry *init_entry(const char *fname,
286		struct conf_entry *src_entry);
287static void parse_args(int argc, char **argv);
288static int parse_doption(const char *doption);
289static void usage(void);
290static int log_trim(const char *logname, const struct conf_entry *log_ent);
291static int age_old_log(const char *file);
292static void savelog(char *from, char *to);
293static void createdir(const struct conf_entry *ent, char *dirpart);
294static void createlog(const struct conf_entry *ent);
295static int parse_signal(const char *str);
296
297/*
298 * All the following take a parameter of 'int', but expect values in the
299 * range of unsigned char.  Define wrappers which take values of type 'char',
300 * whether signed or unsigned, and ensure they end up in the right range.
301 */
302#define	isdigitch(Anychar) isdigit((u_char)(Anychar))
303#define	isprintch(Anychar) isprint((u_char)(Anychar))
304#define	isspacech(Anychar) isspace((u_char)(Anychar))
305#define	tolowerch(Anychar) tolower((u_char)(Anychar))
306
307int
308main(int argc, char **argv)
309{
310	struct cflist *worklist;
311	struct conf_entry *p;
312	struct sigwork_entry *stmp;
313	struct zipwork_entry *ztmp;
314
315	SLIST_INIT(&swhead);
316	SLIST_INIT(&zwhead);
317
318	parse_args(argc, argv);
319	argc -= optind;
320	argv += optind;
321
322	if (needroot && getuid() && geteuid())
323		errx(1, "must have root privs");
324	worklist = get_worklist(argv);
325
326	/*
327	 * Rotate all the files which need to be rotated.  Note that
328	 * some users have *hundreds* of entries in newsyslog.conf!
329	 */
330	while (!STAILQ_EMPTY(worklist)) {
331		p = STAILQ_FIRST(worklist);
332		STAILQ_REMOVE_HEAD(worklist, cf_nextp);
333		if (do_entry(p) == FREE_ENT)
334			free_entry(p);
335	}
336
337	/*
338	 * Send signals to any processes which need a signal to tell
339	 * them to close and re-open the log file(s) we have rotated.
340	 * Note that zipwork_entries include pointers to these
341	 * sigwork_entry's, so we can not free the entries here.
342	 */
343	if (!SLIST_EMPTY(&swhead)) {
344		if (noaction || verbose)
345			printf("Signal all daemon process(es)...\n");
346		SLIST_FOREACH(stmp, &swhead, sw_nextp)
347			do_sigwork(stmp);
348		if (!(rotatereq && nosignal)) {
349			if (noaction)
350				printf("\tsleep 10\n");
351			else {
352				if (verbose)
353					printf("Pause 10 seconds to allow "
354					    "daemon(s) to close log file(s)\n");
355				sleep(10);
356			}
357		}
358	}
359	/*
360	 * Compress all files that we're expected to compress, now
361	 * that all processes should have closed the files which
362	 * have been rotated.
363	 */
364	if (!SLIST_EMPTY(&zwhead)) {
365		if (noaction || verbose)
366			printf("Compress all rotated log file(s)...\n");
367		while (!SLIST_EMPTY(&zwhead)) {
368			ztmp = SLIST_FIRST(&zwhead);
369			do_zipwork(ztmp);
370			SLIST_REMOVE_HEAD(&zwhead, zw_nextp);
371			free(ztmp);
372		}
373	}
374	/* Now free all the sigwork entries. */
375	while (!SLIST_EMPTY(&swhead)) {
376		stmp = SLIST_FIRST(&swhead);
377		SLIST_REMOVE_HEAD(&swhead, sw_nextp);
378		free(stmp);
379	}
380
381	while (wait(NULL) > 0 || errno == EINTR)
382		;
383	return (0);
384}
385
386static struct conf_entry *
387init_entry(const char *fname, struct conf_entry *src_entry)
388{
389	struct conf_entry *tempwork;
390
391	if (verbose > 4)
392		printf("\t--> [creating entry for %s]\n", fname);
393
394	tempwork = malloc(sizeof(struct conf_entry));
395	if (tempwork == NULL)
396		err(1, "malloc of conf_entry for %s", fname);
397
398	if (destdir == NULL || fname[0] != '/')
399		tempwork->log = strdup(fname);
400	else
401		asprintf(&tempwork->log, "%s%s", destdir, fname);
402	if (tempwork->log == NULL)
403		err(1, "strdup for %s", fname);
404
405	if (src_entry != NULL) {
406		tempwork->pid_cmd_file = NULL;
407		if (src_entry->pid_cmd_file)
408			tempwork->pid_cmd_file = strdup(src_entry->pid_cmd_file);
409		tempwork->r_reason = NULL;
410		tempwork->firstcreate = 0;
411		tempwork->rotate = 0;
412		tempwork->fsize = -1;
413		tempwork->uid = src_entry->uid;
414		tempwork->gid = src_entry->gid;
415		tempwork->numlogs = src_entry->numlogs;
416		tempwork->trsize = src_entry->trsize;
417		tempwork->hours = src_entry->hours;
418		tempwork->trim_at = NULL;
419		if (src_entry->trim_at != NULL)
420			tempwork->trim_at = ptime_init(src_entry->trim_at);
421		tempwork->permissions = src_entry->permissions;
422		tempwork->flags = src_entry->flags;
423		tempwork->compress = src_entry->compress;
424		tempwork->sig = src_entry->sig;
425		tempwork->def_cfg = src_entry->def_cfg;
426	} else {
427		/* Initialize as a "do-nothing" entry */
428		tempwork->pid_cmd_file = NULL;
429		tempwork->r_reason = NULL;
430		tempwork->firstcreate = 0;
431		tempwork->rotate = 0;
432		tempwork->fsize = -1;
433		tempwork->uid = (uid_t)-1;
434		tempwork->gid = (gid_t)-1;
435		tempwork->numlogs = 1;
436		tempwork->trsize = -1;
437		tempwork->hours = -1;
438		tempwork->trim_at = NULL;
439		tempwork->permissions = 0;
440		tempwork->flags = 0;
441		tempwork->compress = COMPRESS_NONE;
442		tempwork->sig = SIGHUP;
443		tempwork->def_cfg = 0;
444	}
445
446	return (tempwork);
447}
448
449static void
450free_entry(struct conf_entry *ent)
451{
452
453	if (ent == NULL)
454		return;
455
456	if (ent->log != NULL) {
457		if (verbose > 4)
458			printf("\t--> [freeing entry for %s]\n", ent->log);
459		free(ent->log);
460		ent->log = NULL;
461	}
462
463	if (ent->pid_cmd_file != NULL) {
464		free(ent->pid_cmd_file);
465		ent->pid_cmd_file = NULL;
466	}
467
468	if (ent->r_reason != NULL) {
469		free(ent->r_reason);
470		ent->r_reason = NULL;
471	}
472
473	if (ent->trim_at != NULL) {
474		ptime_free(ent->trim_at);
475		ent->trim_at = NULL;
476	}
477
478	free(ent);
479}
480
481static void
482free_clist(struct cflist *list)
483{
484	struct conf_entry *ent;
485
486	while (!STAILQ_EMPTY(list)) {
487		ent = STAILQ_FIRST(list);
488		STAILQ_REMOVE_HEAD(list, cf_nextp);
489		free_entry(ent);
490	}
491
492	free(list);
493	list = NULL;
494}
495
496static fk_entry
497do_entry(struct conf_entry * ent)
498{
499#define	REASON_MAX	80
500	int modtime;
501	fk_entry free_or_keep;
502	double diffsecs;
503	char temp_reason[REASON_MAX];
504	int oversized;
505
506	free_or_keep = FREE_ENT;
507	if (verbose)
508		printf("%s <%d%s>: ", ent->log, ent->numlogs,
509		    compress_type[ent->compress].flag);
510	ent->fsize = sizefile(ent->log);
511	oversized = ((ent->trsize > 0) && (ent->fsize >= ent->trsize));
512	modtime = age_old_log(ent->log);
513	ent->rotate = 0;
514	ent->firstcreate = 0;
515	if (ent->fsize < 0) {
516		/*
517		 * If either the C flag or the -C option was specified,
518		 * and if we won't be creating the file, then have the
519		 * verbose message include a hint as to why the file
520		 * will not be created.
521		 */
522		temp_reason[0] = '\0';
523		if (createlogs > 1)
524			ent->firstcreate = 1;
525		else if ((ent->flags & CE_CREATE) && createlogs)
526			ent->firstcreate = 1;
527		else if (ent->flags & CE_CREATE)
528			strlcpy(temp_reason, " (no -C option)", REASON_MAX);
529		else if (createlogs)
530			strlcpy(temp_reason, " (no C flag)", REASON_MAX);
531
532		if (ent->firstcreate) {
533			if (verbose)
534				printf("does not exist -> will create.\n");
535			createlog(ent);
536		} else if (verbose) {
537			printf("does not exist, skipped%s.\n", temp_reason);
538		}
539	} else {
540		if (ent->flags & CE_TRIMAT && !force && !rotatereq &&
541		    !oversized) {
542			diffsecs = ptimeget_diff(timenow, ent->trim_at);
543			if (diffsecs < 0.0) {
544				/* trim_at is some time in the future. */
545				if (verbose) {
546					ptime_adjust4dst(ent->trim_at,
547					    timenow);
548					printf("--> will trim at %s",
549					    ptimeget_ctime(ent->trim_at));
550				}
551				return (free_or_keep);
552			} else if (diffsecs >= 3600.0) {
553				/*
554				 * trim_at is more than an hour in the past,
555				 * so find the next valid trim_at time, and
556				 * tell the user what that will be.
557				 */
558				if (verbose && dbg_at_times)
559					printf("\n\t--> prev trim at %s\t",
560					    ptimeget_ctime(ent->trim_at));
561				if (verbose) {
562					ptimeset_nxtime(ent->trim_at);
563					printf("--> will trim at %s",
564					    ptimeget_ctime(ent->trim_at));
565				}
566				return (free_or_keep);
567			} else if (verbose && noaction && dbg_at_times) {
568				/*
569				 * If we are just debugging at-times, then
570				 * a detailed message is helpful.  Also
571				 * skip "doing" any commands, since they
572				 * would all be turned off by no-action.
573				 */
574				printf("\n\t--> timematch at %s",
575				    ptimeget_ctime(ent->trim_at));
576				return (free_or_keep);
577			} else if (verbose && ent->hours <= 0) {
578				printf("--> time is up\n");
579			}
580		}
581		if (verbose && (ent->trsize > 0))
582			printf("size (Kb): %d [%d] ", ent->fsize, ent->trsize);
583		if (verbose && (ent->hours > 0))
584			printf(" age (hr): %d [%d] ", modtime, ent->hours);
585
586		/*
587		 * Figure out if this logfile needs to be rotated.
588		 */
589		temp_reason[0] = '\0';
590		if (rotatereq) {
591			ent->rotate = 1;
592			snprintf(temp_reason, REASON_MAX, " due to -R from %s",
593			    requestor);
594		} else if (force) {
595			ent->rotate = 1;
596			snprintf(temp_reason, REASON_MAX, " due to -F request");
597		} else if (oversized) {
598			ent->rotate = 1;
599			snprintf(temp_reason, REASON_MAX, " due to size>%dK",
600			    ent->trsize);
601		} else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) {
602			ent->rotate = 1;
603		} else if ((ent->hours > 0) && ((modtime >= ent->hours) ||
604		    (modtime < 0))) {
605			ent->rotate = 1;
606		}
607
608		/*
609		 * If the file needs to be rotated, then rotate it.
610		 */
611		if (ent->rotate && !norotate) {
612			if (temp_reason[0] != '\0')
613				ent->r_reason = strdup(temp_reason);
614			if (verbose)
615				printf("--> trimming log....\n");
616			if (noaction && !verbose)
617				printf("%s <%d%s>: trimming\n", ent->log,
618				    ent->numlogs,
619				    compress_type[ent->compress].flag);
620			free_or_keep = do_rotate(ent);
621		} else {
622			if (verbose)
623				printf("--> skipping\n");
624		}
625	}
626	return (free_or_keep);
627#undef REASON_MAX
628}
629
630static void
631parse_args(int argc, char **argv)
632{
633	int ch;
634	char *p;
635
636	timenow = ptime_init(NULL);
637	ptimeset_time(timenow, time(NULL));
638	strlcpy(daytime, ptimeget_ctime(timenow) + 4, DAYTIME_LEN);
639	ptimeget_ctime_rfc5424(timenow, daytime_rfc5424, DAYTIME_RFC5424_LEN);
640
641	/* Let's get our hostname */
642	(void)gethostname(hostname, sizeof(hostname));
643
644	/* Truncate domain */
645	if ((p = strchr(hostname, '.')) != NULL)
646		*p = '\0';
647
648	/* Parse command line options. */
649	while ((ch = getopt(argc, argv, "a:d:f:nrst:vCD:FNPR:S:")) != -1)
650		switch (ch) {
651		case 'a':
652			archtodir++;
653			archdirname = optarg;
654			break;
655		case 'd':
656			destdir = optarg;
657			break;
658		case 'f':
659			conf = optarg;
660			break;
661		case 'n':
662			noaction++;
663			/* FALLTHROUGH */
664		case 'r':
665			needroot = 0;
666			break;
667		case 's':
668			nosignal = 1;
669			break;
670		case 't':
671			if (optarg[0] == '\0' ||
672			    strcmp(optarg, "DEFAULT") == 0)
673				timefnamefmt = strdup(DEFAULT_TIMEFNAME_FMT);
674			else
675				timefnamefmt = strdup(optarg);
676			break;
677		case 'v':
678			verbose++;
679			break;
680		case 'C':
681			/* Useful for things like rc.diskless... */
682			createlogs++;
683			break;
684		case 'D':
685			/*
686			 * Set some debugging option.  The specific option
687			 * depends on the value of optarg.  These options
688			 * may come and go without notice or documentation.
689			 */
690			if (parse_doption(optarg))
691				break;
692			usage();
693			/* NOTREACHED */
694		case 'F':
695			force++;
696			break;
697		case 'N':
698			norotate++;
699			break;
700		case 'P':
701			enforcepid++;
702			break;
703		case 'R':
704			rotatereq++;
705			requestor = strdup(optarg);
706			break;
707		case 'S':
708			path_syslogpid = optarg;
709			break;
710		case 'm':	/* Used by OpenBSD for "monitor mode" */
711		default:
712			usage();
713			/* NOTREACHED */
714		}
715
716	if (force && norotate) {
717		warnx("Only one of -F and -N may be specified.");
718		usage();
719		/* NOTREACHED */
720	}
721
722	if (rotatereq) {
723		if (optind == argc) {
724			warnx("At least one filename must be given when -R is specified.");
725			usage();
726			/* NOTREACHED */
727		}
728		/* Make sure "requestor" value is safe for a syslog message. */
729		for (p = requestor; *p != '\0'; p++) {
730			if (!isprintch(*p) && (*p != '\t'))
731				*p = '.';
732		}
733	}
734
735	if (dbg_timenow) {
736		/*
737		 * Note that the 'daytime' variable is not changed.
738		 * That is only used in messages that track when a
739		 * logfile is rotated, and if a file *is* rotated,
740		 * then it will still rotated at the "real now" time.
741		 */
742		ptime_free(timenow);
743		timenow = dbg_timenow;
744		fprintf(stderr, "Debug: Running as if TimeNow is %s",
745		    ptimeget_ctime(dbg_timenow));
746	}
747
748}
749
750/*
751 * These debugging options are mainly meant for developer use, such
752 * as writing regression-tests.  They would not be needed by users
753 * during normal operation of newsyslog...
754 */
755static int
756parse_doption(const char *doption)
757{
758	const char TN[] = "TN=";
759	int res;
760
761	if (strncmp(doption, TN, sizeof(TN) - 1) == 0) {
762		/*
763		 * The "TimeNow" debugging option.  This might be off
764		 * by an hour when crossing a timezone change.
765		 */
766		dbg_timenow = ptime_init(NULL);
767		res = ptime_relparse(dbg_timenow, PTM_PARSE_ISO8601,
768		    time(NULL), doption + sizeof(TN) - 1);
769		if (res == -2) {
770			warnx("Non-existent time specified on -D %s", doption);
771			return (0);			/* failure */
772		} else if (res < 0) {
773			warnx("Malformed time given on -D %s", doption);
774			return (0);			/* failure */
775		}
776		return (1);			/* successfully parsed */
777
778	}
779
780	if (strcmp(doption, "ats") == 0) {
781		dbg_at_times++;
782		return (1);			/* successfully parsed */
783	}
784
785	/* XXX - This check could probably be dropped. */
786	if ((strcmp(doption, "neworder") == 0) || (strcmp(doption, "oldorder")
787	    == 0)) {
788		warnx("NOTE: newsyslog always uses 'neworder'.");
789		return (1);			/* successfully parsed */
790	}
791
792	warnx("Unknown -D (debug) option: '%s'", doption);
793	return (0);				/* failure */
794}
795
796static void
797usage(void)
798{
799
800	fprintf(stderr,
801	    "usage: newsyslog [-CFNPnrsv] [-a directory] [-d directory] [-f config_file]\n"
802	    "                 [-S pidfile] [-t timefmt] [[-R tagname] file ...]\n");
803	exit(1);
804}
805
806/*
807 * Parse a configuration file and return a linked list of all the logs
808 * which should be processed.
809 */
810static struct cflist *
811get_worklist(char **files)
812{
813	FILE *f;
814	char **given;
815	struct cflist *cmdlist, *filelist, *globlist;
816	struct conf_entry *defconf, *dupent, *ent;
817	struct ilist inclist;
818	struct include_entry *inc;
819	int gmatch, fnres;
820
821	defconf = NULL;
822	STAILQ_INIT(&inclist);
823
824	filelist = malloc(sizeof(struct cflist));
825	if (filelist == NULL)
826		err(1, "malloc of filelist");
827	STAILQ_INIT(filelist);
828	globlist = malloc(sizeof(struct cflist));
829	if (globlist == NULL)
830		err(1, "malloc of globlist");
831	STAILQ_INIT(globlist);
832
833	inc = malloc(sizeof(struct include_entry));
834	if (inc == NULL)
835		err(1, "malloc of inc");
836	inc->file = conf;
837	if (inc->file == NULL)
838		inc->file = _PATH_CONF;
839	STAILQ_INSERT_TAIL(&inclist, inc, inc_nextp);
840
841	STAILQ_FOREACH(inc, &inclist, inc_nextp) {
842		if (strcmp(inc->file, "-") != 0)
843			f = fopen(inc->file, "r");
844		else {
845			f = stdin;
846			inc->file = "<stdin>";
847		}
848		if (!f)
849			err(1, "%s", inc->file);
850
851		if (verbose)
852			printf("Processing %s\n", inc->file);
853		parse_file(f, filelist, globlist, defconf, &inclist);
854		(void) fclose(f);
855	}
856
857	/*
858	 * All config-file information has been read in and turned into
859	 * a filelist and a globlist.  If there were no specific files
860	 * given on the run command, then the only thing left to do is to
861	 * call a routine which finds all files matched by the globlist
862	 * and adds them to the filelist.  Then return the worklist.
863	 */
864	if (*files == NULL) {
865		expand_globs(filelist, globlist);
866		free_clist(globlist);
867		if (defconf != NULL)
868			free_entry(defconf);
869		return (filelist);
870		/* NOTREACHED */
871	}
872
873	/*
874	 * If newsyslog was given a specific list of files to process,
875	 * it may be that some of those files were not listed in any
876	 * config file.  Those unlisted files should get the default
877	 * rotation action.  First, create the default-rotation action
878	 * if none was found in a system config file.
879	 */
880	if (defconf == NULL) {
881		defconf = init_entry(DEFAULT_MARKER, NULL);
882		defconf->numlogs = 3;
883		defconf->trsize = 50;
884		defconf->permissions = S_IRUSR|S_IWUSR;
885	}
886
887	/*
888	 * If newsyslog was run with a list of specific filenames,
889	 * then create a new worklist which has only those files in
890	 * it, picking up the rotation-rules for those files from
891	 * the original filelist.
892	 *
893	 * XXX - Note that this will copy multiple rules for a single
894	 *	logfile, if multiple entries are an exact match for
895	 *	that file.  That matches the historic behavior, but do
896	 *	we want to continue to allow it?  If so, it should
897	 *	probably be handled more intelligently.
898	 */
899	cmdlist = malloc(sizeof(struct cflist));
900	if (cmdlist == NULL)
901		err(1, "malloc of cmdlist");
902	STAILQ_INIT(cmdlist);
903
904	for (given = files; *given; ++given) {
905		/*
906		 * First try to find exact-matches for this given file.
907		 */
908		gmatch = 0;
909		STAILQ_FOREACH(ent, filelist, cf_nextp) {
910			if (strcmp(ent->log, *given) == 0) {
911				gmatch++;
912				dupent = init_entry(*given, ent);
913				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
914			}
915		}
916		if (gmatch) {
917			if (verbose > 2)
918				printf("\t+ Matched entry %s\n", *given);
919			continue;
920		}
921
922		/*
923		 * There was no exact-match for this given file, so look
924		 * for a "glob" entry which does match.
925		 */
926		gmatch = 0;
927		if (verbose > 2 && globlist != NULL)
928			printf("\t+ Checking globs for %s\n", *given);
929		STAILQ_FOREACH(ent, globlist, cf_nextp) {
930			fnres = fnmatch(ent->log, *given, FNM_PATHNAME);
931			if (verbose > 2)
932				printf("\t+    = %d for pattern %s\n", fnres,
933				    ent->log);
934			if (fnres == 0) {
935				gmatch++;
936				dupent = init_entry(*given, ent);
937				/* This new entry is not a glob! */
938				dupent->flags &= ~CE_GLOB;
939				STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
940				/* Only allow a match to one glob-entry */
941				break;
942			}
943		}
944		if (gmatch) {
945			if (verbose > 2)
946				printf("\t+ Matched %s via %s\n", *given,
947				    ent->log);
948			continue;
949		}
950
951		/*
952		 * This given file was not found in any config file, so
953		 * add a worklist item based on the default entry.
954		 */
955		if (verbose > 2)
956			printf("\t+ No entry matched %s  (will use %s)\n",
957			    *given, DEFAULT_MARKER);
958		dupent = init_entry(*given, defconf);
959		/* Mark that it was *not* found in a config file */
960		dupent->def_cfg = 1;
961		STAILQ_INSERT_TAIL(cmdlist, dupent, cf_nextp);
962	}
963
964	/*
965	 * Free all the entries in the original work list, the list of
966	 * glob entries, and the default entry.
967	 */
968	free_clist(filelist);
969	free_clist(globlist);
970	free_entry(defconf);
971
972	/* And finally, return a worklist which matches the given files. */
973	return (cmdlist);
974}
975
976/*
977 * Expand the list of entries with filename patterns, and add all files
978 * which match those glob-entries onto the worklist.
979 */
980static void
981expand_globs(struct cflist *work_p, struct cflist *glob_p)
982{
983	int gmatch, gres;
984	size_t i;
985	char *mfname;
986	struct conf_entry *dupent, *ent, *globent;
987	glob_t pglob;
988	struct stat st_fm;
989
990	/*
991	 * The worklist contains all fully-specified (non-GLOB) names.
992	 *
993	 * Now expand the list of filename-pattern (GLOB) entries into
994	 * a second list, which (by definition) will only match files
995	 * that already exist.  Do not add a glob-related entry for any
996	 * file which already exists in the fully-specified list.
997	 */
998	STAILQ_FOREACH(globent, glob_p, cf_nextp) {
999		gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob);
1000		if (gres != 0) {
1001			warn("cannot expand pattern (%d): %s", gres,
1002			    globent->log);
1003			continue;
1004		}
1005
1006		if (verbose > 2)
1007			printf("\t+ Expanding pattern %s\n", globent->log);
1008		for (i = 0; i < pglob.gl_matchc; i++) {
1009			mfname = pglob.gl_pathv[i];
1010
1011			/* See if this file already has a specific entry. */
1012			gmatch = 0;
1013			STAILQ_FOREACH(ent, work_p, cf_nextp) {
1014				if (strcmp(mfname, ent->log) == 0) {
1015					gmatch++;
1016					break;
1017				}
1018			}
1019			if (gmatch)
1020				continue;
1021
1022			/* Make sure the named matched is a file. */
1023			gres = lstat(mfname, &st_fm);
1024			if (gres != 0) {
1025				/* Error on a file that glob() matched?!? */
1026				warn("Skipping %s - lstat() error", mfname);
1027				continue;
1028			}
1029			if (!S_ISREG(st_fm.st_mode)) {
1030				/* We only rotate files! */
1031				if (verbose > 2)
1032					printf("\t+  . skipping %s (!file)\n",
1033					    mfname);
1034				continue;
1035			}
1036
1037			if (verbose > 2)
1038				printf("\t+  . add file %s\n", mfname);
1039			dupent = init_entry(mfname, globent);
1040			/* This new entry is not a glob! */
1041			dupent->flags &= ~CE_GLOB;
1042
1043			/* Add to the worklist. */
1044			STAILQ_INSERT_TAIL(work_p, dupent, cf_nextp);
1045		}
1046		globfree(&pglob);
1047		if (verbose > 2)
1048			printf("\t+ Done with pattern %s\n", globent->log);
1049	}
1050}
1051
1052/*
1053 * Parse a configuration file and update a linked list of all the logs to
1054 * process.
1055 */
1056static void
1057parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
1058    struct conf_entry *defconf_p, struct ilist *inclist)
1059{
1060	char line[BUFSIZ], *parse, *q;
1061	char *cp, *errline, *group;
1062	struct conf_entry *working;
1063	struct passwd *pwd;
1064	struct group *grp;
1065	glob_t pglob;
1066	int eol, ptm_opts, res, special;
1067	size_t i;
1068
1069	errline = NULL;
1070	while (fgets(line, BUFSIZ, cf)) {
1071		if ((line[0] == '\n') || (line[0] == '#') ||
1072		    (strlen(line) == 0))
1073			continue;
1074		if (errline != NULL)
1075			free(errline);
1076		errline = strdup(line);
1077		for (cp = line + 1; *cp != '\0'; cp++) {
1078			if (*cp != '#')
1079				continue;
1080			if (*(cp - 1) == '\\') {
1081				strcpy(cp - 1, cp);
1082				cp--;
1083				continue;
1084			}
1085			*cp = '\0';
1086			break;
1087		}
1088
1089		q = parse = missing_field(sob(line), errline);
1090		parse = son(line);
1091		if (!*parse)
1092			errx(1, "malformed line (missing fields):\n%s",
1093			    errline);
1094		*parse = '\0';
1095
1096		/*
1097		 * Allow people to set debug options via the config file.
1098		 * (NOTE: debug options are undocumented, and may disappear
1099		 * at any time, etc).
1100		 */
1101		if (strcasecmp(DEBUG_MARKER, q) == 0) {
1102			q = parse = missing_field(sob(parse + 1), errline);
1103			parse = son(parse);
1104			if (!*parse)
1105				warnx("debug line specifies no option:\n%s",
1106				    errline);
1107			else {
1108				*parse = '\0';
1109				parse_doption(q);
1110			}
1111			continue;
1112		} else if (strcasecmp(INCLUDE_MARKER, q) == 0) {
1113			if (verbose)
1114				printf("Found: %s", errline);
1115			q = parse = missing_field(sob(parse + 1), errline);
1116			parse = son(parse);
1117			if (!*parse) {
1118				warnx("include line missing argument:\n%s",
1119				    errline);
1120				continue;
1121			}
1122
1123			*parse = '\0';
1124
1125			if (isglobstr(q)) {
1126				res = glob(q, GLOB_NOCHECK, NULL, &pglob);
1127				if (res != 0) {
1128					warn("cannot expand pattern (%d): %s",
1129					    res, q);
1130					continue;
1131				}
1132
1133				if (verbose > 2)
1134					printf("\t+ Expanding pattern %s\n", q);
1135
1136				for (i = 0; i < pglob.gl_matchc; i++)
1137					add_to_queue(pglob.gl_pathv[i],
1138					    inclist);
1139				globfree(&pglob);
1140			} else
1141				add_to_queue(q, inclist);
1142			continue;
1143		}
1144
1145		special = 0;
1146		working = init_entry(q, NULL);
1147		if (strcasecmp(DEFAULT_MARKER, q) == 0) {
1148			special = 1;
1149			if (defconf_p != NULL) {
1150				warnx("Ignoring duplicate entry for %s!", q);
1151				free_entry(working);
1152				continue;
1153			}
1154			defconf_p = working;
1155		}
1156
1157		q = parse = missing_field(sob(parse + 1), errline);
1158		parse = son(parse);
1159		if (!*parse)
1160			errx(1, "malformed line (missing fields):\n%s",
1161			    errline);
1162		*parse = '\0';
1163		if ((group = strchr(q, ':')) != NULL ||
1164		    (group = strrchr(q, '.')) != NULL) {
1165			*group++ = '\0';
1166			if (*q) {
1167				if (!(isnumberstr(q))) {
1168					if ((pwd = getpwnam(q)) == NULL)
1169						errx(1,
1170				     "error in config file; unknown user:\n%s",
1171						    errline);
1172					working->uid = pwd->pw_uid;
1173				} else
1174					working->uid = atoi(q);
1175			} else
1176				working->uid = (uid_t)-1;
1177
1178			q = group;
1179			if (*q) {
1180				if (!(isnumberstr(q))) {
1181					if ((grp = getgrnam(q)) == NULL)
1182						errx(1,
1183				    "error in config file; unknown group:\n%s",
1184						    errline);
1185					working->gid = grp->gr_gid;
1186				} else
1187					working->gid = atoi(q);
1188			} else
1189				working->gid = (gid_t)-1;
1190
1191			q = parse = missing_field(sob(parse + 1), errline);
1192			parse = son(parse);
1193			if (!*parse)
1194				errx(1, "malformed line (missing fields):\n%s",
1195				    errline);
1196			*parse = '\0';
1197		} else {
1198			working->uid = (uid_t)-1;
1199			working->gid = (gid_t)-1;
1200		}
1201
1202		if (!sscanf(q, "%o", &working->permissions))
1203			errx(1, "error in config file; bad permissions:\n%s",
1204			    errline);
1205
1206		q = parse = missing_field(sob(parse + 1), errline);
1207		parse = son(parse);
1208		if (!*parse)
1209			errx(1, "malformed line (missing fields):\n%s",
1210			    errline);
1211		*parse = '\0';
1212		if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1213			errx(1, "error in config file; bad value for count of logs to save:\n%s",
1214			    errline);
1215
1216		q = parse = missing_field(sob(parse + 1), errline);
1217		parse = son(parse);
1218		if (!*parse)
1219			errx(1, "malformed line (missing fields):\n%s",
1220			    errline);
1221		*parse = '\0';
1222		if (isdigitch(*q))
1223			working->trsize = atoi(q);
1224		else if (strcmp(q, "*") == 0)
1225			working->trsize = -1;
1226		else {
1227			warnx("Invalid value of '%s' for 'size' in line:\n%s",
1228			    q, errline);
1229			working->trsize = -1;
1230		}
1231
1232		working->flags = 0;
1233		working->compress = COMPRESS_NONE;
1234		q = parse = missing_field(sob(parse + 1), errline);
1235		parse = son(parse);
1236		eol = !*parse;
1237		*parse = '\0';
1238		{
1239			char *ep;
1240			u_long ul;
1241
1242			ul = strtoul(q, &ep, 10);
1243			if (ep == q)
1244				working->hours = 0;
1245			else if (*ep == '*')
1246				working->hours = -1;
1247			else if (ul > INT_MAX)
1248				errx(1, "interval is too large:\n%s", errline);
1249			else
1250				working->hours = ul;
1251
1252			if (*ep == '\0' || strcmp(ep, "*") == 0)
1253				goto no_trimat;
1254			if (*ep != '@' && *ep != '$')
1255				errx(1, "malformed interval/at:\n%s", errline);
1256
1257			working->flags |= CE_TRIMAT;
1258			working->trim_at = ptime_init(NULL);
1259			ptm_opts = PTM_PARSE_ISO8601;
1260			if (*ep == '$')
1261				ptm_opts = PTM_PARSE_DWM;
1262			ptm_opts |= PTM_PARSE_MATCHDOM;
1263			res = ptime_relparse(working->trim_at, ptm_opts,
1264			    ptimeget_secs(timenow), ep + 1);
1265			if (res == -2)
1266				errx(1, "nonexistent time for 'at' value:\n%s",
1267				    errline);
1268			else if (res < 0)
1269				errx(1, "malformed 'at' value:\n%s", errline);
1270		}
1271no_trimat:
1272
1273		if (eol)
1274			q = NULL;
1275		else {
1276			q = parse = sob(parse + 1);	/* Optional field */
1277			parse = son(parse);
1278			if (!*parse)
1279				eol = 1;
1280			*parse = '\0';
1281		}
1282
1283		for (; q && *q && !isspacech(*q); q++) {
1284			switch (tolowerch(*q)) {
1285			case 'b':
1286				working->flags |= CE_BINARY;
1287				break;
1288			case 'c':
1289				/*
1290				 * XXX - 	Ick! Ugly! Remove ASAP!
1291				 * We want `c' and `C' for "create".  But we
1292				 * will temporarily treat `c' as `g', because
1293				 * FreeBSD releases <= 4.8 have a typo of
1294				 * checking  ('G' || 'c')  for CE_GLOB.
1295				 */
1296				if (*q == 'c') {
1297					warnx("Assuming 'g' for 'c' in flags for line:\n%s",
1298					    errline);
1299					warnx("The 'c' flag will eventually mean 'CREATE'");
1300					working->flags |= CE_GLOB;
1301					break;
1302				}
1303				working->flags |= CE_CREATE;
1304				break;
1305			case 'd':
1306				working->flags |= CE_NODUMP;
1307				break;
1308			case 'g':
1309				working->flags |= CE_GLOB;
1310				break;
1311			case 'j':
1312				working->compress = COMPRESS_BZIP2;
1313				break;
1314			case 'n':
1315				working->flags |= CE_NOSIGNAL;
1316				break;
1317			case 'r':
1318				working->flags |= CE_PID2CMD;
1319				break;
1320			case 't':
1321				working->flags |= CE_RFC5424;
1322				break;
1323			case 'u':
1324				working->flags |= CE_SIGNALGROUP;
1325				break;
1326			case 'w':
1327				/* Deprecated flag - keep for compatibility purposes */
1328				break;
1329			case 'x':
1330				working->compress = COMPRESS_XZ;
1331				break;
1332			case 'z':
1333				working->compress = COMPRESS_GZIP;
1334				break;
1335			case '-':
1336				break;
1337			case 'f':	/* Used by OpenBSD for "CE_FOLLOW" */
1338			case 'm':	/* Used by OpenBSD for "CE_MONITOR" */
1339			case 'p':	/* Used by NetBSD  for "CE_PLAIN0" */
1340			default:
1341				errx(1, "illegal flag in config file -- %c",
1342				    *q);
1343			}
1344		}
1345
1346		if (eol)
1347			q = NULL;
1348		else {
1349			q = parse = sob(parse + 1);	/* Optional field */
1350			parse = son(parse);
1351			if (!*parse)
1352				eol = 1;
1353			*parse = '\0';
1354		}
1355
1356		working->pid_cmd_file = NULL;
1357		if (q && *q) {
1358			if (*q == '/')
1359				working->pid_cmd_file = strdup(q);
1360			else if (isalnum(*q))
1361				goto got_sig;
1362			else {
1363				errx(1,
1364			"illegal pid file or signal in config file:\n%s",
1365				    errline);
1366			}
1367		}
1368		if (eol)
1369			q = NULL;
1370		else {
1371			q = parse = sob(parse + 1);	/* Optional field */
1372			*(parse = son(parse)) = '\0';
1373		}
1374
1375		working->sig = SIGHUP;
1376		if (q && *q) {
1377got_sig:
1378			working->sig = parse_signal(q);
1379			if (working->sig < 1 || working->sig >= sys_nsig) {
1380				errx(1,
1381				    "illegal signal in config file:\n%s",
1382				    errline);
1383			}
1384		}
1385
1386		/*
1387		 * Finish figuring out what pid-file to use (if any) in
1388		 * later processing if this logfile needs to be rotated.
1389		 */
1390		if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) {
1391			/*
1392			 * This config-entry specified 'n' for nosignal,
1393			 * see if it also specified an explicit pid_cmd_file.
1394			 * This would be a pretty pointless combination.
1395			 */
1396			if (working->pid_cmd_file != NULL) {
1397				warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s",
1398				    working->pid_cmd_file, errline);
1399				free(working->pid_cmd_file);
1400				working->pid_cmd_file = NULL;
1401			}
1402		} else if (working->pid_cmd_file == NULL) {
1403			/*
1404			 * This entry did not specify the 'n' flag, which
1405			 * means it should signal syslogd unless it had
1406			 * specified some other pid-file (and obviously the
1407			 * syslog pid-file will not be for a process-group).
1408			 * Also, we should only try to notify syslog if we
1409			 * are root.
1410			 */
1411			if (working->flags & CE_SIGNALGROUP) {
1412				warnx("Ignoring flag 'U' in line:\n%s",
1413				    errline);
1414				working->flags &= ~CE_SIGNALGROUP;
1415			}
1416			if (needroot)
1417				working->pid_cmd_file = strdup(path_syslogpid);
1418		}
1419
1420		/*
1421		 * Add this entry to the appropriate list of entries, unless
1422		 * it was some kind of special entry (eg: <default>).
1423		 */
1424		if (special) {
1425			;			/* Do not add to any list */
1426		} else if (working->flags & CE_GLOB) {
1427			STAILQ_INSERT_TAIL(glob_p, working, cf_nextp);
1428		} else {
1429			STAILQ_INSERT_TAIL(work_p, working, cf_nextp);
1430		}
1431	}
1432	if (errline != NULL)
1433		free(errline);
1434}
1435
1436static char *
1437missing_field(char *p, char *errline)
1438{
1439
1440	if (!p || !*p)
1441		errx(1, "missing field in config file:\n%s", errline);
1442	return (p);
1443}
1444
1445/*
1446 * In our sort we return it in the reverse of what qsort normally
1447 * would do, as we want the newest files first.  If we have two
1448 * entries with the same time we don't really care about order.
1449 *
1450 * Support function for qsort() in delete_oldest_timelog().
1451 */
1452static int
1453oldlog_entry_compare(const void *a, const void *b)
1454{
1455	const struct oldlog_entry *ola = a, *olb = b;
1456
1457	if (ola->t > olb->t)
1458		return (-1);
1459	else if (ola->t < olb->t)
1460		return (1);
1461	else
1462		return (0);
1463}
1464
1465/*
1466 * Check whether the file corresponding to dp is an archive of the logfile
1467 * logfname, based on the timefnamefmt format string. Return true and fill out
1468 * tm if this is the case; otherwise return false.
1469 */
1470static int
1471validate_old_timelog(int fd, const struct dirent *dp, const char *logfname,
1472    struct tm *tm)
1473{
1474	struct stat sb;
1475	size_t logfname_len;
1476	char *s;
1477	int c;
1478
1479	logfname_len = strlen(logfname);
1480
1481	if (dp->d_type != DT_REG) {
1482		/*
1483		 * Some filesystems (e.g. NFS) don't fill out the d_type field
1484		 * and leave it set to DT_UNKNOWN; in this case we must obtain
1485		 * the file type ourselves.
1486		 */
1487		if (dp->d_type != DT_UNKNOWN ||
1488		    fstatat(fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) != 0 ||
1489		    !S_ISREG(sb.st_mode))
1490			return (0);
1491	}
1492	/* Ignore everything but files with our logfile prefix. */
1493	if (strncmp(dp->d_name, logfname, logfname_len) != 0)
1494		return (0);
1495	/* Ignore the actual non-rotated logfile. */
1496	if (dp->d_namlen == logfname_len)
1497		return (0);
1498
1499	/*
1500	 * Make sure we created have found a logfile, so the
1501	 * postfix is valid, IE format is: '.<time>(.[bgx]z)?'.
1502	 */
1503	if (dp->d_name[logfname_len] != '.') {
1504		if (verbose)
1505			printf("Ignoring %s which has unexpected "
1506			    "extension '%s'\n", dp->d_name,
1507			    &dp->d_name[logfname_len]);
1508		return (0);
1509	}
1510	memset(tm, 0, sizeof(*tm));
1511	if ((s = strptime(&dp->d_name[logfname_len + 1],
1512	    timefnamefmt, tm)) == NULL) {
1513		/*
1514		 * We could special case "old" sequentially named logfiles here,
1515		 * but we do not as that would require special handling to
1516		 * decide which one was the oldest compared to "new" time based
1517		 * logfiles.
1518		 */
1519		if (verbose)
1520			printf("Ignoring %s which does not "
1521			    "match time format\n", dp->d_name);
1522		return (0);
1523	}
1524
1525	for (c = 0; c < COMPRESS_TYPES; c++)
1526		if (strcmp(s, compress_type[c].suffix) == 0)
1527			/* We're done. */
1528			return (1);
1529
1530	if (verbose)
1531		printf("Ignoring %s which has unexpected extension '%s'\n",
1532		    dp->d_name, s);
1533
1534	return (0);
1535}
1536
1537/*
1538 * Delete the oldest logfiles, when using time based filenames.
1539 */
1540static void
1541delete_oldest_timelog(const struct conf_entry *ent, const char *archive_dir)
1542{
1543	char *logfname, *s, *dir, errbuf[80];
1544	int dir_fd, i, logcnt, max_logcnt;
1545	struct oldlog_entry *oldlogs;
1546	struct dirent *dp;
1547	const char *cdir;
1548	struct tm tm;
1549	DIR *dirp;
1550
1551	oldlogs = malloc(MAX_OLDLOGS * sizeof(struct oldlog_entry));
1552	max_logcnt = MAX_OLDLOGS;
1553	logcnt = 0;
1554
1555	if (archive_dir != NULL && archive_dir[0] != '\0')
1556		cdir = archive_dir;
1557	else
1558		if ((cdir = dirname(ent->log)) == NULL)
1559			err(1, "dirname()");
1560	if ((dir = strdup(cdir)) == NULL)
1561		err(1, "strdup()");
1562
1563	if ((s = basename(ent->log)) == NULL)
1564		err(1, "basename()");
1565	if ((logfname = strdup(s)) == NULL)
1566		err(1, "strdup()");
1567	if (strcmp(logfname, "/") == 0)
1568		errx(1, "Invalid log filename - became '/'");
1569
1570	if (verbose > 2)
1571		printf("Searching for old logs in %s\n", dir);
1572
1573	/* First we create a 'list' of all archived logfiles */
1574	if ((dirp = opendir(dir)) == NULL)
1575		err(1, "Cannot open log directory '%s'", dir);
1576	dir_fd = dirfd(dirp);
1577	while ((dp = readdir(dirp)) != NULL) {
1578		if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0)
1579			continue;
1580
1581		/*
1582		 * We should now have old an old rotated logfile, so
1583		 * add it to the 'list'.
1584		 */
1585		if ((oldlogs[logcnt].t = timegm(&tm)) == -1)
1586			err(1, "Could not convert time string to time value");
1587		if ((oldlogs[logcnt].fname = strdup(dp->d_name)) == NULL)
1588			err(1, "strdup()");
1589		logcnt++;
1590
1591		/*
1592		 * It is very unlikely we ever run out of space in the
1593		 * logfile array from the default size, but lets
1594		 * handle it anyway...
1595		 */
1596		if (logcnt >= max_logcnt) {
1597			max_logcnt *= 4;
1598			/* Detect integer overflow */
1599			if (max_logcnt < logcnt)
1600				errx(1, "Too many old logfiles found");
1601			oldlogs = realloc(oldlogs,
1602			    max_logcnt * sizeof(struct oldlog_entry));
1603			if (oldlogs == NULL)
1604				err(1, "realloc()");
1605		}
1606	}
1607
1608	/* Second, if needed we delete oldest archived logfiles */
1609	if (logcnt > 0 && logcnt >= ent->numlogs && ent->numlogs > 1) {
1610		oldlogs = realloc(oldlogs, logcnt *
1611		    sizeof(struct oldlog_entry));
1612		if (oldlogs == NULL)
1613			err(1, "realloc()");
1614
1615		/*
1616		 * We now sort the logs in the order of newest to
1617		 * oldest.  That way we can simply skip over the
1618		 * number of records we want to keep.
1619		 */
1620		qsort(oldlogs, logcnt, sizeof(struct oldlog_entry),
1621		    oldlog_entry_compare);
1622		for (i = ent->numlogs - 1; i < logcnt; i++) {
1623			if (noaction)
1624				printf("\trm -f %s/%s\n", dir,
1625				    oldlogs[i].fname);
1626			else if (unlinkat(dir_fd, oldlogs[i].fname, 0) != 0) {
1627				snprintf(errbuf, sizeof(errbuf),
1628				    "Could not delete old logfile '%s'",
1629				    oldlogs[i].fname);
1630				perror(errbuf);
1631			}
1632		}
1633	} else if (verbose > 1)
1634		printf("No old logs to delete for logfile %s\n", ent->log);
1635
1636	/* Third, cleanup */
1637	closedir(dirp);
1638	for (i = 0; i < logcnt; i++) {
1639		assert(oldlogs[i].fname != NULL);
1640		free(oldlogs[i].fname);
1641	}
1642	free(oldlogs);
1643	free(logfname);
1644	free(dir);
1645}
1646
1647/*
1648 * Generate a log filename, when using classic filenames.
1649 */
1650static void
1651gen_classiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
1652    const char *namepart, int numlogs_c)
1653{
1654
1655	if (archive_dir[0] != '\0')
1656		(void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir,
1657		    namepart, numlogs_c);
1658	else
1659		(void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c);
1660}
1661
1662/*
1663 * Delete a rotated logfile, when using classic filenames.
1664 */
1665static void
1666delete_classiclog(const char *archive_dir, const char *namepart, int numlog_c)
1667{
1668	char file1[MAXPATHLEN], zfile1[MAXPATHLEN];
1669	int c;
1670
1671	gen_classiclog_fname(file1, sizeof(file1), archive_dir, namepart,
1672	    numlog_c);
1673
1674	for (c = 0; c < COMPRESS_TYPES; c++) {
1675		(void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
1676		    compress_type[c].suffix);
1677		if (noaction)
1678			printf("\trm -f %s\n", zfile1);
1679		else
1680			(void) unlink(zfile1);
1681	}
1682}
1683
1684/*
1685 * Only add to the queue if the file hasn't already been added. This is
1686 * done to prevent circular include loops.
1687 */
1688static void
1689add_to_queue(const char *fname, struct ilist *inclist)
1690{
1691	struct include_entry *inc;
1692
1693	STAILQ_FOREACH(inc, inclist, inc_nextp) {
1694		if (strcmp(fname, inc->file) == 0) {
1695			warnx("duplicate include detected: %s", fname);
1696			return;
1697		}
1698	}
1699
1700	inc = malloc(sizeof(struct include_entry));
1701	if (inc == NULL)
1702		err(1, "malloc of inc");
1703	inc->file = strdup(fname);
1704
1705	if (verbose > 2)
1706		printf("\t+ Adding %s to the processing queue.\n", fname);
1707
1708	STAILQ_INSERT_TAIL(inclist, inc, inc_nextp);
1709}
1710
1711/*
1712 * Search for logfile and return its compression suffix (if supported)
1713 * The suffix detection is first-match in the order of compress_types
1714 *
1715 * Note: if logfile without suffix exists (uncompressed, COMPRESS_NONE)
1716 * a zero-length string is returned
1717 */
1718static const char *
1719get_logfile_suffix(const char *logfile)
1720{
1721	struct stat st;
1722	char zfile[MAXPATHLEN];
1723	int c;
1724
1725	for (c = 0; c < COMPRESS_TYPES; c++) {
1726		(void) strlcpy(zfile, logfile, MAXPATHLEN);
1727		(void) strlcat(zfile, compress_type[c].suffix, MAXPATHLEN);
1728		if (lstat(zfile, &st) == 0)
1729			return (compress_type[c].suffix);
1730	}
1731	return (NULL);
1732}
1733
1734static fk_entry
1735do_rotate(const struct conf_entry *ent)
1736{
1737	char dirpart[MAXPATHLEN], namepart[MAXPATHLEN];
1738	char file1[MAXPATHLEN], file2[MAXPATHLEN];
1739	char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1740	const char *logfile_suffix;
1741	char datetimestr[30];
1742	int flags, numlogs_c;
1743	fk_entry free_or_keep;
1744	struct sigwork_entry *swork;
1745	struct stat st;
1746	struct tm tm;
1747	time_t now;
1748
1749	flags = ent->flags;
1750	free_or_keep = FREE_ENT;
1751
1752	if (archtodir) {
1753		char *p;
1754
1755		/* build complete name of archive directory into dirpart */
1756		if (*archdirname == '/') {	/* absolute */
1757			strlcpy(dirpart, archdirname, sizeof(dirpart));
1758		} else {	/* relative */
1759			/* get directory part of logfile */
1760			strlcpy(dirpart, ent->log, sizeof(dirpart));
1761			if ((p = strrchr(dirpart, '/')) == NULL)
1762				dirpart[0] = '\0';
1763			else
1764				*(p + 1) = '\0';
1765			strlcat(dirpart, archdirname, sizeof(dirpart));
1766		}
1767
1768		/* check if archive directory exists, if not, create it */
1769		if (lstat(dirpart, &st))
1770			createdir(ent, dirpart);
1771
1772		/* get filename part of logfile */
1773		if ((p = strrchr(ent->log, '/')) == NULL)
1774			strlcpy(namepart, ent->log, sizeof(namepart));
1775		else
1776			strlcpy(namepart, p + 1, sizeof(namepart));
1777	} else {
1778		/*
1779		 * Tell utility functions we are not using an archive
1780		 * dir.
1781		 */
1782		dirpart[0] = '\0';
1783		strlcpy(namepart, ent->log, sizeof(namepart));
1784	}
1785
1786	/* Delete old logs */
1787	if (timefnamefmt != NULL)
1788		delete_oldest_timelog(ent, dirpart);
1789	else {
1790		/*
1791		 * Handle cleaning up after legacy newsyslog where we
1792		 * kept ent->numlogs + 1 files.  This code can go away
1793		 * at some point in the future.
1794		 */
1795		delete_classiclog(dirpart, namepart, ent->numlogs);
1796
1797		if (ent->numlogs > 0)
1798			delete_classiclog(dirpart, namepart, ent->numlogs - 1);
1799
1800	}
1801
1802	if (timefnamefmt != NULL) {
1803		/* If time functions fails we can't really do any sensible */
1804		if (time(&now) == (time_t)-1 ||
1805		    localtime_r(&now, &tm) == NULL)
1806			bzero(&tm, sizeof(tm));
1807
1808		strftime(datetimestr, sizeof(datetimestr), timefnamefmt, &tm);
1809		if (archtodir)
1810			(void) snprintf(file1, sizeof(file1), "%s/%s.%s",
1811			    dirpart, namepart, datetimestr);
1812		else
1813			(void) snprintf(file1, sizeof(file1), "%s.%s",
1814			    ent->log, datetimestr);
1815
1816		/* Don't run the code to move down logs */
1817		numlogs_c = -1;
1818	} else {
1819		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
1820		    ent->numlogs - 1);
1821		numlogs_c = ent->numlogs - 2;		/* copy for countdown */
1822	}
1823
1824	/* Move down log files */
1825	for (; numlogs_c >= 0; numlogs_c--) {
1826		(void) strlcpy(file2, file1, sizeof(file2));
1827
1828		gen_classiclog_fname(file1, sizeof(file1), dirpart, namepart,
1829		    numlogs_c);
1830
1831		logfile_suffix = get_logfile_suffix(file1);
1832		if (logfile_suffix == NULL)
1833			continue;
1834		(void) strlcpy(zfile1, file1, MAXPATHLEN);
1835		(void) strlcpy(zfile2, file2, MAXPATHLEN);
1836		(void) strlcat(zfile1, logfile_suffix, MAXPATHLEN);
1837		(void) strlcat(zfile2, logfile_suffix, MAXPATHLEN);
1838
1839		if (noaction)
1840			printf("\tmv %s %s\n", zfile1, zfile2);
1841		else {
1842			/* XXX - Ought to be checking for failure! */
1843			(void)rename(zfile1, zfile2);
1844		}
1845		change_attrs(zfile2, ent);
1846	}
1847
1848	if (ent->numlogs > 0) {
1849		if (noaction) {
1850			/*
1851			 * Note that savelog() may succeed with using link()
1852			 * for the archtodir case, but there is no good way
1853			 * of knowing if it will when doing "noaction", so
1854			 * here we claim that it will have to do a copy...
1855			 */
1856			if (archtodir)
1857				printf("\tcp %s %s\n", ent->log, file1);
1858			else
1859				printf("\tln %s %s\n", ent->log, file1);
1860			printf("\ttouch %s\t\t"
1861			    "# Update mtime for 'when'-interval processing\n",
1862			    file1);
1863		} else {
1864			if (!(flags & CE_BINARY)) {
1865				/* Report the trimming to the old log */
1866				log_trim(ent->log, ent);
1867			}
1868			savelog(ent->log, file1);
1869			/*
1870			 * Interval-based rotations are done using the mtime of
1871			 * the most recently archived log, so make sure it gets
1872			 * updated during a rotation.
1873			 */
1874			utimes(file1, NULL);
1875		}
1876		change_attrs(file1, ent);
1877	}
1878
1879	/* Create the new log file and move it into place */
1880	if (noaction)
1881		printf("Start new log...\n");
1882	createlog(ent);
1883
1884	/*
1885	 * Save all signalling and file-compression to be done after log
1886	 * files from all entries have been rotated.  This way any one
1887	 * process will not be sent the same signal multiple times when
1888	 * multiple log files had to be rotated.
1889	 */
1890	swork = NULL;
1891	if (ent->pid_cmd_file != NULL)
1892		swork = save_sigwork(ent);
1893	if (ent->numlogs > 0 && ent->compress > COMPRESS_NONE) {
1894		/*
1895		 * The zipwork_entry will include a pointer to this
1896		 * conf_entry, so the conf_entry should not be freed.
1897		 */
1898		free_or_keep = KEEP_ENT;
1899		save_zipwork(ent, swork, ent->fsize, file1);
1900	}
1901
1902	return (free_or_keep);
1903}
1904
1905static void
1906do_sigwork(struct sigwork_entry *swork)
1907{
1908	struct sigwork_entry *nextsig;
1909	int kres, secs;
1910	char *tmp;
1911
1912	if (swork->sw_runcmd == 0 && (!(swork->sw_pidok) || swork->sw_pid == 0))
1913		return;			/* no work to do... */
1914
1915	/*
1916	 * If nosignal (-s) was specified, then do not signal any process.
1917	 * Note that a nosignal request triggers a warning message if the
1918	 * rotated logfile needs to be compressed, *unless* -R was also
1919	 * specified.  We assume that an `-sR' request came from a process
1920	 * which writes to the logfile, and as such, we assume that process
1921	 * has already made sure the logfile is not presently in use.  This
1922	 * just sets swork->sw_pidok to a special value, and do_zipwork
1923	 * will print any necessary warning(s).
1924	 */
1925	if (nosignal) {
1926		if (!rotatereq)
1927			swork->sw_pidok = -1;
1928		return;
1929	}
1930
1931	/*
1932	 * Compute the pause between consecutive signals.  Use a longer
1933	 * sleep time if we will be sending two signals to the same
1934	 * daemon or process-group.
1935	 */
1936	secs = 0;
1937	nextsig = SLIST_NEXT(swork, sw_nextp);
1938	if (nextsig != NULL) {
1939		if (swork->sw_pid == nextsig->sw_pid)
1940			secs = 10;
1941		else
1942			secs = 1;
1943	}
1944
1945	if (noaction) {
1946		if (swork->sw_runcmd)
1947			printf("\tsh -c '%s %d'\n", swork->sw_fname,
1948			    swork->sw_signum);
1949		else {
1950			printf("\tkill -%d %d \t\t# %s\n", swork->sw_signum,
1951			    (int)swork->sw_pid, swork->sw_fname);
1952			if (secs > 0)
1953				printf("\tsleep %d\n", secs);
1954		}
1955		return;
1956	}
1957
1958	if (swork->sw_runcmd) {
1959		asprintf(&tmp, "%s %d", swork->sw_fname, swork->sw_signum);
1960		if (tmp == NULL) {
1961			warn("can't allocate memory to run %s",
1962			    swork->sw_fname);
1963			return;
1964		}
1965		if (verbose)
1966			printf("Run command: %s\n", tmp);
1967		kres = system(tmp);
1968		if (kres) {
1969			warnx("%s: returned non-zero exit code: %d",
1970			    tmp, kres);
1971		}
1972		free(tmp);
1973		return;
1974	}
1975
1976	kres = kill(swork->sw_pid, swork->sw_signum);
1977	if (kres != 0) {
1978		/*
1979		 * Assume that "no such process" (ESRCH) is something
1980		 * to warn about, but is not an error.  Presumably the
1981		 * process which writes to the rotated log file(s) is
1982		 * gone, in which case we should have no problem with
1983		 * compressing the rotated log file(s).
1984		 */
1985		if (errno != ESRCH)
1986			swork->sw_pidok = 0;
1987		warn("can't notify %s, pid %d = %s", swork->sw_pidtype,
1988		    (int)swork->sw_pid, swork->sw_fname);
1989	} else {
1990		if (verbose)
1991			printf("Notified %s pid %d = %s\n", swork->sw_pidtype,
1992			    (int)swork->sw_pid, swork->sw_fname);
1993		if (secs > 0) {
1994			if (verbose)
1995				printf("Pause %d second(s) between signals\n",
1996				    secs);
1997			sleep(secs);
1998		}
1999	}
2000}
2001
2002static void
2003do_zipwork(struct zipwork_entry *zwork)
2004{
2005	const char *pgm_name, *pgm_path;
2006	int errsav, fcount, zstatus;
2007	pid_t pidzip, wpid;
2008	char zresult[MAXPATHLEN];
2009	int c;
2010
2011	assert(zwork != NULL);
2012	pgm_path = NULL;
2013	strlcpy(zresult, zwork->zw_fname, sizeof(zresult));
2014	if (zwork->zw_conf != NULL &&
2015	    zwork->zw_conf->compress > COMPRESS_NONE)
2016		for (c = 1; c < COMPRESS_TYPES; c++) {
2017			if (zwork->zw_conf->compress == c) {
2018				pgm_path = compress_type[c].path;
2019				(void) strlcat(zresult,
2020				    compress_type[c].suffix, sizeof(zresult));
2021				break;
2022			}
2023		}
2024	if (pgm_path == NULL) {
2025		warnx("invalid entry for %s in do_zipwork", zwork->zw_fname);
2026		return;
2027	}
2028	pgm_name = strrchr(pgm_path, '/');
2029	if (pgm_name == NULL)
2030		pgm_name = pgm_path;
2031	else
2032		pgm_name++;
2033
2034	if (zwork->zw_swork != NULL && zwork->zw_swork->sw_runcmd == 0 &&
2035	    zwork->zw_swork->sw_pidok <= 0) {
2036		warnx(
2037		    "log %s not compressed because daemon(s) not notified",
2038		    zwork->zw_fname);
2039		change_attrs(zwork->zw_fname, zwork->zw_conf);
2040		return;
2041	}
2042
2043	if (noaction) {
2044		printf("\t%s %s\n", pgm_name, zwork->zw_fname);
2045		change_attrs(zresult, zwork->zw_conf);
2046		return;
2047	}
2048
2049	fcount = 1;
2050	pidzip = fork();
2051	while (pidzip < 0) {
2052		/*
2053		 * The fork failed.  If the failure was due to a temporary
2054		 * problem, then wait a short time and try it again.
2055		 */
2056		errsav = errno;
2057		warn("fork() for `%s %s'", pgm_name, zwork->zw_fname);
2058		if (errsav != EAGAIN || fcount > 5)
2059			errx(1, "Exiting...");
2060		sleep(fcount * 12);
2061		fcount++;
2062		pidzip = fork();
2063	}
2064	if (!pidzip) {
2065		/* The child process executes the compression command */
2066		execl(pgm_path, pgm_path, "-f", zwork->zw_fname, (char *)0);
2067		err(1, "execl(`%s -f %s')", pgm_path, zwork->zw_fname);
2068	}
2069
2070	wpid = waitpid(pidzip, &zstatus, 0);
2071	if (wpid == -1) {
2072		/* XXX - should this be a fatal error? */
2073		warn("%s: waitpid(%d)", pgm_path, pidzip);
2074		return;
2075	}
2076	if (!WIFEXITED(zstatus)) {
2077		warnx("`%s -f %s' did not terminate normally", pgm_name,
2078		    zwork->zw_fname);
2079		return;
2080	}
2081	if (WEXITSTATUS(zstatus)) {
2082		warnx("`%s -f %s' terminated with a non-zero status (%d)",
2083		    pgm_name, zwork->zw_fname, WEXITSTATUS(zstatus));
2084		return;
2085	}
2086
2087	/* Compression was successful, set file attributes on the result. */
2088	change_attrs(zresult, zwork->zw_conf);
2089}
2090
2091/*
2092 * Save information on any process we need to signal.  Any single
2093 * process may need to be sent different signal-values for different
2094 * log files, but usually a single signal-value will cause the process
2095 * to close and re-open all of it's log files.
2096 */
2097static struct sigwork_entry *
2098save_sigwork(const struct conf_entry *ent)
2099{
2100	struct sigwork_entry *sprev, *stmp;
2101	int ndiff;
2102	size_t tmpsiz;
2103
2104	sprev = NULL;
2105	ndiff = 1;
2106	SLIST_FOREACH(stmp, &swhead, sw_nextp) {
2107		ndiff = strcmp(ent->pid_cmd_file, stmp->sw_fname);
2108		if (ndiff > 0)
2109			break;
2110		if (ndiff == 0) {
2111			if (ent->sig == stmp->sw_signum)
2112				break;
2113			if (ent->sig > stmp->sw_signum) {
2114				ndiff = 1;
2115				break;
2116			}
2117		}
2118		sprev = stmp;
2119	}
2120	if (stmp != NULL && ndiff == 0)
2121		return (stmp);
2122
2123	tmpsiz = sizeof(struct sigwork_entry) + strlen(ent->pid_cmd_file) + 1;
2124	stmp = malloc(tmpsiz);
2125
2126	stmp->sw_runcmd = 0;
2127	/* If this is a command to run we just set the flag and run command */
2128	if (ent->flags & CE_PID2CMD) {
2129		stmp->sw_pid = -1;
2130		stmp->sw_pidok = 0;
2131		stmp->sw_runcmd = 1;
2132	} else {
2133		set_swpid(stmp, ent);
2134	}
2135	stmp->sw_signum = ent->sig;
2136	strcpy(stmp->sw_fname, ent->pid_cmd_file);
2137	if (sprev == NULL)
2138		SLIST_INSERT_HEAD(&swhead, stmp, sw_nextp);
2139	else
2140		SLIST_INSERT_AFTER(sprev, stmp, sw_nextp);
2141	return (stmp);
2142}
2143
2144/*
2145 * Save information on any file we need to compress.  We may see the same
2146 * file multiple times, so check the full list to avoid duplicates.  The
2147 * list itself is sorted smallest-to-largest, because that's the order we
2148 * want to compress the files.  If the partition is very low on disk space,
2149 * then the smallest files are the most likely to compress, and compressing
2150 * them first will free up more space for the larger files.
2151 */
2152static struct zipwork_entry *
2153save_zipwork(const struct conf_entry *ent, const struct sigwork_entry *swork,
2154    int zsize, const char *zipfname)
2155{
2156	struct zipwork_entry *zprev, *ztmp;
2157	int ndiff;
2158	size_t tmpsiz;
2159
2160	/* Compute the size if the caller did not know it. */
2161	if (zsize < 0)
2162		zsize = sizefile(zipfname);
2163
2164	zprev = NULL;
2165	ndiff = 1;
2166	SLIST_FOREACH(ztmp, &zwhead, zw_nextp) {
2167		ndiff = strcmp(zipfname, ztmp->zw_fname);
2168		if (ndiff == 0)
2169			break;
2170		if (zsize > ztmp->zw_fsize)
2171			zprev = ztmp;
2172	}
2173	if (ztmp != NULL && ndiff == 0)
2174		return (ztmp);
2175
2176	tmpsiz = sizeof(struct zipwork_entry) + strlen(zipfname) + 1;
2177	ztmp = malloc(tmpsiz);
2178	ztmp->zw_conf = ent;
2179	ztmp->zw_swork = swork;
2180	ztmp->zw_fsize = zsize;
2181	strcpy(ztmp->zw_fname, zipfname);
2182	if (zprev == NULL)
2183		SLIST_INSERT_HEAD(&zwhead, ztmp, zw_nextp);
2184	else
2185		SLIST_INSERT_AFTER(zprev, ztmp, zw_nextp);
2186	return (ztmp);
2187}
2188
2189/* Send a signal to the pid specified by pidfile */
2190static void
2191set_swpid(struct sigwork_entry *swork, const struct conf_entry *ent)
2192{
2193	FILE *f;
2194	long minok, maxok, rval;
2195	char *endp, *linep, line[BUFSIZ];
2196
2197	minok = MIN_PID;
2198	maxok = MAX_PID;
2199	swork->sw_pidok = 0;
2200	swork->sw_pid = 0;
2201	swork->sw_pidtype = "daemon";
2202	if (ent->flags & CE_SIGNALGROUP) {
2203		/*
2204		 * If we are expected to signal a process-group when
2205		 * rotating this logfile, then the value read in should
2206		 * be the negative of a valid process ID.
2207		 */
2208		minok = -MAX_PID;
2209		maxok = -MIN_PID;
2210		swork->sw_pidtype = "process-group";
2211	}
2212
2213	f = fopen(ent->pid_cmd_file, "r");
2214	if (f == NULL) {
2215		if (errno == ENOENT && enforcepid == 0) {
2216			/*
2217			 * Warn if the PID file doesn't exist, but do
2218			 * not consider it an error.  Most likely it
2219			 * means the process has been terminated,
2220			 * so it should be safe to rotate any log
2221			 * files that the process would have been using.
2222			 */
2223			swork->sw_pidok = 1;
2224			warnx("pid file doesn't exist: %s", ent->pid_cmd_file);
2225		} else
2226			warn("can't open pid file: %s", ent->pid_cmd_file);
2227		return;
2228	}
2229
2230	if (fgets(line, BUFSIZ, f) == NULL) {
2231		/*
2232		 * Warn if the PID file is empty, but do not consider
2233		 * it an error.  Most likely it means the process has
2234		 * has terminated, so it should be safe to rotate any
2235		 * log files that the process would have been using.
2236		 */
2237		if (feof(f) && enforcepid == 0) {
2238			swork->sw_pidok = 1;
2239			warnx("pid/cmd file is empty: %s", ent->pid_cmd_file);
2240		} else
2241			warn("can't read from pid file: %s", ent->pid_cmd_file);
2242		(void)fclose(f);
2243		return;
2244	}
2245	(void)fclose(f);
2246
2247	errno = 0;
2248	linep = line;
2249	while (*linep == ' ')
2250		linep++;
2251	rval = strtol(linep, &endp, 10);
2252	if (*endp != '\0' && !isspacech(*endp)) {
2253		warnx("pid file does not start with a valid number: %s",
2254		    ent->pid_cmd_file);
2255	} else if (rval < minok || rval > maxok) {
2256		warnx("bad value '%ld' for process number in %s",
2257		    rval, ent->pid_cmd_file);
2258		if (verbose)
2259			warnx("\t(expecting value between %ld and %ld)",
2260			    minok, maxok);
2261	} else {
2262		swork->sw_pidok = 1;
2263		swork->sw_pid = rval;
2264	}
2265
2266	return;
2267}
2268
2269/* Log the fact that the logs were turned over */
2270static int
2271log_trim(const char *logname, const struct conf_entry *log_ent)
2272{
2273	FILE *f;
2274	const char *xtra;
2275
2276	if ((f = fopen(logname, "a")) == NULL)
2277		return (-1);
2278	xtra = "";
2279	if (log_ent->def_cfg)
2280		xtra = " using <default> rule";
2281	if (log_ent->flags & CE_RFC5424) {
2282		if (log_ent->firstcreate) {
2283			fprintf(f, "<%d>1 %s %s newsyslog %d - - %s%s\n",
2284			    LOG_MAKEPRI(LOG_USER, LOG_INFO),
2285			    daytime_rfc5424, hostname, getpid(),
2286			    "logfile first created", xtra);
2287		} else if (log_ent->r_reason != NULL) {
2288			fprintf(f, "<%d>1 %s %s newsyslog %d - - %s%s%s\n",
2289			    LOG_MAKEPRI(LOG_USER, LOG_INFO),
2290			    daytime_rfc5424, hostname, getpid(),
2291			    "logfile turned over", log_ent->r_reason, xtra);
2292		} else {
2293			fprintf(f, "<%d>1 %s %s newsyslog %d - - %s%s\n",
2294			    LOG_MAKEPRI(LOG_USER, LOG_INFO),
2295			    daytime_rfc5424, hostname, getpid(),
2296			    "logfile turned over", xtra);
2297		}
2298	} else {
2299		if (log_ent->firstcreate)
2300			fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n",
2301			    daytime, hostname, getpid(), xtra);
2302		else if (log_ent->r_reason != NULL)
2303			fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n",
2304			    daytime, hostname, getpid(), log_ent->r_reason, xtra);
2305		else
2306			fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n",
2307			    daytime, hostname, getpid(), xtra);
2308	}
2309	if (fclose(f) == EOF)
2310		err(1, "log_trim: fclose");
2311	return (0);
2312}
2313
2314/* Return size in kilobytes of a file */
2315static int
2316sizefile(const char *file)
2317{
2318	struct stat sb;
2319
2320	if (stat(file, &sb) < 0)
2321		return (-1);
2322	return (kbytes(sb.st_size));
2323}
2324
2325/*
2326 * Return the mtime of the most recent archive of the logfile, using timestamp
2327 * based filenames.
2328 */
2329static time_t
2330mtime_old_timelog(const char *file)
2331{
2332	struct stat sb;
2333	struct tm tm;
2334	int dir_fd;
2335	time_t t;
2336	struct dirent *dp;
2337	DIR *dirp;
2338	char *s, *logfname, *dir;
2339
2340	t = -1;
2341
2342	if ((dir = dirname(file)) == NULL) {
2343		warn("dirname() of '%s'", file);
2344		return (t);
2345	}
2346	if ((s = basename(file)) == NULL) {
2347		warn("basename() of '%s'", file);
2348		return (t);
2349	} else if (s[0] == '/') {
2350		warnx("Invalid log filename '%s'", s);
2351		return (t);
2352	} else if ((logfname = strdup(s)) == NULL)
2353		err(1, "strdup()");
2354
2355	if ((dirp = opendir(dir)) == NULL) {
2356		warn("Cannot open log directory '%s'", dir);
2357		return (t);
2358	}
2359	dir_fd = dirfd(dirp);
2360	/* Open the archive dir and find the most recent archive of logfname. */
2361	while ((dp = readdir(dirp)) != NULL) {
2362		if (validate_old_timelog(dir_fd, dp, logfname, &tm) == 0)
2363			continue;
2364
2365		if (fstatat(dir_fd, dp->d_name, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
2366			warn("Cannot stat '%s'", file);
2367			continue;
2368		}
2369		if (t < sb.st_mtime)
2370			t = sb.st_mtime;
2371	}
2372	closedir(dirp);
2373
2374	return (t);
2375}
2376
2377/* Return the age in hours of the most recent archive of the logfile. */
2378static int
2379age_old_log(const char *file)
2380{
2381	struct stat sb;
2382	const char *logfile_suffix;
2383	char tmp[MAXPATHLEN + sizeof(".0") + COMPRESS_SUFFIX_MAXLEN + 1];
2384	time_t mtime;
2385
2386	if (archtodir) {
2387		char *p;
2388
2389		/* build name of archive directory into tmp */
2390		if (*archdirname == '/') {	/* absolute */
2391			strlcpy(tmp, archdirname, sizeof(tmp));
2392		} else {	/* relative */
2393			/* get directory part of logfile */
2394			strlcpy(tmp, file, sizeof(tmp));
2395			if ((p = strrchr(tmp, '/')) == NULL)
2396				tmp[0] = '\0';
2397			else
2398				*(p + 1) = '\0';
2399			strlcat(tmp, archdirname, sizeof(tmp));
2400		}
2401
2402		strlcat(tmp, "/", sizeof(tmp));
2403
2404		/* get filename part of logfile */
2405		if ((p = strrchr(file, '/')) == NULL)
2406			strlcat(tmp, file, sizeof(tmp));
2407		else
2408			strlcat(tmp, p + 1, sizeof(tmp));
2409	} else {
2410		(void) strlcpy(tmp, file, sizeof(tmp));
2411	}
2412
2413	if (timefnamefmt != NULL) {
2414		mtime = mtime_old_timelog(tmp);
2415		if (mtime == -1)
2416			return (-1);
2417	} else {
2418		strlcat(tmp, ".0", sizeof(tmp));
2419		logfile_suffix = get_logfile_suffix(tmp);
2420		if (logfile_suffix == NULL)
2421			return (-1);
2422		(void) strlcat(tmp, logfile_suffix, sizeof(tmp));
2423		if (stat(tmp, &sb) < 0)
2424			return (-1);
2425		mtime = sb.st_mtime;
2426	}
2427
2428	return ((int)(ptimeget_secs(timenow) - mtime + 1800) / 3600);
2429}
2430
2431/* Skip Over Blanks */
2432static char *
2433sob(char *p)
2434{
2435	while (p && *p && isspace(*p))
2436		p++;
2437	return (p);
2438}
2439
2440/* Skip Over Non-Blanks */
2441static char *
2442son(char *p)
2443{
2444	while (p && *p && !isspace(*p))
2445		p++;
2446	return (p);
2447}
2448
2449/* Check if string is actually a number */
2450static int
2451isnumberstr(const char *string)
2452{
2453	while (*string) {
2454		if (!isdigitch(*string++))
2455			return (0);
2456	}
2457	return (1);
2458}
2459
2460/* Check if string contains a glob */
2461static int
2462isglobstr(const char *string)
2463{
2464	char chr;
2465
2466	while ((chr = *string++)) {
2467		if (chr == '*' || chr == '?' || chr == '[')
2468			return (1);
2469	}
2470	return (0);
2471}
2472
2473/*
2474 * Save the active log file under a new name.  A link to the new name
2475 * is the quick-and-easy way to do this.  If that fails (which it will
2476 * if the destination is on another partition), then make a copy of
2477 * the file to the new location.
2478 */
2479static void
2480savelog(char *from, char *to)
2481{
2482	FILE *src, *dst;
2483	int c, res;
2484
2485	res = link(from, to);
2486	if (res == 0)
2487		return;
2488
2489	if ((src = fopen(from, "r")) == NULL)
2490		err(1, "can't fopen %s for reading", from);
2491	if ((dst = fopen(to, "w")) == NULL)
2492		err(1, "can't fopen %s for writing", to);
2493
2494	while ((c = getc(src)) != EOF) {
2495		if ((putc(c, dst)) == EOF)
2496			err(1, "error writing to %s", to);
2497	}
2498
2499	if (ferror(src))
2500		err(1, "error reading from %s", from);
2501	if ((fclose(src)) != 0)
2502		err(1, "can't fclose %s", to);
2503	if ((fclose(dst)) != 0)
2504		err(1, "can't fclose %s", from);
2505}
2506
2507/* create one or more directory components of a path */
2508static void
2509createdir(const struct conf_entry *ent, char *dirpart)
2510{
2511	int res;
2512	char *s, *d;
2513	char mkdirpath[MAXPATHLEN];
2514	struct stat st;
2515
2516	s = dirpart;
2517	d = mkdirpath;
2518
2519	for (;;) {
2520		*d++ = *s++;
2521		if (*s != '/' && *s != '\0')
2522			continue;
2523		*d = '\0';
2524		res = lstat(mkdirpath, &st);
2525		if (res != 0) {
2526			if (noaction) {
2527				printf("\tmkdir %s\n", mkdirpath);
2528			} else {
2529				res = mkdir(mkdirpath, 0755);
2530				if (res != 0)
2531					err(1, "Error on mkdir(\"%s\") for -a",
2532					    mkdirpath);
2533			}
2534		}
2535		if (*s == '\0')
2536			break;
2537	}
2538	if (verbose) {
2539		if (ent->firstcreate)
2540			printf("Created directory '%s' for new %s\n",
2541			    dirpart, ent->log);
2542		else
2543			printf("Created directory '%s' for -a\n", dirpart);
2544	}
2545}
2546
2547/*
2548 * Create a new log file, destroying any currently-existing version
2549 * of the log file in the process.  If the caller wants a backup copy
2550 * of the file to exist, they should call 'link(logfile,logbackup)'
2551 * before calling this routine.
2552 */
2553void
2554createlog(const struct conf_entry *ent)
2555{
2556	int fd, failed;
2557	struct stat st;
2558	char *realfile, *slash, tempfile[MAXPATHLEN];
2559
2560	fd = -1;
2561	realfile = ent->log;
2562
2563	/*
2564	 * If this log file is being created for the first time (-C option),
2565	 * then it may also be true that the parent directory does not exist
2566	 * yet.  Check, and create that directory if it is missing.
2567	 */
2568	if (ent->firstcreate) {
2569		strlcpy(tempfile, realfile, sizeof(tempfile));
2570		slash = strrchr(tempfile, '/');
2571		if (slash != NULL) {
2572			*slash = '\0';
2573			failed = stat(tempfile, &st);
2574			if (failed && errno != ENOENT)
2575				err(1, "Error on stat(%s)", tempfile);
2576			if (failed)
2577				createdir(ent, tempfile);
2578			else if (!S_ISDIR(st.st_mode))
2579				errx(1, "%s exists but is not a directory",
2580				    tempfile);
2581		}
2582	}
2583
2584	/*
2585	 * First create an unused filename, so it can be chown'ed and
2586	 * chmod'ed before it is moved into the real location.  mkstemp
2587	 * will create the file mode=600 & owned by us.  Note that all
2588	 * temp files will have a suffix of '.z<something>'.
2589	 */
2590	strlcpy(tempfile, realfile, sizeof(tempfile));
2591	strlcat(tempfile, ".zXXXXXX", sizeof(tempfile));
2592	if (noaction)
2593		printf("\tmktemp %s\n", tempfile);
2594	else {
2595		fd = mkstemp(tempfile);
2596		if (fd < 0)
2597			err(1, "can't mkstemp logfile %s", tempfile);
2598
2599		/*
2600		 * Add status message to what will become the new log file.
2601		 */
2602		if (!(ent->flags & CE_BINARY)) {
2603			if (log_trim(tempfile, ent))
2604				err(1, "can't add status message to log");
2605		}
2606	}
2607
2608	/* Change the owner/group, if we are supposed to */
2609	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2610		if (noaction)
2611			printf("\tchown %u:%u %s\n", ent->uid, ent->gid,
2612			    tempfile);
2613		else {
2614			failed = fchown(fd, ent->uid, ent->gid);
2615			if (failed)
2616				err(1, "can't fchown temp file %s", tempfile);
2617		}
2618	}
2619
2620	/* Turn on NODUMP if it was requested in the config-file. */
2621	if (ent->flags & CE_NODUMP) {
2622		if (noaction)
2623			printf("\tchflags nodump %s\n", tempfile);
2624		else {
2625			failed = fchflags(fd, UF_NODUMP);
2626			if (failed) {
2627				warn("log_trim: fchflags(NODUMP)");
2628			}
2629		}
2630	}
2631
2632	/*
2633	 * Note that if the real logfile still exists, and if the call
2634	 * to rename() fails, then "neither the old file nor the new
2635	 * file shall be changed or created" (to quote the standard).
2636	 * If the call succeeds, then the file will be replaced without
2637	 * any window where some other process might find that the file
2638	 * did not exist.
2639	 * XXX - ? It may be that for some error conditions, we could
2640	 *	retry by first removing the realfile and then renaming.
2641	 */
2642	if (noaction) {
2643		printf("\tchmod %o %s\n", ent->permissions, tempfile);
2644		printf("\tmv %s %s\n", tempfile, realfile);
2645	} else {
2646		failed = fchmod(fd, ent->permissions);
2647		if (failed)
2648			err(1, "can't fchmod temp file '%s'", tempfile);
2649		failed = rename(tempfile, realfile);
2650		if (failed)
2651			err(1, "can't mv %s to %s", tempfile, realfile);
2652	}
2653
2654	if (fd >= 0)
2655		close(fd);
2656}
2657
2658/*
2659 * Change the attributes of a given filename to what was specified in
2660 * the newsyslog.conf entry.  This routine is only called for files
2661 * that newsyslog expects that it has created, and thus it is a fatal
2662 * error if this routine finds that the file does not exist.
2663 */
2664static void
2665change_attrs(const char *fname, const struct conf_entry *ent)
2666{
2667	int failed;
2668
2669	if (noaction) {
2670		printf("\tchmod %o %s\n", ent->permissions, fname);
2671
2672		if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
2673			printf("\tchown %u:%u %s\n",
2674			    ent->uid, ent->gid, fname);
2675
2676		if (ent->flags & CE_NODUMP)
2677			printf("\tchflags nodump %s\n", fname);
2678		return;
2679	}
2680
2681	failed = chmod(fname, ent->permissions);
2682	if (failed) {
2683		if (errno != EPERM)
2684			err(1, "chmod(%s) in change_attrs", fname);
2685		warn("change_attrs couldn't chmod(%s)", fname);
2686	}
2687
2688	if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) {
2689		failed = chown(fname, ent->uid, ent->gid);
2690		if (failed)
2691			warn("can't chown %s", fname);
2692	}
2693
2694	if (ent->flags & CE_NODUMP) {
2695		failed = chflags(fname, UF_NODUMP);
2696		if (failed)
2697			warn("can't chflags %s NODUMP", fname);
2698	}
2699}
2700
2701/*
2702 * Parse a signal number or signal name. Returns the signal number parsed or -1
2703 * on failure.
2704 */
2705static int
2706parse_signal(const char *str)
2707{
2708	int sig, i;
2709	const char *errstr;
2710
2711	sig = strtonum(str, 1, sys_nsig - 1, &errstr);
2712
2713	if (errstr == NULL)
2714		return (sig);
2715	if (strncasecmp(str, "SIG", 3) == 0)
2716		str += 3;
2717
2718	for (i = 1; i < sys_nsig; i++) {
2719		if (strcasecmp(str, sys_signame[i]) == 0)
2720			return (i);
2721	}
2722
2723	return (-1);
2724}
2725