GNUnet 0.29.1-21-g8c89d4373
 
Loading...
Searching...
No Matches
configuration.c File Reference
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_configuration_lib.h"
Include dependency graph for configuration.c:

Go to the source code of this file.

Data Structures

struct  ConfigSource
 source location of a configuration value More...
 
struct  ConfigEntry
 configuration entry More...
 
struct  ConfigSection
 configuration section More...
 
struct  ConfigFile
 
struct  GNUNET_CONFIGURATION_Handle
 configuration data More...
 
struct  DiffHandle
 Used for diffing a configuration object against the default one. More...
 
struct  CollectFilesContext
 Closure to collect_files_cb. More...
 
struct  CleanLine
 One physical assignment line considered for removal. More...
 
struct  ExpansionFrame
 A [PATHS] option or environment variable whose value is being $-expanded, used to detect definitions that refer to themselves. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "util", __VA_ARGS__)
 
#define LOG_STRERROR_FILE(kind, syscall, filename)    GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
 

Functions

void GNUNET_CONFIGURATION_enable_diagnostics (struct GNUNET_CONFIGURATION_Handle *cfg)
 Enable extra diagnostics.
 
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create (const struct GNUNET_OS_ProjectData *pd)
 Create a new configuration object.
 
void GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
 Destroy configuration object.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse_and_run (const struct GNUNET_OS_ProjectData *pd, const char *filename, GNUNET_CONFIGURATION_Callback cb, void *cb_cls)
 Parse a configuration file filename and run the function cb with the resulting configuration object.
 
static enum GNUNET_GenericReturnValue collect_files_cb (void *cls, const char *filename)
 Function called with a filename.
 
static struct ConfigSection * find_section (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Find a section entry from a configuration.
 
static int pstrcmp (const void *a, const void *b)
 
static void copy_entry_with_sources (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const struct ConfigEntry *entry)
 Copy an entry parsed through an @inline-secret@ directive, including all diagnostic source information.
 
static enum GNUNET_GenericReturnValue handle_inline (struct GNUNET_CONFIGURATION_Handle *cfg, const char *path_or_glob, bool path_is_glob, const char *restrict_section, const char *source_filename, unsigned int source_lineno)
 Handle an inline directive.
 
static struct ConfigEntry * find_entry (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *key)
 Find an entry from a configuration.
 
static void add_entry_source (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value, const char *filename, unsigned int line)
 Record the source of a configuration value.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *source_filename)
 De-serializes configuration.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
 Parse a configuration file, add all of the options in the file to the configuration environment.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_is_dirty (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Test if there are configuration options that were changed since the last save.
 
static bool do_skip (const char *sec, const char *key)
 Should we skip this configuration entry when serializing?
 
char * GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
 Serializes the given configuration.
 
static void dump_os_ipk (const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_Buffer *buf, enum GNUNET_OS_InstallationPathKind ipk)
 Dump an os installation path to a buffer.
 
static char * serialize_diagnostics (const struct GNUNET_CONFIGURATION_Handle *cfg, bool verbose)
 Serialize configuration diagnostics.
 
char * GNUNET_CONFIGURATION_serialize_diagnostics (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Serializes the given configuration with diagnostics information.
 
char * GNUNET_CONFIGURATION_serialize_diagnostics_verbose (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Serializes the given configuration with verbose diagnostics information, including all source definitions of each value.
 
static enum GNUNET_GenericReturnValue read_file (const char *filename, char **contents, size_t *size)
 
static bool file_hash_matches (const char *filename, const struct GNUNET_HashCode *expected)
 Check that a file still has the expected contents.
 
static enum GNUNET_GenericReturnValue write_buffer_atomic (const char *filename, const char *contents, size_t size, const struct GNUNET_HashCode *expected_hash)
 Atomically replace a file with the given bytes.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
 Write configuration file.
 
static struct CleanLine * find_clean_line (struct CleanLine *lines, unsigned int lines_length, unsigned int line)
 Find a cleanup record for a physical line.
 
static bool source_is_removed (const struct ConfigSource *source, const char *target, struct CleanLine *lines, unsigned int lines_length)
 Return whether a source is a target line currently marked for removal.
 
static int clean_line_cmp (const void *a, const void *b)
 Sort cleanup records by source line.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_clean_file (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename, GNUNET_CONFIGURATION_CleanCallback cb, void *cb_cls, unsigned int *removed)
 Remove redundant option definitions from a parsed configuration file.
 
void GNUNET_CONFIGURATION_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
 Iterate over all options in the configuration.
 
void GNUNET_CONFIGURATION_iterate_section_values (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
 Iterate over values of a section in the configuration.
 
void GNUNET_CONFIGURATION_iterate_sections (const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_SectionIterator iter, void *iter_cls)
 Iterate over all sections in the configuration.
 
void GNUNET_CONFIGURATION_remove_section (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
 Remove the given section and all options in it.
 
static void copy_entry (void *cls, const char *section, const char *option, const char *value)
 Copy a configuration value to the given target configuration.
 
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Duplicate an existing configuration object.
 
static void compare_entries (void *cls, const char *section, const char *option, const char *value)
 A callback function, compares entries from two configurations (default against a new configuration) and write the diffs in a diff-configuration object (the callback object).
 
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_get_diff (const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new)
 Compute configuration with only entries that have been changed.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write_diffs (const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new, const char *filename)
 Write only configuration entries that have been changed to configuration file.
 
void GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
 Set a configuration value that should be a string.
 
void GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long number)
 Set a configuration value that should be a number.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
 Get a configuration value that should be a number.
 
void GNUNET_CONFIGURATION_set_value_float (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, float number)
 Set a configuration value that should be a float.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_float (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, float *number)
 Get a configuration value that should be a floating point number.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
 Get a configuration value that should be a relative time.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_size (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *size)
 Get a configuration value that should be a size in bytes.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
 Get a configuration value that should be a string.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_choice (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *const *choices, const char **value)
 Get a configuration value that should be in a set of predefined strings.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, void *buf, size_t buf_size)
 Get Crockford32-encoded fixed-size binary data from a configuration.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
 Test if we have a value for a particular option.
 
static char * expand_all (const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, const struct ExpansionFrame *frame)
 $-expand all terms in a string.
 
static char * expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, unsigned int depth, const struct ExpansionFrame *frame, size_t *skip)
 Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or the environment "FOO" is set to "DIRECTORY".
 
char * GNUNET_CONFIGURATION_expand_dollar (const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig)
 Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or the environment "FOO" is set to "DIRECTORY".
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
 Get a configuration value that should be the name of a file or directory.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
 Get a configuration value that should be in a set of "YES" or "NO".
 
int GNUNET_CONFIGURATION_iterate_value_filenames (const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, GNUNET_FileNameCallback cb, void *cb_cls)
 Iterate over the set of filenames stored in a configuration value.
 
static char * escape_name (const char *value)
 FIXME.
 
static enum GNUNET_GenericReturnValue test_match (void *cls, const char *fn)
 FIXME.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_append_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
 Append a filename to a configuration value that represents a list of filenames.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_remove_value_filename (struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
 Remove a filename from a configuration value that represents a list of filenames.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg, const char *defaults_d)
 Load default configuration.
 
char * GNUNET_CONFIGURATION_default_filename (const struct GNUNET_OS_ProjectData *pd)
 Return the filename of the default configuration filename that is used when no explicit configuration entry point has been specified.
 
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
 Load configuration.
 
static void print_filename_option (void *cls, const char *section, const char *option, const char *value)
 Print each option in a given section as a filename.
 
static void print_option (void *cls, const char *section, const char *option, const char *value)
 Print each option in a given section.
 
static void print_section_name (void *cls, const char *section)
 Print out given section name.
 
void GNUNET_CONFIGURATION_config_tool_run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
 Main task to run to perform operations typical for gnunet-config as per the configuration settings given in cls.
 
void GNUNET_CONFIGURATION_config_settings_free (struct GNUNET_CONFIGURATION_ConfigSettings *cs)
 Free resources associated with cs.
 
const struct GNUNET_OS_ProjectData * GNUNET_CONFIGURATION_get_project_data (const struct GNUNET_CONFIGURATION_Handle *cfg)
 Return the project data associated with this configuration.
 
char * GNUNET_CONFIGURATION_get_suid_binary_path (const struct GNUNET_OS_ProjectData *pd, const struct GNUNET_CONFIGURATION_Handle *cfg, const char *progname)
 Given the name of a helper, service or daemon binary construct the full path to the binary using the SUID_BINARY_PATH in the PATHS section of the configuration.
 

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "util", __VA_ARGS__)

Definition at line 30 of file configuration.c.

◆ LOG_STRERROR_FILE

#define LOG_STRERROR_FILE (   kind,
  syscall,
  filename 
)     GNUNET_log_from_strerror_file (kind, "util", syscall, filename)

Definition at line 32 of file configuration.c.

