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, server_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 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
253 struct winbindd_child *shortest = &domain->children[0];
254 struct winbindd_child *current;
257 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
258 size_t shortest_len, current_len;
260 current = &domain->children[i];
261 current_len = tevent_queue_length(current->queue);
263 if (current_len == 0) {
268 shortest_len = tevent_queue_length(shortest->queue);
270 if (current_len < shortest_len) {
278 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
280 struct winbindd_child *child;
282 child = choose_domain_child(domain);
283 return child->binding_handle;
286 struct wb_domain_request_state {
287 struct tevent_context *ev;
288 struct winbindd_domain *domain;
289 struct winbindd_child *child;
290 struct winbindd_request *request;
291 struct winbindd_request *init_req;
292 struct winbindd_response *response;
295 static void wb_domain_request_gotdc(struct tevent_req *subreq);
296 static void wb_domain_request_initialized(struct tevent_req *subreq);
297 static void wb_domain_request_done(struct tevent_req *subreq);
299 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
300 struct tevent_context *ev,
301 struct winbindd_domain *domain,
302 struct winbindd_request *request)
304 struct tevent_req *req, *subreq;
305 struct wb_domain_request_state *state;
307 req = tevent_req_create(mem_ctx, &state,
308 struct wb_domain_request_state);
313 state->child = choose_domain_child(domain);
315 if (domain->initialized) {
316 subreq = wb_child_request_send(state, ev, state->child,
318 if (tevent_req_nomem(subreq, req)) {
319 return tevent_req_post(req, ev);
321 tevent_req_set_callback(subreq, wb_domain_request_done, req);
325 state->domain = domain;
327 state->request = request;
329 state->init_req = talloc_zero(state, struct winbindd_request);
330 if (tevent_req_nomem(state->init_req, req)) {
331 return tevent_req_post(req, ev);
334 if (IS_DC || domain->primary || domain->internal) {
335 /* The primary domain has to find the DC name itself */
336 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
337 fstrcpy(state->init_req->domain_name, domain->name);
338 state->init_req->data.init_conn.is_primary = domain->primary;
339 fstrcpy(state->init_req->data.init_conn.dcname, "");
341 subreq = wb_child_request_send(state, ev, state->child,
343 if (tevent_req_nomem(subreq, req)) {
344 return tevent_req_post(req, ev);
346 tevent_req_set_callback(subreq, wb_domain_request_initialized,
352 * Ask our DC for a DC name
354 domain = find_our_domain();
356 /* This is *not* the primary domain, let's ask our DC about a DC
359 state->init_req->cmd = WINBINDD_GETDCNAME;
360 fstrcpy(state->init_req->domain_name, domain->name);
362 subreq = wb_child_request_send(state, ev, state->child, request);
363 if (tevent_req_nomem(subreq, req)) {
364 return tevent_req_post(req, ev);
366 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
370 static void wb_domain_request_gotdc(struct tevent_req *subreq)
372 struct tevent_req *req = tevent_req_callback_data(
373 subreq, struct tevent_req);
374 struct wb_domain_request_state *state = tevent_req_data(
375 req, struct wb_domain_request_state);
376 struct winbindd_response *response;
379 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
382 tevent_req_error(req, err);
385 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
386 fstrcpy(state->init_req->domain_name, state->domain->name);
387 state->init_req->data.init_conn.is_primary = False;
388 fstrcpy(state->init_req->data.init_conn.dcname,
389 response->data.dc_name);
391 TALLOC_FREE(response);
393 subreq = wb_child_request_send(state, state->ev, state->child,
395 if (tevent_req_nomem(subreq, req)) {
398 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
401 static void wb_domain_request_initialized(struct tevent_req *subreq)
403 struct tevent_req *req = tevent_req_callback_data(
404 subreq, struct tevent_req);
405 struct wb_domain_request_state *state = tevent_req_data(
406 req, struct wb_domain_request_state);
407 struct winbindd_response *response;
410 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
413 tevent_req_error(req, err);
417 if (!string_to_sid(&state->domain->sid,
418 response->data.domain_info.sid)) {
419 DEBUG(1,("init_child_recv: Could not convert sid %s "
420 "from string\n", response->data.domain_info.sid));
421 tevent_req_error(req, EINVAL);
425 talloc_free(state->domain->name);
426 state->domain->name = talloc_strdup(state->domain,
427 response->data.domain_info.name);
428 if (state->domain->name == NULL) {
429 tevent_req_error(req, ENOMEM);
433 if (response->data.domain_info.alt_name[0] != '\0') {
434 talloc_free(state->domain->alt_name);
436 state->domain->alt_name = talloc_strdup(state->domain,
437 response->data.domain_info.alt_name);
438 if (state->domain->alt_name == NULL) {
439 tevent_req_error(req, ENOMEM);
444 state->domain->native_mode = response->data.domain_info.native_mode;
445 state->domain->active_directory =
446 response->data.domain_info.active_directory;
447 state->domain->initialized = true;
449 TALLOC_FREE(response);
451 subreq = wb_child_request_send(state, state->ev, state->child,
453 if (tevent_req_nomem(subreq, req)) {
456 tevent_req_set_callback(subreq, wb_domain_request_done, req);
459 static void wb_domain_request_done(struct tevent_req *subreq)
461 struct tevent_req *req = tevent_req_callback_data(
462 subreq, struct tevent_req);
463 struct wb_domain_request_state *state = tevent_req_data(
464 req, struct wb_domain_request_state);
467 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
471 tevent_req_error(req, err);
474 tevent_req_done(req);
477 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
478 struct winbindd_response **presponse, int *err)
480 struct wb_domain_request_state *state = tevent_req_data(
481 req, struct wb_domain_request_state);
483 if (tevent_req_is_unix_error(req, err)) {
486 *presponse = talloc_move(mem_ctx, &state->response);
490 static void child_process_request(struct winbindd_child *child,
491 struct winbindd_cli_state *state)
493 struct winbindd_domain *domain = child->domain;
494 const struct winbindd_child_dispatch_table *table = child->table;
496 /* Free response data - we may be interrupted and receive another
497 command before being able to send this data off. */
499 state->response->result = WINBINDD_ERROR;
500 state->response->length = sizeof(struct winbindd_response);
502 /* as all requests in the child are sync, we can use talloc_tos() */
503 state->mem_ctx = talloc_tos();
505 /* Process command */
507 for (; table->name; table++) {
508 if (state->request->cmd == table->struct_cmd) {
509 DEBUG(10,("child_process_request: request fn %s\n",
511 state->response->result = table->struct_fn(domain, state);
516 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
517 (int)state->request->cmd));
518 state->response->result = WINBINDD_ERROR;
521 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
522 const struct winbindd_child_dispatch_table *table,
523 const char *logprefix,
526 if (logprefix && logname) {
527 char *logbase = NULL;
529 if (*lp_logfile(talloc_tos())) {
532 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
533 smb_panic("Internal error: asprintf failed");
536 if ((end = strrchr_m(logbase, '/'))) {
540 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
541 smb_panic("Internal error: asprintf failed");
545 if (asprintf(&child->logfilename, "%s/%s-%s",
546 logbase, logprefix, logname) < 0) {
548 smb_panic("Internal error: asprintf failed");
553 smb_panic("Internal error: logprefix == NULL && "
558 child->domain = domain;
559 child->table = table;
560 child->queue = tevent_queue_create(NULL, "winbind_child");
561 SMB_ASSERT(child->queue != NULL);
562 child->binding_handle = wbint_binding_handle(NULL, domain, child);
563 SMB_ASSERT(child->binding_handle != NULL);
566 void winbind_child_died(pid_t pid)
568 struct winbindd_child *child;
570 for (child = winbindd_children; child != NULL; child = child->next) {
571 if (child->pid == pid) {
577 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
581 /* This will be re-added in fork_domain_child() */
583 DLIST_REMOVE(winbindd_children, child);
586 if (child->sock != -1) {
592 /* Ensure any negative cache entries with the netbios or realm names are removed. */
594 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
596 flush_negative_conn_cache_for_domain(domain->name);
597 if (domain->alt_name != NULL) {
598 flush_negative_conn_cache_for_domain(domain->alt_name);
603 * Parent winbindd process sets its own debug level first and then
604 * sends a message to all the winbindd children to adjust their debug
605 * level to that of parents.
608 void winbind_msg_debug(struct messaging_context *msg_ctx,
611 struct server_id server_id,
614 struct winbindd_child *child;
616 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
618 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
620 for (child = winbindd_children; child != NULL; child = child->next) {
622 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
623 (unsigned int)child->pid));
625 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
628 strlen((char *) data->data) + 1);
632 /* Set our domains as offline and forward the offline message to our children. */
634 void winbind_msg_offline(struct messaging_context *msg_ctx,
637 struct server_id server_id,
640 struct winbindd_child *child;
641 struct winbindd_domain *domain;
643 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
645 if (!lp_winbind_offline_logon()) {
646 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
650 /* Set our global state as offline. */
651 if (!set_global_winbindd_state_offline()) {
652 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
656 /* Set all our domains as offline. */
657 for (domain = domain_list(); domain; domain = domain->next) {
658 if (domain->internal) {
661 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
662 set_domain_offline(domain);
665 for (child = winbindd_children; child != NULL; child = child->next) {
666 /* Don't send message to internal children. We've already
668 if (!child->domain || winbindd_internal_child(child)) {
672 /* Or internal domains (this should not be possible....) */
673 if (child->domain->internal) {
677 /* Each winbindd child should only process requests for one domain - make sure
678 we only set it online / offline for that domain. */
680 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
681 (unsigned int)child->pid, child->domain->name ));
683 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
685 (const uint8_t *)child->domain->name,
686 strlen(child->domain->name)+1);
690 /* Set our domains as online and forward the online message to our children. */
692 void winbind_msg_online(struct messaging_context *msg_ctx,
695 struct server_id server_id,
698 struct winbindd_child *child;
699 struct winbindd_domain *domain;
701 DEBUG(10,("winbind_msg_online: got online message.\n"));
703 if (!lp_winbind_offline_logon()) {
704 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
708 /* Set our global state as online. */
709 set_global_winbindd_state_online();
711 smb_nscd_flush_user_cache();
712 smb_nscd_flush_group_cache();
714 /* Set all our domains as online. */
715 for (domain = domain_list(); domain; domain = domain->next) {
716 if (domain->internal) {
719 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
721 winbindd_flush_negative_conn_cache(domain);
722 set_domain_online_request(domain);
724 /* Send an online message to the idmap child when our
725 primary domain comes back online */
727 if ( domain->primary ) {
728 struct winbindd_child *idmap = idmap_child();
730 if ( idmap->pid != 0 ) {
731 messaging_send_buf(msg_ctx,
732 pid_to_procid(idmap->pid),
734 (const uint8_t *)domain->name,
735 strlen(domain->name)+1);
740 for (child = winbindd_children; child != NULL; child = child->next) {
741 /* Don't send message to internal childs. */
742 if (!child->domain || winbindd_internal_child(child)) {
746 /* Or internal domains (this should not be possible....) */
747 if (child->domain->internal) {
751 /* Each winbindd child should only process requests for one domain - make sure
752 we only set it online / offline for that domain. */
754 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
755 (unsigned int)child->pid, child->domain->name ));
757 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
759 (const uint8_t *)child->domain->name,
760 strlen(child->domain->name)+1);
764 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
766 struct winbindd_domain *domain;
769 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
770 get_global_winbindd_state_offline() ?
771 "Offline":"Online")) == NULL) {
775 for (domain = domain_list(); domain; domain = domain->next) {
776 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
779 "Online":"Offline")) == NULL) {
784 buf = talloc_asprintf_append_buffer(buf, "\n");
786 DEBUG(5,("collect_onlinestatus: %s", buf));
791 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
794 struct server_id server_id,
799 struct server_id *sender;
801 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
807 sender = (struct server_id *)data->data;
809 mem_ctx = talloc_init("winbind_msg_onlinestatus");
810 if (mem_ctx == NULL) {
814 message = collect_onlinestatus(mem_ctx);
815 if (message == NULL) {
816 talloc_destroy(mem_ctx);
820 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
821 (const uint8_t *)message, strlen(message) + 1);
823 talloc_destroy(mem_ctx);
826 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
829 struct server_id server_id,
832 struct winbindd_child *child;
834 DEBUG(10,("winbind_msg_dump_event_list received\n"));
836 DBG_WARNING("dump event list no longer implemented\n");
838 for (child = winbindd_children; child != NULL; child = child->next) {
840 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
841 (unsigned int)child->pid));
843 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
850 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
853 struct server_id server_id,
857 const char *message = NULL;
858 struct server_id *sender = NULL;
859 const char *domain = NULL;
862 struct winbindd_domain *dom = NULL;
864 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
866 if (!data || !data->data) {
870 if (data->length < sizeof(struct server_id)) {
874 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
879 sender = (struct server_id *)data->data;
880 if (data->length > sizeof(struct server_id)) {
881 domain = (const char *)data->data+sizeof(struct server_id);
886 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
889 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
890 find_domain_from_name_noinit(domain));
892 talloc_destroy(mem_ctx);
896 messaging_send_buf(msg_ctx, *sender,
897 MSG_WINBIND_DUMP_DOMAIN_LIST,
898 (const uint8_t *)message, strlen(message) + 1);
900 talloc_destroy(mem_ctx);
905 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
907 for (dom = domain_list(); dom; dom=dom->next) {
908 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
910 talloc_destroy(mem_ctx);
914 s = talloc_asprintf_append(s, "%s\n", message);
916 talloc_destroy(mem_ctx);
921 status = messaging_send_buf(msg_ctx, *sender,
922 MSG_WINBIND_DUMP_DOMAIN_LIST,
923 (uint8_t *)s, strlen(s) + 1);
924 if (!NT_STATUS_IS_OK(status)) {
925 DEBUG(0,("failed to send message: %s\n",
929 talloc_destroy(mem_ctx);
932 static void account_lockout_policy_handler(struct tevent_context *ctx,
933 struct tevent_timer *te,
937 struct winbindd_child *child =
938 (struct winbindd_child *)private_data;
939 TALLOC_CTX *mem_ctx = NULL;
940 struct samr_DomInfo12 lockout_policy;
943 DEBUG(10,("account_lockout_policy_handler called\n"));
945 TALLOC_FREE(child->lockout_policy_event);
947 if ( !winbindd_can_contact_domain( child->domain ) ) {
948 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
949 "do not have an incoming trust to domain %s\n",
950 child->domain->name));
955 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
957 result = NT_STATUS_NO_MEMORY;
959 result = wb_cache_lockout_policy(child->domain, mem_ctx,
962 TALLOC_FREE(mem_ctx);
964 if (!NT_STATUS_IS_OK(result)) {
965 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
969 child->lockout_policy_event = tevent_add_timer(server_event_context(), NULL,
970 timeval_current_ofs(3600, 0),
971 account_lockout_policy_handler,
975 static time_t get_machine_password_timeout(void)
977 /* until we have gpo support use lp setting */
978 return lp_machine_password_timeout();
981 static bool calculate_next_machine_pwd_change(const char *domain,
984 time_t pass_last_set_time;
990 pw = secrets_fetch_machine_password(domain,
995 DEBUG(0,("cannot fetch own machine password ????"));
1001 timeout = get_machine_password_timeout();
1003 DEBUG(10,("machine password never expires\n"));
1007 tv.tv_sec = pass_last_set_time;
1008 DEBUG(10, ("password last changed %s\n",
1009 timeval_string(talloc_tos(), &tv, false)));
1010 tv.tv_sec += timeout;
1011 DEBUGADD(10, ("password valid until %s\n",
1012 timeval_string(talloc_tos(), &tv, false)));
1014 if (time(NULL) < (pass_last_set_time + timeout)) {
1015 next_change = pass_last_set_time + timeout;
1016 DEBUG(10,("machine password still valid until: %s\n",
1017 http_timestring(talloc_tos(), next_change)));
1018 *t = timeval_set(next_change, 0);
1020 if (lp_clustering()) {
1023 * When having a cluster, we have several
1024 * winbinds racing for the password change. In
1025 * the machine_password_change_handler()
1026 * function we check if someone else was
1027 * faster when the event triggers. We add a
1028 * 255-second random delay here, so that we
1029 * don't run to change the password at the
1030 * exact same moment.
1032 generate_random_buffer(&randbuf, sizeof(randbuf));
1033 DEBUG(10, ("adding %d seconds randomness\n",
1035 t->tv_sec += randbuf;
1040 DEBUG(10,("machine password expired, needs immediate change\n"));
1042 *t = timeval_zero();
1047 static void machine_password_change_handler(struct tevent_context *ctx,
1048 struct tevent_timer *te,
1052 struct messaging_context *msg_ctx = server_messaging_context();
1053 struct winbindd_child *child =
1054 (struct winbindd_child *)private_data;
1055 struct rpc_pipe_client *netlogon_pipe = NULL;
1056 struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
1058 struct timeval next_change;
1060 DEBUG(10,("machine_password_change_handler called\n"));
1062 TALLOC_FREE(child->machine_password_change_event);
1064 if (!calculate_next_machine_pwd_change(child->domain->name,
1066 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1070 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1071 timeval_string(talloc_tos(), &next_change, false)));
1073 if (!timeval_expired(&next_change)) {
1074 DEBUG(10, ("Someone else has already changed the pw\n"));
1078 if (!winbindd_can_contact_domain(child->domain)) {
1079 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1080 "do not have an incoming trust to domain %s\n",
1081 child->domain->name));
1085 result = cm_connect_netlogon_secure(child->domain,
1087 &netlogon_creds_ctx);
1088 if (!NT_STATUS_IS_OK(result)) {
1089 DEBUG(10,("machine_password_change_handler: "
1090 "failed to connect netlogon pipe: %s\n",
1091 nt_errstr(result)));
1095 result = trust_pw_change(netlogon_creds_ctx,
1097 netlogon_pipe->binding_handle,
1098 child->domain->name,
1099 child->domain->dcname,
1102 DEBUG(10, ("machine_password_change_handler: "
1103 "trust_pw_change returned %s\n",
1104 nt_errstr(result)));
1106 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1107 DEBUG(3,("machine_password_change_handler: password set returned "
1108 "ACCESS_DENIED. Maybe the trust account "
1109 "password was changed and we didn't know it. "
1110 "Killing connections to domain %s\n",
1111 child->domain->name));
1112 invalidate_cm_connection(child->domain);
1115 if (!calculate_next_machine_pwd_change(child->domain->name,
1117 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1121 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1122 timeval_string(talloc_tos(), &next_change, false)));
1124 if (!NT_STATUS_IS_OK(result)) {
1127 * In case of failure, give the DC a minute to recover
1129 tmp = timeval_current_ofs(60, 0);
1130 next_change = timeval_max(&next_change, &tmp);
1134 child->machine_password_change_event = tevent_add_timer(server_event_context(), NULL,
1136 machine_password_change_handler,
1140 /* Deal with a request to go offline. */
1142 static void child_msg_offline(struct messaging_context *msg,
1145 struct server_id server_id,
1148 struct winbindd_domain *domain;
1149 struct winbindd_domain *primary_domain = NULL;
1150 const char *domainname = (const char *)data->data;
1152 if (data->data == NULL || data->length == 0) {
1156 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1158 if (!lp_winbind_offline_logon()) {
1159 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1163 primary_domain = find_our_domain();
1165 /* Mark the requested domain offline. */
1167 for (domain = domain_list(); domain; domain = domain->next) {
1168 if (domain->internal) {
1171 if (strequal(domain->name, domainname)) {
1172 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1173 set_domain_offline(domain);
1174 /* we are in the trusted domain, set the primary domain
1176 if (domain != primary_domain) {
1177 set_domain_offline(primary_domain);
1183 /* Deal with a request to go online. */
1185 static void child_msg_online(struct messaging_context *msg,
1188 struct server_id server_id,
1191 struct winbindd_domain *domain;
1192 struct winbindd_domain *primary_domain = NULL;
1193 const char *domainname = (const char *)data->data;
1195 if (data->data == NULL || data->length == 0) {
1199 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1201 if (!lp_winbind_offline_logon()) {
1202 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1206 primary_domain = find_our_domain();
1208 /* Set our global state as online. */
1209 set_global_winbindd_state_online();
1211 /* Try and mark everything online - delete any negative cache entries
1212 to force a reconnect now. */
1214 for (domain = domain_list(); domain; domain = domain->next) {
1215 if (domain->internal) {
1218 if (strequal(domain->name, domainname)) {
1219 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1220 winbindd_flush_negative_conn_cache(domain);
1221 set_domain_online_request(domain);
1223 /* we can be in trusted domain, which will contact primary domain
1224 * we have to bring primary domain online in trusted domain process
1225 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1226 * --> contact_domain = find_our_domain()
1228 if (domain != primary_domain) {
1229 winbindd_flush_negative_conn_cache(primary_domain);
1230 set_domain_online_request(primary_domain);
1236 static void child_msg_dump_event_list(struct messaging_context *msg,
1239 struct server_id server_id,
1242 DEBUG(5,("child_msg_dump_event_list received\n"));
1243 DBG_WARNING("dump_event_list no longer implemented\n");
1246 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1247 const char *logfilename)
1249 struct winbindd_domain *domain;
1250 struct winbindd_child *cl;
1253 status = reinit_after_fork(
1254 server_messaging_context(),
1255 server_event_context(),
1257 if (!NT_STATUS_IS_OK(status)) {
1258 DEBUG(0,("reinit_after_fork() failed\n"));
1262 close_conns_after_fork();
1264 if (!override_logfile && logfilename) {
1265 lp_set_logfile(logfilename);
1269 if (!winbindd_setup_sig_term_handler(false))
1270 return NT_STATUS_NO_MEMORY;
1271 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1273 return NT_STATUS_NO_MEMORY;
1275 /* Stop zombies in children */
1278 /* Don't handle the same messages as our parent. */
1279 messaging_deregister(server_messaging_context(),
1280 MSG_SMB_CONF_UPDATED, NULL);
1281 messaging_deregister(server_messaging_context(),
1282 MSG_SHUTDOWN, NULL);
1283 messaging_deregister(server_messaging_context(),
1284 MSG_WINBIND_OFFLINE, NULL);
1285 messaging_deregister(server_messaging_context(),
1286 MSG_WINBIND_ONLINE, NULL);
1287 messaging_deregister(server_messaging_context(),
1288 MSG_WINBIND_ONLINESTATUS, NULL);
1289 messaging_deregister(server_messaging_context(),
1290 MSG_DUMP_EVENT_LIST, NULL);
1291 messaging_deregister(server_messaging_context(),
1292 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1293 messaging_deregister(server_messaging_context(),
1296 messaging_deregister(server_messaging_context(),
1297 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1298 messaging_deregister(server_messaging_context(),
1299 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1301 /* We have destroyed all events in the winbindd_event_context
1302 * in reinit_after_fork(), so clean out all possible pending
1303 * event pointers. */
1305 /* Deal with check_online_events. */
1307 for (domain = domain_list(); domain; domain = domain->next) {
1308 TALLOC_FREE(domain->check_online_event);
1311 /* Ensure we're not handling a credential cache event inherited
1312 * from our parent. */
1314 ccache_remove_all_after_fork();
1316 /* Destroy all possible events in child list. */
1317 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1318 TALLOC_FREE(cl->lockout_policy_event);
1319 TALLOC_FREE(cl->machine_password_change_event);
1321 /* Children should never be able to send
1322 * each other messages, all messages must
1323 * go through the parent.
1328 * Close service sockets to all other children
1330 if ((cl != myself) && (cl->sock != -1)) {
1336 * This is a little tricky, children must not
1337 * send an MSG_WINBIND_ONLINE message to idmap_child().
1338 * If we are in a child of our primary domain or
1339 * in the process created by fork_child_dc_connect(),
1340 * and the primary domain cannot go online,
1341 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1342 * periodically to idmap_child().
1344 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1345 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1346 * ---> init_dc_connection() ---> cm_open_connection --->
1347 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1348 * idmap_child(). Disallow children sending messages
1349 * to each other, all messages must go through the parent.
1354 return NT_STATUS_OK;
1358 * In a child there will be only one domain, reference that here.
1360 static struct winbindd_domain *child_domain;
1362 struct winbindd_domain *wb_child_domain(void)
1364 return child_domain;
1367 struct child_handler_state {
1368 struct winbindd_child *child;
1369 struct winbindd_cli_state cli;
1372 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1373 uint16_t flags, void *private_data)
1375 struct child_handler_state *state =
1376 (struct child_handler_state *)private_data;
1379 /* fetch a request from the main daemon */
1380 status = child_read_request(state->cli.sock, state->cli.request);
1382 if (!NT_STATUS_IS_OK(status)) {
1383 /* we lost contact with our parent */
1387 DEBUG(4,("child daemon request %d\n",
1388 (int)state->cli.request->cmd));
1390 ZERO_STRUCTP(state->cli.response);
1391 state->cli.request->null_term = '\0';
1392 state->cli.mem_ctx = talloc_tos();
1393 child_process_request(state->child, &state->cli);
1395 DEBUG(4, ("Finished processing child request %d\n",
1396 (int)state->cli.request->cmd));
1398 SAFE_FREE(state->cli.request->extra_data.data);
1400 status = child_write_response(state->cli.sock, state->cli.response);
1401 if (!NT_STATUS_IS_OK(status)) {
1406 static bool fork_domain_child(struct winbindd_child *child)
1409 struct child_handler_state state;
1410 struct winbindd_request request;
1411 struct winbindd_response response;
1412 struct winbindd_domain *primary_domain = NULL;
1415 struct tevent_fd *fde;
1417 if (child->domain) {
1418 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1419 child->domain->name));
1421 DEBUG(10, ("fork_domain_child called without domain.\n"));
1424 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1425 DEBUG(0, ("Could not open child pipe: %s\n",
1431 state.child = child;
1432 state.cli.pid = getpid();
1433 state.cli.request = &request;
1434 state.cli.response = &response;
1436 child->pid = fork();
1438 if (child->pid == -1) {
1439 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1445 if (child->pid != 0) {
1451 nread = sys_read(fdpair[1], &status, sizeof(status));
1452 if (nread != sizeof(status)) {
1453 DEBUG(1, ("fork_domain_child: Could not read child status: "
1454 "nread=%d, error=%s\n", (int)nread,
1459 if (!NT_STATUS_IS_OK(status)) {
1460 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1461 nt_errstr(status)));
1466 child->next = child->prev = NULL;
1467 DLIST_ADD(winbindd_children, child);
1468 child->sock = fdpair[1];
1473 child_domain = child->domain;
1475 DEBUG(10, ("Child process %d\n", (int)getpid()));
1477 state.cli.sock = fdpair[0];
1480 status = winbindd_reinit_after_fork(child, child->logfilename);
1482 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1483 if (nwritten != sizeof(status)) {
1484 DEBUG(1, ("fork_domain_child: Could not write status: "
1485 "nwritten=%d, error=%s\n", (int)nwritten,
1489 if (!NT_STATUS_IS_OK(status)) {
1490 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1491 nt_errstr(status)));
1495 if (child_domain != NULL) {
1496 setproctitle("domain child [%s]", child_domain->name);
1497 } else if (child == idmap_child()) {
1498 setproctitle("idmap child");
1501 /* Handle online/offline messages. */
1502 messaging_register(server_messaging_context(), NULL,
1503 MSG_WINBIND_OFFLINE, child_msg_offline);
1504 messaging_register(server_messaging_context(), NULL,
1505 MSG_WINBIND_ONLINE, child_msg_online);
1506 messaging_register(server_messaging_context(), NULL,
1507 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1508 messaging_register(server_messaging_context(), NULL,
1509 MSG_DEBUG, debug_message);
1510 messaging_register(server_messaging_context(), NULL,
1511 MSG_WINBIND_IP_DROPPED,
1512 winbind_msg_ip_dropped);
1515 primary_domain = find_our_domain();
1517 if (primary_domain == NULL) {
1518 smb_panic("no primary domain found");
1521 /* It doesn't matter if we allow cache login,
1522 * try to bring domain online after fork. */
1523 if ( child->domain ) {
1524 child->domain->startup = True;
1525 child->domain->startup_time = time_mono(NULL);
1526 /* we can be in primary domain or in trusted domain
1527 * If we are in trusted domain, set the primary domain
1528 * in start-up mode */
1529 if (!(child->domain->internal)) {
1530 set_domain_online_request(child->domain);
1531 if (!(child->domain->primary)) {
1532 primary_domain->startup = True;
1533 primary_domain->startup_time = time_mono(NULL);
1534 set_domain_online_request(primary_domain);
1540 * We are in idmap child, make sure that we set the
1541 * check_online_event to bring primary domain online.
1543 if (child == idmap_child()) {
1544 set_domain_online_request(primary_domain);
1547 /* We might be in the idmap child...*/
1548 if (child->domain && !(child->domain->internal) &&
1549 lp_winbind_offline_logon()) {
1551 set_domain_online_request(child->domain);
1553 if (primary_domain && (primary_domain != child->domain)) {
1554 /* We need to talk to the primary
1555 * domain as well as the trusted
1556 * domain inside a trusted domain
1559 * set_dc_type_and_flags_trustinfo()
1562 set_domain_online_request(primary_domain);
1565 child->lockout_policy_event = tevent_add_timer(
1566 server_event_context(), NULL, timeval_zero(),
1567 account_lockout_policy_handler,
1571 if (child->domain && child->domain->primary &&
1572 !USE_KERBEROS_KEYTAB &&
1573 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1575 struct timeval next_change;
1577 if (calculate_next_machine_pwd_change(child->domain->name,
1579 child->machine_password_change_event = tevent_add_timer(
1580 server_event_context(), NULL, next_change,
1581 machine_password_change_handler,
1586 fde = tevent_add_fd(server_event_context(), NULL, state.cli.sock,
1587 TEVENT_FD_READ, child_handler, &state);
1589 DEBUG(1, ("tevent_add_fd failed\n"));
1596 TALLOC_CTX *frame = talloc_stackframe();
1598 ret = tevent_loop_once(server_event_context());
1600 DEBUG(1, ("tevent_loop_once failed: %s\n",
1605 if (child->domain && child->domain->startup &&
1606 (time_mono(NULL) > child->domain->startup_time + 30)) {
1607 /* No longer in "startup" mode. */
1608 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1609 child->domain->name ));
1610 child->domain->startup = False;
1617 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1620 struct server_id server_id,
1623 struct winbindd_child *child;
1625 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1629 for (child = winbindd_children; child != NULL; child = child->next) {
1630 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1631 msg_type, data->data, data->length);