2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2002
7 Copyright (C) Volker Lendecke 2004,2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * We fork a child per domain to be able to act non-blocking in the main
26 * winbind daemon. A domain controller thousands of miles away being being
27 * slow replying with a 10.000 user list should not hold up netlogon calls
28 * that can be handled locally.
35 #define DBGC_CLASS DBGC_WINBIND
37 extern BOOL override_logfile;
39 /* Read some data from a client connection */
41 static void child_read_request(struct winbindd_cli_state *state)
47 len = read_data(state->sock, (char *)&state->request,
48 sizeof(state->request));
50 if (len != sizeof(state->request)) {
51 DEBUG(len > 0 ? 0 : 3, ("Got invalid request length: %d\n", (int)len));
52 state->finished = True;
56 if (state->request.extra_len == 0) {
57 state->request.extra_data.data = NULL;
61 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
63 state->request.extra_data.data =
64 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
66 if (state->request.extra_data.data == NULL) {
67 DEBUG(0, ("malloc failed\n"));
68 state->finished = True;
72 /* Ensure null termination */
73 state->request.extra_data.data[state->request.extra_len] = '\0';
75 len = read_data(state->sock, state->request.extra_data.data,
76 state->request.extra_len);
78 if (len != state->request.extra_len) {
79 DEBUG(0, ("Could not read extra data\n"));
80 state->finished = True;
86 * Machinery for async requests sent to children. You set up a
87 * winbindd_request, select a child to query, and issue a async_request
88 * call. When the request is completed, the callback function you specified is
89 * called back with the private pointer you gave to async_request.
92 struct winbindd_async_request {
93 struct winbindd_async_request *next, *prev;
95 struct winbindd_child *child;
96 struct winbindd_request *request;
97 struct winbindd_response *response;
98 void (*continuation)(void *private_data, BOOL success);
99 struct timed_event *reply_timeout_event;
103 static void async_main_request_sent(void *private_data, BOOL success);
104 static void async_request_sent(void *private_data, BOOL success);
105 static void async_reply_recv(void *private_data, BOOL success);
106 static void schedule_async_request(struct winbindd_child *child);
108 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
109 struct winbindd_request *request,
110 struct winbindd_response *response,
111 void (*continuation)(void *private_data, BOOL success),
114 struct winbindd_async_request *state;
116 SMB_ASSERT(continuation != NULL);
118 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
121 DEBUG(0, ("talloc failed\n"));
122 continuation(private_data, False);
126 state->mem_ctx = mem_ctx;
127 state->child = child;
128 state->request = request;
129 state->response = response;
130 state->continuation = continuation;
131 state->private_data = private_data;
133 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
135 schedule_async_request(child);
140 static void async_main_request_sent(void *private_data, BOOL success)
142 struct winbindd_async_request *state =
143 talloc_get_type_abort(private_data, struct winbindd_async_request);
146 DEBUG(5, ("Could not send async request\n"));
148 state->response->length = sizeof(struct winbindd_response);
149 state->response->result = WINBINDD_ERROR;
150 state->continuation(state->private_data, False);
154 if (state->request->extra_len == 0) {
155 async_request_sent(private_data, True);
159 setup_async_write(&state->child->event, state->request->extra_data.data,
160 state->request->extra_len,
161 async_request_sent, state);
164 /****************************************************************
165 Handler triggered if the child winbindd doesn't respond within
167 ****************************************************************/
169 static void async_request_timeout_handler(struct event_context *ctx,
170 struct timed_event *te,
171 const struct timeval *now,
174 struct winbindd_async_request *state =
175 talloc_get_type_abort(private_data, struct winbindd_async_request);
177 /* Deal with the reply - set to error. */
179 async_reply_recv(private_data, False);
181 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
182 "Closing connection to it.\n",
183 state->child->pid ));
185 /* Send kill signal to child. */
186 kill(state->child->pid, SIGTERM);
189 * Close the socket to the child.
192 winbind_child_died(state->child->pid);
195 static void async_request_sent(void *private_data_data, BOOL success)
197 uint32_t timeout = 30;
198 struct winbindd_async_request *state =
199 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
202 DEBUG(5, ("Could not send async request\n"));
204 state->response->length = sizeof(struct winbindd_response);
205 state->response->result = WINBINDD_ERROR;
206 state->continuation(state->private_data, False);
210 /* Request successfully sent to the child, setup the wait for reply */
212 setup_async_read(&state->child->event,
213 &state->response->result,
214 sizeof(state->response->result),
215 async_reply_recv, state);
218 * Normal timeouts are 30s, but auth requests may take a long
222 if (state->request->cmd == WINBINDD_PAM_AUTH ||
223 state->request->cmd == WINBINDD_PAM_AUTH_CRAP ) {
229 * Set up a timeout of 30 seconds for the response.
230 * If we don't get it close the child socket and
234 state->reply_timeout_event = event_add_timed(winbind_event_context(),
236 timeval_current_ofs(timeout,0),
237 "async_request_timeout",
238 async_request_timeout_handler,
240 if (!state->reply_timeout_event) {
241 smb_panic("async_request_sent: failed to add timeout handler.\n");
245 static void async_reply_recv(void *private_data, BOOL success)
247 struct winbindd_async_request *state =
248 talloc_get_type_abort(private_data, struct winbindd_async_request);
249 struct winbindd_child *child = state->child;
251 if (state->reply_timeout_event) {
252 TALLOC_FREE(state->reply_timeout_event);
255 state->response->length = sizeof(struct winbindd_response);
258 DEBUG(5, ("Could not receive async reply\n"));
260 cache_cleanup_response(child->pid);
261 DLIST_REMOVE(child->requests, state);
263 state->response->result = WINBINDD_ERROR;
264 state->continuation(state->private_data, False);
268 SMB_ASSERT(cache_retrieve_response(child->pid,
271 cache_cleanup_response(child->pid);
273 DLIST_REMOVE(child->requests, state);
275 schedule_async_request(child);
277 state->continuation(state->private_data, True);
280 static BOOL fork_domain_child(struct winbindd_child *child);
282 static void schedule_async_request(struct winbindd_child *child)
284 struct winbindd_async_request *request = child->requests;
286 if (request == NULL) {
290 if (child->event.flags != 0) {
294 if ((child->pid == 0) && (!fork_domain_child(child))) {
295 /* Cancel all outstanding requests */
297 while (request != NULL) {
298 /* request might be free'd in the continuation */
299 struct winbindd_async_request *next = request->next;
300 request->continuation(request->private_data, False);
306 setup_async_write(&child->event, request->request,
307 sizeof(*request->request),
308 async_main_request_sent, request);
313 struct domain_request_state {
315 struct winbindd_domain *domain;
316 struct winbindd_request *request;
317 struct winbindd_response *response;
318 void (*continuation)(void *private_data_data, BOOL success);
319 void *private_data_data;
322 static void domain_init_recv(void *private_data_data, BOOL success);
324 void async_domain_request(TALLOC_CTX *mem_ctx,
325 struct winbindd_domain *domain,
326 struct winbindd_request *request,
327 struct winbindd_response *response,
328 void (*continuation)(void *private_data_data, BOOL success),
329 void *private_data_data)
331 struct domain_request_state *state;
333 if (domain->initialized) {
334 async_request(mem_ctx, &domain->child, request, response,
335 continuation, private_data_data);
339 state = TALLOC_P(mem_ctx, struct domain_request_state);
341 DEBUG(0, ("talloc failed\n"));
342 continuation(private_data_data, False);
346 state->mem_ctx = mem_ctx;
347 state->domain = domain;
348 state->request = request;
349 state->response = response;
350 state->continuation = continuation;
351 state->private_data_data = private_data_data;
353 init_child_connection(domain, domain_init_recv, state);
356 static void recvfrom_child(void *private_data_data, BOOL success)
358 struct winbindd_cli_state *state =
359 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
360 enum winbindd_result result = state->response.result;
362 /* This is an optimization: The child has written directly to the
363 * response buffer. The request itself is still in pending state,
364 * state that in the result code. */
366 state->response.result = WINBINDD_PENDING;
368 if ((!success) || (result != WINBINDD_OK)) {
369 request_error(state);
376 void sendto_child(struct winbindd_cli_state *state,
377 struct winbindd_child *child)
379 async_request(state->mem_ctx, child, &state->request,
380 &state->response, recvfrom_child, state);
383 void sendto_domain(struct winbindd_cli_state *state,
384 struct winbindd_domain *domain)
386 async_domain_request(state->mem_ctx, domain,
387 &state->request, &state->response,
388 recvfrom_child, state);
391 static void domain_init_recv(void *private_data_data, BOOL success)
393 struct domain_request_state *state =
394 talloc_get_type_abort(private_data_data, struct domain_request_state);
397 DEBUG(5, ("Domain init returned an error\n"));
398 state->continuation(state->private_data_data, False);
402 async_request(state->mem_ctx, &state->domain->child,
403 state->request, state->response,
404 state->continuation, state->private_data_data);
407 struct winbindd_child_dispatch_table {
408 enum winbindd_cmd cmd;
409 enum winbindd_result (*fn)(struct winbindd_domain *domain,
410 struct winbindd_cli_state *state);
411 const char *winbindd_cmd_name;
414 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
416 { WINBINDD_LOOKUPSID, winbindd_dual_lookupsid, "LOOKUPSID" },
417 { WINBINDD_LOOKUPNAME, winbindd_dual_lookupname, "LOOKUPNAME" },
418 { WINBINDD_LOOKUPRIDS, winbindd_dual_lookuprids, "LOOKUPRIDS" },
419 { WINBINDD_LIST_TRUSTDOM, winbindd_dual_list_trusted_domains, "LIST_TRUSTDOM" },
420 { WINBINDD_INIT_CONNECTION, winbindd_dual_init_connection, "INIT_CONNECTION" },
421 { WINBINDD_GETDCNAME, winbindd_dual_getdcname, "GETDCNAME" },
422 { WINBINDD_SHOW_SEQUENCE, winbindd_dual_show_sequence, "SHOW_SEQUENCE" },
423 { WINBINDD_PAM_AUTH, winbindd_dual_pam_auth, "PAM_AUTH" },
424 { WINBINDD_PAM_AUTH_CRAP, winbindd_dual_pam_auth_crap, "AUTH_CRAP" },
425 { WINBINDD_PAM_LOGOFF, winbindd_dual_pam_logoff, "PAM_LOGOFF" },
426 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
427 { WINBINDD_PAM_CHAUTHTOK, winbindd_dual_pam_chauthtok, "PAM_CHAUTHTOK" },
428 { WINBINDD_CHECK_MACHACC, winbindd_dual_check_machine_acct, "CHECK_MACHACC" },
429 { WINBINDD_DUAL_SID2UID, winbindd_dual_sid2uid, "DUAL_SID2UID" },
430 { WINBINDD_DUAL_SID2GID, winbindd_dual_sid2gid, "DUAL_SID2GID" },
431 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
432 { WINBINDD_DUAL_SIDS2XIDS, winbindd_dual_sids2xids, "DUAL_SIDS2XIDS" },
433 #endif /* end DISABLED */
434 { WINBINDD_DUAL_UID2SID, winbindd_dual_uid2sid, "DUAL_UID2SID" },
435 { WINBINDD_DUAL_GID2SID, winbindd_dual_gid2sid, "DUAL_GID2SID" },
436 { WINBINDD_DUAL_UID2NAME, winbindd_dual_uid2name, "DUAL_UID2NAME" },
437 { WINBINDD_DUAL_NAME2UID, winbindd_dual_name2uid, "DUAL_NAME2UID" },
438 { WINBINDD_DUAL_GID2NAME, winbindd_dual_gid2name, "DUAL_GID2NAME" },
439 { WINBINDD_DUAL_NAME2GID, winbindd_dual_name2gid, "DUAL_NAME2GID" },
440 { WINBINDD_DUAL_SET_MAPPING, winbindd_dual_set_mapping, "DUAL_SET_MAPPING" },
441 { WINBINDD_DUAL_SET_HWM, winbindd_dual_set_hwm, "DUAL_SET_HWMS" },
442 { WINBINDD_DUAL_DUMP_MAPS, winbindd_dual_dump_maps, "DUAL_DUMP_MAPS" },
443 { WINBINDD_DUAL_USERINFO, winbindd_dual_userinfo, "DUAL_USERINFO" },
444 { WINBINDD_ALLOCATE_UID, winbindd_dual_allocate_uid, "ALLOCATE_UID" },
445 { WINBINDD_ALLOCATE_GID, winbindd_dual_allocate_gid, "ALLOCATE_GID" },
446 { WINBINDD_GETUSERDOMGROUPS, winbindd_dual_getuserdomgroups, "GETUSERDOMGROUPS" },
447 { WINBINDD_DUAL_GETSIDALIASES, winbindd_dual_getsidaliases, "GETSIDALIASES" },
448 { WINBINDD_CCACHE_NTLMAUTH, winbindd_dual_ccache_ntlm_auth, "CCACHE_NTLM_AUTH" },
451 { WINBINDD_NUM_CMDS, NULL, "NONE" }
454 static void child_process_request(struct winbindd_domain *domain,
455 struct winbindd_cli_state *state)
457 struct winbindd_child_dispatch_table *table;
459 /* Free response data - we may be interrupted and receive another
460 command before being able to send this data off. */
462 state->response.result = WINBINDD_ERROR;
463 state->response.length = sizeof(struct winbindd_response);
465 state->mem_ctx = talloc_init("winbind request");
466 if (state->mem_ctx == NULL)
469 /* Process command */
471 for (table = child_dispatch_table; table->fn; table++) {
472 if (state->request.cmd == table->cmd) {
473 DEBUG(10,("process_request: request fn %s\n",
474 table->winbindd_cmd_name ));
475 state->response.result = table->fn(domain, state);
481 DEBUG(10,("process_request: unknown request fn number %d\n",
482 (int)state->request.cmd ));
483 state->response.result = WINBINDD_ERROR;
486 talloc_destroy(state->mem_ctx);
489 void setup_domain_child(struct winbindd_domain *domain,
490 struct winbindd_child *child,
491 const char *explicit_logfile)
493 if (explicit_logfile != NULL) {
494 pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
495 dyn_LOGFILEBASE, explicit_logfile);
496 } else if (domain != NULL) {
497 pstr_sprintf(child->logfilename, "%s/log.wb-%s",
498 dyn_LOGFILEBASE, domain->name);
500 smb_panic("Internal error: domain == NULL && "
501 "explicit_logfile == NULL");
504 child->domain = domain;
507 struct winbindd_child *children = NULL;
509 void winbind_child_died(pid_t pid)
511 struct winbindd_child *child;
513 for (child = children; child != NULL; child = child->next) {
514 if (child->pid == pid) {
520 DEBUG(0, ("Unknown child %d died!\n", pid));
524 remove_fd_event(&child->event);
525 close(child->event.fd);
527 child->event.flags = 0;
530 schedule_async_request(child);
533 /* Ensure any negative cache entries with the netbios or realm names are removed. */
535 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
537 flush_negative_conn_cache_for_domain(domain->name);
538 if (*domain->alt_name) {
539 flush_negative_conn_cache_for_domain(domain->alt_name);
543 /* Set our domains as offline and forward the offline message to our children. */
545 void winbind_msg_offline(struct messaging_context *msg_ctx,
548 struct server_id server_id,
551 struct winbindd_child *child;
552 struct winbindd_domain *domain;
554 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
556 if (!lp_winbind_offline_logon()) {
557 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
561 /* Set our global state as offline. */
562 if (!set_global_winbindd_state_offline()) {
563 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
567 /* Set all our domains as offline. */
568 for (domain = domain_list(); domain; domain = domain->next) {
569 if (domain->internal) {
572 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
573 set_domain_offline(domain);
576 for (child = children; child != NULL; child = child->next) {
577 /* Don't send message to idmap child. We've already
579 if (!child->domain || (child == idmap_child())) {
583 /* Or internal domains (this should not be possible....) */
584 if (child->domain->internal) {
588 /* Each winbindd child should only process requests for one domain - make sure
589 we only set it online / offline for that domain. */
591 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
592 (unsigned int)child->pid, domain->name ));
594 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
596 (uint8 *)child->domain->name,
597 strlen(child->domain->name)+1);
601 /* Set our domains as online and forward the online message to our children. */
603 void winbind_msg_online(struct messaging_context *msg_ctx,
606 struct server_id server_id,
609 struct winbindd_child *child;
610 struct winbindd_domain *domain;
612 DEBUG(10,("winbind_msg_online: got online message.\n"));
614 if (!lp_winbind_offline_logon()) {
615 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
619 /* Set our global state as online. */
620 set_global_winbindd_state_online();
622 smb_nscd_flush_user_cache();
623 smb_nscd_flush_group_cache();
625 /* Set all our domains as online. */
626 for (domain = domain_list(); domain; domain = domain->next) {
627 if (domain->internal) {
630 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
632 winbindd_flush_negative_conn_cache(domain);
633 set_domain_online_request(domain);
635 /* Send an online message to the idmap child when our
636 primary domain comes back online */
638 if ( domain->primary ) {
639 struct winbindd_child *idmap = idmap_child();
641 if ( idmap->pid != 0 ) {
642 messaging_send_buf(msg_ctx,
643 pid_to_procid(idmap->pid),
645 (uint8 *)domain->name,
646 strlen(domain->name)+1);
652 for (child = children; child != NULL; child = child->next) {
653 /* Don't send message to idmap child. */
654 if (!child->domain || (child == idmap_child())) {
658 /* Or internal domains (this should not be possible....) */
659 if (child->domain->internal) {
663 /* Each winbindd child should only process requests for one domain - make sure
664 we only set it online / offline for that domain. */
666 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
667 (unsigned int)child->pid, child->domain->name ));
669 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
671 (uint8 *)child->domain->name,
672 strlen(child->domain->name)+1);
676 /* Forward the online/offline messages to our children. */
677 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
680 struct server_id server_id,
683 struct winbindd_child *child;
685 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
687 for (child = children; child != NULL; child = child->next) {
688 if (child->domain && child->domain->primary) {
689 DEBUG(10,("winbind_msg_onlinestatus: "
690 "sending message to pid %u of primary domain.\n",
691 (unsigned int)child->pid));
692 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
693 MSG_WINBIND_ONLINESTATUS,
701 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
704 struct server_id server_id,
707 struct winbindd_child *child;
709 DEBUG(10,("winbind_msg_dump_event_list received\n"));
711 dump_event_list(winbind_event_context());
713 for (child = children; child != NULL; child = child->next) {
715 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
716 (unsigned int)child->pid));
718 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
725 static void account_lockout_policy_handler(struct event_context *ctx,
726 struct timed_event *te,
727 const struct timeval *now,
730 struct winbindd_child *child =
731 (struct winbindd_child *)private_data;
732 TALLOC_CTX *mem_ctx = NULL;
733 struct winbindd_methods *methods;
734 SAM_UNK_INFO_12 lockout_policy;
737 DEBUG(10,("account_lockout_policy_handler called\n"));
739 if (child->lockout_policy_event) {
740 TALLOC_FREE(child->lockout_policy_event);
743 if ( !winbindd_can_contact_domain( child->domain ) ) {
744 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
745 "do not have an incoming trust to domain %s\n",
746 child->domain->name));
751 methods = child->domain->methods;
753 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
755 result = NT_STATUS_NO_MEMORY;
757 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
760 talloc_destroy(mem_ctx);
762 if (!NT_STATUS_IS_OK(result)) {
763 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
767 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
768 timeval_current_ofs(3600, 0),
769 "account_lockout_policy_handler",
770 account_lockout_policy_handler,
774 /* Deal with a request to go offline. */
776 static void child_msg_offline(struct messaging_context *msg,
779 struct server_id server_id,
782 struct winbindd_domain *domain;
783 const char *domainname = (const char *)data->data;
785 if (data->data == NULL || data->length == 0) {
789 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
791 if (!lp_winbind_offline_logon()) {
792 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
796 /* Mark the requested domain offline. */
798 for (domain = domain_list(); domain; domain = domain->next) {
799 if (domain->internal) {
802 if (strequal(domain->name, domainname)) {
803 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
804 set_domain_offline(domain);
809 /* Deal with a request to go online. */
811 static void child_msg_online(struct messaging_context *msg,
814 struct server_id server_id,
817 struct winbindd_domain *domain;
818 const char *domainname = (const char *)data->data;
820 if (data->data == NULL || data->length == 0) {
824 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
826 if (!lp_winbind_offline_logon()) {
827 DEBUG(10,("child_msg_online: rejecting online message.\n"));
831 /* Set our global state as online. */
832 set_global_winbindd_state_online();
834 /* Try and mark everything online - delete any negative cache entries
835 to force a reconnect now. */
837 for (domain = domain_list(); domain; domain = domain->next) {
838 if (domain->internal) {
841 if (strequal(domain->name, domainname)) {
842 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
843 winbindd_flush_negative_conn_cache(domain);
844 set_domain_online_request(domain);
849 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
851 struct winbindd_domain *domain;
854 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
855 get_global_winbindd_state_offline() ?
856 "Offline":"Online")) == NULL) {
860 for (domain = domain_list(); domain; domain = domain->next) {
861 if ((buf = talloc_asprintf_append(buf, "%s:%s ",
864 "Online":"Offline")) == NULL) {
869 buf = talloc_asprintf_append(buf, "\n");
871 DEBUG(5,("collect_onlinestatus: %s", buf));
876 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
879 struct server_id server_id,
884 struct server_id *sender;
886 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
892 sender = (struct server_id *)data->data;
894 mem_ctx = talloc_init("winbind_msg_onlinestatus");
895 if (mem_ctx == NULL) {
899 message = collect_onlinestatus(mem_ctx);
900 if (message == NULL) {
901 talloc_destroy(mem_ctx);
905 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
906 (uint8 *)message, strlen(message) + 1);
908 talloc_destroy(mem_ctx);
911 static void child_msg_dump_event_list(struct messaging_context *msg,
914 struct server_id server_id,
917 DEBUG(5,("child_msg_dump_event_list received\n"));
919 dump_event_list(winbind_event_context());
923 static BOOL fork_domain_child(struct winbindd_child *child)
926 struct winbindd_cli_state state;
927 struct winbindd_domain *domain;
929 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
930 DEBUG(0, ("Could not open child pipe: %s\n",
936 state.pid = sys_getpid();
941 child->pid = sys_fork();
943 if (child->pid == -1) {
944 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
948 if (child->pid != 0) {
951 child->next = child->prev = NULL;
952 DLIST_ADD(children, child);
953 child->event.fd = fdpair[1];
954 child->event.flags = 0;
955 child->requests = NULL;
956 add_fd_event(&child->event);
962 state.sock = fdpair[0];
965 /* tdb needs special fork handling */
966 if (tdb_reopen_all(1) == -1) {
967 DEBUG(0,("tdb_reopen_all failed.\n"));
971 close_conns_after_fork();
973 if (!override_logfile) {
974 lp_set_logfile(child->logfilename);
979 * For clustering, we need to re-init our ctdbd connection after the
982 if (!NT_STATUS_IS_OK(messaging_reinit(winbind_messaging_context())))
985 /* Don't handle the same messages as our parent. */
986 messaging_deregister(winbind_messaging_context(),
987 MSG_SMB_CONF_UPDATED, NULL);
988 messaging_deregister(winbind_messaging_context(),
990 messaging_deregister(winbind_messaging_context(),
991 MSG_WINBIND_OFFLINE, NULL);
992 messaging_deregister(winbind_messaging_context(),
993 MSG_WINBIND_ONLINE, NULL);
994 messaging_deregister(winbind_messaging_context(),
995 MSG_WINBIND_ONLINESTATUS, NULL);
996 messaging_deregister(winbind_messaging_context(),
997 MSG_DUMP_EVENT_LIST, NULL);
999 /* Handle online/offline messages. */
1000 messaging_register(winbind_messaging_context(), NULL,
1001 MSG_WINBIND_OFFLINE, child_msg_offline);
1002 messaging_register(winbind_messaging_context(), NULL,
1003 MSG_WINBIND_ONLINE, child_msg_online);
1004 messaging_register(winbind_messaging_context(), NULL,
1005 MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1006 messaging_register(winbind_messaging_context(), NULL,
1007 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1009 if ( child->domain ) {
1010 child->domain->startup = True;
1011 child->domain->startup_time = time(NULL);
1014 /* Ensure we have no pending check_online events other
1015 than one for this domain. */
1017 for (domain = domain_list(); domain; domain = domain->next) {
1018 if (domain != child->domain) {
1019 if (domain->check_online_event) {
1020 TALLOC_FREE(domain->check_online_event);
1025 /* Ensure we're not handling an event inherited from
1028 cancel_named_event(winbind_event_context(),
1029 "krb5_ticket_refresh_handler");
1031 /* We might be in the idmap child...*/
1032 if (child->domain && !(child->domain->internal) &&
1033 lp_winbind_offline_logon()) {
1035 set_domain_online_request(child->domain);
1037 child->lockout_policy_event = event_add_timed(
1038 winbind_event_context(), NULL, timeval_zero(),
1039 "account_lockout_policy_handler",
1040 account_lockout_policy_handler,
1052 /* free up any talloc memory */
1054 main_loop_TALLOC_FREE();
1056 run_events(winbind_event_context(), 0, NULL, NULL);
1060 if (child->domain && child->domain->startup &&
1061 (now.tv_sec > child->domain->startup_time + 30)) {
1062 /* No longer in "startup" mode. */
1063 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1064 child->domain->name ));
1065 child->domain->startup = False;
1068 tp = get_timed_events_timeout(winbind_event_context(), &t);
1070 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1071 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1074 /* Handle messages */
1076 message_dispatch(winbind_messaging_context());
1079 FD_SET(state.sock, &read_fds);
1081 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1084 DEBUG(11,("nothing is ready yet, continue\n"));
1088 if (ret == -1 && errno == EINTR) {
1089 /* We got a signal - continue. */
1093 if (ret == -1 && errno != EINTR) {
1094 DEBUG(0,("select error occured\n"));
1099 /* fetch a request from the main daemon */
1100 child_read_request(&state);
1102 if (state.finished) {
1103 /* we lost contact with our parent */
1107 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1109 ZERO_STRUCT(state.response);
1110 state.request.null_term = '\0';
1111 child_process_request(child->domain, &state);
1113 SAFE_FREE(state.request.extra_data.data);
1115 cache_store_response(sys_getpid(), &state.response);
1117 SAFE_FREE(state.response.extra_data.data);
1119 /* We just send the result code back, the result
1120 * structure needs to be fetched via the
1121 * winbindd_cache. Hmm. That needs fixing... */
1123 if (write_data(state.sock,
1124 (const char *)&state.response.result,
1125 sizeof(state.response.result)) !=
1126 sizeof(state.response.result)) {
1127 DEBUG(0, ("Could not write result\n"));