38{
42 struct ConfigSource *next;
43
47 char *value;
48
52 char *filename;
53
57 unsigned int lineno;
58};
59
60
64struct ConfigEntry
65{
69 struct ConfigEntry *next;
70
74 char *key;
75
79 char *val;
80
84 struct ConfigSource *sources;
85};
86
87
91struct ConfigSection
92{
96 struct ConfigSection *next;
97
101 struct ConfigEntry *entries;
102
106 char *name;
107
114 bool inaccessible;
115
120
124 char *hint_secret_stat;
125
130
134 unsigned int hint_inlined_from_line;
135};
136
137struct ConfigFile
138{
142 char *source_filename;
143
147 unsigned int level;
148
149 struct ConfigFile *prev;
150
151 struct ConfigFile *next;
152
158
163
168
173};
174
175
180{
184 struct ConfigSection *sections;
185
190
195
199 const struct GNUNET_OS_ProjectData *pd;
200
204 unsigned int current_nest_level;
205
209 bool diagnostics;
210
217
221 bool load_called;
222
226 char *main_filename;
227
233 const char *restrict_section;
234};
235
236
241struct DiffHandle
242{
244
246};
247
248
249void
252{
253 cfg->diagnostics = true;
254}
255
256
259{
261 char *p;
262
264 cfg->pd = pd;
265 /* make certain values from the project data available
266 as PATHS */
269 if (NULL != p)
270 {
272 "PATHS",
273 "DATADIR",
274 p);
275 GNUNET_free (p);
276 }
279 if (NULL != p)
280 {
282 "PATHS",
283 "LIBDIR",
284 p);
285 GNUNET_free (p);
286 }
289 if (NULL != p)
290 {
292 "PATHS",
293 "BINDIR",
294 p);
295 GNUNET_free (p);
296 }
299 if (NULL != p)
300 {
302 "PATHS",
303 "PREFIX",
304 p);
305 GNUNET_free (p);
306 }
309 if (NULL != p)
310 {
312 "PATHS",
313 "LOCALEDIR",
314 p);
315 GNUNET_free (p);
316 }
319 if (NULL != p)
320 {
322 "PATHS",
323 "ICONDIR",
324 p);
325 GNUNET_free (p);
326 }
329 if (NULL != p)
330 {
332 "PATHS",
333 "DOCDIR",
334 p);
335 GNUNET_free (p);
336 }
339 if (NULL != p)
340 {
342 "PATHS",
343 "LIBEXECDIR",
344 p);
345 GNUNET_free (p);
346 }
347 return cfg;
348}
349
350
351void
353{
354 struct ConfigSection *sec;
355 struct ConfigFile *cf;
356
357 while (NULL != (sec = cfg->sections))
359 while (NULL != (cf = cfg->loaded_files_head))
360 {
365 cf);
366 GNUNET_free (cf);
367 }
370}
371
372
375 const char *filename,
377 void *cb_cls)
378{
381
383 if (GNUNET_OK !=
385 filename))
386 {
387 GNUNET_break (0);
389 return GNUNET_SYSERR;
390 }
391 ret = cb (cb_cls,
392 cfg);
394 return ret;
395}
396
397
402{
406 char **files;
407
411 unsigned int files_length;
412};
413
414
425collect_files_cb (void *cls,
426 const char *filename)
427{
428 struct CollectFilesContext *igc = cls;
429
431 igc->files_length,
433 return GNUNET_OK;
434}
435
436
444static struct ConfigSection *
446 const char *section)
447{
448 struct ConfigSection *pos;
449
450 if (NULL == cfg)
451 return NULL;
452 pos = cfg->sections;
453 while ( (NULL != pos) &&
454 (0 != strcasecmp (section,
455 pos->name)) )
456 pos = pos->next;
457 return pos;
458}
459
460
461static int
462pstrcmp (const void *a,
463 const void *b)
464{
465 return strcmp (*((const char **) a),
466 *((const char **) b));
467}
468
469
470static void
472 const char *section,
473 const struct ConfigEntry *entry);
474
475
483 const char *path_or_glob,
484 bool path_is_glob,
485 const char *restrict_section,
486 const char *source_filename,
487 unsigned int source_lineno)
488{
489 char *inline_path = NULL;
490 struct GNUNET_CONFIGURATION_Handle *other_cfg = NULL;
491 struct CollectFilesContext igc = {
492 .files = NULL,
493 .files_length = 0,
494 };
495 enum GNUNET_GenericReturnValue fun_ret;
496 unsigned int old_nest_level = cfg->current_nest_level++;
497
498 /* We support the section restriction only for non-globs */
499 GNUNET_assert (! (path_is_glob && (NULL != restrict_section)));
500
501 if (NULL == source_filename)
502 {
504 "Refusing to parse inline configurations, "
505 "not allowed without source filename!\n");
506 fun_ret = GNUNET_SYSERR;
507 goto cleanup;
508 }
509
510 if ('/' == *path_or_glob)
511 inline_path = GNUNET_strdup (path_or_glob);
512 else
513 {
514 /* We compute the canonical, absolute path first,
515 so that relative imports resolve properly with symlinked
516 config files. */
517 char *source_realpath;
518 char *endsep;
519
520 source_realpath = realpath (source_filename,
521 NULL);
522 if (NULL == source_realpath)
523 {
524 /* Couldn't even resolve path of base dir. */
525 GNUNET_break (0);
526 /* failed to parse included config */
527 fun_ret = GNUNET_SYSERR;
528 goto cleanup;
529 }
530 endsep = strrchr (source_realpath, '/');
531 GNUNET_assert (NULL != endsep);
532 *endsep = '\0';
533 GNUNET_asprintf (&inline_path,
534 "%s/%s",
535 source_realpath,
536 path_or_glob);
537 free (source_realpath);
538 }
539
540 if (path_is_glob)
541 {
542 int nret;
543
545 "processing config glob '%s'\n",
546 inline_path);
547
548 nret = GNUNET_DISK_glob (inline_path,
550 &igc);
551 if (-1 == nret)
552 {
553 fun_ret = GNUNET_SYSERR;
554 goto cleanup;
555 }
556 GNUNET_assert (nret == igc.files_length);
557 qsort (igc.files,
558 igc.files_length,
559 sizeof (char *),
560 &pstrcmp);
561 for (int i = 0; i < nret; i++)
562 {
563 if (GNUNET_OK !=
565 igc.files[i]))
566 {
567 fun_ret = GNUNET_SYSERR;
568 goto cleanup;
569 }
570 }
571 fun_ret = GNUNET_OK;
572 }
573 else if (NULL != restrict_section)
574 {
575 enum GNUNET_GenericReturnValue inner_ret;
576 struct ConfigSection *cs;
577 struct ConfigFile *cf = GNUNET_new (struct ConfigFile);
578
579 inner_ret = GNUNET_DISK_file_test_read (inline_path);
580
581 cs = find_section (cfg, restrict_section);
582
583 if (NULL == cs)
584 {
585 cs = GNUNET_new (struct ConfigSection);
586 cs->name = GNUNET_strdup (restrict_section);
587 cs->next = cfg->sections;
588 cfg->sections = cs;
589 cs->entries = NULL;
590 }
591 if (cfg->diagnostics)
592 {
593 const char *pwname;
594 const char *grname;
595 char *sfn = GNUNET_STRINGS_filename_expand (inline_path);
596 struct stat istat;
597
598 cs->hint_secret_filename = sfn;
599 if (0 == stat (sfn, &istat))
600 {
601 struct passwd *pw = getpwuid (istat.st_uid);
602 struct group *gr = getgrgid (istat.st_gid);
603 pwname = (NULL == pw) ? "<unknown>" : pw->pw_name;
604 grname = (NULL == gr) ? "<unknown>" : gr->gr_name;
605
607 "%s:%s %o",
608 pwname,
609 grname,
610 istat.st_mode);
611 }
612 else
613 {
614 cs->hint_secret_stat = GNUNET_strdup ("<can't stat file>");
615 }
616 if (source_filename)
617 {
618 /* Possible that this secret section has been inlined before */
620 cs->hint_inlined_from_filename = GNUNET_strdup (source_filename);
621 cs->hint_inlined_from_line = source_lineno;
622 }
623 }
624
625 /* Put file in the load list for diagnostics, even if we can't access it. */
626 {
628 cf->source_filename = GNUNET_strdup (inline_path);
629 cf->hint_restrict_section = GNUNET_strdup (restrict_section);
632 cf);
633 }
634
635 if (GNUNET_OK != inner_ret)
636 {
637 cs->inaccessible = true;
638 cf->hint_inaccessible = true;
639 /* File can't be accessed, but that's okay. */
640 fun_ret = GNUNET_OK;
641 goto cleanup;
642 }
643
644 other_cfg = GNUNET_CONFIGURATION_create (cfg->pd);
645 if (cfg->diagnostics)
647 other_cfg->restrict_section = restrict_section;
648 inner_ret = GNUNET_CONFIGURATION_parse (other_cfg,
649 inline_path);
650 if (GNUNET_OK != inner_ret)
651 {
652 cf->hint_inaccessible = true;
653 fun_ret = inner_ret;
654 goto cleanup;
655 }
656
657 cs = find_section (other_cfg,
658 restrict_section);
659 if (NULL == cs)
660 {
662 "Configuration file '%s' loaded with @inline-secret@ "
663 "does not contain section '%s'.\n",
664 inline_path,
665 restrict_section);
666 /* Inlined configuration is accessible but doesn't contain any values.
667 We treat this as if the inlined section was empty, and do not
668 consider it an error. */
669 fun_ret = GNUNET_OK;
670 goto cleanup;
671 }
672 for (struct ConfigEntry *ce = cs->entries;
673 NULL != ce;
674 ce = ce->next)
676 restrict_section,
677 ce);
678 fun_ret = GNUNET_OK;
679 }
680 else if (GNUNET_OK !=
682 inline_path))
683 {
684 fun_ret = GNUNET_SYSERR;
685 goto cleanup;
686 }
687 else
688 {
689 fun_ret = GNUNET_OK;
690 }
691cleanup:
692 cfg->current_nest_level = old_nest_level;
693 if (NULL != other_cfg)
695 GNUNET_free (inline_path);
696 if (igc.files_length > 0)
697 {
698 for (size_t i = 0; i < igc.files_length; i++)
699 GNUNET_free (igc.files[i]);
701 }
702 return fun_ret;
703}
704
705
714static struct ConfigEntry *
716 const char *section,
717 const char *key)
718{
719 struct ConfigSection *sec;
720
721 if (NULL == (sec = find_section (cfg,
722 section)))
723 return NULL;
724 for (struct ConfigEntry *pos = sec->entries;
725 pos != NULL;
726 pos = pos->next)
727 if (0 == strcasecmp (key,
728 pos->key))
729 return pos;
730 if (sec->inaccessible)
731 {
733 "Section '%s' is marked as inaccessible, because the configuration "
734 "file that contains the section can't be read. Attempts to use "
735 "option '%s' will fail.\n",
736 section,
737 key);
738 return NULL;
739 }
740 return NULL;
741}
742
743
754static void
756 const char *section,
757 const char *option,
758 const char *value,
759 const char *filename,
760 unsigned int line)
761{
762 struct ConfigEntry *e;
763 struct ConfigSource *source;
764
765 if (! cfg->diagnostics)
766 return;
767 e = find_entry (cfg,
768 section,
769 option);
770 if (NULL == e)
771 return;
772 source = GNUNET_new (struct ConfigSource);
773 source->value = GNUNET_strdup (value);
774 source->filename = GNUNET_strdup (filename);
775 source->lineno = line;
776 source->next = e->sources;
777 e->sources = source;
778}
779
780
789static void
791 const char *section,
792 const struct ConfigEntry *entry)
793{
794 struct ConfigEntry *dst;
795 struct ConfigSource *head = NULL;
796 struct ConfigSource *tail = NULL;
797
799 section,
800 entry->key,
801 entry->val);
802 if ( (! cfg->diagnostics) ||
803 (NULL == entry->sources) )
804 return;
805 dst = find_entry (cfg,
806 section,
807 entry->key);
808 GNUNET_assert (NULL != dst);
809 for (const struct ConfigSource *pos = entry->sources;
810 NULL != pos;
811 pos = pos->next)
812 {
813 struct ConfigSource *copy = GNUNET_new (struct ConfigSource);
814
815 copy->value = GNUNET_strdup (pos->value);
816 copy->filename = GNUNET_strdup (pos->filename);
817 copy->lineno = pos->lineno;
818 if (NULL == tail)
819 head = copy;
820 else
821 tail->next = copy;
822 tail = copy;
823 }
824 tail->next = dst->sources;
825 dst->sources = head;
826}
827
828
831 const char *mem,
832 size_t size,
833 const char *source_filename)
834{
835 size_t line_size;
836 unsigned int nr;
837 size_t r_bytes;
838 size_t to_read;
840 char *section;
841 char *eq;
842 char *tag;
843 char *value;
844 char *line_orig = NULL;
845
846 ret = GNUNET_OK;
847 section = NULL;
848 nr = 0;
849 r_bytes = 0;
850 while (r_bytes < size)
851 {
852 const char *pos;
853 char *line;
854 bool emptyline;
855
856 GNUNET_free (line_orig);
857 /* fgets-like behaviour on buffer */
858 to_read = size - r_bytes;
859 pos = memchr (&mem[r_bytes], '\n', to_read);
860 if (NULL == pos)
861 {
862 line_orig = GNUNET_strndup (&mem[r_bytes],
863 line_size = to_read);
864 r_bytes += line_size;
865 }
866 else
867 {
868 line_orig = GNUNET_strndup (&mem[r_bytes],
869 line_size = (pos - &mem[r_bytes]));
870 r_bytes += line_size + 1;
871 }
872 line = line_orig;
873 /* increment line number */
874 nr++;
875 if (strlen (line_orig) != line_size)
876 {
878 "NUL byte in line %u\n",
879 nr);
881 break;
882 }
883 /* tabs and '\r' are whitespace */
884 emptyline = GNUNET_YES;
885 for (size_t i = 0; i < line_size; i++)
886 {
887 if (line[i] == '\t')
888 line[i] = ' ';
889 if (line[i] == '\r')
890 line[i] = ' ';
891 if (' ' != line[i])
892 emptyline = GNUNET_NO;
893 }
894 /* ignore empty lines */
895 if (GNUNET_YES == emptyline)
896 continue;
897
898 /* remove tailing whitespace */
899 for (size_t i = line_size - 1;
900 (i >= 1) && (isspace ((unsigned char) line[i]));
901 i--)
902 line[i] = '\0';
903
904 /* remove leading whitespace */
905 for (; line[0] != '\0' && (isspace ((unsigned char) line[0])); line++)
906 ;
907 line_size = strlen (line);
908
909 /* ignore comments */
910 if ( ('#' == line[0]) ||
911 ('%' == line[0]) )
912 continue;
913
914 /* Handle special directives. */
915 if ('@' == line[0])
916 {
917 char *end = strchr (line + 1, '@');
918 char *directive;
919 enum GNUNET_GenericReturnValue directive_ret;
920
921 if (NULL != cfg->restrict_section)
922 {
924 "Illegal directive in line %u (parsing restricted section %s)\n",
925 nr,
928 break;
929 }
930
931 if (NULL == end)
932 {
934 "Bad directive in line %u\n",
935 nr);
937 break;
938 }
939 *end = '\0';
940 directive = line + 1;
941
942 if (0 == strcasecmp (directive,
943 "INLINE"))
944 {
945 const char *path = end + 1;
946
947 /* Skip space before path */
948 for (; isspace (*path); path++)
949 ;
950
951 directive_ret = handle_inline (cfg,
952 path,
953 false,
954 NULL,
955 source_filename,
956 nr);
957 }
958 else if (0 == strcasecmp (directive,
959 "INLINE-MATCHING"))
960 {
961 const char *path = end + 1;
962
963 /* Skip space before path */
964 for (; isspace (*path); path++)
965 ;
966
967 directive_ret = handle_inline (cfg,
968 path,
969 true,
970 NULL,
971 source_filename,
972 nr);
973 }
974 else if (0 == strcasecmp (directive,
975 "INLINE-SECRET"))
976 {
977 char *secname = end + 1;
978 char *secname_end;
979 const char *path;
980
981 /* Skip space before secname */
982 for (; isspace (*secname); secname++)
983 ;
984
985 secname_end = strchr (secname, ' ');
986
987 if (NULL == secname_end)
988 {
990 "Bad inline-secret directive in line %u\n",
991 nr);
993 break;
994 }
995 *secname_end = '\0';
996 path = secname_end + 1;
997
998 /* Skip space before path */
999 for (; isspace (*path); path++)
1000 ;
1001
1002 directive_ret = handle_inline (cfg,
1003 path,
1004 false,
1005 secname,
1006 source_filename,
1007 nr);
1008 }
1009 else
1010 {
1012 "Unknown or malformed directive '%s' in line %u\n",
1013 directive,
1014 nr);
1016 break;
1017 }
1018 if (GNUNET_OK != directive_ret)
1019 {
1020 ret = directive_ret;
1021 break;
1022 }
1023 continue;
1024 }
1025 if ( ('[' == line[0]) &&
1026 (']' == line[line_size - 1]) )
1027 {
1028 /* [value] */
1029 line[line_size - 1] = '\0';
1030 value = &line[1];
1031 GNUNET_free (section);
1032 section = GNUNET_strdup (value);
1033 continue;
1034 }
1035 if (NULL != (eq = strchr (line, '=')))
1036 {
1037
1038 if (NULL == section)
1039 {
1041 "Syntax error while deserializing in line %u (option without section)\n",
1042 nr);
1044 break;
1045 }
1046
1047 /* tag = value */
1048 tag = GNUNET_strndup (line, eq - line);
1049 /* remove tailing whitespace */
1050 for (int i = strlen (tag) - 1;
1051 (i >= 1) && (isspace ((unsigned char) tag[i]));
1052 i--)
1053 tag[i] = '\0';
1054
1055 /* Strip whitespace */
1056 value = eq + 1;
1057 while (isspace ((unsigned char) value[0]))
1058 value++;
1059 for (int i = strlen (value) - 1;
1060 (i >= 1) && (isspace ((unsigned char) value[i]));
1061 i--)
1062 value[i] = '\0';
1063
1064 /* remove quotes */
1065 if ( ('"' == value[0]) &&
1066 ('"' == value[strlen (value) - 1]) )
1067 {
1068 value[strlen (value) - 1] = '\0';
1069 value++;
1070 }
1072 section,
1073 tag,
1074 value);
1075 if (cfg->diagnostics)
1076 {
1078 section,
1079 tag,
1080 value,
1081 source_filename
1082 ? source_filename
1083 : "<input>",
1084 nr);
1085 }
1086 GNUNET_free (tag);
1087 continue;
1088 }
1089 /* parse error */
1091 "Syntax error while deserializing in line %u\n",
1092 nr);
1094 break;
1095 }
1096 GNUNET_free (line_orig);
1097 GNUNET_free (section);
1098 GNUNET_assert ( (GNUNET_OK != ret) ||
1099 (r_bytes == size) );
1100 return ret;
1101}
1102
1103
1106 const char *filename)
1107{
1108 struct ConfigFile *loaded_file;
1109 uint64_t fs64;
1110 size_t fs;
1111 char *fn;
1112 char *mem;
1113 int dirty;
1115 ssize_t sret;
1116
1119 "Asked to parse config file `%s'\n",
1120 fn);
1121 if (NULL == fn)
1122 return GNUNET_SYSERR;
1123
1124
1125 /* Check for cycles */
1126 {
1127 unsigned int lvl = cfg->current_nest_level;
1128 struct ConfigFile *cf = cfg->loaded_files_tail;
1129 struct ConfigFile *parent = NULL;
1130
1131
1132 for (; NULL != cf; parent = cf, cf = cf->prev)
1133 {
1134 /* Check parents based on level, skipping children of siblings. */
1135 if (cf->level >= lvl)
1136 continue;
1137 lvl = cf->level;
1138 if ( (NULL == cf->source_filename) ||
1139 (NULL == filename) )
1140 continue;
1141 if (0 == strcmp (cf->source_filename,
1142 filename))
1143 {
1144 if (NULL == parent)
1145 {
1147 "Forbidden direct cyclic configuration import (%s -> %s)\n",
1148 cf->source_filename,
1149 filename);
1150 }
1151 else
1153 "Forbidden indirect cyclic configuration import (%s -> ... -> %s -> %s)\n",
1154 cf->source_filename,
1155 parent->source_filename,
1156 filename);
1157 GNUNET_free (fn);
1158 return GNUNET_SYSERR;
1159 }
1160 }
1161
1162 }
1163
1164 /* Keep track of loaded files.*/
1165 {
1166 loaded_file = GNUNET_new (struct ConfigFile);
1167
1168 loaded_file->level = cfg->current_nest_level;
1169 loaded_file->source_filename = GNUNET_strdup (filename
1170 ? filename
1171 : "<input>");
1174 loaded_file);
1175 }
1176
1177 dirty = cfg->dirty; /* back up value! */
1178 if (GNUNET_SYSERR ==
1180 &fs64,
1181 GNUNET_NO,
1182 GNUNET_YES))
1183 {
1185 "Error while determining the file size of `%s'\n",
1186 fn);
1187 GNUNET_free (fn);
1188 return GNUNET_SYSERR;
1189 }
1190 if (fs64 > SIZE_MAX)
1191 {
1192 GNUNET_break (0); /* File size is more than the heap size */
1193 GNUNET_free (fn);
1194 return GNUNET_SYSERR;
1195 }
1196 fs = fs64;
1197 mem = GNUNET_malloc (fs);
1198 sret = GNUNET_DISK_fn_read (fn,
1199 mem,
1200 fs);
1201 if ((sret < 0) || (fs != (size_t) sret))
1202 {
1204 "Error while reading file `%s'\n",
1205 fn);
1206 GNUNET_free (fn);
1207 GNUNET_free (mem);
1208 return GNUNET_SYSERR;
1209 }
1210 if (cfg->diagnostics)
1211 {
1212 GNUNET_CRYPTO_hash (mem,
1213 fs,
1214 &loaded_file->content_hash);
1215 }
1217 "Deserializing contents of file `%s'\n",
1218 fn);
1220 mem,
1221 fs,
1222 fn);
1223 if (GNUNET_SYSERR == ret)
1224 {
1226 _ ("Failed to parse configuration file `%s'\n"),
1227 fn);
1228 }
1229 else if (cfg->diagnostics)
1230 {
1231 loaded_file->have_content_hash = true;
1232 }
1233 GNUNET_free (fn);
1234 GNUNET_free (mem);
1235 /* restore dirty flag - anything we set in the meantime
1236 * came from disk */
1237 cfg->dirty = dirty;
1238 return ret;
1239}
1240
1241
1244{
1245 return cfg->dirty;
1246}
1247
1248
1256static bool
1257do_skip (const char *sec,
1258 const char *key)
1259{
1260 if (0 != strcasecmp ("PATHS",
1261 sec))
1262 return false;
1263 return ( (0 == strcasecmp ("DATADIR",
1264 key)) ||
1265 (0 == strcasecmp ("LIBDIR",
1266 key)) ||
1267 (0 == strcasecmp ("BINDIR",
1268 key)) ||
1269 (0 == strcasecmp ("PREFIX",
1270 key)) ||
1271 (0 == strcasecmp ("LOCALEDIR",
1272 key)) ||
1273 (0 == strcasecmp ("ICONDIR",
1274 key)) ||
1275 (0 == strcasecmp ("DOCDIR",
1276 key)) ||
1277 (0 == strcasecmp ("DEFAULTCONFIG",
1278 key)) ||
1279 (0 == strcasecmp ("LIBEXECDIR",
1280 key)) );
1281}
1282
1283
1284char *
1286 size_t *size)
1287{
1288 char *mem;
1289 char *cbuf;
1290 char *val;
1291 char *pos;
1292 size_t m_size;
1293 size_t c_size;
1294
1295 /* Pass1 : calculate the buffer size required */
1296 m_size = 0;
1297 for (struct ConfigSection *sec = cfg->sections;
1298 NULL != sec;
1299 sec = sec->next)
1300 {
1301 if (sec->inaccessible && (NULL == sec->entries))
1302 continue;
1303 /* For each section we need to add 3 characters: {'[',']','\n'} */
1304 m_size += strlen (sec->name) + 3;
1305 for (struct ConfigEntry *ent = sec->entries;
1306 NULL != ent;
1307 ent = ent->next)
1308 {
1309 if (do_skip (sec->name,
1310 ent->key))
1311 continue;
1312 if (NULL != ent->val)
1313 {
1314 /* if val has any '\n' then they occupy +1 character as '\n'->'\\','n' */
1315 pos = ent->val;
1316 while (NULL != (pos = strstr (pos, "\n")))
1317 {
1318 m_size++;
1319 pos++;
1320 }
1321 /* For each key = value pair we need to add 4 characters (2
1322 spaces and 1 equal-to character and 1 new line) */
1323 m_size += strlen (ent->key) + strlen (ent->val) + 4;
1324 }
1325 }
1326 /* A new line after section end */
1327 m_size++;
1328 }
1329
1330 /* Pass2: Allocate memory and write the configuration to it */
1331 mem = GNUNET_malloc (m_size);
1332 c_size = 0;
1333 *size = c_size;
1334 for (struct ConfigSection *sec = cfg->sections;
1335 NULL != sec;
1336 sec = sec->next)
1337 {
1338 int len;
1339
1340 /* skipped in pass 1 */
1341 if (sec->inaccessible && (NULL == sec->entries))
1342 continue;
1343 len = GNUNET_asprintf (&cbuf,
1344 "[%s]\n",
1345 sec->name);
1346 GNUNET_assert (0 < len);
1347 GNUNET_memcpy (mem + c_size,
1348 cbuf,
1349 len);
1350 c_size += len;
1351 GNUNET_free (cbuf);
1352 for (struct ConfigEntry *ent = sec->entries;
1353 NULL != ent;
1354 ent = ent->next)
1355 {
1356 if (do_skip (sec->name,
1357 ent->key))
1358 continue;
1359 if (NULL != ent->val)
1360 {
1361 val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
1362 strcpy (val, ent->val);
1363 while (NULL != (pos = strstr (val, "\n")))
1364 {
1365 memmove (&pos[2], &pos[1], strlen (&pos[1]));
1366 pos[0] = '\\';
1367 pos[1] = 'n';
1368 }
1369 len = GNUNET_asprintf (&cbuf, "%s = %s\n", ent->key, val);
1370 GNUNET_free (val);
1371 GNUNET_memcpy (mem + c_size, cbuf, len);
1372 c_size += len;
1373 GNUNET_free (cbuf);
1374 }
1375 }
1376 GNUNET_memcpy (mem + c_size, "\n", 1);
1377 c_size++;
1378 }
1379 GNUNET_assert (c_size == m_size);
1380 *size = c_size;
1381 return mem;
1382}
1383
1384
1393static void
1395 const struct GNUNET_CONFIGURATION_Handle *cfg,
1396 struct GNUNET_Buffer *buf,
1398{
1400 ipk);
1401 const char *ipkname;
1402 switch (ipk)
1403 {
1405 ipkname = "IPK_DATADIR";
1406 break;
1408 ipkname = "IPK_BINDIR";
1409 break;
1411 ipkname = "IPK_LIBDIR";
1412 break;
1414 ipkname = "IPK_PREFIX";
1416 ipkname = "IPK_LOCALEDIR";
1417 break;
1419 ipkname = "IPK_ICONDIR";
1421 ipkname = "IPK_DOCDIR";
1422 break;
1424 ipkname = "IPK_LIBEXECDIR";
1425 break;
1427 ipkname = "IPK_SELF_PREFIX";
1428 break;
1429 default:
1430 ipkname = "??";
1431 break;
1432 }
1434 "# %s = %s\n",
1435 ipkname,
1436 v);
1437 GNUNET_free (v);
1438}
1439
1440
1448static char *
1450 bool verbose)
1451{
1452 struct GNUNET_Buffer buf = { 0 };
1453
1455 "#\n# Configuration file diagnostics\n#\n");
1457 "# Entry point: %s\n",
1459 "<none>");
1461 "#\n# Files Loaded:\n");
1462
1463 for (struct ConfigFile *cfil = cfg->loaded_files_head;
1464 NULL != cfil;
1465 cfil = cfil->next)
1466 {
1468 "# ");
1469 for (unsigned int i = 0; i < cfil->level; i++)
1471 "+");
1472 if (0 != cfil->level)
1474 " ");
1475
1477 "%s",
1478 cfil->source_filename);
1479
1480 if (NULL != cfil->hint_restrict_section)
1482 " (%s secret section %s)",
1483 cfil->hint_inaccessible
1484 ? "inaccessible"
1485 : "loaded",
1486 cfil->hint_restrict_section);
1487
1489 "\n");
1490 }
1491
1493 "#\n# Installation paths:\n");
1494
1504
1505
1507 "#\n\n");
1508
1509 for (struct ConfigSection *sec = cfg->sections;
1510 NULL != sec;
1511 sec = sec->next)
1512 {
1513 if (sec->hint_secret_filename)
1515 "# secret section from %s\n# secret file stat %s\n",
1516 sec->hint_secret_filename,
1517 sec->hint_secret_stat);
1518 if (sec->hint_inlined_from_filename)
1519 {
1521 "# inlined from %s:%u\n",
1522 sec->hint_inlined_from_filename,
1523 sec->hint_inlined_from_line);
1524 }
1526 "[%s]\n\n",
1527 sec->name);
1528 if (sec->inaccessible && (NULL == sec->entries))
1529 {
1531 "# <section contents inaccessible>\n\n\n");
1532 continue;
1533 }
1534 for (struct ConfigEntry *ent = sec->entries;
1535 NULL != ent;
1536 ent = ent->next)
1537 {
1538 if (do_skip (sec->name,
1539 ent->key))
1540 continue;
1541 if (NULL != ent->val)
1542 {
1543 char *pos;
1544 char *val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
1545 strcpy (val, ent->val);
1546 while (NULL != (pos = strstr (val, "\n")))
1547 {
1548 memmove (&pos[2], &pos[1], strlen (&pos[1]));
1549 pos[0] = '\\';
1550 pos[1] = 'n';
1551 }
1552 if (NULL != ent->sources)
1553 {
1554 if (verbose &&
1555 (NULL != ent->sources->next))
1556 {
1558 &buf,
1559 "# duplicate definitions (highest precedence first):\n");
1560 for (const struct ConfigSource *source = ent->sources;
1561 NULL != source;
1562 source = source->next)
1563 {
1564 const char *status;
1565
1566 if (source == ent->sources)
1567 status = "effective";
1568 else if (0 == strcmp (source->value,
1569 ent->val))
1570 status = "same value";
1571 else
1572 status = "different value";
1574 &buf,
1575 "# %s:%u (%s)\n",
1576 source->filename,
1577 source->lineno,
1578 status);
1579 }
1580 }
1581 else
1582 {
1584 "# %s:%u\n",
1585 ent->sources->filename,
1586 ent->sources->lineno);
1587 }
1588 }
1590 "%s = %s\n",
1591 ent->key,
1592 val);
1593 GNUNET_free (val);
1594 }
1595 GNUNET_buffer_write_str (&buf, "\n");
1596 }
1597 if (sec->inaccessible)
1598 {
1600 "# <additional section contents inaccessible>\n\n\n");
1601 continue;
1602 }
1603
1604 GNUNET_buffer_write_str (&buf, "\n");
1605 }
1606 return GNUNET_buffer_reap_str (&buf);
1607}
1608
1609
1610char *
1612 const struct GNUNET_CONFIGURATION_Handle *cfg)
1613{
1614 return serialize_diagnostics (cfg,
1615 false);
1616}
1617
1618
1619char *
1621 const struct GNUNET_CONFIGURATION_Handle *cfg)
1622{
1623 return serialize_diagnostics (cfg,
1624 true);
1625}
1626
1627
1628static enum GNUNET_GenericReturnValue
1629read_file (const char *filename,
1630 char **contents,
1631 size_t *size)
1632{
1633 uint64_t fs64;
1634 ssize_t ret;
1635
1636 if ( (GNUNET_SYSERR ==
1638 &fs64,
1639 GNUNET_NO,
1640 GNUNET_YES)) ||
1641 (fs64 > SIZE_MAX) )
1642 return GNUNET_SYSERR;
1643 *size = (size_t) fs64;
1644 *contents = GNUNET_malloc (*size);
1646 *contents,
1647 *size);
1648 if ( (ret < 0) || ((size_t) ret != *size) )
1649 {
1650 GNUNET_free (*contents);
1651 *contents = NULL;
1652 return GNUNET_SYSERR;
1653 }
1654 return GNUNET_OK;
1655}
1656
1657
1665static bool
1666file_hash_matches (const char *filename,
1667 const struct GNUNET_HashCode *expected)
1668{
1669 struct GNUNET_HashCode actual;
1670 char *contents;
1671 size_t size;
1672
1674 &contents,
1675 &size))
1676 return false;
1677 GNUNET_CRYPTO_hash (contents,
1678 size,
1679 &actual);
1680 GNUNET_free (contents);
1681 return 0 == GNUNET_memcmp (&actual,
1682 expected);
1683}
1684
1685
1695static enum GNUNET_GenericReturnValue
1696write_buffer_atomic (const char *filename,
1697 const char *contents,
1698 size_t size,
1699 const struct GNUNET_HashCode *expected_hash)
1700{
1701 char *fn;
1702 char *tfn;
1703
1705 if (fn == NULL)
1706 return GNUNET_SYSERR;
1708 {
1709 GNUNET_free (fn);
1710 return GNUNET_SYSERR;
1711 }
1712 {
1713 /* If @a filename is a symlink, replace what it points at, the way
1714 truncating it in place used to. */
1715 char *rfn = realpath (fn,
1716 NULL);
1717
1718 if (NULL != rfn)
1719 {
1720 GNUNET_free (fn);
1721 fn = GNUNET_strdup (rfn);
1722 free (rfn);
1723 }
1724 }
1725 /* Never rewrite the configuration in place: opening it with O_TRUNC leaves
1726 it observably empty until we have written the new contents, and anyone
1727 parsing the file in that instant -- say a daemon that was just started
1728 with this very configuration -- reads a truncated file and fails. So
1729 write a sibling file and rename(2) it over the target; readers then
1730 always see either the complete old or the complete new configuration. */
1731 GNUNET_asprintf (&tfn,
1732 "%s.%u.tmp",
1733 fn,
1734 (unsigned int) getpid ());
1735 {
1736 struct GNUNET_DISK_FileHandle *h;
1737
1738 h = GNUNET_DISK_file_open (tfn,
1746 if (NULL == h)
1747 {
1748 GNUNET_free (tfn);
1749 GNUNET_free (fn);
1750 return GNUNET_SYSERR;
1751 }
1752 if (((ssize_t) size) !=
1754 contents,
1755 size))
1756 {
1758 "write",
1759 tfn);
1761 (void) unlink (tfn);
1762 GNUNET_free (tfn);
1763 GNUNET_free (fn);
1764 return GNUNET_SYSERR;
1765 }
1766 /* Get the contents onto the disk before the rename publishes them, so
1767 that a crash cannot leave an empty configuration behind. */
1769 {
1771 "fsync",
1772 tfn);
1773 (void) GNUNET_DISK_file_close (h);
1774 (void) unlink (tfn);
1775 GNUNET_free (tfn);
1776 GNUNET_free (fn);
1777 return GNUNET_SYSERR;
1778 }
1780 {
1782 "close",
1783 tfn);
1784 (void) unlink (tfn);
1785 GNUNET_free (tfn);
1786 GNUNET_free (fn);
1787 return GNUNET_SYSERR;
1788 }
1789 }
1790 {
1791 struct stat sbuf;
1792
1793 /* rename(2) replaces the inode, so carry over the permissions of the
1794 configuration we are about to replace (we just created @a tfn). */
1795 if ( (0 == stat (fn,
1796 &sbuf)) &&
1797 (0 != chmod (tfn,
1798 sbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) )
1800 "chmod",
1801 tfn);
1802 }
1803 if ( (NULL != expected_hash) &&
1804 (! file_hash_matches (fn,
1805 expected_hash)) )
1806 {
1808 "Configuration file `%s' changed while it was being cleaned\n",
1809 fn);
1810 (void) unlink (tfn);
1811 GNUNET_free (tfn);
1812 GNUNET_free (fn);
1813 return GNUNET_SYSERR;
1814 }
1815 if (0 != rename (tfn,
1816 fn))
1817 {
1819 "rename",
1820 fn);
1821 (void) unlink (tfn);
1822 GNUNET_free (tfn);
1823 GNUNET_free (fn);
1824 return GNUNET_SYSERR;
1825 }
1826 GNUNET_free (tfn);
1827 GNUNET_free (fn);
1828 return GNUNET_OK;
1829}
1830
1831
1835 const char *filename)
1836{
1838 char *cfg_buf;
1839 size_t size;
1840
1842 &size);
1844 cfg_buf,
1845 size,
1846 NULL);
1847 GNUNET_free (cfg_buf);
1849 return ret;
1850}
1851
1852
1856struct CleanLine
1857{
1858 const char *section;
1859 const char *option;
1860 unsigned int line;
1861 bool remove;
1862};
1863
1864
1868static struct CleanLine *
1869find_clean_line (struct CleanLine *lines,
1870 unsigned int lines_length,
1871 unsigned int line)
1872{
1873 for (unsigned int i = 0; i < lines_length; i++)
1874 if (lines[i].line == line)
1875 return &lines[i];
1876 return NULL;
1877}
1878
1879
1883static bool
1885 const char *target,
1886 struct CleanLine *lines,
1887 unsigned int lines_length)
1888{
1889 struct CleanLine *line;
1890
1891 if (0 != strcmp (source->filename,
1892 target))
1893 return false;
1894 line = find_clean_line (lines,
1895 lines_length,
1896 source->lineno);
1897 return (NULL != line) && line->remove;
1898}
1899
1900
1904static int
1905clean_line_cmp (const void *a,
1906 const void *b)
1907{
1908 const struct CleanLine *la = a;
1909 const struct CleanLine *lb = b;
1910
1911 if (la->line < lb->line)
1912 return -1;
1913 if (la->line > lb->line)
1914 return 1;
1915 return 0;
1916}
1917
1918
1921 const struct GNUNET_CONFIGURATION_Handle *cfg,
1922 const char *filename,
1924 void *cb_cls,
1925 unsigned int *removed)
1926{
1927 struct CleanLine *lines = NULL;
1928 unsigned int lines_length = 0;
1929 const struct ConfigFile *parsed_file = NULL;
1930 char *target;
1931 char *contents = NULL;
1932 char *cleaned = NULL;
1933 size_t size;
1934 size_t cleaned_size = 0;
1936
1937 if (NULL != removed)
1938 *removed = 0;
1939 if ( (! cfg->diagnostics) ||
1940 (NULL == filename) )
1941 return GNUNET_SYSERR;
1943 if (NULL == target)
1944 return GNUNET_SYSERR;
1945
1946 for (const struct ConfigFile *cf = cfg->loaded_files_head;
1947 NULL != cf;
1948 cf = cf->next)
1949 {
1950 char *source;
1951
1952 if (! cf->have_content_hash)
1953 continue;
1954 source = GNUNET_STRINGS_filename_expand (cf->source_filename);
1955 if ( (NULL != source) &&
1956 (0 == strcmp (source,
1957 target)) )
1958 parsed_file = cf;
1960 }
1961 if (NULL == parsed_file)
1962 {
1964 "Configuration file `%s' was not parsed with diagnostics enabled\n",
1965 target);
1966 goto cleanup;
1967 }
1968 if (GNUNET_OK != read_file (target,
1969 &contents,
1970 &size))
1971 {
1973 "Configuration file `%s' changed since it was parsed\n",
1974 target);
1975 goto cleanup;
1976 }
1977 {
1978 struct GNUNET_HashCode actual;
1979
1980 GNUNET_CRYPTO_hash (contents,
1981 size,
1982 &actual);
1983 if (0 != GNUNET_memcmp (&actual,
1984 &parsed_file->content_hash))
1985 {
1987 "Configuration file `%s' changed since it was parsed\n",
1988 target);
1989 goto cleanup;
1990 }
1991 }
1992
1993 /* First mark every target definition that has an exact duplicate at a
1994 distinct source location. */
1995 for (const struct ConfigSection *sec = cfg->sections;
1996 NULL != sec;
1997 sec = sec->next)
1998 for (const struct ConfigEntry *ent = sec->entries;
1999 NULL != ent;
2000 ent = ent->next)
2001 for (const struct ConfigSource *source = ent->sources;
2002 NULL != source;
2003 source = source->next)
2004 {
2005 bool duplicate = false;
2006 struct CleanLine line;
2007
2008 if (0 != strcmp (source->filename,
2009 target))
2010 continue;
2011 for (const struct ConfigSource *other = ent->sources;
2012 NULL != other;
2013 other = other->next)
2014 {
2015 if ( (0 == strcmp (source->value,
2016 other->value)) &&
2017 ( (source->lineno != other->lineno) ||
2018 (0 != strcmp (source->filename,
2019 other->filename)) ) )
2020 {
2021 duplicate = true;
2022 break;
2023 }
2024 }
2025 if ( (! duplicate) ||
2026 (NULL != find_clean_line (lines,
2027 lines_length,
2028 source->lineno)) )
2029 continue;
2030 line.section = sec->name;
2031 line.option = ent->key;
2032 line.line = source->lineno;
2033 line.remove = true;
2034 GNUNET_array_append (lines,
2035 lines_length,
2036 line);
2037 }
2038
2039 /* Keep the highest-precedence target duplicate needed to preserve every
2040 effective value after all of the tentative removals. */
2041 for (const struct ConfigSection *sec = cfg->sections;
2042 NULL != sec;
2043 sec = sec->next)
2044 for (const struct ConfigEntry *ent = sec->entries;
2045 NULL != ent;
2046 ent = ent->next)
2047 {
2048 const struct ConfigSource *survivor = NULL;
2049
2050 for (const struct ConfigSource *source = ent->sources;
2051 NULL != source;
2052 source = source->next)
2054 target,
2055 lines,
2056 lines_length))
2057 {
2058 survivor = source;
2059 break;
2060 }
2061 if ( (NULL != survivor) &&
2062 (0 == strcmp (survivor->value,
2063 ent->val)) )
2064 continue;
2065 for (const struct ConfigSource *source = ent->sources;
2066 NULL != source;
2067 source = source->next)
2068 {
2069 struct CleanLine *line;
2070
2071 if ( (0 != strcmp (source->filename,
2072 target)) ||
2073 (0 != strcmp (source->value,
2074 ent->val)) )
2075 continue;
2076 line = find_clean_line (lines,
2077 lines_length,
2078 source->lineno);
2079 if ( (NULL != line) && line->remove)
2080 {
2081 line->remove = false;
2082 break;
2083 }
2084 }
2085 }
2086
2087 if (lines_length > 1)
2088 qsort (lines,
2089 lines_length,
2090 sizeof (*lines),
2092 {
2093 unsigned int remove_count = 0;
2094
2095 for (unsigned int i = 0; i < lines_length; i++)
2096 if (lines[i].remove)
2097 remove_count++;
2098 if (0 == remove_count)
2099 {
2100 if (NULL != removed)
2101 *removed = 0;
2102 ret = GNUNET_OK;
2103 goto cleanup;
2104 }
2105
2106 cleaned = GNUNET_malloc (size);
2107 for (size_t offset = 0, line_number = 1; offset < size; line_number++)
2108 {
2109 size_t end = offset;
2110 struct CleanLine *line;
2111
2112 while ( (end < size) && ('\n' != contents[end]) )
2113 end++;
2114 if (end < size)
2115 end++;
2116 line = find_clean_line (lines,
2117 lines_length,
2118 (unsigned int) line_number);
2119 if ( (NULL == line) || (! line->remove) )
2120 {
2121 GNUNET_memcpy (&cleaned[cleaned_size],
2122 &contents[offset],
2123 end - offset);
2124 cleaned_size += end - offset;
2125 }
2126 offset = end;
2127 }
2128 if (GNUNET_OK !=
2129 write_buffer_atomic (target,
2130 cleaned,
2131 cleaned_size,
2132 &parsed_file->content_hash))
2133 goto cleanup;
2134 if (NULL != removed)
2135 *removed = remove_count;
2136 if (NULL != cb)
2137 for (unsigned int i = 0; i < lines_length; i++)
2138 if (lines[i].remove)
2139 cb (cb_cls,
2140 lines[i].section,
2141 lines[i].option,
2142 lines[i].line);
2143 ret = GNUNET_OK;
2144 }
2145
2146cleanup:
2147 GNUNET_free (cleaned);
2148 GNUNET_free (contents);
2149 GNUNET_array_grow (lines,
2150 lines_length,
2151 0);
2152 GNUNET_free (target);
2153 return ret;
2154}
2155
2156
2157void
2159 const struct GNUNET_CONFIGURATION_Handle *cfg,
2161 void *iter_cls)
2162{
2163 for (struct ConfigSection *spos = cfg->sections;
2164 NULL != spos;
2165 spos = spos->next)
2166 for (struct ConfigEntry *epos = spos->entries;
2167 NULL != epos;
2168 epos = epos->next)
2169 if (NULL != epos->val)
2170 iter (iter_cls,
2171 spos->name,
2172 epos->key,
2173 epos->val);
2174}
2175
2176
2177void
2179 const struct GNUNET_CONFIGURATION_Handle *cfg,
2180 const char *section,
2182 void *iter_cls)
2183{
2184 struct ConfigSection *spos;
2185
2186 spos = cfg->sections;
2187 while ((spos != NULL) && (0 != strcasecmp (spos->name, section)))
2188 spos = spos->next;
2189 if (NULL == spos)
2190 return;
2191 for (struct ConfigEntry *epos = spos->entries;
2192 NULL != epos;
2193 epos = epos->next)
2194 if (NULL != epos->val)
2195 iter (iter_cls,
2196 spos->name,
2197 epos->key,
2198 epos->val);
2199 if (spos->inaccessible)
2200 {
2202 "Section '%s' is marked as inaccessible, because a configuration "
2203 "file that contains the section can't be read.\n",
2204 section);
2205 return;
2206 }
2207}
2208
2209
2210void
2212 const struct GNUNET_CONFIGURATION_Handle *cfg,
2214 void *iter_cls)
2215{
2216 struct ConfigSection *spos;
2217 struct ConfigSection *next;
2218
2219 next = cfg->sections;
2220 while (NULL != next)
2221 {
2222 spos = next;
2223 next = spos->next;
2224 if (spos->inaccessible && (NULL == spos->entries))
2225 continue;
2226 iter (iter_cls,
2227 spos->name);
2228 }
2229}
2230
2231
2232void
2234 const char *section)
2235{
2236 struct ConfigSection *spos;
2237 struct ConfigSection *prev;
2238 struct ConfigEntry *ent;
2239
2240 prev = NULL;
2241 spos = cfg->sections;
2242 while (NULL != spos)
2243 {
2244 if (0 == strcasecmp (section,
2245 spos->name))
2246 {
2247 if (NULL == prev)
2248 cfg->sections = spos->next;
2249 else
2250 prev->next = spos->next;
2251 while (NULL != (ent = spos->entries))
2252 {
2253 struct ConfigSource *source;
2254
2255 spos->entries = ent->next;
2256 GNUNET_free (ent->key);
2257 GNUNET_free (ent->val);
2258 while (NULL != (source = ent->sources))
2259 {
2260 ent->sources = source->next;
2261 GNUNET_free (source->value);
2262 GNUNET_free (source->filename);
2264 }
2265 GNUNET_free (ent);
2266 cfg->dirty = GNUNET_YES;
2267 }
2268 GNUNET_free (spos->name);
2272 GNUNET_free (spos);
2273 return;
2274 }
2275 prev = spos;
2276 spos = spos->next;
2277 }
2278}
2279
2280
2290static void
2291copy_entry (void *cls,
2292 const char *section,
2293 const char *option,
2294 const char *value)
2295{
2296 struct GNUNET_CONFIGURATION_Handle *dst = cls;
2297
2299 section,
2300 option,
2301 value);
2302}
2303
2304
2307 const struct GNUNET_CONFIGURATION_Handle *cfg)
2308{
2310
2313 &copy_entry,
2314 ret);
2315 return ret;
2316}
2317
2318
2329static void
2330compare_entries (void *cls,
2331 const char *section,
2332 const char *option,
2333 const char *value)
2334{
2335 struct DiffHandle *dh = cls;
2336 struct ConfigEntry *entNew;
2337
2338 entNew = find_entry (dh->cfg_default, section, option);
2339 if ((NULL != entNew) && (NULL != entNew->val) &&
2340 (0 == strcmp (entNew->val, value)))
2341 return;
2343 section,
2344 option,
2345 value);
2346}
2347
2348
2351 const struct GNUNET_CONFIGURATION_Handle *cfg_default,
2352 const struct GNUNET_CONFIGURATION_Handle *cfg_new)
2353{
2354 struct DiffHandle diffHandle;
2355
2356 GNUNET_break (cfg_default->pd == cfg_new->pd);
2357 diffHandle.cfgDiff = GNUNET_CONFIGURATION_create (cfg_new->pd);
2358 diffHandle.cfg_default = cfg_default;
2361 &diffHandle);
2362 return diffHandle.cfgDiff;
2363}
2364
2365
2369 const struct GNUNET_CONFIGURATION_Handle *cfg_new,
2370 const char *filename)
2371{
2372 int ret;
2373 struct GNUNET_CONFIGURATION_Handle *diff;
2374
2375 diff = GNUNET_CONFIGURATION_get_diff (cfg_default, cfg_new);
2378 return ret;
2379}
2380
2381
2382void
2385 const char *section,
2386 const char *option,
2387 const char *value)
2388{
2389 struct ConfigSection *sec;
2390 struct ConfigEntry *e;
2391 char *nv;
2392
2393 e = find_entry (cfg, section, option);
2394 if (NULL != e)
2395 {
2396 if (NULL == value)
2397 {
2398 GNUNET_free (e->val);
2399 e->val = NULL;
2400 }
2401 else
2402 {
2403 nv = GNUNET_strdup (value);
2404 GNUNET_free (e->val);
2405 e->val = nv;
2406 }
2407 return;
2408 }
2409 sec = find_section (cfg, section);
2410 if (sec == NULL)
2411 {
2412 sec = GNUNET_new (struct ConfigSection);
2413 sec->name = GNUNET_strdup (section);
2414 sec->next = cfg->sections;
2415 cfg->sections = sec;
2416 }
2417 e = GNUNET_new (struct ConfigEntry);
2418 e->key = GNUNET_strdup (option);
2419 e->val = GNUNET_strdup (value);
2420 e->next = sec->entries;
2421 sec->entries = e;
2422}
2423
2424
2425void
2427 const char *section,
2428 const char *option,
2429 unsigned long long number)
2430{
2431 char s[64];
2432
2433 GNUNET_snprintf (s,
2434 64,
2435 "%llu",
2436 number);
2438 section,
2439 option,
2440 s);
2441}
2442
2443
2446 const struct GNUNET_CONFIGURATION_Handle *cfg,
2447 const char *section,
2448 const char *option,
2449 unsigned long long *number)
2450{
2451 struct ConfigEntry *e;
2452 char dummy[2];
2453
2454 if (NULL == (e = find_entry (cfg,
2455 section,
2456 option)))
2457 return GNUNET_NO;
2458 if (NULL == e->val)
2459 return GNUNET_NO;
2460 if (1 != sscanf (e->val,
2461 "%llu%1s",
2462 number,
2463 dummy))
2464 return GNUNET_SYSERR;
2465 return GNUNET_OK;
2466}
2467
2468
2469void
2471 const char *section,
2472 const char *option,
2473 float number)
2474{
2475 char s[64];
2476
2477 /* See: #9369 */
2478 const locale_t cl = newlocale (LC_NUMERIC_MASK,
2479 "C",
2480 (locale_t) 0);
2481 locale_t old_locale;
2482
2483 GNUNET_assert ((locale_t) 0 != cl);
2484 old_locale = uselocale (cl);
2485 GNUNET_snprintf (s,
2486 64,
2487 "%f",
2488 (double) number);
2489 uselocale (old_locale);
2490 freelocale (cl);
2492 section,
2493 option,
2494 s);
2495}
2496
2497
2500 const struct GNUNET_CONFIGURATION_Handle *cfg,
2501 const char *section,
2502 const char *option,
2503 float *number)
2504{
2505 struct ConfigEntry *e;
2506 char dummy[2];
2507
2508 if (NULL == (e = find_entry (cfg,
2509 section,
2510 option)))
2511 return GNUNET_NO;
2512 if (NULL == e->val)
2513 return GNUNET_NO;
2514 /* See #9369 */
2515 {
2516 const locale_t cl = newlocale (LC_NUMERIC_MASK,
2517 "C",
2518 (locale_t) 0);
2519 locale_t old_locale;
2520 int n;
2521
2522 GNUNET_assert ((locale_t) 0 != cl);
2523 old_locale = uselocale (cl);
2524 n = sscanf (e->val,
2525 "%f%1s",
2526 number,
2527 dummy);
2528 uselocale (old_locale);
2529 freelocale (cl);
2530 if (1 != n)
2531 return GNUNET_SYSERR;
2532 }
2533 return GNUNET_OK;
2534}
2535
2536
2539 const struct GNUNET_CONFIGURATION_Handle *cfg,
2540 const char *section,
2541 const char *option,
2542 struct GNUNET_TIME_Relative *time)
2543{
2544 struct ConfigEntry *e;
2545 int ret;
2546
2547 if (NULL == (e = find_entry (cfg,
2548 section,
2549 option)))
2550 return GNUNET_NO;
2551 if (NULL == e->val)
2552 return GNUNET_NO;
2554 time);
2555 if (GNUNET_OK != ret)
2557 section,
2558 option,
2559 _ ("Not a valid relative time specification"));
2560 return ret;
2561}
2562
2563
2566 const struct GNUNET_CONFIGURATION_Handle *cfg,
2567 const char *section,
2568 const char *option,
2569 unsigned long long *size)
2570{
2571 struct ConfigEntry *e;
2572
2573 if (NULL == (e = find_entry (cfg,
2574 section,
2575 option)))
2576 return GNUNET_NO;
2577 if (NULL == e->val)
2578 return GNUNET_NO;
2580 size);
2581}
2582
2583
2596 const struct GNUNET_CONFIGURATION_Handle *cfg,
2597 const char *section,
2598 const char *option,
2599 char **value)
2600{
2601 struct ConfigEntry *e;
2602
2603 if ( (NULL == (e = find_entry (cfg,
2604 section,
2605 option))) ||
2606 (NULL == e->val) )
2607 {
2608 *value = NULL;
2609 return GNUNET_NO;
2610 }
2611 *value = GNUNET_strdup (e->val);
2612 return GNUNET_OK;
2613}
2614
2615
2618 const struct GNUNET_CONFIGURATION_Handle *cfg,
2619 const char *section,
2620 const char *option,
2621 const char *const *choices,
2622 const char **value)
2623{
2624 struct ConfigEntry *e;
2625 unsigned int i;
2626
2627 if (NULL == (e = find_entry (cfg,
2628 section,
2629 option)))
2630 return GNUNET_NO;
2631 for (i = 0; NULL != choices[i]; i++)
2632 if (0 == strcasecmp (choices[i], e->val))
2633 break;
2634 if (NULL == choices[i])
2635 {
2637 _ ("Configuration value '%s' for '%s'"
2638 " in section '%s' is not in set of legal choices\n"),
2639 e->val,
2640 option,
2641 section);
2642 return GNUNET_SYSERR;
2643 }
2644 *value = choices[i];
2645 return GNUNET_OK;
2646}
2647
2648
2651 const char *section,
2652 const char *option,
2653 void *buf,
2654 size_t buf_size)
2655{
2656 char *enc;
2657 int res;
2658 size_t data_size;
2659
2660 if (GNUNET_OK !=
2661 (res =
2663 section,
2664 option,
2665 &enc)))
2666 return res;
2667 data_size = (strlen (enc) * 5) / 8;
2668 if (data_size != buf_size)
2669 {
2670 GNUNET_free (enc);
2671 return GNUNET_SYSERR;
2672 }
2673 if (GNUNET_OK !=
2675 strlen (enc),
2676 buf,
2677 buf_size))
2678 {
2679 GNUNET_free (enc);
2680 return GNUNET_SYSERR;
2681 }
2682 GNUNET_free (enc);
2683 return GNUNET_OK;
2684}
2685
2686
2689 const char *section,
2690 const char *option)
2691{
2692 struct ConfigEntry *e;
2693
2694 if ( (NULL == (e = find_entry (cfg,
2695 section,
2696 option))) ||
2697 (NULL == e->val) )
2698 return GNUNET_NO;
2699 return GNUNET_YES;
2700}
2701
2702
2707struct ExpansionFrame
2708{
2712 const struct ExpansionFrame *parent;
2713
2717 const char *name;
2718};
2719
2720
2721static char *
2723 char *orig,
2724 const struct ExpansionFrame *frame);
2725
2726
2747static char *
2749 char *orig,
2750 unsigned int depth,
2751 const struct ExpansionFrame *frame,
2752 size_t *skip)
2753{
2754 char *prefix;
2755 char *result;
2756 char *start;
2757 const char *post;
2758 const char *env;
2759 char *def;
2760 char *end;
2761 unsigned int lopen;
2762 char erased_char;
2763 char *erased_pos;
2764 size_t len;
2765 size_t def_skip;
2766 bool recursive = false;
2767 struct ExpansionFrame inner;
2768
2769 *skip = 1;
2770 if (NULL == orig)
2771 return NULL;
2772 if (depth > 128)
2773 {
2775 "Recursive expansion suspected, aborting $-expansion for term `%s'\n",
2776 orig);
2777 return orig;
2778 }
2780 "Asked to $-expand %s\n",
2781 orig);
2782 if ('$' != orig[0])
2783 {
2785 "Doesn't start with $ - not expanding\n");
2786 return orig;
2787 }
2788 erased_char = 0;
2789 erased_pos = NULL;
2790 if ('{' == orig[1])
2791 {
2792 start = &orig[2];
2793 lopen = 1;
2794 end = &orig[1];
2795 while (lopen > 0)
2796 {
2797 end++;
2798 switch (*end)
2799 {
2800 case '}':
2801 lopen--;
2802 break;
2803
2804 case '{':
2805 lopen++;
2806 break;
2807
2808 case '\0':
2810 "Missing closing `}' in option `%s'\n",
2811 orig);
2812 return orig;
2813
2814 default:
2815 break;
2816 }
2817 }
2818 erased_char = *end;
2819 erased_pos = end;
2820 *end = '\0';
2821 post = end + 1;
2822 def = strchr (orig, ':');
2823 if (NULL != def)
2824 {
2825 *def = '\0';
2826 def++;
2827 if (('-' == *def) || ('=' == *def))
2828 def++;
2829 def = GNUNET_strdup (def);
2830 }
2831 }
2832 else
2833 {
2834 int i;
2835
2836 start = &orig[1];
2837 def = NULL;
2838 i = 0;
2839 while ( (orig[i] != '/') &&
2840 (orig[i] != '\\') &&
2841 (orig[i] != '\0') &&
2842 (orig[i] != ' ') )
2843 i++;
2844 if (orig[i] == '\0')
2845 {
2846 post = "";
2847 }
2848 else
2849 {
2850 erased_char = orig[i];
2851 erased_pos = &orig[i];
2852 orig[i] = '\0';
2853 post = &orig[i + 1];
2854 }
2855 }
2857 "Split into `%s' and `%s' with default %s\n",
2858 start,
2859 post,
2860 def);
2861 for (const struct ExpansionFrame *pos = frame;
2862 NULL != pos;
2863 pos = pos->parent)
2864 {
2865 if (0 == strcmp (pos->name,
2866 start))
2867 {
2869 "The definition of `%s' refers to itself, not expanding it\n",
2870 start);
2871 recursive = true;
2872 break;
2873 }
2874 }
2875 if (recursive ||
2876 (GNUNET_OK !=
2878 "PATHS",
2879 start,
2880 &prefix)))
2881 {
2882 if (recursive ||
2883 (NULL == (env = getenv (start))))
2884 {
2885 /* try default */
2886 def = expand_dollar (cfg,
2887 def,
2888 depth + 1,
2889 frame,
2890 &def_skip);
2891 env = def;
2892 }
2893 if (NULL == env)
2894 {
2896 if (erased_pos)
2897 *erased_pos = erased_char;
2899 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n",
2900 start,
2901 orig);
2903 return orig;
2904 }
2906 }
2907 inner.parent = frame;
2908 inner.name = start;
2910 prefix,
2911 &inner);
2912 if ((erased_pos) && ('}' != erased_char))
2913 {
2914 len = strlen (prefix) + 1;
2915 prefix = GNUNET_realloc (prefix, len + 1);
2916 prefix[len - 1] = erased_char;
2917 prefix[len] = '\0';
2918 }
2919 *skip = strlen (prefix);
2920 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
2921 strcpy (result, prefix);
2922 strcat (result, post);
2923 GNUNET_free (def);
2925 GNUNET_free (orig);
2926 return result;
2927}
2928
2929
2938static char *
2940 char *orig,
2941 const struct ExpansionFrame *frame)
2942{
2943 char *dup;
2944 size_t i;
2945 size_t len;
2946 size_t skip;
2947
2948 i = 0;
2949 while ('\0' != orig[i])
2950 {
2951 if ('$' != orig[i])
2952 {
2953 i++;
2954 continue;
2955 }
2956 dup = GNUNET_strdup (orig + i);
2957 dup = expand_dollar (cfg, dup, 0, frame, &skip);
2958 GNUNET_assert (NULL != dup); /* make compiler happy */
2959 len = strlen (dup) + 1;
2960 orig = GNUNET_realloc (orig, i + len);
2961 GNUNET_memcpy (orig + i, dup, len);
2962 GNUNET_free (dup);
2963 /* The expansion is final; continue right after it, which may well be
2964 the end of the string or another '$' if it expanded to "". */
2965 i += skip;
2966 }
2967 return orig;
2968}
2969
2970
2971char *
2973 const struct GNUNET_CONFIGURATION_Handle *cfg,
2974 char *orig)
2975{
2976 return expand_all (cfg,
2977 orig,
2978 NULL);
2979}
2980
2981
2984 const struct GNUNET_CONFIGURATION_Handle *cfg,
2985 const char *section,
2986 const char *option,
2987 char **value)
2988{
2989 char *tmp;
2991
2993 section,
2994 option,
2995 &tmp);
2996 if (GNUNET_OK != ret)
2997 {
2999 "Failed to retrieve filename\n");
3000 *value = NULL;
3001 return ret;
3002 }
3004 tmp);
3006 GNUNET_free (tmp);
3007 if (*value == NULL)
3008 return GNUNET_SYSERR;
3009 return GNUNET_OK;
3010}
3011
3012
3015 const struct GNUNET_CONFIGURATION_Handle *cfg,
3016 const char *section,
3017 const char *option)
3018{
3019 static const char *yesno[] = {
3020 "YES",
3021 "NO",
3022 NULL
3023 };
3024 const char *val;
3026
3027 ret =
3029 section,
3030 option,
3031 yesno,
3032 &val);
3033 if (GNUNET_OK != ret)
3034 return ret;
3035 if (val == yesno[0])
3036 return GNUNET_YES;
3037 return GNUNET_NO;
3038}
3039
3040
3041int
3043 const struct GNUNET_CONFIGURATION_Handle *cfg,
3044 const char *section,
3045 const char *option,
3047 void *cb_cls)
3048{
3049 char *list;
3050 char *pos;
3051 char *end;
3052 char old;
3053 int ret;
3054
3055 if (GNUNET_OK !=
3057 section,
3058 option,
3059 &list))
3060 return 0;
3061 GNUNET_assert (list != NULL);
3062 ret = 0;
3063 pos = list;
3064 while (1)
3065 {
3066 while (pos[0] == ' ')
3067 pos++;
3068 if (strlen (pos) == 0)
3069 break;
3070 end = pos + 1;
3071 while ((end[0] != ' ') && (end[0] != '\0'))
3072 {
3073 if (end[0] == '\\')
3074 {
3075 switch (end[1])
3076 {
3077 case '\\':
3078 case ' ':
3079 memmove (end, &end[1], strlen (&end[1]) + 1);
3080
3081 case '\0':
3082 /* illegal, but just keep it */
3083 break;
3084
3085 default:
3086 /* illegal, but just ignore that there was a '/' */
3087 break;
3088 }
3089 }
3090 end++;
3091 }
3092 old = end[0];
3093 end[0] = '\0';
3094 if (strlen (pos) > 0)
3095 {
3096 ret++;
3097 if ( (NULL != cb) &&
3098 (GNUNET_OK != cb (cb_cls,
3099 pos)))
3100 {
3102 break;
3103 }
3104 }
3105 if (old == '\0')
3106 break;
3107 pos = end + 1;
3108 }
3109 GNUNET_free (list);
3110 return ret;
3111}
3112
3113
3120static char *
3121escape_name (const char *value)
3122{
3123 char *escaped;
3124 const char *rpos;
3125 char *wpos;
3126
3127 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
3128 memset (escaped, 0, strlen (value) * 2 + 1);
3129 rpos = value;
3130 wpos = escaped;
3131 while (rpos[0] != '\0')
3132 {
3133 switch (rpos[0])
3134 {
3135 case '\\':
3136 case ' ':
3137 wpos[0] = '\\';
3138 wpos[1] = rpos[0];
3139 wpos += 2;
3140 break;
3141
3142 default:
3143 wpos[0] = rpos[0];
3144 wpos++;
3145 }
3146 rpos++;
3147 }
3148 return escaped;
3149}
3150
3151
3159static enum GNUNET_GenericReturnValue
3160test_match (void *cls,
3161 const char *fn)
3162{
3163 const char *of = cls;
3164
3165 return (0 == strcmp (of, fn))
3167 : GNUNET_OK;
3168}
3169
3170
3174 const char *section,
3175 const char *option,
3176 const char *value)
3177{
3178 char *escaped;
3179 char *old;
3180 char *nw;
3181
3182 if (GNUNET_SYSERR ==
3184 section,
3185 option,
3186 &test_match,
3187 (void *) value))
3188 return GNUNET_NO; /* already exists */
3189 if (GNUNET_OK !=
3190 GNUNET_CONFIGURATION_get_value_string (cfg, section, option, &old))
3191 old = GNUNET_strdup ("");
3192 escaped = escape_name (value);
3193 nw = GNUNET_malloc (strlen (old) + strlen (escaped) + 2);
3194 strcpy (nw, old);
3195 if (strlen (old) > 0)
3196 strcat (nw, " ");
3197 strcat (nw, escaped);
3199 section,
3200 option,
3201 nw);
3202 GNUNET_free (old);
3203 GNUNET_free (nw);
3204 GNUNET_free (escaped);
3205 return GNUNET_OK;
3206}
3207
3208
3212 const char *section,
3213 const char *option,
3214 const char *value)
3215{
3216 char *list;
3217 char *pos;
3218 char *end;
3219 char *match;
3220 char old;
3221
3222 if (GNUNET_OK !=
3224 section,
3225 option,
3226 &list))
3227 return GNUNET_NO;
3228 match = escape_name (value);
3229 pos = list;
3230 while (1)
3231 {
3232 while (pos[0] == ' ')
3233 pos++;
3234 if (strlen (pos) == 0)
3235 break;
3236 end = pos + 1;
3237 while ((end[0] != ' ') && (end[0] != '\0'))
3238 {
3239 if (end[0] == '\\')
3240 {
3241 switch (end[1])
3242 {
3243 case '\\':
3244 case ' ':
3245 end++;
3246 break;
3247
3248 case '\0':
3249 /* illegal, but just keep it */
3250 break;
3251
3252 default:
3253 /* illegal, but just ignore that there was a '/' */
3254 break;
3255 }
3256 }
3257 end++;
3258 }
3259 old = end[0];
3260 end[0] = '\0';
3261 if (0 == strcmp (pos, match))
3262 {
3263 if (old != '\0')
3264 memmove (pos,
3265 &end[1],
3266 strlen (&end[1]) + 1);
3267 else
3268 {
3269 if (pos != list)
3270 pos[-1] = '\0';
3271 else
3272 pos[0] = '\0';
3273 }
3275 section,
3276 option,
3277 list);
3278 GNUNET_free (list);
3279 GNUNET_free (match);
3280 return GNUNET_OK;
3281 }
3282 if (old == '\0')
3283 break;
3284 end[0] = old;
3285 pos = end + 1;
3286 }
3287 GNUNET_free (list);
3288 GNUNET_free (match);
3289 return GNUNET_NO;
3290}
3291
3292
3296 const char *defaults_d)
3297{
3298 struct CollectFilesContext files_context = {
3299 .files = NULL,
3300 .files_length = 0,
3301 };
3302 enum GNUNET_GenericReturnValue fun_ret = GNUNET_NO;
3303
3304 if (GNUNET_SYSERR ==
3305 GNUNET_DISK_directory_scan (defaults_d,
3307 &files_context))
3308 return GNUNET_SYSERR; /* no configuration at all found */
3309 qsort (files_context.files,
3310 files_context.files_length,
3311 sizeof (char *),
3312 &pstrcmp);
3313 for (unsigned int i = 0; i < files_context.files_length; i++)
3314 {
3315 const char *ext;
3316 const char *filename = files_context.files[i];
3317
3318 /* Examine file extension */
3319 ext = strrchr (filename, '.');
3320 if ( (NULL == ext) ||
3321 (0 != strcmp (ext,
3322 ".conf")) )
3323 {
3325 "Skipping file `%s'\n",
3326 filename);
3327 fun_ret = GNUNET_OK;
3328 continue;
3329 }
3331 filename);
3332 if (fun_ret != GNUNET_OK)
3333 break;
3334 }
3335 if (files_context.files_length > 0)
3336 {
3337 for (size_t i = 0; i < files_context.files_length; i++)
3338 GNUNET_free (files_context.files[i]);
3339 GNUNET_array_grow (files_context.files,
3340 files_context.files_length,
3341 0);
3342 }
3343 return fun_ret;
3344}
3345
3346
3347char *
3349 const struct GNUNET_OS_ProjectData *pd)
3350{
3351 char *cfg_fn;
3352 const char *xdg = getenv ("XDG_CONFIG_HOME");
3353
3354 if (NULL != xdg)
3355 GNUNET_asprintf (&cfg_fn,
3356 "%s%s%s",
3357 xdg,
3359 pd->config_file);
3360 else
3361 cfg_fn = GNUNET_strdup (pd->user_config_file);
3362
3363 if (GNUNET_OK == GNUNET_DISK_file_test_read (cfg_fn))
3364 return cfg_fn;
3365 GNUNET_free (cfg_fn);
3366
3367 /* Fall back to /etc/ for the default configuration.
3368 Should be okay to use forward slashes here. */
3369
3370 GNUNET_asprintf (&cfg_fn,
3371 "/etc/%s",
3372 pd->config_file);
3373 if (GNUNET_OK ==
3375 return cfg_fn;
3376 GNUNET_free (cfg_fn);
3377
3378 GNUNET_asprintf (&cfg_fn,
3379 "/etc/%s/%s",
3380 pd->project_dirname,
3381 pd->config_file);
3382 if (GNUNET_OK ==
3384 return cfg_fn;
3385
3386 GNUNET_free (cfg_fn);
3387 return NULL;
3388}
3389
3390
3394 const char *filename)
3395{
3396 char *baseconfig;
3397 const char *base_config_varname;
3398
3399 if (cfg->load_called)
3400 {
3401 /* FIXME: Make this a GNUNET_assert later */
3402 GNUNET_break (0);
3404 }
3405 cfg->load_called = true;
3406 if (NULL != filename)
3407 {
3410 }
3411
3412 base_config_varname = cfg->pd->base_config_varname;
3413
3414 if ((NULL != base_config_varname)
3415 && (NULL != (baseconfig = getenv (base_config_varname))))
3416 {
3417 baseconfig = GNUNET_strdup (baseconfig);
3418 }
3419 else
3420 {
3421 char *ipath;
3422
3425 if (NULL == ipath)
3426 {
3427 GNUNET_break (0);
3428 return GNUNET_SYSERR;
3429 }
3430 GNUNET_asprintf (&baseconfig,
3431 "%s%s",
3432 ipath,
3433 "config.d");
3434 GNUNET_free (ipath);
3435 }
3436
3437 {
3438 char *dname = GNUNET_STRINGS_filename_expand (baseconfig);
3439
3440 GNUNET_free (baseconfig);
3441
3442 if ((GNUNET_YES ==
3444 GNUNET_YES)) &&
3445 (GNUNET_SYSERR ==
3447 dname)))
3448 {
3450 "Failed to load base configuration from '%s'\n",
3451 filename);
3452 GNUNET_free (dname);
3453 return GNUNET_SYSERR; /* no configuration at all found */
3454 }
3455 GNUNET_free (dname);
3456 }
3457 if ((NULL != filename) &&
3458 (GNUNET_OK !=
3460 filename)))
3461 {
3462 /* specified configuration not found */
3464 "Failed to load configuration from file '%s'\n",
3465 filename);
3466 return GNUNET_SYSERR;
3467 }
3468 if (((GNUNET_YES !=
3470 "PATHS",
3471 "DEFAULTCONFIG"))) &&
3472 (filename != NULL))
3474 "PATHS",
3475 "DEFAULTCONFIG",
3476 filename);
3477 return GNUNET_OK;
3478}
3479
3480
3489static void
3490print_filename_option (void *cls,
3491 const char *section,
3492 const char *option,
3493 const char *value)
3494{
3495 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
3496
3497 char *value_fn;
3498 char *fn;
3499
3502 section,
3503 option,
3504 &value_fn));
3505 fn = GNUNET_STRINGS_filename_expand (value_fn);
3506 if (NULL == fn)
3507 fn = value_fn;
3508 else
3509 GNUNET_free (value_fn);
3510 fprintf (stdout,
3511 "%s = %s\n",
3512 option,
3513 fn);
3514 GNUNET_free (fn);
3515}
3516
3517
3526static void
3527print_option (void *cls,
3528 const char *section,
3529 const char *option,
3530 const char *value)
3531{
3532 (void) cls;
3533 (void) section;
3534
3535 fprintf (stdout,
3536 "%s = %s\n",
3537 option,
3538 value);
3539}
3540
3541
3548static void
3549print_section_name (void *cls,
3550 const char *section)
3551{
3552 (void) cls;
3553 fprintf (stdout,
3554 "%s\n",
3555 section);
3556}
3557
3558
3559void
3561 void *cls,
3562 char *const *args,
3563 const char *cfgfile,
3564 const struct GNUNET_CONFIGURATION_Handle *cfg)
3565{
3566 struct GNUNET_CONFIGURATION_ConfigSettings *cs = cls;
3567 struct GNUNET_CONFIGURATION_Handle *out = NULL;
3568 struct GNUNET_CONFIGURATION_Handle *ncfg = NULL;
3569
3570 (void) args;
3571 if (cs->diagnostics &&
3572 (! cfg->diagnostics))
3573 {
3574 /* Re-parse the configuration with diagnostics enabled. */
3577 if (GNUNET_OK !=
3579 cfgfile))
3580 {
3581 fprintf (stderr,
3582 _ ("Failed to load config file `%s'"),
3583 cfgfile);
3584 return;
3585 }
3586 cfg = ncfg;
3587 }
3588
3589 if (cs->full)
3590 cs->rewrite = GNUNET_YES;
3591 if (cs->list_sections)
3592 {
3593 fprintf (stderr,
3594 _ ("The following sections are available:\n"));
3597 NULL);
3598 return;
3599 }
3600 if ( (! cs->rewrite) &&
3601 (NULL == cs->section) )
3602 {
3603 char *serialization;
3604
3605 if (! cs->diagnostics)
3606 {
3607 fprintf (stderr,
3608 _ ("%s, %s or %s argument is required\n"),
3609 "--section",
3610 "--list-sections",
3611 "--diagnostics");
3613 return;
3614 }
3615 serialization = (cs->diagnostics > 1)
3618 fprintf (stdout,
3619 "%s",
3620 serialization);
3621 GNUNET_free (serialization);
3622 }
3623 else if ( (NULL != cs->section) &&
3624 (NULL == cs->value) )
3625 {
3626 if (NULL == cs->option)
3627 {
3629 cfg,
3630 cs->section,
3631 cs->is_filename
3633 : &print_option,
3634 (void *) cfg);
3635 }
3636 else
3637 {
3638 char *value;
3639
3640 if (cs->is_filename)
3641 {
3642 if (GNUNET_OK !=
3644 cs->section,
3645 cs->option,
3646 &value))
3647 {
3649 cs->section,
3650 cs->option);
3652 return;
3653 }
3654 }
3655 else
3656 {
3657 if (GNUNET_OK !=
3659 cs->section,
3660 cs->option,
3661 &value))
3662 {
3664 cs->section,
3665 cs->option);
3667 return;
3668 }
3669 }
3670 fprintf (stdout,
3671 "%s\n",
3672 value);
3674 }
3675 }
3676 else if (NULL != cs->section)
3677 {
3678 if (NULL == cs->option)
3679 {
3680 fprintf (stderr,
3681 _ ("--option argument required to set value\n"));
3683 return;
3684 }
3687 cs->section,
3688 cs->option,
3689 cs->value);
3690 cs->rewrite = GNUNET_YES;
3691 }
3692 if (cs->rewrite)
3693 {
3694 char *cfg_fn = NULL;
3695
3696 if (NULL == out)
3698
3699 if (NULL == cfgfile)
3700 {
3701 const char *xdg = getenv ("XDG_CONFIG_HOME");
3702
3703 if (NULL != xdg)
3704 GNUNET_asprintf (&cfg_fn,
3705 "%s%s%s",
3706 xdg,
3708 cfg->pd->config_file);
3709 else
3710 cfg_fn = GNUNET_strdup (cfg->pd->user_config_file);
3711 cfgfile = cfg_fn;
3712 }
3713
3714 if (! cs->full)
3715 {
3716 struct GNUNET_CONFIGURATION_Handle *def;
3717
3719 if (GNUNET_OK !=
3721 NULL))
3722 {
3723 fprintf (stderr,
3724 _ ("failed to load configuration defaults"));
3725 cs->global_ret = 1;
3728 GNUNET_free (cfg_fn);
3729 return;
3730 }
3731 if (GNUNET_OK !=
3733 out,
3734 cfgfile))
3735 cs->global_ret = 2;
3737 }
3738 else
3739 {
3740 if (GNUNET_OK !=
3742 cfgfile))
3743 cs->global_ret = 2;
3744 }
3745 GNUNET_free (cfg_fn);
3746 }
3747 if (NULL != out)
3749 if (NULL != ncfg)
3751}
3752
3753
3754void
3757{
3758 GNUNET_free (cs->option);
3759 GNUNET_free (cs->section);
3760 GNUNET_free (cs->value);
3761}
3762
3763
3764const struct GNUNET_OS_ProjectData *
3766 const struct GNUNET_CONFIGURATION_Handle *cfg)
3767{
3768 return cfg->pd;
3769}
3770
3771
3772char *
3774 pd,
3775 const struct
3777 const char *progname)
3778{
3779 static char *cache;
3780 char *binary = NULL;
3781 char *path = NULL;
3782 size_t path_len;
3783
3784 if (GNUNET_YES ==
3786 GNUNET_NO,
3787 NULL,
3788 NULL))
3789 {
3790 return GNUNET_strdup (progname);
3791 }
3792 if (NULL != cache)
3793 path = cache;
3794 else
3796 "PATHS",
3797 "SUID_BINARY_PATH",
3798 &path);
3799 if ( (NULL == path) ||
3800 (0 == strlen (path)) )
3801 {
3802 if (NULL != path)
3803 GNUNET_free (path);
3804 cache = NULL;
3806 progname);
3807 }
3808 path_len = strlen (path);
3809 GNUNET_asprintf (&binary,
3810 "%s%s%s",
3811 path,
3812 (path[path_len - 1] == DIR_SEPARATOR) ? ""
3814 progname);
3815 cache = path;
3816 return binary;
3817}
3818
3819
3820/* end of configuration.c */
struct GNUNET_MQ_Envelope * env
Definition 005.c:1
static bool do_skip(const char *sec, const char *key)
Should we skip this configuration entry when serializing?
static void print_filename_option(void *cls, const char *section, const char *option, const char *value)
Print each option in a given section as a filename.
static char * expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, unsigned int depth, const struct ExpansionFrame *frame, size_t *skip)
Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or...
static bool source_is_removed(const struct ConfigSource *source, const char *target, struct CleanLine *lines, unsigned int lines_length)
Return whether a source is a target line currently marked for removal.
static struct CleanLine * find_clean_line(struct CleanLine *lines, unsigned int lines_length, unsigned int line)
Find a cleanup record for a physical line.
static int clean_line_cmp(const void *a, const void *b)
Sort cleanup records by source line.
static struct ConfigEntry * find_entry(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *key)
Find an entry from a configuration.
static enum GNUNET_GenericReturnValue handle_inline(struct GNUNET_CONFIGURATION_Handle *cfg, const char *path_or_glob, bool path_is_glob, const char *restrict_section, const char *source_filename, unsigned int source_lineno)
Handle an inline directive.
static void print_section_name(void *cls, const char *section)
Print out given section name.
static enum GNUNET_GenericReturnValue read_file(const char *filename, char **contents, size_t *size)
static char * escape_name(const char *value)
FIXME.
static void compare_entries(void *cls, const char *section, const char *option, const char *value)
A callback function, compares entries from two configurations (default against a new configuration) a...
static void add_entry_source(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value, const char *filename, unsigned int line)
Record the source of a configuration value.
static char * serialize_diagnostics(const struct GNUNET_CONFIGURATION_Handle *cfg, bool verbose)
Serialize configuration diagnostics.
static enum GNUNET_GenericReturnValue test_match(void *cls, const char *fn)
FIXME.
static char * expand_all(const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig, const struct ExpansionFrame *frame)
$-expand all terms in a string.
static struct ConfigSection * find_section(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Find a section entry from a configuration.
static enum GNUNET_GenericReturnValue write_buffer_atomic(const char *filename, const char *contents, size_t size, const struct GNUNET_HashCode *expected_hash)
Atomically replace a file with the given bytes.
static void copy_entry_with_sources(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const struct ConfigEntry *entry)
Copy an entry parsed through an @inline-secret@ directive, including all diagnostic source informatio...
static int pstrcmp(const void *a, const void *b)
static enum GNUNET_GenericReturnValue collect_files_cb(void *cls, const char *filename)
Function called with a filename.
#define LOG(kind,...)
static bool file_hash_matches(const char *filename, const struct GNUNET_HashCode *expected)
Check that a file still has the expected contents.
static void copy_entry(void *cls, const char *section, const char *option, const char *value)
Copy a configuration value to the given target configuration.
static void print_option(void *cls, const char *section, const char *option, const char *value)
Print each option in a given section.
static void dump_os_ipk(const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_Buffer *buf, enum GNUNET_OS_InstallationPathKind ipk)
Dump an os installation path to a buffer.
char * getenv()
static struct GNUNET_ARM_Handle * h
Connection with ARM.
Definition gnunet-arm.c:98
static int start
Set if we are to start default services (including ARM).
Definition gnunet-arm.c:38
static int list
Set if we should print a list of currently running services.
Definition gnunet-arm.c:68
static int ret
Final status code.
Definition gnunet-arm.c:93
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
static int end
Set if we are to shutdown all services (including ARM).
Definition gnunet-arm.c:33
static int prefix
If printing the value of PREFIX has been requested.
static char * line
Desired phone line (string to be converted to a hash).
static unsigned int verbose
Be verbose.
struct GNUNET_HashCode key
The key used in the DHT.
static char * filename
static struct GNUNET_FS_Handle * fs
Handle to FS service.
Definition gnunet-fs.c:35
static GstElement * source
Appsrc instance into which we write data for the pipeline.
static OpusEncoder * enc
OPUS encoder.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static char * res
Currently read line or NULL on EOF.
static char * value
Value of the record to add/remove.
static size_t data_size
Number of bytes in data.
static int status
The program status; 0 for success.
Definition gnunet-nse.c:39
static int result
Global testing status.
static void cleanup()
Cleanup task.
static struct GNUNET_Process * p
Helper process we started.
Definition gnunet-uri.c:38
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_data(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, void *buf, size_t buf_size)
Get Crockford32-encoded fixed-size binary data from a configuration.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_create(const struct GNUNET_OS_ProjectData *pd)
Create a new configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_is_dirty(const struct GNUNET_CONFIGURATION_Handle *cfg)
Test if there are configuration options that were changed since the last save.
void GNUNET_CONFIGURATION_set_value_string(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Set a configuration value that should be a string.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_append_value_filename(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Append a filename to a configuration value that represents a list of filenames.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_filename(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be the name of a file or directory.
void GNUNET_CONFIGURATION_remove_section(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section)
Remove the given section and all options in it.
enum GNUNET_GenericReturnValue(* GNUNET_CONFIGURATION_Callback)(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
Signature of a function to be run with a configuration.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_dup(const struct GNUNET_CONFIGURATION_Handle *cfg)
Duplicate an existing configuration object.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
void GNUNET_CONFIGURATION_iterate_sections(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_SectionIterator iter, void *iter_cls)
Iterate over all sections in the configuration.
void GNUNET_CONFIGURATION_destroy(struct GNUNET_CONFIGURATION_Handle *cfg)
Destroy configuration object.
char * GNUNET_CONFIGURATION_get_suid_binary_path(const struct GNUNET_OS_ProjectData *pd, const struct GNUNET_CONFIGURATION_Handle *cfg, const char *progname)
Given the name of a helper, service or daemon binary construct the full path to the binary using the ...
void GNUNET_CONFIGURATION_config_settings_free(struct GNUNET_CONFIGURATION_ConfigSettings *cs)
Free resources associated with cs.
void GNUNET_CONFIGURATION_iterate(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over all options in the configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_remove_value_filename(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *value)
Remove a filename from a configuration value that represents a list of filenames.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse_and_run(const struct GNUNET_OS_ProjectData *pd, const char *filename, GNUNET_CONFIGURATION_Callback cb, void *cb_cls)
Parse a configuration file filename and run the function cb with the resulting configuration object.
void GNUNET_CONFIGURATION_config_tool_run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main task to run to perform operations typical for gnunet-config as per the configuration settings gi...
void(* GNUNET_CONFIGURATION_SectionIterator)(void *cls, const char *section)
Function to iterate over all configuration sections.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_deserialize(struct GNUNET_CONFIGURATION_Handle *cfg, const char *mem, size_t size, const char *source_filename)
De-serializes configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_size(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *size)
Get a configuration value that should be a size in bytes.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
char * GNUNET_CONFIGURATION_serialize(const struct GNUNET_CONFIGURATION_Handle *cfg, size_t *size)
Serializes the given configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
void GNUNET_CONFIGURATION_set_value_number(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long number)
Set a configuration value that should be a number.
char * GNUNET_CONFIGURATION_default_filename(const struct GNUNET_OS_ProjectData *pd)
Return the filename of the default configuration filename that is used when no explicit configuration...
char * GNUNET_CONFIGURATION_expand_dollar(const struct GNUNET_CONFIGURATION_Handle *cfg, char *orig)
Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or...
void GNUNET_CONFIGURATION_set_value_float(struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, float number)
Set a configuration value that should be a float.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_clean_file(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename, GNUNET_CONFIGURATION_CleanCallback cb, void *cb_cls, unsigned int *removed)
Remove redundant option definitions from a parsed configuration file.
const struct GNUNET_OS_ProjectData * GNUNET_CONFIGURATION_get_project_data(const struct GNUNET_CONFIGURATION_Handle *cfg)
Return the project data associated with this configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Load configuration.
struct GNUNET_CONFIGURATION_Handle * GNUNET_CONFIGURATION_get_diff(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new)
Compute configuration with only entries that have been changed.
char * GNUNET_CONFIGURATION_serialize_diagnostics_verbose(const struct GNUNET_CONFIGURATION_Handle *cfg)
Serializes the given configuration with verbose diagnostics information, including all source definit...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_parse(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Parse a configuration file, add all of the options in the file to the configuration environment.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_float(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, float *number)
Get a configuration value that should be a floating point number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_load_from(struct GNUNET_CONFIGURATION_Handle *cfg, const char *defaults_d)
Load default configuration.
int GNUNET_CONFIGURATION_iterate_value_filenames(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, GNUNET_FileNameCallback cb, void *cb_cls)
Iterate over the set of filenames stored in a configuration value.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_have_value(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Test if we have a value for a particular option.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_choice(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, const char *const *choices, const char **value)
Get a configuration value that should be in a set of predefined strings.
void(* GNUNET_CONFIGURATION_CleanCallback)(void *cls, const char *section, const char *option, unsigned int line)
Function called for each duplicate definition removed by GNUNET_CONFIGURATION_clean_file().
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write(struct GNUNET_CONFIGURATION_Handle *cfg, const char *filename)
Write configuration file.
void(* GNUNET_CONFIGURATION_Iterator)(void *cls, const char *section, const char *option, const char *value)
Function to iterate over options.
void GNUNET_CONFIGURATION_iterate_section_values(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, GNUNET_CONFIGURATION_Iterator iter, void *iter_cls)
Iterate over values of a section in the configuration.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_write_diffs(const struct GNUNET_CONFIGURATION_Handle *cfg_default, const struct GNUNET_CONFIGURATION_Handle *cfg_new, const char *filename)
Write only configuration entries that have been changed to configuration file.
char * GNUNET_CONFIGURATION_serialize_diagnostics(const struct GNUNET_CONFIGURATION_Handle *cfg)
Serializes the given configuration with diagnostics information.
void GNUNET_CONFIGURATION_enable_diagnostics(struct GNUNET_CONFIGURATION_Handle *cfg)
Enable extra diagnostics.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition disk.c:1307
int GNUNET_DISK_glob(const char *glob_pattern, GNUNET_FileNameCallback callback, void *callback_cls)
Find all files matching a glob pattern.
Definition disk.c:1062
ssize_t GNUNET_DISK_file_write(const struct GNUNET_DISK_FileHandle *h, const void *buffer, size_t n)
Write a buffer to a file.
Definition disk.c:745
enum GNUNET_GenericReturnValue GNUNET_DISK_file_test_read(const char *fil)
Check that fil corresponds to a filename and the file has read permissions.
Definition disk.c:565
enum GNUNET_GenericReturnValue GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition disk.c:235
enum GNUNET_GenericReturnValue GNUNET_DISK_file_sync(const struct GNUNET_DISK_FileHandle *h)
Write file changes to disk.
Definition disk.c:1506
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_test(const char *fil, int is_readable)
Test if fil is a directory and listable.
Definition disk.c:466
enum GNUNET_GenericReturnValue GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition disk.c:1385
enum GNUNET_GenericReturnValue GNUNET_DISK_directory_create_for_file(const char *filename)
Create the directory structure for storing a file.
Definition disk.c:664
ssize_t GNUNET_DISK_fn_read(const char *fn, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition disk.c:720
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition disk.c:881
@ GNUNET_DISK_OPEN_WRITE
Open the file for writing.
@ GNUNET_DISK_OPEN_TRUNCATE
Truncate file if it exists.
@ GNUNET_DISK_OPEN_CREATE
Create file if it doesn't exist.
@ GNUNET_DISK_PERM_USER_READ
Owner can read.
@ GNUNET_DISK_PERM_GROUP_READ
Group can read.
@ GNUNET_DISK_PERM_GROUP_WRITE
Group can write.
@ GNUNET_DISK_PERM_USER_WRITE
Owner can write.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition crypto_hash.c:40
enum GNUNET_GenericReturnValue(* GNUNET_FileNameCallback)(void *cls, const char *filename)
Function called with a filename.
#define GNUNET_log(kind,...)
char * GNUNET_buffer_reap_str(struct GNUNET_Buffer *buf)
Clear the buffer and return the string it contained.
Definition buffer.c:89
void GNUNET_buffer_write_fstr(struct GNUNET_Buffer *buf, const char *fmt,...) __attribute__((format(printf
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
void GNUNET_buffer_write_str(struct GNUNET_Buffer *buf, const char *str)
Write a 0-terminated string to a buffer, excluding the 0-terminator.
Definition buffer.c:77
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
void GNUNET_log_config_invalid(enum GNUNET_ErrorType kind, const char *section, const char *option, const char *required)
Log error message about invalid configuration option value.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_array_grow(arr, size, tsize)
Grow a well-typed (!) array.
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define GNUNET_array_append(arr, len, element)
Append an element to an array (growing the array by one).
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_OS_installation_get_path(const struct GNUNET_OS_ProjectData *pd, enum GNUNET_OS_InstallationPathKind dirkind)
Get the path to a specific GNUnet installation directory or, with GNUNET_OS_IPK_SELF_PREFIX,...
GNUNET_OS_InstallationPathKind
Possible installation paths to request.
char * GNUNET_OS_get_libexec_binary_path(const struct GNUNET_OS_ProjectData *pd, const char *progname)
Given the name of a gnunet-helper, gnunet-service or gnunet-daemon binary, try to prefix it with the ...
@ GNUNET_OS_IPK_SELF_PREFIX
Return the installation directory of this application, not the one of the overall GNUnet installation...
@ GNUNET_OS_IPK_ICONDIR
Return the prefix of the path with application icons (share/icons/).
@ GNUNET_OS_IPK_DATADIR
Return the directory where data is installed (share/gnunet/)
@ GNUNET_OS_IPK_DOCDIR
Return the prefix of the path with documentation files, including the license (share/doc/gnunet/).
@ GNUNET_OS_IPK_LOCALEDIR
Return the directory where translations are installed (share/locale/)
@ GNUNET_OS_IPK_LIBDIR
Return the directory where libraries are installed.
@ GNUNET_OS_IPK_PREFIX
Return the "PREFIX" directory given to configure.
@ GNUNET_OS_IPK_BINDIR
Return the directory where the program binaries are installed.
@ GNUNET_OS_IPK_LIBEXECDIR
Return the directory where helper binaries are installed (lib/gnunet/libexec/)
char * GNUNET_STRINGS_filename_expand(const char *fil)
Complete filename (a la shell) from abbrevition.
Definition strings.c:531
enum GNUNET_GenericReturnValue GNUNET_STRINGS_string_to_data(const char *enc, size_t enclen, void *out, size_t out_size)
Convert CrockfordBase32 encoding back to data.
Definition strings.c:843
enum GNUNET_GenericReturnValue GNUNET_STRINGS_path_is_absolute(const char *filename, int can_be_uri, int *r_is_uri, char **r_uri_scheme)
Check whether filename is absolute or not, and if it's an URI.
Definition strings.c:1012
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_time_to_relative(const char *fancy_time, struct GNUNET_TIME_Relative *rtime)
Convert a given fancy human-readable time to our internal representation.
Definition strings.c:259
enum GNUNET_GenericReturnValue GNUNET_STRINGS_fancy_size_to_bytes(const char *fancy_size, unsigned long long *size)
Convert a given fancy human-readable size to bytes.
Definition strings.c:235
static unsigned int size
Size of the "table".
Definition peer.c:68
#define DIR_SEPARATOR
Definition platform.h:166
#define DIR_SEPARATOR_STR
Definition platform.h:167
#define _(String)
GNU gettext support macro.
Definition platform.h:179
#define SIZE_MAX
Definition platform.h:213
#define EXIT_INVALIDARGUMENT
Definition platform.h:258
#define EXIT_NOTCONFIGURED
Definition platform.h:274
uint32_t number
One physical assignment line considered for removal.
const char * option
unsigned int line
const char * section
Closure to collect_files_cb.
char ** files
Collected files from globbing.
unsigned int files_length
Size of the files array.
configuration entry
struct ConfigEntry * next
This is a linked list.
char * key
key for this entry
struct ConfigSource * sources
Source definitions retained for diagnostics.
char * val
current, committed value
char * hint_restrict_section
Was this configuration file parsed via @inline-secret@?
struct ConfigFile * next
char * source_filename
Source filename.
bool hint_inaccessible
Was this configuration file inaccessible?
struct ConfigFile * prev
bool have_content_hash
Whether content_hash is available.
unsigned int level
Level in the tree of loaded config files.
struct GNUNET_HashCode content_hash
Hash of the file contents seen while parsing.
configuration section
char * name
name of the section
char * hint_inlined_from_filename
For secret sections: Where was this inlined from?
struct ConfigEntry * entries
entries in the section
bool inaccessible
Is the configuration section marked as inaccessible?
struct ConfigSection * next
This is a linked list.
char * hint_secret_filename
Diagnostics hint for the secret file.
unsigned int hint_inlined_from_line
For secret sections: Where was this inlined from?
char * hint_secret_stat
Extra information regarding permissions of the secret file.
source location of a configuration value
char * value
Value as normalized by the configuration parser.
unsigned int lineno
Line containing the definition.
char * filename
Filename containing the definition.
struct ConfigSource * next
Sources are ordered from highest to lowest precedence.
Used for diffing a configuration object against the default one.
const struct GNUNET_CONFIGURATION_Handle * cfg_default
struct GNUNET_CONFIGURATION_Handle * cfgDiff
A [PATHS] option or environment variable whose value is being $-expanded, used to detect definitions ...
const struct ExpansionFrame * parent
The expansion this one is part of, or NULL.
const char * name
Name of the variable whose value is being expanded.
Dynamically growing buffer.
Closure for GNUNET_CONFIGURATION_config_tool_run() with settings for what should be done with the con...
int full
Should the generated configuration file contain the whole configuration?
int global_ret
Return value from the operation, to be returned from 'main'.
int list_sections
Whether to show the sections.
int rewrite
Should we write out the configuration file, even if no value was changed?
int is_filename
Treat option as a filename.
unsigned int diagnostics
Diagnostics verbosity.
struct ConfigFile * loaded_files_tail
Linked list of loaded files.
bool load_called
Was the configuration ever loaded via GNUNET_CONFIGURATION_load?
unsigned int current_nest_level
Current nesting level of file loading.
const struct GNUNET_OS_ProjectData * pd
Project data for this configuration object.
struct ConfigSection * sections
Configuration sections.
struct ConfigFile * loaded_files_head
Linked list of loaded files.
char * main_filename
Name of the entry point configuration file.
enum GNUNET_GenericReturnValue dirty
Modification indication since last save GNUNET_NO if clean, GNUNET_YES if dirty, GNUNET_SYSERR on err...
bool diagnostics
Enable diagnostics.
const char * restrict_section
When parsing into this configuration, and this value is non-NULL, only parse sections of the same nam...
Handle used to access files (and pipes).
A 512-bit hashcode.
Project-specific data used to help the OS subsystem find installation paths.
const char * base_config_varname
Name of an environment variable that can be used to override the location from which default configur...
const char * project_dirname
Name of the project that is used in the "libexec" prefix, For example, "gnunet".
const char * user_config_file
Configuration file name to use (if $XDG_CONFIG_HOME is not set).
const char * config_file
Configuration file name (in $XDG_CONFIG_HOME) to use.
Time for relative time used by GNUnet, in microseconds.

Function Documentation

◆ collect_files_cb()

static enum GNUNET_GenericReturnValue collect_files_cb ( void *  cls,
const char *  filename 
)
static

Function called with a filename.

Parameters
clsclosure
filenamecomplete filename (absolute path)
Returns
GNUNET_OK to continue to iterate, GNUNET_NO to stop iteration with no error, GNUNET_SYSERR to abort iteration with error!

Definition at line 426 of file configuration.c.

428{
429 struct CollectFilesContext *igc = cls;
430
432 igc->files_length,
434 return GNUNET_OK;
435}

References filename, CollectFilesContext::files, CollectFilesContext::files_length, GNUNET_array_append, GNUNET_OK, and GNUNET_strdup.

Referenced by GNUNET_CONFIGURATION_load_from(), and handle_inline().

Here is the caller graph for this function:

◆ find_section()

static struct ConfigSection * find_section ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
const char *  section 
)
static

Find a section entry from a configuration.

Parameters
cfgconfiguration to search in
sectionname of the section to look for
Returns
matching entry, NULL if not found

Definition at line 446 of file configuration.c.

448{
449 struct ConfigSection *pos;
450
451 if (NULL == cfg)
452 return NULL;
453 pos = cfg->sections;
454 while ( (NULL != pos) &&
455 (0 != strcasecmp (section,
456 pos->name)) )
457 pos = pos->next;
458 return pos;
459}

References cfg, ConfigSection::name, ConfigSection::next, and GNUNET_CONFIGURATION_Handle::sections.

Referenced by find_entry(), GNUNET_CONFIGURATION_set_value_string(), and handle_inline().

Here is the caller graph for this function:

◆ pstrcmp()

static int pstrcmp ( const void *  a,
const void *  b 
)
static

Definition at line 463 of file configuration.c.

465{
466 return strcmp (*((const char **) a),
467 *((const char **) b));
468}

Referenced by GNUNET_CONFIGURATION_load_from(), and handle_inline().

Here is the caller graph for this function:

◆ copy_entry_with_sources()

static void copy_entry_with_sources ( struct GNUNET_CONFIGURATION_Handle *  cfg,
const char *  section,
const struct ConfigEntry *  entry 
)
static

Copy an entry parsed through an @inline-secret@ directive, including all diagnostic source information.

Parameters
cfgdestination configuration
sectiondestination section
entrysource entry

Definition at line 791 of file configuration.c.

794{
795 struct ConfigEntry *dst;
796 struct ConfigSource *head = NULL;
797 struct ConfigSource *tail = NULL;
798
800 section,
801 entry->key,
802 entry->val);
803 if ( (! cfg->diagnostics) ||
804 (NULL == entry->sources) )
805 return;
806 dst = find_entry (cfg,
807 section,
808 entry->key);
809 GNUNET_assert (NULL != dst);
810 for (const struct ConfigSource *pos = entry->sources;
811 NULL != pos;
812 pos = pos->next)
813 {
814 struct ConfigSource *copy = GNUNET_new (struct ConfigSource);
815
816 copy->value = GNUNET_strdup (pos->value);
817 copy->filename = GNUNET_strdup (pos->filename);
818 copy->lineno = pos->lineno;
819 if (NULL == tail)
820 head = copy;
821 else
822 tail->next = copy;
823 tail = copy;
824 }
825 tail->next = dst->sources;
826 dst->sources = head;
827}

References cfg, GNUNET_CONFIGURATION_Handle::diagnostics, ConfigSource::filename, find_entry(), GNUNET_assert, GNUNET_CONFIGURATION_set_value_string(), GNUNET_new, GNUNET_strdup, ConfigEntry::key, ConfigSource::lineno, ConfigSource::next, ConfigEntry::sources, ConfigEntry::val, and ConfigSource::value.

Referenced by handle_inline().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_inline()

static enum GNUNET_GenericReturnValue handle_inline ( struct GNUNET_CONFIGURATION_Handle *  cfg,
const char *  path_or_glob,
bool  path_is_glob,
const char *  restrict_section,
const char *  source_filename,
unsigned int  source_lineno 
)
static

Handle an inline directive.

Returns
GNUNET_SYSERR on error, GNUNET_OK otherwise

Definition at line 483 of file configuration.c.

489{
490 char *inline_path = NULL;
491 struct GNUNET_CONFIGURATION_Handle *other_cfg = NULL;
492 struct CollectFilesContext igc = {
493 .files = NULL,
494 .files_length = 0,
495 };
496 enum GNUNET_GenericReturnValue fun_ret;
497 unsigned int old_nest_level = cfg->current_nest_level++;
498
499 /* We support the section restriction only for non-globs */
500 GNUNET_assert (! (path_is_glob && (NULL != restrict_section)));
501
502 if (NULL == source_filename)
503 {
505 "Refusing to parse inline configurations, "
506 "not allowed without source filename!\n");
507 fun_ret = GNUNET_SYSERR;
508 goto cleanup;
509 }
510
511 if ('/' == *path_or_glob)
512 inline_path = GNUNET_strdup (path_or_glob);
513 else
514 {
515 /* We compute the canonical, absolute path first,
516 so that relative imports resolve properly with symlinked
517 config files. */
518 char *source_realpath;
519 char *endsep;
520
521 source_realpath = realpath (source_filename,
522 NULL);
523 if (NULL == source_realpath)
524 {
525 /* Couldn't even resolve path of base dir. */
526 GNUNET_break (0);
527 /* failed to parse included config */
528 fun_ret = GNUNET_SYSERR;
529 goto cleanup;
530 }
531 endsep = strrchr (source_realpath, '/');
532 GNUNET_assert (NULL != endsep);
533 *endsep = '\0';
534 GNUNET_asprintf (&inline_path,
535 "%s/%s",
536 source_realpath,
537 path_or_glob);
538 free (source_realpath);
539 }
540
541 if (path_is_glob)
542 {
543 int nret;
544
546 "processing config glob '%s'\n",
547 inline_path);
548
549 nret = GNUNET_DISK_glob (inline_path,
551 &igc);
552 if (-1 == nret)
553 {
554 fun_ret = GNUNET_SYSERR;
555 goto cleanup;
556 }
557 GNUNET_assert (nret == igc.files_length);
558 qsort (igc.files,
559 igc.files_length,
560 sizeof (char *),
561 &pstrcmp);
562 for (int i = 0; i < nret; i++)
563 {
564 if (GNUNET_OK !=
566 igc.files[i]))
567 {
568 fun_ret = GNUNET_SYSERR;
569 goto cleanup;
570 }
571 }
572 fun_ret = GNUNET_OK;
573 }
574 else if (NULL != restrict_section)
575 {
576 enum GNUNET_GenericReturnValue inner_ret;
577 struct ConfigSection *cs;
578 struct ConfigFile *cf = GNUNET_new (struct ConfigFile);
579
580 inner_ret = GNUNET_DISK_file_test_read (inline_path);
581
582 cs = find_section (cfg, restrict_section);
583
584 if (NULL == cs)
585 {
586 cs = GNUNET_new (struct ConfigSection);
587 cs->name = GNUNET_strdup (restrict_section);
588 cs->next = cfg->sections;
589 cfg->sections = cs;
590 cs->entries = NULL;
591 }
592 if (cfg->diagnostics)
593 {
594 const char *pwname;
595 const char *grname;
596 char *sfn = GNUNET_STRINGS_filename_expand (inline_path);
597 struct stat istat;
598
599 cs->hint_secret_filename = sfn;
600 if (0 == stat (sfn, &istat))
601 {
602 struct passwd *pw = getpwuid (istat.st_uid);
603 struct group *gr = getgrgid (istat.st_gid);
604 pwname = (NULL == pw) ? "<unknown>" : pw->pw_name;
605 grname = (NULL == gr) ? "<unknown>" : gr->gr_name;
606
608 "%s:%s %o",
609 pwname,
610 grname,
611 istat.st_mode);
612 }
613 else
614 {
615 cs->hint_secret_stat = GNUNET_strdup ("<can't stat file>");
616 }
617 if (source_filename)
618 {
619 /* Possible that this secret section has been inlined before */
621 cs->hint_inlined_from_filename = GNUNET_strdup (source_filename);
622 cs->hint_inlined_from_line = source_lineno;
623 }
624 }
625
626 /* Put file in the load list for diagnostics, even if we can't access it. */
627 {
629 cf->source_filename = GNUNET_strdup (inline_path);
630 cf->hint_restrict_section = GNUNET_strdup (restrict_section);
633 cf);
634 }
635
636 if (GNUNET_OK != inner_ret)
637 {
638 cs->inaccessible = true;
639 cf->hint_inaccessible = true;
640 /* File can't be accessed, but that's okay. */
641 fun_ret = GNUNET_OK;
642 goto cleanup;
643 }
644
645 other_cfg = GNUNET_CONFIGURATION_create (cfg->pd);
646 if (cfg->diagnostics)
648 other_cfg->restrict_section = restrict_section;
649 inner_ret = GNUNET_CONFIGURATION_parse (other_cfg,
650 inline_path);
651 if (GNUNET_OK != inner_ret)
652 {
653 cf->hint_inaccessible = true;
654 fun_ret = inner_ret;
655 goto cleanup;
656 }
657
658 cs = find_section (other_cfg,
659 restrict_section);
660 if (NULL == cs)
661 {
663 "Configuration file '%s' loaded with @inline-secret@ "
664 "does not contain section '%s'.\n",
665 inline_path,
666 restrict_section);
667 /* Inlined configuration is accessible but doesn't contain any values.
668 We treat this as if the inlined section was empty, and do not
669 consider it an error. */
670 fun_ret = GNUNET_OK;
671 goto cleanup;
672 }
673 for (struct ConfigEntry *ce = cs->entries;
674 NULL != ce;
675 ce = ce->next)
677 restrict_section,
678 ce);
679 fun_ret = GNUNET_OK;
680 }
681 else if (GNUNET_OK !=
683 inline_path))
684 {
685 fun_ret = GNUNET_SYSERR;
686 goto cleanup;
687 }
688 else
689 {
690 fun_ret = GNUNET_OK;
691 }
692cleanup:
693 cfg->current_nest_level = old_nest_level;
694 if (NULL != other_cfg)
696 GNUNET_free (inline_path);
697 if (igc.files_length > 0)
698 {
699 for (size_t i = 0; i < igc.files_length; i++)
700 GNUNET_free (igc.files[i]);
702 }
703 return fun_ret;
704}

References cfg, cleanup(), collect_files_cb(), copy_entry_with_sources(), GNUNET_CONFIGURATION_Handle::current_nest_level, GNUNET_CONFIGURATION_Handle::diagnostics, ConfigSection::entries, CollectFilesContext::files, CollectFilesContext::files_length, find_section(), GNUNET_array_grow, GNUNET_asprintf(), GNUNET_assert, GNUNET_break, GNUNET_CONFIGURATION_create(), GNUNET_CONFIGURATION_destroy(), GNUNET_CONFIGURATION_enable_diagnostics(), GNUNET_CONFIGURATION_parse(), GNUNET_CONTAINER_DLL_insert_tail, GNUNET_DISK_file_test_read(), GNUNET_DISK_glob(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_INFO, GNUNET_free, GNUNET_new, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, ConfigFile::hint_inaccessible, ConfigSection::hint_inlined_from_filename, ConfigSection::hint_inlined_from_line, ConfigFile::hint_restrict_section, ConfigSection::hint_secret_filename, ConfigSection::hint_secret_stat, ConfigSection::inaccessible, ConfigFile::level, GNUNET_CONFIGURATION_Handle::loaded_files_head, GNUNET_CONFIGURATION_Handle::loaded_files_tail, LOG, ConfigSection::name, ConfigSection::next, GNUNET_CONFIGURATION_Handle::pd, pstrcmp(), GNUNET_CONFIGURATION_Handle::restrict_section, GNUNET_CONFIGURATION_Handle::sections, and ConfigFile::source_filename.

Referenced by GNUNET_CONFIGURATION_deserialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_entry()

static struct ConfigEntry * find_entry ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
const char *  section,
const char *  key 
)
static

Find an entry from a configuration.

Parameters
cfghandle to the configuration
sectionsection the option is in
keythe option
Returns
matching entry, NULL if not found

Definition at line 716 of file configuration.c.

719{
720 struct ConfigSection *sec;
721
722 if (NULL == (sec = find_section (cfg,
723 section)))
724 return NULL;
725 for (struct ConfigEntry *pos = sec->entries;
726 pos != NULL;
727 pos = pos->next)
728 if (0 == strcasecmp (key,
729 pos->key))
730 return pos;
731 if (sec->inaccessible)
732 {
734 "Section '%s' is marked as inaccessible, because the configuration "
735 "file that contains the section can't be read. Attempts to use "
736 "option '%s' will fail.\n",
737 section,
738 key);
739 return NULL;
740 }
741 return NULL;
742}

References cfg, ConfigSection::entries, find_section(), GNUNET_ERROR_TYPE_WARNING, ConfigSection::inaccessible, key, and LOG.

Referenced by add_entry_source(), compare_entries(), copy_entry_with_sources(), GNUNET_CONFIGURATION_get_value_choice(), GNUNET_CONFIGURATION_get_value_float(), GNUNET_CONFIGURATION_get_value_number(), GNUNET_CONFIGURATION_get_value_size(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_CONFIGURATION_get_value_time(), GNUNET_CONFIGURATION_have_value(), and GNUNET_CONFIGURATION_set_value_string().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_entry_source()

static void add_entry_source ( struct GNUNET_CONFIGURATION_Handle *  cfg,
const char *  section,
const char *  option,
const char *  value,
const char *  filename,
unsigned int  line 
)
static

Record the source of a configuration value.

Parameters
cfgconfiguration handle
sectionsection
optionconfig option
valueparsed value
filenamesource filename
linesource line

Definition at line 756 of file configuration.c.

762{
763 struct ConfigEntry *e;
764 struct ConfigSource *source;
765
766 if (! cfg->diagnostics)
767 return;
768 e = find_entry (cfg,
769 section,
770 option);
771 if (NULL == e)
772 return;
773 source = GNUNET_new (struct ConfigSource);
774 source->value = GNUNET_strdup (value);
775 source->filename = GNUNET_strdup (filename);
776 source->lineno = line;
777 source->next = e->sources;
778 e->sources = source;
779}

References cfg, GNUNET_CONFIGURATION_Handle::diagnostics, filename, find_entry(), GNUNET_new, GNUNET_strdup, line, source, ConfigEntry::sources, and value.

Referenced by GNUNET_CONFIGURATION_deserialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_skip()

static bool do_skip ( const char *  sec,
const char *  key 
)
static

Should we skip this configuration entry when serializing?

Parameters
secsection name
keykey
Returns
true if we should skip it

Definition at line 1258 of file configuration.c.

1260{
1261 if (0 != strcasecmp ("PATHS",
1262 sec))
1263 return false;
1264 return ( (0 == strcasecmp ("DATADIR",
1265 key)) ||
1266 (0 == strcasecmp ("LIBDIR",
1267 key)) ||
1268 (0 == strcasecmp ("BINDIR",
1269 key)) ||
1270 (0 == strcasecmp ("PREFIX",
1271 key)) ||
1272 (0 == strcasecmp ("LOCALEDIR",
1273 key)) ||
1274 (0 == strcasecmp ("ICONDIR",
1275 key)) ||
1276 (0 == strcasecmp ("DOCDIR",
1277 key)) ||
1278 (0 == strcasecmp ("DEFAULTCONFIG",
1279 key)) ||
1280 (0 == strcasecmp ("LIBEXECDIR",
1281 key)) );
1282}

References key.

Referenced by GNUNET_CONFIGURATION_serialize(), and serialize_diagnostics().

Here is the caller graph for this function:

◆ dump_os_ipk()

static void dump_os_ipk ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
struct GNUNET_Buffer *  buf,
enum GNUNET_OS_InstallationPathKind  ipk 
)
static

Dump an os installation path to a buffer.

Parameters
cfgconfiguration
bufbuffer to write to
ipkwhich path to print

Definition at line 1395 of file configuration.c.

1399{
1401 ipk);
1402 const char *ipkname;
1403 switch (ipk)
1404 {
1406 ipkname = "IPK_DATADIR";
1407 break;
1409 ipkname = "IPK_BINDIR";
1410 break;
1412 ipkname = "IPK_LIBDIR";
1413 break;
1415 ipkname = "IPK_PREFIX";
1417 ipkname = "IPK_LOCALEDIR";
1418 break;
1420 ipkname = "IPK_ICONDIR";
1422 ipkname = "IPK_DOCDIR";
1423 break;
1425 ipkname = "IPK_LIBEXECDIR";
1426 break;
1428 ipkname = "IPK_SELF_PREFIX";
1429 break;
1430 default:
1431 ipkname = "??";
1432 break;
1433 }
1435 "# %s = %s\n",
1436 ipkname,
1437 v);
1438 GNUNET_free (v);
1439}

