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

1.62    ! millert     1: /*     $OpenBSD: newsyslog.c,v 1.61 2003/01/25 05:13:02 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:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  * 3. The name of the author may not be used to endorse or promote products
                     16:  *    derived from this software without specific prior written permission.
                     17:  *
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
                     19:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
                     20:  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
                     21:  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                     22:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                     23:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
                     24:  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
                     25:  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
                     26:  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
                     27:  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
1.10      downsj     31:  * Copyright (c) 1997, Jason Downs.  All rights reserved.
                     32:  *
                     33:  * Redistribution and use in source and binary forms, with or without
                     34:  * modification, are permitted provided that the following conditions
                     35:  * are met:
                     36:  * 1. Redistributions of source code must retain the above copyright
                     37:  *    notice, this list of conditions and the following disclaimer.
                     38:  * 2. Redistributions in binary form must reproduce the above copyright
                     39:  *    notice, this list of conditions and the following disclaimer in the
                     40:  *    documentation and/or other materials provided with the distribution.
                     41:  * 3. All advertising materials mentioning features or use of this software
                     42:  *    must display the following acknowledgement:
                     43:  *      This product includes software developed by Jason Downs for the
                     44:  *      OpenBSD system.
                     45:  * 4. Neither the name(s) of the author(s) nor the name OpenBSD
                     46:  *    may be used to endorse or promote products derived from this software
                     47:  *    without specific prior written permission.
                     48:  *
                     49:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
                     50:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     51:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
                     52:  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
                     53:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
                     54:  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
                     55:  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
                     56:  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     57:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     58:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     59:  * SUCH DAMAGE.
                     60:  */
1.3       deraadt    61:
1.1       deraadt    62: /*
                     63:  * This file contains changes from the Open Software Foundation.
                     64:  */
                     65:
                     66: /*
1.50      millert    67:  * Copyright 1988, 1989 by the Massachusetts Institute of Technology
                     68:  *
                     69:  * Permission to use, copy, modify, and distribute this software
                     70:  * and its documentation for any purpose and without fee is
                     71:  * hereby granted, provided that the above copyright notice
                     72:  * appear in all copies and that both that copyright notice and
                     73:  * this permission notice appear in supporting documentation,
                     74:  * and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
                     75:  * used in advertising or publicity pertaining to distribution
                     76:  * of the software without specific, written prior permission.
                     77:  * M.I.T. and the M.I.T. S.I.P.B. make no representations about
                     78:  * the suitability of this software for any purpose.  It is
                     79:  * provided "as is" without express or implied warranty.
                     80:  */
1.1       deraadt    81:
                     82: /*
                     83:  *      newsyslog - roll over selected logs at the appropriate time,
                     84:  *              keeping the a specified number of backup files around.
                     85:  *
                     86:  */
                     87:
                     88: #ifndef lint
1.62    ! millert    89: static const char rcsid[] = "$OpenBSD: newsyslog.c,v 1.61 2003/01/25 05:13:02 millert Exp $";
1.1       deraadt    90: #endif /* not lint */
                     91:
                     92: #ifndef CONF
1.47      millert    93: #define CONF "/etc/newsyslog.conf" /* Configuration file */
1.1       deraadt    94: #endif
                     95: #ifndef PIDFILE
                     96: #define PIDFILE "/etc/syslog.pid"
                     97: #endif
                     98: #ifndef COMPRESS
1.47      millert    99: #define COMPRESS "/usr/bin/compress" /* File compression program */
1.1       deraadt   100: #endif
                    101: #ifndef COMPRESS_POSTFIX
                    102: #define COMPRESS_POSTFIX ".Z"
                    103: #endif
1.10      downsj    104: #ifndef STATS_DIR
                    105: #define STATS_DIR "/etc"
                    106: #endif
                    107: #ifndef SENDMAIL
                    108: #define SENDMAIL "/usr/lib/sendmail"
                    109: #endif
1.1       deraadt   110:
1.48      millert   111: #include <sys/param.h>
                    112: #include <sys/stat.h>
1.9       downsj    113: #include <sys/time.h>
                    114: #include <sys/wait.h>
1.48      millert   115:
1.1       deraadt   116: #include <ctype.h>
1.48      millert   117: #include <err.h>
                    118: #include <errno.h>
1.9       downsj    119: #include <fcntl.h>
1.1       deraadt   120: #include <grp.h>
1.47      millert   121: #include <limits.h>
1.48      millert   122: #include <pwd.h>
                    123: #include <signal.h>
                    124: #include <stdio.h>
                    125: #include <stdlib.h>
                    126: #include <string.h>
1.9       downsj    127: #include <unistd.h>
1.1       deraadt   128:
1.26      millert   129: #define CE_ROTATED     0x01            /* Log file has been rotated */
                    130: #define CE_COMPACT     0x02            /* Compact the achived log files */
                    131: #define CE_BINARY      0x04            /* Logfile is in binary, don't add */
1.10      downsj    132:                                        /* status messages */
1.26      millert   133: #define CE_MONITOR     0x08            /* Monitory for changes */
1.45      wcobb     134: #define CE_FOLLOW      0x10            /* Follow symbolic links */
1.35      deraadt   135:
1.49      millert   136: #define        MIN_PID         4               /* Don't touch pids lower than this */
1.58      millert   137: #define        MIN_SIZE        256             /* Don't rotate if smaller (in bytes) */
1.49      millert   138:
1.50      millert   139: #define        DPRINTF(x)      do { if (verbose) printf x ; } while (0)
                    140:
1.1       deraadt   141: struct conf_entry {
1.35      deraadt   142:        char    *log;           /* Name of the log */
1.51      millert   143:        char    *logbase;       /* Basename of the log */
                    144:        char    *backdir;       /* Directory in which to store backups */
1.35      deraadt   145:        uid_t   uid;            /* Owner of log */
                    146:        gid_t   gid;            /* Group of log */
                    147:        int     numlogs;        /* Number of logs to keep */
1.58      millert   148:        off_t   size;           /* Size cutoff to trigger trimming the log */
1.35      deraadt   149:        int     hours;          /* Hours between log trimming */
                    150:        int     permissions;    /* File permissions on the log */
1.30      millert   151:        int     signal;         /* Signal to send (defaults to SIGHUP) */
1.35      deraadt   152:        int     flags;          /* Flags (CE_COMPACT & CE_BINARY)  */
1.10      downsj    153:        char    *whom;          /* Whom to notify if logfile changes */
1.30      millert   154:        char    *pidfile;       /* Path to file containg pid to signal */
                    155:        char    *runcmd;        /* Command to run instead of sending a signal */
1.35      deraadt   156:        struct conf_entry *next; /* Linked list pointer */
1.1       deraadt   157: };
                    158:
