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