2 Unix SMB/Netbios implementation.
5 Winbind daemon for ntdom nss module
7 Copyright (C) Tim Potter 2000
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);
226 static struct dispatch_table dispatch_table[] = {
230 { WINBINDD_GETPWNAM_FROM_USER, winbindd_getpwnam_from_user },
231 { WINBINDD_GETPWNAM_FROM_UID, winbindd_getpwnam_from_uid },
233 { WINBINDD_SETPWENT, winbindd_setpwent },
234 { WINBINDD_ENDPWENT, winbindd_endpwent },
235 { WINBINDD_GETPWENT, winbindd_getpwent },
239 { WINBINDD_GETGROUPS, winbindd_getgroups },
241 /* Group functions */
243 { WINBINDD_GETGRNAM_FROM_GROUP, winbindd_getgrnam_from_group },
244 { WINBINDD_GETGRNAM_FROM_GID, winbindd_getgrnam_from_gid },
245 { WINBINDD_SETGRENT, winbindd_setgrent },
246 { WINBINDD_ENDGRENT, winbindd_endgrent },
247 { WINBINDD_GETGRENT, winbindd_getgrent },
249 /* PAM auth functions */
251 { WINBINDD_PAM_AUTH, winbindd_pam_auth },
252 { WINBINDD_PAM_AUTH_CRAP, winbindd_pam_auth_crap },
253 { WINBINDD_PAM_CHAUTHTOK, winbindd_pam_chauthtok },
255 /* Enumeration functions */
257 { WINBINDD_LIST_USERS, winbindd_list_users },
258 { WINBINDD_LIST_GROUPS, winbindd_list_groups },
259 { WINBINDD_LIST_TRUSTDOM, winbindd_list_trusted_domains },
261 /* SID related functions */
263 { WINBINDD_LOOKUPSID, winbindd_lookupsid },
264 { WINBINDD_LOOKUPNAME, winbindd_lookupname },
266 /* S*RS related functions */
268 { WINBINDD_SID_TO_UID, winbindd_sid_to_uid },
269 { WINBINDD_SID_TO_GID, winbindd_sid_to_gid },
270 { WINBINDD_GID_TO_SID, winbindd_gid_to_sid },
271 { WINBINDD_UID_TO_SID, winbindd_uid_to_sid },
275 { WINBINDD_CHECK_MACHACC, winbindd_check_machine_acct },
281 { WINBINDD_NUM_CMDS, NULL }
284 static void process_request(struct winbindd_cli_state *state)
286 struct dispatch_table *table = dispatch_table;
288 /* Free response data - we may be interrupted and receive another
289 command before being able to send this data off. */
291 SAFE_FREE(state->response.extra_data);
293 ZERO_STRUCT(state->response);
295 state->response.result = WINBINDD_ERROR;
296 state->response.length = sizeof(struct winbindd_response);
298 /* Process command */
300 for (table = dispatch_table; table->fn; table++) {
301 if (state->request.cmd == table->cmd) {
302 state->response.result = table->fn(state);
307 /* In case extra data pointer is NULL */
309 if (!state->response.extra_data)
310 state->response.length = sizeof(struct winbindd_response);
313 /* Process a new connection by adding it to the client connection list */
315 static void new_connection(int accept_sock)
317 struct sockaddr_un sunaddr;
318 struct winbindd_cli_state *state;
322 /* Accept connection */
324 len = sizeof(sunaddr);
325 if ((sock = accept(accept_sock, (struct sockaddr *)&sunaddr, &len))
329 DEBUG(6,("accepted socket %d\n", sock));
331 /* Create new connection structure */
333 if ((state = (struct winbindd_cli_state *)
334 malloc(sizeof(*state))) == NULL)
340 /* Add to connection list */
342 DLIST_ADD(client_list, state);
346 /* Remove a client connection from client connection list */
348 static void remove_client(struct winbindd_cli_state *state)
350 /* It's a dead client - hold a funeral */
358 /* Free any getent state */
360 free_getent_state(state->getpwent_state);
361 free_getent_state(state->getgrent_state);
363 /* We may have some extra data that was not freed if the
364 client was killed unexpectedly */
366 SAFE_FREE(state->response.extra_data);
368 /* Remove from list and free */
370 DLIST_REMOVE(client_list, state);
376 /* Process a complete received packet from a client */
378 static void process_packet(struct winbindd_cli_state *state)
380 /* Process request */
382 state->pid = state->request.pid;
384 process_request(state);
386 /* Update client state */
388 state->read_buf_len = 0;
389 state->write_buf_len = sizeof(struct winbindd_response);
392 /* Read some data from a client connection */
394 static void client_read(struct winbindd_cli_state *state)
400 n = read(state->sock, state->read_buf_len + (char *)&state->request,
401 sizeof(state->request) - state->read_buf_len);
403 /* Read failed, kill client */
405 if (n == -1 || n == 0) {
406 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
407 state->sock, state->pid,
408 (n == -1) ? strerror(errno) : "EOF"));
410 state->finished = True;
414 /* Update client state */
416 state->read_buf_len += n;
419 /* Write some data to a client connection */
421 static void client_write(struct winbindd_cli_state *state)
426 /* Write some data */
428 if (!state->write_extra_data) {
430 /* Write response structure */
432 data = (char *)&state->response + sizeof(state->response) -
433 state->write_buf_len;
437 /* Write extra data */
439 data = (char *)state->response.extra_data +
440 state->response.length -
441 sizeof(struct winbindd_response) -
442 state->write_buf_len;
445 num_written = write(state->sock, data, state->write_buf_len);
447 /* Write failed, kill cilent */
449 if (num_written == -1 || num_written == 0) {
451 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
452 state->sock, state->pid,
453 (num_written == -1) ? strerror(errno) : "EOF"));
455 state->finished = True;
457 SAFE_FREE(state->response.extra_data);
462 /* Update client state */
464 state->write_buf_len -= num_written;
466 /* Have we written all data? */
468 if (state->write_buf_len == 0) {
470 /* Take care of extra data */
472 if (state->write_extra_data) {
474 SAFE_FREE(state->response.extra_data);
476 state->write_extra_data = False;
478 } else if (state->response.length >
479 sizeof(struct winbindd_response)) {
481 /* Start writing extra data */
483 state->write_buf_len =
484 state->response.length -
485 sizeof(struct winbindd_response);
487 state->write_extra_data = True;
492 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
493 non-forking, non-threaded model which allows us to handle many
494 simultaneous connections while remaining impervious to many denial of
497 static void process_loop(int accept_sock)
499 /* We'll be doing this a lot */
502 struct winbindd_cli_state *state;
504 int maxfd = accept_sock, selret;
505 struct timeval timeout;
507 /* Free up temporary memory */
511 /* Initialise fd lists for select() */
515 FD_SET(accept_sock, &r_fds);
517 timeout.tv_sec = WINBINDD_ESTABLISH_LOOP;
520 /* Set up client readers and writers */
526 /* Dispose of client connection if it is marked as
529 if (state->finished) {
530 struct winbindd_cli_state *next = state->next;
532 remove_client(state);
537 /* Select requires we know the highest fd used */
539 if (state->sock > maxfd) maxfd = state->sock;
541 /* Add fd for reading */
543 if (state->read_buf_len != sizeof(state->request))
544 FD_SET(state->sock, &r_fds);
546 /* Add fd for writing */
548 if (state->write_buf_len)
549 FD_SET(state->sock, &w_fds);
554 /* Check signal handling things */
558 /* Flush winbindd cache */
561 reload_services_file(True);
566 if (print_winbindd_status) {
567 do_print_winbindd_status();
568 print_winbindd_status = False;
573 selret = select(maxfd + 1, &r_fds, &w_fds, NULL, &timeout);
575 if (selret == 0) continue;
577 if ((selret == -1 && errno != EINTR) || selret == 0) {
579 /* Select error, something is badly wrong */
585 /* Create a new connection if accept_sock readable */
589 if (FD_ISSET(accept_sock, &r_fds))
590 new_connection(accept_sock);
592 /* Process activity on client connections */
594 for (state = client_list; state; state = state->next) {
596 /* Data available for reading */
598 if (FD_ISSET(state->sock, &r_fds)) {
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))
624 struct winbindd_state server_state; /* Server state information */
626 int main(int argc, char **argv)
628 extern pstring global_myname;
629 extern pstring debugf;
631 BOOL interactive = False;
632 int opt, new_debuglevel = -1;
634 /* Initialise for running in non-root mode */
638 /* Set environment variable so we don't recursively call ourselves.
639 This may also be useful interactively. */
641 SETENV(WINBINDD_DONT_ENV, "1", 1);
643 /* Initialise samba/rpc client stuff */
645 while ((opt = getopt(argc, argv, "id:s:")) != EOF) {
648 /* Don't become a daemon */
654 /* Run with specified debug level */
657 new_debuglevel = atoi(optarg);
660 /* Load a different smb.conf file */
663 pstrcpy(servicesf,optarg);
667 printf("Unknown option %c\n", (char)opt);
672 snprintf(debugf, sizeof(debugf), "%s/log.winbindd", LOGFILEBASE);
673 setup_logging("winbindd", interactive);
676 if (!*global_myname) {
679 fstrcpy(global_myname, myhostname());
680 p = strchr(global_myname, '.');
687 if (!reload_services_file(False)) {
688 DEBUG(0, ("error opening config file\n"));
692 if (new_debuglevel != -1)
693 DEBUGLEVEL = new_debuglevel;
702 /* Get list of domains we look up requests for. This includes the
703 domain which we are a member of as well as any trusted
708 ZERO_STRUCT(server_state);
710 /* Winbind daemon initialisation */
712 if (!winbindd_param_init())
715 if (!winbindd_idmap_init())
718 winbindd_cache_init();
720 /* Unblock all signals we are interested in as they may have been
721 blocked by the parent process. */
723 BlockSignals(False, SIGINT);
724 BlockSignals(False, SIGQUIT);
725 BlockSignals(False, SIGTERM);
726 BlockSignals(False, SIGUSR1);
727 BlockSignals(False, SIGHUP);
729 /* Setup signal handlers */
731 CatchSignal(SIGINT, termination_handler); /* Exit on these sigs */
732 CatchSignal(SIGQUIT, termination_handler);
733 CatchSignal(SIGTERM, termination_handler);
735 CatchSignal(SIGPIPE, SIG_IGN); /* Ignore sigpipe */
737 CatchSignal(SIGUSR1, sigusr1_handler); /* Debugging sigs */
738 CatchSignal(SIGHUP, sighup_handler);
740 /* Create UNIX domain socket */
742 if ((accept_sock = create_sock()) == -1) {
743 DEBUG(0, ("failed to create socket\n"));
747 /* Loop waiting for requests */
749 process_loop(accept_sock);