1.30      millert   159: struct pidinfo {
                    160:        char    *file;
                    161:        int     signal;
                    162: };
                    163:
1.52      millert   164: int    verbose = 0;            /* Print out what's going on */
                    165: int    needroot = 1;           /* Root privs are necessary */
                    166: int    noaction = 0;           /* Don't do anything, just show it */
1.33      millert   167: int    monitormode = 0;        /* Don't do monitoring by default */
1.52      millert   168: int    force = 0;              /* Force the logs to be rotated */
                    169: char   *conf = CONF;           /* Configuration file to use */
                    170: time_t timenow;
                    171: char   hostname[MAXHOSTNAMELEN]; /* hostname */
                    172: char   *daytime;               /* timenow in human readable form */
                    173: char   *arcdir;                /* dir to put archives in (if it exists) */
1.1       deraadt   174:
1.43      millert   175: void do_entry(struct conf_entry *);
1.48      millert   176: void parse_args(int, char **);
1.43      millert   177: void usage(void);
                    178: struct conf_entry *parse_file(int *);
1.60      millert   179: char *missing_field(char *, char *, int);
1.51      millert   180: void dotrim(struct conf_entry *);
1.43      millert   181: int log_trim(char *);
1.51      millert   182: void compress_log(struct conf_entry *);
1.58      millert   183: off_t sizefile(char *);
1.51      millert   184: int age_old_log(struct conf_entry *);
1.43      millert   185: char *sob(char *);
                    186: char *son(char *);
                    187: int isnumberstr(char *);
1.61      millert   188: int domonitor(struct conf_entry *);
1.43      millert   189: FILE *openmail(void);
                    190: void child_killer(int);
                    191: void run_command(char *);
                    192: void send_signal(char *, int);
1.57      millert   193: char *lstat_log(char *, size_t, int);
                    194: int stat_suffix(char *, size_t, char *, struct stat *, int (*)());
1.1       deraadt   195:
1.25      millert   196: int
1.48      millert   197: main(int argc, char **argv)
1.1       deraadt   198: {
1.53      millert   199:        struct conf_entry *p, *q, *x, *y;
1.30      millert   200:        struct pidinfo *pidlist, *pl;
1.53      millert   201:        char **av;
1.28      millert   202:        int status, listlen;
1.35      deraadt   203:
1.48      millert   204:        parse_args(argc, argv);
1.53      millert   205:        argc -= optind;
                    206:        argv += optind;
                    207:
1.35      deraadt   208:        if (needroot && getuid() && geteuid())
1.11      downsj    209:                errx(1, "You must be root.");
1.53      millert   210:
                    211:        p = parse_file(&listlen);
                    212:        if (argc > 0) {
                    213:                /* Only rotate specified files. */
                    214:                x = y = NULL;
                    215:                listlen = 0;
                    216:                for (av = argv; *av; av++) {
                    217:                        for (q = p; q; q = q->next)
                    218:                                if (strcmp(*av, q->log) == 0) {
                    219:                                        if (x == NULL)
                    220:                                                x = y = q;
                    221:                                        else {
                    222:                                                y->next = q;
                    223:                                                y = q;
                    224:                                        }
                    225:                                        listlen++;
                    226:                                        break;
                    227:                                }
                    228:                        if (q == NULL)
1.60      millert   229:                                warnx("%s: %s not found", conf, *av);
1.53      millert   230:                }
                    231:                if (x == NULL)
1.60      millert   232:                        errx(1, "%s: no specified log files", conf);
1.53      millert   233:                y->next = NULL;
                    234:                p = x;
                    235:        }
1.26      millert   236:
1.46      aaron     237:        pidlist = (struct pidinfo *)calloc(listlen + 1, sizeof(struct pidinfo));
1.30      millert   238:        if (pidlist == NULL)
1.28      millert   239:                err(1, "calloc");
                    240:
1.53      millert   241:        signal(SIGCHLD, child_killer);
                    242:
1.26      millert   243:        /* Step 1, rotate all log files */
1.53      millert   244:        for (q = p; q; q = q->next)
1.35      deraadt   245:                do_entry(q);
1.26      millert   246:
1.28      millert   247:        /* Step 2, make a list of unique pid files */
1.30      millert   248:        for (q = p, pl = pidlist; q; ) {
1.28      millert   249:                if (q->flags & CE_ROTATED) {
1.30      millert   250:                        struct pidinfo *pltmp;
1.28      millert   251:
1.30      millert   252:                        for (pltmp = pidlist; pltmp < pl; pltmp++) {
1.55      millert   253:                                if ((q->pidfile &&
                    254:                                    strcmp(pltmp->file, q->pidfile) == 0 &&
                    255:                                    pltmp->signal == q->signal) ||
                    256:                                    (q->runcmd &&
1.30      millert   257:                                    strcmp(q->runcmd, pltmp->file) == 0))
1.28      millert   258:                                        break;
1.30      millert   259:                        }
                    260:                        if (pltmp == pl) {      /* unique entry */
                    261:                                if (q->runcmd) {
                    262:                                        pl->file = q->runcmd;
                    263:                                        pl->signal = -1;
                    264:                                } else {
                    265:                                        pl->file = q->pidfile;
                    266:                                        pl->signal = q->signal;
                    267:                                }
1.31      millert   268:                                pl++;
1.30      millert   269:                        }
1.28      millert   270:                }
1.35      deraadt   271:                q = q->next;
                    272:        }
1.26      millert   273:
1.30      millert   274:        /* Step 3, send a signal or run a command */
                    275:        for (pl = pidlist; pl->file; pl++) {
1.56      millert   276:                if (pl->file != NULL) {
                    277:                        if (pl->signal == -1)
                    278:                                run_command(pl->file);
                    279:                        else
                    280:                                send_signal(pl->file, pl->signal);
                    281:                }
1.30      millert   282:        }
1.28      millert   283:        if (!noaction)
                    284:                sleep(5);
                    285:
                    286:        /* Step 4, compress the log.0 file if configured to do so and free */
1.35      deraadt   287:        while (p) {
1.26      millert   288:                if ((p->flags & CE_COMPACT) && (p->flags & CE_ROTATED))
1.51      millert   289:                        compress_log(p);
1.26      millert   290:                q = p;
1.35      deraadt   291:                p = p->next;
                    292:                free(q);
                    293:        }
1.16      millert   294:
                    295:        /* Wait for children to finish, then exit */
                    296:        while (waitpid(-1, &status, 0) != -1)
                    297:                ;
1.35      deraadt   298:        exit(0);
1.1       deraadt   299: }
                    300:
1.25      millert   301: void
1.48      millert   302: do_entry(struct conf_entry *ent)
1.1       deraadt   303: {
1.58      millert   304:        int modtime;
                    305:        off_t size;
1.48      millert   306:        struct stat sb;
1.45      wcobb     307:
1.51      millert   308:        if (lstat(ent->log, &sb) != 0)
1.50      millert   309:                return;
                    310:        if (!S_ISREG(sb.st_mode) &&
                    311:            (!S_ISLNK(sb.st_mode) || !(ent->flags & CE_FOLLOW))) {
                    312:                DPRINTF(("--> not a regular file, skipping\n"));
                    313:                return;
1.45      wcobb     314:        }
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.50      millert   321:
1.35      deraadt   322:        size = sizefile(ent->log);
1.51      millert   323:        modtime = age_old_log(ent);
1.35      deraadt   324:        if (size < 0) {
1.50      millert   325:                DPRINTF(("does not exist.\n"));
1.35      deraadt   326:        } else {
1.50      millert   327:                if (ent->size > 0)
1.58      millert   328:                        DPRINTF(("size (KB): %.2f [%d] ", size / 1024.0,
                    329:                            (int)(ent->size / 1024)));
1.50      millert   330:                if (ent->hours > 0)
                    331:                        DPRINTF(("age (hr): %d [%d] ", modtime, ent->hours));
1.61      millert   332:                if (monitormode && (ent->flags & CE_MONITOR) && domonitor(ent))
                    333:                        DPRINTF(("--> monitored\n"));
                    334:                else if (!monitormode &&
                    335:                    (force || (ent->size > 0 && size >= ent->size) ||
1.49      millert   336:                    (ent->hours > 0 && (modtime >= ent->hours || modtime < 0)
                    337:                    && ((ent->flags & CE_BINARY) || size >= MIN_SIZE)))) {
1.50      millert   338:                        DPRINTF(("--> trimming log....\n"));
1.19      kstailey  339:                        if (noaction && !verbose)
1.50      millert   340:                                printf("%s <%d%s%s%s>\n", ent->log,
                    341:                                    ent->numlogs,
                    342:                                    (ent->flags & CE_COMPACT) ? "Z" : "",
                    343:                                    (ent->flags & CE_BINARY) ? "B" : "",
                    344:                                    (ent->flags & CE_FOLLOW) ? "F" : "");
1.51      millert   345:                        dotrim(ent);
1.26      millert   346:                        ent->flags |= CE_ROTATED;
1.50      millert   347:                } else
                    348:                        DPRINTF(("--> skipping\n"));
1.35      deraadt   349:        }
1.1       deraadt   350: }
                    351:
1.30      millert   352: /* Run the specified command */
                    353: void
1.48      millert   354: run_command(char *cmd)
1.30      millert   355: {
                    356:        if (noaction)
1.55      millert   357:                (void)printf("run %s\n", cmd);
1.30      millert   358:        else
                    359:                system(cmd);
                    360: }
                    361:
                    362: /* Send a signal to the pid specified by pidfile */
1.26      millert   363: void
1.48      millert   364: send_signal(char *pidfile, int signal)
1.26      millert   365: {
1.48      millert   366:        pid_t pid;
                    367:        FILE *f;
                    368:        char line[BUFSIZ], *ep, *err;
1.40      deraadt   369:        unsigned long ulval;
1.26      millert   370:
1.35      deraadt   371:        if ((f = fopen(pidfile, "r")) == NULL) {
1.26      millert   372:                warn("can't open %s", pidfile);
                    373:                return;
                    374:        }
                    375:
1.48      millert   376:        pid = 0;
1.42      millert   377:        errno = 0;
                    378:        err = NULL;
1.40      deraadt   379:        if (fgets(line, sizeof(line), f)) {
                    380:                ulval = strtoul(line, &ep, 10);
1.41      marc      381:                if (line[0] == '\0' || (*ep != '\0' && *ep != '\n'))
1.40      deraadt   382:                        err = "invalid number in";
                    383:                else if (errno == ERANGE && ulval == ULONG_MAX)
                    384:                        err = "out of range number in";
                    385:                else if (ulval == 0)
                    386:                        err = "no number in";
                    387:                else if (ulval < MIN_PID)
                    388:                        err = "preposterous process number in";
                    389:                else
                    390:                        pid = ulval;
1.42      millert   391:        } else {
                    392:                if (errno == 0)
                    393:                        err = "empty";
                    394:                else
                    395:                        err = "error reading";
1.40      deraadt   396:        }
1.26      millert   397:        (void)fclose(f);
                    398:
1.40      deraadt   399:        if (err)
                    400:                warnx("%s pid file: %s", err, pidfile);
                    401:        else if (noaction)
1.44      mpech     402:                (void)printf("kill -%s %ld\n", sys_signame[signal], (long)pid);
1.30      millert   403:        else if (kill(pid, signal))
                    404:                warnx("warning - could not send SIG%s to daemon",
                    405:                    sys_signame[signal]);
1.26      millert   406: }
                    407:
1.25      millert   408: void
1.48      millert   409: parse_args(int argc, char **argv)
1.1       deraadt   410: {
1.48      millert   411:        int ch;
                    412:        char *p;
1.1       deraadt   413:
1.35      deraadt   414:        timenow = time(NULL);
                    415:        daytime = ctime(&timenow) + 4;
                    416:        daytime[15] = '\0';
1.1       deraadt   417:
1.35      deraadt   418:        /* Let's get our hostname */
                    419:        (void)gethostname(hostname, sizeof(hostname));
1.1       deraadt   420:
                    421:        /* Truncate domain */
1.48      millert   422:        if ((p = strchr(hostname, '.')) != NULL)
1.1       deraadt   423:                *p = '\0';
                    424:
1.52      millert   425:        while ((ch = getopt(argc, argv, "Fmnrva:f:")) != -1) {
1.48      millert   426:                switch (ch) {
1.51      millert   427:                case 'a':
                    428:                        arcdir = optarg;
                    429:                        break;
1.35      deraadt   430:                case 'n':
                    431:                        noaction++; /* This implies needroot as off */
                    432:                        /* fall through */
                    433:                case 'r':
                    434:                        needroot = 0;
                    435:                        break;
                    436:                case 'v':
                    437:                        verbose++;
                    438:                        break;
                    439:                case 'f':
                    440:                        conf = optarg;
                    441:                        break;
1.10      downsj    442:                case 'm':
1.33      millert   443:                        monitormode++;
1.10      downsj    444:                        break;
1.52      millert   445:                case 'F':
                    446:                        force++;
                    447:                        break;
1.35      deraadt   448:                default:
                    449:                        usage();
                    450:                }
                    451:        }
1.52      millert   452:        if (monitormode && force)
                    453:                errx(1, "cannot specify both -m and -F flags");
1.9       downsj    454: }
1.1       deraadt   455:
1.25      millert   456: void
1.48      millert   457: usage(void)
1.1       deraadt   458: {
1.25      millert   459:        extern const char *__progname;
                    460:
1.52      millert   461:        (void)fprintf(stderr, "usage: %s [-Fmnrv] [-a directory] "
1.53      millert   462:            "[-f config_file] [log ...]\n", __progname);
1.14      millert   463:        exit(1);
1.1       deraadt   464: }
                    465:
1.52      millert   466: /*
                    467:  * Parse a configuration file and return a linked list of all the logs
1.1       deraadt   468:  * to process
                    469:  */
