2010-05-31 20:12:05 +04:00
/*
Unix SMB / CIFS implementation .
Python interface to ldb - utility functions .
Copyright ( C ) 2007 - 2010 Jelmer Vernooij < jelmer @ samba . org >
* * NOTE ! The following LGPL license applies to the ldb
* * library . This does NOT imply that all of Samba is released
* * under the LGPL
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation ; either
version 3 of the License , or ( at your option ) any later version .
This library 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
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public
License along with this library ; if not , see < http : //www.gnu.org/licenses/>.
*/
2010-12-12 23:40:03 +03:00
# include <Python.h>
# include "ldb.h"
2010-05-31 20:12:05 +04:00
# include "pyldb.h"
static PyObject * ldb_module = NULL ;
2010-11-15 08:41:50 +03:00
/**
* Find out PyTypeObject in ldb module for a given typename
*/
static PyTypeObject * PyLdb_GetPyType ( const char * typename )
{
2018-05-03 02:17:55 +03:00
PyTypeObject * type = NULL ;
bool ok ;
2010-11-15 08:41:50 +03:00
if ( ldb_module = = NULL ) {
ldb_module = PyImport_ImportModule ( " ldb " ) ;
if ( ldb_module = = NULL ) {
return NULL ;
}
}
2018-05-03 02:17:55 +03:00
type = ( PyTypeObject * ) PyObject_GetAttrString ( ldb_module , typename ) ;
2010-11-15 08:41:50 +03:00
2018-05-03 02:17:55 +03:00
if ( type = = NULL ) {
PyErr_Format ( PyExc_NameError ,
" Unable to find type %s in ldb module " ,
typename ) ;
return NULL ;
}
ok = PyType_Check ( type ) ;
if ( ! ok ) {
PyErr_Format ( PyExc_TypeError ,
" Expected type ldb.%s, not %s " ,
typename , Py_TYPE ( type ) - > tp_name ) ;
Py_DECREF ( type ) ;
return NULL ;
}
return type ;
2010-11-15 08:41:50 +03:00
}
2018-05-03 03:10:21 +03:00
bool pyldb_check_type ( PyObject * obj , const char * typename )
{
bool ok = false ;
PyTypeObject * type = PyLdb_GetPyType ( typename ) ;
if ( type ! = NULL ) {
ok = PyObject_TypeCheck ( obj , type ) ;
Py_DECREF ( type ) ;
}
return ok ;
}
2010-05-31 20:12:05 +04:00
/**
* Obtain a ldb DN from a Python object .
*
* @ param mem_ctx Memory context
* @ param object Python object
* @ param ldb_ctx LDB context
* @ return Whether or not the conversion succeeded
*/
2011-08-07 19:08:56 +04:00
bool pyldb_Object_AsDn ( TALLOC_CTX * mem_ctx , PyObject * object ,
2010-05-31 20:12:05 +04:00
struct ldb_context * ldb_ctx , struct ldb_dn * * dn )
{
struct ldb_dn * odn ;
2010-11-15 08:41:50 +03:00
PyTypeObject * PyLdb_Dn_Type ;
2010-05-31 20:12:05 +04:00
2019-06-15 14:14:49 +03:00
if ( ldb_ctx ! = NULL & & ( PyUnicode_Check ( object ) ) ) {
2019-06-07 12:21:15 +03:00
odn = ldb_dn_new ( mem_ctx , ldb_ctx , PyUnicode_AsUTF8 ( object ) ) ;
2015-06-09 18:44:40 +03:00
* dn = odn ;
return true ;
}
if ( ldb_ctx ! = NULL & & PyBytes_Check ( object ) ) {
odn = ldb_dn_new ( mem_ctx , ldb_ctx , PyBytes_AsString ( object ) ) ;
2010-05-31 20:12:05 +04:00
* dn = odn ;
return true ;
}
2010-11-15 08:41:50 +03:00
PyLdb_Dn_Type = PyLdb_GetPyType ( " Dn " ) ;
if ( PyLdb_Dn_Type = = NULL ) {
2010-05-31 20:12:05 +04:00
return false ;
2010-11-15 08:41:50 +03:00
}
2010-05-31 20:12:05 +04:00
2010-11-15 08:41:50 +03:00
if ( PyObject_TypeCheck ( object , PyLdb_Dn_Type ) ) {
2018-04-27 04:15:01 +03:00
* dn = pyldb_Dn_AS_DN ( object ) ;
2010-05-31 20:12:05 +04:00
return true ;
}
PyErr_SetString ( PyExc_TypeError , " Expected DN " ) ;
return false ;
}
2010-09-09 11:59:40 +04:00
2011-08-07 19:08:56 +04:00
PyObject * pyldb_Dn_FromDn ( struct ldb_dn * dn )
2010-09-09 11:59:40 +04:00
{
PyLdbDnObject * py_ret ;
PyTypeObject * PyLdb_Dn_Type ;
if ( dn = = NULL ) {
Py_RETURN_NONE ;
}
2010-11-15 08:41:50 +03:00
PyLdb_Dn_Type = PyLdb_GetPyType ( " Dn " ) ;
if ( PyLdb_Dn_Type = = NULL ) {
2010-09-09 11:59:40 +04:00
return NULL ;
2010-11-15 08:41:50 +03:00
}
2010-09-09 11:59:40 +04:00
py_ret = ( PyLdbDnObject * ) PyLdb_Dn_Type - > tp_alloc ( PyLdb_Dn_Type , 0 ) ;
if ( py_ret = = NULL ) {
PyErr_NoMemory ( ) ;
return NULL ;
}
py_ret - > mem_ctx = talloc_new ( NULL ) ;
py_ret - > dn = talloc_reference ( py_ret - > mem_ctx , dn ) ;
return ( PyObject * ) py_ret ;
}