tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / configuration.c
index bc5fd3abdb49a549b954aac2e2ea262cb982c57d..a38438ba833c6ebd58cd4f6534683a5dc99ac3f8 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
 
-     GNUnet is free software; you can redistribute it and/or modify
-     it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     Affero General Public License for more details.
 
-     You should have received a copy of the GNU General Public License
-     along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     You should have received a copy of the GNU Affero General Public License
+     along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+     SPDX-License-Identifier: AGPL3.0-or-later
 */
 
 /**
@@ -139,22 +139,57 @@ GNUNET_CONFIGURATION_destroy (struct GNUNET_CONFIGURATION_Handle *cfg)
 }
 
 
+/**
+ * Parse a configuration file @a filename and run the function
+ * @a cb with the resulting configuration object. Then free the
+ * configuration object and return the status value from @a cb.
+ *
+ * @param filename configuration to parse, NULL for "default"
+ * @param cb function to run
+ * @param cb_cls closure for @a cb
+ * @return #GNUNET_SYSERR if parsing the configuration failed,
+ *   otherwise return value from @a cb.
+ */
+int
+GNUNET_CONFIGURATION_parse_and_run (const char *filename,
+                                   GNUNET_CONFIGURATION_Callback cb,
+                                   void *cb_cls)
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg;
+  int ret;
+
+  cfg = GNUNET_CONFIGURATION_create ();
+  if (GNUNET_OK !=
+      GNUNET_CONFIGURATION_load (cfg,
+                                 filename))
+  {
+    GNUNET_break (0);
+    GNUNET_CONFIGURATION_destroy (cfg);
+    return GNUNET_SYSERR;
+  }
+  ret = cb (cb_cls,
+           cfg);
+  GNUNET_CONFIGURATION_destroy (cfg);
+  return ret;
+}
+
+
 /**
  * De-serializes configuration
  *
  * @param cfg configuration to update
  * @param mem the memory block of serialized configuration
  * @param size the size of the memory block
- * @param allow_inline set to #GNUNET_YES if we recursively load configuration
- *          from inlined configurations; #GNUNET_NO if not and raise warnings
+ * @param basedir set to path from which we recursively load configuration
+ *          from inlined configurations; NULL if not and raise warnings
  *          when we come across them
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
 GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
                                  const char *mem,
-                                 const size_t size,
-                                 int allow_inline)
+                                 size_t size,
+                                 const char *basedir)
 {
   char *line;
   char *line_orig;
@@ -228,13 +263,23 @@ GNUNET_CONFIGURATION_deserialize (struct GNUNET_CONFIGURATION_Handle *cfg,
     {
       /* @INLINE@ value */
       value = &line[strlen ("@INLINE@ ")];
-      if (GNUNET_YES == allow_inline)
+      if (NULL != basedir)
       {
-       if (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, value))
+       char *fn;
+
+       GNUNET_asprintf (&fn,
+                        "%s/%s",
+                        basedir,
+                        value);
+       if (GNUNET_OK !=
+           GNUNET_CONFIGURATION_parse (cfg,
+                                       fn))
        {
+         GNUNET_free (fn);
          ret = GNUNET_SYSERR;    /* failed to parse included config */
          break;
        }
+       GNUNET_free (fn);
       }
       else
       {
@@ -311,8 +356,10 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
   size_t fs;
   char *fn;
   char *mem;
+  char *endsep;
   int dirty;
   int ret;
+  ssize_t sret;
 
   fn = GNUNET_STRINGS_filename_expand (filename);
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -322,7 +369,10 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
     return GNUNET_SYSERR;
   dirty = cfg->dirty;           /* back up value! */
   if (GNUNET_SYSERR ==
-      GNUNET_DISK_file_size (fn, &fs64, GNUNET_YES, GNUNET_YES))
+      GNUNET_DISK_file_size (fn,
+                            &fs64,
+                            GNUNET_YES,
+                            GNUNET_YES))
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
         "Error while determining the file size of `%s'\n",
@@ -338,7 +388,11 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
   }
   fs = fs64;
   mem = GNUNET_malloc (fs);
