/* * conf_lookup -- lookup an entry in the config file */ void * conf_lookup(const char *lhs) { struct confinfo *cp = lut_lookup(Conflut, lhs); if (cp != NULL) err_fileline(Confname, cp->cf_lineno); return (cp); }
/* * conf_opts -- return the parsed opts for an entry */ struct opts * conf_opts(const char *lhs) { struct confinfo *cp = lut_lookup(Conflut, lhs); if (cp != NULL) { if (cp->cf_opts) return (cp->cf_opts); /* already parsed */ err_fileline(Confname, cp->cf_lineno); cp->cf_opts = opts_parse(cp->cf_args, OPTF_CONF); return (cp->cf_opts); } return (opts_parse(NULL, OPTF_CONF)); }
/* * scan the memory image of a file * returns: 0: error, 1: ok, 3: -P option found */ static int conf_scan(const char *fname, char *buf, int buflen, int timescan, struct opts *cliopts) { int ret = 1; int lineno = 0; char *line; char *eline; char *ebuf; char *entry, *comment; ebuf = &buf[buflen]; if (buf[buflen - 1] != '\n') err(EF_WARN|EF_FILE, "file %s doesn't end with newline, " "last line ignored.", fname); for (line = buf; line < ebuf; line = eline) { char *ap; struct opts *opts = NULL; struct confinfo *cp; lineno++; err_fileline(fname, lineno); eline = line; comment = NULL; for (; eline < ebuf; eline++) { /* check for continued lines */ if (comment == NULL && *eline == '\\' && eline + 1 < ebuf && *(eline + 1) == '\n') { *eline = ' '; *(eline + 1) = ' '; lineno++; err_fileline(fname, lineno); continue; } /* check for comments */ if (comment == NULL && *eline == '#') { *eline = '\0'; comment = (eline + 1); continue; } /* check for end of line */ if (*eline == '\n') break; } if (comment >= ebuf) comment = NULL; if (eline >= ebuf) { /* discard trailing unterminated line */ continue; } *eline++ = '\0'; /* * now we have the entry, if any, at "line" * and the comment, if any, at "comment" */ /* entry is first token */ entry = nexttok(&line); if (entry == NULL) { /* it's just a comment line */ if (!timescan) fillconflist(lineno, entry, NULL, comment, 0); continue; } if (strcmp(entry, "logadm-version") == 0) { /* * we somehow opened some future format * conffile that we likely don't understand. * if the given version is "1" then go on, * otherwise someone is mixing versions * and we can't help them other than to * print an error and exit. */ if ((entry = nexttok(&line)) != NULL && strcmp(entry, "1") != 0) err(0, "%s version not supported " "by this version of logadm.", fname); continue; } /* form an argv array */ ArgsI = 0; while (ap = nexttok(&line)) fillargs(ap); Args[ArgsI] = NULL; LOCAL_ERR_BEGIN { if (SETJMP) { err(EF_FILE, "cannot process invalid entry %s", entry); ret = 0; LOCAL_ERR_BREAK; } if (timescan) { /* append to config options */ cp = lut_lookup(Conflut, entry); if (cp == NULL) { /* orphaned entry */ if (opts_count(cliopts, "v")) err(EF_FILE, "stale timestamp " "for %s", entry); LOCAL_ERR_BREAK; } opts = cp->cf_opts; } opts = opts_parse(opts, Args, OPTF_CONF); if (!timescan) { fillconflist(lineno, entry, opts, comment, 0); } LOCAL_ERR_END } if (ret == 1 && opts && opts_optarg(opts, "P") != NULL) ret = 3; } err_fileline(NULL, 0); return (ret); }
/* * conf_open -- open the configuration file, lock it if we have write perms */ void conf_open(const char *fname, int needwrite) { struct stat stbuf; int lineno = 0; char *line; char *eline; char *ebuf; char *comment; Confname = fname; Confentries = fn_list_new(NULL); /* special case this so we don't even try locking the file */ if (strcmp(Confname, "/dev/null") == 0) return; if ((Conffd = open(Confname, (needwrite) ? O_RDWR : O_RDONLY)) < 0) err(EF_SYS, "%s", Confname); if (fstat(Conffd, &stbuf) < 0) err(EF_SYS, "fstat on %s", Confname); if (needwrite && lockf(Conffd, F_LOCK, 0) < 0) err(EF_SYS, "lockf on %s", Confname); if (stbuf.st_size == 0) return; /* empty file, don't bother parsing it */ if ((Confbuf = (char *)mmap(0, stbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, Conffd, 0)) == (char *)-1) err(EF_SYS, "mmap on %s", Confname); Conflen = stbuf.st_size; Confchanged = B_FALSE; ebuf = &Confbuf[Conflen]; if (Confbuf[Conflen - 1] != '\n') err(EF_WARN|EF_FILE, "config file doesn't end with " "newline, last line ignored."); line = Confbuf; while (line < ebuf) { lineno++; err_fileline(Confname, lineno); eline = line; comment = NULL; for (; eline < ebuf; eline++) { /* check for continued lines */ if (comment == NULL && *eline == '\\' && eline + 1 < ebuf && *(eline + 1) == '\n') { *eline = ' '; *(eline + 1) = ' '; lineno++; err_fileline(Confname, lineno); continue; } /* check for comments */ if (comment == NULL && *eline == '#') { *eline = '\0'; comment = (eline + 1); continue; } /* check for end of line */ if (*eline == '\n') break; } if (comment >= ebuf) comment = NULL; if (eline < ebuf) { char *entry; *eline++ = '\0'; /* * now we have the entry, if any, at "line" * and the comment, if any, at "comment" */ /* entry is first token */ if ((entry = nexttok(&line)) != NULL && strcmp(entry, "logadm-version") == 0) { /* * we somehow opened some future format * conffile that we likely don't understand. * if the given version is "1" then go on, * otherwise someone is mixing versions * and we can't help them other than to * print an error and exit. */ if ((entry = nexttok(&line)) != NULL && strcmp(entry, "1") != 0) err(0, "%s version not " "supported by " "this version of logadm.", Confname); } else if (entry) { char *ap; char **args; int i; ArgsI = 0; while (ap = nexttok(&line)) fillargs(ap); if (ArgsI == 0) { /* short entry allowed */ fillconflist(lineno, entry, NULL, NULL, comment, 0); } else { Args[ArgsI++] = NULL; args = MALLOC(sizeof (char *) * ArgsI); for (i = 0; i < ArgsI; i++) args[i] = Args[i]; fillconflist(lineno, entry, args, NULL, comment, 0); } } else fillconflist(lineno, entry, NULL, NULL, comment, 0); } line = eline; } /* * possible future enhancement: go through and mark any entries: * logfile -P <date> * as DELETED if the logfile doesn't exist */ }