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