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