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