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 const 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,
504 * Write only configuration entries that have been changed to configuration file
505 * @param cfgDefault default configuration
506 * @param cfgNew new configuration
507 * @param filename where to write the configuration diff between default and new
508 * @return GNUNET_OK on success, GNUNET_SYSERR on error
511 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
513 const struct GNUNET_CONFIGURATION_Handle *cfgNew,
514 const char *filename)
517 struct DiffHandle diffHandle;
519 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
520 diffHandle.cfgDefault = cfgDefault;
521 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
522 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
523 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
529 * Set a configuration value that should be a string.
531 * @param cfg configuration to update
532 * @param section section of interest
533 * @param option option of interest
534 * @param value value to set
537 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
540 const char *option, const char *value)
542 struct ConfigSection *sec;
543 struct ConfigEntry *e;
545 e = findEntry (cfg, section, option);
548 GNUNET_free_non_null (e->val);
549 e->val = GNUNET_strdup (value);
552 sec = findSection (cfg, section);
555 sec = GNUNET_malloc (sizeof (struct ConfigSection));
556 sec->name = GNUNET_strdup (section);
557 sec->next = cfg->sections;
560 e = GNUNET_malloc (sizeof (struct ConfigEntry));
561 e->key = GNUNET_strdup (option);
562 e->val = GNUNET_strdup (value);
563 e->next = sec->entries;
569 * Set a configuration value that should be a number.
571 * @param cfg configuration to update
572 * @param section section of interest
573 * @param option option of interest
574 * @param number value to set
577 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
578 *cfg, const char *section,
580 unsigned long long number)
583 GNUNET_snprintf (s, 64, "%llu", number);
584 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
589 * Get a configuration value that should be a number.
591 * @param cfg configuration to inspect
592 * @param section section of interest
593 * @param option option of interest
594 * @param number where to store the numeric value of the option
595 * @return GNUNET_OK on success, GNUNET_SYSERR on error
598 GNUNET_CONFIGURATION_get_value_number (const struct
599 GNUNET_CONFIGURATION_Handle *cfg,
602 unsigned long long *number)
604 struct ConfigEntry *e;
606 e = findEntry (cfg, section, option);
608 return GNUNET_SYSERR;
609 if (1 != SSCANF (e->val, "%llu", number))
610 return GNUNET_SYSERR;
616 * Get a configuration value that should be a relative time.
618 * @param cfg configuration to inspect
619 * @param section section of interest
620 * @param option option of interest
621 * @param time set to the time value stored in the configuration
622 * @return GNUNET_OK on success, GNUNET_SYSERR on error
625 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
626 *cfg, const char *section,
628 struct GNUNET_TIME_Relative *time)
630 unsigned long long num;
633 ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
634 if (ret == GNUNET_OK)
635 time->value = (uint64_t) num;
641 * Get a configuration value that should be a string.
643 * @param cfg configuration to inspect
644 * @param section section of interest
645 * @param option option of interest
646 * @param value will be set to a freshly allocated configuration
647 * value, or NULL if option is not specified
648 * @return GNUNET_OK on success, GNUNET_SYSERR on error
651 GNUNET_CONFIGURATION_get_value_string (const struct
652 GNUNET_CONFIGURATION_Handle *cfg,
654 const char *option, char **value)
656 struct ConfigEntry *e;
658 e = findEntry (cfg, section, option);
659 if ((e == NULL) || (e->val == NULL))
662 return GNUNET_SYSERR;
664 *value = GNUNET_strdup (e->val);
670 * Get a configuration value that should be in a set of
673 * @param cfg configuration to inspect
674 * @param section section of interest
675 * @param option option of interest
676 * @param choices NULL-terminated list of legal values
677 * @param value will be set to an entry in the legal list,
678 * or NULL if option is not specified and no default given
679 * @return GNUNET_OK on success, GNUNET_SYSERR on error
682 GNUNET_CONFIGURATION_get_value_choice (const struct
683 GNUNET_CONFIGURATION_Handle *cfg,
686 const char **choices,
689 struct ConfigEntry *e;
692 e = findEntry (cfg, section, option);
694 return GNUNET_SYSERR;
696 while (choices[i] != NULL)
698 if (0 == strcasecmp (choices[i], e->val))
702 if (choices[i] == NULL)
704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
705 _("Configuration value '%s' for '%s'"
706 " in section '%s' is not in set of legal choices\n"),
707 e->val, option, section);
708 return GNUNET_SYSERR;
716 * Test if we have a value for a particular option
717 * @param cfg configuration to inspect
718 * @param section section of interest
719 * @param option option of interest
720 * @return GNUNET_YES if so, GNUNET_NO if not.
723 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
724 *cfg, const char *section,
727 struct ConfigEntry *e;
728 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
735 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
736 * where either in the "PATHS" section or the environtment
737 * "FOO" is set to "DIRECTORY".
739 * @param cfg configuration to use for path expansion
740 * @param orig string to $-expand (will be freed!)
741 * @return $-expanded string
744 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
756 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
767 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
771 if (NULL == (env = getenv (&orig[1])))
773 orig[i] = DIR_SEPARATOR;
776 prefix = GNUNET_strdup (env);
778 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
779 strcpy (result, prefix);
780 if ((strlen (prefix) == 0) ||
781 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
782 strcat (result, DIR_SEPARATOR_STR);
783 strcat (result, post);
784 GNUNET_free (prefix);
791 * Get a configuration value that should be a string.
793 * @param cfg configuration to inspect
794 * @param section section of interest
795 * @param option option of interest
796 * @param value will be set to a freshly allocated configuration
797 * value, or NULL if option is not specified
798 * @return GNUNET_OK on success, GNUNET_SYSERR on error
801 GNUNET_CONFIGURATION_get_value_filename (const struct
802 GNUNET_CONFIGURATION_Handle *cfg,
804 const char *option, char **value)
810 ret = GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp);
811 if (ret == GNUNET_SYSERR)
815 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
816 *value = GNUNET_STRINGS_filename_expand (tmp);
830 * Get a configuration value that should be in a set of
831 * "GNUNET_YES" or "GNUNET_NO".
833 * @param cfg configuration to inspect
834 * @param section section of interest
835 * @param option option of interest
836 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
839 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
840 *cfg, const char *section,
843 static const char *yesno[] = { "YES", "NO", NULL };
847 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
848 section, option, yesno, &val);
849 if (ret == GNUNET_SYSERR)
858 * Iterate over the set of filenames stored in a configuration value.
860 * @param cfg configuration to inspect
861 * @param section section of interest
862 * @param option option of interest
863 * @param cb function to call on each filename
864 * @param cb_cls closure for cb
865 * @return number of filenames iterated over, -1 on error
868 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
869 GNUNET_CONFIGURATION_Handle
870 *cfg, const char *section,
872 GNUNET_FileNameCallback cb,
882 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
884 GNUNET_assert (list != NULL);
889 while (pos[0] == ' ')
891 if (strlen (pos) == 0)
894 while ((end[0] != ' ') && (end[0] != '\0'))
902 memmove (end, &end[1], strlen (&end[1]) + 1);
904 /* illegal, but just keep it */
907 /* illegal, but just ignore that there was a '/' */
915 if (strlen (pos) > 0)
918 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
940 escape_name (const char *value)
946 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
947 memset (escaped, 0, strlen (value) * 2 + 1);
950 while (rpos[0] != '\0')
973 * @param cls string we compare with (const char*)
974 * @param fn filename we are currently looking at
975 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
978 test_match (void *cls, const char *fn)
980 const char *of = cls;
981 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
986 * Append a filename to a configuration value that
987 * represents a list of filenames
989 * @param cfg configuration to update
990 * @param section section of interest
991 * @param option option of interest
992 * @param value filename to append
993 * @return GNUNET_OK on success,
994 * GNUNET_NO if the filename already in the list
995 * GNUNET_SYSERR on error
998 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1000 const char *section,
1009 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1014 return GNUNET_NO; /* already exists */
1016 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1017 old = GNUNET_strdup ("");
1018 escaped = escape_name (value);
1019 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1022 strcat (nw, escaped);
1023 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1026 GNUNET_free (escaped);
1032 * Remove a filename from a configuration value that
1033 * represents a list of filenames
1035 * @param cfg configuration to update
1036 * @param section section of interest
1037 * @param option option of interest
1038 * @param value filename to remove
1039 * @return GNUNET_OK on success,
1040 * GNUNET_NO if the filename is not in the list,
1041 * GNUNET_SYSERR on error
1044 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1046 const char *section,
1057 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1059 match = escape_name (value);
1063 while (pos[0] == ' ')
1065 if (strlen (pos) == 0)
1068 while ((end[0] != ' ') && (end[0] != '\0'))
1079 /* illegal, but just keep it */
1082 /* illegal, but just ignore that there was a '/' */
1090 if (strlen (pos) > 0)
1092 if (0 == strcmp (pos, match))
1094 memmove (pos, &end[1], strlen (&end[1]) + 1);
1097 pos[-1] = ' '; /* previously changed to "\0" */
1098 GNUNET_CONFIGURATION_set_value_string (cfg,
1099 section, option, list);
1101 GNUNET_free (match);
1110 GNUNET_free (match);
1116 * Load configuration (starts with defaults, then loads
1117 * system-specific configuration).
1119 * @param cfg configuration to update
1120 * @param filename name of the configuration file
1121 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1124 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1125 const char *filename)
1130 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1132 return GNUNET_SYSERR;
1134 GNUNET_asprintf (&baseconfig,
1135 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1136 GNUNET_free (ipath);
1138 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1139 (!((filename == NULL) ||
1140 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1142 GNUNET_free (baseconfig);
1143 return GNUNET_SYSERR;
1145 GNUNET_free (baseconfig);
1146 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1149 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1152 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1158 /* end of configuration.c */