2 Unix SMB/CIFS implementation.
4 Python interface to ldb.
6 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
8 ** NOTE! The following LGPL license applies to the ldb
9 ** library. This does NOT imply that all of Samba is released
12 This library is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation; either
15 version 3 of the License, or (at your option) any later version.
17 This library is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 Lesser General Public License for more details.
22 You should have received a copy of the GNU Lesser General Public
23 License along with this library; if not, see <http://www.gnu.org/licenses/>.
30 #include "ldb_private.h"
31 #include "lib/replace/system/python.h"
36 struct ldb_context
*ldb_ctx
;
39 /* pyldb_Ldb_AS_LDBCONTEXT() does not check argument validity,
40 pyldb_Ldb_AsLdbContext() does */
41 #define pyldb_Ldb_AS_LDBCONTEXT(pyobj) ((PyLdbObject *)pyobj)->ldb_ctx
43 #define pyldb_Ldb_AsLdbContext(pyobj) \
44 (pyldb_check_type(pyobj, "Ldb") ? \
45 pyldb_Ldb_AS_LDBCONTEXT(pyobj) : NULL)
47 #define PyErr_LDB_OR_RAISE(py_ldb, ldb) \
48 ldb = pyldb_Ldb_AsLdbContext(py_ldb); \
50 PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
55 * A note about PyLdbObject back-references and struct member ordering.
57 * PyLdbDnObject, PyLdbMessageObject, and PyLdbResultObject all
58 * contain pointers to the PyLdbObject that was used to create them.
59 * This is used to check that the ldb pointer in the underlying ldb
60 * struct is still valid -- that is, it points to the same ldb as the
63 * We keep these pointers in third place in the structs, like this
66 * TALLOC_CTX *mem_ctx;
71 * so that, if we feel like it in future, we can do type-punning in
72 * the way Python does. For example:
76 * TALLOC_CTX *mem_ctx;
80 * #define pyldb_child_get_pyldb(pyobj) ((PyLdbChildObject *)pyobj)->pyldb
82 * #define PY_LDB_OWNS_THIS_CHILD(pyldb, child) \
83 * (((PyLdbObject *)ldb) == ((PyLdbChildObject *)child)->ldb)
85 * At present we don't do this, because there are not a whole lot of
86 * cases where we want to do the same things with dNs, messages, and
94 * We use this to keep a reference to the ldb context within
95 * the struct ldb_dn and to know if it is still valid
101 PyObject
*pyldb_Dn_FromDn(struct ldb_dn
*dn
, PyLdbObject
*pyldb
);
102 bool pyldb_Object_AsDn(TALLOC_CTX
*mem_ctx
, PyObject
*object
, struct ldb_context
*ldb_ctx
, struct ldb_dn
**dn
);
103 #define pyldb_Dn_AS_DN(pyobj) ((PyLdbDnObject *)pyobj)->dn
107 * PyErr_LDB_DN_OR_RAISE does 3 things:
108 * 1. checks that a PyObject is really a PyLdbDnObject.
109 * 2. checks that the ldb that the PyLdbDnObject knows is the ldb that its dn
111 * 3. sets the (struct ldb_dn *) dn argument to the dn the pyobject refers to.
113 * why so much? because we almost always need it.
115 #define PyErr_LDB_DN_OR_RAISE(_py_obj, dn) do { \
116 PyLdbDnObject *_py_dn = NULL; \
117 if (!pyldb_check_type(_py_obj, "Dn")) { \
118 PyErr_SetString(PyExc_TypeError, "ldb Dn object required"); \
121 _py_dn = (PyLdbDnObject *)_py_obj; \
122 dn = pyldb_Dn_AS_DN(_py_dn); \
123 if (_py_dn->pyldb->ldb_ctx != ldb_dn_get_ldb_context(dn)) { \
124 PyErr_SetString(PyExc_RuntimeError, \
125 "Dn has a stale LDB connection"); \
131 bool pyldb_check_type(PyObject
*obj
, const char *type_name
);
137 * We use this to keep a reference to the ldb context within
138 * the struct ldb_dn (under struct ldb_message) and to know if
142 struct ldb_message
*msg
;
143 } PyLdbMessageObject
;
145 #define pyldb_Message_AsMessage(pyobj) ((PyLdbMessageObject *)pyobj)->msg
148 * NOTE: el (and so the return value of
149 * pyldb_MessageElement_AsMessageElement()) may not be a valid talloc
150 * context, it could be part of an array
156 struct ldb_message_element
*el
;
157 } PyLdbMessageElementObject
;
159 #define pyldb_MessageElement_AsMessageElement(pyobj) ((PyLdbMessageElementObject *)pyobj)->el
164 struct ldb_parse_tree
*tree
;
166 #define pyldb_Tree_AsTree(pyobj) ((PyLdbTreeObject *)pyobj)->tree
180 struct ldb_control
*data
;
181 } PyLdbControlObject
;
183 void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
);
185 #define PyErr_LDB_ERROR_IS_ERR_RAISE(err,ret,ldb) do { \
186 if (ret != LDB_SUCCESS) { \
187 PyErr_SetLdbError(err, ret, ldb); \
192 #define PyErr_LDB_ERROR_IS_ERR_RAISE_FREE(err,ret,ldb,mem_ctx) do { \
193 if (ret != LDB_SUCCESS) { \
194 PyErr_SetLdbError(err, ret, ldb); \
195 TALLOC_FREE(mem_ctx); \
200 /* Picked out of thin air. To do this properly, we should probably have some part of the
201 * errors in LDB be allocated to bindings ? */
202 #define LDB_ERR_PYTHON_EXCEPTION 142
204 #endif /* _PYLDB_H_ */