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