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/>.
26 #include "popt_common.h"
28 #include "nsswitch/winbind_client.h"
29 #include "nsswitch/wb_reqtrans.h"
31 #include "../librpc/gen_ndr/srv_lsa.h"
32 #include "../librpc/gen_ndr/srv_samr.h"
35 #include "lib/addrchange.h"
39 #include "../lib/util/pidfile.h"
42 #define DBGC_CLASS DBGC_WINBIND
44 static bool client_is_idle(struct winbindd_cli_state
*state
);
45 static void remove_client(struct winbindd_cli_state
*state
);
47 static bool opt_nocache
= False
;
48 static bool interactive
= False
;
50 extern bool override_logfile
;
52 struct messaging_context
*winbind_messaging_context(void)
54 struct messaging_context
*msg_ctx
= server_messaging_context();
55 if (likely(msg_ctx
!= NULL
)) {
58 smb_panic("Could not init winbindd's messaging context.\n");
62 /* Reload configuration */
64 static bool reload_services_file(const char *lfile
)
69 char *fname
= lp_configfile(talloc_tos());
71 if (file_exist(fname
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
72 set_dyn_CONFIGFILE(fname
);
77 /* if this is a child, restore the logfile to the special
78 name - <domain>, idmap, etc. */
79 if (lfile
&& *lfile
) {
80 lp_set_logfile(lfile
);
84 ret
= lp_load_global(get_dyn_CONFIGFILE());
93 static void winbindd_status(void)
95 struct winbindd_cli_state
*tmp
;
97 DEBUG(0, ("winbindd status:\n"));
99 /* Print client state information */
101 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
103 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
104 DEBUG(2, ("\tclient list:\n"));
105 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
106 DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
107 (unsigned long)tmp
->pid
, tmp
->sock
,
108 client_is_idle(tmp
) ? "idle" : "active"));
113 /* Flush client cache */
115 static void flush_caches(void)
117 /* We need to invalidate cached user list entries on a SIGHUP
118 otherwise cached access denied errors due to restrict anonymous
119 hang around until the sequence number changes. */
121 if (!wcache_invalidate_cache()) {
122 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
123 if (!winbindd_cache_validate_and_initialize()) {
129 static void flush_caches_noinit(void)
132 * We need to invalidate cached user list entries on a SIGHUP
133 * otherwise cached access denied errors due to restrict anonymous
134 * hang around until the sequence number changes.
136 * Skip uninitialized domains when flush cache.
137 * If domain is not initialized, it means it is never
138 * used or never become online. look, wcache_invalidate_cache()
139 * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
140 * for unused domains and large traffic for primay domain's DC if there
144 if (!wcache_invalidate_cache_noinit()) {
145 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
146 if (!winbindd_cache_validate_and_initialize()) {
152 /* Handle the signal by unlinking socket and exiting */
154 static void terminate(bool is_parent
)
157 /* When parent goes away we should
158 * remove the socket file. Not so
159 * when children terminate.
163 if (asprintf(&path
, "%s/%s",
164 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
172 trustdom_cache_shutdown();
174 gencache_stabilize();
178 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
179 char *description
= talloc_describe_all(mem_ctx
);
181 DEBUG(3, ("tallocs left:\n%s\n", description
));
182 talloc_destroy(mem_ctx
);
187 serverid_deregister(procid_self());
188 pidfile_unlink(lp_piddir(), "winbindd");
194 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
195 struct tevent_signal
*se
,
201 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
203 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
204 signum
, (int)*is_parent
));
205 terminate(*is_parent
);
209 handle stdin becoming readable when we are in --foreground mode
211 static void winbindd_stdin_handler(struct tevent_context
*ev
,
212 struct tevent_fd
*fde
,
217 if (read(0, &c
, 1) != 1) {
218 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
220 /* we have reached EOF on stdin, which means the
221 parent has exited. Shutdown the server */
222 DEBUG(0,("EOF on stdin (is_parent=%d)\n",
224 terminate(*is_parent
);
228 bool winbindd_setup_sig_term_handler(bool parent
)
230 struct tevent_signal
*se
;
233 is_parent
= talloc(winbind_event_context(), bool);
240 se
= tevent_add_signal(winbind_event_context(),
243 winbindd_sig_term_handler
,
246 DEBUG(0,("failed to setup SIGTERM handler"));
247 talloc_free(is_parent
);
251 se
= tevent_add_signal(winbind_event_context(),
254 winbindd_sig_term_handler
,
257 DEBUG(0,("failed to setup SIGINT handler"));
258 talloc_free(is_parent
);
262 se
= tevent_add_signal(winbind_event_context(),
265 winbindd_sig_term_handler
,
268 DEBUG(0,("failed to setup SIGINT handler"));
269 talloc_free(is_parent
);
276 bool winbindd_setup_stdin_handler(bool parent
, bool foreground
)
281 is_parent
= talloc(winbind_event_context(), bool);
288 /* if we are running in the foreground then look for
289 EOF on stdin, and exit if it happens. This allows
290 us to die if the parent process dies
292 tevent_add_fd(winbind_event_context(), is_parent
, 0, TEVENT_FD_READ
, winbindd_stdin_handler
, is_parent
);
298 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
299 struct tevent_signal
*se
,
305 const char *file
= (const char *)private_data
;
307 DEBUG(1,("Reloading services after SIGHUP\n"));
308 flush_caches_noinit();
309 reload_services_file(file
);
312 bool winbindd_setup_sig_hup_handler(const char *lfile
)
314 struct tevent_signal
*se
;
318 file
= talloc_strdup(winbind_event_context(),
325 se
= tevent_add_signal(winbind_event_context(),
326 winbind_event_context(),
328 winbindd_sig_hup_handler
,
337 static void winbindd_sig_chld_handler(struct tevent_context
*ev
,
338 struct tevent_signal
*se
,
346 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
347 winbind_child_died(pid
);
351 static bool winbindd_setup_sig_chld_handler(void)
353 struct tevent_signal
*se
;
355 se
= tevent_add_signal(winbind_event_context(),
356 winbind_event_context(),
358 winbindd_sig_chld_handler
,
367 static void winbindd_sig_usr2_handler(struct tevent_context
*ev
,
368 struct tevent_signal
*se
,
377 static bool winbindd_setup_sig_usr2_handler(void)
379 struct tevent_signal
*se
;
381 se
= tevent_add_signal(winbind_event_context(),
382 winbind_event_context(),
384 winbindd_sig_usr2_handler
,
393 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
394 static void msg_reload_services(struct messaging_context
*msg
,
397 struct server_id server_id
,
400 /* Flush various caches */
402 reload_services_file((const char *) private_data
);
405 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
406 static void msg_shutdown(struct messaging_context
*msg
,
409 struct server_id server_id
,
412 /* only the parent waits for this message */
413 DEBUG(0,("Got shutdown message\n"));
418 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
421 struct server_id server_id
,
428 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
432 * call the validation code from a child:
433 * so we don't block the main winbindd and the validation
434 * code can safely use fork/waitpid...
438 if (child_pid
== -1) {
439 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
444 if (child_pid
!= 0) {
446 DEBUG(5, ("winbind_msg_validate_cache: child created with "
447 "pid %d.\n", (int)child_pid
));
453 status
= winbindd_reinit_after_fork(NULL
, NULL
);
454 if (!NT_STATUS_IS_OK(status
)) {
455 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
460 /* install default SIGCHLD handler: validation code uses fork/waitpid */
461 CatchSignal(SIGCHLD
, SIG_DFL
);
463 ret
= (uint8
)winbindd_validate_cache_nobackup();
464 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
465 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
470 static struct winbindd_dispatch_table
{
471 enum winbindd_cmd cmd
;
472 void (*fn
)(struct winbindd_cli_state
*state
);
473 const char *winbindd_cmd_name
;
474 } dispatch_table
[] = {
476 /* Enumeration functions */
478 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
483 { WINBINDD_INFO
, winbindd_info
, "INFO" },
484 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
485 "INTERFACE_VERSION" },
486 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
487 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
488 { WINBINDD_DC_INFO
, winbindd_dc_info
, "DC_INFO" },
489 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
490 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
491 "WINBINDD_PRIV_PIPE_DIR" },
493 /* Credential cache access */
494 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
495 { WINBINDD_CCACHE_SAVE
, winbindd_ccache_save
, "CCACHE_SAVE" },
499 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
502 struct winbindd_async_dispatch_table
{
503 enum winbindd_cmd cmd
;
504 const char *cmd_name
;
505 struct tevent_req
*(*send_req
)(TALLOC_CTX
*mem_ctx
,
506 struct tevent_context
*ev
,
507 struct winbindd_cli_state
*cli
,
508 struct winbindd_request
*request
);
509 NTSTATUS (*recv_req
)(struct tevent_req
*req
,
510 struct winbindd_response
*presp
);
513 static struct winbindd_async_dispatch_table async_nonpriv_table
[] = {
514 { WINBINDD_PING
, "PING",
515 wb_ping_send
, wb_ping_recv
},
516 { WINBINDD_LOOKUPSID
, "LOOKUPSID",
517 winbindd_lookupsid_send
, winbindd_lookupsid_recv
},
518 { WINBINDD_LOOKUPSIDS
, "LOOKUPSIDS",
519 winbindd_lookupsids_send
, winbindd_lookupsids_recv
},
520 { WINBINDD_LOOKUPNAME
, "LOOKUPNAME",
521 winbindd_lookupname_send
, winbindd_lookupname_recv
},
522 { WINBINDD_SID_TO_UID
, "SID_TO_UID",
523 winbindd_sid_to_uid_send
, winbindd_sid_to_uid_recv
},
524 { WINBINDD_SID_TO_GID
, "SID_TO_GID",
525 winbindd_sid_to_gid_send
, winbindd_sid_to_gid_recv
},
526 { WINBINDD_UID_TO_SID
, "UID_TO_SID",
527 winbindd_uid_to_sid_send
, winbindd_uid_to_sid_recv
},
528 { WINBINDD_GID_TO_SID
, "GID_TO_SID",
529 winbindd_gid_to_sid_send
, winbindd_gid_to_sid_recv
},
530 { WINBINDD_SIDS_TO_XIDS
, "SIDS_TO_XIDS",
531 winbindd_sids_to_xids_send
, winbindd_sids_to_xids_recv
},
532 { WINBINDD_GETPWSID
, "GETPWSID",
533 winbindd_getpwsid_send
, winbindd_getpwsid_recv
},
534 { WINBINDD_GETPWNAM
, "GETPWNAM",
535 winbindd_getpwnam_send
, winbindd_getpwnam_recv
},
536 { WINBINDD_GETPWUID
, "GETPWUID",
537 winbindd_getpwuid_send
, winbindd_getpwuid_recv
},
538 { WINBINDD_GETSIDALIASES
, "GETSIDALIASES",
539 winbindd_getsidaliases_send
, winbindd_getsidaliases_recv
},
540 { WINBINDD_GETUSERDOMGROUPS
, "GETUSERDOMGROUPS",
541 winbindd_getuserdomgroups_send
, winbindd_getuserdomgroups_recv
},
542 { WINBINDD_GETGROUPS
, "GETGROUPS",
543 winbindd_getgroups_send
, winbindd_getgroups_recv
},
544 { WINBINDD_SHOW_SEQUENCE
, "SHOW_SEQUENCE",
545 winbindd_show_sequence_send
, winbindd_show_sequence_recv
},
546 { WINBINDD_GETGRGID
, "GETGRGID",
547 winbindd_getgrgid_send
, winbindd_getgrgid_recv
},
548 { WINBINDD_GETGRNAM
, "GETGRNAM",
549 winbindd_getgrnam_send
, winbindd_getgrnam_recv
},
550 { WINBINDD_GETUSERSIDS
, "GETUSERSIDS",
551 winbindd_getusersids_send
, winbindd_getusersids_recv
},
552 { WINBINDD_LOOKUPRIDS
, "LOOKUPRIDS",
553 winbindd_lookuprids_send
, winbindd_lookuprids_recv
},
554 { WINBINDD_SETPWENT
, "SETPWENT",
555 winbindd_setpwent_send
, winbindd_setpwent_recv
},
556 { WINBINDD_GETPWENT
, "GETPWENT",
557 winbindd_getpwent_send
, winbindd_getpwent_recv
},
558 { WINBINDD_ENDPWENT
, "ENDPWENT",
559 winbindd_endpwent_send
, winbindd_endpwent_recv
},
560 { WINBINDD_DSGETDCNAME
, "DSGETDCNAME",
561 winbindd_dsgetdcname_send
, winbindd_dsgetdcname_recv
},
562 { WINBINDD_GETDCNAME
, "GETDCNAME",
563 winbindd_getdcname_send
, winbindd_getdcname_recv
},
564 { WINBINDD_SETGRENT
, "SETGRENT",
565 winbindd_setgrent_send
, winbindd_setgrent_recv
},
566 { WINBINDD_GETGRENT
, "GETGRENT",
567 winbindd_getgrent_send
, winbindd_getgrent_recv
},
568 { WINBINDD_ENDGRENT
, "ENDGRENT",
569 winbindd_endgrent_send
, winbindd_endgrent_recv
},
570 { WINBINDD_LIST_USERS
, "LIST_USERS",
571 winbindd_list_users_send
, winbindd_list_users_recv
},
572 { WINBINDD_LIST_GROUPS
, "LIST_GROUPS",
573 winbindd_list_groups_send
, winbindd_list_groups_recv
},
574 { WINBINDD_CHECK_MACHACC
, "CHECK_MACHACC",
575 winbindd_check_machine_acct_send
, winbindd_check_machine_acct_recv
},
576 { WINBINDD_PING_DC
, "PING_DC",
577 winbindd_ping_dc_send
, winbindd_ping_dc_recv
},
578 { WINBINDD_PAM_AUTH
, "PAM_AUTH",
579 winbindd_pam_auth_send
, winbindd_pam_auth_recv
},
580 { WINBINDD_PAM_LOGOFF
, "PAM_LOGOFF",
581 winbindd_pam_logoff_send
, winbindd_pam_logoff_recv
},
582 { WINBINDD_PAM_CHAUTHTOK
, "PAM_CHAUTHTOK",
583 winbindd_pam_chauthtok_send
, winbindd_pam_chauthtok_recv
},
584 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, "PAM_CHNG_PSWD_AUTH_CRAP",
585 winbindd_pam_chng_pswd_auth_crap_send
,
586 winbindd_pam_chng_pswd_auth_crap_recv
},
587 { WINBINDD_WINS_BYIP
, "WINS_BYIP",
588 winbindd_wins_byip_send
, winbindd_wins_byip_recv
},
589 { WINBINDD_WINS_BYNAME
, "WINS_BYNAME",
590 winbindd_wins_byname_send
, winbindd_wins_byname_recv
},
592 { 0, NULL
, NULL
, NULL
}
595 static struct winbindd_async_dispatch_table async_priv_table
[] = {
596 { WINBINDD_ALLOCATE_UID
, "ALLOCATE_UID",
597 winbindd_allocate_uid_send
, winbindd_allocate_uid_recv
},
598 { WINBINDD_ALLOCATE_GID
, "ALLOCATE_GID",
599 winbindd_allocate_gid_send
, winbindd_allocate_gid_recv
},
600 { WINBINDD_CHANGE_MACHACC
, "CHANGE_MACHACC",
601 winbindd_change_machine_acct_send
, winbindd_change_machine_acct_recv
},
602 { WINBINDD_PAM_AUTH_CRAP
, "PAM_AUTH_CRAP",
603 winbindd_pam_auth_crap_send
, winbindd_pam_auth_crap_recv
},
605 { 0, NULL
, NULL
, NULL
}
608 static void wb_request_done(struct tevent_req
*req
);
610 static void process_request(struct winbindd_cli_state
*state
)
612 struct winbindd_dispatch_table
*table
= dispatch_table
;
613 struct winbindd_async_dispatch_table
*atable
;
615 state
->mem_ctx
= talloc_named(state
, 0, "winbind request");
616 if (state
->mem_ctx
== NULL
)
619 /* Remember who asked us. */
620 state
->pid
= state
->request
->pid
;
622 state
->cmd_name
= "unknown request";
623 state
->recv_fn
= NULL
;
625 /* Process command */
627 for (atable
= async_nonpriv_table
; atable
->send_req
; atable
+= 1) {
628 if (state
->request
->cmd
== atable
->cmd
) {
633 if ((atable
->send_req
== NULL
) && state
->privileged
) {
634 for (atable
= async_priv_table
; atable
->send_req
;
636 if (state
->request
->cmd
== atable
->cmd
) {
642 if (atable
->send_req
!= NULL
) {
643 struct tevent_req
*req
;
645 state
->cmd_name
= atable
->cmd_name
;
646 state
->recv_fn
= atable
->recv_req
;
648 DEBUG(10, ("process_request: Handling async request %d:%s\n",
649 (int)state
->pid
, state
->cmd_name
));
651 req
= atable
->send_req(state
->mem_ctx
, winbind_event_context(),
652 state
, state
->request
);
654 DEBUG(0, ("process_request: atable->send failed for "
655 "%s\n", atable
->cmd_name
));
656 request_error(state
);
659 tevent_req_set_callback(req
, wb_request_done
, state
);
663 state
->response
= talloc_zero(state
->mem_ctx
,
664 struct winbindd_response
);
665 if (state
->response
== NULL
) {
666 DEBUG(10, ("talloc failed\n"));
667 remove_client(state
);
670 state
->response
->result
= WINBINDD_PENDING
;
671 state
->response
->length
= sizeof(struct winbindd_response
);
673 for (table
= dispatch_table
; table
->fn
; table
++) {
674 if (state
->request
->cmd
== table
->cmd
) {
675 DEBUG(10,("process_request: request fn %s\n",
676 table
->winbindd_cmd_name
));
677 state
->cmd_name
= table
->winbindd_cmd_name
;
684 DEBUG(10,("process_request: unknown request fn number %d\n",
685 (int)state
->request
->cmd
));
686 request_error(state
);
690 static void wb_request_done(struct tevent_req
*req
)
692 struct winbindd_cli_state
*state
= tevent_req_callback_data(
693 req
, struct winbindd_cli_state
);
696 state
->response
= talloc_zero(state
->mem_ctx
,
697 struct winbindd_response
);
698 if (state
->response
== NULL
) {
699 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
700 (int)state
->pid
, state
->cmd_name
));
701 remove_client(state
);
704 state
->response
->result
= WINBINDD_PENDING
;
705 state
->response
->length
= sizeof(struct winbindd_response
);
707 status
= state
->recv_fn(req
, state
->response
);
710 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
711 (int)state
->pid
, state
->cmd_name
, nt_errstr(status
)));
713 if (!NT_STATUS_IS_OK(status
)) {
714 request_error(state
);
721 * This is the main event loop of winbind requests. It goes through a
722 * state-machine of 3 read/write requests, 4 if you have extra data to send.
724 * An idle winbind client has a read request of 4 bytes outstanding,
725 * finalizing function is request_len_recv, checking the length. request_recv
726 * then processes the packet. The processing function then at some point has
727 * to call request_finished which schedules sending the response.
730 static void request_finished(struct winbindd_cli_state
*state
);
732 static void winbind_client_request_read(struct tevent_req
*req
);
733 static void winbind_client_response_written(struct tevent_req
*req
);
735 static void request_finished(struct winbindd_cli_state
*state
)
737 struct tevent_req
*req
;
739 TALLOC_FREE(state
->request
);
741 req
= wb_resp_write_send(state
, winbind_event_context(),
742 state
->out_queue
, state
->sock
,
745 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
746 (int)state
->pid
, state
->cmd_name
));
747 remove_client(state
);
750 tevent_req_set_callback(req
, winbind_client_response_written
, state
);
753 static void winbind_client_response_written(struct tevent_req
*req
)
755 struct winbindd_cli_state
*state
= tevent_req_callback_data(
756 req
, struct winbindd_cli_state
);
760 ret
= wb_resp_write_recv(req
, &err
);
765 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
766 (int)state
->pid
, state
->cmd_name
, strerror(err
)));
767 remove_client(state
);
771 DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
772 "to client\n", (int)state
->pid
, state
->cmd_name
));
774 TALLOC_FREE(state
->mem_ctx
);
775 state
->response
= NULL
;
776 state
->cmd_name
= "no request";
777 state
->recv_fn
= NULL
;
779 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
780 WINBINDD_MAX_EXTRA_DATA
);
782 remove_client(state
);
785 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
788 void request_error(struct winbindd_cli_state
*state
)
790 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
791 state
->response
->result
= WINBINDD_ERROR
;
792 request_finished(state
);
795 void request_ok(struct winbindd_cli_state
*state
)
797 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
798 state
->response
->result
= WINBINDD_OK
;
799 request_finished(state
);
802 /* Process a new connection by adding it to the client connection list */
804 static void new_connection(int listen_sock
, bool privileged
)
806 struct sockaddr_un sunaddr
;
807 struct winbindd_cli_state
*state
;
808 struct tevent_req
*req
;
812 /* Accept connection */
814 len
= sizeof(sunaddr
);
816 sock
= accept(listen_sock
, (struct sockaddr
*)(void *)&sunaddr
, &len
);
819 if (errno
!= EINTR
) {
820 DEBUG(0, ("Faild to accept socket - %s\n",
826 DEBUG(6,("accepted socket %d\n", sock
));
828 /* Create new connection structure */
830 if ((state
= talloc_zero(NULL
, struct winbindd_cli_state
)) == NULL
) {
837 state
->out_queue
= tevent_queue_create(state
, "winbind client reply");
838 if (state
->out_queue
== NULL
) {
844 state
->last_access
= time(NULL
);
846 state
->privileged
= privileged
;
848 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
849 WINBINDD_MAX_EXTRA_DATA
);
855 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
857 /* Add to connection list */
859 winbindd_add_client(state
);
862 static void winbind_client_request_read(struct tevent_req
*req
)
864 struct winbindd_cli_state
*state
= tevent_req_callback_data(
865 req
, struct winbindd_cli_state
);
869 ret
= wb_req_read_recv(req
, state
, &state
->request
, &err
);
873 DEBUG(6, ("closing socket %d, client exited\n",
876 DEBUG(2, ("Could not read client request from fd %d: "
877 "%s\n", state
->sock
, strerror(err
)));
881 remove_client(state
);
884 process_request(state
);
887 /* Remove a client connection from client connection list */
889 static void remove_client(struct winbindd_cli_state
*state
)
894 /* It's a dead client - hold a funeral */
900 if (state
->sock
!= -1) {
901 /* tell client, we are closing ... */
902 nwritten
= write(state
->sock
, &c
, sizeof(c
));
903 if (nwritten
== -1) {
904 DEBUG(2, ("final write to client failed: %s\n",
914 TALLOC_FREE(state
->mem_ctx
);
916 /* Remove from list and free */
918 winbindd_remove_client(state
);
922 /* Is a client idle? */
924 static bool client_is_idle(struct winbindd_cli_state
*state
) {
925 return (state
->response
== NULL
&&
926 !state
->pwent_state
&& !state
->grent_state
);
929 /* Shutdown client connection which has been idle for the longest time */
931 static bool remove_idle_client(void)
933 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
934 time_t last_access
= 0;
937 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
938 if (client_is_idle(state
)) {
940 if (!last_access
|| state
->last_access
< last_access
) {
941 last_access
= state
->last_access
;
942 remove_state
= state
;
948 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
949 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
950 remove_client(remove_state
);
957 struct winbindd_listen_state
{
962 static void winbindd_listen_fde_handler(struct tevent_context
*ev
,
963 struct tevent_fd
*fde
,
967 struct winbindd_listen_state
*s
= talloc_get_type_abort(private_data
,
968 struct winbindd_listen_state
);
970 while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
971 DEBUG(5,("winbindd: Exceeding %d client "
972 "connections, removing idle "
973 "connection.\n", lp_winbind_max_clients()));
974 if (!remove_idle_client()) {
975 DEBUG(0,("winbindd: Exceeding %d "
976 "client connections, no idle "
977 "connection found\n",
978 lp_winbind_max_clients()));
982 new_connection(s
->fd
, s
->privileged
);
986 * Winbindd socket accessor functions
989 const char *get_winbind_pipe_dir(void)
991 return lp_parm_const_string(-1, "winbindd", "socket dir", get_dyn_WINBINDD_SOCKET_DIR());
994 char *get_winbind_priv_pipe_dir(void)
996 return state_path(WINBINDD_PRIV_SOCKET_SUBDIR
);
999 static bool winbindd_setup_listeners(void)
1001 struct winbindd_listen_state
*pub_state
= NULL
;
1002 struct winbindd_listen_state
*priv_state
= NULL
;
1003 struct tevent_fd
*fde
;
1006 pub_state
= talloc(winbind_event_context(),
1007 struct winbindd_listen_state
);
1012 pub_state
->privileged
= false;
1013 pub_state
->fd
= create_pipe_sock(
1014 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
, 0755);
1015 if (pub_state
->fd
== -1) {
1018 rc
= listen(pub_state
->fd
, 5);
1023 fde
= tevent_add_fd(winbind_event_context(), pub_state
, pub_state
->fd
,
1024 TEVENT_FD_READ
, winbindd_listen_fde_handler
,
1027 close(pub_state
->fd
);
1030 tevent_fd_set_auto_close(fde
);
1032 priv_state
= talloc(winbind_event_context(),
1033 struct winbindd_listen_state
);
1038 priv_state
->privileged
= true;
1039 priv_state
->fd
= create_pipe_sock(
1040 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME
, 0750);
1041 if (priv_state
->fd
== -1) {
1044 rc
= listen(priv_state
->fd
, 5);
1049 fde
= tevent_add_fd(winbind_event_context(), priv_state
,
1050 priv_state
->fd
, TEVENT_FD_READ
,
1051 winbindd_listen_fde_handler
, priv_state
);
1053 close(priv_state
->fd
);
1056 tevent_fd_set_auto_close(fde
);
1060 TALLOC_FREE(pub_state
);
1061 TALLOC_FREE(priv_state
);
1065 bool winbindd_use_idmap_cache(void)
1067 return !opt_nocache
;
1070 bool winbindd_use_cache(void)
1072 return !opt_nocache
;
1075 void winbindd_register_handlers(bool foreground
)
1077 /* Setup signal handlers */
1079 if (!winbindd_setup_sig_term_handler(true))
1081 if (!winbindd_setup_stdin_handler(true, foreground
))
1083 if (!winbindd_setup_sig_hup_handler(NULL
))
1085 if (!winbindd_setup_sig_chld_handler())
1087 if (!winbindd_setup_sig_usr2_handler())
1090 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1093 * Ensure all cache and idmap caches are consistent
1094 * and initialized before we startup.
1096 if (!winbindd_cache_validate_and_initialize()) {
1100 /* get broadcast messages */
1102 if (!serverid_register(procid_self(),
1106 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1110 /* React on 'smbcontrol winbindd reload-config' in the same way
1111 as to SIGHUP signal */
1112 messaging_register(winbind_messaging_context(), NULL
,
1113 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1114 messaging_register(winbind_messaging_context(), NULL
,
1115 MSG_SHUTDOWN
, msg_shutdown
);
1117 /* Handle online/offline messages. */
1118 messaging_register(winbind_messaging_context(), NULL
,
1119 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1120 messaging_register(winbind_messaging_context(), NULL
,
1121 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1122 messaging_register(winbind_messaging_context(), NULL
,
1123 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1125 messaging_register(winbind_messaging_context(), NULL
,
1126 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1128 messaging_register(winbind_messaging_context(), NULL
,
1129 MSG_WINBIND_VALIDATE_CACHE
,
1130 winbind_msg_validate_cache
);
1132 messaging_register(winbind_messaging_context(), NULL
,
1133 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1134 winbind_msg_dump_domain_list
);
1136 messaging_register(winbind_messaging_context(), NULL
,
1137 MSG_WINBIND_IP_DROPPED
,
1138 winbind_msg_ip_dropped_parent
);
1140 /* Register handler for MSG_DEBUG. */
1141 messaging_register(winbind_messaging_context(), NULL
,
1145 netsamlogon_cache_init(); /* Non-critical */
1147 /* clear the cached list of trusted domains */
1151 if (!init_domain_list()) {
1152 DEBUG(0,("unable to initialize domain list\n"));
1157 init_locator_child();
1159 smb_nscd_flush_user_cache();
1160 smb_nscd_flush_group_cache();
1162 if (lp_allow_trusted_domains()) {
1163 if (tevent_add_timer(winbind_event_context(), NULL
, timeval_zero(),
1164 rescan_trusted_domains
, NULL
) == NULL
) {
1165 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1172 struct winbindd_addrchanged_state
{
1173 struct addrchange_context
*ctx
;
1174 struct tevent_context
*ev
;
1175 struct messaging_context
*msg_ctx
;
1178 static void winbindd_addr_changed(struct tevent_req
*req
);
1180 static void winbindd_init_addrchange(TALLOC_CTX
*mem_ctx
,
1181 struct tevent_context
*ev
,
1182 struct messaging_context
*msg_ctx
)
1184 struct winbindd_addrchanged_state
*state
;
1185 struct tevent_req
*req
;
1188 state
= talloc(mem_ctx
, struct winbindd_addrchanged_state
);
1189 if (state
== NULL
) {
1190 DEBUG(10, ("talloc failed\n"));
1194 state
->msg_ctx
= msg_ctx
;
1196 status
= addrchange_context_create(state
, &state
->ctx
);
1197 if (!NT_STATUS_IS_OK(status
)) {
1198 DEBUG(10, ("addrchange_context_create failed: %s\n",
1199 nt_errstr(status
)));
1203 req
= addrchange_send(state
, ev
, state
->ctx
);
1205 DEBUG(0, ("addrchange_send failed\n"));
1209 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1212 static void winbindd_addr_changed(struct tevent_req
*req
)
1214 struct winbindd_addrchanged_state
*state
= tevent_req_callback_data(
1215 req
, struct winbindd_addrchanged_state
);
1216 enum addrchange_type type
;
1217 struct sockaddr_storage addr
;
1220 status
= addrchange_recv(req
, &type
, &addr
);
1222 if (!NT_STATUS_IS_OK(status
)) {
1223 DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
1224 nt_errstr(status
)));
1228 if (type
== ADDRCHANGE_DEL
) {
1229 char addrstr
[INET6_ADDRSTRLEN
];
1232 print_sockaddr(addrstr
, sizeof(addrstr
), &addr
);
1234 DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
1237 blob
= data_blob_const(addrstr
, strlen(addrstr
)+1);
1239 status
= messaging_send(state
->msg_ctx
,
1240 messaging_server_id(state
->msg_ctx
),
1241 MSG_WINBIND_IP_DROPPED
, &blob
);
1242 if (!NT_STATUS_IS_OK(status
)) {
1243 DEBUG(10, ("messaging_send failed: %s - ignoring\n",
1244 nt_errstr(status
)));
1247 req
= addrchange_send(state
, state
->ev
, state
->ctx
);
1249 DEBUG(0, ("addrchange_send failed\n"));
1253 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1258 int main(int argc
, char **argv
, char **envp
)
1260 static bool is_daemon
= False
;
1261 static bool Fork
= True
;
1262 static bool log_stdout
= False
;
1263 static bool no_process_group
= False
;
1267 OPT_NO_PROCESS_GROUP
,
1270 struct poptOption long_options
[] = {
1272 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1273 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1274 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1275 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1276 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1277 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1287 * Do this before any other talloc operation
1289 talloc_enable_null_tracking();
1290 frame
= talloc_stackframe();
1292 setup_logging("winbindd", DEBUG_DEFAULT_STDOUT
);
1294 /* glibc (?) likes to print "User defined signal 1" and exit if a
1295 SIGUSR[12] is received before a handler is installed */
1297 CatchSignal(SIGUSR1
, SIG_IGN
);
1298 CatchSignal(SIGUSR2
, SIG_IGN
);
1301 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1305 /* Initialise for running in non-root mode */
1309 set_remote_machine_name("winbindd", False
);
1311 /* Set environment variable so we don't recursively call ourselves.
1312 This may also be useful interactively. */
1314 if ( !winbind_off() ) {
1315 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1319 /* Initialise samba/rpc client stuff */
1321 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1323 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1325 /* Don't become a daemon */
1337 case OPT_NO_PROCESS_GROUP
:
1338 no_process_group
= true;
1340 case OPT_LOG_STDOUT
:
1347 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1348 poptBadOption(pc
, 0), poptStrerror(opt
));
1349 poptPrintUsage(pc
, stderr
, 0);
1354 /* We call dump_core_setup one more time because the command line can
1355 * set the log file or the log-basename and this will influence where
1356 * cores are stored. Without this call get_dyn_LOGFILEBASE will be
1357 * the default value derived from build's prefix. For EOM this value
1358 * is often not related to the path where winbindd is actually run
1361 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1362 if (is_daemon
&& interactive
) {
1363 d_fprintf(stderr
,"\nERROR: "
1364 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1365 poptPrintUsage(pc
, stderr
, 0);
1369 if (log_stdout
&& Fork
) {
1370 d_fprintf(stderr
, "\nERROR: "
1371 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1372 poptPrintUsage(pc
, stderr
, 0);
1376 poptFreeContext(pc
);
1378 if (!override_logfile
) {
1380 if (asprintf(&lfile
,"%s/log.winbindd",
1381 get_dyn_LOGFILEBASE()) > 0) {
1382 lp_set_logfile(lfile
);
1388 setup_logging("winbindd", DEBUG_STDOUT
);
1390 setup_logging("winbindd", DEBUG_FILE
);
1394 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1395 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1397 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1398 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1401 /* After parsing the configuration file we setup the core path one more time
1402 * as the log file might have been set in the configuration and cores's
1403 * path is by default basename(lp_logfile()).
1405 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1407 /* Initialise messaging system */
1409 if (winbind_messaging_context() == NULL
) {
1413 if (!reload_services_file(NULL
)) {
1414 DEBUG(0, ("error opening config file\n"));
1418 if (!directory_exist(lp_lockdir())) {
1419 mkdir(lp_lockdir(), 0755);
1422 if (!directory_exist(lp_piddir())) {
1423 mkdir(lp_piddir(), 0755);
1433 if (!secrets_init()) {
1435 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1439 /* Unblock all signals we are interested in as they may have been
1440 blocked by the parent process. */
1442 BlockSignals(False
, SIGINT
);
1443 BlockSignals(False
, SIGQUIT
);
1444 BlockSignals(False
, SIGTERM
);
1445 BlockSignals(False
, SIGUSR1
);
1446 BlockSignals(False
, SIGUSR2
);
1447 BlockSignals(False
, SIGHUP
);
1448 BlockSignals(False
, SIGCHLD
);
1451 become_daemon(Fork
, no_process_group
, log_stdout
);
1453 pidfile_create(lp_piddir(), "winbindd");
1457 * If we're interactive we want to set our own process group for
1458 * signal management.
1460 if (interactive
&& !no_process_group
)
1461 setpgid( (pid_t
)0, (pid_t
)0);
1466 /* Don't use winbindd_reinit_after_fork here as
1467 * we're just starting up and haven't created any
1468 * winbindd-specific resources we must free yet. JRA.
1471 status
= reinit_after_fork(winbind_messaging_context(),
1472 winbind_event_context(),
1474 if (!NT_STATUS_IS_OK(status
)) {
1475 DEBUG(0,("reinit_after_fork() failed\n"));
1480 * Do not initialize the parent-child-pipe before becoming
1481 * a daemon: this is used to detect a died parent in the child
1484 status
= init_before_fork();
1485 if (!NT_STATUS_IS_OK(status
)) {
1486 DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status
)));
1490 winbindd_register_handlers(!Fork
);
1492 status
= init_system_session_info();
1493 if (!NT_STATUS_IS_OK(status
)) {
1494 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
1495 nt_errstr(status
)));
1499 rpc_lsarpc_init(NULL
);
1500 rpc_samr_init(NULL
);
1502 winbindd_init_addrchange(NULL
, winbind_event_context(),
1503 winbind_messaging_context());
1505 /* setup listen sockets */
1507 if (!winbindd_setup_listeners()) {
1508 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1513 /* Loop waiting for requests */
1515 frame
= talloc_stackframe();
1517 if (tevent_loop_once(winbind_event_context()) == -1) {
1518 DEBUG(1, ("tevent_loop_once() failed: %s\n",