s3:build: some implementations of "ln" are picky about the order of arguments
[Samba.git] / source3 / winbindd / winbindd_dual.c
blobd40bab94ef9e5b33ed54ce729ee8f19bac348732
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 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_handler,
251 state);
252 if (!state->reply_timeout_event) {
253 smb_panic("async_request_sent: failed to add timeout handler.\n");
257 static void async_reply_recv(void *private_data, bool success)
259 struct winbindd_async_request *state =
260 talloc_get_type_abort(private_data, struct winbindd_async_request);
261 struct winbindd_child *child = state->child;
263 TALLOC_FREE(state->reply_timeout_event);
265 state->response->length = sizeof(struct winbindd_response);
267 if (!success) {
268 DEBUG(5, ("Could not receive async reply from child pid %u\n",
269 (unsigned int)state->child_pid ));
271 cache_cleanup_response(state->child_pid);
272 async_request_fail(state);
273 return;
276 SMB_ASSERT(cache_retrieve_response(state->child_pid,
277 state->response));
279 cache_cleanup_response(state->child_pid);
281 DLIST_REMOVE(child->requests, state);
283 schedule_async_request(child);
285 state->continuation(state->private_data, True);
288 static bool fork_domain_child(struct winbindd_child *child);
290 static void schedule_async_request(struct winbindd_child *child)
292 struct winbindd_async_request *request = child->requests;
294 if (request == NULL) {
295 return;
298 if (child->event.flags != 0) {
299 return; /* Busy */
303 * This may be a reschedule, so we might
304 * have an existing timeout event pending on
305 * the first entry in the child->requests list
306 * (we only send one request at a time).
307 * Ensure we free it before we reschedule.
308 * Bug #5814, from hargagan <shargagan@novell.com>.
309 * JRA.
312 TALLOC_FREE(request->reply_timeout_event);
314 if ((child->pid == 0) && (!fork_domain_child(child))) {
315 /* fork_domain_child failed.
316 Cancel all outstanding requests */
318 while (request != NULL) {
319 /* request might be free'd in the continuation */
320 struct winbindd_async_request *next = request->next;
322 async_request_fail(request);
323 request = next;
325 return;
328 /* Now we know who we're sending to - remember the pid. */
329 request->child_pid = child->pid;
331 setup_async_write(&child->event, request->request,
332 sizeof(*request->request),
333 async_main_request_sent, request);
335 return;
338 struct domain_request_state {
339 TALLOC_CTX *mem_ctx;
340 struct winbindd_domain *domain;
341 struct winbindd_request *request;
342 struct winbindd_response *response;
343 void (*continuation)(void *private_data_data, bool success);
344 void *private_data_data;
347 static void domain_init_recv(void *private_data_data, bool success);
349 void async_domain_request(TALLOC_CTX *mem_ctx,
350 struct winbindd_domain *domain,
351 struct winbindd_request *request,
352 struct winbindd_response *response,
353 void (*continuation)(void *private_data_data, bool success),
354 void *private_data_data)
356 struct domain_request_state *state;
358 if (domain->initialized) {
359 async_request(mem_ctx, &domain->child, request, response,
360 continuation, private_data_data);
361 return;
364 state = TALLOC_P(mem_ctx, struct domain_request_state);
365 if (state == NULL) {
366 DEBUG(0, ("talloc failed\n"));
367 continuation(private_data_data, False);
368 return;
371 state->mem_ctx = mem_ctx;
372 state->domain = domain;
373 state->request = request;
374 state->response = response;
375 state->continuation = continuation;
376 state->private_data_data = private_data_data;
378 init_child_connection(domain, domain_init_recv, state);
381 static void domain_init_recv(void *private_data_data, bool success)
383 struct domain_request_state *state =
384 talloc_get_type_abort(private_data_data, struct domain_request_state);
386 if (!success) {
387 DEBUG(5, ("Domain init returned an error\n"));
388 state->continuation(state->private_data_data, False);
389 return;
392 async_request(state->mem_ctx, &state->domain->child,
393 state->request, state->response,
394 state->continuation, state->private_data_data);
397 static void recvfrom_child(void *private_data_data, bool success)
399 struct winbindd_cli_state *state =
400 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
401 enum winbindd_result result = state->response.result;
403 /* This is an optimization: The child has written directly to the
404 * response buffer. The request itself is still in pending state,
405 * state that in the result code. */
407 state->response.result = WINBINDD_PENDING;
409 if ((!success) || (result != WINBINDD_OK)) {
410 request_error(state);
411 return;
414 request_ok(state);
417 void sendto_child(struct winbindd_cli_state *state,
418 struct winbindd_child *child)
420 async_request(state->mem_ctx, child, &state->request,
421 &state->response, recvfrom_child, state);
424 void sendto_domain(struct winbindd_cli_state *state,
425 struct winbindd_domain *domain)
427 async_domain_request(state->mem_ctx, domain,
428 &state->request, &state->response,
429 recvfrom_child, state);
432 static void child_process_request(struct winbindd_child *child,
433 struct winbindd_cli_state *state)
435 struct winbindd_domain *domain = child->domain;
436 const struct winbindd_child_dispatch_table *table = child->table;
438 /* Free response data - we may be interrupted and receive another
439 command before being able to send this data off. */
441 state->response.result = WINBINDD_ERROR;
442 state->response.length = sizeof(struct winbindd_response);
444 /* as all requests in the child are sync, we can use talloc_tos() */
445 state->mem_ctx = talloc_tos();
447 /* Process command */
449 for (; table->name; table++) {
450 if (state->request.cmd == table->struct_cmd) {
451 DEBUG(10,("child_process_request: request fn %s\n",
452 table->name));
453 state->response.result = table->struct_fn(domain, state);
454 return;
458 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
459 (int)state->request.cmd));
460 state->response.result = WINBINDD_ERROR;
463 void setup_child(struct winbindd_child *child,
464 const struct winbindd_child_dispatch_table *table,
465 const char *logprefix,
466 const char *logname)
468 if (logprefix && logname) {
469 if (asprintf(&child->logfilename, "%s/%s-%s",
470 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
471 smb_panic("Internal error: asprintf failed");
473 } else {
474 smb_panic("Internal error: logprefix == NULL && "
475 "logname == NULL");
478 child->domain = NULL;
479 child->table = table;
482 struct winbindd_child *children = NULL;
484 void winbind_child_died(pid_t pid)
486 struct winbindd_child *child;
488 for (child = children; child != NULL; child = child->next) {
489 if (child->pid == pid) {
490 break;
494 if (child == NULL) {
495 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
496 return;
499 /* This will be re-added in fork_domain_child() */
501 DLIST_REMOVE(children, child);
503 remove_fd_event(&child->event);
504 close(child->event.fd);
505 child->event.fd = 0;
506 child->event.flags = 0;
507 child->pid = 0;
509 if (child->requests) {
511 * schedule_async_request() will also
512 * clear this event but the call is
513 * idempotent so it doesn't hurt to
514 * cover all possible future code
515 * paths. JRA.
517 TALLOC_FREE(child->requests->reply_timeout_event);
520 schedule_async_request(child);
523 /* Ensure any negative cache entries with the netbios or realm names are removed. */
525 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
527 flush_negative_conn_cache_for_domain(domain->name);
528 if (*domain->alt_name) {
529 flush_negative_conn_cache_for_domain(domain->alt_name);
534 * Parent winbindd process sets its own debug level first and then
535 * sends a message to all the winbindd children to adjust their debug
536 * level to that of parents.
539 void winbind_msg_debug(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;
547 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
549 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
551 for (child = children; child != NULL; child = child->next) {
553 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
554 (unsigned int)child->pid));
556 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
557 MSG_DEBUG,
558 data->data,
559 strlen((char *) data->data) + 1);
563 /* Set our domains as offline and forward the offline message to our children. */
565 void winbind_msg_offline(struct messaging_context *msg_ctx,
566 void *private_data,
567 uint32_t msg_type,
568 struct server_id server_id,
569 DATA_BLOB *data)
571 struct winbindd_child *child;
572 struct winbindd_domain *domain;
574 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
576 if (!lp_winbind_offline_logon()) {
577 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
578 return;
581 /* Set our global state as offline. */
582 if (!set_global_winbindd_state_offline()) {
583 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
584 return;
587 /* Set all our domains as offline. */
588 for (domain = domain_list(); domain; domain = domain->next) {
589 if (domain->internal) {
590 continue;
592 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
593 set_domain_offline(domain);
596 for (child = children; child != NULL; child = child->next) {
597 /* Don't send message to internal childs. We've already
598 done so above. */
599 if (!child->domain || winbindd_internal_child(child)) {
600 continue;
603 /* Or internal domains (this should not be possible....) */
604 if (child->domain->internal) {
605 continue;
608 /* Each winbindd child should only process requests for one domain - make sure
609 we only set it online / offline for that domain. */
611 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
612 (unsigned int)child->pid, domain->name ));
614 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
615 MSG_WINBIND_OFFLINE,
616 (uint8 *)child->domain->name,
617 strlen(child->domain->name)+1);
621 /* Set our domains as online and forward the online message to our children. */
623 void winbind_msg_online(struct messaging_context *msg_ctx,
624 void *private_data,
625 uint32_t msg_type,
626 struct server_id server_id,
627 DATA_BLOB *data)
629 struct winbindd_child *child;
630 struct winbindd_domain *domain;
632 DEBUG(10,("winbind_msg_online: got online message.\n"));
634 if (!lp_winbind_offline_logon()) {
635 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
636 return;
639 /* Set our global state as online. */
640 set_global_winbindd_state_online();
642 smb_nscd_flush_user_cache();
643 smb_nscd_flush_group_cache();
645 /* Set all our domains as online. */
646 for (domain = domain_list(); domain; domain = domain->next) {
647 if (domain->internal) {
648 continue;
650 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
652 winbindd_flush_negative_conn_cache(domain);
653 set_domain_online_request(domain);
655 /* Send an online message to the idmap child when our
656 primary domain comes back online */
658 if ( domain->primary ) {
659 struct winbindd_child *idmap = idmap_child();
661 if ( idmap->pid != 0 ) {
662 messaging_send_buf(msg_ctx,
663 pid_to_procid(idmap->pid),
664 MSG_WINBIND_ONLINE,
665 (uint8 *)domain->name,
666 strlen(domain->name)+1);
672 for (child = children; child != NULL; child = child->next) {
673 /* Don't send message to internal childs. */
674 if (!child->domain || winbindd_internal_child(child)) {
675 continue;
678 /* Or internal domains (this should not be possible....) */
679 if (child->domain->internal) {
680 continue;
683 /* Each winbindd child should only process requests for one domain - make sure
684 we only set it online / offline for that domain. */
686 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
687 (unsigned int)child->pid, child->domain->name ));
689 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
690 MSG_WINBIND_ONLINE,
691 (uint8 *)child->domain->name,
692 strlen(child->domain->name)+1);
696 /* Forward the online/offline messages to our children. */
697 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
698 void *private_data,
699 uint32_t msg_type,
700 struct server_id server_id,
701 DATA_BLOB *data)
703 struct winbindd_child *child;
705 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
707 for (child = children; child != NULL; child = child->next) {
708 if (child->domain && child->domain->primary) {
709 DEBUG(10,("winbind_msg_onlinestatus: "
710 "sending message to pid %u of primary domain.\n",
711 (unsigned int)child->pid));
712 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
713 MSG_WINBIND_ONLINESTATUS,
714 (uint8 *)data->data,
715 data->length);
716 break;
721 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
722 void *private_data,
723 uint32_t msg_type,
724 struct server_id server_id,
725 DATA_BLOB *data)
727 struct winbindd_child *child;
729 DEBUG(10,("winbind_msg_dump_event_list received\n"));
731 dump_event_list(winbind_event_context());
733 for (child = children; child != NULL; child = child->next) {
735 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
736 (unsigned int)child->pid));
738 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
739 MSG_DUMP_EVENT_LIST,
740 NULL, 0);
745 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
746 void *private_data,
747 uint32_t msg_type,
748 struct server_id server_id,
749 DATA_BLOB *data)
751 TALLOC_CTX *mem_ctx;
752 const char *message = NULL;
753 struct server_id *sender = NULL;
754 const char *domain = NULL;
755 char *s = NULL;
756 NTSTATUS status;
757 struct winbindd_domain *dom = NULL;
759 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
761 if (!data || !data->data) {
762 return;
765 if (data->length < sizeof(struct server_id)) {
766 return;
769 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
770 if (!mem_ctx) {
771 return;
774 sender = (struct server_id *)data->data;
775 if (data->length > sizeof(struct server_id)) {
776 domain = (const char *)data->data+sizeof(struct server_id);
779 if (domain) {
781 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
782 domain));
784 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
785 find_domain_from_name_noinit(domain));
786 if (!message) {
787 talloc_destroy(mem_ctx);
788 return;
791 messaging_send_buf(msg_ctx, *sender,
792 MSG_WINBIND_DUMP_DOMAIN_LIST,
793 (uint8_t *)message, strlen(message) + 1);
795 talloc_destroy(mem_ctx);
797 return;
800 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
802 for (dom = domain_list(); dom; dom=dom->next) {
803 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
804 if (!message) {
805 talloc_destroy(mem_ctx);
806 return;
809 s = talloc_asprintf_append(s, "%s\n", message);
810 if (!s) {
811 talloc_destroy(mem_ctx);
812 return;
816 status = messaging_send_buf(msg_ctx, *sender,
817 MSG_WINBIND_DUMP_DOMAIN_LIST,
818 (uint8_t *)s, strlen(s) + 1);
819 if (!NT_STATUS_IS_OK(status)) {
820 DEBUG(0,("failed to send message: %s\n",
821 nt_errstr(status)));
824 talloc_destroy(mem_ctx);
827 static void account_lockout_policy_handler(struct event_context *ctx,
828 struct timed_event *te,
829 struct timeval now,
830 void *private_data)
832 struct winbindd_child *child =
833 (struct winbindd_child *)private_data;
834 TALLOC_CTX *mem_ctx = NULL;
835 struct winbindd_methods *methods;
836 struct samr_DomInfo12 lockout_policy;
837 NTSTATUS result;
839 DEBUG(10,("account_lockout_policy_handler called\n"));
841 TALLOC_FREE(child->lockout_policy_event);
843 if ( !winbindd_can_contact_domain( child->domain ) ) {
844 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
845 "do not have an incoming trust to domain %s\n",
846 child->domain->name));
848 return;
851 methods = child->domain->methods;
853 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
854 if (!mem_ctx) {
855 result = NT_STATUS_NO_MEMORY;
856 } else {
857 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
859 TALLOC_FREE(mem_ctx);
861 if (!NT_STATUS_IS_OK(result)) {
862 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
863 nt_errstr(result)));
866 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
867 timeval_current_ofs(3600, 0),
868 account_lockout_policy_handler,
869 child);
872 static time_t get_machine_password_timeout(void)
874 /* until we have gpo support use lp setting */
875 return lp_machine_password_timeout();
878 static bool calculate_next_machine_pwd_change(const char *domain,
879 struct timeval *t)
881 time_t pass_last_set_time;
882 time_t timeout;
883 time_t next_change;
884 char *pw;
886 pw = secrets_fetch_machine_password(domain,
887 &pass_last_set_time,
888 NULL);
890 if (pw == NULL) {
891 DEBUG(0,("cannot fetch own machine password ????"));
892 return false;
895 SAFE_FREE(pw);
897 timeout = get_machine_password_timeout();
898 if (timeout == 0) {
899 DEBUG(10,("machine password never expires\n"));
900 return false;
903 if (time(NULL) < (pass_last_set_time + timeout)) {
904 next_change = pass_last_set_time + timeout;
905 DEBUG(10,("machine password still valid until: %s\n",
906 http_timestring(talloc_tos(), next_change)));
907 *t = timeval_set(next_change, 0);
908 return true;
911 DEBUG(10,("machine password expired, needs immediate change\n"));
913 *t = timeval_zero();
915 return true;
918 static void machine_password_change_handler(struct event_context *ctx,
919 struct timed_event *te,
920 struct timeval now,
921 void *private_data)
923 struct winbindd_child *child =
924 (struct winbindd_child *)private_data;
925 struct rpc_pipe_client *netlogon_pipe = NULL;
926 TALLOC_CTX *frame;
927 NTSTATUS result;
928 struct timeval next_change;
930 DEBUG(10,("machine_password_change_handler called\n"));
932 TALLOC_FREE(child->machine_password_change_event);
934 if (!calculate_next_machine_pwd_change(child->domain->name,
935 &next_change)) {
936 return;
939 if (!winbindd_can_contact_domain(child->domain)) {
940 DEBUG(10,("machine_password_change_handler: Removing myself since I "
941 "do not have an incoming trust to domain %s\n",
942 child->domain->name));
943 return;
946 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
947 if (!NT_STATUS_IS_OK(result)) {
948 DEBUG(10,("machine_password_change_handler: "
949 "failed to connect netlogon pipe: %s\n",
950 nt_errstr(result)));
951 return;
954 frame = talloc_stackframe();
956 result = trust_pw_find_change_and_store_it(netlogon_pipe,
957 frame,
958 child->domain->name);
959 TALLOC_FREE(frame);
961 if (!NT_STATUS_IS_OK(result)) {
962 DEBUG(10,("machine_password_change_handler: "
963 "failed to change machine password: %s\n",
964 nt_errstr(result)));
965 } else {
966 DEBUG(10,("machine_password_change_handler: "
967 "successfully changed machine password\n"));
970 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
971 next_change,
972 machine_password_change_handler,
973 child);
976 /* Deal with a request to go offline. */
978 static void child_msg_offline(struct messaging_context *msg,
979 void *private_data,
980 uint32_t msg_type,
981 struct server_id server_id,
982 DATA_BLOB *data)
984 struct winbindd_domain *domain;
985 struct winbindd_domain *primary_domain = NULL;
986 const char *domainname = (const char *)data->data;
988 if (data->data == NULL || data->length == 0) {
989 return;
992 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
994 if (!lp_winbind_offline_logon()) {
995 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
996 return;
999 primary_domain = find_our_domain();
1001 /* Mark the requested domain offline. */
1003 for (domain = domain_list(); domain; domain = domain->next) {
1004 if (domain->internal) {
1005 continue;
1007 if (strequal(domain->name, domainname)) {
1008 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1009 set_domain_offline(domain);
1010 /* we are in the trusted domain, set the primary domain
1011 * offline too */
1012 if (domain != primary_domain) {
1013 set_domain_offline(primary_domain);
1019 /* Deal with a request to go online. */
1021 static void child_msg_online(struct messaging_context *msg,
1022 void *private_data,
1023 uint32_t msg_type,
1024 struct server_id server_id,
1025 DATA_BLOB *data)
1027 struct winbindd_domain *domain;
1028 struct winbindd_domain *primary_domain = NULL;
1029 const char *domainname = (const char *)data->data;
1031 if (data->data == NULL || data->length == 0) {
1032 return;
1035 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1037 if (!lp_winbind_offline_logon()) {
1038 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1039 return;
1042 primary_domain = find_our_domain();
1044 /* Set our global state as online. */
1045 set_global_winbindd_state_online();
1047 /* Try and mark everything online - delete any negative cache entries
1048 to force a reconnect now. */
1050 for (domain = domain_list(); domain; domain = domain->next) {
1051 if (domain->internal) {
1052 continue;
1054 if (strequal(domain->name, domainname)) {
1055 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1056 winbindd_flush_negative_conn_cache(domain);
1057 set_domain_online_request(domain);
1059 /* we can be in trusted domain, which will contact primary domain
1060 * we have to bring primary domain online in trusted domain process
1061 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1062 * --> contact_domain = find_our_domain()
1063 * */
1064 if (domain != primary_domain) {
1065 winbindd_flush_negative_conn_cache(primary_domain);
1066 set_domain_online_request(primary_domain);
1072 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
1074 struct winbindd_domain *domain;
1075 char *buf = NULL;
1077 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
1078 get_global_winbindd_state_offline() ?
1079 "Offline":"Online")) == NULL) {
1080 return NULL;
1083 for (domain = domain_list(); domain; domain = domain->next) {
1084 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
1085 domain->name,
1086 domain->online ?
1087 "Online":"Offline")) == NULL) {
1088 return NULL;
1092 buf = talloc_asprintf_append_buffer(buf, "\n");
1094 DEBUG(5,("collect_onlinestatus: %s", buf));
1096 return buf;
1099 static void child_msg_onlinestatus(struct messaging_context *msg_ctx,
1100 void *private_data,
1101 uint32_t msg_type,
1102 struct server_id server_id,
1103 DATA_BLOB *data)
1105 TALLOC_CTX *mem_ctx;
1106 const char *message;
1107 struct server_id *sender;
1109 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1111 if (!data->data) {
1112 return;
1115 sender = (struct server_id *)data->data;
1117 mem_ctx = talloc_init("winbind_msg_onlinestatus");
1118 if (mem_ctx == NULL) {
1119 return;
1122 message = collect_onlinestatus(mem_ctx);
1123 if (message == NULL) {
1124 talloc_destroy(mem_ctx);
1125 return;
1128 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
1129 (uint8 *)message, strlen(message) + 1);
1131 talloc_destroy(mem_ctx);
1134 static void child_msg_dump_event_list(struct messaging_context *msg,
1135 void *private_data,
1136 uint32_t msg_type,
1137 struct server_id server_id,
1138 DATA_BLOB *data)
1140 DEBUG(5,("child_msg_dump_event_list received\n"));
1142 dump_event_list(winbind_event_context());
1145 bool winbindd_reinit_after_fork(const char *logfilename)
1147 struct winbindd_domain *domain;
1148 struct winbindd_child *cl;
1150 if (!reinit_after_fork(winbind_messaging_context(),
1151 winbind_event_context(), true)) {
1152 DEBUG(0,("reinit_after_fork() failed\n"));
1153 return false;
1156 close_conns_after_fork();
1158 if (!override_logfile && logfilename) {
1159 lp_set_logfile(logfilename);
1160 reopen_logs();
1163 if (!winbindd_setup_sig_term_handler(false))
1164 return false;
1165 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1166 logfilename))
1167 return false;
1169 /* Don't handle the same messages as our parent. */
1170 messaging_deregister(winbind_messaging_context(),
1171 MSG_SMB_CONF_UPDATED, NULL);
1172 messaging_deregister(winbind_messaging_context(),
1173 MSG_SHUTDOWN, NULL);
1174 messaging_deregister(winbind_messaging_context(),
1175 MSG_WINBIND_OFFLINE, NULL);
1176 messaging_deregister(winbind_messaging_context(),
1177 MSG_WINBIND_ONLINE, NULL);
1178 messaging_deregister(winbind_messaging_context(),
1179 MSG_WINBIND_ONLINESTATUS, NULL);
1180 messaging_deregister(winbind_messaging_context(),
1181 MSG_DUMP_EVENT_LIST, NULL);
1182 messaging_deregister(winbind_messaging_context(),
1183 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1184 messaging_deregister(winbind_messaging_context(),
1185 MSG_DEBUG, NULL);
1187 /* We have destroyed all events in the winbindd_event_context
1188 * in reinit_after_fork(), so clean out all possible pending
1189 * event pointers. */
1191 /* Deal with check_online_events. */
1193 for (domain = domain_list(); domain; domain = domain->next) {
1194 TALLOC_FREE(domain->check_online_event);
1197 /* Ensure we're not handling a credential cache event inherited
1198 * from our parent. */
1200 ccache_remove_all_after_fork();
1202 /* Destroy all possible events in child list. */
1203 for (cl = children; cl != NULL; cl = cl->next) {
1204 struct winbindd_async_request *request;
1206 for (request = cl->requests; request; request = request->next) {
1207 TALLOC_FREE(request->reply_timeout_event);
1209 TALLOC_FREE(cl->lockout_policy_event);
1210 TALLOC_FREE(cl->machine_password_change_event);
1212 /* Children should never be able to send
1213 * each other messages, all messages must
1214 * go through the parent.
1216 cl->pid = (pid_t)0;
1219 * This is a little tricky, children must not
1220 * send an MSG_WINBIND_ONLINE message to idmap_child().
1221 * If we are in a child of our primary domain or
1222 * in the process created by fork_child_dc_connect(),
1223 * and the primary domain cannot go online,
1224 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1225 * periodically to idmap_child().
1227 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1228 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1229 * ---> init_dc_connection() ---> cm_open_connection --->
1230 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1231 * idmap_child(). Disallow children sending messages
1232 * to each other, all messages must go through the parent.
1234 cl = idmap_child();
1235 cl->pid = (pid_t)0;
1237 return true;
1240 static bool fork_domain_child(struct winbindd_child *child)
1242 int fdpair[2];
1243 struct winbindd_cli_state state;
1244 struct winbindd_domain *primary_domain = NULL;
1246 if (child->domain) {
1247 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1248 child->domain->name));
1249 } else {
1250 DEBUG(10, ("fork_domain_child called without domain.\n"));
1253 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1254 DEBUG(0, ("Could not open child pipe: %s\n",
1255 strerror(errno)));
1256 return False;
1259 ZERO_STRUCT(state);
1260 state.pid = sys_getpid();
1262 child->pid = sys_fork();
1264 if (child->pid == -1) {
1265 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1266 return False;
1269 if (child->pid != 0) {
1270 /* Parent */
1271 close(fdpair[0]);
1272 child->next = child->prev = NULL;
1273 DLIST_ADD(children, child);
1274 child->event.fd = fdpair[1];
1275 child->event.flags = 0;
1276 add_fd_event(&child->event);
1277 return True;
1280 /* Child */
1282 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1284 /* Stop zombies in children */
1285 CatchChild();
1287 state.sock = fdpair[0];
1288 close(fdpair[1]);
1290 if (!winbindd_reinit_after_fork(child->logfilename)) {
1291 _exit(0);
1294 /* Handle online/offline messages. */
1295 messaging_register(winbind_messaging_context(), NULL,
1296 MSG_WINBIND_OFFLINE, child_msg_offline);
1297 messaging_register(winbind_messaging_context(), NULL,
1298 MSG_WINBIND_ONLINE, child_msg_online);
1299 messaging_register(winbind_messaging_context(), NULL,
1300 MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus);
1301 messaging_register(winbind_messaging_context(), NULL,
1302 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1303 messaging_register(winbind_messaging_context(), NULL,
1304 MSG_DEBUG, debug_message);
1306 primary_domain = find_our_domain();
1308 if (primary_domain == NULL) {
1309 smb_panic("no primary domain found");
1312 /* It doesn't matter if we allow cache login,
1313 * try to bring domain online after fork. */
1314 if ( child->domain ) {
1315 child->domain->startup = True;
1316 child->domain->startup_time = time(NULL);
1317 /* we can be in primary domain or in trusted domain
1318 * If we are in trusted domain, set the primary domain
1319 * in start-up mode */
1320 if (!(child->domain->internal)) {
1321 set_domain_online_request(child->domain);
1322 if (!(child->domain->primary)) {
1323 primary_domain->startup = True;
1324 primary_domain->startup_time = time(NULL);
1325 set_domain_online_request(primary_domain);
1331 * We are in idmap child, make sure that we set the
1332 * check_online_event to bring primary domain online.
1334 if (child == idmap_child()) {
1335 set_domain_online_request(primary_domain);
1338 /* We might be in the idmap child...*/
1339 if (child->domain && !(child->domain->internal) &&
1340 lp_winbind_offline_logon()) {
1342 set_domain_online_request(child->domain);
1344 if (primary_domain && (primary_domain != child->domain)) {
1345 /* We need to talk to the primary
1346 * domain as well as the trusted
1347 * domain inside a trusted domain
1348 * child.
1349 * See the code in :
1350 * set_dc_type_and_flags_trustinfo()
1351 * for details.
1353 set_domain_online_request(primary_domain);
1356 child->lockout_policy_event = event_add_timed(
1357 winbind_event_context(), NULL, timeval_zero(),
1358 account_lockout_policy_handler,
1359 child);
1362 if (child->domain && child->domain->primary &&
1363 !USE_KERBEROS_KEYTAB &&
1364 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1366 struct timeval next_change;
1368 if (calculate_next_machine_pwd_change(child->domain->name,
1369 &next_change)) {
1370 child->machine_password_change_event = event_add_timed(
1371 winbind_event_context(), NULL, next_change,
1372 machine_password_change_handler,
1373 child);
1377 while (1) {
1379 int ret;
1380 fd_set r_fds;
1381 fd_set w_fds;
1382 int maxfd;
1383 struct timeval t;
1384 struct timeval *tp;
1385 struct timeval now;
1386 TALLOC_CTX *frame = talloc_stackframe();
1388 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1389 TALLOC_FREE(frame);
1390 continue;
1393 GetTimeOfDay(&now);
1395 if (child->domain && child->domain->startup &&
1396 (now.tv_sec > child->domain->startup_time + 30)) {
1397 /* No longer in "startup" mode. */
1398 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1399 child->domain->name ));
1400 child->domain->startup = False;
1403 FD_ZERO(&r_fds);
1404 FD_ZERO(&w_fds);
1405 FD_SET(state.sock, &r_fds);
1406 maxfd = state.sock;
1408 event_add_to_select_args(winbind_event_context(), &now,
1409 &r_fds, &w_fds, &t, &maxfd);
1410 tp = get_timed_events_timeout(winbind_event_context(), &t);
1411 if (tp) {
1412 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1413 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1416 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1418 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1419 /* We got a signal - continue. */
1420 TALLOC_FREE(frame);
1421 continue;
1424 if (ret == 0) {
1425 DEBUG(11,("nothing is ready yet, continue\n"));
1426 TALLOC_FREE(frame);
1427 continue;
1430 if (ret == -1 && errno == EINTR) {
1431 /* We got a signal - continue. */
1432 TALLOC_FREE(frame);
1433 continue;
1436 if (ret == -1 && errno != EINTR) {
1437 DEBUG(0,("select error occured\n"));
1438 TALLOC_FREE(frame);
1439 perror("select");
1440 _exit(1);
1443 /* fetch a request from the main daemon */
1444 child_read_request(&state);
1446 if (state.finished) {
1447 /* we lost contact with our parent */
1448 _exit(0);
1451 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1453 ZERO_STRUCT(state.response);
1454 state.request.null_term = '\0';
1455 child_process_request(child, &state);
1457 SAFE_FREE(state.request.extra_data.data);
1459 cache_store_response(sys_getpid(), &state.response);
1461 SAFE_FREE(state.response.extra_data.data);
1463 /* We just send the result code back, the result
1464 * structure needs to be fetched via the
1465 * winbindd_cache. Hmm. That needs fixing... */
1467 if (write_data(state.sock,
1468 (const char *)&state.response.result,
1469 sizeof(state.response.result)) !=
1470 sizeof(state.response.result)) {
1471 DEBUG(0, ("Could not write result\n"));
1472 exit(1);
1474 TALLOC_FREE(frame);