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