dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0033-responder_common-Create-fd-for-pipe-in-helper.patch

905b4d
From 2d5dbb5dbe674f012fc044f03441538b9b400983 Mon Sep 17 00:00:00 2001
905b4d
From: Michal Zidek <mzidek@redhat.com>
905b4d
Date: Wed, 15 Oct 2014 17:35:12 +0200
905b4d
Subject: [PATCH 33/46] responder_common: Create fd for pipe in helper
905b4d
905b4d
Move creating of file descriptor for pipes into
905b4d
helper function and make this function public.
905b4d
905b4d
Reviewed-by: Pavel Reichl <preichl@redhat.com>
905b4d
Reviewed-by: Simo Sorce <simo@redhat.com>
905b4d
(cherry picked from commit 2ce29e05e62b2702ba4df5f3316eaf250b0ada7f)
905b4d
---
905b4d
 src/responder/common/responder.h        |   2 +
905b4d
 src/responder/common/responder_common.c | 135 +++++++++++++++-----------------
905b4d
 2 files changed, 65 insertions(+), 72 deletions(-)
905b4d
905b4d
diff --git a/src/responder/common/responder.h b/src/responder/common/responder.h
905b4d
index 97552ec472c5baa285b41cc48b51149f3ef6adb5..d233710782fe7df1bbcc338e3815d1701557519e 100644
905b4d
--- a/src/responder/common/responder.h
905b4d
+++ b/src/responder/common/responder.h
905b4d
@@ -176,6 +176,8 @@ responder_get_domain(struct resp_ctx *rctx, const char *domain);
905b4d
 errno_t responder_get_domain_by_id(struct resp_ctx *rctx, const char *id,
905b4d
                                    struct sss_domain_info **_ret_dom);
905b4d
 
905b4d
+int create_pipe_fd(const char *sock_name, int *fd, mode_t umaskval);
905b4d
+
905b4d
 /* responder_cmd.c */
905b4d
 int sss_cmd_empty_packet(struct sss_packet *packet);
905b4d
 int sss_cmd_send_empty(struct cli_ctx *cctx, TALLOC_CTX *freectx);
905b4d
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
905b4d
index 0ec2372e8d08f1002b303b5edc6897f17cee9699..a262a2c1487c786404b6d0d4c720edd1679279d0 100644
905b4d
--- a/src/responder/common/responder_common.c
905b4d
+++ b/src/responder/common/responder_common.c
905b4d
@@ -584,10 +584,69 @@ static int sss_dp_init(struct resp_ctx *rctx,
905b4d
     return EOK;
905b4d
 }
905b4d
 
905b4d
+int create_pipe_fd(const char *sock_name, int *fd, mode_t umaskval)
905b4d
+{
905b4d
+    struct sockaddr_un addr;
905b4d
+    errno_t ret;
905b4d
+
905b4d
+    *fd = socket(AF_UNIX, SOCK_STREAM, 0);
905b4d
+    if (*fd == -1) {
905b4d
+        return EIO;
905b4d
+    }
905b4d
+
905b4d
+    umask(umaskval);
905b4d
+
905b4d
+    ret = set_nonblocking(*fd);
905b4d
+    if (ret != EOK) {
905b4d
+        goto done;
905b4d
+    }
905b4d
+
905b4d
+    ret = set_close_on_exec(*fd);
905b4d
+    if (ret != EOK) {
905b4d
+        goto done;
905b4d
+    }
905b4d
+
905b4d
+    memset(&addr, 0, sizeof(addr));
905b4d
+    addr.sun_family = AF_UNIX;
905b4d
+    strncpy(addr.sun_path, sock_name, sizeof(addr.sun_path) - 1);
905b4d
+    addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
905b4d
+
905b4d
+    /* make sure we have no old sockets around */
905b4d
+    ret = unlink(sock_name);
905b4d
+    if (ret != 0 && errno != ENOENT) {
905b4d
+        ret = errno;
905b4d
+        DEBUG(SSSDBG_MINOR_FAILURE,
905b4d
+              "Cannot remove old socket (errno=%d), bind might fail!\n", ret);
905b4d
+    }
905b4d
+
905b4d
+    if (bind(*fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
905b4d
+        DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
+              "Unable to bind on socket '%s'\n", sock_name);
905b4d
+        ret = EIO;
905b4d
+        goto done;
905b4d
+    }
905b4d
+    if (listen(*fd, 10) != 0) {
905b4d
+        DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
+              "Unable to listen on socket '%s'\n", sock_name);
905b4d
+        ret = EIO;
905b4d
+        goto done;
905b4d
+    }
905b4d
+
905b4d
+    ret = EOK;
905b4d
+
905b4d
+done:
905b4d
+    /* we want default permissions on created files to be very strict,
905b4d
+       so set our umask to 0177 */
905b4d
+    umask(0177);
905b4d
+    if (ret != EOK) {
905b4d
+        close(*fd);
905b4d
+    }
905b4d
+    return ret;
905b4d
+}
905b4d
+
905b4d
 /* create a unix socket and listen to it */
905b4d
 static int set_unix_socket(struct resp_ctx *rctx)
