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"
41 #include "lib/util/sys_rw.h"
42 #include "lib/util/sys_rw_data.h"
45 #define DBGC_CLASS DBGC_WINBIND
47 extern bool override_logfile;
49 static struct winbindd_child *winbindd_children = NULL;
51 /* Read some data from a client connection */
53 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
57 status = read_data_ntstatus(sock, (char *)wreq, sizeof(*wreq));
58 if (!NT_STATUS_IS_OK(status)) {
59 DEBUG(3, ("child_read_request: read_data failed: %s\n",
64 if (wreq->extra_len == 0) {
65 wreq->extra_data.data = NULL;
69 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
71 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
72 if (wreq->extra_data.data == NULL) {
73 DEBUG(0, ("malloc failed\n"));
74 return NT_STATUS_NO_MEMORY;
77 /* Ensure null termination */
78 wreq->extra_data.data[wreq->extra_len] = '\0';
80 status = read_data_ntstatus(sock, wreq->extra_data.data,
82 if (!NT_STATUS_IS_OK(status)) {
83 DEBUG(0, ("Could not read extra data: %s\n",
89 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
94 iov[0].iov_base = (void *)wrsp;
95 iov[0].iov_len = sizeof(struct winbindd_response);
98 if (wrsp->length > sizeof(struct winbindd_response)) {
99 iov[1].iov_base = (void *)wrsp->extra_data.data;
100 iov[1].iov_len = wrsp->length-iov[0].iov_len;
104 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
106 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
107 DEBUG(0, ("Could not write result\n"));
108 return NT_STATUS_INVALID_HANDLE;
115 * Do winbind child async request. This is not simply wb_simple_trans. We have
116 * to do the queueing ourselves because while a request is queued, the child
117 * might have crashed, and we have to re-fork it in the _trigger function.
120 struct wb_child_request_state {
121 struct tevent_context *ev;
122 struct tevent_req *subreq;
123 struct winbindd_child *child;
124 struct winbindd_request *request;
125 struct winbindd_response *response;
128 static bool fork_domain_child(struct winbindd_child *child);
130 static void wb_child_request_trigger(struct tevent_req *req,
132 static void wb_child_request_done(struct tevent_req *subreq);
134 static void wb_child_request_cleanup(struct tevent_req *req,
135 enum tevent_req_state req_state);
137 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
138 struct tevent_context *ev,
139 struct winbindd_child *child,
140 struct winbindd_request *request)
142 struct tevent_req *req;
143 struct wb_child_request_state *state;
145 req = tevent_req_create(mem_ctx, &state,
146 struct wb_child_request_state);
152 state->child = child;
153 state->request = request;
155 if (!tevent_queue_add(child->queue, ev, req,
156 wb_child_request_trigger, NULL)) {
158 return tevent_req_post(req, ev);
161 tevent_req_set_cleanup_fn(req, wb_child_request_cleanup);
166 static void wb_child_request_trigger(struct tevent_req *req,
169 struct wb_child_request_state *state = tevent_req_data(
170 req, struct wb_child_request_state);
171 struct tevent_req *subreq;
173 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
174 tevent_req_error(req, errno);
178 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
179 state->child->sock, state->request);
180 if (tevent_req_nomem(subreq, req)) {
184 state->subreq = subreq;
185 tevent_req_set_callback(subreq, wb_child_request_done, req);
186 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
189 static void wb_child_request_done(struct tevent_req *subreq)
191 struct tevent_req *req = tevent_req_callback_data(
192 subreq, struct tevent_req);
193 struct wb_child_request_state *state = tevent_req_data(
194 req, struct wb_child_request_state);
197 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
198 /* Freeing the subrequest is deferred until the cleanup function,
199 * which has to know whether a subrequest exists, and consequently
200 * decide whether to shut down the pipe to the child process.
203 tevent_req_error(req, err);
206 tevent_req_done(req);
209 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
210 struct winbindd_response **presponse, int *err)
212 struct wb_child_request_state *state = tevent_req_data(
213 req, struct wb_child_request_state);
215 if (tevent_req_is_unix_error(req, err)) {
218 *presponse = talloc_move(mem_ctx, &state->response);
222 static void wb_child_request_cleanup(struct tevent_req *req,
223 enum tevent_req_state req_state)
225 struct wb_child_request_state *state =
226 tevent_req_data(req, struct wb_child_request_state);
228 if (state->subreq == NULL) {
229 /* nothing to cleanup */
233 TALLOC_FREE(state->subreq);
235 if (req_state == TEVENT_REQ_DONE) {
236 /* transmitted request and got response */
241 * Failed to transmit and receive response, or request
242 * cancelled while being serviced.
243 * The basic parent/child communication broke, close
246 close(state->child->sock);
247 state->child->sock = -1;
248 DLIST_REMOVE(winbindd_children, state->child);
251 static bool winbindd_child_busy(struct winbindd_child *child)
253 return tevent_queue_length(child->queue) > 0;
256 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
260 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
261 if (!winbindd_child_busy(&domain->children[i])) {
262 return &domain->children[i];
269 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
271 struct winbindd_child *result;
273 result = find_idle_child(domain);
274 if (result != NULL) {
277 return &domain->children[rand() % lp_winbind_max_domain_connections()];
280 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
282 struct winbindd_child *child;
284 child = choose_domain_child(domain);
285 return child->binding_handle;
288 struct wb_domain_request_state {
289 struct tevent_context *ev;
290 struct winbindd_domain *domain;
291 struct winbindd_child *child;
292 struct winbindd_request *request;
293 struct winbindd_request *init_req;
294 struct winbindd_response *response;
297 static void wb_domain_request_gotdc(struct tevent_req *subreq);
298 static void wb_domain_request_initialized(struct tevent_req *subreq);
299 static void wb_domain_request_done(struct tevent_req *subreq);
301 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
302 struct tevent_context *ev,
303 struct winbindd_domain *domain,
304 struct winbindd_request *request)
306 struct tevent_req *req, *subreq;
307 struct wb_domain_request_state *state;
309 req = tevent_req_create(mem_ctx, &state,
310 struct wb_domain_request_state);
315 state->child = choose_domain_child(domain);
317 if (domain->initialized) {
318 subreq = wb_child_request_send(state, ev, state->child,
320 if (tevent_req_nomem(subreq, req)) {
321 return tevent_req_post(req, ev);
323 tevent_req_set_callback(subreq, wb_domain_request_done, req);
327 state->domain = domain;
329 state->request = request;
331 state->init_req = talloc_zero(state, struct winbindd_request);
332 if (tevent_req_nomem(state->init_req, req)) {
333 return tevent_req_post(req, ev);
336 if (IS_DC || domain->primary || domain->internal) {
337 /* The primary domain has to find the DC name itself */
338 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
339 fstrcpy(state->init_req->domain_name, domain->name);
340 state->init_req->data.init_conn.is_primary = domain->primary;
341 fstrcpy(state->init_req->data.init_conn.dcname, "");
343 subreq = wb_child_request_send(state, ev, state->child,
345 if (tevent_req_nomem(subreq, req)) {
346 return tevent_req_post(req, ev);
348 tevent_req_set_callback(subreq, wb_domain_request_initialized,
354 * Ask our DC for a DC name
356 domain = find_our_domain();
358 /* This is *not* the primary domain, let's ask our DC about a DC
361 state->init_req->cmd = WINBINDD_GETDCNAME;
362 fstrcpy(state->init_req->domain_name, domain->name);
364 subreq = wb_child_request_send(state, ev, state->child, request);
365 if (tevent_req_nomem(subreq, req)) {
366 return tevent_req_post(req, ev);
368 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
372 static void wb_domain_request_gotdc(struct tevent_req *subreq)
374 struct tevent_req *req = tevent_req_callback_data(
375 subreq, struct tevent_req);
376 struct wb_domain_request_state *state = tevent_req_data(
377 req, struct wb_domain_request_state);
378 struct winbindd_response *response;
381 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
384 tevent_req_error(req, err);
387 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
388 fstrcpy(state->init_req->domain_name, state->domain->name);
389 state->init_req->data.init_conn.is_primary = False;
390 fstrcpy(state->init_req->data.init_conn.dcname,
391 response->data.dc_name);
393 TALLOC_FREE(response);
395 subreq = wb_child_request_send(state, state->ev, state->child,
397 if (tevent_req_nomem(subreq, req)) {
400 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
403 static void wb_domain_request_initialized(struct tevent_req *subreq)
405 struct tevent_req *req = tevent_req_callback_data(
406 subreq, struct tevent_req);
407 struct wb_domain_request_state *state = tevent_req_data(
408 req, struct wb_domain_request_state);
409 struct winbindd_response *response;
412 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
415 tevent_req_error(req, err);
419 if (!string_to_sid(&state->domain->sid,
420 response->data.domain_info.sid)) {
421 DEBUG(1,("init_child_recv: Could not convert sid %s "
422 "from string\n", response->data.domain_info.sid));
423 tevent_req_error(req, EINVAL);
427 talloc_free(state->domain->name);
428 state->domain->name = talloc_strdup(state->domain,
429 response->data.domain_info.name);
430 if (state->domain->name == NULL) {
431 tevent_req_error(req, ENOMEM);
435 if (response->data.domain_info.alt_name[0] != '\0') {
436 talloc_free(state->domain->alt_name);
438 state->domain->alt_name = talloc_strdup(state->domain,
439 response->data.domain_info.alt_name);
440 if (state->domain->alt_name == NULL) {
441 tevent_req_error(req, ENOMEM);
446 state->domain->native_mode = response->data.domain_info.native_mode;
447 state->domain->active_directory =
448 response->data.domain_info.active_directory;
449 state->domain->initialized = true;
451 TALLOC_FREE(response);
453 subreq = wb_child_request_send(state, state->ev, state->child,
455 if (tevent_req_nomem(subreq, req)) {
458 tevent_req_set_callback(subreq, wb_domain_request_done, req);
461 static void wb_domain_request_done(struct tevent_req *subreq)
463 struct tevent_req *req = tevent_req_callback_data(
464 subreq, struct tevent_req);
465 struct wb_domain_request_state *state = tevent_req_data(
466 req, struct wb_domain_request_state);
469 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
473 tevent_req_error(req, err);
476 tevent_req_done(req);
479 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
480 struct winbindd_response **presponse, int *err)
482 struct wb_domain_request_state *state = tevent_req_data(
483 req, struct wb_domain_request_state);
485 if (tevent_req_is_unix_error(req, err)) {
488 *presponse = talloc_move(mem_ctx, &state->response);
492 static void child_process_request(struct winbindd_child *child,
493 struct winbindd_cli_state *state)
495 struct winbindd_domain *domain = child->domain;
496 const struct winbindd_child_dispatch_table *table = child->table;
498 /* Free response data - we may be interrupted and receive another
499 command before being able to send this data off. */
501 state->response->result = WINBINDD_ERROR;
502 state->response->length = sizeof(struct winbindd_response);
504 /* as all requests in the child are sync, we can use talloc_tos() */
505 state->mem_ctx = talloc_tos();
507 /* Process command */
509 for (; table->name; table++) {
510 if (state->request->cmd == table->struct_cmd) {
511 DEBUG(10,("child_process_request: request fn %s\n",
513 state->response->result = table->struct_fn(domain, state);
518 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
519 (int)state->request->cmd));
520 state->response->result = WINBINDD_ERROR;
523 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
524 const struct winbindd_child_dispatch_table *table,
525 const char *logprefix,
528 if (logprefix && logname) {
529 char *logbase = NULL;
531 if (*lp_logfile(talloc_tos())) {
534 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
535 smb_panic("Internal error: asprintf failed");
538 if ((end = strrchr_m(logbase, '/'))) {
542 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
543 smb_panic("Internal error: asprintf failed");
547 if (asprintf(&child->logfilename, "%s/%s-%s",
548 logbase, logprefix, logname) < 0) {
550 smb_panic("Internal error: asprintf failed");
555 smb_panic("Internal error: logprefix == NULL && "
560 child->domain = domain;
561 child->table = table;
562 child->queue = tevent_queue_create(NULL, "winbind_child");
563 SMB_ASSERT(child->queue != NULL);
564 child->binding_handle = wbint_binding_handle(NULL, domain, child);
565 SMB_ASSERT(child->binding_handle != NULL);
568 void winbind_child_died(pid_t pid)
570 struct winbindd_child *child;
572 for (child = winbindd_children; child != NULL; child = child->next) {
573 if (child->pid == pid) {
579 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
583 /* This will be re-added in fork_domain_child() */
585 DLIST_REMOVE(winbindd_children, child);
588 if (child->sock != -1) {
594 /* Ensure any negative cache entries with the netbios or realm names are removed. */
596 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
598 flush_negative_conn_cache_for_domain(domain->name);
599 if (domain->alt_name != NULL) {
600 flush_negative_conn_cache_for_domain(domain->alt_name);
605 * Parent winbindd process sets its own debug level first and then
606 * sends a message to all the winbindd children to adjust their debug
607 * level to that of parents.
610 void winbind_msg_debug(struct messaging_context *msg_ctx,
613 struct server_id server_id,
616 struct winbindd_child *child;
618 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
620 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
622 for (child = winbindd_children; child != NULL; child = child->next) {
624 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
625 (unsigned int)child->pid));
627 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
630 strlen((char *) data->data) + 1);
634 /* Set our domains as offline and forward the offline message to our children. */
636 void winbind_msg_offline(struct messaging_context *msg_ctx,
639 struct server_id server_id,
642 struct winbindd_child *child;
643 struct winbindd_domain *domain;
645 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
647 if (!lp_winbind_offline_logon()) {
648 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
652 /* Set our global state as offline. */
653 if (!set_global_winbindd_state_offline()) {
654 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
658 /* Set all our domains as offline. */
659 for (domain = domain_list(); domain; domain = domain->next) {
660 if (domain->internal) {
663 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
664 set_domain_offline(domain);
667 for (child = winbindd_children; child != NULL; child = child->next) {
668 /* Don't send message to internal children. We've already
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_offline: 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 (const uint8_t *)child->domain->name,
688 strlen(child->domain->name)+1);
692 /* Set our domains as online and forward the online message to our children. */
694 void winbind_msg_online(struct messaging_context *msg_ctx,
697 struct server_id server_id,
700 struct winbindd_child *child;
701 struct winbindd_domain *domain;
703 DEBUG(10,("winbind_msg_online: got online message.\n"));
705 if (!lp_winbind_offline_logon()) {
706 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
710 /* Set our global state as online. */
711 set_global_winbindd_state_online();
713 smb_nscd_flush_user_cache();
714 smb_nscd_flush_group_cache();
716 /* Set all our domains as online. */
717 for (domain = domain_list(); domain; domain = domain->next) {
718 if (domain->internal) {
721 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
723 winbindd_flush_negative_conn_cache(domain);
724 set_domain_online_request(domain);
726 /* Send an online message to the idmap child when our
727 primary domain comes back online */
729 if ( domain->primary ) {
730 struct winbindd_child *idmap = idmap_child();
732 if ( idmap->pid != 0 ) {
733 messaging_send_buf(msg_ctx,
734 pid_to_procid(idmap->pid),
736 (const uint8_t *)domain->name,
737 strlen(domain->name)+1);
742 for (child = winbindd_children; child != NULL; child = child->next) {
743 /* Don't send message to internal childs. */
744 if (!child->domain || winbindd_internal_child(child)) {
748 /* Or internal domains (this should not be possible....) */
749 if (child->domain->internal) {
753 /* Each winbindd child should only process requests for one domain - make sure
754 we only set it online / offline for that domain. */
756 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
757 (unsigned int)child->pid, child->domain->name ));
759 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
761 (const uint8_t *)child->domain->name,
762 strlen(child->domain->name)+1);
766 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
768 struct winbindd_domain *domain;
771 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
772 get_global_winbindd_state_offline() ?
773 "Offline":"Online")) == NULL) {
777 for (domain = domain_list(); domain; domain = domain->next) {
778 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
781 "Online":"Offline")) == NULL) {
786 buf = talloc_asprintf_append_buffer(buf, "\n");
788 DEBUG(5,("collect_onlinestatus: %s", buf));
793 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
796 struct server_id server_id,
801 struct server_id *sender;
803 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
809 sender = (struct server_id *)data->data;
811 mem_ctx = talloc_init("winbind_msg_onlinestatus");
812 if (mem_ctx == NULL) {
816 message = collect_onlinestatus(mem_ctx);
817 if (message == NULL) {
818 talloc_destroy(mem_ctx);
822 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
823 (const uint8_t *)message, strlen(message) + 1);
825 talloc_destroy(mem_ctx);
828 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
831 struct server_id server_id,
834 struct winbindd_child *child;
836 DEBUG(10,("winbind_msg_dump_event_list received\n"));
838 DBG_WARNING("dump event list no longer implemented\n");
840 for (child = winbindd_children; child != NULL; child = child->next) {
842 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
843 (unsigned int)child->pid));
845 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
852 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
855 struct server_id server_id,
859 const char *message = NULL;
860 struct server_id *sender = NULL;
861 const char *domain = NULL;
864 struct winbindd_domain *dom = NULL;
866 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
868 if (!data || !data->data) {
872 if (data->length < sizeof(struct server_id)) {
876 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
881 sender = (struct server_id *)data->data;
882 if (data->length > sizeof(struct server_id)) {
883 domain = (const char *)data->data+sizeof(struct server_id);
888 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
891 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
892 find_domain_from_name_noinit(domain));
894 talloc_destroy(mem_ctx);
898 messaging_send_buf(msg_ctx, *sender,
899 MSG_WINBIND_DUMP_DOMAIN_LIST,
900 (const uint8_t *)message, strlen(message) + 1);
902 talloc_destroy(mem_ctx);
907 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
909 for (dom = domain_list(); dom; dom=dom->next) {
910 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
912 talloc_destroy(mem_ctx);
916 s = talloc_asprintf_append(s, "%s\n", message);
918 talloc_destroy(mem_ctx);
923 status = messaging_send_buf(msg_ctx, *sender,
924 MSG_WINBIND_DUMP_DOMAIN_LIST,
925 (uint8_t *)s, strlen(s) + 1);
926 if (!NT_STATUS_IS_OK(status)) {
927 DEBUG(0,("failed to send message: %s\n",
931 talloc_destroy(mem_ctx);
934 static void account_lockout_policy_handler(struct tevent_context *ctx,
935 struct tevent_timer *te,
939 struct winbindd_child *child =
940 (struct winbindd_child *)private_data;
941 TALLOC_CTX *mem_ctx = NULL;
942 struct samr_DomInfo12 lockout_policy;
945 DEBUG(10,("account_lockout_policy_handler called\n"));
947 TALLOC_FREE(child->lockout_policy_event);
949 if ( !winbindd_can_contact_domain( child->domain ) ) {
950 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
951 "do not have an incoming trust to domain %s\n",
952 child->domain->name));
957 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
959 result = NT_STATUS_NO_MEMORY;
961 result = wb_cache_lockout_policy(child->domain, mem_ctx,
964 TALLOC_FREE(mem_ctx);
966 if (!NT_STATUS_IS_OK(result)) {
967 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
971 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
972 timeval_current_ofs(3600, 0),
973 account_lockout_policy_handler,
977 static time_t get_machine_password_timeout(void)
979 /* until we have gpo support use lp setting */
980 return lp_machine_password_timeout();
983 static bool calculate_next_machine_pwd_change(const char *domain,
986 time_t pass_last_set_time;
992 pw = secrets_fetch_machine_password(domain,
997 DEBUG(0,("cannot fetch own machine password ????"));
1003 timeout = get_machine_password_timeout();
1005 DEBUG(10,("machine password never expires\n"));
1009 tv.tv_sec = pass_last_set_time;
1010 DEBUG(10, ("password last changed %s\n",
1011 timeval_string(talloc_tos(), &tv, false)));
1012 tv.tv_sec += timeout;
1013 DEBUGADD(10, ("password valid until %s\n",
1014 timeval_string(talloc_tos(), &tv, false)));
1016 if (time(NULL) < (pass_last_set_time + timeout)) {
1017 next_change = pass_last_set_time + timeout;
1018 DEBUG(10,("machine password still valid until: %s\n",
1019 http_timestring(talloc_tos(), next_change)));
1020 *t = timeval_set(next_change, 0);
1022 if (lp_clustering()) {
1025 * When having a cluster, we have several
1026 * winbinds racing for the password change. In
1027 * the machine_password_change_handler()
1028 * function we check if someone else was
1029 * faster when the event triggers. We add a
1030 * 255-second random delay here, so that we
1031 * don't run to change the password at the
1032 * exact same moment.
1034 generate_random_buffer(&randbuf, sizeof(randbuf));
1035 DEBUG(10, ("adding %d seconds randomness\n",
1037 t->tv_sec += randbuf;
1042 DEBUG(10,("machine password expired, needs immediate change\n"));
1044 *t = timeval_zero();
1049 static void machine_password_change_handler(struct tevent_context *ctx,
1050 struct tevent_timer *te,
1054 struct messaging_context *msg_ctx = winbind_messaging_context();
1055 struct winbindd_child *child =
1056 (struct winbindd_child *)private_data;
1057 struct rpc_pipe_client *netlogon_pipe = NULL;
1059 struct timeval next_change;
1061 DEBUG(10,("machine_password_change_handler called\n"));
1063 TALLOC_FREE(child->machine_password_change_event);
1065 if (!calculate_next_machine_pwd_change(child->domain->name,
1067 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1071 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1072 timeval_string(talloc_tos(), &next_change, false)));
1074 if (!timeval_expired(&next_change)) {
1075 DEBUG(10, ("Someone else has already changed the pw\n"));
1079 if (!winbindd_can_contact_domain(child->domain)) {
1080 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1081 "do not have an incoming trust to domain %s\n",
1082 child->domain->name));
1086 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1087 if (!NT_STATUS_IS_OK(result)) {
1088 DEBUG(10,("machine_password_change_handler: "
1089 "failed to connect netlogon pipe: %s\n",
1090 nt_errstr(result)));
1094 result = trust_pw_change(child->domain->conn.netlogon_creds,
1096 netlogon_pipe->binding_handle,
1097 child->domain->name,
1098 child->domain->dcname,
1101 DEBUG(10, ("machine_password_change_handler: "
1102 "trust_pw_change returned %s\n",
1103 nt_errstr(result)));
1105 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1106 DEBUG(3,("machine_password_change_handler: password set returned "
1107 "ACCESS_DENIED. Maybe the trust account "
1108 "password was changed and we didn't know it. "
1109 "Killing connections to domain %s\n",
1110 child->domain->name));
1111 invalidate_cm_connection(child->domain);
1114 if (!calculate_next_machine_pwd_change(child->domain->name,
1116 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1120 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1121 timeval_string(talloc_tos(), &next_change, false)));
1123 if (!NT_STATUS_IS_OK(result)) {
1126 * In case of failure, give the DC a minute to recover
1128 tmp = timeval_current_ofs(60, 0);
1129 next_change = timeval_max(&next_change, &tmp);
1133 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1135 machine_password_change_handler,
1139 /* Deal with a request to go offline. */
1141 static void child_msg_offline(struct messaging_context *msg,
1144 struct server_id server_id,
1147 struct winbindd_domain *domain;
1148 struct winbindd_domain *primary_domain = NULL;
1149 const char *domainname = (const char *)data->data;
1151 if (data->data == NULL || data->length == 0) {
1155 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1157 if (!lp_winbind_offline_logon()) {
1158 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1162 primary_domain = find_our_domain();
1164 /* Mark the requested domain offline. */
1166 for (domain = domain_list(); domain; domain = domain->next) {
1167 if (domain->internal) {
1170 if (strequal(domain->name, domainname)) {
1171 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1172 set_domain_offline(domain);
1173 /* we are in the trusted domain, set the primary domain
1175 if (domain != primary_domain) {
1176 set_domain_offline(primary_domain);
1182 /* Deal with a request to go online. */
1184 static void child_msg_online(struct messaging_context *msg,
1187 struct server_id server_id,
1190 struct winbindd_domain *domain;
1191 struct winbindd_domain *primary_domain = NULL;
1192 const char *domainname = (const char *)data->data;
1194 if (data->data == NULL || data->length == 0) {
1198 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1200 if (!lp_winbind_offline_logon()) {
1201 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1205 primary_domain = find_our_domain();
1207 /* Set our global state as online. */
1208 set_global_winbindd_state_online();
1210 /* Try and mark everything online - delete any negative cache entries
1211 to force a reconnect now. */
1213 for (domain = domain_list(); domain; domain = domain->next) {
1214 if (domain->internal) {
1217 if (strequal(domain->name, domainname)) {
1218 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1219 winbindd_flush_negative_conn_cache(domain);
1220 set_domain_online_request(domain);
1222 /* we can be in trusted domain, which will contact primary domain
1223 * we have to bring primary domain online in trusted domain process
1224 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1225 * --> contact_domain = find_our_domain()
1227 if (domain != primary_domain) {
1228 winbindd_flush_negative_conn_cache(primary_domain);
1229 set_domain_online_request(primary_domain);
1235 static void child_msg_dump_event_list(struct messaging_context *msg,
1238 struct server_id server_id,
1241 DEBUG(5,("child_msg_dump_event_list received\n"));
1242 DBG_WARNING("dump_event_list no longer implemented\n");
1245 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1246 const char *logfilename)
1248 struct winbindd_domain *domain;
1249 struct winbindd_child *cl;
1252 status = reinit_after_fork(
1253 winbind_messaging_context(),
1254 winbind_event_context(),
1256 if (!NT_STATUS_IS_OK(status)) {
1257 DEBUG(0,("reinit_after_fork() failed\n"));
1261 close_conns_after_fork();
1263 if (!override_logfile && logfilename) {
1264 lp_set_logfile(logfilename);
1268 if (!winbindd_setup_sig_term_handler(false))
1269 return NT_STATUS_NO_MEMORY;
1270 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1272 return NT_STATUS_NO_MEMORY;
1274 /* Stop zombies in children */
1277 /* Don't handle the same messages as our parent. */
1278 messaging_deregister(winbind_messaging_context(),
1279 MSG_SMB_CONF_UPDATED, NULL);
1280 messaging_deregister(winbind_messaging_context(),
1281 MSG_SHUTDOWN, NULL);
1282 messaging_deregister(winbind_messaging_context(),
1283 MSG_WINBIND_OFFLINE, NULL);
1284 messaging_deregister(winbind_messaging_context(),
1285 MSG_WINBIND_ONLINE, NULL);
1286 messaging_deregister(winbind_messaging_context(),
1287 MSG_WINBIND_ONLINESTATUS, NULL);
1288 messaging_deregister(winbind_messaging_context(),
1289 MSG_DUMP_EVENT_LIST, NULL);
1290 messaging_deregister(winbind_messaging_context(),
1291 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1292 messaging_deregister(winbind_messaging_context(),
1295 messaging_deregister(winbind_messaging_context(),
1296 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1297 messaging_deregister(winbind_messaging_context(),
1298 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1300 /* We have destroyed all events in the winbindd_event_context
1301 * in reinit_after_fork(), so clean out all possible pending
1302 * event pointers. */
1304 /* Deal with check_online_events. */
1306 for (domain = domain_list(); domain; domain = domain->next) {
1307 TALLOC_FREE(domain->check_online_event);
1310 /* Ensure we're not handling a credential cache event inherited
1311 * from our parent. */
1313 ccache_remove_all_after_fork();
1315 /* Destroy all possible events in child list. */
1316 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1317 TALLOC_FREE(cl->lockout_policy_event);
1318 TALLOC_FREE(cl->machine_password_change_event);
1320 /* Children should never be able to send
1321 * each other messages, all messages must
1322 * go through the parent.
1327 * Close service sockets to all other children
1329 if ((cl != myself) && (cl->sock != -1)) {
1335 * This is a little tricky, children must not
1336 * send an MSG_WINBIND_ONLINE message to idmap_child().
1337 * If we are in a child of our primary domain or
1338 * in the process created by fork_child_dc_connect(),
1339 * and the primary domain cannot go online,
1340 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1341 * periodically to idmap_child().
1343 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1344 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1345 * ---> init_dc_connection() ---> cm_open_connection --->
1346 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1347 * idmap_child(). Disallow children sending messages
1348 * to each other, all messages must go through the parent.
1353 return NT_STATUS_OK;
1357 * In a child there will be only one domain, reference that here.
1359 static struct winbindd_domain *child_domain;
1361 struct winbindd_domain *wb_child_domain(void)
1363 return child_domain;
1366 struct child_handler_state {
1367 struct winbindd_child *child;
1368 struct winbindd_cli_state cli;
1371 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1372 uint16_t flags, void *private_data)
1374 struct child_handler_state *state =
1375 (struct child_handler_state *)private_data;
1378 /* fetch a request from the main daemon */
1379 status = child_read_request(state->cli.sock, state->cli.request);
1381 if (!NT_STATUS_IS_OK(status)) {
1382 /* we lost contact with our parent */
1386 DEBUG(4,("child daemon request %d\n",
1387 (int)state->cli.request->cmd));
1389 ZERO_STRUCTP(state->cli.response);
1390 state->cli.request->null_term = '\0';
1391 state->cli.mem_ctx = talloc_tos();
1392 child_process_request(state->child, &state->cli);
1394 DEBUG(4, ("Finished processing child request %d\n",
1395 (int)state->cli.request->cmd));
1397 SAFE_FREE(state->cli.request->extra_data.data);
1399 status = child_write_response(state->cli.sock, state->cli.response);
1400 if (!NT_STATUS_IS_OK(status)) {
1405 static bool fork_domain_child(struct winbindd_child *child)
1408 struct child_handler_state state;
1409 struct winbindd_request request;
1410 struct winbindd_response response;
1411 struct winbindd_domain *primary_domain = NULL;
1414 struct tevent_fd *fde;
1416 if (child->domain) {
1417 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1418 child->domain->name));
1420 DEBUG(10, ("fork_domain_child called without domain.\n"));
1423 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1424 DEBUG(0, ("Could not open child pipe: %s\n",
1430 state.child = child;
1431 state.cli.pid = getpid();
1432 state.cli.request = &request;
1433 state.cli.response = &response;
1435 child->pid = fork();
1437 if (child->pid == -1) {
1438 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1444 if (child->pid != 0) {
1450 nread = sys_read(fdpair[1], &status, sizeof(status));
1451 if (nread != sizeof(status)) {
1452 DEBUG(1, ("fork_domain_child: Could not read child status: "
1453 "nread=%d, error=%s\n", (int)nread,
1458 if (!NT_STATUS_IS_OK(status)) {
1459 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1460 nt_errstr(status)));
1465 child->next = child->prev = NULL;
1466 DLIST_ADD(winbindd_children, child);
1467 child->sock = fdpair[1];
1472 child_domain = child->domain;
1474 DEBUG(10, ("Child process %d\n", (int)getpid()));
1476 state.cli.sock = fdpair[0];
1479 status = winbindd_reinit_after_fork(child, child->logfilename);
1481 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1482 if (nwritten != sizeof(status)) {
1483 DEBUG(1, ("fork_domain_child: Could not write status: "
1484 "nwritten=%d, error=%s\n", (int)nwritten,
1488 if (!NT_STATUS_IS_OK(status)) {
1489 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1490 nt_errstr(status)));
1494 /* Handle online/offline messages. */
1495 messaging_register(winbind_messaging_context(), NULL,
1496 MSG_WINBIND_OFFLINE, child_msg_offline);
1497 messaging_register(winbind_messaging_context(), NULL,
1498 MSG_WINBIND_ONLINE, child_msg_online);
1499 messaging_register(winbind_messaging_context(), NULL,
1500 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1501 messaging_register(winbind_messaging_context(), NULL,
1502 MSG_DEBUG, debug_message);
1503 messaging_register(winbind_messaging_context(), NULL,
1504 MSG_WINBIND_IP_DROPPED,
1505 winbind_msg_ip_dropped);
1508 primary_domain = find_our_domain();
1510 if (primary_domain == NULL) {
1511 smb_panic("no primary domain found");
1514 /* It doesn't matter if we allow cache login,
1515 * try to bring domain online after fork. */
1516 if ( child->domain ) {
1517 child->domain->startup = True;
1518 child->domain->startup_time = time_mono(NULL);
1519 /* we can be in primary domain or in trusted domain
1520 * If we are in trusted domain, set the primary domain
1521 * in start-up mode */
1522 if (!(child->domain->internal)) {
1523 set_domain_online_request(child->domain);
1524 if (!(child->domain->primary)) {
1525 primary_domain->startup = True;
1526 primary_domain->startup_time = time_mono(NULL);
1527 set_domain_online_request(primary_domain);
1533 * We are in idmap child, make sure that we set the
1534 * check_online_event to bring primary domain online.
1536 if (child == idmap_child()) {
1537 set_domain_online_request(primary_domain);
1540 /* We might be in the idmap child...*/
1541 if (child->domain && !(child->domain->internal) &&
1542 lp_winbind_offline_logon()) {
1544 set_domain_online_request(child->domain);
1546 if (primary_domain && (primary_domain != child->domain)) {
1547 /* We need to talk to the primary
1548 * domain as well as the trusted
1549 * domain inside a trusted domain
1552 * set_dc_type_and_flags_trustinfo()
1555 set_domain_online_request(primary_domain);
1558 child->lockout_policy_event = tevent_add_timer(
1559 winbind_event_context(), NULL, timeval_zero(),
1560 account_lockout_policy_handler,
1564 if (child->domain && child->domain->primary &&
1565 !USE_KERBEROS_KEYTAB &&
1566 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1568 struct timeval next_change;
1570 if (calculate_next_machine_pwd_change(child->domain->name,
1572 child->machine_password_change_event = tevent_add_timer(
1573 winbind_event_context(), NULL, next_change,
1574 machine_password_change_handler,
1579 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1580 TEVENT_FD_READ, child_handler, &state);
1582 DEBUG(1, ("tevent_add_fd failed\n"));
1589 TALLOC_CTX *frame = talloc_stackframe();
1591 ret = tevent_loop_once(winbind_event_context());
1593 DEBUG(1, ("tevent_loop_once failed: %s\n",
1598 if (child->domain && child->domain->startup &&
1599 (time_mono(NULL) > child->domain->startup_time + 30)) {
1600 /* No longer in "startup" mode. */
1601 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1602 child->domain->name ));
1603 child->domain->startup = False;
1610 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1613 struct server_id server_id,
1616 struct winbindd_child *child;
1618 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1622 for (child = winbindd_children; child != NULL; child = child->next) {
1623 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1624 msg_type, data->data, data->length);