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"
33 #include "librpc/gen_ndr/messaging.h"
35 #include "../lib/util/select.h"
36 #include "../libcli/security/security.h"
39 #define DBGC_CLASS DBGC_WINBIND
41 extern bool override_logfile;
42 extern struct winbindd_methods cache_methods;
44 /* Read some data from a client connection */
46 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
52 status = read_data(state->sock, (char *)state->request,
53 sizeof(*state->request));
55 if (!NT_STATUS_IS_OK(status)) {
56 DEBUG(3, ("child_read_request: read_data failed: %s\n",
61 if (state->request->extra_len == 0) {
62 state->request->extra_data.data = NULL;
66 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
68 state->request->extra_data.data =
69 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
71 if (state->request->extra_data.data == NULL) {
72 DEBUG(0, ("malloc failed\n"));
73 return NT_STATUS_NO_MEMORY;
76 /* Ensure null termination */
77 state->request->extra_data.data[state->request->extra_len] = '\0';
79 status= read_data(state->sock, state->request->extra_data.data,
80 state->request->extra_len);
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(0, ("Could not read extra data: %s\n",
90 * Do winbind child async request. This is not simply wb_simple_trans. We have
91 * to do the queueing ourselves because while a request is queued, the child
92 * might have crashed, and we have to re-fork it in the _trigger function.
95 struct wb_child_request_state {
96 struct tevent_context *ev;
97 struct winbindd_child *child;
98 struct winbindd_request *request;
99 struct winbindd_response *response;
102 static bool fork_domain_child(struct winbindd_child *child);
104 static void wb_child_request_trigger(struct tevent_req *req,
106 static void wb_child_request_done(struct tevent_req *subreq);
108 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
109 struct tevent_context *ev,
110 struct winbindd_child *child,
111 struct winbindd_request *request)
113 struct tevent_req *req;
114 struct wb_child_request_state *state;
116 req = tevent_req_create(mem_ctx, &state,
117 struct wb_child_request_state);
123 state->child = child;
124 state->request = request;
126 if (!tevent_queue_add(child->queue, ev, req,
127 wb_child_request_trigger, NULL)) {
128 tevent_req_nomem(NULL, req);
129 return tevent_req_post(req, ev);
134 static void wb_child_request_trigger(struct tevent_req *req,
137 struct wb_child_request_state *state = tevent_req_data(
138 req, struct wb_child_request_state);
139 struct tevent_req *subreq;
141 if ((state->child->pid == 0) && (!fork_domain_child(state->child))) {
142 tevent_req_error(req, errno);
146 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
147 state->child->sock, state->request);
148 if (tevent_req_nomem(subreq, req)) {
151 tevent_req_set_callback(subreq, wb_child_request_done, req);
153 if (!tevent_req_set_endtime(req, state->ev,
154 timeval_current_ofs(300, 0))) {
155 tevent_req_nomem(NULL, req);
160 static void wb_child_request_done(struct tevent_req *subreq)
162 struct tevent_req *req = tevent_req_callback_data(
163 subreq, struct tevent_req);
164 struct wb_child_request_state *state = tevent_req_data(
165 req, struct wb_child_request_state);
168 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
171 tevent_req_error(req, err);
174 tevent_req_done(req);
177 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
178 struct winbindd_response **presponse, int *err)
180 struct wb_child_request_state *state = tevent_req_data(
181 req, struct wb_child_request_state);
183 if (tevent_req_is_unix_error(req, err)) {
186 *presponse = talloc_move(mem_ctx, &state->response);
190 struct wb_domain_request_state {
191 struct tevent_context *ev;
192 struct winbindd_domain *domain;
193 struct winbindd_request *request;
194 struct winbindd_request *init_req;
195 struct winbindd_response *response;
198 static void wb_domain_request_gotdc(struct tevent_req *subreq);
199 static void wb_domain_request_initialized(struct tevent_req *subreq);
200 static void wb_domain_request_done(struct tevent_req *subreq);
202 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
203 struct tevent_context *ev,
204 struct winbindd_domain *domain,
205 struct winbindd_request *request)
207 struct tevent_req *req, *subreq;
208 struct wb_domain_request_state *state;
210 req = tevent_req_create(mem_ctx, &state,
211 struct wb_domain_request_state);
216 if (domain->initialized) {
217 subreq = wb_child_request_send(state, ev, &domain->child,
219 if (tevent_req_nomem(subreq, req)) {
220 return tevent_req_post(req, ev);
222 tevent_req_set_callback(subreq, wb_domain_request_done, req);
226 state->domain = domain;
228 state->request = request;
230 state->init_req = talloc_zero(state, struct winbindd_request);
231 if (tevent_req_nomem(state->init_req, req)) {
232 return tevent_req_post(req, ev);
235 if (IS_DC || domain->primary || domain->internal) {
236 /* The primary domain has to find the DC name itself */
237 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
238 fstrcpy(state->init_req->domain_name, domain->name);
239 state->init_req->data.init_conn.is_primary =
240 domain->primary ? true : false;
241 fstrcpy(state->init_req->data.init_conn.dcname, "");
243 subreq = wb_child_request_send(state, ev, &domain->child,
245 if (tevent_req_nomem(subreq, req)) {
246 return tevent_req_post(req, ev);
248 tevent_req_set_callback(subreq, wb_domain_request_initialized,
254 * Ask our DC for a DC name
256 domain = find_our_domain();
258 /* This is *not* the primary domain, let's ask our DC about a DC
261 state->init_req->cmd = WINBINDD_GETDCNAME;
262 fstrcpy(state->init_req->domain_name, domain->name);
264 subreq = wb_child_request_send(state, ev, &domain->child, request);
265 if (tevent_req_nomem(subreq, req)) {
266 return tevent_req_post(req, ev);
268 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
272 static void wb_domain_request_gotdc(struct tevent_req *subreq)
274 struct tevent_req *req = tevent_req_callback_data(
275 subreq, struct tevent_req);
276 struct wb_domain_request_state *state = tevent_req_data(
277 req, struct wb_domain_request_state);
278 struct winbindd_response *response;
281 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
284 tevent_req_error(req, err);
287 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
288 fstrcpy(state->init_req->domain_name, state->domain->name);
289 state->init_req->data.init_conn.is_primary = False;
290 fstrcpy(state->init_req->data.init_conn.dcname,
291 response->data.dc_name);
293 TALLOC_FREE(response);
295 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
297 if (tevent_req_nomem(subreq, req)) {
300 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
303 static void wb_domain_request_initialized(struct tevent_req *subreq)
305 struct tevent_req *req = tevent_req_callback_data(
306 subreq, struct tevent_req);
307 struct wb_domain_request_state *state = tevent_req_data(
308 req, struct wb_domain_request_state);
309 struct winbindd_response *response;
312 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
315 tevent_req_error(req, err);
319 if (!string_to_sid(&state->domain->sid,
320 response->data.domain_info.sid)) {
321 DEBUG(1,("init_child_recv: Could not convert sid %s "
322 "from string\n", response->data.domain_info.sid));
323 tevent_req_error(req, EINVAL);
326 fstrcpy(state->domain->name, response->data.domain_info.name);
327 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
328 state->domain->native_mode = response->data.domain_info.native_mode;
329 state->domain->active_directory =
330 response->data.domain_info.active_directory;
331 state->domain->initialized = true;
333 TALLOC_FREE(response);
335 subreq = wb_child_request_send(state, state->ev, &state->domain->child,
337 if (tevent_req_nomem(subreq, req)) {
340 tevent_req_set_callback(subreq, wb_domain_request_done, req);
343 static void wb_domain_request_done(struct tevent_req *subreq)
345 struct tevent_req *req = tevent_req_callback_data(
346 subreq, struct tevent_req);
347 struct wb_domain_request_state *state = tevent_req_data(
348 req, struct wb_domain_request_state);
351 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
355 tevent_req_error(req, err);
358 tevent_req_done(req);
361 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
362 struct winbindd_response **presponse, int *err)
364 struct wb_domain_request_state *state = tevent_req_data(
365 req, struct wb_domain_request_state);
367 if (tevent_req_is_unix_error(req, err)) {
370 *presponse = talloc_move(mem_ctx, &state->response);
374 static void child_process_request(struct winbindd_child *child,
375 struct winbindd_cli_state *state)
377 struct winbindd_domain *domain = child->domain;
378 const struct winbindd_child_dispatch_table *table = child->table;
380 /* Free response data - we may be interrupted and receive another
381 command before being able to send this data off. */
383 state->response->result = WINBINDD_ERROR;
384 state->response->length = sizeof(struct winbindd_response);
386 /* as all requests in the child are sync, we can use talloc_tos() */
387 state->mem_ctx = talloc_tos();
389 /* Process command */
391 for (; table->name; table++) {
392 if (state->request->cmd == table->struct_cmd) {
393 DEBUG(10,("child_process_request: request fn %s\n",
395 state->response->result = table->struct_fn(domain, state);
400 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
401 (int)state->request->cmd));
402 state->response->result = WINBINDD_ERROR;
405 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
406 const struct winbindd_child_dispatch_table *table,
407 const char *logprefix,
410 if (logprefix && logname) {
411 char *logbase = NULL;
416 if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
417 smb_panic("Internal error: asprintf failed");
420 if ((end = strrchr_m(logbase, '/'))) {
424 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
425 smb_panic("Internal error: asprintf failed");
429 if (asprintf(&child->logfilename, "%s/%s-%s",
430 logbase, logprefix, logname) < 0) {
432 smb_panic("Internal error: asprintf failed");
437 smb_panic("Internal error: logprefix == NULL && "
441 child->domain = domain;
442 child->table = table;
443 child->queue = tevent_queue_create(NULL, "winbind_child");
444 SMB_ASSERT(child->queue != NULL);
445 child->binding_handle = wbint_binding_handle(NULL, domain, child);
446 SMB_ASSERT(child->binding_handle != NULL);
449 static struct winbindd_child *winbindd_children = NULL;
451 void winbind_child_died(pid_t pid)
453 struct winbindd_child *child;
455 for (child = winbindd_children; child != NULL; child = child->next) {
456 if (child->pid == pid) {
462 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
466 /* This will be re-added in fork_domain_child() */
468 DLIST_REMOVE(winbindd_children, child);
475 /* Ensure any negative cache entries with the netbios or realm names are removed. */
477 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
479 flush_negative_conn_cache_for_domain(domain->name);
480 if (*domain->alt_name) {
481 flush_negative_conn_cache_for_domain(domain->alt_name);
486 * Parent winbindd process sets its own debug level first and then
487 * sends a message to all the winbindd children to adjust their debug
488 * level to that of parents.
491 void winbind_msg_debug(struct messaging_context *msg_ctx,
494 struct server_id server_id,
497 struct winbindd_child *child;
499 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
501 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
503 for (child = winbindd_children; child != NULL; child = child->next) {
505 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
506 (unsigned int)child->pid));
508 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
511 strlen((char *) data->data) + 1);
515 /* Set our domains as offline and forward the offline message to our children. */
517 void winbind_msg_offline(struct messaging_context *msg_ctx,
520 struct server_id server_id,
523 struct winbindd_child *child;
524 struct winbindd_domain *domain;
526 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
528 if (!lp_winbind_offline_logon()) {
529 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
533 /* Set our global state as offline. */
534 if (!set_global_winbindd_state_offline()) {
535 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
539 /* Set all our domains as offline. */
540 for (domain = domain_list(); domain; domain = domain->next) {
541 if (domain->internal) {
544 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
545 set_domain_offline(domain);
548 for (child = winbindd_children; child != NULL; child = child->next) {
549 /* Don't send message to internal children. We've already
551 if (!child->domain || winbindd_internal_child(child)) {
555 /* Or internal domains (this should not be possible....) */
556 if (child->domain->internal) {
560 /* Each winbindd child should only process requests for one domain - make sure
561 we only set it online / offline for that domain. */
563 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
564 (unsigned int)child->pid, domain->name ));
566 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
568 (uint8 *)child->domain->name,
569 strlen(child->domain->name)+1);
573 /* Set our domains as online and forward the online message to our children. */
575 void winbind_msg_online(struct messaging_context *msg_ctx,
578 struct server_id server_id,
581 struct winbindd_child *child;
582 struct winbindd_domain *domain;
584 DEBUG(10,("winbind_msg_online: got online message.\n"));
586 if (!lp_winbind_offline_logon()) {
587 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
591 /* Set our global state as online. */
592 set_global_winbindd_state_online();
594 smb_nscd_flush_user_cache();
595 smb_nscd_flush_group_cache();
597 /* Set all our domains as online. */
598 for (domain = domain_list(); domain; domain = domain->next) {
599 if (domain->internal) {
602 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
604 winbindd_flush_negative_conn_cache(domain);
605 set_domain_online_request(domain);
607 /* Send an online message to the idmap child when our
608 primary domain comes back online */
610 if ( domain->primary ) {
611 struct winbindd_child *idmap = idmap_child();
613 if ( idmap->pid != 0 ) {
614 messaging_send_buf(msg_ctx,
615 pid_to_procid(idmap->pid),
617 (uint8 *)domain->name,
618 strlen(domain->name)+1);
623 for (child = winbindd_children; child != NULL; child = child->next) {
624 /* Don't send message to internal childs. */
625 if (!child->domain || winbindd_internal_child(child)) {
629 /* Or internal domains (this should not be possible....) */
630 if (child->domain->internal) {
634 /* Each winbindd child should only process requests for one domain - make sure
635 we only set it online / offline for that domain. */
637 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
638 (unsigned int)child->pid, child->domain->name ));
640 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
642 (uint8 *)child->domain->name,
643 strlen(child->domain->name)+1);
647 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
649 struct winbindd_domain *domain;
652 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
653 get_global_winbindd_state_offline() ?
654 "Offline":"Online")) == NULL) {
658 for (domain = domain_list(); domain; domain = domain->next) {
659 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
662 "Online":"Offline")) == NULL) {
667 buf = talloc_asprintf_append_buffer(buf, "\n");
669 DEBUG(5,("collect_onlinestatus: %s", buf));
674 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
677 struct server_id server_id,
682 struct server_id *sender;
684 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
690 sender = (struct server_id *)data->data;
692 mem_ctx = talloc_init("winbind_msg_onlinestatus");
693 if (mem_ctx == NULL) {
697 message = collect_onlinestatus(mem_ctx);
698 if (message == NULL) {
699 talloc_destroy(mem_ctx);
703 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
704 (uint8 *)message, strlen(message) + 1);
706 talloc_destroy(mem_ctx);
709 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
712 struct server_id server_id,
715 struct winbindd_child *child;
717 DEBUG(10,("winbind_msg_dump_event_list received\n"));
719 dump_event_list(winbind_event_context());
721 for (child = winbindd_children; child != NULL; child = child->next) {
723 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
724 (unsigned int)child->pid));
726 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
733 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
736 struct server_id server_id,
740 const char *message = NULL;
741 struct server_id *sender = NULL;
742 const char *domain = NULL;
745 struct winbindd_domain *dom = NULL;
747 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
749 if (!data || !data->data) {
753 if (data->length < sizeof(struct server_id)) {
757 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
762 sender = (struct server_id *)data->data;
763 if (data->length > sizeof(struct server_id)) {
764 domain = (const char *)data->data+sizeof(struct server_id);
769 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
772 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
773 find_domain_from_name_noinit(domain));
775 talloc_destroy(mem_ctx);
779 messaging_send_buf(msg_ctx, *sender,
780 MSG_WINBIND_DUMP_DOMAIN_LIST,
781 (uint8_t *)message, strlen(message) + 1);
783 talloc_destroy(mem_ctx);
788 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
790 for (dom = domain_list(); dom; dom=dom->next) {
791 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
793 talloc_destroy(mem_ctx);
797 s = talloc_asprintf_append(s, "%s\n", message);
799 talloc_destroy(mem_ctx);
804 status = messaging_send_buf(msg_ctx, *sender,
805 MSG_WINBIND_DUMP_DOMAIN_LIST,
806 (uint8_t *)s, strlen(s) + 1);
807 if (!NT_STATUS_IS_OK(status)) {
808 DEBUG(0,("failed to send message: %s\n",
812 talloc_destroy(mem_ctx);
815 static void account_lockout_policy_handler(struct event_context *ctx,
816 struct timed_event *te,
820 struct winbindd_child *child =
821 (struct winbindd_child *)private_data;
822 TALLOC_CTX *mem_ctx = NULL;
823 struct winbindd_methods *methods;
824 struct samr_DomInfo12 lockout_policy;
827 DEBUG(10,("account_lockout_policy_handler called\n"));
829 TALLOC_FREE(child->lockout_policy_event);
831 if ( !winbindd_can_contact_domain( child->domain ) ) {
832 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
833 "do not have an incoming trust to domain %s\n",
834 child->domain->name));
839 methods = child->domain->methods;
841 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
843 result = NT_STATUS_NO_MEMORY;
845 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
847 TALLOC_FREE(mem_ctx);
849 if (!NT_STATUS_IS_OK(result)) {
850 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
854 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
855 timeval_current_ofs(3600, 0),
856 account_lockout_policy_handler,
860 static time_t get_machine_password_timeout(void)
862 /* until we have gpo support use lp setting */
863 return lp_machine_password_timeout();
866 static bool calculate_next_machine_pwd_change(const char *domain,
869 time_t pass_last_set_time;
875 pw = secrets_fetch_machine_password(domain,
880 DEBUG(0,("cannot fetch own machine password ????"));
886 timeout = get_machine_password_timeout();
888 DEBUG(10,("machine password never expires\n"));
892 tv.tv_sec = pass_last_set_time;
893 DEBUG(10, ("password last changed %s\n",
894 timeval_string(talloc_tos(), &tv, false)));
895 tv.tv_sec += timeout;
896 DEBUGADD(10, ("password valid until %s\n",
897 timeval_string(talloc_tos(), &tv, false)));
899 if (time(NULL) < (pass_last_set_time + timeout)) {
900 next_change = pass_last_set_time + timeout;
901 DEBUG(10,("machine password still valid until: %s\n",
902 http_timestring(talloc_tos(), next_change)));
903 *t = timeval_set(next_change, 0);
905 if (lp_clustering()) {
908 * When having a cluster, we have several
909 * winbinds racing for the password change. In
910 * the machine_password_change_handler()
911 * function we check if someone else was
912 * faster when the event triggers. We add a
913 * 255-second random delay here, so that we
914 * don't run to change the password at the
917 generate_random_buffer(&randbuf, sizeof(randbuf));
918 DEBUG(10, ("adding %d seconds randomness\n",
920 t->tv_sec += randbuf;
925 DEBUG(10,("machine password expired, needs immediate change\n"));
932 static void machine_password_change_handler(struct event_context *ctx,
933 struct timed_event *te,
937 struct winbindd_child *child =
938 (struct winbindd_child *)private_data;
939 struct rpc_pipe_client *netlogon_pipe = NULL;
942 struct timeval next_change;
944 DEBUG(10,("machine_password_change_handler called\n"));
946 TALLOC_FREE(child->machine_password_change_event);
948 if (!calculate_next_machine_pwd_change(child->domain->name,
950 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
954 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
955 timeval_string(talloc_tos(), &next_change, false)));
957 if (!timeval_expired(&next_change)) {
958 DEBUG(10, ("Someone else has already changed the pw\n"));
962 if (!winbindd_can_contact_domain(child->domain)) {
963 DEBUG(10,("machine_password_change_handler: Removing myself since I "
964 "do not have an incoming trust to domain %s\n",
965 child->domain->name));
969 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
970 if (!NT_STATUS_IS_OK(result)) {
971 DEBUG(10,("machine_password_change_handler: "
972 "failed to connect netlogon pipe: %s\n",
977 frame = talloc_stackframe();
979 result = trust_pw_find_change_and_store_it(netlogon_pipe,
981 child->domain->name);
984 DEBUG(10, ("machine_password_change_handler: "
985 "trust_pw_find_change_and_store_it returned %s\n",
988 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
989 DEBUG(3,("machine_password_change_handler: password set returned "
990 "ACCESS_DENIED. Maybe the trust account "
991 "password was changed and we didn't know it. "
992 "Killing connections to domain %s\n",
993 child->domain->name));
994 TALLOC_FREE(child->domain->conn.netlogon_pipe);
997 if (!calculate_next_machine_pwd_change(child->domain->name,
999 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1003 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1004 timeval_string(talloc_tos(), &next_change, false)));
1006 if (!NT_STATUS_IS_OK(result)) {
1009 * In case of failure, give the DC a minute to recover
1011 tmp = timeval_current_ofs(60, 0);
1012 next_change = timeval_max(&next_change, &tmp);
1016 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1018 machine_password_change_handler,
1022 /* Deal with a request to go offline. */
1024 static void child_msg_offline(struct messaging_context *msg,
1027 struct server_id server_id,
1030 struct winbindd_domain *domain;
1031 struct winbindd_domain *primary_domain = NULL;
1032 const char *domainname = (const char *)data->data;
1034 if (data->data == NULL || data->length == 0) {
1038 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1040 if (!lp_winbind_offline_logon()) {
1041 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1045 primary_domain = find_our_domain();
1047 /* Mark the requested domain offline. */
1049 for (domain = domain_list(); domain; domain = domain->next) {
1050 if (domain->internal) {
1053 if (strequal(domain->name, domainname)) {
1054 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1055 set_domain_offline(domain);
1056 /* we are in the trusted domain, set the primary domain
1058 if (domain != primary_domain) {
1059 set_domain_offline(primary_domain);
1065 /* Deal with a request to go online. */
1067 static void child_msg_online(struct messaging_context *msg,
1070 struct server_id server_id,
1073 struct winbindd_domain *domain;
1074 struct winbindd_domain *primary_domain = NULL;
1075 const char *domainname = (const char *)data->data;
1077 if (data->data == NULL || data->length == 0) {
1081 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1083 if (!lp_winbind_offline_logon()) {
1084 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1088 primary_domain = find_our_domain();
1090 /* Set our global state as online. */
1091 set_global_winbindd_state_online();
1093 /* Try and mark everything online - delete any negative cache entries
1094 to force a reconnect now. */
1096 for (domain = domain_list(); domain; domain = domain->next) {
1097 if (domain->internal) {
1100 if (strequal(domain->name, domainname)) {
1101 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1102 winbindd_flush_negative_conn_cache(domain);
1103 set_domain_online_request(domain);
1105 /* we can be in trusted domain, which will contact primary domain
1106 * we have to bring primary domain online in trusted domain process
1107 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1108 * --> contact_domain = find_our_domain()
1110 if (domain != primary_domain) {
1111 winbindd_flush_negative_conn_cache(primary_domain);
1112 set_domain_online_request(primary_domain);
1118 static void child_msg_dump_event_list(struct messaging_context *msg,
1121 struct server_id server_id,
1124 DEBUG(5,("child_msg_dump_event_list received\n"));
1126 dump_event_list(winbind_event_context());
1129 bool winbindd_reinit_after_fork(const char *logfilename)
1131 struct winbindd_domain *domain;
1132 struct winbindd_child *cl;
1135 status = reinit_after_fork(
1136 winbind_messaging_context(),
1137 winbind_event_context(),
1140 if (!NT_STATUS_IS_OK(status)) {
1141 DEBUG(0,("reinit_after_fork() failed\n"));
1145 close_conns_after_fork();
1147 if (!override_logfile && logfilename) {
1148 lp_set_logfile(logfilename);
1152 if (!winbindd_setup_sig_term_handler(false))
1154 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1158 /* Stop zombies in children */
1161 /* Don't handle the same messages as our parent. */
1162 messaging_deregister(winbind_messaging_context(),
1163 MSG_SMB_CONF_UPDATED, NULL);
1164 messaging_deregister(winbind_messaging_context(),
1165 MSG_SHUTDOWN, NULL);
1166 messaging_deregister(winbind_messaging_context(),
1167 MSG_WINBIND_OFFLINE, NULL);
1168 messaging_deregister(winbind_messaging_context(),
1169 MSG_WINBIND_ONLINE, NULL);
1170 messaging_deregister(winbind_messaging_context(),
1171 MSG_WINBIND_ONLINESTATUS, NULL);
1172 messaging_deregister(winbind_messaging_context(),
1173 MSG_DUMP_EVENT_LIST, NULL);
1174 messaging_deregister(winbind_messaging_context(),
1175 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1176 messaging_deregister(winbind_messaging_context(),
1179 /* We have destroyed all events in the winbindd_event_context
1180 * in reinit_after_fork(), so clean out all possible pending
1181 * event pointers. */
1183 /* Deal with check_online_events. */
1185 for (domain = domain_list(); domain; domain = domain->next) {
1186 TALLOC_FREE(domain->check_online_event);
1189 /* Ensure we're not handling a credential cache event inherited
1190 * from our parent. */
1192 ccache_remove_all_after_fork();
1194 /* Destroy all possible events in child list. */
1195 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1196 TALLOC_FREE(cl->lockout_policy_event);
1197 TALLOC_FREE(cl->machine_password_change_event);
1199 /* Children should never be able to send
1200 * each other messages, all messages must
1201 * go through the parent.
1206 * This is a little tricky, children must not
1207 * send an MSG_WINBIND_ONLINE message to idmap_child().
1208 * If we are in a child of our primary domain or
1209 * in the process created by fork_child_dc_connect(),
1210 * and the primary domain cannot go online,
1211 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1212 * periodically to idmap_child().
1214 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1215 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1216 * ---> init_dc_connection() ---> cm_open_connection --->
1217 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1218 * idmap_child(). Disallow children sending messages
1219 * to each other, all messages must go through the parent.
1228 * In a child there will be only one domain, reference that here.
1230 static struct winbindd_domain *child_domain;
1232 struct winbindd_domain *wb_child_domain(void)
1234 return child_domain;
1237 static bool fork_domain_child(struct winbindd_child *child)
1240 struct winbindd_cli_state state;
1241 struct winbindd_request request;
1242 struct winbindd_response response;
1243 struct winbindd_domain *primary_domain = NULL;
1245 if (child->domain) {
1246 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1247 child->domain->name));
1249 DEBUG(10, ("fork_domain_child called without domain.\n"));
1252 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1253 DEBUG(0, ("Could not open child pipe: %s\n",
1259 state.pid = sys_getpid();
1260 state.request = &request;
1261 state.response = &response;
1263 child->pid = sys_fork();
1265 if (child->pid == -1) {
1266 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1270 if (child->pid != 0) {
1273 child->next = child->prev = NULL;
1274 DLIST_ADD(winbindd_children, child);
1275 child->sock = fdpair[1];
1280 child_domain = child->domain;
1282 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1284 state.sock = fdpair[0];
1287 if (!winbindd_reinit_after_fork(child->logfilename)) {
1291 /* Handle online/offline messages. */
1292 messaging_register(winbind_messaging_context(), NULL,
1293 MSG_WINBIND_OFFLINE, child_msg_offline);
1294 messaging_register(winbind_messaging_context(), NULL,
1295 MSG_WINBIND_ONLINE, child_msg_online);
1296 messaging_register(winbind_messaging_context(), NULL,
1297 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1298 messaging_register(winbind_messaging_context(), NULL,
1299 MSG_DEBUG, debug_message);
1300 messaging_register(winbind_messaging_context(), NULL,
1301 MSG_WINBIND_IP_DROPPED,
1302 winbind_msg_ip_dropped);
1305 primary_domain = find_our_domain();
1307 if (primary_domain == NULL) {
1308 smb_panic("no primary domain found");
1311 /* It doesn't matter if we allow cache login,
1312 * try to bring domain online after fork. */
1313 if ( child->domain ) {
1314 child->domain->startup = True;
1315 child->domain->startup_time = time_mono(NULL);
1316 /* we can be in primary domain or in trusted domain
1317 * If we are in trusted domain, set the primary domain
1318 * in start-up mode */
1319 if (!(child->domain->internal)) {
1320 set_domain_online_request(child->domain);
1321 if (!(child->domain->primary)) {
1322 primary_domain->startup = True;
1323 primary_domain->startup_time = time_mono(NULL);
1324 set_domain_online_request(primary_domain);
1330 * We are in idmap child, make sure that we set the
1331 * check_online_event to bring primary domain online.
1333 if (child == idmap_child()) {
1334 set_domain_online_request(primary_domain);
1337 /* We might be in the idmap child...*/
1338 if (child->domain && !(child->domain->internal) &&
1339 lp_winbind_offline_logon()) {
1341 set_domain_online_request(child->domain);
1343 if (primary_domain && (primary_domain != child->domain)) {
1344 /* We need to talk to the primary
1345 * domain as well as the trusted
1346 * domain inside a trusted domain
1349 * set_dc_type_and_flags_trustinfo()
1352 set_domain_online_request(primary_domain);
1355 child->lockout_policy_event = event_add_timed(
1356 winbind_event_context(), NULL, timeval_zero(),
1357 account_lockout_policy_handler,
1361 if (child->domain && child->domain->primary &&
1362 !USE_KERBEROS_KEYTAB &&
1363 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1365 struct timeval next_change;
1367 if (calculate_next_machine_pwd_change(child->domain->name,
1369 child->machine_password_change_event = event_add_timed(
1370 winbind_event_context(), NULL, next_change,
1371 machine_password_change_handler,
1385 TALLOC_CTX *frame = talloc_stackframe();
1386 struct iovec iov[2];
1390 if (run_events(winbind_event_context(), &ret, NULL, NULL)) {
1397 if (child->domain && child->domain->startup &&
1398 (time_mono(NULL) > child->domain->startup_time + 30)) {
1399 /* No longer in "startup" mode. */
1400 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1401 child->domain->name ));
1402 child->domain->startup = False;
1407 FD_SET(state.sock, &r_fds);
1411 * Initialize this high as event_add_to_select_args()
1412 * uses a timeval_min() on this and next_event. Fix
1413 * from Roel van Meer <rolek@alt001.com>.
1418 event_add_to_select_args(winbind_event_context(), &now,
1419 &r_fds, &w_fds, &t, &maxfd);
1420 tp = get_timed_events_timeout(winbind_event_context(), &t);
1422 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1423 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1426 ret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, tp);
1428 if (run_events(winbind_event_context(), &ret, &r_fds, &w_fds)) {
1429 /* We got a signal - continue. */
1435 DEBUG(11,("nothing is ready yet, continue\n"));
1440 if (ret == -1 && errno == EINTR) {
1441 /* We got a signal - continue. */
1446 if (ret == -1 && errno != EINTR) {
1447 DEBUG(0,("select error occured\n"));
1453 /* fetch a request from the main daemon */
1454 status = child_read_request(&state);
1456 if (!NT_STATUS_IS_OK(status)) {
1457 /* we lost contact with our parent */
1461 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1463 ZERO_STRUCTP(state.response);
1464 state.request->null_term = '\0';
1465 state.mem_ctx = frame;
1466 child_process_request(child, &state);
1468 DEBUG(4, ("Finished processing child request %d\n",
1469 (int)state.request->cmd));
1471 SAFE_FREE(state.request->extra_data.data);
1473 iov[0].iov_base = (void *)state.response;
1474 iov[0].iov_len = sizeof(struct winbindd_response);
1477 if (state.response->length > sizeof(struct winbindd_response)) {
1479 (void *)state.response->extra_data.data;
1480 iov[1].iov_len = state.response->length-iov[0].iov_len;
1484 DEBUG(10, ("Writing %d bytes to parent\n",
1485 (int)state.response->length));
1487 if (write_data_iov(state.sock, iov, iov_count) !=
1488 state.response->length) {
1489 DEBUG(0, ("Could not write result\n"));
1496 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1499 struct server_id server_id,
1502 struct winbindd_child *child;
1504 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1508 for (child = winbindd_children; child != NULL; child = child->next) {
1509 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1510 msg_type, data->data, data->length);