[BACK]Return to newsyslog.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / newsyslog

Annotation of src/usr.bin/newsyslog/newsyslog.c, Revision 1.32

1.32    ! millert     1: /*     $OpenBSD: newsyslog.c,v 1.31 2000/06/10 02:49:41 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.32    ! millert    91: static char rcsid[] = "$OpenBSD: newsyslog.c,v 1.31 2000/06/10 02:49:41 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:                                }
1.31      millert   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:                 working->flags = 0;
1.32    ! millert   498:                 q = sob(++parse);      /* Optional field */
        !           499:                if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
        !           500:                    *q == 'M' || *q == 'm') {
        !           501:                        *(parse = son(q)) = '\0';
        !           502:                        while (*q) {
        !           503:                                switch (*q) {
        !           504:                                case 'Z':
        !           505:                                case 'z':
        !           506:                                        working->flags |= CE_COMPACT;
        !           507:                                        break;
        !           508:                                case 'B':
        !           509:                                case 'b':
        !           510:                                        working->flags |= CE_BINARY;
        !           511:                                        break;
        !           512:                                case 'M':
        !           513:                                case 'm':
        !           514:                                        working->flags |= CE_MONITOR;
        !           515:                                        break;
        !           516:                                default:
        !           517:                                        errx(1, "Illegal flag in config file: %c", *q);
        !           518:                                        break;
        !           519:                                }
        !           520:                                q++;
        !           521:                        }
        !           522:                } else
        !           523:                    parse--;    /* no flags so undo */
1.10      downsj    524:
                    525:                working->whom = NULL;
                    526:                if (working->flags & CE_MONITOR) {      /* Optional field */
                    527:                        q = parse = sob(++parse);
                    528:                        *(parse = son(parse)) = '\0';
                    529:
                    530:                        working->whom = strdup(q);
1.11      downsj    531:                        if (working->log == NULL)
                    532:                                err(1, "strdup");
1.10      downsj    533:                }
1.14      millert   534:
                    535:                working->pidfile = PIDFILE;
1.30      millert   536:                working->signal = SIGHUP;
                    537:                working->runcmd = NULL;
                    538:                for (;;) {
                    539:                        q = parse = sob(++parse);       /* Optional field */
                    540:                        if (q == NULL || *q == '\0')
                    541:                                break;
                    542:                        if (*q == '/') {
                    543:                                *(parse = son(parse)) = '\0';
                    544:                                if (strlen(q) >= MAXPATHLEN)
                    545:                                        errx(1, "%s: pathname too long", q);
                    546:                                working->pidfile = strdup(q);
                    547:                                if (working->pidfile == NULL)
                    548:                                        err(1, "strdup");
                    549:                        } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
                    550:                                *(parse = tmp) = '\0';
                    551:                                working->runcmd = strdup(++q);
                    552:                                if (working->runcmd == NULL)
                    553:                                        err(1, "strdup");
                    554:                        } else if (strncmp(q, "SIG", 3) == 0) {
                    555:                                int i;
                    556:
                    557:                                *(parse = son(parse)) = '\0';
                    558:                                for (i = 1; i < NSIG; i++) {
                    559:                                        if (!strcmp(sys_signame[i], q + 3)) {
                    560:                                                working->signal = i;
                    561:                                                break;
                    562:                                        }
                    563:                                }
                    564:                                if (i == NSIG)
                    565:                                        errx(1, "unknown signal: %s", q);
                    566:                        } else
                    567:                                errx(1, "unrecognized field: %s", q);
1.14      millert   568:                }
1.25      millert   569:
                    570:                /* Make sure we can't oflow MAXPATHLEN */
                    571:                if (asprintf(&tmp, "%s.%d%s", working->log, working->numlogs,
                    572:                    COMPRESS_POSTFIX) >= MAXPATHLEN)
                    573:                        errx(1, "%s: pathname too long", working->log);
1.1       deraadt   574:
1.25      millert   575:                 free(tmp);
1.1       deraadt   576:                 free(errline);
                    577:         }
                    578:         if (working)
1.12      kstailey  579:                 working->next = NULL;
1.25      millert   580:         (void)fclose(f);
1.1       deraadt   581:         return(first);
                    582: }
                    583:
1.25      millert   584: char *
                    585: missing_field(p, errline)
                    586:         char    *p;
                    587:        char    *errline;
1.1       deraadt   588: {
                    589:         if (!p || !*p) {
1.14      millert   590:                warnx("Missing field in config file line:");
1.11      downsj    591:                 fputs(errline, stderr);
1.1       deraadt   592:                 exit(1);
                    593:         }
                    594:         return(p);
                    595: }
                    596:
1.25      millert   597: void
1.26      millert   598: dotrim(log, numdays, flags, perm, owner_uid, group_gid)
1.1       deraadt   599:         char    *log;
                    600:         int     numdays;
                    601:         int     flags;
                    602:         int     perm;
1.25      millert   603:         uid_t   owner_uid;
                    604:         gid_t   group_gid;
1.1       deraadt   605: {
1.7       deraadt   606:         char    file1[MAXPATHLEN], file2[MAXPATHLEN];
                    607:         char    zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.1       deraadt   608:         int     fd;
                    609:         struct  stat st;
1.6       tholo     610:        int     days = numdays;
1.1       deraadt   611:
1.25      millert   612:         /* Remove oldest log (may not exist) */
                    613:         (void)sprintf(file1, "%s.%d", log, numdays);
                    614:         (void)strcpy(zfile1, file1);
                    615:         (void)strcat(zfile1, COMPRESS_POSTFIX);
1.1       deraadt   616:         if (noaction) {
1.26      millert   617:                 printf("rm -f %s %s\n", file1, zfile1);
1.1       deraadt   618:         } else {
1.25      millert   619:                 (void)unlink(file1);
                    620:                 (void)unlink(zfile1);
1.1       deraadt   621:         }
                    622:
                    623:         /* Move down log files */
                    624:         while (numdays--) {
1.25      millert   625:                 (void)strcpy(file2, file1);
                    626:                 (void)sprintf(file1, "%s.%d", log, numdays);
                    627:                 (void)strcpy(zfile1, file1);
                    628:                 (void)strcpy(zfile2, file2);
1.1       deraadt   629:                 if (lstat(file1, &st)) {
1.25      millert   630:                         (void)strcat(zfile1, COMPRESS_POSTFIX);
                    631:                         (void)strcat(zfile2, COMPRESS_POSTFIX);
                    632:                         if (lstat(zfile1, &st))
                    633:                                continue;
1.1       deraadt   634:                 }
                    635:                 if (noaction) {
1.25      millert   636:                         printf("mv %s %s\n", zfile1, zfile2);
1.1       deraadt   637:                         printf("chmod %o %s\n", perm, zfile2);
1.25      millert   638:                         printf("chown %d:%d %s\n",
1.1       deraadt   639:                                owner_uid, group_gid, zfile2);
                    640:                 } else {
1.25      millert   641:                         if (rename(zfile1, zfile2))
                    642:                                warn("can't mv %s to %s", zfile1, zfile2);
                    643:                         if (chmod(zfile2, perm))
                    644:                                warn("can't chmod %s", zfile2);
                    645:                         if (chown(zfile2, owner_uid, group_gid))
                    646:                                warn("can't chown %s", zfile2);
1.1       deraadt   647:                 }
                    648:         }
                    649:         if (!noaction && !(flags & CE_BINARY))
1.25      millert   650:                 (void)log_trim(log);  /* Report the trimming to the old log */
1.1       deraadt   651:
1.26      millert   652:        (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", log);
                    653:         if (noaction)  {
                    654:                 printf("Create new log file...\n");
                    655:         } else {
                    656:                 if ((fd = mkstemp(file2)) < 0)
                    657:                        err(1, "can't start '%s' log", file2);
                    658:                 if (fchown(fd, owner_uid, group_gid))
                    659:                        err(1, "can't chown '%s' log file", file2);
                    660:                 if (fchmod(fd, perm))
                    661:                        err(1, "can't chmod '%s' log file", file2);
                    662:                 (void)close(fd);
                    663:                /* Add status message */
                    664:                 if (!(flags & CE_BINARY) && log_trim(file2))
                    665:                        err(1, "can't add status message to log '%s'", file2);
                    666:         }
                    667:
1.6       tholo     668:        if (days == 0) {
1.5       deraadt   669:                if (noaction)
1.25      millert   670:                        printf("rm %s\n", log);
                    671:                else if (unlink(log))
                    672:                        warn("can't rm %s", log);
1.5       deraadt   673:        } else {
                    674:                if (noaction)
1.25      millert   675:                        printf("mv %s to %s\n", log, file1);
                    676:                else if (rename(log, file1))
                    677:                        warn("can't to mv %s to %s", log, file1);
1.5       deraadt   678:        }
                    679:
1.26      millert   680:        /* Now move the new log file into place */
                    681:        if (noaction)
                    682:                printf("mv %s to %s\n", file2, log);
                    683:        else if (rename(file2, log))
                    684:                warn("can't to mv %s to %s", file2, log);
1.1       deraadt   685: }
                    686:
                    687: /* Log the fact that the logs were turned over */
1.25      millert   688: int
                    689: log_trim(log)
1.17      deraadt   690:        char    *log;
1.1       deraadt   691: {
                    692:         FILE    *f;
1.25      millert   693:
                    694:         if ((f = fopen(log, "a")) == NULL)
1.1       deraadt   695:                 return(-1);
1.25      millert   696:         (void)fprintf(f, "%s %s newsyslog[%d]: logfile turned over\n",
                    697:            daytime, hostname, getpid());
1.24      millert   698:         if (fclose(f) == EOF)
1.11      downsj    699:                 err(1, "log_trim: fclose");
1.1       deraadt   700:         return(0);
                    701: }
                    702:
1.16      millert   703: /* Fork off compress or gzip to compress the old log file */
1.25      millert   704: void
                    705: compress_log(log)
1.1       deraadt   706:         char    *log;
                    707: {
1.25      millert   708:         pid_t   pid;
1.27      millert   709:        char    *base;
1.7       deraadt   710:         char    tmp[MAXPATHLEN];
1.1       deraadt   711:
1.27      millert   712:        if ((base = strrchr(COMPRESS, '/')) == NULL)
                    713:                base = COMPRESS;
                    714:        else
                    715:                base++;
                    716:        if (noaction) {
                    717:                printf("%s %s.0\n", base, log);
                    718:                return;
                    719:        }
1.1       deraadt   720:         pid = fork();
1.25      millert   721:         (void)sprintf(tmp, "%s.0", log);
1.1       deraadt   722:         if (pid < 0) {
1.11      downsj    723:                err(1, "fork");
1.1       deraadt   724:         } else if (!pid) {
1.27      millert   725:                 (void)execl(COMPRESS, base, "-f", tmp, 0);
1.16      millert   726:                warn(COMPRESS);
                    727:                _exit(1);
1.1       deraadt   728:         }
                    729: }
                    730:
                    731: /* Return size in kilobytes of a file */
1.25      millert   732: int
                    733: sizefile(file)
1.1       deraadt   734:         char    *file;
                    735: {
                    736:         struct stat sb;
                    737:
1.25      millert   738:         if (stat(file, &sb) < 0)
1.1       deraadt   739:                 return(-1);
1.25      millert   740:         return(sb.st_blocks / (1024.0 / DEV_BSIZE));
1.1       deraadt   741: }
                    742:
1.25      millert   743: /* Return the age (in hours) of old log file (file.0), or -1 if none */
                    744: int
                    745: age_old_log(file)
1.1       deraadt   746:         char    *file;
                    747: {
                    748:         struct stat sb;
1.7       deraadt   749:         char tmp[MAXPATHLEN];
1.1       deraadt   750:
1.25      millert   751:         (void)strcpy(tmp, file);
                    752:         if (stat(strcat(tmp, ".0"), &sb) < 0)
                    753:             if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0)
1.1       deraadt   754:                 return(-1);
1.24      millert   755:         return( (int) (timenow - sb.st_mtime + 1800) / 3600);
1.1       deraadt   756: }
                    757:
                    758: /* Skip Over Blanks */
