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

1.55    ! millert     1: /*     $OpenBSD: newsyslog.c,v 1.54 2002/09/19 15:27:31 millert Exp $  */
1.10      downsj      2:
                      3: /*
1.48      millert     4:  * Copyright (c) 1999, 2002 Todd C. Miller <Todd.Miller@courtesan.com>
1.30      millert     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: /*
1.50      millert    67:  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
                     68:  *
                     69:  * Permission to use, copy, modify, and distribute this software
                     70:  * and its documentation for any purpose and without fee is
                     71:  * hereby granted, provided that the above copyright notice
                     72:  * appear in all copies and that both that copyright notice and
                     73:  * this permission notice appear in supporting documentation,
                     74:  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
                     75:  * used in advertising or publicity pertaining to distribution
                     76:  * of the software without specific, written prior permission.
                     77:  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
                     78:  * the suitability of this software for any purpose.  It is
                     79:  * provided "as is" without express or implied warranty.
                     80:  */
1.1       deraadt    81:
                     82: /*
                     83:  *      newsyslog - roll over selected logs at the appropriate time,
                     84:  *              keeping the a specified number of backup files around.
                     85:  *
                     86:  */
                     87:
                     88: #ifndef lint
1.55    ! millert    89: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.54 2002/09/19 15:27:31 millert Exp $";
1.1       deraadt    90: #endif /* not lint */
                     91:
                     92: #ifndef CONF
1.47      millert    93: #define CONF "/etc/newsyslog.conf" /* Configuration file */
1.1       deraadt    94: #endif
                     95: #ifndef PIDFILE
                     96: #define PIDFILE "/etc/syslog.pid"
                     97: #endif
                     98: #ifndef COMPRESS
1.47      millert    99: #define COMPRESS "/usr/bin/compress" /* File compression program */
1.1       deraadt   100: #endif
                    101: #ifndef COMPRESS_POSTFIX
                    102: #define COMPRESS_POSTFIX ".Z"
                    103: #endif
1.10      downsj    104: #ifndef STATS_DIR
                    105: #define STATS_DIR "/etc"
                    106: #endif
                    107: #ifndef SENDMAIL
                    108: #define SENDMAIL "/usr/lib/sendmail"
                    109: #endif
1.1       deraadt   110:
1.48      millert   111: #include <sys/param.h>
                    112: #include <sys/stat.h>
1.9       downsj    113: #include <sys/time.h>
                    114: #include <sys/wait.h>
1.48      millert   115:
1.1       deraadt   116: #include <ctype.h>
1.48      millert   117: #include <err.h>
                    118: #include <errno.h>
1.9       downsj    119: #include <fcntl.h>
1.1       deraadt   120: #include <grp.h>
1.47      millert   121: #include <limits.h>
1.48      millert   122: #include <pwd.h>
                    123: #include <signal.h>
                    124: #include <stdio.h>
                    125: #include <stdlib.h>
                    126: #include <string.h>
1.9       downsj    127: #include <unistd.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.45      wcobb     134: #define CE_FOLLOW      0x10            /* Follow symbolic links */
1.35      deraadt   135:
1.49      millert   136: #define        MIN_PID         4               /* Don't touch pids lower than this */
                    137: #define        MIN_SIZE        512             /* Don't rotate if smaller than this */
                    138:
1.50      millert   139: #define        DPRINTF(x)      do { if (verbose) printf x ; } while (0)
                    140:
1.1       deraadt   141: struct conf_entry {
1.35      deraadt   142:        char    *log;           /* Name of the log */
1.51      millert   143:        char    *logbase;       /* Basename of the log */
                    144:        char    *backdir;       /* Directory in which to store backups */
1.35      deraadt   145:        uid_t   uid;            /* Owner of log */
                    146:        gid_t   gid;            /* Group of log */
                    147:        int     numlogs;        /* Number of logs to keep */
                    148:        int     size;           /* Size cutoff to trigger trimming the log */
                    149:        int     hours;          /* Hours between log trimming */
                    150:        int     permissions;    /* File permissions on the log */
1.30      millert   151:        int     signal;         /* Signal to send (defaults to SIGHUP) */
1.35      deraadt   152:        int     flags;          /* Flags (CE_COMPACT & CE_BINARY)  */
1.10      downsj    153:        char    *whom;          /* Whom to notify if logfile changes */
1.30      millert   154:        char    *pidfile;       /* Path to file containg pid to signal */
                    155:        char    *runcmd;        /* Command to run instead of sending a signal */
1.35      deraadt   156:        struct conf_entry *next; /* Linked list pointer */
1.1       deraadt   157: };
                    158:
1.30      millert   159: struct pidinfo {
                    160:        char    *file;
                    161:        int     signal;
                    162: };
                    163:
1.52      millert   164: int    verbose = 0;            /* Print out what's going on */
                    165: int    needroot = 1;           /* Root privs are necessary */
                    166: int    noaction = 0;           /* Don't do anything, just show it */
1.33      millert   167: int    monitormode = 0;        /* Don't do monitoring by default */
1.52      millert   168: int    force = 0;              /* Force the logs to be rotated */
                    169: char   *conf = CONF;           /* Configuration file to use */
                    170: time_t timenow;
                    171: char   hostname[MAXHOSTNAMELEN]; /* hostname */
                    172: char   *daytime;               /* timenow in human readable form */
                    173: char   *arcdir;                /* dir to put archives in (if it exists) */
