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