Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.67
1.67 ! deraadt 1: /* $OpenBSD: newsyslog.c,v 1.66 2003/06/03 01:52:41 millert Exp $ */
1.10 downsj 2:
3: /*
1.62 millert 4: * Copyright (c) 1999, 2002, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.30 millert 5: *
1.66 millert 6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND TODD C. MILLER DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12: * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL TODD C. MILLER BE LIABLE
13: * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15: * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.30 millert 17: */
18:
19: /*
1.10 downsj 20: * Copyright (c) 1997, Jason Downs. All rights reserved.
21: *
22: * Redistribution and use in source and binary forms, with or without
23: * modification, are permitted provided that the following conditions
24: * are met:
25: * 1. Redistributions of source code must retain the above copyright
26: * notice, this list of conditions and the following disclaimer.
27: * 2. Redistributions in binary form must reproduce the above copyright
28: * notice, this list of conditions and the following disclaimer in the
29: * documentation and/or other materials provided with the distribution.
30: *
31: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
32: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
35: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41: * SUCH DAMAGE.
42: */
1.3 deraadt 43:
1.1 deraadt 44: /*
45: * This file contains changes from the Open Software Foundation.
46: */
47:
48: /*
1.50 millert 49: * Copyright 1988, 1989 by the Massachusetts Institute of Technology
50: *
51: * Permission to use, copy, modify, and distribute this software
52: * and its documentation for any purpose and without fee is
53: * hereby granted, provided that the above copyright notice
54: * appear in all copies and that both that copyright notice and
55: * this permission notice appear in supporting documentation,
56: * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
57: * used in advertising or publicity pertaining to distribution
58: * of the software without specific, written prior permission.
59: * M.I.T. and the M.I.T. S.I.P.B. make no representations about
60: * the suitability of this software for any purpose. It is
61: * provided "as is" without express or implied warranty.
62: */
1.1 deraadt 63:
64: /*
65: * newsyslog - roll over selected logs at the appropriate time,
1.65 millert 66: * keeping the specified number of backup files around.
1.1 deraadt 67: *
68: */
69:
70: #ifndef lint
1.67 ! deraadt 71: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.66 2003/06/03 01:52:41 millert Exp $";
1.1 deraadt 72: #endif /* not lint */
73:
74: #ifndef CONF
1.47 millert 75: #define CONF "/etc/newsyslog.conf" /* Configuration file */
1.1 deraadt 76: #endif
77: #ifndef PIDFILE
78: #define PIDFILE "/etc/syslog.pid"
79: #endif
80: #ifndef COMPRESS
1.47 millert 81: #define COMPRESS "/usr/bin/compress" /* File compression program */
1.1 deraadt 82: #endif
83: #ifndef COMPRESS_POSTFIX
84: #define COMPRESS_POSTFIX ".Z"
85: #endif
1.10 downsj 86: #ifndef STATS_DIR
87: #define STATS_DIR "/etc"
88: #endif
89: #ifndef SENDMAIL
90: #define SENDMAIL "/usr/lib/sendmail"
91: #endif
1.1 deraadt 92:
1.48 millert 93: #include <sys/param.h>
94: #include <sys/stat.h>
1.9 downsj 95: #include <sys/time.h>
96: #include <sys/wait.h>
1.48 millert 97:
1.1 deraadt 98: #include <ctype.h>
1.48 millert 99: #include <err.h>
100: #include <errno.h>
1.9 downsj 101: #include <fcntl.h>
1.1 deraadt 102: #include <grp.h>
1.47 millert 103: #include <limits.h>
1.48 millert 104: #include <pwd.h>
105: #include <signal.h>
106: #include <stdio.h>
107: #include <stdlib.h>
108: #include <string.h>
1.9 downsj 109: #include <unistd.h>
1.1 deraadt 110:
1.26 millert 111: #define CE_ROTATED 0x01 /* Log file has been rotated */
1.65 millert 112: #define CE_COMPACT 0x02 /* Compact the archived log files */
1.26 millert 113: #define CE_BINARY 0x04 /* Logfile is in binary, don't add */
1.10 downsj 114: /* status messages */
1.65 millert 115: #define CE_MONITOR 0x08 /* Monitor for changes */
1.45 wcobb 116: #define CE_FOLLOW 0x10 /* Follow symbolic links */
1.65 millert 117: #define CE_TRIMAT 0x20 /* Trim at a specific time */
1.35 deraadt 118:
1.49 millert 119: #define MIN_PID 4 /* Don't touch pids lower than this */
1.58 millert 120: #define MIN_SIZE 256 /* Don't rotate if smaller (in bytes) */
1.49 millert 121:
1.50 millert 122: #define DPRINTF(x) do { if (verbose) printf x ; } while (0)
123:
1.1 deraadt 124: struct conf_entry {
1.35 deraadt 125: char *log; /* Name of the log */
1.51 millert 126: char *logbase; /* Basename of the log */
127: char *backdir; /* Directory in which to store backups */
1.35 deraadt 128: uid_t uid; /* Owner of log */
129: gid_t gid; /* Group of log */
130: int numlogs; /* Number of logs to keep */
1.58 millert 131: off_t size; /* Size cutoff to trigger trimming the log */
1.35 deraadt 132: int hours; /* Hours between log trimming */
1.63 millert 133: time_t trim_at; /* Specific time at which to do trimming */
1.35 deraadt 134: int permissions; /* File permissions on the log */
1.30 millert 135: int signal; /* Signal to send (defaults to SIGHUP) */
1.35 deraadt 136: int flags; /* Flags (CE_COMPACT & CE_BINARY) */
1.10 downsj 137: char *whom; /* Whom to notify if logfile changes */
1.65 millert 138: char *pidfile; /* Path to file containing pid to signal */
1.30 millert 139: char *runcmd; /* Command to run instead of sending a signal */
1.35 deraadt 140: struct conf_entry *next; /* Linked list pointer */
1.1 deraadt 141: };
142:
1.30 millert 143: struct pidinfo {
144: char *file;
145: int signal;
146: };
147:
1.52 millert 148: int verbose = 0; /* Print out what's going on */
149: int needroot = 1; /* Root privs are necessary */
150: int noaction = 0; /* Don't do anything, just show it */
1.33 millert 151: int monitormode = 0; /* Don't do monitoring by default */
1.52 millert 152: int force = 0; /* Force the logs to be rotated */
153: char *conf = CONF; /* Configuration file to use */
154: time_t timenow;
1.65 millert 155: char hostname[MAXHOSTNAMELEN]; /* Hostname */
1.52 millert 156: char *daytime; /* timenow in human readable form */
1.65 millert 157: char *arcdir; /* Dir to put archives in (if it exists) */
1.1 deraadt 158:
1.43 millert 159: void do_entry(struct conf_entry *);
1.48 millert 160: void parse_args(int, char **);
1.43 millert 161: void usage(void);
162: struct conf_entry *parse_file(int *);
1.60 millert 163: char *missing_field(char *, char *, int);
1.51 millert 164: void dotrim(struct conf_entry *);
1.43 millert 165: int log_trim(char *);
1.51 millert 166: void compress_log(struct conf_entry *);
1.58 millert 167: off_t sizefile(char *);
1.51 millert 168: int age_old_log(struct conf_entry *);
1.43 millert 169: char *sob(char *);
170: char *son(char *);
171: int isnumberstr(char *);
1.61 millert 172: int domonitor(struct conf_entry *);
1.43 millert 173: FILE *openmail(void);
174: void child_killer(int);
175: void run_command(char *);
176: void send_signal(char *, int);
1.57 millert 177: char *lstat_log(char *, size_t, int);
178: int stat_suffix(char *, size_t, char *, struct stat *, int (*)());
1.63 millert 179: time_t parse8601(char *);
180: time_t parseDWM(char *);
1.1 deraadt 181:
1.25 millert 182: int
1.48 millert 183: main(int argc, char **argv)
1.1 deraadt 184: {
1.53 millert 185: struct conf_entry *p, *q, *x, *y;
1.30 millert 186: struct pidinfo *pidlist, *pl;
1.53 millert 187: char **av;
1.28 millert 188: int status, listlen;
1.35 deraadt 189:
1.48 millert 190: parse_args(argc, argv);
1.53 millert 191: argc -= optind;
192: argv += optind;
193:
1.35 deraadt 194: if (needroot && getuid() && geteuid())
1.11 downsj 195: errx(1, "You must be root.");
1.53 millert 196:
197: p = parse_file(&listlen);
198: if (argc > 0) {
199: /* Only rotate specified files. */
200: x = y = NULL;
201: listlen = 0;
202: for (av = argv; *av; av++) {
203: for (q = p; q; q = q->next)
204: if (strcmp(*av, q->log) == 0) {
205: if (x == NULL)
206: x = y = q;
207: else {
208: y->next = q;
209: y = q;
210: }
211: listlen++;
212: break;
213: }
214: if (q == NULL)
1.60 millert 215: warnx("%s: %s not found", conf, *av);
1.53 millert 216: }
217: if (x == NULL)
1.60 millert 218: errx(1, "%s: no specified log files", conf);
1.53 millert 219: y->next = NULL;
220: p = x;
221: }
1.26 millert 222:
1.46 aaron 223: pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30 millert 224: if (pidlist == NULL)
1.28 millert 225: err(1, "calloc");
226:
1.53 millert 227: signal(SIGCHLD, child_killer);
228:
1.26 millert 229: /* Step 1, rotate all log files */
1.53 millert 230: for (q = p; q; q = q->next)
1.35 deraadt 231: do_entry(q);
1.26 millert 232:
1.28 millert 233: /* Step 2, make a list of unique pid files */
1.30 millert 234: for (q = p, pl = pidlist; q; ) {
1.28 millert 235: if (q->flags & CE_ROTATED) {
1.30 millert 236: struct pidinfo *pltmp;
1.28 millert 237:
1.30 millert 238: for (pltmp = pidlist; pltmp < pl; pltmp++) {
1.55 millert 239: if ((q->pidfile &&
240: strcmp(pltmp->file, q->pidfile) == 0 &&
241: pltmp->signal == q->signal) ||
242: (q->runcmd &&
1.30 millert 243: strcmp(q->runcmd, pltmp->file) == 0))
1.28 millert 244: break;
1.30 millert 245: }
246: if (pltmp == pl) { /* unique entry */
247: if (q->runcmd) {
248: pl->file = q->runcmd;
249: pl->signal = -1;
250: } else {
251: pl->file = q->pidfile;
252: pl->signal = q->signal;
253: }
1.31 millert 254: pl++;
1.30 millert 255: }
1.28 millert 256: }
1.35 deraadt 257: q = q->next;
258: }
1.26 millert 259:
1.30 millert 260: /* Step 3, send a signal or run a command */
261: for (pl = pidlist; pl->file; pl++) {
1.56 millert 262: if (pl->file != NULL) {
263: if (pl->signal == -1)
264: run_command(pl->file);
265: else
266: send_signal(pl->file, pl->signal);
267: }
1.30 millert 268: }
1.28 millert 269: if (!noaction)
270: sleep(5);
271:
272: /* Step 4, compress the log.0 file if configured to do so and free */
1.35 deraadt 273: while (p) {
1.26 millert 274: if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51 millert 275: compress_log(p);
1.26 millert 276: q = p;
1.35 deraadt 277: p = p->next;
278: free(q);
279: }
1.16 millert 280:
281: /* Wait for children to finish, then exit */
282: while (waitpid(-1, &status, 0) != -1)
283: ;
1.35 deraadt 284: exit(0);
1.1 deraadt 285: }
286:
1.25 millert 287: void
1.48 millert 288: do_entry(struct conf_entry *ent)
1.1 deraadt 289: {
1.58 millert 290: int modtime;
291: off_t size;
1.48 millert 292: struct stat sb;
1.45 wcobb 293:
1.51 millert 294: if (lstat(ent->log, &sb) != 0)
1.50 millert 295: return;
296: if (!S_ISREG(sb.st_mode) &&
297: (!S_ISLNK(sb.st_mode) || !(ent->flags & CE_FOLLOW))) {
298: DPRINTF(("--> not a regular file, skipping\n"));
299: return;
1.45 wcobb 300: }
1.14 millert 301:
1.61 millert 302: DPRINTF(("%s <%d%s%s%s%s>: ", ent->log, ent->numlogs,
1.51 millert 303: (ent->flags & CE_COMPACT) ? "Z" : "",
304: (ent->flags & CE_BINARY) ? "B" : "",
1.61 millert 305: (ent->flags & CE_FOLLOW) ? "F" : "",
306: (ent->flags & CE_MONITOR) && monitormode ? "M" : ""));
1.50 millert 307:
1.35 deraadt 308: size = sizefile(ent->log);
1.51 millert 309: modtime = age_old_log(ent);
1.35 deraadt 310: if (size < 0) {
1.50 millert 311: DPRINTF(("does not exist.\n"));
1.35 deraadt 312: } else {
1.63 millert 313: if (ent->flags & CE_TRIMAT && !force) {
314: if (timenow < ent->trim_at ||
315: difftime(timenow, ent->trim_at) >= 60 * 60) {
316: DPRINTF(("--> will trim at %s",
317: ctime(&ent->trim_at)));
318: return;
1.65 millert 319: } else if (ent->hours <= 0) {
1.63 millert 320: DPRINTF(("--> time is up\n"));
321: }
322: }
1.50 millert 323: if (ent->size > 0)
1.58 millert 324: DPRINTF(("size (KB): %.2f [%d] ", size / 1024.0,
325: (int)(ent->size / 1024)));
1.50 millert 326: if (ent->hours > 0)
327: DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.61 millert 328: if (monitormode && (ent->flags & CE_MONITOR) && domonitor(ent))
329: DPRINTF(("--> monitored\n"));
330: else if (!monitormode &&
331: (force || (ent->size > 0 && size >= ent->size) ||
1.63 millert 332: (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) ||
1.49 millert 333: (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
334: && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50 millert 335: DPRINTF(("--> trimming log....\n"));
1.19 kstailey 336: if (noaction && !verbose)
1.50 millert 337: printf("%s <%d%s%s%s>\n", ent->log,
338: ent->numlogs,
339: (ent->flags & CE_COMPACT) ? "Z" : "",
340: (ent->flags & CE_BINARY) ? "B" : "",
341: (ent->flags & CE_FOLLOW) ? "F" : "");
1.51 millert 342: dotrim(ent);
1.26 millert 343: ent->flags |= CE_ROTATED;
1.50 millert 344: } else
345: DPRINTF(("--> skipping\n"));
1.35 deraadt 346: }
1.1 deraadt 347: }
348:
1.30 millert 349: /* Run the specified command */
350: void
1.48 millert 351: run_command(char *cmd)
1.30 millert 352: {
353: if (noaction)
1.55 millert 354: (void)printf("run %s\n", cmd);
1.30 millert 355: else
356: system(cmd);
357: }
358:
359: /* Send a signal to the pid specified by pidfile */
1.26 millert 360: void
1.48 millert 361: send_signal(char *pidfile, int signal)
1.26 millert 362: {
1.48 millert 363: pid_t pid;
364: FILE *f;
365: char line[BUFSIZ], *ep, *err;
1.40 deraadt 366: unsigned long ulval;
1.26 millert 367:
1.35 deraadt 368: if ((f = fopen(pidfile, "r")) == NULL) {
1.26 millert 369: warn("can't open %s", pidfile);
370: return;
371: }
372:
1.48 millert 373: pid = 0;
1.42 millert 374: errno = 0;
375: err = NULL;
1.40 deraadt 376: if (fgets(line, sizeof(line), f)) {
377: ulval = strtoul(line, &ep, 10);
1.41 marc 378: if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40 deraadt 379: err = "invalid number in";
380: else if (errno == ERANGE && ulval == ULONG_MAX)
381: err = "out of range number in";
382: else if (ulval == 0)
383: err = "no number in";
384: else if (ulval < MIN_PID)
385: err = "preposterous process number in";
386: else
387: pid = ulval;
1.42 millert 388: } else {
389: if (errno == 0)
390: err = "empty";
391: else
392: err = "error reading";
1.40 deraadt 393: }
1.26 millert 394: (void)fclose(f);
395:
1.40 deraadt 396: if (err)
397: warnx("%s pid file: %s", err, pidfile);
398: else if (noaction)
1.44 mpech 399: (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30 millert 400: else if (kill(pid, signal))
401: warnx("warning - could not send SIG%s to daemon",
402: sys_signame[signal]);
1.26 millert 403: }
404:
1.25 millert 405: void
1.48 millert 406: parse_args(int argc, char **argv)
1.1 deraadt 407: {
1.48 millert 408: int ch;
409: char *p;
1.1 deraadt 410:
1.35 deraadt 411: timenow = time(NULL);
412: daytime = ctime(&timenow) + 4;
413: daytime[15] = '\0';
1.1 deraadt 414:
1.35 deraadt 415: /* Let's get our hostname */
416: (void)gethostname(hostname, sizeof(hostname));
1.1 deraadt 417:
418: /* Truncate domain */
1.48 millert 419: if ((p = strchr(hostname, '.')) != NULL)
1.1 deraadt 420: *p = '\0';
421:
1.52 millert 422: while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48 millert 423: switch (ch) {
1.51 millert 424: case 'a':
425: arcdir = optarg;
426: break;
1.35 deraadt 427: case 'n':
428: noaction++; /* This implies needroot as off */
429: /* fall through */
430: case 'r':
431: needroot = 0;
432: break;
433: case 'v':
434: verbose++;
435: break;
436: case 'f':
437: conf = optarg;
438: break;
1.10 downsj 439: case 'm':
1.33 millert 440: monitormode++;
1.10 downsj 441: break;
1.52 millert 442: case 'F':
443: force++;
444: break;
1.35 deraadt 445: default:
446: usage();
447: }
448: }
1.52 millert 449: if (monitormode && force)
450: errx(1, "cannot specify both -m and -F flags");
1.9 downsj 451: }
1.1 deraadt 452:
1.25 millert 453: void
1.48 millert 454: usage(void)
1.1 deraadt 455: {
1.25 millert 456: extern const char *__progname;
457:
1.52 millert 458: (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53 millert 459: "[-f config_file] [log ...]\n", __progname);
1.14 millert 460: exit(1);
1.1 deraadt 461: }
462:
1.52 millert 463: /*
464: * Parse a configuration file and return a linked list of all the logs
1.1 deraadt 465: * to process
466: */
1.25 millert 467: struct conf_entry *
1.48 millert 468: parse_file(int *nentries)
1.1 deraadt 469: {
1.48 millert 470: FILE *f;
1.63 millert 471: char line[BUFSIZ], *parse, *q, *errline, *group, *tmp, *ep;
1.60 millert 472: int lineno;
1.63 millert 473: unsigned long ul;
1.35 deraadt 474: struct conf_entry *first = NULL;
475: struct conf_entry *working = NULL;
1.48 millert 476: struct passwd *pwd;
1.35 deraadt 477: struct group *grp;
1.51 millert 478: struct stat sb;
1.1 deraadt 479:
1.35 deraadt 480: if (strcmp(conf, "-") == 0)
481: f = stdin;
1.48 millert 482: else if ((f = fopen(conf, "r")) == NULL)
483: err(1, "can't open %s", conf);
1.11 downsj 484:
1.28 millert 485: *nentries = 0;
1.60 millert 486: for (lineno = 0; fgets(line, sizeof(line), f); lineno++) {
1.48 millert 487: tmp = sob(line);
1.54 millert 488: if (*tmp == '\0' || *tmp == '#')
1.35 deraadt 489: continue;
1.48 millert 490: errline = strdup(tmp);
1.11 downsj 491: if (errline == NULL)
492: err(1, "strdup");
1.28 millert 493: (*nentries)++;
1.35 deraadt 494: if (!first) {
495: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 496: if (working == NULL)
497: err(1, "malloc");
1.35 deraadt 498: first = working;
499: } else {
500: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 501: if (working->next == NULL)
502: err(1, "malloc");
1.35 deraadt 503: working = working->next;
504: }
1.1 deraadt 505:
1.60 millert 506: q = parse = missing_field(sob(line), errline, lineno);
1.35 deraadt 507: *(parse = son(line)) = '\0';
508: working->log = strdup(q);
1.11 downsj 509: if (working->log == NULL)
510: err(1, "strdup");
1.1 deraadt 511:
1.51 millert 512: if ((working->logbase = strrchr(working->log, '/')) != NULL)
513: working->logbase++;
514:
1.60 millert 515: q = parse = missing_field(sob(++parse), errline, lineno);
1.35 deraadt 516: *(parse = son(parse)) = '\0';
1.63 millert 517: if ((group = strchr(q, ':')) != NULL ||
518: (group = strrchr(q, '.')) != NULL) {
1.25 millert 519: *group++ = '\0';
520: if (*q) {
521: if (!(isnumberstr(q))) {
1.48 millert 522: if ((pwd = getpwnam(q)) == NULL)
1.60 millert 523: errx(1, "%s:%d: unknown user: %s",
524: conf, lineno, q);
1.48 millert 525: working->uid = pwd->pw_uid;
1.25 millert 526: } else
527: working->uid = atoi(q);
528: } else
1.50 millert 529: working->uid = (uid_t)-1;
1.25 millert 530:
531: q = group;
532: if (*q) {
533: if (!(isnumberstr(q))) {
534: if ((grp = getgrnam(q)) == NULL)
1.60 millert 535:
536: errx(1, "%s:%d: unknown group: %s",
537: conf, lineno, q);
1.25 millert 538: working->gid = grp->gr_gid;
539: } else
540: working->gid = atoi(q);
541: } else
1.50 millert 542: working->gid = (gid_t)-1;
1.25 millert 543:
1.60 millert 544: q = parse = missing_field(sob(++parse), errline, lineno);
1.25 millert 545: *(parse = son(parse)) = '\0';
1.50 millert 546: } else {
547: working->uid = (uid_t)-1;
548: working->gid = (gid_t)-1;
549: }
1.1 deraadt 550:
1.35 deraadt 551: if (!sscanf(q, "%o", &working->permissions))
1.60 millert 552: errx(1, "%s:%d: bad permissions: %s", conf, lineno, q);
1.1 deraadt 553:
1.60 millert 554: q = parse = missing_field(sob(++parse), errline, lineno);
1.35 deraadt 555: *(parse = son(parse)) = '\0';
556: if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.60 millert 557: errx(1, "%s:%d: bad number: %s", conf, lineno, q);
1.1 deraadt 558:
1.60 millert 559: q = parse = missing_field(sob(++parse), errline, lineno);
1.35 deraadt 560: *(parse = son(parse)) = '\0';
561: if (isdigit(*q))
1.58 millert 562: working->size = atoi(q) * 1024;
1.35 deraadt 563: else
564: working->size = -1;
565:
1.63 millert 566: working->flags = 0;
1.60 millert 567: q = parse = missing_field(sob(++parse), errline, lineno);
1.35 deraadt 568: *(parse = son(parse)) = '\0';
1.63 millert 569: ul = strtoul(q, &ep, 10);
570: if (ul > INT_MAX)
571: errx(1, "%s:%d: interval out of range: %s", conf,
572: lineno, q);
573: working->hours = (int)ul;
574: switch (*ep) {
575: case '\0':
576: break;
577: case '@':
578: working->trim_at = parse8601(ep + 1);
579: if (working->trim_at == (time_t) - 1)
580: errx(1, "%s:%d: bad time: %s", conf, lineno, q);
581: working->flags |= CE_TRIMAT;
582: break;
583: case '$':
584: working->trim_at = parseDWM(ep + 1);
585: if (working->trim_at == (time_t) - 1)
586: errx(1, "%s:%d: bad time: %s", conf, lineno, q);
587: working->flags |= CE_TRIMAT;
588: break;
589: case '*':
590: if (q == ep)
591: break;
592: /* FALLTHROUGH */
593: default:
594: errx(1, "%s:%d: bad interval/at: %s", conf, lineno, q);
595: break;
596: }
1.1 deraadt 597:
1.35 deraadt 598: q = sob(++parse); /* Optional field */
1.32 millert 599: if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
600: *q == 'M' || *q == 'm') {
601: *(parse = son(q)) = '\0';
602: while (*q) {
603: switch (*q) {
604: case 'Z':
605: case 'z':
606: working->flags |= CE_COMPACT;
607: break;
608: case 'B':
609: case 'b':
610: working->flags |= CE_BINARY;
611: break;
612: case 'M':
613: case 'm':
614: working->flags |= CE_MONITOR;
1.45 wcobb 615: break;
616: case 'F':
617: case 'f':
618: working->flags |= CE_FOLLOW;
1.32 millert 619: break;
620: default:
1.60 millert 621: errx(1, "%s:%d: illegal flag: `%c'",
622: conf, lineno, *q);
1.32 millert 623: break;
624: }
625: q++;
626: }
627: } else
1.40 deraadt 628: parse--; /* no flags so undo */
1.10 downsj 629:
1.14 millert 630: working->pidfile = PIDFILE;
1.30 millert 631: working->signal = SIGHUP;
632: working->runcmd = NULL;
1.61 millert 633: working->whom = NULL;
1.30 millert 634: for (;;) {
635: q = parse = sob(++parse); /* Optional field */
636: if (q == NULL || *q == '\0')
637: break;
638: if (*q == '/') {
639: *(parse = son(parse)) = '\0';
640: if (strlen(q) >= MAXPATHLEN)
1.60 millert 641: errx(1, "%s:%d: pathname too long: %s",
642: conf, lineno, q);
1.30 millert 643: working->pidfile = strdup(q);
644: if (working->pidfile == NULL)
645: err(1, "strdup");
646: } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
647: *(parse = tmp) = '\0';
1.56 millert 648: if (*++q != '\0') {
649: working->runcmd = strdup(q);
650: if (working->runcmd == NULL)
651: err(1, "strdup");
652: }
1.55 millert 653: working->pidfile = NULL;
654: working->signal = -1;
1.30 millert 655: } else if (strncmp(q, "SIG", 3) == 0) {
656: int i;
657:
658: *(parse = son(parse)) = '\0';
659: for (i = 1; i < NSIG; i++) {
660: if (!strcmp(sys_signame[i], q + 3)) {
661: working->signal = i;
662: break;
663: }
664: }
665: if (i == NSIG)
1.60 millert 666: errx(1, "%s:%d: unknown signal: %s",
667: conf, lineno, q);
1.61 millert 668: } else if (working->flags & CE_MONITOR) {
669: *(parse = son(parse)) = '\0';
670: working->whom = strdup(q);
671: if (working->whom == NULL)
672: err(1, "strdup");
1.30 millert 673: } else
1.60 millert 674: errx(1, "%s:%d: unrecognized field: %s",
675: conf, lineno, q);
1.14 millert 676: }
1.51 millert 677: free(errline);
678:
1.61 millert 679: if ((working->flags & CE_MONITOR) && working->whom == NULL)
680: errx(1, "%s:%d: missing monitor notification field",
681: conf, lineno);
682:
1.51 millert 683: /* If there is an arcdir, set working->backdir. */
684: if (arcdir != NULL && working->logbase != NULL) {
685: if (*arcdir == '/') {
686: /* Fully qualified arcdir */
687: working->backdir = arcdir;
688: } else {
689: /* arcdir is relative to log's parent dir */
690: *(working->logbase - 1) = '\0';
691: if ((asprintf(&working->backdir, "%s/%s",
692: working->log, arcdir)) == -1)
693: err(1, "malloc");
694: *(working->logbase - 1) = '/';
695: }
696: /* Ignore arcdir if it doesn't exist. */
697: if (stat(working->backdir, &sb) != 0 ||
698: !S_ISDIR(sb.st_mode)) {
699: if (working->backdir != arcdir)
700: free(working->backdir);
701: working->backdir = NULL;
702: }
703: } else
704: working->backdir = NULL;
1.25 millert 705:
706: /* Make sure we can't oflow MAXPATHLEN */
1.51 millert 707: if (working->backdir != NULL) {
708: if (snprintf(line, sizeof(line), "%s/%s.%d%s",
709: working->backdir, working->logbase,
710: working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
1.60 millert 711: errx(1, "%s:%d: pathname too long: %s",
712: conf, lineno, q);
1.51 millert 713: } else {
714: if (snprintf(line, sizeof(line), "%s.%d%s",
715: working->log, working->numlogs, COMPRESS_POSTFIX)
716: >= MAXPATHLEN)
1.60 millert 717: errx(1, "%s:%d: pathname too long: %s",
718: conf, lineno, working->log);
1.51 millert 719: }
1.35 deraadt 720: }
721: if (working)
722: working->next = NULL;
723: (void)fclose(f);
1.48 millert 724: return (first);
1.1 deraadt 725: }
726:
1.25 millert 727: char *
1.60 millert 728: missing_field(char *p, char *errline, int lineno)
1.1 deraadt 729: {
1.60 millert 730: if (p == NULL || *p == '\0') {
731: warnx("%s:%d: missing field", conf, lineno);
1.35 deraadt 732: fputs(errline, stderr);
733: exit(1);
734: }
1.48 millert 735: return (p);
1.1 deraadt 736: }
737:
1.25 millert 738: void
1.51 millert 739: dotrim(struct conf_entry *ent)
1.35 deraadt 740: {
741: char file1[MAXPATHLEN], file2[MAXPATHLEN];
1.57 millert 742: char oldlog[MAXPATHLEN], *suffix;
1.35 deraadt 743: int fd;
1.51 millert 744: int numdays = ent->numlogs;
745:
746: /* Is there a separate backup dir? */
747: if (ent->backdir != NULL)
748: snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
749: ent->logbase);
750: else
751: strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1 deraadt 752:
1.35 deraadt 753: /* Remove oldest log (may not exist) */
1.51 millert 754: (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57 millert 755: (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog, numdays,
1.35 deraadt 756: COMPRESS_POSTFIX);
757:
758: if (noaction) {
1.57 millert 759: printf("\trm -f %s %s\n", file1, file2);
1.35 deraadt 760: } else {
761: (void)unlink(file1);
1.57 millert 762: (void)unlink(file2);
1.35 deraadt 763: }
764:
765: /* Move down log files */
766: while (numdays--) {
1.57 millert 767: /*
1.65 millert 768: * If both the compressed archive and the non-compressed archive
769: * exist, we decide which to rotate based on the CE_COMPACT flag.
1.57 millert 770: */
1.51 millert 771: (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57 millert 772: suffix = lstat_log(file1, sizeof(file1), ent->flags);
773: if (suffix == NULL)
774: continue;
775: (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog,
776: numdays + 1, suffix);
777:
1.35 deraadt 778: if (noaction) {
1.57 millert 779: printf("\tmv %s %s\n", file1, file2);
780: printf("\tchmod %o %s\n", ent->permissions, file2);
1.51 millert 781: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50 millert 782: printf("\tchown %u:%u %s\n",
1.57 millert 783: ent->uid, ent->gid, file2);
1.35 deraadt 784: } else {
1.57 millert 785: if (rename(file1, file2))
786: warn("can't mv %s to %s", file1, file2);
787: if (chmod(file2, ent->permissions))
788: warn("can't chmod %s", file2);
1.51 millert 789: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.57 millert 790: if (chown(file2, ent->uid, ent->gid))
791: warn("can't chown %s", file2);
1.35 deraadt 792: }
793: }
1.51 millert 794: if (!noaction && !(ent->flags & CE_BINARY))
795: (void)log_trim(ent->log); /* Report the trimming to the old log */
1.1 deraadt 796:
1.51 millert 797: (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35 deraadt 798: if (noaction) {
1.50 millert 799: printf("\tmktemp %s\n", file2);
1.35 deraadt 800: } else {
801: if ((fd = mkstemp(file2)) < 0)
1.26 millert 802: err(1, "can't start '%s' log", file2);
1.51 millert 803: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
804: if (fchown(fd, ent->uid, ent->gid))
805: err(1, "can't chown '%s' log file", file2);
806: if (fchmod(fd, ent->permissions))
1.26 millert 807: err(1, "can't chmod '%s' log file", file2);
1.35 deraadt 808: (void)close(fd);
1.26 millert 809: /* Add status message */
1.51 millert 810: if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26 millert 811: err(1, "can't add status message to log '%s'", file2);
1.35 deraadt 812: }
1.26 millert 813:
1.51 millert 814: if (ent->numlogs == 0) {
1.5 deraadt 815: if (noaction)
1.51 millert 816: printf("\trm %s\n", ent->log);
817: else if (unlink(ent->log))
818: warn("can't rm %s", ent->log);
1.5 deraadt 819: } else {
1.57 millert 820: (void)snprintf(file1, sizeof(file1), "%s.0", oldlog);
1.51 millert 821: if (noaction)
822: printf("\tmv %s to %s\n", ent->log, file1);
823: else if (rename(ent->log, file1))
1.59 millert 824: warn("can't mv %s to %s", ent->log, file1);
1.5 deraadt 825: }
826:
1.26 millert 827: /* Now move the new log file into place */
828: if (noaction)
1.51 millert 829: printf("\tmv %s to %s\n", file2, ent->log);
830: else if (rename(file2, ent->log))
1.59 millert 831: warn("can't mv %s to %s", file2, ent->log);
1.1 deraadt 832: }
833:
834: /* Log the fact that the logs were turned over */
1.25 millert 835: int
1.48 millert 836: log_trim(char *log)
1.1 deraadt 837: {
1.35 deraadt 838: FILE *f;
1.25 millert 839:
1.35 deraadt 840: if ((f = fopen(log, "a")) == NULL)
1.48 millert 841: return (-1);
1.44 mpech 842: (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
843: daytime, hostname, (long)getpid());
1.35 deraadt 844: if (fclose(f) == EOF)
845: err(1, "log_trim: fclose");
1.48 millert 846: return (0);
1.1 deraadt 847: }
848:
1.16 millert 849: /* Fork off compress or gzip to compress the old log file */
1.25 millert 850: void
1.51 millert 851: compress_log(struct conf_entry *ent)
1.1 deraadt 852: {
1.48 millert 853: pid_t pid;
854: char *base, tmp[MAXPATHLEN];
1.51 millert 855:
856: if (ent->backdir != NULL)
857: snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
858: ent->logbase);
859: else
860: snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
861:
1.27 millert 862: if ((base = strrchr(COMPRESS, '/')) == NULL)
863: base = COMPRESS;
864: else
865: base++;
866: if (noaction) {
1.51 millert 867: printf("%s %s\n", base, tmp);
1.27 millert 868: return;
869: }
1.35 deraadt 870: pid = fork();
871: if (pid < 0) {
1.11 downsj 872: err(1, "fork");
1.50 millert 873: } else if (pid == 0) {
1.37 deraadt 874: (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16 millert 875: warn(COMPRESS);
876: _exit(1);
1.35 deraadt 877: }
1.1 deraadt 878: }
879:
880: /* Return size in kilobytes of a file */
1.58 millert 881: off_t
1.48 millert 882: sizefile(char *file)
1.1 deraadt 883: {
1.35 deraadt 884: struct stat sb;
1.1 deraadt 885:
1.35 deraadt 886: if (stat(file, &sb) < 0)
1.48 millert 887: return (-1);
1.58 millert 888:
889: /* For sparse files, return the size based on number of blocks used. */
890: if (sb.st_size / DEV_BSIZE > sb.st_blocks)
891: return (sb.st_blocks * DEV_BSIZE);
892: else
893: return (sb.st_size);
1.1 deraadt 894: }
895:
1.25 millert 896: /* Return the age (in hours) of old log file (file.0), or -1 if none */
897: int
1.51 millert 898: age_old_log(struct conf_entry *ent)
1.1 deraadt 899: {
1.35 deraadt 900: struct stat sb;
1.57 millert 901: char file[MAXPATHLEN];
1.1 deraadt 902:
1.51 millert 903: if (ent->backdir != NULL)
1.57 millert 904: (void)snprintf(file, sizeof(file), "%s/%s.0", ent->backdir,
905: ent->logbase);
906: else
907: (void)snprintf(file, sizeof(file), "%s.0", ent->log);
908: if (ent->flags & CE_COMPACT) {
909: if (stat_suffix(file, sizeof(file), COMPRESS_POSTFIX, &sb,
910: stat) < 0 && stat(file, &sb) < 0)
911: return (-1);
912: } else {
913: if (stat(file, &sb) < 0 && stat_suffix(file, sizeof(file),
914: COMPRESS_POSTFIX, &sb, stat) < 0)
915: return (-1);
916: }
1.48 millert 917: return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1 deraadt 918: }
919:
920: /* Skip Over Blanks */
1.25 millert 921: char *
1.48 millert 922: sob(char *p)
1.1 deraadt 923: {
1.35 deraadt 924: while (p && *p && isspace(*p))
925: p++;
1.48 millert 926: return (p);
1.1 deraadt 927: }
928:
929: /* Skip Over Non-Blanks */
1.25 millert 930: char *
1.48 millert 931: son(char *p)
1.1 deraadt 932: {
1.35 deraadt 933: while (p && *p && !isspace(*p))
934: p++;
1.48 millert 935: return (p);
1.1 deraadt 936: }
937:
938: /* Check if string is actually a number */
1.25 millert 939: int
1.48 millert 940: isnumberstr(char *string)
1.1 deraadt 941: {
1.35 deraadt 942: while (*string) {
943: if (!isdigit(*string++))
1.48 millert 944: return (0);
1.35 deraadt 945: }
1.48 millert 946: return (1);
1.10 downsj 947: }
948:
1.61 millert 949: int
950: domonitor(struct conf_entry *ent)
1.10 downsj 951: {
952: struct stat sb, tsb;
1.35 deraadt 953: char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10 downsj 954: FILE *fp;
955: off_t osize;
956: int rd;
957:
1.61 millert 958: if (stat(ent->log, &sb) < 0)
959: return (0);
960:
961: if (noaction) {
962: if (!verbose)
963: printf("%s: monitored\n", ent->log);
964: return (1);
965: }
1.10 downsj 966:
1.61 millert 967: flog = strdup(ent->log);
1.11 downsj 968: if (flog == NULL)
969: err(1, "strdup");
970:
1.10 downsj 971: for (p = flog; *p != '\0'; p++) {
972: if (*p == '/')
973: *p = '_';
974: }
1.48 millert 975: snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35 deraadt 976: STATS_DIR, flog);
1.10 downsj 977:
978: /* ..if it doesn't exist, simply record the current size. */
979: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
980: goto update;
981:
982: fp = fopen(fname, "r");
983: if (fp == NULL) {
1.34 millert 984: warn("%s", fname);
1.10 downsj 985: goto cleanup;
986: }
987: #ifdef QUAD_OFF_T
988: if (fscanf(fp, "%qd\n", &osize) != 1) {
989: #else
990: if (fscanf(fp, "%ld\n", &osize) != 1) {
991: #endif /* QUAD_OFF_T */
992: fclose(fp);
993: goto update;
994: }
995:
996: fclose(fp);
997:
998: /* If the file is smaller, mark the entire thing as changed. */
999: if (sb.st_size < osize)
1000: osize = 0;
1001:
1002: /* Now see if current size is larger. */
1003: if (sb.st_size > osize) {
1004: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 1005: if (rb == NULL)
1006: err(1, "malloc");
1.10 downsj 1007:
1008: /* Open logfile, seek. */
1.61 millert 1009: fp = fopen(ent->log, "r");
1.10 downsj 1010: if (fp == NULL) {
1.61 millert 1011: warn("%s", ent->log);
1.10 downsj 1012: goto cleanup;
1013: }
1014: fseek(fp, osize, SEEK_SET);
1015: rd = fread(rb, 1, sb.st_size - osize, fp);
1016: if (rd < 1) {
1.11 downsj 1017: warn("fread");
1.10 downsj 1018: fclose(fp);
1019: goto cleanup;
1020: }
1021:
1022: /* Send message. */
1023: fclose(fp);
1024:
1025: fp = openmail();
1026: if (fp == NULL) {
1.11 downsj 1027: warn("openmail");
1.10 downsj 1028: goto cleanup;
1029: }
1030: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
1.61 millert 1031: ent->whom, ent->log);
1.10 downsj 1032: fwrite(rb, 1, rd, fp);
1033: fputs("\n\n", fp);
1034:
1.48 millert 1035: pclose(fp);
1.10 downsj 1036: }
1037: update:
1038: /* Reopen for writing and update file. */
1039: fp = fopen(fname, "w");
1040: if (fp == NULL) {
1.34 millert 1041: warn("%s", fname);
1.10 downsj 1042: goto cleanup;
1043: }
1044: #ifdef QUAD_OFF_T
1045: fprintf(fp, "%qd\n", sb.st_size);
1046: #else
1047: fprintf(fp, "%ld\n", sb.st_size);
1048: #endif /* QUAD_OFF_T */
1049: fclose(fp);
1050:
1051: cleanup:
1052: free(flog);
1053: if (rb != NULL)
1054: free(rb);
1.61 millert 1055: return (1);
1.10 downsj 1056: }
1057:
1.25 millert 1058: FILE *
1.48 millert 1059: openmail(void)
1.10 downsj 1060: {
1.48 millert 1061: FILE *ret;
1.35 deraadt 1062: char *cmdbuf = NULL;
1.10 downsj 1063:
1.64 pvalchev 1064: if (asprintf(&cmdbuf, "%s -t", SENDMAIL) != -1) {
1.35 deraadt 1065: ret = popen(cmdbuf, "w");
1066: free(cmdbuf);
1067: return (ret);
1068: }
1069: return (NULL);
1.10 downsj 1070: }
1071:
1.25 millert 1072: void
1.48 millert 1073: child_killer(int signo)
1.16 millert 1074: {
1.38 deraadt 1075: int save_errno = errno;
1.16 millert 1076: int status;
1077:
1078: while (waitpid(-1, &status, WNOHANG) > 0)
1079: ;
1.38 deraadt 1080: errno = save_errno;
1.57 millert 1081: }
1082:
1083: int
1084: stat_suffix(char *file, size_t size, char *suffix, struct stat *sp, int (*func)())
1085: {
1086: size_t n;
1087:
1088: n = strlcat(file, suffix, size);
1089: if (n < size && func(file, sp) == 0)
1090: return (0);
1091: file[n - strlen(suffix)] = '\0';
1092: return (-1);
1093: }
1094:
1095: /*
1.65 millert 1096: * lstat() a log, possibly appending a suffix; order is based on flags.
1.57 millert 1097: * Returns the suffix appended (may be empty string) or NULL if no file.
1098: */
1099: char *
1100: lstat_log(char *file, size_t size, int flags)
1101: {
1102: struct stat sb;
1103:
1104: if (flags & CE_COMPACT) {
1105: if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
1106: return (COMPRESS_POSTFIX);
1107: if (lstat(file, &sb) == 0)
1108: return ("");
1109: } else {
1110: if (lstat(file, &sb) == 0)
1111: return ("");
1112: if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
1113: return (COMPRESS_POSTFIX);
1114:
1115: }
1116: return (NULL);
1.63 millert 1117: }
1118:
1119: /*
1120: * Parse a limited subset of ISO 8601. The specific format is as follows:
1121: *
1122: * [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter)
1123: *
1124: * We don't accept a timezone specification; missing fields (including timezone)
1125: * are defaulted to the current date but time zero.
1126: */
1127: time_t
1128: parse8601(char *s)
1129: {
1130: char *t;
1131: struct tm tm, *tmp;
1132: unsigned long ul;
1133:
1134: tmp = localtime(&timenow);
1135: tm = *tmp;
1136:
1137: tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1138:
1139: ul = strtoul(s, &t, 10);
1140: if (*t != '\0' && *t != 'T')
1141: return (-1);
1142:
1143: /*
1144: * Now t points either to the end of the string (if no time was
1145: * provided) or to the letter `T' which separates date and time in
1146: * ISO 8601. The pointer arithmetic is the same for either case.
1147: */
1148: switch (t - s) {
1149: case 8:
1150: tm.tm_year = ((ul / 1000000) - 19) * 100;
1151: ul = ul % 1000000;
1152: case 6:
1153: tm.tm_year -= tm.tm_year % 100;
1154: tm.tm_year += ul / 10000;
1155: ul = ul % 10000;
1156: case 4:
1157: tm.tm_mon = (ul / 100) - 1;
1158: ul = ul % 100;
1159: case 2:
1160: tm.tm_mday = ul;
1161: case 0:
1162: break;
1163: default:
1164: return (-1);
1165: }
1166:
1167: /* sanity check */
1168: if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
1169: || tm.tm_mday < 1 || tm.tm_mday > 31)
1170: return (-1);
1171:
1172: if (*t != '\0') {
1173: s = ++t;
1174: ul = strtoul(s, &t, 10);
1175: if (*t != '\0' && !isspace(*t))
1176: return (-1);
1177:
1178: switch (t - s) {
1179: case 6:
1180: tm.tm_sec = ul % 100;
1181: ul /= 100;
1182: case 4:
1183: tm.tm_min = ul % 100;
1184: ul /= 100;
1185: case 2:
1186: tm.tm_hour = ul;
1187: case 0:
1188: break;
1189: default:
1190: return (-1);
1191: }
1192:
1193: /* sanity check */
1194: if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0
1195: || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
1196: return (-1);
1197: }
1198: return (mktime(&tm));
1199: }
1200:
1201: /*-
1202: * Parse a cyclic time specification, the format is as follows:
1203: *
1204: * [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
1205: *
1206: * to rotate a logfile cyclic at
1207: *
1208: * - every day (D) within a specific hour (hh) (hh = 0...23)
1209: * - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday)
1210: * - once a month (M) at a specific day (d) (d = 1..31,l|L)
1211: *
1212: * We don't accept a timezone specification; missing fields
1213: * are defaulted to the current date but time zero.
1214: */
1215: time_t
1216: parseDWM(char *s)
1217: {
1218: char *t;
1219: struct tm tm, *tmp;
1220: long l;
1221: int nd;
1222: static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
1223: int WMseen = 0;
1224: int Dseen = 0;
1225:
1226: tmp = localtime(&timenow);
1227: tm = *tmp;
1228:
1229: /* set no. of days per month */
1230:
1231: nd = mtab[tm.tm_mon];
1232:
1233: if (tm.tm_mon == 1) {
1234: if (((tm.tm_year + 1900) % 4 == 0) &&
1235: ((tm.tm_year + 1900) % 100 != 0) &&
1236: ((tm.tm_year + 1900) % 400 == 0)) {
1237: nd++; /* leap year, 29 days in february */
1238: }
1239: }
1240: tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
1241:
1242: for (;;) {
1243: switch (*s) {
1244: case 'D':
1245: if (Dseen)
1246: return (-1);
1247: Dseen++;
1248: s++;
1249: l = strtol(s, &t, 10);
1250: if (l < 0 || l > 23)
1251: return (-1);
1252: tm.tm_hour = l;
1253: break;
1254:
1255: case 'W':
1256: if (WMseen)
1257: return (-1);
1258: WMseen++;
1259: s++;
1260: l = strtol(s, &t, 10);
1261: if (l < 0 || l > 6)
1262: return (-1);
1263: if (l != tm.tm_wday) {
1264: int save;
1265:
1266: if (l < tm.tm_wday) {
1267: save = 6 - tm.tm_wday;
1268: save += (l + 1);
1269: } else {
1270: save = l - tm.tm_wday;
1271: }
1272:
1273: tm.tm_mday += save;
1274:
1275: if (tm.tm_mday > nd) {
1276: tm.tm_mon++;
1277: tm.tm_mday = tm.tm_mday - nd;
1278: }
1279: }
1280: break;
1281:
1282: case 'M':
1283: if (WMseen)
1284: return (-1);
1285: WMseen++;
1286: s++;
1287: if (tolower(*s) == 'l') {
1288: tm.tm_mday = nd;
1289: s++;
1290: t = s;
1291: } else {
1292: l = strtol(s, &t, 10);
1293: if (l < 1 || l > 31)
1294: return (-1);
1295:
1296: if (l > nd)
1297: return (-1);
1298: tm.tm_mday = l;
1299: }
1300: break;
1301:
1302: default:
1303: return (-1);
1304: break;
1305: }
1306:
1307: if (*t == '\0' || isspace(*t))
1308: break;
1309: else
1310: s = t;
1311: }
1312: return (mktime(&tm));
1.1 deraadt 1313: }