Fix winbindd crash in an unusual failure mode. Bug #5737. Based on original patch...
[Samba.git] / source / winbindd / winbindd_dual.c
blob9e9a244acfb35281ed4ab41c7e25e7a62757e268
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind child daemons
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.
30 #include "includes.h"
31 #include "winbindd.h"
33 #undef DBGC_CLASS
34 #define DBGC_CLASS DBGC_WINBIND
36 extern bool override_logfile;
37 extern struct winbindd_methods cache_methods;
39 /* Read some data from a client connection */
41 static void child_read_request(struct winbindd_cli_state *state)
43 NTSTATUS status;
45 /* Read data */
47 status = read_data(state->sock, (char *)&state->request,
48 sizeof(state->request));
50 if (!NT_STATUS_IS_OK(status)) {
51 DEBUG(3, ("child_read_request: read_data failed: %s\n",
52 nt_errstr(status)));
53 state->finished = True;
54 return;
57 if (state->request.extra_len == 0) {
58 state->request.extra_data.data = NULL;
59 return;
62 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
64 state->request.extra_data.data =
65 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
67 if (state->request.extra_data.data == NULL) {
68 DEBUG(0, ("malloc failed\n"));
69 state->finished = True;
70 return;
73 /* Ensure null termination */
74 state->request.extra_data.data[state->request.extra_len] = '\0';
76 status= read_data(state->sock, state->request.extra_data.data,
77 state->request.extra_len);
79 if (!NT_STATUS_IS_OK(status)) {
80 DEBUG(0, ("Could not read extra data: %s\n",
81 nt_errstr(status)));
82 state->finished = True;
83 return;
88 * Machinery for async requests sent to children. You set up a
89 * winbindd_request, select a child to query, and issue a async_request
90 * call. When the request is completed, the callback function you specified is
91 * called back with the private pointer you gave to async_request.
94 struct winbindd_async_request {
95 struct winbindd_async_request *next, *prev;
96 TALLOC_CTX *mem_ctx;
97 struct winbindd_child *child;
98 struct winbindd_request *request;
99 struct winbindd_response *response;
100 void (*continuation)(void *private_data, bool success);
101 struct timed_event *reply_timeout_event;
102 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
103 a restart of the child (child->pid != child_pid). */
104 void *private_data;
107 static void async_request_fail(struct winbindd_async_request *state);
108 static void async_main_request_sent(void *private_data, bool success);
109 static void async_request_sent(void *private_data, bool success);
110 static void async_reply_recv(void *private_data, bool success);
111 static void schedule_async_request(struct winbindd_child *child);
113 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
114 struct winbindd_request *request,
115 struct winbindd_response *response,
116 void (*continuation)(void *private_data, bool success),
117 void *private_data)
119 struct winbindd_async_request *state;
121 SMB_ASSERT(continuation != NULL);
123 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
125 if (state == NULL) {
126 DEBUG(0, ("talloc failed\n"));
127 continuation(private_data, False);
128 return;
131 state->mem_ctx = mem_ctx;
132 state->child = child;
133 state->reply_timeout_event = NULL;
134 state->request = request;
135 state->response = response;
136 state->continuation = continuation;
137 state->private_data = private_data;
139 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
141 schedule_async_request(child);
143 return;
146 static void async_main_request_sent(void *private_data, bool success)
148 struct winbindd_async_request *state =
149 talloc_get_type_abort(private_data, struct winbindd_async_request);
151 if (!success) {
152 DEBUG(5, ("Could not send async request\n"));
153 async_request_fail(state);
154 return;
157 if (state->request->extra_len == 0) {
158 async_request_sent(private_data, True);
159 return;
162 setup_async_write(&state->child->event, state->request->extra_data.data,
163 state->request->extra_len,
164 async_request_sent, state);
167 /****************************************************************
168 Handler triggered if the child winbindd doesn't respond within
169 a given timeout.
170 ****************************************************************/
172 static void async_request_timeout_handler(struct event_context *ctx,
173 struct timed_event *te,
174 const struct timeval *now,
175 void *private_data)
177 struct winbindd_async_request *state =
178 talloc_get_type_abort(private_data, struct winbindd_async_request);
180 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
181 "Closing connection to it.\n",
182 state->child_pid ));
184 /* Deal with the reply - set to error. */
185 async_reply_recv(private_data, False);
188 /**************************************************************
189 Common function called on both async send and recv fail.
190 Cleans up the child and schedules the next request.
191 **************************************************************/
193 static void async_request_fail(struct winbindd_async_request *state)
195 DLIST_REMOVE(state->child->requests, state);
197 TALLOC_FREE(state->reply_timeout_event);
199 /* If child exists and is not already reaped,
200 send kill signal to child. */
202 if ((state->child->pid != (pid_t)0) &&
203 (state->child->pid != (pid_t)-1) &&
204 (state->child->pid == state->child_pid)) {
205 kill(state->child_pid, SIGTERM);
208 * Close the socket to the child.
210 winbind_child_died(state->child_pid);
213 state->response->length = sizeof(struct winbindd_response);
214 state->response->result = WINBINDD_ERROR;
215 state->continuation(state->private_data, False);
218 static void async_request_sent(void *private_data_data, bool success)
220 struct winbindd_async_request *state =
221 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
223 if (!success) {
224 DEBUG(5, ("Could not send async request to child pid %u\n",
225 (unsigned int)state->child_pid ));
226 async_request_fail(state);
227 return;
230 /* Request successfully sent to the child, setup the wait for reply */
232 setup_async_read(&state->child->event,
233 &state->response->result,
234 sizeof(state->response->result),
235 async_reply_recv, state);
238 * Set up a timeout of 300 seconds for the response.
239 * If we don't get it close the child socket and
240 * report failure.
243 state->reply_timeout_event = event_add_timed(winbind_event_context(),
244 NULL,
245 timeval_current_ofs(300,0),
246 "async_request_timeout",
247 async_request_timeout_handler,
248 state);
249 if (!state->reply_timeout_event) {
250 smb_panic("async_request_sent: failed to add timeout handler.\n");
254 static void async_reply_recv(void *private_data, bool success)
256 struct winbindd_async_request *state =
257 talloc_get_type_abort(private_data, struct winbindd_async_request);
258 struct winbindd_child *child = state->child;
260 TALLOC_FREE(state->reply_timeout_event);
262 state->response->length = sizeof(struct winbindd_response);
264 if (!success) {
265 DEBUG(5, ("Could not receive async reply from child pid %u\n",
266 (unsigned int)state->child_pid ));
268 cache_cleanup_response(state->child_pid);
269 async_request_fail(state);
270 return;
273 SMB_ASSERT(cache_retrieve_response(state->child_pid,
274 state->response));
276 cache_cleanup_response(state->child_pid);
278 DLIST_REMOVE(child->requests, state);
280 schedule_async_request(child);
282 state->continuation(state->private_data, True);
285 static bool fork_domain_child(struct winbindd_child *child);
287 static void schedule_async_request(struct winbindd_child *child)
289 struct winbindd_async_request *request = child->requests;
291 if (request == NULL) {
292 return;
295 if (child->event.flags != 0) {
296 return; /* Busy */
299 if ((child->pid == 0) && (!fork_domain_child(child))) {
300 /* fork_domain_child failed.
301 Cancel all outstanding requests */
303 while (request != NULL) {
304 /* request might be free'd in the continuation */
305 struct winbindd_async_request *next = request->next;
307 async_request_fail(request);
308 request = next;
310 return;
313 /* Now we know who we're sending to - remember the pid. */
314 request->child_pid = child->pid;
316 setup_async_write(&child->event, request->request,
317 sizeof(*request->request),
318 async_main_request_sent, request);
320 return;
323 struct domain_request_state {
324 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;
332 static void domain_init_recv(void *private_data_data, bool success);
334 void async_domain_request(TALLOC_CTX *mem_ctx,
335 struct winbindd_domain *domain,
336 struct winbindd_request *request,
337 struct winbindd_response *response,
338 void (*continuation)(void *private_data_data, bool success),
339 void *private_data_data)
341 struct domain_request_state *state;
343 if (domain->initialized) {
344 async_request(mem_ctx, &domain->child, request, response,
345 continuation, private_data_data);
346 return;
349 state = TALLOC_P(mem_ctx, struct domain_request_state);
350 if (state == NULL) {
351 DEBUG(0, ("talloc failed\n"));
352 continuation(private_data_data, False);
353 return;
356 state->mem_ctx = mem_ctx;
357 state->domain = domain;
358 state->request = request;
359 state->response = response;
360 state->continuation = continuation;
361 state->private_data_data = private_data_data;
363 init_child_connection(domain, domain_init_recv, state);
366 static void domain_init_recv(void *private_data_data, bool success)
368 struct domain_request_state *state =
369 talloc_get_type_abort(private_data_data, struct domain_request_state);
371 if (!success) {
372 DEBUG(5, ("Domain init returned an error\n"));
373 state->continuation(state->private_data_data, False);
374 return;
377 async_request(state->mem_ctx, &state->domain->child,
378 state->request, state->response,
379 state->continuation, state->private_data_data);
382 static void recvfrom_child(void *private_data_data, bool success)
384 struct winbindd_cli_state *state =
385 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
386 enum winbindd_result result = state->response.result;
388 /* This is an optimization: The child has written directly to the
389 * response buffer. The request itself is still in pending state,
390 * state that in the result code. */
392 state->response.result = WINBINDD_PENDING;
394 if ((!success) || (result != WINBINDD_OK)) {
395 request_error(state);
396 return;
399 request_ok(state);
402 void sendto_child(struct winbindd_cli_state *state,
403 struct winbindd_child *child)
405 async_request(state->mem_ctx, child, &state->request,
406 &state->response, recvfrom_child, state);
409 void sendto_domain(struct winbindd_cli_state *state,
410 struct winbindd_domain *domain)
412 async_domain_request(state->mem_ctx, domain,
413 &state->request, &state->response,
414 recvfrom_child, state);
417 static void child_process_request(struct winbindd_child *child,
418 struct winbindd_cli_state *state)
420 struct winbindd_domain *domain = child->domain;
421 const struct winbindd_child_dispatch_table *table = child->table;
423 /* Free response data - we may be interrupted and receive another
424 command before being able to send this data off. */
426 state->response.result = WINBINDD_ERROR;
427 state->response.length = sizeof(struct winbindd_response);
429 /* as all requests in the child are sync, we can use talloc_tos() */
430 state->mem_ctx = talloc_tos();
432 /* Process command */
434 for (; table->name; table++) {
435 if (state->request.cmd == table->struct_cmd) {
436 DEBUG(10,("child_process_request: request fn %s\n",
437 table->name));
438 state->response.result = table->struct_fn(domain, state);
439 return;
443 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
444 (int)state->request.cmd));
445 state->response.result = WINBINDD_ERROR;
448 void setup_child(struct winbindd_child *child,
449 const struct winbindd_child_dispatch_table *table,
450 const char *logprefix,
451 const char *logname)
453 if (logprefix && logname) {
454 if (asprintf(&child->logfilename, "%s/%s-%s",
455 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
456 smb_panic("Internal error: asprintf failed");
458 } else {
459 smb_panic("Internal error: logprefix == NULL && "
460 "logname == NULL");
463 child->domain = NULL;
464 child->table = table;
467 struct winbindd_child *children = NULL;
469 void winbind_child_died(pid_t pid)
471 struct winbindd_child *child;
473 for (child = children; child != NULL; child = child->next) {
474 if (child->pid == pid) {
475 break;
479 if (child == NULL) {
480 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
481 return;
484 /* This will be re-added in fork_domain_child() */
486 DLIST_REMOVE(children, child);
488 remove_fd_event(&child->event);
489 close(child->event.fd);
490 child->event.fd = 0;
491 child->event.flags = 0;
492 child->pid = 0;
494 schedule_async_request(child);
497 /* Ensure any negative cache entries with the netbios or realm names are removed. */
499 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
501 flush_negative_conn_cache_for_domain(domain->name);
502 if (*domain->alt_name) {
503 flush_negative_conn_cache_for_domain(domain->alt_name);
508 * Parent winbindd process sets its own debug level first and then
509 * sends a message to all the winbindd children to adjust their debug
510 * level to that of parents.
513 void winbind_msg_debug(struct messaging_context *msg_ctx,
514 void *private_data,
515 uint32_t msg_type,
516 struct server_id server_id,
517 DATA_BLOB *data)
519 struct winbindd_child *child;
521 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
523 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
525 for (child = children; child != NULL; child = child->next) {
527 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
528 (unsigned int)child->pid));
530 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
531 MSG_DEBUG,
532 data->data,
533 strlen((char *) data->data) + 1);
537 /* Set our domains as offline and forward the offline message to our children. */
539 void winbind_msg_offline(struct messaging_context *msg_ctx,
540 void *private_data,
541 uint32_t msg_type,
542 struct server_id server_id,
543 DATA_BLOB *data)
545 struct winbindd_child *child;
546 struct winbindd_domain *domain;
548 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
550 if (!lp_winbind_offline_logon()) {
551 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
552 return;
555 /* Set our global state as offline. */
556 if (!set_global_winbindd_state_offline()) {
557 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
558 return;
561 /* Set all our domains as offline. */
562 for (domain = domain_list(); domain; domain = domain->next) {
563 if (domain->internal) {
564 continue;
566 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
567 set_domain_offline(domain);
570 for (child = children; child != NULL; child = child->next) {
571 /* Don't send message to internal childs. We've already
572 done so above. */
573 if (!child->domain || winbindd_internal_child(child)) {
574 continue;
577 /* Or internal domains (this should not be possible....) */
578 if (child->domain->internal) {
579 continue;
582 /* Each winbindd child should only process requests for one domain - make sure
583 we only set it online / offline for that domain. */
585 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
586 (unsigned int)child->pid, domain->name ));
588 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
589 MSG_WINBIND_OFFLINE,
590 (uint8 *)child->domain->name,
591 strlen(child->domain->name)+1);
595 /* Set our domains as online and forward the online message to our children. */
597 void winbind_msg_online(struct messaging_context *msg_ctx,
598 void *private_data,
599 uint32_t msg_type,
600 struct server_id server_id,
601 DATA_BLOB *data)
603 struct winbindd_child *child;
604 struct winbindd_domain *domain;
606 DEBUG(10,("winbind_msg_online: got online message.\n"));
608 if (!lp_winbind_offline_logon()) {
609 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
610 return;
613 /* Set our global state as online. */
614 set_global_winbindd_state_online();
616 smb_nscd_flush_user_cache();
617 smb_nscd_flush_group_cache();
619 /* Set all our domains as online. */
620 for (domain = domain_list(); domain; domain = domain->next) {
621 if (domain->internal) {
622 continue;
624 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
626 winbindd_flush_negative_conn_cache(domain);
627 set_domain_online_request(domain);
629 /* Send an online message to the idmap child when our
630 primary domain comes back online */
632 if ( domain->primary ) {
633 struct winbindd_child *idmap = idmap_child();
635 if ( idmap->pid != 0 ) {
636 messaging_send_buf(msg_ctx,
637 pid_to_procid(idmap->pid),
638 MSG_WINBIND_ONLINE,
639 (uint8 *)domain->name,
640 strlen(domain->name)+1);
646 for (child = children; child != NULL; child = child->next) {
647 /* Don't send message to internal childs. */
648 if (!child->domain || winbindd_internal_child(child)) {
649 continue;
652 /* Or internal domains (this should not be possible....) */
653 if (child->domain->internal) {
654 continue;
657 /* Each winbindd child should only process requests for one domain - make sure
658 we only set it online / offline for that domain. */
660 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
661 (unsigned int)child->pid, child->domain->name ));
663 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
664 MSG_WINBIND_ONLINE,
665 (uint8 *)child->domain->name,
666 strlen(child->domain->name)+1);
670 /* Forward the online/offline messages to our children. */
671 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
672 void *private_data,
673 uint32_t msg_type,
674 struct server_id server_id,
675 DATA_BLOB *data)
677 struct winbindd_child *child;
679 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
681 for (child = children; child != NULL; child = child->next) {
682 if (child->domain && child->domain->primary) {
683 DEBUG(10,("winbind_msg_onlinestatus: "
684 "sending message to pid %u of primary domain.\n",
685 (unsigned int)child->pid));
686 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
687 MSG_WINBIND_ONLINESTATUS,
688 (uint8 *)data->data,
689 data->length);
690 break;
695 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
696 void *private_data,
697 uint32_t msg_type,
698 struct server_id server_id,
699 DATA_BLOB *data)
701 struct winbindd_child *child;
703 DEBUG(10,("winbind_msg_dump_event_list received\n"));
705 dump_event_list(winbind_event_context());
707 for (child = children; child != NULL; child = child->next) {
709 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
710 (unsigned int)child->pid));
712 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
713 MSG_DUMP_EVENT_LIST,
714 NULL, 0);
719 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
720 void *private_data,
721 uint32_t msg_type,
722 struct server_id server_id,
723 DATA_BLOB *data)
725 TALLOC_CTX *mem_ctx;
726 const char *message = NULL;
727 struct server_id *sender = NULL;
728 const char *domain = NULL;
729 char *s = NULL;
730 NTSTATUS status;
731 struct winbindd_domain *dom = NULL;
733 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
735 if (!data || !data->data) {
736 return;
739 if (data->length < sizeof(struct server_id)) {
740 return;
743 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
744 if (!mem_ctx) {
745 return;
748 sender = (struct server_id *)data->data;
749 if (data->length > sizeof(struct server_id)) {
750 domain = (const char *)data->data+sizeof(struct server_id);
753 if (domain) {
755 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
756 domain));
758 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
759 find_domain_from_name_noinit(domain));
760 if (!message) {
761 talloc_destroy(mem_ctx);
762 return;
765 messaging_send_buf(msg_ctx, *sender,
766 MSG_WINBIND_DUMP_DOMAIN_LIST,
767 (uint8_t *)message, strlen(message) + 1);
769 talloc_destroy(mem_ctx);
771 return;
774 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
776 for (dom = domain_list(); dom; dom=dom->next) {
777 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
778 if (!message) {
779 talloc_destroy(mem_ctx);
780 return;
783 s = talloc_asprintf_append(s, "%s\n", message);
784 if (!s) {
785 talloc_destroy(mem_ctx);
786 return;
790 status = messaging_send_buf(msg_ctx, *sender,
791 MSG_WINBIND_DUMP_DOMAIN_LIST,
792 (uint8_t *)s, strlen(s) + 1);
793 if (!NT_STATUS_IS_OK(status)) {
794 DEBUG(0,("failed to send message: %s\n",
795 nt_errstr(status)));
798 talloc_destroy(mem_ctx);
801 static void account_lockout_policy_handler(struct event_context *ctx,
802 struct timed_event *te,
803 const struct timeval *now,
804 void *private_data)
806 struct winbindd_child *child =
807 (struct winbindd_child *)private_data;
808 TALLOC_CTX *mem_ctx = NULL;
809 struct winbindd_methods *methods;
810 struct samr_DomInfo12 lockout_policy;
811 NTSTATUS result;
813 DEBUG(10,("account_lockout_policy_handler called\n"));
815 TALLOC_FREE(child->lockout_policy_event);
817 if ( !winbindd_can_contact_domain( child->domain ) ) {
818 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
819 "do not have an incoming trust to domain %s\n",
820 child->domain->name));
822 return;
825 methods = child->domain->methods;
827 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
828 if (!mem_ctx) {
829 result = NT_STATUS_NO_MEMORY;
830 } else {
831 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
833 TALLOC_FREE(mem_ctx);
835 if (!NT_STATUS_IS_OK(result)) {
836 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
837 nt_errstr(result)));
840 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
841 timeval_current_ofs(3600, 0),
842 "account_lockout_policy_handler",
843 account_lockout_policy_handler,
844 child);
847 /* Deal with a request to go offline. */
849 static void child_msg_offline(struct messaging_context *msg,
850 void *private_data,
851 uint32_t msg_type,
852 struct server_id server_id,
853 DATA_BLOB *data)
855 struct winbindd_domain *domain;
856 const char *domainname = (const char *)data->data;
858 if (data->data == NULL || data->length == 0) {
859 return;
862 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
864 if (!lp_winbind_offline_logon()) {
865 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
866 return;
869 /* Mark the requested domain offline. */
871 for (domain = domain_list(); domain; domain = domain->next) {
872 if (domain->internal) {
873 continue;
875 if (strequal(domain->name, domainname)) {
876 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
877 set_domain_offline(domain);
882 /* Deal with a request to go online. */
884 static void child_msg_online(struct messaging_context *msg,
885 void *private_data,
886 uint32_t msg_type,
887 struct server_id server_id,
888 DATA_BLOB *data)
890 struct winbindd_domain *domain;
891 const char *domainname = (const char *)data->data;
893 if (data->data == NULL || data->length == 0) {
894 return;
897 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
899 if (!lp_winbind_offline_logon()) {
900 DEBUG(10,("child_msg_online: rejecting online message.\n"));
901 return;
904 /* Set our global state as online. */
905 set_global_winbindd_state_online();
907 /* Try and mark everything online - delete any negative cache entries
908 to force a reconnect now. */
910 for (domain = domain_list(); domain; domain = domain->next) {
911 if (domain->internal) {
912 continue;
914 if (strequal(domain->name, domainname)) {
915 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
916 winbindd_flush_negative_conn_cache(domain);
917 set_domain_online_request(domain);
922 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
924 struct winbindd_domain *domain;
925 char *buf = NULL;
927 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
928 get_global_winbindd_state_offline() ?
929 "Offline":"Online")) == NULL) {
930 return NULL;
933 for (domain = domain_list(); domain; domain = domain->next) {
934 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
935 domain->name,
936 domain->online ?
937 "Online":"Offline")) == NULL) {
938 return NULL;
942 buf = talloc_asprintf_append_buffer(buf, "\n");
944 DEBUG(5,("collect_onlinestatus: %s", buf));
946 return buf;
949 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
950 void *private_data,
951 uint32_t msg_type,
952 struct server_id server_id,
953 DATA_BLOB *data)
955 TALLOC_CTX *mem_ctx;
956 const char *message;
957 struct server_id *sender;
959 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
961 if (!data->data) {
962 return;
965 sender = (struct server_id *)data->data;
967 mem_ctx = talloc_init("winbind_msg_onlinestatus");
968 if (mem_ctx == NULL) {
969 return;
972 message = collect_onlinestatus(mem_ctx);
973 if (message == NULL) {
974 talloc_destroy(mem_ctx);
975 return;
978 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
979 (uint8 *)message, strlen(message) + 1);
981 talloc_destroy(mem_ctx);
984 static void child_msg_dump_event_list(struct messaging_context *msg,
985 void *private_data,
986 uint32_t msg_type,
987 struct server_id server_id,
988 DATA_BLOB *data)
990 DEBUG(5,("child_msg_dump_event_list received\n"));
992 dump_event_list(winbind_event_context());
996 static bool fork_domain_child(struct winbindd_child *child)
998 int fdpair[2];
999 struct winbindd_cli_state state;
1000 struct winbindd_domain *domain;
1001 struct winbindd_domain *primary_domain = NULL;
1003 if (child->domain) {
1004 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1005 child->domain->name));
1006 } else {
1007 DEBUG(10, ("fork_domain_child called without domain.\n"));
1010 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1011 DEBUG(0, ("Could not open child pipe: %s\n",
1012 strerror(errno)));
1013 return False;
1016 ZERO_STRUCT(state);
1017 state.pid = sys_getpid();
1019 child->pid = sys_fork();
1021 if (child->pid == -1) {
1022 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1023 return False;
1026 if (child->pid != 0) {
1027 /* Parent */
1028 close(fdpair[0]);
1029 child->next = child->prev = NULL;
1030 DLIST_ADD(children, child);
1031 child->event.fd = fdpair[1];
1032 child->event.flags = 0;
1033 child->requests = NULL;
1034 add_fd_event(&child->event);
1035 return True;
1038 /* Child */
1040 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1042 /* Stop zombies in children */
1043 CatchChild();
1045 state.sock = fdpair[0];
1046 close(fdpair[1]);
1048 if (!reinit_after_fork(winbind_messaging_context(), true)) {
1049 DEBUG(0,("reinit_after_fork() failed\n"));
1050 _exit(0);
1053 close_conns_after_fork();
1055 if (!override_logfile) {
1056 lp_set_logfile(child->logfilename);
1057 reopen_logs();
1061 * For clustering, we need to re-init our ctdbd connection after the
1062 * fork
1064 if (!NT_STATUS_IS_OK(messaging_reinit(winbind_messaging_context())))
1065 exit(1);
1067 /* Don't handle the same messages as our parent. */
1068 messaging_deregister(winbind_messaging_context(),
1069 MSG_SMB_CONF_UPDATED, NULL);
1070 messaging_deregister(winbind_messaging_context(),
1071 MSG_SHUTDOWN, NULL);
1072 messaging_deregister(winbind_messaging_context(),
1073 MSG_WINBIND_OFFLINE, NULL);
1074 messaging_deregister(winbind_messaging_context(),
1075 MSG_WINBIND_ONLINE, NULL);
1076 messaging_deregister(winbind_messaging_context(),
1077 MSG_WINBIND_ONLINESTATUS, NULL);
1078 messaging_deregister(winbind_messaging_context(),
1079 MSG_DUMP_EVENT_LIST, NULL);
1080 messaging_deregister(winbind_messaging_context(),
1081 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1082 messaging_deregister(winbind_messaging_context(),
1083 MSG_DEBUG, NULL);
1085 /* Handle online/offline messages. */
1086 messaging_register(winbind_messaging_context(), NULL,
1087 MSG_WINBIND_OFFLINE, child_msg_offline);
1088 messaging_register(winbind_messaging_context(), NULL,
1089 MSG_WINBIND_ONLINE, child_msg_online);
1090 messaging_register(winbind_messaging_context(), NULL,
1091 MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1092 messaging_register(winbind_messaging_context(), NULL,
1093 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1094 messaging_register(winbind_messaging_context(), NULL,
1095 MSG_DEBUG, debug_message);
1097 if ( child->domain ) {
1098 child->domain->startup = True;
1099 child->domain->startup_time = time(NULL);
1102 /* Ensure we have no pending check_online events other
1103 than one for this domain or the primary domain. */
1105 for (domain = domain_list(); domain; domain = domain->next) {
1106 if (domain->primary) {
1107 primary_domain = domain;
1109 if ((domain != child->domain) && !domain->primary) {
1110 TALLOC_FREE(domain->check_online_event);
1114 if (primary_domain == NULL) {
1115 smb_panic("no primary domain found");
1118 /* Ensure we're not handling an event inherited from
1119 our parent. */
1121 cancel_named_event(winbind_event_context(),
1122 "krb5_ticket_refresh_handler");
1124 /* We might be in the idmap child...*/
1125 if (child->domain && !(child->domain->internal) &&
1126 lp_winbind_offline_logon()) {
1128 set_domain_online_request(child->domain);
1130 if (primary_domain != child->domain) {
1131 /* We need to talk to the primary
1132 * domain as well as the trusted
1133 * domain inside a trusted domain
1134 * child.
1135 * See the code in :
1136 * set_dc_type_and_flags_trustinfo()
1137 * for details.
1139 set_domain_online_request(primary_domain);
1142 child->lockout_policy_event = event_add_timed(
1143 winbind_event_context(), NULL, timeval_zero(),
1144 "account_lockout_policy_handler",
1145 account_lockout_policy_handler,
1146 child);
1149 while (1) {
1151 int ret;
1152 fd_set read_fds;
1153 struct timeval t;
1154 struct timeval *tp;
1155 struct timeval now;
1156 TALLOC_CTX *frame = talloc_stackframe();
1158 /* check for signals */
1159 winbind_check_sigterm(false);
1160 winbind_check_sighup(override_logfile ? NULL :
1161 child->logfilename);
1163 run_events(winbind_event_context(), 0, NULL, NULL);
1165 GetTimeOfDay(&now);
1167 if (child->domain && child->domain->startup &&
1168 (now.tv_sec > child->domain->startup_time + 30)) {
1169 /* No longer in "startup" mode. */
1170 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1171 child->domain->name ));
1172 child->domain->startup = False;
1175 tp = get_timed_events_timeout(winbind_event_context(), &t);
1176 if (tp) {
1177 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1178 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1181 /* Handle messages */
1183 message_dispatch(winbind_messaging_context());
1185 FD_ZERO(&read_fds);
1186 FD_SET(state.sock, &read_fds);
1188 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1190 if (ret == 0) {
1191 DEBUG(11,("nothing is ready yet, continue\n"));
1192 TALLOC_FREE(frame);
1193 continue;
1196 if (ret == -1 && errno == EINTR) {
1197 /* We got a signal - continue. */
1198 TALLOC_FREE(frame);
1199 continue;
1202 if (ret == -1 && errno != EINTR) {
1203 DEBUG(0,("select error occured\n"));
1204 TALLOC_FREE(frame);
1205 perror("select");
1206 return False;
1209 /* fetch a request from the main daemon */
1210 child_read_request(&state);
1212 if (state.finished) {
1213 /* we lost contact with our parent */
1214 exit(0);
1217 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1219 ZERO_STRUCT(state.response);
1220 state.request.null_term = '\0';
1221 child_process_request(child, &state);
1223 SAFE_FREE(state.request.extra_data.data);
1225 cache_store_response(sys_getpid(), &state.response);
1227 SAFE_FREE(state.response.extra_data.data);
1229 /* We just send the result code back, the result
1230 * structure needs to be fetched via the
1231 * winbindd_cache. Hmm. That needs fixing... */
1233 if (write_data(state.sock,
1234 (const char *)&state.response.result,
1235 sizeof(state.response.result)) !=
1236 sizeof(state.response.result)) {
1237 DEBUG(0, ("Could not write result\n"));
1238 exit(1);
1240 TALLOC_FREE(frame);