Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.54
1.54 ! millert 1: /* $OpenBSD: newsyslog.c,v 1.53 2002/09/17 20:44:38 millert Exp $ */
1.10 downsj 2:
3: /*
1.48 millert 4: * Copyright (c) 1999, 2002 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.54 ! millert 89: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.53 2002/09/17 20:44:38 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.35 deraadt 135:
1.49 millert 136: #define MIN_PID 4 /* Don't touch pids lower than this */
137: #define MIN_SIZE 512 /* Don't rotate if smaller than this */
138:
1.50 millert 139: #define DPRINTF(x) do { if (verbose) printf x ; } while (0)
140:
1.1 deraadt 141: struct conf_entry {
1.35 deraadt 142: char *log; /* Name of the log */
1.51 millert 143: char *logbase; /* Basename of the log */
144: char *backdir; /* Directory in which to store backups */
1.35 deraadt 145: uid_t uid; /* Owner of log */
146: gid_t gid; /* Group of log */
147: int numlogs; /* Number of logs to keep */
148: int size; /* Size cutoff to trigger trimming the log */
149: int hours; /* Hours between log trimming */
150: int permissions; /* File permissions on the log */
1.30 millert 151: int signal; /* Signal to send (defaults to SIGHUP) */
1.35 deraadt 152: int flags; /* Flags (CE_COMPACT & CE_BINARY) */
1.10 downsj 153: char *whom; /* Whom to notify if logfile changes */
1.30 millert 154: char *pidfile; /* Path to file containg pid to signal */
155: char *runcmd; /* Command to run instead of sending a signal */
1.35 deraadt 156: struct conf_entry *next; /* Linked list pointer */
1.1 deraadt 157: };
158:
1.30 millert 159: struct pidinfo {
160: char *file;
161: int signal;
162: };
163:
1.52 millert 164: int verbose = 0; /* Print out what's going on */
165: int needroot = 1; /* Root privs are necessary */
166: int noaction = 0; /* Don't do anything, just show it */
1.33 millert 167: int monitormode = 0; /* Don't do monitoring by default */
1.52 millert 168: int force = 0; /* Force the logs to be rotated */
169: char *conf = CONF; /* Configuration file to use */
170: time_t timenow;
171: char hostname[MAXHOSTNAMELEN]; /* hostname */
172: char *daytime; /* timenow in human readable form */
173: char *arcdir; /* dir to put archives in (if it exists) */
1.1 deraadt 174:
1.43 millert 175: void do_entry(struct conf_entry *);
1.48 millert 176: void parse_args(int, char **);
1.43 millert 177: void usage(void);
178: struct conf_entry *parse_file(int *);
179: char *missing_field(char *, char *);
1.51 millert 180: void dotrim(struct conf_entry *);
1.43 millert 181: int log_trim(char *);
1.51 millert 182: void compress_log(struct conf_entry *);
1.43 millert 183: int sizefile(char *);
1.51 millert 184: int age_old_log(struct conf_entry *);
1.43 millert 185: char *sob(char *);
186: char *son(char *);
187: int isnumberstr(char *);
188: void domonitor(char *, char *);
189: FILE *openmail(void);
190: void child_killer(int);
191: void run_command(char *);
192: void send_signal(char *, int);
1.1 deraadt 193:
1.25 millert 194: int
1.48 millert 195: main(int argc, char **argv)
1.1 deraadt 196: {
1.53 millert 197: struct conf_entry *p, *q, *x, *y;
1.30 millert 198: struct pidinfo *pidlist, *pl;
1.53 millert 199: char **av;
1.28 millert 200: int status, listlen;
1.35 deraadt 201:
1.48 millert 202: parse_args(argc, argv);
1.53 millert 203: argc -= optind;
204: argv += optind;
205:
1.35 deraadt 206: if (needroot && getuid() && geteuid())
1.11 downsj 207: errx(1, "You must be root.");
1.53 millert 208:
209: p = parse_file(&listlen);
210: if (argc > 0) {
211: /* Only rotate specified files. */
212: x = y = NULL;
213: listlen = 0;
214: for (av = argv; *av; av++) {
215: for (q = p; q; q = q->next)
216: if (strcmp(*av, q->log) == 0) {
217: if (x == NULL)
218: x = y = q;
219: else {
220: y->next = q;
221: y = q;
222: }
223: listlen++;
224: break;
225: }
226: if (q == NULL)
227: warnx("%s is not listed in %s", *av, conf);
228: }
229: if (x == NULL)
230: errx(1, "no specified log files found in %s", conf);
231: y->next = NULL;
232: p = x;
233: }
1.26 millert 234:
1.46 aaron 235: pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30 millert 236: if (pidlist == NULL)
1.28 millert 237: err(1, "calloc");
238:
1.53 millert 239: signal(SIGCHLD, child_killer);
240:
1.26 millert 241: /* Step 1, rotate all log files */
1.53 millert 242: for (q = p; q; q = q->next)
1.35 deraadt 243: do_entry(q);
1.26 millert 244:
1.28 millert 245: /* Step 2, make a list of unique pid files */
1.30 millert 246: for (q = p, pl = pidlist; q; ) {
1.28 millert 247: if (q->flags & CE_ROTATED) {
1.30 millert 248: struct pidinfo *pltmp;
1.28 millert 249:
1.30 millert 250: for (pltmp = pidlist; pltmp < pl; pltmp++) {
251: if ((strcmp(pltmp->file, q->pidfile) == 0 &&
252: pltmp->signal == q->signal) || (q->runcmd &&
253: strcmp(q->runcmd, pltmp->file) == 0))
1.28 millert 254: break;
1.30 millert 255: }
256: if (pltmp == pl) { /* unique entry */
257: if (q->runcmd) {
258: pl->file = q->runcmd;
259: pl->signal = -1;
260: } else {
261: pl->file = q->pidfile;
262: pl->signal = q->signal;
263: }
1.31 millert 264: pl++;
1.30 millert 265: }
1.28 millert 266: }
1.35 deraadt 267: q = q->next;
268: }
1.26 millert 269:
1.30 millert 270: /* Step 3, send a signal or run a command */
271: for (pl = pidlist; pl->file; pl++) {
272: if (pl->signal == -1)
273: run_command(pl->file);
274: else
275: send_signal(pl->file, pl->signal);
276: }
1.28 millert 277: if (!noaction)
278: sleep(5);
279:
280: /* Step 4, compress the log.0 file if configured to do so and free */
1.35 deraadt 281: while (p) {
1.26 millert 282: if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51 millert 283: compress_log(p);
1.26 millert 284: q = p;
1.35 deraadt 285: p = p->next;
286: free(q);
287: }
1.16 millert 288:
289: /* Wait for children to finish, then exit */
290: while (waitpid(-1, &status, 0) != -1)
291: ;
1.35 deraadt 292: exit(0);
1.1 deraadt 293: }
294:
1.25 millert 295: void
1.48 millert 296: do_entry(struct conf_entry *ent)
1.1 deraadt 297: {
1.48 millert 298: int modtime, size;
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.50 millert 309: DPRINTF(("%s <%d%s%s%s>: ", ent->log, ent->numlogs,
1.51 millert 310: (ent->flags & CE_COMPACT) ? "Z" : "",
311: (ent->flags & CE_BINARY) ? "B" : "",
312: (ent->flags & CE_FOLLOW) ? "F" : ""));
1.50 millert 313:
1.35 deraadt 314: size = sizefile(ent->log);
1.51 millert 315: modtime = age_old_log(ent);
1.35 deraadt 316: if (size < 0) {
1.50 millert 317: DPRINTF(("does not exist.\n"));
1.35 deraadt 318: } else {
1.50 millert 319: if (ent->size > 0)
320: DPRINTF(("size (Kb): %d [%d] ", size, ent->size));
321: if (ent->hours > 0)
322: DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.33 millert 323: if (monitormode && ent->flags & CE_MONITOR)
1.10 downsj 324: domonitor(ent->log, ent->whom);
1.52 millert 325: if (!monitormode && (force ||
326: (ent->size > 0 && size >= ent->size) ||
1.49 millert 327: (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
328: && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50 millert 329: DPRINTF(("--> trimming log....\n"));
1.19 kstailey 330: if (noaction && !verbose)
1.50 millert 331: printf("%s <%d%s%s%s>\n", ent->log,
332: ent->numlogs,
333: (ent->flags & CE_COMPACT) ? "Z" : "",
334: (ent->flags & CE_BINARY) ? "B" : "",
335: (ent->flags & CE_FOLLOW) ? "F" : "");
1.51 millert 336: dotrim(ent);
1.26 millert 337: ent->flags |= CE_ROTATED;
1.50 millert 338: } else
339: DPRINTF(("--> skipping\n"));
1.35 deraadt 340: }
1.1 deraadt 341: }
342:
1.30 millert 343: /* Run the specified command */
344: void
1.48 millert 345: run_command(char *cmd)
1.30 millert 346: {
347: if (noaction)
1.50 millert 348: (void)printf("\trun %s\n", cmd);
1.30 millert 349: else
350: system(cmd);
351: }
352:
353: /* Send a signal to the pid specified by pidfile */
1.26 millert 354: void
1.48 millert 355: send_signal(char *pidfile, int signal)
1.26 millert 356: {
1.48 millert 357: pid_t pid;
358: FILE *f;
359: char line[BUFSIZ], *ep, *err;
1.40 deraadt 360: unsigned long ulval;
1.26 millert 361:
1.35 deraadt 362: if ((f = fopen(pidfile, "r")) == NULL) {
1.26 millert 363: warn("can't open %s", pidfile);
364: return;
365: }
366:
1.48 millert 367: pid = 0;
1.42 millert 368: errno = 0;
369: err = NULL;
1.40 deraadt 370: if (fgets(line, sizeof(line), f)) {
371: ulval = strtoul(line, &ep, 10);
1.41 marc 372: if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40 deraadt 373: err = "invalid number in";
374: else if (errno == ERANGE && ulval == ULONG_MAX)
375: err = "out of range number in";
376: else if (ulval == 0)
377: err = "no number in";
378: else if (ulval < MIN_PID)
379: err = "preposterous process number in";
380: else
381: pid = ulval;
1.42 millert 382: } else {
383: if (errno == 0)
384: err = "empty";
385: else
386: err = "error reading";
1.40 deraadt 387: }
1.26 millert 388: (void)fclose(f);
389:
1.40 deraadt 390: if (err)
391: warnx("%s pid file: %s", err, pidfile);
392: else if (noaction)
1.44 mpech 393: (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30 millert 394: else if (kill(pid, signal))
395: warnx("warning - could not send SIG%s to daemon",
396: sys_signame[signal]);
1.26 millert 397: }
398:
1.25 millert 399: void
1.48 millert 400: parse_args(int argc, char **argv)
1.1 deraadt 401: {
1.48 millert 402: int ch;
403: char *p;
1.1 deraadt 404:
1.35 deraadt 405: timenow = time(NULL);
406: daytime = ctime(&timenow) + 4;
407: daytime[15] = '\0';
1.1 deraadt 408:
1.35 deraadt 409: /* Let's get our hostname */
410: (void)gethostname(hostname, sizeof(hostname));
1.1 deraadt 411:
412: /* Truncate domain */
1.48 millert 413: if ((p = strchr(hostname, '.')) != NULL)
1.1 deraadt 414: *p = '\0';
415:
1.52 millert 416: while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48 millert 417: switch (ch) {
1.51 millert 418: case 'a':
419: arcdir = optarg;
420: break;
1.35 deraadt 421: case 'n':
422: noaction++; /* This implies needroot as off */
423: /* fall through */
424: case 'r':
425: needroot = 0;
426: break;
427: case 'v':
428: verbose++;
429: break;
430: case 'f':
431: conf = optarg;
432: break;
1.10 downsj 433: case 'm':
1.33 millert 434: monitormode++;
1.10 downsj 435: break;
1.52 millert 436: case 'F':
437: force++;
438: break;
1.35 deraadt 439: default:
440: usage();
441: }
442: }
1.52 millert 443: if (monitormode && force)
444: errx(1, "cannot specify both -m and -F flags");
1.9 downsj 445: }
1.1 deraadt 446:
1.25 millert 447: void
1.48 millert 448: usage(void)
1.1 deraadt 449: {
1.25 millert 450: extern const char *__progname;
451:
1.52 millert 452: (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53 millert 453: "[-f config_file] [log ...]\n", __progname);
1.14 millert 454: exit(1);
1.1 deraadt 455: }
456:
1.52 millert 457: /*
458: * Parse a configuration file and return a linked list of all the logs
1.1 deraadt 459: * to process
460: */
1.25 millert 461: struct conf_entry *
1.48 millert 462: parse_file(int *nentries)
1.1 deraadt 463: {
1.48 millert 464: FILE *f;
465: char line[BUFSIZ], *parse, *q, *errline, *group, *tmp;
1.35 deraadt 466: struct conf_entry *first = NULL;
467: struct conf_entry *working = NULL;
1.48 millert 468: struct passwd *pwd;
1.35 deraadt 469: struct group *grp;
1.51 millert 470: struct stat sb;
1.1 deraadt 471:
1.35 deraadt 472: if (strcmp(conf, "-") == 0)
473: f = stdin;
1.48 millert 474: else if ((f = fopen(conf, "r")) == NULL)
475: err(1, "can't open %s", conf);
1.11 downsj 476:
1.28 millert 477: *nentries = 0;
1.35 deraadt 478: while (fgets(line, sizeof(line), f)) {
1.48 millert 479: tmp = sob(line);
1.54 ! millert 480: if (*tmp == '\0' || *tmp == '#')
1.35 deraadt 481: continue;
1.48 millert 482: errline = strdup(tmp);
1.11 downsj 483: if (errline == NULL)
484: err(1, "strdup");
1.28 millert 485: (*nentries)++;
1.35 deraadt 486: if (!first) {
487: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 488: if (working == NULL)
489: err(1, "malloc");
1.35 deraadt 490: first = working;
491: } else {
492: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 493: if (working->next == NULL)
494: err(1, "malloc");
1.35 deraadt 495: working = working->next;
496: }
1.1 deraadt 497:
1.35 deraadt 498: q = parse = missing_field(sob(line), errline);
499: *(parse = son(line)) = '\0';
500: working->log = strdup(q);
1.11 downsj 501: if (working->log == NULL)
502: err(1, "strdup");
1.1 deraadt 503:
1.51 millert 504: if ((working->logbase = strrchr(working->log, '/')) != NULL)
505: working->logbase++;
506:
1.35 deraadt 507: q = parse = missing_field(sob(++parse), errline);
508: *(parse = son(parse)) = '\0';
1.25 millert 509: if ((group = strchr(q, '.')) != NULL) {
510: *group++ = '\0';
511: if (*q) {
512: if (!(isnumberstr(q))) {
1.48 millert 513: if ((pwd = getpwnam(q)) == NULL)
1.25 millert 514: errx(1, "Error in config file; unknown user: %s", q);
1.48 millert 515: working->uid = pwd->pw_uid;
1.25 millert 516: } else
517: working->uid = atoi(q);
518: } else
1.50 millert 519: working->uid = (uid_t)-1;
1.25 millert 520:
521: q = group;
522: if (*q) {
523: if (!(isnumberstr(q))) {
524: if ((grp = getgrnam(q)) == NULL)
525: errx(1, "Error in config file; unknown group: %s", q);
526: working->gid = grp->gr_gid;
527: } else
528: working->gid = atoi(q);
529: } else
1.50 millert 530: working->gid = (gid_t)-1;
1.25 millert 531:
532: q = parse = missing_field(sob(++parse), errline);
533: *(parse = son(parse)) = '\0';
1.50 millert 534: } else {
535: working->uid = (uid_t)-1;
536: working->gid = (gid_t)-1;
537: }
1.1 deraadt 538:
1.35 deraadt 539: if (!sscanf(q, "%o", &working->permissions))
1.11 downsj 540: errx(1, "Error in config file; bad permissions: %s", q);
1.1 deraadt 541:
1.35 deraadt 542: q = parse = missing_field(sob(++parse), errline);
543: *(parse = son(parse)) = '\0';
544: if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.11 downsj 545: errx(1, "Error in config file; bad number: %s", q);
1.1 deraadt 546:
1.35 deraadt 547: q = parse = missing_field(sob(++parse), errline);
548: *(parse = son(parse)) = '\0';
549: if (isdigit(*q))
550: working->size = atoi(q);
551: else
552: working->size = -1;
553:
554: q = parse = missing_field(sob(++parse), errline);
555: *(parse = son(parse)) = '\0';
556: if (isdigit(*q))
557: working->hours = atoi(q);
558: else
559: working->hours = -1;
1.1 deraadt 560:
1.35 deraadt 561: working->flags = 0;
562: q = sob(++parse); /* Optional field */
1.32 millert 563: if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
564: *q == 'M' || *q == 'm') {
565: *(parse = son(q)) = '\0';
566: while (*q) {
567: switch (*q) {
568: case 'Z':
569: case 'z':
570: working->flags |= CE_COMPACT;
571: break;
572: case 'B':
573: case 'b':
574: working->flags |= CE_BINARY;
575: break;
576: case 'M':
577: case 'm':
578: working->flags |= CE_MONITOR;
1.45 wcobb 579: break;
580: case 'F':
581: case 'f':
582: working->flags |= CE_FOLLOW;
1.32 millert 583: break;
584: default:
585: errx(1, "Illegal flag in config file: %c", *q);
586: break;
587: }
588: q++;
589: }
590: } else
1.40 deraadt 591: parse--; /* no flags so undo */
1.10 downsj 592:
593: working->whom = NULL;
594: if (working->flags & CE_MONITOR) { /* Optional field */
595: q = parse = sob(++parse);
596: *(parse = son(parse)) = '\0';
597:
598: working->whom = strdup(q);
1.11 downsj 599: if (working->log == NULL)
600: err(1, "strdup");
1.10 downsj 601: }
1.14 millert 602:
603: working->pidfile = PIDFILE;
1.30 millert 604: working->signal = SIGHUP;
605: working->runcmd = NULL;
606: for (;;) {
607: q = parse = sob(++parse); /* Optional field */
608: if (q == NULL || *q == '\0')
609: break;
610: if (*q == '/') {
611: *(parse = son(parse)) = '\0';
612: if (strlen(q) >= MAXPATHLEN)
613: errx(1, "%s: pathname too long", q);
614: working->pidfile = strdup(q);
615: if (working->pidfile == NULL)
616: err(1, "strdup");
617: } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
618: *(parse = tmp) = '\0';
619: working->runcmd = strdup(++q);
620: if (working->runcmd == NULL)
621: err(1, "strdup");
622: } else if (strncmp(q, "SIG", 3) == 0) {
623: int i;
624:
625: *(parse = son(parse)) = '\0';
626: for (i = 1; i < NSIG; i++) {
627: if (!strcmp(sys_signame[i], q + 3)) {
628: working->signal = i;
629: break;
630: }
631: }
632: if (i == NSIG)
633: errx(1, "unknown signal: %s", q);
634: } else
635: errx(1, "unrecognized field: %s", q);
1.14 millert 636: }
1.51 millert 637: free(errline);
638:
639: /* If there is an arcdir, set working->backdir. */
640: if (arcdir != NULL && working->logbase != NULL) {
641: if (*arcdir == '/') {
642: /* Fully qualified arcdir */
643: working->backdir = arcdir;
644: } else {
645: /* arcdir is relative to log's parent dir */
646: *(working->logbase - 1) = '\0';
647: if ((asprintf(&working->backdir, "%s/%s",
648: working->log, arcdir)) == -1)
649: err(1, "malloc");
650: *(working->logbase - 1) = '/';
651: }
652: /* Ignore arcdir if it doesn't exist. */
653: if (stat(working->backdir, &sb) != 0 ||
654: !S_ISDIR(sb.st_mode)) {
655: if (working->backdir != arcdir)
656: free(working->backdir);
657: working->backdir = NULL;
658: }
659: } else
660: working->backdir = NULL;
1.25 millert 661:
662: /* Make sure we can't oflow MAXPATHLEN */
1.51 millert 663: if (working->backdir != NULL) {
664: if (snprintf(line, sizeof(line), "%s/%s.%d%s",
665: working->backdir, working->logbase,
666: working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
667: errx(1, "%s: pathname too long", working->log);
668: } else {
669: if (snprintf(line, sizeof(line), "%s.%d%s",
670: working->log, working->numlogs, COMPRESS_POSTFIX)
671: >= MAXPATHLEN)
672: errx(1, "%s: pathname too long", working->log);
673: }
1.35 deraadt 674: }
675: if (working)
676: working->next = NULL;
677: (void)fclose(f);
1.48 millert 678: return (first);
1.1 deraadt 679: }
680:
1.25 millert 681: char *
1.48 millert 682: missing_field(char *p, char *errline)
1.1 deraadt 683: {
1.35 deraadt 684: if (!p || !*p) {
1.14 millert 685: warnx("Missing field in config file line:");
1.35 deraadt 686: fputs(errline, stderr);
687: exit(1);
688: }
1.48 millert 689: return (p);
1.1 deraadt 690: }
691:
1.25 millert 692: void
1.51 millert 693: dotrim(struct conf_entry *ent)
1.35 deraadt 694: {
695: char file1[MAXPATHLEN], file2[MAXPATHLEN];
696: char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.51 millert 697: char oldlog[MAXPATHLEN];
1.35 deraadt 698: int fd;
1.48 millert 699: struct stat sb;
1.51 millert 700: int numdays = ent->numlogs;
701:
702: /* Is there a separate backup dir? */
703: if (ent->backdir != NULL)
704: snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
705: ent->logbase);
706: else
707: strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1 deraadt 708:
1.35 deraadt 709: /* Remove oldest log (may not exist) */
1.51 millert 710: (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
711: (void)snprintf(zfile1, sizeof(zfile1), "%s.%d%s", oldlog, numdays,
1.35 deraadt 712: COMPRESS_POSTFIX);
713:
714: if (noaction) {
1.50 millert 715: printf("\trm -f %s %s\n", file1, zfile1);
1.35 deraadt 716: } else {
717: (void)unlink(file1);
718: (void)unlink(zfile1);
719: }
720:
721: /* Move down log files */
722: while (numdays--) {
1.48 millert 723: (void)strlcpy(file2, file1, sizeof(file2));
1.51 millert 724: (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.48 millert 725: (void)strlcpy(zfile1, file1, sizeof(zfile1));
726: (void)strlcpy(zfile2, file2, sizeof(zfile2));
727: if (lstat(file1, &sb)) {
728: (void)strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1));
729: (void)strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2));
730: if (lstat(zfile1, &sb))
1.35 deraadt 731: continue;
732: }
733: if (noaction) {
1.50 millert 734: printf("\tmv %s %s\n", zfile1, zfile2);
1.51 millert 735: printf("\tchmod %o %s\n", ent->permissions, zfile2);
736: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50 millert 737: printf("\tchown %u:%u %s\n",
1.51 millert 738: ent->uid, ent->gid, zfile2);
1.35 deraadt 739: } else {
740: if (rename(zfile1, zfile2))
1.25 millert 741: warn("can't mv %s to %s", zfile1, zfile2);
1.51 millert 742: if (chmod(zfile2, ent->permissions))
1.25 millert 743: warn("can't chmod %s", zfile2);
1.51 millert 744: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
745: if (chown(zfile2, ent->uid, ent->gid))
1.50 millert 746: warn("can't chown %s", zfile2);
1.35 deraadt 747: }
748: }
1.51 millert 749: if (!noaction && !(ent->flags & CE_BINARY))
750: (void)log_trim(ent->log); /* Report the trimming to the old log */
1.1 deraadt 751:
1.51 millert 752: (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35 deraadt 753: if (noaction) {
1.50 millert 754: printf("\tmktemp %s\n", file2);
1.35 deraadt 755: } else {
756: if ((fd = mkstemp(file2)) < 0)
1.26 millert 757: err(1, "can't start '%s' log", file2);
1.51 millert 758: if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
759: if (fchown(fd, ent->uid, ent->gid))
760: err(1, "can't chown '%s' log file", file2);
761: if (fchmod(fd, ent->permissions))
1.26 millert 762: err(1, "can't chmod '%s' log file", file2);
1.35 deraadt 763: (void)close(fd);
1.26 millert 764: /* Add status message */
1.51 millert 765: if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26 millert 766: err(1, "can't add status message to log '%s'", file2);
1.35 deraadt 767: }
1.26 millert 768:
1.51 millert 769: if (ent->numlogs == 0) {
1.5 deraadt 770: if (noaction)
1.51 millert 771: printf("\trm %s\n", ent->log);
772: else if (unlink(ent->log))
773: warn("can't rm %s", ent->log);
1.5 deraadt 774: } else {
1.51 millert 775: if (noaction)
776: printf("\tmv %s to %s\n", ent->log, file1);
777: else if (rename(ent->log, file1))
778: warn("can't to mv %s to %s", ent->log, file1);
1.5 deraadt 779: }
780:
1.26 millert 781: /* Now move the new log file into place */
782: if (noaction)
1.51 millert 783: printf("\tmv %s to %s\n", file2, ent->log);
784: else if (rename(file2, ent->log))
785: warn("can't to mv %s to %s", file2, ent->log);
1.1 deraadt 786: }
787:
788: /* Log the fact that the logs were turned over */
1.25 millert 789: int
1.48 millert 790: log_trim(char *log)
1.1 deraadt 791: {
1.35 deraadt 792: FILE *f;
1.25 millert 793:
1.35 deraadt 794: if ((f = fopen(log, "a")) == NULL)
1.48 millert 795: return (-1);
1.44 mpech 796: (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
797: daytime, hostname, (long)getpid());
1.35 deraadt 798: if (fclose(f) == EOF)
799: err(1, "log_trim: fclose");
1.48 millert 800: return (0);
1.1 deraadt 801: }
802:
1.16 millert 803: /* Fork off compress or gzip to compress the old log file */
1.25 millert 804: void
1.51 millert 805: compress_log(struct conf_entry *ent)
1.1 deraadt 806: {
1.48 millert 807: pid_t pid;
808: char *base, tmp[MAXPATHLEN];
1.51 millert 809:
810: if (ent->backdir != NULL)
811: snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
812: ent->logbase);
813: else
814: snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
815:
1.27 millert 816: if ((base = strrchr(COMPRESS, '/')) == NULL)
817: base = COMPRESS;
818: else
819: base++;
820: if (noaction) {
1.51 millert 821: printf("%s %s\n", base, tmp);
1.27 millert 822: return;
823: }
1.35 deraadt 824: pid = fork();
825: if (pid < 0) {
1.11 downsj 826: err(1, "fork");
1.50 millert 827: } else if (pid == 0) {
1.37 deraadt 828: (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16 millert 829: warn(COMPRESS);
830: _exit(1);
1.35 deraadt 831: }
1.1 deraadt 832: }
833:
834: /* Return size in kilobytes of a file */
1.25 millert 835: int
1.48 millert 836: sizefile(char *file)
1.1 deraadt 837: {
1.35 deraadt 838: struct stat sb;
1.1 deraadt 839:
1.35 deraadt 840: if (stat(file, &sb) < 0)
1.48 millert 841: return (-1);
842: return (sb.st_blocks / (1024.0 / DEV_BSIZE));
1.1 deraadt 843: }
844:
1.25 millert 845: /* Return the age (in hours) of old log file (file.0), or -1 if none */
846: int
1.51 millert 847: age_old_log(struct conf_entry *ent)
1.1 deraadt 848: {
1.35 deraadt 849: struct stat sb;
850: char tmp[MAXPATHLEN];
1.1 deraadt 851:
1.51 millert 852: if (ent->backdir != NULL)
853: snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir, ent->logbase);
854: else {
855: strlcpy(tmp, ent->log, sizeof(tmp));
856: strlcat(tmp, ".0", sizeof(tmp));
857: }
858: if (ent->flags & CE_COMPACT)
1.48 millert 859: strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp));
1.51 millert 860: if (stat(tmp, &sb) < 0)
861: return (-1);
1.48 millert 862: return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1 deraadt 863: }
864:
865: /* Skip Over Blanks */
1.25 millert 866: char *
1.48 millert 867: sob(char *p)
1.1 deraadt 868: {
1.35 deraadt 869: while (p && *p && isspace(*p))
870: p++;
1.48 millert 871: return (p);
1.1 deraadt 872: }
873:
874: /* Skip Over Non-Blanks */
1.25 millert 875: char *
1.48 millert 876: son(char *p)
1.1 deraadt 877: {
1.35 deraadt 878: while (p && *p && !isspace(*p))
879: p++;
1.48 millert 880: return (p);
1.1 deraadt 881: }
882:
883: /* Check if string is actually a number */
1.25 millert 884: int
1.48 millert 885: isnumberstr(char *string)
1.1 deraadt 886: {
1.35 deraadt 887: while (*string) {
888: if (!isdigit(*string++))
1.48 millert 889: return (0);
1.35 deraadt 890: }
1.48 millert 891: return (1);
1.10 downsj 892: }
893:
1.25 millert 894: void
1.48 millert 895: domonitor(char *log, char *whom)
1.10 downsj 896: {
897: struct stat sb, tsb;
1.35 deraadt 898: char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10 downsj 899: FILE *fp;
900: off_t osize;
901: int rd;
902:
903: if (stat(log, &sb) < 0)
904: return;
905:
906: flog = strdup(log);
1.11 downsj 907: if (flog == NULL)
908: err(1, "strdup");
909:
1.10 downsj 910: for (p = flog; *p != '\0'; p++) {
911: if (*p == '/')
912: *p = '_';
913: }
1.48 millert 914: snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35 deraadt 915: STATS_DIR, flog);
1.10 downsj 916:
917: /* ..if it doesn't exist, simply record the current size. */
918: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
919: goto update;
920:
921: fp = fopen(fname, "r");
922: if (fp == NULL) {
1.34 millert 923: warn("%s", fname);
1.10 downsj 924: goto cleanup;
925: }
926: #ifdef QUAD_OFF_T
927: if (fscanf(fp, "%qd\n", &osize) != 1) {
928: #else
929: if (fscanf(fp, "%ld\n", &osize) != 1) {
930: #endif /* QUAD_OFF_T */
931: fclose(fp);
932: goto update;
933: }
934:
935: fclose(fp);
936:
937: /* If the file is smaller, mark the entire thing as changed. */
938: if (sb.st_size < osize)
939: osize = 0;
940:
941: /* Now see if current size is larger. */
942: if (sb.st_size > osize) {
943: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 944: if (rb == NULL)
945: err(1, "malloc");
1.10 downsj 946:
947: /* Open logfile, seek. */
948: fp = fopen(log, "r");
949: if (fp == NULL) {
1.34 millert 950: warn("%s", log);
1.10 downsj 951: goto cleanup;
952: }
953: fseek(fp, osize, SEEK_SET);
954: rd = fread(rb, 1, sb.st_size - osize, fp);
955: if (rd < 1) {
1.11 downsj 956: warn("fread");
1.10 downsj 957: fclose(fp);
958: goto cleanup;
959: }
960:
961: /* Send message. */
962: fclose(fp);
963:
964: fp = openmail();
965: if (fp == NULL) {
1.11 downsj 966: warn("openmail");
1.10 downsj 967: goto cleanup;
968: }
969: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
970: whom, log);
971: fwrite(rb, 1, rd, fp);
972: fputs("\n\n", fp);
973:
1.48 millert 974: pclose(fp);
1.10 downsj 975: }
976: update:
977: /* Reopen for writing and update file. */
978: fp = fopen(fname, "w");
979: if (fp == NULL) {
1.34 millert 980: warn("%s", fname);
1.10 downsj 981: goto cleanup;
982: }
983: #ifdef QUAD_OFF_T
984: fprintf(fp, "%qd\n", sb.st_size);
985: #else
986: fprintf(fp, "%ld\n", sb.st_size);
987: #endif /* QUAD_OFF_T */
988: fclose(fp);
989:
990: cleanup:
991: free(flog);
992: if (rb != NULL)
993: free(rb);
994: }
995:
1.25 millert 996: FILE *
1.48 millert 997: openmail(void)
1.10 downsj 998: {
1.48 millert 999: FILE *ret;
1.35 deraadt 1000: char *cmdbuf = NULL;
1.10 downsj 1001:
1.35 deraadt 1002: asprintf(&cmdbuf, "%s -t", SENDMAIL);
1003: if (cmdbuf) {
1004: ret = popen(cmdbuf, "w");
1005: free(cmdbuf);
1006: return (ret);
1007: }
1008: return (NULL);
1.10 downsj 1009: }
1010:
1.25 millert 1011: void
1.48 millert 1012: child_killer(int signo)
1.16 millert 1013: {
1.38 deraadt 1014: int save_errno = errno;
1.16 millert 1015: int status;
1016:
1017: while (waitpid(-1, &status, WNOHANG) > 0)
1018: ;
1.38 deraadt 1019: errno = save_errno;
1.1 deraadt 1020: }