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