curl_xml plugin: Fixed tautological pointer comparison error.
[collectd.git] / src / configfile.c
1 /**
2  * collectd - src/configfile.c
3  * Copyright (C) 2005-2009  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  *   Sebastian tokkee Harl <sh at tokkee.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "liboconfig/oconfig.h"
27
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31 #include "types_list.h"
32 #include "utils_threshold.h"
33 #include "filter_chain.h"
34
35 #if HAVE_WORDEXP_H
36 # include <wordexp.h>
37 #endif /* HAVE_WORDEXP_H */
38
39 #define ESCAPE_NULL(str) ((str) == NULL ? "(null)" : (str))
40
41 /*
42  * Private types
43  */
44 typedef struct cf_callback
45 {
46         const char  *type;
47         int  (*callback) (const char *, const char *);
48         const char **keys;
49         int    keys_num;
50         struct cf_callback *next;
51 } cf_callback_t;
52
53 typedef struct cf_complex_callback_s
54 {
55         char *type;
56         int (*callback) (oconfig_item_t *);
57         struct cf_complex_callback_s *next;
58 } cf_complex_callback_t;
59
60 typedef struct cf_value_map_s
61 {
62         char *key;
63         int (*func) (const oconfig_item_t *);
64 } cf_value_map_t;
65
66 typedef struct cf_global_option_s
67 {
68         char *key;
69         char *value;
70         char *def;
71 } cf_global_option_t;
72
73 /*
74  * Prototypes of callback functions
75  */
76 static int dispatch_value_typesdb (const oconfig_item_t *ci);
77 static int dispatch_value_plugindir (const oconfig_item_t *ci);
78 static int dispatch_loadplugin (const oconfig_item_t *ci);
79
80 /*
81  * Private variables
82  */
83 static cf_callback_t *first_callback = NULL;
84 static cf_complex_callback_t *complex_callback_head = NULL;
85
86 static cf_value_map_t cf_value_map[] =
87 {
88         {"TypesDB",    dispatch_value_typesdb},
89         {"PluginDir",  dispatch_value_plugindir},
90         {"LoadPlugin", dispatch_loadplugin}
91 };
92 static int cf_value_map_num = STATIC_ARRAY_LEN (cf_value_map);
93
94 static cf_global_option_t cf_global_options[] =
95 {
96         {"BaseDir",     NULL, PKGLOCALSTATEDIR},
97         {"PIDFile",     NULL, PIDFILE},
98         {"Hostname",    NULL, NULL},
99         {"FQDNLookup",  NULL, "false"},
100         {"Interval",    NULL, "10"},
101         {"ReadThreads", NULL, "5"},
102         {"Timeout",     NULL, "2"},
103         {"PreCacheChain",  NULL, "PreCache"},
104         {"PostCacheChain", NULL, "PostCache"}
105 };
106 static int cf_global_options_num = STATIC_ARRAY_LEN (cf_global_options);
107
108 static int cf_default_typesdb = 1;
109
110 /*
111  * Functions to handle register/unregister, search, and other plugin related
112  * stuff
113  */
114 static cf_callback_t *cf_search (const char *type)
115 {
116         cf_callback_t *cf_cb;
117
118         if (type == NULL)
119                 return (NULL);
120
121         for (cf_cb = first_callback; cf_cb != NULL; cf_cb = cf_cb->next)
122                 if (strcasecmp (cf_cb->type, type) == 0)
123                         break;
124
125         return (cf_cb);
126 }
127
128 static int cf_dispatch (const char *type, const char *orig_key,
129                 const char *orig_value)
130 {
131         cf_callback_t *cf_cb;
132         char *key;
133         char *value;
134         int ret;
135         int i;
136
137         DEBUG ("type = %s, key = %s, value = %s",
138                         ESCAPE_NULL(type),
139                         ESCAPE_NULL(orig_key),
140                         ESCAPE_NULL(orig_value));
141
142         if ((cf_cb = cf_search (type)) == NULL)
143         {
144                 WARNING ("Found a configuration for the `%s' plugin, but "
145                                 "the plugin isn't loaded or didn't register "
146                                 "a configuration callback.", type);
147                 return (-1);
148         }
149
150         if ((key = strdup (orig_key)) == NULL)
151                 return (1);
152         if ((value = strdup (orig_value)) == NULL)
153         {
154                 free (key);
155                 return (2);
156         }
157
158         ret = -1;
159
160         for (i = 0; i < cf_cb->keys_num; i++)
161         {
162                 if ((cf_cb->keys[i] != NULL)
163                                 && (strcasecmp (cf_cb->keys[i], key) == 0))
164                 {
165                         ret = (*cf_cb->callback) (key, value);
166                         break;
167                 }
168         }
169
170         if (i >= cf_cb->keys_num)
171                 WARNING ("Plugin `%s' did not register for value `%s'.", type, key);
172
173         free (key);
174         free (value);
175
176         DEBUG ("cf_dispatch: return (%i)", ret);
177
178         return (ret);
179 } /* int cf_dispatch */
180
181 static int dispatch_global_option (const oconfig_item_t *ci)
182 {
183         if (ci->values_num != 1)
184                 return (-1);
185         if (ci->values[0].type == OCONFIG_TYPE_STRING)
186                 return (global_option_set (ci->key, ci->values[0].value.string));
187         else if (ci->values[0].type == OCONFIG_TYPE_NUMBER)
188         {
189                 char tmp[128];
190                 ssnprintf (tmp, sizeof (tmp), "%lf", ci->values[0].value.number);
191                 return (global_option_set (ci->key, tmp));
192         }
193         else if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
194         {
195                 if (ci->values[0].value.boolean)
196                         return (global_option_set (ci->key, "true"));
197                 else
198                         return (global_option_set (ci->key, "false"));
199         }
200
201         return (-1);
202 } /* int dispatch_global_option */
203
204 static int dispatch_value_typesdb (const oconfig_item_t *ci)
205 {
206         int i = 0;
207
208         assert (strcasecmp (ci->key, "TypesDB") == 0);
209
210         cf_default_typesdb = 0;
211
212         if (ci->values_num < 1) {
213                 ERROR ("configfile: `TypesDB' needs at least one argument.");
214                 return (-1);
215         }
216
217         for (i = 0; i < ci->values_num; ++i)
218         {
219                 if (OCONFIG_TYPE_STRING != ci->values[i].type) {
220                         WARNING ("configfile: TypesDB: Skipping %i. argument which "
221                                         "is not a string.", i + 1);
222                         continue;
223                 }
224
225                 read_types_list (ci->values[i].value.string);
226         }
227         return (0);
228 } /* int dispatch_value_typesdb */
229
230 static int dispatch_value_plugindir (const oconfig_item_t *ci)
231 {
232         assert (strcasecmp (ci->key, "PluginDir") == 0);
233         
234         if (ci->values_num != 1)
235                 return (-1);
236         if (ci->values[0].type != OCONFIG_TYPE_STRING)
237                 return (-1);
238
239         plugin_set_dir (ci->values[0].value.string);
240         return (0);
241 }
242
243 static int dispatch_loadplugin (const oconfig_item_t *ci)
244 {
245         int i;
246         uint32_t flags = 0;
247         assert (strcasecmp (ci->key, "LoadPlugin") == 0);
248
249         if (ci->values_num != 1)
250                 return (-1);
251         if (ci->values[0].type != OCONFIG_TYPE_STRING)
252                 return (-1);
253
254         for (i = 0; i < ci->children_num; ++i) {
255                 if (ci->children[i].values_num != 1 ||
256                                 ci->children[i].values[0].type != OCONFIG_TYPE_BOOLEAN) {
257                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
258                         continue;
259                 }
260                 if (strcasecmp(ci->children[i].key, "globals") == 0) {
261                         flags |= PLUGIN_FLAGS_GLOBAL;
262                 } else {
263                         WARNING("Ignoring unknown LoadPlugin option %s for plugin %s", ci->children[i].key, ci->values[0].value.string);
264                 }
265         }
266         return (plugin_load (ci->values[0].value.string, flags));
267 } /* int dispatch_value_loadplugin */
268
269 static int dispatch_value_plugin (const char *plugin, oconfig_item_t *ci)
270 {
271         char  buffer[4096];
272         char *buffer_ptr;
273         int   buffer_free;
274         int i;
275
276         buffer_ptr = buffer;
277         buffer_free = sizeof (buffer);
278
279         for (i = 0; i < ci->values_num; i++)
280         {
281                 int status = -1;
282
283                 if (ci->values[i].type == OCONFIG_TYPE_STRING)
284                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
285                                         ci->values[i].value.string);
286                 else if (ci->values[i].type == OCONFIG_TYPE_NUMBER)
287                         status = ssnprintf (buffer_ptr, buffer_free, " %lf",
288                                         ci->values[i].value.number);
289                 else if (ci->values[i].type == OCONFIG_TYPE_BOOLEAN)
290                         status = ssnprintf (buffer_ptr, buffer_free, " %s",
291                                         ci->values[i].value.boolean
292                                         ? "true" : "false");
293
294                 if ((status < 0) || (status >= buffer_free))
295                         return (-1);
296                 buffer_free -= status;
297                 buffer_ptr  += status;
298         }
299         /* skip the initial space */
300         buffer_ptr = buffer + 1;
301
302         return (cf_dispatch (plugin, ci->key, buffer_ptr));
303 } /* int dispatch_value_plugin */
304
305 static int dispatch_value (const oconfig_item_t *ci)
306 {
307         int ret = -2;
308         int i;
309
310         for (i = 0; i < cf_value_map_num; i++)
311                 if (strcasecmp (cf_value_map[i].key, ci->key) == 0)
312                 {
313                         ret = cf_value_map[i].func (ci);
314                         break;
315                 }
316
317         for (i = 0; i < cf_global_options_num; i++)
318                 if (strcasecmp (cf_global_options[i].key, ci->key) == 0)
319                 {
320                         ret = dispatch_global_option (ci);
321                         break;
322                 }
323
324         return (ret);
325 } /* int dispatch_value */
326
327 static int dispatch_block_plugin (oconfig_item_t *ci)
328 {
329         int i;
330         char *name;
331
332         cf_complex_callback_t *cb;
333
334         if (strcasecmp (ci->key, "Plugin") != 0)
335                 return (-1);
336         if (ci->values_num < 1)
337                 return (-1);
338         if (ci->values[0].type != OCONFIG_TYPE_STRING)
339                 return (-1);
340
341         name = ci->values[0].value.string;
342
343         /* Check for a complex callback first */
344         for (cb = complex_callback_head; cb != NULL; cb = cb->next)
345                 if (strcasecmp (name, cb->type) == 0)
346                         return (cb->callback (ci));
347
348         /* Hm, no complex plugin found. Dispatch the values one by one */
349         for (i = 0; i < ci->children_num; i++)
350         {
351                 if (ci->children[i].children == NULL)
352                         dispatch_value_plugin (name, ci->children + i);
353                 else
354                 {
355                         WARNING ("There is a `%s' block within the "
356                                         "configuration for the %s plugin. "
357                                         "The plugin either only expects "
358                                         "\"simple\" configuration statements "
359                                         "or wasn't loaded using `LoadPlugin'."
360                                         " Please check your configuration.",
361                                         ci->children[i].key, name);
362                 }
363         }
364
365         return (0);
366 }
367
368
369 static int dispatch_block (oconfig_item_t *ci)
370 {
371         if (strcasecmp (ci->key, "LoadPlugin") == 0)
372                 return (dispatch_loadplugin (ci));
373         else if (strcasecmp (ci->key, "Plugin") == 0)
374                 return (dispatch_block_plugin (ci));
375         else if (strcasecmp (ci->key, "Threshold") == 0)
376                 return (ut_config (ci));
377         else if (strcasecmp (ci->key, "Chain") == 0)
378                 return (fc_configure (ci));
379
380         return (0);
381 }
382
383 static int cf_ci_replace_child (oconfig_item_t *dst, oconfig_item_t *src,
384                 int offset)
385 {
386         oconfig_item_t *temp;
387         int i;
388
389         assert (offset >= 0);
390         assert (dst->children_num > offset);
391
392         /* Free the memory used by the replaced child. Usually that's the
393          * `Include "blah"' statement. */
394         temp = dst->children + offset;
395         for (i = 0; i < temp->values_num; i++)
396         {
397                 if (temp->values[i].type == OCONFIG_TYPE_STRING)
398                 {
399                         sfree (temp->values[i].value.string);
400                 }
401         }
402         sfree (temp->values);
403         temp = NULL;
404
405         /* If (src->children_num == 0) the array size is decreased. If offset
406          * is _not_ the last element, (offset < (dst->children_num - 1)), then
407          * we need to move the trailing elements before resizing the array. */
408         if ((src->children_num == 0) && (offset < (dst->children_num - 1)))
409         {
410                 int nmemb = dst->children_num - (offset + 1);
411                 memmove (dst->children + offset, dst->children + offset + 1,
412                                 sizeof (oconfig_item_t) * nmemb);
413         }
414
415         /* Resize the memory containing the children to be big enough to hold
416          * all children. */
417         if (dst->children_num + src->children_num - 1 == 0)
418         {
419                 dst->children_num = 0;
420                 return (0);
421         }
422
423         temp = (oconfig_item_t *) realloc (dst->children,
424                         sizeof (oconfig_item_t)
425                         * (dst->children_num + src->children_num - 1));
426         if (temp == NULL)
427         {
428                 ERROR ("configfile: realloc failed.");
429                 return (-1);
430         }
431         dst->children = temp;
432
433         /* If there are children behind the include statement, and they have
434          * not yet been moved because (src->children_num == 0), then move them
435          * to the end of the list, so that the new children have room before
436          * them. */
437         if ((src->children_num > 0)
438                         && ((dst->children_num - (offset + 1)) > 0))
439         {
440                 int nmemb = dst->children_num - (offset + 1);
441                 int old_offset = offset + 1;
442                 int new_offset = offset + src->children_num;
443
444                 memmove (dst->children + new_offset,
445                                 dst->children + old_offset,
446                                 sizeof (oconfig_item_t) * nmemb);
447         }
448
449         /* Last but not least: If there are new children, copy them to the
450          * memory reserved for them. */
451         if (src->children_num > 0)
452         {
453                 memcpy (dst->children + offset,
454                                 src->children,
455                                 sizeof (oconfig_item_t) * src->children_num);
456         }
457
458         /* Update the number of children. */
459         dst->children_num += (src->children_num - 1);
460
461         return (0);
462 } /* int cf_ci_replace_child */
463
464 static int cf_ci_append_children (oconfig_item_t *dst, oconfig_item_t *src)
465 {
466         oconfig_item_t *temp;
467
468         if ((src == NULL) || (src->children_num == 0))
469                 return (0);
470
471         temp = (oconfig_item_t *) realloc (dst->children,
472                         sizeof (oconfig_item_t)
473                         * (dst->children_num + src->children_num));
474         if (temp == NULL)
475         {
476                 ERROR ("configfile: realloc failed.");
477                 return (-1);
478         }
479         dst->children = temp;
480
481         memcpy (dst->children + dst->children_num,
482                         src->children,
483                         sizeof (oconfig_item_t)
484                         * src->children_num);
485         dst->children_num += src->children_num;
486
487         return (0);
488 } /* int cf_ci_append_children */
489
490 #define CF_MAX_DEPTH 8
491 static oconfig_item_t *cf_read_generic (const char *path, int depth);
492
493 static int cf_include_all (oconfig_item_t *root, int depth)
494 {
495         int i;
496
497         for (i = 0; i < root->children_num; i++)
498         {
499                 oconfig_item_t *new;
500                 oconfig_item_t *old;
501
502                 /* Ignore all blocks, including `Include' blocks. */
503                 if (root->children[i].children_num != 0)
504                         continue;
505
506                 if (strcasecmp (root->children[i].key, "Include") != 0)
507                         continue;
508
509                 old = root->children + i;
510
511                 if ((old->values_num != 1)
512                                 || (old->values[0].type != OCONFIG_TYPE_STRING))
513                 {
514                         ERROR ("configfile: `Include' needs exactly one string argument.");
515                         continue;
516                 }
517
518                 new = cf_read_generic (old->values[0].value.string, depth + 1);
519                 if (new == NULL)
520                         continue;
521
522                 /* Now replace the i'th child in `root' with `new'. */
523                 if (cf_ci_replace_child (root, new, i) < 0)
524                         return (-1);
525
526                 /* ... and go back to the new i'th child. */
527                 --i;
528
529                 sfree (new->values);
530                 sfree (new);
531         } /* for (i = 0; i < root->children_num; i++) */
532
533         return (0);
534 } /* int cf_include_all */
535
536 static oconfig_item_t *cf_read_file (const char *file, int depth)
537 {
538         oconfig_item_t *root;
539
540         assert (depth < CF_MAX_DEPTH);
541
542         root = oconfig_parse_file (file);
543         if (root == NULL)
544         {
545                 ERROR ("configfile: Cannot read file `%s'.", file);
546                 return (NULL);
547         }
548
549         cf_include_all (root, depth);
550
551         return (root);
552 } /* oconfig_item_t *cf_read_file */
553
554 static int cf_compare_string (const void *p1, const void *p2)
555 {
556         return strcmp (*(const char **) p1, *(const char **) p2);
557 }
558
559 static oconfig_item_t *cf_read_dir (const char *dir, int depth)
560 {
561         oconfig_item_t *root = NULL;
562         DIR *dh;
563         struct dirent *de;
564         char **filenames = NULL;
565         int filenames_num = 0;
566         int status;
567         int i;
568
569         assert (depth < CF_MAX_DEPTH);
570
571         dh = opendir (dir);
572         if (dh == NULL)
573         {
574                 char errbuf[1024];
575                 ERROR ("configfile: opendir failed: %s",
576                                 sstrerror (errno, errbuf, sizeof (errbuf)));
577                 return (NULL);
578         }
579
580         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
581         if (root == NULL)
582         {
583                 ERROR ("configfile: malloc failed.");
584                 return (NULL);
585         }
586         memset (root, 0, sizeof (oconfig_item_t));
587
588         while ((de = readdir (dh)) != NULL)
589         {
590                 char   name[1024];
591                 char **tmp;
592
593                 if ((de->d_name[0] == '.') || (de->d_name[0] == 0))
594                         continue;
595
596                 status = ssnprintf (name, sizeof (name), "%s/%s",
597                                 dir, de->d_name);
598                 if ((status < 0) || ((size_t) status >= sizeof (name)))
599                 {
600                         ERROR ("configfile: Not including `%s/%s' because its"
601                                         " name is too long.",
602                                         dir, de->d_name);
603                         for (i = 0; i < filenames_num; ++i)
604                                 free (filenames[i]);
605                         free (filenames);
606                         free (root);
607                         return (NULL);
608                 }
609
610                 ++filenames_num;
611                 tmp = (char **) realloc (filenames,
612                                 filenames_num * sizeof (*filenames));
613                 if (tmp == NULL) {
614                         ERROR ("configfile: realloc failed.");
615                         for (i = 0; i < filenames_num - 1; ++i)
616                                 free (filenames[i]);
617                         free (filenames);
618                         free (root);
619                         return (NULL);
620                 }
621                 filenames = tmp;
622
623                 filenames[filenames_num - 1] = sstrdup (name);
624         }
625
626         qsort ((void *) filenames, filenames_num, sizeof (*filenames),
627                         cf_compare_string);
628
629         for (i = 0; i < filenames_num; ++i)
630         {
631                 oconfig_item_t *temp;
632                 char *name = filenames[i];
633
634                 temp = cf_read_generic (name, depth);
635                 if (temp == NULL)
636                 {
637                         /* An error should already have been reported. */
638                         sfree (name);
639                         continue;
640                 }
641
642                 cf_ci_append_children (root, temp);
643                 sfree (temp->children);
644                 sfree (temp);
645
646                 free (name);
647         }
648
649         free(filenames);
650         return (root);
651 } /* oconfig_item_t *cf_read_dir */
652
653 /* 
654  * cf_read_generic
655  *
656  * Path is stat'ed and either cf_read_file or cf_read_dir is called
657  * accordingly.
658  *
659  * There are two versions of this function: If `wordexp' exists shell wildcards
660  * will be expanded and the function will include all matches found. If
661  * `wordexp' (or, more precisely, it's header file) is not available the
662  * simpler function is used which does not do any such expansion.
663  */
664 #if HAVE_WORDEXP_H
665 static oconfig_item_t *cf_read_generic (const char *path, int depth)
666 {
667         oconfig_item_t *root = NULL;
668         int status;
669         const char *path_ptr;
670         wordexp_t we;
671         size_t i;
672
673         if (depth >= CF_MAX_DEPTH)
674         {
675                 ERROR ("configfile: Not including `%s' because the maximum "
676                                 "nesting depth has been reached.", path);
677                 return (NULL);
678         }
679
680         status = wordexp (path, &we, WRDE_NOCMD);
681         if (status != 0)
682         {
683                 ERROR ("configfile: wordexp (%s) failed.", path);
684                 return (NULL);
685         }
686
687         root = (oconfig_item_t *) malloc (sizeof (oconfig_item_t));
688         if (root == NULL)
689         {
690                 ERROR ("configfile: malloc failed.");
691                 return (NULL);
692         }
693         memset (root, '\0', sizeof (oconfig_item_t));
694
695         /* wordexp() might return a sorted list already. That's not
696          * documented though, so let's make sure we get what we want. */
697         qsort ((void *) we.we_wordv, we.we_wordc, sizeof (*we.we_wordv),
698                         cf_compare_string);
699
700         for (i = 0; i < we.we_wordc; i++)
701         {
702                 oconfig_item_t *temp;
703                 struct stat statbuf;
704
705                 path_ptr = we.we_wordv[i];
706
707                 status = stat (path_ptr, &statbuf);
708                 if (status != 0)
709                 {
710                         char errbuf[1024];
711                         WARNING ("configfile: stat (%s) failed: %s",
712                                         path_ptr,
713                                         sstrerror (errno, errbuf, sizeof (errbuf)));
714                         continue;
715                 }
716
717                 if (S_ISREG (statbuf.st_mode))
718                         temp = cf_read_file (path_ptr, depth);
719                 else if (S_ISDIR (statbuf.st_mode))
720                         temp = cf_read_dir (path_ptr, depth);
721                 else
722                 {
723                         WARNING ("configfile: %s is neither a file nor a "
724                                         "directory.", path);
725                         continue;
726                 }
727
728                 if (temp == NULL) {
729                         oconfig_free (root);
730                         return (NULL);
731                 }
732
733                 cf_ci_append_children (root, temp);
734                 sfree (temp->children);
735                 sfree (temp);
736         }
737
738         wordfree (&we);
739
740         if (root->children == NULL)
741         {
742                 oconfig_free (root);
743                 return (NULL);
744         }
745
746         return (root);
747 } /* oconfig_item_t *cf_read_generic */
748 /* #endif HAVE_WORDEXP_H */
749
750 #else /* if !HAVE_WORDEXP_H */
751 static oconfig_item_t *cf_read_generic (const char *path, int depth)
752 {
753         struct stat statbuf;
754         int status;
755
756         if (depth >= CF_MAX_DEPTH)
757         {
758                 ERROR ("configfile: Not including `%s' because the maximum "
759                                 "nesting depth has been reached.", path);
760                 return (NULL);
761         }
762
763         status = stat (path, &statbuf);
764         if (status != 0)
765         {
766                 char errbuf[1024];
767                 ERROR ("configfile: stat (%s) failed: %s",
768                                 path,
769                                 sstrerror (errno, errbuf, sizeof (errbuf)));
770                 return (NULL);
771         }
772
773         if (S_ISREG (statbuf.st_mode))
774                 return (cf_read_file (path, depth));
775         else if (S_ISDIR (statbuf.st_mode))
776                 return (cf_read_dir (path, depth));
777
778         ERROR ("configfile: %s is neither a file nor a directory.", path);
779         return (NULL);
780 } /* oconfig_item_t *cf_read_generic */
781 #endif /* !HAVE_WORDEXP_H */
782
783 /* 
784  * Public functions
785  */
786 int global_option_set (const char *option, const char *value)
787 {
788         int i;
789
790         DEBUG ("option = %s; value = %s;", option, value);
791
792         for (i = 0; i < cf_global_options_num; i++)
793                 if (strcasecmp (cf_global_options[i].key, option) == 0)
794                         break;
795
796         if (i >= cf_global_options_num)
797                 return (-1);
798
799         sfree (cf_global_options[i].value);
800
801         if (value != NULL)
802                 cf_global_options[i].value = strdup (value);
803         else
804                 cf_global_options[i].value = NULL;
805
806         return (0);
807 }
808
809 const char *global_option_get (const char *option)
810 {
811         int i;
812
813         for (i = 0; i < cf_global_options_num; i++)
814                 if (strcasecmp (cf_global_options[i].key, option) == 0)
815                         break;
816
817         if (i >= cf_global_options_num)
818                 return (NULL);
819         
820         return ((cf_global_options[i].value != NULL)
821                         ? cf_global_options[i].value
822                         : cf_global_options[i].def);
823 } /* char *global_option_get */
824
825 void cf_unregister (const char *type)
826 {
827         cf_callback_t *this, *prev;
828
829         for (prev = NULL, this = first_callback;
830                         this != NULL;
831                         prev = this, this = this->next)
832                 if (strcasecmp (this->type, type) == 0)
833                 {
834                         if (prev == NULL)
835                                 first_callback = this->next;
836                         else
837                                 prev->next = this->next;
838
839                         free (this);
840                         break;
841                 }
842 } /* void cf_unregister */
843
844 void cf_unregister_complex (const char *type)
845 {
846         cf_complex_callback_t *this, *prev;
847
848         for (prev = NULL, this = complex_callback_head;
849                         this != NULL;
850                         prev = this, this = this->next)
851                 if (strcasecmp (this->type, type) == 0)
852                 {
853                         if (prev == NULL)
854                                 complex_callback_head = this->next;
855                         else
856                                 prev->next = this->next;
857
858                         sfree (this->type);
859                         sfree (this);
860                         break;
861                 }
862 } /* void cf_unregister */
863
864 void cf_register (const char *type,
865                 int (*callback) (const char *, const char *),
866                 const char **keys, int keys_num)
867 {
868         cf_callback_t *cf_cb;
869
870         /* Remove this module from the list, if it already exists */
871         cf_unregister (type);
872
873         /* This pointer will be free'd in `cf_unregister' */
874         if ((cf_cb = (cf_callback_t *) malloc (sizeof (cf_callback_t))) == NULL)
875                 return;
876
877         cf_cb->type     = type;
878         cf_cb->callback = callback;
879         cf_cb->keys     = keys;
880         cf_cb->keys_num = keys_num;
881
882         cf_cb->next = first_callback;
883         first_callback = cf_cb;
884 } /* void cf_register */
885
886 int cf_register_complex (const char *type, int (*callback) (oconfig_item_t *))
887 {
888         cf_complex_callback_t *new;
889
890         new = (cf_complex_callback_t *) malloc (sizeof (cf_complex_callback_t));
891         if (new == NULL)
892                 return (-1);
893
894         new->type = strdup (type);
895         if (new->type == NULL)
896         {
897                 sfree (new);
898                 return (-1);
899         }
900
901         new->callback = callback;
902         new->next = NULL;
903
904         if (complex_callback_head == NULL)
905         {
906                 complex_callback_head = new;
907         }
908         else
909         {
910                 cf_complex_callback_t *last = complex_callback_head;
911                 while (last->next != NULL)
912                         last = last->next;
913                 last->next = new;
914         }
915
916         return (0);
917 } /* int cf_register_complex */
918
919 int cf_read (char *filename)
920 {
921         oconfig_item_t *conf;
922         int i;
923
924         conf = cf_read_generic (filename, 0 /* depth */);
925         if (conf == NULL)
926         {
927                 ERROR ("Unable to read config file %s.", filename);
928                 return (-1);
929         }
930
931         for (i = 0; i < conf->children_num; i++)
932         {
933                 if (conf->children[i].children == NULL)
934                         dispatch_value (conf->children + i);
935                 else
936                         dispatch_block (conf->children + i);
937         }
938
939         oconfig_free (conf);
940
941         /* Read the default types.db if no `TypesDB' option was given. */
942         if (cf_default_typesdb)
943                 read_types_list (PKGDATADIR"/types.db");
944
945         return (0);
946 } /* int cf_read */
947
948 /* Assures the config option is a string, duplicates it and returns the copy in
949  * "ret_string". If necessary "*ret_string" is freed first. Returns zero upon
950  * success. */
951 int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */
952 {
953         char *string;
954
955         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
956         {
957                 ERROR ("cf_util_get_string: The %s option requires "
958                                 "exactly one string argument.", ci->key);
959                 return (-1);
960         }
961
962         string = strdup (ci->values[0].value.string);
963         if (string == NULL)
964                 return (-1);
965
966         if (*ret_string != NULL)
967                 sfree (*ret_string);
968         *ret_string = string;
969
970         return (0);
971 } /* }}} int cf_util_get_string */
972
973 /* Assures the config option is a string and copies it to the provided buffer.
974  * Assures null-termination. */
975 int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */
976                 size_t buffer_size)
977 {
978         if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1))
979                 return (EINVAL);
980
981         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
982         {
983                 ERROR ("cf_util_get_string_buffer: The %s option requires "
984                                 "exactly one string argument.", ci->key);
985                 return (-1);
986         }
987
988         strncpy (buffer, ci->values[0].value.string, buffer_size);
989         buffer[buffer_size - 1] = 0;
990
991         return (0);
992 } /* }}} int cf_util_get_string_buffer */
993
994 /* Assures the config option is a number and returns it as an int. */
995 int cf_util_get_int (const oconfig_item_t *ci, int *ret_value) /* {{{ */
996 {
997         if ((ci == NULL) || (ret_value == NULL))
998                 return (EINVAL);
999
1000         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
1001         {
1002                 ERROR ("cf_util_get_int: The %s option requires "
1003                                 "exactly one numeric argument.", ci->key);
1004                 return (-1);
1005         }
1006
1007         *ret_value = (int) ci->values[0].value.number;
1008
1009         return (0);
1010 } /* }}} int cf_util_get_int */
1011
1012 int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */
1013 {
1014         if ((ci == NULL) || (ret_bool == NULL))
1015                 return (EINVAL);
1016
1017         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
1018         {
1019                 ERROR ("cf_util_get_boolean: The %s option requires "
1020                                 "exactly one boolean argument.", ci->key);
1021                 return (-1);
1022         }
1023
1024         *ret_bool = ci->values[0].value.boolean ? true : false;
1025
1026         return (0);
1027 } /* }}} int cf_util_get_boolean */
1028
1029 /* Assures that the config option is a string. The string is then converted to
1030  * a port number using `service_name_to_port_number' and returned. Returns the
1031  * port number in the range [1-65535] or less than zero upon failure. */
1032 int cf_util_get_port_number (const oconfig_item_t *ci) /* {{{ */
1033 {
1034         if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
1035         {
1036                 ERROR ("cf_util_get_port_number: The %s option requires "
1037                                 "exactly one string argument.", ci->key);
1038                 return (-1);
1039         }
1040
1041         return (service_name_to_port_number (ci->values[0].value.string));
1042 } /* }}} int cf_util_get_port_number */