References cfg, GNUNET_buffer_write_fstr(), GNUNET_free, GNUNET_OS_installation_get_path(), GNUNET_OS_IPK_BINDIR, GNUNET_OS_IPK_DATADIR, GNUNET_OS_IPK_DOCDIR, GNUNET_OS_IPK_ICONDIR, GNUNET_OS_IPK_LIBDIR, GNUNET_OS_IPK_LIBEXECDIR, GNUNET_OS_IPK_LOCALEDIR, GNUNET_OS_IPK_PREFIX, GNUNET_OS_IPK_SELF_PREFIX, and GNUNET_CONFIGURATION_Handle::pd.

Referenced by serialize_diagnostics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serialize_diagnostics()

static char * serialize_diagnostics ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
bool  verbose 
)
static

Serialize configuration diagnostics.

Parameters
cfgconfiguration to serialize
verboseinclude every definition of each value
Returns
allocated diagnostic configuration

Definition at line 1450 of file configuration.c.

1452{
1453 struct GNUNET_Buffer buf = { 0 };
1454
1456 "#\n# Configuration file diagnostics\n#\n");
1458 "# Entry point: %s\n",
1460 "<none>");
1462 "#\n# Files Loaded:\n");
1463
1464 for (struct ConfigFile *cfil = cfg->loaded_files_head;
1465 NULL != cfil;
1466 cfil = cfil->next)
1467 {
1469 "# ");
1470 for (unsigned int i = 0; i < cfil->level; i++)
1472 "+");
1473 if (0 != cfil->level)
1475 " ");
1476
1478 "%s",
1479 cfil->source_filename);
1480
1481 if (NULL != cfil->hint_restrict_section)
1483 " (%s secret section %s)",
1484 cfil->hint_inaccessible
1485 ? "inaccessible"
1486 : "loaded",
1487 cfil->hint_restrict_section);
1488
1490 "\n");
1491 }
1492
1494 "#\n# Installation paths:\n");
1495
1505
1506
1508 "#\n\n");
1509
1510 for (struct ConfigSection *sec = cfg->sections;
1511 NULL != sec;
1512 sec = sec->next)
1513 {
1514 if (sec->hint_secret_filename)
1516 "# secret section from %s\n# secret file stat %s\n",
1517 sec->hint_secret_filename,
1518 sec->hint_secret_stat);
1519 if (sec->hint_inlined_from_filename)
1520 {
1522 "# inlined from %s:%u\n",
1523 sec->hint_inlined_from_filename,
1524 sec->hint_inlined_from_line);
1525 }
1527 "[%s]\n\n",
1528 sec->name);
1529 if (sec->inaccessible && (NULL == sec->entries))
1530 {
1532 "# <section contents inaccessible>\n\n\n");
1533 continue;
1534 }
1535 for (struct ConfigEntry *ent = sec->entries;
1536 NULL != ent;
1537 ent = ent->next)
1538 {
1539 if (do_skip (sec->name,
1540 ent->key))
1541 continue;
1542 if (NULL != ent->val)
1543 {
1544 char *pos;
1545 char *val = GNUNET_malloc (strlen (ent->val) * 2 + 1);
1546 strcpy (val, ent->val);
1547 while (NULL != (pos = strstr (val, "\n")))
1548 {
1549 memmove (&pos[2], &pos[1], strlen (&pos[1]));
1550 pos[0] = '\\';
1551 pos[1] = 'n';
1552 }
1553 if (NULL != ent->sources)
1554 {
1555 if (verbose &&
1556 (NULL != ent->sources->next))
1557 {
1559 &buf,
1560 "# duplicate definitions (highest precedence first):\n");
1561 for (const struct ConfigSource *source = ent->sources;
1562 NULL != source;
1563 source = source->next)
1564 {
1565 const char *status;
1566
1567 if (source == ent->sources)
1568 status = "effective";
1569 else if (0 == strcmp (source->value,
1570 ent->val))
1571 status = "same value";
1572 else
1573 status = "different value";
1575 &buf,
1576 "# %s:%u (%s)\n",
1577 source->filename,
1578 source->lineno,
1579 status);
1580 }
1581 }
1582 else
1583 {
1585 "# %s:%u\n",
1586 ent->sources->filename,
1587 ent->sources->lineno);
1588 }
1589 }
1591 "%s = %s\n",
1592 ent->key,
1593 val);
1594 GNUNET_free (val);
1595 }
1596 GNUNET_buffer_write_str (&buf, "\n");
1597 }
1598 if (sec->inaccessible)
1599 {
1601 "# <additional section contents inaccessible>\n\n\n");
1602 continue;
1603 }
1604
1605 GNUNET_buffer_write_str (&buf, "\n");
1606 }
1607 return GNUNET_buffer_reap_str (&buf);
1608}

