ctdb-logging: Separate out syslog and file logging backends
[Samba.git] / source3 / winbindd / winbindd_dual.c
blobf71d111c7d0b86f8650353e91a3aed19c4ed0548
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;
47 static struct winbindd_child *winbindd_children = NULL;
49 /* Read some data from a client connection */
51 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
53 NTSTATUS status;
55 status = read_data(sock, (char *)wreq, sizeof(*wreq));
56 if (!NT_STATUS_IS_OK(status)) {
57 DEBUG(3, ("child_read_request: read_data failed: %s\n",
58 nt_errstr(status)));
59 return status;
62 if (wreq->extra_len == 0) {
63 wreq->extra_data.data = NULL;
64 return NT_STATUS_OK;
67 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
69 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
70 if (wreq->extra_data.data == NULL) {
71 DEBUG(0, ("malloc failed\n"));
72 return NT_STATUS_NO_MEMORY;
75 /* Ensure null termination */
76 wreq->extra_data.data[wreq->extra_len] = '\0';
78 status = read_data(sock, wreq->extra_data.data, wreq->extra_len);
79 if (!NT_STATUS_IS_OK(status)) {
80 DEBUG(0, ("Could not read extra data: %s\n",
81 nt_errstr(status)));
83 return status;
86 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
88 struct iovec iov[2];
89 int iov_count;
91 iov[0].iov_base = (void *)wrsp;
92 iov[0].iov_len = sizeof(struct winbindd_response);
93 iov_count = 1;
95 if (wrsp->length > sizeof(struct winbindd_response)) {
96 iov[1].iov_base = (void *)wrsp->extra_data.data;
97 iov[1].iov_len = wrsp->length-iov[0].iov_len;
98 iov_count = 2;
101 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
103 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
104 DEBUG(0, ("Could not write result\n"));
105 return NT_STATUS_INVALID_HANDLE;
108 return NT_STATUS_OK;
112 * Do winbind child async request. This is not simply wb_simple_trans. We have
113 * to do the queueing ourselves because while a request is queued, the child
114 * might have crashed, and we have to re-fork it in the _trigger function.
117 struct wb_child_request_state {
118 struct tevent_context *ev;
119 struct winbindd_child *child;
120 struct winbindd_request *request;
121 struct winbindd_response *response;
124 static bool fork_domain_child(struct winbindd_child *child);
126 static void wb_child_request_trigger(struct tevent_req *req,
127 void *private_data);
128 static void wb_child_request_done(struct tevent_req *subreq);
130 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
131 struct tevent_context *ev,
132 struct winbindd_child *child,
133 struct winbindd_request *request)
135 struct tevent_req *req;
136 struct wb_child_request_state *state;
138 req = tevent_req_create(mem_ctx, &state,
139 struct wb_child_request_state);
140 if (req == NULL) {
141 return NULL;
144 state->ev = ev;
145 state->child = child;
146 state->request = request;
148 if (!tevent_queue_add(child->queue, ev, req,
149 wb_child_request_trigger, NULL)) {
150 tevent_req_oom(req);
151 return tevent_req_post(req, ev);
153 return req;
156 static void wb_child_request_trigger(struct tevent_req *req,
157 void *private_data)
159 struct wb_child_request_state *state = tevent_req_data(
160 req, struct wb_child_request_state);
161 struct tevent_req *subreq;
163 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
164 tevent_req_error(req, errno);
165 return;
168 subreq = wb_simple_trans_send(state, winbind_event_context(), NULL,
169 state->child->sock, state->request);
170 if (tevent_req_nomem(subreq, req)) {
171 return;
173 tevent_req_set_callback(subreq, wb_child_request_done, req);
174 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
177 static void wb_child_request_done(struct tevent_req *subreq)
179 struct tevent_req *req = tevent_req_callback_data(
180 subreq, struct tevent_req);
181 struct wb_child_request_state *state = tevent_req_data(
182 req, struct wb_child_request_state);
183 int ret, err;
185 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
186 TALLOC_FREE(subreq);
187 if (ret == -1) {
189 * The basic parent/child communication broke, close
190 * our socket
192 close(state->child->sock);
193 state->child->sock = -1;
194 DLIST_REMOVE(winbindd_children, state->child);
195 tevent_req_error(req, err);
196 return;
198 tevent_req_done(req);
201 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
202 struct winbindd_response **presponse, int *err)
204 struct wb_child_request_state *state = tevent_req_data(
205 req, struct wb_child_request_state);
207 if (tevent_req_is_unix_error(req, err)) {
208 return -1;
210 *presponse = talloc_move(mem_ctx, &state->response);
211 return 0;
214 static bool winbindd_child_busy(struct winbindd_child *child)
216 return tevent_queue_length(child->queue) > 0;
219 static struct winbindd_child *find_idle_child(struct winbindd_domain *domain)
221 int i;
223 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
224 if (!winbindd_child_busy(&domain->children[i])) {
225 return &domain->children[i];
229 return NULL;
232 struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
234 struct winbindd_child *result;
236 result = find_idle_child(domain);
237 if (result != NULL) {
238 return result;
240 return &domain->children[rand() % lp_winbind_max_domain_connections()];
243 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
245 struct winbindd_child *child;
247 child = choose_domain_child(domain);
248 return child->binding_handle;
251 struct wb_domain_request_state {
252 struct tevent_context *ev;
253 struct winbindd_domain *domain;
254 struct winbindd_child *child;
255 struct winbindd_request *request;
256 struct winbindd_request *init_req;
257 struct winbindd_response *response;
260 static void wb_domain_request_gotdc(struct tevent_req *subreq);
261 static void wb_domain_request_initialized(struct tevent_req *subreq);
262 static void wb_domain_request_done(struct tevent_req *subreq);
264 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
265 struct tevent_context *ev,
266 struct winbindd_domain *domain,
267 struct winbindd_request *request)
269 struct tevent_req *req, *subreq;
270 struct wb_domain_request_state *state;
272 req = tevent_req_create(mem_ctx, &state,
273 struct wb_domain_request_state);
274 if (req == NULL) {
275 return NULL;
278 state->child = choose_domain_child(domain);
280 if (domain->initialized) {
281 subreq = wb_child_request_send(state, ev, state->child,
282 request);
283 if (tevent_req_nomem(subreq, req)) {
284 return tevent_req_post(req, ev);
286 tevent_req_set_callback(subreq, wb_domain_request_done, req);
287 return req;
290 state->domain = domain;
291 state->ev = ev;
292 state->request = request;
294 state->init_req = talloc_zero(state, struct winbindd_request);
295 if (tevent_req_nomem(state->init_req, req)) {
296 return tevent_req_post(req, ev);
299 if (IS_DC || domain->primary || domain->internal) {
300 /* The primary domain has to find the DC name itself */
301 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
302 fstrcpy(state->init_req->domain_name, domain->name);
303 state->init_req->data.init_conn.is_primary = domain->primary;
304 fstrcpy(state->init_req->data.init_conn.dcname, "");
306 subreq = wb_child_request_send(state, ev, state->child,
307 state->init_req);
308 if (tevent_req_nomem(subreq, req)) {
309 return tevent_req_post(req, ev);
311 tevent_req_set_callback(subreq, wb_domain_request_initialized,
312 req);
313 return req;
317 * Ask our DC for a DC name
319 domain = find_our_domain();
321 /* This is *not* the primary domain, let's ask our DC about a DC
322 * name */
324 state->init_req->cmd = WINBINDD_GETDCNAME;
325 fstrcpy(state->init_req->domain_name, domain->name);
327 subreq = wb_child_request_send(state, ev, state->child, request);
328 if (tevent_req_nomem(subreq, req)) {
329 return tevent_req_post(req, ev);
331 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
332 return req;
335 static void wb_domain_request_gotdc(struct tevent_req *subreq)
337 struct tevent_req *req = tevent_req_callback_data(
338 subreq, struct tevent_req);
339 struct wb_domain_request_state *state = tevent_req_data(
340 req, struct wb_domain_request_state);
341 struct winbindd_response *response;
342 int ret, err;
344 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
345 TALLOC_FREE(subreq);
346 if (ret == -1) {
347 tevent_req_error(req, err);
348 return;
350 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
351 fstrcpy(state->init_req->domain_name, state->domain->name);
352 state->init_req->data.init_conn.is_primary = False;
353 fstrcpy(state->init_req->data.init_conn.dcname,
354 response->data.dc_name);
356 TALLOC_FREE(response);
358 subreq = wb_child_request_send(state, state->ev, state->child,
359 state->init_req);
360 if (tevent_req_nomem(subreq, req)) {
361 return;
363 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
366 static void wb_domain_request_initialized(struct tevent_req *subreq)
368 struct tevent_req *req = tevent_req_callback_data(
369 subreq, struct tevent_req);
370 struct wb_domain_request_state *state = tevent_req_data(
371 req, struct wb_domain_request_state);
372 struct winbindd_response *response;
373 int ret, err;
375 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
376 TALLOC_FREE(subreq);
377 if (ret == -1) {
378 tevent_req_error(req, err);
379 return;
382 if (!string_to_sid(&state->domain->sid,
383 response->data.domain_info.sid)) {
384 DEBUG(1,("init_child_recv: Could not convert sid %s "
385 "from string\n", response->data.domain_info.sid));
386 tevent_req_error(req, EINVAL);
387 return;
390 talloc_free(state->domain->name);
391 state->domain->name = talloc_strdup(state->domain,
392 response->data.domain_info.name);
393 if (state->domain->name == NULL) {
394 tevent_req_error(req, ENOMEM);
395 return;
398 if (response->data.domain_info.alt_name[0] != '\0') {
399 talloc_free(state->domain->alt_name);
401 state->domain->alt_name = talloc_strdup(state->domain,
402 response->data.domain_info.alt_name);
403 if (state->domain->alt_name == NULL) {
404 tevent_req_error(req, ENOMEM);
405 return;
409 state->domain->native_mode = response->data.domain_info.native_mode;
410 state->domain->active_directory =
411 response->data.domain_info.active_directory;
412 state->domain->initialized = true;
414 TALLOC_FREE(response);
416 subreq = wb_child_request_send(state, state->ev, state->child,
417 state->request);
418 if (tevent_req_nomem(subreq, req)) {
419 return;
421 tevent_req_set_callback(subreq, wb_domain_request_done, req);
424 static void wb_domain_request_done(struct tevent_req *subreq)
426 struct tevent_req *req = tevent_req_callback_data(
427 subreq, struct tevent_req);
428 struct wb_domain_request_state *state = tevent_req_data(
429 req, struct wb_domain_request_state);
430 int ret, err;
432 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
433 &err);
434 TALLOC_FREE(subreq);
435 if (ret == -1) {
436 tevent_req_error(req, err);
437 return;
439 tevent_req_done(req);
442 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
443 struct winbindd_response **presponse, int *err)
445 struct wb_domain_request_state *state = tevent_req_data(
446 req, struct wb_domain_request_state);
448 if (tevent_req_is_unix_error(req, err)) {
449 return -1;
451 *presponse = talloc_move(mem_ctx, &state->response);
452 return 0;
455 static void child_process_request(struct winbindd_child *child,
456 struct winbindd_cli_state *state)
458 struct winbindd_domain *domain = child->domain;
459 const struct winbindd_child_dispatch_table *table = child->table;
461 /* Free response data - we may be interrupted and receive another
462 command before being able to send this data off. */
464 state->response->result = WINBINDD_ERROR;
465 state->response->length = sizeof(struct winbindd_response);
467 /* as all requests in the child are sync, we can use talloc_tos() */
468 state->mem_ctx = talloc_tos();
470 /* Process command */
472 for (; table->name; table++) {
473 if (state->request->cmd == table->struct_cmd) {
474 DEBUG(10,("child_process_request: request fn %s\n",
475 table->name));
476 state->response->result = table->struct_fn(domain, state);
477 return;
481 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
482 (int)state->request->cmd));
483 state->response->result = WINBINDD_ERROR;
486 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
487 const struct winbindd_child_dispatch_table *table,
488 const char *logprefix,
489 const char *logname)
491 if (logprefix && logname) {
492 char *logbase = NULL;
494 if (*lp_logfile(talloc_tos())) {
495 char *end = NULL;
497 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
498 smb_panic("Internal error: asprintf failed");
501 if ((end = strrchr_m(logbase, '/'))) {
502 *end = '\0';
504 } else {
505 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
506 smb_panic("Internal error: asprintf failed");
510 if (asprintf(&child->logfilename, "%s/%s-%s",
511 logbase, logprefix, logname) < 0) {
512 SAFE_FREE(logbase);
513 smb_panic("Internal error: asprintf failed");
516 SAFE_FREE(logbase);
517 } else {
518 smb_panic("Internal error: logprefix == NULL && "
519 "logname == NULL");
522 child->sock = -1;
523 child->domain = domain;
524 child->table = table;
525 child->queue = tevent_queue_create(NULL, "winbind_child");
526 SMB_ASSERT(child->queue != NULL);
527 child->binding_handle = wbint_binding_handle(NULL, domain, child);
528 SMB_ASSERT(child->binding_handle != NULL);
531 void winbind_child_died(pid_t pid)
533 struct winbindd_child *child;
535 for (child = winbindd_children; child != NULL; child = child->next) {
536 if (child->pid == pid) {
537 break;
541 if (child == NULL) {
542 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
543 return;
546 /* This will be re-added in fork_domain_child() */
548 DLIST_REMOVE(winbindd_children, child);
549 child->pid = 0;
551 if (child->sock != -1) {
552 close(child->sock);
553 child->sock = -1;
557 /* Ensure any negative cache entries with the netbios or realm names are removed. */
559 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
561 flush_negative_conn_cache_for_domain(domain->name);
562 if (domain->alt_name != NULL) {
563 flush_negative_conn_cache_for_domain(domain->alt_name);
568 * Parent winbindd process sets its own debug level first and then
569 * sends a message to all the winbindd children to adjust their debug
570 * level to that of parents.
573 void winbind_msg_debug(struct messaging_context *msg_ctx,
574 void *private_data,
575 uint32_t msg_type,
576 struct server_id server_id,
577 DATA_BLOB *data)
579 struct winbindd_child *child;
581 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
583 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
585 for (child = winbindd_children; child != NULL; child = child->next) {
587 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
588 (unsigned int)child->pid));
590 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
591 MSG_DEBUG,
592 data->data,
593 strlen((char *) data->data) + 1);
597 /* Set our domains as offline and forward the offline message to our children. */
599 void winbind_msg_offline(struct messaging_context *msg_ctx,
600 void *private_data,
601 uint32_t msg_type,
602 struct server_id server_id,
603 DATA_BLOB *data)
605 struct winbindd_child *child;
606 struct winbindd_domain *domain;
608 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
610 if (!lp_winbind_offline_logon()) {
611 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
612 return;
615 /* Set our global state as offline. */
616 if (!set_global_winbindd_state_offline()) {
617 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
618 return;
621 /* Set all our domains as offline. */
622 for (domain = domain_list(); domain; domain = domain->next) {
623 if (domain->internal) {
624 continue;
626 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
627 set_domain_offline(domain);
630 for (child = winbindd_children; child != NULL; child = child->next) {
631 /* Don't send message to internal children. We've already
632 done so above. */
633 if (!child->domain || winbindd_internal_child(child)) {
634 continue;
637 /* Or internal domains (this should not be possible....) */
638 if (child->domain->internal) {
639 continue;
642 /* Each winbindd child should only process requests for one domain - make sure
643 we only set it online / offline for that domain. */
645 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
646 (unsigned int)child->pid, child->domain->name ));
648 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
649 MSG_WINBIND_OFFLINE,
650 (const uint8_t *)child->domain->name,
651 strlen(child->domain->name)+1);
655 /* Set our domains as online and forward the online message to our children. */
657 void winbind_msg_online(struct messaging_context *msg_ctx,
658 void *private_data,
659 uint32_t msg_type,
660 struct server_id server_id,
661 DATA_BLOB *data)
663 struct winbindd_child *child;
664 struct winbindd_domain *domain;
666 DEBUG(10,("winbind_msg_online: got online message.\n"));
668 if (!lp_winbind_offline_logon()) {
669 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
670 return;
673 /* Set our global state as online. */
674 set_global_winbindd_state_online();
676 smb_nscd_flush_user_cache();
677 smb_nscd_flush_group_cache();
679 /* Set all our domains as online. */
680 for (domain = domain_list(); domain; domain = domain->next) {
681 if (domain->internal) {
682 continue;
684 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
686 winbindd_flush_negative_conn_cache(domain);
687 set_domain_online_request(domain);
689 /* Send an online message to the idmap child when our
690 primary domain comes back online */
692 if ( domain->primary ) {
693 struct winbindd_child *idmap = idmap_child();
695 if ( idmap->pid != 0 ) {
696 messaging_send_buf(msg_ctx,
697 pid_to_procid(idmap->pid),
698 MSG_WINBIND_ONLINE,
699 (const uint8_t *)domain->name,
700 strlen(domain->name)+1);
705 for (child = winbindd_children; child != NULL; child = child->next) {
706 /* Don't send message to internal childs. */
707 if (!child->domain || winbindd_internal_child(child)) {
708 continue;
711 /* Or internal domains (this should not be possible....) */
712 if (child->domain->internal) {
713 continue;
716 /* Each winbindd child should only process requests for one domain - make sure
717 we only set it online / offline for that domain. */
719 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
720 (unsigned int)child->pid, child->domain->name ));
722 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
723 MSG_WINBIND_ONLINE,
724 (const uint8_t *)child->domain->name,
725 strlen(child->domain->name)+1);
729 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
731 struct winbindd_domain *domain;
732 char *buf = NULL;
734 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
735 get_global_winbindd_state_offline() ?
736 "Offline":"Online")) == NULL) {
737 return NULL;
740 for (domain = domain_list(); domain; domain = domain->next) {
741 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
742 domain->name,
743 domain->online ?
744 "Online":"Offline")) == NULL) {
745 return NULL;
749 buf = talloc_asprintf_append_buffer(buf, "\n");
751 DEBUG(5,("collect_onlinestatus: %s", buf));
753 return buf;
756 void winbind_msg_onlinestatus(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 TALLOC_CTX *mem_ctx;
763 const char *message;
764 struct server_id *sender;
766 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
768 if (!data->data) {
769 return;
772 sender = (struct server_id *)data->data;
774 mem_ctx = talloc_init("winbind_msg_onlinestatus");
775 if (mem_ctx == NULL) {
776 return;
779 message = collect_onlinestatus(mem_ctx);
780 if (message == NULL) {
781 talloc_destroy(mem_ctx);
782 return;
785 messaging_send_buf(msg_ctx, *sender, MSG_WINBIND_ONLINESTATUS,
786 (const uint8 *)message, strlen(message) + 1);
788 talloc_destroy(mem_ctx);
791 void winbind_msg_dump_event_list(struct messaging_context *msg_ctx,
792 void *private_data,
793 uint32_t msg_type,
794 struct server_id server_id,
795 DATA_BLOB *data)
797 struct winbindd_child *child;
799 DEBUG(10,("winbind_msg_dump_event_list received\n"));
801 dump_event_list(winbind_event_context());
803 for (child = winbindd_children; child != NULL; child = child->next) {
805 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
806 (unsigned int)child->pid));
808 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
809 MSG_DUMP_EVENT_LIST,
810 NULL, 0);
815 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
816 void *private_data,
817 uint32_t msg_type,
818 struct server_id server_id,
819 DATA_BLOB *data)
821 TALLOC_CTX *mem_ctx;
822 const char *message = NULL;
823 struct server_id *sender = NULL;
824 const char *domain = NULL;
825 char *s = NULL;
826 NTSTATUS status;
827 struct winbindd_domain *dom = NULL;
829 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
831 if (!data || !data->data) {
832 return;
835 if (data->length < sizeof(struct server_id)) {
836 return;
839 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
840 if (!mem_ctx) {
841 return;
844 sender = (struct server_id *)data->data;
845 if (data->length > sizeof(struct server_id)) {
846 domain = (const char *)data->data+sizeof(struct server_id);
849 if (domain) {
851 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
852 domain));
854 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
855 find_domain_from_name_noinit(domain));
856 if (!message) {
857 talloc_destroy(mem_ctx);
858 return;
861 messaging_send_buf(msg_ctx, *sender,
862 MSG_WINBIND_DUMP_DOMAIN_LIST,
863 (const uint8_t *)message, strlen(message) + 1);
865 talloc_destroy(mem_ctx);
867 return;
870 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
872 for (dom = domain_list(); dom; dom=dom->next) {
873 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
874 if (!message) {
875 talloc_destroy(mem_ctx);
876 return;
879 s = talloc_asprintf_append(s, "%s\n", message);
880 if (!s) {
881 talloc_destroy(mem_ctx);
882 return;
886 status = messaging_send_buf(msg_ctx, *sender,
887 MSG_WINBIND_DUMP_DOMAIN_LIST,
888 (uint8_t *)s, strlen(s) + 1);
889 if (!NT_STATUS_IS_OK(status)) {
890 DEBUG(0,("failed to send message: %s\n",
891 nt_errstr(status)));
894 talloc_destroy(mem_ctx);
897 static void account_lockout_policy_handler(struct tevent_context *ctx,
898 struct tevent_timer *te,
899 struct timeval now,
900 void *private_data)
902 struct winbindd_child *child =
903 (struct winbindd_child *)private_data;
904 TALLOC_CTX *mem_ctx = NULL;
905 struct winbindd_methods *methods;
906 struct samr_DomInfo12 lockout_policy;
907 NTSTATUS result;
909 DEBUG(10,("account_lockout_policy_handler called\n"));
911 TALLOC_FREE(child->lockout_policy_event);
913 if ( !winbindd_can_contact_domain( child->domain ) ) {
914 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
915 "do not have an incoming trust to domain %s\n",
916 child->domain->name));
918 return;
921 methods = child->domain->methods;
923 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
924 if (!mem_ctx) {
925 result = NT_STATUS_NO_MEMORY;
926 } else {
927 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
929 TALLOC_FREE(mem_ctx);
931 if (!NT_STATUS_IS_OK(result)) {
932 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
933 nt_errstr(result)));
936 child->lockout_policy_event = tevent_add_timer(winbind_event_context(), NULL,
937 timeval_current_ofs(3600, 0),
938 account_lockout_policy_handler,
939 child);
942 static time_t get_machine_password_timeout(void)
944 /* until we have gpo support use lp setting */
945 return lp_machine_password_timeout();
948 static bool calculate_next_machine_pwd_change(const char *domain,
949 struct timeval *t)
951 time_t pass_last_set_time;
952 time_t timeout;
953 time_t next_change;
954 struct timeval tv;
955 char *pw;
957 pw = secrets_fetch_machine_password(domain,
958 &pass_last_set_time,
959 NULL);
961 if (pw == NULL) {
962 DEBUG(0,("cannot fetch own machine password ????"));
963 return false;
966 SAFE_FREE(pw);
968 timeout = get_machine_password_timeout();
969 if (timeout == 0) {
970 DEBUG(10,("machine password never expires\n"));
971 return false;
974 tv.tv_sec = pass_last_set_time;
975 DEBUG(10, ("password last changed %s\n",
976 timeval_string(talloc_tos(), &tv, false)));
977 tv.tv_sec += timeout;
978 DEBUGADD(10, ("password valid until %s\n",
979 timeval_string(talloc_tos(), &tv, false)));
981 if (time(NULL) < (pass_last_set_time + timeout)) {
982 next_change = pass_last_set_time + timeout;
983 DEBUG(10,("machine password still valid until: %s\n",
984 http_timestring(talloc_tos(), next_change)));
985 *t = timeval_set(next_change, 0);
987 if (lp_clustering()) {
988 uint8_t randbuf;
990 * When having a cluster, we have several
991 * winbinds racing for the password change. In
992 * the machine_password_change_handler()
993 * function we check if someone else was
994 * faster when the event triggers. We add a
995 * 255-second random delay here, so that we
996 * don't run to change the password at the
997 * exact same moment.
999 generate_random_buffer(&randbuf, sizeof(randbuf));
1000 DEBUG(10, ("adding %d seconds randomness\n",
1001 (int)randbuf));
1002 t->tv_sec += randbuf;
1004 return true;
1007 DEBUG(10,("machine password expired, needs immediate change\n"));
1009 *t = timeval_zero();
1011 return true;
1014 static void machine_password_change_handler(struct tevent_context *ctx,
1015 struct tevent_timer *te,
1016 struct timeval now,
1017 void *private_data)
1019 struct messaging_context *msg_ctx = winbind_messaging_context();
1020 struct winbindd_child *child =
1021 (struct winbindd_child *)private_data;
1022 struct rpc_pipe_client *netlogon_pipe = NULL;
1023 NTSTATUS result;
1024 struct timeval next_change;
1026 DEBUG(10,("machine_password_change_handler called\n"));
1028 TALLOC_FREE(child->machine_password_change_event);
1030 if (!calculate_next_machine_pwd_change(child->domain->name,
1031 &next_change)) {
1032 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1033 return;
1036 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1037 timeval_string(talloc_tos(), &next_change, false)));
1039 if (!timeval_expired(&next_change)) {
1040 DEBUG(10, ("Someone else has already changed the pw\n"));
1041 goto done;
1044 if (!winbindd_can_contact_domain(child->domain)) {
1045 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1046 "do not have an incoming trust to domain %s\n",
1047 child->domain->name));
1048 return;
1051 result = cm_connect_netlogon(child->domain, &netlogon_pipe);
1052 if (!NT_STATUS_IS_OK(result)) {
1053 DEBUG(10,("machine_password_change_handler: "
1054 "failed to connect netlogon pipe: %s\n",
1055 nt_errstr(result)));
1056 return;
1059 result = trust_pw_change(child->domain->conn.netlogon_creds,
1060 msg_ctx,
1061 netlogon_pipe->binding_handle,
1062 child->domain->name,
1063 false); /* force */
1065 DEBUG(10, ("machine_password_change_handler: "
1066 "trust_pw_change returned %s\n",
1067 nt_errstr(result)));
1069 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1070 DEBUG(3,("machine_password_change_handler: password set returned "
1071 "ACCESS_DENIED. Maybe the trust account "
1072 "password was changed and we didn't know it. "
1073 "Killing connections to domain %s\n",
1074 child->domain->name));
1075 invalidate_cm_connection(child->domain);
1078 if (!calculate_next_machine_pwd_change(child->domain->name,
1079 &next_change)) {
1080 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1081 return;
1084 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1085 timeval_string(talloc_tos(), &next_change, false)));
1087 if (!NT_STATUS_IS_OK(result)) {
1088 struct timeval tmp;
1090 * In case of failure, give the DC a minute to recover
1092 tmp = timeval_current_ofs(60, 0);
1093 next_change = timeval_max(&next_change, &tmp);
1096 done:
1097 child->machine_password_change_event = tevent_add_timer(winbind_event_context(), NULL,
1098 next_change,
1099 machine_password_change_handler,
1100 child);
1103 /* Deal with a request to go offline. */
1105 static void child_msg_offline(struct messaging_context *msg,
1106 void *private_data,
1107 uint32_t msg_type,
1108 struct server_id server_id,
1109 DATA_BLOB *data)
1111 struct winbindd_domain *domain;
1112 struct winbindd_domain *primary_domain = NULL;
1113 const char *domainname = (const char *)data->data;
1115 if (data->data == NULL || data->length == 0) {
1116 return;
1119 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1121 if (!lp_winbind_offline_logon()) {
1122 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1123 return;
1126 primary_domain = find_our_domain();
1128 /* Mark the requested domain offline. */
1130 for (domain = domain_list(); domain; domain = domain->next) {
1131 if (domain->internal) {
1132 continue;
1134 if (strequal(domain->name, domainname)) {
1135 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1136 set_domain_offline(domain);
1137 /* we are in the trusted domain, set the primary domain
1138 * offline too */
1139 if (domain != primary_domain) {
1140 set_domain_offline(primary_domain);
1146 /* Deal with a request to go online. */
1148 static void child_msg_online(struct messaging_context *msg,
1149 void *private_data,
1150 uint32_t msg_type,
1151 struct server_id server_id,
1152 DATA_BLOB *data)
1154 struct winbindd_domain *domain;
1155 struct winbindd_domain *primary_domain = NULL;
1156 const char *domainname = (const char *)data->data;
1158 if (data->data == NULL || data->length == 0) {
1159 return;
1162 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1164 if (!lp_winbind_offline_logon()) {
1165 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1166 return;
1169 primary_domain = find_our_domain();
1171 /* Set our global state as online. */
1172 set_global_winbindd_state_online();
1174 /* Try and mark everything online - delete any negative cache entries
1175 to force a reconnect now. */
1177 for (domain = domain_list(); domain; domain = domain->next) {
1178 if (domain->internal) {
1179 continue;
1181 if (strequal(domain->name, domainname)) {
1182 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1183 winbindd_flush_negative_conn_cache(domain);
1184 set_domain_online_request(domain);
1186 /* we can be in trusted domain, which will contact primary domain
1187 * we have to bring primary domain online in trusted domain process
1188 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1189 * --> contact_domain = find_our_domain()
1190 * */
1191 if (domain != primary_domain) {
1192 winbindd_flush_negative_conn_cache(primary_domain);
1193 set_domain_online_request(primary_domain);
1199 static void child_msg_dump_event_list(struct messaging_context *msg,
1200 void *private_data,
1201 uint32_t msg_type,
1202 struct server_id server_id,
1203 DATA_BLOB *data)
1205 DEBUG(5,("child_msg_dump_event_list received\n"));
1207 dump_event_list(winbind_event_context());
1210 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1211 const char *logfilename)
1213 struct winbindd_domain *domain;
1214 struct winbindd_child *cl;
1215 NTSTATUS status;
1217 status = reinit_after_fork(
1218 winbind_messaging_context(),
1219 winbind_event_context(),
1220 true);
1221 if (!NT_STATUS_IS_OK(status)) {
1222 DEBUG(0,("reinit_after_fork() failed\n"));
1223 return status;
1226 close_conns_after_fork();
1228 if (!override_logfile && logfilename) {
1229 lp_set_logfile(logfilename);
1230 reopen_logs();
1233 if (!winbindd_setup_sig_term_handler(false))
1234 return NT_STATUS_NO_MEMORY;
1235 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1236 logfilename))
1237 return NT_STATUS_NO_MEMORY;
1239 /* Stop zombies in children */
1240 CatchChild();
1242 /* Don't handle the same messages as our parent. */
1243 messaging_deregister(winbind_messaging_context(),
1244 MSG_SMB_CONF_UPDATED, NULL);
1245 messaging_deregister(winbind_messaging_context(),
1246 MSG_SHUTDOWN, NULL);
1247 messaging_deregister(winbind_messaging_context(),
1248 MSG_WINBIND_OFFLINE, NULL);
1249 messaging_deregister(winbind_messaging_context(),
1250 MSG_WINBIND_ONLINE, NULL);
1251 messaging_deregister(winbind_messaging_context(),
1252 MSG_WINBIND_ONLINESTATUS, NULL);
1253 messaging_deregister(winbind_messaging_context(),
1254 MSG_DUMP_EVENT_LIST, NULL);
1255 messaging_deregister(winbind_messaging_context(),
1256 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1257 messaging_deregister(winbind_messaging_context(),
1258 MSG_DEBUG, NULL);
1260 messaging_deregister(winbind_messaging_context(),
1261 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1262 messaging_deregister(winbind_messaging_context(),
1263 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1265 /* We have destroyed all events in the winbindd_event_context
1266 * in reinit_after_fork(), so clean out all possible pending
1267 * event pointers. */
1269 /* Deal with check_online_events. */
1271 for (domain = domain_list(); domain; domain = domain->next) {
1272 TALLOC_FREE(domain->check_online_event);
1275 /* Ensure we're not handling a credential cache event inherited
1276 * from our parent. */
1278 ccache_remove_all_after_fork();
1280 /* Destroy all possible events in child list. */
1281 for (cl = winbindd_children; cl != NULL; cl = cl->next) {
1282 TALLOC_FREE(cl->lockout_policy_event);
1283 TALLOC_FREE(cl->machine_password_change_event);
1285 /* Children should never be able to send
1286 * each other messages, all messages must
1287 * go through the parent.
1289 cl->pid = (pid_t)0;
1292 * Close service sockets to all other children
1294 if ((cl != myself) && (cl->sock != -1)) {
1295 close(cl->sock);
1296 cl->sock = -1;
1300 * This is a little tricky, children must not
1301 * send an MSG_WINBIND_ONLINE message to idmap_child().
1302 * If we are in a child of our primary domain or
1303 * in the process created by fork_child_dc_connect(),
1304 * and the primary domain cannot go online,
1305 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1306 * periodically to idmap_child().
1308 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1309 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1310 * ---> init_dc_connection() ---> cm_open_connection --->
1311 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1312 * idmap_child(). Disallow children sending messages
1313 * to each other, all messages must go through the parent.
1315 cl = idmap_child();
1316 cl->pid = (pid_t)0;
1318 return NT_STATUS_OK;
1322 * In a child there will be only one domain, reference that here.
1324 static struct winbindd_domain *child_domain;
1326 struct winbindd_domain *wb_child_domain(void)
1328 return child_domain;
1331 struct child_handler_state {
1332 struct winbindd_child *child;
1333 struct winbindd_cli_state cli;
1336 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1337 uint16_t flags, void *private_data)
1339 struct child_handler_state *state =
1340 (struct child_handler_state *)private_data;
1341 NTSTATUS status;
1343 /* fetch a request from the main daemon */
1344 status = child_read_request(state->cli.sock, state->cli.request);
1346 if (!NT_STATUS_IS_OK(status)) {
1347 /* we lost contact with our parent */
1348 _exit(0);
1351 DEBUG(4,("child daemon request %d\n",
1352 (int)state->cli.request->cmd));
1354 ZERO_STRUCTP(state->cli.response);
1355 state->cli.request->null_term = '\0';
1356 state->cli.mem_ctx = talloc_tos();
1357 child_process_request(state->child, &state->cli);
1359 DEBUG(4, ("Finished processing child request %d\n",
1360 (int)state->cli.request->cmd));
1362 SAFE_FREE(state->cli.request->extra_data.data);
1364 status = child_write_response(state->cli.sock, state->cli.response);
1365 if (!NT_STATUS_IS_OK(status)) {
1366 exit(1);
1370 static bool fork_domain_child(struct winbindd_child *child)
1372 int fdpair[2];
1373 struct child_handler_state state;
1374 struct winbindd_request request;
1375 struct winbindd_response response;
1376 struct winbindd_domain *primary_domain = NULL;
1377 NTSTATUS status;
1378 ssize_t nwritten;
1379 struct tevent_fd *fde;
1381 if (child->domain) {
1382 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1383 child->domain->name));
1384 } else {
1385 DEBUG(10, ("fork_domain_child called without domain.\n"));
1388 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1389 DEBUG(0, ("Could not open child pipe: %s\n",
1390 strerror(errno)));
1391 return False;
1394 ZERO_STRUCT(state);
1395 state.child = child;
1396 state.cli.pid = getpid();
1397 state.cli.request = &request;
1398 state.cli.response = &response;
1400 child->pid = fork();
1402 if (child->pid == -1) {
1403 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1404 close(fdpair[0]);
1405 close(fdpair[1]);
1406 return False;
1409 if (child->pid != 0) {
1410 /* Parent */
1411 ssize_t nread;
1413 close(fdpair[0]);
1415 nread = sys_read(fdpair[1], &status, sizeof(status));
1416 if (nread != sizeof(status)) {
1417 DEBUG(1, ("fork_domain_child: Could not read child status: "
1418 "nread=%d, error=%s\n", (int)nread,
1419 strerror(errno)));
1420 close(fdpair[1]);
1421 return false;
1423 if (!NT_STATUS_IS_OK(status)) {
1424 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1425 nt_errstr(status)));
1426 close(fdpair[1]);
1427 return false;
1430 child->next = child->prev = NULL;
1431 DLIST_ADD(winbindd_children, child);
1432 child->sock = fdpair[1];
1433 return True;
1436 /* Child */
1437 child_domain = child->domain;
1439 DEBUG(10, ("Child process %d\n", (int)getpid()));
1441 state.cli.sock = fdpair[0];
1442 close(fdpair[1]);
1444 status = winbindd_reinit_after_fork(child, child->logfilename);
1446 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1447 if (nwritten != sizeof(status)) {
1448 DEBUG(1, ("fork_domain_child: Could not write status: "
1449 "nwritten=%d, error=%s\n", (int)nwritten,
1450 strerror(errno)));
1451 _exit(0);
1453 if (!NT_STATUS_IS_OK(status)) {
1454 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1455 nt_errstr(status)));
1456 _exit(0);
1459 /* Handle online/offline messages. */
1460 messaging_register(winbind_messaging_context(), NULL,
1461 MSG_WINBIND_OFFLINE, child_msg_offline);
1462 messaging_register(winbind_messaging_context(), NULL,
1463 MSG_WINBIND_ONLINE, child_msg_online);
1464 messaging_register(winbind_messaging_context(), NULL,
1465 MSG_DUMP_EVENT_LIST, child_msg_dump_event_list);
1466 messaging_register(winbind_messaging_context(), NULL,
1467 MSG_DEBUG, debug_message);
1468 messaging_register(winbind_messaging_context(), NULL,
1469 MSG_WINBIND_IP_DROPPED,
1470 winbind_msg_ip_dropped);
1473 primary_domain = find_our_domain();
1475 if (primary_domain == NULL) {
1476 smb_panic("no primary domain found");
1479 /* It doesn't matter if we allow cache login,
1480 * try to bring domain online after fork. */
1481 if ( child->domain ) {
1482 child->domain->startup = True;
1483 child->domain->startup_time = time_mono(NULL);
1484 /* we can be in primary domain or in trusted domain
1485 * If we are in trusted domain, set the primary domain
1486 * in start-up mode */
1487 if (!(child->domain->internal)) {
1488 set_domain_online_request(child->domain);
1489 if (!(child->domain->primary)) {
1490 primary_domain->startup = True;
1491 primary_domain->startup_time = time_mono(NULL);
1492 set_domain_online_request(primary_domain);
1498 * We are in idmap child, make sure that we set the
1499 * check_online_event to bring primary domain online.
1501 if (child == idmap_child()) {
1502 set_domain_online_request(primary_domain);
1505 /* We might be in the idmap child...*/
1506 if (child->domain && !(child->domain->internal) &&
1507 lp_winbind_offline_logon()) {
1509 set_domain_online_request(child->domain);
1511 if (primary_domain && (primary_domain != child->domain)) {
1512 /* We need to talk to the primary
1513 * domain as well as the trusted
1514 * domain inside a trusted domain
1515 * child.
1516 * See the code in :
1517 * set_dc_type_and_flags_trustinfo()
1518 * for details.
1520 set_domain_online_request(primary_domain);
1523 child->lockout_policy_event = tevent_add_timer(
1524 winbind_event_context(), NULL, timeval_zero(),
1525 account_lockout_policy_handler,
1526 child);
1529 if (child->domain && child->domain->primary &&
1530 !USE_KERBEROS_KEYTAB &&
1531 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1533 struct timeval next_change;
1535 if (calculate_next_machine_pwd_change(child->domain->name,
1536 &next_change)) {
1537 child->machine_password_change_event = tevent_add_timer(
1538 winbind_event_context(), NULL, next_change,
1539 machine_password_change_handler,
1540 child);
1544 fde = tevent_add_fd(winbind_event_context(), NULL, state.cli.sock,
1545 TEVENT_FD_READ, child_handler, &state);
1546 if (fde == NULL) {
1547 DEBUG(1, ("tevent_add_fd failed\n"));
1548 _exit(1);
1551 while (1) {
1553 int ret;
1554 TALLOC_CTX *frame = talloc_stackframe();
1556 ret = tevent_loop_once(winbind_event_context());
1557 if (ret != 0) {
1558 DEBUG(1, ("tevent_loop_once failed: %s\n",
1559 strerror(errno)));
1560 _exit(1);
1563 if (child->domain && child->domain->startup &&
1564 (time_mono(NULL) > child->domain->startup_time + 30)) {
1565 /* No longer in "startup" mode. */
1566 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1567 child->domain->name ));
1568 child->domain->startup = False;
1571 TALLOC_FREE(frame);
1575 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1576 void *private_data,
1577 uint32_t msg_type,
1578 struct server_id server_id,
1579 DATA_BLOB *data)
1581 struct winbindd_child *child;
1583 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1584 server_id, data);
1587 for (child = winbindd_children; child != NULL; child = child->next) {
1588 messaging_send_buf(msg_ctx, pid_to_procid(child->pid),
1589 msg_type, data->data, data->length);