2 Unix SMB/CIFS implementation.
4 Python interface to ldb.
6 Copyright (C) 2005,2006 Tim Potter <tpot@samba.org>
7 Copyright (C) 2006 Simo Sorce <idra@samba.org>
8 Copyright (C) 2007-2010 Jelmer Vernooij <jelmer@samba.org>
9 Copyright (C) 2009-2010 Matthias Dieter Wallnöfer
11 ** NOTE! The following LGPL license applies to the ldb
12 ** library. This does NOT imply that all of Samba is released
15 This library is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Lesser General Public
17 License as published by the Free Software Foundation; either
18 version 3 of the License, or (at your option) any later version.
20 This library is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 Lesser General Public License for more details.
25 You should have received a copy of the GNU Lesser General Public
26 License along with this library; if not, see <http://www.gnu.org/licenses/>.
31 #include "ldb_private.h"
35 static PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
);
36 static PyObject
*PyExc_LdbError
;
38 staticforward PyTypeObject PyLdbMessage
;
39 staticforward PyTypeObject PyLdbModule
;
40 staticforward PyTypeObject PyLdbDn
;
41 staticforward PyTypeObject PyLdb
;
42 staticforward PyTypeObject PyLdbMessageElement
;
43 staticforward PyTypeObject PyLdbTree
;
44 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
);
45 static PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
);
46 static struct ldb_message_element
*PyObject_AsMessageElement(
50 const char *attr_name
);
52 /* There's no Py_ssize_t in 2.4, apparently */
53 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
54 typedef int Py_ssize_t
;
55 typedef inquiry lenfunc
;
56 typedef intargfunc ssizeargfunc
;
59 #ifndef Py_RETURN_NONE
60 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
63 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
65 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
66 return; /* Python exception should already be set, just keep that */
68 PyErr_SetObject(error
,
69 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
70 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
73 static PyObject
*PyObject_FromLdbValue(struct ldb_context
*ldb_ctx
,
74 struct ldb_message_element
*el
,
77 struct ldb_val new_val
;
78 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
83 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
91 * Create a Python object from a ldb_result.
93 * @param result LDB result to convert
94 * @return Python object with converted result (a list object)
96 static PyObject
*PyLdbResult_FromResult(struct ldb_result
*result
)
100 if (result
== NULL
) {
103 ret
= PyList_New(result
->count
);
104 for (i
= 0; i
< result
->count
; i
++) {
105 PyList_SetItem(ret
, i
, PyLdbMessage_FromMessage(result
->msgs
[i
]));
111 * Create a LDB Result from a Python object.
112 * If conversion fails, NULL will be returned and a Python exception set.
114 * @param mem_ctx Memory context in which to allocate the LDB Result
115 * @param obj Python object to convert
116 * @return a ldb_result, or NULL if the conversion failed
118 static struct ldb_result
*PyLdbResult_AsResult(TALLOC_CTX
*mem_ctx
,
121 struct ldb_result
*res
;
127 res
= talloc_zero(mem_ctx
, struct ldb_result
);
128 res
->count
= PyList_Size(obj
);
129 res
->msgs
= talloc_array(res
, struct ldb_message
*, res
->count
);
130 for (i
= 0; i
< res
->count
; i
++) {
131 PyObject
*item
= PyList_GetItem(obj
, i
);
132 res
->msgs
[i
] = PyLdbMessage_AsMessage(item
);
137 static PyObject
*py_ldb_dn_validate(PyLdbDnObject
*self
)
139 return PyBool_FromLong(ldb_dn_validate(self
->dn
));
142 static PyObject
*py_ldb_dn_is_valid(PyLdbDnObject
*self
)
144 return PyBool_FromLong(ldb_dn_is_valid(self
->dn
));
147 static PyObject
*py_ldb_dn_is_special(PyLdbDnObject
*self
)
149 return PyBool_FromLong(ldb_dn_is_special(self
->dn
));
152 static PyObject
*py_ldb_dn_is_null(PyLdbDnObject
*self
)
154 return PyBool_FromLong(ldb_dn_is_null(self
->dn
));
157 static PyObject
*py_ldb_dn_get_casefold(PyLdbDnObject
*self
)
159 return PyString_FromString(ldb_dn_get_casefold(self
->dn
));
162 static PyObject
*py_ldb_dn_get_linearized(PyLdbDnObject
*self
)
164 return PyString_FromString(ldb_dn_get_linearized(self
->dn
));
167 static PyObject
*py_ldb_dn_canonical_str(PyLdbDnObject
*self
)
169 return PyString_FromString(ldb_dn_canonical_string(self
->dn
, self
->dn
));
172 static PyObject
*py_ldb_dn_canonical_ex_str(PyLdbDnObject
*self
)
174 return PyString_FromString(ldb_dn_canonical_ex_string(self
->dn
, self
->dn
));
177 static PyObject
*py_ldb_dn_repr(PyLdbDnObject
*self
)
179 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self
->dn
))));
182 static PyObject
*py_ldb_dn_check_special(PyLdbDnObject
*self
, PyObject
*args
)
186 if (!PyArg_ParseTuple(args
, "s", &name
))
189 return ldb_dn_check_special(self
->dn
, name
)?Py_True
:Py_False
;
192 static int py_ldb_dn_compare(PyLdbDnObject
*dn1
, PyLdbDnObject
*dn2
)
195 ret
= ldb_dn_compare(dn1
->dn
, dn2
->dn
);
196 if (ret
< 0) ret
= -1;
197 if (ret
> 0) ret
= 1;
201 static PyObject
*py_ldb_dn_get_parent(PyLdbDnObject
*self
)
203 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
);
204 struct ldb_dn
*parent
;
205 PyLdbDnObject
*py_ret
;
206 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
208 parent
= ldb_dn_get_parent(mem_ctx
, dn
);
209 if (parent
== NULL
) {
210 talloc_free(mem_ctx
);
214 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
215 if (py_ret
== NULL
) {
217 talloc_free(mem_ctx
);
220 py_ret
->mem_ctx
= mem_ctx
;
222 return (PyObject
*)py_ret
;
225 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
227 static PyObject
*py_ldb_dn_add_child(PyLdbDnObject
*self
, PyObject
*args
)
230 struct ldb_dn
*dn
, *other
;
231 if (!PyArg_ParseTuple(args
, "O", &py_other
))
234 dn
= PyLdbDn_AsDn((PyObject
*)self
);
236 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
239 return ldb_dn_add_child(dn
, other
)?Py_True
:Py_False
;
242 static PyObject
*py_ldb_dn_add_base(PyLdbDnObject
*self
, PyObject
*args
)
245 struct ldb_dn
*other
, *dn
;
246 if (!PyArg_ParseTuple(args
, "O", &py_other
))
249 dn
= PyLdbDn_AsDn((PyObject
*)self
);
251 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
254 return ldb_dn_add_base(dn
, other
)?Py_True
:Py_False
;
257 static PyMethodDef py_ldb_dn_methods
[] = {
258 { "validate", (PyCFunction
)py_ldb_dn_validate
, METH_NOARGS
,
259 "S.validate() -> bool\n"
260 "Validate DN is correct." },
261 { "is_valid", (PyCFunction
)py_ldb_dn_is_valid
, METH_NOARGS
,
262 "S.is_valid() -> bool\n" },
263 { "is_special", (PyCFunction
)py_ldb_dn_is_special
, METH_NOARGS
,
264 "S.is_special() -> bool\n"
265 "Check whether this is a special LDB DN." },
266 { "is_null", (PyCFunction
)py_ldb_dn_is_null
, METH_NOARGS
,
267 "Check whether this is a null DN." },
268 { "get_casefold", (PyCFunction
)py_ldb_dn_get_casefold
, METH_NOARGS
,
270 { "get_linearized", (PyCFunction
)py_ldb_dn_get_linearized
, METH_NOARGS
,
272 { "canonical_str", (PyCFunction
)py_ldb_dn_canonical_str
, METH_NOARGS
,
273 "S.canonical_str() -> string\n"
274 "Canonical version of this DN (like a posix path)." },
275 { "canonical_ex_str", (PyCFunction
)py_ldb_dn_canonical_ex_str
, METH_NOARGS
,
276 "S.canonical_ex_str() -> string\n"
277 "Canonical version of this DN (like a posix path, with terminating newline)." },
278 { "parent", (PyCFunction
)py_ldb_dn_get_parent
, METH_NOARGS
,
280 "Get the parent for this DN." },
281 { "add_child", (PyCFunction
)py_ldb_dn_add_child
, METH_VARARGS
,
282 "S.add_child(dn) -> None\n"
283 "Add a child DN to this DN." },
284 { "add_base", (PyCFunction
)py_ldb_dn_add_base
, METH_VARARGS
,
285 "S.add_base(dn) -> None\n"
286 "Add a base DN to this DN." },
287 { "check_special", (PyCFunction
)py_ldb_dn_check_special
, METH_VARARGS
,
288 "S.check_special(name) -> bool\n\n"
289 "Check if name is a special DN name"},
293 static Py_ssize_t
py_ldb_dn_len(PyLdbDnObject
*self
)
295 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject
*)self
));
298 static PyObject
*py_ldb_dn_concat(PyLdbDnObject
*self
, PyObject
*py_other
)
300 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
),
302 PyLdbDnObject
*py_ret
;
304 if (!PyObject_AsDn(NULL
, py_other
, NULL
, &other
))
307 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
308 if (py_ret
== NULL
) {
312 py_ret
->mem_ctx
= talloc_new(NULL
);
313 py_ret
->dn
= ldb_dn_copy(py_ret
->mem_ctx
, dn
);
314 ldb_dn_add_child(py_ret
->dn
, other
);
315 return (PyObject
*)py_ret
;
318 static PySequenceMethods py_ldb_dn_seq
= {
319 .sq_length
= (lenfunc
)py_ldb_dn_len
,
320 .sq_concat
= (binaryfunc
)py_ldb_dn_concat
,
323 static PyObject
*py_ldb_dn_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
328 struct ldb_context
*ldb_ctx
;
330 PyLdbDnObject
*py_ret
;
331 const char * const kwnames
[] = { "ldb", "dn", NULL
};
333 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Os",
334 discard_const_p(char *, kwnames
),
338 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
340 mem_ctx
= talloc_new(NULL
);
341 if (mem_ctx
== NULL
) {
346 ret
= ldb_dn_new(mem_ctx
, ldb_ctx
, str
);
348 if (ret
== NULL
|| !ldb_dn_validate(ret
)) {
349 talloc_free(mem_ctx
);
350 PyErr_SetString(PyExc_ValueError
, "unable to parse dn string");
354 py_ret
= (PyLdbDnObject
*)type
->tp_alloc(type
, 0);
356 talloc_free(mem_ctx
);
360 py_ret
->mem_ctx
= mem_ctx
;
362 return (PyObject
*)py_ret
;
365 static void py_ldb_dn_dealloc(PyLdbDnObject
*self
)
367 talloc_free(self
->mem_ctx
);
371 static PyTypeObject PyLdbDn
= {
373 .tp_methods
= py_ldb_dn_methods
,
374 .tp_str
= (reprfunc
)py_ldb_dn_get_linearized
,
375 .tp_repr
= (reprfunc
)py_ldb_dn_repr
,
376 .tp_compare
= (cmpfunc
)py_ldb_dn_compare
,
377 .tp_as_sequence
= &py_ldb_dn_seq
,
378 .tp_doc
= "A LDB distinguished name.",
379 .tp_new
= py_ldb_dn_new
,
380 .tp_dealloc
= (destructor
)py_ldb_dn_dealloc
,
381 .tp_basicsize
= sizeof(PyLdbObject
),
382 .tp_flags
= Py_TPFLAGS_DEFAULT
,
386 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3, 0);
387 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
)
389 PyObject
*fn
= (PyObject
*)context
;
390 PyObject_CallFunction(fn
, discard_const_p(char, "(i,O)"), level
, PyString_FromFormatV(fmt
, ap
));
393 static PyObject
*py_ldb_set_debug(PyLdbObject
*self
, PyObject
*args
)
397 if (!PyArg_ParseTuple(args
, "O", &cb
))
401 /* FIXME: Where do we DECREF cb ? */
402 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_set_debug(self
->ldb_ctx
, py_ldb_debug
, cb
), PyLdb_AsLdbContext(self
));
407 static PyObject
*py_ldb_set_create_perms(PyTypeObject
*self
, PyObject
*args
)
410 if (!PyArg_ParseTuple(args
, "I", &perms
))
413 ldb_set_create_perms(PyLdb_AsLdbContext(self
), perms
);
418 static PyObject
*py_ldb_set_modules_dir(PyTypeObject
*self
, PyObject
*args
)
421 if (!PyArg_ParseTuple(args
, "s", &modules_dir
))
424 ldb_set_modules_dir(PyLdb_AsLdbContext(self
), modules_dir
);
429 static PyObject
*py_ldb_transaction_start(PyLdbObject
*self
)
431 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_start(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
435 static PyObject
*py_ldb_transaction_commit(PyLdbObject
*self
)
437 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
441 static PyObject
*py_ldb_transaction_prepare_commit(PyLdbObject
*self
)
443 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
447 static PyObject
*py_ldb_transaction_cancel(PyLdbObject
*self
)
449 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_cancel(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
453 static PyObject
*py_ldb_setup_wellknown_attributes(PyLdbObject
*self
)
455 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
459 static PyObject
*py_ldb_repr(PyLdbObject
*self
)
461 return PyString_FromFormat("<ldb connection>");
464 static PyObject
*py_ldb_get_root_basedn(PyLdbObject
*self
)
466 struct ldb_dn
*dn
= ldb_get_root_basedn(PyLdb_AsLdbContext(self
));
469 return PyLdbDn_FromDn(dn
);
473 static PyObject
*py_ldb_get_schema_basedn(PyLdbObject
*self
)
475 struct ldb_dn
*dn
= ldb_get_schema_basedn(PyLdb_AsLdbContext(self
));
478 return PyLdbDn_FromDn(dn
);
481 static PyObject
*py_ldb_get_config_basedn(PyLdbObject
*self
)
483 struct ldb_dn
*dn
= ldb_get_config_basedn(PyLdb_AsLdbContext(self
));
486 return PyLdbDn_FromDn(dn
);
489 static PyObject
*py_ldb_get_default_basedn(PyLdbObject
*self
)
491 struct ldb_dn
*dn
= ldb_get_default_basedn(PyLdb_AsLdbContext(self
));
494 return PyLdbDn_FromDn(dn
);
497 static const char **PyList_AsStringList(TALLOC_CTX
*mem_ctx
, PyObject
*list
,
498 const char *paramname
)
502 if (!PyList_Check(list
)) {
503 PyErr_Format(PyExc_TypeError
, "%s is not a list", paramname
);
506 ret
= talloc_array(NULL
, const char *, PyList_Size(list
)+1);
507 for (i
= 0; i
< PyList_Size(list
); i
++) {
508 PyObject
*item
= PyList_GetItem(list
, i
);
509 if (!PyString_Check(item
)) {
510 PyErr_Format(PyExc_TypeError
, "%s should be strings", paramname
);
513 ret
[i
] = talloc_strndup(ret
, PyString_AsString(item
),
514 PyString_Size(item
));
520 static int py_ldb_init(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
522 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
524 PyObject
*py_options
= Py_None
;
525 const char **options
;
528 struct ldb_context
*ldb
;
530 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO:Ldb.__init__",
531 discard_const_p(char *, kwnames
),
532 &url
, &flags
, &py_options
))
535 ldb
= PyLdb_AsLdbContext(self
);
537 if (py_options
== Py_None
) {
540 options
= PyList_AsStringList(ldb
, py_options
, "options");
546 ret
= ldb_connect(ldb
, url
, flags
, options
);
547 if (ret
!= LDB_SUCCESS
) {
548 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb
);
553 talloc_free(options
);
557 static PyObject
*py_ldb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
560 struct ldb_context
*ldb
;
561 ret
= (PyLdbObject
*)type
->tp_alloc(type
, 0);
566 ret
->mem_ctx
= talloc_new(NULL
);
567 ldb
= ldb_init(ret
->mem_ctx
, NULL
);
575 return (PyObject
*)ret
;
578 static PyObject
*py_ldb_connect(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
582 PyObject
*py_options
= Py_None
;
584 const char **options
;
585 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
587 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO",
588 discard_const_p(char *, kwnames
),
589 &url
, &flags
, &py_options
))
592 if (py_options
== Py_None
) {
595 options
= PyList_AsStringList(NULL
, py_options
, "options");
600 ret
= ldb_connect(PyLdb_AsLdbContext(self
), url
, flags
, options
);
601 talloc_free(options
);
603 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
608 static PyObject
*py_ldb_modify(PyLdbObject
*self
, PyObject
*args
)
611 PyObject
*py_controls
= Py_None
;
612 struct ldb_context
*ldb_ctx
;
613 struct ldb_request
*req
;
614 struct ldb_control
**parsed_controls
;
615 struct ldb_message
*msg
;
619 if (!PyArg_ParseTuple(args
, "O|O", &py_msg
, &py_controls
))
622 mem_ctx
= talloc_new(NULL
);
623 if (mem_ctx
== NULL
) {
627 ldb_ctx
= PyLdb_AsLdbContext(self
);
629 if (py_controls
== Py_None
) {
630 parsed_controls
= NULL
;
632 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
633 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
634 talloc_free(controls
);
637 if (!PyLdbMessage_Check(py_msg
)) {
638 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message");
639 talloc_free(mem_ctx
);
642 msg
= PyLdbMessage_AsMessage(py_msg
);
644 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
645 if (ret
!= LDB_SUCCESS
) {
646 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
647 talloc_free(mem_ctx
);
651 ret
= ldb_build_mod_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
652 NULL
, ldb_op_default_callback
, NULL
);
653 if (ret
!= LDB_SUCCESS
) {
654 PyErr_SetString(PyExc_TypeError
, "failed to build request");
655 talloc_free(mem_ctx
);
659 /* do request and autostart a transaction */
660 /* Then let's LDB handle the message error in case of pb as they are meaningful */
662 ret
= ldb_transaction_start(ldb_ctx
);
663 if (ret
!= LDB_SUCCESS
) {
664 talloc_free(mem_ctx
);
665 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
668 ret
= ldb_request(ldb_ctx
, req
);
669 if (ret
== LDB_SUCCESS
) {
670 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
673 if (ret
== LDB_SUCCESS
) {
674 ret
= ldb_transaction_commit(ldb_ctx
);
676 ldb_transaction_cancel(ldb_ctx
);
677 if (ldb_ctx
->err_string
== NULL
) {
678 /* no error string was setup by the backend */
679 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
683 talloc_free(mem_ctx
);
684 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
691 * Obtain a ldb message from a Python Dictionary object.
693 * @param mem_ctx Memory context
694 * @param py_obj Python Dictionary object
695 * @param ldb_ctx LDB context
696 * @param mod_flags Flags to be set on every message element
697 * @return ldb_message on success or NULL on failure
699 static struct ldb_message
*PyDict_AsMessage(TALLOC_CTX
*mem_ctx
,
701 struct ldb_context
*ldb_ctx
,
702 unsigned int mod_flags
)
704 struct ldb_message
*msg
;
705 unsigned int msg_pos
= 0;
706 Py_ssize_t dict_pos
= 0;
707 PyObject
*key
, *value
;
708 struct ldb_message_element
*msg_el
;
709 PyObject
*dn_value
= PyDict_GetItemString(py_obj
, "dn");
711 msg
= ldb_msg_new(mem_ctx
);
712 msg
->elements
= talloc_zero_array(msg
, struct ldb_message_element
, PyDict_Size(py_obj
));
715 if (!PyObject_AsDn(msg
, dn_value
, ldb_ctx
, &msg
->dn
)) {
716 PyErr_SetString(PyExc_TypeError
, "unable to import dn object");
719 if (msg
->dn
== NULL
) {
720 PyErr_SetString(PyExc_TypeError
, "dn set but not found");
724 PyErr_SetString(PyExc_TypeError
, "no dn set");
728 while (PyDict_Next(py_obj
, &dict_pos
, &key
, &value
)) {
729 char *key_str
= PyString_AsString(key
);
730 if (strcmp(key_str
, "dn") != 0) {
731 msg_el
= PyObject_AsMessageElement(msg
->elements
, value
,
733 if (msg_el
== NULL
) {
734 PyErr_SetString(PyExc_TypeError
, "unable to import element");
737 memcpy(&msg
->elements
[msg_pos
], msg_el
, sizeof(*msg_el
));
742 msg
->num_elements
= msg_pos
;
747 static PyObject
*py_ldb_add(PyLdbObject
*self
, PyObject
*args
)
751 struct ldb_context
*ldb_ctx
;
752 struct ldb_request
*req
;
753 struct ldb_message
*msg
= NULL
;
754 PyObject
*py_controls
= Py_None
;
756 struct ldb_control
**parsed_controls
;
758 if (!PyArg_ParseTuple(args
, "O|O", &py_obj
, &py_controls
))
761 mem_ctx
= talloc_new(NULL
);
762 if (mem_ctx
== NULL
) {
766 ldb_ctx
= PyLdb_AsLdbContext(self
);
768 if (py_controls
== Py_None
) {
769 parsed_controls
= NULL
;
771 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
772 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
773 talloc_free(controls
);
776 if (PyLdbMessage_Check(py_obj
)) {
777 msg
= PyLdbMessage_AsMessage(py_obj
);
778 } else if (PyDict_Check(py_obj
)) {
779 msg
= PyDict_AsMessage(mem_ctx
, py_obj
, ldb_ctx
, LDB_FLAG_MOD_ADD
);
781 PyErr_SetString(PyExc_TypeError
,
782 "Dictionary or LdbMessage object expected!");
786 /* we should have a PyErr already set */
787 talloc_free(mem_ctx
);
791 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
792 if (ret
!= LDB_SUCCESS
) {
793 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
794 talloc_free(mem_ctx
);
798 ret
= ldb_build_add_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
799 NULL
, ldb_op_default_callback
, NULL
);
800 if (ret
!= LDB_SUCCESS
) {
801 PyErr_SetString(PyExc_TypeError
, "failed to build request");
802 talloc_free(mem_ctx
);
806 /* do request and autostart a transaction */
807 /* Then let's LDB handle the message error in case of pb as they are meaningful */
809 ret
= ldb_transaction_start(ldb_ctx
);
810 if (ret
!= LDB_SUCCESS
) {
811 talloc_free(mem_ctx
);
812 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
815 ret
= ldb_request(ldb_ctx
, req
);
816 if (ret
== LDB_SUCCESS
) {
817 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
820 if (ret
== LDB_SUCCESS
) {
821 ret
= ldb_transaction_commit(ldb_ctx
);
823 ldb_transaction_cancel(ldb_ctx
);
824 if (ldb_ctx
->err_string
== NULL
) {
825 /* no error string was setup by the backend */
826 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
830 talloc_free(mem_ctx
);
831 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
836 static PyObject
*py_ldb_delete(PyLdbObject
*self
, PyObject
*args
)
841 struct ldb_context
*ldb_ctx
;
842 struct ldb_request
*req
;
843 PyObject
*py_controls
= Py_None
;
845 struct ldb_control
**parsed_controls
;
847 if (!PyArg_ParseTuple(args
, "O|O", &py_dn
, &py_controls
))
850 mem_ctx
= talloc_new(NULL
);
851 if (mem_ctx
== NULL
) {
855 ldb_ctx
= PyLdb_AsLdbContext(self
);
857 if (py_controls
== Py_None
) {
858 parsed_controls
= NULL
;
860 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
861 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
862 talloc_free(controls
);
865 if (!PyObject_AsDn(mem_ctx
, py_dn
, ldb_ctx
, &dn
)) {
866 talloc_free(mem_ctx
);
870 ret
= ldb_build_del_req(&req
, ldb_ctx
, mem_ctx
, dn
, parsed_controls
,
871 NULL
, ldb_op_default_callback
, NULL
);
872 if (ret
!= LDB_SUCCESS
) {
873 PyErr_SetString(PyExc_TypeError
, "failed to build request");
874 talloc_free(mem_ctx
);
878 /* do request and autostart a transaction */
879 /* Then let's LDB handle the message error in case of pb as they are meaningful */
881 ret
= ldb_transaction_start(ldb_ctx
);
882 if (ret
!= LDB_SUCCESS
) {
883 talloc_free(mem_ctx
);
884 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
887 ret
= ldb_request(ldb_ctx
, req
);
888 if (ret
== LDB_SUCCESS
) {
889 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
892 if (ret
== LDB_SUCCESS
) {
893 ret
= ldb_transaction_commit(ldb_ctx
);
895 ldb_transaction_cancel(ldb_ctx
);
896 if (ldb_ctx
->err_string
== NULL
) {
897 /* no error string was setup by the backend */
898 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
902 talloc_free(mem_ctx
);
903 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
908 static PyObject
*py_ldb_rename(PyLdbObject
*self
, PyObject
*args
)
910 PyObject
*py_dn1
, *py_dn2
;
911 struct ldb_dn
*dn1
, *dn2
;
913 struct ldb_context
*ldb
;
915 PyObject
*py_controls
= Py_None
;
916 struct ldb_control
**parsed_controls
;
917 struct ldb_context
*ldb_ctx
;
918 struct ldb_request
*req
;
920 ldb_ctx
= PyLdb_AsLdbContext(self
);
922 if (!PyArg_ParseTuple(args
, "OO|O", &py_dn1
, &py_dn2
, &py_controls
))
926 mem_ctx
= talloc_new(NULL
);
927 if (mem_ctx
== NULL
) {
931 ldb
= PyLdb_AsLdbContext(self
);
933 if (py_controls
== Py_None
) {
934 parsed_controls
= NULL
;
936 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
937 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
938 talloc_free(controls
);
942 if (!PyObject_AsDn(mem_ctx
, py_dn1
, ldb
, &dn1
)) {
943 talloc_free(mem_ctx
);
947 if (!PyObject_AsDn(mem_ctx
, py_dn2
, ldb
, &dn2
)) {
948 talloc_free(mem_ctx
);
952 ret
= ldb_build_rename_req(&req
, ldb_ctx
, mem_ctx
, dn1
, dn2
, parsed_controls
,
953 NULL
, ldb_op_default_callback
, NULL
);
954 if (ret
!= LDB_SUCCESS
) {
955 PyErr_SetString(PyExc_TypeError
, "failed to build request");
956 talloc_free(mem_ctx
);
960 /* do request and autostart a transaction */
961 /* Then let's LDB handle the message error in case of pb as they are meaningful */
963 ret
= ldb_transaction_start(ldb_ctx
);
964 if (ret
!= LDB_SUCCESS
) {
965 talloc_free(mem_ctx
);
966 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
969 ret
= ldb_request(ldb_ctx
, req
);
970 if (ret
== LDB_SUCCESS
) {
971 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
974 if (ret
== LDB_SUCCESS
) {
975 ret
= ldb_transaction_commit(ldb_ctx
);
977 ldb_transaction_cancel(ldb_ctx
);
978 if (ldb_ctx
->err_string
== NULL
) {
979 /* no error string was setup by the backend */
980 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
984 talloc_free(mem_ctx
);
985 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
990 static PyObject
*py_ldb_schema_attribute_remove(PyLdbObject
*self
, PyObject
*args
)
993 if (!PyArg_ParseTuple(args
, "s", &name
))
996 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self
), name
);
1001 static PyObject
*py_ldb_schema_attribute_add(PyLdbObject
*self
, PyObject
*args
)
1003 char *attribute
, *syntax
;
1006 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
1009 ret
= ldb_schema_attribute_add(PyLdb_AsLdbContext(self
), attribute
, flags
, syntax
);
1011 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
1016 static PyObject
*ldb_ldif_to_pyobject(struct ldb_ldif
*ldif
)
1021 /* We don't want this attached to the 'ldb' any more */
1022 return Py_BuildValue(discard_const_p(char, "(iO)"),
1024 PyLdbMessage_FromMessage(ldif
->msg
));
1029 static PyObject
*py_ldb_write_ldif(PyLdbObject
*self
, PyObject
*args
)
1033 struct ldb_ldif ldif
;
1036 TALLOC_CTX
*mem_ctx
;
1038 if (!PyArg_ParseTuple(args
, "Oi", &py_msg
, &changetype
))
1041 if (!PyLdbMessage_Check(py_msg
)) {
1042 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for msg");
1046 ldif
.msg
= PyLdbMessage_AsMessage(py_msg
);
1047 ldif
.changetype
= changetype
;
1049 mem_ctx
= talloc_new(NULL
);
1051 string
= ldb_ldif_write_string(PyLdb_AsLdbContext(self
), mem_ctx
, &ldif
);
1053 PyErr_SetString(PyExc_KeyError
, "Failed to generate LDIF");
1057 ret
= PyString_FromString(string
);
1059 talloc_free(mem_ctx
);
1064 static PyObject
*py_ldb_parse_ldif(PyLdbObject
*self
, PyObject
*args
)
1067 struct ldb_ldif
*ldif
;
1070 TALLOC_CTX
*mem_ctx
;
1072 if (!PyArg_ParseTuple(args
, "s", &s
))
1075 mem_ctx
= talloc_new(NULL
);
1080 list
= PyList_New(0);
1081 while (s
&& *s
!= '\0') {
1082 ldif
= ldb_ldif_read_string(self
->ldb_ctx
, &s
);
1083 talloc_steal(mem_ctx
, ldif
);
1085 PyList_Append(list
, ldb_ldif_to_pyobject(ldif
));
1087 PyErr_SetString(PyExc_ValueError
, "unable to parse ldif string");
1088 talloc_free(mem_ctx
);
1092 talloc_free(mem_ctx
); /* The pyobject already has a reference to the things it needs */
1093 return PyObject_GetIter(list
);
1096 static PyObject
*py_ldb_msg_diff(PyLdbObject
*self
, PyObject
*args
)
1099 PyObject
*py_msg_old
;
1100 PyObject
*py_msg_new
;
1101 struct ldb_message
*diff
;
1102 struct ldb_context
*ldb
;
1105 if (!PyArg_ParseTuple(args
, "OO", &py_msg_old
, &py_msg_new
))
1108 if (!PyLdbMessage_Check(py_msg_old
)) {
1109 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for old message");
1113 if (!PyLdbMessage_Check(py_msg_new
)) {
1114 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for new message");
1118 ldb
= PyLdb_AsLdbContext(self
);
1119 ldb_ret
= ldb_msg_difference(ldb
, ldb
,
1120 PyLdbMessage_AsMessage(py_msg_old
),
1121 PyLdbMessage_AsMessage(py_msg_new
),
1123 if (ldb_ret
!= LDB_SUCCESS
) {
1124 PyErr_SetString(PyExc_RuntimeError
, "Failed to generate the Ldb Message diff");
1128 py_ret
= PyLdbMessage_FromMessage(diff
);
1130 talloc_unlink(ldb
, diff
);
1135 static PyObject
*py_ldb_schema_format_value(PyLdbObject
*self
, PyObject
*args
)
1137 const struct ldb_schema_attribute
*a
;
1138 struct ldb_val old_val
;
1139 struct ldb_val new_val
;
1140 TALLOC_CTX
*mem_ctx
;
1145 if (!PyArg_ParseTuple(args
, "sO", &element_name
, &val
))
1148 mem_ctx
= talloc_new(NULL
);
1150 old_val
.data
= (uint8_t *)PyString_AsString(val
);
1151 old_val
.length
= PyString_Size(val
);
1153 a
= ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self
), element_name
);
1159 if (a
->syntax
->ldif_write_fn(PyLdb_AsLdbContext(self
), mem_ctx
, &old_val
, &new_val
) != 0) {
1160 talloc_free(mem_ctx
);
1164 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
1166 talloc_free(mem_ctx
);
1171 static PyObject
*py_ldb_search(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1173 PyObject
*py_base
= Py_None
;
1174 int scope
= LDB_SCOPE_DEFAULT
;
1176 PyObject
*py_attrs
= Py_None
;
1177 PyObject
*py_controls
= Py_None
;
1178 const char * const kwnames
[] = { "base", "scope", "expression", "attrs", "controls", NULL
};
1180 struct ldb_result
*res
;
1181 struct ldb_request
*req
;
1183 struct ldb_context
*ldb_ctx
;
1184 struct ldb_control
**parsed_controls
;
1185 struct ldb_dn
*base
;
1187 TALLOC_CTX
*mem_ctx
;
1189 /* type "int" rather than "enum" for "scope" is intentional */
1190 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OizOO",
1191 discard_const_p(char *, kwnames
),
1192 &py_base
, &scope
, &expr
, &py_attrs
, &py_controls
))
1196 mem_ctx
= talloc_new(NULL
);
1197 if (mem_ctx
== NULL
) {
1201 ldb_ctx
= PyLdb_AsLdbContext(self
);
1203 if (py_attrs
== Py_None
) {
1206 attrs
= PyList_AsStringList(mem_ctx
, py_attrs
, "attrs");
1207 if (attrs
== NULL
) {
1208 talloc_free(mem_ctx
);
1213 if (py_base
== Py_None
) {
1214 base
= ldb_get_default_basedn(ldb_ctx
);
1216 if (!PyObject_AsDn(ldb_ctx
, py_base
, ldb_ctx
, &base
)) {
1222 if (py_controls
== Py_None
) {
1223 parsed_controls
= NULL
;
1225 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1226 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1227 talloc_free(controls
);
1230 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1233 talloc_free(mem_ctx
);
1237 ret
= ldb_build_search_req(&req
, ldb_ctx
, mem_ctx
,
1244 ldb_search_default_callback
,
1247 if (ret
!= LDB_SUCCESS
) {
1248 talloc_free(mem_ctx
);
1249 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1253 talloc_steal(req
, attrs
);
1255 ret
= ldb_request(ldb_ctx
, req
);
1257 if (ret
== LDB_SUCCESS
) {
1258 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1261 if (ret
!= LDB_SUCCESS
) {
1262 talloc_free(mem_ctx
);
1263 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1267 py_ret
= PyLdbResult_FromResult(res
);
1269 talloc_free(mem_ctx
);
1274 static PyObject
*py_ldb_get_opaque(PyLdbObject
*self
, PyObject
*args
)
1279 if (!PyArg_ParseTuple(args
, "s", &name
))
1282 data
= ldb_get_opaque(PyLdb_AsLdbContext(self
), name
);
1287 /* FIXME: More interpretation */
1292 static PyObject
*py_ldb_set_opaque(PyLdbObject
*self
, PyObject
*args
)
1297 if (!PyArg_ParseTuple(args
, "sO", &name
, &data
))
1300 /* FIXME: More interpretation */
1302 ldb_set_opaque(PyLdb_AsLdbContext(self
), name
, data
);
1307 static PyObject
*py_ldb_modules(PyLdbObject
*self
)
1309 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1310 PyObject
*ret
= PyList_New(0);
1311 struct ldb_module
*mod
;
1313 for (mod
= ldb
->modules
; mod
; mod
= mod
->next
) {
1314 PyList_Append(ret
, PyLdbModule_FromModule(mod
));
1320 static PyObject
*py_ldb_sequence_number(PyLdbObject
*self
, PyObject
*args
)
1322 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1326 if (!PyArg_ParseTuple(args
, "i", &type
))
1329 /* FIXME: More interpretation */
1331 ret
= ldb_sequence_number(ldb
, type
, &value
);
1333 if (ret
!= LDB_SUCCESS
) {
1334 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
1337 return PyLong_FromLongLong(value
);
1339 static PyMethodDef py_ldb_methods
[] = {
1340 { "set_debug", (PyCFunction
)py_ldb_set_debug
, METH_VARARGS
,
1341 "S.set_debug(callback) -> None\n"
1342 "Set callback for LDB debug messages.\n"
1343 "The callback should accept a debug level and debug text." },
1344 { "set_create_perms", (PyCFunction
)py_ldb_set_create_perms
, METH_VARARGS
,
1345 "S.set_create_perms(mode) -> None\n"
1346 "Set mode to use when creating new LDB files." },
1347 { "set_modules_dir", (PyCFunction
)py_ldb_set_modules_dir
, METH_VARARGS
,
1348 "S.set_modules_dir(path) -> None\n"
1349 "Set path LDB should search for modules" },
1350 { "transaction_start", (PyCFunction
)py_ldb_transaction_start
, METH_NOARGS
,
1351 "S.transaction_start() -> None\n"
1352 "Start a new transaction." },
1353 { "transaction_prepare_commit", (PyCFunction
)py_ldb_transaction_prepare_commit
, METH_NOARGS
,
1354 "S.transaction_prepare_commit() -> None\n"
1355 "prepare to commit a new transaction (2-stage commit)." },
1356 { "transaction_commit", (PyCFunction
)py_ldb_transaction_commit
, METH_NOARGS
,
1357 "S.transaction_commit() -> None\n"
1358 "commit a new transaction." },
1359 { "transaction_cancel", (PyCFunction
)py_ldb_transaction_cancel
, METH_NOARGS
,
1360 "S.transaction_cancel() -> None\n"
1361 "cancel a new transaction." },
1362 { "setup_wellknown_attributes", (PyCFunction
)py_ldb_setup_wellknown_attributes
, METH_NOARGS
,
1364 { "get_root_basedn", (PyCFunction
)py_ldb_get_root_basedn
, METH_NOARGS
,
1366 { "get_schema_basedn", (PyCFunction
)py_ldb_get_schema_basedn
, METH_NOARGS
,
1368 { "get_default_basedn", (PyCFunction
)py_ldb_get_default_basedn
, METH_NOARGS
,
1370 { "get_config_basedn", (PyCFunction
)py_ldb_get_config_basedn
, METH_NOARGS
,
1372 { "connect", (PyCFunction
)py_ldb_connect
, METH_VARARGS
|METH_KEYWORDS
,
1373 "S.connect(url, flags=0, options=None) -> None\n"
1374 "Connect to a LDB URL." },
1375 { "modify", (PyCFunction
)py_ldb_modify
, METH_VARARGS
,
1376 "S.modify(message) -> None\n"
1377 "Modify an entry." },
1378 { "add", (PyCFunction
)py_ldb_add
, METH_VARARGS
,
1379 "S.add(message) -> None\n"
1381 { "delete", (PyCFunction
)py_ldb_delete
, METH_VARARGS
,
1382 "S.delete(dn) -> None\n"
1383 "Remove an entry." },
1384 { "rename", (PyCFunction
)py_ldb_rename
, METH_VARARGS
,
1385 "S.rename(old_dn, new_dn) -> None\n"
1386 "Rename an entry." },
1387 { "search", (PyCFunction
)py_ldb_search
, METH_VARARGS
|METH_KEYWORDS
,
1388 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1389 "Search in a database.\n"
1391 ":param base: Optional base DN to search\n"
1392 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1393 ":param expression: Optional search expression\n"
1394 ":param attrs: Attributes to return (defaults to all)\n"
1395 ":param controls: Optional list of controls\n"
1396 ":return: Iterator over Message objects\n"
1398 { "schema_attribute_remove", (PyCFunction
)py_ldb_schema_attribute_remove
, METH_VARARGS
,
1400 { "schema_attribute_add", (PyCFunction
)py_ldb_schema_attribute_add
, METH_VARARGS
,
1402 { "schema_format_value", (PyCFunction
)py_ldb_schema_format_value
, METH_VARARGS
,
1404 { "parse_ldif", (PyCFunction
)py_ldb_parse_ldif
, METH_VARARGS
,
1405 "S.parse_ldif(ldif) -> iter(messages)\n"
1406 "Parse a string formatted using LDIF." },
1407 { "write_ldif", (PyCFunction
)py_ldb_write_ldif
, METH_VARARGS
,
1408 "S.write_ldif(message, changetype) -> ldif\n"
1409 "Print the message as a string formatted using LDIF." },
1410 { "msg_diff", (PyCFunction
)py_ldb_msg_diff
, METH_VARARGS
,
1411 "S.msg_diff(Message) -> Message\n"
1412 "Return an LDB Message of the difference between two Message objects." },
1413 { "get_opaque", (PyCFunction
)py_ldb_get_opaque
, METH_VARARGS
,
1414 "S.get_opaque(name) -> value\n"
1415 "Get an opaque value set on this LDB connection. \n"
1416 ":note: The returned value may not be useful in Python."
1418 { "set_opaque", (PyCFunction
)py_ldb_set_opaque
, METH_VARARGS
,
1419 "S.set_opaque(name, value) -> None\n"
1420 "Set an opaque value on this LDB connection. \n"
1421 ":note: Passing incorrect values may cause crashes." },
1422 { "modules", (PyCFunction
)py_ldb_modules
, METH_NOARGS
,
1423 "S.modules() -> list\n"
1424 "Return the list of modules on this LDB connection " },
1425 { "sequence_number", (PyCFunction
)py_ldb_sequence_number
, METH_VARARGS
,
1426 "S.sequence_number(type) -> value\n"
1427 "Return the value of the sequence according to the requested type" },
1431 static PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
)
1433 PyLdbModuleObject
*ret
;
1435 ret
= (PyLdbModuleObject
*)PyLdbModule
.tp_alloc(&PyLdbModule
, 0);
1440 ret
->mem_ctx
= talloc_new(NULL
);
1441 ret
->mod
= talloc_reference(ret
->mem_ctx
, mod
);
1442 return (PyObject
*)ret
;
1445 static PyObject
*py_ldb_get_firstmodule(PyLdbObject
*self
, void *closure
)
1447 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self
)->modules
);
1450 static PyGetSetDef py_ldb_getset
[] = {
1451 { discard_const_p(char, "firstmodule"), (getter
)py_ldb_get_firstmodule
, NULL
, NULL
},
1455 static int py_ldb_contains(PyLdbObject
*self
, PyObject
*obj
)
1457 struct ldb_context
*ldb_ctx
= PyLdb_AsLdbContext(self
);
1459 struct ldb_result
*result
;
1463 if (!PyObject_AsDn(ldb_ctx
, obj
, ldb_ctx
, &dn
)) {
1467 ret
= ldb_search(ldb_ctx
, ldb_ctx
, &result
, dn
, LDB_SCOPE_BASE
, NULL
,
1469 if (ret
!= LDB_SUCCESS
) {
1470 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb_ctx
);
1474 count
= result
->count
;
1476 talloc_free(result
);
1479 PyErr_Format(PyExc_RuntimeError
,
1480 "Searching for [%s] dn gave %u results!",
1481 ldb_dn_get_linearized(dn
),
1489 static PySequenceMethods py_ldb_seq
= {
1490 .sq_contains
= (objobjproc
)py_ldb_contains
,
1493 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
)
1497 ret
= (PyLdbObject
*)PyLdb
.tp_alloc(&PyLdb
, 0);
1502 ret
->mem_ctx
= talloc_new(NULL
);
1503 ret
->ldb_ctx
= talloc_reference(ret
->mem_ctx
, ldb_ctx
);
1504 return (PyObject
*)ret
;
1507 static void py_ldb_dealloc(PyLdbObject
*self
)
1509 talloc_free(self
->mem_ctx
);
1513 static PyTypeObject PyLdb
= {
1514 .tp_name
= "ldb.Ldb",
1515 .tp_methods
= py_ldb_methods
,
1516 .tp_repr
= (reprfunc
)py_ldb_repr
,
1517 .tp_new
= py_ldb_new
,
1518 .tp_init
= (initproc
)py_ldb_init
,
1519 .tp_dealloc
= (destructor
)py_ldb_dealloc
,
1520 .tp_getset
= py_ldb_getset
,
1521 .tp_getattro
= PyObject_GenericGetAttr
,
1522 .tp_basicsize
= sizeof(PyLdbObject
),
1523 .tp_doc
= "Connection to a LDB database.",
1524 .tp_as_sequence
= &py_ldb_seq
,
1525 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1528 static PyObject
*py_ldb_module_repr(PyLdbModuleObject
*self
)
1530 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self
)->ops
->name
);
1533 static PyObject
*py_ldb_module_str(PyLdbModuleObject
*self
)
1535 return PyString_FromString(PyLdbModule_AsModule(self
)->ops
->name
);
1538 static PyObject
*py_ldb_module_start_transaction(PyLdbModuleObject
*self
)
1540 PyLdbModule_AsModule(self
)->ops
->start_transaction(PyLdbModule_AsModule(self
));
1544 static PyObject
*py_ldb_module_end_transaction(PyLdbModuleObject
*self
)
1546 PyLdbModule_AsModule(self
)->ops
->end_transaction(PyLdbModule_AsModule(self
));
1550 static PyObject
*py_ldb_module_del_transaction(PyLdbModuleObject
*self
)
1552 PyLdbModule_AsModule(self
)->ops
->del_transaction(PyLdbModule_AsModule(self
));
1556 static PyObject
*py_ldb_module_search(PyLdbModuleObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1558 PyObject
*py_base
, *py_tree
, *py_attrs
, *py_ret
;
1560 struct ldb_request
*req
;
1561 const char * const kwnames
[] = { "base", "scope", "tree", "attrs", NULL
};
1562 struct ldb_module
*mod
;
1563 const char * const*attrs
;
1565 /* type "int" rather than "enum" for "scope" is intentional */
1566 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OiOO",
1567 discard_const_p(char *, kwnames
),
1568 &py_base
, &scope
, &py_tree
, &py_attrs
))
1573 if (py_attrs
== Py_None
) {
1576 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1581 ret
= ldb_build_search_req(&req
, mod
->ldb
, NULL
, PyLdbDn_AsDn(py_base
),
1582 scope
, NULL
/* expr */, attrs
,
1583 NULL
/* controls */, NULL
, NULL
, NULL
);
1585 talloc_steal(req
, attrs
);
1587 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1589 req
->op
.search
.res
= NULL
;
1591 ret
= mod
->ops
->search(mod
, req
);
1593 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1595 py_ret
= PyLdbResult_FromResult(req
->op
.search
.res
);
1603 static PyObject
*py_ldb_module_add(PyLdbModuleObject
*self
, PyObject
*args
)
1605 struct ldb_request
*req
;
1606 PyObject
*py_message
;
1608 struct ldb_module
*mod
;
1610 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1613 req
= talloc_zero(NULL
, struct ldb_request
);
1614 req
->operation
= LDB_ADD
;
1615 req
->op
.add
.message
= PyLdbMessage_AsMessage(py_message
);
1617 mod
= PyLdbModule_AsModule(self
);
1618 ret
= mod
->ops
->add(mod
, req
);
1620 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1625 static PyObject
*py_ldb_module_modify(PyLdbModuleObject
*self
, PyObject
*args
)
1628 struct ldb_request
*req
;
1629 PyObject
*py_message
;
1630 struct ldb_module
*mod
;
1632 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1635 req
= talloc_zero(NULL
, struct ldb_request
);
1636 req
->operation
= LDB_MODIFY
;
1637 req
->op
.mod
.message
= PyLdbMessage_AsMessage(py_message
);
1639 mod
= PyLdbModule_AsModule(self
);
1640 ret
= mod
->ops
->modify(mod
, req
);
1642 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1647 static PyObject
*py_ldb_module_delete(PyLdbModuleObject
*self
, PyObject
*args
)
1650 struct ldb_request
*req
;
1653 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
1656 req
= talloc_zero(NULL
, struct ldb_request
);
1657 req
->operation
= LDB_DELETE
;
1658 req
->op
.del
.dn
= PyLdbDn_AsDn(py_dn
);
1660 ret
= PyLdbModule_AsModule(self
)->ops
->del(PyLdbModule_AsModule(self
), req
);
1662 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1667 static PyObject
*py_ldb_module_rename(PyLdbModuleObject
*self
, PyObject
*args
)
1670 struct ldb_request
*req
;
1671 PyObject
*py_dn1
, *py_dn2
;
1673 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
1676 req
= talloc_zero(NULL
, struct ldb_request
);
1678 req
->operation
= LDB_RENAME
;
1679 req
->op
.rename
.olddn
= PyLdbDn_AsDn(py_dn1
);
1680 req
->op
.rename
.newdn
= PyLdbDn_AsDn(py_dn2
);
1682 ret
= PyLdbModule_AsModule(self
)->ops
->rename(PyLdbModule_AsModule(self
), req
);
1684 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1689 static PyMethodDef py_ldb_module_methods
[] = {
1690 { "search", (PyCFunction
)py_ldb_module_search
, METH_VARARGS
|METH_KEYWORDS
, NULL
},
1691 { "add", (PyCFunction
)py_ldb_module_add
, METH_VARARGS
, NULL
},
1692 { "modify", (PyCFunction
)py_ldb_module_modify
, METH_VARARGS
, NULL
},
1693 { "rename", (PyCFunction
)py_ldb_module_rename
, METH_VARARGS
, NULL
},
1694 { "delete", (PyCFunction
)py_ldb_module_delete
, METH_VARARGS
, NULL
},
1695 { "start_transaction", (PyCFunction
)py_ldb_module_start_transaction
, METH_NOARGS
, NULL
},
1696 { "end_transaction", (PyCFunction
)py_ldb_module_end_transaction
, METH_NOARGS
, NULL
},
1697 { "del_transaction", (PyCFunction
)py_ldb_module_del_transaction
, METH_NOARGS
, NULL
},
1701 static void py_ldb_module_dealloc(PyLdbModuleObject
*self
)
1703 talloc_free(self
->mem_ctx
);
1707 static PyTypeObject PyLdbModule
= {
1708 .tp_name
= "ldb.LdbModule",
1709 .tp_methods
= py_ldb_module_methods
,
1710 .tp_repr
= (reprfunc
)py_ldb_module_repr
,
1711 .tp_str
= (reprfunc
)py_ldb_module_str
,
1712 .tp_basicsize
= sizeof(PyLdbModuleObject
),
1713 .tp_dealloc
= (destructor
)py_ldb_module_dealloc
,
1714 .tp_flags
= Py_TPFLAGS_DEFAULT
,
1719 * Create a ldb_message_element from a Python object.
1721 * This will accept any sequence objects that contains strings, or
1724 * A reference to set_obj will be borrowed.
1726 * @param mem_ctx Memory context
1727 * @param set_obj Python object to convert
1728 * @param flags ldb_message_element flags to set
1729 * @param attr_name Name of the attribute
1730 * @return New ldb_message_element, allocated as child of mem_ctx
1732 static struct ldb_message_element
*PyObject_AsMessageElement(
1733 TALLOC_CTX
*mem_ctx
,
1736 const char *attr_name
)
1738 struct ldb_message_element
*me
;
1740 if (PyLdbMessageElement_Check(set_obj
)) {
1741 PyLdbMessageElementObject
*set_obj_as_me
= (PyLdbMessageElementObject
*)set_obj
;
1742 /* We have to talloc_reference() the memory context, not the pointer which may not actually be it's own context */
1743 if (talloc_reference(mem_ctx
, set_obj_as_me
->mem_ctx
)) {
1744 return PyLdbMessageElement_AsMessageElement(set_obj
);
1749 me
= talloc(mem_ctx
, struct ldb_message_element
);
1755 me
->name
= talloc_strdup(me
, attr_name
);
1757 if (PyString_Check(set_obj
)) {
1759 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1760 me
->values
[0].length
= PyString_Size(set_obj
);
1761 me
->values
[0].data
= talloc_memdup(me
,
1762 (uint8_t *)PyString_AsString(set_obj
), me
->values
[0].length
+1);
1763 } else if (PySequence_Check(set_obj
)) {
1765 me
->num_values
= PySequence_Size(set_obj
);
1766 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1767 for (i
= 0; i
< me
->num_values
; i
++) {
1768 PyObject
*obj
= PySequence_GetItem(set_obj
, i
);
1769 if (!PyString_Check(obj
)) {
1770 PyErr_Format(PyExc_TypeError
,
1771 "Expected string as element %zd in list", i
);
1776 me
->values
[i
].length
= PyString_Size(obj
);
1777 me
->values
[i
].data
= talloc_memdup(me
,
1778 (uint8_t *)PyString_AsString(obj
), me
->values
[i
].length
+1);
1789 static PyObject
*ldb_msg_element_to_set(struct ldb_context
*ldb_ctx
,
1790 struct ldb_message_element
*me
)
1795 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1796 result
= PyList_New(me
->num_values
);
1798 for (i
= 0; i
< me
->num_values
; i
++) {
1799 PyList_SetItem(result
, i
,
1800 PyObject_FromLdbValue(ldb_ctx
, me
, &me
->values
[i
]));
1806 static PyObject
*py_ldb_msg_element_get(PyLdbMessageElementObject
*self
, PyObject
*args
)
1809 if (!PyArg_ParseTuple(args
, "I", &i
))
1811 if (i
>= PyLdbMessageElement_AsMessageElement(self
)->num_values
)
1814 return PyObject_FromLdbValue(NULL
, PyLdbMessageElement_AsMessageElement(self
),
1815 &(PyLdbMessageElement_AsMessageElement(self
)->values
[i
]));
1818 static PyObject
*py_ldb_msg_element_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1820 struct ldb_message_element
*el
;
1822 el
= PyLdbMessageElement_AsMessageElement(self
);
1823 return PyInt_FromLong(el
->flags
);
1826 static PyObject
*py_ldb_msg_element_set_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1829 struct ldb_message_element
*el
;
1830 if (!PyArg_ParseTuple(args
, "i", &flags
))
1833 el
= PyLdbMessageElement_AsMessageElement(self
);
1838 static PyMethodDef py_ldb_msg_element_methods
[] = {
1839 { "get", (PyCFunction
)py_ldb_msg_element_get
, METH_VARARGS
, NULL
},
1840 { "set_flags", (PyCFunction
)py_ldb_msg_element_set_flags
, METH_VARARGS
, NULL
},
1841 { "flags", (PyCFunction
)py_ldb_msg_element_flags
, METH_NOARGS
, NULL
},
1845 static Py_ssize_t
py_ldb_msg_element_len(PyLdbMessageElementObject
*self
)
1847 return PyLdbMessageElement_AsMessageElement(self
)->num_values
;
1850 static PyObject
*py_ldb_msg_element_find(PyLdbMessageElementObject
*self
, Py_ssize_t idx
)
1852 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1853 if (idx
< 0 || idx
>= el
->num_values
) {
1854 PyErr_SetString(PyExc_IndexError
, "Out of range");
1857 return PyString_FromStringAndSize((char *)el
->values
[idx
].data
, el
->values
[idx
].length
);
1860 static PySequenceMethods py_ldb_msg_element_seq
= {
1861 .sq_length
= (lenfunc
)py_ldb_msg_element_len
,
1862 .sq_item
= (ssizeargfunc
)py_ldb_msg_element_find
,
1865 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject
*self
, PyLdbMessageElementObject
*other
)
1867 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self
),
1868 PyLdbMessageElement_AsMessageElement(other
));
1871 static PyObject
*py_ldb_msg_element_iter(PyLdbMessageElementObject
*self
)
1873 return PyObject_GetIter(ldb_msg_element_to_set(NULL
, PyLdbMessageElement_AsMessageElement(self
)));
1876 static PyObject
*PyLdbMessageElement_FromMessageElement(struct ldb_message_element
*el
, TALLOC_CTX
*mem_ctx
)
1878 PyLdbMessageElementObject
*ret
;
1879 ret
= PyObject_New(PyLdbMessageElementObject
, &PyLdbMessageElement
);
1884 ret
->mem_ctx
= talloc_new(NULL
);
1885 if (talloc_reference(ret
->mem_ctx
, mem_ctx
) == NULL
) {
1890 return (PyObject
*)ret
;
1893 static PyObject
*py_ldb_msg_element_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1895 PyObject
*py_elements
= NULL
;
1896 struct ldb_message_element
*el
;
1899 const char * const kwnames
[] = { "elements", "flags", "name", NULL
};
1900 PyLdbMessageElementObject
*ret
;
1901 TALLOC_CTX
*mem_ctx
;
1903 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ois",
1904 discard_const_p(char *, kwnames
),
1905 &py_elements
, &flags
, &name
))
1908 mem_ctx
= talloc_new(NULL
);
1909 if (mem_ctx
== NULL
) {
1914 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
1917 talloc_free(mem_ctx
);
1921 if (py_elements
!= NULL
) {
1923 if (PyString_Check(py_elements
)) {
1925 el
->values
= talloc_array(el
, struct ldb_val
, 1);
1926 if (el
->values
== NULL
) {
1927 talloc_free(mem_ctx
);
1931 el
->values
[0].length
= PyString_Size(py_elements
);
1932 el
->values
[0].data
= talloc_memdup(el
->values
,
1933 (uint8_t *)PyString_AsString(py_elements
), el
->values
[0].length
+1);
1934 } else if (PySequence_Check(py_elements
)) {
1935 el
->num_values
= PySequence_Size(py_elements
);
1936 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
1937 if (el
->values
== NULL
) {
1938 talloc_free(mem_ctx
);
1942 for (i
= 0; i
< el
->num_values
; i
++) {
1943 PyObject
*item
= PySequence_GetItem(py_elements
, i
);
1945 talloc_free(mem_ctx
);
1948 if (!PyString_Check(item
)) {
1949 PyErr_Format(PyExc_TypeError
,
1950 "Expected string as element %zd in list", i
);
1951 talloc_free(mem_ctx
);
1954 el
->values
[i
].length
= PyString_Size(item
);
1955 el
->values
[i
].data
= talloc_memdup(el
,
1956 (uint8_t *)PyString_AsString(item
), el
->values
[i
].length
+1);
1959 PyErr_SetString(PyExc_TypeError
,
1960 "Expected string or list");
1961 talloc_free(mem_ctx
);
1967 el
->name
= talloc_strdup(el
, name
);
1969 ret
= PyObject_New(PyLdbMessageElementObject
, type
);
1971 talloc_free(mem_ctx
);
1975 ret
->mem_ctx
= mem_ctx
;
1977 return (PyObject
*)ret
;
1980 static PyObject
*py_ldb_msg_element_repr(PyLdbMessageElementObject
*self
)
1982 char *element_str
= NULL
;
1984 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1987 for (i
= 0; i
< el
->num_values
; i
++) {
1988 PyObject
*o
= py_ldb_msg_element_find(self
, i
);
1989 if (element_str
== NULL
)
1990 element_str
= talloc_strdup(NULL
, PyObject_REPR(o
));
1992 element_str
= talloc_asprintf_append(element_str
, ",%s", PyObject_REPR(o
));
1995 if (element_str
!= NULL
) {
1996 ret
= PyString_FromFormat("MessageElement([%s])", element_str
);
1997 talloc_free(element_str
);
1999 ret
= PyString_FromString("MessageElement([])");
2005 static PyObject
*py_ldb_msg_element_str(PyLdbMessageElementObject
*self
)
2007 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2009 if (el
->num_values
== 1)
2010 return PyString_FromStringAndSize((char *)el
->values
[0].data
, el
->values
[0].length
);
2015 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject
*self
)
2017 talloc_free(self
->mem_ctx
);
2021 static PyTypeObject PyLdbMessageElement
= {
2022 .tp_name
= "ldb.MessageElement",
2023 .tp_basicsize
= sizeof(PyLdbMessageElementObject
),
2024 .tp_dealloc
= (destructor
)py_ldb_msg_element_dealloc
,
2025 .tp_repr
= (reprfunc
)py_ldb_msg_element_repr
,
2026 .tp_str
= (reprfunc
)py_ldb_msg_element_str
,
2027 .tp_methods
= py_ldb_msg_element_methods
,
2028 .tp_compare
= (cmpfunc
)py_ldb_msg_element_cmp
,
2029 .tp_iter
= (getiterfunc
)py_ldb_msg_element_iter
,
2030 .tp_as_sequence
= &py_ldb_msg_element_seq
,
2031 .tp_new
= py_ldb_msg_element_new
,
2032 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2036 static PyObject
*py_ldb_msg_from_dict(PyTypeObject
*type
, PyObject
*args
)
2041 struct ldb_message
*msg
;
2042 struct ldb_context
*ldb_ctx
;
2043 unsigned int mod_flags
= LDB_FLAG_MOD_REPLACE
;
2045 if (!PyArg_ParseTuple(args
, "O!O!|I",
2046 &PyLdb
, &py_ldb
, &PyDict_Type
, &py_dict
,
2051 /* mask only flags we are going to use */
2052 mod_flags
= LDB_FLAG_MOD_TYPE(mod_flags
);
2054 PyErr_SetString(PyExc_ValueError
,
2055 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2056 " expected as mod_flag value");
2060 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
2062 msg
= PyDict_AsMessage(ldb_ctx
, py_dict
, ldb_ctx
, mod_flags
);
2067 py_ret
= PyLdbMessage_FromMessage(msg
);
2069 talloc_unlink(ldb_ctx
, msg
);
2074 static PyObject
*py_ldb_msg_remove_attr(PyLdbMessageObject
*self
, PyObject
*args
)
2077 if (!PyArg_ParseTuple(args
, "s", &name
))
2080 ldb_msg_remove_attr(self
->msg
, name
);
2085 static PyObject
*py_ldb_msg_keys(PyLdbMessageObject
*self
)
2087 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2088 Py_ssize_t i
, j
= 0;
2089 PyObject
*obj
= PyList_New(msg
->num_elements
+(msg
->dn
!= NULL
?1:0));
2090 if (msg
->dn
!= NULL
) {
2091 PyList_SetItem(obj
, j
, PyString_FromString("dn"));
2094 for (i
= 0; i
< msg
->num_elements
; i
++) {
2095 PyList_SetItem(obj
, j
, PyString_FromString(msg
->elements
[i
].name
));
2101 static PyObject
*py_ldb_msg_getitem_helper(PyLdbMessageObject
*self
, PyObject
*py_name
)
2103 struct ldb_message_element
*el
;
2105 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2106 if (!PyString_Check(py_name
)) {
2107 PyErr_SetNone(PyExc_TypeError
);
2110 name
= PyString_AsString(py_name
);
2111 if (!strcmp(name
, "dn"))
2112 return PyLdbDn_FromDn(msg
->dn
);
2113 el
= ldb_msg_find_element(msg
, name
);
2117 return (PyObject
*)PyLdbMessageElement_FromMessageElement(el
, msg
->elements
);
2120 static PyObject
*py_ldb_msg_getitem(PyLdbMessageObject
*self
, PyObject
*py_name
)
2122 PyObject
*ret
= py_ldb_msg_getitem_helper(self
, py_name
);
2124 PyErr_SetString(PyExc_KeyError
, "No such element");
2130 static PyObject
*py_ldb_msg_get(PyLdbMessageObject
*self
, PyObject
*args
)
2132 PyObject
*name
, *ret
;
2133 if (!PyArg_ParseTuple(args
, "O", &name
))
2136 ret
= py_ldb_msg_getitem_helper(self
, name
);
2138 if (PyErr_Occurred())
2145 static PyObject
*py_ldb_msg_items(PyLdbMessageObject
*self
)
2147 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2148 Py_ssize_t i
, j
= 0;
2149 PyObject
*l
= PyList_New(msg
->num_elements
+ (msg
->dn
== NULL
?0:1));
2150 if (msg
->dn
!= NULL
) {
2151 PyList_SetItem(l
, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg
->dn
)));
2154 for (i
= 0; i
< msg
->num_elements
; i
++, j
++) {
2155 PyObject
*py_el
= PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
);
2156 PyObject
*value
= Py_BuildValue("(sO)", msg
->elements
[i
].name
, py_el
);
2157 PyList_SetItem(l
, j
, value
);
2162 static PyObject
*py_ldb_msg_elements(PyLdbMessageObject
*self
)
2164 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2166 PyObject
*l
= PyList_New(msg
->num_elements
);
2167 for (i
= 0; i
< msg
->num_elements
; i
++) {
2168 PyList_SetItem(l
, i
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
));
2173 static PyObject
*py_ldb_msg_add(PyLdbMessageObject
*self
, PyObject
*args
)
2175 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2176 PyObject
*py_element
;
2178 struct ldb_message_element
*el
;
2180 if (!PyArg_ParseTuple(args
, "O!", &PyLdbMessageElement
, &py_element
))
2183 el
= talloc_reference(msg
, PyLdbMessageElement_AsMessageElement(py_element
));
2189 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2190 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2195 static PyMethodDef py_ldb_msg_methods
[] = {
2196 { "from_dict", (PyCFunction
)py_ldb_msg_from_dict
, METH_CLASS
| METH_VARARGS
,
2197 "Message.from_dict(ldb, dict, mod_flag=FLAG_MOD_REPLACE) -> ldb.Message\n"
2198 "Class method to create ldb.Message object from Dictionary.\n"
2199 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE."},
2200 { "keys", (PyCFunction
)py_ldb_msg_keys
, METH_NOARGS
,
2201 "S.keys() -> list\n\n"
2202 "Return sequence of all attribute names." },
2203 { "remove", (PyCFunction
)py_ldb_msg_remove_attr
, METH_VARARGS
,
2204 "S.remove(name)\n\n"
2205 "Remove all entries for attributes with the specified name."},
2206 { "get", (PyCFunction
)py_ldb_msg_get
, METH_VARARGS
, NULL
},
2207 { "items", (PyCFunction
)py_ldb_msg_items
, METH_NOARGS
, NULL
},
2208 { "elements", (PyCFunction
)py_ldb_msg_elements
, METH_NOARGS
, NULL
},
2209 { "add", (PyCFunction
)py_ldb_msg_add
, METH_VARARGS
,
2210 "S.append(element)\n\n"
2211 "Add an element to this message." },
2215 static PyObject
*py_ldb_msg_iter(PyLdbMessageObject
*self
)
2217 PyObject
*list
, *iter
;
2219 list
= py_ldb_msg_keys(self
);
2220 iter
= PyObject_GetIter(list
);
2225 static int py_ldb_msg_setitem(PyLdbMessageObject
*self
, PyObject
*name
, PyObject
*value
)
2229 if (!PyString_Check(name
)) {
2230 PyErr_SetNone(PyExc_TypeError
);
2234 attr_name
= PyString_AsString(name
);
2235 if (value
== NULL
) {
2237 ldb_msg_remove_attr(self
->msg
, attr_name
);
2239 struct ldb_message_element
*el
= PyObject_AsMessageElement(self
->msg
,
2240 value
, 0, attr_name
);
2243 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self
), attr_name
);
2244 ldb_msg_add(PyLdbMessage_AsMessage(self
), el
, el
->flags
);
2249 static Py_ssize_t
py_ldb_msg_length(PyLdbMessageObject
*self
)
2251 return PyLdbMessage_AsMessage(self
)->num_elements
;
2254 static PyMappingMethods py_ldb_msg_mapping
= {
2255 .mp_length
= (lenfunc
)py_ldb_msg_length
,
2256 .mp_subscript
= (binaryfunc
)py_ldb_msg_getitem
,
2257 .mp_ass_subscript
= (objobjargproc
)py_ldb_msg_setitem
,
2260 static PyObject
*py_ldb_msg_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
2262 const char * const kwnames
[] = { "dn", NULL
};
2263 struct ldb_message
*ret
;
2264 TALLOC_CTX
*mem_ctx
;
2265 PyObject
*pydn
= NULL
;
2266 PyLdbMessageObject
*py_ret
;
2268 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O",
2269 discard_const_p(char *, kwnames
),
2273 mem_ctx
= talloc_new(NULL
);
2274 if (mem_ctx
== NULL
) {
2279 ret
= ldb_msg_new(mem_ctx
);
2281 talloc_free(mem_ctx
);
2288 if (!PyObject_AsDn(NULL
, pydn
, NULL
, &dn
)) {
2289 talloc_free(mem_ctx
);
2292 ret
->dn
= talloc_reference(ret
, dn
);
2295 py_ret
= (PyLdbMessageObject
*)type
->tp_alloc(type
, 0);
2296 if (py_ret
== NULL
) {
2298 talloc_free(mem_ctx
);
2302 py_ret
->mem_ctx
= mem_ctx
;
2304 return (PyObject
*)py_ret
;
2307 static PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
)
2309 PyLdbMessageObject
*ret
;
2311 ret
= (PyLdbMessageObject
*)PyLdbMessage
.tp_alloc(&PyLdbMessage
, 0);
2316 ret
->mem_ctx
= talloc_new(NULL
);
2317 ret
->msg
= talloc_reference(ret
->mem_ctx
, msg
);
2318 return (PyObject
*)ret
;
2321 static PyObject
*py_ldb_msg_get_dn(PyLdbMessageObject
*self
, void *closure
)
2323 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2324 return PyLdbDn_FromDn(msg
->dn
);
2327 static int py_ldb_msg_set_dn(PyLdbMessageObject
*self
, PyObject
*value
, void *closure
)
2329 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2330 if (!PyLdbDn_Check(value
)) {
2331 PyErr_SetNone(PyExc_TypeError
);
2335 msg
->dn
= talloc_reference(msg
, PyLdbDn_AsDn(value
));
2339 static PyGetSetDef py_ldb_msg_getset
[] = {
2340 { discard_const_p(char, "dn"), (getter
)py_ldb_msg_get_dn
, (setter
)py_ldb_msg_set_dn
, NULL
},
2344 static PyObject
*py_ldb_msg_repr(PyLdbMessageObject
*self
)
2346 PyObject
*dict
= PyDict_New(), *ret
;
2347 if (PyDict_Update(dict
, (PyObject
*)self
) != 0)
2349 ret
= PyString_FromFormat("Message(%s)", PyObject_REPR(dict
));
2354 static void py_ldb_msg_dealloc(PyLdbMessageObject
*self
)
2356 talloc_free(self
->mem_ctx
);
2360 static int py_ldb_msg_compare(PyLdbMessageObject
*py_msg1
,
2361 PyLdbMessageObject
*py_msg2
)
2363 struct ldb_message
*msg1
= PyLdbMessage_AsMessage(py_msg1
),
2364 *msg2
= PyLdbMessage_AsMessage(py_msg2
);
2368 if ((msg1
->dn
!= NULL
) || (msg2
->dn
!= NULL
)) {
2369 ret
= ldb_dn_compare(msg1
->dn
, msg2
->dn
);
2375 ret
= msg1
->num_elements
- msg2
->num_elements
;
2380 for (i
= 0; i
< msg1
->num_elements
; i
++) {
2381 ret
= ldb_msg_element_compare_name(&msg1
->elements
[i
],
2382 &msg2
->elements
[i
]);
2387 ret
= ldb_msg_element_compare(&msg1
->elements
[i
],
2388 &msg2
->elements
[i
]);
2397 static PyTypeObject PyLdbMessage
= {
2398 .tp_name
= "ldb.Message",
2399 .tp_methods
= py_ldb_msg_methods
,
2400 .tp_getset
= py_ldb_msg_getset
,
2401 .tp_as_mapping
= &py_ldb_msg_mapping
,
2402 .tp_basicsize
= sizeof(PyLdbMessageObject
),
2403 .tp_dealloc
= (destructor
)py_ldb_msg_dealloc
,
2404 .tp_new
= py_ldb_msg_new
,
2405 .tp_repr
= (reprfunc
)py_ldb_msg_repr
,
2406 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2407 .tp_iter
= (getiterfunc
)py_ldb_msg_iter
,
2408 .tp_compare
= (cmpfunc
)py_ldb_msg_compare
,
2411 static PyObject
*PyLdbTree_FromTree(struct ldb_parse_tree
*tree
)
2413 PyLdbTreeObject
*ret
;
2415 ret
= (PyLdbTreeObject
*)PyLdbTree
.tp_alloc(&PyLdbTree
, 0);
2421 ret
->mem_ctx
= talloc_new(NULL
);
2422 ret
->tree
= talloc_reference(ret
->mem_ctx
, tree
);
2423 return (PyObject
*)ret
;
2426 static void py_ldb_tree_dealloc(PyLdbTreeObject
*self
)
2428 talloc_free(self
->mem_ctx
);
2432 static PyTypeObject PyLdbTree
= {
2433 .tp_name
= "ldb.Tree",
2434 .tp_basicsize
= sizeof(PyLdbTreeObject
),
2435 .tp_dealloc
= (destructor
)py_ldb_tree_dealloc
,
2436 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2440 static int py_module_search(struct ldb_module
*mod
, struct ldb_request
*req
)
2442 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2443 PyObject
*py_result
, *py_base
, *py_attrs
, *py_tree
;
2445 py_base
= PyLdbDn_FromDn(req
->op
.search
.base
);
2447 if (py_base
== NULL
)
2448 return LDB_ERR_OPERATIONS_ERROR
;
2450 py_tree
= PyLdbTree_FromTree(req
->op
.search
.tree
);
2452 if (py_tree
== NULL
)
2453 return LDB_ERR_OPERATIONS_ERROR
;
2455 if (req
->op
.search
.attrs
== NULL
) {
2459 for (len
= 0; req
->op
.search
.attrs
[len
]; len
++);
2460 py_attrs
= PyList_New(len
);
2461 for (i
= 0; i
< len
; i
++)
2462 PyList_SetItem(py_attrs
, i
, PyString_FromString(req
->op
.search
.attrs
[i
]));
2465 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "search"),
2466 discard_const_p(char, "OiOO"),
2467 py_base
, req
->op
.search
.scope
, py_tree
, py_attrs
);
2469 Py_DECREF(py_attrs
);
2473 if (py_result
== NULL
) {
2474 return LDB_ERR_PYTHON_EXCEPTION
;
2477 req
->op
.search
.res
= PyLdbResult_AsResult(NULL
, py_result
);
2478 if (req
->op
.search
.res
== NULL
) {
2479 return LDB_ERR_PYTHON_EXCEPTION
;
2482 Py_DECREF(py_result
);
2487 static int py_module_add(struct ldb_module
*mod
, struct ldb_request
*req
)
2489 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2490 PyObject
*py_result
, *py_msg
;
2492 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.add
.message
));
2494 if (py_msg
== NULL
) {
2495 return LDB_ERR_OPERATIONS_ERROR
;
2498 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "add"),
2499 discard_const_p(char, "O"),
2504 if (py_result
== NULL
) {
2505 return LDB_ERR_PYTHON_EXCEPTION
;
2508 Py_DECREF(py_result
);
2513 static int py_module_modify(struct ldb_module
*mod
, struct ldb_request
*req
)
2515 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2516 PyObject
*py_result
, *py_msg
;
2518 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.mod
.message
));
2520 if (py_msg
== NULL
) {
2521 return LDB_ERR_OPERATIONS_ERROR
;
2524 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "modify"),
2525 discard_const_p(char, "O"),
2530 if (py_result
== NULL
) {
2531 return LDB_ERR_PYTHON_EXCEPTION
;
2534 Py_DECREF(py_result
);
2539 static int py_module_del(struct ldb_module
*mod
, struct ldb_request
*req
)
2541 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2542 PyObject
*py_result
, *py_dn
;
2544 py_dn
= PyLdbDn_FromDn(req
->op
.del
.dn
);
2547 return LDB_ERR_OPERATIONS_ERROR
;
2549 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "delete"),
2550 discard_const_p(char, "O"),
2553 if (py_result
== NULL
) {
2554 return LDB_ERR_PYTHON_EXCEPTION
;
2557 Py_DECREF(py_result
);
2562 static int py_module_rename(struct ldb_module
*mod
, struct ldb_request
*req
)
2564 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2565 PyObject
*py_result
, *py_olddn
, *py_newdn
;
2567 py_olddn
= PyLdbDn_FromDn(req
->op
.rename
.olddn
);
2569 if (py_olddn
== NULL
)
2570 return LDB_ERR_OPERATIONS_ERROR
;
2572 py_newdn
= PyLdbDn_FromDn(req
->op
.rename
.newdn
);
2574 if (py_newdn
== NULL
)
2575 return LDB_ERR_OPERATIONS_ERROR
;
2577 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "rename"),
2578 discard_const_p(char, "OO"),
2579 py_olddn
, py_newdn
);
2581 Py_DECREF(py_olddn
);
2582 Py_DECREF(py_newdn
);
2584 if (py_result
== NULL
) {
2585 return LDB_ERR_PYTHON_EXCEPTION
;
2588 Py_DECREF(py_result
);
2593 static int py_module_request(struct ldb_module
*mod
, struct ldb_request
*req
)
2595 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2596 PyObject
*py_result
;
2598 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "request"),
2599 discard_const_p(char, ""));
2601 return LDB_ERR_OPERATIONS_ERROR
;
2604 static int py_module_extended(struct ldb_module
*mod
, struct ldb_request
*req
)
2606 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2607 PyObject
*py_result
;
2609 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "extended"),
2610 discard_const_p(char, ""));
2612 return LDB_ERR_OPERATIONS_ERROR
;
2615 static int py_module_start_transaction(struct ldb_module
*mod
)
2617 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2618 PyObject
*py_result
;
2620 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "start_transaction"),
2621 discard_const_p(char, ""));
2623 if (py_result
== NULL
) {
2624 return LDB_ERR_PYTHON_EXCEPTION
;
2627 Py_DECREF(py_result
);
2632 static int py_module_end_transaction(struct ldb_module
*mod
)
2634 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2635 PyObject
*py_result
;
2637 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "end_transaction"),
2638 discard_const_p(char, ""));
2640 if (py_result
== NULL
) {
2641 return LDB_ERR_PYTHON_EXCEPTION
;
2644 Py_DECREF(py_result
);
2649 static int py_module_del_transaction(struct ldb_module
*mod
)
2651 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2652 PyObject
*py_result
;
2654 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "del_transaction"),
2655 discard_const_p(char, ""));
2657 if (py_result
== NULL
) {
2658 return LDB_ERR_PYTHON_EXCEPTION
;
2661 Py_DECREF(py_result
);
2666 static int py_module_destructor(struct ldb_module
*mod
)
2668 Py_DECREF((PyObject
*)mod
->private_data
);
2672 static int py_module_init(struct ldb_module
*mod
)
2674 PyObject
*py_class
= (PyObject
*)mod
->ops
->private_data
;
2675 PyObject
*py_result
, *py_next
, *py_ldb
;
2677 py_ldb
= PyLdb_FromLdbContext(mod
->ldb
);
2680 return LDB_ERR_OPERATIONS_ERROR
;
2682 py_next
= PyLdbModule_FromModule(mod
->next
);
2684 if (py_next
== NULL
)
2685 return LDB_ERR_OPERATIONS_ERROR
;
2687 py_result
= PyObject_CallFunction(py_class
, discard_const_p(char, "OO"),
2690 if (py_result
== NULL
) {
2691 return LDB_ERR_PYTHON_EXCEPTION
;
2694 mod
->private_data
= py_result
;
2696 talloc_set_destructor(mod
, py_module_destructor
);
2698 return ldb_next_init(mod
);
2701 static PyObject
*py_register_module(PyObject
*module
, PyObject
*args
)
2704 struct ldb_module_ops
*ops
;
2707 if (!PyArg_ParseTuple(args
, "O", &input
))
2710 ops
= talloc_zero(talloc_autofree_context(), struct ldb_module_ops
);
2716 ops
->name
= talloc_strdup(ops
, PyString_AsString(PyObject_GetAttrString(input
, discard_const_p(char, "name"))));
2719 ops
->private_data
= input
;
2720 ops
->init_context
= py_module_init
;
2721 ops
->search
= py_module_search
;
2722 ops
->add
= py_module_add
;
2723 ops
->modify
= py_module_modify
;
2724 ops
->del
= py_module_del
;
2725 ops
->rename
= py_module_rename
;
2726 ops
->request
= py_module_request
;
2727 ops
->extended
= py_module_extended
;
2728 ops
->start_transaction
= py_module_start_transaction
;
2729 ops
->end_transaction
= py_module_end_transaction
;
2730 ops
->del_transaction
= py_module_del_transaction
;
2732 ret
= ldb_register_module(ops
);
2734 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2739 static PyObject
*py_timestring(PyObject
*module
, PyObject
*args
)
2741 /* most times "time_t" is a signed integer type with 32 or 64 bit:
2742 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
2746 if (!PyArg_ParseTuple(args
, "l", &t_val
))
2748 tresult
= ldb_timestring(NULL
, (time_t) t_val
);
2749 ret
= PyString_FromString(tresult
);
2750 talloc_free(tresult
);
2754 static PyObject
*py_string_to_time(PyObject
*module
, PyObject
*args
)
2757 if (!PyArg_ParseTuple(args
, "s", &str
))
2760 return PyInt_FromLong(ldb_string_to_time(str
));
2763 static PyObject
*py_valid_attr_name(PyObject
*self
, PyObject
*args
)
2766 if (!PyArg_ParseTuple(args
, "s", &name
))
2768 return PyBool_FromLong(ldb_valid_attr_name(name
));
2771 static PyMethodDef py_ldb_global_methods
[] = {
2772 { "register_module", py_register_module
, METH_VARARGS
,
2773 "S.register_module(module) -> None\n"
2774 "Register a LDB module."},
2775 { "timestring", py_timestring
, METH_VARARGS
,
2776 "S.timestring(int) -> string\n"
2777 "Generate a LDAP time string from a UNIX timestamp" },
2778 { "string_to_time", py_string_to_time
, METH_VARARGS
,
2779 "S.string_to_time(string) -> int\n"
2780 "Parse a LDAP time string into a UNIX timestamp." },
2781 { "valid_attr_name", py_valid_attr_name
, METH_VARARGS
,
2782 "S.valid_attr_name(name) -> bool\n"
2783 "Check whether the supplied name is a valid attribute name." },
2784 { "open", (PyCFunction
)py_ldb_new
, METH_VARARGS
|METH_KEYWORDS
,
2793 if (PyType_Ready(&PyLdbDn
) < 0)
2796 if (PyType_Ready(&PyLdbMessage
) < 0)
2799 if (PyType_Ready(&PyLdbMessageElement
) < 0)
2802 if (PyType_Ready(&PyLdb
) < 0)
2805 if (PyType_Ready(&PyLdbModule
) < 0)
2808 if (PyType_Ready(&PyLdbTree
) < 0)
2811 m
= Py_InitModule3("ldb", py_ldb_global_methods
,
2812 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2816 PyModule_AddObject(m
, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ
));
2817 PyModule_AddObject(m
, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP
));
2818 PyModule_AddObject(m
, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT
));
2819 PyModule_AddObject(m
, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT
));
2820 PyModule_AddObject(m
, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE
));
2821 PyModule_AddObject(m
, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL
));
2822 PyModule_AddObject(m
, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE
));
2824 PyModule_AddObject(m
, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE
));
2825 PyModule_AddObject(m
, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD
));
2826 PyModule_AddObject(m
, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE
));
2827 PyModule_AddObject(m
, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY
));
2829 PyModule_AddObject(m
, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD
));
2830 PyModule_AddObject(m
, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE
));
2831 PyModule_AddObject(m
, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE
));
2833 PyModule_AddObject(m
, "SUCCESS", PyInt_FromLong(LDB_SUCCESS
));
2834 PyModule_AddObject(m
, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR
));
2835 PyModule_AddObject(m
, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR
));
2836 PyModule_AddObject(m
, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED
));
2837 PyModule_AddObject(m
, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED
));
2838 PyModule_AddObject(m
, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE
));
2839 PyModule_AddObject(m
, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE
));
2840 PyModule_AddObject(m
, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
));
2841 PyModule_AddObject(m
, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED
));
2842 PyModule_AddObject(m
, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL
));
2843 PyModule_AddObject(m
, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED
));
2844 PyModule_AddObject(m
, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
));
2845 PyModule_AddObject(m
, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED
));
2846 PyModule_AddObject(m
, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS
));
2847 PyModule_AddObject(m
, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE
));
2848 PyModule_AddObject(m
, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
));
2849 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING
));
2850 PyModule_AddObject(m
, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION
));
2851 PyModule_AddObject(m
, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
));
2852 PyModule_AddObject(m
, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
));
2853 PyModule_AddObject(m
, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT
));
2854 PyModule_AddObject(m
, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM
));
2855 PyModule_AddObject(m
, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX
));
2856 PyModule_AddObject(m
, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
));
2857 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION
));
2858 PyModule_AddObject(m
, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS
));
2859 PyModule_AddObject(m
, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
));
2860 PyModule_AddObject(m
, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY
));
2861 PyModule_AddObject(m
, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE
));
2862 PyModule_AddObject(m
, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM
));
2863 PyModule_AddObject(m
, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT
));
2864 PyModule_AddObject(m
, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION
));
2865 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION
));
2866 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
));
2867 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN
));
2868 PyModule_AddObject(m
, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS
));
2869 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
));
2870 PyModule_AddObject(m
, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS
));
2871 PyModule_AddObject(m
, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER
));
2873 PyModule_AddObject(m
, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY
));
2874 PyModule_AddObject(m
, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC
));
2875 PyModule_AddObject(m
, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT
));
2876 PyModule_AddObject(m
, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP
));
2878 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
2880 PyExc_LdbError
= PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL
, NULL
);
2881 PyModule_AddObject(m
, "LdbError", PyExc_LdbError
);
2884 Py_INCREF(&PyLdbDn
);
2885 Py_INCREF(&PyLdbModule
);
2886 Py_INCREF(&PyLdbMessage
);
2887 Py_INCREF(&PyLdbMessageElement
);
2888 Py_INCREF(&PyLdbTree
);
2890 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PyLdb
);
2891 PyModule_AddObject(m
, "Dn", (PyObject
*)&PyLdbDn
);
2892 PyModule_AddObject(m
, "Message", (PyObject
*)&PyLdbMessage
);
2893 PyModule_AddObject(m
, "MessageElement", (PyObject
*)&PyLdbMessageElement
);
2894 PyModule_AddObject(m
, "Module", (PyObject
*)&PyLdbModule
);
2895 PyModule_AddObject(m
, "Tree", (PyObject
*)&PyLdbTree
);
2897 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));