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/>.
29 #define DBGC_CLASS DBGC_WINBIND
31 static bool opt_nocache = False;
32 static bool interactive = False;
34 extern bool override_logfile;
36 struct event_context *winbind_event_context(void)
38 static struct event_context *ctx;
40 if (!ctx && !(ctx = event_context_init(NULL))) {
41 smb_panic("Could not init winbind event context");
46 struct messaging_context *winbind_messaging_context(void)
48 static struct messaging_context *ctx;
51 ctx = messaging_init(NULL, server_id_self(),
52 winbind_event_context());
55 DEBUG(0, ("Could not init winbind messaging context.\n"));
60 /* Reload configuration */
62 static bool reload_services_file(const char *lfile)
67 const char *fname = lp_configfile();
69 if (file_exist(fname) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
70 set_dyn_CONFIGFILE(fname);
74 /* if this is a child, restore the logfile to the special
75 name - <domain>, idmap, etc. */
76 if (lfile && *lfile) {
77 lp_set_logfile(lfile);
81 ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
90 /**************************************************************************** **
92 **************************************************************************** */
94 static void fault_quit(void)
99 static void winbindd_status(void)
101 struct winbindd_cli_state *tmp;
103 DEBUG(0, ("winbindd status:\n"));
105 /* Print client state information */
107 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
109 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
110 DEBUG(2, ("\tclient list:\n"));
111 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
112 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
113 (unsigned long)tmp->pid, tmp->sock));
118 /* Print winbindd status to log file */
120 static void print_winbindd_status(void)
125 /* Flush client cache */
127 static void flush_caches(void)
129 /* We need to invalidate cached user list entries on a SIGHUP
130 otherwise cached access denied errors due to restrict anonymous
131 hang around until the sequence number changes. */
133 if (!wcache_invalidate_cache()) {
134 DEBUG(0, ("invalidating the cache failed; revalidate the cache\n"));
135 if (!winbindd_cache_validate_and_initialize()) {
141 /* Handle the signal by unlinking socket and exiting */
143 static void terminate(bool is_parent)
146 /* When parent goes away we should
147 * remove the socket file. Not so
148 * when children terminate.
152 if (asprintf(&path, "%s/%s",
153 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
161 trustdom_cache_shutdown();
163 gencache_stabilize();
167 TALLOC_CTX *mem_ctx = talloc_init("end_description");
168 char *description = talloc_describe_all(mem_ctx);
170 DEBUG(3, ("tallocs left:\n%s\n", description));
171 talloc_destroy(mem_ctx);
182 static void winbindd_sig_term_handler(struct tevent_context *ev,
183 struct tevent_signal *se,
189 bool *is_parent = talloc_get_type_abort(private_data, bool);
191 DEBUG(0,("Got sig[%d] terminate (is_parent=%d)\n",
192 signum, (int)*is_parent));
193 terminate(*is_parent);
196 bool winbindd_setup_sig_term_handler(bool parent)
198 struct tevent_signal *se;
201 is_parent = talloc(winbind_event_context(), bool);
208 se = tevent_add_signal(winbind_event_context(),
211 winbindd_sig_term_handler,
214 DEBUG(0,("failed to setup SIGTERM handler"));
215 talloc_free(is_parent);
219 se = tevent_add_signal(winbind_event_context(),
222 winbindd_sig_term_handler,
225 DEBUG(0,("failed to setup SIGINT handler"));
226 talloc_free(is_parent);
230 se = tevent_add_signal(winbind_event_context(),
233 winbindd_sig_term_handler,
236 DEBUG(0,("failed to setup SIGINT handler"));
237 talloc_free(is_parent);
244 static void winbindd_sig_hup_handler(struct tevent_context *ev,
245 struct tevent_signal *se,
251 const char *file = (const char *)private_data;
253 DEBUG(1,("Reloading services after SIGHUP\n"));
255 reload_services_file(file);
258 bool winbindd_setup_sig_hup_handler(const char *lfile)
260 struct tevent_signal *se;
264 file = talloc_strdup(winbind_event_context(),
271 se = tevent_add_signal(winbind_event_context(),
272 winbind_event_context(),
274 winbindd_sig_hup_handler,
283 static void winbindd_sig_chld_handler(struct tevent_context *ev,
284 struct tevent_signal *se,
292 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
293 winbind_child_died(pid);
297 static bool winbindd_setup_sig_chld_handler(void)
299 struct tevent_signal *se;
301 se = tevent_add_signal(winbind_event_context(),
302 winbind_event_context(),
304 winbindd_sig_chld_handler,
313 static void winbindd_sig_usr2_handler(struct tevent_context *ev,
314 struct tevent_signal *se,
320 print_winbindd_status();
323 static bool winbindd_setup_sig_usr2_handler(void)
325 struct tevent_signal *se;
327 se = tevent_add_signal(winbind_event_context(),
328 winbind_event_context(),
330 winbindd_sig_usr2_handler,
339 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
340 static void msg_reload_services(struct messaging_context *msg,
343 struct server_id server_id,
346 /* Flush various caches */
348 reload_services_file((const char *) private_data);
351 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
352 static void msg_shutdown(struct messaging_context *msg,
355 struct server_id server_id,
358 /* only the parent waits for this message */
359 DEBUG(0,("Got shutdown message\n"));
364 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
367 struct server_id server_id,
373 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
377 * call the validation code from a child:
378 * so we don't block the main winbindd and the validation
379 * code can safely use fork/waitpid...
381 child_pid = sys_fork();
383 if (child_pid == -1) {
384 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
389 if (child_pid != 0) {
391 DEBUG(5, ("winbind_msg_validate_cache: child created with "
392 "pid %d.\n", (int)child_pid));
398 if (!winbindd_reinit_after_fork(NULL)) {
402 /* install default SIGCHLD handler: validation code uses fork/waitpid */
403 CatchSignal(SIGCHLD, SIG_DFL);
405 ret = (uint8)winbindd_validate_cache_nobackup();
406 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
407 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
412 static struct winbindd_dispatch_table {
413 enum winbindd_cmd cmd;
414 void (*fn)(struct winbindd_cli_state *state);
415 const char *winbindd_cmd_name;
416 } dispatch_table[] = {
420 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
421 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
422 { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
424 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
425 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
426 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
428 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
429 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
430 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
431 "GETUSERDOMGROUPS" },
432 { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
433 "LOOKUPUSERALIASES" },
435 /* Group functions */
437 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
438 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
439 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
440 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
441 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
442 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
444 /* PAM auth functions */
446 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
447 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
448 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
449 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
450 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
452 /* Enumeration functions */
454 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
455 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
456 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
458 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
460 /* SID related functions */
462 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
463 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
464 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
466 /* Lookup related functions */
468 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
469 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
470 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
471 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
472 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
473 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
477 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
478 { WINBINDD_PING_DC, winbindd_ping_dc, "PING_DC" },
479 { WINBINDD_PING, winbindd_ping, "PING" },
480 { WINBINDD_INFO, winbindd_info, "INFO" },
481 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
482 "INTERFACE_VERSION" },
483 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
484 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
485 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
486 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
487 "WINBINDD_PRIV_PIPE_DIR" },
488 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
489 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
491 /* Credential cache access */
492 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
496 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
497 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
501 { WINBINDD_NUM_CMDS, NULL, "NONE" }
504 static void process_request(struct winbindd_cli_state *state)
506 struct winbindd_dispatch_table *table = dispatch_table;
508 /* Free response data - we may be interrupted and receive another
509 command before being able to send this data off. */
511 SAFE_FREE(state->response.extra_data.data);
513 ZERO_STRUCT(state->response);
515 state->response.result = WINBINDD_PENDING;
516 state->response.length = sizeof(struct winbindd_response);
518 state->mem_ctx = talloc_init("winbind request");
519 if (state->mem_ctx == NULL)
522 /* Remember who asked us. */
523 state->pid = state->request.pid;
525 /* Process command */
527 for (table = dispatch_table; table->fn; table++) {
528 if (state->request.cmd == table->cmd) {
529 DEBUG(10,("process_request: request fn %s\n",
530 table->winbindd_cmd_name ));
537 DEBUG(10,("process_request: unknown request fn number %d\n",
538 (int)state->request.cmd ));
539 request_error(state);
544 * A list of file descriptors being monitored by select in the main processing
545 * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
548 static struct winbindd_fd_event *fd_events = NULL;
550 void add_fd_event(struct winbindd_fd_event *ev)
552 struct winbindd_fd_event *match;
554 /* only add unique winbindd_fd_event structs */
556 for (match=fd_events; match; match=match->next ) {
558 SMB_ASSERT( match != ev );
565 DLIST_ADD(fd_events, ev);
568 void remove_fd_event(struct winbindd_fd_event *ev)
570 DLIST_REMOVE(fd_events, ev);
574 * Handler for winbindd_fd_events to complete a read/write request, set up by
575 * setup_async_read/setup_async_write.
578 static void rw_callback(struct winbindd_fd_event *event, int flags)
583 todo = event->length - event->done;
585 if (event->flags & EVENT_FD_WRITE) {
586 SMB_ASSERT(flags == EVENT_FD_WRITE);
587 done = sys_write(event->fd,
588 &((char *)event->data)[event->done],
593 event->finished(event->private_data, False);
598 if (event->flags & EVENT_FD_READ) {
599 SMB_ASSERT(flags == EVENT_FD_READ);
600 done = sys_read(event->fd, &((char *)event->data)[event->done],
605 event->finished(event->private_data, False);
612 if (event->done == event->length) {
614 event->finished(event->private_data, True);
619 * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
620 * when the request is completed or an error had occurred.
623 void setup_async_read(struct winbindd_fd_event *event, void *data, size_t length,
624 void (*finished)(void *private_data, bool success),
627 SMB_ASSERT(event->flags == 0);
629 event->length = length;
631 event->handler = rw_callback;
632 event->finished = finished;
633 event->private_data = private_data;
634 event->flags = EVENT_FD_READ;
637 void setup_async_write(struct winbindd_fd_event *event, void *data, size_t length,
638 void (*finished)(void *private_data, bool success),
641 SMB_ASSERT(event->flags == 0);
643 event->length = length;
645 event->handler = rw_callback;
646 event->finished = finished;
647 event->private_data = private_data;
648 event->flags = EVENT_FD_WRITE;
652 * This is the main event loop of winbind requests. It goes through a
653 * state-machine of 3 read/write requests, 4 if you have extra data to send.
655 * An idle winbind client has a read request of 4 bytes outstanding,
656 * finalizing function is request_len_recv, checking the length. request_recv
657 * then processes the packet. The processing function then at some point has
658 * to call request_finished which schedules sending the response.
661 static void request_len_recv(void *private_data, bool success);
662 static void request_recv(void *private_data, bool success);
663 static void request_main_recv(void *private_data, bool success);
664 static void request_finished(struct winbindd_cli_state *state);
665 static void response_main_sent(void *private_data, bool success);
666 static void response_extra_sent(void *private_data, bool success);
668 static void response_extra_sent(void *private_data, bool success)
670 struct winbindd_cli_state *state =
671 talloc_get_type_abort(private_data, struct winbindd_cli_state);
673 TALLOC_FREE(state->mem_ctx);
676 state->finished = True;
680 SAFE_FREE(state->response.extra_data.data);
682 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
683 request_len_recv, state);
686 static void response_main_sent(void *private_data, bool success)
688 struct winbindd_cli_state *state =
689 talloc_get_type_abort(private_data, struct winbindd_cli_state);
692 state->finished = True;
696 if (state->response.length == sizeof(state->response)) {
697 TALLOC_FREE(state->mem_ctx);
699 setup_async_read(&state->fd_event, &state->request,
700 sizeof(uint32), request_len_recv, state);
704 setup_async_write(&state->fd_event, state->response.extra_data.data,
705 state->response.length - sizeof(state->response),
706 response_extra_sent, state);
709 static void request_finished(struct winbindd_cli_state *state)
711 /* Make sure request.extra_data is freed when finish processing a request */
712 SAFE_FREE(state->request.extra_data.data);
713 setup_async_write(&state->fd_event, &state->response,
714 sizeof(state->response), response_main_sent, state);
717 void request_error(struct winbindd_cli_state *state)
719 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
720 state->response.result = WINBINDD_ERROR;
721 request_finished(state);
724 void request_ok(struct winbindd_cli_state *state)
726 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
727 state->response.result = WINBINDD_OK;
728 request_finished(state);
731 static void request_len_recv(void *private_data, bool success)
733 struct winbindd_cli_state *state =
734 talloc_get_type_abort(private_data, struct winbindd_cli_state);
737 state->finished = True;
741 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
742 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
743 *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
744 state->finished = True;
748 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
749 sizeof(state->request) - sizeof(uint32),
750 request_main_recv, state);
753 static void request_main_recv(void *private_data, bool success)
755 struct winbindd_cli_state *state =
756 talloc_get_type_abort(private_data, struct winbindd_cli_state);
759 state->finished = True;
763 if (state->request.extra_len == 0) {
764 state->request.extra_data.data = NULL;
765 request_recv(state, True);
769 if ((!state->privileged) &&
770 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
771 DEBUG(3, ("Got request with %d bytes extra data on "
772 "unprivileged socket\n", (int)state->request.extra_len));
773 state->request.extra_data.data = NULL;
774 state->finished = True;
778 state->request.extra_data.data =
779 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
781 if (state->request.extra_data.data == NULL) {
782 DEBUG(0, ("malloc failed\n"));
783 state->finished = True;
787 /* Ensure null termination */
788 state->request.extra_data.data[state->request.extra_len] = '\0';
790 setup_async_read(&state->fd_event, state->request.extra_data.data,
791 state->request.extra_len, request_recv, state);
794 static void request_recv(void *private_data, bool success)
796 struct winbindd_cli_state *state =
797 talloc_get_type_abort(private_data, struct winbindd_cli_state);
800 state->finished = True;
804 process_request(state);
807 /* Process a new connection by adding it to the client connection list */
809 static void new_connection(int listen_sock, bool privileged)
811 struct sockaddr_un sunaddr;
812 struct winbindd_cli_state *state;
816 /* Accept connection */
818 len = sizeof(sunaddr);
821 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
822 } while (sock == -1 && errno == EINTR);
827 DEBUG(6,("accepted socket %d\n", sock));
829 /* Create new connection structure */
831 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
838 state->last_access = time(NULL);
840 state->privileged = privileged;
842 state->fd_event.fd = state->sock;
843 state->fd_event.flags = 0;
844 add_fd_event(&state->fd_event);
846 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
847 request_len_recv, state);
849 /* Add to connection list */
851 winbindd_add_client(state);
854 /* Remove a client connection from client connection list */
856 static void remove_client(struct winbindd_cli_state *state)
861 /* It's a dead client - hold a funeral */
867 if (!state->finished) {
868 /* tell client, we are closing ... */
869 nwritten = write(state->sock, &c, sizeof(c));
870 if (nwritten == -1) {
871 DEBUG(2, ("final write to client failed: %s\n",
880 /* Free any getent state */
882 free_getent_state(state->getpwent_state);
883 free_getent_state(state->getgrent_state);
885 /* We may have some extra data that was not freed if the client was
886 killed unexpectedly */
888 SAFE_FREE(state->response.extra_data.data);
890 TALLOC_FREE(state->mem_ctx);
892 remove_fd_event(&state->fd_event);
894 /* Remove from list and free */
896 winbindd_remove_client(state);
900 /* Shutdown client connection which has been idle for the longest time */
902 static bool remove_idle_client(void)
904 struct winbindd_cli_state *state, *remove_state = NULL;
905 time_t last_access = 0;
908 for (state = winbindd_client_list(); state; state = state->next) {
909 if (state->response.result != WINBINDD_PENDING &&
910 state->fd_event.flags == EVENT_FD_READ &&
911 !state->getpwent_state && !state->getgrent_state) {
913 if (!last_access || state->last_access < last_access) {
914 last_access = state->last_access;
915 remove_state = state;
921 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
922 nidle, remove_state->sock, (unsigned int)remove_state->pid));
923 remove_client(remove_state);
930 struct winbindd_listen_state {
933 struct tevent_fd *fde;
936 static void winbindd_listen_fde_handler(struct tevent_context *ev,
937 struct tevent_fd *fde,
941 struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
942 struct winbindd_listen_state);
944 while (winbindd_num_clients() >
945 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
946 DEBUG(5,("winbindd: Exceeding %d client "
947 "connections, removing idle "
949 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
950 if (!remove_idle_client()) {
951 DEBUG(0,("winbindd: Exceeding %d "
952 "client connections, no idle "
953 "connection found\n",
954 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
959 /* new, non-privileged connection */
960 new_connection(s->fd, s->privileged);
963 static bool winbindd_setup_listeners(void)
965 struct winbindd_listen_state *pub_state = NULL;
966 struct winbindd_listen_state *priv_state = NULL;
968 pub_state = talloc(winbind_event_context(),
969 struct winbindd_listen_state);
974 pub_state->privileged = false;
975 pub_state->fd = open_winbindd_socket();
976 if (pub_state->fd == -1) {
980 pub_state->fde = tevent_add_fd(winbind_event_context(),
981 pub_state, pub_state->fd,
983 winbindd_listen_fde_handler,
985 if (!pub_state->fde) {
986 close(pub_state->fd);
989 tevent_fd_set_auto_close(pub_state->fde);
991 priv_state = talloc(winbind_event_context(),
992 struct winbindd_listen_state);
997 priv_state->privileged = true;
998 priv_state->fd = open_winbindd_priv_socket();
999 if (priv_state->fd == -1) {
1003 priv_state->fde = tevent_add_fd(winbind_event_context(),
1004 priv_state, priv_state->fd,
1006 winbindd_listen_fde_handler,
1008 if (!priv_state->fde) {
1009 close(priv_state->fd);
1012 tevent_fd_set_auto_close(priv_state->fde);
1016 TALLOC_FREE(pub_state);
1017 TALLOC_FREE(priv_state);
1021 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
1022 non-forking, non-threaded model which allows us to handle many
1023 simultaneous connections while remaining impervious to many denial of
1026 static void process_loop(void)
1028 struct winbindd_fd_event *ev;
1029 fd_set r_fds, w_fds;
1030 int maxfd = 0, selret;
1031 struct timeval timeout, ev_timeout;
1033 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1037 /* Initialise fd lists for select() */
1042 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
1043 timeout.tv_usec = 0;
1045 /* Check for any event timeouts. */
1050 event_add_to_select_args(winbind_event_context(), &now,
1051 &r_fds, &w_fds, &ev_timeout, &maxfd);
1053 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
1054 timeout = timeval_min(&timeout, &ev_timeout);
1057 for (ev = fd_events; ev; ev = ev->next) {
1058 if (ev->flags & EVENT_FD_READ) {
1059 FD_SET(ev->fd, &r_fds);
1060 maxfd = MAX(ev->fd, maxfd);
1062 if (ev->flags & EVENT_FD_WRITE) {
1063 FD_SET(ev->fd, &w_fds);
1064 maxfd = MAX(ev->fd, maxfd);
1070 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
1077 if (errno == EINTR) {
1081 /* Select error, something is badly wrong */
1089 if (run_events(winbind_event_context(), selret, &r_fds, &w_fds)) {
1094 while (ev != NULL) {
1095 struct winbindd_fd_event *next = ev->next;
1097 if (FD_ISSET(ev->fd, &r_fds))
1098 flags |= EVENT_FD_READ;
1099 if (FD_ISSET(ev->fd, &w_fds))
1100 flags |= EVENT_FD_WRITE;
1102 ev->handler(ev, flags);
1112 run_events(winbind_event_context(), selret, &r_fds, &w_fds);
1115 winbindd_check_cache_size(time(NULL));
1119 bool winbindd_use_idmap_cache(void)
1121 return !opt_nocache;
1124 bool winbindd_use_cache(void)
1126 return !opt_nocache;
1131 int main(int argc, char **argv, char **envp)
1133 static bool is_daemon = False;
1134 static bool Fork = True;
1135 static bool log_stdout = False;
1136 static bool no_process_group = False;
1140 OPT_NO_PROCESS_GROUP,
1143 struct poptOption long_options[] = {
1145 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1146 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1147 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1148 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1149 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1150 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1156 TALLOC_CTX *frame = talloc_stackframe();
1158 /* glibc (?) likes to print "User defined signal 1" and exit if a
1159 SIGUSR[12] is received before a handler is installed */
1161 CatchSignal(SIGUSR1, SIG_IGN);
1162 CatchSignal(SIGUSR2, SIG_IGN);
1164 fault_setup((void (*)(void *))fault_quit );
1165 dump_core_setup("winbindd");
1169 /* Initialise for running in non-root mode */
1173 set_remote_machine_name("winbindd", False);
1175 /* Set environment variable so we don't recursively call ourselves.
1176 This may also be useful interactively. */
1178 if ( !winbind_off() ) {
1179 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1183 /* Initialise samba/rpc client stuff */
1185 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1187 while ((opt = poptGetNextOpt(pc)) != -1) {
1189 /* Don't become a daemon */
1201 case OPT_NO_PROCESS_GROUP:
1202 no_process_group = true;
1204 case OPT_LOG_STDOUT:
1211 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1212 poptBadOption(pc, 0), poptStrerror(opt));
1213 poptPrintUsage(pc, stderr, 0);
1218 if (is_daemon && interactive) {
1219 d_fprintf(stderr,"\nERROR: "
1220 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1221 poptPrintUsage(pc, stderr, 0);
1225 if (log_stdout && Fork) {
1226 d_fprintf(stderr, "\nERROR: "
1227 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1228 poptPrintUsage(pc, stderr, 0);
1232 poptFreeContext(pc);
1234 if (!override_logfile) {
1236 if (asprintf(&lfile,"%s/log.winbindd",
1237 get_dyn_LOGFILEBASE()) > 0) {
1238 lp_set_logfile(lfile);
1242 setup_logging("winbindd", log_stdout);
1245 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1246 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1248 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1249 DEBUG(0, ("error opening config file\n"));
1253 /* Initialise messaging system */
1255 if (winbind_messaging_context() == NULL) {
1259 if (!reload_services_file(NULL)) {
1260 DEBUG(0, ("error opening config file\n"));
1264 if (!directory_exist(lp_lockdir())) {
1265 mkdir(lp_lockdir(), 0755);
1275 if (!secrets_init()) {
1277 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1281 /* Enable netbios namecache */
1285 /* Unblock all signals we are interested in as they may have been
1286 blocked by the parent process. */
1288 BlockSignals(False, SIGINT);
1289 BlockSignals(False, SIGQUIT);
1290 BlockSignals(False, SIGTERM);
1291 BlockSignals(False, SIGUSR1);
1292 BlockSignals(False, SIGUSR2);
1293 BlockSignals(False, SIGHUP);
1294 BlockSignals(False, SIGCHLD);
1297 become_daemon(Fork, no_process_group);
1299 pidfile_create("winbindd");
1303 * If we're interactive we want to set our own process group for
1304 * signal management.
1306 if (interactive && !no_process_group)
1307 setpgid( (pid_t)0, (pid_t)0);
1312 /* Don't use winbindd_reinit_after_fork here as
1313 * we're just starting up and haven't created any
1314 * winbindd-specific resources we must free yet. JRA.
1317 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1318 winbind_event_context(),
1320 DEBUG(0,("reinit_after_fork() failed\n"));
1324 /* Setup signal handlers */
1326 if (!winbindd_setup_sig_term_handler(true))
1328 if (!winbindd_setup_sig_hup_handler(NULL))
1330 if (!winbindd_setup_sig_chld_handler())
1332 if (!winbindd_setup_sig_usr2_handler())
1335 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1338 * Ensure all cache and idmap caches are consistent
1339 * and initialized before we startup.
1341 if (!winbindd_cache_validate_and_initialize()) {
1345 /* get broadcast messages */
1347 if (!serverid_register(procid_self(),
1348 FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
1349 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1353 /* React on 'smbcontrol winbindd reload-config' in the same way
1354 as to SIGHUP signal */
1355 messaging_register(winbind_messaging_context(), NULL,
1356 MSG_SMB_CONF_UPDATED, msg_reload_services);
1357 messaging_register(winbind_messaging_context(), NULL,
1358 MSG_SHUTDOWN, msg_shutdown);
1360 /* Handle online/offline messages. */
1361 messaging_register(winbind_messaging_context(), NULL,
1362 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1363 messaging_register(winbind_messaging_context(), NULL,
1364 MSG_WINBIND_ONLINE, winbind_msg_online);
1365 messaging_register(winbind_messaging_context(), NULL,
1366 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1368 messaging_register(winbind_messaging_context(), NULL,
1369 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1371 messaging_register(winbind_messaging_context(), NULL,
1372 MSG_WINBIND_VALIDATE_CACHE,
1373 winbind_msg_validate_cache);
1375 messaging_register(winbind_messaging_context(), NULL,
1376 MSG_WINBIND_DUMP_DOMAIN_LIST,
1377 winbind_msg_dump_domain_list);
1379 /* Register handler for MSG_DEBUG. */
1380 messaging_register(winbind_messaging_context(), NULL,
1384 netsamlogon_cache_init(); /* Non-critical */
1386 /* clear the cached list of trusted domains */
1390 if (!init_domain_list()) {
1391 DEBUG(0,("unable to initialize domain list\n"));
1396 init_locator_child();
1398 smb_nscd_flush_user_cache();
1399 smb_nscd_flush_group_cache();
1401 /* setup listen sockets */
1403 if (!winbindd_setup_listeners()) {
1404 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1409 /* Loop waiting for requests */
1411 struct winbindd_cli_state *state;
1413 frame = talloc_stackframe();
1415 /* refresh the trusted domain cache */
1417 rescan_trusted_domains();
1419 /* Dispose of client connection if it is marked as
1421 state = winbindd_client_list();
1423 struct winbindd_cli_state *next = state->next;
1425 if (state->finished) {
1426 remove_client(state);