2008-12-21 09:34:27 +03:00
/*
Unix SMB / CIFS implementation .
Samba utility functions
Copyright ( C ) Jelmer Vernooij < jelmer @ samba . org > 2007 - 2008
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 3 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 , see < http : //www.gnu.org/licenses/>.
*/
2009-10-23 09:23:01 +04:00
# include <Python.h>
2016-12-10 15:55:43 +03:00
# include "python/py3compat.h"
2008-12-21 09:34:27 +03:00
# include "includes.h"
# include "param/param.h"
# include "param/loadparm.h"
2011-08-14 17:34:08 +04:00
# include <pytalloc.h>
2011-02-05 02:34:51 +03:00
# include "dynconfig/dynconfig.h"
2008-12-21 09:34:27 +03:00
2011-08-10 17:15:18 +04:00
# define PyLoadparmContext_AsLoadparmContext(obj) pytalloc_get_type(obj, struct loadparm_context)
# define PyLoadparmService_AsLoadparmService(obj) pytalloc_get_type(obj, struct loadparm_service)
2008-12-21 09:34:27 +03:00
2010-11-22 02:07:21 +03:00
extern PyTypeObject PyLoadparmContext ;
extern PyTypeObject PyLoadparmService ;
2008-12-21 09:34:27 +03:00
2011-03-19 02:44:36 +03:00
static PyObject * PyLoadparmService_FromService ( struct loadparm_service * service )
2008-12-21 09:34:27 +03:00
{
2011-08-10 17:15:18 +04:00
return pytalloc_reference ( & PyLoadparmService , service ) ;
2008-12-21 09:34:27 +03:00
}
static PyObject * py_lp_ctx_get_helper ( struct loadparm_context * lp_ctx , const char * service_name , const char * param_name )
{
2010-06-20 16:14:01 +04:00
struct parm_struct * parm = NULL ;
void * parm_ptr = NULL ;
int i ;
2008-12-21 09:34:27 +03:00
2010-06-20 17:04:42 +04:00
if ( service_name ! = NULL & & strwicmp ( service_name , GLOBAL_NAME ) & &
strwicmp ( service_name , GLOBAL_NAME2 ) ) {
2010-06-20 16:14:01 +04:00
struct loadparm_service * service ;
/* its a share parameter */
2010-07-16 08:32:42 +04:00
service = lpcfg_service ( lp_ctx , service_name ) ;
2010-06-20 16:14:01 +04:00
if ( service = = NULL ) {
return NULL ;
}
if ( strchr ( param_name , ' : ' ) ) {
/* its a parametric option on a share */
2010-07-16 08:32:42 +04:00
const char * type = talloc_strndup ( lp_ctx , param_name ,
2010-06-20 16:14:01 +04:00
strcspn ( param_name , " : " ) ) ;
const char * option = strchr ( param_name , ' : ' ) + 1 ;
const char * value ;
if ( type = = NULL | | option = = NULL ) {
return NULL ;
}
2010-07-16 08:32:42 +04:00
value = lpcfg_get_parametric ( lp_ctx , service , type , option ) ;
2010-06-20 16:14:01 +04:00
if ( value = = NULL ) {
return NULL ;
}
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( value ) ;
2010-06-20 16:14:01 +04:00
}
2008-12-21 09:34:27 +03:00
2011-07-07 14:33:55 +04:00
parm = lpcfg_parm_struct ( lp_ctx , param_name ) ;
2011-06-29 08:15:32 +04:00
if ( parm = = NULL | | parm - > p_class = = P_GLOBAL ) {
2010-06-20 16:14:01 +04:00
return NULL ;
}
2010-07-16 08:32:42 +04:00
parm_ptr = lpcfg_parm_ptr ( lp_ctx , service , parm ) ;
2008-12-21 09:34:27 +03:00
} else if ( strchr ( param_name , ' : ' ) ) {
2010-06-20 16:14:01 +04:00
/* its a global parametric option */
2010-07-16 08:32:42 +04:00
const char * type = talloc_strndup ( lp_ctx ,
2010-06-20 16:14:01 +04:00
param_name , strcspn ( param_name , " : " ) ) ;
const char * option = strchr ( param_name , ' : ' ) + 1 ;
const char * value ;
if ( type = = NULL | | option = = NULL ) {
return NULL ;
}
2010-07-16 08:32:42 +04:00
value = lpcfg_get_parametric ( lp_ctx , NULL , type , option ) ;
2010-06-20 16:14:01 +04:00
if ( value = = NULL )
return NULL ;
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( value ) ;
2010-06-20 16:14:01 +04:00
} else {
/* its a global parameter */
2011-07-07 14:33:55 +04:00
parm = lpcfg_parm_struct ( lp_ctx , param_name ) ;
2010-06-20 16:14:01 +04:00
if ( parm = = NULL ) {
return NULL ;
}
2010-07-16 08:32:42 +04:00
parm_ptr = lpcfg_parm_ptr ( lp_ctx , NULL , parm ) ;
2008-12-21 09:34:27 +03:00
}
2010-06-20 16:14:01 +04:00
if ( parm = = NULL | | parm_ptr = = NULL ) {
return NULL ;
2008-12-21 09:34:27 +03:00
}
/* construct and return the right type of python object */
switch ( parm - > type ) {
2011-07-15 05:37:39 +04:00
case P_CHAR :
2016-12-10 15:55:43 +03:00
return PyStr_FromFormat ( " %c " , * ( char * ) parm_ptr ) ;
2008-12-21 09:34:27 +03:00
case P_STRING :
case P_USTRING :
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( * ( char * * ) parm_ptr ) ;
2008-12-21 09:34:27 +03:00
case P_BOOL :
return PyBool_FromLong ( * ( bool * ) parm_ptr ) ;
2011-07-15 05:37:39 +04:00
case P_BOOLREV :
return PyBool_FromLong ( ! ( * ( bool * ) parm_ptr ) ) ;
2008-12-21 09:34:27 +03:00
case P_INTEGER :
case P_OCTAL :
case P_BYTES :
return PyLong_FromLong ( * ( int * ) parm_ptr ) ;
case P_ENUM :
for ( i = 0 ; parm - > enum_list [ i ] . name ; i + + ) {
if ( * ( int * ) parm_ptr = = parm - > enum_list [ i ] . value ) {
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( parm - > enum_list [ i ] . name ) ;
2008-12-21 09:34:27 +03:00
}
}
return NULL ;
2010-12-08 08:27:38 +03:00
case P_CMDLIST :
2008-12-21 09:34:27 +03:00
case P_LIST :
{
int j ;
const char * * strlist = * ( const char * * * ) parm_ptr ;
2009-08-04 02:03:04 +04:00
PyObject * pylist ;
2016-05-03 05:39:08 +03:00
if ( strlist = = NULL ) {
return PyList_New ( 0 ) ;
}
2009-08-04 02:03:04 +04:00
2016-05-03 05:39:08 +03:00
pylist = PyList_New ( str_list_length ( strlist ) ) ;
2008-12-21 09:34:27 +03:00
for ( j = 0 ; strlist [ j ] ; j + + )
PyList_SetItem ( pylist , j ,
2016-12-10 15:55:43 +03:00
PyStr_FromString ( strlist [ j ] ) ) ;
2008-12-21 09:34:27 +03:00
return pylist ;
}
}
return NULL ;
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_load ( PyObject * self , PyObject * args )
2008-12-21 09:34:27 +03:00
{
char * filename ;
bool ret ;
if ( ! PyArg_ParseTuple ( args , " s " , & filename ) )
return NULL ;
2010-07-16 08:32:42 +04:00
ret = lpcfg_load ( PyLoadparmContext_AsLoadparmContext ( self ) , filename ) ;
2008-12-21 09:34:27 +03:00
if ( ! ret ) {
2008-12-22 01:05:35 +03:00
PyErr_Format ( PyExc_RuntimeError , " Unable to load file %s " , filename ) ;
2008-12-21 09:34:27 +03:00
return NULL ;
}
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_load_default ( PyObject * self , PyObject * unused )
2008-12-21 09:34:27 +03:00
{
bool ret ;
2010-07-16 08:32:42 +04:00
ret = lpcfg_load_default ( PyLoadparmContext_AsLoadparmContext ( self ) ) ;
2008-12-21 09:34:27 +03:00
if ( ! ret ) {
2008-12-22 01:05:35 +03:00
PyErr_SetString ( PyExc_RuntimeError , " Unable to load default file " ) ;
2008-12-21 09:34:27 +03:00
return NULL ;
}
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_get ( PyObject * self , PyObject * args )
2008-12-21 09:34:27 +03:00
{
char * param_name ;
char * section_name = NULL ;
PyObject * ret ;
2010-06-20 15:51:14 +04:00
if ( ! PyArg_ParseTuple ( args , " s|z " , & param_name , & section_name ) )
2008-12-21 09:34:27 +03:00
return NULL ;
2009-02-05 12:04:01 +03:00
ret = py_lp_ctx_get_helper ( PyLoadparmContext_AsLoadparmContext ( self ) , section_name , param_name ) ;
2008-12-21 09:34:27 +03:00
if ( ret = = NULL )
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 09:34:27 +03:00
return ret ;
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_is_myname ( PyObject * self , PyObject * args )
2008-12-21 09:34:27 +03:00
{
char * name ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2010-07-16 08:32:42 +04:00
return PyBool_FromLong ( lpcfg_is_myname ( PyLoadparmContext_AsLoadparmContext ( self ) , name ) ) ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_is_mydomain ( PyObject * self , PyObject * args )
2008-12-21 18:32:47 +03:00
{
char * name ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2010-07-16 08:32:42 +04:00
return PyBool_FromLong ( lpcfg_is_mydomain ( PyLoadparmContext_AsLoadparmContext ( self ) , name ) ) ;
2008-12-21 18:32:47 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_set ( PyObject * self , PyObject * args )
2008-12-21 09:34:27 +03:00
{
char * name , * value ;
bool ret ;
if ( ! PyArg_ParseTuple ( args , " ss " , & name , & value ) )
return NULL ;
2010-07-16 08:32:42 +04:00
ret = lpcfg_set_cmdline ( PyLoadparmContext_AsLoadparmContext ( self ) , name , value ) ;
2008-12-21 09:34:27 +03:00
if ( ! ret ) {
PyErr_SetString ( PyExc_RuntimeError , " Unable to set parameter " ) ;
return NULL ;
}
2009-01-06 06:13:57 +03:00
Py_RETURN_NONE ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_private_path ( PyObject * self , PyObject * args )
2008-12-21 09:34:27 +03:00
{
char * name , * path ;
PyObject * ret ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2011-04-29 06:47:11 +04:00
path = lpcfg_private_path ( NULL , PyLoadparmContext_AsLoadparmContext ( self ) , name ) ;
2016-12-10 15:55:43 +03:00
ret = PyStr_FromString ( path ) ;
2008-12-21 09:34:27 +03:00
talloc_free ( path ) ;
return ret ;
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_services ( PyObject * self , PyObject * unused )
2009-06-16 04:24:43 +04:00
{
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
PyObject * ret ;
int i ;
2010-07-16 08:32:42 +04:00
ret = PyList_New ( lpcfg_numservices ( lp_ctx ) ) ;
for ( i = 0 ; i < lpcfg_numservices ( lp_ctx ) ; i + + ) {
struct loadparm_service * service = lpcfg_servicebynum ( lp_ctx , i ) ;
2009-08-04 02:00:25 +04:00
if ( service ! = NULL ) {
2016-12-10 15:55:43 +03:00
PyList_SetItem ( ret , i , PyStr_FromString ( lpcfg_servicename ( service ) ) ) ;
2009-08-04 02:00:25 +04:00
}
2009-06-16 04:24:43 +04:00
}
return ret ;
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_server_role ( PyObject * self , PyObject * unused )
2011-11-10 08:42:44 +04:00
{
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
uint32_t role ;
const char * role_str ;
role = lpcfg_server_role ( lp_ctx ) ;
role_str = server_role_str ( role ) ;
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( role_str ) ;
2011-11-10 08:42:44 +04:00
}
2010-06-20 15:29:35 +04:00
static PyObject * py_lp_dump ( PyObject * self , PyObject * args )
{
bool show_defaults = false ;
2016-12-10 15:55:43 +03:00
const char * file_name = " " ;
const char * mode = " w " ;
2010-06-20 15:29:35 +04:00
FILE * f ;
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
2016-12-10 15:55:43 +03:00
if ( ! PyArg_ParseTuple ( args , " |bss " , & show_defaults , & file_name , & mode ) )
2010-06-20 15:29:35 +04:00
return NULL ;
2016-12-10 15:55:43 +03:00
if ( file_name [ 0 ] = = ' \0 ' ) {
f = stdout ;
} else {
f = fopen ( file_name , mode ) ;
}
2010-06-20 15:29:35 +04:00
if ( f = = NULL ) {
2016-12-10 15:55:43 +03:00
PyErr_SetFromErrno ( PyExc_IOError ) ;
2010-06-20 15:29:35 +04:00
return NULL ;
}
2010-07-16 08:32:42 +04:00
lpcfg_dump ( lp_ctx , f , show_defaults , lpcfg_numservices ( lp_ctx ) ) ;
2010-06-20 15:29:35 +04:00
2016-12-10 15:55:43 +03:00
if ( f ! = stdout ) {
fclose ( f ) ;
}
2010-06-20 15:29:35 +04:00
Py_RETURN_NONE ;
}
2013-12-27 08:09:35 +04:00
static PyObject * py_lp_dump_a_parameter ( PyObject * self , PyObject * args )
{
char * param_name ;
2014-01-03 02:36:08 +04:00
const char * section_name = NULL ;
2016-12-10 15:55:43 +03:00
const char * file_name = " " ;
const char * mode = " w " ;
2013-12-27 08:09:35 +04:00
FILE * f ;
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
struct loadparm_service * service ;
2014-01-03 02:36:08 +04:00
bool ret ;
2013-12-27 08:09:35 +04:00
2016-12-10 15:55:43 +03:00
if ( ! PyArg_ParseTuple ( args , " s|zss " , & param_name , & section_name , & file_name , & mode ) )
2013-12-27 08:09:35 +04:00
return NULL ;
2016-12-10 15:55:43 +03:00
if ( file_name [ 0 ] = = ' \0 ' ) {
f = stdout ;
} else {
f = fopen ( file_name , mode ) ;
}
2013-12-27 08:09:35 +04:00
if ( f = = NULL ) {
return NULL ;
}
if ( section_name ! = NULL & & strwicmp ( section_name , GLOBAL_NAME ) & &
strwicmp ( section_name , GLOBAL_NAME2 ) ) {
/* it's a share parameter */
service = lpcfg_service ( lp_ctx , section_name ) ;
if ( service = = NULL ) {
2014-01-03 02:36:08 +04:00
PyErr_Format ( PyExc_RuntimeError , " Unknown section %s " , section_name ) ;
return NULL ;
2013-12-27 08:09:35 +04:00
}
} else {
/* it's global */
service = NULL ;
2014-01-03 02:36:08 +04:00
section_name = " global " ;
2013-12-27 08:09:35 +04:00
}
2014-01-03 02:36:08 +04:00
ret = lpcfg_dump_a_parameter ( lp_ctx , service , param_name , f ) ;
if ( ! ret ) {
PyErr_Format ( PyExc_RuntimeError , " Parameter %s unknown for section %s " , param_name , section_name ) ;
2017-10-25 20:25:20 +03:00
if ( f ! = stdout ) {
fclose ( f ) ;
}
2014-01-03 02:36:08 +04:00
return NULL ;
}
2013-12-27 08:09:35 +04:00
2016-12-10 15:55:43 +03:00
if ( f ! = stdout ) {
fclose ( f ) ;
}
2013-12-27 08:09:35 +04:00
Py_RETURN_NONE ;
}
2016-09-13 11:48:03 +03:00
static PyObject * py_lp_log_level ( PyObject * self , PyObject * unused )
{
2018-11-07 16:14:05 +03:00
int ret = debuglevel_get ( ) ;
2016-09-13 11:48:03 +03:00
return PyInt_FromLong ( ret ) ;
}
2016-02-29 06:27:31 +03:00
static PyObject * py_samdb_url ( PyObject * self , PyObject * unused )
2011-06-02 09:43:40 +04:00
{
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
2016-12-10 15:55:43 +03:00
return PyStr_FromFormat ( " tdb://%s/sam.ldb " , lpcfg_private_dir ( lp_ctx ) ) ;
2011-06-02 09:43:40 +04:00
}
2018-04-11 00:07:34 +03:00
static PyObject * py_cache_path ( PyObject * self , PyObject * args )
{
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
char * name = NULL ;
char * path = NULL ;
PyObject * ret = NULL ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) ) {
return NULL ;
}
path = lpcfg_cache_path ( NULL , lp_ctx , name ) ;
if ( ! path ) {
PyErr_Format ( PyExc_RuntimeError ,
" Unable to access cache %s " , name ) ;
return NULL ;
}
ret = PyStr_FromString ( path ) ;
talloc_free ( path ) ;
return ret ;
}
2010-06-20 15:40:49 +04:00
2018-06-29 23:08:34 +03:00
static PyObject * py_state_path ( PyObject * self , PyObject * args )
{
struct loadparm_context * lp_ctx =
PyLoadparmContext_AsLoadparmContext ( self ) ;
char * name = NULL ;
char * path = NULL ;
PyObject * ret = NULL ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) ) {
return NULL ;
}
path = lpcfg_state_path ( NULL , lp_ctx , name ) ;
if ( ! path ) {
PyErr_Format ( PyExc_RuntimeError ,
" Unable to access cache %s " , name ) ;
return NULL ;
}
ret = PyStr_FromString ( path ) ;
talloc_free ( path ) ;
return ret ;
}
2008-12-21 09:34:27 +03:00
static PyMethodDef py_lp_ctx_methods [ ] = {
2016-02-29 06:27:31 +03:00
{ " load " , py_lp_ctx_load , METH_VARARGS ,
2008-12-21 09:34:27 +03:00
" S.load(filename) -> None \n "
" Load specified file. " } ,
2016-02-29 06:27:31 +03:00
{ " load_default " , py_lp_ctx_load_default , METH_NOARGS ,
2008-12-21 09:34:27 +03:00
" S.load_default() -> None \n "
" Load default smb.conf file. " } ,
2016-02-29 06:27:31 +03:00
{ " is_myname " , py_lp_ctx_is_myname , METH_VARARGS ,
2008-12-21 09:34:27 +03:00
" S.is_myname(name) -> bool \n "
" Check whether the specified name matches one of our netbios names. " } ,
2016-02-29 06:27:31 +03:00
{ " is_mydomain " , py_lp_ctx_is_mydomain , METH_VARARGS ,
2008-12-21 18:32:47 +03:00
" S.is_mydomain(name) -> bool \n "
" Check whether the specified name matches our domain name. " } ,
2016-02-29 06:27:31 +03:00
{ " get " , py_lp_ctx_get , METH_VARARGS ,
2008-12-21 09:34:27 +03:00
" S.get(name, service_name) -> value \n "
" Find specified parameter. " } ,
2016-02-29 06:27:31 +03:00
{ " set " , py_lp_ctx_set , METH_VARARGS ,
2008-12-21 09:34:27 +03:00
" S.set(name, value) -> bool \n "
" Change a parameter. " } ,
2016-02-29 06:27:31 +03:00
{ " private_path " , py_lp_ctx_private_path , METH_VARARGS ,
2008-12-21 09:34:27 +03:00
" S.private_path(name) -> path \n " } ,
2016-02-29 06:27:31 +03:00
{ " services " , py_lp_ctx_services , METH_NOARGS ,
2009-06-16 04:24:43 +04:00
" S.services() -> list " } ,
2016-02-29 06:27:31 +03:00
{ " server_role " , py_lp_ctx_server_role , METH_NOARGS ,
2011-11-10 08:42:44 +04:00
" S.server_role() -> value \n "
" Get the server role. " } ,
2016-02-29 06:27:31 +03:00
{ " dump " , py_lp_dump , METH_VARARGS ,
2016-12-10 15:55:43 +03:00
" S.dump(show_defaults=False, file_name='', mode='w') " } ,
{ " dump_a_parameter " , py_lp_dump_a_parameter , METH_VARARGS ,
" S.dump_a_parameter(name, service_name, file_name='', mode='w') " } ,
2016-09-13 11:48:03 +03:00
{ " log_level " , py_lp_log_level , METH_NOARGS ,
" S.log_level() -> int \n Get the active log level " } ,
2016-02-29 06:27:31 +03:00
{ " samdb_url " , py_samdb_url , METH_NOARGS ,
2011-06-02 09:43:40 +04:00
" S.samdb_url() -> string \n "
" Returns the current URL for sam.ldb. " } ,
2018-04-11 00:07:34 +03:00
{ " cache_path " , py_cache_path , METH_VARARGS ,
" S.cache_path(name) -> string \n "
" Returns a path in the Samba cache directory. " } ,
2018-06-29 23:08:34 +03:00
{ " state_path " , py_state_path , METH_VARARGS ,
" S.state_path(name) -> string \n "
" Returns a path in the Samba state directory. " } ,
2008-12-21 09:34:27 +03:00
{ NULL }
} ;
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_default_service ( PyObject * self , void * closure )
2008-12-21 09:34:27 +03:00
{
2010-07-16 08:32:42 +04:00
return PyLoadparmService_FromService ( lpcfg_default_service ( PyLoadparmContext_AsLoadparmContext ( self ) ) ) ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_config_file ( PyObject * self , void * closure )
2008-12-21 09:34:27 +03:00
{
2010-07-16 08:32:42 +04:00
const char * configfile = lpcfg_configfile ( PyLoadparmContext_AsLoadparmContext ( self ) ) ;
2009-01-15 23:16:31 +03:00
if ( configfile = = NULL )
Py_RETURN_NONE ;
else
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( configfile ) ;
2008-12-21 09:34:27 +03:00
}
static PyGetSetDef py_lp_ctx_getset [ ] = {
2018-12-13 13:10:40 +03:00
{
. name = discard_const_p ( char , " default_service " ) ,
. get = ( getter ) py_lp_ctx_default_service ,
} ,
{
. name = discard_const_p ( char , " configfile " ) ,
. get = ( getter ) py_lp_ctx_config_file ,
. doc = discard_const_p ( char , " Name of last config file that was loaded. " )
} ,
{ . name = NULL }
2008-12-21 09:34:27 +03:00
} ;
static PyObject * py_lp_ctx_new ( PyTypeObject * type , PyObject * args , PyObject * kwargs )
{
2018-05-01 02:10:36 +03:00
const char * kwnames [ ] = { " filename_for_non_global_lp " , NULL } ;
PyObject * lp_ctx ;
const char * non_global_conf = NULL ;
struct loadparm_context * ctx ;
if ( ! PyArg_ParseTupleAndKeywords ( args ,
kwargs ,
" |s " ,
discard_const_p ( char * ,
kwnames ) ,
& non_global_conf ) ) {
return NULL ;
}
/*
* by default , any LoadParm python objects map to a single global
* underlying object . The filename_for_non_global_lp arg overrides this
* default behaviour and creates a separate underlying LoadParm object .
*/
if ( non_global_conf ! = NULL ) {
bool ok ;
ctx = loadparm_init ( NULL ) ;
if ( ctx = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
lp_ctx = pytalloc_reference ( type , ctx ) ;
if ( lp_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
ok = lpcfg_load_no_global (
PyLoadparmContext_AsLoadparmContext ( lp_ctx ) ,
non_global_conf ) ;
if ( ! ok ) {
PyErr_Format ( PyExc_ValueError ,
" Could not load non-global conf %s " ,
non_global_conf ) ;
return NULL ;
}
return lp_ctx ;
} else {
return pytalloc_reference ( type , loadparm_init_global ( false ) ) ;
}
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static Py_ssize_t py_lp_ctx_len ( PyObject * self )
2008-12-21 09:34:27 +03:00
{
2010-07-16 08:32:42 +04:00
return lpcfg_numservices ( PyLoadparmContext_AsLoadparmContext ( self ) ) ;
2008-12-21 09:34:27 +03:00
}
2016-02-29 06:27:31 +03:00
static PyObject * py_lp_ctx_getitem ( PyObject * self , PyObject * name )
2008-12-21 09:34:27 +03:00
{
struct loadparm_service * service ;
2019-06-07 11:39:11 +03:00
if ( ! ( PyUnicode_Check ( name ) | | PyUnicode_Check ( name ) ) ) {
2008-12-21 09:34:27 +03:00
PyErr_SetString ( PyExc_TypeError , " Only string subscripts are supported " ) ;
return NULL ;
}
2016-12-10 15:55:43 +03:00
service = lpcfg_service ( PyLoadparmContext_AsLoadparmContext ( self ) , PyStr_AsString ( name ) ) ;
2008-12-21 09:34:27 +03:00
if ( service = = NULL ) {
PyErr_SetString ( PyExc_KeyError , " No such section " ) ;
return NULL ;
}
return PyLoadparmService_FromService ( service ) ;
}
static PyMappingMethods py_lp_ctx_mapping = {
. mp_length = ( lenfunc ) py_lp_ctx_len ,
. mp_subscript = ( binaryfunc ) py_lp_ctx_getitem ,
} ;
PyTypeObject PyLoadparmContext = {
2011-07-25 05:45:14 +04:00
. tp_name = " param.LoadParm " ,
2008-12-21 09:34:27 +03:00
. tp_getset = py_lp_ctx_getset ,
. tp_methods = py_lp_ctx_methods ,
. tp_new = py_lp_ctx_new ,
. tp_as_mapping = & py_lp_ctx_mapping ,
. tp_flags = Py_TPFLAGS_DEFAULT ,
} ;
2010-06-20 15:40:49 +04:00
static PyObject * py_lp_service_dump ( PyObject * self , PyObject * args )
{
bool show_defaults = false ;
FILE * f ;
2016-12-10 15:55:43 +03:00
const char * file_name = " " ;
const char * mode = " w " ;
2010-06-20 15:40:49 +04:00
struct loadparm_service * service = PyLoadparmService_AsLoadparmService ( self ) ;
struct loadparm_service * default_service ;
PyObject * py_default_service ;
2016-12-10 15:55:43 +03:00
if ( ! PyArg_ParseTuple ( args , " O|bss " , & py_default_service , & show_defaults , & file_name , & mode ) )
2010-06-20 15:40:49 +04:00
return NULL ;
2016-12-10 15:55:43 +03:00
if ( file_name [ 0 ] = = ' \0 ' ) {
f = stdout ;
} else {
f = fopen ( file_name , mode ) ;
}
2010-06-20 15:40:49 +04:00
if ( f = = NULL ) {
return NULL ;
}
if ( ! PyObject_TypeCheck ( py_default_service , & PyLoadparmService ) ) {
PyErr_SetNone ( PyExc_TypeError ) ;
2017-10-25 20:25:20 +03:00
if ( f ! = stdout ) {
fclose ( f ) ;
}
2010-06-20 15:40:49 +04:00
return NULL ;
}
default_service = PyLoadparmService_AsLoadparmService ( py_default_service ) ;
2010-07-16 08:32:42 +04:00
lpcfg_dump_one ( f , show_defaults , service , default_service ) ;
2010-06-20 15:40:49 +04:00
2016-12-10 15:55:43 +03:00
if ( f ! = stdout ) {
fclose ( f ) ;
}
2010-06-20 15:40:49 +04:00
Py_RETURN_NONE ;
}
static PyMethodDef py_lp_service_methods [ ] = {
{ " dump " , ( PyCFunction ) py_lp_service_dump , METH_VARARGS ,
2016-12-10 15:55:43 +03:00
" S.dump(default_service, show_defaults=False, file_name='', mode='w') " } ,
2010-06-20 15:40:49 +04:00
{ NULL }
} ;
2008-12-21 09:34:27 +03:00
PyTypeObject PyLoadparmService = {
2011-07-25 05:45:14 +04:00
. tp_name = " param.LoadparmService " ,
2010-06-20 15:40:49 +04:00
. tp_methods = py_lp_service_methods ,
2008-12-21 09:34:27 +03:00
. tp_flags = Py_TPFLAGS_DEFAULT ,
} ;
2019-05-02 21:45:14 +03:00
static PyObject * py_default_path ( PyObject * self , PyObject * Py_UNUSED ( ignored ) )
2009-01-16 17:05:15 +03:00
{
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( lp_default_path ( ) ) ;
2009-01-16 17:05:15 +03:00
}
2019-05-02 21:45:14 +03:00
static PyObject * py_setup_dir ( PyObject * self , PyObject * Py_UNUSED ( ignored ) )
2011-02-05 02:34:51 +03:00
{
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( dyn_SETUPDIR ) ;
2011-02-05 02:34:51 +03:00
}
2019-05-02 21:45:14 +03:00
static PyObject * py_modules_dir ( PyObject * self , PyObject * Py_UNUSED ( ignored ) )
2011-06-06 08:39:19 +04:00
{
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( dyn_MODULESDIR ) ;
2011-12-12 07:18:21 +04:00
}
2019-05-02 21:45:14 +03:00
static PyObject * py_bin_dir ( PyObject * self , PyObject * Py_UNUSED ( ignored ) )
2011-12-12 07:18:21 +04:00
{
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( dyn_BINDIR ) ;
2011-12-12 07:18:21 +04:00
}
2019-05-02 21:45:14 +03:00
static PyObject * py_sbin_dir ( PyObject * self , PyObject * Py_UNUSED ( ignored ) )
2011-12-12 07:18:21 +04:00
{
2016-12-10 15:55:43 +03:00
return PyStr_FromString ( dyn_SBINDIR ) ;
2011-06-06 08:39:19 +04:00
}
2009-01-16 17:05:15 +03:00
static PyMethodDef pyparam_methods [ ] = {
2019-05-02 21:45:14 +03:00
{ " default_path " , ( PyCFunction ) py_default_path , METH_NOARGS ,
2012-02-27 01:43:15 +04:00
" Returns the default smb.conf path. " } ,
{ " setup_dir " , ( PyCFunction ) py_setup_dir , METH_NOARGS ,
" Returns the compiled in location of provision tempates. " } ,
{ " modules_dir " , ( PyCFunction ) py_modules_dir , METH_NOARGS ,
" Returns the compiled in location of modules. " } ,
{ " bin_dir " , ( PyCFunction ) py_bin_dir , METH_NOARGS ,
" Returns the compiled in BINDIR. " } ,
{ " sbin_dir " , ( PyCFunction ) py_sbin_dir , METH_NOARGS ,
" Returns the compiled in SBINDIR. " } ,
{ NULL }
2009-01-16 17:05:15 +03:00
} ;
2016-12-10 15:55:43 +03:00
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT ,
. m_name = " param " ,
. m_doc = " Parsing and writing Samba configuration files. " ,
. m_size = - 1 ,
. m_methods = pyparam_methods ,
} ;
MODULE_INIT_FUNC ( param )
2008-12-21 09:34:27 +03:00
{
PyObject * m ;
2016-12-10 15:55:43 +03:00
PyTypeObject * talloc_type = pytalloc_GetObjectType ( ) ;
if ( talloc_type = = NULL )
return NULL ;
2008-12-21 09:34:27 +03:00
2016-02-29 23:31:00 +03:00
if ( pytalloc_BaseObject_PyType_Ready ( & PyLoadparmContext ) < 0 )
2016-12-10 15:55:43 +03:00
return NULL ;
2008-12-21 09:34:27 +03:00
2016-02-29 23:31:00 +03:00
if ( pytalloc_BaseObject_PyType_Ready ( & PyLoadparmService ) < 0 )
2016-12-10 15:55:43 +03:00
return NULL ;
2009-12-20 19:49:48 +03:00
2016-12-10 15:55:43 +03:00
m = PyModule_Create ( & moduledef ) ;
2008-12-21 09:34:27 +03:00
if ( m = = NULL )
2016-12-10 15:55:43 +03:00
return NULL ;
2008-12-21 09:34:27 +03:00
Py_INCREF ( & PyLoadparmContext ) ;
2008-12-21 18:32:47 +03:00
PyModule_AddObject ( m , " LoadParm " , ( PyObject * ) & PyLoadparmContext ) ;
2016-12-10 15:55:43 +03:00
return m ;
2008-12-21 09:34:27 +03:00
}