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

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