-  if (fs != GNUNET_DISK_fn_read (fn, mem, fs))
+  sret = GNUNET_DISK_fn_read (fn,
+                             mem,
+                             fs);
+  if ( (sret < 0) ||
+       (fs != (size_t) sret) )
   {
     LOG (GNUNET_ERROR_TYPE_WARNING,
         _("Error while reading file `%s'\n"),
@@ -350,8 +404,14 @@ GNUNET_CONFIGURATION_parse (struct GNUNET_CONFIGURATION_Handle *cfg,
   LOG (GNUNET_ERROR_TYPE_DEBUG,
        "Deserializing contents of file `%s'\n",
        fn);
+  endsep = strrchr (fn, (int) '/');
+  if (NULL != endsep)
+    *endsep = '\0';
+  ret = GNUNET_CONFIGURATION_deserialize (cfg,
+                                         mem,
+                                         fs,
+                                         fn);
   GNUNET_free (fn);
-  ret = GNUNET_CONFIGURATION_deserialize (cfg, mem, fs, GNUNET_YES);
   GNUNET_free (mem);
   /* restore dirty flag - anything we set in the meantime
    * came from disk */
@@ -396,7 +456,6 @@ GNUNET_CONFIGURATION_serialize (const struct GNUNET_CONFIGURATION_Handle *cfg,
   size_t m_size;
   size_t c_size;
 
-
   /* Pass1 : calculate the buffer size required */
   m_size = 0;
   for (sec = cfg->sections; NULL != sec; sec = sec->next)
@@ -478,6 +537,7 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
   char *fn;
   char *cfg_buf;
   size_t size;
+  ssize_t sret;
 
   fn = GNUNET_STRINGS_filename_expand (filename);
   if (fn == NULL)
@@ -488,16 +548,18 @@ GNUNET_CONFIGURATION_write (struct GNUNET_CONFIGURATION_Handle *cfg,
     return GNUNET_SYSERR;
   }
   cfg_buf = GNUNET_CONFIGURATION_serialize (cfg, &size);
-  if (size != GNUNET_DISK_fn_write (fn, cfg_buf, size,
-                                   GNUNET_DISK_PERM_USER_READ
-                                   | GNUNET_DISK_PERM_USER_WRITE
-                                   | GNUNET_DISK_PERM_GROUP_READ
-                                   | GNUNET_DISK_PERM_GROUP_WRITE))
+  sret = GNUNET_DISK_fn_write (fn, cfg_buf, size,
+                              GNUNET_DISK_PERM_USER_READ
+                              | GNUNET_DISK_PERM_USER_WRITE
+                              | GNUNET_DISK_PERM_GROUP_READ
+                              | GNUNET_DISK_PERM_GROUP_WRITE);
+  if ( (sret < 0) ||
+       (size != (size_t) sret) )
   {
     GNUNET_free (fn);
     GNUNET_free (cfg_buf);
     LOG (GNUNET_ERROR_TYPE_WARNING,
-        "Writing configration to file `%s' failed\n",
+        "Writing configuration to file `%s' failed\n",
          filename);
     cfg->dirty = GNUNET_SYSERR; /* last write failed */
     return GNUNET_SYSERR;
@@ -841,13 +903,20 @@ GNUNET_CONFIGURATION_set_value_string (struct GNUNET_CONFIGURATION_Handle *cfg,
  */
 void
 GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
-                                       const char *section, const char *option,
+                                       const char *section,
+                                      const char *option,
                                        unsigned long long number)
 {
   char s[64];
 
-  GNUNET_snprintf (s, 64, "%llu", number);
-  GNUNET_CONFIGURATION_set_value_string (cfg, section, option, s);
+  GNUNET_snprintf (s,
+                  64,
+                  "%llu",
+                  number);
+  GNUNET_CONFIGURATION_set_value_string (cfg,
+                                        section,
+                                        option,
+                                        s);
 }
 
 
@@ -861,22 +930,27 @@ GNUNET_CONFIGURATION_set_value_number (struct GNUNET_CONFIGURATION_Handle *cfg,
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
-                                       *cfg, const char *section,
+GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                      const char *section,
                                        const char *option,
                                        unsigned long long *number)
 {
   struct ConfigEntry *e;
+  char dummy[2];
 
   if (NULL == (e = find_entry (cfg, section, option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  if (1 != SSCANF (e->val, "%llu", number))
+  if (1 != SSCANF (e->val,
+                  "%llu%1s",
+                  number,
+                  dummy))
     return GNUNET_SYSERR;
   return GNUNET_OK;
 }
 
+
 /**
  * Get a configuration value that should be a floating point number.
  *
@@ -887,18 +961,22 @@ GNUNET_CONFIGURATION_get_value_number (const struct GNUNET_CONFIGURATION_Handle
  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
  */
 int
-GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle
-                                       *cfg, const char *section,
+GNUNET_CONFIGURATION_get_value_float  (const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                      const char *section,
                                        const char *option,
                                        float *number)
 {
   struct ConfigEntry *e;
+  char dummy[2];
 
   if (NULL == (e = find_entry (cfg, section, option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  if (1 != SSCANF (e->val, "%f", number))
+  if (1 != SSCANF (e->val,
+                  "%f%1s",
+                  number,
+                  dummy))
     return GNUNET_SYSERR;
   return GNUNET_OK;
 }
@@ -921,12 +999,22 @@ GNUNET_CONFIGURATION_get_value_time (const struct GNUNET_CONFIGURATION_Handle *c
                                      struct GNUNET_TIME_Relative *time)
 {
   struct ConfigEntry *e;
+  int ret;
 
-  if (NULL == (e = find_entry (cfg, section, option)))
+  if (NULL == (e = find_entry (cfg,
+                               section,
+                               option)))
     return GNUNET_SYSERR;
   if (NULL == e->val)
     return GNUNET_SYSERR;
-  return GNUNET_STRINGS_fancy_time_to_relative (e->val, time);
+  ret = GNUNET_STRINGS_fancy_time_to_relative (e->val,
+                                               time);
+  if (GNUNET_OK != ret)
+    GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
+                               section,
+                               option,
+                               _("Not a valid relative time specification"));
+  return ret;
 }
 
 
@@ -1662,7 +1750,9 @@ GNUNET_CONFIGURATION_load_from (struct GNUNET_CONFIGURATION_Handle *cfg,
                                const char *defaults_d)
 {
   if (GNUNET_SYSERR ==
-      GNUNET_DISK_directory_scan (defaults_d, &parse_configuration_file, cfg))
+      GNUNET_DISK_directory_scan (defaults_d,
+                                  &parse_configuration_file,
+                                  cfg))
     return GNUNET_SYSERR;       /* no configuration at all found */
   return GNUNET_OK;
 }