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

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