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 "nsswitch/wb_reqtrans.h"
33 #include "librpc/gen_ndr/messaging.h"
35 #include "../lib/util/select.h"
36 #include "../libcli/security/security.h"
37 #include "system/select.h"
40 #define DBGC_CLASS DBGC_WINBIND
42 extern bool override_logfile
;
43 extern struct winbindd_methods cache_methods
;
45 /* Read some data from a client connection */
47 static NTSTATUS
child_read_request(struct winbindd_cli_state
*state
)
53 status
= read_data(state
->sock
, (char *)state
->request
,
54 sizeof(*state
->request
));
56 if (!NT_STATUS_IS_OK(status
)) {
57 DEBUG(3, ("child_read_request: read_data failed: %s\n",
62 if (state
->request
->extra_len
== 0) {
63 state
->request
->extra_data
.data
= NULL
;
67 DEBUG(10, ("Need to read %d extra bytes\n", (int)state
->request
->extra_len
));
69 state
->request
->extra_data
.data
=
70 SMB_MALLOC_ARRAY(char, state
->request
->extra_len
+ 1);
72 if (state
->request
->extra_data
.data
== NULL
) {
73 DEBUG(0, ("malloc failed\n"));
74 return NT_STATUS_NO_MEMORY
;
77 /* Ensure null termination */
78 state
->request
->extra_data
.data
[state
->request
->extra_len
] = '\0';
80 status
= read_data(state
->sock
, state
->request
->extra_data
.data
,
81 state
->request
->extra_len
);
83 if (!NT_STATUS_IS_OK(status
)) {
84 DEBUG(0, ("Could not read extra data: %s\n",
91 * Do winbind child async request. This is not simply wb_simple_trans. We have
92 * to do the queueing ourselves because while a request is queued, the child
93 * might have crashed, and we have to re-fork it in the _trigger function.
96 struct wb_child_request_state
{
97 struct tevent_context
*ev
;
98 struct winbindd_child
*child
;
99 struct winbindd_request
*request
;
100 struct winbindd_response
*response
;
103 static bool fork_domain_child(struct winbindd_child
*child
);
105 static void wb_child_request_trigger(struct tevent_req
*req
,
107 static void wb_child_request_done(struct tevent_req
*subreq
);
109 struct tevent_req
*wb_child_request_send(TALLOC_CTX
*mem_ctx
,
110 struct tevent_context
*ev
,
111 struct winbindd_child
*child
,
112 struct winbindd_request
*request
)
114 struct tevent_req
*req
;
115 struct wb_child_request_state
*state
;
117 req
= tevent_req_create(mem_ctx
, &state
,
118 struct wb_child_request_state
);
124 state
->child
= child
;
125 state
->request
= request
;
127 if (!tevent_queue_add(child
->queue
, ev
, req
,
128 wb_child_request_trigger
, NULL
)) {
129 tevent_req_nomem(NULL
, req
);
130 return tevent_req_post(req
, ev
);
135 static void wb_child_request_trigger(struct tevent_req
*req
,
138 struct wb_child_request_state
*state
= tevent_req_data(
139 req
, struct wb_child_request_state
);
140 struct tevent_req
*subreq
;
142 if ((state
->child
->sock
== -1) && (!fork_domain_child(state
->child
))) {
143 tevent_req_error(req
, errno
);
147 subreq
= wb_simple_trans_send(state
, winbind_event_context(), NULL
,
148 state
->child
->sock
, state
->request
);
149 if (tevent_req_nomem(subreq
, req
)) {
152 tevent_req_set_callback(subreq
, wb_child_request_done
, req
);
154 if (!tevent_req_set_endtime(req
, state
->ev
,
155 timeval_current_ofs(300, 0))) {
156 tevent_req_nomem(NULL
, req
);
161 static void wb_child_request_done(struct tevent_req
*subreq
)
163 struct tevent_req
*req
= tevent_req_callback_data(
164 subreq
, struct tevent_req
);
165 struct wb_child_request_state
*state
= tevent_req_data(
166 req
, struct wb_child_request_state
);
169 ret
= wb_simple_trans_recv(subreq
, state
, &state
->response
, &err
);
173 * The basic parent/child communication broke, close
176 close(state
->child
->sock
);
177 state
->child
->sock
= -1;
178 tevent_req_error(req
, err
);
181 tevent_req_done(req
);
184 int wb_child_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
185 struct winbindd_response
**presponse
, int *err
)
187 struct wb_child_request_state
*state
= tevent_req_data(
188 req
, struct wb_child_request_state
);
190 if (tevent_req_is_unix_error(req
, err
)) {
193 *presponse
= talloc_move(mem_ctx
, &state
->response
);
197 static bool winbindd_child_busy(struct winbindd_child
*child
)
199 return tevent_queue_length(child
->queue
) > 0;
202 static struct winbindd_child
*find_idle_child(struct winbindd_domain
*domain
)
206 for (i
=0; i
<lp_winbind_max_domain_connections(); i
++) {
207 if (!winbindd_child_busy(&domain
->children
[i
])) {
208 return &domain
->children
[i
];
215 struct winbindd_child
*choose_domain_child(struct winbindd_domain
*domain
)
217 struct winbindd_child
*result
;
219 result
= find_idle_child(domain
);
220 if (result
!= NULL
) {
223 return &domain
->children
[rand() % lp_winbind_max_domain_connections()];
226 struct dcerpc_binding_handle
*dom_child_handle(struct winbindd_domain
*domain
)
228 struct winbindd_child
*child
;
230 child
= choose_domain_child(domain
);
231 return child
->binding_handle
;
234 struct wb_domain_request_state
{
235 struct tevent_context
*ev
;
236 struct winbindd_domain
*domain
;
237 struct winbindd_child
*child
;
238 struct winbindd_request
*request
;
239 struct winbindd_request
*init_req
;
240 struct winbindd_response
*response
;
243 static void wb_domain_request_gotdc(struct tevent_req
*subreq
);
244 static void wb_domain_request_initialized(struct tevent_req
*subreq
);
245 static void wb_domain_request_done(struct tevent_req
*subreq
);
247 struct tevent_req
*wb_domain_request_send(TALLOC_CTX
*mem_ctx
,
248 struct tevent_context
*ev
,
249 struct winbindd_domain
*domain
,
250 struct winbindd_request
*request
)
252 struct tevent_req
*req
, *subreq
;
253 struct wb_domain_request_state
*state
;
255 req
= tevent_req_create(mem_ctx
, &state
,
256 struct wb_domain_request_state
);
261 state
->child
= choose_domain_child(domain
);
263 if (domain
->initialized
) {
264 subreq
= wb_child_request_send(state
, ev
, state
->child
,
266 if (tevent_req_nomem(subreq
, req
)) {
267 return tevent_req_post(req
, ev
);
269 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
273 state
->domain
= domain
;
275 state
->request
= request
;
277 state
->init_req
= talloc_zero(state
, struct winbindd_request
);
278 if (tevent_req_nomem(state
->init_req
, req
)) {
279 return tevent_req_post(req
, ev
);
282 if (IS_DC
|| domain
->primary
|| domain
->internal
) {
283 /* The primary domain has to find the DC name itself */
284 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
285 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
286 state
->init_req
->data
.init_conn
.is_primary
= domain
->primary
;
287 fstrcpy(state
->init_req
->data
.init_conn
.dcname
, "");
289 subreq
= wb_child_request_send(state
, ev
, state
->child
,
291 if (tevent_req_nomem(subreq
, req
)) {
292 return tevent_req_post(req
, ev
);
294 tevent_req_set_callback(subreq
, wb_domain_request_initialized
,
300 * Ask our DC for a DC name
302 domain
= find_our_domain();
304 /* This is *not* the primary domain, let's ask our DC about a DC
307 state
->init_req
->cmd
= WINBINDD_GETDCNAME
;
308 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
310 subreq
= wb_child_request_send(state
, ev
, state
->child
, request
);
311 if (tevent_req_nomem(subreq
, req
)) {
312 return tevent_req_post(req
, ev
);
314 tevent_req_set_callback(subreq
, wb_domain_request_gotdc
, req
);
318 static void wb_domain_request_gotdc(struct tevent_req
*subreq
)
320 struct tevent_req
*req
= tevent_req_callback_data(
321 subreq
, struct tevent_req
);
322 struct wb_domain_request_state
*state
= tevent_req_data(
323 req
, struct wb_domain_request_state
);
324 struct winbindd_response
*response
;
327 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
330 tevent_req_error(req
, err
);
333 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
334 fstrcpy(state
->init_req
->domain_name
, state
->domain
->name
);
335 state
->init_req
->data
.init_conn
.is_primary
= False
;
336 fstrcpy(state
->init_req
->data
.init_conn
.dcname
,
337 response
->data
.dc_name
);
339 TALLOC_FREE(response
);
341 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
343 if (tevent_req_nomem(subreq
, req
)) {
346 tevent_req_set_callback(subreq
, wb_domain_request_initialized
, req
);
349 static void wb_domain_request_initialized(struct tevent_req
*subreq
)
351 struct tevent_req
*req
= tevent_req_callback_data(
352 subreq
, struct tevent_req
);
353 struct wb_domain_request_state
*state
= tevent_req_data(
354 req
, struct wb_domain_request_state
);
355 struct winbindd_response
*response
;
358 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
361 tevent_req_error(req
, err
);
365 if (!string_to_sid(&state
->domain
->sid
,
366 response
->data
.domain_info
.sid
)) {
367 DEBUG(1,("init_child_recv: Could not convert sid %s "
368 "from string\n", response
->data
.domain_info
.sid
));
369 tevent_req_error(req
, EINVAL
);
372 fstrcpy(state
->domain
->name
, response
->data
.domain_info
.name
);
373 fstrcpy(state
->domain
->alt_name
, response
->data
.domain_info
.alt_name
);
374 state
->domain
->native_mode
= response
->data
.domain_info
.native_mode
;
375 state
->domain
->active_directory
=
376 response
->data
.domain_info
.active_directory
;
377 state
->domain
->initialized
= true;
379 TALLOC_FREE(response
);
381 subreq
= wb_child_request_send(state
, state
->ev
, state
->child
,
383 if (tevent_req_nomem(subreq
, req
)) {
386 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
389 static void wb_domain_request_done(struct tevent_req
*subreq
)
391 struct tevent_req
*req
= tevent_req_callback_data(
392 subreq
, struct tevent_req
);
393 struct wb_domain_request_state
*state
= tevent_req_data(
394 req
, struct wb_domain_request_state
);
397 ret
= wb_child_request_recv(subreq
, talloc_tos(), &state
->response
,
401 tevent_req_error(req
, err
);
404 tevent_req_done(req
);
407 int wb_domain_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
408 struct winbindd_response
**presponse
, int *err
)
410 struct wb_domain_request_state
*state
= tevent_req_data(
411 req
, struct wb_domain_request_state
);
413 if (tevent_req_is_unix_error(req
, err
)) {
416 *presponse
= talloc_move(mem_ctx
, &state
->response
);
420 static void child_process_request(struct winbindd_child
*child
,
421 struct winbindd_cli_state
*state
)
423 struct winbindd_domain
*domain
= child
->domain
;
424 const struct winbindd_child_dispatch_table
*table
= child
->table
;
426 /* Free response data - we may be interrupted and receive another
427 command before being able to send this data off. */
429 state
->response
->result
= WINBINDD_ERROR
;
430 state
->response
->length
= sizeof(struct winbindd_response
);
432 /* as all requests in the child are sync, we can use talloc_tos() */
433 state
->mem_ctx
= talloc_tos();
435 /* Process command */
437 for (; table
->name
; table
++) {
438 if (state
->request
->cmd
== table
->struct_cmd
) {
439 DEBUG(10,("child_process_request: request fn %s\n",
441 state
->response
->result
= table
->struct_fn(domain
, state
);
446 DEBUG(1, ("child_process_request: unknown request fn number %d\n",
447 (int)state
->request
->cmd
));
448 state
->response
->result
= WINBINDD_ERROR
;
451 void setup_child(struct winbindd_domain
*domain
, struct winbindd_child
*child
,
452 const struct winbindd_child_dispatch_table
*table
,
453 const char *logprefix
,
456 if (logprefix
&& logname
) {
457 char *logbase
= NULL
;
462 if (asprintf(&logbase
, "%s", lp_logfile()) < 0) {
463 smb_panic("Internal error: asprintf failed");
466 if ((end
= strrchr_m(logbase
, '/'))) {
470 if (asprintf(&logbase
, "%s", get_dyn_LOGFILEBASE()) < 0) {
471 smb_panic("Internal error: asprintf failed");
475 if (asprintf(&child
->logfilename
, "%s/%s-%s",
476 logbase
, logprefix
, logname
) < 0) {
478 smb_panic("Internal error: asprintf failed");
483 smb_panic("Internal error: logprefix == NULL && "
488 child
->domain
= domain
;
489 child
->table
= table
;
490 child
->queue
= tevent_queue_create(NULL
, "winbind_child");
491 SMB_ASSERT(child
->queue
!= NULL
);
492 child
->binding_handle
= wbint_binding_handle(NULL
, domain
, child
);
493 SMB_ASSERT(child
->binding_handle
!= NULL
);
496 static struct winbindd_child
*winbindd_children
= NULL
;
498 void winbind_child_died(pid_t pid
)
500 struct winbindd_child
*child
;
502 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
503 if (child
->pid
== pid
) {
509 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
513 /* This will be re-added in fork_domain_child() */
515 DLIST_REMOVE(winbindd_children
, child
);
519 /* Ensure any negative cache entries with the netbios or realm names are removed. */
521 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
523 flush_negative_conn_cache_for_domain(domain
->name
);
524 if (*domain
->alt_name
) {
525 flush_negative_conn_cache_for_domain(domain
->alt_name
);
530 * Parent winbindd process sets its own debug level first and then
531 * sends a message to all the winbindd children to adjust their debug
532 * level to that of parents.
535 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
538 struct server_id server_id
,
541 struct winbindd_child
*child
;
543 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
545 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
547 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
549 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
550 (unsigned int)child
->pid
));
552 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
555 strlen((char *) data
->data
) + 1);
559 /* Set our domains as offline and forward the offline message to our children. */
561 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
564 struct server_id server_id
,
567 struct winbindd_child
*child
;
568 struct winbindd_domain
*domain
;
570 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
572 if (!lp_winbind_offline_logon()) {
573 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
577 /* Set our global state as offline. */
578 if (!set_global_winbindd_state_offline()) {
579 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
583 /* Set all our domains as offline. */
584 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
585 if (domain
->internal
) {
588 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
589 set_domain_offline(domain
);
592 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
593 /* Don't send message to internal children. We've already
595 if (!child
->domain
|| winbindd_internal_child(child
)) {
599 /* Or internal domains (this should not be possible....) */
600 if (child
->domain
->internal
) {
604 /* Each winbindd child should only process requests for one domain - make sure
605 we only set it online / offline for that domain. */
607 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
608 (unsigned int)child
->pid
, domain
->name
));
610 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
612 (uint8
*)child
->domain
->name
,
613 strlen(child
->domain
->name
)+1);
617 /* Set our domains as online and forward the online message to our children. */
619 void winbind_msg_online(struct messaging_context
*msg_ctx
,
622 struct server_id server_id
,
625 struct winbindd_child
*child
;
626 struct winbindd_domain
*domain
;
628 DEBUG(10,("winbind_msg_online: got online message.\n"));
630 if (!lp_winbind_offline_logon()) {
631 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
635 /* Set our global state as online. */
636 set_global_winbindd_state_online();
638 smb_nscd_flush_user_cache();
639 smb_nscd_flush_group_cache();
641 /* Set all our domains as online. */
642 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
643 if (domain
->internal
) {
646 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
648 winbindd_flush_negative_conn_cache(domain
);
649 set_domain_online_request(domain
);
651 /* Send an online message to the idmap child when our
652 primary domain comes back online */
654 if ( domain
->primary
) {
655 struct winbindd_child
*idmap
= idmap_child();
657 if ( idmap
->pid
!= 0 ) {
658 messaging_send_buf(msg_ctx
,
659 pid_to_procid(idmap
->pid
),
661 (uint8
*)domain
->name
,
662 strlen(domain
->name
)+1);
667 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
668 /* Don't send message to internal childs. */
669 if (!child
->domain
|| winbindd_internal_child(child
)) {
673 /* Or internal domains (this should not be possible....) */
674 if (child
->domain
->internal
) {
678 /* Each winbindd child should only process requests for one domain - make sure
679 we only set it online / offline for that domain. */
681 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
682 (unsigned int)child
->pid
, child
->domain
->name
));
684 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
686 (uint8
*)child
->domain
->name
,
687 strlen(child
->domain
->name
)+1);
691 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
693 struct winbindd_domain
*domain
;
696 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
697 get_global_winbindd_state_offline() ?
698 "Offline":"Online")) == NULL
) {
702 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
703 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
706 "Online":"Offline")) == NULL
) {
711 buf
= talloc_asprintf_append_buffer(buf
, "\n");
713 DEBUG(5,("collect_onlinestatus: %s", buf
));
718 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
721 struct server_id server_id
,
726 struct server_id
*sender
;
728 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
734 sender
= (struct server_id
*)data
->data
;
736 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
737 if (mem_ctx
== NULL
) {
741 message
= collect_onlinestatus(mem_ctx
);
742 if (message
== NULL
) {
743 talloc_destroy(mem_ctx
);
747 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
748 (uint8
*)message
, strlen(message
) + 1);
750 talloc_destroy(mem_ctx
);
753 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
756 struct server_id server_id
,
759 struct winbindd_child
*child
;
761 DEBUG(10,("winbind_msg_dump_event_list received\n"));
763 dump_event_list(winbind_event_context());
765 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
767 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
768 (unsigned int)child
->pid
));
770 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
777 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
780 struct server_id server_id
,
784 const char *message
= NULL
;
785 struct server_id
*sender
= NULL
;
786 const char *domain
= NULL
;
789 struct winbindd_domain
*dom
= NULL
;
791 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
793 if (!data
|| !data
->data
) {
797 if (data
->length
< sizeof(struct server_id
)) {
801 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
806 sender
= (struct server_id
*)data
->data
;
807 if (data
->length
> sizeof(struct server_id
)) {
808 domain
= (const char *)data
->data
+sizeof(struct server_id
);
813 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
816 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
817 find_domain_from_name_noinit(domain
));
819 talloc_destroy(mem_ctx
);
823 messaging_send_buf(msg_ctx
, *sender
,
824 MSG_WINBIND_DUMP_DOMAIN_LIST
,
825 (uint8_t *)message
, strlen(message
) + 1);
827 talloc_destroy(mem_ctx
);
832 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
834 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
835 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
837 talloc_destroy(mem_ctx
);
841 s
= talloc_asprintf_append(s
, "%s\n", message
);
843 talloc_destroy(mem_ctx
);
848 status
= messaging_send_buf(msg_ctx
, *sender
,
849 MSG_WINBIND_DUMP_DOMAIN_LIST
,
850 (uint8_t *)s
, strlen(s
) + 1);
851 if (!NT_STATUS_IS_OK(status
)) {
852 DEBUG(0,("failed to send message: %s\n",
856 talloc_destroy(mem_ctx
);
859 static void account_lockout_policy_handler(struct event_context
*ctx
,
860 struct timed_event
*te
,
864 struct winbindd_child
*child
=
865 (struct winbindd_child
*)private_data
;
866 TALLOC_CTX
*mem_ctx
= NULL
;
867 struct winbindd_methods
*methods
;
868 struct samr_DomInfo12 lockout_policy
;
871 DEBUG(10,("account_lockout_policy_handler called\n"));
873 TALLOC_FREE(child
->lockout_policy_event
);
875 if ( !winbindd_can_contact_domain( child
->domain
) ) {
876 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
877 "do not have an incoming trust to domain %s\n",
878 child
->domain
->name
));
883 methods
= child
->domain
->methods
;
885 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
887 result
= NT_STATUS_NO_MEMORY
;
889 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
891 TALLOC_FREE(mem_ctx
);
893 if (!NT_STATUS_IS_OK(result
)) {
894 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
898 child
->lockout_policy_event
= event_add_timed(winbind_event_context(), NULL
,
899 timeval_current_ofs(3600, 0),
900 account_lockout_policy_handler
,
904 static time_t get_machine_password_timeout(void)
906 /* until we have gpo support use lp setting */
907 return lp_machine_password_timeout();
910 static bool calculate_next_machine_pwd_change(const char *domain
,
913 time_t pass_last_set_time
;
919 pw
= secrets_fetch_machine_password(domain
,
924 DEBUG(0,("cannot fetch own machine password ????"));
930 timeout
= get_machine_password_timeout();
932 DEBUG(10,("machine password never expires\n"));
936 tv
.tv_sec
= pass_last_set_time
;
937 DEBUG(10, ("password last changed %s\n",
938 timeval_string(talloc_tos(), &tv
, false)));
939 tv
.tv_sec
+= timeout
;
940 DEBUGADD(10, ("password valid until %s\n",
941 timeval_string(talloc_tos(), &tv
, false)));
943 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
944 next_change
= pass_last_set_time
+ timeout
;
945 DEBUG(10,("machine password still valid until: %s\n",
946 http_timestring(talloc_tos(), next_change
)));
947 *t
= timeval_set(next_change
, 0);
949 if (lp_clustering()) {
952 * When having a cluster, we have several
953 * winbinds racing for the password change. In
954 * the machine_password_change_handler()
955 * function we check if someone else was
956 * faster when the event triggers. We add a
957 * 255-second random delay here, so that we
958 * don't run to change the password at the
961 generate_random_buffer(&randbuf
, sizeof(randbuf
));
962 DEBUG(10, ("adding %d seconds randomness\n",
964 t
->tv_sec
+= randbuf
;
969 DEBUG(10,("machine password expired, needs immediate change\n"));
976 static void machine_password_change_handler(struct event_context
*ctx
,
977 struct timed_event
*te
,
981 struct winbindd_child
*child
=
982 (struct winbindd_child
*)private_data
;
983 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
986 struct timeval next_change
;
988 DEBUG(10,("machine_password_change_handler called\n"));
990 TALLOC_FREE(child
->machine_password_change_event
);
992 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
994 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
998 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
999 timeval_string(talloc_tos(), &next_change
, false)));
1001 if (!timeval_expired(&next_change
)) {
1002 DEBUG(10, ("Someone else has already changed the pw\n"));
1006 if (!winbindd_can_contact_domain(child
->domain
)) {
1007 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1008 "do not have an incoming trust to domain %s\n",
1009 child
->domain
->name
));
1013 result
= cm_connect_netlogon(child
->domain
, &netlogon_pipe
);
1014 if (!NT_STATUS_IS_OK(result
)) {
1015 DEBUG(10,("machine_password_change_handler: "
1016 "failed to connect netlogon pipe: %s\n",
1017 nt_errstr(result
)));
1021 frame
= talloc_stackframe();
1023 result
= trust_pw_find_change_and_store_it(netlogon_pipe
,
1025 child
->domain
->name
);
1028 DEBUG(10, ("machine_password_change_handler: "
1029 "trust_pw_find_change_and_store_it returned %s\n",
1030 nt_errstr(result
)));
1032 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1033 DEBUG(3,("machine_password_change_handler: password set returned "
1034 "ACCESS_DENIED. Maybe the trust account "
1035 "password was changed and we didn't know it. "
1036 "Killing connections to domain %s\n",
1037 child
->domain
->name
));
1038 TALLOC_FREE(child
->domain
->conn
.netlogon_pipe
);
1041 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1043 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1047 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1048 timeval_string(talloc_tos(), &next_change
, false)));
1050 if (!NT_STATUS_IS_OK(result
)) {
1053 * In case of failure, give the DC a minute to recover
1055 tmp
= timeval_current_ofs(60, 0);
1056 next_change
= timeval_max(&next_change
, &tmp
);
1060 child
->machine_password_change_event
= event_add_timed(winbind_event_context(), NULL
,
1062 machine_password_change_handler
,
1066 /* Deal with a request to go offline. */
1068 static void child_msg_offline(struct messaging_context
*msg
,
1071 struct server_id server_id
,
1074 struct winbindd_domain
*domain
;
1075 struct winbindd_domain
*primary_domain
= NULL
;
1076 const char *domainname
= (const char *)data
->data
;
1078 if (data
->data
== NULL
|| data
->length
== 0) {
1082 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
1084 if (!lp_winbind_offline_logon()) {
1085 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1089 primary_domain
= find_our_domain();
1091 /* Mark the requested domain offline. */
1093 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1094 if (domain
->internal
) {
1097 if (strequal(domain
->name
, domainname
)) {
1098 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1099 set_domain_offline(domain
);
1100 /* we are in the trusted domain, set the primary domain
1102 if (domain
!= primary_domain
) {
1103 set_domain_offline(primary_domain
);
1109 /* Deal with a request to go online. */
1111 static void child_msg_online(struct messaging_context
*msg
,
1114 struct server_id server_id
,
1117 struct winbindd_domain
*domain
;
1118 struct winbindd_domain
*primary_domain
= NULL
;
1119 const char *domainname
= (const char *)data
->data
;
1121 if (data
->data
== NULL
|| data
->length
== 0) {
1125 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1127 if (!lp_winbind_offline_logon()) {
1128 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1132 primary_domain
= find_our_domain();
1134 /* Set our global state as online. */
1135 set_global_winbindd_state_online();
1137 /* Try and mark everything online - delete any negative cache entries
1138 to force a reconnect now. */
1140 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1141 if (domain
->internal
) {
1144 if (strequal(domain
->name
, domainname
)) {
1145 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1146 winbindd_flush_negative_conn_cache(domain
);
1147 set_domain_online_request(domain
);
1149 /* we can be in trusted domain, which will contact primary domain
1150 * we have to bring primary domain online in trusted domain process
1151 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1152 * --> contact_domain = find_our_domain()
1154 if (domain
!= primary_domain
) {
1155 winbindd_flush_negative_conn_cache(primary_domain
);
1156 set_domain_online_request(primary_domain
);
1162 static void child_msg_dump_event_list(struct messaging_context
*msg
,
1165 struct server_id server_id
,
1168 DEBUG(5,("child_msg_dump_event_list received\n"));
1170 dump_event_list(winbind_event_context());
1173 bool winbindd_reinit_after_fork(const char *logfilename
)
1175 struct winbindd_domain
*domain
;
1176 struct winbindd_child
*cl
;
1179 status
= reinit_after_fork(
1180 winbind_messaging_context(),
1181 winbind_event_context(),
1184 if (!NT_STATUS_IS_OK(status
)) {
1185 DEBUG(0,("reinit_after_fork() failed\n"));
1189 close_conns_after_fork();
1191 if (!override_logfile
&& logfilename
) {
1192 lp_set_logfile(logfilename
);
1196 if (!winbindd_setup_sig_term_handler(false))
1198 if (!winbindd_setup_sig_hup_handler(override_logfile
? NULL
:
1202 /* Stop zombies in children */
1205 /* Don't handle the same messages as our parent. */
1206 messaging_deregister(winbind_messaging_context(),
1207 MSG_SMB_CONF_UPDATED
, NULL
);
1208 messaging_deregister(winbind_messaging_context(),
1209 MSG_SHUTDOWN
, NULL
);
1210 messaging_deregister(winbind_messaging_context(),
1211 MSG_WINBIND_OFFLINE
, NULL
);
1212 messaging_deregister(winbind_messaging_context(),
1213 MSG_WINBIND_ONLINE
, NULL
);
1214 messaging_deregister(winbind_messaging_context(),
1215 MSG_WINBIND_ONLINESTATUS
, NULL
);
1216 messaging_deregister(winbind_messaging_context(),
1217 MSG_DUMP_EVENT_LIST
, NULL
);
1218 messaging_deregister(winbind_messaging_context(),
1219 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1220 messaging_deregister(winbind_messaging_context(),
1223 /* We have destroyed all events in the winbindd_event_context
1224 * in reinit_after_fork(), so clean out all possible pending
1225 * event pointers. */
1227 /* Deal with check_online_events. */
1229 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1230 TALLOC_FREE(domain
->check_online_event
);
1233 /* Ensure we're not handling a credential cache event inherited
1234 * from our parent. */
1236 ccache_remove_all_after_fork();
1238 /* Destroy all possible events in child list. */
1239 for (cl
= winbindd_children
; cl
!= NULL
; cl
= cl
->next
) {
1240 TALLOC_FREE(cl
->lockout_policy_event
);
1241 TALLOC_FREE(cl
->machine_password_change_event
);
1243 /* Children should never be able to send
1244 * each other messages, all messages must
1245 * go through the parent.
1250 * This is a little tricky, children must not
1251 * send an MSG_WINBIND_ONLINE message to idmap_child().
1252 * If we are in a child of our primary domain or
1253 * in the process created by fork_child_dc_connect(),
1254 * and the primary domain cannot go online,
1255 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1256 * periodically to idmap_child().
1258 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1259 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1260 * ---> init_dc_connection() ---> cm_open_connection --->
1261 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1262 * idmap_child(). Disallow children sending messages
1263 * to each other, all messages must go through the parent.
1272 * In a child there will be only one domain, reference that here.
1274 static struct winbindd_domain
*child_domain
;
1276 struct winbindd_domain
*wb_child_domain(void)
1278 return child_domain
;
1281 static bool fork_domain_child(struct winbindd_child
*child
)
1284 struct winbindd_cli_state state
;
1285 struct winbindd_request request
;
1286 struct winbindd_response response
;
1287 struct winbindd_domain
*primary_domain
= NULL
;
1289 if (child
->domain
) {
1290 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1291 child
->domain
->name
));
1293 DEBUG(10, ("fork_domain_child called without domain.\n"));
1296 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1297 DEBUG(0, ("Could not open child pipe: %s\n",
1303 state
.pid
= sys_getpid();
1304 state
.request
= &request
;
1305 state
.response
= &response
;
1307 child
->pid
= sys_fork();
1309 if (child
->pid
== -1) {
1310 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1314 if (child
->pid
!= 0) {
1317 child
->next
= child
->prev
= NULL
;
1318 DLIST_ADD(winbindd_children
, child
);
1319 child
->sock
= fdpair
[1];
1324 child_domain
= child
->domain
;
1326 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1328 state
.sock
= fdpair
[0];
1331 if (!winbindd_reinit_after_fork(child
->logfilename
)) {
1335 /* Handle online/offline messages. */
1336 messaging_register(winbind_messaging_context(), NULL
,
1337 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1338 messaging_register(winbind_messaging_context(), NULL
,
1339 MSG_WINBIND_ONLINE
, child_msg_online
);
1340 messaging_register(winbind_messaging_context(), NULL
,
1341 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1342 messaging_register(winbind_messaging_context(), NULL
,
1343 MSG_DEBUG
, debug_message
);
1344 messaging_register(winbind_messaging_context(), NULL
,
1345 MSG_WINBIND_IP_DROPPED
,
1346 winbind_msg_ip_dropped
);
1349 primary_domain
= find_our_domain();
1351 if (primary_domain
== NULL
) {
1352 smb_panic("no primary domain found");
1355 /* It doesn't matter if we allow cache login,
1356 * try to bring domain online after fork. */
1357 if ( child
->domain
) {
1358 child
->domain
->startup
= True
;
1359 child
->domain
->startup_time
= time_mono(NULL
);
1360 /* we can be in primary domain or in trusted domain
1361 * If we are in trusted domain, set the primary domain
1362 * in start-up mode */
1363 if (!(child
->domain
->internal
)) {
1364 set_domain_online_request(child
->domain
);
1365 if (!(child
->domain
->primary
)) {
1366 primary_domain
->startup
= True
;
1367 primary_domain
->startup_time
= time_mono(NULL
);
1368 set_domain_online_request(primary_domain
);
1374 * We are in idmap child, make sure that we set the
1375 * check_online_event to bring primary domain online.
1377 if (child
== idmap_child()) {
1378 set_domain_online_request(primary_domain
);
1381 /* We might be in the idmap child...*/
1382 if (child
->domain
&& !(child
->domain
->internal
) &&
1383 lp_winbind_offline_logon()) {
1385 set_domain_online_request(child
->domain
);
1387 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1388 /* We need to talk to the primary
1389 * domain as well as the trusted
1390 * domain inside a trusted domain
1393 * set_dc_type_and_flags_trustinfo()
1396 set_domain_online_request(primary_domain
);
1399 child
->lockout_policy_event
= event_add_timed(
1400 winbind_event_context(), NULL
, timeval_zero(),
1401 account_lockout_policy_handler
,
1405 if (child
->domain
&& child
->domain
->primary
&&
1406 !USE_KERBEROS_KEYTAB
&&
1407 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1409 struct timeval next_change
;
1411 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1413 child
->machine_password_change_event
= event_add_timed(
1414 winbind_event_context(), NULL
, next_change
,
1415 machine_password_change_handler
,
1423 struct pollfd
*pfds
;
1428 TALLOC_CTX
*frame
= talloc_stackframe();
1429 struct iovec iov
[2];
1433 if (run_events_poll(winbind_event_context(), 0, NULL
, 0)) {
1438 if (child
->domain
&& child
->domain
->startup
&&
1439 (time_mono(NULL
) > child
->domain
->startup_time
+ 30)) {
1440 /* No longer in "startup" mode. */
1441 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1442 child
->domain
->name
));
1443 child
->domain
->startup
= False
;
1446 pfds
= TALLOC_ZERO_P(talloc_tos(), struct pollfd
);
1448 DEBUG(1, ("talloc failed\n"));
1452 pfds
->fd
= state
.sock
;
1453 pfds
->events
= POLLIN
|POLLHUP
;
1458 if (!event_add_to_poll_args(
1459 winbind_event_context(), talloc_tos(),
1460 &pfds
, &num_pfds
, &timeout
)) {
1461 DEBUG(1, ("event_add_to_poll_args failed\n"));
1464 tp
= get_timed_events_timeout(winbind_event_context(), &t
);
1466 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1467 (unsigned int)tp
->tv_sec
, (unsigned int)tp
->tv_usec
));
1470 ret
= sys_poll(pfds
, num_pfds
, timeout
);
1472 if (run_events_poll(winbind_event_context(), ret
,
1474 /* We got a signal - continue. */
1482 DEBUG(11,("nothing is ready yet, continue\n"));
1487 if (ret
== -1 && errno
== EINTR
) {
1488 /* We got a signal - continue. */
1493 if (ret
== -1 && errno
!= EINTR
) {
1494 DEBUG(0,("poll error occured\n"));
1500 /* fetch a request from the main daemon */
1501 status
= child_read_request(&state
);
1503 if (!NT_STATUS_IS_OK(status
)) {
1504 /* we lost contact with our parent */
1508 DEBUG(4,("child daemon request %d\n", (int)state
.request
->cmd
));
1510 ZERO_STRUCTP(state
.response
);
1511 state
.request
->null_term
= '\0';
1512 state
.mem_ctx
= frame
;
1513 child_process_request(child
, &state
);
1515 DEBUG(4, ("Finished processing child request %d\n",
1516 (int)state
.request
->cmd
));
1518 SAFE_FREE(state
.request
->extra_data
.data
);
1520 iov
[0].iov_base
= (void *)state
.response
;
1521 iov
[0].iov_len
= sizeof(struct winbindd_response
);
1524 if (state
.response
->length
> sizeof(struct winbindd_response
)) {
1526 (void *)state
.response
->extra_data
.data
;
1527 iov
[1].iov_len
= state
.response
->length
-iov
[0].iov_len
;
1531 DEBUG(10, ("Writing %d bytes to parent\n",
1532 (int)state
.response
->length
));
1534 if (write_data_iov(state
.sock
, iov
, iov_count
) !=
1535 state
.response
->length
) {
1536 DEBUG(0, ("Could not write result\n"));
1543 void winbind_msg_ip_dropped_parent(struct messaging_context
*msg_ctx
,
1546 struct server_id server_id
,
1549 struct winbindd_child
*child
;
1551 winbind_msg_ip_dropped(msg_ctx
, private_data
, msg_type
,
1555 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
1556 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
1557 msg_type
, data
->data
, data
->length
);