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