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);
179 dirty = cfg->dirty; /* back up value! */
180 if (NULL == (fp = FOPEN (fn, "r")))
182 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
184 return GNUNET_SYSERR;
188 section = GNUNET_strdup ("");
189 memset (line, 0, 256);
191 while (NULL != fgets (line, 255, fp))
194 for (i = 0; i < 255; i++)
197 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
201 for (i = 0; (i < 255 && line[i] != 0); i++)
202 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
206 /* remove tailing whitespace */
207 for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
209 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
212 if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
213 ret = GNUNET_SYSERR; /* failed to parse included config */
215 else if (1 == sscanf (line, "[%99[^]]]", value))
218 GNUNET_free (section);
219 section = GNUNET_strdup (value);
221 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
225 i = strlen (value) - 1;
226 while ((i >= 0) && (isspace (value[i])))
233 while ((value[i] != '\0') && (value[i] != '"'))
243 GNUNET_CONFIGURATION_set_value_string (cfg,
244 section, tag, &value[i]);
246 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
249 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
256 ("Syntax error in configuration file `%s' at line %u.\n"),
262 GNUNET_assert (0 == fclose (fp));
263 /* restore dirty flag - anything we set in the meantime
266 GNUNET_free (section);
272 * Test if there are configuration options that were
273 * changed since the last save.
275 * @param cfg configuration to inspect
276 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
279 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
286 * Write configuration file.
288 * @param cfg configuration to write
289 * @param filename where to write the configuration
290 * @return GNUNET_OK on success, GNUNET_SYSERR on error
293 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
294 const char *filename)
296 struct ConfigSection *sec;
297 struct ConfigEntry *ent;
304 fn = GNUNET_STRINGS_filename_expand (filename);
305 GNUNET_DISK_directory_create_for_file (fn);
306 if (NULL == (fp = FOPEN (fn, "w")))
308 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
310 return GNUNET_SYSERR;
317 if (0 > fprintf (fp, "[%s]\n", sec->name))
325 if (ent->val != NULL)
327 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
328 strcpy (val, ent->val);
329 while (NULL != (pos = strstr (val, "\n")))
331 memmove (&pos[2], &pos[1], strlen (&pos[1]));
335 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
347 if (0 > fprintf (fp, "\n"))
355 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
356 GNUNET_assert (0 == fclose (fp));
359 cfg->dirty = GNUNET_SYSERR; /* last write failed */
360 return GNUNET_SYSERR;
362 cfg->dirty = GNUNET_NO; /* last write succeeded */
368 * Iterate over all options in the configuration.
370 * @param cfg configuration to inspect
371 * @param iter function to call on each option
372 * @param iter_cls closure for iter
375 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
376 GNUNET_CONFIGURATION_Iterator iter,
379 struct ConfigSection *spos;
380 struct ConfigEntry *epos;
382 spos = cfg->sections;
385 epos = spos->entries;
388 iter (iter_cls, spos->name, epos->key, epos->val);
397 * Copy a configuration value to the given target configuration.
398 * Overwrites existing entries.
400 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
401 * @param section section for the value
402 * @param option option name of the value
403 * @param value value to copy
406 copy_entry (void *cls,
407 const char *section, const char *option, const char *value)
409 struct GNUNET_CONFIGURATION_Handle *dst = cls;
410 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
415 * Duplicate an existing configuration object.
417 * @param cfg configuration to duplicate
418 * @return duplicate configuration
420 struct GNUNET_CONFIGURATION_Handle *
421 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
423 struct GNUNET_CONFIGURATION_Handle *ret;
425 ret = GNUNET_CONFIGURATION_create ();
426 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
435 * @param section FIXME
436 * @return matching entry, NULL if not found
438 static struct ConfigSection *
439 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
442 struct ConfigSection *pos;
445 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
455 * @param section FIXME
457 * @return matching entry, NULL if not found
459 static struct ConfigEntry *
460 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
461 const char *section, const char *key)
463 struct ConfigSection *sec;
464 struct ConfigEntry *pos;
466 sec = findSection (cfg, section);
470 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
477 * A callback function, compares entries from two configurations
478 * (default against a new configuration) and write the diffs in a
479 * diff-configuration object (the callback object).
481 * @param cls the diff configuration (struct DiffHandle*)
482 * @param section section for the value (of the default conf.)
483 * @param option option name of the value (of the default conf.)
484 * @param value value to copy (of the default conf.)
487 compareEntries (void *cls,
488 const char *section, const char *option, const char *value)
490 struct DiffHandle *dh = cls;
491 struct ConfigEntry *entNew;
493 entNew = findEntry (dh->cfgDefault, section, option);
494 if ( (entNew != NULL) &&
495 (strcmp (entNew->val, value) == 0) )
497 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
505 * Write only configuration entries that have been changed to configuration file
506 * @param cfgDefault default configuration
507 * @param cfgNew new configuration
508 * @param filename where to write the configuration diff between default and new
509 * @return GNUNET_OK on success, GNUNET_SYSERR on error
512 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
514 const struct GNUNET_CONFIGURATION_Handle *cfgNew,
515 const char *filename)
518 struct DiffHandle diffHandle;
520 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
521 diffHandle.cfgDefault = cfgDefault;
522 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
523 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
524 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
530 * Set a configuration value that should be a string.
532 * @param cfg configuration to update
533 * @param section section of interest
534 * @param option option of interest
535 * @param value value to set
538 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
541 const char *option, const char *value)
543 struct ConfigSection *sec;
544 struct ConfigEntry *e;
546 e = findEntry (cfg, section, option);
549 GNUNET_free_non_null (e->val);
550 e->val = GNUNET_strdup (value);
553 sec = findSection (cfg, section);
556 sec = GNUNET_malloc (sizeof (struct ConfigSection));
557 sec->name = GNUNET_strdup (section);
558 sec->next = cfg->sections;
561 e = GNUNET_malloc (sizeof (struct ConfigEntry));
562 e->key = GNUNET_strdup (option);
563 e->val = GNUNET_strdup (value);
564 e->next = sec->entries;
570 * Set a configuration value that should be a number.
572 * @param cfg configuration to update
573 * @param section section of interest
574 * @param option option of interest
575 * @param number value to set
578 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
579 *cfg, const char *section,
581 unsigned long long number)
584 GNUNET_snprintf (s, 64, "%llu", number);
585 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
590 * Get a configuration value that should be a number.
592 * @param cfg configuration to inspect
593 * @param section section of interest
594 * @param option option of interest
595 * @param number where to store the numeric value of the option
596 * @return GNUNET_OK on success, GNUNET_SYSERR on error
599 GNUNET_CONFIGURATION_get_value_number (const struct
600 GNUNET_CONFIGURATION_Handle *cfg,
603 unsigned long long *number)
605 struct ConfigEntry *e;
607 e = findEntry (cfg, section, option);
609 return GNUNET_SYSERR;
610 if (1 != SSCANF (e->val, "%llu", number))
611 return GNUNET_SYSERR;
617 * Get a configuration value that should be a relative time.
619 * @param cfg configuration to inspect
620 * @param section section of interest
621 * @param option option of interest
622 * @param time set to the time value stored in the configuration
623 * @return GNUNET_OK on success, GNUNET_SYSERR on error
626 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
627 *cfg, const char *section,
629 struct GNUNET_TIME_Relative *time)
631 unsigned long long num;
634 ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
635 if (ret == GNUNET_OK)
636 time->value = (uint64_t) num;
642 * Get a configuration value that should be a string.
644 * @param cfg configuration to inspect
645 * @param section section of interest
646 * @param option option of interest
647 * @param value will be set to a freshly allocated configuration
648 * value, or NULL if option is not specified
649 * @return GNUNET_OK on success, GNUNET_SYSERR on error
652 GNUNET_CONFIGURATION_get_value_string (const struct
653 GNUNET_CONFIGURATION_Handle *cfg,
655 const char *option, char **value)
657 struct ConfigEntry *e;
659 e = findEntry (cfg, section, option);
660 if ((e == NULL) || (e->val == NULL))
663 return GNUNET_SYSERR;
665 *value = GNUNET_strdup (e->val);
671 * Get a configuration value that should be in a set of
674 * @param cfg configuration to inspect
675 * @param section section of interest
676 * @param option option of interest
677 * @param choices NULL-terminated list of legal values
678 * @param value will be set to an entry in the legal list,
679 * or NULL if option is not specified and no default given
680 * @return GNUNET_OK on success, GNUNET_SYSERR on error
683 GNUNET_CONFIGURATION_get_value_choice (const struct
684 GNUNET_CONFIGURATION_Handle *cfg,
687 const char **choices,
690 struct ConfigEntry *e;
693 e = findEntry (cfg, section, option);
695 return GNUNET_SYSERR;
697 while (choices[i] != NULL)
699 if (0 == strcasecmp (choices[i], e->val))
703 if (choices[i] == NULL)
705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
706 _("Configuration value '%s' for '%s'"
707 " in section '%s' is not in set of legal choices\n"),
708 e->val, option, section);
709 return GNUNET_SYSERR;
717 * Test if we have a value for a particular option
718 * @param cfg configuration to inspect
719 * @param section section of interest
720 * @param option option of interest
721 * @return GNUNET_YES if so, GNUNET_NO if not.
724 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
725 *cfg, const char *section,
728 struct ConfigEntry *e;
729 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
736 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
737 * where either in the "PATHS" section or the environtment
738 * "FOO" is set to "DIRECTORY".
740 * @param cfg configuration to use for path expansion
741 * @param orig string to $-expand (will be freed!)
742 * @return $-expanded string
745 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
757 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
768 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
772 if (NULL == (env = getenv (&orig[1])))
774 orig[i] = DIR_SEPARATOR;
777 prefix = GNUNET_strdup (env);
779 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
780 strcpy (result, prefix);
781 if ((strlen (prefix) == 0) ||
782 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
783 strcat (result, DIR_SEPARATOR_STR);
784 strcat (result, post);
785 GNUNET_free (prefix);
792 * Get a configuration value that should be a string.
794 * @param cfg configuration to inspect
795 * @param section section of interest
796 * @param option option of interest
797 * @param value will be set to a freshly allocated configuration
798 * value, or NULL if option is not specified
799 * @return GNUNET_OK on success, GNUNET_SYSERR on error
802 GNUNET_CONFIGURATION_get_value_filename (const struct
803 GNUNET_CONFIGURATION_Handle *cfg,
805 const char *option, char **value)
811 ret = GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp);
812 if (ret == GNUNET_SYSERR)
816 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
817 *value = GNUNET_STRINGS_filename_expand (tmp);
831 * Get a configuration value that should be in a set of
832 * "GNUNET_YES" or "GNUNET_NO".
834 * @param cfg configuration to inspect
835 * @param section section of interest
836 * @param option option of interest
837 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
840 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
841 *cfg, const char *section,
844 static const char *yesno[] = { "YES", "NO", NULL };
848 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
849 section, option, yesno, &val);
850 if (ret == GNUNET_SYSERR)
859 * Iterate over the set of filenames stored in a configuration value.
861 * @param cfg configuration to inspect
862 * @param section section of interest
863 * @param option option of interest
864 * @param cb function to call on each filename
865 * @param cb_cls closure for cb
866 * @return number of filenames iterated over, -1 on error
869 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
870 GNUNET_CONFIGURATION_Handle
871 *cfg, const char *section,
873 GNUNET_FileNameCallback cb,
883 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
885 GNUNET_assert (list != NULL);
890 while (pos[0] == ' ')
892 if (strlen (pos) == 0)
895 while ((end[0] != ' ') && (end[0] != '\0'))
903 memmove (end, &end[1], strlen (&end[1]) + 1);
905 /* illegal, but just keep it */
908 /* illegal, but just ignore that there was a '/' */
916 if (strlen (pos) > 0)
919 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
941 escape_name (const char *value)
947 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
948 memset (escaped, 0, strlen (value) * 2 + 1);
951 while (rpos[0] != '\0')
974 * @param cls string we compare with (const char*)
975 * @param fn filename we are currently looking at
976 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
979 test_match (void *cls, const char *fn)
981 const char *of = cls;
982 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
987 * Append a filename to a configuration value that
988 * represents a list of filenames
990 * @param cfg configuration to update
991 * @param section section of interest
992 * @param option option of interest
993 * @param value filename to append
994 * @return GNUNET_OK on success,
995 * GNUNET_NO if the filename already in the list
996 * GNUNET_SYSERR on error
999 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1001 const char *section,
1010 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1015 return GNUNET_NO; /* already exists */
1017 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1018 old = GNUNET_strdup ("");
1019 escaped = escape_name (value);
1020 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1023 strcat (nw, escaped);
1024 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1027 GNUNET_free (escaped);
1033 * Remove a filename from a configuration value that
1034 * represents a list of filenames
1036 * @param cfg configuration to update
1037 * @param section section of interest
1038 * @param option option of interest
1039 * @param value filename to remove
1040 * @return GNUNET_OK on success,
1041 * GNUNET_NO if the filename is not in the list,
1042 * GNUNET_SYSERR on error
1045 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1047 const char *section,
1058 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1060 match = escape_name (value);
1064 while (pos[0] == ' ')
1066 if (strlen (pos) == 0)
1069 while ((end[0] != ' ') && (end[0] != '\0'))
1080 /* illegal, but just keep it */
1083 /* illegal, but just ignore that there was a '/' */
1091 if (strlen (pos) > 0)
1093 if (0 == strcmp (pos, match))
1095 memmove (pos, &end[1], strlen (&end[1]) + 1);
1098 pos[-1] = ' '; /* previously changed to "\0" */
1099 GNUNET_CONFIGURATION_set_value_string (cfg,
1100 section, option, list);
1102 GNUNET_free (match);
1111 GNUNET_free (match);
1117 * Load configuration (starts with defaults, then loads
1118 * system-specific configuration).
1120 * @param cfg configuration to update
1121 * @param filename name of the configuration file
1122 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1125 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1126 const char *filename)
1131 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1133 return GNUNET_SYSERR;
1135 GNUNET_asprintf (&baseconfig,
1136 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1137 GNUNET_free (ipath);
1139 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1140 (!((filename == NULL) ||
1141 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1143 GNUNET_free (baseconfig);
1144 return GNUNET_SYSERR;
1146 GNUNET_free (baseconfig);
1147 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1150 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1153 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1159 /* end of configuration.c */