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