From boyang - ensure we never "return" from a forked child, always _exit().
[jra/samba/.git] / source3 / winbindd / winbindd_cm.c
index 05c39f8f2b6be8029540e1a96b31faf2ef8dcd27..962b6e6d1b7257c768fb40fafaf5beec18cee5e6 100644 (file)
@@ -172,6 +172,7 @@ static bool fork_child_dc_connect(struct winbindd_domain *domain)
        int num_dcs = 0;
        TALLOC_CTX *mem_ctx = NULL;
        pid_t parent_pid = sys_getpid();
+       char *lfile = NULL;
 
        /* Stop zombies */
        CatchChild();
@@ -212,26 +213,22 @@ static bool fork_child_dc_connect(struct winbindd_domain *domain)
 
        /* Leave messages blocked - we will never process one. */
 
-       if (!reinit_after_fork(winbind_messaging_context(), true)) {
-               DEBUG(0,("reinit_after_fork() failed\n"));
+       if (!override_logfile) {
+               if (asprintf(&lfile, "%s/log.winbindd-dc-connect", get_dyn_LOGFILEBASE()) == -1) {
+                       DEBUG(0, ("fork_child_dc_connect: out of memory.\n"));
+                       _exit(1);
+               }
+       }
+
+       if (!winbindd_reinit_after_fork(lfile)) {
                messaging_send_buf(winbind_messaging_context(),
                                   pid_to_procid(parent_pid),
                                   MSG_WINBIND_FAILED_TO_GO_ONLINE,
                                   (uint8 *)domain->name,
                                   strlen(domain->name)+1);
-               _exit(0);
-       }
-
-       close_conns_after_fork();
-
-       if (!override_logfile) {
-               char *logfile;
-               if (asprintf(&logfile, "%s/log.winbindd-dc-connect", get_dyn_LOGFILEBASE()) > 0) {
-                       lp_set_logfile(logfile);
-                       SAFE_FREE(logfile);
-                       reopen_logs();
-               }
+               _exit(1);
        }
+       SAFE_FREE(lfile);
 
        mem_ctx = talloc_init("fork_child_dc_connect");
        if (!mem_ctx) {
@@ -241,7 +238,7 @@ static bool fork_child_dc_connect(struct winbindd_domain *domain)
                                   MSG_WINBIND_FAILED_TO_GO_ONLINE,
                                   (uint8 *)domain->name,
                                   strlen(domain->name)+1);
-               _exit(0);
+               _exit(1);
        }
 
        if ((!get_dcs(mem_ctx, domain, &dcs, &num_dcs)) || (num_dcs == 0)) {
@@ -271,7 +268,7 @@ static bool fork_child_dc_connect(struct winbindd_domain *domain)
 
 static void check_domain_online_handler(struct event_context *ctx,
                                        struct timed_event *te,
-                                       const struct timeval *now,
+                                       struct timeval now,
                                        void *private_data)
 {
         struct winbindd_domain *domain =
@@ -285,7 +282,7 @@ static void check_domain_online_handler(struct event_context *ctx,
 
        /* Are we still in "startup" mode ? */
 
-       if (domain->startup && (now->tv_sec > domain->startup_time + 30)) {
+       if (domain->startup && (now.tv_sec > domain->startup_time + 30)) {
                /* No longer in "startup" mode. */
                DEBUG(10,("check_domain_online_handler: domain %s no longer in 'startup' mode.\n",
                        domain->name ));
@@ -366,7 +363,6 @@ void set_domain_offline(struct winbindd_domain *domain)
        domain->check_online_event = event_add_timed(winbind_event_context(),
                                                NULL,
                                                timeval_current_ofs(domain->check_online_timeout,0),
-                                               "check_domain_online_handler",
                                                check_domain_online_handler,
                                                domain);
 
@@ -402,8 +398,6 @@ void set_domain_offline(struct winbindd_domain *domain)
 
 static void set_domain_online(struct winbindd_domain *domain)
 {
-       struct timeval now;
-
        DEBUG(10,("set_domain_online: called for domain %s\n",
                domain->name ));
 
@@ -422,9 +416,7 @@ static void set_domain_online(struct winbindd_domain *domain)
        winbindd_set_locator_kdc_envs(domain);
 
        /* If we are waiting to get a krb5 ticket, trigger immediately. */
-       GetTimeOfDay(&now);
-       set_event_dispatch_time(winbind_event_context(),
-                               "krb5_ticket_gain_handler", now);
+       ccache_regain_all_now();
 
        /* Ok, we're out of any startup mode now... */
        domain->startup = False;
@@ -497,6 +489,15 @@ void set_domain_online_request(struct winbindd_domain *domain)
           because network manager seems to lie.
           Wait at least 5 seconds. Heuristics suck... */
 
+
+       GetTimeOfDay(&tev);
+
+       /* Go into "startup" mode again. */
+       domain->startup_time = tev.tv_sec;
+       domain->startup = True;
+
+       tev.tv_sec += 5;
+
        if (!domain->check_online_event) {
                /* If we've come from being globally offline we
                   don't have a check online event handler set.
@@ -505,29 +506,20 @@ void set_domain_online_request(struct winbindd_domain *domain)
 
                DEBUG(10,("set_domain_online_request: domain %s was globally offline.\n",
                        domain->name ));
-
-               domain->check_online_event = event_add_timed(winbind_event_context(),
-                                                               NULL,
-                                                               timeval_current_ofs(5, 0),
-                                                               "check_domain_online_handler",
-                                                               check_domain_online_handler,
-                                                               domain);
-
-               /* The above *has* to succeed for winbindd to work. */
-               if (!domain->check_online_event) {
-                       smb_panic("set_domain_online_request: failed to add online handler");
-               }
        }
 
-       GetTimeOfDay(&tev);
-
-       /* Go into "startup" mode again. */
-       domain->startup_time = tev.tv_sec;
-       domain->startup = True;
+       TALLOC_FREE(domain->check_online_event);
 
-       tev.tv_sec += 5;
+       domain->check_online_event = event_add_timed(winbind_event_context(),
+                                                    NULL,
+                                                    tev,
+                                                    check_domain_online_handler,
+                                                    domain);
 
-       set_event_dispatch_time(winbind_event_context(), "check_domain_online_handler", tev);
+       /* The above *has* to succeed for winbindd to work. */
+       if (!domain->check_online_event) {
+               smb_panic("set_domain_online_request: failed to add online handler");
+       }
 }
 
 /****************************************************************
@@ -672,7 +664,7 @@ static bool get_dc_name_via_netlogon(struct winbindd_domain *domain,
 
        if (!W_ERROR_IS_OK(werr)) {
                DEBUG(10,("rpccli_netr_GetAnyDCName failed: %s\n",
-                          dos_errstr(werr)));
+                          win_errstr(werr)));
                talloc_destroy(mem_ctx);
                return false;
        }
@@ -831,9 +823,10 @@ static NTSTATUS cm_prepare_connection(const struct winbindd_domain *domain,
 
        cli_setup_signing_state(*cli, Undefined);
 
-       if (!cli_negprot(*cli)) {
-               DEBUG(1, ("cli_negprot failed\n"));
-               result = NT_STATUS_UNSUCCESSFUL;
+       result = cli_negprot(*cli);
+
+       if (!NT_STATUS_IS_OK(result)) {
+               DEBUG(1, ("cli_negprot failed: %s\n", nt_errstr(result)));
                goto done;
        }
 
@@ -1038,7 +1031,7 @@ static bool add_one_dc_unique(TALLOC_CTX *mem_ctx, const char *domain_name,
 
        /* Make sure there's no duplicates in the list */
        for (i=0; i<*num; i++)
-               if (addr_equal((struct sockaddr *)&(*dcs)[i].ss, (struct sockaddr *)pss))
+               if (sockaddr_equal((struct sockaddr *)&(*dcs)[i].ss, (struct sockaddr *)pss))
                        return False;
 
        *dcs = TALLOC_REALLOC_ARRAY(mem_ctx, *dcs, struct dc_name_ip, (*num)+1);
@@ -1064,7 +1057,7 @@ static bool add_sockaddr_to_array(TALLOC_CTX *mem_ctx,
        }
 
        (*addrs)[*num] = *pss;
-       set_sockaddr_port(&(*addrs)[*num], port);
+       set_sockaddr_port((struct sockaddr *)&(*addrs)[*num], port);
 
        *num += 1;
        return True;