2 Unix SMB/Netbios implementation.
5 Winbind daemon for ntdom nss module
7 Copyright (C) by Tim Potter 2000, 2001
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 extern pstring debugf;
28 /* List of all connected clients */
30 struct winbindd_cli_state *client_list;
31 static int num_clients;
34 /* Reload configuration */
36 static BOOL reload_services_file(BOOL test)
43 pstrcpy(fname,lp_configfile());
44 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
45 pstrcpy(dyn_CONFIGFILE,fname);
51 ret = lp_load(dyn_CONFIGFILE,False,False,True);
61 /**************************************************************************** **
62 Prepare to dump a core file - carefully!
63 **************************************************************************** */
65 static BOOL dump_core(void)
69 pstrcpy( dname, debugf );
70 if ((p=strrchr(dname,'/')))
72 pstrcat( dname, "/corefiles" );
74 sys_chown( dname, getuid(), getgid() );
84 getrlimit( RLIMIT_CORE, &rlp );
85 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
86 setrlimit( RLIMIT_CORE, &rlp );
87 getrlimit( RLIMIT_CORE, &rlp );
88 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
93 DEBUG(0,("Dumping core in %s\n",dname));
99 /**************************************************************************** **
101 **************************************************************************** */
103 static void fault_quit(void)
110 static void winbindd_status(void)
112 struct winbindd_cli_state *tmp;
114 DEBUG(0, ("winbindd status:\n"));
116 /* Print client state information */
118 DEBUG(0, ("\t%d clients currently active\n", num_clients));
120 if (DEBUGLEVEL >= 2 && num_clients) {
121 DEBUG(2, ("\tclient list:\n"));
122 for(tmp = client_list; tmp; tmp = tmp->next) {
123 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
124 tmp->pid, tmp->sock, tmp->read_buf_len,
125 tmp->write_buf_len));
130 /* Print winbindd status to log file */
132 static void print_winbindd_status(void)
135 winbindd_idmap_status();
136 winbindd_cm_status();
139 /* Flush client cache */
141 static void flush_caches(void)
143 /* Clear cached user and group enumation info */
144 wcache_flush_cache();
147 /* Handle the signal by unlinking socket and exiting */
149 static void terminate(void)
153 /* Remove socket file */
154 snprintf(path, sizeof(path), "%s/%s",
155 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
160 static BOOL do_sigterm;
162 static void termination_handler(int signum)
167 static BOOL do_sigusr1;
169 static void sigusr1_handler(int signum)
174 static BOOL do_sighup;
176 static void sighup_handler(int signum)
181 /* Create winbindd socket */
183 static int create_sock(void)
185 struct sockaddr_un sunaddr;
191 /* Create the socket directory or reuse the existing one */
193 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
195 if (errno == ENOENT) {
197 /* Create directory */
199 if (mkdir(WINBINDD_SOCKET_DIR, 0755) == -1) {
200 DEBUG(0, ("error creating socket directory "
201 "%s: %s\n", WINBINDD_SOCKET_DIR,
208 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
209 WINBINDD_SOCKET_DIR, strerror(errno)));
215 /* Check ownership and permission on existing directory */
217 if (!S_ISDIR(st.st_mode)) {
218 DEBUG(0, ("socket directory %s isn't a directory\n",
219 WINBINDD_SOCKET_DIR));
223 if ((st.st_uid != sec_initial_uid()) ||
224 ((st.st_mode & 0777) != 0755)) {
225 DEBUG(0, ("invalid permissions on socket directory "
226 "%s\n", WINBINDD_SOCKET_DIR));
231 /* Create the socket file */
233 old_umask = umask(0);
235 sock = socket(AF_UNIX, SOCK_STREAM, 0);
242 snprintf(path, sizeof(path), "%s/%s",
243 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
246 memset(&sunaddr, 0, sizeof(sunaddr));
247 sunaddr.sun_family = AF_UNIX;
248 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
250 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
251 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
258 if (listen(sock, 5) == -1) {
259 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
273 struct dispatch_table {
274 enum winbindd_cmd cmd;
275 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
276 char *winbindd_cmd_name;
279 static struct dispatch_table dispatch_table[] = {
283 { WINBINDD_GETPWNAM_FROM_USER, winbindd_getpwnam_from_user, "GETPWNAM_FROM_USER" },
284 { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid, "GETPWNAM_FROM_UID" },
286 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
287 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
288 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
290 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
292 /* Group functions */
294 { WINBINDD_GETGRNAM_FROM_GROUP, winbindd_getgrnam_from_group, "GETGRNAM_FROM_GROUP" },
295 { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid, "GETGRNAM_FROM_GID" },
296 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
297 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
298 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
300 /* PAM auth functions */
302 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
303 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
304 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
306 /* Enumeration functions */
308 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
309 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
310 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
312 /* SID related functions */
314 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
315 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
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_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
322 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
326 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
330 { WINBINDD_NUM_CMDS, NULL, "NONE" }
333 static void process_request(struct winbindd_cli_state *state)
335 struct dispatch_table *table = dispatch_table;
337 /* Free response data - we may be interrupted and receive another
338 command before being able to send this data off. */
340 SAFE_FREE(state->response.extra_data);
342 ZERO_STRUCT(state->response);
344 state->response.result = WINBINDD_ERROR;
345 state->response.length = sizeof(struct winbindd_response);
347 /* Process command */
349 for (table = dispatch_table; table->fn; table++) {
350 if (state->request.cmd == table->cmd) {
351 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
352 state->response.result = table->fn(state);
358 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
360 /* In case extra data pointer is NULL */
362 if (!state->response.extra_data)
363 state->response.length = sizeof(struct winbindd_response);
366 /* Process a new connection by adding it to the client connection list */
368 static void new_connection(int accept_sock)
370 struct sockaddr_un sunaddr;
371 struct winbindd_cli_state *state;
375 /* Accept connection */
377 len = sizeof(sunaddr);
380 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
381 } while (sock == -1 && errno == EINTR);
386 DEBUG(6,("accepted socket %d\n", sock));
388 /* Create new connection structure */
390 if ((state = (struct winbindd_cli_state *)
391 malloc(sizeof(*state))) == NULL)
397 /* Add to connection list */
399 DLIST_ADD(client_list, state);
403 /* Remove a client connection from client connection list */
405 static void remove_client(struct winbindd_cli_state *state)
407 /* It's a dead client - hold a funeral */
415 /* Free any getent state */
417 free_getent_state(state->getpwent_state);
418 free_getent_state(state->getgrent_state);
420 /* We may have some extra data that was not freed if the
421 client was killed unexpectedly */
423 SAFE_FREE(state->response.extra_data);
425 /* Remove from list and free */
427 DLIST_REMOVE(client_list, state);
433 /* Process a complete received packet from a client */
435 static void process_packet(struct winbindd_cli_state *state)
437 /* Process request */
439 state->pid = state->request.pid;
441 process_request(state);
443 /* Update client state */
445 state->read_buf_len = 0;
446 state->write_buf_len = sizeof(struct winbindd_response);
449 /* Read some data from a client connection */
451 static void client_read(struct winbindd_cli_state *state)
458 n = read(state->sock, state->read_buf_len + (char *)&state->request,
459 sizeof(state->request) - state->read_buf_len);
460 } while (n == -1 && errno == EINTR);
462 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n,
463 sizeof(state->request) - n - state->read_buf_len ));
465 /* Read failed, kill client */
467 if (n == -1 || n == 0) {
468 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
469 state->sock, state->pid,
470 (n == -1) ? strerror(errno) : "EOF"));
472 state->finished = True;
476 /* Update client state */
478 state->read_buf_len += n;
481 /* Write some data to a client connection */
483 static void client_write(struct winbindd_cli_state *state)
488 /* Write some data */
490 if (!state->write_extra_data) {
492 /* Write response structure */
494 data = (char *)&state->response + sizeof(state->response) -
495 state->write_buf_len;
499 /* Write extra data */
501 data = (char *)state->response.extra_data +
502 state->response.length -
503 sizeof(struct winbindd_response) -
504 state->write_buf_len;
508 num_written = write(state->sock, data, state->write_buf_len);
509 } while (num_written == -1 && errno == EINTR);
511 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
513 /* Write failed, kill cilent */
515 if (num_written == -1 || num_written == 0) {
517 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
518 state->sock, state->pid,
519 (num_written == -1) ? strerror(errno) : "EOF"));
521 state->finished = True;
523 SAFE_FREE(state->response.extra_data);
528 /* Update client state */
530 state->write_buf_len -= num_written;
532 /* Have we written all data? */
534 if (state->write_buf_len == 0) {
536 /* Take care of extra data */
538 if (state->write_extra_data) {
540 SAFE_FREE(state->response.extra_data);
542 state->write_extra_data = False;
544 DEBUG(10,("client_write: client_write: complete response written.\n"));
546 } else if (state->response.length >
547 sizeof(struct winbindd_response)) {
549 /* Start writing extra data */
551 state->write_buf_len =
552 state->response.length -
553 sizeof(struct winbindd_response);
555 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
557 state->write_extra_data = True;
562 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
563 non-forking, non-threaded model which allows us to handle many
564 simultaneous connections while remaining impervious to many denial of
567 static void process_loop(int accept_sock)
569 /* We'll be doing this a lot */
572 struct winbindd_cli_state *state;
574 int maxfd = accept_sock, selret;
575 struct timeval timeout;
577 /* Free up temporary memory */
581 /* Initialise fd lists for select() */
585 FD_SET(accept_sock, &r_fds);
587 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
590 /* Set up client readers and writers */
596 /* Dispose of client connection if it is marked as
599 if (state->finished) {
600 struct winbindd_cli_state *next = state->next;
602 remove_client(state);
607 /* Select requires we know the highest fd used */
609 if (state->sock > maxfd)
612 /* Add fd for reading */
614 if (state->read_buf_len != sizeof(state->request))
615 FD_SET(state->sock, &r_fds);
617 /* Add fd for writing */
619 if (state->write_buf_len)
620 FD_SET(state->sock, &w_fds);
627 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
632 if ((selret == -1 && errno != EINTR) || selret == 0) {
634 /* Select error, something is badly wrong */
640 /* Create a new connection if accept_sock readable */
644 if (FD_ISSET(accept_sock, &r_fds))
645 new_connection(accept_sock);
647 /* Process activity on client connections */
649 for (state = client_list; state; state = state->next) {
651 /* Data available for reading */
653 if (FD_ISSET(state->sock, &r_fds)) {
660 /* JRA - currently there's no length field in the request... */
662 * If we have the start of a
663 * packet, then check the
664 * length field to make sure
665 * the client's not talking
669 if (state->read_buf_len >= sizeof(int)
670 && *(int *) state->buf != sizeof(state->request)) {
672 struct winbindd_cli_state *rem_state = state;
674 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
675 *(int *) rem_state->buf, sizeof(rem_state->request) ));
678 remove_client(rem_state);
683 /* A request packet might be
686 if (state->read_buf_len ==
687 sizeof(state->request)) {
688 process_packet(state);
692 /* Data available for writing */
694 if (FD_ISSET(state->sock, &w_fds))
699 /* Check signal handling things */
706 /* Flush winbindd cache */
709 reload_services_file(True);
714 print_winbindd_status();
722 struct winbindd_state server_state; /* Server state information */
724 int main(int argc, char **argv)
726 extern pstring global_myname;
728 BOOL interactive = False;
729 int opt, new_debuglevel = -1;
731 /* glibc (?) likes to print "User defined signal 1" and exit if a
732 SIGUSR1 is received before a handler is installed */
734 CatchSignal(SIGUSR1, SIG_IGN);
736 fault_setup((void (*)(void *))fault_quit );
737 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", dyn_LOGFILEBASE);
739 /* Initialise for running in non-root mode */
743 /* Set environment variable so we don't recursively call ourselves.
744 This may also be useful interactively. */
746 SETENV(WINBINDD_DONT_ENV, "1", 1);
748 /* Initialise samba/rpc client stuff */
750 while ((opt = getopt(argc, argv, "id:s:n")) != EOF) {
753 /* Don't become a daemon */
758 /* disable cacheing */
763 /* Run with specified debug level */
765 new_debuglevel = atoi(optarg);
768 /* Load a different smb.conf file */
770 pstrcpy(dyn_CONFIGFILE,optarg);
774 printf("Unknown option %c\n", (char)opt);
779 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", dyn_LOGFILEBASE);
780 setup_logging("winbindd", interactive);
783 if (!*global_myname) {
786 fstrcpy(global_myname, myhostname());
787 p = strchr(global_myname, '.');
793 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
794 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
796 if (!reload_services_file(False)) {
797 DEBUG(0, ("error opening config file\n"));
801 if (new_debuglevel != -1)
802 DEBUGLEVEL = new_debuglevel;
811 /* Get list of domains we look up requests for. This includes the
812 domain which we are a member of as well as any trusted
817 ZERO_STRUCT(server_state);
819 /* Winbind daemon initialisation */
821 if (!winbindd_param_init())
824 if (!winbindd_idmap_init())
827 /* Unblock all signals we are interested in as they may have been
828 blocked by the parent process. */
830 BlockSignals(False, SIGINT);
831 BlockSignals(False, SIGQUIT);
832 BlockSignals(False, SIGTERM);
833 BlockSignals(False, SIGUSR1);
834 BlockSignals(False, SIGHUP);
836 /* Setup signal handlers */
838 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
839 CatchSignal(SIGQUIT, termination_handler);
840 CatchSignal(SIGTERM, termination_handler);
842 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
844 CatchSignal(SIGUSR1, sigusr1_handler); /* Debugging sigs */
845 CatchSignal(SIGHUP, sighup_handler);
847 /* Create UNIX domain socket */
849 if ((accept_sock = create_sock()) == -1) {
850 DEBUG(0, ("failed to create socket\n"));
854 /* Loop waiting for requests */
856 process_loop(accept_sock);