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/sys_rw.h"
44 #define DBGC_CLASS DBGC_WINBIND
46 extern bool override_logfile;
48 static struct winbindd_child *winbindd_children = NULL;
50 /* Read some data from a client connection */
52 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
56 status = read_data_ntstatus(sock, (char *)wreq, sizeof(*wreq));
57 if (!NT_STATUS_IS_OK(status)) {
58 DEBUG(3, ("child_read_request: read_data failed: %s\n",
63 if (wreq->extra_len == 0) {
64 wreq->extra_data.data = NULL;
68 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
70 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
71 if (wreq->extra_data.data == NULL) {
72 DEBUG(0, ("malloc failed\n"));
73 return NT_STATUS_NO_MEMORY;
76 /* Ensure null termination */
77 wreq->extra_data.data[wreq->extra_len] = '\0';
79 status = read_data_ntstatus(sock, wreq->extra_data.data,
81 if (!NT_STATUS_IS_OK(status)) {
82 DEBUG(0, ("Could not read extra data: %s\n",
88 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
93 iov[0].iov_base = (void *)wrsp;
94 iov[0].iov_len = sizeof(struct winbindd_response);
97 if (wrsp->length > sizeof(struct winbindd_response)) {
98 iov[1].iov_base = (void *)wrsp->extra_data.data;
99 iov[1].iov_len = wrsp->length-iov[0].iov_len;
103 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
105 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
106 DEBUG(0, ("Could not write result\n"));
107 return NT_STATUS_INVALID_HANDLE;
114 * Do winbind child async request. This is not simply wb_simple_trans. We have
115 * to do the queueing ourselves because while a request is queued, the child
116 * might have crashed, and we have to re-fork it in the _trigger function.
119 struct wb_child_request_state {
120 struct tevent_context *ev;
121 struct winbindd_child *child;
122 struct winbindd_request *request;
123 struct winbindd_response *response;
126 static bool fork_domain_child(struct winbindd_child *child);
128 static void wb_child_request_trigger(struct tevent_req *req,
130 static void wb_child_request_done(struct tevent_req *subreq);
132 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
133 struct tevent_context *ev,
134 struct winbindd_child *child,
135 struct winbindd_request *request)
137 struct tevent_req *req;
138 struct wb_child_request_state *state;
140 req = tevent_req_create(mem_ctx, &state,
141 struct wb_child_request_state);
147 state->child = child;
148 state->request = request;
150 if (!tevent_queue_add(child->queue, ev, req,
151 wb_child_request_trigger, NULL)) {
153 return tevent_req_post(req, ev);
158 static void wb_child_request_trigger(struct tevent_req *req,
161 struct wb_child_request_state *state = tevent_req_data(
162 req, struct wb_child_request_state);
163 struct tevent_req *subreq;
165 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
166 tevent_req_error(req, errno);
170 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
171 state->child->sock, state->request);
172 if (tevent_req_nomem(subreq, req)) {
175 tevent_req_set_callback(subreq, wb_child_request_done, req);
176 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
179 static void wb_child_request_done(struct tevent_req *subreq)
181 struct tevent_req *req = tevent_req_callback_data(
182 subreq, struct tevent_req);
183 struct wb_child_request_state *state = tevent_req_data(
184 req, struct wb_child_request_state);
187 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
191 * The basic parent/child communication broke, close
194 close(state->child->sock);
195 state->child->sock = -1;
196 DLIST_REMOVE(winbindd_children, state->child);
197 tevent_req_error(req, err);
200 tevent_req_done(req);
203 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
204 struct winbindd_response **presponse, int *err)
206 struct wb_child_request_state *state = tevent_req_data(
207 req, struct wb_child_request_state);
209 if (tevent_req_is_unix_error(req, err)) {
212 *presponse = talloc_move(mem_ctx, &state->response);
216 static bool winbindd_child_busy(struct winbindd_child *child)
218 return tevent_queue_length(child->queue) > 0;
221 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
225 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
226 if (!winbindd_child_busy(&domain->children[i])) {
227 return &domain->children[i];
234 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
236 struct winbindd_child *result;
238 result = find_idle_child(domain);
239 if (result != NULL) {
242 return &domain->children[rand() % lp_winbind_max_domain_connections()];
245 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
247 struct winbindd_child *child;
249 child = choose_domain_child(domain);
250 return child->binding_handle;
253 struct wb_domain_request_state {
254 struct tevent_context *ev;
255 struct winbindd_domain *domain;
256 struct winbindd_child *child;
257 struct winbindd_request *request;
258 struct winbindd_request *init_req;
259 struct winbindd_response *response;
262 static void wb_domain_request_gotdc(struct tevent_req *subreq);
263 static void wb_domain_request_initialized(struct tevent_req *subreq);
264 static void wb_domain_request_done(struct tevent_req *subreq);
266 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
267 struct tevent_context *ev,
268 struct winbindd_domain *domain,
269 struct winbindd_request *request)
271 struct tevent_req *req, *subreq;
272 struct wb_domain_request_state *state;
274 req = tevent_req_create(mem_ctx, &state,
275 struct wb_domain_request_state);
280 state->child = choose_domain_child(domain);
282 if (domain->initialized) {
283 subreq = wb_child_request_send(state, ev, state->child,
285 if (tevent_req_nomem(subreq, req)) {
286 return tevent_req_post(req, ev);
288 tevent_req_set_callback(subreq, wb_domain_request_done, req);
292 state->domain = domain;
294 state->request = request;
296 state->init_req = talloc_zero(state, struct winbindd_request);
297 if (tevent_req_nomem(state->init_req, req)) {
298 return tevent_req_post(req, ev);
301 if (IS_DC || domain->primary || domain->internal) {
302 /* The primary domain has to find the DC name itself */
303 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
304 fstrcpy(state->init_req->domain_name, domain->name);
305 state->init_req->data.init_conn.is_primary = domain->primary;
306 fstrcpy(state->init_req->data.init_conn.dcname, "");
308 subreq = wb_child_request_send(state, ev, state->child,
310 if (tevent_req_nomem(subreq, req)) {
311 return tevent_req_post(req, ev);
313 tevent_req_set_callback(subreq, wb_domain_request_initialized,
319 * Ask our DC for a DC name
321 domain = find_our_domain();
323 /* This is *not* the primary domain, let's ask our DC about a DC
326 state->init_req->cmd = WINBINDD_GETDCNAME;
327 fstrcpy(state->init_req->domain_name, domain->name);
329 subreq = wb_child_request_send(state, ev, state->child, request);
330 if (tevent_req_nomem(subreq, req)) {
331 return tevent_req_post(req, ev);
333 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
337 static void wb_domain_request_gotdc(struct tevent_req *subreq)
339 struct tevent_req *req = tevent_req_callback_data(
340 subreq, struct tevent_req);
341 struct wb_domain_request_state *state = tevent_req_data(
342 req, struct wb_domain_request_state);
343 struct winbindd_response *response;
346 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
349 tevent_req_error(req, err);
352 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
353 fstrcpy(state->init_req->domain_name, state->domain->name);
354 state->init_req->data.init_conn.is_primary = False;
355 fstrcpy(state->init_req->data.init_conn.dcname,
356 response->data.dc_name);
358 TALLOC_FREE(response);
360 subreq = wb_child_request_send(state, state->ev, state->child,
362 if (tevent_req_nomem(subreq, req)) {
365 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
368 static void wb_domain_request_initialized(struct tevent_req *subreq)
370 struct tevent_req *req = tevent_req_callback_data(
371 subreq, struct tevent_req);
372 struct wb_domain_request_state *state = tevent_req_data(
373 req, struct wb_domain_request_state);
374 struct winbindd_response *response;
377 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
380 tevent_req_error(req, err);
384 if (!string_to_sid(&state->domain->sid,
385 response->data.domain_info.sid)) {
386 DEBUG(1,("init_child_recv: Could not convert sid %s "
387 "from string\n", response->data.domain_info.sid));
388 tevent_req_error(req, EINVAL);
392 talloc_free(state->domain->name);
393 state->domain->name = talloc_strdup(state->domain,
394 response->data.domain_info.name);
395 if (state->domain->name == NULL) {
396 tevent_req_error(req, ENOMEM);
400 if (response->data.domain_info.alt_name[0] != '\0') {
401 talloc_free(state->domain->alt_name);
403 state->domain->alt_name = talloc_strdup(state->domain,
404 response->data.domain_info.alt_name);
405 if (state->domain->alt_name == NULL) {
406 tevent_req_error(req, ENOMEM);
411 state->domain->native_mode = response->data.domain_info.native_mode;
412 state->domain->active_directory =
413 response->data.domain_info.active_directory;
414 state->domain->initialized = true;
416 TALLOC_FREE(response);
418 subreq = wb_child_request_send(state, state->ev, state->child,
420 if (tevent_req_nomem(subreq, req)) {
423 tevent_req_set_callback(subreq, wb_domain_request_done, req);
426 static void wb_domain_request_done(struct tevent_req *subreq)
428 struct tevent_req *req = tevent_req_callback_data(
429 subreq, struct tevent_req);
430 struct wb_domain_request_state *state = tevent_req_data(
431 req, struct wb_domain_request_state);
434 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
438 tevent_req_error(req, err);
441 tevent_req_done(req);
444 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
445 struct winbindd_response **presponse, int *err)
447 struct wb_domain_request_state *state = tevent_req_data(
448 req, struct wb_domain_request_state);
450 if (tevent_req_is_unix_error(req, err)) {
453 *presponse = talloc_move(mem_ctx, &state->response);
457 static void child_process_request(struct winbindd_child *child,
458 struct winbindd_cli_state *state)
460 struct winbindd_domain *domain = child->domain;
461 const struct winbindd_child_dispatch_table *table = child->table;
463 /* Free response data - we may be interrupted and receive another
464 command before being able to send this data off. */
466 state->response->result = WINBINDD_ERROR;
467 state->response->length = sizeof(struct winbindd_response);
469 /* as all requests in the child are sync, we can use talloc_tos() */
470 state->mem_ctx = talloc_tos();
472 /* Process command */
474 for (; table->name; table++) {
475 if (state->request->cmd == table->struct_cmd) {
476 DEBUG(10,("child_process_request: request fn %s\n",
478 state->response->result = table->struct_fn(domain, state);
483 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
484 (int)state->request->cmd));
485 state->response->result = WINBINDD_ERROR;
488 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
489 const struct winbindd_child_dispatch_table *table,
490 const char *logprefix,
493 if (logprefix && logname) {
494 char *logbase = NULL;
496 if (*lp_logfile(talloc_tos())) {
499 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
500 smb_panic("Internal error: asprintf failed");
503 if ((end = strrchr_m(logbase, '/'))) {
507 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
508 smb_panic("Internal error: asprintf failed");
512 if (asprintf(&child->logfilename, "%s/%s-%s",
513 logbase, logprefix, logname) < 0) {
515 smb_panic("Internal error: asprintf failed");
520 smb_panic("Internal error: logprefix == NULL && "
525 child->domain = domain;
526 child->table = table;
527 child->queue = tevent_queue_create(NULL, "winbind_child");
528 SMB_ASSERT(child->queue != NULL);
529 child->binding_handle = wbint_binding_handle(NULL, domain, child);
530 SMB_ASSERT(child->binding_handle != NULL);
533 void winbind_child_died(pid_t pid)
535 struct winbindd_child *child;
537 for (child = winbindd_children; child != NULL; child = child->next) {
538 if (child->pid == pid) {
544 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
548 /* This will be re-added in fork_domain_child() */
550 DLIST_REMOVE(winbindd_children, child);
553 if (child->sock != -1) {
559 /* Ensure any negative cache entries with the netbios or realm names are removed. */
561 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
563 flush_negative_conn_cache_for_domain(domain->name);
564 if (domain->alt_name != NULL) {
565 flush_negative_conn_cache_for_domain(domain->alt_name);
570 * Parent winbindd process sets its own debug level first and then
571 * sends a message to all the winbindd children to adjust their debug
572 * level to that of parents.
575 void winbind_msg_debug(struct messaging_context *msg_ctx,
578 struct server_id server_id,
581 struct winbindd_child *child;
583 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
585 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
587 for (child = winbindd_children; child != NULL; child = child->next) {
589 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
590 (unsigned int)child->pid));
592 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
595 strlen((char *) data->data) + 1);
599 /* Set our domains as offline and forward the offline message to our children. */
601 void winbind_msg_offline(struct messaging_context *msg_ctx,
604 struct server_id server_id,
607 struct winbindd_child *child;
608 struct winbindd_domain *domain;
610 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
612 if (!lp_winbind_offline_logon()) {
613 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
617 /* Set our global state as offline. */
618 if (!set_global_winbindd_state_offline()) {
619 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
623 /* Set all our domains as offline. */
624 for (domain = domain_list(); domain; domain = domain->next) {
625 if (domain->internal) {
628 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
629 set_domain_offline(domain);
632 for (child = winbindd_children; child != NULL; child = child->next) {
633 /* Don't send message to internal children. We've already
635 if (!child->domain || winbindd_internal_child(child)) {
639 /* Or internal domains (this should not be possible....) */
640 if (child->domain->internal) {
644 /* Each winbindd child should only process requests for one domain - make sure
645 we only set it online / offline for that domain. */
647 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
648 (unsigned int)child->pid, child->domain->name ));
650 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
652 (const uint8_t *)child->domain->name,
653 strlen(child->domain->name)+1);
657 /* Set our domains as online and forward the online message to our children. */
659 void winbind_msg_online(struct messaging_context *msg_ctx,
662 struct server_id server_id,
665 struct winbindd_child *child;
666 struct winbindd_domain *domain;
668 DEBUG(10,("winbind_msg_online: got online message.\n"));
670 if (!lp_winbind_offline_logon()) {
671 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
675 /* Set our global state as online. */
676 set_global_winbindd_state_online();
678 smb_nscd_flush_user_cache();
679 smb_nscd_flush_group_cache();
681 /* Set all our domains as online. */
682 for (domain = domain_list(); domain; domain = domain->next) {
683 if (domain->internal) {
686 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
688 winbindd_flush_negative_conn_cache(domain);
689 set_domain_online_request(domain);
691 /* Send an online message to the idmap child when our
692 primary domain comes back online */
694 if ( domain->primary ) {
695 struct winbindd_child *idmap = idmap_child();
697 if ( idmap->pid != 0 ) {
698 messaging_send_buf(msg_ctx,
699 pid_to_procid(idmap->pid),
701 (const uint8_t *)domain->name,
702 strlen(domain->name)+1);
707 for (child = winbindd_children; child != NULL; child = child->next) {
708 /* Don't send message to internal childs. */
709 if (!child->domain || winbindd_internal_child(child)) {
713 /* Or internal domains (this should not be possible....) */
714 if (child->domain->internal) {
718 /* Each winbindd child should only process requests for one domain - make sure
719 we only set it online / offline for that domain. */
721 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
722 (unsigned int)child->pid, child->domain->name ));
724 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
726 (const uint8_t *)child->domain->name,
727 strlen(child->domain->name)+1);
731 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
733 struct winbindd_domain *domain;
736 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
737 get_global_winbindd_state_offline() ?
738 "Offline":"Online")) == NULL) {
742 for (domain = domain_list(); domain; domain = domain->next) {
743 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
746 "Online":"Offline")) == NULL) {
751 buf = talloc_asprintf_append_buffer(buf, "\n");
753 DEBUG(5,("collect_onlinestatus: %s", buf));
758 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
761 struct server_id server_id,
766 struct server_id *sender;
768 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
774 sender = (struct server_id *)data->data;
776 mem_ctx = talloc_init("winbind_msg_onlinestatus");
777 if (mem_ctx == NULL) {
781 message = collect_onlinestatus(mem_ctx);
782 if (message == NULL) {
783 talloc_destroy(mem_ctx);
787 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
788 (const uint8 *)message, strlen(message) + 1);
790 talloc_destroy(mem_ctx);
793 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
796 struct server_id server_id,
799 struct winbindd_child *child;
801 DEBUG(10,("winbind_msg_dump_event_list received\n"));
803 dump_event_list(winbind_event_context());
805 for (child = winbindd_children; child != NULL; child = child->next) {
807 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
808 (unsigned int)child->pid));
810 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
817 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
820 struct server_id server_id,
824 const char *message = NULL;
825 struct server_id *sender = NULL;
826 const char *domain = NULL;
829 struct winbindd_domain *dom = NULL;
831 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
833 if (!data || !data->data) {
837 if (data->length < sizeof(struct server_id)) {
841 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
846 sender = (struct server_id *)data->data;
847 if (data->length > sizeof(struct server_id)) {
848 domain = (const char *)data->data+sizeof(struct server_id);
853 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
856 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
857 find_domain_from_name_noinit(domain));
859 talloc_destroy(mem_ctx);
863 messaging_send_buf(msg_ctx, *sender,
864 MSG_WINBIND_DUMP_DOMAIN_LIST,
865 (const uint8_t *)message, strlen(message) + 1);
867 talloc_destroy(mem_ctx);
872 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
874 for (dom = domain_list(); dom; dom=dom->next) {
875 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
877 talloc_destroy(mem_ctx);
881 s = talloc_asprintf_append(s, "%s\n", message);
883 talloc_destroy(mem_ctx);
888 status = messaging_send_buf(msg_ctx, *sender,
889 MSG_WINBIND_DUMP_DOMAIN_LIST,
890 (uint8_t *)s, strlen(s) + 1);
891 if (!NT_STATUS_IS_OK(status)) {
892 DEBUG(0,("failed to send message: %s\n",
896 talloc_destroy(mem_ctx);
899 static void account_lockout_policy_handler(struct tevent_context *ctx,
900 struct tevent_timer *te,
904 struct winbindd_child *child =
905 (struct winbindd_child *)private_data;
906 TALLOC_CTX *mem_ctx = NULL;
907 struct winbindd_methods *methods;
908 struct samr_DomInfo12 lockout_policy;
911 DEBUG(10,("account_lockout_policy_handler called\n"));
913 TALLOC_FREE(child->lockout_policy_event);
915 if ( !winbindd_can_contact_domain( child->domain ) ) {
916 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
917 "do not have an incoming trust to domain %s\n",
918 child->domain->name));
923 methods = child->domain->methods;
925 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
927 result = NT_STATUS_NO_MEMORY;
929 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
931 TALLOC_FREE(mem_ctx);
933 if (!NT_STATUS_IS_OK(result)) {
934 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
938 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
939 timeval_current_ofs(3600, 0),
940 account_lockout_policy_handler,
944 static time_t get_machine_password_timeout(void)
946 /* until we have gpo support use lp setting */
947 return lp_machine_password_timeout();
950 static bool calculate_next_machine_pwd_change(const char *domain,
953 time_t pass_last_set_time;
959 pw = secrets_fetch_machine_password(domain,
964 DEBUG(0,("cannot fetch own machine password ????"));
970 timeout = get_machine_password_timeout();
972 DEBUG(10,("machine password never expires\n"));
976 tv.tv_sec = pass_last_set_time;
977 DEBUG(10, ("password last changed %s\n",
978 timeval_string(talloc_tos(), &tv, false)));
979 tv.tv_sec += timeout;
980 DEBUGADD(10, ("password valid until %s\n",
981 timeval_string(talloc_tos(), &tv, false)));
983 if (time(NULL) < (pass_last_set_time + timeout)) {
984 next_change = pass_last_set_time + timeout;
985 DEBUG(10,("machine password still valid until: %s\n",
986 http_timestring(talloc_tos(), next_change)));
987 *t = timeval_set(next_change, 0);
989 if (lp_clustering()) {
992 * When having a cluster, we have several
993 * winbinds racing for the password change. In
994 * the machine_password_change_handler()
995 * function we check if someone else was
996 * faster when the event triggers. We add a
997 * 255-second random delay here, so that we
998 * don't run to change the password at the
1001 generate_random_buffer(&randbuf, sizeof(randbuf));
1002 DEBUG(10, ("adding %d seconds randomness\n",
1004 t->tv_sec += randbuf;
1009 DEBUG(10,("machine password expired, needs immediate change\n"));
1011 *t = timeval_zero();
1016 static void machine_password_change_handler(struct tevent_context *ctx,
1017 struct tevent_timer *te,
1021 struct messaging_context *msg_ctx = winbind_messaging_context();
1022 struct winbindd_child *child =
1023 (struct winbindd_child *)private_data;
1024 struct rpc_pipe_client *netlogon_pipe = NULL;
1026 struct timeval next_change;
1028 DEBUG(10,("machine_password_change_handler called\n"));
1030 TALLOC_FREE(child->machine_password_change_event);
1032 if (!calculate_next_machine_pwd_change(child->domain->name,
1034 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1038 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1039 timeval_string(talloc_tos(), &next_change, false)));
1041 if (!timeval_expired(&next_change)) {
1042 DEBUG(10, ("Someone else has already changed the pw\n"));
1046 if (!winbindd_can_contact_domain(child->domain)) {
1047 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1048 "do not have an incoming trust to domain %s\n",
1049 child->domain->name));
1053 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1054 if (!NT_STATUS_IS_OK(result)) {
1055 DEBUG(10,("machine_password_change_handler: "
1056 "failed to connect netlogon pipe: %s\n",
1057 nt_errstr(result)));
1061 result = trust_pw_change(child->domain->conn.netlogon_creds,
1063 netlogon_pipe->binding_handle,
1064 child->domain->name,
1067 DEBUG(10, ("machine_password_change_handler: "
1068 "trust_pw_change returned %s\n",
1069 nt_errstr(result)));
1071 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1072 DEBUG(3,("machine_password_change_handler: password set returned "
1073 "ACCESS_DENIED. Maybe the trust account "
1074 "password was changed and we didn't know it. "
1075 "Killing connections to domain %s\n",
1076 child->domain->name));
1077 invalidate_cm_connection(child->domain);
1080 if (!calculate_next_machine_pwd_change(child->domain->name,
1082 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1086 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1087 timeval_string(talloc_tos(), &next_change, false)));
1089 if (!NT_STATUS_IS_OK(result)) {
1092 * In case of failure, give the DC a minute to recover
1094 tmp = timeval_current_ofs(60, 0);
1095 next_change = timeval_max(&next_change, &tmp);
1099 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1101 machine_password_change_handler,
1105 /* Deal with a request to go offline. */
1107 static void child_msg_offline(struct messaging_context *msg,
1110 struct server_id server_id,
1113 struct winbindd_domain *domain;
1114 struct winbindd_domain *primary_domain = NULL;
1115 const char *domainname = (const char *)data->data;
1117 if (data->data == NULL || data->length == 0) {
1121 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1123 if (!lp_winbind_offline_logon()) {
1124 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1128 primary_domain = find_our_domain();
1130 /* Mark the requested domain offline. */
1132 for (domain = domain_list(); domain; domain = domain->next) {
1133 if (domain->internal) {
1136 if (strequal(domain->name, domainname)) {
1137 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1138 set_domain_offline(domain);
1139 /* we are in the trusted domain, set the primary domain
1141 if (domain != primary_domain) {
1142 set_domain_offline(primary_domain);
1148 /* Deal with a request to go online. */
1150 static void child_msg_online(struct messaging_context *msg,
1153 struct server_id server_id,
1156 struct winbindd_domain *domain;
1157 struct winbindd_domain *primary_domain = NULL;
1158 const char *domainname = (const char *)data->data;
1160 if (data->data == NULL || data->length == 0) {
1164 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1166 if (!lp_winbind_offline_logon()) {
1167 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1171 primary_domain = find_our_domain();
1173 /* Set our global state as online. */
1174 set_global_winbindd_state_online();
1176 /* Try and mark everything online - delete any negative cache entries
1177 to force a reconnect now. */
1179 for (domain = domain_list(); domain; domain = domain->next) {
1180 if (domain->internal) {
1183 if (strequal(domain->name, domainname)) {
1184 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1185 winbindd_flush_negative_conn_cache(domain);
1186 set_domain_online_request(domain);
1188 /* we can be in trusted domain, which will contact primary domain
1189 * we have to bring primary domain online in trusted domain process
1190 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1191 * --> contact_domain = find_our_domain()
1193 if (domain != primary_domain) {
1194 winbindd_flush_negative_conn_cache(primary_domain);
1195 set_domain_online_request(primary_domain);
1201 static void child_msg_dump_event_list(struct messaging_context *msg,
1204 struct server_id server_id,
1207 DEBUG(5,("child_msg_dump_event_list received\n"));
1209 dump_event_list(winbind_event_context());
1212 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1213 const char *logfilename)
1215 struct winbindd_domain *domain;
1216 struct winbindd_child *cl;
1219 status = reinit_after_fork(
1220 winbind_messaging_context(),
1221 winbind_event_context(),
1223 if (!NT_STATUS_IS_OK(status)) {
1224 DEBUG(0,("reinit_after_fork() failed\n"));
1228 close_conns_after_fork();
1230 if (!override_logfile && logfilename) {
1231 lp_set_logfile(logfilename);
1235 if (!winbindd_setup_sig_term_handler(false))
1236 return NT_STATUS_NO_MEMORY;
1237 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1239 return NT_STATUS_NO_MEMORY;
1241 /* Stop zombies in children */
1244 /* Don't handle the same messages as our parent. */
1245 messaging_deregister(winbind_messaging_context(),
1246 MSG_SMB_CONF_UPDATED, NULL);
1247 messaging_deregister(winbind_messaging_context(),
1248 MSG_SHUTDOWN, NULL);
1249 messaging_deregister(winbind_messaging_context(),
1250 MSG_WINBIND_OFFLINE, NULL);
1251 messaging_deregister(winbind_messaging_context(),
1252 MSG_WINBIND_ONLINE, NULL);
1253 messaging_deregister(winbind_messaging_context(),
1254 MSG_WINBIND_ONLINESTATUS, NULL);
1255 messaging_deregister(winbind_messaging_context(),
1256 MSG_DUMP_EVENT_LIST, NULL);
1257 messaging_deregister(winbind_messaging_context(),
1258 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1259 messaging_deregister(winbind_messaging_context(),
1262 messaging_deregister(winbind_messaging_context(),
1263 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1264 messaging_deregister(winbind_messaging_context(),
1265 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1267 /* We have destroyed all events in the winbindd_event_context
1268 * in reinit_after_fork(), so clean out all possible pending
1269 * event pointers. */
1271 /* Deal with check_online_events. */
1273 for (domain = domain_list(); domain; domain = domain->next) {
1274 TALLOC_FREE(domain->check_online_event);
1277 /* Ensure we're not handling a credential cache event inherited
1278 * from our parent. */
1280 ccache_remove_all_after_fork();
1282 /* Destroy all possible events in child list. */
1283 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1284 TALLOC_FREE(cl->lockout_policy_event);
1285 TALLOC_FREE(cl->machine_password_change_event);
1287 /* Children should never be able to send
1288 * each other messages, all messages must
1289 * go through the parent.
1294 * Close service sockets to all other children
1296 if ((cl != myself) && (cl->sock != -1)) {
1302 * This is a little tricky, children must not
1303 * send an MSG_WINBIND_ONLINE message to idmap_child().
1304 * If we are in a child of our primary domain or
1305 * in the process created by fork_child_dc_connect(),
1306 * and the primary domain cannot go online,
1307 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1308 * periodically to idmap_child().
1310 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1311 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1312 * ---> init_dc_connection() ---> cm_open_connection --->
1313 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1314 * idmap_child(). Disallow children sending messages
1315 * to each other, all messages must go through the parent.
1320 return NT_STATUS_OK;
1324 * In a child there will be only one domain, reference that here.
1326 static struct winbindd_domain *child_domain;
1328 struct winbindd_domain *wb_child_domain(void)
1330 return child_domain;
1333 struct child_handler_state {
1334 struct winbindd_child *child;
1335 struct winbindd_cli_state cli;
1338 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1339 uint16_t flags, void *private_data)
1341 struct child_handler_state *state =
1342 (struct child_handler_state *)private_data;
1345 /* fetch a request from the main daemon */
1346 status = child_read_request(state->cli.sock, state->cli.request);
1348 if (!NT_STATUS_IS_OK(status)) {
1349 /* we lost contact with our parent */
1353 DEBUG(4,("child daemon request %d\n",
1354 (int)state->cli.request->cmd));
1356 ZERO_STRUCTP(state->cli.response);
1357 state->cli.request->null_term = '\0';
1358 state->cli.mem_ctx = talloc_tos();
1359 child_process_request(state->child, &state->cli);
1361 DEBUG(4, ("Finished processing child request %d\n",
1362 (int)state->cli.request->cmd));
1364 SAFE_FREE(state->cli.request->extra_data.data);
1366 status = child_write_response(state->cli.sock, state->cli.response);
1367 if (!NT_STATUS_IS_OK(status)) {
1372 static bool fork_domain_child(struct winbindd_child *child)
1375 struct child_handler_state state;
1376 struct winbindd_request request;
1377 struct winbindd_response response;
1378 struct winbindd_domain *primary_domain = NULL;
1381 struct tevent_fd *fde;
1383 if (child->domain) {
1384 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1385 child->domain->name));
1387 DEBUG(10, ("fork_domain_child called without domain.\n"));
1390 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1391 DEBUG(0, ("Could not open child pipe: %s\n",
1397 state.child = child;
1398 state.cli.pid = getpid();
1399 state.cli.request = &request;
1400 state.cli.response = &response;
1402 child->pid = fork();
1404 if (child->pid == -1) {
1405 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1411 if (child->pid != 0) {
1417 nread = sys_read(fdpair[1], &status, sizeof(status));
1418 if (nread != sizeof(status)) {
1419 DEBUG(1, ("fork_domain_child: Could not read child status: "
1420 "nread=%d, error=%s\n", (int)nread,
1425 if (!NT_STATUS_IS_OK(status)) {
1426 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1427 nt_errstr(status)));
1432 child->next = child->prev = NULL;
1433 DLIST_ADD(winbindd_children, child);
1434 child->sock = fdpair[1];
1439 child_domain = child->domain;
1441 DEBUG(10, ("Child process %d\n", (int)getpid()));
1443 state.cli.sock = fdpair[0];
1446 status = winbindd_reinit_after_fork(child, child->logfilename);
1448 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1449 if (nwritten != sizeof(status)) {
1450 DEBUG(1, ("fork_domain_child: Could not write status: "
1451 "nwritten=%d, error=%s\n", (int)nwritten,
1455 if (!NT_STATUS_IS_OK(status)) {
1456 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1457 nt_errstr(status)));
1461 /* Handle online/offline messages. */
1462 messaging_register(winbind_messaging_context(), NULL,
1463 MSG_WINBIND_OFFLINE, child_msg_offline);
1464 messaging_register(winbind_messaging_context(), NULL,
1465 MSG_WINBIND_ONLINE, child_msg_online);
1466 messaging_register(winbind_messaging_context(), NULL,
1467 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1468 messaging_register(winbind_messaging_context(), NULL,
1469 MSG_DEBUG, debug_message);
1470 messaging_register(winbind_messaging_context(), NULL,
1471 MSG_WINBIND_IP_DROPPED,
1472 winbind_msg_ip_dropped);
1475 primary_domain = find_our_domain();
1477 if (primary_domain == NULL) {
1478 smb_panic("no primary domain found");
1481 /* It doesn't matter if we allow cache login,
1482 * try to bring domain online after fork. */
1483 if ( child->domain ) {
1484 child->domain->startup = True;
1485 child->domain->startup_time = time_mono(NULL);
1486 /* we can be in primary domain or in trusted domain
1487 * If we are in trusted domain, set the primary domain
1488 * in start-up mode */
1489 if (!(child->domain->internal)) {
1490 set_domain_online_request(child->domain);
1491 if (!(child->domain->primary)) {
1492 primary_domain->startup = True;
1493 primary_domain->startup_time = time_mono(NULL);
1494 set_domain_online_request(primary_domain);
1500 * We are in idmap child, make sure that we set the
1501 * check_online_event to bring primary domain online.
1503 if (child == idmap_child()) {
1504 set_domain_online_request(primary_domain);
1507 /* We might be in the idmap child...*/
1508 if (child->domain && !(child->domain->internal) &&
1509 lp_winbind_offline_logon()) {
1511 set_domain_online_request(child->domain);
1513 if (primary_domain && (primary_domain != child->domain)) {
1514 /* We need to talk to the primary
1515 * domain as well as the trusted
1516 * domain inside a trusted domain
1519 * set_dc_type_and_flags_trustinfo()
1522 set_domain_online_request(primary_domain);
1525 child->lockout_policy_event = tevent_add_timer(
1526 winbind_event_context(), NULL, timeval_zero(),
1527 account_lockout_policy_handler,
1531 if (child->domain && child->domain->primary &&
1532 !USE_KERBEROS_KEYTAB &&
1533 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1535 struct timeval next_change;
1537 if (calculate_next_machine_pwd_change(child->domain->name,
1539 child->machine_password_change_event = tevent_add_timer(
1540 winbind_event_context(), NULL, next_change,
1541 machine_password_change_handler,
1546 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1547 TEVENT_FD_READ, child_handler, &state);
1549 DEBUG(1, ("tevent_add_fd failed\n"));
1556 TALLOC_CTX *frame = talloc_stackframe();
1558 ret = tevent_loop_once(winbind_event_context());
1560 DEBUG(1, ("tevent_loop_once failed: %s\n",
1565 if (child->domain && child->domain->startup &&
1566 (time_mono(NULL) > child->domain->startup_time + 30)) {
1567 /* No longer in "startup" mode. */
1568 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1569 child->domain->name ));
1570 child->domain->startup = False;
1577 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1580 struct server_id server_id,
1583 struct winbindd_child *child;
1585 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1589 for (child = winbindd_children; child != NULL; child = child->next) {
1590 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1591 msg_type, data->data, data->length);