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