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

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