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 "lib/addrchange.h"
30 #define DBGC_CLASS DBGC_WINBIND
32 static bool opt_nocache = False;
33 static bool interactive = False;
35 extern bool override_logfile;
37 struct event_context *winbind_event_context(void)
39 static struct event_context *ctx;
41 if (!ctx && !(ctx = event_context_init(NULL))) {
42 smb_panic("Could not init winbind event context");
47 struct messaging_context *winbind_messaging_context(void)
49 static struct messaging_context *ctx;
52 ctx = messaging_init(NULL, server_id_self(),
53 winbind_event_context());
56 DEBUG(0, ("Could not init winbind 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 /**************************************************************************** **
93 **************************************************************************** */
95 static void fault_quit(void)
100 static void winbindd_status(void)
102 struct winbindd_cli_state *tmp;
104 DEBUG(0, ("winbindd status:\n"));
106 /* Print client state information */
108 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
110 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
111 DEBUG(2, ("\tclient list:\n"));
112 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
113 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
114 (unsigned long)tmp->pid, tmp->sock));
119 /* Print winbindd status to log file */
121 static void print_winbindd_status(void)
126 /* Flush client cache */
128 static void flush_caches(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 if (!wcache_invalidate_cache()) {
135 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
136 if (!winbindd_cache_validate_and_initialize()) {
142 /* Handle the signal by unlinking socket and exiting */
144 static void terminate(bool is_parent)
147 /* When parent goes away we should
148 * remove the socket file. Not so
149 * when children terminate.
153 if (asprintf(&path, "%s/%s",
154 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
162 trustdom_cache_shutdown();
164 gencache_stabilize();
168 TALLOC_CTX *mem_ctx = talloc_init("end_description");
169 char *description = talloc_describe_all(mem_ctx);
171 DEBUG(3, ("tallocs left:\n%s\n", description));
172 talloc_destroy(mem_ctx);
183 static void winbindd_sig_term_handler(struct tevent_context *ev,
184 struct tevent_signal *se,
190 bool *is_parent = talloc_get_type_abort(private_data, bool);
192 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
193 signum, (int)*is_parent));
195 /* if the parent killed us due to a timeout log where we were */
196 if (signum == SIGXCPU) {
197 log_stack_trace_with_level(1);
200 terminate(*is_parent);
203 bool winbindd_setup_sig_term_handler(bool parent)
205 struct tevent_signal *se;
208 is_parent = talloc(winbind_event_context(), bool);
215 se = tevent_add_signal(winbind_event_context(),
218 winbindd_sig_term_handler,
221 DEBUG(0,("failed to setup SIGTERM handler"));
222 talloc_free(is_parent);
226 se = tevent_add_signal(winbind_event_context(),
229 winbindd_sig_term_handler,
232 DEBUG(0,("failed to setup SIGINT handler"));
233 talloc_free(is_parent);
237 se = tevent_add_signal(winbind_event_context(),
240 winbindd_sig_term_handler,
243 DEBUG(0,("failed to setup SIGINT handler"));
244 talloc_free(is_parent);
248 se = tevent_add_signal(winbind_event_context(),
251 winbindd_sig_term_handler,
254 DEBUG(0,("failed to setup SIGXCPU handler"));
255 talloc_free(is_parent);
261 static void winbindd_sig_hup_handler(struct tevent_context *ev,
262 struct tevent_signal *se,
268 const char *file = (const char *)private_data;
270 DEBUG(1,("Reloading services after SIGHUP\n"));
272 reload_services_file(file);
275 bool winbindd_setup_sig_hup_handler(const char *lfile)
277 struct tevent_signal *se;
281 file = talloc_strdup(winbind_event_context(),
288 se = tevent_add_signal(winbind_event_context(),
289 winbind_event_context(),
291 winbindd_sig_hup_handler,
300 static void winbindd_sig_chld_handler(struct tevent_context *ev,
301 struct tevent_signal *se,
309 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
310 winbind_child_died(pid);
314 static bool winbindd_setup_sig_chld_handler(void)
316 struct tevent_signal *se;
318 se = tevent_add_signal(winbind_event_context(),
319 winbind_event_context(),
321 winbindd_sig_chld_handler,
330 static void winbindd_sig_usr2_handler(struct tevent_context *ev,
331 struct tevent_signal *se,
337 print_winbindd_status();
340 static bool winbindd_setup_sig_usr2_handler(void)
342 struct tevent_signal *se;
344 se = tevent_add_signal(winbind_event_context(),
345 winbind_event_context(),
347 winbindd_sig_usr2_handler,
356 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
357 static void msg_reload_services(struct messaging_context *msg,
360 struct server_id server_id,
363 /* Flush various caches */
365 reload_services_file((const char *) private_data);
368 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
369 static void msg_shutdown(struct messaging_context *msg,
372 struct server_id server_id,
375 /* only the parent waits for this message */
376 DEBUG(0,("Got shutdown message\n"));
381 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
384 struct server_id server_id,
390 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
394 * call the validation code from a child:
395 * so we don't block the main winbindd and the validation
396 * code can safely use fork/waitpid...
398 child_pid = sys_fork();
400 if (child_pid == -1) {
401 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
406 if (child_pid != 0) {
408 DEBUG(5, ("winbind_msg_validate_cache: child created with "
409 "pid %d.\n", (int)child_pid));
415 if (!winbindd_reinit_after_fork(NULL)) {
419 /* install default SIGCHLD handler: validation code uses fork/waitpid */
420 CatchSignal(SIGCHLD, SIG_DFL);
422 ret = (uint8)winbindd_validate_cache_nobackup();
423 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
424 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
429 static struct winbindd_dispatch_table {
430 enum winbindd_cmd cmd;
431 void (*fn)(struct winbindd_cli_state *state);
432 const char *winbindd_cmd_name;
433 } dispatch_table[] = {
437 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
438 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
439 { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
441 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
442 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
443 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
445 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
446 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
447 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
448 "GETUSERDOMGROUPS" },
449 { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
450 "LOOKUPUSERALIASES" },
452 /* Group functions */
454 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
455 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
456 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
457 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
458 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
459 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
461 /* PAM auth functions */
463 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
464 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
465 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
466 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
467 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
469 /* Enumeration functions */
471 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
472 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
473 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
475 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
477 /* SID related functions */
479 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
480 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
481 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
483 /* Lookup related functions */
485 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
486 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
487 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
488 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
489 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
490 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
494 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
495 { WINBINDD_PING_DC, winbindd_ping_dc, "PING_DC" },
496 { WINBINDD_PING, winbindd_ping, "PING" },
497 { WINBINDD_INFO, winbindd_info, "INFO" },
498 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
499 "INTERFACE_VERSION" },
500 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
501 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
502 { WINBINDD_DC_INFO, winbindd_dc_info, "DC_INFO" },
503 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
504 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
505 "WINBINDD_PRIV_PIPE_DIR" },
506 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
507 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
509 /* Credential cache access */
510 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
514 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
515 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
519 { WINBINDD_NUM_CMDS, NULL, "NONE" }
522 static void process_request(struct winbindd_cli_state *state)
524 struct winbindd_dispatch_table *table = dispatch_table;
526 /* Free response data - we may be interrupted and receive another
527 command before being able to send this data off. */
529 SAFE_FREE(state->response.extra_data.data);
531 ZERO_STRUCT(state->response);
533 state->response.result = WINBINDD_PENDING;
534 state->response.length = sizeof(struct winbindd_response);
536 state->mem_ctx = talloc_init("winbind request");
537 if (state->mem_ctx == NULL)
540 /* Remember who asked us. */
541 state->pid = state->request.pid;
543 /* Process command */
545 for (table = dispatch_table; table->fn; table++) {
546 if (state->request.cmd == table->cmd) {
547 DEBUG(10,("process_request: request fn %s\n",
548 table->winbindd_cmd_name ));
555 DEBUG(10,("process_request: unknown request fn number %d\n",
556 (int)state->request.cmd ));
557 request_error(state);
562 * A list of file descriptors being monitored by select in the main processing
563 * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
566 static struct winbindd_fd_event *fd_events = NULL;
568 void add_fd_event(struct winbindd_fd_event *ev)
570 struct winbindd_fd_event *match;
572 /* only add unique winbindd_fd_event structs */
574 for (match=fd_events; match; match=match->next ) {
576 SMB_ASSERT( match != ev );
583 DLIST_ADD(fd_events, ev);
586 void remove_fd_event(struct winbindd_fd_event *ev)
588 DLIST_REMOVE(fd_events, ev);
592 * Handler for winbindd_fd_events to complete a read/write request, set up by
593 * setup_async_read/setup_async_write.
596 static void rw_callback(struct winbindd_fd_event *event, int flags)
601 todo = event->length - event->done;
603 if (event->flags & EVENT_FD_WRITE) {
604 SMB_ASSERT(flags == EVENT_FD_WRITE);
605 done = sys_write(event->fd,
606 &((char *)event->data)[event->done],
611 event->finished(event->private_data, False);
616 if (event->flags & EVENT_FD_READ) {
617 SMB_ASSERT(flags == EVENT_FD_READ);
618 done = sys_read(event->fd, &((char *)event->data)[event->done],
623 event->finished(event->private_data, False);
630 if (event->done == event->length) {
632 event->finished(event->private_data, True);
637 * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
638 * when the request is completed or an error had occurred.
641 void setup_async_read(struct winbindd_fd_event *event, void *data, size_t length,
642 void (*finished)(void *private_data, bool success),
645 SMB_ASSERT(event->flags == 0);
647 event->length = length;
649 event->handler = rw_callback;
650 event->finished = finished;
651 event->private_data = private_data;
652 event->flags = EVENT_FD_READ;
655 void setup_async_write(struct winbindd_fd_event *event, void *data, size_t length,
656 void (*finished)(void *private_data, bool success),
659 SMB_ASSERT(event->flags == 0);
661 event->length = length;
663 event->handler = rw_callback;
664 event->finished = finished;
665 event->private_data = private_data;
666 event->flags = EVENT_FD_WRITE;
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_len_recv(void *private_data, bool success);
680 static void request_recv(void *private_data, bool success);
681 static void request_main_recv(void *private_data, bool success);
682 static void request_finished(struct winbindd_cli_state *state);
683 static void response_main_sent(void *private_data, bool success);
684 static void response_extra_sent(void *private_data, bool success);
686 static void response_extra_sent(void *private_data, bool success)
688 struct winbindd_cli_state *state =
689 talloc_get_type_abort(private_data, struct winbindd_cli_state);
691 TALLOC_FREE(state->mem_ctx);
694 state->finished = True;
698 SAFE_FREE(state->response.extra_data.data);
700 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
701 request_len_recv, state);
704 static void response_main_sent(void *private_data, bool success)
706 struct winbindd_cli_state *state =
707 talloc_get_type_abort(private_data, struct winbindd_cli_state);
710 state->finished = True;
714 if (state->response.length == sizeof(state->response)) {
715 TALLOC_FREE(state->mem_ctx);
717 setup_async_read(&state->fd_event, &state->request,
718 sizeof(uint32), request_len_recv, state);
722 setup_async_write(&state->fd_event, state->response.extra_data.data,
723 state->response.length - sizeof(state->response),
724 response_extra_sent, state);
727 static void request_finished(struct winbindd_cli_state *state)
729 /* Make sure request.extra_data is freed when finish processing a request */
730 SAFE_FREE(state->request.extra_data.data);
731 setup_async_write(&state->fd_event, &state->response,
732 sizeof(state->response), response_main_sent, state);
735 void request_error(struct winbindd_cli_state *state)
737 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
738 state->response.result = WINBINDD_ERROR;
739 request_finished(state);
742 void request_ok(struct winbindd_cli_state *state)
744 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
745 state->response.result = WINBINDD_OK;
746 request_finished(state);
749 static void request_len_recv(void *private_data, bool success)
751 struct winbindd_cli_state *state =
752 talloc_get_type_abort(private_data, struct winbindd_cli_state);
755 state->finished = True;
759 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
760 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
761 *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
762 state->finished = True;
766 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
767 sizeof(state->request) - sizeof(uint32),
768 request_main_recv, state);
771 static void request_main_recv(void *private_data, bool success)
773 struct winbindd_cli_state *state =
774 talloc_get_type_abort(private_data, struct winbindd_cli_state);
777 state->finished = True;
781 if (state->request.extra_len == 0) {
782 state->request.extra_data.data = NULL;
783 request_recv(state, True);
787 if ((!state->privileged) &&
788 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
789 DEBUG(3, ("Got request with %d bytes extra data on "
790 "unprivileged socket\n", (int)state->request.extra_len));
791 state->request.extra_data.data = NULL;
792 state->finished = True;
796 state->request.extra_data.data =
797 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
799 if (state->request.extra_data.data == NULL) {
800 DEBUG(0, ("malloc failed\n"));
801 state->finished = True;
805 /* Ensure null termination */
806 state->request.extra_data.data[state->request.extra_len] = '\0';
808 setup_async_read(&state->fd_event, state->request.extra_data.data,
809 state->request.extra_len, request_recv, state);
812 static void request_recv(void *private_data, bool success)
814 struct winbindd_cli_state *state =
815 talloc_get_type_abort(private_data, struct winbindd_cli_state);
818 state->finished = True;
822 process_request(state);
825 /* Process a new connection by adding it to the client connection list */
827 static void new_connection(int listen_sock, bool privileged)
829 struct sockaddr_un sunaddr;
830 struct winbindd_cli_state *state;
834 /* Accept connection */
836 len = sizeof(sunaddr);
839 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
840 } while (sock == -1 && errno == EINTR);
845 DEBUG(6,("accepted socket %d\n", sock));
847 /* Create new connection structure */
849 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
856 state->last_access = time(NULL);
858 state->privileged = privileged;
860 state->fd_event.fd = state->sock;
861 state->fd_event.flags = 0;
862 add_fd_event(&state->fd_event);
864 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
865 request_len_recv, state);
867 /* Add to connection list */
869 winbindd_add_client(state);
872 /* Remove a client connection from client connection list */
874 static void remove_client(struct winbindd_cli_state *state)
879 /* It's a dead client - hold a funeral */
885 if (!state->finished) {
886 /* tell client, we are closing ... */
887 nwritten = write(state->sock, &c, sizeof(c));
888 if (nwritten == -1) {
889 DEBUG(2, ("final write to client failed: %s\n",
898 /* Free any getent state */
900 free_getent_state(state->getpwent_state);
901 free_getent_state(state->getgrent_state);
903 /* We may have some extra data that was not freed if the client was
904 killed unexpectedly */
906 SAFE_FREE(state->response.extra_data.data);
908 TALLOC_FREE(state->mem_ctx);
910 remove_fd_event(&state->fd_event);
912 /* Remove from list and free */
914 winbindd_remove_client(state);
918 /* Shutdown client connection which has been idle for the longest time */
920 static bool remove_idle_client(void)
922 struct winbindd_cli_state *state, *remove_state = NULL;
923 time_t last_access = 0;
926 for (state = winbindd_client_list(); state; state = state->next) {
927 if (state->response.result != WINBINDD_PENDING &&
928 state->fd_event.flags == EVENT_FD_READ &&
929 !state->getpwent_state && !state->getgrent_state) {
931 if (!last_access || state->last_access < last_access) {
932 last_access = state->last_access;
933 remove_state = state;
939 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
940 nidle, remove_state->sock, (unsigned int)remove_state->pid));
941 remove_client(remove_state);
948 struct winbindd_listen_state {
951 struct tevent_fd *fde;
954 static void winbindd_listen_fde_handler(struct tevent_context *ev,
955 struct tevent_fd *fde,
959 struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
960 struct winbindd_listen_state);
962 while (winbindd_num_clients() >
963 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
964 DEBUG(5,("winbindd: Exceeding %d client "
965 "connections, removing idle "
967 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
968 if (!remove_idle_client()) {
969 DEBUG(0,("winbindd: Exceeding %d "
970 "client connections, no idle "
971 "connection found\n",
972 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
977 /* new, non-privileged connection */
978 new_connection(s->fd, s->privileged);
981 static bool winbindd_setup_listeners(void)
983 struct winbindd_listen_state *pub_state = NULL;
984 struct winbindd_listen_state *priv_state = NULL;
986 pub_state = talloc(winbind_event_context(),
987 struct winbindd_listen_state);
992 pub_state->privileged = false;
993 pub_state->fd = open_winbindd_socket();
994 if (pub_state->fd == -1) {
998 pub_state->fde = tevent_add_fd(winbind_event_context(),
999 pub_state, pub_state->fd,
1001 winbindd_listen_fde_handler,
1003 if (!pub_state->fde) {
1004 close(pub_state->fd);
1007 tevent_fd_set_auto_close(pub_state->fde);
1009 priv_state = talloc(winbind_event_context(),
1010 struct winbindd_listen_state);
1015 priv_state->privileged = true;
1016 priv_state->fd = open_winbindd_priv_socket();
1017 if (priv_state->fd == -1) {
1021 priv_state->fde = tevent_add_fd(winbind_event_context(),
1022 priv_state, priv_state->fd,
1024 winbindd_listen_fde_handler,
1026 if (!priv_state->fde) {
1027 close(priv_state->fd);
1030 tevent_fd_set_auto_close(priv_state->fde);
1034 TALLOC_FREE(pub_state);
1035 TALLOC_FREE(priv_state);
1039 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
1040 non-forking, non-threaded model which allows us to handle many
1041 simultaneous connections while remaining impervious to many denial of
1044 static void process_loop(void)
1046 struct winbindd_fd_event *ev;
1047 fd_set r_fds, w_fds;
1048 int maxfd = 0, selret;
1049 struct timeval timeout, ev_timeout;
1051 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1055 /* Initialise fd lists for select() */
1060 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
1061 timeout.tv_usec = 0;
1063 /* Check for any event timeouts. */
1068 event_add_to_select_args(winbind_event_context(), &now,
1069 &r_fds, &w_fds, &ev_timeout, &maxfd);
1071 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
1072 timeout = timeval_min(&timeout, &ev_timeout);
1075 for (ev = fd_events; ev; ev = ev->next) {
1076 if (ev->flags & EVENT_FD_READ) {
1077 FD_SET(ev->fd, &r_fds);
1078 maxfd = MAX(ev->fd, maxfd);
1080 if (ev->flags & EVENT_FD_WRITE) {
1081 FD_SET(ev->fd, &w_fds);
1082 maxfd = MAX(ev->fd, maxfd);
1088 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
1095 if (errno == EINTR) {
1099 /* Select error, something is badly wrong */
1107 if (run_events(winbind_event_context(), selret, &r_fds, &w_fds)) {
1112 while (ev != NULL) {
1113 struct winbindd_fd_event *next = ev->next;
1115 if (FD_ISSET(ev->fd, &r_fds))
1116 flags |= EVENT_FD_READ;
1117 if (FD_ISSET(ev->fd, &w_fds))
1118 flags |= EVENT_FD_WRITE;
1120 ev->handler(ev, flags);
1130 run_events(winbind_event_context(), selret, &r_fds, &w_fds);
1133 winbindd_check_cache_size(time(NULL));
1137 bool winbindd_use_idmap_cache(void)
1139 return !opt_nocache;
1142 bool winbindd_use_cache(void)
1144 return !opt_nocache;
1147 struct winbindd_addrchanged_state {
1148 struct addrchange_context *ctx;
1149 struct tevent_context *ev;
1150 struct messaging_context *msg_ctx;
1153 static void winbindd_addr_changed(struct tevent_req *req);
1155 static void winbindd_init_addrchange(TALLOC_CTX *mem_ctx,
1156 struct tevent_context *ev,
1157 struct messaging_context *msg_ctx)
1159 struct winbindd_addrchanged_state *state;
1160 struct tevent_req *req;
1163 state = talloc(mem_ctx, struct winbindd_addrchanged_state);
1164 if (state == NULL) {
1165 DEBUG(10, ("talloc failed\n"));
1169 state->msg_ctx = msg_ctx;
1171 status = addrchange_context_create(state, &state->ctx);
1172 if (!NT_STATUS_IS_OK(status)) {
1173 DEBUG(10, ("addrchange_context_create failed: %s\n",
1174 nt_errstr(status)));
1178 req = addrchange_send(state, ev, state->ctx);
1180 DEBUG(10, ("addrchange_send failed\n"));
1183 tevent_req_set_callback(req, winbindd_addr_changed, state);
1186 static void winbindd_addr_changed(struct tevent_req *req)
1188 struct winbindd_addrchanged_state *state = tevent_req_callback_data(
1189 req, struct winbindd_addrchanged_state);
1190 enum addrchange_type type;
1191 struct sockaddr_storage addr;
1194 status = addrchange_recv(req, &type, &addr);
1196 if (!NT_STATUS_IS_OK(status)) {
1197 DEBUG(10, ("addrchange_recv failed: %s, stop listening\n",
1198 nt_errstr(status)));
1201 if (type == ADDRCHANGE_DEL) {
1202 char addrstr[INET6_ADDRSTRLEN];
1205 print_sockaddr(addrstr, sizeof(addrstr), &addr);
1207 DEBUG(3, ("winbindd: kernel (AF_NETLINK) dropped ip %s\n",
1210 blob = data_blob_const(addrstr, strlen(addrstr)+1);
1212 status = messaging_send(state->msg_ctx, procid_self(),
1213 MSG_WINBIND_IP_DROPPED, &blob);
1214 if (!NT_STATUS_IS_OK(status)) {
1215 DEBUG(10, ("messaging_send failed: %s\n",
1216 nt_errstr(status)));
1219 req = addrchange_send(state, state->ev, state->ctx);
1221 DEBUG(10, ("addrchange_send failed\n"));
1224 tevent_req_set_callback(req, winbindd_addr_changed, state);
1229 int main(int argc, char **argv, char **envp)
1231 static bool is_daemon = False;
1232 static bool Fork = True;
1233 static bool log_stdout = False;
1234 static bool no_process_group = False;
1238 OPT_NO_PROCESS_GROUP,
1241 struct poptOption long_options[] = {
1243 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1244 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1245 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1246 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1247 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1248 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1254 TALLOC_CTX *frame = talloc_stackframe();
1256 /* glibc (?) likes to print "User defined signal 1" and exit if a
1257 SIGUSR[12] is received before a handler is installed */
1259 CatchSignal(SIGUSR1, SIG_IGN);
1260 CatchSignal(SIGUSR2, SIG_IGN);
1262 fault_setup((void (*)(void *))fault_quit );
1263 dump_core_setup("winbindd");
1267 /* Initialise for running in non-root mode */
1271 set_remote_machine_name("winbindd", False);
1273 /* Set environment variable so we don't recursively call ourselves.
1274 This may also be useful interactively. */
1276 if ( !winbind_off() ) {
1277 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1281 /* Initialise samba/rpc client stuff */
1283 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1285 while ((opt = poptGetNextOpt(pc)) != -1) {
1287 /* Don't become a daemon */
1299 case OPT_NO_PROCESS_GROUP:
1300 no_process_group = true;
1302 case OPT_LOG_STDOUT:
1309 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1310 poptBadOption(pc, 0), poptStrerror(opt));
1311 poptPrintUsage(pc, stderr, 0);
1316 if (is_daemon && interactive) {
1317 d_fprintf(stderr,"\nERROR: "
1318 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1319 poptPrintUsage(pc, stderr, 0);
1323 if (log_stdout && Fork) {
1324 d_fprintf(stderr, "\nERROR: "
1325 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1326 poptPrintUsage(pc, stderr, 0);
1330 poptFreeContext(pc);
1332 if (!override_logfile) {
1334 if (asprintf(&lfile,"%s/log.winbindd",
1335 get_dyn_LOGFILEBASE()) > 0) {
1336 lp_set_logfile(lfile);
1340 setup_logging("winbindd", log_stdout);
1343 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1344 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1346 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1347 DEBUG(0, ("error opening config file\n"));
1351 /* Initialise messaging system */
1353 if (winbind_messaging_context() == NULL) {
1357 if (!reload_services_file(NULL)) {
1358 DEBUG(0, ("error opening config file\n"));
1362 if (!directory_exist(lp_lockdir())) {
1363 mkdir(lp_lockdir(), 0755);
1373 if (!secrets_init()) {
1375 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1379 /* Enable netbios namecache */
1383 /* Unblock all signals we are interested in as they may have been
1384 blocked by the parent process. */
1386 BlockSignals(False, SIGINT);
1387 BlockSignals(False, SIGQUIT);
1388 BlockSignals(False, SIGTERM);
1389 BlockSignals(False, SIGUSR1);
1390 BlockSignals(False, SIGUSR2);
1391 BlockSignals(False, SIGHUP);
1392 BlockSignals(False, SIGCHLD);
1393 BlockSignals(False, SIGXCPU);
1396 become_daemon(Fork, no_process_group);
1398 pidfile_create("winbindd");
1402 * If we're interactive we want to set our own process group for
1403 * signal management.
1405 if (interactive && !no_process_group)
1406 setpgid( (pid_t)0, (pid_t)0);
1411 /* Don't use winbindd_reinit_after_fork here as
1412 * we're just starting up and haven't created any
1413 * winbindd-specific resources we must free yet. JRA.
1416 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1417 winbind_event_context(),
1419 DEBUG(0,("reinit_after_fork() failed\n"));
1423 /* Setup signal handlers */
1425 if (!winbindd_setup_sig_term_handler(true))
1427 if (!winbindd_setup_sig_hup_handler(NULL))
1429 if (!winbindd_setup_sig_chld_handler())
1431 if (!winbindd_setup_sig_usr2_handler())
1434 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1437 * Ensure all cache and idmap caches are consistent
1438 * and initialized before we startup.
1440 if (!winbindd_cache_validate_and_initialize()) {
1444 /* get broadcast messages */
1446 if (!serverid_register(procid_self(),
1447 FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
1448 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1452 /* React on 'smbcontrol winbindd reload-config' in the same way
1453 as to SIGHUP signal */
1454 messaging_register(winbind_messaging_context(), NULL,
1455 MSG_SMB_CONF_UPDATED, msg_reload_services);
1456 messaging_register(winbind_messaging_context(), NULL,
1457 MSG_SHUTDOWN, msg_shutdown);
1459 /* Handle online/offline messages. */
1460 messaging_register(winbind_messaging_context(), NULL,
1461 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1462 messaging_register(winbind_messaging_context(), NULL,
1463 MSG_WINBIND_ONLINE, winbind_msg_online);
1464 messaging_register(winbind_messaging_context(), NULL,
1465 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1467 messaging_register(winbind_messaging_context(), NULL,
1468 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1470 messaging_register(winbind_messaging_context(), NULL,
1471 MSG_WINBIND_VALIDATE_CACHE,
1472 winbind_msg_validate_cache);
1474 messaging_register(winbind_messaging_context(), NULL,
1475 MSG_WINBIND_DUMP_DOMAIN_LIST,
1476 winbind_msg_dump_domain_list);
1478 messaging_register(winbind_messaging_context(), NULL,
1479 MSG_WINBIND_IP_DROPPED,
1480 winbind_msg_ip_dropped_parent);
1482 /* Register handler for MSG_DEBUG. */
1483 messaging_register(winbind_messaging_context(), NULL,
1487 netsamlogon_cache_init(); /* Non-critical */
1489 /* clear the cached list of trusted domains */
1493 if (!init_domain_list()) {
1494 DEBUG(0,("unable to initialize domain list\n"));
1499 init_locator_child();
1501 smb_nscd_flush_user_cache();
1502 smb_nscd_flush_group_cache();
1504 winbindd_init_addrchange(NULL, winbind_event_context(),
1505 winbind_messaging_context());
1507 /* setup listen sockets */
1509 if (!winbindd_setup_listeners()) {
1510 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1515 /* Loop waiting for requests */
1517 struct winbindd_cli_state *state;
1519 frame = talloc_stackframe();
1521 /* refresh the trusted domain cache */
1523 rescan_trusted_domains();
1525 /* Dispose of client connection if it is marked as
1527 state = winbindd_client_list();
1529 struct winbindd_cli_state *next = state->next;
1531 if (state->finished) {
1532 remove_client(state);