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
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 BOOL opt_nocache = False;
28 BOOL opt_dual_daemon = False;
30 /* Reload configuration */
32 static BOOL reload_services_file(BOOL test)
39 pstrcpy(fname,lp_configfile());
40 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
41 pstrcpy(dyn_CONFIGFILE,fname);
47 ret = lp_load(dyn_CONFIGFILE,False,False,True);
57 /**************************************************************************** **
58 Prepare to dump a core file - carefully!
59 **************************************************************************** */
61 static BOOL dump_core(void)
65 pstrcpy( dname, lp_logfile() );
66 if ((p=strrchr(dname,'/')))
68 pstrcat( dname, "/corefiles" );
70 sys_chown( dname, getuid(), getgid() );
80 getrlimit( RLIMIT_CORE, &rlp );
81 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
82 setrlimit( RLIMIT_CORE, &rlp );
83 getrlimit( RLIMIT_CORE, &rlp );
84 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
89 DEBUG(0,("Dumping core in %s\n",dname));
95 /**************************************************************************** **
97 **************************************************************************** */
99 static void fault_quit(void)
106 static void winbindd_status(void)
108 struct winbindd_cli_state *tmp;
110 DEBUG(0, ("winbindd status:\n"));
112 /* Print client state information */
114 DEBUG(0, ("\t%d clients currently active\n", winbindd_num_clients()));
116 if (DEBUGLEVEL >= 2 && winbindd_num_clients()) {
117 DEBUG(2, ("\tclient list:\n"));
118 for(tmp = winbindd_client_list(); tmp; tmp = tmp->next) {
119 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
120 tmp->pid, tmp->sock, tmp->read_buf_len,
121 tmp->write_buf_len));
126 /* Print winbindd status to log file */
128 static void print_winbindd_status(void)
131 winbindd_idmap_status();
132 winbindd_cm_status();
135 /* Flush client cache */
137 static void flush_caches(void)
139 /* Clear cached user and group enumation info */
140 wcache_flush_cache();
143 /* Handle the signal by unlinking socket and exiting */
145 static void terminate(void)
149 winbindd_idmap_close();
151 /* Remove socket file */
152 snprintf(path, sizeof(path), "%s/%s",
153 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
158 static BOOL do_sigterm;
160 static void termination_handler(int signum)
166 static BOOL do_sigusr2;
168 static void sigusr2_handler(int signum)
174 static BOOL do_sighup;
176 static void sighup_handler(int signum)
182 struct dispatch_table {
183 enum winbindd_cmd cmd;
184 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
185 const char *winbindd_cmd_name;
188 static struct dispatch_table dispatch_table[] = {
192 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
193 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
195 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
196 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
197 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
199 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
201 /* Group functions */
203 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
204 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
205 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
206 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
207 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
208 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
210 /* PAM auth functions */
212 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
213 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
214 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
216 /* Enumeration functions */
218 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
219 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
220 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
221 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
223 /* SID related functions */
225 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
226 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
228 /* Lookup related functions */
230 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
231 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
232 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
233 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
237 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
238 { WINBINDD_PING, winbindd_ping, "PING" },
239 { WINBINDD_INFO, winbindd_info, "INFO" },
240 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
241 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
242 { WINBINDD_NETBIOS_NAME, winbindd_netbios_name, "NETBIOS_NAME" },
243 { WINBINDD_PRIV_PIPE_DIR, winbindd_priv_pipe_dir, "WINBINDD_PRIV_PIPE_DIR" },
247 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
248 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
252 { WINBINDD_NUM_CMDS, NULL, "NONE" }
255 static void process_request(struct winbindd_cli_state *state)
257 struct dispatch_table *table = dispatch_table;
259 /* Free response data - we may be interrupted and receive another
260 command before being able to send this data off. */
262 SAFE_FREE(state->response.extra_data);
264 ZERO_STRUCT(state->response);
266 state->response.result = WINBINDD_ERROR;
267 state->response.length = sizeof(struct winbindd_response);
269 /* Process command */
271 for (table = dispatch_table; table->fn; table++) {
272 if (state->request.cmd == table->cmd) {
273 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
274 state->response.result = table->fn(state);
280 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
282 /* In case extra data pointer is NULL */
284 if (!state->response.extra_data)
285 state->response.length = sizeof(struct winbindd_response);
288 /* Process a new connection by adding it to the client connection list */
290 static void new_connection(int listen_sock, BOOL privileged)
292 struct sockaddr_un sunaddr;
293 struct winbindd_cli_state *state;
297 /* Accept connection */
299 len = sizeof(sunaddr);
302 sock = accept(listen_sock, (struct sockaddr *)&sunaddr, &len);
303 } while (sock == -1 && errno == EINTR);
308 DEBUG(6,("accepted socket %d\n", sock));
310 /* Create new connection structure */
312 if ((state = (struct winbindd_cli_state *)
313 malloc(sizeof(*state))) == NULL)
319 state->last_access = time(NULL);
321 state->privileged = privileged;
323 /* Add to connection list */
325 winbindd_add_client(state);
328 /* Remove a client connection from client connection list */
330 static void remove_client(struct winbindd_cli_state *state)
332 /* It's a dead client - hold a funeral */
340 /* Free any getent state */
342 free_getent_state(state->getpwent_state);
343 free_getent_state(state->getgrent_state);
345 /* We may have some extra data that was not freed if the
346 client was killed unexpectedly */
348 SAFE_FREE(state->response.extra_data);
350 /* Remove from list and free */
352 winbindd_remove_client(state);
358 /* Shutdown client connection which has been idle for the longest time */
360 static BOOL remove_idle_client(void)
362 struct winbindd_cli_state *state, *remove_state = NULL;
363 time_t last_access = 0;
366 for (state = winbindd_client_list(); state; state = state->next) {
367 if (state->read_buf_len == 0 && state->write_buf_len == 0 &&
368 !state->getpwent_state && !state->getgrent_state) {
370 if (!last_access || state->last_access < last_access) {
371 last_access = state->last_access;
372 remove_state = state;
378 DEBUG(5,("Found %d idle client connections, shutting down sock %d, pid %u\n",
379 nidle, remove_state->sock, (unsigned int)remove_state->pid));
380 remove_client(remove_state);
387 /* Process a complete received packet from a client */
389 void winbind_process_packet(struct winbindd_cli_state *state)
391 /* Process request */
393 /* Ensure null termination of entire request */
394 state->request.null_term = '\0';
396 state->pid = state->request.pid;
398 process_request(state);
400 /* Update client state */
402 state->read_buf_len = 0;
403 state->write_buf_len = sizeof(struct winbindd_response);
405 /* we might need to send it to the dual daemon */
406 if (opt_dual_daemon) {
407 dual_send_request(state);
411 /* Read some data from a client connection */
413 void winbind_client_read(struct winbindd_cli_state *state)
419 n = sys_read(state->sock, state->read_buf_len +
420 (char *)&state->request,
421 sizeof(state->request) - state->read_buf_len);
423 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
425 /* Read failed, kill client */
427 if (n == -1 || n == 0) {
428 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
429 state->sock, state->pid,
430 (n == -1) ? strerror(errno) : "EOF"));
432 state->finished = True;
436 /* Update client state */
438 state->read_buf_len += n;
439 state->last_access = time(NULL);
442 /* Write some data to a client connection */
444 static void client_write(struct winbindd_cli_state *state)
449 /* Write some data */
451 if (!state->write_extra_data) {
453 /* Write response structure */
455 data = (char *)&state->response + sizeof(state->response) -
456 state->write_buf_len;
460 /* Write extra data */
462 data = (char *)state->response.extra_data +
463 state->response.length -
464 sizeof(struct winbindd_response) -
465 state->write_buf_len;
468 num_written = sys_write(state->sock, data, state->write_buf_len);
470 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
472 /* Write failed, kill cilent */
474 if (num_written == -1 || num_written == 0) {
476 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
477 state->sock, state->pid,
478 (num_written == -1) ? strerror(errno) : "EOF"));
480 state->finished = True;
482 SAFE_FREE(state->response.extra_data);
487 /* Update client state */
489 state->write_buf_len -= num_written;
490 state->last_access = time(NULL);
492 /* Have we written all data? */
494 if (state->write_buf_len == 0) {
496 /* Take care of extra data */
498 if (state->write_extra_data) {
500 SAFE_FREE(state->response.extra_data);
502 state->write_extra_data = False;
504 DEBUG(10,("client_write: client_write: complete response written.\n"));
506 } else if (state->response.length >
507 sizeof(struct winbindd_response)) {
509 /* Start writing extra data */
511 state->write_buf_len =
512 state->response.length -
513 sizeof(struct winbindd_response);
515 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
517 state->write_extra_data = True;
522 /* Process incoming clients on listen_sock. We use a tricky non-blocking,
523 non-forking, non-threaded model which allows us to handle many
524 simultaneous connections while remaining impervious to many denial of
527 static void process_loop(void)
529 /* We'll be doing this a lot */
532 struct winbindd_cli_state *state;
534 int maxfd, listen_sock, listen_priv_sock, selret;
535 struct timeval timeout;
537 /* Handle messages */
541 /* rescan the trusted domains list. This must be done
542 regularly to cope with transitive trusts */
543 rescan_trusted_domains(False);
545 /* Free up temporary memory */
548 main_loop_talloc_free();
550 /* Initialise fd lists for select() */
552 listen_sock = open_winbindd_socket();
553 listen_priv_sock = open_winbindd_priv_socket();
555 if (listen_sock == -1 || listen_priv_sock == -1) {
556 perror("open_winbind_socket");
560 maxfd = MAX(listen_sock, listen_priv_sock);
564 FD_SET(listen_sock, &r_fds);
565 FD_SET(listen_priv_sock, &r_fds);
567 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
570 if (opt_dual_daemon) {
571 maxfd = dual_select_setup(&w_fds, maxfd);
574 /* Set up client readers and writers */
576 state = winbindd_client_list();
580 /* Dispose of client connection if it is marked as
583 if (state->finished) {
584 struct winbindd_cli_state *next = state->next;
586 remove_client(state);
591 /* Select requires we know the highest fd used */
593 if (state->sock > maxfd)
596 /* Add fd for reading */
598 if (state->read_buf_len != sizeof(state->request))
599 FD_SET(state->sock, &r_fds);
601 /* Add fd for writing */
603 if (state->write_buf_len)
604 FD_SET(state->sock, &w_fds);
611 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
616 if ((selret == -1 && errno != EINTR) || selret == 0) {
618 /* Select error, something is badly wrong */
624 /* Create a new connection if listen_sock readable */
628 if (opt_dual_daemon) {
632 if (FD_ISSET(listen_sock, &r_fds)) {
633 while (winbindd_num_clients() > WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
634 DEBUG(5,("winbindd: Exceeding %d client connections, removing idle connection.\n",
635 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
636 if (!remove_idle_client()) {
637 DEBUG(0,("winbindd: Exceeding %d client connections, no idle connection found\n",
638 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
642 /* new, non-privileged connection */
643 new_connection(listen_sock, False);
646 if (FD_ISSET(listen_priv_sock, &r_fds)) {
647 while (winbindd_num_clients() > WINBINDD_MAX_SIMULTANEOUS_CLIENTS - 1) {
648 DEBUG(5,("winbindd: Exceeding %d client connections, removing idle connection.\n",
649 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
650 if (!remove_idle_client()) {
651 DEBUG(0,("winbindd: Exceeding %d client connections, no idle connection found\n",
652 WINBINDD_MAX_SIMULTANEOUS_CLIENTS));
656 /* new, privileged connection */
657 new_connection(listen_priv_sock, True);
660 /* Process activity on client connections */
662 for (state = winbindd_client_list(); state;
663 state = state->next) {
665 /* Data available for reading */
667 if (FD_ISSET(state->sock, &r_fds)) {
671 winbind_client_read(state);
674 * If we have the start of a
675 * packet, then check the
676 * length field to make sure
677 * the client's not talking
681 if (state->read_buf_len >= sizeof(uint32)
682 && *(uint32 *) &state->request != sizeof(state->request)) {
683 DEBUG(0,("process_loop: Invalid request size from pid %d: %d bytes sent, should be %d\n",
684 state->request.pid, *(uint32 *) &state->request, sizeof(state->request)));
686 remove_client(state);
690 /* A request packet might be
693 if (state->read_buf_len ==
694 sizeof(state->request)) {
695 winbind_process_packet(state);
699 /* Data available for writing */
701 if (FD_ISSET(state->sock, &w_fds))
707 winbindd_check_cache_size(time(NULL));
710 /* Check signal handling things */
717 DEBUG(3, ("got SIGHUP\n"));
719 /* Flush various caches */
722 reload_services_file(True);
727 print_winbindd_status();
735 struct winbindd_state server_state; /* Server state information */
737 int main(int argc, char **argv)
740 static BOOL interactive = False;
741 static BOOL Fork = True;
742 static BOOL log_stdout = False;
743 struct poptOption long_options[] = {
745 { "stdout", 'S', POPT_ARG_VAL, &log_stdout, True, "Log to stdout" },
746 { "foreground", 'F', POPT_ARG_VAL, &Fork, False, "Daemon in foreground mode" },
747 { "interactive", 'i', POPT_ARG_NONE, NULL, 'i', "Interactive mode" },
748 { "dual-daemon", 'B', POPT_ARG_VAL, &opt_dual_daemon, True, "Dual daemon mode" },
749 { "no-caching", 'n', POPT_ARG_VAL, &opt_nocache, False, "Disable caching" },
756 /* glibc (?) likes to print "User defined signal 1" and exit if a
757 SIGUSR[12] is received before a handler is installed */
759 CatchSignal(SIGUSR1, SIG_IGN);
760 CatchSignal(SIGUSR2, SIG_IGN);
762 fault_setup((void (*)(void *))fault_quit );
764 /* Initialise for running in non-root mode */
768 set_remote_machine_name("winbindd", False);
770 /* Set environment variable so we don't recursively call ourselves.
771 This may also be useful interactively. */
773 setenv(WINBINDD_DONT_ENV, "1", 1);
775 /* Initialise samba/rpc client stuff */
777 pc = poptGetContext("winbindd", argc, (const char **)argv, long_options,
778 POPT_CONTEXT_KEEP_FIRST);
780 while ((opt = poptGetNextOpt(pc)) != -1) {
782 /* Don't become a daemon */
792 if (log_stdout && Fork) {
793 printf("Can't log to stdout (-S) unless daemon is in foreground +(-F) or interactive (-i)\n");
794 poptPrintUsage(pc, stderr, 0);
798 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
799 lp_set_logfile(logfile);
800 setup_logging("winbindd", log_stdout);
803 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
804 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2003\n" ) );
806 if (!reload_services_file(False)) {
807 DEBUG(0, ("error opening config file\n"));
818 if (!secrets_init()) {
820 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
824 /* Enable netbios namecache */
828 /* Check winbindd parameters are valid */
830 ZERO_STRUCT(server_state);
832 if (!winbindd_param_init())
835 /* Winbind daemon initialisation */
837 if (!winbindd_idmap_init())
840 /* Unblock all signals we are interested in as they may have been
841 blocked by the parent process. */
843 BlockSignals(False, SIGINT);
844 BlockSignals(False, SIGQUIT);
845 BlockSignals(False, SIGTERM);
846 BlockSignals(False, SIGUSR1);
847 BlockSignals(False, SIGUSR2);
848 BlockSignals(False, SIGHUP);
850 /* Setup signal handlers */
852 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
853 CatchSignal(SIGQUIT, termination_handler);
854 CatchSignal(SIGTERM, termination_handler);
856 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
858 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
859 CatchSignal(SIGHUP, sighup_handler);
864 pidfile_create("winbindd");
868 * If we're interactive we want to set our own process group for
872 setpgid( (pid_t)0, (pid_t)0);
875 if (opt_dual_daemon) {
879 /* Initialise messaging system */
881 if (!message_init()) {
882 DEBUG(0, ("unable to initialise messaging system\n"));
887 /* Loop waiting for requests */
891 trustdom_cache_shutdown();
892 uni_group_cache_shutdown();