From 87c7a20749bc48f97de0f18df77193ed36da6076 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Fri, 1 Jul 2016 12:59:08 +0200
Subject: [PATCH 1/2] sssctl: restart SSSD when removing cache

Resolves:
https://fedorahosted.org/sssd/ticket/3066
---
 src/tools/sssctl/sssctl_data.c | 61 +++++++++++++++++++++++++++---------------
 1 file changed, 40 insertions(+), 21 deletions(-)

diff --git a/src/tools/sssctl/sssctl_data.c b/src/tools/sssctl/sssctl_data.c
index 31b7984a5cc664c7655bddb0d949b74a7a75e392..307696ff79d596ace6eb32a922df928c3d9cbd49 100644
--- a/src/tools/sssctl/sssctl_data.c
+++ b/src/tools/sssctl/sssctl_data.c
@@ -34,6 +34,7 @@
 
 struct sssctl_data_opts {
     int override;
+    int restore;
     int start;
     int stop;
     int restart;
@@ -148,31 +149,14 @@ errno_t sssctl_backup_local_data(struct sss_cmdline *cmdline,
     return ret;
 }
 
-errno_t sssctl_restore_local_data(struct sss_cmdline *cmdline,
-                                  struct sss_tool_ctx *tool_ctx,
-                                  void *pvt)
+static errno_t sssctl_restore(bool force_start, bool force_restart)
 {
-    struct sssctl_data_opts opts = {0};
     errno_t ret;
 
-    /* Parse command line. */
-    struct poptOption options[] = {
-        {"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD if it is not running"), NULL },
-        {"restart", 'r', POPT_ARG_NONE, &opts.restart, 0, _("Restart SSSD after data import"), NULL },
-        POPT_TABLEEND
-    };
-
-    ret = sss_tool_popt(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
-    if (ret != EOK) {
-        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
-        return ret;
-    }
-
-    if (!sssctl_start_sssd(opts.start)) {
+    if (!sssctl_start_sssd(force_start)) {
         return ERR_SSSD_NOT_RUNNING;
     }
 
-
     if (sssctl_backup_file_exists(SSS_BACKUP_USER_OVERRIDES)) {
         ret = sssctl_run_command("sss_override user-import "
                                  SSS_BACKUP_USER_OVERRIDES);
@@ -191,11 +175,36 @@ errno_t sssctl_restore_local_data(struct sss_cmdline *cmdline,
         }
     }
 
-    sssctl_restart_sssd(opts.restart);
+    sssctl_restart_sssd(force_restart);
+
+    ret = EOK;
 
     return ret;
 }
 
+errno_t sssctl_restore_local_data(struct sss_cmdline *cmdline,
+                                  struct sss_tool_ctx *tool_ctx,
+                                  void *pvt)
+{
+    struct sssctl_data_opts opts = {0};
+    errno_t ret;
+
+    /* Parse command line. */
+    struct poptOption options[] = {
+        {"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD if it is not running"), NULL },
+        {"restart", 'r', POPT_ARG_NONE, &opts.restart, 0, _("Restart SSSD after data import"), NULL },
+        POPT_TABLEEND
+    };
+
+    ret = sss_tool_popt(cmdline, options, SSS_TOOL_OPT_OPTIONAL, NULL, NULL);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse command arguments\n");
+        return ret;
+    }
+
+    return sssctl_restore(opts.start, opts.restart);
+}
+
 errno_t sssctl_remove_cache(struct sss_cmdline *cmdline,
                             struct sss_tool_ctx *tool_ctx,
                             void *pvt)
@@ -206,7 +215,9 @@ errno_t sssctl_remove_cache(struct sss_cmdline *cmdline,
     /* Parse command line. */
     struct poptOption options[] = {
         {"override", 'o', POPT_ARG_NONE, &opts.override, 0, _("Override existing backup"), NULL },
-        {"stop", 's', POPT_ARG_NONE, &opts.stop, 0, _("Stop SSSD if it is running"), NULL },
+        {"restore", 'r', POPT_ARG_NONE, &opts.restore, 0, _("Create clean cache files and import local data"), NULL },
+        {"stop", 'p', POPT_ARG_NONE, &opts.stop, 0, _("Stop SSSD before removing the cache"), NULL },
+        {"start", 's', POPT_ARG_NONE, &opts.start, 0, _("Start SSSD when the cache is removed"), NULL },
         POPT_TABLEEND
     };
 
@@ -217,6 +228,7 @@ errno_t sssctl_remove_cache(struct sss_cmdline *cmdline,
     }
 
     if (!sssctl_stop_sssd(opts.stop)) {
+        fprintf(stderr, "Unable to remove the cache unless SSSD is stopped.\n");
         return ERR_SSSD_RUNNING;
     }
 
@@ -235,5 +247,12 @@ errno_t sssctl_remove_cache(struct sss_cmdline *cmdline,
         return ret;
     }
 
+    if (opts.restore) {
+        printf(_("Restoring local data...\n"));
+        sssctl_restore(opts.start, opts.start);
+    } else {
+        sssctl_start_sssd(opts.start);
+    }
+
     return EOK;
 }
-- 
2.1.0

