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/wb_reqtrans.h"
34 #include "../lib/util/select.h"
35 #include "../libcli/security/security.h"
36 #include "system/select.h"
38 #include "../lib/util/tevent_unix.h"
39 #include "lib/param/loadparm.h"
42 #define DBGC_CLASS DBGC_WINBIND
44 extern bool override_logfile;
45 extern struct winbindd_methods cache_methods;
47 static struct winbindd_child *winbindd_children = NULL;
49 /* Read some data from a client connection */
51 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
57 status = read_data(state->sock, (char *)state->request,
58 sizeof(*state->request));
60 if (!NT_STATUS_IS_OK(status)) {
61 DEBUG(3, ("child_read_request: read_data failed: %s\n",
66 if (state->request->extra_len == 0) {
67 state->request->extra_data.data = NULL;
71 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
73 state->request->extra_data.data =
74 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
76 if (state->request->extra_data.data == NULL) {
77 DEBUG(0, ("malloc failed\n"));
78 return NT_STATUS_NO_MEMORY;
81 /* Ensure null termination */
82 state->request->extra_data.data[state->request->extra_len] = '\0';
84 status= read_data(state->sock, state->request->extra_data.data,
85 state->request->extra_len);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(0, ("Could not read extra data: %s\n",
95 * Do winbind child async request. This is not simply wb_simple_trans. We have
96 * to do the queueing ourselves because while a request is queued, the child
97 * might have crashed, and we have to re-fork it in the _trigger function.
100 struct wb_child_request_state {
101 struct tevent_context *ev;
102 struct winbindd_child *child;
103 struct winbindd_request *request;
104 struct winbindd_response *response;
107 static bool fork_domain_child(struct winbindd_child *child);
109 static void wb_child_request_trigger(struct tevent_req *req,
111 static void wb_child_request_done(struct tevent_req *subreq);
113 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
114 struct tevent_context *ev,
115 struct winbindd_child *child,
116 struct winbindd_request *request)
118 struct tevent_req *req;
119 struct wb_child_request_state *state;
121 req = tevent_req_create(mem_ctx, &state,
122 struct wb_child_request_state);
128 state->child = child;
129 state->request = request;
131 if (!tevent_queue_add(child->queue, ev, req,
132 wb_child_request_trigger, NULL)) {
134 return tevent_req_post(req, ev);
139 static void wb_child_request_trigger(struct tevent_req *req,
142 struct wb_child_request_state *state = tevent_req_data(
143 req, struct wb_child_request_state);
144 struct tevent_req *subreq;
146 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
147 tevent_req_error(req, errno);
151 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
152 state->child->sock, state->request);
153 if (tevent_req_nomem(subreq, req)) {
156 tevent_req_set_callback(subreq, wb_child_request_done, req);
157 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
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);
172 * The basic parent/child communication broke, close
175 close(state->child->sock);
176 state->child->sock = -1;
177 DLIST_REMOVE(winbindd_children, state->child);
178 tevent_req_error(req, err);
181 tevent_req_done(req);
184 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
185 struct winbindd_response **presponse, int *err)
187 struct wb_child_request_state *state = tevent_req_data(
188 req, struct wb_child_request_state);
190 if (tevent_req_is_unix_error(req, err)) {
193 *presponse = talloc_move(mem_ctx, &state->response);
197 static bool winbindd_child_busy(struct winbindd_child *child)
199 return tevent_queue_length(child->queue) > 0;
202 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
206 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
207 if (!winbindd_child_busy(&domain->children[i])) {
208 return &domain->children[i];
215 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
217 struct winbindd_child *result;
219 result = find_idle_child(domain);
220 if (result != NULL) {
223 return &domain->children[rand() % lp_winbind_max_domain_connections()];
226 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
228 struct winbindd_child *child;
230 child = choose_domain_child(domain);
231 return child->binding_handle;
234 struct wb_domain_request_state {
235 struct tevent_context *ev;
236 struct winbindd_domain *domain;
237 struct winbindd_child *child;
238 struct winbindd_request *request;
239 struct winbindd_request *init_req;
240 struct winbindd_response *response;
243 static void wb_domain_request_gotdc(struct tevent_req *subreq);
244 static void wb_domain_request_initialized(struct tevent_req *subreq);
245 static void wb_domain_request_done(struct tevent_req *subreq);
247 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
248 struct tevent_context *ev,
249 struct winbindd_domain *domain,
250 struct winbindd_request *request)
252 struct tevent_req *req, *subreq;
253 struct wb_domain_request_state *state;
255 req = tevent_req_create(mem_ctx, &state,
256 struct wb_domain_request_state);
261 state->child = choose_domain_child(domain);
263 if (domain->initialized) {
264 subreq = wb_child_request_send(state, ev, state->child,
266 if (tevent_req_nomem(subreq, req)) {
267 return tevent_req_post(req, ev);
269 tevent_req_set_callback(subreq, wb_domain_request_done, req);
273 state->domain = domain;
275 state->request = request;
277 state->init_req = talloc_zero(state, struct winbindd_request);
278 if (tevent_req_nomem(state->init_req, req)) {
279 return tevent_req_post(req, ev);
282 if (IS_DC || domain->primary || domain->internal) {
283 /* The primary domain has to find the DC name itself */
284 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
285 fstrcpy(state->init_req->domain_name, domain->name);
286 state->init_req->data.init_conn.is_primary = domain->primary;
287 fstrcpy(state->init_req->data.init_conn.dcname, "");
289 subreq = wb_child_request_send(state, ev, state->child,
291 if (tevent_req_nomem(subreq, req)) {
292 return tevent_req_post(req, ev);
294 tevent_req_set_callback(subreq, wb_domain_request_initialized,
300 * Ask our DC for a DC name
302 domain = find_our_domain();
304 /* This is *not* the primary domain, let's ask our DC about a DC
307 state->init_req->cmd = WINBINDD_GETDCNAME;
308 fstrcpy(state->init_req->domain_name, domain->name);
310 subreq = wb_child_request_send(state, ev, state->child, request);
311 if (tevent_req_nomem(subreq, req)) {
312 return tevent_req_post(req, ev);
314 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
318 static void wb_domain_request_gotdc(struct tevent_req *subreq)
320 struct tevent_req *req = tevent_req_callback_data(
321 subreq, struct tevent_req);
322 struct wb_domain_request_state *state = tevent_req_data(
323 req, struct wb_domain_request_state);
324 struct winbindd_response *response;
327 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
330 tevent_req_error(req, err);
333 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
334 fstrcpy(state->init_req->domain_name, state->domain->name);
335 state->init_req->data.init_conn.is_primary = False;
336 fstrcpy(state->init_req->data.init_conn.dcname,
337 response->data.dc_name);
339 TALLOC_FREE(response);
341 subreq = wb_child_request_send(state, state->ev, state->child,
343 if (tevent_req_nomem(subreq, req)) {
346 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
349 static void wb_domain_request_initialized(struct tevent_req *subreq)
351 struct tevent_req *req = tevent_req_callback_data(
352 subreq, struct tevent_req);
353 struct wb_domain_request_state *state = tevent_req_data(
354 req, struct wb_domain_request_state);
355 struct winbindd_response *response;
358 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
361 tevent_req_error(req, err);
365 if (!string_to_sid(&state->domain->sid,
366 response->data.domain_info.sid)) {
367 DEBUG(1,("init_child_recv: Could not convert sid %s "
368 "from string\n", response->data.domain_info.sid));
369 tevent_req_error(req, EINVAL);
373 talloc_free(state->domain->name);
374 state->domain->name = talloc_strdup(state->domain,
375 response->data.domain_info.name);
376 if (state->domain->name == NULL) {
377 tevent_req_error(req, ENOMEM);
381 talloc_free(state->domain->alt_name);
382 state->domain->alt_name = talloc_strdup(state->domain,
383 response->data.domain_info.alt_name);
384 if (state->domain->alt_name == NULL) {
385 tevent_req_error(req, ENOMEM);
389 state->domain->native_mode = response->data.domain_info.native_mode;
390 state->domain->active_directory =
391 response->data.domain_info.active_directory;
392 state->domain->initialized = true;
394 TALLOC_FREE(response);
396 subreq = wb_child_request_send(state, state->ev, state->child,
398 if (tevent_req_nomem(subreq, req)) {
401 tevent_req_set_callback(subreq, wb_domain_request_done, req);
404 static void wb_domain_request_done(struct tevent_req *subreq)
406 struct tevent_req *req = tevent_req_callback_data(
407 subreq, struct tevent_req);
408 struct wb_domain_request_state *state = tevent_req_data(
409 req, struct wb_domain_request_state);
412 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
416 tevent_req_error(req, err);
419 tevent_req_done(req);
422 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
423 struct winbindd_response **presponse, int *err)
425 struct wb_domain_request_state *state = tevent_req_data(
426 req, struct wb_domain_request_state);
428 if (tevent_req_is_unix_error(req, err)) {
431 *presponse = talloc_move(mem_ctx, &state->response);
435 static void child_process_request(struct winbindd_child *child,
436 struct winbindd_cli_state *state)
438 struct winbindd_domain *domain = child->domain;
439 const struct winbindd_child_dispatch_table *table = child->table;
441 /* Free response data - we may be interrupted and receive another
442 command before being able to send this data off. */
444 state->response->result = WINBINDD_ERROR;
445 state->response->length = sizeof(struct winbindd_response);
447 /* as all requests in the child are sync, we can use talloc_tos() */
448 state->mem_ctx = talloc_tos();
450 /* Process command */
452 for (; table->name; table++) {
453 if (state->request->cmd == table->struct_cmd) {
454 DEBUG(10,("child_process_request: request fn %s\n",
456 state->response->result = table->struct_fn(domain, state);
461 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
462 (int)state->request->cmd));
463 state->response->result = WINBINDD_ERROR;
466 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
467 const struct winbindd_child_dispatch_table *table,
468 const char *logprefix,
471 if (logprefix && logname) {
472 char *logbase = NULL;
474 if (*lp_logfile(talloc_tos())) {
477 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
478 smb_panic("Internal error: asprintf failed");
481 if ((end = strrchr_m(logbase, '/'))) {
485 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
486 smb_panic("Internal error: asprintf failed");
490 if (asprintf(&child->logfilename, "%s/%s-%s",
491 logbase, logprefix, logname) < 0) {
493 smb_panic("Internal error: asprintf failed");
498 smb_panic("Internal error: logprefix == NULL && "
503 child->domain = domain;
504 child->table = table;
505 child->queue = tevent_queue_create(NULL, "winbind_child");
506 SMB_ASSERT(child->queue != NULL);
507 child->binding_handle = wbint_binding_handle(NULL, domain, child);
508 SMB_ASSERT(child->binding_handle != NULL);
511 void winbind_child_died(pid_t pid)
513 struct winbindd_child *child;
515 for (child = winbindd_children; child != NULL; child = child->next) {
516 if (child->pid == pid) {
522 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
526 /* This will be re-added in fork_domain_child() */
528 DLIST_REMOVE(winbindd_children, child);
531 if (child->sock != -1) {
537 /* Ensure any negative cache entries with the netbios or realm names are removed. */
539 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
541 flush_negative_conn_cache_for_domain(domain->name);
542 if (*domain->alt_name) {
543 flush_negative_conn_cache_for_domain(domain->alt_name);
548 * Parent winbindd process sets its own debug level first and then
549 * sends a message to all the winbindd children to adjust their debug
550 * level to that of parents.
553 void winbind_msg_debug(struct messaging_context *msg_ctx,
556 struct server_id server_id,
559 struct winbindd_child *child;
561 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
563 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
565 for (child = winbindd_children; child != NULL; child = child->next) {
567 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
568 (unsigned int)child->pid));
570 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
573 strlen((char *) data->data) + 1);
577 /* Set our domains as offline and forward the offline message to our children. */
579 void winbind_msg_offline(struct messaging_context *msg_ctx,
582 struct server_id server_id,
585 struct winbindd_child *child;
586 struct winbindd_domain *domain;
588 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
590 if (!lp_winbind_offline_logon()) {
591 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
595 /* Set our global state as offline. */
596 if (!set_global_winbindd_state_offline()) {
597 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
601 /* Set all our domains as offline. */
602 for (domain = domain_list(); domain; domain = domain->next) {
603 if (domain->internal) {
606 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
607 set_domain_offline(domain);
610 for (child = winbindd_children; child != NULL; child = child->next) {
611 /* Don't send message to internal children. We've already
613 if (!child->domain || winbindd_internal_child(child)) {
617 /* Or internal domains (this should not be possible....) */
618 if (child->domain->internal) {
622 /* Each winbindd child should only process requests for one domain - make sure
623 we only set it online / offline for that domain. */
625 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
626 (unsigned int)child->pid, domain->name ));
628 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
630 (const uint8_t *)child->domain->name,
631 strlen(child->domain->name)+1);
635 /* Set our domains as online and forward the online message to our children. */
637 void winbind_msg_online(struct messaging_context *msg_ctx,
640 struct server_id server_id,
643 struct winbindd_child *child;
644 struct winbindd_domain *domain;
646 DEBUG(10,("winbind_msg_online: got online message.\n"));
648 if (!lp_winbind_offline_logon()) {
649 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
653 /* Set our global state as online. */
654 set_global_winbindd_state_online();
656 smb_nscd_flush_user_cache();
657 smb_nscd_flush_group_cache();
659 /* Set all our domains as online. */
660 for (domain = domain_list(); domain; domain = domain->next) {
661 if (domain->internal) {
664 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
666 winbindd_flush_negative_conn_cache(domain);
667 set_domain_online_request(domain);
669 /* Send an online message to the idmap child when our
670 primary domain comes back online */
672 if ( domain->primary ) {
673 struct winbindd_child *idmap = idmap_child();
675 if ( idmap->pid != 0 ) {
676 messaging_send_buf(msg_ctx,
677 pid_to_procid(idmap->pid),
679 (const uint8_t *)domain->name,
680 strlen(domain->name)+1);
685 for (child = winbindd_children; child != NULL; child = child->next) {
686 /* Don't send message to internal childs. */
687 if (!child->domain || winbindd_internal_child(child)) {
691 /* Or internal domains (this should not be possible....) */
692 if (child->domain->internal) {
696 /* Each winbindd child should only process requests for one domain - make sure
697 we only set it online / offline for that domain. */
699 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
700 (unsigned int)child->pid, child->domain->name ));
702 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
704 (const uint8_t *)child->domain->name,
705 strlen(child->domain->name)+1);
709 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
711 struct winbindd_domain *domain;
714 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
715 get_global_winbindd_state_offline() ?
716 "Offline":"Online")) == NULL) {
720 for (domain = domain_list(); domain; domain = domain->next) {
721 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
724 "Online":"Offline")) == NULL) {
729 buf = talloc_asprintf_append_buffer(buf, "\n");
731 DEBUG(5,("collect_onlinestatus: %s", buf));
736 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
739 struct server_id server_id,
744 struct server_id *sender;
746 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
752 sender = (struct server_id *)data->data;
754 mem_ctx = talloc_init("winbind_msg_onlinestatus");
755 if (mem_ctx == NULL) {
759 message = collect_onlinestatus(mem_ctx);
760 if (message == NULL) {
761 talloc_destroy(mem_ctx);
765 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
766 (const uint8 *)message, strlen(message) + 1);
768 talloc_destroy(mem_ctx);
771 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
774 struct server_id server_id,
777 struct winbindd_child *child;
779 DEBUG(10,("winbind_msg_dump_event_list received\n"));
781 dump_event_list(winbind_event_context());
783 for (child = winbindd_children; child != NULL; child = child->next) {
785 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
786 (unsigned int)child->pid));
788 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
795 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
798 struct server_id server_id,
802 const char *message = NULL;
803 struct server_id *sender = NULL;
804 const char *domain = NULL;
807 struct winbindd_domain *dom = NULL;
809 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
811 if (!data || !data->data) {
815 if (data->length < sizeof(struct server_id)) {
819 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
824 sender = (struct server_id *)data->data;
825 if (data->length > sizeof(struct server_id)) {
826 domain = (const char *)data->data+sizeof(struct server_id);
831 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
834 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
835 find_domain_from_name_noinit(domain));
837 talloc_destroy(mem_ctx);
841 messaging_send_buf(msg_ctx, *sender,
842 MSG_WINBIND_DUMP_DOMAIN_LIST,
843 (const uint8_t *)message, strlen(message) + 1);
845 talloc_destroy(mem_ctx);
850 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
852 for (dom = domain_list(); dom; dom=dom->next) {
853 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
855 talloc_destroy(mem_ctx);
859 s = talloc_asprintf_append(s, "%s\n", message);
861 talloc_destroy(mem_ctx);
866 status = messaging_send_buf(msg_ctx, *sender,
867 MSG_WINBIND_DUMP_DOMAIN_LIST,
868 (uint8_t *)s, strlen(s) + 1);
869 if (!NT_STATUS_IS_OK(status)) {
870 DEBUG(0,("failed to send message: %s\n",
874 talloc_destroy(mem_ctx);
877 static void account_lockout_policy_handler(struct tevent_context *ctx,
878 struct tevent_timer *te,
882 struct winbindd_child *child =
883 (struct winbindd_child *)private_data;
884 TALLOC_CTX *mem_ctx = NULL;
885 struct winbindd_methods *methods;
886 struct samr_DomInfo12 lockout_policy;
889 DEBUG(10,("account_lockout_policy_handler called\n"));
891 TALLOC_FREE(child->lockout_policy_event);
893 if ( !winbindd_can_contact_domain( child->domain ) ) {
894 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
895 "do not have an incoming trust to domain %s\n",
896 child->domain->name));
901 methods = child->domain->methods;
903 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
905 result = NT_STATUS_NO_MEMORY;
907 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
909 TALLOC_FREE(mem_ctx);
911 if (!NT_STATUS_IS_OK(result)) {
912 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
916 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
917 timeval_current_ofs(3600, 0),
918 account_lockout_policy_handler,
922 static time_t get_machine_password_timeout(void)
924 /* until we have gpo support use lp setting */
925 return lp_machine_password_timeout();
928 static bool calculate_next_machine_pwd_change(const char *domain,
931 time_t pass_last_set_time;
937 pw = secrets_fetch_machine_password(domain,
942 DEBUG(0,("cannot fetch own machine password ????"));
948 timeout = get_machine_password_timeout();
950 DEBUG(10,("machine password never expires\n"));
954 tv.tv_sec = pass_last_set_time;
955 DEBUG(10, ("password last changed %s\n",
956 timeval_string(talloc_tos(), &tv, false)));
957 tv.tv_sec += timeout;
958 DEBUGADD(10, ("password valid until %s\n",
959 timeval_string(talloc_tos(), &tv, false)));
961 if (time(NULL) < (pass_last_set_time + timeout)) {
962 next_change = pass_last_set_time + timeout;
963 DEBUG(10,("machine password still valid until: %s\n",
964 http_timestring(talloc_tos(), next_change)));
965 *t = timeval_set(next_change, 0);
967 if (lp_clustering()) {
970 * When having a cluster, we have several
971 * winbinds racing for the password change. In
972 * the machine_password_change_handler()
973 * function we check if someone else was
974 * faster when the event triggers. We add a
975 * 255-second random delay here, so that we
976 * don't run to change the password at the
979 generate_random_buffer(&randbuf, sizeof(randbuf));
980 DEBUG(10, ("adding %d seconds randomness\n",
982 t->tv_sec += randbuf;
987 DEBUG(10,("machine password expired, needs immediate change\n"));
994 static void machine_password_change_handler(struct tevent_context *ctx,
995 struct tevent_timer *te,
999 struct winbindd_child *child =
1000 (struct winbindd_child *)private_data;
1001 struct rpc_pipe_client *netlogon_pipe = NULL;
1004 struct timeval next_change;
1006 DEBUG(10,("machine_password_change_handler called\n"));
1008 TALLOC_FREE(child->machine_password_change_event);
1010 if (!calculate_next_machine_pwd_change(child->domain->name,
1012 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1016 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1017 timeval_string(talloc_tos(), &next_change, false)));
1019 if (!timeval_expired(&next_change)) {
1020 DEBUG(10, ("Someone else has already changed the pw\n"));
1024 if (!winbindd_can_contact_domain(child->domain)) {
1025 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1026 "do not have an incoming trust to domain %s\n",
1027 child->domain->name));
1031 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1032 if (!NT_STATUS_IS_OK(result)) {
1033 DEBUG(10,("machine_password_change_handler: "
1034 "failed to connect netlogon pipe: %s\n",
1035 nt_errstr(result)));
1039 frame = talloc_stackframe();
1041 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1043 child->domain->name);
1046 DEBUG(10, ("machine_password_change_handler: "
1047 "trust_pw_find_change_and_store_it returned %s\n",
1048 nt_errstr(result)));
1050 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1051 DEBUG(3,("machine_password_change_handler: password set returned "
1052 "ACCESS_DENIED. Maybe the trust account "
1053 "password was changed and we didn't know it. "
1054 "Killing connections to domain %s\n",
1055 child->domain->name));
1056 TALLOC_FREE(child->domain->conn.netlogon_pipe);
1059 if (!calculate_next_machine_pwd_change(child->domain->name,
1061 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1065 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1066 timeval_string(talloc_tos(), &next_change, false)));
1068 if (!NT_STATUS_IS_OK(result)) {
1071 * In case of failure, give the DC a minute to recover
1073 tmp = timeval_current_ofs(60, 0);
1074 next_change = timeval_max(&next_change, &tmp);
1078 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1080 machine_password_change_handler,
1084 /* Deal with a request to go offline. */
1086 static void child_msg_offline(struct messaging_context *msg,
1089 struct server_id server_id,
1092 struct winbindd_domain *domain;
1093 struct winbindd_domain *primary_domain = NULL;
1094 const char *domainname = (const char *)data->data;
1096 if (data->data == NULL || data->length == 0) {
1100 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1102 if (!lp_winbind_offline_logon()) {
1103 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1107 primary_domain = find_our_domain();
1109 /* Mark the requested domain offline. */
1111 for (domain = domain_list(); domain; domain = domain->next) {
1112 if (domain->internal) {
1115 if (strequal(domain->name, domainname)) {
1116 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1117 set_domain_offline(domain);
1118 /* we are in the trusted domain, set the primary domain
1120 if (domain != primary_domain) {
1121 set_domain_offline(primary_domain);
1127 /* Deal with a request to go online. */
1129 static void child_msg_online(struct messaging_context *msg,
1132 struct server_id server_id,
1135 struct winbindd_domain *domain;
1136 struct winbindd_domain *primary_domain = NULL;
1137 const char *domainname = (const char *)data->data;
1139 if (data->data == NULL || data->length == 0) {
1143 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1145 if (!lp_winbind_offline_logon()) {
1146 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1150 primary_domain = find_our_domain();
1152 /* Set our global state as online. */
1153 set_global_winbindd_state_online();
1155 /* Try and mark everything online - delete any negative cache entries
1156 to force a reconnect now. */
1158 for (domain = domain_list(); domain; domain = domain->next) {
1159 if (domain->internal) {
1162 if (strequal(domain->name, domainname)) {
1163 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1164 winbindd_flush_negative_conn_cache(domain);
1165 set_domain_online_request(domain);
1167 /* we can be in trusted domain, which will contact primary domain
1168 * we have to bring primary domain online in trusted domain process
1169 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1170 * --> contact_domain = find_our_domain()
1172 if (domain != primary_domain) {
1173 winbindd_flush_negative_conn_cache(primary_domain);
1174 set_domain_online_request(primary_domain);
1180 static void child_msg_dump_event_list(struct messaging_context *msg,
1183 struct server_id server_id,
1186 DEBUG(5,("child_msg_dump_event_list received\n"));
1188 dump_event_list(winbind_event_context());
1191 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1192 const char *logfilename)
1194 struct winbindd_domain *domain;
1195 struct winbindd_child *cl;
1198 status = reinit_after_fork(
1199 winbind_messaging_context(),
1200 winbind_event_context(),
1202 if (!NT_STATUS_IS_OK(status)) {
1203 DEBUG(0,("reinit_after_fork() failed\n"));
1207 close_conns_after_fork();
1209 if (!override_logfile && logfilename) {
1210 lp_set_logfile(logfilename);
1214 if (!winbindd_setup_sig_term_handler(false))
1215 return NT_STATUS_NO_MEMORY;
1216 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1218 return NT_STATUS_NO_MEMORY;
1220 /* Stop zombies in children */
1223 /* Don't handle the same messages as our parent. */
1224 messaging_deregister(winbind_messaging_context(),
1225 MSG_SMB_CONF_UPDATED, NULL);
1226 messaging_deregister(winbind_messaging_context(),
1227 MSG_SHUTDOWN, NULL);
1228 messaging_deregister(winbind_messaging_context(),
1229 MSG_WINBIND_OFFLINE, NULL);
1230 messaging_deregister(winbind_messaging_context(),
1231 MSG_WINBIND_ONLINE, NULL);
1232 messaging_deregister(winbind_messaging_context(),
1233 MSG_WINBIND_ONLINESTATUS, NULL);
1234 messaging_deregister(winbind_messaging_context(),
1235 MSG_DUMP_EVENT_LIST, NULL);
1236 messaging_deregister(winbind_messaging_context(),
1237 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1238 messaging_deregister(winbind_messaging_context(),
1241 /* We have destroyed all events in the winbindd_event_context
1242 * in reinit_after_fork(), so clean out all possible pending
1243 * event pointers. */
1245 /* Deal with check_online_events. */
1247 for (domain = domain_list(); domain; domain = domain->next) {
1248 TALLOC_FREE(domain->check_online_event);
1251 /* Ensure we're not handling a credential cache event inherited
1252 * from our parent. */
1254 ccache_remove_all_after_fork();
1256 /* Destroy all possible events in child list. */
1257 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1258 TALLOC_FREE(cl->lockout_policy_event);
1259 TALLOC_FREE(cl->machine_password_change_event);
1261 /* Children should never be able to send
1262 * each other messages, all messages must
1263 * go through the parent.
1268 * Close service sockets to all other children
1270 if ((cl != myself) && (cl->sock != -1)) {
1276 * This is a little tricky, children must not
1277 * send an MSG_WINBIND_ONLINE message to idmap_child().
1278 * If we are in a child of our primary domain or
1279 * in the process created by fork_child_dc_connect(),
1280 * and the primary domain cannot go online,
1281 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1282 * periodically to idmap_child().
1284 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1285 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1286 * ---> init_dc_connection() ---> cm_open_connection --->
1287 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1288 * idmap_child(). Disallow children sending messages
1289 * to each other, all messages must go through the parent.
1294 return NT_STATUS_OK;
1298 * In a child there will be only one domain, reference that here.
1300 static struct winbindd_domain *child_domain;
1302 struct winbindd_domain *wb_child_domain(void)
1304 return child_domain;
1307 struct child_handler_state {
1308 struct winbindd_child *child;
1309 struct winbindd_cli_state cli;
1312 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1313 uint16_t flags, void *private_data)
1315 struct child_handler_state *state =
1316 (struct child_handler_state *)private_data;
1318 struct iovec iov[2];
1321 /* fetch a request from the main daemon */
1322 status = child_read_request(&state->cli);
1324 if (!NT_STATUS_IS_OK(status)) {
1325 /* we lost contact with our parent */
1329 DEBUG(4,("child daemon request %d\n",
1330 (int)state->cli.request->cmd));
1332 ZERO_STRUCTP(state->cli.response);
1333 state->cli.request->null_term = '\0';
1334 state->cli.mem_ctx = talloc_tos();
1335 child_process_request(state->child, &state->cli);
1337 DEBUG(4, ("Finished processing child request %d\n",
1338 (int)state->cli.request->cmd));
1340 SAFE_FREE(state->cli.request->extra_data.data);
1342 iov[0].iov_base = (void *)state->cli.response;
1343 iov[0].iov_len = sizeof(struct winbindd_response);
1346 if (state->cli.response->length >
1347 sizeof(struct winbindd_response)) {
1349 (void *)state->cli.response->extra_data.data;
1350 iov[1].iov_len = state->cli.response->length-iov[0].iov_len;
1354 DEBUG(10, ("Writing %d bytes to parent\n",
1355 (int)state->cli.response->length));
1357 if (write_data_iov(state->cli.sock, iov, iov_count) !=
1358 state->cli.response->length) {
1359 DEBUG(0, ("Could not write result\n"));
1364 static bool fork_domain_child(struct winbindd_child *child)
1367 struct child_handler_state state;
1368 struct winbindd_request request;
1369 struct winbindd_response response;
1370 struct winbindd_domain *primary_domain = NULL;
1373 struct tevent_fd *fde;
1375 if (child->domain) {
1376 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1377 child->domain->name));
1379 DEBUG(10, ("fork_domain_child called without domain.\n"));
1382 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1383 DEBUG(0, ("Could not open child pipe: %s\n",
1389 state.child = child;
1390 state.cli.pid = getpid();
1391 state.cli.request = &request;
1392 state.cli.response = &response;
1394 child->pid = fork();
1396 if (child->pid == -1) {
1397 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1401 if (child->pid != 0) {
1407 nread = sys_read(fdpair[1], &status, sizeof(status));
1408 if (nread != sizeof(status)) {
1409 DEBUG(1, ("fork_domain_child: Could not read child status: "
1410 "nread=%d, error=%s\n", (int)nread,
1415 if (!NT_STATUS_IS_OK(status)) {
1416 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1417 nt_errstr(status)));
1422 child->next = child->prev = NULL;
1423 DLIST_ADD(winbindd_children, child);
1424 child->sock = fdpair[1];
1429 child_domain = child->domain;
1431 DEBUG(10, ("Child process %d\n", (int)getpid()));
1433 state.cli.sock = fdpair[0];
1436 status = winbindd_reinit_after_fork(child, child->logfilename);
1438 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1439 if (nwritten != sizeof(status)) {
1440 DEBUG(1, ("fork_domain_child: Could not write status: "
1441 "nwritten=%d, error=%s\n", (int)nwritten,
1445 if (!NT_STATUS_IS_OK(status)) {
1446 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1447 nt_errstr(status)));
1451 /* Handle online/offline messages. */
1452 messaging_register(winbind_messaging_context(), NULL,
1453 MSG_WINBIND_OFFLINE, child_msg_offline);
1454 messaging_register(winbind_messaging_context(), NULL,
1455 MSG_WINBIND_ONLINE, child_msg_online);
1456 messaging_register(winbind_messaging_context(), NULL,
1457 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1458 messaging_register(winbind_messaging_context(), NULL,
1459 MSG_DEBUG, debug_message);
1460 messaging_register(winbind_messaging_context(), NULL,
1461 MSG_WINBIND_IP_DROPPED,
1462 winbind_msg_ip_dropped);
1465 primary_domain = find_our_domain();
1467 if (primary_domain == NULL) {
1468 smb_panic("no primary domain found");
1471 /* It doesn't matter if we allow cache login,
1472 * try to bring domain online after fork. */
1473 if ( child->domain ) {
1474 child->domain->startup = True;
1475 child->domain->startup_time = time_mono(NULL);
1476 /* we can be in primary domain or in trusted domain
1477 * If we are in trusted domain, set the primary domain
1478 * in start-up mode */
1479 if (!(child->domain->internal)) {
1480 set_domain_online_request(child->domain);
1481 if (!(child->domain->primary)) {
1482 primary_domain->startup = True;
1483 primary_domain->startup_time = time_mono(NULL);
1484 set_domain_online_request(primary_domain);
1490 * We are in idmap child, make sure that we set the
1491 * check_online_event to bring primary domain online.
1493 if (child == idmap_child()) {
1494 set_domain_online_request(primary_domain);
1497 /* We might be in the idmap child...*/
1498 if (child->domain && !(child->domain->internal) &&
1499 lp_winbind_offline_logon()) {
1501 set_domain_online_request(child->domain);
1503 if (primary_domain && (primary_domain != child->domain)) {
1504 /* We need to talk to the primary
1505 * domain as well as the trusted
1506 * domain inside a trusted domain
1509 * set_dc_type_and_flags_trustinfo()
1512 set_domain_online_request(primary_domain);
1515 child->lockout_policy_event = tevent_add_timer(
1516 winbind_event_context(), NULL, timeval_zero(),
1517 account_lockout_policy_handler,
1521 if (child->domain && child->domain->primary &&
1522 !USE_KERBEROS_KEYTAB &&
1523 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1525 struct timeval next_change;
1527 if (calculate_next_machine_pwd_change(child->domain->name,
1529 child->machine_password_change_event = tevent_add_timer(
1530 winbind_event_context(), NULL, next_change,
1531 machine_password_change_handler,
1536 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1537 TEVENT_FD_READ, child_handler, &state);
1539 DEBUG(1, ("tevent_add_fd failed\n"));
1546 TALLOC_CTX *frame = talloc_stackframe();
1548 ret = tevent_loop_once(winbind_event_context());
1550 DEBUG(1, ("tevent_loop_once failed: %s\n",
1555 if (child->domain && child->domain->startup &&
1556 (time_mono(NULL) > child->domain->startup_time + 30)) {
1557 /* No longer in "startup" mode. */
1558 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1559 child->domain->name ));
1560 child->domain->startup = False;
1567 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1570 struct server_id server_id,
1573 struct winbindd_child *child;
1575 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1579 for (child = winbindd_children; child != NULL; child = child->next) {
1580 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1581 msg_type, data->data, data->length);