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"
38 * @brief configuration entry
44 * This is a linked list.
46 struct ConfigEntry *next;
54 * current, commited value
61 * @brief configuration section
66 * This is a linked list.
68 struct ConfigSection *next;
71 * entries in the section
73 struct ConfigEntry *entries;
83 * @brief configuration data
85 struct GNUNET_CONFIGURATION_Handle
88 * Configuration sections.
90 struct ConfigSection *sections;
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)
103 * Create a GNUNET_CONFIGURATION_Handle.
105 * @return fresh configuration object
107 struct GNUNET_CONFIGURATION_Handle *
108 GNUNET_CONFIGURATION_create ()
110 return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
115 * Destroy configuration object.
117 * @param cfg configuration to destroy
120 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
122 struct ConfigSection *sec;
123 struct ConfigEntry *ent;
125 while (NULL != (sec = cfg->sections))
127 cfg->sections = sec->next;
128 while (NULL != (ent = sec->entries))
130 sec->entries = ent->next;
131 GNUNET_free (ent->key);
132 GNUNET_free_non_null (ent->val);
135 GNUNET_free (sec->name);
143 * Parse a configuration file, add all of the options in the
144 * file to the configuration environment.
146 * @param cfg configuration to update
147 * @param filename name of the configuration file
148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152 const char *filename)
166 fn = GNUNET_STRINGS_filename_expand (filename);
167 dirty = cfg->dirty; /* back up value! */
168 if (NULL == (fp = FOPEN (fn, "r")))
170 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
172 return GNUNET_SYSERR;
176 section = GNUNET_strdup ("");
177 memset (line, 0, 256);
179 while (NULL != fgets (line, 255, fp))
182 for (i = 0; i < 255; i++)
185 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
189 for (i = 0; (i < 255 && line[i] != 0); i++)
190 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
194 /* remove tailing whitespace */
195 for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
197 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
200 if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
201 ret = GNUNET_SYSERR; /* failed to parse included config */
203 else if (1 == sscanf (line, "[%99[^]]]", value))
206 GNUNET_free (section);
207 section = GNUNET_strdup (value);
209 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
213 i = strlen (value) - 1;
214 while ((i >= 0) && (isspace (value[i])))
221 while ((value[i] != '\0') && (value[i] != '"'))
231 GNUNET_CONFIGURATION_set_value_string (cfg,
232 section, tag, &value[i]);
234 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
237 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
244 ("Syntax error in configuration file `%s' at line %u.\n"),
250 GNUNET_assert (0 == fclose (fp));
251 /* restore dirty flag - anything we set in the meantime
254 GNUNET_free (section);
260 * Test if there are configuration options that were
261 * changed since the last save.
263 * @param cfg configuration to inspect
264 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 * Write configuration file.
276 * @param cfg configuration to write
277 * @param filename where to write the configuration
278 * @return GNUNET_OK on success, GNUNET_SYSERR on error
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
282 const char *filename)
284 struct ConfigSection *sec;
285 struct ConfigEntry *ent;
292 fn = GNUNET_STRINGS_filename_expand (filename);
293 GNUNET_DISK_directory_create_for_file (fn);
294 if (NULL == (fp = FOPEN (fn, "w")))
296 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
298 return GNUNET_SYSERR;
305 if (0 > fprintf (fp, "[%s]\n", sec->name))
313 if (ent->val != NULL)
315 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
316 strcpy (val, ent->val);
317 while (NULL != (pos = strstr (val, "\n")))
319 memmove (&pos[2], &pos[1], strlen (&pos[1]));
323 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
335 if (0 > fprintf (fp, "\n"))
343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
344 GNUNET_assert (0 == fclose (fp));
347 cfg->dirty = GNUNET_SYSERR; /* last write failed */
348 return GNUNET_SYSERR;
350 cfg->dirty = GNUNET_NO; /* last write succeeded */
356 * Iterate over all options in the configuration.
358 * @param cfg configuration to inspect
359 * @param iter function to call on each option
360 * @param iter_cls closure for iter
362 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
363 GNUNET_CONFIGURATION_Iterator iter,
366 struct ConfigSection *spos;
367 struct ConfigEntry *epos;
369 spos = cfg->sections;
372 epos = spos->entries;
375 iter (iter_cls, spos->name, epos->key, epos->val);
384 * Copy a configuration value to the given target configuration.
385 * Overwrites existing entries.
387 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
388 * @param section section for the value
389 * @param option option name of the value
390 * @param value value to copy
393 copy_entry (void *cls,
398 struct GNUNET_CONFIGURATION_Handle *dst = cls;
399 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
404 * Duplicate an existing configuration object.
406 * @param cfg configuration to duplicate
407 * @return duplicate configuration
409 struct GNUNET_CONFIGURATION_Handle *
410 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
412 struct GNUNET_CONFIGURATION_Handle *ret;
414 ret = GNUNET_CONFIGURATION_create ();
415 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
424 * @param section FIXME
425 * @return matching entry, NULL if not found
427 static struct ConfigSection *
428 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
430 struct ConfigSection *pos;
433 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
443 * @param section FIXME
445 * @return matching entry, NULL if not found
447 static struct ConfigEntry *
448 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
449 const char *section, const char *key)
451 struct ConfigSection *sec;
452 struct ConfigEntry *pos;
454 sec = findSection (cfg, section);
458 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
465 * Set a configuration value that should be a string.
467 * @param cfg configuration to update
468 * @param section section of interest
469 * @param option option of interest
470 * @param value value to set
473 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
476 const char *option, const char *value)
478 struct ConfigSection *sec;
479 struct ConfigEntry *e;
481 e = findEntry (cfg, section, option);
484 GNUNET_free_non_null (e->val);
485 e->val = GNUNET_strdup (value);
488 sec = findSection (cfg, section);
491 sec = GNUNET_malloc (sizeof (struct ConfigSection));
492 sec->name = GNUNET_strdup (section);
493 sec->next = cfg->sections;
496 e = GNUNET_malloc (sizeof (struct ConfigEntry));
497 e->key = GNUNET_strdup (option);
498 e->val = GNUNET_strdup (value);
499 e->next = sec->entries;
505 * Set a configuration value that should be a number.
507 * @param cfg configuration to update
508 * @param section section of interest
509 * @param option option of interest
510 * @param number value to set
513 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
514 *cfg, const char *section,
516 unsigned long long number)
519 GNUNET_snprintf (s, 64, "%llu", number);
520 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
525 * Get a configuration value that should be a number.
527 * @param cfg configuration to inspect
528 * @param section section of interest
529 * @param option option of interest
530 * @param number where to store the numeric value of the option
531 * @return GNUNET_OK on success, GNUNET_SYSERR on error
534 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
535 *cfg, const char *section,
537 unsigned long long *number)
539 struct ConfigEntry *e;
541 e = findEntry (cfg, section, option);
543 return GNUNET_SYSERR;
544 if (1 != SSCANF (e->val, "%llu", number))
545 return GNUNET_SYSERR;
551 * Get a configuration value that should be a relative time.
553 * @param cfg configuration to inspect
554 * @param section section of interest
555 * @param option option of interest
556 * @param time set to the time value stored in the configuration
557 * @return GNUNET_OK on success, GNUNET_SYSERR on error
560 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
561 *cfg, const char *section,
563 struct GNUNET_TIME_Relative *time)
565 unsigned long long num;
568 ret = GNUNET_CONFIGURATION_get_value_number (cfg,
572 if (ret == GNUNET_OK)
573 time->value = (uint64_t) num;
579 * Get a configuration value that should be a string.
581 * @param cfg configuration to inspect
582 * @param section section of interest
583 * @param option option of interest
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_string (const struct GNUNET_CONFIGURATION_Handle
590 *cfg, const char *section,
591 const char *option, char **value)
593 struct ConfigEntry *e;
595 e = findEntry (cfg, section, option);
596 if ((e == NULL) || (e->val == NULL))
599 return GNUNET_SYSERR;
601 *value = GNUNET_strdup (e->val);
607 * Get a configuration value that should be in a set of
610 * @param cfg configuration to inspect
611 * @param section section of interest
612 * @param option option of interest
613 * @param choices NULL-terminated list of legal values
614 * @param value will be set to an entry in the legal list,
615 * or NULL if option is not specified and no default given
616 * @return GNUNET_OK on success, GNUNET_SYSERR on error
619 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
620 *cfg, const char *section,
622 const char **choices,
625 struct ConfigEntry *e;
628 e = findEntry (cfg, section, option);
630 return GNUNET_SYSERR;
632 while (choices[i] != NULL)
634 if (0 == strcasecmp (choices[i], e->val))
638 if (choices[i] == NULL)
640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
641 _("Configuration value '%s' for '%s'"
642 " in section '%s' is not in set of legal choices\n"),
643 e->val, option, section);
644 return GNUNET_SYSERR;
652 * Test if we have a value for a particular option
653 * @param cfg configuration to inspect
654 * @param section section of interest
655 * @param option option of interest
656 * @return GNUNET_YES if so, GNUNET_NO if not.
659 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
660 const char *section, const char *option)
662 struct ConfigEntry *e;
663 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
670 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
671 * where either in the "PATHS" section or the environtment
672 * "FOO" is set to "DIRECTORY".
674 * @param cfg configuration to use for path expansion
675 * @param orig string to $-expand (will be freed!)
676 * @return $-expanded string
679 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
691 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
702 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
706 if (NULL == (env = getenv (&orig[1])))
708 orig[i] = DIR_SEPARATOR;
711 prefix = GNUNET_strdup (env);
713 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
714 strcpy (result, prefix);
715 if ((strlen (prefix) == 0) ||
716 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
717 strcat (result, DIR_SEPARATOR_STR);
718 strcat (result, post);
719 GNUNET_free (prefix);
726 * Get a configuration value that should be a string.
728 * @param cfg configuration to inspect
729 * @param section section of interest
730 * @param option option of interest
731 * @param value will be set to a freshly allocated configuration
732 * value, or NULL if option is not specified
733 * @return GNUNET_OK on success, GNUNET_SYSERR on error
736 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle
737 *cfg, const char *section,
738 const char *option, char **value)
744 ret = GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp);
745 if (ret == GNUNET_SYSERR)
749 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
750 *value = GNUNET_STRINGS_filename_expand (tmp);
764 * Get a configuration value that should be in a set of
765 * "GNUNET_YES" or "GNUNET_NO".
767 * @param cfg configuration to inspect
768 * @param section section of interest
769 * @param option option of interest
770 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
773 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
774 const char *section, const char *option)
776 static const char *yesno[] = { "YES", "NO", NULL };
780 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
781 section, option, yesno, &val);
782 if (ret == GNUNET_SYSERR)
791 * Iterate over the set of filenames stored in a configuration value.
793 * @param cfg configuration to inspect
794 * @param section section of interest
795 * @param option option of interest
796 * @param cb function to call on each filename
797 * @param cb_cls closure for cb
798 * @return number of filenames iterated over, -1 on error
801 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
802 GNUNET_CONFIGURATION_Handle
803 *cfg, const char *section,
805 GNUNET_FileNameCallback cb,
815 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
817 GNUNET_assert (list != NULL);
822 while (pos[0] == ' ')
824 if (strlen (pos) == 0)
827 while ((end[0] != ' ') && (end[0] != '\0'))
835 memmove (end, &end[1], strlen (&end[1]) + 1);
837 /* illegal, but just keep it */
840 /* illegal, but just ignore that there was a '/' */
848 if (strlen (pos) > 0)
851 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
873 escape_name (const char *value)
879 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
880 memset (escaped, 0, strlen (value) * 2 + 1);
883 while (rpos[0] != '\0')
906 * @param cls string we compare with (const char*)
907 * @param fn filename we are currently looking at
908 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
911 test_match (void *cls, const char *fn)
913 const char *of = cls;
914 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
919 * Append a filename to a configuration value that
920 * represents a list of filenames
922 * @param cfg configuration to update
923 * @param section section of interest
924 * @param option option of interest
925 * @param value filename to append
926 * @return GNUNET_OK on success,
927 * GNUNET_NO if the filename already in the list
928 * GNUNET_SYSERR on error
931 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
942 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
947 return GNUNET_NO; /* already exists */
949 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
950 old = GNUNET_strdup ("");
951 escaped = escape_name (value);
952 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
955 strcat (nw, escaped);
956 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
959 GNUNET_free (escaped);
965 * Remove a filename from a configuration value that
966 * represents a list of filenames
968 * @param cfg configuration to update
969 * @param section section of interest
970 * @param option option of interest
971 * @param value filename to remove
972 * @return GNUNET_OK on success,
973 * GNUNET_NO if the filename is not in the list,
974 * GNUNET_SYSERR on error
977 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
990 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
992 match = escape_name (value);
996 while (pos[0] == ' ')
998 if (strlen (pos) == 0)
1001 while ((end[0] != ' ') && (end[0] != '\0'))
1012 /* illegal, but just keep it */
1015 /* illegal, but just ignore that there was a '/' */
1023 if (strlen (pos) > 0)
1025 if (0 == strcmp (pos, match))
1027 memmove (pos, &end[1], strlen (&end[1]) + 1);
1030 pos[-1] = ' '; /* previously changed to "\0" */
1031 GNUNET_CONFIGURATION_set_value_string (cfg,
1032 section, option, list);
1034 GNUNET_free (match);
1043 GNUNET_free (match);
1049 * Load configuration (starts with defaults, then loads
1050 * system-specific configuration).
1052 * @param cfg configuration to update
1053 * @param filename name of the configuration file
1054 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1057 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1058 const char *filename)
1063 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1065 return GNUNET_SYSERR;
1067 GNUNET_asprintf (&baseconfig,
1068 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1069 GNUNET_free (ipath);
1071 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1072 (!((filename == NULL) ||
1073 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1075 GNUNET_free (baseconfig);
1076 return GNUNET_SYSERR;
1078 GNUNET_free (baseconfig);
1079 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1082 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1085 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1091 /* end of configuration.c */