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_status(void)
60 struct winbindd_cli_state *tmp;
62 DEBUG(0, ("winbindd status:\n"));
64 /* Print client state information */
66 DEBUG(0, ("\t%d clients currently active\n", num_clients));
68 if (DEBUGLEVEL >= 2 && num_clients) {
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 print_winbindd_status(void)
83 winbindd_idmap_status();
84 winbindd_cache_status();
88 /* Flush client cache */
90 static void flush_caches(void)
92 /* Clear cached user and group enumation info */
94 winbindd_flush_cache();
97 /* Handle the signal by unlinking socket and exiting */
99 static void terminate(void)
103 /* Remove socket file */
104 snprintf(path, sizeof(path), "%s/%s",
105 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
110 static BOOL do_sigterm;
112 static void termination_handler(int signum)
117 static BOOL do_sigusr1;
119 static void sigusr1_handler(int signum)
124 static BOOL do_sighup;
126 static void sighup_handler(int signum)
131 /* Create winbindd socket */
133 static int create_sock(void)
135 struct sockaddr_un sunaddr;
141 /* Create the socket directory or reuse the existing one */
143 if (lstat(WINBINDD_SOCKET_DIR, &st) == -1) {
145 if (errno == ENOENT) {
147 /* Create directory */
149 if (mkdir(WINBINDD_SOCKET_DIR, 0755) == -1) {
150 DEBUG(0, ("error creating socket directory "
151 "%s: %s\n", WINBINDD_SOCKET_DIR,
158 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
159 WINBINDD_SOCKET_DIR, strerror(errno)));
165 /* Check ownership and permission on existing directory */
167 if (!S_ISDIR(st.st_mode)) {
168 DEBUG(0, ("socket directory %s isn't a directory\n",
169 WINBINDD_SOCKET_DIR));
173 if ((st.st_uid != sec_initial_uid()) ||
174 ((st.st_mode & 0777) != 0755)) {
175 DEBUG(0, ("invalid permissions on socket directory "
176 "%s\n", WINBINDD_SOCKET_DIR));
181 /* Create the socket file */
183 old_umask = umask(0);
185 sock = socket(AF_UNIX, SOCK_STREAM, 0);
192 snprintf(path, sizeof(path), "%s/%s",
193 WINBINDD_SOCKET_DIR, WINBINDD_SOCKET_NAME);
196 memset(&sunaddr, 0, sizeof(sunaddr));
197 sunaddr.sun_family = AF_UNIX;
198 safe_strcpy(sunaddr.sun_path, path, sizeof(sunaddr.sun_path)-1);
200 if (bind(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
201 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
208 if (listen(sock, 5) == -1) {
209 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
223 struct dispatch_table {
224 enum winbindd_cmd cmd;
225 enum winbindd_result (*fn)(struct winbindd_cli_state *state);
226 char *winbindd_cmd_name;
229 static struct dispatch_table dispatch_table[] = {
233 { WINBINDD_GETPWNAM_FROM_USER, winbindd_getpwnam_from_user, "GETPWNAM_FROM_USER" },
234 { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid, "GETPWNAM_FROM_UID" },
236 { WINBINDD_SETPWENT, winbindd_setpwent, "SETPWENT" },
237 { WINBINDD_ENDPWENT, winbindd_endpwent, "ENDPWENT" },
238 { WINBINDD_GETPWENT, winbindd_getpwent, "GETPWENT" },
240 { WINBINDD_GETGROUPS, winbindd_getgroups, "GETGROUPS" },
242 /* Group functions */
244 { WINBINDD_GETGRNAM_FROM_GROUP, winbindd_getgrnam_from_group, "GETGRNAM_FROM_GROUP" },
245 { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid, "GETGRNAM_FROM_GID" },
246 { WINBINDD_SETGRENT, winbindd_setgrent, "SETGRENT" },
247 { WINBINDD_ENDGRENT, winbindd_endgrent, "ENDGRENT" },
248 { WINBINDD_GETGRENT, winbindd_getgrent, "GETGRENT" },
250 /* PAM auth functions */
252 { WINBINDD_PAM_AUTH, winbindd_pam_auth, "PAM_AUTH" },
253 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap, "AUTH_CRAP" },
254 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok, "CHAUTHTOK" },
256 /* Enumeration functions */
258 { WINBINDD_LIST_USERS, winbindd_list_users, "LIST_USERS" },
259 { WINBINDD_LIST_GROUPS, winbindd_list_groups, "LIST_GROUPS" },
260 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains, "LIST_TRUSTDOM" },
262 /* SID related functions */
264 { WINBINDD_LOOKUPSID, winbindd_lookupsid, "LOOKUPSID" },
265 { WINBINDD_LOOKUPNAME, winbindd_lookupname, "LOOKUPNAME" },
267 /* S*RS related functions */
269 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid, "SID_TO_UID" },
270 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid, "SID_TO_GID" },
271 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid, "GID_TO_SID" },
272 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid, "UID_TO_SID" },
276 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct, "CHECK_MACHACC" },
280 { WINBINDD_NUM_CMDS, NULL, "NONE" }
283 static void process_request(struct winbindd_cli_state *state)
285 struct dispatch_table *table = dispatch_table;
287 /* Free response data - we may be interrupted and receive another
288 command before being able to send this data off. */
290 SAFE_FREE(state->response.extra_data);
292 ZERO_STRUCT(state->response);
294 state->response.result = WINBINDD_ERROR;
295 state->response.length = sizeof(struct winbindd_response);
297 /* Process command */
299 for (table = dispatch_table; table->fn; table++) {
300 if (state->request.cmd == table->cmd) {
301 DEBUG(10,("process_request: request fn %s\n", table->winbindd_cmd_name ));
302 state->response.result = table->fn(state);
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;
325 /* Accept connection */
327 len = sizeof(sunaddr);
330 sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len);
331 } while (sock == -1 && errno == EINTR);
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)
347 /* Add to connection list */
349 DLIST_ADD(client_list, state);
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 */
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);
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)
408 n = read(state->sock, state->read_buf_len + (char *)&state->request,
409 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,
413 sizeof(state->request) - n - state->read_buf_len ));
415 /* Read failed, kill client */
417 if (n == -1 || n == 0) {
418 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
419 state->sock, state->pid,
420 (n == -1) ? strerror(errno) : "EOF"));
422 state->finished = True;
426 /* Update client state */
428 state->read_buf_len += n;
431 /* Write some data to a client connection */
433 static void client_write(struct winbindd_cli_state *state)
438 /* Write some data */
440 if (!state->write_extra_data) {
442 /* Write response structure */
444 data = (char *)&state->response + sizeof(state->response) -
445 state->write_buf_len;
449 /* Write extra data */
451 data = (char *)state->response.extra_data +
452 state->response.length -
453 sizeof(struct winbindd_response) -
454 state->write_buf_len;
458 num_written = write(state->sock, data, state->write_buf_len);
459 } while (num_written == -1 && errno == EINTR);
461 DEBUG(10,("client_write: wrote %d bytes.\n", num_written ));
463 /* Write failed, kill cilent */
465 if (num_written == -1 || num_written == 0) {
467 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
468 state->sock, state->pid,
469 (num_written == -1) ? strerror(errno) : "EOF"));
471 state->finished = True;
473 SAFE_FREE(state->response.extra_data);
478 /* Update client state */
480 state->write_buf_len -= num_written;
482 /* Have we written all data? */
484 if (state->write_buf_len == 0) {
486 /* Take care of extra data */
488 if (state->write_extra_data) {
490 SAFE_FREE(state->response.extra_data);
492 state->write_extra_data = False;
494 DEBUG(10,("client_write: client_write: complete response written.\n"));
496 } else if (state->response.length >
497 sizeof(struct winbindd_response)) {
499 /* Start writing extra data */
501 state->write_buf_len =
502 state->response.length -
503 sizeof(struct winbindd_response);
505 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state->write_buf_len));
507 state->write_extra_data = True;
512 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
513 non-forking, non-threaded model which allows us to handle many
514 simultaneous connections while remaining impervious to many denial of
517 static void process_loop(int accept_sock)
519 /* We'll be doing this a lot */
522 struct winbindd_cli_state *state;
524 int maxfd = accept_sock, selret;
525 struct timeval timeout;
527 /* Free up temporary memory */
531 /* Initialise fd lists for select() */
535 FD_SET(accept_sock, &r_fds);
537 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
540 /* Set up client readers and writers */
546 /* Dispose of client connection if it is marked as
549 if (state->finished) {
550 struct winbindd_cli_state *next = state->next;
552 remove_client(state);
557 /* Select requires we know the highest fd used */
559 if (state->sock > maxfd)
562 /* Add fd for reading */
564 if (state->read_buf_len != sizeof(state->request))
565 FD_SET(state->sock, &r_fds);
567 /* Add fd for writing */
569 if (state->write_buf_len)
570 FD_SET(state->sock, &w_fds);
577 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
582 if ((selret == -1 && errno != EINTR) || selret == 0) {
584 /* Select error, something is badly wrong */
590 /* Create a new connection if accept_sock readable */
594 if (FD_ISSET(accept_sock, &r_fds))
595 new_connection(accept_sock);
597 /* Process activity on client connections */
599 for (state = client_list; state; state = state->next) {
601 /* Data available for reading */
603 if (FD_ISSET(state->sock, &r_fds)) {
610 /* JRA - currently there's no length field in the request... */
612 * If we have the start of a
613 * packet, then check the
614 * length field to make sure
615 * the client's not talking
619 if (state->read_buf_len >= sizeof(int)
620 && *(int *) state->buf != sizeof(state->request)) {
622 struct winbindd_cli_state *rem_state = state;
624 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
625 *(int *) rem_state->buf, sizeof(rem_state->request) ));
628 remove_client(rem_state);
633 /* A request packet might be
636 if (state->read_buf_len ==
637 sizeof(state->request)) {
638 process_packet(state);
642 /* Data available for writing */
644 if (FD_ISSET(state->sock, &w_fds))
649 /* Check signal handling things */
656 /* Flush winbindd cache */
659 reload_services_file(True);
665 print_winbindd_status();
674 struct winbindd_state server_state; /* Server state information */
676 int main(int argc, char **argv)
678 extern pstring global_myname;
679 extern pstring debugf;
681 BOOL interactive = False;
682 int opt, new_debuglevel = -1;
684 /* glibc (?) likes to print "User defined signal 1" and exit if a
685 SIGUSR1 is received before a handler is installed */
687 CatchSignal(SIGUSR1, SIG_IGN);
689 /* Initialise for running in non-root mode */
693 /* Set environment variable so we don't recursively call ourselves.
694 This may also be useful interactively. */
696 SETENV(WINBINDD_DONT_ENV, "1", 1);
698 /* Initialise samba/rpc client stuff */
700 while ((opt = getopt(argc, argv, "id:s:")) != EOF) {
703 /* Don't become a daemon */
709 /* Run with specified debug level */
712 new_debuglevel = atoi(optarg);
715 /* Load a different smb.conf file */
718 pstrcpy(servicesf,optarg);
722 printf("Unknown option %c\n", (char)opt);
727 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", LOGFILEBASE);
728 setup_logging("winbindd", interactive);
731 if (!*global_myname) {
734 fstrcpy(global_myname, myhostname());
735 p = strchr(global_myname, '.');
742 if (!reload_services_file(False)) {
743 DEBUG(0, ("error opening config file\n"));
747 if (new_debuglevel != -1)
748 DEBUGLEVEL = new_debuglevel;
757 /* Get list of domains we look up requests for. This includes the
758 domain which we are a member of as well as any trusted
763 ZERO_STRUCT(server_state);
765 /* Winbind daemon initialisation */
767 if (!winbindd_param_init())
770 if (!winbindd_idmap_init())
773 winbindd_cache_init();
775 /* Unblock all signals we are interested in as they may have been
776 blocked by the parent process. */
778 BlockSignals(False, SIGINT);
779 BlockSignals(False, SIGQUIT);
780 BlockSignals(False, SIGTERM);
781 BlockSignals(False, SIGUSR1);
782 BlockSignals(False, SIGHUP);
784 /* Setup signal handlers */
786 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
787 CatchSignal(SIGQUIT, termination_handler);
788 CatchSignal(SIGTERM, termination_handler);
790 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
792 CatchSignal(SIGUSR1, sigusr1_handler); /* Debugging sigs */
793 CatchSignal(SIGHUP, sighup_handler);
795 /* Create UNIX domain socket */
797 if ((accept_sock = create_sock()) == -1) {
798 DEBUG(0, ("failed to create socket\n"));
802 /* Loop waiting for requests */
804 process_loop(accept_sock);