Imported Upstream version 2.4.3
[platform/upstream/audit.git] / audisp / audispd-pconfig.c
1 /* audispd-pconfig.c -- 
2  * Copyright 2007,2010 Red Hat Inc., Durham, North Carolina.
3  * All Rights Reserved.
4  *
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.
9  *
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.
14  *
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
18  *
19  * Authors:
20  *   Steve Grubb <sgrubb@redhat.com>
21  * 
22  */
23
24 #include "config.h"
25 #include <string.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <libgen.h>
33 #include "audispd-pconfig.h"
34 #include "private.h"
35
36 /* Local prototypes */
37 struct nv_pair
38 {
39         const char *name;
40         const char *value;
41         const char *option;
42 };
43
44 struct kw_pair 
45 {
46         const char *name;
47         int (*parser)(struct nv_pair *, int, plugin_conf_t *);
48         int max_options;
49 };
50
51 struct nv_list
52
53         const char *name;
54         int option;
55 };
56
57 static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
58                 const char *file);
59 static int nv_split(char *buf, struct nv_pair *nv);
60 static const struct kw_pair *kw_lookup(const char *val);
61 static int active_parser(struct nv_pair *nv, int line, 
62                 plugin_conf_t *config);
63 static int direction_parser(struct nv_pair *nv, int line, 
64                 plugin_conf_t *config);
65 static int path_parser(struct nv_pair *nv, int line, 
66                 plugin_conf_t *config);
67 static int service_type_parser(struct nv_pair *nv, int line, 
68                 plugin_conf_t *config);
69 static int args_parser(struct nv_pair *nv, int line, 
70                 plugin_conf_t *config);
71 static int format_parser(struct nv_pair *nv, int line, 
72                 plugin_conf_t *config);
73 static int sanity_check(plugin_conf_t *config, const char *file);
74
75 static const struct kw_pair keywords[] = 
76 {
77   {"active",                   active_parser,                   0 },
78   {"direction",                direction_parser,                0 },
79   {"path",                     path_parser,                     0 },
80   {"type",                     service_type_parser,             0 },
81   {"args",                     args_parser,                     2 },
82   {"format",                   format_parser,                   0 },
83   { NULL,                      NULL }
84 };
85
86 static const struct nv_list active[] =
87 {
88   {"yes",  A_YES },
89   {"no",   A_NO },
90   { NULL,  0 }
91 };
92
93 static const struct nv_list directions[] =
94 {
95 //  {"in",   D_IN },    FIXME: not supported yet
96   {"out",  D_OUT },
97   { NULL,  0 }
98 };
99
100 static const struct nv_list service_type[] =
101 {
102   {"builtin",  S_BUILTIN },
103   {"always",   S_ALWAYS },
104   { NULL,  0 }
105 };
106
107 static const struct nv_list formats[] =
108 {
109   {"binary",  F_BINARY },
110   {"string",  F_STRING },
111   { NULL,  0 }
112 };
113
114 /*
115  * Set everything to its default value
116 */
117 void clear_pconfig(plugin_conf_t *config)
118 {
119         int i;
120
121         config->active = A_NO;
122         config->direction = D_UNSET;
123         config->path = NULL;
124         config->type = S_ALWAYS;
125         for (i=0; i< (MAX_PLUGIN_ARGS + 2); i++)
126                 config->args[i] = NULL;
127         config->format = F_STRING;
128         config->plug_pipe[0] = -1;
129         config->plug_pipe[1] = -1;
130         config->pid = 0;
131         config->inode = 0;
132         config->checked = 0;
133         config->name = NULL;
134         config->restart_cnt = 0;
135 }
136
137 int load_pconfig(plugin_conf_t *config, char *file)
138 {
139         int fd, rc, mode, lineno = 1;
140         struct stat st;
141         FILE *f;
142         char buf[160];
143
144         clear_pconfig(config);
145
146         /* open the file */
147         mode = O_RDONLY;
148         rc = open(file, mode);
149         if (rc < 0) {
150                 if (errno != ENOENT) {
151                         audit_msg(LOG_ERR, "Error opening %s (%s)", file,
152                                 strerror(errno));
153                         return 1;
154                 }
155                 audit_msg(LOG_WARNING,
156                         "Config file %s doesn't exist, skipping", file);
157                 return 0;
158         }
159         fd = rc;
160
161         /* check the file's permissions: owned by root, not world writable,
162          * not symlink.
163          */
164         if (fstat(fd, &st) < 0) {
165                 audit_msg(LOG_ERR, "Error fstat'ing config file (%s)", 
166                         strerror(errno));
167                 close(fd);
168                 return 1;
169         }
170         if (st.st_uid != 0) {
171                 audit_msg(LOG_ERR, "Error - %s isn't owned by root", 
172                         file);
173                 close(fd);
174                 return 1;
175         }
176         if ((st.st_mode & S_IWOTH) == S_IWOTH) {
177                 audit_msg(LOG_ERR, "Error - %s is world writable", 
178                         file);
179                 close(fd);
180                 return 1;
181         }
182         if (!S_ISREG(st.st_mode)) {
183                 audit_msg(LOG_ERR, "Error - %s is not a regular file", 
184                         file);
185                 close(fd);
186                 return 1;
187         }
188
189         /* it's ok, read line by line */
190         f = fdopen(fd, "rm");
191         if (f == NULL) {
192                 audit_msg(LOG_ERR, "Error - fdopen failed (%s)", 
193                         strerror(errno));
194                 close(fd);
195                 return 1;
196         }
197
198         while (get_line(f, buf, sizeof(buf), &lineno, file)) {
199                 // convert line into name-value pair
200                 const struct kw_pair *kw;
201                 struct nv_pair nv;
202                 rc = nv_split(buf, &nv);
203                 switch (rc) {
204                         case 0: // fine
205                                 break;
206                         case 1: // not the right number of tokens.
207                                 audit_msg(LOG_ERR, 
208                                 "Wrong number of arguments for line %d in %s", 
209                                         lineno, file);
210                                 break;
211                         case 2: // no '=' sign
212                                 audit_msg(LOG_ERR, 
213                                         "Missing equal sign for line %d in %s", 
214                                         lineno, file);
215                                 break;
216                         default: // something else went wrong... 
217                                 audit_msg(LOG_ERR, 
218                                         "Unknown error for line %d in %s", 
219                                         lineno, file);
220                                 break;
221                 }
222                 if (nv.name == NULL) {
223                         lineno++;
224                         continue;
225                 }
226                 if (nv.value == NULL) {
227                         fclose(f);
228                         return 1;
229                 }
230
231                 /* identify keyword or error */
232                 kw = kw_lookup(nv.name);
233                 if (kw->name == NULL) {
234                         audit_msg(LOG_ERR, 
235                                 "Unknown keyword \"%s\" in line %d of %s", 
236                                 nv.name, lineno, file);
237                         fclose(f);
238                         return 1;
239                 }
240
241                 /* Check number of options */
242                 if (kw->max_options == 0 && nv.option != NULL) {
243                         audit_msg(LOG_ERR, 
244                                 "Keyword \"%s\" has invalid option "
245                                 "\"%s\" in line %d of %s", 
246                                 nv.name, nv.option, lineno, file);
247                         fclose(f);
248                         return 1;
249                 }
250
251                 /* dispatch to keyword's local parser */
252                 rc = kw->parser(&nv, lineno, config);
253                 if (rc != 0) {
254                         fclose(f);
255                         return 1; // local parser puts message out
256                 }
257
258                 lineno++;
259         }
260
261         fclose(f);
262         config->name = strdup(basename(file));
263         if (lineno > 1)
264                 return sanity_check(config, file);
265         return 0;
266 }
267
268 static char *get_line(FILE *f, char *buf, unsigned size, int *lineno,
269          const char *file)
270 {
271         int too_long = 0;
272
273         if (fgets_unlocked(buf, size, f)) {
274                 /* remove newline */
275                 char *ptr = strchr(buf, 0x0a);
276                 if (ptr) {
277                         if (!too_long) {
278                                 *ptr = 0;
279                                 return buf;
280                         }
281                         // Reset and start with the next line
282                         too_long = 0;
283                         *lineno = *lineno + 1;
284                 } else {
285                         // If a line is too long skip it.
286                         // Only output 1 warning
287                         if (!too_long)
288                                 audit_msg(LOG_ERR,
289                                         "Skipping line %d in %s: too long",
290                                         *lineno, file);
291                         too_long = 1;
292                 }
293         }
294         return NULL;
295 }
296
297 static int nv_split(char *buf, struct nv_pair *nv)
298 {
299         /* Get the name part */
300         char *ptr, *saved = NULL;
301
302         nv->name = NULL;
303         nv->value = NULL;
304         nv->option = NULL;
305         ptr = strtok_r(buf, " ", &saved);
306         if (ptr == NULL)
307                 return 0; /* If there's nothing, go to next line */
308         if (ptr[0] == '#')
309                 return 0; /* If there's a comment, go to next line */
310         nv->name = ptr;
311
312         /* Check for a '=' */
313         ptr = strtok_r(NULL, " ", &saved);
314         if (ptr == NULL)
315                 return 1;
316         if (strcmp(ptr, "=") != 0)
317                 return 2;
318
319         /* get the value */
320         ptr = strtok_r(NULL, " ", &saved);
321         if (ptr == NULL)
322                 return 1;
323         nv->value = ptr;
324
325         /* See if there's an option */
326         ptr = strtok_r(NULL, " ", &saved);
327         if (ptr) {
328                 nv->option = ptr;
329
330                 /* Make sure there's nothing else */
331                 ptr = strtok_r(NULL, " ", &saved);
332                 if (ptr)
333                         return 1;
334         }
335
336         /* Everything is OK */
337         return 0;
338 }
339
340 static const struct kw_pair *kw_lookup(const char *val)
341 {
342         int i = 0;
343         while (keywords[i].name != NULL) {
344                 if (strcasecmp(keywords[i].name, val) == 0)
345                         break;
346                 i++;
347         }
348         return &keywords[i];
349 }
350  
351 static int active_parser(struct nv_pair *nv, int line, 
352                 plugin_conf_t *config)
353 {
354         int i;
355
356         for (i=0; active[i].name != NULL; i++) {
357                 if (strcasecmp(nv->value, active[i].name) == 0) {
358                         config->active = active[i].option;
359                         return 0;
360                 }
361         }
362         audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
363         return 1;
364 }
365
366 static int direction_parser(struct nv_pair *nv, int line, 
367                 plugin_conf_t *config)
368 {
369         int i;
370
371         for (i=0; directions[i].name != NULL; i++) {
372                 if (strcasecmp(nv->value, directions[i].name) == 0) {
373                         config->direction = directions[i].option;
374                         return 0;
375                 }
376         }
377         audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
378         return 1;
379 }
380
381 static int path_parser(struct nv_pair *nv, int line,
382         plugin_conf_t *config)
383 {
384         char *dir = NULL, *tdir;
385         struct stat buf;
386
387         if (nv->value == NULL) {
388                 config->path = NULL;
389                 return 0;
390         }
391
392         if (strncasecmp(nv->value, "builtin_", 8) == 0) {
393                 config->path = strdup(nv->value);
394                 return 0;
395         }
396
397         /* get dir form name. */
398         tdir = strdup(nv->value);
399         if (tdir)
400                 dir = dirname(tdir);
401         if (dir == NULL || strlen(dir) < 4) { //  '/var' is shortest dirname
402                 audit_msg(LOG_ERR,
403                         "The directory name: %s is too short - line %d",
404                         dir, line);
405                 free(tdir);
406                 return 1;
407         }
408
409         free((void *)tdir);
410         /* If the file exists, see that its regular, owned by root,
411          * and not world anything */
412         if (stat(nv->value, &buf) < 0) {
413                 audit_msg(LOG_ERR, "Unable to stat %s (%s)", nv->value,
414                         strerror(errno));
415                 return 1;
416         }
417         if (!S_ISREG(buf.st_mode)) {
418                 audit_msg(LOG_ERR, "%s is not a regular file", nv->value);
419                 return 1;
420         }
421         if (buf.st_uid != 0) {
422                 audit_msg(LOG_ERR, "%s is not owned by root", nv->value);
423                 return 1;
424         }
425         if ((buf.st_mode & (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP)) !=
426                            (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP)) {
427                 audit_msg(LOG_ERR, "%s permissions should be 0750", nv->value);
428                 return 1;
429         }
430         free((void *)config->path);
431         config->path = strdup(nv->value);
432         config->inode = buf.st_ino;
433         if (config->path == NULL)
434                 return 1;
435         return 0;
436 }
437
438 static int service_type_parser(struct nv_pair *nv, int line, 
439                 plugin_conf_t *config)
440 {
441         int i;
442
443         for (i=0; service_type[i].name != NULL; i++) {
444                 if (strcasecmp(nv->value, service_type[i].name) == 0) {
445                         config->type = service_type[i].option;
446                         return 0;
447                 }
448         }
449         audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
450         return 1;
451 }
452
453 static int args_parser(struct nv_pair *nv, int line,
454         plugin_conf_t *config)
455 {
456         int i;
457
458         for (i=0; i < (MAX_PLUGIN_ARGS + 2); i++) {
459                 free((void *)config->args[i]);
460                 config->args[i] = NULL;
461         }
462
463         config->args[1] = strdup(nv->value);
464         if (nv->option)
465                 config->args[2] = strdup(nv->option);
466         return 0;
467 }
468
469 static int format_parser(struct nv_pair *nv, int line, 
470                 plugin_conf_t *config)
471 {
472         int i;
473
474         for (i=0; formats[i].name != NULL; i++) {
475                 if (strcasecmp(nv->value, formats[i].name) == 0) {
476                         config->format = formats[i].option;
477                         return 0;
478                 }
479         }
480         audit_msg(LOG_ERR, "Option %s not found - line %d", nv->value, line);
481         return 1;
482 }
483
484 /*
485  * This function is where we do the integrated check of the audispd config
486  * options. At this point, all fields have been read. Returns 0 if no
487  * problems and 1 if problems detected.
488  */
489 static int sanity_check(plugin_conf_t *config, const char *file)
490 {
491         /* Error checking */
492         if (config->active == A_YES && config->path == NULL) {
493                 audit_msg(LOG_ERR, 
494                     "Error - plugin (%s) is active but no path given", file);
495                 return 1;
496         }
497         return 0;
498 }
499
500 void free_pconfig(plugin_conf_t *config)
501 {
502         int i;
503
504         if (config == NULL)
505                 return;
506
507         for (i=0; i < (MAX_PLUGIN_ARGS + 2); i++)
508                 free(config->args[i]);
509         if (config->plug_pipe[0] >= 0)
510                 close(config->plug_pipe[0]);
511         if (config->plug_pipe[1] >= 0)
512                 close(config->plug_pipe[1]);
513         free((void *)config->path);
514         free((void *)config->name);
515 }
516