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"
30 #include "librpc/gen_ndr/messaging.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(get_dyn_CONFIGFILE(),False,False,True,True);
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);
206 bool winbindd_setup_sig_term_handler(bool parent)
208 struct tevent_signal *se;
211 is_parent = talloc(winbind_event_context(), bool);
218 se = tevent_add_signal(winbind_event_context(),
221 winbindd_sig_term_handler,
224 DEBUG(0,("failed to setup SIGTERM handler"));
225 talloc_free(is_parent);
229 se = tevent_add_signal(winbind_event_context(),
232 winbindd_sig_term_handler,
235 DEBUG(0,("failed to setup SIGINT handler"));
236 talloc_free(is_parent);
240 se = tevent_add_signal(winbind_event_context(),
243 winbindd_sig_term_handler,
246 DEBUG(0,("failed to setup SIGINT handler"));
247 talloc_free(is_parent);
254 static void winbindd_sig_hup_handler(struct tevent_context *ev,
255 struct tevent_signal *se,
261 const char *file = (const char *)private_data;
263 DEBUG(1,("Reloading services after SIGHUP\n"));
264 flush_caches_noinit();
265 reload_services_file(file);
268 bool winbindd_setup_sig_hup_handler(const char *lfile)
270 struct tevent_signal *se;
274 file = talloc_strdup(winbind_event_context(),
281 se = tevent_add_signal(winbind_event_context(),
282 winbind_event_context(),
284 winbindd_sig_hup_handler,
293 static void winbindd_sig_chld_handler(struct tevent_context *ev,
294 struct tevent_signal *se,
302 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
303 winbind_child_died(pid);
307 static bool winbindd_setup_sig_chld_handler(void)
309 struct tevent_signal *se;
311 se = tevent_add_signal(winbind_event_context(),
312 winbind_event_context(),
314 winbindd_sig_chld_handler,
323 static void winbindd_sig_usr2_handler(struct tevent_context *ev,
324 struct tevent_signal *se,
333 static bool winbindd_setup_sig_usr2_handler(void)
335 struct tevent_signal *se;
337 se = tevent_add_signal(winbind_event_context(),
338 winbind_event_context(),
340 winbindd_sig_usr2_handler,
349 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
350 static void msg_reload_services(struct messaging_context *msg,
353 struct server_id server_id,
356 /* Flush various caches */
358 reload_services_file((const char *) private_data);
361 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
362 static void msg_shutdown(struct messaging_context *msg,
365 struct server_id server_id,
368 /* only the parent waits for this message */
369 DEBUG(0,("Got shutdown message\n"));
374 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
377 struct server_id server_id,
383 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
387 * call the validation code from a child:
388 * so we don't block the main winbindd and the validation
389 * code can safely use fork/waitpid...
391 child_pid = sys_fork();
393 if (child_pid == -1) {
394 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
399 if (child_pid != 0) {
401 DEBUG(5, ("winbind_msg_validate_cache: child created with "
402 "pid %d.\n", (int)child_pid));
408 if (!winbindd_reinit_after_fork(NULL)) {
412 /* install default SIGCHLD handler: validation code uses fork/waitpid */
413 CatchSignal(SIGCHLD, SIG_DFL);
415 ret = (uint8)winbindd_validate_cache_nobackup();
416 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
417 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
422 static struct winbindd_dispatch_table {
423 enum winbindd_cmd cmd;
424 void (*fn)(struct winbindd_cli_state *state);
425 const char *winbindd_cmd_name;
426 } dispatch_table[] = {
428 /* Enumeration functions */
430 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
435 { WINBINDD_INFO, winbindd_info, "INFO" },
436 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
437 "INTERFACE_VERSION" },
438 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
439 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
440 { WINBINDD_DC_INFO, winbindd_dc_info, "DC_INFO" },
441 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
442 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
443 "WINBINDD_PRIV_PIPE_DIR" },
445 /* Credential cache access */
446 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
447 { WINBINDD_CCACHE_SAVE, winbindd_ccache_save, "CCACHE_SAVE" },
451 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
452 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
456 { WINBINDD_NUM_CMDS, NULL, "NONE" }
459 struct winbindd_async_dispatch_table {
460 enum winbindd_cmd cmd;
461 const char *cmd_name;
462 struct tevent_req *(*send_req)(TALLOC_CTX *mem_ctx,
463 struct tevent_context *ev,
464 struct winbindd_cli_state *cli,
465 struct winbindd_request *request);
466 NTSTATUS (*recv_req)(struct tevent_req *req,
467 struct winbindd_response *presp);
470 static struct winbindd_async_dispatch_table async_nonpriv_table[] = {
471 { WINBINDD_PING, "PING",
472 wb_ping_send, wb_ping_recv },
473 { WINBINDD_LOOKUPSID, "LOOKUPSID",
474 winbindd_lookupsid_send, winbindd_lookupsid_recv },
475 { WINBINDD_LOOKUPNAME, "LOOKUPNAME",
476 winbindd_lookupname_send, winbindd_lookupname_recv },
477 { WINBINDD_SID_TO_UID, "SID_TO_UID",
478 winbindd_sid_to_uid_send, winbindd_sid_to_uid_recv },
479 { WINBINDD_SID_TO_GID, "SID_TO_GID",
480 winbindd_sid_to_gid_send, winbindd_sid_to_gid_recv },
481 { WINBINDD_UID_TO_SID, "UID_TO_SID",
482 winbindd_uid_to_sid_send, winbindd_uid_to_sid_recv },
483 { WINBINDD_GID_TO_SID, "GID_TO_SID",
484 winbindd_gid_to_sid_send, winbindd_gid_to_sid_recv },
485 { WINBINDD_GETPWSID, "GETPWSID",
486 winbindd_getpwsid_send, winbindd_getpwsid_recv },
487 { WINBINDD_GETPWNAM, "GETPWNAM",
488 winbindd_getpwnam_send, winbindd_getpwnam_recv },
489 { WINBINDD_GETPWUID, "GETPWUID",
490 winbindd_getpwuid_send, winbindd_getpwuid_recv },
491 { WINBINDD_GETSIDALIASES, "GETSIDALIASES",
492 winbindd_getsidaliases_send, winbindd_getsidaliases_recv },
493 { WINBINDD_GETUSERDOMGROUPS, "GETUSERDOMGROUPS",
494 winbindd_getuserdomgroups_send, winbindd_getuserdomgroups_recv },
495 { WINBINDD_GETGROUPS, "GETGROUPS",
496 winbindd_getgroups_send, winbindd_getgroups_recv },
497 { WINBINDD_SHOW_SEQUENCE, "SHOW_SEQUENCE",
498 winbindd_show_sequence_send, winbindd_show_sequence_recv },
499 { WINBINDD_GETGRGID, "GETGRGID",
500 winbindd_getgrgid_send, winbindd_getgrgid_recv },
501 { WINBINDD_GETGRNAM, "GETGRNAM",
502 winbindd_getgrnam_send, winbindd_getgrnam_recv },
503 { WINBINDD_GETUSERSIDS, "GETUSERSIDS",
504 winbindd_getusersids_send, winbindd_getusersids_recv },
505 { WINBINDD_LOOKUPRIDS, "LOOKUPRIDS",
506 winbindd_lookuprids_send, winbindd_lookuprids_recv },
507 { WINBINDD_SETPWENT, "SETPWENT",
508 winbindd_setpwent_send, winbindd_setpwent_recv },
509 { WINBINDD_GETPWENT, "GETPWENT",
510 winbindd_getpwent_send, winbindd_getpwent_recv },
511 { WINBINDD_ENDPWENT, "ENDPWENT",
512 winbindd_endpwent_send, winbindd_endpwent_recv },
513 { WINBINDD_DSGETDCNAME, "DSGETDCNAME",
514 winbindd_dsgetdcname_send, winbindd_dsgetdcname_recv },
515 { WINBINDD_GETDCNAME, "GETDCNAME",
516 winbindd_getdcname_send, winbindd_getdcname_recv },
517 { WINBINDD_SETGRENT, "SETGRENT",
518 winbindd_setgrent_send, winbindd_setgrent_recv },
519 { WINBINDD_GETGRENT, "GETGRENT",
520 winbindd_getgrent_send, winbindd_getgrent_recv },
521 { WINBINDD_ENDGRENT, "ENDGRENT",
522 winbindd_endgrent_send, winbindd_endgrent_recv },
523 { WINBINDD_LIST_USERS, "LIST_USERS",
524 winbindd_list_users_send, winbindd_list_users_recv },
525 { WINBINDD_LIST_GROUPS, "LIST_GROUPS",
526 winbindd_list_groups_send, winbindd_list_groups_recv },
527 { WINBINDD_CHECK_MACHACC, "CHECK_MACHACC",
528 winbindd_check_machine_acct_send, winbindd_check_machine_acct_recv },
529 { WINBINDD_PING_DC, "PING_DC",
530 winbindd_ping_dc_send, winbindd_ping_dc_recv },
531 { WINBINDD_PAM_AUTH, "PAM_AUTH",
532 winbindd_pam_auth_send, winbindd_pam_auth_recv },
533 { WINBINDD_PAM_LOGOFF, "PAM_LOGOFF",
534 winbindd_pam_logoff_send, winbindd_pam_logoff_recv },
535 { WINBINDD_PAM_CHAUTHTOK, "PAM_CHAUTHTOK",
536 winbindd_pam_chauthtok_send, winbindd_pam_chauthtok_recv },
537 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, "PAM_CHNG_PSWD_AUTH_CRAP",
538 winbindd_pam_chng_pswd_auth_crap_send,
539 winbindd_pam_chng_pswd_auth_crap_recv },
541 { 0, NULL, NULL, NULL }
544 static struct winbindd_async_dispatch_table async_priv_table[] = {
545 { WINBINDD_ALLOCATE_UID, "ALLOCATE_UID",
546 winbindd_allocate_uid_send, winbindd_allocate_uid_recv },
547 { WINBINDD_ALLOCATE_GID, "ALLOCATE_GID",
548 winbindd_allocate_gid_send, winbindd_allocate_gid_recv },
549 { WINBINDD_CHANGE_MACHACC, "CHANGE_MACHACC",
550 winbindd_change_machine_acct_send, winbindd_change_machine_acct_recv },
551 { WINBINDD_PAM_AUTH_CRAP, "PAM_AUTH_CRAP",
552 winbindd_pam_auth_crap_send, winbindd_pam_auth_crap_recv },
554 { 0, NULL, NULL, NULL }
557 static void wb_request_done(struct tevent_req *req);
559 static void process_request(struct winbindd_cli_state *state)
561 struct winbindd_dispatch_table *table = dispatch_table;
562 struct winbindd_async_dispatch_table *atable;
564 state->mem_ctx = talloc_named(state, 0, "winbind request");
565 if (state->mem_ctx == NULL)
568 /* Remember who asked us. */
569 state->pid = state->request->pid;
571 state->cmd_name = "unknown request";
572 state->recv_fn = NULL;
574 /* Process command */
576 for (atable = async_nonpriv_table; atable->send_req; atable += 1) {
577 if (state->request->cmd == atable->cmd) {
582 if ((atable->send_req == NULL) && state->privileged) {
583 for (atable = async_priv_table; atable->send_req;
585 if (state->request->cmd == atable->cmd) {
591 if (atable->send_req != NULL) {
592 struct tevent_req *req;
594 state->cmd_name = atable->cmd_name;
595 state->recv_fn = atable->recv_req;
597 DEBUG(10, ("process_request: Handling async request %d:%s\n",
598 (int)state->pid, state->cmd_name));
600 req = atable->send_req(state->mem_ctx, winbind_event_context(),
601 state, state->request);
603 DEBUG(0, ("process_request: atable->send failed for "
604 "%s\n", atable->cmd_name));
605 request_error(state);
608 tevent_req_set_callback(req, wb_request_done, state);
612 state->response = talloc_zero(state->mem_ctx,
613 struct winbindd_response);
614 if (state->response == NULL) {
615 DEBUG(10, ("talloc failed\n"));
616 remove_client(state);
619 state->response->result = WINBINDD_PENDING;
620 state->response->length = sizeof(struct winbindd_response);
622 for (table = dispatch_table; table->fn; table++) {
623 if (state->request->cmd == table->cmd) {
624 DEBUG(10,("process_request: request fn %s\n",
625 table->winbindd_cmd_name ));
626 state->cmd_name = table->winbindd_cmd_name;
633 DEBUG(10,("process_request: unknown request fn number %d\n",
634 (int)state->request->cmd ));
635 request_error(state);
639 static void wb_request_done(struct tevent_req *req)
641 struct winbindd_cli_state *state = tevent_req_callback_data(
642 req, struct winbindd_cli_state);
645 state->response = talloc_zero(state->mem_ctx,
646 struct winbindd_response);
647 if (state->response == NULL) {
648 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
649 (int)state->pid, state->cmd_name));
650 remove_client(state);
653 state->response->result = WINBINDD_PENDING;
654 state->response->length = sizeof(struct winbindd_response);
656 status = state->recv_fn(req, state->response);
659 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
660 (int)state->pid, state->cmd_name, nt_errstr(status)));
662 if (!NT_STATUS_IS_OK(status)) {
663 request_error(state);
670 * This is the main event loop of winbind requests. It goes through a
671 * state-machine of 3 read/write requests, 4 if you have extra data to send.
673 * An idle winbind client has a read request of 4 bytes outstanding,
674 * finalizing function is request_len_recv, checking the length. request_recv
675 * then processes the packet. The processing function then at some point has
676 * to call request_finished which schedules sending the response.
679 static void request_finished(struct winbindd_cli_state *state);
681 static void winbind_client_request_read(struct tevent_req *req);
682 static void winbind_client_response_written(struct tevent_req *req);
684 static void request_finished(struct winbindd_cli_state *state)
686 struct tevent_req *req;
688 TALLOC_FREE(state->request);
690 req = wb_resp_write_send(state, winbind_event_context(),
691 state->out_queue, state->sock,
694 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
695 (int)state->pid, state->cmd_name));
696 remove_client(state);
699 tevent_req_set_callback(req, winbind_client_response_written, state);
702 static void winbind_client_response_written(struct tevent_req *req)
704 struct winbindd_cli_state *state = tevent_req_callback_data(
705 req, struct winbindd_cli_state);
709 ret = wb_resp_write_recv(req, &err);
714 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
715 (int)state->pid, state->cmd_name, strerror(err)));
716 remove_client(state);
720 DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
721 "to client\n", (int)state->pid, state->cmd_name));
723 TALLOC_FREE(state->mem_ctx);
724 state->response = NULL;
725 state->cmd_name = "no request";
726 state->recv_fn = NULL;
728 req = wb_req_read_send(state, winbind_event_context(), state->sock,
729 WINBINDD_MAX_EXTRA_DATA);
731 remove_client(state);
734 tevent_req_set_callback(req, winbind_client_request_read, state);
737 void request_error(struct winbindd_cli_state *state)
739 SMB_ASSERT(state->response->result == WINBINDD_PENDING);
740 state->response->result = WINBINDD_ERROR;
741 request_finished(state);
744 void request_ok(struct winbindd_cli_state *state)
746 SMB_ASSERT(state->response->result == WINBINDD_PENDING);
747 state->response->result = WINBINDD_OK;
748 request_finished(state);
751 /* Process a new connection by adding it to the client connection list */
753 static void new_connection(int listen_sock, bool privileged)
755 struct sockaddr_un sunaddr;
756 struct winbindd_cli_state *state;
757 struct tevent_req *req;
761 /* Accept connection */
763 len = sizeof(sunaddr);
765 sock = accept(listen_sock, (struct sockaddr *)(void *)&sunaddr, &len);
768 if (errno != EINTR) {
769 DEBUG(0, ("Faild to accept socket - %s\n",
775 DEBUG(6,("accepted socket %d\n", sock));
777 /* Create new connection structure */
779 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
786 state->out_queue = tevent_queue_create(state, "winbind client reply");
787 if (state->out_queue == NULL) {
793 state->last_access = time(NULL);
795 state->privileged = privileged;
797 req = wb_req_read_send(state, winbind_event_context(), state->sock,
798 WINBINDD_MAX_EXTRA_DATA);
804 tevent_req_set_callback(req, winbind_client_request_read, state);
806 /* Add to connection list */
808 winbindd_add_client(state);
811 static void winbind_client_request_read(struct tevent_req *req)
813 struct winbindd_cli_state *state = tevent_req_callback_data(
814 req, struct winbindd_cli_state);
818 ret = wb_req_read_recv(req, state, &state->request, &err);
822 DEBUG(6, ("closing socket %d, client exited\n",
825 DEBUG(2, ("Could not read client request from fd %d: "
826 "%s\n", state->sock, strerror(err)));
830 remove_client(state);
833 process_request(state);
836 /* Remove a client connection from client connection list */
838 static void remove_client(struct winbindd_cli_state *state)
843 /* It's a dead client - hold a funeral */
849 if (state->sock != -1) {
850 /* tell client, we are closing ... */
851 nwritten = write(state->sock, &c, sizeof(c));
852 if (nwritten == -1) {
853 DEBUG(2, ("final write to client failed: %s\n",
863 TALLOC_FREE(state->mem_ctx);
865 /* Remove from list and free */
867 winbindd_remove_client(state);
871 /* Is a client idle? */
873 static bool client_is_idle(struct winbindd_cli_state *state) {
874 return (state->response == NULL &&
875 !state->pwent_state && !state->grent_state);
878 /* Shutdown client connection which has been idle for the longest time */
880 static bool remove_idle_client(void)
882 struct winbindd_cli_state *state, *remove_state = NULL;
883 time_t last_access = 0;
886 for (state = winbindd_client_list(); state; state = state->next) {
887 if (client_is_idle(state)) {
889 if (!last_access || state->last_access < last_access) {
890 last_access = state->last_access;
891 remove_state = state;
897 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
898 nidle, remove_state->sock, (unsigned int)remove_state->pid));
899 remove_client(remove_state);
906 struct winbindd_listen_state {
911 static void winbindd_listen_fde_handler(struct tevent_context *ev,
912 struct tevent_fd *fde,
916 struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
917 struct winbindd_listen_state);
919 while (winbindd_num_clients() > lp_winbind_max_clients() - 1) {
920 DEBUG(5,("winbindd: Exceeding %d client "
921 "connections, removing idle "
922 "connection.\n", lp_winbind_max_clients()));
923 if (!remove_idle_client()) {
924 DEBUG(0,("winbindd: Exceeding %d "
925 "client connections, no idle "
926 "connection found\n",
927 lp_winbind_max_clients()));
931 new_connection(s->fd, s->privileged);
935 * Winbindd socket accessor functions
938 const char *get_winbind_pipe_dir(void)
940 return lp_parm_const_string(-1, "winbindd", "socket dir", WINBINDD_SOCKET_DIR);
943 char *get_winbind_priv_pipe_dir(void)
945 return lock_path(WINBINDD_PRIV_SOCKET_SUBDIR);
948 static bool winbindd_setup_listeners(void)
950 struct winbindd_listen_state *pub_state = NULL;
951 struct winbindd_listen_state *priv_state = NULL;
952 struct tevent_fd *fde;
954 pub_state = talloc(winbind_event_context(),
955 struct winbindd_listen_state);
960 pub_state->privileged = false;
961 pub_state->fd = create_pipe_sock(
962 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME, 0755);
963 if (pub_state->fd == -1) {
967 fde = tevent_add_fd(winbind_event_context(), pub_state, pub_state->fd,
968 TEVENT_FD_READ, winbindd_listen_fde_handler,
971 close(pub_state->fd);
974 tevent_fd_set_auto_close(fde);
976 priv_state = talloc(winbind_event_context(),
977 struct winbindd_listen_state);
982 priv_state->privileged = true;
983 priv_state->fd = create_pipe_sock(
984 get_winbind_priv_pipe_dir(), WINBINDD_SOCKET_NAME, 0750);
985 if (priv_state->fd == -1) {
989 fde = tevent_add_fd(winbind_event_context(), priv_state,
990 priv_state->fd, TEVENT_FD_READ,
991 winbindd_listen_fde_handler, priv_state);
993 close(priv_state->fd);
996 tevent_fd_set_auto_close(fde);
1000 TALLOC_FREE(pub_state);
1001 TALLOC_FREE(priv_state);
1005 bool winbindd_use_idmap_cache(void)
1007 return !opt_nocache;
1010 bool winbindd_use_cache(void)
1012 return !opt_nocache;
1015 void winbindd_register_handlers(void)
1017 struct tevent_timer *te;
1018 /* Setup signal handlers */
1020 if (!winbindd_setup_sig_term_handler(true))
1022 if (!winbindd_setup_sig_hup_handler(NULL))
1024 if (!winbindd_setup_sig_chld_handler())
1026 if (!winbindd_setup_sig_usr2_handler())
1029 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1032 * Ensure all cache and idmap caches are consistent
1033 * and initialized before we startup.
1035 if (!winbindd_cache_validate_and_initialize()) {
1039 /* get broadcast messages */
1041 if (!serverid_register(procid_self(),
1042 FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
1043 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1047 /* React on 'smbcontrol winbindd reload-config' in the same way
1048 as to SIGHUP signal */
1049 messaging_register(winbind_messaging_context(), NULL,
1050 MSG_SMB_CONF_UPDATED, msg_reload_services);
1051 messaging_register(winbind_messaging_context(), NULL,
1052 MSG_SHUTDOWN, msg_shutdown);
1054 /* Handle online/offline messages. */
1055 messaging_register(winbind_messaging_context(), NULL,
1056 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1057 messaging_register(winbind_messaging_context(), NULL,
1058 MSG_WINBIND_ONLINE, winbind_msg_online);
1059 messaging_register(winbind_messaging_context(), NULL,
1060 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1062 messaging_register(winbind_messaging_context(), NULL,
1063 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1065 messaging_register(winbind_messaging_context(), NULL,
1066 MSG_WINBIND_VALIDATE_CACHE,
1067 winbind_msg_validate_cache);
1069 messaging_register(winbind_messaging_context(), NULL,
1070 MSG_WINBIND_DUMP_DOMAIN_LIST,
1071 winbind_msg_dump_domain_list);
1073 messaging_register(winbind_messaging_context(), NULL,
1074 MSG_WINBIND_IP_DROPPED,
1075 winbind_msg_ip_dropped_parent);
1077 /* Register handler for MSG_DEBUG. */
1078 messaging_register(winbind_messaging_context(), NULL,
1082 netsamlogon_cache_init(); /* Non-critical */
1084 /* clear the cached list of trusted domains */
1088 if (!init_domain_list()) {
1089 DEBUG(0,("unable to initialize domain list\n"));
1094 init_locator_child();
1096 smb_nscd_flush_user_cache();
1097 smb_nscd_flush_group_cache();
1099 te = tevent_add_timer(winbind_event_context(), NULL, timeval_zero(),
1100 rescan_trusted_domains, NULL);
1102 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1108 struct winbindd_addrchanged_state {
1109 struct addrchange_context *ctx;
1110 struct tevent_context *ev;
1111 struct messaging_context *msg_ctx;
1114 static void winbindd_addr_changed(struct tevent_req *req);
1116 static void winbindd_init_addrchange(TALLOC_CTX *mem_ctx,
1117 struct tevent_context *ev,
1118 struct messaging_context *msg_ctx)
1120 struct winbindd_addrchanged_state *state;
1121 struct tevent_req *req;
1124 state = talloc(mem_ctx, struct winbindd_addrchanged_state);
1125 if (state == NULL) {
1126 DEBUG(10, ("talloc failed\n"));
1130 state->msg_ctx = msg_ctx;
1132 status = addrchange_context_create(state, &state->ctx);
1133 if (!NT_STATUS_IS_OK(status)) {
1134 DEBUG(10, ("addrchange_context_create failed: %s\n",
1135 nt_errstr(status)));
1139 req = addrchange_send(state, ev, state->ctx);
1141 DEBUG(0, ("addrchange_send failed\n"));
1145 tevent_req_set_callback(req, winbindd_addr_changed, state);
1148 static void winbindd_addr_changed(struct tevent_req *req)
1150 struct winbindd_addrchanged_state *state = tevent_req_callback_data(
1151 req, struct winbindd_addrchanged_state);
1152 enum addrchange_type type;
1153 struct sockaddr_storage addr;
1156 status = addrchange_recv(req, &type, &addr);
1158 if (!NT_STATUS_IS_OK(status)) {
1159 DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
1160 nt_errstr(status)));
1164 if (type == ADDRCHANGE_DEL) {
1165 char addrstr[INET6_ADDRSTRLEN];
1168 print_sockaddr(addrstr, sizeof(addrstr), &addr);
1170 DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
1173 blob = data_blob_const(addrstr, strlen(addrstr)+1);
1175 status = messaging_send(state->msg_ctx,
1176 messaging_server_id(state->msg_ctx),
1177 MSG_WINBIND_IP_DROPPED, &blob);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 DEBUG(10, ("messaging_send failed: %s - ignoring\n",
1180 nt_errstr(status)));
1183 req = addrchange_send(state, state->ev, state->ctx);
1185 DEBUG(0, ("addrchange_send failed\n"));
1189 tevent_req_set_callback(req, winbindd_addr_changed, state);
1194 int main(int argc, char **argv, char **envp)
1196 static bool is_daemon = False;
1197 static bool Fork = True;
1198 static bool log_stdout = False;
1199 static bool no_process_group = False;
1203 OPT_NO_PROCESS_GROUP,
1206 struct poptOption long_options[] = {
1208 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1209 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1210 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1211 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1212 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1213 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1223 * Do this before any other talloc operation
1225 talloc_enable_null_tracking();
1226 frame = talloc_stackframe();
1228 /* glibc (?) likes to print "User defined signal 1" and exit if a
1229 SIGUSR[12] is received before a handler is installed */
1231 CatchSignal(SIGUSR1, SIG_IGN);
1232 CatchSignal(SIGUSR2, SIG_IGN);
1235 dump_core_setup("winbindd", lp_logfile());
1239 /* Initialise for running in non-root mode */
1243 set_remote_machine_name("winbindd", False);
1245 /* Set environment variable so we don't recursively call ourselves.
1246 This may also be useful interactively. */
1248 if ( !winbind_off() ) {
1249 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1253 /* Initialise samba/rpc client stuff */
1255 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1257 while ((opt = poptGetNextOpt(pc)) != -1) {
1259 /* Don't become a daemon */
1271 case OPT_NO_PROCESS_GROUP:
1272 no_process_group = true;
1274 case OPT_LOG_STDOUT:
1281 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1282 poptBadOption(pc, 0), poptStrerror(opt));
1283 poptPrintUsage(pc, stderr, 0);
1288 if (is_daemon && interactive) {
1289 d_fprintf(stderr,"\nERROR: "
1290 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1291 poptPrintUsage(pc, stderr, 0);
1295 if (log_stdout && Fork) {
1296 d_fprintf(stderr, "\nERROR: "
1297 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1298 poptPrintUsage(pc, stderr, 0);
1302 poptFreeContext(pc);
1304 if (!override_logfile) {
1306 if (asprintf(&lfile,"%s/log.winbindd",
1307 get_dyn_LOGFILEBASE()) > 0) {
1308 lp_set_logfile(lfile);
1313 setup_logging("winbindd", DEBUG_STDOUT);
1315 setup_logging("winbindd", DEBUG_FILE);
1319 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1320 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1322 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1323 DEBUG(0, ("error opening config file '%s'\n", get_dyn_CONFIGFILE()));
1327 /* Initialise messaging system */
1329 if (winbind_messaging_context() == NULL) {
1333 if (!reload_services_file(NULL)) {
1334 DEBUG(0, ("error opening config file\n"));
1338 if (!directory_exist(lp_lockdir())) {
1339 mkdir(lp_lockdir(), 0755);
1349 if (!secrets_init()) {
1351 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1355 /* Unblock all signals we are interested in as they may have been
1356 blocked by the parent process. */
1358 BlockSignals(False, SIGINT);
1359 BlockSignals(False, SIGQUIT);
1360 BlockSignals(False, SIGTERM);
1361 BlockSignals(False, SIGUSR1);
1362 BlockSignals(False, SIGUSR2);
1363 BlockSignals(False, SIGHUP);
1364 BlockSignals(False, SIGCHLD);
1367 become_daemon(Fork, no_process_group, log_stdout);
1369 pidfile_create("winbindd");
1373 * If we're interactive we want to set our own process group for
1374 * signal management.
1376 if (interactive && !no_process_group)
1377 setpgid( (pid_t)0, (pid_t)0);
1382 /* Don't use winbindd_reinit_after_fork here as
1383 * we're just starting up and haven't created any
1384 * winbindd-specific resources we must free yet. JRA.
1387 status = reinit_after_fork(winbind_messaging_context(),
1388 winbind_event_context(),
1389 procid_self(), false);
1390 if (!NT_STATUS_IS_OK(status)) {
1391 DEBUG(0,("reinit_after_fork() failed\n"));
1395 winbindd_register_handlers();
1397 status = init_system_info();
1398 if (!NT_STATUS_IS_OK(status)) {
1399 DEBUG(1, ("ERROR: failed to setup system user info: %s.\n",
1400 nt_errstr(status)));
1404 rpc_lsarpc_init(NULL);
1405 rpc_samr_init(NULL);
1407 winbindd_init_addrchange(NULL, winbind_event_context(),
1408 winbind_messaging_context());
1410 /* setup listen sockets */
1412 if (!winbindd_setup_listeners()) {
1413 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1418 /* Loop waiting for requests */
1420 frame = talloc_stackframe();
1422 if (tevent_loop_once(winbind_event_context()) == -1) {
1423 DEBUG(1, ("tevent_loop_once() failed: %s\n",