2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2002
7 Copyright (C) Volker Lendecke 2004,2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * We fork a child per domain to be able to act non-blocking in the main
25 * winbind daemon. A domain controller thousands of miles away being being
26 * slow replying with a 10.000 user list should not hold up netlogon calls
27 * that can be handled locally.
32 #include "../../nsswitch/libwbclient/wbc_async.h"
35 #define DBGC_CLASS DBGC_WINBIND
37 extern bool override_logfile;
38 extern struct winbindd_methods cache_methods;
40 /* Read some data from a client connection */
42 static void child_read_request(struct winbindd_cli_state *state)
48 status = read_data(state->sock, (char *)state->request,
49 sizeof(*state->request));
51 if (!NT_STATUS_IS_OK(status)) {
52 DEBUG(3, ("child_read_request: read_data failed: %s\n",
54 state->finished = True;
58 if (state->request->extra_len == 0) {
59 state->request->extra_data.data = NULL;
63 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
65 state->request->extra_data.data =
66 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
68 if (state->request->extra_data.data == NULL) {
69 DEBUG(0, ("malloc failed\n"));
70 state->finished = True;
74 /* Ensure null termination */
75 state->request->extra_data.data[state->request->extra_len] = '\0';
77 status= read_data(state->sock, state->request->extra_data.data,
78 state->request->extra_len);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(0, ("Could not read extra data: %s\n",
83 state->finished = True;
89 * Do winbind child async request. This is not simply wb_simple_trans. We have
90 * to do the queueing ourselves because while a request is queued, the child
91 * might have crashed, and we have to re-fork it in the _trigger function.
94 struct wb_child_request_state {
95 struct tevent_context *ev;
96 struct winbindd_child *child;
97 struct winbindd_request *request;
98 struct winbindd_response *response;
101 static bool fork_domain_child(struct winbindd_child *child);
103 static void wb_child_request_trigger(struct tevent_req *req,
105 static void wb_child_request_done(struct tevent_req *subreq);
107 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
108 struct tevent_context *ev,
109 struct winbindd_child *child,
110 struct winbindd_request *request)
112 struct tevent_req *req;
113 struct wb_child_request_state *state;
115 req = tevent_req_create(mem_ctx, &state,
116 struct wb_child_request_state);
122 state->child = child;
123 state->request = request;
125 if (!tevent_queue_add(child->queue, ev, req,
126 wb_child_request_trigger, NULL)) {
127 tevent_req_nomem(NULL, req);
128 return tevent_req_post(req, ev);
133 static void wb_child_request_trigger(struct tevent_req *req,
136 struct wb_child_request_state *state = tevent_req_data(
137 req, struct wb_child_request_state);
138 struct tevent_req *subreq;
140 if ((state->child->pid == 0) && (!fork_domain_child(state->child))) {
141 tevent_req_error(req, errno);
145 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
146 state->child->sock, state->request);
147 if (tevent_req_nomem(subreq, req)) {
150 tevent_req_set_callback(subreq, wb_child_request_done, req);
152 if (!tevent_req_set_endtime(req, state->ev,
153 timeval_current_ofs(300, 0))) {
154 tevent_req_nomem(NULL, req);
159 static void wb_child_request_done(struct tevent_req *subreq)
161 struct tevent_req *req = tevent_req_callback_data(
162 subreq, struct tevent_req);
163 struct wb_child_request_state *state = tevent_req_data(
164 req, struct wb_child_request_state);
167 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
170 tevent_req_error(req, err);
173 tevent_req_done(req);
176 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
177 struct winbindd_response **presponse, int *err)
179 struct wb_child_request_state *state = tevent_req_data(
180 req, struct wb_child_request_state);
182 if (tevent_req_is_unix_error(req, err)) {
185 if (state->response->result != WINBINDD_OK) {
186 *err = EIO; /* EIO doesn't fit, but what would be better? */
189 *presponse = talloc_move(mem_ctx, &state->response);
194 * Machinery for async requests sent to children. You set up a
195 * winbindd_request, select a child to query, and issue a async_request
196 * call. When the request is completed, the callback function you specified is
197 * called back with the private pointer you gave to async_request.
200 struct winbindd_async_request {
201 struct winbindd_async_request *next, *prev;
203 struct winbindd_child *child;
204 struct winbindd_response *response;
205 void (*continuation)(void *private_data, bool success);
206 struct timed_event *reply_timeout_event;
207 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
208 a restart of the child (child->pid != child_pid). */
212 static bool fork_domain_child(struct winbindd_child *child);
213 static void async_request_done(struct tevent_req *req);
215 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
216 struct winbindd_request *request,
217 struct winbindd_response *response,
218 void (*continuation)(void *private_data, bool success),
221 struct winbindd_async_request *state;
222 struct tevent_req *req;
224 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
226 (child->domain != NULL) ? child->domain->name : "''"));
228 state = talloc(mem_ctx, struct winbindd_async_request);
230 DEBUG(0, ("talloc failed\n"));
231 continuation(private_data, False);
235 state->mem_ctx = mem_ctx;
236 state->child = child;
237 state->reply_timeout_event = NULL;
238 state->response = response;
239 state->continuation = continuation;
240 state->private_data = private_data;
242 request->pid = child->pid;
244 req = wb_child_request_send(state, winbind_event_context(),
247 DEBUG(0, ("wb_child_request_send failed\n"));
248 continuation(private_data, false);
251 tevent_req_set_callback(req, async_request_done, state);
254 static void async_request_done(struct tevent_req *req)
256 struct winbindd_async_request *state = tevent_req_callback_data(
257 req, struct winbindd_async_request);
258 struct winbindd_response *response;
261 ret = wb_child_request_recv(req, state, &response, &err);
264 DEBUG(2, ("wb_child_request_recv failed: %s\n",
266 state->continuation(state->private_data, false);
269 *state->response = *response;
270 state->continuation(state->private_data, true);
273 struct domain_request_state {
275 struct winbindd_domain *domain;
276 struct winbindd_request *request;
277 struct winbindd_response *response;
278 void (*continuation)(void *private_data_data, bool success);
279 void *private_data_data;
282 static void domain_init_recv(void *private_data_data, bool success);
284 void async_domain_request(TALLOC_CTX *mem_ctx,
285 struct winbindd_domain *domain,
286 struct winbindd_request *request,
287 struct winbindd_response *response,
288 void (*continuation)(void *private_data_data, bool success),
289 void *private_data_data)
291 struct domain_request_state *state;
293 if (domain->initialized) {
294 async_request(mem_ctx, &domain->child, request, response,
295 continuation, private_data_data);
299 state = TALLOC_P(mem_ctx, struct domain_request_state);
301 DEBUG(0, ("talloc failed\n"));
302 continuation(private_data_data, False);
306 state->mem_ctx = mem_ctx;
307 state->domain = domain;
308 state->request = request;
309 state->response = response;
310 state->continuation = continuation;
311 state->private_data_data = private_data_data;
313 init_child_connection(domain, domain_init_recv, state);
316 static void domain_init_recv(void *private_data_data, bool success)
318 struct domain_request_state *state =
319 talloc_get_type_abort(private_data_data, struct domain_request_state);
322 DEBUG(5, ("Domain init returned an error\n"));
323 state->continuation(state->private_data_data, False);
327 async_request(state->mem_ctx, &state->domain->child,
328 state->request, state->response,
329 state->continuation, state->private_data_data);
332 static void recvfrom_child(void *private_data_data, bool success)
334 struct winbindd_cli_state *state =
335 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
336 enum winbindd_result result = state->response.result;
338 /* This is an optimization: The child has written directly to the
339 * response buffer. The request itself is still in pending state,
340 * state that in the result code. */
342 state->response.result = WINBINDD_PENDING;
344 if ((!success) || (result != WINBINDD_OK)) {
345 request_error(state);
352 void sendto_child(struct winbindd_cli_state *state,
353 struct winbindd_child *child)
355 async_request(state->mem_ctx, child, state->request,
356 &state->response, recvfrom_child, state);
359 void sendto_domain(struct winbindd_cli_state *state,
360 struct winbindd_domain *domain)
362 async_domain_request(state->mem_ctx, domain,
363 state->request, &state->response,
364 recvfrom_child, state);
367 static void child_process_request(struct winbindd_child *child,
368 struct winbindd_cli_state *state)
370 struct winbindd_domain *domain = child->domain;
371 const struct winbindd_child_dispatch_table *table = child->table;
373 /* Free response data - we may be interrupted and receive another
374 command before being able to send this data off. */
376 state->response.result = WINBINDD_ERROR;
377 state->response.length = sizeof(struct winbindd_response);
379 /* as all requests in the child are sync, we can use talloc_tos() */
380 state->mem_ctx = talloc_tos();
382 /* Process command */
384 for (; table->name; table++) {
385 if (state->request->cmd == table->struct_cmd) {
386 DEBUG(10,("child_process_request: request fn %s\n",
388 state->response.result = table->struct_fn(domain, state);
393 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
394 (int)state->request->cmd));
395 state->response.result = WINBINDD_ERROR;
398 void setup_child(struct winbindd_child *child,
399 const struct winbindd_child_dispatch_table *table,
400 const char *logprefix,
403 if (logprefix && logname) {
404 if (asprintf(&child->logfilename, "%s/%s-%s",
405 get_dyn_LOGFILEBASE(), logprefix, logname) < 0) {
406 smb_panic("Internal error: asprintf failed");
409 smb_panic("Internal error: logprefix == NULL && "
413 child->domain = NULL;
414 child->table = table;
415 child->queue = tevent_queue_create(NULL, "winbind_child");
416 SMB_ASSERT(child->queue != NULL);
419 struct winbindd_child *children = NULL;
421 void winbind_child_died(pid_t pid)
423 struct winbindd_child *child;
425 for (child = children; child != NULL; child = child->next) {
426 if (child->pid == pid) {
432 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
436 /* This will be re-added in fork_domain_child() */
438 DLIST_REMOVE(children, child);
445 /* Ensure any negative cache entries with the netbios or realm names are removed. */
447 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
449 flush_negative_conn_cache_for_domain(domain->name);
450 if (*domain->alt_name) {
451 flush_negative_conn_cache_for_domain(domain->alt_name);
456 * Parent winbindd process sets its own debug level first and then
457 * sends a message to all the winbindd children to adjust their debug
458 * level to that of parents.
461 void winbind_msg_debug(struct messaging_context *msg_ctx,
464 struct server_id server_id,
467 struct winbindd_child *child;
469 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
471 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
473 for (child = children; child != NULL; child = child->next) {
475 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
476 (unsigned int)child->pid));
478 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
481 strlen((char *) data->data) + 1);
485 /* Set our domains as offline and forward the offline message to our children. */
487 void winbind_msg_offline(struct messaging_context *msg_ctx,
490 struct server_id server_id,
493 struct winbindd_child *child;
494 struct winbindd_domain *domain;
496 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
498 if (!lp_winbind_offline_logon()) {
499 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
503 /* Set our global state as offline. */
504 if (!set_global_winbindd_state_offline()) {
505 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
509 /* Set all our domains as offline. */
510 for (domain = domain_list(); domain; domain = domain->next) {
511 if (domain->internal) {
514 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
515 set_domain_offline(domain);
518 for (child = children; child != NULL; child = child->next) {
519 /* Don't send message to internal childs. We've already
521 if (!child->domain || winbindd_internal_child(child)) {
525 /* Or internal domains (this should not be possible....) */
526 if (child->domain->internal) {
530 /* Each winbindd child should only process requests for one domain - make sure
531 we only set it online / offline for that domain. */
533 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
534 (unsigned int)child->pid, domain->name ));
536 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
538 (uint8 *)child->domain->name,
539 strlen(child->domain->name)+1);
543 /* Set our domains as online and forward the online message to our children. */
545 void winbind_msg_online(struct messaging_context *msg_ctx,
548 struct server_id server_id,
551 struct winbindd_child *child;
552 struct winbindd_domain *domain;
554 DEBUG(10,("winbind_msg_online: got online message.\n"));
556 if (!lp_winbind_offline_logon()) {
557 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
561 /* Set our global state as online. */
562 set_global_winbindd_state_online();
564 smb_nscd_flush_user_cache();
565 smb_nscd_flush_group_cache();
567 /* Set all our domains as online. */
568 for (domain = domain_list(); domain; domain = domain->next) {
569 if (domain->internal) {
572 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
574 winbindd_flush_negative_conn_cache(domain);
575 set_domain_online_request(domain);
577 /* Send an online message to the idmap child when our
578 primary domain comes back online */
580 if ( domain->primary ) {
581 struct winbindd_child *idmap = idmap_child();
583 if ( idmap->pid != 0 ) {
584 messaging_send_buf(msg_ctx,
585 pid_to_procid(idmap->pid),
587 (uint8 *)domain->name,
588 strlen(domain->name)+1);
593 for (child = children; child != NULL; child = child->next) {
594 /* Don't send message to internal childs. */
595 if (!child->domain || winbindd_internal_child(child)) {
599 /* Or internal domains (this should not be possible....) */
600 if (child->domain->internal) {
604 /* Each winbindd child should only process requests for one domain - make sure
605 we only set it online / offline for that domain. */
607 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
608 (unsigned int)child->pid, child->domain->name ));
610 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
612 (uint8 *)child->domain->name,
613 strlen(child->domain->name)+1);
617 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
619 struct winbindd_domain *domain;
622 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
623 get_global_winbindd_state_offline() ?
624 "Offline":"Online")) == NULL) {
628 for (domain = domain_list(); domain; domain = domain->next) {
629 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
632 "Online":"Offline")) == NULL) {
637 buf = talloc_asprintf_append_buffer(buf, "\n");
639 DEBUG(5,("collect_onlinestatus: %s", buf));
644 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
647 struct server_id server_id,
652 struct server_id *sender;
654 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
660 sender = (struct server_id *)data->data;
662 mem_ctx = talloc_init("winbind_msg_onlinestatus");
663 if (mem_ctx == NULL) {
667 message = collect_onlinestatus(mem_ctx);
668 if (message == NULL) {
669 talloc_destroy(mem_ctx);
673 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
674 (uint8 *)message, strlen(message) + 1);
676 talloc_destroy(mem_ctx);
679 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
682 struct server_id server_id,
685 struct winbindd_child *child;
687 DEBUG(10,("winbind_msg_dump_event_list received\n"));
689 dump_event_list(winbind_event_context());
691 for (child = children; child != NULL; child = child->next) {
693 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
694 (unsigned int)child->pid));
696 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
703 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
706 struct server_id server_id,
710 const char *message = NULL;
711 struct server_id *sender = NULL;
712 const char *domain = NULL;
715 struct winbindd_domain *dom = NULL;
717 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
719 if (!data || !data->data) {
723 if (data->length < sizeof(struct server_id)) {
727 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
732 sender = (struct server_id *)data->data;
733 if (data->length > sizeof(struct server_id)) {
734 domain = (const char *)data->data+sizeof(struct server_id);
739 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
742 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
743 find_domain_from_name_noinit(domain));
745 talloc_destroy(mem_ctx);
749 messaging_send_buf(msg_ctx, *sender,
750 MSG_WINBIND_DUMP_DOMAIN_LIST,
751 (uint8_t *)message, strlen(message) + 1);
753 talloc_destroy(mem_ctx);
758 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
760 for (dom = domain_list(); dom; dom=dom->next) {
761 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
763 talloc_destroy(mem_ctx);
767 s = talloc_asprintf_append(s, "%s\n", message);
769 talloc_destroy(mem_ctx);
774 status = messaging_send_buf(msg_ctx, *sender,
775 MSG_WINBIND_DUMP_DOMAIN_LIST,
776 (uint8_t *)s, strlen(s) + 1);
777 if (!NT_STATUS_IS_OK(status)) {
778 DEBUG(0,("failed to send message: %s\n",
782 talloc_destroy(mem_ctx);
785 static void account_lockout_policy_handler(struct event_context *ctx,
786 struct timed_event *te,
790 struct winbindd_child *child =
791 (struct winbindd_child *)private_data;
792 TALLOC_CTX *mem_ctx = NULL;
793 struct winbindd_methods *methods;
794 struct samr_DomInfo12 lockout_policy;
797 DEBUG(10,("account_lockout_policy_handler called\n"));
799 TALLOC_FREE(child->lockout_policy_event);
801 if ( !winbindd_can_contact_domain( child->domain ) ) {
802 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
803 "do not have an incoming trust to domain %s\n",
804 child->domain->name));
809 methods = child->domain->methods;
811 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
813 result = NT_STATUS_NO_MEMORY;
815 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
817 TALLOC_FREE(mem_ctx);
819 if (!NT_STATUS_IS_OK(result)) {
820 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
824 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
825 timeval_current_ofs(3600, 0),
826 account_lockout_policy_handler,
830 static time_t get_machine_password_timeout(void)
832 /* until we have gpo support use lp setting */
833 return lp_machine_password_timeout();
836 static bool calculate_next_machine_pwd_change(const char *domain,
839 time_t pass_last_set_time;
844 pw = secrets_fetch_machine_password(domain,
849 DEBUG(0,("cannot fetch own machine password ????"));
855 timeout = get_machine_password_timeout();
857 DEBUG(10,("machine password never expires\n"));
861 if (time(NULL) < (pass_last_set_time + timeout)) {
862 next_change = pass_last_set_time + timeout;
863 DEBUG(10,("machine password still valid until: %s\n",
864 http_timestring(talloc_tos(), next_change)));
865 *t = timeval_set(next_change, 0);
869 DEBUG(10,("machine password expired, needs immediate change\n"));
876 static void machine_password_change_handler(struct event_context *ctx,
877 struct timed_event *te,
881 struct winbindd_child *child =
882 (struct winbindd_child *)private_data;
883 struct rpc_pipe_client *netlogon_pipe = NULL;
886 struct timeval next_change;
888 DEBUG(10,("machine_password_change_handler called\n"));
890 TALLOC_FREE(child->machine_password_change_event);
892 if (!calculate_next_machine_pwd_change(child->domain->name,
897 if (!winbindd_can_contact_domain(child->domain)) {
898 DEBUG(10,("machine_password_change_handler: Removing myself since I "
899 "do not have an incoming trust to domain %s\n",
900 child->domain->name));
904 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
905 if (!NT_STATUS_IS_OK(result)) {
906 DEBUG(10,("machine_password_change_handler: "
907 "failed to connect netlogon pipe: %s\n",
912 frame = talloc_stackframe();
914 result = trust_pw_find_change_and_store_it(netlogon_pipe,
916 child->domain->name);
919 if (!NT_STATUS_IS_OK(result)) {
920 DEBUG(10,("machine_password_change_handler: "
921 "failed to change machine password: %s\n",
924 DEBUG(10,("machine_password_change_handler: "
925 "successfully changed machine password\n"));
928 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
930 machine_password_change_handler,
934 /* Deal with a request to go offline. */
936 static void child_msg_offline(struct messaging_context *msg,
939 struct server_id server_id,
942 struct winbindd_domain *domain;
943 struct winbindd_domain *primary_domain = NULL;
944 const char *domainname = (const char *)data->data;
946 if (data->data == NULL || data->length == 0) {
950 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
952 if (!lp_winbind_offline_logon()) {
953 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
957 primary_domain = find_our_domain();
959 /* Mark the requested domain offline. */
961 for (domain = domain_list(); domain; domain = domain->next) {
962 if (domain->internal) {
965 if (strequal(domain->name, domainname)) {
966 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
967 set_domain_offline(domain);
968 /* we are in the trusted domain, set the primary domain
970 if (domain != primary_domain) {
971 set_domain_offline(primary_domain);
977 /* Deal with a request to go online. */
979 static void child_msg_online(struct messaging_context *msg,
982 struct server_id server_id,
985 struct winbindd_domain *domain;
986 struct winbindd_domain *primary_domain = NULL;
987 const char *domainname = (const char *)data->data;
989 if (data->data == NULL || data->length == 0) {
993 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
995 if (!lp_winbind_offline_logon()) {
996 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1000 primary_domain = find_our_domain();
1002 /* Set our global state as online. */
1003 set_global_winbindd_state_online();
1005 /* Try and mark everything online - delete any negative cache entries
1006 to force a reconnect now. */
1008 for (domain = domain_list(); domain; domain = domain->next) {
1009 if (domain->internal) {
1012 if (strequal(domain->name, domainname)) {
1013 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1014 winbindd_flush_negative_conn_cache(domain);
1015 set_domain_online_request(domain);
1017 /* we can be in trusted domain, which will contact primary domain
1018 * we have to bring primary domain online in trusted domain process
1019 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1020 * --> contact_domain = find_our_domain()
1022 if (domain != primary_domain) {
1023 winbindd_flush_negative_conn_cache(primary_domain);
1024 set_domain_online_request(primary_domain);
1030 static void child_msg_dump_event_list(struct messaging_context *msg,
1033 struct server_id server_id,
1036 DEBUG(5,("child_msg_dump_event_list received\n"));
1038 dump_event_list(winbind_event_context());
1041 bool winbindd_reinit_after_fork(const char *logfilename)
1043 struct winbindd_domain *domain;
1044 struct winbindd_child *cl;
1046 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1047 winbind_event_context(),
1049 DEBUG(0,("reinit_after_fork() failed\n"));
1053 close_conns_after_fork();
1055 if (!override_logfile && logfilename) {
1056 lp_set_logfile(logfilename);
1060 if (!winbindd_setup_sig_term_handler(false))
1062 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1066 /* Don't handle the same messages as our parent. */
1067 messaging_deregister(winbind_messaging_context(),
1068 MSG_SMB_CONF_UPDATED, NULL);
1069 messaging_deregister(winbind_messaging_context(),
1070 MSG_SHUTDOWN, NULL);
1071 messaging_deregister(winbind_messaging_context(),
1072 MSG_WINBIND_OFFLINE, NULL);
1073 messaging_deregister(winbind_messaging_context(),
1074 MSG_WINBIND_ONLINE, NULL);
1075 messaging_deregister(winbind_messaging_context(),
1076 MSG_WINBIND_ONLINESTATUS, NULL);
1077 messaging_deregister(winbind_messaging_context(),
1078 MSG_DUMP_EVENT_LIST, NULL);
1079 messaging_deregister(winbind_messaging_context(),
1080 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1081 messaging_deregister(winbind_messaging_context(),
1084 /* We have destroyed all events in the winbindd_event_context
1085 * in reinit_after_fork(), so clean out all possible pending
1086 * event pointers. */
1088 /* Deal with check_online_events. */
1090 for (domain = domain_list(); domain; domain = domain->next) {
1091 TALLOC_FREE(domain->check_online_event);
1094 /* Ensure we're not handling a credential cache event inherited
1095 * from our parent. */
1097 ccache_remove_all_after_fork();
1099 /* Destroy all possible events in child list. */
1100 for (cl = children; cl != NULL; cl = cl->next) {
1101 TALLOC_FREE(cl->lockout_policy_event);
1102 TALLOC_FREE(cl->machine_password_change_event);
1104 /* Children should never be able to send
1105 * each other messages, all messages must
1106 * go through the parent.
1111 * This is a little tricky, children must not
1112 * send an MSG_WINBIND_ONLINE message to idmap_child().
1113 * If we are in a child of our primary domain or
1114 * in the process created by fork_child_dc_connect(),
1115 * and the primary domain cannot go online,
1116 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1117 * periodically to idmap_child().
1119 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1120 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1121 * ---> init_dc_connection() ---> cm_open_connection --->
1122 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1123 * idmap_child(). Disallow children sending messages
1124 * to each other, all messages must go through the parent.
1132 static bool fork_domain_child(struct winbindd_child *child)
1135 struct winbindd_cli_state state;
1136 struct winbindd_domain *primary_domain = NULL;
1138 if (child->domain) {
1139 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1140 child->domain->name));
1142 DEBUG(10, ("fork_domain_child called without domain.\n"));
1145 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1146 DEBUG(0, ("Could not open child pipe: %s\n",
1152 state.pid = sys_getpid();
1153 state.request = &state._request;
1155 child->pid = sys_fork();
1157 if (child->pid == -1) {
1158 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1162 if (child->pid != 0) {
1165 child->next = child->prev = NULL;
1166 DLIST_ADD(children, child);
1167 child->sock = fdpair[1];
1173 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1175 /* Stop zombies in children */
1178 state.sock = fdpair[0];
1181 if (!winbindd_reinit_after_fork(child->logfilename)) {
1185 /* Handle online/offline messages. */
1186 messaging_register(winbind_messaging_context(), NULL,
1187 MSG_WINBIND_OFFLINE, child_msg_offline);
1188 messaging_register(winbind_messaging_context(), NULL,
1189 MSG_WINBIND_ONLINE, child_msg_online);
1190 messaging_register(winbind_messaging_context(), NULL,
1191 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1192 messaging_register(winbind_messaging_context(), NULL,
1193 MSG_DEBUG, debug_message);
1195 primary_domain = find_our_domain();
1197 if (primary_domain == NULL) {
1198 smb_panic("no primary domain found");
1201 /* It doesn't matter if we allow cache login,
1202 * try to bring domain online after fork. */
1203 if ( child->domain ) {
1204 child->domain->startup = True;
1205 child->domain->startup_time = time(NULL);
1206 /* we can be in primary domain or in trusted domain
1207 * If we are in trusted domain, set the primary domain
1208 * in start-up mode */
1209 if (!(child->domain->internal)) {
1210 set_domain_online_request(child->domain);
1211 if (!(child->domain->primary)) {
1212 primary_domain->startup = True;
1213 primary_domain->startup_time = time(NULL);
1214 set_domain_online_request(primary_domain);
1220 * We are in idmap child, make sure that we set the
1221 * check_online_event to bring primary domain online.
1223 if (child == idmap_child()) {
1224 set_domain_online_request(primary_domain);
1227 /* We might be in the idmap child...*/
1228 if (child->domain && !(child->domain->internal) &&
1229 lp_winbind_offline_logon()) {
1231 set_domain_online_request(child->domain);
1233 if (primary_domain && (primary_domain != child->domain)) {
1234 /* We need to talk to the primary
1235 * domain as well as the trusted
1236 * domain inside a trusted domain
1239 * set_dc_type_and_flags_trustinfo()
1242 set_domain_online_request(primary_domain);
1245 child->lockout_policy_event = event_add_timed(
1246 winbind_event_context(), NULL, timeval_zero(),
1247 account_lockout_policy_handler,
1251 if (child->domain && child->domain->primary &&
1252 !USE_KERBEROS_KEYTAB &&
1253 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1255 struct timeval next_change;
1257 if (calculate_next_machine_pwd_change(child->domain->name,
1259 child->machine_password_change_event = event_add_timed(
1260 winbind_event_context(), NULL, next_change,
1261 machine_password_change_handler,
1275 TALLOC_CTX *frame = talloc_stackframe();
1276 struct iovec iov[2];
1279 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1286 if (child->domain && child->domain->startup &&
1287 (now.tv_sec > child->domain->startup_time + 30)) {
1288 /* No longer in "startup" mode. */
1289 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1290 child->domain->name ));
1291 child->domain->startup = False;
1296 FD_SET(state.sock, &r_fds);
1299 event_add_to_select_args(winbind_event_context(), &now,
1300 &r_fds, &w_fds, &t, &maxfd);
1301 tp = get_timed_events_timeout(winbind_event_context(), &t);
1303 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1304 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1307 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1309 if (run_events(winbind_event_context(), ret, &r_fds, &w_fds)) {
1310 /* We got a signal - continue. */
1316 DEBUG(11,("nothing is ready yet, continue\n"));
1321 if (ret == -1 && errno == EINTR) {
1322 /* We got a signal - continue. */
1327 if (ret == -1 && errno != EINTR) {
1328 DEBUG(0,("select error occured\n"));
1334 /* fetch a request from the main daemon */
1335 child_read_request(&state);
1337 if (state.finished) {
1338 /* we lost contact with our parent */
1342 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1344 ZERO_STRUCT(state.response);
1345 state.request->null_term = '\0';
1346 child_process_request(child, &state);
1348 DEBUG(4, ("Finished processing child request %d\n",
1349 (int)state.request->cmd));
1351 SAFE_FREE(state.request->extra_data.data);
1353 iov[0].iov_base = (void *)&state.response;
1354 iov[0].iov_len = sizeof(struct winbindd_response);
1357 if (state.response.length > sizeof(struct winbindd_response)) {
1359 (void *)state.response.extra_data.data;
1360 iov[1].iov_len = state.response.length-iov[0].iov_len;
1364 DEBUG(10, ("Writing %d bytes to parent\n",
1365 (int)state.response.length));
1367 if (write_data_iov(state.sock, iov, iov_count) !=
1368 state.response.length) {
1369 DEBUG(0, ("Could not write result\n"));