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

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