2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) by Tim Potter 2000, 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 /* List of all connected clients */
27 struct winbindd_cli_state
*client_list
;
28 static int num_clients
;
31 pstring servicesf
= CONFIGFILE
;
33 /* Reload configuration */
35 static BOOL
reload_services_file(BOOL test
)
43 pstrcpy(fname
,lp_configfile());
44 if (file_exist(fname
,NULL
) && !strcsequal(fname
,servicesf
)) {
45 pstrcpy(servicesf
,fname
);
50 snprintf(logfile
, sizeof(logfile
), "%s/log.winbindd", LOGFILEBASE
);
51 lp_set_logfile(logfile
);
54 fprintf(stderr
, "Could not open logfile: %s\n", logfile
);
55 fprintf(stderr
, "Continuing in the hope that that is OK\n");
58 ret
= lp_load(servicesf
,False
,False
,True
);
60 snprintf(logfile
, sizeof(logfile
), "%s/log.winbindd", LOGFILEBASE
);
61 lp_set_logfile(logfile
);
64 fprintf(stderr
, "Could not open logfile: %s\n", logfile
);
65 fprintf(stderr
, "Continuing in the hope that that is OK\n");
75 /**************************************************************************** **
76 Prepare to dump a core file - carefully!
77 **************************************************************************** */
79 static BOOL
dump_core(void)
83 pstrcpy( dname
, lp_logfile() );
84 if ((p
=strrchr(dname
,'/')))
86 pstrcat( dname
, "/corefiles" );
88 sys_chown( dname
, getuid(), getgid() );
98 getrlimit( RLIMIT_CORE
, &rlp
);
99 rlp
.rlim_cur
= MAX( 4*1024*1024, rlp
.rlim_cur
);
100 setrlimit( RLIMIT_CORE
, &rlp
);
101 getrlimit( RLIMIT_CORE
, &rlp
);
102 DEBUG( 3, ( "Core limits now %d %d\n", (int)rlp
.rlim_cur
, (int)rlp
.rlim_max
) );
107 DEBUG(0,("Dumping core in %s\n",dname
));
113 /**************************************************************************** **
115 **************************************************************************** */
117 static void fault_quit(void)
124 static void winbindd_status(void)
126 struct winbindd_cli_state
*tmp
;
128 DEBUG(0, ("winbindd status:\n"));
130 /* Print client state information */
132 DEBUG(0, ("\t%d clients currently active\n", num_clients
));
134 if (DEBUGLEVEL
>= 2 && num_clients
) {
135 DEBUG(2, ("\tclient list:\n"));
136 for(tmp
= client_list
; tmp
; tmp
= tmp
->next
) {
137 DEBUG(2, ("\t\tpid %d, sock %d, rbl %d, wbl %d\n",
138 tmp
->pid
, tmp
->sock
, tmp
->read_buf_len
,
139 tmp
->write_buf_len
));
144 /* Print winbindd status to log file */
146 static void print_winbindd_status(void)
149 winbindd_idmap_status();
150 winbindd_cm_status();
153 /* Flush client cache */
155 static void flush_caches(void)
157 /* Clear cached user and group enumation info */
158 wcache_flush_cache();
161 /* Handle the signal by unlinking socket and exiting */
163 static void terminate(void)
167 winbindd_idmap_close();
169 /* Remove socket file */
170 snprintf(path
, sizeof(path
), "%s/%s",
171 WINBINDD_SOCKET_DIR
, WINBINDD_SOCKET_NAME
);
176 static BOOL do_sigterm
;
178 static void termination_handler(int signum
)
184 static BOOL do_sigusr2
;
186 static void sigusr2_handler(int signum
)
192 static BOOL do_sighup
;
194 static void sighup_handler(int signum
)
200 /* Create winbindd socket */
202 static int create_sock(void)
204 return create_pipe_sock( WINBINDD_SOCKET_DIR
,
205 WINBINDD_SOCKET_NAME
, 0755);
208 struct dispatch_table
{
209 enum winbindd_cmd cmd
;
210 enum winbindd_result (*fn
)(struct winbindd_cli_state
*state
);
211 char *winbindd_cmd_name
;
214 static struct dispatch_table dispatch_table
[] = {
218 { WINBINDD_GETPWNAM
, winbindd_getpwnam
, "GETPWNAM" },
219 { WINBINDD_GETPWUID
, winbindd_getpwuid
, "GETPWUID" },
221 { WINBINDD_SETPWENT
, winbindd_setpwent
, "SETPWENT" },
222 { WINBINDD_ENDPWENT
, winbindd_endpwent
, "ENDPWENT" },
223 { WINBINDD_GETPWENT
, winbindd_getpwent
, "GETPWENT" },
225 { WINBINDD_GETGROUPS
, winbindd_getgroups
, "GETGROUPS" },
227 /* Group functions */
229 { WINBINDD_GETGRNAM
, winbindd_getgrnam
, "GETGRNAM" },
230 { WINBINDD_GETGRGID
, winbindd_getgrgid
, "GETGRGID" },
231 { WINBINDD_SETGRENT
, winbindd_setgrent
, "SETGRENT" },
232 { WINBINDD_ENDGRENT
, winbindd_endgrent
, "ENDGRENT" },
233 { WINBINDD_GETGRENT
, winbindd_getgrent
, "GETGRENT" },
235 /* PAM auth functions */
237 { WINBINDD_PAM_AUTH
, winbindd_pam_auth
, "PAM_AUTH" },
238 #ifdef WITH_WINBIND_AUTH_CRAP
239 { WINBINDD_PAM_AUTH_CRAP
, winbindd_pam_auth_crap
, "AUTH_CRAP" },
241 { WINBINDD_PAM_CHAUTHTOK
, winbindd_pam_chauthtok
, "CHAUTHTOK" },
243 /* Enumeration functions */
245 { WINBINDD_LIST_USERS
, winbindd_list_users
, "LIST_USERS" },
246 { WINBINDD_LIST_GROUPS
, winbindd_list_groups
, "LIST_GROUPS" },
247 { WINBINDD_LIST_TRUSTDOM
, winbindd_list_trusted_domains
, "LIST_TRUSTDOM" },
248 { WINBINDD_SHOW_SEQUENCE
, winbindd_show_sequence
, "SHOW_SEQUENCE" },
250 /* SID related functions */
252 { WINBINDD_LOOKUPSID
, winbindd_lookupsid
, "LOOKUPSID" },
253 { WINBINDD_LOOKUPNAME
, winbindd_lookupname
, "LOOKUPNAME" },
255 /* Lookup related functions */
257 { WINBINDD_SID_TO_UID
, winbindd_sid_to_uid
, "SID_TO_UID" },
258 { WINBINDD_SID_TO_GID
, winbindd_sid_to_gid
, "SID_TO_GID" },
259 { WINBINDD_GID_TO_SID
, winbindd_gid_to_sid
, "GID_TO_SID" },
260 { WINBINDD_UID_TO_SID
, winbindd_uid_to_sid
, "UID_TO_SID" },
264 { WINBINDD_CHECK_MACHACC
, winbindd_check_machine_acct
, "CHECK_MACHACC" },
265 { WINBINDD_PING
, winbindd_ping
, "PING" },
266 { WINBINDD_INFO
, winbindd_info
, "INFO" },
267 { WINBINDD_INTERFACE_VERSION
, winbindd_interface_version
, "INTERFACE_VERSION" },
268 { WINBINDD_DOMAIN_NAME
, winbindd_domain_name
, "DOMAIN_NAME" },
272 { WINBINDD_WINS_BYNAME
, winbindd_wins_byname
, "WINS_BYNAME" },
273 { WINBINDD_WINS_BYIP
, winbindd_wins_byip
, "WINS_BYIP" },
277 { WINBINDD_NUM_CMDS
, NULL
, "NONE" }
280 static void process_request(struct winbindd_cli_state
*state
)
282 struct dispatch_table
*table
= dispatch_table
;
284 /* Free response data - we may be interrupted and receive another
285 command before being able to send this data off. */
287 SAFE_FREE(state
->response
.extra_data
);
289 ZERO_STRUCT(state
->response
);
291 state
->response
.result
= WINBINDD_ERROR
;
292 state
->response
.length
= sizeof(struct winbindd_response
);
294 /* Process command */
296 for (table
= dispatch_table
; table
->fn
; table
++) {
297 if (state
->request
.cmd
== table
->cmd
) {
298 DEBUG(10,("process_request: request fn %s\n", table
->winbindd_cmd_name
));
299 state
->response
.result
= table
->fn(state
);
305 DEBUG(10,("process_request: unknown request fn number %d\n", (int)state
->request
.cmd
));
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
);
327 sock
= accept(accept_sock
, (struct sockaddr
*)&sunaddr
, &len
);
328 } while (sock
== -1 && errno
== EINTR
);
333 DEBUG(6,("accepted socket %d\n", sock
));
335 /* Create new connection structure */
337 if ((state
= (struct winbindd_cli_state
*)
338 malloc(sizeof(*state
))) == NULL
)
344 /* Add to connection list */
346 DLIST_ADD(client_list
, state
);
350 /* Remove a client connection from client connection list */
352 static void remove_client(struct winbindd_cli_state
*state
)
354 /* It's a dead client - hold a funeral */
362 /* Free any getent state */
364 free_getent_state(state
->getpwent_state
);
365 free_getent_state(state
->getgrent_state
);
367 /* We may have some extra data that was not freed if the
368 client was killed unexpectedly */
370 SAFE_FREE(state
->response
.extra_data
);
372 /* Remove from list and free */
374 DLIST_REMOVE(client_list
, state
);
380 /* Process a complete received packet from a client */
382 static void process_packet(struct winbindd_cli_state
*state
)
384 /* Process request */
386 state
->pid
= state
->request
.pid
;
388 process_request(state
);
390 /* Update client state */
392 state
->read_buf_len
= 0;
393 state
->write_buf_len
= sizeof(struct winbindd_response
);
396 /* Read some data from a client connection */
398 static void client_read(struct winbindd_cli_state
*state
)
406 n
= read(state
->sock
, state
->read_buf_len
+
407 (char *)&state
->request
,
408 sizeof(state
->request
) - state
->read_buf_len
);
410 } while (n
== -1 && errno
== EINTR
);
412 if (n
== -1 || n
== 0) {
413 /* Read failed, kill client */
414 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
415 state
->sock
, state
->pid
,
416 (n
== -1) ? strerror(errno
) : "EOF"));
418 state
->finished
= True
;
422 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n
, sizeof(state
->request
) - n
- state
->read_buf_len
));
424 /* Update client state */
426 state
->read_buf_len
+= n
;
429 /* Write some data to a client connection */
431 static void client_write(struct winbindd_cli_state
*state
)
436 /* Write some data */
438 if (!state
->write_extra_data
) {
440 /* Write response structure */
442 data
= (char *)&state
->response
+ sizeof(state
->response
) -
443 state
->write_buf_len
;
447 /* Write extra data */
449 data
= (char *)state
->response
.extra_data
+
450 state
->response
.length
-
451 sizeof(struct winbindd_response
) -
452 state
->write_buf_len
;
456 num_written
= write(state
->sock
, data
, state
->write_buf_len
);
457 } while (num_written
== -1 && errno
== EINTR
);
459 DEBUG(10,("client_write: wrote %d bytes.\n", num_written
));
461 /* Write failed, kill cilent */
463 if (num_written
== -1 || num_written
== 0) {
465 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
466 state
->sock
, state
->pid
,
467 (num_written
== -1) ? strerror(errno
) : "EOF"));
469 state
->finished
= True
;
471 SAFE_FREE(state
->response
.extra_data
);
476 /* Update client state */
478 state
->write_buf_len
-= num_written
;
480 /* Have we written all data? */
482 if (state
->write_buf_len
== 0) {
484 /* Take care of extra data */
486 if (state
->write_extra_data
) {
488 SAFE_FREE(state
->response
.extra_data
);
490 state
->write_extra_data
= False
;
492 DEBUG(10,("client_write: client_write: complete response written.\n"));
494 } else if (state
->response
.length
>
495 sizeof(struct winbindd_response
)) {
497 /* Start writing extra data */
499 state
->write_buf_len
=
500 state
->response
.length
-
501 sizeof(struct winbindd_response
);
503 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state
->write_buf_len
));
505 state
->write_extra_data
= True
;
510 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
511 non-forking, non-threaded model which allows us to handle many
512 simultaneous connections while remaining impervious to many denial of
515 static void process_loop(int accept_sock
)
517 /* We'll be doing this a lot */
520 struct winbindd_cli_state
*state
;
522 int maxfd
= accept_sock
, selret
;
523 struct timeval timeout
;
525 /* Handle messages */
529 /* Free up temporary memory */
532 main_loop_talloc_free();
534 /* Initialise fd lists for select() */
538 FD_SET(accept_sock
, &r_fds
);
540 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
543 /* Set up client readers and writers */
549 /* Dispose of client connection if it is marked as
552 if (state
->finished
) {
553 struct winbindd_cli_state
*next
= state
->next
;
555 remove_client(state
);
560 /* Select requires we know the highest fd used */
562 if (state
->sock
> maxfd
)
565 /* Add fd for reading */
567 if (state
->read_buf_len
!= sizeof(state
->request
))
568 FD_SET(state
->sock
, &r_fds
);
570 /* Add fd for writing */
572 if (state
->write_buf_len
)
573 FD_SET(state
->sock
, &w_fds
);
580 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
585 if ((selret
== -1 && errno
!= EINTR
) || selret
== 0) {
587 /* Select error, something is badly wrong */
593 /* Create a new connection if accept_sock readable */
597 if (FD_ISSET(accept_sock
, &r_fds
))
598 new_connection(accept_sock
);
600 /* Process activity on client connections */
602 for (state
= client_list
; state
; state
= state
->next
) {
604 /* Data available for reading */
606 if (FD_ISSET(state
->sock
, &r_fds
)) {
613 * If we have the start of a
614 * packet, then check the
615 * length field to make sure
616 * the client's not talking
620 if (state
->read_buf_len
>= sizeof(uint32
)
621 && *(uint32
*) &state
->request
!= sizeof(state
->request
)) {
622 DEBUG(0,("process_loop: Invalid request size (%d) send, should be (%d)\n",
623 *(uint32
*) &state
->request
, sizeof(state
->request
)));
625 remove_client(state
);
629 /* A request packet might be
632 if (state
->read_buf_len
==
633 sizeof(state
->request
)) {
634 process_packet(state
);
638 /* Data available for writing */
640 if (FD_ISSET(state
->sock
, &w_fds
))
646 winbindd_check_cache_size(time(NULL
));
649 /* Check signal handling things */
656 /* Flush winbindd cache */
659 reload_services_file(True
);
664 print_winbindd_status();
672 struct winbindd_state server_state
; /* Server state information */
675 static void usage(void)
677 printf("Usage: winbindd [options]\n");
678 printf("\t-i interactive mode\n");
679 printf("\t-n disable cacheing\n");
680 printf("\t-d level set debug level\n");
681 printf("\t-s configfile choose smb.conf location\n");
682 printf("\t-h show this help message\n");
685 int main(int argc
, char **argv
)
687 extern BOOL AllowDebugChange
;
688 extern pstring global_myname
;
689 extern fstring global_myworkgroup
;
690 extern BOOL append_log
;
693 BOOL interactive
= False
;
696 /* glibc (?) likes to print "User defined signal 1" and exit if a
697 SIGUSR[12] is received before a handler is installed */
699 CatchSignal(SIGUSR1
, SIG_IGN
);
700 CatchSignal(SIGUSR2
, SIG_IGN
);
704 charset_initialise(); /* For *&#^%'s sake don't remove this */
706 fault_setup((void (*)(void *))fault_quit
);
708 /* Initialise for running in non-root mode */
712 /* Set environment variable so we don't recursively call ourselves.
713 This may also be useful interactively. */
715 SETENV(WINBINDD_DONT_ENV
, "1", 1);
717 /* Initialise samba/rpc client stuff */
719 while ((opt
= getopt(argc
, argv
, "id:s:nh")) != EOF
) {
722 /* Don't become a daemon */
727 /* disable cacheing */
732 /* Run with specified debug level */
734 DEBUGLEVEL
= atoi(optarg
);
735 AllowDebugChange
= False
;
738 /* Load a different smb.conf file */
740 pstrcpy(servicesf
,optarg
);
748 printf("Unknown option %c\n", (char)opt
);
753 /* Append to log file by default as we are a single process daemon
758 snprintf(logfile
, sizeof(logfile
), "%s/log.winbindd", LOGFILEBASE
);
759 lp_set_logfile(logfile
);
761 setup_logging("winbindd", interactive
);
762 if (!reopen_logs()) {
763 fprintf(stderr
, "Could not open logfile: %s\n", logfile
);
764 fprintf(stderr
, "Continuing in the hope that that is OK\n");
767 DEBUG(1, ("winbindd version %s started.\n", VERSION
) );
768 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
770 if (!reload_services_file(False
)) {
771 DEBUG(0, ("error opening config file\n"));
775 codepage_initialise(lp_client_code_page());
779 if (!*global_myname
) {
782 fstrcpy(global_myname
, myhostname());
783 p
= strchr(global_myname
, '.');
788 fstrcpy(global_myworkgroup
, lp_workgroup());
792 pidfile_create("winbindd");
798 * If we're interactive we want to set our own process group for
802 setpgid( (pid_t
)0, (pid_t
)0);
807 if (!secrets_init()) {
809 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
814 /* Get list of domains we look up requests for. This includes the
815 domain which we are a member of as well as any trusted
820 ZERO_STRUCT(server_state
);
822 /* Winbind daemon initialisation */
824 if (!winbindd_param_init())
827 if (!winbindd_idmap_init())
830 /* Unblock all signals we are interested in as they may have been
831 blocked by the parent process. */
833 BlockSignals(False
, SIGINT
);
834 BlockSignals(False
, SIGQUIT
);
835 BlockSignals(False
, SIGTERM
);
836 BlockSignals(False
, SIGUSR1
);
837 BlockSignals(False
, SIGUSR2
);
838 BlockSignals(False
, SIGHUP
);
840 /* Setup signal handlers */
842 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
843 CatchSignal(SIGQUIT
, termination_handler
);
844 CatchSignal(SIGTERM
, termination_handler
);
846 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
848 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
849 CatchSignal(SIGHUP
, sighup_handler
);
851 /* Initialise messaging system */
853 if (!message_init()) {
854 DEBUG(0, ("unable to initialise messaging system\n"));
858 /* Create UNIX domain socket */
860 if ((accept_sock
= create_sock()) == -1) {
861 DEBUG(0, ("failed to create socket\n"));
865 /* Loop waiting for requests */
867 process_loop(accept_sock
);
870 uni_group_cache_shutdown();