Fix a race condition in winbind leading to a crash
[samba.git] / source / winbindd / winbindd_dual.c
index 7e53fbbbeeb91570825cf79371e1d2e999fdae6e..f2be6d692c5d57941d6f0af61d7df175df7c2b80 100644 (file)
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_WINBIND
 
-extern BOOL override_logfile;
+extern bool override_logfile;
+extern struct winbindd_methods cache_methods;
 
 /* Read some data from a client connection */
 
 static void child_read_request(struct winbindd_cli_state *state)
 {
-       ssize_t len;
+       NTSTATUS status;
 
        /* Read data */
 
-       len = read_data(state->sock, (char *)&state->request,
-                       sizeof(state->request));
+       status = read_data(state->sock, (char *)&state->request,
+                          sizeof(state->request));
 
-       if (len != sizeof(state->request)) {
-               DEBUG(len > 0 ? 0 : 3, ("Got invalid request length: %d\n", (int)len));
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(3, ("child_read_request: read_data failed: %s\n",
+                         nt_errstr(status)));
                state->finished = True;
                return;
        }
@@ -71,11 +73,12 @@ static void child_read_request(struct winbindd_cli_state *state)
        /* Ensure null termination */
        state->request.extra_data.data[state->request.extra_len] = '\0';
 
-       len = read_data(state->sock, state->request.extra_data.data,
-                       state->request.extra_len);
+       status= read_data(state->sock, state->request.extra_data.data,
+                         state->request.extra_len);
 
-       if (len != state->request.extra_len) {
-               DEBUG(0, ("Could not read extra data\n"));
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0, ("Could not read extra data: %s\n",
+                         nt_errstr(status)));
                state->finished = True;
                return;
        }
@@ -94,22 +97,23 @@ struct winbindd_async_request {
        struct winbindd_child *child;
        struct winbindd_request *request;
        struct winbindd_response *response;
-       void (*continuation)(void *private_data, BOOL success);
+       void (*continuation)(void *private_data, bool success);
        struct timed_event *reply_timeout_event;
        pid_t child_pid; /* pid of the child we're waiting on. Used to detect
                            a restart of the child (child->pid != child_pid). */
        void *private_data;
 };
 
-static void async_main_request_sent(void *private_data, BOOL success);
-static void async_request_sent(void *private_data, BOOL success);
-static void async_reply_recv(void *private_data, BOOL success);
+static void async_request_fail(struct winbindd_async_request *state);
+static void async_main_request_sent(void *private_data, bool success);
+static void async_request_sent(void *private_data, bool success);
+static void async_reply_recv(void *private_data, bool success);
 static void schedule_async_request(struct winbindd_child *child);
 
 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
                   struct winbindd_request *request,
                   struct winbindd_response *response,
-                  void (*continuation)(void *private_data, BOOL success),
+                  void (*continuation)(void *private_data, bool success),
                   void *private_data)
 {
        struct winbindd_async_request *state;
@@ -126,6 +130,7 @@ void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
 
        state->mem_ctx = mem_ctx;
        state->child = child;
+       state->reply_timeout_event = NULL;
        state->request = request;
        state->response = response;
        state->continuation = continuation;
@@ -138,17 +143,14 @@ void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
        return;
 }
 
-static void async_main_request_sent(void *private_data, BOOL success)
+static void async_main_request_sent(void *private_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data, struct winbindd_async_request);
 
        if (!success) {
                DEBUG(5, ("Could not send async request\n"));
-
-               state->response->length = sizeof(struct winbindd_response);
-               state->response->result = WINBINDD_ERROR;
-               state->continuation(state->private_data, False);
+               async_request_fail(state);
                return;
        }
 
@@ -211,7 +213,7 @@ static void async_request_fail(struct winbindd_async_request *state)
        state->continuation(state->private_data, False);
 }
 
-static void async_request_sent(void *private_data_data, BOOL success)
+static void async_request_sent(void *private_data_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data_data, struct winbindd_async_request);
@@ -247,7 +249,7 @@ static void async_request_sent(void *private_data_data, BOOL success)
        }
 }
 
-static void async_reply_recv(void *private_data, BOOL success)
+static void async_reply_recv(void *private_data, bool success)
 {
        struct winbindd_async_request *state =
                talloc_get_type_abort(private_data, struct winbindd_async_request);
@@ -278,7 +280,7 @@ static void async_reply_recv(void *private_data, BOOL success)
        state->continuation(state->private_data, True);
 }
 
