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 "../libcli/security/security.h"
37 #include "system/select.h"
39 #include "../lib/util/tevent_unix.h"
40 #include "lib/param/loadparm.h"
43 #define DBGC_CLASS DBGC_WINBIND
45 extern bool override_logfile
;
46 extern struct winbindd_methods cache_methods
;
48 static struct winbindd_child
*winbindd_children
= NULL
;
50 /* Read some data from a client connection */
52 static NTSTATUS
child_read_request(int sock
, struct winbindd_request
*wreq
)
56 status
= read_data(sock
, (char *)wreq
, sizeof(*wreq
));
57 if (!NT_STATUS_IS_OK(status
)) {
58 DEBUG(3, ("child_read_request: read_data failed: %s\n",
63 if (wreq
->extra_len
== 0) {
64 wreq
->extra_data
.data
= NULL
;
68 DEBUG(10, ("Need to read %d extra bytes\n", (int)wreq
->extra_len
));
70 wreq
->extra_data
.data
= SMB_MALLOC_ARRAY(char, wreq
->extra_len
+ 1);
71 if (wreq
->extra_data
.data
== NULL
) {
72 DEBUG(0, ("malloc failed\n"));
73 return NT_STATUS_NO_MEMORY
;
76 /* Ensure null termination */
77 wreq
->extra_data
.data
[wreq
->extra_len
] = '\0';
79 status
= read_data(sock
, wreq
->extra_data
.data
, wreq
->extra_len
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 DEBUG(0, ("Could not read extra data: %s\n",
87 static NTSTATUS
child_write_response(int sock
, struct winbindd_response
*wrsp
)
92 iov
[0].iov_base
= (void *)wrsp
;
93 iov
[0].iov_len
= sizeof(struct winbindd_response
);
96 if (wrsp
->length
> sizeof(struct winbindd_response
)) {
97 iov
[1].iov_base
= (void *)wrsp
->extra_data
.data
;
98 iov
[1].iov_len
= wrsp
->length
-iov
[0].iov_len
;
102 DEBUG(10, ("Writing %d bytes to parent\n", (int)wrsp
->length
));
104 if (write_data_iov(sock
, iov
, iov_count
) != wrsp
->length
) {
105 DEBUG(0, ("Could not write result\n"));
106 return NT_STATUS_INVALID_HANDLE
;
113 * Do winbind child async request. This is not simply wb_simple_trans. We have
114 * to do the queueing ourselves because while a request is queued, the child
115 * might have crashed, and we have to re-fork it in the _trigger function.
118 struct wb_child_request_state
{
119 struct tevent_context
*ev
;
120 struct winbindd_child
*child
;
121 struct winbindd_request
*request
;
122 struct winbindd_response
*response
;
125 static bool fork_domain_child(struct winbindd_child
*child
);
127 static void wb_child_request_trigger(struct tevent_req
*req
,
129 static void wb_child_request_done(struct tevent_req
*subreq
);
131 struct tevent_req
*wb_child_request_send(TALLOC_CTX
*mem_ctx
,
132 struct tevent_context
*ev
,
133 struct winbindd_child
*child
,
134 struct winbindd_request
*request
)
136 struct tevent_req
*req
;
137 struct wb_child_request_state
*state
;
139 req
= tevent_req_create(mem_ctx
, &state
,
140 struct wb_child_request_state
);
146 state
->child
= child
;
147 state
->request
= request
;
149 if (!tevent_queue_add(child
->queue
, ev
, req
,
150 wb_child_request_trigger
, NULL
)) {
152 return tevent_req_post(req
, ev
);
157 static void wb_child_request_trigger(struct tevent_req
*req
,
160 struct wb_child_request_state
*state
= tevent_req_data(
161 req
, struct wb_child_request_state
);
162 struct tevent_req
*subreq
;
164 if ((state
->child
->sock
== -1) && (!fork_domain_child(state
->child
))) {
165 tevent_req_error(req
, errno
);
169 subreq
= wb_simple_trans_send(state
, winbind_event_context(), NULL
,
170 state
->child
->sock
, state
->request
);
171 if (tevent_req_nomem(subreq
, req
)) {
174 tevent_req_set_callback(subreq
, wb_child_request_done
, req
);
175 tevent_req_set_endtime(req
, state
->ev
, timeval_current_ofs(300, 0));
178 static void wb_child_request_done(struct tevent_req
*subreq
)
180 struct tevent_req
*req
= tevent_req_callback_data(
181 subreq
, struct tevent_req
);
182 struct wb_child_request_state
*state
= tevent_req_data(
183 req
, struct wb_child_request_state
);
186 ret
= wb_simple_trans_recv(subreq
, state
, &state
->response
, &err
);
190 * The basic parent/child communication broke, close
193 close(state
->child
->sock
);
194 state
->child
->sock
= -1;
195 DLIST_REMOVE(winbindd_children
, state
->child
);
196 tevent_req_error(req
, err
);
199 tevent_req_done(req
);
202 int wb_child_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
203 struct winbindd_response
**presponse
, int *err
)
205 struct wb_child_request_state
*state
= tevent_req_data(
206 req
, struct wb_child_request_state
);
208 if (tevent_req_is_unix_error(req
, err
)) {
211 *presponse
= talloc_move(mem_ctx
, &state
->response
);
215 static bool winbindd_child_busy(struct winbindd_child
*child
)
217 return tevent_queue_length(child
->queue
) > 0;
220 static struct winbindd_child
*find_idle_child(struct winbindd_domain
*domain
)
224 for (i
=0; i
<lp_winbind_max_domain_connections(); i
++) {
225 if (!winbindd_child_busy(&domain
->children
[i
])) {
226 return &domain
->children
[i
];
233 struct winbindd_child
*choose_domain_child(struct winbindd_domain
*domain
)
235 struct winbindd_child
*result
;
237 result
= find_idle_child(domain
);
238 if (result
!= NULL
) {
241 return &domain
->children
[rand() % lp_winbind_max_domain_connections()];
244 struct dcerpc_binding_handle
*dom_child_handle(struct winbindd_domain
*domain
)
246 struct winbindd_child
*child
;
248 child
= choose_domain_child(domain
);
249 return child
->binding_handle
;
252 struct wb_domain_request_state
{
253 struct tevent_context
*ev
;
254 struct winbindd_domain
*domain
;
255 struct winbindd_child
*child
;
256 struct winbindd_request
*request
;
257 struct winbindd_request
*init_req
;
258 struct winbindd_response
*response
;
261 static void wb_domain_request_gotdc(struct tevent_req
*subreq
);
262 static void wb_domain_request_initialized(struct tevent_req
*subreq
);
263 static void wb_domain_request_done(struct tevent_req
*subreq
);
265 struct tevent_req
*wb_domain_request_send(TALLOC_CTX
*mem_ctx
,
266 struct tevent_context
*ev
,
267 struct winbindd_domain
*domain
,
268 struct winbindd_request
*request
)
270 struct tevent_req
*req
, *subreq
;
271 struct wb_domain_request_state
*state
;
273 req
= tevent_req_create(mem_ctx
, &state
,
274 struct wb_domain_request_state
);
279 state
->child
= choose_domain_child(domain
);
281 if (domain
->initialized
) {
282 subreq
= wb_child_request_send(state
, ev
, state
->child
,
284 if (tevent_req_nomem(subreq
, req
)) {
285 return tevent_req_post(req
, ev
);
287 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
291 state
->domain
= domain
;
293 state
->request
= request
;
295 state
->init_req
= talloc_zero(state
, struct winbindd_request
);
296 if (tevent_req_nomem(state
->init_req
, req
)) {
297 return tevent_req_post(req
, ev
);
300 if (IS_DC
|| domain
->primary
|| domain
->internal
) {
301 /* The primary domain has to find the DC name itself */
302 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
303 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
304 state
->init_req
->data
.init_conn
.is_primary
= domain
->primary
;
305 fstrcpy(state
->init_req
->data
.init_conn
.dcname
, "");
307 subreq
= wb_child_request_send(state
, ev
, state
->child
,
309 if (tevent_req_nomem(subreq
, req
)) {
310 return tevent_req_post(req
, ev
);
312 tevent_req_set_callback(subreq
, wb_domain_request_initialized
,
318 * Ask our DC for a DC name
320 domain
= find_our_domain();
322 /* This is *not* the primary domain, let's ask our DC about a DC
325 state
->init_req
->cmd
= WINBINDD_GETDCNAME
;
326 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
328 subreq
= wb_child_request_send(state
, ev
, state
->child
, request
);
329 if (tevent_req_nomem(subreq
, req
)) {
330 return tevent_req_post(req
, ev
);
332 tevent_req_set_callback(subreq
, wb_domain_request_gotdc
, req
);
336 static void wb_domain_request_gotdc(struct tevent_req
*subreq
)
338 struct tevent_req
*req
= tevent_req_callback_data(
339 subreq
, struct tevent_req
);
340 struct wb_domain_request_state
*state
= tevent_req_data(
341 req
, struct wb_domain_request_state
);
342 struct winbindd_response
*response
;
345 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
348 tevent_req_error(req
, err
);
351 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
352 fstrcpy(state
->init_req
->domain_name
, state
->domain
->name
);
353 state
->init_req
->data
.init_conn
.is_primary
= False
;
354 fstrcpy(state
->init_req
->data
.init_conn
.dcname
,
355 response
->data
.dc_name
);
357 TALLOC_FREE(response
);
359 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
361 if (tevent_req_nomem(subreq
, req
)) {
364 tevent_req_set_callback(subreq
, wb_domain_request_initialized
, req
);
367 static void wb_domain_request_initialized(struct tevent_req
*subreq
)
369 struct tevent_req
*req
= tevent_req_callback_data(
370 subreq
, struct tevent_req
);
371 struct wb_domain_request_state
*state
= tevent_req_data(
372 req
, struct wb_domain_request_state
);
373 struct winbindd_response
*response
;
376 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
379 tevent_req_error(req
, err
);
383 if (!string_to_sid(&state
->domain
->sid
,
384 response
->data
.domain_info
.sid
)) {
385 DEBUG(1,("init_child_recv: Could not convert sid %s "
386 "from string\n", response
->data
.domain_info
.sid
));
387 tevent_req_error(req
, EINVAL
);
391 talloc_free(state
->domain
->name
);
392 state
->domain
->name
= talloc_strdup(state
->domain
,
393 response
->data
.domain_info
.name
);
394 if (state
->domain
->name
== NULL
) {
395 tevent_req_error(req
, ENOMEM
);
399 if (response
->data
.domain_info
.alt_name
[0] != '\0') {
400 talloc_free(state
->domain
->alt_name
);
402 state
->domain
->alt_name
= talloc_strdup(state
->domain
,
403 response
->data
.domain_info
.alt_name
);
404 if (state
->domain
->alt_name
== NULL
) {
405 tevent_req_error(req
, ENOMEM
);
410 state
->domain
->native_mode
= response
->data
.domain_info
.native_mode
;
411 state
->domain
->active_directory
=
412 response
->data
.domain_info
.active_directory
;
413 state
->domain
->initialized
= true;
415 TALLOC_FREE(response
);
417 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
419 if (tevent_req_nomem(subreq
, req
)) {
422 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
425 static void wb_domain_request_done(struct tevent_req
*subreq
)
427 struct tevent_req
*req
= tevent_req_callback_data(
428 subreq
, struct tevent_req
);
429 struct wb_domain_request_state
*state
= tevent_req_data(
430 req
, struct wb_domain_request_state
);
433 ret
= wb_child_request_recv(subreq
, talloc_tos(), &state
->response
,
437 tevent_req_error(req
, err
);
440 tevent_req_done(req
);
443 int wb_domain_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
444 struct winbindd_response
**presponse
, int *err
)
446 struct wb_domain_request_state
*state
= tevent_req_data(
447 req
, struct wb_domain_request_state
);
449 if (tevent_req_is_unix_error(req
, err
)) {
452 *presponse
= talloc_move(mem_ctx
, &state
->response
);
456 static void child_process_request(struct winbindd_child
*child
,
457 struct winbindd_cli_state
*state
)
459 struct winbindd_domain
*domain
= child
->domain
;
460 const struct winbindd_child_dispatch_table
*table
= child
->table
;
462 /* Free response data - we may be interrupted and receive another
463 command before being able to send this data off. */
465 state
->response
->result
= WINBINDD_ERROR
;
466 state
->response
->length
= sizeof(struct winbindd_response
);
468 /* as all requests in the child are sync, we can use talloc_tos() */
469 state
->mem_ctx
= talloc_tos();
471 /* Process command */
473 for (; table
->name
; table
++) {
474 if (state
->request
->cmd
== table
->struct_cmd
) {
475 DEBUG(10,("child_process_request: request fn %s\n",
477 state
->response
->result
= table
->struct_fn(domain
, state
);
482 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
483 (int)state
->request
->cmd
));
484 state
->response
->result
= WINBINDD_ERROR
;
487 void setup_child(struct winbindd_domain
*domain
, struct winbindd_child
*child
,
488 const struct winbindd_child_dispatch_table
*table
,
489 const char *logprefix
,
492 if (logprefix
&& logname
) {
493 char *logbase
= NULL
;
495 if (*lp_logfile(talloc_tos())) {
498 if (asprintf(&logbase
, "%s", lp_logfile(talloc_tos())) < 0) {
499 smb_panic("Internal error: asprintf failed");
502 if ((end
= strrchr_m(logbase
, '/'))) {
506 if (asprintf(&logbase
, "%s", get_dyn_LOGFILEBASE()) < 0) {
507 smb_panic("Internal error: asprintf failed");
511 if (asprintf(&child
->logfilename
, "%s/%s-%s",
512 logbase
, logprefix
, logname
) < 0) {
514 smb_panic("Internal error: asprintf failed");
519 smb_panic("Internal error: logprefix == NULL && "
524 child
->domain
= domain
;
525 child
->table
= table
;
526 child
->queue
= tevent_queue_create(NULL
, "winbind_child");
527 SMB_ASSERT(child
->queue
!= NULL
);
528 child
->binding_handle
= wbint_binding_handle(NULL
, domain
, child
);
529 SMB_ASSERT(child
->binding_handle
!= NULL
);
532 void winbind_child_died(pid_t pid
)
534 struct winbindd_child
*child
;
536 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
537 if (child
->pid
== pid
) {
543 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
547 /* This will be re-added in fork_domain_child() */
549 DLIST_REMOVE(winbindd_children
, child
);
552 if (child
->sock
!= -1) {
558 /* Ensure any negative cache entries with the netbios or realm names are removed. */
560 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
562 flush_negative_conn_cache_for_domain(domain
->name
);
563 if (domain
->alt_name
!= NULL
) {
564 flush_negative_conn_cache_for_domain(domain
->alt_name
);
569 * Parent winbindd process sets its own debug level first and then
570 * sends a message to all the winbindd children to adjust their debug
571 * level to that of parents.
574 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
577 struct server_id server_id
,
580 struct winbindd_child
*child
;
582 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
584 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
586 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
588 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
589 (unsigned int)child
->pid
));
591 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
594 strlen((char *) data
->data
) + 1);
598 /* Set our domains as offline and forward the offline message to our children. */
600 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
603 struct server_id server_id
,
606 struct winbindd_child
*child
;
607 struct winbindd_domain
*domain
;
609 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
611 if (!lp_winbind_offline_logon()) {
612 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
616 /* Set our global state as offline. */
617 if (!set_global_winbindd_state_offline()) {
618 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
622 /* Set all our domains as offline. */
623 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
624 if (domain
->internal
) {
627 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
628 set_domain_offline(domain
);
631 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
632 /* Don't send message to internal children. We've already
634 if (!child
->domain
|| winbindd_internal_child(child
)) {
638 /* Or internal domains (this should not be possible....) */
639 if (child
->domain
->internal
) {
643 /* Each winbindd child should only process requests for one domain - make sure
644 we only set it online / offline for that domain. */
646 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
647 (unsigned int)child
->pid
, child
->domain
->name
));
649 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
651 (const uint8_t *)child
->domain
->name
,
652 strlen(child
->domain
->name
)+1);
656 /* Set our domains as online and forward the online message to our children. */
658 void winbind_msg_online(struct messaging_context
*msg_ctx
,
661 struct server_id server_id
,
664 struct winbindd_child
*child
;
665 struct winbindd_domain
*domain
;
667 DEBUG(10,("winbind_msg_online: got online message.\n"));
669 if (!lp_winbind_offline_logon()) {
670 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
674 /* Set our global state as online. */
675 set_global_winbindd_state_online();
677 smb_nscd_flush_user_cache();
678 smb_nscd_flush_group_cache();
680 /* Set all our domains as online. */
681 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
682 if (domain
->internal
) {
685 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
687 winbindd_flush_negative_conn_cache(domain
);
688 set_domain_online_request(domain
);
690 /* Send an online message to the idmap child when our
691 primary domain comes back online */
693 if ( domain
->primary
) {
694 struct winbindd_child
*idmap
= idmap_child();
696 if ( idmap
->pid
!= 0 ) {
697 messaging_send_buf(msg_ctx
,
698 pid_to_procid(idmap
->pid
),
700 (const uint8_t *)domain
->name
,
701 strlen(domain
->name
)+1);
706 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
707 /* Don't send message to internal childs. */
708 if (!child
->domain
|| winbindd_internal_child(child
)) {
712 /* Or internal domains (this should not be possible....) */
713 if (child
->domain
->internal
) {
717 /* Each winbindd child should only process requests for one domain - make sure
718 we only set it online / offline for that domain. */
720 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
721 (unsigned int)child
->pid
, child
->domain
->name
));
723 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
725 (const uint8_t *)child
->domain
->name
,
726 strlen(child
->domain
->name
)+1);
730 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
732 struct winbindd_domain
*domain
;
735 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
736 get_global_winbindd_state_offline() ?
737 "Offline":"Online")) == NULL
) {
741 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
742 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
745 "Online":"Offline")) == NULL
) {
750 buf
= talloc_asprintf_append_buffer(buf
, "\n");
752 DEBUG(5,("collect_onlinestatus: %s", buf
));
757 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
760 struct server_id server_id
,
765 struct server_id
*sender
;
767 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
773 sender
= (struct server_id
*)data
->data
;
775 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
776 if (mem_ctx
== NULL
) {
780 message
= collect_onlinestatus(mem_ctx
);
781 if (message
== NULL
) {
782 talloc_destroy(mem_ctx
);
786 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
787 (const uint8
*)message
, strlen(message
) + 1);
789 talloc_destroy(mem_ctx
);
792 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
795 struct server_id server_id
,
798 struct winbindd_child
*child
;
800 DEBUG(10,("winbind_msg_dump_event_list received\n"));
802 dump_event_list(winbind_event_context());
804 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
806 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
807 (unsigned int)child
->pid
));
809 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
816 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
819 struct server_id server_id
,
823 const char *message
= NULL
;
824 struct server_id
*sender
= NULL
;
825 const char *domain
= NULL
;
828 struct winbindd_domain
*dom
= NULL
;
830 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
832 if (!data
|| !data
->data
) {
836 if (data
->length
< sizeof(struct server_id
)) {
840 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
845 sender
= (struct server_id
*)data
->data
;
846 if (data
->length
> sizeof(struct server_id
)) {
847 domain
= (const char *)data
->data
+sizeof(struct server_id
);
852 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
855 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
856 find_domain_from_name_noinit(domain
));
858 talloc_destroy(mem_ctx
);
862 messaging_send_buf(msg_ctx
, *sender
,
863 MSG_WINBIND_DUMP_DOMAIN_LIST
,
864 (const uint8_t *)message
, strlen(message
) + 1);
866 talloc_destroy(mem_ctx
);
871 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
873 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
874 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
876 talloc_destroy(mem_ctx
);
880 s
= talloc_asprintf_append(s
, "%s\n", message
);
882 talloc_destroy(mem_ctx
);
887 status
= messaging_send_buf(msg_ctx
, *sender
,
888 MSG_WINBIND_DUMP_DOMAIN_LIST
,
889 (uint8_t *)s
, strlen(s
) + 1);
890 if (!NT_STATUS_IS_OK(status
)) {
891 DEBUG(0,("failed to send message: %s\n",
895 talloc_destroy(mem_ctx
);
898 static void account_lockout_policy_handler(struct tevent_context
*ctx
,
899 struct tevent_timer
*te
,
903 struct winbindd_child
*child
=
904 (struct winbindd_child
*)private_data
;
905 TALLOC_CTX
*mem_ctx
= NULL
;
906 struct winbindd_methods
*methods
;
907 struct samr_DomInfo12 lockout_policy
;
910 DEBUG(10,("account_lockout_policy_handler called\n"));
912 TALLOC_FREE(child
->lockout_policy_event
);
914 if ( !winbindd_can_contact_domain( child
->domain
) ) {
915 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
916 "do not have an incoming trust to domain %s\n",
917 child
->domain
->name
));
922 methods
= child
->domain
->methods
;
924 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
926 result
= NT_STATUS_NO_MEMORY
;
928 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
930 TALLOC_FREE(mem_ctx
);
932 if (!NT_STATUS_IS_OK(result
)) {
933 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
937 child
->lockout_policy_event
= tevent_add_timer(winbind_event_context(), NULL
,
938 timeval_current_ofs(3600, 0),
939 account_lockout_policy_handler
,
943 static time_t get_machine_password_timeout(void)
945 /* until we have gpo support use lp setting */
946 return lp_machine_password_timeout();
949 static bool calculate_next_machine_pwd_change(const char *domain
,
952 time_t pass_last_set_time
;
958 pw
= secrets_fetch_machine_password(domain
,
963 DEBUG(0,("cannot fetch own machine password ????"));
969 timeout
= get_machine_password_timeout();
971 DEBUG(10,("machine password never expires\n"));
975 tv
.tv_sec
= pass_last_set_time
;
976 DEBUG(10, ("password last changed %s\n",
977 timeval_string(talloc_tos(), &tv
, false)));
978 tv
.tv_sec
+= timeout
;
979 DEBUGADD(10, ("password valid until %s\n",
980 timeval_string(talloc_tos(), &tv
, false)));
982 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
983 next_change
= pass_last_set_time
+ timeout
;
984 DEBUG(10,("machine password still valid until: %s\n",
985 http_timestring(talloc_tos(), next_change
)));
986 *t
= timeval_set(next_change
, 0);
988 if (lp_clustering()) {
991 * When having a cluster, we have several
992 * winbinds racing for the password change. In
993 * the machine_password_change_handler()
994 * function we check if someone else was
995 * faster when the event triggers. We add a
996 * 255-second random delay here, so that we
997 * don't run to change the password at the
1000 generate_random_buffer(&randbuf
, sizeof(randbuf
));
1001 DEBUG(10, ("adding %d seconds randomness\n",
1003 t
->tv_sec
+= randbuf
;
1008 DEBUG(10,("machine password expired, needs immediate change\n"));
1010 *t
= timeval_zero();
1015 static void machine_password_change_handler(struct tevent_context
*ctx
,
1016 struct tevent_timer
*te
,
1020 struct messaging_context
*msg_ctx
= winbind_messaging_context();
1021 struct winbindd_child
*child
=
1022 (struct winbindd_child
*)private_data
;
1023 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1025 struct timeval next_change
;
1027 DEBUG(10,("machine_password_change_handler called\n"));
1029 TALLOC_FREE(child
->machine_password_change_event
);
1031 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1033 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1037 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1038 timeval_string(talloc_tos(), &next_change
, false)));
1040 if (!timeval_expired(&next_change
)) {
1041 DEBUG(10, ("Someone else has already changed the pw\n"));
1045 if (!winbindd_can_contact_domain(child
->domain
)) {
1046 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1047 "do not have an incoming trust to domain %s\n",
1048 child
->domain
->name
));
1052 result
= cm_connect_netlogon(child
->domain
, &netlogon_pipe
);
1053 if (!NT_STATUS_IS_OK(result
)) {
1054 DEBUG(10,("machine_password_change_handler: "
1055 "failed to connect netlogon pipe: %s\n",
1056 nt_errstr(result
)));
1060 result
= trust_pw_change(child
->domain
->conn
.netlogon_creds
,
1062 netlogon_pipe
->binding_handle
,
1063 child
->domain
->name
,
1066 DEBUG(10, ("machine_password_change_handler: "
1067 "trust_pw_change returned %s\n",
1068 nt_errstr(result
)));
1070 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1071 DEBUG(3,("machine_password_change_handler: password set returned "
1072 "ACCESS_DENIED. Maybe the trust account "
1073 "password was changed and we didn't know it. "
1074 "Killing connections to domain %s\n",
1075 child
->domain
->name
));
1076 invalidate_cm_connection(&child
->domain
->conn
);
1079 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1081 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1085 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1086 timeval_string(talloc_tos(), &next_change
, false)));
1088 if (!NT_STATUS_IS_OK(result
)) {
1091 * In case of failure, give the DC a minute to recover
1093 tmp
= timeval_current_ofs(60, 0);
1094 next_change
= timeval_max(&next_change
, &tmp
);
1098 child
->machine_password_change_event
= tevent_add_timer(winbind_event_context(), NULL
,
1100 machine_password_change_handler
,
1104 /* Deal with a request to go offline. */
1106 static void child_msg_offline(struct messaging_context
*msg
,
1109 struct server_id server_id
,
1112 struct winbindd_domain
*domain
;
1113 struct winbindd_domain
*primary_domain
= NULL
;
1114 const char *domainname
= (const char *)data
->data
;
1116 if (data
->data
== NULL
|| data
->length
== 0) {
1120 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
1122 if (!lp_winbind_offline_logon()) {
1123 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1127 primary_domain
= find_our_domain();
1129 /* Mark the requested domain offline. */
1131 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1132 if (domain
->internal
) {
1135 if (strequal(domain
->name
, domainname
)) {
1136 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1137 set_domain_offline(domain
);
1138 /* we are in the trusted domain, set the primary domain
1140 if (domain
!= primary_domain
) {
1141 set_domain_offline(primary_domain
);
1147 /* Deal with a request to go online. */
1149 static void child_msg_online(struct messaging_context
*msg
,
1152 struct server_id server_id
,
1155 struct winbindd_domain
*domain
;
1156 struct winbindd_domain
*primary_domain
= NULL
;
1157 const char *domainname
= (const char *)data
->data
;
1159 if (data
->data
== NULL
|| data
->length
== 0) {
1163 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1165 if (!lp_winbind_offline_logon()) {
1166 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1170 primary_domain
= find_our_domain();
1172 /* Set our global state as online. */
1173 set_global_winbindd_state_online();
1175 /* Try and mark everything online - delete any negative cache entries
1176 to force a reconnect now. */
1178 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1179 if (domain
->internal
) {
1182 if (strequal(domain
->name
, domainname
)) {
1183 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1184 winbindd_flush_negative_conn_cache(domain
);
1185 set_domain_online_request(domain
);
1187 /* we can be in trusted domain, which will contact primary domain
1188 * we have to bring primary domain online in trusted domain process
1189 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1190 * --> contact_domain = find_our_domain()
1192 if (domain
!= primary_domain
) {
1193 winbindd_flush_negative_conn_cache(primary_domain
);
1194 set_domain_online_request(primary_domain
);
1200 static void child_msg_dump_event_list(struct messaging_context
*msg
,
1203 struct server_id server_id
,
1206 DEBUG(5,("child_msg_dump_event_list received\n"));
1208 dump_event_list(winbind_event_context());
1211 NTSTATUS
winbindd_reinit_after_fork(const struct winbindd_child
*myself
,
1212 const char *logfilename
)
1214 struct winbindd_domain
*domain
;
1215 struct winbindd_child
*cl
;
1218 status
= reinit_after_fork(
1219 winbind_messaging_context(),
1220 winbind_event_context(),
1222 if (!NT_STATUS_IS_OK(status
)) {
1223 DEBUG(0,("reinit_after_fork() failed\n"));
1227 close_conns_after_fork();
1229 if (!override_logfile
&& logfilename
) {
1230 lp_set_logfile(logfilename
);
1234 if (!winbindd_setup_sig_term_handler(false))
1235 return NT_STATUS_NO_MEMORY
;
1236 if (!winbindd_setup_sig_hup_handler(override_logfile
? NULL
:
1238 return NT_STATUS_NO_MEMORY
;
1240 /* Stop zombies in children */
1243 /* Don't handle the same messages as our parent. */
1244 messaging_deregister(winbind_messaging_context(),
1245 MSG_SMB_CONF_UPDATED
, NULL
);
1246 messaging_deregister(winbind_messaging_context(),
1247 MSG_SHUTDOWN
, NULL
);
1248 messaging_deregister(winbind_messaging_context(),
1249 MSG_WINBIND_OFFLINE
, NULL
);
1250 messaging_deregister(winbind_messaging_context(),
1251 MSG_WINBIND_ONLINE
, NULL
);
1252 messaging_deregister(winbind_messaging_context(),
1253 MSG_WINBIND_ONLINESTATUS
, NULL
);
1254 messaging_deregister(winbind_messaging_context(),
1255 MSG_DUMP_EVENT_LIST
, NULL
);
1256 messaging_deregister(winbind_messaging_context(),
1257 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1258 messaging_deregister(winbind_messaging_context(),
1261 messaging_deregister(winbind_messaging_context(),
1262 MSG_WINBIND_DOMAIN_OFFLINE
, NULL
);
1263 messaging_deregister(winbind_messaging_context(),
1264 MSG_WINBIND_DOMAIN_ONLINE
, NULL
);
1266 /* We have destroyed all events in the winbindd_event_context
1267 * in reinit_after_fork(), so clean out all possible pending
1268 * event pointers. */
1270 /* Deal with check_online_events. */
1272 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1273 TALLOC_FREE(domain
->check_online_event
);
1276 /* Ensure we're not handling a credential cache event inherited
1277 * from our parent. */
1279 ccache_remove_all_after_fork();
1281 /* Destroy all possible events in child list. */
1282 for (cl
= winbindd_children
; cl
!= NULL
; cl
= cl
->next
) {
1283 TALLOC_FREE(cl
->lockout_policy_event
);
1284 TALLOC_FREE(cl
->machine_password_change_event
);
1286 /* Children should never be able to send
1287 * each other messages, all messages must
1288 * go through the parent.
1293 * Close service sockets to all other children
1295 if ((cl
!= myself
) && (cl
->sock
!= -1)) {
1301 * This is a little tricky, children must not
1302 * send an MSG_WINBIND_ONLINE message to idmap_child().
1303 * If we are in a child of our primary domain or
1304 * in the process created by fork_child_dc_connect(),
1305 * and the primary domain cannot go online,
1306 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1307 * periodically to idmap_child().
1309 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1310 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1311 * ---> init_dc_connection() ---> cm_open_connection --->
1312 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1313 * idmap_child(). Disallow children sending messages
1314 * to each other, all messages must go through the parent.
1319 return NT_STATUS_OK
;
1323 * In a child there will be only one domain, reference that here.
1325 static struct winbindd_domain
*child_domain
;
1327 struct winbindd_domain
*wb_child_domain(void)
1329 return child_domain
;
1332 struct child_handler_state
{
1333 struct winbindd_child
*child
;
1334 struct winbindd_cli_state cli
;
1337 static void child_handler(struct tevent_context
*ev
, struct tevent_fd
*fde
,
1338 uint16_t flags
, void *private_data
)
1340 struct child_handler_state
*state
=
1341 (struct child_handler_state
*)private_data
;
1344 /* fetch a request from the main daemon */
1345 status
= child_read_request(state
->cli
.sock
, state
->cli
.request
);
1347 if (!NT_STATUS_IS_OK(status
)) {
1348 /* we lost contact with our parent */
1352 DEBUG(4,("child daemon request %d\n",
1353 (int)state
->cli
.request
->cmd
));
1355 ZERO_STRUCTP(state
->cli
.response
);
1356 state
->cli
.request
->null_term
= '\0';
1357 state
->cli
.mem_ctx
= talloc_tos();
1358 child_process_request(state
->child
, &state
->cli
);
1360 DEBUG(4, ("Finished processing child request %d\n",
1361 (int)state
->cli
.request
->cmd
));
1363 SAFE_FREE(state
->cli
.request
->extra_data
.data
);
1365 status
= child_write_response(state
->cli
.sock
, state
->cli
.response
);
1366 if (!NT_STATUS_IS_OK(status
)) {
1371 static bool fork_domain_child(struct winbindd_child
*child
)
1374 struct child_handler_state state
;
1375 struct winbindd_request request
;
1376 struct winbindd_response response
;
1377 struct winbindd_domain
*primary_domain
= NULL
;
1380 struct tevent_fd
*fde
;
1382 if (child
->domain
) {
1383 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1384 child
->domain
->name
));
1386 DEBUG(10, ("fork_domain_child called without domain.\n"));
1389 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1390 DEBUG(0, ("Could not open child pipe: %s\n",
1396 state
.child
= child
;
1397 state
.cli
.pid
= getpid();
1398 state
.cli
.request
= &request
;
1399 state
.cli
.response
= &response
;
1401 child
->pid
= fork();
1403 if (child
->pid
== -1) {
1404 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1410 if (child
->pid
!= 0) {
1416 nread
= sys_read(fdpair
[1], &status
, sizeof(status
));
1417 if (nread
!= sizeof(status
)) {
1418 DEBUG(1, ("fork_domain_child: Could not read child status: "
1419 "nread=%d, error=%s\n", (int)nread
,
1424 if (!NT_STATUS_IS_OK(status
)) {
1425 DEBUG(1, ("fork_domain_child: Child status is %s\n",
1426 nt_errstr(status
)));
1431 child
->next
= child
->prev
= NULL
;
1432 DLIST_ADD(winbindd_children
, child
);
1433 child
->sock
= fdpair
[1];
1438 child_domain
= child
->domain
;
1440 DEBUG(10, ("Child process %d\n", (int)getpid()));
1442 state
.cli
.sock
= fdpair
[0];
1445 status
= winbindd_reinit_after_fork(child
, child
->logfilename
);
1447 nwritten
= sys_write(state
.cli
.sock
, &status
, sizeof(status
));
1448 if (nwritten
!= sizeof(status
)) {
1449 DEBUG(1, ("fork_domain_child: Could not write status: "
1450 "nwritten=%d, error=%s\n", (int)nwritten
,
1454 if (!NT_STATUS_IS_OK(status
)) {
1455 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
1456 nt_errstr(status
)));
1460 /* Handle online/offline messages. */
1461 messaging_register(winbind_messaging_context(), NULL
,
1462 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1463 messaging_register(winbind_messaging_context(), NULL
,
1464 MSG_WINBIND_ONLINE
, child_msg_online
);
1465 messaging_register(winbind_messaging_context(), NULL
,
1466 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1467 messaging_register(winbind_messaging_context(), NULL
,
1468 MSG_DEBUG
, debug_message
);
1469 messaging_register(winbind_messaging_context(), NULL
,
1470 MSG_WINBIND_IP_DROPPED
,
1471 winbind_msg_ip_dropped
);
1474 primary_domain
= find_our_domain();
1476 if (primary_domain
== NULL
) {
1477 smb_panic("no primary domain found");
1480 /* It doesn't matter if we allow cache login,
1481 * try to bring domain online after fork. */
1482 if ( child
->domain
) {
1483 child
->domain
->startup
= True
;
1484 child
->domain
->startup_time
= time_mono(NULL
);
1485 /* we can be in primary domain or in trusted domain
1486 * If we are in trusted domain, set the primary domain
1487 * in start-up mode */
1488 if (!(child
->domain
->internal
)) {
1489 set_domain_online_request(child
->domain
);
1490 if (!(child
->domain
->primary
)) {
1491 primary_domain
->startup
= True
;
1492 primary_domain
->startup_time
= time_mono(NULL
);
1493 set_domain_online_request(primary_domain
);
1499 * We are in idmap child, make sure that we set the
1500 * check_online_event to bring primary domain online.
1502 if (child
== idmap_child()) {
1503 set_domain_online_request(primary_domain
);
1506 /* We might be in the idmap child...*/
1507 if (child
->domain
&& !(child
->domain
->internal
) &&
1508 lp_winbind_offline_logon()) {
1510 set_domain_online_request(child
->domain
);
1512 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1513 /* We need to talk to the primary
1514 * domain as well as the trusted
1515 * domain inside a trusted domain
1518 * set_dc_type_and_flags_trustinfo()
1521 set_domain_online_request(primary_domain
);
1524 child
->lockout_policy_event
= tevent_add_timer(
1525 winbind_event_context(), NULL
, timeval_zero(),
1526 account_lockout_policy_handler
,
1530 if (child
->domain
&& child
->domain
->primary
&&
1531 !USE_KERBEROS_KEYTAB
&&
1532 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1534 struct timeval next_change
;
1536 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1538 child
->machine_password_change_event
= tevent_add_timer(
1539 winbind_event_context(), NULL
, next_change
,
1540 machine_password_change_handler
,
1545 fde
= tevent_add_fd(winbind_event_context(), NULL
, state
.cli
.sock
,
1546 TEVENT_FD_READ
, child_handler
, &state
);
1548 DEBUG(1, ("tevent_add_fd failed\n"));
1555 TALLOC_CTX
*frame
= talloc_stackframe();
1557 ret
= tevent_loop_once(winbind_event_context());
1559 DEBUG(1, ("tevent_loop_once failed: %s\n",
1564 if (child
->domain
&& child
->domain
->startup
&&
1565 (time_mono(NULL
) > child
->domain
->startup_time
+ 30)) {
1566 /* No longer in "startup" mode. */
1567 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1568 child
->domain
->name
));
1569 child
->domain
->startup
= False
;
1576 void winbind_msg_ip_dropped_parent(struct messaging_context
*msg_ctx
,
1579 struct server_id server_id
,
1582 struct winbindd_child
*child
;
1584 winbind_msg_ip_dropped(msg_ctx
, private_data
, msg_type
,
1588 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
1589 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
1590 msg_type
, data
->data
, data
->length
);