2 Unix SMB/CIFS implementation.
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.
32 #include "rpc_client/rpc_client.h"
33 #include "nsswitch/wb_reqtrans.h"
35 #include "../lib/util/select.h"
36 #include "winbindd_traceid.h"
37 #include "../libcli/security/security.h"
38 #include "system/select.h"
40 #include "../lib/util/tevent_unix.h"
41 #include "lib/param/loadparm.h"
42 #include "lib/util/sys_rw.h"
43 #include "lib/util/sys_rw_data.h"
45 #include "lib/util/string_wrappers.h"
46 #include "lib/global_contexts.h"
48 #include "libcli/auth/netlogon_creds_cli.h"
49 #include "../lib/util/pidfile.h"
50 #include "librpc/gen_ndr/ndr_winbind_c.h"
51 #include "lib/util/util_process.h"
54 #define DBGC_CLASS DBGC_WINBIND
56 static void forall_domain_children(bool (*fn
)(struct winbindd_child
*c
,
60 struct winbindd_domain
*d
;
62 for (d
= domain_list(); d
!= NULL
; d
= d
->next
) {
65 for (i
= 0; i
< talloc_array_length(d
->children
); i
++) {
66 struct winbindd_child
*c
= &d
->children
[i
];
73 ok
= fn(c
, private_data
);
81 static void forall_children(bool (*fn
)(struct winbindd_child
*c
,
85 struct winbindd_child
*c
;
90 ok
= fn(c
, private_data
);
98 ok
= fn(c
, private_data
);
104 forall_domain_children(fn
, private_data
);
107 /* Read some data from a client connection */
109 static NTSTATUS
child_read_request(int sock
, struct winbindd_request
*wreq
)
113 status
= read_data_ntstatus(sock
, (char *)wreq
, sizeof(*wreq
));
114 if (!NT_STATUS_IS_OK(status
)) {
115 DEBUG(3, ("child_read_request: read_data failed: %s\n",
120 if (wreq
->extra_len
== 0) {
121 wreq
->extra_data
.data
= NULL
;
125 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq
->extra_len
));
127 wreq
->extra_data
.data
= SMB_MALLOC_ARRAY(char, wreq
->extra_len
+ 1);
128 if (wreq
->extra_data
.data
== NULL
) {
129 DEBUG(0, ("malloc failed\n"));
130 return NT_STATUS_NO_MEMORY
;
133 /* Ensure null termination */
134 wreq
->extra_data
.data
[wreq
->extra_len
] = '\0';
136 status
= read_data_ntstatus(sock
, wreq
->extra_data
.data
,
138 if (!NT_STATUS_IS_OK(status
)) {
139 DEBUG(0, ("Could not read extra data: %s\n",
145 static NTSTATUS
child_write_response(int sock
, struct winbindd_response
*wrsp
)
150 iov
[0].iov_base
= (void *)wrsp
;
151 iov
[0].iov_len
= sizeof(struct winbindd_response
);
154 if (wrsp
->length
> sizeof(struct winbindd_response
)) {
155 iov
[1].iov_base
= (void *)wrsp
->extra_data
.data
;
156 iov
[1].iov_len
= wrsp
->length
-iov
[0].iov_len
;
160 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp
->length
));
162 if (write_data_iov(sock
, iov
, iov_count
) != wrsp
->length
) {
163 DEBUG(0, ("Could not write result\n"));
164 return NT_STATUS_INVALID_HANDLE
;
171 * Do winbind child async request. This is not simply wb_simple_trans. We have
172 * to do the queueing ourselves because while a request is queued, the child
173 * might have crashed, and we have to re-fork it in the _trigger function.
176 struct wb_child_request_state
{
177 struct tevent_context
*ev
;
178 struct tevent_req
*queue_subreq
;
179 struct tevent_req
*subreq
;
180 struct winbindd_child
*child
;
181 struct winbindd_request
*request
;
182 struct winbindd_response
*response
;
185 static bool fork_domain_child(struct winbindd_child
*child
);
187 static void wb_child_request_waited(struct tevent_req
*subreq
);
188 static void wb_child_request_done(struct tevent_req
*subreq
);
189 static void wb_child_request_orphaned(struct tevent_req
*subreq
);
191 static void wb_child_request_cleanup(struct tevent_req
*req
,
192 enum tevent_req_state req_state
);
194 struct tevent_req
*wb_child_request_send(TALLOC_CTX
*mem_ctx
,
195 struct tevent_context
*ev
,
196 struct winbindd_child
*child
,
197 struct winbindd_request
*request
)
199 struct tevent_req
*req
;
200 struct wb_child_request_state
*state
;
201 struct tevent_req
*subreq
;
203 req
= tevent_req_create(mem_ctx
, &state
,
204 struct wb_child_request_state
);
210 state
->child
= child
;
213 * We have to make a copy of "request", because our caller
214 * might drop us via talloc_free().
216 * The talloc_move() magic in wb_child_request_cleanup() keeps
217 * all the requests, but if we are sitting deep within
218 * writev_send() down to the client, we have given it the
219 * pointer to "request". As our caller lost interest, it will
220 * just free "request", while writev_send still references it.
223 state
->request
= talloc_memdup(state
, request
, sizeof(*request
));
224 if (tevent_req_nomem(state
->request
, req
)) {
225 return tevent_req_post(req
, ev
);
228 state
->request
->traceid
= debug_traceid_get();
230 if (request
->extra_data
.data
!= NULL
) {
231 state
->request
->extra_data
.data
= talloc_memdup(
233 request
->extra_data
.data
,
235 if (tevent_req_nomem(state
->request
->extra_data
.data
, req
)) {
236 return tevent_req_post(req
, ev
);
240 subreq
= tevent_queue_wait_send(state
, ev
, child
->queue
);
241 if (tevent_req_nomem(subreq
, req
)) {
242 return tevent_req_post(req
, ev
);
244 tevent_req_set_callback(subreq
, wb_child_request_waited
, req
);
245 state
->queue_subreq
= subreq
;
247 tevent_req_set_cleanup_fn(req
, wb_child_request_cleanup
);
252 static void wb_child_request_waited(struct tevent_req
*subreq
)
254 struct tevent_req
*req
= tevent_req_callback_data(
255 subreq
, struct tevent_req
);
256 struct wb_child_request_state
*state
= tevent_req_data(
257 req
, struct wb_child_request_state
);
260 ok
= tevent_queue_wait_recv(subreq
);
266 * We need to keep state->queue_subreq
267 * in order to block the queue.
271 if ((state
->child
->sock
== -1) && (!fork_domain_child(state
->child
))) {
272 tevent_req_error(req
, errno
);
276 tevent_fd_set_flags(state
->child
->monitor_fde
, 0);
278 subreq
= wb_simple_trans_send(state
, global_event_context(), NULL
,
279 state
->child
->sock
, state
->request
);
280 if (tevent_req_nomem(subreq
, req
)) {
284 state
->subreq
= subreq
;
285 tevent_req_set_callback(subreq
, wb_child_request_done
, req
);
286 tevent_req_set_endtime(req
, state
->ev
, timeval_current_ofs(300, 0));
289 static void wb_child_request_done(struct tevent_req
*subreq
)
291 struct tevent_req
*req
= tevent_req_callback_data(
292 subreq
, struct tevent_req
);
293 struct wb_child_request_state
*state
= tevent_req_data(
294 req
, struct wb_child_request_state
);
297 ret
= wb_simple_trans_recv(subreq
, state
, &state
->response
, &err
);
298 /* Freeing the subrequest is deferred until the cleanup function,
299 * which has to know whether a subrequest exists, and consequently
300 * decide whether to shut down the pipe to the child process.
303 tevent_req_error(req
, err
);
306 tevent_req_done(req
);
309 static void wb_child_request_orphaned(struct tevent_req
*subreq
)
311 struct winbindd_child
*child
=
312 (struct winbindd_child
*)tevent_req_callback_data_void(subreq
);
314 DBG_WARNING("cleanup orphaned subreq[%p]\n", subreq
);
317 if (child
->domain
!= NULL
) {
319 * If the child is attached to a domain,
320 * we need to make sure the domain queue
321 * can move forward, after the orphaned
324 tevent_queue_start(child
->domain
->queue
);
328 int wb_child_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
329 struct winbindd_response
**presponse
, int *err
)
331 struct wb_child_request_state
*state
= tevent_req_data(
332 req
, struct wb_child_request_state
);
334 if (tevent_req_is_unix_error(req
, err
)) {
337 *presponse
= talloc_move(mem_ctx
, &state
->response
);
341 static void wb_child_request_cleanup(struct tevent_req
*req
,
342 enum tevent_req_state req_state
)
344 struct wb_child_request_state
*state
=
345 tevent_req_data(req
, struct wb_child_request_state
);
347 if (state
->subreq
== NULL
) {
348 /* nothing to cleanup */
352 if (req_state
== TEVENT_REQ_RECEIVED
) {
353 struct tevent_req
*subreq
= NULL
;
356 * Our caller gave up, but we need to keep
357 * the low level request (wb_simple_trans)
358 * in order to maintain the parent child protocol.
360 * We also need to keep the child queue blocked
361 * until we got the response from the child.
364 subreq
= talloc_move(state
->child
->queue
, &state
->subreq
);
365 talloc_move(subreq
, &state
->queue_subreq
);
366 talloc_move(subreq
, &state
->request
);
367 tevent_req_set_callback(subreq
,
368 wb_child_request_orphaned
,
371 DBG_WARNING("keep orphaned subreq[%p]\n", subreq
);
375 TALLOC_FREE(state
->subreq
);
376 TALLOC_FREE(state
->queue_subreq
);
378 tevent_fd_set_flags(state
->child
->monitor_fde
, TEVENT_FD_READ
);
380 if (state
->child
->domain
!= NULL
) {
382 * If the child is attached to a domain,
383 * we need to make sure the domain queue
384 * can move forward, after the request
387 tevent_queue_start(state
->child
->domain
->queue
);
390 if (req_state
== TEVENT_REQ_DONE
) {
391 /* transmitted request and got response */
396 * Failed to transmit and receive response, or request
397 * cancelled while being serviced.
398 * The basic parent/child communication broke, close
401 TALLOC_FREE(state
->child
->monitor_fde
);
402 close(state
->child
->sock
);
403 state
->child
->sock
= -1;
406 static void child_socket_readable(struct tevent_context
*ev
,
407 struct tevent_fd
*fde
,
411 struct winbindd_child
*child
= private_data
;
413 if ((flags
& TEVENT_FD_READ
) == 0) {
417 TALLOC_FREE(child
->monitor_fde
);
420 * We're only active when there is no outstanding child
421 * request. Arriving here means the child closed its socket,
422 * it died. Do the same here.
425 SMB_ASSERT(child
->sock
!= -1);
431 static struct winbindd_child
*choose_domain_child(struct winbindd_domain
*domain
)
433 struct winbindd_child
*shortest
= &domain
->children
[0];
434 struct winbindd_child
*current
;
437 for (i
=0; i
<talloc_array_length(domain
->children
); i
++) {
438 size_t shortest_len
, current_len
;
440 current
= &domain
->children
[i
];
441 current_len
= tevent_queue_length(current
->queue
);
443 if (current_len
== 0) {
448 shortest_len
= tevent_queue_length(shortest
->queue
);
450 if (current_len
< shortest_len
) {
458 struct dcerpc_binding_handle
*dom_child_handle(struct winbindd_domain
*domain
)
460 return domain
->binding_handle
;
463 struct wb_domain_request_state
{
464 struct tevent_context
*ev
;
465 struct tevent_queue_entry
*queue_entry
;
466 struct winbindd_domain
*domain
;
467 struct winbindd_child
*child
;
468 struct winbindd_request
*request
;
469 struct winbindd_request
*init_req
;
470 struct winbindd_response
*response
;
471 struct tevent_req
*pending_subreq
;
472 struct wbint_InitConnection r
;
475 static void wb_domain_request_cleanup(struct tevent_req
*req
,
476 enum tevent_req_state req_state
)
478 struct wb_domain_request_state
*state
= tevent_req_data(
479 req
, struct wb_domain_request_state
);
482 * If we're completely done or got a failure.
483 * we should remove ourself from the domain queue,
484 * after removing the child subreq from the child queue
485 * and give the next one in the queue the chance
486 * to check for an idle child.
488 TALLOC_FREE(state
->pending_subreq
);
489 TALLOC_FREE(state
->queue_entry
);
490 tevent_queue_start(state
->domain
->queue
);
493 static void wb_domain_request_trigger(struct tevent_req
*req
,
495 static void wb_domain_request_gotdc(struct tevent_req
*subreq
);
496 static void wb_domain_request_initialized(struct tevent_req
*subreq
);
497 static void wb_domain_request_done(struct tevent_req
*subreq
);
499 struct tevent_req
*wb_domain_request_send(TALLOC_CTX
*mem_ctx
,
500 struct tevent_context
*ev
,
501 struct winbindd_domain
*domain
,
502 struct winbindd_request
*request
)
504 struct tevent_req
*req
;
505 struct wb_domain_request_state
*state
;
507 req
= tevent_req_create(mem_ctx
, &state
,
508 struct wb_domain_request_state
);
513 state
->domain
= domain
;
515 state
->request
= request
;
517 tevent_req_set_cleanup_fn(req
, wb_domain_request_cleanup
);
519 state
->queue_entry
= tevent_queue_add_entry(
520 domain
->queue
, state
->ev
, req
,
521 wb_domain_request_trigger
, NULL
);
522 if (tevent_req_nomem(state
->queue_entry
, req
)) {
523 return tevent_req_post(req
, ev
);
529 static void wb_domain_request_trigger(struct tevent_req
*req
,
532 struct wb_domain_request_state
*state
= tevent_req_data(
533 req
, struct wb_domain_request_state
);
534 struct winbindd_domain
*domain
= state
->domain
;
535 struct tevent_req
*subreq
= NULL
;
536 size_t shortest_queue_length
;
538 state
->child
= choose_domain_child(domain
);
539 shortest_queue_length
= tevent_queue_length(state
->child
->queue
);
540 if (shortest_queue_length
> 0) {
542 * All children are busy, we need to stop
543 * the queue and untrigger our own queue
544 * entry. Once a pending request
545 * is done it calls tevent_queue_start
546 * and we get retriggered.
549 tevent_queue_stop(state
->domain
->queue
);
550 tevent_queue_entry_untrigger(state
->queue_entry
);
554 if (domain
->initialized
) {
555 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
557 if (tevent_req_nomem(subreq
, req
)) {
560 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
561 state
->pending_subreq
= subreq
;
564 * Once the domain is initialized and
565 * once we placed our real request into the child queue,
566 * we can remove ourself from the domain queue
567 * and give the next one in the queue the chance
568 * to check for an idle child.
570 TALLOC_FREE(state
->queue_entry
);
574 state
->init_req
= talloc_zero(state
, struct winbindd_request
);
575 if (tevent_req_nomem(state
->init_req
, req
)) {
579 if (IS_DC
|| domain
->primary
|| domain
->internal
) {
580 /* The primary domain has to find the DC name itself */
581 state
->r
.in
.dcname
= talloc_strdup(state
, "");
582 if (tevent_req_nomem(state
->r
.in
.dcname
, req
)) {
586 subreq
= dcerpc_wbint_InitConnection_r_send(state
,
588 state
->child
->binding_handle
,
590 if (tevent_req_nomem(subreq
, req
)) {
593 tevent_req_set_callback(subreq
, wb_domain_request_initialized
,
595 state
->pending_subreq
= subreq
;
600 * This is *not* the primary domain,
601 * let's ask our DC about a DC name.
603 * We prefer getting a dns name in dc_unc,
604 * which is indicated by DS_RETURN_DNS_NAME.
605 * For NT4 domains we still get the netbios name.
607 subreq
= wb_dsgetdcname_send(state
, state
->ev
,
609 NULL
, /* domain_guid */
610 NULL
, /* site_name */
611 DS_RETURN_DNS_NAME
); /* flags */
612 if (tevent_req_nomem(subreq
, req
)) {
615 tevent_req_set_callback(subreq
, wb_domain_request_gotdc
, req
);
616 state
->pending_subreq
= subreq
;
620 static void wb_domain_request_gotdc(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 netr_DsRGetDCNameInfo
*dcinfo
= NULL
;
628 const char *dcname
= NULL
;
630 state
->pending_subreq
= NULL
;
632 status
= wb_dsgetdcname_recv(subreq
, state
, &dcinfo
);
634 if (tevent_req_nterror(req
, status
)) {
637 dcname
= dcinfo
->dc_unc
;
638 while (dcname
!= NULL
&& *dcname
== '\\') {
642 state
->r
.in
.dcname
= talloc_strdup(state
, dcname
);
643 if (tevent_req_nomem(state
->r
.in
.dcname
, req
)) {
649 subreq
= dcerpc_wbint_InitConnection_r_send(state
,
651 state
->child
->binding_handle
,
653 if (tevent_req_nomem(subreq
, req
)) {
656 tevent_req_set_callback(subreq
, wb_domain_request_initialized
, req
);
657 state
->pending_subreq
= subreq
;
660 static void wb_domain_request_initialized(struct tevent_req
*subreq
)
662 struct tevent_req
*req
= tevent_req_callback_data(
663 subreq
, struct tevent_req
);
664 struct wb_domain_request_state
*state
= tevent_req_data(
665 req
, struct wb_domain_request_state
);
668 state
->pending_subreq
= NULL
;
670 status
= dcerpc_wbint_InitConnection_r_recv(subreq
, state
);
672 if (NT_STATUS_IS_ERR(status
)) {
673 tevent_req_error(req
, map_errno_from_nt_status(status
));
677 status
= state
->r
.out
.result
;
678 if (NT_STATUS_IS_ERR(status
)) {
679 tevent_req_error(req
, map_errno_from_nt_status(status
));
683 state
->domain
->sid
= *state
->r
.out
.sid
;
685 talloc_free(state
->domain
->name
);
686 state
->domain
->name
= talloc_strdup(state
->domain
, *state
->r
.out
.name
);
687 if (state
->domain
->name
== NULL
) {
688 tevent_req_error(req
, ENOMEM
);
692 if (*state
->r
.out
.alt_name
!= NULL
&&
693 strlen(*state
->r
.out
.alt_name
) > 0) {
694 talloc_free(state
->domain
->alt_name
);
696 state
->domain
->alt_name
= talloc_strdup(state
->domain
,
697 *state
->r
.out
.alt_name
);
698 if (state
->domain
->alt_name
== NULL
) {
699 tevent_req_error(req
, ENOMEM
);
704 state
->domain
->native_mode
=
705 (*state
->r
.out
.flags
& WB_DOMINFO_DOMAIN_NATIVE
);
706 state
->domain
->active_directory
=
707 (*state
->r
.out
.flags
& WB_DOMINFO_DOMAIN_AD
);
708 state
->domain
->initialized
= true;
710 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
712 if (tevent_req_nomem(subreq
, req
)) {
715 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
716 state
->pending_subreq
= subreq
;
719 * Once the domain is initialized and
720 * once we placed our real request into the child queue,
721 * we can remove ourself from the domain queue
722 * and give the next one in the queue the chance
723 * to check for an idle child.
725 TALLOC_FREE(state
->queue_entry
);
728 static void wb_domain_request_done(struct tevent_req
*subreq
)
730 struct tevent_req
*req
= tevent_req_callback_data(
731 subreq
, struct tevent_req
);
732 struct wb_domain_request_state
*state
= tevent_req_data(
733 req
, struct wb_domain_request_state
);
736 state
->pending_subreq
= NULL
;
738 ret
= wb_child_request_recv(subreq
, talloc_tos(), &state
->response
,
742 tevent_req_error(req
, err
);
745 tevent_req_done(req
);
748 int wb_domain_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
749 struct winbindd_response
**presponse
, int *err
)
751 struct wb_domain_request_state
*state
= tevent_req_data(
752 req
, struct wb_domain_request_state
);
754 if (tevent_req_is_unix_error(req
, err
)) {
757 *presponse
= talloc_move(mem_ctx
, &state
->response
);
761 static void child_process_request(struct winbindd_child
*child
,
762 struct winbindd_cli_state
*state
)
764 struct winbindd_domain
*domain
= child
->domain
;
766 /* Free response data - we may be interrupted and receive another
767 command before being able to send this data off. */
769 state
->response
->result
= WINBINDD_ERROR
;
770 state
->response
->length
= sizeof(struct winbindd_response
);
772 /* as all requests in the child are sync, we can use talloc_tos() */
773 state
->mem_ctx
= talloc_tos();
775 /* Process command */
776 state
->response
->result
= winbindd_dual_ndrcmd(domain
, state
);
779 void setup_child(struct winbindd_domain
*domain
, struct winbindd_child
*child
,
780 const char *logprefix
,
783 const struct loadparm_substitution
*lp_sub
=
784 loadparm_s3_global_substitution();
786 if (logprefix
&& logname
) {
787 char *logbase
= NULL
;
789 if (*lp_logfile(talloc_tos(), lp_sub
)) {
792 if (asprintf(&logbase
, "%s", lp_logfile(talloc_tos(), lp_sub
)) < 0) {
793 smb_panic("Internal error: asprintf failed");
796 if ((end
= strrchr_m(logbase
, '/'))) {
800 if (asprintf(&logbase
, "%s", get_dyn_LOGFILEBASE()) < 0) {
801 smb_panic("Internal error: asprintf failed");
805 if (asprintf(&child
->logfilename
, "%s/%s-%s",
806 logbase
, logprefix
, logname
) < 0) {
808 smb_panic("Internal error: asprintf failed");
813 smb_panic("Internal error: logprefix == NULL && "
819 child
->domain
= domain
;
820 child
->queue
= tevent_queue_create(NULL
, "winbind_child");
821 SMB_ASSERT(child
->queue
!= NULL
);
823 child
->binding_handle
= wbint_binding_handle(NULL
, NULL
, child
);
824 SMB_ASSERT(child
->binding_handle
!= NULL
);
827 struct winbind_child_died_state
{
829 struct winbindd_child
*child
;
832 static bool winbind_child_died_fn(struct winbindd_child
*child
,
835 struct winbind_child_died_state
*state
= private_data
;
837 if (child
->pid
== state
->pid
) {
838 state
->child
= child
;
844 void winbind_child_died(pid_t pid
)
846 struct winbind_child_died_state state
= { .pid
= pid
};
848 forall_children(winbind_child_died_fn
, &state
);
850 if (state
.child
== NULL
) {
851 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
855 state
.child
->pid
= 0;
858 /* Ensure any negative cache entries with the netbios or realm names are removed. */
860 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
862 flush_negative_conn_cache_for_domain(domain
->name
);
863 if (domain
->alt_name
!= NULL
) {
864 flush_negative_conn_cache_for_domain(domain
->alt_name
);
869 * Parent winbindd process sets its own debug level first and then
870 * sends a message to all the winbindd children to adjust their debug
871 * level to that of parents.
874 struct winbind_msg_relay_state
{
875 struct messaging_context
*msg_ctx
;
880 static bool winbind_msg_relay_fn(struct winbindd_child
*child
,
883 struct winbind_msg_relay_state
*state
= private_data
;
885 DBG_DEBUG("sending message to pid %u.\n",
886 (unsigned int)child
->pid
);
888 messaging_send(state
->msg_ctx
, pid_to_procid(child
->pid
),
889 state
->msg_type
, state
->data
);
893 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
896 struct server_id server_id
,
899 struct winbind_msg_relay_state state
= {
900 .msg_ctx
= msg_ctx
, .msg_type
= msg_type
, .data
= data
903 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
905 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
907 forall_children(winbind_msg_relay_fn
, &state
);
910 void winbind_disconnect_dc_parent(struct messaging_context
*msg_ctx
,
913 struct server_id server_id
,
916 struct winbind_msg_relay_state state
= {
917 .msg_ctx
= msg_ctx
, .msg_type
= msg_type
, .data
= data
920 DBG_DEBUG("Got disconnect_dc message\n");
922 forall_children(winbind_msg_relay_fn
, &state
);
925 static bool winbindd_child_msg_filter(struct messaging_rec
*rec
,
928 struct winbindd_child
*child
= talloc_get_type_abort(private_data
,
929 struct winbindd_child
);
931 if (rec
->msg_type
== MSG_SMB_CONF_UPDATED
) {
932 DBG_DEBUG("Got reload-config message\n");
933 winbindd_reload_services_file(child
->logfilename
);
939 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
940 void winbindd_msg_reload_services_parent(struct messaging_context
*msg
,
943 struct server_id server_id
,
946 struct winbind_msg_relay_state state
= {
948 .msg_type
= msg_type
,
953 DBG_DEBUG("Got reload-config message\n");
955 /* Flush various caches */
956 winbindd_flush_caches();
958 winbindd_reload_services_file((const char *)private_data
);
960 /* Set tevent_thread_call_depth_set_callback according to debug level */
961 if (lp_winbind_debug_traceid() && debuglevel_get() > 1) {
962 tevent_thread_call_depth_set_callback(winbind_call_flow
, NULL
);
964 tevent_thread_call_depth_set_callback(NULL
, NULL
);
967 ok
= add_trusted_domains_dc();
969 DBG_ERR("add_trusted_domains_dc() failed\n");
972 forall_children(winbind_msg_relay_fn
, &state
);
975 /* Set our domains as offline and forward the offline message to our children. */
977 struct winbind_msg_on_offline_state
{
978 struct messaging_context
*msg_ctx
;
982 static bool winbind_msg_on_offline_fn(struct winbindd_child
*child
,
985 struct winbind_msg_on_offline_state
*state
= private_data
;
987 if (child
->domain
->internal
) {
992 * Each winbindd child should only process requests for one
993 * domain - make sure we only set it online / offline for that
996 DBG_DEBUG("sending message to pid %u for domain %s.\n",
997 (unsigned int)child
->pid
, child
->domain
->name
);
999 messaging_send_buf(state
->msg_ctx
,
1000 pid_to_procid(child
->pid
),
1002 (const uint8_t *)child
->domain
->name
,
1003 strlen(child
->domain
->name
)+1);
1008 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
1011 struct server_id server_id
,
1014 struct winbind_msg_on_offline_state state
= {
1016 .msg_type
= MSG_WINBIND_OFFLINE
,
1018 struct winbindd_domain
*domain
;
1020 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
1022 if (!lp_winbind_offline_logon()) {
1023 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
1027 /* Set our global state as offline. */
1028 if (!set_global_winbindd_state_offline()) {
1029 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
1033 /* Set all our domains as offline. */
1034 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1035 if (domain
->internal
) {
1038 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
1039 domain
->online
= false;
1042 forall_domain_children(winbind_msg_on_offline_fn
, &state
);
1045 /* Set our domains as online and forward the online message to our children. */
1047 void winbind_msg_online(struct messaging_context
*msg_ctx
,
1050 struct server_id server_id
,
1053 struct winbind_msg_on_offline_state state
= {
1055 .msg_type
= MSG_WINBIND_ONLINE
,
1058 DEBUG(10,("winbind_msg_online: got online message.\n"));
1060 if (!lp_winbind_offline_logon()) {
1061 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
1065 /* Set our global state as online. */
1066 set_global_winbindd_state_online();
1068 smb_nscd_flush_user_cache();
1069 smb_nscd_flush_group_cache();
1071 /* Tell all our child domains to go online online. */
1072 forall_domain_children(winbind_msg_on_offline_fn
, &state
);
1075 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
1077 struct winbindd_domain
*domain
;
1080 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
1081 get_global_winbindd_state_offline() ?
1082 "Offline":"Online")) == NULL
) {
1086 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1087 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
1090 "Online":"Offline")) == NULL
) {
1095 buf
= talloc_asprintf_append_buffer(buf
, "\n");
1097 DEBUG(5,("collect_onlinestatus: %s", buf
));
1102 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
1105 struct server_id server_id
,
1108 TALLOC_CTX
*mem_ctx
;
1109 const char *message
;
1111 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1113 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
1114 if (mem_ctx
== NULL
) {
1118 message
= collect_onlinestatus(mem_ctx
);
1119 if (message
== NULL
) {
1120 talloc_destroy(mem_ctx
);
1124 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_ONLINESTATUS
,
1125 (const uint8_t *)message
, strlen(message
) + 1);
1127 talloc_destroy(mem_ctx
);
1130 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
1133 struct server_id server_id
,
1136 TALLOC_CTX
*mem_ctx
;
1137 const char *message
= NULL
;
1138 const char *domain
= NULL
;
1141 struct winbindd_domain
*dom
= NULL
;
1143 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
1145 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
1150 if (data
->length
> 0) {
1151 domain
= (const char *)data
->data
;
1156 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
1159 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
1160 find_domain_from_name_noinit(domain
));
1162 talloc_destroy(mem_ctx
);
1166 messaging_send_buf(msg_ctx
, server_id
,
1167 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1168 (const uint8_t *)message
, strlen(message
) + 1);
1170 talloc_destroy(mem_ctx
);
1175 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
1177 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
1178 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
1180 talloc_destroy(mem_ctx
);
1184 s
= talloc_asprintf_append(s
, "%s\n", message
);
1186 talloc_destroy(mem_ctx
);
1191 status
= messaging_send_buf(msg_ctx
, server_id
,
1192 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1193 (uint8_t *)s
, strlen(s
) + 1);
1194 if (!NT_STATUS_IS_OK(status
)) {
1195 DEBUG(0,("failed to send message: %s\n",
1196 nt_errstr(status
)));
1199 talloc_destroy(mem_ctx
);
1202 static void account_lockout_policy_handler(struct tevent_context
*ctx
,
1203 struct tevent_timer
*te
,
1207 struct winbindd_child
*child
=
1208 (struct winbindd_child
*)private_data
;
1209 TALLOC_CTX
*mem_ctx
= NULL
;
1210 struct samr_DomInfo12 lockout_policy
;
1213 DEBUG(10,("account_lockout_policy_handler called\n"));
1215 TALLOC_FREE(child
->lockout_policy_event
);
1217 if ( !winbindd_can_contact_domain( child
->domain
) ) {
1218 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
1219 "do not have an incoming trust to domain %s\n",
1220 child
->domain
->name
));
1225 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
1227 result
= NT_STATUS_NO_MEMORY
;
1229 result
= wb_cache_lockout_policy(child
->domain
, mem_ctx
,
1232 TALLOC_FREE(mem_ctx
);
1234 if (!NT_STATUS_IS_OK(result
)) {
1235 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
1236 nt_errstr(result
)));
1239 child
->lockout_policy_event
= tevent_add_timer(global_event_context(), NULL
,
1240 timeval_current_ofs(3600, 0),
1241 account_lockout_policy_handler
,
1245 static time_t get_machine_password_timeout(void)
1247 /* until we have gpo support use lp setting */
1248 return lp_machine_password_timeout();
1251 static bool calculate_next_machine_pwd_change(const char *domain
,
1254 time_t pass_last_set_time
;
1260 pw
= secrets_fetch_machine_password(domain
,
1261 &pass_last_set_time
,
1265 DEBUG(0,("cannot fetch own machine password ????\n"));
1271 timeout
= get_machine_password_timeout();
1273 DEBUG(10,("machine password never expires\n"));
1277 tv
.tv_sec
= pass_last_set_time
;
1278 DEBUG(10, ("password last changed %s\n",
1279 timeval_string(talloc_tos(), &tv
, false)));
1280 tv
.tv_sec
+= timeout
;
1281 DEBUGADD(10, ("password valid until %s\n",
1282 timeval_string(talloc_tos(), &tv
, false)));
1284 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
1285 next_change
= pass_last_set_time
+ timeout
;
1286 DEBUG(10,("machine password still valid until: %s\n",
1287 http_timestring(talloc_tos(), next_change
)));
1288 *t
= tevent_timeval_set(next_change
, 0);
1290 if (lp_clustering()) {
1293 * When having a cluster, we have several
1294 * winbinds racing for the password change. In
1295 * the machine_password_change_handler()
1296 * function we check if someone else was
1297 * faster when the event triggers. We add a
1298 * 255-second random delay here, so that we
1299 * don't run to change the password at the
1300 * exact same moment.
1302 generate_random_buffer(&randbuf
, sizeof(randbuf
));
1303 DEBUG(10, ("adding %d seconds randomness\n",
1305 t
->tv_sec
+= randbuf
;
1310 DEBUG(10,("machine password expired, needs immediate change\n"));
1312 *t
= timeval_zero();
1317 static void machine_password_change_handler(struct tevent_context
*ctx
,
1318 struct tevent_timer
*te
,
1322 struct messaging_context
*msg_ctx
= global_messaging_context();
1323 struct winbindd_child
*child
=
1324 (struct winbindd_child
*)private_data
;
1325 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1326 struct netlogon_creds_cli_context
*netlogon_creds_ctx
= NULL
;
1328 struct timeval next_change
;
1330 DEBUG(10,("machine_password_change_handler called\n"));
1332 TALLOC_FREE(child
->machine_password_change_event
);
1334 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1336 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1340 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1341 timeval_string(talloc_tos(), &next_change
, false)));
1343 if (!timeval_expired(&next_change
)) {
1344 DEBUG(10, ("Someone else has already changed the pw\n"));
1348 if (!winbindd_can_contact_domain(child
->domain
)) {
1349 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1350 "do not have an incoming trust to domain %s\n",
1351 child
->domain
->name
));
1355 result
= cm_connect_netlogon_secure(child
->domain
,
1357 &netlogon_creds_ctx
);
1358 if (!NT_STATUS_IS_OK(result
)) {
1359 DEBUG(10,("machine_password_change_handler: "
1360 "failed to connect netlogon pipe: %s\n",
1361 nt_errstr(result
)));
1365 result
= trust_pw_change(netlogon_creds_ctx
,
1367 netlogon_pipe
->binding_handle
,
1368 child
->domain
->name
,
1369 child
->domain
->dcname
,
1372 DEBUG(10, ("machine_password_change_handler: "
1373 "trust_pw_change returned %s\n",
1374 nt_errstr(result
)));
1376 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1377 DEBUG(3,("machine_password_change_handler: password set returned "
1378 "ACCESS_DENIED. Maybe the trust account "
1379 "password was changed and we didn't know it. "
1380 "Killing connections to domain %s\n",
1381 child
->domain
->name
));
1382 invalidate_cm_connection(child
->domain
);
1385 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1387 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1391 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1392 timeval_string(talloc_tos(), &next_change
, false)));
1394 if (!NT_STATUS_IS_OK(result
)) {
1397 * In case of failure, give the DC a minute to recover
1399 tmp
= timeval_current_ofs(60, 0);
1400 next_change
= timeval_max(&next_change
, &tmp
);
1404 child
->machine_password_change_event
= tevent_add_timer(global_event_context(), NULL
,
1406 machine_password_change_handler
,
1410 /* Deal with a request to go offline. */
1412 static void child_msg_offline(struct messaging_context
*msg
,
1415 struct server_id server_id
,
1418 struct winbindd_domain
*domain
;
1419 struct winbindd_domain
*primary_domain
= NULL
;
1420 const char *domainname
= (const char *)data
->data
;
1422 if (data
->data
== NULL
|| data
->length
== 0) {
1426 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
1428 if (!lp_winbind_offline_logon()) {
1429 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1433 primary_domain
= find_our_domain();
1435 /* Mark the requested domain offline. */
1437 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1438 if (domain
->internal
) {
1441 if (strequal(domain
->name
, domainname
)) {
1442 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1443 set_domain_offline(domain
);
1444 /* we are in the trusted domain, set the primary domain
1446 if (domain
!= primary_domain
) {
1447 set_domain_offline(primary_domain
);
1453 /* Deal with a request to go online. */
1455 static void child_msg_online(struct messaging_context
*msg
,
1458 struct server_id server_id
,
1461 struct winbindd_domain
*domain
;
1462 struct winbindd_domain
*primary_domain
= NULL
;
1463 const char *domainname
= (const char *)data
->data
;
1465 if (data
->data
== NULL
|| data
->length
== 0) {
1469 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1471 if (!lp_winbind_offline_logon()) {
1472 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1476 primary_domain
= find_our_domain();
1478 /* Set our global state as online. */
1479 set_global_winbindd_state_online();
1481 /* Try and mark everything online - delete any negative cache entries
1482 to force a reconnect now. */
1484 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1485 if (domain
->internal
) {
1488 if (strequal(domain
->name
, domainname
)) {
1489 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1490 winbindd_flush_negative_conn_cache(domain
);
1491 set_domain_online_request(domain
);
1493 /* we can be in trusted domain, which will contact primary domain
1494 * we have to bring primary domain online in trusted domain process
1495 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1496 * --> contact_domain = find_our_domain()
1498 if (domain
!= primary_domain
) {
1499 winbindd_flush_negative_conn_cache(primary_domain
);
1500 set_domain_online_request(primary_domain
);
1506 struct winbindd_reinit_after_fork_state
{
1507 const struct winbindd_child
*myself
;
1510 static bool winbindd_reinit_after_fork_fn(struct winbindd_child
*child
,
1513 struct winbindd_reinit_after_fork_state
*state
= private_data
;
1515 if (child
== state
->myself
) {
1519 /* Destroy all possible events in child list. */
1520 TALLOC_FREE(child
->lockout_policy_event
);
1521 TALLOC_FREE(child
->machine_password_change_event
);
1524 * Children should never be able to send each other messages,
1525 * all messages must go through the parent.
1527 child
->pid
= (pid_t
)0;
1530 * Close service sockets to all other children
1532 if (child
->sock
!= -1) {
1540 NTSTATUS
winbindd_reinit_after_fork(const struct winbindd_child
*myself
,
1541 const char *logfilename
)
1543 struct winbindd_reinit_after_fork_state state
= { .myself
= myself
};
1544 struct winbindd_domain
*domain
;
1547 status
= reinit_after_fork(
1548 global_messaging_context(),
1549 global_event_context(),
1551 if (!NT_STATUS_IS_OK(status
)) {
1552 DEBUG(0,("reinit_after_fork() failed\n"));
1555 initialize_password_db(true, global_event_context());
1557 close_conns_after_fork();
1559 if (logfilename
!= NULL
) {
1560 lp_set_logfile(logfilename
);
1564 if (!winbindd_setup_sig_term_handler(false)) {
1565 return NT_STATUS_NO_MEMORY
;
1568 if (!winbindd_setup_sig_hup_handler(logfilename
)) {
1569 return NT_STATUS_NO_MEMORY
;
1572 /* Stop zombies in children */
1575 /* Don't handle the same messages as our parent. */
1576 messaging_deregister(global_messaging_context(),
1577 MSG_SMB_CONF_UPDATED
, NULL
);
1578 messaging_deregister(global_messaging_context(),
1579 MSG_SHUTDOWN
, NULL
);
1580 messaging_deregister(global_messaging_context(),
1581 MSG_WINBIND_OFFLINE
, NULL
);
1582 messaging_deregister(global_messaging_context(),
1583 MSG_WINBIND_ONLINE
, NULL
);
1584 messaging_deregister(global_messaging_context(),
1585 MSG_WINBIND_ONLINESTATUS
, NULL
);
1586 messaging_deregister(global_messaging_context(),
1587 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1588 messaging_deregister(global_messaging_context(),
1591 messaging_deregister(global_messaging_context(),
1592 MSG_WINBIND_DOMAIN_OFFLINE
, NULL
);
1593 messaging_deregister(global_messaging_context(),
1594 MSG_WINBIND_DOMAIN_ONLINE
, NULL
);
1596 /* We have destroyed all events in the winbindd_event_context
1597 * in reinit_after_fork(), so clean out all possible pending
1598 * event pointers. */
1600 /* Deal with check_online_events. */
1602 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1603 TALLOC_FREE(domain
->check_online_event
);
1606 /* Ensure we're not handling a credential cache event inherited
1607 * from our parent. */
1609 ccache_remove_all_after_fork();
1611 forall_children(winbindd_reinit_after_fork_fn
, &state
);
1613 return NT_STATUS_OK
;
1617 * In a child there will be only one domain, reference that here.
1619 static struct winbindd_domain
*child_domain
;
1621 struct winbindd_domain
*wb_child_domain(void)
1623 return child_domain
;
1626 struct child_handler_state
{
1627 struct winbindd_child
*child
;
1628 struct winbindd_cli_state cli
;
1631 static void child_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
1632 uint16_t flags
, void *private_data
)
1634 struct child_handler_state
*state
=
1635 (struct child_handler_state
*)private_data
;
1637 uint64_t parent_traceid
;
1639 /* fetch a request from the main daemon */
1640 status
= child_read_request(state
->cli
.sock
, state
->cli
.request
);
1642 if (!NT_STATUS_IS_OK(status
)) {
1643 /* we lost contact with our parent */
1647 /* read traceid from request */
1648 parent_traceid
= state
->cli
.request
->traceid
;
1649 debug_traceid_set(parent_traceid
);
1651 DEBUG(4,("child daemon request %d\n",
1652 (int)state
->cli
.request
->cmd
));
1654 ZERO_STRUCTP(state
->cli
.response
);
1655 state
->cli
.request
->null_term
= '\0';
1656 state
->cli
.mem_ctx
= talloc_tos();
1657 child_process_request(state
->child
, &state
->cli
);
1659 DEBUG(4, ("Finished processing child request %d\n",
1660 (int)state
->cli
.request
->cmd
));
1662 SAFE_FREE(state
->cli
.request
->extra_data
.data
);
1664 status
= child_write_response(state
->cli
.sock
, state
->cli
.response
);
1665 if (!NT_STATUS_IS_OK(status
)) {
1670 static bool fork_domain_child(struct winbindd_child
*child
)
1673 struct child_handler_state state
;
1674 struct winbindd_request request
;
1675 struct winbindd_response response
;
1676 struct winbindd_domain
*primary_domain
= NULL
;
1679 struct tevent_fd
*fde
;
1680 struct tevent_req
*req
= NULL
;
1682 if (child
->domain
) {
1683 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1684 child
->domain
->name
));
1686 DEBUG(10, ("fork_domain_child called without domain.\n"));
1689 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1690 DEBUG(0, ("Could not open child pipe: %s\n",
1696 state
.child
= child
;
1697 state
.cli
.pid
= getpid();
1698 state
.cli
.request
= &request
;
1699 state
.cli
.response
= &response
;
1701 child
->pid
= fork();
1703 if (child
->pid
== -1) {
1704 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1710 if (child
->pid
!= 0) {
1717 nread
= sys_read(fdpair
[1], &status
, sizeof(status
));
1718 if (nread
!= sizeof(status
)) {
1719 DEBUG(1, ("fork_domain_child: Could not read child status: "
1720 "nread=%d, error=%s\n", (int)nread
,
1725 if (!NT_STATUS_IS_OK(status
)) {
1726 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1727 nt_errstr(status
)));
1732 child
->monitor_fde
= tevent_add_fd(global_event_context(),
1733 global_event_context(),
1736 child_socket_readable
,
1738 if (child
->monitor_fde
== NULL
) {
1739 DBG_WARNING("tevent_add_fd failed\n");
1744 rc
= set_blocking(fdpair
[1], false);
1750 child
->sock
= fdpair
[1];
1756 child_domain
= child
->domain
;
1758 DEBUG(10, ("Child process %d\n", (int)getpid()));
1760 state
.cli
.sock
= fdpair
[0];
1763 /* Reset traceid and deactivate call_depth tracking */
1764 if (lp_winbind_debug_traceid()) {
1765 debug_traceid_set(1);
1766 tevent_thread_call_depth_set_callback(NULL
, NULL
);
1769 status
= winbindd_reinit_after_fork(child
, child
->logfilename
);
1771 /* setup callbacks again, one of them is removed in reinit_after_fork */
1772 if (lp_winbind_debug_traceid()) {
1773 winbind_debug_traceid_setup(global_event_context());
1776 nwritten
= sys_write(state
.cli
.sock
, &status
, sizeof(status
));
1777 if (nwritten
!= sizeof(status
)) {
1778 DEBUG(1, ("fork_domain_child: Could not write status: "
1779 "nwritten=%d, error=%s\n", (int)nwritten
,
1783 if (!NT_STATUS_IS_OK(status
)) {
1784 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1785 nt_errstr(status
)));
1789 if (child_domain
!= NULL
) {
1790 process_set_title("wb[%s]", "domain child [%s]", child_domain
->name
);
1791 } else if (is_idmap_child(child
)) {
1792 process_set_title("wb-idmap", "idmap child");
1795 /* Handle online/offline messages. */
1796 messaging_register(global_messaging_context(), NULL
,
1797 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1798 messaging_register(global_messaging_context(), NULL
,
1799 MSG_WINBIND_ONLINE
, child_msg_online
);
1800 messaging_register(global_messaging_context(), NULL
,
1801 MSG_DEBUG
, debug_message
);
1802 messaging_register(global_messaging_context(), NULL
,
1803 MSG_WINBIND_IP_DROPPED
,
1804 winbind_msg_ip_dropped
);
1805 messaging_register(global_messaging_context(), NULL
,
1806 MSG_WINBIND_DISCONNECT_DC
,
1807 winbind_msg_disconnect_dc
);
1809 req
= messaging_filtered_read_send(global_event_context(),
1810 global_event_context(),
1811 global_messaging_context(),
1812 winbindd_child_msg_filter
,
1815 DBG_ERR("messaging_filtered_read_send failed\n");
1819 primary_domain
= find_our_domain();
1821 if (primary_domain
== NULL
) {
1822 smb_panic("no primary domain found");
1825 /* It doesn't matter if we allow cache login,
1826 * try to bring domain online after fork. */
1827 if ( child
->domain
) {
1828 child
->domain
->startup
= True
;
1829 child
->domain
->startup_time
= time_mono(NULL
);
1830 /* we can be in primary domain or in trusted domain
1831 * If we are in trusted domain, set the primary domain
1832 * in start-up mode */
1833 if (!(child
->domain
->internal
)) {
1834 set_domain_online_request(child
->domain
);
1835 if (!(child
->domain
->primary
)) {
1836 primary_domain
->startup
= True
;
1837 primary_domain
->startup_time
= time_mono(NULL
);
1838 set_domain_online_request(primary_domain
);
1843 /* We might be in the idmap child...*/
1844 if (child
->domain
&& !(child
->domain
->internal
) &&
1845 lp_winbind_offline_logon()) {
1847 set_domain_online_request(child
->domain
);
1849 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1850 /* We need to talk to the primary
1851 * domain as well as the trusted
1852 * domain inside a trusted domain
1855 * set_dc_type_and_flags_trustinfo()
1858 set_domain_online_request(primary_domain
);
1861 child
->lockout_policy_event
= tevent_add_timer(
1862 global_event_context(), NULL
, timeval_zero(),
1863 account_lockout_policy_handler
,
1867 if (child
->domain
&& child
->domain
->primary
&&
1868 !USE_KERBEROS_KEYTAB
&&
1869 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1871 struct timeval next_change
;
1873 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1875 child
->machine_password_change_event
= tevent_add_timer(
1876 global_event_context(), NULL
, next_change
,
1877 machine_password_change_handler
,
1882 fde
= tevent_add_fd(global_event_context(), NULL
, state
.cli
.sock
,
1883 TEVENT_FD_READ
, child_handler
, &state
);
1885 DEBUG(1, ("tevent_add_fd failed\n"));
1892 TALLOC_CTX
*frame
= talloc_stackframe();
1894 ret
= tevent_loop_once(global_event_context());
1896 DEBUG(1, ("tevent_loop_once failed: %s\n",
1901 if (child
->domain
&& child
->domain
->startup
&&
1902 (time_mono(NULL
) > child
->domain
->startup_time
+ 30)) {
1903 /* No longer in "startup" mode. */
1904 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1905 child
->domain
->name
));
1906 child
->domain
->startup
= False
;
1913 void winbind_msg_ip_dropped_parent(struct messaging_context
*msg_ctx
,
1916 struct server_id server_id
,
1919 struct winbind_msg_relay_state state
= {
1921 .msg_type
= msg_type
,
1925 winbind_msg_ip_dropped(msg_ctx
, private_data
, msg_type
,
1928 forall_children(winbind_msg_relay_fn
, &state
);
1931 void winbindd_terminate(bool is_parent
)
1934 /* When parent goes away we should
1935 * remove the socket file. Not so
1936 * when children terminate.
1940 if (asprintf(&path
, "%s/%s",
1941 lp_winbindd_socket_directory(), WINBINDD_SOCKET_NAME
) > 0) {
1949 netlogon_creds_cli_close_global_db();
1953 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
1954 char *description
= talloc_describe_all(mem_ctx
);
1956 DEBUG(3, ("tallocs left:\n%s\n", description
));
1957 talloc_destroy(mem_ctx
);
1962 pidfile_unlink(lp_pid_directory(), "winbindd");
1968 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
1969 struct tevent_signal
*se
,
1975 bool *p
= talloc_get_type_abort(private_data
, bool);
1976 bool is_parent
= *p
;
1980 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
1981 signum
, is_parent
));
1982 winbindd_terminate(is_parent
);
1985 bool winbindd_setup_sig_term_handler(bool parent
)
1987 struct tevent_signal
*se
;
1990 is_parent
= talloc(global_event_context(), bool);
1995 *is_parent
= parent
;
1997 se
= tevent_add_signal(global_event_context(),
2000 winbindd_sig_term_handler
,
2003 DEBUG(0,("failed to setup SIGTERM handler\n"));
2004 talloc_free(is_parent
);
2008 se
= tevent_add_signal(global_event_context(),
2011 winbindd_sig_term_handler
,
2014 DEBUG(0,("failed to setup SIGINT handler\n"));
2015 talloc_free(is_parent
);
2019 se
= tevent_add_signal(global_event_context(),
2022 winbindd_sig_term_handler
,
2025 DEBUG(0,("failed to setup SIGINT handler\n"));
2026 talloc_free(is_parent
);
2033 static void flush_caches_noinit(void)
2036 * We need to invalidate cached user list entries on a SIGHUP
2037 * otherwise cached access denied errors due to restrict anonymous
2038 * hang around until the sequence number changes.
2040 * Skip uninitialized domains when flush cache.
2041 * If domain is not initialized, it means it is never
2042 * used or never become online. look, wcache_invalidate_cache()
2043 * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
2044 * for unused domains and large traffic for primary domain's DC if there
2045 * are many domains..
2048 if (!wcache_invalidate_cache_noinit()) {
2049 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
2050 if (!winbindd_cache_validate_and_initialize()) {
2056 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
2057 struct tevent_signal
*se
,
2063 const char *file
= (const char *)private_data
;
2065 DEBUG(1,("Reloading services after SIGHUP\n"));
2066 flush_caches_noinit();
2067 winbindd_reload_services_file(file
);
2070 bool winbindd_setup_sig_hup_handler(const char *lfile
)
2072 struct tevent_signal
*se
;
2076 file
= talloc_strdup(global_event_context(),
2083 se
= tevent_add_signal(global_event_context(),
2084 global_event_context(),
2086 winbindd_sig_hup_handler
,