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 pstring servicesf = CONFIGFILE;
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)
42 pstrcpy(fname,lp_configfile());
43 if (file_exist(fname,NULL) && !strcsequal(fname,servicesf)) {
44 pstrcpy(servicesf,fname);
50 ret = lp_load(servicesf,False,False,True);
58 static void winbindd_dump_status(void)
60 struct winbindd_cli_state *tmp;
62 DEBUG(0, ("Global status for winbindd:\n"));
64 /* Print client state information */
66 DEBUG(0, ("\t%d clients currently active\n", num_clients));
68 if (DEBUGLEVEL >= 2) {
69 DEBUG(2, ("\tclient list:\n"));
70 for(tmp = client_list; tmp; tmp = tmp->next) {
71 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
72 tmp->pid, tmp->sock, tmp->read_buf_len,
78 /* Print winbindd status to log file */
80 static void do_print_winbindd_status(void)
82 winbindd_dump_status();
83 winbindd_idmap_dump_status();
84 winbindd_cache_dump_status();
87 /* Flush client cache */
89 static void do_flush_caches(void)
91 /* Clear cached user and group enumation info */
93 winbindd_flush_cache();
96 /* Handle the signal by unlinking socket and exiting */
98 static void termination_handler(int signum)
102 /* Remove socket file */
104 snprintf(path, sizeof(path), "%s/%s",
105 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
111 static BOOL print_winbindd_status;
113 static void sigusr1_handler(int signum)
115 BlockSignals(True, SIGUSR1);
116 print_winbindd_status = True;
117 BlockSignals(False, SIGUSR1);
120 static BOOL do_sighup;
122 static void sighup_handler(int signum)
124 BlockSignals(True, SIGHUP);
126 BlockSignals(False, SIGHUP);
129 /* Create winbindd socket */
131 static int create_sock(void)
133 struct sockaddr_un sunaddr;
139 /* Create the socket directory or reuse the existing one */
141 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
143 if (errno == ENOENT) {
145 /* Create directory */
147 if (mkdir(WINBINDD_SOCKET_DIR, 0755) == -1) {
148 DEBUG(0, ("error creating socket directory "
149 "%s: %s\n", WINBINDD_SOCKET_DIR,
156 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
157 WINBINDD_SOCKET_DIR, strerror(errno)));
163 /* Check ownership and permission on existing directory */
165 if (!S_ISDIR(st.st_mode)) {
166 DEBUG(0, ("socket directory %s isn't a directory\n",
167 WINBINDD_SOCKET_DIR));
171 if ((st.st_uid != sec_initial_uid()) ||
172 ((st.st_mode & 0777) != 0755)) {
173 DEBUG(0, ("invalid permissions on socket directory "
174 "%s\n", WINBINDD_SOCKET_DIR));
179 /* Create the socket file */
181 old_umask = umask(0);
183 sock = socket(AF_UNIX, SOCK_STREAM, 0);
190 snprintf(path, sizeof(path), "%s/%s",
191 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
194 memset(&sunaddr, 0, sizeof(sunaddr));
195 sunaddr.sun_family = AF_UNIX;
196 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
198 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
199 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
206 if (listen(sock, 5) == -1) {
207 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
221 struct dispatch_table {
222 enum winbindd_cmd cmd;
223 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
224 char *winbindd_cmd_name;
227 static struct dispatch_table dispatch_table[] = {
231 { WINBINDD_GETPWNAM_FROM_USER, winbindd_getpwnam_from_user, "GETPWNAM_FROM_USER" },
232 { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid, "GETPWNAM_FROM_UID" },
234 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
235 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
236 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
238 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
240 /* Group functions */
242 { WINBINDD_GETGRNAM_FROM_GROUP, winbindd_getgrnam_from_group, "GETGRNAM_FROM_GROUP" },
243 { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid, "GETGRNAM_FROM_GID" },
244 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
245 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
246 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
248 /* PAM auth functions */
250 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
251 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
252 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
254 /* Enumeration functions */
256 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
257 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
258 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
260 /* SID related functions */
262 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
263 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
265 /* S*RS related functions */
267 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
268 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
269 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
270 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
274 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
278 { WINBINDD_NUM_CMDS, NULL, "NONE" }
281 static void process_request(struct winbindd_cli_state *state)
283 struct dispatch_table *table = dispatch_table;
285 /* Free response data - we may be interrupted and receive another
286 command before being able to send this data off. */
288 SAFE_FREE(state->response.extra_data);
290 ZERO_STRUCT(state->response);
292 state->response.result = WINBINDD_ERROR;
293 state->response.length = sizeof(struct winbindd_response);
295 /* Process command */
297 for (table = dispatch_table; table->fn; table++) {
298 if (state->request.cmd == table->cmd) {
299 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
300 state->response.result = table->fn(state);
306 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state->request.cmd ));
308 /* In case extra data pointer is NULL */
310 if (!state->response.extra_data)
311 state->response.length = sizeof(struct winbindd_response);
314 /* Process a new connection by adding it to the client connection list */
316 static void new_connection(int accept_sock)
318 struct sockaddr_un sunaddr;
319 struct winbindd_cli_state *state;
323 /* Accept connection */
325 len = sizeof(sunaddr);
326 if ((sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len))
330 DEBUG(6,("accepted socket %d\n", sock));
332 /* Create new connection structure */
334 if ((state = (struct winbindd_cli_state *)
335 malloc(sizeof(*state))) == NULL)
341 /* Add to connection list */
343 DLIST_ADD(client_list, state);
347 /* Remove a client connection from client connection list */
349 static void remove_client(struct winbindd_cli_state *state)
351 /* It's a dead client - hold a funeral */
359 /* Free any getent state */
361 free_getent_state(state->getpwent_state);
362 free_getent_state(state->getgrent_state);
364 /* We may have some extra data that was not freed if the
365 client was killed unexpectedly */
367 SAFE_FREE(state->response.extra_data);
369 /* Remove from list and free */
371 DLIST_REMOVE(client_list, state);
377 /* Process a complete received packet from a client */
379 static void process_packet(struct winbindd_cli_state *state)
381 /* Process request */
383 state->pid = state->request.pid;
385 process_request(state);
387 /* Update client state */
389 state->read_buf_len = 0;
390 state->write_buf_len = sizeof(struct winbindd_response);
393 /* Read some data from a client connection */
395 static void client_read(struct winbindd_cli_state *state)
401 n = read(state->sock, state->read_buf_len + (char *)&state->request,
402 sizeof(state->request) - state->read_buf_len);
404 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n,
405 sizeof(state->request) - n - state->read_buf_len ));
407 /* Read failed, kill client */
409 if (n == -1 || n == 0) {
410 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
411 state->sock, state->pid,
412 (n == -1) ? strerror(errno) : "EOF"));
414 state->finished = True;
418 /* Update client state */
420 state->read_buf_len += n;
423 /* Write some data to a client connection */
425 static void client_write(struct winbindd_cli_state *state)
430 /* Write some data */
432 if (!state->write_extra_data) {
434 /* Write response structure */
436 data = (char *)&state->response + sizeof(state->response) -
437 state->write_buf_len;
441 /* Write extra data */
443 data = (char *)state->response.extra_data +
444 state->response.length -
445 sizeof(struct winbindd_response) -
446 state->write_buf_len;
449 num_written = write(state->sock, data, state->write_buf_len);
451 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
453 /* Write failed, kill cilent */
455 if (num_written == -1 || num_written == 0) {
457 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
458 state->sock, state->pid,
459 (num_written == -1) ? strerror(errno) : "EOF"));
461 state->finished = True;
463 SAFE_FREE(state->response.extra_data);
468 /* Update client state */
470 state->write_buf_len -= num_written;
472 /* Have we written all data? */
474 if (state->write_buf_len == 0) {
476 /* Take care of extra data */
478 if (state->write_extra_data) {
480 SAFE_FREE(state->response.extra_data);
482 state->write_extra_data = False;
484 DEBUG(10,("client_write: client_write: complete response written.\n"));
486 } else if (state->response.length >
487 sizeof(struct winbindd_response)) {
489 /* Start writing extra data */
491 state->write_buf_len =
492 state->response.length -
493 sizeof(struct winbindd_response);
495 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
497 state->write_extra_data = True;
502 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
503 non-forking, non-threaded model which allows us to handle many
504 simultaneous connections while remaining impervious to many denial of
507 static void process_loop(int accept_sock)
509 /* We'll be doing this a lot */
512 struct winbindd_cli_state *state;
514 int maxfd = accept_sock, selret;
515 struct timeval timeout;
517 /* Free up temporary memory */
521 /* Initialise fd lists for select() */
525 FD_SET(accept_sock, &r_fds);
527 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
530 /* Set up client readers and writers */
536 /* Dispose of client connection if it is marked as
539 if (state->finished) {
540 struct winbindd_cli_state *next = state->next;
542 remove_client(state);
547 /* Select requires we know the highest fd used */
549 if (state->sock > maxfd) maxfd = state->sock;
551 /* Add fd for reading */
553 if (state->read_buf_len != sizeof(state->request))
554 FD_SET(state->sock, &r_fds);
556 /* Add fd for writing */
558 if (state->write_buf_len)
559 FD_SET(state->sock, &w_fds);
564 /* Check signal handling things */
568 /* Flush winbindd cache */
571 reload_services_file(True);
576 if (print_winbindd_status) {
577 do_print_winbindd_status();
578 print_winbindd_status = False;
583 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
585 if (selret == 0) continue;
587 if ((selret == -1 && errno != EINTR) || selret == 0) {
589 /* Select error, something is badly wrong */
595 /* Create a new connection if accept_sock readable */
599 if (FD_ISSET(accept_sock, &r_fds))
600 new_connection(accept_sock);
602 /* Process activity on client connections */
604 for (state = client_list; state; state = state->next) {
606 /* Data available for reading */
608 if (FD_ISSET(state->sock, &r_fds)) {
615 /* If we have the start of a
616 * packet, then check the
617 * length field to make sure
618 * the client's not talking
620 if (state->read_buf_len >= sizeof(int)
621 && *(int *) state->buf != sizeof(state->request)) {
626 /* A request packet might be
629 if (state->read_buf_len ==
630 sizeof(state->request)) {
631 process_packet(state);
635 /* Data available for writing */
637 if (FD_ISSET(state->sock, &w_fds))
646 struct winbindd_state server_state; /* Server state information */
648 int main(int argc, char **argv)
650 extern pstring global_myname;
651 extern pstring debugf;
653 BOOL interactive = False;
654 int opt, new_debuglevel = -1;
656 /* Initialise for running in non-root mode */
660 /* Set environment variable so we don't recursively call ourselves.
661 This may also be useful interactively. */
663 SETENV(WINBINDD_DONT_ENV, "1", 1);
665 /* Initialise samba/rpc client stuff */
667 while ((opt = getopt(argc, argv, "id:s:")) != EOF) {
670 /* Don't become a daemon */
676 /* Run with specified debug level */
679 new_debuglevel = atoi(optarg);
682 /* Load a different smb.conf file */
685 pstrcpy(servicesf,optarg);
689 printf("Unknown option %c\n", (char)opt);
694 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", LOGFILEBASE);
695 setup_logging("winbindd", interactive);
698 if (!*global_myname) {
701 fstrcpy(global_myname, myhostname());
702 p = strchr(global_myname, '.');
709 if (!reload_services_file(False)) {
710 DEBUG(0, ("error opening config file\n"));
714 if (new_debuglevel != -1)
715 DEBUGLEVEL = new_debuglevel;
724 /* Get list of domains we look up requests for. This includes the
725 domain which we are a member of as well as any trusted
730 ZERO_STRUCT(server_state);
732 /* Winbind daemon initialisation */
734 if (!winbindd_param_init())
737 if (!winbindd_idmap_init())
740 winbindd_cache_init();
742 /* Unblock all signals we are interested in as they may have been
743 blocked by the parent process. */
745 BlockSignals(False, SIGINT);
746 BlockSignals(False, SIGQUIT);
747 BlockSignals(False, SIGTERM);
748 BlockSignals(False, SIGUSR1);
749 BlockSignals(False, SIGHUP);
751 /* Setup signal handlers */
753 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
754 CatchSignal(SIGQUIT, termination_handler);
755 CatchSignal(SIGTERM, termination_handler);
757 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
759 CatchSignal(SIGUSR1, sigusr1_handler); /* Debugging sigs */
760 CatchSignal(SIGHUP, sighup_handler);
762 /* Create UNIX domain socket */
764 if ((accept_sock = create_sock()) == -1) {
765 DEBUG(0, ("failed to create socket\n"));
769 /* Loop waiting for requests */
771 process_loop(accept_sock);