Remove DEBUG statement from wb_common.c as it should not be there.
[samba.git] / source3 / nsswitch / wb_common.c
index e0cae0abe3efc7a1d3e4af17f4c9e5085a07891b..f146391653a19e07cf6a70f5a8bd76f267de7ce5 100644 (file)
@@ -24,8 +24,7 @@
    Boston, MA  02111-1307, USA.   
 */
 
-#include "winbind_nss_config.h"
-#include "winbindd_nss.h"
+#include "winbind_client.h"
 
 /* Global variables.  These are effectively the client state information */
 
@@ -82,7 +81,7 @@ static int make_nonstd_fd_internals(int fd, int limit /* Recursion limiter */)
                if ((new_fd = fcntl(fd, F_DUPFD, 3)) == -1) {
                        return -1;
                }
-               /* Parinoia */
+               /* Paranoia */
                if (new_fd < 3) {
                        close(new_fd);
                        return -1;
@@ -132,27 +131,16 @@ static int make_safe_fd(int fd)
 
 /* Connect to winbindd socket */
 
-int winbind_open_pipe_sock(void)
+static int winbind_named_pipe_sock(const char *dir)
 {
-#ifdef HAVE_UNIXSOCKET
        struct sockaddr_un sunaddr;
-       static pid_t our_pid;
        struct stat st;
        pstring path;
        int fd;
        
-       if (our_pid != getpid()) {
-               close_sock();
-               our_pid = getpid();
-       }
-       
-       if (winbindd_fd != -1) {
-               return winbindd_fd;
-       }
-       
        /* Check permissions on unix socket directory */
        
-       if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
+       if (lstat(dir, &st) == -1) {
                return -1;
        }
        
@@ -163,13 +151,13 @@ int winbind_open_pipe_sock(void)
        
        /* Connect to socket */
        
-       strncpy(path, WINBINDD_SOCKET_DIR, sizeof(path) - 1);
+       strncpy(path, dir, sizeof(path) - 1);
        path[sizeof(path) - 1] = '\0';
        
-       strncat(path, "/", sizeof(path) - 1);
+       strncat(path, "/", sizeof(path) - 1 - strlen(path));
        path[sizeof(path) - 1] = '\0';
        
-       strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1);
+       strncat(path, WINBINDD_SOCKET_NAME, sizeof(path) - 1 - strlen(path));
        path[sizeof(path) - 1] = '\0';
        
        ZERO_STRUCT(sunaddr);
@@ -197,16 +185,62 @@ int winbind_open_pipe_sock(void)
                return -1;
        }
 
-       if ((winbindd_fd = make_safe_fd( fd)) == -1) {
-               return winbindd_fd;
+       if ((fd = make_safe_fd( fd)) == -1) {
+               return fd;
        }
        
-       if (connect(winbindd_fd, (struct sockaddr *)&sunaddr, 
+       if (connect(fd, (struct sockaddr *)&sunaddr, 
                    sizeof(sunaddr)) == -1) {
-               close_sock();
+               close(fd);
                return -1;
        }
         
+       return fd;
+}
+
+/* Connect to winbindd socket */
+
+int winbind_open_pipe_sock(void)
+{
+#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()) {
+               close_sock();
+               our_pid = getpid();
+       }
+       
+       if (winbindd_fd != -1) {
+               return winbindd_fd;
+       }
+
+       if ((winbindd_fd = winbind_named_pipe_sock(WINBINDD_SOCKET_DIR)) == -1) {
+               return -1;
+       }
+
+       /* version-check the socket */
+
+       if ((winbindd_request(WINBINDD_INTERFACE_VERSION, &request, &response) != NSS_STATUS_SUCCESS) || (response.data.interface_version != WINBIND_INTERFACE_VERSION)) {
+               close_sock();
+               return -1;
+       }
+
+       /* try and get priv pipe */
+
+       if (winbindd_request(WINBINDD_PRIV_PIPE_DIR, &request, &response) == NSS_STATUS_SUCCESS) {
+               int fd;
+               if ((fd = winbind_named_pipe_sock(response.extra_data)) != -1) {
+                       close(winbindd_fd);
+                       winbindd_fd = fd;
+               }
+       }
+
+       SAFE_FREE(response.extra_data);
+
        return winbindd_fd;
 #else
        return -1;
@@ -363,11 +397,15 @@ int read_reply(struct winbindd_response *response)
 NSS_STATUS winbindd_send_request(int req_type, struct winbindd_request *request)
 {
        struct winbindd_request lrequest;
-
+       char *env;
+       int  value;
+       
        /* Check for our tricky environment variable */
 
-       if (getenv(WINBINDD_DONT_ENV)) {
-               return NSS_STATUS_NOTFOUND;
+       if ( (env = getenv(WINBINDD_DONT_ENV)) != NULL ) {
+               value = atoi(env);
+               if ( value == 1 )
+                       return NSS_STATUS_NOTFOUND;
        }
 
        if (!request) {
@@ -432,3 +470,19 @@ NSS_STATUS winbindd_request(int req_type,
                return(status);
        return winbindd_get_response(response);
 }
+
+/*************************************************************************
+ A couple of simple jfunctions to disable winbindd lookups and re-
+ enable them
+ ************************************************************************/
+BOOL winbind_off( void )
+{
+       return (setenv( WINBINDD_DONT_ENV, "1", 1 ) != -1); 
+}
+
+BOOL winbind_on( void )
+{
+       return (setenv( WINBINDD_DONT_ENV, "0", 1 ) != -1); 
+}
+