References cfg, do_skip(), dump_os_ipk(), GNUNET_buffer_reap_str(), GNUNET_buffer_write_fstr(), GNUNET_buffer_write_str(), GNUNET_free, GNUNET_malloc, GNUNET_OS_IPK_BINDIR, GNUNET_OS_IPK_DATADIR, GNUNET_OS_IPK_DOCDIR, GNUNET_OS_IPK_ICONDIR, GNUNET_OS_IPK_LIBDIR, GNUNET_OS_IPK_LIBEXECDIR, GNUNET_OS_IPK_LOCALEDIR, GNUNET_OS_IPK_PREFIX, GNUNET_OS_IPK_SELF_PREFIX, GNUNET_CONFIGURATION_Handle::loaded_files_head, GNUNET_CONFIGURATION_Handle::main_filename, GNUNET_CONFIGURATION_Handle::sections, source, status, and verbose.

Referenced by GNUNET_CONFIGURATION_serialize_diagnostics(), and GNUNET_CONFIGURATION_serialize_diagnostics_verbose().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_file()

static enum GNUNET_GenericReturnValue read_file ( const char *  filename,
char **  contents,
size_t *  size 
)
static

Definition at line 1630 of file configuration.c.

1633{
1634 uint64_t fs64;
1635 ssize_t ret;
1636
1637 if ( (GNUNET_SYSERR ==
1639 &fs64,
1640 GNUNET_NO,
1641 GNUNET_YES)) ||
1642 (fs64 > SIZE_MAX) )
1643 return GNUNET_SYSERR;
1644 *size = (size_t) fs64;
1645 *contents = GNUNET_malloc (*size);
1647 *contents,
1648 *size);
1649 if ( (ret < 0) || ((size_t) ret != *size) )
1650 {
1651 GNUNET_free (*contents);
1652 *contents = NULL;
1653 return GNUNET_SYSERR;
1654 }
1655 return GNUNET_OK;
1656}