1.25      millert   470: struct conf_entry *
1.48      millert   471: parse_file(int *nentries)
1.1       deraadt   472: {
1.48      millert   473:        FILE *f;
                    474:        char line[BUFSIZ], *parse, *q, *errline, *group, *tmp;
1.60      millert   475:        int lineno;
1.35      deraadt   476:        struct conf_entry *first = NULL;
                    477:        struct conf_entry *working = NULL;
1.48      millert   478:        struct passwd *pwd;
1.35      deraadt   479:        struct group *grp;
1.51      millert   480:        struct stat sb;
1.1       deraadt   481:
1.35      deraadt   482:        if (strcmp(conf, "-") == 0)
                    483:                f = stdin;
1.48      millert   484:        else if ((f = fopen(conf, "r")) == NULL)
                    485:                err(1, "can't open %s", conf);
1.11      downsj    486:
1.28      millert   487:        *nentries = 0;
1.60      millert   488:        for (lineno = 0; fgets(line, sizeof(line), f); lineno++) {
1.48      millert   489:                tmp = sob(line);
1.54      millert   490:                if (*tmp == '\0' || *tmp == '#')
1.35      deraadt   491:                        continue;
1.48      millert   492:                errline = strdup(tmp);
1.11      downsj    493:                if (errline == NULL)
                    494:                        err(1, "strdup");
1.28      millert   495:                (*nentries)++;
1.35      deraadt   496:                if (!first) {
                    497:                        working = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    498:                        if (working == NULL)
                    499:                                err(1, "malloc");
1.35      deraadt   500:                        first = working;
                    501:                } else {
                    502:                        working->next = (struct conf_entry *) malloc(sizeof(struct conf_entry));
1.11      downsj    503:                        if (working->next == NULL)
                    504:                                err(1, "malloc");
1.35      deraadt   505:                        working = working->next;
                    506:                }
1.1       deraadt   507:
1.60      millert   508:                q = parse = missing_field(sob(line), errline, lineno);
1.35      deraadt   509:                *(parse = son(line)) = '\0';
                    510:                working->log = strdup(q);
1.11      downsj    511:                if (working->log == NULL)
                    512:                        err(1, "strdup");
1.1       deraadt   513:
1.51      millert   514:                if ((working->logbase = strrchr(working->log, '/')) != NULL)
                    515:                        working->logbase++;
                    516:
1.60      millert   517:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   518:                *(parse = son(parse)) = '\0';
1.25      millert   519:                if ((group = strchr(q, '.')) != NULL) {
                    520:                        *group++ = '\0';
                    521:                        if (*q) {
                    522:                                if (!(isnumberstr(q))) {
1.48      millert   523:                                        if ((pwd = getpwnam(q)) == NULL)
1.60      millert   524:                                                errx(1, "%s:%d: unknown user: %s",
                    525:                                                    conf, lineno, q);
1.48      millert   526:                                        working->uid = pwd->pw_uid;
1.25      millert   527:                                } else
                    528:                                        working->uid = atoi(q);
                    529:                        } else
1.50      millert   530:                                working->uid = (uid_t)-1;
1.25      millert   531:
                    532:                        q = group;
                    533:                        if (*q) {
                    534:                                if (!(isnumberstr(q))) {
                    535:                                        if ((grp = getgrnam(q)) == NULL)
1.60      millert   536:
                    537:                                                errx(1, "%s:%d: unknown group: %s",
                    538:                                                    conf, lineno, q);
1.25      millert   539:                                        working->gid = grp->gr_gid;
                    540:                                } else
                    541:                                        working->gid = atoi(q);
                    542:                        } else
1.50      millert   543:                                working->gid = (gid_t)-1;
1.25      millert   544:
1.60      millert   545:                        q = parse = missing_field(sob(++parse), errline, lineno);
1.25      millert   546:                        *(parse = son(parse)) = '\0';
1.50      millert   547:                } else {
                    548:                        working->uid = (uid_t)-1;
                    549:                        working->gid = (gid_t)-1;
                    550:                }
1.1       deraadt   551:
1.35      deraadt   552:                if (!sscanf(q, "%o", &working->permissions))
1.60      millert   553:                        errx(1, "%s:%d: bad permissions: %s", conf, lineno, q);
1.1       deraadt   554:
1.60      millert   555:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   556:                *(parse = son(parse)) = '\0';
                    557:                if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0)
1.60      millert   558:                        errx(1, "%s:%d: bad number: %s", conf, lineno, q);
1.1       deraadt   559:
1.60      millert   560:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   561:                *(parse = son(parse)) = '\0';
                    562:                if (isdigit(*q))
1.58      millert   563:                        working->size = atoi(q) * 1024;
1.35      deraadt   564:                else
                    565:                        working->size = -1;
                    566:
1.60      millert   567:                q = parse = missing_field(sob(++parse), errline, lineno);
1.35      deraadt   568:                *(parse = son(parse)) = '\0';
                    569:                if (isdigit(*q))
                    570:                        working->hours = atoi(q);
                    571:                else
                    572:                        working->hours = -1;
1.1       deraadt   573:
1.35      deraadt   574:                working->flags = 0;
                    575:                q = sob(++parse);       /* Optional field */
1.32      millert   576:                if (*q == 'Z' || *q == 'z' || *q == 'B' || *q == 'b' ||
                    577:                    *q == 'M' || *q == 'm') {
                    578:                        *(parse = son(q)) = '\0';
                    579:                        while (*q) {
                    580:                                switch (*q) {
                    581:                                case 'Z':
                    582:                                case 'z':
                    583:                                        working->flags |= CE_COMPACT;
                    584:                                        break;
                    585:                                case 'B':
                    586:                                case 'b':
                    587:                                        working->flags |= CE_BINARY;
                    588:                                        break;
                    589:                                case 'M':
                    590:                                case 'm':
                    591:                                        working->flags |= CE_MONITOR;
1.45      wcobb     592:                                        break;
                    593:                                case 'F':
                    594:                                case 'f':
                    595:                                        working->flags |= CE_FOLLOW;
1.32      millert   596:                                        break;
                    597:                                default:
1.60      millert   598:                                        errx(1, "%s:%d: illegal flag: `%c'",
                    599:                                            conf, lineno, *q);
1.32      millert   600:                                        break;
                    601:                                }
                    602:                                q++;
                    603:                        }
                    604:                } else
1.40      deraadt   605:                        parse--;        /* no flags so undo */
1.10      downsj    606:
1.14      millert   607:                working->pidfile = PIDFILE;
1.30      millert   608:                working->signal = SIGHUP;
                    609:                working->runcmd = NULL;
1.61      millert   610:                working->whom = NULL;
1.30      millert   611:                for (;;) {
                    612:                        q = parse = sob(++parse);       /* Optional field */
                    613:                        if (q == NULL || *q == '\0')
                    614:                                break;
                    615:                        if (*q == '/') {
                    616:                                *(parse = son(parse)) = '\0';
                    617:                                if (strlen(q) >= MAXPATHLEN)
1.60      millert   618:                                        errx(1, "%s:%d: pathname too long: %s",
                    619:                                            conf, lineno, q);
1.30      millert   620:                                working->pidfile = strdup(q);
                    621:                                if (working->pidfile == NULL)
                    622:                                        err(1, "strdup");
                    623:                        } else if (*q == '"' && (tmp = strchr(q + 1, '"'))) {
                    624:                                *(parse = tmp) = '\0';
1.56      millert   625:                                if (*++q != '\0') {
                    626:                                        working->runcmd = strdup(q);
                    627:                                        if (working->runcmd == NULL)
                    628:                                                err(1, "strdup");
                    629:                                }
1.55      millert   630:                                working->pidfile = NULL;
                    631:                                working->signal = -1;
1.30      millert   632:                        } else if (strncmp(q, "SIG", 3) == 0) {
                    633:                                int i;
                    634:
                    635:                                *(parse = son(parse)) = '\0';
                    636:                                for (i = 1; i < NSIG; i++) {
                    637:                                        if (!strcmp(sys_signame[i], q + 3)) {
                    638:                                                working->signal = i;
                    639:                                                break;
                    640:                                        }
                    641:                                }
                    642:                                if (i == NSIG)
1.60      millert   643:                                        errx(1, "%s:%d: unknown signal: %s",
                    644:                                            conf, lineno, q);
1.61      millert   645:                        } else if (working->flags & CE_MONITOR) {
                    646:                                *(parse = son(parse)) = '\0';
                    647:                                working->whom = strdup(q);
                    648:                                if (working->whom == NULL)
                    649:                                        err(1, "strdup");
1.30      millert   650:                        } else
1.60      millert   651:                                errx(1, "%s:%d: unrecognized field: %s",
                    652:                                    conf, lineno, q);
1.14      millert   653:                }
1.51      millert   654:                free(errline);
                    655:
1.61      millert   656:                if ((working->flags & CE_MONITOR) && working->whom == NULL)
                    657:                        errx(1, "%s:%d: missing monitor notification field",
                    658:                            conf, lineno);
                    659:
1.51      millert   660:                /* If there is an arcdir, set working->backdir. */
                    661:                if (arcdir != NULL && working->logbase != NULL) {
                    662:                        if (*arcdir == '/') {
                    663:                                /* Fully qualified arcdir */
                    664:                                working->backdir = arcdir;
                    665:                        } else {
                    666:                                /* arcdir is relative to log's parent dir */
                    667:                                *(working->logbase - 1) = '\0';
                    668:                                if ((asprintf(&working->backdir, "%s/%s",
                    669:                                    working->log, arcdir)) == -1)
                    670:                                        err(1, "malloc");
                    671:                                *(working->logbase - 1) = '/';
                    672:                        }
                    673:                        /* Ignore arcdir if it doesn't exist. */
                    674:                        if (stat(working->backdir, &sb) != 0 ||
                    675:                            !S_ISDIR(sb.st_mode)) {
                    676:                                if (working->backdir != arcdir)
                    677:                                        free(working->backdir);
                    678:                                working->backdir = NULL;
                    679:                        }
                    680:                } else
                    681:                        working->backdir = NULL;
1.25      millert   682:
                    683:                /* Make sure we can't oflow MAXPATHLEN */
1.51      millert   684:                if (working->backdir != NULL) {
                    685:                        if (snprintf(line, sizeof(line), "%s/%s.%d%s",
                    686:                            working->backdir, working->logbase,
                    687:                            working->numlogs, COMPRESS_POSTFIX) >= MAXPATHLEN)
1.60      millert   688:                                errx(1, "%s:%d: pathname too long: %s",
                    689:                                    conf, lineno, q);
1.51      millert   690:                } else {
                    691:                        if (snprintf(line, sizeof(line), "%s.%d%s",
                    692:                            working->log, working->numlogs, COMPRESS_POSTFIX)
                    693:                            >= MAXPATHLEN)
1.60      millert   694:                                errx(1, "%s:%d: pathname too long: %s",
                    695:                                    conf, lineno, working->log);
1.51      millert   696:                }
1.35      deraadt   697:        }
                    698:        if (working)
                    699:                working->next = NULL;
                    700:        (void)fclose(f);
