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 2 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, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #define DBGC_CLASS DBGC_WINBIND
32 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 smbd event context\n");
47 /* Reload configuration */
49 static BOOL reload_services_file(void)
56 pstrcpy(fname,lp_configfile());
57 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
58 pstrcpy(dyn_CONFIGFILE,fname);
63 ret = lp_load(dyn_CONFIGFILE,False,False,True,True);
72 /**************************************************************************** **
74 **************************************************************************** */
76 static void fault_quit(void)
81 static void winbindd_status(void)
83 struct winbindd_cli_state *tmp;
85 DEBUG(0, ("winbindd status:\n"));
87 /* Print client state information */
89 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
91 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
92 DEBUG(2, ("\tclient list:\n"));
93 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
94 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
95 (unsigned long)tmp->pid, tmp->sock));
100 /* Print winbindd status to log file */
102 static void print_winbindd_status(void)
107 /* Flush client cache */
109 static void flush_caches(void)
111 /* We need to invalidate cached user list entries on a SIGHUP
112 otherwise cached access denied errors due to restrict anonymous
113 hang around until the sequence number changes. */
115 wcache_invalidate_cache();
118 /* Handle the signal by unlinking socket and exiting */
120 static void terminate(void)
126 /* Remove socket file */
127 pstr_sprintf(path, "%s/%s",
128 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
133 TALLOC_CTX *mem_ctx = talloc_init("end_description");
134 char *description = talloc_describe_all(mem_ctx);
136 DEBUG(3, ("tallocs left:\n%s\n", description));
137 talloc_destroy(mem_ctx);
144 static BOOL do_sigterm;
146 static void termination_handler(int signum)
149 sys_select_signal(signum);
152 static BOOL do_sigusr2;
154 static void sigusr2_handler(int signum)
157 sys_select_signal(SIGUSR2);
160 static BOOL do_sighup;
162 static void sighup_handler(int signum)
165 sys_select_signal(SIGHUP);
168 static BOOL do_sigchld;
170 static void sigchld_handler(int signum)
173 sys_select_signal(SIGCHLD);
176 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
177 static void msg_reload_services(int msg_type, struct process_id src, void *buf, size_t len)
179 /* Flush various caches */
181 reload_services_file();
184 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
185 static void msg_shutdown(int msg_type, struct process_id src, void *buf, size_t len)
190 static struct winbindd_dispatch_table {
191 enum winbindd_cmd cmd;
192 void (*fn)(struct winbindd_cli_state *state);
193 const char *winbindd_cmd_name;
194 } dispatch_table[] = {
198 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
199 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
201 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
202 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
203 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
205 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
206 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
207 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
208 "GETUSERDOMGROUPS" },
210 /* Group functions */
212 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
213 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
214 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
215 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
216 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
217 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
219 /* PAM auth functions */
221 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
222 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
223 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
224 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
225 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
227 /* Enumeration functions */
229 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
230 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
231 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
233 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
235 /* SID related functions */
237 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
238 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
239 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
241 /* Lookup related functions */
243 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
244 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
245 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
246 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
247 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
248 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
249 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
250 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
251 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
255 { WINBINDD_DUMP_MAPS, winbindd_dump_maps, "DUMP_MAPS" },
257 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
258 { WINBINDD_PING, winbindd_ping, "PING" },
259 { WINBINDD_INFO, winbindd_info, "INFO" },
260 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
261 "INTERFACE_VERSION" },
262 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
263 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
264 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
265 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
266 "WINBINDD_PRIV_PIPE_DIR" },
267 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
269 /* Credential cache access */
270 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
274 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
275 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
279 { WINBINDD_NUM_CMDS, NULL, "NONE" }
282 static void process_request(struct winbindd_cli_state *state)
284 struct winbindd_dispatch_table *table = dispatch_table;
286 /* Free response data - we may be interrupted and receive another
287 command before being able to send this data off. */
289 SAFE_FREE(state->response.extra_data.data);
291 ZERO_STRUCT(state->response);
293 state->response.result = WINBINDD_PENDING;
294 state->response.length = sizeof(struct winbindd_response);
296 state->mem_ctx = talloc_init("winbind request");
297 if (state->mem_ctx == NULL)
300 /* Process command */
302 for (table = dispatch_table; table->fn; table++) {
303 if (state->request.cmd == table->cmd) {
304 DEBUG(10,("process_request: request fn %s\n",
305 table->winbindd_cmd_name ));
312 DEBUG(10,("process_request: unknown request fn number %d\n",
313 (int)state->request.cmd ));
314 request_error(state);
319 * A list of file descriptors being monitored by select in the main processing
320 * loop. fd_event->handler is called whenever the socket is readable/writable.
323 static struct fd_event *fd_events = NULL;
325 void add_fd_event(struct fd_event *ev)
327 struct fd_event *match;
329 /* only add unique fd_event structs */
331 for (match=fd_events; match; match=match->next ) {
333 SMB_ASSERT( match != ev );
340 DLIST_ADD(fd_events, ev);
343 void remove_fd_event(struct fd_event *ev)
345 DLIST_REMOVE(fd_events, ev);
349 * Handler for fd_events to complete a read/write request, set up by
350 * setup_async_read/setup_async_write.
353 static void rw_callback(struct fd_event *event, int flags)
358 todo = event->length - event->done;
360 if (event->flags & EVENT_FD_WRITE) {
361 SMB_ASSERT(flags == EVENT_FD_WRITE);
362 done = sys_write(event->fd,
363 &((char *)event->data)[event->done],
368 event->finished(event->private_data, False);
373 if (event->flags & EVENT_FD_READ) {
374 SMB_ASSERT(flags == EVENT_FD_READ);
375 done = sys_read(event->fd, &((char *)event->data)[event->done],
380 event->finished(event->private_data, False);
387 if (event->done == event->length) {
389 event->finished(event->private_data, True);
394 * Request an async read/write on a fd_event structure. (*finished) is called
395 * when the request is completed or an error had occurred.
398 void setup_async_read(struct fd_event *event, void *data, size_t length,
399 void (*finished)(void *private_data, BOOL success),
402 SMB_ASSERT(event->flags == 0);
404 event->length = length;
406 event->handler = rw_callback;
407 event->finished = finished;
408 event->private_data = private_data;
409 event->flags = EVENT_FD_READ;
412 void setup_async_write(struct fd_event *event, void *data, size_t length,
413 void (*finished)(void *private_data, BOOL success),
416 SMB_ASSERT(event->flags == 0);
418 event->length = length;
420 event->handler = rw_callback;
421 event->finished = finished;
422 event->private_data = private_data;
423 event->flags = EVENT_FD_WRITE;
427 * This is the main event loop of winbind requests. It goes through a
428 * state-machine of 3 read/write requests, 4 if you have extra data to send.
430 * An idle winbind client has a read request of 4 bytes outstanding,
431 * finalizing function is request_len_recv, checking the length. request_recv
432 * then processes the packet. The processing function then at some point has
433 * to call request_finished which schedules sending the response.
436 static void request_len_recv(void *private_data, BOOL success);
437 static void request_recv(void *private_data, BOOL success);
438 static void request_main_recv(void *private_data, BOOL success);
439 static void request_finished(struct winbindd_cli_state *state);
440 void request_finished_cont(void *private_data, BOOL success);
441 static void response_main_sent(void *private_data, BOOL success);
442 static void response_extra_sent(void *private_data, BOOL success);
444 static void response_extra_sent(void *private_data, BOOL success)
446 struct winbindd_cli_state *state =
447 talloc_get_type_abort(private_data, struct winbindd_cli_state);
449 if (state->mem_ctx != NULL) {
450 talloc_destroy(state->mem_ctx);
451 state->mem_ctx = NULL;
455 state->finished = True;
459 SAFE_FREE(state->request.extra_data.data);
460 SAFE_FREE(state->response.extra_data.data);
462 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
463 request_len_recv, state);
466 static void response_main_sent(void *private_data, BOOL success)
468 struct winbindd_cli_state *state =
469 talloc_get_type_abort(private_data, struct winbindd_cli_state);
472 state->finished = True;
476 if (state->response.length == sizeof(state->response)) {
477 if (state->mem_ctx != NULL) {
478 talloc_destroy(state->mem_ctx);
479 state->mem_ctx = NULL;
482 setup_async_read(&state->fd_event, &state->request,
483 sizeof(uint32), request_len_recv, state);
487 setup_async_write(&state->fd_event, state->response.extra_data.data,
488 state->response.length - sizeof(state->response),
489 response_extra_sent, state);
492 static void request_finished(struct winbindd_cli_state *state)
494 setup_async_write(&state->fd_event, &state->response,
495 sizeof(state->response), response_main_sent, state);
498 void request_error(struct winbindd_cli_state *state)
500 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
501 state->response.result = WINBINDD_ERROR;
502 request_finished(state);
505 void request_ok(struct winbindd_cli_state *state)
507 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
508 state->response.result = WINBINDD_OK;
509 request_finished(state);
512 void request_finished_cont(void *private_data, BOOL success)
514 struct winbindd_cli_state *state =
515 talloc_get_type_abort(private_data, struct winbindd_cli_state);
520 request_error(state);
523 static void request_len_recv(void *private_data, BOOL success)
525 struct winbindd_cli_state *state =
526 talloc_get_type_abort(private_data, struct winbindd_cli_state);
529 state->finished = True;
533 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
534 DEBUG(0,("request_len_recv: Invalid request size received: %d\n",
535 *(uint32 *)(&state->request)));
536 state->finished = True;
540 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
541 sizeof(state->request) - sizeof(uint32),
542 request_main_recv, state);
545 static void request_main_recv(void *private_data, BOOL success)
547 struct winbindd_cli_state *state =
548 talloc_get_type_abort(private_data, struct winbindd_cli_state);
551 state->finished = True;
555 if (state->request.extra_len == 0) {
556 state->request.extra_data.data = NULL;
557 request_recv(state, True);
561 if ((!state->privileged) &&
562 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
563 DEBUG(3, ("Got request with %d bytes extra data on "
564 "unprivileged socket\n", (int)state->request.extra_len));
565 state->request.extra_data.data = NULL;
566 state->finished = True;
570 state->request.extra_data.data =
571 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
573 if (state->request.extra_data.data == NULL) {
574 DEBUG(0, ("malloc failed\n"));
575 state->finished = True;
579 /* Ensure null termination */
580 state->request.extra_data.data[state->request.extra_len] = '\0';
582 setup_async_read(&state->fd_event, state->request.extra_data.data,
583 state->request.extra_len, request_recv, state);
586 static void request_recv(void *private_data, BOOL success)
588 struct winbindd_cli_state *state =
589 talloc_get_type_abort(private_data, struct winbindd_cli_state);
592 state->finished = True;
596 process_request(state);
599 /* Process a new connection by adding it to the client connection list */
601 static void new_connection(int listen_sock, BOOL privileged)
603 struct sockaddr_un sunaddr;
604 struct winbindd_cli_state *state;
608 /* Accept connection */
610 len = sizeof(sunaddr);
613 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
614 } while (sock == -1 && errno == EINTR);
619 DEBUG(6,("accepted socket %d\n", sock));
621 /* Create new connection structure */
623 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL)
628 state->last_access = time(NULL);
630 state->privileged = privileged;
632 state->fd_event.fd = state->sock;
633 state->fd_event.flags = 0;
634 add_fd_event(&state->fd_event);
636 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
637 request_len_recv, state);
639 /* Add to connection list */
641 winbindd_add_client(state);
644 /* Remove a client connection from client connection list */
646 static void remove_client(struct winbindd_cli_state *state)
648 /* It's a dead client - hold a funeral */
658 /* Free any getent state */
660 free_getent_state(state->getpwent_state);
661 free_getent_state(state->getgrent_state);
663 /* We may have some extra data that was not freed if the client was
664 killed unexpectedly */
666 SAFE_FREE(state->response.extra_data.data);
668 if (state->mem_ctx != NULL) {
669 talloc_destroy(state->mem_ctx);
670 state->mem_ctx = NULL;
673 remove_fd_event(&state->fd_event);
675 /* Remove from list and free */
677 winbindd_remove_client(state);
681 /* Shutdown client connection which has been idle for the longest time */
683 static BOOL remove_idle_client(void)
685 struct winbindd_cli_state *state, *remove_state = NULL;
686 time_t last_access = 0;
689 for (state = winbindd_client_list(); state; state = state->next) {
690 if (state->response.result != WINBINDD_PENDING &&
691 !state->getpwent_state && !state->getgrent_state) {
693 if (!last_access || state->last_access < last_access) {
694 last_access = state->last_access;
695 remove_state = state;
701 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
702 nidle, remove_state->sock, (unsigned int)remove_state->pid));
703 remove_client(remove_state);
710 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
711 non-forking, non-threaded model which allows us to handle many
712 simultaneous connections while remaining impervious to many denial of
715 static void process_loop(void)
717 struct winbindd_cli_state *state;
720 int maxfd, listen_sock, listen_priv_sock, selret;
721 struct timeval timeout, ev_timeout;
723 /* We'll be doing this a lot */
725 /* Handle messages */
729 run_events(winbind_event_context(), 0, NULL, NULL);
731 /* refresh the trusted domain cache */
733 rescan_trusted_domains();
735 /* Free up temporary memory */
738 main_loop_TALLOC_FREE();
740 /* Initialise fd lists for select() */
742 listen_sock = open_winbindd_socket();
743 listen_priv_sock = open_winbindd_priv_socket();
745 if (listen_sock == -1 || listen_priv_sock == -1) {
746 perror("open_winbind_socket");
750 maxfd = MAX(listen_sock, listen_priv_sock);
754 FD_SET(listen_sock, &r_fds);
755 FD_SET(listen_priv_sock, &r_fds);
757 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
760 /* Check for any event timeouts. */
761 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
762 timeout = timeval_min(&timeout, &ev_timeout);
765 /* Set up client readers and writers */
767 state = winbindd_client_list();
771 struct winbindd_cli_state *next = state->next;
773 /* Dispose of client connection if it is marked as
777 remove_client(state);
782 for (ev = fd_events; ev; ev = ev->next) {
783 if (ev->flags & EVENT_FD_READ) {
784 FD_SET(ev->fd, &r_fds);
785 maxfd = MAX(ev->fd, maxfd);
787 if (ev->flags & EVENT_FD_WRITE) {
788 FD_SET(ev->fd, &w_fds);
789 maxfd = MAX(ev->fd, maxfd);
795 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
802 if (errno == EINTR) {
806 /* Select error, something is badly wrong */
816 struct fd_event *next = ev->next;
818 if (FD_ISSET(ev->fd, &r_fds))
819 flags |= EVENT_FD_READ;
820 if (FD_ISSET(ev->fd, &w_fds))
821 flags |= EVENT_FD_WRITE;
823 ev->handler(ev, flags);
827 if (FD_ISSET(listen_sock, &r_fds)) {
828 while (winbindd_num_clients() >
829 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
830 DEBUG(5,("winbindd: Exceeding %d client "
831 "connections, removing idle "
833 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
834 if (!remove_idle_client()) {
835 DEBUG(0,("winbindd: Exceeding %d "
836 "client connections, no idle "
837 "connection found\n",
838 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
842 /* new, non-privileged connection */
843 new_connection(listen_sock, False);
846 if (FD_ISSET(listen_priv_sock, &r_fds)) {
847 while (winbindd_num_clients() >
848 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
849 DEBUG(5,("winbindd: Exceeding %d client "
850 "connections, removing idle "
852 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
853 if (!remove_idle_client()) {
854 DEBUG(0,("winbindd: Exceeding %d "
855 "client connections, no idle "
856 "connection found\n",
857 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
861 /* new, privileged connection */
862 new_connection(listen_priv_sock, True);
868 winbindd_check_cache_size(time(NULL));
871 /* Check signal handling things */
878 DEBUG(3, ("got SIGHUP\n"));
880 msg_reload_services(MSG_SMB_CONF_UPDATED, pid_to_procid(0), NULL, 0);
885 print_winbindd_status();
894 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
895 winbind_child_died(pid);
902 int main(int argc, char **argv, char **envp)
905 static BOOL Fork = True;
906 static BOOL log_stdout = False;
907 static BOOL no_process_group = False;
908 struct poptOption long_options[] = {
910 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
911 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
912 { "no-process-group", 0, POPT_ARG_VAL, &no_process_group, True, "Don't create a new process group" },
913 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
914 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, True, "Disable caching" },
921 /* glibc (?) likes to print "User defined signal 1" and exit if a
922 SIGUSR[12] is received before a handler is installed */
924 CatchSignal(SIGUSR1, SIG_IGN);
925 CatchSignal(SIGUSR2, SIG_IGN);
927 fault_setup((void (*)(void *))fault_quit );
928 dump_core_setup("winbindd");
932 /* Initialise for running in non-root mode */
936 set_remote_machine_name("winbindd", False);
938 /* Set environment variable so we don't recursively call ourselves.
939 This may also be useful interactively. */
941 if ( !winbind_off() ) {
942 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
946 /* Initialise samba/rpc client stuff */
948 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
949 POPT_CONTEXT_KEEP_FIRST);
951 while ((opt = poptGetNextOpt(pc)) != -1) {
953 /* Don't become a daemon */
963 if (log_stdout && Fork) {
964 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
965 poptPrintUsage(pc, stderr, 0);
969 if (!override_logfile) {
970 pstr_sprintf(logfile, "%s/log.winbindd", dyn_LOGFILEBASE);
971 lp_set_logfile(logfile);
973 setup_logging("winbindd", log_stdout);
976 DEBUG(1, ("winbindd version %s started.\n", SAMBA_VERSION_STRING) );
977 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2004\n" ) );
979 if (!reload_services_file()) {
980 DEBUG(0, ("error opening config file\n"));
984 if (!directory_exist(lp_lockdir(), NULL)) {
985 mkdir(lp_lockdir(), 0755);
995 if (!secrets_init()) {
997 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1001 /* Enable netbios namecache */
1005 /* Winbind daemon initialisation */
1007 if ( ! NT_STATUS_IS_OK(idmap_init()) ) {
1008 DEBUG(1, ("Could not init idmap! - Sid/[UG]id mapping will not be available\n"));
1011 /* Unblock all signals we are interested in as they may have been
1012 blocked by the parent process. */
1014 BlockSignals(False, SIGINT);
1015 BlockSignals(False, SIGQUIT);
1016 BlockSignals(False, SIGTERM);
1017 BlockSignals(False, SIGUSR1);
1018 BlockSignals(False, SIGUSR2);
1019 BlockSignals(False, SIGHUP);
1020 BlockSignals(False, SIGCHLD);
1022 /* Setup signal handlers */
1024 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1025 CatchSignal(SIGQUIT, termination_handler);
1026 CatchSignal(SIGTERM, termination_handler);
1027 CatchSignal(SIGCHLD, sigchld_handler);
1029 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1031 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1032 CatchSignal(SIGHUP, sighup_handler);
1035 become_daemon(Fork, no_process_group);
1037 pidfile_create("winbindd");
1039 /* Ensure all cache and idmap caches are consistent
1040 before we startup. */
1042 if (winbindd_validate_cache()) {
1043 /* We have a bad cache, but luckily we
1044 just deleted it. Restart ourselves */
1046 /* Ensure we have no open low fd's. */
1047 for (i = 3; i < 100; i++) {
1050 return execve(argv[0], argv, envp);
1055 * If we're interactive we want to set our own process group for
1056 * signal management.
1058 if (interactive && !no_process_group)
1059 setpgid( (pid_t)0, (pid_t)0);
1064 /* Initialise messaging system */
1066 if (!message_init()) {
1067 DEBUG(0, ("unable to initialize messaging system\n"));
1071 /* React on 'smbcontrol winbindd reload-config' in the same way
1072 as to SIGHUP signal */
1073 message_register(MSG_SMB_CONF_UPDATED, msg_reload_services);
1074 message_register(MSG_SHUTDOWN, msg_shutdown);
1076 /* Handle online/offline messages. */
1077 message_register(MSG_WINBIND_OFFLINE,winbind_msg_offline);
1078 message_register(MSG_WINBIND_ONLINE,winbind_msg_online);
1079 message_register(MSG_WINBIND_ONLINESTATUS,winbind_msg_onlinestatus);
1081 poptFreeContext(pc);
1083 netsamlogon_cache_init(); /* Non-critical */
1085 if (!init_domain_list()) {
1086 DEBUG(0,("unable to initalize domain list\n"));
1092 smb_nscd_flush_user_cache();
1093 smb_nscd_flush_group_cache();
1095 /* Loop waiting for requests */
1100 trustdom_cache_shutdown();