2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000-2002
7 Copyright (C) Andrew Tridgell 2002
8 Copyright (C) Jelmer Vernooij 2003
9 Copyright (C) Volker Lendecke 2004
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "../../nsswitch/libwbclient/wbc_async.h"
30 #define DBGC_CLASS DBGC_WINBIND
32 static void remove_client(struct winbindd_cli_state *state);
34 static bool opt_nocache = False;
35 static bool interactive = False;
37 extern bool override_logfile;
39 struct event_context *winbind_event_context(void)
41 static struct event_context *ctx;
43 if (!ctx && !(ctx = event_context_init(NULL))) {
44 smb_panic("Could not init winbind event context");
49 struct messaging_context *winbind_messaging_context(void)
51 static struct messaging_context *ctx;
54 ctx = messaging_init(NULL, procid_self(),
55 winbind_event_context());
58 DEBUG(0, ("Could not init winbind messaging context.\n"));
63 /* Reload configuration */
65 static bool reload_services_file(const char *lfile)
70 const char *fname = lp_configfile();
72 if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
73 set_dyn_CONFIGFILE(fname);
77 /* if this is a child, restore the logfile to the special
78 name - <domain>, idmap, etc. */
79 if (lfile && *lfile) {
80 lp_set_logfile(lfile);
84 ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
93 /**************************************************************************** **
95 **************************************************************************** */
97 static void fault_quit(void)
102 static void winbindd_status(void)
104 struct winbindd_cli_state *tmp;
106 DEBUG(0, ("winbindd status:\n"));
108 /* Print client state information */
110 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
112 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
113 DEBUG(2, ("\tclient list:\n"));
114 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
115 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
116 (unsigned long)tmp->pid, tmp->sock));
121 /* Print winbindd status to log file */
123 static void print_winbindd_status(void)
128 /* Flush client cache */
130 static void flush_caches(void)
132 /* We need to invalidate cached user list entries on a SIGHUP
133 otherwise cached access denied errors due to restrict anonymous
134 hang around until the sequence number changes. */
136 if (!wcache_invalidate_cache()) {
137 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
138 if (!winbindd_cache_validate_and_initialize()) {
144 static void flush_caches_noinit(void)
147 * We need to invalidate cached user list entries on a SIGHUP
148 * otherwise cached access denied errors due to restrict anonymous
149 * hang around until the sequence number changes.
151 * Skip uninitialized domains when flush cache.
152 * If domain is not initialized, it means it is never
153 * used or never become online. look, wcache_invalidate_cache()
154 * -> get_cache() -> init_dc_connection(). It causes a lot of traffic
155 * for unused domains and large traffic for primay domain's DC if there
159 if (!wcache_invalidate_cache_noinit()) {
160 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
161 if (!winbindd_cache_validate_and_initialize()) {
167 /* Handle the signal by unlinking socket and exiting */
169 static void terminate(bool is_parent)
172 /* When parent goes away we should
173 * remove the socket file. Not so
174 * when children terminate.
178 if (asprintf(&path, "%s/%s",
179 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
187 trustdom_cache_shutdown();
189 gencache_stabilize();
193 TALLOC_CTX *mem_ctx = talloc_init("end_description");
194 char *description = talloc_describe_all(mem_ctx);
196 DEBUG(3, ("tallocs left:\n%s\n", description));
197 talloc_destroy(mem_ctx);
202 serverid_deregister_self();
209 static void winbindd_sig_term_handler(struct tevent_context *ev,
210 struct tevent_signal *se,
216 bool *is_parent = talloc_get_type_abort(private_data, bool);
218 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
219 signum, (int)*is_parent));
220 terminate(*is_parent);
223 bool winbindd_setup_sig_term_handler(bool parent)
225 struct tevent_signal *se;
228 is_parent = talloc(winbind_event_context(), bool);
235 se = tevent_add_signal(winbind_event_context(),
238 winbindd_sig_term_handler,
241 DEBUG(0,("failed to setup SIGTERM handler"));
242 talloc_free(is_parent);
246 se = tevent_add_signal(winbind_event_context(),
249 winbindd_sig_term_handler,
252 DEBUG(0,("failed to setup SIGINT handler"));
253 talloc_free(is_parent);
257 se = tevent_add_signal(winbind_event_context(),
260 winbindd_sig_term_handler,
263 DEBUG(0,("failed to setup SIGINT handler"));
264 talloc_free(is_parent);
271 static void winbindd_sig_hup_handler(struct tevent_context *ev,
272 struct tevent_signal *se,
278 const char *file = (const char *)private_data;
280 DEBUG(1,("Reloading services after SIGHUP\n"));
281 flush_caches_noinit();
282 reload_services_file(file);
285 bool winbindd_setup_sig_hup_handler(const char *lfile)
287 struct tevent_signal *se;
291 file = talloc_strdup(winbind_event_context(),
298 se = tevent_add_signal(winbind_event_context(),
299 winbind_event_context(),
301 winbindd_sig_hup_handler,
310 static void winbindd_sig_chld_handler(struct tevent_context *ev,
311 struct tevent_signal *se,
319 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
320 winbind_child_died(pid);
324 static bool winbindd_setup_sig_chld_handler(void)
326 struct tevent_signal *se;
328 se = tevent_add_signal(winbind_event_context(),
329 winbind_event_context(),
331 winbindd_sig_chld_handler,
340 static void winbindd_sig_usr2_handler(struct tevent_context *ev,
341 struct tevent_signal *se,
347 print_winbindd_status();
350 static bool winbindd_setup_sig_usr2_handler(void)
352 struct tevent_signal *se;
354 se = tevent_add_signal(winbind_event_context(),
355 winbind_event_context(),
357 winbindd_sig_usr2_handler,
366 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
367 static void msg_reload_services(struct messaging_context *msg,
370 struct server_id server_id,
373 /* Flush various caches */
375 reload_services_file((const char *) private_data);
378 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
379 static void msg_shutdown(struct messaging_context *msg,
382 struct server_id server_id,
385 /* only the parent waits for this message */
386 DEBUG(0,("Got shutdown message\n"));
391 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
394 struct server_id server_id,
400 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
404 * call the validation code from a child:
405 * so we don't block the main winbindd and the validation
406 * code can safely use fork/waitpid...
408 child_pid = sys_fork();
410 if (child_pid == -1) {
411 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
416 if (child_pid != 0) {
418 DEBUG(5, ("winbind_msg_validate_cache: child created with "
419 "pid %d.\n", (int)child_pid));
425 if (!winbindd_reinit_after_fork(NULL)) {
429 /* install default SIGCHLD handler: validation code uses fork/waitpid */
430 CatchSignal(SIGCHLD, SIG_DFL);
432 ret = (uint8)winbindd_validate_cache_nobackup();
433 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
434 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
439 static struct winbindd_dispatch_table {
440 enum winbindd_cmd cmd;
441 void (*fn)(struct winbindd_cli_state *state);
442 const char *winbindd_cmd_name;
443 } dispatch_table[] = {
445 /* PAM auth functions */
447 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
448 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
449 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
451 /* Enumeration functions */
453 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
458 { WINBINDD_INFO, winbindd_info, "INFO" },
459 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
460 "INTERFACE_VERSION" },
461 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
462 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
463 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
464 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
465 "WINBINDD_PRIV_PIPE_DIR" },
467 /* Credential cache access */
468 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
469 { WINBINDD_CCACHE_SAVE, winbindd_ccache_save, "CCACHE_SAVE" },
473 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
474 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
478 { WINBINDD_NUM_CMDS, NULL, "NONE" }
481 struct winbindd_async_dispatch_table {
482 enum winbindd_cmd cmd;
483 const char *cmd_name;
484 struct tevent_req *(*send_req)(TALLOC_CTX *mem_ctx,
485 struct tevent_context *ev,
486 struct winbindd_cli_state *cli,
487 struct winbindd_request *request);
488 NTSTATUS (*recv_req)(struct tevent_req *req,
489 struct winbindd_response *presp);
492 static struct winbindd_async_dispatch_table async_nonpriv_table[] = {
493 { WINBINDD_PING, "PING",
494 wb_ping_send, wb_ping_recv },
495 { WINBINDD_LOOKUPSID, "LOOKUPSID",
496 winbindd_lookupsid_send, winbindd_lookupsid_recv },
497 { WINBINDD_LOOKUPNAME, "LOOKUPNAME",
498 winbindd_lookupname_send, winbindd_lookupname_recv },
499 { WINBINDD_SID_TO_UID, "SID_TO_UID",
500 winbindd_sid_to_uid_send, winbindd_sid_to_uid_recv },
501 { WINBINDD_SID_TO_GID, "SID_TO_GID",
502 winbindd_sid_to_gid_send, winbindd_sid_to_gid_recv },
503 { WINBINDD_UID_TO_SID, "UID_TO_SID",
504 winbindd_uid_to_sid_send, winbindd_uid_to_sid_recv },
505 { WINBINDD_GID_TO_SID, "GID_TO_SID",
506 winbindd_gid_to_sid_send, winbindd_gid_to_sid_recv },
507 { WINBINDD_GETPWSID, "GETPWSID",
508 winbindd_getpwsid_send, winbindd_getpwsid_recv },
509 { WINBINDD_GETPWNAM, "GETPWNAM",
510 winbindd_getpwnam_send, winbindd_getpwnam_recv },
511 { WINBINDD_GETPWUID, "GETPWUID",
512 winbindd_getpwuid_send, winbindd_getpwuid_recv },
513 { WINBINDD_GETSIDALIASES, "GETSIDALIASES",
514 winbindd_getsidaliases_send, winbindd_getsidaliases_recv },
515 { WINBINDD_GETUSERDOMGROUPS, "GETUSERDOMGROUPS",
516 winbindd_getuserdomgroups_send, winbindd_getuserdomgroups_recv },
517 { WINBINDD_GETGROUPS, "GETGROUPS",
518 winbindd_getgroups_send, winbindd_getgroups_recv },
519 { WINBINDD_SHOW_SEQUENCE, "SHOW_SEQUENCE",
520 winbindd_show_sequence_send, winbindd_show_sequence_recv },
521 { WINBINDD_GETGRGID, "GETGRGID",
522 winbindd_getgrgid_send, winbindd_getgrgid_recv },
523 { WINBINDD_GETGRNAM, "GETGRNAM",
524 winbindd_getgrnam_send, winbindd_getgrnam_recv },
525 { WINBINDD_GETUSERSIDS, "GETUSERSIDS",
526 winbindd_getusersids_send, winbindd_getusersids_recv },
527 { WINBINDD_LOOKUPRIDS, "LOOKUPRIDS",
528 winbindd_lookuprids_send, winbindd_lookuprids_recv },
529 { WINBINDD_SETPWENT, "SETPWENT",
530 winbindd_setpwent_send, winbindd_setpwent_recv },
531 { WINBINDD_GETPWENT, "GETPWENT",
532 winbindd_getpwent_send, winbindd_getpwent_recv },
533 { WINBINDD_ENDPWENT, "ENDPWENT",
534 winbindd_endpwent_send, winbindd_endpwent_recv },
535 { WINBINDD_DSGETDCNAME, "DSGETDCNAME",
536 winbindd_dsgetdcname_send, winbindd_dsgetdcname_recv },
537 { WINBINDD_GETDCNAME, "GETDCNAME",
538 winbindd_getdcname_send, winbindd_getdcname_recv },
539 { WINBINDD_SETGRENT, "SETGRENT",
540 winbindd_setgrent_send, winbindd_setgrent_recv },
541 { WINBINDD_GETGRENT, "GETGRENT",
542 winbindd_getgrent_send, winbindd_getgrent_recv },
543 { WINBINDD_ENDGRENT, "ENDGRENT",
544 winbindd_endgrent_send, winbindd_endgrent_recv },
545 { WINBINDD_LIST_USERS, "LIST_USERS",
546 winbindd_list_users_send, winbindd_list_users_recv },
547 { WINBINDD_LIST_GROUPS, "LIST_GROUPS",
548 winbindd_list_groups_send, winbindd_list_groups_recv },
549 { WINBINDD_CHECK_MACHACC, "CHECK_MACHACC",
550 winbindd_check_machine_acct_send, winbindd_check_machine_acct_recv },
551 { WINBINDD_PING_DC, "PING_DC",
552 winbindd_ping_dc_send, winbindd_ping_dc_recv },
553 { WINBINDD_PAM_AUTH, "PAM_AUTH",
554 winbindd_pam_auth_send, winbindd_pam_auth_recv },
556 { 0, NULL, NULL, NULL }
559 static struct winbindd_async_dispatch_table async_priv_table[] = {
560 { WINBINDD_ALLOCATE_UID, "ALLOCATE_UID",
561 winbindd_allocate_uid_send, winbindd_allocate_uid_recv },
562 { WINBINDD_ALLOCATE_GID, "ALLOCATE_GID",
563 winbindd_allocate_gid_send, winbindd_allocate_gid_recv },
564 { WINBINDD_SET_MAPPING, "SET_MAPPING",
565 winbindd_set_mapping_send, winbindd_set_mapping_recv },
566 { WINBINDD_REMOVE_MAPPING, "SET_MAPPING",
567 winbindd_remove_mapping_send, winbindd_remove_mapping_recv },
568 { WINBINDD_SET_HWM, "SET_HWM",
569 winbindd_set_hwm_send, winbindd_set_hwm_recv },
570 { WINBINDD_CHANGE_MACHACC, "CHANGE_MACHACC",
571 winbindd_change_machine_acct_send, winbindd_change_machine_acct_recv },
572 { WINBINDD_PAM_AUTH_CRAP, "PAM_AUTH_CRAP",
573 winbindd_pam_auth_crap_send, winbindd_pam_auth_crap_recv },
575 { 0, NULL, NULL, NULL }
578 static void wb_request_done(struct tevent_req *req);
580 static void process_request(struct winbindd_cli_state *state)
582 struct winbindd_dispatch_table *table = dispatch_table;
583 struct winbindd_async_dispatch_table *atable;
585 state->mem_ctx = talloc_init("winbind request");
586 if (state->mem_ctx == NULL)
589 /* Remember who asked us. */
590 state->pid = state->request->pid;
592 state->cmd_name = "unknown request";
593 state->recv_fn = NULL;
595 /* Process command */
597 for (atable = async_nonpriv_table; atable->send_req; atable += 1) {
598 if (state->request->cmd == atable->cmd) {
603 if ((atable->send_req == NULL) && state->privileged) {
604 for (atable = async_priv_table; atable->send_req;
606 if (state->request->cmd == atable->cmd) {
612 if (atable->send_req != NULL) {
613 struct tevent_req *req;
615 state->cmd_name = atable->cmd_name;
616 state->recv_fn = atable->recv_req;
618 DEBUG(10, ("process_request: Handling async request %d:%s\n",
619 (int)state->pid, state->cmd_name));
621 req = atable->send_req(state->mem_ctx, winbind_event_context(),
622 state, state->request);
624 DEBUG(0, ("process_request: atable->send failed for "
625 "%s\n", atable->cmd_name));
626 request_error(state);
629 tevent_req_set_callback(req, wb_request_done, state);
633 state->response = talloc_zero(state->mem_ctx,
634 struct winbindd_response);
635 if (state->response == NULL) {
636 DEBUG(10, ("talloc failed\n"));
637 remove_client(state);
640 state->response->result = WINBINDD_PENDING;
641 state->response->length = sizeof(struct winbindd_response);
643 for (table = dispatch_table; table->fn; table++) {
644 if (state->request->cmd == table->cmd) {
645 DEBUG(10,("process_request: request fn %s\n",
646 table->winbindd_cmd_name ));
647 state->cmd_name = table->winbindd_cmd_name;
654 DEBUG(10,("process_request: unknown request fn number %d\n",
655 (int)state->request->cmd ));
656 request_error(state);
660 static void wb_request_done(struct tevent_req *req)
662 struct winbindd_cli_state *state = tevent_req_callback_data(
663 req, struct winbindd_cli_state);
666 state->response = talloc_zero(state->mem_ctx,
667 struct winbindd_response);
668 if (state->response == NULL) {
669 DEBUG(0, ("wb_request_done[%d:%s]: talloc_zero failed - removing client\n",
670 (int)state->pid, state->cmd_name));
671 remove_client(state);
674 state->response->result = WINBINDD_PENDING;
675 state->response->length = sizeof(struct winbindd_response);
677 status = state->recv_fn(req, state->response);
680 DEBUG(10,("wb_request_done[%d:%s]: %s\n",
681 (int)state->pid, state->cmd_name, nt_errstr(status)));
683 if (!NT_STATUS_IS_OK(status)) {
684 request_error(state);
691 * This is the main event loop of winbind requests. It goes through a
692 * state-machine of 3 read/write requests, 4 if you have extra data to send.
694 * An idle winbind client has a read request of 4 bytes outstanding,
695 * finalizing function is request_len_recv, checking the length. request_recv
696 * then processes the packet. The processing function then at some point has
697 * to call request_finished which schedules sending the response.
700 static void request_finished(struct winbindd_cli_state *state);
702 static void winbind_client_request_read(struct tevent_req *req);
703 static void winbind_client_response_written(struct tevent_req *req);
705 static void request_finished(struct winbindd_cli_state *state)
707 struct tevent_req *req;
709 TALLOC_FREE(state->request);
711 req = wb_resp_write_send(state, winbind_event_context(),
712 state->out_queue, state->sock,
715 DEBUG(10,("request_finished[%d:%s]: wb_resp_write_send() failed\n",
716 (int)state->pid, state->cmd_name));
717 remove_client(state);
720 tevent_req_set_callback(req, winbind_client_response_written, state);
723 static void winbind_client_response_written(struct tevent_req *req)
725 struct winbindd_cli_state *state = tevent_req_callback_data(
726 req, struct winbindd_cli_state);
730 ret = wb_resp_write_recv(req, &err);
735 DEBUG(2, ("Could not write response[%d:%s] to client: %s\n",
736 (int)state->pid, state->cmd_name, strerror(err)));
737 remove_client(state);
741 DEBUG(10,("winbind_client_response_written[%d:%s]: delivered response "
742 "to client\n", (int)state->pid, state->cmd_name));
744 TALLOC_FREE(state->mem_ctx);
745 state->response = NULL;
746 state->cmd_name = "no request";
747 state->recv_fn = NULL;
749 req = wb_req_read_send(state, winbind_event_context(), state->sock,
750 WINBINDD_MAX_EXTRA_DATA);
752 remove_client(state);
755 tevent_req_set_callback(req, winbind_client_request_read, state);
758 void request_error(struct winbindd_cli_state *state)
760 SMB_ASSERT(state->response->result == WINBINDD_PENDING);
761 state->response->result = WINBINDD_ERROR;
762 request_finished(state);
765 void request_ok(struct winbindd_cli_state *state)
767 SMB_ASSERT(state->response->result == WINBINDD_PENDING);
768 state->response->result = WINBINDD_OK;
769 request_finished(state);
772 /* Process a new connection by adding it to the client connection list */
774 static void new_connection(int listen_sock, bool privileged)
776 struct sockaddr_un sunaddr;
777 struct winbindd_cli_state *state;
778 struct tevent_req *req;
782 /* Accept connection */
784 len = sizeof(sunaddr);
787 sock = accept(listen_sock, (struct sockaddr *)(void *)&sunaddr,
789 } while (sock == -1 && errno == EINTR);
794 DEBUG(6,("accepted socket %d\n", sock));
796 /* Create new connection structure */
798 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
805 state->out_queue = tevent_queue_create(state, "winbind client reply");
806 if (state->out_queue == NULL) {
812 state->last_access = time(NULL);
814 state->privileged = privileged;
816 req = wb_req_read_send(state, winbind_event_context(), state->sock,
817 WINBINDD_MAX_EXTRA_DATA);
823 tevent_req_set_callback(req, winbind_client_request_read, state);
825 /* Add to connection list */
827 winbindd_add_client(state);
830 static void winbind_client_request_read(struct tevent_req *req)
832 struct winbindd_cli_state *state = tevent_req_callback_data(
833 req, struct winbindd_cli_state);
837 ret = wb_req_read_recv(req, state, &state->request, &err);
841 DEBUG(6, ("closing socket %d, client exited\n",
844 DEBUG(2, ("Could not read client request from fd %d: "
845 "%s\n", state->sock, strerror(err)));
849 remove_client(state);
852 process_request(state);
855 /* Remove a client connection from client connection list */
857 static void remove_client(struct winbindd_cli_state *state)
862 /* It's a dead client - hold a funeral */
868 if (state->sock != -1) {
869 /* tell client, we are closing ... */
870 nwritten = write(state->sock, &c, sizeof(c));
871 if (nwritten == -1) {
872 DEBUG(2, ("final write to client failed: %s\n",
882 TALLOC_FREE(state->mem_ctx);
884 /* Remove from list and free */
886 winbindd_remove_client(state);
890 /* Shutdown client connection which has been idle for the longest time */
892 static bool remove_idle_client(void)
894 struct winbindd_cli_state *state, *remove_state = NULL;
895 time_t last_access = 0;
898 for (state = winbindd_client_list(); state; state = state->next) {
899 if (state->response == NULL &&
900 !state->pwent_state && !state->grent_state) {
902 if (!last_access || state->last_access < last_access) {
903 last_access = state->last_access;
904 remove_state = state;
910 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
911 nidle, remove_state->sock, (unsigned int)remove_state->pid));
912 remove_client(remove_state);
919 struct winbindd_listen_state {
924 static void winbindd_listen_fde_handler(struct tevent_context *ev,
925 struct tevent_fd *fde,
929 struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
930 struct winbindd_listen_state);
932 while (winbindd_num_clients() >
933 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
934 DEBUG(5,("winbindd: Exceeding %d client "
935 "connections, removing idle "
937 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
938 if (!remove_idle_client()) {
939 DEBUG(0,("winbindd: Exceeding %d "
940 "client connections, no idle "
941 "connection found\n",
942 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
946 new_connection(s->fd, s->privileged);
949 static bool winbindd_setup_listeners(void)
951 struct winbindd_listen_state *pub_state = NULL;
952 struct winbindd_listen_state *priv_state = NULL;
953 struct tevent_fd *fde;
955 pub_state = talloc(winbind_event_context(),
956 struct winbindd_listen_state);
961 pub_state->privileged = false;
962 pub_state->fd = open_winbindd_socket();
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 = open_winbindd_priv_socket();
984 if (priv_state->fd == -1) {
988 fde = tevent_add_fd(winbind_event_context(), priv_state,
989 priv_state->fd, TEVENT_FD_READ,
990 winbindd_listen_fde_handler, priv_state);
992 close(priv_state->fd);
995 tevent_fd_set_auto_close(fde);
999 TALLOC_FREE(pub_state);
1000 TALLOC_FREE(priv_state);
1004 bool winbindd_use_idmap_cache(void)
1006 return !opt_nocache;
1009 bool winbindd_use_cache(void)
1011 return !opt_nocache;
1016 int main(int argc, char **argv, char **envp)
1018 static bool is_daemon = False;
1019 static bool Fork = True;
1020 static bool log_stdout = False;
1021 static bool no_process_group = False;
1025 OPT_NO_PROCESS_GROUP,
1028 struct poptOption long_options[] = {
1030 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1031 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1032 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1033 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1034 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1035 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1041 TALLOC_CTX *frame = talloc_stackframe();
1042 struct tevent_timer *te;
1044 /* glibc (?) likes to print "User defined signal 1" and exit if a
1045 SIGUSR[12] is received before a handler is installed */
1047 CatchSignal(SIGUSR1, SIG_IGN);
1048 CatchSignal(SIGUSR2, SIG_IGN);
1050 fault_setup((void (*)(void *))fault_quit );
1051 dump_core_setup("winbindd");
1055 /* Initialise for running in non-root mode */
1059 set_remote_machine_name("winbindd", False);
1061 /* Set environment variable so we don't recursively call ourselves.
1062 This may also be useful interactively. */
1064 if ( !winbind_off() ) {
1065 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1069 /* Initialise samba/rpc client stuff */
1071 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1073 while ((opt = poptGetNextOpt(pc)) != -1) {
1075 /* Don't become a daemon */
1087 case OPT_NO_PROCESS_GROUP:
1088 no_process_group = true;
1090 case OPT_LOG_STDOUT:
1097 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1098 poptBadOption(pc, 0), poptStrerror(opt));
1099 poptPrintUsage(pc, stderr, 0);
1104 if (is_daemon && interactive) {
1105 d_fprintf(stderr,"\nERROR: "
1106 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1107 poptPrintUsage(pc, stderr, 0);
1111 if (log_stdout && Fork) {
1112 d_fprintf(stderr, "\nERROR: "
1113 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1114 poptPrintUsage(pc, stderr, 0);
1118 poptFreeContext(pc);
1120 if (!override_logfile) {
1122 if (asprintf(&lfile,"%s/log.winbindd",
1123 get_dyn_LOGFILEBASE()) > 0) {
1124 lp_set_logfile(lfile);
1128 setup_logging("winbindd", log_stdout);
1131 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1132 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1134 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1135 DEBUG(0, ("error opening config file\n"));
1139 /* Initialise messaging system */
1141 if (winbind_messaging_context() == NULL) {
1145 if (!reload_services_file(NULL)) {
1146 DEBUG(0, ("error opening config file\n"));
1150 if (!directory_exist(lp_lockdir())) {
1151 mkdir(lp_lockdir(), 0755);
1161 if (!secrets_init()) {
1163 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1167 /* Enable netbios namecache */
1171 /* Unblock all signals we are interested in as they may have been
1172 blocked by the parent process. */
1174 BlockSignals(False, SIGINT);
1175 BlockSignals(False, SIGQUIT);
1176 BlockSignals(False, SIGTERM);
1177 BlockSignals(False, SIGUSR1);
1178 BlockSignals(False, SIGUSR2);
1179 BlockSignals(False, SIGHUP);
1180 BlockSignals(False, SIGCHLD);
1183 become_daemon(Fork, no_process_group, log_stdout);
1185 pidfile_create("winbindd");
1189 * If we're interactive we want to set our own process group for
1190 * signal management.
1192 if (interactive && !no_process_group)
1193 setpgid( (pid_t)0, (pid_t)0);
1198 /* Don't use winbindd_reinit_after_fork here as
1199 * we're just starting up and haven't created any
1200 * winbindd-specific resources we must free yet. JRA.
1203 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1204 winbind_event_context(),
1206 DEBUG(0,("reinit_after_fork() failed\n"));
1210 /* Setup signal handlers */
1212 if (!winbindd_setup_sig_term_handler(true))
1214 if (!winbindd_setup_sig_hup_handler(NULL))
1216 if (!winbindd_setup_sig_chld_handler())
1218 if (!winbindd_setup_sig_usr2_handler())
1221 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1224 * Ensure all cache and idmap caches are consistent
1225 * and initialized before we startup.
1227 if (!winbindd_cache_validate_and_initialize()) {
1231 /* get broadcast messages */
1233 if (!serverid_register_self(FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
1234 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1238 /* React on 'smbcontrol winbindd reload-config' in the same way
1239 as to SIGHUP signal */
1240 messaging_register(winbind_messaging_context(), NULL,
1241 MSG_SMB_CONF_UPDATED, msg_reload_services);
1242 messaging_register(winbind_messaging_context(), NULL,
1243 MSG_SHUTDOWN, msg_shutdown);
1245 /* Handle online/offline messages. */
1246 messaging_register(winbind_messaging_context(), NULL,
1247 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1248 messaging_register(winbind_messaging_context(), NULL,
1249 MSG_WINBIND_ONLINE, winbind_msg_online);
1250 messaging_register(winbind_messaging_context(), NULL,
1251 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1253 messaging_register(winbind_messaging_context(), NULL,
1254 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1256 messaging_register(winbind_messaging_context(), NULL,
1257 MSG_WINBIND_VALIDATE_CACHE,
1258 winbind_msg_validate_cache);
1260 messaging_register(winbind_messaging_context(), NULL,
1261 MSG_WINBIND_DUMP_DOMAIN_LIST,
1262 winbind_msg_dump_domain_list);
1264 /* Register handler for MSG_DEBUG. */
1265 messaging_register(winbind_messaging_context(), NULL,
1269 netsamlogon_cache_init(); /* Non-critical */
1271 /* clear the cached list of trusted domains */
1275 if (!init_domain_list()) {
1276 DEBUG(0,("unable to initialize domain list\n"));
1281 init_locator_child();
1283 smb_nscd_flush_user_cache();
1284 smb_nscd_flush_group_cache();
1286 /* setup listen sockets */
1288 if (!winbindd_setup_listeners()) {
1289 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1293 te = tevent_add_timer(winbind_event_context(), NULL, timeval_zero(),
1294 rescan_trusted_domains, NULL);
1296 DEBUG(0, ("Could not trigger rescan_trusted_domains()\n"));
1301 /* Loop waiting for requests */
1303 frame = talloc_stackframe();
1305 if (tevent_loop_once(winbind_event_context()) == -1) {
1306 DEBUG(1, ("tevent_loop_once() failed: %s\n",