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 "rpc_client/rpc_client.h"
33 #include "nsswitch/wb_reqtrans.h"
35 #include "../lib/util/select.h"
36 #include "../libcli/security/security.h"
37 #include "system/select.h"
39 #include "../lib/util/tevent_unix.h"
40 #include "lib/param/loadparm.h"
43 #define DBGC_CLASS DBGC_WINBIND
45 extern bool override_logfile;
47 static struct winbindd_child *winbindd_children = NULL;
49 /* Read some data from a client connection */
51 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
55 status = read_data(sock, (char *)wreq, sizeof(*wreq));
56 if (!NT_STATUS_IS_OK(status)) {
57 DEBUG(3, ("child_read_request: read_data failed: %s\n",
62 if (wreq->extra_len == 0) {
63 wreq->extra_data.data = NULL;
67 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
69 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
70 if (wreq->extra_data.data == NULL) {
71 DEBUG(0, ("malloc failed\n"));
72 return NT_STATUS_NO_MEMORY;
75 /* Ensure null termination */
76 wreq->extra_data.data[wreq->extra_len] = '\0';
78 status = read_data(sock, wreq->extra_data.data, wreq->extra_len);
79 if (!NT_STATUS_IS_OK(status)) {
80 DEBUG(0, ("Could not read extra data: %s\n",
86 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
91 iov[0].iov_base = (void *)wrsp;
92 iov[0].iov_len = sizeof(struct winbindd_response);
95 if (wrsp->length > sizeof(struct winbindd_response)) {
96 iov[1].iov_base = (void *)wrsp->extra_data.data;
97 iov[1].iov_len = wrsp->length-iov[0].iov_len;
101 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
103 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
104 DEBUG(0, ("Could not write result\n"));
105 return NT_STATUS_INVALID_HANDLE;
112 * Do winbind child async request. This is not simply wb_simple_trans. We have
113 * to do the queueing ourselves because while a request is queued, the child
114 * might have crashed, and we have to re-fork it in the _trigger function.
117 struct wb_child_request_state {
118 struct tevent_context *ev;
119 struct winbindd_child *child;
120 struct winbindd_request *request;
121 struct winbindd_response *response;
124 static bool fork_domain_child(struct winbindd_child *child);
126 static void wb_child_request_trigger(struct tevent_req *req,
128 static void wb_child_request_done(struct tevent_req *subreq);
130 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
131 struct tevent_context *ev,
132 struct winbindd_child *child,
133 struct winbindd_request *request)
135 struct tevent_req *req;
136 struct wb_child_request_state *state;
138 req = tevent_req_create(mem_ctx, &state,
139 struct wb_child_request_state);
145 state->child = child;
146 state->request = request;
148 if (!tevent_queue_add(child->queue, ev, req,
149 wb_child_request_trigger, NULL)) {
151 return tevent_req_post(req, ev);
156 static void wb_child_request_trigger(struct tevent_req *req,
159 struct wb_child_request_state *state = tevent_req_data(
160 req, struct wb_child_request_state);
161 struct tevent_req *subreq;
163 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
164 tevent_req_error(req, errno);
168 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
169 state->child->sock, state->request);
170 if (tevent_req_nomem(subreq, req)) {
173 tevent_req_set_callback(subreq, wb_child_request_done, req);
174 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
177 static void wb_child_request_done(struct tevent_req *subreq)
179 struct tevent_req *req = tevent_req_callback_data(
180 subreq, struct tevent_req);
181 struct wb_child_request_state *state = tevent_req_data(
182 req, struct wb_child_request_state);
185 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
189 * The basic parent/child communication broke, close
192 close(state->child->sock);
193 state->child->sock = -1;
194 DLIST_REMOVE(winbindd_children, state->child);
195 tevent_req_error(req, err);
198 tevent_req_done(req);
201 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
202 struct winbindd_response **presponse, int *err)
204 struct wb_child_request_state *state = tevent_req_data(
205 req, struct wb_child_request_state);
207 if (tevent_req_is_unix_error(req, err)) {
210 *presponse = talloc_move(mem_ctx, &state->response);
214 static bool winbindd_child_busy(struct winbindd_child *child)
216 return tevent_queue_length(child->queue) > 0;
219 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
223 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
224 if (!winbindd_child_busy(&domain->children[i])) {
225 return &domain->children[i];
232 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
234 struct winbindd_child *result;
236 result = find_idle_child(domain);
237 if (result != NULL) {
240 return &domain->children[rand() % lp_winbind_max_domain_connections()];
243 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
245 struct winbindd_child *child;
247 child = choose_domain_child(domain);
248 return child->binding_handle;
251 struct wb_domain_request_state {
252 struct tevent_context *ev;
253 struct winbindd_domain *domain;
254 struct winbindd_child *child;
255 struct winbindd_request *request;
256 struct winbindd_request *init_req;
257 struct winbindd_response *response;
260 static void wb_domain_request_gotdc(struct tevent_req *subreq);
261 static void wb_domain_request_initialized(struct tevent_req *subreq);
262 static void wb_domain_request_done(struct tevent_req *subreq);
264 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
265 struct tevent_context *ev,
266 struct winbindd_domain *domain,
267 struct winbindd_request *request)
269 struct tevent_req *req, *subreq;
270 struct wb_domain_request_state *state;
272 req = tevent_req_create(mem_ctx, &state,
273 struct wb_domain_request_state);
278 state->child = choose_domain_child(domain);
280 if (domain->initialized) {
281 subreq = wb_child_request_send(state, ev, state->child,
283 if (tevent_req_nomem(subreq, req)) {
284 return tevent_req_post(req, ev);
286 tevent_req_set_callback(subreq, wb_domain_request_done, req);
290 state->domain = domain;
292 state->request = request;
294 state->init_req = talloc_zero(state, struct winbindd_request);
295 if (tevent_req_nomem(state->init_req, req)) {
296 return tevent_req_post(req, ev);
299 if (IS_DC || domain->primary || domain->internal) {
300 /* The primary domain has to find the DC name itself */
301 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
302 fstrcpy(state->init_req->domain_name, domain->name);
303 state->init_req->data.init_conn.is_primary = domain->primary;
304 fstrcpy(state->init_req->data.init_conn.dcname, "");
306 subreq = wb_child_request_send(state, ev, state->child,
308 if (tevent_req_nomem(subreq, req)) {
309 return tevent_req_post(req, ev);
311 tevent_req_set_callback(subreq, wb_domain_request_initialized,
317 * Ask our DC for a DC name
319 domain = find_our_domain();
321 /* This is *not* the primary domain, let's ask our DC about a DC
324 state->init_req->cmd = WINBINDD_GETDCNAME;
325 fstrcpy(state->init_req->domain_name, domain->name);
327 subreq = wb_child_request_send(state, ev, state->child, request);
328 if (tevent_req_nomem(subreq, req)) {
329 return tevent_req_post(req, ev);
331 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
335 static void wb_domain_request_gotdc(struct tevent_req *subreq)
337 struct tevent_req *req = tevent_req_callback_data(
338 subreq, struct tevent_req);
339 struct wb_domain_request_state *state = tevent_req_data(
340 req, struct wb_domain_request_state);
341 struct winbindd_response *response;
344 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
347 tevent_req_error(req, err);
350 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
351 fstrcpy(state->init_req->domain_name, state->domain->name);
352 state->init_req->data.init_conn.is_primary = False;
353 fstrcpy(state->init_req->data.init_conn.dcname,
354 response->data.dc_name);
356 TALLOC_FREE(response);
358 subreq = wb_child_request_send(state, state->ev, state->child,
360 if (tevent_req_nomem(subreq, req)) {
363 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
366 static void wb_domain_request_initialized(struct tevent_req *subreq)
368 struct tevent_req *req = tevent_req_callback_data(
369 subreq, struct tevent_req);
370 struct wb_domain_request_state *state = tevent_req_data(
371 req, struct wb_domain_request_state);
372 struct winbindd_response *response;
375 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
378 tevent_req_error(req, err);
382 if (!string_to_sid(&state->domain->sid,
383 response->data.domain_info.sid)) {
384 DEBUG(1,("init_child_recv: Could not convert sid %s "
385 "from string\n", response->data.domain_info.sid));
386 tevent_req_error(req, EINVAL);
390 talloc_free(state->domain->name);
391 state->domain->name = talloc_strdup(state->domain,
392 response->data.domain_info.name);
393 if (state->domain->name == NULL) {
394 tevent_req_error(req, ENOMEM);
398 if (response->data.domain_info.alt_name[0] != '\0') {
399 talloc_free(state->domain->alt_name);
401 state->domain->alt_name = talloc_strdup(state->domain,
402 response->data.domain_info.alt_name);
403 if (state->domain->alt_name == NULL) {
404 tevent_req_error(req, ENOMEM);
409 state->domain->native_mode = response->data.domain_info.native_mode;
410 state->domain->active_directory =
411 response->data.domain_info.active_directory;
412 state->domain->initialized = true;
414 TALLOC_FREE(response);
416 subreq = wb_child_request_send(state, state->ev, state->child,
418 if (tevent_req_nomem(subreq, req)) {
421 tevent_req_set_callback(subreq, wb_domain_request_done, req);
424 static void wb_domain_request_done(struct tevent_req *subreq)
426 struct tevent_req *req = tevent_req_callback_data(
427 subreq, struct tevent_req);
428 struct wb_domain_request_state *state = tevent_req_data(
429 req, struct wb_domain_request_state);
432 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
436 tevent_req_error(req, err);
439 tevent_req_done(req);
442 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
443 struct winbindd_response **presponse, int *err)
445 struct wb_domain_request_state *state = tevent_req_data(
446 req, struct wb_domain_request_state);
448 if (tevent_req_is_unix_error(req, err)) {
451 *presponse = talloc_move(mem_ctx, &state->response);
455 static void child_process_request(struct winbindd_child *child,
456 struct winbindd_cli_state *state)
458 struct winbindd_domain *domain = child->domain;
459 const struct winbindd_child_dispatch_table *table = child->table;
461 /* Free response data - we may be interrupted and receive another
462 command before being able to send this data off. */
464 state->response->result = WINBINDD_ERROR;
465 state->response->length = sizeof(struct winbindd_response);
467 /* as all requests in the child are sync, we can use talloc_tos() */
468 state->mem_ctx = talloc_tos();
470 /* Process command */
472 for (; table->name; table++) {
473 if (state->request->cmd == table->struct_cmd) {
474 DEBUG(10,("child_process_request: request fn %s\n",
476 state->response->result = table->struct_fn(domain, state);
481 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
482 (int)state->request->cmd));
483 state->response->result = WINBINDD_ERROR;
486 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
487 const struct winbindd_child_dispatch_table *table,
488 const char *logprefix,
491 if (logprefix && logname) {
492 char *logbase = NULL;
494 if (*lp_logfile(talloc_tos())) {
497 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
498 smb_panic("Internal error: asprintf failed");
501 if ((end = strrchr_m(logbase, '/'))) {
505 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
506 smb_panic("Internal error: asprintf failed");
510 if (asprintf(&child->logfilename, "%s/%s-%s",
511 logbase, logprefix, logname) < 0) {
513 smb_panic("Internal error: asprintf failed");
518 smb_panic("Internal error: logprefix == NULL && "
523 child->domain = domain;
524 child->table = table;
525 child->queue = tevent_queue_create(NULL, "winbind_child");
526 SMB_ASSERT(child->queue != NULL);
527 child->binding_handle = wbint_binding_handle(NULL, domain, child);
528 SMB_ASSERT(child->binding_handle != NULL);
531 void winbind_child_died(pid_t pid)
533 struct winbindd_child *child;
535 for (child = winbindd_children; child != NULL; child = child->next) {
536 if (child->pid == pid) {
542 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
546 /* This will be re-added in fork_domain_child() */
548 DLIST_REMOVE(winbindd_children, child);
551 if (child->sock != -1) {
557 /* Ensure any negative cache entries with the netbios or realm names are removed. */
559 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
561 flush_negative_conn_cache_for_domain(domain->name);
562 if (domain->alt_name != NULL) {
563 flush_negative_conn_cache_for_domain(domain->alt_name);
568 * Parent winbindd process sets its own debug level first and then
569 * sends a message to all the winbindd children to adjust their debug
570 * level to that of parents.
573 void winbind_msg_debug(struct messaging_context *msg_ctx,
576 struct server_id server_id,
579 struct winbindd_child *child;
581 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
583 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
585 for (child = winbindd_children; child != NULL; child = child->next) {
587 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
588 (unsigned int)child->pid));
590 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
593 strlen((char *) data->data) + 1);
597 /* Set our domains as offline and forward the offline message to our children. */
599 void winbind_msg_offline(struct messaging_context *msg_ctx,
602 struct server_id server_id,
605 struct winbindd_child *child;
606 struct winbindd_domain *domain;
608 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
610 if (!lp_winbind_offline_logon()) {
611 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
615 /* Set our global state as offline. */
616 if (!set_global_winbindd_state_offline()) {
617 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
621 /* Set all our domains as offline. */
622 for (domain = domain_list(); domain; domain = domain->next) {
623 if (domain->internal) {
626 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
627 set_domain_offline(domain);
630 for (child = winbindd_children; child != NULL; child = child->next) {
631 /* Don't send message to internal children. We've already
633 if (!child->domain || winbindd_internal_child(child)) {
637 /* Or internal domains (this should not be possible....) */
638 if (child->domain->internal) {
642 /* Each winbindd child should only process requests for one domain - make sure
643 we only set it online / offline for that domain. */
645 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
646 (unsigned int)child->pid, child->domain->name ));
648 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
650 (const uint8_t *)child->domain->name,
651 strlen(child->domain->name)+1);
655 /* Set our domains as online and forward the online message to our children. */
657 void winbind_msg_online(struct messaging_context *msg_ctx,
660 struct server_id server_id,
663 struct winbindd_child *child;
664 struct winbindd_domain *domain;
666 DEBUG(10,("winbind_msg_online: got online message.\n"));
668 if (!lp_winbind_offline_logon()) {
669 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
673 /* Set our global state as online. */
674 set_global_winbindd_state_online();
676 smb_nscd_flush_user_cache();
677 smb_nscd_flush_group_cache();
679 /* Set all our domains as online. */
680 for (domain = domain_list(); domain; domain = domain->next) {
681 if (domain->internal) {
684 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
686 winbindd_flush_negative_conn_cache(domain);
687 set_domain_online_request(domain);
689 /* Send an online message to the idmap child when our
690 primary domain comes back online */
692 if ( domain->primary ) {
693 struct winbindd_child *idmap = idmap_child();
695 if ( idmap->pid != 0 ) {
696 messaging_send_buf(msg_ctx,
697 pid_to_procid(idmap->pid),
699 (const uint8_t *)domain->name,
700 strlen(domain->name)+1);
705 for (child = winbindd_children; child != NULL; child = child->next) {
706 /* Don't send message to internal childs. */
707 if (!child->domain || winbindd_internal_child(child)) {
711 /* Or internal domains (this should not be possible....) */
712 if (child->domain->internal) {
716 /* Each winbindd child should only process requests for one domain - make sure
717 we only set it online / offline for that domain. */
719 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
720 (unsigned int)child->pid, child->domain->name ));
722 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
724 (const uint8_t *)child->domain->name,
725 strlen(child->domain->name)+1);
729 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
731 struct winbindd_domain *domain;
734 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
735 get_global_winbindd_state_offline() ?
736 "Offline":"Online")) == NULL) {
740 for (domain = domain_list(); domain; domain = domain->next) {
741 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
744 "Online":"Offline")) == NULL) {
749 buf = talloc_asprintf_append_buffer(buf, "\n");
751 DEBUG(5,("collect_onlinestatus: %s", buf));
756 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
759 struct server_id server_id,
764 struct server_id *sender;
766 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
772 sender = (struct server_id *)data->data;
774 mem_ctx = talloc_init("winbind_msg_onlinestatus");
775 if (mem_ctx == NULL) {
779 message = collect_onlinestatus(mem_ctx);
780 if (message == NULL) {
781 talloc_destroy(mem_ctx);
785 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
786 (const uint8 *)message, strlen(message) + 1);
788 talloc_destroy(mem_ctx);
791 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
794 struct server_id server_id,
797 struct winbindd_child *child;
799 DEBUG(10,("winbind_msg_dump_event_list received\n"));
801 dump_event_list(winbind_event_context());
803 for (child = winbindd_children; child != NULL; child = child->next) {
805 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
806 (unsigned int)child->pid));
808 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
815 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
818 struct server_id server_id,
822 const char *message = NULL;
823 struct server_id *sender = NULL;
824 const char *domain = NULL;
827 struct winbindd_domain *dom = NULL;
829 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
831 if (!data || !data->data) {
835 if (data->length < sizeof(struct server_id)) {
839 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
844 sender = (struct server_id *)data->data;
845 if (data->length > sizeof(struct server_id)) {
846 domain = (const char *)data->data+sizeof(struct server_id);
851 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
854 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
855 find_domain_from_name_noinit(domain));
857 talloc_destroy(mem_ctx);
861 messaging_send_buf(msg_ctx, *sender,
862 MSG_WINBIND_DUMP_DOMAIN_LIST,
863 (const uint8_t *)message, strlen(message) + 1);
865 talloc_destroy(mem_ctx);
870 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
872 for (dom = domain_list(); dom; dom=dom->next) {
873 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
875 talloc_destroy(mem_ctx);
879 s = talloc_asprintf_append(s, "%s\n", message);
881 talloc_destroy(mem_ctx);
886 status = messaging_send_buf(msg_ctx, *sender,
887 MSG_WINBIND_DUMP_DOMAIN_LIST,
888 (uint8_t *)s, strlen(s) + 1);
889 if (!NT_STATUS_IS_OK(status)) {
890 DEBUG(0,("failed to send message: %s\n",
894 talloc_destroy(mem_ctx);
897 static void account_lockout_policy_handler(struct tevent_context *ctx,
898 struct tevent_timer *te,
902 struct winbindd_child *child =
903 (struct winbindd_child *)private_data;
904 TALLOC_CTX *mem_ctx = NULL;
905 struct winbindd_methods *methods;
906 struct samr_DomInfo12 lockout_policy;
909 DEBUG(10,("account_lockout_policy_handler called\n"));
911 TALLOC_FREE(child->lockout_policy_event);
913 if ( !winbindd_can_contact_domain( child->domain ) ) {
914 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
915 "do not have an incoming trust to domain %s\n",
916 child->domain->name));
921 methods = child->domain->methods;
923 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
925 result = NT_STATUS_NO_MEMORY;
927 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
929 TALLOC_FREE(mem_ctx);
931 if (!NT_STATUS_IS_OK(result)) {
932 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
936 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
937 timeval_current_ofs(3600, 0),
938 account_lockout_policy_handler,
942 static time_t get_machine_password_timeout(void)
944 /* until we have gpo support use lp setting */
945 return lp_machine_password_timeout();
948 static bool calculate_next_machine_pwd_change(const char *domain,
951 time_t pass_last_set_time;
957 pw = secrets_fetch_machine_password(domain,
962 DEBUG(0,("cannot fetch own machine password ????"));
968 timeout = get_machine_password_timeout();
970 DEBUG(10,("machine password never expires\n"));
974 tv.tv_sec = pass_last_set_time;
975 DEBUG(10, ("password last changed %s\n",
976 timeval_string(talloc_tos(), &tv, false)));
977 tv.tv_sec += timeout;
978 DEBUGADD(10, ("password valid until %s\n",
979 timeval_string(talloc_tos(), &tv, false)));
981 if (time(NULL) < (pass_last_set_time + timeout)) {
982 next_change = pass_last_set_time + timeout;
983 DEBUG(10,("machine password still valid until: %s\n",
984 http_timestring(talloc_tos(), next_change)));
985 *t = timeval_set(next_change, 0);
987 if (lp_clustering()) {
990 * When having a cluster, we have several
991 * winbinds racing for the password change. In
992 * the machine_password_change_handler()
993 * function we check if someone else was
994 * faster when the event triggers. We add a
995 * 255-second random delay here, so that we
996 * don't run to change the password at the
999 generate_random_buffer(&randbuf, sizeof(randbuf));
1000 DEBUG(10, ("adding %d seconds randomness\n",
1002 t->tv_sec += randbuf;
1007 DEBUG(10,("machine password expired, needs immediate change\n"));
1009 *t = timeval_zero();
1014 static void machine_password_change_handler(struct tevent_context *ctx,
1015 struct tevent_timer *te,
1019 struct messaging_context *msg_ctx = winbind_messaging_context();
1020 struct winbindd_child *child =
1021 (struct winbindd_child *)private_data;
1022 struct rpc_pipe_client *netlogon_pipe = NULL;
1024 struct timeval next_change;
1026 DEBUG(10,("machine_password_change_handler called\n"));
1028 TALLOC_FREE(child->machine_password_change_event);
1030 if (!calculate_next_machine_pwd_change(child->domain->name,
1032 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1036 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1037 timeval_string(talloc_tos(), &next_change, false)));
1039 if (!timeval_expired(&next_change)) {
1040 DEBUG(10, ("Someone else has already changed the pw\n"));
1044 if (!winbindd_can_contact_domain(child->domain)) {
1045 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1046 "do not have an incoming trust to domain %s\n",
1047 child->domain->name));
1051 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1052 if (!NT_STATUS_IS_OK(result)) {
1053 DEBUG(10,("machine_password_change_handler: "
1054 "failed to connect netlogon pipe: %s\n",
1055 nt_errstr(result)));
1059 result = trust_pw_change(child->domain->conn.netlogon_creds,
1061 netlogon_pipe->binding_handle,
1062 child->domain->name,
1065 DEBUG(10, ("machine_password_change_handler: "
1066 "trust_pw_change returned %s\n",
1067 nt_errstr(result)));
1069 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1070 DEBUG(3,("machine_password_change_handler: password set returned "
1071 "ACCESS_DENIED. Maybe the trust account "
1072 "password was changed and we didn't know it. "
1073 "Killing connections to domain %s\n",
1074 child->domain->name));
1075 invalidate_cm_connection(&child->domain->conn);
1078 if (!calculate_next_machine_pwd_change(child->domain->name,
1080 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1084 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1085 timeval_string(talloc_tos(), &next_change, false)));
1087 if (!NT_STATUS_IS_OK(result)) {
1090 * In case of failure, give the DC a minute to recover
1092 tmp = timeval_current_ofs(60, 0);
1093 next_change = timeval_max(&next_change, &tmp);
1097 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1099 machine_password_change_handler,
1103 /* Deal with a request to go offline. */
1105 static void child_msg_offline(struct messaging_context *msg,
1108 struct server_id server_id,
1111 struct winbindd_domain *domain;
1112 struct winbindd_domain *primary_domain = NULL;
1113 const char *domainname = (const char *)data->data;
1115 if (data->data == NULL || data->length == 0) {
1119 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1121 if (!lp_winbind_offline_logon()) {
1122 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1126 primary_domain = find_our_domain();
1128 /* Mark the requested domain offline. */
1130 for (domain = domain_list(); domain; domain = domain->next) {
1131 if (domain->internal) {
1134 if (strequal(domain->name, domainname)) {
1135 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1136 set_domain_offline(domain);
1137 /* we are in the trusted domain, set the primary domain
1139 if (domain != primary_domain) {
1140 set_domain_offline(primary_domain);
1146 /* Deal with a request to go online. */
1148 static void child_msg_online(struct messaging_context *msg,
1151 struct server_id server_id,
1154 struct winbindd_domain *domain;
1155 struct winbindd_domain *primary_domain = NULL;
1156 const char *domainname = (const char *)data->data;
1158 if (data->data == NULL || data->length == 0) {
1162 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1164 if (!lp_winbind_offline_logon()) {
1165 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1169 primary_domain = find_our_domain();
1171 /* Set our global state as online. */
1172 set_global_winbindd_state_online();
1174 /* Try and mark everything online - delete any negative cache entries
1175 to force a reconnect now. */
1177 for (domain = domain_list(); domain; domain = domain->next) {
1178 if (domain->internal) {
1181 if (strequal(domain->name, domainname)) {
1182 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1183 winbindd_flush_negative_conn_cache(domain);
1184 set_domain_online_request(domain);
1186 /* we can be in trusted domain, which will contact primary domain
1187 * we have to bring primary domain online in trusted domain process
1188 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1189 * --> contact_domain = find_our_domain()
1191 if (domain != primary_domain) {
1192 winbindd_flush_negative_conn_cache(primary_domain);
1193 set_domain_online_request(primary_domain);
1199 static void child_msg_dump_event_list(struct messaging_context *msg,
1202 struct server_id server_id,
1205 DEBUG(5,("child_msg_dump_event_list received\n"));
1207 dump_event_list(winbind_event_context());
1210 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1211 const char *logfilename)
1213 struct winbindd_domain *domain;
1214 struct winbindd_child *cl;
1217 status = reinit_after_fork(
1218 winbind_messaging_context(),
1219 winbind_event_context(),
1221 if (!NT_STATUS_IS_OK(status)) {
1222 DEBUG(0,("reinit_after_fork() failed\n"));
1226 close_conns_after_fork();
1228 if (!override_logfile && logfilename) {
1229 lp_set_logfile(logfilename);
1233 if (!winbindd_setup_sig_term_handler(false))
1234 return NT_STATUS_NO_MEMORY;
1235 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1237 return NT_STATUS_NO_MEMORY;
1239 /* Stop zombies in children */
1242 /* Don't handle the same messages as our parent. */
1243 messaging_deregister(winbind_messaging_context(),
1244 MSG_SMB_CONF_UPDATED, NULL);
1245 messaging_deregister(winbind_messaging_context(),
1246 MSG_SHUTDOWN, NULL);
1247 messaging_deregister(winbind_messaging_context(),
1248 MSG_WINBIND_OFFLINE, NULL);
1249 messaging_deregister(winbind_messaging_context(),
1250 MSG_WINBIND_ONLINE, NULL);
1251 messaging_deregister(winbind_messaging_context(),
1252 MSG_WINBIND_ONLINESTATUS, NULL);
1253 messaging_deregister(winbind_messaging_context(),
1254 MSG_DUMP_EVENT_LIST, NULL);
1255 messaging_deregister(winbind_messaging_context(),
1256 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1257 messaging_deregister(winbind_messaging_context(),
1260 messaging_deregister(winbind_messaging_context(),
1261 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1262 messaging_deregister(winbind_messaging_context(),
1263 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1265 /* We have destroyed all events in the winbindd_event_context
1266 * in reinit_after_fork(), so clean out all possible pending
1267 * event pointers. */
1269 /* Deal with check_online_events. */
1271 for (domain = domain_list(); domain; domain = domain->next) {
1272 TALLOC_FREE(domain->check_online_event);
1275 /* Ensure we're not handling a credential cache event inherited
1276 * from our parent. */
1278 ccache_remove_all_after_fork();
1280 /* Destroy all possible events in child list. */
1281 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1282 TALLOC_FREE(cl->lockout_policy_event);
1283 TALLOC_FREE(cl->machine_password_change_event);
1285 /* Children should never be able to send
1286 * each other messages, all messages must
1287 * go through the parent.
1292 * Close service sockets to all other children
1294 if ((cl != myself) && (cl->sock != -1)) {
1300 * This is a little tricky, children must not
1301 * send an MSG_WINBIND_ONLINE message to idmap_child().
1302 * If we are in a child of our primary domain or
1303 * in the process created by fork_child_dc_connect(),
1304 * and the primary domain cannot go online,
1305 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1306 * periodically to idmap_child().
1308 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1309 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1310 * ---> init_dc_connection() ---> cm_open_connection --->
1311 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1312 * idmap_child(). Disallow children sending messages
1313 * to each other, all messages must go through the parent.
1318 return NT_STATUS_OK;
1322 * In a child there will be only one domain, reference that here.
1324 static struct winbindd_domain *child_domain;
1326 struct winbindd_domain *wb_child_domain(void)
1328 return child_domain;
1331 struct child_handler_state {
1332 struct winbindd_child *child;
1333 struct winbindd_cli_state cli;
1336 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1337 uint16_t flags, void *private_data)
1339 struct child_handler_state *state =
1340 (struct child_handler_state *)private_data;
1343 /* fetch a request from the main daemon */
1344 status = child_read_request(state->cli.sock, state->cli.request);
1346 if (!NT_STATUS_IS_OK(status)) {
1347 /* we lost contact with our parent */
1351 DEBUG(4,("child daemon request %d\n",
1352 (int)state->cli.request->cmd));
1354 ZERO_STRUCTP(state->cli.response);
1355 state->cli.request->null_term = '\0';
1356 state->cli.mem_ctx = talloc_tos();
1357 child_process_request(state->child, &state->cli);
1359 DEBUG(4, ("Finished processing child request %d\n",
1360 (int)state->cli.request->cmd));
1362 SAFE_FREE(state->cli.request->extra_data.data);
1364 status = child_write_response(state->cli.sock, state->cli.response);
1365 if (!NT_STATUS_IS_OK(status)) {
1370 static bool fork_domain_child(struct winbindd_child *child)
1373 struct child_handler_state state;
1374 struct winbindd_request request;
1375 struct winbindd_response response;
1376 struct winbindd_domain *primary_domain = NULL;
1379 struct tevent_fd *fde;
1381 if (child->domain) {
1382 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1383 child->domain->name));
1385 DEBUG(10, ("fork_domain_child called without domain.\n"));
1388 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1389 DEBUG(0, ("Could not open child pipe: %s\n",
1395 state.child = child;
1396 state.cli.pid = getpid();
1397 state.cli.request = &request;
1398 state.cli.response = &response;
1400 child->pid = fork();
1402 if (child->pid == -1) {
1403 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1409 if (child->pid != 0) {
1415 nread = sys_read(fdpair[1], &status, sizeof(status));
1416 if (nread != sizeof(status)) {
1417 DEBUG(1, ("fork_domain_child: Could not read child status: "
1418 "nread=%d, error=%s\n", (int)nread,
1423 if (!NT_STATUS_IS_OK(status)) {
1424 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1425 nt_errstr(status)));
1430 child->next = child->prev = NULL;
1431 DLIST_ADD(winbindd_children, child);
1432 child->sock = fdpair[1];
1437 child_domain = child->domain;
1439 DEBUG(10, ("Child process %d\n", (int)getpid()));
1441 state.cli.sock = fdpair[0];
1444 status = winbindd_reinit_after_fork(child, child->logfilename);
1446 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1447 if (nwritten != sizeof(status)) {
1448 DEBUG(1, ("fork_domain_child: Could not write status: "
1449 "nwritten=%d, error=%s\n", (int)nwritten,
1453 if (!NT_STATUS_IS_OK(status)) {
1454 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1455 nt_errstr(status)));
1459 /* Handle online/offline messages. */
1460 messaging_register(winbind_messaging_context(), NULL,
1461 MSG_WINBIND_OFFLINE, child_msg_offline);
1462 messaging_register(winbind_messaging_context(), NULL,
1463 MSG_WINBIND_ONLINE, child_msg_online);
1464 messaging_register(winbind_messaging_context(), NULL,
1465 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1466 messaging_register(winbind_messaging_context(), NULL,
1467 MSG_DEBUG, debug_message);
1468 messaging_register(winbind_messaging_context(), NULL,
1469 MSG_WINBIND_IP_DROPPED,
1470 winbind_msg_ip_dropped);
1473 primary_domain = find_our_domain();
1475 if (primary_domain == NULL) {
1476 smb_panic("no primary domain found");
1479 /* It doesn't matter if we allow cache login,
1480 * try to bring domain online after fork. */
1481 if ( child->domain ) {
1482 child->domain->startup = True;
1483 child->domain->startup_time = time_mono(NULL);
1484 /* we can be in primary domain or in trusted domain
1485 * If we are in trusted domain, set the primary domain
1486 * in start-up mode */
1487 if (!(child->domain->internal)) {
1488 set_domain_online_request(child->domain);
1489 if (!(child->domain->primary)) {
1490 primary_domain->startup = True;
1491 primary_domain->startup_time = time_mono(NULL);
1492 set_domain_online_request(primary_domain);
1498 * We are in idmap child, make sure that we set the
1499 * check_online_event to bring primary domain online.
1501 if (child == idmap_child()) {
1502 set_domain_online_request(primary_domain);
1505 /* We might be in the idmap child...*/
1506 if (child->domain && !(child->domain->internal) &&
1507 lp_winbind_offline_logon()) {
1509 set_domain_online_request(child->domain);
1511 if (primary_domain && (primary_domain != child->domain)) {
1512 /* We need to talk to the primary
1513 * domain as well as the trusted
1514 * domain inside a trusted domain
1517 * set_dc_type_and_flags_trustinfo()
1520 set_domain_online_request(primary_domain);
1523 child->lockout_policy_event = tevent_add_timer(
1524 winbind_event_context(), NULL, timeval_zero(),
1525 account_lockout_policy_handler,
1529 if (child->domain && child->domain->primary &&
1530 !USE_KERBEROS_KEYTAB &&
1531 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1533 struct timeval next_change;
1535 if (calculate_next_machine_pwd_change(child->domain->name,
1537 child->machine_password_change_event = tevent_add_timer(
1538 winbind_event_context(), NULL, next_change,
1539 machine_password_change_handler,
1544 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1545 TEVENT_FD_READ, child_handler, &state);
1547 DEBUG(1, ("tevent_add_fd failed\n"));
1554 TALLOC_CTX *frame = talloc_stackframe();
1556 ret = tevent_loop_once(winbind_event_context());
1558 DEBUG(1, ("tevent_loop_once failed: %s\n",
1563 if (child->domain && child->domain->startup &&
1564 (time_mono(NULL) > child->domain->startup_time + 30)) {
1565 /* No longer in "startup" mode. */
1566 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1567 child->domain->name ));
1568 child->domain->startup = False;
1575 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1578 struct server_id server_id,
1581 struct winbindd_child *child;
1583 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1587 for (child = winbindd_children; child != NULL; child = child->next) {
1588 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1589 msg_type, data->data, data->length);