2006-01-06 03:46:35 +03:00
/*
Unix SMB / CIFS implementation .
Test validity of smb . conf
Copyright ( C ) Karl Auer 1993 , 1994 - 1998
Extensively modified by Andrew Tridgell , 1995
Converted to popt by Jelmer Vernooij ( jelmer @ nl . linux . org ) , 2002
Updated for Samba4 by Andrew Bartlett < abartlet @ samba . org > 2006
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-10 06:07:03 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-01-06 03:46:35 +03: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 06:07:03 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-01-06 03:46:35 +03:00
*/
/*
* Testbed for loadparm . c / params . c
*
* This module simply loads a specified configuration file and
* if successful , dumps it ' s contents to stdout . Note that the
* operation is performed with DEBUGLEVEL at 3.
*
* Useful for a quick ' syntax check ' of a configuration file .
*
*/
# include "includes.h"
# include "system/filesys.h"
# include "lib/cmdline/popt_common.h"
# include "lib/socket/socket.h"
2007-09-08 16:42:09 +04:00
# include "param/param.h"
2008-10-24 20:06:57 +04:00
# include "param/loadparm.h"
2006-01-06 03:46:35 +03:00
/***********************************************
Here we do a set of ' hard coded ' checks for bad
configuration settings .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-09-28 05:17:46 +04:00
static int do_global_checks ( struct loadparm_context * lp_ctx )
2006-01-06 03:46:35 +03:00
{
int ret = 0 ;
2007-09-28 05:17:46 +04:00
if ( ! directory_exist ( lp_lockdir ( lp_ctx ) ) ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " ERROR: lock directory %s does not exist \n " ,
2007-09-28 05:17:46 +04:00
lp_lockdir ( lp_ctx ) ) ;
2006-01-06 03:46:35 +03:00
ret = 1 ;
}
2007-09-28 05:17:46 +04:00
if ( ! directory_exist ( lp_piddir ( lp_ctx ) ) ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " ERROR: pid directory %s does not exist \n " ,
2007-09-28 05:17:46 +04:00
lp_piddir ( lp_ctx ) ) ;
2006-01-06 03:46:35 +03:00
ret = 1 ;
}
2007-09-28 05:17:46 +04:00
if ( strlen ( lp_winbind_separator ( lp_ctx ) ) ! = 1 ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " ERROR: the 'winbind separator' parameter must be a single character. \n " ) ;
ret = 1 ;
}
2007-09-28 05:17:46 +04:00
if ( * lp_winbind_separator ( lp_ctx ) = = ' + ' ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " 'winbind separator = +' might cause problems with group membership. \n " ) ;
}
return ret ;
}
2007-12-02 19:09:52 +03:00
static int do_share_checks ( struct loadparm_context * lp_ctx , const char * cname , const char * caddr , bool silent_mode ,
bool show_defaults , const char * section_name , const char * parameter_name )
2006-01-06 03:46:35 +03:00
{
int ret = 0 ;
2007-12-02 19:09:52 +03:00
int s ;
2006-01-06 03:46:35 +03:00
2007-12-02 19:09:52 +03:00
for ( s = 0 ; s < lp_numservices ( lp_ctx ) ; s + + ) {
struct loadparm_service * service = lp_servicebynum ( lp_ctx , s ) ;
2007-09-08 20:46:30 +04:00
if ( service ! = NULL )
2007-12-02 19:09:52 +03:00
if ( strlen ( lp_servicename ( lp_servicebynum ( lp_ctx , s ) ) ) > 12 ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " WARNING: You have some share names that are longer than 12 characters. \n " ) ;
fprintf ( stderr , " These may not be accessible to some older clients. \n " ) ;
fprintf ( stderr , " (Eg. Windows9x, WindowsMe, and not listed in smbclient in Samba 3.0.) \n " ) ;
break ;
}
}
2007-12-02 19:09:52 +03:00
for ( s = 0 ; s < lp_numservices ( lp_ctx ) ; s + + ) {
struct loadparm_service * service = lp_servicebynum ( lp_ctx , s ) ;
2007-09-08 20:46:30 +04:00
if ( service ! = NULL ) {
2008-02-28 22:04:58 +03:00
const char * * deny_list = lp_hostsdeny ( service , lp_default_service ( lp_ctx ) ) ;
const char * * allow_list = lp_hostsallow ( service , lp_default_service ( lp_ctx ) ) ;
2006-01-06 03:46:35 +03:00
int i ;
if ( deny_list ) {
for ( i = 0 ; deny_list [ i ] ; i + + ) {
char * hasstar = strchr_m ( deny_list [ i ] , ' * ' ) ;
char * hasquery = strchr_m ( deny_list [ i ] , ' ? ' ) ;
if ( hasstar | | hasquery ) {
fprintf ( stderr , " Invalid character %c in hosts deny list (%s) for service %s. \n " ,
2007-09-08 20:46:30 +04:00
hasstar ? * hasstar : * hasquery , deny_list [ i ] , lp_servicename ( service ) ) ;
2006-01-06 03:46:35 +03:00
}
}
}
if ( allow_list ) {
for ( i = 0 ; allow_list [ i ] ; i + + ) {
char * hasstar = strchr_m ( allow_list [ i ] , ' * ' ) ;
char * hasquery = strchr_m ( allow_list [ i ] , ' ? ' ) ;
if ( hasstar | | hasquery ) {
fprintf ( stderr , " Invalid character %c in hosts allow list (%s) for service %s. \n " ,
2007-09-08 20:46:30 +04:00
hasstar ? * hasstar : * hasquery , allow_list [ i ] , lp_servicename ( service ) ) ;
2006-01-06 03:46:35 +03:00
}
}
}
}
}
if ( ! cname ) {
if ( ! silent_mode ) {
fprintf ( stderr , " Press enter to see a dump of your service definitions \n " ) ;
fflush ( stdout ) ;
getc ( stdin ) ;
}
2007-12-09 01:32:01 +03:00
if ( section_name ! = NULL | | parameter_name ! = NULL ) {
2007-09-09 23:34:30 +04:00
struct loadparm_service * service = NULL ;
2006-01-06 05:13:01 +03:00
if ( ! section_name ) {
section_name = GLOBAL_NAME ;
2007-09-08 20:46:30 +04:00
service = NULL ;
} else if ( ( ! strwicmp ( section_name , GLOBAL_NAME ) ) = = 0 & &
2007-12-02 19:09:52 +03:00
( service = lp_service ( lp_ctx , section_name ) ) = = NULL ) {
2006-01-06 05:13:01 +03:00
fprintf ( stderr , " Unknown section %s \n " ,
section_name ) ;
return ( 1 ) ;
}
2006-01-06 05:28:36 +03:00
if ( ! parameter_name ) {
2008-02-28 22:04:58 +03:00
lp_dump_one ( stdout , show_defaults , service , lp_default_service ( lp_ctx ) ) ;
2006-01-06 05:13:01 +03:00
} else {
2007-12-09 01:32:37 +03:00
ret = ! lp_dump_a_parameter ( lp_ctx , service , parameter_name , stdout ) ;
2006-01-06 05:13:01 +03:00
}
} else {
2007-12-02 19:09:52 +03:00
lp_dump ( lp_ctx , stdout , show_defaults , lp_numservices ( lp_ctx ) ) ;
2006-01-06 05:13:01 +03:00
}
return ( ret ) ;
2006-01-06 03:46:35 +03:00
}
if ( cname & & caddr ) {
/* this is totally ugly, a real `quick' hack */
2007-12-02 19:09:52 +03:00
for ( s = 0 ; s < lp_numservices ( lp_ctx ) ; s + + ) {
struct loadparm_service * service = lp_servicebynum ( lp_ctx , s ) ;
2007-09-08 20:46:30 +04:00
if ( service ! = NULL ) {
2008-02-28 22:04:58 +03:00
if ( allow_access ( NULL , lp_hostsdeny ( NULL , lp_default_service ( lp_ctx ) ) , lp_hostsallow ( NULL , lp_default_service ( lp_ctx ) ) , cname , caddr )
& & allow_access ( NULL , lp_hostsdeny ( service , lp_default_service ( lp_ctx ) ) , lp_hostsallow ( service , lp_default_service ( lp_ctx ) ) , cname , caddr ) ) {
2006-01-06 03:46:35 +03:00
fprintf ( stderr , " Allow connection from %s (%s) to %s \n " ,
2007-09-08 20:46:30 +04:00
cname , caddr , lp_servicename ( service ) ) ;
2006-01-06 03:46:35 +03:00
} else {
fprintf ( stderr , " Deny connection from %s (%s) to %s \n " ,
2007-09-08 20:46:30 +04:00
cname , caddr , lp_servicename ( service ) ) ;
2006-01-06 03:46:35 +03:00
}
}
}
}
2007-12-02 19:09:52 +03:00
return ret ;
}
int main ( int argc , const char * argv [ ] )
{
2007-12-20 02:02:15 +03:00
static bool silent_mode = false ;
2007-12-02 19:09:52 +03:00
int ret = 0 ;
poptContext pc ;
/*
2007-12-20 02:02:15 +03:00
static int show_all_parameters = 0 ;
static char * new_local_machine = NULL ;
2007-12-02 19:09:52 +03:00
*/
2007-12-20 02:02:15 +03:00
static const char * section_name = NULL ;
static char * parameter_name = NULL ;
static const char * cname ;
static const char * caddr ;
static bool show_defaults = false ;
2007-12-04 02:12:13 +03:00
struct loadparm_context * lp_ctx ;
2007-12-02 19:09:52 +03:00
struct poptOption long_options [ ] = {
POPT_AUTOHELP
{ " suppress-prompt " , 0 , POPT_ARG_NONE , & silent_mode , true , " Suppress prompt for enter " } ,
{ " verbose " , ' v ' , POPT_ARG_NONE , & show_defaults , true , " Show default options too " } ,
/*
We need support for smb . conf macros before this will work again
{ " server " , ' L ' , POPT_ARG_STRING , & new_local_machine , 0 , " Set %%L macro to servername \n " } ,
*/
/*
These are harder to do with the new code structure
{ " show-all-parameters " , ' \0 ' , POPT_ARG_NONE , & show_all_parameters , 1 , " Show the parameters, type, possible values " } ,
*/
{ " section-name " , ' \0 ' , POPT_ARG_STRING , & section_name , 0 , " Limit testparm to a named section " } ,
{ " parameter-name " , ' \0 ' , POPT_ARG_STRING , & parameter_name , 0 , " Limit testparm to a named parameter " } ,
{ " client-name " , ' \0 ' , POPT_ARG_STRING , & cname , 0 , " Client DNS name for 'hosts allow' checking (should match reverse lookup) " } ,
{ " client-ip " , ' \0 ' , POPT_ARG_STRING , & caddr , 0 , " Client IP address for 'hosts allow' checking " } ,
POPT_COMMON_SAMBA
POPT_COMMON_VERSION
{ NULL }
} ;
setup_logging ( NULL , DEBUG_STDERR ) ;
pc = poptGetContext ( NULL , argc , argv , long_options ,
POPT_CONTEXT_KEEP_FIRST ) ;
poptSetOtherOptionHelp ( pc , " [OPTION...] [host-name] [host-ip] " ) ;
while ( poptGetNextOpt ( pc ) ! = - 1 ) ;
/*
if ( show_all_parameters ) {
show_parameter_list ( ) ;
exit ( 0 ) ;
}
*/
if ( cname & & ! caddr ) {
printf ( " ERROR: For 'hosts allow' check you must specify both a DNS name and an IP address. \n " ) ;
return ( 1 ) ;
}
/*
We need support for smb . conf macros before this will work again
if ( new_local_machine ) {
set_local_machine_name ( new_local_machine , True ) ;
}
*/
2007-12-09 01:32:37 +03:00
2007-12-10 06:33:16 +03:00
lp_ctx = cmdline_lp_ctx ;
2007-12-02 19:09:52 +03:00
/* We need this to force the output */
2007-12-09 01:32:37 +03:00
lp_set_cmdline ( lp_ctx , " log level " , " 2 " ) ;
2007-12-02 19:09:52 +03:00
2007-12-09 01:32:37 +03:00
fprintf ( stderr , " Loaded smb config files from %s \n " , lp_configfile ( lp_ctx ) ) ;
2007-12-02 19:09:52 +03:00
2007-12-09 01:32:37 +03:00
if ( ! lp_load ( lp_ctx , lp_configfile ( lp_ctx ) ) ) {
2007-12-02 19:09:52 +03:00
fprintf ( stderr , " Error loading services. \n " ) ;
return ( 1 ) ;
}
fprintf ( stderr , " Loaded services file OK. \n " ) ;
2007-12-04 02:12:13 +03:00
ret = do_global_checks ( lp_ctx ) ;
ret | = do_share_checks ( lp_ctx , cname , caddr , silent_mode , show_defaults , section_name , parameter_name ) ;
2007-12-02 19:09:52 +03:00
2006-01-06 03:46:35 +03:00
return ( ret ) ;
}