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.
34 #define DBGC_CLASS DBGC_WINBIND
36 extern bool override_logfile
;
37 extern struct winbindd_methods cache_methods
;
39 /* Read some data from a client connection */
41 static void child_read_request(struct winbindd_cli_state
*state
)
47 status
= read_data(state
->sock
, (char *)&state
->request
,
48 sizeof(state
->request
));
50 if (!NT_STATUS_IS_OK(status
)) {
51 DEBUG(3, ("child_read_request: read_data failed: %s\n",
53 state
->finished
= True
;
57 if (state
->request
.extra_len
== 0) {
58 state
->request
.extra_data
.data
= NULL
;
62 DEBUG(10, ("Need to read %d extra bytes\n", (int)state
->request
.extra_len
));
64 state
->request
.extra_data
.data
=
65 SMB_MALLOC_ARRAY(char, state
->request
.extra_len
+ 1);
67 if (state
->request
.extra_data
.data
== NULL
) {
68 DEBUG(0, ("malloc failed\n"));
69 state
->finished
= True
;
73 /* Ensure null termination */
74 state
->request
.extra_data
.data
[state
->request
.extra_len
] = '\0';
76 status
= read_data(state
->sock
, state
->request
.extra_data
.data
,
77 state
->request
.extra_len
);
79 if (!NT_STATUS_IS_OK(status
)) {
80 DEBUG(0, ("Could not read extra data: %s\n",
82 state
->finished
= True
;
88 * Machinery for async requests sent to children. You set up a
89 * winbindd_request, select a child to query, and issue a async_request
90 * call. When the request is completed, the callback function you specified is
91 * called back with the private pointer you gave to async_request.
94 struct winbindd_async_request
{
95 struct winbindd_async_request
*next
, *prev
;
97 struct winbindd_child
*child
;
98 struct winbindd_request
*request
;
99 struct winbindd_response
*response
;
100 void (*continuation
)(void *private_data
, bool success
);
101 struct timed_event
*reply_timeout_event
;
102 pid_t child_pid
; /* pid of the child we're waiting on. Used to detect
103 a restart of the child (child->pid != child_pid). */
107 static void async_request_fail(struct winbindd_async_request
*state
);
108 static void async_main_request_sent(void *private_data
, bool success
);
109 static void async_request_sent(void *private_data
, bool success
);
110 static void async_reply_recv(void *private_data
, bool success
);
111 static void schedule_async_request(struct winbindd_child
*child
);
113 void async_request(TALLOC_CTX
*mem_ctx
, struct winbindd_child
*child
,
114 struct winbindd_request
*request
,
115 struct winbindd_response
*response
,
116 void (*continuation
)(void *private_data
, bool success
),
119 struct winbindd_async_request
*state
;
121 SMB_ASSERT(continuation
!= NULL
);
123 DEBUG(10, ("Sending request to child pid %d (domain=%s)\n",
125 (child
->domain
!= NULL
) ? child
->domain
->name
: "''"));
127 state
= TALLOC_P(mem_ctx
, struct winbindd_async_request
);
130 DEBUG(0, ("talloc failed\n"));
131 continuation(private_data
, False
);
135 state
->mem_ctx
= mem_ctx
;
136 state
->child
= child
;
137 state
->reply_timeout_event
= NULL
;
138 state
->request
= request
;
139 state
->response
= response
;
140 state
->continuation
= continuation
;
141 state
->private_data
= private_data
;
143 DLIST_ADD_END(child
->requests
, state
, struct winbindd_async_request
*);
145 schedule_async_request(child
);
150 static void async_main_request_sent(void *private_data
, bool success
)
152 struct winbindd_async_request
*state
=
153 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
156 DEBUG(5, ("Could not send async request\n"));
157 async_request_fail(state
);
161 if (state
->request
->extra_len
== 0) {
162 async_request_sent(private_data
, True
);
166 setup_async_write(&state
->child
->event
, state
->request
->extra_data
.data
,
167 state
->request
->extra_len
,
168 async_request_sent
, state
);
171 /****************************************************************
172 Handler triggered if the child winbindd doesn't respond within
174 ****************************************************************/
176 static void async_request_timeout_handler(struct event_context
*ctx
,
177 struct timed_event
*te
,
181 struct winbindd_async_request
*state
=
182 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
184 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
185 "Closing connection to it.\n",
188 /* Deal with the reply - set to error. */
189 async_reply_recv(private_data
, False
);
192 /**************************************************************
193 Common function called on both async send and recv fail.
194 Cleans up the child and schedules the next request.
195 **************************************************************/
197 static void async_request_fail(struct winbindd_async_request
*state
)
199 DLIST_REMOVE(state
->child
->requests
, state
);
201 TALLOC_FREE(state
->reply_timeout_event
);
203 /* If child exists and is not already reaped,
204 send kill signal to child. */
206 if ((state
->child
->pid
!= (pid_t
)0) &&
207 (state
->child
->pid
!= (pid_t
)-1) &&
208 (state
->child
->pid
== state
->child_pid
)) {
209 kill(state
->child_pid
, SIGTERM
);
212 * Close the socket to the child.
214 winbind_child_died(state
->child_pid
);
217 state
->response
->length
= sizeof(struct winbindd_response
);
218 state
->response
->result
= WINBINDD_ERROR
;
219 state
->continuation(state
->private_data
, False
);
222 static void async_request_sent(void *private_data_data
, bool success
)
224 struct winbindd_async_request
*state
=
225 talloc_get_type_abort(private_data_data
, struct winbindd_async_request
);
228 DEBUG(5, ("Could not send async request to child pid %u\n",
229 (unsigned int)state
->child_pid
));
230 async_request_fail(state
);
234 /* Request successfully sent to the child, setup the wait for reply */
236 setup_async_read(&state
->child
->event
,
237 &state
->response
->result
,
238 sizeof(state
->response
->result
),
239 async_reply_recv
, state
);
242 * Set up a timeout of 300 seconds for the response.
243 * If we don't get it close the child socket and
247 state
->reply_timeout_event
= event_add_timed(winbind_event_context(),
249 timeval_current_ofs(300,0),
250 async_request_timeout_handler
,
252 if (!state
->reply_timeout_event
) {
253 smb_panic("async_request_sent: failed to add timeout handler.\n");
257 static void async_reply_recv(void *private_data
, bool success
)
259 struct winbindd_async_request
*state
=
260 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
261 struct winbindd_child
*child
= state
->child
;
263 TALLOC_FREE(state
->reply_timeout_event
);
265 state
->response
->length
= sizeof(struct winbindd_response
);
268 DEBUG(5, ("Could not receive async reply from child pid %u\n",
269 (unsigned int)state
->child_pid
));
271 cache_cleanup_response(state
->child_pid
);
272 async_request_fail(state
);
276 SMB_ASSERT(cache_retrieve_response(state
->child_pid
,
279 cache_cleanup_response(state
->child_pid
);
281 DLIST_REMOVE(child
->requests
, state
);
283 schedule_async_request(child
);
285 state
->continuation(state
->private_data
, True
);
288 static bool fork_domain_child(struct winbindd_child
*child
);
290 static void schedule_async_request(struct winbindd_child
*child
)
292 struct winbindd_async_request
*request
= child
->requests
;
294 if (request
== NULL
) {
298 if (child
->event
.flags
!= 0) {
303 * This may be a reschedule, so we might
304 * have an existing timeout event pending on
305 * the first entry in the child->requests list
306 * (we only send one request at a time).
307 * Ensure we free it before we reschedule.
308 * Bug #5814, from hargagan <shargagan@novell.com>.
312 TALLOC_FREE(request
->reply_timeout_event
);
314 if ((child
->pid
== 0) && (!fork_domain_child(child
))) {
315 /* fork_domain_child failed.
316 Cancel all outstanding requests */
318 while (request
!= NULL
) {
319 /* request might be free'd in the continuation */
320 struct winbindd_async_request
*next
= request
->next
;
322 async_request_fail(request
);
328 /* Now we know who we're sending to - remember the pid. */
329 request
->child_pid
= child
->pid
;
331 setup_async_write(&child
->event
, request
->request
,
332 sizeof(*request
->request
),
333 async_main_request_sent
, request
);
338 struct domain_request_state
{
340 struct winbindd_domain
*domain
;
341 struct winbindd_request
*request
;
342 struct winbindd_response
*response
;
343 void (*continuation
)(void *private_data_data
, bool success
);
344 void *private_data_data
;
347 static void domain_init_recv(void *private_data_data
, bool success
);
349 void async_domain_request(TALLOC_CTX
*mem_ctx
,
350 struct winbindd_domain
*domain
,
351 struct winbindd_request
*request
,
352 struct winbindd_response
*response
,
353 void (*continuation
)(void *private_data_data
, bool success
),
354 void *private_data_data
)
356 struct domain_request_state
*state
;
358 if (domain
->initialized
) {
359 async_request(mem_ctx
, &domain
->child
, request
, response
,
360 continuation
, private_data_data
);
364 state
= TALLOC_P(mem_ctx
, struct domain_request_state
);
366 DEBUG(0, ("talloc failed\n"));
367 continuation(private_data_data
, False
);
371 state
->mem_ctx
= mem_ctx
;
372 state
->domain
= domain
;
373 state
->request
= request
;
374 state
->response
= response
;
375 state
->continuation
= continuation
;
376 state
->private_data_data
= private_data_data
;
378 init_child_connection(domain
, domain_init_recv
, state
);
381 static void domain_init_recv(void *private_data_data
, bool success
)
383 struct domain_request_state
*state
=
384 talloc_get_type_abort(private_data_data
, struct domain_request_state
);
387 DEBUG(5, ("Domain init returned an error\n"));
388 state
->continuation(state
->private_data_data
, False
);
392 async_request(state
->mem_ctx
, &state
->domain
->child
,
393 state
->request
, state
->response
,
394 state
->continuation
, state
->private_data_data
);
397 static void recvfrom_child(void *private_data_data
, bool success
)
399 struct winbindd_cli_state
*state
=
400 talloc_get_type_abort(private_data_data
, struct winbindd_cli_state
);
401 enum winbindd_result result
= state
->response
.result
;
403 /* This is an optimization: The child has written directly to the
404 * response buffer. The request itself is still in pending state,
405 * state that in the result code. */
407 state
->response
.result
= WINBINDD_PENDING
;
409 if ((!success
) || (result
!= WINBINDD_OK
)) {
410 request_error(state
);
417 void sendto_child(struct winbindd_cli_state
*state
,
418 struct winbindd_child
*child
)
420 async_request(state
->mem_ctx
, child
, &state
->request
,
421 &state
->response
, recvfrom_child
, state
);
424 void sendto_domain(struct winbindd_cli_state
*state
,
425 struct winbindd_domain
*domain
)
427 async_domain_request(state
->mem_ctx
, domain
,
428 &state
->request
, &state
->response
,
429 recvfrom_child
, state
);
432 static void child_process_request(struct winbindd_child
*child
,
433 struct winbindd_cli_state
*state
)
435 struct winbindd_domain
*domain
= child
->domain
;
436 const struct winbindd_child_dispatch_table
*table
= child
->table
;
438 /* Free response data - we may be interrupted and receive another
439 command before being able to send this data off. */
441 state
->response
.result
= WINBINDD_ERROR
;
442 state
->response
.length
= sizeof(struct winbindd_response
);
444 /* as all requests in the child are sync, we can use talloc_tos() */
445 state
->mem_ctx
= talloc_tos();
447 /* Process command */
449 for (; table
->name
; table
++) {
450 if (state
->request
.cmd
== table
->struct_cmd
) {
451 DEBUG(10,("child_process_request: request fn %s\n",
453 state
->response
.result
= table
->struct_fn(domain
, state
);
458 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
459 (int)state
->request
.cmd
));
460 state
->response
.result
= WINBINDD_ERROR
;
463 void setup_child(struct winbindd_child
*child
,
464 const struct winbindd_child_dispatch_table
*table
,
465 const char *logprefix
,
468 if (logprefix
&& logname
) {
469 if (asprintf(&child
->logfilename
, "%s/%s-%s",
470 get_dyn_LOGFILEBASE(), logprefix
, logname
) < 0) {
471 smb_panic("Internal error: asprintf failed");
474 smb_panic("Internal error: logprefix == NULL && "
478 child
->domain
= NULL
;
479 child
->table
= table
;
482 struct winbindd_child
*children
= NULL
;
484 void winbind_child_died(pid_t pid
)
486 struct winbindd_child
*child
;
488 for (child
= children
; child
!= NULL
; child
= child
->next
) {
489 if (child
->pid
== pid
) {
495 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
499 /* This will be re-added in fork_domain_child() */
501 DLIST_REMOVE(children
, child
);
503 remove_fd_event(&child
->event
);
504 close(child
->event
.fd
);
506 child
->event
.flags
= 0;
509 if (child
->requests
) {
511 * schedule_async_request() will also
512 * clear this event but the call is
513 * idempotent so it doesn't hurt to
514 * cover all possible future code
517 TALLOC_FREE(child
->requests
->reply_timeout_event
);
520 schedule_async_request(child
);
523 /* Ensure any negative cache entries with the netbios or realm names are removed. */
525 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
527 flush_negative_conn_cache_for_domain(domain
->name
);
528 if (*domain
->alt_name
) {
529 flush_negative_conn_cache_for_domain(domain
->alt_name
);
534 * Parent winbindd process sets its own debug level first and then
535 * sends a message to all the winbindd children to adjust their debug
536 * level to that of parents.
539 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
542 struct server_id server_id
,
545 struct winbindd_child
*child
;
547 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
549 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
551 for (child
= children
; child
!= NULL
; child
= child
->next
) {
553 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
554 (unsigned int)child
->pid
));
556 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
559 strlen((char *) data
->data
) + 1);
563 /* Set our domains as offline and forward the offline message to our children. */
565 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
568 struct server_id server_id
,
571 struct winbindd_child
*child
;
572 struct winbindd_domain
*domain
;
574 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
576 if (!lp_winbind_offline_logon()) {
577 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
581 /* Set our global state as offline. */
582 if (!set_global_winbindd_state_offline()) {
583 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
587 /* Set all our domains as offline. */
588 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
589 if (domain
->internal
) {
592 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
593 set_domain_offline(domain
);
596 for (child
= children
; child
!= NULL
; child
= child
->next
) {
597 /* Don't send message to internal childs. We've already
599 if (!child
->domain
|| winbindd_internal_child(child
)) {
603 /* Or internal domains (this should not be possible....) */
604 if (child
->domain
->internal
) {
608 /* Each winbindd child should only process requests for one domain - make sure
609 we only set it online / offline for that domain. */
611 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
612 (unsigned int)child
->pid
, domain
->name
));
614 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
616 (uint8
*)child
->domain
->name
,
617 strlen(child
->domain
->name
)+1);
621 /* Set our domains as online and forward the online message to our children. */
623 void winbind_msg_online(struct messaging_context
*msg_ctx
,
626 struct server_id server_id
,
629 struct winbindd_child
*child
;
630 struct winbindd_domain
*domain
;
632 DEBUG(10,("winbind_msg_online: got online message.\n"));
634 if (!lp_winbind_offline_logon()) {
635 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
639 /* Set our global state as online. */
640 set_global_winbindd_state_online();
642 smb_nscd_flush_user_cache();
643 smb_nscd_flush_group_cache();
645 /* Set all our domains as online. */
646 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
647 if (domain
->internal
) {
650 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
652 winbindd_flush_negative_conn_cache(domain
);
653 set_domain_online_request(domain
);
655 /* Send an online message to the idmap child when our
656 primary domain comes back online */
658 if ( domain
->primary
) {
659 struct winbindd_child
*idmap
= idmap_child();
661 if ( idmap
->pid
!= 0 ) {
662 messaging_send_buf(msg_ctx
,
663 pid_to_procid(idmap
->pid
),
665 (uint8
*)domain
->name
,
666 strlen(domain
->name
)+1);
672 for (child
= children
; child
!= NULL
; child
= child
->next
) {
673 /* Don't send message to internal childs. */
674 if (!child
->domain
|| winbindd_internal_child(child
)) {
678 /* Or internal domains (this should not be possible....) */
679 if (child
->domain
->internal
) {
683 /* Each winbindd child should only process requests for one domain - make sure
684 we only set it online / offline for that domain. */
686 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
687 (unsigned int)child
->pid
, child
->domain
->name
));
689 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
691 (uint8
*)child
->domain
->name
,
692 strlen(child
->domain
->name
)+1);
696 /* Forward the online/offline messages to our children. */
697 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
700 struct server_id server_id
,
703 struct winbindd_child
*child
;
705 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
707 for (child
= children
; child
!= NULL
; child
= child
->next
) {
708 if (child
->domain
&& child
->domain
->primary
) {
709 DEBUG(10,("winbind_msg_onlinestatus: "
710 "sending message to pid %u of primary domain.\n",
711 (unsigned int)child
->pid
));
712 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
713 MSG_WINBIND_ONLINESTATUS
,
721 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
724 struct server_id server_id
,
727 struct winbindd_child
*child
;
729 DEBUG(10,("winbind_msg_dump_event_list received\n"));
731 dump_event_list(winbind_event_context());
733 for (child
= children
; child
!= NULL
; child
= child
->next
) {
735 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
736 (unsigned int)child
->pid
));
738 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
745 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
748 struct server_id server_id
,
752 const char *message
= NULL
;
753 struct server_id
*sender
= NULL
;
754 const char *domain
= NULL
;
757 struct winbindd_domain
*dom
= NULL
;
759 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
761 if (!data
|| !data
->data
) {
765 if (data
->length
< sizeof(struct server_id
)) {
769 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
774 sender
= (struct server_id
*)data
->data
;
775 if (data
->length
> sizeof(struct server_id
)) {
776 domain
= (const char *)data
->data
+sizeof(struct server_id
);
781 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
784 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
785 find_domain_from_name_noinit(domain
));
787 talloc_destroy(mem_ctx
);
791 messaging_send_buf(msg_ctx
, *sender
,
792 MSG_WINBIND_DUMP_DOMAIN_LIST
,
793 (uint8_t *)message
, strlen(message
) + 1);
795 talloc_destroy(mem_ctx
);
800 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
802 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
803 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
805 talloc_destroy(mem_ctx
);
809 s
= talloc_asprintf_append(s
, "%s\n", message
);
811 talloc_destroy(mem_ctx
);
816 status
= messaging_send_buf(msg_ctx
, *sender
,
817 MSG_WINBIND_DUMP_DOMAIN_LIST
,
818 (uint8_t *)s
, strlen(s
) + 1);
819 if (!NT_STATUS_IS_OK(status
)) {
820 DEBUG(0,("failed to send message: %s\n",
824 talloc_destroy(mem_ctx
);
827 static void account_lockout_policy_handler(struct event_context
*ctx
,
828 struct timed_event
*te
,
832 struct winbindd_child
*child
=
833 (struct winbindd_child
*)private_data
;
834 TALLOC_CTX
*mem_ctx
= NULL
;
835 struct winbindd_methods
*methods
;
836 struct samr_DomInfo12 lockout_policy
;
839 DEBUG(10,("account_lockout_policy_handler called\n"));
841 TALLOC_FREE(child
->lockout_policy_event
);
843 if ( !winbindd_can_contact_domain( child
->domain
) ) {
844 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
845 "do not have an incoming trust to domain %s\n",
846 child
->domain
->name
));
851 methods
= child
->domain
->methods
;
853 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
855 result
= NT_STATUS_NO_MEMORY
;
857 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
859 TALLOC_FREE(mem_ctx
);
861 if (!NT_STATUS_IS_OK(result
)) {
862 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
866 child
->lockout_policy_event
= event_add_timed(winbind_event_context(), NULL
,
867 timeval_current_ofs(3600, 0),
868 account_lockout_policy_handler
,
872 static time_t get_machine_password_timeout(void)
874 /* until we have gpo support use lp setting */
875 return lp_machine_password_timeout();
878 static bool calculate_next_machine_pwd_change(const char *domain
,
881 time_t pass_last_set_time
;
886 pw
= secrets_fetch_machine_password(domain
,
891 DEBUG(0,("cannot fetch own machine password ????"));
897 timeout
= get_machine_password_timeout();
899 DEBUG(10,("machine password never expires\n"));
903 if (time(NULL
) < (pass_last_set_time
+ timeout
)) {
904 next_change
= pass_last_set_time
+ timeout
;
905 DEBUG(10,("machine password still valid until: %s\n",
906 http_timestring(talloc_tos(), next_change
)));
907 *t
= timeval_set(next_change
, 0);
911 DEBUG(10,("machine password expired, needs immediate change\n"));
918 static void machine_password_change_handler(struct event_context
*ctx
,
919 struct timed_event
*te
,
923 struct winbindd_child
*child
=
924 (struct winbindd_child
*)private_data
;
925 struct rpc_pipe_client
*netlogon_pipe
= NULL
;
928 struct timeval next_change
;
930 DEBUG(10,("machine_password_change_handler called\n"));
932 TALLOC_FREE(child
->machine_password_change_event
);
934 if (!calculate_next_machine_pwd_change(child
->domain
->name
,
939 if (!winbindd_can_contact_domain(child
->domain
)) {
940 DEBUG(10,("machine_password_change_handler: Removing myself since I "
941 "do not have an incoming trust to domain %s\n",
942 child
->domain
->name
));
946 result
= cm_connect_netlogon(child
->domain
, &netlogon_pipe
);
947 if (!NT_STATUS_IS_OK(result
)) {
948 DEBUG(10,("machine_password_change_handler: "
949 "failed to connect netlogon pipe: %s\n",
954 frame
= talloc_stackframe();
956 result
= trust_pw_find_change_and_store_it(netlogon_pipe
,
958 child
->domain
->name
);
961 if (!NT_STATUS_IS_OK(result
)) {
962 DEBUG(10,("machine_password_change_handler: "
963 "failed to change machine password: %s\n",
966 DEBUG(10,("machine_password_change_handler: "
967 "successfully changed machine password\n"));
970 child
->machine_password_change_event
= event_add_timed(winbind_event_context(), NULL
,
972 machine_password_change_handler
,
976 /* Deal with a request to go offline. */
978 static void child_msg_offline(struct messaging_context
*msg
,
981 struct server_id server_id
,
984 struct winbindd_domain
*domain
;
985 struct winbindd_domain
*primary_domain
= NULL
;
986 const char *domainname
= (const char *)data
->data
;
988 if (data
->data
== NULL
|| data
->length
== 0) {
992 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
994 if (!lp_winbind_offline_logon()) {
995 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
999 primary_domain
= find_our_domain();
1001 /* Mark the requested domain offline. */
1003 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1004 if (domain
->internal
) {
1007 if (strequal(domain
->name
, domainname
)) {
1008 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
1009 set_domain_offline(domain
);
1010 /* we are in the trusted domain, set the primary domain
1012 if (domain
!= primary_domain
) {
1013 set_domain_offline(primary_domain
);
1019 /* Deal with a request to go online. */
1021 static void child_msg_online(struct messaging_context
*msg
,
1024 struct server_id server_id
,
1027 struct winbindd_domain
*domain
;
1028 struct winbindd_domain
*primary_domain
= NULL
;
1029 const char *domainname
= (const char *)data
->data
;
1031 if (data
->data
== NULL
|| data
->length
== 0) {
1035 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
1037 if (!lp_winbind_offline_logon()) {
1038 DEBUG(10,("child_msg_online: rejecting online message.\n"));
1042 primary_domain
= find_our_domain();
1044 /* Set our global state as online. */
1045 set_global_winbindd_state_online();
1047 /* Try and mark everything online - delete any negative cache entries
1048 to force a reconnect now. */
1050 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1051 if (domain
->internal
) {
1054 if (strequal(domain
->name
, domainname
)) {
1055 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
1056 winbindd_flush_negative_conn_cache(domain
);
1057 set_domain_online_request(domain
);
1059 /* we can be in trusted domain, which will contact primary domain
1060 * we have to bring primary domain online in trusted domain process
1061 * see, winbindd_dual_pam_auth() --> winbindd_dual_pam_auth_samlogon()
1062 * --> contact_domain = find_our_domain()
1064 if (domain
!= primary_domain
) {
1065 winbindd_flush_negative_conn_cache(primary_domain
);
1066 set_domain_online_request(primary_domain
);
1072 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
1074 struct winbindd_domain
*domain
;
1077 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
1078 get_global_winbindd_state_offline() ?
1079 "Offline":"Online")) == NULL
) {
1083 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1084 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
1087 "Online":"Offline")) == NULL
) {
1092 buf
= talloc_asprintf_append_buffer(buf
, "\n");
1094 DEBUG(5,("collect_onlinestatus: %s", buf
));
1099 static void child_msg_onlinestatus(struct messaging_context
*msg_ctx
,
1102 struct server_id server_id
,
1105 TALLOC_CTX
*mem_ctx
;
1106 const char *message
;
1107 struct server_id
*sender
;
1109 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
1115 sender
= (struct server_id
*)data
->data
;
1117 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
1118 if (mem_ctx
== NULL
) {
1122 message
= collect_onlinestatus(mem_ctx
);
1123 if (message
== NULL
) {
1124 talloc_destroy(mem_ctx
);
1128 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
1129 (uint8
*)message
, strlen(message
) + 1);
1131 talloc_destroy(mem_ctx
);
1134 static void child_msg_dump_event_list(struct messaging_context
*msg
,
1137 struct server_id server_id
,
1140 DEBUG(5,("child_msg_dump_event_list received\n"));
1142 dump_event_list(winbind_event_context());
1145 bool winbindd_reinit_after_fork(const char *logfilename
)
1147 struct winbindd_domain
*domain
;
1148 struct winbindd_child
*cl
;
1150 if (!reinit_after_fork(winbind_messaging_context(),
1151 winbind_event_context(), true)) {
1152 DEBUG(0,("reinit_after_fork() failed\n"));
1156 close_conns_after_fork();
1158 if (!override_logfile
&& logfilename
) {
1159 lp_set_logfile(logfilename
);
1163 /* Don't handle the same messages as our parent. */
1164 messaging_deregister(winbind_messaging_context(),
1165 MSG_SMB_CONF_UPDATED
, NULL
);
1166 messaging_deregister(winbind_messaging_context(),
1167 MSG_SHUTDOWN
, NULL
);
1168 messaging_deregister(winbind_messaging_context(),
1169 MSG_WINBIND_OFFLINE
, NULL
);
1170 messaging_deregister(winbind_messaging_context(),
1171 MSG_WINBIND_ONLINE
, NULL
);
1172 messaging_deregister(winbind_messaging_context(),
1173 MSG_WINBIND_ONLINESTATUS
, NULL
);
1174 messaging_deregister(winbind_messaging_context(),
1175 MSG_DUMP_EVENT_LIST
, NULL
);
1176 messaging_deregister(winbind_messaging_context(),
1177 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1178 messaging_deregister(winbind_messaging_context(),
1181 /* We have destroyed all events in the winbindd_event_context
1182 * in reinit_after_fork(), so clean out all possible pending
1183 * event pointers. */
1185 /* Deal with check_online_events. */
1187 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1188 TALLOC_FREE(domain
->check_online_event
);
1191 /* Ensure we're not handling a credential cache event inherited
1192 * from our parent. */
1194 ccache_remove_all_after_fork();
1196 /* Destroy all possible events in child list. */
1197 for (cl
= children
; cl
!= NULL
; cl
= cl
->next
) {
1198 struct winbindd_async_request
*request
;
1200 for (request
= cl
->requests
; request
; request
= request
->next
) {
1201 TALLOC_FREE(request
->reply_timeout_event
);
1203 TALLOC_FREE(cl
->lockout_policy_event
);
1204 TALLOC_FREE(cl
->machine_password_change_event
);
1206 /* Children should never be able to send
1207 * each other messages, all meesages must
1208 * go through the parent.
1213 * This is a little tricky, we don't want child
1214 * to send MSG_WINBIND_ONLINE to idmap_child().
1215 * If we are in the child of trusted domain or
1216 * in the process created by fork_child_dc_connect().
1217 * And the trusted domain cannot go online,
1218 * fork_child_dc_connection() sends MSG_WINBIND_ONLINE
1219 * periodically to idmap_child().
1220 * look, fork_child_dc_connect() ---> getdcs() --->
1221 * get_dc_name_via_netlogon() ---> cm_connect_netlogon()
1222 * ---> init_dc_connection() ---> cm_open_connection --->
1223 * set_domain_online(), here send MSG_WINBIND_ONLINE to
1232 static bool fork_domain_child(struct winbindd_child
*child
)
1235 struct winbindd_cli_state state
;
1236 struct winbindd_domain
*primary_domain
= NULL
;
1238 if (child
->domain
) {
1239 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1240 child
->domain
->name
));
1242 DEBUG(10, ("fork_domain_child called without domain.\n"));
1245 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1246 DEBUG(0, ("Could not open child pipe: %s\n",
1252 state
.pid
= sys_getpid();
1254 child
->pid
= sys_fork();
1256 if (child
->pid
== -1) {
1257 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1261 if (child
->pid
!= 0) {
1264 child
->next
= child
->prev
= NULL
;
1265 DLIST_ADD(children
, child
);
1266 child
->event
.fd
= fdpair
[1];
1267 child
->event
.flags
= 0;
1268 add_fd_event(&child
->event
);
1274 DEBUG(10, ("Child process %d\n", (int)sys_getpid()));
1276 /* Stop zombies in children */
1279 state
.sock
= fdpair
[0];
1282 if (!winbindd_reinit_after_fork(child
->logfilename
)) {
1286 /* Handle online/offline messages. */
1287 messaging_register(winbind_messaging_context(), NULL
,
1288 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1289 messaging_register(winbind_messaging_context(), NULL
,
1290 MSG_WINBIND_ONLINE
, child_msg_online
);
1291 messaging_register(winbind_messaging_context(), NULL
,
1292 MSG_WINBIND_ONLINESTATUS
, child_msg_onlinestatus
);
1293 messaging_register(winbind_messaging_context(), NULL
,
1294 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1295 messaging_register(winbind_messaging_context(), NULL
,
1296 MSG_DEBUG
, debug_message
);
1298 primary_domain
= find_our_domain();
1300 if (primary_domain
== NULL
) {
1301 smb_panic("no primary domain found");
1304 /* It doesn't matter if we allow cache login,
1305 * try to bring domain online after fork. */
1306 if ( child
->domain
) {
1307 child
->domain
->startup
= True
;
1308 child
->domain
->startup_time
= time(NULL
);
1309 /* we can be in primary domain or in trusted domain
1310 * If we are in trusted domain, set the primary domain
1311 * in start-up mode */
1312 if (!(child
->domain
->internal
)) {
1313 set_domain_online_request(child
->domain
);
1314 if (!(child
->domain
->primary
)) {
1315 primary_domain
->startup
= True
;
1316 primary_domain
->startup_time
= time(NULL
);
1317 set_domain_online_request(primary_domain
);
1323 * We are in idmap child, make sure that we set the
1324 * check_online_event to bring primary domain online.
1326 if (child
== idmap_child()) {
1327 set_domain_online_request(primary_domain
);
1330 /* We might be in the idmap child...*/
1331 if (child
->domain
&& !(child
->domain
->internal
) &&
1332 lp_winbind_offline_logon()) {
1334 set_domain_online_request(child
->domain
);
1336 if (primary_domain
&& (primary_domain
!= child
->domain
)) {
1337 /* We need to talk to the primary
1338 * domain as well as the trusted
1339 * domain inside a trusted domain
1342 * set_dc_type_and_flags_trustinfo()
1345 set_domain_online_request(primary_domain
);
1348 child
->lockout_policy_event
= event_add_timed(
1349 winbind_event_context(), NULL
, timeval_zero(),
1350 account_lockout_policy_handler
,
1354 if (child
->domain
&& child
->domain
->primary
&&
1355 !lp_use_kerberos_keytab() &&
1356 lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1358 struct timeval next_change
;
1360 if (calculate_next_machine_pwd_change(child
->domain
->name
,
1362 child
->machine_password_change_event
= event_add_timed(
1363 winbind_event_context(), NULL
, next_change
,
1364 machine_password_change_handler
,
1376 TALLOC_CTX
*frame
= talloc_stackframe();
1378 /* check for signals */
1379 winbind_check_sigterm(false);
1380 winbind_check_sighup(override_logfile
? NULL
:
1381 child
->logfilename
);
1383 run_events(winbind_event_context(), 0, NULL
, NULL
);
1387 if (child
->domain
&& child
->domain
->startup
&&
1388 (now
.tv_sec
> child
->domain
->startup_time
+ 30)) {
1389 /* No longer in "startup" mode. */
1390 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1391 child
->domain
->name
));
1392 child
->domain
->startup
= False
;
1395 tp
= get_timed_events_timeout(winbind_event_context(), &t
);
1397 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1398 (unsigned int)tp
->tv_sec
, (unsigned int)tp
->tv_usec
));
1401 /* Handle messages */
1403 message_dispatch(winbind_messaging_context());
1406 FD_SET(state
.sock
, &read_fds
);
1408 ret
= sys_select(state
.sock
+ 1, &read_fds
, NULL
, NULL
, tp
);
1411 DEBUG(11,("nothing is ready yet, continue\n"));
1416 if (ret
== -1 && errno
== EINTR
) {
1417 /* We got a signal - continue. */
1422 if (ret
== -1 && errno
!= EINTR
) {
1423 DEBUG(0,("select error occured\n"));
1429 /* fetch a request from the main daemon */
1430 child_read_request(&state
);
1432 if (state
.finished
) {
1433 /* we lost contact with our parent */
1437 DEBUG(4,("child daemon request %d\n", (int)state
.request
.cmd
));
1439 ZERO_STRUCT(state
.response
);
1440 state
.request
.null_term
= '\0';
1441 child_process_request(child
, &state
);
1443 SAFE_FREE(state
.request
.extra_data
.data
);
1445 cache_store_response(sys_getpid(), &state
.response
);
1447 SAFE_FREE(state
.response
.extra_data
.data
);
1449 /* We just send the result code back, the result
1450 * structure needs to be fetched via the
1451 * winbindd_cache. Hmm. That needs fixing... */
1453 if (write_data(state
.sock
,
1454 (const char *)&state
.response
.result
,
1455 sizeof(state
.response
.result
)) !=
1456 sizeof(state
.response
.result
)) {
1457 DEBUG(0, ("Could not write result\n"));