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
44 * This is a linked list.
46 struct ConfigEntry *next;
54 * current, commited value
61 * @brief configuration section
66 * This is a linked list.
68 struct ConfigSection *next;
71 * entries in the section
73 struct ConfigEntry *entries;
83 * @brief configuration data
85 struct GNUNET_CONFIGURATION_Handle
88 * Configuration sections.
90 struct ConfigSection *sections;
93 * Modification indication since last save
94 * #GNUNET_NO if clean, #GNUNET_YES if dirty,
95 * #GNUNET_SYSERR on error (i.e. last save failed)
102 * Used for diffing a configuration object against
107 const struct GNUNET_CONFIGURATION_Handle *cfg_default;
109 struct GNUNET_CONFIGURATION_Handle *cfgDiff;
114 * Create a GNUNET_CONFIGURATION_Handle.
116 * @return fresh configuration object
118 struct GNUNET_CONFIGURATION_Handle *
119 GNUNET_CONFIGURATION_create ()
121 return GNUNET_new (struct GNUNET_CONFIGURATION_Handle);
126 * Destroy configuration object.
128 * @param cfg configuration to destroy
131 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
133 struct ConfigSection *sec;
135 while (NULL != (sec = cfg->sections))
136 GNUNET_CONFIGURATION_remove_section (cfg, sec->name);
142 * Parse a configuration file @a filename and run the function
143 * @a cb with the resulting configuration object. Then free the
144 * configuration object and return the status value from @a cb.
146 * @param filename configuration to parse, NULL for "default"
147 * @param cb function to run
148 * @param cb_cls closure for @a cb
149 * @return #GNUNET_SYSERR if parsing the configuration failed,
150 * otherwise return value from @a cb.
153 GNUNET_CONFIGURATION_parse_and_run (const char *filename,
154 GNUNET_CONFIGURATION_Callback cb,
157 struct GNUNET_CONFIGURATION_Handle *cfg;
160 cfg = GNUNET_CONFIGURATION_create ();
161 if (GNUNET_OK != GNUNET_CONFIGURATION_load (cfg, filename))
164 GNUNET_CONFIGURATION_destroy (cfg);
165 return GNUNET_SYSERR;
167 ret = cb (cb_cls, cfg);
168 GNUNET_CONFIGURATION_destroy (cfg);
174 * De-serializes configuration
176 * @param cfg configuration to update
177 * @param mem the memory block of serialized configuration
178 * @param size the size of the memory block
179 * @param basedir set to path from which we recursively load configuration
180 * from inlined configurations; NULL if not and raise warnings
181 * when we come across them
182 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
185 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
206 section = GNUNET_strdup ("");
210 while (r_bytes < size)
212 GNUNET_free_non_null (line_orig);
213 /* fgets-like behaviour on buffer */
214 to_read = size - r_bytes;
215 pos = memchr (&mem[r_bytes], '\n', to_read);
218 line_orig = GNUNET_strndup (&mem[r_bytes], line_size = to_read);
219 r_bytes += line_size;
224 GNUNET_strndup (&mem[r_bytes], line_size = (pos - &mem[r_bytes]));
225 r_bytes += line_size + 1;
228 /* increment line number */
230 /* tabs and '\r' are whitespace */
231 emptyline = GNUNET_YES;
232 for (i = 0; i < line_size; i++)
239 emptyline = GNUNET_NO;
241 /* ignore empty lines */
242 if (GNUNET_YES == emptyline)
245 /* remove tailing whitespace */
246 for (i = line_size - 1; (i >= 1) && (isspace ((unsigned char) line[i]));
250 /* remove leading whitespace */
251 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
254 /* ignore comments */
255 if (('#' == line[0]) || ('%' == line[0]))
258 /* handle special "@INLINE@" directive */
259 if (0 == strncasecmp (line, "@INLINE@ ", strlen ("@INLINE@ ")))
262 value = &line[strlen ("@INLINE@ ")];
267 GNUNET_asprintf (&fn, "%s/%s", basedir, value);
268 if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, fn))
271 ret = GNUNET_SYSERR; /* failed to parse included config */
278 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Ignoring parsing @INLINE@ configurations, not allowed!\n");
285 if (('[' == line[0]) && (']' == line[line_size - 1]))
288 line[line_size - 1] = '\0';
290 GNUNET_free (section);
291 section = GNUNET_strdup (value);
294 if (NULL != (eq = strchr (line, '=')))
297 tag = GNUNET_strndup (line, eq - line);
298 /* remove tailing whitespace */
299 for (i = strlen (tag) - 1; (i >= 1) && (isspace ((unsigned char) tag[i]));
303 /* Strip whitespace */
305 while (isspace ((unsigned char) value[0]))
307 for (i = strlen (value) - 1;
308 (i >= 1) && (isspace ((unsigned char) value[i]));
314 if (('"' == value[0]) && ('"' == value[strlen (value) - 1]))
316 value[strlen (value) - 1] = '\0';
319 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, &value[i]);
324 LOG (GNUNET_ERROR_TYPE_WARNING,
325 _ ("Syntax error while deserializing in line %u\n"),
330 GNUNET_free_non_null (line_orig);
331 GNUNET_free (section);
332 GNUNET_assert ((GNUNET_OK != ret) || (r_bytes == size));
338 * Parse a configuration file, add all of the options in the
339 * file to the configuration environment.
341 * @param cfg configuration to update
342 * @param filename name of the configuration file
343 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
346 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
347 const char *filename)
358 fn = GNUNET_STRINGS_filename_expand (filename);
359 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to parse config file `%s'\n", fn);
361 return GNUNET_SYSERR;
362 dirty = cfg->dirty; /* back up value! */
364 GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
366 LOG (GNUNET_ERROR_TYPE_WARNING,
367 "Error while determining the file size of `%s'\n",
370 return GNUNET_SYSERR;
374 GNUNET_break (0); /* File size is more than the heap size */
376 return GNUNET_SYSERR;
379 mem = GNUNET_malloc (fs);
380 sret = GNUNET_DISK_fn_read (fn, mem, fs);
381 if ((sret < 0) || (fs != (size_t) sret))
383 LOG (GNUNET_ERROR_TYPE_WARNING, _ ("Error while reading file `%s'\n"), fn);
386 return GNUNET_SYSERR;
388 LOG (GNUNET_ERROR_TYPE_DEBUG, "Deserializing contents of file `%s'\n", fn);
389 endsep = strrchr (fn, (int) '/');
392 ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, fn);
395 /* restore dirty flag - anything we set in the meantime
403 * Test if there are configuration options that were
404 * changed since the last save.
406 * @param cfg configuration to inspect
407 * @return #GNUNET_NO if clean, #GNUNET_YES if dirty, #GNUNET_SYSERR on error (i.e. last save failed)
410 GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
417 * Serializes the given configuration.
419 * @param cfg configuration to serialize
420 * @param size will be set to the size of the serialized memory block
421 * @return the memory block where the serialized configuration is
422 * present. This memory should be freed by the caller
425 GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
428 struct ConfigSection *sec;
429 struct ConfigEntry *ent;
437 /* Pass1 : calculate the buffer size required */
439 for (sec = cfg->sections; NULL != sec; sec = sec->next)
441 /* For each section we need to add 3 charaters: {'[',']','\n'} */
442 m_size += strlen (sec->name) + 3;
443 for (ent = sec->entries; NULL != ent; ent = ent->next)
445 if (NULL != ent->val)
447 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
449 while (NULL != (pos = strstr (pos, "\n")))
454 /* For each key = value pair we need to add 4 characters (2
455 spaces and 1 equal-to character and 1 new line) */
456 m_size += strlen (ent->key) + strlen (ent->val) + 4;
459 /* A new line after section end */
463 /* Pass2: Allocate memory and write the configuration to it */
464 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,
1190 LOG (GNUNET_ERROR_TYPE_WARNING,
1192 "Recursive expansion suspected, aborting $-expansion for term `%s'\n"),
1196 LOG (GNUNET_ERROR_TYPE_DEBUG, "Asked to $-expand %s\n", orig);
1199 LOG (GNUNET_ERROR_TYPE_DEBUG, "Doesn't start with $ - not expanding\n");
1223 LOG (GNUNET_ERROR_TYPE_WARNING,
1224 _ ("Missing closing `%s' in option `%s'\n"),
1237 def = strchr (orig, ':');
1242 if (('-' == *def) || ('=' == *def))
1244 def = GNUNET_strdup (def);
1254 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0') &&
1257 if (orig[i] == '\0')
1263 erased_char = orig[i];
1264 erased_pos = &orig[i];
1266 post = &orig[i + 1];
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Split into `%s' and `%s' with default %s\n",
1275 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS", start, &prefix))
1277 if (NULL == (env = getenv (start)))
1280 def = expand_dollar (cfg, def, depth + 1);
1285 start = GNUNET_strdup (start);
1287 *erased_pos = erased_char;
1288 LOG (GNUNET_ERROR_TYPE_WARNING,
1290 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n"),
1293 GNUNET_free (start);
1296 prefix = GNUNET_strdup (env);
1298 prefix = GNUNET_CONFIGURATION_expand_dollar (cfg, prefix);
1299 if ((erased_pos) && ('}' != erased_char))
1301 len = strlen (prefix) + 1;
1302 prefix = GNUNET_realloc (prefix, len + 1);
1303 prefix[len - 1] = erased_char;
1306 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
1307 strcpy (result, prefix);
1308 strcat (result, post);
1309 GNUNET_free_non_null (def);
1310 GNUNET_free (prefix);
1317 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
1318 * where either in the "PATHS" section or the environtment "FOO" is
1319 * set to "DIRECTORY". We also support default expansion,
1320 * i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is
1321 * set in PATHS or the environment, and otherwise to "default". Note
1322 * that "default" itself can also be a $-expression, thus
1323 * "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined
1326 * @param cfg configuration to use for path expansion
1327 * @param orig string to $-expand (will be freed!). Note that multiple
1328 * $-expressions can be present in this string. They will all be
1330 * @return $-expanded string
1333 GNUNET_CONFIGURATION_expand_dollar (
1334 const struct GNUNET_CONFIGURATION_Handle *cfg,
1341 for (i = 0; '\0' != orig[i]; i++)
1345 dup = GNUNET_strdup (orig + i);
1346 dup = expand_dollar (cfg, dup, 0);
1347 len = strlen (dup) + 1;
1348 orig = GNUNET_realloc (orig, i + len);
1349 GNUNET_memcpy (orig + i, dup, len);
1357 * Get a configuration value that should be a string.
1359 * @param cfg configuration to inspect
1360 * @param section section of interest
1361 * @param option option of interest
1362 * @param value will be set to a freshly allocated configuration
1363 * value, or NULL if option is not specified
1364 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1367 GNUNET_CONFIGURATION_get_value_filename (
1368 const struct GNUNET_CONFIGURATION_Handle *cfg,
1369 const char *section,
1376 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &tmp))
1378 LOG (GNUNET_ERROR_TYPE_DEBUG, "Failed to retrieve filename\n");
1380 return GNUNET_SYSERR;
1382 tmp = GNUNET_CONFIGURATION_expand_dollar (cfg, tmp);
1383 *value = GNUNET_STRINGS_filename_expand (tmp);
1386 return GNUNET_SYSERR;
1392 * Get a configuration value that should be in a set of
1395 * @param cfg configuration to inspect
1396 * @param section section of interest
1397 * @param option option of interest
1398 * @return #GNUNET_YES, #GNUNET_NO or #GNUNET_SYSERR
1401 GNUNET_CONFIGURATION_get_value_yesno (
1402 const struct GNUNET_CONFIGURATION_Handle *cfg,
1403 const char *section,
1406 static const char *yesno[] = { "YES", "NO", NULL };
1411 GNUNET_CONFIGURATION_get_value_choice (cfg, section, option, yesno, &val);
1412 if (ret == GNUNET_SYSERR)
1414 if (val == yesno[0])
1421 * Iterate over the set of filenames stored in a configuration value.
1423 * @param cfg configuration to inspect
1424 * @param section section of interest
1425 * @param option option of interest
1426 * @param cb function to call on each filename
1427 * @param cb_cls closure for @a cb
1428 * @return number of filenames iterated over, -1 on error
1431 GNUNET_CONFIGURATION_iterate_value_filenames (
1432 const struct GNUNET_CONFIGURATION_Handle *cfg,
1433 const char *section,
1435 GNUNET_FileNameCallback cb,
1445 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1447 GNUNET_assert (list != NULL);
1452 while (pos[0] == ' ')
1454 if (strlen (pos) == 0)
1457 while ((end[0] != ' ') && (end[0] != '\0'))
1465 memmove (end, &end[1], strlen (&end[1]) + 1);
1468 /* illegal, but just keep it */
1472 /* illegal, but just ignore that there was a '/' */
1480 if (strlen (pos) > 0)
1483 if ((cb != NULL) && (GNUNET_OK != cb (cb_cls, pos)))
1485 ret = GNUNET_SYSERR;
1501 * @param value FIXME
1505 escape_name (const char *value)
1511 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
1512 memset (escaped, 0, strlen (value) * 2 + 1);
1515 while (rpos[0] != '\0')
1539 * @param cls string we compare with (const char*)
1540 * @param fn filename we are currently looking at
1541 * @return #GNUNET_OK if the names do not match, #GNUNET_SYSERR if they do
1544 test_match (void *cls, const char *fn)
1546 const char *of = cls;
1548 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
1553 * Append a filename to a configuration value that
1554 * represents a list of filenames
1556 * @param cfg configuration to update
1557 * @param section section of interest
1558 * @param option option of interest
1559 * @param value filename to append
1560 * @return #GNUNET_OK on success,
1561 * #GNUNET_NO if the filename already in the list
1562 * #GNUNET_SYSERR on error
1565 GNUNET_CONFIGURATION_append_value_filename (
1566 struct GNUNET_CONFIGURATION_Handle *cfg,
1567 const char *section,
1575 if (GNUNET_SYSERR ==
1576 GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
1581 return GNUNET_NO; /* already exists */
1583 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
1584 old = GNUNET_strdup ("");
1585 escaped = escape_name (value);
1586 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
1588 if (strlen (old) > 0)
1590 strcat (nw, escaped);
1591 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
1594 GNUNET_free (escaped);
1600 * Remove a filename from a configuration value that
1601 * represents a list of filenames
1603 * @param cfg configuration to update
1604 * @param section section of interest
1605 * @param option option of interest
1606 * @param value filename to remove
1607 * @return #GNUNET_OK on success,
1608 * #GNUNET_NO if the filename is not in the list,
1609 * #GNUNET_SYSERR on error
1612 GNUNET_CONFIGURATION_remove_value_filename (
1613 struct GNUNET_CONFIGURATION_Handle *cfg,
1614 const char *section,
1625 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
1627 match = escape_name (value);
1631 while (pos[0] == ' ')
1633 if (strlen (pos) == 0)
1636 while ((end[0] != ' ') && (end[0] != '\0'))
1648 /* illegal, but just keep it */
1652 /* illegal, but just ignore that there was a '/' */
1660 if (0 == strcmp (pos, match))
1663 memmove (pos, &end[1], strlen (&end[1]) + 1);
1671 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, list);
1673 GNUNET_free (match);
1682 GNUNET_free (match);
1688 * Wrapper around #GNUNET_CONFIGURATION_parse. Called on each
1689 * file in a directory, we trigger parsing on those files that
1692 * @param cls the cfg
1693 * @param filename file to parse
1694 * @return #GNUNET_OK on success
1697 parse_configuration_file (void *cls, const char *filename)
1699 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
1703 /* Examine file extension */
1704 ext = strrchr (filename, '.');
1705 if ((NULL == ext) || (0 != strcmp (ext, ".conf")))
1707 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Skipping file `%s'\n", filename);
1711 ret = GNUNET_CONFIGURATION_parse (cfg, filename);
1717 * Load default configuration. This function will parse the
1718 * defaults from the given defaults_d directory.
1720 * @param cfg configuration to update
1721 * @param defaults_d directory with the defaults
1722 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
1725 GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
1726 const char *defaults_d)
1728 if (GNUNET_SYSERR ==
1729 GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
1730 return GNUNET_SYSERR; /* no configuration at all found */
1735 /* end of configuration.c */