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/>.
*/
# include <stdint.h>
# include <stdbool.h>
# include "includes.h"
# include "param/param.h"
# include "param/loadparm.h"
2009-01-08 14:20:20 +03:00
# include <Python.h>
2008-12-21 09:34:27 +03:00
# include "pytalloc.h"
2008-12-23 13:25:06 +03:00
/* There's no Py_ssize_t in 2.4, apparently */
# if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
typedef int Py_ssize_t ;
typedef inquiry lenfunc ;
# endif
2009-01-08 14:20:20 +03:00
# ifndef Py_RETURN_NONE
# define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
# endif
2009-02-05 12:04:01 +03:00
# define PyLoadparmContext_AsLoadparmContext(obj) py_talloc_get_type(obj, struct loadparm_context)
2008-12-21 09:34:27 +03:00
PyAPI_DATA ( PyTypeObject ) PyLoadparmContext ;
PyAPI_DATA ( PyTypeObject ) PyLoadparmService ;
PyObject * PyLoadparmService_FromService ( struct loadparm_service * service )
{
2009-07-01 08:06:38 +04:00
return py_talloc_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 )
{
struct parm_struct * parm = NULL ;
void * parm_ptr = NULL ;
int i ;
if ( service_name ! = NULL ) {
struct loadparm_service * service ;
/* its a share parameter */
service = lp_service ( lp_ctx , service_name ) ;
if ( service = = NULL ) {
return NULL ;
}
if ( strchr ( param_name , ' : ' ) ) {
/* its a parametric option on a share */
const char * type = talloc_strndup ( lp_ctx ,
param_name ,
strcspn ( param_name , " : " ) ) ;
const char * option = strchr ( param_name , ' : ' ) + 1 ;
const char * value ;
if ( type = = NULL | | option = = NULL ) {
return NULL ;
}
value = lp_get_parametric ( lp_ctx , service , type , option ) ;
if ( value = = NULL ) {
return NULL ;
}
return PyString_FromString ( value ) ;
}
parm = lp_parm_struct ( param_name ) ;
2009-02-02 13:24:08 +03:00
if ( parm = = NULL | | parm - > pclass = = P_GLOBAL ) {
2008-12-21 09:34:27 +03:00
return NULL ;
}
parm_ptr = lp_parm_ptr ( lp_ctx , service , parm ) ;
} else if ( strchr ( param_name , ' : ' ) ) {
/* its a global parametric option */
const char * type = talloc_strndup ( lp_ctx ,
param_name , strcspn ( param_name , " : " ) ) ;
const char * option = strchr ( param_name , ' : ' ) + 1 ;
const char * value ;
if ( type = = NULL | | option = = NULL ) {
return NULL ;
}
value = lp_get_parametric ( lp_ctx , NULL , type , option ) ;
if ( value = = NULL )
return NULL ;
return PyString_FromString ( value ) ;
} else {
/* its a global parameter */
parm = lp_parm_struct ( param_name ) ;
if ( parm = = NULL ) {
return NULL ;
}
parm_ptr = lp_parm_ptr ( lp_ctx , NULL , parm ) ;
}
if ( parm = = NULL | | parm_ptr = = NULL ) {
return NULL ;
}
/* construct and return the right type of python object */
switch ( parm - > type ) {
case P_STRING :
case P_USTRING :
return PyString_FromString ( * ( char * * ) parm_ptr ) ;
case P_BOOL :
return PyBool_FromLong ( * ( bool * ) parm_ptr ) ;
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 ) {
return PyString_FromString ( parm - > enum_list [ i ] . name ) ;
}
}
return NULL ;
case P_LIST :
{
int j ;
const char * * strlist = * ( const char * * * ) parm_ptr ;
PyObject * pylist = PyList_New ( str_list_length ( strlist ) ) ;
for ( j = 0 ; strlist [ j ] ; j + + )
PyList_SetItem ( pylist , j ,
PyString_FromString ( strlist [ j ] ) ) ;
return pylist ;
}
break ;
}
return NULL ;
}
static PyObject * py_lp_ctx_load ( py_talloc_Object * self , PyObject * args )
{
char * filename ;
bool ret ;
if ( ! PyArg_ParseTuple ( args , " s " , & filename ) )
return NULL ;
2009-02-05 12:04:01 +03:00
ret = lp_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
}
static PyObject * py_lp_ctx_load_default ( py_talloc_Object * self )
{
bool ret ;
2009-02-05 12:04:01 +03:00
ret = lp_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
}
static PyObject * py_lp_ctx_get ( py_talloc_Object * self , PyObject * args )
{
char * param_name ;
char * section_name = NULL ;
PyObject * ret ;
if ( ! PyArg_ParseTuple ( args , " s|s " , & param_name , & section_name ) )
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 ;
}
static PyObject * py_lp_ctx_is_myname ( py_talloc_Object * self , PyObject * args )
{
char * name ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2009-02-05 12:04:01 +03:00
return PyBool_FromLong ( lp_is_myname ( PyLoadparmContext_AsLoadparmContext ( self ) , name ) ) ;
2008-12-21 09:34:27 +03:00
}
2008-12-21 18:32:47 +03:00
static PyObject * py_lp_ctx_is_mydomain ( py_talloc_Object * self , PyObject * args )
{
char * name ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2009-02-05 12:04:01 +03:00
return PyBool_FromLong ( lp_is_mydomain ( PyLoadparmContext_AsLoadparmContext ( self ) , name ) ) ;
2008-12-21 18:32:47 +03:00
}
2008-12-21 09:34:27 +03:00
static PyObject * py_lp_ctx_set ( py_talloc_Object * self , PyObject * args )
{
char * name , * value ;
bool ret ;
if ( ! PyArg_ParseTuple ( args , " ss " , & name , & value ) )
return NULL ;
2009-02-05 12:04:01 +03:00
ret = lp_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
}
static PyObject * py_lp_ctx_private_path ( py_talloc_Object * self , PyObject * args )
{
char * name , * path ;
PyObject * ret ;
if ( ! PyArg_ParseTuple ( args , " s " , & name ) )
return NULL ;
2009-02-05 12:04:01 +03:00
path = private_path ( NULL , PyLoadparmContext_AsLoadparmContext ( self ) , name ) ;
2008-12-21 09:34:27 +03:00
ret = PyString_FromString ( path ) ;
talloc_free ( path ) ;
return ret ;
}
2009-06-16 04:24:43 +04:00
static PyObject * py_lp_ctx_services ( py_talloc_Object * self )
{
struct loadparm_context * lp_ctx = PyLoadparmContext_AsLoadparmContext ( self ) ;
const char * * names ;
PyObject * ret ;
int i ;
names = lp_server_services ( lp_ctx ) ;
ret = PyList_New ( str_list_length ( names ) ) ;
for ( i = 0 ; names [ i ] ; i + + ) {
PyList_SetItem ( ret , i , PyString_FromString ( names [ i ] ) ) ;
}
return ret ;
}
2008-12-21 09:34:27 +03:00
static PyMethodDef py_lp_ctx_methods [ ] = {
{ " load " , ( PyCFunction ) py_lp_ctx_load , METH_VARARGS ,
" S.load(filename) -> None \n "
" Load specified file. " } ,
{ " load_default " , ( PyCFunction ) py_lp_ctx_load_default , METH_NOARGS ,
" S.load_default() -> None \n "
" Load default smb.conf file. " } ,
{ " is_myname " , ( PyCFunction ) py_lp_ctx_is_myname , METH_VARARGS ,
" S.is_myname(name) -> bool \n "
" Check whether the specified name matches one of our netbios names. " } ,
2008-12-21 18:32:47 +03:00
{ " is_mydomain " , ( PyCFunction ) py_lp_ctx_is_mydomain , METH_VARARGS ,
" S.is_mydomain(name) -> bool \n "
" Check whether the specified name matches our domain name. " } ,
2008-12-21 09:34:27 +03:00
{ " get " , ( PyCFunction ) py_lp_ctx_get , METH_VARARGS ,
" S.get(name, service_name) -> value \n "
" Find specified parameter. " } ,
{ " set " , ( PyCFunction ) py_lp_ctx_set , METH_VARARGS ,
" S.set(name, value) -> bool \n "
" Change a parameter. " } ,
{ " private_path " , ( PyCFunction ) py_lp_ctx_private_path , METH_VARARGS ,
" S.private_path(name) -> path \n " } ,
2009-06-16 04:24:43 +04:00
{ " services " , ( PyCFunction ) py_lp_ctx_services , METH_NOARGS ,
" S.services() -> list " } ,
2008-12-21 09:34:27 +03:00
{ NULL }
} ;
static PyObject * py_lp_ctx_default_service ( py_talloc_Object * self , void * closure )
{
2009-02-05 12:04:01 +03:00
return PyLoadparmService_FromService ( lp_default_service ( PyLoadparmContext_AsLoadparmContext ( self ) ) ) ;
2008-12-21 09:34:27 +03:00
}
static PyObject * py_lp_ctx_config_file ( py_talloc_Object * self , void * closure )
{
2009-02-05 12:04:01 +03:00
const char * configfile = lp_configfile ( PyLoadparmContext_AsLoadparmContext ( self ) ) ;
2009-01-15 23:16:31 +03:00
if ( configfile = = NULL )
Py_RETURN_NONE ;
else
return PyString_FromString ( configfile ) ;
2008-12-21 09:34:27 +03:00
}
static PyGetSetDef py_lp_ctx_getset [ ] = {
2009-02-05 12:04:01 +03:00
{ discard_const_p ( char , " default_service " ) , ( getter ) py_lp_ctx_default_service , NULL , NULL } ,
{ discard_const_p ( char , " configfile " ) , ( getter ) py_lp_ctx_config_file , NULL ,
discard_const_p ( char , " Name of last config file that was loaded. " ) } ,
2008-12-21 09:34:27 +03:00
{ NULL }
} ;
static PyObject * py_lp_ctx_new ( PyTypeObject * type , PyObject * args , PyObject * kwargs )
{
2009-06-17 21:07:44 +04:00
py_talloc_Object * ret = ( py_talloc_Object * ) type - > tp_alloc ( type , 0 ) ;
if ( ret = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
ret - > talloc_ctx = talloc_new ( NULL ) ;
if ( ret - > talloc_ctx = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
ret - > ptr = loadparm_init ( ret - > talloc_ctx ) ;
return ( PyObject * ) ret ;
2008-12-21 09:34:27 +03:00
}
static Py_ssize_t py_lp_ctx_len ( py_talloc_Object * self )
{
2009-02-05 12:04:01 +03:00
return lp_numservices ( PyLoadparmContext_AsLoadparmContext ( self ) ) ;
2008-12-21 09:34:27 +03:00
}
static PyObject * py_lp_ctx_getitem ( py_talloc_Object * self , PyObject * name )
{
struct loadparm_service * service ;
if ( ! PyString_Check ( name ) ) {
PyErr_SetString ( PyExc_TypeError , " Only string subscripts are supported " ) ;
return NULL ;
}
2009-02-05 12:04:01 +03:00
service = lp_service ( PyLoadparmContext_AsLoadparmContext ( self ) , PyString_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 = {
2008-12-21 18:32:47 +03:00
. tp_name = " LoadParm " ,
2008-12-21 09:34:27 +03:00
. tp_basicsize = sizeof ( py_talloc_Object ) ,
. tp_dealloc = py_talloc_dealloc ,
. 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 ,
} ;
PyTypeObject PyLoadparmService = {
. tp_name = " LoadparmService " ,
. tp_dealloc = py_talloc_dealloc ,
. tp_basicsize = sizeof ( py_talloc_Object ) ,
. tp_flags = Py_TPFLAGS_DEFAULT ,
} ;
2009-01-16 17:05:15 +03:00
static PyObject * py_default_path ( PyObject * self )
{
return PyString_FromString ( lp_default_path ( ) ) ;
}
static PyMethodDef pyparam_methods [ ] = {
{ " default_path " , ( PyCFunction ) py_default_path , METH_NOARGS ,
" Returns the default smb.conf path. " } ,
{ NULL }
} ;
2008-12-21 09:34:27 +03:00
void initparam ( void )
{
PyObject * m ;
if ( PyType_Ready ( & PyLoadparmContext ) < 0 )
return ;
2009-01-16 17:05:15 +03:00
m = Py_InitModule3 ( " param " , pyparam_methods , " Parsing and writing Samba configuration files. " ) ;
2008-12-21 09:34:27 +03:00
if ( m = = NULL )
return ;
Py_INCREF ( & PyLoadparmContext ) ;
2008-12-21 18:32:47 +03:00
PyModule_AddObject ( m , " LoadParm " , ( PyObject * ) & PyLoadparmContext ) ;
2008-12-21 09:34:27 +03:00
}