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 extern pstring debugf
;
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
,dyn_CONFIGFILE
)) {
44 pstrcpy(dyn_CONFIGFILE
,fname
);
50 ret
= lp_load(dyn_CONFIGFILE
,False
,False
,True
);
60 /**************************************************************************** **
61 Prepare to dump a core file - carefully!
62 **************************************************************************** */
64 static BOOL
dump_core(void)
68 pstrcpy( dname
, debugf
);
69 if ((p
=strrchr(dname
,'/')))
71 pstrcat( dname
, "/corefiles" );
73 sys_chown( dname
, getuid(), getgid() );
83 getrlimit( RLIMIT_CORE
, &rlp
);
84 rlp
.rlim_cur
= MAX( 4*1024*1024, rlp
.rlim_cur
);
85 setrlimit( RLIMIT_CORE
, &rlp
);
86 getrlimit( RLIMIT_CORE
, &rlp
);
87 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp
.rlim_cur
, (int)rlp
.rlim_max
) );
92 DEBUG(0,("Dumping core in %s\n",dname
));
98 /**************************************************************************** **
100 **************************************************************************** */
102 static void fault_quit(void)
109 static void winbindd_status(void)
111 struct winbindd_cli_state
*tmp
;
113 DEBUG(0, ("winbindd status:\n"));
115 /* Print client state information */
117 DEBUG(0, ("\t%d clients currently active\n", num_clients
));
119 if (DEBUGLEVEL
>= 2 && num_clients
) {
120 DEBUG(2, ("\tclient list:\n"));
121 for(tmp
= client_list
; tmp
; tmp
= tmp
->next
) {
122 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
123 tmp
->pid
, tmp
->sock
, tmp
->read_buf_len
,
124 tmp
->write_buf_len
));
129 /* Print winbindd status to log file */
131 static void print_winbindd_status(void)
134 winbindd_idmap_status();
135 winbindd_cache_status();
136 winbindd_cm_status();
139 /* Flush client cache */
141 static void flush_caches(void)
143 /* Clear cached user and group enumation info */
145 winbindd_flush_cache();
148 /* Handle the signal by unlinking socket and exiting */
150 static void terminate(void)
154 /* Remove socket file */
155 snprintf(path
, sizeof(path
), "%s/%s",
156 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
);
161 static BOOL do_sigterm
;
163 static void termination_handler(int signum
)
168 static BOOL do_sigusr1
;
170 static void sigusr1_handler(int signum
)
175 static BOOL do_sighup
;
177 static void sighup_handler(int signum
)
182 /* Create winbindd socket */
184 static int create_sock(void)
186 struct sockaddr_un sunaddr
;
192 /* Create the socket directory or reuse the existing one */
194 if (lstat(WINBINDD_SOCKET_DIR
, &st
) == -1) {
196 if (errno
== ENOENT
) {
198 /* Create directory */
200 if (mkdir(WINBINDD_SOCKET_DIR
, 0755) == -1) {
201 DEBUG(0, ("error creating socket directory "
202 "%s: %s\n", WINBINDD_SOCKET_DIR
,
209 DEBUG(0, ("lstat failed on socket directory %s: %s\n",
210 WINBINDD_SOCKET_DIR
, strerror(errno
)));
216 /* Check ownership and permission on existing directory */
218 if (!S_ISDIR(st
.st_mode
)) {
219 DEBUG(0, ("socket directory %s isn't a directory\n",
220 WINBINDD_SOCKET_DIR
));
224 if ((st
.st_uid
!= sec_initial_uid()) ||
225 ((st
.st_mode
& 0777) != 0755)) {
226 DEBUG(0, ("invalid permissions on socket directory "
227 "%s\n", WINBINDD_SOCKET_DIR
));
232 /* Create the socket file */
234 old_umask
= umask(0);
236 sock
= socket(AF_UNIX
, SOCK_STREAM
, 0);
243 snprintf(path
, sizeof(path
), "%s/%s",
244 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
);
247 memset(&sunaddr
, 0, sizeof(sunaddr
));
248 sunaddr
.sun_family
= AF_UNIX
;
249 safe_strcpy(sunaddr
.sun_path
, path
, sizeof(sunaddr
.sun_path
)-1);
251 if (bind(sock
, (struct sockaddr
*)&sunaddr
, sizeof(sunaddr
)) == -1) {
252 DEBUG(0, ("bind failed on winbind socket %s: %s\n",
259 if (listen(sock
, 5) == -1) {
260 DEBUG(0, ("listen failed on winbind socket %s: %s\n",
274 struct dispatch_table
{
275 enum winbindd_cmd cmd
;
276 enum winbindd_result (*fn
)(struct winbindd_cli_state
*state
);
277 char *winbindd_cmd_name
;
280 static struct dispatch_table dispatch_table
[] = {
284 { WINBINDD_GETPWNAM_FROM_USER
, winbindd_getpwnam_from_user
, "GETPWNAM_FROM_USER" },
285 { WINBINDD_GETPWNAM_FROM_UID
, winbindd_getpwnam_from_uid
, "GETPWNAM_FROM_UID" },
287 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
288 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
289 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
291 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
293 /* Group functions */
295 { WINBINDD_GETGRNAM_FROM_GROUP
, winbindd_getgrnam_from_group
, "GETGRNAM_FROM_GROUP" },
296 { WINBINDD_GETGRNAM_FROM_GID
, winbindd_getgrnam_from_gid
, "GETGRNAM_FROM_GID" },
297 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
298 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
299 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
301 /* PAM auth functions */
303 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
304 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
305 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
307 /* Enumeration functions */
309 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
310 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
311 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
, "LIST_TRUSTDOM" },
313 /* SID related functions */
315 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
316 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
318 /* Lookup related functions */
320 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
321 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
322 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
323 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
327 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
331 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
334 static void process_request(struct winbindd_cli_state
*state
)
336 struct dispatch_table
*table
= dispatch_table
;
338 /* Free response data - we may be interrupted and receive another
339 command before being able to send this data off. */
341 SAFE_FREE(state
->response
.extra_data
);
343 ZERO_STRUCT(state
->response
);
345 state
->response
.result
= WINBINDD_ERROR
;
346 state
->response
.length
= sizeof(struct winbindd_response
);
348 /* Process command */
350 for (table
= dispatch_table
; table
->fn
; table
++) {
351 if (state
->request
.cmd
== table
->cmd
) {
352 DEBUG(10,("process_request: request fn %s\n", table
->winbindd_cmd_name
));
353 state
->response
.result
= table
->fn(state
);
359 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state
->request
.cmd
));
361 /* In case extra data pointer is NULL */
363 if (!state
->response
.extra_data
)
364 state
->response
.length
= sizeof(struct winbindd_response
);
367 /* Process a new connection by adding it to the client connection list */
369 static void new_connection(int accept_sock
)
371 struct sockaddr_un sunaddr
;
372 struct winbindd_cli_state
*state
;
376 /* Accept connection */
378 len
= sizeof(sunaddr
);
381 sock
= accept(accept_sock
, (struct sockaddr
*)&sunaddr
, &len
);
382 } while (sock
== -1 && errno
== EINTR
);
387 DEBUG(6,("accepted socket %d\n", sock
));
389 /* Create new connection structure */
391 if ((state
= (struct winbindd_cli_state
*)
392 malloc(sizeof(*state
))) == NULL
)
398 /* Add to connection list */
400 DLIST_ADD(client_list
, state
);
404 /* Remove a client connection from client connection list */
406 static void remove_client(struct winbindd_cli_state
*state
)
408 /* It's a dead client - hold a funeral */
416 /* Free any getent state */
418 free_getent_state(state
->getpwent_state
);
419 free_getent_state(state
->getgrent_state
);
421 /* We may have some extra data that was not freed if the
422 client was killed unexpectedly */
424 SAFE_FREE(state
->response
.extra_data
);
426 /* Remove from list and free */
428 DLIST_REMOVE(client_list
, state
);
434 /* Process a complete received packet from a client */
436 static void process_packet(struct winbindd_cli_state
*state
)
438 /* Process request */
440 state
->pid
= state
->request
.pid
;
442 process_request(state
);
444 /* Update client state */
446 state
->read_buf_len
= 0;
447 state
->write_buf_len
= sizeof(struct winbindd_response
);
450 /* Read some data from a client connection */
452 static void client_read(struct winbindd_cli_state
*state
)
459 n
= read(state
->sock
, state
->read_buf_len
+ (char *)&state
->request
,
460 sizeof(state
->request
) - state
->read_buf_len
);
461 } while (n
== -1 && errno
== EINTR
);
463 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n
,
464 sizeof(state
->request
) - n
- state
->read_buf_len
));
466 /* Read failed, kill client */
468 if (n
== -1 || n
== 0) {
469 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
470 state
->sock
, state
->pid
,
471 (n
== -1) ? strerror(errno
) : "EOF"));
473 state
->finished
= True
;
477 /* Update client state */
479 state
->read_buf_len
+= n
;
482 /* Write some data to a client connection */
484 static void client_write(struct winbindd_cli_state
*state
)
489 /* Write some data */
491 if (!state
->write_extra_data
) {
493 /* Write response structure */
495 data
= (char *)&state
->response
+ sizeof(state
->response
) -
496 state
->write_buf_len
;
500 /* Write extra data */
502 data
= (char *)state
->response
.extra_data
+
503 state
->response
.length
-
504 sizeof(struct winbindd_response
) -
505 state
->write_buf_len
;
509 num_written
= write(state
->sock
, data
, state
->write_buf_len
);
510 } while (num_written
== -1 && errno
== EINTR
);
512 DEBUG(10,("client_write: wrote %d bytes.\n", num_written
));
514 /* Write failed, kill cilent */
516 if (num_written
== -1 || num_written
== 0) {
518 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
519 state
->sock
, state
->pid
,
520 (num_written
== -1) ? strerror(errno
) : "EOF"));
522 state
->finished
= True
;
524 SAFE_FREE(state
->response
.extra_data
);
529 /* Update client state */
531 state
->write_buf_len
-= num_written
;
533 /* Have we written all data? */
535 if (state
->write_buf_len
== 0) {
537 /* Take care of extra data */
539 if (state
->write_extra_data
) {
541 SAFE_FREE(state
->response
.extra_data
);
543 state
->write_extra_data
= False
;
545 DEBUG(10,("client_write: client_write: complete response written.\n"));
547 } else if (state
->response
.length
>
548 sizeof(struct winbindd_response
)) {
550 /* Start writing extra data */
552 state
->write_buf_len
=
553 state
->response
.length
-
554 sizeof(struct winbindd_response
);
556 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state
->write_buf_len
));
558 state
->write_extra_data
= True
;
563 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
564 non-forking, non-threaded model which allows us to handle many
565 simultaneous connections while remaining impervious to many denial of
568 static void process_loop(int accept_sock
)
570 /* We'll be doing this a lot */
573 struct winbindd_cli_state
*state
;
575 int maxfd
= accept_sock
, selret
;
576 struct timeval timeout
;
578 /* Free up temporary memory */
582 /* Initialise fd lists for select() */
586 FD_SET(accept_sock
, &r_fds
);
588 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
591 /* Set up client readers and writers */
597 /* Dispose of client connection if it is marked as
600 if (state
->finished
) {
601 struct winbindd_cli_state
*next
= state
->next
;
603 remove_client(state
);
608 /* Select requires we know the highest fd used */
610 if (state
->sock
> maxfd
)
613 /* Add fd for reading */
615 if (state
->read_buf_len
!= sizeof(state
->request
))
616 FD_SET(state
->sock
, &r_fds
);
618 /* Add fd for writing */
620 if (state
->write_buf_len
)
621 FD_SET(state
->sock
, &w_fds
);
628 selret
= select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
633 if ((selret
== -1 && errno
!= EINTR
) || selret
== 0) {
635 /* Select error, something is badly wrong */
641 /* Create a new connection if accept_sock readable */
645 if (FD_ISSET(accept_sock
, &r_fds
))
646 new_connection(accept_sock
);
648 /* Process activity on client connections */
650 for (state
= client_list
; state
; state
= state
->next
) {
652 /* Data available for reading */
654 if (FD_ISSET(state
->sock
, &r_fds
)) {
661 /* JRA - currently there's no length field in the request... */
663 * If we have the start of a
664 * packet, then check the
665 * length field to make sure
666 * the client's not talking
670 if (state
->read_buf_len
>= sizeof(int)
671 && *(int *) state
->buf
!= sizeof(state
->request
)) {
673 struct winbindd_cli_state
*rem_state
= state
;
675 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
676 *(int *) rem_state
->buf
, sizeof(rem_state
->request
) ));
679 remove_client(rem_state
);
684 /* A request packet might be
687 if (state
->read_buf_len
==
688 sizeof(state
->request
)) {
689 process_packet(state
);
693 /* Data available for writing */
695 if (FD_ISSET(state
->sock
, &w_fds
))
700 /* Check signal handling things */
707 /* Flush winbindd cache */
710 reload_services_file(True
);
715 print_winbindd_status();
723 struct winbindd_state server_state
; /* Server state information */
725 int main(int argc
, char **argv
)
727 extern pstring global_myname
;
729 BOOL interactive
= False
;
730 int opt
, new_debuglevel
= -1;
732 /* glibc (?) likes to print "User defined signal 1" and exit if a
733 SIGUSR1 is received before a handler is installed */
735 CatchSignal(SIGUSR1
, SIG_IGN
);
737 fault_setup((void (*)(void *))fault_quit
);
738 snprintf(debugf
, sizeof(debugf
), "%s/log.winbindd", dyn_LOGFILEBASE
);
740 /* Initialise for running in non-root mode */
744 /* Set environment variable so we don't recursively call ourselves.
745 This may also be useful interactively. */
747 SETENV(WINBINDD_DONT_ENV
, "1", 1);
749 /* Initialise samba/rpc client stuff */
751 while ((opt
= getopt(argc
, argv
, "id:s:")) != EOF
) {
754 /* Don't become a daemon */
760 /* Run with specified debug level */
763 new_debuglevel
= atoi(optarg
);
766 /* Load a different smb.conf file */
769 pstrcpy(dyn_CONFIGFILE
,optarg
);
773 printf("Unknown option %c\n", (char)opt
);
778 snprintf(debugf
, sizeof(debugf
), "%s/log.winbindd", dyn_LOGFILEBASE
);
779 setup_logging("winbindd", interactive
);
782 if (!*global_myname
) {
785 fstrcpy(global_myname
, myhostname());
786 p
= strchr(global_myname
, '.');
792 DEBUG(1, ("winbindd version %s started.\n", VERSION
) );
793 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
795 if (!reload_services_file(False
)) {
796 DEBUG(0, ("error opening config file\n"));
800 if (new_debuglevel
!= -1)
801 DEBUGLEVEL
= new_debuglevel
;
810 /* Get list of domains we look up requests for. This includes the
811 domain which we are a member of as well as any trusted
816 ZERO_STRUCT(server_state
);
818 /* Winbind daemon initialisation */
820 if (!winbindd_param_init())
823 if (!winbindd_idmap_init())
826 winbindd_cache_init();
828 /* Unblock all signals we are interested in as they may have been
829 blocked by the parent process. */
831 BlockSignals(False
, SIGINT
);
832 BlockSignals(False
, SIGQUIT
);
833 BlockSignals(False
, SIGTERM
);
834 BlockSignals(False
, SIGUSR1
);
835 BlockSignals(False
, SIGHUP
);
837 /* Setup signal handlers */
839 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
840 CatchSignal(SIGQUIT
, termination_handler
);
841 CatchSignal(SIGTERM
, termination_handler
);
843 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
845 CatchSignal(SIGUSR1
, sigusr1_handler
); /* Debugging sigs */
846 CatchSignal(SIGHUP
, sighup_handler
);
848 /* Create UNIX domain socket */
850 if ((accept_sock
= create_sock()) == -1) {
851 DEBUG(0, ("failed to create socket\n"));
855 /* Loop waiting for requests */
857 process_loop(accept_sock
);