-static BOOL fork_domain_child(struct winbindd_child *child);
+static bool fork_domain_child(struct winbindd_child *child);
 
 static void schedule_async_request(struct winbindd_child *child)
 {
@@ -319,17 +321,17 @@ struct domain_request_state {
        struct winbindd_domain *domain;
        struct winbindd_request *request;
        struct winbindd_response *response;
-       void (*continuation)(void *private_data_data, BOOL success);
+       void (*continuation)(void *private_data_data, bool success);
        void *private_data_data;
 };
 
-static void domain_init_recv(void *private_data_data, BOOL success);
+static void domain_init_recv(void *private_data_data, bool success);
 
 void async_domain_request(TALLOC_CTX *mem_ctx,
                          struct winbindd_domain *domain,
                          struct winbindd_request *request,
                          struct winbindd_response *response,
-                         void (*continuation)(void *private_data_data, BOOL success),
+                         void (*continuation)(void *private_data_data, bool success),
                          void *private_data_data)
 {
        struct domain_request_state *state;
@@ -357,7 +359,7 @@ void async_domain_request(TALLOC_CTX *mem_ctx,
        init_child_connection(domain, domain_init_recv, state);
 }
 
-static void domain_init_recv(void *private_data_data, BOOL success)
+static void domain_init_recv(void *private_data_data, bool success)
 {
        struct domain_request_state *state =
                talloc_get_type_abort(private_data_data, struct domain_request_state);
@@ -373,7 +375,7 @@ static void domain_init_recv(void *private_data_data, BOOL success)
                      state->continuation, state->private_data_data);
 }
 
-static void recvfrom_child(void *private_data_data, BOOL success)
+static void recvfrom_child(void *private_data_data, bool success)
 {
        struct winbindd_cli_state *state =
                talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
@@ -408,30 +410,6 @@ void sendto_domain(struct winbindd_cli_state *state,
                             recvfrom_child, state);
 }
 
-const struct winbindd_child_dispatch_table domain_dispatch_table[] = {
-
-       { WINBINDD_LOOKUPSID,            winbindd_dual_lookupsid,             "LOOKUPSID" },
-       { WINBINDD_LOOKUPNAME,           winbindd_dual_lookupname,            "LOOKUPNAME" },
-       { WINBINDD_LOOKUPRIDS,           winbindd_dual_lookuprids,            "LOOKUPRIDS" },
-       { WINBINDD_LIST_TRUSTDOM,        winbindd_dual_list_trusted_domains,  "LIST_TRUSTDOM" },
-       { WINBINDD_INIT_CONNECTION,      winbindd_dual_init_connection,       "INIT_CONNECTION" },
-       { WINBINDD_GETDCNAME,            winbindd_dual_getdcname,             "GETDCNAME" },
-       { WINBINDD_SHOW_SEQUENCE,        winbindd_dual_show_sequence,         "SHOW_SEQUENCE" },
-       { WINBINDD_PAM_AUTH,             winbindd_dual_pam_auth,              "PAM_AUTH" },
-       { WINBINDD_PAM_AUTH_CRAP,        winbindd_dual_pam_auth_crap,         "AUTH_CRAP" },
-       { WINBINDD_PAM_LOGOFF,           winbindd_dual_pam_logoff,            "PAM_LOGOFF" },
-       { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
-       { WINBINDD_PAM_CHAUTHTOK,        winbindd_dual_pam_chauthtok,         "PAM_CHAUTHTOK" },
-       { WINBINDD_CHECK_MACHACC,        winbindd_dual_check_machine_acct,    "CHECK_MACHACC" },
-       { WINBINDD_DUAL_USERINFO,        winbindd_dual_userinfo,              "DUAL_USERINFO" },
-       { WINBINDD_GETUSERDOMGROUPS,     winbindd_dual_getuserdomgroups,      "GETUSERDOMGROUPS" },
-       { WINBINDD_DUAL_GETSIDALIASES,   winbindd_dual_getsidaliases,         "GETSIDALIASES" },
-       { WINBINDD_CCACHE_NTLMAUTH,      winbindd_dual_ccache_ntlm_auth,      "CCACHE_NTLM_AUTH" },
-       /* End of list */
-
-       { WINBINDD_NUM_CMDS, NULL, "NONE" }
-};
-
 static void child_process_request(struct winbindd_child *child,
                                  struct winbindd_cli_state *state)
 {
@@ -449,39 +427,36 @@ static void child_process_request(struct winbindd_child *child,
 
        /* Process command */
 
-       for (; table->fn; table++) {
-               if (state->request.cmd == table->cmd) {
-                       DEBUG(10,("process_request: request fn %s\n",
-                                 table->winbindd_cmd_name ));
-                       state->response.result = table->fn(domain, state);
-                       break;
+       for (; table->name; table++) {
+               if (state->request.cmd == table->struct_cmd) {
+                       DEBUG(10,("child_process_request: request fn %s\n",
+                                 table->name));
+                       state->response.result = table->struct_fn(domain, state);
+                       return;
                }
        }
 
-       if (!table->fn) {
-               DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
-                         (int)state->request.cmd ));
-               state->response.result = WINBINDD_ERROR;
-       }
+       DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
+                 (int)state->request.cmd));
+       state->response.result = WINBINDD_ERROR;
 }
 
-void setup_domain_child(struct winbindd_domain *domain,
-                       struct winbindd_child *child,
-                       const struct winbindd_child_dispatch_table *table,
-                       const char *explicit_logfile)
+void setup_child(struct winbindd_child *child,
+                const struct winbindd_child_dispatch_table *table,
+                const char *logprefix,
+                const char *logname)
 {
-       if (explicit_logfile != NULL) {
-               pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
-                            dyn_LOGFILEBASE, explicit_logfile);
-       } else if (domain != NULL) {
-               pstr_sprintf(child->logfilename, "%s/log.wb-%s",
-                            dyn_LOGFILEBASE, domain->name);
+       if (logprefix && logname) {
+               if (asprintf(&child->logfilename, "%s/%s-%s",
+                            get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
+                       smb_panic("Internal error: asprintf failed");
+               }
        } else {
-               smb_panic("Internal error: domain == NULL && "
-                         "explicit_logfile == NULL");
+               smb_panic("Internal error: logprefix == NULL && "
+                         "logname == NULL");
        }
 
-       child->domain = domain;
+       child->domain = NULL;
        child->table = table;
 }
 
@@ -502,6 +477,10 @@ void winbind_child_died(pid_t pid)
                return;
        }
 
+       /* This will be re-added in fork_domain_child() */
+
+       DLIST_REMOVE(children, child);
+       
        remove_fd_event(&child->event);
        close(child->event.fd);
        child->event.fd = 0;
@@ -521,6 +500,36 @@ void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
        }
 }
 
