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