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