config.c 42.2 KB
Newer Older
1
#include <sys/queue.h>
2 3
/* Alloca is defined in stdlib.h in NetBSD */
#ifndef __NetBSD__
4
#include <alloca.h>
5
#endif
6
#include <limits.h>
7 8 9 10 11 12
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <grp.h>
13
#include <popt.h>
14 15 16 17 18 19 20
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
21
#include <assert.h>
22 23 24
#include <wchar.h>
#include <wctype.h>
#include <fnmatch.h>
25
#include <sys/mman.h>
26 27 28 29 30 31 32 33 34

#include "basenames.h"
#include "log.h"
#include "logrotate.h"

#if !defined(GLOB_ABORTED) && defined(GLOB_ABEND)
#define GLOB_ABORTED GLOB_ABEND
#endif

35 36
#define REALLOC_STEP    10

37 38
#if defined(SunOS)
#include <limits.h>
39
#if !defined(isblank)
40 41
#define isblank(c) 	( (c) == ' ' || (c) == '\t' ) ? 1 : 0
#endif
42
#endif
43

44 45 46 47
#ifdef __hpux
#include "asprintf.c"
#endif

48
#if !defined(asprintf) && !defined(_FORTIFY_SOURCE)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <stdarg.h>

int asprintf(char **string_ptr, const char *format, ...)
{
	va_list arg;
	char *str;
	int size;
	int rv;

	va_start(arg, format);
	size = vsnprintf(NULL, 0, format, arg);
	size++;
	va_start(arg, format);
	str = malloc(size);
	if (str == NULL) {
		va_end(arg);
		/*
		 * Strictly speaking, GNU asprintf doesn't do this,
		 * but the caller isn't checking the return value.
		 */
		fprintf(stderr, "failed to allocate memory\\n");
		exit(1);
	}
	rv = vsnprintf(str, size, format, arg);
	va_end(arg);

	*string_ptr = str;
	return (rv);
}

#endif

81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
#if !defined(strndup)
char *strndup(const char *s, size_t n)
{
       size_t nAvail;
       char *p;

       if(!s)
               return NULL;

       /* min() */
       nAvail = strlen(s) + 1;
       if ( (n + 1) < nAvail)
               nAvail = n + 1;

       p = malloc(nAvail);
       if (!p)
               return NULL;
       memcpy(p, s, nAvail);
       p[nAvail - 1] = 0;
       return p;
}
#endif

104 105 106 107 108 109 110 111 112
enum {
	STATE_DEFAULT = 2,
	STATE_SKIP_LINE = 4,
	STATE_DEFINITION_END = 8,
	STATE_SKIP_CONFIG = 16,
	STATE_LOAD_SCRIPT = 32,
	STATE_ERROR = 64,
};

113
static char *defTabooExts[] = { ".rpmsave", ".rpmorig", "~", ",v",
114 115
    ".disabled", ".dpkg-old", ".dpkg-dist", ".dpkg-new", ".cfsaved",
    ".ucf-old", ".ucf-dist", ".ucf-new",
116 117
    ".rpmnew", ".swp", ".cfsaved", ".rhn-cfg-tmp-*"
};
118 119 120
static int defTabooCount = sizeof(defTabooExts) / sizeof(char *);

/* I shouldn't use globals here :-( */
121
static char **tabooExts = NULL;
122
int tabooCount = 0;
123
static int glob_errno = 0;
124

125 126
static int readConfigFile(const char *configFile, struct logInfo *defConfig);
static int globerr(const char *pathname, int theerr);
127

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
static char *isolateLine(char **strt, char **buf, size_t length) {
	char *endtag, *start, *tmp;
	start = *strt;
	endtag = start;
	while (endtag - *buf < length && *endtag != '\n') {
		endtag++;}
	if (endtag - *buf > length)
		return NULL;
	tmp = endtag - 1;
	while (isspace(*endtag))
		endtag--;
	char *key = strndup(start, endtag - start + 1);
	*strt = tmp;
	return key;
}

static char *isolateValue(const char *fileName, int lineNum, char *key,
			char **startPtr, char **buf, size_t length)
146 147
{
    char *chptr = *startPtr;
148

149
    while (chptr - *buf < length && isblank(*chptr))
150
	chptr++;
151
    if (chptr - *buf < length && *chptr == '=') {
152
	chptr++;
153
	while ( chptr - *buf < length && isblank(*chptr))
154
	    chptr++;
155 156
    }

157 158 159 160
    if (chptr - *buf < length && *chptr == '\n') {
		message(MESS_ERROR, "%s:%d argument expected after %s\n",
			fileName, lineNum, key);
		return NULL;
161 162
    }

163 164 165
	*startPtr = chptr;
	return isolateLine(startPtr, buf, length);
}
166

167 168 169 170 171 172 173 174 175 176 177 178 179
static char *isolateWord(char **strt, char **buf, size_t length) {
	char *endtag, *start;
	start = *strt;
	while (start - *buf < length && isblank(*start))
		start++;
	endtag = start;
	while (endtag - *buf < length && isalpha(*endtag)) {
		endtag++;}
	if (endtag - *buf > length)
		return NULL;
	char *key = strndup(start, endtag - start);
	*strt = endtag;
	return key;
180 181
}

182
static char *readPath(const char *configFile, int lineNum, char *key,
183
		      char **startPtr, char **buf, size_t length)
184
{
185
    char *chptr;
186 187 188
    char *start = *startPtr;
    char *path;

189 190 191
    wchar_t pwc;
    size_t len;

192
    if ((start = isolateValue(configFile, lineNum, key, startPtr, buf, length)) != NULL) {
193 194 195

	chptr = start;

196
	while( (len = mbrtowc(&pwc, chptr, strlen(chptr), NULL)) != 0 && strlen(chptr) != 0) {
197 198 199 200 201 202 203 204 205
		if( len == (size_t)(-1) || len == (size_t)(-2) || !iswprint(pwc) || iswblank(pwc) ) {
		    message(MESS_ERROR, "%s:%d bad %s path %s\n",
			    configFile, lineNum, key, start);
		    return NULL;
		}
		chptr += len;
	}

/*
206 207 208 209 210 211 212
	while (*chptr && isprint(*chptr) && *chptr != ' ')
	    chptr++;
	if (*chptr) {
	    message(MESS_ERROR, "%s:%d bad %s path %s\n",
		    configFile, lineNum, key, start);
	    return NULL;
	}
213 214
*/

215
	path = strdup(start);
216
	free(start);
217 218 219 220 221 222

	return path;
    } else
	return NULL;
}

223
static char *readAddress(const char *configFile, int lineNum, char *key,
224
			 char **startPtr, char **buf, size_t length)
