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

1.72    ! millert     1: /*     $OpenBSD: newsyslog.c,v 1.71 2003/06/10 22:58:45 millert Exp $  */
1.10      downsj      2:
                      3: /*
1.62      millert     4:  * Copyright (c) 1999, 2002, 2003 Todd C. Miller <Todd.Miller@courtesan.com>
1.30      millert     5:  *
1.66      millert     6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.72    ! millert    10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            17:  * Sponsored in part by the Defense Advanced Research Projects
        !            18:  * Agency (DARPA) and Air Force Research Laboratory, Air Force
        !            19:  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
1.30      millert    20:  */
                     21:
                     22: /*
1.10      downsj     23:  * Copyright (c) 1997, Jason Downs.  All rights reserved.
                     24:  *
                     25:  * Redistribution and use in source and binary forms, with or without
                     26:  * modification, are permitted provided that the following conditions
                     27:  * are met:
                     28:  * 1. Redistributions of source code must retain the above copyright
                     29:  *    notice, this list of conditions and the following disclaimer.
                     30:  * 2. Redistributions in binary form must reproduce the above copyright
                     31:  *    notice, this list of conditions and the following disclaimer in the
                     32:  *    documentation and/or other materials provided with the distribution.
                     33:  *
                     34:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
                     35:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     36:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     37:  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
                     38:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     39:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     40:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
                     41:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44:  * SUCH DAMAGE.
                     45:  */
1.3       deraadt    46:
1.1       deraadt    47: /*
                     48:  * This file contains changes from the Open Software Foundation.
                     49:  */
                     50:
                     51: /*
1.50      millert    52:  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
                     53:  *
                     54:  * Permission to use, copy, modify, and distribute this software
                     55:  * and its documentation for any purpose and without fee is
                     56:  * hereby granted, provided that the above copyright notice
                     57:  * appear in all copies and that both that copyright notice and
                     58:  * this permission notice appear in supporting documentation,
                     59:  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
                     60:  * used in advertising or publicity pertaining to distribution
                     61:  * of the software without specific, written prior permission.
                     62:  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
                     63:  * the suitability of this software for any purpose.  It is
                     64:  * provided "as is" without express or implied warranty.
                     65:  */
1.1       deraadt    66:
                     67: /*
                     68:  *      newsyslog - roll over selected logs at the appropriate time,
1.65      millert    69:  *              keeping the specified number of backup files around.
1.1       deraadt    70:  *
                     71:  */
                     72:
                     73: #ifndef lint
1.72    ! millert    74: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.71 2003/06/10 22:58:45 millert Exp $";
1.1       deraadt    75: #endif /* not lint */
                     76:
                     77: #ifndef CONF
1.47      millert    78: #define CONF "/etc/newsyslog.conf" /* Configuration file */
1.1       deraadt    79: #endif
                     80: #ifndef PIDFILE
                     81: #define PIDFILE "/etc/syslog.pid"
                     82: #endif
                     83: #ifndef COMPRESS
1.47      millert    84: #define COMPRESS "/usr/bin/compress" /* File compression program */
1.1       deraadt    85: #endif
                     86: #ifndef COMPRESS_POSTFIX
                     87: #define COMPRESS_POSTFIX ".Z"
                     88: #endif
1.10      downsj     89: #ifndef STATS_DIR
                     90: #define STATS_DIR "/etc"
                     91: #endif
                     92: #ifndef SENDMAIL
                     93: #define SENDMAIL "/usr/lib/sendmail"
                     94: #endif
1.1       deraadt    95:
1.48      millert    96: #include <sys/param.h>
                     97: #include <sys/stat.h>
1.9       downsj     98: #include <sys/time.h>
                     99: #include <sys/wait.h>
1.48      millert   100:
1.1       deraadt   101: #include <ctype.h>
1.48      millert   102: #include <err.h>
                    103: #include <errno.h>
1.9       downsj    104: #include <fcntl.h>
1.1       deraadt   105: #include <grp.h>
1.47      millert   106: #include <limits.h>
1.48      millert   107: #include <pwd.h>
                    108: #include <signal.h>
                    109: #include <stdio.h>
                    110: #include <stdlib.h>
                    111: #include <string.h>
1.68      millert   112: #include <time.h>
1.9       downsj    113: #include <unistd.h>
1.1       deraadt   114:
1.26      millert   115: #define CE_ROTATED     0x01            /* Log file has been rotated */
1.65      millert   116: #define CE_COMPACT     0x02            /* Compact the archived log files */
1.26      millert   117: #define CE_BINARY      0x04            /* Logfile is in binary, don't add */
1.10      downsj    118:                                        /* status messages */
1.65      millert   119: #define CE_MONITOR     0x08            /* Monitor for changes */
1.45      wcobb     120: #define CE_FOLLOW      0x10            /* Follow symbolic links */
1.65      millert   121: #define CE_TRIMAT      0x20            /* Trim at a specific time */
1.35      deraadt   122:
1.49      millert   123: #define        MIN_PID         4               /* Don't touch pids lower than this */
1.58      millert   124: #define        MIN_SIZE        256             /* Don't rotate if smaller (in bytes) */
1.49      millert   125:
1.50      millert   126: #define        DPRINTF(x)      do { if (verbose) printf x ; } while (0)
                    127:
1.1       deraadt   128: struct conf_entry {
1.35      deraadt   129:        char    *log;           /* Name of the log */
1.51      millert   130:        char    *logbase;       /* Basename of the log */
                    131:        char    *backdir;       /* Directory in which to store backups */
1.35      deraadt   132:        uid_t   uid;            /* Owner of log */
                    133:        gid_t   gid;            /* Group of log */
                    134:        int     numlogs;        /* Number of logs to keep */
1.58      millert   135:        off_t   size;           /* Size cutoff to trigger trimming the log */
1.35      deraadt   136:        int     hours;          /* Hours between log trimming */
1.63      millert   137:        time_t  trim_at;        /* Specific time at which to do trimming */
1.35      deraadt   138:        int     permissions;    /* File permissions on the log */
1.30      millert   139:        int     signal;         /* Signal to send (defaults to SIGHUP) */
1.35      deraadt   140:        int     flags;          /* Flags (CE_COMPACT & CE_BINARY)  */
1.10      downsj    141:        char    *whom;          /* Whom to notify if logfile changes */
1.65      millert   142:        char    *pidfile;       /* Path to file containing pid to signal */
1.30      millert   143:        char    *runcmd;        /* Command to run instead of sending a signal */
1.35      deraadt   144:        struct conf_entry *next; /* Linked list pointer */
1.1       deraadt   145: };
                    146:
1.30      millert   147: struct pidinfo {
                    148:        char    *file;
                    149:        int     signal;
                    150: };
                    151:
1.52      millert   152: int    verbose = 0;            /* Print out what's going on */
                    153: int    needroot = 1;           /* Root privs are necessary */
                    154: int    noaction = 0;           /* Don't do anything, just show it */
1.33      millert   155: int    monitormode = 0;        /* Don't do monitoring by default */
1.52      millert   156: int    force = 0;              /* Force the logs to be rotated */
                    157: char   *conf = CONF;           /* Configuration file to use */
                    158: time_t timenow;
