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
);
300 if (state
->response
.nt_status
)
301 DEBUG(10, ("returning extended error 0x%08x\n",
302 state
->response
.nt_status
));
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
)
409 n
= read(state
->sock
, state
->read_buf_len
+
410 (char *)&state
->request
,
411 sizeof(state
->request
) - state
->read_buf_len
);
413 } while (n
== -1 && errno
== EINTR
);
415 if (n
== -1 || n
== 0) {
416 /* Read failed, kill client */
417 DEBUG(5,("read failed on sock %d, pid %d: %s\n",
418 state
->sock
, state
->pid
,
419 (n
== -1) ? strerror(errno
) : "EOF"));
421 state
->finished
= True
;
425 DEBUG(10,("client_read: read %d bytes. Need %d more for a full request.\n", n
, sizeof(state
->request
) - n
- state
->read_buf_len
));
427 /* Update client state */
429 state
->read_buf_len
+= n
;
432 /* Write some data to a client connection */
434 static void client_write(struct winbindd_cli_state
*state
)
439 /* Write some data */
441 if (!state
->write_extra_data
) {
443 /* Write response structure */
445 data
= (char *)&state
->response
+ sizeof(state
->response
) -
446 state
->write_buf_len
;
450 /* Write extra data */
452 data
= (char *)state
->response
.extra_data
+
453 state
->response
.length
-
454 sizeof(struct winbindd_response
) -
455 state
->write_buf_len
;
459 num_written
= write(state
->sock
, data
, state
->write_buf_len
);
460 } while (num_written
== -1 && errno
== EINTR
);
462 DEBUG(10,("client_write: wrote %d bytes.\n", num_written
));
464 /* Write failed, kill cilent */
466 if (num_written
== -1 || num_written
== 0) {
468 DEBUG(3,("write failed on sock %d, pid %d: %s\n",
469 state
->sock
, state
->pid
,
470 (num_written
== -1) ? strerror(errno
) : "EOF"));
472 state
->finished
= True
;
474 SAFE_FREE(state
->response
.extra_data
);
479 /* Update client state */
481 state
->write_buf_len
-= num_written
;
483 /* Have we written all data? */
485 if (state
->write_buf_len
== 0) {
487 /* Take care of extra data */
489 if (state
->write_extra_data
) {
491 SAFE_FREE(state
->response
.extra_data
);
493 state
->write_extra_data
= False
;
495 DEBUG(10,("client_write: client_write: complete response written.\n"));
497 } else if (state
->response
.length
>
498 sizeof(struct winbindd_response
)) {
500 /* Start writing extra data */
502 state
->write_buf_len
=
503 state
->response
.length
-
504 sizeof(struct winbindd_response
);
506 DEBUG(10,("client_write: need to write %d extra data bytes.\n", (int)state
->write_buf_len
));
508 state
->write_extra_data
= True
;
513 /* Process incoming clients on accept_sock. We use a tricky non-blocking,
514 non-forking, non-threaded model which allows us to handle many
515 simultaneous connections while remaining impervious to many denial of
518 static void process_loop(int accept_sock
)
520 /* We'll be doing this a lot */
523 struct winbindd_cli_state
*state
;
525 int maxfd
= accept_sock
, selret
;
526 struct timeval timeout
;
528 /* Handle messages */
532 /* Free up temporary memory */
535 main_loop_talloc_free();
537 /* Initialise fd lists for select() */
541 FD_SET(accept_sock
, &r_fds
);
543 timeout
.tv_sec
= WINBINDD_ESTABLISH_LOOP
;
546 /* Set up client readers and writers */
552 /* Dispose of client connection if it is marked as
555 if (state
->finished
) {
556 struct winbindd_cli_state
*next
= state
->next
;
558 remove_client(state
);
563 /* Select requires we know the highest fd used */
565 if (state
->sock
> maxfd
)
568 /* Add fd for reading */
570 if (state
->read_buf_len
!= sizeof(state
->request
))
571 FD_SET(state
->sock
, &r_fds
);
573 /* Add fd for writing */
575 if (state
->write_buf_len
)
576 FD_SET(state
->sock
, &w_fds
);
583 selret
= sys_select(maxfd
+ 1, &r_fds
, &w_fds
, NULL
, &timeout
);
588 if ((selret
== -1 && errno
!= EINTR
) || selret
== 0) {
590 /* Select error, something is badly wrong */
596 /* Create a new connection if accept_sock readable */
600 if (FD_ISSET(accept_sock
, &r_fds
))
601 new_connection(accept_sock
);
603 /* Process activity on client connections */
605 for (state
= client_list
; state
; state
= state
->next
) {
607 /* Data available for reading */
609 if (FD_ISSET(state
->sock
, &r_fds
)) {
616 * If we have the start of a
617 * packet, then check the
618 * length field to make sure
619 * the client's not talking
623 if (state
->read_buf_len
>= sizeof(uint32
)
624 && *(uint32
*) &state
->request
!= sizeof(state
->request
)) {
625 DEBUG(0,("process_loop: Invalid request size (%d) sent, should be (%d)\n",
626 *(uint32
*) &state
->request
, sizeof(state
->request
)));
628 remove_client(state
);
632 /* A request packet might be
635 if (state
->read_buf_len
==
636 sizeof(state
->request
)) {
637 process_packet(state
);
641 /* Data available for writing */
643 if (FD_ISSET(state
->sock
, &w_fds
))
649 winbindd_check_cache_size(time(NULL
));
652 /* Check signal handling things */
659 /* Flush winbindd cache */
662 reload_services_file(True
);
663 #if 0 /* Notised at present. */
672 print_winbindd_status();
680 struct winbindd_state server_state
; /* Server state information */
683 static void usage(void)
685 printf("Usage: winbindd [options]\n");
686 printf("Version: %s\n", VERSION
);
687 printf("\t-i interactive mode\n");
688 printf("\t-n disable cacheing\n");
689 printf("\t-d level set debug level\n");
690 printf("\t-s configfile choose smb.conf location\n");
691 printf("\t-h show this help message\n");
694 int main(int argc
, char **argv
)
696 extern BOOL AllowDebugChange
;
697 extern pstring global_myname
;
698 extern fstring global_myworkgroup
;
699 extern BOOL append_log
;
702 BOOL interactive
= False
;
705 /* glibc (?) likes to print "User defined signal 1" and exit if a
706 SIGUSR[12] is received before a handler is installed */
708 CatchSignal(SIGUSR1
, SIG_IGN
);
709 CatchSignal(SIGUSR2
, SIG_IGN
);
713 charset_initialise(); /* For *&#^%'s sake don't remove this */
715 fault_setup((void (*)(void *))fault_quit
);
717 /* Initialise for running in non-root mode */
721 /* Set environment variable so we don't recursively call ourselves.
722 This may also be useful interactively. */
724 SETENV(WINBINDD_DONT_ENV
, "1", 1);
726 /* Initialise samba/rpc client stuff */
728 while ((opt
= getopt(argc
, argv
, "id:s:nh")) != EOF
) {
731 /* Don't become a daemon */
736 /* disable cacheing */
741 /* Run with specified debug level */
743 DEBUGLEVEL
= atoi(optarg
);
744 AllowDebugChange
= False
;
747 /* Load a different smb.conf file */
749 pstrcpy(servicesf
,optarg
);
757 printf("Unknown option %c\n", (char)opt
);
762 /* Append to log file by default as we are a single process daemon
767 snprintf(logfile
, sizeof(logfile
), "%s/log.winbindd", LOGFILEBASE
);
768 lp_set_logfile(logfile
);
770 setup_logging("winbindd", interactive
);
771 if (!reopen_logs()) {
772 fprintf(stderr
, "Could not open logfile: %s\n", logfile
);
773 fprintf(stderr
, "Continuing in the hope that that is OK\n");
776 DEBUG(1, ("winbindd version %s started.\n", VERSION
) );
777 DEBUGADD( 1, ( "Copyright The Samba Team 2000-2001\n" ) );
779 if (!reload_services_file(False
)) {
780 DEBUG(0, ("error opening config file\n"));
784 codepage_initialise(lp_client_code_page());
788 if (!*global_myname
) {
791 fstrcpy(global_myname
, myhostname());
792 p
= strchr(global_myname
, '.');
797 fstrcpy(global_myworkgroup
, lp_workgroup());
801 pidfile_create("winbindd");
807 * If we're interactive we want to set our own process group for
811 setpgid( (pid_t
)0, (pid_t
)0);
816 if (!secrets_init()) {
818 DEBUG(0,("Could not initialize domain trust account secrets. Giving up\n"));
823 #if 0 /* Notised at present. */
824 namecache_enable(); /* Enable netbios namecache */
827 /* Get list of domains we look up requests for. This includes the
828 domain which we are a member of as well as any trusted
833 ZERO_STRUCT(server_state
);
835 /* Winbind daemon initialisation */
837 if (!winbindd_param_init())
840 if (!winbindd_idmap_init())
843 /* Unblock all signals we are interested in as they may have been
844 blocked by the parent process. */
846 BlockSignals(False
, SIGINT
);
847 BlockSignals(False
, SIGQUIT
);
848 BlockSignals(False
, SIGTERM
);
849 BlockSignals(False
, SIGUSR1
);
850 BlockSignals(False
, SIGUSR2
);
851 BlockSignals(False
, SIGHUP
);
853 /* Setup signal handlers */
855 CatchSignal(SIGINT
, termination_handler
); /* Exit on these sigs */
856 CatchSignal(SIGQUIT
, termination_handler
);
857 CatchSignal(SIGTERM
, termination_handler
);
859 CatchSignal(SIGPIPE
, SIG_IGN
); /* Ignore sigpipe */
861 CatchSignal(SIGUSR2
, sigusr2_handler
); /* Debugging sigs */
862 CatchSignal(SIGHUP
, sighup_handler
);
864 /* Initialise messaging system */
866 if (!message_init()) {
867 DEBUG(0, ("unable to initialise messaging system\n"));
871 /* Create UNIX domain socket */
873 if ((accept_sock
= create_sock()) == -1) {
874 DEBUG(0, ("failed to create socket\n"));
878 /* Loop waiting for requests */
880 process_loop(accept_sock
);
883 uni_group_cache_shutdown();