DEBUG(0, -> DEBUG(10, Caught by Andrew Bartlett (thanks !).
[Samba.git] / source / nsswitch / winbindd.c
blobb4fe60487cf2d20181cfa92a5fca6c6ea364f756
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 if (state->response.nt_status)
301 DEBUG(10, ("returning extended error 0x%08x\n",
302 state->response.nt_status));
303 break;
307 if (!table->fn)
308 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
310 /* In case extra data pointer is NULL */
312 if (!state->response.extra_data)
313 state->response.length = sizeof(struct winbindd_response);
316 /* Process a new connection by adding it to the client connection list */
318 static void new_connection(int accept_sock)
320 struct sockaddr_un sunaddr;
321 struct winbindd_cli_state *state;
322 socklen_t len;
323 int sock;
325 /* Accept connection */
327 len = sizeof(sunaddr);
329 do {
330 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
331 } while (sock == -1 && errno == EINTR);
333 if (sock == -1)
334 return;
336 DEBUG(6,("accepted socket %d\n", sock));
338 /* Create new connection structure */
340 if ((state = (struct winbindd_cli_state *)
341 malloc(sizeof(*state))) == NULL)
342 return;
344 ZERO_STRUCTP(state);
345 state->sock = sock;
347 /* Add to connection list */
349 DLIST_ADD(client_list, state);
350 num_clients++;
353 /* Remove a client connection from client connection list */
355 static void remove_client(struct winbindd_cli_state *state)
357 /* It's a dead client - hold a funeral */
359 if (state != NULL) {
361 /* Close socket */
363 close(state->sock);
365 /* Free any getent state */
367 free_getent_state(state->getpwent_state);
368 free_getent_state(state->getgrent_state);
370 /* We may have some extra data that was not freed if the
371 client was killed unexpectedly */
373 SAFE_FREE(state->response.extra_data);
375 /* Remove from list and free */
377 DLIST_REMOVE(client_list, state);
378 SAFE_FREE(state);
379 num_clients--;
383 /* Process a complete received packet from a client */
385 static void process_packet(struct winbindd_cli_state *state)
387 /* Process request */
389 state->pid = state->request.pid;
391 process_request(state);
393 /* Update client state */
395 state->read_buf_len = 0;
396 state->write_buf_len = sizeof(struct winbindd_response);
399 /* Read some data from a client connection */
401 static void client_read(struct winbindd_cli_state *state)
403 int n;
405 /* Read data */
407 do {
409 n = read(state->sock, state->read_buf_len +
410 (char *)&state->request,
411 sizeof(state->request) - state->read_buf_len);
413 } while (n == -1 && errno == EINTR);
415 if (n == -1 || n == 0) {
416 /* Read failed, kill client */
417 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
418 state->sock, state->pid,
419 (n == -1) ? strerror(errno) : "EOF"));
421 state->finished = True;
422 return;
425 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
427 /* Update client state */
429 state->read_buf_len += n;
432 /* Write some data to a client connection */
434 static void client_write(struct winbindd_cli_state *state)
436 char *data;
437 int num_written;
439 /* Write some data */
441 if (!state->write_extra_data) {
443 /* Write response structure */
445 data = (char *)&state->response + sizeof(state->response) -
446 state->write_buf_len;
448 } else {
450 /* Write extra data */
452 data = (char *)state->response.extra_data +
453 state->response.length -
454 sizeof(struct winbindd_response) -
455 state->write_buf_len;
458 do {
459 num_written = write(state->sock, data, state->write_buf_len);
460 } while (num_written == -1 && errno == EINTR);
462 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
464 /* Write failed, kill cilent */
466 if (num_written == -1 || num_written == 0) {
468 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
469 state->sock, state->pid,
470 (num_written == -1) ? strerror(errno) : "EOF"));
472 state->finished = True;
474 SAFE_FREE(state->response.extra_data);
476 return;
479 /* Update client state */
481 state->write_buf_len -= num_written;
483 /* Have we written all data? */
485 if (state->write_buf_len == 0) {
487 /* Take care of extra data */
489 if (state->write_extra_data) {
491 SAFE_FREE(state->response.extra_data);
493 state->write_extra_data = False;
495 DEBUG(10,("client_write: client_write: complete response written.\n"));
497 } else if (state->response.length >
498 sizeof(struct winbindd_response)) {
500 /* Start writing extra data */
502 state->write_buf_len =
503 state->response.length -
504 sizeof(struct winbindd_response);
506 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
508 state->write_extra_data = True;
513 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
514 non-forking, non-threaded model which allows us to handle many
515 simultaneous connections while remaining impervious to many denial of
516 service attacks. */
518 static void process_loop(int accept_sock)
520 /* We'll be doing this a lot */
522 while (1) {
523 struct winbindd_cli_state *state;
524 fd_set r_fds, w_fds;
525 int maxfd = accept_sock, selret;
526 struct timeval timeout;
528 /* Handle messages */
530 message_dispatch();
532 /* Free up temporary memory */
534 lp_talloc_free();
535 main_loop_talloc_free();
537 /* Initialise fd lists for select() */
539 FD_ZERO(&r_fds);
540 FD_ZERO(&w_fds);
541 FD_SET(accept_sock, &r_fds);
543 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
544 timeout.tv_usec = 0;
546 /* Set up client readers and writers */
548 state = client_list;
550 while (state) {
552 /* Dispose of client connection if it is marked as
553 finished */
555 if (state->finished) {
556 struct winbindd_cli_state *next = state->next;
558 remove_client(state);
559 state = next;
560 continue;
563 /* Select requires we know the highest fd used */
565 if (state->sock > maxfd)
566 maxfd = state->sock;
568 /* Add fd for reading */
570 if (state->read_buf_len != sizeof(state->request))
571 FD_SET(state->sock, &r_fds);
573 /* Add fd for writing */
575 if (state->write_buf_len)
576 FD_SET(state->sock, &w_fds);
578 state = state->next;
581 /* Call select */
583 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
585 if (selret == 0)
586 continue;
588 if ((selret == -1 && errno != EINTR) || selret == 0) {
590 /* Select error, something is badly wrong */
592 perror("select");
593 exit(1);
596 /* Create a new connection if accept_sock readable */
598 if (selret > 0) {
600 if (FD_ISSET(accept_sock, &r_fds))
601 new_connection(accept_sock);
603 /* Process activity on client connections */
605 for (state = client_list; state; state = state->next) {
607 /* Data available for reading */
609 if (FD_ISSET(state->sock, &r_fds)) {
611 /* Read data */
613 client_read(state);
616 * If we have the start of a
617 * packet, then check the
618 * length field to make sure
619 * the client's not talking
620 * Mock Swedish.
623 if (state->read_buf_len >= sizeof(uint32)
624 && *(uint32 *) &state->request != sizeof(state->request)) {
625 DEBUG(0,("process_loop: Invalid request size (%d) sent, should be (%d)\n",
626 *(uint32 *) &state->request, sizeof(state->request)));
628 remove_client(state);
629 break;
632 /* A request packet might be
633 complete */
635 if (state->read_buf_len ==
636 sizeof(state->request)) {
637 process_packet(state);
641 /* Data available for writing */
643 if (FD_ISSET(state->sock, &w_fds))
644 client_write(state);
648 #if 0
649 winbindd_check_cache_size(time(NULL));
650 #endif
652 /* Check signal handling things */
654 if (do_sigterm)
655 terminate();
657 if (do_sighup) {
659 /* Flush winbindd cache */
661 flush_caches();
662 reload_services_file(True);
663 #if 0 /* Notised at present. */
664 namecache_flush();
665 #endif
666 winbindd_cm_flush();
668 do_sighup = False;
671 if (do_sigusr2) {
672 print_winbindd_status();
673 do_sigusr2 = False;
678 /* Main function */
680 struct winbindd_state server_state; /* Server state information */
683 static void usage(void)
685 printf("Usage: winbindd [options]\n");
686 printf("Version: %s\n", VERSION);
687 printf("\t-i interactive mode\n");
688 printf("\t-n disable cacheing\n");
689 printf("\t-d level set debug level\n");
690 printf("\t-s configfile choose smb.conf location\n");
691 printf("\t-h show this help message\n");
694 int main(int argc, char **argv)
696 extern BOOL AllowDebugChange;
697 extern pstring global_myname;
698 extern fstring global_myworkgroup;
699 extern BOOL append_log;
700 pstring logfile;
701 int accept_sock;
702 BOOL interactive = False;
703 int opt;
705 /* glibc (?) likes to print "User defined signal 1" and exit if a
706 SIGUSR[12] is received before a handler is installed */
708 CatchSignal(SIGUSR1, SIG_IGN);
709 CatchSignal(SIGUSR2, SIG_IGN);
711 TimeInit();
713 charset_initialise(); /* For *&#^%'s sake don't remove this */
715 fault_setup((void (*)(void *))fault_quit );
717 /* Initialise for running in non-root mode */
719 sec_init();
721 /* Set environment variable so we don't recursively call ourselves.
722 This may also be useful interactively. */
724 SETENV(WINBINDD_DONT_ENV, "1", 1);
726 /* Initialise samba/rpc client stuff */
728 while ((opt = getopt(argc, argv, "id:s:nh")) != EOF) {
729 switch (opt) {
731 /* Don't become a daemon */
732 case 'i':
733 interactive = True;
734 break;
736 /* disable cacheing */
737 case 'n':
738 opt_nocache = True;
739 break;
741 /* Run with specified debug level */
742 case 'd':
743 DEBUGLEVEL = atoi(optarg);
744 AllowDebugChange = False;
745 break;
747 /* Load a different smb.conf file */
748 case 's':
749 pstrcpy(servicesf,optarg);
750 break;
752 case 'h':
753 usage();
754 exit(0);
756 default:
757 printf("Unknown option %c\n", (char)opt);
758 exit(1);
762 /* Append to log file by default as we are a single process daemon
763 program. */
765 append_log = True;
767 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", LOGFILEBASE);
768 lp_set_logfile(logfile);
770 setup_logging("winbindd", interactive);
771 if (!reopen_logs()) {
772 fprintf(stderr, "Could not open logfile: %s\n", logfile);
773 fprintf(stderr, "Continuing in the hope that that is OK\n");
776 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
777 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
779 if (!reload_services_file(False)) {
780 DEBUG(0, ("error opening config file\n"));
781 exit(1);
784 codepage_initialise(lp_client_code_page());
786 /* Setup names. */
788 if (!*global_myname) {
789 char *p;
791 fstrcpy(global_myname, myhostname());
792 p = strchr(global_myname, '.');
793 if (p)
794 *p = 0;
797 fstrcpy(global_myworkgroup, lp_workgroup());
799 if (!interactive) {
800 become_daemon();
801 pidfile_create("winbindd");
805 #if HAVE_SETPGID
807 * If we're interactive we want to set our own process group for
808 * signal management.
810 if (interactive)
811 setpgid( (pid_t)0, (pid_t)0);
812 #endif
814 load_interfaces();
816 if (!secrets_init()) {
818 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
819 return 1;
823 #if 0 /* Notised at present. */
824 namecache_enable(); /* Enable netbios namecache */
825 #endif
827 /* Get list of domains we look up requests for. This includes the
828 domain which we are a member of as well as any trusted
829 domains. */
831 init_domain_list();
833 ZERO_STRUCT(server_state);
835 /* Winbind daemon initialisation */
837 if (!winbindd_param_init())
838 return 1;
840 if (!winbindd_idmap_init())
841 return 1;
843 /* Unblock all signals we are interested in as they may have been
844 blocked by the parent process. */
846 BlockSignals(False, SIGINT);
847 BlockSignals(False, SIGQUIT);
848 BlockSignals(False, SIGTERM);
849 BlockSignals(False, SIGUSR1);
850 BlockSignals(False, SIGUSR2);
851 BlockSignals(False, SIGHUP);
853 /* Setup signal handlers */
855 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
856 CatchSignal(SIGQUIT, termination_handler);
857 CatchSignal(SIGTERM, termination_handler);
859 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
861 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
862 CatchSignal(SIGHUP, sighup_handler);
864 /* Initialise messaging system */
866 if (!message_init()) {
867 DEBUG(0, ("unable to initialise messaging system\n"));
868 exit(1);
871 /* Create UNIX domain socket */
873 if ((accept_sock = create_sock()) == -1) {
874 DEBUG(0, ("failed to create socket\n"));
875 return 1;
878 /* Loop waiting for requests */
880 process_loop(accept_sock);
882 #if 0
883 uni_group_cache_shutdown();
884 #endif
886 return 0;