]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib:cmdline: Add initial code for new cmdline option parser
authorAndreas Schneider <asn@samba.org>
Thu, 16 Jul 2020 13:15:07 +0000 (15:15 +0200)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 28 Apr 2021 03:43:34 +0000 (03:43 +0000)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/cmdline/cmdline.c [new file with mode: 0644]
lib/cmdline/cmdline.h [new file with mode: 0644]
lib/cmdline/cmdline_private.h [new file with mode: 0644]
lib/cmdline/cmdline_s3.c [new file with mode: 0644]
lib/cmdline/cmdline_s4.c [new file with mode: 0644]
lib/cmdline/wscript [new file with mode: 0644]
wscript_build

diff --git a/lib/cmdline/cmdline.c b/lib/cmdline/cmdline.c
new file mode 100644 (file)
index 0000000..b64a6ea
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include "lib/param/param.h"
+#include "cmdline_private.h"
+
+static TALLOC_CTX *cmdline_mem_ctx;
+static struct loadparm_context *cmdline_lp_ctx;
+
+/* PRIVATE */
+bool samba_cmdline_set_talloc_ctx(TALLOC_CTX *mem_ctx)
+{
+       if (cmdline_mem_ctx != NULL) {
+               return false;
+       }
+
+       cmdline_mem_ctx = mem_ctx;
+       return true;
+}
+
+TALLOC_CTX *samba_cmdline_get_talloc_ctx(void)
+{
+       return cmdline_mem_ctx;
+}
+
+static void _samba_cmdline_talloc_log(const char *message)
+{
+       DBG_ERR("%s", message);
+}
+
+bool samba_cmdline_init_common(TALLOC_CTX *mem_ctx)
+{
+       bool ok;
+
+       ok = samba_cmdline_set_talloc_ctx(mem_ctx);
+       if (!ok) {
+               return false;
+       }
+
+       fault_setup();
+
+       /*
+        * Log to stdout by default.
+        * This can be changed to stderr using the option: --debug-stderr
+        */
+       setup_logging(getprogname(), DEBUG_DEFAULT_STDOUT);
+
+       talloc_set_log_fn(_samba_cmdline_talloc_log);
+       talloc_set_abort_fn(smb_panic);
+
+       return true;
+}
+
+/* PUBLIC */
+bool samba_cmdline_set_lp_ctx(struct loadparm_context *lp_ctx)
+{
+       if (lp_ctx == NULL) {
+               return false;
+       }
+       cmdline_lp_ctx = lp_ctx;
+
+       return true;
+}
+
+struct loadparm_context *samba_cmdline_get_lp_ctx(void)
+{
+       return cmdline_lp_ctx;
+}
diff --git a/lib/cmdline/cmdline.h b/lib/cmdline/cmdline.h
new file mode 100644 (file)
index 0000000..1a90237
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _CMDLINE_H
+#define _CMDLINE_H
+
+/**
+ * @brief Initialize the commandline interface for parsing options.
+ *
+ * This initialized the interface for parsing options given on the command
+ * line. It sets up the loadparm and client credentials contexts.
+ *
+ * @param[in]  mem_ctx  The talloc memory context to use for allocating memory.
+ *                      This should be a long living context till the client
+ *                      exits.
+ *
+ * @param[in]  require_smbconf  Wether the smb.conf file should to be present
+ *                              or not?
+ *
+ * @return true on success, false if an error occured.
+ */
+bool samba_cmdline_init(TALLOC_CTX *mem_ctx, bool require_smbconf);
+
+/**
+ * @brief Get a pointer of loadparm context used for the command line interface.
+ *
+ * @return The loadparm context.
+ */
+struct loadparm_context *samba_cmdline_get_lp_ctx(void);
+
+#endif /* _CMDLINE_H */
diff --git a/lib/cmdline/cmdline_private.h b/lib/cmdline/cmdline_private.h
new file mode 100644 (file)
index 0000000..050aff9
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _CMDLINE_PRIVATE_H
+#define _CMDLINE_PRIVATE_H
+
+#include "lib/cmdline/cmdline.h"
+
+/**
+ * @internal
+ *
+ * @brief Initialize the commandline interface for parsing options.
+ *
+ * This the common function to initialize the command line interface. This
+ * initializes:
+ *
+ *   - Crash setup
+ *   - logging system sening logs to stdout
+ *   - talloc leak reporting
+ *
+ * @param[in]  mem_ctx  The talloc memory context to use for allocating memory.
+ *                      This should be a long living context till the client
+ *                      exits.
+ *
+ * @return true on success, false if an error occured.
+ */
+bool samba_cmdline_init_common(TALLOC_CTX *mem_ctx);
+
+/**
+ * @internal
+ *
+ * @brief Set the talloc context for the command line interface.
+ *
+ * This is stored as a static pointer.
+ *
+ * @param[in]  mem_ctx  The talloc memory context.
+ *
+ * @return true on success, false if an error occured.
+ */
+bool samba_cmdline_set_talloc_ctx(TALLOC_CTX *mem_ctx);
+
+/**
+ * @internal
+ *
+ * @brief Get the talloc context for the cmdline interface.
+ *
+ * @return A talloc context.
+ */
+TALLOC_CTX *samba_cmdline_get_talloc_ctx(void);
+
+/**
+ * @internal
+ *
+ * @brief Set the loadparm context for the command line interface.
+ *
+ * @param[in]  lp_ctx  The loadparm context to use.
+ *
+ * @return true on success, false if an error occured.
+ */
+bool samba_cmdline_set_lp_ctx(struct loadparm_context *lp_ctx);
+
+#endif /* _CMDLINE_PRIVATE_H */
diff --git a/lib/cmdline/cmdline_s3.c b/lib/cmdline/cmdline_s3.c
new file mode 100644 (file)
index 0000000..b95e917
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "lib/replace/replace.h"
+#include <talloc.h>
+#include "lib/param/param.h"
+#include "lib/util/debug.h"
+#include "lib/util/fault.h"
+#include "source3/param/loadparm.h"
+#include "cmdline_private.h"
+
+static bool _require_smbconf;
+
+bool samba_cmdline_init(TALLOC_CTX *mem_ctx, bool require_smbconf)
+{
+       struct loadparm_context *lp_ctx = NULL;
+       bool ok;
+
+       ok = samba_cmdline_init_common(mem_ctx);
+       if (!ok) {
+               return false;
+       }
+
+       lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
+       if (lp_ctx == NULL) {
+               return false;
+       }
+       ok = samba_cmdline_set_lp_ctx(lp_ctx);
+       if (!ok) {
+               return false;
+       }
+
+       _require_smbconf = require_smbconf;
+
+       return true;
+}
diff --git a/lib/cmdline/cmdline_s4.c b/lib/cmdline/cmdline_s4.c
new file mode 100644 (file)
index 0000000..266c044
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2020      Andreas Schneider <asn@samba.org>
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "lib/replace/replace.h"
+#include <talloc.h>
+#include "lib/param/param.h"
+#include "lib/util/debug.h"
+#include "lib/util/fault.h"
+#include "cmdline_private.h"
+
+static bool _require_smbconf;
+
+bool samba_cmdline_init(TALLOC_CTX *mem_ctx, bool require_smbconf)
+{
+       struct loadparm_context *lp_ctx = NULL;
+       bool ok;
+
+       ok = samba_cmdline_init_common(mem_ctx);
+       if (!ok) {
+               return false;
+       }
+
+       lp_ctx = loadparm_init_global(false);
+       if (lp_ctx == NULL) {
+               return false;
+       }
+
+       ok = samba_cmdline_set_lp_ctx(lp_ctx);
+       if (!ok) {
+               return false;
+       }
+       _require_smbconf = require_smbconf;
+
+       return true;
+}
diff --git a/lib/cmdline/wscript b/lib/cmdline/wscript
new file mode 100644 (file)
index 0000000..912c89b
--- /dev/null
@@ -0,0 +1,23 @@
+#!/usr/bin/env python
+
+import os
+import sys
+from waflib import Logs
+
+def build(bld):
+    bld.SAMBA_LIBRARY('cmdline',
+                      source='cmdline.c',
+                      deps='''
+                           talloc
+                           samba-hostconfig
+                           popt
+                           ''',
+                      private_library=True)
+
+    bld.SAMBA_SUBSYSTEM('CMDLINE_S3',
+                        source='cmdline_s3.c',
+                        deps='cmdline')
+
+    bld.SAMBA_SUBSYSTEM('CMDLINE_S4',
+                        source='cmdline_s4.c',
+                        deps='cmdline')
index 7ac64b7af832c873e695c49ad2a156da7f29a1ee..ae77abb03ddd52eb36b35c211d1751d176ff33e1 100644 (file)
@@ -123,6 +123,7 @@ bld.RECURSE('libcli/samsync')
 bld.RECURSE('libcli/registry')
 bld.RECURSE('libcli/http')
 bld.RECURSE('lib/mscat')
+bld.RECURSE('lib/cmdline')
 bld.RECURSE('source4/lib/policy')
 bld.RECURSE('libcli/named_pipe_auth')
 if bld.CONFIG_GET('ENABLE_SELFTEST'):