+/* 
+ * Parent winbindd process sets its own debug level first and then
+ * sends a message to all the winbindd children to adjust their debug
+ * level to that of parents.
+ */
+
+void winbind_msg_debug(struct messaging_context *msg_ctx,
+                        void *private_data,
+                        uint32_t msg_type,
+                        struct server_id server_id,
+                        DATA_BLOB *data)
+{
+       struct winbindd_child *child;
+
+       DEBUG(10,("winbind_msg_debug: got debug message.\n"));
+       
+       debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
+
+       for (child = children; child != NULL; child = child->next) {
+
+               DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
+                       (unsigned int)child->pid));
+
+               messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
+                          MSG_DEBUG,
+                          data->data,
+                          strlen((char *) data->data) + 1);
+       }
+}
+
 /* Set our domains as offline and forward the offline message to our children. */
 
 void winbind_msg_offline(struct messaging_context *msg_ctx,
@@ -703,6 +712,88 @@ void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
 
 }
 
+void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
+                                 void *private_data,
+                                 uint32_t msg_type,
+                                 struct server_id server_id,
+                                 DATA_BLOB *data)
+{
+       TALLOC_CTX *mem_ctx;
+       const char *message = NULL;
+       struct server_id *sender = NULL;
+       const char *domain = NULL;
+       char *s = NULL;
+       NTSTATUS status;
+       struct winbindd_domain *dom = NULL;
+
+       DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
+
+       if (!data || !data->data) {
+               return;
+       }
+
+       if (data->length < sizeof(struct server_id)) {
+               return;
+       }
+
+       mem_ctx = talloc_init("winbind_msg_dump_domain_list");
+       if (!mem_ctx) {
+               return;
+       }
+
+       sender = (struct server_id *)data->data;
+       if (data->length > sizeof(struct server_id)) {
+               domain = (const char *)data->data+sizeof(struct server_id);
+       }
+
+       if (domain) {
+
+               DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
+                       domain));
+
+               message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
+                                                 find_domain_from_name_noinit(domain));
+               if (!message) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+
+               messaging_send_buf(msg_ctx, *sender,
+                                  MSG_WINBIND_DUMP_DOMAIN_LIST,
+                                  (uint8_t *)message, strlen(message) + 1);
+
+               talloc_destroy(mem_ctx);
+
+               return;
+       }
+
+       DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
+
+       for (dom = domain_list(); dom; dom=dom->next) {
+               message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
+               if (!message) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+
+               s = talloc_asprintf_append(s, "%s\n", message);
+               if (!s) {
+                       talloc_destroy(mem_ctx);
+                       return;
+               }
+       }
+
+       status = messaging_send_buf(msg_ctx, *sender,
+                                   MSG_WINBIND_DUMP_DOMAIN_LIST,
+                                   (uint8_t *)s, strlen(s) + 1);
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(0,("failed to send message: %s\n",
+               nt_errstr(status)));
+       }
+
+       talloc_destroy(mem_ctx);
+}
+
 static void account_lockout_policy_handler(struct event_context *ctx,
                                           struct timed_event *te,
                                           const struct timeval *now,
@@ -712,7 +803,7 @@ static void account_lockout_policy_handler(struct event_context *ctx,
                (struct winbindd_child *)private_data;
        TALLOC_CTX *mem_ctx = NULL;
        struct winbindd_methods *methods;
-       SAM_UNK_INFO_12 lockout_policy;
+       struct samr_DomInfo12 lockout_policy;
        NTSTATUS result;
 
        DEBUG(10,("account_lockout_policy_handler called\n"));
@@ -898,11 +989,19 @@ static void child_msg_dump_event_list(struct messaging_context *msg,
 }
 
 
-static BOOL fork_domain_child(struct winbindd_child *child)
+static bool fork_domain_child(struct winbindd_child *child)
 {
        int fdpair[2];
        struct winbindd_cli_state state;
        struct winbindd_domain *domain;
+       struct winbindd_domain *primary_domain = NULL;
+
+       if (child->domain) {
+               DEBUG(10, ("fork_domain_child called for domain '%s'\n",
+                          child->domain->name));
+       } else {
+               DEBUG(10, ("fork_domain_child called without domain.\n"));
+       }
 
        if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
                DEBUG(0, ("Could not open child pipe: %s\n",
@@ -913,9 +1012,6 @@ static BOOL fork_domain_child(struct winbindd_child *child)
        ZERO_STRUCT(state);
        state.pid = sys_getpid();
 
-       /* Stop zombies */
-       CatchChild();
-
        child->pid = sys_fork();
 
        if (child->pid == -1) {
@@ -937,12 +1033,14 @@ static BOOL fork_domain_child(struct winbindd_child *child)
 
        /* Child */
 
+       /* Stop zombies in children */
+       CatchChild();
+
        state.sock = fdpair[0];
        close(fdpair[1]);
 
-       /* tdb needs special fork handling */
-       if (tdb_reopen_all(1) == -1) {
-               DEBUG(0,("tdb_reopen_all failed.\n"));
+       if (!reinit_after_fork(winbind_messaging_context(), true)) {
+               DEBUG(0,("reinit_after_fork() failed\n"));
                _exit(0);
        }
 
@@ -973,6 +1071,10 @@ static BOOL fork_domain_child(struct winbindd_child *child)
                             MSG_WINBIND_ONLINESTATUS, NULL);
        messaging_deregister(winbind_messaging_context(),
                             MSG_DUMP_EVENT_LIST, NULL);
+       messaging_deregister(winbind_messaging_context(),
+                            MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
+       messaging_deregister(winbind_messaging_context(),
+                            MSG_DEBUG, NULL);
 
        /* Handle online/offline messages. */
        messaging_register(winbind_messaging_context(), NULL,
@@ -983,6 +1085,8 @@ static BOOL fork_domain_child(struct winbindd_child *child)
                           MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
        messaging_register(winbind_messaging_context(), NULL,
                           MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
+       messaging_register(winbind_messaging_context(), NULL,
+                          MSG_DEBUG, debug_message);
 
        if ( child->domain ) {
                child->domain->startup = True;
@@ -990,10 +1094,13 @@ static BOOL fork_domain_child(struct winbindd_child *child)
        }
 
        /* Ensure we have no pending check_online events other
-          than one for this domain. */
+          than one for this domain or the primary domain. */
 
        for (domain = domain_list(); domain; domain = domain->next) {
-               if (domain != child->domain) {
+               if (domain->primary) {
+                       primary_domain = domain;
+               }
+               if ((domain != child->domain) && !domain->primary) {
                        TALLOC_FREE(domain->check_online_event);
                }
        }
@@ -1010,6 +1117,18 @@ static BOOL fork_domain_child(struct winbindd_child *child)
 
                set_domain_online_request(child->domain);
 
+               if (primary_domain != child->domain) {
+                       /* We need to talk to the primary
+                        * domain as well as the trusted
+                        * domain inside a trusted domain
+                        * child.
+                        * See the code in :
+                        * set_dc_type_and_flags_trustinfo()
+                        * for details.
+                        */
+                       set_domain_online_request(primary_domain);
+               }
+
                child->lockout_policy_event = event_add_timed(
                        winbind_event_context(), NULL, timeval_zero(),
                        "account_lockout_policy_handler",
@@ -1026,6 +1145,11 @@ static BOOL fork_domain_child(struct winbindd_child *child)
                struct timeval now;
                TALLOC_CTX *frame = talloc_stackframe();
 
+               /* check for signals */
+               winbind_check_sigterm(false);
+               winbind_check_sighup(override_logfile ? NULL :
+                               child->logfilename);
+
                run_events(winbind_event_context(), 0, NULL, NULL);
 
                GetTimeOfDay(&now);