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 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;
50 if (!ctx && !(ctx = messaging_init(NULL, server_id_self(),
51 winbind_event_context()))) {
52 smb_panic("Could not init winbind messaging context");
57 /* Reload configuration */
59 static BOOL reload_services_file(void)
66 pstrcpy(fname,lp_configfile());
67 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
68 pstrcpy(dyn_CONFIGFILE,fname);
73 ret = lp_load(dyn_CONFIGFILE,False,False,True,True);
82 /**************************************************************************** **
84 **************************************************************************** */
86 static void fault_quit(void)
91 static void winbindd_status(void)
93 struct winbindd_cli_state *tmp;
95 DEBUG(0, ("winbindd status:\n"));
97 /* Print client state information */
99 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
101 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
102 DEBUG(2, ("\tclient list:\n"));
103 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
104 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
105 (unsigned long)tmp->pid, tmp->sock));
110 /* Print winbindd status to log file */
112 static void print_winbindd_status(void)
117 /* Flush client cache */
119 static void flush_caches(void)
121 /* We need to invalidate cached user list entries on a SIGHUP
122 otherwise cached access denied errors due to restrict anonymous
123 hang around until the sequence number changes. */
125 wcache_invalidate_cache();
128 /* Handle the signal by unlinking socket and exiting */
130 static void terminate(void)
134 /* Remove socket file */
135 pstr_sprintf(path, "%s/%s",
136 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME);
141 trustdom_cache_shutdown();
145 TALLOC_CTX *mem_ctx = talloc_init("end_description");
146 char *description = talloc_describe_all(mem_ctx);
148 DEBUG(3, ("tallocs left:\n%s\n", description));
149 talloc_destroy(mem_ctx);
156 static BOOL do_sigterm;
158 static void termination_handler(int signum)
161 sys_select_signal(signum);
164 static BOOL do_sigusr2;
166 static void sigusr2_handler(int signum)
169 sys_select_signal(SIGUSR2);
172 static BOOL do_sighup;
174 static void sighup_handler(int signum)
177 sys_select_signal(SIGHUP);
180 static BOOL do_sigchld;
182 static void sigchld_handler(int signum)
185 sys_select_signal(SIGCHLD);
188 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
189 static void msg_reload_services(struct messaging_context *msg,
192 struct server_id server_id,
195 /* Flush various caches */
197 reload_services_file();
200 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
201 static void msg_shutdown(struct messaging_context *msg,
204 struct server_id server_id,
211 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
214 struct server_id server_id,
219 struct sigaction act;
220 struct sigaction oldact;
222 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
226 * call the validation code from a child:
227 * so we don't block the main winbindd and the validation
228 * code can safely use fork/waitpid...
231 child_pid = sys_fork();
233 if (child_pid == -1) {
234 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
239 if (child_pid != 0) {
241 DEBUG(5, ("winbind_msg_validate_cache: child created with "
242 "pid %d.\n", child_pid));
248 /* install default SIGCHLD handler: validation code uses fork/waitpid */
250 act.sa_handler = SIG_DFL;
252 /* We *want* SIGALRM to interrupt a system call. */
253 act.sa_flags = SA_RESTART;
255 sigemptyset(&act.sa_mask);
256 sigaddset(&act.sa_mask,SIGCHLD);
257 sigaction(SIGCHLD,&act,&oldact);
259 ret = (uint8)winbindd_validate_cache_nobackup();
260 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
261 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
266 static struct winbindd_dispatch_table {
267 enum winbindd_cmd cmd;
268 void (*fn)(struct winbindd_cli_state *state);
269 const char *winbindd_cmd_name;
270 } dispatch_table[] = {
274 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
275 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
277 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
278 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
279 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
281 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
282 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
283 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
284 "GETUSERDOMGROUPS" },
286 /* Group functions */
288 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
289 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
290 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
291 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
292 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
293 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
295 /* PAM auth functions */
297 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
298 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
299 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
300 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
301 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
303 /* Enumeration functions */
305 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
306 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
307 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
309 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
311 /* SID related functions */
313 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
314 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
315 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
317 /* Lookup related functions */
319 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
320 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
321 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
322 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
323 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
324 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
325 #endif /* end DISABLED */
326 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
327 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
328 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
329 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
333 { WINBINDD_DUMP_MAPS, winbindd_dump_maps, "DUMP_MAPS" },
335 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
336 { WINBINDD_PING, winbindd_ping, "PING" },
337 { WINBINDD_INFO, winbindd_info, "INFO" },
338 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
339 "INTERFACE_VERSION" },
340 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
341 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
342 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
343 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
344 "WINBINDD_PRIV_PIPE_DIR" },
345 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
346 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
348 /* Credential cache access */
349 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
353 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
354 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
358 { WINBINDD_NUM_CMDS, NULL, "NONE" }
361 static void process_request(struct winbindd_cli_state *state)
363 struct winbindd_dispatch_table *table = dispatch_table;
365 /* Free response data - we may be interrupted and receive another
366 command before being able to send this data off. */
368 SAFE_FREE(state->response.extra_data.data);
370 ZERO_STRUCT(state->response);
372 state->response.result = WINBINDD_PENDING;
373 state->response.length = sizeof(struct winbindd_response);
375 state->mem_ctx = talloc_init("winbind request");
376 if (state->mem_ctx == NULL)
379 /* Remember who asked us. */
380 state->pid = state->request.pid;
382 /* Process command */
384 for (table = dispatch_table; table->fn; table++) {
385 if (state->request.cmd == table->cmd) {
386 DEBUG(10,("process_request: request fn %s\n",
387 table->winbindd_cmd_name ));
394 DEBUG(10,("process_request: unknown request fn number %d\n",
395 (int)state->request.cmd ));
396 request_error(state);
401 * A list of file descriptors being monitored by select in the main processing
402 * loop. fd_event->handler is called whenever the socket is readable/writable.
405 static struct fd_event *fd_events = NULL;
407 void add_fd_event(struct fd_event *ev)
409 struct fd_event *match;
411 /* only add unique fd_event structs */
413 for (match=fd_events; match; match=match->next ) {
415 SMB_ASSERT( match != ev );
422 DLIST_ADD(fd_events, ev);
425 void remove_fd_event(struct fd_event *ev)
427 DLIST_REMOVE(fd_events, ev);
431 * Handler for fd_events to complete a read/write request, set up by
432 * setup_async_read/setup_async_write.
435 static void rw_callback(struct fd_event *event, int flags)
440 todo = event->length - event->done;
442 if (event->flags & EVENT_FD_WRITE) {
443 SMB_ASSERT(flags == EVENT_FD_WRITE);
444 done = sys_write(event->fd,
445 &((char *)event->data)[event->done],
450 event->finished(event->private_data, False);
455 if (event->flags & EVENT_FD_READ) {
456 SMB_ASSERT(flags == EVENT_FD_READ);
457 done = sys_read(event->fd, &((char *)event->data)[event->done],
462 event->finished(event->private_data, False);
469 if (event->done == event->length) {
471 event->finished(event->private_data, True);
476 * Request an async read/write on a fd_event structure. (*finished) is called
477 * when the request is completed or an error had occurred.
480 void setup_async_read(struct fd_event *event, void *data, size_t length,
481 void (*finished)(void *private_data, BOOL success),
484 SMB_ASSERT(event->flags == 0);
486 event->length = length;
488 event->handler = rw_callback;
489 event->finished = finished;
490 event->private_data = private_data;
491 event->flags = EVENT_FD_READ;
494 void setup_async_write(struct fd_event *event, void *data, size_t length,
495 void (*finished)(void *private_data, BOOL success),
498 SMB_ASSERT(event->flags == 0);
500 event->length = length;
502 event->handler = rw_callback;
503 event->finished = finished;
504 event->private_data = private_data;
505 event->flags = EVENT_FD_WRITE;
509 * This is the main event loop of winbind requests. It goes through a
510 * state-machine of 3 read/write requests, 4 if you have extra data to send.
512 * An idle winbind client has a read request of 4 bytes outstanding,
513 * finalizing function is request_len_recv, checking the length. request_recv
514 * then processes the packet. The processing function then at some point has
515 * to call request_finished which schedules sending the response.
518 static void request_len_recv(void *private_data, BOOL success);
519 static void request_recv(void *private_data, BOOL success);
520 static void request_main_recv(void *private_data, BOOL success);
521 static void request_finished(struct winbindd_cli_state *state);
522 void request_finished_cont(void *private_data, BOOL success);
523 static void response_main_sent(void *private_data, BOOL success);
524 static void response_extra_sent(void *private_data, BOOL success);
526 static void response_extra_sent(void *private_data, BOOL success)
528 struct winbindd_cli_state *state =
529 talloc_get_type_abort(private_data, struct winbindd_cli_state);
531 if (state->mem_ctx != NULL) {
532 talloc_destroy(state->mem_ctx);
533 state->mem_ctx = NULL;
537 state->finished = True;
541 SAFE_FREE(state->request.extra_data.data);
542 SAFE_FREE(state->response.extra_data.data);
544 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
545 request_len_recv, state);
548 static void response_main_sent(void *private_data, BOOL success)
550 struct winbindd_cli_state *state =
551 talloc_get_type_abort(private_data, struct winbindd_cli_state);
554 state->finished = True;
558 if (state->response.length == sizeof(state->response)) {
559 if (state->mem_ctx != NULL) {
560 talloc_destroy(state->mem_ctx);
561 state->mem_ctx = NULL;
564 setup_async_read(&state->fd_event, &state->request,
565 sizeof(uint32), request_len_recv, state);
569 setup_async_write(&state->fd_event, state->response.extra_data.data,
570 state->response.length - sizeof(state->response),
571 response_extra_sent, state);
574 static void request_finished(struct winbindd_cli_state *state)
576 setup_async_write(&state->fd_event, &state->response,
577 sizeof(state->response), response_main_sent, state);
580 void request_error(struct winbindd_cli_state *state)
582 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
583 state->response.result = WINBINDD_ERROR;
584 request_finished(state);
587 void request_ok(struct winbindd_cli_state *state)
589 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
590 state->response.result = WINBINDD_OK;
591 request_finished(state);
594 void request_finished_cont(void *private_data, BOOL success)
596 struct winbindd_cli_state *state =
597 talloc_get_type_abort(private_data, struct winbindd_cli_state);
602 request_error(state);
605 static void request_len_recv(void *private_data, BOOL success)
607 struct winbindd_cli_state *state =
608 talloc_get_type_abort(private_data, struct winbindd_cli_state);
611 state->finished = True;
615 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
616 DEBUG(0,("request_len_recv: Invalid request size received: %d\n",
617 *(uint32 *)(&state->request)));
618 state->finished = True;
622 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
623 sizeof(state->request) - sizeof(uint32),
624 request_main_recv, state);
627 static void request_main_recv(void *private_data, BOOL success)
629 struct winbindd_cli_state *state =
630 talloc_get_type_abort(private_data, struct winbindd_cli_state);
633 state->finished = True;
637 if (state->request.extra_len == 0) {
638 state->request.extra_data.data = NULL;
639 request_recv(state, True);
643 if ((!state->privileged) &&
644 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
645 DEBUG(3, ("Got request with %d bytes extra data on "
646 "unprivileged socket\n", (int)state->request.extra_len));
647 state->request.extra_data.data = NULL;
648 state->finished = True;
652 state->request.extra_data.data =
653 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
655 if (state->request.extra_data.data == NULL) {
656 DEBUG(0, ("malloc failed\n"));
657 state->finished = True;
661 /* Ensure null termination */
662 state->request.extra_data.data[state->request.extra_len] = '\0';
664 setup_async_read(&state->fd_event, state->request.extra_data.data,
665 state->request.extra_len, request_recv, state);
668 static void request_recv(void *private_data, BOOL success)
670 struct winbindd_cli_state *state =
671 talloc_get_type_abort(private_data, struct winbindd_cli_state);
674 state->finished = True;
678 process_request(state);
681 /* Process a new connection by adding it to the client connection list */
683 static void new_connection(int listen_sock, BOOL privileged)
685 struct sockaddr_un sunaddr;
686 struct winbindd_cli_state *state;
690 /* Accept connection */
692 len = sizeof(sunaddr);
695 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
696 } while (sock == -1 && errno == EINTR);
701 DEBUG(6,("accepted socket %d\n", sock));
703 /* Create new connection structure */
705 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
712 state->last_access = time(NULL);
714 state->privileged = privileged;
716 state->fd_event.fd = state->sock;
717 state->fd_event.flags = 0;
718 add_fd_event(&state->fd_event);
720 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
721 request_len_recv, state);
723 /* Add to connection list */
725 winbindd_add_client(state);
728 /* Remove a client connection from client connection list */
730 static void remove_client(struct winbindd_cli_state *state)
732 /* It's a dead client - hold a funeral */
742 /* Free any getent state */
744 free_getent_state(state->getpwent_state);
745 free_getent_state(state->getgrent_state);
747 /* We may have some extra data that was not freed if the client was
748 killed unexpectedly */
750 SAFE_FREE(state->response.extra_data.data);
752 if (state->mem_ctx != NULL) {
753 talloc_destroy(state->mem_ctx);
754 state->mem_ctx = NULL;
757 remove_fd_event(&state->fd_event);
759 /* Remove from list and free */
761 winbindd_remove_client(state);
765 /* Shutdown client connection which has been idle for the longest time */
767 static BOOL remove_idle_client(void)
769 struct winbindd_cli_state *state, *remove_state = NULL;
770 time_t last_access = 0;
773 for (state = winbindd_client_list(); state; state = state->next) {
774 if (state->response.result != WINBINDD_PENDING &&
775 !state->getpwent_state && !state->getgrent_state) {
777 if (!last_access || state->last_access < last_access) {
778 last_access = state->last_access;
779 remove_state = state;
785 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
786 nidle, remove_state->sock, (unsigned int)remove_state->pid));
787 remove_client(remove_state);
794 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
795 non-forking, non-threaded model which allows us to handle many
796 simultaneous connections while remaining impervious to many denial of
799 static void process_loop(void)
801 struct winbindd_cli_state *state;
804 int maxfd, listen_sock, listen_priv_sock, selret;
805 struct timeval timeout, ev_timeout;
807 /* Open Sockets here to get stuff going ASAP */
808 listen_sock = open_winbindd_socket();
809 listen_priv_sock = open_winbindd_priv_socket();
811 if (listen_sock == -1 || listen_priv_sock == -1) {
812 perror("open_winbind_socket");
816 /* We'll be doing this a lot */
818 /* Handle messages */
820 message_dispatch(winbind_messaging_context());
822 run_events(winbind_event_context(), 0, NULL, NULL);
824 /* refresh the trusted domain cache */
826 rescan_trusted_domains();
828 /* Initialise fd lists for select() */
830 maxfd = MAX(listen_sock, listen_priv_sock);
834 FD_SET(listen_sock, &r_fds);
835 FD_SET(listen_priv_sock, &r_fds);
837 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
840 /* Check for any event timeouts. */
841 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
842 timeout = timeval_min(&timeout, &ev_timeout);
845 /* Set up client readers and writers */
847 state = winbindd_client_list();
851 struct winbindd_cli_state *next = state->next;
853 /* Dispose of client connection if it is marked as
857 remove_client(state);
862 for (ev = fd_events; ev; ev = ev->next) {
863 if (ev->flags & EVENT_FD_READ) {
864 FD_SET(ev->fd, &r_fds);
865 maxfd = MAX(ev->fd, maxfd);
867 if (ev->flags & EVENT_FD_WRITE) {
868 FD_SET(ev->fd, &w_fds);
869 maxfd = MAX(ev->fd, maxfd);
875 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
882 if (errno == EINTR) {
886 /* Select error, something is badly wrong */
896 struct fd_event *next = ev->next;
898 if (FD_ISSET(ev->fd, &r_fds))
899 flags |= EVENT_FD_READ;
900 if (FD_ISSET(ev->fd, &w_fds))
901 flags |= EVENT_FD_WRITE;
903 ev->handler(ev, flags);
907 if (FD_ISSET(listen_sock, &r_fds)) {
908 while (winbindd_num_clients() >
909 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
910 DEBUG(5,("winbindd: Exceeding %d client "
911 "connections, removing idle "
913 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
914 if (!remove_idle_client()) {
915 DEBUG(0,("winbindd: Exceeding %d "
916 "client connections, no idle "
917 "connection found\n",
918 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
922 /* new, non-privileged connection */
923 new_connection(listen_sock, False);
926 if (FD_ISSET(listen_priv_sock, &r_fds)) {
927 while (winbindd_num_clients() >
928 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
929 DEBUG(5,("winbindd: Exceeding %d client "
930 "connections, removing idle "
932 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
933 if (!remove_idle_client()) {
934 DEBUG(0,("winbindd: Exceeding %d "
935 "client connections, no idle "
936 "connection found\n",
937 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
941 /* new, privileged connection */
942 new_connection(listen_priv_sock, True);
948 winbindd_check_cache_size(time(NULL));
951 /* Check signal handling things */
958 DEBUG(3, ("got SIGHUP\n"));
961 reload_services_file();
967 print_winbindd_status();
976 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
977 winbind_child_died(pid);
984 int main(int argc, char **argv, char **envp)
987 static BOOL is_daemon = False;
988 static BOOL Fork = True;
989 static BOOL log_stdout = False;
990 static BOOL no_process_group = False;
991 struct poptOption long_options[] = {
993 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
994 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
995 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
996 { "daemon", 'D', POPT_ARG_NONE, NULL, 'D', "Become a daemon (default)" },
997 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
998 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
1005 /* glibc (?) likes to print "User defined signal 1" and exit if a
1006 SIGUSR[12] is received before a handler is installed */
1008 CatchSignal(SIGUSR1, SIG_IGN);
1009 CatchSignal(SIGUSR2, SIG_IGN);
1011 fault_setup((void (*)(void *))fault_quit );
1012 dump_core_setup("winbindd");
1016 /* Initialise for running in non-root mode */
1020 set_remote_machine_name("winbindd", False);
1022 /* Set environment variable so we don't recursively call ourselves.
1023 This may also be useful interactively. */
1025 if ( !winbind_off() ) {
1026 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1030 /* Initialise samba/rpc client stuff */
1032 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1034 while ((opt = poptGetNextOpt(pc)) != -1) {
1036 /* Don't become a daemon */
1046 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1047 poptBadOption(pc, 0), poptStrerror(opt));
1048 poptPrintUsage(pc, stderr, 0);
1053 if (is_daemon && interactive) {
1054 d_fprintf(stderr,"\nERROR: "
1055 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1056 poptPrintUsage(pc, stderr, 0);
1060 if (log_stdout && Fork) {
1061 d_fprintf(stderr, "\nERROR: "
1062 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1063 poptPrintUsage(pc, stderr, 0);
1067 poptFreeContext(pc);
1069 if (!override_logfile) {
1070 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
1071 lp_set_logfile(logfile);
1073 setup_logging("winbindd", log_stdout);
1076 DEBUG(1, ("winbindd version %s started.\n%s\n",
1077 SAMBA_VERSION_STRING,
1078 COPYRIGHT_STARTUP_MESSAGE) );
1080 if (!reload_services_file()) {
1081 DEBUG(0, ("error opening config file\n"));
1085 if (!directory_exist(lp_lockdir(), NULL)) {
1086 mkdir(lp_lockdir(), 0755);
1096 if (!secrets_init()) {
1098 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1102 /* Enable netbios namecache */
1106 /* Winbind daemon initialisation */
1108 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1109 DEBUG(1, ("Could not init idmap cache!\n"));
1112 /* Unblock all signals we are interested in as they may have been
1113 blocked by the parent process. */
1115 BlockSignals(False, SIGINT);
1116 BlockSignals(False, SIGQUIT);
1117 BlockSignals(False, SIGTERM);
1118 BlockSignals(False, SIGUSR1);
1119 BlockSignals(False, SIGUSR2);
1120 BlockSignals(False, SIGHUP);
1121 BlockSignals(False, SIGCHLD);
1123 /* Setup signal handlers */
1125 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1126 CatchSignal(SIGQUIT, termination_handler);
1127 CatchSignal(SIGTERM, termination_handler);
1128 CatchSignal(SIGCHLD, sigchld_handler);
1130 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1132 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1133 CatchSignal(SIGHUP, sighup_handler);
1136 become_daemon(Fork, no_process_group);
1138 pidfile_create("winbindd");
1140 /* Ensure all cache and idmap caches are consistent
1141 before we startup. */
1143 if (winbindd_validate_cache()) {
1144 /* We have a bad cache, but luckily we
1145 just deleted it. Restart ourselves */
1147 /* Ensure we have no open low fd's. */
1148 for (i = 3; i < 100; i++) {
1151 return execve(argv[0], argv, envp);
1156 * If we're interactive we want to set our own process group for
1157 * signal management.
1159 if (interactive && !no_process_group)
1160 setpgid( (pid_t)0, (pid_t)0);
1165 /* Initialise messaging system */
1167 if (winbind_messaging_context() == NULL) {
1168 DEBUG(0, ("unable to initialize messaging system\n"));
1172 /* Initialize cache (ensure version is correct). */
1173 if (!initialize_winbindd_cache()) {
1177 /* React on 'smbcontrol winbindd reload-config' in the same way
1178 as to SIGHUP signal */
1179 messaging_register(winbind_messaging_context(), NULL,
1180 MSG_SMB_CONF_UPDATED, msg_reload_services);
1181 messaging_register(winbind_messaging_context(), NULL,
1182 MSG_SHUTDOWN, msg_shutdown);
1184 /* Handle online/offline messages. */
1185 messaging_register(winbind_messaging_context(), NULL,
1186 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1187 messaging_register(winbind_messaging_context(), NULL,
1188 MSG_WINBIND_ONLINE, winbind_msg_online);
1189 messaging_register(winbind_messaging_context(), NULL,
1190 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1192 messaging_register(winbind_messaging_context(), NULL,
1193 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1195 messaging_register(winbind_messaging_context(), NULL,
1196 MSG_WINBIND_VALIDATE_CACHE,
1197 winbind_msg_validate_cache);
1199 netsamlogon_cache_init(); /* Non-critical */
1201 /* clear the cached list of trusted domains */
1205 if (!init_domain_list()) {
1206 DEBUG(0,("unable to initalize domain list\n"));
1211 init_locator_child();
1213 smb_nscd_flush_user_cache();
1214 smb_nscd_flush_group_cache();
1216 /* Loop waiting for requests */
1219 TALLOC_CTX *frame = talloc_stackframe();