|
|
6cf099 |
From edb91d864b48bf6e6240fe7eee84e68cdaaf012a Mon Sep 17 00:00:00 2001
|
|
|
6cf099 |
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
|
|
|
6cf099 |
Date: Wed, 22 Jul 2015 10:02:02 +0200
|
|
|
6cf099 |
Subject: [PATCH 22/23] TOOLS: add common command framework
|
|
|
6cf099 |
|
|
|
6cf099 |
Add general framework to simplify creating "cmd COMMAND [OPTIONS...]"
|
|
|
6cf099 |
style tools.
|
|
|
6cf099 |
|
|
|
6cf099 |
Preparation for:
|
|
|
6cf099 |
https://fedorahosted.org/sssd/ticket/2584
|
|
|
6cf099 |
|
|
|
6cf099 |
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
|
|
|
6cf099 |
---
|
|
|
6cf099 |
Makefile.am | 5 +-
|
|
|
6cf099 |
src/tools/common/sss_tools.c | 406 +++++++++++++++++++++++++++++++++++++++++++
|
|
|
6cf099 |
src/tools/common/sss_tools.h | 91 ++++++++++
|
|
|
6cf099 |
3 files changed, 501 insertions(+), 1 deletion(-)
|
|
|
6cf099 |
create mode 100644 src/tools/common/sss_tools.c
|
|
|
6cf099 |
create mode 100644 src/tools/common/sss_tools.h
|
|
|
6cf099 |
|
|
|
6cf099 |
diff --git a/Makefile.am b/Makefile.am
|
|
|
6cf099 |
index b8cbc6df23ded1edb945a709b6dbe1c44eb54017..1edecc483c61d04562b7bfd9086146e93963b74e 100644
|
|
|
6cf099 |
--- a/Makefile.am
|
|
|
6cf099 |
+++ b/Makefile.am
|
|
|
6cf099 |
@@ -445,7 +445,9 @@ SSSD_TOOLS_OBJ = \
|
|
|
6cf099 |
src/tools/tools_util.c \
|
|
|
6cf099 |
src/tools/files.c \
|
|
|
6cf099 |
src/tools/selinux.c \
|
|
|
6cf099 |
- src/util/nscd.c
|
|
|
6cf099 |
+ src/tools/common/sss_tools.c \
|
|
|
6cf099 |
+ src/util/nscd.c \
|
|
|
6cf099 |
+ $(NULL)
|
|
|
6cf099 |
|
|
|
6cf099 |
SSSD_LCL_TOOLS_OBJ = \
|
|
|
6cf099 |
src/sss_client/common.c \
|
|
|
6cf099 |
@@ -641,6 +643,7 @@ dist_noinst_HEADERS = \
|
|
|
6cf099 |
src/lib/idmap/sss_idmap_private.h \
|
|
|
6cf099 |
src/lib/sifp/sss_sifp_private.h \
|
|
|
6cf099 |
src/tests/cmocka/test_utils.h \
|
|
|
6cf099 |
+ src/tools/common/sss_tools.h \
|
|
|
6cf099 |
$(NULL)
|
|
|
6cf099 |
|
|
|
6cf099 |
|
|
|
6cf099 |
diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c
|
|
|
6cf099 |
new file mode 100644
|
|
|
6cf099 |
index 0000000000000000000000000000000000000000..6bbce3a25ddddc0b23ebc108a917a38e94981b65
|
|
|
6cf099 |
--- /dev/null
|
|
|
6cf099 |
+++ b/src/tools/common/sss_tools.c
|
|
|
6cf099 |
@@ -0,0 +1,406 @@
|
|
|
6cf099 |
+/*
|
|
|
6cf099 |
+ Authors:
|
|
|
6cf099 |
+ Pavel Březina <pbrezina@redhat.com>
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ Copyright (C) 2015 Red Hat
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ This program is free software; you can redistribute it and/or modify
|
|
|
6cf099 |
+ it under the terms of the GNU General Public License as published by
|
|
|
6cf099 |
+ the Free Software Foundation; either version 3 of the License, or
|
|
|
6cf099 |
+ (at your option) any later version.
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ This program is distributed in the hope that it will be useful,
|
|
|
6cf099 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
6cf099 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
6cf099 |
+ GNU General Public License for more details.
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ You should have received a copy of the GNU General Public License
|
|
|
6cf099 |
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
6cf099 |
+*/
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#include <talloc.h>
|
|
|
6cf099 |
+#include <stdlib.h>
|
|
|
6cf099 |
+#include <string.h>
|
|
|
6cf099 |
+#include <popt.h>
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#include "config.h"
|
|
|
6cf099 |
+#include "util/util.h"
|
|
|
6cf099 |
+#include "confdb/confdb.h"
|
|
|
6cf099 |
+#include "db/sysdb.h"
|
|
|
6cf099 |
+#include "tools/common/sss_tools.h"
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_cmdline {
|
|
|
6cf099 |
+ const char *exec; /* argv[0] */
|
|
|
6cf099 |
+ const char *command; /* command name */
|
|
|
6cf099 |
+ int argc; /* rest of arguments */
|
|
|
6cf099 |
+ const char **argv;
|
|
|
6cf099 |
+};
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+static void sss_tool_common_opts(struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ int *argc, const char **argv)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ poptContext pc;
|
|
|
6cf099 |
+ int debug = SSSDBG_DEFAULT;
|
|
|
6cf099 |
+ int orig_argc = *argc;
|
|
|
6cf099 |
+ int opt;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ struct poptOption options[] = {
|
|
|
6cf099 |
+ {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_STRIP, &debug,
|
|
|
6cf099 |
+ 0, _("The debug level to run with"), NULL },
|
|
|
6cf099 |
+ POPT_TABLEEND
|
|
|
6cf099 |
+ };
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ pc = poptGetContext(argv[0], orig_argc, argv, options, 0);
|
|
|
6cf099 |
+ while ((opt = poptGetNextOpt(pc)) != -1) {
|
|
|
6cf099 |
+ /* do nothing */
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Strip common options from arguments. We will discard_const here,
|
|
|
6cf099 |
+ * since it is not worth the trouble to convert it back and forth. */
|
|
|
6cf099 |
+ *argc = poptStrippedArgv(pc, orig_argc, discard_const_p(char *, argv));
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ DEBUG_CLI_INIT(debug);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ poptFreeContext(pc);
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+static errno_t sss_tool_confdb_init(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ struct confdb_ctx **_confdb)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ struct confdb_ctx *confdb;
|
|
|
6cf099 |
+ char *path;
|
|
|
6cf099 |
+ errno_t ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ path = talloc_asprintf(mem_ctx, "%s/%s", DB_PATH, CONFDB_FILE);
|
|
|
6cf099 |
+ if (path == NULL) {
|
|
|
6cf099 |
+ return ENOMEM;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = confdb_init(mem_ctx, &confdb, path);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE,
|
|
|
6cf099 |
+ "Could not initialize connection to the confdb\n");
|
|
|
6cf099 |
+ talloc_free(path);
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ if (_confdb != NULL) {
|
|
|
6cf099 |
+ *_confdb = confdb;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return EOK;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+static errno_t sss_tool_domains_init(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ struct confdb_ctx *confdb,
|
|
|
6cf099 |
+ struct sss_domain_info **_domains)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ struct sss_domain_info *domains;
|
|
|
6cf099 |
+ struct sss_domain_info *dom;
|
|
|
6cf099 |
+ errno_t ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = confdb_get_domains(confdb, &domains);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
|
|
|
6cf099 |
+ ret, sss_strerror(ret));
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = sysdb_init(mem_ctx, domains, false);
|
|
|
6cf099 |
+ SYSDB_VERSION_ERROR(ret);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE,
|
|
|
6cf099 |
+ "Could not initialize connection to the sysdb\n");
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ for (dom = domains; dom != NULL; dom = get_next_domain(dom, true)) {
|
|
|
6cf099 |
+ if (!IS_SUBDOMAIN(dom)) {
|
|
|
6cf099 |
+ /* Update list of subdomains for this domain */
|
|
|
6cf099 |
+ ret = sysdb_update_subdomains(dom);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_MINOR_FAILURE,
|
|
|
6cf099 |
+ "Failed to update subdomains for domain %s.\n",
|
|
|
6cf099 |
+ dom->name);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ for (dom = domains; dom != NULL; dom = get_next_domain(dom, true)) {
|
|
|
6cf099 |
+ ret = sss_names_init(mem_ctx, confdb, dom->name, &dom->names);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "sss_names_init() failed\n");
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ *_domains = domains;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_tool_ctx *sss_tool_init(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ int *argc, const char **argv)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx;
|
|
|
6cf099 |
+ errno_t ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ tool_ctx = talloc_zero(mem_ctx, struct sss_tool_ctx);
|
|
|
6cf099 |
+ if (tool_ctx == NULL) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
|
|
|
6cf099 |
+ return NULL;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ sss_tool_common_opts(tool_ctx, argc, argv);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Connect to confdb. */
|
|
|
6cf099 |
+ ret = sss_tool_confdb_init(tool_ctx, &tool_ctx->confdb);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open confdb [%d]: %s\n",
|
|
|
6cf099 |
+ ret, sss_strerror(ret));
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Setup domains. */
|
|
|
6cf099 |
+ ret = sss_tool_domains_init(tool_ctx, tool_ctx->confdb, &tool_ctx->domains);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup domains [%d]: %s\n",
|
|
|
6cf099 |
+ ret, sss_strerror(ret));
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = confdb_get_string(tool_ctx->confdb, tool_ctx,
|
|
|
6cf099 |
+ CONFDB_MONITOR_CONF_ENTRY,
|
|
|
6cf099 |
+ CONFDB_MONITOR_DEFAULT_DOMAIN,
|
|
|
6cf099 |
+ NULL, &tool_ctx->default_domain);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_OP_FAILURE, "Cannot get the default domain [%d]: %s\n",
|
|
|
6cf099 |
+ ret, strerror(ret));
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = EOK;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+done:
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ talloc_zfree(tool_ctx);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return tool_ctx;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_usage(const char *tool_name,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ int i;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ fprintf(stderr, _("Usage:\n%s COMMAND COMMAND-ARGS\n\n"), tool_name);
|
|
|
6cf099 |
+ fprintf(stderr, _("Available commands:\n"));
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ for (i = 0; commands[i].command != NULL; i++) {
|
|
|
6cf099 |
+ fprintf(stderr, "* %s\n", commands[i].command);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return EXIT_FAILURE;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_route(int argc, const char **argv,
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands,
|
|
|
6cf099 |
+ void *pvt)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ struct sss_cmdline cmdline;
|
|
|
6cf099 |
+ const char *cmd;
|
|
|
6cf099 |
+ int i;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ if (commands == NULL) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Bug: commands can't be NULL!\n");
|
|
|
6cf099 |
+ return EXIT_FAILURE;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ if (argc < 2) {
|
|
|
6cf099 |
+ return sss_tool_usage(argv[0], commands);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ cmd = argv[1];
|
|
|
6cf099 |
+ for (i = 0; commands[i].command != NULL; i++) {
|
|
|
6cf099 |
+ if (strcmp(commands[i].command, cmd) == 0) {
|
|
|
6cf099 |
+ cmdline.exec = argv[0];
|
|
|
6cf099 |
+ cmdline.command = argv[1];
|
|
|
6cf099 |
+ cmdline.argc = argc - 2;
|
|
|
6cf099 |
+ cmdline.argv = argv + 2;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return commands[i].fn(&cmdline, tool_ctx, pvt);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return sss_tool_usage(argv[0], commands);
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_popt_ex(struct sss_cmdline *cmdline,
|
|
|
6cf099 |
+ struct poptOption *options,
|
|
|
6cf099 |
+ enum sss_tool_opt require_option,
|
|
|
6cf099 |
+ sss_popt_fn popt_fn,
|
|
|
6cf099 |
+ void *popt_fn_pvt,
|
|
|
6cf099 |
+ const char *fopt_name,
|
|
|
6cf099 |
+ const char *fopt_help,
|
|
|
6cf099 |
+ const char **_fopt)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ const char *optstr;
|
|
|
6cf099 |
+ char *help;
|
|
|
6cf099 |
+ poptContext pc;
|
|
|
6cf099 |
+ int ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Create help option string. We always need to append command name since
|
|
|
6cf099 |
+ * we use POPT_CONTEXT_KEEP_FIRST. */
|
|
|
6cf099 |
+ optstr = options == NULL ? "" : _(" [OPTIONS...]");
|
|
|
6cf099 |
+ if (fopt_name == NULL) {
|
|
|
6cf099 |
+ help = talloc_asprintf(NULL, "%s %s%s",
|
|
|
6cf099 |
+ cmdline->exec, cmdline->command, optstr);
|
|
|
6cf099 |
+ } else {
|
|
|
6cf099 |
+ help = talloc_asprintf(NULL, "%s %s %s%s",
|
|
|
6cf099 |
+ cmdline->exec, cmdline->command, fopt_name, optstr);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ if (help == NULL) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
|
|
|
6cf099 |
+ return EXIT_FAILURE;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Create popt context. This function is supposed to be called on
|
|
|
6cf099 |
+ * command argv which does not contain executable (argv[0]), therefore
|
|
|
6cf099 |
+ * we need to use KEEP_FIRST that ensures argv[0] is also processed. */
|
|
|
6cf099 |
+ pc = poptGetContext(cmdline->exec, cmdline->argc, cmdline->argv,
|
|
|
6cf099 |
+ options, POPT_CONTEXT_KEEP_FIRST);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ poptSetOtherOptionHelp(pc, help);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Parse options. Invoke custom function if provided. If no parsing
|
|
|
6cf099 |
+ * function is provided, print error on unknown option. */
|
|
|
6cf099 |
+ while ((ret = poptGetNextOpt(pc)) != -1) {
|
|
|
6cf099 |
+ if (popt_fn != NULL) {
|
|
|
6cf099 |
+ ret = popt_fn(pc, ret, popt_fn_pvt);
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ ret = EXIT_FAILURE;
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ } else {
|
|
|
6cf099 |
+ fprintf(stderr, _("Invalid option %s: %s\n\n"),
|
|
|
6cf099 |
+ poptBadOption(pc, 0), poptStrerror(ret));
|
|
|
6cf099 |
+ poptPrintHelp(pc, stderr, 0);
|
|
|
6cf099 |
+ ret = EXIT_FAILURE;
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* Parse free option which is always required if requested. */
|
|
|
6cf099 |
+ if (_fopt != NULL) {
|
|
|
6cf099 |
+ *_fopt = poptGetArg(pc);
|
|
|
6cf099 |
+ if (*_fopt == NULL) {
|
|
|
6cf099 |
+ fprintf(stderr, _("Missing option: %s\n\n"), fopt_help);
|
|
|
6cf099 |
+ poptPrintHelp(pc, stderr, 0);
|
|
|
6cf099 |
+ ret = EXIT_FAILURE;
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* No more arguments expected. If something follows it is an error. */
|
|
|
6cf099 |
+ if (poptGetArg(pc)) {
|
|
|
6cf099 |
+ fprintf(stderr, _("Only one free argument is expected!\n\n"));
|
|
|
6cf099 |
+ poptPrintHelp(pc, stderr, 0);
|
|
|
6cf099 |
+ ret = EXIT_FAILURE;
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ /* If at least one option is required and not provided, print error. */
|
|
|
6cf099 |
+ if (require_option == SSS_TOOL_OPT_REQUIRED
|
|
|
6cf099 |
+ && ((_fopt != NULL && cmdline->argc < 2) || cmdline->argc < 1)) {
|
|
|
6cf099 |
+ fprintf(stderr, _("At least one option is required!\n\n"));
|
|
|
6cf099 |
+ poptPrintHelp(pc, stderr, 0);
|
|
|
6cf099 |
+ ret = EXIT_FAILURE;
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = EXIT_SUCCESS;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+done:
|
|
|
6cf099 |
+ poptFreeContext(pc);
|
|
|
6cf099 |
+ talloc_free(help);
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_popt(struct sss_cmdline *cmdline,
|
|
|
6cf099 |
+ struct poptOption *options,
|
|
|
6cf099 |
+ enum sss_tool_opt require_option,
|
|
|
6cf099 |
+ sss_popt_fn popt_fn,
|
|
|
6cf099 |
+ void *popt_fn_pvt)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ return sss_tool_popt_ex(cmdline, options, require_option,
|
|
|
6cf099 |
+ popt_fn, popt_fn_pvt, NULL, NULL, NULL);
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_main(int argc, const char **argv,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands,
|
|
|
6cf099 |
+ void *pvt)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx;
|
|
|
6cf099 |
+ uid_t uid;
|
|
|
6cf099 |
+ int ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ uid = getuid();
|
|
|
6cf099 |
+ if (uid != 0) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Running under %d, must be root\n", uid);
|
|
|
6cf099 |
+ ERROR("%1$s must be run as root\n", argv[0]);
|
|
|
6cf099 |
+ return EXIT_FAILURE;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ tool_ctx = sss_tool_init(NULL, &argc, argv);
|
|
|
6cf099 |
+ if (tool_ctx == NULL) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create tool context\n");
|
|
|
6cf099 |
+ return EXIT_FAILURE;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = sss_tool_route(argc, argv, tool_ctx, commands, pvt);
|
|
|
6cf099 |
+ talloc_free(tool_ctx);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_parse_name(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ const char *input,
|
|
|
6cf099 |
+ const char **_username,
|
|
|
6cf099 |
+ struct sss_domain_info **_domain)
|
|
|
6cf099 |
+{
|
|
|
6cf099 |
+ char *username = NULL;
|
|
|
6cf099 |
+ char *domname = NULL;
|
|
|
6cf099 |
+ struct sss_domain_info *domain;
|
|
|
6cf099 |
+ int ret;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = sss_parse_name_for_domains(mem_ctx, tool_ctx->domains,
|
|
|
6cf099 |
+ tool_ctx->default_domain, input,
|
|
|
6cf099 |
+ &domname, &username);
|
|
|
6cf099 |
+ if (ret == EAGAIN) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to find domain. The domain name may "
|
|
|
6cf099 |
+ "be a subdomain that was not yet found.\n");
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ } else if (ret != EOK) {
|
|
|
6cf099 |
+ DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse name [%d]: %s\n",
|
|
|
6cf099 |
+ ret, sss_strerror(ret));
|
|
|
6cf099 |
+ goto done;
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ domain = find_domain_by_name(tool_ctx->domains, domname, true);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ *_username = username;
|
|
|
6cf099 |
+ *_domain = domain;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ ret = EOK;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+done:
|
|
|
6cf099 |
+ if (ret != EOK) {
|
|
|
6cf099 |
+ talloc_zfree(username);
|
|
|
6cf099 |
+ talloc_zfree(domname);
|
|
|
6cf099 |
+ }
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ return ret;
|
|
|
6cf099 |
+}
|
|
|
6cf099 |
diff --git a/src/tools/common/sss_tools.h b/src/tools/common/sss_tools.h
|
|
|
6cf099 |
new file mode 100644
|
|
|
6cf099 |
index 0000000000000000000000000000000000000000..cfe11d06e1dadf8e49efe155c8a53f99a31e97fb
|
|
|
6cf099 |
--- /dev/null
|
|
|
6cf099 |
+++ b/src/tools/common/sss_tools.h
|
|
|
6cf099 |
@@ -0,0 +1,91 @@
|
|
|
6cf099 |
+/*
|
|
|
6cf099 |
+ Authors:
|
|
|
6cf099 |
+ Pavel Březina <pbrezina@redhat.com>
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ Copyright (C) 2015 Red Hat
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ This program is free software; you can redistribute it and/or modify
|
|
|
6cf099 |
+ it under the terms of the GNU General Public License as published by
|
|
|
6cf099 |
+ the Free Software Foundation; either version 3 of the License, or
|
|
|
6cf099 |
+ (at your option) any later version.
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ This program is distributed in the hope that it will be useful,
|
|
|
6cf099 |
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
6cf099 |
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
6cf099 |
+ GNU General Public License for more details.
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ You should have received a copy of the GNU General Public License
|
|
|
6cf099 |
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
6cf099 |
+*/
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#ifndef _SSS_TOOLS_H_
|
|
|
6cf099 |
+#define _SSS_TOOLS_H_
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#include <talloc.h>
|
|
|
6cf099 |
+#include <popt.h>
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#include "confdb/confdb.h"
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_tool_ctx {
|
|
|
6cf099 |
+ struct confdb_ctx *confdb;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+ char *default_domain;
|
|
|
6cf099 |
+ struct sss_domain_info *domains;
|
|
|
6cf099 |
+};
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_tool_ctx *sss_tool_init(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ int *argc, const char **argv);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_cmdline;
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+typedef int
|
|
|
6cf099 |
+(*sss_route_fn)(struct sss_cmdline *cmdline,
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ void *pvt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+struct sss_route_cmd {
|
|
|
6cf099 |
+ const char *command;
|
|
|
6cf099 |
+ sss_route_fn fn;
|
|
|
6cf099 |
+};
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_usage(const char *tool_name,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_route(int argc, const char **argv,
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands,
|
|
|
6cf099 |
+ void *pvt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+typedef int (*sss_popt_fn)(poptContext pc, char option, void *pvt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+enum sss_tool_opt {
|
|
|
6cf099 |
+ SSS_TOOL_OPT_REQUIRED,
|
|
|
6cf099 |
+ SSS_TOOL_OPT_OPTIONAL
|
|
|
6cf099 |
+};
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_popt_ex(struct sss_cmdline *cmdline,
|
|
|
6cf099 |
+ struct poptOption *options,
|
|
|
6cf099 |
+ enum sss_tool_opt require_option,
|
|
|
6cf099 |
+ sss_popt_fn popt_fn,
|
|
|
6cf099 |
+ void *popt_fn_pvt,
|
|
|
6cf099 |
+ const char *free_opt_name,
|
|
|
6cf099 |
+ const char *free_opt_help,
|
|
|
6cf099 |
+ const char **_free_opt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_popt(struct sss_cmdline *cmdline,
|
|
|
6cf099 |
+ struct poptOption *options,
|
|
|
6cf099 |
+ enum sss_tool_opt require_option,
|
|
|
6cf099 |
+ sss_popt_fn popt_fn,
|
|
|
6cf099 |
+ void *popt_fn_pvt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_main(int argc, const char **argv,
|
|
|
6cf099 |
+ struct sss_route_cmd *commands,
|
|
|
6cf099 |
+ void *pvt);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+int sss_tool_parse_name(TALLOC_CTX *mem_ctx,
|
|
|
6cf099 |
+ struct sss_tool_ctx *tool_ctx,
|
|
|
6cf099 |
+ const char *input,
|
|
|
6cf099 |
+ const char **_username,
|
|
|
6cf099 |
+ struct sss_domain_info **_domain);
|
|
|
6cf099 |
+
|
|
|
6cf099 |
+#endif /* SRC_TOOLS_COMMON_SSS_TOOLS_H_ */
|
|
|
6cf099 |
--
|
|
|
6cf099 |
2.4.3
|
|
|
6cf099 |
|