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 tevent_context
*winbind_event_context(void)
54 static struct tevent_context
*ev
= NULL
;
61 * Note we MUST use the NULL context here, not the autofree context,
62 * to avoid side effects in forked children exiting.
64 ev
= samba_tevent_context_init(NULL
);
66 smb_panic("Could not init winbindd's messaging context.\n");
71 struct messaging_context
*winbind_messaging_context(void)
73 static struct messaging_context
*msg
= NULL
;
80 * Note we MUST use the NULL context here, not the autofree context,
81 * to avoid side effects in forked children exiting.
83 msg
= messaging_init(NULL
, winbind_event_context());
85 smb_panic("Could not init winbindd's messaging context.\n");
90 /* Reload configuration */
92 static bool reload_services_file(const char *lfile
)
97 char *fname
= lp_configfile(talloc_tos());
99 if (file_exist(fname
) && !strcsequal(fname
,get_dyn_CONFIGFILE())) {
100 set_dyn_CONFIGFILE(fname
);
105 /* if this is a child, restore the logfile to the special
106 name - <domain>, idmap, etc. */
107 if (lfile
&& *lfile
) {
108 lp_set_logfile(lfile
);
112 ret
= lp_load_global(get_dyn_CONFIGFILE());
121 static void winbindd_status(void)
123 struct winbindd_cli_state
*tmp
;
125 DEBUG(0, ("winbindd status:\n"));
127 /* Print client state information */
129 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
131 if (DEBUGLEVEL
>= 2 && winbindd_num_clients()) {
132 DEBUG(2, ("\tclient list:\n"));
133 for(tmp
= winbindd_client_list(); tmp
; tmp
= tmp
->next
) {
134 DEBUGADD(2, ("\t\tpid %lu, sock %d (%s)\n",
135 (unsigned long)tmp
->pid
, tmp
->sock
,
136 client_is_idle(tmp
) ? "idle" : "active"));
141 /* Flush client cache */
143 static void flush_caches(void)
145 /* We need to invalidate cached user list entries on a SIGHUP
146 otherwise cached access denied errors due to restrict anonymous
147 hang around until the sequence number changes. */
149 if (!wcache_invalidate_cache()) {
150 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
151 if (!winbindd_cache_validate_and_initialize()) {
157 static void flush_caches_noinit(void)
160 * We need to invalidate cached user list entries on a SIGHUP
161 * otherwise cached access denied errors due to restrict anonymous
162 * hang around until the sequence number changes.
164 * Skip uninitialized domains when flush cache.
165 * If domain is not initialized, it means it is never
166 * used or never become online. look, wcache_invalidate_cache()
167 * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
168 * for unused domains and large traffic for primay domain's DC if there
172 if (!wcache_invalidate_cache_noinit()) {
173 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
174 if (!winbindd_cache_validate_and_initialize()) {
180 /* Handle the signal by unlinking socket and exiting */
182 static void terminate(bool is_parent
)
185 /* When parent goes away we should
186 * remove the socket file. Not so
187 * when children terminate.
191 if (asprintf(&path
, "%s/%s",
192 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
) > 0) {
200 trustdom_cache_shutdown();
202 gencache_stabilize();
206 TALLOC_CTX
*mem_ctx
= talloc_init("end_description");
207 char *description
= talloc_describe_all(mem_ctx
);
209 DEBUG(3, ("tallocs left:\n%s\n", description
));
210 talloc_destroy(mem_ctx
);
215 serverid_deregister(procid_self());
216 pidfile_unlink(lp_piddir(), "winbindd");
222 static void winbindd_sig_term_handler(struct tevent_context
*ev
,
223 struct tevent_signal
*se
,
229 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
231 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
232 signum
, (int)*is_parent
));
233 terminate(*is_parent
);
237 handle stdin becoming readable when we are in --foreground mode
239 static void winbindd_stdin_handler(struct tevent_context
*ev
,
240 struct tevent_fd
*fde
,
245 if (read(0, &c
, 1) != 1) {
246 bool *is_parent
= talloc_get_type_abort(private_data
, bool);
248 /* we have reached EOF on stdin, which means the
249 parent has exited. Shutdown the server */
250 DEBUG(0,("EOF on stdin (is_parent=%d)\n",
252 terminate(*is_parent
);
256 bool winbindd_setup_sig_term_handler(bool parent
)
258 struct tevent_signal
*se
;
261 is_parent
= talloc(winbind_event_context(), bool);
268 se
= tevent_add_signal(winbind_event_context(),
271 winbindd_sig_term_handler
,
274 DEBUG(0,("failed to setup SIGTERM handler"));
275 talloc_free(is_parent
);
279 se
= tevent_add_signal(winbind_event_context(),
282 winbindd_sig_term_handler
,
285 DEBUG(0,("failed to setup SIGINT handler"));
286 talloc_free(is_parent
);
290 se
= tevent_add_signal(winbind_event_context(),
293 winbindd_sig_term_handler
,
296 DEBUG(0,("failed to setup SIGINT handler"));
297 talloc_free(is_parent
);
304 bool winbindd_setup_stdin_handler(bool parent
, bool foreground
)
309 is_parent
= talloc(winbind_event_context(), bool);
316 /* if we are running in the foreground then look for
317 EOF on stdin, and exit if it happens. This allows
318 us to die if the parent process dies
320 tevent_add_fd(winbind_event_context(), is_parent
, 0, TEVENT_FD_READ
, winbindd_stdin_handler
, is_parent
);
326 static void winbindd_sig_hup_handler(struct tevent_context
*ev
,
327 struct tevent_signal
*se
,
333 const char *file
= (const char *)private_data
;
335 DEBUG(1,("Reloading services after SIGHUP\n"));
336 flush_caches_noinit();
337 reload_services_file(file
);
340 bool winbindd_setup_sig_hup_handler(const char *lfile
)
342 struct tevent_signal
*se
;
346 file
= talloc_strdup(winbind_event_context(),
353 se
= tevent_add_signal(winbind_event_context(),
354 winbind_event_context(),
356 winbindd_sig_hup_handler
,
365 static void winbindd_sig_chld_handler(struct tevent_context
*ev
,
366 struct tevent_signal
*se
,
374 while ((pid
= sys_waitpid(-1, NULL
, WNOHANG
)) > 0) {
375 winbind_child_died(pid
);
379 static bool winbindd_setup_sig_chld_handler(void)
381 struct tevent_signal
*se
;
383 se
= tevent_add_signal(winbind_event_context(),
384 winbind_event_context(),
386 winbindd_sig_chld_handler
,
395 static void winbindd_sig_usr2_handler(struct tevent_context
*ev
,
396 struct tevent_signal
*se
,
405 static bool winbindd_setup_sig_usr2_handler(void)
407 struct tevent_signal
*se
;
409 se
= tevent_add_signal(winbind_event_context(),
410 winbind_event_context(),
412 winbindd_sig_usr2_handler
,
421 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
422 static void msg_reload_services(struct messaging_context
*msg
,
425 struct server_id server_id
,
428 /* Flush various caches */
430 reload_services_file((const char *) private_data
);
433 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
434 static void msg_shutdown(struct messaging_context
*msg
,
437 struct server_id server_id
,
440 /* only the parent waits for this message */
441 DEBUG(0,("Got shutdown message\n"));
446 static void winbind_msg_validate_cache(struct messaging_context
*msg_ctx
,
449 struct server_id server_id
,
456 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
460 * call the validation code from a child:
461 * so we don't block the main winbindd and the validation
462 * code can safely use fork/waitpid...
466 if (child_pid
== -1) {
467 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
472 if (child_pid
!= 0) {
474 DEBUG(5, ("winbind_msg_validate_cache: child created with "
475 "pid %d.\n", (int)child_pid
));
481 status
= winbindd_reinit_after_fork(NULL
, NULL
);
482 if (!NT_STATUS_IS_OK(status
)) {
483 DEBUG(1, ("winbindd_reinit_after_fork failed: %s\n",
488 /* install default SIGCHLD handler: validation code uses fork/waitpid */
489 CatchSignal(SIGCHLD
, SIG_DFL
);
491 ret
= (uint8
)winbindd_validate_cache_nobackup();
492 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret
));
493 messaging_send_buf(msg_ctx
, server_id
, MSG_WINBIND_VALIDATE_CACHE
, &ret
,
498 static struct winbindd_dispatch_table
{
499 enum winbindd_cmd cmd
;
500 void (*fn
)(struct winbindd_cli_state
*state
);
501 const char *winbindd_cmd_name
;
502 } dispatch_table
[] = {
504 /* Enumeration functions */
506 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
,
511 { WINBINDD_INFO
, winbindd_info
, "INFO" },
512 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
,
513 "INTERFACE_VERSION" },
514 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
515 { WINBINDD_DOMAIN_INFO
, winbindd_domain_info
, "DOMAIN_INFO" },
516 { WINBINDD_DC_INFO
, winbindd_dc_info
, "DC_INFO" },
517 { WINBINDD_NETBIOS_NAME
, winbindd_netbios_name
, "NETBIOS_NAME" },
518 { WINBINDD_PRIV_PIPE_DIR
, winbindd_priv_pipe_dir
,
519 "WINBINDD_PRIV_PIPE_DIR" },
521 /* Credential cache access */
522 { WINBINDD_CCACHE_NTLMAUTH
, winbindd_ccache_ntlm_auth
, "NTLMAUTH" },
523 { WINBINDD_CCACHE_SAVE
, winbindd_ccache_save
, "CCACHE_SAVE" },
527 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
530 struct winbindd_async_dispatch_table
{
531 enum winbindd_cmd cmd
;
532 const char *cmd_name
;
533 struct tevent_req
*(*send_req
)(TALLOC_CTX
*mem_ctx
,
534 struct tevent_context
*ev
,
535 struct winbindd_cli_state
*cli
,
536 struct winbindd_request
*request
);
537 NTSTATUS (*recv_req
)(struct tevent_req
*req
,
538 struct winbindd_response
*presp
);
541 static struct winbindd_async_dispatch_table async_nonpriv_table
[] = {
542 { WINBINDD_PING
, "PING",
543 wb_ping_send
, wb_ping_recv
},
544 { WINBINDD_LOOKUPSID
, "LOOKUPSID",
545 winbindd_lookupsid_send
, winbindd_lookupsid_recv
},
546 { WINBINDD_LOOKUPSIDS
, "LOOKUPSIDS",
547 winbindd_lookupsids_send
, winbindd_lookupsids_recv
},
548 { WINBINDD_LOOKUPNAME
, "LOOKUPNAME",
549 winbindd_lookupname_send
, winbindd_lookupname_recv
},
550 { WINBINDD_SID_TO_UID
, "SID_TO_UID",
551 winbindd_sid_to_uid_send
, winbindd_sid_to_uid_recv
},
552 { WINBINDD_SID_TO_GID
, "SID_TO_GID",
553 winbindd_sid_to_gid_send
, winbindd_sid_to_gid_recv
},
554 { WINBINDD_UID_TO_SID
, "UID_TO_SID",
555 winbindd_uid_to_sid_send
, winbindd_uid_to_sid_recv
},
556 { WINBINDD_GID_TO_SID
, "GID_TO_SID",
557 winbindd_gid_to_sid_send
, winbindd_gid_to_sid_recv
},
558 { WINBINDD_SIDS_TO_XIDS
, "SIDS_TO_XIDS",
559 winbindd_sids_to_xids_send
, winbindd_sids_to_xids_recv
},
560 { WINBINDD_GETPWSID
, "GETPWSID",
561 winbindd_getpwsid_send
, winbindd_getpwsid_recv
},
562 { WINBINDD_GETPWNAM
, "GETPWNAM",
563 winbindd_getpwnam_send
, winbindd_getpwnam_recv
},
564 { WINBINDD_GETPWUID
, "GETPWUID",
565 winbindd_getpwuid_send
, winbindd_getpwuid_recv
},
566 { WINBINDD_GETSIDALIASES
, "GETSIDALIASES",
567 winbindd_getsidaliases_send
, winbindd_getsidaliases_recv
},
568 { WINBINDD_GETUSERDOMGROUPS
, "GETUSERDOMGROUPS",
569 winbindd_getuserdomgroups_send
, winbindd_getuserdomgroups_recv
},
570 { WINBINDD_GETGROUPS
, "GETGROUPS",
571 winbindd_getgroups_send
, winbindd_getgroups_recv
},
572 { WINBINDD_SHOW_SEQUENCE
, "SHOW_SEQUENCE",
573 winbindd_show_sequence_send
, winbindd_show_sequence_recv
},
574 { WINBINDD_GETGRGID
, "GETGRGID",
575 winbindd_getgrgid_send
, winbindd_getgrgid_recv
},
576 { WINBINDD_GETGRNAM
, "GETGRNAM",
577 winbindd_getgrnam_send
, winbindd_getgrnam_recv
},
578 { WINBINDD_GETUSERSIDS
, "GETUSERSIDS",
579 winbindd_getusersids_send
, winbindd_getusersids_recv
},
580 { WINBINDD_LOOKUPRIDS
, "LOOKUPRIDS",
581 winbindd_lookuprids_send
, winbindd_lookuprids_recv
},
582 { WINBINDD_SETPWENT
, "SETPWENT",
583 winbindd_setpwent_send
, winbindd_setpwent_recv
},
584 { WINBINDD_GETPWENT
, "GETPWENT",
585 winbindd_getpwent_send
, winbindd_getpwent_recv
},
586 { WINBINDD_ENDPWENT
, "ENDPWENT",
587 winbindd_endpwent_send
, winbindd_endpwent_recv
},
588 { WINBINDD_DSGETDCNAME
, "DSGETDCNAME",
589 winbindd_dsgetdcname_send
, winbindd_dsgetdcname_recv
},
590 { WINBINDD_GETDCNAME
, "GETDCNAME",
591 winbindd_getdcname_send
, winbindd_getdcname_recv
},
592 { WINBINDD_SETGRENT
, "SETGRENT",
593 winbindd_setgrent_send
, winbindd_setgrent_recv
},
594 { WINBINDD_GETGRENT
, "GETGRENT",
595 winbindd_getgrent_send
, winbindd_getgrent_recv
},
596 { WINBINDD_ENDGRENT
, "ENDGRENT",
597 winbindd_endgrent_send
, winbindd_endgrent_recv
},
598 { WINBINDD_LIST_USERS
, "LIST_USERS",
599 winbindd_list_users_send
, winbindd_list_users_recv
},
600 { WINBINDD_LIST_GROUPS
, "LIST_GROUPS",
601 winbindd_list_groups_send
, winbindd_list_groups_recv
},
602 { WINBINDD_CHECK_MACHACC
, "CHECK_MACHACC",
603 winbindd_check_machine_acct_send
, winbindd_check_machine_acct_recv
},
604 { WINBINDD_PING_DC
, "PING_DC",
605 winbindd_ping_dc_send
, winbindd_ping_dc_recv
},
606 { WINBINDD_PAM_AUTH
, "PAM_AUTH",
607 winbindd_pam_auth_send
, winbindd_pam_auth_recv
},
608 { WINBINDD_PAM_LOGOFF
, "PAM_LOGOFF",
609 winbindd_pam_logoff_send
, winbindd_pam_logoff_recv
},
610 { WINBINDD_PAM_CHAUTHTOK
, "PAM_CHAUTHTOK",
611 winbindd_pam_chauthtok_send
, winbindd_pam_chauthtok_recv
},
612 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP
, "PAM_CHNG_PSWD_AUTH_CRAP",
613 winbindd_pam_chng_pswd_auth_crap_send
,
614 winbindd_pam_chng_pswd_auth_crap_recv
},
615 { WINBINDD_WINS_BYIP
, "WINS_BYIP",
616 winbindd_wins_byip_send
, winbindd_wins_byip_recv
},
617 { WINBINDD_WINS_BYNAME
, "WINS_BYNAME",
618 winbindd_wins_byname_send
, winbindd_wins_byname_recv
},
620 { 0, NULL
, NULL
, NULL
}
623 static struct winbindd_async_dispatch_table async_priv_table
[] = {
624 { WINBINDD_ALLOCATE_UID
, "ALLOCATE_UID",
625 winbindd_allocate_uid_send
, winbindd_allocate_uid_recv
},
626 { WINBINDD_ALLOCATE_GID
, "ALLOCATE_GID",
627 winbindd_allocate_gid_send
, winbindd_allocate_gid_recv
},
628 { WINBINDD_CHANGE_MACHACC
, "CHANGE_MACHACC",
629 winbindd_change_machine_acct_send
, winbindd_change_machine_acct_recv
},
630 { WINBINDD_PAM_AUTH_CRAP
, "PAM_AUTH_CRAP",
631 winbindd_pam_auth_crap_send
, winbindd_pam_auth_crap_recv
},
633 { 0, NULL
, NULL
, NULL
}
636 static void wb_request_done(struct tevent_req
*req
);
638 static void process_request(struct winbindd_cli_state
*state
)
640 struct winbindd_dispatch_table
*table
= dispatch_table
;
641 struct winbindd_async_dispatch_table
*atable
;
643 state
->mem_ctx
= talloc_named(state
, 0, "winbind request");
644 if (state
->mem_ctx
== NULL
)
647 /* Remember who asked us. */
648 state
->pid
= state
->request
->pid
;
650 state
->cmd_name
= "unknown request";
651 state
->recv_fn
= NULL
;
652 state
->last_access
= time(NULL
);
654 /* Process command */
656 for (atable
= async_nonpriv_table
; atable
->send_req
; atable
+= 1) {
657 if (state
->request
->cmd
== atable
->cmd
) {
662 if ((atable
->send_req
== NULL
) && state
->privileged
) {
663 for (atable
= async_priv_table
; atable
->send_req
;
665 if (state
->request
->cmd
== atable
->cmd
) {
671 if (atable
->send_req
!= NULL
) {
672 struct tevent_req
*req
;
674 state
->cmd_name
= atable
->cmd_name
;
675 state
->recv_fn
= atable
->recv_req
;
677 DEBUG(10, ("process_request: Handling async request %d:%s\n",
678 (int)state
->pid
, state
->cmd_name
));
680 req
= atable
->send_req(state
->mem_ctx
, winbind_event_context(),
681 state
, state
->request
);
683 DEBUG(0, ("process_request: atable->send failed for "
684 "%s\n", atable
->cmd_name
));
685 request_error(state
);
688 tevent_req_set_callback(req
, wb_request_done
, state
);
692 state
->response
= talloc_zero(state
->mem_ctx
,
693 struct winbindd_response
);
694 if (state
->response
== NULL
) {
695 DEBUG(10, ("talloc failed\n"));
696 remove_client(state
);
699 state
->response
->result
= WINBINDD_PENDING
;
700 state
->response
->length
= sizeof(struct winbindd_response
);
702 for (table
= dispatch_table
; table
->fn
; table
++) {
703 if (state
->request
->cmd
== table
->cmd
) {
704 DEBUG(10,("process_request: request fn %s\n",
705 table
->winbindd_cmd_name
));
706 state
->cmd_name
= table
->winbindd_cmd_name
;
713 DEBUG(10,("process_request: unknown request fn number %d\n",
714 (int)state
->request
->cmd
));
715 request_error(state
);
719 static void wb_request_done(struct tevent_req
*req
)
721 struct winbindd_cli_state
*state
= tevent_req_callback_data(
722 req
, struct winbindd_cli_state
);
725 state
->response
= talloc_zero(state
->mem_ctx
,
726 struct winbindd_response
);
727 if (state
->response
== NULL
) {
728 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
729 (int)state
->pid
, state
->cmd_name
));
730 remove_client(state
);
733 state
->response
->result
= WINBINDD_PENDING
;
734 state
->response
->length
= sizeof(struct winbindd_response
);
736 status
= state
->recv_fn(req
, state
->response
);
739 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
740 (int)state
->pid
, state
->cmd_name
, nt_errstr(status
)));
742 if (!NT_STATUS_IS_OK(status
)) {
743 request_error(state
);
750 * This is the main event loop of winbind requests. It goes through a
751 * state-machine of 3 read/write requests, 4 if you have extra data to send.
753 * An idle winbind client has a read request of 4 bytes outstanding,
754 * finalizing function is request_len_recv, checking the length. request_recv
755 * then processes the packet. The processing function then at some point has
756 * to call request_finished which schedules sending the response.
759 static void request_finished(struct winbindd_cli_state
*state
);
761 static void winbind_client_request_read(struct tevent_req
*req
);
762 static void winbind_client_response_written(struct tevent_req
*req
);
764 static void request_finished(struct winbindd_cli_state
*state
)
766 struct tevent_req
*req
;
768 TALLOC_FREE(state
->request
);
770 req
= wb_resp_write_send(state
, winbind_event_context(),
771 state
->out_queue
, state
->sock
,
774 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
775 (int)state
->pid
, state
->cmd_name
));
776 remove_client(state
);
779 tevent_req_set_callback(req
, winbind_client_response_written
, state
);
782 static void winbind_client_response_written(struct tevent_req
*req
)
784 struct winbindd_cli_state
*state
= tevent_req_callback_data(
785 req
, struct winbindd_cli_state
);
789 ret
= wb_resp_write_recv(req
, &err
);
794 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
795 (int)state
->pid
, state
->cmd_name
, strerror(err
)));
796 remove_client(state
);
800 DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
801 "to client\n", (int)state
->pid
, state
->cmd_name
));
803 TALLOC_FREE(state
->mem_ctx
);
804 state
->response
= NULL
;
805 state
->cmd_name
= "no request";
806 state
->recv_fn
= NULL
;
808 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
809 WINBINDD_MAX_EXTRA_DATA
);
811 remove_client(state
);
814 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
817 void request_error(struct winbindd_cli_state
*state
)
819 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
820 state
->response
->result
= WINBINDD_ERROR
;
821 request_finished(state
);
824 void request_ok(struct winbindd_cli_state
*state
)
826 SMB_ASSERT(state
->response
->result
== WINBINDD_PENDING
);
827 state
->response
->result
= WINBINDD_OK
;
828 request_finished(state
);
831 /* Process a new connection by adding it to the client connection list */
833 static void new_connection(int listen_sock
, bool privileged
)
835 struct sockaddr_un sunaddr
;
836 struct winbindd_cli_state
*state
;
837 struct tevent_req
*req
;
841 /* Accept connection */
843 len
= sizeof(sunaddr
);
845 sock
= accept(listen_sock
, (struct sockaddr
*)(void *)&sunaddr
, &len
);
848 if (errno
!= EINTR
) {
849 DEBUG(0, ("Failed to accept socket - %s\n",
855 DEBUG(6,("accepted socket %d\n", sock
));
857 /* Create new connection structure */
859 if ((state
= talloc_zero(NULL
, struct winbindd_cli_state
)) == NULL
) {
866 state
->out_queue
= tevent_queue_create(state
, "winbind client reply");
867 if (state
->out_queue
== NULL
) {
873 state
->last_access
= time(NULL
);
875 state
->privileged
= privileged
;
877 req
= wb_req_read_send(state
, winbind_event_context(), state
->sock
,
878 WINBINDD_MAX_EXTRA_DATA
);
884 tevent_req_set_callback(req
, winbind_client_request_read
, state
);
886 /* Add to connection list */
888 winbindd_add_client(state
);
891 static void winbind_client_request_read(struct tevent_req
*req
)
893 struct winbindd_cli_state
*state
= tevent_req_callback_data(
894 req
, struct winbindd_cli_state
);
898 ret
= wb_req_read_recv(req
, state
, &state
->request
, &err
);
902 DEBUG(6, ("closing socket %d, client exited\n",
905 DEBUG(2, ("Could not read client request from fd %d: "
906 "%s\n", state
->sock
, strerror(err
)));
910 remove_client(state
);
913 process_request(state
);
916 /* Remove a client connection from client connection list */
918 static void remove_client(struct winbindd_cli_state
*state
)
923 /* It's a dead client - hold a funeral */
929 if (state
->sock
!= -1) {
930 /* tell client, we are closing ... */
931 nwritten
= write(state
->sock
, &c
, sizeof(c
));
932 if (nwritten
== -1) {
933 DEBUG(2, ("final write to client failed: %s\n",
943 TALLOC_FREE(state
->mem_ctx
);
945 /* Remove from list and free */
947 winbindd_remove_client(state
);
951 /* Is a client idle? */
953 static bool client_is_idle(struct winbindd_cli_state
*state
) {
954 return (state
->request
== NULL
&&
955 state
->response
== NULL
&&
956 !state
->pwent_state
&& !state
->grent_state
);
959 /* Shutdown client connection which has been idle for the longest time */
961 static bool remove_idle_client(void)
963 struct winbindd_cli_state
*state
, *remove_state
= NULL
;
964 time_t last_access
= 0;
967 for (state
= winbindd_client_list(); state
; state
= state
->next
) {
968 if (client_is_idle(state
)) {
970 if (!last_access
|| state
->last_access
< last_access
) {
971 last_access
= state
->last_access
;
972 remove_state
= state
;
978 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
979 nidle
, remove_state
->sock
, (unsigned int)remove_state
->pid
));
980 remove_client(remove_state
);
987 struct winbindd_listen_state
{
992 static void winbindd_listen_fde_handler(struct tevent_context
*ev
,
993 struct tevent_fd
*fde
,
997 struct winbindd_listen_state
*s
= talloc_get_type_abort(private_data
,
998 struct winbindd_listen_state
);
1000 while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
1001 DEBUG(5,("winbindd: Exceeding %d client "
1002 "connections, removing idle "
1003 "connection.\n", lp_winbind_max_clients()));
1004 if (!remove_idle_client()) {
1005 DEBUG(0,("winbindd: Exceeding %d "
1006 "client connections, no idle "
1007 "connection found\n",
1008 lp_winbind_max_clients()));
1012 new_connection(s
->fd
, s
->privileged
);
1016 * Winbindd socket accessor functions
1019 const char *get_winbind_pipe_dir(void)
1021 return lp_parm_const_string(-1, "winbindd", "socket dir", get_dyn_WINBINDD_SOCKET_DIR());
1024 char *get_winbind_priv_pipe_dir(void)
1026 return state_path(WINBINDD_PRIV_SOCKET_SUBDIR
);
1029 static bool winbindd_setup_listeners(void)
1031 struct winbindd_listen_state
*pub_state
= NULL
;
1032 struct winbindd_listen_state
*priv_state
= NULL
;
1033 struct tevent_fd
*fde
;
1036 pub_state
= talloc(winbind_event_context(),
1037 struct winbindd_listen_state
);
1042 pub_state
->privileged
= false;
1043 pub_state
->fd
= create_pipe_sock(
1044 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME
, 0755);
1045 if (pub_state
->fd
== -1) {
1048 rc
= listen(pub_state
->fd
, 5);
1053 fde
= tevent_add_fd(winbind_event_context(), pub_state
, pub_state
->fd
,
1054 TEVENT_FD_READ
, winbindd_listen_fde_handler
,
1057 close(pub_state
->fd
);
1060 tevent_fd_set_auto_close(fde
);
1062 priv_state
= talloc(winbind_event_context(),
1063 struct winbindd_listen_state
);
1068 priv_state
->privileged
= true;
1069 priv_state
->fd
= create_pipe_sock(
1070 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME
, 0750);
1071 if (priv_state
->fd
== -1) {
1074 rc
= listen(priv_state
->fd
, 5);
1079 fde
= tevent_add_fd(winbind_event_context(), priv_state
,
1080 priv_state
->fd
, TEVENT_FD_READ
,
1081 winbindd_listen_fde_handler
, priv_state
);
1083 close(priv_state
->fd
);
1086 tevent_fd_set_auto_close(fde
);
1090 TALLOC_FREE(pub_state
);
1091 TALLOC_FREE(priv_state
);
1095 bool winbindd_use_idmap_cache(void)
1097 return !opt_nocache
;
1100 bool winbindd_use_cache(void)
1102 return !opt_nocache
;
1105 static void winbindd_register_handlers(struct messaging_context
*msg_ctx
,
1108 /* Setup signal handlers */
1110 if (!winbindd_setup_sig_term_handler(true))
1112 if (!winbindd_setup_stdin_handler(true, foreground
))
1114 if (!winbindd_setup_sig_hup_handler(NULL
))
1116 if (!winbindd_setup_sig_chld_handler())
1118 if (!winbindd_setup_sig_usr2_handler())
1121 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
1124 * Ensure all cache and idmap caches are consistent
1125 * and initialized before we startup.
1127 if (!winbindd_cache_validate_and_initialize()) {
1131 /* get broadcast messages */
1133 if (!serverid_register(messaging_server_id(msg_ctx
),
1137 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1141 /* React on 'smbcontrol winbindd reload-config' in the same way
1142 as to SIGHUP signal */
1143 messaging_register(msg_ctx
, NULL
,
1144 MSG_SMB_CONF_UPDATED
, msg_reload_services
);
1145 messaging_register(msg_ctx
, NULL
,
1146 MSG_SHUTDOWN
, msg_shutdown
);
1148 /* Handle online/offline messages. */
1149 messaging_register(msg_ctx
, NULL
,
1150 MSG_WINBIND_OFFLINE
, winbind_msg_offline
);
1151 messaging_register(msg_ctx
, NULL
,
1152 MSG_WINBIND_ONLINE
, winbind_msg_online
);
1153 messaging_register(msg_ctx
, NULL
,
1154 MSG_WINBIND_ONLINESTATUS
, winbind_msg_onlinestatus
);
1156 messaging_register(msg_ctx
, NULL
,
1157 MSG_DUMP_EVENT_LIST
, winbind_msg_dump_event_list
);
1159 messaging_register(msg_ctx
, NULL
,
1160 MSG_WINBIND_VALIDATE_CACHE
,
1161 winbind_msg_validate_cache
);
1163 messaging_register(msg_ctx
, NULL
,
1164 MSG_WINBIND_DUMP_DOMAIN_LIST
,
1165 winbind_msg_dump_domain_list
);
1167 messaging_register(msg_ctx
, NULL
,
1168 MSG_WINBIND_IP_DROPPED
,
1169 winbind_msg_ip_dropped_parent
);
1171 /* Register handler for MSG_DEBUG. */
1172 messaging_register(msg_ctx
, NULL
,
1176 netsamlogon_cache_init(); /* Non-critical */
1178 /* clear the cached list of trusted domains */
1182 if (!init_domain_list()) {
1183 DEBUG(0,("unable to initialize domain list\n"));
1188 init_locator_child();
1190 smb_nscd_flush_user_cache();
1191 smb_nscd_flush_group_cache();
1193 if (lp_allow_trusted_domains()) {
1194 if (tevent_add_timer(winbind_event_context(), NULL
, timeval_zero(),
1195 rescan_trusted_domains
, NULL
) == NULL
) {
1196 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1203 struct winbindd_addrchanged_state
{
1204 struct addrchange_context
*ctx
;
1205 struct tevent_context
*ev
;
1206 struct messaging_context
*msg_ctx
;
1209 static void winbindd_addr_changed(struct tevent_req
*req
);
1211 static void winbindd_init_addrchange(TALLOC_CTX
*mem_ctx
,
1212 struct tevent_context
*ev
,
1213 struct messaging_context
*msg_ctx
)
1215 struct winbindd_addrchanged_state
*state
;
1216 struct tevent_req
*req
;
1219 state
= talloc(mem_ctx
, struct winbindd_addrchanged_state
);
1220 if (state
== NULL
) {
1221 DEBUG(10, ("talloc failed\n"));
1225 state
->msg_ctx
= msg_ctx
;
1227 status
= addrchange_context_create(state
, &state
->ctx
);
1228 if (!NT_STATUS_IS_OK(status
)) {
1229 DEBUG(10, ("addrchange_context_create failed: %s\n",
1230 nt_errstr(status
)));
1234 req
= addrchange_send(state
, ev
, state
->ctx
);
1236 DEBUG(0, ("addrchange_send failed\n"));
1240 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1243 static void winbindd_addr_changed(struct tevent_req
*req
)
1245 struct winbindd_addrchanged_state
*state
= tevent_req_callback_data(
1246 req
, struct winbindd_addrchanged_state
);
1247 enum addrchange_type type
;
1248 struct sockaddr_storage addr
;
1251 status
= addrchange_recv(req
, &type
, &addr
);
1253 if (!NT_STATUS_IS_OK(status
)) {
1254 DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
1255 nt_errstr(status
)));
1259 if (type
== ADDRCHANGE_DEL
) {
1260 char addrstr
[INET6_ADDRSTRLEN
];
1263 print_sockaddr(addrstr
, sizeof(addrstr
), &addr
);
1265 DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
1268 blob
= data_blob_const(addrstr
, strlen(addrstr
)+1);
1270 status
= messaging_send(state
->msg_ctx
,
1271 messaging_server_id(state
->msg_ctx
),
1272 MSG_WINBIND_IP_DROPPED
, &blob
);
1273 if (!NT_STATUS_IS_OK(status
)) {
1274 DEBUG(10, ("messaging_send failed: %s - ignoring\n",
1275 nt_errstr(status
)));
1278 req
= addrchange_send(state
, state
->ev
, state
->ctx
);
1280 DEBUG(0, ("addrchange_send failed\n"));
1284 tevent_req_set_callback(req
, winbindd_addr_changed
, state
);
1289 int main(int argc
, char **argv
, char **envp
)
1291 static bool is_daemon
= False
;
1292 static bool Fork
= True
;
1293 static bool log_stdout
= False
;
1294 static bool no_process_group
= False
;
1298 OPT_NO_PROCESS_GROUP
,
1301 struct poptOption long_options
[] = {
1303 { "stdout", 'S', POPT_ARG_NONE
, NULL
, OPT_LOG_STDOUT
, "Log to stdout" },
1304 { "foreground", 'F', POPT_ARG_NONE
, NULL
, OPT_FORK
, "Daemon in foreground mode" },
1305 { "no-process-group", 0, POPT_ARG_NONE
, NULL
, OPT_NO_PROCESS_GROUP
, "Don't create a new process group" },
1306 { "daemon", 'D', POPT_ARG_NONE
, NULL
, OPT_DAEMON
, "Become a daemon (default)" },
1307 { "interactive", 'i', POPT_ARG_NONE
, NULL
, 'i', "Interactive mode" },
1308 { "no-caching", 'n', POPT_ARG_NONE
, NULL
, 'n', "Disable caching" },
1319 * Do this before any other talloc operation
1321 talloc_enable_null_tracking();
1322 frame
= talloc_stackframe();
1324 setup_logging("winbindd", DEBUG_DEFAULT_STDOUT
);
1326 /* glibc (?) likes to print "User defined signal 1" and exit if a
1327 SIGUSR[12] is received before a handler is installed */
1329 CatchSignal(SIGUSR1
, SIG_IGN
);
1330 CatchSignal(SIGUSR2
, SIG_IGN
);
1333 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1337 /* Initialise for running in non-root mode */
1341 set_remote_machine_name("winbindd", False
);
1343 /* Set environment variable so we don't recursively call ourselves.
1344 This may also be useful interactively. */
1346 if ( !winbind_off() ) {
1347 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1351 /* Initialise samba/rpc client stuff */
1353 pc
= poptGetContext("winbindd", argc
, (const char **)argv
, long_options
, 0);
1355 while ((opt
= poptGetNextOpt(pc
)) != -1) {
1357 /* Don't become a daemon */
1369 case OPT_NO_PROCESS_GROUP
:
1370 no_process_group
= true;
1372 case OPT_LOG_STDOUT
:
1379 d_fprintf(stderr
, "\nInvalid option %s: %s\n\n",
1380 poptBadOption(pc
, 0), poptStrerror(opt
));
1381 poptPrintUsage(pc
, stderr
, 0);
1386 /* We call dump_core_setup one more time because the command line can
1387 * set the log file or the log-basename and this will influence where
1388 * cores are stored. Without this call get_dyn_LOGFILEBASE will be
1389 * the default value derived from build's prefix. For EOM this value
1390 * is often not related to the path where winbindd is actually run
1393 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1394 if (is_daemon
&& interactive
) {
1395 d_fprintf(stderr
,"\nERROR: "
1396 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1397 poptPrintUsage(pc
, stderr
, 0);
1401 if (log_stdout
&& Fork
) {
1402 d_fprintf(stderr
, "\nERROR: "
1403 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1404 poptPrintUsage(pc
, stderr
, 0);
1408 poptFreeContext(pc
);
1410 if (!override_logfile
) {
1412 if (asprintf(&lfile
,"%s/log.winbindd",
1413 get_dyn_LOGFILEBASE()) > 0) {
1414 lp_set_logfile(lfile
);
1420 setup_logging("winbindd", DEBUG_STDOUT
);
1422 setup_logging("winbindd", DEBUG_FILE
);
1426 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1427 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE
));
1429 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1430 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1433 /* After parsing the configuration file we setup the core path one more time
1434 * as the log file might have been set in the configuration and cores's
1435 * path is by default basename(lp_logfile()).
1437 dump_core_setup("winbindd", lp_logfile(talloc_tos()));
1439 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
) {
1440 DEBUG(0, ("server role = 'active directory domain controller' not compatible with running the winbindd binary. \n"));
1441 DEBUGADD(0, ("You should start 'samba' instead, and it will control starting the internal AD DC winbindd implementation, which is not the same as this one\n"));
1445 /* Initialise messaging system */
1447 if (winbind_messaging_context() == NULL
) {
1451 if (!reload_services_file(NULL
)) {
1452 DEBUG(0, ("error opening config file\n"));
1456 ok
= directory_create_or_exist(lp_lockdir(), geteuid(), 0755);
1458 DEBUG(0, ("Failed to create directory %s for lock files - %s\n",
1459 lp_lockdir(), strerror(errno
)));
1463 ok
= directory_create_or_exist(lp_piddir(), geteuid(), 0755);
1465 DEBUG(0, ("Failed to create directory %s for pid files - %s\n",
1466 lp_piddir(), strerror(errno
)));
1477 if (!secrets_init()) {
1479 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1483 /* Unblock all signals we are interested in as they may have been
1484 blocked by the parent process. */
1486 BlockSignals(False
, SIGINT
);
1487 BlockSignals(False
, SIGQUIT
);
1488 BlockSignals(False
, SIGTERM
);
1489 BlockSignals(False
, SIGUSR1
);
1490 BlockSignals(False
, SIGUSR2
);
1491 BlockSignals(False
, SIGHUP
);
1492 BlockSignals(False
, SIGCHLD
);
1495 become_daemon(Fork
, no_process_group
, log_stdout
);
1497 pidfile_create(lp_piddir(), "winbindd");
1501 * If we're interactive we want to set our own process group for
1502 * signal management.
1504 if (interactive
&& !no_process_group
)
1505 setpgid( (pid_t
)0, (pid_t
)0);
1510 /* Don't use winbindd_reinit_after_fork here as
1511 * we're just starting up and haven't created any
1512 * winbindd-specific resources we must free yet. JRA.
1515 status
= reinit_after_fork(winbind_messaging_context(),
1516 winbind_event_context(),
1518 if (!NT_STATUS_IS_OK(status
)) {
1519 DEBUG(0,("reinit_after_fork() failed\n"));
1524 * Do not initialize the parent-child-pipe before becoming
1525 * a daemon: this is used to detect a died parent in the child
1528 status
= init_before_fork();
1529 if (!NT_STATUS_IS_OK(status
)) {
1530 DEBUG(0, ("init_before_fork failed: %s\n", nt_errstr(status
)));
1534 winbindd_register_handlers(winbind_messaging_context(), !Fork
);
1536 status
= init_system_session_info();
1537 if (!NT_STATUS_IS_OK(status
)) {
1538 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
1539 nt_errstr(status
)));
1543 rpc_lsarpc_init(NULL
);
1544 rpc_samr_init(NULL
);
1546 winbindd_init_addrchange(NULL
, winbind_event_context(),
1547 winbind_messaging_context());
1549 /* setup listen sockets */
1551 if (!winbindd_setup_listeners()) {
1552 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1557 /* Loop waiting for requests */
1559 frame
= talloc_stackframe();
1561 if (tevent_loop_once(winbind_event_context()) == -1) {
1562 DEBUG(1, ("tevent_loop_once() failed: %s\n",