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)
64 const char *fname = lp_configfile();
66 if (file_exist(fname,NULL) && !strcsequal(fname,get_dyn_CONFIGFILE())) {
67 set_dyn_CONFIGFILE(fname);
72 ret = lp_load(get_dyn_CONFIGFILE(),False,False,True,True);
81 /**************************************************************************** **
83 **************************************************************************** */
85 static void fault_quit(void)
90 static void winbindd_status(void)
92 struct winbindd_cli_state *tmp;
94 DEBUG(0, ("winbindd status:\n"));
96 /* Print client state information */
98 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
100 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
101 DEBUG(2, ("\tclient list:\n"));
102 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
103 DEBUGADD(2, ("\t\tpid %lu, sock %d\n",
104 (unsigned long)tmp->pid, tmp->sock));
109 /* Print winbindd status to log file */
111 static void print_winbindd_status(void)
116 /* Flush client cache */
118 static void flush_caches(void)
120 /* We need to invalidate cached user list entries on a SIGHUP
121 otherwise cached access denied errors due to restrict anonymous
122 hang around until the sequence number changes. */
124 wcache_invalidate_cache();
127 /* Handle the signal by unlinking socket and exiting */
129 static void terminate(void)
133 /* Remove socket file */
134 if (asprintf(&path, "%s/%s",
135 get_winbind_pipe_dir(), WINBINDD_SOCKET_NAME) > 0) {
142 trustdom_cache_shutdown();
146 TALLOC_CTX *mem_ctx = talloc_init("end_description");
147 char *description = talloc_describe_all(mem_ctx);
149 DEBUG(3, ("tallocs left:\n%s\n", description));
150 talloc_destroy(mem_ctx);
157 static bool do_sigterm;
159 static void termination_handler(int signum)
162 sys_select_signal(signum);
165 static bool do_sigusr2;
167 static void sigusr2_handler(int signum)
170 sys_select_signal(SIGUSR2);
173 static bool do_sighup;
175 static void sighup_handler(int signum)
178 sys_select_signal(SIGHUP);
181 static bool do_sigchld;
183 static void sigchld_handler(int signum)
186 sys_select_signal(SIGCHLD);
189 /* React on 'smbcontrol winbindd reload-config' in the same way as on SIGHUP*/
190 static void msg_reload_services(struct messaging_context *msg,
193 struct server_id server_id,
196 /* Flush various caches */
198 reload_services_file();
201 /* React on 'smbcontrol winbindd shutdown' in the same way as on SIGTERM*/
202 static void msg_shutdown(struct messaging_context *msg,
205 struct server_id server_id,
212 static void winbind_msg_validate_cache(struct messaging_context *msg_ctx,
215 struct server_id server_id,
220 struct sigaction act;
221 struct sigaction oldact;
223 DEBUG(10, ("winbindd_msg_validate_cache: got validate-cache "
227 * call the validation code from a child:
228 * so we don't block the main winbindd and the validation
229 * code can safely use fork/waitpid...
232 child_pid = sys_fork();
234 if (child_pid == -1) {
235 DEBUG(1, ("winbind_msg_validate_cache: Could not fork: %s\n",
240 if (child_pid != 0) {
242 DEBUG(5, ("winbind_msg_validate_cache: child created with "
243 "pid %d.\n", child_pid));
249 /* install default SIGCHLD handler: validation code uses fork/waitpid */
251 act.sa_handler = SIG_DFL;
253 /* We *want* SIGALRM to interrupt a system call. */
254 act.sa_flags = SA_RESTART;
256 sigemptyset(&act.sa_mask);
257 sigaddset(&act.sa_mask,SIGCHLD);
258 sigaction(SIGCHLD,&act,&oldact);
260 ret = (uint8)winbindd_validate_cache_nobackup();
261 DEBUG(10, ("winbindd_msg_validata_cache: got return value %d\n", ret));
262 messaging_send_buf(msg_ctx, server_id, MSG_WINBIND_VALIDATE_CACHE, &ret,
267 static struct winbindd_dispatch_table {
268 enum winbindd_cmd cmd;
269 void (*fn)(struct winbindd_cli_state *state);
270 const char *winbindd_cmd_name;
271 } dispatch_table[] = {
275 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
276 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
278 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
279 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
280 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
282 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
283 { WINBINDD_GETUSERSIDS, winbindd_getusersids, "GETUSERSIDS" },
284 { WINBINDD_GETUSERDOMGROUPS, winbindd_getuserdomgroups,
285 "GETUSERDOMGROUPS" },
287 /* Group functions */
289 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
290 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
291 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
292 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
293 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
294 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
296 /* PAM auth functions */
298 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
299 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
300 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
301 { WINBINDD_PAM_LOGOFF, winbindd_pam_logoff, "PAM_LOGOFF" },
302 { WINBINDD_PAM_CHNG_PSWD_AUTH_CRAP, winbindd_pam_chng_pswd_auth_crap, "CHNG_PSWD_AUTH_CRAP" },
304 /* Enumeration functions */
306 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
307 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
308 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains,
310 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
312 /* SID related functions */
314 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
315 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
316 { WINBINDD_LOOKUPRIDS, winbindd_lookuprids, "LOOKUPRIDS" },
318 /* Lookup related functions */
320 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
321 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
322 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
323 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
324 #if 0 /* DISABLED until we fix the interface in Samba 3.0.26 --jerry */
325 { WINBINDD_SIDS_TO_XIDS, winbindd_sids_to_unixids, "SIDS_TO_XIDS" },
326 #endif /* end DISABLED */
327 { WINBINDD_ALLOCATE_UID, winbindd_allocate_uid, "ALLOCATE_UID" },
328 { WINBINDD_ALLOCATE_GID, winbindd_allocate_gid, "ALLOCATE_GID" },
329 { WINBINDD_SET_MAPPING, winbindd_set_mapping, "SET_MAPPING" },
330 { WINBINDD_SET_HWM, winbindd_set_hwm, "SET_HWMS" },
334 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
335 { WINBINDD_PING, winbindd_ping, "PING" },
336 { WINBINDD_INFO, winbindd_info, "INFO" },
337 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version,
338 "INTERFACE_VERSION" },
339 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
340 { WINBINDD_DOMAIN_INFO, winbindd_domain_info, "DOMAIN_INFO" },
341 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
342 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir,
343 "WINBINDD_PRIV_PIPE_DIR" },
344 { WINBINDD_GETDCNAME, winbindd_getdcname, "GETDCNAME" },
345 { WINBINDD_DSGETDCNAME, winbindd_dsgetdcname, "DSGETDCNAME" },
347 /* Credential cache access */
348 { WINBINDD_CCACHE_NTLMAUTH, winbindd_ccache_ntlm_auth, "NTLMAUTH" },
352 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
353 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
357 { WINBINDD_NUM_CMDS, NULL, "NONE" }
360 static void process_request(struct winbindd_cli_state *state)
362 struct winbindd_dispatch_table *table = dispatch_table;
364 /* Free response data - we may be interrupted and receive another
365 command before being able to send this data off. */
367 SAFE_FREE(state->response.extra_data.data);
369 ZERO_STRUCT(state->response);
371 state->response.result = WINBINDD_PENDING;
372 state->response.length = sizeof(struct winbindd_response);
374 state->mem_ctx = talloc_init("winbind request");
375 if (state->mem_ctx == NULL)
378 /* Remember who asked us. */
379 state->pid = state->request.pid;
381 /* Process command */
383 for (table = dispatch_table; table->fn; table++) {
384 if (state->request.cmd == table->cmd) {
385 DEBUG(10,("process_request: request fn %s\n",
386 table->winbindd_cmd_name ));
393 DEBUG(10,("process_request: unknown request fn number %d\n",
394 (int)state->request.cmd ));
395 request_error(state);
400 * A list of file descriptors being monitored by select in the main processing
401 * loop. fd_event->handler is called whenever the socket is readable/writable.
404 static struct fd_event *fd_events = NULL;
406 void add_fd_event(struct fd_event *ev)
408 struct fd_event *match;
410 /* only add unique fd_event structs */
412 for (match=fd_events; match; match=match->next ) {
414 SMB_ASSERT( match != ev );
421 DLIST_ADD(fd_events, ev);
424 void remove_fd_event(struct fd_event *ev)
426 DLIST_REMOVE(fd_events, ev);
430 * Handler for fd_events to complete a read/write request, set up by
431 * setup_async_read/setup_async_write.
434 static void rw_callback(struct fd_event *event, int flags)
439 todo = event->length - event->done;
441 if (event->flags & EVENT_FD_WRITE) {
442 SMB_ASSERT(flags == EVENT_FD_WRITE);
443 done = sys_write(event->fd,
444 &((char *)event->data)[event->done],
449 event->finished(event->private_data, False);
454 if (event->flags & EVENT_FD_READ) {
455 SMB_ASSERT(flags == EVENT_FD_READ);
456 done = sys_read(event->fd, &((char *)event->data)[event->done],
461 event->finished(event->private_data, False);
468 if (event->done == event->length) {
470 event->finished(event->private_data, True);
475 * Request an async read/write on a fd_event structure. (*finished) is called
476 * when the request is completed or an error had occurred.
479 void setup_async_read(struct fd_event *event, void *data, size_t length,
480 void (*finished)(void *private_data, bool success),
483 SMB_ASSERT(event->flags == 0);
485 event->length = length;
487 event->handler = rw_callback;
488 event->finished = finished;
489 event->private_data = private_data;
490 event->flags = EVENT_FD_READ;
493 void setup_async_write(struct fd_event *event, void *data, size_t length,
494 void (*finished)(void *private_data, bool success),
497 SMB_ASSERT(event->flags == 0);
499 event->length = length;
501 event->handler = rw_callback;
502 event->finished = finished;
503 event->private_data = private_data;
504 event->flags = EVENT_FD_WRITE;
508 * This is the main event loop of winbind requests. It goes through a
509 * state-machine of 3 read/write requests, 4 if you have extra data to send.
511 * An idle winbind client has a read request of 4 bytes outstanding,
512 * finalizing function is request_len_recv, checking the length. request_recv
513 * then processes the packet. The processing function then at some point has
514 * to call request_finished which schedules sending the response.
517 static void request_len_recv(void *private_data, bool success);
518 static void request_recv(void *private_data, bool success);
519 static void request_main_recv(void *private_data, bool success);
520 static void request_finished(struct winbindd_cli_state *state);
521 void request_finished_cont(void *private_data, bool success);
522 static void response_main_sent(void *private_data, bool success);
523 static void response_extra_sent(void *private_data, bool success);
525 static void response_extra_sent(void *private_data, bool success)
527 struct winbindd_cli_state *state =
528 talloc_get_type_abort(private_data, struct winbindd_cli_state);
530 if (state->mem_ctx != NULL) {
531 talloc_destroy(state->mem_ctx);
532 state->mem_ctx = NULL;
536 state->finished = True;
540 SAFE_FREE(state->request.extra_data.data);
541 SAFE_FREE(state->response.extra_data.data);
543 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
544 request_len_recv, state);
547 static void response_main_sent(void *private_data, bool success)
549 struct winbindd_cli_state *state =
550 talloc_get_type_abort(private_data, struct winbindd_cli_state);
553 state->finished = True;
557 if (state->response.length == sizeof(state->response)) {
558 if (state->mem_ctx != NULL) {
559 talloc_destroy(state->mem_ctx);
560 state->mem_ctx = NULL;
563 setup_async_read(&state->fd_event, &state->request,
564 sizeof(uint32), request_len_recv, state);
568 setup_async_write(&state->fd_event, state->response.extra_data.data,
569 state->response.length - sizeof(state->response),
570 response_extra_sent, state);
573 static void request_finished(struct winbindd_cli_state *state)
575 setup_async_write(&state->fd_event, &state->response,
576 sizeof(state->response), response_main_sent, state);
579 void request_error(struct winbindd_cli_state *state)
581 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
582 state->response.result = WINBINDD_ERROR;
583 request_finished(state);
586 void request_ok(struct winbindd_cli_state *state)
588 SMB_ASSERT(state->response.result == WINBINDD_PENDING);
589 state->response.result = WINBINDD_OK;
590 request_finished(state);
593 void request_finished_cont(void *private_data, bool success)
595 struct winbindd_cli_state *state =
596 talloc_get_type_abort(private_data, struct winbindd_cli_state);
601 request_error(state);
604 static void request_len_recv(void *private_data, bool success)
606 struct winbindd_cli_state *state =
607 talloc_get_type_abort(private_data, struct winbindd_cli_state);
610 state->finished = True;
614 if (*(uint32 *)(&state->request) != sizeof(state->request)) {
615 DEBUG(0,("request_len_recv: Invalid request size received: %d (expected %u)\n",
616 *(uint32_t *)(&state->request), (uint32_t)sizeof(state->request)));
617 state->finished = True;
621 setup_async_read(&state->fd_event, (uint32 *)(&state->request)+1,
622 sizeof(state->request) - sizeof(uint32),
623 request_main_recv, state);
626 static void request_main_recv(void *private_data, bool success)
628 struct winbindd_cli_state *state =
629 talloc_get_type_abort(private_data, struct winbindd_cli_state);
632 state->finished = True;
636 if (state->request.extra_len == 0) {
637 state->request.extra_data.data = NULL;
638 request_recv(state, True);
642 if ((!state->privileged) &&
643 (state->request.extra_len > WINBINDD_MAX_EXTRA_DATA)) {
644 DEBUG(3, ("Got request with %d bytes extra data on "
645 "unprivileged socket\n", (int)state->request.extra_len));
646 state->request.extra_data.data = NULL;
647 state->finished = True;
651 state->request.extra_data.data =
652 SMB_MALLOC_ARRAY(char, state->request.extra_len + 1);
654 if (state->request.extra_data.data == NULL) {
655 DEBUG(0, ("malloc failed\n"));
656 state->finished = True;
660 /* Ensure null termination */
661 state->request.extra_data.data[state->request.extra_len] = '\0';
663 setup_async_read(&state->fd_event, state->request.extra_data.data,
664 state->request.extra_len, request_recv, state);
667 static void request_recv(void *private_data, bool success)
669 struct winbindd_cli_state *state =
670 talloc_get_type_abort(private_data, struct winbindd_cli_state);
673 state->finished = True;
677 process_request(state);
680 /* Process a new connection by adding it to the client connection list */
682 static void new_connection(int listen_sock, bool privileged)
684 struct sockaddr_un sunaddr;
685 struct winbindd_cli_state *state;
689 /* Accept connection */
691 len = sizeof(sunaddr);
694 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
695 } while (sock == -1 && errno == EINTR);
700 DEBUG(6,("accepted socket %d\n", sock));
702 /* Create new connection structure */
704 if ((state = TALLOC_ZERO_P(NULL, struct winbindd_cli_state)) == NULL) {
711 state->last_access = time(NULL);
713 state->privileged = privileged;
715 state->fd_event.fd = state->sock;
716 state->fd_event.flags = 0;
717 add_fd_event(&state->fd_event);
719 setup_async_read(&state->fd_event, &state->request, sizeof(uint32),
720 request_len_recv, state);
722 /* Add to connection list */
724 winbindd_add_client(state);
727 /* Remove a client connection from client connection list */
729 static void remove_client(struct winbindd_cli_state *state)
731 /* It's a dead client - hold a funeral */
741 /* Free any getent state */
743 free_getent_state(state->getpwent_state);
744 free_getent_state(state->getgrent_state);
746 /* We may have some extra data that was not freed if the client was
747 killed unexpectedly */
749 SAFE_FREE(state->response.extra_data.data);
751 if (state->mem_ctx != NULL) {
752 talloc_destroy(state->mem_ctx);
753 state->mem_ctx = NULL;
756 remove_fd_event(&state->fd_event);
758 /* Remove from list and free */
760 winbindd_remove_client(state);
764 /* Shutdown client connection which has been idle for the longest time */
766 static bool remove_idle_client(void)
768 struct winbindd_cli_state *state, *remove_state = NULL;
769 time_t last_access = 0;
772 for (state = winbindd_client_list(); state; state = state->next) {
773 if (state->response.result != WINBINDD_PENDING &&
774 !state->getpwent_state && !state->getgrent_state) {
776 if (!last_access || state->last_access < last_access) {
777 last_access = state->last_access;
778 remove_state = state;
784 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
785 nidle, remove_state->sock, (unsigned int)remove_state->pid));
786 remove_client(remove_state);
793 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
794 non-forking, non-threaded model which allows us to handle many
795 simultaneous connections while remaining impervious to many denial of
798 static void process_loop(void)
800 struct winbindd_cli_state *state;
803 int maxfd, listen_sock, listen_priv_sock, selret;
804 struct timeval timeout, ev_timeout;
806 /* Open Sockets here to get stuff going ASAP */
807 listen_sock = open_winbindd_socket();
808 listen_priv_sock = open_winbindd_priv_socket();
810 if (listen_sock == -1 || listen_priv_sock == -1) {
811 perror("open_winbind_socket");
815 /* We'll be doing this a lot */
817 /* Handle messages */
819 message_dispatch(winbind_messaging_context());
821 run_events(winbind_event_context(), 0, NULL, NULL);
823 /* refresh the trusted domain cache */
825 rescan_trusted_domains();
827 /* Initialise fd lists for select() */
829 maxfd = MAX(listen_sock, listen_priv_sock);
833 FD_SET(listen_sock, &r_fds);
834 FD_SET(listen_priv_sock, &r_fds);
836 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
839 /* Check for any event timeouts. */
840 if (get_timed_events_timeout(winbind_event_context(), &ev_timeout)) {
841 timeout = timeval_min(&timeout, &ev_timeout);
844 /* Set up client readers and writers */
846 state = winbindd_client_list();
850 struct winbindd_cli_state *next = state->next;
852 /* Dispose of client connection if it is marked as
856 remove_client(state);
861 for (ev = fd_events; ev; ev = ev->next) {
862 if (ev->flags & EVENT_FD_READ) {
863 FD_SET(ev->fd, &r_fds);
864 maxfd = MAX(ev->fd, maxfd);
866 if (ev->flags & EVENT_FD_WRITE) {
867 FD_SET(ev->fd, &w_fds);
868 maxfd = MAX(ev->fd, maxfd);
874 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
881 if (errno == EINTR) {
885 /* Select error, something is badly wrong */
895 struct fd_event *next = ev->next;
897 if (FD_ISSET(ev->fd, &r_fds))
898 flags |= EVENT_FD_READ;
899 if (FD_ISSET(ev->fd, &w_fds))
900 flags |= EVENT_FD_WRITE;
902 ev->handler(ev, flags);
906 if (FD_ISSET(listen_sock, &r_fds)) {
907 while (winbindd_num_clients() >
908 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
909 DEBUG(5,("winbindd: Exceeding %d client "
910 "connections, removing idle "
912 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
913 if (!remove_idle_client()) {
914 DEBUG(0,("winbindd: Exceeding %d "
915 "client connections, no idle "
916 "connection found\n",
917 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
921 /* new, non-privileged connection */
922 new_connection(listen_sock, False);
925 if (FD_ISSET(listen_priv_sock, &r_fds)) {
926 while (winbindd_num_clients() >
927 WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
928 DEBUG(5,("winbindd: Exceeding %d client "
929 "connections, removing idle "
931 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
932 if (!remove_idle_client()) {
933 DEBUG(0,("winbindd: Exceeding %d "
934 "client connections, no idle "
935 "connection found\n",
936 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
940 /* new, privileged connection */
941 new_connection(listen_priv_sock, True);
947 winbindd_check_cache_size(time(NULL));
950 /* Check signal handling things */
957 DEBUG(3, ("got SIGHUP\n"));
960 reload_services_file();
966 print_winbindd_status();
975 while ((pid = sys_waitpid(-1, NULL, WNOHANG)) > 0) {
976 winbind_child_died(pid);
983 int main(int argc, char **argv, char **envp)
985 static bool is_daemon = False;
986 static bool Fork = True;
987 static bool log_stdout = False;
988 static bool no_process_group = False;
992 OPT_NO_PROCESS_GROUP,
995 struct poptOption long_options[] = {
997 { "stdout", 'S', POPT_ARG_NONE, NULL, OPT_LOG_STDOUT, "Log to stdout" },
998 { "foreground", 'F', POPT_ARG_NONE, NULL, OPT_FORK, "Daemon in foreground mode" },
999 { "no-process-group", 0, POPT_ARG_NONE, NULL, OPT_NO_PROCESS_GROUP, "Don't create a new process group" },
1000 { "daemon", 'D', POPT_ARG_NONE, NULL, OPT_DAEMON, "Become a daemon (default)" },
1001 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
1002 { "no-caching", 'n', POPT_ARG_NONE, NULL, 'n', "Disable caching" },
1008 TALLOC_CTX *frame = talloc_stackframe();
1010 /* glibc (?) likes to print "User defined signal 1" and exit if a
1011 SIGUSR[12] is received before a handler is installed */
1013 CatchSignal(SIGUSR1, SIG_IGN);
1014 CatchSignal(SIGUSR2, SIG_IGN);
1016 fault_setup((void (*)(void *))fault_quit );
1017 dump_core_setup("winbindd");
1021 /* Initialise for running in non-root mode */
1025 set_remote_machine_name("winbindd", False);
1027 /* Set environment variable so we don't recursively call ourselves.
1028 This may also be useful interactively. */
1030 if ( !winbind_off() ) {
1031 DEBUG(0,("Failed to disable recusive winbindd calls. Exiting.\n"));
1035 /* Initialise samba/rpc client stuff */
1037 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options, 0);
1039 while ((opt = poptGetNextOpt(pc)) != -1) {
1041 /* Don't become a daemon */
1053 case OPT_NO_PROCESS_GROUP:
1054 no_process_group = true;
1056 case OPT_LOG_STDOUT:
1063 d_fprintf(stderr, "\nInvalid option %s: %s\n\n",
1064 poptBadOption(pc, 0), poptStrerror(opt));
1065 poptPrintUsage(pc, stderr, 0);
1070 if (is_daemon && interactive) {
1071 d_fprintf(stderr,"\nERROR: "
1072 "Option -i|--interactive is not allowed together with -D|--daemon\n\n");
1073 poptPrintUsage(pc, stderr, 0);
1077 if (log_stdout && Fork) {
1078 d_fprintf(stderr, "\nERROR: "
1079 "Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n\n");
1080 poptPrintUsage(pc, stderr, 0);
1084 poptFreeContext(pc);
1086 if (!override_logfile) {
1087 char *logfile = NULL;
1088 if (asprintf(&logfile,"%s/log.winbindd",
1089 get_dyn_LOGFILEBASE()) > 0) {
1090 lp_set_logfile(logfile);
1094 setup_logging("winbindd", log_stdout);
1097 DEBUG(0,("winbindd version %s started.\n", SAMBA_VERSION_STRING));
1098 DEBUGADD(0,("%s\n", COPYRIGHT_STARTUP_MESSAGE));
1100 if (!reload_services_file()) {
1101 DEBUG(0, ("error opening config file\n"));
1105 if (!directory_exist(lp_lockdir(), NULL)) {
1106 mkdir(lp_lockdir(), 0755);
1116 if (!secrets_init()) {
1118 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
1122 /* Enable netbios namecache */
1126 /* Winbind daemon initialisation */
1128 if ( ! NT_STATUS_IS_OK(idmap_init_cache()) ) {
1129 DEBUG(1, ("Could not init idmap cache!\n"));
1132 /* Unblock all signals we are interested in as they may have been
1133 blocked by the parent process. */
1135 BlockSignals(False, SIGINT);
1136 BlockSignals(False, SIGQUIT);
1137 BlockSignals(False, SIGTERM);
1138 BlockSignals(False, SIGUSR1);
1139 BlockSignals(False, SIGUSR2);
1140 BlockSignals(False, SIGHUP);
1141 BlockSignals(False, SIGCHLD);
1143 /* Setup signal handlers */
1145 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
1146 CatchSignal(SIGQUIT, termination_handler);
1147 CatchSignal(SIGTERM, termination_handler);
1148 CatchSignal(SIGCHLD, sigchld_handler);
1150 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
1152 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
1153 CatchSignal(SIGHUP, sighup_handler);
1156 become_daemon(Fork, no_process_group);
1158 pidfile_create("winbindd");
1162 * If we're interactive we want to set our own process group for
1163 * signal management.
1165 if (interactive && !no_process_group)
1166 setpgid( (pid_t)0, (pid_t)0);
1171 /* Initialise messaging system */
1173 if (winbind_messaging_context() == NULL) {
1174 DEBUG(0, ("unable to initialize messaging system\n"));
1179 * Ensure all cache and idmap caches are consistent
1180 * before we startup.
1182 if (winbindd_validate_cache() < 0) {
1183 DEBUG(0, ("corrupted tdb found, trying to restore backup\n"));
1186 /* Initialize cache (ensure version is correct). */
1187 if (!initialize_winbindd_cache()) {
1191 /* React on 'smbcontrol winbindd reload-config' in the same way
1192 as to SIGHUP signal */
1193 messaging_register(winbind_messaging_context(), NULL,
1194 MSG_SMB_CONF_UPDATED, msg_reload_services);
1195 messaging_register(winbind_messaging_context(), NULL,
1196 MSG_SHUTDOWN, msg_shutdown);
1198 /* Handle online/offline messages. */
1199 messaging_register(winbind_messaging_context(), NULL,
1200 MSG_WINBIND_OFFLINE, winbind_msg_offline);
1201 messaging_register(winbind_messaging_context(), NULL,
1202 MSG_WINBIND_ONLINE, winbind_msg_online);
1203 messaging_register(winbind_messaging_context(), NULL,
1204 MSG_WINBIND_ONLINESTATUS, winbind_msg_onlinestatus);
1206 messaging_register(winbind_messaging_context(), NULL,
1207 MSG_DUMP_EVENT_LIST, winbind_msg_dump_event_list);
1209 messaging_register(winbind_messaging_context(), NULL,
1210 MSG_WINBIND_VALIDATE_CACHE,
1211 winbind_msg_validate_cache);
1213 netsamlogon_cache_init(); /* Non-critical */
1215 /* clear the cached list of trusted domains */
1219 if (!init_domain_list()) {
1220 DEBUG(0,("unable to initalize domain list\n"));
1225 init_locator_child();
1227 smb_nscd_flush_user_cache();
1228 smb_nscd_flush_group_cache();
1230 /* Loop waiting for requests */
1234 frame = talloc_stackframe();