winbind: Fix a race between the sigchld and 0-sized socket read
[Samba.git] / source3 / winbindd / winbindd_dual.c
blob2a4950b56bfe81cfa054040ff194ff9de91b2362
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"
41 #include "lib/util/sys_rw.h"
42 #include "lib/util/sys_rw_data.h"
44 #undef DBGC_CLASS
45 #define DBGC_CLASS DBGC_WINBIND
47 extern bool override_logfile;
49 static void forall_domain_children(bool (*fn)(struct winbindd_child *c,
50 void *private_data),
51 void *private_data)
53 struct winbindd_domain *d;
55 for (d = domain_list(); d != NULL; d = d->next) {
56 int i;
58 for (i = 0; i < lp_winbind_max_domain_connections(); i++) {
59 struct winbindd_child *c = &d->children[i];
60 bool ok;
62 if (c->pid == 0) {
63 continue;
66 ok = fn(c, private_data);
67 if (!ok) {
68 return;
74 static void forall_children(bool (*fn)(struct winbindd_child *c,
75 void *private_data),
76 void *private_data)
78 struct winbindd_child *c;
79 bool ok;
81 c = idmap_child();
82 if (c->pid != 0) {
83 ok = fn(c, private_data);
84 if (!ok) {
85 return;
89 c = locator_child();
90 if (c->pid != 0) {
91 ok = fn(c, private_data);
92 if (!ok) {
93 return;
97 forall_domain_children(fn, private_data);
100 /* Read some data from a client connection */
102 static NTSTATUS child_read_request(int sock, struct winbindd_request *wreq)
104 NTSTATUS status;
106 status = read_data_ntstatus(sock, (char *)wreq, sizeof(*wreq));
107 if (!NT_STATUS_IS_OK(status)) {
108 DEBUG(3, ("child_read_request: read_data failed: %s\n",
109 nt_errstr(status)));
110 return status;
113 if (wreq->extra_len == 0) {
114 wreq->extra_data.data = NULL;
115 return NT_STATUS_OK;
118 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq->extra_len));
120 wreq->extra_data.data = SMB_MALLOC_ARRAY(char, wreq->extra_len + 1);
121 if (wreq->extra_data.data == NULL) {
122 DEBUG(0, ("malloc failed\n"));
123 return NT_STATUS_NO_MEMORY;
126 /* Ensure null termination */
127 wreq->extra_data.data[wreq->extra_len] = '\0';
129 status = read_data_ntstatus(sock, wreq->extra_data.data,
130 wreq->extra_len);
131 if (!NT_STATUS_IS_OK(status)) {
132 DEBUG(0, ("Could not read extra data: %s\n",
133 nt_errstr(status)));
135 return status;
138 static NTSTATUS child_write_response(int sock, struct winbindd_response *wrsp)
140 struct iovec iov[2];
141 int iov_count;
143 iov[0].iov_base = (void *)wrsp;
144 iov[0].iov_len = sizeof(struct winbindd_response);
145 iov_count = 1;
147 if (wrsp->length > sizeof(struct winbindd_response)) {
148 iov[1].iov_base = (void *)wrsp->extra_data.data;
149 iov[1].iov_len = wrsp->length-iov[0].iov_len;
150 iov_count = 2;
153 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp->length));
155 if (write_data_iov(sock, iov, iov_count) != wrsp->length) {
156 DEBUG(0, ("Could not write result\n"));
157 return NT_STATUS_INVALID_HANDLE;
160 return NT_STATUS_OK;
164 * Do winbind child async request. This is not simply wb_simple_trans. We have
165 * to do the queueing ourselves because while a request is queued, the child
166 * might have crashed, and we have to re-fork it in the _trigger function.
169 struct wb_child_request_state {
170 struct tevent_context *ev;
171 struct tevent_req *queue_subreq;
172 struct tevent_req *subreq;
173 struct winbindd_child *child;
174 struct winbindd_request *request;
175 struct winbindd_response *response;
178 static bool fork_domain_child(struct winbindd_child *child);
180 static void wb_child_request_waited(struct tevent_req *subreq);
181 static void wb_child_request_done(struct tevent_req *subreq);
182 static void wb_child_request_orphaned(struct tevent_req *subreq);
184 static void wb_child_request_cleanup(struct tevent_req *req,
185 enum tevent_req_state req_state);
187 struct tevent_req *wb_child_request_send(TALLOC_CTX *mem_ctx,
188 struct tevent_context *ev,
189 struct winbindd_child *child,
190 struct winbindd_request *request)
192 struct tevent_req *req;
193 struct wb_child_request_state *state;
194 struct tevent_req *subreq;
196 req = tevent_req_create(mem_ctx, &state,
197 struct wb_child_request_state);
198 if (req == NULL) {
199 return NULL;
202 state->ev = ev;
203 state->child = child;
204 state->request = request;
206 subreq = tevent_queue_wait_send(state, ev, child->queue);
207 if (tevent_req_nomem(subreq, req)) {
208 return tevent_req_post(req, ev);
210 tevent_req_set_callback(subreq, wb_child_request_waited, req);
211 state->queue_subreq = subreq;
213 tevent_req_set_cleanup_fn(req, wb_child_request_cleanup);
215 return req;
218 static void wb_child_request_waited(struct tevent_req *subreq)
220 struct tevent_req *req = tevent_req_callback_data(
221 subreq, struct tevent_req);
222 struct wb_child_request_state *state = tevent_req_data(
223 req, struct wb_child_request_state);
224 bool ok;
226 ok = tevent_queue_wait_recv(subreq);
227 if (!ok) {
228 tevent_req_oom(req);
229 return;
232 * We need to keep state->queue_subreq
233 * in order to block the queue.
235 subreq = NULL;
237 if ((state->child->sock == -1) && (!fork_domain_child(state->child))) {
238 tevent_req_error(req, errno);
239 return;
242 tevent_fd_set_flags(state->child->monitor_fde, 0);
244 subreq = wb_simple_trans_send(state, server_event_context(), NULL,
245 state->child->sock, state->request);
246 if (tevent_req_nomem(subreq, req)) {
247 return;
250 state->subreq = subreq;
251 tevent_req_set_callback(subreq, wb_child_request_done, req);
252 tevent_req_set_endtime(req, state->ev, timeval_current_ofs(300, 0));
255 static void wb_child_request_done(struct tevent_req *subreq)
257 struct tevent_req *req = tevent_req_callback_data(
258 subreq, struct tevent_req);
259 struct wb_child_request_state *state = tevent_req_data(
260 req, struct wb_child_request_state);
261 int ret, err;
263 ret = wb_simple_trans_recv(subreq, state, &state->response, &err);
264 /* Freeing the subrequest is deferred until the cleanup function,
265 * which has to know whether a subrequest exists, and consequently
266 * decide whether to shut down the pipe to the child process.
268 if (ret == -1) {
269 tevent_req_error(req, err);
270 return;
272 tevent_req_done(req);
275 static void wb_child_request_orphaned(struct tevent_req *subreq)
277 struct winbindd_child *child =
278 (struct winbindd_child *)tevent_req_callback_data_void(subreq);
280 DBG_WARNING("cleanup orphaned subreq[%p]\n", subreq);
281 TALLOC_FREE(subreq);
283 if (child->domain != NULL) {
285 * If the child is attached to a domain,
286 * we need to make sure the domain queue
287 * can move forward, after the orphaned
288 * request is done.
290 tevent_queue_start(child->domain->queue);
294 int wb_child_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
295 struct winbindd_response **presponse, int *err)
297 struct wb_child_request_state *state = tevent_req_data(
298 req, struct wb_child_request_state);
300 if (tevent_req_is_unix_error(req, err)) {
301 return -1;
303 *presponse = talloc_move(mem_ctx, &state->response);
304 return 0;
307 static void wb_child_request_cleanup(struct tevent_req *req,
308 enum tevent_req_state req_state)
310 struct wb_child_request_state *state =
311 tevent_req_data(req, struct wb_child_request_state);
313 if (state->subreq == NULL) {
314 /* nothing to cleanup */
315 return;
318 if (req_state == TEVENT_REQ_RECEIVED) {
319 struct tevent_req *subreq = NULL;
322 * Our caller gave up, but we need to keep
323 * the low level request (wb_simple_trans)
324 * in order to maintain the parent child protocol.
326 * We also need to keep the child queue blocked
327 * until we got the response from the child.
330 subreq = talloc_move(state->child->queue, &state->subreq);
331 talloc_move(subreq, &state->queue_subreq);
332 tevent_req_set_callback(subreq,
333 wb_child_request_orphaned,
334 state->child);
336 DBG_WARNING("keep orphaned subreq[%p]\n", subreq);
337 return;
340 TALLOC_FREE(state->subreq);
341 TALLOC_FREE(state->queue_subreq);
343 tevent_fd_set_flags(state->child->monitor_fde, TEVENT_FD_READ);
345 if (state->child->domain != NULL) {
347 * If the child is attached to a domain,
348 * we need to make sure the domain queue
349 * can move forward, after the request
350 * is done.
352 tevent_queue_start(state->child->domain->queue);
355 if (req_state == TEVENT_REQ_DONE) {
356 /* transmitted request and got response */
357 return;
361 * Failed to transmit and receive response, or request
362 * cancelled while being serviced.
363 * The basic parent/child communication broke, close
364 * our socket
366 TALLOC_FREE(state->child->monitor_fde);
367 close(state->child->sock);
368 state->child->sock = -1;
371 static void child_socket_readable(struct tevent_context *ev,
372 struct tevent_fd *fde,
373 uint16_t flags,
374 void *private_data)
376 struct winbindd_child *child = private_data;
378 if ((flags & TEVENT_FD_READ) == 0) {
379 return;
382 TALLOC_FREE(child->monitor_fde);
385 * We're only active when there is no outstanding child
386 * request. Arriving here means the child closed its socket,
387 * it died. Do the same here.
390 SMB_ASSERT(child->sock != -1);
392 close(child->sock);
393 child->sock = -1;
396 static struct winbindd_child *choose_domain_child(struct winbindd_domain *domain)
398 struct winbindd_child *shortest = &domain->children[0];
399 struct winbindd_child *current;
400 int i;
402 for (i=0; i<lp_winbind_max_domain_connections(); i++) {
403 size_t shortest_len, current_len;
405 current = &domain->children[i];
406 current_len = tevent_queue_length(current->queue);
408 if (current_len == 0) {
409 /* idle child */
410 return current;
413 shortest_len = tevent_queue_length(shortest->queue);
415 if (current_len < shortest_len) {
416 shortest = current;
420 return shortest;
423 struct dcerpc_binding_handle *dom_child_handle(struct winbindd_domain *domain)
425 return domain->binding_handle;
428 struct wb_domain_request_state {
429 struct tevent_context *ev;
430 struct tevent_queue_entry *queue_entry;
431 struct winbindd_domain *domain;
432 struct winbindd_child *child;
433 struct winbindd_request *request;
434 struct winbindd_request *init_req;
435 struct winbindd_response *response;
436 struct tevent_req *pending_subreq;
439 static void wb_domain_request_cleanup(struct tevent_req *req,
440 enum tevent_req_state req_state)
442 struct wb_domain_request_state *state = tevent_req_data(
443 req, struct wb_domain_request_state);
446 * If we're completely done or got a failure.
447 * we should remove ourself from the domain queue,
448 * after removing the child subreq from the child queue
449 * and give the next one in the queue the chance
450 * to check for an idle child.
452 TALLOC_FREE(state->pending_subreq);
453 TALLOC_FREE(state->queue_entry);
454 tevent_queue_start(state->domain->queue);
457 static void wb_domain_request_trigger(struct tevent_req *req,
458 void *private_data);
459 static void wb_domain_request_gotdc(struct tevent_req *subreq);
460 static void wb_domain_request_initialized(struct tevent_req *subreq);
461 static void wb_domain_request_done(struct tevent_req *subreq);
463 struct tevent_req *wb_domain_request_send(TALLOC_CTX *mem_ctx,
464 struct tevent_context *ev,
465 struct winbindd_domain *domain,
466 struct winbindd_request *request)
468 struct tevent_req *req;
469 struct wb_domain_request_state *state;
471 req = tevent_req_create(mem_ctx, &state,
472 struct wb_domain_request_state);
473 if (req == NULL) {
474 return NULL;
477 state->domain = domain;
478 state->ev = ev;
479 state->request = request;
481 tevent_req_set_cleanup_fn(req, wb_domain_request_cleanup);
483 state->queue_entry = tevent_queue_add_entry(
484 domain->queue, state->ev, req,
485 wb_domain_request_trigger, NULL);
486 if (tevent_req_nomem(state->queue_entry, req)) {
487 return tevent_req_post(req, ev);
490 return req;
493 static void wb_domain_request_trigger(struct tevent_req *req,
494 void *private_data)
496 struct wb_domain_request_state *state = tevent_req_data(
497 req, struct wb_domain_request_state);
498 struct winbindd_domain *domain = state->domain;
499 struct tevent_req *subreq = NULL;
500 size_t shortest_queue_length;
502 state->child = choose_domain_child(domain);
503 shortest_queue_length = tevent_queue_length(state->child->queue);
504 if (shortest_queue_length > 0) {
506 * All children are busy, we need to stop
507 * the queue and untrigger our own queue
508 * entry. Once a pending request
509 * is done it calls tevent_queue_start
510 * and we get retriggered.
512 state->child = NULL;
513 tevent_queue_stop(state->domain->queue);
514 tevent_queue_entry_untrigger(state->queue_entry);
515 return;
518 if (domain->initialized) {
519 subreq = wb_child_request_send(state, state->ev, state->child,
520 state->request);
521 if (tevent_req_nomem(subreq, req)) {
522 return;
524 tevent_req_set_callback(subreq, wb_domain_request_done, req);
525 state->pending_subreq = subreq;
528 * Once the domain is initialized and
529 * once we placed our real request into the child queue,
530 * we can remove ourself from the domain queue
531 * and give the next one in the queue the chance
532 * to check for an idle child.
534 TALLOC_FREE(state->queue_entry);
535 return;
538 state->init_req = talloc_zero(state, struct winbindd_request);
539 if (tevent_req_nomem(state->init_req, req)) {
540 return;
543 if (IS_DC || domain->primary || domain->internal) {
544 /* The primary domain has to find the DC name itself */
545 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
546 fstrcpy(state->init_req->domain_name, domain->name);
547 state->init_req->data.init_conn.is_primary = domain->primary;
548 fstrcpy(state->init_req->data.init_conn.dcname, "");
550 subreq = wb_child_request_send(state, state->ev, state->child,
551 state->init_req);
552 if (tevent_req_nomem(subreq, req)) {
553 return;
555 tevent_req_set_callback(subreq, wb_domain_request_initialized,
556 req);
557 state->pending_subreq = subreq;
558 return;
562 * This is *not* the primary domain,
563 * let's ask our DC about a DC name.
565 * We prefer getting a dns name in dc_unc,
566 * which is indicated by DS_RETURN_DNS_NAME.
567 * For NT4 domains we still get the netbios name.
569 subreq = wb_dsgetdcname_send(state, state->ev,
570 state->domain->name,
571 NULL, /* domain_guid */
572 NULL, /* site_name */
573 DS_RETURN_DNS_NAME); /* flags */
574 if (tevent_req_nomem(subreq, req)) {
575 return;
577 tevent_req_set_callback(subreq, wb_domain_request_gotdc, req);
578 state->pending_subreq = subreq;
579 return;
582 static void wb_domain_request_gotdc(struct tevent_req *subreq)
584 struct tevent_req *req = tevent_req_callback_data(
585 subreq, struct tevent_req);
586 struct wb_domain_request_state *state = tevent_req_data(
587 req, struct wb_domain_request_state);
588 struct netr_DsRGetDCNameInfo *dcinfo = NULL;
589 NTSTATUS status;
590 const char *dcname = NULL;
592 state->pending_subreq = NULL;
594 status = wb_dsgetdcname_recv(subreq, state, &dcinfo);
595 TALLOC_FREE(subreq);
596 if (tevent_req_nterror(req, status)) {
597 return;
599 dcname = dcinfo->dc_unc;
600 while (dcname != NULL && *dcname == '\\') {
601 dcname++;
603 state->init_req->cmd = WINBINDD_INIT_CONNECTION;
604 fstrcpy(state->init_req->domain_name, state->domain->name);
605 state->init_req->data.init_conn.is_primary = False;
606 fstrcpy(state->init_req->data.init_conn.dcname,
607 dcname);
609 TALLOC_FREE(dcinfo);
611 subreq = wb_child_request_send(state, state->ev, state->child,
612 state->init_req);
613 if (tevent_req_nomem(subreq, req)) {
614 return;
616 tevent_req_set_callback(subreq, wb_domain_request_initialized, req);
617 state->pending_subreq = subreq;
620 static void wb_domain_request_initialized(struct tevent_req *subreq)
622 struct tevent_req *req = tevent_req_callback_data(
623 subreq, struct tevent_req);
624 struct wb_domain_request_state *state = tevent_req_data(
625 req, struct wb_domain_request_state);
626 struct winbindd_response *response;
627 int ret, err;
629 state->pending_subreq = NULL;
631 ret = wb_child_request_recv(subreq, talloc_tos(), &response, &err);
632 TALLOC_FREE(subreq);
633 if (ret == -1) {
634 tevent_req_error(req, err);
635 return;
638 if (!string_to_sid(&state->domain->sid,
639 response->data.domain_info.sid)) {
640 DEBUG(1,("init_child_recv: Could not convert sid %s "
641 "from string\n", response->data.domain_info.sid));
642 tevent_req_error(req, EINVAL);
643 return;
646 talloc_free(state->domain->name);
647 state->domain->name = talloc_strdup(state->domain,
648 response->data.domain_info.name);
649 if (state->domain->name == NULL) {
650 tevent_req_error(req, ENOMEM);
651 return;
654 if (response->data.domain_info.alt_name[0] != '\0') {
655 talloc_free(state->domain->alt_name);
657 state->domain->alt_name = talloc_strdup(state->domain,
658 response->data.domain_info.alt_name);
659 if (state->domain->alt_name == NULL) {
660 tevent_req_error(req, ENOMEM);
661 return;
665 state->domain->native_mode = response->data.domain_info.native_mode;
666 state->domain->active_directory =
667 response->data.domain_info.active_directory;
668 state->domain->initialized = true;
670 TALLOC_FREE(response);
672 subreq = wb_child_request_send(state, state->ev, state->child,
673 state->request);
674 if (tevent_req_nomem(subreq, req)) {
675 return;
677 tevent_req_set_callback(subreq, wb_domain_request_done, req);
678 state->pending_subreq = subreq;
681 * Once the domain is initialized and
682 * once we placed our real request into the child queue,
683 * we can remove ourself from the domain queue
684 * and give the next one in the queue the chance
685 * to check for an idle child.
687 TALLOC_FREE(state->queue_entry);
690 static void wb_domain_request_done(struct tevent_req *subreq)
692 struct tevent_req *req = tevent_req_callback_data(
693 subreq, struct tevent_req);
694 struct wb_domain_request_state *state = tevent_req_data(
695 req, struct wb_domain_request_state);
696 int ret, err;
698 state->pending_subreq = NULL;
700 ret = wb_child_request_recv(subreq, talloc_tos(), &state->response,
701 &err);
702 TALLOC_FREE(subreq);
703 if (ret == -1) {
704 tevent_req_error(req, err);
705 return;
707 tevent_req_done(req);
710 int wb_domain_request_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
711 struct winbindd_response **presponse, int *err)
713 struct wb_domain_request_state *state = tevent_req_data(
714 req, struct wb_domain_request_state);
716 if (tevent_req_is_unix_error(req, err)) {
717 return -1;
719 *presponse = talloc_move(mem_ctx, &state->response);
720 return 0;
723 static void child_process_request(struct winbindd_child *child,
724 struct winbindd_cli_state *state)
726 struct winbindd_domain *domain = child->domain;
727 const struct winbindd_child_dispatch_table *table = child->table;
729 /* Free response data - we may be interrupted and receive another
730 command before being able to send this data off. */
732 state->response->result = WINBINDD_ERROR;
733 state->response->length = sizeof(struct winbindd_response);
735 /* as all requests in the child are sync, we can use talloc_tos() */
736 state->mem_ctx = talloc_tos();
738 /* Process command */
740 for (; table->name; table++) {
741 if (state->request->cmd == table->struct_cmd) {
742 DEBUG(10,("child_process_request: request fn %s\n",
743 table->name));
744 state->response->result = table->struct_fn(domain, state);
745 return;
749 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
750 (int)state->request->cmd));
751 state->response->result = WINBINDD_ERROR;
754 void setup_child(struct winbindd_domain *domain, struct winbindd_child *child,
755 const struct winbindd_child_dispatch_table *table,
756 const char *logprefix,
757 const char *logname)
759 if (logprefix && logname) {
760 char *logbase = NULL;
762 if (*lp_logfile(talloc_tos())) {
763 char *end = NULL;
765 if (asprintf(&logbase, "%s", lp_logfile(talloc_tos())) < 0) {
766 smb_panic("Internal error: asprintf failed");
769 if ((end = strrchr_m(logbase, '/'))) {
770 *end = '\0';
772 } else {
773 if (asprintf(&logbase, "%s", get_dyn_LOGFILEBASE()) < 0) {
774 smb_panic("Internal error: asprintf failed");
778 if (asprintf(&child->logfilename, "%s/%s-%s",
779 logbase, logprefix, logname) < 0) {
780 SAFE_FREE(logbase);
781 smb_panic("Internal error: asprintf failed");
784 SAFE_FREE(logbase);
785 } else {
786 smb_panic("Internal error: logprefix == NULL && "
787 "logname == NULL");
790 child->pid = 0;
791 child->sock = -1;
792 child->domain = domain;
793 child->table = table;
794 child->queue = tevent_queue_create(NULL, "winbind_child");
795 SMB_ASSERT(child->queue != NULL);
796 if (domain == NULL) {
797 child->binding_handle = wbint_binding_handle(NULL, NULL, child);
798 SMB_ASSERT(child->binding_handle != NULL);
802 struct winbind_child_died_state {
803 pid_t pid;
804 struct winbindd_child *child;
807 static bool winbind_child_died_fn(struct winbindd_child *child,
808 void *private_data)
810 struct winbind_child_died_state *state = private_data;
812 if (child->pid == state->pid) {
813 state->child = child;
814 return false;
816 return true;
819 void winbind_child_died(pid_t pid)
821 struct winbind_child_died_state state = { .pid = pid };
823 forall_children(winbind_child_died_fn, &state);
825 if (state.child == NULL) {
826 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
827 return;
830 state.child->pid = 0;
833 /* Ensure any negative cache entries with the netbios or realm names are removed. */
835 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
837 flush_negative_conn_cache_for_domain(domain->name);
838 if (domain->alt_name != NULL) {
839 flush_negative_conn_cache_for_domain(domain->alt_name);
844 * Parent winbindd process sets its own debug level first and then
845 * sends a message to all the winbindd children to adjust their debug
846 * level to that of parents.
849 struct winbind_msg_relay_state {
850 struct messaging_context *msg_ctx;
851 uint32_t msg_type;
852 DATA_BLOB *data;
855 static bool winbind_msg_relay_fn(struct winbindd_child *child,
856 void *private_data)
858 struct winbind_msg_relay_state *state = private_data;
860 DBG_DEBUG("sending message to pid %u.\n",
861 (unsigned int)child->pid);
863 messaging_send(state->msg_ctx, pid_to_procid(child->pid),
864 state->msg_type, state->data);
865 return true;
868 void winbind_msg_debug(struct messaging_context *msg_ctx,
869 void *private_data,
870 uint32_t msg_type,
871 struct server_id server_id,
872 DATA_BLOB *data)
874 struct winbind_msg_relay_state state = {
875 .msg_ctx = msg_ctx, .msg_type = msg_type, .data = data
878 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
880 debug_message(msg_ctx, private_data, MSG_DEBUG, server_id, data);
882 forall_children(winbind_msg_relay_fn, &state);
885 /* Set our domains as offline and forward the offline message to our children. */
887 struct winbind_msg_on_offline_state {
888 struct messaging_context *msg_ctx;
889 uint32_t msg_type;
892 static bool winbind_msg_on_offline_fn(struct winbindd_child *child,
893 void *private_data)
895 struct winbind_msg_on_offline_state *state = private_data;
897 if (child->domain->internal) {
898 return true;
902 * Each winbindd child should only process requests for one
903 * domain - make sure we only set it online / offline for that
904 * domain.
906 DBG_DEBUG("sending message to pid %u for domain %s.\n",
907 (unsigned int)child->pid, child->domain->name);
909 messaging_send_buf(state->msg_ctx,
910 pid_to_procid(child->pid),
911 state->msg_type,
912 (const uint8_t *)child->domain->name,
913 strlen(child->domain->name)+1);
915 return true;
918 void winbind_msg_offline(struct messaging_context *msg_ctx,
919 void *private_data,
920 uint32_t msg_type,
921 struct server_id server_id,
922 DATA_BLOB *data)
924 struct winbind_msg_on_offline_state state = {
925 .msg_ctx = msg_ctx,
926 .msg_type = MSG_WINBIND_OFFLINE,
928 struct winbindd_domain *domain;
930 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
932 if (!lp_winbind_offline_logon()) {
933 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
934 return;
937 /* Set our global state as offline. */
938 if (!set_global_winbindd_state_offline()) {
939 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
940 return;
943 /* Set all our domains as offline. */
944 for (domain = domain_list(); domain; domain = domain->next) {
945 if (domain->internal) {
946 continue;
948 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
949 set_domain_offline(domain);
952 forall_domain_children(winbind_msg_on_offline_fn, &state);
955 /* Set our domains as online and forward the online message to our children. */
957 void winbind_msg_online(struct messaging_context *msg_ctx,
958 void *private_data,
959 uint32_t msg_type,
960 struct server_id server_id,
961 DATA_BLOB *data)
963 struct winbind_msg_on_offline_state state = {
964 .msg_ctx = msg_ctx,
965 .msg_type = MSG_WINBIND_ONLINE,
967 struct winbindd_domain *domain;
969 DEBUG(10,("winbind_msg_online: got online message.\n"));
971 if (!lp_winbind_offline_logon()) {
972 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
973 return;
976 /* Set our global state as online. */
977 set_global_winbindd_state_online();
979 smb_nscd_flush_user_cache();
980 smb_nscd_flush_group_cache();
982 /* Set all our domains as online. */
983 for (domain = domain_list(); domain; domain = domain->next) {
984 if (domain->internal) {
985 continue;
987 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
989 winbindd_flush_negative_conn_cache(domain);
990 set_domain_online_request(domain);
992 /* Send an online message to the idmap child when our
993 primary domain comes back online */
995 if ( domain->primary ) {
996 struct winbindd_child *idmap = idmap_child();
998 if ( idmap->pid != 0 ) {
999 messaging_send_buf(msg_ctx,
1000 pid_to_procid(idmap->pid),
1001 MSG_WINBIND_ONLINE,
1002 (const uint8_t *)domain->name,
1003 strlen(domain->name)+1);
1008 forall_domain_children(winbind_msg_on_offline_fn, &state);
1011 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
1013 struct winbindd_domain *domain;
1014 char *buf = NULL;
1016 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
1017 get_global_winbindd_state_offline() ?
1018 "Offline":"Online")) == NULL) {
1019 return NULL;
1022 for (domain = domain_list(); domain; domain = domain->next) {
1023 if ((buf = talloc_asprintf_append_buffer(buf, "%s:%s ",
1024 domain->name,
1025 domain->online ?
1026 "Online":"Offline")) == NULL) {
1027 return NULL;
1031 buf = talloc_asprintf_append_buffer(buf, "\n");
1033 DEBUG(5,("collect_onlinestatus: %s", buf));
1035 return buf;
1038 void winbind_msg_onlinestatus(struct messaging_context *msg_ctx,
1039 void *private_data,
1040 uint32_t msg_type,
1041 struct server_id server_id,
1042 DATA_BLOB *data)
1044 TALLOC_CTX *mem_ctx;
1045 const char *message;
1047 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1049 mem_ctx = talloc_init("winbind_msg_onlinestatus");
1050 if (mem_ctx == NULL) {
1051 return;
1054 message = collect_onlinestatus(mem_ctx);
1055 if (message == NULL) {
1056 talloc_destroy(mem_ctx);
1057 return;
1060 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_ONLINESTATUS,
1061 (const uint8_t *)message, strlen(message) + 1);
1063 talloc_destroy(mem_ctx);
1066 void winbind_msg_dump_domain_list(struct messaging_context *msg_ctx,
1067 void *private_data,
1068 uint32_t msg_type,
1069 struct server_id server_id,
1070 DATA_BLOB *data)
1072 TALLOC_CTX *mem_ctx;
1073 const char *message = NULL;
1074 const char *domain = NULL;
1075 char *s = NULL;
1076 NTSTATUS status;
1077 struct winbindd_domain *dom = NULL;
1079 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
1081 mem_ctx = talloc_init("winbind_msg_dump_domain_list");
1082 if (!mem_ctx) {
1083 return;
1086 if (data->length > 0) {
1087 domain = (const char *)data->data;
1090 if (domain) {
1092 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
1093 domain));
1095 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain,
1096 find_domain_from_name_noinit(domain));
1097 if (!message) {
1098 talloc_destroy(mem_ctx);
1099 return;
1102 messaging_send_buf(msg_ctx, server_id,
1103 MSG_WINBIND_DUMP_DOMAIN_LIST,
1104 (const uint8_t *)message, strlen(message) + 1);
1106 talloc_destroy(mem_ctx);
1108 return;
1111 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
1113 for (dom = domain_list(); dom; dom=dom->next) {
1114 message = NDR_PRINT_STRUCT_STRING(mem_ctx, winbindd_domain, dom);
1115 if (!message) {
1116 talloc_destroy(mem_ctx);
1117 return;
1120 s = talloc_asprintf_append(s, "%s\n", message);
1121 if (!s) {
1122 talloc_destroy(mem_ctx);
1123 return;
1127 status = messaging_send_buf(msg_ctx, server_id,
1128 MSG_WINBIND_DUMP_DOMAIN_LIST,
1129 (uint8_t *)s, strlen(s) + 1);
1130 if (!NT_STATUS_IS_OK(status)) {
1131 DEBUG(0,("failed to send message: %s\n",
1132 nt_errstr(status)));
1135 talloc_destroy(mem_ctx);
1138 static void account_lockout_policy_handler(struct tevent_context *ctx,
1139 struct tevent_timer *te,
1140 struct timeval now,
1141 void *private_data)
1143 struct winbindd_child *child =
1144 (struct winbindd_child *)private_data;
1145 TALLOC_CTX *mem_ctx = NULL;
1146 struct samr_DomInfo12 lockout_policy;
1147 NTSTATUS result;
1149 DEBUG(10,("account_lockout_policy_handler called\n"));
1151 TALLOC_FREE(child->lockout_policy_event);
1153 if ( !winbindd_can_contact_domain( child->domain ) ) {
1154 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
1155 "do not have an incoming trust to domain %s\n",
1156 child->domain->name));
1158 return;
1161 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
1162 if (!mem_ctx) {
1163 result = NT_STATUS_NO_MEMORY;
1164 } else {
1165 result = wb_cache_lockout_policy(child->domain, mem_ctx,
1166 &lockout_policy);
1168 TALLOC_FREE(mem_ctx);
1170 if (!NT_STATUS_IS_OK(result)) {
1171 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1172 nt_errstr(result)));
1175 child->lockout_policy_event = tevent_add_timer(server_event_context(), NULL,
1176 timeval_current_ofs(3600, 0),
1177 account_lockout_policy_handler,
1178 child);
1181 static time_t get_machine_password_timeout(void)
1183 /* until we have gpo support use lp setting */
1184 return lp_machine_password_timeout();
1187 static bool calculate_next_machine_pwd_change(const char *domain,
1188 struct timeval *t)
1190 time_t pass_last_set_time;
1191 time_t timeout;
1192 time_t next_change;
1193 struct timeval tv;
1194 char *pw;
1196 pw = secrets_fetch_machine_password(domain,
1197 &pass_last_set_time,
1198 NULL);
1200 if (pw == NULL) {
1201 DEBUG(0,("cannot fetch own machine password ????"));
1202 return false;
1205 SAFE_FREE(pw);
1207 timeout = get_machine_password_timeout();
1208 if (timeout == 0) {
1209 DEBUG(10,("machine password never expires\n"));
1210 return false;
1213 tv.tv_sec = pass_last_set_time;
1214 DEBUG(10, ("password last changed %s\n",
1215 timeval_string(talloc_tos(), &tv, false)));
1216 tv.tv_sec += timeout;
1217 DEBUGADD(10, ("password valid until %s\n",
1218 timeval_string(talloc_tos(), &tv, false)));
1220 if (time(NULL) < (pass_last_set_time + timeout)) {
1221 next_change = pass_last_set_time + timeout;
1222 DEBUG(10,("machine password still valid until: %s\n",
1223 http_timestring(talloc_tos(), next_change)));
1224 *t = timeval_set(next_change, 0);
1226 if (lp_clustering()) {
1227 uint8_t randbuf;
1229 * When having a cluster, we have several
1230 * winbinds racing for the password change. In
1231 * the machine_password_change_handler()
1232 * function we check if someone else was
1233 * faster when the event triggers. We add a
1234 * 255-second random delay here, so that we
1235 * don't run to change the password at the
1236 * exact same moment.
1238 generate_random_buffer(&randbuf, sizeof(randbuf));
1239 DEBUG(10, ("adding %d seconds randomness\n",
1240 (int)randbuf));
1241 t->tv_sec += randbuf;
1243 return true;
1246 DEBUG(10,("machine password expired, needs immediate change\n"));
1248 *t = timeval_zero();
1250 return true;
1253 static void machine_password_change_handler(struct tevent_context *ctx,
1254 struct tevent_timer *te,
1255 struct timeval now,
1256 void *private_data)
1258 struct messaging_context *msg_ctx = server_messaging_context();
1259 struct winbindd_child *child =
1260 (struct winbindd_child *)private_data;
1261 struct rpc_pipe_client *netlogon_pipe = NULL;
1262 struct netlogon_creds_cli_context *netlogon_creds_ctx = NULL;
1263 NTSTATUS result;
1264 struct timeval next_change;
1266 DEBUG(10,("machine_password_change_handler called\n"));
1268 TALLOC_FREE(child->machine_password_change_event);
1270 if (!calculate_next_machine_pwd_change(child->domain->name,
1271 &next_change)) {
1272 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1273 return;
1276 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1277 timeval_string(talloc_tos(), &next_change, false)));
1279 if (!timeval_expired(&next_change)) {
1280 DEBUG(10, ("Someone else has already changed the pw\n"));
1281 goto done;
1284 if (!winbindd_can_contact_domain(child->domain)) {
1285 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1286 "do not have an incoming trust to domain %s\n",
1287 child->domain->name));
1288 return;
1291 result = cm_connect_netlogon_secure(child->domain,
1292 &netlogon_pipe,
1293 &netlogon_creds_ctx);
1294 if (!NT_STATUS_IS_OK(result)) {
1295 DEBUG(10,("machine_password_change_handler: "
1296 "failed to connect netlogon pipe: %s\n",
1297 nt_errstr(result)));
1298 return;
1301 result = trust_pw_change(netlogon_creds_ctx,
1302 msg_ctx,
1303 netlogon_pipe->binding_handle,
1304 child->domain->name,
1305 child->domain->dcname,
1306 false); /* force */
1308 DEBUG(10, ("machine_password_change_handler: "
1309 "trust_pw_change returned %s\n",
1310 nt_errstr(result)));
1312 if (NT_STATUS_EQUAL(result, NT_STATUS_ACCESS_DENIED) ) {
1313 DEBUG(3,("machine_password_change_handler: password set returned "
1314 "ACCESS_DENIED. Maybe the trust account "
1315 "password was changed and we didn't know it. "
1316 "Killing connections to domain %s\n",
1317 child->domain->name));
1318 invalidate_cm_connection(child->domain);
1321 if (!calculate_next_machine_pwd_change(child->domain->name,
1322 &next_change)) {
1323 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1324 return;
1327 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1328 timeval_string(talloc_tos(), &next_change, false)));
1330 if (!NT_STATUS_IS_OK(result)) {
1331 struct timeval tmp;
1333 * In case of failure, give the DC a minute to recover
1335 tmp = timeval_current_ofs(60, 0);
1336 next_change = timeval_max(&next_change, &tmp);
1339 done:
1340 child->machine_password_change_event = tevent_add_timer(server_event_context(), NULL,
1341 next_change,
1342 machine_password_change_handler,
1343 child);
1346 /* Deal with a request to go offline. */
1348 static void child_msg_offline(struct messaging_context *msg,
1349 void *private_data,
1350 uint32_t msg_type,
1351 struct server_id server_id,
1352 DATA_BLOB *data)
1354 struct winbindd_domain *domain;
1355 struct winbindd_domain *primary_domain = NULL;
1356 const char *domainname = (const char *)data->data;
1358 if (data->data == NULL || data->length == 0) {
1359 return;
1362 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
1364 if (!lp_winbind_offline_logon()) {
1365 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1366 return;
1369 primary_domain = find_our_domain();
1371 /* Mark the requested domain offline. */
1373 for (domain = domain_list(); domain; domain = domain->next) {
1374 if (domain->internal) {
1375 continue;
1377 if (strequal(domain->name, domainname)) {
1378 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
1379 set_domain_offline(domain);
1380 /* we are in the trusted domain, set the primary domain
1381 * offline too */
1382 if (domain != primary_domain) {
1383 set_domain_offline(primary_domain);
1389 /* Deal with a request to go online. */
1391 static void child_msg_online(struct messaging_context *msg,
1392 void *private_data,
1393 uint32_t msg_type,
1394 struct server_id server_id,
1395 DATA_BLOB *data)
1397 struct winbindd_domain *domain;
1398 struct winbindd_domain *primary_domain = NULL;
1399 const char *domainname = (const char *)data->data;
1401 if (data->data == NULL || data->length == 0) {
1402 return;
1405 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
1407 if (!lp_winbind_offline_logon()) {
1408 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1409 return;
1412 primary_domain = find_our_domain();
1414 /* Set our global state as online. */
1415 set_global_winbindd_state_online();
1417 /* Try and mark everything online - delete any negative cache entries
1418 to force a reconnect now. */
1420 for (domain = domain_list(); domain; domain = domain->next) {
1421 if (domain->internal) {
1422 continue;
1424 if (strequal(domain->name, domainname)) {
1425 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
1426 winbindd_flush_negative_conn_cache(domain);
1427 set_domain_online_request(domain);
1429 /* we can be in trusted domain, which will contact primary domain
1430 * we have to bring primary domain online in trusted domain process
1431 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1432 * --> contact_domain = find_our_domain()
1433 * */
1434 if (domain != primary_domain) {
1435 winbindd_flush_negative_conn_cache(primary_domain);
1436 set_domain_online_request(primary_domain);
1442 struct winbindd_reinit_after_fork_state {
1443 const struct winbindd_child *myself;
1446 static bool winbindd_reinit_after_fork_fn(struct winbindd_child *child,
1447 void *private_data)
1449 struct winbindd_reinit_after_fork_state *state = private_data;
1451 if (child == state->myself) {
1452 return true;
1455 /* Destroy all possible events in child list. */
1456 TALLOC_FREE(child->lockout_policy_event);
1457 TALLOC_FREE(child->machine_password_change_event);
1460 * Children should never be able to send each other messages,
1461 * all messages must go through the parent.
1463 child->pid = (pid_t)0;
1466 * Close service sockets to all other children
1468 if (child->sock != -1) {
1469 close(child->sock);
1470 child->sock = -1;
1473 return true;
1476 NTSTATUS winbindd_reinit_after_fork(const struct winbindd_child *myself,
1477 const char *logfilename)
1479 struct winbindd_reinit_after_fork_state state = { .myself = myself };
1480 struct winbindd_domain *domain;
1481 NTSTATUS status;
1483 status = reinit_after_fork(
1484 server_messaging_context(),
1485 server_event_context(),
1486 true, NULL);
1487 if (!NT_STATUS_IS_OK(status)) {
1488 DEBUG(0,("reinit_after_fork() failed\n"));
1489 return status;
1492 close_conns_after_fork();
1494 if (!override_logfile && logfilename) {
1495 lp_set_logfile(logfilename);
1496 reopen_logs();
1499 if (!winbindd_setup_sig_term_handler(false))
1500 return NT_STATUS_NO_MEMORY;
1501 if (!winbindd_setup_sig_hup_handler(override_logfile ? NULL :
1502 logfilename))
1503 return NT_STATUS_NO_MEMORY;
1505 /* Stop zombies in children */
1506 CatchChild();
1508 /* Don't handle the same messages as our parent. */
1509 messaging_deregister(server_messaging_context(),
1510 MSG_SMB_CONF_UPDATED, NULL);
1511 messaging_deregister(server_messaging_context(),
1512 MSG_SHUTDOWN, NULL);
1513 messaging_deregister(server_messaging_context(),
1514 MSG_WINBIND_OFFLINE, NULL);
1515 messaging_deregister(server_messaging_context(),
1516 MSG_WINBIND_ONLINE, NULL);
1517 messaging_deregister(server_messaging_context(),
1518 MSG_WINBIND_ONLINESTATUS, NULL);
1519 messaging_deregister(server_messaging_context(),
1520 MSG_WINBIND_DUMP_DOMAIN_LIST, NULL);
1521 messaging_deregister(server_messaging_context(),
1522 MSG_DEBUG, NULL);
1524 messaging_deregister(server_messaging_context(),
1525 MSG_WINBIND_DOMAIN_OFFLINE, NULL);
1526 messaging_deregister(server_messaging_context(),
1527 MSG_WINBIND_DOMAIN_ONLINE, NULL);
1529 /* We have destroyed all events in the winbindd_event_context
1530 * in reinit_after_fork(), so clean out all possible pending
1531 * event pointers. */
1533 /* Deal with check_online_events. */
1535 for (domain = domain_list(); domain; domain = domain->next) {
1536 TALLOC_FREE(domain->check_online_event);
1539 /* Ensure we're not handling a credential cache event inherited
1540 * from our parent. */
1542 ccache_remove_all_after_fork();
1544 forall_children(winbindd_reinit_after_fork_fn, &state);
1546 return NT_STATUS_OK;
1550 * In a child there will be only one domain, reference that here.
1552 static struct winbindd_domain *child_domain;
1554 struct winbindd_domain *wb_child_domain(void)
1556 return child_domain;
1559 struct child_handler_state {
1560 struct winbindd_child *child;
1561 struct winbindd_cli_state cli;
1564 static void child_handler(struct tevent_context *ev, struct tevent_fd *fde,
1565 uint16_t flags, void *private_data)
1567 struct child_handler_state *state =
1568 (struct child_handler_state *)private_data;
1569 NTSTATUS status;
1571 /* fetch a request from the main daemon */
1572 status = child_read_request(state->cli.sock, state->cli.request);
1574 if (!NT_STATUS_IS_OK(status)) {
1575 /* we lost contact with our parent */
1576 _exit(0);
1579 DEBUG(4,("child daemon request %d\n",
1580 (int)state->cli.request->cmd));
1582 ZERO_STRUCTP(state->cli.response);
1583 state->cli.request->null_term = '\0';
1584 state->cli.mem_ctx = talloc_tos();
1585 child_process_request(state->child, &state->cli);
1587 DEBUG(4, ("Finished processing child request %d\n",
1588 (int)state->cli.request->cmd));
1590 SAFE_FREE(state->cli.request->extra_data.data);
1592 status = child_write_response(state->cli.sock, state->cli.response);
1593 if (!NT_STATUS_IS_OK(status)) {
1594 exit(1);
1598 static bool fork_domain_child(struct winbindd_child *child)
1600 int fdpair[2];
1601 struct child_handler_state state;
1602 struct winbindd_request request;
1603 struct winbindd_response response;
1604 struct winbindd_domain *primary_domain = NULL;
1605 NTSTATUS status;
1606 ssize_t nwritten;
1607 struct tevent_fd *fde;
1609 if (child->domain) {
1610 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1611 child->domain->name));
1612 } else {
1613 DEBUG(10, ("fork_domain_child called without domain.\n"));
1616 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
1617 DEBUG(0, ("Could not open child pipe: %s\n",
1618 strerror(errno)));
1619 return False;
1622 ZERO_STRUCT(state);
1623 state.child = child;
1624 state.cli.pid = getpid();
1625 state.cli.request = &request;
1626 state.cli.response = &response;
1628 child->pid = fork();
1630 if (child->pid == -1) {
1631 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
1632 close(fdpair[0]);
1633 close(fdpair[1]);
1634 return False;
1637 if (child->pid != 0) {
1638 /* Parent */
1639 ssize_t nread;
1641 close(fdpair[0]);
1643 nread = sys_read(fdpair[1], &status, sizeof(status));
1644 if (nread != sizeof(status)) {
1645 DEBUG(1, ("fork_domain_child: Could not read child status: "
1646 "nread=%d, error=%s\n", (int)nread,
1647 strerror(errno)));
1648 close(fdpair[1]);
1649 return false;
1651 if (!NT_STATUS_IS_OK(status)) {
1652 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1653 nt_errstr(status)));
1654 close(fdpair[1]);
1655 return false;
1658 child->monitor_fde = tevent_add_fd(server_event_context(),
1659 server_event_context(),
1660 fdpair[1],
1661 TEVENT_FD_READ,
1662 child_socket_readable,
1663 child);
1664 if (child->monitor_fde == NULL) {
1665 DBG_WARNING("tevent_add_fd failed\n");
1666 close(fdpair[1]);
1667 return false;
1670 child->sock = fdpair[1];
1671 return True;
1674 /* Child */
1675 child_domain = child->domain;
1677 DEBUG(10, ("Child process %d\n", (int)getpid()));
1679 state.cli.sock = fdpair[0];
1680 close(fdpair[1]);
1682 status = winbindd_reinit_after_fork(child, child->logfilename);
1684 nwritten = sys_write(state.cli.sock, &status, sizeof(status));
1685 if (nwritten != sizeof(status)) {
1686 DEBUG(1, ("fork_domain_child: Could not write status: "
1687 "nwritten=%d, error=%s\n", (int)nwritten,
1688 strerror(errno)));
1689 _exit(0);
1691 if (!NT_STATUS_IS_OK(status)) {
1692 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1693 nt_errstr(status)));
1694 _exit(0);
1697 if (child_domain != NULL) {
1698 setproctitle("domain child [%s]", child_domain->name);
1699 } else if (child == idmap_child()) {
1700 setproctitle("idmap child");
1703 /* Handle online/offline messages. */
1704 messaging_register(server_messaging_context(), NULL,
1705 MSG_WINBIND_OFFLINE, child_msg_offline);
1706 messaging_register(server_messaging_context(), NULL,
1707 MSG_WINBIND_ONLINE, child_msg_online);
1708 messaging_register(server_messaging_context(), NULL,
1709 MSG_DEBUG, debug_message);
1710 messaging_register(server_messaging_context(), NULL,
1711 MSG_WINBIND_IP_DROPPED,
1712 winbind_msg_ip_dropped);
1715 primary_domain = find_our_domain();
1717 if (primary_domain == NULL) {
1718 smb_panic("no primary domain found");
1721 /* It doesn't matter if we allow cache login,
1722 * try to bring domain online after fork. */
1723 if ( child->domain ) {
1724 child->domain->startup = True;
1725 child->domain->startup_time = time_mono(NULL);
1726 /* we can be in primary domain or in trusted domain
1727 * If we are in trusted domain, set the primary domain
1728 * in start-up mode */
1729 if (!(child->domain->internal)) {
1730 set_domain_online_request(child->domain);
1731 if (!(child->domain->primary)) {
1732 primary_domain->startup = True;
1733 primary_domain->startup_time = time_mono(NULL);
1734 set_domain_online_request(primary_domain);
1740 * We are in idmap child, make sure that we set the
1741 * check_online_event to bring primary domain online.
1743 if (child == idmap_child()) {
1744 set_domain_online_request(primary_domain);
1747 /* We might be in the idmap child...*/
1748 if (child->domain && !(child->domain->internal) &&
1749 lp_winbind_offline_logon()) {
1751 set_domain_online_request(child->domain);
1753 if (primary_domain && (primary_domain != child->domain)) {
1754 /* We need to talk to the primary
1755 * domain as well as the trusted
1756 * domain inside a trusted domain
1757 * child.
1758 * See the code in :
1759 * set_dc_type_and_flags_trustinfo()
1760 * for details.
1762 set_domain_online_request(primary_domain);
1765 child->lockout_policy_event = tevent_add_timer(
1766 server_event_context(), NULL, timeval_zero(),
1767 account_lockout_policy_handler,
1768 child);
1771 if (child->domain && child->domain->primary &&
1772 !USE_KERBEROS_KEYTAB &&
1773 lp_server_role() == ROLE_DOMAIN_MEMBER) {
1775 struct timeval next_change;
1777 if (calculate_next_machine_pwd_change(child->domain->name,
1778 &next_change)) {
1779 child->machine_password_change_event = tevent_add_timer(
1780 server_event_context(), NULL, next_change,
1781 machine_password_change_handler,
1782 child);
1786 fde = tevent_add_fd(server_event_context(), NULL, state.cli.sock,
1787 TEVENT_FD_READ, child_handler, &state);
1788 if (fde == NULL) {
1789 DEBUG(1, ("tevent_add_fd failed\n"));
1790 _exit(1);
1793 while (1) {
1795 int ret;
1796 TALLOC_CTX *frame = talloc_stackframe();
1798 ret = tevent_loop_once(server_event_context());
1799 if (ret != 0) {
1800 DEBUG(1, ("tevent_loop_once failed: %s\n",
1801 strerror(errno)));
1802 _exit(1);
1805 if (child->domain && child->domain->startup &&
1806 (time_mono(NULL) > child->domain->startup_time + 30)) {
1807 /* No longer in "startup" mode. */
1808 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1809 child->domain->name ));
1810 child->domain->startup = False;
1813 TALLOC_FREE(frame);
1817 void winbind_msg_ip_dropped_parent(struct messaging_context *msg_ctx,
1818 void *private_data,
1819 uint32_t msg_type,
1820 struct server_id server_id,
1821 DATA_BLOB *data)
1823 struct winbind_msg_relay_state state = {
1824 .msg_ctx = msg_ctx,
1825 .msg_type = msg_type,
1826 .data = data,
1829 winbind_msg_ip_dropped(msg_ctx, private_data, msg_type,
1830 server_id, data);
1832 forall_children(winbind_msg_relay_fn, &state);