>From df8da2627f5ff02889fb148c3aaf57939b4c37d9 Mon Sep 17 00:00:00 2001
From: Pavel Reichl <preichl@redhat.com>
Date: Thu, 23 Oct 2014 16:21:10 +0100
Subject: [PATCH] RESPONDERS: Don't set  umask in utility function

Move setting umask outside of utility function into more visible place.
Also make default umask value symbolic constant.

Resolves:
https://fedorahosted.org/sssd/ticket/2468
---
 src/responder/common/responder.h        |  6 +++++-
 src/responder/common/responder_common.c | 15 +++++++--------
 src/responder/pam/pamsrv.c              |  8 ++++++--
 src/tests/cwrap/test_responder_common.c |  8 ++++++--
 4 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/responder/common/responder.h b/src/responder/common/responder.h
index cd2b3232c53e919c7d47170ccd1016a8604c9742..dd854bb177cc7405c671a1b5cb7937e0e010395a 100644
--- a/src/responder/common/responder.h
+++ b/src/responder/common/responder.h
@@ -38,6 +38,10 @@
 
 extern hash_table_t *dp_requests;
 
+/* we want default permissions on created files to be very strict,
+ * so set our umask to 0177 */
+#define DEF_RSP_UMASK 0177
+
 /* if there is a provider other than the special local */
 #define NEED_CHECK_PROVIDER(provider) \
     (provider != NULL && strcmp(provider, "local") != 0)
@@ -178,7 +182,7 @@ responder_get_domain(struct resp_ctx *rctx, const char *domain);
 errno_t responder_get_domain_by_id(struct resp_ctx *rctx, const char *id,
                                    struct sss_domain_info **_ret_dom);
 
-int create_pipe_fd(const char *sock_name, int *_fd, mode_t umaskval);
+int create_pipe_fd(const char *sock_name, int *_fd);
 
 /* responder_cmd.c */
 int sss_cmd_empty_packet(struct sss_packet *packet);
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 9a13c95d63d93df96ffceb51f969eb562c3a6b08..fc28d3577f533453cc9585097f0edbcead394ed5 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -584,7 +584,7 @@ static int sss_dp_init(struct resp_ctx *rctx,
     return EOK;
 }
 
-int create_pipe_fd(const char *sock_name, int *_fd, mode_t umaskval)
+int create_pipe_fd(const char *sock_name, int *_fd)
 {
     struct sockaddr_un addr;
     errno_t ret;
@@ -595,8 +595,6 @@ int create_pipe_fd(const char *sock_name, int *_fd, mode_t umaskval)
         return EIO;
     }
 
-    umask(umaskval);
-
     ret = set_nonblocking(fd);
     if (ret != EOK) {
         goto done;
@@ -636,9 +634,6 @@ int create_pipe_fd(const char *sock_name, int *_fd, mode_t umaskval)
     ret = EOK;
 
 done:
-    /* we want default permissions on created files to be very strict,
-       so set our umask to 0177 */
-    umask(0177);
     if (ret == EOK) {
         *_fd = fd;
     } else {
@@ -693,7 +688,9 @@ static int set_unix_socket(struct resp_ctx *rctx)
         /* Set the umask so that permissions are set right on the socket.
          * It must be readable and writable by anybody on the system. */
         if (rctx->lfd == -1) {
-            ret = create_pipe_fd(rctx->sock_name, &rctx->lfd, 0111);
+            umask(0111);
+            ret = create_pipe_fd(rctx->sock_name, &rctx->lfd);
+            umask(DEF_RSP_UMASK);
             if (ret != EOK) {
                 return ret;
             }
@@ -716,7 +713,9 @@ static int set_unix_socket(struct resp_ctx *rctx)
     if (rctx->priv_sock_name != NULL ) {
         /* create privileged pipe */
         if (rctx->priv_lfd == -1) {
-            ret = create_pipe_fd(rctx->priv_sock_name, &rctx->priv_lfd, 0177);
+            umask(0177);
+            ret = create_pipe_fd(rctx->priv_sock_name, &rctx->priv_lfd);
+            umask(DEF_RSP_UMASK);
             if (ret != EOK) {
                 goto failed;
             }
diff --git a/src/responder/pam/pamsrv.c b/src/responder/pam/pamsrv.c
index c7e3c20b2731efb9393bc820ab09486c48e0a9ea..a280294f78521787dd3ff3c286e1600e14771a66 100644
--- a/src/responder/pam/pamsrv.c
+++ b/src/responder/pam/pamsrv.c
@@ -352,7 +352,9 @@ int main(int argc, const char *argv[])
 
     /* Crate pipe file descriptors here before privileges are dropped
      * in server_setup() */
-    ret = create_pipe_fd(SSS_PAM_SOCKET_NAME, &pipe_fd, 0111);
+    umask(0111);
+    ret = create_pipe_fd(SSS_PAM_SOCKET_NAME, &pipe_fd);
+    umask(DEF_RSP_UMASK);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE,
               "create_pipe_fd failed [%d]: %s.\n",
@@ -360,7 +362,9 @@ int main(int argc, const char *argv[])
         return 2;
     }
 
-    ret = create_pipe_fd(SSS_PAM_PRIV_SOCKET_NAME, &priv_pipe_fd, 0177);
+    umask(0177);
+    ret = create_pipe_fd(SSS_PAM_PRIV_SOCKET_NAME, &priv_pipe_fd);
+    umask(DEF_RSP_UMASK);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE,
               "create_pipe_fd failed (priviledged pipe) [%d]: %s.\n",
diff --git a/src/tests/cwrap/test_responder_common.c b/src/tests/cwrap/test_responder_common.c
index 7e3f2e025a0dce15cc93e560a42bb566eff9fb30..f74210bfd0239a3aad519f5a9c85156681361477 100644
--- a/src/tests/cwrap/test_responder_common.c
+++ b/src/tests/cwrap/test_responder_common.c
@@ -169,13 +169,17 @@ void test_create_pipe_fd(void **state)
 
     ctx->sock_name = __FUNCTION__;
 
-    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0111);
+    umask(0111);
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd);
+    umask(DEF_RSP_UMASK);
     assert_int_equal(ret, EOK);
     assert_int_not_equal(ctx->fd, -1);
     check_sock_properties(ctx, 0666);
 
     /* Make sure we can overwrite an existing socket */
-    ret = create_pipe_fd(ctx->sock_name, &ctx->fd, 0000);
+    umask(0000);
+    ret = create_pipe_fd(ctx->sock_name, &ctx->fd);
+    umask(DEF_RSP_UMASK);
     assert_int_equal(ret, EOK);
     assert_int_not_equal(ctx->fd, -1);
     check_sock_properties(ctx, 0777);
-- 
1.9.3

