s3: Use talloc_tos() in yield_connection()
[ira/wip.git] / source3 / winbindd / winbindd.c
index e583dae4a9b2b8ec199cd24dba85931e77a2e952..e4df27b45e6f4bd4a7f7098c00acb324a4ee7579 100644 (file)
@@ -51,7 +51,7 @@ struct messaging_context *winbind_messaging_context(void)
        static struct messaging_context *ctx;
 
        if (ctx == NULL) {
-               ctx = messaging_init(NULL, server_id_self(),
+               ctx = messaging_init(NULL, procid_self(),
                                     winbind_event_context());
        }
        if (ctx == NULL) {
@@ -141,6 +141,29 @@ static void flush_caches(void)
        }
 }
 
+static void flush_caches_noinit(void)
+{
+       /*
+        * We need to invalidate cached user list entries on a SIGHUP
+         * otherwise cached access denied errors due to restrict anonymous
+         * hang around until the sequence number changes.
+        * NB
+        * Skip uninitialized domains when flush cache.
+        * If domain is not initialized, it means it is never
+        * used or never become online. look, wcache_invalidate_cache()
+        * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
+        * for unused domains and large traffic for primay domain's DC if there
+        * are many domains..
+        */
+
+       if (!wcache_invalidate_cache_noinit()) {
+               DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
+               if (!winbindd_cache_validate_and_initialize()) {
+                       exit(1);
+               }
+       }
+}
+
 /* Handle the signal by unlinking socket and exiting */
 
 static void terminate(bool is_parent)
@@ -254,7 +277,7 @@ static void winbindd_sig_hup_handler(struct tevent_context *ev,
        const char *file = (const char *)private_data;
 
        DEBUG(1,("Reloading services after SIGHUP\n"));
-       flush_caches();
+       flush_caches_noinit();
        reload_services_file(file);
 }
 
@@ -438,17 +461,8 @@ static struct winbindd_dispatch_table {
        { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
          "LIST_TRUSTDOM" },
 
-       /* Lookup related functions */
-
-       { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
-       { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
-       { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
-       { WINBINDD_REMOVE_MAPPING, winbindd_remove_mapping, "REMOVE_MAPPING" },
-       { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
-
        /* Miscellaneous */
 
-       { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
        { WINBINDD_INFO, winbindd_info, "INFO" },
        { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
          "INTERFACE_VERSION" },
@@ -460,6 +474,7 @@ static struct winbindd_dispatch_table {
 
        /* Credential cache access */
        { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
+       { WINBINDD_CCACHE_SAVE, winbindd_ccache_save, "CCACHE_SAVE" },
 
        /* WINS functions */
 
@@ -539,6 +554,27 @@ static struct winbindd_async_dispatch_table async_nonpriv_table[] = {
          winbindd_list_users_send, winbindd_list_users_recv },
        { WINBINDD_LIST_GROUPS, "LIST_GROUPS",
          winbindd_list_groups_send, winbindd_list_groups_recv },
+       { WINBINDD_CHECK_MACHACC, "CHECK_MACHACC",
+         winbindd_check_machine_acct_send, winbindd_check_machine_acct_recv },
+       { WINBINDD_PING_DC, "PING_DC",
+         winbindd_ping_dc_send, winbindd_ping_dc_recv },
+
+       { 0, NULL, NULL, NULL }
+};
+
+static struct winbindd_async_dispatch_table async_priv_table[] = {
+       { WINBINDD_ALLOCATE_UID, "ALLOCATE_UID",
+         winbindd_allocate_uid_send, winbindd_allocate_uid_recv },
+       { WINBINDD_ALLOCATE_GID, "ALLOCATE_GID",
+         winbindd_allocate_gid_send, winbindd_allocate_gid_recv },
+       { WINBINDD_SET_MAPPING, "SET_MAPPING",
+         winbindd_set_mapping_send, winbindd_set_mapping_recv },
+       { WINBINDD_REMOVE_MAPPING, "SET_MAPPING",
+         winbindd_remove_mapping_send, winbindd_remove_mapping_recv },
+       { WINBINDD_SET_HWM, "SET_HWM",
+         winbindd_set_hwm_send, winbindd_set_hwm_recv },
+       { WINBINDD_CHANGE_MACHACC, "CHANGE_MACHACC",
+         winbindd_change_machine_acct_send, winbindd_change_machine_acct_recv },
 
        { 0, NULL, NULL, NULL }
 };
@@ -565,6 +601,15 @@ static void process_request(struct winbindd_cli_state *state)
                }
        }
 
+       if ((atable->send_req == NULL) && state->privileged) {
+               for (atable = async_priv_table; atable->send_req;
+                    atable += 1) {
+                       if (state->request->cmd == atable->cmd) {
+                               break;
+                       }
+               }
+       }
+
        if (atable->send_req != NULL) {
                struct tevent_req *req;
 
@@ -675,6 +720,8 @@ static void winbind_client_response_written(struct tevent_req *req)
        ret = wb_resp_write_recv(req, &err);
        TALLOC_FREE(req);
        if (ret == -1) {
+               close(state->sock);
+               state->sock = -1;
                DEBUG(2, ("Could not write response to client: %s\n",
                          strerror(err)));
                remove_client(state);
@@ -775,8 +822,15 @@ static void winbind_client_request_read(struct tevent_req *req)
        ret = wb_req_read_recv(req, state, &state->request, &err);
        TALLOC_FREE(req);
        if (ret == -1) {
-               DEBUG(2, ("Could not read client request: %s\n",
-                         strerror(err)));
+               if (err == EPIPE) {
+                       DEBUG(6, ("closing socket %d, client exited\n",
+                                 state->sock));
+               } else {
+                       DEBUG(2, ("Could not read client request from fd %d: "
+                                 "%s\n", state->sock, strerror(err)));
+               }
+               close(state->sock);
+               state->sock = -1;
                remove_client(state);
                return;
        }
@@ -796,26 +850,19 @@ static void remove_client(struct winbindd_cli_state *state)
                return;
        }
 
-       /* tell client, we are closing ... */
-       nwritten = write(state->sock, &c, sizeof(c));
-       if (nwritten == -1) {
-               /* 
-                * ignore EPIPE error here, because the other end might
-                * have already closed the socket.
-                */
-               if (errno != EPIPE) {
+       if (state->sock != -1) {
+               /* tell client, we are closing ... */
+               nwritten = write(state->sock, &c, sizeof(c));
+               if (nwritten == -1) {
                        DEBUG(2, ("final write to client failed: %s\n",
-                               strerror(errno)));
+                               strerror(errno)));
                }
-       }
 
-       /* Close socket */
+               /* Close socket */
 
-       close(state->sock);
-
-       /* Free any getent state */
-
-       free_getent_state(state->getgrent_state);
+               close(state->sock);
+               state->sock = -1;
+       }
 
        TALLOC_FREE(state->mem_ctx);
 
@@ -835,7 +882,7 @@ static bool remove_idle_client(void)
 
        for (state = winbindd_client_list(); state; state = state->next) {
                if (state->response == NULL &&
-                   !state->pwent_state && !state->getgrent_state) {
+                   !state->pwent_state && !state->grent_state) {
                        nidle++;
                        if (!last_access || state->last_access < last_access) {
                                last_access = state->last_access;