Fixed delete on close bug. Added core dump code to winbindd.
[Samba/ekacnet.git] / source / nsswitch / winbindd.c
blobc1402dc8fb67d02e7e6fb37bc8f2ef79c5f44b2a
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
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.
24 #include "winbindd.h"
26 extern pstring debugf;
28 /* List of all connected clients */
30 struct winbindd_cli_state *client_list;
31 static int num_clients;
33 /* Reload configuration */
35 static BOOL reload_services_file(BOOL test)
37 BOOL ret;
39 if (lp_loaded()) {
40 pstring fname;
42 pstrcpy(fname,lp_configfile());
43 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
44 pstrcpy(dyn_CONFIGFILE,fname);
45 test = False;
49 reopen_logs();
50 ret = lp_load(dyn_CONFIGFILE,False,False,True);
52 reopen_logs();
53 load_interfaces();
55 return(ret);
58 #if DUMP_CORE
60 /**************************************************************************** **
61 Prepare to dump a core file - carefully!
62 **************************************************************************** */
64 static BOOL dump_core(void)
66 char *p;
67 pstring dname;
68 pstrcpy( dname, debugf );
69 if ((p=strrchr(dname,'/')))
70 *p=0;
71 pstrcat( dname, "/corefiles" );
72 mkdir( dname, 0700 );
73 sys_chown( dname, getuid(), getgid() );
74 chmod( dname, 0700 );
75 if ( chdir(dname) )
76 return( False );
77 umask( ~(0700) );
79 #ifdef HAVE_GETRLIMIT
80 #ifdef RLIMIT_CORE
82 struct rlimit rlp;
83 getrlimit( RLIMIT_CORE, &rlp );
84 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
85 setrlimit( RLIMIT_CORE, &rlp );
86 getrlimit( RLIMIT_CORE, &rlp );
87 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
89 #endif
90 #endif
92 DEBUG(0,("Dumping core in %s\n",dname));
93 abort();
94 return( True );
95 } /* dump_core */
96 #endif
98 /**************************************************************************** **
99 Handle a fault..
100 **************************************************************************** */
102 static void fault_quit(void)
104 #if DUMP_CORE
105 dump_core();
106 #endif
109 static void winbindd_status(void)
111 struct winbindd_cli_state *tmp;
113 DEBUG(0, ("winbindd status:\n"));
115 /* Print client state information */
117 DEBUG(0, ("\t%d clients currently active\n", num_clients));
119 if (DEBUGLEVEL >= 2 && num_clients) {
120 DEBUG(2, ("\tclient list:\n"));
121 for(tmp = client_list; tmp; tmp = tmp->next) {
122 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
123 tmp->pid, tmp->sock, tmp->read_buf_len,
124 tmp->write_buf_len));
129 /* Print winbindd status to log file */
131 static void print_winbindd_status(void)
133 winbindd_status();
134 winbindd_idmap_status();
135 winbindd_cache_status();
136 winbindd_cm_status();
139 /* Flush client cache */
141 static void flush_caches(void)
143 /* Clear cached user and group enumation info */
145 winbindd_flush_cache();
148 /* Handle the signal by unlinking socket and exiting */
150 static void terminate(void)
152 pstring path;
154 /* Remove socket file */
155 snprintf(path, sizeof(path), "%s/%s",
156 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
157 unlink(path);
158 exit(0);
161 static BOOL do_sigterm;
163 static void termination_handler(int signum)
165 do_sigterm = True;
168 static BOOL do_sigusr1;
170 static void sigusr1_handler(int signum)
172 do_sigusr1 = True;
175 static BOOL do_sighup;
177 static void sighup_handler(int signum)
179 do_sighup = True;
182 /* Create winbindd socket */
184 static int create_sock(void)
186 struct sockaddr_un sunaddr;
187 struct stat st;
188 int sock;
189 mode_t old_umask;
190 pstring path;
192 /* Create the socket directory or reuse the existing one */
194 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
196 if (errno == ENOENT) {
198 /* Create directory */
200 if (mkdir(WINBINDD_SOCKET_DIR, 0755) == -1) {
201 DEBUG(0, ("error creating socket directory "
202 "%s: %s\n", WINBINDD_SOCKET_DIR,
203 strerror(errno)));
204 return -1;
207 } else {
209 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
210 WINBINDD_SOCKET_DIR, strerror(errno)));
211 return -1;
214 } else {
216 /* Check ownership and permission on existing directory */
218 if (!S_ISDIR(st.st_mode)) {
219 DEBUG(0, ("socket directory %s isn't a directory\n",
220 WINBINDD_SOCKET_DIR));
221 return -1;
224 if ((st.st_uid != sec_initial_uid()) ||
225 ((st.st_mode & 0777) != 0755)) {
226 DEBUG(0, ("invalid permissions on socket directory "
227 "%s\n", WINBINDD_SOCKET_DIR));
228 return -1;
232 /* Create the socket file */
234 old_umask = umask(0);
236 sock = socket(AF_UNIX, SOCK_STREAM, 0);
238 if (sock == -1) {
239 perror("socket");
240 return -1;
243 snprintf(path, sizeof(path), "%s/%s",
244 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
246 unlink(path);
247 memset(&sunaddr, 0, sizeof(sunaddr));
248 sunaddr.sun_family = AF_UNIX;
249 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
251 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
252 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
253 path,
254 strerror(errno)));
255 close(sock);
256 return -1;
259 if (listen(sock, 5) == -1) {
260 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
261 path,
262 strerror(errno)));
263 close(sock);
264 return -1;
267 umask(old_umask);
269 /* Success! */
271 return sock;
274 struct dispatch_table {
275 enum winbindd_cmd cmd;
276 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
277 char *winbindd_cmd_name;
280 static struct dispatch_table dispatch_table[] = {
282 /* User functions */
284 { WINBINDD_GETPWNAM_FROM_USER, winbindd_getpwnam_from_user, "GETPWNAM_FROM_USER" },
285 { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid, "GETPWNAM_FROM_UID" },
287 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
288 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
289 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
291 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
293 /* Group functions */
295 { WINBINDD_GETGRNAM_FROM_GROUP, winbindd_getgrnam_from_group, "GETGRNAM_FROM_GROUP" },
296 { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid, "GETGRNAM_FROM_GID" },
297 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
298 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
299 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
301 /* PAM auth functions */
303 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
304 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
305 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
307 /* Enumeration functions */
309 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
310 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
311 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
313 /* SID related functions */
315 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
316 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
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_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
323 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
325 /* Miscellaneous */
327 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
329 /* End of list */
331 { WINBINDD_NUM_CMDS, NULL, "NONE" }
334 static void process_request(struct winbindd_cli_state *state)
336 struct dispatch_table *table = dispatch_table;
338 /* Free response data - we may be interrupted and receive another
339 command before being able to send this data off. */
341 SAFE_FREE(state->response.extra_data);
343 ZERO_STRUCT(state->response);
345 state->response.result = WINBINDD_ERROR;
346 state->response.length = sizeof(struct winbindd_response);
348 /* Process command */
350 for (table = dispatch_table; table->fn; table++) {
351 if (state->request.cmd == table->cmd) {
352 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
353 state->response.result = table->fn(state);
354 break;
358 if (!table->fn)
359 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
361 /* In case extra data pointer is NULL */
363 if (!state->response.extra_data)
364 state->response.length = sizeof(struct winbindd_response);
367 /* Process a new connection by adding it to the client connection list */
369 static void new_connection(int accept_sock)
371 struct sockaddr_un sunaddr;
372 struct winbindd_cli_state *state;
373 socklen_t len;
374 int sock;
376 /* Accept connection */
378 len = sizeof(sunaddr);
380 do {
381 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
382 } while (sock == -1 && errno == EINTR);
384 if (sock == -1)
385 return;
387 DEBUG(6,("accepted socket %d\n", sock));
389 /* Create new connection structure */
391 if ((state = (struct winbindd_cli_state *)
392 malloc(sizeof(*state))) == NULL)
393 return;
395 ZERO_STRUCTP(state);
396 state->sock = sock;
398 /* Add to connection list */
400 DLIST_ADD(client_list, state);
401 num_clients++;
404 /* Remove a client connection from client connection list */
406 static void remove_client(struct winbindd_cli_state *state)
408 /* It's a dead client - hold a funeral */
410 if (state != NULL) {
412 /* Close socket */
414 close(state->sock);
416 /* Free any getent state */
418 free_getent_state(state->getpwent_state);
419 free_getent_state(state->getgrent_state);
421 /* We may have some extra data that was not freed if the
422 client was killed unexpectedly */
424 SAFE_FREE(state->response.extra_data);
426 /* Remove from list and free */
428 DLIST_REMOVE(client_list, state);
429 SAFE_FREE(state);
430 num_clients--;
434 /* Process a complete received packet from a client */
436 static void process_packet(struct winbindd_cli_state *state)
438 /* Process request */
440 state->pid = state->request.pid;
442 process_request(state);
444 /* Update client state */
446 state->read_buf_len = 0;
447 state->write_buf_len = sizeof(struct winbindd_response);
450 /* Read some data from a client connection */
452 static void client_read(struct winbindd_cli_state *state)
454 int n;
456 /* Read data */
458 do {
459 n = read(state->sock, state->read_buf_len + (char *)&state->request,
460 sizeof(state->request) - state->read_buf_len);
461 } while (n == -1 && errno == EINTR);
463 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n,
464 sizeof(state->request) - n - state->read_buf_len ));
466 /* Read failed, kill client */
468 if (n == -1 || n == 0) {
469 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
470 state->sock, state->pid,
471 (n == -1) ? strerror(errno) : "EOF"));
473 state->finished = True;
474 return;
477 /* Update client state */
479 state->read_buf_len += n;
482 /* Write some data to a client connection */
484 static void client_write(struct winbindd_cli_state *state)
486 char *data;
487 int num_written;
489 /* Write some data */
491 if (!state->write_extra_data) {
493 /* Write response structure */
495 data = (char *)&state->response + sizeof(state->response) -
496 state->write_buf_len;
498 } else {
500 /* Write extra data */
502 data = (char *)state->response.extra_data +
503 state->response.length -
504 sizeof(struct winbindd_response) -
505 state->write_buf_len;
508 do {
509 num_written = write(state->sock, data, state->write_buf_len);
510 } while (num_written == -1 && errno == EINTR);
512 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
514 /* Write failed, kill cilent */
516 if (num_written == -1 || num_written == 0) {
518 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
519 state->sock, state->pid,
520 (num_written == -1) ? strerror(errno) : "EOF"));
522 state->finished = True;
524 SAFE_FREE(state->response.extra_data);
526 return;
529 /* Update client state */
531 state->write_buf_len -= num_written;
533 /* Have we written all data? */
535 if (state->write_buf_len == 0) {
537 /* Take care of extra data */
539 if (state->write_extra_data) {
541 SAFE_FREE(state->response.extra_data);
543 state->write_extra_data = False;
545 DEBUG(10,("client_write: client_write: complete response written.\n"));
547 } else if (state->response.length >
548 sizeof(struct winbindd_response)) {
550 /* Start writing extra data */
552 state->write_buf_len =
553 state->response.length -
554 sizeof(struct winbindd_response);
556 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
558 state->write_extra_data = True;
563 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
564 non-forking, non-threaded model which allows us to handle many
565 simultaneous connections while remaining impervious to many denial of
566 service attacks. */
568 static void process_loop(int accept_sock)
570 /* We'll be doing this a lot */
572 while (1) {
573 struct winbindd_cli_state *state;
574 fd_set r_fds, w_fds;
575 int maxfd = accept_sock, selret;
576 struct timeval timeout;
578 /* Free up temporary memory */
580 lp_talloc_free();
582 /* Initialise fd lists for select() */
584 FD_ZERO(&r_fds);
585 FD_ZERO(&w_fds);
586 FD_SET(accept_sock, &r_fds);
588 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
589 timeout.tv_usec = 0;
591 /* Set up client readers and writers */
593 state = client_list;
595 while (state) {
597 /* Dispose of client connection if it is marked as
598 finished */
600 if (state->finished) {
601 struct winbindd_cli_state *next = state->next;
603 remove_client(state);
604 state = next;
605 continue;
608 /* Select requires we know the highest fd used */
610 if (state->sock > maxfd)
611 maxfd = state->sock;
613 /* Add fd for reading */
615 if (state->read_buf_len != sizeof(state->request))
616 FD_SET(state->sock, &r_fds);
618 /* Add fd for writing */
620 if (state->write_buf_len)
621 FD_SET(state->sock, &w_fds);
623 state = state->next;
626 /* Call select */
628 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
630 if (selret == 0)
631 continue;
633 if ((selret == -1 && errno != EINTR) || selret == 0) {
635 /* Select error, something is badly wrong */
637 perror("select");
638 exit(1);
641 /* Create a new connection if accept_sock readable */
643 if (selret > 0) {
645 if (FD_ISSET(accept_sock, &r_fds))
646 new_connection(accept_sock);
648 /* Process activity on client connections */
650 for (state = client_list; state; state = state->next) {
652 /* Data available for reading */
654 if (FD_ISSET(state->sock, &r_fds)) {
656 /* Read data */
658 client_read(state);
660 #if 0
661 /* JRA - currently there's no length field in the request... */
663 * If we have the start of a
664 * packet, then check the
665 * length field to make sure
666 * the client's not talking
667 * Mock Swedish.
670 if (state->read_buf_len >= sizeof(int)
671 && *(int *) state->buf != sizeof(state->request)) {
673 struct winbindd_cli_state *rem_state = state;
675 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
676 *(int *) rem_state->buf, sizeof(rem_state->request) ));
678 state = state_next;
679 remove_client(rem_state);
680 continue;
682 #endif
684 /* A request packet might be
685 complete */
687 if (state->read_buf_len ==
688 sizeof(state->request)) {
689 process_packet(state);
693 /* Data available for writing */
695 if (FD_ISSET(state->sock, &w_fds))
696 client_write(state);
700 /* Check signal handling things */
702 if (do_sigterm)
703 terminate();
705 if (do_sighup) {
707 /* Flush winbindd cache */
709 flush_caches();
710 reload_services_file(True);
711 do_sighup = False;
714 if (do_sigusr1) {
715 print_winbindd_status();
716 do_sigusr1 = False;
721 /* Main function */
723 struct winbindd_state server_state; /* Server state information */
725 int main(int argc, char **argv)
727 extern pstring global_myname;
728 int accept_sock;
729 BOOL interactive = False;
730 int opt, new_debuglevel = -1;
732 /* glibc (?) likes to print "User defined signal 1" and exit if a
733 SIGUSR1 is received before a handler is installed */
735 CatchSignal(SIGUSR1, SIG_IGN);
737 fault_setup((void (*)(void *))fault_quit );
738 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", dyn_LOGFILEBASE);
740 /* Initialise for running in non-root mode */
742 sec_init();
744 /* Set environment variable so we don't recursively call ourselves.
745 This may also be useful interactively. */
747 SETENV(WINBINDD_DONT_ENV, "1", 1);
749 /* Initialise samba/rpc client stuff */
751 while ((opt = getopt(argc, argv, "id:s:")) != EOF) {
752 switch (opt) {
754 /* Don't become a daemon */
756 case 'i':
757 interactive = True;
758 break;
760 /* Run with specified debug level */
762 case 'd':
763 new_debuglevel = atoi(optarg);
764 break;
766 /* Load a different smb.conf file */
768 case 's':
769 pstrcpy(dyn_CONFIGFILE,optarg);
770 break;
772 default:
773 printf("Unknown option %c\n", (char)opt);
774 exit(1);
778 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", dyn_LOGFILEBASE);
779 setup_logging("winbindd", interactive);
780 reopen_logs();
782 if (!*global_myname) {
783 char *p;
785 fstrcpy(global_myname, myhostname());
786 p = strchr(global_myname, '.');
787 if (p)
788 *p = 0;
792 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
793 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
795 if (!reload_services_file(False)) {
796 DEBUG(0, ("error opening config file\n"));
797 exit(1);
800 if (new_debuglevel != -1)
801 DEBUGLEVEL = new_debuglevel;
803 if (!interactive)
804 become_daemon();
806 load_interfaces();
808 secrets_init();
810 /* Get list of domains we look up requests for. This includes the
811 domain which we are a member of as well as any trusted
812 domains. */
814 get_domain_info();
816 ZERO_STRUCT(server_state);
818 /* Winbind daemon initialisation */
820 if (!winbindd_param_init())
821 return 1;
823 if (!winbindd_idmap_init())
824 return 1;
826 winbindd_cache_init();
828 /* Unblock all signals we are interested in as they may have been
829 blocked by the parent process. */
831 BlockSignals(False, SIGINT);
832 BlockSignals(False, SIGQUIT);
833 BlockSignals(False, SIGTERM);
834 BlockSignals(False, SIGUSR1);
835 BlockSignals(False, SIGHUP);
837 /* Setup signal handlers */
839 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
840 CatchSignal(SIGQUIT, termination_handler);
841 CatchSignal(SIGTERM, termination_handler);
843 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
845 CatchSignal(SIGUSR1, sigusr1_handler); /* Debugging sigs */
846 CatchSignal(SIGHUP, sighup_handler);
848 /* Create UNIX domain socket */
850 if ((accept_sock = create_sock()) == -1) {
851 DEBUG(0, ("failed to create socket\n"));
852 return 1;
855 /* Loop waiting for requests */
857 process_loop(accept_sock);
859 return 0;