WHATSNEW: Add release notes for Samba 4.0.24.
[Samba.git] / source3 / winbindd / winbindd_dual.c
blob3c0e75713a95378dc6880ac419604a2ba529ef8c
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 "nsswitch/wb_reqtrans.h"
33 #include "secrets.h"
34 #include "../lib/util/select.h"
35 #include "../libcli/security/security.h"
36 #include "system/select.h"
37 #include "messages.h"
38 #include "../lib/util/tevent_unix.h"
39 #include "lib/param/loadparm.h"
41 #undef DBGC_CLASS
42 #define DBGC_CLASS DBGC_WINBIND
44 extern bool override_logfile;
45 extern struct winbindd_methods cache_methods;
47 static struct winbindd_child *winbindd_children = NULL;
49 /* Read some data from a client connection */
51 static NTSTATUS child_read_request(struct winbindd_cli_state *state)
53 NTSTATUS status;
55 /* Read data */
57 status = read_data(state->sock, (char *)state->request,
58 sizeof(*state->request));
60 if (!NT_STATUS_IS_OK(status)) {
61 DEBUG(3, ("child_read_request: read_data failed: %s\n",
62 nt_errstr(status)));
63 return status;
66 if (state->request->extra_len == 0) {
67 state->request->extra_data.data = NULL;
68 return NT_STATUS_OK;
71 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request->extra_len));
73 state->request->extra_data.data =
74 SMB_MALLOC_ARRAY(char, state->request->extra_len + 1);
76 if (state->request->extra_data.data == NULL) {
77 DEBUG(0, ("malloc failed\n"));
78 return NT_STATUS_NO_MEMORY;
81 /* Ensure null termination */
82 state->request->extra_data.data[state->request->extra_len] = '\0';
84 status= read_data(state->sock, state->request->extra_data.data,
85 state->request->extra_len);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(0, ("Could not read extra data: %s\n",
89 nt_errstr(status)));
91 return status;
95 * Do winbind child async request. This is not simply wb_simple_trans. We have
96 * to do the queueing ourselves because while a request is queued, the child
97 * might have crashed, and we have to re-fork it in the _trigger function.
100 struct wb_child_request_state {
101 struct tevent_context *ev;
102 struct winbindd_child *child;
103 struct winbindd_request *request;
104 struct winbindd_response *response;
107 static bool fork_domain_child(struct winbindd_child *child);
109 static void wb_child_request_trigger(struct tevent_req *req,
110 void *private_data);
111 static void wb_child_request_done(struct tevent_req *subreq);
113 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
114 struct tevent_context *ev,
115 struct winbindd_child *child,
116 struct winbindd_request *request)
118 struct tevent_req *req;
119 struct wb_child_request_state *state;
121 req = tevent_req_create(mem_ctx, &state,
122 struct wb_child_request_state);
123 if (req == NULL) {
124 return NULL;
127 state->ev = ev;
128 state->child = child;
129 state->request = request;
131 if (!tevent_queue_add(child->queue, ev, req,
132 wb_child_request_trigger, NULL)) {
133 tevent_req_oom(req);
134 return tevent_req_post(req, ev);
136 return req;
139 static void wb_child_request_trigger(struct tevent_req *req,
140 void *private_data)
142 struct wb_child_request_state *state = tevent_req_data(
143 req, struct wb_child_request_state);
144 struct tevent_req *subreq;
146 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
147 tevent_req_error(req, errno);
148 return;
151 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
152 state->child->sock, state->request);
153 if (tevent_req_nomem(subreq, req)) {
154 return;
156 tevent_req_set_callback(subreq, wb_child_request_done, req);
157 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
160 static void wb_child_request_done(struct tevent_req *subreq)
162 struct tevent_req *req = tevent_req_callback_data(
163 subreq, struct tevent_req);
164 struct wb_child_request_state *state = tevent_req_data(
165 req, struct wb_child_request_state);
166 int ret, err;
168 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
169 TALLOC_FREE(subreq);
170 if (ret == -1) {
172 * The basic parent/child communication broke, close
173 * our socket
175 close(state->child->sock);
176 state->child->sock = -1;
177 DLIST_REMOVE(winbindd_children, state->child);
178 tevent_req_error(req, err);
179 return;
181 tevent_req_done(req);
184 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
185 struct winbindd_response **presponse, int *err)
187 struct wb_child_request_state *state = tevent_req_data(
188 req, struct wb_child_request_state);
190 if (tevent_req_is_unix_error(req, err)) {
191 return -1;
193 *presponse = talloc_move(mem_ctx, &state->response);
194 return 0;
197 static bool winbindd_child_busy(struct winbindd_child *child)
199 return tevent_queue_length(child->queue) > 0;
202 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
204 int i;
206 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
207 if (!winbindd_child_busy(&domain->children[i])) {
208 return &domain->children[i];
212 return NULL;
215 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
217 struct winbindd_child *result;
219 result = find_idle_child(domain);
220 if (result != NULL) {
221 return result;
223 return &domain->children[rand() % lp_winbind_max_domain_connections()];
226 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
228 struct winbindd_child *child;
230 child = choose_domain_child(domain);
231 return child->binding_handle;
234 struct wb_domain_request_state {
235 struct tevent_context *ev;
236 struct winbindd_domain *domain;
237 struct winbindd_child *child;
238 struct winbindd_request *request;
239 struct winbindd_request *init_req;
240 struct winbindd_response *response;
243 static void wb_domain_request_gotdc(struct tevent_req *subreq);
244 static void wb_domain_request_initialized(struct tevent_req *subreq);
245 static void wb_domain_request_done(struct tevent_req *subreq);
247 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
248 struct tevent_context *ev,
249 struct winbindd_domain *domain,
250 struct winbindd_request *request)
252 struct tevent_req *req, *subreq;
253 struct wb_domain_request_state *state;
255 req = tevent_req_create(mem_ctx, &state,
256 struct wb_domain_request_state);
257 if (req == NULL) {
258 return NULL;
261 state->child = choose_domain_child(domain);
263 if (domain->initialized) {
264 subreq = wb_child_request_send(state, ev, state->child,
265 request);
266 if (tevent_req_nomem(subreq, req)) {
267 return tevent_req_post(req, ev);
269 tevent_req_set_callback(subreq, wb_domain_request_done, req);
270 return req;
273 state->domain = domain;
274 state->ev = ev;
275 state->request = request;
277 state->init_req = talloc_zero(state, struct winbindd_request);
278 if (tevent_req_nomem(state->init_req, req)) {
279 return tevent_req_post(req, ev);
282 if (IS_DC || domain->primary || domain->internal) {
283 /* The primary domain has to find the DC name itself */
284 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
285 fstrcpy(state->init_req->domain_name, domain->name);
286 state->init_req->data.init_conn.is_primary = domain->primary;
287 fstrcpy(state->init_req->data.init_conn.dcname, "");
289 subreq = wb_child_request_send(state, ev, state->child,
290 state->init_req);
291 if (tevent_req_nomem(subreq, req)) {
292 return tevent_req_post(req, ev);
294 tevent_req_set_callback(subreq, wb_domain_request_initialized,
295 req);
296 return req;
300 * Ask our DC for a DC name
302 domain = find_our_domain();
304 /* This is *not* the primary domain, let's ask our DC about a DC
305 * name */
307 state->init_req->cmd = WINBINDD_GETDCNAME;
308 fstrcpy(state->init_req->domain_name, domain->name);
310 subreq = wb_child_request_send(state, ev, state->child, request);
311 if (tevent_req_nomem(subreq, req)) {
312 return tevent_req_post(req, ev);
314 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
315 return req;
318 static void wb_domain_request_gotdc(struct tevent_req *subreq)
320 struct tevent_req *req = tevent_req_callback_data(
321 subreq, struct tevent_req);
322 struct wb_domain_request_state *state = tevent_req_data(
323 req, struct wb_domain_request_state);
324 struct winbindd_response *response;
325 int ret, err;
327 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
328 TALLOC_FREE(subreq);
329 if (ret == -1) {
330 tevent_req_error(req, err);
331 return;
333 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
334 fstrcpy(state->init_req->domain_name, state->domain->name);
335 state->init_req->data.init_conn.is_primary = False;
336 fstrcpy(state->init_req->data.init_conn.dcname,
337 response->data.dc_name);
339 TALLOC_FREE(response);
341 subreq = wb_child_request_send(state, state->ev, state->child,
342 state->init_req);
343 if (tevent_req_nomem(subreq, req)) {
344 return;
346 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
349 static void wb_domain_request_initialized(struct tevent_req *subreq)
351 struct tevent_req *req = tevent_req_callback_data(
352 subreq, struct tevent_req);
353 struct wb_domain_request_state *state = tevent_req_data(
354 req, struct wb_domain_request_state);
355 struct winbindd_response *response;
356 int ret, err;
358 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
359 TALLOC_FREE(subreq);
360 if (ret == -1) {
361 tevent_req_error(req, err);
362 return;
365 if (!string_to_sid(&state->domain->sid,
366 response->data.domain_info.sid)) {
367 DEBUG(1,("init_child_recv: Could not convert sid %s "
368 "from string\n", response->data.domain_info.sid));
369 tevent_req_error(req, EINVAL);
370 return;
372 fstrcpy(state->domain->name, response->data.domain_info.name);
373 fstrcpy(state->domain->alt_name, response->data.domain_info.alt_name);
374 state->domain->native_mode = response->data.domain_info.native_mode;
375 state->domain->active_directory =
376 response->data.domain_info.active_directory;
377 state->domain->initialized = true;
379 TALLOC_FREE(response);
381 subreq = wb_child_request_send(state, state->ev, state->child,
382 state->request);
383 if (tevent_req_nomem(subreq, req)) {
384 return;
386 tevent_req_set_callback(subreq, wb_domain_request_done, req);
389 static void wb_domain_request_done(struct tevent_req *subreq)
391 struct tevent_req *req = tevent_req_callback_data(
392 subreq, struct tevent_req);
393 struct wb_domain_request_state *state = tevent_req_data(
394 req, struct wb_domain_request_state);
395 int ret, err;
397 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
398 &err);
399 TALLOC_FREE(subreq);
400 if (ret == -1) {
401 tevent_req_error(req, err);
402 return;
404 tevent_req_done(req);
407 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
408 struct winbindd_response **presponse, int *err)
410 struct wb_domain_request_state *state = tevent_req_data(
411 req, struct wb_domain_request_state);
413 if (tevent_req_is_unix_error(req, err)) {
414 return -1;
416 *presponse = talloc_move(mem_ctx, &state->response);
417 return 0;
420 static void child_process_request(struct winbindd_child *child,
421 struct winbindd_cli_state *state)
423 struct winbindd_domain *domain = child->domain;
424 const struct winbindd_child_dispatch_table *table = child->table;
426 /* Free response data - we may be interrupted and receive another
427 command before being able to send this data off. */
429 state->response->result = WINBINDD_ERROR;
430 state->response->length = sizeof(struct winbindd_response);
432 /* as all requests in the child are sync, we can use talloc_tos() */
433 state->mem_ctx = talloc_tos();
435 /* Process command */
437 for (; table->name; table++) {
438 if (state->request->cmd == table->struct_cmd) {
439 DEBUG(10,("child_process_request: request fn %s\n",
440 table->name));
441 state->response->result = table->struct_fn(domain, state);
442 return;
446 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
447 (int)state->request->cmd));
448 state->response->result = WINBINDD_ERROR;
451 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
452 const struct winbindd_child_dispatch_table *table,
453 const char *logprefix,
454 const char *logname)
456 if (logprefix && logname) {
457 char *logbase = NULL;
459 if (*lp_logfile(talloc_tos())) {
460 char *end = NULL;
462 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
463 smb_panic("Internal error: asprintf failed");
466 if ((end = strrchr_m(logbase, '/'))) {
467 *end = '\0';
469 } else {
470 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
471 smb_panic("Internal error: asprintf failed");
475 if (asprintf(&child->logfilename, "%s/%s-%s",
476 logbase, logprefix, logname) < 0) {
477 SAFE_FREE(logbase);
478 smb_panic("Internal error: asprintf failed");
481 SAFE_FREE(logbase);
482 } else {
483 smb_panic("Internal error: logprefix == NULL && "
484 "logname == NULL");
487 child->sock = -1;
488 child->domain = domain;
489 child->table = table;
490 child->queue = tevent_queue_create(NULL, "winbind_child");
491 SMB_ASSERT(child->queue != NULL);
492 child->binding_handle = wbint_binding_handle(NULL, domain, child);
493 SMB_ASSERT(child->binding_handle != NULL);
496 void winbind_child_died(pid_t pid)
498 struct winbindd_child *child;
500 for (child = winbindd_children; child != NULL; child = child->next) {
501 if (child->pid == pid) {
502 break;
506 if (child == NULL) {
507 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
508 return;
511 /* This will be re-added in fork_domain_child() */
513 DLIST_REMOVE(winbindd_children, child);
514 child->pid = 0;
516 if (child->sock != -1) {
517 close(child->sock);
518 child->sock = -1;
522 /* Ensure any negative cache entries with the netbios or realm names are removed. */
524 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
526 flush_negative_conn_cache_for_domain(domain->name);
527 if (*domain->alt_name) {
528 flush_negative_conn_cache_for_domain(domain->alt_name);
533 * Parent winbindd process sets its own debug level first and then
534 * sends a message to all the winbindd children to adjust their debug
535 * level to that of parents.
538 void winbind_msg_debug(struct messaging_context *msg_ctx,
539 void *private_data,
540 uint32_t msg_type,
541 struct server_id server_id,
542 DATA_BLOB *data)
544 struct winbindd_child *child;
546 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
548 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
550 for (child = winbindd_children; child != NULL; child = child->next) {
552 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
553 (unsigned int)child->pid));
555 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
556 MSG_DEBUG,
557 data->data,
558 strlen((char *) data->data) + 1);
562 /* Set our domains as offline and forward the offline message to our children. */
564 void winbind_msg_offline(struct messaging_context *msg_ctx,
565 void *private_data,
566 uint32_t msg_type,
567 struct server_id server_id,
568 DATA_BLOB *data)
570 struct winbindd_child *child;
571 struct winbindd_domain *domain;
573 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
575 if (!lp_winbind_offline_logon()) {
576 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
577 return;
580 /* Set our global state as offline. */
581 if (!set_global_winbindd_state_offline()) {
582 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
583 return;
586 /* Set all our domains as offline. */
587 for (domain = domain_list(); domain; domain = domain->next) {
588 if (domain->internal) {
589 continue;
591 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
592 set_domain_offline(domain);
595 for (child = winbindd_children; child != NULL; child = child->next) {
596 /* Don't send message to internal children. We've already
597 done so above. */
598 if (!child->domain || winbindd_internal_child(child)) {
599 continue;
602 /* Or internal domains (this should not be possible....) */
603 if (child->domain->internal) {
604 continue;
607 /* Each winbindd child should only process requests for one domain - make sure
608 we only set it online / offline for that domain. */
610 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
611 (unsigned int)child->pid, domain->name ));
613 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
614 MSG_WINBIND_OFFLINE,
615 (uint8 *)child->domain->name,
616 strlen(child->domain->name)+1);
620 /* Set our domains as online and forward the online message to our children. */
622 void winbind_msg_online(struct messaging_context *msg_ctx,
623 void *private_data,
624 uint32_t msg_type,
625 struct server_id server_id,
626 DATA_BLOB *data)
628 struct winbindd_child *child;
629 struct winbindd_domain *domain;
631 DEBUG(10,("winbind_msg_online: got online message.\n"));
633 if (!lp_winbind_offline_logon()) {
634 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
635 return;
638 /* Set our global state as online. */
639 set_global_winbindd_state_online();
641 smb_nscd_flush_user_cache();
642 smb_nscd_flush_group_cache();
644 /* Set all our domains as online. */
645 for (domain = domain_list(); domain; domain = domain->next) {
646 if (domain->internal) {
647 continue;
649 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
651 winbindd_flush_negative_conn_cache(domain);
652 set_domain_online_request(domain);
654 /* Send an online message to the idmap child when our
655 primary domain comes back online */
657 if ( domain->primary ) {
658 struct winbindd_child *idmap = idmap_child();
660 if ( idmap->pid != 0 ) {
661 messaging_send_buf(msg_ctx,
662 pid_to_procid(idmap->pid),
663 MSG_WINBIND_ONLINE,
664 (uint8 *)domain->name,
665 strlen(domain->name)+1);
670 for (child = winbindd_children; child != NULL; child = child->next) {
671 /* Don't send message to internal childs. */
672 if (!child->domain || winbindd_internal_child(child)) {
673 continue;
676 /* Or internal domains (this should not be possible....) */
677 if (child->domain->internal) {
678 continue;
681 /* Each winbindd child should only process requests for one domain - make sure
682 we only set it online / offline for that domain. */
684 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
685 (unsigned int)child->pid, child->domain->name ));
687 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
688 MSG_WINBIND_ONLINE,
689 (uint8 *)child->domain->name,
690 strlen(child->domain->name)+1);
694 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
696 struct winbindd_domain *domain;
697 char *buf = NULL;
699 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
700 get_global_winbindd_state_offline() ?
701 "Offline":"Online")) == NULL) {
702 return NULL;
705 for (domain = domain_list(); domain; domain = domain->next) {
706 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
707 domain->name,
708 domain->online ?
709 "Online":"Offline")) == NULL) {
710 return NULL;
714 buf = talloc_asprintf_append_buffer(buf, "\n");
716 DEBUG(5,("collect_onlinestatus: %s", buf));
718 return buf;
721 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
722 void *private_data,
723 uint32_t msg_type,
724 struct server_id server_id,
725 DATA_BLOB *data)
727 TALLOC_CTX *mem_ctx;
728 const char *message;
729 struct server_id *sender;
731 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
733 if (!data->data) {
734 return;
737 sender = (struct server_id *)data->data;
739 mem_ctx = talloc_init("winbind_msg_onlinestatus");
740 if (mem_ctx == NULL) {
741 return;
744 message = collect_onlinestatus(mem_ctx);
745 if (message == NULL) {
746 talloc_destroy(mem_ctx);
747 return;
750 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
751 (const uint8 *)message, strlen(message) + 1);
753 talloc_destroy(mem_ctx);
756 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
757 void *private_data,
758 uint32_t msg_type,
759 struct server_id server_id,
760 DATA_BLOB *data)
762 struct winbindd_child *child;
764 DEBUG(10,("winbind_msg_dump_event_list received\n"));
766 dump_event_list(winbind_event_context());
768 for (child = winbindd_children; child != NULL; child = child->next) {
770 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
771 (unsigned int)child->pid));
773 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
774 MSG_DUMP_EVENT_LIST,
775 NULL, 0);
780 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
781 void *private_data,
782 uint32_t msg_type,
783 struct server_id server_id,
784 DATA_BLOB *data)
786 TALLOC_CTX *mem_ctx;
787 const char *message = NULL;
788 struct server_id *sender = NULL;
789 const char *domain = NULL;
790 char *s = NULL;
791 NTSTATUS status;
792 struct winbindd_domain *dom = NULL;
794 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
796 if (!data || !data->data) {
797 return;
800 if (data->length < sizeof(struct server_id)) {
801 return;
804 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
805 if (!mem_ctx) {
806 return;
809 sender = (struct server_id *)data->data;
810 if (data->length > sizeof(struct server_id)) {
811 domain = (const char *)data->data+sizeof(struct server_id);
814 if (domain) {
816 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
817 domain));
819 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
820 find_domain_from_name_noinit(domain));
821 if (!message) {
822 talloc_destroy(mem_ctx);
823 return;
826 messaging_send_buf(msg_ctx, *sender,
827 MSG_WINBIND_DUMP_DOMAIN_LIST,
828 (const uint8_t *)message, strlen(message) + 1);
830 talloc_destroy(mem_ctx);
832 return;
835 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
837 for (dom = domain_list(); dom; dom=dom->next) {
838 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
839 if (!message) {
840 talloc_destroy(mem_ctx);
841 return;
844 s = talloc_asprintf_append(s, "%s\n", message);
845 if (!s) {
846 talloc_destroy(mem_ctx);
847 return;
851 status = messaging_send_buf(msg_ctx, *sender,
852 MSG_WINBIND_DUMP_DOMAIN_LIST,
853 (uint8_t *)s, strlen(s) + 1);
854 if (!NT_STATUS_IS_OK(status)) {
855 DEBUG(0,("failed to send message: %s\n",
856 nt_errstr(status)));
859 talloc_destroy(mem_ctx);
862 static void account_lockout_policy_handler(struct event_context *ctx,
863 struct timed_event *te,
864 struct timeval now,
865 void *private_data)
867 struct winbindd_child *child =
868 (struct winbindd_child *)private_data;
869 TALLOC_CTX *mem_ctx = NULL;
870 struct winbindd_methods *methods;
871 struct samr_DomInfo12 lockout_policy;
872 NTSTATUS result;
874 DEBUG(10,("account_lockout_policy_handler called\n"));
876 TALLOC_FREE(child->lockout_policy_event);
878 if ( !winbindd_can_contact_domain( child->domain ) ) {
879 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
880 "do not have an incoming trust to domain %s\n",
881 child->domain->name));
883 return;
886 methods = child->domain->methods;
888 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
889 if (!mem_ctx) {
890 result = NT_STATUS_NO_MEMORY;
891 } else {
892 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
894 TALLOC_FREE(mem_ctx);
896 if (!NT_STATUS_IS_OK(result)) {
897 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
898 nt_errstr(result)));
901 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
902 timeval_current_ofs(3600, 0),
903 account_lockout_policy_handler,
904 child);
907 static time_t get_machine_password_timeout(void)
909 /* until we have gpo support use lp setting */
910 return lp_machine_password_timeout();
913 static bool calculate_next_machine_pwd_change(const char *domain,
914 struct timeval *t)
916 time_t pass_last_set_time;
917 time_t timeout;
918 time_t next_change;
919 struct timeval tv;
920 char *pw;
922 pw = secrets_fetch_machine_password(domain,
923 &pass_last_set_time,
924 NULL);
926 if (pw == NULL) {
927 DEBUG(0,("cannot fetch own machine password ????"));
928 return false;
931 SAFE_FREE(pw);
933 timeout = get_machine_password_timeout();
934 if (timeout == 0) {
935 DEBUG(10,("machine password never expires\n"));
936 return false;
939 tv.tv_sec = pass_last_set_time;
940 DEBUG(10, ("password last changed %s\n",
941 timeval_string(talloc_tos(), &tv, false)));
942 tv.tv_sec += timeout;
943 DEBUGADD(10, ("password valid until %s\n",
944 timeval_string(talloc_tos(), &tv, false)));
946 if (time(NULL) < (pass_last_set_time + timeout)) {
947 next_change = pass_last_set_time + timeout;
948 DEBUG(10,("machine password still valid until: %s\n",
949 http_timestring(talloc_tos(), next_change)));
950 *t = timeval_set(next_change, 0);
952 if (lp_clustering()) {
953 uint8_t randbuf;
955 * When having a cluster, we have several
956 * winbinds racing for the password change. In
957 * the machine_password_change_handler()
958 * function we check if someone else was
959 * faster when the event triggers. We add a
960 * 255-second random delay here, so that we
961 * don't run to change the password at the
962 * exact same moment.
964 generate_random_buffer(&randbuf, sizeof(randbuf));
965 DEBUG(10, ("adding %d seconds randomness\n",
966 (int)randbuf));
967 t->tv_sec += randbuf;
969 return true;
972 DEBUG(10,("machine password expired, needs immediate change\n"));
974 *t = timeval_zero();
976 return true;
979 static void machine_password_change_handler(struct event_context *ctx,
980 struct timed_event *te,
981 struct timeval now,
982 void *private_data)
984 struct winbindd_child *child =
985 (struct winbindd_child *)private_data;
986 struct rpc_pipe_client *netlogon_pipe = NULL;
987 TALLOC_CTX *frame;
988 NTSTATUS result;
989 struct timeval next_change;
991 DEBUG(10,("machine_password_change_handler called\n"));
993 TALLOC_FREE(child->machine_password_change_event);
995 if (!calculate_next_machine_pwd_change(child->domain->name,
996 &next_change)) {
997 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
998 return;
1001 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1002 timeval_string(talloc_tos(), &next_change, false)));
1004 if (!timeval_expired(&next_change)) {
1005 DEBUG(10, ("Someone else has already changed the pw\n"));
1006 goto done;
1009 if (!winbindd_can_contact_domain(child->domain)) {
1010 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1011 "do not have an incoming trust to domain %s\n",
1012 child->domain->name));
1013 return;
1016 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1017 if (!NT_STATUS_IS_OK(result)) {
1018 DEBUG(10,("machine_password_change_handler: "
1019 "failed to connect netlogon pipe: %s\n",
1020 nt_errstr(result)));
1021 return;
1024 frame = talloc_stackframe();
1026 result = trust_pw_find_change_and_store_it(netlogon_pipe,
1027 frame,
1028 child->domain->name);
1029 TALLOC_FREE(frame);
1031 DEBUG(10, ("machine_password_change_handler: "
1032 "trust_pw_find_change_and_store_it returned %s\n",
1033 nt_errstr(result)));
1035 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1036 DEBUG(3,("machine_password_change_handler: password set returned "
1037 "ACCESS_DENIED. Maybe the trust account "
1038 "password was changed and we didn't know it. "
1039 "Killing connections to domain %s\n",
1040 child->domain->name));
1041 TALLOC_FREE(child->domain->conn.netlogon_pipe);
1044 if (!calculate_next_machine_pwd_change(child->domain->name,
1045 &next_change)) {
1046 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1047 return;
1050 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1051 timeval_string(talloc_tos(), &next_change, false)));
1053 if (!NT_STATUS_IS_OK(result)) {
1054 struct timeval tmp;
1056 * In case of failure, give the DC a minute to recover
1058 tmp = timeval_current_ofs(60, 0);
1059 next_change = timeval_max(&next_change, &tmp);
1062 done:
1063 child->machine_password_change_event = event_add_timed(winbind_event_context(), NULL,
1064 next_change,
1065 machine_password_change_handler,
1066 child);
1069 /* Deal with a request to go offline. */
1071 static void child_msg_offline(struct messaging_context *msg,
1072 void *private_data,
1073 uint32_t msg_type,
1074 struct server_id server_id,
1075 DATA_BLOB *data)
1077 struct winbindd_domain *domain;
1078 struct winbindd_domain *primary_domain = NULL;
1079 const char *domainname = (const char *)data->data;
1081 if (data->data == NULL || data->length == 0) {
1082 return;
1085 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1087 if (!lp_winbind_offline_logon()) {
1088 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1089 return;
1092 primary_domain = find_our_domain();
1094 /* Mark the requested domain offline. */
1096 for (domain = domain_list(); domain; domain = domain->next) {
1097 if (domain->internal) {
1098 continue;
1100 if (strequal(domain->name, domainname)) {
1101 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1102 set_domain_offline(domain);
1103 /* we are in the trusted domain, set the primary domain
1104 * offline too */
1105 if (domain != primary_domain) {
1106 set_domain_offline(primary_domain);
1112 /* Deal with a request to go online. */
1114 static void child_msg_online(struct messaging_context *msg,
1115 void *private_data,
1116 uint32_t msg_type,
1117 struct server_id server_id,
1118 DATA_BLOB *data)
1120 struct winbindd_domain *domain;
1121 struct winbindd_domain *primary_domain = NULL;
1122 const char *domainname = (const char *)data->data;
1124 if (data->data == NULL || data->length == 0) {
1125 return;
1128 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1130 if (!lp_winbind_offline_logon()) {
1131 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1132 return;
1135 primary_domain = find_our_domain();
1137 /* Set our global state as online. */
1138 set_global_winbindd_state_online();
1140 /* Try and mark everything online - delete any negative cache entries
1141 to force a reconnect now. */
1143 for (domain = domain_list(); domain; domain = domain->next) {
1144 if (domain->internal) {
1145 continue;
1147 if (strequal(domain->name, domainname)) {
1148 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1149 winbindd_flush_negative_conn_cache(domain);
1150 set_domain_online_request(domain);
1152 /* we can be in trusted domain, which will contact primary domain
1153 * we have to bring primary domain online in trusted domain process
1154 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1155 * --> contact_domain = find_our_domain()
1156 * */
1157 if (domain != primary_domain) {
1158 winbindd_flush_negative_conn_cache(primary_domain);
1159 set_domain_online_request(primary_domain);
1165 static void child_msg_dump_event_list(struct messaging_context *msg,
1166 void *private_data,
1167 uint32_t msg_type,
1168 struct server_id server_id,
1169 DATA_BLOB *data)
1171 DEBUG(5,("child_msg_dump_event_list received\n"));
1173 dump_event_list(winbind_event_context());
1176 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1177 const char *logfilename)
1179 struct winbindd_domain *domain;
1180 struct winbindd_child *cl;
1181 NTSTATUS status;
1183 status = reinit_after_fork(
1184 winbind_messaging_context(),
1185 winbind_event_context(),
1186 true);
1187 if (!NT_STATUS_IS_OK(status)) {
1188 DEBUG(0,("reinit_after_fork() failed\n"));
1189 return status;
1192 close_conns_after_fork();
1194 if (!override_logfile && logfilename) {
1195 lp_set_logfile(logfilename);
1196 reopen_logs();
1199 if (!winbindd_setup_sig_term_handler(false))
1200 return NT_STATUS_NO_MEMORY;
1201 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1202 logfilename))
1203 return NT_STATUS_NO_MEMORY;
1205 /* Stop zombies in children */
1206 CatchChild();
1208 /* Don't handle the same messages as our parent. */
1209 messaging_deregister(winbind_messaging_context(),
1210 MSG_SMB_CONF_UPDATED, NULL);
1211 messaging_deregister(winbind_messaging_context(),
1212 MSG_SHUTDOWN, NULL);
1213 messaging_deregister(winbind_messaging_context(),
1214 MSG_WINBIND_OFFLINE, NULL);
1215 messaging_deregister(winbind_messaging_context(),
1216 MSG_WINBIND_ONLINE, NULL);
1217 messaging_deregister(winbind_messaging_context(),
1218 MSG_WINBIND_ONLINESTATUS, NULL);
1219 messaging_deregister(winbind_messaging_context(),
1220 MSG_DUMP_EVENT_LIST, NULL);
1221 messaging_deregister(winbind_messaging_context(),
1222 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1223 messaging_deregister(winbind_messaging_context(),
1224 MSG_DEBUG, NULL);
1226 messaging_deregister(winbind_messaging_context(),
1227 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1228 messaging_deregister(winbind_messaging_context(),
1229 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1231 /* We have destroyed all events in the winbindd_event_context
1232 * in reinit_after_fork(), so clean out all possible pending
1233 * event pointers. */
1235 /* Deal with check_online_events. */
1237 for (domain = domain_list(); domain; domain = domain->next) {
1238 TALLOC_FREE(domain->check_online_event);
1241 /* Ensure we're not handling a credential cache event inherited
1242 * from our parent. */
1244 ccache_remove_all_after_fork();
1246 /* Destroy all possible events in child list. */
1247 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1248 TALLOC_FREE(cl->lockout_policy_event);
1249 TALLOC_FREE(cl->machine_password_change_event);
1251 /* Children should never be able to send
1252 * each other messages, all messages must
1253 * go through the parent.
1255 cl->pid = (pid_t)0;
1258 * Close service sockets to all other children
1260 if ((cl != myself) && (cl->sock != -1)) {
1261 close(cl->sock);
1262 cl->sock = -1;
1266 * This is a little tricky, children must not
1267 * send an MSG_WINBIND_ONLINE message to idmap_child().
1268 * If we are in a child of our primary domain or
1269 * in the process created by fork_child_dc_connect(),
1270 * and the primary domain cannot go online,
1271 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1272 * periodically to idmap_child().
1274 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1275 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1276 * ---> init_dc_connection() ---> cm_open_connection --->
1277 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1278 * idmap_child(). Disallow children sending messages
1279 * to each other, all messages must go through the parent.
1281 cl = idmap_child();
1282 cl->pid = (pid_t)0;
1284 return NT_STATUS_OK;
1288 * In a child there will be only one domain, reference that here.
1290 static struct winbindd_domain *child_domain;
1292 struct winbindd_domain *wb_child_domain(void)
1294 return child_domain;
1297 static bool fork_domain_child(struct winbindd_child *child)
1299 int fdpair[2];
1300 struct winbindd_cli_state state;
1301 struct winbindd_request request;
1302 struct winbindd_response response;
1303 struct winbindd_domain *primary_domain = NULL;
1304 NTSTATUS status;
1305 ssize_t nwritten;
1307 if (child->domain) {
1308 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1309 child->domain->name));
1310 } else {
1311 DEBUG(10, ("fork_domain_child called without domain.\n"));
1314 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1315 DEBUG(0, ("Could not open child pipe: %s\n",
1316 strerror(errno)));
1317 return False;
1320 ZERO_STRUCT(state);
1321 state.pid = getpid();
1322 state.request = &request;
1323 state.response = &response;
1325 child->pid = fork();
1327 if (child->pid == -1) {
1328 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1329 return False;
1332 if (child->pid != 0) {
1333 /* Parent */
1334 ssize_t nread;
1336 close(fdpair[0]);
1338 nread = sys_read(fdpair[1], &status, sizeof(status));
1339 if (nread != sizeof(status)) {
1340 DEBUG(1, ("fork_domain_child: Could not read child status: "
1341 "nread=%d, error=%s\n", (int)nread,
1342 strerror(errno)));
1343 close(fdpair[1]);
1344 return false;
1346 if (!NT_STATUS_IS_OK(status)) {
1347 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1348 nt_errstr(status)));
1349 close(fdpair[1]);
1350 return false;
1353 child->next = child->prev = NULL;
1354 DLIST_ADD(winbindd_children, child);
1355 child->sock = fdpair[1];
1356 return True;
1359 /* Child */
1360 child_domain = child->domain;
1362 DEBUG(10, ("Child process %d\n", (int)getpid()));
1364 state.sock = fdpair[0];
1365 close(fdpair[1]);
1367 status = winbindd_reinit_after_fork(child, child->logfilename);
1369 nwritten = sys_write(state.sock, &status, sizeof(status));
1370 if (nwritten != sizeof(status)) {
1371 DEBUG(1, ("fork_domain_child: Could not write status: "
1372 "nwritten=%d, error=%s\n", (int)nwritten,
1373 strerror(errno)));
1374 _exit(0);
1376 if (!NT_STATUS_IS_OK(status)) {
1377 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1378 nt_errstr(status)));
1379 _exit(0);
1382 /* Handle online/offline messages. */
1383 messaging_register(winbind_messaging_context(), NULL,
1384 MSG_WINBIND_OFFLINE, child_msg_offline);
1385 messaging_register(winbind_messaging_context(), NULL,
1386 MSG_WINBIND_ONLINE, child_msg_online);
1387 messaging_register(winbind_messaging_context(), NULL,
1388 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1389 messaging_register(winbind_messaging_context(), NULL,
1390 MSG_DEBUG, debug_message);
1391 messaging_register(winbind_messaging_context(), NULL,
1392 MSG_WINBIND_IP_DROPPED,
1393 winbind_msg_ip_dropped);
1396 primary_domain = find_our_domain();
1398 if (primary_domain == NULL) {
1399 smb_panic("no primary domain found");
1402 /* It doesn't matter if we allow cache login,
1403 * try to bring domain online after fork. */
1404 if ( child->domain ) {
1405 child->domain->startup = True;
1406 child->domain->startup_time = time_mono(NULL);
1407 /* we can be in primary domain or in trusted domain
1408 * If we are in trusted domain, set the primary domain
1409 * in start-up mode */
1410 if (!(child->domain->internal)) {
1411 set_domain_online_request(child->domain);
1412 if (!(child->domain->primary)) {
1413 primary_domain->startup = True;
1414 primary_domain->startup_time = time_mono(NULL);
1415 set_domain_online_request(primary_domain);
1421 * We are in idmap child, make sure that we set the
1422 * check_online_event to bring primary domain online.
1424 if (child == idmap_child()) {
1425 set_domain_online_request(primary_domain);
1428 /* We might be in the idmap child...*/
1429 if (child->domain && !(child->domain->internal) &&
1430 lp_winbind_offline_logon()) {
1432 set_domain_online_request(child->domain);
1434 if (primary_domain && (primary_domain != child->domain)) {
1435 /* We need to talk to the primary
1436 * domain as well as the trusted
1437 * domain inside a trusted domain
1438 * child.
1439 * See the code in :
1440 * set_dc_type_and_flags_trustinfo()
1441 * for details.
1443 set_domain_online_request(primary_domain);
1446 child->lockout_policy_event = event_add_timed(
1447 winbind_event_context(), NULL, timeval_zero(),
1448 account_lockout_policy_handler,
1449 child);
1452 if (child->domain && child->domain->primary &&
1453 !USE_KERBEROS_KEYTAB &&
1454 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1456 struct timeval next_change;
1458 if (calculate_next_machine_pwd_change(child->domain->name,
1459 &next_change)) {
1460 child->machine_password_change_event = event_add_timed(
1461 winbind_event_context(), NULL, next_change,
1462 machine_password_change_handler,
1463 child);
1467 while (1) {
1469 int ret;
1470 struct pollfd *pfds;
1471 int num_pfds;
1472 int timeout;
1473 struct timeval t;
1474 struct timeval *tp;
1475 TALLOC_CTX *frame = talloc_stackframe();
1476 struct iovec iov[2];
1477 int iov_count;
1479 if (run_events_poll(winbind_event_context(), 0, NULL, 0)) {
1480 TALLOC_FREE(frame);
1481 continue;
1484 if (child->domain && child->domain->startup &&
1485 (time_mono(NULL) > child->domain->startup_time + 30)) {
1486 /* No longer in "startup" mode. */
1487 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1488 child->domain->name ));
1489 child->domain->startup = False;
1492 pfds = talloc_zero(talloc_tos(), struct pollfd);
1493 if (pfds == NULL) {
1494 DEBUG(1, ("talloc failed\n"));
1495 _exit(1);
1498 pfds->fd = state.sock;
1499 pfds->events = POLLIN|POLLHUP;
1500 num_pfds = 1;
1502 timeout = INT_MAX;
1504 if (!event_add_to_poll_args(
1505 winbind_event_context(), talloc_tos(),
1506 &pfds, &num_pfds, &timeout)) {
1507 DEBUG(1, ("event_add_to_poll_args failed\n"));
1508 _exit(1);
1510 tp = get_timed_events_timeout(winbind_event_context(), &t);
1511 if (tp) {
1512 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1513 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1516 ret = poll(pfds, num_pfds, timeout);
1518 if (run_events_poll(winbind_event_context(), ret,
1519 pfds, num_pfds)) {
1520 /* We got a signal - continue. */
1521 TALLOC_FREE(frame);
1522 continue;
1525 TALLOC_FREE(pfds);
1527 if (ret == 0) {
1528 DEBUG(11,("nothing is ready yet, continue\n"));
1529 TALLOC_FREE(frame);
1530 continue;
1533 if (ret == -1 && errno == EINTR) {
1534 /* We got a signal - continue. */
1535 TALLOC_FREE(frame);
1536 continue;
1539 if (ret == -1 && errno != EINTR) {
1540 DEBUG(0,("poll error occured\n"));
1541 TALLOC_FREE(frame);
1542 perror("poll");
1543 _exit(1);
1546 /* fetch a request from the main daemon */
1547 status = child_read_request(&state);
1549 if (!NT_STATUS_IS_OK(status)) {
1550 /* we lost contact with our parent */
1551 _exit(0);
1554 DEBUG(4,("child daemon request %d\n", (int)state.request->cmd));
1556 ZERO_STRUCTP(state.response);
1557 state.request->null_term = '\0';
1558 state.mem_ctx = frame;
1559 child_process_request(child, &state);
1561 DEBUG(4, ("Finished processing child request %d\n",
1562 (int)state.request->cmd));
1564 SAFE_FREE(state.request->extra_data.data);
1566 iov[0].iov_base = (void *)state.response;
1567 iov[0].iov_len = sizeof(struct winbindd_response);
1568 iov_count = 1;
1570 if (state.response->length > sizeof(struct winbindd_response)) {
1571 iov[1].iov_base =
1572 (void *)state.response->extra_data.data;
1573 iov[1].iov_len = state.response->length-iov[0].iov_len;
1574 iov_count = 2;
1577 DEBUG(10, ("Writing %d bytes to parent\n",
1578 (int)state.response->length));
1580 if (write_data_iov(state.sock, iov, iov_count) !=
1581 state.response->length) {
1582 DEBUG(0, ("Could not write result\n"));
1583 exit(1);
1585 TALLOC_FREE(frame);
1589 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1590 void *private_data,
1591 uint32_t msg_type,
1592 struct server_id server_id,
1593 DATA_BLOB *data)
1595 struct winbindd_child *child;
1597 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1598 server_id, data);
1601 for (child = winbindd_children; child != NULL; child = child->next) {
1602 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1603 msg_type, data->data, data->length);