2 * Copyright 2007-08,2010,2014 Red Hat Inc., Durham, North Carolina.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Steve Grubb <sgrubb@redhat.com>
35 #include "audispd-config.h"
38 /* Local prototypes */
49 int (*parser)(struct nv_pair *, int, daemon_conf_t *);
59 static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
61 static int nv_split(char *buf, struct nv_pair *nv);
62 static const struct kw_pair *kw_lookup(const char *val);
63 static int q_depth_parser(struct nv_pair *nv, int line,
64 daemon_conf_t *config);
65 static int name_format_parser(struct nv_pair *nv, int line,
66 daemon_conf_t *config);
67 static int name_parser(struct nv_pair *nv, int line,
68 daemon_conf_t *config);
69 static int overflow_action_parser(struct nv_pair *nv, int line,
70 daemon_conf_t *config);
71 static int priority_boost_parser(struct nv_pair *nv, int line,
72 daemon_conf_t *config);
73 static int max_restarts_parser(struct nv_pair *nv, int line,
74 daemon_conf_t *config);
75 static int sanity_check(daemon_conf_t *config, const char *file);
77 static const struct kw_pair keywords[] =
79 {"q_depth", q_depth_parser, 0 },
80 {"name_format", name_format_parser, 0 },
81 {"name", name_parser, 0 },
82 {"overflow_action", overflow_action_parser, 0 },
83 {"priority_boost", priority_boost_parser, 0 },
84 {"max_restarts", max_restarts_parser, 0 },
88 static const struct nv_list node_name_formats[] =
91 {"hostname", N_HOSTNAME },
93 {"numeric", N_NUMERIC },
98 static const struct nv_list overflow_actions[] =
100 {"ignore", O_IGNORE },
101 {"syslog", O_SYSLOG },
102 {"suspend", O_SUSPEND },
103 {"single", O_SINGLE },
109 * Set everything to its default value
111 void clear_config(daemon_conf_t *config)
113 config->q_depth = 80;
114 config->overflow_action = O_SYSLOG;
115 config->priority_boost = 4;
116 config->max_restarts = 10;
117 config->node_name_format = N_NONE;
121 int load_config(daemon_conf_t *config, const char *file)
123 int fd, rc, mode, lineno = 1;
128 clear_config(config);
132 rc = open(file, mode);
134 if (errno != ENOENT) {
135 audit_msg(LOG_ERR, "Error opening %s (%s)", file,
139 audit_msg(LOG_WARNING,
140 "Config file %s doesn't exist, skipping", file);
145 /* check the file's permissions: owned by root, not world writable,
148 if (fstat(fd, &st) < 0) {
149 audit_msg(LOG_ERR, "Error fstat'ing config file (%s)",
154 if (st.st_uid != 0) {
155 audit_msg(LOG_ERR, "Error - %s isn't owned by root",
160 if ((st.st_mode & S_IWOTH) == S_IWOTH) {
161 audit_msg(LOG_ERR, "Error - %s is world writable",
166 if (!S_ISREG(st.st_mode)) {
167 audit_msg(LOG_ERR, "Error - %s is not a regular file",
173 /* it's ok, read line by line */
174 f = fdopen(fd, "rm");
176 audit_msg(LOG_ERR, "Error - fdopen failed (%s)",
182 while (get_line(f, buf, sizeof(buf), &lineno, file)) {
183 // convert line into name-value pair
184 const struct kw_pair *kw;
186 rc = nv_split(buf, &nv);
190 case 1: // not the right number of tokens.
192 "Wrong number of arguments for line %d in %s",
195 case 2: // no '=' sign
197 "Missing equal sign for line %d in %s",
200 default: // something else went wrong...
202 "Unknown error for line %d in %s",
206 if (nv.name == NULL) {
210 if (nv.value == NULL) {
213 "Not processing any more lines in %s", file);
217 /* identify keyword or error */
218 kw = kw_lookup(nv.name);
219 if (kw->name == NULL) {
221 "Unknown keyword \"%s\" in line %d of %s",
222 nv.name, lineno, file);
227 /* Check number of options */
228 if (kw->max_options == 0 && nv.option != NULL) {
230 "Keyword \"%s\" has invalid option "
231 "\"%s\" in line %d of %s",
232 nv.name, nv.option, lineno, file);
237 /* dispatch to keyword's local parser */
238 rc = kw->parser(&nv, lineno, config);
241 return 1; // local parser puts message out
249 return sanity_check(config, file);
253 static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
258 while (fgets_unlocked(buf, size, f)) {
260 char *ptr = strchr(buf, 0x0a);
266 // Reset and start with the next line
268 *lineno = *lineno + 1;
270 // If a line is too long skip it.
271 // Only output 1 warning
274 "Skipping line %d in %s: too long",
282 static int nv_split(char *buf, struct nv_pair *nv)
284 /* Get the name part */
285 char *ptr, *saved = NULL;
290 ptr = strtok_r(buf, " ", &saved);
292 return 0; /* If there's nothing, go to next line */
294 return 0; /* If there's a comment, go to next line */
297 /* Check for a '=' */
298 ptr = strtok_r(NULL, " ", &saved);
301 if (strcmp(ptr, "=") != 0)
305 ptr = strtok_r(NULL, " ", &saved);
310 /* See if there's an option */
311 ptr = strtok_r(NULL, " ", &saved);
315 /* Make sure there's nothing else */
316 ptr = strtok_r(NULL, " ", &saved);
321 /* Everything is OK */
325 static const struct kw_pair *kw_lookup(const char *val)
328 while (keywords[i].name != NULL) {
329 if (strcasecmp(keywords[i].name, val) == 0)
336 static int q_depth_parser(struct nv_pair *nv, int line,
337 daemon_conf_t *config)
339 const char *ptr = nv->value;
342 /* check that all chars are numbers */
343 for (i=0; ptr[i]; i++) {
344 if (!isdigit(ptr[i])) {
346 "Value %s should only be numbers - line %d",
352 /* convert to unsigned long */
354 i = strtoul(nv->value, NULL, 10);
357 "Error converting string to a number (%s) - line %d",
358 strerror(errno), line);
362 audit_msg(LOG_ERR, "q_depth must be 99999 or less");
370 static int name_format_parser(struct nv_pair *nv, int line,
371 daemon_conf_t *config)
375 for (i=0; node_name_formats[i].name != NULL; i++) {
376 if (strcasecmp(nv->value, node_name_formats[i].name) == 0) {
377 config->node_name_format = node_name_formats[i].option;
381 audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
385 static int name_parser(struct nv_pair *nv, int line,
386 daemon_conf_t *config)
388 if (nv->value == NULL)
391 config->name = strdup(nv->value);
395 static int overflow_action_parser(struct nv_pair *nv, int line,
396 daemon_conf_t *config)
400 for (i=0; overflow_actions[i].name != NULL; i++) {
401 if (strcasecmp(nv->value, overflow_actions[i].name) == 0) {
402 config->overflow_action = overflow_actions[i].option;
406 audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
410 static int priority_boost_parser(struct nv_pair *nv, int line,
411 struct daemon_conf *config)
413 const char *ptr = nv->value;
416 audit_msg(LOG_DEBUG, "priority_boost_parser called with: %s",
419 /* check that all chars are numbers */
420 for (i=0; ptr[i]; i++) {
421 if (!isdigit(ptr[i])) {
423 "Value %s should only be numbers - line %d",
428 /* convert to unsigned int */
430 i = strtoul(nv->value, NULL, 10);
433 "Error converting string to a number (%s) - line %d",
434 strerror(errno), line);
437 /* Check its range */
440 "Error - converted number (%s) is too large - line %d",
444 config->priority_boost = (unsigned int)i;
448 static int max_restarts_parser(struct nv_pair *nv, int line,
449 struct daemon_conf *config)
451 const char *ptr = nv->value;
454 audit_msg(LOG_DEBUG, "max_restarts_parser called with: %s",
457 /* check that all chars are numbers */
458 for (i=0; ptr[i]; i++) {
459 if (!isdigit(ptr[i])) {
461 "Value %s should only be numbers - line %d",
466 /* convert to unsigned int */
468 i = strtoul(nv->value, NULL, 10);
471 "Error converting string to a number (%s) - line %d",
472 strerror(errno), line);
475 /* Check its range */
478 "Error - converted number (%s) is too large - line %d",
482 config->max_restarts = (unsigned int)i;
487 * This function is where we do the integrated check of the audispd config
488 * options. At this point, all fields have been read. Returns 0 if no
489 * problems and 1 if problems detected.
491 static int sanity_check(daemon_conf_t *config, const char *file)
494 if (config->node_name_format == N_USER && config->name == NULL) {
496 "Error - node_name_format is user supplied but none given (%s)",
503 void free_config(daemon_conf_t *config)
505 free((void *)config->name);