nss_wins: Use lp_global_no_reinit()
[metze/samba/wip.git] / nsswitch / wb_common.c
index d0dfcb8bbfb67326d82f2736b8110b3a9baf8688..da1a84476e8b88cf7f9a1896f0b93f714e1629e4 100644 (file)
@@ -6,6 +6,7 @@
    Copyright (C) Tim Potter 2000
    Copyright (C) Andrew Tridgell 2000
    Copyright (C) Andrew Bartlett 2002
+   Copyright (C) Matthew Newton 2015
 
 
    This library is free software; you can redistribute it and/or
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "replace.h"
+#include "system/select.h"
 #include "winbind_client.h"
 
-/* Global variables.  These are effectively the client state information */
+/* Global context */
 
-int winbindd_fd = -1;           /* fd for winbindd socket */
-static int is_privileged = 0;
+struct winbindd_context {
+       int winbindd_fd;        /* winbind file descriptor */
+       bool is_privileged;     /* using the privileged socket? */
+       pid_t our_pid;          /* calling process pid */
+};
+
+static struct winbindd_context wb_global_ctx = {
+       .winbindd_fd = -1,
+       .is_privileged = 0,
+       .our_pid = 0
+};
 
 /* Free a response structure */
 
@@ -41,7 +53,8 @@ void winbindd_free_response(struct winbindd_response *response)
 
 /* Initialise a request structure */
 
-void winbindd_init_request(struct winbindd_request *request, int request_type)
+static void winbindd_init_request(struct winbindd_request *request,
+                                 int request_type)
 {
        request->length = sizeof(struct winbindd_request);
 
@@ -61,14 +74,28 @@ static void init_response(struct winbindd_response *response)
 
 /* Close established socket */
 
-void winbind_close_sock(void)
+static void winbind_close_sock(struct winbindd_context *ctx)
 {
-       if (winbindd_fd != -1) {
-               close(winbindd_fd);
-               winbindd_fd = -1;
+       if (!ctx) {
+               return;
+       }
+
+       if (ctx->winbindd_fd != -1) {
+               close(ctx->winbindd_fd);
+               ctx->winbindd_fd = -1;
        }
 }
 
+/* Destructor for global context to ensure fd is closed */
+
+#if HAVE_DESTRUCTOR_ATTRIBUTE
+__attribute__((destructor))
+#endif
+static void winbind_destructor(void)
+{
+       winbind_close_sock(&wb_global_ctx);
+}
+
 #define CONNECT_TIMEOUT 30
 
 /* Make sure socket handle isn't stdin, stdout or stderr */
@@ -162,16 +189,41 @@ static int make_safe_fd(int fd)
        return new_fd;
 }
 
+/**
+ * @internal
+ *
+ * @brief Check if we talk to the priviliged pipe which should be owned by root.
+ *
+ * This checks if we have uid_wrapper running and if this is the case it will
+ * allow to connect to the winbind privileged pipe even it is not owned by root.
+ *
+ * @param[in]  uid      The uid to check if we can safely talk to the pipe.
+ *
+ * @return              If we have access it returns true, else false.
+ */
+static bool winbind_privileged_pipe_is_root(uid_t uid)
+{
+       if (uid == 0) {
+               return true;
+       }
+
+       if (uid_wrapper_enabled()) {
+               return true;
+       }
+
+       return false;
+}
+
 /* Connect to winbindd socket */
 
 static int winbind_named_pipe_sock(const char *dir)
 {
        struct sockaddr_un sunaddr;
        struct stat st;
-       char *path = NULL;
        int fd;
        int wait_time;
        int slept;
+       int ret;
 
        /* Check permissions on unix socket directory */
 
@@ -180,37 +232,44 @@ static int winbind_named_pipe_sock(const char *dir)
                return -1;
        }
 
+       /*
+        * This tells us that the pipe is owned by a privileged
+        * process, as we will be sending passwords to it.
+        */
        if (!S_ISDIR(st.st_mode) ||
-           (st.st_uid != 0 && st.st_uid != geteuid())) {
+           !winbind_privileged_pipe_is_root(st.st_uid)) {
                errno = ENOENT;
                return -1;
        }
 
        /* Connect to socket */
 
-       if (asprintf(&path, "%s/%s", dir, WINBINDD_SOCKET_NAME) < 0) {
+       sunaddr = (struct sockaddr_un) { .sun_family = AF_UNIX };
+
+       ret = snprintf(sunaddr.sun_path, sizeof(sunaddr.sun_path),
+                      "%s/%s", dir, WINBINDD_SOCKET_NAME);
+       if ((ret == -1) || (ret >= sizeof(sunaddr.sun_path))) {
+               errno = ENAMETOOLONG;
                return -1;
        }
 
-       ZERO_STRUCT(sunaddr);
-       sunaddr.sun_family = AF_UNIX;
-       strncpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path) - 1);
-
        /* If socket file doesn't exist, don't bother trying to connect
           with retry.  This is an attempt to make the system usable when
           the winbindd daemon is not running. */
 
