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"
36 /* There's no Py_ssize_t in 2.4, apparently */
37 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
38 typedef int Py_ssize_t
;
39 typedef inquiry lenfunc
;
40 typedef intargfunc ssizeargfunc
;
43 #ifndef Py_RETURN_NONE
44 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
47 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
49 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
50 return; /* Python exception should already be set, just keep that */
52 PyErr_SetObject(error
,
53 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
54 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
57 static PyObject
*PyExc_LdbError
;
59 extern PyTypeObject PyLdbMessage
;
60 extern PyTypeObject PyLdbModule
;
61 extern PyTypeObject PyLdbDn
;
62 extern PyTypeObject PyLdb
;
63 staticforward PyTypeObject PyLdbMessageElement
;
64 extern PyTypeObject PyLdbTree
;
66 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
);
68 static PyObject
*PyObject_FromLdbValue(struct ldb_context
*ldb_ctx
,
69 struct ldb_message_element
*el
,
72 struct ldb_val new_val
;
73 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
78 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
86 * Create a Python object from a ldb_result.
88 * @param result LDB result to convert
89 * @return Python object with converted result (a list object)
91 static PyObject
*PyLdbResult_FromResult(struct ldb_result
*result
)
98 ret
= PyList_New(result
->count
);
99 for (i
= 0; i
< result
->count
; i
++) {
100 PyList_SetItem(ret
, i
, PyLdbMessage_FromMessage(result
->msgs
[i
]));
106 * Create a LDB Result from a Python object.
107 * If conversion fails, NULL will be returned and a Python exception set.
109 * @param mem_ctx Memory context in which to allocate the LDB Result
110 * @param obj Python object to convert
111 * @return a ldb_result, or NULL if the conversion failed
113 static struct ldb_result
*PyLdbResult_AsResult(TALLOC_CTX
*mem_ctx
,
116 struct ldb_result
*res
;
122 res
= talloc_zero(mem_ctx
, struct ldb_result
);
123 res
->count
= PyList_Size(obj
);
124 res
->msgs
= talloc_array(res
, struct ldb_message
*, res
->count
);
125 for (i
= 0; i
< res
->count
; i
++) {
126 PyObject
*item
= PyList_GetItem(obj
, i
);
127 res
->msgs
[i
] = PyLdbMessage_AsMessage(item
);
132 static PyObject
*py_ldb_dn_validate(PyLdbDnObject
*self
)
134 return PyBool_FromLong(ldb_dn_validate(self
->dn
));
137 static PyObject
*py_ldb_dn_is_valid(PyLdbDnObject
*self
)
139 return PyBool_FromLong(ldb_dn_is_valid(self
->dn
));
142 static PyObject
*py_ldb_dn_is_special(PyLdbDnObject
*self
)
144 return PyBool_FromLong(ldb_dn_is_special(self
->dn
));
147 static PyObject
*py_ldb_dn_is_null(PyLdbDnObject
*self
)
149 return PyBool_FromLong(ldb_dn_is_null(self
->dn
));
152 static PyObject
*py_ldb_dn_get_casefold(PyLdbDnObject
*self
)
154 return PyString_FromString(ldb_dn_get_casefold(self
->dn
));
157 static PyObject
*py_ldb_dn_get_linearized(PyLdbDnObject
*self
)
159 return PyString_FromString(ldb_dn_get_linearized(self
->dn
));
162 static PyObject
*py_ldb_dn_canonical_str(PyLdbDnObject
*self
)
164 return PyString_FromString(ldb_dn_canonical_string(self
->dn
, self
->dn
));
167 static PyObject
*py_ldb_dn_canonical_ex_str(PyLdbDnObject
*self
)
169 return PyString_FromString(ldb_dn_canonical_ex_string(self
->dn
, self
->dn
));
172 static PyObject
*py_ldb_dn_repr(PyLdbDnObject
*self
)
174 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self
->dn
))));
177 static PyObject
*py_ldb_dn_check_special(PyLdbDnObject
*self
, PyObject
*args
)
181 if (!PyArg_ParseTuple(args
, "s", &name
))
184 return ldb_dn_check_special(self
->dn
, name
)?Py_True
:Py_False
;
187 static int py_ldb_dn_compare(PyLdbDnObject
*dn1
, PyLdbDnObject
*dn2
)
190 ret
= ldb_dn_compare(dn1
->dn
, dn2
->dn
);
191 if (ret
< 0) ret
= -1;
192 if (ret
> 0) ret
= 1;
196 static PyObject
*py_ldb_dn_get_parent(PyLdbDnObject
*self
)
198 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
);
199 struct ldb_dn
*parent
;
200 PyLdbDnObject
*py_ret
;
201 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
203 parent
= ldb_dn_get_parent(mem_ctx
, dn
);
204 if (parent
== NULL
) {
205 talloc_free(mem_ctx
);
209 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
210 if (py_ret
== NULL
) {
212 talloc_free(mem_ctx
);
215 py_ret
->mem_ctx
= mem_ctx
;
217 return (PyObject
*)py_ret
;
220 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
222 static PyObject
*py_ldb_dn_add_child(PyLdbDnObject
*self
, PyObject
*args
)
225 struct ldb_dn
*dn
, *other
;
226 if (!PyArg_ParseTuple(args
, "O", &py_other
))
229 dn
= PyLdbDn_AsDn((PyObject
*)self
);
231 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
234 return ldb_dn_add_child(dn
, other
)?Py_True
:Py_False
;
237 static PyObject
*py_ldb_dn_add_base(PyLdbDnObject
*self
, PyObject
*args
)
240 struct ldb_dn
*other
, *dn
;
241 if (!PyArg_ParseTuple(args
, "O", &py_other
))
244 dn
= PyLdbDn_AsDn((PyObject
*)self
);
246 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
249 return ldb_dn_add_base(dn
, other
)?Py_True
:Py_False
;
252 static PyMethodDef py_ldb_dn_methods
[] = {
253 { "validate", (PyCFunction
)py_ldb_dn_validate
, METH_NOARGS
,
254 "S.validate() -> bool\n"
255 "Validate DN is correct." },
256 { "is_valid", (PyCFunction
)py_ldb_dn_is_valid
, METH_NOARGS
,
257 "S.is_valid() -> bool\n" },
258 { "is_special", (PyCFunction
)py_ldb_dn_is_special
, METH_NOARGS
,
259 "S.is_special() -> bool\n"
260 "Check whether this is a special LDB DN." },
261 { "is_null", (PyCFunction
)py_ldb_dn_is_null
, METH_NOARGS
,
262 "Check whether this is a null DN." },
263 { "get_casefold", (PyCFunction
)py_ldb_dn_get_casefold
, METH_NOARGS
,
265 { "get_linearized", (PyCFunction
)py_ldb_dn_get_linearized
, METH_NOARGS
,
267 { "canonical_str", (PyCFunction
)py_ldb_dn_canonical_str
, METH_NOARGS
,
268 "S.canonical_str() -> string\n"
269 "Canonical version of this DN (like a posix path)." },
270 { "canonical_ex_str", (PyCFunction
)py_ldb_dn_canonical_ex_str
, METH_NOARGS
,
271 "S.canonical_ex_str() -> string\n"
272 "Canonical version of this DN (like a posix path, with terminating newline)." },
273 { "parent", (PyCFunction
)py_ldb_dn_get_parent
, METH_NOARGS
,
275 "Get the parent for this DN." },
276 { "add_child", (PyCFunction
)py_ldb_dn_add_child
, METH_VARARGS
,
277 "S.add_child(dn) -> None\n"
278 "Add a child DN to this DN." },
279 { "add_base", (PyCFunction
)py_ldb_dn_add_base
, METH_VARARGS
,
280 "S.add_base(dn) -> None\n"
281 "Add a base DN to this DN." },
282 { "check_special", (PyCFunction
)py_ldb_dn_check_special
, METH_VARARGS
,
283 "S.check_special(name) -> bool\n\n"
284 "Check if name is a special DN name"},
288 static Py_ssize_t
py_ldb_dn_len(PyLdbDnObject
*self
)
290 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject
*)self
));
293 static PyObject
*py_ldb_dn_concat(PyLdbDnObject
*self
, PyObject
*py_other
)
295 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
),
297 PyLdbDnObject
*py_ret
;
299 if (!PyObject_AsDn(NULL
, py_other
, NULL
, &other
))
302 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
303 if (py_ret
== NULL
) {
307 py_ret
->mem_ctx
= talloc_new(NULL
);
308 py_ret
->dn
= ldb_dn_copy(py_ret
->mem_ctx
, dn
);
309 ldb_dn_add_child(py_ret
->dn
, other
);
310 return (PyObject
*)py_ret
;
313 static PySequenceMethods py_ldb_dn_seq
= {
314 .sq_length
= (lenfunc
)py_ldb_dn_len
,
315 .sq_concat
= (binaryfunc
)py_ldb_dn_concat
,
318 static PyObject
*py_ldb_dn_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
323 struct ldb_context
*ldb_ctx
;
325 PyLdbDnObject
*py_ret
;
326 const char * const kwnames
[] = { "ldb", "dn", NULL
};
328 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Os",
329 discard_const_p(char *, kwnames
),
333 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
335 mem_ctx
= talloc_new(NULL
);
336 if (mem_ctx
== NULL
) {
341 ret
= ldb_dn_new(mem_ctx
, ldb_ctx
, str
);
343 if (ret
== NULL
|| !ldb_dn_validate(ret
)) {
344 talloc_free(mem_ctx
);
345 PyErr_SetString(PyExc_ValueError
, "unable to parse dn string");
349 py_ret
= (PyLdbDnObject
*)type
->tp_alloc(type
, 0);
351 talloc_free(mem_ctx
);
355 py_ret
->mem_ctx
= mem_ctx
;
357 return (PyObject
*)py_ret
;
360 static void py_ldb_dn_dealloc(PyLdbDnObject
*self
)
362 talloc_free(self
->mem_ctx
);
366 PyTypeObject PyLdbDn
= {
368 .tp_methods
= py_ldb_dn_methods
,
369 .tp_str
= (reprfunc
)py_ldb_dn_get_linearized
,
370 .tp_repr
= (reprfunc
)py_ldb_dn_repr
,
371 .tp_compare
= (cmpfunc
)py_ldb_dn_compare
,
372 .tp_as_sequence
= &py_ldb_dn_seq
,
373 .tp_doc
= "A LDB distinguished name.",
374 .tp_new
= py_ldb_dn_new
,
375 .tp_dealloc
= (destructor
)py_ldb_dn_dealloc
,
376 .tp_basicsize
= sizeof(PyLdbObject
),
377 .tp_flags
= Py_TPFLAGS_DEFAULT
,
381 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3, 0);
382 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
)
384 PyObject
*fn
= (PyObject
*)context
;
385 PyObject_CallFunction(fn
, discard_const_p(char, "(i,O)"), level
, PyString_FromFormatV(fmt
, ap
));
388 static PyObject
*py_ldb_set_debug(PyLdbObject
*self
, PyObject
*args
)
392 if (!PyArg_ParseTuple(args
, "O", &cb
))
396 /* FIXME: Where do we DECREF cb ? */
397 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_set_debug(self
->ldb_ctx
, py_ldb_debug
, cb
), PyLdb_AsLdbContext(self
));
402 static PyObject
*py_ldb_set_create_perms(PyTypeObject
*self
, PyObject
*args
)
405 if (!PyArg_ParseTuple(args
, "I", &perms
))
408 ldb_set_create_perms(PyLdb_AsLdbContext(self
), perms
);
413 static PyObject
*py_ldb_set_modules_dir(PyTypeObject
*self
, PyObject
*args
)
416 if (!PyArg_ParseTuple(args
, "s", &modules_dir
))
419 ldb_set_modules_dir(PyLdb_AsLdbContext(self
), modules_dir
);
424 static PyObject
*py_ldb_transaction_start(PyLdbObject
*self
)
426 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_start(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
430 static PyObject
*py_ldb_transaction_commit(PyLdbObject
*self
)
432 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
436 static PyObject
*py_ldb_transaction_prepare_commit(PyLdbObject
*self
)
438 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
442 static PyObject
*py_ldb_transaction_cancel(PyLdbObject
*self
)
444 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_cancel(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
448 static PyObject
*py_ldb_setup_wellknown_attributes(PyLdbObject
*self
)
450 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
454 static PyObject
*py_ldb_repr(PyLdbObject
*self
)
456 return PyString_FromFormat("<ldb connection>");
459 static PyObject
*py_ldb_get_root_basedn(PyLdbObject
*self
)
461 struct ldb_dn
*dn
= ldb_get_root_basedn(PyLdb_AsLdbContext(self
));
464 return PyLdbDn_FromDn(dn
);
468 static PyObject
*py_ldb_get_schema_basedn(PyLdbObject
*self
)
470 struct ldb_dn
*dn
= ldb_get_schema_basedn(PyLdb_AsLdbContext(self
));
473 return PyLdbDn_FromDn(dn
);
476 static PyObject
*py_ldb_get_config_basedn(PyLdbObject
*self
)
478 struct ldb_dn
*dn
= ldb_get_config_basedn(PyLdb_AsLdbContext(self
));
481 return PyLdbDn_FromDn(dn
);
484 static PyObject
*py_ldb_get_default_basedn(PyLdbObject
*self
)
486 struct ldb_dn
*dn
= ldb_get_default_basedn(PyLdb_AsLdbContext(self
));
489 return PyLdbDn_FromDn(dn
);
492 static const char **PyList_AsStringList(TALLOC_CTX
*mem_ctx
, PyObject
*list
,
493 const char *paramname
)
497 if (!PyList_Check(list
)) {
498 PyErr_Format(PyExc_TypeError
, "%s is not a list", paramname
);
501 ret
= talloc_array(NULL
, const char *, PyList_Size(list
)+1);
502 for (i
= 0; i
< PyList_Size(list
); i
++) {
503 PyObject
*item
= PyList_GetItem(list
, i
);
504 if (!PyString_Check(item
)) {
505 PyErr_Format(PyExc_TypeError
, "%s should be strings", paramname
);
508 ret
[i
] = talloc_strndup(ret
, PyString_AsString(item
),
509 PyString_Size(item
));
515 static int py_ldb_init(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
517 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
519 PyObject
*py_options
= Py_None
;
520 const char **options
;
523 struct ldb_context
*ldb
;
525 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO:Ldb.__init__",
526 discard_const_p(char *, kwnames
),
527 &url
, &flags
, &py_options
))
530 ldb
= PyLdb_AsLdbContext(self
);
532 if (py_options
== Py_None
) {
535 options
= PyList_AsStringList(ldb
, py_options
, "options");
541 ret
= ldb_connect(ldb
, url
, flags
, options
);
542 if (ret
!= LDB_SUCCESS
) {
543 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb
);
548 talloc_free(options
);
552 static PyObject
*py_ldb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
555 struct ldb_context
*ldb
;
556 ret
= (PyLdbObject
*)type
->tp_alloc(type
, 0);
561 ret
->mem_ctx
= talloc_new(NULL
);
562 ldb
= ldb_init(ret
->mem_ctx
, NULL
);
570 return (PyObject
*)ret
;
573 static PyObject
*py_ldb_connect(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
577 PyObject
*py_options
= Py_None
;
579 const char **options
;
580 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
582 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO",
583 discard_const_p(char *, kwnames
),
584 &url
, &flags
, &py_options
))
587 if (py_options
== Py_None
) {
590 options
= PyList_AsStringList(NULL
, py_options
, "options");
595 ret
= ldb_connect(PyLdb_AsLdbContext(self
), url
, flags
, options
);
596 talloc_free(options
);
598 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
603 static PyObject
*py_ldb_modify(PyLdbObject
*self
, PyObject
*args
)
606 PyObject
*py_controls
= Py_None
;
607 struct ldb_context
*ldb_ctx
;
608 struct ldb_request
*req
;
609 struct ldb_control
**parsed_controls
;
610 struct ldb_message
*msg
;
614 if (!PyArg_ParseTuple(args
, "O|O", &py_msg
, &py_controls
))
617 mem_ctx
= talloc_new(NULL
);
618 if (mem_ctx
== NULL
) {
622 ldb_ctx
= PyLdb_AsLdbContext(self
);
624 if (py_controls
== Py_None
) {
625 parsed_controls
= NULL
;
627 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
628 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
629 talloc_free(controls
);
632 if (!PyLdbMessage_Check(py_msg
)) {
633 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message");
634 talloc_free(mem_ctx
);
637 msg
= PyLdbMessage_AsMessage(py_msg
);
639 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
640 if (ret
!= LDB_SUCCESS
) {
641 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
642 talloc_free(mem_ctx
);
646 ret
= ldb_build_mod_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
647 NULL
, ldb_op_default_callback
, NULL
);
648 if (ret
!= LDB_SUCCESS
) {
649 PyErr_SetString(PyExc_TypeError
, "failed to build request");
650 talloc_free(mem_ctx
);
654 /* do request and autostart a transaction */
655 /* Then let's LDB handle the message error in case of pb as they are meaningful */
657 ret
= ldb_transaction_start(ldb_ctx
);
658 if (ret
!= LDB_SUCCESS
) {
659 talloc_free(mem_ctx
);
660 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
663 ret
= ldb_request(ldb_ctx
, req
);
664 if (ret
== LDB_SUCCESS
) {
665 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
668 if (ret
== LDB_SUCCESS
) {
669 ret
= ldb_transaction_commit(ldb_ctx
);
671 ldb_transaction_cancel(ldb_ctx
);
672 if (ldb_ctx
->err_string
== NULL
) {
673 /* no error string was setup by the backend */
674 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
678 talloc_free(mem_ctx
);
679 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
686 * Obtain a ldb message from a Python Dictionary object.
688 * @param mem_ctx Memory context
689 * @param py_obj Python Dictionary object
690 * @param ldb_ctx LDB context
691 * @param mod_flags Flags to be set on every message element
692 * @return ldb_message on success or NULL on failure
694 static struct ldb_message
*PyDict_AsMessage(TALLOC_CTX
*mem_ctx
,
696 struct ldb_context
*ldb_ctx
,
697 unsigned int mod_flags
)
699 struct ldb_message
*msg
;
700 unsigned int msg_pos
= 0;
701 Py_ssize_t dict_pos
= 0;
702 PyObject
*key
, *value
;
703 struct ldb_message_element
*msg_el
;
704 PyObject
*dn_value
= PyDict_GetItemString(py_obj
, "dn");
706 msg
= ldb_msg_new(mem_ctx
);
707 msg
->elements
= talloc_zero_array(msg
, struct ldb_message_element
, PyDict_Size(py_obj
));
710 if (!PyObject_AsDn(msg
, dn_value
, ldb_ctx
, &msg
->dn
)) {
711 PyErr_SetString(PyExc_TypeError
, "unable to import dn object");
714 if (msg
->dn
== NULL
) {
715 PyErr_SetString(PyExc_TypeError
, "dn set but not found");
719 PyErr_SetString(PyExc_TypeError
, "no dn set");
723 while (PyDict_Next(py_obj
, &dict_pos
, &key
, &value
)) {
724 char *key_str
= PyString_AsString(key
);
725 if (strcmp(key_str
, "dn") != 0) {
726 msg_el
= PyObject_AsMessageElement(msg
->elements
, value
,
728 if (msg_el
== NULL
) {
729 PyErr_SetString(PyExc_TypeError
, "unable to import element");
732 memcpy(&msg
->elements
[msg_pos
], msg_el
, sizeof(*msg_el
));
737 msg
->num_elements
= msg_pos
;
742 static PyObject
*py_ldb_add(PyLdbObject
*self
, PyObject
*args
)
746 struct ldb_context
*ldb_ctx
;
747 struct ldb_request
*req
;
748 struct ldb_message
*msg
= NULL
;
749 PyObject
*py_controls
= Py_None
;
751 struct ldb_control
**parsed_controls
;
753 if (!PyArg_ParseTuple(args
, "O|O", &py_obj
, &py_controls
))
756 mem_ctx
= talloc_new(NULL
);
757 if (mem_ctx
== NULL
) {
761 ldb_ctx
= PyLdb_AsLdbContext(self
);
763 if (py_controls
== Py_None
) {
764 parsed_controls
= NULL
;
766 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
767 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
768 talloc_free(controls
);
771 if (PyLdbMessage_Check(py_obj
)) {
772 msg
= PyLdbMessage_AsMessage(py_obj
);
773 } else if (PyDict_Check(py_obj
)) {
774 msg
= PyDict_AsMessage(mem_ctx
, py_obj
, ldb_ctx
, LDB_FLAG_MOD_ADD
);
776 PyErr_SetString(PyExc_TypeError
,
777 "Dictionary or LdbMessage object expected!");
781 /* we should have a PyErr already set */
782 talloc_free(mem_ctx
);
786 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
787 if (ret
!= LDB_SUCCESS
) {
788 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
789 talloc_free(mem_ctx
);
793 ret
= ldb_build_add_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
794 NULL
, ldb_op_default_callback
, NULL
);
795 if (ret
!= LDB_SUCCESS
) {
796 PyErr_SetString(PyExc_TypeError
, "failed to build request");
797 talloc_free(mem_ctx
);
801 /* do request and autostart a transaction */
802 /* Then let's LDB handle the message error in case of pb as they are meaningful */
804 ret
= ldb_transaction_start(ldb_ctx
);
805 if (ret
!= LDB_SUCCESS
) {
806 talloc_free(mem_ctx
);
807 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
810 ret
= ldb_request(ldb_ctx
, req
);
811 if (ret
== LDB_SUCCESS
) {
812 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
815 if (ret
== LDB_SUCCESS
) {
816 ret
= ldb_transaction_commit(ldb_ctx
);
818 ldb_transaction_cancel(ldb_ctx
);
819 if (ldb_ctx
->err_string
== NULL
) {
820 /* no error string was setup by the backend */
821 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
825 talloc_free(mem_ctx
);
826 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
831 static PyObject
*py_ldb_delete(PyLdbObject
*self
, PyObject
*args
)
836 struct ldb_context
*ldb_ctx
;
837 struct ldb_request
*req
;
838 PyObject
*py_controls
= Py_None
;
840 struct ldb_control
**parsed_controls
;
842 if (!PyArg_ParseTuple(args
, "O|O", &py_dn
, &py_controls
))
845 mem_ctx
= talloc_new(NULL
);
846 if (mem_ctx
== NULL
) {
850 ldb_ctx
= PyLdb_AsLdbContext(self
);
852 if (py_controls
== Py_None
) {
853 parsed_controls
= NULL
;
855 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
856 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
857 talloc_free(controls
);
860 if (!PyObject_AsDn(mem_ctx
, py_dn
, ldb_ctx
, &dn
)) {
861 talloc_free(mem_ctx
);
865 ret
= ldb_build_del_req(&req
, ldb_ctx
, mem_ctx
, dn
, parsed_controls
,
866 NULL
, ldb_op_default_callback
, NULL
);
867 if (ret
!= LDB_SUCCESS
) {
868 PyErr_SetString(PyExc_TypeError
, "failed to build request");
869 talloc_free(mem_ctx
);
873 /* do request and autostart a transaction */
874 /* Then let's LDB handle the message error in case of pb as they are meaningful */
876 ret
= ldb_transaction_start(ldb_ctx
);
877 if (ret
!= LDB_SUCCESS
) {
878 talloc_free(mem_ctx
);
879 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
882 ret
= ldb_request(ldb_ctx
, req
);
883 if (ret
== LDB_SUCCESS
) {
884 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
887 if (ret
== LDB_SUCCESS
) {
888 ret
= ldb_transaction_commit(ldb_ctx
);
890 ldb_transaction_cancel(ldb_ctx
);
891 if (ldb_ctx
->err_string
== NULL
) {
892 /* no error string was setup by the backend */
893 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
897 talloc_free(mem_ctx
);
898 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
903 static PyObject
*py_ldb_rename(PyLdbObject
*self
, PyObject
*args
)
905 PyObject
*py_dn1
, *py_dn2
;
906 struct ldb_dn
*dn1
, *dn2
;
908 struct ldb_context
*ldb
;
910 PyObject
*py_controls
= Py_None
;
911 struct ldb_control
**parsed_controls
;
912 struct ldb_context
*ldb_ctx
;
913 struct ldb_request
*req
;
915 ldb_ctx
= PyLdb_AsLdbContext(self
);
917 if (!PyArg_ParseTuple(args
, "OO|O", &py_dn1
, &py_dn2
, &py_controls
))
921 mem_ctx
= talloc_new(NULL
);
922 if (mem_ctx
== NULL
) {
926 ldb
= PyLdb_AsLdbContext(self
);
928 if (py_controls
== Py_None
) {
929 parsed_controls
= NULL
;
931 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
932 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
933 talloc_free(controls
);
937 if (!PyObject_AsDn(mem_ctx
, py_dn1
, ldb
, &dn1
)) {
938 talloc_free(mem_ctx
);
942 if (!PyObject_AsDn(mem_ctx
, py_dn2
, ldb
, &dn2
)) {
943 talloc_free(mem_ctx
);
947 ret
= ldb_build_rename_req(&req
, ldb_ctx
, mem_ctx
, dn1
, dn2
, parsed_controls
,
948 NULL
, ldb_op_default_callback
, NULL
);
949 if (ret
!= LDB_SUCCESS
) {
950 PyErr_SetString(PyExc_TypeError
, "failed to build request");
951 talloc_free(mem_ctx
);
955 /* do request and autostart a transaction */
956 /* Then let's LDB handle the message error in case of pb as they are meaningful */
958 ret
= ldb_transaction_start(ldb_ctx
);
959 if (ret
!= LDB_SUCCESS
) {
960 talloc_free(mem_ctx
);
961 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
964 ret
= ldb_request(ldb_ctx
, req
);
965 if (ret
== LDB_SUCCESS
) {
966 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
969 if (ret
== LDB_SUCCESS
) {
970 ret
= ldb_transaction_commit(ldb_ctx
);
972 ldb_transaction_cancel(ldb_ctx
);
973 if (ldb_ctx
->err_string
== NULL
) {
974 /* no error string was setup by the backend */
975 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
979 talloc_free(mem_ctx
);
980 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
985 static PyObject
*py_ldb_schema_attribute_remove(PyLdbObject
*self
, PyObject
*args
)
988 if (!PyArg_ParseTuple(args
, "s", &name
))
991 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self
), name
);
996 static PyObject
*py_ldb_schema_attribute_add(PyLdbObject
*self
, PyObject
*args
)
998 char *attribute
, *syntax
;
1001 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
1004 ret
= ldb_schema_attribute_add(PyLdb_AsLdbContext(self
), attribute
, flags
, syntax
);
1006 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
1011 static PyObject
*ldb_ldif_to_pyobject(struct ldb_ldif
*ldif
)
1016 /* We don't want this attached to the 'ldb' any more */
1017 return Py_BuildValue(discard_const_p(char, "(iO)"),
1019 PyLdbMessage_FromMessage(ldif
->msg
));
1024 static PyObject
*py_ldb_write_ldif(PyLdbObject
*self
, PyObject
*args
)
1028 struct ldb_ldif ldif
;
1031 TALLOC_CTX
*mem_ctx
;
1033 if (!PyArg_ParseTuple(args
, "Oi", &py_msg
, &changetype
))
1036 if (!PyLdbMessage_Check(py_msg
)) {
1037 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for msg");
1041 ldif
.msg
= PyLdbMessage_AsMessage(py_msg
);
1042 ldif
.changetype
= changetype
;
1044 mem_ctx
= talloc_new(NULL
);
1046 string
= ldb_ldif_write_string(PyLdb_AsLdbContext(self
), mem_ctx
, &ldif
);
1048 PyErr_SetString(PyExc_KeyError
, "Failed to generate LDIF");
1052 ret
= PyString_FromString(string
);
1054 talloc_free(mem_ctx
);
1059 static PyObject
*py_ldb_parse_ldif(PyLdbObject
*self
, PyObject
*args
)
1062 struct ldb_ldif
*ldif
;
1065 TALLOC_CTX
*mem_ctx
;
1067 if (!PyArg_ParseTuple(args
, "s", &s
))
1070 mem_ctx
= talloc_new(NULL
);
1075 list
= PyList_New(0);
1076 while (s
&& *s
!= '\0') {
1077 ldif
= ldb_ldif_read_string(self
->ldb_ctx
, &s
);
1078 talloc_steal(mem_ctx
, ldif
);
1080 PyList_Append(list
, ldb_ldif_to_pyobject(ldif
));
1082 PyErr_SetString(PyExc_ValueError
, "unable to parse ldif string");
1083 talloc_free(mem_ctx
);
1087 talloc_free(mem_ctx
); /* The pyobject already has a reference to the things it needs */
1088 return PyObject_GetIter(list
);
1091 static PyObject
*py_ldb_msg_diff(PyLdbObject
*self
, PyObject
*args
)
1094 PyObject
*py_msg_old
;
1095 PyObject
*py_msg_new
;
1096 struct ldb_message
*diff
;
1097 struct ldb_context
*ldb
;
1100 if (!PyArg_ParseTuple(args
, "OO", &py_msg_old
, &py_msg_new
))
1103 if (!PyLdbMessage_Check(py_msg_old
)) {
1104 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for old message");
1108 if (!PyLdbMessage_Check(py_msg_new
)) {
1109 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for new message");
1113 ldb
= PyLdb_AsLdbContext(self
);
1114 ldb_ret
= ldb_msg_difference(ldb
, ldb
,
1115 PyLdbMessage_AsMessage(py_msg_old
),
1116 PyLdbMessage_AsMessage(py_msg_new
),
1118 if (ldb_ret
!= LDB_SUCCESS
) {
1119 PyErr_SetString(PyExc_RuntimeError
, "Failed to generate the Ldb Message diff");
1123 py_ret
= PyLdbMessage_FromMessage(diff
);
1125 talloc_unlink(ldb
, diff
);
1130 static PyObject
*py_ldb_schema_format_value(PyLdbObject
*self
, PyObject
*args
)
1132 const struct ldb_schema_attribute
*a
;
1133 struct ldb_val old_val
;
1134 struct ldb_val new_val
;
1135 TALLOC_CTX
*mem_ctx
;
1140 if (!PyArg_ParseTuple(args
, "sO", &element_name
, &val
))
1143 mem_ctx
= talloc_new(NULL
);
1145 old_val
.data
= (uint8_t *)PyString_AsString(val
);
1146 old_val
.length
= PyString_Size(val
);
1148 a
= ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self
), element_name
);
1154 if (a
->syntax
->ldif_write_fn(PyLdb_AsLdbContext(self
), mem_ctx
, &old_val
, &new_val
) != 0) {
1155 talloc_free(mem_ctx
);
1159 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
1161 talloc_free(mem_ctx
);
1166 static PyObject
*py_ldb_search(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1168 PyObject
*py_base
= Py_None
;
1169 int scope
= LDB_SCOPE_DEFAULT
;
1171 PyObject
*py_attrs
= Py_None
;
1172 PyObject
*py_controls
= Py_None
;
1173 const char * const kwnames
[] = { "base", "scope", "expression", "attrs", "controls", NULL
};
1175 struct ldb_result
*res
;
1176 struct ldb_request
*req
;
1178 struct ldb_context
*ldb_ctx
;
1179 struct ldb_control
**parsed_controls
;
1180 struct ldb_dn
*base
;
1182 TALLOC_CTX
*mem_ctx
;
1184 /* type "int" rather than "enum" for "scope" is intentional */
1185 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OizOO",
1186 discard_const_p(char *, kwnames
),
1187 &py_base
, &scope
, &expr
, &py_attrs
, &py_controls
))
1191 mem_ctx
= talloc_new(NULL
);
1192 if (mem_ctx
== NULL
) {
1196 ldb_ctx
= PyLdb_AsLdbContext(self
);
1198 if (py_attrs
== Py_None
) {
1201 attrs
= PyList_AsStringList(mem_ctx
, py_attrs
, "attrs");
1202 if (attrs
== NULL
) {
1203 talloc_free(mem_ctx
);
1208 if (py_base
== Py_None
) {
1209 base
= ldb_get_default_basedn(ldb_ctx
);
1211 if (!PyObject_AsDn(ldb_ctx
, py_base
, ldb_ctx
, &base
)) {
1217 if (py_controls
== Py_None
) {
1218 parsed_controls
= NULL
;
1220 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1221 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1222 talloc_free(controls
);
1225 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1228 talloc_free(mem_ctx
);
1232 ret
= ldb_build_search_req(&req
, ldb_ctx
, mem_ctx
,
1239 ldb_search_default_callback
,
1242 if (ret
!= LDB_SUCCESS
) {
1243 talloc_free(mem_ctx
);
1244 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1248 talloc_steal(req
, attrs
);
1250 ret
= ldb_request(ldb_ctx
, req
);
1252 if (ret
== LDB_SUCCESS
) {
1253 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1256 if (ret
!= LDB_SUCCESS
) {
1257 talloc_free(mem_ctx
);
1258 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1262 py_ret
= PyLdbResult_FromResult(res
);
1264 talloc_free(mem_ctx
);
1269 static PyObject
*py_ldb_get_opaque(PyLdbObject
*self
, PyObject
*args
)
1274 if (!PyArg_ParseTuple(args
, "s", &name
))
1277 data
= ldb_get_opaque(PyLdb_AsLdbContext(self
), name
);
1282 /* FIXME: More interpretation */
1287 static PyObject
*py_ldb_set_opaque(PyLdbObject
*self
, PyObject
*args
)
1292 if (!PyArg_ParseTuple(args
, "sO", &name
, &data
))
1295 /* FIXME: More interpretation */
1297 ldb_set_opaque(PyLdb_AsLdbContext(self
), name
, data
);
1302 static PyObject
*py_ldb_modules(PyLdbObject
*self
)
1304 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1305 PyObject
*ret
= PyList_New(0);
1306 struct ldb_module
*mod
;
1308 for (mod
= ldb
->modules
; mod
; mod
= mod
->next
) {
1309 PyList_Append(ret
, PyLdbModule_FromModule(mod
));
1315 static PyObject
*py_ldb_sequence_number(PyLdbObject
*self
, PyObject
*args
)
1317 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1321 if (!PyArg_ParseTuple(args
, "i", &type
))
1324 /* FIXME: More interpretation */
1326 ret
= ldb_sequence_number(ldb
, type
, &value
);
1328 if (ret
!= LDB_SUCCESS
) {
1329 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
1332 return PyLong_FromLongLong(value
);
1334 static PyMethodDef py_ldb_methods
[] = {
1335 { "set_debug", (PyCFunction
)py_ldb_set_debug
, METH_VARARGS
,
1336 "S.set_debug(callback) -> None\n"
1337 "Set callback for LDB debug messages.\n"
1338 "The callback should accept a debug level and debug text." },
1339 { "set_create_perms", (PyCFunction
)py_ldb_set_create_perms
, METH_VARARGS
,
1340 "S.set_create_perms(mode) -> None\n"
1341 "Set mode to use when creating new LDB files." },
1342 { "set_modules_dir", (PyCFunction
)py_ldb_set_modules_dir
, METH_VARARGS
,
1343 "S.set_modules_dir(path) -> None\n"
1344 "Set path LDB should search for modules" },
1345 { "transaction_start", (PyCFunction
)py_ldb_transaction_start
, METH_NOARGS
,
1346 "S.transaction_start() -> None\n"
1347 "Start a new transaction." },
1348 { "transaction_prepare_commit", (PyCFunction
)py_ldb_transaction_prepare_commit
, METH_NOARGS
,
1349 "S.transaction_prepare_commit() -> None\n"
1350 "prepare to commit a new transaction (2-stage commit)." },
1351 { "transaction_commit", (PyCFunction
)py_ldb_transaction_commit
, METH_NOARGS
,
1352 "S.transaction_commit() -> None\n"
1353 "commit a new transaction." },
1354 { "transaction_cancel", (PyCFunction
)py_ldb_transaction_cancel
, METH_NOARGS
,
1355 "S.transaction_cancel() -> None\n"
1356 "cancel a new transaction." },
1357 { "setup_wellknown_attributes", (PyCFunction
)py_ldb_setup_wellknown_attributes
, METH_NOARGS
,
1359 { "get_root_basedn", (PyCFunction
)py_ldb_get_root_basedn
, METH_NOARGS
,
1361 { "get_schema_basedn", (PyCFunction
)py_ldb_get_schema_basedn
, METH_NOARGS
,
1363 { "get_default_basedn", (PyCFunction
)py_ldb_get_default_basedn
, METH_NOARGS
,
1365 { "get_config_basedn", (PyCFunction
)py_ldb_get_config_basedn
, METH_NOARGS
,
1367 { "connect", (PyCFunction
)py_ldb_connect
, METH_VARARGS
|METH_KEYWORDS
,
1368 "S.connect(url, flags=0, options=None) -> None\n"
1369 "Connect to a LDB URL." },
1370 { "modify", (PyCFunction
)py_ldb_modify
, METH_VARARGS
,
1371 "S.modify(message) -> None\n"
1372 "Modify an entry." },
1373 { "add", (PyCFunction
)py_ldb_add
, METH_VARARGS
,
1374 "S.add(message) -> None\n"
1376 { "delete", (PyCFunction
)py_ldb_delete
, METH_VARARGS
,
1377 "S.delete(dn) -> None\n"
1378 "Remove an entry." },
1379 { "rename", (PyCFunction
)py_ldb_rename
, METH_VARARGS
,
1380 "S.rename(old_dn, new_dn) -> None\n"
1381 "Rename an entry." },
1382 { "search", (PyCFunction
)py_ldb_search
, METH_VARARGS
|METH_KEYWORDS
,
1383 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1384 "Search in a database.\n"
1386 ":param base: Optional base DN to search\n"
1387 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1388 ":param expression: Optional search expression\n"
1389 ":param attrs: Attributes to return (defaults to all)\n"
1390 ":param controls: Optional list of controls\n"
1391 ":return: Iterator over Message objects\n"
1393 { "schema_attribute_remove", (PyCFunction
)py_ldb_schema_attribute_remove
, METH_VARARGS
,
1395 { "schema_attribute_add", (PyCFunction
)py_ldb_schema_attribute_add
, METH_VARARGS
,
1397 { "schema_format_value", (PyCFunction
)py_ldb_schema_format_value
, METH_VARARGS
,
1399 { "parse_ldif", (PyCFunction
)py_ldb_parse_ldif
, METH_VARARGS
,
1400 "S.parse_ldif(ldif) -> iter(messages)\n"
1401 "Parse a string formatted using LDIF." },
1402 { "write_ldif", (PyCFunction
)py_ldb_write_ldif
, METH_VARARGS
,
1403 "S.write_ldif(message, changetype) -> ldif\n"
1404 "Print the message as a string formatted using LDIF." },
1405 { "msg_diff", (PyCFunction
)py_ldb_msg_diff
, METH_VARARGS
,
1406 "S.msg_diff(Message) -> Message\n"
1407 "Return an LDB Message of the difference between two Message objects." },
1408 { "get_opaque", (PyCFunction
)py_ldb_get_opaque
, METH_VARARGS
,
1409 "S.get_opaque(name) -> value\n"
1410 "Get an opaque value set on this LDB connection. \n"
1411 ":note: The returned value may not be useful in Python."
1413 { "set_opaque", (PyCFunction
)py_ldb_set_opaque
, METH_VARARGS
,
1414 "S.set_opaque(name, value) -> None\n"
1415 "Set an opaque value on this LDB connection. \n"
1416 ":note: Passing incorrect values may cause crashes." },
1417 { "modules", (PyCFunction
)py_ldb_modules
, METH_NOARGS
,
1418 "S.modules() -> list\n"
1419 "Return the list of modules on this LDB connection " },
1420 { "sequence_number", (PyCFunction
)py_ldb_sequence_number
, METH_VARARGS
,
1421 "S.sequence_number(type) -> value\n"
1422 "Return the value of the sequence according to the requested type" },
1426 PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
)
1428 PyLdbModuleObject
*ret
;
1430 ret
= (PyLdbModuleObject
*)PyLdbModule
.tp_alloc(&PyLdbModule
, 0);
1435 ret
->mem_ctx
= talloc_new(NULL
);
1436 ret
->mod
= talloc_reference(ret
->mem_ctx
, mod
);
1437 return (PyObject
*)ret
;
1440 static PyObject
*py_ldb_get_firstmodule(PyLdbObject
*self
, void *closure
)
1442 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self
)->modules
);
1445 static PyGetSetDef py_ldb_getset
[] = {
1446 { discard_const_p(char, "firstmodule"), (getter
)py_ldb_get_firstmodule
, NULL
, NULL
},
1450 static int py_ldb_contains(PyLdbObject
*self
, PyObject
*obj
)
1452 struct ldb_context
*ldb_ctx
= PyLdb_AsLdbContext(self
);
1454 struct ldb_result
*result
;
1458 if (!PyObject_AsDn(ldb_ctx
, obj
, ldb_ctx
, &dn
)) {
1462 ret
= ldb_search(ldb_ctx
, ldb_ctx
, &result
, dn
, LDB_SCOPE_BASE
, NULL
,
1464 if (ret
!= LDB_SUCCESS
) {
1465 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb_ctx
);
1469 count
= result
->count
;
1471 talloc_free(result
);
1474 PyErr_Format(PyExc_RuntimeError
,
1475 "Searching for [%s] dn gave %u results!",
1476 ldb_dn_get_linearized(dn
),
1484 static PySequenceMethods py_ldb_seq
= {
1485 .sq_contains
= (objobjproc
)py_ldb_contains
,
1488 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
)
1492 ret
= (PyLdbObject
*)PyLdb
.tp_alloc(&PyLdb
, 0);
1497 ret
->mem_ctx
= talloc_new(NULL
);
1498 ret
->ldb_ctx
= talloc_reference(ret
->mem_ctx
, ldb_ctx
);
1499 return (PyObject
*)ret
;
1502 static void py_ldb_dealloc(PyLdbObject
*self
)
1504 talloc_free(self
->mem_ctx
);
1508 PyTypeObject PyLdb
= {
1509 .tp_name
= "ldb.Ldb",
1510 .tp_methods
= py_ldb_methods
,
1511 .tp_repr
= (reprfunc
)py_ldb_repr
,
1512 .tp_new
= py_ldb_new
,
1513 .tp_init
= (initproc
)py_ldb_init
,
1514 .tp_dealloc
= (destructor
)py_ldb_dealloc
,
1515 .tp_getset
= py_ldb_getset
,
1516 .tp_getattro
= PyObject_GenericGetAttr
,
1517 .tp_basicsize
= sizeof(PyLdbObject
),
1518 .tp_doc
= "Connection to a LDB database.",
1519 .tp_as_sequence
= &py_ldb_seq
,
1520 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1523 static PyObject
*py_ldb_module_repr(PyLdbModuleObject
*self
)
1525 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self
)->ops
->name
);
1528 static PyObject
*py_ldb_module_str(PyLdbModuleObject
*self
)
1530 return PyString_FromString(PyLdbModule_AsModule(self
)->ops
->name
);
1533 static PyObject
*py_ldb_module_start_transaction(PyLdbModuleObject
*self
)
1535 PyLdbModule_AsModule(self
)->ops
->start_transaction(PyLdbModule_AsModule(self
));
1539 static PyObject
*py_ldb_module_end_transaction(PyLdbModuleObject
*self
)
1541 PyLdbModule_AsModule(self
)->ops
->end_transaction(PyLdbModule_AsModule(self
));
1545 static PyObject
*py_ldb_module_del_transaction(PyLdbModuleObject
*self
)
1547 PyLdbModule_AsModule(self
)->ops
->del_transaction(PyLdbModule_AsModule(self
));
1551 static PyObject
*py_ldb_module_search(PyLdbModuleObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1553 PyObject
*py_base
, *py_tree
, *py_attrs
, *py_ret
;
1555 struct ldb_request
*req
;
1556 const char * const kwnames
[] = { "base", "scope", "tree", "attrs", NULL
};
1557 struct ldb_module
*mod
;
1558 const char * const*attrs
;
1560 /* type "int" rather than "enum" for "scope" is intentional */
1561 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OiOO",
1562 discard_const_p(char *, kwnames
),
1563 &py_base
, &scope
, &py_tree
, &py_attrs
))
1568 if (py_attrs
== Py_None
) {
1571 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1576 ret
= ldb_build_search_req(&req
, mod
->ldb
, NULL
, PyLdbDn_AsDn(py_base
),
1577 scope
, NULL
/* expr */, attrs
,
1578 NULL
/* controls */, NULL
, NULL
, NULL
);
1580 talloc_steal(req
, attrs
);
1582 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1584 req
->op
.search
.res
= NULL
;
1586 ret
= mod
->ops
->search(mod
, req
);
1588 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1590 py_ret
= PyLdbResult_FromResult(req
->op
.search
.res
);
1598 static PyObject
*py_ldb_module_add(PyLdbModuleObject
*self
, PyObject
*args
)
1600 struct ldb_request
*req
;
1601 PyObject
*py_message
;
1603 struct ldb_module
*mod
;
1605 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1608 req
= talloc_zero(NULL
, struct ldb_request
);
1609 req
->operation
= LDB_ADD
;
1610 req
->op
.add
.message
= PyLdbMessage_AsMessage(py_message
);
1612 mod
= PyLdbModule_AsModule(self
);
1613 ret
= mod
->ops
->add(mod
, req
);
1615 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1620 static PyObject
*py_ldb_module_modify(PyLdbModuleObject
*self
, PyObject
*args
)
1623 struct ldb_request
*req
;
1624 PyObject
*py_message
;
1625 struct ldb_module
*mod
;
1627 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1630 req
= talloc_zero(NULL
, struct ldb_request
);
1631 req
->operation
= LDB_MODIFY
;
1632 req
->op
.mod
.message
= PyLdbMessage_AsMessage(py_message
);
1634 mod
= PyLdbModule_AsModule(self
);
1635 ret
= mod
->ops
->modify(mod
, req
);
1637 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1642 static PyObject
*py_ldb_module_delete(PyLdbModuleObject
*self
, PyObject
*args
)
1645 struct ldb_request
*req
;
1648 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
1651 req
= talloc_zero(NULL
, struct ldb_request
);
1652 req
->operation
= LDB_DELETE
;
1653 req
->op
.del
.dn
= PyLdbDn_AsDn(py_dn
);
1655 ret
= PyLdbModule_AsModule(self
)->ops
->del(PyLdbModule_AsModule(self
), req
);
1657 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1662 static PyObject
*py_ldb_module_rename(PyLdbModuleObject
*self
, PyObject
*args
)
1665 struct ldb_request
*req
;
1666 PyObject
*py_dn1
, *py_dn2
;
1668 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
1671 req
= talloc_zero(NULL
, struct ldb_request
);
1673 req
->operation
= LDB_RENAME
;
1674 req
->op
.rename
.olddn
= PyLdbDn_AsDn(py_dn1
);
1675 req
->op
.rename
.newdn
= PyLdbDn_AsDn(py_dn2
);
1677 ret
= PyLdbModule_AsModule(self
)->ops
->rename(PyLdbModule_AsModule(self
), req
);
1679 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1684 static PyMethodDef py_ldb_module_methods
[] = {
1685 { "search", (PyCFunction
)py_ldb_module_search
, METH_VARARGS
|METH_KEYWORDS
, NULL
},
1686 { "add", (PyCFunction
)py_ldb_module_add
, METH_VARARGS
, NULL
},
1687 { "modify", (PyCFunction
)py_ldb_module_modify
, METH_VARARGS
, NULL
},
1688 { "rename", (PyCFunction
)py_ldb_module_rename
, METH_VARARGS
, NULL
},
1689 { "delete", (PyCFunction
)py_ldb_module_delete
, METH_VARARGS
, NULL
},
1690 { "start_transaction", (PyCFunction
)py_ldb_module_start_transaction
, METH_NOARGS
, NULL
},
1691 { "end_transaction", (PyCFunction
)py_ldb_module_end_transaction
, METH_NOARGS
, NULL
},
1692 { "del_transaction", (PyCFunction
)py_ldb_module_del_transaction
, METH_NOARGS
, NULL
},
1696 static void py_ldb_module_dealloc(PyLdbModuleObject
*self
)
1698 talloc_free(self
->mem_ctx
);
1702 PyTypeObject PyLdbModule
= {
1703 .tp_name
= "ldb.LdbModule",
1704 .tp_methods
= py_ldb_module_methods
,
1705 .tp_repr
= (reprfunc
)py_ldb_module_repr
,
1706 .tp_str
= (reprfunc
)py_ldb_module_str
,
1707 .tp_basicsize
= sizeof(PyLdbModuleObject
),
1708 .tp_dealloc
= (destructor
)py_ldb_module_dealloc
,
1709 .tp_flags
= Py_TPFLAGS_DEFAULT
,
1714 * Create a ldb_message_element from a Python object.
1716 * This will accept any sequence objects that contains strings, or
1719 * A reference to set_obj will be borrowed.
1721 * @param mem_ctx Memory context
1722 * @param set_obj Python object to convert
1723 * @param flags ldb_message_element flags to set
1724 * @param attr_name Name of the attribute
1725 * @return New ldb_message_element, allocated as child of mem_ctx
1727 struct ldb_message_element
*PyObject_AsMessageElement(TALLOC_CTX
*mem_ctx
,
1730 const char *attr_name
)
1732 struct ldb_message_element
*me
;
1734 if (PyLdbMessageElement_Check(set_obj
)) {
1735 PyLdbMessageElementObject
*set_obj_as_me
= (PyLdbMessageElementObject
*)set_obj
;
1736 /* We have to talloc_reference() the memory context, not the pointer which may not actually be it's own context */
1737 if (talloc_reference(mem_ctx
, set_obj_as_me
->mem_ctx
)) {
1738 return PyLdbMessageElement_AsMessageElement(set_obj
);
1743 me
= talloc(mem_ctx
, struct ldb_message_element
);
1745 me
->name
= talloc_strdup(me
, attr_name
);
1747 if (PyString_Check(set_obj
)) {
1749 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1750 me
->values
[0].length
= PyString_Size(set_obj
);
1751 me
->values
[0].data
= talloc_memdup(me
,
1752 (uint8_t *)PyString_AsString(set_obj
), me
->values
[0].length
+1);
1753 } else if (PySequence_Check(set_obj
)) {
1755 me
->num_values
= PySequence_Size(set_obj
);
1756 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1757 for (i
= 0; i
< me
->num_values
; i
++) {
1758 PyObject
*obj
= PySequence_GetItem(set_obj
, i
);
1759 if (!PyString_Check(obj
)) {
1760 PyErr_Format(PyExc_TypeError
,
1761 "Expected string as element %zd in list", i
);
1766 me
->values
[i
].length
= PyString_Size(obj
);
1767 me
->values
[i
].data
= talloc_memdup(me
,
1768 (uint8_t *)PyString_AsString(obj
), me
->values
[i
].length
+1);
1779 static PyObject
*ldb_msg_element_to_set(struct ldb_context
*ldb_ctx
,
1780 struct ldb_message_element
*me
)
1785 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1786 result
= PyList_New(me
->num_values
);
1788 for (i
= 0; i
< me
->num_values
; i
++) {
1789 PyList_SetItem(result
, i
,
1790 PyObject_FromLdbValue(ldb_ctx
, me
, &me
->values
[i
]));
1796 static PyObject
*py_ldb_msg_element_get(PyLdbMessageElementObject
*self
, PyObject
*args
)
1799 if (!PyArg_ParseTuple(args
, "I", &i
))
1801 if (i
>= PyLdbMessageElement_AsMessageElement(self
)->num_values
)
1804 return PyObject_FromLdbValue(NULL
, PyLdbMessageElement_AsMessageElement(self
),
1805 &(PyLdbMessageElement_AsMessageElement(self
)->values
[i
]));
1808 static PyObject
*py_ldb_msg_element_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1810 struct ldb_message_element
*el
;
1812 el
= PyLdbMessageElement_AsMessageElement(self
);
1813 return PyInt_FromLong(el
->flags
);
1816 static PyObject
*py_ldb_msg_element_set_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1819 struct ldb_message_element
*el
;
1820 if (!PyArg_ParseTuple(args
, "i", &flags
))
1823 el
= PyLdbMessageElement_AsMessageElement(self
);
1828 static PyMethodDef py_ldb_msg_element_methods
[] = {
1829 { "get", (PyCFunction
)py_ldb_msg_element_get
, METH_VARARGS
, NULL
},
1830 { "set_flags", (PyCFunction
)py_ldb_msg_element_set_flags
, METH_VARARGS
, NULL
},
1831 { "flags", (PyCFunction
)py_ldb_msg_element_flags
, METH_NOARGS
, NULL
},
1835 static Py_ssize_t
py_ldb_msg_element_len(PyLdbMessageElementObject
*self
)
1837 return PyLdbMessageElement_AsMessageElement(self
)->num_values
;
1840 static PyObject
*py_ldb_msg_element_find(PyLdbMessageElementObject
*self
, Py_ssize_t idx
)
1842 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1843 if (idx
< 0 || idx
>= el
->num_values
) {
1844 PyErr_SetString(PyExc_IndexError
, "Out of range");
1847 return PyString_FromStringAndSize((char *)el
->values
[idx
].data
, el
->values
[idx
].length
);
1850 static PySequenceMethods py_ldb_msg_element_seq
= {
1851 .sq_length
= (lenfunc
)py_ldb_msg_element_len
,
1852 .sq_item
= (ssizeargfunc
)py_ldb_msg_element_find
,
1855 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject
*self
, PyLdbMessageElementObject
*other
)
1857 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self
),
1858 PyLdbMessageElement_AsMessageElement(other
));
1861 static PyObject
*py_ldb_msg_element_iter(PyLdbMessageElementObject
*self
)
1863 return PyObject_GetIter(ldb_msg_element_to_set(NULL
, PyLdbMessageElement_AsMessageElement(self
)));
1866 PyObject
*PyLdbMessageElement_FromMessageElement(struct ldb_message_element
*el
, TALLOC_CTX
*mem_ctx
)
1868 PyLdbMessageElementObject
*ret
;
1869 ret
= PyObject_New(PyLdbMessageElementObject
, &PyLdbMessageElement
);
1874 ret
->mem_ctx
= talloc_new(NULL
);
1875 if (talloc_reference(ret
->mem_ctx
, mem_ctx
) == NULL
) {
1880 return (PyObject
*)ret
;
1883 static PyObject
*py_ldb_msg_element_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1885 PyObject
*py_elements
= NULL
;
1886 struct ldb_message_element
*el
;
1889 const char * const kwnames
[] = { "elements", "flags", "name", NULL
};
1890 PyLdbMessageElementObject
*ret
;
1891 TALLOC_CTX
*mem_ctx
;
1893 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ois",
1894 discard_const_p(char *, kwnames
),
1895 &py_elements
, &flags
, &name
))
1898 mem_ctx
= talloc_new(NULL
);
1899 if (mem_ctx
== NULL
) {
1904 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
1906 if (py_elements
!= NULL
) {
1908 if (PyString_Check(py_elements
)) {
1910 el
->values
= talloc_array(el
, struct ldb_val
, 1);
1911 el
->values
[0].length
= PyString_Size(py_elements
);
1912 el
->values
[0].data
= talloc_memdup(el
,
1913 (uint8_t *)PyString_AsString(py_elements
), el
->values
[0].length
+1);
1914 } else if (PySequence_Check(py_elements
)) {
1915 el
->num_values
= PySequence_Size(py_elements
);
1916 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
1917 for (i
= 0; i
< el
->num_values
; i
++) {
1918 PyObject
*item
= PySequence_GetItem(py_elements
, i
);
1919 if (!PyString_Check(item
)) {
1920 PyErr_Format(PyExc_TypeError
,
1921 "Expected string as element %zd in list", i
);
1922 talloc_free(mem_ctx
);
1925 el
->values
[i
].length
= PyString_Size(item
);
1926 el
->values
[i
].data
= talloc_memdup(el
,
1927 (uint8_t *)PyString_AsString(item
), el
->values
[i
].length
+1);
1930 PyErr_SetString(PyExc_TypeError
,
1931 "Expected string or list");
1932 talloc_free(mem_ctx
);
1938 el
->name
= talloc_strdup(el
, name
);
1940 ret
= PyObject_New(PyLdbMessageElementObject
, &PyLdbMessageElement
);
1943 talloc_free(mem_ctx
);
1947 ret
->mem_ctx
= mem_ctx
;
1949 return (PyObject
*)ret
;
1952 static PyObject
*py_ldb_msg_element_repr(PyLdbMessageElementObject
*self
)
1954 char *element_str
= NULL
;
1956 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1959 for (i
= 0; i
< el
->num_values
; i
++) {
1960 PyObject
*o
= py_ldb_msg_element_find(self
, i
);
1961 if (element_str
== NULL
)
1962 element_str
= talloc_strdup(NULL
, PyObject_REPR(o
));
1964 element_str
= talloc_asprintf_append(element_str
, ",%s", PyObject_REPR(o
));
1967 if (element_str
!= NULL
) {
1968 ret
= PyString_FromFormat("MessageElement([%s])", element_str
);
1969 talloc_free(element_str
);
1971 ret
= PyString_FromString("MessageElement([])");
1977 static PyObject
*py_ldb_msg_element_str(PyLdbMessageElementObject
*self
)
1979 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1981 if (el
->num_values
== 1)
1982 return PyString_FromStringAndSize((char *)el
->values
[0].data
, el
->values
[0].length
);
1987 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject
*self
)
1989 talloc_free(self
->mem_ctx
);
1993 static PyTypeObject PyLdbMessageElement
= {
1994 .tp_name
= "ldb.MessageElement",
1995 .tp_basicsize
= sizeof(PyLdbMessageElementObject
),
1996 .tp_dealloc
= (destructor
)py_ldb_msg_element_dealloc
,
1997 .tp_repr
= (reprfunc
)py_ldb_msg_element_repr
,
1998 .tp_str
= (reprfunc
)py_ldb_msg_element_str
,
1999 .tp_methods
= py_ldb_msg_element_methods
,
2000 .tp_compare
= (cmpfunc
)py_ldb_msg_element_cmp
,
2001 .tp_iter
= (getiterfunc
)py_ldb_msg_element_iter
,
2002 .tp_as_sequence
= &py_ldb_msg_element_seq
,
2003 .tp_new
= py_ldb_msg_element_new
,
2004 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2008 static PyObject
*py_ldb_msg_from_dict(PyTypeObject
*type
, PyObject
*args
)
2013 struct ldb_message
*msg
;
2014 struct ldb_context
*ldb_ctx
;
2015 unsigned int mod_flags
= LDB_FLAG_MOD_REPLACE
;
2017 if (!PyArg_ParseTuple(args
, "O!O!|I",
2018 &PyLdb
, &py_ldb
, &PyDict_Type
, &py_dict
,
2023 /* mask only flags we are going to use */
2024 mod_flags
= LDB_FLAG_MOD_TYPE(mod_flags
);
2026 PyErr_SetString(PyExc_ValueError
,
2027 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2028 " expected as mod_flag value");
2032 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
2034 msg
= PyDict_AsMessage(ldb_ctx
, py_dict
, ldb_ctx
, mod_flags
);
2039 py_ret
= PyLdbMessage_FromMessage(msg
);
2041 talloc_unlink(ldb_ctx
, msg
);
2046 static PyObject
*py_ldb_msg_remove_attr(PyLdbMessageObject
*self
, PyObject
*args
)
2049 if (!PyArg_ParseTuple(args
, "s", &name
))
2052 ldb_msg_remove_attr(self
->msg
, name
);
2057 static PyObject
*py_ldb_msg_keys(PyLdbMessageObject
*self
)
2059 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2060 Py_ssize_t i
, j
= 0;
2061 PyObject
*obj
= PyList_New(msg
->num_elements
+(msg
->dn
!= NULL
?1:0));
2062 if (msg
->dn
!= NULL
) {
2063 PyList_SetItem(obj
, j
, PyString_FromString("dn"));
2066 for (i
= 0; i
< msg
->num_elements
; i
++) {
2067 PyList_SetItem(obj
, j
, PyString_FromString(msg
->elements
[i
].name
));
2073 static PyObject
*py_ldb_msg_getitem_helper(PyLdbMessageObject
*self
, PyObject
*py_name
)
2075 struct ldb_message_element
*el
;
2077 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2078 if (!PyString_Check(py_name
)) {
2079 PyErr_SetNone(PyExc_TypeError
);
2082 name
= PyString_AsString(py_name
);
2083 if (!strcmp(name
, "dn"))
2084 return PyLdbDn_FromDn(msg
->dn
);
2085 el
= ldb_msg_find_element(msg
, name
);
2089 return (PyObject
*)PyLdbMessageElement_FromMessageElement(el
, msg
->elements
);
2092 static PyObject
*py_ldb_msg_getitem(PyLdbMessageObject
*self
, PyObject
*py_name
)
2094 PyObject
*ret
= py_ldb_msg_getitem_helper(self
, py_name
);
2096 PyErr_SetString(PyExc_KeyError
, "No such element");
2102 static PyObject
*py_ldb_msg_get(PyLdbMessageObject
*self
, PyObject
*args
)
2104 PyObject
*name
, *ret
;
2105 if (!PyArg_ParseTuple(args
, "O", &name
))
2108 ret
= py_ldb_msg_getitem_helper(self
, name
);
2110 if (PyErr_Occurred())
2117 static PyObject
*py_ldb_msg_items(PyLdbMessageObject
*self
)
2119 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2120 Py_ssize_t i
, j
= 0;
2121 PyObject
*l
= PyList_New(msg
->num_elements
+ (msg
->dn
== NULL
?0:1));
2122 if (msg
->dn
!= NULL
) {
2123 PyList_SetItem(l
, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg
->dn
)));
2126 for (i
= 0; i
< msg
->num_elements
; i
++, j
++) {
2127 PyList_SetItem(l
, j
, Py_BuildValue("(sO)", msg
->elements
[i
].name
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
)));
2132 static PyObject
*py_ldb_msg_elements(PyLdbMessageObject
*self
)
2134 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2136 PyObject
*l
= PyList_New(msg
->num_elements
);
2137 for (i
= 0; i
< msg
->num_elements
; i
++) {
2138 PyList_SetItem(l
, i
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
));
2143 static PyObject
*py_ldb_msg_add(PyLdbMessageObject
*self
, PyObject
*args
)
2145 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2146 PyObject
*py_element
;
2148 struct ldb_message_element
*el
;
2150 if (!PyArg_ParseTuple(args
, "O!", &PyLdbMessageElement
, &py_element
))
2153 el
= talloc_reference(msg
, PyLdbMessageElement_AsMessageElement(py_element
));
2159 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2160 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2165 static PyMethodDef py_ldb_msg_methods
[] = {
2166 { "from_dict", (PyCFunction
)py_ldb_msg_from_dict
, METH_CLASS
| METH_VARARGS
,
2167 "Message.from_dict(ldb, dict, mod_flag=FLAG_MOD_REPLACE) -> ldb.Message\n"
2168 "Class method to create ldb.Message object from Dictionary.\n"
2169 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE."},
2170 { "keys", (PyCFunction
)py_ldb_msg_keys
, METH_NOARGS
,
2171 "S.keys() -> list\n\n"
2172 "Return sequence of all attribute names." },
2173 { "remove", (PyCFunction
)py_ldb_msg_remove_attr
, METH_VARARGS
,
2174 "S.remove(name)\n\n"
2175 "Remove all entries for attributes with the specified name."},
2176 { "get", (PyCFunction
)py_ldb_msg_get
, METH_VARARGS
, NULL
},
2177 { "items", (PyCFunction
)py_ldb_msg_items
, METH_NOARGS
, NULL
},
2178 { "elements", (PyCFunction
)py_ldb_msg_elements
, METH_NOARGS
, NULL
},
2179 { "add", (PyCFunction
)py_ldb_msg_add
, METH_VARARGS
,
2180 "S.append(element)\n\n"
2181 "Add an element to this message." },
2185 static PyObject
*py_ldb_msg_iter(PyLdbMessageObject
*self
)
2187 PyObject
*list
, *iter
;
2189 list
= py_ldb_msg_keys(self
);
2190 iter
= PyObject_GetIter(list
);
2195 static int py_ldb_msg_setitem(PyLdbMessageObject
*self
, PyObject
*name
, PyObject
*value
)
2199 if (!PyString_Check(name
)) {
2200 PyErr_SetNone(PyExc_TypeError
);
2204 attr_name
= PyString_AsString(name
);
2205 if (value
== NULL
) {
2207 ldb_msg_remove_attr(self
->msg
, attr_name
);
2209 struct ldb_message_element
*el
= PyObject_AsMessageElement(self
->msg
,
2210 value
, 0, attr_name
);
2213 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self
), attr_name
);
2214 ldb_msg_add(PyLdbMessage_AsMessage(self
), el
, el
->flags
);
2219 static Py_ssize_t
py_ldb_msg_length(PyLdbMessageObject
*self
)
2221 return PyLdbMessage_AsMessage(self
)->num_elements
;
2224 static PyMappingMethods py_ldb_msg_mapping
= {
2225 .mp_length
= (lenfunc
)py_ldb_msg_length
,
2226 .mp_subscript
= (binaryfunc
)py_ldb_msg_getitem
,
2227 .mp_ass_subscript
= (objobjargproc
)py_ldb_msg_setitem
,
2230 static PyObject
*py_ldb_msg_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
2232 const char * const kwnames
[] = { "dn", NULL
};
2233 struct ldb_message
*ret
;
2234 TALLOC_CTX
*mem_ctx
;
2235 PyObject
*pydn
= NULL
;
2236 PyLdbMessageObject
*py_ret
;
2238 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O",
2239 discard_const_p(char *, kwnames
),
2243 mem_ctx
= talloc_new(NULL
);
2244 if (mem_ctx
== NULL
) {
2249 ret
= ldb_msg_new(mem_ctx
);
2251 talloc_free(mem_ctx
);
2258 if (!PyObject_AsDn(NULL
, pydn
, NULL
, &dn
)) {
2259 talloc_free(mem_ctx
);
2262 ret
->dn
= talloc_reference(ret
, dn
);
2265 py_ret
= (PyLdbMessageObject
*)type
->tp_alloc(type
, 0);
2266 if (py_ret
== NULL
) {
2268 talloc_free(mem_ctx
);
2272 py_ret
->mem_ctx
= mem_ctx
;
2274 return (PyObject
*)py_ret
;
2277 PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
)
2279 PyLdbMessageObject
*ret
;
2281 ret
= (PyLdbMessageObject
*)PyLdbMessage
.tp_alloc(&PyLdbMessage
, 0);
2286 ret
->mem_ctx
= talloc_new(NULL
);
2287 ret
->msg
= talloc_reference(ret
->mem_ctx
, msg
);
2288 return (PyObject
*)ret
;
2291 static PyObject
*py_ldb_msg_get_dn(PyLdbMessageObject
*self
, void *closure
)
2293 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2294 return PyLdbDn_FromDn(msg
->dn
);
2297 static int py_ldb_msg_set_dn(PyLdbMessageObject
*self
, PyObject
*value
, void *closure
)
2299 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2300 if (!PyLdbDn_Check(value
)) {
2301 PyErr_SetNone(PyExc_TypeError
);
2305 msg
->dn
= talloc_reference(msg
, PyLdbDn_AsDn(value
));
2309 static PyGetSetDef py_ldb_msg_getset
[] = {
2310 { discard_const_p(char, "dn"), (getter
)py_ldb_msg_get_dn
, (setter
)py_ldb_msg_set_dn
, NULL
},
2314 static PyObject
*py_ldb_msg_repr(PyLdbMessageObject
*self
)
2316 PyObject
*dict
= PyDict_New(), *ret
;
2317 if (PyDict_Update(dict
, (PyObject
*)self
) != 0)
2319 ret
= PyString_FromFormat("Message(%s)", PyObject_REPR(dict
));
2324 static void py_ldb_msg_dealloc(PyLdbMessageObject
*self
)
2326 talloc_free(self
->mem_ctx
);
2330 static int py_ldb_msg_compare(PyLdbMessageObject
*py_msg1
,
2331 PyLdbMessageObject
*py_msg2
)
2333 struct ldb_message
*msg1
= PyLdbMessage_AsMessage(py_msg1
),
2334 *msg2
= PyLdbMessage_AsMessage(py_msg2
);
2338 if ((msg1
->dn
!= NULL
) || (msg2
->dn
!= NULL
)) {
2339 ret
= ldb_dn_compare(msg1
->dn
, msg2
->dn
);
2345 ret
= msg1
->num_elements
- msg2
->num_elements
;
2350 for (i
= 0; i
< msg1
->num_elements
; i
++) {
2351 ret
= ldb_msg_element_compare_name(&msg1
->elements
[i
],
2352 &msg2
->elements
[i
]);
2357 ret
= ldb_msg_element_compare(&msg1
->elements
[i
],
2358 &msg2
->elements
[i
]);
2367 PyTypeObject PyLdbMessage
= {
2368 .tp_name
= "ldb.Message",
2369 .tp_methods
= py_ldb_msg_methods
,
2370 .tp_getset
= py_ldb_msg_getset
,
2371 .tp_as_mapping
= &py_ldb_msg_mapping
,
2372 .tp_basicsize
= sizeof(PyLdbMessageObject
),
2373 .tp_dealloc
= (destructor
)py_ldb_msg_dealloc
,
2374 .tp_new
= py_ldb_msg_new
,
2375 .tp_repr
= (reprfunc
)py_ldb_msg_repr
,
2376 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2377 .tp_iter
= (getiterfunc
)py_ldb_msg_iter
,
2378 .tp_compare
= (cmpfunc
)py_ldb_msg_compare
,
2381 PyObject
*PyLdbTree_FromTree(struct ldb_parse_tree
*tree
)
2383 PyLdbTreeObject
*ret
;
2385 ret
= (PyLdbTreeObject
*)PyLdbTree
.tp_alloc(&PyLdbTree
, 0);
2391 ret
->mem_ctx
= talloc_new(NULL
);
2392 ret
->tree
= talloc_reference(ret
->mem_ctx
, tree
);
2393 return (PyObject
*)ret
;
2396 static void py_ldb_tree_dealloc(PyLdbTreeObject
*self
)
2398 talloc_free(self
->mem_ctx
);
2402 PyTypeObject PyLdbTree
= {
2403 .tp_name
= "ldb.Tree",
2404 .tp_basicsize
= sizeof(PyLdbTreeObject
),
2405 .tp_dealloc
= (destructor
)py_ldb_tree_dealloc
,
2406 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2410 static int py_module_search(struct ldb_module
*mod
, struct ldb_request
*req
)
2412 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2413 PyObject
*py_result
, *py_base
, *py_attrs
, *py_tree
;
2415 py_base
= PyLdbDn_FromDn(req
->op
.search
.base
);
2417 if (py_base
== NULL
)
2418 return LDB_ERR_OPERATIONS_ERROR
;
2420 py_tree
= PyLdbTree_FromTree(req
->op
.search
.tree
);
2422 if (py_tree
== NULL
)
2423 return LDB_ERR_OPERATIONS_ERROR
;
2425 if (req
->op
.search
.attrs
== NULL
) {
2429 for (len
= 0; req
->op
.search
.attrs
[len
]; len
++);
2430 py_attrs
= PyList_New(len
);
2431 for (i
= 0; i
< len
; i
++)
2432 PyList_SetItem(py_attrs
, i
, PyString_FromString(req
->op
.search
.attrs
[i
]));
2435 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "search"),
2436 discard_const_p(char, "OiOO"),
2437 py_base
, req
->op
.search
.scope
, py_tree
, py_attrs
);
2439 Py_DECREF(py_attrs
);
2443 if (py_result
== NULL
) {
2444 return LDB_ERR_PYTHON_EXCEPTION
;
2447 req
->op
.search
.res
= PyLdbResult_AsResult(NULL
, py_result
);
2448 if (req
->op
.search
.res
== NULL
) {
2449 return LDB_ERR_PYTHON_EXCEPTION
;
2452 Py_DECREF(py_result
);
2457 static int py_module_add(struct ldb_module
*mod
, struct ldb_request
*req
)
2459 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2460 PyObject
*py_result
, *py_msg
;
2462 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.add
.message
));
2464 if (py_msg
== NULL
) {
2465 return LDB_ERR_OPERATIONS_ERROR
;
2468 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "add"),
2469 discard_const_p(char, "O"),
2474 if (py_result
== NULL
) {
2475 return LDB_ERR_PYTHON_EXCEPTION
;
2478 Py_DECREF(py_result
);
2483 static int py_module_modify(struct ldb_module
*mod
, struct ldb_request
*req
)
2485 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2486 PyObject
*py_result
, *py_msg
;
2488 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.mod
.message
));
2490 if (py_msg
== NULL
) {
2491 return LDB_ERR_OPERATIONS_ERROR
;
2494 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "modify"),
2495 discard_const_p(char, "O"),
2500 if (py_result
== NULL
) {
2501 return LDB_ERR_PYTHON_EXCEPTION
;
2504 Py_DECREF(py_result
);
2509 static int py_module_del(struct ldb_module
*mod
, struct ldb_request
*req
)
2511 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2512 PyObject
*py_result
, *py_dn
;
2514 py_dn
= PyLdbDn_FromDn(req
->op
.del
.dn
);
2517 return LDB_ERR_OPERATIONS_ERROR
;
2519 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "delete"),
2520 discard_const_p(char, "O"),
2523 if (py_result
== NULL
) {
2524 return LDB_ERR_PYTHON_EXCEPTION
;
2527 Py_DECREF(py_result
);
2532 static int py_module_rename(struct ldb_module
*mod
, struct ldb_request
*req
)
2534 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2535 PyObject
*py_result
, *py_olddn
, *py_newdn
;
2537 py_olddn
= PyLdbDn_FromDn(req
->op
.rename
.olddn
);
2539 if (py_olddn
== NULL
)
2540 return LDB_ERR_OPERATIONS_ERROR
;
2542 py_newdn
= PyLdbDn_FromDn(req
->op
.rename
.newdn
);
2544 if (py_newdn
== NULL
)
2545 return LDB_ERR_OPERATIONS_ERROR
;
2547 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "rename"),
2548 discard_const_p(char, "OO"),
2549 py_olddn
, py_newdn
);
2551 Py_DECREF(py_olddn
);
2552 Py_DECREF(py_newdn
);
2554 if (py_result
== NULL
) {
2555 return LDB_ERR_PYTHON_EXCEPTION
;
2558 Py_DECREF(py_result
);
2563 static int py_module_request(struct ldb_module
*mod
, struct ldb_request
*req
)
2565 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2566 PyObject
*py_result
;
2568 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "request"),
2569 discard_const_p(char, ""));
2571 return LDB_ERR_OPERATIONS_ERROR
;
2574 static int py_module_extended(struct ldb_module
*mod
, struct ldb_request
*req
)
2576 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2577 PyObject
*py_result
;
2579 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "extended"),
2580 discard_const_p(char, ""));
2582 return LDB_ERR_OPERATIONS_ERROR
;
2585 static int py_module_start_transaction(struct ldb_module
*mod
)
2587 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2588 PyObject
*py_result
;
2590 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "start_transaction"),
2591 discard_const_p(char, ""));
2593 if (py_result
== NULL
) {
2594 return LDB_ERR_PYTHON_EXCEPTION
;
2597 Py_DECREF(py_result
);
2602 static int py_module_end_transaction(struct ldb_module
*mod
)
2604 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2605 PyObject
*py_result
;
2607 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "end_transaction"),
2608 discard_const_p(char, ""));
2610 if (py_result
== NULL
) {
2611 return LDB_ERR_PYTHON_EXCEPTION
;
2614 Py_DECREF(py_result
);
2619 static int py_module_del_transaction(struct ldb_module
*mod
)
2621 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2622 PyObject
*py_result
;
2624 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "del_transaction"),
2625 discard_const_p(char, ""));
2627 if (py_result
== NULL
) {
2628 return LDB_ERR_PYTHON_EXCEPTION
;
2631 Py_DECREF(py_result
);
2636 static int py_module_destructor(struct ldb_module
*mod
)
2638 Py_DECREF((PyObject
*)mod
->private_data
);
2642 static int py_module_init(struct ldb_module
*mod
)
2644 PyObject
*py_class
= (PyObject
*)mod
->ops
->private_data
;
2645 PyObject
*py_result
, *py_next
, *py_ldb
;
2647 py_ldb
= PyLdb_FromLdbContext(mod
->ldb
);
2650 return LDB_ERR_OPERATIONS_ERROR
;
2652 py_next
= PyLdbModule_FromModule(mod
->next
);
2654 if (py_next
== NULL
)
2655 return LDB_ERR_OPERATIONS_ERROR
;
2657 py_result
= PyObject_CallFunction(py_class
, discard_const_p(char, "OO"),
2660 if (py_result
== NULL
) {
2661 return LDB_ERR_PYTHON_EXCEPTION
;
2664 mod
->private_data
= py_result
;
2666 talloc_set_destructor(mod
, py_module_destructor
);
2668 return ldb_next_init(mod
);
2671 static PyObject
*py_register_module(PyObject
*module
, PyObject
*args
)
2674 struct ldb_module_ops
*ops
;
2677 if (!PyArg_ParseTuple(args
, "O", &input
))
2680 ops
= talloc_zero(talloc_autofree_context(), struct ldb_module_ops
);
2686 ops
->name
= talloc_strdup(ops
, PyString_AsString(PyObject_GetAttrString(input
, discard_const_p(char, "name"))));
2689 ops
->private_data
= input
;
2690 ops
->init_context
= py_module_init
;
2691 ops
->search
= py_module_search
;
2692 ops
->add
= py_module_add
;
2693 ops
->modify
= py_module_modify
;
2694 ops
->del
= py_module_del
;
2695 ops
->rename
= py_module_rename
;
2696 ops
->request
= py_module_request
;
2697 ops
->extended
= py_module_extended
;
2698 ops
->start_transaction
= py_module_start_transaction
;
2699 ops
->end_transaction
= py_module_end_transaction
;
2700 ops
->del_transaction
= py_module_del_transaction
;
2702 ret
= ldb_register_module(ops
);
2704 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2709 static PyObject
*py_timestring(PyObject
*module
, PyObject
*args
)
2711 /* most times "time_t" is a signed integer type with 32 or 64 bit:
2712 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
2716 if (!PyArg_ParseTuple(args
, "l", &t_val
))
2718 tresult
= ldb_timestring(NULL
, (time_t) t_val
);
2719 ret
= PyString_FromString(tresult
);
2720 talloc_free(tresult
);
2724 static PyObject
*py_string_to_time(PyObject
*module
, PyObject
*args
)
2727 if (!PyArg_ParseTuple(args
, "s", &str
))
2730 return PyInt_FromLong(ldb_string_to_time(str
));
2733 static PyObject
*py_valid_attr_name(PyObject
*self
, PyObject
*args
)
2736 if (!PyArg_ParseTuple(args
, "s", &name
))
2738 return PyBool_FromLong(ldb_valid_attr_name(name
));
2741 static PyMethodDef py_ldb_global_methods
[] = {
2742 { "register_module", py_register_module
, METH_VARARGS
,
2743 "S.register_module(module) -> None\n"
2744 "Register a LDB module."},
2745 { "timestring", py_timestring
, METH_VARARGS
,
2746 "S.timestring(int) -> string\n"
2747 "Generate a LDAP time string from a UNIX timestamp" },
2748 { "string_to_time", py_string_to_time
, METH_VARARGS
,
2749 "S.string_to_time(string) -> int\n"
2750 "Parse a LDAP time string into a UNIX timestamp." },
2751 { "valid_attr_name", py_valid_attr_name
, METH_VARARGS
,
2752 "S.valid_attr_name(name) -> bool\n"
2753 "Check whether the supplied name is a valid attribute name." },
2754 { "open", (PyCFunction
)py_ldb_new
, METH_VARARGS
|METH_KEYWORDS
,
2763 if (PyType_Ready(&PyLdbDn
) < 0)
2766 if (PyType_Ready(&PyLdbMessage
) < 0)
2769 if (PyType_Ready(&PyLdbMessageElement
) < 0)
2772 if (PyType_Ready(&PyLdb
) < 0)
2775 if (PyType_Ready(&PyLdbModule
) < 0)
2778 if (PyType_Ready(&PyLdbTree
) < 0)
2781 m
= Py_InitModule3("ldb", py_ldb_global_methods
,
2782 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2786 PyModule_AddObject(m
, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ
));
2787 PyModule_AddObject(m
, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP
));
2788 PyModule_AddObject(m
, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT
));
2789 PyModule_AddObject(m
, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT
));
2790 PyModule_AddObject(m
, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE
));
2791 PyModule_AddObject(m
, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL
));
2792 PyModule_AddObject(m
, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE
));
2794 PyModule_AddObject(m
, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE
));
2795 PyModule_AddObject(m
, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD
));
2796 PyModule_AddObject(m
, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE
));
2797 PyModule_AddObject(m
, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY
));
2799 PyModule_AddObject(m
, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD
));
2800 PyModule_AddObject(m
, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE
));
2801 PyModule_AddObject(m
, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE
));
2803 PyModule_AddObject(m
, "SUCCESS", PyInt_FromLong(LDB_SUCCESS
));
2804 PyModule_AddObject(m
, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR
));
2805 PyModule_AddObject(m
, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR
));
2806 PyModule_AddObject(m
, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED
));
2807 PyModule_AddObject(m
, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED
));
2808 PyModule_AddObject(m
, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE
));
2809 PyModule_AddObject(m
, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE
));
2810 PyModule_AddObject(m
, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
));
2811 PyModule_AddObject(m
, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED
));
2812 PyModule_AddObject(m
, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL
));
2813 PyModule_AddObject(m
, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED
));
2814 PyModule_AddObject(m
, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
));
2815 PyModule_AddObject(m
, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED
));
2816 PyModule_AddObject(m
, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS
));
2817 PyModule_AddObject(m
, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE
));
2818 PyModule_AddObject(m
, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
));
2819 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING
));
2820 PyModule_AddObject(m
, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION
));
2821 PyModule_AddObject(m
, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
));
2822 PyModule_AddObject(m
, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
));
2823 PyModule_AddObject(m
, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT
));
2824 PyModule_AddObject(m
, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM
));
2825 PyModule_AddObject(m
, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX
));
2826 PyModule_AddObject(m
, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
));
2827 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION
));
2828 PyModule_AddObject(m
, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS
));
2829 PyModule_AddObject(m
, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
));
2830 PyModule_AddObject(m
, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY
));
2831 PyModule_AddObject(m
, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE
));
2832 PyModule_AddObject(m
, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM
));
2833 PyModule_AddObject(m
, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT
));
2834 PyModule_AddObject(m
, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION
));
2835 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION
));
2836 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
));
2837 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN
));
2838 PyModule_AddObject(m
, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS
));
2839 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
));
2840 PyModule_AddObject(m
, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS
));
2841 PyModule_AddObject(m
, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER
));
2843 PyModule_AddObject(m
, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY
));
2844 PyModule_AddObject(m
, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC
));
2845 PyModule_AddObject(m
, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT
));
2846 PyModule_AddObject(m
, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP
));
2848 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
2850 PyExc_LdbError
= PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL
, NULL
);
2851 PyModule_AddObject(m
, "LdbError", PyExc_LdbError
);
2854 Py_INCREF(&PyLdbDn
);
2855 Py_INCREF(&PyLdbModule
);
2856 Py_INCREF(&PyLdbMessage
);
2857 Py_INCREF(&PyLdbMessageElement
);
2858 Py_INCREF(&PyLdbTree
);
2860 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PyLdb
);
2861 PyModule_AddObject(m
, "Dn", (PyObject
*)&PyLdbDn
);
2862 PyModule_AddObject(m
, "Message", (PyObject
*)&PyLdbMessage
);
2863 PyModule_AddObject(m
, "MessageElement", (PyObject
*)&PyLdbMessageElement
);
2864 PyModule_AddObject(m
, "Module", (PyObject
*)&PyLdbModule
);
2865 PyModule_AddObject(m
, "Tree", (PyObject
*)&PyLdbTree
);
2867 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));