2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013, 2020 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file src/util/configuration.c
22 * @brief configuration management
23 * @author Christian Grothoff
26 #include "gnunet_crypto_lib.h"
27 #include "gnunet_strings_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_configuration_lib.h"
30 #include "gnunet_disk_lib.h"
32 #define LOG(kind, ...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34 #define LOG_STRERROR_FILE(kind, syscall, filename) \
35 GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
38 * @brief configuration entry
43 * This is a linked list.
45 struct ConfigEntry *next;
53 * current, commited value
60 * @brief configuration section
65 * This is a linked list.
67 struct ConfigSection *next;
70 * entries in the section
72 struct ConfigEntry *entries;
82 * @brief configuration data
84 struct GNUNET_CONFIGURATION_Handle
87 * Configuration sections.
89 struct ConfigSection *sections;
92 * Modification indication since last save
93 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
94 * #GNUNET_SYSERR on error (i.e. last save failed)
101 * Used for diffing a configuration object against
106 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
108 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
113 * Create a GNUNET_CONFIGURATION_Handle.
115 * @return fresh configuration object
117 struct GNUNET_CONFIGURATION_Handle *
118 GNUNET_CONFIGURATION_create ()
120 struct GNUNET_CONFIGURATION_Handle *cfg;
123 cfg = GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
124 /* make certain values from the project data available
126 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
129 GNUNET_CONFIGURATION_set_value_string (cfg,
135 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR);
138 GNUNET_CONFIGURATION_set_value_string (cfg,
144 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_BINDIR);
147 GNUNET_CONFIGURATION_set_value_string (cfg,
153 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_PREFIX);
156 GNUNET_CONFIGURATION_set_value_string (cfg,
162 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
165 GNUNET_CONFIGURATION_set_value_string (cfg,
171 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_ICONDIR);
174 GNUNET_CONFIGURATION_set_value_string (cfg,
180 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DOCDIR);
183 GNUNET_CONFIGURATION_set_value_string (cfg,
189 p = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBEXECDIR);
192 GNUNET_CONFIGURATION_set_value_string (cfg,
203 * Destroy configuration object.
205 * @param cfg configuration to destroy
208 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
210 struct ConfigSection *sec;
212 while (NULL != (sec = cfg->sections))
213 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
219 * Parse a configuration file @a filename and run the function
220 * @a cb with the resulting configuration object. Then free the
221 * configuration object and return the status value from @a cb.
223 * @param filename configuration to parse, NULL for "default"
224 * @param cb function to run
225 * @param cb_cls closure for @a cb
226 * @return #GNUNET_SYSERR if parsing the configuration failed,
227 * otherwise return value from @a cb.
230 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
231 GNUNET_CONFIGURATION_Callback cb,
234 struct GNUNET_CONFIGURATION_Handle *cfg;
237 cfg = GNUNET_CONFIGURATION_create ();
238 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, filename))
241 GNUNET_CONFIGURATION_destroy (cfg);
242 return GNUNET_SYSERR;
244 ret = cb (cb_cls, cfg);
245 GNUNET_CONFIGURATION_destroy (cfg);
251 * De-serializes configuration
253 * @param cfg configuration to update
254 * @param mem the memory block of serialized configuration
255 * @param size the size of the memory block
256 * @param basedir set to path from which we recursively load configuration
257 * from inlined configurations; NULL if not and raise warnings
258 * when we come across them
259 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
262 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
283 section = GNUNET_strdup ("");
287 while (r_bytes < size)
289 GNUNET_free_non_null (line_orig);
290 /* fgets-like behaviour on buffer */
291 to_read = size - r_bytes;
292 pos = memchr (&mem[r_bytes], '\n', to_read);
295 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
296 r_bytes += line_size;
301 GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
302 r_bytes += line_size + 1;
305 /* increment line number */
307 /* tabs and '\r' are whitespace */
308 emptyline = GNUNET_YES;
309 for (i = 0; i < line_size; i++)
316 emptyline = GNUNET_NO;
318 /* ignore empty lines */
319 if (GNUNET_YES == emptyline)
322 /* remove tailing whitespace */
323 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));
327 /* remove leading whitespace */
328 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
331 /* ignore comments */
332 if (('#' == line[0]) || ('%' == line[0]))
335 /* handle special "@INLINE@" directive */
336 if (0 == strncasecmp (line, "@INLINE@ ", strlen ("@INLINE@ ")))
339 value = &line[strlen ("@INLINE@ ")];
344 GNUNET_asprintf (&fn, "%s/%s", basedir, value);
345 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, fn))
348 ret = GNUNET_SYSERR; /* failed to parse included config */
355 LOG (GNUNET_ERROR_TYPE_DEBUG,
356 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
362 if (('[' == line[0]) && (']' == line[line_size - 1]))
365 line[line_size - 1] = '\0';
367 GNUNET_free (section);
368 section = GNUNET_strdup (value);
371 if (NULL != (eq = strchr (line, '=')))
374 tag = GNUNET_strndup (line, eq - line);
375 /* remove tailing whitespace */
376 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));
380 /* Strip whitespace */
382 while (isspace ((unsigned char) value[0]))
384 for (i = strlen (value) - 1;
385 (i >= 1) && (isspace ((unsigned char) value[i]));
391 if (('"' == value[0]) && ('"' == value[strlen (value) - 1]))
393 value[strlen (value) - 1] = '\0';
396 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
401 LOG (GNUNET_ERROR_TYPE_WARNING,
402 _ ("Syntax error while deserializing in line %u\n"),
407 GNUNET_free_non_null (line_orig);
408 GNUNET_free (section);
409 GNUNET_assert ((GNUNET_OK != ret) || (r_bytes == size));
415 * Parse a configuration file, add all of the options in the
416 * file to the configuration environment.
418 * @param cfg configuration to update
419 * @param filename name of the configuration file
420 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
423 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
424 const char *filename)
435 fn = GNUNET_STRINGS_filename_expand (filename);
436 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
438 return GNUNET_SYSERR;
439 dirty = cfg->dirty; /* back up value! */
441 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
443 LOG (GNUNET_ERROR_TYPE_WARNING,
444 "Error while determining the file size of `%s'\n",
447 return GNUNET_SYSERR;
451 GNUNET_break (0); /* File size is more than the heap size */
453 return GNUNET_SYSERR;
456 mem = GNUNET_malloc (fs);
457 sret = GNUNET_DISK_fn_read (fn, mem, fs);
458 if ((sret < 0) || (fs != (size_t) sret))
460 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Error while reading file `%s'\n"), fn);
463 return GNUNET_SYSERR;
465 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
466 endsep = strrchr (fn, (int) '/');
469 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, fn);
472 /* restore dirty flag - anything we set in the meantime
480 * Test if there are configuration options that were
481 * changed since the last save.
483 * @param cfg configuration to inspect
484 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
487 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
494 * Serializes the given configuration.
496 * @param cfg configuration to serialize
497 * @param size will be set to the size of the serialized memory block
498 * @return the memory block where the serialized configuration is
499 * present. This memory should be freed by the caller
502 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
505 struct ConfigSection *sec;
506 struct ConfigEntry *ent;
514 /* Pass1 : calculate the buffer size required */
516 for (sec = cfg->sections; NULL != sec; sec = sec->next)
518 /* For each section we need to add 3 charaters: {'[',']','\n'} */
519 m_size += strlen (sec->name) + 3;
520 for (ent = sec->entries; NULL != ent; ent = ent->next)
522 if (NULL != ent->val)
524 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
526 while (NULL != (pos = strstr (pos, "\n")))
531 /* For each key = value pair we need to add 4 characters (2
532 spaces and 1 equal-to character and 1 new line) */
533 m_size += strlen (ent->key) + strlen (ent->val) + 4;
536 /* A new line after section end */
540 /* Pass2: Allocate memory and write the configuration to it */
541 mem = GNUNET_malloc (m_size);
549 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
550 GNUNET_assert (0 < len);
551 GNUNET_memcpy (mem + c_size, cbuf, len);
554 for (ent = sec->entries; NULL != ent; ent = ent->next)
556 if (NULL != ent->val)
558 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
559 strcpy (val, ent->val);
560 while (NULL != (pos = strstr (val, "\n")))
562 memmove (&pos[2], &pos[1], strlen (&pos[1]));
566 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
568 GNUNET_memcpy (mem + c_size, cbuf, len);
573 GNUNET_memcpy (mem + c_size, "\n", 1);
577 GNUNET_assert (c_size == m_size);
584 * Write configuration file.
586 * @param cfg configuration to write
587 * @param filename where to write the configuration
588 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
591 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
592 const char *filename)
599 fn = GNUNET_STRINGS_filename_expand (filename);
601 return GNUNET_SYSERR;
602 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
605 return GNUNET_SYSERR;
607 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
608 sret = GNUNET_DISK_fn_write (fn,
611 GNUNET_DISK_PERM_USER_READ
612 | GNUNET_DISK_PERM_USER_WRITE
613 | GNUNET_DISK_PERM_GROUP_READ
614 | GNUNET_DISK_PERM_GROUP_WRITE);
615 if ((sret < 0) || (size != (size_t) sret))
618 GNUNET_free (cfg_buf);
619 LOG (GNUNET_ERROR_TYPE_WARNING,
620 "Writing configuration to file `%s' failed\n",
622 cfg->dirty = GNUNET_SYSERR; /* last write failed */
623 return GNUNET_SYSERR;
626 GNUNET_free (cfg_buf);
627 cfg->dirty = GNUNET_NO; /* last write succeeded */
633 * Iterate over all options in the configuration.
635 * @param cfg configuration to inspect
636 * @param iter function to call on each option
637 * @param iter_cls closure for @a iter
640 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
641 GNUNET_CONFIGURATION_Iterator iter,
644 struct ConfigSection *spos;
645 struct ConfigEntry *epos;
647 for (spos = cfg->sections; NULL != spos; spos = spos->next)
648 for (epos = spos->entries; NULL != epos; epos = epos->next)
649 if (NULL != epos->val)
650 iter (iter_cls, spos->name, epos->key, epos->val);
655 * Iterate over values of a section in the configuration.
657 * @param cfg configuration to inspect
658 * @param section the section
659 * @param iter function to call on each option
660 * @param iter_cls closure for @a iter
663 GNUNET_CONFIGURATION_iterate_section_values (
664 const struct GNUNET_CONFIGURATION_Handle *cfg,
666 GNUNET_CONFIGURATION_Iterator iter,
669 struct ConfigSection *spos;
670 struct ConfigEntry *epos;
672 spos = cfg->sections;
673 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
677 for (epos = spos->entries; NULL != epos; epos = epos->next)
678 if (NULL != epos->val)
679 iter (iter_cls, spos->name, epos->key, epos->val);
684 * Iterate over all sections in the configuration.
686 * @param cfg configuration to inspect
687 * @param iter function to call on each section
688 * @param iter_cls closure for @a iter
691 GNUNET_CONFIGURATION_iterate_sections (
692 const struct GNUNET_CONFIGURATION_Handle *cfg,
693 GNUNET_CONFIGURATION_Section_Iterator iter,
696 struct ConfigSection *spos;
697 struct ConfigSection *next;
699 next = cfg->sections;
704 iter (iter_cls, spos->name);
710 * Remove the given section and all options in it.
712 * @param cfg configuration to inspect
713 * @param section name of the section to remove
716 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
719 struct ConfigSection *spos;
720 struct ConfigSection *prev;
721 struct ConfigEntry *ent;
724 spos = cfg->sections;
727 if (0 == strcasecmp (section, spos->name))
730 cfg->sections = spos->next;
732 prev->next = spos->next;
733 while (NULL != (ent = spos->entries))
735 spos->entries = ent->next;
736 GNUNET_free (ent->key);
737 GNUNET_free_non_null (ent->val);
739 cfg->dirty = GNUNET_YES;
741 GNUNET_free (spos->name);
752 * Copy a configuration value to the given target configuration.
753 * Overwrites existing entries.
755 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
756 * @param section section for the value
757 * @param option option name of the value
758 * @param value value to copy
761 copy_entry (void *cls,
766 struct GNUNET_CONFIGURATION_Handle *dst = cls;
768 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
773 * Duplicate an existing configuration object.
775 * @param cfg configuration to duplicate
776 * @return duplicate configuration
778 struct GNUNET_CONFIGURATION_Handle *
779 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
781 struct GNUNET_CONFIGURATION_Handle *ret;
783 ret = GNUNET_CONFIGURATION_create ();
784 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
790 * Find a section entry from a configuration.
792 * @param cfg configuration to search in
793 * @param section name of the section to look for
794 * @return matching entry, NULL if not found
796 static struct ConfigSection *
797 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
800 struct ConfigSection *pos;
803 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
810 * Find an entry from a configuration.
812 * @param cfg handle to the configuration
813 * @param section section the option is in
814 * @param key the option
815 * @return matching entry, NULL if not found
817 static struct ConfigEntry *
818 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
822 struct ConfigSection *sec;
823 struct ConfigEntry *pos;
825 if (NULL == (sec = find_section (cfg, section)))
828 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
835 * A callback function, compares entries from two configurations
836 * (default against a new configuration) and write the diffs in a
837 * diff-configuration object (the callback object).
839 * @param cls the diff configuration (`struct DiffHandle *`)
840 * @param section section for the value (of the default conf.)
841 * @param option option name of the value (of the default conf.)
842 * @param value value to copy (of the default conf.)
845 compare_entries (void *cls,
850 struct DiffHandle *dh = cls;
851 struct ConfigEntry *entNew;
853 entNew = find_entry (dh->cfg_default, section, option);
854 if ((NULL != entNew) && (NULL != entNew->val) &&
855 (0 == strcmp (entNew->val, value)))
857 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
862 * Compute configuration with only entries that have been changed
864 * @param cfg_default original configuration
865 * @param cfg_new new configuration
866 * @return configuration with only the differences, never NULL
868 struct GNUNET_CONFIGURATION_Handle *
869 GNUNET_CONFIGURATION_get_diff (
870 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
871 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
873 struct DiffHandle diffHandle;
875 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
876 diffHandle.cfg_default = cfg_default;
877 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
878 return diffHandle.cfgDiff;
883 * Write only configuration entries that have been changed to configuration file
885 * @param cfg_default default configuration
886 * @param cfg_new new configuration
887 * @param filename where to write the configuration diff between default and new
888 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
891 GNUNET_CONFIGURATION_write_diffs (
892 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
893 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
894 const char *filename)
897 struct GNUNET_CONFIGURATION_Handle *diff;
899 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
900 ret = GNUNET_CONFIGURATION_write (diff, filename);
901 GNUNET_CONFIGURATION_destroy (diff);
907 * Set a configuration value that should be a string.
909 * @param cfg configuration to update
910 * @param section section of interest
911 * @param option option of interest
912 * @param value value to set
915 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
920 struct ConfigSection *sec;
921 struct ConfigEntry *e;
924 e = find_entry (cfg, section, option);
929 GNUNET_free_non_null (e->val);
934 nv = GNUNET_strdup (value);
935 GNUNET_free_non_null (e->val);
940 sec = find_section (cfg, section);
943 sec = GNUNET_new (struct ConfigSection);
944 sec->name = GNUNET_strdup (section);
945 sec->next = cfg->sections;
948 e = GNUNET_new (struct ConfigEntry);
949 e->key = GNUNET_strdup (option);
950 e->val = GNUNET_strdup (value);
951 e->next = sec->entries;
957 * Set a configuration value that should be a number.
959 * @param cfg configuration to update
960 * @param section section of interest
961 * @param option option of interest
962 * @param number value to set
965 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
968 unsigned long long number)
972 GNUNET_snprintf (s, 64, "%llu", number);
973 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
978 * Get a configuration value that should be a number.
980 * @param cfg configuration to inspect
981 * @param section section of interest
982 * @param option option of interest
983 * @param number where to store the numeric value of the option
984 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
987 GNUNET_CONFIGURATION_get_value_number (
988 const struct GNUNET_CONFIGURATION_Handle *cfg,
991 unsigned long long *number)
993 struct ConfigEntry *e;
996 if (NULL == (e = find_entry (cfg, section, option)))
997 return GNUNET_SYSERR;
999 return GNUNET_SYSERR;
1000 if (1 != sscanf (e->val, "%llu%1s", number, dummy))
1001 return GNUNET_SYSERR;
1007 * Get a configuration value that should be a floating point number.
1009 * @param cfg configuration to inspect
1010 * @param section section of interest
1011 * @param option option of interest
1012 * @param number where to store the floating value of the option
1013 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1016 GNUNET_CONFIGURATION_get_value_float (
1017 const struct GNUNET_CONFIGURATION_Handle *cfg,
1018 const char *section,
1022 struct ConfigEntry *e;
1025 if (NULL == (e = find_entry (cfg, section, option)))
1026 return GNUNET_SYSERR;
1028 return GNUNET_SYSERR;
1029 if (1 != sscanf (e->val, "%f%1s", number, dummy))
1030 return GNUNET_SYSERR;
1036 * Get a configuration value that should be a relative time.
1038 * @param cfg configuration to inspect
1039 * @param section section of interest
1040 * @param option option of interest
1041 * @param time set to the time value stored in the configuration
1042 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1045 GNUNET_CONFIGURATION_get_value_time (
1046 const struct GNUNET_CONFIGURATION_Handle *cfg,
1047 const char *section,
1049 struct GNUNET_TIME_Relative *time)
1051 struct ConfigEntry *e;
1054 if (NULL == (e = find_entry (cfg, section, option)))
1055 return GNUNET_SYSERR;
1057 return GNUNET_SYSERR;
1058 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
1059 if (GNUNET_OK != ret)
1060 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1063 _ ("Not a valid relative time specification"));
1069 * Get a configuration value that should be a size in bytes.
1071 * @param cfg configuration to inspect
1072 * @param section section of interest
1073 * @param option option of interest
1074 * @param size set to the size in bytes as stored in the configuration
1075 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1078 GNUNET_CONFIGURATION_get_value_size (
1079 const struct GNUNET_CONFIGURATION_Handle *cfg,
1080 const char *section,
1082 unsigned long long *size)
1084 struct ConfigEntry *e;
1086 if (NULL == (e = find_entry (cfg, section, option)))
1087 return GNUNET_SYSERR;
1089 return GNUNET_SYSERR;
1090 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
1095 * Get a configuration value that should be a string.
1097 * @param cfg configuration to inspect
1098 * @param section section of interest
1099 * @param option option of interest
1100 * @param value will be set to a freshly allocated configuration
1101 * value, or NULL if option is not specified
1102 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1105 GNUNET_CONFIGURATION_get_value_string (
1106 const struct GNUNET_CONFIGURATION_Handle *cfg,
1107 const char *section,
1111 struct ConfigEntry *e;
1113 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1116 return GNUNET_SYSERR;
1118 *value = GNUNET_strdup (e->val);
1124 * Get a configuration value that should be in a set of
1125 * predefined strings
1127 * @param cfg configuration to inspect
1128 * @param section section of interest
1129 * @param option option of interest
1130 * @param choices NULL-terminated list of legal values
1131 * @param value will be set to an entry in the legal list,
1132 * or NULL if option is not specified and no default given
1133 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1136 GNUNET_CONFIGURATION_get_value_choice (
1137 const struct GNUNET_CONFIGURATION_Handle *cfg,
1138 const char *section,
1140 const char *const *choices,
1143 struct ConfigEntry *e;
1146 if (NULL == (e = find_entry (cfg, section, option)))
1147 return GNUNET_SYSERR;
1148 for (i = 0; NULL != choices[i]; i++)
1149 if (0 == strcasecmp (choices[i], e->val))
1151 if (NULL == choices[i])
1153 LOG (GNUNET_ERROR_TYPE_ERROR,
1154 _ ("Configuration value '%s' for '%s'"
1155 " in section '%s' is not in set of legal choices\n"),
1159 return GNUNET_SYSERR;
1161 *value = choices[i];
1167 * Get crockford32-encoded fixed-size binary data from a configuration.
1169 * @param cfg configuration to access
1170 * @param section section to access
1171 * @param option option to access
1172 * @param buf where to store the decoded binary result
1173 * @param buf_size exact number of bytes to store in @a buf
1174 * @return #GNUNET_OK on success
1175 * #GNUNET_NO is the value does not exist
1176 * #GNUNET_SYSERR on decoding error
1179 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1180 const char *section,
1191 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &enc)))
1193 data_size = (strlen (enc) * 5) / 8;
1194 if (data_size != buf_size)
1197 return GNUNET_SYSERR;
1200 GNUNET_STRINGS_string_to_data (enc, strlen (enc), buf, buf_size))
1203 return GNUNET_SYSERR;
1211 * Test if we have a value for a particular option
1213 * @param cfg configuration to inspect
1214 * @param section section of interest
1215 * @param option option of interest
1216 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1219 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1220 const char *section,
1223 struct ConfigEntry *e;
1225 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1232 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1233 * where either in the "PATHS" section or the environtment "FOO" is
1234 * set to "DIRECTORY". We also support default expansion,
1235 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1236 * set in PATHS or the environment, and otherwise to "default". Note
1237 * that "default" itself can also be a $-expression, thus
1238 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1241 * @param cfg configuration to use for path expansion
1242 * @param orig string to $-expand (will be freed!)
1243 * @param depth recursion depth, used to detect recursive expansions
1244 * @return $-expanded string, never NULL unless @a orig was NULL
1247 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1267 LOG (GNUNET_ERROR_TYPE_WARNING,
1269 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1273 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1276 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1300 LOG (GNUNET_ERROR_TYPE_WARNING,
1301 _ ("Missing closing `%s' in option `%s'\n"),
1314 def = strchr (orig, ':');
1319 if (('-' == *def) || ('=' == *def))
1321 def = GNUNET_strdup (def);
1331 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1334 if (orig[i] == '\0')
1340 erased_char = orig[i];
1341 erased_pos = &orig[i];
1343 post = &orig[i + 1];
1346 LOG (GNUNET_ERROR_TYPE_DEBUG,
1347 "Split into `%s' and `%s' with default %s\n",
1352 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", start, &prefix))
1354 if (NULL == (env = getenv (start)))
1357 def = expand_dollar (cfg, def, depth + 1);
1362 start = GNUNET_strdup (start);
1364 *erased_pos = erased_char;
1365 LOG (GNUNET_ERROR_TYPE_WARNING,
1367 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1370 GNUNET_free (start);
1373 prefix = GNUNET_strdup (env);
1375 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1376 if ((erased_pos) && ('}' != erased_char))
1378 len = strlen (prefix) + 1;
1379 prefix = GNUNET_realloc (prefix, len + 1);
1380 prefix[len - 1] = erased_char;
1383 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1384 strcpy (result, prefix);
1385 strcat (result, post);
1386 GNUNET_free_non_null (def);
1387 GNUNET_free (prefix);
1394 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1395 * where either in the "PATHS" section or the environtment "FOO" is
1396 * set to "DIRECTORY". We also support default expansion,
1397 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1398 * set in PATHS or the environment, and otherwise to "default". Note
1399 * that "default" itself can also be a $-expression, thus
1400 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1403 * @param cfg configuration to use for path expansion
1404 * @param orig string to $-expand (will be freed!). Note that multiple
1405 * $-expressions can be present in this string. They will all be
1407 * @return $-expanded string
1410 GNUNET_CONFIGURATION_expand_dollar (
1411 const struct GNUNET_CONFIGURATION_Handle *cfg,
1418 for (i = 0; '\0' != orig[i]; i++)
1422 dup = GNUNET_strdup (orig + i);
1423 dup = expand_dollar (cfg, dup, 0);
1424 GNUNET_assert (NULL != dup); /* make compiler happy */
1425 len = strlen (dup) + 1;
1426 orig = GNUNET_realloc (orig, i + len);
1427 GNUNET_memcpy (orig + i, dup, len);
1435 * Get a configuration value that should be a string.
1437 * @param cfg configuration to inspect
1438 * @param section section of interest
1439 * @param option option of interest
1440 * @param value will be set to a freshly allocated configuration
1441 * value, or NULL if option is not specified
1442 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1445 GNUNET_CONFIGURATION_get_value_filename (
1446 const struct GNUNET_CONFIGURATION_Handle *cfg,
1447 const char *section,
1454 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1456 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1458 return GNUNET_SYSERR;
1460 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1461 *value = GNUNET_STRINGS_filename_expand (tmp);
1464 return GNUNET_SYSERR;
1470 * Get a configuration value that should be in a set of
1473 * @param cfg configuration to inspect
1474 * @param section section of interest
1475 * @param option option of interest
1476 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1479 GNUNET_CONFIGURATION_get_value_yesno (
1480 const struct GNUNET_CONFIGURATION_Handle *cfg,
1481 const char *section,
1484 static const char *yesno[] = { "YES", "NO", NULL };
1489 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1490 if (ret == GNUNET_SYSERR)
1492 if (val == yesno[0])
1499 * Iterate over the set of filenames stored in a configuration value.
1501 * @param cfg configuration to inspect
1502 * @param section section of interest
1503 * @param option option of interest
1504 * @param cb function to call on each filename
1505 * @param cb_cls closure for @a cb
1506 * @return number of filenames iterated over, -1 on error
1509 GNUNET_CONFIGURATION_iterate_value_filenames (
1510 const struct GNUNET_CONFIGURATION_Handle *cfg,
1511 const char *section,
1513 GNUNET_FileNameCallback cb,
1523 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1525 GNUNET_assert (list != NULL);
1530 while (pos[0] == ' ')
1532 if (strlen (pos) == 0)
1535 while ((end[0] != ' ') && (end[0] != '\0'))
1543 memmove (end, &end[1], strlen (&end[1]) + 1);
1546 /* illegal, but just keep it */
1550 /* illegal, but just ignore that there was a '/' */
1558 if (strlen (pos) > 0)
1561 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1563 ret = GNUNET_SYSERR;
1579 * @param value FIXME
1583 escape_name (const char *value)
1589 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1590 memset (escaped, 0, strlen (value) * 2 + 1);
1593 while (rpos[0] != '\0')
1617 * @param cls string we compare with (const char*)
1618 * @param fn filename we are currently looking at
1619 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1622 test_match (void *cls, const char *fn)
1624 const char *of = cls;
1626 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1631 * Append a filename to a configuration value that
1632 * represents a list of filenames
1634 * @param cfg configuration to update
1635 * @param section section of interest
1636 * @param option option of interest
1637 * @param value filename to append
1638 * @return #GNUNET_OK on success,
1639 * #GNUNET_NO if the filename already in the list
1640 * #GNUNET_SYSERR on error
1643 GNUNET_CONFIGURATION_append_value_filename (
1644 struct GNUNET_CONFIGURATION_Handle *cfg,
1645 const char *section,
1653 if (GNUNET_SYSERR ==
1654 GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1659 return GNUNET_NO; /* already exists */
1661 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1662 old = GNUNET_strdup ("");
1663 escaped = escape_name (value);
1664 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1666 if (strlen (old) > 0)
1668 strcat (nw, escaped);
1669 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1672 GNUNET_free (escaped);
1678 * Remove a filename from a configuration value that
1679 * represents a list of filenames
1681 * @param cfg configuration to update
1682 * @param section section of interest
1683 * @param option option of interest
1684 * @param value filename to remove
1685 * @return #GNUNET_OK on success,
1686 * #GNUNET_NO if the filename is not in the list,
1687 * #GNUNET_SYSERR on error
1690 GNUNET_CONFIGURATION_remove_value_filename (
1691 struct GNUNET_CONFIGURATION_Handle *cfg,
1692 const char *section,
1703 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1705 match = escape_name (value);
1709 while (pos[0] == ' ')
1711 if (strlen (pos) == 0)
1714 while ((end[0] != ' ') && (end[0] != '\0'))
1726 /* illegal, but just keep it */
1730 /* illegal, but just ignore that there was a '/' */
1738 if (0 == strcmp (pos, match))
1741 memmove (pos, &end[1], strlen (&end[1]) + 1);
1749 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1751 GNUNET_free (match);
1760 GNUNET_free (match);
1766 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1767 * file in a directory, we trigger parsing on those files that
1770 * @param cls the cfg
1771 * @param filename file to parse
1772 * @return #GNUNET_OK on success
1775 parse_configuration_file (void *cls, const char *filename)
1777 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1781 /* Examine file extension */
1782 ext = strrchr (filename, '.');
1783 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1785 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1789 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1795 * Load default configuration. This function will parse the
1796 * defaults from the given defaults_d directory.
1798 * @param cfg configuration to update
1799 * @param defaults_d directory with the defaults
1800 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1803 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1804 const char *defaults_d)
1806 if (GNUNET_SYSERR ==
1807 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1808 return GNUNET_SYSERR; /* no configuration at all found */
1813 /* end of configuration.c */