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