newsyslog.c revision 114601
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 114601 2003-05-03 21:06:42Z obrien $"); 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 */ 11859003Shmtime_t timenow; 11959003Shm 12071299Sjedgarchar hostname[MAXHOSTNAMELEN]; /* hostname */ 121108164Strhodeschar daytime[16]; /* timenow in human readable form */ 12213244Sgraichen 123111773Sgadstatic struct conf_entry *get_worklist(char **files); 124111773Sgadstatic void parse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 125112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p); 12616240Salexstatic char *sob(char *p); 12716240Salexstatic char *son(char *p); 12859003Shmstatic char *missing_field(char *p, char *errline); 12959003Shmstatic void do_entry(struct conf_entry * ent); 130112020Sgadstatic void expand_globs(struct conf_entry **work_p, 131112020Sgad struct conf_entry **glob_p); 132112020Sgadstatic void free_clist(struct conf_entry **firstent); 133111388Sgadstatic void free_entry(struct conf_entry *ent); 134111388Sgadstatic struct conf_entry *init_entry(const char *fname, 135111388Sgad struct conf_entry *src_entry); 136111781Sgadstatic void parse_args(int argc, char **argv); 13780640Sobrienstatic void usage(void); 138111779Sgadstatic void dotrim(const struct conf_entry *ent, char *log, 139111780Sgad int numdays, int flags); 140111768Sgadstatic int log_trim(const char *log, const struct conf_entry *log_ent); 141107916Ssobomaxstatic void compress_log(char *log, int dowait); 142107916Ssobomaxstatic void bzcompress_log(char *log, int dowait); 14316240Salexstatic int sizefile(char *file); 14416240Salexstatic int age_old_log(char *file); 145112003Sgadstatic int send_signal(const struct conf_entry *ent); 14693659Scjcstatic time_t parse8601(char *s, char *errline); 147111779Sgadstatic void movefile(char *from, char *to, int perm, uid_t owner_uid, 148111779Sgad gid_t group_gid); 149114137Sgadstatic void createdir(const struct conf_entry *ent, char *dirpart); 150114137Sgadstatic void createlog(const struct conf_entry *ent); 15193659Scjcstatic time_t parseDWM(char *s, char *errline); 15213244Sgraichen 153111768Sgad/* 154111768Sgad * All the following are defined to work on an 'int', in the 155111768Sgad * range 0 to 255, plus EOF. Define wrappers which can take 156111768Sgad * values of type 'char', either signed or unsigned. 157111768Sgad */ 158111772Sgad#define isprintch(Anychar) isprint(((int) Anychar) & 255) 159111768Sgad#define isspacech(Anychar) isspace(((int) Anychar) & 255) 160111768Sgad#define tolowerch(Anychar) tolower(((int) Anychar) & 255) 161111768Sgad 16259004Shmint 16359004Shmmain(int argc, char **argv) 16413244Sgraichen{ 16559003Shm struct conf_entry *p, *q; 16625443Sache 167111781Sgad parse_args(argc, argv); 168111773Sgad argc -= optind; 169111773Sgad argv += optind; 170111773Sgad 17159003Shm if (needroot && getuid() && geteuid()) 17259003Shm errx(1, "must have root privs"); 173111773Sgad p = q = get_worklist(argv); 17459003Shm 17559003Shm while (p) { 176112020Sgad do_entry(p); 17759003Shm p = p->next; 178111388Sgad free_entry(q); 17959003Shm q = p; 18059003Shm } 18195999Smaxim while (wait(NULL) > 0 || errno == EINTR) 18295999Smaxim ; 18359003Shm return (0); 18413244Sgraichen} 18513244Sgraichen 186111388Sgadstatic struct conf_entry * 187111388Sgadinit_entry(const char *fname, struct conf_entry *src_entry) 188111388Sgad{ 189111388Sgad struct conf_entry *tempwork; 190111388Sgad 191111388Sgad if (verbose > 4) 192111388Sgad printf("\t--> [creating entry for %s]\n", fname); 193111388Sgad 194111388Sgad tempwork = malloc(sizeof(struct conf_entry)); 195111388Sgad if (tempwork == NULL) 196111388Sgad err(1, "malloc of conf_entry for %s", fname); 197111388Sgad 198111388Sgad tempwork->log = strdup(fname); 199111388Sgad if (tempwork->log == NULL) 200111388Sgad err(1, "strdup for %s", fname); 201111388Sgad 202111388Sgad if (src_entry != NULL) { 203111388Sgad tempwork->pid_file = NULL; 204111388Sgad if (src_entry->pid_file) 205111388Sgad tempwork->pid_file = strdup(src_entry->pid_file); 206111772Sgad tempwork->r_reason = NULL; 207114137Sgad tempwork->firstcreate = 0; 208111772Sgad tempwork->rotate = 0; 209111388Sgad tempwork->uid = src_entry->uid; 210111388Sgad tempwork->gid = src_entry->gid; 211111388Sgad tempwork->numlogs = src_entry->numlogs; 212111388Sgad tempwork->size = src_entry->size; 213111388Sgad tempwork->hours = src_entry->hours; 214111388Sgad tempwork->trim_at = src_entry->trim_at; 215111388Sgad tempwork->permissions = src_entry->permissions; 216111388Sgad tempwork->flags = src_entry->flags; 217111388Sgad tempwork->sig = src_entry->sig; 218111388Sgad tempwork->def_cfg = src_entry->def_cfg; 219111388Sgad } else { 220111388Sgad /* Initialize as a "do-nothing" entry */ 221111388Sgad tempwork->pid_file = NULL; 222111772Sgad tempwork->r_reason = NULL; 223114137Sgad tempwork->firstcreate = 0; 224111772Sgad tempwork->rotate = 0; 225111779Sgad tempwork->uid = (uid_t)-1; 226111779Sgad tempwork->gid = (gid_t)-1; 227111388Sgad tempwork->numlogs = 1; 228111388Sgad tempwork->size = -1; 229111388Sgad tempwork->hours = -1; 230111388Sgad tempwork->trim_at = (time_t)0; 231111388Sgad tempwork->permissions = 0; 232111388Sgad tempwork->flags = 0; 233111388Sgad tempwork->sig = SIGHUP; 234111388Sgad tempwork->def_cfg = 0; 235111388Sgad } 236111388Sgad tempwork->next = NULL; 237111388Sgad 238111388Sgad return (tempwork); 239111388Sgad} 240111388Sgad 24159004Shmstatic void 242111388Sgadfree_entry(struct conf_entry *ent) 243111388Sgad{ 244111388Sgad 245111388Sgad if (ent == NULL) 246111388Sgad return; 247111388Sgad 248111388Sgad if (ent->log != NULL) { 249111388Sgad if (verbose > 4) 250111388Sgad printf("\t--> [freeing entry for %s]\n", ent->log); 251111388Sgad free(ent->log); 252111388Sgad ent->log = NULL; 253111388Sgad } 254111388Sgad 255111388Sgad if (ent->pid_file != NULL) { 256111388Sgad free(ent->pid_file); 257111388Sgad ent->pid_file = NULL; 258111388Sgad } 259111388Sgad 260111772Sgad if (ent->r_reason != NULL) { 261111772Sgad free(ent->r_reason); 262111772Sgad ent->r_reason = NULL; 263111772Sgad } 264111772Sgad 265111388Sgad free(ent); 266111388Sgad} 267111388Sgad 268111388Sgadstatic void 269112020Sgadfree_clist(struct conf_entry **firstent) 270112020Sgad{ 271112020Sgad struct conf_entry *ent, *nextent; 272112020Sgad 273112020Sgad if (firstent == NULL) 274112020Sgad return; /* There is nothing to do. */ 275112020Sgad 276112020Sgad ent = *firstent; 277112020Sgad firstent = NULL; 278112020Sgad 279112020Sgad while (ent) { 280112020Sgad nextent = ent->next; 281112020Sgad free_entry(ent); 282112020Sgad ent = nextent; 283112020Sgad } 284112020Sgad} 285112020Sgad 286112020Sgadstatic void 28759004Shmdo_entry(struct conf_entry * ent) 28813244Sgraichen{ 289111772Sgad#define REASON_MAX 80 29059003Shm int size, modtime; 291111772Sgad char temp_reason[REASON_MAX]; 29259003Shm 29359003Shm if (verbose) { 29459003Shm if (ent->flags & CE_COMPACT) 29559003Shm printf("%s <%dZ>: ", ent->log, ent->numlogs); 29680638Sobrien else if (ent->flags & CE_BZCOMPACT) 29780638Sobrien printf("%s <%dJ>: ", ent->log, ent->numlogs); 29859003Shm else 29959003Shm printf("%s <%d>: ", ent->log, ent->numlogs); 30059003Shm } 30159003Shm size = sizefile(ent->log); 30259003Shm modtime = age_old_log(ent->log); 303111772Sgad ent->rotate = 0; 304114137Sgad ent->firstcreate = 0; 30559003Shm if (size < 0) { 306114137Sgad /* 307114137Sgad * If either the C flag or the -C option was specified, 308114137Sgad * and if we won't be creating the file, then have the 309114137Sgad * verbose message include a hint as to why the file 310114137Sgad * will not be created. 311114137Sgad */ 312114137Sgad temp_reason[0] = '\0'; 313114137Sgad if (createlogs > 1) 314114137Sgad ent->firstcreate = 1; 315114137Sgad else if ((ent->flags & CE_CREATE) && createlogs) 316114137Sgad ent->firstcreate = 1; 317114137Sgad else if (ent->flags & CE_CREATE) 318114137Sgad strncpy(temp_reason, " (no -C option)", REASON_MAX); 319114137Sgad else if (createlogs) 320114137Sgad strncpy(temp_reason, " (no C flag)", REASON_MAX); 321114137Sgad 322114137Sgad if (ent->firstcreate) { 323114137Sgad if (verbose) 324114137Sgad printf("does not exist -> will create.\n"); 325114137Sgad createlog(ent); 326114137Sgad } else if (verbose) { 327114137Sgad printf("does not exist, skipped%s.\n", temp_reason); 328114137Sgad } 32959003Shm } else { 330111772Sgad if (ent->flags & CE_TRIMAT && !force && !rotatereq) { 33143071Swollman if (timenow < ent->trim_at 33259003Shm || difftime(timenow, ent->trim_at) >= 60 * 60) { 33343071Swollman if (verbose) 33443071Swollman printf("--> will trim at %s", 33559003Shm ctime(&ent->trim_at)); 33643071Swollman return; 33743071Swollman } else if (verbose && ent->hours <= 0) { 33843071Swollman printf("--> time is up\n"); 33943071Swollman } 34043071Swollman } 34159003Shm if (verbose && (ent->size > 0)) 34259003Shm printf("size (Kb): %d [%d] ", size, ent->size); 34359003Shm if (verbose && (ent->hours > 0)) 34459003Shm printf(" age (hr): %d [%d] ", modtime, ent->hours); 345111772Sgad 346111772Sgad /* 347111772Sgad * Figure out if this logfile needs to be rotated. 348111772Sgad */ 349111772Sgad temp_reason[0] = '\0'; 350111772Sgad if (rotatereq) { 351111772Sgad ent->rotate = 1; 352111772Sgad snprintf(temp_reason, REASON_MAX, " due to -R from %s", 353111772Sgad requestor); 354111772Sgad } else if (force) { 355111772Sgad ent->rotate = 1; 356111772Sgad snprintf(temp_reason, REASON_MAX, " due to -F request"); 357111772Sgad } else if ((ent->size > 0) && (size >= ent->size)) { 358111772Sgad ent->rotate = 1; 359111772Sgad snprintf(temp_reason, REASON_MAX, " due to size>%dK", 360111772Sgad ent->size); 361111772Sgad } else if (ent->hours <= 0 && (ent->flags & CE_TRIMAT)) { 362111772Sgad ent->rotate = 1; 363111772Sgad } else if ((ent->hours > 0) && ((modtime >= ent->hours) || 364111772Sgad (modtime < 0))) { 365111772Sgad ent->rotate = 1; 366111772Sgad } 367111772Sgad 368111772Sgad /* 369111772Sgad * If the file needs to be rotated, then rotate it. 370111772Sgad */ 371111772Sgad if (ent->rotate) { 372111772Sgad if (temp_reason[0] != '\0') 373111772Sgad ent->r_reason = strdup(temp_reason); 37459003Shm if (verbose) 37559003Shm printf("--> trimming log....\n"); 37659003Shm if (noaction && !verbose) { 37759003Shm if (ent->flags & CE_COMPACT) 37859003Shm printf("%s <%dZ>: trimming\n", 37959003Shm ent->log, ent->numlogs); 38080638Sobrien else if (ent->flags & CE_BZCOMPACT) 38180638Sobrien printf("%s <%dJ>: trimming\n", 38280638Sobrien ent->log, ent->numlogs); 38359003Shm else 38459003Shm printf("%s <%d>: trimming\n", 38559003Shm ent->log, ent->numlogs); 38659003Shm } 387111780Sgad dotrim(ent, ent->log, ent->numlogs, ent->flags); 38859003Shm } else { 38959003Shm if (verbose) 39059003Shm printf("--> skipping\n"); 39159003Shm } 39259003Shm } 393111772Sgad#undef REASON_MAX 39413244Sgraichen} 39513244Sgraichen 396112003Sgad/* Send a signal to the pid specified by pidfile */ 397112003Sgadstatic int 398112003Sgadsend_signal(const struct conf_entry *ent) 399112003Sgad{ 400112003Sgad pid_t target_pid; 401112003Sgad int did_notify; 402112003Sgad FILE *f; 403112003Sgad long minok, maxok, rval; 404112003Sgad const char *target_name; 405112003Sgad char *endp, *linep, line[BUFSIZ]; 406112003Sgad 407112003Sgad did_notify = 0; 408112003Sgad f = fopen(ent->pid_file, "r"); 409112003Sgad if (f == NULL) { 410112003Sgad warn("can't open pid file: %s", ent->pid_file); 411112003Sgad return (did_notify); 412112003Sgad /* NOTREACHED */ 413112003Sgad } 414112003Sgad 415112003Sgad if (fgets(line, BUFSIZ, f) == NULL) { 416112003Sgad /* 417112003Sgad * XXX - If the pid file is empty, is that really a 418112003Sgad * problem? Wouldn't that mean that the process 419112003Sgad * has shut down? In that case there would be no 420112003Sgad * problem with compressing the rotated log file. 421112003Sgad */ 422112003Sgad if (feof(f)) 423112003Sgad warnx("pid file is empty: %s", ent->pid_file); 424112003Sgad else 425112003Sgad warn("can't read from pid file: %s", ent->pid_file); 426112003Sgad (void) fclose(f); 427112003Sgad return (did_notify); 428112003Sgad /* NOTREACHED */ 429112003Sgad } 430112003Sgad (void) fclose(f); 431112003Sgad 432112003Sgad target_name = "daemon"; 433112003Sgad minok = MIN_PID; 434112003Sgad maxok = MAX_PID; 435112003Sgad if (ent->flags & CE_SIGNALGROUP) { 436112003Sgad /* 437112003Sgad * If we are expected to signal a process-group when 438112003Sgad * rotating this logfile, then the value read in should 439112003Sgad * be the negative of a valid process ID. 440112003Sgad */ 441112003Sgad target_name = "process-group"; 442112003Sgad minok = -MAX_PID; 443112003Sgad maxok = -MIN_PID; 444112003Sgad } 445112003Sgad 446112003Sgad errno = 0; 447112003Sgad linep = line; 448112003Sgad while (*linep == ' ') 449112003Sgad linep++; 450112003Sgad rval = strtol(linep, &endp, 10); 451112003Sgad if (*endp != '\0' && !isspacech(*endp)) { 452112003Sgad warnx("pid file does not start with a valid number: %s", 453112003Sgad ent->pid_file); 454112003Sgad rval = 0; 455112003Sgad } else if (rval < minok || rval > maxok) { 456112003Sgad warnx("bad value '%ld' for process number in %s", 457112003Sgad rval, ent->pid_file); 458112003Sgad if (verbose) 459112003Sgad warnx("\t(expecting value between %ld and %ld)", 460112003Sgad minok, maxok); 461112003Sgad rval = 0; 462112003Sgad } 463112003Sgad if (rval == 0) { 464112003Sgad return (did_notify); 465112003Sgad /* NOTREACHED */ 466112003Sgad } 467112003Sgad 468112003Sgad target_pid = rval; 469112003Sgad 470112003Sgad if (noaction) { 471112003Sgad did_notify = 1; 472112003Sgad printf("\tkill -%d %d\n", ent->sig, (int) target_pid); 473112003Sgad } else if (kill(target_pid, ent->sig)) { 474112003Sgad /* 475112003Sgad * XXX - Iff the error was "no such process", should that 476112003Sgad * really be an error for us? Perhaps the process 477112003Sgad * is already gone, in which case there would be no 478112003Sgad * problem with compressing the rotated log file. 479112003Sgad */ 480112003Sgad warn("can't notify %s, pid %d", target_name, 481112003Sgad (int) target_pid); 482112003Sgad } else { 483112003Sgad did_notify = 1; 484112003Sgad if (verbose) 485112003Sgad printf("%s pid %d notified\n", target_name, 486112003Sgad (int) target_pid); 487112003Sgad } 488112003Sgad 489112003Sgad return (did_notify); 490112003Sgad} 491112003Sgad 49259004Shmstatic void 493111781Sgadparse_args(int argc, char **argv) 49413244Sgraichen{ 495111781Sgad int ch; 49659003Shm char *p; 49713244Sgraichen 498111781Sgad timenow = time(NULL); 499108164Strhodes (void)strncpy(daytime, ctime(&timenow) + 4, 15); 50059003Shm daytime[15] = '\0'; 50113244Sgraichen 50259003Shm /* Let's get our hostname */ 503111781Sgad (void)gethostname(hostname, sizeof(hostname)); 50413244Sgraichen 50513244Sgraichen /* Truncate domain */ 506111781Sgad if ((p = strchr(hostname, '.')) != NULL) 50713244Sgraichen *p = '\0'; 508111768Sgad 509111768Sgad /* Parse command line options. */ 510114137Sgad while ((ch = getopt(argc, argv, "a:f:nrsvCFR:")) != -1) 511111781Sgad switch (ch) { 51259004Shm case 'a': 51359004Shm archtodir++; 51459004Shm archdirname = optarg; 51559004Shm break; 516111768Sgad case 'f': 517111768Sgad conf = optarg; 518111768Sgad break; 519111768Sgad case 'n': 520111768Sgad noaction++; 521111768Sgad break; 52259003Shm case 'r': 52359003Shm needroot = 0; 52459003Shm break; 525111768Sgad case 's': 526111768Sgad nosignal = 1; 527111768Sgad break; 52859003Shm case 'v': 52959003Shm verbose++; 53059003Shm break; 531114137Sgad case 'C': 532114137Sgad /* Useful for things like rc.diskless... */ 533114137Sgad createlogs++; 534114137Sgad break; 53534584Spst case 'F': 53634584Spst force++; 53734584Spst break; 538111772Sgad case 'R': 539111772Sgad rotatereq++; 540111772Sgad requestor = strdup(optarg); 541111772Sgad break; 542111781Sgad case 'm': /* Used by OpenBSD for "monitor mode" */ 54359003Shm default: 54459003Shm usage(); 545111768Sgad /* NOTREACHED */ 54659003Shm } 547111772Sgad 548111772Sgad if (rotatereq) { 549111772Sgad if (optind == argc) { 550111772Sgad warnx("At least one filename must be given when -R is specified."); 551111772Sgad usage(); 552111772Sgad /* NOTREACHED */ 553111772Sgad } 554111772Sgad /* Make sure "requestor" value is safe for a syslog message. */ 555111772Sgad for (p = requestor; *p != '\0'; p++) { 556111772Sgad if (!isprintch(*p) && (*p != '\t')) 557111772Sgad *p = '.'; 558111772Sgad } 559111772Sgad } 56043071Swollman} 56113244Sgraichen 56259004Shmstatic void 56359004Shmusage(void) 56413244Sgraichen{ 56580646Sobrien 56680646Sobrien fprintf(stderr, 567114137Sgad "usage: newsyslog [-CFnrsv] [-a directory] [-f config-file]\n" 568111772Sgad " [ [-R requestor] filename ... ]\n"); 56959003Shm exit(1); 57013244Sgraichen} 57113244Sgraichen 57259004Shm/* 573111773Sgad * Parse a configuration file and return a linked list of all the logs 574111773Sgad * which should be processed. 57513244Sgraichen */ 57659003Shmstatic struct conf_entry * 577111773Sgadget_worklist(char **files) 57813244Sgraichen{ 57959003Shm FILE *f; 580111773Sgad const char *fname; 581111773Sgad char **given; 582111773Sgad struct conf_entry *defconf, *dupent, *ent, *firstnew; 583112020Sgad struct conf_entry *globlist, *lastnew, *worklist; 584112020Sgad int gmatch, fnres; 585111773Sgad 586112020Sgad defconf = globlist = worklist = NULL; 587111773Sgad 588111773Sgad fname = conf; 589111773Sgad if (fname == NULL) 590111773Sgad fname = _PATH_CONF; 591111773Sgad 592111773Sgad if (strcmp(fname, "-") != 0) 593111773Sgad f = fopen(fname, "r"); 594111773Sgad else { 595111773Sgad f = stdin; 596111773Sgad fname = "<stdin>"; 597111773Sgad } 598111773Sgad if (!f) 599111773Sgad err(1, "%s", conf); 600111773Sgad 601112020Sgad parse_file(f, fname, &worklist, &globlist, &defconf); 602111773Sgad (void) fclose(f); 603111773Sgad 604111773Sgad /* 605111773Sgad * All config-file information has been read in and turned into 606112020Sgad * a worklist and a globlist. If there were no specific files 607112020Sgad * given on the run command, then the only thing left to do is to 608112020Sgad * call a routine which finds all files matched by the globlist 609112020Sgad * and adds them to the worklist. Then return the worklist. 610111773Sgad */ 611111773Sgad if (*files == NULL) { 612112020Sgad expand_globs(&worklist, &globlist); 613112020Sgad free_clist(&globlist); 614111773Sgad if (defconf != NULL) 615111773Sgad free_entry(defconf); 616111773Sgad return (worklist); 617111773Sgad /* NOTREACHED */ 618111773Sgad } 619111773Sgad 620111773Sgad /* 621111773Sgad * If newsyslog was given a specific list of files to process, 622111773Sgad * it may be that some of those files were not listed in any 623111773Sgad * config file. Those unlisted files should get the default 624111773Sgad * rotation action. First, create the default-rotation action 625111773Sgad * if none was found in a system config file. 626111773Sgad */ 627111773Sgad if (defconf == NULL) { 628111773Sgad defconf = init_entry(DEFAULT_MARKER, NULL); 629111773Sgad defconf->numlogs = 3; 630111773Sgad defconf->size = 50; 631111773Sgad defconf->permissions = S_IRUSR|S_IWUSR; 632111773Sgad } 633111773Sgad 634111773Sgad /* 635111773Sgad * If newsyslog was run with a list of specific filenames, 636111773Sgad * then create a new worklist which has only those files in 637111773Sgad * it, picking up the rotation-rules for those files from 638111773Sgad * the original worklist. 639111773Sgad * 640111773Sgad * XXX - Note that this will copy multiple rules for a single 641111773Sgad * logfile, if multiple entries are an exact match for 642111773Sgad * that file. That matches the historic behavior, but do 643111773Sgad * we want to continue to allow it? If so, it should 644111773Sgad * probably be handled more intelligently. 645111773Sgad */ 646112020Sgad firstnew = lastnew = NULL; 647111773Sgad for (given = files; *given; ++given) { 648111773Sgad /* 649111773Sgad * First try to find exact-matches for this given file. 650111773Sgad */ 651112020Sgad gmatch = 0; 652111773Sgad for (ent = worklist; ent; ent = ent->next) { 653111773Sgad if (strcmp(ent->log, *given) == 0) { 654111773Sgad gmatch++; 655111773Sgad dupent = init_entry(*given, ent); 656111773Sgad if (!firstnew) 657111773Sgad firstnew = dupent; 658111773Sgad else 659112020Sgad lastnew->next = dupent; 660112020Sgad lastnew = dupent; 661111773Sgad } 662111773Sgad } 663111773Sgad if (gmatch) { 664111773Sgad if (verbose > 2) 665111773Sgad printf("\t+ Matched entry %s\n", *given); 666111773Sgad continue; 667111773Sgad } 668111773Sgad 669111773Sgad /* 670111773Sgad * There was no exact-match for this given file, so look 671111773Sgad * for a "glob" entry which does match. 672111773Sgad */ 673112020Sgad gmatch = 0; 674112020Sgad if (verbose > 2 && globlist != NULL) 675112020Sgad printf("\t+ Checking globs for %s\n", *given); 676112020Sgad for (ent = globlist; ent; ent = ent->next) { 677112020Sgad fnres = fnmatch(ent->log, *given, FNM_PATHNAME); 678112020Sgad if (verbose > 2) 679112020Sgad printf("\t+ = %d for pattern %s\n", fnres, 680112020Sgad ent->log); 681112020Sgad if (fnres == 0) { 682111773Sgad gmatch++; 683111773Sgad dupent = init_entry(*given, ent); 684111773Sgad if (!firstnew) 685111773Sgad firstnew = dupent; 686111773Sgad else 687112020Sgad lastnew->next = dupent; 688112020Sgad lastnew = dupent; 689112020Sgad /* This new entry is not a glob! */ 690111773Sgad dupent->flags &= ~CE_GLOB; 691111773Sgad /* Only allow a match to one glob-entry */ 692111773Sgad break; 693111773Sgad } 694111773Sgad } 695111773Sgad if (gmatch) { 696111773Sgad if (verbose > 2) 697111773Sgad printf("\t+ Matched %s via %s\n", *given, 698111773Sgad ent->log); 699111773Sgad continue; 700111773Sgad } 701111773Sgad 702111773Sgad /* 703111773Sgad * This given file was not found in any config file, so 704111773Sgad * add a worklist item based on the default entry. 705111773Sgad */ 706111773Sgad if (verbose > 2) 707111773Sgad printf("\t+ No entry matched %s (will use %s)\n", 708111773Sgad *given, DEFAULT_MARKER); 709111773Sgad dupent = init_entry(*given, defconf); 710111773Sgad if (!firstnew) 711111773Sgad firstnew = dupent; 712111773Sgad else 713112020Sgad lastnew->next = dupent; 714111773Sgad /* Mark that it was *not* found in a config file */ 715111773Sgad dupent->def_cfg = 1; 716112020Sgad lastnew = dupent; 717111773Sgad } 718111773Sgad 719111773Sgad /* 720112020Sgad * Free all the entries in the original work list, the list of 721112020Sgad * glob entries, and the default entry. 722111773Sgad */ 723112020Sgad free_clist(&worklist); 724112020Sgad free_clist(&globlist); 725112020Sgad free_entry(defconf); 726111773Sgad 727112020Sgad /* And finally, return a worklist which matches the given files. */ 728112013Sgad return (firstnew); 729111773Sgad} 730111773Sgad 731111773Sgad/* 732112020Sgad * Expand the list of entries with filename patterns, and add all files 733112020Sgad * which match those glob-entries onto the worklist. 734112020Sgad */ 735112020Sgadstatic void 736112020Sgadexpand_globs(struct conf_entry **work_p, struct conf_entry **glob_p) 737112020Sgad{ 738112020Sgad int gmatch, gres, i; 739112020Sgad char *mfname; 740112020Sgad struct conf_entry *dupent, *ent, *firstmatch, *globent; 741112020Sgad struct conf_entry *lastmatch; 742112020Sgad glob_t pglob; 743112020Sgad struct stat st_fm; 744112020Sgad 745112020Sgad if ((glob_p == NULL) || (*glob_p == NULL)) 746112020Sgad return; /* There is nothing to do. */ 747112020Sgad 748112020Sgad /* 749112020Sgad * The worklist contains all fully-specified (non-GLOB) names. 750112020Sgad * 751112020Sgad * Now expand the list of filename-pattern (GLOB) entries into 752112020Sgad * a second list, which (by definition) will only match files 753112020Sgad * that already exist. Do not add a glob-related entry for any 754112020Sgad * file which already exists in the fully-specified list. 755112020Sgad */ 756112020Sgad firstmatch = lastmatch = NULL; 757112020Sgad for (globent = *glob_p; globent; globent = globent->next) { 758112020Sgad 759112020Sgad gres = glob(globent->log, GLOB_NOCHECK, NULL, &pglob); 760112020Sgad if (gres != 0) { 761112020Sgad warn("cannot expand pattern (%d): %s", gres, 762112020Sgad globent->log); 763112020Sgad continue; 764112020Sgad } 765112020Sgad 766112020Sgad if (verbose > 2) 767112020Sgad printf("\t+ Expanding pattern %s\n", globent->log); 768112020Sgad for (i = 0; i < pglob.gl_matchc; i++) { 769112020Sgad mfname = pglob.gl_pathv[i]; 770112020Sgad 771112020Sgad /* See if this file already has a specific entry. */ 772112020Sgad gmatch = 0; 773112020Sgad for (ent = *work_p; ent; ent = ent->next) { 774112020Sgad if (strcmp(mfname, ent->log) == 0) { 775112020Sgad gmatch++; 776112020Sgad break; 777112020Sgad } 778112020Sgad } 779112020Sgad if (gmatch) 780112020Sgad continue; 781112020Sgad 782112020Sgad /* Make sure the named matched is a file. */ 783112020Sgad gres = lstat(mfname, &st_fm); 784112020Sgad if (gres != 0) { 785112020Sgad /* Error on a file that glob() matched?!? */ 786112020Sgad warn("Skipping %s - lstat() error", mfname); 787112020Sgad continue; 788112020Sgad } 789112020Sgad if (!S_ISREG(st_fm.st_mode)) { 790112020Sgad /* We only rotate files! */ 791112020Sgad if (verbose > 2) 792112020Sgad printf("\t+ . skipping %s (!file)\n", 793112020Sgad mfname); 794112020Sgad continue; 795112020Sgad } 796112020Sgad 797112020Sgad if (verbose > 2) 798112020Sgad printf("\t+ . add file %s\n", mfname); 799112020Sgad dupent = init_entry(mfname, globent); 800112020Sgad if (!firstmatch) 801112020Sgad firstmatch = dupent; 802112020Sgad else 803112020Sgad lastmatch->next = dupent; 804112020Sgad lastmatch = dupent; 805112020Sgad /* This new entry is not a glob! */ 806112020Sgad dupent->flags &= ~CE_GLOB; 807112020Sgad } 808112020Sgad globfree(&pglob); 809112020Sgad if (verbose > 2) 810112020Sgad printf("\t+ Done with pattern %s\n", globent->log); 811112020Sgad } 812112020Sgad 813112020Sgad /* Add the list of matched files to the end of the worklist. */ 814112020Sgad if (!*work_p) 815112020Sgad *work_p = firstmatch; 816112020Sgad else { 817112020Sgad ent = *work_p; 818112020Sgad while (ent->next) 819112020Sgad ent = ent->next; 820112020Sgad ent->next = firstmatch; 821112020Sgad } 822112020Sgad 823112020Sgad} 824112020Sgad 825112020Sgad/* 826111773Sgad * Parse a configuration file and update a linked list of all the logs to 827111773Sgad * process. 828111773Sgad */ 829111773Sgadstatic void 830111773Sgadparse_file(FILE *cf, const char *cfname, struct conf_entry **work_p, 831112020Sgad struct conf_entry **glob_p, struct conf_entry **defconf_p) 832111773Sgad{ 83359003Shm char line[BUFSIZ], *parse, *q; 834107737Ssobomax char *cp, *errline, *group; 835112020Sgad struct conf_entry *lastglob, *lastwork, *working; 836111781Sgad struct passwd *pwd; 83759003Shm struct group *grp; 838112020Sgad int eol, special; 83913244Sgraichen 840111773Sgad /* 841111773Sgad * XXX - for now, assume that only one config file will be read, 842111773Sgad * ie, this routine is only called one time. 843111773Sgad */ 844112020Sgad lastglob = lastwork = NULL; 84580646Sobrien 846111773Sgad while (fgets(line, BUFSIZ, cf)) { 847107737Ssobomax if ((line[0] == '\n') || (line[0] == '#') || 848107737Ssobomax (strlen(line) == 0)) 84959003Shm continue; 85059003Shm errline = strdup(line); 851107737Ssobomax for (cp = line + 1; *cp != '\0'; cp++) { 852107737Ssobomax if (*cp != '#') 853107737Ssobomax continue; 854107737Ssobomax if (*(cp - 1) == '\\') { 855107737Ssobomax strcpy(cp - 1, cp); 856107737Ssobomax cp--; 857107737Ssobomax continue; 858107737Ssobomax } 859107737Ssobomax *cp = '\0'; 860107737Ssobomax break; 861107737Ssobomax } 86260373Sdes 86360373Sdes q = parse = missing_field(sob(line), errline); 86460373Sdes parse = son(line); 86560373Sdes if (!*parse) 86680646Sobrien errx(1, "malformed line (missing fields):\n%s", 86780646Sobrien errline); 86860373Sdes *parse = '\0'; 86960373Sdes 870112020Sgad special = 0; 871111388Sgad working = init_entry(q, NULL); 872111388Sgad if (strcasecmp(DEFAULT_MARKER, q) == 0) { 873112020Sgad special = 1; 874111773Sgad if (defconf_p == NULL) { 875111773Sgad warnx("Ignoring entry for %s in %s!", q, 876111773Sgad cfname); 877111773Sgad free_entry(working); 878111773Sgad continue; 879111773Sgad } else if (*defconf_p != NULL) { 880111388Sgad warnx("Ignoring duplicate entry for %s!", q); 881111388Sgad free_entry(working); 882111388Sgad continue; 883111388Sgad } 884111773Sgad *defconf_p = working; 88559003Shm } 88613244Sgraichen 88759003Shm q = parse = missing_field(sob(++parse), errline); 88859003Shm parse = son(parse); 88925518Sbrian if (!*parse) 89080646Sobrien errx(1, "malformed line (missing fields):\n%s", 89180646Sobrien errline); 89259003Shm *parse = '\0'; 89359003Shm if ((group = strchr(q, ':')) != NULL || 89459003Shm (group = strrchr(q, '.')) != NULL) { 89559003Shm *group++ = '\0'; 89659003Shm if (*q) { 89759003Shm if (!(isnumber(*q))) { 898111781Sgad if ((pwd = getpwnam(q)) == NULL) 89959003Shm errx(1, 90080646Sobrien "error in config file; unknown user:\n%s", 90159003Shm errline); 902111781Sgad working->uid = pwd->pw_uid; 90359003Shm } else 90459003Shm working->uid = atoi(q); 90559003Shm } else 906111779Sgad working->uid = (uid_t)-1; 90713244Sgraichen 90859003Shm q = group; 90959003Shm if (*q) { 91059003Shm if (!(isnumber(*q))) { 91159003Shm if ((grp = getgrnam(q)) == NULL) 91259003Shm errx(1, 91380646Sobrien "error in config file; unknown group:\n%s", 91459003Shm errline); 91559003Shm working->gid = grp->gr_gid; 91659003Shm } else 91759003Shm working->gid = atoi(q); 91859003Shm } else 919111779Sgad working->gid = (gid_t)-1; 92013244Sgraichen 92159003Shm q = parse = missing_field(sob(++parse), errline); 92259003Shm parse = son(parse); 92359003Shm if (!*parse) 92480646Sobrien errx(1, "malformed line (missing fields):\n%s", 92580646Sobrien errline); 92659003Shm *parse = '\0'; 927111779Sgad } else { 928111779Sgad working->uid = (uid_t)-1; 929111779Sgad working->gid = (gid_t)-1; 930111779Sgad } 93159003Shm 93259003Shm if (!sscanf(q, "%o", &working->permissions)) 93359003Shm errx(1, "error in config file; bad permissions:\n%s", 93459003Shm errline); 93559003Shm 93659003Shm q = parse = missing_field(sob(++parse), errline); 93759003Shm parse = son(parse); 93825518Sbrian if (!*parse) 93980646Sobrien errx(1, "malformed line (missing fields):\n%s", 94080646Sobrien errline); 94159003Shm *parse = '\0'; 942111400Sgad if (!sscanf(q, "%d", &working->numlogs) || working->numlogs < 0) 943111400Sgad errx(1, "error in config file; bad value for count of logs to save:\n%s", 94459003Shm errline); 94513244Sgraichen 94659003Shm q = parse = missing_field(sob(++parse), errline); 94759003Shm parse = son(parse); 94825518Sbrian if (!*parse) 94980646Sobrien errx(1, "malformed line (missing fields):\n%s", 95080646Sobrien errline); 95159003Shm *parse = '\0'; 95259003Shm if (isdigit(*q)) 95359003Shm working->size = atoi(q); 95459003Shm else 95559003Shm working->size = -1; 95659003Shm 95759003Shm working->flags = 0; 95859003Shm q = parse = missing_field(sob(++parse), errline); 95959003Shm parse = son(parse); 96025518Sbrian eol = !*parse; 96159003Shm *parse = '\0'; 96243071Swollman { 96359003Shm char *ep; 96459003Shm u_long ul; 96513244Sgraichen 96643071Swollman ul = strtoul(q, &ep, 10); 96743071Swollman if (ep == q) 96843071Swollman working->hours = 0; 96943071Swollman else if (*ep == '*') 97043071Swollman working->hours = -1; 97143071Swollman else if (ul > INT_MAX) 97243071Swollman errx(1, "interval is too large:\n%s", errline); 97343071Swollman else 97443071Swollman working->hours = ul; 97543071Swollman 97680646Sobrien if (*ep != '\0' && *ep != '@' && *ep != '*' && 97780646Sobrien *ep != '$') 97843071Swollman errx(1, "malformed interval/at:\n%s", errline); 97943071Swollman if (*ep == '@') { 98093659Scjc if ((working->trim_at = parse8601(ep + 1, errline)) 98159003Shm == (time_t) - 1) 98243071Swollman errx(1, "malformed at:\n%s", errline); 98343071Swollman working->flags |= CE_TRIMAT; 98459004Shm } else if (*ep == '$') { 98593659Scjc if ((working->trim_at = parseDWM(ep + 1, errline)) 98659004Shm == (time_t) - 1) 98759004Shm errx(1, "malformed at:\n%s", errline); 98859004Shm working->flags |= CE_TRIMAT; 98943071Swollman } 99043071Swollman } 99143071Swollman 99225518Sbrian if (eol) 99359003Shm q = NULL; 99425518Sbrian else { 99559003Shm q = parse = sob(++parse); /* Optional field */ 99659003Shm parse = son(parse); 99759003Shm if (!*parse) 99859003Shm eol = 1; 99959003Shm *parse = '\0'; 100025518Sbrian } 100125443Sache 1002111768Sgad for (; q && *q && !isspacech(*q); q++) { 1003111768Sgad switch (tolowerch(*q)) { 1004111768Sgad case 'b': 100559003Shm working->flags |= CE_BINARY; 1006111768Sgad break; 1007114137Sgad case 'c': 1008111768Sgad /* 1009114137Sgad * XXX - Ick! Ugly! Remove ASAP! 1010114137Sgad * We want `c' and `C' for "create". But we 1011114137Sgad * will temporarily treat `c' as `g', because 1012114137Sgad * FreeBSD releases <= 4.8 have a typo of 1013114137Sgad * checking ('G' || 'c') for CE_GLOB. 1014111768Sgad */ 1015114137Sgad if (*q == 'c') { 1016114137Sgad warnx("Assuming 'g' for 'c' in flags for line:\n%s", 1017114137Sgad errline); 1018114137Sgad warnx("The 'c' flag will eventually mean 'CREATE'"); 1019114137Sgad working->flags |= CE_GLOB; 1020114137Sgad break; 1021114137Sgad } 1022114137Sgad working->flags |= CE_CREATE; 1023114137Sgad break; 1024111768Sgad case 'g': 1025106905Ssobomax working->flags |= CE_GLOB; 1026111768Sgad break; 1027111768Sgad case 'j': 1028111768Sgad working->flags |= CE_BZCOMPACT; 1029111768Sgad break; 1030111768Sgad case 'n': 1031111768Sgad working->flags |= CE_NOSIGNAL; 1032111768Sgad break; 1033112003Sgad case 'u': 1034112003Sgad working->flags |= CE_SIGNALGROUP; 1035112003Sgad break; 1036111768Sgad case 'w': 1037107916Ssobomax working->flags |= CE_COMPACTWAIT; 1038111768Sgad break; 1039111768Sgad case 'z': 1040111768Sgad working->flags |= CE_COMPACT; 1041111768Sgad break; 1042111768Sgad case '-': 1043111768Sgad break; 1044111781Sgad case 'f': /* Used by OpenBSD for "CE_FOLLOW" */ 1045111781Sgad case 'm': /* Used by OpenBSD for "CE_MONITOR" */ 1046111781Sgad case 'p': /* Used by NetBSD for "CE_PLAIN0" */ 1047111768Sgad default: 104880646Sobrien errx(1, "illegal flag in config file -- %c", 104980646Sobrien *q); 1050111768Sgad } 105159003Shm } 105259003Shm 105325518Sbrian if (eol) 105459003Shm q = NULL; 105525518Sbrian else { 105659003Shm q = parse = sob(++parse); /* Optional field */ 105759003Shm parse = son(parse); 105859003Shm if (!*parse) 105959003Shm eol = 1; 106059003Shm *parse = '\0'; 106125518Sbrian } 106225443Sache 106325443Sache working->pid_file = NULL; 106425443Sache if (q && *q) { 106525443Sache if (*q == '/') 106625443Sache working->pid_file = strdup(q); 106736817Sache else if (isdigit(*q)) 106836817Sache goto got_sig; 106959003Shm else 107080646Sobrien errx(1, 107180646Sobrien "illegal pid file or signal number in config file:\n%s", 107280646Sobrien errline); 107325443Sache } 107436817Sache if (eol) 107559003Shm q = NULL; 107636817Sache else { 107759003Shm q = parse = sob(++parse); /* Optional field */ 107859003Shm *(parse = son(parse)) = '\0'; 107936817Sache } 108036817Sache 108136817Sache working->sig = SIGHUP; 108236817Sache if (q && *q) { 108336817Sache if (isdigit(*q)) { 108459003Shm got_sig: 108536817Sache working->sig = atoi(q); 108636817Sache } else { 108759003Shm err_sig: 108880646Sobrien errx(1, 108980646Sobrien "illegal signal number in config file:\n%s", 109080646Sobrien errline); 109136817Sache } 109236817Sache if (working->sig < 1 || working->sig >= NSIG) 109336817Sache goto err_sig; 109436817Sache } 1095111768Sgad 1096111768Sgad /* 1097111768Sgad * Finish figuring out what pid-file to use (if any) in 1098111768Sgad * later processing if this logfile needs to be rotated. 1099111768Sgad */ 1100111768Sgad if ((working->flags & CE_NOSIGNAL) == CE_NOSIGNAL) { 1101111768Sgad /* 1102111768Sgad * This config-entry specified 'n' for nosignal, 1103111768Sgad * see if it also specified an explicit pid_file. 1104111768Sgad * This would be a pretty pointless combination. 1105111768Sgad */ 1106111768Sgad if (working->pid_file != NULL) { 1107111768Sgad warnx("Ignoring '%s' because flag 'n' was specified in line:\n%s", 1108111768Sgad working->pid_file, errline); 1109111768Sgad free(working->pid_file); 1110111768Sgad working->pid_file = NULL; 1111111768Sgad } 1112111768Sgad } else if (working->pid_file == NULL) { 1113111768Sgad /* 1114111768Sgad * This entry did not specify the 'n' flag, which 1115111768Sgad * means it should signal syslogd unless it had 1116112003Sgad * specified some other pid-file (and obviously the 1117112003Sgad * syslog pid-file will not be for a process-group). 1118112003Sgad * Also, we should only try to notify syslog if we 1119112003Sgad * are root. 1120111768Sgad */ 1121112003Sgad if (working->flags & CE_SIGNALGROUP) { 1122112003Sgad warnx("Ignoring flag 'U' in line:\n%s", 1123112003Sgad errline); 1124112003Sgad working->flags &= ~CE_SIGNALGROUP; 1125112003Sgad } 1126111768Sgad if (needroot) 1127111768Sgad working->pid_file = strdup(_PATH_SYSLOGPID); 1128111768Sgad } 1129111768Sgad 1130112020Sgad /* 1131112020Sgad * Add this entry to the appropriate list of entries, unless 1132112020Sgad * it was some kind of special entry (eg: <default>). 1133112020Sgad */ 1134112020Sgad if (special) { 1135112020Sgad ; /* Do not add to any list */ 1136112020Sgad } else if (working->flags & CE_GLOB) { 1137112020Sgad if (!*glob_p) 1138112020Sgad *glob_p = working; 1139112020Sgad else 1140112020Sgad lastglob->next = working; 1141112020Sgad lastglob = working; 1142112020Sgad } else { 1143112020Sgad if (!*work_p) 1144112020Sgad *work_p = working; 1145112020Sgad else 1146112020Sgad lastwork->next = working; 1147112020Sgad lastwork = working; 1148112020Sgad } 1149112020Sgad 115059003Shm free(errline); 1151111768Sgad errline = NULL; 115259003Shm } 115313244Sgraichen} 115413244Sgraichen 115559003Shmstatic char * 115659004Shmmissing_field(char *p, char *errline) 115713244Sgraichen{ 115880646Sobrien 115959003Shm if (!p || !*p) 116059003Shm errx(1, "missing field in config file:\n%s", errline); 116159003Shm return (p); 116213244Sgraichen} 116313244Sgraichen 116459004Shmstatic void 1165111780Sgaddotrim(const struct conf_entry *ent, char *log, int numdays, int flags) 116613244Sgraichen{ 116771299Sjedgar char dirpart[MAXPATHLEN], namepart[MAXPATHLEN]; 116871299Sjedgar char file1[MAXPATHLEN], file2[MAXPATHLEN]; 116971299Sjedgar char zfile1[MAXPATHLEN], zfile2[MAXPATHLEN]; 117080638Sobrien char jfile1[MAXPATHLEN]; 117194352Ssheldonh char tfile[MAXPATHLEN]; 117259003Shm int notified, need_notification, fd, _numdays; 117359003Shm struct stat st; 117413244Sgraichen 117559004Shm if (archtodir) { 117659004Shm char *p; 117713244Sgraichen 117859004Shm /* build complete name of archive directory into dirpart */ 117959004Shm if (*archdirname == '/') { /* absolute */ 118071299Sjedgar strlcpy(dirpart, archdirname, sizeof(dirpart)); 118159004Shm } else { /* relative */ 118259004Shm /* get directory part of logfile */ 118371299Sjedgar strlcpy(dirpart, log, sizeof(dirpart)); 118459004Shm if ((p = rindex(dirpart, '/')) == NULL) 118559004Shm dirpart[0] = '\0'; 118659004Shm else 118759004Shm *(p + 1) = '\0'; 118871299Sjedgar strlcat(dirpart, archdirname, sizeof(dirpart)); 118959004Shm } 119059004Shm 119159004Shm /* check if archive directory exists, if not, create it */ 119259004Shm if (lstat(dirpart, &st)) 1193114137Sgad createdir(ent, dirpart); 119459004Shm 119559004Shm /* get filename part of logfile */ 119659004Shm if ((p = rindex(log, '/')) == NULL) 119771299Sjedgar strlcpy(namepart, log, sizeof(namepart)); 119859004Shm else 119971299Sjedgar strlcpy(namepart, p + 1, sizeof(namepart)); 120059004Shm 120159004Shm /* name of oldest log */ 120280646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart, 120380646Sobrien namepart, numdays); 120471299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 120571299Sjedgar COMPRESS_POSTFIX); 120680638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 120780638Sobrien BZCOMPRESS_POSTFIX); 120859004Shm } else { 120959004Shm /* name of oldest log */ 121071299Sjedgar (void) snprintf(file1, sizeof(file1), "%s.%d", log, numdays); 121171299Sjedgar (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1, 121271299Sjedgar COMPRESS_POSTFIX); 121380638Sobrien snprintf(jfile1, sizeof(jfile1), "%s%s", file1, 121480638Sobrien BZCOMPRESS_POSTFIX); 121559004Shm } 121659004Shm 121759003Shm if (noaction) { 1218111967Sgad printf("\trm -f %s\n", file1); 1219111967Sgad printf("\trm -f %s\n", zfile1); 1220111967Sgad printf("\trm -f %s\n", jfile1); 122159003Shm } else { 122259003Shm (void) unlink(file1); 122359003Shm (void) unlink(zfile1); 122480638Sobrien (void) unlink(jfile1); 122559003Shm } 122613244Sgraichen 122759003Shm /* Move down log files */ 122818188Sjkh _numdays = numdays; /* preserve */ 122959003Shm while (numdays--) { 123059004Shm 123171299Sjedgar (void) strlcpy(file2, file1, sizeof(file2)); 123259004Shm 123359004Shm if (archtodir) 123480646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s.%d", 123580646Sobrien dirpart, namepart, numdays); 123659004Shm else 123780646Sobrien (void) snprintf(file1, sizeof(file1), "%s.%d", log, 123880646Sobrien numdays); 123959004Shm 124071299Sjedgar (void) strlcpy(zfile1, file1, sizeof(zfile1)); 124171299Sjedgar (void) strlcpy(zfile2, file2, sizeof(zfile2)); 124259003Shm if (lstat(file1, &st)) { 124380646Sobrien (void) strlcat(zfile1, COMPRESS_POSTFIX, 124480646Sobrien sizeof(zfile1)); 124580646Sobrien (void) strlcat(zfile2, COMPRESS_POSTFIX, 124680646Sobrien sizeof(zfile2)); 124780638Sobrien if (lstat(zfile1, &st)) { 124880638Sobrien strlcpy(zfile1, file1, sizeof(zfile1)); 124980638Sobrien strlcpy(zfile2, file2, sizeof(zfile2)); 125080638Sobrien strlcat(zfile1, BZCOMPRESS_POSTFIX, 125180638Sobrien sizeof(zfile1)); 125280638Sobrien strlcat(zfile2, BZCOMPRESS_POSTFIX, 125380638Sobrien sizeof(zfile2)); 125480638Sobrien if (lstat(zfile1, &st)) 125580638Sobrien continue; 125680638Sobrien } 125759003Shm } 125859003Shm if (noaction) { 1259111967Sgad printf("\tmv %s %s\n", zfile1, zfile2); 1260111967Sgad printf("\tchmod %o %s\n", ent->permissions, zfile2); 1261111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1262111967Sgad printf("\tchown %u:%u %s\n", 1263111779Sgad ent->uid, ent->gid, zfile2); 126459003Shm } else { 126559003Shm (void) rename(zfile1, zfile2); 1266111780Sgad if (chmod(zfile2, ent->permissions)) 1267111780Sgad warn("can't chmod %s", file2); 1268111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1269111779Sgad if (chown(zfile2, ent->uid, ent->gid)) 1270111779Sgad warn("can't chown %s", zfile2); 127159003Shm } 127259003Shm } 1273111388Sgad if (!noaction && !(flags & CE_BINARY)) { 1274111388Sgad /* Report the trimming to the old log */ 1275111779Sgad (void) log_trim(log, ent); 1276111388Sgad } 127713244Sgraichen 127818188Sjkh if (!_numdays) { 127918075Sjkh if (noaction) 1280111967Sgad printf("\trm %s\n", log); 128118075Sjkh else 128259003Shm (void) unlink(log); 128359003Shm } else { 128418075Sjkh if (noaction) 1285111967Sgad printf("\tmv %s to %s\n", log, file1); 128659004Shm else { 128759004Shm if (archtodir) 1288111780Sgad movefile(log, file1, ent->permissions, ent->uid, 1289111779Sgad ent->gid); 129059004Shm else 129159004Shm (void) rename(log, file1); 129259004Shm } 129318075Sjkh } 129418075Sjkh 1295111781Sgad /* Now move the new log file into place */ 1296114137Sgad /* XXX - We should replace the above 'rename' with 'link(log, file1)' 1297114137Sgad * then replace the following with 'createfile(ent)' */ 1298111967Sgad strlcpy(tfile, log, sizeof(tfile)); 1299111967Sgad strlcat(tfile, ".XXXXXX", sizeof(tfile)); 1300111967Sgad if (noaction) { 1301111781Sgad printf("Start new log...\n"); 1302111967Sgad printf("\tmktemp %s\n", tfile); 1303111967Sgad } else { 130494352Ssheldonh mkstemp(tfile); 1305111780Sgad fd = creat(tfile, ent->permissions); 130659003Shm if (fd < 0) 130759003Shm err(1, "can't start new log"); 1308111779Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) 1309111779Sgad if (fchown(fd, ent->uid, ent->gid)) 1310111779Sgad err(1, "can't chown new log file"); 131159003Shm (void) close(fd); 1312111388Sgad if (!(flags & CE_BINARY)) { 1313111388Sgad /* Add status message to new log file */ 1314111779Sgad if (log_trim(tfile, ent)) 131559003Shm err(1, "can't add status message to log"); 1316111388Sgad } 131759003Shm } 1318111967Sgad if (noaction) { 1319111967Sgad printf("\tchmod %o %s\n", ent->permissions, tfile); 1320111967Sgad printf("\tmv %s %s\n", tfile, log); 1321111967Sgad } else { 1322111780Sgad (void) chmod(tfile, ent->permissions); 132394352Ssheldonh if (rename(tfile, log) < 0) { 132494352Ssheldonh err(1, "can't start new log"); 132594352Ssheldonh (void) unlink(tfile); 132694352Ssheldonh } 132794352Ssheldonh } 132825443Sache 1329111966Sgad /* 1330111966Sgad * Find out if there is a process to signal. If nosignal (-s) was 1331111966Sgad * specified, then do not signal any process. Note that nosignal 1332111966Sgad * will trigger a warning message if the rotated logfile needs to 1333111966Sgad * be compressed, *unless* -R was specified. This is because there 1334111966Sgad * presumably still are process(es) writing to the old logfile, but 1335111966Sgad * we assume that a -sR request comes from a process which writes 1336111966Sgad * to the logfile, and as such, that process has already made sure 1337111966Sgad * that the logfile is not presently in use. 1338111966Sgad */ 133925443Sache need_notification = notified = 0; 1340111779Sgad if (ent->pid_file != NULL) { 134125443Sache need_notification = 1; 1342111966Sgad if (!nosignal) 1343112003Sgad notified = send_signal(ent); /* the normal case! */ 1344111966Sgad else if (rotatereq) 1345111966Sgad need_notification = 0; 134625443Sache } 1347112003Sgad 134880638Sobrien if ((flags & CE_COMPACT) || (flags & CE_BZCOMPACT)) { 134925443Sache if (need_notification && !notified) 135080646Sobrien warnx( 1351112003Sgad "log %s.0 not compressed because daemon(s) not notified", 135280646Sobrien log); 135325443Sache else if (noaction) 1354111967Sgad if (flags & CE_COMPACT) 1355111967Sgad printf("\tgzip %s.0\n", log); 1356111967Sgad else 1357111967Sgad printf("\tbzip2 %s.0\n", log); 135825443Sache else { 135925443Sache if (notified) { 136025443Sache if (verbose) 1361112003Sgad printf("small pause to allow daemon(s) to close log\n"); 136231460Sache sleep(10); 136325443Sache } 136459004Shm if (archtodir) { 136580646Sobrien (void) snprintf(file1, sizeof(file1), "%s/%s", 136680646Sobrien dirpart, namepart); 136780638Sobrien if (flags & CE_COMPACT) 1368107916Ssobomax compress_log(file1, 1369107916Ssobomax flags & CE_COMPACTWAIT); 137080638Sobrien else if (flags & CE_BZCOMPACT) 1371107916Ssobomax bzcompress_log(file1, 1372107916Ssobomax flags & CE_COMPACTWAIT); 137359004Shm } else { 137480638Sobrien if (flags & CE_COMPACT) 1375107916Ssobomax compress_log(log, 1376107916Ssobomax flags & CE_COMPACTWAIT); 137780638Sobrien else if (flags & CE_BZCOMPACT) 1378107916Ssobomax bzcompress_log(log, 1379107916Ssobomax flags & CE_COMPACTWAIT); 138059004Shm } 138125443Sache } 138259003Shm } 138313244Sgraichen} 138413244Sgraichen 138513244Sgraichen/* Log the fact that the logs were turned over */ 138659004Shmstatic int 1387111768Sgadlog_trim(const char *log, const struct conf_entry *log_ent) 138813244Sgraichen{ 138959003Shm FILE *f; 1390111388Sgad const char *xtra; 139159003Shm 139259003Shm if ((f = fopen(log, "a")) == NULL) 139359003Shm return (-1); 1394111388Sgad xtra = ""; 1395111768Sgad if (log_ent->def_cfg) 1396111388Sgad xtra = " using <default> rule"; 1397114137Sgad if (log_ent->firstcreate) 1398114137Sgad fprintf(f, "%s %s newsyslog[%d]: logfile first created%s\n", 1399114137Sgad daytime, hostname, (int) getpid(), xtra); 1400114137Sgad else if (log_ent->r_reason != NULL) 1401111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s%s\n", 1402111772Sgad daytime, hostname, (int) getpid(), log_ent->r_reason, xtra); 1403111772Sgad else 1404111772Sgad fprintf(f, "%s %s newsyslog[%d]: logfile turned over%s\n", 1405111772Sgad daytime, hostname, (int) getpid(), xtra); 140659003Shm if (fclose(f) == EOF) 140759003Shm err(1, "log_trim: fclose:"); 140859003Shm return (0); 140913244Sgraichen} 141013244Sgraichen 141159004Shm/* Fork of gzip to compress the old log file */ 141259004Shmstatic void 1413107916Ssobomaxcompress_log(char *log, int dowait) 141413244Sgraichen{ 141559003Shm pid_t pid; 141671299Sjedgar char tmp[MAXPATHLEN]; 141759003Shm 1418107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1419107916Ssobomax ; 142071299Sjedgar (void) snprintf(tmp, sizeof(tmp), "%s.0", log); 142125443Sache pid = fork(); 142259003Shm if (pid < 0) 142380638Sobrien err(1, "gzip fork"); 142459003Shm else if (!pid) { 142579452Sbrian (void) execl(_PATH_GZIP, _PATH_GZIP, "-f", tmp, (char *)0); 142643071Swollman err(1, _PATH_GZIP); 142759003Shm } 142813244Sgraichen} 142913244Sgraichen 143080638Sobrien/* Fork of bzip2 to compress the old log file */ 143180638Sobrienstatic void 1432107916Ssobomaxbzcompress_log(char *log, int dowait) 143380638Sobrien{ 143480638Sobrien pid_t pid; 143580638Sobrien char tmp[MAXPATHLEN]; 143680638Sobrien 1437107916Ssobomax while (dowait && (wait(NULL) > 0 || errno == EINTR)) 1438107916Ssobomax ; 143980638Sobrien snprintf(tmp, sizeof(tmp), "%s.0", log); 144080638Sobrien pid = fork(); 144180638Sobrien if (pid < 0) 144280638Sobrien err(1, "bzip2 fork"); 144380638Sobrien else if (!pid) { 144486360Sobrien execl(_PATH_BZIP2, _PATH_BZIP2, "-f", tmp, (char *)0); 144580638Sobrien err(1, _PATH_BZIP2); 144680638Sobrien } 144780638Sobrien} 144880638Sobrien 144913244Sgraichen/* Return size in kilobytes of a file */ 145059004Shmstatic int 145159004Shmsizefile(char *file) 145213244Sgraichen{ 145359003Shm struct stat sb; 145413244Sgraichen 145559003Shm if (stat(file, &sb) < 0) 145659003Shm return (-1); 145759003Shm return (kbytes(dbtob(sb.st_blocks))); 145813244Sgraichen} 145913244Sgraichen 146013244Sgraichen/* Return the age of old log file (file.0) */ 146159004Shmstatic int 146259004Shmage_old_log(char *file) 146313244Sgraichen{ 146459003Shm struct stat sb; 146559003Shm char tmp[MAXPATHLEN + sizeof(".0") + sizeof(COMPRESS_POSTFIX) + 1]; 146613244Sgraichen 146759004Shm if (archtodir) { 146859004Shm char *p; 146959004Shm 147059004Shm /* build name of archive directory into tmp */ 147159004Shm if (*archdirname == '/') { /* absolute */ 147271299Sjedgar strlcpy(tmp, archdirname, sizeof(tmp)); 147359004Shm } else { /* relative */ 147459004Shm /* get directory part of logfile */ 147571299Sjedgar strlcpy(tmp, file, sizeof(tmp)); 147659004Shm if ((p = rindex(tmp, '/')) == NULL) 147759004Shm tmp[0] = '\0'; 147859004Shm else 147959004Shm *(p + 1) = '\0'; 148071299Sjedgar strlcat(tmp, archdirname, sizeof(tmp)); 148159004Shm } 148259004Shm 148371299Sjedgar strlcat(tmp, "/", sizeof(tmp)); 148459004Shm 148559004Shm /* get filename part of logfile */ 148659004Shm if ((p = rindex(file, '/')) == NULL) 148771299Sjedgar strlcat(tmp, file, sizeof(tmp)); 148859004Shm else 148971299Sjedgar strlcat(tmp, p + 1, sizeof(tmp)); 149059004Shm } else { 149171299Sjedgar (void) strlcpy(tmp, file, sizeof(tmp)); 149259004Shm } 149359004Shm 149459003Shm if (stat(strcat(tmp, ".0"), &sb) < 0) 149559003Shm if (stat(strcat(tmp, COMPRESS_POSTFIX), &sb) < 0) 149659003Shm return (-1); 1497111781Sgad return ((int)(timenow - sb.st_mtime + 1800) / 3600); 149813244Sgraichen} 149913244Sgraichen 150013244Sgraichen/* Skip Over Blanks */ 1501111820Sgadstatic char * 150259004Shmsob(char *p) 150313244Sgraichen{ 150459003Shm while (p && *p && isspace(*p)) 150559003Shm p++; 150659003Shm return (p); 150713244Sgraichen} 150813244Sgraichen 150913244Sgraichen/* Skip Over Non-Blanks */ 1510111820Sgadstatic char * 151159004Shmson(char *p) 151213244Sgraichen{ 151359003Shm while (p && *p && !isspace(*p)) 151459003Shm p++; 151559003Shm return (p); 151613244Sgraichen} 151743071Swollman 151843071Swollman/* 151959004Shm * Parse a limited subset of ISO 8601. The specific format is as follows: 152043071Swollman * 152159004Shm * [CC[YY[MM[DD]]]][THH[MM[SS]]] (where `T' is the literal letter) 152243071Swollman * 152359004Shm * We don't accept a timezone specification; missing fields (including timezone) 152459004Shm * are defaulted to the current date but time zero. 152543071Swollman */ 152643071Swollmanstatic time_t 152793659Scjcparse8601(char *s, char *errline) 152843071Swollman{ 152959003Shm char *t; 153093659Scjc time_t tsecs; 153159003Shm struct tm tm, *tmp; 153259003Shm u_long ul; 153343071Swollman 153443071Swollman tmp = localtime(&timenow); 153543071Swollman tm = *tmp; 153643071Swollman 153743071Swollman tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 153843071Swollman 153943071Swollman ul = strtoul(s, &t, 10); 154043071Swollman if (*t != '\0' && *t != 'T') 1541111392Sgad return (-1); 154243071Swollman 154343071Swollman /* 154459003Shm * Now t points either to the end of the string (if no time was 154559003Shm * provided) or to the letter `T' which separates date and time in 154659003Shm * ISO 8601. The pointer arithmetic is the same for either case. 154743071Swollman */ 154843071Swollman switch (t - s) { 154943071Swollman case 8: 155043071Swollman tm.tm_year = ((ul / 1000000) - 19) * 100; 155143071Swollman ul = ul % 1000000; 155243071Swollman case 6: 155380666Swollman tm.tm_year -= tm.tm_year % 100; 155443071Swollman tm.tm_year += ul / 10000; 155543071Swollman ul = ul % 10000; 155643071Swollman case 4: 155743071Swollman tm.tm_mon = (ul / 100) - 1; 155843071Swollman ul = ul % 100; 155943071Swollman case 2: 156043071Swollman tm.tm_mday = ul; 156143071Swollman case 0: 156243071Swollman break; 156343071Swollman default: 1564111392Sgad return (-1); 156543071Swollman } 156643071Swollman 156743071Swollman /* sanity check */ 156843071Swollman if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12 156943071Swollman || tm.tm_mday < 1 || tm.tm_mday > 31) 1570111392Sgad return (-1); 157143071Swollman 157243071Swollman if (*t != '\0') { 157343071Swollman s = ++t; 157443071Swollman ul = strtoul(s, &t, 10); 157543071Swollman if (*t != '\0' && !isspace(*t)) 1576111392Sgad return (-1); 157743071Swollman 157843071Swollman switch (t - s) { 157943071Swollman case 6: 158043071Swollman tm.tm_sec = ul % 100; 158143071Swollman ul /= 100; 158243071Swollman case 4: 158343071Swollman tm.tm_min = ul % 100; 158443071Swollman ul /= 100; 158543071Swollman case 2: 158643071Swollman tm.tm_hour = ul; 158743071Swollman case 0: 158843071Swollman break; 158943071Swollman default: 1590111392Sgad return (-1); 159143071Swollman } 159243071Swollman 159343071Swollman /* sanity check */ 159443071Swollman if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0 159543071Swollman || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23) 1596111392Sgad return (-1); 159743071Swollman } 159893659Scjc if ((tsecs = mktime(&tm)) == -1) 159999209Smaxim errx(1, "nonexistent time:\n%s", errline); 1600111392Sgad return (tsecs); 160143071Swollman} 160259004Shm 160359004Shm/* physically move file */ 160459004Shmstatic void 1605111779Sgadmovefile(char *from, char *to, int perm, uid_t owner_uid, gid_t group_gid) 160659004Shm{ 160759004Shm FILE *src, *dst; 160859004Shm int c; 160959004Shm 161059004Shm if ((src = fopen(from, "r")) == NULL) 161159004Shm err(1, "can't fopen %s for reading", from); 161259004Shm if ((dst = fopen(to, "w")) == NULL) 161359004Shm err(1, "can't fopen %s for writing", to); 1614111779Sgad if (owner_uid != (uid_t)-1 || group_gid != (gid_t)-1) { 1615111779Sgad if (fchown(fileno(dst), owner_uid, group_gid)) 1616111779Sgad err(1, "can't fchown %s", to); 1617111779Sgad } 161859004Shm if (fchmod(fileno(dst), perm)) 161959004Shm err(1, "can't fchmod %s", to); 162059004Shm 162159004Shm while ((c = getc(src)) != EOF) { 162259004Shm if ((putc(c, dst)) == EOF) 162359004Shm err(1, "error writing to %s", to); 162459004Shm } 162559004Shm 162659004Shm if (ferror(src)) 162759004Shm err(1, "error reading from %s", from); 162859004Shm if ((fclose(src)) != 0) 162959004Shm err(1, "can't fclose %s", to); 163059004Shm if ((fclose(dst)) != 0) 163159004Shm err(1, "can't fclose %s", from); 163259004Shm if ((unlink(from)) != 0) 163359004Shm err(1, "can't unlink %s", from); 163459004Shm} 163559004Shm 163659004Shm/* create one or more directory components of a path */ 163759004Shmstatic void 1638114137Sgadcreatedir(const struct conf_entry *ent, char *dirpart) 163959004Shm{ 1640111398Sgad int res; 164159004Shm char *s, *d; 164271299Sjedgar char mkdirpath[MAXPATHLEN]; 164359004Shm struct stat st; 164459004Shm 164559004Shm s = dirpart; 164659004Shm d = mkdirpath; 164759004Shm 164859004Shm for (;;) { 164959004Shm *d++ = *s++; 1650111398Sgad if (*s != '/' && *s != '\0') 1651111398Sgad continue; 1652111398Sgad *d = '\0'; 1653111398Sgad res = lstat(mkdirpath, &st); 1654111398Sgad if (res != 0) { 1655111398Sgad if (noaction) { 1656111967Sgad printf("\tmkdir %s\n", mkdirpath); 1657111398Sgad } else { 1658111398Sgad res = mkdir(mkdirpath, 0755); 1659111398Sgad if (res != 0) 1660111398Sgad err(1, "Error on mkdir(\"%s\") for -a", 1661111398Sgad mkdirpath); 1662111398Sgad } 166359004Shm } 166459004Shm if (*s == '\0') 166559004Shm break; 166659004Shm } 1667114137Sgad if (verbose) { 1668114137Sgad if (ent->firstcreate) 1669114137Sgad printf("Created directory '%s' for new %s\n", 1670114137Sgad dirpart, ent->log); 1671114137Sgad else 1672114137Sgad printf("Created directory '%s' for -a\n", dirpart); 1673114137Sgad } 167459004Shm} 167559004Shm 1676114137Sgad/* 1677114137Sgad * Create a new log file, destroying any currently-existing version 1678114137Sgad * of the log file in the process. If the caller wants a backup copy 1679114137Sgad * of the file to exist, they should call 'link(logfile,logbackup)' 1680114137Sgad * before calling this routine. 1681114137Sgad */ 1682114137Sgadvoid 1683114137Sgadcreatelog(const struct conf_entry *ent) 1684114137Sgad{ 1685114137Sgad int fd, failed; 1686114137Sgad struct stat st; 1687114137Sgad char *realfile, *slash, tempfile[MAXPATHLEN]; 1688114137Sgad 1689114137Sgad fd = -1; 1690114137Sgad realfile = ent->log; 1691114137Sgad 1692114137Sgad /* 1693114137Sgad * If this log file is being created for the first time (-C option), 1694114137Sgad * then it may also be true that the parent directory does not exist 1695114137Sgad * yet. Check, and create that directory if it is missing. 1696114137Sgad */ 1697114137Sgad if (ent->firstcreate) { 1698114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 1699114137Sgad slash = strrchr(tempfile, '/'); 1700114137Sgad if (slash != NULL) { 1701114137Sgad *slash = '\0'; 1702114137Sgad failed = lstat(tempfile, &st); 1703114137Sgad if (failed && errno != ENOENT) 1704114137Sgad err(1, "Error on lstat(%s)", tempfile); 1705114137Sgad if (failed) 1706114137Sgad createdir(ent, tempfile); 1707114137Sgad else if (!S_ISDIR(st.st_mode)) 1708114137Sgad errx(1, "%s exists but is not a directory", 1709114137Sgad tempfile); 1710114137Sgad } 1711114137Sgad } 1712114137Sgad 1713114137Sgad /* 1714114137Sgad * First create an unused filename, so it can be chown'ed and 1715114137Sgad * chmod'ed before it is moved into the real location. mkstemp 1716114137Sgad * will create the file mode=600 & owned by us. Note that all 1717114137Sgad * temp files will have a suffix of '.z<something>'. 1718114137Sgad */ 1719114137Sgad strlcpy(tempfile, realfile, sizeof(tempfile)); 1720114137Sgad strlcat(tempfile, ".zXXXXXX", sizeof(tempfile)); 1721114137Sgad if (noaction) 1722114137Sgad printf("\tmktemp %s\n", tempfile); 1723114137Sgad else { 1724114137Sgad fd = mkstemp(tempfile); 1725114137Sgad if (fd < 0) 1726114137Sgad err(1, "can't mkstemp logfile %s", tempfile); 1727114137Sgad 1728114137Sgad /* 1729114137Sgad * Add status message to what will become the new log file. 1730114137Sgad */ 1731114137Sgad if (!(ent->flags & CE_BINARY)) { 1732114137Sgad if (log_trim(tempfile, ent)) 1733114137Sgad err(1, "can't add status message to log"); 1734114137Sgad } 1735114137Sgad } 1736114137Sgad 1737114137Sgad /* Change the owner/group, if we are supposed to */ 1738114137Sgad if (ent->uid != (uid_t)-1 || ent->gid != (gid_t)-1) { 1739114137Sgad if (noaction) 1740114137Sgad printf("\tchown %u:%u %s\n", ent->uid, ent->gid, 1741114137Sgad tempfile); 1742114137Sgad else { 1743114137Sgad failed = fchown(fd, ent->uid, ent->gid); 1744114137Sgad if (failed) 1745114137Sgad err(1, "can't fchown temp file %s", tempfile); 1746114137Sgad (void) close(fd); 1747114137Sgad } 1748114137Sgad } 1749114137Sgad 1750114137Sgad /* 1751114137Sgad * Note that if the real logfile still exists, and if the call 1752114137Sgad * to rename() fails, then "neither the old file nor the new 1753114137Sgad * file shall be changed or created" (to quote the standard). 1754114137Sgad * If the call succeeds, then the file will be replaced without 1755114137Sgad * any window where some other process might find that the file 1756114137Sgad * did not exist. 1757114137Sgad * XXX - ? It may be that for some error conditions, we could 1758114137Sgad * retry by first removing the realfile and then renaming. 1759114137Sgad */ 1760114137Sgad if (noaction) { 1761114137Sgad printf("\tchmod %o %s\n", ent->permissions, tempfile); 1762114137Sgad printf("\tmv %s %s\n", tempfile, realfile); 1763114137Sgad } else { 1764114137Sgad failed = fchmod(fd, ent->permissions); 1765114137Sgad if (failed) 1766114137Sgad err(1, "can't fchmod temp file '%s'", tempfile); 1767114137Sgad failed = rename(tempfile, realfile); 1768114137Sgad if (failed) 1769114137Sgad err(1, "can't mv %s to %s", tempfile, realfile); 1770114137Sgad } 1771114137Sgad 1772114137Sgad if (fd >= 0) 1773114137Sgad close(fd); 1774114137Sgad} 1775114137Sgad 177659004Shm/*- 177759004Shm * Parse a cyclic time specification, the format is as follows: 177859004Shm * 177959004Shm * [Dhh] or [Wd[Dhh]] or [Mdd[Dhh]] 178059004Shm * 178159004Shm * to rotate a logfile cyclic at 178259004Shm * 178359004Shm * - every day (D) within a specific hour (hh) (hh = 0...23) 178459004Shm * - once a week (W) at a specific day (d) OR (d = 0..6, 0 = Sunday) 178559004Shm * - once a month (M) at a specific day (d) (d = 1..31,l|L) 178659004Shm * 178759004Shm * We don't accept a timezone specification; missing fields 178859004Shm * are defaulted to the current date but time zero. 178959004Shm */ 179059004Shmstatic time_t 179193659ScjcparseDWM(char *s, char *errline) 179259004Shm{ 179359004Shm char *t; 179493659Scjc time_t tsecs; 179559004Shm struct tm tm, *tmp; 179680742Sobrien long l; 179759004Shm int nd; 179859004Shm static int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; 179959004Shm int WMseen = 0; 180059004Shm int Dseen = 0; 180159004Shm 180259004Shm tmp = localtime(&timenow); 180359004Shm tm = *tmp; 180459004Shm 180559004Shm /* set no. of days per month */ 180659004Shm 180759004Shm nd = mtab[tm.tm_mon]; 180859004Shm 180959004Shm if (tm.tm_mon == 1) { 181059004Shm if (((tm.tm_year + 1900) % 4 == 0) && 181159004Shm ((tm.tm_year + 1900) % 100 != 0) && 181259004Shm ((tm.tm_year + 1900) % 400 == 0)) { 181359004Shm nd++; /* leap year, 29 days in february */ 181459004Shm } 181559004Shm } 181659004Shm tm.tm_hour = tm.tm_min = tm.tm_sec = 0; 181759004Shm 181859004Shm for (;;) { 181959004Shm switch (*s) { 182059004Shm case 'D': 182159004Shm if (Dseen) 1822111392Sgad return (-1); 182359004Shm Dseen++; 182459004Shm s++; 182580742Sobrien l = strtol(s, &t, 10); 182680742Sobrien if (l < 0 || l > 23) 1827111392Sgad return (-1); 182880742Sobrien tm.tm_hour = l; 182959004Shm break; 183059004Shm 183159004Shm case 'W': 183259004Shm if (WMseen) 1833111392Sgad return (-1); 183459004Shm WMseen++; 183559004Shm s++; 183680742Sobrien l = strtol(s, &t, 10); 183780742Sobrien if (l < 0 || l > 6) 1838111392Sgad return (-1); 183980742Sobrien if (l != tm.tm_wday) { 184059004Shm int save; 184159004Shm 184280742Sobrien if (l < tm.tm_wday) { 184359004Shm save = 6 - tm.tm_wday; 184480742Sobrien save += (l + 1); 184559004Shm } else { 184680742Sobrien save = l - tm.tm_wday; 184759004Shm } 184859004Shm 184959004Shm tm.tm_mday += save; 185059004Shm 185159004Shm if (tm.tm_mday > nd) { 185259004Shm tm.tm_mon++; 185359004Shm tm.tm_mday = tm.tm_mday - nd; 185459004Shm } 185559004Shm } 185659004Shm break; 185759004Shm 185859004Shm case 'M': 185959004Shm if (WMseen) 1860111392Sgad return (-1); 186159004Shm WMseen++; 186259004Shm s++; 186359004Shm if (tolower(*s) == 'l') { 186459004Shm tm.tm_mday = nd; 186559004Shm s++; 186659004Shm t = s; 186759004Shm } else { 186880742Sobrien l = strtol(s, &t, 10); 186980742Sobrien if (l < 1 || l > 31) 1870111392Sgad return (-1); 187159004Shm 187280742Sobrien if (l > nd) 1873111392Sgad return (-1); 187480742Sobrien tm.tm_mday = l; 187559004Shm } 187659004Shm break; 187759004Shm 187859004Shm default: 187959004Shm return (-1); 188059004Shm break; 188159004Shm } 188259004Shm 188359004Shm if (*t == '\0' || isspace(*t)) 188459004Shm break; 188559004Shm else 188659004Shm s = t; 188759004Shm } 188893659Scjc if ((tsecs = mktime(&tm)) == -1) 188999209Smaxim errx(1, "nonexistent time:\n%s", errline); 1890111392Sgad return (tsecs); 189159004Shm} 1892