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