1.25      millert   759: char *
                    760: sob(p)
1.1       deraadt   761:         register char   *p;
                    762: {
                    763:         while (p && *p && isspace(*p))
                    764:                 p++;
                    765:         return(p);
                    766: }
                    767:
                    768: /* Skip Over Non-Blanks */
1.25      millert   769: char *
                    770: son(p)
1.1       deraadt   771:         register char   *p;
                    772: {
                    773:         while (p && *p && !isspace(*p))
                    774:                 p++;
                    775:         return(p);
                    776: }
                    777:
                    778: /* Check if string is actually a number */
1.25      millert   779: int
                    780: isnumberstr(string)
1.10      downsj    781:        char *string;
1.1       deraadt   782: {
1.25      millert   783:         while (*string) {
1.9       downsj    784:             if (!isdigit(*string++))
                    785:                return(0);
1.1       deraadt   786:         }
                    787:         return(1);
1.10      downsj    788: }
                    789:
1.25      millert   790: void
                    791: domonitor(log, whom)
1.10      downsj    792:        char *log, *whom;
                    793: {
                    794:        struct stat sb, tsb;
                    795:        char *fname, *flog, *p, *rb = NULL;
                    796:        FILE *fp;
                    797:        off_t osize;
                    798:        int rd;
                    799:
                    800:        if (stat(log, &sb) < 0)
                    801:                return;
                    802:
                    803:        flog = strdup(log);
1.11      downsj    804:        if (flog == NULL)
                    805:                err(1, "strdup");
                    806:
1.10      downsj    807:        for (p = flog; *p != '\0'; p++) {
                    808:                if (*p == '/')
                    809:                        *p = '_';
                    810:        }
1.20      fgsch     811:        fname = (char *) malloc(sizeof(STATS_DIR) + strlen(flog) + 16);
1.11      downsj    812:        if (fname == NULL)
                    813:                err(1, "malloc");
                    814:
1.10      downsj    815:        sprintf(fname, "%s/newsyslog.%s.size", STATS_DIR, flog);
                    816:
                    817:        /* ..if it doesn't exist, simply record the current size. */
                    818:        if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
                    819:                goto update;
                    820:
                    821:        fp = fopen(fname, "r");
                    822:        if (fp == NULL) {
1.11      downsj    823:                warn(fname);
1.10      downsj    824:                goto cleanup;
                    825:        }
                    826: #ifdef QUAD_OFF_T
                    827:        if (fscanf(fp, "%qd\n", &osize) != 1) {
                    828: #else
                    829:        if (fscanf(fp, "%ld\n", &osize) != 1) {
                    830: #endif /* QUAD_OFF_T */
                    831:                fclose(fp);
                    832:                goto update;
                    833:        }
                    834:
                    835:        fclose(fp);
                    836:
                    837:        /* If the file is smaller, mark the entire thing as changed. */
                    838:        if (sb.st_size < osize)
                    839:                osize = 0;
                    840:
                    841:        /* Now see if current size is larger. */
                    842:        if (sb.st_size > osize) {
                    843:                rb = (char *) malloc(sb.st_size - osize);
1.11      downsj    844:                if (rb == NULL)
                    845:                        err(1, "malloc");
1.10      downsj    846:
                    847:                /* Open logfile, seek. */
                    848:                fp = fopen(log, "r");
                    849:                if (fp == NULL) {
1.11      downsj    850:                        warn(log);
1.10      downsj    851:                        goto cleanup;
                    852:                }
                    853:                fseek(fp, osize, SEEK_SET);
                    854:                rd = fread(rb, 1, sb.st_size - osize, fp);
                    855:                if (rd < 1) {
1.11      downsj    856:                        warn("fread");
1.10      downsj    857:                        fclose(fp);
                    858:                        goto cleanup;
                    859:                }
                    860:
                    861:                /* Send message. */
                    862:                fclose(fp);
                    863:
                    864:                fp = openmail();
                    865:                if (fp == NULL) {
1.11      downsj    866:                        warn("openmail");
1.10      downsj    867:                        goto cleanup;
                    868:                }
                    869:                fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
                    870:                    whom, log);
                    871:                fwrite(rb, 1, rd, fp);
                    872:                fputs("\n\n", fp);
                    873:
                    874:                closemail(fp);
                    875:        }
                    876: update:
                    877:        /* Reopen for writing and update file. */
                    878:        fp = fopen(fname, "w");
                    879:        if (fp == NULL) {
1.11      downsj    880:                warn(fname);
1.10      downsj    881:                goto cleanup;
                    882:        }
                    883: #ifdef QUAD_OFF_T
                    884:        fprintf(fp, "%qd\n", sb.st_size);
                    885: #else
                    886:        fprintf(fp, "%ld\n", sb.st_size);
                    887: #endif /* QUAD_OFF_T */
                    888:        fclose(fp);
                    889:
                    890: cleanup:
                    891:        free(flog);
                    892:        free(fname);
                    893:        if (rb != NULL)
                    894:                free(rb);
                    895: }
                    896:
1.25      millert   897: FILE *
                    898: openmail()
1.10      downsj    899: {
                    900:        char *cmdbuf;
                    901:        FILE *ret;
                    902:
1.20      fgsch     903:        cmdbuf = (char *) malloc(sizeof(SENDMAIL) + 3);
1.10      downsj    904:        if (cmdbuf == NULL)
                    905:                return(NULL);
                    906:
                    907:        sprintf(cmdbuf, "%s -t", SENDMAIL);
                    908:        ret = popen(cmdbuf, "w");
                    909:
                    910:        free(cmdbuf);
                    911:        return(ret);
                    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: {
                    925:        int status;
                    926:
                    927:        while (waitpid(-1, &status, WNOHANG) > 0)
                    928:                ;
1.1       deraadt   929: }