References filename, GNUNET_DISK_file_size(), GNUNET_DISK_fn_read(), GNUNET_free, GNUNET_malloc, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, ret, size, and SIZE_MAX.

Referenced by file_hash_matches(), GNUNET_CONFIGURATION_clean_file(), libgnunet_test_transport_plugin_cmd_udp_backchannel_init(), start_testcase(), start_testcase(), start_testcase(), start_testcase(), start_testcase(), start_testcase(), and start_testcase().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ file_hash_matches()

static bool file_hash_matches ( const char *  filename,
const struct GNUNET_HashCode *  expected 
)
static

Check that a file still has the expected contents.

Parameters
filenamefile to check
expectedexpected hash
Returns
true on match

Definition at line 1667 of file configuration.c.

1669{
1670 struct GNUNET_HashCode actual;
1671 char *contents;
1672 size_t size;
1673
1675 &contents,
1676 &size))
1677 return false;
1678 GNUNET_CRYPTO_hash (contents,
1679 size,
1680 &actual);
1681 GNUNET_free (contents);
1682 return 0 == GNUNET_memcmp (&actual,
1683 expected);
1684}

References filename, GNUNET_CRYPTO_hash(), GNUNET_free, GNUNET_memcmp, GNUNET_OK, read_file(), and size.

