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"
33 #include "librpc/gen_ndr/messaging.h"
37 #define DBGC_CLASS DBGC_WINBIND
39 extern bool override_logfile
;
40 extern struct winbindd_methods cache_methods
;
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
->pid
== 0) && (!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
);
169 tevent_req_error(req
, err
);
172 tevent_req_done(req
);
175 int wb_child_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
176 struct winbindd_response
**presponse
, int *err
)
178 struct wb_child_request_state
*state
= tevent_req_data(
179 req
, struct wb_child_request_state
);
181 if (tevent_req_is_unix_error(req
, err
)) {
184 *presponse
= talloc_move(mem_ctx
, &state
->response
);
188 struct wb_domain_request_state
{
189 struct tevent_context
*ev
;
190 struct winbindd_domain
*domain
;
191 struct winbindd_request
*request
;
192 struct winbindd_request
*init_req
;
193 struct winbindd_response
*response
;
196 static void wb_domain_request_gotdc(struct tevent_req
*subreq
);
197 static void wb_domain_request_initialized(struct tevent_req
*subreq
);
198 static void wb_domain_request_done(struct tevent_req
*subreq
);
200 struct tevent_req
*wb_domain_request_send(TALLOC_CTX
*mem_ctx
,
201 struct tevent_context
*ev
,
202 struct winbindd_domain
*domain
,
203 struct winbindd_request
*request
)
205 struct tevent_req
*req
, *subreq
;
206 struct wb_domain_request_state
*state
;
208 req
= tevent_req_create(mem_ctx
, &state
,
209 struct wb_domain_request_state
);
214 if (domain
->initialized
) {
215 subreq
= wb_child_request_send(state
, ev
, &domain
->child
,
217 if (tevent_req_nomem(subreq
, req
)) {
218 return tevent_req_post(req
, ev
);
220 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
224 state
->domain
= domain
;
226 state
->request
= request
;
228 state
->init_req
= talloc_zero(state
, struct winbindd_request
);
229 if (tevent_req_nomem(state
->init_req
, req
)) {
230 return tevent_req_post(req
, ev
);
233 if (IS_DC
|| domain
->primary
|| domain
->internal
) {
234 /* The primary domain has to find the DC name itself */
235 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
236 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
237 state
->init_req
->data
.init_conn
.is_primary
=
238 domain
->primary
? true : false;
239 fstrcpy(state
->init_req
->data
.init_conn
.dcname
, "");
241 subreq
= wb_child_request_send(state
, ev
, &domain
->child
,
243 if (tevent_req_nomem(subreq
, req
)) {
244 return tevent_req_post(req
, ev
);
246 tevent_req_set_callback(subreq
, wb_domain_request_initialized
,
252 * Ask our DC for a DC name
254 domain
= find_our_domain();
256 /* This is *not* the primary domain, let's ask our DC about a DC
259 state
->init_req
->cmd
= WINBINDD_GETDCNAME
;
260 fstrcpy(state
->init_req
->domain_name
, domain
->name
);
262 subreq
= wb_child_request_send(state
, ev
, &domain
->child
, request
);
263 if (tevent_req_nomem(subreq
, req
)) {
264 return tevent_req_post(req
, ev
);
266 tevent_req_set_callback(subreq
, wb_domain_request_gotdc
, req
);
270 static void wb_domain_request_gotdc(struct tevent_req
*subreq
)
272 struct tevent_req
*req
= tevent_req_callback_data(
273 subreq
, struct tevent_req
);
274 struct wb_domain_request_state
*state
= tevent_req_data(
275 req
, struct wb_domain_request_state
);
276 struct winbindd_response
*response
;
279 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
282 tevent_req_error(req
, err
);
285 state
->init_req
->cmd
= WINBINDD_INIT_CONNECTION
;
286 fstrcpy(state
->init_req
->domain_name
, state
->domain
->name
);
287 state
->init_req
->data
.init_conn
.is_primary
= False
;
288 fstrcpy(state
->init_req
->data
.init_conn
.dcname
,
289 response
->data
.dc_name
);
291 TALLOC_FREE(response
);
293 subreq
= wb_child_request_send(state
, state
->ev
, &state
->domain
->child
,
295 if (tevent_req_nomem(subreq
, req
)) {
298 tevent_req_set_callback(subreq
, wb_domain_request_initialized
, req
);
301 static void wb_domain_request_initialized(struct tevent_req
*subreq
)
303 struct tevent_req
*req
= tevent_req_callback_data(
304 subreq
, struct tevent_req
);
305 struct wb_domain_request_state
*state
= tevent_req_data(
306 req
, struct wb_domain_request_state
);
307 struct winbindd_response
*response
;
310 ret
= wb_child_request_recv(subreq
, talloc_tos(), &response
, &err
);
313 tevent_req_error(req
, err
);
317 if (!string_to_sid(&state
->domain
->sid
,
318 response
->data
.domain_info
.sid
)) {
319 DEBUG(1,("init_child_recv: Could not convert sid %s "
320 "from string\n", response
->data
.domain_info
.sid
));
321 tevent_req_error(req
, EINVAL
);
324 fstrcpy(state
->domain
->name
, response
->data
.domain_info
.name
);
325 fstrcpy(state
->domain
->alt_name
, response
->data
.domain_info
.alt_name
);
326 state
->domain
->native_mode
= response
->data
.domain_info
.native_mode
;
327 state
->domain
->active_directory
=
328 response
->data
.domain_info
.active_directory
;
329 state
->domain
->initialized
= true;
331 TALLOC_FREE(response
);
333 subreq
= wb_child_request_send(state
, state
->ev
, &state
->domain
->child
,
335 if (tevent_req_nomem(subreq
, req
)) {
338 tevent_req_set_callback(subreq
, wb_domain_request_done
, req
);
341 static void wb_domain_request_done(struct tevent_req
*subreq
)
343 struct tevent_req
*req
= tevent_req_callback_data(
344 subreq
, struct tevent_req
);
345 struct wb_domain_request_state
*state
= tevent_req_data(
346 req
, struct wb_domain_request_state
);
349 ret
= wb_child_request_recv(subreq
, talloc_tos(), &state
->response
,
353 tevent_req_error(req
, err
);
356 tevent_req_done(req
);
359 int wb_domain_request_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
360 struct winbindd_response
**presponse
, int *err
)
362 struct wb_domain_request_state
*state
= tevent_req_data(
363 req
, struct wb_domain_request_state
);
365 if (tevent_req_is_unix_error(req
, err
)) {
368 *presponse
= talloc_move(mem_ctx
, &state
->response
);
372 static void child_process_request(struct winbindd_child
*child
,
373 struct winbindd_cli_state
*state
)
375 struct winbindd_domain
*domain
= child
->domain
;
376 const struct winbindd_child_dispatch_table
*table
= child
->table
;
378 /* Free response data - we may be interrupted and receive another
379 command before being able to send this data off. */
381 state
->response
->result
= WINBINDD_ERROR
;
382 state
->response
->length
= sizeof(struct winbindd_response
);
384 /* as all requests in the child are sync, we can use talloc_tos() */
385 state
->mem_ctx
= talloc_tos();
387 /* Process command */
389 for (; table
->name
; table
++) {
390 if (state
->request
->cmd
== table
->struct_cmd
) {
391 DEBUG(10,("child_process_request: request fn %s\n",
393 state
->response
->result
= table
->struct_fn(domain
, state
);
398 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
399 (int)state
->request
->cmd
));
400 state
->response
->result
= WINBINDD_ERROR
;
403 void setup_child(struct winbindd_domain
*domain
, struct winbindd_child
*child
,
404 const struct winbindd_child_dispatch_table
*table
,
405 const char *logprefix
,
408 if (logprefix
&& logname
) {
409 char *logbase
= NULL
;
414 if (asprintf(&logbase
, "%s", lp_logfile()) < 0) {
415 smb_panic("Internal error: asprintf failed");
418 if ((end
= strrchr_m(logbase
, '/'))) {
422 if (asprintf(&logbase
, "%s", get_dyn_LOGFILEBASE()) < 0) {
423 smb_panic("Internal error: asprintf failed");
427 if (asprintf(&child
->logfilename
, "%s/%s-%s",
428 logbase
, logprefix
, logname
) < 0) {
430 smb_panic("Internal error: asprintf failed");
435 smb_panic("Internal error: logprefix == NULL && "
439 child
->domain
= domain
;
440 child
->table
= table
;
441 child
->queue
= tevent_queue_create(NULL
, "winbind_child");
442 SMB_ASSERT(child
->queue
!= NULL
);
443 child
->rpccli
= wbint_rpccli_create(NULL
, domain
, child
);
444 SMB_ASSERT(child
->rpccli
!= NULL
);
447 static struct winbindd_child
*winbindd_children
= NULL
;
449 void winbind_child_died(pid_t pid
)
451 struct winbindd_child
*child
;
453 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
454 if (child
->pid
== pid
) {
460 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
464 /* This will be re-added in fork_domain_child() */
466 DLIST_REMOVE(winbindd_children
, child
);
473 /* Ensure any negative cache entries with the netbios or realm names are removed. */
475 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
477 flush_negative_conn_cache_for_domain(domain
->name
);
478 if (*domain
->alt_name
) {
479 flush_negative_conn_cache_for_domain(domain
->alt_name
);
484 * Parent winbindd process sets its own debug level first and then
485 * sends a message to all the winbindd children to adjust their debug
486 * level to that of parents.
489 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
492 struct server_id server_id
,
495 struct winbindd_child
*child
;
497 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
499 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
501 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
503 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
504 (unsigned int)child
->pid
));
506 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
509 strlen((char *) data
->data
) + 1);
513 /* Set our domains as offline and forward the offline message to our children. */
515 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
518 struct server_id server_id
,
521 struct winbindd_child
*child
;
522 struct winbindd_domain
*domain
;
524 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
526 if (!lp_winbind_offline_logon()) {
527 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
531 /* Set our global state as offline. */
532 if (!set_global_winbindd_state_offline()) {
533 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
537 /* Set all our domains as offline. */
538 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
539 if (domain
->internal
) {
542 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
543 set_domain_offline(domain
);
546 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
547 /* Don't send message to internal children. We've already
549 if (!child
->domain
|| winbindd_internal_child(child
)) {
553 /* Or internal domains (this should not be possible....) */
554 if (child
->domain
->internal
) {
558 /* Each winbindd child should only process requests for one domain - make sure
559 we only set it online / offline for that domain. */
561 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
562 (unsigned int)child
->pid
, domain
->name
));
564 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
566 (uint8
*)child
->domain
->name
,
567 strlen(child
->domain
->name
)+1);
571 /* Set our domains as online and forward the online message to our children. */
573 void winbind_msg_online(struct messaging_context
*msg_ctx
,
576 struct server_id server_id
,
579 struct winbindd_child
*child
;
580 struct winbindd_domain
*domain
;
582 DEBUG(10,("winbind_msg_online: got online message.\n"));
584 if (!lp_winbind_offline_logon()) {
585 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
589 /* Set our global state as online. */
590 set_global_winbindd_state_online();
592 smb_nscd_flush_user_cache();
593 smb_nscd_flush_group_cache();
595 /* Set all our domains as online. */
596 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
597 if (domain
->internal
) {
600 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
602 winbindd_flush_negative_conn_cache(domain
);
603 set_domain_online_request(domain
);
605 /* Send an online message to the idmap child when our
606 primary domain comes back online */
608 if ( domain
->primary
) {
609 struct winbindd_child
*idmap
= idmap_child();
611 if ( idmap
->pid
!= 0 ) {
612 messaging_send_buf(msg_ctx
,
613 pid_to_procid(idmap
->pid
),
615 (uint8
*)domain
->name
,
616 strlen(domain
->name
)+1);
621 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
622 /* Don't send message to internal childs. */
623 if (!child
->domain
|| winbindd_internal_child(child
)) {
627 /* Or internal domains (this should not be possible....) */
628 if (child
->domain
->internal
) {
632 /* Each winbindd child should only process requests for one domain - make sure
633 we only set it online / offline for that domain. */
635 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
636 (unsigned int)child
->pid
, child
->domain
->name
));
638 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
640 (uint8
*)child
->domain
->name
,
641 strlen(child
->domain
->name
)+1);
645 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
647 struct winbindd_domain
*domain
;
650 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
651 get_global_winbindd_state_offline() ?
652 "Offline":"Online")) == NULL
) {
656 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
657 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
660 "Online":"Offline")) == NULL
) {
665 buf
= talloc_asprintf_append_buffer(buf
, "\n");
667 DEBUG(5,("collect_onlinestatus: %s", buf
));
672 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
675 struct server_id server_id
,
680 struct server_id
*sender
;
682 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
688 sender
= (struct server_id
*)data
->data
;
690 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
691 if (mem_ctx
== NULL
) {
695 message
= collect_onlinestatus(mem_ctx
);
696 if (message
== NULL
) {
697 talloc_destroy(mem_ctx
);
701 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
702 (uint8
*)message
, strlen(message
) + 1);
704 talloc_destroy(mem_ctx
);
707 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
710 struct server_id server_id
,
713 struct winbindd_child
*child
;
715 DEBUG(10,("winbind_msg_dump_event_list received\n"));
717 dump_event_list(winbind_event_context());
719 for (child
= winbindd_children
; child
!= NULL
; child
= child
->next
) {
721 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
722 (unsigned int)child
->pid
));
724 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
731 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
734 struct server_id server_id
,
738 const char *message
= NULL
;
739 struct server_id
*sender
= NULL
;
740 const char *domain
= NULL
;
743 struct winbindd_domain
*dom
= NULL
;
745 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
747 if (!data
|| !data
->data
) {
751 if (data
->length
< sizeof(struct server_id
)) {
755 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
760 sender
= (struct server_id
*)data
->data
;
761 if (data
->length
> sizeof(struct server_id
)) {
762 domain
= (const char *)data
->data
+sizeof(struct server_id
);
767 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
770 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
771 find_domain_from_name_noinit(domain
));
773 talloc_destroy(mem_ctx
);
777 messaging_send_buf(msg_ctx
, *sender
,
778 MSG_WINBIND_DUMP_DOMAIN_LIST
,
779 (uint8_t *)message
, strlen(message
) + 1);
781 talloc_destroy(mem_ctx
);
786 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
788 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
789 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
791 talloc_destroy(mem_ctx
);
795 s
= talloc_asprintf_append(s
, "%s\n", message
);
797 talloc_destroy(mem_ctx
);
802 status
= messaging_send_buf(msg_ctx
, *sender
,
803 MSG_WINBIND_DUMP_DOMAIN_LIST
,
804 (uint8_t *)s
, strlen(s
) + 1);
805 if (!NT_STATUS_IS_OK(status
)) {
806 DEBUG(0,("failed to send message: %s\n",
810 talloc_destroy(mem_ctx
);
813 static void account_lockout_policy_handler(struct event_context
*ctx
,
814 struct timed_event
*te
,
818 struct winbindd_child
*child
=
819 (struct winbindd_child
*)private_data
;
820 TALLOC_CTX
*mem_ctx
= NULL
;
821 struct winbindd_methods
*methods
;
822 struct samr_DomInfo12 lockout_policy
;
825 DEBUG(10,("account_lockout_policy_handler called\n"));
827 TALLOC_FREE(child
->lockout_policy_event
);
829 if ( !winbindd_can_contact_domain( child
->domain
) ) {
830 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
831 "do not have an incoming trust to domain %s\n",
832 child
->domain
->name
));
837 methods
= child
->domain
->methods
;
839 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
841 result
= NT_STATUS_NO_MEMORY
;
843 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
845 TALLOC_FREE(mem_ctx
);
847 if (!NT_STATUS_IS_OK(result
)) {
848 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
852 child
->lockout_policy_event
= event_add_timed(winbind_event_context(), NULL
,
853 timeval_current_ofs(3600, 0),
854 account_lockout_policy_handler
,
858 static time_t get_machine_password_timeout(void)
860 /* until we have gpo support use lp setting */
861 return lp_machine_password_timeout();
864 static bool calculate_next_machine_pwd_change(const char *domain
,
867 time_t pass_last_set_time
;
873 pw
= secrets_fetch_machine_password(domain
,
878 DEBUG(0,("cannot fetch own machine password ????"));
884 timeout
= get_machine_password_timeout();
886 DEBUG(10,("machine password never expires\n"));
890 tv
.tv_sec
= pass_last_set_time
;
891 DEBUG(10, ("password last changed %s\n",
892 timeval_string(talloc_tos(), &tv
, false)));
893 tv
.tv_sec
+= timeout
;
894 DEBUGADD(10, ("password valid until %s\n",
895 timeval_string(talloc_tos(), &tv
, false)));
897 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
898 next_change
= pass_last_set_time
+ timeout
;
899 DEBUG(10,("machine password still valid until: %s\n",
900 http_timestring(talloc_tos(), next_change
)));
901 *t
= timeval_set(next_change
, 0);
903 if (lp_clustering()) {
906 * When having a cluster, we have several
907 * winbinds racing for the password change. In
908 * the machine_password_change_handler()
909 * function we check if someone else was
910 * faster when the event triggers. We add a
911 * 255-second random delay here, so that we
912 * don't run to change the password at the
915 generate_random_buffer(&randbuf
, sizeof(randbuf
));
916 DEBUG(10, ("adding %d seconds randomness\n",
918 t
->tv_sec
+= randbuf
;
923 DEBUG(10,("machine password expired, needs immediate change\n"));
930 static void machine_password_change_handler(struct event_context
*ctx
,
931 struct timed_event
*te
,
935 struct winbindd_child
*child
=
936 (struct winbindd_child
*)private_data
;
937 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
940 struct timeval next_change
;
942 DEBUG(10,("machine_password_change_handler called\n"));
944 TALLOC_FREE(child
->machine_password_change_event
);
946 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
948 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
952 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
953 timeval_string(talloc_tos(), &next_change
, false)));
955 if (!timeval_expired(&next_change
)) {
956 DEBUG(10, ("Someone else has already changed the pw\n"));
960 if (!winbindd_can_contact_domain(child
->domain
)) {
961 DEBUG(10,("machine_password_change_handler: Removing myself since I "
962 "do not have an incoming trust to domain %s\n",
963 child
->domain
->name
));
967 result
= cm_connect_netlogon(child
->domain
, &netlogon_pipe
);
968 if (!NT_STATUS_IS_OK(result
)) {
969 DEBUG(10,("machine_password_change_handler: "
970 "failed to connect netlogon pipe: %s\n",
975 frame
= talloc_stackframe();
977 result
= trust_pw_find_change_and_store_it(netlogon_pipe
,
979 child
->domain
->name
);
982 DEBUG(10, ("machine_password_change_handler: "
983 "trust_pw_find_change_and_store_it returned %s\n",
986 if (NT_STATUS_EQUAL(result
, NT_STATUS_ACCESS_DENIED
) ) {
987 DEBUG(3,("machine_password_change_handler: password set returned "
988 "ACCESS_DENIED. Maybe the trust account "
989 "password was changed and we didn't know it. "
990 "Killing connections to domain %s\n",
991 child
->domain
->name
));
992 TALLOC_FREE(child
->domain
->conn
.netlogon_pipe
);
995 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
997 DEBUG(10, ("calculate_next_machine_pwd_change failed\n"));
1001 DEBUG(10, ("calculate_next_machine_pwd_change returned %s\n",
1002 timeval_string(talloc_tos(), &next_change
, false)));
1004 if (!NT_STATUS_IS_OK(result
)) {
1007 * In case of failure, give the DC a minute to recover
1009 tmp
= timeval_current_ofs(60, 0);
1010 next_change
= timeval_max(&next_change
, &tmp
);
1014 child
->machine_password_change_event
= event_add_timed(winbind_event_context(), NULL
,
1016 machine_password_change_handler
,
1020 /* Deal with a request to go offline. */
1022 static void child_msg_offline(struct messaging_context
*msg
,
1025 struct server_id server_id
,
1028 struct winbindd_domain
*domain
;
1029 struct winbindd_domain
*primary_domain
= NULL
;
1030 const char *domainname
= (const char *)data
->data
;
1032 if (data
->data
== NULL
|| data
->length
== 0) {
1036 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
1038 if (!lp_winbind_offline_logon()) {
1039 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
1043 primary_domain
= find_our_domain();
1045 /* Mark the requested domain offline. */
1047 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1048 if (domain
->internal
) {
1051 if (strequal(domain
->name
, domainname
)) {
1052 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1053 set_domain_offline(domain
);
1054 /* we are in the trusted domain, set the primary domain
1056 if (domain
!= primary_domain
) {
1057 set_domain_offline(primary_domain
);
1063 /* Deal with a request to go online. */
1065 static void child_msg_online(struct messaging_context
*msg
,
1068 struct server_id server_id
,
1071 struct winbindd_domain
*domain
;
1072 struct winbindd_domain
*primary_domain
= NULL
;
1073 const char *domainname
= (const char *)data
->data
;
1075 if (data
->data
== NULL
|| data
->length
== 0) {
1079 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1081 if (!lp_winbind_offline_logon()) {
1082 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1086 primary_domain
= find_our_domain();
1088 /* Set our global state as online. */
1089 set_global_winbindd_state_online();
1091 /* Try and mark everything online - delete any negative cache entries
1092 to force a reconnect now. */
1094 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1095 if (domain
->internal
) {
1098 if (strequal(domain
->name
, domainname
)) {
1099 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1100 winbindd_flush_negative_conn_cache(domain
);
1101 set_domain_online_request(domain
);
1103 /* we can be in trusted domain, which will contact primary domain
1104 * we have to bring primary domain online in trusted domain process
1105 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1106 * --> contact_domain = find_our_domain()
1108 if (domain
!= primary_domain
) {
1109 winbindd_flush_negative_conn_cache(primary_domain
);
1110 set_domain_online_request(primary_domain
);
1116 static void child_msg_dump_event_list(struct messaging_context
*msg
,
1119 struct server_id server_id
,
1122 DEBUG(5,("child_msg_dump_event_list received\n"));
1124 dump_event_list(winbind_event_context());
1127 bool winbindd_reinit_after_fork(const char *logfilename
)
1129 struct winbindd_domain
*domain
;
1130 struct winbindd_child
*cl
;
1133 status
= reinit_after_fork(
1134 winbind_messaging_context(),
1135 winbind_event_context(),
1136 messaging_server_id(winbind_messaging_context()),
1138 if (!NT_STATUS_IS_OK(status
)) {
1139 DEBUG(0,("reinit_after_fork() failed\n"));
1143 close_conns_after_fork();
1145 if (!override_logfile
&& logfilename
) {
1146 lp_set_logfile(logfilename
);
1150 if (!winbindd_setup_sig_term_handler(false))
1152 if (!winbindd_setup_sig_hup_handler(override_logfile
? NULL
:
1156 /* Stop zombies in children */
1159 /* Don't handle the same messages as our parent. */
1160 messaging_deregister(winbind_messaging_context(),
1161 MSG_SMB_CONF_UPDATED
, NULL
);
1162 messaging_deregister(winbind_messaging_context(),
1163 MSG_SHUTDOWN
, NULL
);
1164 messaging_deregister(winbind_messaging_context(),
1165 MSG_WINBIND_OFFLINE
, NULL
);
1166 messaging_deregister(winbind_messaging_context(),
1167 MSG_WINBIND_ONLINE
, NULL
);
1168 messaging_deregister(winbind_messaging_context(),
1169 MSG_WINBIND_ONLINESTATUS
, NULL
);
1170 messaging_deregister(winbind_messaging_context(),
1171 MSG_DUMP_EVENT_LIST
, NULL
);
1172 messaging_deregister(winbind_messaging_context(),
1173 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1174 messaging_deregister(winbind_messaging_context(),
1177 /* We have destroyed all events in the winbindd_event_context
1178 * in reinit_after_fork(), so clean out all possible pending
1179 * event pointers. */
1181 /* Deal with check_online_events. */
1183 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1184 TALLOC_FREE(domain
->check_online_event
);
1187 /* Ensure we're not handling a credential cache event inherited
1188 * from our parent. */
1190 ccache_remove_all_after_fork();
1192 /* Destroy all possible events in child list. */
1193 for (cl
= winbindd_children
; cl
!= NULL
; cl
= cl
->next
) {
1194 TALLOC_FREE(cl
->lockout_policy_event
);
1195 TALLOC_FREE(cl
->machine_password_change_event
);
1197 /* Children should never be able to send
1198 * each other messages, all messages must
1199 * go through the parent.
1204 * This is a little tricky, children must not
1205 * send an MSG_WINBIND_ONLINE message to idmap_child().
1206 * If we are in a child of our primary domain or
1207 * in the process created by fork_child_dc_connect(),
1208 * and the primary domain cannot go online,
1209 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1210 * periodically to idmap_child().
1212 * The sequence is, fork_child_dc_connect() ---> getdcs() --->
1213 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1214 * ---> init_dc_connection() ---> cm_open_connection --->
1215 * set_domain_online(), sends MSG_WINBIND_ONLINE to
1216 * idmap_child(). Disallow children sending messages
1217 * to each other, all messages must go through the parent.
1226 * In a child there will be only one domain, reference that here.
1228 static struct winbindd_domain
*child_domain
;
1230 struct winbindd_domain
*wb_child_domain(void)
1232 return child_domain
;
1235 static bool fork_domain_child(struct winbindd_child
*child
)
1238 struct winbindd_cli_state state
;
1239 struct winbindd_request request
;
1240 struct winbindd_response response
;
1241 struct winbindd_domain
*primary_domain
= NULL
;
1243 if (child
->domain
) {
1244 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1245 child
->domain
->name
));
1247 DEBUG(10, ("fork_domain_child called without domain.\n"));
1250 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1251 DEBUG(0, ("Could not open child pipe: %s\n",
1257 state
.pid
= sys_getpid();
1258 state
.request
= &request
;
1259 state
.response
= &response
;
1261 child
->pid
= sys_fork();
1263 if (child
->pid
== -1) {
1264 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1268 if (child
->pid
!= 0) {
1271 child
->next
= child
->prev
= NULL
;
1272 DLIST_ADD(winbindd_children
, child
);
1273 child
->sock
= fdpair
[1];
1278 child_domain
= child
->domain
;
1280 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1282 state
.sock
= fdpair
[0];
1285 if (!winbindd_reinit_after_fork(child
->logfilename
)) {
1289 /* Handle online/offline messages. */
1290 messaging_register(winbind_messaging_context(), NULL
,
1291 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1292 messaging_register(winbind_messaging_context(), NULL
,
1293 MSG_WINBIND_ONLINE
, child_msg_online
);
1294 messaging_register(winbind_messaging_context(), NULL
,
1295 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1296 messaging_register(winbind_messaging_context(), NULL
,
1297 MSG_DEBUG
, debug_message
);
1299 primary_domain
= find_our_domain();
1301 if (primary_domain
== NULL
) {
1302 smb_panic("no primary domain found");
1305 /* It doesn't matter if we allow cache login,
1306 * try to bring domain online after fork. */
1307 if ( child
->domain
) {
1308 child
->domain
->startup
= True
;
1309 child
->domain
->startup_time
= time(NULL
);
1310 /* we can be in primary domain or in trusted domain
1311 * If we are in trusted domain, set the primary domain
1312 * in start-up mode */
1313 if (!(child
->domain
->internal
)) {
1314 set_domain_online_request(child
->domain
);
1315 if (!(child
->domain
->primary
)) {
1316 primary_domain
->startup
= True
;
1317 primary_domain
->startup_time
= time(NULL
);
1318 set_domain_online_request(primary_domain
);
1324 * We are in idmap child, make sure that we set the
1325 * check_online_event to bring primary domain online.
1327 if (child
== idmap_child()) {
1328 set_domain_online_request(primary_domain
);
1331 /* We might be in the idmap child...*/
1332 if (child
->domain
&& !(child
->domain
->internal
) &&
1333 lp_winbind_offline_logon()) {
1335 set_domain_online_request(child
->domain
);
1337 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1338 /* We need to talk to the primary
1339 * domain as well as the trusted
1340 * domain inside a trusted domain
1343 * set_dc_type_and_flags_trustinfo()
1346 set_domain_online_request(primary_domain
);
1349 child
->lockout_policy_event
= event_add_timed(
1350 winbind_event_context(), NULL
, timeval_zero(),
1351 account_lockout_policy_handler
,
1355 if (child
->domain
&& child
->domain
->primary
&&
1356 !USE_KERBEROS_KEYTAB
&&
1357 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1359 struct timeval next_change
;
1361 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1363 child
->machine_password_change_event
= event_add_timed(
1364 winbind_event_context(), NULL
, next_change
,
1365 machine_password_change_handler
,
1379 TALLOC_CTX
*frame
= talloc_stackframe();
1380 struct iovec iov
[2];
1384 if (run_events(winbind_event_context(), 0, NULL
, NULL
)) {
1391 if (child
->domain
&& child
->domain
->startup
&&
1392 (now
.tv_sec
> child
->domain
->startup_time
+ 30)) {
1393 /* No longer in "startup" mode. */
1394 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1395 child
->domain
->name
));
1396 child
->domain
->startup
= False
;
1401 FD_SET(state
.sock
, &r_fds
);
1405 * Initialize this high as event_add_to_select_args()
1406 * uses a timeval_min() on this and next_event. Fix
1407 * from Roel van Meer <rolek@alt001.com>.
1412 event_add_to_select_args(winbind_event_context(), &now
,
1413 &r_fds
, &w_fds
, &t
, &maxfd
);
1414 tp
= get_timed_events_timeout(winbind_event_context(), &t
);
1416 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1417 (unsigned int)tp
->tv_sec
, (unsigned int)tp
->tv_usec
));
1420 ret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, tp
);
1422 if (run_events(winbind_event_context(), ret
, &r_fds
, &w_fds
)) {
1423 /* We got a signal - continue. */
1429 DEBUG(11,("nothing is ready yet, continue\n"));
1434 if (ret
== -1 && errno
== EINTR
) {
1435 /* We got a signal - continue. */
1440 if (ret
== -1 && errno
!= EINTR
) {
1441 DEBUG(0,("select error occured\n"));
1447 /* fetch a request from the main daemon */
1448 status
= child_read_request(&state
);
1450 if (!NT_STATUS_IS_OK(status
)) {
1451 /* we lost contact with our parent */
1455 DEBUG(4,("child daemon request %d\n", (int)state
.request
->cmd
));
1457 ZERO_STRUCTP(state
.response
);
1458 state
.request
->null_term
= '\0';
1459 state
.mem_ctx
= frame
;
1460 child_process_request(child
, &state
);
1462 DEBUG(4, ("Finished processing child request %d\n",
1463 (int)state
.request
->cmd
));
1465 SAFE_FREE(state
.request
->extra_data
.data
);
1467 iov
[0].iov_base
= (void *)state
.response
;
1468 iov
[0].iov_len
= sizeof(struct winbindd_response
);
1471 if (state
.response
->length
> sizeof(struct winbindd_response
)) {
1473 (void *)state
.response
->extra_data
.data
;
1474 iov
[1].iov_len
= state
.response
->length
-iov
[0].iov_len
;
1478 DEBUG(10, ("Writing %d bytes to parent\n",
1479 (int)state
.response
->length
));
1481 if (write_data_iov(state
.sock
, iov
, iov_count
) !=
1482 state
.response
->length
) {
1483 DEBUG(0, ("Could not write result\n"));