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 state
= TALLOC_P(mem_ctx
, struct winbindd_async_request
);
126 DEBUG(0, ("talloc failed\n"));
127 continuation(private_data
, False
);
131 state
->mem_ctx
= mem_ctx
;
132 state
->child
= child
;
133 state
->reply_timeout_event
= NULL
;
134 state
->request
= request
;
135 state
->response
= response
;
136 state
->continuation
= continuation
;
137 state
->private_data
= private_data
;
139 DLIST_ADD_END(child
->requests
, state
, struct winbindd_async_request
*);
141 schedule_async_request(child
);
146 static void async_main_request_sent(void *private_data
, bool success
)
148 struct winbindd_async_request
*state
=
149 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
152 DEBUG(5, ("Could not send async request\n"));
153 async_request_fail(state
);
157 if (state
->request
->extra_len
== 0) {
158 async_request_sent(private_data
, True
);
162 setup_async_write(&state
->child
->event
, state
->request
->extra_data
.data
,
163 state
->request
->extra_len
,
164 async_request_sent
, state
);
167 /****************************************************************
168 Handler triggered if the child winbindd doesn't respond within
170 ****************************************************************/
172 static void async_request_timeout_handler(struct event_context
*ctx
,
173 struct timed_event
*te
,
174 const struct timeval
*now
,
177 struct winbindd_async_request
*state
=
178 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
180 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
181 "Closing connection to it.\n",
184 /* Deal with the reply - set to error. */
185 async_reply_recv(private_data
, False
);
188 /**************************************************************
189 Common function called on both async send and recv fail.
190 Cleans up the child and schedules the next request.
191 **************************************************************/
193 static void async_request_fail(struct winbindd_async_request
*state
)
195 DLIST_REMOVE(state
->child
->requests
, state
);
197 TALLOC_FREE(state
->reply_timeout_event
);
199 SMB_ASSERT(state
->child_pid
!= (pid_t
)0);
201 /* If not already reaped, send kill signal to child. */
202 if (state
->child
->pid
== state
->child_pid
) {
203 kill(state
->child_pid
, SIGTERM
);
206 * Close the socket to the child.
208 winbind_child_died(state
->child_pid
);
211 state
->response
->length
= sizeof(struct winbindd_response
);
212 state
->response
->result
= WINBINDD_ERROR
;
213 state
->continuation(state
->private_data
, False
);
216 static void async_request_sent(void *private_data_data
, bool success
)
218 struct winbindd_async_request
*state
=
219 talloc_get_type_abort(private_data_data
, struct winbindd_async_request
);
222 DEBUG(5, ("Could not send async request to child pid %u\n",
223 (unsigned int)state
->child_pid
));
224 async_request_fail(state
);
228 /* Request successfully sent to the child, setup the wait for reply */
230 setup_async_read(&state
->child
->event
,
231 &state
->response
->result
,
232 sizeof(state
->response
->result
),
233 async_reply_recv
, state
);
236 * Set up a timeout of 300 seconds for the response.
237 * If we don't get it close the child socket and
241 state
->reply_timeout_event
= event_add_timed(winbind_event_context(),
243 timeval_current_ofs(300,0),
244 "async_request_timeout",
245 async_request_timeout_handler
,
247 if (!state
->reply_timeout_event
) {
248 smb_panic("async_request_sent: failed to add timeout handler.\n");
252 static void async_reply_recv(void *private_data
, bool success
)
254 struct winbindd_async_request
*state
=
255 talloc_get_type_abort(private_data
, struct winbindd_async_request
);
256 struct winbindd_child
*child
= state
->child
;
258 TALLOC_FREE(state
->reply_timeout_event
);
260 state
->response
->length
= sizeof(struct winbindd_response
);
263 DEBUG(5, ("Could not receive async reply from child pid %u\n",
264 (unsigned int)state
->child_pid
));
266 cache_cleanup_response(state
->child_pid
);
267 async_request_fail(state
);
271 SMB_ASSERT(cache_retrieve_response(state
->child_pid
,
274 cache_cleanup_response(state
->child_pid
);
276 DLIST_REMOVE(child
->requests
, state
);
278 schedule_async_request(child
);
280 state
->continuation(state
->private_data
, True
);
283 static bool fork_domain_child(struct winbindd_child
*child
);
285 static void schedule_async_request(struct winbindd_child
*child
)
287 struct winbindd_async_request
*request
= child
->requests
;
289 if (request
== NULL
) {
293 if (child
->event
.flags
!= 0) {
297 if ((child
->pid
== 0) && (!fork_domain_child(child
))) {
298 /* Cancel all outstanding requests */
300 while (request
!= NULL
) {
301 /* request might be free'd in the continuation */
302 struct winbindd_async_request
*next
= request
->next
;
303 request
->continuation(request
->private_data
, False
);
309 /* Now we know who we're sending to - remember the pid. */
310 request
->child_pid
= child
->pid
;
312 setup_async_write(&child
->event
, request
->request
,
313 sizeof(*request
->request
),
314 async_main_request_sent
, request
);
319 struct domain_request_state
{
321 struct winbindd_domain
*domain
;
322 struct winbindd_request
*request
;
323 struct winbindd_response
*response
;
324 void (*continuation
)(void *private_data_data
, bool success
);
325 void *private_data_data
;
328 static void domain_init_recv(void *private_data_data
, bool success
);
330 void async_domain_request(TALLOC_CTX
*mem_ctx
,
331 struct winbindd_domain
*domain
,
332 struct winbindd_request
*request
,
333 struct winbindd_response
*response
,
334 void (*continuation
)(void *private_data_data
, bool success
),
335 void *private_data_data
)
337 struct domain_request_state
*state
;
339 if (domain
->initialized
) {
340 async_request(mem_ctx
, &domain
->child
, request
, response
,
341 continuation
, private_data_data
);
345 state
= TALLOC_P(mem_ctx
, struct domain_request_state
);
347 DEBUG(0, ("talloc failed\n"));
348 continuation(private_data_data
, False
);
352 state
->mem_ctx
= mem_ctx
;
353 state
->domain
= domain
;
354 state
->request
= request
;
355 state
->response
= response
;
356 state
->continuation
= continuation
;
357 state
->private_data_data
= private_data_data
;
359 init_child_connection(domain
, domain_init_recv
, state
);
362 static void domain_init_recv(void *private_data_data
, bool success
)
364 struct domain_request_state
*state
=
365 talloc_get_type_abort(private_data_data
, struct domain_request_state
);
368 DEBUG(5, ("Domain init returned an error\n"));
369 state
->continuation(state
->private_data_data
, False
);
373 async_request(state
->mem_ctx
, &state
->domain
->child
,
374 state
->request
, state
->response
,
375 state
->continuation
, state
->private_data_data
);
378 static void recvfrom_child(void *private_data_data
, bool success
)
380 struct winbindd_cli_state
*state
=
381 talloc_get_type_abort(private_data_data
, struct winbindd_cli_state
);
382 enum winbindd_result result
= state
->response
.result
;
384 /* This is an optimization: The child has written directly to the
385 * response buffer. The request itself is still in pending state,
386 * state that in the result code. */
388 state
->response
.result
= WINBINDD_PENDING
;
390 if ((!success
) || (result
!= WINBINDD_OK
)) {
391 request_error(state
);
398 void sendto_child(struct winbindd_cli_state
*state
,
399 struct winbindd_child
*child
)
401 async_request(state
->mem_ctx
, child
, &state
->request
,
402 &state
->response
, recvfrom_child
, state
);
405 void sendto_domain(struct winbindd_cli_state
*state
,
406 struct winbindd_domain
*domain
)
408 async_domain_request(state
->mem_ctx
, domain
,
409 &state
->request
, &state
->response
,
410 recvfrom_child
, state
);
413 static void child_process_request(struct winbindd_child
*child
,
414 struct winbindd_cli_state
*state
)
416 struct winbindd_domain
*domain
= child
->domain
;
417 const struct winbindd_child_dispatch_table
*table
= child
->table
;
419 /* Free response data - we may be interrupted and receive another
420 command before being able to send this data off. */
422 state
->response
.result
= WINBINDD_ERROR
;
423 state
->response
.length
= sizeof(struct winbindd_response
);
425 /* as all requests in the child are sync, we can use talloc_tos() */
426 state
->mem_ctx
= talloc_tos();
428 /* Process command */
430 for (; table
->name
; table
++) {
431 if (state
->request
.cmd
== table
->struct_cmd
) {
432 DEBUG(10,("child_process_request: request fn %s\n",
434 state
->response
.result
= table
->struct_fn(domain
, state
);
439 DEBUG(1 ,("child_process_request: unknown request fn number %d\n",
440 (int)state
->request
.cmd
));
441 state
->response
.result
= WINBINDD_ERROR
;
444 void setup_child(struct winbindd_child
*child
,
445 const struct winbindd_child_dispatch_table
*table
,
446 const char *logprefix
,
449 if (logprefix
&& logname
) {
450 if (asprintf(&child
->logfilename
, "%s/%s-%s",
451 get_dyn_LOGFILEBASE(), logprefix
, logname
) < 0) {
452 smb_panic("Internal error: asprintf failed");
455 smb_panic("Internal error: logprefix == NULL && "
459 child
->domain
= NULL
;
460 child
->table
= table
;
463 struct winbindd_child
*children
= NULL
;
465 void winbind_child_died(pid_t pid
)
467 struct winbindd_child
*child
;
469 for (child
= children
; child
!= NULL
; child
= child
->next
) {
470 if (child
->pid
== pid
) {
476 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid
));
480 /* This will be re-added in fork_domain_child() */
482 DLIST_REMOVE(children
, child
);
484 remove_fd_event(&child
->event
);
485 close(child
->event
.fd
);
487 child
->event
.flags
= 0;
490 schedule_async_request(child
);
493 /* Ensure any negative cache entries with the netbios or realm names are removed. */
495 void winbindd_flush_negative_conn_cache(struct winbindd_domain
*domain
)
497 flush_negative_conn_cache_for_domain(domain
->name
);
498 if (*domain
->alt_name
) {
499 flush_negative_conn_cache_for_domain(domain
->alt_name
);
504 * Parent winbindd process sets its own debug level first and then
505 * sends a message to all the winbindd children to adjust their debug
506 * level to that of parents.
509 void winbind_msg_debug(struct messaging_context
*msg_ctx
,
512 struct server_id server_id
,
515 struct winbindd_child
*child
;
517 DEBUG(10,("winbind_msg_debug: got debug message.\n"));
519 debug_message(msg_ctx
, private_data
, MSG_DEBUG
, server_id
, data
);
521 for (child
= children
; child
!= NULL
; child
= child
->next
) {
523 DEBUG(10,("winbind_msg_debug: sending message to pid %u.\n",
524 (unsigned int)child
->pid
));
526 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
529 strlen((char *) data
->data
) + 1);
533 /* Set our domains as offline and forward the offline message to our children. */
535 void winbind_msg_offline(struct messaging_context
*msg_ctx
,
538 struct server_id server_id
,
541 struct winbindd_child
*child
;
542 struct winbindd_domain
*domain
;
544 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
546 if (!lp_winbind_offline_logon()) {
547 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
551 /* Set our global state as offline. */
552 if (!set_global_winbindd_state_offline()) {
553 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
557 /* Set all our domains as offline. */
558 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
559 if (domain
->internal
) {
562 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain
->name
));
563 set_domain_offline(domain
);
566 for (child
= children
; child
!= NULL
; child
= child
->next
) {
567 /* Don't send message to internal childs. We've already
569 if (!child
->domain
|| winbindd_internal_child(child
)) {
573 /* Or internal domains (this should not be possible....) */
574 if (child
->domain
->internal
) {
578 /* Each winbindd child should only process requests for one domain - make sure
579 we only set it online / offline for that domain. */
581 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
582 (unsigned int)child
->pid
, domain
->name
));
584 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
586 (uint8
*)child
->domain
->name
,
587 strlen(child
->domain
->name
)+1);
591 /* Set our domains as online and forward the online message to our children. */
593 void winbind_msg_online(struct messaging_context
*msg_ctx
,
596 struct server_id server_id
,
599 struct winbindd_child
*child
;
600 struct winbindd_domain
*domain
;
602 DEBUG(10,("winbind_msg_online: got online message.\n"));
604 if (!lp_winbind_offline_logon()) {
605 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
609 /* Set our global state as online. */
610 set_global_winbindd_state_online();
612 smb_nscd_flush_user_cache();
613 smb_nscd_flush_group_cache();
615 /* Set all our domains as online. */
616 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
617 if (domain
->internal
) {
620 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain
->name
));
622 winbindd_flush_negative_conn_cache(domain
);
623 set_domain_online_request(domain
);
625 /* Send an online message to the idmap child when our
626 primary domain comes back online */
628 if ( domain
->primary
) {
629 struct winbindd_child
*idmap
= idmap_child();
631 if ( idmap
->pid
!= 0 ) {
632 messaging_send_buf(msg_ctx
,
633 pid_to_procid(idmap
->pid
),
635 (uint8
*)domain
->name
,
636 strlen(domain
->name
)+1);
642 for (child
= children
; child
!= NULL
; child
= child
->next
) {
643 /* Don't send message to internal childs. */
644 if (!child
->domain
|| winbindd_internal_child(child
)) {
648 /* Or internal domains (this should not be possible....) */
649 if (child
->domain
->internal
) {
653 /* Each winbindd child should only process requests for one domain - make sure
654 we only set it online / offline for that domain. */
656 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
657 (unsigned int)child
->pid
, child
->domain
->name
));
659 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
661 (uint8
*)child
->domain
->name
,
662 strlen(child
->domain
->name
)+1);
666 /* Forward the online/offline messages to our children. */
667 void winbind_msg_onlinestatus(struct messaging_context
*msg_ctx
,
670 struct server_id server_id
,
673 struct winbindd_child
*child
;
675 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
677 for (child
= children
; child
!= NULL
; child
= child
->next
) {
678 if (child
->domain
&& child
->domain
->primary
) {
679 DEBUG(10,("winbind_msg_onlinestatus: "
680 "sending message to pid %u of primary domain.\n",
681 (unsigned int)child
->pid
));
682 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
683 MSG_WINBIND_ONLINESTATUS
,
691 void winbind_msg_dump_event_list(struct messaging_context
*msg_ctx
,
694 struct server_id server_id
,
697 struct winbindd_child
*child
;
699 DEBUG(10,("winbind_msg_dump_event_list received\n"));
701 dump_event_list(winbind_event_context());
703 for (child
= children
; child
!= NULL
; child
= child
->next
) {
705 DEBUG(10,("winbind_msg_dump_event_list: sending message to pid %u\n",
706 (unsigned int)child
->pid
));
708 messaging_send_buf(msg_ctx
, pid_to_procid(child
->pid
),
715 void winbind_msg_dump_domain_list(struct messaging_context
*msg_ctx
,
718 struct server_id server_id
,
722 const char *message
= NULL
;
723 struct server_id
*sender
= NULL
;
724 const char *domain
= NULL
;
727 struct winbindd_domain
*dom
= NULL
;
729 DEBUG(5,("winbind_msg_dump_domain_list received.\n"));
731 if (!data
|| !data
->data
) {
735 if (data
->length
< sizeof(struct server_id
)) {
739 mem_ctx
= talloc_init("winbind_msg_dump_domain_list");
744 sender
= (struct server_id
*)data
->data
;
745 if (data
->length
> sizeof(struct server_id
)) {
746 domain
= (const char *)data
->data
+sizeof(struct server_id
);
751 DEBUG(5,("winbind_msg_dump_domain_list for domain: %s\n",
754 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
,
755 find_domain_from_name_noinit(domain
));
757 talloc_destroy(mem_ctx
);
761 messaging_send_buf(msg_ctx
, *sender
,
762 MSG_WINBIND_DUMP_DOMAIN_LIST
,
763 (uint8_t *)message
, strlen(message
) + 1);
765 talloc_destroy(mem_ctx
);
770 DEBUG(5,("winbind_msg_dump_domain_list all domains\n"));
772 for (dom
= domain_list(); dom
; dom
=dom
->next
) {
773 message
= NDR_PRINT_STRUCT_STRING(mem_ctx
, winbindd_domain
, dom
);
775 talloc_destroy(mem_ctx
);
779 s
= talloc_asprintf_append(s
, "%s\n", message
);
781 talloc_destroy(mem_ctx
);
786 status
= messaging_send_buf(msg_ctx
, *sender
,
787 MSG_WINBIND_DUMP_DOMAIN_LIST
,
788 (uint8_t *)s
, strlen(s
) + 1);
789 if (!NT_STATUS_IS_OK(status
)) {
790 DEBUG(0,("failed to send message: %s\n",
794 talloc_destroy(mem_ctx
);
797 static void account_lockout_policy_handler(struct event_context
*ctx
,
798 struct timed_event
*te
,
799 const struct timeval
*now
,
802 struct winbindd_child
*child
=
803 (struct winbindd_child
*)private_data
;
804 TALLOC_CTX
*mem_ctx
= NULL
;
805 struct winbindd_methods
*methods
;
806 struct samr_DomInfo12 lockout_policy
;
809 DEBUG(10,("account_lockout_policy_handler called\n"));
811 TALLOC_FREE(child
->lockout_policy_event
);
813 if ( !winbindd_can_contact_domain( child
->domain
) ) {
814 DEBUG(10,("account_lockout_policy_handler: Removing myself since I "
815 "do not have an incoming trust to domain %s\n",
816 child
->domain
->name
));
821 methods
= child
->domain
->methods
;
823 mem_ctx
= talloc_init("account_lockout_policy_handler ctx");
825 result
= NT_STATUS_NO_MEMORY
;
827 result
= methods
->lockout_policy(child
->domain
, mem_ctx
, &lockout_policy
);
829 TALLOC_FREE(mem_ctx
);
831 if (!NT_STATUS_IS_OK(result
)) {
832 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
836 child
->lockout_policy_event
= event_add_timed(winbind_event_context(), NULL
,
837 timeval_current_ofs(3600, 0),
838 "account_lockout_policy_handler",
839 account_lockout_policy_handler
,
843 /* Deal with a request to go offline. */
845 static void child_msg_offline(struct messaging_context
*msg
,
848 struct server_id server_id
,
851 struct winbindd_domain
*domain
;
852 const char *domainname
= (const char *)data
->data
;
854 if (data
->data
== NULL
|| data
->length
== 0) {
858 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname
));
860 if (!lp_winbind_offline_logon()) {
861 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
865 /* Mark the requested domain offline. */
867 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
868 if (domain
->internal
) {
871 if (strequal(domain
->name
, domainname
)) {
872 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain
->name
));
873 set_domain_offline(domain
);
878 /* Deal with a request to go online. */
880 static void child_msg_online(struct messaging_context
*msg
,
883 struct server_id server_id
,
886 struct winbindd_domain
*domain
;
887 const char *domainname
= (const char *)data
->data
;
889 if (data
->data
== NULL
|| data
->length
== 0) {
893 DEBUG(5,("child_msg_online received for domain %s.\n", domainname
));
895 if (!lp_winbind_offline_logon()) {
896 DEBUG(10,("child_msg_online: rejecting online message.\n"));
900 /* Set our global state as online. */
901 set_global_winbindd_state_online();
903 /* Try and mark everything online - delete any negative cache entries
904 to force a reconnect now. */
906 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
907 if (domain
->internal
) {
910 if (strequal(domain
->name
, domainname
)) {
911 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain
->name
));
912 winbindd_flush_negative_conn_cache(domain
);
913 set_domain_online_request(domain
);
918 static const char *collect_onlinestatus(TALLOC_CTX
*mem_ctx
)
920 struct winbindd_domain
*domain
;
923 if ((buf
= talloc_asprintf(mem_ctx
, "global:%s ",
924 get_global_winbindd_state_offline() ?
925 "Offline":"Online")) == NULL
) {
929 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
930 if ((buf
= talloc_asprintf_append_buffer(buf
, "%s:%s ",
933 "Online":"Offline")) == NULL
) {
938 buf
= talloc_asprintf_append_buffer(buf
, "\n");
940 DEBUG(5,("collect_onlinestatus: %s", buf
));
945 static void child_msg_onlinestatus(struct messaging_context
*msg_ctx
,
948 struct server_id server_id
,
953 struct server_id
*sender
;
955 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
961 sender
= (struct server_id
*)data
->data
;
963 mem_ctx
= talloc_init("winbind_msg_onlinestatus");
964 if (mem_ctx
== NULL
) {
968 message
= collect_onlinestatus(mem_ctx
);
969 if (message
== NULL
) {
970 talloc_destroy(mem_ctx
);
974 messaging_send_buf(msg_ctx
, *sender
, MSG_WINBIND_ONLINESTATUS
,
975 (uint8
*)message
, strlen(message
) + 1);
977 talloc_destroy(mem_ctx
);
980 static void child_msg_dump_event_list(struct messaging_context
*msg
,
983 struct server_id server_id
,
986 DEBUG(5,("child_msg_dump_event_list received\n"));
988 dump_event_list(winbind_event_context());
992 static bool fork_domain_child(struct winbindd_child
*child
)
995 struct winbindd_cli_state state
;
996 struct winbindd_domain
*domain
;
997 struct winbindd_domain
*primary_domain
= NULL
;
1000 DEBUG(10, ("fork_domain_child called for domain '%s'\n",
1001 child
->domain
->name
));
1003 DEBUG(10, ("fork_domain_child called without domain.\n"));
1006 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, fdpair
) != 0) {
1007 DEBUG(0, ("Could not open child pipe: %s\n",
1013 state
.pid
= sys_getpid();
1015 child
->pid
= sys_fork();
1017 if (child
->pid
== -1) {
1018 DEBUG(0, ("Could not fork: %s\n", strerror(errno
)));
1022 if (child
->pid
!= 0) {
1025 child
->next
= child
->prev
= NULL
;
1026 DLIST_ADD(children
, child
);
1027 child
->event
.fd
= fdpair
[1];
1028 child
->event
.flags
= 0;
1029 child
->requests
= NULL
;
1030 add_fd_event(&child
->event
);
1036 /* Stop zombies in children */
1039 state
.sock
= fdpair
[0];
1042 if (!reinit_after_fork(winbind_messaging_context(), true)) {
1043 DEBUG(0,("reinit_after_fork() failed\n"));
1047 close_conns_after_fork();
1049 if (!override_logfile
) {
1050 lp_set_logfile(child
->logfilename
);
1055 * For clustering, we need to re-init our ctdbd connection after the
1058 if (!NT_STATUS_IS_OK(messaging_reinit(winbind_messaging_context())))
1061 /* Don't handle the same messages as our parent. */
1062 messaging_deregister(winbind_messaging_context(),
1063 MSG_SMB_CONF_UPDATED
, NULL
);
1064 messaging_deregister(winbind_messaging_context(),
1065 MSG_SHUTDOWN
, NULL
);
1066 messaging_deregister(winbind_messaging_context(),
1067 MSG_WINBIND_OFFLINE
, NULL
);
1068 messaging_deregister(winbind_messaging_context(),
1069 MSG_WINBIND_ONLINE
, NULL
);
1070 messaging_deregister(winbind_messaging_context(),
1071 MSG_WINBIND_ONLINESTATUS
, NULL
);
1072 messaging_deregister(winbind_messaging_context(),
1073 MSG_DUMP_EVENT_LIST
, NULL
);
1074 messaging_deregister(winbind_messaging_context(),
1075 MSG_WINBIND_DUMP_DOMAIN_LIST
, NULL
);
1076 messaging_deregister(winbind_messaging_context(),
1079 /* Handle online/offline messages. */
1080 messaging_register(winbind_messaging_context(), NULL
,
1081 MSG_WINBIND_OFFLINE
, child_msg_offline
);
1082 messaging_register(winbind_messaging_context(), NULL
,
1083 MSG_WINBIND_ONLINE
, child_msg_online
);
1084 messaging_register(winbind_messaging_context(), NULL
,
1085 MSG_WINBIND_ONLINESTATUS
, child_msg_onlinestatus
);
1086 messaging_register(winbind_messaging_context(), NULL
,
1087 MSG_DUMP_EVENT_LIST
, child_msg_dump_event_list
);
1088 messaging_register(winbind_messaging_context(), NULL
,
1089 MSG_DEBUG
, debug_message
);
1091 if ( child
->domain
) {
1092 child
->domain
->startup
= True
;
1093 child
->domain
->startup_time
= time(NULL
);
1096 /* Ensure we have no pending check_online events other
1097 than one for this domain or the primary domain. */
1099 for (domain
= domain_list(); domain
; domain
= domain
->next
) {
1100 if (domain
->primary
) {
1101 primary_domain
= domain
;
1103 if ((domain
!= child
->domain
) && !domain
->primary
) {
1104 TALLOC_FREE(domain
->check_online_event
);
1108 /* Ensure we're not handling an event inherited from
1111 cancel_named_event(winbind_event_context(),
1112 "krb5_ticket_refresh_handler");
1114 /* We might be in the idmap child...*/
1115 if (child
->domain
&& !(child
->domain
->internal
) &&
1116 lp_winbind_offline_logon()) {
1118 set_domain_online_request(child
->domain
);
1120 if (primary_domain
!= child
->domain
) {
1121 /* We need to talk to the primary
1122 * domain as well as the trusted
1123 * domain inside a trusted domain
1126 * set_dc_type_and_flags_trustinfo()
1129 set_domain_online_request(primary_domain
);
1132 child
->lockout_policy_event
= event_add_timed(
1133 winbind_event_context(), NULL
, timeval_zero(),
1134 "account_lockout_policy_handler",
1135 account_lockout_policy_handler
,
1146 TALLOC_CTX
*frame
= talloc_stackframe();
1148 /* check for signals */
1149 winbind_check_sigterm(false);
1150 winbind_check_sighup(override_logfile
? NULL
:
1151 child
->logfilename
);
1153 run_events(winbind_event_context(), 0, NULL
, NULL
);
1157 if (child
->domain
&& child
->domain
->startup
&&
1158 (now
.tv_sec
> child
->domain
->startup_time
+ 30)) {
1159 /* No longer in "startup" mode. */
1160 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1161 child
->domain
->name
));
1162 child
->domain
->startup
= False
;
1165 tp
= get_timed_events_timeout(winbind_event_context(), &t
);
1167 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1168 (unsigned int)tp
->tv_sec
, (unsigned int)tp
->tv_usec
));
1171 /* Handle messages */
1173 message_dispatch(winbind_messaging_context());
1176 FD_SET(state
.sock
, &read_fds
);
1178 ret
= sys_select(state
.sock
+ 1, &read_fds
, NULL
, NULL
, tp
);
1181 DEBUG(11,("nothing is ready yet, continue\n"));
1186 if (ret
== -1 && errno
== EINTR
) {
1187 /* We got a signal - continue. */
1192 if (ret
== -1 && errno
!= EINTR
) {
1193 DEBUG(0,("select error occured\n"));
1199 /* fetch a request from the main daemon */
1200 child_read_request(&state
);
1202 if (state
.finished
) {
1203 /* we lost contact with our parent */
1207 DEBUG(4,("child daemon request %d\n", (int)state
.request
.cmd
));
1209 ZERO_STRUCT(state
.response
);
1210 state
.request
.null_term
= '\0';
1211 child_process_request(child
, &state
);
1213 SAFE_FREE(state
.request
.extra_data
.data
);
1215 cache_store_response(sys_getpid(), &state
.response
);
1217 SAFE_FREE(state
.response
.extra_data
.data
);
1219 /* We just send the result code back, the result
1220 * structure needs to be fetched via the
1221 * winbindd_cache. Hmm. That needs fixing... */
1223 if (write_data(state
.sock
,
1224 (const char *)&state
.response
.result
,
1225 sizeof(state
.response
.result
)) !=
1226 sizeof(state
.response
.result
)) {
1227 DEBUG(0, ("Could not write result\n"));