libndr: Add overflow check to ndr_push_subcontext_end()
[samba4-gss.git] / lib / ldb / pyldb.h
blob27d5fe3b4411311fe6f2b894320100c71aa32277
1 /*
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
10 ** under the LGPL
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/>.
26 #ifndef _PYLDB_H_
27 #define _PYLDB_H_
29 #include <talloc.h>
30 #include "ldb_private.h"
31 #include "lib/replace/system/python.h"
33 typedef struct {
34 PyObject_HEAD
35 TALLOC_CTX *mem_ctx;
36 struct ldb_context *ldb_ctx;
37 } PyLdbObject;
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); \
49 if (!ldb) { \
50 PyErr_SetString(PyExc_TypeError, "Ldb connection object required"); \
51 return NULL; \
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
61 * PyLdbObject.
63 * We keep these pointers in third place in the structs, like this
64 * {
65 * PyObject_HEAD
66 * TALLOC_CTX *mem_ctx;
67 * PyLdbObject *pyldb;
68 * ...
69 * }
71 * so that, if we feel like it in future, we can do type-punning in
72 * the way Python does. For example:
74 * typedef struct {
75 * PyObject_HEAD
76 * TALLOC_CTX *mem_ctx;
77 * PyLdbObject *pyldb;
78 * } PyLdbChildObject;
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
87 * results.
90 typedef struct {
91 PyObject_HEAD
92 TALLOC_CTX *mem_ctx;
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
97 PyLdbObject *pyldb;
98 struct ldb_dn *dn;
99 } PyLdbDnObject;
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
110 * knows.
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"); \
119 return NULL; \
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"); \
126 return NULL; \
128 } while(0)
131 bool pyldb_check_type(PyObject *obj, const char *type_name);
133 typedef struct {
134 PyObject_HEAD
135 TALLOC_CTX *mem_ctx;
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
139 * it is still valid
141 PyLdbObject *pyldb;
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
153 typedef struct {
154 PyObject_HEAD
155 TALLOC_CTX *mem_ctx;
156 struct ldb_message_element *el;
157 } PyLdbMessageElementObject;
159 #define pyldb_MessageElement_AsMessageElement(pyobj) ((PyLdbMessageElementObject *)pyobj)->el
161 typedef struct {
162 PyObject_HEAD
163 TALLOC_CTX *mem_ctx;
164 struct ldb_parse_tree *tree;
165 } PyLdbTreeObject;
166 #define pyldb_Tree_AsTree(pyobj) ((PyLdbTreeObject *)pyobj)->tree
168 typedef struct {
169 PyObject_HEAD
170 TALLOC_CTX *mem_ctx;
171 PyLdbObject *pyldb;
172 PyObject *msgs;
173 PyObject *referals;
174 PyObject *controls;
175 } PyLdbResultObject;
177 typedef struct {
178 PyObject_HEAD
179 TALLOC_CTX *mem_ctx;
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); \
188 return NULL; \
190 } while(0)
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); \
196 return NULL; \
198 } while(0)
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_ */