From cfaa777313a74b8bf29ee76d657eb73832e458ab Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 5 May 2010 07:35:56 -0400
Subject: [PATCH 2/2] Clean up kdcinfo and kpasswdinfo files when exiting

---
 src/providers/krb5/krb5_common.c |   40 ++++++++++++++++++++++++++++++++++++++
 src/providers/krb5/krb5_common.h |    7 ++++++
 src/providers/krb5/krb5_init.c   |   18 +++++++++++++++++
 src/util/server.c                |    2 +-
 src/util/util.h                  |    1 +
 5 files changed, 67 insertions(+), 1 deletions(-)

diff --git a/src/providers/krb5/krb5_common.c b/src/providers/krb5/krb5_common.c
index 52cbe1650f95095dbf0fd80ed5a70d1a03976b83..26d37c0a961e90dce1ec87e722edc153ace1a667 100644
--- a/src/providers/krb5/krb5_common.c
+++ b/src/providers/krb5/krb5_common.c
@@ -413,3 +413,43 @@ done:
     return ret;
 }
 
+void krb5_finalize(struct tevent_context *ev,
+                   struct tevent_signal *se,
+                   int signum,
+                   int count,
+                   void *siginfo,
+                   void *private_data)
+{
+    char *realm = (char *)private_data;
+    int ret;
+    errno_t err;
+    char *file;
+
+    file = talloc_asprintf(se, KDCINFO_TMPL, realm);
+    if(file == NULL) {
+        sig_term(signum);
+    }
+    errno = 0;
+    ret = unlink(file);
+    if (ret == -1) {
+        err = errno;
+        DEBUG(5, ("Could not remove [%s], [%d][%s]\n", file,
+                  err, strerror(err)));
+    }
+
+    errno = 0;
+    file = talloc_asprintf(se, KPASSWDINFO_TMPL, realm);
+    if(file == NULL) {
+        sig_term(signum);
+    }
+
+    errno = 0;
+    ret = unlink(file);
+    if (ret == -1) {
+        err = errno;
+        DEBUG(5, ("Could not remove [%s], [%d][%s]\n", file,
+                  err, strerror(err)));
+    }
+
+    sig_term(signum);
+}
diff --git a/src/providers/krb5/krb5_common.h b/src/providers/krb5/krb5_common.h
index 0b0da31f2882349a33c839301cd7d519924ab56b..51afa0c654fa1d1864e0ed17e5aeae4cb78ae6dd 100644
--- a/src/providers/krb5/krb5_common.h
+++ b/src/providers/krb5/krb5_common.h
@@ -75,4 +75,11 @@ errno_t write_krb5info_file(const char *realm, const char *kdc,
 int krb5_service_init(TALLOC_CTX *memctx, struct be_ctx *ctx,
                       const char *service_name, const char *servers,
                       const char *realm, struct krb5_service **_service);
+
+void krb5_finalize(struct tevent_context *ev,
+                   struct tevent_signal *se,
+                   int signum,
+                   int count,
+                   void *siginfo,
+                   void *private_data);
 #endif /* __KRB5_COMMON_H__ */
diff --git a/src/providers/krb5/krb5_init.c b/src/providers/krb5/krb5_init.c
index 9cf5bfac3a898cb8c59570a8bf1c223ca7008b07..ac4043f733dbabc738eb83ab2d426cae15c54840 100644
--- a/src/providers/krb5/krb5_init.c
+++ b/src/providers/krb5/krb5_init.c
@@ -48,12 +48,14 @@ int sssm_krb5_auth_init(struct be_ctx *bectx,
 {
     struct krb5_ctx *ctx = NULL;
     int ret;
+    struct tevent_signal *sige;
     unsigned v;
     FILE *debug_filep;
     const char *krb5_servers;
     const char *krb5_kpasswd_servers;
     const char *krb5_realm;
     const char *errstr;
+    char *sig_realm;
     int errval;
     int errpos;
 
@@ -125,6 +127,22 @@ int sssm_krb5_auth_init(struct be_ctx *bectx,
         goto fail;
     }
 
+
+    BlockSignals(false, SIGTERM);
+    sig_realm = talloc_strdup(ctx, krb5_realm);
+    if (sig_realm == NULL) {
+        ret = ENOMEM;
+        goto fail;
+    }
+    sige = tevent_add_signal(bectx->ev, ctx, SIGTERM, SA_SIGINFO,
+                             krb5_finalize, sig_realm);
+    if (sige == NULL) {
+        DEBUG(1, ("tevent_add_signal failed.\n"));
+        ret = ENOMEM;
+        goto fail;
+    }
+    talloc_steal(sige, sig_realm);
+
     if (debug_to_file != 0) {
         ret = open_debug_file_ex("krb5_child", &debug_filep);
         if (ret != EOK) {
diff --git a/src/util/server.c b/src/util/server.c
index a0ec2a265f7fcba8b4adc121ca8c6f9598e21dd9..007b362414cac34412ba29b8bae76bb0df9b7a7c 100644
--- a/src/util/server.c
+++ b/src/util/server.c
@@ -178,7 +178,7 @@ static void sig_hup(int sig)
 	return;
 }
 
-static void sig_term(int sig)
+void sig_term(int sig)
 {
 #if HAVE_GETPGRP
 	static int done_sigterm;
diff --git a/src/util/util.h b/src/util/util.h
index 0b691a7e66e4b0eb22b65428d379f520d0adb0b9..25e7b6c28039723b71d2331298f6299aade69ecf 100644
--- a/src/util/util.h
+++ b/src/util/util.h
@@ -218,6 +218,7 @@ int server_setup(const char *name, int flags,
                  const char *conf_entry,
                  struct main_context **main_ctx);
 void server_loop(struct main_context *main_ctx);
+void sig_term(int sig);
 
 /* from signal.c */
 #include <signal.h>
-- 
1.7.0.1