1.1       deraadt   174:
1.43      millert   175: void do_entry(struct conf_entry *);
1.48      millert   176: void parse_args(int, char **);
1.43      millert   177: void usage(void);
                    178: struct conf_entry *parse_file(int *);
                    179: char *missing_field(char *, char *);
1.51      millert   180: void dotrim(struct conf_entry *);
1.43      millert   181: int log_trim(char *);
1.51      millert   182: void compress_log(struct conf_entry *);
1.43      millert   183: int sizefile(char *);
1.51      millert   184: int age_old_log(struct conf_entry *);
1.43      millert   185: char *sob(char *);
                    186: char *son(char *);
                    187: int isnumberstr(char *);
                    188: void domonitor(char *, char *);
                    189: FILE *openmail(void);
                    190: void child_killer(int);
                    191: void run_command(char *);
                    192: void send_signal(char *, int);
1.1       deraadt   193:
1.25      millert   194: int
1.48      millert   195: main(int argc, char **argv)
1.1       deraadt   196: {
1.53      millert   197:        struct conf_entry *p, *q, *x, *y;
1.30      millert   198:        struct pidinfo *pidlist, *pl;
1.53      millert   199:        char **av;
1.28      millert   200:        int status, listlen;
1.35      deraadt   201:
1.48      millert   202:        parse_args(argc, argv);
1.53      millert   203:        argc -= optind;
                    204:        argv += optind;
                    205:
1.35      deraadt   206:        if (needroot && getuid() && geteuid())
1.11      downsj    207:                errx(1, "You must be root.");
1.53      millert   208:
                    209:        p = parse_file(&listlen);
                    210:        if (argc > 0) {
                    211:                /* Only rotate specified files. */
                    212:                x = y = NULL;
                    213:                listlen = 0;
                    214:                for (av = argv; *av; av++) {
                    215:                        for (q = p; q; q = q->next)
                    216:                                if (strcmp(*av, q->log) == 0) {
                    217:                                        if (x == NULL)
                    218:                                                x = y = q;
                    219:                                        else {
                    220:                                                y->next = q;
                    221:                                                y = q;
                    222:                                        }
                    223:                                        listlen++;
                    224:                                        break;
                    225:                                }
                    226:                        if (q == NULL)
                    227:                                warnx("%s is not listed in %s", *av, conf);
                    228:                }
                    229:                if (x == NULL)
                    230:                        errx(1, "no specified log files found in %s", conf);
                    231:                y->next = NULL;
                    232:                p = x;
                    233:        }
1.26      millert   234:
1.46      aaron     235:        pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30      millert   236:        if (pidlist == NULL)
1.28      millert   237:                err(1, "calloc");
                    238:
1.53      millert   239:        signal(SIGCHLD, child_killer);
                    240:
1.26      millert   241:        /* Step 1, rotate all log files */
1.53      millert   242:        for (q = p; q; q = q->next)
1.35      deraadt   243:                do_entry(q);
1.26      millert   244:
1.28      millert   245:        /* Step 2, make a list of unique pid files */
1.30      millert   246:        for (q = p, pl = pidlist; q; ) {
1.28      millert   247:                if (q->flags & CE_ROTATED) {
1.30      millert   248:                        struct pidinfo *pltmp;
1.28      millert   249:
1.30      millert   250:                        for (pltmp = pidlist; pltmp < pl; pltmp++) {
1.55    ! millert   251:                                if ((q->pidfile &&
        !           252:                                    strcmp(pltmp->file, q->pidfile) == 0 &&
        !           253:                                    pltmp->signal == q->signal) ||
        !           254:                                    (q->runcmd &&
1.30      millert   255:                                    strcmp(q->runcmd, pltmp->file) == 0))
1.28      millert   256:                                        break;
1.30      millert   257:                        }
                    258:                        if (pltmp == pl) {      /* unique entry */
                    259:                                if (q->runcmd) {
                    260:                                        pl->file = q->runcmd;
                    261:                                        pl->signal = -1;
                    262:                                } else {
                    263:                                        pl->file = q->pidfile;
                    264:                                        pl->signal = q->signal;
                    265:                                }
1.31      millert   266:                                pl++;
1.30      millert   267:                        }
1.28      millert   268:                }
1.35      deraadt   269:                q = q->next;
                    270:        }
1.26      millert   271:
1.30      millert   272:        /* Step 3, send a signal or run a command */
                    273:        for (pl = pidlist; pl->file; pl++) {
                    274:                if (pl->signal == -1)
                    275:                        run_command(pl->file);
                    276:                else
                    277:                        send_signal(pl->file, pl->signal);
                    278:        }
1.28      millert   279:        if (!noaction)
                    280:                sleep(5);
                    281:
                    282:        /* Step 4, compress the log.0 file if configured to do so and free */
1.35      deraadt   283:        while (p) {
1.26      millert   284:                if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51      millert   285:                        compress_log(p);
1.26      millert   286:                q = p;
1.35      deraadt   287:                p = p->next;
                    288:                free(q);
                    289:        }
1.16      millert   290:
                    291:        /* Wait for children to finish, then exit */
                    292:        while (waitpid(-1, &status, 0) != -1)
                    293:                ;
1.35      deraadt   294:        exit(0);
1.1       deraadt   295: }
                    296:
1.25      millert   297: void
1.48      millert   298: do_entry(struct conf_entry *ent)
1.1       deraadt   299: {
1.48      millert   300:        int modtime, size;
                    301:        struct stat sb;
1.45      wcobb     302:
1.51      millert   303:        if (lstat(ent->log, &sb) != 0)
1.50      millert   304:                return;
                    305:        if (!S_ISREG(sb.st_mode) &&
                    306:            (!S_ISLNK(sb.st_mode) || !(ent->flags & CE_FOLLOW))) {
                    307:                DPRINTF(("--> not a regular file, skipping\n"));
                    308:                return;
1.45      wcobb     309:        }
1.14      millert   310:
1.50      millert   311:        DPRINTF(("%s <%d%s%s%s>: ", ent->log, ent->numlogs,
1.51      millert   312:            (ent->flags & CE_COMPACT) ? "Z" : "",
                    313:            (ent->flags & CE_BINARY) ? "B" : "",
                    314:            (ent->flags & CE_FOLLOW) ? "F" : ""));
1.50      millert   315:
1.35      deraadt   316:        size = sizefile(ent->log);
1.51      millert   317:        modtime = age_old_log(ent);
1.35      deraadt   318:        if (size < 0) {
1.50      millert   319:                DPRINTF(("does not exist.\n"));
1.35      deraadt   320:        } else {
1.50      millert   321:                if (ent->size > 0)
                    322:                        DPRINTF(("size (Kb): %d [%d] ", size, ent->size));
                    323:                if (ent->hours > 0)
                    324:                        DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.33      millert   325:                if (monitormode && ent->flags & CE_MONITOR)
1.10      downsj    326:                        domonitor(ent->log, ent->whom);
1.52      millert   327:                if (!monitormode && (force ||
                    328:                    (ent->size > 0 && size >= ent->size) ||
1.49      millert   329:                    (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
                    330:                    && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50      millert   331:                        DPRINTF(("--> trimming log....\n"));
1.19      kstailey  332:                        if (noaction && !verbose)
1.50      millert   333:                                printf("%s <%d%s%s%s>\n", ent->log,
                    334:                                    ent->numlogs,
                    335:                                    (ent->flags & CE_COMPACT) ? "Z" : "",
                    336:                                    (ent->flags & CE_BINARY) ? "B" : "",
                    337:                                    (ent->flags & CE_FOLLOW) ? "F" : "");
1.51      millert   338:                        dotrim(ent);
1.26      millert   339:                        ent->flags |= CE_ROTATED;
1.50      millert   340:                } else
                    341:                        DPRINTF(("--> skipping\n"));
1.35      deraadt   342:        }
1.1       deraadt   343: }
                    344:
1.30      millert   345: /* Run the specified command */
                    346: void
1.48      millert   347: run_command(char *cmd)
1.30      millert   348: {
                    349:        if (noaction)
1.55    ! millert   350:                (void)printf("run %s\n", cmd);
1.30      millert   351:        else
                    352:                system(cmd);
                    353: }
                    354:
                    355: /* Send a signal to the pid specified by pidfile */
1.26      millert   356: void
1.48      millert   357: send_signal(char *pidfile, int signal)
1.26      millert   358: {
1.48      millert   359:        pid_t pid;
                    360:        FILE *f;
                    361:        char line[BUFSIZ], *ep, *err;
1.40      deraadt   362:        unsigned long ulval;
1.26      millert   363:
1.35      deraadt   364:        if ((f = fopen(pidfile, "r")) == NULL) {
1.26      millert   365:                warn("can't open %s", pidfile);
                    366:                return;
                    367:        }
                    368:
1.48      millert   369:        pid = 0;
1.42      millert   370:        errno = 0;
                    371:        err = NULL;
1.40      deraadt   372:        if (fgets(line, sizeof(line), f)) {
                    373:                ulval = strtoul(line, &ep, 10);
1.41      marc      374:                if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40      deraadt   375:                        err = "invalid number in";
                    376:                else if (errno == ERANGE && ulval == ULONG_MAX)
                    377:                        err = "out of range number in";
                    378:                else if (ulval == 0)
                    379:                        err = "no number in";
                    380:                else if (ulval < MIN_PID)
                    381:                        err = "preposterous process number in";
                    382:                else
                    383:                        pid = ulval;
1.42      millert   384:        } else {
                    385:                if (errno == 0)
                    386:                        err = "empty";
                    387:                else
                    388:                        err = "error reading";
1.40      deraadt   389:        }
1.26      millert   390:        (void)fclose(f);
                    391:
1.40      deraadt   392:        if (err)
                    393:                warnx("%s pid file: %s", err, pidfile);
                    394:        else if (noaction)
1.44      mpech     395:                (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30      millert   396:        else if (kill(pid, signal))
                    397:                warnx("warning - could not send SIG%s to daemon",
                    398:                    sys_signame[signal]);
1.26      millert   399: }
                    400:
1.25      millert   401: void
1.48      millert   402: parse_args(int argc, char **argv)
1.1       deraadt   403: {
1.48      millert   404:        int ch;
                    405:        char *p;
1.1       deraadt   406:
1.35      deraadt   407:        timenow = time(NULL);
                    408:        daytime = ctime(&timenow) + 4;
                    409:        daytime[15] = '\0';
1.1       deraadt   410:
1.35      deraadt   411:        /* Let's get our hostname */
                    412:        (void)gethostname(hostname, sizeof(hostname));
1.1       deraadt   413:
                    414:        /* Truncate domain */
1.48      millert   415:        if ((p = strchr(hostname, '.')) != NULL)
1.1       deraadt   416:                *p = '\0';
                    417:
1.52      millert   418:        while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48      millert   419:                switch (ch) {
1.51      millert   420:                case 'a':
                    421:                        arcdir = optarg;
                    422:                        break;
1.35      deraadt   423:                case 'n':
                    424:                        noaction++; /* This implies needroot as off */
                    425:                        /* fall through */
                    426:                case 'r':
                    427:                        needroot = 0;
                    428:                        break;
                    429:                case 'v':
                    430:                        verbose++;
                    431:                        break;
                    432:                case 'f':
                    433:                        conf = optarg;
                    434:                        break;
1.10      downsj    435:                case 'm':
1.33      millert   436:                        monitormode++;
1.10      downsj    437:                        break;
1.52      millert   438:                case 'F':
                    439:                        force++;
                    440:                        break;
1.35      deraadt   441:                default:
                    442:                        usage();
                    443:                }
                    444:        }
1.52      millert   445:        if (monitormode && force)
                    446:                errx(1, "cannot specify both -m and -F flags");
1.9       downsj    447: }
1.1       deraadt   448:
1.25      millert   449: void
1.48      millert   450: usage(void)
1.1       deraadt   451: {
1.25      millert   452:        extern const char *__progname;
                    453:
1.52      millert   454:        (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53      millert   455:            "[-f config_file] [log ...]\n", __progname);
1.14      millert   456:        exit(1);
1.1       deraadt   457: }
                    458:
1.52      millert   459: /*
                    460:  * Parse a configuration file and return a linked list of all the logs
1.1       deraadt   461:  * to process
                    462:  */
1.25      millert   463: struct conf_entry *
1.48      millert   464: parse_file(int *nentries)
1.1       deraadt   465: {
1.48      millert   466:        FILE *f;
                    467:        char line[BUFSIZ], *parse, *q, *errline, *group, *tmp;
1.35      deraadt   468:        struct conf_entry *first = NULL;
                    469:        struct conf_entry *working = NULL;
1.48      millert   470:        struct passwd *pwd;
1.35      deraadt   471:        struct group *grp;
1.51      millert   472:        struct stat sb;
1.1       deraadt   473:
1.35      deraadt   474:        if (strcmp(conf, "-") == 0)
                    475:                f = stdin;
1.48      millert   476:        else if ((f = fopen(conf, "r")) == NULL)
                    477:                err(1, "can't open %s", conf);
1.11      downsj    478:
1.28      millert   479:        *nentries = 0;
1.35      deraadt   480:        while (fgets(line, sizeof(line), f)) {
1.48      millert   481:                tmp = sob(line);
1.54      millert   482:                if (*tmp == '\0' || *tmp == '#')
1.35      deraadt   483:                        continue;
1.48      millert   484:                errline = strdup(tmp);
1.11      downsj    485:                if (errline == NULL)
                    486:                        err(1, "strdup");
1.28      millert   487:                (*nentries)++;
1.35      deraadt   488:                if (!first) {
                    489:                        working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    490:                        if (working == NULL)
                    491:                                err(1, "malloc");
1.35      deraadt   492:                        first = working;
                    493:                } else {
                    494:                        working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    495:                        if (working->next == NULL)
                    496:                                err(1, "malloc");
1.35      deraadt   497:                        working = working->next;
                    498:                }
1.1       deraadt   499:
1.35      deraadt   500:                q = parse = missing_field(sob(line), errline);
                    501:                *(parse = son(line)) = '\0';
                    502:                working->log = strdup(q);
1.11      downsj    503:                if (working->log == NULL)
                    504:                        err(1, "strdup");
1.1       deraadt   505:
1.51      millert   506:                if ((working->logbase = strrchr(working->log, '/')) != NULL)
                    507:                        working->logbase++;
                    508:
1.35      deraadt   509:                q = parse = missing_field(sob(++parse), errline);
                    510:                *(parse = son(parse)) = '\0';
1.25      millert   511:                if ((group = strchr(q, '.')) != NULL) {
                    512:                        *group++ = '\0';
                    513:                        if (*q) {
                    514:                                if (!(isnumberstr(q))) {
1.48      millert   515:                                        if ((pwd = getpwnam(q)) == NULL)
1.25      millert   516:                                                errx(1, "Error in config file; unknown user: %s", q);
1.48      millert   517:                                        working->uid = pwd->pw_uid;
1.25      millert   518:                                } else
                    519:                                        working->uid = atoi(q);
                    520:                        } else
1.50      millert   521:                                working->uid = (uid_t)-1;
1.25      millert   522:
                    523:                        q = group;
                    524:                        if (*q) {
                    525:                                if (!(isnumberstr(q))) {
                    526:                                        if ((grp = getgrnam(q)) == NULL)
                    527:                                                errx(1, "Error in config file; unknown group: %s", q);
                    528:                                        working->gid = grp->gr_gid;
                    529:                                } else
                    530:                                        working->gid = atoi(q);
                    531:                        } else
1.50      millert   532:                                working->gid = (gid_t)-1;
1.25      millert   533:
                    534:                        q = parse = missing_field(sob(++parse), errline);
                    535:                        *(parse = son(parse)) = '\0';
1.50      millert   536:                } else {
                    537:                        working->uid = (uid_t)-1;
                    538:                        working->gid = (gid_t)-1;
                    539:                }
1.1       deraadt   540:
1.35      deraadt   541:                if (!sscanf(q, "%o", &working->permissions))
1.11      downsj    542:                        errx(1, "Error in config file; bad permissions: %s", q);
1.1       deraadt   543:
1.35      deraadt   544:                q = parse = missing_field(sob(++parse), errline);
                    545:                *(parse = son(parse)) = '\0';
                    546:                if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.11      downsj    547:                        errx(1, "Error in config file; bad number: %s", q);
1.1       deraadt   548:
1.35      deraadt   549:                q = parse = missing_field(sob(++parse), errline);
                    550:                *(parse = son(parse)) = '\0';
                    551:                if (isdigit(*q))
                    552:                        working->size = atoi(q);
                    553:                else
                    554:                        working->size = -1;
                    555:
                    556:                q = parse = missing_field(sob(++parse), errline);
                    557:                *(parse = son(parse)) = '\0';
                    558:                if (isdigit(*q))
                    559:                        working->hours = atoi(q);
                    560:                else
                    561:                        working->hours = -1;
1.1       deraadt   562:
1.35      deraadt   563:                working->flags = 0;
                    564:                q = sob(++parse);       /* Optional field */
1.32      millert   565:                if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
                    566:                    *q == 'M' || *q == 'm') {
                    567:                        *(parse = son(q)) = '\0';
                    568:                        while (*q) {
                    569:                                switch (*q) {
                    570:                                case 'Z':
                    571:                                case 'z':
                    572:                                        working->flags |= CE_COMPACT;
                    573:                                        break;
                    574:                                case 'B':
                    575:                                case 'b':
                    576:                                        working->flags |= CE_BINARY;
                    577:                                        break;
                    578:                                case 'M':
                    579:                                case 'm':
                    580:                                        working->flags |= CE_MONITOR;
1.45      wcobb     581:                                        break;
                    582:                                case 'F':
                    583:                                case 'f':
                    584:                                        working->flags |= CE_FOLLOW;
1.32      millert   585:                                        break;
                    586:                                default:
                    587:                                        errx(1, "Illegal flag in config file: %c", *q);
                    588:                                        break;
                    589:                                }
                    590:                                q++;
                    591:                        }
                    592:                } else
1.40      deraadt   593:                        parse--;        /* no flags so undo */
1.10      downsj    594:
                    595:                working->whom = NULL;
                    596:                if (working->flags & CE_MONITOR) {      /* Optional field */
                    597:                        q = parse = sob(++parse);
                    598:                        *(parse = son(parse)) = '\0';
                    599:
                    600:                        working->whom = strdup(q);
1.11      downsj    601:                        if (working->log == NULL)
                    602:                                err(1, "strdup");
1.10      downsj    603:                }
1.14      millert   604:
                    605:                working->pidfile = PIDFILE;
1.30      millert   606:                working->signal = SIGHUP;
                    607:                working->runcmd = NULL;
                    608:                for (;;) {
                    609:                        q = parse = sob(++parse);       /* Optional field */
                    610:                        if (q == NULL || *q == '\0')
                    611:                                break;
                    612:                        if (*q == '/') {
                    613:                                *(parse = son(parse)) = '\0';
                    614:                                if (strlen(q) >= MAXPATHLEN)
                    615:                                        errx(1, "%s: pathname too long", q);
                    616:                                working->pidfile = strdup(q);
                    617:                                if (working->pidfile == NULL)
                    618:                                        err(1, "strdup");
                    619:                        } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
                    620:                                *(parse = tmp) = '\0';
                    621:                                working->runcmd = strdup(++q);
                    622:                                if (working->runcmd == NULL)
                    623:                                        err(1, "strdup");
1.55    ! millert   624:                                working->pidfile = NULL;
        !           625:                                working->signal = -1;
1.30      millert   626:                        } else if (strncmp(q, "SIG", 3) == 0) {
                    627:                                int i;
                    628:
                    629:                                *(parse = son(parse)) = '\0';
                    630:                                for (i = 1; i < NSIG; i++) {
                    631:                                        if (!strcmp(sys_signame[i], q + 3)) {
                    632:                                                working->signal = i;
                    633:                                                break;
                    634:                                        }
                    635:                                }
                    636:                                if (i == NSIG)
                    637:                                        errx(1, "unknown signal: %s", q);
                    638:                        } else
                    639:                                errx(1, "unrecognized field: %s", q);
1.14      millert   640:                }
1.51      millert   641:                free(errline);
                    642:
                    643:                /* If there is an arcdir, set working->backdir. */
                    644:                if (arcdir != NULL && working->logbase != NULL) {
                    645:                        if (*arcdir == '/') {
                    646:                                /* Fully qualified arcdir */
                    647:                                working->backdir = arcdir;
                    648:                        } else {
                    649:                                /* arcdir is relative to log's parent dir */
                    650:                                *(working->logbase - 1) = '\0';
                    651:                                if ((asprintf(&working->backdir, "%s/%s",
                    652:                                    working->log, arcdir)) == -1)
                    653:                                        err(1, "malloc");
                    654:                                *(working->logbase - 1) = '/';
                    655:                        }
                    656:                        /* Ignore arcdir if it doesn't exist. */
                    657:                        if (stat(working->backdir, &sb) != 0 ||
                    658:                            !S_ISDIR(sb.st_mode)) {
                    659:                                if (working->backdir != arcdir)
                    660:                                        free(working->backdir);
                    661:                                working->backdir = NULL;
                    662:                        }
                    663:                } else
                    664:                        working->backdir = NULL;
1.25      millert   665:
                    666:                /* Make sure we can't oflow MAXPATHLEN */
1.51      millert   667:                if (working->backdir != NULL) {
                    668:                        if (snprintf(line, sizeof(line), "%s/%s.%d%s",
                    669:                            working->backdir, working->logbase,
                    670:                            working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
                    671:                                errx(1, "%s: pathname too long", working->log);
                    672:                } else {
                    673:                        if (snprintf(line, sizeof(line), "%s.%d%s",
                    674:                            working->log, working->numlogs, COMPRESS_POSTFIX)
                    675:                            >= MAXPATHLEN)
                    676:                                errx(1, "%s: pathname too long", working->log);
                    677:                }
1.35      deraadt   678:        }
                    679:        if (working)
                    680:                working->next = NULL;
                    681:        (void)fclose(f);
1.48      millert   682:        return (first);
1.1       deraadt   683: }
                    684:
1.25      millert   685: char *
1.48      millert   686: missing_field(char *p, char *errline)
1.1       deraadt   687: {
1.35      deraadt   688:        if (!p || !*p) {
1.14      millert   689:                warnx("Missing field in config file line:");
1.35      deraadt   690:                fputs(errline, stderr);
                    691:                exit(1);
                    692:        }
1.48      millert   693:        return (p);
1.1       deraadt   694: }
                    695:
1.25      millert   696: void
1.51      millert   697: dotrim(struct conf_entry *ent)
1.35      deraadt   698: {
                    699:        char    file1[MAXPATHLEN], file2[MAXPATHLEN];
                    700:        char    zfile1[MAXPATHLEN], zfile2[MAXPATHLEN];
1.51      millert   701:        char    oldlog[MAXPATHLEN];
1.35      deraadt   702:        int     fd;
1.48      millert   703:        struct  stat sb;
1.51      millert   704:        int     numdays = ent->numlogs;
                    705:
                    706:        /* Is there a separate backup dir? */
                    707:        if (ent->backdir != NULL)
                    708:                snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
                    709:                    ent->logbase);
                    710:        else
                    711:                strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1       deraadt   712:
1.35      deraadt   713:        /* Remove oldest log (may not exist) */
1.51      millert   714:        (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
                    715:        (void)snprintf(zfile1, sizeof(zfile1), "%s.%d%s", oldlog, numdays,
1.35      deraadt   716:            COMPRESS_POSTFIX);
                    717:
                    718:        if (noaction) {
1.50      millert   719:                printf("\trm -f %s %s\n", file1, zfile1);
1.35      deraadt   720:        } else {
                    721:                (void)unlink(file1);
                    722:                (void)unlink(zfile1);
                    723:        }
                    724:
                    725:        /* Move down log files */
                    726:        while (numdays--) {
1.48      millert   727:                (void)strlcpy(file2, file1, sizeof(file2));
1.51      millert   728:                (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.48      millert   729:                (void)strlcpy(zfile1, file1, sizeof(zfile1));
                    730:                (void)strlcpy(zfile2, file2, sizeof(zfile2));
                    731:                if (lstat(file1, &sb)) {
                    732:                        (void)strlcat(zfile1, COMPRESS_POSTFIX, sizeof(zfile1));
                    733:                        (void)strlcat(zfile2, COMPRESS_POSTFIX, sizeof(zfile2));
                    734:                        if (lstat(zfile1, &sb))
1.35      deraadt   735:                                continue;
                    736:                }
                    737:                if (noaction) {
1.50      millert   738:                        printf("\tmv %s %s\n", zfile1, zfile2);
1.51      millert   739:                        printf("\tchmod %o %s\n", ent->permissions, zfile2);
                    740:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50      millert   741:                                printf("\tchown %u:%u %s\n",
1.51      millert   742:                                    ent->uid, ent->gid, zfile2);
1.35      deraadt   743:                } else {
                    744:                        if (rename(zfile1, zfile2))
1.25      millert   745:                                warn("can't mv %s to %s", zfile1, zfile2);
1.51      millert   746:                        if (chmod(zfile2, ent->permissions))
1.25      millert   747:                                warn("can't chmod %s", zfile2);
1.51      millert   748:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                    749:                                if (chown(zfile2, ent->uid, ent->gid))
1.50      millert   750:                                        warn("can't chown %s", zfile2);
1.35      deraadt   751:                }
                    752:        }
1.51      millert   753:        if (!noaction && !(ent->flags & CE_BINARY))
                    754:                (void)log_trim(ent->log);  /* Report the trimming to the old log */
1.1       deraadt   755:
1.51      millert   756:        (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35      deraadt   757:        if (noaction)  {
1.50      millert   758:                printf("\tmktemp %s\n", file2);
1.35      deraadt   759:        } else {
                    760:                if ((fd = mkstemp(file2)) < 0)
1.26      millert   761:                        err(1, "can't start '%s' log", file2);
1.51      millert   762:                if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                    763:                        if (fchown(fd, ent->uid, ent->gid))
                    764:                            err(1, "can't chown '%s' log file", file2);
                    765:                if (fchmod(fd, ent->permissions))
1.26      millert   766:                        err(1, "can't chmod '%s' log file", file2);
1.35      deraadt   767:                (void)close(fd);
1.26      millert   768:                /* Add status message */
1.51      millert   769:                if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26      millert   770:                        err(1, "can't add status message to log '%s'", file2);
1.35      deraadt   771:        }
1.26      millert   772:
1.51      millert   773:        if (ent->numlogs == 0) {
1.5       deraadt   774:                if (noaction)
1.51      millert   775:                        printf("\trm %s\n", ent->log);
                    776:                else if (unlink(ent->log))
                    777:                        warn("can't rm %s", ent->log);
1.5       deraadt   778:        } else {
1.51      millert   779:                if (noaction)
                    780:                        printf("\tmv %s to %s\n", ent->log, file1);
                    781:                else if (rename(ent->log, file1))
                    782:                        warn("can't to mv %s to %s", ent->log, file1);
1.5       deraadt   783:        }
                    784:
1.26      millert   785:        /* Now move the new log file into place */
                    786:        if (noaction)
1.51      millert   787:                printf("\tmv %s to %s\n", file2, ent->log);
                    788:        else if (rename(file2, ent->log))
                    789:                warn("can't to mv %s to %s", file2, ent->log);
1.1       deraadt   790: }
                    791:
                    792: /* Log the fact that the logs were turned over */
1.25      millert   793: int
1.48      millert   794: log_trim(char *log)
1.1       deraadt   795: {
1.35      deraadt   796:        FILE    *f;
1.25      millert   797:
1.35      deraadt   798:        if ((f = fopen(log, "a")) == NULL)
1.48      millert   799:                return (-1);
1.44      mpech     800:        (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
                    801:            daytime, hostname, (long)getpid());
1.35      deraadt   802:        if (fclose(f) == EOF)
                    803:                err(1, "log_trim: fclose");
1.48      millert   804:        return (0);
1.1       deraadt   805: }
                    806:
1.16      millert   807: /* Fork off compress or gzip to compress the old log file */
1.25      millert   808: void
1.51      millert   809: compress_log(struct conf_entry *ent)
1.1       deraadt   810: {
1.48      millert   811:        pid_t pid;
                    812:        char *base, tmp[MAXPATHLEN];
1.51      millert   813:
                    814:        if (ent->backdir != NULL)
                    815:                snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
                    816:                    ent->logbase);
                    817:        else
                    818:                snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
                    819:
1.27      millert   820:        if ((base = strrchr(COMPRESS, '/')) == NULL)
                    821:                base = COMPRESS;
                    822:        else
                    823:                base++;
                    824:        if (noaction) {
1.51      millert   825:                printf("%s %s\n", base, tmp);
1.27      millert   826:                return;
                    827:        }
1.35      deraadt   828:        pid = fork();
                    829:        if (pid < 0) {
1.11      downsj    830:                err(1, "fork");
1.50      millert   831:        } else if (pid == 0) {
1.37      deraadt   832:                (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16      millert   833:                warn(COMPRESS);
                    834:                _exit(1);
1.35      deraadt   835:        }
1.1       deraadt   836: }
                    837:
                    838: /* Return size in kilobytes of a file */
1.25      millert   839: int
1.48      millert   840: sizefile(char *file)
1.1       deraadt   841: {
1.35      deraadt   842:        struct stat sb;
1.1       deraadt   843:
1.35      deraadt   844:        if (stat(file, &sb) < 0)
1.48      millert   845:                return (-1);
                    846:        return (sb.st_blocks / (1024.0 / DEV_BSIZE));
1.1       deraadt   847: }
                    848:
1.25      millert   849: /* Return the age (in hours) of old log file (file.0), or -1 if none */
                    850: int
1.51      millert   851: age_old_log(struct conf_entry *ent)
1.1       deraadt   852: {
1.35      deraadt   853:        struct stat sb;
                    854:        char tmp[MAXPATHLEN];
1.1       deraadt   855:
1.51      millert   856:        if (ent->backdir != NULL)
                    857:                snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir, ent->logbase);
                    858:        else {
                    859:                strlcpy(tmp, ent->log, sizeof(tmp));
                    860:                strlcat(tmp, ".0", sizeof(tmp));
                    861:        }
                    862:        if (ent->flags & CE_COMPACT)
1.48      millert   863:                strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp));
1.51      millert   864:        if (stat(tmp, &sb) < 0)
                    865:                return (-1);
1.48      millert   866:        return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1       deraadt   867: }
                    868:
                    869: /* Skip Over Blanks */
1.25      millert   870: char *
1.48      millert   871: sob(char *p)
1.1       deraadt   872: {
1.35      deraadt   873:        while (p && *p && isspace(*p))
                    874:                p++;
1.48      millert   875:        return (p);
1.1       deraadt   876: }
                    877:
                    878: /* Skip Over Non-Blanks */
1.25      millert   879: char *
1.48      millert   880: son(char *p)
1.1       deraadt   881: {
1.35      deraadt   882:        while (p && *p && !isspace(*p))
                    883:                p++;
1.48      millert   884:        return (p);
1.1       deraadt   885: }
                    886:
                    887: /* Check if string is actually a number */
1.25      millert   888: int
1.48      millert   889: isnumberstr(char *string)
1.1       deraadt   890: {
1.35      deraadt   891:        while (*string) {
                    892:                if (!isdigit(*string++))
1.48      millert   893:                        return (0);
1.35      deraadt   894:        }
1.48      millert   895:        return (1);
1.10      downsj    896: }
                    897:
1.25      millert   898: void
1.48      millert   899: domonitor(char *log, char *whom)
1.10      downsj    900: {
                    901:        struct stat sb, tsb;
1.35      deraadt   902:        char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10      downsj    903:        FILE *fp;
                    904:        off_t osize;
                    905:        int rd;
                    906:
                    907:        if (stat(log, &sb) < 0)
                    908:                return;
                    909:
                    910:        flog = strdup(log);
1.11      downsj    911:        if (flog == NULL)
                    912:                err(1, "strdup");
                    913:
1.10      downsj    914:        for (p = flog; *p != '\0'; p++) {
                    915:                if (*p == '/')
                    916:                        *p = '_';
                    917:        }
1.48      millert   918:        snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35      deraadt   919:            STATS_DIR, flog);
1.10      downsj    920:
                    921:        /* ..if it doesn't exist, simply record the current size. */
                    922:        if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
                    923:                goto update;
                    924:
                    925:        fp = fopen(fname, "r");
                    926:        if (fp == NULL) {
1.34      millert   927:                warn("%s", fname);
1.10      downsj    928:                goto cleanup;
                    929:        }
                    930: #ifdef QUAD_OFF_T
                    931:        if (fscanf(fp, "%qd\n", &osize) != 1) {
                    932: #else
                    933:        if (fscanf(fp, "%ld\n", &osize) != 1) {
                    934: #endif /* QUAD_OFF_T */
                    935:                fclose(fp);
                    936:                goto update;
                    937:        }
                    938:
                    939:        fclose(fp);
                    940:
                    941:        /* If the file is smaller, mark the entire thing as changed. */
                    942:        if (sb.st_size < osize)
                    943:                osize = 0;
                    944:
                    945:        /* Now see if current size is larger. */
                    946:        if (sb.st_size > osize) {
                    947:                rb = (char *) malloc(sb.st_size - osize);
1.11      downsj    948:                if (rb == NULL)
                    949:                        err(1, "malloc");
1.10      downsj    950:
                    951:                /* Open logfile, seek. */
                    952:                fp = fopen(log, "r");
                    953:                if (fp == NULL) {
1.34      millert   954:                        warn("%s", log);
1.10      downsj    955:                        goto cleanup;
                    956:                }
                    957:                fseek(fp, osize, SEEK_SET);
                    958:                rd = fread(rb, 1, sb.st_size - osize, fp);
                    959:                if (rd < 1) {
1.11      downsj    960:                        warn("fread");
1.10      downsj    961:                        fclose(fp);
                    962:                        goto cleanup;
                    963:                }
                    964:
                    965:                /* Send message. */
                    966:                fclose(fp);
                    967:
                    968:                fp = openmail();
                    969:                if (fp == NULL) {
1.11      downsj    970:                        warn("openmail");
1.10      downsj    971:                        goto cleanup;
                    972:                }
                    973:                fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
                    974:                    whom, log);
                    975:                fwrite(rb, 1, rd, fp);
                    976:                fputs("\n\n", fp);
                    977:
1.48      millert   978:                pclose(fp);
1.10      downsj    979:        }
                    980: update:
                    981:        /* Reopen for writing and update file. */
                    982:        fp = fopen(fname, "w");
                    983:        if (fp == NULL) {
1.34      millert   984:                warn("%s", fname);
1.10      downsj    985:                goto cleanup;
                    986:        }
                    987: #ifdef QUAD_OFF_T
                    988:        fprintf(fp, "%qd\n", sb.st_size);
                    989: #else
                    990:        fprintf(fp, "%ld\n", sb.st_size);
                    991: #endif /* QUAD_OFF_T */
                    992:        fclose(fp);
                    993:
                    994: cleanup:
                    995:        free(flog);
                    996:        if (rb != NULL)
                    997:                free(rb);
                    998: }
                    999:
1.25      millert  1000: FILE *
1.48      millert  1001: openmail(void)
1.10      downsj   1002: {
1.48      millert  1003:        FILE *ret;
1.35      deraadt  1004:        char *cmdbuf = NULL;
1.10      downsj   1005:
1.35      deraadt  1006:        asprintf(&cmdbuf, "%s -t", SENDMAIL);
                   1007:        if (cmdbuf) {
                   1008:                ret = popen(cmdbuf, "w");
                   1009:                free(cmdbuf);
                   1010:                return (ret);
                   1011:        }
                   1012:        return (NULL);
1.10      downsj   1013: }
                   1014:
1.25      millert  1015: void
1.48      millert  1016: child_killer(int signo)
1.16      millert  1017: {
1.38      deraadt  1018:        int save_errno = errno;
1.16      millert  1019:        int status;
                   1020:
                   1021:        while (waitpid(-1, &status, WNOHANG) > 0)
                   1022:                ;
1.38      deraadt  1023:        errno = save_errno;
1.1       deraadt  1024: }