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

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