1.48      millert   701:        return (first);
1.1       deraadt   702: }
                    703:
1.25      millert   704: char *
1.60      millert   705: missing_field(char *p, char *errline, int lineno)
1.1       deraadt   706: {
1.60      millert   707:        if (p == NULL || *p == '\0') {
                    708:                warnx("%s:%d: missing field", conf, lineno);
1.35      deraadt   709:                fputs(errline, stderr);
                    710:                exit(1);
                    711:        }
1.48      millert   712:        return (p);
1.1       deraadt   713: }
                    714:
1.25      millert   715: void
1.51      millert   716: dotrim(struct conf_entry *ent)
1.35      deraadt   717: {
                    718:        char    file1[MAXPATHLEN], file2[MAXPATHLEN];
1.57      millert   719:        char    oldlog[MAXPATHLEN], *suffix;
1.35      deraadt   720:        int     fd;
1.51      millert   721:        int     numdays = ent->numlogs;
                    722:
                    723:        /* Is there a separate backup dir? */
                    724:        if (ent->backdir != NULL)
                    725:                snprintf(oldlog, sizeof(oldlog), "%s/%s", ent->backdir,
                    726:                    ent->logbase);
                    727:        else
                    728:                strlcpy(oldlog, ent->log, sizeof(oldlog));
1.1       deraadt   729:
1.35      deraadt   730:        /* Remove oldest log (may not exist) */
1.51      millert   731:        (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   732:        (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog, numdays,
1.35      deraadt   733:            COMPRESS_POSTFIX);
                    734:
                    735:        if (noaction) {
1.57      millert   736:                printf("\trm -f %s %s\n", file1, file2);
1.35      deraadt   737:        } else {
                    738:                (void)unlink(file1);
1.57      millert   739:                (void)unlink(file2);
1.35      deraadt   740:        }
                    741:
                    742:        /* Move down log files */
                    743:        while (numdays--) {
1.57      millert   744:                /*
                    745:                 * If both the compressed archive or the non-compressed archive
                    746:                 * exist, we one or the other based on the CE_COMPACT flag.
                    747:                 */
1.51      millert   748:                (void)snprintf(file1, sizeof(file1), "%s.%d", oldlog, numdays);
1.57      millert   749:                suffix = lstat_log(file1, sizeof(file1), ent->flags);
                    750:                if (suffix == NULL)
                    751:                        continue;
                    752:                (void)snprintf(file2, sizeof(file2), "%s.%d%s", oldlog,
                    753:                    numdays + 1, suffix);
                    754:
1.35      deraadt   755:                if (noaction) {
1.57      millert   756:                        printf("\tmv %s %s\n", file1, file2);
                    757:                        printf("\tchmod %o %s\n", ent->permissions, file2);
1.51      millert   758:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.50      millert   759:                                printf("\tchown %u:%u %s\n",
1.57      millert   760:                                    ent->uid, ent->gid, file2);
1.35      deraadt   761:                } else {
1.57      millert   762:                        if (rename(file1, file2))
                    763:                                warn("can't mv %s to %s", file1, file2);
                    764:                        if (chmod(file2, ent->permissions))
                    765:                                warn("can't chmod %s", file2);
1.51      millert   766:                        if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
1.57      millert   767:                                if (chown(file2, ent->uid, ent->gid))
                    768:                                        warn("can't chown %s", file2);
1.35      deraadt   769:                }
                    770:        }
1.51      millert   771:        if (!noaction && !(ent->flags & CE_BINARY))
                    772:                (void)log_trim(ent->log);  /* Report the trimming to the old log */
1.1       deraadt   773:
1.51      millert   774:        (void)snprintf(file2, sizeof(file2), "%s.XXXXXXXXXX", ent->log);
1.35      deraadt   775:        if (noaction)  {
1.50      millert   776:                printf("\tmktemp %s\n", file2);
1.35      deraadt   777:        } else {
                    778:                if ((fd = mkstemp(file2)) < 0)
1.26      millert   779:                        err(1, "can't start '%s' log", file2);
1.51      millert   780:                if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1)
                    781:                        if (fchown(fd, ent->uid, ent->gid))
                    782:                            err(1, "can't chown '%s' log file", file2);
                    783:                if (fchmod(fd, ent->permissions))
1.26      millert   784:                        err(1, "can't chmod '%s' log file", file2);
1.35      deraadt   785:                (void)close(fd);
1.26      millert   786:                /* Add status message */
1.51      millert   787:                if (!(ent->flags & CE_BINARY) && log_trim(file2))
1.26      millert   788:                        err(1, "can't add status message to log '%s'", file2);
1.35      deraadt   789:        }
1.26      millert   790:
1.51      millert   791:        if (ent->numlogs == 0) {
1.5       deraadt   792:                if (noaction)
1.51      millert   793:                        printf("\trm %s\n", ent->log);
                    794:                else if (unlink(ent->log))
                    795:                        warn("can't rm %s", ent->log);
1.5       deraadt   796:        } else {
1.57      millert   797:                (void)snprintf(file1, sizeof(file1), "%s.0", oldlog);
1.51      millert   798:                if (noaction)
                    799:                        printf("\tmv %s to %s\n", ent->log, file1);
                    800:                else if (rename(ent->log, file1))
1.59      millert   801:                        warn("can't mv %s to %s", ent->log, file1);
1.5       deraadt   802:        }
                    803:
1.26      millert   804:        /* Now move the new log file into place */
                    805:        if (noaction)
1.51      millert   806:                printf("\tmv %s to %s\n", file2, ent->log);
                    807:        else if (rename(file2, ent->log))
1.59      millert   808:                warn("can't mv %s to %s", file2, ent->log);
1.1       deraadt   809: }
                    810:
                    811: /* Log the fact that the logs were turned over */
