Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.14
1.14 ! millert 1: /* $OpenBSD: newsyslog.c,v 1.13 1998/04/25 18:12:58 mickey Exp $ */
1.10 downsj 2:
3: /*
4: * Copyright (c) 1997, Jason Downs. All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: * 2. Redistributions in binary form must reproduce the above copyright
12: * notice, this list of conditions and the following disclaimer in the
13: * documentation and/or other materials provided with the distribution.
14: * 3. All advertising materials mentioning features or use of this software
15: * must display the following acknowledgement:
16: * This product includes software developed by Jason Downs for the
17: * OpenBSD system.
18: * 4. Neither the name(s) of the author(s) nor the name OpenBSD
19: * may be used to endorse or promote products derived from this software
20: * without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
23: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
26: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32: * SUCH DAMAGE.
33: */
1.3 deraadt 34:
1.1 deraadt 35: /*
36: * This file contains changes from the Open Software Foundation.
37: */
38:
39: /*
40:
41: Copyright 1988, 1989 by the Massachusetts Institute of Technology
42:
43: Permission to use, copy, modify, and distribute this software
44: and its documentation for any purpose and without fee is
45: hereby granted, provided that the above copyright notice
46: appear in all copies and that both that copyright notice and
47: this permission notice appear in supporting documentation,
48: and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
49: used in advertising or publicity pertaining to distribution
50: of the software without specific, written prior permission.
51: M.I.T. and the M.I.T. S.I.P.B. make no representations about
52: the suitability of this software for any purpose. It is
53: provided "as is" without express or implied warranty.
54:
55: */
56:
57: /*
58: * newsyslog - roll over selected logs at the appropriate time,
59: * keeping the a specified number of backup files around.
60: *
61: */
62:
63: #ifndef lint
1.14 ! millert 64: static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.13 1998/04/25 18:12:58 mickey Exp $";
1.1 deraadt 65: #endif /* not lint */
66:
67: #ifndef CONF
68: #define CONF "/etc/athena/newsyslog.conf" /* Configuration file */
69: #endif
70: #ifndef PIDFILE
71: #define PIDFILE "/etc/syslog.pid"
72: #endif
73: #ifndef COMPRESS
74: #define COMPRESS "/usr/ucb/compress" /* File compression program */
75: #endif
76: #ifndef COMPRESS_POSTFIX
77: #define COMPRESS_POSTFIX ".Z"
78: #endif
1.10 downsj 79: #ifndef STATS_DIR
80: #define STATS_DIR "/etc"
81: #endif
82: #ifndef SENDMAIL
83: #define SENDMAIL "/usr/lib/sendmail"
84: #endif
1.1 deraadt 85:
86: #include <stdio.h>
1.9 downsj 87: #include <sys/types.h>
88: #include <sys/time.h>
89: #include <sys/stat.h>
90: #include <sys/param.h>
91: #include <sys/wait.h>
1.1 deraadt 92: #include <stdlib.h>
93: #include <string.h>
94: #include <ctype.h>
95: #include <signal.h>
1.9 downsj 96: #include <fcntl.h>
1.1 deraadt 97: #include <pwd.h>
98: #include <grp.h>
1.9 downsj 99: #include <unistd.h>
1.11 downsj 100: #include <err.h>
1.1 deraadt 101:
102: #define kbytes(size) (((size) + 1023) >> 10)
103:
1.10 downsj 104: #define CE_COMPACT 0x01 /* Compact the achived log files */
105: #define CE_BINARY 0x02 /* Logfile is in binary, don't add */
106: /* status messages */
107: #define CE_MONITOR 0x04 /* Monitory for changes */
1.1 deraadt 108: #define NONE -1
109:
110: struct conf_entry {
111: char *log; /* Name of the log */
112: int uid; /* Owner of log */
113: int gid; /* Group of log */
114: int numlogs; /* Number of logs to keep */
115: int size; /* Size cutoff to trigger trimming the log */
116: int hours; /* Hours between log trimming */
117: int permissions; /* File permissions on the log */
118: int flags; /* Flags (CE_COMPACT & CE_BINARY) */
1.10 downsj 119: char *whom; /* Whom to notify if logfile changes */
1.14 ! millert 120: char *pidfile; /* Path to file containg pid to HUP */
1.1 deraadt 121: struct conf_entry *next; /* Linked list pointer */
122: };
123:
1.11 downsj 124: extern const char *__progname;
125:
1.1 deraadt 126: int verbose = 0; /* Print out what's going on */
127: int needroot = 1; /* Root privs are necessary */
128: int noaction = 0; /* Don't do anything, just show it */
1.10 downsj 129: int monitor = 0; /* Don't do monitoring by default */
1.1 deraadt 130: char *conf = CONF; /* Configuration file to use */
131: time_t timenow;
132: #define MIN_PID 3
1.14 ! millert 133: #define MAX_PID 30000
1.7 deraadt 134: char hostname[MAXHOSTNAMELEN]; /* hostname */
1.1 deraadt 135: char *daytime; /* timenow in human readable form */
136:
137:
1.9 downsj 138: void do_entry __P((struct conf_entry *));
139: void PRS __P((int, char **));
140: void usage __P((void));
141: struct conf_entry *parse_file __P((void));
142: char *missing_field __P((char *, char *));
1.14 ! millert 143: void dotrim __P((char *, int, int, int, int, int, int));
1.9 downsj 144: int log_trim __P((char *));
145: void compress_log __P((char *));
146: int sizefile __P((char *));
147: int age_old_log __P((char *));
148: char *sob __P((char *));
149: char *son __P((char *));
1.10 downsj 150: int isnumberstr __P((char *));
151: void domonitor __P((char *, char *));
152: FILE *openmail __P((void));
153: void closemail __P((FILE *));
1.1 deraadt 154:
1.9 downsj 155: int main(argc, argv)
1.1 deraadt 156: int argc;
157: char **argv;
158: {
159: struct conf_entry *p, *q;
160:
161: PRS(argc,argv);
1.11 downsj 162: if (needroot && getuid() && geteuid())
163: errx(1, "You must be root.");
1.1 deraadt 164: p = q = parse_file();
165: while (p) {
166: do_entry(p);
167: p=p->next;
1.11 downsj 168: free(q);
1.1 deraadt 169: q=p;
170: }
171: exit(0);
172: }
173:
1.9 downsj 174: void do_entry(ent)
1.1 deraadt 175: struct conf_entry *ent;
176:
177: {
1.14 ! millert 178: int size, modtime, pid;
! 179: char line[BUFSIZ];
! 180: FILE *f;
! 181:
! 182: /* First find the pid to HUP */
! 183: pid = -1;
! 184: if ((f = fopen(ent->pidfile,"r")) != NULL) {
! 185: if (fgets(line,BUFSIZ,f))
! 186: pid = atoi(line);
! 187: (void)fclose(f);
! 188: }
1.1 deraadt 189:
190: if (verbose) {
191: if (ent->flags & CE_COMPACT)
192: printf("%s <%dZ>: ",ent->log,ent->numlogs);
193: else
194: printf("%s <%d>: ",ent->log,ent->numlogs);
195: }
196: size = sizefile(ent->log);
197: modtime = age_old_log(ent->log);
198: if (size < 0) {
199: if (verbose)
200: printf("does not exist.\n");
201: } else {
202: if (verbose && (ent->size > 0))
203: printf("size (Kb): %d [%d] ", size, ent->size);
204: if (verbose && (ent->hours > 0))
205: printf(" age (hr): %d [%d] ", modtime, ent->hours);
1.10 downsj 206: if (monitor && ent->flags & CE_MONITOR)
207: domonitor(ent->log, ent->whom);
208: if (!monitor && ((ent->size > 0) && (size >= ent->size)) ||
1.1 deraadt 209: ((ent->hours > 0) && ((modtime >= ent->hours)
210: || (modtime < 0)))) {
211: if (verbose)
212: printf("--> trimming log....\n");
213: if (noaction && !verbose) {
214: if (ent->flags & CE_COMPACT)
215: printf("%s <%dZ>: trimming",
216: ent->log,ent->numlogs);
217: else
218: printf("%s <%d>: trimming",
219: ent->log,ent->numlogs);
220: }
221: dotrim(ent->log, ent->numlogs, ent->flags,
1.14 ! millert 222: ent->permissions, ent->uid, ent->gid, pid);
1.1 deraadt 223: } else {
224: if (verbose)
225: printf("--> skipping\n");
226: }
227: }
228: }
229:
1.9 downsj 230: void PRS(argc, argv)
1.1 deraadt 231: int argc;
232: char **argv;
233: {
234: int c;
235: char *p;
236:
1.12 kstailey 237: timenow = time(NULL);
1.1 deraadt 238: daytime = ctime(&timenow) + 4;
1.2 deraadt 239: daytime[15] = '\0';
1.1 deraadt 240:
241: /* Let's get our hostname */
242: (void) gethostname(hostname, sizeof(hostname));
243:
244: /* Truncate domain */
1.9 downsj 245: p = strchr(hostname, '.');
246: if (p)
1.1 deraadt 247: *p = '\0';
248:
249: optind = 1; /* Start options parsing */
1.10 downsj 250: while ((c = getopt(argc,argv,"nrvmf:t:")) != -1) {
1.1 deraadt 251: switch (c) {
252: case 'n':
253: noaction++; /* This implies needroot as off */
254: /* fall through */
255: case 'r':
256: needroot = 0;
257: break;
258: case 'v':
259: verbose++;
260: break;
261: case 'f':
262: conf = optarg;
263: break;
1.10 downsj 264: case 'm':
265: monitor++;
266: break;
1.1 deraadt 267: default:
268: usage();
269: }
270: }
1.9 downsj 271: }
1.1 deraadt 272:
1.9 downsj 273: void usage()
1.1 deraadt 274: {
1.14 ! millert 275: fprintf(stderr, "usage: %s [-nrvm] [-f config-file]\n", __progname);
! 276: exit(1);
1.1 deraadt 277: }
278:
279: /* Parse a configuration file and return a linked list of all the logs
280: * to process
281: */
282: struct conf_entry *parse_file()
283: {
284: FILE *f;
285: char line[BUFSIZ], *parse, *q;
286: char *errline, *group;
287: struct conf_entry *first = NULL;
288: struct conf_entry *working;
289: struct passwd *pass;
290: struct group *grp;
291:
292: if (strcmp(conf,"-"))
293: f = fopen(conf,"r");
294: else
295: f = stdin;
1.11 downsj 296: if (!f)
297: err(1, conf);
298:
1.1 deraadt 299: while (fgets(line,BUFSIZ,f)) {
300: if ((line[0]== '\n') || (line[0] == '#'))
301: continue;
302: errline = strdup(line);
1.11 downsj 303: if (errline == NULL)
304: err(1, "strdup");
1.1 deraadt 305: if (!first) {
306: working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 307: if (working == NULL)
308: err(1, "malloc");
1.1 deraadt 309: first = working;
310: } else {
311: working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11 downsj 312: if (working->next == NULL)
313: err(1, "malloc");
1.1 deraadt 314: working = working->next;
315: }
316:
317: q = parse = missing_field(sob(line),errline);
318: *(parse = son(line)) = '\0';
319: working->log = strdup(q);
1.11 downsj 320: if (working->log == NULL)
321: err(1, "strdup");
1.1 deraadt 322:
323: q = parse = missing_field(sob(++parse),errline);
324: *(parse = son(parse)) = '\0';
325: if ((group = strchr(q, '.')) != NULL) {
326: *group++ = '\0';
327: if (*q) {
1.10 downsj 328: if (!(isnumberstr(q))) {
1.11 downsj 329: if ((pass = getpwnam(q)) == NULL)
330: errx(1, "Error in config file; unknown user: %s", q);
1.1 deraadt 331: working->uid = pass->pw_uid;
332: } else
333: working->uid = atoi(q);
334: } else
335: working->uid = NONE;
336:
337: q = group;
338: if (*q) {
1.10 downsj 339: if (!(isnumberstr(q))) {
1.11 downsj 340: if ((grp = getgrnam(q)) == NULL)
341: errx(1, "Error in config file; unknown group: %s", q);
1.1 deraadt 342: working->gid = grp->gr_gid;
343: } else
344: working->gid = atoi(q);
345: } else
346: working->gid = NONE;
347:
348: q = parse = missing_field(sob(++parse),errline);
349: *(parse = son(parse)) = '\0';
1.11 downsj 350: } else
1.1 deraadt 351: working->uid = working->gid = NONE;
352:
1.11 downsj 353: if (!sscanf(q,"%o",&working->permissions))
354: errx(1, "Error in config file; bad permissions: %s", q);
1.1 deraadt 355:
356: q = parse = missing_field(sob(++parse),errline);
357: *(parse = son(parse)) = '\0';
1.11 downsj 358: if (!sscanf(q,"%d",&working->numlogs))
359: errx(1, "Error in config file; bad number: %s", q);
1.1 deraadt 360:
361: q = parse = missing_field(sob(++parse),errline);
362: *(parse = son(parse)) = '\0';
363: if (isdigit(*q))
364: working->size = atoi(q);
365: else
366: working->size = -1;
367:
368: q = parse = missing_field(sob(++parse),errline);
369: *(parse = son(parse)) = '\0';
370: if (isdigit(*q))
371: working->hours = atoi(q);
372: else
373: working->hours = -1;
374:
375: q = parse = sob(++parse); /* Optional field */
376: *(parse = son(parse)) = '\0';
377: working->flags = 0;
378: while (q && *q && !isspace(*q)) {
379: if ((*q == 'Z') || (*q == 'z'))
380: working->flags |= CE_COMPACT;
381: else if ((*q == 'B') || (*q == 'b'))
382: working->flags |= CE_BINARY;
1.10 downsj 383: else if ((*q == 'M') || (*q == 'm'))
384: working->flags |= CE_MONITOR;
1.11 downsj 385: else
386: errx(1, "Illegal flag in config file: %c", *q);
1.1 deraadt 387: q++;
388: }
1.10 downsj 389:
390: working->whom = NULL;
391: if (working->flags & CE_MONITOR) { /* Optional field */
392: q = parse = sob(++parse);
393: *(parse = son(parse)) = '\0';
394:
395: working->whom = strdup(q);
1.11 downsj 396: if (working->log == NULL)
397: err(1, "strdup");
1.10 downsj 398: }
1.14 ! millert 399:
! 400: working->pidfile = PIDFILE;
! 401: q = parse = sob(++parse); /* Optional field */
! 402: *(parse = son(parse)) = '\0';
! 403: if (q && *q != '\0') {
! 404: working->pidfile = strdup(q);
! 405: if (working->pidfile == NULL)
! 406: err(1, "strdup");
! 407: }
1.1 deraadt 408:
409: free(errline);
410: }
411: if (working)
1.12 kstailey 412: working->next = NULL;
1.1 deraadt 413: (void) fclose(f);
414: return(first);
415: }
416:
1.9 downsj 417: char *missing_field(p, errline)
1.1 deraadt 418: char *p,*errline;
419: {
420: if (!p || !*p) {
1.14 ! millert 421: warnx("Missing field in config file line:");
1.11 downsj 422: fputs(errline, stderr);
1.1 deraadt 423: exit(1);
424: }
425: return(p);
426: }
427:
1.14 ! millert 428: void dotrim(log, numdays, flags, perm, owner_uid, group_gid, daemon_pid)
1.1 deraadt 429: char *log;
430: int numdays;
431: int flags;
432: int perm;
433: int owner_uid;
434: int group_gid;
1.14 ! millert 435: int daemon_pid;
1.1 deraadt 436: {
1.7 deraadt 437: char file1[MAXPATHLEN], file2[MAXPATHLEN];
438: char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.1 deraadt 439: int fd;
440: struct stat st;
1.6 tholo 441: int days = numdays;
1.1 deraadt 442:
443: /* Remove oldest log */
444: (void) sprintf(file1,"%s.%d",log,numdays);
445: (void) strcpy(zfile1, file1);
446: (void) strcat(zfile1, COMPRESS_POSTFIX);
447:
448: if (noaction) {
449: printf("rm -f %s\n", file1);
450: printf("rm -f %s\n", zfile1);
451: } else {
452: (void) unlink(file1);
453: (void) unlink(zfile1);
454: }
455:
456: /* Move down log files */
457: while (numdays--) {
458: (void) strcpy(file2,file1);
459: (void) sprintf(file1,"%s.%d",log,numdays);
460: (void) strcpy(zfile1, file1);
461: (void) strcpy(zfile2, file2);
462: if (lstat(file1, &st)) {
463: (void) strcat(zfile1, COMPRESS_POSTFIX);
464: (void) strcat(zfile2, COMPRESS_POSTFIX);
465: if (lstat(zfile1, &st)) continue;
466: }
467: if (noaction) {
468: printf("mv %s %s\n",zfile1,zfile2);
469: printf("chmod %o %s\n", perm, zfile2);
470: printf("chown %d.%d %s\n",
471: owner_uid, group_gid, zfile2);
472: } else {
473: (void) rename(zfile1, zfile2);
474: (void) chmod(zfile2, perm);
475: (void) chown(zfile2, owner_uid, group_gid);
476: }
477: }
478: if (!noaction && !(flags & CE_BINARY))
479: (void) log_trim(log); /* Report the trimming to the old log */
480:
1.6 tholo 481: if (days == 0) {
1.5 deraadt 482: if (noaction)
483: printf("rm %s\n",log);
484: else
485: (void) unlink(log);
486: } else {
487: if (noaction)
488: printf("mv %s to %s\n",log,file1);
489: else
490: (void) rename(log,file1);
491: }
492:
1.1 deraadt 493: if (noaction)
494: printf("Start new log...");
495: else {
496: fd = creat(log,perm);
1.11 downsj 497: if (fd < 0)
1.13 mickey 498: err(1, "can't start \'%s\' log", log);
1.11 downsj 499: if (fchown(fd, owner_uid, group_gid))
1.13 mickey 500: err(1, "can't chown \'%s\' log file", log);
1.1 deraadt 501: (void) close(fd);
502: if (!(flags & CE_BINARY))
1.11 downsj 503: if (log_trim(log)) /* Add status message */
1.13 mickey 504: err(1, "can't add status message to log \'%s\'", log);
1.1 deraadt 505: }
506: if (noaction)
507: printf("chmod %o %s...",perm,log);
508: else
509: (void) chmod(log,perm);
510: if (noaction)
1.14 ! millert 511: printf("kill -HUP %d\n",daemon_pid);
! 512: else if (daemon_pid < MIN_PID || daemon_pid > MAX_PID)
! 513: warnx("preposterous process number: %d", daemon_pid);
! 514: else if (kill(daemon_pid,SIGHUP))
! 515: warnx("warning - could not HUP daemon");
1.1 deraadt 516: if (flags & CE_COMPACT) {
517: if (noaction)
518: printf("Compress %s.0\n",log);
519: else
520: compress_log(log);
521: }
522: }
523:
524: /* Log the fact that the logs were turned over */
1.9 downsj 525: int log_trim(log)
1.1 deraadt 526: char *log;
527: {
528: FILE *f;
529: if ((f = fopen(log,"a")) == NULL)
530: return(-1);
531: fprintf(f,"%s %s newsyslog[%d]: logfile turned over\n",
532: daytime, hostname, getpid());
533: if (fclose(f) == EOF) {
1.11 downsj 534: err(1, "log_trim: fclose");
1.1 deraadt 535: }
536: return(0);
537: }
538:
539: /* Fork of /usr/ucb/compress to compress the old log file */
1.9 downsj 540: void compress_log(log)
1.1 deraadt 541: char *log;
542: {
543: int pid;
1.7 deraadt 544: char tmp[MAXPATHLEN];
1.1 deraadt 545:
546: pid = fork();
547: (void) sprintf(tmp,"%s.0",log);
548: if (pid < 0) {
1.11 downsj 549: err(1, "fork");
1.1 deraadt 550: } else if (!pid) {
551: (void) execl(COMPRESS,"compress","-f",tmp,0);
1.11 downsj 552: err(1, COMPRESS);
1.1 deraadt 553: }
554: }
555:
556: /* Return size in kilobytes of a file */
557: int sizefile(file)
558: char *file;
559: {
560: struct stat sb;
561:
562: if (stat(file,&sb) < 0)
563: return(-1);
564: return(kbytes(dbtob(sb.st_blocks)));
565: }
566:
567: /* Return the age of old log file (file.0) */
568: int age_old_log(file)
569: char *file;
570: {
571: struct stat sb;
1.7 deraadt 572: char tmp[MAXPATHLEN];
1.1 deraadt 573:
574: (void) strcpy(tmp,file);
575: if (stat(strcat(tmp,".0"),&sb) < 0)
576: if (stat(strcat(tmp,COMPRESS_POSTFIX), &sb) < 0)
577: return(-1);
578: return( (int) (timenow - sb.st_mtime + 1800) / 3600);
579: }
580:
581: /* Skip Over Blanks */
582: char *sob(p)
583: register char *p;
584: {
585: while (p && *p && isspace(*p))
586: p++;
587: return(p);
588: }
589:
590: /* Skip Over Non-Blanks */
591: char *son(p)
592: register char *p;
593: {
594: while (p && *p && !isspace(*p))
595: p++;
596: return(p);
597: }
598:
599:
600: /* Check if string is actually a number */
601:
1.10 downsj 602: int isnumberstr(string)
603: char *string;
1.1 deraadt 604: {
605: while (*string != '\0') {
1.9 downsj 606: if (!isdigit(*string++))
607: return(0);
1.1 deraadt 608: }
609: return(1);
1.10 downsj 610: }
611:
612: void domonitor(log, whom)
613: char *log, *whom;
614: {
615: struct stat sb, tsb;
616: char *fname, *flog, *p, *rb = NULL;
617: FILE *fp;
618: off_t osize;
619: int rd;
620:
621: if (stat(log, &sb) < 0)
622: return;
623:
624: flog = strdup(log);
1.11 downsj 625: if (flog == NULL)
626: err(1, "strdup");
627:
1.10 downsj 628: for (p = flog; *p != '\0'; p++) {
629: if (*p == '/')
630: *p = '_';
631: }
632: fname = (char *) malloc(strlen(STATS_DIR) + strlen(flog) + 17);
1.11 downsj 633: if (fname == NULL)
634: err(1, "malloc");
635:
1.10 downsj 636: sprintf(fname, "%s/newsyslog.%s.size", STATS_DIR, flog);
637:
638: /* ..if it doesn't exist, simply record the current size. */
639: if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
640: goto update;
641:
642: fp = fopen(fname, "r");
643: if (fp == NULL) {
1.11 downsj 644: warn(fname);
1.10 downsj 645: goto cleanup;
646: }
647: #ifdef QUAD_OFF_T
648: if (fscanf(fp, "%qd\n", &osize) != 1) {
649: #else
650: if (fscanf(fp, "%ld\n", &osize) != 1) {
651: #endif /* QUAD_OFF_T */
652: fclose(fp);
653: goto update;
654: }
655:
656: fclose(fp);
657:
658: /* If the file is smaller, mark the entire thing as changed. */
659: if (sb.st_size < osize)
660: osize = 0;
661:
662: /* Now see if current size is larger. */
663: if (sb.st_size > osize) {
664: rb = (char *) malloc(sb.st_size - osize);
1.11 downsj 665: if (rb == NULL)
666: err(1, "malloc");
1.10 downsj 667:
668: /* Open logfile, seek. */
669: fp = fopen(log, "r");
670: if (fp == NULL) {
1.11 downsj 671: warn(log);
1.10 downsj 672: goto cleanup;
673: }
674: fseek(fp, osize, SEEK_SET);
675: rd = fread(rb, 1, sb.st_size - osize, fp);
676: if (rd < 1) {
1.11 downsj 677: warn("fread");
1.10 downsj 678: fclose(fp);
679: goto cleanup;
680: }
681:
682: /* Send message. */
683: fclose(fp);
684:
685: fp = openmail();
686: if (fp == NULL) {
1.11 downsj 687: warn("openmail");
1.10 downsj 688: goto cleanup;
689: }
690: fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
691: whom, log);
692: fwrite(rb, 1, rd, fp);
693: fputs("\n\n", fp);
694:
695: closemail(fp);
696: }
697: update:
698: /* Reopen for writing and update file. */
699: fp = fopen(fname, "w");
700: if (fp == NULL) {
1.11 downsj 701: warn(fname);
1.10 downsj 702: goto cleanup;
703: }
704: #ifdef QUAD_OFF_T
705: fprintf(fp, "%qd\n", sb.st_size);
706: #else
707: fprintf(fp, "%ld\n", sb.st_size);
708: #endif /* QUAD_OFF_T */
709: fclose(fp);
710:
711: cleanup:
712: free(flog);
713: free(fname);
714: if (rb != NULL)
715: free(rb);
716: }
717:
718: FILE *openmail()
719: {
720: char *cmdbuf;
721: FILE *ret;
722:
723: cmdbuf = (char *) malloc(strlen(SENDMAIL) + 3);
724: if (cmdbuf == NULL)
725: return(NULL);
726:
727: sprintf(cmdbuf, "%s -t", SENDMAIL);
728: ret = popen(cmdbuf, "w");
729:
730: free(cmdbuf);
731: return(ret);
732: }
733:
734: void closemail(pfp)
735: FILE *pfp;
736: {
737: pclose(pfp);
1.1 deraadt 738: }