e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina/*
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina Authors:
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina Pavel Březina <pbrezina@redhat.com>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina Copyright (C) 2016 Red Hat
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina This program is free software; you can redistribute it and/or modify
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina it under the terms of the GNU General Public License as published by
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina the Free Software Foundation; either version 3 of the License, or
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina (at your option) any later version.
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina This program is distributed in the hope that it will be useful,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina GNU General Public License for more details.
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina You should have received a copy of the GNU General Public License
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina*/
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include "config.h"
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <stdlib.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <limits.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <talloc.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <sys/types.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <dirent.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <ctype.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <signal.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <utime.h>
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include <ldb.h>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include <popt.h>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include <stdio.h>
cf098cbeef745b75d2efe8334d4d715bcef31883Lukas Slebodnik#include <signal.h>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include "util/util.h"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include "tools/common/sss_process.h"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include "tools/sssctl/sssctl.h"
68f73e56a9b6133f8a9f4b3c0e696df6c30fec91Pavel Březina#include "tools/tools_util.h"
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#include "confdb/confdb.h"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#define LOG_FILE(file) " " LOG_PATH "/" file
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#define LOG_FILES LOG_FILE("*.log")
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson#define CHECK(expr, done, msg) do { \
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (expr) { \
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ERROR(msg "\n"); \
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto done; \
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson } \
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson} while(0)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonstruct debuglevel_tool_ctx {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct confdb_ctx *confdb;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char **sections;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson};
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastruct sssctl_logs_opts {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int delete;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int archived;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina};
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonerrno_t set_debug_level(struct debuglevel_tool_ctx *tool_ctx,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int debug_to_set, const char *config_file)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson{
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int err;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson const char *values[2];
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char **section = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson TALLOC_CTX *tmp_ctx = talloc_new(NULL);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (tmp_ctx == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* convert debug_to_set to string */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson values[0] = talloc_asprintf(tmp_ctx, "0x%.4x", debug_to_set);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (values[0] == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto done;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson values[1] = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* write to confdb */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson for (section = tool_ctx->sections; *section != NULL; section++) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = confdb_add_param(tool_ctx->confdb, 1, *section,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_SERVICE_DEBUG_LEVEL, values);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (ret != EOK) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto done;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /*
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson * Change atime and mtime of sssd.conf,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson * so the configuration can be restored on next start.
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson errno = 0;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (utime(config_file, NULL) == -1) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson err = errno;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_MINOR_FAILURE, "Unable to change mtime of \"%s\": %s\n",
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson config_file, strerror(err));
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = EOK;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensondone:
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson talloc_free(tmp_ctx);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson}
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonerrno_t connect_to_confdb(TALLOC_CTX *ctx, struct confdb_ctx **cdb_ctx)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson{
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char *confdb_path = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson confdb_path = talloc_asprintf(ctx, "%s/%s", DB_PATH, CONFDB_FILE);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (confdb_path == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Could not allocate memory for confdb path\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = confdb_init(ctx, cdb_ctx, confdb_path);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (ret != EOK) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Could not initialize connection to the confdb\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson talloc_free(confdb_path);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson}
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonerrno_t get_confdb_sections(TALLOC_CTX *ctx, struct confdb_ctx *confdb,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char ***output_sections)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson{
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int domain_count = 0;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int i = 0;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct sss_domain_info *domain = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct sss_domain_info *domain_list = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char **sections;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson const char *known_services[] = {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_MONITOR_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_NSS_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_PAM_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_PAC_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_SSH_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_SUDO_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_AUTOFS_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CONFDB_IFP_CONF_ENTRY,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson };
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson static const int known_services_count = sizeof(known_services)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson / sizeof(*known_services);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson TALLOC_CTX *tmp_ctx = talloc_new(NULL);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (tmp_ctx == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* get domains */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = confdb_get_domains(confdb, &domain_list);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (ret != EOK)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "Unable to get domain list\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson for (domain = domain_list;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain = get_next_domain(domain, 0)) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain_count++;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* allocate output space */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson sections = talloc_array(ctx, char *,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain_count + known_services_count + 1);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (sections == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Could not allocate memory for sections\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto fail;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson for (i = 0; i < known_services_count; i++) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson sections[i] = talloc_strdup(tmp_ctx, known_services[i]);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (sections[i] == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto fail;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson for (domain = domain_list;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain = get_next_domain(domain, 0), i++) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson sections[i] = talloc_asprintf(tmp_ctx, CONFDB_DOMAIN_PATH_TMPL,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson domain->name);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (sections[i] == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf() failed\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto fail;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* add NULL to the end */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson sections[i] = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson *output_sections = talloc_steal(ctx, sections);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return EOK;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonfail:
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson talloc_free(tmp_ctx);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson}
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonint parse_debug_level(const char *strlevel)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson{
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson long value;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson char *endptr;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson errno = 0;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson value = strtol(strlevel, &endptr, 0);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if ((errno != 0) || (endptr == strlevel) || (*endptr != '\0')) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return SSSDBG_INVALID;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return debug_convert_old_level(value);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson}
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_logs_remove(struct sss_cmdline *cmdline,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina struct sss_tool_ctx *tool_ctx,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina void *pvt)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina struct sssctl_logs_opts opts = {0};
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina errno_t ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina /* Parse command line. */
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina struct poptOption options[] = {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina {"delete", 'd', POPT_ARG_NONE, &opts.delete, 0, _("Delete log files instead of truncating"), NULL },
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina POPT_TABLEEND
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina };
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sss_tool_popt(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (opts.delete) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina printf(_("Deleting log files...\n"));
5b93634c7f0e34f69b4cf8fb9b2e77b9179024a7Fabiano Fidêncio ret = sss_remove_subtree(LOG_PATH);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to remove log files\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina sss_signal(SIGHUP);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina } else {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina printf(_("Truncating log files...\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command("truncate --size 0 " LOG_FILES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to truncate log files\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_logs_fetch(struct sss_cmdline *cmdline,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina struct sss_tool_ctx *tool_ctx,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina void *pvt)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina const char *file;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina const char *cmd;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina errno_t ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina /* Parse command line. */
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sss_tool_popt_ex(cmdline, NULL, SSS_TOOL_OPT_OPTIONAL, NULL, NULL,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina "FILE", "Output file", &file, NULL);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina cmd = talloc_asprintf(tool_ctx, "tar -czf %s %s", file, LOG_FILES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (cmd == NULL) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Out of memory!"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina printf(_("Archiving log files into %s...\n"), file);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command(cmd);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to archive log files\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonerrno_t sssctl_debug_level(struct sss_cmdline *cmdline,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct sss_tool_ctx *tool_ctx,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson void *pvt)
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson{
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson int debug_to_set = SSSDBG_INVALID;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson const char *debug_as_string = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson const char *config_file = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson const char *pc_config_file = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct debuglevel_tool_ctx *ctx = NULL;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson struct poptOption long_options[] = {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson {"config", 'c', POPT_ARG_STRING, &pc_config_file,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson 0, _("Specify a non-default config file"), NULL},
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson POPT_TABLEEND
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson };
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik ret = sss_tool_popt_ex(cmdline, long_options, SSS_TOOL_OPT_OPTIONAL, NULL,
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik NULL, "DEBUG_LEVEL_TO_SET",
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik _("Specify debug level you want to set"),
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik &debug_as_string, NULL);
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik if (ret != EOK) {
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
40151421697db0910c454d1af441cce4962dfb0bLukas Slebodnik return ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* get config file */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (pc_config_file) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson config_file = talloc_strdup(ctx, pc_config_file);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson } else {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson config_file = talloc_strdup(ctx, SSSD_CONFIG_FILE);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (config_file == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto fini;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK_ROOT(ret, debug_prg_name);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* free pc_config_file? */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* free debug_as_string? */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson debug_to_set = parse_debug_level(debug_as_string);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK(debug_to_set == SSSDBG_INVALID, fini, "Invalid debug level.");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson /* allocate context */
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ctx = talloc_zero(NULL, struct debuglevel_tool_ctx);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson if (ctx == NULL) {
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson DEBUG(SSSDBG_CRIT_FAILURE,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Could not allocate memory for tools context\n");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = ENOMEM;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson goto fini;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson }
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = connect_to_confdb(ctx, &ctx->confdb);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK(ret != EOK, fini, "Could not connect to configuration database.");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = get_confdb_sections(ctx, ctx->confdb, &ctx->sections);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK(ret != EOK, fini, "Could not get all configuration sections.");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = set_debug_level(ctx, debug_to_set, config_file);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK(ret != EOK, fini, "Could not set debug level.");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson ret = sss_signal(SIGHUP);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson CHECK(ret != EOK, fini,
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Could not force sssd processes to reload configuration. "
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson "Is sssd running?");
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephensonfini:
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson talloc_free(ctx);
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson return ret;
d2c614143870e6efd4b3ab20c3a55cf714595256Justin Stephenson}