1996-05-04 11:50:46 +04:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1996-05-04 11:50:46 +04:00
Main SMB server routines
2002-01-15 05:15:58 +03:00
Copyright ( C ) Andrew Tridgell 1992 - 1998
Copyright ( C ) Martin Pool 2002
2002-11-09 20:08:58 +03:00
Copyright ( C ) Jelmer Vernooij 2002
1996-05-04 11:50:46 +04:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
1998-04-25 05:12:08 +04:00
extern fstring global_myworkgroup ;
1998-04-30 02:27:26 +04:00
extern pstring global_myname ;
1996-05-04 11:50:46 +04:00
1996-12-10 21:00:22 +03:00
int am_parent = 1 ;
1996-05-04 11:50:46 +04:00
/* the last message the was processed */
int last_message = - 1 ;
/* a useful macro to debug the last message processed */
# define LAST_MESSAGE() smb_fn_name(last_message)
extern pstring user_socket_options ;
2002-09-25 19:19:00 +04:00
extern SIG_ATOMIC_T got_sig_term ;
extern SIG_ATOMIC_T reload_after_sighup ;
1996-05-04 11:50:46 +04:00
1998-07-29 07:08:05 +04:00
# ifdef WITH_DFS
1998-03-04 04:50:47 +03:00
extern int dcelogin_atmost_once ;
1998-07-29 07:08:05 +04:00
# endif /* WITH_DFS */
1998-03-04 04:50:47 +03:00
2000-04-11 17:55:53 +04:00
/* really we should have a top level context structure that has the
client file descriptor as an element . That would require a major rewrite : (
the following 2 functions are an alternative - they make the file
descriptor private to smbd
*/
2000-05-10 02:05:39 +04:00
static int server_fd = - 1 ;
2000-04-11 17:55:53 +04:00
int smbd_server_fd ( void )
{
return server_fd ;
}
2002-07-15 14:35:28 +04:00
static void smbd_set_server_fd ( int fd )
2000-04-11 17:55:53 +04:00
{
server_fd = fd ;
client_setfd ( fd ) ;
}
1996-12-10 21:00:22 +03:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Terminate signal .
1996-12-10 21:00:22 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
static void sig_term ( void )
{
got_sig_term = 1 ;
sys_select_signal ( ) ;
}
/****************************************************************************
Catch a sighup .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void sig_hup ( int sig )
1996-12-10 21:00:22 +03:00
{
2002-03-14 05:15:08 +03:00
reload_after_sighup = 1 ;
sys_select_signal ( ) ;
1996-12-10 21:00:22 +03:00
}
1998-08-17 18:11:44 +04:00
1996-12-10 21:00:22 +03:00
/****************************************************************************
Send a SIGTERM to our process group .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
1998-09-05 09:07:05 +04:00
static void killkids ( void )
1996-12-10 21:00:22 +03:00
{
1998-08-17 18:11:44 +04:00
if ( am_parent ) kill ( 0 , SIGTERM ) ;
1996-12-10 21:00:22 +03:00
}
1996-05-04 11:50:46 +04:00
2001-08-28 10:34:53 +04:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Process a sam sync message - not sure whether to do this here or
somewhere else .
2001-08-28 10:34:53 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
2002-01-09 08:27:59 +03:00
static void msg_sam_sync ( int UNUSED ( msg_type ) , pid_t UNUSED ( pid ) ,
void * UNUSED ( buf ) , size_t UNUSED ( len ) )
2001-08-28 10:34:53 +04:00
{
DEBUG ( 10 , ( " ** sam sync message received, ignoring \n " ) ) ;
}
/****************************************************************************
2002-03-14 05:15:08 +03:00
Process a sam sync replicate message - not sure whether to do this here or
somewhere else .
2001-08-28 10:34:53 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
2001-08-28 10:34:53 +04:00
static void msg_sam_repl ( int msg_type , pid_t pid , void * buf , size_t len )
{
uint32 low_serial ;
if ( len ! = sizeof ( uint32 ) )
return ;
low_serial = * ( ( uint32 * ) buf ) ;
DEBUG ( 3 , ( " received sam replication message, serial = 0x%04x \n " ,
low_serial ) ) ;
}
1997-09-23 23:19:06 +04:00
1997-09-30 06:38:19 +04:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Open the socket communication - inetd .
1997-09-30 06:38:19 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
1998-08-17 18:11:44 +04:00
static BOOL open_sockets_inetd ( void )
1997-09-30 06:38:19 +04:00
{
1998-08-17 18:11:44 +04:00
/* Started from inetd. fd 0 is the socket. */
/* We will abort gracefully when the client or remote system
goes away */
2000-04-11 17:55:53 +04:00
smbd_set_server_fd ( dup ( 0 ) ) ;
1998-08-17 18:11:44 +04:00
/* close our standard file descriptors */
2002-08-17 19:27:10 +04:00
close_low_fds ( False ) ; /* Don't close stderr */
1998-08-17 18:11:44 +04:00
2000-04-11 17:55:53 +04:00
set_socket_options ( smbd_server_fd ( ) , " SO_KEEPALIVE " ) ;
2002-01-16 05:20:34 +03:00
set_socket_options ( smbd_server_fd ( ) , user_socket_options ) ;
1998-08-17 18:11:44 +04:00
return True ;
}
1997-10-02 03:32:22 +04:00
2002-03-29 17:28:38 +03:00
static void msg_exit_server ( int msg_type , pid_t src , void * buf , size_t len )
{
exit_server ( " Got a SHUTDOWN message " ) ;
}
1997-09-25 04:25:44 +04:00
1998-08-17 18:11:44 +04:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Open the socket communication .
1998-08-17 18:11:44 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
2002-08-17 19:27:10 +04:00
static BOOL open_sockets_smbd ( BOOL is_daemon , const char * smb_ports )
1998-08-17 18:11:44 +04:00
{
int num_interfaces = iface_count ( ) ;
2002-08-17 19:27:10 +04:00
int num_sockets = 0 ;
1998-08-17 18:11:44 +04:00
int fd_listenset [ FD_SETSIZE ] ;
fd_set listen_set ;
int s ;
int i ;
2002-08-17 19:27:10 +04:00
char * ports ;
1998-08-17 18:11:44 +04:00
if ( ! is_daemon ) {
return open_sockets_inetd ( ) ;
}
1998-08-17 17:11:34 +04:00
# ifdef HAVE_ATEXIT
1998-08-17 18:11:44 +04:00
{
static int atexit_set ;
if ( atexit_set = = 0 ) {
atexit_set = 1 ;
atexit ( killkids ) ;
}
}
1998-08-17 17:11:34 +04:00
# endif
1997-10-02 07:26:07 +04:00
1998-08-17 18:11:44 +04:00
/* Stop zombies */
CatchChild ( ) ;
2002-08-17 19:27:10 +04:00
1998-08-17 18:11:44 +04:00
FD_ZERO ( & listen_set ) ;
2002-08-17 19:27:10 +04:00
/* use a reasonable default set of ports - listing on 445 and 139 */
if ( ! smb_ports ) {
ports = lp_smb_ports ( ) ;
if ( ! ports | | ! * ports ) {
ports = SMB_PORTS ;
}
ports = strdup ( ports ) ;
} else {
ports = strdup ( smb_ports ) ;
}
if ( lp_interfaces ( ) & & lp_bind_interfaces_only ( ) ) {
1998-08-17 18:11:44 +04:00
/* We have been given an interfaces line, and been
told to only bind to those interfaces . Create a
socket per interface and bind to only these .
*/
/* Now open a listen socket for each of the
interfaces . */
for ( i = 0 ; i < num_interfaces ; i + + ) {
struct in_addr * ifip = iface_n_ip ( i ) ;
2002-08-17 19:27:10 +04:00
fstring tok ;
char * ptr ;
1998-08-17 18:11:44 +04:00
if ( ifip = = NULL ) {
2002-08-17 19:27:10 +04:00
DEBUG ( 0 , ( " open_sockets_smbd: interface %d has NULL IP address ! \n " , i ) ) ;
1998-08-17 18:11:44 +04:00
continue ;
}
2001-08-09 16:52:05 +04:00
2002-08-17 19:27:10 +04:00
for ( ptr = ports ; next_token ( & ptr , tok , NULL , sizeof ( tok ) ) ; ) {
unsigned port = atoi ( tok ) ;
if ( port = = 0 ) continue ;
s = fd_listenset [ num_sockets ] = open_socket_in ( SOCK_STREAM , port , 0 , ifip - > s_addr , True ) ;
if ( s = = - 1 )
return False ;
/* ready to listen */
set_socket_options ( s , " SO_KEEPALIVE " ) ;
set_socket_options ( s , user_socket_options ) ;
2001-08-09 16:52:05 +04:00
2002-08-17 19:27:10 +04:00
if ( listen ( s , 5 ) = = - 1 ) {
DEBUG ( 0 , ( " listen: %s \n " , strerror ( errno ) ) ) ;
close ( s ) ;
return False ;
}
FD_SET ( s , & listen_set ) ;
num_sockets + + ;
if ( num_sockets > = FD_SETSIZE ) {
DEBUG ( 0 , ( " open_sockets_smbd: Too many sockets to bind to \n " ) ) ;
return False ;
}
1999-12-13 16:27:58 +03:00
}
1998-08-17 18:11:44 +04:00
}
} else {
/* Just bind to 0.0.0.0 - accept connections
from anywhere . */
2002-08-17 19:27:10 +04:00
fstring tok ;
char * ptr ;
1998-08-17 18:11:44 +04:00
num_interfaces = 1 ;
2002-08-17 19:27:10 +04:00
for ( ptr = ports ; next_token ( & ptr , tok , NULL , sizeof ( tok ) ) ; ) {
unsigned port = atoi ( tok ) ;
if ( port = = 0 ) continue ;
/* open an incoming socket */
s = open_socket_in ( SOCK_STREAM , port , 0 ,
interpret_addr ( lp_socket_address ( ) ) , True ) ;
if ( s = = - 1 )
return ( False ) ;
1999-12-13 16:27:58 +03:00
2002-08-17 19:27:10 +04:00
/* ready to listen */
set_socket_options ( s , " SO_KEEPALIVE " ) ;
set_socket_options ( s , user_socket_options ) ;
if ( listen ( s , 5 ) = = - 1 ) {
DEBUG ( 0 , ( " open_sockets_smbd: listen: %s \n " ,
strerror ( errno ) ) ) ;
close ( s ) ;
return False ;
}
fd_listenset [ num_sockets ] = s ;
FD_SET ( s , & listen_set ) ;
num_sockets + + ;
if ( num_sockets > = FD_SETSIZE ) {
DEBUG ( 0 , ( " open_sockets_smbd: Too many sockets to bind to \n " ) ) ;
return False ;
}
1999-12-13 16:27:58 +03:00
}
1998-08-17 18:11:44 +04:00
}
2002-08-17 19:27:10 +04:00
SAFE_FREE ( ports ) ;
2001-08-28 10:34:53 +04:00
/* Listen to messages */
message_register ( MSG_SMB_SAM_SYNC , msg_sam_sync ) ;
message_register ( MSG_SMB_SAM_REPL , msg_sam_repl ) ;
2002-03-29 17:28:38 +03:00
message_register ( MSG_SHUTDOWN , msg_exit_server ) ;
2001-08-28 10:34:53 +04:00
1998-08-17 18:11:44 +04:00
/* now accept incoming connections - forking a new process
for each incoming connection */
DEBUG ( 2 , ( " waiting for a connection \n " ) ) ;
while ( 1 ) {
fd_set lfds ;
int num ;
2000-11-22 22:42:54 +03:00
/* Free up temporary memory from the main smbd. */
lp_talloc_free ( ) ;
2000-11-28 00:37:07 +03:00
/* Ensure we respond to PING and DEBUG messages from the main smbd. */
message_dispatch ( ) ;
1998-08-17 18:11:44 +04:00
memcpy ( ( char * ) & lfds , ( char * ) & listen_set ,
sizeof ( listen_set ) ) ;
2002-02-01 02:26:12 +03:00
num = sys_select ( FD_SETSIZE , & lfds , NULL , NULL , NULL ) ;
1998-08-17 18:11:44 +04:00
2000-07-24 10:20:13 +04:00
if ( num = = - 1 & & errno = = EINTR ) {
2002-03-14 05:15:08 +03:00
if ( got_sig_term ) {
exit_server ( " Caught TERM signal " ) ;
}
2000-07-24 10:20:13 +04:00
/* check for sighup processing */
if ( reload_after_sighup ) {
2001-10-19 00:15:12 +04:00
change_to_root_user ( ) ;
2000-07-24 10:20:13 +04:00
DEBUG ( 1 , ( " Reloading services after SIGHUP \n " ) ) ;
reload_services ( False ) ;
2002-03-14 05:15:08 +03:00
reload_after_sighup = 0 ;
2000-07-24 10:20:13 +04:00
}
1998-08-17 18:11:44 +04:00
continue ;
2000-07-24 10:20:13 +04:00
}
1998-08-17 18:11:44 +04:00
1999-12-13 16:27:58 +03:00
/* check if we need to reload services */
check_reload ( time ( NULL ) ) ;
1998-08-17 18:11:44 +04:00
/* Find the sockets that are read-ready -
accept on these . */
for ( ; num > 0 ; num - - ) {
struct sockaddr addr ;
2001-06-25 06:53:13 +04:00
socklen_t in_addrlen = sizeof ( addr ) ;
1998-08-17 18:11:44 +04:00
s = - 1 ;
2002-08-17 19:27:10 +04:00
for ( i = 0 ; i < num_sockets ; i + + ) {
1999-12-13 16:27:58 +03:00
if ( FD_ISSET ( fd_listenset [ i ] , & lfds ) ) {
s = fd_listenset [ i ] ;
/* Clear this so we don't look
at it again . */
FD_CLR ( fd_listenset [ i ] , & lfds ) ;
1999-09-08 23:37:45 +04:00
break ;
}
1998-08-17 18:11:44 +04:00
}
2000-04-11 17:55:53 +04:00
smbd_set_server_fd ( accept ( s , & addr , & in_addrlen ) ) ;
1998-08-17 18:11:44 +04:00
2000-04-11 17:55:53 +04:00
if ( smbd_server_fd ( ) = = - 1 & & errno = = EINTR )
1998-08-17 18:11:44 +04:00
continue ;
2000-04-11 17:55:53 +04:00
if ( smbd_server_fd ( ) = = - 1 ) {
2002-08-17 19:27:10 +04:00
DEBUG ( 0 , ( " open_sockets_smbd: accept: %s \n " ,
1998-08-17 18:11:44 +04:00
strerror ( errno ) ) ) ;
continue ;
}
2000-05-02 06:23:41 +04:00
if ( smbd_server_fd ( ) ! = - 1 & & sys_fork ( ) = = 0 ) {
1998-08-17 18:11:44 +04:00
/* Child code ... */
/* close the listening socket(s) */
2002-08-17 19:27:10 +04:00
for ( i = 0 ; i < num_sockets ; i + + )
1998-08-17 18:11:44 +04:00
close ( fd_listenset [ i ] ) ;
/* close our standard file
descriptors */
2002-08-17 19:27:10 +04:00
close_low_fds ( False ) ;
1998-08-17 18:11:44 +04:00
am_parent = 0 ;
2000-04-11 17:55:53 +04:00
set_socket_options ( smbd_server_fd ( ) , " SO_KEEPALIVE " ) ;
2002-01-16 05:20:34 +03:00
set_socket_options ( smbd_server_fd ( ) , user_socket_options ) ;
1998-08-17 18:11:44 +04:00
2002-08-17 19:27:10 +04:00
/* this is needed so that we get decent entries
in smbstatus for port 445 connects */
set_remote_machine_name ( get_socket_addr ( smbd_server_fd ( ) ) ) ;
1998-08-17 18:11:44 +04:00
/* Reset global variables in util.c so
that client substitutions will be
done correctly in the process . */
reset_globals_after_fork ( ) ;
1998-09-26 03:40:49 +04:00
2001-09-19 09:43:15 +04:00
/* tdb needs special fork handling */
2002-11-01 02:41:00 +03:00
if ( tdb_reopen_all ( ) = = - 1 ) {
DEBUG ( 0 , ( " tdb_reopen_all failed. \n " ) ) ;
return False ;
}
2001-09-19 09:43:15 +04:00
1998-08-17 18:11:44 +04:00
return True ;
}
/* The parent doesn't need this socket */
2000-04-11 17:55:53 +04:00
close ( smbd_server_fd ( ) ) ;
1998-08-17 18:11:44 +04:00
2001-05-08 04:03:45 +04:00
/* Sun May 6 18:56:14 2001 ackley@cs.unm.edu:
Clear the closed fd info out of server_fd - -
and more importantly , out of client_fd in
util_sock . c , to avoid a possible
getpeername failure if we reopen the logs
and use % I in the filename .
*/
smbd_set_server_fd ( - 1 ) ;
1998-08-17 18:11:44 +04:00
/* Force parent to check log size after
* spawning child . Fix from
* klausr @ ITAP . Physik . Uni - Stuttgart . De . The
* parent smbd will log to logserver . smb . It
* writes only two messages for each child
* started / finished . But each child writes ,
* say , 50 messages also in logserver . smb ,
* begining with the debug_count of the
* parent , before the child opens its own log
* file logserver . client . In a worst case
* scenario the size of logserver . smb would be
* checked after about 50 * 50 = 2500 messages
* ( ca . 100 kb ) .
* */
force_check_log_size ( ) ;
1998-08-17 17:11:34 +04:00
1998-08-17 18:11:44 +04:00
} /* end for num */
} /* end while 1 */
1998-08-28 00:38:53 +04:00
/* NOTREACHED return True; */
1997-11-19 02:30:49 +03:00
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Reload the services file .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
1996-05-04 11:50:46 +04:00
BOOL reload_services ( BOOL test )
{
1998-08-14 21:38:29 +04:00
BOOL ret ;
2000-03-09 01:14:30 +03:00
1998-08-14 21:38:29 +04:00
if ( lp_loaded ( ) ) {
pstring fname ;
pstrcpy ( fname , lp_configfile ( ) ) ;
2002-01-16 05:20:34 +03:00
if ( file_exist ( fname , NULL ) & &
! strcsequal ( fname , dyn_CONFIGFILE ) ) {
pstrcpy ( dyn_CONFIGFILE , fname ) ;
1998-08-17 17:11:34 +04:00
test = False ;
1998-08-14 21:38:29 +04:00
}
}
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
reopen_logs ( ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
if ( test & & ! lp_file_list_changed ( ) )
return ( True ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
lp_killunused ( conn_snum_used ) ;
2000-03-09 01:14:30 +03:00
2002-01-16 05:20:34 +03:00
ret = lp_load ( dyn_CONFIGFILE , False , False , True ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
load_printers ( ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
/* perhaps the config filename is now set */
if ( ! test )
reload_services ( True ) ;
1997-10-08 04:21:39 +04:00
1998-08-17 17:11:34 +04:00
reopen_logs ( ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 17:11:34 +04:00
load_interfaces ( ) ;
1996-05-04 11:50:46 +04:00
1998-08-14 21:38:29 +04:00
{
2000-04-11 17:55:53 +04:00
if ( smbd_server_fd ( ) ! = - 1 ) {
set_socket_options ( smbd_server_fd ( ) , " SO_KEEPALIVE " ) ;
2002-01-16 05:20:34 +03:00
set_socket_options ( smbd_server_fd ( ) , user_socket_options ) ;
1998-08-17 17:11:34 +04:00
}
1998-08-14 21:38:29 +04:00
}
1998-01-31 17:31:08 +03:00
2002-04-11 06:20:56 +04:00
mangle_reset_cache ( ) ;
2000-05-10 15:45:16 +04:00
reset_stat_cache ( ) ;
1998-01-31 17:31:08 +03:00
1998-08-17 17:11:34 +04:00
/* this forces service parameters to be flushed */
2001-10-19 00:15:12 +04:00
set_current_service ( NULL , True ) ;
1998-01-31 17:31:08 +03:00
1998-08-17 17:11:34 +04:00
return ( ret ) ;
1998-01-31 17:31:08 +03:00
}
1996-05-04 11:50:46 +04:00
# if DUMP_CORE
/*******************************************************************
prepare to dump a core file - carefully !
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL dump_core ( void )
{
1998-08-17 18:11:44 +04:00
char * p ;
pstring dname ;
2002-01-15 05:15:58 +03:00
2001-12-21 01:27:05 +03:00
pstrcpy ( dname , lp_logfile ( ) ) ;
2002-01-16 05:20:34 +03:00
if ( ( p = strrchr_m ( dname , ' / ' ) ) ) * p = 0 ;
1998-08-17 18:11:44 +04:00
pstrcat ( dname , " /corefiles " ) ;
2002-01-16 05:20:34 +03:00
mkdir ( dname , 0700 ) ;
sys_chown ( dname , getuid ( ) , getgid ( ) ) ;
chmod ( dname , 0700 ) ;
if ( chdir ( dname ) ) return ( False ) ;
1998-08-17 18:11:44 +04:00
umask ( ~ ( 0700 ) ) ;
1996-05-04 11:50:46 +04:00
1998-07-29 07:08:05 +04:00
# ifdef HAVE_GETRLIMIT
1996-05-04 11:50:46 +04:00
# ifdef RLIMIT_CORE
1998-08-17 18:11:44 +04:00
{
struct rlimit rlp ;
getrlimit ( RLIMIT_CORE , & rlp ) ;
rlp . rlim_cur = MAX ( 4 * 1024 * 1024 , rlp . rlim_cur ) ;
setrlimit ( RLIMIT_CORE , & rlp ) ;
getrlimit ( RLIMIT_CORE , & rlp ) ;
DEBUG ( 3 , ( " Core limits now %d %d \n " ,
( int ) rlp . rlim_cur , ( int ) rlp . rlim_max ) ) ;
}
1996-05-04 11:50:46 +04:00
# endif
# endif
2002-01-16 05:20:34 +03:00
DEBUG ( 0 , ( " Dumping core in %s \n " , dname ) ) ;
1998-08-17 18:11:44 +04:00
abort ( ) ;
return ( True ) ;
1996-05-04 11:50:46 +04:00
}
# endif
2001-02-12 19:18:02 +03:00
/****************************************************************************
update the current smbd process count
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2001-05-25 04:48:28 +04:00
2001-02-12 19:18:02 +03:00
static void decrement_smbd_process_count ( void )
{
2002-01-09 05:35:08 +03:00
int32 total_smbds ;
2001-02-12 19:18:02 +03:00
if ( lp_max_smbd_processes ( ) ) {
2001-05-25 04:48:28 +04:00
total_smbds = 0 ;
2002-01-09 05:35:08 +03:00
tdb_change_int32_atomic ( conn_tdb_ctx ( ) , " INFO/total_smbds " , & total_smbds , - 1 ) ;
2001-02-12 19:18:02 +03:00
}
}
1996-05-04 11:50:46 +04:00
/****************************************************************************
2002-03-14 05:15:08 +03:00
Exit the server .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
1996-05-04 11:50:46 +04:00
void exit_server ( char * reason )
{
1998-08-17 18:11:44 +04:00
static int firsttime = 1 ;
extern char * last_inbuf ;
2002-01-05 07:55:41 +03:00
extern struct auth_context * negprot_global_auth_context ;
1996-05-04 11:50:46 +04:00
2001-10-19 00:15:12 +04:00
if ( ! firsttime )
exit ( 0 ) ;
1998-08-17 18:11:44 +04:00
firsttime = 0 ;
1998-08-17 10:13:32 +04:00
2001-10-19 00:15:12 +04:00
change_to_root_user ( ) ;
1998-08-17 18:11:44 +04:00
DEBUG ( 2 , ( " Closing connections \n " ) ) ;
2002-01-05 07:55:41 +03:00
if ( negprot_global_auth_context ) {
2002-01-09 10:52:51 +03:00
( negprot_global_auth_context - > free ) ( & negprot_global_auth_context ) ;
2002-01-05 07:55:41 +03:00
}
This is another rather major change to the samba authenticaion
subystem.
The particular aim is to modularized the interface - so that we
can have arbitrary password back-ends.
This code adds one such back-end, a 'winbind' module to authenticate
against the winbind_auth_crap functionality. While fully-functional
this code is mainly useful as a demonstration, because we don't get
back the info3 as we would for direct ntdomain authentication.
This commit introduced the new 'auth methods' parameter, in the
spirit of the 'auth order' discussed on the lists. It is renamed
because not all the methods may be consulted, even if previous
methods fail - they may not have a suitable challenge for example.
Also, we have a 'local' authentication method, for old-style
'unix if plaintext, sam if encrypted' authentication and a
'guest' module to handle guest logins in a single place.
While this current design is not ideal, I feel that it does
provide a better infrastructure than the current design, and can
be built upon.
The following parameters have changed:
- use rhosts =
This has been replaced by the 'rhosts' authentication method,
and can be specified like 'auth methods = guest rhosts'
- hosts equiv =
This needs both this parameter and an 'auth methods' entry
to be effective. (auth methods = guest hostsequiv ....)
- plaintext to smbpasswd =
This is replaced by specifying 'sam' rather than 'local'
in the auth methods.
The security = parameter is unchanged, and now provides defaults
for the 'auth methods' parameter.
The available auth methods are:
guest
rhosts
hostsequiv
sam (passdb direct hash access)
unix (PAM, crypt() etc)
local (the combination of the above, based on encryption)
smbserver (old security=server)
ntdomain (old security=domain)
winbind (use winbind to cache DC connections)
Assistance in testing, or the production of new and interesting
authentication modules is always appreciated.
Andrew Bartlett
(This used to be commit 8d31eae52a9757739711dbb82035a4dfe6b40c99)
2001-11-24 15:12:38 +03:00
1998-08-17 18:11:44 +04:00
conn_close_all ( ) ;
1998-08-17 10:13:32 +04:00
2001-04-18 20:41:04 +04:00
invalidate_all_vuids ( ) ;
2002-09-25 19:19:00 +04:00
print_notify_send_messages ( ) ;
2001-01-09 00:19:15 +03:00
/* delete our entry in the connections database. */
2002-01-14 22:34:28 +03:00
yield_connection ( NULL , " " ) ;
2001-01-09 00:19:15 +03:00
2001-04-18 20:41:04 +04:00
respond_to_all_remaining_local_messages ( ) ;
2001-02-12 19:18:02 +03:00
decrement_smbd_process_count ( ) ;
1999-12-13 16:27:58 +03:00
1998-07-29 07:08:05 +04:00
# ifdef WITH_DFS
1998-08-17 18:11:44 +04:00
if ( dcelogin_atmost_once ) {
dfs_unlogin ( ) ;
}
1996-05-04 11:50:46 +04:00
# endif
1998-08-17 18:11:44 +04:00
if ( ! reason ) {
int oldlevel = DEBUGLEVEL ;
DEBUGLEVEL = 10 ;
DEBUG ( 0 , ( " Last message was %s \n " , smb_fn_name ( last_message ) ) ) ;
if ( last_inbuf )
show_msg ( last_inbuf ) ;
DEBUGLEVEL = oldlevel ;
DEBUG ( 0 , ( " =============================================================== \n " ) ) ;
1996-05-04 11:50:46 +04:00
# if DUMP_CORE
1998-08-17 18:11:44 +04:00
if ( dump_core ( ) ) return ;
1996-05-04 11:50:46 +04:00
# endif
1998-08-17 18:11:44 +04:00
}
1997-01-09 21:02:17 +03:00
1998-08-17 18:11:44 +04:00
locking_end ( ) ;
2002-07-15 14:35:28 +04:00
printing_end ( ) ;
1997-01-09 21:02:17 +03:00
1998-08-17 18:11:44 +04:00
DEBUG ( 3 , ( " Server exit (%s) \n " , ( reason ? reason : " " ) ) ) ;
exit ( 0 ) ;
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2002-03-14 05:15:08 +03:00
Initialise connect , service and file structs .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
1999-12-13 16:27:58 +03:00
static void init_structs ( void )
1996-05-04 11:50:46 +04:00
{
1999-12-13 16:27:58 +03:00
/*
* Set the machine NETBIOS name if not already
* set from the config file .
*/
2002-01-16 05:20:34 +03:00
if ( ! * global_myname ) {
1999-12-13 16:27:58 +03:00
char * p ;
2002-01-15 05:15:58 +03:00
pstrcpy ( global_myname , myhostname ( ) ) ;
2002-01-16 05:20:34 +03:00
p = strchr_m ( global_myname , ' . ' ) ;
1999-12-13 16:27:58 +03:00
if ( p )
* p = 0 ;
}
2002-01-16 05:20:34 +03:00
strupper ( global_myname ) ;
1999-12-13 16:27:58 +03:00
1998-08-17 18:11:44 +04:00
conn_init ( ) ;
1999-12-13 16:27:58 +03:00
1998-08-17 18:11:44 +04:00
file_init ( ) ;
1999-12-13 16:27:58 +03:00
/* for RPC pipes */
init_rpc_pipe_hnd ( ) ;
1998-08-17 18:11:44 +04:00
init_dptrs ( ) ;
2000-05-08 14:42:21 +04:00
secrets_init ( ) ;
2002-01-26 09:24:53 +03:00
1996-05-04 11:50:46 +04:00
}
/****************************************************************************
2002-03-14 05:15:08 +03:00
main program .
1996-05-04 11:50:46 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2002-03-14 05:15:08 +03:00
2002-11-09 20:08:58 +03:00
int main ( int argc , const char * argv [ ] )
1996-05-04 11:50:46 +04:00
{
1998-08-17 18:11:44 +04:00
/* shall I run as a daemon */
2002-11-09 20:08:58 +03:00
static BOOL is_daemon = False ;
static BOOL interactive = False ;
static char * ports = NULL ;
1998-08-17 18:11:44 +04:00
int opt ;
2002-11-09 20:08:58 +03:00
poptContext pc ;
struct poptOption long_options [ ] = {
POPT_AUTOHELP
{ " daemon " , ' D ' , POPT_ARG_VAL , & is_daemon , True , " Become a daemon (default) " } ,
{ " interactive " , ' i ' , POPT_ARG_VAL , & interactive , True , " Run interactive (not a daemon) " } ,
{ " build-options " , ' b ' , POPT_ARG_NONE , NULL , ' b ' , " Print build options " } ,
{ " port " , ' p ' , POPT_ARG_STRING , & ports , 0 , " Listen on the specified ports " } ,
{ NULL , 0 , POPT_ARG_INCLUDE_TABLE , popt_common_debug } ,
{ NULL , 0 , POPT_ARG_INCLUDE_TABLE , popt_common_configfile } ,
{ NULL , 0 , POPT_ARG_INCLUDE_TABLE , popt_common_socket_options } ,
{ NULL , 0 , POPT_ARG_INCLUDE_TABLE , popt_common_log_base } ,
{ NULL , 0 , POPT_ARG_INCLUDE_TABLE , popt_common_version } ,
{ NULL }
} ;
2001-11-19 05:49:53 +03:00
1998-07-29 07:08:05 +04:00
# ifdef HAVE_SET_AUTH_PARAMETERS
1998-08-17 18:11:44 +04:00
set_auth_parameters ( argc , argv ) ;
1996-05-04 11:50:46 +04:00
# endif
2002-11-09 20:08:58 +03:00
pc = poptGetContext ( " smbd " , argc , argv , long_options , 0 ) ;
while ( ( opt = poptGetNextOpt ( pc ) ) ! = - 1 ) {
1998-08-17 18:11:44 +04:00
switch ( opt ) {
2001-09-19 13:44:12 +04:00
case ' b ' :
2002-11-09 20:08:58 +03:00
build_options ( True ) ; /* Display output to screen as well as debug */
2001-09-19 13:44:12 +04:00
exit ( 0 ) ;
break ;
1998-08-17 18:11:44 +04:00
}
2002-11-09 20:08:58 +03:00
}
poptFreeContext ( pc ) ;
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
# ifdef HAVE_SETLUID
/* needed for SecureWare on SCO */
setluid ( 0 ) ;
# endif
2001-06-25 04:46:34 +04:00
sec_init ( ) ;
1999-12-13 16:27:58 +03:00
2001-10-03 16:18:20 +04:00
load_case_tables ( ) ;
2002-08-17 19:27:10 +04:00
set_remote_machine_name ( " smbd " ) ;
1999-12-13 16:27:58 +03:00
2001-12-30 00:13:53 +03:00
setup_logging ( argv [ 0 ] , interactive ) ;
1999-12-13 16:27:58 +03:00
/* we want to re-seed early to prevent time delays causing
client problems at a later date . ( tridge ) */
generate_random_buffer ( NULL , 0 , False ) ;
/* make absolutely sure we run as root - to handle cases where people
are crazy enough to have it setuid */
gain_root_privilege ( ) ;
gain_root_group_privilege ( ) ;
fault_setup ( ( void ( * ) ( void * ) ) exit_server ) ;
2002-03-14 05:15:08 +03:00
CatchSignal ( SIGTERM , SIGNAL_CAST sig_term ) ;
CatchSignal ( SIGHUP , SIGNAL_CAST sig_hup ) ;
1999-12-13 16:27:58 +03:00
/* we are never interested in SIGPIPE */
BlockSignals ( True , SIGPIPE ) ;
# if defined(SIGFPE)
/* we are never interested in SIGFPE */
BlockSignals ( True , SIGFPE ) ;
# endif
2000-10-13 01:19:49 +04:00
# if defined(SIGUSR2)
/* We are no longer interested in USR2 */
BlockSignals ( True , SIGUSR2 ) ;
# endif
2001-01-25 19:36:42 +03:00
/* POSIX demands that signals are inherited. If the invoking process has
* these signals masked , we will have problems , as we won ' t recieve them . */
BlockSignals ( False , SIGHUP ) ;
BlockSignals ( False , SIGUSR1 ) ;
2002-03-14 05:15:08 +03:00
BlockSignals ( False , SIGTERM ) ;
2001-01-25 19:36:42 +03:00
1999-12-13 16:27:58 +03:00
/* we want total control over the permissions on created files,
so set our umask to 0 */
umask ( 0 ) ;
2000-06-23 09:54:49 +04:00
init_sec_ctx ( ) ;
1999-12-13 16:27:58 +03:00
1998-08-17 18:11:44 +04:00
reopen_logs ( ) ;
1996-05-04 11:50:46 +04:00
2002-01-18 06:26:53 +03:00
DEBUG ( 0 , ( " smbd version %s started. \n " , VERSION ) ) ;
DEBUGADD ( 0 , ( " Copyright Andrew Tridgell and the Samba Team 1992-2002 \n " ) ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 18:11:44 +04:00
DEBUG ( 2 , ( " uid=%d gid=%d euid=%d egid=%d \n " ,
( int ) getuid ( ) , ( int ) getgid ( ) , ( int ) geteuid ( ) , ( int ) getegid ( ) ) ) ;
1996-05-04 11:50:46 +04:00
2001-09-19 13:44:12 +04:00
/* Output the build options to the debug log */
build_options ( False ) ;
1998-08-17 18:11:44 +04:00
if ( sizeof ( uint16 ) < 2 | | sizeof ( uint32 ) < 4 ) {
DEBUG ( 0 , ( " ERROR: Samba is not configured correctly for the word size on your machine \n " ) ) ;
exit ( 1 ) ;
}
1996-05-04 11:50:46 +04:00
1999-12-13 16:27:58 +03:00
/*
* Do this before reload_services .
*/
1999-02-08 04:46:46 +03:00
1998-08-17 18:11:44 +04:00
if ( ! reload_services ( False ) )
return ( - 1 ) ;
1996-05-04 11:50:46 +04:00
1999-02-08 04:46:46 +03:00
init_structs ( ) ;
2001-07-10 06:28:17 +04:00
1999-04-01 09:22:58 +04:00
# ifdef WITH_PROFILE
if ( ! profile_setup ( False ) ) {
DEBUG ( 0 , ( " ERROR: failed to setup profiling \n " ) ) ;
return - 1 ;
}
# endif
1999-12-13 16:27:58 +03:00
fstrcpy ( global_myworkgroup , lp_workgroup ( ) ) ;
1997-05-20 04:32:51 +04:00
1998-08-17 18:11:44 +04:00
DEBUG ( 3 , ( " loaded services \n " ) ) ;
1996-05-04 11:50:46 +04:00
1998-08-17 18:11:44 +04:00
if ( ! is_daemon & & ! is_a_socket ( 0 ) ) {
2002-01-30 08:45:09 +03:00
if ( ! interactive )
DEBUG ( 0 , ( " standard input is not a socket, assuming -D option \n " ) ) ;
/*
* Setting is_daemon here prevents us from eventually calling
* the open_sockets_inetd ( )
*/
1998-08-17 18:11:44 +04:00
is_daemon = True ;
}
1997-11-11 03:48:42 +03:00
2001-12-30 00:13:53 +03:00
if ( is_daemon & & ! interactive ) {
1998-08-17 18:11:44 +04:00
DEBUG ( 3 , ( " Becoming a daemon. \n " ) ) ;
become_daemon ( ) ;
}
2000-02-07 19:22:16 +03:00
2001-12-30 04:46:38 +03:00
# if HAVE_SETPGID
/*
* If we ' re interactive we want to set our own process group for
* signal management .
*/
if ( interactive )
setpgid ( ( pid_t ) 0 , ( pid_t ) 0 ) ;
# endif
1998-08-17 18:11:44 +04:00
if ( ! directory_exist ( lp_lockdir ( ) , NULL ) ) {
mkdir ( lp_lockdir ( ) , 0755 ) ;
}
1996-05-04 11:50:46 +04:00
1998-08-17 18:11:44 +04:00
if ( is_daemon ) {
pidfile_create ( " smbd " ) ;
}
1996-08-15 19:11:34 +04:00
2000-10-12 23:42:18 +04:00
if ( ! message_init ( ) ) {
exit ( 1 ) ;
}
2002-01-03 06:53:41 +03:00
register_msg_pool_usage ( ) ;
2002-01-09 11:27:15 +03:00
register_dmalloc_msgs ( ) ;
2000-10-12 23:42:18 +04:00
2002-11-01 02:41:00 +03:00
if ( ! print_backend_init ( ) )
exit ( 1 ) ;
2000-11-28 00:37:07 +03:00
/* Setup the main smbd so that we can get messages. */
2002-09-25 19:19:00 +04:00
claim_connection ( NULL , " " , 0 , True , FLAG_MSG_GENERAL | FLAG_MSG_SMBD ) ;
2000-11-28 00:37:07 +03:00
2001-10-18 14:40:36 +04:00
/*
DO NOT ENABLE THIS TILL YOU COPE WITH KILLING THESE TASKS AND INETD
THIS * killed * LOTS OF BUILD FARM MACHINES . IT CREATED HUNDREDS OF
smbd PROCESSES THAT NEVER DIE
start_background_queue ( ) ;
*/
2001-10-17 03:16:00 +04:00
2002-08-17 19:27:10 +04:00
if ( ! open_sockets_smbd ( is_daemon , ports ) )
1998-08-17 18:11:44 +04:00
exit ( 1 ) ;
1998-05-19 03:57:28 +04:00
2000-01-08 05:16:15 +03:00
/*
2000-06-10 17:38:07 +04:00
* everything after this point is run after the fork ( )
2000-01-08 05:16:15 +03:00
*/
2002-08-17 19:27:10 +04:00
namecache_enable ( ) ;
2002-01-14 22:34:28 +03:00
if ( ! locking_init ( 0 ) )
1999-12-13 16:27:58 +03:00
exit ( 1 ) ;
2002-01-14 22:34:28 +03:00
if ( ! share_info_db_init ( ) )
2001-04-09 10:36:38 +04:00
exit ( 1 ) ;
2002-07-15 14:35:28 +04:00
if ( ! init_registry ( ) )
exit ( 1 ) ;
2002-01-14 22:34:28 +03:00
if ( ! initialize_password_db ( False ) )
1998-08-17 18:11:44 +04:00
exit ( 1 ) ;
1996-05-04 11:50:46 +04:00
2002-01-26 09:24:53 +03:00
uni_group_cache_init ( ) ; /* Non-critical */
1998-08-17 18:11:44 +04:00
/* possibly reload the services file. */
reload_services ( True ) ;
2000-01-03 22:19:48 +03:00
2002-07-15 14:35:28 +04:00
if ( ! get_global_sam_sid ( ) ) {
2001-12-03 20:14:23 +03:00
DEBUG ( 0 , ( " ERROR: Samba cannot create a SAM SID. \n " ) ) ;
exit ( 1 ) ;
}
if ( ! init_account_policy ( ) ) {
DEBUG ( 0 , ( " Could not open account policy tdb. \n " ) ) ;
2000-01-03 22:19:48 +03:00
exit ( 1 ) ;
}
1998-08-17 18:11:44 +04:00
if ( * lp_rootdir ( ) ) {
if ( sys_chroot ( lp_rootdir ( ) ) = = 0 )
DEBUG ( 2 , ( " Changed root to %s \n " , lp_rootdir ( ) ) ) ;
}
1997-09-23 23:19:06 +04:00
2000-06-10 17:38:07 +04:00
/* Setup oplocks */
2002-01-14 22:34:28 +03:00
if ( ! init_oplocks ( ) )
1998-08-17 18:11:44 +04:00
exit ( 1 ) ;
2001-10-22 10:53:28 +04:00
totally rewrote the async signal, notification and oplock notification
handling in Samba. This was needed due to several limitations and
races in the previous code - as a side effect the new code is much
cleaner :)
in summary:
- changed sys_select() to avoid a signal/select race condition. It is a
rare race but once we have signals doing notification and oplocks it
is important.
- changed our main processing loop to take advantage of the new
sys_select semantics
- split the notify code into implementaion dependent and general
parts. Added the following structure that defines an implementation:
struct cnotify_fns {
void * (*register_notify)(connection_struct *conn, char *path, uint32 flags);
BOOL (*check_notify)(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *data, time_t t);
void (*remove_notify)(void *data);
};
then I wrote two implementations, one using hash/poll (like our old
code) and the other using the new Linux kernel change notify. It
should be easy to add other change notify implementations by creating
a sructure of the above type.
- fixed a bug in change notify where we were returning the wrong error
code.
- rewrote the core change notify code to be much simpler
- moved to real-time signals for leases and change notify
Amazingly, it all seems to work. I was very surprised!
(This used to be commit 44766c39e0027c762bee8b33b12c621c109a3267)
2000-06-12 19:53:31 +04:00
/* Setup change notify */
2002-01-14 22:34:28 +03:00
if ( ! init_change_notify ( ) )
totally rewrote the async signal, notification and oplock notification
handling in Samba. This was needed due to several limitations and
races in the previous code - as a side effect the new code is much
cleaner :)
in summary:
- changed sys_select() to avoid a signal/select race condition. It is a
rare race but once we have signals doing notification and oplocks it
is important.
- changed our main processing loop to take advantage of the new
sys_select semantics
- split the notify code into implementaion dependent and general
parts. Added the following structure that defines an implementation:
struct cnotify_fns {
void * (*register_notify)(connection_struct *conn, char *path, uint32 flags);
BOOL (*check_notify)(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *data, time_t t);
void (*remove_notify)(void *data);
};
then I wrote two implementations, one using hash/poll (like our old
code) and the other using the new Linux kernel change notify. It
should be easy to add other change notify implementations by creating
a sructure of the above type.
- fixed a bug in change notify where we were returning the wrong error
code.
- rewrote the core change notify code to be much simpler
- moved to real-time signals for leases and change notify
Amazingly, it all seems to work. I was very surprised!
(This used to be commit 44766c39e0027c762bee8b33b12c621c109a3267)
2000-06-12 19:53:31 +04:00
exit ( 1 ) ;
2002-08-17 19:27:10 +04:00
/* re-initialise the timezone */
TimeInit ( ) ;
/* register our message handlers */
message_register ( MSG_SMB_FORCE_TDIS , msg_force_tdis ) ;
talloc_init_named ( " dummy! " ) ;
1998-08-17 18:11:44 +04:00
smbd_process ( ) ;
2002-01-13 02:57:10 +03:00
uni_group_cache_shutdown ( ) ;
1998-08-17 18:11:44 +04:00
exit_server ( " normal exit " ) ;
return ( 0 ) ;
1996-05-04 11:50:46 +04:00
}