[PATCH] Add hook that allows modification of default settings.

Jelmer Vernooij jelmer at samba.org
Sat Dec 6 15:08:33 MST 2014


This is useful for reducing the amount of configuration necessary for
OpenChange.

The hook is ideally registered from a plugin initialization function,
so that it automatically gets used whenever the plugin is installed.

This makes it possible for plugins to e.g. extend the default value for
the list of enabled dcerpc endpoint services.

Change-Id: Ic8bacdd8b4c92a2a4b97cfa1a50dc41365b78071
Signed-Off-By: Jelmer Vernooij <jelmer at samba.org>
---
 lib/param/loadparm.c | 35 ++++++++++++++++++++++++++++++++++-
 lib/param/param.h    | 15 +++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/lib/param/loadparm.c b/lib/param/loadparm.c
index d1e36df..44dc91b 100644
--- a/lib/param/loadparm.c
+++ b/lib/param/loadparm.c
@@ -2346,6 +2346,25 @@ static int lpcfg_destructor(struct loadparm_context *lp_ctx)
 	return 0;
 }
 
+typedef bool (*lpcfg_defaults_hook) (struct loadparm_context *);
+
+struct defaults_hook_data {
+	const char *name;
+	lpcfg_defaults_hook hook;
+	struct defaults_hook_data *prev, *next;
+} *defaults_hooks = NULL;
+
+
+bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook)
+{
+	struct defaults_hook_data *hook_data = talloc(talloc_autofree_context(),
+												  struct defaults_hook_data);
+	hook_data->name = talloc_strdup(hook_data, name);
+	hook_data->hook = hook;
+	DLIST_ADD(defaults_hooks, hook_data);
+	return false;
+}
+
 /**
  * Initialise the global parameter structure.
  *
@@ -2358,6 +2377,7 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 	struct loadparm_context *lp_ctx;
 	struct parmlist_entry *parm;
 	char *logfile;
+	struct defaults_hook_data *defaults_hook;
 
 	lp_ctx = talloc_zero(mem_ctx, struct loadparm_context);
 	if (lp_ctx == NULL)
@@ -2751,6 +2771,20 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 
 	lpcfg_do_global_parameter(lp_ctx, "printjob username", "%U");
 
+	/* Allow modules to adjust defaults */
+	for (defaults_hook = defaults_hooks; defaults_hook;
+		 defaults_hook = defaults_hook->next) {
+		bool ret;
+
+		ret = defaults_hook->hook(lp_ctx);
+		if (!ret) {
+			DEBUG(1, ("Defaults hook %s failed to run.",
+					  defaults_hook->name));
+			talloc_free(lp_ctx);
+			return NULL;
+		}
+	}
+
 	for (i = 0; parm_table[i].label; i++) {
 		if (!(lp_ctx->flags[i] & FLAG_CMDLINE)) {
 			lp_ctx->flags[i] |= FLAG_DEFAULT;
@@ -2769,7 +2803,6 @@ struct loadparm_context *loadparm_init(TALLOC_CTX *mem_ctx)
 		}
 	}
 
-
 	return lp_ctx;
 }
 
diff --git a/lib/param/param.h b/lib/param/param.h
index 2fb6d4b..9064033 100644
--- a/lib/param/param.h
+++ b/lib/param/param.h
@@ -37,6 +37,8 @@ struct param_section {
 struct param_context;
 struct smbsrv_connection;
 
+typedef bool (*lpcfg_defaults_hook) (struct loadparm_context *);
+
 #define Auto (2)
 
 #include "libds/common/roles.h"
@@ -212,6 +214,19 @@ const char *lpcfg_socket_options(struct loadparm_context *);
 struct dcerpc_server_info *lpcfg_dcerpc_server_info(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx);
 struct gensec_settings *lpcfg_gensec_settings(TALLOC_CTX *, struct loadparm_context *);
 
+/* Hooks to override defaults.
+ *
+ * Every time a loadparm context is initialized, the hooks are
+ * called on it, once Samba itself has set defaults.
+ *
+ * This allows modules to tweak defaults (before any smb.conf file or registry
+ * is loaded). Usually they would do this by calling lpcfg_do_global_parameter
+ * or lpcfg_do_service_parameter.
+ *
+ * A good use case for this is OpenChange, which by default enables its
+ * DCE/RPC services when it is installed.
+ * */
+bool lpcfg_register_defaults_hook(const char *name, lpcfg_defaults_hook hook);
 
 /* The following definitions come from param/generic.c  */
 
-- 
2.2.0.rc0.207.ga3a616c



More information about the samba-technical mailing list