Fix some type-punned warnings
[ira/wip.git] / source3 / winbindd / winbindd.c
index 5720bfc5178c995f25d48a98e4164b1eb55edf77..bcfe52891a5edebe9b25f157877740efa13ec992 100644 (file)
@@ -28,7 +28,7 @@
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-bool opt_nocache = False;
+static bool opt_nocache = False;
 static bool interactive = False;
 
 extern bool override_logfile;
@@ -320,7 +320,7 @@ static bool winbindd_setup_sig_usr2_handler(void)
 
        se = tevent_add_signal(winbind_event_context(),
                               winbind_event_context(),
-                              SIGCHLD, 0,
+                              SIGUSR2, 0,
                               winbindd_sig_usr2_handler,
                               NULL);
        if (!se) {
@@ -386,7 +386,7 @@ static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
        if (child_pid != 0) {
                /* parent */
                DEBUG(5, ("winbind_msg_validate_cache: child created with "
-                         "pid %d.\n", child_pid));
+                         "pid %d.\n", (int)child_pid));
                return;
        }
 
@@ -420,6 +420,7 @@ static struct winbindd_dispatch_table {
 
        { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
        { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
+       { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
 
        { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
        { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
@@ -429,6 +430,8 @@ static struct winbindd_dispatch_table {
        { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
        { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
          "GETUSERDOMGROUPS" },
+       { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
+          "LOOKUPUSERALIASES" },
 
        /* Group functions */
 
@@ -738,14 +741,16 @@ static void request_len_recv(void *private_data, bool success)
                return;
        }
 
-       if (*(uint32 *)(&state->request) != sizeof(state->request)) {
+       if (*(uint32 *)(void *)(&state->request) != sizeof(state->request)) {
                DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
-                        *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
+                        *(uint32_t *)(void *)(&state->request),
+                        (uint32_t)sizeof(state->request)));
                state->finished = True;
                return;
        }
 
-       setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
+       setup_async_read(&state->fd_event,
+                        (uint32 *)(void *)(&state->request)+1,
                         sizeof(state->request) - sizeof(uint32),
                         request_main_recv, state);
 }
@@ -818,7 +823,8 @@ static void new_connection(int listen_sock, bool privileged)
        len = sizeof(sunaddr);
 
        do {
-               sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
+               sock = accept(listen_sock, (struct sockaddr *)(void *)&sunaddr,
+                             &len);
        } while (sock == -1 && errno == EINTR);
 
        if (sock == -1)
@@ -924,6 +930,95 @@ static bool remove_idle_client(void)
        return False;
 }
 
+struct winbindd_listen_state {
+       bool privileged;
+       int fd;
+       struct tevent_fd *fde;
+};
+
+static void winbindd_listen_fde_handler(struct tevent_context *ev,
+                                       struct tevent_fd *fde,
+                                       uint16_t flags,
+                                       void *private_data)
+{
+       struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
+                                         struct winbindd_listen_state);
+
+       while (winbindd_num_clients() >
+              WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
+               DEBUG(5,("winbindd: Exceeding %d client "
+                        "connections, removing idle "
+                        "connection.\n",
+                        WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
+               if (!remove_idle_client()) {
+                       DEBUG(0,("winbindd: Exceeding %d "
+                                "client connections, no idle "
+                                "connection found\n",
+                                WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
+                       break;
+               }
+       }
+       new_connection(s->fd, s->privileged);
+}
+
+static bool winbindd_setup_listeners(void)
+{
+       struct winbindd_listen_state *pub_state = NULL;
+       struct winbindd_listen_state *priv_state = NULL;
+
+       pub_state = talloc(winbind_event_context(),
+                          struct winbindd_listen_state);
+       if (!pub_state) {
+               goto failed;
+       }
+
+       pub_state->privileged = false;
+       pub_state->fd = open_winbindd_socket();
+       if (pub_state->fd == -1) {
+               goto failed;
+       }
+
+       pub_state->fde = tevent_add_fd(winbind_event_context(),
+                                      pub_state, pub_state->fd,
+                                      TEVENT_FD_READ,
+                                      winbindd_listen_fde_handler,
+                                      pub_state);
+       if (!pub_state->fde) {
+               close(pub_state->fd);
+               goto failed;
+       }
+       tevent_fd_set_auto_close(pub_state->fde);
+
+       priv_state = talloc(winbind_event_context(),
+                           struct winbindd_listen_state);
+       if (!priv_state) {
+               goto failed;
+       }
+
+       priv_state->privileged = true;
+       priv_state->fd = open_winbindd_priv_socket();
+       if (priv_state->fd == -1) {
+               goto failed;
+       }
+
+       priv_state->fde = tevent_add_fd(winbind_event_context(),
+                                       priv_state, priv_state->fd,
+                                       TEVENT_FD_READ,
+                                       winbindd_listen_fde_handler,
+                                       priv_state);
+       if (!priv_state->fde) {
+               close(priv_state->fd);
+               goto failed;
+       }
+       tevent_fd_set_auto_close(priv_state->fde);
+
+       return true;
+failed:
+       TALLOC_FREE(pub_state);
+       TALLOC_FREE(priv_state);
+       return false;
+}
+
 /* Process incoming clients on listen_sock.  We use a tricky non-blocking,
    non-forking, non-threaded model which allows us to handle many
    simultaneous connections while remaining impervious to many denial of
@@ -931,35 +1026,17 @@ static bool remove_idle_client(void)
 
 static void process_loop(void)
 {
-       struct winbindd_cli_state *state;
        struct winbindd_fd_event *ev;
        fd_set r_fds, w_fds;
-       int maxfd, listen_sock, listen_priv_sock, selret;
+       int maxfd = 0, selret;
        struct timeval timeout, ev_timeout;
 
-       /* Open Sockets here to get stuff going ASAP */
-       listen_sock = open_winbindd_socket();
-       listen_priv_sock = open_winbindd_priv_socket();
-
-       if (listen_sock == -1 || listen_priv_sock == -1) {
-               perror("open_winbind_socket");
-               exit(1);
-       }
-
        run_events(winbind_event_context(), 0, NULL, NULL);
 
-       /* refresh the trusted domain cache */
-
-       rescan_trusted_domains();
-
        /* Initialise fd lists for select() */
 
-       maxfd = MAX(listen_sock, listen_priv_sock);
-
        FD_ZERO(&r_fds);
        FD_ZERO(&w_fds);
-       FD_SET(listen_sock, &r_fds);
-       FD_SET(listen_priv_sock, &r_fds);
 
        timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
        timeout.tv_usec = 0;
@@ -976,23 +1053,6 @@ static void process_loop(void)
                timeout = timeval_min(&timeout, &ev_timeout);
        }
 
-       /* Set up client readers and writers */
-
-       state = winbindd_client_list();
-
-       while (state) {
-
-               struct winbindd_cli_state *next = state->next;
-
-               /* Dispose of client connection if it is marked as 
-                  finished */ 
-
-               if (state->finished)
-                       remove_client(state);
-
-               state = next;
-       }
-
        for (ev = fd_events; ev; ev = ev->next) {
                if (ev->flags & EVENT_FD_READ) {
                        FD_SET(ev->fd, &r_fds);
@@ -1040,43 +1100,7 @@ static void process_loop(void)
                ev = next;
        }
 
-       if (FD_ISSET(listen_sock, &r_fds)) {
-               while (winbindd_num_clients() >
-                      WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
-                       DEBUG(5,("winbindd: Exceeding %d client "
-                                "connections, removing idle "
-                                "connection.\n",
-                                WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
-                       if (!remove_idle_client()) {
-                               DEBUG(0,("winbindd: Exceeding %d "
-                                        "client connections, no idle "
-                                        "connection found\n",
-                                        WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
-                               break;
-                       }
-               }
-               /* new, non-privileged connection */
-               new_connection(listen_sock, False);
-       }
-
-       if (FD_ISSET(listen_priv_sock, &r_fds)) {
-               while (winbindd_num_clients() >
-                      WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
-                       DEBUG(5,("winbindd: Exceeding %d client "
-                                "connections, removing idle "
-                                "connection.\n",
-                                WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
-                       if (!remove_idle_client()) {
-                               DEBUG(0,("winbindd: Exceeding %d "
-                                        "client connections, no idle "
-                                        "connection found\n",
-                                        WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
-                               break;
-                       }
-               }
-               /* new, privileged connection */
-               new_connection(listen_priv_sock, True);
-       }
+       return;
 
  no_fds_ready:
 
@@ -1087,6 +1111,16 @@ static void process_loop(void)
 #endif
 }
 
+bool winbindd_use_idmap_cache(void)
+{
+       return !opt_nocache;
+}
+
+bool winbindd_use_cache(void)
+{
+       return !opt_nocache;
+}
+
 /* Main function */
 
 int main(int argc, char **argv, char **envp)
@@ -1353,12 +1387,39 @@ int main(int argc, char **argv, char **envp)
        smb_nscd_flush_user_cache();
        smb_nscd_flush_group_cache();
 
-       /* Loop waiting for requests */
+       /* setup listen sockets */
+
+       if (!winbindd_setup_listeners()) {
+               DEBUG(0,("winbindd_setup_listeners() failed\n"));
+               exit(1);
+       }
 
        TALLOC_FREE(frame);
+       /* Loop waiting for requests */
        while (1) {
+               struct winbindd_cli_state *state;
+
                frame = talloc_stackframe();
+
+               /* refresh the trusted domain cache */
+
+               rescan_trusted_domains();
+
+               /* Dispose of client connection if it is marked as
+                  finished */
+               state = winbindd_client_list();
+               while (state) {
+                       struct winbindd_cli_state *next = state->next;
+
+                       if (state->finished) {
+                               remove_client(state);
+                       }
+
+                       state = next;
+               }
+
                process_loop();
+
                TALLOC_FREE(frame);
        }