2005-08-30 17:58:48 +04:00
/*
* Unix SMB / CIFS implementation .
* Copyright ( C ) Jelmer Vernooij 2005
*
* 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:46:15 +04:00
* the Free Software Foundation ; either version 3 of the License , or
2005-08-30 17:58:48 +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 09:23:25 +04:00
* along with this program ; if not , see < http : //www.gnu.org/licenses/>.
2005-08-30 17:58:48 +04:00
*/
# include "includes.h"
2008-10-11 23:31:42 +04:00
# include "../lib/util/dlinklist.h"
2009-11-24 05:43:59 +03:00
# include "../lib/util/parmlist.h"
2006-03-14 18:22:36 +03:00
# include "param/param.h"
2008-10-24 20:06:57 +04:00
# include "param/loadparm.h"
2005-08-30 18:44:33 +04:00
# include "system/filesys.h"
2005-08-30 17:58:48 +04:00
struct param_section * param_get_section ( struct param_context * ctx , const char * name )
{
struct param_section * sect ;
if ( name = = NULL )
name = GLOBAL_NAME ;
for ( sect = ctx - > sections ; sect ; sect = sect - > next ) {
if ( ! strcasecmp_m ( sect - > name , name ) )
return sect ;
}
return NULL ;
}
2009-09-27 01:59:35 +04:00
struct parmlist_entry * param_section_get ( struct param_section * section , const char * name )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
return parmlist_get ( section - > parameters , name ) ;
2005-08-30 17:58:48 +04:00
}
2007-12-17 13:12:36 +03:00
struct param_section * param_add_section ( struct param_context * ctx , const char * section_name )
{
struct param_section * section ;
section = talloc_zero ( ctx , struct param_section ) ;
if ( section = = NULL )
return NULL ;
section - > name = talloc_strdup ( section , section_name ) ;
DLIST_ADD_END ( ctx - > sections , section , struct param_section * ) ;
return section ;
}
2005-08-30 17:58:48 +04:00
/* Look up parameter. If it is not found, add it */
2009-09-27 01:59:35 +04:00
struct parmlist_entry * param_get_add ( struct param_context * ctx , const char * name , const char * section_name )
2005-08-30 17:58:48 +04:00
{
struct param_section * section ;
2009-09-27 01:59:35 +04:00
struct parmlist_entry * p ;
2005-08-30 17:58:48 +04:00
2007-11-21 16:49:27 +03:00
SMB_ASSERT ( section_name ! = NULL ) ;
SMB_ASSERT ( name ! = NULL ) ;
2005-08-30 17:58:48 +04:00
section = param_get_section ( ctx , section_name ) ;
if ( section = = NULL ) {
2007-12-17 13:12:36 +03:00
section = param_add_section ( ctx , section_name ) ;
2005-08-30 17:58:48 +04:00
}
p = param_section_get ( section , name ) ;
if ( p = = NULL ) {
2009-09-27 01:59:35 +04:00
p = talloc_zero ( section , struct parmlist_entry ) ;
2005-08-31 18:26:20 +04:00
if ( p = = NULL )
return NULL ;
2007-10-02 17:00:33 +04:00
p - > key = talloc_strdup ( p , name ) ;
2009-09-27 01:59:35 +04:00
DLIST_ADD_END ( section - > parameters - > entries , p , struct parmlist_entry * ) ;
2005-08-30 17:58:48 +04:00
}
return p ;
}
2009-09-27 01:59:35 +04:00
const char * param_get_string ( struct param_context * ctx , const char * param , const char * section_name )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct param_section * section = param_get_section ( ctx , section_name ) ;
2005-08-30 17:58:48 +04:00
2009-09-27 01:59:35 +04:00
if ( section = = NULL )
2005-08-30 17:58:48 +04:00
return NULL ;
2009-09-27 01:59:35 +04:00
return parmlist_get_string ( section - > parameters , param , NULL ) ;
2005-08-30 17:58:48 +04:00
}
2009-09-27 19:37:53 +04:00
int param_set_string ( struct param_context * ctx , const char * param , const char * value , const char * section_name )
2005-08-30 17:58:48 +04:00
{
2009-09-27 19:37:53 +04:00
struct param_section * section = param_get_section ( ctx , section_name ) ;
2005-08-30 17:58:48 +04:00
2009-09-27 19:37:53 +04:00
if ( section = = NULL )
2005-08-30 18:44:33 +04:00
return - 1 ;
2009-09-27 19:37:53 +04:00
return parmlist_set_string ( section - > parameters , param , value ) ;
2005-08-30 17:58:48 +04:00
}
2009-09-27 01:59:35 +04:00
const char * * param_get_string_list ( struct param_context * ctx , const char * param , const char * separator , const char * section_name )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct param_section * section = param_get_section ( ctx , section_name ) ;
if ( section = = NULL )
2005-08-30 17:58:48 +04:00
return NULL ;
2005-08-30 18:44:33 +04:00
2009-09-27 01:59:35 +04:00
return parmlist_get_string_list ( section - > parameters , param , separator ) ;
2005-08-30 17:58:48 +04:00
}
2007-12-17 13:12:36 +03:00
int param_set_string_list ( struct param_context * ctx , const char * param , const char * * list , const char * section )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct parmlist_entry * p = param_get_add ( ctx , param , section ) ;
2005-08-30 17:58:48 +04:00
p - > value = str_list_join ( p , list , ' ' ) ;
2005-08-30 20:09:38 +04:00
return 0 ;
2005-08-30 17:58:48 +04:00
}
2009-09-27 01:59:35 +04:00
int param_get_int ( struct param_context * ctx , const char * param , int default_v , const char * section_name )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct param_section * section = param_get_section ( ctx , section_name ) ;
2005-08-30 17:58:48 +04:00
2009-09-27 01:59:35 +04:00
if ( section = = NULL )
return default_v ;
return parmlist_get_int ( section - > parameters , param , default_v ) ;
2005-08-30 17:58:48 +04:00
}
2007-12-17 13:12:36 +03:00
void param_set_int ( struct param_context * ctx , const char * param , int value , const char * section )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct parmlist_entry * p = param_get_add ( ctx , section , param ) ;
2005-08-30 17:58:48 +04:00
2005-08-31 18:26:20 +04:00
if ( ! p )
return ;
2005-08-30 17:58:48 +04:00
p - > value = talloc_asprintf ( p , " %d " , value ) ;
}
2007-12-17 13:12:36 +03:00
unsigned long param_get_ulong ( struct param_context * ctx , const char * param , unsigned long default_v , const char * section )
2005-08-30 17:58:48 +04:00
{
2007-12-17 13:12:36 +03:00
const char * value = param_get_string ( ctx , param , section ) ;
2005-08-30 17:58:48 +04:00
if ( value )
return strtoul ( value , NULL , 0 ) ;
return default_v ;
}
2007-12-17 13:12:36 +03:00
void param_set_ulong ( struct param_context * ctx , const char * name , unsigned long value , const char * section )
2005-08-30 17:58:48 +04:00
{
2009-09-27 01:59:35 +04:00
struct parmlist_entry * p = param_get_add ( ctx , name , section ) ;
2005-08-30 17:58:48 +04:00
2005-08-31 18:26:20 +04:00
if ( ! p )
return ;
2005-08-30 17:58:48 +04:00
p - > value = talloc_asprintf ( p , " %lu " , value ) ;
}
2007-09-08 17:27:14 +04:00
static bool param_sfunc ( const char * name , void * _ctx )
2005-08-30 17:58:48 +04:00
{
2007-09-08 17:27:14 +04:00
struct param_context * ctx = ( struct param_context * ) _ctx ;
2005-08-30 17:58:48 +04:00
struct param_section * section = param_get_section ( ctx , name ) ;
if ( section = = NULL ) {
section = talloc_zero ( ctx , struct param_section ) ;
2005-08-31 18:26:20 +04:00
if ( section = = NULL )
2007-09-08 17:27:14 +04:00
return false ;
2005-08-31 18:26:20 +04:00
2005-08-30 17:58:48 +04:00
section - > name = talloc_strdup ( section , name ) ;
2007-11-21 16:49:27 +03:00
DLIST_ADD_END ( ctx - > sections , section , struct param_section * ) ;
2005-08-30 17:58:48 +04:00
}
2005-08-30 18:44:33 +04:00
/* Make sure this section is on top of the list for param_pfunc */
2005-08-30 17:58:48 +04:00
DLIST_PROMOTE ( ctx - > sections , section ) ;
2007-09-08 17:27:14 +04:00
return true ;
2005-08-30 17:58:48 +04:00
}
2007-09-08 17:27:14 +04:00
static bool param_pfunc ( const char * name , const char * value , void * _ctx )
2005-08-30 17:58:48 +04:00
{
2007-09-08 17:27:14 +04:00
struct param_context * ctx = ( struct param_context * ) _ctx ;
2009-09-27 01:59:35 +04:00
struct parmlist_entry * p = param_section_get ( ctx - > sections , name ) ;
2005-08-30 17:58:48 +04:00
if ( ! p ) {
2009-09-27 01:59:35 +04:00
p = talloc_zero ( ctx - > sections , struct parmlist_entry ) ;
2005-08-31 18:26:20 +04:00
if ( p = = NULL )
2007-10-07 01:39:52 +04:00
return false ;
2005-08-31 18:26:20 +04:00
2007-10-02 17:00:33 +04:00
p - > key = talloc_strdup ( p , name ) ;
2005-08-30 17:58:48 +04:00
p - > value = talloc_strdup ( p , value ) ;
2009-09-27 01:59:35 +04:00
DLIST_ADD ( ctx - > sections - > parameters - > entries , p ) ;
2005-08-30 17:58:48 +04:00
} else { /* Replace current value */
talloc_free ( p - > value ) ;
p - > value = talloc_strdup ( p , value ) ;
}
2007-10-07 01:39:52 +04:00
return true ;
2005-08-30 17:58:48 +04:00
}
2005-08-30 18:44:33 +04:00
struct param_context * param_init ( TALLOC_CTX * mem_ctx )
2005-08-30 17:58:48 +04:00
{
2005-08-30 18:44:33 +04:00
return talloc_zero ( mem_ctx , struct param_context ) ;
}
2005-08-30 17:58:48 +04:00
2005-08-30 18:44:33 +04:00
int param_read ( struct param_context * ctx , const char * fn )
{
2005-08-30 17:58:48 +04:00
ctx - > sections = talloc_zero ( ctx , struct param_section ) ;
2005-08-31 18:26:20 +04:00
if ( ctx - > sections = = NULL )
return - 1 ;
2005-08-30 17:58:48 +04:00
ctx - > sections - > name = talloc_strdup ( ctx - > sections , " global " ) ;
if ( ! pm_process ( fn , param_sfunc , param_pfunc , ctx ) ) {
2005-08-30 18:44:33 +04:00
return - 1 ;
2005-08-30 17:58:48 +04:00
}
2005-08-30 18:44:33 +04:00
return 0 ;
2005-08-30 17:58:48 +04:00
}
2007-11-21 16:49:27 +03:00
int param_use ( struct loadparm_context * lp_ctx , struct param_context * ctx )
{
struct param_section * section ;
for ( section = ctx - > sections ; section ; section = section - > next ) {
2009-09-27 01:59:35 +04:00
struct parmlist_entry * param ;
2007-11-21 16:49:27 +03:00
bool isglobal = strcmp ( section - > name , " global " ) = = 0 ;
2009-09-27 01:59:35 +04:00
for ( param = section - > parameters - > entries ; param ; param = param - > next ) {
2007-11-21 16:49:27 +03:00
if ( isglobal )
2010-07-16 08:32:42 +04:00
lpcfg_do_global_parameter ( lp_ctx , param - > key ,
2007-11-21 16:49:27 +03:00
param - > value ) ;
else {
struct loadparm_service * service =
2010-07-16 08:32:42 +04:00
lpcfg_service ( lp_ctx , section - > name ) ;
2007-11-21 16:49:27 +03:00
if ( service = = NULL )
2010-07-16 08:32:42 +04:00
service = lpcfg_add_service ( lp_ctx , lpcfg_default_service ( lp_ctx ) , section - > name ) ;
lpcfg_do_service_parameter ( lp_ctx , service , param - > key , param - > value ) ;
2007-11-21 16:49:27 +03:00
}
}
}
return 0 ;
}
2005-08-30 18:44:33 +04:00
int param_write ( struct param_context * ctx , const char * fn )
2005-08-30 17:58:48 +04:00
{
2005-09-15 23:52:13 +04:00
int file ;
2005-08-30 17:58:48 +04:00
struct param_section * section ;
2005-08-30 18:44:33 +04:00
if ( fn = = NULL | | ctx = = NULL )
2005-08-30 17:58:48 +04:00
return - 1 ;
2005-09-15 23:52:13 +04:00
file = open ( fn , O_WRONLY | O_CREAT , 0755 ) ;
2005-08-30 18:44:33 +04:00
2005-09-15 23:52:13 +04:00
if ( file = = - 1 )
2005-08-30 17:58:48 +04:00
return - 1 ;
for ( section = ctx - > sections ; section ; section = section - > next ) {
2009-09-27 01:59:35 +04:00
struct parmlist_entry * param ;
2005-08-30 17:58:48 +04:00
2005-09-15 23:52:13 +04:00
fdprintf ( file , " [%s] \n " , section - > name ) ;
2009-09-27 01:59:35 +04:00
for ( param = section - > parameters - > entries ; param ; param = param - > next ) {
2007-10-02 17:00:33 +04:00
fdprintf ( file , " \t %s = %s \n " , param - > key , param - > value ) ;
2005-08-30 17:58:48 +04:00
}
2005-09-15 23:52:13 +04:00
fdprintf ( file , " \n " ) ;
2005-08-30 17:58:48 +04:00
}
2005-09-15 23:52:13 +04:00
close ( file ) ;
2005-08-30 18:44:33 +04:00
2005-08-30 17:58:48 +04:00
return 0 ;
}