1.25      millert   812: int
1.48      millert   813: log_trim(char *log)
1.1       deraadt   814: {
1.35      deraadt   815:        FILE    *f;
1.25      millert   816:
1.35      deraadt   817:        if ((f = fopen(log, "a")) == NULL)
1.48      millert   818:                return (-1);
1.44      mpech     819:        (void)fprintf(f, "%s %s newsyslog[%ld]: logfile turned over\n",
                    820:            daytime, hostname, (long)getpid());
1.35      deraadt   821:        if (fclose(f) == EOF)
                    822:                err(1, "log_trim: fclose");
1.48      millert   823:        return (0);
1.1       deraadt   824: }
                    825:
1.16      millert   826: /* Fork off compress or gzip to compress the old log file */
1.25      millert   827: void
1.51      millert   828: compress_log(struct conf_entry *ent)
1.1       deraadt   829: {
1.48      millert   830:        pid_t pid;
                    831:        char *base, tmp[MAXPATHLEN];
1.51      millert   832:
                    833:        if (ent->backdir != NULL)
                    834:                snprintf(tmp, sizeof(tmp), "%s/%s.0", ent->backdir,
                    835:                    ent->logbase);
                    836:        else
                    837:                snprintf(tmp, sizeof(tmp), "%s.0", ent->log);
                    838:
1.27      millert   839:        if ((base = strrchr(COMPRESS, '/')) == NULL)
                    840:                base = COMPRESS;
                    841:        else
                    842:                base++;
                    843:        if (noaction) {
1.51      millert   844:                printf("%s %s\n", base, tmp);
1.27      millert   845:                return;
                    846:        }
1.35      deraadt   847:        pid = fork();
                    848:        if (pid < 0) {
1.11      downsj    849:                err(1, "fork");
1.50      millert   850:        } else if (pid == 0) {
1.37      deraadt   851:                (void)execl(COMPRESS, base, "-f", tmp, (char *)NULL);
1.16      millert   852:                warn(COMPRESS);
                    853:                _exit(1);
1.35      deraadt   854:        }
1.1       deraadt   855: }
                    856:
                    857: /* Return size in kilobytes of a file */
1.58      millert   858: off_t
1.48      millert   859: sizefile(char *file)
1.1       deraadt   860: {
1.35      deraadt   861:        struct stat sb;
1.1       deraadt   862:
1.35      deraadt   863:        if (stat(file, &sb) < 0)
1.48      millert   864:                return (-1);
1.58      millert   865:
                    866:        /* For sparse files, return the size based on number of blocks used. */
                    867:        if (sb.st_size / DEV_BSIZE > sb.st_blocks)
                    868:                return (sb.st_blocks * DEV_BSIZE);
                    869:        else
                    870:                return (sb.st_size);
1.1       deraadt   871: }
                    872:
1.25      millert   873: /* Return the age (in hours) of old log file (file.0), or -1 if none */
                    874: int
1.51      millert   875: age_old_log(struct conf_entry *ent)
1.1       deraadt   876: {
1.35      deraadt   877:        struct stat sb;
1.57      millert   878:        char file[MAXPATHLEN];
1.1       deraadt   879:
1.51      millert   880:        if (ent->backdir != NULL)
1.57      millert   881:                (void)snprintf(file, sizeof(file), "%s/%s.0", ent->backdir,
                    882:                    ent->logbase);
                    883:        else
                    884:                (void)snprintf(file, sizeof(file), "%s.0", ent->log);
                    885:        if (ent->flags & CE_COMPACT) {
                    886:                if (stat_suffix(file, sizeof(file), COMPRESS_POSTFIX, &sb,
                    887:                    stat) < 0 && stat(file, &sb) < 0)
                    888:                        return (-1);
                    889:        } else {
                    890:                if (stat(file, &sb) < 0 && stat_suffix(file, sizeof(file),
                    891:                    COMPRESS_POSTFIX, &sb, stat) < 0)
                    892:                        return (-1);
                    893:        }
1.48      millert   894:        return ((int)(timenow - sb.st_mtime + 1800) / 3600);
1.1       deraadt   895: }
                    896:
                    897: /* Skip Over Blanks */
1.25      millert   898: char *
1.48      millert   899: sob(char *p)
1.1       deraadt   900: {
1.35      deraadt   901:        while (p && *p && isspace(*p))
                    902:                p++;
1.48      millert   903:        return (p);
1.1       deraadt   904: }
                    905:
                    906: /* Skip Over Non-Blanks */
1.25      millert   907: char *
1.48      millert   908: son(char *p)
1.1       deraadt   909: {
1.35      deraadt   910:        while (p && *p && !isspace(*p))
                    911:                p++;
1.48      millert   912:        return (p);
1.1       deraadt   913: }
                    914:
                    915: /* Check if string is actually a number */
1.25      millert   916: int
1.48      millert   917: isnumberstr(char *string)
1.1       deraadt   918: {
1.35      deraadt   919:        while (*string) {
                    920:                if (!isdigit(*string++))
1.48      millert   921:                        return (0);
1.35      deraadt   922:        }
1.48      millert   923:        return (1);
1.10      downsj    924: }
                    925:
1.61      millert   926: int
                    927: domonitor(struct conf_entry *ent)
