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 3 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, see <http://www.gnu.org/licenses/>.
24 * We fork a child per domain to be able to act non-blocking in the main
25 * winbind daemon. A domain controller thousands of miles away being being
26 * slow replying with a 10.000 user list should not hold up netlogon calls
27 * that can be handled locally.
34 #define DBGC_CLASS DBGC_WINBIND
36 extern BOOL override_logfile;
38 /* Read some data from a client connection */
40 static void child_read_request(struct winbindd_cli_state *state)
46 len = read_data(state->sock, (char *)&state->request,
47 sizeof(state->request));
49 if (len != sizeof(state->request)) {
50 DEBUG(len > 0 ? 0 : 3, ("Got invalid request length: %d\n", (int)len));
51 state->finished = True;
55 if (state->request.extra_len == 0) {
56 state->request.extra_data.data = NULL;
60 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
62 state->request.extra_data.data =
63 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
65 if (state->request.extra_data.data == NULL) {
66 DEBUG(0, ("malloc failed\n"));
67 state->finished = True;
71 /* Ensure null termination */
72 state->request.extra_data.data[state->request.extra_len] = '\0';
74 len = read_data(state->sock, state->request.extra_data.data,
75 state->request.extra_len);
77 if (len != state->request.extra_len) {
78 DEBUG(0, ("Could not read extra data\n"));
79 state->finished = True;
85 * Machinery for async requests sent to children. You set up a
86 * winbindd_request, select a child to query, and issue a async_request
87 * call. When the request is completed, the callback function you specified is
88 * called back with the private pointer you gave to async_request.
91 struct winbindd_async_request {
92 struct winbindd_async_request *next, *prev;
94 struct winbindd_child *child;
95 struct winbindd_request *request;
96 struct winbindd_response *response;
97 void (*continuation)(void *private_data, BOOL success);
98 struct timed_event *reply_timeout_event;
99 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
100 a restart of the child (child->pid != child_pid). */
104 static void async_main_request_sent(void *private_data, BOOL success);
105 static void async_request_sent(void *private_data, BOOL success);
106 static void async_reply_recv(void *private_data, BOOL success);
107 static void schedule_async_request(struct winbindd_child *child);
109 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
110 struct winbindd_request *request,
111 struct winbindd_response *response,
112 void (*continuation)(void *private_data, BOOL success),
115 struct winbindd_async_request *state;
117 SMB_ASSERT(continuation != NULL);
119 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
122 DEBUG(0, ("talloc failed\n"));
123 continuation(private_data, False);
127 state->mem_ctx = mem_ctx;
128 state->child = child;
129 state->request = request;
130 state->response = response;
131 state->continuation = continuation;
132 state->private_data = private_data;
134 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
136 schedule_async_request(child);
141 static void async_main_request_sent(void *private_data, BOOL success)
143 struct winbindd_async_request *state =
144 talloc_get_type_abort(private_data, struct winbindd_async_request);
147 DEBUG(5, ("Could not send async request\n"));
149 state->response->length = sizeof(struct winbindd_response);
150 state->response->result = WINBINDD_ERROR;
151 state->continuation(state->private_data, False);
155 if (state->request->extra_len == 0) {
156 async_request_sent(private_data, True);
160 setup_async_write(&state->child->event, state->request->extra_data.data,
161 state->request->extra_len,
162 async_request_sent, state);
165 /****************************************************************
166 Handler triggered if the child winbindd doesn't respond within
168 ****************************************************************/
170 static void async_request_timeout_handler(struct event_context *ctx,
171 struct timed_event *te,
172 const struct timeval *now,
175 struct winbindd_async_request *state =
176 talloc_get_type_abort(private_data, struct winbindd_async_request);
178 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
179 "Closing connection to it.\n",
182 /* Deal with the reply - set to error. */
183 async_reply_recv(private_data, False);
186 /**************************************************************
187 Common function called on both async send and recv fail.
188 Cleans up the child and schedules the next request.
189 **************************************************************/
191 static void async_request_fail(struct winbindd_async_request *state)
193 DLIST_REMOVE(state->child->requests, state);
195 TALLOC_FREE(state->reply_timeout_event);
197 SMB_ASSERT(state->child_pid != (pid_t)0);
199 /* If not already reaped, send kill signal to child. */
200 if (state->child->pid == state->child_pid) {
201 kill(state->child_pid, SIGTERM);
204 * Close the socket to the child.
206 winbind_child_died(state->child_pid);
209 state->response->length = sizeof(struct winbindd_response);
210 state->response->result = WINBINDD_ERROR;
211 state->continuation(state->private_data, False);
214 static void async_request_sent(void *private_data_data, BOOL success)
216 struct winbindd_async_request *state =
217 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
220 DEBUG(5, ("Could not send async request to child pid %u\n",
221 (unsigned int)state->child_pid ));
222 async_request_fail(state);
226 /* Request successfully sent to the child, setup the wait for reply */
228 setup_async_read(&state->child->event,
229 &state->response->result,
230 sizeof(state->response->result),
231 async_reply_recv, state);
234 * Set up a timeout of 300 seconds for the response.
235 * If we don't get it close the child socket and
239 state->reply_timeout_event = event_add_timed(winbind_event_context(),
241 timeval_current_ofs(300,0),
242 "async_request_timeout",
243 async_request_timeout_handler,
245 if (!state->reply_timeout_event) {
246 smb_panic("async_request_sent: failed to add timeout handler.\n");
250 static void async_reply_recv(void *private_data, BOOL success)
252 struct winbindd_async_request *state =
253 talloc_get_type_abort(private_data, struct winbindd_async_request);
254 struct winbindd_child *child = state->child;
256 TALLOC_FREE(state->reply_timeout_event);
258 state->response->length = sizeof(struct winbindd_response);
261 DEBUG(5, ("Could not receive async reply from child pid %u\n",
262 (unsigned int)state->child_pid ));
264 cache_cleanup_response(state->child_pid);
265 async_request_fail(state);
269 SMB_ASSERT(cache_retrieve_response(state->child_pid,
272 cache_cleanup_response(state->child_pid);
274 DLIST_REMOVE(child->requests, state);
276 schedule_async_request(child);
278 state->continuation(state->private_data, True);
281 static BOOL fork_domain_child(struct winbindd_child *child);
283 static void schedule_async_request(struct winbindd_child *child)
285 struct winbindd_async_request *request = child->requests;
287 if (request == NULL) {
291 if (child->event.flags != 0) {
295 if ((child->pid == 0) && (!fork_domain_child(child))) {
296 /* Cancel all outstanding requests */
298 while (request != NULL) {
299 /* request might be free'd in the continuation */
300 struct winbindd_async_request *next = request->next;
301 request->continuation(request->private_data, False);
307 /* Now we know who we're sending to - remember the pid. */
308 request->child_pid = child->pid;
310 setup_async_write(&child->event, request->request,
311 sizeof(*request->request),
312 async_main_request_sent, request);
317 struct domain_request_state {
319 struct winbindd_domain *domain;
320 struct winbindd_request *request;
321 struct winbindd_response *response;
322 void (*continuation)(void *private_data_data, BOOL success);
323 void *private_data_data;
326 static void domain_init_recv(void *private_data_data, BOOL success);
328 void async_domain_request(TALLOC_CTX *mem_ctx,
329 struct winbindd_domain *domain,
330 struct winbindd_request *request,
331 struct winbindd_response *response,
332 void (*continuation)(void *private_data_data, BOOL success),
333 void *private_data_data)
335 struct domain_request_state *state;
337 if (domain->initialized) {
338 async_request(mem_ctx, &domain->child, request, response,
339 continuation, private_data_data);
343 state = TALLOC_P(mem_ctx, struct domain_request_state);
345 DEBUG(0, ("talloc failed\n"));
346 continuation(private_data_data, False);
350 state->mem_ctx = mem_ctx;
351 state->domain = domain;
352 state->request = request;
353 state->response = response;
354 state->continuation = continuation;
355 state->private_data_data = private_data_data;
357 init_child_connection(domain, domain_init_recv, state);
360 static void domain_init_recv(void *private_data_data, BOOL success)
362 struct domain_request_state *state =
363 talloc_get_type_abort(private_data_data, struct domain_request_state);
366 DEBUG(5, ("Domain init returned an error\n"));
367 state->continuation(state->private_data_data, False);
371 async_request(state->mem_ctx, &state->domain->child,
372 state->request, state->response,
373 state->continuation, state->private_data_data);
376 static void recvfrom_child(void *private_data_data, BOOL success)
378 struct winbindd_cli_state *state =
379 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
380 enum winbindd_result result = state->response.result;
382 /* This is an optimization: The child has written directly to the
383 * response buffer. The request itself is still in pending state,
384 * state that in the result code. */
386 state->response.result = WINBINDD_PENDING;
388 if ((!success) || (result != WINBINDD_OK)) {
389 request_error(state);
396 void sendto_child(struct winbindd_cli_state *state,
397 struct winbindd_child *child)
399 async_request(state->mem_ctx, child, &state->request,
400 &state->response, recvfrom_child, state);
403 void sendto_domain(struct winbindd_cli_state *state,
404 struct winbindd_domain *domain)
406 async_domain_request(state->mem_ctx, domain,
407 &state->request, &state->response,
408 recvfrom_child, state);
412 struct winbindd_child_dispatch_table {
413 enum winbindd_cmd cmd;
414 enum winbindd_result (*fn)(struct winbindd_domain *domain,
415 struct winbindd_cli_state *state);
416 const char *winbindd_cmd_name;
419 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
421 { WINBINDD_LOOKUPSID, winbindd_dual_lookupsid, "LOOKUPSID" },
422 { WINBINDD_LOOKUPNAME, winbindd_dual_lookupname, "LOOKUPNAME" },
423 { WINBINDD_LOOKUPRIDS, winbindd_dual_lookuprids, "LOOKUPRIDS" },
424 { WINBINDD_LIST_TRUSTDOM, winbindd_dual_list_trusted_domains, "LIST_TRUSTDOM" },
425 { WINBINDD_INIT_CONNECTION, winbindd_dual_init_connection, "INIT_CONNECTION" },
426 { WINBINDD_GETDCNAME, winbindd_dual_getdcname, "GETDCNAME" },
427 { WINBINDD_DSGETDCNAME, winbindd_dual_dsgetdcname, "DSGETDCNAME" },
428 { WINBINDD_SHOW_SEQUENCE, winbindd_dual_show_sequence, "SHOW_SEQUENCE" },
429 { WINBINDD_PAM_AUTH, winbindd_dual_pam_auth, "PAM_AUTH" },
430 { WINBINDD_PAM_AUTH_CRAP, winbindd_dual_pam_auth_crap, "AUTH_CRAP" },
431 { WINBINDD_PAM_LOGOFF, winbindd_dual_pam_logoff, "PAM_LOGOFF" },
432 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
433 { WINBINDD_PAM_CHAUTHTOK, winbindd_dual_pam_chauthtok, "PAM_CHAUTHTOK" },
434 { WINBINDD_CHECK_MACHACC, winbindd_dual_check_machine_acct, "CHECK_MACHACC" },
435 { WINBINDD_DUAL_SID2UID, winbindd_dual_sid2uid, "DUAL_SID2UID" },
436 { WINBINDD_DUAL_SID2GID, winbindd_dual_sid2gid, "DUAL_SID2GID" },
437 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
438 { WINBINDD_DUAL_SIDS2XIDS, winbindd_dual_sids2xids, "DUAL_SIDS2XIDS" },
439 #endif /* end DISABLED */
440 { WINBINDD_DUAL_UID2SID, winbindd_dual_uid2sid, "DUAL_UID2SID" },
441 { WINBINDD_DUAL_GID2SID, winbindd_dual_gid2sid, "DUAL_GID2SID" },
442 { WINBINDD_DUAL_UID2NAME, winbindd_dual_uid2name, "DUAL_UID2NAME" },
443 { WINBINDD_DUAL_NAME2UID, winbindd_dual_name2uid, "DUAL_NAME2UID" },
444 { WINBINDD_DUAL_GID2NAME, winbindd_dual_gid2name, "DUAL_GID2NAME" },
445 { WINBINDD_DUAL_NAME2GID, winbindd_dual_name2gid, "DUAL_NAME2GID" },
446 { WINBINDD_DUAL_SET_MAPPING, winbindd_dual_set_mapping, "DUAL_SET_MAPPING" },
447 { WINBINDD_DUAL_SET_HWM, winbindd_dual_set_hwm, "DUAL_SET_HWMS" },
448 { WINBINDD_DUAL_DUMP_MAPS, winbindd_dual_dump_maps, "DUAL_DUMP_MAPS" },
449 { WINBINDD_DUAL_USERINFO, winbindd_dual_userinfo, "DUAL_USERINFO" },
450 { WINBINDD_ALLOCATE_UID, winbindd_dual_allocate_uid, "ALLOCATE_UID" },
451 { WINBINDD_ALLOCATE_GID, winbindd_dual_allocate_gid, "ALLOCATE_GID" },
452 { WINBINDD_GETUSERDOMGROUPS, winbindd_dual_getuserdomgroups, "GETUSERDOMGROUPS" },
453 { WINBINDD_DUAL_GETSIDALIASES, winbindd_dual_getsidaliases, "GETSIDALIASES" },
454 { WINBINDD_CCACHE_NTLMAUTH, winbindd_dual_ccache_ntlm_auth, "CCACHE_NTLM_AUTH" },
457 { WINBINDD_NUM_CMDS, NULL, "NONE" }
460 static void child_process_request(struct winbindd_domain *domain,
461 struct winbindd_cli_state *state)
463 struct winbindd_child_dispatch_table *table;
465 /* Free response data - we may be interrupted and receive another
466 command before being able to send this data off. */
468 state->response.result = WINBINDD_ERROR;
469 state->response.length = sizeof(struct winbindd_response);
471 /* as all requests in the child are sync, we can use talloc_tos() */
472 state->mem_ctx = talloc_tos();
474 /* Process command */
476 for (table = child_dispatch_table; table->fn; table++) {
477 if (state->request.cmd == table->cmd) {
478 DEBUG(10,("process_request: request fn %s\n",
479 table->winbindd_cmd_name ));
480 state->response.result = table->fn(domain, state);
486 DEBUG(10,("process_request: unknown request fn number %d\n",
487 (int)state->request.cmd ));
488 state->response.result = WINBINDD_ERROR;
492 void setup_domain_child(struct winbindd_domain *domain,
493 struct winbindd_child *child,
494 const char *explicit_logfile)
496 if (explicit_logfile != NULL) {
497 pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
498 dyn_LOGFILEBASE, explicit_logfile);
499 } else if (domain != NULL) {
500 pstr_sprintf(child->logfilename, "%s/log.wb-%s",
501 dyn_LOGFILEBASE, domain->name);
503 smb_panic("Internal error: domain == NULL && "
504 "explicit_logfile == NULL");
507 child->domain = domain;
510 struct winbindd_child *children = NULL;
512 void winbind_child_died(pid_t pid)
514 struct winbindd_child *child;
516 for (child = children; child != NULL; child = child->next) {
517 if (child->pid == pid) {
523 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
527 remove_fd_event(&child->event);
528 close(child->event.fd);
530 child->event.flags = 0;
533 schedule_async_request(child);
536 /* Ensure any negative cache entries with the netbios or realm names are removed. */
538 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
540 flush_negative_conn_cache_for_domain(domain->name);
541 if (*domain->alt_name) {
542 flush_negative_conn_cache_for_domain(domain->alt_name);
546 /* Set our domains as offline and forward the offline message to our children. */
548 void winbind_msg_offline(struct messaging_context *msg_ctx,
551 struct server_id server_id,
554 struct winbindd_child *child;
555 struct winbindd_domain *domain;
557 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
559 if (!lp_winbind_offline_logon()) {
560 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
564 /* Set our global state as offline. */
565 if (!set_global_winbindd_state_offline()) {
566 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
570 /* Set all our domains as offline. */
571 for (domain = domain_list(); domain; domain = domain->next) {
572 if (domain->internal) {
575 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
576 set_domain_offline(domain);
579 for (child = children; child != NULL; child = child->next) {
580 /* Don't send message to internal childs. We've already
582 if (!child->domain || winbindd_internal_child(child)) {
586 /* Or internal domains (this should not be possible....) */
587 if (child->domain->internal) {
591 /* Each winbindd child should only process requests for one domain - make sure
592 we only set it online / offline for that domain. */
594 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
595 (unsigned int)child->pid, domain->name ));
597 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
599 (uint8 *)child->domain->name,
600 strlen(child->domain->name)+1);
604 /* Set our domains as online and forward the online message to our children. */
606 void winbind_msg_online(struct messaging_context *msg_ctx,
609 struct server_id server_id,
612 struct winbindd_child *child;
613 struct winbindd_domain *domain;
615 DEBUG(10,("winbind_msg_online: got online message.\n"));
617 if (!lp_winbind_offline_logon()) {
618 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
622 /* Set our global state as online. */
623 set_global_winbindd_state_online();
625 smb_nscd_flush_user_cache();
626 smb_nscd_flush_group_cache();
628 /* Set all our domains as online. */
629 for (domain = domain_list(); domain; domain = domain->next) {
630 if (domain->internal) {
633 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
635 winbindd_flush_negative_conn_cache(domain);
636 set_domain_online_request(domain);
638 /* Send an online message to the idmap child when our
639 primary domain comes back online */
641 if ( domain->primary ) {
642 struct winbindd_child *idmap = idmap_child();
644 if ( idmap->pid != 0 ) {
645 messaging_send_buf(msg_ctx,
646 pid_to_procid(idmap->pid),
648 (uint8 *)domain->name,
649 strlen(domain->name)+1);
655 for (child = children; child != NULL; child = child->next) {
656 /* Don't send message to internal childs. */
657 if (!child->domain || winbindd_internal_child(child)) {
661 /* Or internal domains (this should not be possible....) */
662 if (child->domain->internal) {
666 /* Each winbindd child should only process requests for one domain - make sure
667 we only set it online / offline for that domain. */
669 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
670 (unsigned int)child->pid, child->domain->name ));
672 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
674 (uint8 *)child->domain->name,
675 strlen(child->domain->name)+1);
679 /* Forward the online/offline messages to our children. */
680 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
683 struct server_id server_id,
686 struct winbindd_child *child;
688 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
690 for (child = children; child != NULL; child = child->next) {
691 if (child->domain && child->domain->primary) {
692 DEBUG(10,("winbind_msg_onlinestatus: "
693 "sending message to pid %u of primary domain.\n",
694 (unsigned int)child->pid));
695 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
696 MSG_WINBIND_ONLINESTATUS,
704 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
707 struct server_id server_id,
710 struct winbindd_child *child;
712 DEBUG(10,("winbind_msg_dump_event_list received\n"));
714 dump_event_list(winbind_event_context());
716 for (child = children; child != NULL; child = child->next) {
718 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
719 (unsigned int)child->pid));
721 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
728 static void account_lockout_policy_handler(struct event_context *ctx,
729 struct timed_event *te,
730 const struct timeval *now,
733 struct winbindd_child *child =
734 (struct winbindd_child *)private_data;
735 TALLOC_CTX *mem_ctx = NULL;
736 struct winbindd_methods *methods;
737 SAM_UNK_INFO_12 lockout_policy;
740 DEBUG(10,("account_lockout_policy_handler called\n"));
742 TALLOC_FREE(child->lockout_policy_event);
744 if ( !winbindd_can_contact_domain( child->domain ) ) {
745 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
746 "do not have an incoming trust to domain %s\n",
747 child->domain->name));
752 methods = child->domain->methods;
754 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
756 result = NT_STATUS_NO_MEMORY;
758 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
760 TALLOC_FREE(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_buffer(buf, "%s:%s ",
864 "Online":"Offline")) == NULL) {
869 buf = talloc_asprintf_append_buffer(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 TALLOC_FREE(domain->check_online_event);
1023 /* Ensure we're not handling an event inherited from
1026 cancel_named_event(winbind_event_context(),
1027 "krb5_ticket_refresh_handler");
1029 /* We might be in the idmap child...*/
1030 if (child->domain && !(child->domain->internal) &&
1031 lp_winbind_offline_logon()) {
1033 set_domain_online_request(child->domain);
1035 child->lockout_policy_event = event_add_timed(
1036 winbind_event_context(), NULL, timeval_zero(),
1037 "account_lockout_policy_handler",
1038 account_lockout_policy_handler,
1049 TALLOC_CTX *frame = talloc_stackframe();
1051 run_events(winbind_event_context(), 0, NULL, NULL);
1055 if (child->domain && child->domain->startup &&
1056 (now.tv_sec > child->domain->startup_time + 30)) {
1057 /* No longer in "startup" mode. */
1058 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1059 child->domain->name ));
1060 child->domain->startup = False;
1063 tp = get_timed_events_timeout(winbind_event_context(), &t);
1065 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1066 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1069 /* Handle messages */
1071 message_dispatch(winbind_messaging_context());
1074 FD_SET(state.sock, &read_fds);
1076 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1079 DEBUG(11,("nothing is ready yet, continue\n"));
1084 if (ret == -1 && errno == EINTR) {
1085 /* We got a signal - continue. */
1090 if (ret == -1 && errno != EINTR) {
1091 DEBUG(0,("select error occured\n"));
1097 /* fetch a request from the main daemon */
1098 child_read_request(&state);
1100 if (state.finished) {
1101 /* we lost contact with our parent */
1105 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1107 ZERO_STRUCT(state.response);
1108 state.request.null_term = '\0';
1109 child_process_request(child->domain, &state);
1111 SAFE_FREE(state.request.extra_data.data);
1113 cache_store_response(sys_getpid(), &state.response);
1115 SAFE_FREE(state.response.extra_data.data);
1117 /* We just send the result code back, the result
1118 * structure needs to be fetched via the
1119 * winbindd_cache. Hmm. That needs fixing... */
1121 if (write_data(state.sock,
1122 (const char *)&state.response.result,
1123 sizeof(state.response.result)) !=
1124 sizeof(state.response.result)) {
1125 DEBUG(0, ("Could not write result\n"));