2 This file is part of GNUnet.
3 (C) 2006, 2007, 2008, 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file src/util/configuration.c
23 * @brief configuration management
25 * @author Christian Grothoff
29 #include "gnunet_common.h"
30 #include "gnunet_configuration_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_disk_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_strings_lib.h"
37 * @brief configuration entry
43 * This is a linked list.
45 struct ConfigEntry *next;
53 * current, commited value
59 * @brief configuration section
64 * This is a linked list.
66 struct ConfigSection *next;
69 * entries in the section
71 struct ConfigEntry *entries;
80 * @brief configuration data
82 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 * Create a GNUNET_CONFIGURATION_Configuration.
101 struct GNUNET_CONFIGURATION_Handle *
102 GNUNET_CONFIGURATION_create ()
104 return GNUNET_malloc (sizeof (struct GNUNET_CONFIGURATION_Handle));
108 GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
110 struct ConfigSection *sec;
111 struct ConfigEntry *ent;
113 while (NULL != (sec = cfg->sections))
115 cfg->sections = sec->next;
116 while (NULL != (ent = sec->entries))
118 sec->entries = ent->next;
119 GNUNET_free (ent->key);
120 GNUNET_free_non_null (ent->val);
123 GNUNET_free (sec->name);
130 GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
131 const char *filename)
145 fn = GNUNET_STRINGS_filename_expand (filename);
146 dirty = cfg->dirty; /* back up value! */
147 if (NULL == (fp = FOPEN (fn, "r")))
149 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
151 return GNUNET_SYSERR;
155 section = GNUNET_strdup ("");
156 memset (line, 0, 256);
158 while (NULL != fgets (line, 255, fp))
161 for (i = 0; i < 255; i++)
164 if (line[0] == '\n' || line[0] == '#' || line[0] == '%' ||
168 for (i = 0; (i < 255 && line[i] != 0); i++)
169 if (line[i] != ' ' && line[i] != '\n' && line[i] != '\r')
173 /* remove tailing whitespace */
174 for (i = strlen (line) - 1; (i >= 0) && (isspace (line[i])); i--)
176 if (1 == sscanf (line, "@INLINE@ %191[^\n]", value))
179 if (0 != GNUNET_CONFIGURATION_parse (cfg, value))
180 ret = GNUNET_SYSERR; /* failed to parse included config */
182 else if (1 == sscanf (line, "[%99[^]]]", value))
185 GNUNET_free (section);
186 section = GNUNET_strdup (value);
188 else if (2 == sscanf (line, " %63[^= ] = %191[^\n]", tag, value))
192 i = strlen (value) - 1;
193 while ((i >= 0) && (isspace (value[i])))
200 while ((value[i] != '\0') && (value[i] != '"'))
210 GNUNET_CONFIGURATION_set_value_string (cfg,
211 section, tag, &value[i]);
213 else if (1 == sscanf (line, " %63[^= ] =[^\n]", tag))
216 GNUNET_CONFIGURATION_set_value_string (cfg, section, tag, "");
221 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
223 ("Syntax error in configuration file `%s' at line %u.\n"),
229 GNUNET_assert (0 == fclose (fp));
230 /* restore dirty flag - anything we set in the meantime
233 GNUNET_free (section);
238 GNUNET_CONFIGURATION_test_dirty (struct GNUNET_CONFIGURATION_Handle *cfg)
244 GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *data,
245 const char *filename)
247 struct ConfigSection *sec;
248 struct ConfigEntry *ent;
255 fn = GNUNET_STRINGS_filename_expand (filename);
256 GNUNET_DISK_directory_create_for_file (fn);
257 if (NULL == (fp = FOPEN (fn, "w")))
259 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fopen", fn);
261 return GNUNET_SYSERR;
265 sec = data->sections;
268 if (0 > fprintf (fp, "[%s]\n", sec->name))
276 if (ent->val != NULL)
278 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
279 strcpy (val, ent->val);
280 while (NULL != (pos = strstr (val, "\n")))
282 memmove (&pos[2], &pos[1], strlen (&pos[1]));
286 if (0 > fprintf (fp, "%s = %s\n", ent->key, val))
298 if (0 > fprintf (fp, "\n"))
306 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fprintf", filename);
307 GNUNET_assert (0 == fclose (fp));
310 data->dirty = GNUNET_SYSERR; /* last write failed */
311 return GNUNET_SYSERR;
313 data->dirty = GNUNET_NO; /* last write succeeded */
318 static struct ConfigSection *
319 findSection (struct GNUNET_CONFIGURATION_Handle *data, const char *section)
321 struct ConfigSection *pos;
323 pos = data->sections;
324 while ((pos != NULL) && (0 != strcasecmp (section, pos->name)))
330 static struct ConfigEntry *
331 findEntry (struct GNUNET_CONFIGURATION_Handle *data,
332 const char *section, const char *key)
334 struct ConfigSection *sec;
335 struct ConfigEntry *pos;
337 sec = findSection (data, section);
341 while ((pos != NULL) && (0 != strcasecmp (key, pos->key)))
347 GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle
350 const char *option, const char *value)
352 struct ConfigSection *sec;
353 struct ConfigEntry *e;
355 e = findEntry (data, section, option);
358 GNUNET_free_non_null (e->val);
359 e->val = GNUNET_strdup (value);
362 sec = findSection (data, section);
365 sec = GNUNET_malloc (sizeof (struct ConfigSection));
366 sec->name = GNUNET_strdup (section);
367 sec->next = data->sections;
368 data->sections = sec;
370 e = GNUNET_malloc (sizeof (struct ConfigEntry));
371 e->key = GNUNET_strdup (option);
372 e->val = GNUNET_strdup (value);
373 e->next = sec->entries;
378 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle
379 *cfg, const char *section,
381 unsigned long long number)
384 GNUNET_snprintf (s, 64, "%llu", number);
385 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
389 GNUNET_CONFIGURATION_get_value_number (struct GNUNET_CONFIGURATION_Handle
390 *cfg, const char *section,
392 unsigned long long *number)
394 struct ConfigEntry *e;
396 e = findEntry (cfg, section, option);
398 return GNUNET_SYSERR;
399 if (1 != SSCANF (e->val, "%llu", number))
400 return GNUNET_SYSERR;
405 GNUNET_CONFIGURATION_get_value_string (struct GNUNET_CONFIGURATION_Handle
406 *cfg, const char *section,
407 const char *option, char **value)
409 struct ConfigEntry *e;
411 e = findEntry (cfg, section, option);
412 if ((e == NULL) || (e->val == NULL))
415 return GNUNET_SYSERR;
417 *value = GNUNET_strdup (e->val);
422 GNUNET_CONFIGURATION_get_value_choice (struct GNUNET_CONFIGURATION_Handle
423 *cfg, const char *section,
425 const char **choices,
428 struct ConfigEntry *e;
431 e = findEntry (cfg, section, option);
433 return GNUNET_SYSERR;
435 while (choices[i] != NULL)
437 if (0 == strcasecmp (choices[i], e->val))
441 if (choices[i] == NULL)
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444 _("Configuration value '%s' for '%s'"
445 " in section '%s' is not in set of legal choices\n"),
446 e->val, option, section);
447 return GNUNET_SYSERR;
454 * Test if we have a value for a particular option
455 * @return GNUNET_YES if so, GNUNET_NO if not.
458 GNUNET_CONFIGURATION_have_value (struct GNUNET_CONFIGURATION_Handle *cfg,
459 const char *section, const char *option)
461 struct ConfigEntry *e;
462 if ((NULL == (e = findEntry (cfg, section, option))) || (e->val == NULL))
468 * Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR"
469 * where either in the "PATHS" section or the environtment
470 * "FOO" is set to "DIRECTORY".
472 * @param old string to $-expand (will be freed!)
473 * @return $-expanded string
476 GNUNET_CONFIGURATION_expand_dollar (struct GNUNET_CONFIGURATION_Handle *cfg,
488 while ((orig[i] != '/') && (orig[i] != '\\') && (orig[i] != '\0'))
499 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
503 if (NULL == (env = getenv (&orig[1])))
505 orig[i] = DIR_SEPARATOR;
508 prefix = GNUNET_strdup (env);
510 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 2);
511 strcpy (result, prefix);
512 if ((strlen (prefix) == 0) ||
513 ((prefix[strlen (prefix) - 1] != DIR_SEPARATOR) && (strlen (post) > 0)))
514 strcat (result, DIR_SEPARATOR_STR);
515 strcat (result, post);
516 GNUNET_free (prefix);
522 * Get a configuration value that should be a string.
523 * @param value will be set to a freshly allocated configuration
524 * value, or NULL if option is not specified
525 * @return GNUNET_OK on success, GNUNET_SYSERR on error
528 GNUNET_CONFIGURATION_get_value_filename (struct GNUNET_CONFIGURATION_Handle
529 *data, const char *section,
530 const char *option, char **value)
536 ret = GNUNET_CONFIGURATION_get_value_string (data, section, option, &tmp);
537 if (ret == GNUNET_SYSERR)
541 tmp = GNUNET_CONFIGURATION_expand_dollar (data, tmp);
542 *value = GNUNET_STRINGS_filename_expand (tmp);
553 * Get a configuration value that should be in a set of
554 * "GNUNET_YES" or "GNUNET_NO".
556 * @return GNUNET_YES, GNUNET_NO or GNUNET_SYSERR
559 GNUNET_CONFIGURATION_get_value_yesno (struct GNUNET_CONFIGURATION_Handle *cfg,
560 const char *section, const char *option)
562 static const char *yesno[] = { "YES", "NO", NULL };
566 ret = GNUNET_CONFIGURATION_get_value_choice (cfg,
567 section, option, yesno, &val);
568 if (ret == GNUNET_SYSERR)
577 * Iterate over the set of filenames stored in a configuration value.
579 * @return number of filenames iterated over, -1 on error
582 GNUNET_CONFIGURATION_iterate_value_filenames (struct
583 GNUNET_CONFIGURATION_Handle
584 *cfg, const char *section,
586 GNUNET_FileNameCallback cb,
596 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
598 GNUNET_assert (list != NULL);
603 while (pos[0] == ' ')
605 if (strlen (pos) == 0)
608 while ((end[0] != ' ') && (end[0] != '\0'))
616 memmove (end, &end[1], strlen (&end[1]) + 1);
618 /* illegal, but just keep it */
621 /* illegal, but just ignore that there was a '/' */
629 if (strlen (pos) > 0)
632 if ((cb != NULL) && (GNUNET_OK != cb (cls, pos)))
647 escape_name (const char *value)
653 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
654 memset (escaped, 0, strlen (value) * 2 + 1);
657 while (rpos[0] != '\0')
677 test_match (void *cls, const char *fn)
679 const char *of = cls;
680 return (0 == strcmp (of, fn)) ? GNUNET_SYSERR : GNUNET_OK;
684 * Append a filename to a configuration value that
685 * represents a list of filenames
687 * @param value filename to append
688 * @return GNUNET_OK on success,
689 * GNUNET_NO if the filename already in the list
690 * GNUNET_SYSERR on error
693 GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle
704 == GNUNET_CONFIGURATION_iterate_value_filenames (cfg,
709 return GNUNET_NO; /* already exists */
711 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
712 old = GNUNET_strdup ("");
713 escaped = escape_name (value);
714 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
717 strcat (nw, escaped);
718 GNUNET_CONFIGURATION_set_value_string (cfg, section, option, nw);
721 GNUNET_free (escaped);
727 * Remove a filename from a configuration value that
728 * represents a list of filenames
730 * @param value filename to remove
731 * @return GNUNET_OK on success,
732 * GNUNET_NO if the filename is not in the list,
733 * GNUNET_SYSERR on error
736 GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle
750 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &list))
752 match = escape_name (value);
757 while (pos[0] == ' ')
759 if (strlen (pos) == 0)
762 while ((end[0] != ' ') && (end[0] != '\0'))
773 /* illegal, but just keep it */
776 /* illegal, but just ignore that there was a '/' */
784 if (strlen (pos) > 0)
786 if (0 == strcmp (pos, match))
788 memmove (pos, &end[1], strlen (&end[1]) + 1);
791 pos[-1] = ' '; /* previously changed to "\0" */
792 GNUNET_CONFIGURATION_set_value_string (cfg,
793 section, option, list);
810 * Load configuration (starts with defaults, then loads
811 * system-specific configuration).
814 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
820 ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
822 return GNUNET_SYSERR;
824 GNUNET_asprintf (&baseconfig,
825 "%s%s%s", ipath, DIR_SEPARATOR_STR, "defaults.conf");
828 GNUNET_CONFIGURATION_parse (cfg, baseconfig)) ||
829 (!((cfgfn == NULL) ||
830 (GNUNET_OK == GNUNET_CONFIGURATION_parse (cfg, cfgfn)))))
832 GNUNET_free (baseconfig);
833 return GNUNET_SYSERR;
835 GNUNET_free (baseconfig);
836 if ((GNUNET_YES == GNUNET_CONFIGURATION_have_value (cfg,
839 (GNUNET_YES == GNUNET_CONFIGURATION_get_value_yesno (cfg,
842 GNUNET_CRYPTO_random_disable_entropy_gathering ();
848 /* end of configuration.c */