[Bug 6228] SMBC_open_ctx failure due to path resolve failure doesn't set errno
[Samba.git] / source / winbindd / winbindd_dual.c
blob49978c223e858daa1ad36ddea8ef43110141593d
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 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
124 (int)child->pid,
125 (child->domain != NULL) ? child->domain->name : "''"));
127 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
129 if (state == NULL) {
130 DEBUG(0, ("talloc failed\n"));
131 continuation(private_data, False);
132 return;
135 state->mem_ctx = mem_ctx;
136 state->child = child;
137 state->reply_timeout_event = NULL;
138 state->request = request;
139 state->response = response;
140 state->continuation = continuation;
141 state->private_data = private_data;
143 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
145 schedule_async_request(child);
147 return;
150 static void async_main_request_sent(void *private_data, bool success)
152 struct winbindd_async_request *state =
153 talloc_get_type_abort(private_data, struct winbindd_async_request);
155 if (!success) {
156 DEBUG(5, ("Could not send async request\n"));
157 async_request_fail(state);
158 return;
161 if (state->request->extra_len == 0) {
162 async_request_sent(private_data, True);
163 return;
166 setup_async_write(&state->child->event, state->request->extra_data.data,
167 state->request->extra_len,
168 async_request_sent, state);
171 /****************************************************************
172 Handler triggered if the child winbindd doesn't respond within
173 a given timeout.
174 ****************************************************************/
176 static void async_request_timeout_handler(struct event_context *ctx,
177 struct timed_event *te,
178 const struct timeval *now,
179 void *private_data)
181 struct winbindd_async_request *state =
182 talloc_get_type_abort(private_data, struct winbindd_async_request);
184 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
185 "Closing connection to it.\n",
186 state->child_pid ));
188 /* Deal with the reply - set to error. */
189 async_reply_recv(private_data, False);
192 /**************************************************************
193 Common function called on both async send and recv fail.
194 Cleans up the child and schedules the next request.
195 **************************************************************/
197 static void async_request_fail(struct winbindd_async_request *state)
199 DLIST_REMOVE(state->child->requests, state);
201 TALLOC_FREE(state->reply_timeout_event);
203 /* If child exists and is not already reaped,
204 send kill signal to child. */
206 if ((state->child->pid != (pid_t)0) &&
207 (state->child->pid != (pid_t)-1) &&
208 (state->child->pid == state->child_pid)) {
209 kill(state->child_pid, SIGTERM);
212 * Close the socket to the child.
214 winbind_child_died(state->child_pid);
217 state->response->length = sizeof(struct winbindd_response);
218 state->response->result = WINBINDD_ERROR;
219 state->continuation(state->private_data, False);
222 static void async_request_sent(void *private_data_data, bool success)
224 struct winbindd_async_request *state =
225 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
227 if (!success) {
228 DEBUG(5, ("Could not send async request to child pid %u\n",
229 (unsigned int)state->child_pid ));
230 async_request_fail(state);
231 return;
234 /* Request successfully sent to the child, setup the wait for reply */
236 setup_async_read(&state->child->event,
237 &state->response->result,
238 sizeof(state->response->result),
239 async_reply_recv, state);
242 * Set up a timeout of 300 seconds for the response.
243 * If we don't get it close the child socket and
244 * report failure.
247 state->reply_timeout_event = event_add_timed(winbind_event_context(),
248 NULL,
249 timeval_current_ofs(300,0),
250 "async_request_timeout",
251 async_request_timeout_handler,
252 state);
253 if (!state->reply_timeout_event) {
254 smb_panic("async_request_sent: failed to add timeout handler.\n");
258 static void async_reply_recv(void *private_data, bool success)
260 struct winbindd_async_request *state =
261 talloc_get_type_abort(private_data, struct winbindd_async_request);
262 struct winbindd_child *child = state->child;
264 TALLOC_FREE(state->reply_timeout_event);
266 state->response->length = sizeof(struct winbindd_response);
268 if (!success) {
269 DEBUG(5, ("Could not receive async reply from child pid %u\n",
270 (unsigned int)state->child_pid ));
272 cache_cleanup_response(state->child_pid);
273 async_request_fail(state);
274 return;
277 SMB_ASSERT(cache_retrieve_response(state->child_pid,
278 state->response));
280 cache_cleanup_response(state->child_pid);
282 DLIST_REMOVE(child->requests, state);
284 schedule_async_request(child);
286 state->continuation(state->private_data, True);
289 static bool fork_domain_child(struct winbindd_child *child);
291 static void schedule_async_request(struct winbindd_child *child)
293 struct winbindd_async_request *request = child->requests;
295 if (request == NULL) {
296 return;
299 if (child->event.flags != 0) {
300 return; /* Busy */
304 * This may be a reschedule, so we might
305 * have an existing timeout event pending on
306 * the first entry in the child->requests list
307 * (we only send one request at a time).
308 * Ensure we free it before we reschedule.
309 * Bug #5814, from hargagan <shargagan@novell.com>.
310 * JRA.
313 TALLOC_FREE(request->reply_timeout_event);
315 if ((child->pid == 0) && (!fork_domain_child(child))) {
316 /* fork_domain_child failed.
317 Cancel all outstanding requests */
319 while (request != NULL) {
320 /* request might be free'd in the continuation */
321 struct winbindd_async_request *next = request->next;
323 async_request_fail(request);
324 request = next;
326 return;
329 /* Now we know who we're sending to - remember the pid. */
330 request->child_pid = child->pid;
332 setup_async_write(&child->event, request->request,
333 sizeof(*request->request),
334 async_main_request_sent, request);
336 return;
339 struct domain_request_state {
340 TALLOC_CTX *mem_ctx;
341 struct winbindd_domain *domain;
342 struct winbindd_request *request;
343 struct winbindd_response *response;
344 void (*continuation)(void *private_data_data, bool success);
345 void *private_data_data;
348 static void domain_init_recv(void *private_data_data, bool success);
350 void async_domain_request(TALLOC_CTX *mem_ctx,
351 struct winbindd_domain *domain,
352 struct winbindd_request *request,
353 struct winbindd_response *response,
354 void (*continuation)(void *private_data_data, bool success),
355 void *private_data_data)
357 struct domain_request_state *state;
359 if (domain->initialized) {
360 async_request(mem_ctx, &domain->child, request, response,
361 continuation, private_data_data);
362 return;
365 state = TALLOC_P(mem_ctx, struct domain_request_state);
366 if (state == NULL) {
367 DEBUG(0, ("talloc failed\n"));
368 continuation(private_data_data, False);
369 return;
372 state->mem_ctx = mem_ctx;
373 state->domain = domain;
374 state->request = request;
375 state->response = response;
376 state->continuation = continuation;
377 state->private_data_data = private_data_data;
379 init_child_connection(domain, domain_init_recv, state);
382 static void domain_init_recv(void *private_data_data, bool success)
384 struct domain_request_state *state =
385 talloc_get_type_abort(private_data_data, struct domain_request_state);
387 if (!success) {
388 DEBUG(5, ("Domain init returned an error\n"));
389 state->continuation(state->private_data_data, False);
390 return;
393 async_request(state->mem_ctx, &state->domain->child,
394 state->request, state->response,
395 state->continuation, state->private_data_data);
398 static void recvfrom_child(void *private_data_data, bool success)
400 struct winbindd_cli_state *state =
401 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
402 enum winbindd_result result = state->response.result;
404 /* This is an optimization: The child has written directly to the
405 * response buffer. The request itself is still in pending state,
406 * state that in the result code. */
408 state->response.result = WINBINDD_PENDING;
410 if ((!success) || (result != WINBINDD_OK)) {
411 request_error(state);
412 return;
415 request_ok(state);
418 void sendto_child(struct winbindd_cli_state *state,
419 struct winbindd_child *child)
421 async_request(state->mem_ctx, child, &state->request,
422 &state->response, recvfrom_child, state);
425 void sendto_domain(struct winbindd_cli_state *state,
426 struct winbindd_domain *domain)
428 async_domain_request(state->mem_ctx, domain,
429 &state->request, &state->response,
430 recvfrom_child, state);
433 static void child_process_request(struct winbindd_child *child,
434 struct winbindd_cli_state *state)
436 struct winbindd_domain *domain = child->domain;
437 const struct winbindd_child_dispatch_table *table = child->table;
439 /* Free response data - we may be interrupted and receive another
440 command before being able to send this data off. */
442 state->response.result = WINBINDD_ERROR;
443 state->response.length = sizeof(struct winbindd_response);
445 /* as all requests in the child are sync, we can use talloc_tos() */
446 state->mem_ctx = talloc_tos();
448 /* Process command */
450 for (; table->name; table++) {
451 if (state->request.cmd == table->struct_cmd) {
452 DEBUG(10,("child_process_request: request fn %s\n",
453 table->name));
454 state->response.result = table->struct_fn(domain, state);
455 return;
459 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
460 (int)state->request.cmd));
461 state->response.result = WINBINDD_ERROR;
464 void setup_child(struct winbindd_child *child,
465 const struct winbindd_child_dispatch_table *table,
466 const char *logprefix,
467 const char *logname)
469 if (logprefix && logname) {
470 if (asprintf(&child->logfilename, "%s/%s-%s",
471 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
472 smb_panic("Internal error: asprintf failed");
474 } else {
475 smb_panic("Internal error: logprefix == NULL && "
476 "logname == NULL");
479 child->domain = NULL;
480 child->table = table;
483 struct winbindd_child *children = NULL;
485 void winbind_child_died(pid_t pid)
487 struct winbindd_child *child;
489 for (child = children; child != NULL; child = child->next) {
490 if (child->pid == pid) {
491 break;
495 if (child == NULL) {
496 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
497 return;
500 /* This will be re-added in fork_domain_child() */
502 DLIST_REMOVE(children, child);
504 remove_fd_event(&child->event);
505 close(child->event.fd);
506 child->event.fd = 0;
507 child->event.flags = 0;
508 child->pid = 0;
510 if (child->requests) {
512 * schedule_async_request() will also
513 * clear this event but the call is
514 * idempotent so it doesn't hurt to
515 * cover all possible future code
516 * paths. JRA.
518 TALLOC_FREE(child->requests->reply_timeout_event);
521 schedule_async_request(child);
524 /* Ensure any negative cache entries with the netbios or realm names are removed. */
526 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
528 flush_negative_conn_cache_for_domain(domain->name);
529 if (*domain->alt_name) {
530 flush_negative_conn_cache_for_domain(domain->alt_name);
535 * Parent winbindd process sets its own debug level first and then
536 * sends a message to all the winbindd children to adjust their debug
537 * level to that of parents.
540 void winbind_msg_debug(struct messaging_context *msg_ctx,
541 void *private_data,
542 uint32_t msg_type,
543 struct server_id server_id,
544 DATA_BLOB *data)
546 struct winbindd_child *child;
548 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
550 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
552 for (child = children; child != NULL; child = child->next) {
554 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
555 (unsigned int)child->pid));
557 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
558 MSG_DEBUG,
559 data->data,
560 strlen((char *) data->data) + 1);
564 /* Set our domains as offline and forward the offline message to our children. */
566 void winbind_msg_offline(struct messaging_context *msg_ctx,
567 void *private_data,
568 uint32_t msg_type,
569 struct server_id server_id,
570 DATA_BLOB *data)
572 struct winbindd_child *child;
573 struct winbindd_domain *domain;
575 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
577 if (!lp_winbind_offline_logon()) {
578 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
579 return;
582 /* Set our global state as offline. */
583 if (!set_global_winbindd_state_offline()) {
584 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
585 return;
588 /* Set all our domains as offline. */
589 for (domain = domain_list(); domain; domain = domain->next) {
590 if (domain->internal) {
591 continue;
593 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
594 set_domain_offline(domain);
597 for (child = children; child != NULL; child = child->next) {
598 /* Don't send message to internal childs. We've already
599 done so above. */
600 if (!child->domain || winbindd_internal_child(child)) {
601 continue;
604 /* Or internal domains (this should not be possible....) */
605 if (child->domain->internal) {
606 continue;
609 /* Each winbindd child should only process requests for one domain - make sure
610 we only set it online / offline for that domain. */
612 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
613 (unsigned int)child->pid, domain->name ));
615 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
616 MSG_WINBIND_OFFLINE,
617 (uint8 *)child->domain->name,
618 strlen(child->domain->name)+1);
622 /* Set our domains as online and forward the online message to our children. */
624 void winbind_msg_online(struct messaging_context *msg_ctx,
625 void *private_data,
626 uint32_t msg_type,
627 struct server_id server_id,
628 DATA_BLOB *data)
630 struct winbindd_child *child;
631 struct winbindd_domain *domain;
633 DEBUG(10,("winbind_msg_online: got online message.\n"));
635 if (!lp_winbind_offline_logon()) {
636 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
637 return;
640 /* Set our global state as online. */
641 set_global_winbindd_state_online();
643 smb_nscd_flush_user_cache();
644 smb_nscd_flush_group_cache();
646 /* Set all our domains as online. */
647 for (domain = domain_list(); domain; domain = domain->next) {
648 if (domain->internal) {
649 continue;
651 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
653 winbindd_flush_negative_conn_cache(domain);
654 set_domain_online_request(domain);
656 /* Send an online message to the idmap child when our
657 primary domain comes back online */
659 if ( domain->primary ) {
660 struct winbindd_child *idmap = idmap_child();
662 if ( idmap->pid != 0 ) {
663 messaging_send_buf(msg_ctx,
664 pid_to_procid(idmap->pid),
665 MSG_WINBIND_ONLINE,
666 (uint8 *)domain->name,
667 strlen(domain->name)+1);
673 for (child = children; child != NULL; child = child->next) {
674 /* Don't send message to internal childs. */
675 if (!child->domain || winbindd_internal_child(child)) {
676 continue;
679 /* Or internal domains (this should not be possible....) */
680 if (child->domain->internal) {
681 continue;
684 /* Each winbindd child should only process requests for one domain - make sure
685 we only set it online / offline for that domain. */
687 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
688 (unsigned int)child->pid, child->domain->name ));
690 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
691 MSG_WINBIND_ONLINE,
692 (uint8 *)child->domain->name,
693 strlen(child->domain->name)+1);
697 /* Forward the online/offline messages to our children. */
698 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
699 void *private_data,
700 uint32_t msg_type,
701 struct server_id server_id,
702 DATA_BLOB *data)
704 struct winbindd_child *child;
706 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
708 for (child = children; child != NULL; child = child->next) {
709 if (child->domain && child->domain->primary) {
710 DEBUG(10,("winbind_msg_onlinestatus: "
711 "sending message to pid %u of primary domain.\n",
712 (unsigned int)child->pid));
713 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
714 MSG_WINBIND_ONLINESTATUS,
715 (uint8 *)data->data,
716 data->length);
717 break;
722 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
723 void *private_data,
724 uint32_t msg_type,
725 struct server_id server_id,
726 DATA_BLOB *data)
728 struct winbindd_child *child;
730 DEBUG(10,("winbind_msg_dump_event_list received\n"));
732 dump_event_list(winbind_event_context());
734 for (child = children; child != NULL; child = child->next) {
736 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
737 (unsigned int)child->pid));
739 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
740 MSG_DUMP_EVENT_LIST,
741 NULL, 0);
746 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
747 void *private_data,
748 uint32_t msg_type,
749 struct server_id server_id,
750 DATA_BLOB *data)
752 TALLOC_CTX *mem_ctx;
753 const char *message = NULL;
754 struct server_id *sender = NULL;
755 const char *domain = NULL;
756 char *s = NULL;
757 NTSTATUS status;
758 struct winbindd_domain *dom = NULL;
760 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
762 if (!data || !data->data) {
763 return;
766 if (data->length < sizeof(struct server_id)) {
767 return;
770 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
771 if (!mem_ctx) {
772 return;
775 sender = (struct server_id *)data->data;
776 if (data->length > sizeof(struct server_id)) {
777 domain = (const char *)data->data+sizeof(struct server_id);
780 if (domain) {
782 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
783 domain));
785 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
786 find_domain_from_name_noinit(domain));
787 if (!message) {
788 talloc_destroy(mem_ctx);
789 return;
792 messaging_send_buf(msg_ctx, *sender,
793 MSG_WINBIND_DUMP_DOMAIN_LIST,
794 (uint8_t *)message, strlen(message) + 1);
796 talloc_destroy(mem_ctx);
798 return;
801 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
803 for (dom = domain_list(); dom; dom=dom->next) {
804 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
805 if (!message) {
806 talloc_destroy(mem_ctx);
807 return;
810 s = talloc_asprintf_append(s, "%s\n", message);
811 if (!s) {
812 talloc_destroy(mem_ctx);
813 return;
817 status = messaging_send_buf(msg_ctx, *sender,
818 MSG_WINBIND_DUMP_DOMAIN_LIST,
819 (uint8_t *)s, strlen(s) + 1);
820 if (!NT_STATUS_IS_OK(status)) {
821 DEBUG(0,("failed to send message: %s\n",
822 nt_errstr(status)));
825 talloc_destroy(mem_ctx);
828 static void account_lockout_policy_handler(struct event_context *ctx,
829 struct timed_event *te,
830 const struct timeval *now,
831 void *private_data)
833 struct winbindd_child *child =
834 (struct winbindd_child *)private_data;
835 TALLOC_CTX *mem_ctx = NULL;
836 struct winbindd_methods *methods;
837 struct samr_DomInfo12 lockout_policy;
838 NTSTATUS result;
840 DEBUG(10,("account_lockout_policy_handler called\n"));
842 TALLOC_FREE(child->lockout_policy_event);
844 if ( !winbindd_can_contact_domain( child->domain ) ) {
845 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
846 "do not have an incoming trust to domain %s\n",
847 child->domain->name));
849 return;
852 methods = child->domain->methods;
854 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
855 if (!mem_ctx) {
856 result = NT_STATUS_NO_MEMORY;
857 } else {
858 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
860 TALLOC_FREE(mem_ctx);
862 if (!NT_STATUS_IS_OK(result)) {
863 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
864 nt_errstr(result)));
867 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
868 timeval_current_ofs(3600, 0),
869 "account_lockout_policy_handler",
870 account_lockout_policy_handler,
871 child);
874 /* Deal with a request to go offline. */
876 static void child_msg_offline(struct messaging_context *msg,
877 void *private_data,
878 uint32_t msg_type,
879 struct server_id server_id,
880 DATA_BLOB *data)
882 struct winbindd_domain *domain;
883 struct winbindd_domain *primary_domain = NULL;
884 const char *domainname = (const char *)data->data;
886 if (data->data == NULL || data->length == 0) {
887 return;
890 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
892 if (!lp_winbind_offline_logon()) {
893 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
894 return;
897 primary_domain = find_our_domain();
899 /* Mark the requested domain offline. */
901 for (domain = domain_list(); domain; domain = domain->next) {
902 if (domain->internal) {
903 continue;
905 if (strequal(domain->name, domainname)) {
906 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
907 set_domain_offline(domain);
908 /* we are in the trusted domain, set the primary domain
909 * offline too */
910 if (domain != primary_domain) {
911 set_domain_offline(primary_domain);
917 /* Deal with a request to go online. */
919 static void child_msg_online(struct messaging_context *msg,
920 void *private_data,
921 uint32_t msg_type,
922 struct server_id server_id,
923 DATA_BLOB *data)
925 struct winbindd_domain *domain;
926 struct winbindd_domain *primary_domain = NULL;
927 const char *domainname = (const char *)data->data;
929 if (data->data == NULL || data->length == 0) {
930 return;
933 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
935 if (!lp_winbind_offline_logon()) {
936 DEBUG(10,("child_msg_online: rejecting online message.\n"));
937 return;
940 primary_domain = find_our_domain();
942 /* Set our global state as online. */
943 set_global_winbindd_state_online();
945 /* Try and mark everything online - delete any negative cache entries
946 to force a reconnect now. */
948 for (domain = domain_list(); domain; domain = domain->next) {
949 if (domain->internal) {
950 continue;
952 if (strequal(domain->name, domainname)) {
953 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
954 winbindd_flush_negative_conn_cache(domain);
955 set_domain_online_request(domain);
957 /* we can be in trusted domain, which will contact primary domain
958 * we have to bring primary domain online in trusted domain process
959 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
960 * --> contact_domain = find_our_domain()
961 * */
962 if (domain != primary_domain) {
963 winbindd_flush_negative_conn_cache(primary_domain);
964 set_domain_online_request(primary_domain);
970 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
972 struct winbindd_domain *domain;
973 char *buf = NULL;
975 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
976 get_global_winbindd_state_offline() ?
977 "Offline":"Online")) == NULL) {
978 return NULL;
981 for (domain = domain_list(); domain; domain = domain->next) {
982 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
983 domain->name,
984 domain->online ?
985 "Online":"Offline")) == NULL) {
986 return NULL;
990 buf = talloc_asprintf_append_buffer(buf, "\n");
992 DEBUG(5,("collect_onlinestatus: %s", buf));
994 return buf;
997 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
998 void *private_data,
999 uint32_t msg_type,
1000 struct server_id server_id,
1001 DATA_BLOB *data)
1003 TALLOC_CTX *mem_ctx;
1004 const char *message;
1005 struct server_id *sender;
1007 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1009 if (!data->data) {
1010 return;
1013 sender = (struct server_id *)data->data;
1015 mem_ctx = talloc_init("winbind_msg_onlinestatus");
1016 if (mem_ctx == NULL) {
1017 return;
1020 message = collect_onlinestatus(mem_ctx);
1021 if (message == NULL) {
1022 talloc_destroy(mem_ctx);
1023 return;
1026 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
1027 (uint8 *)message, strlen(message) + 1);
1029 talloc_destroy(mem_ctx);
1032 static void child_msg_dump_event_list(struct messaging_context *msg,
1033 void *private_data,
1034 uint32_t msg_type,
1035 struct server_id server_id,
1036 DATA_BLOB *data)
1038 DEBUG(5,("child_msg_dump_event_list received\n"));
1040 dump_event_list(winbind_event_context());
1043 void ccache_remove_all_after_fork(void);
1045 bool winbindd_reinit_after_fork(const char *logfilename)
1047 struct winbindd_domain *domain;
1048 struct winbindd_child *cl;
1050 if (!reinit_after_fork(winbind_messaging_context(),
1051 winbind_event_context(), true)) {
1052 DEBUG(0,("reinit_after_fork() failed\n"));
1053 return false;
1056 close_conns_after_fork();
1058 if (!override_logfile && logfilename) {
1059 lp_set_logfile(logfilename);
1060 reopen_logs();
1063 /* Don't handle the same messages as our parent. */
1064 messaging_deregister(winbind_messaging_context(),
1065 MSG_SMB_CONF_UPDATED, NULL);
1066 messaging_deregister(winbind_messaging_context(),
1067 MSG_SHUTDOWN, NULL);
1068 messaging_deregister(winbind_messaging_context(),
1069 MSG_WINBIND_OFFLINE, NULL);
1070 messaging_deregister(winbind_messaging_context(),
1071 MSG_WINBIND_ONLINE, NULL);
1072 messaging_deregister(winbind_messaging_context(),
1073 MSG_WINBIND_ONLINESTATUS, NULL);
1074 messaging_deregister(winbind_messaging_context(),
1075 MSG_DUMP_EVENT_LIST, NULL);
1076 messaging_deregister(winbind_messaging_context(),
1077 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1078 messaging_deregister(winbind_messaging_context(),
1079 MSG_DEBUG, NULL);
1081 /* We have destroyed all events in the winbindd_event_context
1082 * in reinit_after_fork(), so clean out all possible pending
1083 * event pointers. */
1085 /* Deal with check_online_events. */
1087 for (domain = domain_list(); domain; domain = domain->next) {
1088 TALLOC_FREE(domain->check_online_event);
1091 /* Ensure we're not handling a credential cache event inherited
1092 * from our parent. */
1094 ccache_remove_all_after_fork();
1096 /* Destroy all possible events in child list. */
1097 for (cl = children; cl != NULL; cl = cl->next) {
1098 struct winbindd_async_request *request;
1100 for (request = cl->requests; request; request = request->next) {
1101 TALLOC_FREE(request->reply_timeout_event);
1103 TALLOC_FREE(cl->lockout_policy_event);
1105 /* Children should never be able to send
1106 * each other messages, all messages must
1107 * go through the parent.
1109 cl->pid = (pid_t)0;
1112 * This is a little tricky, children must not
1113 * send an MSG_WINBIND_ONLINE message to idmap_child().
1114 * If we are in a child of our primary domain or
1115 * in the process created by fork_child_dc_connect(),
1116 * and the primary domain cannot go online,
1117 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1118 * periodically to idmap_child().
1120 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1121 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1122 * ---> init_dc_connection() ---> cm_open_connection --->
1123 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1124 * idmap_child(). Disallow children sending messages
1125 * to each other, all messages must go through the parent.
1127 cl = idmap_child();
1128 cl->pid = (pid_t)0;
1130 return true;
1133 static bool fork_domain_child(struct winbindd_child *child)
1135 int fdpair[2];
1136 struct winbindd_cli_state state;
1137 struct winbindd_domain *primary_domain = NULL;
1139 if (child->domain) {
1140 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1141 child->domain->name));
1142 } else {
1143 DEBUG(10, ("fork_domain_child called without domain.\n"));
1146 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1147 DEBUG(0, ("Could not open child pipe: %s\n",
1148 strerror(errno)));
1149 return False;
1152 ZERO_STRUCT(state);
1153 state.pid = sys_getpid();
1155 child->pid = sys_fork();
1157 if (child->pid == -1) {
1158 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1159 return False;
1162 if (child->pid != 0) {
1163 /* Parent */
1164 close(fdpair[0]);
1165 child->next = child->prev = NULL;
1166 DLIST_ADD(children, child);
1167 child->event.fd = fdpair[1];
1168 child->event.flags = 0;
1169 add_fd_event(&child->event);
1170 return True;
1173 /* Child */
1175 /* Stop zombies in children */
1176 CatchChild();
1178 state.sock = fdpair[0];
1179 close(fdpair[1]);
1181 if (!winbindd_reinit_after_fork(child->logfilename)) {
1182 DEBUG(0, ("winbindd_reinit_after_fork failed.\n"));
1183 _exit(0);
1186 /* Handle online/offline messages. */
1187 messaging_register(winbind_messaging_context(), NULL,
1188 MSG_WINBIND_OFFLINE, child_msg_offline);
1189 messaging_register(winbind_messaging_context(), NULL,
1190 MSG_WINBIND_ONLINE, child_msg_online);
1191 messaging_register(winbind_messaging_context(), NULL,
1192 MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1193 messaging_register(winbind_messaging_context(), NULL,
1194 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1195 messaging_register(winbind_messaging_context(), NULL,
1196 MSG_DEBUG, debug_message);
1198 primary_domain = find_our_domain();
1200 if (primary_domain == NULL) {
1201 smb_panic("no primary domain found");
1204 /* It doesn't matter if we allow cache login,
1205 * try to bring domain online after fork. */
1206 if ( child->domain ) {
1207 child->domain->startup = True;
1208 child->domain->startup_time = time(NULL);
1209 /* we can be in primary domain or in trusted domain
1210 * If we are in trusted domain, set the primary domain
1211 * in start-up mode */
1212 if (!(child->domain->internal)) {
1213 set_domain_online_request(child->domain);
1214 if (!(child->domain->primary)) {
1215 primary_domain->startup = True;
1216 primary_domain->startup_time = time(NULL);
1217 set_domain_online_request(primary_domain);
1223 * We are in idmap child, make sure that we set the
1224 * check_online_event to bring primary domain online.
1226 if (child == idmap_child()) {
1227 set_domain_online_request(primary_domain);
1230 /* We might be in the idmap child...*/
1231 if (child->domain && !(child->domain->internal) &&
1232 lp_winbind_offline_logon()) {
1234 set_domain_online_request(child->domain);
1236 if (primary_domain && (primary_domain != child->domain)) {
1237 /* We need to talk to the primary
1238 * domain as well as the trusted
1239 * domain inside a trusted domain
1240 * child.
1241 * See the code in :
1242 * set_dc_type_and_flags_trustinfo()
1243 * for details.
1245 set_domain_online_request(primary_domain);
1248 child->lockout_policy_event = event_add_timed(
1249 winbind_event_context(), NULL, timeval_zero(),
1250 "account_lockout_policy_handler",
1251 account_lockout_policy_handler,
1252 child);
1255 while (1) {
1257 int ret;
1258 fd_set read_fds;
1259 struct timeval t;
1260 struct timeval *tp;
1261 struct timeval now;
1262 TALLOC_CTX *frame = talloc_stackframe();
1264 /* check for signals */
1265 winbind_check_sigterm(false);
1266 winbind_check_sighup(override_logfile ? NULL :
1267 child->logfilename);
1269 run_events(winbind_event_context(), 0, NULL, NULL);
1271 GetTimeOfDay(&now);
1273 if (child->domain && child->domain->startup &&
1274 (now.tv_sec > child->domain->startup_time + 30)) {
1275 /* No longer in "startup" mode. */
1276 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1277 child->domain->name ));
1278 child->domain->startup = False;
1281 tp = get_timed_events_timeout(winbind_event_context(), &t);
1282 if (tp) {
1283 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1284 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1287 /* Handle messages */
1289 message_dispatch(winbind_messaging_context());
1291 FD_ZERO(&read_fds);
1292 FD_SET(state.sock, &read_fds);
1294 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1296 if (ret == 0) {
1297 DEBUG(11,("nothing is ready yet, continue\n"));
1298 TALLOC_FREE(frame);
1299 continue;
1302 if (ret == -1 && errno == EINTR) {
1303 /* We got a signal - continue. */
1304 TALLOC_FREE(frame);
1305 continue;
1308 if (ret == -1 && errno != EINTR) {
1309 DEBUG(0,("select error occured\n"));
1310 TALLOC_FREE(frame);
1311 perror("select");
1312 _exit(1);
1315 /* fetch a request from the main daemon */
1316 child_read_request(&state);
1318 if (state.finished) {
1319 /* we lost contact with our parent */
1320 _exit(0);
1323 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1325 ZERO_STRUCT(state.response);
1326 state.request.null_term = '\0';
1327 child_process_request(child, &state);
1329 SAFE_FREE(state.request.extra_data.data);
1331 cache_store_response(sys_getpid(), &state.response);
1333 SAFE_FREE(state.response.extra_data.data);
1335 /* We just send the result code back, the result
1336 * structure needs to be fetched via the
1337 * winbindd_cache. Hmm. That needs fixing... */
1339 if (write_data(state.sock,
1340 (const char *)&state.response.result,
1341 sizeof(state.response.result)) !=
1342 sizeof(state.response.result)) {
1343 DEBUG(0, ("Could not write result\n"));
1344 exit(1);
1346 TALLOC_FREE(frame);