[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.44

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