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 /* List of all connected clients */
28 struct winbindd_cli_state *client_list;
29 static int num_clients;
32 /* Reload configuration */
34 static BOOL reload_services_file(BOOL test)
42 pstrcpy(fname,lp_configfile());
43 if (file_exist(fname,NULL) && !strcsequal(fname,dyn_CONFIGFILE)) {
44 pstrcpy(dyn_CONFIGFILE,fname);
49 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
50 lp_set_logfile(logfile);
53 ret = lp_load(dyn_CONFIGFILE,False,False,True);
55 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
56 lp_set_logfile(logfile);
66 /**************************************************************************** **
67 Prepare to dump a core file - carefully!
68 **************************************************************************** */
70 static BOOL dump_core(void)
74 pstrcpy( dname, lp_logfile() );
75 if ((p=strrchr(dname,'/')))
77 pstrcat( dname, "/corefiles" );
79 sys_chown( dname, getuid(), getgid() );
89 getrlimit( RLIMIT_CORE, &rlp );
90 rlp.rlim_cur = MAX( 4*1024*1024, rlp.rlim_cur );
91 setrlimit( RLIMIT_CORE, &rlp );
92 getrlimit( RLIMIT_CORE, &rlp );
93 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp.rlim_cur, (int)rlp.rlim_max ) );
98 DEBUG(0,("Dumping core in %s\n",dname));
104 /**************************************************************************** **
106 **************************************************************************** */
108 static void fault_quit(void)
115 static void winbindd_status(void)
117 struct winbindd_cli_state *tmp;
119 DEBUG(0, ("winbindd status:\n"));
121 /* Print client state information */
123 DEBUG(0, ("\t%d clients currently active\n", num_clients));
125 if (DEBUGLEVEL >= 2 && num_clients) {
126 DEBUG(2, ("\tclient list:\n"));
127 for(tmp = client_list; tmp; tmp = tmp->next) {
128 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
129 tmp->pid, tmp->sock, tmp->read_buf_len,
130 tmp->write_buf_len));
135 /* Print winbindd status to log file */
137 static void print_winbindd_status(void)
140 winbindd_idmap_status();
141 winbindd_cm_status();
144 /* Flush client cache */
146 static void flush_caches(void)
148 /* Clear cached user and group enumation info */
149 wcache_flush_cache();
152 /* Handle the signal by unlinking socket and exiting */
154 static void terminate(void)
158 winbindd_idmap_close();
160 /* Remove socket file */
161 snprintf(path, sizeof(path), "%s/%s",
162 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
167 static BOOL do_sigterm;
169 static void termination_handler(int signum)
174 static BOOL do_sigusr2;
176 static void sigusr2_handler(int signum)
181 static BOOL do_sighup;
183 static void sighup_handler(int signum)
188 /* Create winbindd socket */
190 static int create_sock(void)
192 return create_pipe_sock( WINBINDD_SOCKET_DIR,
193 WINBINDD_SOCKET_NAME, 0755);
196 struct dispatch_table {
197 enum winbindd_cmd cmd;
198 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
199 char *winbindd_cmd_name;
202 static struct dispatch_table dispatch_table[] = {
206 { WINBINDD_GETPWNAM, winbindd_getpwnam, "GETPWNAM" },
207 { WINBINDD_GETPWUID, winbindd_getpwuid, "GETPWUID" },
209 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
210 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
211 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
213 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
215 /* Group functions */
217 { WINBINDD_GETGRNAM, winbindd_getgrnam, "GETGRNAM" },
218 { WINBINDD_GETGRGID, winbindd_getgrgid, "GETGRGID" },
219 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
220 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
221 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
223 /* PAM auth functions */
225 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
226 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
227 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
229 /* Enumeration functions */
231 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
232 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
233 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
235 /* SID related functions */
237 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
238 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
240 /* Lookup related functions */
242 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
243 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
244 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
245 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
249 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
250 { WINBINDD_PING, winbindd_ping, "PING" },
251 { WINBINDD_INFO, winbindd_info, "INFO" },
252 { WINBINDD_INTERFACE_VERSION, winbindd_interface_version, "INTERFACE_VERSION" },
256 { WINBINDD_NUM_CMDS, NULL, "NONE" }
259 static void process_request(struct winbindd_cli_state *state)
261 struct dispatch_table *table = dispatch_table;
263 /* Free response data - we may be interrupted and receive another
264 command before being able to send this data off. */
266 SAFE_FREE(state->response.extra_data);
268 ZERO_STRUCT(state->response);
270 state->response.result = WINBINDD_ERROR;
271 state->response.length = sizeof(struct winbindd_response);
273 /* Process command */
275 for (table = dispatch_table; table->fn; table++) {
276 if (state->request.cmd == table->cmd) {
277 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
278 state->response.result = table->fn(state);
284 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
286 /* In case extra data pointer is NULL */
288 if (!state->response.extra_data)
289 state->response.length = sizeof(struct winbindd_response);
292 /* Process a new connection by adding it to the client connection list */
294 static void new_connection(int accept_sock)
296 struct sockaddr_un sunaddr;
297 struct winbindd_cli_state *state;
301 /* Accept connection */
303 len = sizeof(sunaddr);
306 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
307 } while (sock == -1 && errno == EINTR);
312 DEBUG(6,("accepted socket %d\n", sock));
314 /* Create new connection structure */
316 if ((state = (struct winbindd_cli_state *)
317 malloc(sizeof(*state))) == NULL)
323 /* Add to connection list */
325 DLIST_ADD(client_list, state);
329 /* Remove a client connection from client connection list */
331 static void remove_client(struct winbindd_cli_state *state)
333 /* It's a dead client - hold a funeral */
341 /* Free any getent state */
343 free_getent_state(state->getpwent_state);
344 free_getent_state(state->getgrent_state);
346 /* We may have some extra data that was not freed if the
347 client was killed unexpectedly */
349 SAFE_FREE(state->response.extra_data);
351 /* Remove from list and free */
353 DLIST_REMOVE(client_list, state);
359 /* Process a complete received packet from a client */
361 static void process_packet(struct winbindd_cli_state *state)
363 /* Process request */
365 state->pid = state->request.pid;
367 process_request(state);
369 /* Update client state */
371 state->read_buf_len = 0;
372 state->write_buf_len = sizeof(struct winbindd_response);
375 /* Read some data from a client connection */
377 static void client_read(struct winbindd_cli_state *state)
385 n = read(state->sock, state->read_buf_len +
386 (char *)&state->request,
387 sizeof(state->request) - state->read_buf_len);
389 } while (n == -1 && errno == EINTR);
391 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n, sizeof(state->request) - n - state->read_buf_len ));
393 /* Read failed, kill client */
395 if (n == -1 || n == 0) {
396 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
397 state->sock, state->pid,
398 (n == -1) ? strerror(errno) : "EOF"));
400 state->finished = True;
404 /* Update client state */
406 state->read_buf_len += n;
409 /* Write some data to a client connection */
411 static void client_write(struct winbindd_cli_state *state)
416 /* Write some data */
418 if (!state->write_extra_data) {
420 /* Write response structure */
422 data = (char *)&state->response + sizeof(state->response) -
423 state->write_buf_len;
427 /* Write extra data */
429 data = (char *)state->response.extra_data +
430 state->response.length -
431 sizeof(struct winbindd_response) -
432 state->write_buf_len;
436 num_written = write(state->sock, data, state->write_buf_len);
437 } while (num_written == -1 && errno == EINTR);
439 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
441 /* Write failed, kill cilent */
443 if (num_written == -1 || num_written == 0) {
445 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
446 state->sock, state->pid,
447 (num_written == -1) ? strerror(errno) : "EOF"));
449 state->finished = True;
451 SAFE_FREE(state->response.extra_data);
456 /* Update client state */
458 state->write_buf_len -= num_written;
460 /* Have we written all data? */
462 if (state->write_buf_len == 0) {
464 /* Take care of extra data */
466 if (state->write_extra_data) {
468 SAFE_FREE(state->response.extra_data);
470 state->write_extra_data = False;
472 DEBUG(10,("client_write: client_write: complete response written.\n"));
474 } else if (state->response.length >
475 sizeof(struct winbindd_response)) {
477 /* Start writing extra data */
479 state->write_buf_len =
480 state->response.length -
481 sizeof(struct winbindd_response);
483 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
485 state->write_extra_data = True;
490 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
491 non-forking, non-threaded model which allows us to handle many
492 simultaneous connections while remaining impervious to many denial of
495 static void process_loop(int accept_sock)
497 /* We'll be doing this a lot */
500 struct winbindd_cli_state *state;
502 int maxfd = accept_sock, selret;
503 struct timeval timeout;
505 /* Free up temporary memory */
509 /* Initialise fd lists for select() */
513 FD_SET(accept_sock, &r_fds);
515 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
518 /* Set up client readers and writers */
524 /* Dispose of client connection if it is marked as
527 if (state->finished) {
528 struct winbindd_cli_state *next = state->next;
530 remove_client(state);
535 /* Select requires we know the highest fd used */
537 if (state->sock > maxfd)
540 /* Add fd for reading */
542 if (state->read_buf_len != sizeof(state->request))
543 FD_SET(state->sock, &r_fds);
545 /* Add fd for writing */
547 if (state->write_buf_len)
548 FD_SET(state->sock, &w_fds);
555 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
560 if ((selret == -1 && errno != EINTR) || selret == 0) {
562 /* Select error, something is badly wrong */
568 /* Create a new connection if accept_sock readable */
572 if (FD_ISSET(accept_sock, &r_fds))
573 new_connection(accept_sock);
575 /* Process activity on client connections */
577 for (state = client_list; state; state = state->next) {
579 /* Data available for reading */
581 if (FD_ISSET(state->sock, &r_fds)) {
588 * If we have the start of a
589 * packet, then check the
590 * length field to make sure
591 * the client's not talking
595 if (state->read_buf_len >= sizeof(uint32)
596 && *(uint32 *) &state->request != sizeof(state->request)) {
597 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
598 *(uint32 *) &state->request, sizeof(state->request)));
600 remove_client(state);
604 /* A request packet might be
607 if (state->read_buf_len ==
608 sizeof(state->request)) {
609 process_packet(state);
613 /* Data available for writing */
615 if (FD_ISSET(state->sock, &w_fds))
620 /* Check signal handling things */
627 /* Flush winbindd cache */
630 reload_services_file(True);
635 print_winbindd_status();
643 struct winbindd_state server_state; /* Server state information */
645 int main(int argc, char **argv)
647 extern pstring global_myname;
648 extern fstring global_myworkgroup;
649 extern BOOL append_log;
652 BOOL interactive = False;
653 int opt, new_debuglevel = -1;
655 /* glibc (?) likes to print "User defined signal 1" and exit if a
656 SIGUSR2 is received before a handler is installed */
658 CatchSignal(SIGUSR2, SIG_IGN);
660 fault_setup((void (*)(void *))fault_quit );
662 /* Append to log file by default as we are a single process daemon
667 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
668 lp_set_logfile(logfile);
670 /* Initialise for running in non-root mode */
674 /* Set environment variable so we don't recursively call ourselves.
675 This may also be useful interactively. */
677 SETENV(WINBINDD_DONT_ENV, "1", 1);
679 /* Initialise samba/rpc client stuff */
681 while ((opt = getopt(argc, argv, "id:s:n")) != EOF) {
684 /* Don't become a daemon */
689 /* disable cacheing */
694 /* Run with specified debug level */
696 new_debuglevel = atoi(optarg);
699 /* Load a different smb.conf file */
701 pstrcpy(dyn_CONFIGFILE,optarg);
705 printf("Unknown option %c\n", (char)opt);
710 snprintf(logfile, sizeof(logfile), "%s/log.winbindd", dyn_LOGFILEBASE);
711 lp_set_logfile(logfile);
712 setup_logging("winbindd", interactive);
715 DEBUG(1, ("winbindd version %s started.\n", VERSION ) );
716 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
718 if (!reload_services_file(False)) {
719 DEBUG(0, ("error opening config file\n"));
724 if (!*global_myname) {
727 fstrcpy(global_myname, myhostname());
728 p = strchr(global_myname, '.');
733 fstrcpy(global_myworkgroup, lp_workgroup());
735 if (new_debuglevel != -1)
736 DEBUGLEVEL = new_debuglevel;
743 * If we're interactive we want to set our own process group for
747 setpgid( (pid_t)0, (pid_t)0);
754 /* Get list of domains we look up requests for. This includes the
755 domain which we are a member of as well as any trusted
760 ZERO_STRUCT(server_state);
762 /* Winbind daemon initialisation */
764 if (!winbindd_param_init())
767 if (!winbindd_idmap_init())
770 /* Unblock all signals we are interested in as they may have been
771 blocked by the parent process. */
773 BlockSignals(False, SIGINT);
774 BlockSignals(False, SIGQUIT);
775 BlockSignals(False, SIGTERM);
776 BlockSignals(False, SIGUSR2);
777 BlockSignals(False, SIGHUP);
779 /* Setup signal handlers */
781 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
782 CatchSignal(SIGQUIT, termination_handler);
783 CatchSignal(SIGTERM, termination_handler);
785 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
786 CatchSignal(SIGUSR1, SIG_IGN); /* Samba messages */
788 CatchSignal(SIGUSR2, sigusr2_handler); /* Debugging sigs */
789 CatchSignal(SIGHUP, sighup_handler);
791 /* Create UNIX domain socket */
793 if ((accept_sock = create_sock()) == -1) {
794 DEBUG(0, ("failed to create socket\n"));
798 /* Loop waiting for requests */
800 process_loop(accept_sock);
802 uni_group_cache_shutdown();