]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add a config_validate() function to invoke verify callbacks.
authorNick Mathewson <nickm@torproject.org>
Wed, 23 Oct 2019 19:12:19 +0000 (15:12 -0400)
committerNick Mathewson <nickm@torproject.org>
Fri, 25 Oct 2019 00:30:32 +0000 (20:30 -0400)
As we turn our monolithic configuration objects into suites of
smaller module-specific configuration objects, we will want each one
to be able to provide its own verification callbacks.  That means
that instead of invoking these verification callbacks directly, we will
want to call them via the configuration manager object.

src/lib/confmgt/confparse.c
src/lib/confmgt/confparse.h

index 96eba610bba1cfaf1762efe836caf60d4b722dfb..6d272ed044ae72ed2b5f27b3ddbdacbe011fd8fe 100644 (file)
@@ -1142,6 +1142,79 @@ config_init(const config_mgr_t *mgr, void *options)
   } SMARTLIST_FOREACH_END(mv);
 }
 
+/**
+ * Normalize and validate a single object `options` within a configuration
+ * suite, according to its format.  `options` may be modified as appropriate
+ * in order to set ancillary data.  If `old_options` is provided, make sure
+ * that the transition from `old_options` to `options` is permitted.
+ *
+ * On success return 0; on failure set *msg_out to a newly allocated string
+ * explaining what is wrong, and return -1.
+ **/
+static int
+config_validate_single(const config_format_t *fmt,
+                       const void *old_options, void *options,
+                       char **msg_out)
+{
+  tor_assert(fmt);
+  tor_assert(options);
+
+  if (fmt->legacy_validate_fn) {
+    if (fmt->legacy_validate_fn(old_options, options, msg_out) < 0) {
+      return -1;
+    }
+  }
+
+  return 0;
+}
+
+/**
+ * Normalize and validate all the options in configuration object `options`
+ * and its sub-objects. `options` may be modified as appropriate in order to
+ * set ancillary data.  If `old_options` is provided, make sure that the
+ * transition from `old_options` to `options` is permitted.
+ *
+ * On success return 0; on failure set *msg_out to a newly allocated string
+ * explaining what is wrong, and return -1.
+ **/
+int
+config_validate(const config_mgr_t *mgr,
+                const void *old_options, void *options,
+                char **msg_out)
+{
+  int rv;
+  CONFIG_CHECK(mgr, options);
+  if (old_options) {
+    CONFIG_CHECK(mgr, old_options);
+  }
+
+  config_suite_t **suitep_new = config_mgr_get_suite_ptr(mgr, options);
+  config_suite_t **suitep_old = NULL;
+  if (old_options)
+    suitep_old = config_mgr_get_suite_ptr(mgr, (void*) old_options);
+
+  /* Validate the sub-objects */
+  if (suitep_new) {
+    SMARTLIST_FOREACH_BEGIN(mgr->subconfigs, const config_format_t *, fmt) {
+      void *obj = smartlist_get((*suitep_new)->configs, fmt_sl_idx);
+      const void *obj_old=NULL;
+      if (suitep_old)
+        obj_old = smartlist_get((*suitep_old)->configs, fmt_sl_idx);
+
+      rv = config_validate_single(fmt, obj_old, obj, msg_out);
+      if (rv < 0)
+        return rv;
+    } SMARTLIST_FOREACH_END(fmt);
+  }
+
+  /* Validate the top-level object. */
+  rv = config_validate_single(mgr->toplevel, old_options, options, msg_out);
+  if (rv < 0)
+    return rv;
+
+  return 0;
+}
+
 /** Allocate and return a new string holding the written-out values of the vars
  * in 'options'.  If 'minimal', do not write out any default-valued vars.
  * Else, if comment_defaults, write default values as comments.
index d50d6d8143a42ef7de6e9b64b9f68536fa6ad2c4..fea80f916920bad640c619eb8e23e661d63f9ff3 100644 (file)
@@ -169,6 +169,9 @@ int config_is_same(const config_mgr_t *fmt,
 struct config_line_t *config_get_changes(const config_mgr_t *mgr,
                                   const void *options1, const void *options2);
 void config_init(const config_mgr_t *mgr, void *options);
+int config_validate(const config_mgr_t *mgr,
+                    const void *old_options, void *options,
+                    char **msg_out);
 void *config_dup(const config_mgr_t *mgr, const void *old);
 char *config_dump(const config_mgr_t *mgr, const void *default_options,
                   const void *options, int minimal,