905b4d
 {
905b4d
-    struct sockaddr_un addr;
905b4d
     errno_t ret;
905b4d
     struct accept_fd_ctx *accept_ctx;
905b4d
 
905b4d
@@ -628,42 +687,11 @@ static int set_unix_socket(struct resp_ctx *rctx)
905b4d
 #endif
905b4d
 
905b4d
     if (rctx->sock_name != NULL ) {
905b4d
-        rctx->lfd = socket(AF_UNIX, SOCK_STREAM, 0);
905b4d
-        if (rctx->lfd == -1) {
905b4d
-            return EIO;
905b4d
-        }
905b4d
-
905b4d
         /* Set the umask so that permissions are set right on the socket.
905b4d
          * It must be readable and writable by anybody on the system. */
905b4d
-        umask(0111);
905b4d
-
905b4d
-        ret = set_nonblocking(rctx->lfd);
905b4d
+        ret = create_pipe_fd(rctx->sock_name, &rctx->lfd, 0111);
905b4d
         if (ret != EOK) {
905b4d
-            goto failed;
905b4d
-        }
905b4d
-
905b4d
-        ret = set_close_on_exec(rctx->lfd);
905b4d
-        if (ret != EOK) {
905b4d
-            goto failed;
905b4d
-        }
905b4d
-
905b4d
-        memset(&addr, 0, sizeof(addr));
905b4d
-        addr.sun_family = AF_UNIX;
905b4d
-        strncpy(addr.sun_path, rctx->sock_name, sizeof(addr.sun_path)-1);
905b4d
-        addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
905b4d
-
905b4d
-        /* make sure we have no old sockets around */
905b4d
-        unlink(rctx->sock_name);
905b4d
-
905b4d
-        if (bind(rctx->lfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
905b4d
-            DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
-                  "Unable to bind on socket '%s'\n", rctx->sock_name);
905b4d
-            goto failed;
905b4d
-        }
905b4d
-        if (listen(rctx->lfd, 10) != 0) {
905b4d
-            DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
-                  "Unable to listen on socket '%s'\n", rctx->sock_name);
905b4d
-            goto failed;
905b4d
+            return ret;
905b4d
         }
905b4d
 
905b4d
         accept_ctx = talloc_zero(rctx, struct accept_fd_ctx);
905b4d
@@ -682,42 +710,11 @@ static int set_unix_socket(struct resp_ctx *rctx)
905b4d
 
905b4d
     if (rctx->priv_sock_name != NULL ) {
905b4d
         /* create privileged pipe */
905b4d
-        rctx->priv_lfd = socket(AF_UNIX, SOCK_STREAM, 0);
905b4d
-        if (rctx->priv_lfd == -1) {
905b4d
-            close(rctx->lfd);
905b4d
-            return EIO;
905b4d
-        }
905b4d
-
905b4d
-        umask(0177);
905b4d
-
905b4d
-        ret = set_nonblocking(rctx->priv_lfd);
905b4d
+        ret = create_pipe_fd(rctx->priv_sock_name, &rctx->priv_lfd, 0177);
905b4d
         if (ret != EOK) {
905b4d
             goto failed;
905b4d
         }
905b4d
 
905b4d
-        ret = set_close_on_exec(rctx->priv_lfd);
905b4d
-        if (ret != EOK) {
905b4d
-            goto failed;
905b4d
-        }
905b4d
-
905b4d
-        memset(&addr, 0, sizeof(addr));
905b4d
-        addr.sun_family = AF_UNIX;
905b4d
-        strncpy(addr.sun_path, rctx->priv_sock_name, sizeof(addr.sun_path)-1);
905b4d
-        addr.sun_path[sizeof(addr.sun_path)-1] = '\0';
905b4d
-
905b4d
-        unlink(rctx->priv_sock_name);
905b4d
-
905b4d
-        if (bind(rctx->priv_lfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
905b4d
-            DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
-                  "Unable to bind on socket '%s'\n", rctx->priv_sock_name);
905b4d
-            goto failed;
905b4d
-        }
905b4d
-        if (listen(rctx->priv_lfd, 10) != 0) {
905b4d
-            DEBUG(SSSDBG_FATAL_FAILURE,
905b4d
-                  "Unable to listen on socket '%s'\n", rctx->priv_sock_name);
905b4d
-            goto failed;
905b4d
-        }
905b4d
-
905b4d
         accept_ctx = talloc_zero(rctx, struct accept_fd_ctx);
905b4d
         if(!accept_ctx) goto failed;
905b4d
         accept_ctx->rctx = rctx;
905b4d
@@ -733,15 +730,9 @@ static int set_unix_socket(struct resp_ctx *rctx)
905b4d
         }
905b4d
     }
905b4d
 
905b4d
-    /* we want default permissions on created files to be very strict,
905b4d
-       so set our umask to 0177 */
905b4d
-    umask(0177);
905b4d
     return EOK;
905b4d
 
905b4d
 failed:
905b4d
-    /* we want default permissions on created files to be very strict,
905b4d
-       so set our umask to 0177 */
905b4d
-    umask(0177);
905b4d
     close(rctx->lfd);
905b4d
     close(rctx->priv_lfd);
905b4d
     return EIO;
905b4d
-- 
905b4d
1.9.3
905b4d