sync'ing up for 3.0alpha20 release
[Samba.git] / source / nsswitch / winbindd.c
blobbb4a1b78ec5509aec5fa40e0468b6e6e30c064a1
1 /*
2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000, 2001
7 Copyright (C) Andrew Tridgell 2002
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 /* List of all connected clients */
28 static struct winbindd_cli_state *client_list;
29 static int num_clients;
30 BOOL opt_nocache = False;
31 BOOL opt_dual_daemon = False;
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,dyn_CONFIGFILE)) {
45 pstrcpy(dyn_CONFIGFILE,fname);
46 test = False;
50 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
51 lp_set_logfile(logfile);
53 reopen_logs();
54 ret = lp_load(dyn_CONFIGFILE,False,False,True);
56 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
57 lp_set_logfile(logfile);
59 reopen_logs();
60 load_interfaces();
62 return(ret);
65 #if DUMP_CORE
67 /**************************************************************************** **
68 Prepare to dump a core file - carefully!
69 **************************************************************************** */
71 static BOOL dump_core(void)
73 char *p;
74 pstring dname;
75 pstrcpy( dname, lp_logfile() );
76 if ((p=strrchr(dname,'/')))
77 *p=0;
78 pstrcat( dname, "/corefiles" );
79 mkdir( dname, 0700 );
80 sys_chown( dname, getuid(), getgid() );
81 chmod( dname, 0700 );
82 if ( chdir(dname) )
83 return( False );
84 umask( ~(0700) );
86 #ifdef HAVE_GETRLIMIT
87 #ifdef RLIMIT_CORE
89 struct rlimit rlp;
90 getrlimit( RLIMIT_CORE, &rlp );
91 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
92 setrlimit( RLIMIT_CORE, &rlp );
93 getrlimit( RLIMIT_CORE, &rlp );
94 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
96 #endif
97 #endif
99 DEBUG(0,("Dumping core in %s\n",dname));
100 abort();
101 return( True );
102 } /* dump_core */
103 #endif
105 /**************************************************************************** **
106 Handle a fault..
107 **************************************************************************** */
109 static void fault_quit(void)
111 #if DUMP_CORE
112 dump_core();
113 #endif
116 static void winbindd_status(void)
118 struct winbindd_cli_state *tmp;
120 DEBUG(0, ("winbindd status:\n"));
122 /* Print client state information */
124 DEBUG(0, ("\t%d clients currently active\n", num_clients));
126 if (DEBUGLEVEL >= 2 && num_clients) {
127 DEBUG(2, ("\tclient list:\n"));
128 for(tmp = client_list; tmp; tmp = tmp->next) {
129 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
130 tmp->pid, tmp->sock, tmp->read_buf_len,
131 tmp->write_buf_len));
136 /* Print winbindd status to log file */
138 static void print_winbindd_status(void)
140 winbindd_status();
141 winbindd_idmap_status();
142 winbindd_cm_status();
145 /* Flush client cache */
147 static void flush_caches(void)
149 /* Clear cached user and group enumation info */
150 wcache_flush_cache();
153 /* Handle the signal by unlinking socket and exiting */
155 static void terminate(void)
157 pstring path;
159 winbindd_idmap_close();
161 /* Remove socket file */
162 snprintf(path, sizeof(path), "%s/%s",
163 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
164 unlink(path);
165 exit(0);
168 static BOOL do_sigterm;
170 static void termination_handler(int signum)
172 do_sigterm = True;
173 sys_select_signal();
176 static BOOL do_sigusr2;
178 static void sigusr2_handler(int signum)
180 do_sigusr2 = True;
181 sys_select_signal();
184 static BOOL do_sighup;
186 static void sighup_handler(int signum)
188 do_sighup = True;
189 sys_select_signal();
192 /* Create winbindd socket */
194 static int create_sock(void)
196 return create_pipe_sock( WINBINDD_SOCKET_DIR,
197 WINBINDD_SOCKET_NAME, 0755);
200 struct dispatch_table {
201 enum winbindd_cmd cmd;
202 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
203 char *winbindd_cmd_name;
206 static struct dispatch_table dispatch_table[] = {
208 /* User functions */
210 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
211 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
213 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
214 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
215 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
217 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
219 /* Group functions */
221 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
222 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
223 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
224 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
225 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
226 { WINBINDD_GETGRLST, winbindd_getgrent, "GETGRLST" },
228 /* PAM auth functions */
230 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
231 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
232 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
234 /* Enumeration functions */
236 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
237 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
238 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
239 { WINBINDD_SHOW_SEQUENCE, winbindd_show_sequence, "SHOW_SEQUENCE" },
241 /* SID related functions */
243 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
244 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
246 /* Lookup related functions */
248 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
249 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
250 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
251 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
253 /* Miscellaneous */
255 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
256 { WINBINDD_PING, winbindd_ping, "PING" },
257 { WINBINDD_INFO, winbindd_info, "INFO" },
258 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
259 { WINBINDD_DOMAIN_NAME, winbindd_domain_name, "DOMAIN_NAME" },
261 /* WINS functions */
263 { WINBINDD_WINS_BYNAME, winbindd_wins_byname, "WINS_BYNAME" },
264 { WINBINDD_WINS_BYIP, winbindd_wins_byip, "WINS_BYIP" },
266 /* End of list */
268 { WINBINDD_NUM_CMDS, NULL, "NONE" }
271 static void process_request(struct winbindd_cli_state *state)
273 struct dispatch_table *table = dispatch_table;
275 /* Free response data - we may be interrupted and receive another
276 command before being able to send this data off. */
278 SAFE_FREE(state->response.extra_data);
280 ZERO_STRUCT(state->response);
282 state->response.result = WINBINDD_ERROR;
283 state->response.length = sizeof(struct winbindd_response);
285 /* Process command */
287 for (table = dispatch_table; table->fn; table++) {
288 if (state->request.cmd == table->cmd) {
289 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
290 state->response.result = table->fn(state);
291 break;
295 if (!table->fn)
296 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
298 /* In case extra data pointer is NULL */
300 if (!state->response.extra_data)
301 state->response.length = sizeof(struct winbindd_response);
304 /* Process a new connection by adding it to the client connection list */
306 static void new_connection(int accept_sock)
308 struct sockaddr_un sunaddr;
309 struct winbindd_cli_state *state;
310 socklen_t len;
311 int sock;
313 /* Accept connection */
315 len = sizeof(sunaddr);
317 do {
318 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
319 } while (sock == -1 && errno == EINTR);
321 if (sock == -1)
322 return;
324 DEBUG(6,("accepted socket %d\n", sock));
326 /* Create new connection structure */
328 if ((state = (struct winbindd_cli_state *)
329 malloc(sizeof(*state))) == NULL)
330 return;
332 ZERO_STRUCTP(state);
333 state->sock = sock;
335 /* Add to connection list */
337 DLIST_ADD(client_list, state);
338 num_clients++;
341 /* Remove a client connection from client connection list */
343 static void remove_client(struct winbindd_cli_state *state)
345 /* It's a dead client - hold a funeral */
347 if (state != NULL) {
349 /* Close socket */
351 close(state->sock);
353 /* Free any getent state */
355 free_getent_state(state->getpwent_state);
356 free_getent_state(state->getgrent_state);
358 /* We may have some extra data that was not freed if the
359 client was killed unexpectedly */
361 SAFE_FREE(state->response.extra_data);
363 /* Remove from list and free */
365 DLIST_REMOVE(client_list, state);
366 SAFE_FREE(state);
367 num_clients--;
372 /* Process a complete received packet from a client */
374 void winbind_process_packet(struct winbindd_cli_state *state)
376 /* Process request */
378 /* Ensure null termination of entire request */
379 state->request.domain[sizeof(state->request.domain)-1]='\0';
381 state->pid = state->request.pid;
383 process_request(state);
385 /* Update client state */
387 state->read_buf_len = 0;
388 state->write_buf_len = sizeof(struct winbindd_response);
390 /* we might need to send it to the dual daemon */
391 if (opt_dual_daemon) {
392 dual_send_request(state);
396 /* Read some data from a client connection */
398 void winbind_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 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
414 /* Read failed, kill client */
416 if (n == -1 || n == 0) {
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 /* Update client state */
427 state->read_buf_len += n;
430 /* Write some data to a client connection */
432 static void client_write(struct winbindd_cli_state *state)
434 char *data;
435 int num_written;
437 /* Write some data */
439 if (!state->write_extra_data) {
441 /* Write response structure */
443 data = (char *)&state->response + sizeof(state->response) -
444 state->write_buf_len;
446 } else {
448 /* Write extra data */
450 data = (char *)state->response.extra_data +
451 state->response.length -
452 sizeof(struct winbindd_response) -
453 state->write_buf_len;
456 do {
457 num_written = write(state->sock, data, state->write_buf_len);
458 } while (num_written == -1 && errno == EINTR);
460 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
462 /* Write failed, kill cilent */
464 if (num_written == -1 || num_written == 0) {
466 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
467 state->sock, state->pid,
468 (num_written == -1) ? strerror(errno) : "EOF"));
470 state->finished = True;
472 SAFE_FREE(state->response.extra_data);
474 return;
477 /* Update client state */
479 state->write_buf_len -= num_written;
481 /* Have we written all data? */
483 if (state->write_buf_len == 0) {
485 /* Take care of extra data */
487 if (state->write_extra_data) {
489 SAFE_FREE(state->response.extra_data);
491 state->write_extra_data = False;
493 DEBUG(10,("client_write: client_write: complete response written.\n"));
495 } else if (state->response.length >
496 sizeof(struct winbindd_response)) {
498 /* Start writing extra data */
500 state->write_buf_len =
501 state->response.length -
502 sizeof(struct winbindd_response);
504 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
506 state->write_extra_data = True;
511 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
512 non-forking, non-threaded model which allows us to handle many
513 simultaneous connections while remaining impervious to many denial of
514 service attacks. */
516 static void process_loop(int accept_sock)
518 /* We'll be doing this a lot */
520 while (1) {
521 struct winbindd_cli_state *state;
522 fd_set r_fds, w_fds;
523 int maxfd = accept_sock, selret;
524 struct timeval timeout;
526 /* Handle messages */
528 message_dispatch();
530 /* Free up temporary memory */
532 lp_talloc_free();
533 main_loop_talloc_free();
535 /* Initialise fd lists for select() */
537 FD_ZERO(&r_fds);
538 FD_ZERO(&w_fds);
539 FD_SET(accept_sock, &r_fds);
541 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
542 timeout.tv_usec = 0;
544 if (opt_dual_daemon) {
545 maxfd = dual_select_setup(&w_fds, maxfd);
548 /* Set up client readers and writers */
550 state = client_list;
552 while (state) {
554 /* Dispose of client connection if it is marked as
555 finished */
557 if (state->finished) {
558 struct winbindd_cli_state *next = state->next;
560 remove_client(state);
561 state = next;
562 continue;
565 /* Select requires we know the highest fd used */
567 if (state->sock > maxfd)
568 maxfd = state->sock;
570 /* Add fd for reading */
572 if (state->read_buf_len != sizeof(state->request))
573 FD_SET(state->sock, &r_fds);
575 /* Add fd for writing */
577 if (state->write_buf_len)
578 FD_SET(state->sock, &w_fds);
580 state = state->next;
583 /* Call select */
585 selret = sys_select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
587 if (selret == 0)
588 continue;
590 if ((selret == -1 && errno != EINTR) || selret == 0) {
592 /* Select error, something is badly wrong */
594 perror("select");
595 exit(1);
598 /* Create a new connection if accept_sock readable */
600 if (selret > 0) {
602 if (opt_dual_daemon) {
603 dual_select(&w_fds);
606 if (FD_ISSET(accept_sock, &r_fds))
607 new_connection(accept_sock);
609 /* Process activity on client connections */
611 for (state = client_list; state; state = state->next) {
613 /* Data available for reading */
615 if (FD_ISSET(state->sock, &r_fds)) {
617 /* Read data */
619 winbind_client_read(state);
622 * If we have the start of a
623 * packet, then check the
624 * length field to make sure
625 * the client's not talking
626 * Mock Swedish.
629 if (state->read_buf_len >= sizeof(uint32)
630 && *(uint32 *) &state->request != sizeof(state->request)) {
631 DEBUG(0,("process_loop: Invalid request size from pid %d: %d bytes sent, should be %d\n",
632 state->request.pid, *(uint32 *) &state->request, sizeof(state->request)));
634 remove_client(state);
635 break;
638 /* A request packet might be
639 complete */
641 if (state->read_buf_len ==
642 sizeof(state->request)) {
643 winbind_process_packet(state);
647 /* Data available for writing */
649 if (FD_ISSET(state->sock, &w_fds))
650 client_write(state);
654 #if 0
655 winbindd_check_cache_size(time(NULL));
656 #endif
658 /* Check signal handling things */
660 if (do_sigterm)
661 terminate();
663 if (do_sighup) {
665 /* Flush winbindd cache */
667 flush_caches();
668 reload_services_file(True);
669 do_sighup = False;
672 if (do_sigusr2) {
673 print_winbindd_status();
674 do_sigusr2 = False;
681 these are split out from the main winbindd for use by the background daemon
683 int winbind_setup_common(void)
685 load_interfaces();
687 if (!secrets_init()) {
689 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
690 return 1;
694 namecache_enable(); /* Enable netbios namecache */
696 /* Get list of domains we look up requests for. This includes the
697 domain which we are a member of as well as any trusted
698 domains. */
700 init_domain_list();
702 ZERO_STRUCT(server_state);
704 /* Winbind daemon initialisation */
706 if (!winbindd_param_init())
707 return 1;
709 if (!winbindd_idmap_init())
710 return 1;
712 /* Unblock all signals we are interested in as they may have been
713 blocked by the parent process. */
715 BlockSignals(False, SIGINT);
716 BlockSignals(False, SIGQUIT);
717 BlockSignals(False, SIGTERM);
718 BlockSignals(False, SIGUSR1);
719 BlockSignals(False, SIGUSR2);
720 BlockSignals(False, SIGHUP);
722 /* Setup signal handlers */
724 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
725 CatchSignal(SIGQUIT, termination_handler);
726 CatchSignal(SIGTERM, termination_handler);
728 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
730 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
731 CatchSignal(SIGHUP, sighup_handler);
733 return 0;
737 /* Main function */
739 struct winbindd_state server_state; /* Server state information */
742 static void usage(void)
744 printf("Usage: winbindd [options]\n");
745 printf("\t-i interactive mode\n");
746 printf("\t-B dual daemon mode\n");
747 printf("\t-n disable cacheing\n");
748 printf("\t-d level set debug level\n");
749 printf("\t-s configfile choose smb.conf location\n");
750 printf("\t-h show this help message\n");
753 int main(int argc, char **argv)
755 extern BOOL AllowDebugChange;
756 extern pstring global_myname;
757 extern fstring global_myworkgroup;
758 extern BOOL append_log;
759 pstring logfile;
760 int accept_sock;
761 BOOL interactive = False;
762 int opt;
764 /* glibc (?) likes to print "User defined signal 1" and exit if a
765 SIGUSR[12] is received before a handler is installed */
767 CatchSignal(SIGUSR1, SIG_IGN);
768 CatchSignal(SIGUSR2, SIG_IGN);
770 fault_setup((void (*)(void *))fault_quit );
772 /* Append to log file by default as we are a single process daemon
773 program. */
775 append_log = True;
777 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
778 lp_set_logfile(logfile);
780 /* Initialise for running in non-root mode */
782 sec_init();
784 /* Set environment variable so we don't recursively call ourselves.
785 This may also be useful interactively. */
787 SETENV(WINBINDD_DONT_ENV, "1", 1);
789 /* Initialise samba/rpc client stuff */
791 while ((opt = getopt(argc, argv, "id:s:nhB")) != EOF) {
792 switch (opt) {
794 /* Don't become a daemon */
795 case 'i':
796 interactive = True;
797 break;
799 /* dual daemon system */
800 case 'B':
801 opt_dual_daemon = True;
802 break;
804 /* disable cacheing */
805 case 'n':
806 opt_nocache = True;
807 break;
809 /* Run with specified debug level */
810 case 'd':
811 DEBUGLEVEL = atoi(optarg);
812 AllowDebugChange = False;
813 break;
815 /* Load a different smb.conf file */
816 case 's':
817 pstrcpy(dyn_CONFIGFILE,optarg);
818 break;
820 case 'h':
821 usage();
822 exit(0);
824 default:
825 printf("Unknown option %c\n", (char)opt);
826 exit(1);
830 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
831 lp_set_logfile(logfile);
832 setup_logging("winbindd", interactive);
833 reopen_logs();
835 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
836 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
838 if (!reload_services_file(False)) {
839 DEBUG(0, ("error opening config file\n"));
840 exit(1);
843 /* Setup names. */
845 if (!*global_myname) {
846 char *p;
848 fstrcpy(global_myname, myhostname());
849 p = strchr(global_myname, '.');
850 if (p)
851 *p = 0;
854 fstrcpy(global_myworkgroup, lp_workgroup());
856 if (!interactive) {
857 become_daemon();
858 pidfile_create("winbindd");
862 #if HAVE_SETPGID
864 * If we're interactive we want to set our own process group for
865 * signal management.
867 if (interactive)
868 setpgid( (pid_t)0, (pid_t)0);
869 #endif
871 if (opt_dual_daemon) {
872 do_dual_daemon();
875 if (winbind_setup_common() != 0) {
876 return 1;
879 /* Initialise messaging system */
881 if (!message_init()) {
882 DEBUG(0, ("unable to initialise messaging system\n"));
883 exit(1);
886 register_msg_pool_usage();
888 /* Create UNIX domain socket */
890 if ((accept_sock = create_sock()) == -1) {
891 DEBUG(0, ("failed to create socket\n"));
892 return 1;
895 /* Loop waiting for requests */
897 process_loop(accept_sock);
899 uni_group_cache_shutdown();
900 return 0;