s3:winbindd: s/struct timed_event/struct tevent_timer
[Samba/gebeck_regimport.git] / source3 / winbindd / winbindd_dual.c
blobb4ef0bd79ebaaae80f28e9e1bc317da50e5bb8c3
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 tevent_context *ctx,
863 struct tevent_timer *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 tevent_context *ctx,
980 struct tevent_timer *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 /* We have destroyed all events in the winbindd_event_context
1227 * in reinit_after_fork(), so clean out all possible pending
1228 * event pointers. */
1230 /* Deal with check_online_events. */
1232 for (domain = domain_list(); domain; domain = domain->next) {
1233 TALLOC_FREE(domain->check_online_event);
1236 /* Ensure we're not handling a credential cache event inherited
1237 * from our parent. */
1239 ccache_remove_all_after_fork();
1241 /* Destroy all possible events in child list. */
1242 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1243 TALLOC_FREE(cl->lockout_policy_event);
1244 TALLOC_FREE(cl->machine_password_change_event);
1246 /* Children should never be able to send
1247 * each other messages, all messages must
1248 * go through the parent.
1250 cl->pid = (pid_t)0;
1253 * Close service sockets to all other children
1255 if ((cl != myself) && (cl->sock != -1)) {
1256 close(cl->sock);
1257 cl->sock = -1;
1261 * This is a little tricky, children must not
1262 * send an MSG_WINBIND_ONLINE message to idmap_child().
1263 * If we are in a child of our primary domain or
1264 * in the process created by fork_child_dc_connect(),
1265 * and the primary domain cannot go online,
1266 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1267 * periodically to idmap_child().
1269 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1270 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1271 * ---> init_dc_connection() ---> cm_open_connection --->
1272 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1273 * idmap_child(). Disallow children sending messages
1274 * to each other, all messages must go through the parent.
1276 cl = idmap_child();
1277 cl->pid = (pid_t)0;
1279 return NT_STATUS_OK;
1283 * In a child there will be only one domain, reference that here.
1285 static struct winbindd_domain *child_domain;
1287 struct winbindd_domain *wb_child_domain(void)
1289 return child_domain;
1292 struct child_handler_state {
1293 struct winbindd_child *child;
1294 struct winbindd_cli_state cli;
1297 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1298 uint16_t flags, void *private_data)
1300 struct child_handler_state *state =
1301 (struct child_handler_state *)private_data;
1302 NTSTATUS status;
1303 struct iovec iov[2];
1304 int iov_count;
1306 /* fetch a request from the main daemon */
1307 status = child_read_request(&state->cli);
1309 if (!NT_STATUS_IS_OK(status)) {
1310 /* we lost contact with our parent */
1311 _exit(0);
1314 DEBUG(4,("child daemon request %d\n",
1315 (int)state->cli.request->cmd));
1317 ZERO_STRUCTP(state->cli.response);
1318 state->cli.request->null_term = '\0';
1319 state->cli.mem_ctx = talloc_tos();
1320 child_process_request(state->child, &state->cli);
1322 DEBUG(4, ("Finished processing child request %d\n",
1323 (int)state->cli.request->cmd));
1325 SAFE_FREE(state->cli.request->extra_data.data);
1327 iov[0].iov_base = (void *)state->cli.response;
1328 iov[0].iov_len = sizeof(struct winbindd_response);
1329 iov_count = 1;
1331 if (state->cli.response->length >
1332 sizeof(struct winbindd_response)) {
1333 iov[1].iov_base =
1334 (void *)state->cli.response->extra_data.data;
1335 iov[1].iov_len = state->cli.response->length-iov[0].iov_len;
1336 iov_count = 2;
1339 DEBUG(10, ("Writing %d bytes to parent\n",
1340 (int)state->cli.response->length));
1342 if (write_data_iov(state->cli.sock, iov, iov_count) !=
1343 state->cli.response->length) {
1344 DEBUG(0, ("Could not write result\n"));
1345 exit(1);
1349 static bool fork_domain_child(struct winbindd_child *child)
1351 int fdpair[2];
1352 struct child_handler_state state;
1353 struct winbindd_request request;
1354 struct winbindd_response response;
1355 struct winbindd_domain *primary_domain = NULL;
1356 NTSTATUS status;
1357 ssize_t nwritten;
1358 struct tevent_fd *fde;
1360 if (child->domain) {
1361 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1362 child->domain->name));
1363 } else {
1364 DEBUG(10, ("fork_domain_child called without domain.\n"));
1367 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1368 DEBUG(0, ("Could not open child pipe: %s\n",
1369 strerror(errno)));
1370 return False;
1373 ZERO_STRUCT(state);
1374 state.child = child;
1375 state.cli.pid = getpid();
1376 state.cli.request = &request;
1377 state.cli.response = &response;
1379 child->pid = fork();
1381 if (child->pid == -1) {
1382 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1383 return False;
1386 if (child->pid != 0) {
1387 /* Parent */
1388 ssize_t nread;
1390 close(fdpair[0]);
1392 nread = sys_read(fdpair[1], &status, sizeof(status));
1393 if (nread != sizeof(status)) {
1394 DEBUG(1, ("fork_domain_child: Could not read child status: "
1395 "nread=%d, error=%s\n", (int)nread,
1396 strerror(errno)));
1397 close(fdpair[1]);
1398 return false;
1400 if (!NT_STATUS_IS_OK(status)) {
1401 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1402 nt_errstr(status)));
1403 close(fdpair[1]);
1404 return false;
1407 child->next = child->prev = NULL;
1408 DLIST_ADD(winbindd_children, child);
1409 child->sock = fdpair[1];
1410 return True;
1413 /* Child */
1414 child_domain = child->domain;
1416 DEBUG(10, ("Child process %d\n", (int)getpid()));
1418 state.cli.sock = fdpair[0];
1419 close(fdpair[1]);
1421 status = winbindd_reinit_after_fork(child, child->logfilename);
1423 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1424 if (nwritten != sizeof(status)) {
1425 DEBUG(1, ("fork_domain_child: Could not write status: "
1426 "nwritten=%d, error=%s\n", (int)nwritten,
1427 strerror(errno)));
1428 _exit(0);
1430 if (!NT_STATUS_IS_OK(status)) {
1431 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1432 nt_errstr(status)));
1433 _exit(0);
1436 /* Handle online/offline messages. */
1437 messaging_register(winbind_messaging_context(), NULL,
1438 MSG_WINBIND_OFFLINE, child_msg_offline);
1439 messaging_register(winbind_messaging_context(), NULL,
1440 MSG_WINBIND_ONLINE, child_msg_online);
1441 messaging_register(winbind_messaging_context(), NULL,
1442 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1443 messaging_register(winbind_messaging_context(), NULL,
1444 MSG_DEBUG, debug_message);
1445 messaging_register(winbind_messaging_context(), NULL,
1446 MSG_WINBIND_IP_DROPPED,
1447 winbind_msg_ip_dropped);
1450 primary_domain = find_our_domain();
1452 if (primary_domain == NULL) {
1453 smb_panic("no primary domain found");
1456 /* It doesn't matter if we allow cache login,
1457 * try to bring domain online after fork. */
1458 if ( child->domain ) {
1459 child->domain->startup = True;
1460 child->domain->startup_time = time_mono(NULL);
1461 /* we can be in primary domain or in trusted domain
1462 * If we are in trusted domain, set the primary domain
1463 * in start-up mode */
1464 if (!(child->domain->internal)) {
1465 set_domain_online_request(child->domain);
1466 if (!(child->domain->primary)) {
1467 primary_domain->startup = True;
1468 primary_domain->startup_time = time_mono(NULL);
1469 set_domain_online_request(primary_domain);
1475 * We are in idmap child, make sure that we set the
1476 * check_online_event to bring primary domain online.
1478 if (child == idmap_child()) {
1479 set_domain_online_request(primary_domain);
1482 /* We might be in the idmap child...*/
1483 if (child->domain && !(child->domain->internal) &&
1484 lp_winbind_offline_logon()) {
1486 set_domain_online_request(child->domain);
1488 if (primary_domain && (primary_domain != child->domain)) {
1489 /* We need to talk to the primary
1490 * domain as well as the trusted
1491 * domain inside a trusted domain
1492 * child.
1493 * See the code in :
1494 * set_dc_type_and_flags_trustinfo()
1495 * for details.
1497 set_domain_online_request(primary_domain);
1500 child->lockout_policy_event = event_add_timed(
1501 winbind_event_context(), NULL, timeval_zero(),
1502 account_lockout_policy_handler,
1503 child);
1506 if (child->domain && child->domain->primary &&
1507 !USE_KERBEROS_KEYTAB &&
1508 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1510 struct timeval next_change;
1512 if (calculate_next_machine_pwd_change(child->domain->name,
1513 &next_change)) {
1514 child->machine_password_change_event = event_add_timed(
1515 winbind_event_context(), NULL, next_change,
1516 machine_password_change_handler,
1517 child);
1521 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1522 TEVENT_FD_READ, child_handler, &state);
1523 if (fde == NULL) {
1524 DEBUG(1, ("tevent_add_fd failed\n"));
1525 _exit(1);
1528 while (1) {
1530 int ret;
1531 TALLOC_CTX *frame = talloc_stackframe();
1533 ret = tevent_loop_once(winbind_event_context());
1534 if (ret != 0) {
1535 DEBUG(1, ("tevent_loop_once failed: %s\n",
1536 strerror(errno)));
1537 _exit(1);
1540 if (child->domain && child->domain->startup &&
1541 (time_mono(NULL) > child->domain->startup_time + 30)) {
1542 /* No longer in "startup" mode. */
1543 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1544 child->domain->name ));
1545 child->domain->startup = False;
1548 TALLOC_FREE(frame);
1552 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1553 void *private_data,
1554 uint32_t msg_type,
1555 struct server_id server_id,
1556 DATA_BLOB *data)
1558 struct winbindd_child *child;
1560 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1561 server_id, data);
1564 for (child = winbindd_children; child != NULL; child = child->next) {
1565 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1566 msg_type, data->data, data->length);