Referenced by write_buffer_atomic().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_buffer_atomic()

static enum GNUNET_GenericReturnValue write_buffer_atomic ( const char *  filename,
const char *  contents,
size_t  size,
const struct GNUNET_HashCode *  expected_hash 
)
static

Atomically replace a file with the given bytes.

Parameters
filenametarget file
contentsnew contents
sizenumber of bytes in contents
expected_hashif non-NULL, require the target to still have this hash
Returns
GNUNET_OK on success

Definition at line 1697 of file configuration.c.

1701{
1702 char *fn;
1703 char *tfn;
1704
1706 if (fn == NULL)
1707 return GNUNET_SYSERR;
1709 {
1710 GNUNET_free (fn);
1711 return GNUNET_SYSERR;
1712 }
1713 {
1714 /* If @a filename is a symlink, replace what it points at, the way
1715 truncating it in place used to. */
1716 char *rfn = realpath (fn,
1717 NULL);
1718
1719 if (NULL != rfn)
1720 {
1721 GNUNET_free (fn);
1722 fn = GNUNET_strdup (rfn);
1723 free (rfn);
1724 }
1725 }
1726 /* Never rewrite the configuration in place: opening it with O_TRUNC leaves
1727 it observably empty until we have written the new contents, and anyone
1728 parsing the file in that instant -- say a daemon that was just started
1729 with this very configuration -- reads a truncated file and fails. So
1730 write a sibling file and rename(2) it over the target; readers then
1731 always see either the complete old or the complete new configuration. */
1732 GNUNET_asprintf (&tfn,
1733 "%s.%u.tmp",
1734 fn,
1735 (unsigned int) getpid ());
1736 {
1737 struct GNUNET_DISK_FileHandle *h;
1738
1739 h = GNUNET_DISK_file_open (tfn,
1747 if (NULL == h)
1748 {
1749 GNUNET_free (tfn);
1750 GNUNET_free (fn);
1751 return GNUNET_SYSERR;
1752 }
1753 if (((ssize_t) size) !=
1755 contents,
1756 size))
1757 {
1759 "write",
1760 tfn);
1762 (void) unlink (tfn);
1763 GNUNET_free (tfn);
1764 GNUNET_free (fn);
1765 return GNUNET_SYSERR;
1766 }
1767 /* Get the contents onto the disk before the rename publishes them, so
1768 that a crash cannot leave an empty configuration behind. */
1770 {
1772 "fsync",
1773 tfn);
1774 (void) GNUNET_DISK_file_close (h);
1775 (void) unlink (tfn);
1776 GNUNET_free (tfn);
1777 GNUNET_free (fn);
1778 return GNUNET_SYSERR;
1779 }
1781 {
1783 "close",
1784 tfn);
1785 (void) unlink (tfn);
1786 GNUNET_free (tfn);
1787 GNUNET_free (fn);
1788 return GNUNET_SYSERR;
1789 }
1790 }
1791 {
1792 struct stat sbuf;
1793
1794 /* rename(2) replaces the inode, so carry over the permissions of the
1795 configuration we are about to replace (we just created @a tfn). */
1796 if ( (0 == stat (fn,
1797 &sbuf)) &&
1798 (0 != chmod (tfn,
1799 sbuf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO))) )
1801 "chmod",
1802 tfn);
1803 }
1804 if ( (NULL != expected_hash) &&
1805 (! file_hash_matches (fn,
1806 expected_hash)) )
1807 {
1809 "Configuration file `%s' changed while it was being cleaned\n",
1810 fn);
1811 (void) unlink (tfn);
1812 GNUNET_free (tfn);
1813 GNUNET_free (fn);
1814 return GNUNET_SYSERR;
1815 }
1816 if (0 != rename (tfn,
1817 fn))
1818 {
1820 "rename",
1821 fn);
1822 (void) unlink (tfn);
1823 GNUNET_free (tfn);
1824 GNUNET_free (fn);
1825 return GNUNET_SYSERR;
1826 }
1827 GNUNET_free (tfn);
1828 GNUNET_free (fn);
1829 return GNUNET_OK;
1830}

