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
45 * This is a linked list.
47 struct ConfigEntry *next;
55 * current, commited value
62 * @brief configuration section
67 * This is a linked list.
69 struct ConfigSection *next;
72 * entries in the section
74 struct ConfigEntry *entries;
84 * @brief configuration data
86 struct GNUNET_CONFIGURATION_Handle
89 * Configuration sections.
91 struct ConfigSection *sections;
94 * Modification indication since last save
95 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
96 * #GNUNET_SYSERR on error (i.e. last save failed)
103 * Used for diffing a configuration object against
108 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
110 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_new (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;
136 while (NULL != (sec = cfg->sections))
137 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
143 * Parse a configuration file @a filename and run the function
144 * @a cb with the resulting configuration object. Then free the
145 * configuration object and return the status value from @a cb.
147 * @param filename configuration to parse, NULL for "default"
148 * @param cb function to run
149 * @param cb_cls closure for @a cb
150 * @return #GNUNET_SYSERR if parsing the configuration failed,
151 * otherwise return value from @a cb.
154 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
155 GNUNET_CONFIGURATION_Callback cb,
158 struct GNUNET_CONFIGURATION_Handle *cfg;
161 cfg = GNUNET_CONFIGURATION_create ();
162 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, filename))
165 GNUNET_CONFIGURATION_destroy (cfg);
166 return GNUNET_SYSERR;
168 ret = cb (cb_cls, cfg);
169 GNUNET_CONFIGURATION_destroy (cfg);
175 * De-serializes configuration
177 * @param cfg configuration to update
178 * @param mem the memory block of serialized configuration
179 * @param size the size of the memory block
180 * @param basedir set to path from which we recursively load configuration
181 * from inlined configurations; NULL if not and raise warnings
182 * when we come across them
183 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
186 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
207 section = GNUNET_strdup ("");
211 while (r_bytes < size)
213 GNUNET_free_non_null (line_orig);
214 /* fgets-like behaviour on buffer */
215 to_read = size - r_bytes;
216 pos = memchr (&mem[r_bytes], '\n', to_read);
219 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
220 r_bytes += line_size;
225 GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
226 r_bytes += line_size + 1;
229 /* increment line number */
231 /* tabs and '\r' are whitespace */
232 emptyline = GNUNET_YES;
233 for (i = 0; i < line_size; i++)
240 emptyline = GNUNET_NO;
242 /* ignore empty lines */
243 if (GNUNET_YES == emptyline)
246 /* remove tailing whitespace */
247 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));
251 /* remove leading whitespace */
252 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
255 /* ignore comments */
256 if (('#' == line[0]) || ('%' == line[0]))
259 /* handle special "@INLINE@" directive */
260 if (0 == strncasecmp (line, "@INLINE@ ", strlen ("@INLINE@ ")))
263 value = &line[strlen ("@INLINE@ ")];
268 GNUNET_asprintf (&fn, "%s/%s", basedir, value);
269 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, fn))
272 ret = GNUNET_SYSERR; /* failed to parse included config */
279 LOG (GNUNET_ERROR_TYPE_DEBUG,
280 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
286 if (('[' == line[0]) && (']' == line[line_size - 1]))
289 line[line_size - 1] = '\0';
291 GNUNET_free (section);
292 section = GNUNET_strdup (value);
295 if (NULL != (eq = strchr (line, '=')))
298 tag = GNUNET_strndup (line, eq - line);
299 /* remove tailing whitespace */
300 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));
304 /* Strip whitespace */
306 while (isspace ((unsigned char) value[0]))
308 for (i = strlen (value) - 1;
309 (i >= 1) && (isspace ((unsigned char) value[i]));
315 if (('"' == value[0]) && ('"' == value[strlen (value) - 1]))
317 value[strlen (value) - 1] = '\0';
320 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
325 LOG (GNUNET_ERROR_TYPE_WARNING,
326 _ ("Syntax error while deserializing in line %u\n"),
331 GNUNET_free_non_null (line_orig);
332 GNUNET_free (section);
333 GNUNET_assert ((GNUNET_OK != ret) || (r_bytes == size));
339 * Parse a configuration file, add all of the options in the
340 * file to the configuration environment.
342 * @param cfg configuration to update
343 * @param filename name of the configuration file
344 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
347 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
348 const char *filename)
359 fn = GNUNET_STRINGS_filename_expand (filename);
360 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
362 return GNUNET_SYSERR;
363 dirty = cfg->dirty; /* back up value! */
365 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
367 LOG (GNUNET_ERROR_TYPE_WARNING,
368 "Error while determining the file size of `%s'\n",
371 return GNUNET_SYSERR;
375 GNUNET_break (0); /* File size is more than the heap size */
377 return GNUNET_SYSERR;
380 mem = GNUNET_malloc (fs);
381 sret = GNUNET_DISK_fn_read (fn, mem, fs);
382 if ((sret < 0) || (fs != (size_t) sret))
384 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Error while reading file `%s'\n"), fn);
387 return GNUNET_SYSERR;
389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
390 endsep = strrchr (fn, (int) '/');
393 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, fn);
396 /* restore dirty flag - anything we set in the meantime
404 * Test if there are configuration options that were
405 * changed since the last save.
407 * @param cfg configuration to inspect
408 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
411 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
418 * Serializes the given configuration.
420 * @param cfg configuration to serialize
421 * @param size will be set to the size of the serialized memory block
422 * @return the memory block where the serialized configuration is
423 * present. This memory should be freed by the caller
426 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
429 struct ConfigSection *sec;
430 struct ConfigEntry *ent;
439 /* Pass1 : calculate the buffer size required */
441 for (sec = cfg->sections; NULL != sec; sec = sec->next)
443 /* For each section we need to add 3 charaters: {'[',']','\n'} */
444 m_size += strlen (sec->name) + 3;
445 for (ent = sec->entries; NULL != ent; ent = ent->next)
447 if (NULL != ent->val)
449 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
451 while (NULL != (pos = strstr (pos, "\n")))
456 /* For each key = value pair we need to add 4 characters (2
457 spaces and 1 equal-to character and 1 new line) */
458 m_size += strlen (ent->key) + strlen (ent->val) + 4;
461 /* A new line after section end */
465 /* Pass2: Allocate memory and write the configuration to it */
466 mem = GNUNET_malloc (m_size);
472 len = GNUNET_asprintf (&cbuf, "[%s]\n", sec->name);
473 GNUNET_assert (0 < len);
474 GNUNET_memcpy (mem + c_size, cbuf, len);
477 for (ent = sec->entries; NULL != ent; ent = ent->next)
479 if (NULL != ent->val)
481 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
482 strcpy (val, ent->val);
483 while (NULL != (pos = strstr (val, "\n")))
485 memmove (&pos[2], &pos[1], strlen (&pos[1]));
489 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
491 GNUNET_memcpy (mem + c_size, cbuf, len);
496 GNUNET_memcpy (mem + c_size, "\n", 1);
500 GNUNET_assert (c_size == m_size);
507 * Write configuration file.
509 * @param cfg configuration to write
510 * @param filename where to write the configuration
511 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
514 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
515 const char *filename)
522 fn = GNUNET_STRINGS_filename_expand (filename);
524 return GNUNET_SYSERR;
525 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (fn))
528 return GNUNET_SYSERR;
530 cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
531 sret = GNUNET_DISK_fn_write (fn,
534 GNUNET_DISK_PERM_USER_READ |
535 GNUNET_DISK_PERM_USER_WRITE |
536 GNUNET_DISK_PERM_GROUP_READ |
537 GNUNET_DISK_PERM_GROUP_WRITE);
538 if ((sret < 0) || (size != (size_t) sret))
541 GNUNET_free (cfg_buf);
542 LOG (GNUNET_ERROR_TYPE_WARNING,
543 "Writing configuration to file `%s' failed\n",
545 cfg->dirty = GNUNET_SYSERR; /* last write failed */
546 return GNUNET_SYSERR;
549 GNUNET_free (cfg_buf);
550 cfg->dirty = GNUNET_NO; /* last write succeeded */
556 * Iterate over all options in the configuration.
558 * @param cfg configuration to inspect
559 * @param iter function to call on each option
560 * @param iter_cls closure for @a iter
563 GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
564 GNUNET_CONFIGURATION_Iterator iter,
567 struct ConfigSection *spos;
568 struct ConfigEntry *epos;
570 for (spos = cfg->sections; NULL != spos; spos = spos->next)
571 for (epos = spos->entries; NULL != epos; epos = epos->next)
572 if (NULL != epos->val)
573 iter (iter_cls, spos->name, epos->key, epos->val);
578 * Iterate over values of a section in the configuration.
580 * @param cfg configuration to inspect
581 * @param section the section
582 * @param iter function to call on each option
583 * @param iter_cls closure for @a iter
586 GNUNET_CONFIGURATION_iterate_section_values (
587 const struct GNUNET_CONFIGURATION_Handle *cfg,
589 GNUNET_CONFIGURATION_Iterator iter,
592 struct ConfigSection *spos;
593 struct ConfigEntry *epos;
595 spos = cfg->sections;
596 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
600 for (epos = spos->entries; NULL != epos; epos = epos->next)
601 if (NULL != epos->val)
602 iter (iter_cls, spos->name, epos->key, epos->val);
607 * Iterate over all sections in the configuration.
609 * @param cfg configuration to inspect
610 * @param iter function to call on each section
611 * @param iter_cls closure for @a iter
614 GNUNET_CONFIGURATION_iterate_sections (
615 const struct GNUNET_CONFIGURATION_Handle *cfg,
616 GNUNET_CONFIGURATION_Section_Iterator iter,
619 struct ConfigSection *spos;
620 struct ConfigSection *next;
622 next = cfg->sections;
627 iter (iter_cls, spos->name);
633 * Remove the given section and all options in it.
635 * @param cfg configuration to inspect
636 * @param section name of the section to remove
639 GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg,
642 struct ConfigSection *spos;
643 struct ConfigSection *prev;
644 struct ConfigEntry *ent;
647 spos = cfg->sections;
650 if (0 == strcasecmp (section, spos->name))
653 cfg->sections = spos->next;
655 prev->next = spos->next;
656 while (NULL != (ent = spos->entries))
658 spos->entries = ent->next;
659 GNUNET_free (ent->key);
660 GNUNET_free_non_null (ent->val);
662 cfg->dirty = GNUNET_YES;
664 GNUNET_free (spos->name);
675 * Copy a configuration value to the given target configuration.
676 * Overwrites existing entries.
678 * @param cls the destination configuration (`struct GNUNET_CONFIGURATION_Handle *`)
679 * @param section section for the value
680 * @param option option name of the value
681 * @param value value to copy
684 copy_entry (void *cls,
689 struct GNUNET_CONFIGURATION_Handle *dst = cls;
691 GNUNET_CONFIGURATION_set_value_string (dst, section, option, value);
696 * Duplicate an existing configuration object.
698 * @param cfg configuration to duplicate
699 * @return duplicate configuration
701 struct GNUNET_CONFIGURATION_Handle *
702 GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
704 struct GNUNET_CONFIGURATION_Handle *ret;
706 ret = GNUNET_CONFIGURATION_create ();
707 GNUNET_CONFIGURATION_iterate (cfg, ©_entry, ret);
713 * Find a section entry from a configuration.
715 * @param cfg configuration to search in
716 * @param section name of the section to look for
717 * @return matching entry, NULL if not found
719 static struct ConfigSection *
720 find_section (const struct GNUNET_CONFIGURATION_Handle *cfg,
723 struct ConfigSection *pos;
726 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
733 * Find an entry from a configuration.
735 * @param cfg handle to the configuration
736 * @param section section the option is in
737 * @param key the option
738 * @return matching entry, NULL if not found
740 static struct ConfigEntry *
741 find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg,
745 struct ConfigSection *sec;
746 struct ConfigEntry *pos;
748 if (NULL == (sec = find_section (cfg, section)))
751 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
758 * A callback function, compares entries from two configurations
759 * (default against a new configuration) and write the diffs in a
760 * diff-configuration object (the callback object).
762 * @param cls the diff configuration (`struct DiffHandle *`)
763 * @param section section for the value (of the default conf.)
764 * @param option option name of the value (of the default conf.)
765 * @param value value to copy (of the default conf.)
768 compare_entries (void *cls,
773 struct DiffHandle *dh = cls;
774 struct ConfigEntry *entNew;
776 entNew = find_entry (dh->cfg_default, section, option);
777 if ((NULL != entNew) && (NULL != entNew->val) &&
778 (0 == strcmp (entNew->val, value)))
780 GNUNET_CONFIGURATION_set_value_string (dh->cfgDiff, section, option, value);
785 * Compute configuration with only entries that have been changed
787 * @param cfg_default original configuration
788 * @param cfg_new new configuration
789 * @return configuration with only the differences, never NULL
791 struct GNUNET_CONFIGURATION_Handle *
792 GNUNET_CONFIGURATION_get_diff (
793 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
794 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
796 struct DiffHandle diffHandle;
798 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create ();
799 diffHandle.cfg_default = cfg_default;
800 GNUNET_CONFIGURATION_iterate (cfg_new, &compare_entries, &diffHandle);
801 return diffHandle.cfgDiff;
806 * Write only configuration entries that have been changed to configuration file
808 * @param cfg_default default configuration
809 * @param cfg_new new configuration
810 * @param filename where to write the configuration diff between default and new
811 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
814 GNUNET_CONFIGURATION_write_diffs (
815 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
816 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
817 const char *filename)
820 struct GNUNET_CONFIGURATION_Handle *diff;
822 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
823 ret = GNUNET_CONFIGURATION_write (diff, filename);
824 GNUNET_CONFIGURATION_destroy (diff);
830 * Set a configuration value that should be a string.
832 * @param cfg configuration to update
833 * @param section section of interest
834 * @param option option of interest
835 * @param value value to set
838 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
843 struct ConfigSection *sec;
844 struct ConfigEntry *e;
847 e = find_entry (cfg, section, option);
852 GNUNET_free_non_null (e->val);
857 nv = GNUNET_strdup (value);
858 GNUNET_free_non_null (e->val);
863 sec = find_section (cfg, section);
866 sec = GNUNET_new (struct ConfigSection);
867 sec->name = GNUNET_strdup (section);
868 sec->next = cfg->sections;
871 e = GNUNET_new (struct ConfigEntry);
872 e->key = GNUNET_strdup (option);
873 e->val = GNUNET_strdup (value);
874 e->next = sec->entries;
880 * Set a configuration value that should be a number.
882 * @param cfg configuration to update
883 * @param section section of interest
884 * @param option option of interest
885 * @param number value to set
888 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
891 unsigned long long number)
895 GNUNET_snprintf (s, 64, "%llu", number);
896 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
901 * Get a configuration value that should be a number.
903 * @param cfg configuration to inspect
904 * @param section section of interest
905 * @param option option of interest
906 * @param number where to store the numeric value of the option
907 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
910 GNUNET_CONFIGURATION_get_value_number (
911 const struct GNUNET_CONFIGURATION_Handle *cfg,
914 unsigned long long *number)
916 struct ConfigEntry *e;
919 if (NULL == (e = find_entry (cfg, section, option)))
920 return GNUNET_SYSERR;
922 return GNUNET_SYSERR;
923 if (1 != sscanf (e->val, "%llu%1s", number, dummy))
924 return GNUNET_SYSERR;
930 * Get a configuration value that should be a floating point number.
932 * @param cfg configuration to inspect
933 * @param section section of interest
934 * @param option option of interest
935 * @param number where to store the floating value of the option
936 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
939 GNUNET_CONFIGURATION_get_value_float (
940 const struct GNUNET_CONFIGURATION_Handle *cfg,
945 struct ConfigEntry *e;
948 if (NULL == (e = find_entry (cfg, section, option)))
949 return GNUNET_SYSERR;
951 return GNUNET_SYSERR;
952 if (1 != sscanf (e->val, "%f%1s", number, dummy))
953 return GNUNET_SYSERR;
959 * Get a configuration value that should be a relative time.
961 * @param cfg configuration to inspect
962 * @param section section of interest
963 * @param option option of interest
964 * @param time set to the time value stored in the configuration
965 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
968 GNUNET_CONFIGURATION_get_value_time (
969 const struct GNUNET_CONFIGURATION_Handle *cfg,
972 struct GNUNET_TIME_Relative *time)
974 struct ConfigEntry *e;
977 if (NULL == (e = find_entry (cfg, section, option)))
978 return GNUNET_SYSERR;
980 return GNUNET_SYSERR;
981 ret = GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
982 if (GNUNET_OK != ret)
983 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
986 _ ("Not a valid relative time specification"));
992 * Get a configuration value that should be a size in bytes.
994 * @param cfg configuration to inspect
995 * @param section section of interest
996 * @param option option of interest
997 * @param size set to the size in bytes as stored in the configuration
998 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1001 GNUNET_CONFIGURATION_get_value_size (
1002 const struct GNUNET_CONFIGURATION_Handle *cfg,
1003 const char *section,
1005 unsigned long long *size)
1007 struct ConfigEntry *e;
1009 if (NULL == (e = find_entry (cfg, section, option)))
1010 return GNUNET_SYSERR;
1012 return GNUNET_SYSERR;
1013 return GNUNET_STRINGS_fancy_size_to_bytes (e->val, size);
1018 * Get a configuration value that should be a string.
1020 * @param cfg configuration to inspect
1021 * @param section section of interest
1022 * @param option option of interest
1023 * @param value will be set to a freshly allocated configuration
1024 * value, or NULL if option is not specified
1025 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1028 GNUNET_CONFIGURATION_get_value_string (
1029 const struct GNUNET_CONFIGURATION_Handle *cfg,
1030 const char *section,
1034 struct ConfigEntry *e;
1036 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1039 return GNUNET_SYSERR;
1041 *value = GNUNET_strdup (e->val);
1047 * Get a configuration value that should be in a set of
1048 * predefined strings
1050 * @param cfg configuration to inspect
1051 * @param section section of interest
1052 * @param option option of interest
1053 * @param choices NULL-terminated list of legal values
1054 * @param value will be set to an entry in the legal list,
1055 * or NULL if option is not specified and no default given
1056 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1059 GNUNET_CONFIGURATION_get_value_choice (
1060 const struct GNUNET_CONFIGURATION_Handle *cfg,
1061 const char *section,
1063 const char *const *choices,
1066 struct ConfigEntry *e;
1069 if (NULL == (e = find_entry (cfg, section, option)))
1070 return GNUNET_SYSERR;
1071 for (i = 0; NULL != choices[i]; i++)
1072 if (0 == strcasecmp (choices[i], e->val))
1074 if (NULL == choices[i])
1076 LOG (GNUNET_ERROR_TYPE_ERROR,
1077 _ ("Configuration value '%s' for '%s'"
1078 " in section '%s' is not in set of legal choices\n"),
1082 return GNUNET_SYSERR;
1084 *value = choices[i];
1090 * Get crockford32-encoded fixed-size binary data from a configuration.
1092 * @param cfg configuration to access
1093 * @param section section to access
1094 * @param option option to access
1095 * @param buf where to store the decoded binary result
1096 * @param buf_size exact number of bytes to store in @a buf
1097 * @return #GNUNET_OK on success
1098 * #GNUNET_NO is the value does not exist
1099 * #GNUNET_SYSERR on decoding error
1102 GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg,
1103 const char *section,
1114 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &enc)))
1116 data_size = (strlen (enc) * 5) / 8;
1117 if (data_size != buf_size)
1120 return GNUNET_SYSERR;
1123 GNUNET_STRINGS_string_to_data (enc, strlen (enc), buf, buf_size))
1126 return GNUNET_SYSERR;
1134 * Test if we have a value for a particular option
1136 * @param cfg configuration to inspect
1137 * @param section section of interest
1138 * @param option option of interest
1139 * @return #GNUNET_YES if so, #GNUNET_NO if not.
1142 GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg,
1143 const char *section,
1146 struct ConfigEntry *e;
1148 if ((NULL == (e = find_entry (cfg, section, option))) || (NULL == e->val))
1155 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1156 * where either in the "PATHS" section or the environtment "FOO" is
1157 * set to "DIRECTORY". We also support default expansion,
1158 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1159 * set in PATHS or the environment, and otherwise to "default". Note
1160 * that "default" itself can also be a $-expression, thus
1161 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1164 * @param cfg configuration to use for path expansion
1165 * @param orig string to $-expand (will be freed!)
1166 * @param depth recursion depth, used to detect recursive expansions
1167 * @return $-expanded string
1170 expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg,
1191 LOG (GNUNET_ERROR_TYPE_WARNING,
1193 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1197 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1200 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1222 LOG (GNUNET_ERROR_TYPE_WARNING,
1223 _ ("Missing closing `%s' in option `%s'\n"),
1235 def = strchr (orig, ':');
1240 if (('-' == *def) || ('=' == *def))
1242 def = GNUNET_strdup (def);
1250 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1253 if (orig[i] == '\0')
1259 erased_char = orig[i];
1260 erased_pos = &orig[i];
1262 post = &orig[i + 1];
1265 LOG (GNUNET_ERROR_TYPE_DEBUG,
1266 "Split into `%s' and `%s' with default %s\n",
1271 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", start, &prefix))
1273 if (NULL == (env = getenv (start)))
1276 def = expand_dollar (cfg, def, depth + 1);
1281 start = GNUNET_strdup (start);
1283 *erased_pos = erased_char;
1284 LOG (GNUNET_ERROR_TYPE_WARNING,
1286 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1289 GNUNET_free (start);
1292 prefix = GNUNET_strdup (env);
1294 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1295 if ((erased_pos) && ('}' != erased_char))
1297 len = strlen (prefix) + 1;
1298 prefix = GNUNET_realloc (prefix, len + 1);
1299 prefix[len - 1] = erased_char;
1302 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1303 strcpy (result, prefix);
1304 strcat (result, post);
1305 GNUNET_free_non_null (def);
1306 GNUNET_free (prefix);
1313 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1314 * where either in the "PATHS" section or the environtment "FOO" is
1315 * set to "DIRECTORY". We also support default expansion,
1316 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1317 * set in PATHS or the environment, and otherwise to "default". Note
1318 * that "default" itself can also be a $-expression, thus
1319 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1322 * @param cfg configuration to use for path expansion
1323 * @param orig string to $-expand (will be freed!). Note that multiple
1324 * $-expressions can be present in this string. They will all be
1326 * @return $-expanded string
1329 GNUNET_CONFIGURATION_expand_dollar (
1330 const struct GNUNET_CONFIGURATION_Handle *cfg,
1337 for (i = 0; '\0' != orig[i]; i++)
1341 dup = GNUNET_strdup (orig + i);
1342 dup = expand_dollar (cfg, dup, 0);
1343 len = strlen (dup) + 1;
1344 orig = GNUNET_realloc (orig, i + len);
1345 GNUNET_memcpy (orig + i, dup, len);
1353 * Get a configuration value that should be a string.
1355 * @param cfg configuration to inspect
1356 * @param section section of interest
1357 * @param option option of interest
1358 * @param value will be set to a freshly allocated configuration
1359 * value, or NULL if option is not specified
1360 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1363 GNUNET_CONFIGURATION_get_value_filename (
1364 const struct GNUNET_CONFIGURATION_Handle *cfg,
1365 const char *section,
1372 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1374 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1376 return GNUNET_SYSERR;
1378 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1379 *value = GNUNET_STRINGS_filename_expand (tmp);
1382 return GNUNET_SYSERR;
1388 * Get a configuration value that should be in a set of
1391 * @param cfg configuration to inspect
1392 * @param section section of interest
1393 * @param option option of interest
1394 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1397 GNUNET_CONFIGURATION_get_value_yesno (
1398 const struct GNUNET_CONFIGURATION_Handle *cfg,
1399 const char *section,
1402 static const char *yesno[] = {"YES", "NO", NULL};
1407 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1408 if (ret == GNUNET_SYSERR)
1410 if (val == yesno[0])
1417 * Iterate over the set of filenames stored in a configuration value.
1419 * @param cfg configuration to inspect
1420 * @param section section of interest
1421 * @param option option of interest
1422 * @param cb function to call on each filename
1423 * @param cb_cls closure for @a cb
1424 * @return number of filenames iterated over, -1 on error
1427 GNUNET_CONFIGURATION_iterate_value_filenames (
1428 const struct GNUNET_CONFIGURATION_Handle *cfg,
1429 const char *section,
1431 GNUNET_FileNameCallback cb,
1441 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1443 GNUNET_assert (list != NULL);
1448 while (pos[0] == ' ')
1450 if (strlen (pos) == 0)
1453 while ((end[0] != ' ') && (end[0] != '\0'))
1461 memmove (end, &end[1], strlen (&end[1]) + 1);
1463 /* 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')
1532 * @param cls string we compare with (const char*)
1533 * @param fn filename we are currently looking at
1534 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1537 test_match (void *cls, const char *fn)
1539 const char *of = cls;
1541 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1546 * Append a filename to a configuration value that
1547 * represents a list of filenames
1549 * @param cfg configuration to update
1550 * @param section section of interest
1551 * @param option option of interest
1552 * @param value filename to append
1553 * @return #GNUNET_OK on success,
1554 * #GNUNET_NO if the filename already in the list
1555 * #GNUNET_SYSERR on error
1558 GNUNET_CONFIGURATION_append_value_filename (
1559 struct GNUNET_CONFIGURATION_Handle *cfg,
1560 const char *section,
1568 if (GNUNET_SYSERR ==
1569 GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1574 return GNUNET_NO; /* already exists */
1576 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1577 old = GNUNET_strdup ("");
1578 escaped = escape_name (value);
1579 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1581 if (strlen (old) > 0)
1583 strcat (nw, escaped);
1584 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1587 GNUNET_free (escaped);
1593 * Remove a filename from a configuration value that
1594 * represents a list of filenames
1596 * @param cfg configuration to update
1597 * @param section section of interest
1598 * @param option option of interest
1599 * @param value filename to remove
1600 * @return #GNUNET_OK on success,
1601 * #GNUNET_NO if the filename is not in the list,
1602 * #GNUNET_SYSERR on error
1605 GNUNET_CONFIGURATION_remove_value_filename (
1606 struct GNUNET_CONFIGURATION_Handle *cfg,
1607 const char *section,
1618 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1620 match = escape_name (value);
1624 while (pos[0] == ' ')
1626 if (strlen (pos) == 0)
1629 while ((end[0] != ' ') && (end[0] != '\0'))
1640 /* illegal, but just keep it */
1643 /* illegal, but just ignore that there was a '/' */
1651 if (0 == strcmp (pos, match))
1654 memmove (pos, &end[1], strlen (&end[1]) + 1);
1662 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1664 GNUNET_free (match);
1673 GNUNET_free (match);
1679 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1680 * file in a directory, we trigger parsing on those files that
1683 * @param cls the cfg
1684 * @param filename file to parse
1685 * @return #GNUNET_OK on success
1688 parse_configuration_file (void *cls, const char *filename)
1690 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1694 /* Examine file extension */
1695 ext = strrchr (filename, '.');
1696 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1702 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1708 * Load default configuration. This function will parse the
1709 * defaults from the given defaults_d directory.
1711 * @param cfg configuration to update
1712 * @param defaults_d directory with the defaults
1713 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1716 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1717 const char *defaults_d)
1719 if (GNUNET_SYSERR ==
1720 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1721 return GNUNET_SYSERR; /* no configuration at all found */
1726 /* end of configuration.c */