Remove trailing withespace from wbinfo -m which breaks gdm auth.
[Samba/gbeck.git] / source3 / winbindd / winbindd_dual.c
blob57ab627a5bec4a4fcdb7f741f5f8b94a2d0e49db
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_main_request_sent(void *private_data, bool success);
108 static void async_request_sent(void *private_data, bool success);
109 static void async_reply_recv(void *private_data, bool success);
110 static void schedule_async_request(struct winbindd_child *child);
112 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
113 struct winbindd_request *request,
114 struct winbindd_response *response,
115 void (*continuation)(void *private_data, bool success),
116 void *private_data)
118 struct winbindd_async_request *state;
120 SMB_ASSERT(continuation != NULL);
122 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
124 if (state == NULL) {
125 DEBUG(0, ("talloc failed\n"));
126 continuation(private_data, False);
127 return;
130 state->mem_ctx = mem_ctx;
131 state->child = child;
132 state->request = request;
133 state->response = response;
134 state->continuation = continuation;
135 state->private_data = private_data;
137 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
139 schedule_async_request(child);
141 return;
144 static void async_main_request_sent(void *private_data, bool success)
146 struct winbindd_async_request *state =
147 talloc_get_type_abort(private_data, struct winbindd_async_request);
149 if (!success) {
150 DEBUG(5, ("Could not send async request\n"));
152 state->response->length = sizeof(struct winbindd_response);
153 state->response->result = WINBINDD_ERROR;
154 state->continuation(state->private_data, False);
155 return;
158 if (state->request->extra_len == 0) {
159 async_request_sent(private_data, True);
160 return;
163 setup_async_write(&state->child->event, state->request->extra_data.data,
164 state->request->extra_len,
165 async_request_sent, state);
168 /****************************************************************
169 Handler triggered if the child winbindd doesn't respond within
170 a given timeout.
171 ****************************************************************/
173 static void async_request_timeout_handler(struct event_context *ctx,
174 struct timed_event *te,
175 const struct timeval *now,
176 void *private_data)
178 struct winbindd_async_request *state =
179 talloc_get_type_abort(private_data, struct winbindd_async_request);
181 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
182 "Closing connection to it.\n",
183 state->child_pid ));
185 /* Deal with the reply - set to error. */
186 async_reply_recv(private_data, False);
189 /**************************************************************
190 Common function called on both async send and recv fail.
191 Cleans up the child and schedules the next request.
192 **************************************************************/
194 static void async_request_fail(struct winbindd_async_request *state)
196 DLIST_REMOVE(state->child->requests, state);
198 TALLOC_FREE(state->reply_timeout_event);
200 SMB_ASSERT(state->child_pid != (pid_t)0);
202 /* If not already reaped, send kill signal to child. */
203 if (state->child->pid == state->child_pid) {
204 kill(state->child_pid, SIGTERM);
207 * Close the socket to the child.
209 winbind_child_died(state->child_pid);
212 state->response->length = sizeof(struct winbindd_response);
213 state->response->result = WINBINDD_ERROR;
214 state->continuation(state->private_data, False);
217 static void async_request_sent(void *private_data_data, bool success)
219 struct winbindd_async_request *state =
220 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
222 if (!success) {
223 DEBUG(5, ("Could not send async request to child pid %u\n",
224 (unsigned int)state->child_pid ));
225 async_request_fail(state);
226 return;
229 /* Request successfully sent to the child, setup the wait for reply */
231 setup_async_read(&state->child->event,
232 &state->response->result,
233 sizeof(state->response->result),
234 async_reply_recv, state);
237 * Set up a timeout of 300 seconds for the response.
238 * If we don't get it close the child socket and
239 * report failure.
242 state->reply_timeout_event = event_add_timed(winbind_event_context(),
243 NULL,
244 timeval_current_ofs(300,0),
245 "async_request_timeout",
246 async_request_timeout_handler,
247 state);
248 if (!state->reply_timeout_event) {
249 smb_panic("async_request_sent: failed to add timeout handler.\n");
253 static void async_reply_recv(void *private_data, bool success)
255 struct winbindd_async_request *state =
256 talloc_get_type_abort(private_data, struct winbindd_async_request);
257 struct winbindd_child *child = state->child;
259 TALLOC_FREE(state->reply_timeout_event);
261 state->response->length = sizeof(struct winbindd_response);
263 if (!success) {
264 DEBUG(5, ("Could not receive async reply from child pid %u\n",
265 (unsigned int)state->child_pid ));
267 cache_cleanup_response(state->child_pid);
268 async_request_fail(state);
269 return;
272 SMB_ASSERT(cache_retrieve_response(state->child_pid,
273 state->response));
275 cache_cleanup_response(state->child_pid);
277 DLIST_REMOVE(child->requests, state);
279 schedule_async_request(child);
281 state->continuation(state->private_data, True);
284 static bool fork_domain_child(struct winbindd_child *child);
286 static void schedule_async_request(struct winbindd_child *child)
288 struct winbindd_async_request *request = child->requests;
290 if (request == NULL) {
291 return;
294 if (child->event.flags != 0) {
295 return; /* Busy */
298 if ((child->pid == 0) && (!fork_domain_child(child))) {
299 /* Cancel all outstanding requests */
301 while (request != NULL) {
302 /* request might be free'd in the continuation */
303 struct winbindd_async_request *next = request->next;
304 request->continuation(request->private_data, False);
305 request = next;
307 return;
310 /* Now we know who we're sending to - remember the pid. */
311 request->child_pid = child->pid;
313 setup_async_write(&child->event, request->request,
314 sizeof(*request->request),
315 async_main_request_sent, request);
317 return;
320 struct domain_request_state {
321 TALLOC_CTX *mem_ctx;
322 struct winbindd_domain *domain;
323 struct winbindd_request *request;
324 struct winbindd_response *response;
325 void (*continuation)(void *private_data_data, bool success);
326 void *private_data_data;
329 static void domain_init_recv(void *private_data_data, bool success);
331 void async_domain_request(TALLOC_CTX *mem_ctx,
332 struct winbindd_domain *domain,
333 struct winbindd_request *request,
334 struct winbindd_response *response,
335 void (*continuation)(void *private_data_data, bool success),
336 void *private_data_data)
338 struct domain_request_state *state;
340 if (domain->initialized) {
341 async_request(mem_ctx, &domain->child, request, response,
342 continuation, private_data_data);
343 return;
346 state = TALLOC_P(mem_ctx, struct domain_request_state);
347 if (state == NULL) {
348 DEBUG(0, ("talloc failed\n"));
349 continuation(private_data_data, False);
350 return;
353 state->mem_ctx = mem_ctx;
354 state->domain = domain;
355 state->request = request;
356 state->response = response;
357 state->continuation = continuation;
358 state->private_data_data = private_data_data;
360 init_child_connection(domain, domain_init_recv, state);
363 static void domain_init_recv(void *private_data_data, bool success)
365 struct domain_request_state *state =
366 talloc_get_type_abort(private_data_data, struct domain_request_state);
368 if (!success) {
369 DEBUG(5, ("Domain init returned an error\n"));
370 state->continuation(state->private_data_data, False);
371 return;
374 async_request(state->mem_ctx, &state->domain->child,
375 state->request, state->response,
376 state->continuation, state->private_data_data);
379 static void recvfrom_child(void *private_data_data, bool success)
381 struct winbindd_cli_state *state =
382 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
383 enum winbindd_result result = state->response.result;
385 /* This is an optimization: The child has written directly to the
386 * response buffer. The request itself is still in pending state,
387 * state that in the result code. */
389 state->response.result = WINBINDD_PENDING;
391 if ((!success) || (result != WINBINDD_OK)) {
392 request_error(state);
393 return;
396 request_ok(state);
399 void sendto_child(struct winbindd_cli_state *state,
400 struct winbindd_child *child)
402 async_request(state->mem_ctx, child, &state->request,
403 &state->response, recvfrom_child, state);
406 void sendto_domain(struct winbindd_cli_state *state,
407 struct winbindd_domain *domain)
409 async_domain_request(state->mem_ctx, domain,
410 &state->request, &state->response,
411 recvfrom_child, state);
414 static void child_process_request(struct winbindd_child *child,
415 struct winbindd_cli_state *state)
417 struct winbindd_domain *domain = child->domain;
418 const struct winbindd_child_dispatch_table *table = child->table;
420 /* Free response data - we may be interrupted and receive another
421 command before being able to send this data off. */
423 state->response.result = WINBINDD_ERROR;
424 state->response.length = sizeof(struct winbindd_response);
426 /* as all requests in the child are sync, we can use talloc_tos() */
427 state->mem_ctx = talloc_tos();
429 /* Process command */
431 for (; table->name; table++) {
432 if (state->request.cmd == table->struct_cmd) {
433 DEBUG(10,("child_process_request: request fn %s\n",
434 table->name));
435 state->response.result = table->struct_fn(domain, state);
436 return;
440 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
441 (int)state->request.cmd));
442 state->response.result = WINBINDD_ERROR;
445 void setup_child(struct winbindd_child *child,
446 const struct winbindd_child_dispatch_table *table,
447 const char *logprefix,
448 const char *logname)
450 if (logprefix && logname) {
451 if (asprintf(&child->logfilename, "%s/%s-%s",
452 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
453 smb_panic("Internal error: asprintf failed");
455 } else {
456 smb_panic("Internal error: logprefix == NULL && "
457 "logname == NULL");
460 child->domain = NULL;
461 child->table = table;
464 struct winbindd_child *children = NULL;
466 void winbind_child_died(pid_t pid)
468 struct winbindd_child *child;
470 for (child = children; child != NULL; child = child->next) {
471 if (child->pid == pid) {
472 break;
476 if (child == NULL) {
477 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
478 return;
481 /* This will be re-added in fork_domain_child() */
483 DLIST_REMOVE(children, child);
485 remove_fd_event(&child->event);
486 close(child->event.fd);
487 child->event.fd = 0;
488 child->event.flags = 0;
489 child->pid = 0;
491 schedule_async_request(child);
494 /* Ensure any negative cache entries with the netbios or realm names are removed. */
496 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
498 flush_negative_conn_cache_for_domain(domain->name);
499 if (*domain->alt_name) {
500 flush_negative_conn_cache_for_domain(domain->alt_name);
505 * Parent winbindd process sets its own debug level first and then
506 * sends a message to all the winbindd children to adjust their debug
507 * level to that of parents.
510 void winbind_msg_debug(struct messaging_context *msg_ctx,
511 void *private_data,
512 uint32_t msg_type,
513 struct server_id server_id,
514 DATA_BLOB *data)
516 struct winbindd_child *child;
518 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
520 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
522 for (child = children; child != NULL; child = child->next) {
524 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
525 (unsigned int)child->pid));
527 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
528 MSG_DEBUG,
529 data->data,
530 strlen((char *) data->data) + 1);
534 /* Set our domains as offline and forward the offline message to our children. */
536 void winbind_msg_offline(struct messaging_context *msg_ctx,
537 void *private_data,
538 uint32_t msg_type,
539 struct server_id server_id,
540 DATA_BLOB *data)
542 struct winbindd_child *child;
543 struct winbindd_domain *domain;
545 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
547 if (!lp_winbind_offline_logon()) {
548 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
549 return;
552 /* Set our global state as offline. */
553 if (!set_global_winbindd_state_offline()) {
554 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
555 return;
558 /* Set all our domains as offline. */
559 for (domain = domain_list(); domain; domain = domain->next) {
560 if (domain->internal) {
561 continue;
563 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
564 set_domain_offline(domain);
567 for (child = children; child != NULL; child = child->next) {
568 /* Don't send message to internal childs. We've already
569 done so above. */
570 if (!child->domain || winbindd_internal_child(child)) {
571 continue;
574 /* Or internal domains (this should not be possible....) */
575 if (child->domain->internal) {
576 continue;
579 /* Each winbindd child should only process requests for one domain - make sure
580 we only set it online / offline for that domain. */
582 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
583 (unsigned int)child->pid, domain->name ));
585 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
586 MSG_WINBIND_OFFLINE,
587 (uint8 *)child->domain->name,
588 strlen(child->domain->name)+1);
592 /* Set our domains as online and forward the online message to our children. */
594 void winbind_msg_online(struct messaging_context *msg_ctx,
595 void *private_data,
596 uint32_t msg_type,
597 struct server_id server_id,
598 DATA_BLOB *data)
600 struct winbindd_child *child;
601 struct winbindd_domain *domain;
603 DEBUG(10,("winbind_msg_online: got online message.\n"));
605 if (!lp_winbind_offline_logon()) {
606 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
607 return;
610 /* Set our global state as online. */
611 set_global_winbindd_state_online();
613 smb_nscd_flush_user_cache();
614 smb_nscd_flush_group_cache();
616 /* Set all our domains as online. */
617 for (domain = domain_list(); domain; domain = domain->next) {
618 if (domain->internal) {
619 continue;
621 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
623 winbindd_flush_negative_conn_cache(domain);
624 set_domain_online_request(domain);
626 /* Send an online message to the idmap child when our
627 primary domain comes back online */
629 if ( domain->primary ) {
630 struct winbindd_child *idmap = idmap_child();
632 if ( idmap->pid != 0 ) {
633 messaging_send_buf(msg_ctx,
634 pid_to_procid(idmap->pid),
635 MSG_WINBIND_ONLINE,
636 (uint8 *)domain->name,
637 strlen(domain->name)+1);
643 for (child = children; child != NULL; child = child->next) {
644 /* Don't send message to internal childs. */
645 if (!child->domain || winbindd_internal_child(child)) {
646 continue;
649 /* Or internal domains (this should not be possible....) */
650 if (child->domain->internal) {
651 continue;
654 /* Each winbindd child should only process requests for one domain - make sure
655 we only set it online / offline for that domain. */
657 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
658 (unsigned int)child->pid, child->domain->name ));
660 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
661 MSG_WINBIND_ONLINE,
662 (uint8 *)child->domain->name,
663 strlen(child->domain->name)+1);
667 /* Forward the online/offline messages to our children. */
668 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
669 void *private_data,
670 uint32_t msg_type,
671 struct server_id server_id,
672 DATA_BLOB *data)
674 struct winbindd_child *child;
676 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
678 for (child = children; child != NULL; child = child->next) {
679 if (child->domain && child->domain->primary) {
680 DEBUG(10,("winbind_msg_onlinestatus: "
681 "sending message to pid %u of primary domain.\n",
682 (unsigned int)child->pid));
683 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
684 MSG_WINBIND_ONLINESTATUS,
685 (uint8 *)data->data,
686 data->length);
687 break;
692 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
693 void *private_data,
694 uint32_t msg_type,
695 struct server_id server_id,
696 DATA_BLOB *data)
698 struct winbindd_child *child;
700 DEBUG(10,("winbind_msg_dump_event_list received\n"));
702 dump_event_list(winbind_event_context());
704 for (child = children; child != NULL; child = child->next) {
706 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
707 (unsigned int)child->pid));
709 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
710 MSG_DUMP_EVENT_LIST,
711 NULL, 0);
716 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
717 void *private_data,
718 uint32_t msg_type,
719 struct server_id server_id,
720 DATA_BLOB *data)
722 TALLOC_CTX *mem_ctx;
723 const char *message = NULL;
724 struct server_id *sender = NULL;
725 const char *domain = NULL;
726 char *s = NULL;
727 NTSTATUS status;
728 struct winbindd_domain *dom = NULL;
730 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
732 if (!data || !data->data) {
733 return;
736 if (data->length < sizeof(struct server_id)) {
737 return;
740 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
741 if (!mem_ctx) {
742 return;
745 sender = (struct server_id *)data->data;
746 if (data->length > sizeof(struct server_id)) {
747 domain = (const char *)data->data+sizeof(struct server_id);
750 if (domain) {
752 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
753 domain));
755 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
756 find_domain_from_name_noinit(domain));
757 if (!message) {
758 talloc_destroy(mem_ctx);
759 return;
762 messaging_send_buf(msg_ctx, *sender,
763 MSG_WINBIND_DUMP_DOMAIN_LIST,
764 (uint8_t *)message, strlen(message) + 1);
766 talloc_destroy(mem_ctx);
768 return;
771 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
773 for (dom = domain_list(); dom; dom=dom->next) {
774 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
775 if (!message) {
776 talloc_destroy(mem_ctx);
777 return;
780 s = talloc_asprintf_append(s, "%s\n", message);
781 if (!s) {
782 talloc_destroy(mem_ctx);
783 return;
787 status = messaging_send_buf(msg_ctx, *sender,
788 MSG_WINBIND_DUMP_DOMAIN_LIST,
789 (uint8_t *)s, strlen(s) + 1);
790 if (!NT_STATUS_IS_OK(status)) {
791 DEBUG(0,("failed to send message: %s\n",
792 nt_errstr(status)));
795 talloc_destroy(mem_ctx);
798 static void account_lockout_policy_handler(struct event_context *ctx,
799 struct timed_event *te,
800 const struct timeval *now,
801 void *private_data)
803 struct winbindd_child *child =
804 (struct winbindd_child *)private_data;
805 TALLOC_CTX *mem_ctx = NULL;
806 struct winbindd_methods *methods;
807 struct samr_DomInfo12 lockout_policy;
808 NTSTATUS result;
810 DEBUG(10,("account_lockout_policy_handler called\n"));
812 TALLOC_FREE(child->lockout_policy_event);
814 if ( !winbindd_can_contact_domain( child->domain ) ) {
815 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
816 "do not have an incoming trust to domain %s\n",
817 child->domain->name));
819 return;
822 methods = child->domain->methods;
824 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
825 if (!mem_ctx) {
826 result = NT_STATUS_NO_MEMORY;
827 } else {
828 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
830 TALLOC_FREE(mem_ctx);
832 if (!NT_STATUS_IS_OK(result)) {
833 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
834 nt_errstr(result)));
837 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
838 timeval_current_ofs(3600, 0),
839 "account_lockout_policy_handler",
840 account_lockout_policy_handler,
841 child);
844 /* Deal with a request to go offline. */
846 static void child_msg_offline(struct messaging_context *msg,
847 void *private_data,
848 uint32_t msg_type,
849 struct server_id server_id,
850 DATA_BLOB *data)
852 struct winbindd_domain *domain;
853 const char *domainname = (const char *)data->data;
855 if (data->data == NULL || data->length == 0) {
856 return;
859 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
861 if (!lp_winbind_offline_logon()) {
862 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
863 return;
866 /* Mark the requested domain offline. */
868 for (domain = domain_list(); domain; domain = domain->next) {
869 if (domain->internal) {
870 continue;
872 if (strequal(domain->name, domainname)) {
873 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
874 set_domain_offline(domain);
879 /* Deal with a request to go online. */
881 static void child_msg_online(struct messaging_context *msg,
882 void *private_data,
883 uint32_t msg_type,
884 struct server_id server_id,
885 DATA_BLOB *data)
887 struct winbindd_domain *domain;
888 const char *domainname = (const char *)data->data;
890 if (data->data == NULL || data->length == 0) {
891 return;
894 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
896 if (!lp_winbind_offline_logon()) {
897 DEBUG(10,("child_msg_online: rejecting online message.\n"));
898 return;
901 /* Set our global state as online. */
902 set_global_winbindd_state_online();
904 /* Try and mark everything online - delete any negative cache entries
905 to force a reconnect now. */
907 for (domain = domain_list(); domain; domain = domain->next) {
908 if (domain->internal) {
909 continue;
911 if (strequal(domain->name, domainname)) {
912 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
913 winbindd_flush_negative_conn_cache(domain);
914 set_domain_online_request(domain);
919 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
921 struct winbindd_domain *domain;
922 char *buf = NULL;
924 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
925 get_global_winbindd_state_offline() ?
926 "Offline":"Online")) == NULL) {
927 return NULL;
930 for (domain = domain_list(); domain; domain = domain->next) {
931 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
932 domain->name,
933 domain->online ?
934 "Online":"Offline")) == NULL) {
935 return NULL;
939 buf = talloc_asprintf_append_buffer(buf, "\n");
941 DEBUG(5,("collect_onlinestatus: %s", buf));
943 return buf;
946 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
947 void *private_data,
948 uint32_t msg_type,
949 struct server_id server_id,
950 DATA_BLOB *data)
952 TALLOC_CTX *mem_ctx;
953 const char *message;
954 struct server_id *sender;
956 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
958 if (!data->data) {
959 return;
962 sender = (struct server_id *)data->data;
964 mem_ctx = talloc_init("winbind_msg_onlinestatus");
965 if (mem_ctx == NULL) {
966 return;
969 message = collect_onlinestatus(mem_ctx);
970 if (message == NULL) {
971 talloc_destroy(mem_ctx);
972 return;
975 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
976 (uint8 *)message, strlen(message) + 1);
978 talloc_destroy(mem_ctx);
981 static void child_msg_dump_event_list(struct messaging_context *msg,
982 void *private_data,
983 uint32_t msg_type,
984 struct server_id server_id,
985 DATA_BLOB *data)
987 DEBUG(5,("child_msg_dump_event_list received\n"));
989 dump_event_list(winbind_event_context());
993 static bool fork_domain_child(struct winbindd_child *child)
995 int fdpair[2];
996 struct winbindd_cli_state state;
997 struct winbindd_domain *domain;
998 struct winbindd_domain *primary_domain = NULL;
1000 if (child->domain) {
1001 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1002 child->domain->name));
1003 } else {
1004 DEBUG(10, ("fork_domain_child called without domain.\n"));
1007 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1008 DEBUG(0, ("Could not open child pipe: %s\n",
1009 strerror(errno)));
1010 return False;
1013 ZERO_STRUCT(state);
1014 state.pid = sys_getpid();
1016 child->pid = sys_fork();
1018 if (child->pid == -1) {
1019 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1020 return False;
1023 if (child->pid != 0) {
1024 /* Parent */
1025 close(fdpair[0]);
1026 child->next = child->prev = NULL;
1027 DLIST_ADD(children, child);
1028 child->event.fd = fdpair[1];
1029 child->event.flags = 0;
1030 child->requests = NULL;
1031 add_fd_event(&child->event);
1032 return True;
1035 /* Child */
1037 /* Stop zombies in children */
1038 CatchChild();
1040 state.sock = fdpair[0];
1041 close(fdpair[1]);
1043 if (!reinit_after_fork(winbind_messaging_context(), true)) {
1044 DEBUG(0,("reinit_after_fork() failed\n"));
1045 _exit(0);
1048 close_conns_after_fork();
1050 if (!override_logfile) {
1051 lp_set_logfile(child->logfilename);
1052 reopen_logs();
1056 * For clustering, we need to re-init our ctdbd connection after the
1057 * fork
1059 if (!NT_STATUS_IS_OK(messaging_reinit(winbind_messaging_context())))
1060 exit(1);
1062 /* Don't handle the same messages as our parent. */
1063 messaging_deregister(winbind_messaging_context(),
1064 MSG_SMB_CONF_UPDATED, NULL);
1065 messaging_deregister(winbind_messaging_context(),
1066 MSG_SHUTDOWN, NULL);
1067 messaging_deregister(winbind_messaging_context(),
1068 MSG_WINBIND_OFFLINE, NULL);
1069 messaging_deregister(winbind_messaging_context(),
1070 MSG_WINBIND_ONLINE, NULL);
1071 messaging_deregister(winbind_messaging_context(),
1072 MSG_WINBIND_ONLINESTATUS, NULL);
1073 messaging_deregister(winbind_messaging_context(),
1074 MSG_DUMP_EVENT_LIST, NULL);
1075 messaging_deregister(winbind_messaging_context(),
1076 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1077 messaging_deregister(winbind_messaging_context(),
1078 MSG_DEBUG, NULL);
1080 /* Handle online/offline messages. */
1081 messaging_register(winbind_messaging_context(), NULL,
1082 MSG_WINBIND_OFFLINE, child_msg_offline);
1083 messaging_register(winbind_messaging_context(), NULL,
1084 MSG_WINBIND_ONLINE, child_msg_online);
1085 messaging_register(winbind_messaging_context(), NULL,
1086 MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1087 messaging_register(winbind_messaging_context(), NULL,
1088 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1089 messaging_register(winbind_messaging_context(), NULL,
1090 MSG_DEBUG, debug_message);
1092 if ( child->domain ) {
1093 child->domain->startup = True;
1094 child->domain->startup_time = time(NULL);
1097 /* Ensure we have no pending check_online events other
1098 than one for this domain or the primary domain. */
1100 for (domain = domain_list(); domain; domain = domain->next) {
1101 if (domain->primary) {
1102 primary_domain = domain;
1104 if ((domain != child->domain) && !domain->primary) {
1105 TALLOC_FREE(domain->check_online_event);
1109 /* Ensure we're not handling an event inherited from
1110 our parent. */
1112 cancel_named_event(winbind_event_context(),
1113 "krb5_ticket_refresh_handler");
1115 /* We might be in the idmap child...*/
1116 if (child->domain && !(child->domain->internal) &&
1117 lp_winbind_offline_logon()) {
1119 set_domain_online_request(child->domain);
1121 if (primary_domain != child->domain) {
1122 /* We need to talk to the primary
1123 * domain as well as the trusted
1124 * domain inside a trusted domain
1125 * child.
1126 * See the code in :
1127 * set_dc_type_and_flags_trustinfo()
1128 * for details.
1130 set_domain_online_request(primary_domain);
1133 child->lockout_policy_event = event_add_timed(
1134 winbind_event_context(), NULL, timeval_zero(),
1135 "account_lockout_policy_handler",
1136 account_lockout_policy_handler,
1137 child);
1140 while (1) {
1142 int ret;
1143 fd_set read_fds;
1144 struct timeval t;
1145 struct timeval *tp;
1146 struct timeval now;
1147 TALLOC_CTX *frame = talloc_stackframe();
1149 /* check for signals */
1150 winbind_check_sigterm(false);
1151 winbind_check_sighup(override_logfile ? NULL :
1152 child->logfilename);
1154 run_events(winbind_event_context(), 0, NULL, NULL);
1156 GetTimeOfDay(&now);
1158 if (child->domain && child->domain->startup &&
1159 (now.tv_sec > child->domain->startup_time + 30)) {
1160 /* No longer in "startup" mode. */
1161 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1162 child->domain->name ));
1163 child->domain->startup = False;
1166 tp = get_timed_events_timeout(winbind_event_context(), &t);
1167 if (tp) {
1168 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1169 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1172 /* Handle messages */
1174 message_dispatch(winbind_messaging_context());
1176 FD_ZERO(&read_fds);
1177 FD_SET(state.sock, &read_fds);
1179 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1181 if (ret == 0) {
1182 DEBUG(11,("nothing is ready yet, continue\n"));
1183 TALLOC_FREE(frame);
1184 continue;
1187 if (ret == -1 && errno == EINTR) {
1188 /* We got a signal - continue. */
1189 TALLOC_FREE(frame);
1190 continue;
1193 if (ret == -1 && errno != EINTR) {
1194 DEBUG(0,("select error occured\n"));
1195 TALLOC_FREE(frame);
1196 perror("select");
1197 return False;
1200 /* fetch a request from the main daemon */
1201 child_read_request(&state);
1203 if (state.finished) {
1204 /* we lost contact with our parent */
1205 exit(0);
1208 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1210 ZERO_STRUCT(state.response);
1211 state.request.null_term = '\0';
1212 child_process_request(child, &state);
1214 SAFE_FREE(state.request.extra_data.data);
1216 cache_store_response(sys_getpid(), &state.response);
1218 SAFE_FREE(state.response.extra_data.data);
1220 /* We just send the result code back, the result
1221 * structure needs to be fetched via the
1222 * winbindd_cache. Hmm. That needs fixing... */
1224 if (write_data(state.sock,
1225 (const char *)&state.response.result,
1226 sizeof(state.response.result)) !=
1227 sizeof(state.response.result)) {
1228 DEBUG(0, ("Could not write result\n"));
1229 exit(1);
1231 TALLOC_FREE(frame);