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"
41 #define DBGC_CLASS DBGC_WINBIND
43 static bool client_is_idle(struct winbindd_cli_state
*state
);
44 static void remove_client(struct winbindd_cli_state
*state
);
46 static bool opt_nocache
= False
;
47 static bool interactive
= False
;
49 extern bool override_logfile
;
51 struct messaging_context
*winbind_messaging_context(void)
53 struct messaging_context
*msg_ctx
= server_messaging_context();
54 if (likely(msg_ctx
!= NULL
)) {
57 smb_panic("Could not init winbindd's messaging context.\n");
61 /* Reload configuration */
63 static bool reload_services_file(const char *lfile
)
68 const char *fname
= lp_configfile();
70 if (file_exist(fname
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
71 set_dyn_CONFIGFILE(fname
);
75 /* if this is a child, restore the logfile to the special
76 name - <domain>, idmap, etc. */
77 if (lfile
&& *lfile
) {
78 lp_set_logfile(lfile
);
82 ret
= lp_load_global(get_dyn_CONFIGFILE());
91 static void winbindd_status(void)
93 struct winbindd_cli_state
*tmp
;
95 DEBUG(0, ("winbindd status:\n"));
97 /* Print client state information */
99 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
101 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
102 DEBUG(2, ("\tclient list:\n"));
103 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
104 DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
105 (unsigned long)tmp
->pid
, tmp
->sock
,
106 client_is_idle(tmp
) ? "idle" : "active"));
111 /* Flush client cache */
113 static void flush_caches(void)
115 /* We need to invalidate cached user list entries on a SIGHUP
116 otherwise cached access denied errors due to restrict anonymous
117 hang around until the sequence number changes. */
119 if (!wcache_invalidate_cache()) {
120 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
121 if (!winbindd_cache_validate_and_initialize()) {
127 static void flush_caches_noinit(void)
130 * We need to invalidate cached user list entries on a SIGHUP
131 * otherwise cached access denied errors due to restrict anonymous
132 * hang around until the sequence number changes.
134 * Skip uninitialized domains when flush cache.
135 * If domain is not initialized, it means it is never
136 * used or never become online. look, wcache_invalidate_cache()
137 * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
138 * for unused domains and large traffic for primay domain's DC if there
142 if (!wcache_invalidate_cache_noinit()) {
143 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
144 if (!winbindd_cache_validate_and_initialize()) {
150 /* Handle the signal by unlinking socket and exiting */
152 static void terminate(bool is_parent
)
155 /* When parent goes away we should
156 * remove the socket file. Not so
157 * when children terminate.
161 if (asprintf(&path
, "%s/%s",
162 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
170 trustdom_cache_shutdown();
172 gencache_stabilize();
176 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
177 char *description
= talloc_describe_all(mem_ctx
);
179 DEBUG(3, ("tallocs left:\n%s\n", description
));
180 talloc_destroy(mem_ctx
);
185 serverid_deregister(procid_self());
192 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
193 struct tevent_signal
*se
,
199 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
201 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
202 signum
, (int)*is_parent
));
203 terminate(*is_parent
);
207 handle stdin becoming readable when we are in --foreground mode
209 static void winbindd_stdin_handler(struct tevent_context
*ev
,
210 struct tevent_fd
*fde
,
215 if (read(0, &c
, 1) != 1) {
216 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
218 /* we have reached EOF on stdin, which means the
219 parent has exited. Shutdown the server */
220 DEBUG(0,("EOF on stdin (is_parent=%d)\n",
222 terminate(*is_parent
);
226 bool winbindd_setup_sig_term_handler(bool parent
)
228 struct tevent_signal
*se
;
231 is_parent
= talloc(winbind_event_context(), bool);
238 se
= tevent_add_signal(winbind_event_context(),
241 winbindd_sig_term_handler
,
244 DEBUG(0,("failed to setup SIGTERM handler"));
245 talloc_free(is_parent
);
249 se
= tevent_add_signal(winbind_event_context(),
252 winbindd_sig_term_handler
,
255 DEBUG(0,("failed to setup SIGINT handler"));
256 talloc_free(is_parent
);
260 se
= tevent_add_signal(winbind_event_context(),
263 winbindd_sig_term_handler
,
266 DEBUG(0,("failed to setup SIGINT handler"));
267 talloc_free(is_parent
);
274 bool winbindd_setup_stdin_handler(bool parent
, bool foreground
)
279 is_parent
= talloc(winbind_event_context(), bool);
286 /* if we are running in the foreground then look for
287 EOF on stdin, and exit if it happens. This allows
288 us to die if the parent process dies
290 tevent_add_fd(winbind_event_context(), is_parent
, 0, TEVENT_FD_READ
, winbindd_stdin_handler
, is_parent
);
296 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
297 struct tevent_signal
*se
,
303 const char *file
= (const char *)private_data
;
305 DEBUG(1,("Reloading services after SIGHUP\n"));
306 flush_caches_noinit();
307 reload_services_file(file
);
310 bool winbindd_setup_sig_hup_handler(const char *lfile
)
312 struct tevent_signal
*se
;
316 file
= talloc_strdup(winbind_event_context(),
323 se
= tevent_add_signal(winbind_event_context(),
324 winbind_event_context(),
326 winbindd_sig_hup_handler
,
335 static void winbindd_sig_chld_handler(struct tevent_context
*ev
,
336 struct tevent_signal
*se
,
344 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
345 winbind_child_died(pid
);
349 static bool winbindd_setup_sig_chld_handler(void)
351 struct tevent_signal
*se
;
353 se
= tevent_add_signal(winbind_event_context(),
354 winbind_event_context(),
356 winbindd_sig_chld_handler
,
365 static void winbindd_sig_usr2_handler(struct tevent_context
*ev
,
366 struct tevent_signal
*se
,
375 static bool winbindd_setup_sig_usr2_handler(void)
377 struct tevent_signal
*se
;
379 se
= tevent_add_signal(winbind_event_context(),
380 winbind_event_context(),
382 winbindd_sig_usr2_handler
,
391 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
392 static void msg_reload_services(struct messaging_context
*msg
,
395 struct server_id server_id
,
398 /* Flush various caches */
400 reload_services_file((const char *) private_data
);
403 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
404 static void msg_shutdown(struct messaging_context
*msg
,
407 struct server_id server_id
,
410 /* only the parent waits for this message */
411 DEBUG(0,("Got shutdown message\n"));
416 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
419 struct server_id server_id
,
426 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
430 * call the validation code from a child:
431 * so we don't block the main winbindd and the validation
432 * code can safely use fork/waitpid...
434 child_pid
= sys_fork();
436 if (child_pid
== -1) {
437 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
442 if (child_pid
!= 0) {
444 DEBUG(5, ("winbind_msg_validate_cache: child created with "
445 "pid %d.\n", (int)child_pid
));
451 status
= winbindd_reinit_after_fork(NULL
, NULL
);
452 if (!NT_STATUS_IS_OK(status
)) {
453 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
458 /* install default SIGCHLD handler: validation code uses fork/waitpid */
459 CatchSignal(SIGCHLD
, SIG_DFL
);
461 ret
= (uint8
)winbindd_validate_cache_nobackup();
462 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
463 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
468 static struct winbindd_dispatch_table
{
469 enum winbindd_cmd cmd
;
470 void (*fn
)(struct winbindd_cli_state
*state
);
471 const char *winbindd_cmd_name
;
472 } dispatch_table
[] = {
474 /* Enumeration functions */
476 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
481 { WINBINDD_INFO
, winbindd_info
, "INFO" },
482 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
483 "INTERFACE_VERSION" },
484 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
485 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
486 { WINBINDD_DC_INFO
, winbindd_dc_info
, "DC_INFO" },
487 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
488 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
489 "WINBINDD_PRIV_PIPE_DIR" },
491 /* Credential cache access */
492 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
493 { WINBINDD_CCACHE_SAVE
, winbindd_ccache_save
, "CCACHE_SAVE" },
497 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
500 struct winbindd_async_dispatch_table
{
501 enum winbindd_cmd cmd
;
502 const char *cmd_name
;
503 struct tevent_req
*(*send_req
)(TALLOC_CTX
*mem_ctx
,
504 struct tevent_context
*ev
,
505 struct winbindd_cli_state
*cli
,
506 struct winbindd_request
*request
);
507 NTSTATUS (*recv_req
)(struct tevent_req
*req
,
508 struct winbindd_response
*presp
);
511 static struct winbindd_async_dispatch_table async_nonpriv_table
[] = {
512 { WINBINDD_PING
, "PING",
513 wb_ping_send
, wb_ping_recv
},
514 { WINBINDD_LOOKUPSID
, "LOOKUPSID",
515 winbindd_lookupsid_send
, winbindd_lookupsid_recv
},
516 { WINBINDD_LOOKUPSIDS
, "LOOKUPSIDS",
517 winbindd_lookupsids_send
, winbindd_lookupsids_recv
},
518 { WINBINDD_LOOKUPNAME
, "LOOKUPNAME",
519 winbindd_lookupname_send
, winbindd_lookupname_recv
},
520 { WINBINDD_SID_TO_UID
, "SID_TO_UID",
521 winbindd_sid_to_uid_send
, winbindd_sid_to_uid_recv
},
522 { WINBINDD_SID_TO_GID
, "SID_TO_GID",
523 winbindd_sid_to_gid_send
, winbindd_sid_to_gid_recv
},
524 { WINBINDD_UID_TO_SID
, "UID_TO_SID",
525 winbindd_uid_to_sid_send
, winbindd_uid_to_sid_recv
},
526 { WINBINDD_GID_TO_SID
, "GID_TO_SID",
527 winbindd_gid_to_sid_send
, winbindd_gid_to_sid_recv
},
528 { WINBINDD_SIDS_TO_XIDS
, "SIDS_TO_XIDS",
529 winbindd_sids_to_xids_send
, winbindd_sids_to_xids_recv
},
530 { WINBINDD_GETPWSID
, "GETPWSID",
531 winbindd_getpwsid_send
, winbindd_getpwsid_recv
},
532 { WINBINDD_GETPWNAM
, "GETPWNAM",
533 winbindd_getpwnam_send
, winbindd_getpwnam_recv
},
534 { WINBINDD_GETPWUID
, "GETPWUID",
535 winbindd_getpwuid_send
, winbindd_getpwuid_recv
},
536 { WINBINDD_GETSIDALIASES
, "GETSIDALIASES",
537 winbindd_getsidaliases_send
, winbindd_getsidaliases_recv
},
538 { WINBINDD_GETUSERDOMGROUPS
, "GETUSERDOMGROUPS",
539 winbindd_getuserdomgroups_send
, winbindd_getuserdomgroups_recv
},
540 { WINBINDD_GETGROUPS
, "GETGROUPS",
541 winbindd_getgroups_send
, winbindd_getgroups_recv
},
542 { WINBINDD_SHOW_SEQUENCE
, "SHOW_SEQUENCE",
543 winbindd_show_sequence_send
, winbindd_show_sequence_recv
},
544 { WINBINDD_GETGRGID
, "GETGRGID",
545 winbindd_getgrgid_send
, winbindd_getgrgid_recv
},
546 { WINBINDD_GETGRNAM
, "GETGRNAM",
547 winbindd_getgrnam_send
, winbindd_getgrnam_recv
},
548 { WINBINDD_GETUSERSIDS
, "GETUSERSIDS",
549 winbindd_getusersids_send
, winbindd_getusersids_recv
},
550 { WINBINDD_LOOKUPRIDS
, "LOOKUPRIDS",
551 winbindd_lookuprids_send
, winbindd_lookuprids_recv
},
552 { WINBINDD_SETPWENT
, "SETPWENT",
553 winbindd_setpwent_send
, winbindd_setpwent_recv
},
554 { WINBINDD_GETPWENT
, "GETPWENT",
555 winbindd_getpwent_send
, winbindd_getpwent_recv
},
556 { WINBINDD_ENDPWENT
, "ENDPWENT",
557 winbindd_endpwent_send
, winbindd_endpwent_recv
},
558 { WINBINDD_DSGETDCNAME
, "DSGETDCNAME",
559 winbindd_dsgetdcname_send
, winbindd_dsgetdcname_recv
},
560 { WINBINDD_GETDCNAME
, "GETDCNAME",
561 winbindd_getdcname_send
, winbindd_getdcname_recv
},
562 { WINBINDD_SETGRENT
, "SETGRENT",
563 winbindd_setgrent_send
, winbindd_setgrent_recv
},
564 { WINBINDD_GETGRENT
, "GETGRENT",
565 winbindd_getgrent_send
, winbindd_getgrent_recv
},
566 { WINBINDD_ENDGRENT
, "ENDGRENT",
567 winbindd_endgrent_send
, winbindd_endgrent_recv
},
568 { WINBINDD_LIST_USERS
, "LIST_USERS",
569 winbindd_list_users_send
, winbindd_list_users_recv
},
570 { WINBINDD_LIST_GROUPS
, "LIST_GROUPS",
571 winbindd_list_groups_send
, winbindd_list_groups_recv
},
572 { WINBINDD_CHECK_MACHACC
, "CHECK_MACHACC",
573 winbindd_check_machine_acct_send
, winbindd_check_machine_acct_recv
},
574 { WINBINDD_PING_DC
, "PING_DC",
575 winbindd_ping_dc_send
, winbindd_ping_dc_recv
},
576 { WINBINDD_PAM_AUTH
, "PAM_AUTH",
577 winbindd_pam_auth_send
, winbindd_pam_auth_recv
},
578 { WINBINDD_PAM_LOGOFF
, "PAM_LOGOFF",
579 winbindd_pam_logoff_send
, winbindd_pam_logoff_recv
},
580 { WINBINDD_PAM_CHAUTHTOK
, "PAM_CHAUTHTOK",
581 winbindd_pam_chauthtok_send
, winbindd_pam_chauthtok_recv
},
582 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, "PAM_CHNG_PSWD_AUTH_CRAP",
583 winbindd_pam_chng_pswd_auth_crap_send
,
584 winbindd_pam_chng_pswd_auth_crap_recv
},
585 { WINBINDD_WINS_BYIP
, "WINS_BYIP",
586 winbindd_wins_byip_send
, winbindd_wins_byip_recv
},
587 { WINBINDD_WINS_BYNAME
, "WINS_BYNAME",
588 winbindd_wins_byname_send
, winbindd_wins_byname_recv
},
590 { 0, NULL
, NULL
, NULL
}
593 static struct winbindd_async_dispatch_table async_priv_table
[] = {
594 { WINBINDD_ALLOCATE_UID
, "ALLOCATE_UID",
595 winbindd_allocate_uid_send
, winbindd_allocate_uid_recv
},
596 { WINBINDD_ALLOCATE_GID
, "ALLOCATE_GID",
597 winbindd_allocate_gid_send
, winbindd_allocate_gid_recv
},
598 { WINBINDD_CHANGE_MACHACC
, "CHANGE_MACHACC",
599 winbindd_change_machine_acct_send
, winbindd_change_machine_acct_recv
},
600 { WINBINDD_PAM_AUTH_CRAP
, "PAM_AUTH_CRAP",
601 winbindd_pam_auth_crap_send
, winbindd_pam_auth_crap_recv
},
603 { 0, NULL
, NULL
, NULL
}
606 static void wb_request_done(struct tevent_req
*req
);
608 static void process_request(struct winbindd_cli_state
*state
)
610 struct winbindd_dispatch_table
*table
= dispatch_table
;
611 struct winbindd_async_dispatch_table
*atable
;
613 state
->mem_ctx
= talloc_named(state
, 0, "winbind request");
614 if (state
->mem_ctx
== NULL
)
617 /* Remember who asked us. */
618 state
->pid
= state
->request
->pid
;
620 state
->cmd_name
= "unknown request";
621 state
->recv_fn
= NULL
;
623 /* Process command */
625 for (atable
= async_nonpriv_table
; atable
->send_req
; atable
+= 1) {
626 if (state
->request
->cmd
== atable
->cmd
) {
631 if ((atable
->send_req
== NULL
) && state
->privileged
) {
632 for (atable
= async_priv_table
; atable
->send_req
;
634 if (state
->request
->cmd
== atable
->cmd
) {
640 if (atable
->send_req
!= NULL
) {
641 struct tevent_req
*req
;
643 state
->cmd_name
= atable
->cmd_name
;
644 state
->recv_fn
= atable
->recv_req
;
646 DEBUG(10, ("process_request: Handling async request %d:%s\n",
647 (int)state
->pid
, state
->cmd_name
));
649 req
= atable
->send_req(state
->mem_ctx
, winbind_event_context(),
650 state
, state
->request
);
652 DEBUG(0, ("process_request: atable->send failed for "
653 "%s\n", atable
->cmd_name
));
654 request_error(state
);
657 tevent_req_set_callback(req
, wb_request_done
, state
);
661 state
->response
= talloc_zero(state
->mem_ctx
,
662 struct winbindd_response
);
663 if (state
->response
== NULL
) {
664 DEBUG(10, ("talloc failed\n"));
665 remove_client(state
);
668 state
->response
->result
= WINBINDD_PENDING
;
669 state
->response
->length
= sizeof(struct winbindd_response
);
671 for (table
= dispatch_table
; table
->fn
; table
++) {
672 if (state
->request
->cmd
== table
->cmd
) {
673 DEBUG(10,("process_request: request fn %s\n",
674 table
->winbindd_cmd_name
));
675 state
->cmd_name
= table
->winbindd_cmd_name
;
682 DEBUG(10,("process_request: unknown request fn number %d\n",
683 (int)state
->request
->cmd
));
684 request_error(state
);
688 static void wb_request_done(struct tevent_req
*req
)
690 struct winbindd_cli_state
*state
= tevent_req_callback_data(
691 req
, struct winbindd_cli_state
);
694 state
->response
= talloc_zero(state
->mem_ctx
,
695 struct winbindd_response
);
696 if (state
->response
== NULL
) {
697 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
698 (int)state
->pid
, state
->cmd_name
));
699 remove_client(state
);
702 state
->response
->result
= WINBINDD_PENDING
;
703 state
->response
->length
= sizeof(struct winbindd_response
);
705 status
= state
->recv_fn(req
, state
->response
);
708 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
709 (int)state
->pid
, state
->cmd_name
, nt_errstr(status
)));
711 if (!NT_STATUS_IS_OK(status
)) {
712 request_error(state
);
719 * This is the main event loop of winbind requests. It goes through a
720 * state-machine of 3 read/write requests, 4 if you have extra data to send.
722 * An idle winbind client has a read request of 4 bytes outstanding,
723 * finalizing function is request_len_recv, checking the length. request_recv
724 * then processes the packet. The processing function then at some point has
725 * to call request_finished which schedules sending the response.
728 static void request_finished(struct winbindd_cli_state
*state
);
730 static void winbind_client_request_read(struct tevent_req
*req
);
731 static void winbind_client_response_written(struct tevent_req
*req
);
733 static void request_finished(struct winbindd_cli_state
*state
)
735 struct tevent_req
*req
;
737 TALLOC_FREE(state
->request
);
739 req
= wb_resp_write_send(state
, winbind_event_context(),
740 state
->out_queue
, state
->sock
,
743 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
744 (int)state
->pid
, state
->cmd_name
));
745 remove_client(state
);
748 tevent_req_set_callback(req
, winbind_client_response_written
, state
);
751 static void winbind_client_response_written(struct tevent_req
*req
)
753 struct winbindd_cli_state
*state
= tevent_req_callback_data(
754 req
, struct winbindd_cli_state
);
758 ret
= wb_resp_write_recv(req
, &err
);
763 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
764 (int)state
->pid
, state
->cmd_name
, strerror(err
)));
765 remove_client(state
);
769 DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
770 "to client\n", (int)state
->pid
, state
->cmd_name
));
772 TALLOC_FREE(state
->mem_ctx
);
773 state
->response
= NULL
;
774 state
->cmd_name
= "no request";
775 state
->recv_fn
= NULL
;
777 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
778 WINBINDD_MAX_EXTRA_DATA
);
780 remove_client(state
);
783 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
786 void request_error(struct winbindd_cli_state
*state
)
788 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
789 state
->response
->result
= WINBINDD_ERROR
;
790 request_finished(state
);
793 void request_ok(struct winbindd_cli_state
*state
)
795 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
796 state
->response
->result
= WINBINDD_OK
;
797 request_finished(state
);
800 /* Process a new connection by adding it to the client connection list */
802 static void new_connection(int listen_sock
, bool privileged
)
804 struct sockaddr_un sunaddr
;
805 struct winbindd_cli_state
*state
;
806 struct tevent_req
*req
;
810 /* Accept connection */
812 len
= sizeof(sunaddr
);
814 sock
= accept(listen_sock
, (struct sockaddr
*)(void *)&sunaddr
, &len
);
817 if (errno
!= EINTR
) {
818 DEBUG(0, ("Faild to accept socket - %s\n",
824 DEBUG(6,("accepted socket %d\n", sock
));
826 /* Create new connection structure */
828 if ((state
= talloc_zero(NULL
, struct winbindd_cli_state
)) == NULL
) {
835 state
->out_queue
= tevent_queue_create(state
, "winbind client reply");
836 if (state
->out_queue
== NULL
) {
842 state
->last_access
= time(NULL
);
844 state
->privileged
= privileged
;
846 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
847 WINBINDD_MAX_EXTRA_DATA
);
853 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
855 /* Add to connection list */
857 winbindd_add_client(state
);
860 static void winbind_client_request_read(struct tevent_req
*req
)
862 struct winbindd_cli_state
*state
= tevent_req_callback_data(
863 req
, struct winbindd_cli_state
);
867 ret
= wb_req_read_recv(req
, state
, &state
->request
, &err
);
871 DEBUG(6, ("closing socket %d, client exited\n",
874 DEBUG(2, ("Could not read client request from fd %d: "
875 "%s\n", state
->sock
, strerror(err
)));
879 remove_client(state
);
882 process_request(state
);
885 /* Remove a client connection from client connection list */
887 static void remove_client(struct winbindd_cli_state
*state
)
892 /* It's a dead client - hold a funeral */
898 if (state
->sock
!= -1) {
899 /* tell client, we are closing ... */
900 nwritten
= write(state
->sock
, &c
, sizeof(c
));
901 if (nwritten
== -1) {
902 DEBUG(2, ("final write to client failed: %s\n",
912 TALLOC_FREE(state
->mem_ctx
);
914 /* Remove from list and free */
916 winbindd_remove_client(state
);
920 /* Is a client idle? */
922 static bool client_is_idle(struct winbindd_cli_state
*state
) {
923 return (state
->response
== NULL
&&
924 !state
->pwent_state
&& !state
->grent_state
);
927 /* Shutdown client connection which has been idle for the longest time */
929 static bool remove_idle_client(void)
931 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
932 time_t last_access
= 0;
935 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
936 if (client_is_idle(state
)) {
938 if (!last_access
|| state
->last_access
< last_access
) {
939 last_access
= state
->last_access
;
940 remove_state
= state
;
946 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
947 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
948 remove_client(remove_state
);
955 struct winbindd_listen_state
{
960 static void winbindd_listen_fde_handler(struct tevent_context
*ev
,
961 struct tevent_fd
*fde
,
965 struct winbindd_listen_state
*s
= talloc_get_type_abort(private_data
,
966 struct winbindd_listen_state
);
968 while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
969 DEBUG(5,("winbindd: Exceeding %d client "
970 "connections, removing idle "
971 "connection.\n", lp_winbind_max_clients()));
972 if (!remove_idle_client()) {
973 DEBUG(0,("winbindd: Exceeding %d "
974 "client connections, no idle "
975 "connection found\n",
976 lp_winbind_max_clients()));
980 new_connection(s
->fd
, s
->privileged
);
984 * Winbindd socket accessor functions
987 const char *get_winbind_pipe_dir(void)
989 return lp_parm_const_string(-1, "winbindd", "socket dir", get_dyn_WINBINDD_SOCKET_DIR());
992 char *get_winbind_priv_pipe_dir(void)
994 return state_path(WINBINDD_PRIV_SOCKET_SUBDIR
);
997 static bool winbindd_setup_listeners(void)
999 struct winbindd_listen_state
*pub_state
= NULL
;
1000 struct winbindd_listen_state
*priv_state
= NULL
;
1001 struct tevent_fd
*fde
;
1004 pub_state
= talloc(winbind_event_context(),
1005 struct winbindd_listen_state
);
1010 pub_state
->privileged
= false;
1011 pub_state
->fd
= create_pipe_sock(
1012 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
, 0755);
1013 if (pub_state
->fd
== -1) {
1016 rc
= listen(pub_state
->fd
, 5);
1021 fde
= tevent_add_fd(winbind_event_context(), pub_state
, pub_state
->fd
,
1022 TEVENT_FD_READ
, winbindd_listen_fde_handler
,
1025 close(pub_state
->fd
);
1028 tevent_fd_set_auto_close(fde
);
1030 priv_state
= talloc(winbind_event_context(),
1031 struct winbindd_listen_state
);
1036 priv_state
->privileged
= true;
1037 priv_state
->fd
= create_pipe_sock(
1038 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME
, 0750);
1039 if (priv_state
->fd
== -1) {
1042 rc
= listen(priv_state
->fd
, 5);
1047 fde
= tevent_add_fd(winbind_event_context(), priv_state
,
1048 priv_state
->fd
, TEVENT_FD_READ
,
1049 winbindd_listen_fde_handler
, priv_state
);
1051 close(priv_state
->fd
);
1054 tevent_fd_set_auto_close(fde
);
1058 TALLOC_FREE(pub_state
);
1059 TALLOC_FREE(priv_state
);
1063 bool winbindd_use_idmap_cache(void)
1065 return !opt_nocache
;
1068 bool winbindd_use_cache(void)
1070 return !opt_nocache
;
1073 void winbindd_register_handlers(bool foreground
)
1075 /* Setup signal handlers */
1077 if (!winbindd_setup_sig_term_handler(true))
1079 if (!winbindd_setup_stdin_handler(true, foreground
))
1081 if (!winbindd_setup_sig_hup_handler(NULL
))
1083 if (!winbindd_setup_sig_chld_handler())
1085 if (!winbindd_setup_sig_usr2_handler())
1088 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1091 * Ensure all cache and idmap caches are consistent
1092 * and initialized before we startup.
1094 if (!winbindd_cache_validate_and_initialize()) {
1098 /* get broadcast messages */
1100 if (!serverid_register(procid_self(),
1104 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1108 /* React on 'smbcontrol winbindd reload-config' in the same way
1109 as to SIGHUP signal */
1110 messaging_register(winbind_messaging_context(), NULL
,
1111 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1112 messaging_register(winbind_messaging_context(), NULL
,
1113 MSG_SHUTDOWN
, msg_shutdown
);
1115 /* Handle online/offline messages. */
1116 messaging_register(winbind_messaging_context(), NULL
,
1117 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1118 messaging_register(winbind_messaging_context(), NULL
,
1119 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1120 messaging_register(winbind_messaging_context(), NULL
,
1121 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1123 messaging_register(winbind_messaging_context(), NULL
,
1124 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1126 messaging_register(winbind_messaging_context(), NULL
,
1127 MSG_WINBIND_VALIDATE_CACHE
,
1128 winbind_msg_validate_cache
);
1130 messaging_register(winbind_messaging_context(), NULL
,
1131 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1132 winbind_msg_dump_domain_list
);
1134 messaging_register(winbind_messaging_context(), NULL
,
1135 MSG_WINBIND_IP_DROPPED
,
1136 winbind_msg_ip_dropped_parent
);
1138 /* Register handler for MSG_DEBUG. */
1139 messaging_register(winbind_messaging_context(), NULL
,
1143 netsamlogon_cache_init(); /* Non-critical */
1145 /* clear the cached list of trusted domains */
1149 if (!init_domain_list()) {
1150 DEBUG(0,("unable to initialize domain list\n"));
1155 init_locator_child();
1157 smb_nscd_flush_user_cache();
1158 smb_nscd_flush_group_cache();
1160 if (lp_allow_trusted_domains()) {
1161 if (tevent_add_timer(winbind_event_context(), NULL
, timeval_zero(),
1162 rescan_trusted_domains
, NULL
) == NULL
) {
1163 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1170 struct winbindd_addrchanged_state
{
1171 struct addrchange_context
*ctx
;
1172 struct tevent_context
*ev
;
1173 struct messaging_context
*msg_ctx
;
1176 static void winbindd_addr_changed(struct tevent_req
*req
);
1178 static void winbindd_init_addrchange(TALLOC_CTX
*mem_ctx
,
1179 struct tevent_context
*ev
,
1180 struct messaging_context
*msg_ctx
)
1182 struct winbindd_addrchanged_state
*state
;
1183 struct tevent_req
*req
;
1186 state
= talloc(mem_ctx
, struct winbindd_addrchanged_state
);
1187 if (state
== NULL
) {
1188 DEBUG(10, ("talloc failed\n"));
1192 state
->msg_ctx
= msg_ctx
;
1194 status
= addrchange_context_create(state
, &state
->ctx
);
1195 if (!NT_STATUS_IS_OK(status
)) {
1196 DEBUG(10, ("addrchange_context_create failed: %s\n",
1197 nt_errstr(status
)));
1201 req
= addrchange_send(state
, ev
, state
->ctx
);
1203 DEBUG(0, ("addrchange_send failed\n"));
1207 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1210 static void winbindd_addr_changed(struct tevent_req
*req
)
1212 struct winbindd_addrchanged_state
*state
= tevent_req_callback_data(
1213 req
, struct winbindd_addrchanged_state
);
1214 enum addrchange_type type
;
1215 struct sockaddr_storage addr
;
1218 status
= addrchange_recv(req
, &type
, &addr
);
1220 if (!NT_STATUS_IS_OK(status
)) {
1221 DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
1222 nt_errstr(status
)));
1226 if (type
== ADDRCHANGE_DEL
) {
1227 char addrstr
[INET6_ADDRSTRLEN
];
1230 print_sockaddr(addrstr
, sizeof(addrstr
), &addr
);
1232 DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
1235 blob
= data_blob_const(addrstr
, strlen(addrstr
)+1);
1237 status
= messaging_send(state
->msg_ctx
,
1238 messaging_server_id(state
->msg_ctx
),
1239 MSG_WINBIND_IP_DROPPED
, &blob
);
1240 if (!NT_STATUS_IS_OK(status
)) {
1241 DEBUG(10, ("messaging_send failed: %s - ignoring\n",
1242 nt_errstr(status
)));
1245 req
= addrchange_send(state
, state
->ev
, state
->ctx
);
1247 DEBUG(0, ("addrchange_send failed\n"));
1251 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1256 int main(int argc
, char **argv
, char **envp
)
1258 static bool is_daemon
= False
;
1259 static bool Fork
= True
;
1260 static bool log_stdout
= False
;
1261 static bool no_process_group
= False
;
1265 OPT_NO_PROCESS_GROUP
,
1268 struct poptOption long_options
[] = {
1270 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1271 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1272 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1273 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1274 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1275 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1285 * Do this before any other talloc operation
1287 talloc_enable_null_tracking();
1288 frame
= talloc_stackframe();
1290 setup_logging("winbindd", DEBUG_DEFAULT_STDOUT
);
1292 /* glibc (?) likes to print "User defined signal 1" and exit if a
1293 SIGUSR[12] is received before a handler is installed */
1295 CatchSignal(SIGUSR1
, SIG_IGN
);
1296 CatchSignal(SIGUSR2
, SIG_IGN
);
1299 dump_core_setup("winbindd", lp_logfile());
1303 /* Initialise for running in non-root mode */
1307 set_remote_machine_name("winbindd", False
);
1309 /* Set environment variable so we don't recursively call ourselves.
1310 This may also be useful interactively. */
1312 if ( !winbind_off() ) {
1313 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1317 /* Initialise samba/rpc client stuff */
1319 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1321 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1323 /* Don't become a daemon */
1335 case OPT_NO_PROCESS_GROUP
:
1336 no_process_group
= true;
1338 case OPT_LOG_STDOUT
:
1345 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1346 poptBadOption(pc
, 0), poptStrerror(opt
));
1347 poptPrintUsage(pc
, stderr
, 0);
1352 if (is_daemon
&& interactive
) {
1353 d_fprintf(stderr
,"\nERROR: "
1354 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1355 poptPrintUsage(pc
, stderr
, 0);
1359 if (log_stdout
&& Fork
) {
1360 d_fprintf(stderr
, "\nERROR: "
1361 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1362 poptPrintUsage(pc
, stderr
, 0);
1366 poptFreeContext(pc
);
1368 if (!override_logfile
) {
1370 if (asprintf(&lfile
,"%s/log.winbindd",
1371 get_dyn_LOGFILEBASE()) > 0) {
1372 lp_set_logfile(lfile
);
1378 setup_logging("winbindd", DEBUG_STDOUT
);
1380 setup_logging("winbindd", DEBUG_FILE
);
1384 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1385 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1387 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1388 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1392 /* Initialise messaging system */
1394 if (winbind_messaging_context() == NULL
) {
1398 if (!reload_services_file(NULL
)) {
1399 DEBUG(0, ("error opening config file\n"));
1403 if (!directory_exist(lp_lockdir())) {
1404 mkdir(lp_lockdir(), 0755);
1414 if (!secrets_init()) {
1416 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1420 /* Unblock all signals we are interested in as they may have been
1421 blocked by the parent process. */
1423 BlockSignals(False
, SIGINT
);
1424 BlockSignals(False
, SIGQUIT
);
1425 BlockSignals(False
, SIGTERM
);
1426 BlockSignals(False
, SIGUSR1
);
1427 BlockSignals(False
, SIGUSR2
);
1428 BlockSignals(False
, SIGHUP
);
1429 BlockSignals(False
, SIGCHLD
);
1432 become_daemon(Fork
, no_process_group
, log_stdout
);
1434 pidfile_create("winbindd");
1438 * If we're interactive we want to set our own process group for
1439 * signal management.
1441 if (interactive
&& !no_process_group
)
1442 setpgid( (pid_t
)0, (pid_t
)0);
1447 /* Don't use winbindd_reinit_after_fork here as
1448 * we're just starting up and haven't created any
1449 * winbindd-specific resources we must free yet. JRA.
1452 status
= reinit_after_fork(winbind_messaging_context(),
1453 winbind_event_context(),
1455 if (!NT_STATUS_IS_OK(status
)) {
1456 DEBUG(0,("reinit_after_fork() failed\n"));
1460 winbindd_register_handlers(!Fork
);
1462 status
= init_system_info();
1463 if (!NT_STATUS_IS_OK(status
)) {
1464 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
1465 nt_errstr(status
)));
1469 rpc_lsarpc_init(NULL
);
1470 rpc_samr_init(NULL
);
1472 winbindd_init_addrchange(NULL
, winbind_event_context(),
1473 winbind_messaging_context());
1475 /* setup listen sockets */
1477 if (!winbindd_setup_listeners()) {
1478 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1483 /* Loop waiting for requests */
1485 frame
= talloc_stackframe();
1487 if (tevent_loop_once(winbind_event_context()) == -1) {
1488 DEBUG(1, ("tevent_loop_once() failed: %s\n",