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