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 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
312 return GNUNET_SYSERR;
314 if (NULL == (fp = FOPEN (fn, "w")))
316 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
318 return GNUNET_SYSERR;
325 if (0 > fprintf (fp, "[%s]\n", sec->name))
333 if (ent->val != NULL)
335 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
336 strcpy (val, ent->val);
337 while (NULL != (pos = strstr (val, "\n")))
339 memmove (&pos[2], &pos[1], strlen (&pos[1]));
343 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
355 if (0 > fprintf (fp, "\n"))
363 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
364 GNUNET_assert (0 == fclose (fp));
367 cfg->dirty = GNUNET_SYSERR; /* last write failed */
368 return GNUNET_SYSERR;
370 cfg->dirty = GNUNET_NO; /* last write succeeded */
376 * Iterate over all options in the configuration.
378 * @param cfg configuration to inspect
379 * @param iter function to call on each option
380 * @param iter_cls closure for iter
383 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
384 GNUNET_CONFIGURATION_Iterator iter,
387 struct ConfigSection *spos;
388 struct ConfigEntry *epos;
390 spos = cfg->sections;
393 epos = spos->entries;
396 iter (iter_cls, spos->name, epos->key, epos->val);
405 * Iterate over all sections in the configuration.
407 * @param cfg configuration to inspect
408 * @param iter function to call on each section
409 * @param iter_cls closure for iter
412 GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg,
413 GNUNET_CONFIGURATION_Section_Iterator iter,
416 struct ConfigSection *spos;
418 for (spos = cfg->sections; spos != NULL; spos = spos->next)
419 iter (iter_cls, spos->name);
424 * Copy a configuration value to the given target configuration.
425 * Overwrites existing entries.
427 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
428 * @param section section for the value
429 * @param option option name of the value
430 * @param value value to copy
433 copy_entry (void *cls,
434 const char *section, const char *option, const char *value)
436 struct GNUNET_CONFIGURATION_Handle *dst = cls;
437 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
442 * Duplicate an existing configuration object.
444 * @param cfg configuration to duplicate
445 * @return duplicate configuration
447 struct GNUNET_CONFIGURATION_Handle *
448 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
450 struct GNUNET_CONFIGURATION_Handle *ret;
452 ret = GNUNET_CONFIGURATION_create ();
453 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
462 * @param section FIXME
463 * @return matching entry, NULL if not found
465 static struct ConfigSection *
466 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
469 struct ConfigSection *pos;
472 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
479 * Find an entry from a configuration.
481 * @param cfg handle to the configuration
482 * @param section section the option is in
483 * @param key the option
484 * @return matching entry, NULL if not found
486 static struct ConfigEntry *
487 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
488 const char *section, const char *key)
490 struct ConfigSection *sec;
491 struct ConfigEntry *pos;
493 sec = findSection (cfg, section);
497 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
504 * A callback function, compares entries from two configurations
505 * (default against a new configuration) and write the diffs in a
506 * diff-configuration object (the callback object).
508 * @param cls the diff configuration (struct DiffHandle*)
509 * @param section section for the value (of the default conf.)
510 * @param option option name of the value (of the default conf.)
511 * @param value value to copy (of the default conf.)
514 compareEntries (void *cls,
515 const char *section, const char *option, const char *value)
517 struct DiffHandle *dh = cls;
518 struct ConfigEntry *entNew;
520 entNew = findEntry (dh->cfgDefault, section, option);
521 if ( (entNew != NULL) &&
522 (strcmp (entNew->val, value) == 0) )
524 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
532 * Write only configuration entries that have been changed to configuration file
533 * @param cfgDefault default configuration
534 * @param cfgNew new configuration
535 * @param filename where to write the configuration diff between default and new
536 * @return GNUNET_OK on success, GNUNET_SYSERR on error
539 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
541 const struct GNUNET_CONFIGURATION_Handle *cfgNew,
542 const char *filename)
545 struct DiffHandle diffHandle;
547 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
548 diffHandle.cfgDefault = cfgDefault;
549 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
550 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
551 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
557 * Set a configuration value that should be a string.
559 * @param cfg configuration to update
560 * @param section section of interest
561 * @param option option of interest
562 * @param value value to set
565 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
568 const char *option, const char *value)
570 struct ConfigSection *sec;
571 struct ConfigEntry *e;
573 e = findEntry (cfg, section, option);
576 GNUNET_free_non_null (e->val);
577 e->val = GNUNET_strdup (value);
580 sec = findSection (cfg, section);
583 sec = GNUNET_malloc (sizeof (struct ConfigSection));
584 sec->name = GNUNET_strdup (section);
585 sec->next = cfg->sections;
588 e = GNUNET_malloc (sizeof (struct ConfigEntry));
589 e->key = GNUNET_strdup (option);
590 e->val = GNUNET_strdup (value);
591 e->next = sec->entries;
597 * Set a configuration value that should be a number.
599 * @param cfg configuration to update
600 * @param section section of interest
601 * @param option option of interest
602 * @param number value to set
605 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
606 *cfg, const char *section,
608 unsigned long long number)
611 GNUNET_snprintf (s, 64, "%llu", number);
612 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
617 * Get a configuration value that should be a number.
619 * @param cfg configuration to inspect
620 * @param section section of interest
621 * @param option option of interest
622 * @param number where to store the numeric value of the option
623 * @return GNUNET_OK on success, GNUNET_SYSERR on error
626 GNUNET_CONFIGURATION_get_value_number (const struct
627 GNUNET_CONFIGURATION_Handle *cfg,
630 unsigned long long *number)
632 struct ConfigEntry *e;
634 e = findEntry (cfg, section, option);
636 return GNUNET_SYSERR;
637 if (1 != SSCANF (e->val, "%llu", number))
638 return GNUNET_SYSERR;
644 * Get a configuration value that should be a relative time.
646 * @param cfg configuration to inspect
647 * @param section section of interest
648 * @param option option of interest
649 * @param time set to the time value stored in the configuration
650 * @return GNUNET_OK on success, GNUNET_SYSERR on error
653 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
654 *cfg, const char *section,
656 struct GNUNET_TIME_Relative *time)
658 unsigned long long num;
661 ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
662 if (ret == GNUNET_OK)
663 time->rel_value = (uint64_t) num;
669 * Get a configuration value that should be a string.
671 * @param cfg configuration to inspect
672 * @param section section of interest
673 * @param option option of interest
674 * @param value will be set to a freshly allocated configuration
675 * value, or NULL if option is not specified
676 * @return GNUNET_OK on success, GNUNET_SYSERR on error
679 GNUNET_CONFIGURATION_get_value_string (const struct
680 GNUNET_CONFIGURATION_Handle *cfg,
682 const char *option, char **value)
684 struct ConfigEntry *e;
686 e = findEntry (cfg, section, option);
687 if ((e == NULL) || (e->val == NULL))
690 return GNUNET_SYSERR;
692 *value = GNUNET_strdup (e->val);
698 * Get a configuration value that should be in a set of
701 * @param cfg configuration to inspect
702 * @param section section of interest
703 * @param option option of interest
704 * @param choices NULL-terminated list of legal values
705 * @param value will be set to an entry in the legal list,
706 * or NULL if option is not specified and no default given
707 * @return GNUNET_OK on success, GNUNET_SYSERR on error
710 GNUNET_CONFIGURATION_get_value_choice (const struct
711 GNUNET_CONFIGURATION_Handle *cfg,
714 const char **choices,
717 struct ConfigEntry *e;
720 e = findEntry (cfg, section, option);
722 return GNUNET_SYSERR;
724 while (choices[i] != NULL)
726 if (0 == strcasecmp (choices[i], e->val))
730 if (choices[i] == NULL)
732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
733 _("Configuration value '%s' for '%s'"
734 " in section '%s' is not in set of legal choices\n"),
735 e->val, option, section);
736 return GNUNET_SYSERR;
744 * Test if we have a value for a particular option
745 * @param cfg configuration to inspect
746 * @param section section of interest
747 * @param option option of interest
748 * @return GNUNET_YES if so, GNUNET_NO if not.
751 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
752 *cfg, const char *section,
755 struct ConfigEntry *e;
756 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
763 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
764 * where either in the "PATHS" section or the environtment
765 * "FOO" is set to "DIRECTORY".
767 * @param cfg configuration to use for path expansion
768 * @param orig string to $-expand (will be freed!)
769 * @return $-expanded string
772 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
784 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
795 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
799 if (NULL == (env = getenv (&orig[1])))
801 orig[i] = DIR_SEPARATOR;
804 prefix = GNUNET_strdup (env);
806 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
807 strcpy (result, prefix);
808 if ((strlen (prefix) == 0) ||
809 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
810 strcat (result, DIR_SEPARATOR_STR);
811 strcat (result, post);
812 GNUNET_free (prefix);
819 * Get a configuration value that should be a string.
821 * @param cfg configuration to inspect
822 * @param section section of interest
823 * @param option option of interest
824 * @param value will be set to a freshly allocated configuration
825 * value, or NULL if option is not specified
826 * @return GNUNET_OK on success, GNUNET_SYSERR on error
829 GNUNET_CONFIGURATION_get_value_filename (const struct
830 GNUNET_CONFIGURATION_Handle *cfg,
832 const char *option, char **value)
837 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
840 return GNUNET_SYSERR;
842 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
843 *value = GNUNET_STRINGS_filename_expand (tmp);
846 return GNUNET_SYSERR;
852 * Get a configuration value that should be in a set of
853 * "GNUNET_YES" or "GNUNET_NO".
855 * @param cfg configuration to inspect
856 * @param section section of interest
857 * @param option option of interest
858 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
861 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
862 *cfg, const char *section,
865 static const char *yesno[] = { "YES", "NO", NULL };
869 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
870 section, option, yesno, &val);
871 if (ret == GNUNET_SYSERR)
880 * Iterate over the set of filenames stored in a configuration value.
882 * @param cfg configuration to inspect
883 * @param section section of interest
884 * @param option option of interest
885 * @param cb function to call on each filename
886 * @param cb_cls closure for cb
887 * @return number of filenames iterated over, -1 on error
890 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
891 GNUNET_CONFIGURATION_Handle
892 *cfg, const char *section,
894 GNUNET_FileNameCallback cb,
904 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
906 GNUNET_assert (list != NULL);
911 while (pos[0] == ' ')
913 if (strlen (pos) == 0)
916 while ((end[0] != ' ') && (end[0] != '\0'))
924 memmove (end, &end[1], strlen (&end[1]) + 1);
926 /* illegal, but just keep it */
929 /* illegal, but just ignore that there was a '/' */
937 if (strlen (pos) > 0)
940 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
962 escape_name (const char *value)
968 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
969 memset (escaped, 0, strlen (value) * 2 + 1);
972 while (rpos[0] != '\0')
995 * @param cls string we compare with (const char*)
996 * @param fn filename we are currently looking at
997 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
1000 test_match (void *cls, const char *fn)
1002 const char *of = cls;
1003 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1008 * Append a filename to a configuration value that
1009 * represents a list of filenames
1011 * @param cfg configuration to update
1012 * @param section section of interest
1013 * @param option option of interest
1014 * @param value filename to append
1015 * @return GNUNET_OK on success,
1016 * GNUNET_NO if the filename already in the list
1017 * GNUNET_SYSERR on error
1020 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1022 const char *section,
1031 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1036 return GNUNET_NO; /* already exists */
1038 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1039 old = GNUNET_strdup ("");
1040 escaped = escape_name (value);
1041 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1043 if (strlen (old) > 0)
1045 strcat (nw, escaped);
1046 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1049 GNUNET_free (escaped);
1055 * Remove a filename from a configuration value that
1056 * represents a list of filenames
1058 * @param cfg configuration to update
1059 * @param section section of interest
1060 * @param option option of interest
1061 * @param value filename to remove
1062 * @return GNUNET_OK on success,
1063 * GNUNET_NO if the filename is not in the list,
1064 * GNUNET_SYSERR on error
1067 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1069 const char *section,
1080 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1082 match = escape_name (value);
1086 while (pos[0] == ' ')
1088 if (strlen (pos) == 0)
1091 while ((end[0] != ' ') && (end[0] != '\0'))
1102 /* illegal, but just keep it */
1105 /* illegal, but just ignore that there was a '/' */
1113 if (0 == strcmp (pos, match))
1116 memmove (pos, &end[1], strlen (&end[1]) + 1);
1124 GNUNET_CONFIGURATION_set_value_string (cfg,
1125 section, option, list);
1127 GNUNET_free (match);
1136 GNUNET_free (match);
1142 * Load configuration (starts with defaults, then loads
1143 * system-specific configuration).
1145 * @param cfg configuration to update
1146 * @param filename name of the configuration file, NULL to load defaults
1147 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1150 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1151 const char *filename)
1156 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1158 return GNUNET_SYSERR;
1160 GNUNET_asprintf (&baseconfig,
1161 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1162 GNUNET_free (ipath);
1164 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1165 (!((filename == NULL) ||
1166 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1168 GNUNET_free (baseconfig);
1169 return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1171 GNUNET_free (baseconfig);
1172 if ( ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
1174 "DEFAULTCONFIG"))) &&
1175 (filename != NULL) )
1176 GNUNET_CONFIGURATION_set_value_string (cfg,
1180 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1183 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1186 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1192 /* end of configuration.c */