2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000, 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* List of all connected clients */
27 struct winbindd_cli_state *client_list;
28 static int num_clients;
31 /* Reload configuration */
33 static BOOL reload_services_file(BOOL test)
41 pstrcpy(fname,lp_configfile());
42 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
43 pstrcpy(dyn_CONFIGFILE,fname);
48 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
49 lp_set_logfile(logfile);
52 ret = lp_load(dyn_CONFIGFILE,False,False,True);
54 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
55 lp_set_logfile(logfile);
65 /**************************************************************************** **
66 Prepare to dump a core file - carefully!
67 **************************************************************************** */
69 static BOOL dump_core(void)
73 pstrcpy( dname, lp_logfile() );
74 if ((p=strrchr(dname,'/')))
76 pstrcat( dname, "/corefiles" );
78 sys_chown( dname, getuid(), getgid() );
88 getrlimit( RLIMIT_CORE, &rlp );
89 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
90 setrlimit( RLIMIT_CORE, &rlp );
91 getrlimit( RLIMIT_CORE, &rlp );
92 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
97 DEBUG(0,("Dumping core in %s\n",dname));
103 /**************************************************************************** **
105 **************************************************************************** */
107 static void fault_quit(void)
114 static void winbindd_status(void)
116 struct winbindd_cli_state *tmp;
118 DEBUG(0, ("winbindd status:\n"));
120 /* Print client state information */
122 DEBUG(0, ("\t%d clients currently active\n", num_clients));
124 if (DEBUGLEVEL >= 2 && num_clients) {
125 DEBUG(2, ("\tclient list:\n"));
126 for(tmp = client_list; tmp; tmp = tmp->next) {
127 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
128 tmp->pid, tmp->sock, tmp->read_buf_len,
129 tmp->write_buf_len));
134 /* Print winbindd status to log file */
136 static void print_winbindd_status(void)
139 winbindd_idmap_status();
140 winbindd_cm_status();
143 /* Flush client cache */
145 static void flush_caches(void)
147 /* Clear cached user and group enumation info */
148 wcache_flush_cache();
151 /* Handle the signal by unlinking socket and exiting */
153 static void terminate(void)
157 winbindd_idmap_close();
159 /* Remove socket file */
160 snprintf(path, sizeof(path), "%s/%s",
161 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
166 static BOOL do_sigterm;
168 static void termination_handler(int signum)
174 static BOOL do_sigusr2;
176 static void sigusr2_handler(int signum)
182 static BOOL do_sighup;
184 static void sighup_handler(int signum)
190 /* Create winbindd socket */
192 static int create_sock(void)
194 return create_pipe_sock( WINBINDD_SOCKET_DIR,
195 WINBINDD_SOCKET_NAME, 0755);
198 struct dispatch_table {
199 enum winbindd_cmd cmd;
200 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
201 char *winbindd_cmd_name;
204 static struct dispatch_table dispatch_table[] = {
208 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
209 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
211 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
212 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
213 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
215 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
217 /* Group functions */
219 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
220 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
221 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
222 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
223 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
225 /* PAM auth functions */
227 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
228 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
229 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
231 /* Enumeration functions */
233 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
234 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
235 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
236 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
238 /* SID related functions */
240 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
241 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
243 /* Lookup related functions */
245 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
246 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
247 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
248 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
252 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
253 { WINBINDD_PING, winbindd_ping, "PING" },
254 { WINBINDD_INFO, winbindd_info, "INFO" },
255 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
256 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
260 { WINBINDD_NUM_CMDS, NULL, "NONE" }
263 static void process_request(struct winbindd_cli_state *state)
265 struct dispatch_table *table = dispatch_table;
267 /* Free response data - we may be interrupted and receive another
268 command before being able to send this data off. */
270 SAFE_FREE(state->response.extra_data);
272 ZERO_STRUCT(state->response);
274 state->response.result = WINBINDD_ERROR;
275 state->response.length = sizeof(struct winbindd_response);
277 /* Process command */
279 for (table = dispatch_table; table->fn; table++) {
280 if (state->request.cmd == table->cmd) {
281 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
282 state->response.result = table->fn(state);
288 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
290 /* In case extra data pointer is NULL */
292 if (!state->response.extra_data)
293 state->response.length = sizeof(struct winbindd_response);
296 /* Process a new connection by adding it to the client connection list */
298 static void new_connection(int accept_sock)
300 struct sockaddr_un sunaddr;
301 struct winbindd_cli_state *state;
305 /* Accept connection */
307 len = sizeof(sunaddr);
310 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
311 } while (sock == -1 && errno == EINTR);
316 DEBUG(6,("accepted socket %d\n", sock));
318 /* Create new connection structure */
320 if ((state = (struct winbindd_cli_state *)
321 malloc(sizeof(*state))) == NULL)
327 /* Add to connection list */
329 DLIST_ADD(client_list, state);
333 /* Remove a client connection from client connection list */
335 static void remove_client(struct winbindd_cli_state *state)
337 /* It's a dead client - hold a funeral */
345 /* Free any getent state */
347 free_getent_state(state->getpwent_state);
348 free_getent_state(state->getgrent_state);
350 /* We may have some extra data that was not freed if the
351 client was killed unexpectedly */
353 SAFE_FREE(state->response.extra_data);
355 /* Remove from list and free */
357 DLIST_REMOVE(client_list, state);
363 /* Process a complete received packet from a client */
365 static void process_packet(struct winbindd_cli_state *state)
367 /* Process request */
369 state->pid = state->request.pid;
371 process_request(state);
373 /* Update client state */
375 state->read_buf_len = 0;
376 state->write_buf_len = sizeof(struct winbindd_response);
379 /* Read some data from a client connection */
381 static void client_read(struct winbindd_cli_state *state)
389 n = read(state->sock, state->read_buf_len +
390 (char *)&state->request,
391 sizeof(state->request) - state->read_buf_len);
393 } while (n == -1 && errno == EINTR);
395 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
397 /* Read failed, kill client */
399 if (n == -1 || n == 0) {
400 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
401 state->sock, state->pid,
402 (n == -1) ? strerror(errno) : "EOF"));
404 state->finished = True;
408 /* Update client state */
410 state->read_buf_len += n;
413 /* Write some data to a client connection */
415 static void client_write(struct winbindd_cli_state *state)
420 /* Write some data */
422 if (!state->write_extra_data) {
424 /* Write response structure */
426 data = (char *)&state->response + sizeof(state->response) -
427 state->write_buf_len;
431 /* Write extra data */
433 data = (char *)state->response.extra_data +
434 state->response.length -
435 sizeof(struct winbindd_response) -
436 state->write_buf_len;
440 num_written = write(state->sock, data, state->write_buf_len);
441 } while (num_written == -1 && errno == EINTR);
443 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
445 /* Write failed, kill cilent */
447 if (num_written == -1 || num_written == 0) {
449 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
450 state->sock, state->pid,
451 (num_written == -1) ? strerror(errno) : "EOF"));
453 state->finished = True;
455 SAFE_FREE(state->response.extra_data);
460 /* Update client state */
462 state->write_buf_len -= num_written;
464 /* Have we written all data? */
466 if (state->write_buf_len == 0) {
468 /* Take care of extra data */
470 if (state->write_extra_data) {
472 SAFE_FREE(state->response.extra_data);
474 state->write_extra_data = False;
476 DEBUG(10,("client_write: client_write: complete response written.\n"));
478 } else if (state->response.length >
479 sizeof(struct winbindd_response)) {
481 /* Start writing extra data */
483 state->write_buf_len =
484 state->response.length -
485 sizeof(struct winbindd_response);
487 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
489 state->write_extra_data = True;
494 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
495 non-forking, non-threaded model which allows us to handle many
496 simultaneous connections while remaining impervious to many denial of
499 static void process_loop(int accept_sock)
501 /* We'll be doing this a lot */
504 struct winbindd_cli_state *state;
506 int maxfd = accept_sock, selret;
507 struct timeval timeout;
509 /* Free up temporary memory */
513 /* Initialise fd lists for select() */
517 FD_SET(accept_sock, &r_fds);
519 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
522 /* Set up client readers and writers */
528 /* Dispose of client connection if it is marked as
531 if (state->finished) {
532 struct winbindd_cli_state *next = state->next;
534 remove_client(state);
539 /* Select requires we know the highest fd used */
541 if (state->sock > maxfd)
544 /* Add fd for reading */
546 if (state->read_buf_len != sizeof(state->request))
547 FD_SET(state->sock, &r_fds);
549 /* Add fd for writing */
551 if (state->write_buf_len)
552 FD_SET(state->sock, &w_fds);
559 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
564 if ((selret == -1 && errno != EINTR) || selret == 0) {
566 /* Select error, something is badly wrong */
572 /* Create a new connection if accept_sock readable */
576 if (FD_ISSET(accept_sock, &r_fds))
577 new_connection(accept_sock);
579 /* Process activity on client connections */
581 for (state = client_list; state; state = state->next) {
583 /* Data available for reading */
585 if (FD_ISSET(state->sock, &r_fds)) {
592 * If we have the start of a
593 * packet, then check the
594 * length field to make sure
595 * the client's not talking
599 if (state->read_buf_len >= sizeof(uint32)
600 && *(uint32 *) &state->request != sizeof(state->request)) {
601 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
602 *(uint32 *) &state->request, sizeof(state->request)));
604 remove_client(state);
608 /* A request packet might be
611 if (state->read_buf_len ==
612 sizeof(state->request)) {
613 process_packet(state);
617 /* Data available for writing */
619 if (FD_ISSET(state->sock, &w_fds))
624 /* Check signal handling things */
631 /* Flush winbindd cache */
634 reload_services_file(True);
639 print_winbindd_status();
647 struct winbindd_state server_state; /* Server state information */
649 int main(int argc, char **argv)
651 extern pstring global_myname;
652 extern fstring global_myworkgroup;
653 extern BOOL append_log;
656 BOOL interactive = False;
657 int opt, new_debuglevel = -1;
659 /* glibc (?) likes to print "User defined signal 1" and exit if a
660 SIGUSR2 is received before a handler is installed */
662 CatchSignal(SIGUSR2, SIG_IGN);
664 fault_setup((void (*)(void *))fault_quit );
666 /* Append to log file by default as we are a single process daemon
671 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
672 lp_set_logfile(logfile);
674 /* Initialise for running in non-root mode */
678 /* Set environment variable so we don't recursively call ourselves.
679 This may also be useful interactively. */
681 SETENV(WINBINDD_DONT_ENV, "1", 1);
683 /* Initialise samba/rpc client stuff */
685 while ((opt = getopt(argc, argv, "id:s:n")) != EOF) {
688 /* Don't become a daemon */
693 /* disable cacheing */
698 /* Run with specified debug level */
700 new_debuglevel = atoi(optarg);
703 /* Load a different smb.conf file */
705 pstrcpy(dyn_CONFIGFILE,optarg);
709 printf("Unknown option %c\n", (char)opt);
714 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
715 lp_set_logfile(logfile);
716 setup_logging("winbindd", interactive);
719 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
720 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
722 if (!reload_services_file(False)) {
723 DEBUG(0, ("error opening config file\n"));
727 pidfile_create("winbindd");
731 if (!*global_myname) {
734 fstrcpy(global_myname, myhostname());
735 p = strchr(global_myname, '.');
740 fstrcpy(global_myworkgroup, lp_workgroup());
742 if (new_debuglevel != -1)
743 DEBUGLEVEL = new_debuglevel;
750 * If we're interactive we want to set our own process group for
754 setpgid( (pid_t)0, (pid_t)0);
761 /* Get list of domains we look up requests for. This includes the
762 domain which we are a member of as well as any trusted
767 ZERO_STRUCT(server_state);
769 /* Winbind daemon initialisation */
771 if (!winbindd_param_init())
774 if (!winbindd_idmap_init())
777 /* Unblock all signals we are interested in as they may have been
778 blocked by the parent process. */
780 BlockSignals(False, SIGINT);
781 BlockSignals(False, SIGQUIT);
782 BlockSignals(False, SIGTERM);
783 BlockSignals(False, SIGUSR2);
784 BlockSignals(False, SIGHUP);
786 /* Setup signal handlers */
788 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
789 CatchSignal(SIGQUIT, termination_handler);
790 CatchSignal(SIGTERM, termination_handler);
792 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
793 CatchSignal(SIGUSR1, SIG_IGN); /* Samba messages */
795 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
796 CatchSignal(SIGHUP, sighup_handler);
798 /* Create UNIX domain socket */
800 if ((accept_sock = create_sock()) == -1) {
801 DEBUG(0, ("failed to create socket\n"));
805 /* Loop waiting for requests */
807 process_loop(accept_sock);
809 uni_group_cache_shutdown();