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/libwbclient/wbc_async.h"
35 #define DBGC_CLASS DBGC_WINBIND
37 extern bool override_logfile
;
38 extern struct winbindd_methods cache_methods
;
40 static struct winbindd_child
*children
= NULL
;
42 /* Read some data from a client connection */
44 static NTSTATUS
child_read_request(struct winbindd_cli_state
*state
)
50 status
= read_data(state
->sock
, (char *)state
->request
,
51 sizeof(*state
->request
));
53 if (!NT_STATUS_IS_OK(status
)) {
54 DEBUG(3, ("child_read_request: read_data failed: %s\n",
59 if (state
->request
->extra_len
== 0) {
60 state
->request
->extra_data
.data
= NULL
;
64 DEBUG(10, ("Need to read %d extra bytes\n", (int)state
->request
->extra_len
));
66 state
->request
->extra_data
.data
=
67 SMB_MALLOC_ARRAY(char, state
->request
->extra_len
+ 1);
69 if (state
->request
->extra_data
.data
== NULL
) {
70 DEBUG(0, ("malloc failed\n"));
71 return NT_STATUS_NO_MEMORY
;
74 /* Ensure null termination */
75 state
->request
->extra_data
.data
[state
->request
->extra_len
] = '\0';
77 status
= read_data(state
->sock
, state
->request
->extra_data
.data
,
78 state
->request
->extra_len
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 DEBUG(0, ("Could not read extra data: %s\n",
88 * Do winbind child async request. This is not simply wb_simple_trans. We have
89 * to do the queueing ourselves because while a request is queued, the child
90 * might have crashed, and we have to re-fork it in the _trigger function.
93 struct wb_child_request_state
{
94 struct tevent_context
*ev
;
95 struct winbindd_child
*child
;
96 struct winbindd_request
*request
;
97 struct winbindd_response
*response
;
100 static bool fork_domain_child(struct winbindd_child
*child
);
102 static void wb_child_request_trigger(struct tevent_req
*req
,
104 static void wb_child_request_done(struct tevent_req
*subreq
);
106 struct tevent_req
*wb_child_request_send(TALLOC_CTX
*mem_ctx
,
107 struct tevent_context
*ev
,
108 struct winbindd_child
*child
,
109 struct winbindd_request
*request
)
111 struct tevent_req
*req
;
112 struct wb_child_request_state
*state
;
114 req
= tevent_req_create(mem_ctx
, &state
,
115 struct wb_child_request_state
);
121 state
->child
= child
;
122 state
->request
= request
;
124 if (!tevent_queue_add(child
->queue
, ev
, req
,
125 wb_child_request_trigger
, NULL
)) {
126 tevent_req_nomem(NULL
, req
);
127 return tevent_req_post(req
, ev
);
132 static void wb_child_request_trigger(struct tevent_req
*req
,
135 struct wb_child_request_state
*state
= tevent_req_data(
136 req
, struct wb_child_request_state
);
137 struct tevent_req
*subreq
;
139 if ((state
->child
->sock
== -1) && (!fork_domain_child(state
->child
))) {
140 tevent_req_error(req
, errno
);
144 subreq
= wb_simple_trans_send(state
, winbind_event_context(), NULL
,
145 state
->child
->sock
, state
->request
);
146 if (tevent_req_nomem(subreq
, req
)) {
149 tevent_req_set_callback(subreq
, wb_child_request_done
, req
);
151 if (!tevent_req_set_endtime(req
, state
->ev
,
152 timeval_current_ofs(300, 0))) {
153 tevent_req_nomem(NULL
, req
);
158 static void wb_child_request_done(struct tevent_req
*subreq
)
160 struct tevent_req
*req
= tevent_req_callback_data(
161 subreq
, struct tevent_req
);
162 struct wb_child_request_state
*state
= tevent_req_data(
163 req
, struct wb_child_request_state
);
166 ret
= wb_simple_trans_recv(subreq
, state
, &state
->response
, &err
);
170 * The basic parent/child communication broke, close
173 close(state
->child
->sock
);
174 state
->child
->sock
= -1;
175 DLIST_REMOVE(children
, state
->child
);
176 tevent_req_error(req
, err
);
179 tevent_req_done(req
);
182 int wb_child_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
183 struct winbindd_response
**presponse
, int *err
)
185 struct wb_child_request_state
*state
= tevent_req_data(
186 req
, struct wb_child_request_state
);
188 if (tevent_req_is_unix_error(req
, err
)) {
191 *presponse
= talloc_move(mem_ctx
, &state
->response
);
195 struct wb_domain_request_state
{
196 struct tevent_context
*ev
;
197 struct winbindd_domain
*domain
;
198 struct winbindd_request
*request
;
199 struct winbindd_request
*init_req
;
200 struct winbindd_response
*response
;
203 static void wb_domain_request_gotdc(struct tevent_req
*subreq
);
204 static void wb_domain_request_initialized(struct tevent_req
*subreq
);
205 static void wb_domain_request_done(struct tevent_req
*subreq
);
207 struct tevent_req
*wb_domain_request_send(TALLOC_CTX
*mem_ctx
,
208 struct tevent_context
*ev
,
209 struct winbindd_domain
*domain
,
210 struct winbindd_request
*request
)
212 struct tevent_req
*req
, *subreq
;
213 struct wb_domain_request_state
*state
;
215 req
= tevent_req_create(mem_ctx
, &state
,
216 struct wb_domain_request_state
);
221 if (domain
->initialized
) {
222 subreq
= wb_child_request_send(state
, ev
, &domain
->child
,
224 if (tevent_req_nomem(subreq
, req
)) {
225 return tevent_req_post(req
, ev
);
227 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
231 state
->domain
= domain
;
233 state
->request
= request
;
235 state
->init_req
= talloc_zero(state
, struct winbindd_request
);
236 if (tevent_req_nomem(state
->init_req
, req
)) {
237 return tevent_req_post(req
, ev
);
240 if (IS_DC
|| domain
->primary
|| domain
->internal
) {
241 /* The primary domain has to find the DC name itself */
242 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
243 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
244 state
->init_req
->data
.init_conn
.is_primary
=
245 domain
->primary
? true : false;
246 fstrcpy(state
->init_req
->data
.init_conn
.dcname
, "");
248 subreq
= wb_child_request_send(state
, ev
, &domain
->child
,
250 if (tevent_req_nomem(subreq
, req
)) {
251 return tevent_req_post(req
, ev
);
253 tevent_req_set_callback(subreq
, wb_domain_request_initialized
,
259 * Ask our DC for a DC name
261 domain
= find_our_domain();
263 /* This is *not* the primary domain, let's ask our DC about a DC
266 state
->init_req
->cmd
= WINBINDD_GETDCNAME
;
267 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
269 subreq
= wb_child_request_send(state
, ev
, &domain
->child
, request
);
270 if (tevent_req_nomem(subreq
, req
)) {
271 return tevent_req_post(req
, ev
);
273 tevent_req_set_callback(subreq
, wb_domain_request_gotdc
, req
);
277 static void wb_domain_request_gotdc(struct tevent_req
*subreq
)
279 struct tevent_req
*req
= tevent_req_callback_data(
280 subreq
, struct tevent_req
);
281 struct wb_domain_request_state
*state
= tevent_req_data(
282 req
, struct wb_domain_request_state
);
283 struct winbindd_response
*response
;
286 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
289 tevent_req_error(req
, err
);
292 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
293 fstrcpy(state
->init_req
->domain_name
, state
->domain
->name
);
294 state
->init_req
->data
.init_conn
.is_primary
= False
;
295 fstrcpy(state
->init_req
->data
.init_conn
.dcname
,
296 response
->data
.dc_name
);
298 TALLOC_FREE(response
);
300 subreq
= wb_child_request_send(state
, state
->ev
, &state
->domain
->child
,
302 if (tevent_req_nomem(subreq
, req
)) {
305 tevent_req_set_callback(subreq
, wb_domain_request_initialized
, req
);
308 static void wb_domain_request_initialized(struct tevent_req
*subreq
)
310 struct tevent_req
*req
= tevent_req_callback_data(
311 subreq
, struct tevent_req
);
312 struct wb_domain_request_state
*state
= tevent_req_data(
313 req
, struct wb_domain_request_state
);
314 struct winbindd_response
*response
;
317 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
320 tevent_req_error(req
, err
);
324 if (!string_to_sid(&state
->domain
->sid
,
325 response
->data
.domain_info
.sid
)) {
326 DEBUG(1,("init_child_recv: Could not convert sid %s "
327 "from string\n", response
->data
.domain_info
.sid
));
328 tevent_req_error(req
, EINVAL
);
331 fstrcpy(state
->domain
->name
, response
->data
.domain_info
.name
);
332 fstrcpy(state
->domain
->alt_name
, response
->data
.domain_info
.alt_name
);
333 state
->domain
->native_mode
= response
->data
.domain_info
.native_mode
;
334 state
->domain
->active_directory
=
335 response
->data
.domain_info
.active_directory
;
336 state
->domain
->initialized
= true;
338 TALLOC_FREE(response
);
340 subreq
= wb_child_request_send(state
, state
->ev
, &state
->domain
->child
,
342 if (tevent_req_nomem(subreq
, req
)) {
345 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
348 static void wb_domain_request_done(struct tevent_req
*subreq
)
350 struct tevent_req
*req
= tevent_req_callback_data(
351 subreq
, struct tevent_req
);
352 struct wb_domain_request_state
*state
= tevent_req_data(
353 req
, struct wb_domain_request_state
);
356 ret
= wb_child_request_recv(subreq
, talloc_tos(), &state
->response
,
360 tevent_req_error(req
, err
);
363 tevent_req_done(req
);
366 int wb_domain_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
367 struct winbindd_response
**presponse
, int *err
)
369 struct wb_domain_request_state
*state
= tevent_req_data(
370 req
, struct wb_domain_request_state
);
372 if (tevent_req_is_unix_error(req
, err
)) {
375 *presponse
= talloc_move(mem_ctx
, &state
->response
);
379 struct domain_request_state
{
380 struct winbindd_domain
*domain
;
381 struct winbindd_request
*request
;
382 struct winbindd_response
*response
;
383 void (*continuation
)(void *private_data_data
, bool success
);
384 void *private_data_data
;
387 static void async_domain_request_done(struct tevent_req
*req
);
389 void async_domain_request(TALLOC_CTX
*mem_ctx
,
390 struct winbindd_domain
*domain
,
391 struct winbindd_request
*request
,
392 struct winbindd_response
*response
,
393 void (*continuation
)(void *private_data_data
, bool success
),
394 void *private_data_data
)
396 struct tevent_req
*subreq
;
397 struct domain_request_state
*state
;
399 state
= TALLOC_P(mem_ctx
, struct domain_request_state
);
401 DEBUG(0, ("talloc failed\n"));
402 continuation(private_data_data
, False
);
406 state
->domain
= domain
;
407 state
->request
= request
;
408 state
->response
= response
;
409 state
->continuation
= continuation
;
410 state
->private_data_data
= private_data_data
;
412 subreq
= wb_domain_request_send(state
, winbind_event_context(),
414 if (subreq
== NULL
) {
415 DEBUG(5, ("wb_domain_request_send failed\n"));
416 continuation(private_data_data
, false);
419 tevent_req_set_callback(subreq
, async_domain_request_done
, state
);
422 static void async_domain_request_done(struct tevent_req
*req
)
424 struct domain_request_state
*state
= tevent_req_callback_data(
425 req
, struct domain_request_state
);
426 struct winbindd_response
*response
;
429 ret
= wb_domain_request_recv(req
, state
, &response
, &err
);
432 DEBUG(5, ("wb_domain_request returned %s\n", strerror(err
)));
433 state
->continuation(state
->private_data_data
, false);
436 *(state
->response
) = *response
;
437 state
->continuation(state
->private_data_data
, true);
440 static void recvfrom_child(void *private_data_data
, bool success
)
442 struct winbindd_cli_state
*state
=
443 talloc_get_type_abort(private_data_data
, struct winbindd_cli_state
);
444 enum winbindd_result result
= state
->response
->result
;
446 /* This is an optimization: The child has written directly to the
447 * response buffer. The request itself is still in pending state,
448 * state that in the result code. */
450 state
->response
->result
= WINBINDD_PENDING
;
452 if ((!success
) || (result
!= WINBINDD_OK
)) {
453 request_error(state
);
460 void sendto_domain(struct winbindd_cli_state
*state
,
461 struct winbindd_domain
*domain
)
463 async_domain_request(state
->mem_ctx
, domain
,
464 state
->request
, state
->response
,
465 recvfrom_child
, state
);
468 static void child_process_request(struct winbindd_child
*child
,
469 struct winbindd_cli_state
*state
)
471 struct winbindd_domain
*domain
= child
->domain
;
472 const struct winbindd_child_dispatch_table
*table
= child
->table
;
474 /* Free response data - we may be interrupted and receive another
475 command before being able to send this data off. */
477 state
->response
->result
= WINBINDD_ERROR
;
478 state
->response
->length
= sizeof(struct winbindd_response
);
480 /* as all requests in the child are sync, we can use talloc_tos() */
481 state
->mem_ctx
= talloc_tos();
483 /* Process command */
485 for (; table
->name
; table
++) {
486 if (state
->request
->cmd
== table
->struct_cmd
) {
487 DEBUG(10,("child_process_request: request fn %s\n",
489 state
->response
->result
= table
->struct_fn(domain
, state
);
494 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
495 (int)state
->request
->cmd
));
496 state
->response
->result
= WINBINDD_ERROR
;
499 void setup_child(struct winbindd_domain
*domain
, struct winbindd_child
*child
,
500 const struct winbindd_child_dispatch_table
*table
,
501 const char *logprefix
,
504 if (logprefix
&& logname
) {
505 if (asprintf(&child
->logfilename
, "%s/%s-%s",
506 get_dyn_LOGFILEBASE(), logprefix
, logname
) < 0) {
507 smb_panic("Internal error: asprintf failed");
510 smb_panic("Internal error: logprefix == NULL && "
515 child
->domain
= domain
;
516 child
->table
= table
;
517 child
->queue
= tevent_queue_create(NULL
, "winbind_child");
518 SMB_ASSERT(child
->queue
!= NULL
);
519 child
->rpccli
= wbint_rpccli_create(NULL
, domain
, child
);
520 SMB_ASSERT(child
->rpccli
!= NULL
);
523 void winbind_child_died(pid_t pid
)
525 struct winbindd_child
*child
;
527 for (child
= children
; child
!= NULL
; child
= child
->next
) {
528 if (child
->pid
== pid
) {
534 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
538 /* This will be re-added in fork_domain_child() */
540 DLIST_REMOVE(children
, child
);
544 /* Ensure any negative cache entries with the netbios or realm names are removed. */
546 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
548 flush_negative_conn_cache_for_domain(domain
->name
);
549 if (*domain
->alt_name
) {
550 flush_negative_conn_cache_for_domain(domain
->alt_name
);
555 * Parent winbindd process sets its own debug level first and then
556 * sends a message to all the winbindd children to adjust their debug
557 * level to that of parents.
560 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
563 struct server_id server_id
,
566 struct winbindd_child
*child
;
568 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
570 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
572 for (child
= children
; child
!= NULL
; child
= child
->next
) {
574 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
575 (unsigned int)child
->pid
));
577 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
580 strlen((char *) data
->data
) + 1);
584 /* Set our domains as offline and forward the offline message to our children. */
586 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
589 struct server_id server_id
,
592 struct winbindd_child
*child
;
593 struct winbindd_domain
*domain
;
595 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
597 if (!lp_winbind_offline_logon()) {
598 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
602 /* Set our global state as offline. */
603 if (!set_global_winbindd_state_offline()) {
604 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
608 /* Set all our domains as offline. */
609 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
610 if (domain
->internal
) {
613 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
614 set_domain_offline(domain
);
617 for (child
= children
; child
!= NULL
; child
= child
->next
) {
618 /* Don't send message to internal childs. We've already
620 if (!child
->domain
|| winbindd_internal_child(child
)) {
624 /* Or internal domains (this should not be possible....) */
625 if (child
->domain
->internal
) {
629 /* Each winbindd child should only process requests for one domain - make sure
630 we only set it online / offline for that domain. */
632 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
633 (unsigned int)child
->pid
, domain
->name
));
635 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
637 (uint8
*)child
->domain
->name
,
638 strlen(child
->domain
->name
)+1);
642 /* Set our domains as online and forward the online message to our children. */
644 void winbind_msg_online(struct messaging_context
*msg_ctx
,
647 struct server_id server_id
,
650 struct winbindd_child
*child
;
651 struct winbindd_domain
*domain
;
653 DEBUG(10,("winbind_msg_online: got online message.\n"));
655 if (!lp_winbind_offline_logon()) {
656 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
660 /* Set our global state as online. */
661 set_global_winbindd_state_online();
663 smb_nscd_flush_user_cache();
664 smb_nscd_flush_group_cache();
666 /* Set all our domains as online. */
667 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
668 if (domain
->internal
) {
671 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
673 winbindd_flush_negative_conn_cache(domain
);
674 set_domain_online_request(domain
);
676 /* Send an online message to the idmap child when our
677 primary domain comes back online */
679 if ( domain
->primary
) {
680 struct winbindd_child
*idmap
= idmap_child();
682 if ( idmap
->pid
!= 0 ) {
683 messaging_send_buf(msg_ctx
,
684 pid_to_procid(idmap
->pid
),
686 (uint8
*)domain
->name
,
687 strlen(domain
->name
)+1);
692 for (child
= children
; child
!= NULL
; child
= child
->next
) {
693 /* Don't send message to internal childs. */
694 if (!child
->domain
|| winbindd_internal_child(child
)) {
698 /* Or internal domains (this should not be possible....) */
699 if (child
->domain
->internal
) {
703 /* Each winbindd child should only process requests for one domain - make sure
704 we only set it online / offline for that domain. */
706 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
707 (unsigned int)child
->pid
, child
->domain
->name
));
709 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
711 (uint8
*)child
->domain
->name
,
712 strlen(child
->domain
->name
)+1);
716 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
718 struct winbindd_domain
*domain
;
721 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
722 get_global_winbindd_state_offline() ?
723 "Offline":"Online")) == NULL
) {
727 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
728 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
731 "Online":"Offline")) == NULL
) {
736 buf
= talloc_asprintf_append_buffer(buf
, "\n");
738 DEBUG(5,("collect_onlinestatus: %s", buf
));
743 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
746 struct server_id server_id
,
751 struct server_id
*sender
;
753 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
759 sender
= (struct server_id
*)data
->data
;
761 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
762 if (mem_ctx
== NULL
) {
766 message
= collect_onlinestatus(mem_ctx
);
767 if (message
== NULL
) {
768 talloc_destroy(mem_ctx
);
772 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
773 (uint8
*)message
, strlen(message
) + 1);
775 talloc_destroy(mem_ctx
);
778 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
781 struct server_id server_id
,
784 struct winbindd_child
*child
;
786 DEBUG(10,("winbind_msg_dump_event_list received\n"));
788 dump_event_list(winbind_event_context());
790 for (child
= children
; child
!= NULL
; child
= child
->next
) {
792 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
793 (unsigned int)child
->pid
));
795 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
802 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
805 struct server_id server_id
,
809 const char *message
= NULL
;
810 struct server_id
*sender
= NULL
;
811 const char *domain
= NULL
;
814 struct winbindd_domain
*dom
= NULL
;
816 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
818 if (!data
|| !data
->data
) {
822 if (data
->length
< sizeof(struct server_id
)) {
826 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
831 sender
= (struct server_id
*)data
->data
;
832 if (data
->length
> sizeof(struct server_id
)) {
833 domain
= (const char *)data
->data
+sizeof(struct server_id
);
838 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
841 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
842 find_domain_from_name_noinit(domain
));
844 talloc_destroy(mem_ctx
);
848 messaging_send_buf(msg_ctx
, *sender
,
849 MSG_WINBIND_DUMP_DOMAIN_LIST
,
850 (uint8_t *)message
, strlen(message
) + 1);
852 talloc_destroy(mem_ctx
);
857 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
859 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
860 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
862 talloc_destroy(mem_ctx
);
866 s
= talloc_asprintf_append(s
, "%s\n", message
);
868 talloc_destroy(mem_ctx
);
873 status
= messaging_send_buf(msg_ctx
, *sender
,
874 MSG_WINBIND_DUMP_DOMAIN_LIST
,
875 (uint8_t *)s
, strlen(s
) + 1);
876 if (!NT_STATUS_IS_OK(status
)) {
877 DEBUG(0,("failed to send message: %s\n",
881 talloc_destroy(mem_ctx
);
884 static void account_lockout_policy_handler(struct event_context
*ctx
,
885 struct timed_event
*te
,
889 struct winbindd_child
*child
=
890 (struct winbindd_child
*)private_data
;
891 TALLOC_CTX
*mem_ctx
= NULL
;
892 struct winbindd_methods
*methods
;
893 struct samr_DomInfo12 lockout_policy
;
896 DEBUG(10,("account_lockout_policy_handler called\n"));
898 TALLOC_FREE(child
->lockout_policy_event
);
900 if ( !winbindd_can_contact_domain( child
->domain
) ) {
901 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
902 "do not have an incoming trust to domain %s\n",
903 child
->domain
->name
));
908 methods
= child
->domain
->methods
;
910 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
912 result
= NT_STATUS_NO_MEMORY
;
914 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
916 TALLOC_FREE(mem_ctx
);
918 if (!NT_STATUS_IS_OK(result
)) {
919 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
923 child
->lockout_policy_event
= event_add_timed(winbind_event_context(), NULL
,
924 timeval_current_ofs(3600, 0),
925 account_lockout_policy_handler
,
929 static time_t get_machine_password_timeout(void)
931 /* until we have gpo support use lp setting */
932 return lp_machine_password_timeout();
935 static bool calculate_next_machine_pwd_change(const char *domain
,
938 time_t pass_last_set_time
;
944 pw
= secrets_fetch_machine_password(domain
,
949 DEBUG(0,("cannot fetch own machine password ????"));
955 timeout
= get_machine_password_timeout();
957 DEBUG(10,("machine password never expires\n"));
961 tv
.tv_sec
= pass_last_set_time
;
962 DEBUG(10, ("password last changed %s\n",
963 timeval_string(talloc_tos(), &tv
, false)));
964 tv
.tv_sec
+= timeout
;
965 DEBUGADD(10, ("password valid until %s\n",
966 timeval_string(talloc_tos(), &tv
, false)));
968 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
969 next_change
= pass_last_set_time
+ timeout
;
970 DEBUG(10,("machine password still valid until: %s\n",
971 http_timestring(talloc_tos(), next_change
)));
972 *t
= timeval_set(next_change
, 0);
974 if (lp_clustering()) {
977 * When having a cluster, we have several
978 * winbinds racing for the password change. In
979 * the machine_password_change_handler()
980 * function we check if someone else was
981 * faster when the event triggers. We add a
982 * 255-second random delay here, so that we
983 * don't run to change the password at the
986 generate_random_buffer(&randbuf
, sizeof(randbuf
));
987 DEBUG(10, ("adding %d seconds randomness\n",
989 t
->tv_sec
+= randbuf
;
994 DEBUG(10,("machine password expired, needs immediate change\n"));
1001 static void machine_password_change_handler(struct event_context
*ctx
,
1002 struct timed_event
*te
,
1006 struct winbindd_child
*child
=
1007 (struct winbindd_child
*)private_data
;
1008 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
1011 struct timeval next_change
;
1013 DEBUG(10,("machine_password_change_handler called\n"));
1015 TALLOC_FREE(child
->machine_password_change_event
);
1017 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1019 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1023 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1024 timeval_string(talloc_tos(), &next_change
, false)));
1026 if (!timeval_expired(&next_change
)) {
1027 DEBUG(10, ("Someone else has already changed the pw\n"));
1031 if (!winbindd_can_contact_domain(child
->domain
)) {
1032 DEBUG(10,("machine_password_change_handler: Removing myself since I "
1033 "do not have an incoming trust to domain %s\n",
1034 child
->domain
->name
));
1038 result
= cm_connect_netlogon(child
->domain
, &netlogon_pipe
);
1039 if (!NT_STATUS_IS_OK(result
)) {
1040 DEBUG(10,("machine_password_change_handler: "
1041 "failed to connect netlogon pipe: %s\n",
1042 nt_errstr(result
)));
1046 frame
= talloc_stackframe();
1048 result
= trust_pw_find_change_and_store_it(netlogon_pipe
,
1050 child
->domain
->name
);
1053 DEBUG(10, ("machine_password_change_handler: "
1054 "trust_pw_find_change_and_store_it returned %s\n",
1055 nt_errstr(result
)));
1057 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
1058 DEBUG(3,("machine_password_change_handler: password set returned "
1059 "ACCESS_DENIED. Maybe the trust account "
1060 "password was changed and we didn't know it. "
1061 "Killing connections to domain %s\n",
1062 child
->domain
->name
));
1063 TALLOC_FREE(child
->domain
->conn
.netlogon_pipe
);
1066 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
1068 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1072 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1073 timeval_string(talloc_tos(), &next_change
, false)));
1075 if (!NT_STATUS_IS_OK(result
)) {
1078 * In case of failure, give the DC a minute to recover
1080 tmp
= timeval_current_ofs(60, 0);
1081 next_change
= timeval_max(&next_change
, &tmp
);
1085 child
->machine_password_change_event
= event_add_timed(winbind_event_context(), NULL
,
1087 machine_password_change_handler
,
1091 /* Deal with a request to go offline. */
1093 static void child_msg_offline(struct messaging_context
*msg
,
1096 struct server_id server_id
,
1099 struct winbindd_domain
*domain
;
1100 struct winbindd_domain
*primary_domain
= NULL
;
1101 const char *domainname
= (const char *)data
->data
;
1103 if (data
->data
== NULL
|| data
->length
== 0) {
1107 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
1109 if (!lp_winbind_offline_logon()) {
1110 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1114 primary_domain
= find_our_domain();
1116 /* Mark the requested domain offline. */
1118 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1119 if (domain
->internal
) {
1122 if (strequal(domain
->name
, domainname
)) {
1123 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1124 set_domain_offline(domain
);
1125 /* we are in the trusted domain, set the primary domain
1127 if (domain
!= primary_domain
) {
1128 set_domain_offline(primary_domain
);
1134 /* Deal with a request to go online. */
1136 static void child_msg_online(struct messaging_context
*msg
,
1139 struct server_id server_id
,
1142 struct winbindd_domain
*domain
;
1143 struct winbindd_domain
*primary_domain
= NULL
;
1144 const char *domainname
= (const char *)data
->data
;
1146 if (data
->data
== NULL
|| data
->length
== 0) {
1150 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1152 if (!lp_winbind_offline_logon()) {
1153 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1157 primary_domain
= find_our_domain();
1159 /* Set our global state as online. */
1160 set_global_winbindd_state_online();
1162 /* Try and mark everything online - delete any negative cache entries
1163 to force a reconnect now. */
1165 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1166 if (domain
->internal
) {
1169 if (strequal(domain
->name
, domainname
)) {
1170 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1171 winbindd_flush_negative_conn_cache(domain
);
1172 set_domain_online_request(domain
);
1174 /* we can be in trusted domain, which will contact primary domain
1175 * we have to bring primary domain online in trusted domain process
1176 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1177 * --> contact_domain = find_our_domain()
1179 if (domain
!= primary_domain
) {
1180 winbindd_flush_negative_conn_cache(primary_domain
);
1181 set_domain_online_request(primary_domain
);
1187 static void child_msg_dump_event_list(struct messaging_context
*msg
,
1190 struct server_id server_id
,
1193 DEBUG(5,("child_msg_dump_event_list received\n"));
1195 dump_event_list(winbind_event_context());
1198 bool winbindd_reinit_after_fork(const char *logfilename
)
1200 struct winbindd_domain
*domain
;
1201 struct winbindd_child
*cl
;
1203 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1204 winbind_event_context(),
1206 DEBUG(0,("reinit_after_fork() failed\n"));
1210 close_conns_after_fork();
1212 if (!override_logfile
&& logfilename
) {
1213 lp_set_logfile(logfilename
);
1217 if (!winbindd_setup_sig_term_handler(false))
1219 if (!winbindd_setup_sig_hup_handler(override_logfile
? NULL
:
1223 /* Stop zombies in children */
1226 /* Don't handle the same messages as our parent. */
1227 messaging_deregister(winbind_messaging_context(),
1228 MSG_SMB_CONF_UPDATED
, NULL
);
1229 messaging_deregister(winbind_messaging_context(),
1230 MSG_SHUTDOWN
, NULL
);
1231 messaging_deregister(winbind_messaging_context(),
1232 MSG_WINBIND_OFFLINE
, NULL
);
1233 messaging_deregister(winbind_messaging_context(),
1234 MSG_WINBIND_ONLINE
, NULL
);
1235 messaging_deregister(winbind_messaging_context(),
1236 MSG_WINBIND_ONLINESTATUS
, NULL
);
1237 messaging_deregister(winbind_messaging_context(),
1238 MSG_DUMP_EVENT_LIST
, NULL
);
1239 messaging_deregister(winbind_messaging_context(),
1240 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1241 messaging_deregister(winbind_messaging_context(),
1244 /* We have destroyed all events in the winbindd_event_context
1245 * in reinit_after_fork(), so clean out all possible pending
1246 * event pointers. */
1248 /* Deal with check_online_events. */
1250 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1251 TALLOC_FREE(domain
->check_online_event
);
1254 /* Ensure we're not handling a credential cache event inherited
1255 * from our parent. */
1257 ccache_remove_all_after_fork();
1259 /* Destroy all possible events in child list. */
1260 for (cl
= children
; cl
!= NULL
; cl
= cl
->next
) {
1261 TALLOC_FREE(cl
->lockout_policy_event
);
1262 TALLOC_FREE(cl
->machine_password_change_event
);
1264 /* Children should never be able to send
1265 * each other messages, all messages must
1266 * go through the parent.
1271 * This is a little tricky, children must not
1272 * send an MSG_WINBIND_ONLINE message to idmap_child().
1273 * If we are in a child of our primary domain or
1274 * in the process created by fork_child_dc_connect(),
1275 * and the primary domain cannot go online,
1276 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1277 * periodically to idmap_child().
1279 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1280 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1281 * ---> init_dc_connection() ---> cm_open_connection --->
1282 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1283 * idmap_child(). Disallow children sending messages
1284 * to each other, all messages must go through the parent.
1293 * In a child there will be only one domain, reference that here.
1295 static struct winbindd_domain
*child_domain
;
1297 struct winbindd_domain
*wb_child_domain(void)
1299 return child_domain
;
1302 static bool fork_domain_child(struct winbindd_child
*child
)
1305 struct winbindd_cli_state state
;
1306 struct winbindd_request request
;
1307 struct winbindd_response response
;
1308 struct winbindd_domain
*primary_domain
= NULL
;
1310 if (child
->domain
) {
1311 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1312 child
->domain
->name
));
1314 DEBUG(10, ("fork_domain_child called without domain.\n"));
1316 child_domain
= child
->domain
;
1318 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1319 DEBUG(0, ("Could not open child pipe: %s\n",
1325 state
.pid
= sys_getpid();
1326 state
.request
= &request
;
1327 state
.response
= &response
;
1329 child
->pid
= sys_fork();
1331 if (child
->pid
== -1) {
1332 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1336 if (child
->pid
!= 0) {
1339 child
->next
= child
->prev
= NULL
;
1340 DLIST_ADD(children
, child
);
1341 child
->sock
= fdpair
[1];
1347 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1349 state
.sock
= fdpair
[0];
1352 if (!winbindd_reinit_after_fork(child
->logfilename
)) {
1356 /* Handle online/offline messages. */
1357 messaging_register(winbind_messaging_context(), NULL
,
1358 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1359 messaging_register(winbind_messaging_context(), NULL
,
1360 MSG_WINBIND_ONLINE
, child_msg_online
);
1361 messaging_register(winbind_messaging_context(), NULL
,
1362 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1363 messaging_register(winbind_messaging_context(), NULL
,
1364 MSG_DEBUG
, debug_message
);
1366 primary_domain
= find_our_domain();
1368 if (primary_domain
== NULL
) {
1369 smb_panic("no primary domain found");
1372 /* It doesn't matter if we allow cache login,
1373 * try to bring domain online after fork. */
1374 if ( child
->domain
) {
1375 child
->domain
->startup
= True
;
1376 child
->domain
->startup_time
= time(NULL
);
1377 /* we can be in primary domain or in trusted domain
1378 * If we are in trusted domain, set the primary domain
1379 * in start-up mode */
1380 if (!(child
->domain
->internal
)) {
1381 set_domain_online_request(child
->domain
);
1382 if (!(child
->domain
->primary
)) {
1383 primary_domain
->startup
= True
;
1384 primary_domain
->startup_time
= time(NULL
);
1385 set_domain_online_request(primary_domain
);
1391 * We are in idmap child, make sure that we set the
1392 * check_online_event to bring primary domain online.
1394 if (child
== idmap_child()) {
1395 set_domain_online_request(primary_domain
);
1398 /* We might be in the idmap child...*/
1399 if (child
->domain
&& !(child
->domain
->internal
) &&
1400 lp_winbind_offline_logon()) {
1402 set_domain_online_request(child
->domain
);
1404 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1405 /* We need to talk to the primary
1406 * domain as well as the trusted
1407 * domain inside a trusted domain
1410 * set_dc_type_and_flags_trustinfo()
1413 set_domain_online_request(primary_domain
);
1416 child
->lockout_policy_event
= event_add_timed(
1417 winbind_event_context(), NULL
, timeval_zero(),
1418 account_lockout_policy_handler
,
1422 if (child
->domain
&& child
->domain
->primary
&&
1423 !USE_KERBEROS_KEYTAB
&&
1424 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1426 struct timeval next_change
;
1428 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1430 child
->machine_password_change_event
= event_add_timed(
1431 winbind_event_context(), NULL
, next_change
,
1432 machine_password_change_handler
,
1446 TALLOC_CTX
*frame
= talloc_stackframe();
1447 struct iovec iov
[2];
1451 if (run_events(winbind_event_context(), 0, NULL
, NULL
)) {
1458 if (child
->domain
&& child
->domain
->startup
&&
1459 (now
.tv_sec
> child
->domain
->startup_time
+ 30)) {
1460 /* No longer in "startup" mode. */
1461 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1462 child
->domain
->name
));
1463 child
->domain
->startup
= False
;
1469 if (state
.sock
< 0 || state
.sock
>= FD_SETSIZE
) {
1475 FD_SET(state
.sock
, &r_fds
);
1479 * Initialize this high as event_add_to_select_args()
1480 * uses a timeval_min() on this and next_event. Fix
1481 * from Roel van Meer <rolek@alt001.com>.
1486 event_add_to_select_args(winbind_event_context(), &now
,
1487 &r_fds
, &w_fds
, &t
, &maxfd
);
1488 tp
= get_timed_events_timeout(winbind_event_context(), &t
);
1490 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1491 (unsigned int)tp
->tv_sec
, (unsigned int)tp
->tv_usec
));
1494 ret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, tp
);
1496 if (run_events(winbind_event_context(), ret
, &r_fds
, &w_fds
)) {
1497 /* We got a signal - continue. */
1503 DEBUG(11,("nothing is ready yet, continue\n"));
1508 if (ret
== -1 && errno
== EINTR
) {
1509 /* We got a signal - continue. */
1514 if (ret
== -1 && errno
!= EINTR
) {
1515 DEBUG(0,("select error occured\n"));
1521 /* fetch a request from the main daemon */
1522 status
= child_read_request(&state
);
1524 if (!NT_STATUS_IS_OK(status
)) {
1525 /* we lost contact with our parent */
1529 DEBUG(4,("child daemon request %d\n", (int)state
.request
->cmd
));
1531 ZERO_STRUCTP(state
.response
);
1532 state
.request
->null_term
= '\0';
1533 state
.mem_ctx
= frame
;
1534 child_process_request(child
, &state
);
1536 DEBUG(4, ("Finished processing child request %d\n",
1537 (int)state
.request
->cmd
));
1539 SAFE_FREE(state
.request
->extra_data
.data
);
1541 iov
[0].iov_base
= (void *)state
.response
;
1542 iov
[0].iov_len
= sizeof(struct winbindd_response
);
1545 if (state
.response
->length
> sizeof(struct winbindd_response
)) {
1547 (void *)state
.response
->extra_data
.data
;
1548 iov
[1].iov_len
= state
.response
->length
-iov
[0].iov_len
;
1552 DEBUG(10, ("Writing %d bytes to parent\n",
1553 (int)state
.response
->length
));
1555 if (write_data_iov(state
.sock
, iov
, iov_count
) !=
1556 state
.response
->length
) {
1557 DEBUG(0, ("Could not write result\n"));