1.65      millert   159: char   hostname[MAXHOSTNAMELEN]; /* Hostname */
1.52      millert   160: char   *daytime;               /* timenow in human readable form */
1.65      millert   161: char   *arcdir;                /* Dir to put archives in (if it exists) */
1.1       deraadt   162:
1.43      millert   163: void do_entry(struct conf_entry *);
1.48      millert   164: void parse_args(int, char **);
1.43      millert   165: void usage(void);
                    166: struct conf_entry *parse_file(int *);
1.60      millert   167: char *missing_field(char *, char *, int);
1.51      millert   168: void dotrim(struct conf_entry *);
1.43      millert   169: int log_trim(char *);
1.51      millert   170: void compress_log(struct conf_entry *);
1.58      millert   171: off_t sizefile(char *);
1.51      millert   172: int age_old_log(struct conf_entry *);
1.43      millert   173: char *sob(char *);
                    174: char *son(char *);
                    175: int isnumberstr(char *);
1.61      millert   176: int domonitor(struct conf_entry *);
1.43      millert   177: FILE *openmail(void);
                    178: void child_killer(int);
                    179: void run_command(char *);
                    180: void send_signal(char *, int);
1.57      millert   181: char *lstat_log(char *, size_t, int);
                    182: int stat_suffix(char *, size_t, char *, struct stat *, int (*)());
1.63      millert   183: time_t parse8601(char *);
                    184: time_t parseDWM(char *);
1.1       deraadt   185:
1.25      millert   186: int
1.48      millert   187: main(int argc, char **argv)
1.1       deraadt   188: {
1.53      millert   189:        struct conf_entry *p, *q, *x, *y;
1.30      millert   190:        struct pidinfo *pidlist, *pl;
1.53      millert   191:        char **av;
1.28      millert   192:        int status, listlen;
1.35      deraadt   193:
1.48      millert   194:        parse_args(argc, argv);
1.53      millert   195:        argc -= optind;
                    196:        argv += optind;
                    197:
1.35      deraadt   198:        if (needroot && getuid() && geteuid())
1.11      downsj    199:                errx(1, "You must be root.");
1.53      millert   200:
                    201:        p = parse_file(&listlen);
                    202:        if (argc > 0) {
                    203:                /* Only rotate specified files. */
                    204:                x = y = NULL;
                    205:                listlen = 0;
                    206:                for (av = argv; *av; av++) {
                    207:                        for (q = p; q; q = q->next)
                    208:                                if (strcmp(*av, q->log) == 0) {
                    209:                                        if (x == NULL)
                    210:                                                x = y = q;
                    211:                                        else {
                    212:                                                y->next = q;
                    213:                                                y = q;
                    214:                                        }
                    215:                                        listlen++;
                    216:                                        break;
                    217:                                }
                    218:                        if (q == NULL)
1.60      millert   219:                                warnx("%s: %s not found", conf, *av);
1.53      millert   220:                }
                    221:                if (x == NULL)
1.60      millert   222:                        errx(1, "%s: no specified log files", conf);
1.53      millert   223:                y->next = NULL;
                    224:                p = x;
                    225:        }
1.26      millert   226:
1.46      aaron     227:        pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30      millert   228:        if (pidlist == NULL)
1.28      millert   229:                err(1, "calloc");
                    230:
1.53      millert   231:        signal(SIGCHLD, child_killer);
                    232:
1.26      millert   233:        /* Step 1, rotate all log files */
1.53      millert   234:        for (q = p; q; q = q->next)
1.35      deraadt   235:                do_entry(q);
1.26      millert   236:
1.28      millert   237:        /* Step 2, make a list of unique pid files */
1.30      millert   238:        for (q = p, pl = pidlist; q; ) {
1.28      millert   239:                if (q->flags & CE_ROTATED) {
1.30      millert   240:                        struct pidinfo *pltmp;
1.28      millert   241:
1.30      millert   242:                        for (pltmp = pidlist; pltmp < pl; pltmp++) {
1.55      millert   243:                                if ((q->pidfile &&
                    244:                                    strcmp(pltmp->file, q->pidfile) == 0 &&
                    245:                                    pltmp->signal == q->signal) ||
                    246:                                    (q->runcmd &&
1.30      millert   247:                                    strcmp(q->runcmd, pltmp->file) == 0))
1.28      millert   248:                                        break;
1.30      millert   249:                        }
                    250:                        if (pltmp == pl) {      /* unique entry */
                    251:                                if (q->runcmd) {
                    252:                                        pl->file = q->runcmd;
                    253:                                        pl->signal = -1;
                    254:                                } else {
                    255:                                        pl->file = q->pidfile;
                    256:                                        pl->signal = q->signal;
                    257:                                }
1.31      millert   258:                                pl++;
1.30      millert   259:                        }
1.28      millert   260:                }
1.35      deraadt   261:                q = q->next;
                    262:        }
1.26      millert   263:
1.30      millert   264:        /* Step 3, send a signal or run a command */
                    265:        for (pl = pidlist; pl->file; pl++) {
1.56      millert   266:                if (pl->file != NULL) {
                    267:                        if (pl->signal == -1)
                    268:                                run_command(pl->file);
                    269:                        else
                    270:                                send_signal(pl->file, pl->signal);
                    271:                }
1.30      millert   272:        }
1.28      millert   273:        if (!noaction)
                    274:                sleep(5);
                    275:
                    276:        /* Step 4, compress the log.0 file if configured to do so and free */
1.35      deraadt   277:        while (p) {
1.26      millert   278:                if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51      millert   279:                        compress_log(p);
1.26      millert   280:                q = p;
1.35      deraadt   281:                p = p->next;
                    282:                free(q);
                    283:        }
1.16      millert   284:
                    285:        /* Wait for children to finish, then exit */
                    286:        while (waitpid(-1, &status, 0) != -1)
                    287:                ;
1.35      deraadt   288:        exit(0);
1.1       deraadt   289: }
                    290:
1.25      millert   291: void
1.48      millert   292: do_entry(struct conf_entry *ent)
1.1       deraadt   293: {
1.58      millert   294:        int modtime;
                    295:        off_t size;
1.48      millert   296:        struct stat sb;
1.45      wcobb     297:
1.51      millert   298:        if (lstat(ent->log, &sb) != 0)
1.50      millert   299:                return;
                    300:        if (!S_ISREG(sb.st_mode) &&
                    301:            (!S_ISLNK(sb.st_mode) || !(ent->flags & CE_FOLLOW))) {
                    302:                DPRINTF(("--> not a regular file, skipping\n"));
                    303:                return;
1.45      wcobb     304:        }
1.14      millert   305:
1.61      millert   306:        DPRINTF(("%s <%d%s%s%s%s>: ", ent->log, ent->numlogs,
1.51      millert   307:            (ent->flags & CE_COMPACT) ? "Z" : "",
                    308:            (ent->flags & CE_BINARY) ? "B" : "",
1.61      millert   309:            (ent->flags & CE_FOLLOW) ? "F" : "",
                    310:            (ent->flags & CE_MONITOR) && monitormode ? "M" : ""));
1.50      millert   311:
1.35      deraadt   312:        size = sizefile(ent->log);
1.51      millert   313:        modtime = age_old_log(ent);
1.35      deraadt   314:        if (size < 0) {
1.50      millert   315:                DPRINTF(("does not exist.\n"));
1.35      deraadt   316:        } else {
1.63      millert   317:                if (ent->flags & CE_TRIMAT && !force) {
                    318:                        if (timenow < ent->trim_at ||
                    319:                            difftime(timenow, ent->trim_at) >= 60 * 60) {
                    320:                                DPRINTF(("--> will trim at %s",
                    321:                                    ctime(&ent->trim_at)));
                    322:                                return;
1.65      millert   323:                        } else if (ent->hours <= 0) {
1.63      millert   324:                                DPRINTF(("--> time is up\n"));
                    325:                        }
                    326:                }
1.50      millert   327:                if (ent->size > 0)
1.58      millert   328:                        DPRINTF(("size (KB): %.2f [%d] ", size / 1024.0,
                    329:                            (int)(ent->size / 1024)));
1.50      millert   330:                if (ent->hours > 0)
                    331:                        DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.61      millert   332:                if (monitormode && (ent->flags & CE_MONITOR) && domonitor(ent))
                    333:                        DPRINTF(("--> monitored\n"));
                    334:                else if (!monitormode &&
                    335:                    (force || (ent->size > 0 && size >= ent->size) ||
1.63      millert   336:                    (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) ||
1.49      millert   337:                    (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
                    338:                    && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50      millert   339:                        DPRINTF(("--> trimming log....\n"));
1.19      kstailey  340:                        if (noaction && !verbose)
1.50      millert   341:                                printf("%s <%d%s%s%s>\n", ent->log,
                    342:                                    ent->numlogs,
                    343:                                    (ent->flags & CE_COMPACT) ? "Z" : "",
                    344:                                    (ent->flags & CE_BINARY) ? "B" : "",
                    345:                                    (ent->flags & CE_FOLLOW) ? "F" : "");
1.51      millert   346:                        dotrim(ent);
1.26      millert   347:                        ent->flags |= CE_ROTATED;
1.50      millert   348:                } else
                    349:                        DPRINTF(("--> skipping\n"));
1.35      deraadt   350:        }
1.1       deraadt   351: }
                    352:
1.30      millert   353: /* Run the specified command */
                    354: void
1.48      millert   355: run_command(char *cmd)
1.30      millert   356: {
                    357:        if (noaction)
1.55      millert   358:                (void)printf("run %s\n", cmd);
1.30      millert   359:        else
                    360:                system(cmd);
                    361: }
                    362:
                    363: /* Send a signal to the pid specified by pidfile */
1.26      millert   364: void
1.48      millert   365: send_signal(char *pidfile, int signal)
1.26      millert   366: {
1.48      millert   367:        pid_t pid;
                    368:        FILE *f;
                    369:        char line[BUFSIZ], *ep, *err;
1.69      millert   370:        long lval;
1.26      millert   371:
1.35      deraadt   372:        if ((f = fopen(pidfile, "r")) == NULL) {
1.26      millert   373:                warn("can't open %s", pidfile);
                    374:                return;
                    375:        }
                    376:
1.48      millert   377:        pid = 0;
1.42      millert   378:        errno = 0;
                    379:        err = NULL;
1.40      deraadt   380:        if (fgets(line, sizeof(line), f)) {
1.69      millert   381:                lval = strtol(line, &ep, 10);
1.41      marc      382:                if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40      deraadt   383:                        err = "invalid number in";
1.69      millert   384:                else if (lval < 0 || (errno == ERANGE && lval == LONG_MAX))
1.40      deraadt   385:                        err = "out of range number in";
1.69      millert   386:                else if (lval == 0)
1.40      deraadt   387:                        err = "no number in";
1.69      millert   388:                else if (lval < MIN_PID)
1.40      deraadt   389:                        err = "preposterous process number in";
                    390:                else
1.69      millert   391:                        pid = (pid_t)lval;
1.42      millert   392:        } else {
                    393:                if (errno == 0)
                    394:                        err = "empty";
                    395:                else
                    396:                        err = "error reading";
1.40      deraadt   397:        }
1.26      millert   398:        (void)fclose(f);
                    399:
1.40      deraadt   400:        if (err)
                    401:                warnx("%s pid file: %s", err, pidfile);
                    402:        else if (noaction)
1.44      mpech     403:                (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30      millert   404:        else if (kill(pid, signal))
                    405:                warnx("warning - could not send SIG%s to daemon",
                    406:                    sys_signame[signal]);
1.26      millert   407: }
                    408:
1.25      millert   409: void
1.48      millert   410: parse_args(int argc, char **argv)
1.1       deraadt   411: {
1.48      millert   412:        int ch;
                    413:        char *p;
1.1       deraadt   414:
1.35      deraadt   415:        timenow = time(NULL);
                    416:        daytime = ctime(&timenow) + 4;
                    417:        daytime[15] = '\0';
1.1       deraadt   418:
1.35      deraadt   419:        /* Let's get our hostname */
                    420:        (void)gethostname(hostname, sizeof(hostname));
1.1       deraadt   421:
                    422:        /* Truncate domain */
1.48      millert   423:        if ((p = strchr(hostname, '.')) != NULL)
1.1       deraadt   424:                *p = '\0';
                    425:
1.52      millert   426:        while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48      millert   427:                switch (ch) {
1.51      millert   428:                case 'a':
                    429:                        arcdir = optarg;
                    430:                        break;
1.35      deraadt   431:                case 'n':
                    432:                        noaction++; /* This implies needroot as off */
                    433:                        /* fall through */
                    434:                case 'r':
                    435:                        needroot = 0;
                    436:                        break;
                    437:                case 'v':
                    438:                        verbose++;
                    439:                        break;
                    440:                case 'f':
                    441:                        conf = optarg;
                    442:                        break;
1.10      downsj    443:                case 'm':
1.33      millert   444:                        monitormode++;
1.10      downsj    445:                        break;
1.52      millert   446:                case 'F':
                    447:                        force++;
                    448:                        break;
1.35      deraadt   449:                default:
                    450:                        usage();
                    451:                }
                    452:        }
1.52      millert   453:        if (monitormode && force)
                    454:                errx(1, "cannot specify both -m and -F flags");
1.9       downsj    455: }
1.1       deraadt   456:
1.25      millert   457: void
1.48      millert   458: usage(void)
1.1       deraadt   459: {
1.25      millert   460:        extern const char *__progname;
                    461:
1.52      millert   462:        (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53      millert   463:            "[-f config_file] [log ...]\n", __progname);
1.14      millert   464:        exit(1);
1.1       deraadt   465: }
                    466:
1.52      millert   467: /*
                    468:  * Parse a configuration file and return a linked list of all the logs
1.1       deraadt   469:  * to process
                    470:  */
1.25      millert   471: struct conf_entry *
1.48      millert   472: parse_file(int *nentries)
1.1       deraadt   473: {
1.48      millert   474:        FILE *f;
1.63      millert   475:        char line[BUFSIZ], *parse, *q, *errline, *group, *tmp, *ep;
1.60      millert   476:        int lineno;
1.70      millert   477:        long l;
1.35      deraadt   478:        struct conf_entry *first = NULL;
                    479:        struct conf_entry *working = NULL;
1.48      millert   480:        struct passwd *pwd;
1.35      deraadt   481:        struct group *grp;
1.51      millert   482:        struct stat sb;
1.1       deraadt   483:
1.35      deraadt   484:        if (strcmp(conf, "-") == 0)
                    485:                f = stdin;
1.48      millert   486:        else if ((f = fopen(conf, "r")) == NULL)
                    487:                err(1, "can't open %s", conf);
1.11      downsj    488:
1.28      millert   489:        *nentries = 0;
1.60      millert   490:        for (lineno = 0; fgets(line, sizeof(line), f); lineno++) {
1.48      millert   491:                tmp = sob(line);
1.54      millert   492:                if (*tmp == '\0' || *tmp == '#')
1.35      deraadt   493:                        continue;
1.48      millert   494:                errline = strdup(tmp);
1.11      downsj    495:                if (errline == NULL)
                    496:                        err(1, "strdup");
1.28      millert   497:                (*nentries)++;
1.35      deraadt   498:                if (!first) {
                    499:                        working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    500:                        if (working == NULL)
                    501:                                err(1, "malloc");
1.35      deraadt   502:                        first = working;
                    503:                } else {
                    504:                        working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    505:                        if (working->next == NULL)
                    506:                                err(1, "malloc");
1.35      deraadt   507:                        working = working->next;
                    508:                }
1.1       deraadt   509:
1.60      millert   510:                q = parse = missing_field(sob(line), errline, lineno);
1.35      deraadt   511:                *(parse = son(line)) = '\0';
                    512:                working->log = strdup(q);
1.11      downsj    513:                if (working->log == NULL)
                    514:                        err(1, "strdup");
1.1       deraadt   515:
1.51      millert   516:                if ((working->logbase = strrchr(working->log, '/')) != NULL)
                    517:                        working->logbase++;
                    518:
1.60      millert   519:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   520:                *(parse = son(parse)) = '\0';
1.63      millert   521:                if ((group = strchr(q, ':')) != NULL ||
                    522:                    (group = strrchr(q, '.')) != NULL)  {
1.25      millert   523:                        *group++ = '\0';
                    524:                        if (*q) {
                    525:                                if (!(isnumberstr(q))) {
1.48      millert   526:                                        if ((pwd = getpwnam(q)) == NULL)
1.60      millert   527:                                                errx(1, "%s:%d: unknown user: %s",
                    528:                                                    conf, lineno, q);
1.48      millert   529:                                        working->uid = pwd->pw_uid;
1.25      millert   530:                                } else
                    531:                                        working->uid = atoi(q);
                    532:                        } else
1.50      millert   533:                                working->uid = (uid_t)-1;
1.25      millert   534:
                    535:                        q = group;
                    536:                        if (*q) {
                    537:                                if (!(isnumberstr(q))) {
                    538:                                        if ((grp = getgrnam(q)) == NULL)
1.60      millert   539:
                    540:                                                errx(1, "%s:%d: unknown group: %s",
                    541:                                                    conf, lineno, q);
1.25      millert   542:                                        working->gid = grp->gr_gid;
                    543:                                } else
                    544:                                        working->gid = atoi(q);
                    545:                        } else
1.50      millert   546:                                working->gid = (gid_t)-1;
1.25      millert   547:
1.60      millert   548:                        q = parse = missing_field(sob(++parse), errline, lineno);
1.25      millert   549:                        *(parse = son(parse)) = '\0';
1.50      millert   550:                } else {
                    551:                        working->uid = (uid_t)-1;
                    552:                        working->gid = (gid_t)-1;
                    553:                }
1.1       deraadt   554:
1.35      deraadt   555:                if (!sscanf(q, "%o", &working->permissions))
1.60      millert   556:                        errx(1, "%s:%d: bad permissions: %s", conf, lineno, q);
1.1       deraadt   557:
1.60      millert   558:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   559:                *(parse = son(parse)) = '\0';
                    560:                if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.60      millert   561:                        errx(1, "%s:%d: bad number: %s", conf, lineno, q);
1.1       deraadt   562:
1.60      millert   563:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   564:                *(parse = son(parse)) = '\0';
                    565:                if (isdigit(*q))
1.58      millert   566:                        working->size = atoi(q) * 1024;
1.35      deraadt   567:                else
                    568:                        working->size = -1;
                    569:
1.63      millert   570:                working->flags = 0;
1.60      millert   571:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   572:                *(parse = son(parse)) = '\0';
1.70      millert   573:                l = strtol(q, &ep, 10);
1.71      millert   574:                if (l < 0 || l >= INT_MAX)
1.63      millert   575:                        errx(1, "%s:%d: interval out of range: %s", conf,
                    576:                            lineno, q);
1.70      millert   577:                working->hours = (int)l;
1.63      millert   578:                switch (*ep) {
                    579:                case '\0':
                    580:                        break;
                    581:                case '@':
                    582:                        working->trim_at = parse8601(ep + 1);
                    583:                        if (working->trim_at == (time_t) - 1)
                    584:                                errx(1, "%s:%d: bad time: %s", conf, lineno, q);
                    585:                        working->flags |= CE_TRIMAT;
                    586:                        break;
                    587:                case '$':
                    588:                        working->trim_at = parseDWM(ep + 1);
                    589:                        if (working->trim_at == (time_t) - 1)
                    590:                                errx(1, "%s:%d: bad time: %s", conf, lineno, q);
                    591:                        working->flags |= CE_TRIMAT;
                    592:                        break;
                    593:                case '*':
                    594:                        if (q == ep)
                    595:                                break;
                    596:                        /* FALLTHROUGH */
                    597:                default:
                    598:                        errx(1, "%s:%d: bad interval/at: %s", conf, lineno, q);
                    599:                        break;
                    600:                }
1.1       deraadt   601:
1.35      deraadt   602:                q = sob(++parse);       /* Optional field */
1.32      millert   603:                if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
                    604:                    *q == 'M' || *q == 'm') {
                    605:                        *(parse = son(q)) = '\0';
                    606:                        while (*q) {
                    607:                                switch (*q) {
                    608:                                case 'Z':
                    609:                                case 'z':
                    610:                                        working->flags |= CE_COMPACT;
                    611:                                        break;
                    612:                                case 'B':
                    613:                                case 'b':
                    614:                                        working->flags |= CE_BINARY;
                    615:                                        break;
                    616:                                case 'M':
                    617:                                case 'm':
                    618:                                        working->flags |= CE_MONITOR;
1.45      wcobb     619:                                        break;
                    620:                                case 'F':
                    621:                                case 'f':
                    622:                                        working->flags |= CE_FOLLOW;
1.32      millert   623:                                        break;
                    624:                                default:
1.60      millert   625:                                        errx(1, "%s:%d: illegal flag: `%c'",
                    626:                                            conf, lineno, *q);
1.32      millert   627:                                        break;
                    628:                                }
                    629:                                q++;
                    630:                        }
                    631:                } else
1.40      deraadt   632:                        parse--;        /* no flags so undo */
1.10      downsj    633:
1.14      millert   634:                working->pidfile = PIDFILE;
1.30      millert   635:                working->signal = SIGHUP;
                    636:                working->runcmd = NULL;
1.61      millert   637:                working->whom = NULL;
1.30      millert   638:                for (;;) {
                    639:                        q = parse = sob(++parse);       /* Optional field */
                    640:                        if (q == NULL || *q == '\0')
                    641:                                break;
                    642:                        if (*q == '/') {
                    643:                                *(parse = son(parse)) = '\0';
                    644:                                if (strlen(q) >= MAXPATHLEN)
1.60      millert   645:                                        errx(1, "%s:%d: pathname too long: %s",
                    646:                                            conf, lineno, q);
1.30      millert   647:                                working->pidfile = strdup(q);
                    648:                                if (working->pidfile == NULL)
                    649:                                        err(1, "strdup");
                    650:                        } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
                    651:                                *(parse = tmp) = '\0';
1.56      millert   652:                                if (*++q != '\0') {
                    653:                                        working->runcmd = strdup(q);
                    654:                                        if (working->runcmd == NULL)
                    655:                                                err(1, "strdup");
                    656:                                }
1.55      millert   657:                                working->pidfile = NULL;
                    658:                                working->signal = -1;
1.30      millert   659:                        } else if (strncmp(q, "SIG", 3) == 0) {
                    660:                                int i;
                    661:
                    662:                                *(parse = son(parse)) = '\0';
                    663:                                for (i = 1; i < NSIG; i++) {
                    664:                                        if (!strcmp(sys_signame[i], q + 3)) {
                    665:                                                working->signal = i;
                    666:                                                break;
                    667:                                        }
                    668:                                }
                    669:                                if (i == NSIG)
1.60      millert   670:                                        errx(1, "%s:%d: unknown signal: %s",
                    671:                                            conf, lineno, q);
1.61      millert   672:                        } else if (working->flags & CE_MONITOR) {
                    673:                                *(parse = son(parse)) = '\0';
                    674:                                working->whom = strdup(q);
                    675:                                if (working->whom == NULL)
                    676:                                        err(1, "strdup");
1.30      millert   677:                        } else
1.60      millert   678:                                errx(1, "%s:%d: unrecognized field: %s",
                    679:                                    conf, lineno, q);
1.14      millert   680:                }
1.51      millert   681:                free(errline);
                    682:
1.61      millert   683:                if ((working->flags & CE_MONITOR) && working->whom == NULL)
                    684:                        errx(1, "%s:%d: missing monitor notification field",
                    685:                            conf, lineno);
                    686:
1.51      millert   687:                /* If there is an arcdir, set working->backdir. */
                    688:                if (arcdir != NULL && working->logbase != NULL) {
                    689:                        if (*arcdir == '/') {
                    690:                                /* Fully qualified arcdir */
                    691:                                working->backdir = arcdir;
                    692:                        } else {
                    693:                                /* arcdir is relative to log's parent dir */
                    694:                                *(working->logbase - 1) = '\0';
                    695:                                if ((asprintf(&working->backdir, "%s/%s",
                    696:                                    working->log, arcdir)) == -1)
                    697:                                        err(1, "malloc");
                    698:                                *(working->logbase - 1) = '/';
                    699:                        }
                    700:                        /* Ignore arcdir if it doesn't exist. */
                    701:                        if (stat(working->backdir, &sb) != 0 ||
                    702:                            !S_ISDIR(sb.st_mode)) {
                    703:                                if (working->backdir != arcdir)
                    704:                                        free(working->backdir);
                    705:                                working->backdir = NULL;
                    706:                        }
                    707:                } else
                    708:                        working->backdir = NULL;
1.25      millert   709:
                    710:                /* Make sure we can't oflow MAXPATHLEN */
1.51      millert   711:                if (working->backdir != NULL) {
                    712:                        if (snprintf(line, sizeof(line), "%s/%s.%d%s",
                    713:                            working->backdir, working->logbase,
                    714:                            working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
1.60      millert   715:                                errx(1, "%s:%d: pathname too long: %s",
                    716:                                    conf, lineno, q);
1.51      millert   717:                } else {
                    718:                        if (snprintf(line, sizeof(line), "%s.%d%s",
                    719:                            working->log, working->numlogs, COMPRESS_POSTFIX)
                    720:                            >= MAXPATHLEN)
1.60      millert   721:                                errx(1, "%s:%d: pathname too long: %s",
                    722:                                    conf, lineno, working->log);
1.51      millert   723:                }
1.35      deraadt   724:        }
                    725:        if (working)
                    726:                working->next = NULL;
                    727:        (void)fclose(f);
1.48      millert   728:        return (first);
1.1       deraadt   729: }
                    730:
1.25      millert   731: char *
1.60      millert   732: missing_field(char *p, char *errline, int lineno)
1.1       deraadt   733: {
1.60      millert   734:        if (p == NULL || *p == '\0') {
                    735:                warnx("%s:%d: missing field", conf, lineno);
1.35      deraadt   736:                fputs(errline, stderr);
                    737:                exit(1);
                    738:        }
1.48      millert   739:        return (p);
1.1       deraadt   740: }
                    741:
1.25      millert   742: void
1.51      millert   743: dotrim(struct conf_entry *ent)
1.35      deraadt   744: {
                    745:        char    file1[MAXPATHLEN], file2[MAXPATHLEN];
1.57      millert   746:        char    oldlog[MAXPATHLEN], *suffix;
1.35      deraadt   747:        int     fd;
1.51      millert   748:        int     numdays = ent->numlogs;
                    749:
                    750:        /* Is there a separate backup dir? */
                    751:        if (ent->backdir != NULL)
                    752:                snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
                    753:                    ent->logbase);
                    754:        else
                    755:                strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1       deraadt   756:
1.35      deraadt   757:        /* Remove oldest log (may not exist) */
1.51      millert   758:        (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   759:        (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog, numdays,
1.35      deraadt   760:            COMPRESS_POSTFIX);
                    761:
                    762:        if (noaction) {
1.57      millert   763:                printf("\trm -f %s %s\n", file1, file2);
1.35      deraadt   764:        } else {
                    765:                (void)unlink(file1);
1.57      millert   766:                (void)unlink(file2);
1.35      deraadt   767:        }
                    768:
                    769:        /* Move down log files */
                    770:        while (numdays--) {
1.57      millert   771:                /*
1.65      millert   772:                 * If both the compressed archive and the non-compressed archive
1.70      millert   773:                 * exist, we decide which to rotate based on the CE_COMPACT flag
1.57      millert   774:                 */
1.51      millert   775:                (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   776:                suffix = lstat_log(file1, sizeof(file1), ent->flags);
                    777:                if (suffix == NULL)
                    778:                        continue;
                    779:                (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog,
                    780:                    numdays + 1, suffix);
                    781:
1.35      deraadt   782:                if (noaction) {
1.57      millert   783:                        printf("\tmv %s %s\n", file1, file2);
                    784:                        printf("\tchmod %o %s\n", ent->permissions, file2);
1.51      millert   785:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50      millert   786:                                printf("\tchown %u:%u %s\n",
1.57      millert   787:                                    ent->uid, ent->gid, file2);
1.35      deraadt   788:                } else {
1.57      millert   789:                        if (rename(file1, file2))
                    790:                                warn("can't mv %s to %s", file1, file2);
                    791:                        if (chmod(file2, ent->permissions))
                    792:                                warn("can't chmod %s", file2);
1.51      millert   793:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.57      millert   794:                                if (chown(file2, ent->uid, ent->gid))
                    795:                                        warn("can't chown %s", file2);
1.35      deraadt   796:                }
                    797:        }
1.51      millert   798:        if (!noaction && !(ent->flags & CE_BINARY))
                    799:                (void)log_trim(ent->log);  /* Report the trimming to the old log */
1.1       deraadt   800:
1.51      millert   801:        (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35      deraadt   802:        if (noaction)  {
1.50      millert   803:                printf("\tmktemp %s\n", file2);
1.35      deraadt   804:        } else {
                    805:                if ((fd = mkstemp(file2)) < 0)
1.26      millert   806:                        err(1, "can't start '%s' log", file2);
1.51      millert   807:                if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                    808:                        if (fchown(fd, ent->uid, ent->gid))
1.70      millert   809:                                err(1, "can't chown '%s' log file", file2);
1.51      millert   810:                if (fchmod(fd, ent->permissions))
1.26      millert   811:                        err(1, "can't chmod '%s' log file", file2);
1.35      deraadt   812:                (void)close(fd);
1.26      millert   813:                /* Add status message */
1.51      millert   814:                if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26      millert   815:                        err(1, "can't add status message to log '%s'", file2);
1.35      deraadt   816:        }
1.26      millert   817:
1.51      millert   818:        if (ent->numlogs == 0) {
1.5       deraadt   819:                if (noaction)
1.51      millert   820:                        printf("\trm %s\n", ent->log);
                    821:                else if (unlink(ent->log))
                    822:                        warn("can't rm %s", ent->log);
1.5       deraadt   823:        } else {
1.57      millert   824:                (void)snprintf(file1, sizeof(file1), "%s.0", oldlog);
1.51      millert   825:                if (noaction)
                    826:                        printf("\tmv %s to %s\n", ent->log, file1);
                    827:                else if (rename(ent->log, file1))
1.59      millert   828:                        warn("can't mv %s to %s", ent->log, file1);
1.5       deraadt   829:        }
                    830:
1.26      millert   831:        /* Now move the new log file into place */
                    832:        if (noaction)
1.51      millert   833:                printf("\tmv %s to %s\n", file2, ent->log);
                    834:        else if (rename(file2, ent->log))
1.59      millert   835:                warn("can't mv %s to %s", file2, ent->log);
1.1       deraadt   836: }
                    837:
                    838: /* Log the fact that the logs were turned over */
1.25      millert   839: int
1.48      millert   840: log_trim(char *log)
1.1       deraadt   841: {
1.35      deraadt   842:        FILE    *f;
1.25      millert   843:
1.35      deraadt   844:        if ((f = fopen(log, "a")) == NULL)
1.48      millert   845:                return (-1);
1.44      mpech     846:        (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
                    847:            daytime, hostname, (long)getpid());
1.35      deraadt   848:        if (fclose(f) == EOF)
                    849:                err(1, "log_trim: fclose");
1.48      millert   850:        return (0);
1.1       deraadt   851: }
                    852:
1.16      millert   853: /* Fork off compress or gzip to compress the old log file */
1.25      millert   854: void
1.51      millert   855: compress_log(struct conf_entry *ent)
1.1       deraadt   856: {
1.48      millert   857:        pid_t pid;
                    858:        char *base, tmp[MAXPATHLEN];
1.51      millert   859:
                    860:        if (ent->backdir != NULL)
                    861:                snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
                    862:                    ent->logbase);
                    863:        else
                    864:                snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
                    865:
1.27      millert   866:        if ((base = strrchr(COMPRESS, '/')) == NULL)
                    867:                base = COMPRESS;
                    868:        else
                    869:                base++;
                    870:        if (noaction) {
1.51      millert   871:                printf("%s %s\n", base, tmp);
1.27      millert   872:                return;
                    873:        }
1.35      deraadt   874:        pid = fork();
                    875:        if (pid < 0) {
1.11      downsj    876:                err(1, "fork");
1.50      millert   877:        } else if (pid == 0) {
1.37      deraadt   878:                (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16      millert   879:                warn(COMPRESS);
                    880:                _exit(1);
1.35      deraadt   881:        }
1.1       deraadt   882: }
                    883:
                    884: /* Return size in kilobytes of a file */
1.58      millert   885: off_t
1.48      millert   886: sizefile(char *file)
1.1       deraadt   887: {
1.35      deraadt   888:        struct stat sb;
1.1       deraadt   889:
1.35      deraadt   890:        if (stat(file, &sb) < 0)
1.48      millert   891:                return (-1);
1.58      millert   892:
                    893:        /* For sparse files, return the size based on number of blocks used. */
                    894:        if (sb.st_size / DEV_BSIZE > sb.st_blocks)
                    895:                return (sb.st_blocks * DEV_BSIZE);
                    896:        else
                    897:                return (sb.st_size);
1.1       deraadt   898: }
                    899:
1.25      millert   900: /* Return the age (in hours) of old log file (file.0), or -1 if none */
                    901: int
1.51      millert   902: age_old_log(struct conf_entry *ent)
1.1       deraadt   903: {
1.35      deraadt   904:        struct stat sb;
1.57      millert   905:        char file[MAXPATHLEN];
1.1       deraadt   906:
1.51      millert   907:        if (ent->backdir != NULL)
1.57      millert   908:                (void)snprintf(file, sizeof(file), "%s/%s.0", ent->backdir,
                    909:                    ent->logbase);
                    910:        else
                    911:                (void)snprintf(file, sizeof(file), "%s.0", ent->log);
                    912:        if (ent->flags & CE_COMPACT) {
                    913:                if (stat_suffix(file, sizeof(file), COMPRESS_POSTFIX, &sb,
                    914:                    stat) < 0 && stat(file, &sb) < 0)
                    915:                        return (-1);
                    916:        } else {
                    917:                if (stat(file, &sb) < 0 && stat_suffix(file, sizeof(file),
                    918:                    COMPRESS_POSTFIX, &sb, stat) < 0)
                    919:                        return (-1);
                    920:        }
1.48      millert   921:        return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1       deraadt   922: }
                    923:
                    924: /* Skip Over Blanks */
1.25      millert   925: char *
1.48      millert   926: sob(char *p)
1.1       deraadt   927: {
1.35      deraadt   928:        while (p && *p && isspace(*p))
                    929:                p++;
1.48      millert   930:        return (p);
1.1       deraadt   931: }
                    932:
                    933: /* Skip Over Non-Blanks */
1.25      millert   934: char *
1.48      millert   935: son(char *p)
1.1       deraadt   936: {
1.35      deraadt   937:        while (p && *p && !isspace(*p))
                    938:                p++;
1.48      millert   939:        return (p);
1.1       deraadt   940: }
                    941:
                    942: /* Check if string is actually a number */
1.25      millert   943: int
1.48      millert   944: isnumberstr(char *string)
1.1       deraadt   945: {
1.35      deraadt   946:        while (*string) {
                    947:                if (!isdigit(*string++))
1.48      millert   948:                        return (0);
1.35      deraadt   949:        }
1.48      millert   950:        return (1);
1.10      downsj    951: }
                    952:
1.61      millert   953: int
                    954: domonitor(struct conf_entry *ent)
1.10      downsj    955: {
                    956:        struct stat sb, tsb;
1.35      deraadt   957:        char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10      downsj    958:        FILE *fp;
                    959:        off_t osize;
                    960:        int rd;
                    961:
1.61      millert   962:        if (stat(ent->log, &sb) < 0)
                    963:                return (0);
                    964:
                    965:        if (noaction) {
                    966:                if (!verbose)
                    967:                        printf("%s: monitored\n", ent->log);
                    968:                return (1);
                    969:        }
1.10      downsj    970:
1.61      millert   971:        flog = strdup(ent->log);
1.11      downsj    972:        if (flog == NULL)
                    973:                err(1, "strdup");
                    974:
1.10      downsj    975:        for (p = flog; *p != '\0'; p++) {
                    976:                if (*p == '/')
                    977:                        *p = '_';
                    978:        }
1.48      millert   979:        snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35      deraadt   980:            STATS_DIR, flog);
1.10      downsj    981:
                    982:        /* ..if it doesn't exist, simply record the current size. */
                    983:        if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
                    984:                goto update;
                    985:
                    986:        fp = fopen(fname, "r");
                    987:        if (fp == NULL) {
1.34      millert   988:                warn("%s", fname);
1.10      downsj    989:                goto cleanup;
                    990:        }
                    991: #ifdef QUAD_OFF_T
1.70      millert   992:        if (fscanf(fp, "%lld\n", &osize) != 1) {
1.10      downsj    993: #else
                    994:        if (fscanf(fp, "%ld\n", &osize) != 1) {
                    995: #endif /* QUAD_OFF_T */
                    996:                fclose(fp);
                    997:                goto update;
                    998:        }
                    999:
                   1000:        fclose(fp);
                   1001:
                   1002:        /* If the file is smaller, mark the entire thing as changed. */
                   1003:        if (sb.st_size < osize)
                   1004:                osize = 0;
                   1005:
                   1006:        /* Now see if current size is larger. */
                   1007:        if (sb.st_size > osize) {
                   1008:                rb = (char *) malloc(sb.st_size - osize);
1.11      downsj   1009:                if (rb == NULL)
                   1010:                        err(1, "malloc");
1.10      downsj   1011:
                   1012:                /* Open logfile, seek. */
1.61      millert  1013:                fp = fopen(ent->log, "r");
1.10      downsj   1014:                if (fp == NULL) {
1.61      millert  1015:                        warn("%s", ent->log);
1.10      downsj   1016:                        goto cleanup;
                   1017:                }
                   1018:                fseek(fp, osize, SEEK_SET);
                   1019:                rd = fread(rb, 1, sb.st_size - osize, fp);
                   1020:                if (rd < 1) {
1.11      downsj   1021:                        warn("fread");
1.10      downsj   1022:                        fclose(fp);
                   1023:                        goto cleanup;
                   1024:                }
                   1025:
                   1026:                /* Send message. */
                   1027:                fclose(fp);
                   1028:
                   1029:                fp = openmail();
                   1030:                if (fp == NULL) {
1.11      downsj   1031:                        warn("openmail");
1.10      downsj   1032:                        goto cleanup;
                   1033:                }
                   1034:                fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
1.61      millert  1035:                    ent->whom, ent->log);
1.10      downsj   1036:                fwrite(rb, 1, rd, fp);
                   1037:                fputs("\n\n", fp);
                   1038:
1.48      millert  1039:                pclose(fp);
1.10      downsj   1040:        }
                   1041: update:
                   1042:        /* Reopen for writing and update file. */
                   1043:        fp = fopen(fname, "w");
                   1044:        if (fp == NULL) {
1.34      millert  1045:                warn("%s", fname);
1.10      downsj   1046:                goto cleanup;
                   1047:        }
                   1048: #ifdef QUAD_OFF_T
1.70      millert  1049:        fprintf(fp, "%lld\n", (long long)sb.st_size);
1.10      downsj   1050: #else
1.70      millert  1051:        fprintf(fp, "%ld\n", (long)sb.st_size);
1.10      downsj   1052: #endif /* QUAD_OFF_T */
                   1053:        fclose(fp);
                   1054:
                   1055: cleanup:
                   1056:        free(flog);
                   1057:        if (rb != NULL)
                   1058:                free(rb);
1.61      millert  1059:        return (1);
1.10      downsj   1060: }
                   1061:
1.25      millert  1062: FILE *
1.48      millert  1063: openmail(void)
1.10      downsj   1064: {
1.48      millert  1065:        FILE *ret;
1.35      deraadt  1066:        char *cmdbuf = NULL;
1.10      downsj   1067:
1.64      pvalchev 1068:        if (asprintf(&cmdbuf, "%s -t", SENDMAIL) != -1) {
1.35      deraadt  1069:                ret = popen(cmdbuf, "w");
                   1070:                free(cmdbuf);
                   1071:                return (ret);
                   1072:        }
                   1073:        return (NULL);
1.10      downsj   1074: }
                   1075:
1.25      millert  1076: void
1.48      millert  1077: child_killer(int signo)
1.16      millert  1078: {
1.38      deraadt  1079:        int save_errno = errno;
1.16      millert  1080:        int status;
                   1081:
                   1082:        while (waitpid(-1, &status, WNOHANG) > 0)
                   1083:                ;
1.38      deraadt  1084:        errno = save_errno;
1.57      millert  1085: }
                   1086:
                   1087: int
                   1088: stat_suffix(char *file, size_t size, char *suffix, struct stat *sp, int (*func)())
                   1089: {
                   1090:        size_t n;
                   1091:
                   1092:        n = strlcat(file, suffix, size);
                   1093:        if (n < size && func(file, sp) == 0)
                   1094:                return (0);
                   1095:        file[n - strlen(suffix)] = '\0';
                   1096:        return (-1);
                   1097: }
                   1098:
                   1099: /*
1.65      millert  1100:  * lstat() a log, possibly appending a suffix; order is based on flags.
1.57      millert  1101:  * Returns the suffix appended (may be empty string) or NULL if no file.
                   1102:  */
                   1103: char *
                   1104: lstat_log(char *file, size_t size, int flags)
                   1105: {
                   1106:        struct stat sb;
                   1107:
                   1108:        if (flags & CE_COMPACT) {
                   1109:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1110:                        return (COMPRESS_POSTFIX);
                   1111:                if (lstat(file, &sb) == 0)
                   1112:                        return ("");
                   1113:        } else {
                   1114:                if (lstat(file, &sb) == 0)
                   1115:                        return ("");
                   1116:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1117:                        return (COMPRESS_POSTFIX);
                   1118:
                   1119:        }
                   1120:        return (NULL);
1.63      millert  1121: }
                   1122:
                   1123: /*
                   1124:  * Parse a limited subset of ISO 8601. The specific format is as follows:
                   1125:  *
                   1126:  * [CC[YY[MM[DD]]]][THH[MM[SS]]]       (where `T' is the literal letter)
                   1127:  *
                   1128:  * We don't accept a timezone specification; missing fields (including timezone)
                   1129:  * are defaulted to the current date but time zero.
                   1130:  */
                   1131: time_t
                   1132: parse8601(char *s)
                   1133: {
                   1134:        char *t;
                   1135:        struct tm tm, *tmp;
1.70      millert  1136:        long l;
1.63      millert  1137:
                   1138:        tmp = localtime(&timenow);
                   1139:        tm = *tmp;
                   1140:
                   1141:        tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
                   1142:
1.70      millert  1143:        l = strtol(s, &t, 10);
                   1144:        if (l < 0 || l >= INT_MAX || (*t != '\0' && *t != 'T'))
1.63      millert  1145:                return (-1);
                   1146:
                   1147:        /*
                   1148:         * Now t points either to the end of the string (if no time was
                   1149:         * provided) or to the letter `T' which separates date and time in
                   1150:         * ISO 8601.  The pointer arithmetic is the same for either case.
                   1151:         */
                   1152:        switch (t - s) {
                   1153:        case 8:
1.70      millert  1154:                tm.tm_year = ((l / 1000000) - 19) * 100;
                   1155:                l = l % 1000000;
1.63      millert  1156:        case 6:
                   1157:                tm.tm_year -= tm.tm_year % 100;
1.70      millert  1158:                tm.tm_year += l / 10000;
                   1159:                l = l % 10000;
1.63      millert  1160:        case 4:
1.70      millert  1161:                tm.tm_mon = (l / 100) - 1;
                   1162:                l = l % 100;
1.63      millert  1163:        case 2:
1.70      millert  1164:                tm.tm_mday = l;
1.63      millert  1165:        case 0:
                   1166:                break;
                   1167:        default:
                   1168:                return (-1);
                   1169:        }
                   1170:
                   1171:        /* sanity check */
                   1172:        if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
                   1173:            || tm.tm_mday < 1 || tm.tm_mday > 31)
                   1174:                return (-1);
                   1175:
                   1176:        if (*t != '\0') {
                   1177:                s = ++t;
1.70      millert  1178:                l = strtol(s, &t, 10);
                   1179:                if (l < 0 || l >= INT_MAX || (*t != '\0' && !isspace(*t)))
1.63      millert  1180:                        return (-1);
                   1181:
                   1182:                switch (t - s) {
                   1183:                case 6:
1.70      millert  1184:                        tm.tm_sec = l % 100;
                   1185:                        l /= 100;
1.63      millert  1186:                case 4:
1.70      millert  1187:                        tm.tm_min = l % 100;
                   1188:                        l /= 100;
1.63      millert  1189:                case 2:
1.70      millert  1190:                        tm.tm_hour = l;
1.63      millert  1191:                case 0:
                   1192:                        break;
                   1193:                default:
                   1194:                        return (-1);
                   1195:                }
                   1196:
                   1197:                /* sanity check */
                   1198:                if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0
                   1199:                    || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
                   1200:                        return (-1);
                   1201:        }
                   1202:        return (mktime(&tm));
                   1203: }
                   1204:
                   1205: /*-
                   1206:  * Parse a cyclic time specification, the format is as follows:
                   1207:  *
                   1208:  *     [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
                   1209:  *
                   1210:  * to rotate a logfile cyclic at
                   1211:  *
                   1212:  *     - every day (D) within a specific hour (hh)     (hh = 0...23)
                   1213:  *     - once a week (W) at a specific day (d)     OR  (d = 0..6, 0 = Sunday)
                   1214:  *     - once a month (M) at a specific day (d)        (d = 1..31,l|L)
                   1215:  *
                   1216:  * We don't accept a timezone specification; missing fields
                   1217:  * are defaulted to the current date but time zero.
                   1218:  */
                   1219: time_t
                   1220: parseDWM(char *s)
                   1221: {
                   1222:        char *t;
                   1223:        struct tm tm, *tmp;
                   1224:        long l;
                   1225:        int nd;
                   1226:        static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
                   1227:        int WMseen = 0;
                   1228:        int Dseen = 0;
                   1229:
                   1230:        tmp = localtime(&timenow);
                   1231:        tm = *tmp;
                   1232:
                   1233:        /* set no. of days per month */
                   1234:
                   1235:        nd = mtab[tm.tm_mon];
                   1236:
                   1237:        if (tm.tm_mon == 1) {
                   1238:                if (((tm.tm_year + 1900) % 4 == 0) &&
                   1239:                    ((tm.tm_year + 1900) % 100 != 0) &&
                   1240:                    ((tm.tm_year + 1900) % 400 == 0)) {
                   1241:                        nd++;   /* leap year, 29 days in february */
                   1242:                }
                   1243:        }
                   1244:        tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
                   1245:
                   1246:        for (;;) {
                   1247:                switch (*s) {
                   1248:                case 'D':
                   1249:                        if (Dseen)
                   1250:                                return (-1);
                   1251:                        Dseen++;
                   1252:                        s++;
                   1253:                        l = strtol(s, &t, 10);
                   1254:                        if (l < 0 || l > 23)
                   1255:                                return (-1);
                   1256:                        tm.tm_hour = l;
                   1257:                        break;
                   1258:
                   1259:                case 'W':
                   1260:                        if (WMseen)
                   1261:                                return (-1);
                   1262:                        WMseen++;
                   1263:                        s++;
                   1264:                        l = strtol(s, &t, 10);
                   1265:                        if (l < 0 || l > 6)
                   1266:                                return (-1);
                   1267:                        if (l != tm.tm_wday) {
                   1268:                                int save;
                   1269:
                   1270:                                if (l < tm.tm_wday) {
                   1271:                                        save = 6 - tm.tm_wday;
                   1272:                                        save += (l + 1);
                   1273:                                } else {
                   1274:                                        save = l - tm.tm_wday;
                   1275:                                }
                   1276:
                   1277:                                tm.tm_mday += save;
                   1278:
                   1279:                                if (tm.tm_mday > nd) {
                   1280:                                        tm.tm_mon++;
                   1281:                                        tm.tm_mday = tm.tm_mday - nd;
                   1282:                                }
                   1283:                        }
                   1284:                        break;
                   1285:
                   1286:                case 'M':
                   1287:                        if (WMseen)
                   1288:                                return (-1);
                   1289:                        WMseen++;
                   1290:                        s++;
                   1291:                        if (tolower(*s) == 'l') {
                   1292:                                tm.tm_mday = nd;
                   1293:                                s++;
                   1294:                                t = s;
                   1295:                        } else {
                   1296:                                l = strtol(s, &t, 10);
                   1297:                                if (l < 1 || l > 31)
                   1298:                                        return (-1);
                   1299:
                   1300:                                if (l > nd)
                   1301:                                        return (-1);
                   1302:                                tm.tm_mday = l;
                   1303:                        }
                   1304:                        break;
                   1305:
                   1306:                default:
                   1307:                        return (-1);
                   1308:                        break;
                   1309:                }
                   1310:
                   1311:                if (*t == '\0' || isspace(*t))
                   1312:                        break;
                   1313:                else
                   1314:                        s = t;
                   1315:        }
                   1316:        return (mktime(&tm));
1.1       deraadt  1317: }