removed two unneeded files after Richard backed out these changes.
[Samba.git] / source / nsswitch / winbindd.c
blob65361b571c45c010115d11c670bab179815754de
1 /*
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.
23 #include "winbindd.h"
25 /* List of all connected clients */
27 struct winbindd_cli_state *client_list;
28 static int num_clients;
29 BOOL opt_nocache;
31 pstring servicesf = CONFIGFILE;
33 /* Reload configuration */
35 static BOOL reload_services_file(BOOL test)
37 BOOL ret;
38 pstring logfile;
40 if (lp_loaded()) {
41 pstring fname;
43 pstrcpy(fname,lp_configfile());
44 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
45 pstrcpy(servicesf,fname);
46 test = False;
50 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", LOGFILEBASE);
51 lp_set_logfile(logfile);
53 if (!reopen_logs()) {
54 fprintf(stderr, "Could not open logfile: %s\n", logfile);
55 fprintf(stderr, "Continuing in the hope that that is OK\n");
58 ret = lp_load(servicesf,False,False,True);
60 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", LOGFILEBASE);
61 lp_set_logfile(logfile);
63 if (!reopen_logs()) {
64 fprintf(stderr, "Could not open logfile: %s\n", logfile);
65 fprintf(stderr, "Continuing in the hope that that is OK\n");
68 load_interfaces();
70 return(ret);
73 #if DUMP_CORE
75 /**************************************************************************** **
76 Prepare to dump a core file - carefully!
77 **************************************************************************** */
79 static BOOL dump_core(void)
81 char *p;
82 pstring dname;
83 pstrcpy( dname, lp_logfile() );
84 if ((p=strrchr(dname,'/')))
85 *p=0;
86 pstrcat( dname, "/corefiles" );
87 mkdir( dname, 0700 );
88 sys_chown( dname, getuid(), getgid() );
89 chmod( dname, 0700 );
90 if ( chdir(dname) )
91 return( False );
92 umask( ~(0700) );
94 #ifdef HAVE_GETRLIMIT
95 #ifdef RLIMIT_CORE
97 struct rlimit rlp;
98 getrlimit( RLIMIT_CORE, &rlp );
99 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
100 setrlimit( RLIMIT_CORE, &rlp );
101 getrlimit( RLIMIT_CORE, &rlp );
102 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
104 #endif
105 #endif
107 DEBUG(0,("Dumping core in %s\n",dname));
108 abort();
109 return( True );
110 } /* dump_core */
111 #endif
113 /**************************************************************************** **
114 Handle a fault..
115 **************************************************************************** */
117 static void fault_quit(void)
119 #if DUMP_CORE
120 dump_core();
121 #endif
124 static void winbindd_status(void)
126 struct winbindd_cli_state *tmp;
128 DEBUG(0, ("winbindd status:\n"));
130 /* Print client state information */
132 DEBUG(0, ("\t%d clients currently active\n", num_clients));
134 if (DEBUGLEVEL >= 2 && num_clients) {
135 DEBUG(2, ("\tclient list:\n"));
136 for(tmp = client_list; tmp; tmp = tmp->next) {
137 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
138 tmp->pid, tmp->sock, tmp->read_buf_len,
139 tmp->write_buf_len));
144 /* Print winbindd status to log file */
146 static void print_winbindd_status(void)
148 winbindd_status();
149 winbindd_idmap_status();
150 winbindd_cm_status();
153 /* Flush client cache */
155 static void flush_caches(void)
157 /* Clear cached user and group enumation info */
158 wcache_flush_cache();
161 /* Handle the signal by unlinking socket and exiting */
163 static void terminate(void)
165 pstring path;
167 winbindd_idmap_close();
169 /* Remove socket file */
170 snprintf(path, sizeof(path), "%s/%s",
171 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
172 unlink(path);
173 exit(0);
176 static BOOL do_sigterm;
178 static void termination_handler(int signum)
180 do_sigterm = True;
181 sys_select_signal();
184 static BOOL do_sigusr2;
186 static void sigusr2_handler(int signum)
188 do_sigusr2 = True;
189 sys_select_signal();
192 static BOOL do_sighup;
194 static void sighup_handler(int signum)
196 do_sighup = True;
197 sys_select_signal();
200 /* Create winbindd socket */
202 static int create_sock(void)
204 return create_pipe_sock( WINBINDD_SOCKET_DIR,
205 WINBINDD_SOCKET_NAME, 0755);
208 struct dispatch_table {
209 enum winbindd_cmd cmd;
210 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
211 char *winbindd_cmd_name;
214 static struct dispatch_table dispatch_table[] = {
216 /* User functions */
218 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
219 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
221 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
222 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
223 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
225 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
227 /* Group functions */
229 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
230 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
231 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
232 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
233 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
235 /* PAM auth functions */
237 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
238 #ifdef WITH_WINBIND_AUTH_CRAP
239 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
240 #endif
241 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
243 /* Enumeration functions */
245 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
246 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
247 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
248 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
250 /* SID related functions */
252 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
253 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
255 /* Lookup related functions */
257 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
258 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
259 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
260 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
262 /* Miscellaneous */
264 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
265 { WINBINDD_PING, winbindd_ping, "PING" },
266 { WINBINDD_INFO, winbindd_info, "INFO" },
267 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
268 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
270 /* WINS functions */
272 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
273 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
275 /* End of list */
277 { WINBINDD_NUM_CMDS, NULL, "NONE" }
280 static void process_request(struct winbindd_cli_state *state)
282 struct dispatch_table *table = dispatch_table;
284 /* Free response data - we may be interrupted and receive another
285 command before being able to send this data off. */
287 SAFE_FREE(state->response.extra_data);
289 ZERO_STRUCT(state->response);
291 state->response.result = WINBINDD_ERROR;
292 state->response.length = sizeof(struct winbindd_response);
294 /* Process command */
296 for (table = dispatch_table; table->fn; table++) {
297 if (state->request.cmd == table->cmd) {
298 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
299 state->response.result = table->fn(state);
300 break;
304 if (!table->fn)
305 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
307 /* In case extra data pointer is NULL */
309 if (!state->response.extra_data)
310 state->response.length = sizeof(struct winbindd_response);
313 /* Process a new connection by adding it to the client connection list */
315 static void new_connection(int accept_sock)
317 struct sockaddr_un sunaddr;
318 struct winbindd_cli_state *state;
319 socklen_t len;
320 int sock;
322 /* Accept connection */
324 len = sizeof(sunaddr);
326 do {
327 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
328 } while (sock == -1 && errno == EINTR);
330 if (sock == -1)
331 return;
333 DEBUG(6,("accepted socket %d\n", sock));
335 /* Create new connection structure */
337 if ((state = (struct winbindd_cli_state *)
338 malloc(sizeof(*state))) == NULL)
339 return;
341 ZERO_STRUCTP(state);
342 state->sock = sock;
344 /* Add to connection list */
346 DLIST_ADD(client_list, state);
347 num_clients++;
350 /* Remove a client connection from client connection list */
352 static void remove_client(struct winbindd_cli_state *state)
354 /* It's a dead client - hold a funeral */
356 if (state != NULL) {
358 /* Close socket */
360 close(state->sock);
362 /* Free any getent state */
364 free_getent_state(state->getpwent_state);
365 free_getent_state(state->getgrent_state);
367 /* We may have some extra data that was not freed if the
368 client was killed unexpectedly */
370 SAFE_FREE(state->response.extra_data);
372 /* Remove from list and free */
374 DLIST_REMOVE(client_list, state);
375 SAFE_FREE(state);
376 num_clients--;
380 /* Process a complete received packet from a client */
382 static void process_packet(struct winbindd_cli_state *state)
384 /* Process request */
386 state->pid = state->request.pid;
388 process_request(state);
390 /* Update client state */
392 state->read_buf_len = 0;
393 state->write_buf_len = sizeof(struct winbindd_response);
396 /* Read some data from a client connection */
398 static void client_read(struct winbindd_cli_state *state)
400 int n;
402 /* Read data */
404 do {
406 n = read(state->sock, state->read_buf_len +
407 (char *)&state->request,
408 sizeof(state->request) - state->read_buf_len);
410 } while (n == -1 && errno == EINTR);
412 if (n == -1 || n == 0) {
413 /* Read failed, kill client */
414 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
415 state->sock, state->pid,
416 (n == -1) ? strerror(errno) : "EOF"));
418 state->finished = True;
419 return;
422 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
424 /* Update client state */
426 state->read_buf_len += n;
429 /* Write some data to a client connection */
431 static void client_write(struct winbindd_cli_state *state)
433 char *data;
434 int num_written;
436 /* Write some data */
438 if (!state->write_extra_data) {
440 /* Write response structure */
442 data = (char *)&state->response + sizeof(state->response) -
443 state->write_buf_len;
445 } else {
447 /* Write extra data */
449 data = (char *)state->response.extra_data +
450 state->response.length -
451 sizeof(struct winbindd_response) -
452 state->write_buf_len;
455 do {
456 num_written = write(state->sock, data, state->write_buf_len);
457 } while (num_written == -1 && errno == EINTR);
459 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
461 /* Write failed, kill cilent */
463 if (num_written == -1 || num_written == 0) {
465 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
466 state->sock, state->pid,
467 (num_written == -1) ? strerror(errno) : "EOF"));
469 state->finished = True;
471 SAFE_FREE(state->response.extra_data);
473 return;
476 /* Update client state */
478 state->write_buf_len -= num_written;
480 /* Have we written all data? */
482 if (state->write_buf_len == 0) {
484 /* Take care of extra data */
486 if (state->write_extra_data) {
488 SAFE_FREE(state->response.extra_data);
490 state->write_extra_data = False;
492 DEBUG(10,("client_write: client_write: complete response written.\n"));
494 } else if (state->response.length >
495 sizeof(struct winbindd_response)) {
497 /* Start writing extra data */
499 state->write_buf_len =
500 state->response.length -
501 sizeof(struct winbindd_response);
503 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
505 state->write_extra_data = True;
510 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
511 non-forking, non-threaded model which allows us to handle many
512 simultaneous connections while remaining impervious to many denial of
513 service attacks. */
515 static void process_loop(int accept_sock)
517 /* We'll be doing this a lot */
519 while (1) {
520 struct winbindd_cli_state *state;
521 fd_set r_fds, w_fds;
522 int maxfd = accept_sock, selret;
523 struct timeval timeout;
525 /* Handle messages */
527 message_dispatch();
529 /* Free up temporary memory */
531 lp_talloc_free();
532 main_loop_talloc_free();
534 /* Initialise fd lists for select() */
536 FD_ZERO(&r_fds);
537 FD_ZERO(&w_fds);
538 FD_SET(accept_sock, &r_fds);
540 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
541 timeout.tv_usec = 0;
543 /* Set up client readers and writers */
545 state = client_list;
547 while (state) {
549 /* Dispose of client connection if it is marked as
550 finished */
552 if (state->finished) {
553 struct winbindd_cli_state *next = state->next;
555 remove_client(state);
556 state = next;
557 continue;
560 /* Select requires we know the highest fd used */
562 if (state->sock > maxfd)
563 maxfd = state->sock;
565 /* Add fd for reading */
567 if (state->read_buf_len != sizeof(state->request))
568 FD_SET(state->sock, &r_fds);
570 /* Add fd for writing */
572 if (state->write_buf_len)
573 FD_SET(state->sock, &w_fds);
575 state = state->next;
578 /* Call select */
580 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
582 if (selret == 0)
583 continue;
585 if ((selret == -1 && errno != EINTR) || selret == 0) {
587 /* Select error, something is badly wrong */
589 perror("select");
590 exit(1);
593 /* Create a new connection if accept_sock readable */
595 if (selret > 0) {
597 if (FD_ISSET(accept_sock, &r_fds))
598 new_connection(accept_sock);
600 /* Process activity on client connections */
602 for (state = client_list; state; state = state->next) {
604 /* Data available for reading */
606 if (FD_ISSET(state->sock, &r_fds)) {
608 /* Read data */
610 client_read(state);
613 * If we have the start of a
614 * packet, then check the
615 * length field to make sure
616 * the client's not talking
617 * Mock Swedish.
620 if (state->read_buf_len >= sizeof(uint32)
621 && *(uint32 *) &state->request != sizeof(state->request)) {
622 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
623 *(uint32 *) &state->request, sizeof(state->request)));
625 remove_client(state);
626 break;
629 /* A request packet might be
630 complete */
632 if (state->read_buf_len ==
633 sizeof(state->request)) {
634 process_packet(state);
638 /* Data available for writing */
640 if (FD_ISSET(state->sock, &w_fds))
641 client_write(state);
645 #if 0
646 winbindd_check_cache_size(time(NULL));
647 #endif
649 /* Check signal handling things */
651 if (do_sigterm)
652 terminate();
654 if (do_sighup) {
656 /* Flush winbindd cache */
658 flush_caches();
659 reload_services_file(True);
660 do_sighup = False;
663 if (do_sigusr2) {
664 print_winbindd_status();
665 do_sigusr2 = False;
670 /* Main function */
672 struct winbindd_state server_state; /* Server state information */
675 static void usage(void)
677 printf("Usage: winbindd [options]\n");
678 printf("\t-i interactive mode\n");
679 printf("\t-n disable cacheing\n");
680 printf("\t-d level set debug level\n");
681 printf("\t-s configfile choose smb.conf location\n");
682 printf("\t-h show this help message\n");
685 int main(int argc, char **argv)
687 extern BOOL AllowDebugChange;
688 extern pstring global_myname;
689 extern fstring global_myworkgroup;
690 extern BOOL append_log;
691 pstring logfile;
692 int accept_sock;
693 BOOL interactive = False;
694 int opt;
696 /* glibc (?) likes to print "User defined signal 1" and exit if a
697 SIGUSR[12] is received before a handler is installed */
699 CatchSignal(SIGUSR1, SIG_IGN);
700 CatchSignal(SIGUSR2, SIG_IGN);
702 TimeInit();
704 charset_initialise(); /* For *&#^%'s sake don't remove this */
706 fault_setup((void (*)(void *))fault_quit );
708 /* Initialise for running in non-root mode */
710 sec_init();
712 /* Set environment variable so we don't recursively call ourselves.
713 This may also be useful interactively. */
715 SETENV(WINBINDD_DONT_ENV, "1", 1);
717 /* Initialise samba/rpc client stuff */
719 while ((opt = getopt(argc, argv, "id:s:nh")) != EOF) {
720 switch (opt) {
722 /* Don't become a daemon */
723 case 'i':
724 interactive = True;
725 break;
727 /* disable cacheing */
728 case 'n':
729 opt_nocache = True;
730 break;
732 /* Run with specified debug level */
733 case 'd':
734 DEBUGLEVEL = atoi(optarg);
735 AllowDebugChange = False;
736 break;
738 /* Load a different smb.conf file */
739 case 's':
740 pstrcpy(servicesf,optarg);
741 break;
743 case 'h':
744 usage();
745 exit(0);
747 default:
748 printf("Unknown option %c\n", (char)opt);
749 exit(1);
753 /* Append to log file by default as we are a single process daemon
754 program. */
756 append_log = True;
758 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", LOGFILEBASE);
759 lp_set_logfile(logfile);
761 setup_logging("winbindd", interactive);
762 if (!reopen_logs()) {
763 fprintf(stderr, "Could not open logfile: %s\n", logfile);
764 fprintf(stderr, "Continuing in the hope that that is OK\n");
767 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
768 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
770 if (!reload_services_file(False)) {
771 DEBUG(0, ("error opening config file\n"));
772 exit(1);
775 codepage_initialise(lp_client_code_page());
777 /* Setup names. */
779 if (!*global_myname) {
780 char *p;
782 fstrcpy(global_myname, myhostname());
783 p = strchr(global_myname, '.');
784 if (p)
785 *p = 0;
788 fstrcpy(global_myworkgroup, lp_workgroup());
790 if (!interactive) {
791 become_daemon();
792 pidfile_create("winbindd");
796 #if HAVE_SETPGID
798 * If we're interactive we want to set our own process group for
799 * signal management.
801 if (interactive)
802 setpgid( (pid_t)0, (pid_t)0);
803 #endif
805 load_interfaces();
807 if (!secrets_init()) {
809 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
810 return 1;
814 /* Get list of domains we look up requests for. This includes the
815 domain which we are a member of as well as any trusted
816 domains. */
818 init_domain_list();
820 ZERO_STRUCT(server_state);
822 /* Winbind daemon initialisation */
824 if (!winbindd_param_init())
825 return 1;
827 if (!winbindd_idmap_init())
828 return 1;
830 /* Unblock all signals we are interested in as they may have been
831 blocked by the parent process. */
833 BlockSignals(False, SIGINT);
834 BlockSignals(False, SIGQUIT);
835 BlockSignals(False, SIGTERM);
836 BlockSignals(False, SIGUSR1);
837 BlockSignals(False, SIGUSR2);
838 BlockSignals(False, SIGHUP);
840 /* Setup signal handlers */
842 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
843 CatchSignal(SIGQUIT, termination_handler);
844 CatchSignal(SIGTERM, termination_handler);
846 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
848 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
849 CatchSignal(SIGHUP, sighup_handler);
851 /* Initialise messaging system */
853 if (!message_init()) {
854 DEBUG(0, ("unable to initialise messaging system\n"));
855 exit(1);
858 /* Create UNIX domain socket */
860 if ((accept_sock = create_sock()) == -1) {
861 DEBUG(0, ("failed to create socket\n"));
862 return 1;
865 /* Loop waiting for requests */
867 process_loop(accept_sock);
869 #if 0
870 uni_group_cache_shutdown();
871 #endif
873 return 0;