2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000-2002
7 Copyright (C) Andrew Tridgell 2002
8 Copyright (C) Jelmer Vernooij 2003
9 Copyright (C) Volker Lendecke 2004
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../../nsswitch/libwbclient/wbc_async.h"
30 #define DBGC_CLASS DBGC_WINBIND
32 static void remove_client(struct winbindd_cli_state
*state
);
34 static bool opt_nocache
= False
;
35 static bool interactive
= False
;
37 extern bool override_logfile
;
39 struct event_context
*winbind_event_context(void)
41 static struct event_context
*ctx
;
43 if (!ctx
&& !(ctx
= event_context_init(NULL
))) {
44 smb_panic("Could not init winbind event context");
49 struct messaging_context
*winbind_messaging_context(void)
51 static struct messaging_context
*ctx
;
54 ctx
= messaging_init(NULL
, server_id_self(),
55 winbind_event_context());
58 DEBUG(0, ("Could not init winbind messaging context.\n"));
63 /* Reload configuration */
65 static bool reload_services_file(const char *lfile
)
70 char *fname
= lp_configfile();
72 if (file_exist(fname
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
73 set_dyn_CONFIGFILE(fname
);
78 /* if this is a child, restore the logfile to the special
79 name - <domain>, idmap, etc. */
80 if (lfile
&& *lfile
) {
81 lp_set_logfile(lfile
);
85 ret
= lp_load(get_dyn_CONFIGFILE(),False
,False
,True
,True
);
94 /**************************************************************************** **
96 **************************************************************************** */
98 static void fault_quit(void)
103 static void winbindd_status(void)
105 struct winbindd_cli_state
*tmp
;
107 DEBUG(0, ("winbindd status:\n"));
109 /* Print client state information */
111 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
113 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
114 DEBUG(2, ("\tclient list:\n"));
115 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
116 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
117 (unsigned long)tmp
->pid
, tmp
->sock
));
122 /* Print winbindd status to log file */
124 static void print_winbindd_status(void)
129 /* Flush client cache */
131 static void flush_caches(void)
133 /* We need to invalidate cached user list entries on a SIGHUP
134 otherwise cached access denied errors due to restrict anonymous
135 hang around until the sequence number changes. */
137 if (!wcache_invalidate_cache()) {
138 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
139 if (!winbindd_cache_validate_and_initialize()) {
145 /* Handle the signal by unlinking socket and exiting */
147 static void terminate(bool is_parent
)
150 /* When parent goes away we should
151 * remove the socket file. Not so
152 * when children terminate.
156 if (asprintf(&path
, "%s/%s",
157 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
165 trustdom_cache_shutdown();
167 gencache_stabilize();
171 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
172 char *description
= talloc_describe_all(mem_ctx
);
174 DEBUG(3, ("tallocs left:\n%s\n", description
));
175 talloc_destroy(mem_ctx
);
186 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
187 struct tevent_signal
*se
,
193 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
195 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
196 signum
, (int)*is_parent
));
197 terminate(*is_parent
);
200 bool winbindd_setup_sig_term_handler(bool parent
)
202 struct tevent_signal
*se
;
205 is_parent
= talloc(winbind_event_context(), bool);
212 se
= tevent_add_signal(winbind_event_context(),
215 winbindd_sig_term_handler
,
218 DEBUG(0,("failed to setup SIGTERM handler"));
219 talloc_free(is_parent
);
223 se
= tevent_add_signal(winbind_event_context(),
226 winbindd_sig_term_handler
,
229 DEBUG(0,("failed to setup SIGINT handler"));
230 talloc_free(is_parent
);
234 se
= tevent_add_signal(winbind_event_context(),
237 winbindd_sig_term_handler
,
240 DEBUG(0,("failed to setup SIGINT handler"));
241 talloc_free(is_parent
);
248 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
249 struct tevent_signal
*se
,
255 const char *file
= (const char *)private_data
;
257 DEBUG(1,("Reloading services after SIGHUP\n"));
259 reload_services_file(file
);
262 bool winbindd_setup_sig_hup_handler(const char *lfile
)
264 struct tevent_signal
*se
;
268 file
= talloc_strdup(winbind_event_context(),
275 se
= tevent_add_signal(winbind_event_context(),
276 winbind_event_context(),
278 winbindd_sig_hup_handler
,
287 static void winbindd_sig_chld_handler(struct tevent_context
*ev
,
288 struct tevent_signal
*se
,
296 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
297 winbind_child_died(pid
);
301 static bool winbindd_setup_sig_chld_handler(void)
303 struct tevent_signal
*se
;
305 se
= tevent_add_signal(winbind_event_context(),
306 winbind_event_context(),
308 winbindd_sig_chld_handler
,
317 static void winbindd_sig_usr2_handler(struct tevent_context
*ev
,
318 struct tevent_signal
*se
,
324 print_winbindd_status();
327 static bool winbindd_setup_sig_usr2_handler(void)
329 struct tevent_signal
*se
;
331 se
= tevent_add_signal(winbind_event_context(),
332 winbind_event_context(),
334 winbindd_sig_usr2_handler
,
343 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
344 static void msg_reload_services(struct messaging_context
*msg
,
347 struct server_id server_id
,
350 /* Flush various caches */
352 reload_services_file((const char *) private_data
);
355 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
356 static void msg_shutdown(struct messaging_context
*msg
,
359 struct server_id server_id
,
362 /* only the parent waits for this message */
363 DEBUG(0,("Got shutdown message\n"));
368 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
371 struct server_id server_id
,
377 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
381 * call the validation code from a child:
382 * so we don't block the main winbindd and the validation
383 * code can safely use fork/waitpid...
385 child_pid
= sys_fork();
387 if (child_pid
== -1) {
388 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
393 if (child_pid
!= 0) {
395 DEBUG(5, ("winbind_msg_validate_cache: child created with "
396 "pid %d.\n", (int)child_pid
));
402 if (!winbindd_reinit_after_fork(NULL
)) {
406 /* install default SIGCHLD handler: validation code uses fork/waitpid */
407 CatchSignal(SIGCHLD
, SIG_DFL
);
409 ret
= (uint8
)winbindd_validate_cache_nobackup();
410 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
411 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
416 static struct winbindd_dispatch_table
{
417 enum winbindd_cmd cmd
;
418 void (*fn
)(struct winbindd_cli_state
*state
);
419 const char *winbindd_cmd_name
;
420 } dispatch_table
[] = {
422 /* PAM auth functions */
424 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
425 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
426 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
427 { WINBINDD_PAM_LOGOFF
, winbindd_pam_logoff
, "PAM_LOGOFF" },
428 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, winbindd_pam_chng_pswd_auth_crap
, "CHNG_PSWD_AUTH_CRAP" },
430 /* Enumeration functions */
432 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
437 { WINBINDD_INFO
, winbindd_info
, "INFO" },
438 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
439 "INTERFACE_VERSION" },
440 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
441 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
442 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
443 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
444 "WINBINDD_PRIV_PIPE_DIR" },
446 /* Credential cache access */
447 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
448 { WINBINDD_CCACHE_SAVE
, winbindd_ccache_save
, "CCACHE_SAVE" },
452 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
453 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
457 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
460 struct winbindd_async_dispatch_table
{
461 enum winbindd_cmd cmd
;
462 const char *cmd_name
;
463 struct tevent_req
*(*send_req
)(TALLOC_CTX
*mem_ctx
,
464 struct tevent_context
*ev
,
465 struct winbindd_cli_state
*cli
,
466 struct winbindd_request
*request
);
467 NTSTATUS (*recv_req
)(struct tevent_req
*req
,
468 struct winbindd_response
*presp
);
471 static struct winbindd_async_dispatch_table async_nonpriv_table
[] = {
472 { WINBINDD_PING
, "PING",
473 wb_ping_send
, wb_ping_recv
},
474 { WINBINDD_LOOKUPSID
, "LOOKUPSID",
475 winbindd_lookupsid_send
, winbindd_lookupsid_recv
},
476 { WINBINDD_LOOKUPNAME
, "LOOKUPNAME",
477 winbindd_lookupname_send
, winbindd_lookupname_recv
},
478 { WINBINDD_SID_TO_UID
, "SID_TO_UID",
479 winbindd_sid_to_uid_send
, winbindd_sid_to_uid_recv
},
480 { WINBINDD_SID_TO_GID
, "SID_TO_GID",
481 winbindd_sid_to_gid_send
, winbindd_sid_to_gid_recv
},
482 { WINBINDD_UID_TO_SID
, "UID_TO_SID",
483 winbindd_uid_to_sid_send
, winbindd_uid_to_sid_recv
},
484 { WINBINDD_GID_TO_SID
, "GID_TO_SID",
485 winbindd_gid_to_sid_send
, winbindd_gid_to_sid_recv
},
486 { WINBINDD_GETPWSID
, "GETPWSID",
487 winbindd_getpwsid_send
, winbindd_getpwsid_recv
},
488 { WINBINDD_GETPWNAM
, "GETPWNAM",
489 winbindd_getpwnam_send
, winbindd_getpwnam_recv
},
490 { WINBINDD_GETPWUID
, "GETPWUID",
491 winbindd_getpwuid_send
, winbindd_getpwuid_recv
},
492 { WINBINDD_GETSIDALIASES
, "GETSIDALIASES",
493 winbindd_getsidaliases_send
, winbindd_getsidaliases_recv
},
494 { WINBINDD_GETUSERDOMGROUPS
, "GETUSERDOMGROUPS",
495 winbindd_getuserdomgroups_send
, winbindd_getuserdomgroups_recv
},
496 { WINBINDD_GETGROUPS
, "GETGROUPS",
497 winbindd_getgroups_send
, winbindd_getgroups_recv
},
498 { WINBINDD_SHOW_SEQUENCE
, "SHOW_SEQUENCE",
499 winbindd_show_sequence_send
, winbindd_show_sequence_recv
},
500 { WINBINDD_GETGRGID
, "GETGRGID",
501 winbindd_getgrgid_send
, winbindd_getgrgid_recv
},
502 { WINBINDD_GETGRNAM
, "GETGRNAM",
503 winbindd_getgrnam_send
, winbindd_getgrnam_recv
},
504 { WINBINDD_GETUSERSIDS
, "GETUSERSIDS",
505 winbindd_getusersids_send
, winbindd_getusersids_recv
},
506 { WINBINDD_LOOKUPRIDS
, "LOOKUPRIDS",
507 winbindd_lookuprids_send
, winbindd_lookuprids_recv
},
508 { WINBINDD_SETPWENT
, "SETPWENT",
509 winbindd_setpwent_send
, winbindd_setpwent_recv
},
510 { WINBINDD_GETPWENT
, "GETPWENT",
511 winbindd_getpwent_send
, winbindd_getpwent_recv
},
512 { WINBINDD_ENDPWENT
, "ENDPWENT",
513 winbindd_endpwent_send
, winbindd_endpwent_recv
},
514 { WINBINDD_DSGETDCNAME
, "DSGETDCNAME",
515 winbindd_dsgetdcname_send
, winbindd_dsgetdcname_recv
},
516 { WINBINDD_GETDCNAME
, "GETDCNAME",
517 winbindd_getdcname_send
, winbindd_getdcname_recv
},
518 { WINBINDD_SETGRENT
, "SETGRENT",
519 winbindd_setgrent_send
, winbindd_setgrent_recv
},
520 { WINBINDD_GETGRENT
, "GETGRENT",
521 winbindd_getgrent_send
, winbindd_getgrent_recv
},
522 { WINBINDD_ENDGRENT
, "ENDGRENT",
523 winbindd_endgrent_send
, winbindd_endgrent_recv
},
524 { WINBINDD_LIST_USERS
, "LIST_USERS",
525 winbindd_list_users_send
, winbindd_list_users_recv
},
526 { WINBINDD_LIST_GROUPS
, "LIST_GROUPS",
527 winbindd_list_groups_send
, winbindd_list_groups_recv
},
528 { WINBINDD_CHECK_MACHACC
, "CHECK_MACHACC",
529 winbindd_check_machine_acct_send
, winbindd_check_machine_acct_recv
},
530 { WINBINDD_PING_DC
, "PING_DC",
531 winbindd_ping_dc_send
, winbindd_ping_dc_recv
},
533 { 0, NULL
, NULL
, NULL
}
536 static struct winbindd_async_dispatch_table async_priv_table
[] = {
537 { WINBINDD_ALLOCATE_UID
, "ALLOCATE_UID",
538 winbindd_allocate_uid_send
, winbindd_allocate_uid_recv
},
539 { WINBINDD_ALLOCATE_GID
, "ALLOCATE_GID",
540 winbindd_allocate_gid_send
, winbindd_allocate_gid_recv
},
541 { WINBINDD_SET_MAPPING
, "SET_MAPPING",
542 winbindd_set_mapping_send
, winbindd_set_mapping_recv
},
543 { WINBINDD_REMOVE_MAPPING
, "SET_MAPPING",
544 winbindd_remove_mapping_send
, winbindd_remove_mapping_recv
},
545 { WINBINDD_SET_HWM
, "SET_HWM",
546 winbindd_set_hwm_send
, winbindd_set_hwm_recv
},
547 { WINBINDD_CHANGE_MACHACC
, "CHANGE_MACHACC",
548 winbindd_change_machine_acct_send
, winbindd_change_machine_acct_recv
},
550 { 0, NULL
, NULL
, NULL
}
553 static void wb_request_done(struct tevent_req
*req
);
555 static void process_request(struct winbindd_cli_state
*state
)
557 struct winbindd_dispatch_table
*table
= dispatch_table
;
558 struct winbindd_async_dispatch_table
*atable
;
560 state
->mem_ctx
= talloc_init("winbind request");
561 if (state
->mem_ctx
== NULL
)
564 /* Remember who asked us. */
565 state
->pid
= state
->request
->pid
;
567 state
->cmd_name
= "unknown request";
568 state
->recv_fn
= NULL
;
570 /* Process command */
572 for (atable
= async_nonpriv_table
; atable
->send_req
; atable
+= 1) {
573 if (state
->request
->cmd
== atable
->cmd
) {
578 if ((atable
->send_req
== NULL
) && state
->privileged
) {
579 for (atable
= async_priv_table
; atable
->send_req
;
581 if (state
->request
->cmd
== atable
->cmd
) {
587 if (atable
->send_req
!= NULL
) {
588 struct tevent_req
*req
;
590 state
->cmd_name
= atable
->cmd_name
;
591 state
->recv_fn
= atable
->recv_req
;
593 DEBUG(10, ("process_request: Handling async request %d:%s\n",
594 (int)state
->pid
, state
->cmd_name
));
596 req
= atable
->send_req(state
->mem_ctx
, winbind_event_context(),
597 state
, state
->request
);
599 DEBUG(0, ("process_request: atable->send failed for "
600 "%s\n", atable
->cmd_name
));
601 request_error(state
);
604 tevent_req_set_callback(req
, wb_request_done
, state
);
608 state
->response
= talloc_zero(state
->mem_ctx
,
609 struct winbindd_response
);
610 if (state
->response
== NULL
) {
611 DEBUG(10, ("talloc failed\n"));
612 remove_client(state
);
615 state
->response
->result
= WINBINDD_PENDING
;
616 state
->response
->length
= sizeof(struct winbindd_response
);
618 for (table
= dispatch_table
; table
->fn
; table
++) {
619 if (state
->request
->cmd
== table
->cmd
) {
620 DEBUG(10,("process_request: request fn %s\n",
621 table
->winbindd_cmd_name
));
622 state
->cmd_name
= table
->winbindd_cmd_name
;
629 DEBUG(10,("process_request: unknown request fn number %d\n",
630 (int)state
->request
->cmd
));
631 request_error(state
);
635 static void wb_request_done(struct tevent_req
*req
)
637 struct winbindd_cli_state
*state
= tevent_req_callback_data(
638 req
, struct winbindd_cli_state
);
641 state
->response
= talloc_zero(state
->mem_ctx
,
642 struct winbindd_response
);
643 if (state
->response
== NULL
) {
644 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
645 (int)state
->pid
, state
->cmd_name
));
646 remove_client(state
);
649 state
->response
->result
= WINBINDD_PENDING
;
650 state
->response
->length
= sizeof(struct winbindd_response
);
652 status
= state
->recv_fn(req
, state
->response
);
655 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
656 (int)state
->pid
, state
->cmd_name
, nt_errstr(status
)));
658 if (!NT_STATUS_IS_OK(status
)) {
659 request_error(state
);
666 * This is the main event loop of winbind requests. It goes through a
667 * state-machine of 3 read/write requests, 4 if you have extra data to send.
669 * An idle winbind client has a read request of 4 bytes outstanding,
670 * finalizing function is request_len_recv, checking the length. request_recv
671 * then processes the packet. The processing function then at some point has
672 * to call request_finished which schedules sending the response.
675 static void request_finished(struct winbindd_cli_state
*state
);
677 static void winbind_client_request_read(struct tevent_req
*req
);
678 static void winbind_client_response_written(struct tevent_req
*req
);
680 static void request_finished(struct winbindd_cli_state
*state
)
682 struct tevent_req
*req
;
684 TALLOC_FREE(state
->request
);
686 req
= wb_resp_write_send(state
, winbind_event_context(),
687 state
->out_queue
, state
->sock
,
690 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
691 (int)state
->pid
, state
->cmd_name
));
692 remove_client(state
);
695 tevent_req_set_callback(req
, winbind_client_response_written
, state
);
698 static void winbind_client_response_written(struct tevent_req
*req
)
700 struct winbindd_cli_state
*state
= tevent_req_callback_data(
701 req
, struct winbindd_cli_state
);
705 ret
= wb_resp_write_recv(req
, &err
);
710 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
711 (int)state
->pid
, state
->cmd_name
, strerror(err
)));
712 remove_client(state
);
716 DEBUG(10,("winbind_client_response_written[%d:%s]: deliverd response to client\n",
717 (int)state
->pid
, state
->cmd_name
));
719 TALLOC_FREE(state
->mem_ctx
);
720 state
->response
= NULL
;
721 state
->cmd_name
= "no request";
722 state
->recv_fn
= NULL
;
724 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
725 WINBINDD_MAX_EXTRA_DATA
);
727 remove_client(state
);
730 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
733 void request_error(struct winbindd_cli_state
*state
)
735 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
736 state
->response
->result
= WINBINDD_ERROR
;
737 request_finished(state
);
740 void request_ok(struct winbindd_cli_state
*state
)
742 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
743 state
->response
->result
= WINBINDD_OK
;
744 request_finished(state
);
747 /* Process a new connection by adding it to the client connection list */
749 static void new_connection(int listen_sock
, bool privileged
)
751 struct sockaddr_un sunaddr
;
752 struct winbindd_cli_state
*state
;
753 struct tevent_req
*req
;
757 /* Accept connection */
759 len
= sizeof(sunaddr
);
762 sock
= accept(listen_sock
, (struct sockaddr
*)(void *)&sunaddr
,
764 } while (sock
== -1 && errno
== EINTR
);
769 DEBUG(6,("accepted socket %d\n", sock
));
771 /* Create new connection structure */
773 if ((state
= TALLOC_ZERO_P(NULL
, struct winbindd_cli_state
)) == NULL
) {
780 state
->out_queue
= tevent_queue_create(state
, "winbind client reply");
781 if (state
->out_queue
== NULL
) {
787 state
->last_access
= time(NULL
);
789 state
->privileged
= privileged
;
791 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
792 WINBINDD_MAX_EXTRA_DATA
);
798 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
800 /* Add to connection list */
802 winbindd_add_client(state
);
805 static void winbind_client_request_read(struct tevent_req
*req
)
807 struct winbindd_cli_state
*state
= tevent_req_callback_data(
808 req
, struct winbindd_cli_state
);
812 ret
= wb_req_read_recv(req
, state
, &state
->request
, &err
);
816 DEBUG(6, ("closing socket %d, client exited\n",
819 DEBUG(2, ("Could not read client request from fd %d: "
820 "%s\n", state
->sock
, strerror(err
)));
824 remove_client(state
);
827 process_request(state
);
830 /* Remove a client connection from client connection list */
832 static void remove_client(struct winbindd_cli_state
*state
)
837 /* It's a dead client - hold a funeral */
843 if (state
->sock
!= -1) {
844 /* tell client, we are closing ... */
845 nwritten
= write(state
->sock
, &c
, sizeof(c
));
846 if (nwritten
== -1) {
847 DEBUG(2, ("final write to client failed: %s\n",
857 TALLOC_FREE(state
->mem_ctx
);
859 /* Remove from list and free */
861 winbindd_remove_client(state
);
865 /* Shutdown client connection which has been idle for the longest time */
867 static bool remove_idle_client(void)
869 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
870 time_t last_access
= 0;
873 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
874 if (state
->response
== NULL
&&
875 !state
->pwent_state
&& !state
->grent_state
) {
877 if (!last_access
|| state
->last_access
< last_access
) {
878 last_access
= state
->last_access
;
879 remove_state
= state
;
885 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
886 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
887 remove_client(remove_state
);
894 struct winbindd_listen_state
{
899 static void winbindd_listen_fde_handler(struct tevent_context
*ev
,
900 struct tevent_fd
*fde
,
904 struct winbindd_listen_state
*s
= talloc_get_type_abort(private_data
,
905 struct winbindd_listen_state
);
907 while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
908 DEBUG(5,("winbindd: Exceeding %d client "
909 "connections, removing idle "
910 "connection.\n", lp_winbind_max_clients()));
911 if (!remove_idle_client()) {
912 DEBUG(0,("winbindd: Exceeding %d "
913 "client connections, no idle "
914 "connection found\n",
915 lp_winbind_max_clients()));
919 new_connection(s
->fd
, s
->privileged
);
922 static bool winbindd_setup_listeners(void)
924 struct winbindd_listen_state
*pub_state
= NULL
;
925 struct winbindd_listen_state
*priv_state
= NULL
;
926 struct tevent_fd
*fde
;
928 pub_state
= talloc(winbind_event_context(),
929 struct winbindd_listen_state
);
934 pub_state
->privileged
= false;
935 pub_state
->fd
= open_winbindd_socket();
936 if (pub_state
->fd
== -1) {
940 fde
= tevent_add_fd(winbind_event_context(), pub_state
, pub_state
->fd
,
941 TEVENT_FD_READ
, winbindd_listen_fde_handler
,
944 close(pub_state
->fd
);
947 tevent_fd_set_auto_close(fde
);
949 priv_state
= talloc(winbind_event_context(),
950 struct winbindd_listen_state
);
955 priv_state
->privileged
= true;
956 priv_state
->fd
= open_winbindd_priv_socket();
957 if (priv_state
->fd
== -1) {
961 fde
= tevent_add_fd(winbind_event_context(), priv_state
,
962 priv_state
->fd
, TEVENT_FD_READ
,
963 winbindd_listen_fde_handler
, priv_state
);
965 close(priv_state
->fd
);
968 tevent_fd_set_auto_close(fde
);
972 TALLOC_FREE(pub_state
);
973 TALLOC_FREE(priv_state
);
977 bool winbindd_use_idmap_cache(void)
982 bool winbindd_use_cache(void)
989 int main(int argc
, char **argv
, char **envp
)
991 static bool is_daemon
= False
;
992 static bool Fork
= True
;
993 static bool log_stdout
= False
;
994 static bool no_process_group
= False
;
998 OPT_NO_PROCESS_GROUP
,
1001 struct poptOption long_options
[] = {
1003 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1004 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1005 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1006 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1007 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1008 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1014 TALLOC_CTX
*frame
= talloc_stackframe();
1016 /* glibc (?) likes to print "User defined signal 1" and exit if a
1017 SIGUSR[12] is received before a handler is installed */
1019 CatchSignal(SIGUSR1
, SIG_IGN
);
1020 CatchSignal(SIGUSR2
, SIG_IGN
);
1022 fault_setup((void (*)(void *))fault_quit
);
1023 dump_core_setup("winbindd");
1027 /* Initialise for running in non-root mode */
1031 set_remote_machine_name("winbindd", False
);
1033 /* Set environment variable so we don't recursively call ourselves.
1034 This may also be useful interactively. */
1036 if ( !winbind_off() ) {
1037 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1041 /* Initialise samba/rpc client stuff */
1043 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1045 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1047 /* Don't become a daemon */
1059 case OPT_NO_PROCESS_GROUP
:
1060 no_process_group
= true;
1062 case OPT_LOG_STDOUT
:
1069 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1070 poptBadOption(pc
, 0), poptStrerror(opt
));
1071 poptPrintUsage(pc
, stderr
, 0);
1076 if (is_daemon
&& interactive
) {
1077 d_fprintf(stderr
,"\nERROR: "
1078 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1079 poptPrintUsage(pc
, stderr
, 0);
1083 if (log_stdout
&& Fork
) {
1084 d_fprintf(stderr
, "\nERROR: "
1085 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1086 poptPrintUsage(pc
, stderr
, 0);
1090 poptFreeContext(pc
);
1092 if (!override_logfile
) {
1094 if (asprintf(&lfile
,"%s/log.winbindd",
1095 get_dyn_LOGFILEBASE()) > 0) {
1096 lp_set_logfile(lfile
);
1100 setup_logging("winbindd", log_stdout
);
1103 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1104 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1106 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1107 DEBUG(0, ("error opening config file\n"));
1111 /* Initialise messaging system */
1113 if (winbind_messaging_context() == NULL
) {
1117 if (!reload_services_file(NULL
)) {
1118 DEBUG(0, ("error opening config file\n"));
1122 if (!directory_exist(lp_lockdir())) {
1123 mkdir(lp_lockdir(), 0755);
1133 if (!secrets_init()) {
1135 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1139 /* Enable netbios namecache */
1143 /* Unblock all signals we are interested in as they may have been
1144 blocked by the parent process. */
1146 BlockSignals(False
, SIGINT
);
1147 BlockSignals(False
, SIGQUIT
);
1148 BlockSignals(False
, SIGTERM
);
1149 BlockSignals(False
, SIGUSR1
);
1150 BlockSignals(False
, SIGUSR2
);
1151 BlockSignals(False
, SIGHUP
);
1152 BlockSignals(False
, SIGCHLD
);
1155 become_daemon(Fork
, no_process_group
);
1157 pidfile_create("winbindd");
1161 * If we're interactive we want to set our own process group for
1162 * signal management.
1164 if (interactive
&& !no_process_group
)
1165 setpgid( (pid_t
)0, (pid_t
)0);
1170 /* Don't use winbindd_reinit_after_fork here as
1171 * we're just starting up and haven't created any
1172 * winbindd-specific resources we must free yet. JRA.
1175 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1176 winbind_event_context(),
1178 DEBUG(0,("reinit_after_fork() failed\n"));
1182 /* Setup signal handlers */
1184 if (!winbindd_setup_sig_term_handler(true))
1186 if (!winbindd_setup_sig_hup_handler(NULL
))
1188 if (!winbindd_setup_sig_chld_handler())
1190 if (!winbindd_setup_sig_usr2_handler())
1193 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1196 * Ensure all cache and idmap caches are consistent
1197 * and initialized before we startup.
1199 if (!winbindd_cache_validate_and_initialize()) {
1203 /* get broadcast messages */
1204 claim_connection(NULL
,"",FLAG_MSG_GENERAL
|FLAG_MSG_DBWRAP
);
1206 /* React on 'smbcontrol winbindd reload-config' in the same way
1207 as to SIGHUP signal */
1208 messaging_register(winbind_messaging_context(), NULL
,
1209 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1210 messaging_register(winbind_messaging_context(), NULL
,
1211 MSG_SHUTDOWN
, msg_shutdown
);
1213 /* Handle online/offline messages. */
1214 messaging_register(winbind_messaging_context(), NULL
,
1215 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1216 messaging_register(winbind_messaging_context(), NULL
,
1217 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1218 messaging_register(winbind_messaging_context(), NULL
,
1219 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1221 messaging_register(winbind_messaging_context(), NULL
,
1222 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1224 messaging_register(winbind_messaging_context(), NULL
,
1225 MSG_WINBIND_VALIDATE_CACHE
,
1226 winbind_msg_validate_cache
);
1228 messaging_register(winbind_messaging_context(), NULL
,
1229 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1230 winbind_msg_dump_domain_list
);
1232 /* Register handler for MSG_DEBUG. */
1233 messaging_register(winbind_messaging_context(), NULL
,
1237 netsamlogon_cache_init(); /* Non-critical */
1239 /* clear the cached list of trusted domains */
1243 if (!init_domain_list()) {
1244 DEBUG(0,("unable to initialize domain list\n"));
1249 init_locator_child();
1251 smb_nscd_flush_user_cache();
1252 smb_nscd_flush_group_cache();
1254 /* setup listen sockets */
1256 if (!winbindd_setup_listeners()) {
1257 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1261 if (lp_allow_trusted_domains()) {
1262 if (tevent_add_timer(winbind_event_context(), NULL
, timeval_zero(),
1263 rescan_trusted_domains
, NULL
) == NULL
) {
1264 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1270 /* Loop waiting for requests */
1272 frame
= talloc_stackframe();
1274 if (tevent_loop_once(winbind_event_context()) == -1) {
1275 DEBUG(1, ("tevent_loop_once() failed: %s\n",