fix
[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   GNUNET_DISK_directory_create_for_file (fn);
310   if (NULL == (fp = FOPEN (fn, "w")))
311     {
312       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
313       GNUNET_free (fn);
314       return GNUNET_SYSERR;
315     }
316   GNUNET_free (fn);
317   error = 0;
318   sec = cfg->sections;
319   while (sec != NULL)
320     {
321       if (0 > fprintf (fp, "[%s]\n", sec->name))
322         {
323           error = 1;
324           break;
325         }
326       ent = sec->entries;
327       while (ent != NULL)
328         {
329           if (ent->val != NULL)
330             {
331               val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
332               strcpy (val, ent->val);
333               while (NULL != (pos = strstr (val, "\n")))
334                 {
335                   memmove (&pos[2], &pos[1], strlen (&pos[1]));
336                   pos[0] = '\\';
337                   pos[1] = 'n';
338                 }
339               if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
340                 {
341                   error = 1;
342                   GNUNET_free (val);
343                   break;
344                 }
345               GNUNET_free (val);
346             }
347           ent = ent->next;
348         }
349       if (error != 0)
350         break;
351       if (0 > fprintf (fp, "\n"))
352         {
353           error = 1;
354           break;
355         }
356       sec = sec->next;
357     }
358   if (error != 0)
359     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
360   GNUNET_assert (0 == fclose (fp));
361   if (error != 0)
362     {
363       cfg->dirty = GNUNET_SYSERR;       /* last write failed */
364       return GNUNET_SYSERR;
365     }
366   cfg->dirty = GNUNET_NO;       /* last write succeeded */
367   return GNUNET_OK;
368 }
369
370
371 /**
372  * Iterate over all options in the configuration.
373  *
374  * @param cfg configuration to inspect
375  * @param iter function to call on each option
376  * @param iter_cls closure for iter
377  */
378 void
379 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
380                               GNUNET_CONFIGURATION_Iterator iter,
381                               void *iter_cls)
382 {
383   struct ConfigSection *spos;
384   struct ConfigEntry *epos;
385
386   spos = cfg->sections;
387   while (spos != NULL)
388     {
389       epos = spos->entries;
390       while (epos != NULL)
391         {
392           iter (iter_cls, spos->name, epos->key, epos->val);
393           epos = epos->next;
394         }
395       spos = spos->next;
396     }
397 }
398
399
400 /**
401  * Copy a configuration value to the given target configuration.
402  * Overwrites existing entries.
403  *
404  * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
405  * @param section section for the value
406  * @param option option name of the value
407  * @param value value to copy 
408  */
409 static void
410 copy_entry (void *cls,
411             const char *section, const char *option, const char *value)
412 {
413   struct GNUNET_CONFIGURATION_Handle *dst = cls;
414   GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
415 }
416
417
418 /**
419  * Duplicate an existing configuration object.
420  *
421  * @param cfg configuration to duplicate
422  * @return duplicate configuration
423  */
424 struct GNUNET_CONFIGURATION_Handle *
425 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
426 {
427   struct GNUNET_CONFIGURATION_Handle *ret;
428
429   ret = GNUNET_CONFIGURATION_create ();
430   GNUNET_CONFIGURATION_iterate (cfg, &copy_entry, ret);
431   return ret;
432 }
433
434
435 /**
436  * FIXME.
437  *
438  * @param cfg FIXME
439  * @param section FIXME
440  * @return matching entry, NULL if not found
441  */
442 static struct ConfigSection *
443 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
444              const char *section)
445 {
446   struct ConfigSection *pos;
447
448   pos = cfg->sections;
449   while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
450     pos = pos->next;
451   return pos;
452 }
453
454
455 /**
456  * Find an entry from a configuration.
457  *
458  * @param cfg handle to the configuration
459  * @param section section the option is in
460  * @param key the option
461  * @return matching entry, NULL if not found
462  */
463 static struct ConfigEntry *
464 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
465            const char *section, const char *key)
466 {
467   struct ConfigSection *sec;
468   struct ConfigEntry *pos;
469
470   sec = findSection (cfg, section);
471   if (sec == NULL)
472     return NULL;
473   pos = sec->entries;
474   while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
475     pos = pos->next;
476   return pos;
477 }
478
479
480 /**
481  * A callback function, compares entries from two configurations
482  * (default against a new configuration) and write the diffs in a
483  * diff-configuration object (the callback object).
484  *
485  * @param cls the diff configuration (struct DiffHandle*)
486  * @param section section for the value (of the default conf.)
487  * @param option option name of the value (of the default conf.)
488  * @param value value to copy (of the default conf.)
489  */
490 static void
491 compareEntries (void *cls,
492                 const char *section, const char *option, const char *value)
493 {
494   struct DiffHandle *dh = cls;
495   struct ConfigEntry *entNew;
496  
497   entNew = findEntry (dh->cfgDefault, section, option);
498   if ( (entNew != NULL) &&
499        (strcmp (entNew->val, value) == 0) )
500     return;
501   GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
502                                          section,
503                                          option,
504                                          value);
505 }
506
507
508 /**
509  * Write only configuration entries that have been changed to configuration file
510  * @param cfgDefault default configuration
511  * @param cfgNew new configuration
512  * @param filename where to write the configuration diff between default and new
513  * @return GNUNET_OK on success, GNUNET_SYSERR on error
514  */
515 int
516 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
517                                   *cfgDefault,
518                                   const struct GNUNET_CONFIGURATION_Handle *cfgNew,
519                                   const char *filename)
520 {
521   int ret;
522   struct DiffHandle diffHandle;
523
524   diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
525   diffHandle.cfgDefault = cfgDefault;
526   GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
527   ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
528   GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
529   return ret;
530 }
531
532
533 /**
534  * Set a configuration value that should be a string.
535  *
536  * @param cfg configuration to update
537  * @param section section of interest
538  * @param option option of interest
539  * @param value value to set
540  */
541 void
542 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
543                                        *cfg,
544                                        const char *section,
545                                        const char *option, const char *value)
546 {
547   struct ConfigSection *sec;
548   struct ConfigEntry *e;
549
550   e = findEntry (cfg, section, option);
551   if (e != NULL)
552     {
553       GNUNET_free_non_null (e->val);
554       e->val = GNUNET_strdup (value);
555       return;
556     }
557   sec = findSection (cfg, section);
558   if (sec == NULL)
559     {
560       sec = GNUNET_malloc (sizeof (struct ConfigSection));
561       sec->name = GNUNET_strdup (section);
562       sec->next = cfg->sections;
563       cfg->sections = sec;
564     }
565   e = GNUNET_malloc (sizeof (struct ConfigEntry));
566   e->key = GNUNET_strdup (option);
567   e->val = GNUNET_strdup (value);
568   e->next = sec->entries;
569   sec->entries = e;
570 }
571
572
573 /**
574  * Set a configuration value that should be a number.
575  *
576  * @param cfg configuration to update
577  * @param section section of interest
578  * @param option option of interest
579  * @param number value to set
580  */
581 void
582 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
583                                        *cfg, const char *section,
584                                        const char *option,
585                                        unsigned long long number)
586 {
587   char s[64];
588   GNUNET_snprintf (s, 64, "%llu", number);
589   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
590 }
591
592
593 /**
594  * Get a configuration value that should be a number.
595  *
596  * @param cfg configuration to inspect
597  * @param section section of interest
598  * @param option option of interest
599  * @param number where to store the numeric value of the option
600  * @return GNUNET_OK on success, GNUNET_SYSERR on error
601  */
602 int
603 GNUNET_CONFIGURATION_get_value_number (const struct
604                                        GNUNET_CONFIGURATION_Handle *cfg,
605                                        const char *section,
606                                        const char *option,
607                                        unsigned long long *number)
608 {
609   struct ConfigEntry *e;
610
611   e = findEntry (cfg, section, option);
612   if (e == NULL)
613     return GNUNET_SYSERR;
614   if (1 != SSCANF (e->val, "%llu", number))
615     return GNUNET_SYSERR;
616   return GNUNET_OK;
617 }
618
619
620 /**
621  * Get a configuration value that should be a relative time.
622  *
623  * @param cfg configuration to inspect
624  * @param section section of interest
625  * @param option option of interest
626  * @param time set to the time value stored in the configuration
627  * @return GNUNET_OK on success, GNUNET_SYSERR on error
628  */
629 int
630 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
631                                      *cfg, const char *section,
632                                      const char *option,
633                                      struct GNUNET_TIME_Relative *time)
634 {
635   unsigned long long num;
636   int ret;
637
638   ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
639   if (ret == GNUNET_OK)
640     time->rel_value = (uint64_t) num;
641   return ret;
642 }
643
644
645 /**
646  * Get a configuration value that should be a string.
647  *
648  * @param cfg configuration to inspect
649  * @param section section of interest
650  * @param option option of interest
651  * @param value will be set to a freshly allocated configuration
652  *        value, or NULL if option is not specified
653  * @return GNUNET_OK on success, GNUNET_SYSERR on error
654  */
655 int
656 GNUNET_CONFIGURATION_get_value_string (const struct
657                                        GNUNET_CONFIGURATION_Handle *cfg,
658                                        const char *section,
659                                        const char *option, char **value)
660 {
661   struct ConfigEntry *e;
662
663   e = findEntry (cfg, section, option);
664   if ((e == NULL) || (e->val == NULL))
665     {
666       *value = NULL;
667       return GNUNET_SYSERR;
668     }
669   *value = GNUNET_strdup (e->val);
670   return GNUNET_OK;
671 }
672
673
674 /**
675  * Get a configuration value that should be in a set of
676  * predefined strings
677  *
678  * @param cfg configuration to inspect
679  * @param section section of interest
680  * @param option option of interest
681  * @param choices NULL-terminated list of legal values
682  * @param value will be set to an entry in the legal list,
683  *        or NULL if option is not specified and no default given
684  * @return GNUNET_OK on success, GNUNET_SYSERR on error
685  */
686 int
687 GNUNET_CONFIGURATION_get_value_choice (const struct
688                                        GNUNET_CONFIGURATION_Handle *cfg,
689                                        const char *section,
690                                        const char *option,
691                                        const char **choices,
692                                        const char **value)
693 {
694   struct ConfigEntry *e;
695   int i;
696
697   e = findEntry (cfg, section, option);
698   if (e == NULL)
699     return GNUNET_SYSERR;
700   i = 0;
701   while (choices[i] != NULL)
702     {
703       if (0 == strcasecmp (choices[i], e->val))
704         break;
705       i++;
706     }
707   if (choices[i] == NULL)
708     {
709       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
710                   _("Configuration value '%s' for '%s'"
711                     " in section '%s' is not in set of legal choices\n"),
712                   e->val, option, section);
713       return GNUNET_SYSERR;
714     }
715   *value = choices[i];
716   return GNUNET_OK;
717 }
718
719
720 /**
721  * Test if we have a value for a particular option
722  * @param cfg configuration to inspect
723  * @param section section of interest
724  * @param option option of interest
725  * @return GNUNET_YES if so, GNUNET_NO if not.
726  */
727 int
728 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
729                                  *cfg, const char *section,
730                                  const char *option)
731 {
732   struct ConfigEntry *e;
733   if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
734     return GNUNET_NO;
735   return GNUNET_YES;
736 }
737
738
739 /**
740  * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
741  * where either in the "PATHS" section or the environtment
742  * "FOO" is set to "DIRECTORY".
743  *
744  * @param cfg configuration to use for path expansion
745  * @param orig string to $-expand (will be freed!)
746  * @return $-expanded string
747  */
748 char *
749 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
750                                     *cfg, char *orig)
751 {
752   int i;
753   char *prefix;
754   char *result;
755   const char *post;
756   const char *env;
757
758   if (orig[0] != '$')
759     return orig;
760   i = 0;
761   while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
762     i++;
763   if (orig[i] == '\0')
764     {
765       post = "";
766     }
767   else
768     {
769       orig[i] = '\0';
770       post = &orig[i + 1];
771     }
772   if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
773                                                             "PATHS",
774                                                             &orig[1], &prefix))
775     {
776       if (NULL == (env = getenv (&orig[1])))
777         {
778           orig[i] = DIR_SEPARATOR;
779           return orig;
780         }
781       prefix = GNUNET_strdup (env);
782     }
783   result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
784   strcpy (result, prefix);
785   if ((strlen (prefix) == 0) ||
786       ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
787     strcat (result, DIR_SEPARATOR_STR);
788   strcat (result, post);
789   GNUNET_free (prefix);
790   GNUNET_free (orig);
791   return result;
792 }
793
794
795 /**
796  * Get a configuration value that should be a string.
797  *
798  * @param cfg configuration to inspect
799  * @param section section of interest
800  * @param option option of interest
801  * @param value will be set to a freshly allocated configuration
802  *        value, or NULL if option is not specified
803  * @return GNUNET_OK on success, GNUNET_SYSERR on error
804  */
805 int
806 GNUNET_CONFIGURATION_get_value_filename (const struct
807                                          GNUNET_CONFIGURATION_Handle *cfg,
808                                          const char *section,
809                                          const char *option, char **value)
810 {
811   char *tmp;
812
813   if (GNUNET_OK !=
814       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
815     {
816       *value = NULL;
817       return GNUNET_SYSERR;
818     }
819   tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
820   *value = GNUNET_STRINGS_filename_expand (tmp);
821   GNUNET_free (tmp);
822   if (*value == NULL)
823     return GNUNET_SYSERR;
824   return GNUNET_OK;
825 }
826
827
828 /**
829  * Get a configuration value that should be in a set of
830  * "GNUNET_YES" or "GNUNET_NO".
831  *
832  * @param cfg configuration to inspect
833  * @param section section of interest
834  * @param option option of interest
835  * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
836  */
837 int
838 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
839                                       *cfg, const char *section,
840                                       const char *option)
841 {
842   static const char *yesno[] = { "YES", "NO", NULL };
843   const char *val;
844   int ret;
845
846   ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
847                                                section, option, yesno, &val);
848   if (ret == GNUNET_SYSERR)
849     return ret;
850   if (val == yesno[0])
851     return GNUNET_YES;
852   return GNUNET_NO;
853 }
854
855
856 /**
857  * Iterate over the set of filenames stored in a configuration value.
858  *
859  * @param cfg configuration to inspect
860  * @param section section of interest
861  * @param option option of interest
862  * @param cb function to call on each filename
863  * @param cb_cls closure for cb
864  * @return number of filenames iterated over, -1 on error
865  */
866 int
867 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
868                                               GNUNET_CONFIGURATION_Handle
869                                               *cfg, const char *section,
870                                               const char *option,
871                                               GNUNET_FileNameCallback cb,
872                                               void *cb_cls)
873 {
874   char *list;
875   char *pos;
876   char *end;
877   char old;
878   int ret;
879
880   if (GNUNET_OK !=
881       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
882     return 0;
883   GNUNET_assert (list != NULL);
884   ret = 0;
885   pos = list;
886   while (1)
887     {
888       while (pos[0] == ' ')
889         pos++;
890       if (strlen (pos) == 0)
891         break;
892       end = pos + 1;
893       while ((end[0] != ' ') && (end[0] != '\0'))
894         {
895           if (end[0] == '\\')
896             {
897               switch (end[1])
898                 {
899                 case '\\':
900                 case ' ':
901                   memmove (end, &end[1], strlen (&end[1]) + 1);
902                 case '\0':
903                   /* illegal, but just keep it */
904                   break;
905                 default:
906                   /* illegal, but just ignore that there was a '/' */
907                   break;
908                 }
909             }
910           end++;
911         }
912       old = end[0];
913       end[0] = '\0';
914       if (strlen (pos) > 0)
915         {
916           ret++;
917           if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
918             {
919               ret = GNUNET_SYSERR;
920               break;
921             }
922         }
923       if (old == '\0')
924         break;
925       pos = end + 1;
926     }
927   GNUNET_free (list);
928   return ret;
929 }
930
931
932 /**
933  * FIXME.
934  *
935  * @param value FIXME
936  * @return FIXME
937  */
938 static char *
939 escape_name (const char *value)
940 {
941   char *escaped;
942   const char *rpos;
943   char *wpos;
944
945   escaped = GNUNET_malloc (strlen (value) * 2 + 1);
946   memset (escaped, 0, strlen (value) * 2 + 1);
947   rpos = value;
948   wpos = escaped;
949   while (rpos[0] != '\0')
950     {
951       switch (rpos[0])
952         {
953         case '\\':
954         case ' ':
955           wpos[0] = '\\';
956           wpos[1] = rpos[0];
957           wpos += 2;
958           break;
959         default:
960           wpos[0] = rpos[0];
961           wpos++;
962         }
963       rpos++;
964     }
965   return escaped;
966 }
967
968
969 /**
970  * FIXME.
971  *
972  * @param cls string we compare with (const char*)
973  * @param fn filename we are currently looking at
974  * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
975  */
976 static int
977 test_match (void *cls, const char *fn)
978 {
979   const char *of = cls;
980   return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
981 }
982
983
984 /**
985  * Append a filename to a configuration value that
986  * represents a list of filenames
987  *
988  * @param cfg configuration to update
989  * @param section section of interest
990  * @param option option of interest
991  * @param value filename to append
992  * @return GNUNET_OK on success,
993  *         GNUNET_NO if the filename already in the list
994  *         GNUNET_SYSERR on error
995  */
996 int
997 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
998                                             *cfg,
999                                             const char *section,
1000                                             const char *option,
1001                                             const char *value)
1002 {
1003   char *escaped;
1004   char *old;
1005   char *nw;
1006
1007   if (GNUNET_SYSERR
1008       == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1009                                                        section,
1010                                                        option,
1011                                                        &test_match,
1012                                                        (void *) value))
1013     return GNUNET_NO;           /* already exists */
1014   if (GNUNET_OK !=
1015       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1016     old = GNUNET_strdup ("");
1017   escaped = escape_name (value);
1018   nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1019   strcpy (nw, old);
1020   if (strlen (old) > 0)
1021     strcat (nw, " ");
1022   strcat (nw, escaped);
1023   GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1024   GNUNET_free (old);
1025   GNUNET_free (nw);
1026   GNUNET_free (escaped);
1027   return GNUNET_OK;
1028 }
1029
1030
1031 /**
1032  * Remove a filename from a configuration value that
1033  * represents a list of filenames
1034  *
1035  * @param cfg configuration to update
1036  * @param section section of interest
1037  * @param option option of interest
1038  * @param value filename to remove
1039  * @return GNUNET_OK on success,
1040  *         GNUNET_NO if the filename is not in the list,
1041  *         GNUNET_SYSERR on error
1042  */
1043 int
1044 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1045                                             *cfg,
1046                                             const char *section,
1047                                             const char *option,
1048                                             const char *value)
1049 {
1050   char *list;
1051   char *pos;
1052   char *end;
1053   char *match;
1054   char old;
1055
1056   if (GNUNET_OK !=
1057       GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1058     return GNUNET_NO;
1059   match = escape_name (value);
1060   pos = list;
1061   while (1)
1062     {
1063       while (pos[0] == ' ')
1064         pos++;
1065       if (strlen (pos) == 0)
1066         break;
1067       end = pos + 1;
1068       while ((end[0] != ' ') && (end[0] != '\0'))
1069         {
1070           if (end[0] == '\\')
1071             {
1072               switch (end[1])
1073                 {
1074                 case '\\':
1075                 case ' ':
1076                   end++;
1077                   break;
1078                 case '\0':
1079                   /* illegal, but just keep it */
1080                   break;
1081                 default:
1082                   /* illegal, but just ignore that there was a '/' */
1083                   break;
1084                 }
1085             }
1086           end++;
1087         }
1088       old = end[0];
1089       end[0] = '\0';
1090       if (0 == strcmp (pos, match))
1091         {
1092           if (old != '\0')
1093             memmove (pos, &end[1], strlen (&end[1]) + 1);
1094           else
1095             {
1096               if (pos != list) 
1097                 pos[-1] = '\0';
1098               else
1099                 pos[0] = '\0';
1100             }
1101           GNUNET_CONFIGURATION_set_value_string (cfg,
1102                                                  section, option, list);
1103           GNUNET_free (list);
1104           GNUNET_free (match);
1105           return GNUNET_OK;
1106         }        
1107       if (old == '\0')
1108         break;
1109       end[0] = old;
1110       pos = end + 1;
1111     }
1112   GNUNET_free (list);
1113   GNUNET_free (match);
1114   return GNUNET_NO;
1115 }
1116
1117
1118 /**
1119  * Load configuration (starts with defaults, then loads
1120  * system-specific configuration).
1121  *
1122  * @param cfg configuration to update
1123  * @param filename name of the configuration file, NULL to load defaults
1124  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1125  */
1126 int
1127 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1128                            const char *filename)
1129 {
1130   char *baseconfig;
1131   char *ipath;
1132
1133   ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1134   if (ipath == NULL)
1135     return GNUNET_SYSERR;
1136   baseconfig = NULL;
1137   GNUNET_asprintf (&baseconfig,
1138                    "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1139   GNUNET_free (ipath);
1140   if ((GNUNET_OK !=
1141        GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1142       (!((filename == NULL) ||
1143          (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1144     {
1145       GNUNET_free (baseconfig);
1146       return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1147     }
1148   GNUNET_free (baseconfig);
1149   if ( ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
1150                                                         "PATHS",
1151                                                         "DEFAULTCONFIG"))) &&
1152        (filename != NULL) )
1153     GNUNET_CONFIGURATION_set_value_string (cfg,
1154                                            "PATHS",
1155                                            "DEFAULTCONFIG",
1156                                            filename);
1157   if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1158                                                       "TESTING",
1159                                                       "WEAKRANDOM")) &&
1160       (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1161                                                            "TESTING",
1162                                                            "WEAKRANDOM")))
1163     GNUNET_CRYPTO_random_disable_entropy_gathering ();
1164   return GNUNET_OK;
1165 }
1166
1167
1168
1169 /* end of configuration.c */