225 226 227 228
{
    char *endtag, *chptr;
    char *start = *startPtr;
    char *address;
229 230
	
    if ((endtag = isolateValue(configFile, lineNum, key, startPtr, buf, length)) != NULL) {
231

232 233
	chptr = endtag;
	while (*chptr && isprint(*chptr) && *chptr != ' ') {
234
	    chptr++;
235 236
	}

237 238 239 240 241 242
	if (*chptr) {
	    message(MESS_ERROR, "%s:%d bad %s address %s\n",
		    configFile, lineNum, key, start);
	    return NULL;
	}

243 244 245
	address = strdup(endtag);
	
	free(endtag);
246 247 248 249 250 251

	return address;
    } else
	return NULL;
}

252 253 254
static int checkFile(const char *fname)
{
	int i;
255
	char *pattern;
256 257 258 259 260 261 262

	/* Check if fname is '.' or '..'; if so, return false */
	if (fname[0] == '.' && (!fname[1] || (fname[1] == '.' && !fname[2])))
		return 0;

	/* Check if fname is ending in a taboo-extension; if so, return false */
	for (i = 0; i < tabooCount; i++) {
263 264 265
		if (asprintf(&pattern, "*%s", tabooExts[i]) < 0) {
			message(MESS_FATAL, "failed to allocate taboo pattern memory\n");
		}
266 267
		if (!fnmatch(pattern, fname, 0))
		{
268
			free(pattern);
269 270 271 272
			message(MESS_DEBUG, "Ignoring %s, because of %s ending\n",
					fname, tabooExts[i]);
			return 0;
		}
273
	}
274
	free(pattern);
275 276
	/* All checks have been passed; return true */
	return 1;
277 278 279
}

/* Used by qsort to sort filelist */
280 281 282
static int compar(const void *p, const void *q)
{
    return strcoll(*((char **) p), *((char **) q));
283 284
}

285 286
/* Free memory blocks pointed to by pointers in a 2d array and the array itself */
static void free_2d_array(char **array, int lines_count)
287 288
{
    int i;
289 290 291
    for (i = 0; i < lines_count; ++i)
	free(array[i]);
    free(array);
292 293
}

294 295 296 297 298 299 300 301
static void copyLogInfo(struct logInfo *to, struct logInfo *from)
{
    memset(to, 0, sizeof(*to));
    if (from->oldDir)
	to->oldDir = strdup(from->oldDir);
    to->criterium = from->criterium;
    to->threshhold = from->threshhold;
    to->minsize = from->minsize;
302
	to->maxsize = from->maxsize;
303 304 305 306 307 308 309 310 311 312 313
    to->rotateCount = from->rotateCount;
    to->rotateAge = from->rotateAge;
    to->logStart = from->logStart;
    if (from->pre)
	to->pre = strdup(from->pre);
    if (from->post)
	to->post = strdup(from->post);
    if (from->first)
	to->first = strdup(from->first);
    if (from->last)
	to->last = strdup(from->last);
314 315
    if (from->preremove)
	to->preremove = strdup(from->preremove);
316 317 318 319 320 321 322 323 324 325 326
    if (from->logAddress)
	to->logAddress = strdup(from->logAddress);
    if (from->extension)
	to->extension = strdup(from->extension);
    if (from->compress_prog)
	to->compress_prog = strdup(from->compress_prog);
    if (from->uncompress_prog)
	to->uncompress_prog = strdup(from->uncompress_prog);
    if (from->compress_ext)
	to->compress_ext = strdup(from->compress_ext);
    to->flags = from->flags;
327
	to->shred_cycles = from->shred_cycles;
328 329 330
    to->createMode = from->createMode;
    to->createUid = from->createUid;
    to->createGid = from->createGid;
331 332
    to->suUid = from->suUid;
    to->suGid = from->suGid;
333 334 335 336 337 338 339
    if (from->compress_options_count) {
        poptDupArgv(from->compress_options_count, from->compress_options_list, 
                    &to->compress_options_count,  &to->compress_options_list);
    }
	if (from->dateformat)
		to->dateformat = strdup(from->dateformat);
}
340

341 342 343 344 345 346 347 348 349
static void freeLogInfo(struct logInfo *log)
{
	free(log->pattern);
	free_2d_array(log->files, log->numFiles);
	free(log->oldDir);
	free(log->pre);
	free(log->post);
	free(log->first);
	free(log->last);
350
	free(log->preremove);
351 352 353 354 355 356 357 358
	free(log->logAddress);
	free(log->extension);
	free(log->compress_prog);
	free(log->uncompress_prog);
	free(log->compress_ext);
	free(log->compress_options_list);
	free(log->dateformat);
}
359

360 361 362
static struct logInfo *newLogInfo(struct logInfo *template)
{
	struct logInfo *new;
363

364 365
	if ((new = malloc(sizeof(*new))) == NULL)
		return NULL;
366

367 368 369
	copyLogInfo(new, template);
	TAILQ_INSERT_TAIL(&logs, new, list);
	numLogs++;
370

371 372
	return new;
}
373

374 375 376 377 378 379 380 381
static void removeLogInfo(struct logInfo *log)
{
	if (log == NULL)
		return;

	freeLogInfo(log);
	TAILQ_REMOVE(&logs, log, list);
	numLogs--;
382 383
}

384 385 386
static void freeTailLogs(int num)
{
	message(MESS_DEBUG, "removing last %d log configs\n", num);
387

388
	while (num--)
389 390
		removeLogInfo(TAILQ_LAST(&logs, logInfoHead));

391 392 393 394 395 396 397
}

