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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * We fork a child per domain to be able to act non-blocking in the main
26 * winbind daemon. A domain controller thousands of miles away being being
27 * slow replying with a 10.000 user list should not hold up netlogon calls
28 * that can be handled locally.
35 #define DBGC_CLASS DBGC_WINBIND
37 extern BOOL override_logfile;
39 /* Read some data from a client connection */
41 static void child_read_request(struct winbindd_cli_state *state)
47 len = read_data(state->sock, (char *)&state->request,
48 sizeof(state->request));
50 if (len != sizeof(state->request)) {
51 DEBUG(len > 0 ? 0 : 3, ("Got invalid request length: %d\n", (int)len));
52 state->finished = True;
56 if (state->request.extra_len == 0) {
57 state->request.extra_data.data = NULL;
61 DEBUG(10, ("Need to read %d extra bytes\n", (int)state->request.extra_len));
63 state->request.extra_data.data =
64 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
66 if (state->request.extra_data.data == NULL) {
67 DEBUG(0, ("malloc failed\n"));
68 state->finished = True;
72 /* Ensure null termination */
73 state->request.extra_data.data[state->request.extra_len] = '\0';
75 len = read_data(state->sock, state->request.extra_data.data,
76 state->request.extra_len);
78 if (len != state->request.extra_len) {
79 DEBUG(0, ("Could not read extra data\n"));
80 state->finished = True;
86 * Machinery for async requests sent to children. You set up a
87 * winbindd_request, select a child to query, and issue a async_request
88 * call. When the request is completed, the callback function you specified is
89 * called back with the private pointer you gave to async_request.
92 struct winbindd_async_request {
93 struct winbindd_async_request *next, *prev;
95 struct winbindd_child *child;
96 struct winbindd_request *request;
97 struct winbindd_response *response;
98 void (*continuation)(void *private_data, BOOL success);
99 struct timed_event *reply_timeout_event;
100 pid_t child_pid; /* pid of the child we're waiting on. Used to detect
101 a restart of the child (child->pid != child_pid). */
105 static void async_main_request_sent(void *private_data, BOOL success);
106 static void async_request_sent(void *private_data, BOOL success);
107 static void async_reply_recv(void *private_data, BOOL success);
108 static void schedule_async_request(struct winbindd_child *child);
110 void async_request(TALLOC_CTX *mem_ctx, struct winbindd_child *child,
111 struct winbindd_request *request,
112 struct winbindd_response *response,
113 void (*continuation)(void *private_data, BOOL success),
116 struct winbindd_async_request *state;
118 SMB_ASSERT(continuation != NULL);
120 state = TALLOC_P(mem_ctx, struct winbindd_async_request);
123 DEBUG(0, ("talloc failed\n"));
124 continuation(private_data, False);
128 state->mem_ctx = mem_ctx;
129 state->child = child;
130 state->request = request;
131 state->response = response;
132 state->continuation = continuation;
133 state->private_data = private_data;
135 DLIST_ADD_END(child->requests, state, struct winbindd_async_request *);
137 schedule_async_request(child);
142 static void async_main_request_sent(void *private_data, BOOL success)
144 struct winbindd_async_request *state =
145 talloc_get_type_abort(private_data, struct winbindd_async_request);
148 DEBUG(5, ("Could not send async request\n"));
150 state->response->length = sizeof(struct winbindd_response);
151 state->response->result = WINBINDD_ERROR;
152 state->continuation(state->private_data, False);
156 if (state->request->extra_len == 0) {
157 async_request_sent(private_data, True);
161 setup_async_write(&state->child->event, state->request->extra_data.data,
162 state->request->extra_len,
163 async_request_sent, state);
166 /****************************************************************
167 Handler triggered if the child winbindd doesn't respond within
169 ****************************************************************/
171 static void async_request_timeout_handler(struct event_context *ctx,
172 struct timed_event *te,
173 const struct timeval *now,
176 struct winbindd_async_request *state =
177 talloc_get_type_abort(private_data, struct winbindd_async_request);
179 DEBUG(0,("async_request_timeout_handler: child pid %u is not responding. "
180 "Closing connection to it.\n",
183 /* Deal with the reply - set to error. */
184 async_reply_recv(private_data, False);
187 /**************************************************************
188 Common function called on both async send and recv fail.
189 Cleans up the child and schedules the next request.
190 **************************************************************/
192 static void async_request_fail(struct winbindd_async_request *state)
194 DLIST_REMOVE(state->child->requests, state);
196 TALLOC_FREE(state->reply_timeout_event);
198 SMB_ASSERT(state->child_pid != (pid_t)0);
200 /* If not already reaped, send kill signal to child. */
201 if (state->child->pid == state->child_pid) {
202 kill(state->child_pid, SIGTERM);
205 * Close the socket to the child.
207 winbind_child_died(state->child_pid);
210 state->response->length = sizeof(struct winbindd_response);
211 state->response->result = WINBINDD_ERROR;
212 state->continuation(state->private_data, False);
215 static void async_request_sent(void *private_data_data, BOOL success)
217 struct winbindd_async_request *state =
218 talloc_get_type_abort(private_data_data, struct winbindd_async_request);
221 DEBUG(5, ("Could not send async request to child pid %u\n",
222 (unsigned int)state->child_pid ));
223 async_request_fail(state);
227 /* Request successfully sent to the child, setup the wait for reply */
229 setup_async_read(&state->child->event,
230 &state->response->result,
231 sizeof(state->response->result),
232 async_reply_recv, state);
235 * Set up a timeout of 300 seconds for the response.
236 * If we don't get it close the child socket and
240 state->reply_timeout_event = event_add_timed(winbind_event_context(),
242 timeval_current_ofs(300,0),
243 "async_request_timeout",
244 async_request_timeout_handler,
246 if (!state->reply_timeout_event) {
247 smb_panic("async_request_sent: failed to add timeout handler.\n");
251 static void async_reply_recv(void *private_data, BOOL success)
253 struct winbindd_async_request *state =
254 talloc_get_type_abort(private_data, struct winbindd_async_request);
255 struct winbindd_child *child = state->child;
257 TALLOC_FREE(state->reply_timeout_event);
259 state->response->length = sizeof(struct winbindd_response);
262 DEBUG(5, ("Could not receive async reply from child pid %u\n",
263 (unsigned int)state->child_pid ));
265 cache_cleanup_response(state->child_pid);
266 async_request_fail(state);
270 SMB_ASSERT(cache_retrieve_response(state->child_pid,
273 cache_cleanup_response(state->child_pid);
275 DLIST_REMOVE(child->requests, state);
277 schedule_async_request(child);
279 state->continuation(state->private_data, True);
282 static BOOL fork_domain_child(struct winbindd_child *child);
284 static void schedule_async_request(struct winbindd_child *child)
286 struct winbindd_async_request *request = child->requests;
288 if (request == NULL) {
292 if (child->event.flags != 0) {
296 if ((child->pid == 0) && (!fork_domain_child(child))) {
297 /* Cancel all outstanding requests */
299 while (request != NULL) {
300 /* request might be free'd in the continuation */
301 struct winbindd_async_request *next = request->next;
302 request->continuation(request->private_data, False);
308 /* Now we know who we're sending to - remember the pid. */
309 request->child_pid = child->pid;
311 setup_async_write(&child->event, request->request,
312 sizeof(*request->request),
313 async_main_request_sent, request);
318 struct domain_request_state {
320 struct winbindd_domain *domain;
321 struct winbindd_request *request;
322 struct winbindd_response *response;
323 void (*continuation)(void *private_data_data, BOOL success);
324 void *private_data_data;
327 static void domain_init_recv(void *private_data_data, BOOL success);
329 void async_domain_request(TALLOC_CTX *mem_ctx,
330 struct winbindd_domain *domain,
331 struct winbindd_request *request,
332 struct winbindd_response *response,
333 void (*continuation)(void *private_data_data, BOOL success),
334 void *private_data_data)
336 struct domain_request_state *state;
338 if (domain->initialized) {
339 async_request(mem_ctx, &domain->child, request, response,
340 continuation, private_data_data);
344 state = TALLOC_P(mem_ctx, struct domain_request_state);
346 DEBUG(0, ("talloc failed\n"));
347 continuation(private_data_data, False);
351 state->mem_ctx = mem_ctx;
352 state->domain = domain;
353 state->request = request;
354 state->response = response;
355 state->continuation = continuation;
356 state->private_data_data = private_data_data;
358 init_child_connection(domain, domain_init_recv, state);
361 static void recvfrom_child(void *private_data_data, BOOL success)
363 struct winbindd_cli_state *state =
364 talloc_get_type_abort(private_data_data, struct winbindd_cli_state);
365 enum winbindd_result result = state->response.result;
367 /* This is an optimization: The child has written directly to the
368 * response buffer. The request itself is still in pending state,
369 * state that in the result code. */
371 state->response.result = WINBINDD_PENDING;
373 if ((!success) || (result != WINBINDD_OK)) {
374 request_error(state);
381 void sendto_child(struct winbindd_cli_state *state,
382 struct winbindd_child *child)
384 async_request(state->mem_ctx, child, &state->request,
385 &state->response, recvfrom_child, state);
388 void sendto_domain(struct winbindd_cli_state *state,
389 struct winbindd_domain *domain)
391 async_domain_request(state->mem_ctx, domain,
392 &state->request, &state->response,
393 recvfrom_child, state);
396 static void domain_init_recv(void *private_data_data, BOOL success)
398 struct domain_request_state *state =
399 talloc_get_type_abort(private_data_data, struct domain_request_state);
402 DEBUG(5, ("Domain init returned an error\n"));
403 state->continuation(state->private_data_data, False);
407 async_request(state->mem_ctx, &state->domain->child,
408 state->request, state->response,
409 state->continuation, state->private_data_data);
412 struct winbindd_child_dispatch_table {
413 enum winbindd_cmd cmd;
414 enum winbindd_result (*fn)(struct winbindd_domain *domain,
415 struct winbindd_cli_state *state);
416 const char *winbindd_cmd_name;
419 static struct winbindd_child_dispatch_table child_dispatch_table[] = {
421 { WINBINDD_LOOKUPSID, winbindd_dual_lookupsid, "LOOKUPSID" },
422 { WINBINDD_LOOKUPNAME, winbindd_dual_lookupname, "LOOKUPNAME" },
423 { WINBINDD_LOOKUPRIDS, winbindd_dual_lookuprids, "LOOKUPRIDS" },
424 { WINBINDD_LIST_TRUSTDOM, winbindd_dual_list_trusted_domains, "LIST_TRUSTDOM" },
425 { WINBINDD_INIT_CONNECTION, winbindd_dual_init_connection, "INIT_CONNECTION" },
426 { WINBINDD_GETDCNAME, winbindd_dual_getdcname, "GETDCNAME" },
427 { WINBINDD_SHOW_SEQUENCE, winbindd_dual_show_sequence, "SHOW_SEQUENCE" },
428 { WINBINDD_PAM_AUTH, winbindd_dual_pam_auth, "PAM_AUTH" },
429 { WINBINDD_PAM_AUTH_CRAP, winbindd_dual_pam_auth_crap, "AUTH_CRAP" },
430 { WINBINDD_PAM_LOGOFF, winbindd_dual_pam_logoff, "PAM_LOGOFF" },
431 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP,winbindd_dual_pam_chng_pswd_auth_crap,"CHNG_PSWD_AUTH_CRAP" },
432 { WINBINDD_PAM_CHAUTHTOK, winbindd_dual_pam_chauthtok, "PAM_CHAUTHTOK" },
433 { WINBINDD_CHECK_MACHACC, winbindd_dual_check_machine_acct, "CHECK_MACHACC" },
434 { WINBINDD_DUAL_SID2UID, winbindd_dual_sid2uid, "DUAL_SID2UID" },
435 { WINBINDD_DUAL_SID2GID, winbindd_dual_sid2gid, "DUAL_SID2GID" },
436 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
437 { WINBINDD_DUAL_SIDS2XIDS, winbindd_dual_sids2xids, "DUAL_SIDS2XIDS" },
438 #endif /* end DISABLED */
439 { WINBINDD_DUAL_UID2SID, winbindd_dual_uid2sid, "DUAL_UID2SID" },
440 { WINBINDD_DUAL_GID2SID, winbindd_dual_gid2sid, "DUAL_GID2SID" },
441 { WINBINDD_DUAL_UID2NAME, winbindd_dual_uid2name, "DUAL_UID2NAME" },
442 { WINBINDD_DUAL_NAME2UID, winbindd_dual_name2uid, "DUAL_NAME2UID" },
443 { WINBINDD_DUAL_GID2NAME, winbindd_dual_gid2name, "DUAL_GID2NAME" },
444 { WINBINDD_DUAL_NAME2GID, winbindd_dual_name2gid, "DUAL_NAME2GID" },
445 { WINBINDD_DUAL_SET_MAPPING, winbindd_dual_set_mapping, "DUAL_SET_MAPPING" },
446 { WINBINDD_DUAL_SET_HWM, winbindd_dual_set_hwm, "DUAL_SET_HWMS" },
447 { WINBINDD_DUAL_DUMP_MAPS, winbindd_dual_dump_maps, "DUAL_DUMP_MAPS" },
448 { WINBINDD_DUAL_USERINFO, winbindd_dual_userinfo, "DUAL_USERINFO" },
449 { WINBINDD_ALLOCATE_UID, winbindd_dual_allocate_uid, "ALLOCATE_UID" },
450 { WINBINDD_ALLOCATE_GID, winbindd_dual_allocate_gid, "ALLOCATE_GID" },
451 { WINBINDD_GETUSERDOMGROUPS, winbindd_dual_getuserdomgroups, "GETUSERDOMGROUPS" },
452 { WINBINDD_DUAL_GETSIDALIASES, winbindd_dual_getsidaliases, "GETSIDALIASES" },
453 { WINBINDD_CCACHE_NTLMAUTH, winbindd_dual_ccache_ntlm_auth, "CCACHE_NTLM_AUTH" },
456 { WINBINDD_NUM_CMDS, NULL, "NONE" }
459 static void child_process_request(struct winbindd_domain *domain,
460 struct winbindd_cli_state *state)
462 struct winbindd_child_dispatch_table *table;
464 /* Free response data - we may be interrupted and receive another
465 command before being able to send this data off. */
467 state->response.result = WINBINDD_ERROR;
468 state->response.length = sizeof(struct winbindd_response);
470 state->mem_ctx = talloc_init("winbind request");
471 if (state->mem_ctx == NULL)
474 /* Process command */
476 for (table = child_dispatch_table; table->fn; table++) {
477 if (state->request.cmd == table->cmd) {
478 DEBUG(10,("process_request: request fn %s\n",
479 table->winbindd_cmd_name ));
480 state->response.result = table->fn(domain, state);
486 DEBUG(10,("process_request: unknown request fn number %d\n",
487 (int)state->request.cmd ));
488 state->response.result = WINBINDD_ERROR;
491 talloc_destroy(state->mem_ctx);
494 void setup_domain_child(struct winbindd_domain *domain,
495 struct winbindd_child *child,
496 const char *explicit_logfile)
498 if (explicit_logfile != NULL) {
499 pstr_sprintf(child->logfilename, "%s/log.winbindd-%s",
500 dyn_LOGFILEBASE, explicit_logfile);
501 } else if (domain != NULL) {
502 pstr_sprintf(child->logfilename, "%s/log.wb-%s",
503 dyn_LOGFILEBASE, domain->name);
505 smb_panic("Internal error: domain == NULL && "
506 "explicit_logfile == NULL");
509 child->domain = domain;
512 struct winbindd_child *children = NULL;
514 void winbind_child_died(pid_t pid)
516 struct winbindd_child *child;
518 for (child = children; child != NULL; child = child->next) {
519 if (child->pid == pid) {
525 DEBUG(5, ("Already reaped child %u died\n", (unsigned int)pid));
529 /* This will be re-added in fork_domain_child() */
531 DLIST_REMOVE(children, child);
533 remove_fd_event(&child->event);
534 close(child->event.fd);
536 child->event.flags = 0;
539 schedule_async_request(child);
542 /* Ensure any negative cache entries with the netbios or realm names are removed. */
544 void winbindd_flush_negative_conn_cache(struct winbindd_domain *domain)
546 flush_negative_conn_cache_for_domain(domain->name);
547 if (*domain->alt_name) {
548 flush_negative_conn_cache_for_domain(domain->alt_name);
552 /* Set our domains as offline and forward the offline message to our children. */
554 void winbind_msg_offline(int msg_type, struct process_id src,
555 void *buf, size_t len, void *private_data)
557 struct winbindd_child *child;
558 struct winbindd_domain *domain;
560 DEBUG(10,("winbind_msg_offline: got offline message.\n"));
562 if (!lp_winbind_offline_logon()) {
563 DEBUG(10,("winbind_msg_offline: rejecting offline message.\n"));
567 /* Set our global state as offline. */
568 if (!set_global_winbindd_state_offline()) {
569 DEBUG(10,("winbind_msg_offline: offline request failed.\n"));
573 /* Set all our domains as offline. */
574 for (domain = domain_list(); domain; domain = domain->next) {
575 if (domain->internal) {
578 DEBUG(5,("winbind_msg_offline: marking %s offline.\n", domain->name));
579 set_domain_offline(domain);
581 /* Send an offline message to the idmap child when our
582 primary domain goes offline */
584 if ( domain->primary ) {
585 struct winbindd_child *idmap = idmap_child();
587 if ( idmap->pid != 0 ) {
588 message_send_pid(pid_to_procid(idmap->pid),
591 strlen(domain->name)+1,
597 for (child = children; child != NULL; child = child->next) {
598 /* Don't send message to idmap child. We've already
600 if (!child->domain || (child == idmap_child())) {
604 /* Or internal domains (this should not be possible....) */
605 if (child->domain->internal) {
609 /* Each winbindd child should only process requests for one domain - make sure
610 we only set it online / offline for that domain. */
612 DEBUG(10,("winbind_msg_offline: sending message to pid %u for domain %s.\n",
613 (unsigned int)child->pid, domain->name ));
615 message_send_pid(pid_to_procid(child->pid), MSG_WINBIND_OFFLINE, child->domain->name,
616 strlen(child->domain->name)+1, False);
620 /* Set our domains as online and forward the online message to our children. */
622 void winbind_msg_online(int msg_type, struct process_id src,
623 void *buf, size_t len, void *private_data)
625 struct winbindd_child *child;
626 struct winbindd_domain *domain;
628 DEBUG(10,("winbind_msg_online: got online message.\n"));
630 if (!lp_winbind_offline_logon()) {
631 DEBUG(10,("winbind_msg_online: rejecting online message.\n"));
635 /* Set our global state as online. */
636 set_global_winbindd_state_online();
638 smb_nscd_flush_user_cache();
639 smb_nscd_flush_group_cache();
641 /* Set all our domains as online. */
642 for (domain = domain_list(); domain; domain = domain->next) {
643 if (domain->internal) {
646 DEBUG(5,("winbind_msg_online: requesting %s to go online.\n", domain->name));
648 winbindd_flush_negative_conn_cache(domain);
649 set_domain_online_request(domain);
651 /* Send an online message to the idmap child when our
652 primary domain comes back online */
654 if ( domain->primary ) {
655 struct winbindd_child *idmap = idmap_child();
657 if ( idmap->pid != 0 ) {
658 message_send_pid(pid_to_procid(idmap->pid),
661 strlen(domain->name)+1,
668 for (child = children; child != NULL; child = child->next) {
669 /* Don't send message to idmap child. */
670 if (!child->domain || (child == idmap_child())) {
674 /* Or internal domains (this should not be possible....) */
675 if (child->domain->internal) {
679 /* Each winbindd child should only process requests for one domain - make sure
680 we only set it online / offline for that domain. */
682 DEBUG(10,("winbind_msg_online: sending message to pid %u for domain %s.\n",
683 (unsigned int)child->pid, child->domain->name ));
685 message_send_pid(pid_to_procid(child->pid), MSG_WINBIND_ONLINE, child->domain->name,
686 strlen(child->domain->name)+1, False);
690 /* Forward the online/offline messages to our children. */
691 void winbind_msg_onlinestatus(int msg_type, struct process_id src,
692 void *buf, size_t len, void *private_data)
694 struct winbindd_child *child;
696 DEBUG(10,("winbind_msg_onlinestatus: got onlinestatus message.\n"));
698 for (child = children; child != NULL; child = child->next) {
699 if (child->domain && child->domain->primary) {
700 DEBUG(10,("winbind_msg_onlinestatus: "
701 "sending message to pid %u of primary domain.\n",
702 (unsigned int)child->pid));
703 message_send_pid(pid_to_procid(child->pid),
704 MSG_WINBIND_ONLINESTATUS, buf, len, False);
711 static void account_lockout_policy_handler(struct event_context *ctx,
712 struct timed_event *te,
713 const struct timeval *now,
716 struct winbindd_child *child =
717 (struct winbindd_child *)private_data;
718 TALLOC_CTX *mem_ctx = NULL;
719 struct winbindd_methods *methods;
720 SAM_UNK_INFO_12 lockout_policy;
723 DEBUG(10,("account_lockout_policy_handler called\n"));
725 TALLOC_FREE(child->lockout_policy_event);
727 methods = child->domain->methods;
729 mem_ctx = talloc_init("account_lockout_policy_handler ctx");
731 result = NT_STATUS_NO_MEMORY;
733 result = methods->lockout_policy(child->domain, mem_ctx, &lockout_policy);
736 talloc_destroy(mem_ctx);
738 if (!NT_STATUS_IS_OK(result)) {
739 DEBUG(10,("account_lockout_policy_handler: lockout_policy failed error %s\n",
743 child->lockout_policy_event = event_add_timed(winbind_event_context(), NULL,
744 timeval_current_ofs(3600, 0),
745 "account_lockout_policy_handler",
746 account_lockout_policy_handler,
750 /* Deal with a request to go offline. */
752 static void child_msg_offline(int msg_type, struct process_id src,
753 void *buf, size_t len, void *private_data)
755 struct winbindd_domain *domain;
756 const char *domainname = (const char *)buf;
758 if (buf == NULL || len == 0) {
762 DEBUG(5,("child_msg_offline received for domain %s.\n", domainname));
764 if (!lp_winbind_offline_logon()) {
765 DEBUG(10,("child_msg_offline: rejecting offline message.\n"));
769 /* Set our global state as offline. */
770 if (!set_global_winbindd_state_offline()) {
771 DEBUG(10,("child_msg_offline: offline request failed.\n"));
775 /* Mark the requested domain offline. */
777 for (domain = domain_list(); domain; domain = domain->next) {
778 if (domain->internal) {
781 if (strequal(domain->name, domainname)) {
782 DEBUG(5,("child_msg_offline: marking %s offline.\n", domain->name));
783 set_domain_offline(domain);
788 /* Deal with a request to go online. */
790 static void child_msg_online(int msg_type, struct process_id src,
791 void *buf, size_t len, void *private_data)
793 struct winbindd_domain *domain;
794 const char *domainname = (const char *)buf;
796 if (buf == NULL || len == 0) {
800 DEBUG(5,("child_msg_online received for domain %s.\n", domainname));
802 if (!lp_winbind_offline_logon()) {
803 DEBUG(10,("child_msg_online: rejecting online message.\n"));
807 /* Set our global state as online. */
808 set_global_winbindd_state_online();
810 /* Try and mark everything online - delete any negative cache entries
811 to force a reconnect now. */
813 for (domain = domain_list(); domain; domain = domain->next) {
814 if (domain->internal) {
817 if (strequal(domain->name, domainname)) {
818 DEBUG(5,("child_msg_online: requesting %s to go online.\n", domain->name));
819 winbindd_flush_negative_conn_cache(domain);
820 set_domain_online_request(domain);
825 static const char *collect_onlinestatus(TALLOC_CTX *mem_ctx)
827 struct winbindd_domain *domain;
830 if ((buf = talloc_asprintf(mem_ctx, "global:%s ",
831 get_global_winbindd_state_offline() ?
832 "Offline":"Online")) == NULL) {
836 for (domain = domain_list(); domain; domain = domain->next) {
837 if ((buf = talloc_asprintf_append(buf, "%s:%s ",
840 "Online":"Offline")) == NULL) {
845 buf = talloc_asprintf_append(buf, "\n");
847 DEBUG(5,("collect_onlinestatus: %s", buf));
852 static void child_msg_onlinestatus(int msg_type, struct process_id src,
853 void *buf, size_t len, void *private_data)
857 struct process_id *sender;
859 DEBUG(5,("winbind_msg_onlinestatus received.\n"));
865 sender = (struct process_id *)buf;
867 mem_ctx = talloc_init("winbind_msg_onlinestatus");
868 if (mem_ctx == NULL) {
872 message = collect_onlinestatus(mem_ctx);
873 if (message == NULL) {
874 talloc_destroy(mem_ctx);
878 message_send_pid(*sender, MSG_WINBIND_ONLINESTATUS,
879 message, strlen(message) + 1, True);
881 talloc_destroy(mem_ctx);
884 static BOOL fork_domain_child(struct winbindd_child *child)
887 struct winbindd_cli_state state;
888 struct winbindd_domain *domain;
889 struct winbindd_domain *primary_domain = NULL;
891 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fdpair) != 0) {
892 DEBUG(0, ("Could not open child pipe: %s\n",
898 state.pid = sys_getpid();
900 /* Ensure we don't process messages whilst we're
901 changing the disposition for the child. */
904 child->pid = sys_fork();
906 if (child->pid == -1) {
907 DEBUG(0, ("Could not fork: %s\n", strerror(errno)));
912 if (child->pid != 0) {
915 child->next = child->prev = NULL;
916 DLIST_ADD(children, child);
917 child->event.fd = fdpair[1];
918 child->event.flags = 0;
919 child->requests = NULL;
920 add_fd_event(&child->event);
921 /* We're ok with online/offline messages now. */
928 /* Stop zombies in children */
931 state.sock = fdpair[0];
934 /* tdb needs special fork handling */
935 if (tdb_reopen_all(1) == -1) {
936 DEBUG(0,("tdb_reopen_all failed.\n"));
940 close_conns_after_fork();
942 if (!override_logfile) {
943 lp_set_logfile(child->logfilename);
947 /* Don't handle the same messages as our parent. */
948 message_deregister(MSG_SMB_CONF_UPDATED);
949 message_deregister(MSG_SHUTDOWN);
950 message_deregister(MSG_WINBIND_OFFLINE);
951 message_deregister(MSG_WINBIND_ONLINE);
952 message_deregister(MSG_WINBIND_ONLINESTATUS);
954 /* The child is ok with online/offline messages now. */
957 /* Handle online/offline messages. */
958 message_register(MSG_WINBIND_OFFLINE, child_msg_offline, NULL);
959 message_register(MSG_WINBIND_ONLINE, child_msg_online, NULL);
960 message_register(MSG_WINBIND_ONLINESTATUS, child_msg_onlinestatus,
963 if ( child->domain ) {
964 child->domain->startup = True;
965 child->domain->startup_time = time(NULL);
968 /* Ensure we have no pending check_online events other
969 than one for this domain or the primary domain. */
971 for (domain = domain_list(); domain; domain = domain->next) {
972 if (domain->primary) {
973 primary_domain = domain;
975 if ((domain != child->domain) && !domain->primary) {
976 TALLOC_FREE(domain->check_online_event);
980 /* Ensure we're not handling an event inherited from
983 cancel_named_event(winbind_event_context(),
984 "krb5_ticket_refresh_handler");
986 /* We might be in the idmap child...*/
987 if (child->domain && !(child->domain->internal) &&
988 lp_winbind_offline_logon()) {
990 set_domain_online_request(child->domain);
992 if (primary_domain != child->domain) {
993 /* We need to talk to the primary
994 * domain as well as the trusted
995 * domain inside a trusted domain
998 * winbindd_dual_pam_auth_samlogon()
999 * especially the calling of
1000 * contact_domain = find_our_domain()
1001 * in the non-DC case for details.
1003 set_domain_online_request(primary_domain);
1006 child->lockout_policy_event = event_add_timed(
1007 winbind_event_context(), NULL, timeval_zero(),
1008 "account_lockout_policy_handler",
1009 account_lockout_policy_handler,
1021 /* free up any talloc memory */
1023 main_loop_TALLOC_FREE();
1025 /* check for signals */
1026 winbind_check_sigterm(false);
1027 winbind_check_sighup(override_logfile ? NULL :
1028 child->logfilename);
1030 run_events(winbind_event_context(), 0, NULL, NULL);
1034 if (child->domain && child->domain->startup &&
1035 (now.tv_sec > child->domain->startup_time + 30)) {
1036 /* No longer in "startup" mode. */
1037 DEBUG(10,("fork_domain_child: domain %s no longer in 'startup' mode.\n",
1038 child->domain->name ));
1039 child->domain->startup = False;
1042 tp = get_timed_events_timeout(winbind_event_context(), &t);
1044 DEBUG(11,("select will use timeout of %u.%u seconds\n",
1045 (unsigned int)tp->tv_sec, (unsigned int)tp->tv_usec ));
1048 /* Handle messages */
1053 FD_SET(state.sock, &read_fds);
1055 ret = sys_select(state.sock + 1, &read_fds, NULL, NULL, tp);
1058 DEBUG(11,("nothing is ready yet, continue\n"));
1062 if (ret == -1 && errno == EINTR) {
1063 /* We got a signal - continue. */
1067 if (ret == -1 && errno != EINTR) {
1068 DEBUG(0,("select error occured\n"));
1073 /* fetch a request from the main daemon */
1074 child_read_request(&state);
1076 if (state.finished) {
1077 /* we lost contact with our parent */
1081 DEBUG(4,("child daemon request %d\n", (int)state.request.cmd));
1083 ZERO_STRUCT(state.response);
1084 state.request.null_term = '\0';
1085 child_process_request(child->domain, &state);
1087 SAFE_FREE(state.request.extra_data.data);
1089 cache_store_response(sys_getpid(), &state.response);
1091 SAFE_FREE(state.response.extra_data.data);
1093 /* We just send the result code back, the result
1094 * structure needs to be fetched via the
1095 * winbindd_cache. Hmm. That needs fixing... */
1097 if (write_data(state.sock,
1098 (const char *)&state.response.result,
1099 sizeof(state.response.result)) !=
1100 sizeof(state.response.result)) {
1101 DEBUG(0, ("Could not write result\n"));