2002-04-14 09:00:13 +00:00
/*
Python wrappers for DCERPC / SMB client routines .
Copyright ( C ) Tim Potter , 2002
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 2 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 , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
2002-04-14 00:59:50 +00:00
# include "python/py_lsa.h"
2002-03-20 03:29:03 +00:00
2006-05-01 22:31:33 +00:00
PyObject * new_lsa_policy_hnd_object ( struct rpc_pipe_client * cli , TALLOC_CTX * mem_ctx ,
2002-04-14 00:59:50 +00:00
POLICY_HND * pol )
{
lsa_policy_hnd_object * o ;
o = PyObject_New ( lsa_policy_hnd_object , & lsa_policy_hnd_type ) ;
o - > cli = cli ;
o - > mem_ctx = mem_ctx ;
memcpy ( & o - > pol , pol , sizeof ( POLICY_HND ) ) ;
return ( PyObject * ) o ;
}
2002-03-20 03:29:03 +00:00
/*
* Exceptions raised by this module
*/
PyObject * lsa_error ; /* This indicates a non-RPC related error
such as name lookup failure */
PyObject * lsa_ntstatus ; /* This exception is raised when a RPC call
returns a status code other than
NT_STATUS_OK */
/*
* Open / close lsa handles
*/
2002-04-14 09:00:13 +00:00
static PyObject * lsa_open_policy ( PyObject * self , PyObject * args ,
2002-03-20 03:29:03 +00:00
PyObject * kw )
{
static char * kwlist [ ] = { " servername " , " creds " , " access " , NULL } ;
2002-05-16 04:00:31 +00:00
char * server , * errstr ;
2002-05-27 06:34:13 +00:00
PyObject * creds = NULL , * result = NULL ;
2004-10-26 01:37:19 +00:00
uint32 desired_access = GENERIC_EXECUTE_ACCESS ;
2002-05-27 06:34:13 +00:00
struct cli_state * cli = NULL ;
2002-04-14 00:59:50 +00:00
NTSTATUS ntstatus ;
2002-05-27 06:34:13 +00:00
TALLOC_CTX * mem_ctx = NULL ;
2002-04-14 00:59:50 +00:00
POLICY_HND hnd ;
2002-03-20 03:29:03 +00:00
if ( ! PyArg_ParseTupleAndKeywords (
2002-05-28 02:08:39 +00:00
args , kw , " s|Oi " , kwlist , & server , & creds , & desired_access ) )
2002-04-14 00:59:50 +00:00
return NULL ;
2002-03-20 03:29:03 +00:00
2002-05-28 02:08:39 +00:00
if ( creds & & creds ! = Py_None & & ! PyDict_Check ( creds ) ) {
PyErr_SetString ( PyExc_TypeError ,
" credentials must be dictionary or None " ) ;
return NULL ;
}
2002-05-28 03:14:28 +00:00
if ( server [ 0 ] ! = ' \\ ' | | server [ 1 ] ! = ' \\ ' ) {
PyErr_SetString ( PyExc_ValueError , " UNC name required " ) ;
return NULL ;
}
server + = 2 ;
2002-11-04 20:33:16 +00:00
if ( ! ( cli = open_pipe_creds ( server , creds , PI_LSARPC , & errstr ) ) ) {
2002-05-16 04:00:31 +00:00
PyErr_SetString ( lsa_error , errstr ) ;
free ( errstr ) ;
2002-04-14 00:59:50 +00:00
return NULL ;
2002-03-20 03:29:03 +00:00
}
2002-12-23 23:54:10 +00:00
if ( ! ( mem_ctx = talloc_init ( " lsa_open_policy " ) ) ) {
2002-05-27 06:34:13 +00:00
PyErr_SetString ( lsa_error , " unable to init talloc context \n " ) ;
goto done ;
2002-04-14 00:59:50 +00:00
}
2005-10-07 04:54:41 +00:00
ntstatus = rpccli_lsa_open_policy (
2006-05-01 22:31:33 +00:00
cli - > pipe_list , mem_ctx , True , desired_access , & hnd ) ;
2002-04-14 00:59:50 +00:00
if ( ! NT_STATUS_IS_OK ( ntstatus ) ) {
PyErr_SetObject ( lsa_ntstatus , py_ntstatus_tuple ( ntstatus ) ) ;
2002-05-27 06:34:13 +00:00
goto done ;
2002-04-14 00:59:50 +00:00
}
2006-05-01 22:31:33 +00:00
result = new_lsa_policy_hnd_object ( cli - > pipe_list , mem_ctx , & hnd ) ;
2002-04-14 00:59:50 +00:00
2002-05-27 06:34:13 +00:00
done :
if ( ! result ) {
if ( cli )
cli_shutdown ( cli ) ;
2003-08-04 00:50:00 +00:00
talloc_destroy ( mem_ctx ) ;
2002-05-27 06:34:13 +00:00
}
2002-04-14 00:59:50 +00:00
return result ;
2002-03-20 03:29:03 +00:00
}
static PyObject * lsa_close ( PyObject * self , PyObject * args , PyObject * kw )
{
2002-04-14 00:59:50 +00:00
PyObject * po ;
lsa_policy_hnd_object * hnd ;
NTSTATUS result ;
/* Parse parameters */
if ( ! PyArg_ParseTuple ( args , " O! " , & lsa_policy_hnd_type , & po ) )
return NULL ;
hnd = ( lsa_policy_hnd_object * ) po ;
/* Call rpc function */
2006-09-20 22:49:02 +00:00
result = rpccli_lsa_Close ( hnd - > cli , hnd - > mem_ctx , & hnd - > pol ) ;
2002-04-14 00:59:50 +00:00
2002-04-14 09:00:13 +00:00
/* Cleanup samba stuff */
2002-04-14 00:59:50 +00:00
cli_shutdown ( hnd - > cli ) ;
talloc_destroy ( hnd - > mem_ctx ) ;
/* Return value */
Py_INCREF ( Py_None ) ;
return Py_None ;
2002-03-20 03:29:03 +00:00
}
2002-04-14 09:00:13 +00:00
static PyObject * lsa_lookup_names ( PyObject * self , PyObject * args )
2002-03-20 03:29:03 +00:00
{
2003-08-04 00:50:00 +00:00
PyObject * py_names , * result = NULL ;
2002-04-14 09:00:13 +00:00
NTSTATUS ntstatus ;
lsa_policy_hnd_object * hnd = ( lsa_policy_hnd_object * ) self ;
int num_names , i ;
const char * * names ;
DOM_SID * sids ;
2003-08-04 00:50:00 +00:00
TALLOC_CTX * mem_ctx = NULL ;
2006-09-08 14:28:06 +00:00
enum lsa_SidType * name_types ;
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
if ( ! PyArg_ParseTuple ( args , " O " , & py_names ) )
2002-04-14 09:00:13 +00:00
return NULL ;
2002-05-06 04:53:44 +00:00
if ( ! PyList_Check ( py_names ) & & ! PyString_Check ( py_names ) ) {
PyErr_SetString ( PyExc_TypeError , " must be list or string " ) ;
return NULL ;
}
2002-04-14 09:00:13 +00:00
2003-08-04 00:50:00 +00:00
if ( ! ( mem_ctx = talloc_init ( " lsa_lookup_names " ) ) ) {
PyErr_SetString ( lsa_error , " unable to init talloc context \n " ) ;
goto done ;
}
2002-05-06 04:53:44 +00:00
if ( PyList_Check ( py_names ) ) {
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
/* Convert list to char ** array */
num_names = PyList_Size ( py_names ) ;
2005-05-09 12:43:12 +00:00
names = ( const char * * ) _talloc ( mem_ctx , num_names * sizeof ( char * ) ) ;
2002-05-06 04:53:44 +00:00
for ( i = 0 ; i < num_names ; i + + ) {
PyObject * obj = PyList_GetItem ( py_names , i ) ;
2003-08-04 00:50:00 +00:00
names [ i ] = talloc_strdup ( mem_ctx , PyString_AsString ( obj ) ) ;
2002-05-06 04:53:44 +00:00
}
} else {
/* Just a single element */
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
num_names = 1 ;
2005-05-09 12:43:12 +00:00
names = ( const char * * ) _talloc ( mem_ctx , sizeof ( char * ) ) ;
2002-05-06 04:53:44 +00:00
names [ 0 ] = PyString_AsString ( py_names ) ;
2002-04-14 09:00:13 +00:00
}
2005-10-07 04:54:41 +00:00
ntstatus = rpccli_lsa_lookup_names (
2006-02-03 22:19:41 +00:00
hnd - > cli , mem_ctx , & hnd - > pol , num_names , names ,
2007-06-27 11:42:17 +00:00
NULL , 1 , & sids , & name_types ) ;
2002-04-14 09:00:13 +00:00
if ( ! NT_STATUS_IS_OK ( ntstatus ) & & NT_STATUS_V ( ntstatus ) ! = 0x107 ) {
PyErr_SetObject ( lsa_ntstatus , py_ntstatus_tuple ( ntstatus ) ) ;
2003-08-04 00:50:00 +00:00
goto done ;
2002-04-14 09:00:13 +00:00
}
result = PyList_New ( num_names ) ;
for ( i = 0 ; i < num_names ; i + + ) {
PyObject * sid_obj , * obj ;
py_from_SID ( & sid_obj , & sids [ i ] ) ;
2003-08-04 00:50:00 +00:00
obj = Py_BuildValue ( " (Ni) " , sid_obj , name_types [ i ] ) ;
2002-04-14 09:00:13 +00:00
PyList_SetItem ( result , i , obj ) ;
}
2003-08-04 00:50:00 +00:00
done :
talloc_destroy ( mem_ctx ) ;
2002-04-14 09:00:13 +00:00
return result ;
2002-03-20 03:29:03 +00:00
}
2002-04-14 09:00:13 +00:00
static PyObject * lsa_lookup_sids ( PyObject * self , PyObject * args ,
PyObject * kw )
2002-03-20 03:29:03 +00:00
{
2003-08-04 00:50:00 +00:00
PyObject * py_sids , * result = NULL ;
2002-04-14 09:00:13 +00:00
NTSTATUS ntstatus ;
int num_sids , i ;
char * * domains , * * names ;
uint32 * types ;
lsa_policy_hnd_object * hnd = ( lsa_policy_hnd_object * ) self ;
2003-07-29 00:08:05 +00:00
TALLOC_CTX * mem_ctx = NULL ;
2002-04-14 09:00:13 +00:00
DOM_SID * sids ;
2002-05-06 04:53:44 +00:00
if ( ! PyArg_ParseTuple ( args , " O " , & py_sids ) )
return NULL ;
if ( ! PyList_Check ( py_sids ) & & ! PyString_Check ( py_sids ) ) {
PyErr_SetString ( PyExc_TypeError , " must be list or string " ) ;
2002-04-14 09:00:13 +00:00
return NULL ;
2002-05-06 04:53:44 +00:00
}
2003-08-04 00:50:00 +00:00
if ( ! ( mem_ctx = talloc_init ( " lsa_lookup_sids " ) ) ) {
2003-07-29 00:08:05 +00:00
PyErr_SetString ( lsa_error , " unable to init talloc context \n " ) ;
goto done ;
}
2002-05-06 04:53:44 +00:00
if ( PyList_Check ( py_sids ) ) {
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
/* Convert dictionary to char ** array */
num_sids = PyList_Size ( py_sids ) ;
2005-05-09 12:43:12 +00:00
sids = ( DOM_SID * ) _talloc ( mem_ctx , num_sids * sizeof ( DOM_SID ) ) ;
2002-05-06 04:53:44 +00:00
memset ( sids , 0 , num_sids * sizeof ( DOM_SID ) ) ;
for ( i = 0 ; i < num_sids ; i + + ) {
PyObject * obj = PyList_GetItem ( py_sids , i ) ;
2003-02-18 07:15:52 +00:00
if ( ! string_to_sid ( & sids [ i ] , PyString_AsString ( obj ) ) ) {
PyErr_SetString ( PyExc_ValueError , " string_to_sid failed " ) ;
2003-07-29 00:08:05 +00:00
goto done ;
2003-02-18 07:15:52 +00:00
}
2002-05-06 04:53:44 +00:00
}
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
} else {
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
/* Just a single element */
2002-04-14 09:00:13 +00:00
2002-05-06 04:53:44 +00:00
num_sids = 1 ;
2005-05-09 12:43:12 +00:00
sids = ( DOM_SID * ) _talloc ( mem_ctx , sizeof ( DOM_SID ) ) ;
2002-04-14 09:00:13 +00:00
2003-02-18 07:15:52 +00:00
if ( ! string_to_sid ( & sids [ 0 ] , PyString_AsString ( py_sids ) ) ) {
PyErr_SetString ( PyExc_ValueError , " string_to_sid failed " ) ;
2003-07-29 00:08:05 +00:00
goto done ;
2003-02-18 07:15:52 +00:00
}
2002-04-14 09:00:13 +00:00
}
2005-10-07 04:54:41 +00:00
ntstatus = rpccli_lsa_lookup_sids (
hnd - > cli , mem_ctx , & hnd - > pol , num_sids , sids , & domains ,
& names , & types ) ;
2002-04-14 09:00:13 +00:00
if ( ! NT_STATUS_IS_OK ( ntstatus ) ) {
PyErr_SetObject ( lsa_ntstatus , py_ntstatus_tuple ( ntstatus ) ) ;
2003-07-29 00:08:05 +00:00
goto done ;
2002-04-14 09:00:13 +00:00
}
result = PyList_New ( num_sids ) ;
for ( i = 0 ; i < num_sids ; i + + ) {
2002-04-14 12:26:09 +00:00
PyObject * obj ;
2002-04-14 09:00:13 +00:00
obj = Py_BuildValue ( " {sssssi} " , " username " , names [ i ] ,
" domain " , domains [ i ] , " name_type " ,
types [ i ] ) ;
PyList_SetItem ( result , i , obj ) ;
}
2003-07-29 00:08:05 +00:00
done :
2003-08-04 00:50:00 +00:00
talloc_destroy ( mem_ctx ) ;
2003-07-29 00:08:05 +00:00
2002-04-14 09:00:13 +00:00
return result ;
2002-03-20 03:29:03 +00:00
}
2002-04-14 12:26:09 +00:00
static PyObject * lsa_enum_trust_dom ( PyObject * self , PyObject * args )
{
lsa_policy_hnd_object * hnd = ( lsa_policy_hnd_object * ) self ;
NTSTATUS ntstatus ;
2002-08-06 01:07:07 +00:00
uint32 enum_ctx = 0 , num_domains , i ;
2002-04-14 12:26:09 +00:00
char * * domain_names ;
DOM_SID * domain_sids ;
PyObject * result ;
if ( ! PyArg_ParseTuple ( args , " " ) )
return NULL ;
2005-10-07 04:54:41 +00:00
ntstatus = rpccli_lsa_enum_trust_dom (
2002-05-27 06:34:13 +00:00
hnd - > cli , hnd - > mem_ctx , & hnd - > pol , & enum_ctx ,
2002-08-06 01:07:07 +00:00
& num_domains , & domain_names , & domain_sids ) ;
2002-04-14 12:26:09 +00:00
if ( ! NT_STATUS_IS_OK ( ntstatus ) ) {
PyErr_SetObject ( lsa_ntstatus , py_ntstatus_tuple ( ntstatus ) ) ;
return NULL ;
}
result = PyList_New ( num_domains ) ;
for ( i = 0 ; i < num_domains ; i + + ) {
fstring sid_str ;
sid_to_string ( sid_str , & domain_sids [ i ] ) ;
PyList_SetItem (
result , i ,
Py_BuildValue ( " (ss) " , domain_names [ i ] , sid_str ) ) ;
}
return result ;
}
2002-03-20 03:29:03 +00:00
/*
2002-04-14 09:00:13 +00:00
* Method dispatch tables
2002-03-20 03:29:03 +00:00
*/
2002-04-14 09:00:13 +00:00
static PyMethodDef lsa_hnd_methods [ ] = {
2002-04-14 12:26:09 +00:00
/* SIDs<->names */
2002-05-14 02:37:47 +00:00
{ " lookup_sids " , ( PyCFunction ) lsa_lookup_sids ,
METH_VARARGS | METH_KEYWORDS ,
2002-04-14 09:00:13 +00:00
" Convert sids to names. " } ,
2002-05-14 02:37:47 +00:00
{ " lookup_names " , ( PyCFunction ) lsa_lookup_names ,
METH_VARARGS | METH_KEYWORDS ,
2002-04-14 09:00:13 +00:00
" Convert names to sids. " } ,
2002-04-14 12:26:09 +00:00
/* Trusted domains */
2002-05-14 02:37:47 +00:00
{ " enum_trusted_domains " , ( PyCFunction ) lsa_enum_trust_dom ,
METH_VARARGS ,
2002-04-14 12:26:09 +00:00
" Enumerate trusted domains. " } ,
2002-04-14 09:00:13 +00:00
{ NULL }
} ;
static void py_lsa_policy_hnd_dealloc ( PyObject * self )
{
PyObject_Del ( self ) ;
}
static PyObject * py_lsa_policy_hnd_getattr ( PyObject * self , char * attrname )
{
return Py_FindMethod ( lsa_hnd_methods , self , attrname ) ;
}
PyTypeObject lsa_policy_hnd_type = {
PyObject_HEAD_INIT ( NULL )
0 ,
" LSA Policy Handle " ,
sizeof ( lsa_policy_hnd_object ) ,
0 ,
py_lsa_policy_hnd_dealloc , /*tp_dealloc*/
0 , /*tp_print*/
py_lsa_policy_hnd_getattr , /*tp_getattr*/
0 , /*tp_setattr*/
0 , /*tp_compare*/
0 , /*tp_repr*/
0 , /*tp_as_number*/
0 , /*tp_as_sequence*/
0 , /*tp_as_mapping*/
0 , /*tp_hash */
} ;
2002-03-20 03:29:03 +00:00
static PyMethodDef lsa_methods [ ] = {
/* Open/close lsa handles */
2002-05-14 02:37:47 +00:00
{ " open_policy " , ( PyCFunction ) lsa_open_policy ,
METH_VARARGS | METH_KEYWORDS ,
2002-03-20 03:29:03 +00:00
" Open a policy handle " } ,
2002-05-14 02:37:47 +00:00
{ " close " , ( PyCFunction ) lsa_close ,
METH_VARARGS ,
" Close a policy handle " } ,
2002-05-28 02:08:39 +00:00
/* Other stuff - this should really go into a samba config module
but for the moment let ' s leave it here . */
{ " setup_logging " , ( PyCFunction ) py_setup_logging ,
METH_VARARGS | METH_KEYWORDS ,
2003-03-20 01:05:22 +00:00
" Set up debug logging. \n "
" \n "
" Initialises Samba's debug logging system. One argument is expected which \n "
" is a boolean specifying whether debugging is interactive and sent to stdout \n "
" or logged to a file. \n "
" \n "
" Example: \n "
" \n "
2003-08-04 00:50:00 +00:00
" >>> lsa.setup_logging(interactive = 1) " } ,
2002-05-28 02:08:39 +00:00
{ " get_debuglevel " , ( PyCFunction ) get_debuglevel ,
METH_VARARGS ,
2003-03-20 01:05:22 +00:00
" Set the current debug level. \n "
" \n "
" Example: \n "
" \n "
2003-08-04 00:50:00 +00:00
" >>> lsa.get_debuglevel() \n "
2003-03-20 01:05:22 +00:00
" 0 " } ,
2002-05-28 02:08:39 +00:00
{ " set_debuglevel " , ( PyCFunction ) set_debuglevel ,
METH_VARARGS ,
2003-03-20 01:05:22 +00:00
" Get the current debug level. \n "
" \n "
" Example: \n "
" \n "
2003-08-04 00:50:00 +00:00
" >>> lsa.set_debuglevel(10) " } ,
2002-05-28 02:08:39 +00:00
2002-05-14 02:37:47 +00:00
{ NULL }
} ;
2002-03-20 03:29:03 +00:00
2002-05-14 02:37:47 +00:00
static struct const_vals {
char * name ;
uint32 value ;
} module_const_vals [ ] = {
2002-03-20 03:29:03 +00:00
{ NULL }
} ;
2002-05-14 02:37:47 +00:00
static void const_init ( PyObject * dict )
{
struct const_vals * tmp ;
PyObject * obj ;
for ( tmp = module_const_vals ; tmp - > name ; tmp + + ) {
obj = PyInt_FromLong ( tmp - > value ) ;
PyDict_SetItemString ( dict , tmp - > name , obj ) ;
Py_DECREF ( obj ) ;
}
}
2002-03-20 03:29:03 +00:00
/*
* Module initialisation
2002-05-14 02:37:47 +00:00
*/
2002-03-20 03:29:03 +00:00
void initlsa ( void )
{
PyObject * module , * dict ;
/* Initialise module */
module = Py_InitModule ( " lsa " , lsa_methods ) ;
dict = PyModule_GetDict ( module ) ;
lsa_error = PyErr_NewException ( " lsa.error " , NULL , NULL ) ;
PyDict_SetItemString ( dict , " error " , lsa_error ) ;
lsa_ntstatus = PyErr_NewException ( " lsa.ntstatus " , NULL , NULL ) ;
PyDict_SetItemString ( dict , " ntstatus " , lsa_ntstatus ) ;
/* Initialise policy handle object */
lsa_policy_hnd_type . ob_type = & PyType_Type ;
/* Initialise constants */
2002-05-14 02:37:47 +00:00
const_init ( dict ) ;
2002-03-20 03:29:03 +00:00
/* Do samba initialisation */
py_samba_init ( ) ;
2002-04-14 09:00:13 +00:00
setup_logging ( " lsa " , True ) ;
DEBUGLEVEL = 10 ;
2002-03-20 03:29:03 +00:00
}