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);
512 for (i
= 0; i
< PyList_Size(list
); i
++) {
513 PyObject
*item
= PyList_GetItem(list
, i
);
514 if (!PyString_Check(item
)) {
515 PyErr_Format(PyExc_TypeError
, "%s should be strings", paramname
);
518 ret
[i
] = talloc_strndup(ret
, PyString_AsString(item
),
519 PyString_Size(item
));
525 static int py_ldb_init(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
527 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
529 PyObject
*py_options
= Py_None
;
530 const char **options
;
533 struct ldb_context
*ldb
;
535 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO:Ldb.__init__",
536 discard_const_p(char *, kwnames
),
537 &url
, &flags
, &py_options
))
540 ldb
= PyLdb_AsLdbContext(self
);
542 if (py_options
== Py_None
) {
545 options
= PyList_AsStringList(ldb
, py_options
, "options");
551 ret
= ldb_connect(ldb
, url
, flags
, options
);
552 if (ret
!= LDB_SUCCESS
) {
553 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb
);
558 talloc_free(options
);
562 static PyObject
*py_ldb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
565 struct ldb_context
*ldb
;
566 ret
= (PyLdbObject
*)type
->tp_alloc(type
, 0);
571 ret
->mem_ctx
= talloc_new(NULL
);
572 ldb
= ldb_init(ret
->mem_ctx
, NULL
);
580 return (PyObject
*)ret
;
583 static PyObject
*py_ldb_connect(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
587 PyObject
*py_options
= Py_None
;
589 const char **options
;
590 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
592 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO",
593 discard_const_p(char *, kwnames
),
594 &url
, &flags
, &py_options
))
597 if (py_options
== Py_None
) {
600 options
= PyList_AsStringList(NULL
, py_options
, "options");
605 ret
= ldb_connect(PyLdb_AsLdbContext(self
), url
, flags
, options
);
606 talloc_free(options
);
608 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
613 static PyObject
*py_ldb_modify(PyLdbObject
*self
, PyObject
*args
)
616 PyObject
*py_controls
= Py_None
;
617 struct ldb_context
*ldb_ctx
;
618 struct ldb_request
*req
;
619 struct ldb_control
**parsed_controls
;
620 struct ldb_message
*msg
;
624 if (!PyArg_ParseTuple(args
, "O|O", &py_msg
, &py_controls
))
627 mem_ctx
= talloc_new(NULL
);
628 if (mem_ctx
== NULL
) {
632 ldb_ctx
= PyLdb_AsLdbContext(self
);
634 if (py_controls
== Py_None
) {
635 parsed_controls
= NULL
;
637 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
638 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
639 talloc_free(controls
);
642 if (!PyLdbMessage_Check(py_msg
)) {
643 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message");
644 talloc_free(mem_ctx
);
647 msg
= PyLdbMessage_AsMessage(py_msg
);
649 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
650 if (ret
!= LDB_SUCCESS
) {
651 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
652 talloc_free(mem_ctx
);
656 ret
= ldb_build_mod_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
657 NULL
, ldb_op_default_callback
, NULL
);
658 if (ret
!= LDB_SUCCESS
) {
659 PyErr_SetString(PyExc_TypeError
, "failed to build request");
660 talloc_free(mem_ctx
);
664 /* do request and autostart a transaction */
665 /* Then let's LDB handle the message error in case of pb as they are meaningful */
667 ret
= ldb_transaction_start(ldb_ctx
);
668 if (ret
!= LDB_SUCCESS
) {
669 talloc_free(mem_ctx
);
670 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
673 ret
= ldb_request(ldb_ctx
, req
);
674 if (ret
== LDB_SUCCESS
) {
675 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
678 if (ret
== LDB_SUCCESS
) {
679 ret
= ldb_transaction_commit(ldb_ctx
);
681 ldb_transaction_cancel(ldb_ctx
);
682 if (ldb_ctx
->err_string
== NULL
) {
683 /* no error string was setup by the backend */
684 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
688 talloc_free(mem_ctx
);
689 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
696 * Obtain a ldb message from a Python Dictionary object.
698 * @param mem_ctx Memory context
699 * @param py_obj Python Dictionary object
700 * @param ldb_ctx LDB context
701 * @param mod_flags Flags to be set on every message element
702 * @return ldb_message on success or NULL on failure
704 static struct ldb_message
*PyDict_AsMessage(TALLOC_CTX
*mem_ctx
,
706 struct ldb_context
*ldb_ctx
,
707 unsigned int mod_flags
)
709 struct ldb_message
*msg
;
710 unsigned int msg_pos
= 0;
711 Py_ssize_t dict_pos
= 0;
712 PyObject
*key
, *value
;
713 struct ldb_message_element
*msg_el
;
714 PyObject
*dn_value
= PyDict_GetItemString(py_obj
, "dn");
716 msg
= ldb_msg_new(mem_ctx
);
717 msg
->elements
= talloc_zero_array(msg
, struct ldb_message_element
, PyDict_Size(py_obj
));
720 if (!PyObject_AsDn(msg
, dn_value
, ldb_ctx
, &msg
->dn
)) {
721 PyErr_SetString(PyExc_TypeError
, "unable to import dn object");
724 if (msg
->dn
== NULL
) {
725 PyErr_SetString(PyExc_TypeError
, "dn set but not found");
729 PyErr_SetString(PyExc_TypeError
, "no dn set");
733 while (PyDict_Next(py_obj
, &dict_pos
, &key
, &value
)) {
734 char *key_str
= PyString_AsString(key
);
735 if (strcmp(key_str
, "dn") != 0) {
736 msg_el
= PyObject_AsMessageElement(msg
->elements
, value
,
738 if (msg_el
== NULL
) {
739 PyErr_SetString(PyExc_TypeError
, "unable to import element");
742 memcpy(&msg
->elements
[msg_pos
], msg_el
, sizeof(*msg_el
));
747 msg
->num_elements
= msg_pos
;
752 static PyObject
*py_ldb_add(PyLdbObject
*self
, PyObject
*args
)
756 struct ldb_context
*ldb_ctx
;
757 struct ldb_request
*req
;
758 struct ldb_message
*msg
= NULL
;
759 PyObject
*py_controls
= Py_None
;
761 struct ldb_control
**parsed_controls
;
763 if (!PyArg_ParseTuple(args
, "O|O", &py_obj
, &py_controls
))
766 mem_ctx
= talloc_new(NULL
);
767 if (mem_ctx
== NULL
) {
771 ldb_ctx
= PyLdb_AsLdbContext(self
);
773 if (py_controls
== Py_None
) {
774 parsed_controls
= NULL
;
776 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
777 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
778 talloc_free(controls
);
781 if (PyLdbMessage_Check(py_obj
)) {
782 msg
= PyLdbMessage_AsMessage(py_obj
);
783 } else if (PyDict_Check(py_obj
)) {
784 msg
= PyDict_AsMessage(mem_ctx
, py_obj
, ldb_ctx
, LDB_FLAG_MOD_ADD
);
786 PyErr_SetString(PyExc_TypeError
,
787 "Dictionary or LdbMessage object expected!");
791 /* we should have a PyErr already set */
792 talloc_free(mem_ctx
);
796 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
797 if (ret
!= LDB_SUCCESS
) {
798 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
799 talloc_free(mem_ctx
);
803 ret
= ldb_build_add_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
804 NULL
, ldb_op_default_callback
, NULL
);
805 if (ret
!= LDB_SUCCESS
) {
806 PyErr_SetString(PyExc_TypeError
, "failed to build request");
807 talloc_free(mem_ctx
);
811 /* do request and autostart a transaction */
812 /* Then let's LDB handle the message error in case of pb as they are meaningful */
814 ret
= ldb_transaction_start(ldb_ctx
);
815 if (ret
!= LDB_SUCCESS
) {
816 talloc_free(mem_ctx
);
817 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
820 ret
= ldb_request(ldb_ctx
, req
);
821 if (ret
== LDB_SUCCESS
) {
822 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
825 if (ret
== LDB_SUCCESS
) {
826 ret
= ldb_transaction_commit(ldb_ctx
);
828 ldb_transaction_cancel(ldb_ctx
);
829 if (ldb_ctx
->err_string
== NULL
) {
830 /* no error string was setup by the backend */
831 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
835 talloc_free(mem_ctx
);
836 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
841 static PyObject
*py_ldb_delete(PyLdbObject
*self
, PyObject
*args
)
846 struct ldb_context
*ldb_ctx
;
847 struct ldb_request
*req
;
848 PyObject
*py_controls
= Py_None
;
850 struct ldb_control
**parsed_controls
;
852 if (!PyArg_ParseTuple(args
, "O|O", &py_dn
, &py_controls
))
855 mem_ctx
= talloc_new(NULL
);
856 if (mem_ctx
== NULL
) {
860 ldb_ctx
= PyLdb_AsLdbContext(self
);
862 if (py_controls
== Py_None
) {
863 parsed_controls
= NULL
;
865 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
866 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
867 talloc_free(controls
);
870 if (!PyObject_AsDn(mem_ctx
, py_dn
, ldb_ctx
, &dn
)) {
871 talloc_free(mem_ctx
);
875 ret
= ldb_build_del_req(&req
, ldb_ctx
, mem_ctx
, dn
, parsed_controls
,
876 NULL
, ldb_op_default_callback
, NULL
);
877 if (ret
!= LDB_SUCCESS
) {
878 PyErr_SetString(PyExc_TypeError
, "failed to build request");
879 talloc_free(mem_ctx
);
883 /* do request and autostart a transaction */
884 /* Then let's LDB handle the message error in case of pb as they are meaningful */
886 ret
= ldb_transaction_start(ldb_ctx
);
887 if (ret
!= LDB_SUCCESS
) {
888 talloc_free(mem_ctx
);
889 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
892 ret
= ldb_request(ldb_ctx
, req
);
893 if (ret
== LDB_SUCCESS
) {
894 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
897 if (ret
== LDB_SUCCESS
) {
898 ret
= ldb_transaction_commit(ldb_ctx
);
900 ldb_transaction_cancel(ldb_ctx
);
901 if (ldb_ctx
->err_string
== NULL
) {
902 /* no error string was setup by the backend */
903 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
907 talloc_free(mem_ctx
);
908 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
913 static PyObject
*py_ldb_rename(PyLdbObject
*self
, PyObject
*args
)
915 PyObject
*py_dn1
, *py_dn2
;
916 struct ldb_dn
*dn1
, *dn2
;
918 struct ldb_context
*ldb
;
920 PyObject
*py_controls
= Py_None
;
921 struct ldb_control
**parsed_controls
;
922 struct ldb_context
*ldb_ctx
;
923 struct ldb_request
*req
;
925 ldb_ctx
= PyLdb_AsLdbContext(self
);
927 if (!PyArg_ParseTuple(args
, "OO|O", &py_dn1
, &py_dn2
, &py_controls
))
931 mem_ctx
= talloc_new(NULL
);
932 if (mem_ctx
== NULL
) {
936 ldb
= PyLdb_AsLdbContext(self
);
938 if (py_controls
== Py_None
) {
939 parsed_controls
= NULL
;
941 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
942 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
943 talloc_free(controls
);
947 if (!PyObject_AsDn(mem_ctx
, py_dn1
, ldb
, &dn1
)) {
948 talloc_free(mem_ctx
);
952 if (!PyObject_AsDn(mem_ctx
, py_dn2
, ldb
, &dn2
)) {
953 talloc_free(mem_ctx
);
957 ret
= ldb_build_rename_req(&req
, ldb_ctx
, mem_ctx
, dn1
, dn2
, parsed_controls
,
958 NULL
, ldb_op_default_callback
, NULL
);
959 if (ret
!= LDB_SUCCESS
) {
960 PyErr_SetString(PyExc_TypeError
, "failed to build request");
961 talloc_free(mem_ctx
);
965 /* do request and autostart a transaction */
966 /* Then let's LDB handle the message error in case of pb as they are meaningful */
968 ret
= ldb_transaction_start(ldb_ctx
);
969 if (ret
!= LDB_SUCCESS
) {
970 talloc_free(mem_ctx
);
971 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
974 ret
= ldb_request(ldb_ctx
, req
);
975 if (ret
== LDB_SUCCESS
) {
976 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
979 if (ret
== LDB_SUCCESS
) {
980 ret
= ldb_transaction_commit(ldb_ctx
);
982 ldb_transaction_cancel(ldb_ctx
);
983 if (ldb_ctx
->err_string
== NULL
) {
984 /* no error string was setup by the backend */
985 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
989 talloc_free(mem_ctx
);
990 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
995 static PyObject
*py_ldb_schema_attribute_remove(PyLdbObject
*self
, PyObject
*args
)
998 if (!PyArg_ParseTuple(args
, "s", &name
))
1001 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self
), name
);
1006 static PyObject
*py_ldb_schema_attribute_add(PyLdbObject
*self
, PyObject
*args
)
1008 char *attribute
, *syntax
;
1011 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
1014 ret
= ldb_schema_attribute_add(PyLdb_AsLdbContext(self
), attribute
, flags
, syntax
);
1016 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
1021 static PyObject
*ldb_ldif_to_pyobject(struct ldb_ldif
*ldif
)
1026 /* We don't want this attached to the 'ldb' any more */
1027 return Py_BuildValue(discard_const_p(char, "(iO)"),
1029 PyLdbMessage_FromMessage(ldif
->msg
));
1034 static PyObject
*py_ldb_write_ldif(PyLdbObject
*self
, PyObject
*args
)
1038 struct ldb_ldif ldif
;
1041 TALLOC_CTX
*mem_ctx
;
1043 if (!PyArg_ParseTuple(args
, "Oi", &py_msg
, &changetype
))
1046 if (!PyLdbMessage_Check(py_msg
)) {
1047 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for msg");
1051 ldif
.msg
= PyLdbMessage_AsMessage(py_msg
);
1052 ldif
.changetype
= changetype
;
1054 mem_ctx
= talloc_new(NULL
);
1056 string
= ldb_ldif_write_string(PyLdb_AsLdbContext(self
), mem_ctx
, &ldif
);
1058 PyErr_SetString(PyExc_KeyError
, "Failed to generate LDIF");
1062 ret
= PyString_FromString(string
);
1064 talloc_free(mem_ctx
);
1069 static PyObject
*py_ldb_parse_ldif(PyLdbObject
*self
, PyObject
*args
)
1072 struct ldb_ldif
*ldif
;
1075 TALLOC_CTX
*mem_ctx
;
1077 if (!PyArg_ParseTuple(args
, "s", &s
))
1080 mem_ctx
= talloc_new(NULL
);
1085 list
= PyList_New(0);
1086 while (s
&& *s
!= '\0') {
1087 ldif
= ldb_ldif_read_string(self
->ldb_ctx
, &s
);
1088 talloc_steal(mem_ctx
, ldif
);
1090 PyList_Append(list
, ldb_ldif_to_pyobject(ldif
));
1092 PyErr_SetString(PyExc_ValueError
, "unable to parse ldif string");
1093 talloc_free(mem_ctx
);
1097 talloc_free(mem_ctx
); /* The pyobject already has a reference to the things it needs */
1098 return PyObject_GetIter(list
);
1101 static PyObject
*py_ldb_msg_diff(PyLdbObject
*self
, PyObject
*args
)
1104 PyObject
*py_msg_old
;
1105 PyObject
*py_msg_new
;
1106 struct ldb_message
*diff
;
1107 struct ldb_context
*ldb
;
1110 if (!PyArg_ParseTuple(args
, "OO", &py_msg_old
, &py_msg_new
))
1113 if (!PyLdbMessage_Check(py_msg_old
)) {
1114 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for old message");
1118 if (!PyLdbMessage_Check(py_msg_new
)) {
1119 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for new message");
1123 ldb
= PyLdb_AsLdbContext(self
);
1124 ldb_ret
= ldb_msg_difference(ldb
, ldb
,
1125 PyLdbMessage_AsMessage(py_msg_old
),
1126 PyLdbMessage_AsMessage(py_msg_new
),
1128 if (ldb_ret
!= LDB_SUCCESS
) {
1129 PyErr_SetString(PyExc_RuntimeError
, "Failed to generate the Ldb Message diff");
1133 py_ret
= PyLdbMessage_FromMessage(diff
);
1135 talloc_unlink(ldb
, diff
);
1140 static PyObject
*py_ldb_schema_format_value(PyLdbObject
*self
, PyObject
*args
)
1142 const struct ldb_schema_attribute
*a
;
1143 struct ldb_val old_val
;
1144 struct ldb_val new_val
;
1145 TALLOC_CTX
*mem_ctx
;
1150 if (!PyArg_ParseTuple(args
, "sO", &element_name
, &val
))
1153 mem_ctx
= talloc_new(NULL
);
1155 old_val
.data
= (uint8_t *)PyString_AsString(val
);
1156 old_val
.length
= PyString_Size(val
);
1158 a
= ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self
), element_name
);
1164 if (a
->syntax
->ldif_write_fn(PyLdb_AsLdbContext(self
), mem_ctx
, &old_val
, &new_val
) != 0) {
1165 talloc_free(mem_ctx
);
1169 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
1171 talloc_free(mem_ctx
);
1176 static PyObject
*py_ldb_search(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1178 PyObject
*py_base
= Py_None
;
1179 int scope
= LDB_SCOPE_DEFAULT
;
1181 PyObject
*py_attrs
= Py_None
;
1182 PyObject
*py_controls
= Py_None
;
1183 const char * const kwnames
[] = { "base", "scope", "expression", "attrs", "controls", NULL
};
1185 struct ldb_result
*res
;
1186 struct ldb_request
*req
;
1188 struct ldb_context
*ldb_ctx
;
1189 struct ldb_control
**parsed_controls
;
1190 struct ldb_dn
*base
;
1192 TALLOC_CTX
*mem_ctx
;
1194 /* type "int" rather than "enum" for "scope" is intentional */
1195 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OizOO",
1196 discard_const_p(char *, kwnames
),
1197 &py_base
, &scope
, &expr
, &py_attrs
, &py_controls
))
1201 mem_ctx
= talloc_new(NULL
);
1202 if (mem_ctx
== NULL
) {
1206 ldb_ctx
= PyLdb_AsLdbContext(self
);
1208 if (py_attrs
== Py_None
) {
1211 attrs
= PyList_AsStringList(mem_ctx
, py_attrs
, "attrs");
1212 if (attrs
== NULL
) {
1213 talloc_free(mem_ctx
);
1218 if (py_base
== Py_None
) {
1219 base
= ldb_get_default_basedn(ldb_ctx
);
1221 if (!PyObject_AsDn(ldb_ctx
, py_base
, ldb_ctx
, &base
)) {
1227 if (py_controls
== Py_None
) {
1228 parsed_controls
= NULL
;
1230 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1231 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1232 talloc_free(controls
);
1235 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1238 talloc_free(mem_ctx
);
1242 ret
= ldb_build_search_req(&req
, ldb_ctx
, mem_ctx
,
1249 ldb_search_default_callback
,
1252 if (ret
!= LDB_SUCCESS
) {
1253 talloc_free(mem_ctx
);
1254 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1258 talloc_steal(req
, attrs
);
1260 ret
= ldb_request(ldb_ctx
, req
);
1262 if (ret
== LDB_SUCCESS
) {
1263 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1266 if (ret
!= LDB_SUCCESS
) {
1267 talloc_free(mem_ctx
);
1268 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1272 py_ret
= PyLdbResult_FromResult(res
);
1274 talloc_free(mem_ctx
);
1279 static PyObject
*py_ldb_get_opaque(PyLdbObject
*self
, PyObject
*args
)
1284 if (!PyArg_ParseTuple(args
, "s", &name
))
1287 data
= ldb_get_opaque(PyLdb_AsLdbContext(self
), name
);
1292 /* FIXME: More interpretation */
1297 static PyObject
*py_ldb_set_opaque(PyLdbObject
*self
, PyObject
*args
)
1302 if (!PyArg_ParseTuple(args
, "sO", &name
, &data
))
1305 /* FIXME: More interpretation */
1307 ldb_set_opaque(PyLdb_AsLdbContext(self
), name
, data
);
1312 static PyObject
*py_ldb_modules(PyLdbObject
*self
)
1314 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1315 PyObject
*ret
= PyList_New(0);
1316 struct ldb_module
*mod
;
1318 for (mod
= ldb
->modules
; mod
; mod
= mod
->next
) {
1319 PyList_Append(ret
, PyLdbModule_FromModule(mod
));
1325 static PyObject
*py_ldb_sequence_number(PyLdbObject
*self
, PyObject
*args
)
1327 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1331 if (!PyArg_ParseTuple(args
, "i", &type
))
1334 /* FIXME: More interpretation */
1336 ret
= ldb_sequence_number(ldb
, type
, &value
);
1338 if (ret
!= LDB_SUCCESS
) {
1339 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
1342 return PyLong_FromLongLong(value
);
1344 static PyMethodDef py_ldb_methods
[] = {
1345 { "set_debug", (PyCFunction
)py_ldb_set_debug
, METH_VARARGS
,
1346 "S.set_debug(callback) -> None\n"
1347 "Set callback for LDB debug messages.\n"
1348 "The callback should accept a debug level and debug text." },
1349 { "set_create_perms", (PyCFunction
)py_ldb_set_create_perms
, METH_VARARGS
,
1350 "S.set_create_perms(mode) -> None\n"
1351 "Set mode to use when creating new LDB files." },
1352 { "set_modules_dir", (PyCFunction
)py_ldb_set_modules_dir
, METH_VARARGS
,
1353 "S.set_modules_dir(path) -> None\n"
1354 "Set path LDB should search for modules" },
1355 { "transaction_start", (PyCFunction
)py_ldb_transaction_start
, METH_NOARGS
,
1356 "S.transaction_start() -> None\n"
1357 "Start a new transaction." },
1358 { "transaction_prepare_commit", (PyCFunction
)py_ldb_transaction_prepare_commit
, METH_NOARGS
,
1359 "S.transaction_prepare_commit() -> None\n"
1360 "prepare to commit a new transaction (2-stage commit)." },
1361 { "transaction_commit", (PyCFunction
)py_ldb_transaction_commit
, METH_NOARGS
,
1362 "S.transaction_commit() -> None\n"
1363 "commit a new transaction." },
1364 { "transaction_cancel", (PyCFunction
)py_ldb_transaction_cancel
, METH_NOARGS
,
1365 "S.transaction_cancel() -> None\n"
1366 "cancel a new transaction." },
1367 { "setup_wellknown_attributes", (PyCFunction
)py_ldb_setup_wellknown_attributes
, METH_NOARGS
,
1369 { "get_root_basedn", (PyCFunction
)py_ldb_get_root_basedn
, METH_NOARGS
,
1371 { "get_schema_basedn", (PyCFunction
)py_ldb_get_schema_basedn
, METH_NOARGS
,
1373 { "get_default_basedn", (PyCFunction
)py_ldb_get_default_basedn
, METH_NOARGS
,
1375 { "get_config_basedn", (PyCFunction
)py_ldb_get_config_basedn
, METH_NOARGS
,
1377 { "connect", (PyCFunction
)py_ldb_connect
, METH_VARARGS
|METH_KEYWORDS
,
1378 "S.connect(url, flags=0, options=None) -> None\n"
1379 "Connect to a LDB URL." },
1380 { "modify", (PyCFunction
)py_ldb_modify
, METH_VARARGS
,
1381 "S.modify(message) -> None\n"
1382 "Modify an entry." },
1383 { "add", (PyCFunction
)py_ldb_add
, METH_VARARGS
,
1384 "S.add(message) -> None\n"
1386 { "delete", (PyCFunction
)py_ldb_delete
, METH_VARARGS
,
1387 "S.delete(dn) -> None\n"
1388 "Remove an entry." },
1389 { "rename", (PyCFunction
)py_ldb_rename
, METH_VARARGS
,
1390 "S.rename(old_dn, new_dn) -> None\n"
1391 "Rename an entry." },
1392 { "search", (PyCFunction
)py_ldb_search
, METH_VARARGS
|METH_KEYWORDS
,
1393 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1394 "Search in a database.\n"
1396 ":param base: Optional base DN to search\n"
1397 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1398 ":param expression: Optional search expression\n"
1399 ":param attrs: Attributes to return (defaults to all)\n"
1400 ":param controls: Optional list of controls\n"
1401 ":return: Iterator over Message objects\n"
1403 { "schema_attribute_remove", (PyCFunction
)py_ldb_schema_attribute_remove
, METH_VARARGS
,
1405 { "schema_attribute_add", (PyCFunction
)py_ldb_schema_attribute_add
, METH_VARARGS
,
1407 { "schema_format_value", (PyCFunction
)py_ldb_schema_format_value
, METH_VARARGS
,
1409 { "parse_ldif", (PyCFunction
)py_ldb_parse_ldif
, METH_VARARGS
,
1410 "S.parse_ldif(ldif) -> iter(messages)\n"
1411 "Parse a string formatted using LDIF." },
1412 { "write_ldif", (PyCFunction
)py_ldb_write_ldif
, METH_VARARGS
,
1413 "S.write_ldif(message, changetype) -> ldif\n"
1414 "Print the message as a string formatted using LDIF." },
1415 { "msg_diff", (PyCFunction
)py_ldb_msg_diff
, METH_VARARGS
,
1416 "S.msg_diff(Message) -> Message\n"
1417 "Return an LDB Message of the difference between two Message objects." },
1418 { "get_opaque", (PyCFunction
)py_ldb_get_opaque
, METH_VARARGS
,
1419 "S.get_opaque(name) -> value\n"
1420 "Get an opaque value set on this LDB connection. \n"
1421 ":note: The returned value may not be useful in Python."
1423 { "set_opaque", (PyCFunction
)py_ldb_set_opaque
, METH_VARARGS
,
1424 "S.set_opaque(name, value) -> None\n"
1425 "Set an opaque value on this LDB connection. \n"
1426 ":note: Passing incorrect values may cause crashes." },
1427 { "modules", (PyCFunction
)py_ldb_modules
, METH_NOARGS
,
1428 "S.modules() -> list\n"
1429 "Return the list of modules on this LDB connection " },
1430 { "sequence_number", (PyCFunction
)py_ldb_sequence_number
, METH_VARARGS
,
1431 "S.sequence_number(type) -> value\n"
1432 "Return the value of the sequence according to the requested type" },
1436 static PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
)
1438 PyLdbModuleObject
*ret
;
1440 ret
= (PyLdbModuleObject
*)PyLdbModule
.tp_alloc(&PyLdbModule
, 0);
1445 ret
->mem_ctx
= talloc_new(NULL
);
1446 ret
->mod
= talloc_reference(ret
->mem_ctx
, mod
);
1447 return (PyObject
*)ret
;
1450 static PyObject
*py_ldb_get_firstmodule(PyLdbObject
*self
, void *closure
)
1452 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self
)->modules
);
1455 static PyGetSetDef py_ldb_getset
[] = {
1456 { discard_const_p(char, "firstmodule"), (getter
)py_ldb_get_firstmodule
, NULL
, NULL
},
1460 static int py_ldb_contains(PyLdbObject
*self
, PyObject
*obj
)
1462 struct ldb_context
*ldb_ctx
= PyLdb_AsLdbContext(self
);
1464 struct ldb_result
*result
;
1468 if (!PyObject_AsDn(ldb_ctx
, obj
, ldb_ctx
, &dn
)) {
1472 ret
= ldb_search(ldb_ctx
, ldb_ctx
, &result
, dn
, LDB_SCOPE_BASE
, NULL
,
1474 if (ret
!= LDB_SUCCESS
) {
1475 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb_ctx
);
1479 count
= result
->count
;
1481 talloc_free(result
);
1484 PyErr_Format(PyExc_RuntimeError
,
1485 "Searching for [%s] dn gave %u results!",
1486 ldb_dn_get_linearized(dn
),
1494 static PySequenceMethods py_ldb_seq
= {
1495 .sq_contains
= (objobjproc
)py_ldb_contains
,
1498 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
)
1502 ret
= (PyLdbObject
*)PyLdb
.tp_alloc(&PyLdb
, 0);
1507 ret
->mem_ctx
= talloc_new(NULL
);
1508 ret
->ldb_ctx
= talloc_reference(ret
->mem_ctx
, ldb_ctx
);
1509 return (PyObject
*)ret
;
1512 static void py_ldb_dealloc(PyLdbObject
*self
)
1514 talloc_free(self
->mem_ctx
);
1515 self
->ob_type
->tp_free(self
);
1518 static PyTypeObject PyLdb
= {
1519 .tp_name
= "ldb.Ldb",
1520 .tp_methods
= py_ldb_methods
,
1521 .tp_repr
= (reprfunc
)py_ldb_repr
,
1522 .tp_new
= py_ldb_new
,
1523 .tp_init
= (initproc
)py_ldb_init
,
1524 .tp_dealloc
= (destructor
)py_ldb_dealloc
,
1525 .tp_getset
= py_ldb_getset
,
1526 .tp_getattro
= PyObject_GenericGetAttr
,
1527 .tp_basicsize
= sizeof(PyLdbObject
),
1528 .tp_doc
= "Connection to a LDB database.",
1529 .tp_as_sequence
= &py_ldb_seq
,
1530 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1533 static PyObject
*py_ldb_module_repr(PyLdbModuleObject
*self
)
1535 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self
)->ops
->name
);
1538 static PyObject
*py_ldb_module_str(PyLdbModuleObject
*self
)
1540 return PyString_FromString(PyLdbModule_AsModule(self
)->ops
->name
);
1543 static PyObject
*py_ldb_module_start_transaction(PyLdbModuleObject
*self
)
1545 PyLdbModule_AsModule(self
)->ops
->start_transaction(PyLdbModule_AsModule(self
));
1549 static PyObject
*py_ldb_module_end_transaction(PyLdbModuleObject
*self
)
1551 PyLdbModule_AsModule(self
)->ops
->end_transaction(PyLdbModule_AsModule(self
));
1555 static PyObject
*py_ldb_module_del_transaction(PyLdbModuleObject
*self
)
1557 PyLdbModule_AsModule(self
)->ops
->del_transaction(PyLdbModule_AsModule(self
));
1561 static PyObject
*py_ldb_module_search(PyLdbModuleObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1563 PyObject
*py_base
, *py_tree
, *py_attrs
, *py_ret
;
1565 struct ldb_request
*req
;
1566 const char * const kwnames
[] = { "base", "scope", "tree", "attrs", NULL
};
1567 struct ldb_module
*mod
;
1568 const char * const*attrs
;
1570 /* type "int" rather than "enum" for "scope" is intentional */
1571 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OiOO",
1572 discard_const_p(char *, kwnames
),
1573 &py_base
, &scope
, &py_tree
, &py_attrs
))
1578 if (py_attrs
== Py_None
) {
1581 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1586 ret
= ldb_build_search_req(&req
, mod
->ldb
, NULL
, PyLdbDn_AsDn(py_base
),
1587 scope
, NULL
/* expr */, attrs
,
1588 NULL
/* controls */, NULL
, NULL
, NULL
);
1590 talloc_steal(req
, attrs
);
1592 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1594 req
->op
.search
.res
= NULL
;
1596 ret
= mod
->ops
->search(mod
, req
);
1598 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1600 py_ret
= PyLdbResult_FromResult(req
->op
.search
.res
);
1608 static PyObject
*py_ldb_module_add(PyLdbModuleObject
*self
, PyObject
*args
)
1610 struct ldb_request
*req
;
1611 PyObject
*py_message
;
1613 struct ldb_module
*mod
;
1615 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1618 req
= talloc_zero(NULL
, struct ldb_request
);
1619 req
->operation
= LDB_ADD
;
1620 req
->op
.add
.message
= PyLdbMessage_AsMessage(py_message
);
1622 mod
= PyLdbModule_AsModule(self
);
1623 ret
= mod
->ops
->add(mod
, req
);
1625 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1630 static PyObject
*py_ldb_module_modify(PyLdbModuleObject
*self
, PyObject
*args
)
1633 struct ldb_request
*req
;
1634 PyObject
*py_message
;
1635 struct ldb_module
*mod
;
1637 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1640 req
= talloc_zero(NULL
, struct ldb_request
);
1641 req
->operation
= LDB_MODIFY
;
1642 req
->op
.mod
.message
= PyLdbMessage_AsMessage(py_message
);
1644 mod
= PyLdbModule_AsModule(self
);
1645 ret
= mod
->ops
->modify(mod
, req
);
1647 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1652 static PyObject
*py_ldb_module_delete(PyLdbModuleObject
*self
, PyObject
*args
)
1655 struct ldb_request
*req
;
1658 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
1661 req
= talloc_zero(NULL
, struct ldb_request
);
1662 req
->operation
= LDB_DELETE
;
1663 req
->op
.del
.dn
= PyLdbDn_AsDn(py_dn
);
1665 ret
= PyLdbModule_AsModule(self
)->ops
->del(PyLdbModule_AsModule(self
), req
);
1667 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1672 static PyObject
*py_ldb_module_rename(PyLdbModuleObject
*self
, PyObject
*args
)
1675 struct ldb_request
*req
;
1676 PyObject
*py_dn1
, *py_dn2
;
1678 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
1681 req
= talloc_zero(NULL
, struct ldb_request
);
1683 req
->operation
= LDB_RENAME
;
1684 req
->op
.rename
.olddn
= PyLdbDn_AsDn(py_dn1
);
1685 req
->op
.rename
.newdn
= PyLdbDn_AsDn(py_dn2
);
1687 ret
= PyLdbModule_AsModule(self
)->ops
->rename(PyLdbModule_AsModule(self
), req
);
1689 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1694 static PyMethodDef py_ldb_module_methods
[] = {
1695 { "search", (PyCFunction
)py_ldb_module_search
, METH_VARARGS
|METH_KEYWORDS
, NULL
},
1696 { "add", (PyCFunction
)py_ldb_module_add
, METH_VARARGS
, NULL
},
1697 { "modify", (PyCFunction
)py_ldb_module_modify
, METH_VARARGS
, NULL
},
1698 { "rename", (PyCFunction
)py_ldb_module_rename
, METH_VARARGS
, NULL
},
1699 { "delete", (PyCFunction
)py_ldb_module_delete
, METH_VARARGS
, NULL
},
1700 { "start_transaction", (PyCFunction
)py_ldb_module_start_transaction
, METH_NOARGS
, NULL
},
1701 { "end_transaction", (PyCFunction
)py_ldb_module_end_transaction
, METH_NOARGS
, NULL
},
1702 { "del_transaction", (PyCFunction
)py_ldb_module_del_transaction
, METH_NOARGS
, NULL
},
1706 static void py_ldb_module_dealloc(PyLdbModuleObject
*self
)
1708 talloc_free(self
->mem_ctx
);
1712 static PyTypeObject PyLdbModule
= {
1713 .tp_name
= "ldb.LdbModule",
1714 .tp_methods
= py_ldb_module_methods
,
1715 .tp_repr
= (reprfunc
)py_ldb_module_repr
,
1716 .tp_str
= (reprfunc
)py_ldb_module_str
,
1717 .tp_basicsize
= sizeof(PyLdbModuleObject
),
1718 .tp_dealloc
= (destructor
)py_ldb_module_dealloc
,
1719 .tp_flags
= Py_TPFLAGS_DEFAULT
,
1724 * Create a ldb_message_element from a Python object.
1726 * This will accept any sequence objects that contains strings, or
1729 * A reference to set_obj will be borrowed.
1731 * @param mem_ctx Memory context
1732 * @param set_obj Python object to convert
1733 * @param flags ldb_message_element flags to set
1734 * @param attr_name Name of the attribute
1735 * @return New ldb_message_element, allocated as child of mem_ctx
1737 static struct ldb_message_element
*PyObject_AsMessageElement(
1738 TALLOC_CTX
*mem_ctx
,
1741 const char *attr_name
)
1743 struct ldb_message_element
*me
;
1745 if (PyLdbMessageElement_Check(set_obj
)) {
1746 PyLdbMessageElementObject
*set_obj_as_me
= (PyLdbMessageElementObject
*)set_obj
;
1747 /* We have to talloc_reference() the memory context, not the pointer
1748 * which may not actually be it's own context */
1749 if (talloc_reference(mem_ctx
, set_obj_as_me
->mem_ctx
)) {
1750 return PyLdbMessageElement_AsMessageElement(set_obj
);
1755 me
= talloc(mem_ctx
, struct ldb_message_element
);
1761 me
->name
= talloc_strdup(me
, attr_name
);
1763 if (PyString_Check(set_obj
)) {
1765 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1766 me
->values
[0].length
= PyString_Size(set_obj
);
1767 me
->values
[0].data
= talloc_memdup(me
,
1768 (uint8_t *)PyString_AsString(set_obj
), me
->values
[0].length
+1);
1769 } else if (PySequence_Check(set_obj
)) {
1771 me
->num_values
= PySequence_Size(set_obj
);
1772 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1773 for (i
= 0; i
< me
->num_values
; i
++) {
1774 PyObject
*obj
= PySequence_GetItem(set_obj
, i
);
1775 if (!PyString_Check(obj
)) {
1776 PyErr_Format(PyExc_TypeError
,
1777 "Expected string as element %zd in list", i
);
1782 me
->values
[i
].length
= PyString_Size(obj
);
1783 me
->values
[i
].data
= talloc_memdup(me
,
1784 (uint8_t *)PyString_AsString(obj
), me
->values
[i
].length
+1);
1795 static PyObject
*ldb_msg_element_to_set(struct ldb_context
*ldb_ctx
,
1796 struct ldb_message_element
*me
)
1801 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1802 result
= PyList_New(me
->num_values
);
1804 for (i
= 0; i
< me
->num_values
; i
++) {
1805 PyList_SetItem(result
, i
,
1806 PyObject_FromLdbValue(ldb_ctx
, me
, &me
->values
[i
]));
1812 static PyObject
*py_ldb_msg_element_get(PyLdbMessageElementObject
*self
, PyObject
*args
)
1815 if (!PyArg_ParseTuple(args
, "I", &i
))
1817 if (i
>= PyLdbMessageElement_AsMessageElement(self
)->num_values
)
1820 return PyObject_FromLdbValue(NULL
, PyLdbMessageElement_AsMessageElement(self
),
1821 &(PyLdbMessageElement_AsMessageElement(self
)->values
[i
]));
1824 static PyObject
*py_ldb_msg_element_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1826 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1827 return PyInt_FromLong(el
->flags
);
1830 static PyObject
*py_ldb_msg_element_set_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1833 struct ldb_message_element
*el
;
1834 if (!PyArg_ParseTuple(args
, "i", &flags
))
1837 el
= PyLdbMessageElement_AsMessageElement(self
);
1842 static PyMethodDef py_ldb_msg_element_methods
[] = {
1843 { "get", (PyCFunction
)py_ldb_msg_element_get
, METH_VARARGS
, NULL
},
1844 { "set_flags", (PyCFunction
)py_ldb_msg_element_set_flags
, METH_VARARGS
, NULL
},
1845 { "flags", (PyCFunction
)py_ldb_msg_element_flags
, METH_NOARGS
, NULL
},
1849 static Py_ssize_t
py_ldb_msg_element_len(PyLdbMessageElementObject
*self
)
1851 return PyLdbMessageElement_AsMessageElement(self
)->num_values
;
1854 static PyObject
*py_ldb_msg_element_find(PyLdbMessageElementObject
*self
, Py_ssize_t idx
)
1856 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1857 if (idx
< 0 || idx
>= el
->num_values
) {
1858 PyErr_SetString(PyExc_IndexError
, "Out of range");
1861 return PyString_FromStringAndSize((char *)el
->values
[idx
].data
, el
->values
[idx
].length
);
1864 static PySequenceMethods py_ldb_msg_element_seq
= {
1865 .sq_length
= (lenfunc
)py_ldb_msg_element_len
,
1866 .sq_item
= (ssizeargfunc
)py_ldb_msg_element_find
,
1869 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject
*self
, PyLdbMessageElementObject
*other
)
1871 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self
),
1872 PyLdbMessageElement_AsMessageElement(other
));
1875 static PyObject
*py_ldb_msg_element_iter(PyLdbMessageElementObject
*self
)
1877 return PyObject_GetIter(ldb_msg_element_to_set(NULL
, PyLdbMessageElement_AsMessageElement(self
)));
1880 static PyObject
*PyLdbMessageElement_FromMessageElement(struct ldb_message_element
*el
, TALLOC_CTX
*mem_ctx
)
1882 PyLdbMessageElementObject
*ret
;
1883 ret
= PyObject_New(PyLdbMessageElementObject
, &PyLdbMessageElement
);
1888 ret
->mem_ctx
= talloc_new(NULL
);
1889 if (talloc_reference(ret
->mem_ctx
, mem_ctx
) == NULL
) {
1894 return (PyObject
*)ret
;
1897 static PyObject
*py_ldb_msg_element_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1899 PyObject
*py_elements
= NULL
;
1900 struct ldb_message_element
*el
;
1903 const char * const kwnames
[] = { "elements", "flags", "name", NULL
};
1904 PyLdbMessageElementObject
*ret
;
1905 TALLOC_CTX
*mem_ctx
;
1907 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ois",
1908 discard_const_p(char *, kwnames
),
1909 &py_elements
, &flags
, &name
))
1912 mem_ctx
= talloc_new(NULL
);
1913 if (mem_ctx
== NULL
) {
1918 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
1921 talloc_free(mem_ctx
);
1925 if (py_elements
!= NULL
) {
1927 if (PyString_Check(py_elements
)) {
1929 el
->values
= talloc_array(el
, struct ldb_val
, 1);
1930 if (el
->values
== NULL
) {
1931 talloc_free(mem_ctx
);
1935 el
->values
[0].length
= PyString_Size(py_elements
);
1936 el
->values
[0].data
= talloc_memdup(el
->values
,
1937 (uint8_t *)PyString_AsString(py_elements
), el
->values
[0].length
+1);
1938 } else if (PySequence_Check(py_elements
)) {
1939 el
->num_values
= PySequence_Size(py_elements
);
1940 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
1941 if (el
->values
== NULL
) {
1942 talloc_free(mem_ctx
);
1946 for (i
= 0; i
< el
->num_values
; i
++) {
1947 PyObject
*item
= PySequence_GetItem(py_elements
, i
);
1949 talloc_free(mem_ctx
);
1952 if (!PyString_Check(item
)) {
1953 PyErr_Format(PyExc_TypeError
,
1954 "Expected string as element %zd in list", i
);
1955 talloc_free(mem_ctx
);
1958 el
->values
[i
].length
= PyString_Size(item
);
1959 el
->values
[i
].data
= talloc_memdup(el
,
1960 (uint8_t *)PyString_AsString(item
), el
->values
[i
].length
+1);
1963 PyErr_SetString(PyExc_TypeError
,
1964 "Expected string or list");
1965 talloc_free(mem_ctx
);
1971 el
->name
= talloc_strdup(el
, name
);
1973 ret
= PyObject_New(PyLdbMessageElementObject
, type
);
1975 talloc_free(mem_ctx
);
1979 ret
->mem_ctx
= mem_ctx
;
1981 return (PyObject
*)ret
;
1984 static PyObject
*py_ldb_msg_element_repr(PyLdbMessageElementObject
*self
)
1986 char *element_str
= NULL
;
1988 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1991 for (i
= 0; i
< el
->num_values
; i
++) {
1992 PyObject
*o
= py_ldb_msg_element_find(self
, i
);
1993 if (element_str
== NULL
)
1994 element_str
= talloc_strdup(NULL
, PyObject_REPR(o
));
1996 element_str
= talloc_asprintf_append(element_str
, ",%s", PyObject_REPR(o
));
1999 if (element_str
!= NULL
) {
2000 ret
= PyString_FromFormat("MessageElement([%s])", element_str
);
2001 talloc_free(element_str
);
2003 ret
= PyString_FromString("MessageElement([])");
2009 static PyObject
*py_ldb_msg_element_str(PyLdbMessageElementObject
*self
)
2011 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2013 if (el
->num_values
== 1)
2014 return PyString_FromStringAndSize((char *)el
->values
[0].data
, el
->values
[0].length
);
2019 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject
*self
)
2021 talloc_free(self
->mem_ctx
);
2025 static PyTypeObject PyLdbMessageElement
= {
2026 .tp_name
= "ldb.MessageElement",
2027 .tp_basicsize
= sizeof(PyLdbMessageElementObject
),
2028 .tp_dealloc
= (destructor
)py_ldb_msg_element_dealloc
,
2029 .tp_repr
= (reprfunc
)py_ldb_msg_element_repr
,
2030 .tp_str
= (reprfunc
)py_ldb_msg_element_str
,
2031 .tp_methods
= py_ldb_msg_element_methods
,
2032 .tp_compare
= (cmpfunc
)py_ldb_msg_element_cmp
,
2033 .tp_iter
= (getiterfunc
)py_ldb_msg_element_iter
,
2034 .tp_as_sequence
= &py_ldb_msg_element_seq
,
2035 .tp_new
= py_ldb_msg_element_new
,
2036 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2040 static PyObject
*py_ldb_msg_from_dict(PyTypeObject
*type
, PyObject
*args
)
2045 struct ldb_message
*msg
;
2046 struct ldb_context
*ldb_ctx
;
2047 unsigned int mod_flags
= LDB_FLAG_MOD_REPLACE
;
2049 if (!PyArg_ParseTuple(args
, "O!O!|I",
2050 &PyLdb
, &py_ldb
, &PyDict_Type
, &py_dict
,
2055 /* mask only flags we are going to use */
2056 mod_flags
= LDB_FLAG_MOD_TYPE(mod_flags
);
2058 PyErr_SetString(PyExc_ValueError
,
2059 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2060 " expected as mod_flag value");
2064 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
2066 msg
= PyDict_AsMessage(ldb_ctx
, py_dict
, ldb_ctx
, mod_flags
);
2071 py_ret
= PyLdbMessage_FromMessage(msg
);
2073 talloc_unlink(ldb_ctx
, msg
);
2078 static PyObject
*py_ldb_msg_remove_attr(PyLdbMessageObject
*self
, PyObject
*args
)
2081 if (!PyArg_ParseTuple(args
, "s", &name
))
2084 ldb_msg_remove_attr(self
->msg
, name
);
2089 static PyObject
*py_ldb_msg_keys(PyLdbMessageObject
*self
)
2091 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2092 Py_ssize_t i
, j
= 0;
2093 PyObject
*obj
= PyList_New(msg
->num_elements
+(msg
->dn
!= NULL
?1:0));
2094 if (msg
->dn
!= NULL
) {
2095 PyList_SetItem(obj
, j
, PyString_FromString("dn"));
2098 for (i
= 0; i
< msg
->num_elements
; i
++) {
2099 PyList_SetItem(obj
, j
, PyString_FromString(msg
->elements
[i
].name
));
2105 static PyObject
*py_ldb_msg_getitem_helper(PyLdbMessageObject
*self
, PyObject
*py_name
)
2107 struct ldb_message_element
*el
;
2109 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2110 if (!PyString_Check(py_name
)) {
2111 PyErr_SetNone(PyExc_TypeError
);
2114 name
= PyString_AsString(py_name
);
2115 if (!strcmp(name
, "dn"))
2116 return PyLdbDn_FromDn(msg
->dn
);
2117 el
= ldb_msg_find_element(msg
, name
);
2121 return (PyObject
*)PyLdbMessageElement_FromMessageElement(el
, msg
->elements
);
2124 static PyObject
*py_ldb_msg_getitem(PyLdbMessageObject
*self
, PyObject
*py_name
)
2126 PyObject
*ret
= py_ldb_msg_getitem_helper(self
, py_name
);
2128 PyErr_SetString(PyExc_KeyError
, "No such element");
2134 static PyObject
*py_ldb_msg_get(PyLdbMessageObject
*self
, PyObject
*args
)
2136 PyObject
*name
, *ret
;
2137 if (!PyArg_ParseTuple(args
, "O", &name
))
2140 ret
= py_ldb_msg_getitem_helper(self
, name
);
2142 if (PyErr_Occurred())
2149 static PyObject
*py_ldb_msg_items(PyLdbMessageObject
*self
)
2151 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2152 Py_ssize_t i
, j
= 0;
2153 PyObject
*l
= PyList_New(msg
->num_elements
+ (msg
->dn
== NULL
?0:1));
2154 if (msg
->dn
!= NULL
) {
2155 PyList_SetItem(l
, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg
->dn
)));
2158 for (i
= 0; i
< msg
->num_elements
; i
++, j
++) {
2159 PyObject
*py_el
= PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
);
2160 PyObject
*value
= Py_BuildValue("(sO)", msg
->elements
[i
].name
, py_el
);
2161 PyList_SetItem(l
, j
, value
);
2166 static PyObject
*py_ldb_msg_elements(PyLdbMessageObject
*self
)
2168 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2170 PyObject
*l
= PyList_New(msg
->num_elements
);
2171 for (i
= 0; i
< msg
->num_elements
; i
++) {
2172 PyList_SetItem(l
, i
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
));
2177 static PyObject
*py_ldb_msg_add(PyLdbMessageObject
*self
, PyObject
*args
)
2179 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2180 PyLdbMessageElementObject
*py_element
;
2182 struct ldb_message_element
*el
;
2184 if (!PyArg_ParseTuple(args
, "O!", &PyLdbMessageElement
, &py_element
))
2187 el
= talloc_reference(msg
, py_element
->mem_ctx
);
2193 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2194 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2199 static PyMethodDef py_ldb_msg_methods
[] = {
2200 { "from_dict", (PyCFunction
)py_ldb_msg_from_dict
, METH_CLASS
| METH_VARARGS
,
2201 "Message.from_dict(ldb, dict, mod_flag=FLAG_MOD_REPLACE) -> ldb.Message\n"
2202 "Class method to create ldb.Message object from Dictionary.\n"
2203 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE."},
2204 { "keys", (PyCFunction
)py_ldb_msg_keys
, METH_NOARGS
,
2205 "S.keys() -> list\n\n"
2206 "Return sequence of all attribute names." },
2207 { "remove", (PyCFunction
)py_ldb_msg_remove_attr
, METH_VARARGS
,
2208 "S.remove(name)\n\n"
2209 "Remove all entries for attributes with the specified name."},
2210 { "get", (PyCFunction
)py_ldb_msg_get
, METH_VARARGS
, NULL
},
2211 { "items", (PyCFunction
)py_ldb_msg_items
, METH_NOARGS
, NULL
},
2212 { "elements", (PyCFunction
)py_ldb_msg_elements
, METH_NOARGS
, NULL
},
2213 { "add", (PyCFunction
)py_ldb_msg_add
, METH_VARARGS
,
2214 "S.append(element)\n\n"
2215 "Add an element to this message." },
2219 static PyObject
*py_ldb_msg_iter(PyLdbMessageObject
*self
)
2221 PyObject
*list
, *iter
;
2223 list
= py_ldb_msg_keys(self
);
2224 iter
= PyObject_GetIter(list
);
2229 static int py_ldb_msg_setitem(PyLdbMessageObject
*self
, PyObject
*name
, PyObject
*value
)
2233 if (!PyString_Check(name
)) {
2234 PyErr_SetNone(PyExc_TypeError
);
2238 attr_name
= PyString_AsString(name
);
2239 if (value
== NULL
) {
2241 ldb_msg_remove_attr(self
->msg
, attr_name
);
2243 struct ldb_message_element
*el
= PyObject_AsMessageElement(self
->msg
,
2244 value
, 0, attr_name
);
2247 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self
), attr_name
);
2248 ldb_msg_add(PyLdbMessage_AsMessage(self
), el
, el
->flags
);
2253 static Py_ssize_t
py_ldb_msg_length(PyLdbMessageObject
*self
)
2255 return PyLdbMessage_AsMessage(self
)->num_elements
;
2258 static PyMappingMethods py_ldb_msg_mapping
= {
2259 .mp_length
= (lenfunc
)py_ldb_msg_length
,
2260 .mp_subscript
= (binaryfunc
)py_ldb_msg_getitem
,
2261 .mp_ass_subscript
= (objobjargproc
)py_ldb_msg_setitem
,
2264 static PyObject
*py_ldb_msg_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
2266 const char * const kwnames
[] = { "dn", NULL
};
2267 struct ldb_message
*ret
;
2268 TALLOC_CTX
*mem_ctx
;
2269 PyObject
*pydn
= NULL
;
2270 PyLdbMessageObject
*py_ret
;
2272 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O",
2273 discard_const_p(char *, kwnames
),
2277 mem_ctx
= talloc_new(NULL
);
2278 if (mem_ctx
== NULL
) {
2283 ret
= ldb_msg_new(mem_ctx
);
2285 talloc_free(mem_ctx
);
2292 if (!PyObject_AsDn(NULL
, pydn
, NULL
, &dn
)) {
2293 talloc_free(mem_ctx
);
2296 ret
->dn
= talloc_reference(ret
, dn
);
2299 py_ret
= (PyLdbMessageObject
*)type
->tp_alloc(type
, 0);
2300 if (py_ret
== NULL
) {
2302 talloc_free(mem_ctx
);
2306 py_ret
->mem_ctx
= mem_ctx
;
2308 return (PyObject
*)py_ret
;
2311 static PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
)
2313 PyLdbMessageObject
*ret
;
2315 ret
= (PyLdbMessageObject
*)PyLdbMessage
.tp_alloc(&PyLdbMessage
, 0);
2320 ret
->mem_ctx
= talloc_new(NULL
);
2321 ret
->msg
= talloc_reference(ret
->mem_ctx
, msg
);
2322 return (PyObject
*)ret
;
2325 static PyObject
*py_ldb_msg_get_dn(PyLdbMessageObject
*self
, void *closure
)
2327 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2328 return PyLdbDn_FromDn(msg
->dn
);
2331 static int py_ldb_msg_set_dn(PyLdbMessageObject
*self
, PyObject
*value
, void *closure
)
2333 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2334 if (!PyLdbDn_Check(value
)) {
2335 PyErr_SetNone(PyExc_TypeError
);
2339 msg
->dn
= talloc_reference(msg
, PyLdbDn_AsDn(value
));
2343 static PyGetSetDef py_ldb_msg_getset
[] = {
2344 { discard_const_p(char, "dn"), (getter
)py_ldb_msg_get_dn
, (setter
)py_ldb_msg_set_dn
, NULL
},
2348 static PyObject
*py_ldb_msg_repr(PyLdbMessageObject
*self
)
2350 PyObject
*dict
= PyDict_New(), *ret
;
2351 if (PyDict_Update(dict
, (PyObject
*)self
) != 0)
2353 ret
= PyString_FromFormat("Message(%s)", PyObject_REPR(dict
));
2358 static void py_ldb_msg_dealloc(PyLdbMessageObject
*self
)
2360 talloc_free(self
->mem_ctx
);
2364 static int py_ldb_msg_compare(PyLdbMessageObject
*py_msg1
,
2365 PyLdbMessageObject
*py_msg2
)
2367 struct ldb_message
*msg1
= PyLdbMessage_AsMessage(py_msg1
),
2368 *msg2
= PyLdbMessage_AsMessage(py_msg2
);
2372 if ((msg1
->dn
!= NULL
) || (msg2
->dn
!= NULL
)) {
2373 ret
= ldb_dn_compare(msg1
->dn
, msg2
->dn
);
2379 ret
= msg1
->num_elements
- msg2
->num_elements
;
2384 for (i
= 0; i
< msg1
->num_elements
; i
++) {
2385 ret
= ldb_msg_element_compare_name(&msg1
->elements
[i
],
2386 &msg2
->elements
[i
]);
2391 ret
= ldb_msg_element_compare(&msg1
->elements
[i
],
2392 &msg2
->elements
[i
]);
2401 static PyTypeObject PyLdbMessage
= {
2402 .tp_name
= "ldb.Message",
2403 .tp_methods
= py_ldb_msg_methods
,
2404 .tp_getset
= py_ldb_msg_getset
,
2405 .tp_as_mapping
= &py_ldb_msg_mapping
,
2406 .tp_basicsize
= sizeof(PyLdbMessageObject
),
2407 .tp_dealloc
= (destructor
)py_ldb_msg_dealloc
,
2408 .tp_new
= py_ldb_msg_new
,
2409 .tp_repr
= (reprfunc
)py_ldb_msg_repr
,
2410 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2411 .tp_iter
= (getiterfunc
)py_ldb_msg_iter
,
2412 .tp_compare
= (cmpfunc
)py_ldb_msg_compare
,
2415 static PyObject
*PyLdbTree_FromTree(struct ldb_parse_tree
*tree
)
2417 PyLdbTreeObject
*ret
;
2419 ret
= (PyLdbTreeObject
*)PyLdbTree
.tp_alloc(&PyLdbTree
, 0);
2425 ret
->mem_ctx
= talloc_new(NULL
);
2426 ret
->tree
= talloc_reference(ret
->mem_ctx
, tree
);
2427 return (PyObject
*)ret
;
2430 static void py_ldb_tree_dealloc(PyLdbTreeObject
*self
)
2432 talloc_free(self
->mem_ctx
);
2436 static PyTypeObject PyLdbTree
= {
2437 .tp_name
= "ldb.Tree",
2438 .tp_basicsize
= sizeof(PyLdbTreeObject
),
2439 .tp_dealloc
= (destructor
)py_ldb_tree_dealloc
,
2440 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2444 static int py_module_search(struct ldb_module
*mod
, struct ldb_request
*req
)
2446 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2447 PyObject
*py_result
, *py_base
, *py_attrs
, *py_tree
;
2449 py_base
= PyLdbDn_FromDn(req
->op
.search
.base
);
2451 if (py_base
== NULL
)
2452 return LDB_ERR_OPERATIONS_ERROR
;
2454 py_tree
= PyLdbTree_FromTree(req
->op
.search
.tree
);
2456 if (py_tree
== NULL
)
2457 return LDB_ERR_OPERATIONS_ERROR
;
2459 if (req
->op
.search
.attrs
== NULL
) {
2463 for (len
= 0; req
->op
.search
.attrs
[len
]; len
++);
2464 py_attrs
= PyList_New(len
);
2465 for (i
= 0; i
< len
; i
++)
2466 PyList_SetItem(py_attrs
, i
, PyString_FromString(req
->op
.search
.attrs
[i
]));
2469 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "search"),
2470 discard_const_p(char, "OiOO"),
2471 py_base
, req
->op
.search
.scope
, py_tree
, py_attrs
);
2473 Py_DECREF(py_attrs
);
2477 if (py_result
== NULL
) {
2478 return LDB_ERR_PYTHON_EXCEPTION
;
2481 req
->op
.search
.res
= PyLdbResult_AsResult(NULL
, py_result
);
2482 if (req
->op
.search
.res
== NULL
) {
2483 return LDB_ERR_PYTHON_EXCEPTION
;
2486 Py_DECREF(py_result
);
2491 static int py_module_add(struct ldb_module
*mod
, struct ldb_request
*req
)
2493 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2494 PyObject
*py_result
, *py_msg
;
2496 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.add
.message
));
2498 if (py_msg
== NULL
) {
2499 return LDB_ERR_OPERATIONS_ERROR
;
2502 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "add"),
2503 discard_const_p(char, "O"),
2508 if (py_result
== NULL
) {
2509 return LDB_ERR_PYTHON_EXCEPTION
;
2512 Py_DECREF(py_result
);
2517 static int py_module_modify(struct ldb_module
*mod
, struct ldb_request
*req
)
2519 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2520 PyObject
*py_result
, *py_msg
;
2522 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.mod
.message
));
2524 if (py_msg
== NULL
) {
2525 return LDB_ERR_OPERATIONS_ERROR
;
2528 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "modify"),
2529 discard_const_p(char, "O"),
2534 if (py_result
== NULL
) {
2535 return LDB_ERR_PYTHON_EXCEPTION
;
2538 Py_DECREF(py_result
);
2543 static int py_module_del(struct ldb_module
*mod
, struct ldb_request
*req
)
2545 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2546 PyObject
*py_result
, *py_dn
;
2548 py_dn
= PyLdbDn_FromDn(req
->op
.del
.dn
);
2551 return LDB_ERR_OPERATIONS_ERROR
;
2553 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "delete"),
2554 discard_const_p(char, "O"),
2557 if (py_result
== NULL
) {
2558 return LDB_ERR_PYTHON_EXCEPTION
;
2561 Py_DECREF(py_result
);
2566 static int py_module_rename(struct ldb_module
*mod
, struct ldb_request
*req
)
2568 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2569 PyObject
*py_result
, *py_olddn
, *py_newdn
;
2571 py_olddn
= PyLdbDn_FromDn(req
->op
.rename
.olddn
);
2573 if (py_olddn
== NULL
)
2574 return LDB_ERR_OPERATIONS_ERROR
;
2576 py_newdn
= PyLdbDn_FromDn(req
->op
.rename
.newdn
);
2578 if (py_newdn
== NULL
)
2579 return LDB_ERR_OPERATIONS_ERROR
;
2581 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "rename"),
2582 discard_const_p(char, "OO"),
2583 py_olddn
, py_newdn
);
2585 Py_DECREF(py_olddn
);
2586 Py_DECREF(py_newdn
);
2588 if (py_result
== NULL
) {
2589 return LDB_ERR_PYTHON_EXCEPTION
;
2592 Py_DECREF(py_result
);
2597 static int py_module_request(struct ldb_module
*mod
, struct ldb_request
*req
)
2599 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2600 PyObject
*py_result
;
2602 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "request"),
2603 discard_const_p(char, ""));
2605 return LDB_ERR_OPERATIONS_ERROR
;
2608 static int py_module_extended(struct ldb_module
*mod
, struct ldb_request
*req
)
2610 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2611 PyObject
*py_result
;
2613 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "extended"),
2614 discard_const_p(char, ""));
2616 return LDB_ERR_OPERATIONS_ERROR
;
2619 static int py_module_start_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, "start_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_end_transaction(struct ldb_module
*mod
)
2638 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2639 PyObject
*py_result
;
2641 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "end_transaction"),
2642 discard_const_p(char, ""));
2644 if (py_result
== NULL
) {
2645 return LDB_ERR_PYTHON_EXCEPTION
;
2648 Py_DECREF(py_result
);
2653 static int py_module_del_transaction(struct ldb_module
*mod
)
2655 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2656 PyObject
*py_result
;
2658 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "del_transaction"),
2659 discard_const_p(char, ""));
2661 if (py_result
== NULL
) {
2662 return LDB_ERR_PYTHON_EXCEPTION
;
2665 Py_DECREF(py_result
);
2670 static int py_module_destructor(struct ldb_module
*mod
)
2672 Py_DECREF((PyObject
*)mod
->private_data
);
2676 static int py_module_init(struct ldb_module
*mod
)
2678 PyObject
*py_class
= (PyObject
*)mod
->ops
->private_data
;
2679 PyObject
*py_result
, *py_next
, *py_ldb
;
2681 py_ldb
= PyLdb_FromLdbContext(mod
->ldb
);
2684 return LDB_ERR_OPERATIONS_ERROR
;
2686 py_next
= PyLdbModule_FromModule(mod
->next
);
2688 if (py_next
== NULL
)
2689 return LDB_ERR_OPERATIONS_ERROR
;
2691 py_result
= PyObject_CallFunction(py_class
, discard_const_p(char, "OO"),
2694 if (py_result
== NULL
) {
2695 return LDB_ERR_PYTHON_EXCEPTION
;
2698 mod
->private_data
= py_result
;
2700 talloc_set_destructor(mod
, py_module_destructor
);
2702 return ldb_next_init(mod
);
2705 static PyObject
*py_register_module(PyObject
*module
, PyObject
*args
)
2708 struct ldb_module_ops
*ops
;
2711 if (!PyArg_ParseTuple(args
, "O", &input
))
2714 ops
= talloc_zero(talloc_autofree_context(), struct ldb_module_ops
);
2720 ops
->name
= talloc_strdup(ops
, PyString_AsString(PyObject_GetAttrString(input
, discard_const_p(char, "name"))));
2723 ops
->private_data
= input
;
2724 ops
->init_context
= py_module_init
;
2725 ops
->search
= py_module_search
;
2726 ops
->add
= py_module_add
;
2727 ops
->modify
= py_module_modify
;
2728 ops
->del
= py_module_del
;
2729 ops
->rename
= py_module_rename
;
2730 ops
->request
= py_module_request
;
2731 ops
->extended
= py_module_extended
;
2732 ops
->start_transaction
= py_module_start_transaction
;
2733 ops
->end_transaction
= py_module_end_transaction
;
2734 ops
->del_transaction
= py_module_del_transaction
;
2736 ret
= ldb_register_module(ops
);
2738 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2743 static PyObject
*py_timestring(PyObject
*module
, PyObject
*args
)
2745 /* most times "time_t" is a signed integer type with 32 or 64 bit:
2746 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
2750 if (!PyArg_ParseTuple(args
, "l", &t_val
))
2752 tresult
= ldb_timestring(NULL
, (time_t) t_val
);
2753 ret
= PyString_FromString(tresult
);
2754 talloc_free(tresult
);
2758 static PyObject
*py_string_to_time(PyObject
*module
, PyObject
*args
)
2761 if (!PyArg_ParseTuple(args
, "s", &str
))
2764 return PyInt_FromLong(ldb_string_to_time(str
));
2767 static PyObject
*py_valid_attr_name(PyObject
*self
, PyObject
*args
)
2770 if (!PyArg_ParseTuple(args
, "s", &name
))
2772 return PyBool_FromLong(ldb_valid_attr_name(name
));
2775 static PyMethodDef py_ldb_global_methods
[] = {
2776 { "register_module", py_register_module
, METH_VARARGS
,
2777 "S.register_module(module) -> None\n"
2778 "Register a LDB module."},
2779 { "timestring", py_timestring
, METH_VARARGS
,
2780 "S.timestring(int) -> string\n"
2781 "Generate a LDAP time string from a UNIX timestamp" },
2782 { "string_to_time", py_string_to_time
, METH_VARARGS
,
2783 "S.string_to_time(string) -> int\n"
2784 "Parse a LDAP time string into a UNIX timestamp." },
2785 { "valid_attr_name", py_valid_attr_name
, METH_VARARGS
,
2786 "S.valid_attr_name(name) -> bool\n"
2787 "Check whether the supplied name is a valid attribute name." },
2788 { "open", (PyCFunction
)py_ldb_new
, METH_VARARGS
|METH_KEYWORDS
,
2797 if (PyType_Ready(&PyLdbDn
) < 0)
2800 if (PyType_Ready(&PyLdbMessage
) < 0)
2803 if (PyType_Ready(&PyLdbMessageElement
) < 0)
2806 if (PyType_Ready(&PyLdb
) < 0)
2809 if (PyType_Ready(&PyLdbModule
) < 0)
2812 if (PyType_Ready(&PyLdbTree
) < 0)
2815 m
= Py_InitModule3("ldb", py_ldb_global_methods
,
2816 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2820 PyModule_AddObject(m
, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ
));
2821 PyModule_AddObject(m
, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP
));
2822 PyModule_AddObject(m
, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT
));
2823 PyModule_AddObject(m
, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT
));
2824 PyModule_AddObject(m
, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE
));
2825 PyModule_AddObject(m
, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL
));
2826 PyModule_AddObject(m
, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE
));
2828 PyModule_AddObject(m
, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE
));
2829 PyModule_AddObject(m
, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD
));
2830 PyModule_AddObject(m
, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE
));
2831 PyModule_AddObject(m
, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY
));
2833 PyModule_AddObject(m
, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD
));
2834 PyModule_AddObject(m
, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE
));
2835 PyModule_AddObject(m
, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE
));
2837 PyModule_AddObject(m
, "SUCCESS", PyInt_FromLong(LDB_SUCCESS
));
2838 PyModule_AddObject(m
, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR
));
2839 PyModule_AddObject(m
, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR
));
2840 PyModule_AddObject(m
, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED
));
2841 PyModule_AddObject(m
, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED
));
2842 PyModule_AddObject(m
, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE
));
2843 PyModule_AddObject(m
, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE
));
2844 PyModule_AddObject(m
, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
));
2845 PyModule_AddObject(m
, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED
));
2846 PyModule_AddObject(m
, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL
));
2847 PyModule_AddObject(m
, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED
));
2848 PyModule_AddObject(m
, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
));
2849 PyModule_AddObject(m
, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED
));
2850 PyModule_AddObject(m
, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS
));
2851 PyModule_AddObject(m
, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE
));
2852 PyModule_AddObject(m
, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
));
2853 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING
));
2854 PyModule_AddObject(m
, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION
));
2855 PyModule_AddObject(m
, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
));
2856 PyModule_AddObject(m
, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
));
2857 PyModule_AddObject(m
, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT
));
2858 PyModule_AddObject(m
, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM
));
2859 PyModule_AddObject(m
, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX
));
2860 PyModule_AddObject(m
, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
));
2861 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION
));
2862 PyModule_AddObject(m
, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS
));
2863 PyModule_AddObject(m
, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
));
2864 PyModule_AddObject(m
, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY
));
2865 PyModule_AddObject(m
, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE
));
2866 PyModule_AddObject(m
, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM
));
2867 PyModule_AddObject(m
, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT
));
2868 PyModule_AddObject(m
, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION
));
2869 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION
));
2870 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
));
2871 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN
));
2872 PyModule_AddObject(m
, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS
));
2873 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
));
2874 PyModule_AddObject(m
, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS
));
2875 PyModule_AddObject(m
, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER
));
2877 PyModule_AddObject(m
, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY
));
2878 PyModule_AddObject(m
, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC
));
2879 PyModule_AddObject(m
, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT
));
2880 PyModule_AddObject(m
, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP
));
2882 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
2884 PyExc_LdbError
= PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL
, NULL
);
2885 PyModule_AddObject(m
, "LdbError", PyExc_LdbError
);
2888 Py_INCREF(&PyLdbDn
);
2889 Py_INCREF(&PyLdbModule
);
2890 Py_INCREF(&PyLdbMessage
);
2891 Py_INCREF(&PyLdbMessageElement
);
2892 Py_INCREF(&PyLdbTree
);
2894 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PyLdb
);
2895 PyModule_AddObject(m
, "Dn", (PyObject
*)&PyLdbDn
);
2896 PyModule_AddObject(m
, "Message", (PyObject
*)&PyLdbMessage
);
2897 PyModule_AddObject(m
, "MessageElement", (PyObject
*)&PyLdbMessageElement
);
2898 PyModule_AddObject(m
, "Module", (PyObject
*)&PyLdbModule
);
2899 PyModule_AddObject(m
, "Tree", (PyObject
*)&PyLdbTree
);
2901 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));