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

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