From c4f8b4f757ee9152d7abba9cb6005eca4f9ff43a Mon Sep 17 00:00:00 2001
From: Jakub Hrozek <jhrozek@redhat.com>
Date: Mon, 22 Mar 2010 22:10:29 +0100
Subject: [PATCH] Add userdel_cmd param

Fixes: #229
---
 src/confdb/confdb.h     |    1 +
 src/man/sssd.conf.5.xml |   14 +++++++++++++
 src/python/pysss.c      |    6 +++++
 src/tools/sss_userdel.c |    6 +++++
 src/tools/tools_util.c  |   49 +++++++++++++++++++++++++++++++++++++++++++++++
 src/tools/tools_util.h  |    2 +
 6 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 452fbdc..0e0a1b1 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -111,6 +111,7 @@
 #define CONFDB_LOCAL_UMASK           "homedir_umask"
 #define CONFDB_LOCAL_SKEL_DIR        "skel_dir"
 #define CONFDB_LOCAL_MAIL_DIR        "mail_dir"
+#define CONFDB_LOCAL_USERDEL_CMD     "userdel_cmd"
 
 /* Proxy Provider */
 #define CONFDB_PROXY_LIBNAME "proxy_lib_name"
diff --git a/src/man/sssd.conf.5.xml b/src/man/sssd.conf.5.xml
index daf61af..93bc219 100644
--- a/src/man/sssd.conf.5.xml
+++ b/src/man/sssd.conf.5.xml
@@ -784,6 +784,20 @@
                         </para>
                     </listitem>
                 </varlistentry>
+                <varlistentry>
+                    <term>userdel_cmd (string)</term>
+                    <listitem>
+                        <para>
+                            The command that is run after a user is removed.
+                            The command us passed the username of the user being
+                            removed as the first and only parameter. The return
+                            code of the command is not taken into account.
+                        </para>
+                        <para>
+                            Default: None, no command is run
+                        </para>
+                    </listitem>
+                </varlistentry>
             </variablelist>
         </refsect2>
 
diff --git a/src/python/pysss.c b/src/python/pysss.c
index 8011ed6..9397538 100644
--- a/src/python/pysss.c
+++ b/src/python/pysss.c
@@ -366,6 +366,12 @@ static PyObject *py_sss_userdel(PySssLocalObject *self,
         goto fail;
     }
 
+    ret = run_userdel_cmd(tctx);
+    if (ret != EOK) {
+        PyErr_SetSssError(ret);
+        goto fail;
+    }
+
     if (tctx->octx->remove_homedir) {
         ret = sysdb_getpwnam_sync(tctx,
                                   tctx->ev,
diff --git a/src/tools/sss_userdel.c b/src/tools/sss_userdel.c
index 227a688..8fe4d83 100644
--- a/src/tools/sss_userdel.c
+++ b/src/tools/sss_userdel.c
@@ -203,6 +203,12 @@ int main(int argc, const char **argv)
 
     end_transaction(tctx);
 
+    ret = run_userdel_cmd(tctx);
+    if (ret != EOK) {
+        ERROR("The post-delete command failed: %s\n", strerror(ret));
+        goto fini;
+    }
+
     if (tctx->octx->remove_homedir) {
         ret = remove_homedir(tctx,
                              tctx->octx->home,
diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c
index 9794523..81f85e8 100644
--- a/src/tools/tools_util.c
+++ b/src/tools/tools_util.c
@@ -25,6 +25,7 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/wait.h>
 #include <fcntl.h>
 
 #include "config.h"
@@ -518,3 +519,51 @@ done:
     return ret;
 }
 
+int run_userdel_cmd(struct tools_ctx *tctx)
+{
+    int ret;
+    char *cmd = NULL;
+    char *userdel_cmd = NULL;
+    char *conf_path = NULL;
+
+    conf_path = talloc_asprintf(tctx, CONFDB_DOMAIN_PATH_TMPL,
+                                      tctx->local->name);
+    if (!conf_path) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = confdb_get_string(tctx->confdb, tctx,
+                            conf_path, CONFDB_LOCAL_USERDEL_CMD,
+                            NULL, &userdel_cmd);
+    if (ret != EOK || !userdel_cmd) {
+        goto done;
+    }
+
+    cmd = talloc_asprintf(tctx, "%s %s", userdel_cmd,
+                                         tctx->octx->name);
+    if (!cmd) {
+        ret = ENOMEM;
+        goto done;
+    }
+
+    ret = system(cmd);
+    if (ret) {
+        if (ret == -1) {
+            DEBUG(1, ("system(3) failed\n"));
+            ret = EFAULT;
+        } else {
+            DEBUG(5, ("command [%s] returned nonzero status [%d].\n", cmd,
+                                                        WEXITSTATUS(ret)));
+            ret = EOK;  /* Ignore return code of the command */
+        }
+        goto done;
+    }
+
+    ret = EOK;
+done:
+    talloc_free(userdel_cmd);
+    talloc_free(conf_path);
+    talloc_free(cmd);
+    return ret;
+}
diff --git a/src/tools/tools_util.h b/src/tools/tools_util.h
index b763c83..5c48c5d 100644
--- a/src/tools/tools_util.h
+++ b/src/tools/tools_util.h
@@ -95,6 +95,8 @@ int remove_homedir(TALLOC_CTX *mem_ctx,
                    const char *username,
                    uid_t uid, bool force);
 
+int run_userdel_cmd(struct tools_ctx *tctx);
+
 /* from files.c */
 int remove_tree(const char *root);
 
-- 
1.6.6.1

