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