newsyslog.c revision 119926
113244Sgraichen/* 213244Sgraichen * This file contains changes from the Open Software Foundation. 313244Sgraichen */ 413244Sgraichen 513244Sgraichen/* 659004Shm * Copyright 1988, 1989 by the Massachusetts Institute of Technology 759004Shm * 859004Shm * Permission to use, copy, modify, and distribute this software and its 959004Shm * documentation for any purpose and without fee is hereby granted, provided 1059004Shm * that the above copyright notice appear in all copies and that both that 1159004Shm * copyright notice and this permission notice appear in supporting 1259004Shm * documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be 1359004Shm * used in advertising or publicity pertaining to distribution of the 1459004Shm * software without specific, written prior permission. M.I.T. and the M.I.T. 1559004Shm * S.I.P.B. make no representations about the suitability of this software 1659004Shm * for any purpose. It is provided "as is" without express or implied 1759004Shm * warranty. 1859004Shm * 1959004Shm */ 2013244Sgraichen 2113244Sgraichen/* 2259004Shm * newsyslog - roll over selected logs at the appropriate time, keeping the a 2359004Shm * specified number of backup files around. 2413244Sgraichen */ 2513244Sgraichen 26114601Sobrien#include <sys/cdefs.h> 27114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/newsyslog/newsyslog.c 119926 2003-09-09 20:29:26Z gad $"); 2813244Sgraichen 2943071Swollman#define OSF 3013244Sgraichen#ifndef COMPRESS_POSTFIX 3143071Swollman#define COMPRESS_POSTFIX ".gz" 3213244Sgraichen#endif 3380638Sobrien#ifndef BZCOMPRESS_POSTFIX 3480638Sobrien#define BZCOMPRESS_POSTFIX ".bz2" 3580638Sobrien#endif 3613244Sgraichen 3796001Smaxim#include <sys/param.h> 3896001Smaxim#include <sys/stat.h> 3996001Smaxim#include <sys/wait.h> 4096001Smaxim 4130160Scharnier#include <ctype.h> 4230160Scharnier#include <err.h> 4395999Smaxim#include <errno.h> 4430160Scharnier#include <fcntl.h> 45111773Sgad#include <fnmatch.h> 46106905Ssobomax#include <glob.h> 4730160Scharnier#include <grp.h> 4843071Swollman#include <paths.h> 4930160Scharnier#include <pwd.h> 5030160Scharnier#include <signal.h> 5113244Sgraichen#include <stdio.h> 5213244Sgraichen#include <stdlib.h> 5313244Sgraichen#include <string.h> 5443071Swollman#include <time.h> 5516240Salex#include <unistd.h> 5613244Sgraichen 5743071Swollman#include "pathnames.h" 5843071Swollman 59111768Sgad/* 60111768Sgad * Bit-values for the 'flags' parsed from a config-file entry. 61111768Sgad */ 62111768Sgad#define CE_COMPACT 0x0001 /* Compact the achived log files with gzip. */ 63111768Sgad#define CE_BZCOMPACT 0x0002 /* Compact the achived log files with bzip2. */ 64111781Sgad#define CE_COMPACTWAIT 0x0004 /* wait until compressing one file finishes */ 65111768Sgad /* before starting the next step. */ 66111768Sgad#define CE_BINARY 0x0008 /* Logfile is in binary, do not add status */ 67111768Sgad /* messages to logfile(s) when rotating. */ 68111781Sgad#define CE_NOSIGNAL 0x0010 /* There is no process to signal when */ 69111768Sgad /* trimming this file. */ 70111781Sgad#define CE_TRIMAT 0x0020 /* trim file at a specific time. */ 71111781Sgad#define CE_GLOB 0x0040 /* name of the log is file name pattern. */ 72112003Sgad#define CE_SIGNALGROUP 0x0080 /* Signal a process-group instead of a single */ 73112003Sgad /* process when trimming this file. */ 74114137Sgad#define CE_CREATE 0x0100 /* Create the log file if it does not exist. */ 7543071Swollman 76111781Sgad#define MIN_PID 5 /* Don't touch pids lower than this */ 77111781Sgad#define MAX_PID 99999 /* was lower, see /usr/include/sys/proc.h */ 78111781Sgad 79111781Sgad#define kbytes(size) (((size) + 1023) >> 10) 80111781Sgad 8113244Sgraichenstruct conf_entry { 8259003Shm char *log; /* Name of the log */ 8359003Shm char *pid_file; /* PID file */ 84111772Sgad char *r_reason; /* The reason this file is being rotated */ 85114137Sgad int firstcreate; /* Creating log for the first time (-C). */ 86111772Sgad int rotate; /* Non-zero if this file should be rotated */ 87111779Sgad uid_t uid; /* Owner of log */ 88111779Sgad gid_t gid; /* Group of log */ 8959003Shm int numlogs; /* Number of logs to keep */ 9059003Shm int size; /* Size cutoff to trigger trimming the log */ 9159003Shm int hours; /* Hours between log trimming */ 9259003Shm time_t trim_at; /* Specific time to do trimming */ 9359003Shm int permissions; /* File permissions on the log */ 9480646Sobrien int flags; /* CE_COMPACT, CE_BZCOMPACT, CE_BINARY */ 9559003Shm int sig; /* Signal to send */ 96111388Sgad int def_cfg; /* Using the <default> rule for this file */ 9759003Shm struct conf_entry *next;/* Linked list pointer */ 9813244Sgraichen}; 9913244Sgraichen 100111388Sgad#define DEFAULT_MARKER "<default>" 101111388Sgad 10259004Shmint archtodir = 0; /* Archive old logfiles to other directory */ 103114137Sgadint createlogs; /* Create (non-GLOB) logfiles which do not */ 104114137Sgad /* already exist. 1=='for entries with */ 105114137Sgad /* C flag', 2=='for all entries'. */ 10659003Shmint verbose = 0; /* Print out what's going on */ 10759003Shmint needroot = 1; /* Root privs are necessary */ 10859003Shmint noaction = 0; /* Don't do anything, just show it */ 109111768Sgadint nosignal; /* Do not send any signals */ 11059003Shmint force = 0; /* Force the trim no matter what */ 111111772Sgadint rotatereq = 0; /* -R = Always rotate the file(s) as given */ 112111772Sgad /* on the command (this also requires */ 113111772Sgad /* that a list of files *are* given on */ 114111772Sgad /* the run command). */ 115111772Sgadchar *requestor; /* The name given on a -R request */ 11659004Shmchar *archdirname; /* Directory path to old logfiles archive */ 117111773Sgadconst char *conf; /* Configuration file to use */ 118119904Sgadtime_t dbg_timenow; /* A "timenow" value set via -D option */ 11959003Shmtime_t timenow; 12059003Shm 12171299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 122108164Strhodeschar daytime[16]; /* timenow in human readable form */ 12313244Sgraichen 124111773Sgadstatic struct conf_entry *get_worklist(char **files); 125111773Sgadstatic void parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 126112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p); 12716240Salexstatic char *sob(char *p); 12816240Salexstatic char *son(char *p); 129119102Sgadstatic int isnumberstr(const char *); 13059003Shmstatic char *missing_field(char *p, char *errline); 13159003Shmstatic void do_entry(struct conf_entry * ent); 132112020Sgadstatic void expand_globs(struct conf_entry **work_p, 133112020Sgad struct conf_entry **glob_p); 134112020Sgadstatic void free_clist(struct conf_entry **firstent); 135111388Sgadstatic void free_entry(struct conf_entry *ent); 136111388Sgadstatic struct conf_entry *init_entry(const char *fname, 137111388Sgad struct conf_entry *src_entry); 138111781Sgadstatic void parse_args(int argc, char **argv); 139119904Sgadstatic int parse_doption(const char *doption); 14080640Sobrienstatic void usage(void); 141111779Sgadstatic void dotrim(const struct conf_entry *ent, char *log, 142111780Sgad int numdays, int flags); 143119926Sgadstatic int log_trim(const char *logname, const struct conf_entry *log_ent); 144119926Sgadstatic void compress_log(char *logname, int dowait); 145119926Sgadstatic void bzcompress_log(char *logname, int dowait); 14616240Salexstatic int sizefile(char *file); 14716240Salexstatic int age_old_log(char *file); 148112003Sgadstatic int send_signal(const struct conf_entry *ent); 149111779Sgadstatic void movefile(char *from, char *to, int perm, uid_t owner_uid, 150111779Sgad gid_t group_gid); 151114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 152114137Sgadstatic void createlog(const struct conf_entry *ent); 153119904Sgadstatic time_t parse8601(const char *s); 154119899Sgadstatic time_t parseDWM(char *s); 15513244Sgraichen 156111768Sgad/* 157111768Sgad * All the following are defined to work on an 'int', in the 158111768Sgad * range 0 to 255, plus EOF. Define wrappers which can take 159111768Sgad * values of type 'char', either signed or unsigned. 160111768Sgad */ 161114762Sgad#define isdigitch(Anychar) isdigit(((int) Anychar) & 255) 162111772Sgad#define isprintch(Anychar) isprint(((int) Anychar) & 255) 163111768Sgad#define isspacech(Anychar) isspace(((int) Anychar) & 255) 164111768Sgad#define tolowerch(Anychar) tolower(((int) Anychar) & 255) 165111768Sgad 16659004Shmint 16759004Shmmain(int argc, char **argv) 16813244Sgraichen{ 16959003Shm struct conf_entry *p, *q; 17025443Sache 171111781Sgad parse_args(argc, argv); 172111773Sgad argc -= optind; 173111773Sgad argv += optind; 174111773Sgad 17559003Shm if (needroot && getuid() && geteuid()) 17659003Shm errx(1, "must have root privs"); 177111773Sgad p = q = get_worklist(argv); 17859003Shm 17959003Shm while (p) { 180112020Sgad do_entry(p); 18159003Shm p = p->next; 182111388Sgad free_entry(q); 18359003Shm q = p; 18459003Shm } 18595999Smaxim while (wait(NULL) > 0 || errno == EINTR) 18695999Smaxim ; 18759003Shm return (0); 18813244Sgraichen} 18913244Sgraichen 190111388Sgadstatic struct conf_entry * 191111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 192111388Sgad{ 193111388Sgad struct conf_entry *tempwork; 194111388Sgad 195111388Sgad if (verbose > 4) 196111388Sgad printf("\t--> [creating entry for %s]\n", fname); 197111388Sgad 198111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 199111388Sgad if (tempwork == NULL) 200111388Sgad err(1, "malloc of conf_entry for %s", fname); 201111388Sgad 202111388Sgad tempwork->log = strdup(fname); 203111388Sgad if (tempwork->log == NULL) 204111388Sgad err(1, "strdup for %s", fname); 205111388Sgad 206111388Sgad if (src_entry != NULL) { 207111388Sgad tempwork->pid_file = NULL; 208111388Sgad if (src_entry->pid_file) 209111388Sgad tempwork->pid_file = strdup(src_entry->pid_file); 210111772Sgad tempwork->r_reason = NULL; 211114137Sgad tempwork->firstcreate = 0; 212111772Sgad tempwork->rotate = 0; 213111388Sgad tempwork->uid = src_entry->uid; 214111388Sgad tempwork->gid = src_entry->gid; 215111388Sgad tempwork->numlogs = src_entry->numlogs; 216111388Sgad tempwork->size = src_entry->size; 217111388Sgad tempwork->hours = src_entry->hours; 218111388Sgad tempwork->trim_at = src_entry->trim_at; 219111388Sgad tempwork->permissions = src_entry->permissions; 220111388Sgad tempwork->flags = src_entry->flags; 221111388Sgad tempwork->sig = src_entry->sig; 222111388Sgad tempwork->def_cfg = src_entry->def_cfg; 223111388Sgad } else { 224111388Sgad /* Initialize as a "do-nothing" entry */ 225111388Sgad tempwork->pid_file = NULL; 226111772Sgad tempwork->r_reason = NULL; 227114137Sgad tempwork->firstcreate = 0; 228111772Sgad tempwork->rotate = 0; 229111779Sgad tempwork->uid = (uid_t)-1; 230111779Sgad tempwork->gid = (gid_t)-1; 231111388Sgad tempwork->numlogs = 1; 232111388Sgad tempwork->size = -1; 233111388Sgad tempwork->hours = -1; 234111388Sgad tempwork->trim_at = (time_t)0; 235111388Sgad tempwork->permissions = 0; 236111388Sgad tempwork->flags = 0; 237111388Sgad tempwork->sig = SIGHUP; 238111388Sgad tempwork->def_cfg = 0; 239111388Sgad } 240111388Sgad tempwork->next = NULL; 241111388Sgad 242111388Sgad return (tempwork); 243111388Sgad} 244111388Sgad 24559004Shmstatic void 246111388Sgadfree_entry(struct conf_entry *ent) 247111388Sgad{ 248111388Sgad 249111388Sgad if (ent == NULL) 250111388Sgad return; 251111388Sgad 252111388Sgad if (ent->log != NULL) { 253111388Sgad if (verbose > 4) 254111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 255111388Sgad free(ent->log); 256111388Sgad ent->log = NULL; 257111388Sgad } 258111388Sgad 259111388Sgad if (ent->pid_file != NULL) { 260111388Sgad free(ent->pid_file); 261111388Sgad ent->pid_file = NULL; 262111388Sgad } 263111388Sgad 264111772Sgad if (ent->r_reason != NULL) { 265111772Sgad free(ent->r_reason); 266111772Sgad ent->r_reason = NULL; 267111772Sgad } 268111772Sgad 269111388Sgad free(ent); 270111388Sgad} 271111388Sgad 272111388Sgadstatic void 273112020Sgadfree_clist(struct conf_entry **firstent) 274112020Sgad{ 275112020Sgad struct conf_entry *ent, *nextent; 276112020Sgad 277112020Sgad if (firstent == NULL) 278112020Sgad return; /* There is nothing to do. */ 279112020Sgad 280112020Sgad ent = *firstent; 281112020Sgad firstent = NULL; 282112020Sgad 283112020Sgad while (ent) { 284112020Sgad nextent = ent->next; 285112020Sgad free_entry(ent); 286112020Sgad ent = nextent; 287112020Sgad } 288112020Sgad} 289112020Sgad 290112020Sgadstatic void 29159004Shmdo_entry(struct conf_entry * ent) 29213244Sgraichen{ 293111772Sgad#define REASON_MAX 80 29459003Shm int size, modtime; 295111772Sgad char temp_reason[REASON_MAX]; 29659003Shm 29759003Shm if (verbose) { 29859003Shm if (ent->flags & CE_COMPACT) 29959003Shm printf("%s <%dZ>: ", ent->log, ent->numlogs); 30080638Sobrien else if (ent->flags & CE_BZCOMPACT) 30180638Sobrien printf("%s <%dJ>: ", ent->log, ent->numlogs); 30259003Shm else 30359003Shm printf("%s <%d>: ", ent->log, ent->numlogs); 30459003Shm } 30559003Shm size = sizefile(ent->log); 30659003Shm modtime = age_old_log(ent->log); 307111772Sgad ent->rotate = 0; 308114137Sgad ent->firstcreate = 0; 30959003Shm if (size < 0) { 310114137Sgad /* 311114137Sgad * If either the C flag or the -C option was specified, 312114137Sgad * and if we won't be creating the file, then have the 313114137Sgad * verbose message include a hint as to why the file 314114137Sgad * will not be created. 315114137Sgad */ 316114137Sgad temp_reason[0] = '\0'; 317114137Sgad if (createlogs > 1) 318114137Sgad ent->firstcreate = 1; 319114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 320114137Sgad ent->firstcreate = 1; 321114137Sgad else if (ent->flags & CE_CREATE) 322114137Sgad strncpy(temp_reason, " (no -C option)", REASON_MAX); 323114137Sgad else if (createlogs) 324114137Sgad strncpy(temp_reason, " (no C flag)", REASON_MAX); 325114137Sgad 326114137Sgad if (ent->firstcreate) { 327114137Sgad if (verbose) 328114137Sgad printf("does not exist -> will create.\n"); 329114137Sgad createlog(ent); 330114137Sgad } else if (verbose) { 331114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 332114137Sgad } 33359003Shm } else { 334111772Sgad if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 33543071Swollman if (timenow < ent->trim_at 33659003Shm || difftime(timenow, ent->trim_at) >= 60 * 60) { 33743071Swollman if (verbose) 33843071Swollman printf("--> will trim at %s", 33959003Shm ctime(&ent->trim_at)); 34043071Swollman return; 34143071Swollman } else if (verbose && ent->hours <= 0) { 34243071Swollman printf("--> time is up\n"); 34343071Swollman } 34443071Swollman } 34559003Shm if (verbose && (ent->size > 0)) 34659003Shm printf("size (Kb): %d [%d] ", size, ent->size); 34759003Shm if (verbose && (ent->hours > 0)) 34859003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 349111772Sgad 350111772Sgad /* 351111772Sgad * Figure out if this logfile needs to be rotated. 352111772Sgad */ 353111772Sgad temp_reason[0] = '\0'; 354111772Sgad if (rotatereq) { 355111772Sgad ent->rotate = 1; 356111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 357111772Sgad requestor); 358111772Sgad } else if (force) { 359111772Sgad ent->rotate = 1; 360111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 361111772Sgad } else if ((ent->size > 0) && (size >= ent->size)) { 362111772Sgad ent->rotate = 1; 363111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 364111772Sgad ent->size); 365111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 366111772Sgad ent->rotate = 1; 367111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 368111772Sgad (modtime < 0))) { 369111772Sgad ent->rotate = 1; 370111772Sgad } 371111772Sgad 372111772Sgad /* 373111772Sgad * If the file needs to be rotated, then rotate it. 374111772Sgad */ 375111772Sgad if (ent->rotate) { 376111772Sgad if (temp_reason[0] != '\0') 377111772Sgad ent->r_reason = strdup(temp_reason); 37859003Shm if (verbose) 37959003Shm printf("--> trimming log....\n"); 38059003Shm if (noaction && !verbose) { 38159003Shm if (ent->flags & CE_COMPACT) 38259003Shm printf("%s <%dZ>: trimming\n", 38359003Shm ent->log, ent->numlogs); 38480638Sobrien else if (ent->flags & CE_BZCOMPACT) 38580638Sobrien printf("%s <%dJ>: trimming\n", 38680638Sobrien ent->log, ent->numlogs); 38759003Shm else 38859003Shm printf("%s <%d>: trimming\n", 38959003Shm ent->log, ent->numlogs); 39059003Shm } 391111780Sgad dotrim(ent, ent->log, ent->numlogs, ent->flags); 39259003Shm } else { 39359003Shm if (verbose) 39459003Shm printf("--> skipping\n"); 39559003Shm } 39659003Shm } 397111772Sgad#undef REASON_MAX 39813244Sgraichen} 39913244Sgraichen 400112003Sgad/* Send a signal to the pid specified by pidfile */ 401112003Sgadstatic int 402112003Sgadsend_signal(const struct conf_entry *ent) 403112003Sgad{ 404112003Sgad pid_t target_pid; 405112003Sgad int did_notify; 406112003Sgad FILE *f; 407112003Sgad long minok, maxok, rval; 408112003Sgad const char *target_name; 409112003Sgad char *endp, *linep, line[BUFSIZ]; 410112003Sgad 411112003Sgad did_notify = 0; 412112003Sgad f = fopen(ent->pid_file, "r"); 413112003Sgad if (f == NULL) { 414112003Sgad warn("can't open pid file: %s", ent->pid_file); 415112003Sgad return (did_notify); 416112003Sgad /* NOTREACHED */ 417112003Sgad } 418112003Sgad 419112003Sgad if (fgets(line, BUFSIZ, f) == NULL) { 420112003Sgad /* 421112003Sgad * XXX - If the pid file is empty, is that really a 422112003Sgad * problem? Wouldn't that mean that the process 423112003Sgad * has shut down? In that case there would be no 424112003Sgad * problem with compressing the rotated log file. 425112003Sgad */ 426112003Sgad if (feof(f)) 427112003Sgad warnx("pid file is empty: %s", ent->pid_file); 428112003Sgad else 429112003Sgad warn("can't read from pid file: %s", ent->pid_file); 430112003Sgad (void) fclose(f); 431112003Sgad return (did_notify); 432112003Sgad /* NOTREACHED */ 433112003Sgad } 434112003Sgad (void) fclose(f); 435112003Sgad 436112003Sgad target_name = "daemon"; 437112003Sgad minok = MIN_PID; 438112003Sgad maxok = MAX_PID; 439112003Sgad if (ent->flags & CE_SIGNALGROUP) { 440112003Sgad /* 441112003Sgad * If we are expected to signal a process-group when 442112003Sgad * rotating this logfile, then the value read in should 443112003Sgad * be the negative of a valid process ID. 444112003Sgad */ 445112003Sgad target_name = "process-group"; 446112003Sgad minok = -MAX_PID; 447112003Sgad maxok = -MIN_PID; 448112003Sgad } 449112003Sgad 450112003Sgad errno = 0; 451112003Sgad linep = line; 452112003Sgad while (*linep == ' ') 453112003Sgad linep++; 454112003Sgad rval = strtol(linep, &endp, 10); 455112003Sgad if (*endp != '\0' && !isspacech(*endp)) { 456112003Sgad warnx("pid file does not start with a valid number: %s", 457112003Sgad ent->pid_file); 458112003Sgad rval = 0; 459112003Sgad } else if (rval < minok || rval > maxok) { 460112003Sgad warnx("bad value '%ld' for process number in %s", 461112003Sgad rval, ent->pid_file); 462112003Sgad if (verbose) 463112003Sgad warnx("\t(expecting value between %ld and %ld)", 464112003Sgad minok, maxok); 465112003Sgad rval = 0; 466112003Sgad } 467112003Sgad if (rval == 0) { 468112003Sgad return (did_notify); 469112003Sgad /* NOTREACHED */ 470112003Sgad } 471112003Sgad 472112003Sgad target_pid = rval; 473112003Sgad 474112003Sgad if (noaction) { 475112003Sgad did_notify = 1; 476112003Sgad printf("\tkill -%d %d\n", ent->sig, (int) target_pid); 477112003Sgad } else if (kill(target_pid, ent->sig)) { 478112003Sgad /* 479112003Sgad * XXX - Iff the error was "no such process", should that 480112003Sgad * really be an error for us? Perhaps the process 481112003Sgad * is already gone, in which case there would be no 482112003Sgad * problem with compressing the rotated log file. 483112003Sgad */ 484112003Sgad warn("can't notify %s, pid %d", target_name, 485112003Sgad (int) target_pid); 486112003Sgad } else { 487112003Sgad did_notify = 1; 488112003Sgad if (verbose) 489112003Sgad printf("%s pid %d notified\n", target_name, 490112003Sgad (int) target_pid); 491112003Sgad } 492112003Sgad 493112003Sgad return (did_notify); 494112003Sgad} 495112003Sgad 49659004Shmstatic void 497111781Sgadparse_args(int argc, char **argv) 49813244Sgraichen{ 499111781Sgad int ch; 50059003Shm char *p; 501119904Sgad char debugtime[32]; 50213244Sgraichen 503111781Sgad timenow = time(NULL); 504108164Strhodes (void)strncpy(daytime, ctime(&timenow) + 4, 15); 50559003Shm daytime[15] = '\0'; 50613244Sgraichen 50759003Shm /* Let's get our hostname */ 508111781Sgad (void)gethostname(hostname, sizeof(hostname)); 50913244Sgraichen 51013244Sgraichen /* Truncate domain */ 511111781Sgad if ((p = strchr(hostname, '.')) != NULL) 51213244Sgraichen *p = '\0'; 513111768Sgad 514111768Sgad /* Parse command line options. */ 515119904Sgad while ((ch = getopt(argc, argv, "a:f:nrsvCD:FR:")) != -1) 516111781Sgad switch (ch) { 51759004Shm case 'a': 51859004Shm archtodir++; 51959004Shm archdirname = optarg; 52059004Shm break; 521111768Sgad case 'f': 522111768Sgad conf = optarg; 523111768Sgad break; 524111768Sgad case 'n': 525111768Sgad noaction++; 526111768Sgad break; 52759003Shm case 'r': 52859003Shm needroot = 0; 52959003Shm break; 530111768Sgad case 's': 531111768Sgad nosignal = 1; 532111768Sgad break; 53359003Shm case 'v': 53459003Shm verbose++; 53559003Shm break; 536114137Sgad case 'C': 537114137Sgad /* Useful for things like rc.diskless... */ 538114137Sgad createlogs++; 539114137Sgad break; 540119904Sgad case 'D': 541119904Sgad /* 542119904Sgad * Set some debugging option. The specific option 543119904Sgad * depends on the value of optarg. These options 544119904Sgad * may come and go without notice or documentation. 545119904Sgad */ 546119904Sgad if (parse_doption(optarg)) 547119904Sgad break; 548119904Sgad usage(); 549119904Sgad /* NOTREACHED */ 55034584Spst case 'F': 55134584Spst force++; 55234584Spst break; 553111772Sgad case 'R': 554111772Sgad rotatereq++; 555111772Sgad requestor = strdup(optarg); 556111772Sgad break; 557111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 55859003Shm default: 55959003Shm usage(); 560111768Sgad /* NOTREACHED */ 56159003Shm } 562111772Sgad 563111772Sgad if (rotatereq) { 564111772Sgad if (optind == argc) { 565111772Sgad warnx("At least one filename must be given when -R is specified."); 566111772Sgad usage(); 567111772Sgad /* NOTREACHED */ 568111772Sgad } 569111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 570111772Sgad for (p = requestor; *p != '\0'; p++) { 571111772Sgad if (!isprintch(*p) && (*p != '\t')) 572111772Sgad *p = '.'; 573111772Sgad } 574111772Sgad } 575119904Sgad 576119904Sgad if (dbg_timenow) { 577119904Sgad /* 578119904Sgad * Note that the 'daytime' variable is not changed. 579119904Sgad * That is only used in messages that track when a 580119904Sgad * logfile is rotated, and if a file *is* rotated, 581119904Sgad * then it will still rotated at the "real now" time. 582119904Sgad */ 583119904Sgad timenow = dbg_timenow; 584119904Sgad strlcpy(debugtime, ctime(&timenow), sizeof(debugtime)); 585119904Sgad fprintf(stderr, "Debug: Running as if TimeNow is %s", 586119904Sgad debugtime); 587119904Sgad } 588119904Sgad 58943071Swollman} 59013244Sgraichen 591119904Sgadstatic int 592119904Sgadparse_doption(const char *doption) 593119904Sgad{ 594119904Sgad const char TN[] = "TN="; 595119904Sgad 596119904Sgad if (strncmp(doption, TN, sizeof(TN) - 1) == 0) { 597119904Sgad /* 598119904Sgad * The "TimeNow" debugging option. This probably will 599119904Sgad * be off by an hour when crossing a timezone change. 600119904Sgad */ 601119904Sgad dbg_timenow = parse8601(doption + sizeof(TN) - 1); 602119904Sgad if (dbg_timenow == (time_t)-1) { 603119904Sgad warnx("Malformed time given on -D %s", doption); 604119904Sgad return (0); /* failure */ 605119904Sgad } 606119904Sgad if (dbg_timenow == (time_t)-2) { 607119904Sgad warnx("Non-existent time specified on -D %s", doption); 608119904Sgad return (0); /* failure */ 609119904Sgad } 610119904Sgad return (1); /* successfully parsed */ 611119904Sgad 612119904Sgad } 613119904Sgad 614119904Sgad warnx("Unknown -D (debug) option: %s", doption); 615119904Sgad return (0); /* failure */ 616119904Sgad} 617119904Sgad 61859004Shmstatic void 61959004Shmusage(void) 62013244Sgraichen{ 62180646Sobrien 62280646Sobrien fprintf(stderr, 623114137Sgad "usage: newsyslog [-CFnrsv] [-a directory] [-f config-file]\n" 624111772Sgad " [ [-R requestor] filename ... ]\n"); 62559003Shm exit(1); 62613244Sgraichen} 62713244Sgraichen 62859004Shm/* 629111773Sgad * Parse a configuration file and return a linked list of all the logs 630111773Sgad * which should be processed. 63113244Sgraichen */ 63259003Shmstatic struct conf_entry * 633111773Sgadget_worklist(char **files) 63413244Sgraichen{ 63559003Shm FILE *f; 636111773Sgad const char *fname; 637111773Sgad char **given; 638111773Sgad struct conf_entry *defconf, *dupent, *ent, *firstnew; 639112020Sgad struct conf_entry *globlist, *lastnew, *worklist; 640112020Sgad int gmatch, fnres; 641111773Sgad 642112020Sgad defconf = globlist = worklist = NULL; 643111773Sgad 644111773Sgad fname = conf; 645111773Sgad if (fname == NULL) 646111773Sgad fname = _PATH_CONF; 647111773Sgad 648111773Sgad if (strcmp(fname, "-") != 0) 649111773Sgad f = fopen(fname, "r"); 650111773Sgad else { 651111773Sgad f = stdin; 652111773Sgad fname = "<stdin>"; 653111773Sgad } 654111773Sgad if (!f) 655111773Sgad err(1, "%s", conf); 656111773Sgad 657112020Sgad parse_file(f, fname, &worklist, &globlist, &defconf); 658111773Sgad (void) fclose(f); 659111773Sgad 660111773Sgad /* 661111773Sgad * All config-file information has been read in and turned into 662112020Sgad * a worklist and a globlist. If there were no specific files 663112020Sgad * given on the run command, then the only thing left to do is to 664112020Sgad * call a routine which finds all files matched by the globlist 665112020Sgad * and adds them to the worklist. Then return the worklist. 666111773Sgad */ 667111773Sgad if (*files == NULL) { 668112020Sgad expand_globs(&worklist, &globlist); 669112020Sgad free_clist(&globlist); 670111773Sgad if (defconf != NULL) 671111773Sgad free_entry(defconf); 672111773Sgad return (worklist); 673111773Sgad /* NOTREACHED */ 674111773Sgad } 675111773Sgad 676111773Sgad /* 677111773Sgad * If newsyslog was given a specific list of files to process, 678111773Sgad * it may be that some of those files were not listed in any 679111773Sgad * config file. Those unlisted files should get the default 680111773Sgad * rotation action. First, create the default-rotation action 681111773Sgad * if none was found in a system config file. 682111773Sgad */ 683111773Sgad if (defconf == NULL) { 684111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 685111773Sgad defconf->numlogs = 3; 686111773Sgad defconf->size = 50; 687111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 688111773Sgad } 689111773Sgad 690111773Sgad /* 691111773Sgad * If newsyslog was run with a list of specific filenames, 692111773Sgad * then create a new worklist which has only those files in 693111773Sgad * it, picking up the rotation-rules for those files from 694111773Sgad * the original worklist. 695111773Sgad * 696111773Sgad * XXX - Note that this will copy multiple rules for a single 697111773Sgad * logfile, if multiple entries are an exact match for 698111773Sgad * that file. That matches the historic behavior, but do 699111773Sgad * we want to continue to allow it? If so, it should 700111773Sgad * probably be handled more intelligently. 701111773Sgad */ 702112020Sgad firstnew = lastnew = NULL; 703111773Sgad for (given = files; *given; ++given) { 704111773Sgad /* 705111773Sgad * First try to find exact-matches for this given file. 706111773Sgad */ 707112020Sgad gmatch = 0; 708111773Sgad for (ent = worklist; ent; ent = ent->next) { 709111773Sgad if (strcmp(ent->log, *given) == 0) { 710111773Sgad gmatch++; 711111773Sgad dupent = init_entry(*given, ent); 712111773Sgad if (!firstnew) 713111773Sgad firstnew = dupent; 714111773Sgad else 715112020Sgad lastnew->next = dupent; 716112020Sgad lastnew = dupent; 717111773Sgad } 718111773Sgad } 719111773Sgad if (gmatch) { 720111773Sgad if (verbose > 2) 721111773Sgad printf("\t+ Matched entry %s\n", *given); 722111773Sgad continue; 723111773Sgad } 724111773Sgad 725111773Sgad /* 726111773Sgad * There was no exact-match for this given file, so look 727111773Sgad * for a "glob" entry which does match. 728111773Sgad */ 729112020Sgad gmatch = 0; 730112020Sgad if (verbose > 2 && globlist != NULL) 731112020Sgad printf("\t+ Checking globs for %s\n", *given); 732112020Sgad for (ent = globlist; ent; ent = ent->next) { 733112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 734112020Sgad if (verbose > 2) 735112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 736112020Sgad ent->log); 737112020Sgad if (fnres == 0) { 738111773Sgad gmatch++; 739111773Sgad dupent = init_entry(*given, ent); 740111773Sgad if (!firstnew) 741111773Sgad firstnew = dupent; 742111773Sgad else 743112020Sgad lastnew->next = dupent; 744112020Sgad lastnew = dupent; 745112020Sgad /* This new entry is not a glob! */ 746111773Sgad dupent->flags &= ~CE_GLOB; 747111773Sgad /* Only allow a match to one glob-entry */ 748111773Sgad break; 749111773Sgad } 750111773Sgad } 751111773Sgad if (gmatch) { 752111773Sgad if (verbose > 2) 753111773Sgad printf("\t+ Matched %s via %s\n", *given, 754111773Sgad ent->log); 755111773Sgad continue; 756111773Sgad } 757111773Sgad 758111773Sgad /* 759111773Sgad * This given file was not found in any config file, so 760111773Sgad * add a worklist item based on the default entry. 761111773Sgad */ 762111773Sgad if (verbose > 2) 763111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 764111773Sgad *given, DEFAULT_MARKER); 765111773Sgad dupent = init_entry(*given, defconf); 766111773Sgad if (!firstnew) 767111773Sgad firstnew = dupent; 768111773Sgad else 769112020Sgad lastnew->next = dupent; 770111773Sgad /* Mark that it was *not* found in a config file */ 771111773Sgad dupent->def_cfg = 1; 772112020Sgad lastnew = dupent; 773111773Sgad } 774111773Sgad 775111773Sgad /* 776112020Sgad * Free all the entries in the original work list, the list of 777112020Sgad * glob entries, and the default entry. 778111773Sgad */ 779112020Sgad free_clist(&worklist); 780112020Sgad free_clist(&globlist); 781112020Sgad free_entry(defconf); 782111773Sgad 783112020Sgad /* And finally, return a worklist which matches the given files. */ 784112013Sgad return (firstnew); 785111773Sgad} 786111773Sgad 787111773Sgad/* 788112020Sgad * Expand the list of entries with filename patterns, and add all files 789112020Sgad * which match those glob-entries onto the worklist. 790112020Sgad */ 791112020Sgadstatic void 792112020Sgadexpand_globs(struct conf_entry **work_p, struct conf_entry **glob_p) 793112020Sgad{ 794112020Sgad int gmatch, gres, i; 795112020Sgad char *mfname; 796112020Sgad struct conf_entry *dupent, *ent, *firstmatch, *globent; 797112020Sgad struct conf_entry *lastmatch; 798112020Sgad glob_t pglob; 799112020Sgad struct stat st_fm; 800112020Sgad 801112020Sgad if ((glob_p == NULL) || (*glob_p == NULL)) 802112020Sgad return; /* There is nothing to do. */ 803112020Sgad 804112020Sgad /* 805112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 806112020Sgad * 807112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 808112020Sgad * a second list, which (by definition) will only match files 809112020Sgad * that already exist. Do not add a glob-related entry for any 810112020Sgad * file which already exists in the fully-specified list. 811112020Sgad */ 812112020Sgad firstmatch = lastmatch = NULL; 813112020Sgad for (globent = *glob_p; globent; globent = globent->next) { 814112020Sgad 815112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 816112020Sgad if (gres != 0) { 817112020Sgad warn("cannot expand pattern (%d): %s", gres, 818112020Sgad globent->log); 819112020Sgad continue; 820112020Sgad } 821112020Sgad 822112020Sgad if (verbose > 2) 823112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 824112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 825112020Sgad mfname = pglob.gl_pathv[i]; 826112020Sgad 827112020Sgad /* See if this file already has a specific entry. */ 828112020Sgad gmatch = 0; 829112020Sgad for (ent = *work_p; ent; ent = ent->next) { 830112020Sgad if (strcmp(mfname, ent->log) == 0) { 831112020Sgad gmatch++; 832112020Sgad break; 833112020Sgad } 834112020Sgad } 835112020Sgad if (gmatch) 836112020Sgad continue; 837112020Sgad 838112020Sgad /* Make sure the named matched is a file. */ 839112020Sgad gres = lstat(mfname, &st_fm); 840112020Sgad if (gres != 0) { 841112020Sgad /* Error on a file that glob() matched?!? */ 842112020Sgad warn("Skipping %s - lstat() error", mfname); 843112020Sgad continue; 844112020Sgad } 845112020Sgad if (!S_ISREG(st_fm.st_mode)) { 846112020Sgad /* We only rotate files! */ 847112020Sgad if (verbose > 2) 848112020Sgad printf("\t+ . skipping %s (!file)\n", 849112020Sgad mfname); 850112020Sgad continue; 851112020Sgad } 852112020Sgad 853112020Sgad if (verbose > 2) 854112020Sgad printf("\t+ . add file %s\n", mfname); 855112020Sgad dupent = init_entry(mfname, globent); 856112020Sgad if (!firstmatch) 857112020Sgad firstmatch = dupent; 858112020Sgad else 859112020Sgad lastmatch->next = dupent; 860112020Sgad lastmatch = dupent; 861112020Sgad /* This new entry is not a glob! */ 862112020Sgad dupent->flags &= ~CE_GLOB; 863112020Sgad } 864112020Sgad globfree(&pglob); 865112020Sgad if (verbose > 2) 866112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 867112020Sgad } 868112020Sgad 869112020Sgad /* Add the list of matched files to the end of the worklist. */ 870112020Sgad if (!*work_p) 871112020Sgad *work_p = firstmatch; 872112020Sgad else { 873112020Sgad ent = *work_p; 874112020Sgad while (ent->next) 875112020Sgad ent = ent->next; 876112020Sgad ent->next = firstmatch; 877112020Sgad } 878112020Sgad 879112020Sgad} 880112020Sgad 881112020Sgad/* 882111773Sgad * Parse a configuration file and update a linked list of all the logs to 883111773Sgad * process. 884111773Sgad */ 885111773Sgadstatic void 886111773Sgadparse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 887112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p) 888111773Sgad{ 88959003Shm char line[BUFSIZ], *parse, *q; 890107737Ssobomax char *cp, *errline, *group; 891112020Sgad struct conf_entry *lastglob, *lastwork, *working; 892111781Sgad struct passwd *pwd; 89359003Shm struct group *grp; 894112020Sgad int eol, special; 89513244Sgraichen 896111773Sgad /* 897111773Sgad * XXX - for now, assume that only one config file will be read, 898111773Sgad * ie, this routine is only called one time. 899111773Sgad */ 900112020Sgad lastglob = lastwork = NULL; 90180646Sobrien 902111773Sgad while (fgets(line, BUFSIZ, cf)) { 903107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 904107737Ssobomax (strlen(line) == 0)) 90559003Shm continue; 90659003Shm errline = strdup(line); 907107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 908107737Ssobomax if (*cp != '#') 909107737Ssobomax continue; 910107737Ssobomax if (*(cp - 1) == '\\') { 911107737Ssobomax strcpy(cp - 1, cp); 912107737Ssobomax cp--; 913107737Ssobomax continue; 914107737Ssobomax } 915107737Ssobomax *cp = '\0'; 916107737Ssobomax break; 917107737Ssobomax } 91860373Sdes 91960373Sdes q = parse = missing_field(sob(line), errline); 92060373Sdes parse = son(line); 92160373Sdes if (!*parse) 92280646Sobrien errx(1, "malformed line (missing fields):\n%s", 92380646Sobrien errline); 92460373Sdes *parse = '\0'; 92560373Sdes 926112020Sgad special = 0; 927111388Sgad working = init_entry(q, NULL); 928111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 929112020Sgad special = 1; 930111773Sgad if (defconf_p == NULL) { 931111773Sgad warnx("Ignoring entry for %s in %s!", q, 932111773Sgad cfname); 933111773Sgad free_entry(working); 934111773Sgad continue; 935111773Sgad } else if (*defconf_p != NULL) { 936111388Sgad warnx("Ignoring duplicate entry for %s!", q); 937111388Sgad free_entry(working); 938111388Sgad continue; 939111388Sgad } 940111773Sgad *defconf_p = working; 94159003Shm } 94213244Sgraichen 94359003Shm q = parse = missing_field(sob(++parse), errline); 94459003Shm parse = son(parse); 94525518Sbrian if (!*parse) 94680646Sobrien errx(1, "malformed line (missing fields):\n%s", 94780646Sobrien errline); 94859003Shm *parse = '\0'; 94959003Shm if ((group = strchr(q, ':')) != NULL || 95059003Shm (group = strrchr(q, '.')) != NULL) { 95159003Shm *group++ = '\0'; 95259003Shm if (*q) { 953119102Sgad if (!(isnumberstr(q))) { 954111781Sgad if ((pwd = getpwnam(q)) == NULL) 95559003Shm errx(1, 95680646Sobrien "error in config file; unknown user:\n%s", 95759003Shm errline); 958111781Sgad working->uid = pwd->pw_uid; 95959003Shm } else 96059003Shm working->uid = atoi(q); 96159003Shm } else 962111779Sgad working->uid = (uid_t)-1; 96313244Sgraichen 96459003Shm q = group; 96559003Shm if (*q) { 966119102Sgad if (!(isnumberstr(q))) { 96759003Shm if ((grp = getgrnam(q)) == NULL) 96859003Shm errx(1, 96980646Sobrien "error in config file; unknown group:\n%s", 97059003Shm errline); 97159003Shm working->gid = grp->gr_gid; 97259003Shm } else 97359003Shm working->gid = atoi(q); 97459003Shm } else 975111779Sgad working->gid = (gid_t)-1; 97613244Sgraichen 97759003Shm q = parse = missing_field(sob(++parse), errline); 97859003Shm parse = son(parse); 97959003Shm if (!*parse) 98080646Sobrien errx(1, "malformed line (missing fields):\n%s", 98180646Sobrien errline); 98259003Shm *parse = '\0'; 983111779Sgad } else { 984111779Sgad working->uid = (uid_t)-1; 985111779Sgad working->gid = (gid_t)-1; 986111779Sgad } 98759003Shm 98859003Shm if (!sscanf(q, "%o", &working->permissions)) 98959003Shm errx(1, "error in config file; bad permissions:\n%s", 99059003Shm errline); 99159003Shm 99259003Shm q = parse = missing_field(sob(++parse), errline); 99359003Shm parse = son(parse); 99425518Sbrian if (!*parse) 99580646Sobrien errx(1, "malformed line (missing fields):\n%s", 99680646Sobrien errline); 99759003Shm *parse = '\0'; 998111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 999111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 100059003Shm errline); 100113244Sgraichen 100259003Shm q = parse = missing_field(sob(++parse), errline); 100359003Shm parse = son(parse); 100425518Sbrian if (!*parse) 100580646Sobrien errx(1, "malformed line (missing fields):\n%s", 100680646Sobrien errline); 100759003Shm *parse = '\0'; 1008114762Sgad if (isdigitch(*q)) 100959003Shm working->size = atoi(q); 1010114762Sgad else if (strcmp(q,"*") == 0) 101159003Shm working->size = -1; 1012114762Sgad else { 1013114762Sgad warnx("Invalid value of '%s' for 'size' in line:\n%s", 1014114762Sgad q, errline); 1015114762Sgad working->size = -1; 1016114762Sgad } 101759003Shm 101859003Shm working->flags = 0; 101959003Shm q = parse = missing_field(sob(++parse), errline); 102059003Shm parse = son(parse); 102125518Sbrian eol = !*parse; 102259003Shm *parse = '\0'; 102343071Swollman { 102459003Shm char *ep; 102559003Shm u_long ul; 102613244Sgraichen 102743071Swollman ul = strtoul(q, &ep, 10); 102843071Swollman if (ep == q) 102943071Swollman working->hours = 0; 103043071Swollman else if (*ep == '*') 103143071Swollman working->hours = -1; 103243071Swollman else if (ul > INT_MAX) 103343071Swollman errx(1, "interval is too large:\n%s", errline); 103443071Swollman else 103543071Swollman working->hours = ul; 103643071Swollman 103780646Sobrien if (*ep != '\0' && *ep != '@' && *ep != '*' && 103880646Sobrien *ep != '$') 103943071Swollman errx(1, "malformed interval/at:\n%s", errline); 104043071Swollman if (*ep == '@') { 1041119899Sgad working->trim_at = parse8601(ep + 1); 104243071Swollman working->flags |= CE_TRIMAT; 104359004Shm } else if (*ep == '$') { 1044119899Sgad working->trim_at = parseDWM(ep + 1); 104559004Shm working->flags |= CE_TRIMAT; 104643071Swollman } 1047119901Sgad if (working->flags & CE_TRIMAT) { 1048119899Sgad if (working->trim_at == (time_t)-1) 1049119899Sgad errx(1, "malformed at:\n%s", errline); 1050119899Sgad if (working->trim_at == (time_t)-2) 1051119899Sgad errx(1, "nonexistent time:\n%s", 1052119899Sgad errline); 1053119899Sgad } 105443071Swollman } 105543071Swollman 105625518Sbrian if (eol) 105759003Shm q = NULL; 105825518Sbrian else { 105959003Shm q = parse = sob(++parse); /* Optional field */ 106059003Shm parse = son(parse); 106159003Shm if (!*parse) 106259003Shm eol = 1; 106359003Shm *parse = '\0'; 106425518Sbrian } 106525443Sache 1066111768Sgad for (; q && *q && !isspacech(*q); q++) { 1067111768Sgad switch (tolowerch(*q)) { 1068111768Sgad case 'b': 106959003Shm working->flags |= CE_BINARY; 1070111768Sgad break; 1071114137Sgad case 'c': 1072111768Sgad /* 1073114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1074114137Sgad * We want `c' and `C' for "create". But we 1075114137Sgad * will temporarily treat `c' as `g', because 1076114137Sgad * FreeBSD releases <= 4.8 have a typo of 1077114137Sgad * checking ('G' || 'c') for CE_GLOB. 1078111768Sgad */ 1079114137Sgad if (*q == 'c') { 1080114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1081114137Sgad errline); 1082114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1083114137Sgad working->flags |= CE_GLOB; 1084114137Sgad break; 1085114137Sgad } 1086114137Sgad working->flags |= CE_CREATE; 1087114137Sgad break; 1088111768Sgad case 'g': 1089106905Ssobomax working->flags |= CE_GLOB; 1090111768Sgad break; 1091111768Sgad case 'j': 1092111768Sgad working->flags |= CE_BZCOMPACT; 1093111768Sgad break; 1094111768Sgad case 'n': 1095111768Sgad working->flags |= CE_NOSIGNAL; 1096111768Sgad break; 1097112003Sgad case 'u': 1098112003Sgad working->flags |= CE_SIGNALGROUP; 1099112003Sgad break; 1100111768Sgad case 'w': 1101107916Ssobomax working->flags |= CE_COMPACTWAIT; 1102111768Sgad break; 1103111768Sgad case 'z': 1104111768Sgad working->flags |= CE_COMPACT; 1105111768Sgad break; 1106111768Sgad case '-': 1107111768Sgad break; 1108111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1109111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1110111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1111111768Sgad default: 111280646Sobrien errx(1, "illegal flag in config file -- %c", 111380646Sobrien *q); 1114111768Sgad } 111559003Shm } 111659003Shm 111725518Sbrian if (eol) 111859003Shm q = NULL; 111925518Sbrian else { 112059003Shm q = parse = sob(++parse); /* Optional field */ 112159003Shm parse = son(parse); 112259003Shm if (!*parse) 112359003Shm eol = 1; 112459003Shm *parse = '\0'; 112525518Sbrian } 112625443Sache 112725443Sache working->pid_file = NULL; 112825443Sache if (q && *q) { 112925443Sache if (*q == '/') 113025443Sache working->pid_file = strdup(q); 113136817Sache else if (isdigit(*q)) 113236817Sache goto got_sig; 113359003Shm else 113480646Sobrien errx(1, 113580646Sobrien "illegal pid file or signal number in config file:\n%s", 113680646Sobrien errline); 113725443Sache } 113836817Sache if (eol) 113959003Shm q = NULL; 114036817Sache else { 114159003Shm q = parse = sob(++parse); /* Optional field */ 114259003Shm *(parse = son(parse)) = '\0'; 114336817Sache } 114436817Sache 114536817Sache working->sig = SIGHUP; 114636817Sache if (q && *q) { 114736817Sache if (isdigit(*q)) { 114859003Shm got_sig: 114936817Sache working->sig = atoi(q); 115036817Sache } else { 115159003Shm err_sig: 115280646Sobrien errx(1, 115380646Sobrien "illegal signal number in config file:\n%s", 115480646Sobrien errline); 115536817Sache } 115636817Sache if (working->sig < 1 || working->sig >= NSIG) 115736817Sache goto err_sig; 115836817Sache } 1159111768Sgad 1160111768Sgad /* 1161111768Sgad * Finish figuring out what pid-file to use (if any) in 1162111768Sgad * later processing if this logfile needs to be rotated. 1163111768Sgad */ 1164111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1165111768Sgad /* 1166111768Sgad * This config-entry specified 'n' for nosignal, 1167111768Sgad * see if it also specified an explicit pid_file. 1168111768Sgad * This would be a pretty pointless combination. 1169111768Sgad */ 1170111768Sgad if (working->pid_file != NULL) { 1171111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1172111768Sgad working->pid_file, errline); 1173111768Sgad free(working->pid_file); 1174111768Sgad working->pid_file = NULL; 1175111768Sgad } 1176111768Sgad } else if (working->pid_file == NULL) { 1177111768Sgad /* 1178111768Sgad * This entry did not specify the 'n' flag, which 1179111768Sgad * means it should signal syslogd unless it had 1180112003Sgad * specified some other pid-file (and obviously the 1181112003Sgad * syslog pid-file will not be for a process-group). 1182112003Sgad * Also, we should only try to notify syslog if we 1183112003Sgad * are root. 1184111768Sgad */ 1185112003Sgad if (working->flags & CE_SIGNALGROUP) { 1186112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1187112003Sgad errline); 1188112003Sgad working->flags &= ~CE_SIGNALGROUP; 1189112003Sgad } 1190111768Sgad if (needroot) 1191111768Sgad working->pid_file = strdup(_PATH_SYSLOGPID); 1192111768Sgad } 1193111768Sgad 1194112020Sgad /* 1195112020Sgad * Add this entry to the appropriate list of entries, unless 1196112020Sgad * it was some kind of special entry (eg: <default>). 1197112020Sgad */ 1198112020Sgad if (special) { 1199112020Sgad ; /* Do not add to any list */ 1200112020Sgad } else if (working->flags & CE_GLOB) { 1201112020Sgad if (!*glob_p) 1202112020Sgad *glob_p = working; 1203112020Sgad else 1204112020Sgad lastglob->next = working; 1205112020Sgad lastglob = working; 1206112020Sgad } else { 1207112020Sgad if (!*work_p) 1208112020Sgad *work_p = working; 1209112020Sgad else 1210112020Sgad lastwork->next = working; 1211112020Sgad lastwork = working; 1212112020Sgad } 1213112020Sgad 121459003Shm free(errline); 1215111768Sgad errline = NULL; 121659003Shm } 121713244Sgraichen} 121813244Sgraichen 121959003Shmstatic char * 122059004Shmmissing_field(char *p, char *errline) 122113244Sgraichen{ 122280646Sobrien 122359003Shm if (!p || !*p) 122459003Shm errx(1, "missing field in config file:\n%s", errline); 122559003Shm return (p); 122613244Sgraichen} 122713244Sgraichen 122859004Shmstatic void 1229111780Sgaddotrim(const struct conf_entry *ent, char *log, int numdays, int flags) 123013244Sgraichen{ 123171299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 123271299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 123371299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 123480638Sobrien char jfile1[MAXPATHLEN]; 123594352Ssheldonh char tfile[MAXPATHLEN]; 123659003Shm int notified, need_notification, fd, _numdays; 123759003Shm struct stat st; 123813244Sgraichen 123959004Shm if (archtodir) { 124059004Shm char *p; 124113244Sgraichen 124259004Shm /* build complete name of archive directory into dirpart */ 124359004Shm if (*archdirname == '/') { /* absolute */ 124471299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 124559004Shm } else { /* relative */ 124659004Shm /* get directory part of logfile */ 124771299Sjedgar strlcpy(dirpart, log, sizeof(dirpart)); 124859004Shm if ((p = rindex(dirpart, '/')) == NULL) 124959004Shm dirpart[0] = '\0'; 125059004Shm else 125159004Shm *(p + 1) = '\0'; 125271299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 125359004Shm } 125459004Shm 125559004Shm /* check if archive directory exists, if not, create it */ 125659004Shm if (lstat(dirpart, &st)) 1257114137Sgad createdir(ent, dirpart); 125859004Shm 125959004Shm /* get filename part of logfile */ 126059004Shm if ((p = rindex(log, '/')) == NULL) 126171299Sjedgar strlcpy(namepart, log, sizeof(namepart)); 126259004Shm else 126371299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 126459004Shm 126559004Shm /* name of oldest log */ 126680646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 126780646Sobrien namepart, numdays); 126871299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 126971299Sjedgar COMPRESS_POSTFIX); 127080638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 127180638Sobrien BZCOMPRESS_POSTFIX); 127259004Shm } else { 127359004Shm /* name of oldest log */ 127471299Sjedgar (void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays); 127571299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 127671299Sjedgar COMPRESS_POSTFIX); 127780638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 127880638Sobrien BZCOMPRESS_POSTFIX); 127959004Shm } 128059004Shm 128159003Shm if (noaction) { 1282111967Sgad printf("\trm -f %s\n", file1); 1283111967Sgad printf("\trm -f %s\n", zfile1); 1284111967Sgad printf("\trm -f %s\n", jfile1); 128559003Shm } else { 128659003Shm (void) unlink(file1); 128759003Shm (void) unlink(zfile1); 128880638Sobrien (void) unlink(jfile1); 128959003Shm } 129013244Sgraichen 129159003Shm /* Move down log files */ 129218188Sjkh _numdays = numdays; /* preserve */ 129359003Shm while (numdays--) { 129459004Shm 129571299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 129659004Shm 129759004Shm if (archtodir) 129880646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 129980646Sobrien dirpart, namepart, numdays); 130059004Shm else 130180646Sobrien (void) snprintf(file1, sizeof(file1), "%s.%d", log, 130280646Sobrien numdays); 130359004Shm 130471299Sjedgar (void) strlcpy(zfile1, file1, sizeof(zfile1)); 130571299Sjedgar (void) strlcpy(zfile2, file2, sizeof(zfile2)); 130659003Shm if (lstat(file1, &st)) { 130780646Sobrien (void) strlcat(zfile1, COMPRESS_POSTFIX, 130880646Sobrien sizeof(zfile1)); 130980646Sobrien (void) strlcat(zfile2, COMPRESS_POSTFIX, 131080646Sobrien sizeof(zfile2)); 131180638Sobrien if (lstat(zfile1, &st)) { 131280638Sobrien strlcpy(zfile1, file1, sizeof(zfile1)); 131380638Sobrien strlcpy(zfile2, file2, sizeof(zfile2)); 131480638Sobrien strlcat(zfile1, BZCOMPRESS_POSTFIX, 131580638Sobrien sizeof(zfile1)); 131680638Sobrien strlcat(zfile2, BZCOMPRESS_POSTFIX, 131780638Sobrien sizeof(zfile2)); 131880638Sobrien if (lstat(zfile1, &st)) 131980638Sobrien continue; 132080638Sobrien } 132159003Shm } 132259003Shm if (noaction) { 1323111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1324111967Sgad printf("\tchmod %o %s\n", ent->permissions, zfile2); 1325111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1326111967Sgad printf("\tchown %u:%u %s\n", 1327111779Sgad ent->uid, ent->gid, zfile2); 132859003Shm } else { 132959003Shm (void) rename(zfile1, zfile2); 1330111780Sgad if (chmod(zfile2, ent->permissions)) 1331111780Sgad warn("can't chmod %s", file2); 1332111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1333111779Sgad if (chown(zfile2, ent->uid, ent->gid)) 1334111779Sgad warn("can't chown %s", zfile2); 133559003Shm } 133659003Shm } 1337111388Sgad if (!noaction && !(flags & CE_BINARY)) { 1338111388Sgad /* Report the trimming to the old log */ 1339111779Sgad (void) log_trim(log, ent); 1340111388Sgad } 134113244Sgraichen 134218188Sjkh if (!_numdays) { 134318075Sjkh if (noaction) 1344111967Sgad printf("\trm %s\n", log); 134518075Sjkh else 134659003Shm (void) unlink(log); 134759003Shm } else { 134818075Sjkh if (noaction) 1349111967Sgad printf("\tmv %s to %s\n", log, file1); 135059004Shm else { 135159004Shm if (archtodir) 1352111780Sgad movefile(log, file1, ent->permissions, ent->uid, 1353111779Sgad ent->gid); 135459004Shm else 135559004Shm (void) rename(log, file1); 135659004Shm } 135718075Sjkh } 135818075Sjkh 1359111781Sgad /* Now move the new log file into place */ 1360114137Sgad /* XXX - We should replace the above 'rename' with 'link(log, file1)' 1361114137Sgad * then replace the following with 'createfile(ent)' */ 1362111967Sgad strlcpy(tfile, log, sizeof(tfile)); 1363111967Sgad strlcat(tfile, ".XXXXXX", sizeof(tfile)); 1364111967Sgad if (noaction) { 1365111781Sgad printf("Start new log...\n"); 1366111967Sgad printf("\tmktemp %s\n", tfile); 1367111967Sgad } else { 136894352Ssheldonh mkstemp(tfile); 1369111780Sgad fd = creat(tfile, ent->permissions); 137059003Shm if (fd < 0) 137159003Shm err(1, "can't start new log"); 1372111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1373111779Sgad if (fchown(fd, ent->uid, ent->gid)) 1374111779Sgad err(1, "can't chown new log file"); 137559003Shm (void) close(fd); 1376111388Sgad if (!(flags & CE_BINARY)) { 1377111388Sgad /* Add status message to new log file */ 1378111779Sgad if (log_trim(tfile, ent)) 137959003Shm err(1, "can't add status message to log"); 1380111388Sgad } 138159003Shm } 1382111967Sgad if (noaction) { 1383111967Sgad printf("\tchmod %o %s\n", ent->permissions, tfile); 1384111967Sgad printf("\tmv %s %s\n", tfile, log); 1385111967Sgad } else { 1386111780Sgad (void) chmod(tfile, ent->permissions); 138794352Ssheldonh if (rename(tfile, log) < 0) { 138894352Ssheldonh err(1, "can't start new log"); 138994352Ssheldonh (void) unlink(tfile); 139094352Ssheldonh } 139194352Ssheldonh } 139225443Sache 1393111966Sgad /* 1394111966Sgad * Find out if there is a process to signal. If nosignal (-s) was 1395111966Sgad * specified, then do not signal any process. Note that nosignal 1396111966Sgad * will trigger a warning message if the rotated logfile needs to 1397111966Sgad * be compressed, *unless* -R was specified. This is because there 1398111966Sgad * presumably still are process(es) writing to the old logfile, but 1399111966Sgad * we assume that a -sR request comes from a process which writes 1400111966Sgad * to the logfile, and as such, that process has already made sure 1401111966Sgad * that the logfile is not presently in use. 1402111966Sgad */ 140325443Sache need_notification = notified = 0; 1404111779Sgad if (ent->pid_file != NULL) { 140525443Sache need_notification = 1; 1406111966Sgad if (!nosignal) 1407112003Sgad notified = send_signal(ent); /* the normal case! */ 1408111966Sgad else if (rotatereq) 1409111966Sgad need_notification = 0; 141025443Sache } 1411112003Sgad 141280638Sobrien if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) { 141325443Sache if (need_notification && !notified) 141480646Sobrien warnx( 1415112003Sgad "log %s.0 not compressed because daemon(s) not notified", 141680646Sobrien log); 141725443Sache else if (noaction) 1418111967Sgad if (flags & CE_COMPACT) 1419111967Sgad printf("\tgzip %s.0\n", log); 1420111967Sgad else 1421111967Sgad printf("\tbzip2 %s.0\n", log); 142225443Sache else { 142325443Sache if (notified) { 142425443Sache if (verbose) 1425112003Sgad printf("small pause to allow daemon(s) to close log\n"); 142631460Sache sleep(10); 142725443Sache } 142859004Shm if (archtodir) { 142980646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s", 143080646Sobrien dirpart, namepart); 143180638Sobrien if (flags & CE_COMPACT) 1432107916Ssobomax compress_log(file1, 1433107916Ssobomax flags & CE_COMPACTWAIT); 143480638Sobrien else if (flags & CE_BZCOMPACT) 1435107916Ssobomax bzcompress_log(file1, 1436107916Ssobomax flags & CE_COMPACTWAIT); 143759004Shm } else { 143880638Sobrien if (flags & CE_COMPACT) 1439107916Ssobomax compress_log(log, 1440107916Ssobomax flags & CE_COMPACTWAIT); 144180638Sobrien else if (flags & CE_BZCOMPACT) 1442107916Ssobomax bzcompress_log(log, 1443107916Ssobomax flags & CE_COMPACTWAIT); 144459004Shm } 144525443Sache } 144659003Shm } 144713244Sgraichen} 144813244Sgraichen 144913244Sgraichen/* Log the fact that the logs were turned over */ 145059004Shmstatic int 1451119926Sgadlog_trim(const char *logname, const struct conf_entry *log_ent) 145213244Sgraichen{ 145359003Shm FILE *f; 1454111388Sgad const char *xtra; 145559003Shm 1456119926Sgad if ((f = fopen(logname, "a")) == NULL) 145759003Shm return (-1); 1458111388Sgad xtra = ""; 1459111768Sgad if (log_ent->def_cfg) 1460111388Sgad xtra = " using <default> rule"; 1461114137Sgad if (log_ent->firstcreate) 1462114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 1463114137Sgad daytime, hostname, (int) getpid(), xtra); 1464114137Sgad else if (log_ent->r_reason != NULL) 1465111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 1466111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 1467111772Sgad else 1468111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 1469111772Sgad daytime, hostname, (int) getpid(), xtra); 147059003Shm if (fclose(f) == EOF) 147159003Shm err(1, "log_trim: fclose:"); 147259003Shm return (0); 147313244Sgraichen} 147413244Sgraichen 147559004Shm/* Fork of gzip to compress the old log file */ 147659004Shmstatic void 1477119926Sgadcompress_log(char *logname, int dowait) 147813244Sgraichen{ 147959003Shm pid_t pid; 148071299Sjedgar char tmp[MAXPATHLEN]; 148159003Shm 1482107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1483107916Ssobomax ; 1484119926Sgad (void) snprintf(tmp, sizeof(tmp), "%s.0", logname); 148525443Sache pid = fork(); 148659003Shm if (pid < 0) 148780638Sobrien err(1, "gzip fork"); 148859003Shm else if (!pid) { 148979452Sbrian (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0); 149043071Swollman err(1, _PATH_GZIP); 149159003Shm } 149213244Sgraichen} 149313244Sgraichen 149480638Sobrien/* Fork of bzip2 to compress the old log file */ 149580638Sobrienstatic void 1496119926Sgadbzcompress_log(char *logname, int dowait) 149780638Sobrien{ 149880638Sobrien pid_t pid; 149980638Sobrien char tmp[MAXPATHLEN]; 150080638Sobrien 1501107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1502107916Ssobomax ; 1503119926Sgad snprintf(tmp, sizeof(tmp), "%s.0", logname); 150480638Sobrien pid = fork(); 150580638Sobrien if (pid < 0) 150680638Sobrien err(1, "bzip2 fork"); 150780638Sobrien else if (!pid) { 150886360Sobrien execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, (char *)0); 150980638Sobrien err(1, _PATH_BZIP2); 151080638Sobrien } 151180638Sobrien} 151280638Sobrien 151313244Sgraichen/* Return size in kilobytes of a file */ 151459004Shmstatic int 151559004Shmsizefile(char *file) 151613244Sgraichen{ 151759003Shm struct stat sb; 151813244Sgraichen 151959003Shm if (stat(file, &sb) < 0) 152059003Shm return (-1); 152159003Shm return (kbytes(dbtob(sb.st_blocks))); 152213244Sgraichen} 152313244Sgraichen 152413244Sgraichen/* Return the age of old log file (file.0) */ 152559004Shmstatic int 152659004Shmage_old_log(char *file) 152713244Sgraichen{ 152859003Shm struct stat sb; 1529114764Sgad char *endp; 1530114764Sgad char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1531114764Sgad sizeof(BZCOMPRESS_POSTFIX) + 1]; 153213244Sgraichen 153359004Shm if (archtodir) { 153459004Shm char *p; 153559004Shm 153659004Shm /* build name of archive directory into tmp */ 153759004Shm if (*archdirname == '/') { /* absolute */ 153871299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 153959004Shm } else { /* relative */ 154059004Shm /* get directory part of logfile */ 154171299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 154259004Shm if ((p = rindex(tmp, '/')) == NULL) 154359004Shm tmp[0] = '\0'; 154459004Shm else 154559004Shm *(p + 1) = '\0'; 154671299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 154759004Shm } 154859004Shm 154971299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 155059004Shm 155159004Shm /* get filename part of logfile */ 155259004Shm if ((p = rindex(file, '/')) == NULL) 155371299Sjedgar strlcat(tmp, file, sizeof(tmp)); 155459004Shm else 155571299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 155659004Shm } else { 155771299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 155859004Shm } 155959004Shm 1560114764Sgad strlcat(tmp, ".0", sizeof(tmp)); 1561114764Sgad if (stat(tmp, &sb) < 0) { 1562114764Sgad /* 1563114764Sgad * A plain '.0' file does not exist. Try again, first 1564114764Sgad * with the added suffix of '.gz', then with an added 1565114764Sgad * suffix of '.bz2' instead of '.gz'. 1566114764Sgad */ 1567114764Sgad endp = strchr(tmp, '\0'); 1568114764Sgad strlcat(tmp, COMPRESS_POSTFIX, sizeof(tmp)); 1569114764Sgad if (stat(tmp, &sb) < 0) { 1570114764Sgad *endp = '\0'; /* Remove .gz */ 1571114764Sgad strlcat(tmp, BZCOMPRESS_POSTFIX, sizeof(tmp)); 1572114764Sgad if (stat(tmp, &sb) < 0) 1573114764Sgad return (-1); 1574114764Sgad } 1575114764Sgad } 1576111781Sgad return ((int)(timenow - sb.st_mtime + 1800) / 3600); 157713244Sgraichen} 157813244Sgraichen 157913244Sgraichen/* Skip Over Blanks */ 1580111820Sgadstatic char * 158159004Shmsob(char *p) 158213244Sgraichen{ 158359003Shm while (p && *p && isspace(*p)) 158459003Shm p++; 158559003Shm return (p); 158613244Sgraichen} 158713244Sgraichen 158813244Sgraichen/* Skip Over Non-Blanks */ 1589111820Sgadstatic char * 159059004Shmson(char *p) 159113244Sgraichen{ 159259003Shm while (p && *p && !isspace(*p)) 159359003Shm p++; 159459003Shm return (p); 159513244Sgraichen} 159643071Swollman 1597119102Sgad/* Check if string is actually a number */ 1598119102Sgadstatic int 1599119102Sgadisnumberstr(const char *string) 1600119102Sgad{ 1601119102Sgad while (*string) { 1602119102Sgad if (!isdigitch(*string++)) 1603119102Sgad return (0); 1604119102Sgad } 1605119102Sgad return (1); 1606119102Sgad} 1607119102Sgad 160859004Shm/* physically move file */ 160959004Shmstatic void 1610111779Sgadmovefile(char *from, char *to, int perm, uid_t owner_uid, gid_t group_gid) 161159004Shm{ 161259004Shm FILE *src, *dst; 161359004Shm int c; 161459004Shm 161559004Shm if ((src = fopen(from, "r")) == NULL) 161659004Shm err(1, "can't fopen %s for reading", from); 161759004Shm if ((dst = fopen(to, "w")) == NULL) 161859004Shm err(1, "can't fopen %s for writing", to); 1619111779Sgad if (owner_uid != (uid_t)-1 || group_gid != (gid_t)-1) { 1620111779Sgad if (fchown(fileno(dst), owner_uid, group_gid)) 1621111779Sgad err(1, "can't fchown %s", to); 1622111779Sgad } 162359004Shm if (fchmod(fileno(dst), perm)) 162459004Shm err(1, "can't fchmod %s", to); 162559004Shm 162659004Shm while ((c = getc(src)) != EOF) { 162759004Shm if ((putc(c, dst)) == EOF) 162859004Shm err(1, "error writing to %s", to); 162959004Shm } 163059004Shm 163159004Shm if (ferror(src)) 163259004Shm err(1, "error reading from %s", from); 163359004Shm if ((fclose(src)) != 0) 163459004Shm err(1, "can't fclose %s", to); 163559004Shm if ((fclose(dst)) != 0) 163659004Shm err(1, "can't fclose %s", from); 163759004Shm if ((unlink(from)) != 0) 163859004Shm err(1, "can't unlink %s", from); 163959004Shm} 164059004Shm 164159004Shm/* create one or more directory components of a path */ 164259004Shmstatic void 1643114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 164459004Shm{ 1645111398Sgad int res; 164659004Shm char *s, *d; 164771299Sjedgar char mkdirpath[MAXPATHLEN]; 164859004Shm struct stat st; 164959004Shm 165059004Shm s = dirpart; 165159004Shm d = mkdirpath; 165259004Shm 165359004Shm for (;;) { 165459004Shm *d++ = *s++; 1655111398Sgad if (*s != '/' && *s != '\0') 1656111398Sgad continue; 1657111398Sgad *d = '\0'; 1658111398Sgad res = lstat(mkdirpath, &st); 1659111398Sgad if (res != 0) { 1660111398Sgad if (noaction) { 1661111967Sgad printf("\tmkdir %s\n", mkdirpath); 1662111398Sgad } else { 1663111398Sgad res = mkdir(mkdirpath, 0755); 1664111398Sgad if (res != 0) 1665111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 1666111398Sgad mkdirpath); 1667111398Sgad } 166859004Shm } 166959004Shm if (*s == '\0') 167059004Shm break; 167159004Shm } 1672114137Sgad if (verbose) { 1673114137Sgad if (ent->firstcreate) 1674114137Sgad printf("Created directory '%s' for new %s\n", 1675114137Sgad dirpart, ent->log); 1676114137Sgad else 1677114137Sgad printf("Created directory '%s' for -a\n", dirpart); 1678114137Sgad } 167959004Shm} 168059004Shm 1681114137Sgad/* 1682114137Sgad * Create a new log file, destroying any currently-existing version 1683114137Sgad * of the log file in the process. If the caller wants a backup copy 1684114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 1685114137Sgad * before calling this routine. 1686114137Sgad */ 1687114137Sgadvoid 1688114137Sgadcreatelog(const struct conf_entry *ent) 1689114137Sgad{ 1690114137Sgad int fd, failed; 1691114137Sgad struct stat st; 1692114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 1693114137Sgad 1694114137Sgad fd = -1; 1695114137Sgad realfile = ent->log; 1696114137Sgad 1697114137Sgad /* 1698114137Sgad * If this log file is being created for the first time (-C option), 1699114137Sgad * then it may also be true that the parent directory does not exist 1700114137Sgad * yet. Check, and create that directory if it is missing. 1701114137Sgad */ 1702114137Sgad if (ent->firstcreate) { 1703114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 1704114137Sgad slash = strrchr(tempfile, '/'); 1705114137Sgad if (slash != NULL) { 1706114137Sgad *slash = '\0'; 1707114137Sgad failed = lstat(tempfile, &st); 1708114137Sgad if (failed && errno != ENOENT) 1709114137Sgad err(1, "Error on lstat(%s)", tempfile); 1710114137Sgad if (failed) 1711114137Sgad createdir(ent, tempfile); 1712114137Sgad else if (!S_ISDIR(st.st_mode)) 1713114137Sgad errx(1, "%s exists but is not a directory", 1714114137Sgad tempfile); 1715114137Sgad } 1716114137Sgad } 1717114137Sgad 1718114137Sgad /* 1719114137Sgad * First create an unused filename, so it can be chown'ed and 1720114137Sgad * chmod'ed before it is moved into the real location. mkstemp 1721114137Sgad * will create the file mode=600 & owned by us. Note that all 1722114137Sgad * temp files will have a suffix of '.z<something>'. 1723114137Sgad */ 1724114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 1725114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 1726114137Sgad if (noaction) 1727114137Sgad printf("\tmktemp %s\n", tempfile); 1728114137Sgad else { 1729114137Sgad fd = mkstemp(tempfile); 1730114137Sgad if (fd < 0) 1731114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 1732114137Sgad 1733114137Sgad /* 1734114137Sgad * Add status message to what will become the new log file. 1735114137Sgad */ 1736114137Sgad if (!(ent->flags & CE_BINARY)) { 1737114137Sgad if (log_trim(tempfile, ent)) 1738114137Sgad err(1, "can't add status message to log"); 1739114137Sgad } 1740114137Sgad } 1741114137Sgad 1742114137Sgad /* Change the owner/group, if we are supposed to */ 1743114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 1744114137Sgad if (noaction) 1745114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 1746114137Sgad tempfile); 1747114137Sgad else { 1748114137Sgad failed = fchown(fd, ent->uid, ent->gid); 1749114137Sgad if (failed) 1750114137Sgad err(1, "can't fchown temp file %s", tempfile); 1751114137Sgad } 1752114137Sgad } 1753114137Sgad 1754114137Sgad /* 1755114137Sgad * Note that if the real logfile still exists, and if the call 1756114137Sgad * to rename() fails, then "neither the old file nor the new 1757114137Sgad * file shall be changed or created" (to quote the standard). 1758114137Sgad * If the call succeeds, then the file will be replaced without 1759114137Sgad * any window where some other process might find that the file 1760114137Sgad * did not exist. 1761114137Sgad * XXX - ? It may be that for some error conditions, we could 1762114137Sgad * retry by first removing the realfile and then renaming. 1763114137Sgad */ 1764114137Sgad if (noaction) { 1765114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 1766114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 1767114137Sgad } else { 1768114137Sgad failed = fchmod(fd, ent->permissions); 1769114137Sgad if (failed) 1770114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 1771114137Sgad failed = rename(tempfile, realfile); 1772114137Sgad if (failed) 1773114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 1774114137Sgad } 1775114137Sgad 1776114137Sgad if (fd >= 0) 1777114137Sgad close(fd); 1778114137Sgad} 1779114137Sgad 178059004Shm/*- 1781119897Sgad * Parse a limited subset of ISO 8601. The specific format is as follows: 1782119897Sgad * 1783119897Sgad * [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter) 1784119897Sgad * 1785119897Sgad * We don't accept a timezone specification; missing fields (including timezone) 1786119897Sgad * are defaulted to the current date but time zero. 1787119897Sgad */ 1788119897Sgadstatic time_t 1789119904Sgadparse8601(const char *s) 1790119897Sgad{ 1791119897Sgad char *t; 1792119897Sgad time_t tsecs; 1793119897Sgad struct tm tm, *tmp; 1794119902Sgad long l; 1795119897Sgad 1796119897Sgad tmp = localtime(&timenow); 1797119897Sgad tm = *tmp; 1798119897Sgad 1799119897Sgad tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 1800119897Sgad 1801119902Sgad l = strtol(s, &t, 10); 1802119902Sgad if (l < 0 || l >= INT_MAX || (*t != '\0' && *t != 'T')) 1803119897Sgad return (-1); 1804119897Sgad 1805119897Sgad /* 1806119897Sgad * Now t points either to the end of the string (if no time was 1807119897Sgad * provided) or to the letter `T' which separates date and time in 1808119897Sgad * ISO 8601. The pointer arithmetic is the same for either case. 1809119897Sgad */ 1810119897Sgad switch (t - s) { 1811119897Sgad case 8: 1812119902Sgad tm.tm_year = ((l / 1000000) - 19) * 100; 1813119902Sgad l = l % 1000000; 1814119897Sgad case 6: 1815119897Sgad tm.tm_year -= tm.tm_year % 100; 1816119902Sgad tm.tm_year += l / 10000; 1817119902Sgad l = l % 10000; 1818119897Sgad case 4: 1819119902Sgad tm.tm_mon = (l / 100) - 1; 1820119902Sgad l = l % 100; 1821119897Sgad case 2: 1822119902Sgad tm.tm_mday = l; 1823119897Sgad case 0: 1824119897Sgad break; 1825119897Sgad default: 1826119897Sgad return (-1); 1827119897Sgad } 1828119897Sgad 1829119897Sgad /* sanity check */ 1830119897Sgad if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12 1831119897Sgad || tm.tm_mday < 1 || tm.tm_mday > 31) 1832119897Sgad return (-1); 1833119897Sgad 1834119897Sgad if (*t != '\0') { 1835119897Sgad s = ++t; 1836119902Sgad l = strtol(s, &t, 10); 1837119902Sgad if (l < 0 || l >= INT_MAX || (*t != '\0' && !isspace(*t))) 1838119897Sgad return (-1); 1839119897Sgad 1840119897Sgad switch (t - s) { 1841119897Sgad case 6: 1842119902Sgad tm.tm_sec = l % 100; 1843119902Sgad l /= 100; 1844119897Sgad case 4: 1845119902Sgad tm.tm_min = l % 100; 1846119902Sgad l /= 100; 1847119897Sgad case 2: 1848119902Sgad tm.tm_hour = l; 1849119897Sgad case 0: 1850119897Sgad break; 1851119897Sgad default: 1852119897Sgad return (-1); 1853119897Sgad } 1854119897Sgad 1855119897Sgad /* sanity check */ 1856119897Sgad if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0 1857119897Sgad || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23) 1858119897Sgad return (-1); 1859119897Sgad } 1860119899Sgad 1861119899Sgad tsecs = mktime(&tm); 1862119899Sgad /* 1863119899Sgad * Check for invalid times, including things like the missing 1864119905Sgad * hour when switching from "standard time" to "daylight saving". 1865119899Sgad */ 1866119899Sgad if (tsecs == (time_t)-1) 1867119899Sgad tsecs = (time_t)-2; 1868119897Sgad return (tsecs); 1869119897Sgad} 1870119897Sgad 1871119897Sgad/*- 187259004Shm * Parse a cyclic time specification, the format is as follows: 187359004Shm * 187459004Shm * [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]] 187559004Shm * 187659004Shm * to rotate a logfile cyclic at 187759004Shm * 187859004Shm * - every day (D) within a specific hour (hh) (hh = 0...23) 187959004Shm * - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday) 188059004Shm * - once a month (M) at a specific day (d) (d = 1..31,l|L) 188159004Shm * 188259004Shm * We don't accept a timezone specification; missing fields 188359004Shm * are defaulted to the current date but time zero. 188459004Shm */ 188559004Shmstatic time_t 1886119899SgadparseDWM(char *s) 188759004Shm{ 188859004Shm char *t; 188993659Scjc time_t tsecs; 189059004Shm struct tm tm, *tmp; 189180742Sobrien long l; 189259004Shm int nd; 189359004Shm static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 189459004Shm int WMseen = 0; 189559004Shm int Dseen = 0; 189659004Shm 189759004Shm tmp = localtime(&timenow); 189859004Shm tm = *tmp; 189959004Shm 190059004Shm /* set no. of days per month */ 190159004Shm 190259004Shm nd = mtab[tm.tm_mon]; 190359004Shm 190459004Shm if (tm.tm_mon == 1) { 190559004Shm if (((tm.tm_year + 1900) % 4 == 0) && 190659004Shm ((tm.tm_year + 1900) % 100 != 0) && 190759004Shm ((tm.tm_year + 1900) % 400 == 0)) { 190859004Shm nd++; /* leap year, 29 days in february */ 190959004Shm } 191059004Shm } 191159004Shm tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 191259004Shm 191359004Shm for (;;) { 191459004Shm switch (*s) { 191559004Shm case 'D': 191659004Shm if (Dseen) 1917111392Sgad return (-1); 191859004Shm Dseen++; 191959004Shm s++; 192080742Sobrien l = strtol(s, &t, 10); 192180742Sobrien if (l < 0 || l > 23) 1922111392Sgad return (-1); 192380742Sobrien tm.tm_hour = l; 192459004Shm break; 192559004Shm 192659004Shm case 'W': 192759004Shm if (WMseen) 1928111392Sgad return (-1); 192959004Shm WMseen++; 193059004Shm s++; 193180742Sobrien l = strtol(s, &t, 10); 193280742Sobrien if (l < 0 || l > 6) 1933111392Sgad return (-1); 193480742Sobrien if (l != tm.tm_wday) { 193559004Shm int save; 193659004Shm 193780742Sobrien if (l < tm.tm_wday) { 193859004Shm save = 6 - tm.tm_wday; 193980742Sobrien save += (l + 1); 194059004Shm } else { 194180742Sobrien save = l - tm.tm_wday; 194259004Shm } 194359004Shm 194459004Shm tm.tm_mday += save; 194559004Shm 194659004Shm if (tm.tm_mday > nd) { 194759004Shm tm.tm_mon++; 194859004Shm tm.tm_mday = tm.tm_mday - nd; 194959004Shm } 195059004Shm } 195159004Shm break; 195259004Shm 195359004Shm case 'M': 195459004Shm if (WMseen) 1955111392Sgad return (-1); 195659004Shm WMseen++; 195759004Shm s++; 195859004Shm if (tolower(*s) == 'l') { 195959004Shm tm.tm_mday = nd; 196059004Shm s++; 196159004Shm t = s; 196259004Shm } else { 196380742Sobrien l = strtol(s, &t, 10); 196480742Sobrien if (l < 1 || l > 31) 1965111392Sgad return (-1); 196659004Shm 196780742Sobrien if (l > nd) 1968111392Sgad return (-1); 196980742Sobrien tm.tm_mday = l; 197059004Shm } 197159004Shm break; 197259004Shm 197359004Shm default: 197459004Shm return (-1); 197559004Shm break; 197659004Shm } 197759004Shm 197859004Shm if (*t == '\0' || isspace(*t)) 197959004Shm break; 198059004Shm else 198159004Shm s = t; 198259004Shm } 1983119899Sgad 1984119899Sgad tsecs = mktime(&tm); 1985119899Sgad /* 1986119899Sgad * Check for invalid times, including things like the missing 1987119905Sgad * hour when switching from "standard time" to "daylight saving". 1988119899Sgad */ 1989119899Sgad if (tsecs == (time_t)-1) 1990119899Sgad tsecs = (time_t)-2; 1991111392Sgad return (tsecs); 199259004Shm} 1993