2 This file is part of GNUnet.
3 Copyright (C) 2006, 2007, 2008, 2009, 2013 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
22 * @file src/util/configuration.c
23 * @brief configuration management
24 * @author Christian Grothoff
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_strings_lib.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_disk_lib.h"
33 #define LOG(kind, ...) GNUNET_log_from(kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR_FILE(kind, syscall, filename) \
36 GNUNET_log_from_strerror_file(kind, "util", syscall, filename)
39 * @brief configuration entry
43 * This is a linked list.
45 struct ConfigEntry *next;
53 * current, commited value
60 * @brief configuration section
62 struct ConfigSection {
64 * This is a linked list.
66 struct ConfigSection *next;
69 * entries in the section
71 struct ConfigEntry *entries;
81 * @brief configuration data
83 struct GNUNET_CONFIGURATION_Handle {
85 * Configuration sections.
87 struct ConfigSection *sections;
90 * Modification indication since last save
91 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
92 * #GNUNET_SYSERR on error (i.e. last save failed)
99 * Used for diffing a configuration object against
103 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
105 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
110 * Create a GNUNET_CONFIGURATION_Handle.
112 * @return fresh configuration object
114 struct GNUNET_CONFIGURATION_Handle *
115 GNUNET_CONFIGURATION_create()
117 return GNUNET_new(struct GNUNET_CONFIGURATION_Handle);
122 * Destroy configuration object.
124 * @param cfg configuration to destroy
127 GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
129 struct ConfigSection *sec;
131 while (NULL != (sec = cfg->sections))
132 GNUNET_CONFIGURATION_remove_section(cfg, sec->name);
138 * Parse a configuration file @a filename and run the function
139 * @a cb with the resulting configuration object. Then free the
140 * configuration object and return the status value from @a cb.
142 * @param filename configuration to parse, NULL for "default"
143 * @param cb function to run
144 * @param cb_cls closure for @a cb
145 * @return #GNUNET_SYSERR if parsing the configuration failed,
146 * otherwise return value from @a cb.
149 GNUNET_CONFIGURATION_parse_and_run(const char *filename,
150 GNUNET_CONFIGURATION_Callback cb,
153 struct GNUNET_CONFIGURATION_Handle *cfg;
156 cfg = GNUNET_CONFIGURATION_create();
157 if (GNUNET_OK != GNUNET_CONFIGURATION_load(cfg, filename))
160 GNUNET_CONFIGURATION_destroy(cfg);
161 return GNUNET_SYSERR;
163 ret = cb(cb_cls, cfg);
164 GNUNET_CONFIGURATION_destroy(cfg);
170 * De-serializes configuration
172 * @param cfg configuration to update
173 * @param mem the memory block of serialized configuration
174 * @param size the size of the memory block
175 * @param basedir set to path from which we recursively load configuration
176 * from inlined configurations; NULL if not and raise warnings
177 * when we come across them
178 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
181 GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg,
202 section = GNUNET_strdup("");
206 while (r_bytes < size)
208 GNUNET_free_non_null(line_orig);
209 /* fgets-like behaviour on buffer */
210 to_read = size - r_bytes;
211 pos = memchr(&mem[r_bytes], '\n', to_read);
214 line_orig = GNUNET_strndup(&mem[r_bytes], line_size = to_read);
215 r_bytes += line_size;
220 GNUNET_strndup(&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
221 r_bytes += line_size + 1;
224 /* increment line number */
226 /* tabs and '\r' are whitespace */
227 emptyline = GNUNET_YES;
228 for (i = 0; i < line_size; i++)
235 emptyline = GNUNET_NO;
237 /* ignore empty lines */
238 if (GNUNET_YES == emptyline)
241 /* remove tailing whitespace */
242 for (i = line_size - 1; (i >= 1) && (isspace((unsigned char)line[i]));
246 /* remove leading whitespace */
247 for (; line[0] != '\0' && (isspace((unsigned char)line[0])); line++)
250 /* ignore comments */
251 if (('#' == line[0]) || ('%' == line[0]))
254 /* handle special "@INLINE@" directive */
255 if (0 == strncasecmp(line, "@INLINE@ ", strlen("@INLINE@ ")))
258 value = &line[strlen("@INLINE@ ")];
263 GNUNET_asprintf(&fn, "%s/%s", basedir, value);
264 if (GNUNET_OK != GNUNET_CONFIGURATION_parse(cfg, fn))
267 ret = GNUNET_SYSERR; /* failed to parse included config */
274 LOG(GNUNET_ERROR_TYPE_DEBUG,
275 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
281 if (('[' == line[0]) && (']' == line[line_size - 1]))
284 line[line_size - 1] = '\0';
286 GNUNET_free(section);
287 section = GNUNET_strdup(value);
290 if (NULL != (eq = strchr(line, '=')))
293 tag = GNUNET_strndup(line, eq - line);
294 /* remove tailing whitespace */
295 for (i = strlen(tag) - 1; (i >= 1) && (isspace((unsigned char)tag[i]));
299 /* Strip whitespace */
301 while (isspace((unsigned char)value[0]))
303 for (i = strlen(value) - 1;
304 (i >= 1) && (isspace((unsigned char)value[i]));
310 if (('"' == value[0]) && ('"' == value[strlen(value) - 1]))
312 value[strlen(value) - 1] = '\0';
315 GNUNET_CONFIGURATION_set_value_string(cfg, section, tag, &value[i]);
320 LOG(GNUNET_ERROR_TYPE_WARNING,
321 _("Syntax error while deserializing in line %u\n"),
326 GNUNET_free_non_null(line_orig);
327 GNUNET_free(section);
328 GNUNET_assert((GNUNET_OK != ret) || (r_bytes == size));
334 * Parse a configuration file, add all of the options in the
335 * file to the configuration environment.
337 * @param cfg configuration to update
338 * @param filename name of the configuration file
339 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
342 GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg,
343 const char *filename)
354 fn = GNUNET_STRINGS_filename_expand(filename);
355 LOG(GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
357 return GNUNET_SYSERR;
358 dirty = cfg->dirty; /* back up value! */
360 GNUNET_DISK_file_size(fn, &fs64, GNUNET_YES, GNUNET_YES))
362 LOG(GNUNET_ERROR_TYPE_WARNING,
363 "Error while determining the file size of `%s'\n",
366 return GNUNET_SYSERR;
370 GNUNET_break(0); /* File size is more than the heap size */
372 return GNUNET_SYSERR;
375 mem = GNUNET_malloc(fs);
376 sret = GNUNET_DISK_fn_read(fn, mem, fs);
377 if ((sret < 0) || (fs != (size_t)sret))
379 LOG(GNUNET_ERROR_TYPE_WARNING, _("Error while reading file `%s'\n"), fn);
382 return GNUNET_SYSERR;
384 LOG(GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
385 endsep = strrchr(fn, (int)'/');
388 ret = GNUNET_CONFIGURATION_deserialize(cfg, mem, fs, fn);
391 /* restore dirty flag - anything we set in the meantime
399 * Test if there are configuration options that were
400 * changed since the last save.
402 * @param cfg configuration to inspect
403 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
406 GNUNET_CONFIGURATION_is_dirty(const struct GNUNET_CONFIGURATION_Handle *cfg)
413 * Serializes the given configuration.
415 * @param cfg configuration to serialize
416 * @param size will be set to the size of the serialized memory block
417 * @return the memory block where the serialized configuration is
418 * present. This memory should be freed by the caller
421 GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg,
424 struct ConfigSection *sec;
425 struct ConfigEntry *ent;
434 /* Pass1 : calculate the buffer size required */
436 for (sec = cfg->sections; NULL != sec; sec = sec->next)
438 /* For each section we need to add 3 charaters: {'[',']','\n'} */
439 m_size += strlen(sec->name) + 3;
440 for (ent = sec->entries; NULL != ent; ent = ent->next)
442 if (NULL != ent->val)
444 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
446 while (NULL != (pos = strstr(pos, "\n")))
451 /* For each key = value pair we need to add 4 characters (2
452 spaces and 1 equal-to character and 1 new line) */
453 m_size += strlen(ent->key) + strlen(ent->val) + 4;
456 /* A new line after section end */
460 /* Pass2: Allocate memory and write the configuration to it */
461 mem = GNUNET_malloc(m_size);
467 len = GNUNET_asprintf(&cbuf, "[%s]\n", sec->name);
468 GNUNET_assert(0 < len);
469 GNUNET_memcpy(mem + c_size, cbuf, len);
472 for (ent = sec->entries; NULL != ent; ent = ent->next)
474 if (NULL != ent->val)
476 val = GNUNET_malloc(strlen(ent->val) * 2 + 1);
477 strcpy(val, ent->val);
478 while (NULL != (pos = strstr(val, "\n")))
480 memmove(&pos[2], &pos[1], strlen(&pos[1]));
484 len = GNUNET_asprintf(&cbuf, "%s = %s\n", ent->key, val);
486 GNUNET_memcpy(mem + c_size, cbuf, len);
491 GNUNET_memcpy(mem + c_size, "\n", 1);
495 GNUNET_assert(c_size == m_size);
502 * Write configuration file.
504 * @param cfg configuration to write
505 * @param filename where to write the configuration
506 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
509 GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg,
510 const char *filename)
517 fn = GNUNET_STRINGS_filename_expand(filename);
519 return GNUNET_SYSERR;
520 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(fn))
523 return GNUNET_SYSERR;
525 cfg_buf = GNUNET_CONFIGURATION_serialize(cfg, &size);
526 sret = GNUNET_DISK_fn_write(fn,
529 GNUNET_DISK_PERM_USER_READ |
530 GNUNET_DISK_PERM_USER_WRITE |
531 GNUNET_DISK_PERM_GROUP_READ |
532 GNUNET_DISK_PERM_GROUP_WRITE);
533 if ((sret < 0) || (size != (size_t)sret))
536 GNUNET_free(cfg_buf);
537 LOG(GNUNET_ERROR_TYPE_WARNING,
538 "Writing configuration to file `%s' failed\n",
540 cfg->dirty = GNUNET_SYSERR; /* last write failed */
541 return GNUNET_SYSERR;
544 GNUNET_free(cfg_buf);
545 cfg->dirty = GNUNET_NO; /* last write succeeded */
551 * Iterate over all options in the configuration.
553 * @param cfg configuration to inspect
554 * @param iter function to call on each option
555 * @param iter_cls closure for @a iter
558 GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg,
559 GNUNET_CONFIGURATION_Iterator iter,
562 struct ConfigSection *spos;
563 struct ConfigEntry *epos;
565 for (spos = cfg->sections; NULL != spos; spos = spos->next)
566 for (epos = spos->entries; NULL != epos; epos = epos->next)
567 if (NULL != epos->val)
568 iter(iter_cls, spos->name, epos->key, epos->val);
573 * Iterate over values of a section in the configuration.
575 * @param cfg configuration to inspect
576 * @param section the section
577 * @param iter function to call on each option
578 * @param iter_cls closure for @a iter
581 GNUNET_CONFIGURATION_iterate_section_values(
582 const struct GNUNET_CONFIGURATION_Handle *cfg,
584 GNUNET_CONFIGURATION_Iterator iter,
587 struct ConfigSection *spos;
588 struct ConfigEntry *epos;
590 spos = cfg->sections;
591 while ((spos != NULL) && (0 != strcasecmp(spos->name, section)))
595 for (epos = spos->entries; NULL != epos; epos = epos->next)
596 if (NULL != epos->val)
597 iter(iter_cls, spos->name, epos->key, epos->val);
602 * Iterate over all sections in the configuration.
604 * @param cfg configuration to inspect
605 * @param iter function to call on each section
606 * @param iter_cls closure for @a iter
609 GNUNET_CONFIGURATION_iterate_sections(
610 const struct GNUNET_CONFIGURATION_Handle *cfg,
611 GNUNET_CONFIGURATION_Section_Iterator iter,
614 struct ConfigSection *spos;
615 struct ConfigSection *next;
617 next = cfg->sections;
622 iter(iter_cls, spos->name);
628 * Remove the given section and all options in it.
630 * @param cfg configuration to inspect
631 * @param section name of the section to remove
634 GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg,
637 struct ConfigSection *spos;
638 struct ConfigSection *prev;
639 struct ConfigEntry *ent;
642 spos = cfg->sections;
645 if (0 == strcasecmp(section, spos->name))
648 cfg->sections = spos->next;
650 prev->next = spos->next;
651 while (NULL != (ent = spos->entries))
653 spos->entries = ent->next;
654 GNUNET_free(ent->key);
655 GNUNET_free_non_null(ent->val);
657 cfg->dirty = GNUNET_YES;
659 GNUNET_free(spos->name);
670 * Copy a configuration value to the given target configuration.
671 * Overwrites existing entries.
673 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
674 * @param section section for the value
675 * @param option option name of the value
676 * @param value value to copy
679 copy_entry(void *cls,
684 struct GNUNET_CONFIGURATION_Handle *dst = cls;
686 GNUNET_CONFIGURATION_set_value_string(dst, section, option, value);
691 * Duplicate an existing configuration object.
693 * @param cfg configuration to duplicate
694 * @return duplicate configuration
696 struct GNUNET_CONFIGURATION_Handle *
697 GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
699 struct GNUNET_CONFIGURATION_Handle *ret;
701 ret = GNUNET_CONFIGURATION_create();
702 GNUNET_CONFIGURATION_iterate(cfg, ©_entry, ret);
708 * Find a section entry from a configuration.
710 * @param cfg configuration to search in
711 * @param section name of the section to look for
712 * @return matching entry, NULL if not found
714 static struct ConfigSection *
715 find_section(const struct GNUNET_CONFIGURATION_Handle *cfg,
718 struct ConfigSection *pos;
721 while ((pos != NULL) && (0 != strcasecmp(section, pos->name)))
728 * Find an entry from a configuration.
730 * @param cfg handle to the configuration
731 * @param section section the option is in
732 * @param key the option
733 * @return matching entry, NULL if not found
735 static struct ConfigEntry *
736 find_entry(const struct GNUNET_CONFIGURATION_Handle *cfg,
740 struct ConfigSection *sec;
741 struct ConfigEntry *pos;
743 if (NULL == (sec = find_section(cfg, section)))
746 while ((pos != NULL) && (0 != strcasecmp(key, pos->key)))
753 * A callback function, compares entries from two configurations
754 * (default against a new configuration) and write the diffs in a
755 * diff-configuration object (the callback object).
757 * @param cls the diff configuration (`struct DiffHandle *`)
758 * @param section section for the value (of the default conf.)
759 * @param option option name of the value (of the default conf.)
760 * @param value value to copy (of the default conf.)
763 compare_entries(void *cls,
768 struct DiffHandle *dh = cls;
769 struct ConfigEntry *entNew;
771 entNew = find_entry(dh->cfg_default, section, option);
772 if ((NULL != entNew) && (NULL != entNew->val) &&
773 (0 == strcmp(entNew->val, value)))
775 GNUNET_CONFIGURATION_set_value_string(dh->cfgDiff, section, option, value);
780 * Compute configuration with only entries that have been changed
782 * @param cfg_default original configuration
783 * @param cfg_new new configuration
784 * @return configuration with only the differences, never NULL
786 struct GNUNET_CONFIGURATION_Handle *
787 GNUNET_CONFIGURATION_get_diff(
788 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
789 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
791 struct DiffHandle diffHandle;
793 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create();
794 diffHandle.cfg_default = cfg_default;
795 GNUNET_CONFIGURATION_iterate(cfg_new, &compare_entries, &diffHandle);
796 return diffHandle.cfgDiff;
801 * Write only configuration entries that have been changed to configuration file
803 * @param cfg_default default configuration
804 * @param cfg_new new configuration
805 * @param filename where to write the configuration diff between default and new
806 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
809 GNUNET_CONFIGURATION_write_diffs(
810 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
811 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
812 const char *filename)
815 struct GNUNET_CONFIGURATION_Handle *diff;
817 diff = GNUNET_CONFIGURATION_get_diff(cfg_default, cfg_new);
818 ret = GNUNET_CONFIGURATION_write(diff, filename);
819 GNUNET_CONFIGURATION_destroy(diff);
825 * Set a configuration value that should be a string.
827 * @param cfg configuration to update
828 * @param section section of interest
829 * @param option option of interest
830 * @param value value to set
833 GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg,
838 struct ConfigSection *sec;
839 struct ConfigEntry *e;
842 e = find_entry(cfg, section, option);
847 GNUNET_free_non_null(e->val);
852 nv = GNUNET_strdup(value);
853 GNUNET_free_non_null(e->val);
858 sec = find_section(cfg, section);
861 sec = GNUNET_new(struct ConfigSection);
862 sec->name = GNUNET_strdup(section);
863 sec->next = cfg->sections;
866 e = GNUNET_new(struct ConfigEntry);
867 e->key = GNUNET_strdup(option);
868 e->val = GNUNET_strdup(value);
869 e->next = sec->entries;
875 * Set a configuration value that should be a number.
877 * @param cfg configuration to update
878 * @param section section of interest
879 * @param option option of interest
880 * @param number value to set
883 GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg,
886 unsigned long long number)
890 GNUNET_snprintf(s, 64, "%llu", number);
891 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, s);
896 * Get a configuration value that should be a number.
898 * @param cfg configuration to inspect
899 * @param section section of interest
900 * @param option option of interest
901 * @param number where to store the numeric value of the option
902 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
905 GNUNET_CONFIGURATION_get_value_number(
906 const struct GNUNET_CONFIGURATION_Handle *cfg,
909 unsigned long long *number)
911 struct ConfigEntry *e;
914 if (NULL == (e = find_entry(cfg, section, option)))
915 return GNUNET_SYSERR;
917 return GNUNET_SYSERR;
918 if (1 != sscanf(e->val, "%llu%1s", number, dummy))
919 return GNUNET_SYSERR;
925 * Get a configuration value that should be a floating point number.
927 * @param cfg configuration to inspect
928 * @param section section of interest
929 * @param option option of interest
930 * @param number where to store the floating value of the option
931 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
934 GNUNET_CONFIGURATION_get_value_float(
935 const struct GNUNET_CONFIGURATION_Handle *cfg,
940 struct ConfigEntry *e;
943 if (NULL == (e = find_entry(cfg, section, option)))
944 return GNUNET_SYSERR;
946 return GNUNET_SYSERR;
947 if (1 != sscanf(e->val, "%f%1s", number, dummy))
948 return GNUNET_SYSERR;
954 * Get a configuration value that should be a relative time.
956 * @param cfg configuration to inspect
957 * @param section section of interest
958 * @param option option of interest
959 * @param time set to the time value stored in the configuration
960 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
963 GNUNET_CONFIGURATION_get_value_time(
964 const struct GNUNET_CONFIGURATION_Handle *cfg,
967 struct GNUNET_TIME_Relative *time)
969 struct ConfigEntry *e;
972 if (NULL == (e = find_entry(cfg, section, option)))
973 return GNUNET_SYSERR;
975 return GNUNET_SYSERR;
976 ret = GNUNET_STRINGS_fancy_time_to_relative(e->val, time);
977 if (GNUNET_OK != ret)
978 GNUNET_log_config_invalid(GNUNET_ERROR_TYPE_ERROR,
981 _("Not a valid relative time specification"));
987 * Get a configuration value that should be a size in bytes.
989 * @param cfg configuration to inspect
990 * @param section section of interest
991 * @param option option of interest
992 * @param size set to the size in bytes as stored in the configuration
993 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
996 GNUNET_CONFIGURATION_get_value_size(
997 const struct GNUNET_CONFIGURATION_Handle *cfg,
1000 unsigned long long *size)
1002 struct ConfigEntry *e;
1004 if (NULL == (e = find_entry(cfg, section, option)))
1005 return GNUNET_SYSERR;
1007 return GNUNET_SYSERR;
1008 return GNUNET_STRINGS_fancy_size_to_bytes(e->val, size);
1013 * Get a configuration value that should be a string.
1015 * @param cfg configuration to inspect
1016 * @param section section of interest
1017 * @param option option of interest
1018 * @param value will be set to a freshly allocated configuration
1019 * value, or NULL if option is not specified
1020 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1023 GNUNET_CONFIGURATION_get_value_string(
1024 const struct GNUNET_CONFIGURATION_Handle *cfg,
1025 const char *section,
1029 struct ConfigEntry *e;
1031 if ((NULL == (e = find_entry(cfg, section, option))) || (NULL == e->val))
1034 return GNUNET_SYSERR;
1036 *value = GNUNET_strdup(e->val);
1042 * Get a configuration value that should be in a set of
1043 * predefined strings
1045 * @param cfg configuration to inspect
1046 * @param section section of interest
1047 * @param option option of interest
1048 * @param choices NULL-terminated list of legal values
1049 * @param value will be set to an entry in the legal list,
1050 * or NULL if option is not specified and no default given
1051 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1054 GNUNET_CONFIGURATION_get_value_choice(
1055 const struct GNUNET_CONFIGURATION_Handle *cfg,
1056 const char *section,
1058 const char *const *choices,
1061 struct ConfigEntry *e;
1064 if (NULL == (e = find_entry(cfg, section, option)))
1065 return GNUNET_SYSERR;
1066 for (i = 0; NULL != choices[i]; i++)
1067 if (0 == strcasecmp(choices[i], e->val))
1069 if (NULL == choices[i])
1071 LOG(GNUNET_ERROR_TYPE_ERROR,
1072 _("Configuration value '%s' for '%s'"
1073 " in section '%s' is not in set of legal choices\n"),
1077 return GNUNET_SYSERR;
1079 *value = choices[i];
1085 * Get crockford32-encoded fixed-size binary data from a configuration.
1087 * @param cfg configuration to access
1088 * @param section section to access
1089 * @param option option to access
1090 * @param buf where to store the decoded binary result
1091 * @param buf_size exact number of bytes to store in @a buf
1092 * @return #GNUNET_OK on success
1093 * #GNUNET_NO is the value does not exist
1094 * #GNUNET_SYSERR on decoding error
1097 GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg,
1098 const char *section,
1109 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &enc)))
1111 data_size = (strlen(enc) * 5) / 8;
1112 if (data_size != buf_size)
1115 return GNUNET_SYSERR;
1118 GNUNET_STRINGS_string_to_data(enc, strlen(enc), buf, buf_size))
1121 return GNUNET_SYSERR;
1129 * Test if we have a value for a particular option
1131 * @param cfg configuration to inspect
1132 * @param section section of interest
1133 * @param option option of interest
1134 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1137 GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg,
1138 const char *section,
1141 struct ConfigEntry *e;
1143 if ((NULL == (e = find_entry(cfg, section, option))) || (NULL == e->val))
1150 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1151 * where either in the "PATHS" section or the environtment "FOO" is
1152 * set to "DIRECTORY". We also support default expansion,
1153 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1154 * set in PATHS or the environment, and otherwise to "default". Note
1155 * that "default" itself can also be a $-expression, thus
1156 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1159 * @param cfg configuration to use for path expansion
1160 * @param orig string to $-expand (will be freed!)
1161 * @param depth recursion depth, used to detect recursive expansions
1162 * @return $-expanded string
1165 expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg,
1186 LOG(GNUNET_ERROR_TYPE_WARNING,
1188 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1192 LOG(GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1195 LOG(GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1219 LOG(GNUNET_ERROR_TYPE_WARNING,
1220 _("Missing closing `%s' in option `%s'\n"),
1233 def = strchr(orig, ':');
1238 if (('-' == *def) || ('=' == *def))
1240 def = GNUNET_strdup(def);
1248 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1251 if (orig[i] == '\0')
1257 erased_char = orig[i];
1258 erased_pos = &orig[i];
1260 post = &orig[i + 1];
1263 LOG(GNUNET_ERROR_TYPE_DEBUG,
1264 "Split into `%s' and `%s' with default %s\n",
1269 GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", start, &prefix))
1271 if (NULL == (env = getenv(start)))
1274 def = expand_dollar(cfg, def, depth + 1);
1279 start = GNUNET_strdup(start);
1281 *erased_pos = erased_char;
1282 LOG(GNUNET_ERROR_TYPE_WARNING,
1284 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1290 prefix = GNUNET_strdup(env);
1292 prefix = GNUNET_CONFIGURATION_expand_dollar(cfg, prefix);
1293 if ((erased_pos) && ('}' != erased_char))
1295 len = strlen(prefix) + 1;
1296 prefix = GNUNET_realloc(prefix, len + 1);
1297 prefix[len - 1] = erased_char;
1300 result = GNUNET_malloc(strlen(prefix) + strlen(post) + 1);
1301 strcpy(result, prefix);
1302 strcat(result, post);
1303 GNUNET_free_non_null(def);
1304 GNUNET_free(prefix);
1311 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1312 * where either in the "PATHS" section or the environtment "FOO" is
1313 * set to "DIRECTORY". We also support default expansion,
1314 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1315 * set in PATHS or the environment, and otherwise to "default". Note
1316 * that "default" itself can also be a $-expression, thus
1317 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1320 * @param cfg configuration to use for path expansion
1321 * @param orig string to $-expand (will be freed!). Note that multiple
1322 * $-expressions can be present in this string. They will all be
1324 * @return $-expanded string
1327 GNUNET_CONFIGURATION_expand_dollar(
1328 const struct GNUNET_CONFIGURATION_Handle *cfg,
1335 for (i = 0; '\0' != orig[i]; i++)
1339 dup = GNUNET_strdup(orig + i);
1340 dup = expand_dollar(cfg, dup, 0);
1341 len = strlen(dup) + 1;
1342 orig = GNUNET_realloc(orig, i + len);
1343 GNUNET_memcpy(orig + i, dup, len);
1351 * Get a configuration value that should be a string.
1353 * @param cfg configuration to inspect
1354 * @param section section of interest
1355 * @param option option of interest
1356 * @param value will be set to a freshly allocated configuration
1357 * value, or NULL if option is not specified
1358 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1361 GNUNET_CONFIGURATION_get_value_filename(
1362 const struct GNUNET_CONFIGURATION_Handle *cfg,
1363 const char *section,
1370 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &tmp))
1372 LOG(GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1374 return GNUNET_SYSERR;
1376 tmp = GNUNET_CONFIGURATION_expand_dollar(cfg, tmp);
1377 *value = GNUNET_STRINGS_filename_expand(tmp);
1380 return GNUNET_SYSERR;
1386 * Get a configuration value that should be in a set of
1389 * @param cfg configuration to inspect
1390 * @param section section of interest
1391 * @param option option of interest
1392 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1395 GNUNET_CONFIGURATION_get_value_yesno(
1396 const struct GNUNET_CONFIGURATION_Handle *cfg,
1397 const char *section,
1400 static const char *yesno[] = { "YES", "NO", NULL };
1405 GNUNET_CONFIGURATION_get_value_choice(cfg, section, option, yesno, &val);
1406 if (ret == GNUNET_SYSERR)
1408 if (val == yesno[0])
1415 * Iterate over the set of filenames stored in a configuration value.
1417 * @param cfg configuration to inspect
1418 * @param section section of interest
1419 * @param option option of interest
1420 * @param cb function to call on each filename
1421 * @param cb_cls closure for @a cb
1422 * @return number of filenames iterated over, -1 on error
1425 GNUNET_CONFIGURATION_iterate_value_filenames(
1426 const struct GNUNET_CONFIGURATION_Handle *cfg,
1427 const char *section,
1429 GNUNET_FileNameCallback cb,
1439 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &list))
1441 GNUNET_assert(list != NULL);
1446 while (pos[0] == ' ')
1448 if (strlen(pos) == 0)
1451 while ((end[0] != ' ') && (end[0] != '\0'))
1459 memmove(end, &end[1], strlen(&end[1]) + 1);
1462 /* illegal, but just keep it */
1466 /* illegal, but just ignore that there was a '/' */
1474 if (strlen(pos) > 0)
1477 if ((cb != NULL) && (GNUNET_OK != cb(cb_cls, pos)))
1479 ret = GNUNET_SYSERR;
1495 * @param value FIXME
1499 escape_name(const char *value)
1505 escaped = GNUNET_malloc(strlen(value) * 2 + 1);
1506 memset(escaped, 0, strlen(value) * 2 + 1);
1509 while (rpos[0] != '\0')
1533 * @param cls string we compare with (const char*)
1534 * @param fn filename we are currently looking at
1535 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1538 test_match(void *cls, const char *fn)
1540 const char *of = cls;
1542 return (0 == strcmp(of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1547 * Append a filename to a configuration value that
1548 * represents a list of filenames
1550 * @param cfg configuration to update
1551 * @param section section of interest
1552 * @param option option of interest
1553 * @param value filename to append
1554 * @return #GNUNET_OK on success,
1555 * #GNUNET_NO if the filename already in the list
1556 * #GNUNET_SYSERR on error
1559 GNUNET_CONFIGURATION_append_value_filename(
1560 struct GNUNET_CONFIGURATION_Handle *cfg,
1561 const char *section,
1569 if (GNUNET_SYSERR ==
1570 GNUNET_CONFIGURATION_iterate_value_filenames(cfg,
1575 return GNUNET_NO; /* already exists */
1577 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &old))
1578 old = GNUNET_strdup("");
1579 escaped = escape_name(value);
1580 nw = GNUNET_malloc(strlen(old) + strlen(escaped) + 2);
1582 if (strlen(old) > 0)
1584 strcat(nw, escaped);
1585 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, nw);
1588 GNUNET_free(escaped);
1594 * Remove a filename from a configuration value that
1595 * represents a list of filenames
1597 * @param cfg configuration to update
1598 * @param section section of interest
1599 * @param option option of interest
1600 * @param value filename to remove
1601 * @return #GNUNET_OK on success,
1602 * #GNUNET_NO if the filename is not in the list,
1603 * #GNUNET_SYSERR on error
1606 GNUNET_CONFIGURATION_remove_value_filename(
1607 struct GNUNET_CONFIGURATION_Handle *cfg,
1608 const char *section,
1619 GNUNET_CONFIGURATION_get_value_string(cfg, section, option, &list))
1621 match = escape_name(value);
1625 while (pos[0] == ' ')
1627 if (strlen(pos) == 0)
1630 while ((end[0] != ' ') && (end[0] != '\0'))
1642 /* illegal, but just keep it */
1646 /* illegal, but just ignore that there was a '/' */
1654 if (0 == strcmp(pos, match))
1657 memmove(pos, &end[1], strlen(&end[1]) + 1);
1665 GNUNET_CONFIGURATION_set_value_string(cfg, section, option, list);
1682 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1683 * file in a directory, we trigger parsing on those files that
1686 * @param cls the cfg
1687 * @param filename file to parse
1688 * @return #GNUNET_OK on success
1691 parse_configuration_file(void *cls, const char *filename)
1693 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1697 /* Examine file extension */
1698 ext = strrchr(filename, '.');
1699 if ((NULL == ext) || (0 != strcmp(ext, ".conf")))
1701 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1705 ret = GNUNET_CONFIGURATION_parse(cfg, filename);
1711 * Load default configuration. This function will parse the
1712 * defaults from the given defaults_d directory.
1714 * @param cfg configuration to update
1715 * @param defaults_d directory with the defaults
1716 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1719 GNUNET_CONFIGURATION_load_from(struct GNUNET_CONFIGURATION_Handle *cfg,
1720 const char *defaults_d)
1722 if (GNUNET_SYSERR ==
1723 GNUNET_DISK_directory_scan(defaults_d, &parse_configuration_file, cfg))
1724 return GNUNET_SYSERR; /* no configuration at all found */
1729 /* end of configuration.c */