static int readConfigPath(const char *path, struct logInfo *defConfig)
{
    struct stat sb;
    int here, oldnumlogs, result = 1;
	struct logInfo defConfigBackup;
398 399 400 401 402 403 404

    if (stat(path, &sb)) {
	message(MESS_ERROR, "cannot stat %s: %s\n", path, strerror(errno));
	return 1;
    }

    if (S_ISDIR(sb.st_mode)) {
405 406 407 408
	char **namelist, **p;
	struct dirent *dp;
	int files_count, i;
	DIR *dirp;
409 410 411

	here = open(".", O_RDONLY);

412
	if ((dirp = opendir(path)) == NULL) {
413
	    message(MESS_ERROR, "cannot open directory %s: %s\n", path,
414
		    strerror(errno));
415
	    close(here);
416 417
	    return 1;
	}
418 419 420 421 422 423
	files_count = 0;
	namelist = NULL;
	while ((dp = readdir(dirp)) != NULL) {
	    if (checkFile(dp->d_name)) {
		/* Realloc memory for namelist array if necessary */
		if (files_count % REALLOC_STEP == 0) {
424 425 426
		    p = (char **) realloc(namelist,
					  (files_count +
					   REALLOC_STEP) * sizeof(char *));
427 428
		    if (p) {
			namelist = p;
429 430
			memset(namelist + files_count, '\0',
			       REALLOC_STEP * sizeof(char *));
431
		    } else {
432 433 434 435 436
			free_2d_array(namelist, files_count);
			closedir(dirp);
			close(here);
			message(MESS_ERROR, "cannot realloc: %s\n",
				strerror(errno));
437 438 439
			return 1;
		    }
		}
440
		/* Alloc memory for file name */
441 442
		if ((namelist[files_count] =
		     (char *) malloc(strlen(dp->d_name) + 1))) {
443 444 445
		    strcpy(namelist[files_count], dp->d_name);
		    files_count++;
		} else {
446 447 448 449 450
		    free_2d_array(namelist, files_count);
		    closedir(dirp);
		    close(here);
		    message(MESS_ERROR, "cannot realloc: %s\n",
			    strerror(errno));
451 452
		    return 1;
		}
453
	    }
454
	}
455
	closedir(dirp);
456

457 458 459
	if (files_count > 0) {
	    qsort(namelist, files_count, sizeof(char *), compar);
	} else {
460
	    close(here);
461 462 463 464 465 466 467
	    return 0;
	}

	if (chdir(path)) {
	    message(MESS_ERROR, "error in chdir(\"%s\"): %s\n", path,
		    strerror(errno));
	    close(here);
468
	    free_2d_array(namelist, files_count);
469 470 471
	    return 1;
	}

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	for (i = 0; i < files_count; ++i) {
	    assert(namelist[i] != NULL);
	    oldnumlogs = numLogs;
	    copyLogInfo(&defConfigBackup, defConfig);
	    if (readConfigFile(namelist[i], defConfig)) {
		message(MESS_ERROR, "found error in file %s, skipping\n", namelist[i]);
		freeTailLogs(numLogs - oldnumlogs);
		freeLogInfo(defConfig);
		copyLogInfo(defConfig, &defConfigBackup);
		freeLogInfo(&defConfigBackup);
		continue;
	    } else {
		result = 0;
	    }
	    freeLogInfo(&defConfigBackup);
	}
488

489 490 491
	if (fchdir(here) < 0) {
		message(MESS_ERROR, "could not change directory to '.'");
	}
492
	close(here);
493
	free_2d_array(namelist, files_count);
494
    } else {
495 496 497 498 499 500 501 502 503 504
    	oldnumlogs = numLogs;
	copyLogInfo(&defConfigBackup, defConfig);
	if (readConfigFile(path, defConfig)) {
	    freeTailLogs(numLogs - oldnumlogs);
	    freeLogInfo(defConfig);
	    copyLogInfo(defConfig, &defConfigBackup);
	} else {
	    result = 0;
	}
	freeLogInfo(&defConfigBackup);
505 506
    }

507 508 509 510 511 512 513
    return result;
}

int readAllConfigPaths(const char **paths)
{
    int i, result = 0;
    const char **file;
514 515 516 517 518 519 520 521
    struct logInfo defConfig = {
		.pattern = NULL,
		.files = NULL,
		.numFiles = 0,
		.oldDir = NULL,
		.criterium = ROT_SIZE,
		.threshhold = 1024 * 1024,
		.minsize = 0,
522
		.maxsize = 0,
523 524 525 526 527 528 529
		.rotateCount = 0,
		.rotateAge = 0,
		.logStart = -1,
		.pre = NULL,
		.post = NULL,
		.first = NULL,
		.last = NULL,
530
		.preremove = NULL,
531 532 533 534 535 536 537 538 539 540 541 542 543
		.logAddress = NULL,
		.extension = NULL,
		.compress_prog = NULL,
		.uncompress_prog = NULL,
		.compress_ext = NULL,
		.dateformat = NULL,
		.flags = LOG_FLAG_IFEMPTY,
		.shred_cycles = 0,
		.createMode = NO_MODE,
		.createUid = NO_UID,
		.createGid = NO_GID,
		.compress_options_list = NULL,
		.compress_options_count = 0
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
    };

    tabooExts = malloc(sizeof(*tabooExts) * defTabooCount);
    for (i = 0; i < defTabooCount; i++) {
	if ((tabooExts[i] = (char *) malloc(strlen(defTabooExts[i]) + 1))) {
	    strcpy(tabooExts[i], defTabooExts[i]);
	    tabooCount++;
	} else {
	    free_2d_array(tabooExts, tabooCount);
	    message(MESS_ERROR, "cannot malloc: %s\n", strerror(errno));
	    return 1;
	}
    }

    for (file = paths; *file; file++) {
	if (readConfigPath(*file, &defConfig)) {
	    result = 1;
	    break;
	}
    }
    free_2d_array(tabooExts, tabooCount);
    freeLogInfo(&defConfig);
    return result;
567 568
}

569 570
static int globerr(const char *pathname, int theerr)
{
571
    glob_errno = theerr;
572

573
    /* We want the glob operation to abort on error, so return 1 */
574 575 576
    return 1;
}

577
#define freeLogItem(what) \
578 579 580 581 582
	do { \
		free(newlog->what); \
		newlog->what = NULL; \
	} while (0);
#define MAX_NESTING 16U
583 584 585