References file_hash_matches(), filename, GNUNET_asprintf(), GNUNET_DISK_directory_create_for_file(), GNUNET_DISK_file_close(), GNUNET_DISK_file_open(), GNUNET_DISK_file_sync(), GNUNET_DISK_file_write(), GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_OPEN_TRUNCATE, GNUNET_DISK_OPEN_WRITE, GNUNET_DISK_PERM_GROUP_READ, GNUNET_DISK_PERM_GROUP_WRITE, GNUNET_DISK_PERM_USER_READ, GNUNET_DISK_PERM_USER_WRITE, GNUNET_ERROR_TYPE_ERROR, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_log_strerror_file, GNUNET_OK, GNUNET_strdup, GNUNET_STRINGS_filename_expand(), GNUNET_SYSERR, h, LOG, and size.

Referenced by GNUNET_CONFIGURATION_clean_file(), and GNUNET_CONFIGURATION_write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ find_clean_line()

static struct CleanLine * find_clean_line ( struct CleanLine *  lines,
unsigned int  lines_length,
unsigned int  line 
)
static

Find a cleanup record for a physical line.

Definition at line 1870 of file configuration.c.

1873{
1874 for (unsigned int i = 0; i < lines_length; i++)
1875 if (lines[i].line == line)
1876 return &lines[i];
1877 return NULL;
1878}

