2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file src/util/configuration.c
23 * @brief configuration management
25 * @author Christian Grothoff
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"
37 * @brief configuration entry
43 * This is a linked list.
45 struct ConfigEntry *next;
53 * current, commited value
59 * @brief configuration section
64 * This is a linked list.
66 struct ConfigSection *next;
69 * entries in the section
71 struct ConfigEntry *entries;
80 * @brief configuration data
82 struct GNUNET_CONFIGURATION_Handle
85 * Configuration sections.
87 struct ConfigSection *sections;
90 * Modification indication since last save
91 * GNUNET_NO if clean, GNUNET_YES if dirty,
92 * GNUNET_SYSERR on error (i.e. last save failed)
99 * Create a GNUNET_CONFIGURATION_Configuration.
101 struct GNUNET_CONFIGURATION_Handle *
102 GNUNET_CONFIGURATION_create ()
104 return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
108 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
110 struct ConfigSection *sec;
111 struct ConfigEntry *ent;
113 while (NULL != (sec = cfg->sections))
115 cfg->sections = sec->next;
116 while (NULL != (ent = sec->entries))
118 sec->entries = ent->next;
119 GNUNET_free (ent->key);
120 GNUNET_free_non_null (ent->val);
123 GNUNET_free (sec->name);
130 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
131 const char *filename)
145 fn = GNUNET_STRINGS_filename_expand (filename);
146 dirty = cfg->dirty; /* back up value! */
147 if (NULL == (fp = FOPEN (fn, "r")))
149 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
151 return GNUNET_SYSERR;
155 section = GNUNET_strdup ("");
156 memset (line, 0, 256);
158 while (NULL != fgets (line, 255, fp))
161 for (i = 0; i < 255; i++)
164 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
168 for (i = 0; (i < 255 && line[i] != 0); i++)
169 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
173 /* remove tailing whitespace */
174 for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
176 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
179 if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
180 ret = GNUNET_SYSERR; /* failed to parse included config */
182 else if (1 == sscanf (line, "[%99[^]]]", value))
185 GNUNET_free (section);
186 section = GNUNET_strdup (value);
188 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
192 i = strlen (value) - 1;
193 while ((i >= 0) && (isspace (value[i])))
200 while ((value[i] != '\0') && (value[i] != '"'))
210 GNUNET_CONFIGURATION_set_value_string (cfg,
211 section, tag, &value[i]);
213 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
216 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
223 ("Syntax error in configuration file `%s' at line %u.\n"),
229 GNUNET_assert (0 == fclose (fp));
230 /* restore dirty flag - anything we set in the meantime
233 GNUNET_free (section);
238 GNUNET_CONFIGURATION_test_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
244 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *data,
245 const char *filename)
247 struct ConfigSection *sec;
248 struct ConfigEntry *ent;
255 fn = GNUNET_STRINGS_filename_expand (filename);
256 GNUNET_DISK_directory_create_for_file (fn);
257 if (NULL == (fp = FOPEN (fn, "w")))
259 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
261 return GNUNET_SYSERR;
265 sec = data->sections;
268 if (0 > fprintf (fp, "[%s]\n", sec->name))
276 if (ent->val != NULL)
278 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
279 strcpy (val, ent->val);
280 while (NULL != (pos = strstr (val, "\n")))
282 memmove (&pos[2], &pos[1], strlen (&pos[1]));
286 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
298 if (0 > fprintf (fp, "\n"))
306 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
307 GNUNET_assert (0 == fclose (fp));
310 data->dirty = GNUNET_SYSERR; /* last write failed */
311 return GNUNET_SYSERR;
313 data->dirty = GNUNET_NO; /* last write succeeded */
318 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
319 GNUNET_CONFIGURATION_Iterator iter,
322 struct ConfigSection *spos;
323 struct ConfigEntry *epos;
325 spos = cfg->sections;
328 epos = spos->entries;
331 iter (iter_cls, spos->name, epos->key, epos->val);
340 copy_entry (void *cls,
345 struct GNUNET_CONFIGURATION_Handle *dst = cls;
346 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
350 struct GNUNET_CONFIGURATION_Handle *
351 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
353 struct GNUNET_CONFIGURATION_Handle *ret;
355 ret = GNUNET_CONFIGURATION_create ();
356 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
361 static struct ConfigSection *
362 findSection (const struct GNUNET_CONFIGURATION_Handle *data, const char *section)
364 struct ConfigSection *pos;
366 pos = data->sections;
367 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
373 static struct ConfigEntry *
374 findEntry (const struct GNUNET_CONFIGURATION_Handle *data,
375 const char *section, const char *key)
377 struct ConfigSection *sec;
378 struct ConfigEntry *pos;
380 sec = findSection (data, section);
384 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
390 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
393 const char *option, const char *value)
395 struct ConfigSection *sec;
396 struct ConfigEntry *e;
398 e = findEntry (data, section, option);
401 GNUNET_free_non_null (e->val);
402 e->val = GNUNET_strdup (value);
405 sec = findSection (data, section);
408 sec = GNUNET_malloc (sizeof (struct ConfigSection));
409 sec->name = GNUNET_strdup (section);
410 sec->next = data->sections;
411 data->sections = sec;
413 e = GNUNET_malloc (sizeof (struct ConfigEntry));
414 e->key = GNUNET_strdup (option);
415 e->val = GNUNET_strdup (value);
416 e->next = sec->entries;
421 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
422 *cfg, const char *section,
424 unsigned long long number)
427 GNUNET_snprintf (s, 64, "%llu", number);
428 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
432 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
433 *cfg, const char *section,
435 unsigned long long *number)
437 struct ConfigEntry *e;
439 e = findEntry (cfg, section, option);
441 return GNUNET_SYSERR;
442 if (1 != SSCANF (e->val, "%llu", number))
443 return GNUNET_SYSERR;
448 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
449 *cfg, const char *section,
451 struct GNUNET_TIME_Relative *time)
453 unsigned long long num;
456 ret = GNUNET_CONFIGURATION_get_value_number (cfg,
460 if (ret == GNUNET_OK)
461 time->value = (uint64_t) num;
466 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
467 *cfg, const char *section,
468 const char *option, char **value)
470 struct ConfigEntry *e;
472 e = findEntry (cfg, section, option);
473 if ((e == NULL) || (e->val == NULL))
476 return GNUNET_SYSERR;
478 *value = GNUNET_strdup (e->val);
483 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
484 *cfg, const char *section,
486 const char **choices,
489 struct ConfigEntry *e;
492 e = findEntry (cfg, section, option);
494 return GNUNET_SYSERR;
496 while (choices[i] != NULL)
498 if (0 == strcasecmp (choices[i], e->val))
502 if (choices[i] == NULL)
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505 _("Configuration value '%s' for '%s'"
506 " in section '%s' is not in set of legal choices\n"),
507 e->val, option, section);
508 return GNUNET_SYSERR;
515 * Test if we have a value for a particular option
516 * @return GNUNET_YES if so, GNUNET_NO if not.
519 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
520 const char *section, const char *option)
522 struct ConfigEntry *e;
523 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
529 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
530 * where either in the "PATHS" section or the environtment
531 * "FOO" is set to "DIRECTORY".
533 * @param old string to $-expand (will be freed!)
534 * @return $-expanded string
537 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
549 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
560 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
564 if (NULL == (env = getenv (&orig[1])))
566 orig[i] = DIR_SEPARATOR;
569 prefix = GNUNET_strdup (env);
571 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
572 strcpy (result, prefix);
573 if ((strlen (prefix) == 0) ||
574 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
575 strcat (result, DIR_SEPARATOR_STR);
576 strcat (result, post);
577 GNUNET_free (prefix);
583 * Get a configuration value that should be a string.
584 * @param value will be set to a freshly allocated configuration
585 * value, or NULL if option is not specified
586 * @return GNUNET_OK on success, GNUNET_SYSERR on error
589 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle
590 *data, const char *section,
591 const char *option, char **value)
597 ret = GNUNET_CONFIGURATION_get_value_string (data, section, option, &tmp);
598 if (ret == GNUNET_SYSERR)
602 tmp = GNUNET_CONFIGURATION_expand_dollar (data, tmp);
603 *value = GNUNET_STRINGS_filename_expand (tmp);
614 * Get a configuration value that should be in a set of
615 * "GNUNET_YES" or "GNUNET_NO".
617 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
620 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
621 const char *section, const char *option)
623 static const char *yesno[] = { "YES", "NO", NULL };
627 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
628 section, option, yesno, &val);
629 if (ret == GNUNET_SYSERR)
638 * Iterate over the set of filenames stored in a configuration value.
640 * @return number of filenames iterated over, -1 on error
643 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
644 GNUNET_CONFIGURATION_Handle
645 *cfg, const char *section,
647 GNUNET_FileNameCallback cb,
657 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
659 GNUNET_assert (list != NULL);
664 while (pos[0] == ' ')
666 if (strlen (pos) == 0)
669 while ((end[0] != ' ') && (end[0] != '\0'))
677 memmove (end, &end[1], strlen (&end[1]) + 1);
679 /* illegal, but just keep it */
682 /* illegal, but just ignore that there was a '/' */
690 if (strlen (pos) > 0)
693 if ((cb != NULL) && (GNUNET_OK != cb (cls, pos)))
708 escape_name (const char *value)
714 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
715 memset (escaped, 0, strlen (value) * 2 + 1);
718 while (rpos[0] != '\0')
738 test_match (void *cls, const char *fn)
740 const char *of = cls;
741 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
745 * Append a filename to a configuration value that
746 * represents a list of filenames
748 * @param value filename to append
749 * @return GNUNET_OK on success,
750 * GNUNET_NO if the filename already in the list
751 * GNUNET_SYSERR on error
754 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
765 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
770 return GNUNET_NO; /* already exists */
772 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
773 old = GNUNET_strdup ("");
774 escaped = escape_name (value);
775 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
778 strcat (nw, escaped);
779 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
782 GNUNET_free (escaped);
788 * Remove a filename from a configuration value that
789 * represents a list of filenames
791 * @param value filename to remove
792 * @return GNUNET_OK on success,
793 * GNUNET_NO if the filename is not in the list,
794 * GNUNET_SYSERR on error
797 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
811 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
813 match = escape_name (value);
818 while (pos[0] == ' ')
820 if (strlen (pos) == 0)
823 while ((end[0] != ' ') && (end[0] != '\0'))
834 /* illegal, but just keep it */
837 /* illegal, but just ignore that there was a '/' */
845 if (strlen (pos) > 0)
847 if (0 == strcmp (pos, match))
849 memmove (pos, &end[1], strlen (&end[1]) + 1);
852 pos[-1] = ' '; /* previously changed to "\0" */
853 GNUNET_CONFIGURATION_set_value_string (cfg,
854 section, option, list);
871 * Load configuration (starts with defaults, then loads
872 * system-specific configuration).
875 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
881 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
883 return GNUNET_SYSERR;
885 GNUNET_asprintf (&baseconfig,
886 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
889 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
890 (!((cfgfn == NULL) ||
891 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, cfgfn)))))
893 GNUNET_free (baseconfig);
894 return GNUNET_SYSERR;
896 GNUNET_free (baseconfig);
897 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
900 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
903 GNUNET_CRYPTO_random_disable_entropy_gathering ();
909 /* end of configuration.c */