s3:winbindd: make use of trust_pw_change() for periodic password changes
[Samba.git] / source3 / winbindd / winbindd_dual.c
blob1d6a5baf70860043f21cf41e2586900e0b0fbf30
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind child daemons
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.
30 #include "includes.h"
31 #include "winbindd.h"
32 #include "rpc_client/rpc_client.h"
33 #include "nsswitch/wb_reqtrans.h"
34 #include "secrets.h"
35 #include "../lib/util/select.h"
36 #include "../libcli/security/security.h"
37 #include "system/select.h"
38 #include "messages.h"
39 #include "../lib/util/tevent_unix.h"
40 #include "lib/param/loadparm.h"
42 #undef DBGC_CLASS
43 #define DBGC_CLASS DBGC_WINBIND
45 extern bool override_logfile;
46 extern struct winbindd_methods cache_methods;
48 static struct winbindd_child *winbindd_children = NULL;
50 /* Read some data from a client connection */
52 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
54 NTSTATUS status;
56 /* Read data */
58 status = read_data(state->sock, (char *)state->request,
59 sizeof(*state->request));
61 if (!NT_STATUS_IS_OK(status)) {
62 DEBUG(3, ("child_read_request: read_data failed: %s\n",
63 nt_errstr(status)));
64 return status;
67 if (state->request->extra_len == 0) {
68 state->request->extra_data.data = NULL;
69 return NT_STATUS_OK;
72 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
74 state->request->extra_data.data =
75 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
77 if (state->request->extra_data.data == NULL) {
78 DEBUG(0, ("malloc failed\n"));
79 return NT_STATUS_NO_MEMORY;
82 /* Ensure null termination */
83 state->request->extra_data.data[state->request->extra_len] = '\0';
85 status= read_data(state->sock, state->request->extra_data.data,
86 state->request->extra_len);
88 if (!NT_STATUS_IS_OK(status)) {
89 DEBUG(0, ("Could not read extra data: %s\n",
90 nt_errstr(status)));
92 return status;
96 * Do winbind child async request. This is not simply wb_simple_trans. We have
97 * to do the queueing ourselves because while a request is queued, the child
98 * might have crashed, and we have to re-fork it in the _trigger function.
101 struct wb_child_request_state {
102 struct tevent_context *ev;
103 struct winbindd_child *child;
104 struct winbindd_request *request;
105 struct winbindd_response *response;
108 static bool fork_domain_child(struct winbindd_child *child);
110 static void wb_child_request_trigger(struct tevent_req *req,
111 void *private_data);
112 static void wb_child_request_done(struct tevent_req *subreq);
114 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
115 struct tevent_context *ev,
116 struct winbindd_child *child,
117 struct winbindd_request *request)
119 struct tevent_req *req;
120 struct wb_child_request_state *state;
122 req = tevent_req_create(mem_ctx, &state,
123 struct wb_child_request_state);
124 if (req == NULL) {
125 return NULL;
128 state->ev = ev;
129 state->child = child;
130 state->request = request;
132 if (!tevent_queue_add(child->queue, ev, req,
133 wb_child_request_trigger, NULL)) {
134 tevent_req_oom(req);
135 return tevent_req_post(req, ev);
137 return req;
140 static void wb_child_request_trigger(struct tevent_req *req,
141 void *private_data)
143 struct wb_child_request_state *state = tevent_req_data(
144 req, struct wb_child_request_state);
145 struct tevent_req *subreq;
147 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
148 tevent_req_error(req, errno);
149 return;
152 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
153 state->child->sock, state->request);
154 if (tevent_req_nomem(subreq, req)) {
155 return;
157 tevent_req_set_callback(subreq, wb_child_request_done, req);
158 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
161 static void wb_child_request_done(struct tevent_req *subreq)
163 struct tevent_req *req = tevent_req_callback_data(
164 subreq, struct tevent_req);
165 struct wb_child_request_state *state = tevent_req_data(
166 req, struct wb_child_request_state);
167 int ret, err;
169 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
170 TALLOC_FREE(subreq);
171 if (ret == -1) {
173 * The basic parent/child communication broke, close
174 * our socket
176 close(state->child->sock);
177 state->child->sock = -1;
178 DLIST_REMOVE(winbindd_children, state->child);
179 tevent_req_error(req, err);
180 return;
182 tevent_req_done(req);
185 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
186 struct winbindd_response **presponse, int *err)
188 struct wb_child_request_state *state = tevent_req_data(
189 req, struct wb_child_request_state);
191 if (tevent_req_is_unix_error(req, err)) {
192 return -1;
194 *presponse = talloc_move(mem_ctx, &state->response);
195 return 0;
198 static bool winbindd_child_busy(struct winbindd_child *child)
200 return tevent_queue_length(child->queue) > 0;
203 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
205 int i;
207 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
208 if (!winbindd_child_busy(&domain->children[i])) {
209 return &domain->children[i];
213 return NULL;
216 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
218 struct winbindd_child *result;
220 result = find_idle_child(domain);
221 if (result != NULL) {
222 return result;
224 return &domain->children[rand() % lp_winbind_max_domain_connections()];
227 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
229 struct winbindd_child *child;
231 child = choose_domain_child(domain);
232 return child->binding_handle;
235 struct wb_domain_request_state {
236 struct tevent_context *ev;
237 struct winbindd_domain *domain;
238 struct winbindd_child *child;
239 struct winbindd_request *request;
240 struct winbindd_request *init_req;
241 struct winbindd_response *response;
244 static void wb_domain_request_gotdc(struct tevent_req *subreq);
245 static void wb_domain_request_initialized(struct tevent_req *subreq);
246 static void wb_domain_request_done(struct tevent_req *subreq);
248 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
249 struct tevent_context *ev,
250 struct winbindd_domain *domain,
251 struct winbindd_request *request)
253 struct tevent_req *req, *subreq;
254 struct wb_domain_request_state *state;
256 req = tevent_req_create(mem_ctx, &state,
257 struct wb_domain_request_state);
258 if (req == NULL) {
259 return NULL;
262 state->child = choose_domain_child(domain);
264 if (domain->initialized) {
265 subreq = wb_child_request_send(state, ev, state->child,
266 request);
267 if (tevent_req_nomem(subreq, req)) {
268 return tevent_req_post(req, ev);
270 tevent_req_set_callback(subreq, wb_domain_request_done, req);
271 return req;
274 state->domain = domain;
275 state->ev = ev;
276 state->request = request;
278 state->init_req = talloc_zero(state, struct winbindd_request);
279 if (tevent_req_nomem(state->init_req, req)) {
280 return tevent_req_post(req, ev);
283 if (IS_DC || domain->primary || domain->internal) {
284 /* The primary domain has to find the DC name itself */
285 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
286 fstrcpy(state->init_req->domain_name, domain->name);
287 state->init_req->data.init_conn.is_primary = domain->primary;
288 fstrcpy(state->init_req->data.init_conn.dcname, "");
290 subreq = wb_child_request_send(state, ev, state->child,
291 state->init_req);
292 if (tevent_req_nomem(subreq, req)) {
293 return tevent_req_post(req, ev);
295 tevent_req_set_callback(subreq, wb_domain_request_initialized,
296 req);
297 return req;
301 * Ask our DC for a DC name
303 domain = find_our_domain();
305 /* This is *not* the primary domain, let's ask our DC about a DC
306 * name */
308 state->init_req->cmd = WINBINDD_GETDCNAME;
309 fstrcpy(state->init_req->domain_name, domain->name);
311 subreq = wb_child_request_send(state, ev, state->child, request);
312 if (tevent_req_nomem(subreq, req)) {
313 return tevent_req_post(req, ev);
315 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
316 return req;
319 static void wb_domain_request_gotdc(struct tevent_req *subreq)
321 struct tevent_req *req = tevent_req_callback_data(
322 subreq, struct tevent_req);
323 struct wb_domain_request_state *state = tevent_req_data(
324 req, struct wb_domain_request_state);
325 struct winbindd_response *response;
326 int ret, err;
328 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
329 TALLOC_FREE(subreq);
330 if (ret == -1) {
331 tevent_req_error(req, err);
332 return;
334 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
335 fstrcpy(state->init_req->domain_name, state->domain->name);
336 state->init_req->data.init_conn.is_primary = False;
337 fstrcpy(state->init_req->data.init_conn.dcname,
338 response->data.dc_name);
340 TALLOC_FREE(response);
342 subreq = wb_child_request_send(state, state->ev, state->child,
343 state->init_req);
344 if (tevent_req_nomem(subreq, req)) {
345 return;
347 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
350 static void wb_domain_request_initialized(struct tevent_req *subreq)
352 struct tevent_req *req = tevent_req_callback_data(
353 subreq, struct tevent_req);
354 struct wb_domain_request_state *state = tevent_req_data(
355 req, struct wb_domain_request_state);
356 struct winbindd_response *response;
357 int ret, err;
359 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
360 TALLOC_FREE(subreq);
361 if (ret == -1) {
362 tevent_req_error(req, err);
363 return;
366 if (!string_to_sid(&state->domain->sid,
367 response->data.domain_info.sid)) {
368 DEBUG(1,("init_child_recv: Could not convert sid %s "
369 "from string\n", response->data.domain_info.sid));
370 tevent_req_error(req, EINVAL);
371 return;
374 talloc_free(state->domain->name);
375 state->domain->name = talloc_strdup(state->domain,
376 response->data.domain_info.name);
377 if (state->domain->name == NULL) {
378 tevent_req_error(req, ENOMEM);
379 return;
382 if (response->data.domain_info.alt_name[0] != '\0') {
383 talloc_free(state->domain->alt_name);
385 state->domain->alt_name = talloc_strdup(state->domain,
386 response->data.domain_info.alt_name);
387 if (state->domain->alt_name == NULL) {
388 tevent_req_error(req, ENOMEM);
389 return;
393 state->domain->native_mode = response->data.domain_info.native_mode;
394 state->domain->active_directory =
395 response->data.domain_info.active_directory;
396 state->domain->initialized = true;
398 TALLOC_FREE(response);
400 subreq = wb_child_request_send(state, state->ev, state->child,
401 state->request);
402 if (tevent_req_nomem(subreq, req)) {
403 return;
405 tevent_req_set_callback(subreq, wb_domain_request_done, req);
408 static void wb_domain_request_done(struct tevent_req *subreq)
410 struct tevent_req *req = tevent_req_callback_data(
411 subreq, struct tevent_req);
412 struct wb_domain_request_state *state = tevent_req_data(
413 req, struct wb_domain_request_state);
414 int ret, err;
416 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
417 &err);
418 TALLOC_FREE(subreq);
419 if (ret == -1) {
420 tevent_req_error(req, err);
421 return;
423 tevent_req_done(req);
426 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
427 struct winbindd_response **presponse, int *err)
429 struct wb_domain_request_state *state = tevent_req_data(
430 req, struct wb_domain_request_state);
432 if (tevent_req_is_unix_error(req, err)) {
433 return -1;
435 *presponse = talloc_move(mem_ctx, &state->response);
436 return 0;
439 static void child_process_request(struct winbindd_child *child,
440 struct winbindd_cli_state *state)
442 struct winbindd_domain *domain = child->domain;
443 const struct winbindd_child_dispatch_table *table = child->table;
445 /* Free response data - we may be interrupted and receive another
446 command before being able to send this data off. */
448 state->response->result = WINBINDD_ERROR;
449 state->response->length = sizeof(struct winbindd_response);
451 /* as all requests in the child are sync, we can use talloc_tos() */
452 state->mem_ctx = talloc_tos();
454 /* Process command */
456 for (; table->name; table++) {
457 if (state->request->cmd == table->struct_cmd) {
458 DEBUG(10,("child_process_request: request fn %s\n",
459 table->name));
460 state->response->result = table->struct_fn(domain, state);
461 return;
465 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
466 (int)state->request->cmd));
467 state->response->result = WINBINDD_ERROR;
470 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
471 const struct winbindd_child_dispatch_table *table,
472 const char *logprefix,
473 const char *logname)
475 if (logprefix && logname) {
476 char *logbase = NULL;
478 if (*lp_logfile(talloc_tos())) {
479 char *end = NULL;
481 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
482 smb_panic("Internal error: asprintf failed");
485 if ((end = strrchr_m(logbase, '/'))) {
486 *end = '\0';
488 } else {
489 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
490 smb_panic("Internal error: asprintf failed");
494 if (asprintf(&child->logfilename, "%s/%s-%s",
495 logbase, logprefix, logname) < 0) {
496 SAFE_FREE(logbase);
497 smb_panic("Internal error: asprintf failed");
500 SAFE_FREE(logbase);
501 } else {
502 smb_panic("Internal error: logprefix == NULL && "
503 "logname == NULL");
506 child->sock = -1;
507 child->domain = domain;
508 child->table = table;
509 child->queue = tevent_queue_create(NULL, "winbind_child");
510 SMB_ASSERT(child->queue != NULL);
511 child->binding_handle = wbint_binding_handle(NULL, domain, child);
512 SMB_ASSERT(child->binding_handle != NULL);
515 void winbind_child_died(pid_t pid)
517 struct winbindd_child *child;
519 for (child = winbindd_children; child != NULL; child = child->next) {
520 if (child->pid == pid) {
521 break;
525 if (child == NULL) {
526 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
527 return;
530 /* This will be re-added in fork_domain_child() */
532 DLIST_REMOVE(winbindd_children, child);
533 child->pid = 0;
535 if (child->sock != -1) {
536 close(child->sock);
537 child->sock = -1;
541 /* Ensure any negative cache entries with the netbios or realm names are removed. */
543 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
545 flush_negative_conn_cache_for_domain(domain->name);
546 if (domain->alt_name != NULL) {
547 flush_negative_conn_cache_for_domain(domain->alt_name);
552 * Parent winbindd process sets its own debug level first and then
553 * sends a message to all the winbindd children to adjust their debug
554 * level to that of parents.
557 void winbind_msg_debug(struct messaging_context *msg_ctx,
558 void *private_data,
559 uint32_t msg_type,
560 struct server_id server_id,
561 DATA_BLOB *data)
563 struct winbindd_child *child;
565 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
567 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
569 for (child = winbindd_children; child != NULL; child = child->next) {
571 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
572 (unsigned int)child->pid));
574 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
575 MSG_DEBUG,
576 data->data,
577 strlen((char *) data->data) + 1);
581 /* Set our domains as offline and forward the offline message to our children. */
583 void winbind_msg_offline(struct messaging_context *msg_ctx,
584 void *private_data,
585 uint32_t msg_type,
586 struct server_id server_id,
587 DATA_BLOB *data)
589 struct winbindd_child *child;
590 struct winbindd_domain *domain;
592 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
594 if (!lp_winbind_offline_logon()) {
595 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
596 return;
599 /* Set our global state as offline. */
600 if (!set_global_winbindd_state_offline()) {
601 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
602 return;
605 /* Set all our domains as offline. */
606 for (domain = domain_list(); domain; domain = domain->next) {
607 if (domain->internal) {
608 continue;
610 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
611 set_domain_offline(domain);
614 for (child = winbindd_children; child != NULL; child = child->next) {
615 /* Don't send message to internal children. We've already
616 done so above. */
617 if (!child->domain || winbindd_internal_child(child)) {
618 continue;
621 /* Or internal domains (this should not be possible....) */
622 if (child->domain->internal) {
623 continue;
626 /* Each winbindd child should only process requests for one domain - make sure
627 we only set it online / offline for that domain. */
629 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
630 (unsigned int)child->pid, child->domain->name ));
632 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
633 MSG_WINBIND_OFFLINE,
634 (const uint8_t *)child->domain->name,
635 strlen(child->domain->name)+1);
639 /* Set our domains as online and forward the online message to our children. */
641 void winbind_msg_online(struct messaging_context *msg_ctx,
642 void *private_data,
643 uint32_t msg_type,
644 struct server_id server_id,
645 DATA_BLOB *data)
647 struct winbindd_child *child;
648 struct winbindd_domain *domain;
650 DEBUG(10,("winbind_msg_online: got online message.\n"));
652 if (!lp_winbind_offline_logon()) {
653 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
654 return;
657 /* Set our global state as online. */
658 set_global_winbindd_state_online();
660 smb_nscd_flush_user_cache();
661 smb_nscd_flush_group_cache();
663 /* Set all our domains as online. */
664 for (domain = domain_list(); domain; domain = domain->next) {
665 if (domain->internal) {
666 continue;
668 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
670 winbindd_flush_negative_conn_cache(domain);
671 set_domain_online_request(domain);
673 /* Send an online message to the idmap child when our
674 primary domain comes back online */
676 if ( domain->primary ) {
677 struct winbindd_child *idmap = idmap_child();
679 if ( idmap->pid != 0 ) {
680 messaging_send_buf(msg_ctx,
681 pid_to_procid(idmap->pid),
682 MSG_WINBIND_ONLINE,
683 (const uint8_t *)domain->name,
684 strlen(domain->name)+1);
689 for (child = winbindd_children; child != NULL; child = child->next) {
690 /* Don't send message to internal childs. */
691 if (!child->domain || winbindd_internal_child(child)) {
692 continue;
695 /* Or internal domains (this should not be possible....) */
696 if (child->domain->internal) {
697 continue;
700 /* Each winbindd child should only process requests for one domain - make sure
701 we only set it online / offline for that domain. */
703 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
704 (unsigned int)child->pid, child->domain->name ));
706 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
707 MSG_WINBIND_ONLINE,
708 (const uint8_t *)child->domain->name,
709 strlen(child->domain->name)+1);
713 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
715 struct winbindd_domain *domain;
716 char *buf = NULL;
718 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
719 get_global_winbindd_state_offline() ?
720 "Offline":"Online")) == NULL) {
721 return NULL;
724 for (domain = domain_list(); domain; domain = domain->next) {
725 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
726 domain->name,
727 domain->online ?
728 "Online":"Offline")) == NULL) {
729 return NULL;
733 buf = talloc_asprintf_append_buffer(buf, "\n");
735 DEBUG(5,("collect_onlinestatus: %s", buf));
737 return buf;
740 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
741 void *private_data,
742 uint32_t msg_type,
743 struct server_id server_id,
744 DATA_BLOB *data)
746 TALLOC_CTX *mem_ctx;
747 const char *message;
748 struct server_id *sender;
750 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
752 if (!data->data) {
753 return;
756 sender = (struct server_id *)data->data;
758 mem_ctx = talloc_init("winbind_msg_onlinestatus");
759 if (mem_ctx == NULL) {
760 return;
763 message = collect_onlinestatus(mem_ctx);
764 if (message == NULL) {
765 talloc_destroy(mem_ctx);
766 return;
769 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
770 (const uint8 *)message, strlen(message) + 1);
772 talloc_destroy(mem_ctx);
775 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
776 void *private_data,
777 uint32_t msg_type,
778 struct server_id server_id,
779 DATA_BLOB *data)
781 struct winbindd_child *child;
783 DEBUG(10,("winbind_msg_dump_event_list received\n"));
785 dump_event_list(winbind_event_context());
787 for (child = winbindd_children; child != NULL; child = child->next) {
789 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
790 (unsigned int)child->pid));
792 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
793 MSG_DUMP_EVENT_LIST,
794 NULL, 0);
799 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
800 void *private_data,
801 uint32_t msg_type,
802 struct server_id server_id,
803 DATA_BLOB *data)
805 TALLOC_CTX *mem_ctx;
806 const char *message = NULL;
807 struct server_id *sender = NULL;
808 const char *domain = NULL;
809 char *s = NULL;
810 NTSTATUS status;
811 struct winbindd_domain *dom = NULL;
813 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
815 if (!data || !data->data) {
816 return;
819 if (data->length < sizeof(struct server_id)) {
820 return;
823 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
824 if (!mem_ctx) {
825 return;
828 sender = (struct server_id *)data->data;
829 if (data->length > sizeof(struct server_id)) {
830 domain = (const char *)data->data+sizeof(struct server_id);
833 if (domain) {
835 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
836 domain));
838 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
839 find_domain_from_name_noinit(domain));
840 if (!message) {
841 talloc_destroy(mem_ctx);
842 return;
845 messaging_send_buf(msg_ctx, *sender,
846 MSG_WINBIND_DUMP_DOMAIN_LIST,
847 (const uint8_t *)message, strlen(message) + 1);
849 talloc_destroy(mem_ctx);
851 return;
854 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
856 for (dom = domain_list(); dom; dom=dom->next) {
857 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
858 if (!message) {
859 talloc_destroy(mem_ctx);
860 return;
863 s = talloc_asprintf_append(s, "%s\n", message);
864 if (!s) {
865 talloc_destroy(mem_ctx);
866 return;
870 status = messaging_send_buf(msg_ctx, *sender,
871 MSG_WINBIND_DUMP_DOMAIN_LIST,
872 (uint8_t *)s, strlen(s) + 1);
873 if (!NT_STATUS_IS_OK(status)) {
874 DEBUG(0,("failed to send message: %s\n",
875 nt_errstr(status)));
878 talloc_destroy(mem_ctx);
881 static void account_lockout_policy_handler(struct tevent_context *ctx,
882 struct tevent_timer *te,
883 struct timeval now,
884 void *private_data)
886 struct winbindd_child *child =
887 (struct winbindd_child *)private_data;
888 TALLOC_CTX *mem_ctx = NULL;
889 struct winbindd_methods *methods;
890 struct samr_DomInfo12 lockout_policy;
891 NTSTATUS result;
893 DEBUG(10,("account_lockout_policy_handler called\n"));
895 TALLOC_FREE(child->lockout_policy_event);
897 if ( !winbindd_can_contact_domain( child->domain ) ) {
898 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
899 "do not have an incoming trust to domain %s\n",
900 child->domain->name));
902 return;
905 methods = child->domain->methods;
907 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
908 if (!mem_ctx) {
909 result = NT_STATUS_NO_MEMORY;
910 } else {
911 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
913 TALLOC_FREE(mem_ctx);
915 if (!NT_STATUS_IS_OK(result)) {
916 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
917 nt_errstr(result)));
920 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
921 timeval_current_ofs(3600, 0),
922 account_lockout_policy_handler,
923 child);
926 static time_t get_machine_password_timeout(void)
928 /* until we have gpo support use lp setting */
929 return lp_machine_password_timeout();
932 static bool calculate_next_machine_pwd_change(const char *domain,
933 struct timeval *t)
935 time_t pass_last_set_time;
936 time_t timeout;
937 time_t next_change;
938 struct timeval tv;
939 char *pw;
941 pw = secrets_fetch_machine_password(domain,
942 &pass_last_set_time,
943 NULL);
945 if (pw == NULL) {
946 DEBUG(0,("cannot fetch own machine password ????"));
947 return false;
950 SAFE_FREE(pw);
952 timeout = get_machine_password_timeout();
953 if (timeout == 0) {
954 DEBUG(10,("machine password never expires\n"));
955 return false;
958 tv.tv_sec = pass_last_set_time;
959 DEBUG(10, ("password last changed %s\n",
960 timeval_string(talloc_tos(), &tv, false)));
961 tv.tv_sec += timeout;
962 DEBUGADD(10, ("password valid until %s\n",
963 timeval_string(talloc_tos(), &tv, false)));
965 if (time(NULL) < (pass_last_set_time + timeout)) {
966 next_change = pass_last_set_time + timeout;
967 DEBUG(10,("machine password still valid until: %s\n",
968 http_timestring(talloc_tos(), next_change)));
969 *t = timeval_set(next_change, 0);
971 if (lp_clustering()) {
972 uint8_t randbuf;
974 * When having a cluster, we have several
975 * winbinds racing for the password change. In
976 * the machine_password_change_handler()
977 * function we check if someone else was
978 * faster when the event triggers. We add a
979 * 255-second random delay here, so that we
980 * don't run to change the password at the
981 * exact same moment.
983 generate_random_buffer(&randbuf, sizeof(randbuf));
984 DEBUG(10, ("adding %d seconds randomness\n",
985 (int)randbuf));
986 t->tv_sec += randbuf;
988 return true;
991 DEBUG(10,("machine password expired, needs immediate change\n"));
993 *t = timeval_zero();
995 return true;
998 static void machine_password_change_handler(struct tevent_context *ctx,
999 struct tevent_timer *te,
1000 struct timeval now,
1001 void *private_data)
1003 struct messaging_context *msg_ctx = winbind_messaging_context();
1004 struct winbindd_child *child =
1005 (struct winbindd_child *)private_data;
1006 struct rpc_pipe_client *netlogon_pipe = NULL;
1007 NTSTATUS result;
1008 struct timeval next_change;
1010 DEBUG(10,("machine_password_change_handler called\n"));
1012 TALLOC_FREE(child->machine_password_change_event);
1014 if (!calculate_next_machine_pwd_change(child->domain->name,
1015 &next_change)) {
1016 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1017 return;
1020 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1021 timeval_string(talloc_tos(), &next_change, false)));
1023 if (!timeval_expired(&next_change)) {
1024 DEBUG(10, ("Someone else has already changed the pw\n"));
1025 goto done;
1028 if (!winbindd_can_contact_domain(child->domain)) {
1029 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1030 "do not have an incoming trust to domain %s\n",
1031 child->domain->name));
1032 return;
1035 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1036 if (!NT_STATUS_IS_OK(result)) {
1037 DEBUG(10,("machine_password_change_handler: "
1038 "failed to connect netlogon pipe: %s\n",
1039 nt_errstr(result)));
1040 return;
1043 result = trust_pw_change(child->domain->conn.netlogon_creds,
1044 msg_ctx,
1045 netlogon_pipe->binding_handle,
1046 child->domain->name,
1047 false); /* force */
1049 DEBUG(10, ("machine_password_change_handler: "
1050 "trust_pw_change returned %s\n",
1051 nt_errstr(result)));
1053 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1054 DEBUG(3,("machine_password_change_handler: password set returned "
1055 "ACCESS_DENIED. Maybe the trust account "
1056 "password was changed and we didn't know it. "
1057 "Killing connections to domain %s\n",
1058 child->domain->name));
1059 invalidate_cm_connection(&child->domain->conn);
1062 if (!calculate_next_machine_pwd_change(child->domain->name,
1063 &next_change)) {
1064 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1065 return;
1068 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1069 timeval_string(talloc_tos(), &next_change, false)));
1071 if (!NT_STATUS_IS_OK(result)) {
1072 struct timeval tmp;
1074 * In case of failure, give the DC a minute to recover
1076 tmp = timeval_current_ofs(60, 0);
1077 next_change = timeval_max(&next_change, &tmp);
1080 done:
1081 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1082 next_change,
1083 machine_password_change_handler,
1084 child);
1087 /* Deal with a request to go offline. */
1089 static void child_msg_offline(struct messaging_context *msg,
1090 void *private_data,
1091 uint32_t msg_type,
1092 struct server_id server_id,
1093 DATA_BLOB *data)
1095 struct winbindd_domain *domain;
1096 struct winbindd_domain *primary_domain = NULL;
1097 const char *domainname = (const char *)data->data;
1099 if (data->data == NULL || data->length == 0) {
1100 return;
1103 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1105 if (!lp_winbind_offline_logon()) {
1106 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1107 return;
1110 primary_domain = find_our_domain();
1112 /* Mark the requested domain offline. */
1114 for (domain = domain_list(); domain; domain = domain->next) {
1115 if (domain->internal) {
1116 continue;
1118 if (strequal(domain->name, domainname)) {
1119 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1120 set_domain_offline(domain);
1121 /* we are in the trusted domain, set the primary domain
1122 * offline too */
1123 if (domain != primary_domain) {
1124 set_domain_offline(primary_domain);
1130 /* Deal with a request to go online. */
1132 static void child_msg_online(struct messaging_context *msg,
1133 void *private_data,
1134 uint32_t msg_type,
1135 struct server_id server_id,
1136 DATA_BLOB *data)
1138 struct winbindd_domain *domain;
1139 struct winbindd_domain *primary_domain = NULL;
1140 const char *domainname = (const char *)data->data;
1142 if (data->data == NULL || data->length == 0) {
1143 return;
1146 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1148 if (!lp_winbind_offline_logon()) {
1149 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1150 return;
1153 primary_domain = find_our_domain();
1155 /* Set our global state as online. */
1156 set_global_winbindd_state_online();
1158 /* Try and mark everything online - delete any negative cache entries
1159 to force a reconnect now. */
1161 for (domain = domain_list(); domain; domain = domain->next) {
1162 if (domain->internal) {
1163 continue;
1165 if (strequal(domain->name, domainname)) {
1166 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1167 winbindd_flush_negative_conn_cache(domain);
1168 set_domain_online_request(domain);
1170 /* we can be in trusted domain, which will contact primary domain
1171 * we have to bring primary domain online in trusted domain process
1172 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1173 * --> contact_domain = find_our_domain()
1174 * */
1175 if (domain != primary_domain) {
1176 winbindd_flush_negative_conn_cache(primary_domain);
1177 set_domain_online_request(primary_domain);
1183 static void child_msg_dump_event_list(struct messaging_context *msg,
1184 void *private_data,
1185 uint32_t msg_type,
1186 struct server_id server_id,
1187 DATA_BLOB *data)
1189 DEBUG(5,("child_msg_dump_event_list received\n"));
1191 dump_event_list(winbind_event_context());
1194 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1195 const char *logfilename)
1197 struct winbindd_domain *domain;
1198 struct winbindd_child *cl;
1199 NTSTATUS status;
1201 status = reinit_after_fork(
1202 winbind_messaging_context(),
1203 winbind_event_context(),
1204 true);
1205 if (!NT_STATUS_IS_OK(status)) {
1206 DEBUG(0,("reinit_after_fork() failed\n"));
1207 return status;
1210 close_conns_after_fork();
1212 if (!override_logfile && logfilename) {
1213 lp_set_logfile(logfilename);
1214 reopen_logs();
1217 if (!winbindd_setup_sig_term_handler(false))
1218 return NT_STATUS_NO_MEMORY;
1219 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1220 logfilename))
1221 return NT_STATUS_NO_MEMORY;
1223 /* Stop zombies in children */
1224 CatchChild();
1226 /* Don't handle the same messages as our parent. */
1227 messaging_deregister(winbind_messaging_context(),
1228 MSG_SMB_CONF_UPDATED, NULL);
1229 messaging_deregister(winbind_messaging_context(),
1230 MSG_SHUTDOWN, NULL);
1231 messaging_deregister(winbind_messaging_context(),
1232 MSG_WINBIND_OFFLINE, NULL);
1233 messaging_deregister(winbind_messaging_context(),
1234 MSG_WINBIND_ONLINE, NULL);
1235 messaging_deregister(winbind_messaging_context(),
1236 MSG_WINBIND_ONLINESTATUS, NULL);
1237 messaging_deregister(winbind_messaging_context(),
1238 MSG_DUMP_EVENT_LIST, NULL);
1239 messaging_deregister(winbind_messaging_context(),
1240 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1241 messaging_deregister(winbind_messaging_context(),
1242 MSG_DEBUG, NULL);
1244 messaging_deregister(winbind_messaging_context(),
1245 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1246 messaging_deregister(winbind_messaging_context(),
1247 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1249 /* We have destroyed all events in the winbindd_event_context
1250 * in reinit_after_fork(), so clean out all possible pending
1251 * event pointers. */
1253 /* Deal with check_online_events. */
1255 for (domain = domain_list(); domain; domain = domain->next) {
1256 TALLOC_FREE(domain->check_online_event);
1259 /* Ensure we're not handling a credential cache event inherited
1260 * from our parent. */
1262 ccache_remove_all_after_fork();
1264 /* Destroy all possible events in child list. */
1265 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1266 TALLOC_FREE(cl->lockout_policy_event);
1267 TALLOC_FREE(cl->machine_password_change_event);
1269 /* Children should never be able to send
1270 * each other messages, all messages must
1271 * go through the parent.
1273 cl->pid = (pid_t)0;
1276 * Close service sockets to all other children
1278 if ((cl != myself) && (cl->sock != -1)) {
1279 close(cl->sock);
1280 cl->sock = -1;
1284 * This is a little tricky, children must not
1285 * send an MSG_WINBIND_ONLINE message to idmap_child().
1286 * If we are in a child of our primary domain or
1287 * in the process created by fork_child_dc_connect(),
1288 * and the primary domain cannot go online,
1289 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1290 * periodically to idmap_child().
1292 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1293 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1294 * ---> init_dc_connection() ---> cm_open_connection --->
1295 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1296 * idmap_child(). Disallow children sending messages
1297 * to each other, all messages must go through the parent.
1299 cl = idmap_child();
1300 cl->pid = (pid_t)0;
1302 return NT_STATUS_OK;
1306 * In a child there will be only one domain, reference that here.
1308 static struct winbindd_domain *child_domain;
1310 struct winbindd_domain *wb_child_domain(void)
1312 return child_domain;
1315 struct child_handler_state {
1316 struct winbindd_child *child;
1317 struct winbindd_cli_state cli;
1320 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1321 uint16_t flags, void *private_data)
1323 struct child_handler_state *state =
1324 (struct child_handler_state *)private_data;
1325 NTSTATUS status;
1326 struct iovec iov[2];
1327 int iov_count;
1329 /* fetch a request from the main daemon */
1330 status = child_read_request(&state->cli);
1332 if (!NT_STATUS_IS_OK(status)) {
1333 /* we lost contact with our parent */
1334 _exit(0);
1337 DEBUG(4,("child daemon request %d\n",
1338 (int)state->cli.request->cmd));
1340 ZERO_STRUCTP(state->cli.response);
1341 state->cli.request->null_term = '\0';
1342 state->cli.mem_ctx = talloc_tos();
1343 child_process_request(state->child, &state->cli);
1345 DEBUG(4, ("Finished processing child request %d\n",
1346 (int)state->cli.request->cmd));
1348 SAFE_FREE(state->cli.request->extra_data.data);
1350 iov[0].iov_base = (void *)state->cli.response;
1351 iov[0].iov_len = sizeof(struct winbindd_response);
1352 iov_count = 1;
1354 if (state->cli.response->length >
1355 sizeof(struct winbindd_response)) {
1356 iov[1].iov_base =
1357 (void *)state->cli.response->extra_data.data;
1358 iov[1].iov_len = state->cli.response->length-iov[0].iov_len;
1359 iov_count = 2;
1362 DEBUG(10, ("Writing %d bytes to parent\n",
1363 (int)state->cli.response->length));
1365 if (write_data_iov(state->cli.sock, iov, iov_count) !=
1366 state->cli.response->length) {
1367 DEBUG(0, ("Could not write result\n"));
1368 exit(1);
1372 static bool fork_domain_child(struct winbindd_child *child)
1374 int fdpair[2];
1375 struct child_handler_state state;
1376 struct winbindd_request request;
1377 struct winbindd_response response;
1378 struct winbindd_domain *primary_domain = NULL;
1379 NTSTATUS status;
1380 ssize_t nwritten;
1381 struct tevent_fd *fde;
1383 if (child->domain) {
1384 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1385 child->domain->name));
1386 } else {
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",
1392 strerror(errno)));
1393 return False;
1396 ZERO_STRUCT(state);
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)));
1406 close(fdpair[0]);
1407 close(fdpair[1]);
1408 return False;
1411 if (child->pid != 0) {
1412 /* Parent */
1413 ssize_t nread;
1415 close(fdpair[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,
1421 strerror(errno)));
1422 close(fdpair[1]);
1423 return false;
1425 if (!NT_STATUS_IS_OK(status)) {
1426 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1427 nt_errstr(status)));
1428 close(fdpair[1]);
1429 return false;
1432 child->next = child->prev = NULL;
1433 DLIST_ADD(winbindd_children, child);
1434 child->sock = fdpair[1];
1435 return True;
1438 /* Child */
1439 child_domain = child->domain;
1441 DEBUG(10, ("Child process %d\n", (int)getpid()));
1443 state.cli.sock = fdpair[0];
1444 close(fdpair[1]);
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,
1452 strerror(errno)));
1453 _exit(0);
1455 if (!NT_STATUS_IS_OK(status)) {
1456 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1457 nt_errstr(status)));
1458 _exit(0);
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
1517 * child.
1518 * See the code in :
1519 * set_dc_type_and_flags_trustinfo()
1520 * for details.
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,
1528 child);
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,
1538 &next_change)) {
1539 child->machine_password_change_event = tevent_add_timer(
1540 winbind_event_context(), NULL, next_change,
1541 machine_password_change_handler,
1542 child);
1546 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1547 TEVENT_FD_READ, child_handler, &state);
1548 if (fde == NULL) {
1549 DEBUG(1, ("tevent_add_fd failed\n"));
1550 _exit(1);
1553 while (1) {
1555 int ret;
1556 TALLOC_CTX *frame = talloc_stackframe();
1558 ret = tevent_loop_once(winbind_event_context());
1559 if (ret != 0) {
1560 DEBUG(1, ("tevent_loop_once failed: %s\n",
1561 strerror(errno)));
1562 _exit(1);
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;
1573 TALLOC_FREE(frame);
1577 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1578 void *private_data,
1579 uint32_t msg_type,
1580 struct server_id server_id,
1581 DATA_BLOB *data)
1583 struct winbindd_child *child;
1585 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1586 server_id, data);
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);