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 * Used for diffing a configuration object against
108 const struct GNUNET_CONFIGURATION_Handle *cfgDefault;
109 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
115 * Create a GNUNET_CONFIGURATION_Handle.
117 * @return fresh configuration object
119 struct GNUNET_CONFIGURATION_Handle *
120 GNUNET_CONFIGURATION_create ()
122 return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
127 * Destroy configuration object.
129 * @param cfg configuration to destroy
132 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
134 struct ConfigSection *sec;
135 struct ConfigEntry *ent;
137 while (NULL != (sec = cfg->sections))
139 cfg->sections = sec->next;
140 while (NULL != (ent = sec->entries))
142 sec->entries = ent->next;
143 GNUNET_free (ent->key);
144 GNUNET_free_non_null (ent->val);
147 GNUNET_free (sec->name);
155 * Parse a configuration file, add all of the options in the
156 * file to the configuration environment.
158 * @param cfg configuration to update
159 * @param filename name of the configuration file
160 * @return GNUNET_OK on success, GNUNET_SYSERR on error
163 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
164 const char *filename)
178 fn = GNUNET_STRINGS_filename_expand (filename);
180 return GNUNET_SYSERR;
181 dirty = cfg->dirty; /* back up value! */
182 if (NULL == (fp = FOPEN (fn, "r")))
184 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
186 return GNUNET_SYSERR;
190 section = GNUNET_strdup ("");
191 memset (line, 0, 256);
193 while (NULL != fgets (line, 255, fp))
196 for (i = 0; i < 255; i++)
199 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
203 for (i = 0; (i < 255 && line[i] != 0); i++)
204 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
208 /* remove tailing whitespace */
209 for (i = strlen (line) - 1; (i >= 0) && (isspace ( (unsigned char) line[i])); i--)
211 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
214 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
215 ret = GNUNET_SYSERR; /* failed to parse included config */
217 else if (1 == sscanf (line, "[%99[^]]]", value))
220 GNUNET_free (section);
221 section = GNUNET_strdup (value);
223 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
227 i = strlen (value) - 1;
228 while ((i >= 0) && (isspace ( (unsigned char) value[i])))
235 while ((value[i] != '\0') && (value[i] != '"'))
245 GNUNET_CONFIGURATION_set_value_string (cfg,
246 section, tag, &value[i]);
248 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
251 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
256 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
258 ("Syntax error in configuration file `%s' at line %u.\n"),
264 GNUNET_assert (0 == fclose (fp));
265 /* restore dirty flag - anything we set in the meantime
268 GNUNET_free (section);
274 * Test if there are configuration options that were
275 * changed since the last save.
277 * @param cfg configuration to inspect
278 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
281 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
288 * Write configuration file.
290 * @param cfg configuration to write
291 * @param filename where to write the configuration
292 * @return GNUNET_OK on success, GNUNET_SYSERR on error
295 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
296 const char *filename)
298 struct ConfigSection *sec;
299 struct ConfigEntry *ent;
306 fn = GNUNET_STRINGS_filename_expand (filename);
308 return GNUNET_SYSERR;
309 GNUNET_DISK_directory_create_for_file (fn);
310 if (NULL == (fp = FOPEN (fn, "w")))
312 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
314 return GNUNET_SYSERR;
321 if (0 > fprintf (fp, "[%s]\n", sec->name))
329 if (ent->val != NULL)
331 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
332 strcpy (val, ent->val);
333 while (NULL != (pos = strstr (val, "\n")))
335 memmove (&pos[2], &pos[1], strlen (&pos[1]));
339 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
351 if (0 > fprintf (fp, "\n"))
359 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
360 GNUNET_assert (0 == fclose (fp));
363 cfg->dirty = GNUNET_SYSERR; /* last write failed */
364 return GNUNET_SYSERR;
366 cfg->dirty = GNUNET_NO; /* last write succeeded */
372 * Iterate over all options in the configuration.
374 * @param cfg configuration to inspect
375 * @param iter function to call on each option
376 * @param iter_cls closure for iter
379 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
380 GNUNET_CONFIGURATION_Iterator iter,
383 struct ConfigSection *spos;
384 struct ConfigEntry *epos;
386 spos = cfg->sections;
389 epos = spos->entries;
392 iter (iter_cls, spos->name, epos->key, epos->val);
401 * Copy a configuration value to the given target configuration.
402 * Overwrites existing entries.
404 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
405 * @param section section for the value
406 * @param option option name of the value
407 * @param value value to copy
410 copy_entry (void *cls,
411 const char *section, const char *option, const char *value)
413 struct GNUNET_CONFIGURATION_Handle *dst = cls;
414 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
419 * Duplicate an existing configuration object.
421 * @param cfg configuration to duplicate
422 * @return duplicate configuration
424 struct GNUNET_CONFIGURATION_Handle *
425 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
427 struct GNUNET_CONFIGURATION_Handle *ret;
429 ret = GNUNET_CONFIGURATION_create ();
430 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
439 * @param section FIXME
440 * @return matching entry, NULL if not found
442 static struct ConfigSection *
443 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
446 struct ConfigSection *pos;
449 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
456 * Find an entry from a configuration.
458 * @param cfg handle to the configuration
459 * @param section section the option is in
460 * @param key the option
461 * @return matching entry, NULL if not found
463 static struct ConfigEntry *
464 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
465 const char *section, const char *key)
467 struct ConfigSection *sec;
468 struct ConfigEntry *pos;
470 sec = findSection (cfg, section);
474 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
481 * A callback function, compares entries from two configurations
482 * (default against a new configuration) and write the diffs in a
483 * diff-configuration object (the callback object).
485 * @param cls the diff configuration (struct DiffHandle*)
486 * @param section section for the value (of the default conf.)
487 * @param option option name of the value (of the default conf.)
488 * @param value value to copy (of the default conf.)
491 compareEntries (void *cls,
492 const char *section, const char *option, const char *value)
494 struct DiffHandle *dh = cls;
495 struct ConfigEntry *entNew;
497 entNew = findEntry (dh->cfgDefault, section, option);
498 if ( (entNew != NULL) &&
499 (strcmp (entNew->val, value) == 0) )
501 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
509 * Write only configuration entries that have been changed to configuration file
510 * @param cfgDefault default configuration
511 * @param cfgNew new configuration
512 * @param filename where to write the configuration diff between default and new
513 * @return GNUNET_OK on success, GNUNET_SYSERR on error
516 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
518 const struct GNUNET_CONFIGURATION_Handle *cfgNew,
519 const char *filename)
522 struct DiffHandle diffHandle;
524 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
525 diffHandle.cfgDefault = cfgDefault;
526 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
527 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
528 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
534 * Set a configuration value that should be a string.
536 * @param cfg configuration to update
537 * @param section section of interest
538 * @param option option of interest
539 * @param value value to set
542 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
545 const char *option, const char *value)
547 struct ConfigSection *sec;
548 struct ConfigEntry *e;
550 e = findEntry (cfg, section, option);
553 GNUNET_free_non_null (e->val);
554 e->val = GNUNET_strdup (value);
557 sec = findSection (cfg, section);
560 sec = GNUNET_malloc (sizeof (struct ConfigSection));
561 sec->name = GNUNET_strdup (section);
562 sec->next = cfg->sections;
565 e = GNUNET_malloc (sizeof (struct ConfigEntry));
566 e->key = GNUNET_strdup (option);
567 e->val = GNUNET_strdup (value);
568 e->next = sec->entries;
574 * Set a configuration value that should be a number.
576 * @param cfg configuration to update
577 * @param section section of interest
578 * @param option option of interest
579 * @param number value to set
582 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
583 *cfg, const char *section,
585 unsigned long long number)
588 GNUNET_snprintf (s, 64, "%llu", number);
589 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
594 * Get a configuration value that should be a number.
596 * @param cfg configuration to inspect
597 * @param section section of interest
598 * @param option option of interest
599 * @param number where to store the numeric value of the option
600 * @return GNUNET_OK on success, GNUNET_SYSERR on error
603 GNUNET_CONFIGURATION_get_value_number (const struct
604 GNUNET_CONFIGURATION_Handle *cfg,
607 unsigned long long *number)
609 struct ConfigEntry *e;
611 e = findEntry (cfg, section, option);
613 return GNUNET_SYSERR;
614 if (1 != SSCANF (e->val, "%llu", number))
615 return GNUNET_SYSERR;
621 * Get a configuration value that should be a relative time.
623 * @param cfg configuration to inspect
624 * @param section section of interest
625 * @param option option of interest
626 * @param time set to the time value stored in the configuration
627 * @return GNUNET_OK on success, GNUNET_SYSERR on error
630 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
631 *cfg, const char *section,
633 struct GNUNET_TIME_Relative *time)
635 unsigned long long num;
638 ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
639 if (ret == GNUNET_OK)
640 time->rel_value = (uint64_t) num;
646 * Get a configuration value that should be a string.
648 * @param cfg configuration to inspect
649 * @param section section of interest
650 * @param option option of interest
651 * @param value will be set to a freshly allocated configuration
652 * value, or NULL if option is not specified
653 * @return GNUNET_OK on success, GNUNET_SYSERR on error
656 GNUNET_CONFIGURATION_get_value_string (const struct
657 GNUNET_CONFIGURATION_Handle *cfg,
659 const char *option, char **value)
661 struct ConfigEntry *e;
663 e = findEntry (cfg, section, option);
664 if ((e == NULL) || (e->val == NULL))
667 return GNUNET_SYSERR;
669 *value = GNUNET_strdup (e->val);
675 * Get a configuration value that should be in a set of
678 * @param cfg configuration to inspect
679 * @param section section of interest
680 * @param option option of interest
681 * @param choices NULL-terminated list of legal values
682 * @param value will be set to an entry in the legal list,
683 * or NULL if option is not specified and no default given
684 * @return GNUNET_OK on success, GNUNET_SYSERR on error
687 GNUNET_CONFIGURATION_get_value_choice (const struct
688 GNUNET_CONFIGURATION_Handle *cfg,
691 const char **choices,
694 struct ConfigEntry *e;
697 e = findEntry (cfg, section, option);
699 return GNUNET_SYSERR;
701 while (choices[i] != NULL)
703 if (0 == strcasecmp (choices[i], e->val))
707 if (choices[i] == NULL)
709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
710 _("Configuration value '%s' for '%s'"
711 " in section '%s' is not in set of legal choices\n"),
712 e->val, option, section);
713 return GNUNET_SYSERR;
721 * Test if we have a value for a particular option
722 * @param cfg configuration to inspect
723 * @param section section of interest
724 * @param option option of interest
725 * @return GNUNET_YES if so, GNUNET_NO if not.
728 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
729 *cfg, const char *section,
732 struct ConfigEntry *e;
733 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
740 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
741 * where either in the "PATHS" section or the environtment
742 * "FOO" is set to "DIRECTORY".
744 * @param cfg configuration to use for path expansion
745 * @param orig string to $-expand (will be freed!)
746 * @return $-expanded string
749 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
761 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
772 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
776 if (NULL == (env = getenv (&orig[1])))
778 orig[i] = DIR_SEPARATOR;
781 prefix = GNUNET_strdup (env);
783 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
784 strcpy (result, prefix);
785 if ((strlen (prefix) == 0) ||
786 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
787 strcat (result, DIR_SEPARATOR_STR);
788 strcat (result, post);
789 GNUNET_free (prefix);
796 * Get a configuration value that should be a string.
798 * @param cfg configuration to inspect
799 * @param section section of interest
800 * @param option option of interest
801 * @param value will be set to a freshly allocated configuration
802 * value, or NULL if option is not specified
803 * @return GNUNET_OK on success, GNUNET_SYSERR on error
806 GNUNET_CONFIGURATION_get_value_filename (const struct
807 GNUNET_CONFIGURATION_Handle *cfg,
809 const char *option, char **value)
814 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
817 return GNUNET_SYSERR;
819 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
820 *value = GNUNET_STRINGS_filename_expand (tmp);
823 return GNUNET_SYSERR;
829 * Get a configuration value that should be in a set of
830 * "GNUNET_YES" or "GNUNET_NO".
832 * @param cfg configuration to inspect
833 * @param section section of interest
834 * @param option option of interest
835 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
838 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
839 *cfg, const char *section,
842 static const char *yesno[] = { "YES", "NO", NULL };
846 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
847 section, option, yesno, &val);
848 if (ret == GNUNET_SYSERR)
857 * Iterate over the set of filenames stored in a configuration value.
859 * @param cfg configuration to inspect
860 * @param section section of interest
861 * @param option option of interest
862 * @param cb function to call on each filename
863 * @param cb_cls closure for cb
864 * @return number of filenames iterated over, -1 on error
867 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
868 GNUNET_CONFIGURATION_Handle
869 *cfg, const char *section,
871 GNUNET_FileNameCallback cb,
881 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
883 GNUNET_assert (list != NULL);
888 while (pos[0] == ' ')
890 if (strlen (pos) == 0)
893 while ((end[0] != ' ') && (end[0] != '\0'))
901 memmove (end, &end[1], strlen (&end[1]) + 1);
903 /* illegal, but just keep it */
906 /* illegal, but just ignore that there was a '/' */
914 if (strlen (pos) > 0)
917 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
939 escape_name (const char *value)
945 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
946 memset (escaped, 0, strlen (value) * 2 + 1);
949 while (rpos[0] != '\0')
972 * @param cls string we compare with (const char*)
973 * @param fn filename we are currently looking at
974 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
977 test_match (void *cls, const char *fn)
979 const char *of = cls;
980 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
985 * Append a filename to a configuration value that
986 * represents a list of filenames
988 * @param cfg configuration to update
989 * @param section section of interest
990 * @param option option of interest
991 * @param value filename to append
992 * @return GNUNET_OK on success,
993 * GNUNET_NO if the filename already in the list
994 * GNUNET_SYSERR on error
997 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1008 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1013 return GNUNET_NO; /* already exists */
1015 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1016 old = GNUNET_strdup ("");
1017 escaped = escape_name (value);
1018 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1021 strcat (nw, escaped);
1022 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1025 GNUNET_free (escaped);
1031 * Remove a filename from a configuration value that
1032 * represents a list of filenames
1034 * @param cfg configuration to update
1035 * @param section section of interest
1036 * @param option option of interest
1037 * @param value filename to remove
1038 * @return GNUNET_OK on success,
1039 * GNUNET_NO if the filename is not in the list,
1040 * GNUNET_SYSERR on error
1043 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1045 const char *section,
1056 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1058 match = escape_name (value);
1062 while (pos[0] == ' ')
1064 if (strlen (pos) == 0)
1067 while ((end[0] != ' ') && (end[0] != '\0'))
1078 /* illegal, but just keep it */
1081 /* illegal, but just ignore that there was a '/' */
1089 if (strlen (pos) > 0)
1091 if (0 == strcmp (pos, match))
1093 memmove (pos, &end[1], strlen (&end[1]) + 1);
1096 pos[-1] = ' '; /* previously changed to "\0" */
1097 GNUNET_CONFIGURATION_set_value_string (cfg,
1098 section, option, list);
1100 GNUNET_free (match);
1109 GNUNET_free (match);
1115 * Load configuration (starts with defaults, then loads
1116 * system-specific configuration).
1118 * @param cfg configuration to update
1119 * @param filename name of the configuration file
1120 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1123 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1124 const char *filename)
1129 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1131 return GNUNET_SYSERR;
1133 GNUNET_asprintf (&baseconfig,
1134 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1135 GNUNET_free (ipath);
1137 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1138 (!((filename == NULL) ||
1139 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1141 GNUNET_free (baseconfig);
1142 return GNUNET_SYSERR;
1144 GNUNET_free (baseconfig);
1145 if ( ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
1147 "DEFAULTCONFIG"))) &&
1148 (filename != NULL) )
1149 GNUNET_CONFIGURATION_set_value_string (cfg,
1153 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1156 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1159 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1165 /* end of configuration.c */