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