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
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include <popt.h>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include <stdio.h>
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include "util/util.h"
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek#include "db/sysdb.h"
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek#include "tools/common/sss_process.h"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#include "tools/sssctl/sssctl.h"
68f73e56a9b6133f8a9f4b3c0e696df6c30fec91Pavel Březina#include "tools/tools_util.h"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#define SSS_BACKUP_DIR SSS_STATEDIR "/backup"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#define SSS_BACKUP_USER_OVERRIDES SSS_BACKUP_DIR "/sssd_user_overrides.bak"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina#define SSS_BACKUP_GROUP_OVERRIDES SSS_BACKUP_DIR "/sssd_group_overrides.bak"
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson#define SSS_CACHE "sss_cache"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastruct sssctl_data_opts {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int override;
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina int restore;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int start;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int stop;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int restart;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina};
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastatic errno_t sssctl_create_backup_dir(const char *path)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina mode_t old_umask;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina errno_t ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina old_umask = umask(SSS_DFL_X_UMASK);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = mkdir(path, 0700);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina umask(old_umask);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK && errno != EEXIST) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = errno;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina DEBUG(SSSDBG_MINOR_FAILURE, "Unable to create backup directory "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina "[%d]: %s\n", ret, sss_strerror(ret));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastatic bool sssctl_backup_file_exists(const char *file)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return access(file, F_OK) == 0;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastatic bool sssctl_backup_exist(const char **files)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina int i;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina for (i = 0; files[i] != NULL; i++) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (sssctl_backup_file_exists(files[i])) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return true;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return false;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březinastatic errno_t sssctl_backup(bool force)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina const char *files[] = {SSS_BACKUP_USER_OVERRIDES,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina SSS_BACKUP_GROUP_OVERRIDES,
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina NULL};
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina enum sssctl_prompt_result prompt;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina errno_t ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_create_backup_dir(SSS_BACKUP_DIR);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to create backup directory [%d]: %s"),
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret, sss_strerror(ret));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (sssctl_backup_exist(files) && !force) {
9be0fce9bc1f48241425726ddd612ffd64556715Fabiano Fidêncio prompt = sssctl_prompt(_("SSSD backup of local data already exists, "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina "override?"), SSSCTL_PROMPT_NO);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina switch (prompt) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina case SSSCTL_PROMPT_YES:
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina /* continue */
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina break;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina case SSSCTL_PROMPT_NO:
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EEXIST;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina case SSSCTL_PROMPT_ERROR:
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EIO;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command("sss_override user-export "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina SSS_BACKUP_USER_OVERRIDES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to export user overrides\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command("sss_override group-export "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina SSS_BACKUP_GROUP_OVERRIDES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to export group overrides\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_client_data_backup(struct sss_cmdline *cmdline,
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židek struct sss_tool_ctx *tool_ctx,
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židek void *pvt)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina struct sssctl_data_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 {"override", 'o', POPT_ARG_NONE, &opts.override, 0, _("Override existing backup"), 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 ret = sssctl_backup(opts.override);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret == EEXIST) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březinastatic errno_t sssctl_restore(bool force_start, bool force_restart)
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina{
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina errno_t ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina if (!sssctl_start_sssd(force_start)) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ERR_SSSD_NOT_RUNNING;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (sssctl_backup_file_exists(SSS_BACKUP_USER_OVERRIDES)) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command("sss_override user-import "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina SSS_BACKUP_USER_OVERRIDES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to import user overrides\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (sssctl_backup_file_exists(SSS_BACKUP_USER_OVERRIDES)) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_run_command("sss_override group-import "
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina SSS_BACKUP_GROUP_OVERRIDES);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to import group overrides\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina sssctl_restart_sssd(force_restart);
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina ret = EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_client_data_restore(struct sss_cmdline *cmdline,
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židek struct sss_tool_ctx *tool_ctx,
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židek void *pvt)
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina{
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina struct sssctl_data_opts opts = {0};
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina errno_t ret;
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina /* Parse command line. */
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina struct poptOption options[] = {
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina {"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD if it is not running"), NULL },
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina {"restart", 'r', POPT_ARG_NONE, &opts.restart, 0, _("Restart SSSD after data import"), NULL },
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina POPT_TABLEEND
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina };
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina ret = sss_tool_popt(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina if (ret != EOK) {
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina return ret;
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina }
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina return sssctl_restore(opts.start, opts.restart);
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina}
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_cache_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_data_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 {"override", 'o', POPT_ARG_NONE, &opts.override, 0, _("Override existing backup"), NULL },
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina {"restore", 'r', POPT_ARG_NONE, &opts.restore, 0, _("Create clean cache files and import local data"), NULL },
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina {"stop", 'p', POPT_ARG_NONE, &opts.stop, 0, _("Stop SSSD before removing the cache"), NULL },
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina {"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD when the cache is removed"), 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 (!sssctl_stop_sssd(opts.stop)) {
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina fprintf(stderr, "Unable to remove the cache unless SSSD is stopped.\n");
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ERR_SSSD_RUNNING;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina printf(_("Creating backup of local data...\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina ret = sssctl_backup(opts.override);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to create backup of local data,"
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina " can not remove the cache.\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina printf(_("Removing cache files...\n"));
5b93634c7f0e34f69b4cf8fb9b2e77b9179024a7Fabiano Fidêncio ret = sss_remove_subtree(DB_PATH);
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina if (ret != EOK) {
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina fprintf(stderr, _("Unable to remove cache files\n"));
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return ret;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina }
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina if (opts.restore) {
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina printf(_("Restoring local data...\n"));
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina sssctl_restore(opts.start, opts.start);
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina } else {
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina sssctl_start_sssd(opts.start);
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina }
b6cc93b259b96408a827a4349a98600c0b2f6660Pavel Březina
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina return EOK;
e157b9f6cb370e1b94bcac2044d26ad66d640fbaPavel Březina}
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek
488b455f6b7881ec108a127840b1c1f1523d937fMichal Židekerrno_t sssctl_cache_upgrade(struct sss_cmdline *cmdline,
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek struct sss_tool_ctx *tool_ctx,
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek void *pvt)
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek{
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek struct sysdb_upgrade_ctx db_up_ctx;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek errno_t ret;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek ret = sss_tool_popt(cmdline, NULL, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek if (ret != EOK) {
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek return ret;
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek }
55857e924977dbc66958f8033c6b38d6262ee631Michal Židek
a02a5ed51178b2cbede0396d66aed716b8898096René Genz if (sss_daemon_running()) {
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek return ERR_SSSD_RUNNING;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek }
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek ret = confdb_get_domains(tool_ctx->confdb, &tool_ctx->domains);
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek if (ret != EOK) {
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "No domains configured.\n");
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek return ret;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek }
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek db_up_ctx.cdb = tool_ctx->confdb;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek ret = sysdb_init_ext(tool_ctx, tool_ctx->domains, &db_up_ctx,
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek true, 0, 0);
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek if (ret != EOK) {
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek SYSDB_VERSION_ERROR_DAEMON(ret);
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek return ret;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek }
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek return EOK;
552390afcc81af96ca201fa6c25ddefbbecbeb4eJakub Hrozek}
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephensonerrno_t sssctl_cache_expire(struct sss_cmdline *cmdline,
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson struct sss_tool_ctx *tool_ctx,
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson void *pvt)
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson{
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson errno_t ret;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson char *cmd_args = NULL;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson const char *cachecmd = SSS_CACHE;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson char *cmd = NULL;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson int i;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson if (cmdline->argc == 0) {
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson ret = sssctl_run_command(cachecmd);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson goto done;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson }
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson cmd_args = talloc_strdup(tool_ctx, "");
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson if (cmd_args == NULL) {
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson ret = ENOMEM;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson goto done;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson }
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson for (i = 0; i < cmdline->argc; i++) {
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson cmd_args = talloc_strdup_append(cmd_args, cmdline->argv[i]);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson if (i != cmdline->argc - 1) {
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson cmd_args = talloc_strdup_append(cmd_args, " ");
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson }
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson }
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson cmd = talloc_asprintf(tool_ctx, "%s %s", cachecmd, cmd_args);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson if (cmd == NULL) {
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson ret = ENOMEM;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson goto done;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson }
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson ret = sssctl_run_command(cmd);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephensondone:
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson talloc_free(cmd_args);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson talloc_free(cmd);
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson return ret;
f74408e37a3007aa41b19ab2afb693a91694da42Justin Stephenson}