static int readConfigFile(const char *configFile, struct logInfo *defConfig)
{
586
    int fd;
587 588
    char *buf, *endtag, *key = NULL;
    char foo;
589
    off_t length;
590
    int lineNum = 1;
591
    unsigned long long multiplier;
592 593 594 595 596 597 598
    int i, k;
    char *scriptStart = NULL;
    char **scriptDest = NULL;
    struct logInfo *newlog = defConfig;
    char *start, *chptr;
    char *dirName;
    struct group *group;
599
    struct passwd *pw = NULL;
600 601 602 603 604
    int rc;
    char createOwner[200], createGroup[200];
    int createMode;
    struct stat sb, sb2;
    glob_t globResult;
605
    const char **argv;
606
    int argc, argNum;
607 608
	int flags;
	int state = STATE_DEFAULT;
609 610
    int logerror = 0;
    struct logInfo *log;
611
	static unsigned recursion_depth = 0U;
612
	char *globerr_msg = NULL;
613
	int in_config = 0;
614 615 616 617 618 619
	struct flock fd_lock = {
		.l_start = 0,
		.l_len = 0,
		.l_whence = SEEK_SET,
		.l_type = F_RDLCK
	};
620 621 622 623 624

    /* FIXME: createOwner and createGroup probably shouldn't be fixed
       length arrays -- of course, if we aren't run setuid it doesn't
       matter much */

625 626 627 628 629 630 631 632 633
	fd = open(configFile, O_RDONLY);
	if (fd < 0) {
		message(MESS_ERROR, "failed to open config file %s: %s\n",
			configFile, strerror(errno));
		return 1;
	}
	if ((flags = fcntl(fd, F_GETFD)) == -1) {
		message(MESS_ERROR, "Could not retrieve flags from file %s\n",
				configFile);
634
		close(fd);
635 636 637 638 639 640
		return 1;
	}
	flags |= FD_CLOEXEC;
	if (fcntl(fd, F_SETFD, flags) == -1) {
		message(MESS_ERROR, "Could not set flags on file %s\n",
				configFile);
641
		close(fd);
642 643
		return 1;
	}
644 645 646
	/* We don't want anybody to change the file while we parse it,
	 * let's try to lock it for reading. */
	if (fcntl(fd, F_SETLK, &fd_lock) == -1) {
647 648
	message(MESS_ERROR, "Could not lock file %s for reading\n",
			configFile);
649
	}
650 651 652 653 654 655 656
    if (fstat(fd, &sb)) {
	message(MESS_ERROR, "fstat of %s failed: %s\n", configFile,
		strerror(errno));
	close(fd);
	return 1;
    }
    if (!S_ISREG(sb.st_mode)) {
657 658
	message(MESS_DEBUG,
		"Ignoring %s because it's not a regular file.\n",
659 660 661 662
		configFile);
	close(fd);
	return 0;
    }
663 664 665
    
	if (!(pw = getpwuid(getuid()))) {
		message(MESS_ERROR, "Logrotate UID is not in passwd file.\n");
666
		close(fd);
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
		return 1;
	}

	if (getuid() == ROOT_UID) {
		if ((sb.st_mode & 07533) != 0400) {
			message(MESS_DEBUG,
				"Ignoring %s because of bad file mode.\n",
				configFile);
			close(fd);
			return 0;
		}

		if ((pw = getpwnam("root")) == NULL) {
			message(MESS_DEBUG,
				"Ignoring %s because there's no password entry for the owner.\n",
				configFile);
			close(fd);
			return 0;
		}

		if (sb.st_uid != ROOT_UID && (pw == NULL ||
				sb.st_uid != pw->pw_uid ||
				strcmp("root", pw->pw_name) != 0)) {
			message(MESS_DEBUG,
				"Ignoring %s because the file owner is wrong (should be root).\n",
				configFile);
			close(fd);
			return 0;
		}
	}
697

698
	length = sb.st_size;
699

700 701 702 703 704 705 706
	if (length > 0xffffff) {
		message(MESS_ERROR, "file %s too large, probably not a config file.\n",
				configFile);
		close(fd);
		return 1;
	}   

707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
	/* We can't mmap empty file... */
	if (length == 0) {
		message(MESS_DEBUG,
			"Ignoring %s because it's empty.\n",
			configFile);
		close(fd);
		return 0;
	}

#ifdef MAP_POPULATE
 	buf = mmap(NULL, (size_t) length, PROT_READ,
 			MAP_PRIVATE | MAP_POPULATE, fd, (off_t) 0);
#else /* MAP_POPULATE */
	buf = mmap(NULL, (size_t) length, PROT_READ,
			MAP_PRIVATE, fd, (off_t) 0);
#endif /* MAP_POPULATE */

724 725 726 727 728 729
	if (buf == MAP_FAILED) {
		message(MESS_ERROR, "Error mapping config file %s: %s\n",
				configFile, strerror(errno));
		close(fd);
		return 1;
	}
730

731
#ifdef MADV_DONTFORK
732 733
	madvise(buf, (size_t)(length + 2),
			MADV_SEQUENTIAL | MADV_WILLNEED | MADV_DONTFORK);
734 735 736 737
#else /* MADV_DONTFORK */
	madvise(buf, (size_t)(length + 2),
			MADV_SEQUENTIAL | MADV_WILLNEED);
#endif /* MADV_DONTFORK */
738 739 740

    message(MESS_DEBUG, "reading config file %s\n", configFile);

741 742 743 744 745
	start = buf;
    for (start = buf; start - buf < length; start++) {
	if (key) {
		free(key);
		key = NULL;
746
	}
747 748 749 750 751 752 753 754
	switch (state) {
		case STATE_DEFAULT:
			if (isblank(*start))
				continue;
			/* Skip comment */
			if (*start == '#') {
				state = STATE_SKIP_LINE;
				continue;
755
			}
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
			
			if (isalpha(*start)) {
				if ((key = isolateWord(&start, &buf, length)) == NULL)
					continue;
				if (!strcmp(key, "compress")) {
					newlog->flags |= LOG_FLAG_COMPRESS;
				} else if (!strcmp(key, "nocompress")) {
					newlog->flags &= ~LOG_FLAG_COMPRESS;
				} else if (!strcmp(key, "compress")) {
					newlog->flags |= LOG_FLAG_COMPRESS;
				} else if (!strcmp(key, "nocompress")) {
					newlog->flags &= ~LOG_FLAG_COMPRESS;
				} else if (!strcmp(key, "delaycompress")) {
					newlog->flags |= LOG_FLAG_DELAYCOMPRESS;
				} else if (!strcmp(key, "nodelaycompress")) {
					newlog->flags &= ~LOG_FLAG_DELAYCOMPRESS;
				} else if (!strcmp(key, "shred")) {
					newlog->flags |= LOG_FLAG_SHRED;
				} else if (!strcmp(key, "noshred")) { 
					newlog->flags &= ~LOG_FLAG_SHRED;
				} else if (!strcmp(key, "sharedscripts")) {
					newlog->flags |= LOG_FLAG_SHAREDSCRIPTS;
				} else if (!strcmp(key, "nosharedscripts")) {
					newlog->flags &= ~LOG_FLAG_SHAREDSCRIPTS;
				} else if (!strcmp(key, "copytruncate")) {
					newlog->flags |= LOG_FLAG_COPYTRUNCATE;
				} else if (!strcmp(key, "nocopytruncate")) {
					newlog->flags &= ~LOG_FLAG_COPYTRUNCATE;
				} else if (!strcmp(key, "copy")) {
					newlog->flags |= LOG_FLAG_COPY;
				} else if (!strcmp(key, "nocopy")) {
					newlog->flags &= ~LOG_FLAG_COPY;
				} else if (!strcmp(key, "ifempty")) {
					newlog->flags |= LOG_FLAG_IFEMPTY;
				} else if (!strcmp(key, "notifempty")) {
					newlog->flags &= ~LOG_FLAG_IFEMPTY;
				} else if (!strcmp(key, "dateext")) {
					newlog->flags |= LOG_FLAG_DATEEXT;
				} else if (!strcmp(key, "nodateext")) {
					newlog->flags &= ~LOG_FLAG_DATEEXT;
				} else if (!strcmp(key, "dateyesterday")) {
					newlog->flags |= LOG_FLAG_DATEYESTERDAY;
				} else if (!strcmp(key, "dateformat")) {
					freeLogItem(dateformat);
					newlog->dateformat = isolateLine(&start, &buf, length);
					if (newlog->dateformat == NULL)
						continue;
				} else if (!strcmp(key, "noolddir")) {
					newlog->oldDir = NULL;
				} else if (!strcmp(key, "mailfirst")) {
					newlog->flags |= LOG_FLAG_MAILFIRST;
				} else if (!strcmp(key, "maillast")) {
					newlog->flags &= ~LOG_FLAG_MAILFIRST;
				} else if (!strcmp(key, "su")) {
					free(key);
					key = isolateLine(&start, &buf, length);
					if (key == NULL)
						continue;

815
					rc = sscanf(key, "%199s %199s%c", createOwner,
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
								createGroup, &foo);
					if (rc == 3) {
						message(MESS_ERROR, "%s:%d extra arguments for "
							"su\n", configFile, lineNum);
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					if (rc > 0) {
						pw = getpwnam(createOwner);
						if (!pw) {
							message(MESS_ERROR, "%s:%d unknown user '%s'\n",
								configFile, lineNum, createOwner);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
						newlog->suUid = pw->pw_uid;
						endpwent();
					}
					if (rc > 1) {
						group = getgrnam(createGroup);
						if (!group) {
							message(MESS_ERROR, "%s:%d unknown group '%s'\n",
								configFile, lineNum, createGroup);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
						newlog->suGid = group->gr_gid;
						endgrent();
					}

					newlog->flags |= LOG_FLAG_SU;
				} else if (!strcmp(key, "create")) {
					free(key);
					key = isolateLine(&start, &buf, length);
					if (key == NULL)
						continue;

866
					rc = sscanf(key, "%o %199s %199s%c", &createMode,
867
							createOwner, createGroup, &foo);
868 869
					/* We support 'create <owner> <group> notation now */
					if (rc == 0) {
870
						rc = sscanf(key, "%199s %199s%c",
871 872 873 874 875 876 877 878
								createOwner, createGroup, &foo);
						/* Simulate that we have read createMode and se it
						 * to NO_MODE. */
						if (rc > 0) {
							createMode = NO_MODE;
							rc += 1;
						}
					}
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
					if (rc == 4) {
						message(MESS_ERROR, "%s:%d extra arguments for "
							"create\n", configFile, lineNum);
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					if (rc > 0)
						newlog->createMode = createMode;

					if (rc > 1) {
						pw = getpwnam(createOwner);
						if (!pw) {
							message(MESS_ERROR, "%s:%d unknown user '%s'\n",
								configFile, lineNum, createOwner);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
						newlog->createUid = pw->pw_uid;
						endpwent();
					}
					if (rc > 2) {
						group = getgrnam(createGroup);
						if (!group) {
							message(MESS_ERROR, "%s:%d unknown group '%s'\n",
								configFile, lineNum, createGroup);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
						newlog->createGid = group->gr_gid;
						endgrent();
					}

					newlog->flags |= LOG_FLAG_CREATE;
				} else if (!strcmp(key, "nocreate")) {
					newlog->flags &= ~LOG_FLAG_CREATE;
927 928
				} else if (!strcmp(key, "size") || !strcmp(key, "minsize") ||
							!strcmp(key, "maxsize")) {
929 930 931 932 933 934
					unsigned long long size = 0;
					char *opt = key;
							
					if ((key = isolateValue(configFile, lineNum, opt, &start,
							&buf, length)) != NULL) {
						int l = strlen(key) - 1;
935
						if (key[l] == 'k' || key[l] == 'K') {
936 937 938 939 940 941 942 943 944
							key[l] = '\0';
							multiplier = 1024;
						} else if (key[l] == 'M') {
							key[l] = '\0';
							multiplier = 1024 * 1024;
						} else if (key[l] == 'G') {
							key[l] = '\0';
							multiplier = 1024 * 1024 * 1024;
						} else if (!isdigit(key[l])) {
945
							free(opt);
946 947 948 949 950 951 952 953 954 955 956 957
							message(MESS_ERROR, "%s:%d unknown unit '%c'\n",
								configFile, lineNum, key[l]);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						} else {
							multiplier = 1;
						}

958
						size = multiplier * strtoull(key, &chptr, 0);
959 960 961
						if (*chptr) {
							message(MESS_ERROR, "%s:%d bad size '%s'\n",
								configFile, lineNum, key);
962
							free(opt);
963 964 965 966 967 968 969
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
970
						if (!strncmp(opt, "size", 4)) {
971 972
						  newlog->criterium = ROT_SIZE;
						  newlog->threshhold = size;
973 974 975
						} else if (!strncmp(opt, "maxsize", 7)) {
						  newlog->maxsize = size;
						} else {
976
						  newlog->minsize = size;
977 978
						}
						free(opt);
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995
					}
					else {
						free(opt);
						continue;
					}
				} else if (!strcmp(key, "shredcycles")) {
					free(key);
					if ((key = isolateValue(configFile, lineNum, "shred cycles", 
							&start, &buf, length)) != NULL) {
						newlog->shred_cycles = strtoul(key, &chptr, 0);
						if (*chptr || newlog->shred_cycles < 0) {
							message(MESS_ERROR, "%s:%d bad shred cycles '%s'\n",
									configFile, lineNum, key);
							goto error;
						}
					}
					else continue;
996 997
				} else if (!strcmp(key, "hourly")) {
					newlog->criterium = ROT_HOURLY;
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
				} else if (!strcmp(key, "daily")) {
					newlog->criterium = ROT_DAYS;
					newlog->threshhold = 1;
				} else if (!strcmp(key, "monthly")) {
					newlog->criterium = ROT_MONTHLY;
				} else if (!strcmp(key, "weekly")) {
					newlog->criterium = ROT_WEEKLY;
				} else if (!strcmp(key, "yearly")) {
					newlog->criterium = ROT_YEARLY;
				} else if (!strcmp(key, "rotate")) {
					free(key);
					if ((key = isolateValue
						(configFile, lineNum, "rotate count", &start,
						&buf, length)) != NULL) {

						newlog->rotateCount = strtoul(key, &chptr, 0);
						if (*chptr || newlog->rotateCount < 0) {
							message(MESS_ERROR,
								"%s:%d bad rotation count '%s'\n",
								configFile, lineNum, key);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
					}
					else continue;
				} else if (!strcmp(key, "start")) {
					free(key);
					if ((key = isolateValue
						(configFile, lineNum, "start count", &start,
						&buf, length)) != NULL) {

						newlog->logStart = strtoul(key, &chptr, 0);
						if (*chptr || newlog->logStart < 0) {
							message(MESS_ERROR, "%s:%d bad start count '%s'\n",
								configFile, lineNum, key);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
					}
					else continue;
				} else if (!strcmp(key, "maxage")) {
					free(key);
					if ((key = isolateValue
						(configFile, lineNum, "maxage count", &start,
						&buf, length)) != NULL) {
						newlog->rotateAge = strtoul(key, &chptr, 0);
						if (*chptr || newlog->rotateAge < 0) {
							message(MESS_ERROR, "%s:%d bad maximum age '%s'\n",
								configFile, lineNum, start);
							if (newlog != defConfig) {
								state = STATE_ERROR;
								continue;
							} else {
								goto error;
							}
						}
					}
					else continue;
				} else if (!strcmp(key, "errors")) {
					message(MESS_DEBUG,
						"%s: %d: the errors directive is deprecated and no longer used.\n",
						configFile, lineNum);
				} else if (!strcmp(key, "mail")) {
					freeLogItem(logAddress);
					if (!(newlog->logAddress = readAddress(configFile, lineNum,
										"mail", &start, &buf, length))) {
						if (newlog != defConfig) {
						state = STATE_ERROR;
						continue;
						} else {
						goto error;
						}
					}
					else continue;
				} else if (!strcmp(key, "nomail")) {
					freeLogItem(logAddress);
				} else if (!strcmp(key, "missingok")) {
					newlog->flags |= LOG_FLAG_MISSINGOK;
				} else if (!strcmp(key, "nomissingok")) {
					newlog->flags &= ~LOG_FLAG_MISSINGOK;
				} else if (!strcmp(key, "prerotate")) {
					freeLogItem (pre);
					scriptStart = start;
					scriptDest = &newlog->pre;
					state = STATE_LOAD_SCRIPT;
				} else if (!strcmp(key, "firstaction")) {
					freeLogItem (first);
					scriptStart = start;
					scriptDest = &newlog->first;
					state = STATE_LOAD_SCRIPT;
				} else if (!strcmp(key, "postrotate")) {
					freeLogItem (post);
					scriptStart = start;
					scriptDest = &newlog->post;
					state = STATE_LOAD_SCRIPT;
				} else if (!strcmp(key, "lastaction")) {
					freeLogItem (last);
					scriptStart = start;
					scriptDest = &newlog->last;
					state = STATE_LOAD_SCRIPT;
1106 1107 1108 1109 1110
				} else if (!strcmp(key, "preremove")) {
					freeLogItem (preremove);
					scriptStart = start;
					scriptDest = &newlog->preremove;
					state = STATE_LOAD_SCRIPT;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
				} else if (!strcmp(key, "tabooext")) {
					if (newlog != defConfig) {
						message(MESS_ERROR,
							"%s:%d tabooext may not appear inside "
							"of log file definition\n", configFile,
							lineNum);
						state = STATE_ERROR;
						continue;
					}
					free(key);
					if ((key = isolateValue(configFile, lineNum, "tabooext", &start,
							&buf, length)) != NULL) {
						endtag = key;
						if (*endtag == '+') {
							endtag++;
							while (isspace(*endtag) && *endtag)
								endtag++;
						} else {
							free_2d_array(tabooExts, tabooCount);
							tabooCount = 0;
							tabooExts = malloc(1);
						}


						while (*endtag) {
							chptr = endtag;
							while (!isspace(*chptr) && *chptr != ',' && *chptr)
								chptr++;

							tabooExts = realloc(tabooExts, sizeof(*tabooExts) *
										(tabooCount + 1));
							tabooExts[tabooCount] = malloc(chptr - endtag + 1);
							strncpy(tabooExts[tabooCount], endtag,
								chptr - endtag);
							tabooExts[tabooCount][chptr - endtag] = '\0';
							tabooCount++;

							endtag = chptr;
							if (*endtag == ',')
1150 1151
								endtag++;
							while (*endtag && isspace(*endtag))
1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
								endtag++;
						}
					}
					else continue;
				} else if (!strcmp(key, "include")) {
					free(key);
					if ((key = isolateValue(configFile, lineNum, "include", &start,
							&buf, length)) != NULL) {

						message(MESS_DEBUG, "including %s\n", key);
						if (++recursion_depth > MAX_NESTING) {
							message(MESS_ERROR, "%s:%d include nesting too deep\n",
									configFile, lineNum);
							--recursion_depth;
							goto error;
						}
						if (readConfigPath(key, newlog)) {
							--recursion_depth;
							goto error;
						}
						--recursion_depth;
					}
					else continue;
				} else if (!strcmp(key, "olddir")) {
					freeLogItem (oldDir);

					if (!(newlog->oldDir = readPath(configFile, lineNum,
									"olddir", &start, &buf, length))) {
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}
1187

1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
#if 0
					if (stat(newlog->oldDir, &sb)) {
						message(MESS_ERROR, "%s:%d error verifying olddir "
							"path %s: %s\n", configFile, lineNum,
							newlog->oldDir, strerror(errno));
						free(newlog->oldDir);
						goto error;
					}

					if (!S_ISDIR(sb.st_mode)) {
						message(MESS_ERROR, "%s:%d olddir path %s is not a "
							"directory\n", configFile, lineNum,
							newlog->oldDir);
						free(newlog->oldDir);
						goto error;
					}
1204
#endif
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
					message(MESS_DEBUG, "olddir is now %s\n", newlog->oldDir);
				} else if (!strcmp(key, "extension")) {
					if ((key = isolateValue
						(configFile, lineNum, "extension name", &start,
							&buf, length)) != NULL) {
						freeLogItem (extension);
						newlog->extension = key;
						key = NULL;
					}
					else continue;

					message(MESS_DEBUG, "extension is now %s\n",
						newlog->extension);

				} else if (!strcmp(key, "compresscmd")) {
					freeLogItem (compress_prog);

					if (!
						(newlog->compress_prog =
							readPath(configFile, lineNum, "compress", &start, &buf, length))) {
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					if (access(newlog->compress_prog, X_OK)) {
						message(MESS_ERROR,
							"%s:%d compression program %s is not an executable file\n",
							configFile, lineNum, newlog->compress_prog);
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					message(MESS_DEBUG, "compress_prog is now %s\n",
						newlog->compress_prog);

				} else if (!strcmp(key, "uncompresscmd")) {
					freeLogItem (uncompress_prog);

					if (!
						(newlog->uncompress_prog =
							readPath(configFile, lineNum, "uncompress",
								&start, &buf, length))) {
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					if (access(newlog->uncompress_prog, X_OK)) {
						message(MESS_ERROR,
							"%s:%d uncompression program %s is not an executable file\n",
							configFile, lineNum, newlog->uncompress_prog);
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					message(MESS_DEBUG, "uncompress_prog is now %s\n",
						newlog->uncompress_prog);

				} else if (!strcmp(key, "compressoptions")) {
					char *options;

					if (newlog->compress_options_list) {
						free(newlog->compress_options_list);
						newlog->compress_options_list = NULL;
						newlog->compress_options_count = 0;
					}

1287
					if (!(options = isolateLine(&start, &buf, length))) {
1288 1289 1290 1291 1292 1293
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
1294
					}
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326

					if (poptParseArgvString(options,
								&newlog->compress_options_count,
								&newlog->compress_options_list)) {
						message(MESS_ERROR,
							"%s:%d invalid compression options\n",
							configFile, lineNum);
						free(options);
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
					}

					message(MESS_DEBUG, "compress_options is now %s\n",
						options);
					free(options);
				} else if (!strcmp(key, "compressext")) {
					freeLogItem (compress_ext);

					if (!
						(newlog->compress_ext =
							readPath(configFile, lineNum, "compress-ext",
								&start, &buf, length))) {
						if (newlog != defConfig) {
							state = STATE_ERROR;
							continue;
						} else {
							goto error;
						}
1327
					}
1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338

					message(MESS_DEBUG, "compress_ext is now %s\n",
						newlog->compress_ext);
				} else {
					message(MESS_ERROR, "%s:%d unknown option '%s' "
						"-- ignoring line\n", configFile, lineNum, key);
					if (*start != '\n')
						state = STATE_SKIP_LINE;
				}
				free(key);
				key = NULL;
1339 1340 1341 1342 1343 1344
			} else if (*start == '/' || *start == '"' || *start == '\''
#ifdef GLOB_TILDE
                                                                           || *start == '~'
#endif
                                                                           ) {
				in_config = 0;
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
				if (newlog != defConfig) {
					message(MESS_ERROR, "%s:%d unexpected log filename\n",
						configFile, lineNum);
					state = STATE_ERROR;
					continue;
				}

				/* If no compression options were found in config file, set
				default values */
				if (!newlog->compress_prog)
					newlog->compress_prog = strdup(COMPRESS_COMMAND);
				if (!newlog->uncompress_prog)
					newlog->uncompress_prog = strdup(UNCOMPRESS_COMMAND);
				if (!newlog->compress_ext)
					newlog->compress_ext = strdup(COMPRESS_EXT);

				/* Allocate a new logInfo structure and insert it into the logs
				queue, copying the actual values from defConfig */
				if ((newlog = newLogInfo(defConfig)) == NULL)
					goto error;

				endtag = start;
1367
				while (endtag - buf < length && *endtag != '{' && *endtag != '}' && *endtag != '\0') {
1368 1369 1370
					endtag++;}
				if (endtag - buf > length)
					continue;
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
				if (*endtag == '}') {
					message(MESS_ERROR, "%s:%d unexpected } (missing previous '{')\n", configFile,
						lineNum);
					goto error;
				}
				if (*endtag == '{') {
					in_config = 1;
				}
				else {
					message(MESS_ERROR, "%s:%d missing '{' after log files definition\n", configFile,
						lineNum);
					goto error;
				}
1384 1385 1386 1387 1388 1389 1390
				char *key = strndup(start, endtag - start);
				start = endtag;

				if (poptParseArgvString(key, &argc, &argv)) {
				message(MESS_ERROR, "%s:%d error parsing filename\n",
					configFile, lineNum);
				free(key);
1391
				goto error;
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
				} else if (argc < 1) {
				message(MESS_ERROR,
					"%s:%d { expected after log file name(s)\n",
					configFile, lineNum);
				free(key);
				goto error;
				}

				newlog->files = NULL;
				newlog->numFiles = 0;
				for (argNum = 0; argNum < argc && logerror != 1; argNum++) {
				if (globerr_msg) {
					free(globerr_msg);
					globerr_msg = NULL;
				}
					
1408 1409 1410 1411 1412
				rc = glob(argv[argNum], GLOB_NOCHECK
#ifdef GLOB_TILDE
                                                        | GLOB_TILDE
#endif 
                                                    , globerr, &globResult);
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
				if (rc == GLOB_ABORTED) {
					if (newlog->flags & LOG_FLAG_MISSINGOK) {
						continue;
					}

				/* We don't yet know whether this stanza has "missingok"
					* set, so store the error message for later. */
					rc = asprintf(&globerr_msg, "%s:%d glob failed for %s: %s\n",
						configFile, lineNum, argv[argNum], strerror(glob_errno));
					if (rc == -1)
					globerr_msg = NULL;
					
					globResult.gl_pathc = 0;
				}

				newlog->files =
					realloc(newlog->files,
						sizeof(*newlog->files) * (newlog->numFiles +
									globResult.
									gl_pathc));

				for (i = 0; i < globResult.gl_pathc; i++) {
					/* if we glob directories we can get false matches */
					if (!lstat(globResult.gl_pathv[i], &sb) &&
					S_ISDIR(sb.st_mode)) {
						continue;
					}

					for (log = logs.tqh_first; log != NULL;
						log = log->list.tqe_next) {
					for (k = 0; k < log->numFiles; k++) {
						if (!strcmp(log->files[k],
							globResult.gl_pathv[i])) {
						message(MESS_ERROR,
							"%s:%d duplicate log entry for %s\n",
							configFile, lineNum,
							globResult.gl_pathv[i]);
						logerror = 1;
						goto duperror;
						}
					}
					}

					newlog->files[newlog->numFiles] =
					strdup(globResult.gl_pathv[i]);
					newlog->numFiles++;
				}
		duperror:
				globfree(&globResult);
				}

				newlog->pattern = key;

				free(argv);

			} else if (*start == '}') {
				if (newlog == defConfig) {
					message(MESS_ERROR, "%s:%d unexpected }\n", configFile,
						lineNum);
					goto error;
				}
1474 1475 1476 1477 1478 1479
				if (!in_config) {
					message(MESS_ERROR, "%s:%d unexpected } (missing previous '{')\n", configFile,
						lineNum);
					goto error;
				}
				in_config = 0;
1480 1481 1482 1483 1484 1485
			if (globerr_msg) {
				if (!(newlog->flags & LOG_FLAG_MISSINGOK))
					message(MESS_ERROR, "%s", globerr_msg);
				free(globerr_msg);
				globerr_msg = NULL;
				if (!(newlog->flags & LOG_FLAG_MISSINGOK))
1486
					goto error;
1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
				}

				if (newlog->oldDir) {
				for (i = 0; i < newlog->numFiles; i++) {
					char *ld;
					dirName = ourDirName(newlog->files[i]);
					if (stat(dirName, &sb2)) {
					message(MESS_ERROR,
						"%s:%d error verifying log file "
						"path %s: %s\n", configFile, lineNum,
						dirName, strerror(errno));
					free(dirName);
					goto error;
					}
					ld = alloca(strlen(dirName) + strlen(newlog->oldDir) +
						2);
					sprintf(ld, "%s/%s", dirName, newlog->oldDir);
					free(dirName);

					if (newlog->oldDir[0] != '/')
					dirName = ld;
					else
					dirName = newlog->oldDir;
					if (stat(dirName, &sb)) {
					message(MESS_ERROR, "%s:%d error verifying olddir "
						"path %s: %s\n", configFile, lineNum,
						dirName, strerror(errno));
					goto error;
					}

					if (sb.st_dev != sb2.st_dev) {
					message(MESS_ERROR,
						"%s:%d olddir %s and log file %s "
						"are on different devices\n", configFile,
						lineNum, newlog->oldDir, newlog->files[i]);
					goto error;
					}
				}
				}

				newlog = defConfig;
				state = STATE_DEFINITION_END;
			} else if (*start != '\n') {
				message(MESS_ERROR, "%s:%d lines must begin with a keyword "
					"or a filename (possibly in double quotes)\n",
					configFile, lineNum);
					state = STATE_SKIP_LINE;
1534
			}
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
			break;
		case STATE_SKIP_LINE:
		case STATE_SKIP_LINE | STATE_SKIP_CONFIG:
			if (*start == '\n')
				state = state & STATE_SKIP_CONFIG ? STATE_SKIP_CONFIG : STATE_DEFAULT;
			break;
		case STATE_SKIP_LINE | STATE_LOAD_SCRIPT:
			if (*start == '\n')
				state = STATE_LOAD_SCRIPT;
			break;
		case STATE_SKIP_LINE | STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG:
			if (*start == '\n')
				state = STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG;
			break;
		case STATE_DEFINITION_END:
		case STATE_DEFINITION_END | STATE_SKIP_CONFIG:
			if (isblank(*start))
				continue;
			if (*start != '\n') {
				message(MESS_ERROR, "%s:%d, unexpected text after }\n",
					configFile, lineNum);
				state = STATE_SKIP_LINE | (state & STATE_SKIP_CONFIG ? STATE_SKIP_CONFIG : 0);
1557
			}
1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
			else
				state = state & STATE_SKIP_CONFIG ? STATE_SKIP_CONFIG : STATE_DEFAULT;
			break;
		case STATE_ERROR:
			assert(newlog != defConfig);

			message(MESS_ERROR, "found error in %s, skipping\n",
				newlog->pattern ? newlog->pattern : "log config");

			state = STATE_SKIP_CONFIG;
			break;
		case STATE_LOAD_SCRIPT:
		case STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG:
			if ((key = isolateWord(&start, &buf, length)) == NULL)
				continue;

			if (strcmp(key, "endscript") == 0) {
				if (state & STATE_SKIP_CONFIG) {
					state = STATE_SKIP_CONFIG;
				}
				else {
					endtag = start - 9;
					while (*endtag != '\n')
					endtag--;
					endtag++;
					*scriptDest = malloc(endtag - scriptStart + 1);
					strncpy(*scriptDest, scriptStart,
						endtag - scriptStart);
					(*scriptDest)[endtag - scriptStart] = '\0';

					scriptDest = NULL;
					scriptStart = NULL;
				}
				state = state & STATE_SKIP_CONFIG ? STATE_SKIP_CONFIG : STATE_DEFAULT;
1592
			}
1593 1594 1595 1596
			else {
				state = (*start == '\n' ? 0 : STATE_SKIP_LINE) |
					STATE_LOAD_SCRIPT |
					(state & STATE_SKIP_CONFIG ? STATE_SKIP_CONFIG : 0);
1597
			}
1598 1599 1600 1601 1602 1603
			break;
		case STATE_SKIP_CONFIG:
			if (*start == '}') {
				state = STATE_DEFAULT;
				freeTailLogs(1);
				newlog = defConfig;
1604
			}
1605 1606 1607 1608 1609 1610
			else {
				if ((key = isolateWord(&start, &buf, length)) == NULL)
					continue;
				if (
					(strcmp(key, "postrotate") == 0) ||
					(strcmp(key, "prerotate") == 0) ||
1611 1612 1613
					(strcmp(key, "firstaction") == 0) ||
					(strcmp(key, "lastaction") == 0) ||
					(strcmp(key, "preremove") == 0)
1614 1615 1616 1617
					) {
					state = STATE_LOAD_SCRIPT | STATE_SKIP_CONFIG;
				}
				else {
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
					/* isolateWord moves the "start" pointer.
					 * If we have a line like
					 *    rotate 5 
					 * after isolateWord "start" points to "5" and it
					 * is OK to skip the line, but if we have a line
					 * like the following
					 *    nocompress
					 * after isolateWord "start" points to "\n". In
					 * this case if we skip a line, we skip the next 
					 * line, not the current "nocompress" one, 
					 * because in the for cycle the "start"
					 * pointer is increased by one and, after this, 
					 * "start" points to the beginning of the next line.
					*/
					if (*start != '\n') {
						state = STATE_SKIP_LINE | STATE_SKIP_CONFIG;
					}
1635 1636 1637
				}
				free(key);
				key = NULL;
1638
			}
1639 1640 1641 1642 1643 1644 1645
			break;
	}
	if (key) {
		free(key);
		key = NULL;
	}
	if (*start == '\n') {
1646 1647
	    lineNum++;
	}
1648

1649
    }
1650
 
1651 1652
    if (scriptStart) {
	message(MESS_ERROR,
1653
		"%s:prerotate, postrotate or preremove without endscript\n",
1654
		configFile);
1655
	goto error;
1656
    }
1657 1658

	munmap(buf, (size_t) length);
1659
	close(fd);
1660
    return 0;
1661
error:
1662 1663 1664
	if (key)
		free(key);
	munmap(buf, (size_t) length);
1665 1666
	close(fd);
    return 1;
1667
}