Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.39
1.39 ! mpech 1: /* $OpenBSD: newsyslog.c,v 1.38 2001/11/16 23:49:07 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.39 ! mpech 91: static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.38 2001/11/16 23:49:07 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)) ||
287: ((ent->hours > 0) && ((modtime >= ent->hours)
288: || (modtime < 0))))) {
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,
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: {
320: FILE *f;
1.35 deraadt 321: char line[BUFSIZ];
1.26 millert 322: pid_t pid = 0;
323:
1.35 deraadt 324: if ((f = fopen(pidfile, "r")) == NULL) {
1.26 millert 325: warn("can't open %s", pidfile);
326: return;
327: }
328:
329: if (fgets(line, sizeof(line), f))
330: pid = atoi(line);
331: (void)fclose(f);
332:
1.35 deraadt 333: if (noaction)
334: (void)printf("kill -%s %d\n", sys_signame[signal], pid);
1.26 millert 335: else if (pid == 0)
336: warnx("empty pid file: %s", pidfile);
337: else if (pid < MIN_PID)
338: warnx("preposterous process number: %d", pid);
1.30 millert 339: else if (kill(pid, signal))
340: warnx("warning - could not send SIG%s to daemon",
341: sys_signame[signal]);
1.26 millert 342: }
343:
1.25 millert 344: void
345: PRS(argc, argv)
1.35 deraadt 346: int argc;
347: char **argv;
1.1 deraadt 348: {
1.35 deraadt 349: int c;
1.1 deraadt 350: char *p;
351:
1.35 deraadt 352: timenow = time(NULL);
353: daytime = ctime(&timenow) + 4;
354: daytime[15] = '\0';
1.1 deraadt 355:
1.35 deraadt 356: /* Let's get our hostname */
357: (void)gethostname(hostname, sizeof(hostname));
1.1 deraadt 358:
359: /* Truncate domain */
1.9 downsj 360: p = strchr(hostname, '.');
361: if (p)
1.1 deraadt 362: *p = '\0';
363:
1.35 deraadt 364: optind = 1; /* Start options parsing */
1.36 aaron 365: while ((c = getopt(argc, argv, "nrvmf:")) != -1) {
1.35 deraadt 366: switch (c) {
367: case 'n':
368: noaction++; /* This implies needroot as off */
369: /* fall through */
370: case 'r':
371: needroot = 0;
372: break;
373: case 'v':
374: verbose++;
375: break;
376: case 'f':
377: conf = optarg;
378: break;
1.10 downsj 379: case 'm':
1.33 millert 380: monitormode++;
1.10 downsj 381: break;
1.35 deraadt 382: default:
383: usage();
384: }
385: }
1.9 downsj 386: }
1.1 deraadt 387:
1.25 millert 388: void
389: usage()
1.1 deraadt 390: {
1.25 millert 391: extern const char *__progname;
392:
1.36 aaron 393: (void)fprintf(stderr, "usage: %s [-mnrv] [-f config_file]\n",
1.25 millert 394: __progname);
1.14 millert 395: exit(1);
1.1 deraadt 396: }
397:
398: /* Parse a configuration file and return a linked list of all the logs
399: * to process
400: */
1.25 millert 401: struct conf_entry *
1.28 millert 402: parse_file(nentries)
403: int *nentries;
1.1 deraadt 404: {
1.35 deraadt 405: FILE *f;
406: char line[BUFSIZ], *parse, *q;
407: char *errline, *group, *tmp;
408: struct conf_entry *first = NULL;
409: struct conf_entry *working = NULL;
410: struct passwd *pass;
411: struct group *grp;
1.1 deraadt 412:
1.35 deraadt 413: if (strcmp(conf, "-") == 0)
414: f = stdin;
1.25 millert 415: else {
1.35 deraadt 416: if ((f = fopen(conf, "r")) == NULL)
1.25 millert 417: err(1, "can't open %s", conf);
418: }
1.11 downsj 419:
1.28 millert 420: *nentries = 0;
1.35 deraadt 421: while (fgets(line, sizeof(line), f)) {
422: if ((line[0] == '\n') || (line[0] == '#'))
423: continue;
424: errline = strdup(line);
1.11 downsj 425: if (errline == NULL)
426: err(1, "strdup");
1.28 millert 427: (*nentries)++;
1.35 deraadt 428: if (!first) {
429: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 430: if (working == NULL)
431: err(1, "malloc");
1.35 deraadt 432: first = working;
433: } else {
434: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 435: if (working->next == NULL)
436: err(1, "malloc");
1.35 deraadt 437: working = working->next;
438: }
1.1 deraadt 439:
1.35 deraadt 440: q = parse = missing_field(sob(line), errline);
441: *(parse = son(line)) = '\0';
442: working->log = strdup(q);
1.11 downsj 443: if (working->log == NULL)
444: err(1, "strdup");
1.1 deraadt 445:
1.35 deraadt 446: q = parse = missing_field(sob(++parse), errline);
447: *(parse = son(parse)) = '\0';
1.25 millert 448: if ((group = strchr(q, '.')) != NULL) {
449: *group++ = '\0';
450: if (*q) {
451: if (!(isnumberstr(q))) {
452: if ((pass = getpwnam(q)) == NULL)
453: errx(1, "Error in config file; unknown user: %s", q);
454: working->uid = pass->pw_uid;
455: } else
456: working->uid = atoi(q);
457: } else
458: working->uid = NONE;
459:
460: q = group;
461: if (*q) {
462: if (!(isnumberstr(q))) {
463: if ((grp = getgrnam(q)) == NULL)
464: errx(1, "Error in config file; unknown group: %s", q);
465: working->gid = grp->gr_gid;
466: } else
467: working->gid = atoi(q);
468: } else
469: working->gid = NONE;
470:
471: q = parse = missing_field(sob(++parse), errline);
472: *(parse = son(parse)) = '\0';
473: } else
474: working->uid = working->gid = NONE;
1.1 deraadt 475:
1.35 deraadt 476: if (!sscanf(q, "%o", &working->permissions))
1.11 downsj 477: errx(1, "Error in config file; bad permissions: %s", q);
1.1 deraadt 478:
1.35 deraadt 479: q = parse = missing_field(sob(++parse), errline);
480: *(parse = son(parse)) = '\0';
481: if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.11 downsj 482: errx(1, "Error in config file; bad number: %s", q);
1.1 deraadt 483:
1.35 deraadt 484: q = parse = missing_field(sob(++parse), errline);
485: *(parse = son(parse)) = '\0';
486: if (isdigit(*q))
487: working->size = atoi(q);
488: else
489: working->size = -1;
490:
491: q = parse = missing_field(sob(++parse), errline);
492: *(parse = son(parse)) = '\0';
493: if (isdigit(*q))
494: working->hours = atoi(q);
495: else
496: working->hours = -1;
1.1 deraadt 497:
1.35 deraadt 498: working->flags = 0;
499: q = sob(++parse); /* Optional field */
1.32 millert 500: if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
501: *q == 'M' || *q == 'm') {
502: *(parse = son(q)) = '\0';
503: while (*q) {
504: switch (*q) {
505: case 'Z':
506: case 'z':
507: working->flags |= CE_COMPACT;
508: break;
509: case 'B':
510: case 'b':
511: working->flags |= CE_BINARY;
512: break;
513: case 'M':
514: case 'm':
515: working->flags |= CE_MONITOR;
516: break;
517: default:
518: errx(1, "Illegal flag in config file: %c", *q);
519: break;
520: }
521: q++;
522: }
523: } else
524: parse--; /* no flags so undo */
1.10 downsj 525:
526: working->whom = NULL;
527: if (working->flags & CE_MONITOR) { /* Optional field */
528: q = parse = sob(++parse);
529: *(parse = son(parse)) = '\0';
530:
531: working->whom = strdup(q);
1.11 downsj 532: if (working->log == NULL)
533: err(1, "strdup");
1.10 downsj 534: }
1.14 millert 535:
536: working->pidfile = PIDFILE;
1.30 millert 537: working->signal = SIGHUP;
538: working->runcmd = NULL;
539: for (;;) {
540: q = parse = sob(++parse); /* Optional field */
541: if (q == NULL || *q == '\0')
542: break;
543: if (*q == '/') {
544: *(parse = son(parse)) = '\0';
545: if (strlen(q) >= MAXPATHLEN)
546: errx(1, "%s: pathname too long", q);
547: working->pidfile = strdup(q);
548: if (working->pidfile == NULL)
549: err(1, "strdup");
550: } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
551: *(parse = tmp) = '\0';
552: working->runcmd = strdup(++q);
553: if (working->runcmd == NULL)
554: err(1, "strdup");
555: } else if (strncmp(q, "SIG", 3) == 0) {
556: int i;
557:
558: *(parse = son(parse)) = '\0';
559: for (i = 1; i < NSIG; i++) {
560: if (!strcmp(sys_signame[i], q + 3)) {
561: working->signal = i;
562: break;
563: }
564: }
565: if (i == NSIG)
566: errx(1, "unknown signal: %s", q);
567: } else
568: errx(1, "unrecognized field: %s", q);
1.14 millert 569: }
1.25 millert 570:
571: /* Make sure we can't oflow MAXPATHLEN */
572: if (asprintf(&tmp, "%s.%d%s", working->log, working->numlogs,
573: COMPRESS_POSTFIX) >= MAXPATHLEN)
574: errx(1, "%s: pathname too long", working->log);
1.35 deraadt 575:
576: if (tmp)
577: free(tmp);
578: free(errline);
579: }
580: if (working)
581: working->next = NULL;
582: (void)fclose(f);
583: return(first);
1.1 deraadt 584: }
585:
1.25 millert 586: char *
587: missing_field(p, errline)
1.35 deraadt 588: char *p;
1.25 millert 589: char *errline;
1.1 deraadt 590: {
1.35 deraadt 591: if (!p || !*p) {
1.14 millert 592: warnx("Missing field in config file line:");
1.35 deraadt 593: fputs(errline, stderr);
594: exit(1);
595: }
596: return(p);
1.1 deraadt 597: }
598:
1.25 millert 599: void
1.26 millert 600: dotrim(log, numdays, flags, perm, owner_uid, group_gid)
1.35 deraadt 601: char *log;
602: int numdays;
603: int flags;
604: int perm;
605: uid_t owner_uid;
606: gid_t group_gid;
607: {
608: char file1[MAXPATHLEN], file2[MAXPATHLEN];
609: char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
610: int fd;
611: struct stat st;
1.6 tholo 612: int days = numdays;
1.1 deraadt 613:
1.35 deraadt 614: /* Remove oldest log (may not exist) */
615: (void)snprintf(file1, sizeof file1, "%s.%d", log, numdays);
616: (void)snprintf(zfile1, sizeof zfile1, "%s.%d%s", log, numdays,
617: COMPRESS_POSTFIX);
618:
619: if (noaction) {
620: printf("rm -f %s %s\n", file1, zfile1);
621: } else {
622: (void)unlink(file1);
623: (void)unlink(zfile1);
624: }
625:
626: /* Move down log files */
627: while (numdays--) {
628: (void)strlcpy(file2, file1, sizeof file2);
629: (void)snprintf(file1, sizeof file1, "%s.%d", log, numdays);
630: (void)strlcpy(zfile1, file1, sizeof zfile1);
631: (void)strlcpy(zfile2, file2, sizeof zfile2);
632: if (lstat(file1, &st)) {
633: (void)strlcat(zfile1, COMPRESS_POSTFIX, sizeof zfile1);
634: (void)strlcat(zfile2, COMPRESS_POSTFIX, sizeof zfile2);
635: if (lstat(zfile1, &st))
636: continue;
637: }
638: if (noaction) {
639: printf("mv %s %s\n", zfile1, zfile2);
640: printf("chmod %o %s\n", perm, zfile2);
641: printf("chown %d:%d %s\n",
642: owner_uid, group_gid, zfile2);
643: } else {
644: if (rename(zfile1, zfile2))
1.25 millert 645: warn("can't mv %s to %s", zfile1, zfile2);
1.35 deraadt 646: if (chmod(zfile2, perm))
1.25 millert 647: warn("can't chmod %s", zfile2);
1.35 deraadt 648: if (chown(zfile2, owner_uid, group_gid))
1.25 millert 649: warn("can't chown %s", zfile2);
1.35 deraadt 650: }
651: }
652: if (!noaction && !(flags & CE_BINARY))
653: (void)log_trim(log); /* Report the trimming to the old log */
1.1 deraadt 654:
1.26 millert 655: (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", log);
1.35 deraadt 656: if (noaction) {
657: printf("Create new log file...\n");
658: } else {
659: if ((fd = mkstemp(file2)) < 0)
1.26 millert 660: err(1, "can't start '%s' log", file2);
1.35 deraadt 661: if (fchown(fd, owner_uid, group_gid))
1.26 millert 662: err(1, "can't chown '%s' log file", file2);
1.35 deraadt 663: if (fchmod(fd, perm))
1.26 millert 664: err(1, "can't chmod '%s' log file", file2);
1.35 deraadt 665: (void)close(fd);
1.26 millert 666: /* Add status message */
1.35 deraadt 667: if (!(flags & CE_BINARY) && log_trim(file2))
1.26 millert 668: err(1, "can't add status message to log '%s'", file2);
1.35 deraadt 669: }
1.26 millert 670:
1.6 tholo 671: if (days == 0) {
1.5 deraadt 672: if (noaction)
1.25 millert 673: printf("rm %s\n", log);
674: else if (unlink(log))
675: warn("can't rm %s", log);
1.5 deraadt 676: } else {
677: if (noaction)
1.35 deraadt 678: printf("mv %s to %s\n", log, file1);
679: else if (rename(log, file1))
1.25 millert 680: warn("can't to mv %s to %s", log, file1);
1.5 deraadt 681: }
682:
1.26 millert 683: /* Now move the new log file into place */
684: if (noaction)
685: printf("mv %s to %s\n", file2, log);
686: else if (rename(file2, log))
687: warn("can't to mv %s to %s", file2, log);
1.1 deraadt 688: }
689:
690: /* Log the fact that the logs were turned over */
1.25 millert 691: int
692: log_trim(log)
1.17 deraadt 693: char *log;
1.1 deraadt 694: {
1.35 deraadt 695: FILE *f;
1.25 millert 696:
1.35 deraadt 697: if ((f = fopen(log, "a")) == NULL)
698: return(-1);
699: (void)fprintf(f, "%s %s newsyslog[%d]: logfile turned over\n",
1.25 millert 700: daytime, hostname, getpid());
1.35 deraadt 701: if (fclose(f) == EOF)
702: err(1, "log_trim: fclose");
703: return(0);
1.1 deraadt 704: }
705:
1.16 millert 706: /* Fork off compress or gzip to compress the old log file */
1.25 millert 707: void
708: compress_log(log)
1.35 deraadt 709: char *log;
1.1 deraadt 710: {
1.35 deraadt 711: pid_t pid;
1.27 millert 712: char *base;
1.35 deraadt 713: char tmp[MAXPATHLEN];
714:
1.27 millert 715: if ((base = strrchr(COMPRESS, '/')) == NULL)
716: base = COMPRESS;
717: else
718: base++;
719: if (noaction) {
720: printf("%s %s.0\n", base, log);
721: return;
722: }
1.35 deraadt 723: pid = fork();
724: (void)snprintf(tmp, sizeof tmp, "%s.0", log);
725: if (pid < 0) {
1.11 downsj 726: err(1, "fork");
1.35 deraadt 727: } else if (!pid) {
1.37 deraadt 728: (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16 millert 729: warn(COMPRESS);
730: _exit(1);
1.35 deraadt 731: }
1.1 deraadt 732: }
733:
734: /* Return size in kilobytes of a file */
1.25 millert 735: int
736: sizefile(file)
1.35 deraadt 737: char *file;
1.1 deraadt 738: {
1.35 deraadt 739: struct stat sb;
1.1 deraadt 740:
1.35 deraadt 741: if (stat(file, &sb) < 0)
742: return(-1);
743: return(sb.st_blocks / (1024.0 / DEV_BSIZE));
1.1 deraadt 744: }
745:
1.25 millert 746: /* Return the age (in hours) of old log file (file.0), or -1 if none */
747: int
748: age_old_log(file)
1.35 deraadt 749: char *file;
1.1 deraadt 750: {
1.35 deraadt 751: struct stat sb;
752: char tmp[MAXPATHLEN];
1.1 deraadt 753:
1.35 deraadt 754: (void)strlcpy(tmp, file, sizeof tmp);
755: strlcat(tmp, ".0", sizeof tmp);
756: if (stat(tmp, &sb) < 0) {
757: strlcat(tmp, COMPRESS_POSTFIX, sizeof tmp);
758: if (stat(tmp, &sb) < 0)
759: return(-1);
760: }
761: return( (int) (timenow - sb.st_mtime + 1800) / 3600);
1.1 deraadt 762: }
763:
764: /* Skip Over Blanks */
1.25 millert 765: char *
766: sob(p)
1.39 ! mpech 767: char *p;
1.1 deraadt 768: {
1.35 deraadt 769: while (p && *p && isspace(*p))
770: p++;
771: return(p);
1.1 deraadt 772: }
773:
774: /* Skip Over Non-Blanks */
1.25 millert 775: char *
776: son(p)
1.39 ! mpech 777: char *p;
1.1 deraadt 778: {
1.35 deraadt 779: while (p && *p && !isspace(*p))
780: p++;
781: return(p);
1.1 deraadt 782: }
783:
784: /* Check if string is actually a number */
1.25 millert 785: int
786: isnumberstr(string)
1.10 downsj 787: char *string;
1.1 deraadt 788: {
1.35 deraadt 789: while (*string) {
790: if (!isdigit(*string++))
791: return(0);
792: }
793: return(1);
1.10 downsj 794: }
795:
1.25 millert 796: void
797: domonitor(log, whom)
1.10 downsj 798: char *log, *whom;
799: {
800: struct stat sb, tsb;
1.35 deraadt 801: char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10 downsj 802: FILE *fp;
803: off_t osize;
804: int rd;
805:
806: if (stat(log, &sb) < 0)
807: return;
808:
809: flog = strdup(log);
1.11 downsj 810: if (flog == NULL)
811: err(1, "strdup");
812:
1.10 downsj 813: for (p = flog; *p != '\0'; p++) {
814: if (*p == '/')
815: *p = '_';
816: }
1.35 deraadt 817: snprintf(fname, sizeof fname, "%s/newsyslog.%s.size",
818: STATS_DIR, flog);
1.10 downsj 819:
820: /* ..if it doesn't exist, simply record the current size. */
821: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
822: goto update;
823:
824: fp = fopen(fname, "r");
825: if (fp == NULL) {
1.34 millert 826: warn("%s", fname);
1.10 downsj 827: goto cleanup;
828: }
829: #ifdef QUAD_OFF_T
830: if (fscanf(fp, "%qd\n", &osize) != 1) {
831: #else
832: if (fscanf(fp, "%ld\n", &osize) != 1) {
833: #endif /* QUAD_OFF_T */
834: fclose(fp);
835: goto update;
836: }
837:
838: fclose(fp);
839:
840: /* If the file is smaller, mark the entire thing as changed. */
841: if (sb.st_size < osize)
842: osize = 0;
843:
844: /* Now see if current size is larger. */
845: if (sb.st_size > osize) {
846: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 847: if (rb == NULL)
848: err(1, "malloc");
1.10 downsj 849:
850: /* Open logfile, seek. */
851: fp = fopen(log, "r");
852: if (fp == NULL) {
1.34 millert 853: warn("%s", log);
1.10 downsj 854: goto cleanup;
855: }
856: fseek(fp, osize, SEEK_SET);
857: rd = fread(rb, 1, sb.st_size - osize, fp);
858: if (rd < 1) {
1.11 downsj 859: warn("fread");
1.10 downsj 860: fclose(fp);
861: goto cleanup;
862: }
863:
864: /* Send message. */
865: fclose(fp);
866:
867: fp = openmail();
868: if (fp == NULL) {
1.11 downsj 869: warn("openmail");
1.10 downsj 870: goto cleanup;
871: }
872: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
873: whom, log);
874: fwrite(rb, 1, rd, fp);
875: fputs("\n\n", fp);
876:
877: closemail(fp);
878: }
879: update:
880: /* Reopen for writing and update file. */
881: fp = fopen(fname, "w");
882: if (fp == NULL) {
1.34 millert 883: warn("%s", fname);
1.10 downsj 884: goto cleanup;
885: }
886: #ifdef QUAD_OFF_T
887: fprintf(fp, "%qd\n", sb.st_size);
888: #else
889: fprintf(fp, "%ld\n", sb.st_size);
890: #endif /* QUAD_OFF_T */
891: fclose(fp);
892:
893: cleanup:
894: free(flog);
895: if (rb != NULL)
896: free(rb);
897: }
898:
1.25 millert 899: FILE *
900: openmail()
1.10 downsj 901: {
1.35 deraadt 902: char *cmdbuf = NULL;
1.10 downsj 903: FILE *ret;
904:
1.35 deraadt 905: asprintf(&cmdbuf, "%s -t", SENDMAIL);
906: if (cmdbuf) {
907: ret = popen(cmdbuf, "w");
908: free(cmdbuf);
909: return (ret);
910: }
911: return (NULL);
1.10 downsj 912: }
913:
1.25 millert 914: void
915: closemail(pfp)
1.10 downsj 916: FILE *pfp;
917: {
918: pclose(pfp);
1.16 millert 919: }
920:
1.25 millert 921: void
922: child_killer(signum)
1.16 millert 923: int signum;
924: {
1.38 deraadt 925: int save_errno = errno;
1.16 millert 926: int status;
927:
928: while (waitpid(-1, &status, WNOHANG) > 0)
929: ;
1.38 deraadt 930: errno = save_errno;
1.1 deraadt 931: }