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"
41 #define DBGC_CLASS DBGC_WINBIND
43 extern bool override_logfile;
44 extern struct winbindd_methods cache_methods;
46 /* Read some data from a client connection */
48 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
54 status = read_data(state->sock, (char *)state->request,
55 sizeof(*state->request));
57 if (!NT_STATUS_IS_OK(status)) {
58 DEBUG(3, ("child_read_request: read_data failed: %s\n",
63 if (state->request->extra_len == 0) {
64 state->request->extra_data.data = NULL;
68 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
70 state->request->extra_data.data =
71 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
73 if (state->request->extra_data.data == NULL) {
74 DEBUG(0, ("malloc failed\n"));
75 return NT_STATUS_NO_MEMORY;
78 /* Ensure null termination */
79 state->request->extra_data.data[state->request->extra_len] = '\0';
81 status= read_data(state->sock, state->request->extra_data.data,
82 state->request->extra_len);
84 if (!NT_STATUS_IS_OK(status)) {
85 DEBUG(0, ("Could not read extra data: %s\n",
92 * Do winbind child async request. This is not simply wb_simple_trans. We have
93 * to do the queueing ourselves because while a request is queued, the child
94 * might have crashed, and we have to re-fork it in the _trigger function.
97 struct wb_child_request_state {
98 struct tevent_context *ev;
99 struct winbindd_child *child;
100 struct winbindd_request *request;
101 struct winbindd_response *response;
104 static bool fork_domain_child(struct winbindd_child *child);
106 static void wb_child_request_trigger(struct tevent_req *req,
108 static void wb_child_request_done(struct tevent_req *subreq);
110 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
111 struct tevent_context *ev,
112 struct winbindd_child *child,
113 struct winbindd_request *request)
115 struct tevent_req *req;
116 struct wb_child_request_state *state;
118 req = tevent_req_create(mem_ctx, &state,
119 struct wb_child_request_state);
125 state->child = child;
126 state->request = request;
128 if (!tevent_queue_add(child->queue, ev, req,
129 wb_child_request_trigger, NULL)) {
131 return tevent_req_post(req, ev);
136 static void wb_child_request_trigger(struct tevent_req *req,
139 struct wb_child_request_state *state = tevent_req_data(
140 req, struct wb_child_request_state);
141 struct tevent_req *subreq;
143 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
144 tevent_req_error(req, errno);
148 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
149 state->child->sock, state->request);
150 if (tevent_req_nomem(subreq, req)) {
153 tevent_req_set_callback(subreq, wb_child_request_done, req);
154 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
157 static void wb_child_request_done(struct tevent_req *subreq)
159 struct tevent_req *req = tevent_req_callback_data(
160 subreq, struct tevent_req);
161 struct wb_child_request_state *state = tevent_req_data(
162 req, struct wb_child_request_state);
165 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
169 * The basic parent/child communication broke, close
172 close(state->child->sock);
173 state->child->sock = -1;
174 tevent_req_error(req, err);
177 tevent_req_done(req);
180 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
181 struct winbindd_response **presponse, int *err)
183 struct wb_child_request_state *state = tevent_req_data(
184 req, struct wb_child_request_state);
186 if (tevent_req_is_unix_error(req, err)) {
189 *presponse = talloc_move(mem_ctx, &state->response);
193 static bool winbindd_child_busy(struct winbindd_child *child)
195 return tevent_queue_length(child->queue) > 0;
198 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
202 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
203 if (!winbindd_child_busy(&domain->children[i])) {
204 return &domain->children[i];
211 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
213 struct winbindd_child *result;
215 result = find_idle_child(domain);
216 if (result != NULL) {
219 return &domain->children[rand() % lp_winbind_max_domain_connections()];
222 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
224 struct winbindd_child *child;
226 child = choose_domain_child(domain);
227 return child->binding_handle;
230 struct wb_domain_request_state {
231 struct tevent_context *ev;
232 struct winbindd_domain *domain;
233 struct winbindd_child *child;
234 struct winbindd_request *request;
235 struct winbindd_request *init_req;
236 struct winbindd_response *response;
239 static void wb_domain_request_gotdc(struct tevent_req *subreq);
240 static void wb_domain_request_initialized(struct tevent_req *subreq);
241 static void wb_domain_request_done(struct tevent_req *subreq);
243 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
244 struct tevent_context *ev,
245 struct winbindd_domain *domain,
246 struct winbindd_request *request)
248 struct tevent_req *req, *subreq;
249 struct wb_domain_request_state *state;
251 req = tevent_req_create(mem_ctx, &state,
252 struct wb_domain_request_state);
257 state->child = choose_domain_child(domain);
259 if (domain->initialized) {
260 subreq = wb_child_request_send(state, ev, state->child,
262 if (tevent_req_nomem(subreq, req)) {
263 return tevent_req_post(req, ev);
265 tevent_req_set_callback(subreq, wb_domain_request_done, req);
269 state->domain = domain;
271 state->request = request;
273 state->init_req = talloc_zero(state, struct winbindd_request);
274 if (tevent_req_nomem(state->init_req, req)) {
275 return tevent_req_post(req, ev);
278 if (IS_DC || domain->primary || domain->internal) {
279 /* The primary domain has to find the DC name itself */
280 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
281 fstrcpy(state->init_req->domain_name, domain->name);
282 state->init_req->data.init_conn.is_primary = domain->primary;
283 fstrcpy(state->init_req->data.init_conn.dcname, "");
285 subreq = wb_child_request_send(state, ev, state->child,
287 if (tevent_req_nomem(subreq, req)) {
288 return tevent_req_post(req, ev);
290 tevent_req_set_callback(subreq, wb_domain_request_initialized,
296 * Ask our DC for a DC name
298 domain = find_our_domain();
300 /* This is *not* the primary domain, let's ask our DC about a DC
303 state->init_req->cmd = WINBINDD_GETDCNAME;
304 fstrcpy(state->init_req->domain_name, domain->name);
306 subreq = wb_child_request_send(state, ev, state->child, request);
307 if (tevent_req_nomem(subreq, req)) {
308 return tevent_req_post(req, ev);
310 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
314 static void wb_domain_request_gotdc(struct tevent_req *subreq)
316 struct tevent_req *req = tevent_req_callback_data(
317 subreq, struct tevent_req);
318 struct wb_domain_request_state *state = tevent_req_data(
319 req, struct wb_domain_request_state);
320 struct winbindd_response *response;
323 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
326 tevent_req_error(req, err);
329 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
330 fstrcpy(state->init_req->domain_name, state->domain->name);
331 state->init_req->data.init_conn.is_primary = False;
332 fstrcpy(state->init_req->data.init_conn.dcname,
333 response->data.dc_name);
335 TALLOC_FREE(response);
337 subreq = wb_child_request_send(state, state->ev, state->child,
339 if (tevent_req_nomem(subreq, req)) {
342 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
345 static void wb_domain_request_initialized(struct tevent_req *subreq)
347 struct tevent_req *req = tevent_req_callback_data(
348 subreq, struct tevent_req);
349 struct wb_domain_request_state *state = tevent_req_data(
350 req, struct wb_domain_request_state);
351 struct winbindd_response *response;
354 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
357 tevent_req_error(req, err);
361 if (!string_to_sid(&state->domain->sid,
362 response->data.domain_info.sid)) {
363 DEBUG(1,("init_child_recv: Could not convert sid %s "
364 "from string\n", response->data.domain_info.sid));
365 tevent_req_error(req, EINVAL);
368 fstrcpy(state->domain->name, response->data.domain_info.name);
369 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
370 state->domain->native_mode = response->data.domain_info.native_mode;
371 state->domain->active_directory =
372 response->data.domain_info.active_directory;
373 state->domain->initialized = true;
375 TALLOC_FREE(response);
377 subreq = wb_child_request_send(state, state->ev, state->child,
379 if (tevent_req_nomem(subreq, req)) {
382 tevent_req_set_callback(subreq, wb_domain_request_done, req);
385 static void wb_domain_request_done(struct tevent_req *subreq)
387 struct tevent_req *req = tevent_req_callback_data(
388 subreq, struct tevent_req);
389 struct wb_domain_request_state *state = tevent_req_data(
390 req, struct wb_domain_request_state);
393 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
397 tevent_req_error(req, err);
400 tevent_req_done(req);
403 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
404 struct winbindd_response **presponse, int *err)
406 struct wb_domain_request_state *state = tevent_req_data(
407 req, struct wb_domain_request_state);
409 if (tevent_req_is_unix_error(req, err)) {
412 *presponse = talloc_move(mem_ctx, &state->response);
416 static void child_process_request(struct winbindd_child *child,
417 struct winbindd_cli_state *state)
419 struct winbindd_domain *domain = child->domain;
420 const struct winbindd_child_dispatch_table *table = child->table;
422 /* Free response data - we may be interrupted and receive another
423 command before being able to send this data off. */
425 state->response->result = WINBINDD_ERROR;
426 state->response->length = sizeof(struct winbindd_response);
428 /* as all requests in the child are sync, we can use talloc_tos() */
429 state->mem_ctx = talloc_tos();
431 /* Process command */
433 for (; table->name; table++) {
434 if (state->request->cmd == table->struct_cmd) {
435 DEBUG(10,("child_process_request: request fn %s\n",
437 state->response->result = table->struct_fn(domain, state);
442 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
443 (int)state->request->cmd));
444 state->response->result = WINBINDD_ERROR;
447 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
448 const struct winbindd_child_dispatch_table *table,
449 const char *logprefix,
452 if (logprefix && logname) {
453 char *logbase = NULL;
458 if (asprintf(&logbase, "%s", lp_logfile()) < 0) {
459 smb_panic("Internal error: asprintf failed");
462 if ((end = strrchr_m(logbase, '/'))) {
466 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
467 smb_panic("Internal error: asprintf failed");
471 if (asprintf(&child->logfilename, "%s/%s-%s",
472 logbase, logprefix, logname) < 0) {
474 smb_panic("Internal error: asprintf failed");
479 smb_panic("Internal error: logprefix == NULL && "
484 child->domain = domain;
485 child->table = table;
486 child->queue = tevent_queue_create(NULL, "winbind_child");
487 SMB_ASSERT(child->queue != NULL);
488 child->binding_handle = wbint_binding_handle(NULL, domain, child);
489 SMB_ASSERT(child->binding_handle != NULL);
492 static struct winbindd_child *winbindd_children = NULL;
494 void winbind_child_died(pid_t pid)
496 struct winbindd_child *child;
498 for (child = winbindd_children; child != NULL; child = child->next) {
499 if (child->pid == pid) {
505 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
509 /* This will be re-added in fork_domain_child() */
511 DLIST_REMOVE(winbindd_children, child);
514 if (child->sock != -1) {
520 /* Ensure any negative cache entries with the netbios or realm names are removed. */
522 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
524 flush_negative_conn_cache_for_domain(domain->name);
525 if (*domain->alt_name) {
526 flush_negative_conn_cache_for_domain(domain->alt_name);
531 * Parent winbindd process sets its own debug level first and then
532 * sends a message to all the winbindd children to adjust their debug
533 * level to that of parents.
536 void winbind_msg_debug(struct messaging_context *msg_ctx,
539 struct server_id server_id,
542 struct winbindd_child *child;
544 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
546 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
548 for (child = winbindd_children; child != NULL; child = child->next) {
550 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
551 (unsigned int)child->pid));
553 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
556 strlen((char *) data->data) + 1);
560 /* Set our domains as offline and forward the offline message to our children. */
562 void winbind_msg_offline(struct messaging_context *msg_ctx,
565 struct server_id server_id,
568 struct winbindd_child *child;
569 struct winbindd_domain *domain;
571 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
573 if (!lp_winbind_offline_logon()) {
574 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
578 /* Set our global state as offline. */
579 if (!set_global_winbindd_state_offline()) {
580 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
584 /* Set all our domains as offline. */
585 for (domain = domain_list(); domain; domain = domain->next) {
586 if (domain->internal) {
589 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
590 set_domain_offline(domain);
593 for (child = winbindd_children; child != NULL; child = child->next) {
594 /* Don't send message to internal children. We've already
596 if (!child->domain || winbindd_internal_child(child)) {
600 /* Or internal domains (this should not be possible....) */
601 if (child->domain->internal) {
605 /* Each winbindd child should only process requests for one domain - make sure
606 we only set it online / offline for that domain. */
608 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
609 (unsigned int)child->pid, domain->name ));
611 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
613 (uint8 *)child->domain->name,
614 strlen(child->domain->name)+1);
618 /* Set our domains as online and forward the online message to our children. */
620 void winbind_msg_online(struct messaging_context *msg_ctx,
623 struct server_id server_id,
626 struct winbindd_child *child;
627 struct winbindd_domain *domain;
629 DEBUG(10,("winbind_msg_online: got online message.\n"));
631 if (!lp_winbind_offline_logon()) {
632 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
636 /* Set our global state as online. */
637 set_global_winbindd_state_online();
639 smb_nscd_flush_user_cache();
640 smb_nscd_flush_group_cache();
642 /* Set all our domains as online. */
643 for (domain = domain_list(); domain; domain = domain->next) {
644 if (domain->internal) {
647 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
649 winbindd_flush_negative_conn_cache(domain);
650 set_domain_online_request(domain);
652 /* Send an online message to the idmap child when our
653 primary domain comes back online */
655 if ( domain->primary ) {
656 struct winbindd_child *idmap = idmap_child();
658 if ( idmap->pid != 0 ) {
659 messaging_send_buf(msg_ctx,
660 pid_to_procid(idmap->pid),
662 (uint8 *)domain->name,
663 strlen(domain->name)+1);
668 for (child = winbindd_children; child != NULL; child = child->next) {
669 /* Don't send message to internal childs. */
670 if (!child->domain || winbindd_internal_child(child)) {
674 /* Or internal domains (this should not be possible....) */
675 if (child->domain->internal) {
679 /* Each winbindd child should only process requests for one domain - make sure
680 we only set it online / offline for that domain. */
682 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
683 (unsigned int)child->pid, child->domain->name ));
685 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
687 (uint8 *)child->domain->name,
688 strlen(child->domain->name)+1);
692 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
694 struct winbindd_domain *domain;
697 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
698 get_global_winbindd_state_offline() ?
699 "Offline":"Online")) == NULL) {
703 for (domain = domain_list(); domain; domain = domain->next) {
704 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
707 "Online":"Offline")) == NULL) {
712 buf = talloc_asprintf_append_buffer(buf, "\n");
714 DEBUG(5,("collect_onlinestatus: %s", buf));
719 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
722 struct server_id server_id,
727 struct server_id *sender;
729 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
735 sender = (struct server_id *)data->data;
737 mem_ctx = talloc_init("winbind_msg_onlinestatus");
738 if (mem_ctx == NULL) {
742 message = collect_onlinestatus(mem_ctx);
743 if (message == NULL) {
744 talloc_destroy(mem_ctx);
748 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
749 (const uint8 *)message, strlen(message) + 1);
751 talloc_destroy(mem_ctx);
754 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
757 struct server_id server_id,
760 struct winbindd_child *child;
762 DEBUG(10,("winbind_msg_dump_event_list received\n"));
764 dump_event_list(winbind_event_context());
766 for (child = winbindd_children; child != NULL; child = child->next) {
768 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
769 (unsigned int)child->pid));
771 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
778 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
781 struct server_id server_id,
785 const char *message = NULL;
786 struct server_id *sender = NULL;
787 const char *domain = NULL;
790 struct winbindd_domain *dom = NULL;
792 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
794 if (!data || !data->data) {
798 if (data->length < sizeof(struct server_id)) {
802 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
807 sender = (struct server_id *)data->data;
808 if (data->length > sizeof(struct server_id)) {
809 domain = (const char *)data->data+sizeof(struct server_id);
814 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
817 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
818 find_domain_from_name_noinit(domain));
820 talloc_destroy(mem_ctx);
824 messaging_send_buf(msg_ctx, *sender,
825 MSG_WINBIND_DUMP_DOMAIN_LIST,
826 (const uint8_t *)message, strlen(message) + 1);
828 talloc_destroy(mem_ctx);
833 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
835 for (dom = domain_list(); dom; dom=dom->next) {
836 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
838 talloc_destroy(mem_ctx);
842 s = talloc_asprintf_append(s, "%s\n", message);
844 talloc_destroy(mem_ctx);
849 status = messaging_send_buf(msg_ctx, *sender,
850 MSG_WINBIND_DUMP_DOMAIN_LIST,
851 (uint8_t *)s, strlen(s) + 1);
852 if (!NT_STATUS_IS_OK(status)) {
853 DEBUG(0,("failed to send message: %s\n",
857 talloc_destroy(mem_ctx);
860 static void account_lockout_policy_handler(struct event_context *ctx,
861 struct timed_event *te,
865 struct winbindd_child *child =
866 (struct winbindd_child *)private_data;
867 TALLOC_CTX *mem_ctx = NULL;
868 struct winbindd_methods *methods;
869 struct samr_DomInfo12 lockout_policy;
872 DEBUG(10,("account_lockout_policy_handler called\n"));
874 TALLOC_FREE(child->lockout_policy_event);
876 if ( !winbindd_can_contact_domain( child->domain ) ) {
877 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
878 "do not have an incoming trust to domain %s\n",
879 child->domain->name));
884 methods = child->domain->methods;
886 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
888 result = NT_STATUS_NO_MEMORY;
890 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
892 TALLOC_FREE(mem_ctx);
894 if (!NT_STATUS_IS_OK(result)) {
895 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
899 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
900 timeval_current_ofs(3600, 0),
901 account_lockout_policy_handler,
905 static time_t get_machine_password_timeout(void)
907 /* until we have gpo support use lp setting */
908 return lp_machine_password_timeout();
911 static bool calculate_next_machine_pwd_change(const char *domain,
914 time_t pass_last_set_time;
920 pw = secrets_fetch_machine_password(domain,
925 DEBUG(0,("cannot fetch own machine password ????"));
931 timeout = get_machine_password_timeout();
933 DEBUG(10,("machine password never expires\n"));
937 tv.tv_sec = pass_last_set_time;
938 DEBUG(10, ("password last changed %s\n",
939 timeval_string(talloc_tos(), &tv, false)));
940 tv.tv_sec += timeout;
941 DEBUGADD(10, ("password valid until %s\n",
942 timeval_string(talloc_tos(), &tv, false)));
944 if (time(NULL) < (pass_last_set_time + timeout)) {
945 next_change = pass_last_set_time + timeout;
946 DEBUG(10,("machine password still valid until: %s\n",
947 http_timestring(talloc_tos(), next_change)));
948 *t = timeval_set(next_change, 0);
950 if (lp_clustering()) {
953 * When having a cluster, we have several
954 * winbinds racing for the password change. In
955 * the machine_password_change_handler()
956 * function we check if someone else was
957 * faster when the event triggers. We add a
958 * 255-second random delay here, so that we
959 * don't run to change the password at the
962 generate_random_buffer(&randbuf, sizeof(randbuf));
963 DEBUG(10, ("adding %d seconds randomness\n",
965 t->tv_sec += randbuf;
970 DEBUG(10,("machine password expired, needs immediate change\n"));
977 static void machine_password_change_handler(struct event_context *ctx,
978 struct timed_event *te,
982 struct winbindd_child *child =
983 (struct winbindd_child *)private_data;
984 struct rpc_pipe_client *netlogon_pipe = NULL;
987 struct timeval next_change;
989 DEBUG(10,("machine_password_change_handler called\n"));
991 TALLOC_FREE(child->machine_password_change_event);
993 if (!calculate_next_machine_pwd_change(child->domain->name,
995 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
999 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1000 timeval_string(talloc_tos(), &next_change, false)));
1002 if (!timeval_expired(&next_change)) {
1003 DEBUG(10, ("Someone else has already changed the pw\n"));
1007 if (!winbindd_can_contact_domain(child->domain)) {
1008 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1009 "do not have an incoming trust to domain %s\n",
1010 child->domain->name));
1014 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1015 if (!NT_STATUS_IS_OK(result)) {
1016 DEBUG(10,("machine_password_change_handler: "
1017 "failed to connect netlogon pipe: %s\n",
1018 nt_errstr(result)));
1022 frame = talloc_stackframe();
1024 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1026 child->domain->name);
1029 DEBUG(10, ("machine_password_change_handler: "
1030 "trust_pw_find_change_and_store_it returned %s\n",
1031 nt_errstr(result)));
1033 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1034 DEBUG(3,("machine_password_change_handler: password set returned "
1035 "ACCESS_DENIED. Maybe the trust account "
1036 "password was changed and we didn't know it. "
1037 "Killing connections to domain %s\n",
1038 child->domain->name));
1039 TALLOC_FREE(child->domain->conn.netlogon_pipe);
1042 if (!calculate_next_machine_pwd_change(child->domain->name,
1044 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1048 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1049 timeval_string(talloc_tos(), &next_change, false)));
1051 if (!NT_STATUS_IS_OK(result)) {
1054 * In case of failure, give the DC a minute to recover
1056 tmp = timeval_current_ofs(60, 0);
1057 next_change = timeval_max(&next_change, &tmp);
1061 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1063 machine_password_change_handler,
1067 /* Deal with a request to go offline. */
1069 static void child_msg_offline(struct messaging_context *msg,
1072 struct server_id server_id,
1075 struct winbindd_domain *domain;
1076 struct winbindd_domain *primary_domain = NULL;
1077 const char *domainname = (const char *)data->data;
1079 if (data->data == NULL || data->length == 0) {
1083 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1085 if (!lp_winbind_offline_logon()) {
1086 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1090 primary_domain = find_our_domain();
1092 /* Mark the requested domain offline. */
1094 for (domain = domain_list(); domain; domain = domain->next) {
1095 if (domain->internal) {
1098 if (strequal(domain->name, domainname)) {
1099 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1100 set_domain_offline(domain);
1101 /* we are in the trusted domain, set the primary domain
1103 if (domain != primary_domain) {
1104 set_domain_offline(primary_domain);
1110 /* Deal with a request to go online. */
1112 static void child_msg_online(struct messaging_context *msg,
1115 struct server_id server_id,
1118 struct winbindd_domain *domain;
1119 struct winbindd_domain *primary_domain = NULL;
1120 const char *domainname = (const char *)data->data;
1122 if (data->data == NULL || data->length == 0) {
1126 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1128 if (!lp_winbind_offline_logon()) {
1129 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1133 primary_domain = find_our_domain();
1135 /* Set our global state as online. */
1136 set_global_winbindd_state_online();
1138 /* Try and mark everything online - delete any negative cache entries
1139 to force a reconnect now. */
1141 for (domain = domain_list(); domain; domain = domain->next) {
1142 if (domain->internal) {
1145 if (strequal(domain->name, domainname)) {
1146 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1147 winbindd_flush_negative_conn_cache(domain);
1148 set_domain_online_request(domain);
1150 /* we can be in trusted domain, which will contact primary domain
1151 * we have to bring primary domain online in trusted domain process
1152 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1153 * --> contact_domain = find_our_domain()
1155 if (domain != primary_domain) {
1156 winbindd_flush_negative_conn_cache(primary_domain);
1157 set_domain_online_request(primary_domain);
1163 static void child_msg_dump_event_list(struct messaging_context *msg,
1166 struct server_id server_id,
1169 DEBUG(5,("child_msg_dump_event_list received\n"));
1171 dump_event_list(winbind_event_context());
1174 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1175 const char *logfilename)
1177 struct winbindd_domain *domain;
1178 struct winbindd_child *cl;
1181 status = reinit_after_fork(
1182 winbind_messaging_context(),
1183 winbind_event_context(),
1186 if (!NT_STATUS_IS_OK(status)) {
1187 DEBUG(0,("reinit_after_fork() failed\n"));
1191 close_conns_after_fork();
1193 if (!override_logfile && logfilename) {
1194 lp_set_logfile(logfilename);
1198 if (!winbindd_setup_sig_term_handler(false))
1199 return NT_STATUS_NO_MEMORY;
1200 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1202 return NT_STATUS_NO_MEMORY;
1204 /* Stop zombies in children */
1207 /* Don't handle the same messages as our parent. */
1208 messaging_deregister(winbind_messaging_context(),
1209 MSG_SMB_CONF_UPDATED, NULL);
1210 messaging_deregister(winbind_messaging_context(),
1211 MSG_SHUTDOWN, NULL);
1212 messaging_deregister(winbind_messaging_context(),
1213 MSG_WINBIND_OFFLINE, NULL);
1214 messaging_deregister(winbind_messaging_context(),
1215 MSG_WINBIND_ONLINE, NULL);
1216 messaging_deregister(winbind_messaging_context(),
1217 MSG_WINBIND_ONLINESTATUS, NULL);
1218 messaging_deregister(winbind_messaging_context(),
1219 MSG_DUMP_EVENT_LIST, NULL);
1220 messaging_deregister(winbind_messaging_context(),
1221 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1222 messaging_deregister(winbind_messaging_context(),
1225 /* We have destroyed all events in the winbindd_event_context
1226 * in reinit_after_fork(), so clean out all possible pending
1227 * event pointers. */
1229 /* Deal with check_online_events. */
1231 for (domain = domain_list(); domain; domain = domain->next) {
1232 TALLOC_FREE(domain->check_online_event);
1235 /* Ensure we're not handling a credential cache event inherited
1236 * from our parent. */
1238 ccache_remove_all_after_fork();
1240 /* Destroy all possible events in child list. */
1241 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1242 TALLOC_FREE(cl->lockout_policy_event);
1243 TALLOC_FREE(cl->machine_password_change_event);
1245 /* Children should never be able to send
1246 * each other messages, all messages must
1247 * go through the parent.
1252 * Close service sockets to all other children
1254 if ((cl != myself) && (cl->sock != -1)) {
1260 * This is a little tricky, children must not
1261 * send an MSG_WINBIND_ONLINE message to idmap_child().
1262 * If we are in a child of our primary domain or
1263 * in the process created by fork_child_dc_connect(),
1264 * and the primary domain cannot go online,
1265 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1266 * periodically to idmap_child().
1268 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1269 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1270 * ---> init_dc_connection() ---> cm_open_connection --->
1271 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1272 * idmap_child(). Disallow children sending messages
1273 * to each other, all messages must go through the parent.
1278 return NT_STATUS_OK;
1282 * In a child there will be only one domain, reference that here.
1284 static struct winbindd_domain *child_domain;
1286 struct winbindd_domain *wb_child_domain(void)
1288 return child_domain;
1291 static bool fork_domain_child(struct winbindd_child *child)
1294 struct winbindd_cli_state state;
1295 struct winbindd_request request;
1296 struct winbindd_response response;
1297 struct winbindd_domain *primary_domain = NULL;
1301 if (child->domain) {
1302 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1303 child->domain->name));
1305 DEBUG(10, ("fork_domain_child called without domain.\n"));
1308 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1309 DEBUG(0, ("Could not open child pipe: %s\n",
1315 state.pid = sys_getpid();
1316 state.request = &request;
1317 state.response = &response;
1319 child->pid = sys_fork();
1321 if (child->pid == -1) {
1322 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1326 if (child->pid != 0) {
1332 nread = read(fdpair[1], &status, sizeof(status));
1333 if (nread != sizeof(status)) {
1334 DEBUG(1, ("fork_domain_child: Could not read child status: "
1335 "nread=%d, error=%s\n", (int)nread,
1340 if (!NT_STATUS_IS_OK(status)) {
1341 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1342 nt_errstr(status)));
1347 child->next = child->prev = NULL;
1348 DLIST_ADD(winbindd_children, child);
1349 child->sock = fdpair[1];
1354 child_domain = child->domain;
1356 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1358 state.sock = fdpair[0];
1361 status = winbindd_reinit_after_fork(child, child->logfilename);
1363 nwritten = write(state.sock, &status, sizeof(status));
1364 if (nwritten != sizeof(status)) {
1365 DEBUG(1, ("fork_domain_child: Could not write status: "
1366 "nwritten=%d, error=%s\n", (int)nwritten,
1370 if (!NT_STATUS_IS_OK(status)) {
1371 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1372 nt_errstr(status)));
1376 /* Handle online/offline messages. */
1377 messaging_register(winbind_messaging_context(), NULL,
1378 MSG_WINBIND_OFFLINE, child_msg_offline);
1379 messaging_register(winbind_messaging_context(), NULL,
1380 MSG_WINBIND_ONLINE, child_msg_online);
1381 messaging_register(winbind_messaging_context(), NULL,
1382 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1383 messaging_register(winbind_messaging_context(), NULL,
1384 MSG_DEBUG, debug_message);
1385 messaging_register(winbind_messaging_context(), NULL,
1386 MSG_WINBIND_IP_DROPPED,
1387 winbind_msg_ip_dropped);
1390 primary_domain = find_our_domain();
1392 if (primary_domain == NULL) {
1393 smb_panic("no primary domain found");
1396 /* It doesn't matter if we allow cache login,
1397 * try to bring domain online after fork. */
1398 if ( child->domain ) {
1399 child->domain->startup = True;
1400 child->domain->startup_time = time_mono(NULL);
1401 /* we can be in primary domain or in trusted domain
1402 * If we are in trusted domain, set the primary domain
1403 * in start-up mode */
1404 if (!(child->domain->internal)) {
1405 set_domain_online_request(child->domain);
1406 if (!(child->domain->primary)) {
1407 primary_domain->startup = True;
1408 primary_domain->startup_time = time_mono(NULL);
1409 set_domain_online_request(primary_domain);
1415 * We are in idmap child, make sure that we set the
1416 * check_online_event to bring primary domain online.
1418 if (child == idmap_child()) {
1419 set_domain_online_request(primary_domain);
1422 /* We might be in the idmap child...*/
1423 if (child->domain && !(child->domain->internal) &&
1424 lp_winbind_offline_logon()) {
1426 set_domain_online_request(child->domain);
1428 if (primary_domain && (primary_domain != child->domain)) {
1429 /* We need to talk to the primary
1430 * domain as well as the trusted
1431 * domain inside a trusted domain
1434 * set_dc_type_and_flags_trustinfo()
1437 set_domain_online_request(primary_domain);
1440 child->lockout_policy_event = event_add_timed(
1441 winbind_event_context(), NULL, timeval_zero(),
1442 account_lockout_policy_handler,
1446 if (child->domain && child->domain->primary &&
1447 !USE_KERBEROS_KEYTAB &&
1448 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1450 struct timeval next_change;
1452 if (calculate_next_machine_pwd_change(child->domain->name,
1454 child->machine_password_change_event = event_add_timed(
1455 winbind_event_context(), NULL, next_change,
1456 machine_password_change_handler,
1464 struct pollfd *pfds;
1469 TALLOC_CTX *frame = talloc_stackframe();
1470 struct iovec iov[2];
1473 if (run_events_poll(winbind_event_context(), 0, NULL, 0)) {
1478 if (child->domain && child->domain->startup &&
1479 (time_mono(NULL) > child->domain->startup_time + 30)) {
1480 /* No longer in "startup" mode. */
1481 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1482 child->domain->name ));
1483 child->domain->startup = False;
1486 pfds = talloc_zero(talloc_tos(), struct pollfd);
1488 DEBUG(1, ("talloc failed\n"));
1492 pfds->fd = state.sock;
1493 pfds->events = POLLIN|POLLHUP;
1498 if (!event_add_to_poll_args(
1499 winbind_event_context(), talloc_tos(),
1500 &pfds, &num_pfds, &timeout)) {
1501 DEBUG(1, ("event_add_to_poll_args failed\n"));
1504 tp = get_timed_events_timeout(winbind_event_context(), &t);
1506 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1507 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1510 ret = sys_poll(pfds, num_pfds, timeout);
1512 if (run_events_poll(winbind_event_context(), ret,
1514 /* We got a signal - continue. */
1522 DEBUG(11,("nothing is ready yet, continue\n"));
1527 if (ret == -1 && errno == EINTR) {
1528 /* We got a signal - continue. */
1533 if (ret == -1 && errno != EINTR) {
1534 DEBUG(0,("poll error occured\n"));
1540 /* fetch a request from the main daemon */
1541 status = child_read_request(&state);
1543 if (!NT_STATUS_IS_OK(status)) {
1544 /* we lost contact with our parent */
1548 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1550 ZERO_STRUCTP(state.response);
1551 state.request->null_term = '\0';
1552 state.mem_ctx = frame;
1553 child_process_request(child, &state);
1555 DEBUG(4, ("Finished processing child request %d\n",
1556 (int)state.request->cmd));
1558 SAFE_FREE(state.request->extra_data.data);
1560 iov[0].iov_base = (void *)state.response;
1561 iov[0].iov_len = sizeof(struct winbindd_response);
1564 if (state.response->length > sizeof(struct winbindd_response)) {
1566 (void *)state.response->extra_data.data;
1567 iov[1].iov_len = state.response->length-iov[0].iov_len;
1571 DEBUG(10, ("Writing %d bytes to parent\n",
1572 (int)state.response->length));
1574 if (write_data_iov(state.sock, iov, iov_count) !=
1575 state.response->length) {
1576 DEBUG(0, ("Could not write result\n"));
1583 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1586 struct server_id server_id,
1589 struct winbindd_child *child;
1591 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1595 for (child = winbindd_children; child != NULL; child = child->next) {
1596 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1597 msg_type, data->data, data->length);