10f2d757557384b8bd6a8d48db37103969d95131
[oweals/gnunet.git] / src / util / configuration.c
1 /*
2      This file is part of GNUnet.
3      (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet 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
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21 /**
22  * @file src/util/configuration.c
23  * @brief configuration management
24  *
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_strings_lib.h"
35
36
37 /**
38  * @brief configuration entry
39  */
40 struct ConfigEntry
41 {
42
43   /**
44    * This is a linked list.
45    */
46   struct ConfigEntry *next;
47
48   /**
49    * key for this entry
50    */
51   char *key;
52
53   /**
54    * current, commited value
55    */
56   char *val;
57 };
58
59
60 /**
61  * @brief configuration section
62  */
63 struct ConfigSection
64 {
65   /**
66    * This is a linked list.
67    */
68   struct ConfigSection *next;
69
70   /**
71    * entries in the section
72    */
73   struct ConfigEntry *entries;
74
75   /**
76    * name of the section
77    */
78   char *name;
79 };
80
81
82 /**
83  * @brief configuration data
84  */
85 struct GNUNET_CONFIGURATION_Handle
86 {
87   /**
88    * Configuration sections.
89    */
90   struct ConfigSection *sections;
91
92   /**
93    * Modification indication since last save
94    * GNUNET_NO if clean, GNUNET_YES if dirty,
95    * GNUNET_SYSERR on error (i.e. last save failed)
96    */
97   int dirty;
98
99 };
100
101
102 /**
103  * Used for diffing a configuration object against
104  * the default one
105  */
106 struct DiffHandle
107 {
108   const struct GNUNET_CONFIGURATION_Handle *cfgDefault;
109   struct GNUNET_CONFIGURATION_Handle *cfgDiff;
110 };
111
112
113
114 /**
115  * Create a GNUNET_CONFIGURATION_Handle.
116  *
117  * @return fresh configuration object
118  */
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
121 {
122   return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
123 }
124
125
126 /**
127  * Destroy configuration object.
128  *
129  * @param cfg configuration to destroy
130  */
131 void
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
133 {
134   struct ConfigSection *sec;
135   struct ConfigEntry *ent;
136
137   while (NULL != (sec = cfg->sections))
138     {
139       cfg->sections = sec->next;
140       while (NULL != (ent = sec->entries))
141         {
142           sec->entries = ent->next;
143           GNUNET_free (ent->key);
144           GNUNET_free_non_null (ent->val);
145           GNUNET_free (ent);
146         }
147       GNUNET_free (sec->name);
148       GNUNET_free (sec);
149     }
150   GNUNET_free (cfg);
151 }
152
153
154 /**
155  * Parse a configuration file, add all of the options in the
156  * file to the configuration environment.
157  *
158  * @param cfg configuration to update
159  * @param filename name of the configuration file
160  * @return GNUNET_OK on success, GNUNET_SYSERR on error
161  */
162 int
163 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
164                             const char *filename)
165 {
166   int dirty;
167   char line[256];
168   char tag[64];
169   char value[192];
170   FILE *fp;
171   unsigned int nr;
172   int i;
173   int emptyline;
174   int ret;
175   char *section;
176   char *fn;
177
178   fn = GNUNET_STRINGS_filename_expand (filename);
179   if (fn == NULL)
180     return GNUNET_SYSERR;
181   dirty = cfg->dirty;           /* back up value! */
182   if (NULL == (fp = FOPEN (fn, "r")))
183     {
184       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
185       GNUNET_free (fn);
186       return GNUNET_SYSERR;
187     }
188   GNUNET_free (fn);
189   ret = GNUNET_OK;
190   section = GNUNET_strdup ("");
191   memset (line, 0, 256);
192   nr = 0;
193   while (NULL != fgets (line, 255, fp))
194     {
195       nr++;
196       for (i = 0; i < 255; i++)
197         if (line[i] == '\t')
198           line[i] = ' ';
199       if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
200           line[0] == '\r')
201         continue;
202       emptyline = 1;
203       for (i = 0; (i < 255 && line[i] != 0); i++)
204         if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
205           emptyline = 0;
206       if (emptyline == 1)
207         continue;
208       /* remove tailing whitespace */
209       for (i = strlen (line) - 1; (i >= 0) && (isspace ( (unsigned char) line[i])); i--)
210         line[i] = '\0';
211       if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
212         {
213           /* @INLINE@ value */
214           if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
215             ret = GNUNET_SYSERR;        /* failed to parse included config */
216         }
217       else if (1 == sscanf (line, "[%99[^]]]", value))
218         {
219           /* [value] */
220           GNUNET_free (section);
221           section = GNUNET_strdup (value);
222         }
223       else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
224         {
225           /* tag = value */
226           /* Strip LF */
227           i = strlen (value) - 1;
228           while ((i >= 0) && (isspace ( (unsigned char) value[i])))
229             value[i--] = '\0';
230           /* remove quotes */
231           i = 0;
232           if (value[0] == '"')
233             {
234               i = 1;
235               while ((value[i] != '\0') && (value[i] != '"'))
236                 i++;
237               if (value[i] == '"')
238                 {
239                   value[i] = '\0';
240                   i = 1;
241                 }
242               else
243                 i = 0;
244             }
245           GNUNET_CONFIGURATION_set_value_string (cfg,
246                                                  section, tag, &value[i]);
247         }
248       else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
249         {
250           /* tag = */
251           GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
252         }
253       else
254         {
255           /* parse error */
256           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
257                       _
258                       ("Syntax error in configuration file `%s' at line %u.\n"),
259                       filename, nr);
260           ret = GNUNET_SYSERR;
261           break;
262         }
263     }
264   GNUNET_assert (0 == fclose (fp));
265   /* restore dirty flag - anything we set in the meantime
266      came from disk */
267   cfg->dirty = dirty;
268   GNUNET_free (section);
269   return ret;
270 }
271
272
273 /**
274  * Test if there are configuration options that were
275  * changed since the last save.
276  *
277  * @param cfg configuration to inspect
278  * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
279  */
280 int
281 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
282 {
283   return cfg->dirty;
284 }
285
286
287 /**
288  * Write configuration file.
289  *
290  * @param cfg configuration to write
291  * @param filename where to write the configuration
292  * @return GNUNET_OK on success, GNUNET_SYSERR on error
293  */
294 int
295 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
296                             const char *filename)
297 {
298   struct ConfigSection *sec;
299   struct ConfigEntry *ent;
300   FILE *fp;
301   int error;
302   char *fn;
303   char *val;
304   char *pos;
305
306   fn = GNUNET_STRINGS_filename_expand (filename);
307   if (fn == NULL)
308     return GNUNET_SYSERR;
309   if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
310     {
311       GNUNET_free (fn);
312       return GNUNET_SYSERR;
313     }
314   if (NULL == (fp = FOPEN (fn, "w")))
315     {
316       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
317       GNUNET_free (fn);
318       return GNUNET_SYSERR;
319     }
320   GNUNET_free (fn);
321   error = 0;
322   sec = cfg->sections;
323   while (sec != NULL)
324     {
325       if (0 > fprintf (fp, "[%s]\n", sec->name))
326         {
327           error = 1;
328           break;
329         }
330       ent = sec->entries;
331       while (ent != NULL)
332         {
333           if (ent->val != NULL)
334             {
335               val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
336               strcpy (val, ent->val);
337               while (NULL != (pos = strstr (val, "\n")))
338                 {
339                   memmove (&pos[2], &pos[1], strlen (&pos[1]));
340                   pos[0] = '\\';
341                   pos[1] = 'n';
342                 }
343               if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
344                 {
345                   error = 1;
346                   GNUNET_free (val);
347                   break;
348                 }
349               GNUNET_free (val);
350             }
351           ent = ent->next;
352         }
353       if (error != 0)
354         break;
355       if (0 > fprintf (fp, "\n"))
356         {
357           error = 1;
358           break;
359         }
360       sec = sec->next;
361     }
362   if (error != 0)
363     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
364   GNUNET_assert (0 == fclose (fp));
365   if (error != 0)
366     {
367       cfg->dirty = GNUNET_SYSERR;       /* last write failed */
368       return GNUNET_SYSERR;
369     }
370   cfg->dirty = GNUNET_NO;       /* last write succeeded */
371   return GNUNET_OK;
372 }
373
374
375 /**
376  * Iterate over all options in the configuration.
377  *
378  * @param cfg configuration to inspect
379  * @param iter function to call on each option
380  * @param iter_cls closure for iter
381  */
382 void
383 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
384                               GNUNET_CONFIGURATION_Iterator iter,
385                               void *iter_cls)
386 {
387   struct ConfigSection *spos;
388   struct ConfigEntry *epos;
389
390   spos = cfg->sections;
391   while (spos != NULL)
392     {
393       epos = spos->entries;
394       while (epos != NULL)
395         {
396           iter (iter_cls, spos->name, epos->key, epos->val);
397           epos = epos->next;
398         }
399       spos = spos->next;
400     }
401 }
402
403
404 /**
405  * Iterate over all sections in the configuration.
406  *
407  * @param cfg configuration to inspect
408  * @param iter function to call on each section
409  * @param iter_cls closure for iter
410  */
411 void
412 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
413                                        GNUNET_CONFIGURATION_Section_Iterator iter,
414                                        void *iter_cls)
415 {
416   struct ConfigSection *spos;
417
418   for (spos = cfg->sections; spos != NULL; spos = spos->next)
419     iter (iter_cls, spos->name);
420 }
421
422
423 /**
424  * Copy a configuration value to the given target configuration.
425  * Overwrites existing entries.
426  *
427  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
428  * @param section section for the value
429  * @param option option name of the value
430  * @param value value to copy 
431  */
432 static void
433 copy_entry (void *cls,
434             const char *section, const char *option, const char *value)
435 {
436   struct GNUNET_CONFIGURATION_Handle *dst = cls;
437   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
438 }
439
440
441 /**
442  * Duplicate an existing configuration object.
443  *
444  * @param cfg configuration to duplicate
445  * @return duplicate configuration
446  */
447 struct GNUNET_CONFIGURATION_Handle *
448 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
449 {
450   struct GNUNET_CONFIGURATION_Handle *ret;
451
452   ret = GNUNET_CONFIGURATION_create ();
453   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
454   return ret;
455 }
456
457
458 /**
459  * FIXME.
460  *
461  * @param cfg FIXME
462  * @param section FIXME
463  * @return matching entry, NULL if not found
464  */
465 static struct ConfigSection *
466 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
467              const char *section)
468 {
469   struct ConfigSection *pos;
470
471   pos = cfg->sections;
472   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
473     pos = pos->next;
474   return pos;
475 }
476
477
478 /**
479  * Find an entry from a configuration.
480  *
481  * @param cfg handle to the configuration
482  * @param section section the option is in
483  * @param key the option
484  * @return matching entry, NULL if not found
485  */
486 static struct ConfigEntry *
487 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
488            const char *section, const char *key)
489 {
490   struct ConfigSection *sec;
491   struct ConfigEntry *pos;
492
493   sec = findSection (cfg, section);
494   if (sec == NULL)
495     return NULL;
496   pos = sec->entries;
497   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
498     pos = pos->next;
499   return pos;
500 }
501
502
503 /**
504  * A callback function, compares entries from two configurations
505  * (default against a new configuration) and write the diffs in a
506  * diff-configuration object (the callback object).
507  *
508  * @param cls the diff configuration (struct DiffHandle*)
509  * @param section section for the value (of the default conf.)
510  * @param option option name of the value (of the default conf.)
511  * @param value value to copy (of the default conf.)
512  */
513 static void
514 compareEntries (void *cls,
515                 const char *section, const char *option, const char *value)
516 {
517   struct DiffHandle *dh = cls;
518   struct ConfigEntry *entNew;
519  
520   entNew = findEntry (dh->cfgDefault, section, option);
521   if ( (entNew != NULL) &&
522        (strcmp (entNew->val, value) == 0) )
523     return;
524   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
525                                          section,
526                                          option,
527                                          value);
528 }
529
530
531 /**
532  * Write only configuration entries that have been changed to configuration file
533  * @param cfgDefault default configuration
534  * @param cfgNew new configuration
535  * @param filename where to write the configuration diff between default and new
536  * @return GNUNET_OK on success, GNUNET_SYSERR on error
537  */
538 int
539 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
540                                   *cfgDefault,
541                                   const struct GNUNET_CONFIGURATION_Handle *cfgNew,
542                                   const char *filename)
543 {
544   int ret;
545   struct DiffHandle diffHandle;
546
547   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
548   diffHandle.cfgDefault = cfgDefault;
549   GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
550   ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
551   GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
552   return ret;
553 }
554
555
556 /**
557  * Set a configuration value that should be a string.
558  *
559  * @param cfg configuration to update
560  * @param section section of interest
561  * @param option option of interest
562  * @param value value to set
563  */
564 void
565 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
566                                        *cfg,
567                                        const char *section,
568                                        const char *option, const char *value)
569 {
570   struct ConfigSection *sec;
571   struct ConfigEntry *e;
572
573   e = findEntry (cfg, section, option);
574   if (e != NULL)
575     {
576       GNUNET_free_non_null (e->val);
577       e->val = GNUNET_strdup (value);
578       return;
579     }
580   sec = findSection (cfg, section);
581   if (sec == NULL)
582     {
583       sec = GNUNET_malloc (sizeof (struct ConfigSection));
584       sec->name = GNUNET_strdup (section);
585       sec->next = cfg->sections;
586       cfg->sections = sec;
587     }
588   e = GNUNET_malloc (sizeof (struct ConfigEntry));
589   e->key = GNUNET_strdup (option);
590   e->val = GNUNET_strdup (value);
591   e->next = sec->entries;
592   sec->entries = e;
593 }
594
595
596 /**
597  * Set a configuration value that should be a number.
598  *
599  * @param cfg configuration to update
600  * @param section section of interest
601  * @param option option of interest
602  * @param number value to set
603  */
604 void
605 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
606                                        *cfg, const char *section,
607                                        const char *option,
608                                        unsigned long long number)
609 {
610   char s[64];
611   GNUNET_snprintf (s, 64, "%llu", number);
612   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
613 }
614
615
616 /**
617  * Get a configuration value that should be a number.
618  *
619  * @param cfg configuration to inspect
620  * @param section section of interest
621  * @param option option of interest
622  * @param number where to store the numeric value of the option
623  * @return GNUNET_OK on success, GNUNET_SYSERR on error
624  */
625 int
626 GNUNET_CONFIGURATION_get_value_number (const struct
627                                        GNUNET_CONFIGURATION_Handle *cfg,
628                                        const char *section,
629                                        const char *option,
630                                        unsigned long long *number)
631 {
632   struct ConfigEntry *e;
633
634   e = findEntry (cfg, section, option);
635   if (e == NULL)
636     return GNUNET_SYSERR;
637   if (1 != SSCANF (e->val, "%llu", number))
638     return GNUNET_SYSERR;
639   return GNUNET_OK;
640 }
641
642
643 /**
644  * Get a configuration value that should be a relative time.
645  *
646  * @param cfg configuration to inspect
647  * @param section section of interest
648  * @param option option of interest
649  * @param time set to the time value stored in the configuration
650  * @return GNUNET_OK on success, GNUNET_SYSERR on error
651  */
652 int
653 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
654                                      *cfg, const char *section,
655                                      const char *option,
656                                      struct GNUNET_TIME_Relative *time)
657 {
658   unsigned long long num;
659   int ret;
660
661   ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
662   if (ret == GNUNET_OK)
663     time->rel_value = (uint64_t) num;
664   return ret;
665 }
666
667
668 /**
669  * Get a configuration value that should be a string.
670  *
671  * @param cfg configuration to inspect
672  * @param section section of interest
673  * @param option option of interest
674  * @param value will be set to a freshly allocated configuration
675  *        value, or NULL if option is not specified
676  * @return GNUNET_OK on success, GNUNET_SYSERR on error
677  */
678 int
679 GNUNET_CONFIGURATION_get_value_string (const struct
680                                        GNUNET_CONFIGURATION_Handle *cfg,
681                                        const char *section,
682                                        const char *option, char **value)
683 {
684   struct ConfigEntry *e;
685
686   e = findEntry (cfg, section, option);
687   if ((e == NULL) || (e->val == NULL))
688     {
689       *value = NULL;
690       return GNUNET_SYSERR;
691     }
692   *value = GNUNET_strdup (e->val);
693   return GNUNET_OK;
694 }
695
696
697 /**
698  * Get a configuration value that should be in a set of
699  * predefined strings
700  *
701  * @param cfg configuration to inspect
702  * @param section section of interest
703  * @param option option of interest
704  * @param choices NULL-terminated list of legal values
705  * @param value will be set to an entry in the legal list,
706  *        or NULL if option is not specified and no default given
707  * @return GNUNET_OK on success, GNUNET_SYSERR on error
708  */
709 int
710 GNUNET_CONFIGURATION_get_value_choice (const struct
711                                        GNUNET_CONFIGURATION_Handle *cfg,
712                                        const char *section,
713                                        const char *option,
714                                        const char **choices,
715                                        const char **value)
716 {
717   struct ConfigEntry *e;
718   int i;
719
720   e = findEntry (cfg, section, option);
721   if (e == NULL)
722     return GNUNET_SYSERR;
723   i = 0;
724   while (choices[i] != NULL)
725     {
726       if (0 == strcasecmp (choices[i], e->val))
727         break;
728       i++;
729     }
730   if (choices[i] == NULL)
731     {
732       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
733                   _("Configuration value '%s' for '%s'"
734                     " in section '%s' is not in set of legal choices\n"),
735                   e->val, option, section);
736       return GNUNET_SYSERR;
737     }
738   *value = choices[i];
739   return GNUNET_OK;
740 }
741
742
743 /**
744  * Test if we have a value for a particular option
745  * @param cfg configuration to inspect
746  * @param section section of interest
747  * @param option option of interest
748  * @return GNUNET_YES if so, GNUNET_NO if not.
749  */
750 int
751 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
752                                  *cfg, const char *section,
753                                  const char *option)
754 {
755   struct ConfigEntry *e;
756   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
757     return GNUNET_NO;
758   return GNUNET_YES;
759 }
760
761
762 /**
763  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
764  * where either in the "PATHS" section or the environtment
765  * "FOO" is set to "DIRECTORY".
766  *
767  * @param cfg configuration to use for path expansion
768  * @param orig string to $-expand (will be freed!)
769  * @return $-expanded string
770  */
771 char *
772 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
773                                     *cfg, char *orig)
774 {
775   int i;
776   char *prefix;
777   char *result;
778   const char *post;
779   const char *env;
780
781   if (orig[0] != '$')
782     return orig;
783   i = 0;
784   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
785     i++;
786   if (orig[i] == '\0')
787     {
788       post = "";
789     }
790   else
791     {
792       orig[i] = '\0';
793       post = &orig[i + 1];
794     }
795   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
796                                                             "PATHS",
797                                                             &orig[1], &prefix))
798     {
799       if (NULL == (env = getenv (&orig[1])))
800         {
801           orig[i] = DIR_SEPARATOR;
802           return orig;
803         }
804       prefix = GNUNET_strdup (env);
805     }
806   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
807   strcpy (result, prefix);
808   if ((strlen (prefix) == 0) ||
809       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
810     strcat (result, DIR_SEPARATOR_STR);
811   strcat (result, post);
812   GNUNET_free (prefix);
813   GNUNET_free (orig);
814   return result;
815 }
816
817
818 /**
819  * Get a configuration value that should be a string.
820  *
821  * @param cfg configuration to inspect
822  * @param section section of interest
823  * @param option option of interest
824  * @param value will be set to a freshly allocated configuration
825  *        value, or NULL if option is not specified
826  * @return GNUNET_OK on success, GNUNET_SYSERR on error
827  */
828 int
829 GNUNET_CONFIGURATION_get_value_filename (const struct
830                                          GNUNET_CONFIGURATION_Handle *cfg,
831                                          const char *section,
832                                          const char *option, char **value)
833 {
834   char *tmp;
835
836   if (GNUNET_OK !=
837       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
838     {
839       *value = NULL;
840       return GNUNET_SYSERR;
841     }
842   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
843   *value = GNUNET_STRINGS_filename_expand (tmp);
844   GNUNET_free (tmp);
845   if (*value == NULL)
846     return GNUNET_SYSERR;
847   return GNUNET_OK;
848 }
849
850
851 /**
852  * Get a configuration value that should be in a set of
853  * "GNUNET_YES" or "GNUNET_NO".
854  *
855  * @param cfg configuration to inspect
856  * @param section section of interest
857  * @param option option of interest
858  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
859  */
860 int
861 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
862                                       *cfg, const char *section,
863                                       const char *option)
864 {
865   static const char *yesno[] = { "YES", "NO", NULL };
866   const char *val;
867   int ret;
868
869   ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
870                                                section, option, yesno, &val);
871   if (ret == GNUNET_SYSERR)
872     return ret;
873   if (val == yesno[0])
874     return GNUNET_YES;
875   return GNUNET_NO;
876 }
877
878
879 /**
880  * Iterate over the set of filenames stored in a configuration value.
881  *
882  * @param cfg configuration to inspect
883  * @param section section of interest
884  * @param option option of interest
885  * @param cb function to call on each filename
886  * @param cb_cls closure for cb
887  * @return number of filenames iterated over, -1 on error
888  */
889 int
890 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
891                                               GNUNET_CONFIGURATION_Handle
892                                               *cfg, const char *section,
893                                               const char *option,
894                                               GNUNET_FileNameCallback cb,
895                                               void *cb_cls)
896 {
897   char *list;
898   char *pos;
899   char *end;
900   char old;
901   int ret;
902
903   if (GNUNET_OK !=
904       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
905     return 0;
906   GNUNET_assert (list != NULL);
907   ret = 0;
908   pos = list;
909   while (1)
910     {
911       while (pos[0] == ' ')
912         pos++;
913       if (strlen (pos) == 0)
914         break;
915       end = pos + 1;
916       while ((end[0] != ' ') && (end[0] != '\0'))
917         {
918           if (end[0] == '\\')
919             {
920               switch (end[1])
921                 {
922                 case '\\':
923                 case ' ':
924                   memmove (end, &end[1], strlen (&end[1]) + 1);
925                 case '\0':
926                   /* illegal, but just keep it */
927                   break;
928                 default:
929                   /* illegal, but just ignore that there was a '/' */
930                   break;
931                 }
932             }
933           end++;
934         }
935       old = end[0];
936       end[0] = '\0';
937       if (strlen (pos) > 0)
938         {
939           ret++;
940           if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
941             {
942               ret = GNUNET_SYSERR;
943               break;
944             }
945         }
946       if (old == '\0')
947         break;
948       pos = end + 1;
949     }
950   GNUNET_free (list);
951   return ret;
952 }
953
954
955 /**
956  * FIXME.
957  *
958  * @param value FIXME
959  * @return FIXME
960  */
961 static char *
962 escape_name (const char *value)
963 {
964   char *escaped;
965   const char *rpos;
966   char *wpos;
967
968   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
969   memset (escaped, 0, strlen (value) * 2 + 1);
970   rpos = value;
971   wpos = escaped;
972   while (rpos[0] != '\0')
973     {
974       switch (rpos[0])
975         {
976         case '\\':
977         case ' ':
978           wpos[0] = '\\';
979           wpos[1] = rpos[0];
980           wpos += 2;
981           break;
982         default:
983           wpos[0] = rpos[0];
984           wpos++;
985         }
986       rpos++;
987     }
988   return escaped;
989 }
990
991
992 /**
993  * FIXME.
994  *
995  * @param cls string we compare with (const char*)
996  * @param fn filename we are currently looking at
997  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
998  */
999 static int
1000 test_match (void *cls, const char *fn)
1001 {
1002   const char *of = cls;
1003   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1004 }
1005
1006
1007 /**
1008  * Append a filename to a configuration value that
1009  * represents a list of filenames
1010  *
1011  * @param cfg configuration to update
1012  * @param section section of interest
1013  * @param option option of interest
1014  * @param value filename to append
1015  * @return GNUNET_OK on success,
1016  *         GNUNET_NO if the filename already in the list
1017  *         GNUNET_SYSERR on error
1018  */
1019 int
1020 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1021                                             *cfg,
1022                                             const char *section,
1023                                             const char *option,
1024                                             const char *value)
1025 {
1026   char *escaped;
1027   char *old;
1028   char *nw;
1029
1030   if (GNUNET_SYSERR
1031       == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1032                                                        section,
1033                                                        option,
1034                                                        &test_match,
1035                                                        (void *) value))
1036     return GNUNET_NO;           /* already exists */
1037   if (GNUNET_OK !=
1038       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1039     old = GNUNET_strdup ("");
1040   escaped = escape_name (value);
1041   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1042   strcpy (nw, old);
1043   if (strlen (old) > 0)
1044     strcat (nw, " ");
1045   strcat (nw, escaped);
1046   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1047   GNUNET_free (old);
1048   GNUNET_free (nw);
1049   GNUNET_free (escaped);
1050   return GNUNET_OK;
1051 }
1052
1053
1054 /**
1055  * Remove a filename from a configuration value that
1056  * represents a list of filenames
1057  *
1058  * @param cfg configuration to update
1059  * @param section section of interest
1060  * @param option option of interest
1061  * @param value filename to remove
1062  * @return GNUNET_OK on success,
1063  *         GNUNET_NO if the filename is not in the list,
1064  *         GNUNET_SYSERR on error
1065  */
1066 int
1067 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1068                                             *cfg,
1069                                             const char *section,
1070                                             const char *option,
1071                                             const char *value)
1072 {
1073   char *list;
1074   char *pos;
1075   char *end;
1076   char *match;
1077   char old;
1078
1079   if (GNUNET_OK !=
1080       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1081     return GNUNET_NO;
1082   match = escape_name (value);
1083   pos = list;
1084   while (1)
1085     {
1086       while (pos[0] == ' ')
1087         pos++;
1088       if (strlen (pos) == 0)
1089         break;
1090       end = pos + 1;
1091       while ((end[0] != ' ') && (end[0] != '\0'))
1092         {
1093           if (end[0] == '\\')
1094             {
1095               switch (end[1])
1096                 {
1097                 case '\\':
1098                 case ' ':
1099                   end++;
1100                   break;
1101                 case '\0':
1102                   /* illegal, but just keep it */
1103                   break;
1104                 default:
1105                   /* illegal, but just ignore that there was a '/' */
1106                   break;
1107                 }
1108             }
1109           end++;
1110         }
1111       old = end[0];
1112       end[0] = '\0';
1113       if (0 == strcmp (pos, match))
1114         {
1115           if (old != '\0')
1116             memmove (pos, &end[1], strlen (&end[1]) + 1);
1117           else
1118             {
1119               if (pos != list) 
1120                 pos[-1] = '\0';
1121               else
1122                 pos[0] = '\0';
1123             }
1124           GNUNET_CONFIGURATION_set_value_string (cfg,
1125                                                  section, option, list);
1126           GNUNET_free (list);
1127           GNUNET_free (match);
1128           return GNUNET_OK;
1129         }        
1130       if (old == '\0')
1131         break;
1132       end[0] = old;
1133       pos = end + 1;
1134     }
1135   GNUNET_free (list);
1136   GNUNET_free (match);
1137   return GNUNET_NO;
1138 }
1139
1140
1141 /**
1142  * Load configuration (starts with defaults, then loads
1143  * system-specific configuration).
1144  *
1145  * @param cfg configuration to update
1146  * @param filename name of the configuration file, NULL to load defaults
1147  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1148  */
1149 int
1150 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1151                            const char *filename)
1152 {
1153   char *baseconfig;
1154   char *ipath;
1155
1156   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1157   if (ipath == NULL)
1158     return GNUNET_SYSERR;
1159   baseconfig = NULL;
1160   GNUNET_asprintf (&baseconfig,
1161                    "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1162   GNUNET_free (ipath);
1163   if ((GNUNET_OK !=
1164        GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1165       (!((filename == NULL) ||
1166          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1167     {
1168       GNUNET_free (baseconfig);
1169       return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1170     }
1171   GNUNET_free (baseconfig);
1172   if ( ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
1173                                                         "PATHS",
1174                                                         "DEFAULTCONFIG"))) &&
1175        (filename != NULL) )
1176     GNUNET_CONFIGURATION_set_value_string (cfg,
1177                                            "PATHS",
1178                                            "DEFAULTCONFIG",
1179                                            filename);
1180   if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1181                                                       "TESTING",
1182                                                       "WEAKRANDOM")) &&
1183       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1184                                                            "TESTING",
1185                                                            "WEAKRANDOM")))
1186     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1187   return GNUNET_OK;
1188 }
1189
1190
1191
1192 /* end of configuration.c */