diff options
author | Justin Stephenson <jstephen@redhat.com> | 2017-05-12 23:10:18 -0400 |
---|---|---|
committer | Lukas Slebodnik <lslebodn@redhat.com> | 2017-09-25 21:50:21 +0200 |
commit | d2c614143870e6efd4b3ab20c3a55cf714595256 (patch) | |
tree | c0fd87110bdf25d0d961a83079c4c68fc58b70ea | |
parent | 1024dbcba0c16fb5df5e6d16894a8c7a36dc75f2 (diff) | |
download | sssd-d2c614143870e6efd4b3ab20c3a55cf714595256.tar.gz sssd-d2c614143870e6efd4b3ab20c3a55cf714595256.tar.xz sssd-d2c614143870e6efd4b3ab20c3a55cf714595256.zip |
SSSCTL: Move sss_debuglevel to sssctl debug-level
Move code from sss_debuglevel to sssctl_logs.c and add new debug-logs
sssctl command to perform the same task of changing debug level
dynamically.
POPT_CONTEXT_KEEP_FIRST Flag added to poptGetContext call in
sssctl_debug_level() to fix argument parsing.
Resolves:
https://pagure.io/SSSD/sssd/issue/3057
Reviewed-by: Lukáš Slebodník <lslebodn@redhat.com>
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
Reviewed-by: Michal Židek <mzidek@redhat.com>
-rw-r--r-- | Makefile.am | 8 | ||||
-rw-r--r-- | contrib/sssd.spec.in | 2 | ||||
-rw-r--r-- | po/POTFILES.in | 1 | ||||
-rw-r--r-- | src/tools/common/sss_tools.c | 7 | ||||
-rw-r--r-- | src/tools/common/sss_tools.h | 7 | ||||
-rw-r--r-- | src/tools/sss_debuglevel.c | 323 | ||||
-rw-r--r-- | src/tools/sssctl/sssctl.c | 1 | ||||
-rw-r--r-- | src/tools/sssctl/sssctl.h | 4 | ||||
-rw-r--r-- | src/tools/sssctl/sssctl_logs.c | 292 |
9 files changed, 304 insertions, 341 deletions
diff --git a/Makefile.am b/Makefile.am index c0586d7ec..352401ce5 100644 --- a/Makefile.am +++ b/Makefile.am @@ -143,7 +143,6 @@ sbin_PROGRAMS = \ sss_groupmod \ sss_groupshow \ sss_cache \ - sss_debuglevel \ sss_override \ sss_seed \ sssctl \ @@ -1693,13 +1692,6 @@ sss_cache_LDADD = \ $(CLIENT_LIBS) sss_cache_CFLAGS = $(AM_CFLAGS) -sss_debuglevel_SOURCES = \ - src/tools/sss_debuglevel.c \ - $(SSSD_TOOLS_OBJ) -sss_debuglevel_LDADD = \ - $(TOOLS_LIBS) \ - $(SSSD_INTERNAL_LTLIBS) - sss_seed_SOURCES = \ src/tools/sss_seed.c \ $(SSSD_TOOLS_OBJ) diff --git a/contrib/sssd.spec.in b/contrib/sssd.spec.in index e76b51833..48ee3d7b9 100644 --- a/contrib/sssd.spec.in +++ b/contrib/sssd.spec.in @@ -1127,7 +1127,7 @@ done %{_sbindir}/sss_groupshow %{_sbindir}/sss_obfuscate %{_sbindir}/sss_override -%{_sbindir}/sss_debuglevel +#%{_sbindir}/sss_debuglevel %{_sbindir}/sss_seed %{_sbindir}/sssctl %{_mandir}/man8/sss_groupadd.8* diff --git a/po/POTFILES.in b/po/POTFILES.in index 33e7ed7f9..f1eb344c0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -20,7 +20,6 @@ src/tools/sss_useradd.c src/tools/sss_userdel.c src/tools/sss_usermod.c src/tools/sss_cache.c -src/tools/sss_debuglevel.c src/tools/tools_util.c src/tools/tools_util.h src/tools/sssctl/sssctl.c diff --git a/src/tools/common/sss_tools.c b/src/tools/common/sss_tools.c index 97a3caab3..0b676341f 100644 --- a/src/tools/common/sss_tools.c +++ b/src/tools/common/sss_tools.c @@ -30,13 +30,6 @@ #include "db/sysdb.h" #include "tools/common/sss_tools.h" -struct sss_cmdline { - const char *exec; /* argv[0] */ - const char *command; /* command name */ - int argc; /* rest of arguments */ - const char **argv; -}; - static void sss_tool_print_common_opts(int min_len) { fprintf(stderr, _("Common options:\n")); diff --git a/src/tools/common/sss_tools.h b/src/tools/common/sss_tools.h index 49da7d634..848009365 100644 --- a/src/tools/common/sss_tools.h +++ b/src/tools/common/sss_tools.h @@ -38,7 +38,12 @@ errno_t sss_tool_init(TALLOC_CTX *mem_ctx, int *argc, const char **argv, struct sss_tool_ctx **_tool_ctx); -struct sss_cmdline; +struct sss_cmdline { + const char *exec; /* argv[0] */ + const char *command; /* command name */ + int argc; /* rest of arguments */ + const char **argv; +}; typedef errno_t (*sss_route_fn)(struct sss_cmdline *cmdline, diff --git a/src/tools/sss_debuglevel.c b/src/tools/sss_debuglevel.c deleted file mode 100644 index e8b156ea1..000000000 --- a/src/tools/sss_debuglevel.c +++ /dev/null @@ -1,323 +0,0 @@ -/* - Authors: - Pavel Březina <pbrezina@redhat.com> - - Copyright (C) 2011 Red Hat - - 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 <stdio.h> -#include <stdlib.h> -#include <limits.h> -#include <talloc.h> -#include <popt.h> -#include <sys/types.h> -#include <dirent.h> -#include <ctype.h> -#include <signal.h> -#include <utime.h> -#include <ldb.h> - -#include "config.h" -#include "util/util.h" -#include "tools/tools_util.h" -#include "tools/common/sss_process.h" -#include "confdb/confdb.h" - -#define CHECK(expr, done, msg) do { \ - if (expr) { \ - ERROR(msg "\n"); \ - goto done; \ - } \ -} while(0) - -struct debuglevel_tool_ctx { - struct confdb_ctx *confdb; - char **sections; -}; - -static errno_t set_debug_level(struct debuglevel_tool_ctx *tool_ctx, - int debug_to_set, const char *config_file); -static errno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx); -static errno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb, - char ***output_sections); -static int parse_debug_level(const char *strlevel); - -int main(int argc, const char **argv) -{ - int ret; - int pc_debug = SSSDBG_DEFAULT; - int debug_to_set = SSSDBG_INVALID; - const char *debug_as_string = NULL; - const char *config_file = NULL; - const char *pc_config_file = NULL; - struct debuglevel_tool_ctx *ctx = NULL; - struct poptOption long_options[] = { - POPT_AUTOHELP - {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, - 0, _("The debug level to run with"), NULL }, - {"config", 'c', POPT_ARG_STRING, &pc_config_file, - 0, _("Specify a non-default config file"), NULL}, - POPT_TABLEEND - }; - poptContext pc = NULL; - - debug_prg_name = argv[0]; - - /* parse parameters */ - pc = poptGetContext(argv[0], argc, argv, long_options, 0); - poptSetOtherOptionHelp(pc, "DEBUG_LEVEL_TO_SET"); - while((ret = poptGetNextOpt(pc)) != -1) { - switch(ret) { - default: - fprintf(stderr, "\nInvalid option %s: %s\n\n", - poptBadOption(pc, 0), poptStrerror(ret)); - poptPrintUsage(pc, stderr, 0); - ret = EXIT_FAILURE; - goto fini; - } - } - DEBUG_CLI_INIT(pc_debug); - - /* get debug level */ - debug_as_string = poptGetArg(pc); - if (debug_as_string == NULL) { - BAD_POPT_PARAMS(pc, _("Specify debug level you want to set\n"), - ret, fini); - } - - /* No more arguments expected. If something follows it is an error. */ - if (poptGetArg(pc)) { - BAD_POPT_PARAMS(pc, _("Only one argument expected\n"), - ret, fini); - } - - /* get config file */ - if (pc_config_file) { - config_file = talloc_strdup(ctx, pc_config_file); - } else { - config_file = talloc_strdup(ctx, SSSD_CONFIG_FILE); - } - - if (config_file == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n"); - ret = ENOMEM; - goto fini; - } - - CHECK_ROOT(ret, debug_prg_name); - - /* free pc_config_file? */ - /* free debug_as_string? */ - - debug_to_set = parse_debug_level(debug_as_string); - CHECK(debug_to_set == SSSDBG_INVALID, fini, "Invalid debug level."); - - /* allocate context */ - ctx = talloc_zero(NULL, struct debuglevel_tool_ctx); - if (ctx == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not allocate memory for tools context\n"); - ret = ENOMEM; - goto fini; - } - - ret = connect_to_confdb(ctx, &ctx->confdb); - CHECK(ret != EOK, fini, "Could not connect to configuration database."); - - ret = get_confdb_sections(ctx, ctx->confdb, &ctx->sections); - CHECK(ret != EOK, fini, "Could not get all configuration sections."); - - ret = set_debug_level(ctx, debug_to_set, config_file); - CHECK(ret != EOK, fini, "Could not set debug level."); - - ret = sss_signal(SIGHUP); - CHECK(ret != EOK, fini, - "Could not force sssd processes to reload configuration. " - "Is sssd running?"); - -fini: - poptFreeContext(pc); - talloc_free(ctx); - return ret; -} - -errno_t set_debug_level(struct debuglevel_tool_ctx *tool_ctx, - int debug_to_set, const char *config_file) -{ - int ret; - int err; - const char *values[2]; - char **section = NULL; - TALLOC_CTX *tmp_ctx = talloc_new(NULL); - - if (tmp_ctx == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n"); - return ENOMEM; - } - - /* convert debug_to_set to string */ - values[0] = talloc_asprintf(tmp_ctx, "0x%.4x", debug_to_set); - if (values[0] == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "Could not allocate memory for " - "debug_to_set to string conversion\n"); - ret = ENOMEM; - goto done; - } - values[1] = NULL; - - /* write to confdb */ - for (section = tool_ctx->sections; *section != NULL; section++) { - ret = confdb_add_param(tool_ctx->confdb, 1, *section, - CONFDB_SERVICE_DEBUG_LEVEL, values); - if (ret != EOK) { - goto done; - } - } - - /* - * Change atime and mtime of sssd.conf, - * so the configuration can be restored on next start. - */ - errno = 0; - if (utime(config_file, NULL) == -1 ) { - err = errno; - DEBUG(SSSDBG_MINOR_FAILURE, "Unable to change mtime of \"%s\": %s\n", - config_file, strerror(err)); - } - - ret = EOK; - -done: - talloc_free(tmp_ctx); - return ret; -} - -errno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx) -{ - int ret; - char* confdb_path = NULL; - - confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE); - if (confdb_path == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not allocate memory for confdb path\n"); - return ENOMEM; - } - - ret = confdb_init(ctx, cdb_ctx, confdb_path); - if (ret != EOK) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not initialize connection to the confdb\n"); - } - - talloc_free(confdb_path); - return ret; -} - -errno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb, - char ***output_sections) -{ - int ret; - int domain_count = 0; - int i = 0; - struct sss_domain_info *domain = NULL; - struct sss_domain_info *domain_list = NULL; - char **sections; - const char *known_services[] = { - CONFDB_MONITOR_CONF_ENTRY, - CONFDB_NSS_CONF_ENTRY, - CONFDB_PAM_CONF_ENTRY, - CONFDB_PAC_CONF_ENTRY, - CONFDB_SSH_CONF_ENTRY, - CONFDB_SUDO_CONF_ENTRY, - CONFDB_AUTOFS_CONF_ENTRY, - CONFDB_IFP_CONF_ENTRY, - }; - static const int known_services_count = sizeof(known_services) - / sizeof(*known_services); - TALLOC_CTX *tmp_ctx = talloc_new(NULL); - - if (tmp_ctx == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n"); - return ENOMEM; - } - - /* get domains */ - ret = confdb_get_domains(confdb, &domain_list); - if (ret != EOK) - DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get domain list\n"); - - for (domain = domain_list; - domain; - domain = get_next_domain(domain, 0)) { - domain_count++; - } - - /* allocate output space */ - sections = talloc_array(ctx, char*, - domain_count + known_services_count + 1); - if (sections == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, - "Could not allocate memory for sections\n"); - ret = ENOMEM; - goto fail; - } - - for (i = 0; i < known_services_count; i++) { - sections[i] = talloc_strdup(tmp_ctx, known_services[i]); - if (sections[i] == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n"); - ret = ENOMEM; - goto fail; - } - } - - for (domain = domain_list; - domain; - domain = get_next_domain(domain, 0), i++) { - sections[i] = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL, - domain->name); - if (sections[i] == NULL) { - DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n"); - ret = ENOMEM; - goto fail; - } - } - - /* add NULL to the end */ - sections[i] = NULL; - - *output_sections = talloc_steal(ctx, sections); - - return EOK; -fail: - talloc_free(tmp_ctx); - return ret; -} - -int parse_debug_level(const char *strlevel) -{ - long value; - char *endptr; - - errno = 0; - value = strtol(strlevel, &endptr, 0); - if ((errno != 0) || (endptr == strlevel) || (*endptr != '\0')) { - return SSSDBG_INVALID; - } - - return debug_convert_old_level(value); -} diff --git a/src/tools/sssctl/sssctl.c b/src/tools/sssctl/sssctl.c index 509d2e1a0..5b66fcd1a 100644 --- a/src/tools/sssctl/sssctl.c +++ b/src/tools/sssctl/sssctl.c @@ -276,6 +276,7 @@ int main(int argc, const char **argv) SSS_TOOL_DELIMITER("Log files tools:"), SSS_TOOL_COMMAND("logs-remove", "Remove existing SSSD log files", 0, sssctl_logs_remove), SSS_TOOL_COMMAND("logs-fetch", "Archive SSSD log files in tarball", 0, sssctl_logs_fetch), + SSS_TOOL_COMMAND("debug-level", "Change SSSD debug level", 0, sssctl_debug_level), #ifdef HAVE_LIBINI_CONFIG_V1_3 SSS_TOOL_DELIMITER("Configuration files tools:"), SSS_TOOL_COMMAND_FLAGS("config-check", "Perform static analysis of SSSD configuration", 0, sssctl_config_check, SSS_TOOL_FLAG_SKIP_CMD_INIT), diff --git a/src/tools/sssctl/sssctl.h b/src/tools/sssctl/sssctl.h index 22626e221..9b3fd66a8 100644 --- a/src/tools/sssctl/sssctl.h +++ b/src/tools/sssctl/sssctl.h @@ -106,6 +106,10 @@ errno_t sssctl_logs_fetch(struct sss_cmdline *cmdline, struct sss_tool_ctx *tool_ctx, void *pvt); +errno_t sssctl_debug_level(struct sss_cmdline *cmdline, + struct sss_tool_ctx *tool_ctx, + void *pvt); + errno_t sssctl_user_show(struct sss_cmdline *cmdline, struct sss_tool_ctx *tool_ctx, void *pvt); diff --git a/src/tools/sssctl/sssctl_logs.c b/src/tools/sssctl/sssctl_logs.c index 472a553d1..32e0a7ac5 100644 --- a/src/tools/sssctl/sssctl_logs.c +++ b/src/tools/sssctl/sssctl_logs.c @@ -18,6 +18,17 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include "config.h" + +#include <stdlib.h> +#include <limits.h> +#include <talloc.h> +#include <sys/types.h> +#include <dirent.h> +#include <ctype.h> +#include <signal.h> +#include <utime.h> +#include <ldb.h> #include <popt.h> #include <stdio.h> #include <signal.h> @@ -26,15 +37,194 @@ #include "tools/common/sss_process.h" #include "tools/sssctl/sssctl.h" #include "tools/tools_util.h" +#include "confdb/confdb.h" #define LOG_FILE(file) " " LOG_PATH "/" file #define LOG_FILES LOG_FILE("*.log") +#define CHECK(expr, done, msg) do { \ + if (expr) { \ + ERROR(msg "\n"); \ + goto done; \ + } \ +} while(0) + +struct debuglevel_tool_ctx { + struct confdb_ctx *confdb; + char **sections; +}; + struct sssctl_logs_opts { int delete; int archived; }; +errno_t set_debug_level(struct debuglevel_tool_ctx *tool_ctx, + int debug_to_set, const char *config_file) +{ + int ret; + int err; + const char *values[2]; + char **section = NULL; + TALLOC_CTX *tmp_ctx = talloc_new(NULL); + + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n"); + return ENOMEM; + } + + /* convert debug_to_set to string */ + values[0] = talloc_asprintf(tmp_ctx, "0x%.4x", debug_to_set); + if (values[0] == NULL) { + ret = ENOMEM; + goto done; + } + values[1] = NULL; + + /* write to confdb */ + for (section = tool_ctx->sections; *section != NULL; section++) { + ret = confdb_add_param(tool_ctx->confdb, 1, *section, + CONFDB_SERVICE_DEBUG_LEVEL, values); + if (ret != EOK) { + goto done; + } + } + + /* + * Change atime and mtime of sssd.conf, + * so the configuration can be restored on next start. + */ + errno = 0; + if (utime(config_file, NULL) == -1) { + err = errno; + DEBUG(SSSDBG_MINOR_FAILURE, "Unable to change mtime of \"%s\": %s\n", + config_file, strerror(err)); + } + + ret = EOK; + +done: + talloc_free(tmp_ctx); + return ret; +} + +errno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx) +{ + int ret; + char *confdb_path = NULL; + + confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE); + if (confdb_path == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Could not allocate memory for confdb path\n"); + return ENOMEM; + } + + ret = confdb_init(ctx, cdb_ctx, confdb_path); + if (ret != EOK) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Could not initialize connection to the confdb\n"); + } + + talloc_free(confdb_path); + return ret; +} + +errno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb, + char ***output_sections) +{ + int ret; + int domain_count = 0; + int i = 0; + struct sss_domain_info *domain = NULL; + struct sss_domain_info *domain_list = NULL; + char **sections; + const char *known_services[] = { + CONFDB_MONITOR_CONF_ENTRY, + CONFDB_NSS_CONF_ENTRY, + CONFDB_PAM_CONF_ENTRY, + CONFDB_PAC_CONF_ENTRY, + CONFDB_SSH_CONF_ENTRY, + CONFDB_SUDO_CONF_ENTRY, + CONFDB_AUTOFS_CONF_ENTRY, + CONFDB_IFP_CONF_ENTRY, + }; + static const int known_services_count = sizeof(known_services) + / sizeof(*known_services); + TALLOC_CTX *tmp_ctx = talloc_new(NULL); + + if (tmp_ctx == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n"); + return ENOMEM; + } + + /* get domains */ + ret = confdb_get_domains(confdb, &domain_list); + if (ret != EOK) + DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get domain list\n"); + + for (domain = domain_list; + domain; + domain = get_next_domain(domain, 0)) { + domain_count++; + } + + /* allocate output space */ + sections = talloc_array(ctx, char *, + domain_count + known_services_count + 1); + if (sections == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Could not allocate memory for sections\n"); + ret = ENOMEM; + goto fail; + } + + for (i = 0; i < known_services_count; i++) { + sections[i] = talloc_strdup(tmp_ctx, known_services[i]); + if (sections[i] == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n"); + ret = ENOMEM; + goto fail; + } + } + + for (domain = domain_list; + domain; + domain = get_next_domain(domain, 0), i++) { + sections[i] = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL, + domain->name); + if (sections[i] == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n"); + ret = ENOMEM; + goto fail; + } + } + + /* add NULL to the end */ + sections[i] = NULL; + + *output_sections = talloc_steal(ctx, sections); + + return EOK; +fail: + talloc_free(tmp_ctx); + return ret; +} + +int parse_debug_level(const char *strlevel) +{ + long value; + char *endptr; + + errno = 0; + value = strtol(strlevel, &endptr, 0); + if ((errno != 0) || (endptr == strlevel) || (*endptr != '\0')) { + return SSSDBG_INVALID; + } + + return debug_convert_old_level(value); +} + errno_t sssctl_logs_remove(struct sss_cmdline *cmdline, struct sss_tool_ctx *tool_ctx, void *pvt) @@ -105,3 +295,105 @@ errno_t sssctl_logs_fetch(struct sss_cmdline *cmdline, return EOK; } + +errno_t sssctl_debug_level(struct sss_cmdline *cmdline, + struct sss_tool_ctx *tool_ctx, + void *pvt) +{ + int ret; + int pc_debug = SSSDBG_DEFAULT; + int debug_to_set = SSSDBG_INVALID; + const char *debug_as_string = NULL; + const char *config_file = NULL; + const char *pc_config_file = NULL; + struct debuglevel_tool_ctx *ctx = NULL; + struct poptOption long_options[] = { + POPT_AUTOHELP + {"debug", '\0', POPT_ARG_INT | POPT_ARGFLAG_DOC_HIDDEN, &pc_debug, + 0, _("The debug level to run with"), NULL }, + {"config", 'c', POPT_ARG_STRING, &pc_config_file, + 0, _("Specify a non-default config file"), NULL}, + POPT_TABLEEND + }; + poptContext pc = NULL; + + debug_prg_name = cmdline->argv[0]; + + /* parse parameters */ + pc = poptGetContext(cmdline->argv[0], cmdline->argc, cmdline->argv, + long_options, POPT_CONTEXT_KEEP_FIRST); + poptSetOtherOptionHelp(pc, "DEBUG_LEVEL_TO_SET"); + while ((ret = poptGetNextOpt(pc)) != -1) { + switch (ret) { + default: + fprintf(stderr, "\nInvalid option %s: %s\n\n", + poptBadOption(pc, 0), poptStrerror(ret)); + poptPrintUsage(pc, stderr, 0); + ret = EXIT_FAILURE; + goto fini; + } + } + DEBUG_CLI_INIT(pc_debug); + + /* get debug level */ + debug_as_string = poptGetArg(pc); + if (debug_as_string == NULL) { + BAD_POPT_PARAMS(pc, _("Specify debug level you want to set\n"), + ret, fini); + } + + /* No more arguments expected. If something follows it is an error. */ + if (poptGetArg(pc)) { + BAD_POPT_PARAMS(pc, _("Only one argument expected\n"), + ret, fini); + } + + /* get config file */ + if (pc_config_file) { + config_file = talloc_strdup(ctx, pc_config_file); + } else { + config_file = talloc_strdup(ctx, SSSD_CONFIG_FILE); + } + + if (config_file == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n"); + ret = ENOMEM; + goto fini; + } + + CHECK_ROOT(ret, debug_prg_name); + + /* free pc_config_file? */ + /* free debug_as_string? */ + + debug_to_set = parse_debug_level(debug_as_string); + CHECK(debug_to_set == SSSDBG_INVALID, fini, "Invalid debug level."); + + /* allocate context */ + ctx = talloc_zero(NULL, struct debuglevel_tool_ctx); + if (ctx == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, + "Could not allocate memory for tools context\n"); + ret = ENOMEM; + goto fini; + } + + ret = connect_to_confdb(ctx, &ctx->confdb); + CHECK(ret != EOK, fini, "Could not connect to configuration database."); + + ret = get_confdb_sections(ctx, ctx->confdb, &ctx->sections); + CHECK(ret != EOK, fini, "Could not get all configuration sections."); + + ret = set_debug_level(ctx, debug_to_set, config_file); + CHECK(ret != EOK, fini, "Could not set debug level."); + + ret = sss_signal(SIGHUP); + CHECK(ret != EOK, fini, + "Could not force sssd processes to reload configuration. " + "Is sssd running?"); + +fini: + poptFreeContext(pc); + talloc_free(ctx); + return ret; +} |