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