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 * Copy a configuration value to the given target configuration.
406 * Overwrites existing entries.
408 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
409 * @param section section for the value
410 * @param option option name of the value
411 * @param value value to copy
414 copy_entry (void *cls,
415 const char *section, const char *option, const char *value)
417 struct GNUNET_CONFIGURATION_Handle *dst = cls;
418 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
423 * Duplicate an existing configuration object.
425 * @param cfg configuration to duplicate
426 * @return duplicate configuration
428 struct GNUNET_CONFIGURATION_Handle *
429 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
431 struct GNUNET_CONFIGURATION_Handle *ret;
433 ret = GNUNET_CONFIGURATION_create ();
434 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
443 * @param section FIXME
444 * @return matching entry, NULL if not found
446 static struct ConfigSection *
447 findSection (const struct GNUNET_CONFIGURATION_Handle *cfg,
450 struct ConfigSection *pos;
453 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
460 * Find an entry from a configuration.
462 * @param cfg handle to the configuration
463 * @param section section the option is in
464 * @param key the option
465 * @return matching entry, NULL if not found
467 static struct ConfigEntry *
468 findEntry (const struct GNUNET_CONFIGURATION_Handle *cfg,
469 const char *section, const char *key)
471 struct ConfigSection *sec;
472 struct ConfigEntry *pos;
474 sec = findSection (cfg, section);
478 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
485 * A callback function, compares entries from two configurations
486 * (default against a new configuration) and write the diffs in a
487 * diff-configuration object (the callback object).
489 * @param cls the diff configuration (struct DiffHandle*)
490 * @param section section for the value (of the default conf.)
491 * @param option option name of the value (of the default conf.)
492 * @param value value to copy (of the default conf.)
495 compareEntries (void *cls,
496 const char *section, const char *option, const char *value)
498 struct DiffHandle *dh = cls;
499 struct ConfigEntry *entNew;
501 entNew = findEntry (dh->cfgDefault, section, option);
502 if ( (entNew != NULL) &&
503 (strcmp (entNew->val, value) == 0) )
505 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff,
513 * Write only configuration entries that have been changed to configuration file
514 * @param cfgDefault default configuration
515 * @param cfgNew new configuration
516 * @param filename where to write the configuration diff between default and new
517 * @return GNUNET_OK on success, GNUNET_SYSERR on error
520 GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle
522 const struct GNUNET_CONFIGURATION_Handle *cfgNew,
523 const char *filename)
526 struct DiffHandle diffHandle;
528 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
529 diffHandle.cfgDefault = cfgDefault;
530 GNUNET_CONFIGURATION_iterate (cfgNew, compareEntries, &diffHandle);
531 ret = GNUNET_CONFIGURATION_write (diffHandle.cfgDiff, filename);
532 GNUNET_CONFIGURATION_destroy (diffHandle.cfgDiff);
538 * Set a configuration value that should be a string.
540 * @param cfg configuration to update
541 * @param section section of interest
542 * @param option option of interest
543 * @param value value to set
546 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
549 const char *option, const char *value)
551 struct ConfigSection *sec;
552 struct ConfigEntry *e;
554 e = findEntry (cfg, section, option);
557 GNUNET_free_non_null (e->val);
558 e->val = GNUNET_strdup (value);
561 sec = findSection (cfg, section);
564 sec = GNUNET_malloc (sizeof (struct ConfigSection));
565 sec->name = GNUNET_strdup (section);
566 sec->next = cfg->sections;
569 e = GNUNET_malloc (sizeof (struct ConfigEntry));
570 e->key = GNUNET_strdup (option);
571 e->val = GNUNET_strdup (value);
572 e->next = sec->entries;
578 * Set a configuration value that should be a number.
580 * @param cfg configuration to update
581 * @param section section of interest
582 * @param option option of interest
583 * @param number value to set
586 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
587 *cfg, const char *section,
589 unsigned long long number)
592 GNUNET_snprintf (s, 64, "%llu", number);
593 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
598 * Get a configuration value that should be a number.
600 * @param cfg configuration to inspect
601 * @param section section of interest
602 * @param option option of interest
603 * @param number where to store the numeric value of the option
604 * @return GNUNET_OK on success, GNUNET_SYSERR on error
607 GNUNET_CONFIGURATION_get_value_number (const struct
608 GNUNET_CONFIGURATION_Handle *cfg,
611 unsigned long long *number)
613 struct ConfigEntry *e;
615 e = findEntry (cfg, section, option);
617 return GNUNET_SYSERR;
618 if (1 != SSCANF (e->val, "%llu", number))
619 return GNUNET_SYSERR;
625 * Get a configuration value that should be a relative time.
627 * @param cfg configuration to inspect
628 * @param section section of interest
629 * @param option option of interest
630 * @param time set to the time value stored in the configuration
631 * @return GNUNET_OK on success, GNUNET_SYSERR on error
634 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
635 *cfg, const char *section,
637 struct GNUNET_TIME_Relative *time)
639 unsigned long long num;
642 ret = GNUNET_CONFIGURATION_get_value_number (cfg, section, option, &num);
643 if (ret == GNUNET_OK)
644 time->rel_value = (uint64_t) num;
650 * Get a configuration value that should be a string.
652 * @param cfg configuration to inspect
653 * @param section section of interest
654 * @param option option of interest
655 * @param value will be set to a freshly allocated configuration
656 * value, or NULL if option is not specified
657 * @return GNUNET_OK on success, GNUNET_SYSERR on error
660 GNUNET_CONFIGURATION_get_value_string (const struct
661 GNUNET_CONFIGURATION_Handle *cfg,
663 const char *option, char **value)
665 struct ConfigEntry *e;
667 e = findEntry (cfg, section, option);
668 if ((e == NULL) || (e->val == NULL))
671 return GNUNET_SYSERR;
673 *value = GNUNET_strdup (e->val);
679 * Get a configuration value that should be in a set of
682 * @param cfg configuration to inspect
683 * @param section section of interest
684 * @param option option of interest
685 * @param choices NULL-terminated list of legal values
686 * @param value will be set to an entry in the legal list,
687 * or NULL if option is not specified and no default given
688 * @return GNUNET_OK on success, GNUNET_SYSERR on error
691 GNUNET_CONFIGURATION_get_value_choice (const struct
692 GNUNET_CONFIGURATION_Handle *cfg,
695 const char **choices,
698 struct ConfigEntry *e;
701 e = findEntry (cfg, section, option);
703 return GNUNET_SYSERR;
705 while (choices[i] != NULL)
707 if (0 == strcasecmp (choices[i], e->val))
711 if (choices[i] == NULL)
713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
714 _("Configuration value '%s' for '%s'"
715 " in section '%s' is not in set of legal choices\n"),
716 e->val, option, section);
717 return GNUNET_SYSERR;
725 * Test if we have a value for a particular option
726 * @param cfg configuration to inspect
727 * @param section section of interest
728 * @param option option of interest
729 * @return GNUNET_YES if so, GNUNET_NO if not.
732 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle
733 *cfg, const char *section,
736 struct ConfigEntry *e;
737 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
744 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
745 * where either in the "PATHS" section or the environtment
746 * "FOO" is set to "DIRECTORY".
748 * @param cfg configuration to use for path expansion
749 * @param orig string to $-expand (will be freed!)
750 * @return $-expanded string
753 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle
765 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
776 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg,
780 if (NULL == (env = getenv (&orig[1])))
782 orig[i] = DIR_SEPARATOR;
785 prefix = GNUNET_strdup (env);
787 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
788 strcpy (result, prefix);
789 if ((strlen (prefix) == 0) ||
790 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
791 strcat (result, DIR_SEPARATOR_STR);
792 strcat (result, post);
793 GNUNET_free (prefix);
800 * Get a configuration value that should be a string.
802 * @param cfg configuration to inspect
803 * @param section section of interest
804 * @param option option of interest
805 * @param value will be set to a freshly allocated configuration
806 * value, or NULL if option is not specified
807 * @return GNUNET_OK on success, GNUNET_SYSERR on error
810 GNUNET_CONFIGURATION_get_value_filename (const struct
811 GNUNET_CONFIGURATION_Handle *cfg,
813 const char *option, char **value)
818 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
821 return GNUNET_SYSERR;
823 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
824 *value = GNUNET_STRINGS_filename_expand (tmp);
827 return GNUNET_SYSERR;
833 * Get a configuration value that should be in a set of
834 * "GNUNET_YES" or "GNUNET_NO".
836 * @param cfg configuration to inspect
837 * @param section section of interest
838 * @param option option of interest
839 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
842 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle
843 *cfg, const char *section,
846 static const char *yesno[] = { "YES", "NO", NULL };
850 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
851 section, option, yesno, &val);
852 if (ret == GNUNET_SYSERR)
861 * Iterate over the set of filenames stored in a configuration value.
863 * @param cfg configuration to inspect
864 * @param section section of interest
865 * @param option option of interest
866 * @param cb function to call on each filename
867 * @param cb_cls closure for cb
868 * @return number of filenames iterated over, -1 on error
871 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
872 GNUNET_CONFIGURATION_Handle
873 *cfg, const char *section,
875 GNUNET_FileNameCallback cb,
885 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
887 GNUNET_assert (list != NULL);
892 while (pos[0] == ' ')
894 if (strlen (pos) == 0)
897 while ((end[0] != ' ') && (end[0] != '\0'))
905 memmove (end, &end[1], strlen (&end[1]) + 1);
907 /* illegal, but just keep it */
910 /* illegal, but just ignore that there was a '/' */
918 if (strlen (pos) > 0)
921 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
943 escape_name (const char *value)
949 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
950 memset (escaped, 0, strlen (value) * 2 + 1);
953 while (rpos[0] != '\0')
976 * @param cls string we compare with (const char*)
977 * @param fn filename we are currently looking at
978 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
981 test_match (void *cls, const char *fn)
983 const char *of = cls;
984 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
989 * Append a filename to a configuration value that
990 * represents a list of filenames
992 * @param cfg configuration to update
993 * @param section section of interest
994 * @param option option of interest
995 * @param value filename to append
996 * @return GNUNET_OK on success,
997 * GNUNET_NO if the filename already in the list
998 * GNUNET_SYSERR on error
1001 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
1003 const char *section,
1012 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1017 return GNUNET_NO; /* already exists */
1019 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1020 old = GNUNET_strdup ("");
1021 escaped = escape_name (value);
1022 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1024 if (strlen (old) > 0)
1026 strcat (nw, escaped);
1027 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1030 GNUNET_free (escaped);
1036 * Remove a filename from a configuration value that
1037 * represents a list of filenames
1039 * @param cfg configuration to update
1040 * @param section section of interest
1041 * @param option option of interest
1042 * @param value filename to remove
1043 * @return GNUNET_OK on success,
1044 * GNUNET_NO if the filename is not in the list,
1045 * GNUNET_SYSERR on error
1048 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
1050 const char *section,
1061 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1063 match = escape_name (value);
1067 while (pos[0] == ' ')
1069 if (strlen (pos) == 0)
1072 while ((end[0] != ' ') && (end[0] != '\0'))
1083 /* illegal, but just keep it */
1086 /* illegal, but just ignore that there was a '/' */
1094 if (0 == strcmp (pos, match))
1097 memmove (pos, &end[1], strlen (&end[1]) + 1);
1105 GNUNET_CONFIGURATION_set_value_string (cfg,
1106 section, option, list);
1108 GNUNET_free (match);
1117 GNUNET_free (match);
1123 * Load configuration (starts with defaults, then loads
1124 * system-specific configuration).
1126 * @param cfg configuration to update
1127 * @param filename name of the configuration file, NULL to load defaults
1128 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1131 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1132 const char *filename)
1137 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1139 return GNUNET_SYSERR;
1141 GNUNET_asprintf (&baseconfig,
1142 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1143 GNUNET_free (ipath);
1145 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1146 (!((filename == NULL) ||
1147 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, filename)))))
1149 GNUNET_free (baseconfig);
1150 return (filename == NULL) ? GNUNET_OK : GNUNET_SYSERR;
1152 GNUNET_free (baseconfig);
1153 if ( ((GNUNET_YES != GNUNET_CONFIGURATION_have_value (cfg,
1155 "DEFAULTCONFIG"))) &&
1156 (filename != NULL) )
1157 GNUNET_CONFIGURATION_set_value_string (cfg,
1161 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1164 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1167 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1173 /* end of configuration.c */