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));
194 /* if the parent killed us due to a timeout log where we were */
195 if (signum == SIGXCPU) {
196 log_stack_trace_with_level(1);
199 terminate(*is_parent);
202 bool winbindd_setup_sig_term_handler(bool parent)
204 struct tevent_signal *se;
207 is_parent = talloc(winbind_event_context(), bool);
214 se = tevent_add_signal(winbind_event_context(),
217 winbindd_sig_term_handler,
220 DEBUG(0,("failed to setup SIGTERM handler"));
221 talloc_free(is_parent);
225 se = tevent_add_signal(winbind_event_context(),
228 winbindd_sig_term_handler,
231 DEBUG(0,("failed to setup SIGINT handler"));
232 talloc_free(is_parent);
236 se = tevent_add_signal(winbind_event_context(),
239 winbindd_sig_term_handler,
242 DEBUG(0,("failed to setup SIGINT handler"));
243 talloc_free(is_parent);
247 se = tevent_add_signal(winbind_event_context(),
250 winbindd_sig_term_handler,
253 DEBUG(0,("failed to setup SIGXCPU handler"));
254 talloc_free(is_parent);
260 static void winbindd_sig_hup_handler(struct tevent_context *ev,
261 struct tevent_signal *se,
267 const char *file = (const char *)private_data;
269 DEBUG(1,("Reloading services after SIGHUP\n"));
271 reload_services_file(file);
274 bool winbindd_setup_sig_hup_handler(const char *lfile)
276 struct tevent_signal *se;
280 file = talloc_strdup(winbind_event_context(),
287 se = tevent_add_signal(winbind_event_context(),
288 winbind_event_context(),
290 winbindd_sig_hup_handler,
299 static void winbindd_sig_chld_handler(struct tevent_context *ev,
300 struct tevent_signal *se,
308 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
309 winbind_child_died(pid);
313 static bool winbindd_setup_sig_chld_handler(void)
315 struct tevent_signal *se;
317 se = tevent_add_signal(winbind_event_context(),
318 winbind_event_context(),
320 winbindd_sig_chld_handler,
329 static void winbindd_sig_usr2_handler(struct tevent_context *ev,
330 struct tevent_signal *se,
336 print_winbindd_status();
339 static bool winbindd_setup_sig_usr2_handler(void)
341 struct tevent_signal *se;
343 se = tevent_add_signal(winbind_event_context(),
344 winbind_event_context(),
346 winbindd_sig_usr2_handler,
355 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
356 static void msg_reload_services(struct messaging_context *msg,
359 struct server_id server_id,
362 /* Flush various caches */
364 reload_services_file((const char *) private_data);
367 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
368 static void msg_shutdown(struct messaging_context *msg,
371 struct server_id server_id,
374 /* only the parent waits for this message */
375 DEBUG(0,("Got shutdown message\n"));
380 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
383 struct server_id server_id,
389 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
393 * call the validation code from a child:
394 * so we don't block the main winbindd and the validation
395 * code can safely use fork/waitpid...
397 child_pid = sys_fork();
399 if (child_pid == -1) {
400 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
405 if (child_pid != 0) {
407 DEBUG(5, ("winbind_msg_validate_cache: child created with "
408 "pid %d.\n", (int)child_pid));
414 if (!winbindd_reinit_after_fork(NULL)) {
418 /* install default SIGCHLD handler: validation code uses fork/waitpid */
419 CatchSignal(SIGCHLD, SIG_DFL);
421 ret = (uint8)winbindd_validate_cache_nobackup();
422 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
423 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
428 static struct winbindd_dispatch_table {
429 enum winbindd_cmd cmd;
430 void (*fn)(struct winbindd_cli_state *state);
431 const char *winbindd_cmd_name;
432 } dispatch_table[] = {
436 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
437 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
438 { WINBINDD_GETPWSID, winbindd_getpwsid, "GETPWSID" },
440 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
441 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
442 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
444 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
445 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
446 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
447 "GETUSERDOMGROUPS" },
448 { WINBINDD_GETSIDALIASES, winbindd_getsidaliases,
449 "LOOKUPUSERALIASES" },
451 /* Group functions */
453 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
454 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
455 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
456 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
457 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
458 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
460 /* PAM auth functions */
462 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
463 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
464 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
465 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
466 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
468 /* Enumeration functions */
470 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
471 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
472 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
474 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
476 /* SID related functions */
478 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
479 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
480 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
482 /* Lookup related functions */
484 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
485 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
486 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
487 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
488 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
489 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
493 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
494 { WINBINDD_PING_DC, winbindd_ping_dc, "PING_DC" },
495 { WINBINDD_PING, winbindd_ping, "PING" },
496 { WINBINDD_INFO, winbindd_info, "INFO" },
497 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
498 "INTERFACE_VERSION" },
499 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
500 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
501 { WINBINDD_DC_INFO, winbindd_dc_info, "DC_INFO" },
502 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
503 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
504 "WINBINDD_PRIV_PIPE_DIR" },
505 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
506 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
508 /* Credential cache access */
509 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
513 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
514 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
518 { WINBINDD_NUM_CMDS, NULL, "NONE" }
521 static void process_request(struct winbindd_cli_state *state)
523 struct winbindd_dispatch_table *table = dispatch_table;
525 /* Free response data - we may be interrupted and receive another
526 command before being able to send this data off. */
528 SAFE_FREE(state->response.extra_data.data);
530 ZERO_STRUCT(state->response);
532 state->response.result = WINBINDD_PENDING;
533 state->response.length = sizeof(struct winbindd_response);
535 state->mem_ctx = talloc_init("winbind request");
536 if (state->mem_ctx == NULL)
539 /* Remember who asked us. */
540 state->pid = state->request.pid;
542 /* Process command */
544 for (table = dispatch_table; table->fn; table++) {
545 if (state->request.cmd == table->cmd) {
546 DEBUG(10,("process_request: request fn %s\n",
547 table->winbindd_cmd_name ));
554 DEBUG(10,("process_request: unknown request fn number %d\n",
555 (int)state->request.cmd ));
556 request_error(state);
561 * A list of file descriptors being monitored by select in the main processing
562 * loop. winbindd_fd_event->handler is called whenever the socket is readable/writable.
565 static struct winbindd_fd_event *fd_events = NULL;
567 void add_fd_event(struct winbindd_fd_event *ev)
569 struct winbindd_fd_event *match;
571 /* only add unique winbindd_fd_event structs */
573 for (match=fd_events; match; match=match->next ) {
575 SMB_ASSERT( match != ev );
582 DLIST_ADD(fd_events, ev);
585 void remove_fd_event(struct winbindd_fd_event *ev)
587 DLIST_REMOVE(fd_events, ev);
591 * Handler for winbindd_fd_events to complete a read/write request, set up by
592 * setup_async_read/setup_async_write.
595 static void rw_callback(struct winbindd_fd_event *event, int flags)
600 todo = event->length - event->done;
602 if (event->flags & EVENT_FD_WRITE) {
603 SMB_ASSERT(flags == EVENT_FD_WRITE);
604 done = sys_write(event->fd,
605 &((char *)event->data)[event->done],
610 event->finished(event->private_data, False);
615 if (event->flags & EVENT_FD_READ) {
616 SMB_ASSERT(flags == EVENT_FD_READ);
617 done = sys_read(event->fd, &((char *)event->data)[event->done],
622 event->finished(event->private_data, False);
629 if (event->done == event->length) {
631 event->finished(event->private_data, True);
636 * Request an async read/write on a winbindd_fd_event structure. (*finished) is called
637 * when the request is completed or an error had occurred.
640 void setup_async_read(struct winbindd_fd_event *event, void *data, size_t length,
641 void (*finished)(void *private_data, bool success),
644 SMB_ASSERT(event->flags == 0);
646 event->length = length;
648 event->handler = rw_callback;
649 event->finished = finished;
650 event->private_data = private_data;
651 event->flags = EVENT_FD_READ;
654 void setup_async_write(struct winbindd_fd_event *event, void *data, size_t length,
655 void (*finished)(void *private_data, bool success),
658 SMB_ASSERT(event->flags == 0);
660 event->length = length;
662 event->handler = rw_callback;
663 event->finished = finished;
664 event->private_data = private_data;
665 event->flags = EVENT_FD_WRITE;
669 * This is the main event loop of winbind requests. It goes through a
670 * state-machine of 3 read/write requests, 4 if you have extra data to send.
672 * An idle winbind client has a read request of 4 bytes outstanding,
673 * finalizing function is request_len_recv, checking the length. request_recv
674 * then processes the packet. The processing function then at some point has
675 * to call request_finished which schedules sending the response.
678 static void request_len_recv(void *private_data, bool success);
679 static void request_recv(void *private_data, bool success);
680 static void request_main_recv(void *private_data, bool success);
681 static void request_finished(struct winbindd_cli_state *state);
682 static void response_main_sent(void *private_data, bool success);
683 static void response_extra_sent(void *private_data, bool success);
685 static void response_extra_sent(void *private_data, bool success)
687 struct winbindd_cli_state *state =
688 talloc_get_type_abort(private_data, struct winbindd_cli_state);
690 TALLOC_FREE(state->mem_ctx);
693 state->finished = True;
697 SAFE_FREE(state->response.extra_data.data);
699 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
700 request_len_recv, state);
703 static void response_main_sent(void *private_data, bool success)
705 struct winbindd_cli_state *state =
706 talloc_get_type_abort(private_data, struct winbindd_cli_state);
709 state->finished = True;
713 if (state->response.length == sizeof(state->response)) {
714 TALLOC_FREE(state->mem_ctx);
716 setup_async_read(&state->fd_event, &state->request,
717 sizeof(uint32), request_len_recv, state);
721 setup_async_write(&state->fd_event, state->response.extra_data.data,
722 state->response.length - sizeof(state->response),
723 response_extra_sent, state);
726 static void request_finished(struct winbindd_cli_state *state)
728 /* Make sure request.extra_data is freed when finish processing a request */
729 SAFE_FREE(state->request.extra_data.data);
730 setup_async_write(&state->fd_event, &state->response,
731 sizeof(state->response), response_main_sent, state);
734 void request_error(struct winbindd_cli_state *state)
736 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
737 state->response.result = WINBINDD_ERROR;
738 request_finished(state);
741 void request_ok(struct winbindd_cli_state *state)
743 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
744 state->response.result = WINBINDD_OK;
745 request_finished(state);
748 static void request_len_recv(void *private_data, bool success)
750 struct winbindd_cli_state *state =
751 talloc_get_type_abort(private_data, struct winbindd_cli_state);
754 state->finished = True;
758 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
759 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
760 *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
761 state->finished = True;
765 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
766 sizeof(state->request) - sizeof(uint32),
767 request_main_recv, state);
770 static void request_main_recv(void *private_data, bool success)
772 struct winbindd_cli_state *state =
773 talloc_get_type_abort(private_data, struct winbindd_cli_state);
776 state->finished = True;
780 if (state->request.extra_len == 0) {
781 state->request.extra_data.data = NULL;
782 request_recv(state, True);
786 if ((!state->privileged) &&
787 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
788 DEBUG(3, ("Got request with %d bytes extra data on "
789 "unprivileged socket\n", (int)state->request.extra_len));
790 state->request.extra_data.data = NULL;
791 state->finished = True;
795 state->request.extra_data.data =
796 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
798 if (state->request.extra_data.data == NULL) {
799 DEBUG(0, ("malloc failed\n"));
800 state->finished = True;
804 /* Ensure null termination */
805 state->request.extra_data.data[state->request.extra_len] = '\0';
807 setup_async_read(&state->fd_event, state->request.extra_data.data,
808 state->request.extra_len, request_recv, state);
811 static void request_recv(void *private_data, bool success)
813 struct winbindd_cli_state *state =
814 talloc_get_type_abort(private_data, struct winbindd_cli_state);
817 state->finished = True;
821 process_request(state);
824 /* Process a new connection by adding it to the client connection list */
826 static void new_connection(int listen_sock, bool privileged)
828 struct sockaddr_un sunaddr;
829 struct winbindd_cli_state *state;
833 /* Accept connection */
835 len = sizeof(sunaddr);
838 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
839 } while (sock == -1 && errno == EINTR);
844 DEBUG(6,("accepted socket %d\n", sock));
846 /* Create new connection structure */
848 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
855 state->last_access = time(NULL);
857 state->privileged = privileged;
859 state->fd_event.fd = state->sock;
860 state->fd_event.flags = 0;
861 add_fd_event(&state->fd_event);
863 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
864 request_len_recv, state);
866 /* Add to connection list */
868 winbindd_add_client(state);
871 /* Remove a client connection from client connection list */
873 static void remove_client(struct winbindd_cli_state *state)
878 /* It's a dead client - hold a funeral */
884 if (!state->finished) {
885 /* tell client, we are closing ... */
886 nwritten = write(state->sock, &c, sizeof(c));
887 if (nwritten == -1) {
888 DEBUG(2, ("final write to client failed: %s\n",
897 /* Free any getent state */
899 free_getent_state(state->getpwent_state);
900 free_getent_state(state->getgrent_state);
902 /* We may have some extra data that was not freed if the client was
903 killed unexpectedly */
905 SAFE_FREE(state->response.extra_data.data);
907 TALLOC_FREE(state->mem_ctx);
909 remove_fd_event(&state->fd_event);
911 /* Remove from list and free */
913 winbindd_remove_client(state);
917 /* Shutdown client connection which has been idle for the longest time */
919 static bool remove_idle_client(void)
921 struct winbindd_cli_state *state, *remove_state = NULL;
922 time_t last_access = 0;
925 for (state = winbindd_client_list(); state; state = state->next) {
926 if (state->response.result != WINBINDD_PENDING &&
927 state->fd_event.flags == EVENT_FD_READ &&
928 !state->getpwent_state && !state->getgrent_state) {
930 if (!last_access || state->last_access < last_access) {
931 last_access = state->last_access;
932 remove_state = state;
938 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
939 nidle, remove_state->sock, (unsigned int)remove_state->pid));
940 remove_client(remove_state);
947 struct winbindd_listen_state {
950 struct tevent_fd *fde;
953 static void winbindd_listen_fde_handler(struct tevent_context *ev,
954 struct tevent_fd *fde,
958 struct winbindd_listen_state *s = talloc_get_type_abort(private_data,
959 struct winbindd_listen_state);
961 while (winbindd_num_clients() >
962 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
963 DEBUG(5,("winbindd: Exceeding %d client "
964 "connections, removing idle "
966 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
967 if (!remove_idle_client()) {
968 DEBUG(0,("winbindd: Exceeding %d "
969 "client connections, no idle "
970 "connection found\n",
971 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
976 /* new, non-privileged connection */
977 new_connection(s->fd, s->privileged);
980 static bool winbindd_setup_listeners(void)
982 struct winbindd_listen_state *pub_state = NULL;
983 struct winbindd_listen_state *priv_state = NULL;
985 pub_state = talloc(winbind_event_context(),
986 struct winbindd_listen_state);
991 pub_state->privileged = false;
992 pub_state->fd = open_winbindd_socket();
993 if (pub_state->fd == -1) {
997 pub_state->fde = tevent_add_fd(winbind_event_context(),
998 pub_state, pub_state->fd,
1000 winbindd_listen_fde_handler,
1002 if (!pub_state->fde) {
1003 close(pub_state->fd);
1006 tevent_fd_set_auto_close(pub_state->fde);
1008 priv_state = talloc(winbind_event_context(),
1009 struct winbindd_listen_state);
1014 priv_state->privileged = true;
1015 priv_state->fd = open_winbindd_priv_socket();
1016 if (priv_state->fd == -1) {
1020 priv_state->fde = tevent_add_fd(winbind_event_context(),
1021 priv_state, priv_state->fd,
1023 winbindd_listen_fde_handler,
1025 if (!priv_state->fde) {
1026 close(priv_state->fd);
1029 tevent_fd_set_auto_close(priv_state->fde);
1033 TALLOC_FREE(pub_state);
1034 TALLOC_FREE(priv_state);
1038 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
1039 non-forking, non-threaded model which allows us to handle many
1040 simultaneous connections while remaining impervious to many denial of
1043 static void process_loop(void)
1045 struct winbindd_fd_event *ev;
1046 fd_set r_fds, w_fds;
1047 int maxfd = 0, selret;
1048 struct timeval timeout, ev_timeout;
1050 if (run_events(winbind_event_context(), 0, NULL, NULL)) {
1054 /* Initialise fd lists for select() */
1059 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
1060 timeout.tv_usec = 0;
1062 /* Check for any event timeouts. */
1067 event_add_to_select_args(winbind_event_context(), &now,
1068 &r_fds, &w_fds, &ev_timeout, &maxfd);
1070 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
1071 timeout = timeval_min(&timeout, &ev_timeout);
1074 for (ev = fd_events; ev; ev = ev->next) {
1075 if (ev->flags & EVENT_FD_READ) {
1076 FD_SET(ev->fd, &r_fds);
1077 maxfd = MAX(ev->fd, maxfd);
1079 if (ev->flags & EVENT_FD_WRITE) {
1080 FD_SET(ev->fd, &w_fds);
1081 maxfd = MAX(ev->fd, maxfd);
1087 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
1094 if (errno == EINTR) {
1098 /* Select error, something is badly wrong */
1106 if (run_events(winbind_event_context(), selret, &r_fds, &w_fds)) {
1111 while (ev != NULL) {
1112 struct winbindd_fd_event *next = ev->next;
1114 if (FD_ISSET(ev->fd, &r_fds))
1115 flags |= EVENT_FD_READ;
1116 if (FD_ISSET(ev->fd, &w_fds))
1117 flags |= EVENT_FD_WRITE;
1119 ev->handler(ev, flags);
1129 run_events(winbind_event_context(), selret, &r_fds, &w_fds);
1132 winbindd_check_cache_size(time(NULL));
1136 bool winbindd_use_idmap_cache(void)
1138 return !opt_nocache;
1141 bool winbindd_use_cache(void)
1143 return !opt_nocache;
1148 int main(int argc, char **argv, char **envp)
1150 static bool is_daemon = False;
1151 static bool Fork = True;
1152 static bool log_stdout = False;
1153 static bool no_process_group = False;
1157 OPT_NO_PROCESS_GROUP,
1160 struct poptOption long_options[] = {
1162 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
1163 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
1164 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1165 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1166 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1167 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1173 TALLOC_CTX *frame = talloc_stackframe();
1175 /* glibc (?) likes to print "User defined signal 1" and exit if a
1176 SIGUSR[12] is received before a handler is installed */
1178 CatchSignal(SIGUSR1, SIG_IGN);
1179 CatchSignal(SIGUSR2, SIG_IGN);
1181 fault_setup((void (*)(void *))fault_quit );
1182 dump_core_setup("winbindd");
1186 /* Initialise for running in non-root mode */
1190 set_remote_machine_name("winbindd", False);
1192 /* Set environment variable so we don't recursively call ourselves.
1193 This may also be useful interactively. */
1195 if ( !winbind_off() ) {
1196 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1200 /* Initialise samba/rpc client stuff */
1202 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1204 while ((opt = poptGetNextOpt(pc)) != -1) {
1206 /* Don't become a daemon */
1218 case OPT_NO_PROCESS_GROUP:
1219 no_process_group = true;
1221 case OPT_LOG_STDOUT:
1228 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1229 poptBadOption(pc, 0), poptStrerror(opt));
1230 poptPrintUsage(pc, stderr, 0);
1235 if (is_daemon && interactive) {
1236 d_fprintf(stderr,"\nERROR: "
1237 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1238 poptPrintUsage(pc, stderr, 0);
1242 if (log_stdout && Fork) {
1243 d_fprintf(stderr, "\nERROR: "
1244 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1245 poptPrintUsage(pc, stderr, 0);
1249 poptFreeContext(pc);
1251 if (!override_logfile) {
1253 if (asprintf(&lfile,"%s/log.winbindd",
1254 get_dyn_LOGFILEBASE()) > 0) {
1255 lp_set_logfile(lfile);
1259 setup_logging("winbindd", log_stdout);
1262 DEBUG(0,("winbindd version %s started.\n", samba_version_string()));
1263 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1265 if (!lp_load_initial_only(get_dyn_CONFIGFILE())) {
1266 DEBUG(0, ("error opening config file\n"));
1270 /* Initialise messaging system */
1272 if (winbind_messaging_context() == NULL) {
1276 if (!reload_services_file(NULL)) {
1277 DEBUG(0, ("error opening config file\n"));
1281 if (!directory_exist(lp_lockdir())) {
1282 mkdir(lp_lockdir(), 0755);
1292 if (!secrets_init()) {
1294 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1298 /* Enable netbios namecache */
1302 /* Unblock all signals we are interested in as they may have been
1303 blocked by the parent process. */
1305 BlockSignals(False, SIGINT);
1306 BlockSignals(False, SIGQUIT);
1307 BlockSignals(False, SIGTERM);
1308 BlockSignals(False, SIGUSR1);
1309 BlockSignals(False, SIGUSR2);
1310 BlockSignals(False, SIGHUP);
1311 BlockSignals(False, SIGCHLD);
1312 BlockSignals(False, SIGXCPU);
1315 become_daemon(Fork, no_process_group);
1317 pidfile_create("winbindd");
1321 * If we're interactive we want to set our own process group for
1322 * signal management.
1324 if (interactive && !no_process_group)
1325 setpgid( (pid_t)0, (pid_t)0);
1330 /* Don't use winbindd_reinit_after_fork here as
1331 * we're just starting up and haven't created any
1332 * winbindd-specific resources we must free yet. JRA.
1335 if (!NT_STATUS_IS_OK(reinit_after_fork(winbind_messaging_context(),
1336 winbind_event_context(),
1338 DEBUG(0,("reinit_after_fork() failed\n"));
1342 /* Setup signal handlers */
1344 if (!winbindd_setup_sig_term_handler(true))
1346 if (!winbindd_setup_sig_hup_handler(NULL))
1348 if (!winbindd_setup_sig_chld_handler())
1350 if (!winbindd_setup_sig_usr2_handler())
1353 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1356 * Ensure all cache and idmap caches are consistent
1357 * and initialized before we startup.
1359 if (!winbindd_cache_validate_and_initialize()) {
1363 /* get broadcast messages */
1365 if (!serverid_register(procid_self(),
1366 FLAG_MSG_GENERAL|FLAG_MSG_DBWRAP)) {
1367 DEBUG(1, ("Could not register myself in serverid.tdb\n"));
1371 /* React on 'smbcontrol winbindd reload-config' in the same way
1372 as to SIGHUP signal */
1373 messaging_register(winbind_messaging_context(), NULL,
1374 MSG_SMB_CONF_UPDATED, msg_reload_services);
1375 messaging_register(winbind_messaging_context(), NULL,
1376 MSG_SHUTDOWN, msg_shutdown);
1378 /* Handle online/offline messages. */
1379 messaging_register(winbind_messaging_context(), NULL,
1380 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1381 messaging_register(winbind_messaging_context(), NULL,
1382 MSG_WINBIND_ONLINE, winbind_msg_online);
1383 messaging_register(winbind_messaging_context(), NULL,
1384 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1386 messaging_register(winbind_messaging_context(), NULL,
1387 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1389 messaging_register(winbind_messaging_context(), NULL,
1390 MSG_WINBIND_VALIDATE_CACHE,
1391 winbind_msg_validate_cache);
1393 messaging_register(winbind_messaging_context(), NULL,
1394 MSG_WINBIND_DUMP_DOMAIN_LIST,
1395 winbind_msg_dump_domain_list);
1397 messaging_register(winbind_messaging_context(), NULL,
1398 MSG_WINBIND_IP_DROPPED,
1399 winbind_msg_ip_dropped_parent);
1401 /* Register handler for MSG_DEBUG. */
1402 messaging_register(winbind_messaging_context(), NULL,
1406 netsamlogon_cache_init(); /* Non-critical */
1408 /* clear the cached list of trusted domains */
1412 if (!init_domain_list()) {
1413 DEBUG(0,("unable to initialize domain list\n"));
1418 init_locator_child();
1420 smb_nscd_flush_user_cache();
1421 smb_nscd_flush_group_cache();
1423 /* setup listen sockets */
1425 if (!winbindd_setup_listeners()) {
1426 DEBUG(0,("winbindd_setup_listeners() failed\n"));
1431 /* Loop waiting for requests */
1433 struct winbindd_cli_state *state;
1435 frame = talloc_stackframe();
1437 /* refresh the trusted domain cache */
1439 rescan_trusted_domains();
1441 /* Dispose of client connection if it is marked as
1443 state = winbindd_client_list();
1445 struct winbindd_cli_state *next = state->next;
1447 if (state->finished) {
1448 remove_client(state);