1.10      downsj    928: {
                    929:        struct stat sb, tsb;
1.35      deraadt   930:        char fname[MAXPATHLEN], *flog, *p, *rb = NULL;
1.10      downsj    931:        FILE *fp;
                    932:        off_t osize;
                    933:        int rd;
                    934:
1.61      millert   935:        if (stat(ent->log, &sb) < 0)
                    936:                return (0);
                    937:
                    938:        if (noaction) {
                    939:                if (!verbose)
                    940:                        printf("%s: monitored\n", ent->log);
                    941:                return (1);
                    942:        }
1.10      downsj    943:
1.61      millert   944:        flog = strdup(ent->log);
1.11      downsj    945:        if (flog == NULL)
                    946:                err(1, "strdup");
                    947:
1.10      downsj    948:        for (p = flog; *p != '\0'; p++) {
                    949:                if (*p == '/')
                    950:                        *p = '_';
                    951:        }
1.48      millert   952:        snprintf(fname, sizeof(fname), "%s/newsyslog.%s.size",
1.35      deraadt   953:            STATS_DIR, flog);
1.10      downsj    954:
                    955:        /* ..if it doesn't exist, simply record the current size. */
                    956:        if ((sb.st_size == 0) || stat(fname, &tsb) < 0)
                    957:                goto update;
                    958:
                    959:        fp = fopen(fname, "r");
                    960:        if (fp == NULL) {
1.34      millert   961:                warn("%s", fname);
1.10      downsj    962:                goto cleanup;
                    963:        }
                    964: #ifdef QUAD_OFF_T
                    965:        if (fscanf(fp, "%qd\n", &osize) != 1) {
                    966: #else
                    967:        if (fscanf(fp, "%ld\n", &osize) != 1) {
                    968: #endif /* QUAD_OFF_T */
                    969:                fclose(fp);
                    970:                goto update;
                    971:        }
                    972:
                    973:        fclose(fp);
                    974:
                    975:        /* If the file is smaller, mark the entire thing as changed. */
                    976:        if (sb.st_size < osize)
                    977:                osize = 0;
                    978:
                    979:        /* Now see if current size is larger. */
                    980:        if (sb.st_size > osize) {
                    981:                rb = (char *) malloc(sb.st_size - osize);
1.11      downsj    982:                if (rb == NULL)
                    983:                        err(1, "malloc");
1.10      downsj    984:
                    985:                /* Open logfile, seek. */
1.61      millert   986:                fp = fopen(ent->log, "r");
1.10      downsj    987:                if (fp == NULL) {
1.61      millert   988:                        warn("%s", ent->log);
1.10      downsj    989:                        goto cleanup;
                    990:                }
                    991:                fseek(fp, osize, SEEK_SET);
                    992:                rd = fread(rb, 1, sb.st_size - osize, fp);
                    993:                if (rd < 1) {
1.11      downsj    994:                        warn("fread");
1.10      downsj    995:                        fclose(fp);
                    996:                        goto cleanup;
                    997:                }
                    998:
                    999:                /* Send message. */
                   1000:                fclose(fp);
                   1001:
                   1002:                fp = openmail();
                   1003:                if (fp == NULL) {
1.11      downsj   1004:                        warn("openmail");
1.10      downsj   1005:                        goto cleanup;
                   1006:                }
                   1007:                fprintf(fp, "To: %s\nSubject: LOGFILE NOTIFICATION: %s\n\n\n",
1.61      millert  1008:                    ent->whom, ent->log);
1.10      downsj   1009:                fwrite(rb, 1, rd, fp);
                   1010:                fputs("\n\n", fp);
                   1011:
1.48      millert  1012:                pclose(fp);
1.10      downsj   1013:        }
                   1014: update:
                   1015:        /* Reopen for writing and update file. */
                   1016:        fp = fopen(fname, "w");
                   1017:        if (fp == NULL) {
1.34      millert  1018:                warn("%s", fname);
1.10      downsj   1019:                goto cleanup;
                   1020:        }
                   1021: #ifdef QUAD_OFF_T
                   1022:        fprintf(fp, "%qd\n", sb.st_size);
                   1023: #else
                   1024:        fprintf(fp, "%ld\n", sb.st_size);
                   1025: #endif /* QUAD_OFF_T */
                   1026:        fclose(fp);
                   1027:
                   1028: cleanup:
                   1029:        free(flog);
                   1030:        if (rb != NULL)
                   1031:                free(rb);
1.61      millert  1032:        return (1);
1.10      downsj   1033: }
                   1034:
1.25      millert  1035: FILE *
1.48      millert  1036: openmail(void)
1.10      downsj   1037: {
1.48      millert  1038:        FILE *ret;
1.35      deraadt  1039:        char *cmdbuf = NULL;
1.10      downsj   1040:
1.35      deraadt  1041:        asprintf(&cmdbuf, "%s -t", SENDMAIL);
                   1042:        if (cmdbuf) {
                   1043:                ret = popen(cmdbuf, "w");
                   1044:                free(cmdbuf);
                   1045:                return (ret);
                   1046:        }
                   1047:        return (NULL);
1.10      downsj   1048: }
                   1049:
1.25      millert  1050: void
1.48      millert  1051: child_killer(int signo)
1.16      millert  1052: {
1.38      deraadt  1053:        int save_errno = errno;
1.16      millert  1054:        int status;
                   1055:
                   1056:        while (waitpid(-1, &status, WNOHANG) > 0)
                   1057:                ;
1.38      deraadt  1058:        errno = save_errno;
1.57      millert  1059: }
                   1060:
                   1061: int
                   1062: stat_suffix(char *file, size_t size, char *suffix, struct stat *sp, int (*func)())
                   1063: {
                   1064:        size_t n;
                   1065:
                   1066:        n = strlcat(file, suffix, size);
                   1067:        if (n < size && func(file, sp) == 0)
                   1068:                return (0);
                   1069:        file[n - strlen(suffix)] = '\0';
                   1070:        return (-1);
                   1071: }
                   1072:
                   1073: /*
                   1074:  * lstat() a log, possibily appending a suffix; order is based on flags.
                   1075:  * Returns the suffix appended (may be empty string) or NULL if no file.
                   1076:  */
                   1077: char *
                   1078: lstat_log(char *file, size_t size, int flags)
                   1079: {
                   1080:        struct stat sb;
                   1081:
                   1082:        if (flags & CE_COMPACT) {
                   1083:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1084:                        return (COMPRESS_POSTFIX);
                   1085:                if (lstat(file, &sb) == 0)
                   1086:                        return ("");
                   1087:        } else {
                   1088:                if (lstat(file, &sb) == 0)
                   1089:                        return ("");
                   1090:                if (stat_suffix(file, size, COMPRESS_POSTFIX, &sb, lstat) == 0)
                   1091:                        return (COMPRESS_POSTFIX);
                   1092:
                   1093:        }
                   1094:        return (NULL);
1.1       deraadt  1095: }