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 * Create a GNUNET_CONFIGURATION_Handle.
105 * @return fresh configuration object
107 struct GNUNET_CONFIGURATION_Handle *
108 GNUNET_CONFIGURATION_create ()
110 return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
115 * Destroy configuration object.
117 * @param cfg configuration to destroy
120 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
122 struct ConfigSection *sec;
123 struct ConfigEntry *ent;
125 while (NULL != (sec = cfg->sections))
127 cfg->sections = sec->next;
128 while (NULL != (ent = sec->entries))
130 sec->entries = ent->next;
131 GNUNET_free (ent->key);
132 GNUNET_free_non_null (ent->val);
135 GNUNET_free (sec->name);
143 * Parse a configuration file, add all of the options in the
144 * file to the configuration environment.
146 * @param cfg configuration to update
147 * @param filename name of the configuration file
148 * @return GNUNET_OK on success, GNUNET_SYSERR on error
151 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
152 const char *filename)
166 fn = GNUNET_STRINGS_filename_expand (filename);
167 dirty = cfg->dirty; /* back up value! */
168 if (NULL == (fp = FOPEN (fn, "r")))
170 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
172 return GNUNET_SYSERR;
176 section = GNUNET_strdup ("");
177 memset (line, 0, 256);
179 while (NULL != fgets (line, 255, fp))
182 for (i = 0; i < 255; i++)
185 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
189 for (i = 0; (i < 255 && line[i] != 0); i++)
190 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
194 /* remove tailing whitespace */
195 for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
197 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
200 if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
201 ret = GNUNET_SYSERR; /* failed to parse included config */
203 else if (1 == sscanf (line, "[%99[^]]]", value))
206 GNUNET_free (section);
207 section = GNUNET_strdup (value);
209 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
213 i = strlen (value) - 1;
214 while ((i >= 0) && (isspace (value[i])))
221 while ((value[i] != '\0') && (value[i] != '"'))
231 GNUNET_CONFIGURATION_set_value_string (cfg,
232 section, tag, &value[i]);
234 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
237 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
244 ("Syntax error in configuration file `%s' at line %u.\n"),
250 GNUNET_assert (0 == fclose (fp));
251 /* restore dirty flag - anything we set in the meantime
254 GNUNET_free (section);
260 * Test if there are configuration options that were
261 * changed since the last save.
263 * @param cfg configuration to inspect
264 * @return GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on error (i.e. last save failed)
267 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 * Write configuration file.
276 * @param cfg configuration to write
277 * @param filename where to write the configuration
278 * @return GNUNET_OK on success, GNUNET_SYSERR on error
281 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *data,
282 const char *filename)
284 struct ConfigSection *sec;
285 struct ConfigEntry *ent;
292 fn = GNUNET_STRINGS_filename_expand (filename);
293 GNUNET_DISK_directory_create_for_file (fn);
294 if (NULL == (fp = FOPEN (fn, "w")))
296 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
298 return GNUNET_SYSERR;
302 sec = data->sections;
305 if (0 > fprintf (fp, "[%s]\n", sec->name))
313 if (ent->val != NULL)
315 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
316 strcpy (val, ent->val);
317 while (NULL != (pos = strstr (val, "\n")))
319 memmove (&pos[2], &pos[1], strlen (&pos[1]));
323 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
335 if (0 > fprintf (fp, "\n"))
343 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
344 GNUNET_assert (0 == fclose (fp));
347 data->dirty = GNUNET_SYSERR; /* last write failed */
348 return GNUNET_SYSERR;
350 data->dirty = GNUNET_NO; /* last write succeeded */
356 * Iterate over all options in the configuration.
358 * @param cfg configuration to inspect
359 * @param iter function to call on each option
360 * @param iter_cls closure for iter
362 void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
363 GNUNET_CONFIGURATION_Iterator iter,
366 struct ConfigSection *spos;
367 struct ConfigEntry *epos;
369 spos = cfg->sections;
372 epos = spos->entries;
375 iter (iter_cls, spos->name, epos->key, epos->val);
386 * @param cls the destination configuration (struct GNUNET_CONFIGURATION_Handle*)
387 * @param section FIXME
388 * @param option FIXME
392 copy_entry (void *cls,
397 struct GNUNET_CONFIGURATION_Handle *dst = cls;
398 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
403 * Duplicate an existing configuration object.
405 * @param c configuration to duplicate
406 * @return duplicate configuration
408 struct GNUNET_CONFIGURATION_Handle *
409 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
411 struct GNUNET_CONFIGURATION_Handle *ret;
413 ret = GNUNET_CONFIGURATION_create ();
414 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
423 * @param section FIXME
424 * @return matching entry, NULL if not found
426 static struct ConfigSection *
427 findSection (const struct GNUNET_CONFIGURATION_Handle *data, const char *section)
429 struct ConfigSection *pos;
431 pos = data->sections;
432 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
442 * @param section FIXME
444 * @return matching entry, NULL if not found
446 static struct ConfigEntry *
447 findEntry (const struct GNUNET_CONFIGURATION_Handle *data,
448 const char *section, const char *key)
450 struct ConfigSection *sec;
451 struct ConfigEntry *pos;
453 sec = findSection (data, section);
457 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
464 * Set a configuration value that should be a string.
466 * @param cfg configuration to update
467 * @param section section of interest
468 * @param option option of interest
469 * @param value value to set
472 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
475 const char *option, const char *value)
477 struct ConfigSection *sec;
478 struct ConfigEntry *e;
480 e = findEntry (data, section, option);
483 GNUNET_free_non_null (e->val);
484 e->val = GNUNET_strdup (value);
487 sec = findSection (data, section);
490 sec = GNUNET_malloc (sizeof (struct ConfigSection));
491 sec->name = GNUNET_strdup (section);
492 sec->next = data->sections;
493 data->sections = sec;
495 e = GNUNET_malloc (sizeof (struct ConfigEntry));
496 e->key = GNUNET_strdup (option);
497 e->val = GNUNET_strdup (value);
498 e->next = sec->entries;
504 * Set a configuration value that should be a number.
506 * @param cfg configuration to update
507 * @param section section of interest
508 * @param option option of interest
509 * @param number value to set
512 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
513 *cfg, const char *section,
515 unsigned long long number)
518 GNUNET_snprintf (s, 64, "%llu", number);
519 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
524 * Get a configuration value that should be a number.
526 * @param cfg configuration to inspect
527 * @param section section of interest
528 * @param option option of interest
529 * @param number where to store the numeric value of the option
530 * @return GNUNET_OK on success, GNUNET_SYSERR on error
533 GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
534 *cfg, const char *section,
536 unsigned long long *number)
538 struct ConfigEntry *e;
540 e = findEntry (cfg, section, option);
542 return GNUNET_SYSERR;
543 if (1 != SSCANF (e->val, "%llu", number))
544 return GNUNET_SYSERR;
550 * Get a configuration value that should be a relative time.
552 * @param cfg configuration to inspect
553 * @param section section of interest
554 * @param option option of interest
555 * @param time set to the time value stored in the configuration
556 * @return GNUNET_OK on success, GNUNET_SYSERR on error
559 GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle
560 *cfg, const char *section,
562 struct GNUNET_TIME_Relative *time)
564 unsigned long long num;
567 ret = GNUNET_CONFIGURATION_get_value_number (cfg,
571 if (ret == GNUNET_OK)
572 time->value = (uint64_t) num;
578 * Get a configuration value that should be a string.
580 * @param cfg configuration to inspect
581 * @param section section of interest
582 * @param option option of interest
583 * @param value will be set to a freshly allocated configuration
584 * value, or NULL if option is not specified
585 * @return GNUNET_OK on success, GNUNET_SYSERR on error
588 GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle
589 *cfg, const char *section,
590 const char *option, char **value)
592 struct ConfigEntry *e;
594 e = findEntry (cfg, section, option);
595 if ((e == NULL) || (e->val == NULL))
598 return GNUNET_SYSERR;
600 *value = GNUNET_strdup (e->val);
606 * Get a configuration value that should be in a set of
609 * @param cfg configuration to inspect
610 * @param section section of interest
611 * @param option option of interest
612 * @param choices NULL-terminated list of legal values
613 * @param value will be set to an entry in the legal list,
614 * or NULL if option is not specified and no default given
615 * @return GNUNET_OK on success, GNUNET_SYSERR on error
618 GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle
619 *cfg, const char *section,
621 const char **choices,
624 struct ConfigEntry *e;
627 e = findEntry (cfg, section, option);
629 return GNUNET_SYSERR;
631 while (choices[i] != NULL)
633 if (0 == strcasecmp (choices[i], e->val))
637 if (choices[i] == NULL)
639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
640 _("Configuration value '%s' for '%s'"
641 " in section '%s' is not in set of legal choices\n"),
642 e->val, option, section);
643 return GNUNET_SYSERR;
651 * Test if we have a value for a particular option
652 * @param cfg configuration to inspect
653 * @param section section of interest
654 * @param option option of interest
655 * @return GNUNET_YES if so, GNUNET_NO if not.
658 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
659 const char *section, const char *option)
661 struct ConfigEntry *e;
662 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
669 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
670 * where either in the "PATHS" section or the environtment
671 * "FOO" is set to "DIRECTORY".
673 * @param cfg configuration to use for path expansion
674 * @param old string to $-expand (will be freed!)
675 * @return $-expanded string
678 GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
690 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
701 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
705 if (NULL == (env = getenv (&orig[1])))
707 orig[i] = DIR_SEPARATOR;
710 prefix = GNUNET_strdup (env);
712 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
713 strcpy (result, prefix);
714 if ((strlen (prefix) == 0) ||
715 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
716 strcat (result, DIR_SEPARATOR_STR);
717 strcat (result, post);
718 GNUNET_free (prefix);
725 * Get a configuration value that should be a string.
727 * @param cfg configuration to inspect
728 * @param section section of interest
729 * @param option option of interest
730 * @param value will be set to a freshly allocated configuration
731 * value, or NULL if option is not specified
732 * @return GNUNET_OK on success, GNUNET_SYSERR on error
735 GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle
736 *data, const char *section,
737 const char *option, char **value)
743 ret = GNUNET_CONFIGURATION_get_value_string (data, section, option, &tmp);
744 if (ret == GNUNET_SYSERR)
748 tmp = GNUNET_CONFIGURATION_expand_dollar (data, tmp);
749 *value = GNUNET_STRINGS_filename_expand (tmp);
763 * Get a configuration value that should be in a set of
764 * "GNUNET_YES" or "GNUNET_NO".
766 * @param cfg configuration to inspect
767 * @param section section of interest
768 * @param option option of interest
769 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
772 GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg,
773 const char *section, const char *option)
775 static const char *yesno[] = { "YES", "NO", NULL };
779 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
780 section, option, yesno, &val);
781 if (ret == GNUNET_SYSERR)
790 * Iterate over the set of filenames stored in a configuration value.
792 * @param cfg configuration to inspect
793 * @param section section of interest
794 * @param option option of interest
795 * @param cb function to call on each filename
796 * @param cb_cls closure for cb
797 * @return number of filenames iterated over, -1 on error
800 GNUNET_CONFIGURATION_iterate_value_filenames (const struct
801 GNUNET_CONFIGURATION_Handle
802 *cfg, const char *section,
804 GNUNET_FileNameCallback cb,
814 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
816 GNUNET_assert (list != NULL);
821 while (pos[0] == ' ')
823 if (strlen (pos) == 0)
826 while ((end[0] != ' ') && (end[0] != '\0'))
834 memmove (end, &end[1], strlen (&end[1]) + 1);
836 /* illegal, but just keep it */
839 /* illegal, but just ignore that there was a '/' */
847 if (strlen (pos) > 0)
850 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
872 escape_name (const char *value)
878 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
879 memset (escaped, 0, strlen (value) * 2 + 1);
882 while (rpos[0] != '\0')
905 * @param cls string we compare with (const char*)
906 * @param fn filename we are currently looking at
907 * @return GNUNET_OK if the names do not match, GNUNET_SYSERR if they do
910 test_match (void *cls, const char *fn)
912 const char *of = cls;
913 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
918 * Append a filename to a configuration value that
919 * represents a list of filenames
921 * @param cfg configuration to update
922 * @param section section of interest
923 * @param option option of interest
924 * @param value filename to append
925 * @return GNUNET_OK on success,
926 * GNUNET_NO if the filename already in the list
927 * GNUNET_SYSERR on error
930 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
941 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
946 return GNUNET_NO; /* already exists */
948 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
949 old = GNUNET_strdup ("");
950 escaped = escape_name (value);
951 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
954 strcat (nw, escaped);
955 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
958 GNUNET_free (escaped);
964 * Remove a filename from a configuration value that
965 * represents a list of filenames
967 * @param cfg configuration to update
968 * @param section section of interest
969 * @param option option of interest
970 * @param value filename to remove
971 * @return GNUNET_OK on success,
972 * GNUNET_NO if the filename is not in the list,
973 * GNUNET_SYSERR on error
976 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
989 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
991 match = escape_name (value);
995 while (pos[0] == ' ')
997 if (strlen (pos) == 0)
1000 while ((end[0] != ' ') && (end[0] != '\0'))
1011 /* illegal, but just keep it */
1014 /* illegal, but just ignore that there was a '/' */
1022 if (strlen (pos) > 0)
1024 if (0 == strcmp (pos, match))
1026 memmove (pos, &end[1], strlen (&end[1]) + 1);
1029 pos[-1] = ' '; /* previously changed to "\0" */
1030 GNUNET_CONFIGURATION_set_value_string (cfg,
1031 section, option, list);
1033 GNUNET_free (match);
1042 GNUNET_free (match);
1048 * Load configuration (starts with defaults, then loads
1049 * system-specific configuration).
1051 * @param cfg configuration to update
1052 * @param filename name of the configuration file
1053 * @return GNUNET_OK on success, GNUNET_SYSERR on error
1056 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
1062 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
1064 return GNUNET_SYSERR;
1066 GNUNET_asprintf (&baseconfig,
1067 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
1068 GNUNET_free (ipath);
1070 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
1071 (!((cfgfn == NULL) ||
1072 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, cfgfn)))))
1074 GNUNET_free (baseconfig);
1075 return GNUNET_SYSERR;
1077 GNUNET_free (baseconfig);
1078 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
1081 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
1084 GNUNET_CRYPTO_random_disable_entropy_gathering ();
1090 /* end of configuration.c */