References line.

Referenced by GNUNET_CONFIGURATION_clean_file(), and source_is_removed().

Here is the caller graph for this function:

◆ source_is_removed()

static bool source_is_removed ( const struct ConfigSource *  source,
const char *  target,
struct CleanLine *  lines,
unsigned int  lines_length 
)
static

Return whether a source is a target line currently marked for removal.

Definition at line 1885 of file configuration.c.

1889{
1890 struct CleanLine *line;
1891
1892 if (0 != strcmp (source->filename,
1893 target))
1894 return false;
1895 line = find_clean_line (lines,
1896 lines_length,
1897 source->lineno);
1898 return (NULL != line) && line->remove;
1899}

References find_clean_line(), line, and source.

Referenced by GNUNET_CONFIGURATION_clean_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ clean_line_cmp()

static int clean_line_cmp ( const void *  a,
const void *  b 
)
static

Sort cleanup records by source line.

Definition at line 1906 of file configuration.c.

1908{
1909 const struct CleanLine *la = a;
1910 const struct CleanLine *lb = b;
1911
1912 if (la->line < lb->line)
1913 return -1;
1914 if (la->line > lb->line)
1915 return 1;
1916 return 0;
1917}

References CleanLine::line.

Referenced by GNUNET_CONFIGURATION_clean_file().

Here is the caller graph for this function:

◆ copy_entry()

static void copy_entry ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

Copy a configuration value to the given target configuration.

Overwrites existing entries.

Parameters
clsthe destination configuration (struct GNUNET_CONFIGURATION_Handle *)
sectionsection for the value
optionoption name of the value
valuevalue to copy

Definition at line 2292 of file configuration.c.

2296{
2297 struct GNUNET_CONFIGURATION_Handle *dst = cls;
2298
2300 section,
2301 option,
2302 value);
2303}

References GNUNET_CONFIGURATION_set_value_string(), and value.

Referenced by GNUNET_CONFIGURATION_dup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ compare_entries()

static void compare_entries ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

A callback function, compares entries from two configurations (default against a new configuration) and write the diffs in a diff-configuration object (the callback object).

Parameters
clsthe diff configuration (struct DiffHandle *)
sectionsection for the value (of the default conf.)
optionoption name of the value (of the default conf.)
valuevalue to copy (of the default conf.)

Definition at line 2331 of file configuration.c.

2335{
2336 struct DiffHandle *dh = cls;
2337 struct ConfigEntry *entNew;
2338
2339 entNew = find_entry (dh->cfg_default, section, option);
2340 if ((NULL != entNew) && (NULL != entNew->val) &&
2341 (0 == strcmp (entNew->val, value)))
2342 return;
2344 section,
2345 option,
2346 value);
2347}

References DiffHandle::cfg_default, DiffHandle::cfgDiff, find_entry(), GNUNET_CONFIGURATION_set_value_string(), ConfigEntry::val, and value.

Referenced by GNUNET_CONFIGURATION_get_diff().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expand_all()

static char * expand_all ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
char *  orig,
const struct ExpansionFrame *  frame 
)
static

$-expand all terms in a string.

Parameters
cfgconfiguration to use for path expansion
origstring to $-expand (will be freed!)
framevariables whose values are being expanded
Returns
$-expanded string

Definition at line 2940 of file configuration.c.

2943{
2944 char *dup;
2945 size_t i;
2946 size_t len;
2947 size_t skip;
2948
2949 i = 0;
2950 while ('\0' != orig[i])
2951 {
2952 if ('$' != orig[i])
2953 {
2954 i++;
2955 continue;
2956 }
2957 dup = GNUNET_strdup (orig + i);
2958 dup = expand_dollar (cfg, dup, 0, frame, &skip);
2959 GNUNET_assert (NULL != dup); /* make compiler happy */
2960 len = strlen (dup) + 1;
2961 orig = GNUNET_realloc (orig, i + len);
2962 GNUNET_memcpy (orig + i, dup, len);
2963 GNUNET_free (dup);
2964 /* The expansion is final; continue right after it, which may well be
2965 the end of the string or another '$' if it expanded to "". */
2966 i += skip;
2967 }
2968 return orig;
2969}

References cfg, expand_dollar(), GNUNET_assert, GNUNET_free, GNUNET_memcpy, GNUNET_realloc, and GNUNET_strdup.

Referenced by expand_dollar(), and GNUNET_CONFIGURATION_expand_dollar().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ expand_dollar()

static char * expand_dollar ( const struct GNUNET_CONFIGURATION_Handle *  cfg,
char *  orig,
unsigned int  depth,
const struct ExpansionFrame *  frame,
size_t *  skip 
)
static

Expand an expression of the form "$FOO/BAR" to "DIRECTORY/BAR" where either in the "PATHS" section or the environment "FOO" is set to "DIRECTORY".

We also support default expansion, i.e. ${VARIABLE:-default} will expand to $VARIABLE if VARIABLE is set in PATHS or the environment, and otherwise to "default". Note that "default" itself can also be a $-expression, thus "${VAR1:-{$VAR2}}" will expand to VAR1 and if that is not defined to VAR2.

Parameters
cfgconfiguration to use for path expansion
origstring to $-expand (will be freed!)
depthrecursion depth, used to detect recursive expansions
framevariables whose values are being expanded, a variable among them is treated as undefined
[out]skipset to the length of the expanded term at the start of the result, or to 1 if the term was not expanded, so that the caller can continue after it
Returns
$-expanded string, never NULL unless orig was NULL

Definition at line 2749 of file configuration.c.

2754{
2755 char *prefix;
2756 char *result;
2757 char *start;
2758 const char *post;
2759 const char *env;
2760 char *def;
2761 char *end;
2762 unsigned int lopen;
2763 char erased_char;
2764 char *erased_pos;
2765 size_t len;
2766 size_t def_skip;
2767 bool recursive = false;
2768 struct ExpansionFrame inner;
2769
2770 *skip = 1;
2771 if (NULL == orig)
2772 return NULL;
2773 if (depth > 128)
2774 {
2776 "Recursive expansion suspected, aborting $-expansion for term `%s'\n",
2777 orig);
2778 return orig;
2779 }
2781 "Asked to $-expand %s\n",
2782 orig);
2783 if ('$' != orig[0])
2784 {
2786 "Doesn't start with $ - not expanding\n");
2787 return orig;
2788 }
2789 erased_char = 0;
2790 erased_pos = NULL;
2791 if ('{' == orig[1])
2792 {
2793 start = &orig[2];
2794 lopen = 1;
2795 end = &orig[1];
2796 while (lopen > 0)
2797 {
2798 end++;
2799 switch (*end)
2800 {
2801 case '}':
2802 lopen--;
2803 break;
2804
2805 case '{':
2806 lopen++;
2807 break;
2808
2809 case '\0':
2811 "Missing closing `}' in option `%s'\n",
2812 orig);
2813 return orig;
2814
2815 default:
2816 break;
2817 }
2818 }
2819 erased_char = *end;
2820 erased_pos = end;
2821 *end = '\0';
2822 post = end + 1;
2823 def = strchr (orig, ':');
2824 if (NULL != def)
2825 {
2826 *def = '\0';
2827 def++;
2828 if (('-' == *def) || ('=' == *def))
2829 def++;
2830 def = GNUNET_strdup (def);
2831 }
2832 }
2833 else
2834 {
2835 int i;
2836
2837 start = &orig[1];
2838 def = NULL;
2839 i = 0;
2840 while ( (orig[i] != '/') &&
2841 (orig[i] != '\\') &&
2842 (orig[i] != '\0') &&
2843 (orig[i] != ' ') )
2844 i++;
2845 if (orig[i] == '\0')
2846 {
2847 post = "";
2848 }
2849 else
2850 {
2851 erased_char = orig[i];
2852 erased_pos = &orig[i];
2853 orig[i] = '\0';
2854 post = &orig[i + 1];
2855 }
2856 }
2858 "Split into `%s' and `%s' with default %s\n",
2859 start,
2860 post,
2861 def);
2862 for (const struct ExpansionFrame *pos = frame;
2863 NULL != pos;
2864 pos = pos->parent)
2865 {
2866 if (0 == strcmp (pos->name,
2867 start))
2868 {
2870 "The definition of `%s' refers to itself, not expanding it\n",
2871 start);
2872 recursive = true;
2873 break;
2874 }
2875 }
2876 if (recursive ||
2877 (GNUNET_OK !=
2879 "PATHS",
2880 start,
2881 &prefix)))
2882 {
2883 if (recursive ||
2884 (NULL == (env = getenv (start))))
2885 {
2886 /* try default */
2887 def = expand_dollar (cfg,
2888 def,
2889 depth + 1,
2890 frame,
2891 &def_skip);
2892 env = def;
2893 }
2894 if (NULL == env)
2895 {
2897 if (erased_pos)
2898 *erased_pos = erased_char;
2900 "Failed to expand `%s' in `%s' as it is neither found in [PATHS] nor defined as an environmental variable\n",
2901 start,
2902 orig);
2904 return orig;
2905 }
2907 }
2908 inner.parent = frame;
2909 inner.name = start;
2911 prefix,
2912 &inner);
2913 if ((erased_pos) && ('}' != erased_char))
2914 {
2915 len = strlen (prefix) + 1;
2916 prefix = GNUNET_realloc (prefix, len + 1);
2917 prefix[len - 1] = erased_char;
2918 prefix[len] = '\0';
2919 }
2920 *skip = strlen (prefix);
2921 result = GNUNET_malloc (strlen (prefix) + strlen (post) + 1);
2922 strcpy (result, prefix);
2923 strcat (result, post);
2924 GNUNET_free (def);
2926 GNUNET_free (orig);
2927 return result;
2928}

References cfg, end, env, expand_all(), expand_dollar(), getenv(), GNUNET_CONFIGURATION_get_value_string(), GNUNET_ERROR_TYPE_DEBUG, GNUNET_ERROR_TYPE_WARNING, GNUNET_free, GNUNET_malloc, GNUNET_OK, GNUNET_realloc, GNUNET_strdup, LOG, ExpansionFrame::name, ExpansionFrame::parent, prefix, result, and start.

Referenced by expand_all(), and expand_dollar().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ escape_name()

static char * escape_name ( const char *  value)
static

FIXME.

Parameters
valueFIXME
Returns
FIXME

Definition at line 3122 of file configuration.c.

3123{
3124 char *escaped;
3125 const char *rpos;
3126 char *wpos;
3127
3128 escaped = GNUNET_malloc (strlen (value) * 2 + 1);
3129 memset (escaped, 0, strlen (value) * 2 + 1);
3130 rpos = value;
3131 wpos = escaped;
3132 while (rpos[0] != '\0')
3133 {
3134 switch (rpos[0])
3135 {
3136 case '\\':
3137 case ' ':
3138 wpos[0] = '\\';
3139 wpos[1] = rpos[0];
3140 wpos += 2;
3141 break;
3142
3143 default:
3144 wpos[0] = rpos[0];
3145 wpos++;
3146 }
3147 rpos++;
3148 }
3149 return escaped;
3150}

References GNUNET_malloc, and value.

Referenced by GNUNET_CONFIGURATION_append_value_filename(), and GNUNET_CONFIGURATION_remove_value_filename().

Here is the caller graph for this function:

◆ test_match()

static enum GNUNET_GenericReturnValue test_match ( void *  cls,
const char *  fn 
)
static

FIXME.

Parameters
clsstring we compare with (const char*)
fnfilename we are currently looking at
Returns
GNUNET_OK if the names do not match, GNUNET_SYSERR if they do

Definition at line 3161 of file configuration.c.

3163{
3164 const char *of = cls;
3165
3166 return (0 == strcmp (of, fn))
3168 : GNUNET_OK;
3169}

References GNUNET_OK, and GNUNET_SYSERR.

Referenced by GNUNET_CONFIGURATION_append_value_filename().

Here is the caller graph for this function:

◆ print_filename_option()

static void print_filename_option ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

Print each option in a given section as a filename.

Parameters
clsclosure
sectionname of the section
optionname of the option
valuevalue of the option

Definition at line 3491 of file configuration.c.

3495{
3496 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
3497
3498 char *value_fn;
3499 char *fn;
3500
3503 section,
3504 option,
3505 &value_fn));
3506 fn = GNUNET_STRINGS_filename_expand (value_fn);
3507 if (NULL == fn)
3508 fn = value_fn;
3509 else
3510 GNUNET_free (value_fn);
3511 fprintf (stdout,
3512 "%s = %s\n",
3513 option,
3514 fn);
3515 GNUNET_free (fn);
3516}

References cfg, GNUNET_assert, GNUNET_CONFIGURATION_get_value_filename(), GNUNET_free, GNUNET_OK, and GNUNET_STRINGS_filename_expand().

Referenced by GNUNET_CONFIGURATION_config_tool_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_option()

static void print_option ( void *  cls,
const char *  section,
const char *  option,
const char *  value 
)
static

Print each option in a given section.

Parameters
clsclosure
sectionname of the section
optionname of the option
valuevalue of the option

Definition at line 3528 of file configuration.c.

3532{
3533 (void) cls;
3534 (void) section;
3535
3536 fprintf (stdout,
3537 "%s = %s\n",
3538 option,
3539 value);
3540}

References value.

Referenced by GNUNET_CONFIGURATION_config_tool_run().

Here is the caller graph for this function:

◆ print_section_name()

static void print_section_name ( void *  cls,
const char *  section 
)
static

Print out given section name.

Parameters
clsunused
sectiona section in the configuration file

Definition at line 3550 of file configuration.c.

3552{
3553 (void) cls;
3554 fprintf (stdout,
3555 "%s\n",
3556 section);
3557}

Referenced by GNUNET_CONFIGURATION_config_tool_run().

Here is the caller graph for this function: