/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- PyErr_SetLdbError
- PyObject_FromLdbValue
- PyObject_AsDn
- PyLdbResult_FromResult
- PyLdbResult_AsResult
- py_ldb_dn_validate
- py_ldb_dn_is_valid
- py_ldb_dn_is_special
- py_ldb_dn_is_null
- py_ldb_dn_get_casefold
- py_ldb_dn_get_linearized
- py_ldb_dn_canonical_str
- py_ldb_dn_canonical_ex_str
- py_ldb_dn_repr
- py_ldb_dn_check_special
- py_ldb_dn_compare
- py_ldb_dn_get_parent
- py_ldb_dn_add_child
- py_ldb_dn_add_base
- py_ldb_dn_len
- py_ldb_dn_concat
- py_ldb_dn_new
- PyLdbDn_FromDn
- py_ldb_dn_dealloc
- py_ldb_debug
- py_ldb_set_debug
- py_ldb_set_create_perms
- py_ldb_set_modules_dir
- py_ldb_transaction_start
- py_ldb_transaction_commit
- py_ldb_transaction_cancel
- py_ldb_setup_wellknown_attributes
- py_ldb_repr
- py_ldb_get_root_basedn
- py_ldb_get_schema_basedn
- py_ldb_get_config_basedn
- py_ldb_get_default_basedn
- PyList_AsStringList
- py_ldb_init
- py_ldb_new
- py_ldb_connect
- py_ldb_modify
- py_ldb_add
- py_ldb_delete
- py_ldb_rename
- py_ldb_schema_attribute_remove
- py_ldb_schema_attribute_add
- ldb_ldif_to_pyobject
- py_ldb_parse_ldif
- py_ldb_schema_format_value
- py_ldb_search
- py_ldb_get_opaque
- py_ldb_set_opaque
- py_ldb_modules
- PyLdbModule_FromModule
- py_ldb_get_firstmodule
- py_ldb_contains
- PyLdb_FromLdbContext
- py_ldb_dealloc
- py_ldb_module_repr
- py_ldb_module_str
- py_ldb_module_start_transaction
- py_ldb_module_end_transaction
- py_ldb_module_del_transaction
- py_ldb_module_search
- py_ldb_module_add
- py_ldb_module_modify
- py_ldb_module_delete
- py_ldb_module_rename
- py_ldb_module_dealloc
- PyObject_AsMessageElement
- ldb_msg_element_to_set
- py_ldb_msg_element_get
- py_ldb_msg_element_len
- py_ldb_msg_element_find
- py_ldb_msg_element_cmp
- py_ldb_msg_element_iter
- PyLdbMessageElement_FromMessageElement
- py_ldb_msg_element_new
- py_ldb_msg_element_repr
- py_ldb_msg_element_str
- py_ldb_msg_element_dealloc
- py_ldb_msg_remove_attr
- py_ldb_msg_keys
- py_ldb_msg_getitem_helper
- py_ldb_msg_getitem
- py_ldb_msg_get
- py_ldb_msg_items
- py_ldb_msg_iter
- py_ldb_msg_setitem
- py_ldb_msg_length
- py_ldb_msg_new
- PyLdbMessage_FromMessage
- py_ldb_msg_get_dn
- py_ldb_msg_set_dn
- py_ldb_msg_repr
- py_ldb_msg_dealloc
- PyLdbTree_FromTree
- py_ldb_tree_dealloc
- py_module_search
- py_module_add
- py_module_modify
- py_module_del
- py_module_rename
- py_module_request
- py_module_extended
- py_module_start_transaction
- py_module_end_transaction
- py_module_del_transaction
- py_module_destructor
- py_module_init
- py_register_module
- py_timestring
- py_string_to_time
- py_valid_attr_name
- initldb
1 /*
2 Unix SMB/CIFS implementation.
3
4 Swig interface to ldb.
5
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2008 Jelmer Vernooij <jelmer@samba.org>
9
10 ** NOTE! The following LGPL license applies to the ldb
11 ** library. This does NOT imply that all of Samba is released
12 ** under the LGPL
13
14 This library is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Lesser General Public
16 License as published by the Free Software Foundation; either
17 version 3 of the License, or (at your option) any later version.
18
19 This library is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 Lesser General Public License for more details.
23
24 You should have received a copy of the GNU Lesser General Public
25 License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 */
27
28 #include "replace.h"
29 #include "ldb_private.h"
30 #include <Python.h>
31 #include "pyldb.h"
32
33 /* There's no Py_ssize_t in 2.4, apparently */
34 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
35 typedef int Py_ssize_t;
36 typedef inquiry lenfunc;
37 typedef intargfunc ssizeargfunc;
38 #endif
39
40 #ifndef Py_RETURN_NONE
41 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
42 #endif
43
44 /* Picked out of thin air. To do this properly, we should probably have some part of the
45 * errors in LDB be allocated to bindings ? */
46 #define LDB_ERR_PYTHON_EXCEPTION 142
47
48 static PyObject *PyExc_LdbError;
49
50 void PyErr_SetLdbError(int ret, struct ldb_context *ldb_ctx)
/* [<][>][^][v][top][bottom][index][help] */
51 {
52 if (ret == LDB_ERR_PYTHON_EXCEPTION)
53 return; /* Python exception should already be set, just keep that */
54 PyErr_SetObject(PyExc_LdbError, Py_BuildValue(discard_const_p(char, "(i,s)"),
55 ret, ldb_ctx == NULL?ldb_strerror(ret):ldb_errstring(ldb_ctx)));
56 }
57
58 static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx,
/* [<][>][^][v][top][bottom][index][help] */
59 struct ldb_message_element *el,
60 struct ldb_val *val)
61 {
62 const struct ldb_schema_attribute *a;
63 struct ldb_val new_val;
64 TALLOC_CTX *mem_ctx = talloc_new(NULL);
65 PyObject *ret;
66
67 new_val = *val;
68
69 if (ldb_ctx != NULL) {
70 a = ldb_schema_attribute_by_name(ldb_ctx, el->name);
71
72 if (a != NULL) {
73 if (a->syntax->ldif_write_fn(ldb_ctx, mem_ctx, val, &new_val) != 0) {
74 talloc_free(mem_ctx);
75 return NULL;
76 }
77 }
78 }
79
80 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
81
82 talloc_free(mem_ctx);
83
84 return ret;
85 }
86
87 bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object,
/* [<][>][^][v][top][bottom][index][help] */
88 struct ldb_context *ldb_ctx, struct ldb_dn **dn)
89 {
90 struct ldb_dn *odn;
91
92 if (ldb_ctx != NULL && PyString_Check(object)) {
93 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object));
94 *dn = odn;
95 return true;
96 }
97
98 if (PyLdbDn_Check(object)) {
99 *dn = PyLdbDn_AsDn(object);
100 return true;
101 }
102
103 PyErr_SetString(PyExc_TypeError, "Expected DN");
104 return false;
105 }
106
107 static PyObject *PyLdbResult_FromResult(struct ldb_result *result)
/* [<][>][^][v][top][bottom][index][help] */
108 {
109 PyObject *ret;
110 int i;
111 if (result == NULL) {
112 Py_RETURN_NONE;
113 }
114 ret = PyList_New(result->count);
115 for (i = 0; i < result->count; i++) {
116 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i])
117 );
118 }
119 return ret;
120 }
121
122 static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, PyObject *obj)
/* [<][>][^][v][top][bottom][index][help] */
123 {
124 struct ldb_result *res;
125 int i;
126
127 if (obj == Py_None)
128 return NULL;
129
130 res = talloc_zero(mem_ctx, struct ldb_result);
131 res->count = PyList_Size(obj);
132 res->msgs = talloc_array(res, struct ldb_message *, res->count);
133 for (i = 0; i < res->count; i++) {
134 PyObject *item = PyList_GetItem(obj, i);
135 res->msgs[i] = PyLdbMessage_AsMessage(item);
136 }
137 return res;
138 }
139
140 static PyObject *py_ldb_dn_validate(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
141 {
142 return PyBool_FromLong(ldb_dn_validate(self->dn));
143 }
144
145 static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
146 {
147 return PyBool_FromLong(ldb_dn_is_valid(self->dn));
148 }
149
150 static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
151 {
152 return PyBool_FromLong(ldb_dn_is_special(self->dn));
153 }
154
155 static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
156 {
157 return PyBool_FromLong(ldb_dn_is_null(self->dn));
158 }
159
160 static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
161 {
162 return PyString_FromString(ldb_dn_get_casefold(self->dn));
163 }
164
165 static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
166 {
167 return PyString_FromString(ldb_dn_get_linearized(self->dn));
168 }
169
170 static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
171 {
172 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn));
173 }
174
175 static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
176 {
177 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn));
178 }
179
180 static PyObject *py_ldb_dn_repr(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
181 {
182 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn))));
183 }
184
185 static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
186 {
187 char *name;
188
189 if (!PyArg_ParseTuple(args, "s", &name))
190 return NULL;
191
192 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False;
193 }
194
195 static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2)
/* [<][>][^][v][top][bottom][index][help] */
196 {
197 return ldb_dn_compare(dn1->dn, dn2->dn);
198 }
199
200 static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
201 {
202 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self);
203 return PyLdbDn_FromDn(ldb_dn_get_parent(NULL, dn));
204 }
205
206 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
207
208 static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
209 {
210 PyObject *py_other;
211 struct ldb_dn *dn, *other;
212 if (!PyArg_ParseTuple(args, "O", &py_other))
213 return NULL;
214
215 dn = PyLdbDn_AsDn((PyObject *)self);
216
217 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
218 return NULL;
219
220 return ldb_dn_add_child(dn, other)?Py_True:Py_False;
221 }
222
223 static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
224 {
225 PyObject *py_other;
226 struct ldb_dn *other, *dn;
227 if (!PyArg_ParseTuple(args, "O", &py_other))
228 return NULL;
229
230 dn = PyLdbDn_AsDn((PyObject *)self);
231
232 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other))
233 return NULL;
234
235 return ldb_dn_add_base(dn, other)?Py_True:Py_False;
236 }
237
238 static PyMethodDef py_ldb_dn_methods[] = {
239 { "validate", (PyCFunction)py_ldb_dn_validate, METH_NOARGS,
240 "S.validate() -> bool\n"
241 "Validate DN is correct." },
242 { "is_valid", (PyCFunction)py_ldb_dn_is_valid, METH_NOARGS,
243 "S.is_valid() -> bool\n" },
244 { "is_special", (PyCFunction)py_ldb_dn_is_special, METH_NOARGS,
245 "S.is_special() -> bool\n"
246 "Check whether this is a special LDB DN." },
247 { "is_null", (PyCFunction)py_ldb_dn_is_null, METH_NOARGS,
248 "Check whether this is a null DN." },
249 { "get_casefold", (PyCFunction)py_ldb_dn_get_casefold, METH_NOARGS,
250 NULL },
251 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS,
252 NULL },
253 { "canonical_str", (PyCFunction)py_ldb_dn_canonical_str, METH_NOARGS,
254 "S.canonical_str() -> string\n"
255 "Canonical version of this DN (like a posix path)." },
256 { "canonical_ex_str", (PyCFunction)py_ldb_dn_canonical_ex_str, METH_NOARGS,
257 "S.canonical_ex_str() -> string\n"
258 "Canonical version of this DN (like a posix path, with terminating newline)." },
259 { "check_special", (PyCFunction)py_ldb_dn_is_special, METH_VARARGS,
260 NULL },
261 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS,
262 "S.parent() -> dn\n"
263 "Get the parent for this DN." },
264 { "add_child", (PyCFunction)py_ldb_dn_add_child, METH_VARARGS,
265 "S.add_child(dn) -> None\n"
266 "Add a child DN to this DN." },
267 { "add_base", (PyCFunction)py_ldb_dn_add_base, METH_VARARGS,
268 "S.add_base(dn) -> None\n"
269 "Add a base DN to this DN." },
270 { "check_special", (PyCFunction)py_ldb_dn_check_special, METH_VARARGS,
271 NULL },
272 { NULL }
273 };
274
275 static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
276 {
277 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self));
278 }
279
280 static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other)
/* [<][>][^][v][top][bottom][index][help] */
281 {
282 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self),
283 *other;
284 struct ldb_dn *ret = ldb_dn_copy(NULL, dn);
285 if (!PyObject_AsDn(NULL, py_other, NULL, &other))
286 return NULL;
287 ldb_dn_add_child(ret, other);
288 return PyLdbDn_FromDn(ret);
289 }
290
291 static PySequenceMethods py_ldb_dn_seq = {
292 .sq_length = (lenfunc)py_ldb_dn_len,
293 .sq_concat = (binaryfunc)py_ldb_dn_concat,
294 };
295
296 static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
297 {
298 struct ldb_dn *ret;
299 char *str;
300 PyObject *py_ldb;
301 struct ldb_context *ldb_ctx;
302 PyLdbDnObject *py_ret;
303 const char * const kwnames[] = { "ldb", "dn", NULL };
304
305 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os",
306 discard_const_p(char *, kwnames),
307 &py_ldb, &str))
308 return NULL;
309
310 ldb_ctx = PyLdb_AsLdbContext(py_ldb);
311
312 ret = ldb_dn_new(ldb_ctx, ldb_ctx, str);
313 /* ldb_dn_new() doesn't accept NULL as memory context, so
314 we do it this way... */
315 talloc_steal(NULL, ret);
316
317 if (ret == NULL || !ldb_dn_validate(ret)) {
318 PyErr_SetString(PyExc_ValueError, "unable to parse dn string");
319 return NULL;
320 }
321
322 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0);
323 if (ret == NULL) {
324 PyErr_NoMemory();
325 return NULL;
326 }
327 py_ret->dn = ret;
328 return (PyObject *)py_ret;
329 }
330
331 PyObject *PyLdbDn_FromDn(struct ldb_dn *dn)
/* [<][>][^][v][top][bottom][index][help] */
332 {
333 PyLdbDnObject *py_ret;
334 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0);
335 if (py_ret == NULL) {
336 PyErr_NoMemory();
337 return NULL;
338 }
339 py_ret->mem_ctx = talloc_new(NULL);
340 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn);
341 return (PyObject *)py_ret;
342 }
343
344 static void py_ldb_dn_dealloc(PyLdbDnObject *self)
/* [<][>][^][v][top][bottom][index][help] */
345 {
346 talloc_free(self->mem_ctx);
347 self->ob_type->tp_free(self);
348 }
349
350 PyTypeObject PyLdbDn = {
351 .tp_name = "Dn",
352 .tp_methods = py_ldb_dn_methods,
353 .tp_str = (reprfunc)py_ldb_dn_get_linearized,
354 .tp_repr = (reprfunc)py_ldb_dn_repr,
355 .tp_compare = (cmpfunc)py_ldb_dn_compare,
356 .tp_as_sequence = &py_ldb_dn_seq,
357 .tp_doc = "A LDB distinguished name.",
358 .tp_new = py_ldb_dn_new,
359 .tp_dealloc = (destructor)py_ldb_dn_dealloc,
360 .tp_basicsize = sizeof(PyLdbObject),
361 .tp_flags = Py_TPFLAGS_DEFAULT,
362 };
363
364 /* Debug */
365 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0);
/* [<][>][^][v][top][bottom][index][help] */
366 static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap)
367 {
368 PyObject *fn = (PyObject *)context;
369 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap));
370 }
371
372 static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
373 {
374 PyObject *cb;
375
376 if (!PyArg_ParseTuple(args, "O", &cb))
377 return NULL;
378
379 Py_INCREF(cb);
380 /* FIXME: Where do we DECREF cb ? */
381 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_set_debug(self->ldb_ctx, py_ldb_debug, cb), PyLdb_AsLdbContext(self));
382
383 Py_RETURN_NONE;
384 }
385
386 static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
387 {
388 unsigned int perms;
389 if (!PyArg_ParseTuple(args, "I", &perms))
390 return NULL;
391
392 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms);
393
394 Py_RETURN_NONE;
395 }
396
397 static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
398 {
399 char *modules_dir;
400 if (!PyArg_ParseTuple(args, "s", &modules_dir))
401 return NULL;
402
403 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir);
404
405 Py_RETURN_NONE;
406 }
407
408 static PyObject *py_ldb_transaction_start(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
409 {
410 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
411 Py_RETURN_NONE;
412 }
413
414 static PyObject *py_ldb_transaction_commit(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
415 {
416 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
417 Py_RETURN_NONE;
418 }
419
420 static PyObject *py_ldb_transaction_cancel(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
421 {
422 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
423 Py_RETURN_NONE;
424 }
425
426 static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
427 {
428 PyErr_LDB_ERROR_IS_ERR_RAISE(ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self));
429 Py_RETURN_NONE;
430 }
431
432 static PyObject *py_ldb_repr(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
433 {
434 return PyString_FromFormat("<ldb connection>");
435 }
436
437 static PyObject *py_ldb_get_root_basedn(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
438 {
439 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self));
440 if (dn == NULL)
441 Py_RETURN_NONE;
442 return PyLdbDn_FromDn(dn);
443 }
444
445
446 static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
447 {
448 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self));
449 if (dn == NULL)
450 Py_RETURN_NONE;
451 return PyLdbDn_FromDn(dn);
452 }
453
454
455 static PyObject *py_ldb_get_config_basedn(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
456 {
457 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self));
458 if (dn == NULL)
459 Py_RETURN_NONE;
460 return PyLdbDn_FromDn(dn);
461 }
462
463
464 static PyObject *py_ldb_get_default_basedn(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
465 {
466 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self));
467 if (dn == NULL)
468 Py_RETURN_NONE;
469 return PyLdbDn_FromDn(dn);
470 }
471
472 static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list)
/* [<][>][^][v][top][bottom][index][help] */
473 {
474 const char **ret;
475 int i;
476 if (!PyList_Check(list)) {
477 PyErr_SetString(PyExc_TypeError, "options is not a list");
478 return NULL;
479 }
480 ret = talloc_array(NULL, const char *, PyList_Size(list)+1);
481 for (i = 0; i < PyList_Size(list); i++) {
482 PyObject *item = PyList_GetItem(list, i);
483 if (!PyString_Check(item)) {
484 PyErr_SetString(PyExc_TypeError, "options should be strings");
485 return NULL;
486 }
487 ret[i] = PyString_AsString(item);
488 }
489 ret[i] = NULL;
490 return ret;
491 }
492
493 static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
494 {
495 const char * const kwnames[] = { "url", "flags", "options", NULL };
496 char *url = NULL;
497 PyObject *py_options = Py_None;
498 const char **options;
499 int flags = 0;
500 int ret;
501 struct ldb_context *ldb;
502
503 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__",
504 discard_const_p(char *, kwnames),
505 &url, &flags, &py_options))
506 return -1;
507
508 ldb = PyLdb_AsLdbContext(self);
509
510 if (py_options == Py_None) {
511 options = NULL;
512 } else {
513 options = PyList_AsStringList(ldb, py_options);
514 if (options == NULL)
515 return -1;
516 }
517
518 if (url != NULL) {
519 ret = ldb_connect(ldb, url, flags, options);
520 if (ret != LDB_SUCCESS) {
521 PyErr_SetLdbError(ret, ldb);
522 return -1;
523 }
524 }
525
526 talloc_free(options);
527 return 0;
528 }
529
530 static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
531 {
532 PyLdbObject *ret;
533 struct ldb_context *ldb;
534 ldb = ldb_init(NULL, NULL);
535 if (ldb == NULL) {
536 PyErr_NoMemory();
537 return NULL;
538 }
539
540 ret = (PyLdbObject *)type->tp_alloc(type, 0);
541 if (ret == NULL) {
542 PyErr_NoMemory();
543 return NULL;
544 }
545 ret->ldb_ctx = ldb;
546 return (PyObject *)ret;
547 }
548
549 static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
550 {
551 char *url;
552 int flags = 0;
553 PyObject *py_options = Py_None;
554 int ret;
555 const char **options;
556 const char * const kwnames[] = { "url", "flags", "options", NULL };
557
558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|iO",
559 discard_const_p(char *, kwnames),
560 &url, &flags, &py_options))
561 return NULL;
562
563 if (py_options == Py_None) {
564 options = NULL;
565 } else {
566 options = PyList_AsStringList(NULL, py_options);
567 if (options == NULL)
568 return NULL;
569 }
570
571 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options);
572 talloc_free(options);
573
574 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
575
576 Py_RETURN_NONE;
577 }
578
579 static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
580 {
581 PyObject *py_msg;
582 int ret;
583 if (!PyArg_ParseTuple(args, "O", &py_msg))
584 return NULL;
585
586 if (!PyLdbMessage_Check(py_msg)) {
587 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message");
588 return NULL;
589 }
590
591 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg));
592 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
593
594 Py_RETURN_NONE;
595 }
596
597 static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
598 {
599 PyObject *py_msg;
600 int ret;
601 Py_ssize_t dict_pos, msg_pos;
602 struct ldb_message_element *msgel;
603 struct ldb_message *msg;
604 PyObject *key, *value;
605
606 if (!PyArg_ParseTuple(args, "O", &py_msg))
607 return NULL;
608
609 if (PyDict_Check(py_msg)) {
610 PyObject *dn_value = PyDict_GetItemString(py_msg, "dn");
611 msg = ldb_msg_new(NULL);
612 msg->elements = talloc_zero_array(msg, struct ldb_message_element, PyDict_Size(py_msg));
613 msg_pos = dict_pos = 0;
614 if (dn_value) {
615 if (!PyObject_AsDn(msg, dn_value, PyLdb_AsLdbContext(self), &msg->dn)) {
616 PyErr_SetString(PyExc_TypeError, "unable to import dn object");
617 return NULL;
618 }
619 if (msg->dn == NULL) {
620 PyErr_SetString(PyExc_TypeError, "dn set but not found");
621 return NULL;
622 }
623 }
624
625 while (PyDict_Next(py_msg, &dict_pos, &key, &value)) {
626 char *key_str = PyString_AsString(key);
627 if (strcmp(key_str, "dn") != 0) {
628 msgel = PyObject_AsMessageElement(msg->elements, value, 0, key_str);
629 if (msgel == NULL) {
630 PyErr_SetString(PyExc_TypeError, "unable to import element");
631 return NULL;
632 }
633 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel));
634 msg_pos++;
635 }
636 }
637
638 if (msg->dn == NULL) {
639 PyErr_SetString(PyExc_TypeError, "no dn set");
640 return NULL;
641 }
642
643 msg->num_elements = msg_pos;
644 } else {
645 msg = PyLdbMessage_AsMessage(py_msg);
646 }
647
648 ret = ldb_add(PyLdb_AsLdbContext(self), msg);
649 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
650
651 Py_RETURN_NONE;
652 }
653
654
655
656 static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
657 {
658 PyObject *py_dn;
659 struct ldb_dn *dn;
660 int ret;
661 struct ldb_context *ldb;
662 if (!PyArg_ParseTuple(args, "O", &py_dn))
663 return NULL;
664
665 ldb = PyLdb_AsLdbContext(self);
666
667 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn))
668 return NULL;
669
670 ret = ldb_delete(ldb, dn);
671 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
672
673 Py_RETURN_NONE;
674 }
675
676 static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
677 {
678 PyObject *py_dn1, *py_dn2;
679 struct ldb_dn *dn1, *dn2;
680 int ret;
681 struct ldb_context *ldb;
682 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
683 return NULL;
684
685 ldb = PyLdb_AsLdbContext(self);
686 if (!PyObject_AsDn(NULL, py_dn1, ldb, &dn1))
687 return NULL;
688
689 if (!PyObject_AsDn(NULL, py_dn2, ldb, &dn2))
690 return NULL;
691
692 ret = ldb_rename(ldb, dn1, dn2);
693 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb);
694
695 Py_RETURN_NONE;
696 }
697
698 static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
699 {
700 char *name;
701 if (!PyArg_ParseTuple(args, "s", &name))
702 return NULL;
703
704 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name);
705
706 Py_RETURN_NONE;
707 }
708
709 static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
710 {
711 char *attribute, *syntax;
712 unsigned int flags;
713 int ret;
714 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax))
715 return NULL;
716
717 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax);
718
719 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, PyLdb_AsLdbContext(self));
720
721 Py_RETURN_NONE;
722 }
723
724 static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif)
/* [<][>][^][v][top][bottom][index][help] */
725 {
726 if (ldif == NULL) {
727 Py_RETURN_NONE;
728 } else {
729 /* We don't want this attached to the 'ldb' any more */
730 talloc_steal(NULL, ldif);
731 return Py_BuildValue(discard_const_p(char, "(iO)"),
732 ldif->changetype,
733 PyLdbMessage_FromMessage(ldif->msg));
734 }
735 }
736
737
738 static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
739 {
740 PyObject *list;
741 struct ldb_ldif *ldif;
742 const char *s;
743
744 if (!PyArg_ParseTuple(args, "s", &s))
745 return NULL;
746
747 list = PyList_New(0);
748 while ((ldif = ldb_ldif_read_string(self->ldb_ctx, &s)) != NULL) {
749 PyList_Append(list, ldb_ldif_to_pyobject(ldif));
750 }
751 return PyObject_GetIter(list);
752 }
753
754 static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
755 {
756 const struct ldb_schema_attribute *a;
757 struct ldb_val old_val;
758 struct ldb_val new_val;
759 TALLOC_CTX *mem_ctx;
760 PyObject *ret;
761 char *element_name;
762 PyObject *val;
763
764 if (!PyArg_ParseTuple(args, "sO", &element_name, &val))
765 return NULL;
766
767 mem_ctx = talloc_new(NULL);
768
769 old_val.data = (uint8_t *)PyString_AsString(val);
770 old_val.length = PyString_Size(val);
771
772 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name);
773
774 if (a == NULL) {
775 Py_RETURN_NONE;
776 }
777
778 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) {
779 talloc_free(mem_ctx);
780 Py_RETURN_NONE;
781 }
782
783 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length);
784
785 talloc_free(mem_ctx);
786
787 return ret;
788 }
789
790 static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
791 {
792 PyObject *py_base = Py_None;
793 enum ldb_scope scope = LDB_SCOPE_DEFAULT;
794 char *expr = NULL;
795 PyObject *py_attrs = Py_None;
796 PyObject *py_controls = Py_None;
797 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL };
798 int ret;
799 struct ldb_result *res;
800 struct ldb_request *req;
801 const char **attrs;
802 struct ldb_context *ldb_ctx;
803 struct ldb_control **parsed_controls;
804 struct ldb_dn *base;
805
806 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO",
807 discard_const_p(char *, kwnames),
808 &py_base, &scope, &expr, &py_attrs, &py_controls))
809 return NULL;
810
811 ldb_ctx = PyLdb_AsLdbContext(self);
812
813 if (py_attrs == Py_None) {
814 attrs = NULL;
815 } else {
816 attrs = PyList_AsStringList(ldb_ctx, py_attrs);
817 if (attrs == NULL)
818 return NULL;
819 }
820
821 if (py_base == Py_None) {
822 base = ldb_get_default_basedn(ldb_ctx);
823 } else {
824 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base))
825 return NULL;
826 }
827
828 if (py_controls == Py_None) {
829 parsed_controls = NULL;
830 } else {
831 const char **controls = PyList_AsStringList(ldb_ctx, py_controls);
832 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls);
833 talloc_free(controls);
834 }
835
836 res = talloc_zero(ldb_ctx, struct ldb_result);
837 if (res == NULL) {
838 PyErr_NoMemory();
839 return NULL;
840 }
841
842 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx,
843 base,
844 scope,
845 expr,
846 attrs,
847 parsed_controls,
848 res,
849 ldb_search_default_callback,
850 NULL);
851
852 if (ret != LDB_SUCCESS) {
853 talloc_free(res);
854 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
855 return NULL;
856 }
857
858 ret = ldb_request(ldb_ctx, req);
859
860 if (ret == LDB_SUCCESS) {
861 ret = ldb_wait(req->handle, LDB_WAIT_ALL);
862 }
863
864 talloc_free(req);
865
866 if (ret != LDB_SUCCESS) {
867 talloc_free(res);
868 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, ldb_ctx);
869 return NULL;
870 }
871
872 return PyLdbResult_FromResult(res);
873 }
874
875 static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
876 {
877 char *name;
878 void *data;
879
880 if (!PyArg_ParseTuple(args, "s", &name))
881 return NULL;
882
883 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name);
884
885 if (data == NULL)
886 Py_RETURN_NONE;
887
888 /* FIXME: More interpretation */
889
890 return Py_True;
891 }
892
893 static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
894 {
895 char *name;
896 PyObject *data;
897
898 if (!PyArg_ParseTuple(args, "sO", &name, &data))
899 return NULL;
900
901 /* FIXME: More interpretation */
902
903 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data);
904
905 Py_RETURN_NONE;
906 }
907
908 static PyObject *py_ldb_modules(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
909 {
910 struct ldb_context *ldb = PyLdb_AsLdbContext(self);
911 PyObject *ret = PyList_New(0);
912 struct ldb_module *mod;
913
914 for (mod = ldb->modules; mod; mod = mod->next) {
915 PyList_Append(ret, PyLdbModule_FromModule(mod));
916 }
917
918 return ret;
919 }
920
921 static PyMethodDef py_ldb_methods[] = {
922 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS,
923 "S.set_debug(callback) -> None\n"
924 "Set callback for LDB debug messages.\n"
925 "The callback should accept a debug level and debug text." },
926 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS,
927 "S.set_create_perms(mode) -> None\n"
928 "Set mode to use when creating new LDB files." },
929 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS,
930 "S.set_modules_dir(path) -> None\n"
931 "Set path LDB should search for modules" },
932 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS,
933 "S.transaction_start() -> None\n"
934 "Start a new transaction." },
935 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS,
936 "S.transaction_commit() -> None\n"
937 "commit a new transaction." },
938 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS,
939 "S.transaction_cancel() -> None\n"
940 "cancel a new transaction." },
941 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS,
942 NULL },
943 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS,
944 NULL },
945 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS,
946 NULL },
947 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS,
948 NULL },
949 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS,
950 NULL },
951 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS,
952 "S.connect(url, flags=0, options=None) -> None\n"
953 "Connect to a LDB URL." },
954 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS,
955 "S.modify(message) -> None\n"
956 "Modify an entry." },
957 { "add", (PyCFunction)py_ldb_add, METH_VARARGS,
958 "S.add(message) -> None\n"
959 "Add an entry." },
960 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS,
961 "S.delete(dn) -> None\n"
962 "Remove an entry." },
963 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS,
964 "S.rename(old_dn, new_dn) -> None\n"
965 "Rename an entry." },
966 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS,
967 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
968 "Search in a database.\n"
969 "\n"
970 ":param base: Optional base DN to search\n"
971 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
972 ":param expression: Optional search expression\n"
973 ":param attrs: Attributes to return (defaults to all)\n"
974 ":param controls: Optional list of controls\n"
975 ":return: Iterator over Message objects\n"
976 },
977 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS,
978 NULL },
979 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS,
980 NULL },
981 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS,
982 NULL },
983 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS,
984 "S.parse_ldif(ldif) -> iter(messages)\n"
985 "Parse a string formatted using LDIF." },
986 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS,
987 "S.get_opaque(name) -> value\n"
988 "Get an opaque value set on this LDB connection. \n"
989 ":note: The returned value may not be useful in Python."
990 },
991 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS,
992 "S.set_opaque(name, value) -> None\n"
993 "Set an opaque value on this LDB connection. \n"
994 ":note: Passing incorrect values may cause crashes." },
995 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS,
996 "S.modules() -> list\n"
997 "Return the list of modules on this LDB connection " },
998 { NULL },
999 };
1000
1001 PyObject *PyLdbModule_FromModule(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1002 {
1003 PyLdbModuleObject *ret;
1004
1005 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0);
1006 if (ret == NULL) {
1007 PyErr_NoMemory();
1008 return NULL;
1009 }
1010 ret->mem_ctx = talloc_new(NULL);
1011 ret->mod = talloc_reference(ret->mem_ctx, mod);
1012 return (PyObject *)ret;
1013 }
1014
1015 static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure)
/* [<][>][^][v][top][bottom][index][help] */
1016 {
1017 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules);
1018 }
1019
1020 static PyGetSetDef py_ldb_getset[] = {
1021 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL },
1022 { NULL }
1023 };
1024
1025 static int py_ldb_contains(PyLdbObject *self, PyObject *obj)
/* [<][>][^][v][top][bottom][index][help] */
1026 {
1027 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self);
1028 struct ldb_dn *dn;
1029 struct ldb_result *result;
1030 int ret;
1031 int count;
1032
1033 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn))
1034 return -1;
1035
1036 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL);
1037 if (ret != LDB_SUCCESS) {
1038 PyErr_SetLdbError(ret, ldb_ctx);
1039 return -1;
1040 }
1041
1042 count = result->count;
1043
1044 talloc_free(result);
1045
1046 return count;
1047 }
1048
1049 static PySequenceMethods py_ldb_seq = {
1050 .sq_contains = (objobjproc)py_ldb_contains,
1051 };
1052
1053 PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx)
/* [<][>][^][v][top][bottom][index][help] */
1054 {
1055 PyLdbObject *ret;
1056
1057 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0);
1058 if (ret == NULL) {
1059 PyErr_NoMemory();
1060 return NULL;
1061 }
1062 ret->mem_ctx = talloc_new(NULL);
1063 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx);
1064 return (PyObject *)ret;
1065 }
1066
1067 static void py_ldb_dealloc(PyLdbObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1068 {
1069 talloc_free(self->mem_ctx);
1070 self->ob_type->tp_free(self);
1071 }
1072
1073 PyTypeObject PyLdb = {
1074 .tp_name = "Ldb",
1075 .tp_methods = py_ldb_methods,
1076 .tp_repr = (reprfunc)py_ldb_repr,
1077 .tp_new = py_ldb_new,
1078 .tp_init = (initproc)py_ldb_init,
1079 .tp_dealloc = (destructor)py_ldb_dealloc,
1080 .tp_getset = py_ldb_getset,
1081 .tp_getattro = PyObject_GenericGetAttr,
1082 .tp_basicsize = sizeof(PyLdbObject),
1083 .tp_doc = "Connection to a LDB database.",
1084 .tp_as_sequence = &py_ldb_seq,
1085 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
1086 };
1087
1088 static PyObject *py_ldb_module_repr(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1089 {
1090 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name);
1091 }
1092
1093 static PyObject *py_ldb_module_str(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1094 {
1095 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name);
1096 }
1097
1098 static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1099 {
1100 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self));
1101 Py_RETURN_NONE;
1102 }
1103
1104 static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1105 {
1106 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self));
1107 Py_RETURN_NONE;
1108 }
1109
1110 static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1111 {
1112 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self));
1113 Py_RETURN_NONE;
1114 }
1115
1116 static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
1117 {
1118 PyObject *py_base, *py_tree, *py_attrs;
1119 int ret, scope;
1120 struct ldb_request *req;
1121 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL };
1122 struct ldb_module *mod;
1123
1124 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO",
1125 discard_const_p(char *, kwnames),
1126 &py_base, &scope, &py_tree, &py_attrs))
1127 return NULL;
1128
1129 mod = self->mod;
1130
1131 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base),
1132 scope, NULL /* expr */, py_attrs == Py_None?NULL:PyList_AsStringList(req, py_attrs),
1133 NULL /* controls */, NULL, NULL, NULL);
1134 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1135
1136 ret = mod->ops->search(mod, req);
1137 talloc_free(req);
1138
1139 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1140
1141 return PyLdbResult_FromResult(req->op.search.res);
1142 }
1143
1144
1145 static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1146 {
1147 struct ldb_request *req;
1148 PyObject *py_message;
1149 int ret;
1150 struct ldb_module *mod;
1151
1152 if (!PyArg_ParseTuple(args, "O", &py_message))
1153 return NULL;
1154
1155 req = talloc_zero(NULL, struct ldb_request);
1156 req->operation = LDB_ADD;
1157 req->op.add.message = PyLdbMessage_AsMessage(py_message);
1158
1159 mod = PyLdbModule_AsModule(self);
1160 ret = mod->ops->add(mod, req);
1161
1162 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1163
1164 Py_RETURN_NONE;
1165 }
1166
1167 static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1168 {
1169 int ret;
1170 struct ldb_request *req;
1171 PyObject *py_message;
1172 struct ldb_module *mod;
1173
1174 if (!PyArg_ParseTuple(args, "O", &py_message))
1175 return NULL;
1176
1177 req = talloc_zero(NULL, struct ldb_request);
1178 req->operation = LDB_MODIFY;
1179 req->op.mod.message = PyLdbMessage_AsMessage(py_message);
1180
1181 mod = PyLdbModule_AsModule(self);
1182 ret = mod->ops->modify(mod, req);
1183
1184 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, mod->ldb);
1185
1186 Py_RETURN_NONE;
1187 }
1188
1189 static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1190 {
1191 int ret;
1192 struct ldb_request *req;
1193 PyObject *py_dn;
1194
1195 if (!PyArg_ParseTuple(args, "O", &py_dn))
1196 return NULL;
1197
1198 req = talloc_zero(NULL, struct ldb_request);
1199 req->operation = LDB_DELETE;
1200 req->op.del.dn = PyLdbDn_AsDn(py_dn);
1201
1202 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req);
1203
1204 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1205
1206 Py_RETURN_NONE;
1207 }
1208
1209 static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1210 {
1211 int ret;
1212 struct ldb_request *req;
1213 PyObject *py_dn1, *py_dn2;
1214
1215 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2))
1216 return NULL;
1217
1218 req = talloc_zero(NULL, struct ldb_request);
1219
1220 req->operation = LDB_RENAME;
1221 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1);
1222 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2);
1223
1224 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req);
1225
1226 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
1227
1228 Py_RETURN_NONE;
1229 }
1230
1231 static PyMethodDef py_ldb_module_methods[] = {
1232 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL },
1233 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL },
1234 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL },
1235 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL },
1236 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL },
1237 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL },
1238 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL },
1239 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL },
1240 { NULL },
1241 };
1242
1243 static void py_ldb_module_dealloc(PyLdbModuleObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1244 {
1245 talloc_free(self->mem_ctx);
1246 self->ob_type->tp_free(self);
1247 }
1248
1249 PyTypeObject PyLdbModule = {
1250 .tp_name = "LdbModule",
1251 .tp_methods = py_ldb_module_methods,
1252 .tp_repr = (reprfunc)py_ldb_module_repr,
1253 .tp_str = (reprfunc)py_ldb_module_str,
1254 .tp_basicsize = sizeof(PyLdbModuleObject),
1255 .tp_dealloc = (destructor)py_ldb_module_dealloc,
1256 .tp_flags = Py_TPFLAGS_DEFAULT,
1257 };
1258
1259 struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx,
/* [<][>][^][v][top][bottom][index][help] */
1260 PyObject *set_obj, int flags,
1261 const char *attr_name)
1262 {
1263 struct ldb_message_element *me;
1264
1265 if (PyLdbMessageElement_Check(set_obj))
1266 return PyLdbMessageElement_AsMessageElement(set_obj);
1267
1268 me = talloc(mem_ctx, struct ldb_message_element);
1269
1270 me->name = attr_name;
1271 me->flags = flags;
1272 if (PyString_Check(set_obj)) {
1273 me->num_values = 1;
1274 me->values = talloc_array(me, struct ldb_val, me->num_values);
1275 me->values[0].length = PyString_Size(set_obj);
1276 me->values[0].data = (uint8_t *)talloc_strndup(me->values,
1277 PyString_AsString(set_obj),
1278 me->values[0].length);
1279 } else if (PySequence_Check(set_obj)) {
1280 int i;
1281 me->num_values = PySequence_Size(set_obj);
1282 me->values = talloc_array(me, struct ldb_val, me->num_values);
1283 for (i = 0; i < me->num_values; i++) {
1284 PyObject *obj = PySequence_GetItem(set_obj, i);
1285
1286 me->values[i].length = PyString_Size(obj);
1287 me->values[i].data = (uint8_t *)PyString_AsString(obj);
1288 }
1289 } else {
1290 talloc_free(me);
1291 me = NULL;
1292 }
1293
1294 return me;
1295 }
1296
1297
1298 static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx,
/* [<][>][^][v][top][bottom][index][help] */
1299 struct ldb_message_element *me)
1300 {
1301 int i;
1302 PyObject *result;
1303
1304 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1305 result = PyList_New(me->num_values);
1306
1307 for (i = 0; i < me->num_values; i++) {
1308 PyList_SetItem(result, i,
1309 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i]));
1310 }
1311
1312 return result;
1313 }
1314
1315 static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1316 {
1317 int i;
1318 if (!PyArg_ParseTuple(args, "i", &i))
1319 return NULL;
1320 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values)
1321 Py_RETURN_NONE;
1322
1323 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self),
1324 &(PyLdbMessageElement_AsMessageElement(self)->values[i]));
1325 }
1326
1327 static PyMethodDef py_ldb_msg_element_methods[] = {
1328 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL },
1329 { NULL },
1330 };
1331
1332 static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1333 {
1334 return PyLdbMessageElement_AsMessageElement(self)->num_values;
1335 }
1336
1337 static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx)
/* [<][>][^][v][top][bottom][index][help] */
1338 {
1339 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1340 if (idx < 0 || idx >= el->num_values) {
1341 PyErr_SetString(PyExc_IndexError, "Out of range");
1342 return NULL;
1343 }
1344 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length);
1345 }
1346
1347 static PySequenceMethods py_ldb_msg_element_seq = {
1348 .sq_length = (lenfunc)py_ldb_msg_element_len,
1349 .sq_item = (ssizeargfunc)py_ldb_msg_element_find,
1350 };
1351
1352 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other)
/* [<][>][^][v][top][bottom][index][help] */
1353 {
1354 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self),
1355 PyLdbMessageElement_AsMessageElement(other));
1356 }
1357
1358 static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1359 {
1360 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self)));
1361 }
1362
1363 PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx)
/* [<][>][^][v][top][bottom][index][help] */
1364 {
1365 PyLdbMessageElementObject *ret;
1366 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1367 if (ret == NULL) {
1368 PyErr_NoMemory();
1369 return NULL;
1370 }
1371 ret->mem_ctx = talloc_new(NULL);
1372 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) {
1373 PyErr_NoMemory();
1374 return NULL;
1375 }
1376 ret->el = el;
1377 return (PyObject *)ret;
1378 }
1379
1380 static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
1381 {
1382 PyObject *py_elements = NULL;
1383 struct ldb_message_element *el;
1384 int flags = 0;
1385 char *name = NULL;
1386 const char * const kwnames[] = { "elements", "flags", "name", NULL };
1387 PyLdbMessageElementObject *ret;
1388
1389 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois",
1390 discard_const_p(char *, kwnames),
1391 &py_elements, &flags, &name))
1392 return NULL;
1393
1394 el = talloc_zero(NULL, struct ldb_message_element);
1395
1396 if (py_elements != NULL) {
1397 int i;
1398 if (PyString_Check(py_elements)) {
1399 el->num_values = 1;
1400 el->values = talloc_array(el, struct ldb_val, 1);
1401 el->values[0].data = (uint8_t *)PyString_AsString(py_elements);
1402 el->values[0].length = PyString_Size(py_elements);
1403 } else if (PySequence_Check(py_elements)) {
1404 el->num_values = PySequence_Size(py_elements);
1405 el->values = talloc_array(el, struct ldb_val, el->num_values);
1406 for (i = 0; i < el->num_values; i++) {
1407 PyObject *item = PySequence_GetItem(py_elements, i);
1408 el->values[i].data = (uint8_t *)PyString_AsString(item);
1409 el->values[i].length = PyString_Size(item);
1410 }
1411 } else {
1412 PyErr_SetString(PyExc_TypeError,
1413 "Expected string or list");
1414 talloc_free(el);
1415 return NULL;
1416 }
1417 }
1418
1419 el->flags = flags;
1420 el->name = talloc_strdup(el, name);
1421
1422 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0);
1423 if (ret == NULL) {
1424 PyErr_NoMemory();
1425 talloc_free(el);
1426 return NULL;
1427 }
1428
1429 ret->mem_ctx = talloc_new(NULL);
1430 ret->el = talloc_reference(ret->mem_ctx, el);
1431 return (PyObject *)ret;
1432 }
1433
1434 static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1435 {
1436 char *element_str = NULL;
1437 int i;
1438 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1439 PyObject *ret;
1440
1441 for (i = 0; i < el->num_values; i++) {
1442 PyObject *o = py_ldb_msg_element_find(self, i);
1443 if (element_str == NULL)
1444 element_str = talloc_strdup(NULL, PyObject_REPR(o));
1445 else
1446 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o));
1447 }
1448
1449 ret = PyString_FromFormat("MessageElement([%s])", element_str);
1450
1451 talloc_free(element_str);
1452
1453 return ret;
1454 }
1455
1456 static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1457 {
1458 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self);
1459
1460 if (el->num_values == 1)
1461 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length);
1462 else
1463 Py_RETURN_NONE;
1464 }
1465
1466 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1467 {
1468 talloc_free(self->mem_ctx);
1469 self->ob_type->tp_free(self);
1470 }
1471
1472 PyTypeObject PyLdbMessageElement = {
1473 .tp_name = "MessageElement",
1474 .tp_basicsize = sizeof(PyLdbMessageElementObject),
1475 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc,
1476 .tp_repr = (reprfunc)py_ldb_msg_element_repr,
1477 .tp_str = (reprfunc)py_ldb_msg_element_str,
1478 .tp_methods = py_ldb_msg_element_methods,
1479 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp,
1480 .tp_iter = (getiterfunc)py_ldb_msg_element_iter,
1481 .tp_as_sequence = &py_ldb_msg_element_seq,
1482 .tp_new = py_ldb_msg_element_new,
1483 .tp_flags = Py_TPFLAGS_DEFAULT,
1484 };
1485
1486 static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1487 {
1488 char *name;
1489 if (!PyArg_ParseTuple(args, "s", &name))
1490 return NULL;
1491
1492 ldb_msg_remove_attr(self->msg, name);
1493
1494 Py_RETURN_NONE;
1495 }
1496
1497 static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1498 {
1499 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1500 int i, j = 0;
1501 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0));
1502 if (msg->dn != NULL) {
1503 PyList_SetItem(obj, j, PyString_FromString("dn"));
1504 j++;
1505 }
1506 for (i = 0; i < msg->num_elements; i++) {
1507 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name));
1508 j++;
1509 }
1510 return obj;
1511 }
1512
1513 static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name)
/* [<][>][^][v][top][bottom][index][help] */
1514 {
1515 struct ldb_message_element *el;
1516 char *name = PyString_AsString(py_name);
1517 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1518 if (!strcmp(name, "dn"))
1519 return PyLdbDn_FromDn(msg->dn);
1520 el = ldb_msg_find_element(msg, name);
1521 if (el == NULL) {
1522 return NULL;
1523 }
1524 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg);
1525 }
1526
1527 static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name)
/* [<][>][^][v][top][bottom][index][help] */
1528 {
1529 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name);
1530 if (ret == NULL) {
1531 PyErr_SetString(PyExc_KeyError, "No such element");
1532 return NULL;
1533 }
1534 return ret;
1535 }
1536
1537 static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1538 {
1539 PyObject *name, *ret;
1540 if (!PyArg_ParseTuple(args, "O", &name))
1541 return NULL;
1542
1543 ret = py_ldb_msg_getitem_helper(self, name);
1544 if (ret == NULL)
1545 Py_RETURN_NONE;
1546 return ret;
1547 }
1548
1549 static PyObject *py_ldb_msg_items(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1550 {
1551 struct ldb_message *msg = PyLdbMessage_AsMessage(self);
1552 int i, j;
1553 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1));
1554 j = 0;
1555 if (msg->dn != NULL) {
1556 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn)));
1557 j++;
1558 }
1559 for (i = 0; i < msg->num_elements; i++, j++) {
1560 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg)));
1561 }
1562 return l;
1563 }
1564
1565 static PyMethodDef py_ldb_msg_methods[] = {
1566 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL },
1567 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL },
1568 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL },
1569 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL },
1570 { NULL },
1571 };
1572
1573 static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1574 {
1575 PyObject *list, *iter;
1576
1577 list = py_ldb_msg_keys(self);
1578 iter = PyObject_GetIter(list);
1579 Py_DECREF(list);
1580 return iter;
1581 }
1582
1583 static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value)
/* [<][>][^][v][top][bottom][index][help] */
1584 {
1585 char *attr_name = PyString_AsString(name);
1586 if (value == NULL) {
1587 ldb_msg_remove_attr(self->msg, attr_name);
1588 } else {
1589 struct ldb_message_element *el = PyObject_AsMessageElement(NULL,
1590 value, 0, attr_name);
1591 if (el == NULL)
1592 return -1;
1593 talloc_steal(self->msg, el);
1594 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name);
1595 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags);
1596 }
1597 return 0;
1598 }
1599
1600 static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1601 {
1602 return PyLdbMessage_AsMessage(self)->num_elements;
1603 }
1604
1605 static PyMappingMethods py_ldb_msg_mapping = {
1606 .mp_length = (lenfunc)py_ldb_msg_length,
1607 .mp_subscript = (binaryfunc)py_ldb_msg_getitem,
1608 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem,
1609 };
1610
1611 static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
/* [<][>][^][v][top][bottom][index][help] */
1612 {
1613 const char * const kwnames[] = { "dn", NULL };
1614 struct ldb_message *ret;
1615 PyObject *pydn = NULL;
1616 PyLdbMessageObject *py_ret;
1617
1618 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O",
1619 discard_const_p(char *, kwnames),
1620 &pydn))
1621 return NULL;
1622
1623 ret = ldb_msg_new(NULL);
1624 if (ret == NULL) {
1625 PyErr_NoMemory();
1626 return NULL;
1627 }
1628
1629 if (pydn != NULL)
1630 if (!PyObject_AsDn(NULL, pydn, NULL, &ret->dn))
1631 return NULL;
1632
1633 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0);
1634 if (py_ret == NULL) {
1635 PyErr_NoMemory();
1636 return NULL;
1637 }
1638
1639 py_ret->mem_ctx = talloc_new(NULL);
1640 py_ret->msg = talloc_reference(py_ret->mem_ctx, ret);
1641 return (PyObject *)py_ret;
1642 }
1643
1644 PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg)
/* [<][>][^][v][top][bottom][index][help] */
1645 {
1646 PyLdbMessageObject *ret;
1647
1648 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0);
1649 if (ret == NULL) {
1650 PyErr_NoMemory();
1651 return NULL;
1652 }
1653 ret->mem_ctx = talloc_new(NULL);
1654 ret->msg = talloc_reference(ret->mem_ctx, msg);
1655 return (PyObject *)ret;
1656 }
1657
1658 static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure)
/* [<][>][^][v][top][bottom][index][help] */
1659 {
1660 return PyLdbDn_FromDn(PyLdbMessage_AsMessage(self)->dn);
1661 }
1662
1663 static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure)
/* [<][>][^][v][top][bottom][index][help] */
1664 {
1665 PyLdbMessage_AsMessage(self)->dn = PyLdbDn_AsDn(value);
1666 return 0;
1667 }
1668
1669 static PyGetSetDef py_ldb_msg_getset[] = {
1670 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL },
1671 { NULL }
1672 };
1673
1674 static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1675 {
1676 PyObject *dict = PyDict_New(), *ret;
1677 if (PyDict_Update(dict, (PyObject *)self) != 0)
1678 return NULL;
1679 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict));
1680 Py_DECREF(dict);
1681 return ret;
1682 }
1683
1684 static void py_ldb_msg_dealloc(PyLdbMessageObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1685 {
1686 talloc_free(self->mem_ctx);
1687 self->ob_type->tp_free(self);
1688 }
1689
1690 PyTypeObject PyLdbMessage = {
1691 .tp_name = "Message",
1692 .tp_methods = py_ldb_msg_methods,
1693 .tp_getset = py_ldb_msg_getset,
1694 .tp_as_mapping = &py_ldb_msg_mapping,
1695 .tp_basicsize = sizeof(PyLdbMessageObject),
1696 .tp_dealloc = (destructor)py_ldb_msg_dealloc,
1697 .tp_new = py_ldb_msg_new,
1698 .tp_repr = (reprfunc)py_ldb_msg_repr,
1699 .tp_flags = Py_TPFLAGS_DEFAULT,
1700 .tp_iter = (getiterfunc)py_ldb_msg_iter,
1701 };
1702
1703 PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree)
/* [<][>][^][v][top][bottom][index][help] */
1704 {
1705 PyLdbTreeObject *ret;
1706
1707 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0);
1708 if (ret == NULL) {
1709 PyErr_NoMemory();
1710 return NULL;
1711 }
1712
1713 ret->mem_ctx = talloc_new(NULL);
1714 ret->tree = talloc_reference(ret->mem_ctx, tree);
1715 return (PyObject *)ret;
1716 }
1717
1718 static void py_ldb_tree_dealloc(PyLdbTreeObject *self)
/* [<][>][^][v][top][bottom][index][help] */
1719 {
1720 talloc_free(self->mem_ctx);
1721 self->ob_type->tp_free(self);
1722 }
1723
1724 PyTypeObject PyLdbTree = {
1725 .tp_name = "Tree",
1726 .tp_basicsize = sizeof(PyLdbTreeObject),
1727 .tp_dealloc = (destructor)py_ldb_tree_dealloc,
1728 .tp_flags = Py_TPFLAGS_DEFAULT,
1729 };
1730
1731 /* Ldb_module */
1732 static int py_module_search(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1733 {
1734 PyObject *py_ldb = (PyObject *)mod->private_data;
1735 PyObject *py_result, *py_base, *py_attrs, *py_tree;
1736
1737 py_base = PyLdbDn_FromDn(req->op.search.base);
1738
1739 if (py_base == NULL)
1740 return LDB_ERR_OPERATIONS_ERROR;
1741
1742 py_tree = PyLdbTree_FromTree(req->op.search.tree);
1743
1744 if (py_tree == NULL)
1745 return LDB_ERR_OPERATIONS_ERROR;
1746
1747 if (req->op.search.attrs == NULL) {
1748 py_attrs = Py_None;
1749 } else {
1750 int i, len;
1751 for (len = 0; req->op.search.attrs[len]; len++);
1752 py_attrs = PyList_New(len);
1753 for (i = 0; i < len; i++)
1754 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i]));
1755 }
1756
1757 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"),
1758 discard_const_p(char, "OiOO"),
1759 py_base, req->op.search.scope, py_tree, py_attrs);
1760
1761 Py_DECREF(py_attrs);
1762 Py_DECREF(py_tree);
1763 Py_DECREF(py_base);
1764
1765 if (py_result == NULL) {
1766 return LDB_ERR_PYTHON_EXCEPTION;
1767 }
1768
1769 req->op.search.res = PyLdbResult_AsResult(NULL, py_result);
1770 if (req->op.search.res == NULL) {
1771 return LDB_ERR_PYTHON_EXCEPTION;
1772 }
1773
1774 Py_DECREF(py_result);
1775
1776 return LDB_SUCCESS;
1777 }
1778
1779 static int py_module_add(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1780 {
1781 PyObject *py_ldb = (PyObject *)mod->private_data;
1782 PyObject *py_result, *py_msg;
1783
1784 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message));
1785
1786 if (py_msg == NULL) {
1787 return LDB_ERR_OPERATIONS_ERROR;
1788 }
1789
1790 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"),
1791 discard_const_p(char, "O"),
1792 py_msg);
1793
1794 Py_DECREF(py_msg);
1795
1796 if (py_result == NULL) {
1797 return LDB_ERR_PYTHON_EXCEPTION;
1798 }
1799
1800 Py_DECREF(py_result);
1801
1802 return LDB_SUCCESS;
1803 }
1804
1805 static int py_module_modify(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1806 {
1807 PyObject *py_ldb = (PyObject *)mod->private_data;
1808 PyObject *py_result, *py_msg;
1809
1810 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message));
1811
1812 if (py_msg == NULL) {
1813 return LDB_ERR_OPERATIONS_ERROR;
1814 }
1815
1816 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"),
1817 discard_const_p(char, "O"),
1818 py_msg);
1819
1820 Py_DECREF(py_msg);
1821
1822 if (py_result == NULL) {
1823 return LDB_ERR_PYTHON_EXCEPTION;
1824 }
1825
1826 Py_DECREF(py_result);
1827
1828 return LDB_SUCCESS;
1829 }
1830
1831 static int py_module_del(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1832 {
1833 PyObject *py_ldb = (PyObject *)mod->private_data;
1834 PyObject *py_result, *py_dn;
1835
1836 py_dn = PyLdbDn_FromDn(req->op.del.dn);
1837
1838 if (py_dn == NULL)
1839 return LDB_ERR_OPERATIONS_ERROR;
1840
1841 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"),
1842 discard_const_p(char, "O"),
1843 py_dn);
1844
1845 if (py_result == NULL) {
1846 return LDB_ERR_PYTHON_EXCEPTION;
1847 }
1848
1849 Py_DECREF(py_result);
1850
1851 return LDB_SUCCESS;
1852 }
1853
1854 static int py_module_rename(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1855 {
1856 PyObject *py_ldb = (PyObject *)mod->private_data;
1857 PyObject *py_result, *py_olddn, *py_newdn;
1858
1859 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn);
1860
1861 if (py_olddn == NULL)
1862 return LDB_ERR_OPERATIONS_ERROR;
1863
1864 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn);
1865
1866 if (py_newdn == NULL)
1867 return LDB_ERR_OPERATIONS_ERROR;
1868
1869 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"),
1870 discard_const_p(char, "OO"),
1871 py_olddn, py_newdn);
1872
1873 Py_DECREF(py_olddn);
1874 Py_DECREF(py_newdn);
1875
1876 if (py_result == NULL) {
1877 return LDB_ERR_PYTHON_EXCEPTION;
1878 }
1879
1880 Py_DECREF(py_result);
1881
1882 return LDB_SUCCESS;
1883 }
1884
1885 static int py_module_request(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1886 {
1887 PyObject *py_ldb = (PyObject *)mod->private_data;
1888 PyObject *py_result;
1889
1890 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"),
1891 discard_const_p(char, ""));
1892
1893 return LDB_ERR_OPERATIONS_ERROR;
1894 }
1895
1896 static int py_module_extended(struct ldb_module *mod, struct ldb_request *req)
/* [<][>][^][v][top][bottom][index][help] */
1897 {
1898 PyObject *py_ldb = (PyObject *)mod->private_data;
1899 PyObject *py_result;
1900
1901 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"),
1902 discard_const_p(char, ""));
1903
1904 return LDB_ERR_OPERATIONS_ERROR;
1905 }
1906
1907 static int py_module_start_transaction(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1908 {
1909 PyObject *py_ldb = (PyObject *)mod->private_data;
1910 PyObject *py_result;
1911
1912 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"),
1913 discard_const_p(char, ""));
1914
1915 if (py_result == NULL) {
1916 return LDB_ERR_PYTHON_EXCEPTION;
1917 }
1918
1919 Py_DECREF(py_result);
1920
1921 return LDB_SUCCESS;
1922 }
1923
1924 static int py_module_end_transaction(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1925 {
1926 PyObject *py_ldb = (PyObject *)mod->private_data;
1927 PyObject *py_result;
1928
1929 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"),
1930 discard_const_p(char, ""));
1931
1932 if (py_result == NULL) {
1933 return LDB_ERR_PYTHON_EXCEPTION;
1934 }
1935
1936 Py_DECREF(py_result);
1937
1938 return LDB_SUCCESS;
1939 }
1940
1941 static int py_module_del_transaction(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1942 {
1943 PyObject *py_ldb = (PyObject *)mod->private_data;
1944 PyObject *py_result;
1945
1946 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"),
1947 discard_const_p(char, ""));
1948
1949 if (py_result == NULL) {
1950 return LDB_ERR_PYTHON_EXCEPTION;
1951 }
1952
1953 Py_DECREF(py_result);
1954
1955 return LDB_SUCCESS;
1956 }
1957
1958 static int py_module_destructor(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1959 {
1960 Py_DECREF((PyObject *)mod->private_data);
1961 return 0;
1962 }
1963
1964 static int py_module_init(struct ldb_module *mod)
/* [<][>][^][v][top][bottom][index][help] */
1965 {
1966 PyObject *py_class = (PyObject *)mod->ops->private_data;
1967 PyObject *py_result, *py_next, *py_ldb;
1968
1969 py_ldb = PyLdb_FromLdbContext(mod->ldb);
1970
1971 if (py_ldb == NULL)
1972 return LDB_ERR_OPERATIONS_ERROR;
1973
1974 py_next = PyLdbModule_FromModule(mod->next);
1975
1976 if (py_next == NULL)
1977 return LDB_ERR_OPERATIONS_ERROR;
1978
1979 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"),
1980 py_ldb, py_next);
1981
1982 if (py_result == NULL) {
1983 return LDB_ERR_PYTHON_EXCEPTION;
1984 }
1985
1986 mod->private_data = py_result;
1987
1988 talloc_set_destructor(mod, py_module_destructor);
1989
1990 return ldb_next_init(mod);
1991 }
1992
1993 static PyObject *py_register_module(PyObject *module, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
1994 {
1995 int ret;
1996 struct ldb_module_ops *ops;
1997 PyObject *input;
1998
1999 if (!PyArg_ParseTuple(args, "O", &input))
2000 return NULL;
2001
2002 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops);
2003 if (ops == NULL) {
2004 PyErr_NoMemory();
2005 return NULL;
2006 }
2007
2008 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name"))));
2009
2010 Py_INCREF(input);
2011 ops->private_data = input;
2012 ops->init_context = py_module_init;
2013 ops->search = py_module_search;
2014 ops->add = py_module_add;
2015 ops->modify = py_module_modify;
2016 ops->del = py_module_del;
2017 ops->rename = py_module_rename;
2018 ops->request = py_module_request;
2019 ops->extended = py_module_extended;
2020 ops->start_transaction = py_module_start_transaction;
2021 ops->end_transaction = py_module_end_transaction;
2022 ops->del_transaction = py_module_del_transaction;
2023
2024 ret = ldb_register_module(ops);
2025
2026 PyErr_LDB_ERROR_IS_ERR_RAISE(ret, NULL);
2027
2028 Py_RETURN_NONE;
2029 }
2030
2031 static PyObject *py_timestring(PyObject *module, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
2032 {
2033 time_t t;
2034 char *tresult;
2035 PyObject *ret;
2036 if (!PyArg_ParseTuple(args, "L", &t))
2037 return NULL;
2038 tresult = ldb_timestring(NULL, t);
2039 ret = PyString_FromString(tresult);
2040 talloc_free(tresult);
2041 return ret;
2042 }
2043
2044 static PyObject *py_string_to_time(PyObject *module, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
2045 {
2046 char *str;
2047 if (!PyArg_ParseTuple(args, "s", &str))
2048 return NULL;
2049
2050 return PyInt_FromLong(ldb_string_to_time(str));
2051 }
2052
2053 static PyObject *py_valid_attr_name(PyObject *self, PyObject *args)
/* [<][>][^][v][top][bottom][index][help] */
2054 {
2055 char *name;
2056 if (!PyArg_ParseTuple(args, "s", &name))
2057 return NULL;
2058 return PyBool_FromLong(ldb_valid_attr_name(name));
2059 }
2060
2061 static PyMethodDef py_ldb_global_methods[] = {
2062 { "register_module", py_register_module, METH_VARARGS,
2063 "S.register_module(module) -> None\n"
2064 "Register a LDB module."},
2065 { "timestring", py_timestring, METH_VARARGS,
2066 "S.timestring(int) -> string\n"
2067 "Generate a LDAP time string from a UNIX timestamp" },
2068 { "string_to_time", py_string_to_time, METH_VARARGS,
2069 "S.string_to_time(string) -> int\n"
2070 "Parse a LDAP time string into a UNIX timestamp." },
2071 { "valid_attr_name", py_valid_attr_name, METH_VARARGS,
2072 "S.valid_attr_name(name) -> bool\n"
2073 "Check whether the supplied name is a valid attribute name." },
2074 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS,
2075 NULL },
2076 { NULL }
2077 };
2078
2079 void initldb(void)
/* [<][>][^][v][top][bottom][index][help] */
2080 {
2081 PyObject *m;
2082
2083 if (PyType_Ready(&PyLdbDn) < 0)
2084 return;
2085
2086 if (PyType_Ready(&PyLdbMessage) < 0)
2087 return;
2088
2089 if (PyType_Ready(&PyLdbMessageElement) < 0)
2090 return;
2091
2092 if (PyType_Ready(&PyLdb) < 0)
2093 return;
2094
2095 if (PyType_Ready(&PyLdbModule) < 0)
2096 return;
2097
2098 if (PyType_Ready(&PyLdbTree) < 0)
2099 return;
2100
2101 m = Py_InitModule3("ldb", py_ldb_global_methods,
2102 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2103 if (m == NULL)
2104 return;
2105
2106 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT));
2107 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE));
2108 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL));
2109 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE));
2110
2111 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE));
2112 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD));
2113 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE));
2114 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY));
2115
2116 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS));
2117 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR));
2118 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR));
2119 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED));
2120 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED));
2121 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE));
2122 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE));
2123 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED));
2124 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED));
2125 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL));
2126 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED));
2127 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION));
2128 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED));
2129 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS));
2130 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE));
2131 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE));
2132 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING));
2133 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION));
2134 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS));
2135 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX));
2136 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT));
2137 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM));
2138 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX));
2139 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM));
2140 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION));
2141 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS));
2142 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS));
2143 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY));
2144 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE));
2145 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM));
2146 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT));
2147 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION));
2148 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION));
2149 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF));
2150 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN));
2151 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS));
2152 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED));
2153 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS));
2154
2155 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER));
2156
2157 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText"));
2158
2159 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL);
2160 PyModule_AddObject(m, "LdbError", PyExc_LdbError);
2161
2162 Py_INCREF(&PyLdb);
2163 Py_INCREF(&PyLdbDn);
2164 Py_INCREF(&PyLdbModule);
2165 Py_INCREF(&PyLdbMessage);
2166 Py_INCREF(&PyLdbMessageElement);
2167 Py_INCREF(&PyLdbTree);
2168
2169 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb);
2170 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn);
2171 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage);
2172 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement);
2173 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule);
2174 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree);
2175 }