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
2003-04-14 07:30:20 +04:00
Copyright ( C ) Jelmer Vernooij 2002 - 2003
2007-04-21 01:09:44 +04:00
Copyright ( C ) James Peach 2007
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1996-05-04 11:50:46 +04:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1996-05-04 11:50:46 +04:00
*/
# include "includes.h"
2006-04-26 19:41:25 +04:00
static_decl_rpc ;
2004-02-08 14:00:25 +03:00
static int am_parent = 1 ;
1996-12-10 21:00:22 +03:00
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)
2005-04-06 20:28:04 +04:00
extern struct auth_context * negprot_global_auth_context ;
1996-05-04 11:50:46 +04:00
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 ;
2006-08-16 14:36:19 +04:00
static SIG_ATOMIC_T got_sig_cld ;
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 ) ;
}
2007-01-17 15:59:14 +03:00
struct event_context * smbd_event_context ( void )
{
static struct event_context * ctx ;
if ( ! ctx & & ! ( ctx = event_context_init ( NULL ) ) ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " Could not init smbd event context " ) ;
2007-01-17 15:59:14 +03:00
}
return ctx ;
}
2007-01-31 17:28:08 +03:00
struct messaging_context * smbd_messaging_context ( void )
{
static struct messaging_context * ctx ;
if ( ! ctx & & ! ( ctx = messaging_init ( NULL , server_id_self ( ) ,
smbd_event_context ( ) ) ) ) {
2007-06-16 01:58:49 +04:00
smb_panic ( " Could not init smbd messaging context " ) ;
2007-01-31 17:28:08 +03:00
}
return ctx ;
}
2006-02-04 01:19:41 +03:00
/*******************************************************************
What to do when smb . conf is updated .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-16 18:24:06 +04:00
static void smb_conf_updated ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
2006-02-04 01:19:41 +03:00
{
2007-05-16 18:24:06 +04:00
DEBUG ( 10 , ( " smb_conf_updated: Got message saying smb.conf was "
" updated. Reloading. \n " ) ) ;
2006-02-04 01:19:41 +03:00
reload_services ( False ) ;
}
2007-01-20 00:46:12 +03:00
/*******************************************************************
Delete a statcache entry .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-05-16 18:24:06 +04:00
static void smb_stat_cache_delete ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_tnype ,
struct server_id server_id ,
DATA_BLOB * data )
2007-01-20 00:46:12 +03:00
{
2007-05-16 18:24:06 +04:00
const char * name = ( const char * ) data - > data ;
2007-01-20 00:46:12 +03:00
DEBUG ( 10 , ( " smb_stat_cache_delete: delete name %s \n " , name ) ) ;
stat_cache_delete ( name ) ;
}
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 ;
2005-06-09 19:20:11 +04:00
sys_select_signal ( SIGTERM ) ;
2002-03-14 05:15:08 +03:00
}
/****************************************************************************
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 ;
2005-06-09 19:20:11 +04:00
sys_select_signal ( SIGHUP ) ;
1996-12-10 21:00:22 +03:00
}
1998-08-17 18:11:44 +04:00
2006-08-16 14:36:19 +04:00
/****************************************************************************
Catch a sigcld
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static void sig_cld ( int sig )
{
got_sig_cld = 1 ;
sys_select_signal ( SIGCLD ) ;
}
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
2007-05-16 18:24:06 +04:00
static void msg_sam_sync ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
2001-08-28 10:34:53 +04:00
{
DEBUG ( 10 , ( " ** sam sync message received, ignoring \n " ) ) ;
}
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
2007-05-16 18:24:06 +04:00
static void msg_exit_server ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_type ,
struct server_id server_id ,
DATA_BLOB * data )
2002-03-29 17:28:38 +03:00
{
2006-04-04 04:27:50 +04:00
DEBUG ( 3 , ( " got a SHUTDOWN message \n " ) ) ;
2006-04-11 02:47:09 +04:00
exit_server_cleanly ( NULL ) ;
2006-04-04 04:27:50 +04:00
}
# ifdef DEVELOPER
2007-05-16 18:24:06 +04:00
static void msg_inject_fault ( struct messaging_context * msg ,
void * private_data ,
uint32_t msg_type ,
struct server_id src ,
DATA_BLOB * data )
2006-04-04 04:27:50 +04:00
{
int sig ;
2007-05-16 18:24:06 +04:00
if ( data - > length ! = sizeof ( sig ) ) {
2006-04-04 04:27:50 +04:00
2007-05-16 18:24:06 +04:00
DEBUG ( 0 , ( " Process %s sent bogus signal injection request \n " ,
procid_str_static ( & src ) ) ) ;
2006-04-04 04:27:50 +04:00
return ;
}
2007-05-16 18:24:06 +04:00
sig = * ( int * ) data - > data ;
2006-04-04 04:27:50 +04:00
if ( sig = = - 1 ) {
exit_server ( " internal error injected " ) ;
return ;
}
# if HAVE_STRSIGNAL
2007-05-16 18:24:06 +04:00
DEBUG ( 0 , ( " Process %s requested injection of signal %d (%s) \n " ,
procid_str_static ( & src ) , sig , strsignal ( sig ) ) ) ;
2006-04-04 04:27:50 +04:00
# else
2007-05-16 18:24:06 +04:00
DEBUG ( 0 , ( " Process %s requested injection of signal %d \n " ,
procid_str_static ( & src ) , sig ) ) ;
2006-04-04 04:27:50 +04:00
# endif
kill ( sys_getpid ( ) , sig ) ;
2002-03-29 17:28:38 +03:00
}
2006-04-04 04:27:50 +04:00
# endif /* DEVELOPER */
2002-03-29 17:28:38 +03:00
2006-08-16 14:36:19 +04:00
struct child_pid {
struct child_pid * prev , * next ;
pid_t pid ;
} ;
static struct child_pid * children ;
static int num_children ;
static void add_child_pid ( pid_t pid )
{
struct child_pid * child ;
if ( lp_max_smbd_processes ( ) = = 0 ) {
/* Don't bother with the child list if we don't care anyway */
return ;
}
child = SMB_MALLOC_P ( struct child_pid ) ;
if ( child = = NULL ) {
DEBUG ( 0 , ( " Could not add child struct -- malloc failed \n " ) ) ;
return ;
}
child - > pid = pid ;
DLIST_ADD ( children , child ) ;
num_children + = 1 ;
}
static void remove_child_pid ( pid_t pid )
{
struct child_pid * child ;
if ( lp_max_smbd_processes ( ) = = 0 ) {
/* Don't bother with the child list if we don't care anyway */
return ;
}
for ( child = children ; child ! = NULL ; child = child - > next ) {
if ( child - > pid = = pid ) {
struct child_pid * tmp = child ;
DLIST_REMOVE ( children , child ) ;
SAFE_FREE ( tmp ) ;
num_children - = 1 ;
return ;
}
}
DEBUG ( 0 , ( " Could not find child %d -- ignoring \n " , ( int ) pid ) ) ;
}
1997-09-25 04:25:44 +04:00
2003-02-06 00:16:55 +03:00
/****************************************************************************
Have we reached the process limit ?
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-02-08 11:38:42 +03:00
static BOOL allowable_number_of_smbd_processes ( void )
2003-02-06 00:16:55 +03:00
{
int max_processes = lp_max_smbd_processes ( ) ;
if ( ! max_processes )
return True ;
2006-08-16 14:36:19 +04:00
return num_children < max_processes ;
2003-02-06 00:16:55 +03:00
}
2007-06-14 22:48:51 +04:00
/****************************************************************************
Are we idle enough that we could safely exit ?
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL smbd_is_idle ( void )
{
/* Currently we define "idle" as having no client connections. */
return count_all_current_connections ( ) = = 0 ;
}
2007-04-21 01:09:44 +04:00
/****************************************************************************
Open the socket communication .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
static BOOL open_sockets_smbd ( enum smb_server_mode server_mode , const char * smb_ports )
{
int num_sockets = 0 ;
int fd_listenset [ FD_SETSIZE ] ;
fd_set listen_set ;
int s ;
int maxfd = 0 ;
int i ;
2007-04-27 18:00:27 +04:00
struct timeval idle_timeout = timeval_zero ( ) ;
2007-04-21 01:09:44 +04:00
if ( server_mode = = SERVER_MODE_INETD ) {
return open_sockets_inetd ( ) ;
}
# ifdef HAVE_ATEXIT
{
static int atexit_set ;
if ( atexit_set = = 0 ) {
atexit_set = 1 ;
atexit ( killkids ) ;
}
}
# endif
/* Stop zombies */
CatchSignal ( SIGCLD , sig_cld ) ;
FD_ZERO ( & listen_set ) ;
/* At this point, it doesn't matter what daemon mode we are in, we
2007-04-24 02:17:06 +04:00
* need some sockets to listen on .
2007-04-21 01:09:44 +04:00
*/
2007-04-24 02:17:06 +04:00
num_sockets = smbd_sockinit ( smb_ports , fd_listenset , & idle_timeout ) ;
2007-04-21 01:09:44 +04:00
if ( num_sockets = = 0 ) {
return False ;
}
for ( i = 0 ; i < num_sockets ; + + i ) {
FD_SET ( fd_listenset [ i ] , & listen_set ) ;
maxfd = MAX ( maxfd , fd_listenset [ i ] ) ;
}
2002-08-17 19:27:10 +04:00
2007-07-24 14:35:10 +04:00
/* Setup the main smbd so that we can get messages. Note that
do this after starting listening . This is needed as when in
clustered mode , ctdb won ' t allow us to start doing database
operations until it has gone thru a full startup , which
includes checking to see that smbd is listening . */
claim_connection ( NULL , " " , FLAG_MSG_GENERAL | FLAG_MSG_SMBD ) ;
2001-08-28 10:34:53 +04:00
/* Listen to messages */
2007-05-16 18:24:06 +04:00
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_SAM_SYNC , msg_sam_sync ) ;
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SHUTDOWN , msg_exit_server ) ;
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_FILE_RENAME , msg_file_was_renamed ) ;
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_CONF_UPDATED , smb_conf_updated ) ;
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_STAT_CACHE_DELETE , smb_stat_cache_delete ) ;
2007-05-29 18:49:19 +04:00
brl_register_msgs ( smbd_messaging_context ( ) ) ;
2001-08-28 10:34:53 +04:00
2006-04-04 04:27:50 +04:00
# ifdef DEVELOPER
2007-05-16 18:24:06 +04:00
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_INJECT_FAULT , msg_inject_fault ) ;
2006-04-04 04:27:50 +04:00
# endif
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 ) {
2007-05-30 17:41:38 +04:00
struct timeval now ;
fd_set r_fds , w_fds ;
1998-08-17 18:11:44 +04:00
int num ;
2000-11-22 22:42:54 +03:00
/* Free up temporary memory from the main smbd. */
2006-02-20 20:59:58 +03:00
lp_TALLOC_FREE ( ) ;
2000-11-22 22:42:54 +03:00
2000-11-28 00:37:07 +03:00
/* Ensure we respond to PING and DEBUG messages from the main smbd. */
2007-05-22 02:17:13 +04:00
message_dispatch ( smbd_messaging_context ( ) ) ;
2000-11-28 00:37:07 +03:00
2006-08-16 14:36:19 +04:00
if ( got_sig_cld ) {
pid_t pid ;
got_sig_cld = False ;
while ( ( pid = sys_waitpid ( - 1 , NULL , WNOHANG ) ) > 0 ) {
remove_child_pid ( pid ) ;
}
}
2007-05-30 17:41:38 +04:00
memcpy ( ( char * ) & r_fds , ( char * ) & listen_set ,
1998-08-17 18:11:44 +04:00
sizeof ( listen_set ) ) ;
2007-05-30 17:41:38 +04:00
FD_ZERO ( & w_fds ) ;
GetTimeOfDay ( & now ) ;
2007-04-21 01:09:44 +04:00
2007-05-30 17:41:38 +04:00
event_add_to_select_args ( smbd_event_context ( ) , & now ,
& r_fds , & w_fds , & idle_timeout ,
& maxfd ) ;
2007-06-14 22:48:51 +04:00
if ( timeval_is_zero ( & idle_timeout ) ) {
num = sys_select ( maxfd + 1 , & r_fds , & w_fds ,
NULL , NULL ) ;
} else {
num = sys_select ( maxfd + 1 , & r_fds , & w_fds ,
NULL , & idle_timeout ) ;
/* If the idle timeout fired and we are idle, exit
* gracefully . We expect to be running under a process
* controller that will restart us if necessry .
*/
if ( num = = 0 & & smbd_is_idle ( ) ) {
exit_server_cleanly ( " idle timeout " ) ;
}
}
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 ) {
2006-04-11 02:47:09 +04:00
exit_server_cleanly ( NULL ) ;
2002-03-14 05:15:08 +03:00
}
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
}
2007-04-21 01:09:44 +04:00
2007-05-31 21:26:30 +04:00
if ( run_events ( smbd_event_context ( ) , num , & r_fds , & w_fds ) ) {
continue ;
}
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 ) ;
2006-08-16 14:36:19 +04:00
pid_t child = 0 ;
2003-02-06 00:16:55 +03:00
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 + + ) {
2007-05-30 17:41:38 +04:00
if ( FD_ISSET ( fd_listenset [ i ] , & r_fds ) ) {
1999-12-13 16:27:58 +03:00
s = fd_listenset [ i ] ;
/* Clear this so we don't look
at it again . */
2007-05-30 17:41:38 +04:00
FD_CLR ( fd_listenset [ i ] , & r_fds ) ;
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 ;
}
2003-01-14 18:52:14 +03:00
2004-03-27 02:02:53 +03:00
/* Ensure child is set to blocking mode */
set_blocking ( smbd_server_fd ( ) , True ) ;
2007-04-20 22:34:33 +04:00
/* In interactive mode, return with a connected socket.
* Foreground and daemon modes should fork worker
* processes .
*/
if ( server_mode = = SERVER_MODE_INTERACTIVE ) {
2003-01-14 18:52:14 +03:00
return True ;
2007-04-20 22:34:33 +04:00
}
1998-08-17 18:11:44 +04:00
2006-08-16 13:10:54 +04:00
if ( allowable_number_of_smbd_processes ( ) & &
2006-08-16 14:36:19 +04:00
smbd_server_fd ( ) ! = - 1 & &
( ( child = sys_fork ( ) ) = = 0 ) ) {
1998-08-17 18:11:44 +04:00
/* Child code ... */
2006-08-25 23:13:37 +04:00
/* Stop zombies, the parent explicitly handles
* them , counting worker smbds . */
CatchChild ( ) ;
1998-08-17 18:11:44 +04:00
/* 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 */
2006-08-16 13:10:54 +04:00
set_remote_machine_name ( get_peer_addr ( smbd_server_fd ( ) ) ,
False ) ;
2002-08-17 19:27:10 +04:00
2004-07-14 08:36:01 +04:00
/* Reset the state of the random
* number generation system , so
* children do not get the same random
* numbers as each other */
1998-09-26 03:40:49 +04:00
2004-07-14 08:36:01 +04:00
set_need_random_reseed ( ) ;
2006-08-16 13:10:54 +04:00
/* tdb needs special fork handling - remove
* CLEAR_IF_FIRST flags */
2006-04-07 02:31:45 +04:00
if ( tdb_reopen_all ( 1 ) = = - 1 ) {
2002-11-01 02:41:00 +03:00
DEBUG ( 0 , ( " tdb_reopen_all failed. \n " ) ) ;
2007-06-16 01:58:49 +04:00
smb_panic ( " tdb_reopen_all failed " ) ;
2002-11-01 02:41:00 +03:00
}
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 ) ;
2006-08-16 14:36:19 +04:00
if ( child ! = 0 ) {
add_child_pid ( child ) ;
}
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
}
2005-01-05 19:20:35 +03:00
/****************************************************************************
Reload printers
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void reload_printers ( void )
{
int snum ;
int n_services = lp_numservices ( ) ;
int pnum = lp_servicenumber ( PRINTERS_NAME ) ;
const char * pname ;
pcap_cache_reload ( ) ;
/* remove stale printers */
for ( snum = 0 ; snum < n_services ; snum + + ) {
/* avoid removing PRINTERS_NAME or non-autoloaded printers */
if ( snum = = pnum | | ! ( lp_snum_ok ( snum ) & & lp_print_ok ( snum ) & &
lp_autoloaded ( snum ) ) )
continue ;
pname = lp_printername ( snum ) ;
if ( ! pcap_printername_ok ( pname ) ) {
DEBUG ( 3 , ( " removing stale printer %s \n " , pname ) ) ;
if ( is_printer_published ( NULL , snum , NULL ) )
nt_printer_publish ( NULL , snum , SPOOL_DS_UNPUBLISH ) ;
del_a_printer ( pname ) ;
lp_killservice ( snum ) ;
}
}
load_printers ( ) ;
}
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 ) ;
2004-11-12 20:39:32 +03:00
2006-01-29 01:53:04 +03:00
ret = lp_load ( dyn_CONFIGFILE , False , False , True , True ) ;
1996-05-04 11:50:46 +04:00
2005-01-05 19:20:35 +03:00
reload_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
2004-06-15 22:36:45 +04:00
if ( smbd_server_fd ( ) ! = - 1 ) {
set_socket_options ( smbd_server_fd ( ) , " SO_KEEPALIVE " ) ;
set_socket_options ( smbd_server_fd ( ) , user_socket_options ) ;
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 */
2004-06-15 22:36:45 +04:00
set_current_service ( NULL , 0 , 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
/****************************************************************************
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
2006-04-04 04:27:50 +04:00
/* Reasons for shutting down a server process. */
enum server_exit_reason { SERVER_EXIT_NORMAL , SERVER_EXIT_ABNORMAL } ;
static void exit_server_common ( enum server_exit_reason how ,
const char * const reason ) NORETURN_ATTRIBUTE ;
static void exit_server_common ( enum server_exit_reason how ,
const char * const reason )
1996-05-04 11:50:46 +04:00
{
1998-08-17 18:11:44 +04:00
static int firsttime = 1 ;
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
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 ( ) ;
2007-05-15 00:31:28 +04:00
/* 3 second timeout. */
print_notify_send_messages ( smbd_messaging_context ( ) , 3 ) ;
2002-09-25 19:19:00 +04:00
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 ( ) ;
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
2006-04-04 04:27:50 +04:00
locking_end ( ) ;
printing_end ( ) ;
2007-03-20 04:17:47 +03:00
server_encryption_shutdown ( ) ;
2006-04-04 04:27:50 +04:00
if ( how ! = SERVER_EXIT_NORMAL ) {
1998-08-17 18:11:44 +04:00
int oldlevel = DEBUGLEVEL ;
2005-06-28 02:53:56 +04:00
char * last_inbuf = get_InBuffer ( ) ;
2006-04-04 04:27:50 +04:00
1998-08-17 18:11:44 +04:00
DEBUGLEVEL = 10 ;
2006-04-04 04:27:50 +04:00
DEBUGSEP ( 0 ) ;
DEBUG ( 0 , ( " Abnormal server exit: %s \n " ,
reason ? reason : " no explanation provided " ) ) ;
DEBUGSEP ( 0 ) ;
log_stack_trace ( ) ;
if ( last_inbuf ) {
DEBUG ( 0 , ( " Last message was %s \n " , LAST_MESSAGE ( ) ) ) ;
1998-08-17 18:11:44 +04:00
show_msg ( last_inbuf ) ;
2006-04-04 04:27:50 +04:00
}
1998-08-17 18:11:44 +04:00
DEBUGLEVEL = oldlevel ;
2006-04-01 09:41:34 +04:00
dump_core ( ) ;
1997-01-09 21:02:17 +03:00
2006-04-04 04:27:50 +04:00
} else {
DEBUG ( 3 , ( " Server exit (%s) \n " ,
( reason ? reason : " normal exit " ) ) ) ;
}
1997-01-09 21:02:17 +03:00
1998-08-17 18:11:44 +04:00
exit ( 0 ) ;
1996-05-04 11:50:46 +04:00
}
2006-04-04 04:27:50 +04:00
void exit_server ( const char * const explanation )
{
exit_server_common ( SERVER_EXIT_ABNORMAL , explanation ) ;
}
2006-04-11 02:47:09 +04:00
void exit_server_cleanly ( const char * const explanation )
2006-04-04 04:27:50 +04:00
{
2006-04-11 02:47:09 +04:00
exit_server_common ( SERVER_EXIT_NORMAL , explanation ) ;
2006-04-04 04:27:50 +04:00
}
void exit_server_fault ( void )
{
exit_server ( " critical server fault " ) ;
}
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
2002-11-13 02:20:50 +03:00
static BOOL 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-11-13 02:20:50 +03:00
if ( ! init_names ( ) )
return False ;
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
2002-11-13 02:20:50 +03:00
return True ;
1996-05-04 11:50:46 +04:00
}
2007-03-18 13:57:46 +03:00
/*
* Send keepalive packets to our client
*/
static BOOL keepalive_fn ( const struct timeval * now , void * private_data )
{
if ( ! send_keepalive ( smbd_server_fd ( ) ) ) {
DEBUG ( 2 , ( " Keepalive failed - exiting. \n " ) ) ;
return False ;
}
return True ;
}
2007-03-18 16:19:40 +03:00
/*
* Do the recurring check if we ' re idle
*/
static BOOL deadtime_fn ( const struct timeval * now , void * private_data )
{
if ( ( conn_num_open ( ) = = 0 )
| | ( conn_idle_all ( now - > tv_sec ) ) ) {
DEBUG ( 2 , ( " Closing idle connection \n " ) ) ;
2007-05-15 00:31:28 +04:00
messaging_send ( smbd_messaging_context ( ) , procid_self ( ) ,
MSG_SHUTDOWN , & data_blob_null ) ;
2007-03-18 16:19:40 +03:00
return False ;
}
return True ;
}
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
2003-05-27 06:35:53 +04:00
/* Declare prototype for build_options() to avoid having to run it through
mkproto . h . Mixing $ ( builddir ) and $ ( srcdir ) source files in the current
prototype generation system is too complicated . */
2006-12-12 23:15:47 +03:00
extern void build_options ( BOOL screen ) ;
2003-05-27 06:35:53 +04: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 */
2007-04-20 22:34:33 +04:00
BOOL no_process_group = False ;
BOOL log_stdout = False ;
const char * ports = NULL ;
const char * profile_level = NULL ;
1998-08-17 18:11:44 +04:00
int opt ;
2002-11-09 20:08:58 +03:00
poptContext pc ;
2007-04-20 22:34:33 +04:00
enum smb_server_mode server_mode = SERVER_MODE_DAEMON ;
2002-11-09 20:08:58 +03:00
struct poptOption long_options [ ] = {
2006-05-24 08:02:14 +04:00
POPT_AUTOHELP
2007-04-20 22:34:33 +04:00
{ " daemon " , ' D ' , POPT_ARG_VAL , & server_mode , SERVER_MODE_DAEMON ,
" Become a daemon (default) " } ,
{ " interactive " , ' i ' , POPT_ARG_VAL , & server_mode , SERVER_MODE_INTERACTIVE ,
" Run interactive (not a daemon) " } ,
{ " foreground " , ' F ' , POPT_ARG_VAL , & server_mode , SERVER_MODE_FOREGROUND ,
" Run daemon in foreground (for daemontools, etc.) " } ,
{ " no-process-group " , ' \0 ' , POPT_ARG_VAL , & no_process_group , True ,
" Don't create a new process group " } ,
2003-01-03 20:39:30 +03:00
{ " log-stdout " , ' S ' , POPT_ARG_VAL , & log_stdout , True , " Log to stdout " } ,
2002-11-09 20:08:58 +03:00
{ " build-options " , ' b ' , POPT_ARG_NONE , NULL , ' b ' , " Print build options " } ,
{ " port " , ' p ' , POPT_ARG_STRING , & ports , 0 , " Listen on the specified ports " } ,
2007-04-20 05:52:44 +04:00
{ " profiling-level " , ' P ' , POPT_ARG_STRING , & profile_level , 0 , " Set profiling level " , " PROFILE_LEVEL " } ,
2003-04-14 07:30:20 +04:00
POPT_COMMON_SAMBA
2006-05-24 08:02:14 +04:00
POPT_COMMON_DYNCONFIG
POPT_TABLEEND
2002-11-09 20:08:58 +03:00
} ;
2001-11-19 05:49:53 +03:00
2005-12-29 16:10:33 +03:00
load_case_tables ( ) ;
2007-02-07 23:20:56 +03:00
TimeInit ( ) ;
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 ) ;
2007-08-21 18:22:16 +04:00
default :
d_fprintf ( stderr , " \n Invalid option %s: %s \n " ,
poptBadOption ( pc , 0 ) , poptStrerror ( opt ) ) ;
exit ( 1 ) ;
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
2003-03-18 12:52:55 +03:00
set_remote_machine_name ( " smbd " , False ) ;
1999-12-13 16:27:58 +03:00
2007-04-20 22:34:33 +04:00
if ( server_mode = = SERVER_MODE_INTERACTIVE ) {
2003-01-03 20:39:30 +03:00
log_stdout = True ;
2007-04-20 22:34:33 +04:00
if ( DEBUGLEVEL > = 9 ) {
talloc_enable_leak_report ( ) ;
}
2003-01-03 20:39:30 +03:00
}
2007-04-20 22:34:33 +04:00
if ( log_stdout & & server_mode = = SERVER_MODE_DAEMON ) {
2003-01-03 20:39:30 +03:00
DEBUG ( 0 , ( " ERROR: Can't log to stdout (-S) unless daemon is in foreground (-F) or interactive (-i) \n " ) ) ;
exit ( 1 ) ;
}
setup_logging ( argv [ 0 ] , log_stdout ) ;
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 ) */
2004-07-14 08:36:01 +04:00
generate_random_buffer ( NULL , 0 ) ;
1999-12-13 16:27:58 +03:00
/* 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 ( ) ;
2006-04-04 04:27:50 +04:00
fault_setup ( ( void ( * ) ( void * ) ) exit_server_fault ) ;
dump_core_setup ( " smbd " ) ;
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
2003-08-20 21:13:38 +04:00
DEBUG ( 0 , ( " smbd version %s started. \n " , SAMBA_VERSION_STRING ) ) ;
2005-10-07 19:43:32 +04:00
DEBUGADD ( 0 , ( " %s \n " , COPYRIGHT_STARTUP_MESSAGE ) ) ;
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
2007-05-15 16:18:17 +04:00
if ( ! profile_setup ( smbd_messaging_context ( ) , False ) ) {
1999-04-01 09:22:58 +04:00
DEBUG ( 0 , ( " ERROR: failed to setup profiling \n " ) ) ;
return - 1 ;
}
2007-04-20 05:52:44 +04:00
if ( profile_level ! = NULL ) {
int pl = atoi ( profile_level ) ;
2007-05-07 13:35:35 +04:00
struct server_id src ;
2007-04-20 05:52:44 +04:00
DEBUG ( 1 , ( " setting profiling level: %s \n " , profile_level ) ) ;
src . pid = getpid ( ) ;
set_profile_level ( pl , src ) ;
}
1999-04-01 09:22:58 +04:00
# endif
1998-08-17 18:11:44 +04:00
DEBUG ( 3 , ( " loaded services \n " ) ) ;
1996-05-04 11:50:46 +04:00
2007-04-20 22:34:33 +04:00
if ( is_a_socket ( 0 ) ) {
if ( server_mode = = SERVER_MODE_DAEMON ) {
DEBUG ( 0 , ( " standard input is a socket, "
" assuming -F option \n " ) ) ;
}
server_mode = SERVER_MODE_INETD ;
1998-08-17 18:11:44 +04:00
}
1997-11-11 03:48:42 +03:00
2007-04-20 22:34:33 +04:00
if ( server_mode = = SERVER_MODE_DAEMON ) {
1998-08-17 18:11:44 +04:00
DEBUG ( 3 , ( " Becoming a daemon. \n " ) ) ;
2007-04-20 22:34:33 +04:00
become_daemon ( True , no_process_group ) ;
} else if ( server_mode = = SERVER_MODE_FOREGROUND ) {
become_daemon ( False , no_process_group ) ;
1998-08-17 18:11:44 +04:00
}
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 .
*/
2007-04-20 22:34:33 +04:00
if ( server_mode = = SERVER_MODE_INTERACTIVE & & ! no_process_group ) {
2001-12-30 04:46:38 +03:00
setpgid ( ( pid_t ) 0 , ( pid_t ) 0 ) ;
2007-04-20 22:34:33 +04:00
}
2001-12-30 04:46:38 +03:00
# endif
2002-12-20 23:21:31 +03:00
if ( ! directory_exist ( lp_lockdir ( ) , NULL ) )
1998-08-17 18:11:44 +04:00
mkdir ( lp_lockdir ( ) , 0755 ) ;
1996-05-04 11:50:46 +04:00
2007-04-20 22:34:33 +04:00
if ( server_mode ! = SERVER_MODE_INETD & &
server_mode ! = SERVER_MODE_INTERACTIVE ) {
1998-08-17 18:11:44 +04:00
pidfile_create ( " smbd " ) ;
2007-04-20 22:34:33 +04:00
}
1996-08-15 19:11:34 +04:00
2004-02-19 04:55:24 +03:00
/* Setup all the TDB's - including CLEAR_IF_FIRST tdb's. */
2007-05-29 18:49:19 +04:00
2007-05-15 19:14:32 +04:00
if ( smbd_messaging_context ( ) = = NULL )
2000-10-12 23:42:18 +04:00
exit ( 1 ) ;
2002-12-20 23:21:31 +03:00
2006-04-18 13:02:58 +04:00
/* Initialise the password backed before the global_sam_sid
to ensure that we fetch from ldap before we make a domain sid up */
2007-03-11 19:49:16 +03:00
if ( ! initialize_password_db ( False , smbd_event_context ( ) ) )
2006-04-18 13:02:58 +04:00
exit ( 1 ) ;
if ( ! secrets_init ( ) ) {
DEBUG ( 0 , ( " ERROR: smbd can not open secrets.tdb \n " ) ) ;
exit ( 1 ) ;
}
if ( ! get_global_sam_sid ( ) ) {
DEBUG ( 0 , ( " ERROR: Samba cannot create a SAM SID. \n " ) ) ;
exit ( 1 ) ;
}
2004-02-19 04:55:24 +03:00
if ( ! session_init ( ) )
exit ( 1 ) ;
2007-05-08 17:44:36 +04:00
if ( ! connections_init ( True ) )
2004-02-19 04:55:24 +03:00
exit ( 1 ) ;
if ( ! locking_init ( 0 ) )
exit ( 1 ) ;
namecache_enable ( ) ;
if ( ! init_registry ( ) )
exit ( 1 ) ;
2005-06-16 04:46:43 +04:00
#if 0
2005-06-14 07:17:31 +04:00
if ( ! init_svcctl_db ( ) )
exit ( 1 ) ;
2005-06-16 04:46:43 +04:00
# endif
2005-06-14 07:17:31 +04:00
2007-05-17 00:56:39 +04:00
if ( ! print_backend_init ( smbd_messaging_context ( ) ) )
2002-11-01 02:41:00 +03:00
exit ( 1 ) ;
2006-02-04 01:19:41 +03:00
if ( ! init_guest_info ( ) ) {
DEBUG ( 0 , ( " ERROR: failed to setup guest info. \n " ) ) ;
return - 1 ;
}
2004-05-27 19:38:54 +04:00
/* only start the background queue daemon if we are
running as a daemon - - bad things will happen if
smbd is launched via inetd and we fork a copy of
ourselves here */
2007-04-20 22:34:33 +04:00
if ( server_mode ! = SERVER_MODE_INETD & &
server_mode ! = SERVER_MODE_INTERACTIVE ) {
2004-05-27 19:38:54 +04:00
start_background_queue ( ) ;
2007-04-20 22:34:33 +04:00
}
2001-10-17 03:16:00 +04:00
2006-03-23 02:49:09 +03:00
/* Always attempt to initialize DMAPI. We will only use it later if
* lp_dmapi_support is set on the share , but we need a single global
* session to work with .
*/
dmapi_init_session ( ) ;
2007-04-20 22:34:33 +04:00
if ( ! open_sockets_smbd ( server_mode , ports ) ) {
1998-08-17 18:11:44 +04:00
exit ( 1 ) ;
2007-04-20 22:34:33 +04:00
}
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
*/
2003-04-16 16:13:07 +04:00
static_init_rpc ;
init_modules ( ) ;
2007-01-23 07:12:51 +03:00
/* Possibly reload the services file. Only worth doing in
* daemon mode . In inetd mode , we know we only just loaded this .
*/
2007-04-20 22:34:33 +04:00
if ( server_mode ! = SERVER_MODE_INETD & &
server_mode ! = SERVER_MODE_INTERACTIVE ) {
2007-01-23 07:12:51 +03:00
reload_services ( True ) ;
}
2000-01-03 22:19:48 +03:00
2001-12-03 20:14:23 +03:00
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 */
2007-05-15 00:31:28 +04:00
if ( ! init_oplocks ( smbd_messaging_context ( ) ) )
1998-08-17 18:11:44 +04:00
exit ( 1 ) ;
2001-10-22 10:53:28 +04:00
2005-06-28 02:53:56 +04:00
/* Setup aio signal handler. */
initialize_async_io_handler ( ) ;
2007-06-10 21:02:09 +04:00
/*
* For clustering , we need to re - init our ctdbd connection after the
* fork
*/
if ( ! NT_STATUS_IS_OK ( messaging_reinit ( smbd_messaging_context ( ) ) ) )
exit ( 1 ) ;
2002-08-17 19:27:10 +04:00
/* register our message handlers */
2007-05-16 18:24:06 +04:00
messaging_register ( smbd_messaging_context ( ) , NULL ,
MSG_SMB_FORCE_TDIS , msg_force_tdis ) ;
2002-08-17 19:27:10 +04:00
2007-03-18 16:19:40 +03:00
if ( ( lp_keepalive ( ) ! = 0 )
& & ! ( event_add_idle ( smbd_event_context ( ) , NULL ,
timeval_set ( lp_keepalive ( ) , 0 ) ,
" keepalive " , keepalive_fn ,
NULL ) ) ) {
DEBUG ( 0 , ( " Could not add keepalive event \n " ) ) ;
exit ( 1 ) ;
}
2007-03-18 13:57:46 +03:00
2007-03-18 16:19:40 +03:00
if ( ! ( event_add_idle ( smbd_event_context ( ) , NULL ,
timeval_set ( IDLE_CLOSED_TIMEOUT , 0 ) ,
" deadtime " , deadtime_fn , NULL ) ) ) {
DEBUG ( 0 , ( " Could not add deadtime event \n " ) ) ;
exit ( 1 ) ;
2007-03-18 13:57:46 +03:00
}
1998-08-17 18:11:44 +04:00
smbd_process ( ) ;
2006-04-04 04:27:50 +04:00
2003-04-23 15:54:56 +04:00
namecache_shutdown ( ) ;
2004-11-06 00:45:02 +03:00
2006-04-11 02:47:09 +04:00
exit_server_cleanly ( NULL ) ;
1998-08-17 18:11:44 +04:00
return ( 0 ) ;
1996-05-04 11:50:46 +04:00
}