-       if (lstat(path, &st) == -1) {
+       if (lstat(sunaddr.sun_path, &st) == -1) {
                errno = ENOENT;
-               SAFE_FREE(path);
                return -1;
        }
 
-       SAFE_FREE(path);
        /* Check permissions on unix socket file */
 
+       /*
+        * This tells us that the pipe is owned by a privileged
+        * process, as we will be sending passwords to it.
+        */
        if (!S_ISSOCK(st.st_mode) ||
-           (st.st_uid != 0 && st.st_uid != geteuid())) {
+           !winbind_privileged_pipe_is_root(st.st_uid)) {
                errno = ENOENT;
                return -1;
        }
@@ -229,9 +288,7 @@ static int winbind_named_pipe_sock(const char *dir)
 
        for (wait_time = 0; connect(fd, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1;
                        wait_time += slept) {
-               struct timeval tv;
-               fd_set w_fds;
-               int ret;
+               struct pollfd pfd;
                int connect_errno = 0;
                socklen_t errnosize;
 
@@ -240,12 +297,10 @@ static int winbind_named_pipe_sock(const char *dir)
 
                switch (errno) {
                        case EINPROGRESS:
-                               FD_ZERO(&w_fds);
-                               FD_SET(fd, &w_fds);
-                               tv.tv_sec = CONNECT_TIMEOUT - wait_time;
-                               tv.tv_usec = 0;
+                               pfd.fd = fd;
+                               pfd.events = POLLOUT;
 
-                               ret = select(fd + 1, NULL, &w_fds, NULL, &tv);
+                               ret = poll(&pfd, 1, (CONNECT_TIMEOUT - wait_time) * 1000);
 
                                if (ret > 0) {
                                        errnosize = sizeof(connect_errno);
@@ -283,79 +338,97 @@ static int winbind_named_pipe_sock(const char *dir)
 
 static const char *winbindd_socket_dir(void)
 {
-#ifdef SOCKET_WRAPPER
-       const char *env_dir;
+       if (nss_wrapper_enabled()) {
+               const char *env_dir;
 
-       env_dir = getenv(WINBINDD_SOCKET_DIR_ENVVAR);
-       if (env_dir) {
-               return env_dir;
+               env_dir = getenv("SELFTEST_WINBINDD_SOCKET_DIR");
+               if (env_dir != NULL) {
+                       return env_dir;
+               }
        }
-#endif
 
        return WINBINDD_SOCKET_DIR;
 }
 
 /* Connect to winbindd socket */
 
-static int winbind_open_pipe_sock(int recursing, int need_priv)
+static int winbind_open_pipe_sock(struct winbindd_context *ctx,
+                                 int recursing, int need_priv)
 {
 #ifdef HAVE_UNIXSOCKET
-       static pid_t our_pid;
        struct winbindd_request request;
        struct winbindd_response response;
+
        ZERO_STRUCT(request);
        ZERO_STRUCT(response);
 
-       if (our_pid != getpid()) {
-               winbind_close_sock();
-               our_pid = getpid();
+       if (!ctx) {
+               return -1;
+       }
+
+       if (ctx->our_pid != getpid()) {
+               winbind_close_sock(ctx);
+               ctx->our_pid = getpid();
        }
 
-       if ((need_priv != 0) && (is_privileged == 0)) {
-               winbind_close_sock();
+       if ((need_priv != 0) && (ctx->is_privileged == 0)) {
+               winbind_close_sock(ctx);
        }
 
-       if (winbindd_fd != -1) {
-               return winbindd_fd;
+       if (ctx->winbindd_fd != -1) {
+               return ctx->winbindd_fd;
        }
 
        if (recursing) {
                return -1;
        }
 
-       if ((winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir())) == -1) {
+       ctx->winbindd_fd = winbind_named_pipe_sock(winbindd_socket_dir());
+
+       if (ctx->winbindd_fd == -1) {
                return -1;
        }
 
-       is_privileged = 0;
+       ctx->is_privileged = 0;
 
        /* version-check the socket */
 
        request.wb_flags = WBFLAG_RECURSE;
-       if ((winbindd_request_response(WINBINDD_INTERFACE_VERSION, &request, &response) != NSS_STATUS_SUCCESS) || (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
-               winbind_close_sock();
+       if ((winbindd_request_response(ctx, WINBINDD_INTERFACE_VERSION, &request,
+                                      &response) != NSS_STATUS_SUCCESS) ||
+           (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
+               winbind_close_sock(ctx);
                return -1;
        }
 
        /* try and get priv pipe */
 
        request.wb_flags = WBFLAG_RECURSE;
-       if (winbindd_request_response(WINBINDD_PRIV_PIPE_DIR, &request, &response) == NSS_STATUS_SUCCESS) {
+
+       /* Note that response needs to be initialized to avoid
+        * crashing on clean up after WINBINDD_PRIV_PIPE_DIR call failed
+        * as interface version (from the first request) returned as a fstring,
+        * thus response.extra_data.data will not be NULL even though
+        * winbindd response did not write over it due to a failure */
+       ZERO_STRUCT(response);
+       if (winbindd_request_response(ctx, WINBINDD_PRIV_PIPE_DIR, &request,
+                                     &response) == NSS_STATUS_SUCCESS) {
                int fd;
-               if ((fd = winbind_named_pipe_sock((char *)response.extra_data.data)) != -1) {
-                       close(winbindd_fd);
-                       winbindd_fd = fd;
-                       is_privileged = 1;
+               fd = winbind_named_pipe_sock((char *)response.extra_data.data);
+               if (fd != -1) {
+                       close(ctx->winbindd_fd);
+                       ctx->winbindd_fd = fd;
+                       ctx->is_privileged = 1;
                }
        }
 
-       if ((need_priv != 0) && (is_privileged == 0)) {
+       if ((need_priv != 0) && (ctx->is_privileged == 0)) {
                return -1;
        }
 
        SAFE_FREE(response.extra_data.data);
 
-       return winbindd_fd;
+       return ctx->winbindd_fd;
 #else
        return -1;
 #endif /* HAVE_UNIXSOCKET */
@@ -363,15 +436,17 @@ static int winbind_open_pipe_sock(int recursing, int need_priv)
 
 /* Write data to winbindd socket */
 
-int winbind_write_sock(void *buffer, int count, int recursing, int need_priv)
+static int winbind_write_sock(struct winbindd_context *ctx, void *buffer,
+                             int count, int recursing, int need_priv)
 {
-       int result, nwritten;
+       int fd, result, nwritten;
 
        /* Open connection to winbind daemon */
 
  restart:
 
-       if (winbind_open_pipe_sock(recursing, need_priv) == -1) {
+       fd = winbind_open_pipe_sock(ctx, recursing, need_priv);
+       if (fd == -1) {
                errno = ENOENT;
                return -1;
        }
@@ -381,48 +456,45 @@ int winbind_write_sock(void *buffer, int count, int recursing, int need_priv)
        nwritten = 0;
 
        while(nwritten < count) {
-               struct timeval tv;
-               fd_set r_fds;
+               struct pollfd pfd;
+               int ret;
 
                /* Catch pipe close on other end by checking if a read()
-                  call would not block by calling select(). */
+                  call would not block by calling poll(). */
 
-               FD_ZERO(&r_fds);
-               FD_SET(winbindd_fd, &r_fds);
-               ZERO_STRUCT(tv);
+               pfd.fd = fd;
+               pfd.events = POLLIN|POLLOUT|POLLHUP;
 
-               if (select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv) == -1) {
-                       winbind_close_sock();
-                       return -1;                   /* Select error */
+               ret = poll(&pfd, 1, -1);
+               if (ret == -1) {
+                       winbind_close_sock(ctx);
+                       return -1;                   /* poll error */
                }
 
                /* Write should be OK if fd not available for reading */
 
-               if (!FD_ISSET(winbindd_fd, &r_fds)) {
-
-                       /* Do the write */
+               if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
 
-                       result = write(winbindd_fd,
-                                      (char *)buffer + nwritten,
-                                      count - nwritten);
-
-                       if ((result == -1) || (result == 0)) {
+                       /* Pipe has closed on remote end */
 
-                               /* Write failed */
+                       winbind_close_sock(ctx);
+                       goto restart;
+               }
 
-                               winbind_close_sock();
-                               return -1;
-                       }
+               /* Do the write */
 
-                       nwritten += result;
+               result = write(fd, (char *)buffer + nwritten,
+                              count - nwritten);
 
-               } else {
+               if ((result == -1) || (result == 0)) {
 
-                       /* Pipe has closed on remote end */
+                       /* Write failed */
 
-                       winbind_close_sock();
-                       goto restart;
+                       winbind_close_sock(ctx);
+                       return -1;
                }
+
+               nwritten += result;
        }
 
        return nwritten;
@@ -430,50 +502,53 @@ int winbind_write_sock(void *buffer, int count, int recursing, int need_priv)
 
 /* Read data from winbindd socket */
 
-int winbind_read_sock(void *buffer, int count)
+static int winbind_read_sock(struct winbindd_context *ctx,
+                            void *buffer, int count)
 {
+       int fd;
        int nread = 0;
-       int total_time = 0, selret;
+       int total_time = 0;
 
-       if (winbindd_fd == -1) {
+       fd = winbind_open_pipe_sock(ctx, false, false);
+       if (fd == -1) {
                return -1;
        }
 
        /* Read data from socket */
        while(nread < count) {
-               struct timeval tv;
-               fd_set r_fds;
+               struct pollfd pfd;
+               int ret;
 
                /* Catch pipe close on other end by checking if a read()
-                  call would not block by calling select(). */
+                  call would not block by calling poll(). */
+
+               pfd.fd = fd;
+               pfd.events = POLLIN|POLLHUP;
 
-               FD_ZERO(&r_fds);
-               FD_SET(winbindd_fd, &r_fds);
-               ZERO_STRUCT(tv);
                /* Wait for 5 seconds for a reply. May need to parameterise this... */
-               tv.tv_sec = 5;
 
-               if ((selret = select(winbindd_fd + 1, &r_fds, NULL, NULL, &tv)) == -1) {
-                       winbind_close_sock();
-                       return -1;                   /* Select error */
+               ret = poll(&pfd, 1, 5000);
+               if (ret == -1) {
+                       winbind_close_sock(ctx);
+                       return -1;                   /* poll error */
                }
 
-               if (selret == 0) {
+               if (ret == 0) {
                        /* Not ready for read yet... */
-                       if (total_time >= 30) {
+                       if (total_time >= 300) {
                                /* Timeout */
-                               winbind_close_sock();
+                               winbind_close_sock(ctx);
                                return -1;
                        }
                        total_time += 5;
                        continue;
                }
 
-               if (FD_ISSET(winbindd_fd, &r_fds)) {
+               if ((ret == 1) && (pfd.revents & (POLLIN|POLLHUP|POLLERR))) {
 
                        /* Do the Read */
 
-                       int result = read(winbindd_fd, (char *)buffer + nread,
+                       int result = read(fd, (char *)buffer + nread,
                              count - nread);
 
                        if ((result == -1) || (result == 0)) {
@@ -482,7 +557,7 @@ int winbind_read_sock(void *buffer, int count)
                                   can do here is just return -1 and fail since the
                                   transaction has failed half way through. */
 
-                               winbind_close_sock();
+                               winbind_close_sock(ctx);
                                return -1;
                        }
 
@@ -496,7 +571,8 @@ int winbind_read_sock(void *buffer, int count)
 
 /* Read reply */
 
-int winbindd_read_reply(struct winbindd_response *response)
+static int winbindd_read_reply(struct winbindd_context *ctx,
+                              struct winbindd_response *response)
 {
        int result1, result2 = 0;
 
@@ -506,11 +582,8 @@ int winbindd_read_reply(struct winbindd_response *response)
 
        /* Read fixed length response */
 
-       result1 = winbind_read_sock(response,
+       result1 = winbind_read_sock(ctx, response,
                                    sizeof(struct winbindd_response));
-       if (result1 == -1) {
-               return -1;
-       }
 
        /* We actually send the pointer value of the extra_data field from
           the server.  This has no meaning in the client's address space
@@ -518,6 +591,14 @@ int winbindd_read_reply(struct winbindd_response *response)
 
        response->extra_data.data = NULL;
 
+       if (result1 == -1) {
+               return -1;
+       }
+
+       if (response->length < sizeof(struct winbindd_response)) {
+               return -1;
+       }
+
        /* Read variable length response */
 
        if (response->length > sizeof(struct winbindd_response)) {
@@ -530,7 +611,7 @@ int winbindd_read_reply(struct winbindd_response *response)
                        return -1;
                }
 
-               result2 = winbind_read_sock(response->extra_data.data,
+               result2 = winbind_read_sock(ctx, response->extra_data.data,
                                            extra_data_len);
                if (result2 == -1) {
                        winbindd_free_response(response);
@@ -547,7 +628,8 @@ int winbindd_read_reply(struct winbindd_response *response)
  * send simple types of requests
  */
 
-NSS_STATUS winbindd_send_request(int req_type, int need_priv,
+NSS_STATUS winbindd_send_request(struct winbindd_context *ctx,
+                                int req_type, int need_priv,
                                 struct winbindd_request *request)
 {
        struct winbindd_request lrequest;
@@ -567,7 +649,7 @@ NSS_STATUS winbindd_send_request(int req_type, int need_priv,
 
        winbindd_init_request(request, req_type);
 
-       if (winbind_write_sock(request, sizeof(*request),
+       if (winbind_write_sock(ctx, request, sizeof(*request),
                               request->wb_flags & WBFLAG_RECURSE,
                               need_priv) == -1)
        {
@@ -578,7 +660,7 @@ NSS_STATUS winbindd_send_request(int req_type, int need_priv,
        }
 
        if ((request->extra_len != 0) &&
-           (winbind_write_sock(request->extra_data.data,
+           (winbind_write_sock(ctx, request->extra_data.data,
                                request->extra_len,
                                request->wb_flags & WBFLAG_RECURSE,
                                need_priv) == -1))
@@ -596,7 +678,8 @@ NSS_STATUS winbindd_send_request(int req_type, int need_priv,
  * Get results from winbindd request
  */
 
-NSS_STATUS winbindd_get_response(struct winbindd_response *response)
+NSS_STATUS winbindd_get_response(struct winbindd_context *ctx,
+                                struct winbindd_response *response)
 {
        struct winbindd_response lresponse;
 
@@ -608,7 +691,7 @@ NSS_STATUS winbindd_get_response(struct winbindd_response *response)
        init_response(response);
 
        /* Wait for reply */
-       if (winbindd_read_reply(response) == -1) {
+       if (winbindd_read_reply(ctx, response) == -1) {
                /* Set ENOENT for consistency.  Required by some apps */
                errno = ENOENT;
 
@@ -630,61 +713,65 @@ NSS_STATUS winbindd_get_response(struct winbindd_response *response)
 
 /* Handle simple types of requests */
 
-NSS_STATUS winbindd_request_response(int req_type,
-                           struct winbindd_request *request,
-                           struct winbindd_response *response)
+NSS_STATUS winbindd_request_response(struct winbindd_context *ctx,
+                                    int req_type,
+                                    struct winbindd_request *request,
+                                    struct winbindd_response *response)
 {
        NSS_STATUS status = NSS_STATUS_UNAVAIL;
-       int count = 0;
+       struct winbindd_context *wb_ctx = ctx;
 
-       while ((status == NSS_STATUS_UNAVAIL) && (count < 10)) {
-               status = winbindd_send_request(req_type, 0, request);
-               if (status != NSS_STATUS_SUCCESS)
-                       return(status);
-               status = winbindd_get_response(response);
-               count += 1;
+       if (ctx == NULL) {
+               wb_ctx = &wb_global_ctx;
        }
 
+       status = winbindd_send_request(wb_ctx, req_type, 0, request);
+       if (status != NSS_STATUS_SUCCESS)
+               return (status);
+       status = winbindd_get_response(wb_ctx, response);
+
        return status;
 }
 
-NSS_STATUS winbindd_priv_request_response(int req_type,
+NSS_STATUS winbindd_priv_request_response(struct winbindd_context *ctx,
+                                         int req_type,
                                          struct winbindd_request *request,
                                          struct winbindd_response *response)
 {
        NSS_STATUS status = NSS_STATUS_UNAVAIL;
-       int count = 0;
+       struct winbindd_context *wb_ctx = ctx;
 
-       while ((status == NSS_STATUS_UNAVAIL) && (count < 10)) {
-               status = winbindd_send_request(req_type, 1, request);
-               if (status != NSS_STATUS_SUCCESS)
-                       return(status);
-               status = winbindd_get_response(response);
-               count += 1;
+       if (ctx == NULL) {
+               wb_ctx = &wb_global_ctx;
        }
 
+       status = winbindd_send_request(wb_ctx, req_type, 1, request);
+       if (status != NSS_STATUS_SUCCESS)
+               return (status);
+       status = winbindd_get_response(wb_ctx, response);
+
        return status;
 }
 
-/*************************************************************************
- ************************************************************************/
+/* Create and free winbindd context */
 
-const char *nss_err_str(NSS_STATUS ret)
+struct winbindd_context *winbindd_ctx_create(void)
 {
-       switch (ret) {
-               case NSS_STATUS_TRYAGAIN:
-                       return "NSS_STATUS_TRYAGAIN";
-               case NSS_STATUS_SUCCESS:
-                       return "NSS_STATUS_SUCCESS";
-               case NSS_STATUS_NOTFOUND:
-                       return "NSS_STATUS_NOTFOUND";
-               case NSS_STATUS_UNAVAIL:
-                       return "NSS_STATUS_UNAVAIL";
-#ifdef NSS_STATUS_RETURN
-               case NSS_STATUS_RETURN:
-                       return "NSS_STATUS_RETURN";
-#endif
-               default:
-                       return "UNKNOWN RETURN CODE!!!!!!!";
+       struct winbindd_context *ctx;
+
+       ctx = calloc(1, sizeof(struct winbindd_context));
+
+       if (!ctx) {
+               return NULL;
        }
+
+       ctx->winbindd_fd = -1;
+
+       return ctx;
+}
+
+void winbindd_ctx_free(struct winbindd_context *ctx)
+{
+       winbind_close_sock(ctx);
+       free(ctx);
 }