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
10 Copyright (C) 2009-2011 Andrew Tridgell
11 Copyright (C) 2009-2011 Andrew Bartlett
13 ** NOTE! The following LGPL license applies to the ldb
14 ** library. This does NOT imply that all of Samba is released
17 This library is free software; you can redistribute it and/or
18 modify it under the terms of the GNU Lesser General Public
19 License as published by the Free Software Foundation; either
20 version 3 of the License, or (at your option) any later version.
22 This library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Lesser General Public License for more details.
27 You should have received a copy of the GNU Lesser General Public
28 License along with this library; if not, see <http://www.gnu.org/licenses/>.
33 #include "ldb_private.h"
37 static PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
);
38 static PyObject
*PyExc_LdbError
;
40 staticforward PyTypeObject PyLdbControl
;
41 staticforward PyTypeObject PyLdbResult
;
42 staticforward PyTypeObject PyLdbMessage
;
43 staticforward PyTypeObject PyLdbModule
;
44 staticforward PyTypeObject PyLdbDn
;
45 staticforward PyTypeObject PyLdb
;
46 staticforward PyTypeObject PyLdbMessageElement
;
47 staticforward PyTypeObject PyLdbTree
;
48 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
);
49 static PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
);
50 static struct ldb_message_element
*PyObject_AsMessageElement(
54 const char *attr_name
);
56 /* There's no Py_ssize_t in 2.4, apparently */
57 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
58 typedef int Py_ssize_t
;
59 typedef inquiry lenfunc
;
60 typedef intargfunc ssizeargfunc
;
63 #ifndef Py_RETURN_NONE
64 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
67 #define SIGN(a) (((a) == 0)?0:((a) < 0?-1:1))
71 static PyObject
*py_ldb_control_str(PyLdbControlObject
*self
)
73 if (self
->data
!= NULL
) {
74 char* control
= ldb_control_to_string(self
->mem_ctx
, self
->data
);
75 if (control
== NULL
) {
79 return PyString_FromString(control
);
81 return PyString_FromFormat("ldb control");
85 static void py_ldb_control_dealloc(PyLdbControlObject
*self
)
87 if (self
->mem_ctx
!= NULL
) {
88 talloc_free(self
->mem_ctx
);
91 self
->ob_type
->tp_free(self
);
94 static PyObject
*py_ldb_control_get_oid(PyLdbControlObject
*self
)
96 return PyString_FromString(self
->data
->oid
);
99 static PyObject
*py_ldb_control_get_critical(PyLdbControlObject
*self
)
101 return PyBool_FromLong(self
->data
->critical
);
104 static PyObject
*py_ldb_control_set_critical(PyLdbControlObject
*self
, PyObject
*value
, void *closure
)
106 if (PyObject_IsTrue(value
)) {
107 self
->data
->critical
= true;
109 self
->data
->critical
= false;
114 static PyObject
*py_ldb_control_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
117 const char * const kwnames
[] = { "ldb", "data", NULL
};
118 struct ldb_control
*parsed_controls
;
119 PyLdbControlObject
*ret
;
122 struct ldb_context
*ldb_ctx
;
124 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Os",
125 discard_const_p(char *, kwnames
),
129 mem_ctx
= talloc_new(NULL
);
130 if (mem_ctx
== NULL
) {
135 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
136 parsed_controls
= ldb_parse_control_from_string(ldb_ctx
, mem_ctx
, data
);
138 if (!parsed_controls
) {
139 talloc_free(mem_ctx
);
140 PyErr_SetString(PyExc_ValueError
, "unable to parse control string");
144 ret
= PyObject_New(PyLdbControlObject
, type
);
147 talloc_free(mem_ctx
);
151 ret
->mem_ctx
= mem_ctx
;
153 ret
->data
= talloc_move(mem_ctx
, &parsed_controls
);
154 if (ret
->data
== NULL
) {
157 talloc_free(mem_ctx
);
161 return (PyObject
*)ret
;
164 static PyGetSetDef py_ldb_control_getset
[] = {
165 { discard_const_p(char, "oid"), (getter
)py_ldb_control_get_oid
, NULL
, NULL
},
166 { discard_const_p(char, "critical"), (getter
)py_ldb_control_get_critical
, (setter
)py_ldb_control_set_critical
, NULL
},
170 static PyTypeObject PyLdbControl
= {
171 .tp_name
= "ldb.control",
172 .tp_dealloc
= (destructor
)py_ldb_control_dealloc
,
173 .tp_getattro
= PyObject_GenericGetAttr
,
174 .tp_basicsize
= sizeof(PyLdbControlObject
),
175 .tp_getset
= py_ldb_control_getset
,
176 .tp_doc
= "LDB control.",
177 .tp_str
= (reprfunc
)py_ldb_control_str
,
178 .tp_new
= py_ldb_control_new
,
179 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
182 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
184 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
185 return; /* Python exception should already be set, just keep that */
187 PyErr_SetObject(error
,
188 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
189 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
192 static PyObject
*PyObject_FromLdbValue(struct ldb_val
*val
)
194 return PyString_FromStringAndSize((const char *)val
->data
, val
->length
);
198 * Create a Python object from a ldb_result.
200 * @param result LDB result to convert
201 * @return Python object with converted result (a list object)
203 static PyObject
*PyLdbControl_FromControl(struct ldb_control
*control
)
205 TALLOC_CTX
*ctl_ctx
= talloc_new(NULL
);
206 PyLdbControlObject
*ctrl
;
207 if (ctl_ctx
== NULL
) {
212 ctrl
= (PyLdbControlObject
*)PyLdbControl
.tp_alloc(&PyLdbControl
, 0);
214 talloc_free(ctl_ctx
);
218 ctrl
->mem_ctx
= ctl_ctx
;
219 ctrl
->data
= talloc_steal(ctrl
->mem_ctx
, control
);
220 if (ctrl
->data
== NULL
) {
225 return (PyObject
*) ctrl
;
229 * Create a Python object from a ldb_result.
231 * @param result LDB result to convert
232 * @return Python object with converted result (a list object)
234 static PyObject
*PyLdbResult_FromResult(struct ldb_result
*result
)
236 PyLdbResultObject
*ret
;
237 PyObject
*list
, *controls
, *referals
;
240 if (result
== NULL
) {
244 ret
= (PyLdbResultObject
*)PyLdbResult
.tp_alloc(&PyLdbResult
, 0);
250 list
= PyList_New(result
->count
);
257 for (i
= 0; i
< result
->count
; i
++) {
258 PyList_SetItem(list
, i
, PyLdbMessage_FromMessage(result
->msgs
[i
]));
261 ret
->mem_ctx
= talloc_new(NULL
);
262 if (ret
->mem_ctx
== NULL
) {
271 if (result
->controls
) {
272 controls
= PyList_New(1);
273 if (controls
== NULL
) {
278 for (i
=0; result
->controls
[i
]; i
++) {
279 PyObject
*ctrl
= (PyObject
*) PyLdbControl_FromControl(result
->controls
[i
]);
286 PyList_SetItem(controls
, i
, ctrl
);
290 * No controls so we keep an empty list
292 controls
= PyList_New(0);
293 if (controls
== NULL
) {
300 ret
->controls
= controls
;
304 while (result
->refs
&& result
->refs
[i
]) {
308 referals
= PyList_New(i
);
309 if (referals
== NULL
) {
315 for (i
= 0;result
->refs
&& result
->refs
[i
]; i
++) {
316 PyList_SetItem(referals
, i
, PyString_FromString(result
->refs
[i
]));
318 ret
->referals
= referals
;
319 return (PyObject
*)ret
;
323 * Create a LDB Result from a Python object.
324 * If conversion fails, NULL will be returned and a Python exception set.
326 * @param mem_ctx Memory context in which to allocate the LDB Result
327 * @param obj Python object to convert
328 * @return a ldb_result, or NULL if the conversion failed
330 static struct ldb_result
*PyLdbResult_AsResult(TALLOC_CTX
*mem_ctx
,
333 struct ldb_result
*res
;
339 res
= talloc_zero(mem_ctx
, struct ldb_result
);
340 res
->count
= PyList_Size(obj
);
341 res
->msgs
= talloc_array(res
, struct ldb_message
*, res
->count
);
342 for (i
= 0; i
< res
->count
; i
++) {
343 PyObject
*item
= PyList_GetItem(obj
, i
);
344 res
->msgs
[i
] = PyLdbMessage_AsMessage(item
);
349 static PyObject
*py_ldb_dn_validate(PyLdbDnObject
*self
)
351 return PyBool_FromLong(ldb_dn_validate(self
->dn
));
354 static PyObject
*py_ldb_dn_is_valid(PyLdbDnObject
*self
)
356 return PyBool_FromLong(ldb_dn_is_valid(self
->dn
));
359 static PyObject
*py_ldb_dn_is_special(PyLdbDnObject
*self
)
361 return PyBool_FromLong(ldb_dn_is_special(self
->dn
));
364 static PyObject
*py_ldb_dn_is_null(PyLdbDnObject
*self
)
366 return PyBool_FromLong(ldb_dn_is_null(self
->dn
));
369 static PyObject
*py_ldb_dn_get_casefold(PyLdbDnObject
*self
)
371 return PyString_FromString(ldb_dn_get_casefold(self
->dn
));
374 static PyObject
*py_ldb_dn_get_linearized(PyLdbDnObject
*self
)
376 return PyString_FromString(ldb_dn_get_linearized(self
->dn
));
379 static PyObject
*py_ldb_dn_canonical_str(PyLdbDnObject
*self
)
381 return PyString_FromString(ldb_dn_canonical_string(self
->dn
, self
->dn
));
384 static PyObject
*py_ldb_dn_canonical_ex_str(PyLdbDnObject
*self
)
386 return PyString_FromString(ldb_dn_canonical_ex_string(self
->dn
, self
->dn
));
389 static PyObject
*py_ldb_dn_extended_str(PyLdbDnObject
*self
, PyObject
*args
, PyObject
*kwargs
)
391 const char * const kwnames
[] = { "mode", NULL
};
393 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|i",
394 discard_const_p(char *, kwnames
),
397 return PyString_FromString(ldb_dn_get_extended_linearized(self
->dn
, self
->dn
, mode
));
400 static PyObject
*py_ldb_dn_get_extended_component(PyLdbDnObject
*self
, PyObject
*args
)
403 const struct ldb_val
*val
;
405 if (!PyArg_ParseTuple(args
, "s", &name
))
407 val
= ldb_dn_get_extended_component(self
->dn
, name
);
412 return PyString_FromStringAndSize((const char *)val
->data
, val
->length
);
415 static PyObject
*py_ldb_dn_set_extended_component(PyLdbDnObject
*self
, PyObject
*args
)
421 if (!PyArg_ParseTuple(args
, "sO", &name
, &value
))
424 if (value
== Py_None
) {
425 err
= ldb_dn_set_extended_component(self
->dn
, name
, NULL
);
428 if (!PyString_Check(value
)) {
429 PyErr_SetString(PyExc_TypeError
, "Expected a string argument");
432 val
.data
= (uint8_t *)PyString_AsString(value
);
433 val
.length
= PyString_Size(value
);
434 err
= ldb_dn_set_extended_component(self
->dn
, name
, &val
);
437 if (err
!= LDB_SUCCESS
) {
438 PyErr_SetString(PyExc_TypeError
, "Failed to set extended component");
445 static PyObject
*py_ldb_dn_repr(PyLdbDnObject
*self
)
447 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self
->dn
))));
450 static PyObject
*py_ldb_dn_check_special(PyLdbDnObject
*self
, PyObject
*args
)
454 if (!PyArg_ParseTuple(args
, "s", &name
))
457 return ldb_dn_check_special(self
->dn
, name
)?Py_True
:Py_False
;
460 static int py_ldb_dn_compare(PyLdbDnObject
*dn1
, PyLdbDnObject
*dn2
)
463 ret
= ldb_dn_compare(dn1
->dn
, dn2
->dn
);
464 if (ret
< 0) ret
= -1;
465 if (ret
> 0) ret
= 1;
469 static PyObject
*py_ldb_dn_get_parent(PyLdbDnObject
*self
)
471 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
);
472 struct ldb_dn
*parent
;
473 PyLdbDnObject
*py_ret
;
474 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
476 parent
= ldb_dn_get_parent(mem_ctx
, dn
);
477 if (parent
== NULL
) {
478 talloc_free(mem_ctx
);
482 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
483 if (py_ret
== NULL
) {
485 talloc_free(mem_ctx
);
488 py_ret
->mem_ctx
= mem_ctx
;
490 return (PyObject
*)py_ret
;
493 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
495 static PyObject
*py_ldb_dn_add_child(PyLdbDnObject
*self
, PyObject
*args
)
498 struct ldb_dn
*dn
, *other
;
499 if (!PyArg_ParseTuple(args
, "O", &py_other
))
502 dn
= PyLdbDn_AsDn((PyObject
*)self
);
504 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
507 return ldb_dn_add_child(dn
, other
)?Py_True
:Py_False
;
510 static PyObject
*py_ldb_dn_add_base(PyLdbDnObject
*self
, PyObject
*args
)
513 struct ldb_dn
*other
, *dn
;
514 if (!PyArg_ParseTuple(args
, "O", &py_other
))
517 dn
= PyLdbDn_AsDn((PyObject
*)self
);
519 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
522 return ldb_dn_add_base(dn
, other
)?Py_True
:Py_False
;
525 static PyObject
*py_ldb_dn_is_child_of(PyLdbDnObject
*self
, PyObject
*args
)
528 struct ldb_dn
*dn
, *base
;
529 if (!PyArg_ParseTuple(args
, "O", &py_base
))
532 dn
= PyLdbDn_AsDn((PyObject
*)self
);
534 if (!PyObject_AsDn(NULL
, py_base
, dn_ldb_ctx(dn
), &base
))
537 return PyBool_FromLong(ldb_dn_compare_base(base
, dn
) == 0);
540 static PyMethodDef py_ldb_dn_methods
[] = {
541 { "validate", (PyCFunction
)py_ldb_dn_validate
, METH_NOARGS
,
542 "S.validate() -> bool\n"
543 "Validate DN is correct." },
544 { "is_valid", (PyCFunction
)py_ldb_dn_is_valid
, METH_NOARGS
,
545 "S.is_valid() -> bool\n" },
546 { "is_special", (PyCFunction
)py_ldb_dn_is_special
, METH_NOARGS
,
547 "S.is_special() -> bool\n"
548 "Check whether this is a special LDB DN." },
549 { "is_null", (PyCFunction
)py_ldb_dn_is_null
, METH_NOARGS
,
550 "Check whether this is a null DN." },
551 { "get_casefold", (PyCFunction
)py_ldb_dn_get_casefold
, METH_NOARGS
,
553 { "get_linearized", (PyCFunction
)py_ldb_dn_get_linearized
, METH_NOARGS
,
555 { "canonical_str", (PyCFunction
)py_ldb_dn_canonical_str
, METH_NOARGS
,
556 "S.canonical_str() -> string\n"
557 "Canonical version of this DN (like a posix path)." },
558 { "is_child_of", (PyCFunction
)py_ldb_dn_is_child_of
, METH_VARARGS
,
559 "S.is_child_of(basedn) -> int\nReturns True if this DN is a child of basedn\n"},
560 { "canonical_ex_str", (PyCFunction
)py_ldb_dn_canonical_ex_str
, METH_NOARGS
,
561 "S.canonical_ex_str() -> string\n"
562 "Canonical version of this DN (like a posix path, with terminating newline)." },
563 { "extended_str", (PyCFunction
)py_ldb_dn_extended_str
, METH_VARARGS
| METH_KEYWORDS
,
564 "S.extended_str(mode=1) -> string\n"
565 "Extended version of this DN" },
566 { "parent", (PyCFunction
)py_ldb_dn_get_parent
, METH_NOARGS
,
568 "Get the parent for this DN." },
569 { "add_child", (PyCFunction
)py_ldb_dn_add_child
, METH_VARARGS
,
570 "S.add_child(dn) -> None\n"
571 "Add a child DN to this DN." },
572 { "add_base", (PyCFunction
)py_ldb_dn_add_base
, METH_VARARGS
,
573 "S.add_base(dn) -> None\n"
574 "Add a base DN to this DN." },
575 { "check_special", (PyCFunction
)py_ldb_dn_check_special
, METH_VARARGS
,
576 "S.check_special(name) -> bool\n\n"
577 "Check if name is a special DN name"},
578 { "get_extended_component", (PyCFunction
)py_ldb_dn_get_extended_component
, METH_VARARGS
,
579 "S.get_extended_component(name) -> string\n\n"
580 "returns a DN extended component as a binary string"},
581 { "set_extended_component", (PyCFunction
)py_ldb_dn_set_extended_component
, METH_VARARGS
,
582 "S.set_extended_component(name, value) -> string\n\n"
583 "set a DN extended component as a binary string"},
587 static Py_ssize_t
py_ldb_dn_len(PyLdbDnObject
*self
)
589 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject
*)self
));
592 static PyObject
*py_ldb_dn_concat(PyLdbDnObject
*self
, PyObject
*py_other
)
594 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
),
596 PyLdbDnObject
*py_ret
;
598 if (!PyObject_AsDn(NULL
, py_other
, NULL
, &other
))
601 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
602 if (py_ret
== NULL
) {
606 py_ret
->mem_ctx
= talloc_new(NULL
);
607 py_ret
->dn
= ldb_dn_copy(py_ret
->mem_ctx
, dn
);
608 ldb_dn_add_child(py_ret
->dn
, other
);
609 return (PyObject
*)py_ret
;
612 static PySequenceMethods py_ldb_dn_seq
= {
613 .sq_length
= (lenfunc
)py_ldb_dn_len
,
614 .sq_concat
= (binaryfunc
)py_ldb_dn_concat
,
617 static PyObject
*py_ldb_dn_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
622 struct ldb_context
*ldb_ctx
;
624 PyLdbDnObject
*py_ret
;
625 const char * const kwnames
[] = { "ldb", "dn", NULL
};
627 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Os",
628 discard_const_p(char *, kwnames
),
632 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
634 mem_ctx
= talloc_new(NULL
);
635 if (mem_ctx
== NULL
) {
640 ret
= ldb_dn_new(mem_ctx
, ldb_ctx
, str
);
641 if (!ldb_dn_validate(ret
)) {
642 talloc_free(mem_ctx
);
643 PyErr_SetString(PyExc_ValueError
, "unable to parse dn string");
647 py_ret
= (PyLdbDnObject
*)type
->tp_alloc(type
, 0);
649 talloc_free(mem_ctx
);
653 py_ret
->mem_ctx
= mem_ctx
;
655 return (PyObject
*)py_ret
;
658 static void py_ldb_dn_dealloc(PyLdbDnObject
*self
)
660 talloc_free(self
->mem_ctx
);
664 static PyTypeObject PyLdbDn
= {
666 .tp_methods
= py_ldb_dn_methods
,
667 .tp_str
= (reprfunc
)py_ldb_dn_get_linearized
,
668 .tp_repr
= (reprfunc
)py_ldb_dn_repr
,
669 .tp_compare
= (cmpfunc
)py_ldb_dn_compare
,
670 .tp_as_sequence
= &py_ldb_dn_seq
,
671 .tp_doc
= "A LDB distinguished name.",
672 .tp_new
= py_ldb_dn_new
,
673 .tp_dealloc
= (destructor
)py_ldb_dn_dealloc
,
674 .tp_basicsize
= sizeof(PyLdbDnObject
),
675 .tp_flags
= Py_TPFLAGS_DEFAULT
,
679 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3, 0);
680 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
)
682 PyObject
*fn
= (PyObject
*)context
;
683 PyObject_CallFunction(fn
, discard_const_p(char, "(i,O)"), level
, PyString_FromFormatV(fmt
, ap
));
686 static PyObject
*py_ldb_set_debug(PyLdbObject
*self
, PyObject
*args
)
690 if (!PyArg_ParseTuple(args
, "O", &cb
))
694 /* FIXME: Where do we DECREF cb ? */
695 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_set_debug(self
->ldb_ctx
, py_ldb_debug
, cb
), PyLdb_AsLdbContext(self
));
700 static PyObject
*py_ldb_set_create_perms(PyTypeObject
*self
, PyObject
*args
)
703 if (!PyArg_ParseTuple(args
, "I", &perms
))
706 ldb_set_create_perms(PyLdb_AsLdbContext(self
), perms
);
711 static PyObject
*py_ldb_set_modules_dir(PyTypeObject
*self
, PyObject
*args
)
714 if (!PyArg_ParseTuple(args
, "s", &modules_dir
))
717 ldb_set_modules_dir(PyLdb_AsLdbContext(self
), modules_dir
);
722 static PyObject
*py_ldb_transaction_start(PyLdbObject
*self
)
724 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_start(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
728 static PyObject
*py_ldb_transaction_commit(PyLdbObject
*self
)
730 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
734 static PyObject
*py_ldb_transaction_prepare_commit(PyLdbObject
*self
)
736 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_prepare_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
740 static PyObject
*py_ldb_transaction_cancel(PyLdbObject
*self
)
742 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_cancel(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
746 static PyObject
*py_ldb_setup_wellknown_attributes(PyLdbObject
*self
)
748 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
752 static PyObject
*py_ldb_repr(PyLdbObject
*self
)
754 return PyString_FromFormat("<ldb connection>");
757 static PyObject
*py_ldb_get_root_basedn(PyLdbObject
*self
)
759 struct ldb_dn
*dn
= ldb_get_root_basedn(PyLdb_AsLdbContext(self
));
762 return PyLdbDn_FromDn(dn
);
766 static PyObject
*py_ldb_get_schema_basedn(PyLdbObject
*self
)
768 struct ldb_dn
*dn
= ldb_get_schema_basedn(PyLdb_AsLdbContext(self
));
771 return PyLdbDn_FromDn(dn
);
774 static PyObject
*py_ldb_get_config_basedn(PyLdbObject
*self
)
776 struct ldb_dn
*dn
= ldb_get_config_basedn(PyLdb_AsLdbContext(self
));
779 return PyLdbDn_FromDn(dn
);
782 static PyObject
*py_ldb_get_default_basedn(PyLdbObject
*self
)
784 struct ldb_dn
*dn
= ldb_get_default_basedn(PyLdb_AsLdbContext(self
));
787 return PyLdbDn_FromDn(dn
);
790 static const char **PyList_AsStringList(TALLOC_CTX
*mem_ctx
, PyObject
*list
,
791 const char *paramname
)
795 if (!PyList_Check(list
)) {
796 PyErr_Format(PyExc_TypeError
, "%s is not a list", paramname
);
799 ret
= talloc_array(NULL
, const char *, PyList_Size(list
)+1);
805 for (i
= 0; i
< PyList_Size(list
); i
++) {
806 PyObject
*item
= PyList_GetItem(list
, i
);
807 if (!PyString_Check(item
)) {
808 PyErr_Format(PyExc_TypeError
, "%s should be strings", paramname
);
811 ret
[i
] = talloc_strndup(ret
, PyString_AsString(item
),
812 PyString_Size(item
));
818 static int py_ldb_init(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
820 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
822 PyObject
*py_options
= Py_None
;
823 const char **options
;
824 unsigned int flags
= 0;
826 struct ldb_context
*ldb
;
828 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|zIO:Ldb.__init__",
829 discard_const_p(char *, kwnames
),
830 &url
, &flags
, &py_options
))
833 ldb
= PyLdb_AsLdbContext(self
);
835 if (py_options
== Py_None
) {
838 options
= PyList_AsStringList(ldb
, py_options
, "options");
844 ret
= ldb_connect(ldb
, url
, flags
, options
);
845 if (ret
!= LDB_SUCCESS
) {
846 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb
);
851 talloc_free(options
);
855 static PyObject
*py_ldb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
858 struct ldb_context
*ldb
;
859 ret
= (PyLdbObject
*)type
->tp_alloc(type
, 0);
864 ret
->mem_ctx
= talloc_new(NULL
);
865 ldb
= ldb_init(ret
->mem_ctx
, NULL
);
873 return (PyObject
*)ret
;
876 static PyObject
*py_ldb_connect(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
879 unsigned int flags
= 0;
880 PyObject
*py_options
= Py_None
;
882 const char **options
;
883 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
885 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|zIO",
886 discard_const_p(char *, kwnames
),
887 &url
, &flags
, &py_options
))
890 if (py_options
== Py_None
) {
893 options
= PyList_AsStringList(NULL
, py_options
, "options");
898 ret
= ldb_connect(PyLdb_AsLdbContext(self
), url
, flags
, options
);
899 talloc_free(options
);
901 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
906 static PyObject
*py_ldb_modify(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
909 PyObject
*py_controls
= Py_None
;
910 struct ldb_context
*ldb_ctx
;
911 struct ldb_request
*req
;
912 struct ldb_control
**parsed_controls
;
913 struct ldb_message
*msg
;
917 const char * const kwnames
[] = { "message", "controls", "validate", NULL
};
919 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|Ob",
920 discard_const_p(char *, kwnames
),
921 &py_msg
, &py_controls
, &validate
))
924 mem_ctx
= talloc_new(NULL
);
925 if (mem_ctx
== NULL
) {
929 ldb_ctx
= PyLdb_AsLdbContext(self
);
931 if (py_controls
== Py_None
) {
932 parsed_controls
= NULL
;
934 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
935 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
936 talloc_free(controls
);
939 if (!PyLdbMessage_Check(py_msg
)) {
940 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message");
941 talloc_free(mem_ctx
);
944 msg
= PyLdbMessage_AsMessage(py_msg
);
947 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
948 if (ret
!= LDB_SUCCESS
) {
949 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
950 talloc_free(mem_ctx
);
955 ret
= ldb_build_mod_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
956 NULL
, ldb_op_default_callback
, NULL
);
957 if (ret
!= LDB_SUCCESS
) {
958 PyErr_SetString(PyExc_TypeError
, "failed to build request");
959 talloc_free(mem_ctx
);
963 /* do request and autostart a transaction */
964 /* Then let's LDB handle the message error in case of pb as they are meaningful */
966 ret
= ldb_transaction_start(ldb_ctx
);
967 if (ret
!= LDB_SUCCESS
) {
968 talloc_free(mem_ctx
);
969 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
972 ret
= ldb_request(ldb_ctx
, req
);
973 if (ret
== LDB_SUCCESS
) {
974 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
977 if (ret
== LDB_SUCCESS
) {
978 ret
= ldb_transaction_commit(ldb_ctx
);
980 ldb_transaction_cancel(ldb_ctx
);
981 if (ldb_ctx
->err_string
== NULL
) {
982 /* no error string was setup by the backend */
983 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
987 talloc_free(mem_ctx
);
988 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
995 * Obtain a ldb message from a Python Dictionary object.
997 * @param mem_ctx Memory context
998 * @param py_obj Python Dictionary object
999 * @param ldb_ctx LDB context
1000 * @param mod_flags Flags to be set on every message element
1001 * @return ldb_message on success or NULL on failure
1003 static struct ldb_message
*PyDict_AsMessage(TALLOC_CTX
*mem_ctx
,
1005 struct ldb_context
*ldb_ctx
,
1006 unsigned int mod_flags
)
1008 struct ldb_message
*msg
;
1009 unsigned int msg_pos
= 0;
1010 Py_ssize_t dict_pos
= 0;
1011 PyObject
*key
, *value
;
1012 struct ldb_message_element
*msg_el
;
1013 PyObject
*dn_value
= PyDict_GetItemString(py_obj
, "dn");
1015 msg
= ldb_msg_new(mem_ctx
);
1016 msg
->elements
= talloc_zero_array(msg
, struct ldb_message_element
, PyDict_Size(py_obj
));
1019 if (!PyObject_AsDn(msg
, dn_value
, ldb_ctx
, &msg
->dn
)) {
1020 PyErr_SetString(PyExc_TypeError
, "unable to import dn object");
1023 if (msg
->dn
== NULL
) {
1024 PyErr_SetString(PyExc_TypeError
, "dn set but not found");
1028 PyErr_SetString(PyExc_TypeError
, "no dn set");
1032 while (PyDict_Next(py_obj
, &dict_pos
, &key
, &value
)) {
1033 char *key_str
= PyString_AsString(key
);
1034 if (strcmp(key_str
, "dn") != 0) {
1035 msg_el
= PyObject_AsMessageElement(msg
->elements
, value
,
1036 mod_flags
, key_str
);
1037 if (msg_el
== NULL
) {
1038 PyErr_SetString(PyExc_TypeError
, "unable to import element");
1041 memcpy(&msg
->elements
[msg_pos
], msg_el
, sizeof(*msg_el
));
1046 msg
->num_elements
= msg_pos
;
1051 static PyObject
*py_ldb_add(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1055 struct ldb_context
*ldb_ctx
;
1056 struct ldb_request
*req
;
1057 struct ldb_message
*msg
= NULL
;
1058 PyObject
*py_controls
= Py_None
;
1059 TALLOC_CTX
*mem_ctx
;
1060 struct ldb_control
**parsed_controls
;
1061 const char * const kwnames
[] = { "message", "controls", NULL
};
1063 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|O",
1064 discard_const_p(char *, kwnames
),
1065 &py_obj
, &py_controls
))
1068 mem_ctx
= talloc_new(NULL
);
1069 if (mem_ctx
== NULL
) {
1073 ldb_ctx
= PyLdb_AsLdbContext(self
);
1075 if (py_controls
== Py_None
) {
1076 parsed_controls
= NULL
;
1078 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1079 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1080 talloc_free(controls
);
1083 if (PyLdbMessage_Check(py_obj
)) {
1084 msg
= PyLdbMessage_AsMessage(py_obj
);
1085 } else if (PyDict_Check(py_obj
)) {
1086 msg
= PyDict_AsMessage(mem_ctx
, py_obj
, ldb_ctx
, LDB_FLAG_MOD_ADD
);
1088 PyErr_SetString(PyExc_TypeError
,
1089 "Dictionary or LdbMessage object expected!");
1093 /* we should have a PyErr already set */
1094 talloc_free(mem_ctx
);
1098 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
1099 if (ret
!= LDB_SUCCESS
) {
1100 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1101 talloc_free(mem_ctx
);
1105 ret
= ldb_build_add_req(&req
, ldb_ctx
, mem_ctx
, msg
, parsed_controls
,
1106 NULL
, ldb_op_default_callback
, NULL
);
1107 if (ret
!= LDB_SUCCESS
) {
1108 PyErr_SetString(PyExc_TypeError
, "failed to build request");
1109 talloc_free(mem_ctx
);
1113 /* do request and autostart a transaction */
1114 /* Then let's LDB handle the message error in case of pb as they are meaningful */
1116 ret
= ldb_transaction_start(ldb_ctx
);
1117 if (ret
!= LDB_SUCCESS
) {
1118 talloc_free(mem_ctx
);
1119 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1122 ret
= ldb_request(ldb_ctx
, req
);
1123 if (ret
== LDB_SUCCESS
) {
1124 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1127 if (ret
== LDB_SUCCESS
) {
1128 ret
= ldb_transaction_commit(ldb_ctx
);
1130 ldb_transaction_cancel(ldb_ctx
);
1131 if (ldb_ctx
->err_string
== NULL
) {
1132 /* no error string was setup by the backend */
1133 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
1137 talloc_free(mem_ctx
);
1138 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1143 static PyObject
*py_ldb_delete(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1148 struct ldb_context
*ldb_ctx
;
1149 struct ldb_request
*req
;
1150 PyObject
*py_controls
= Py_None
;
1151 TALLOC_CTX
*mem_ctx
;
1152 struct ldb_control
**parsed_controls
;
1153 const char * const kwnames
[] = { "dn", "controls", NULL
};
1155 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|O",
1156 discard_const_p(char *, kwnames
),
1157 &py_dn
, &py_controls
))
1160 mem_ctx
= talloc_new(NULL
);
1161 if (mem_ctx
== NULL
) {
1165 ldb_ctx
= PyLdb_AsLdbContext(self
);
1167 if (py_controls
== Py_None
) {
1168 parsed_controls
= NULL
;
1170 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1171 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1172 talloc_free(controls
);
1175 if (!PyObject_AsDn(mem_ctx
, py_dn
, ldb_ctx
, &dn
)) {
1176 talloc_free(mem_ctx
);
1180 ret
= ldb_build_del_req(&req
, ldb_ctx
, mem_ctx
, dn
, parsed_controls
,
1181 NULL
, ldb_op_default_callback
, NULL
);
1182 if (ret
!= LDB_SUCCESS
) {
1183 PyErr_SetString(PyExc_TypeError
, "failed to build request");
1184 talloc_free(mem_ctx
);
1188 /* do request and autostart a transaction */
1189 /* Then let's LDB handle the message error in case of pb as they are meaningful */
1191 ret
= ldb_transaction_start(ldb_ctx
);
1192 if (ret
!= LDB_SUCCESS
) {
1193 talloc_free(mem_ctx
);
1194 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1197 ret
= ldb_request(ldb_ctx
, req
);
1198 if (ret
== LDB_SUCCESS
) {
1199 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1202 if (ret
== LDB_SUCCESS
) {
1203 ret
= ldb_transaction_commit(ldb_ctx
);
1205 ldb_transaction_cancel(ldb_ctx
);
1206 if (ldb_ctx
->err_string
== NULL
) {
1207 /* no error string was setup by the backend */
1208 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
1212 talloc_free(mem_ctx
);
1213 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1218 static PyObject
*py_ldb_rename(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1220 PyObject
*py_dn1
, *py_dn2
;
1221 struct ldb_dn
*dn1
, *dn2
;
1223 struct ldb_context
*ldb
;
1224 TALLOC_CTX
*mem_ctx
;
1225 PyObject
*py_controls
= Py_None
;
1226 struct ldb_control
**parsed_controls
;
1227 struct ldb_context
*ldb_ctx
;
1228 struct ldb_request
*req
;
1229 const char * const kwnames
[] = { "dn1", "dn2", "controls", NULL
};
1231 ldb_ctx
= PyLdb_AsLdbContext(self
);
1233 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OO|O",
1234 discard_const_p(char *, kwnames
),
1235 &py_dn1
, &py_dn2
, &py_controls
))
1239 mem_ctx
= talloc_new(NULL
);
1240 if (mem_ctx
== NULL
) {
1244 ldb
= PyLdb_AsLdbContext(self
);
1246 if (py_controls
== Py_None
) {
1247 parsed_controls
= NULL
;
1249 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1250 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1251 talloc_free(controls
);
1255 if (!PyObject_AsDn(mem_ctx
, py_dn1
, ldb
, &dn1
)) {
1256 talloc_free(mem_ctx
);
1260 if (!PyObject_AsDn(mem_ctx
, py_dn2
, ldb
, &dn2
)) {
1261 talloc_free(mem_ctx
);
1265 ret
= ldb_build_rename_req(&req
, ldb_ctx
, mem_ctx
, dn1
, dn2
, parsed_controls
,
1266 NULL
, ldb_op_default_callback
, NULL
);
1267 if (ret
!= LDB_SUCCESS
) {
1268 PyErr_SetString(PyExc_TypeError
, "failed to build request");
1269 talloc_free(mem_ctx
);
1273 /* do request and autostart a transaction */
1274 /* Then let's LDB handle the message error in case of pb as they are meaningful */
1276 ret
= ldb_transaction_start(ldb_ctx
);
1277 if (ret
!= LDB_SUCCESS
) {
1278 talloc_free(mem_ctx
);
1279 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1282 ret
= ldb_request(ldb_ctx
, req
);
1283 if (ret
== LDB_SUCCESS
) {
1284 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1287 if (ret
== LDB_SUCCESS
) {
1288 ret
= ldb_transaction_commit(ldb_ctx
);
1290 ldb_transaction_cancel(ldb_ctx
);
1291 if (ldb_ctx
->err_string
== NULL
) {
1292 /* no error string was setup by the backend */
1293 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
1297 talloc_free(mem_ctx
);
1298 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1303 static PyObject
*py_ldb_schema_attribute_remove(PyLdbObject
*self
, PyObject
*args
)
1306 if (!PyArg_ParseTuple(args
, "s", &name
))
1309 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self
), name
);
1314 static PyObject
*py_ldb_schema_attribute_add(PyLdbObject
*self
, PyObject
*args
)
1316 char *attribute
, *syntax
;
1319 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
1322 ret
= ldb_schema_attribute_add(PyLdb_AsLdbContext(self
), attribute
, flags
, syntax
);
1324 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
1329 static PyObject
*ldb_ldif_to_pyobject(struct ldb_ldif
*ldif
)
1334 /* We don't want this attached to the 'ldb' any more */
1335 return Py_BuildValue(discard_const_p(char, "(iO)"),
1337 PyLdbMessage_FromMessage(ldif
->msg
));
1342 static PyObject
*py_ldb_write_ldif(PyLdbObject
*self
, PyObject
*args
)
1346 struct ldb_ldif ldif
;
1349 TALLOC_CTX
*mem_ctx
;
1351 if (!PyArg_ParseTuple(args
, "Oi", &py_msg
, &changetype
))
1354 if (!PyLdbMessage_Check(py_msg
)) {
1355 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for msg");
1359 ldif
.msg
= PyLdbMessage_AsMessage(py_msg
);
1360 ldif
.changetype
= changetype
;
1362 mem_ctx
= talloc_new(NULL
);
1364 string
= ldb_ldif_write_string(PyLdb_AsLdbContext(self
), mem_ctx
, &ldif
);
1366 PyErr_SetString(PyExc_KeyError
, "Failed to generate LDIF");
1370 ret
= PyString_FromString(string
);
1372 talloc_free(mem_ctx
);
1377 static PyObject
*py_ldb_parse_ldif(PyLdbObject
*self
, PyObject
*args
)
1380 struct ldb_ldif
*ldif
;
1383 TALLOC_CTX
*mem_ctx
;
1385 if (!PyArg_ParseTuple(args
, "s", &s
))
1388 mem_ctx
= talloc_new(NULL
);
1393 list
= PyList_New(0);
1394 while (s
&& *s
!= '\0') {
1395 ldif
= ldb_ldif_read_string(self
->ldb_ctx
, &s
);
1396 talloc_steal(mem_ctx
, ldif
);
1398 PyList_Append(list
, ldb_ldif_to_pyobject(ldif
));
1400 PyErr_SetString(PyExc_ValueError
, "unable to parse ldif string");
1401 talloc_free(mem_ctx
);
1405 talloc_free(mem_ctx
); /* The pyobject already has a reference to the things it needs */
1406 return PyObject_GetIter(list
);
1409 static PyObject
*py_ldb_msg_diff(PyLdbObject
*self
, PyObject
*args
)
1412 PyObject
*py_msg_old
;
1413 PyObject
*py_msg_new
;
1414 struct ldb_message
*diff
;
1415 struct ldb_context
*ldb
;
1418 if (!PyArg_ParseTuple(args
, "OO", &py_msg_old
, &py_msg_new
))
1421 if (!PyLdbMessage_Check(py_msg_old
)) {
1422 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for old message");
1426 if (!PyLdbMessage_Check(py_msg_new
)) {
1427 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for new message");
1431 ldb
= PyLdb_AsLdbContext(self
);
1432 ldb_ret
= ldb_msg_difference(ldb
, ldb
,
1433 PyLdbMessage_AsMessage(py_msg_old
),
1434 PyLdbMessage_AsMessage(py_msg_new
),
1436 if (ldb_ret
!= LDB_SUCCESS
) {
1437 PyErr_SetString(PyExc_RuntimeError
, "Failed to generate the Ldb Message diff");
1441 py_ret
= PyLdbMessage_FromMessage(diff
);
1443 talloc_unlink(ldb
, diff
);
1448 static PyObject
*py_ldb_schema_format_value(PyLdbObject
*self
, PyObject
*args
)
1450 const struct ldb_schema_attribute
*a
;
1451 struct ldb_val old_val
;
1452 struct ldb_val new_val
;
1453 TALLOC_CTX
*mem_ctx
;
1458 if (!PyArg_ParseTuple(args
, "sO", &element_name
, &val
))
1461 mem_ctx
= talloc_new(NULL
);
1463 old_val
.data
= (uint8_t *)PyString_AsString(val
);
1464 old_val
.length
= PyString_Size(val
);
1466 a
= ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self
), element_name
);
1472 if (a
->syntax
->ldif_write_fn(PyLdb_AsLdbContext(self
), mem_ctx
, &old_val
, &new_val
) != 0) {
1473 talloc_free(mem_ctx
);
1477 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
1479 talloc_free(mem_ctx
);
1484 static PyObject
*py_ldb_search(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1486 PyObject
*py_base
= Py_None
;
1487 int scope
= LDB_SCOPE_DEFAULT
;
1489 PyObject
*py_attrs
= Py_None
;
1490 PyObject
*py_controls
= Py_None
;
1491 const char * const kwnames
[] = { "base", "scope", "expression", "attrs", "controls", NULL
};
1493 struct ldb_result
*res
;
1494 struct ldb_request
*req
;
1496 struct ldb_context
*ldb_ctx
;
1497 struct ldb_control
**parsed_controls
;
1498 struct ldb_dn
*base
;
1500 TALLOC_CTX
*mem_ctx
;
1502 /* type "int" rather than "enum" for "scope" is intentional */
1503 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OizOO",
1504 discard_const_p(char *, kwnames
),
1505 &py_base
, &scope
, &expr
, &py_attrs
, &py_controls
))
1509 mem_ctx
= talloc_new(NULL
);
1510 if (mem_ctx
== NULL
) {
1514 ldb_ctx
= PyLdb_AsLdbContext(self
);
1516 if (py_attrs
== Py_None
) {
1519 attrs
= PyList_AsStringList(mem_ctx
, py_attrs
, "attrs");
1520 if (attrs
== NULL
) {
1521 talloc_free(mem_ctx
);
1526 if (py_base
== Py_None
) {
1527 base
= ldb_get_default_basedn(ldb_ctx
);
1529 if (!PyObject_AsDn(ldb_ctx
, py_base
, ldb_ctx
, &base
)) {
1535 if (py_controls
== Py_None
) {
1536 parsed_controls
= NULL
;
1538 const char **controls
= PyList_AsStringList(mem_ctx
, py_controls
, "controls");
1539 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, mem_ctx
, controls
);
1540 talloc_free(controls
);
1543 res
= talloc_zero(mem_ctx
, struct ldb_result
);
1546 talloc_free(mem_ctx
);
1550 ret
= ldb_build_search_req(&req
, ldb_ctx
, mem_ctx
,
1557 ldb_search_default_callback
,
1560 if (ret
!= LDB_SUCCESS
) {
1561 talloc_free(mem_ctx
);
1562 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1566 talloc_steal(req
, attrs
);
1568 ret
= ldb_request(ldb_ctx
, req
);
1570 if (ret
== LDB_SUCCESS
) {
1571 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1574 if (ret
!= LDB_SUCCESS
) {
1575 talloc_free(mem_ctx
);
1576 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1580 py_ret
= PyLdbResult_FromResult(res
);
1582 talloc_free(mem_ctx
);
1587 static PyObject
*py_ldb_get_opaque(PyLdbObject
*self
, PyObject
*args
)
1592 if (!PyArg_ParseTuple(args
, "s", &name
))
1595 data
= ldb_get_opaque(PyLdb_AsLdbContext(self
), name
);
1600 /* FIXME: More interpretation */
1605 static PyObject
*py_ldb_set_opaque(PyLdbObject
*self
, PyObject
*args
)
1610 if (!PyArg_ParseTuple(args
, "sO", &name
, &data
))
1613 /* FIXME: More interpretation */
1615 ldb_set_opaque(PyLdb_AsLdbContext(self
), name
, data
);
1620 static PyObject
*py_ldb_modules(PyLdbObject
*self
)
1622 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1623 PyObject
*ret
= PyList_New(0);
1624 struct ldb_module
*mod
;
1626 for (mod
= ldb
->modules
; mod
; mod
= mod
->next
) {
1627 PyList_Append(ret
, PyLdbModule_FromModule(mod
));
1633 static PyObject
*py_ldb_sequence_number(PyLdbObject
*self
, PyObject
*args
)
1635 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1639 if (!PyArg_ParseTuple(args
, "i", &type
))
1642 /* FIXME: More interpretation */
1644 ret
= ldb_sequence_number(ldb
, type
, &value
);
1646 if (ret
!= LDB_SUCCESS
) {
1647 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
1650 return PyLong_FromLongLong(value
);
1652 static PyMethodDef py_ldb_methods
[] = {
1653 { "set_debug", (PyCFunction
)py_ldb_set_debug
, METH_VARARGS
,
1654 "S.set_debug(callback) -> None\n"
1655 "Set callback for LDB debug messages.\n"
1656 "The callback should accept a debug level and debug text." },
1657 { "set_create_perms", (PyCFunction
)py_ldb_set_create_perms
, METH_VARARGS
,
1658 "S.set_create_perms(mode) -> None\n"
1659 "Set mode to use when creating new LDB files." },
1660 { "set_modules_dir", (PyCFunction
)py_ldb_set_modules_dir
, METH_VARARGS
,
1661 "S.set_modules_dir(path) -> None\n"
1662 "Set path LDB should search for modules" },
1663 { "transaction_start", (PyCFunction
)py_ldb_transaction_start
, METH_NOARGS
,
1664 "S.transaction_start() -> None\n"
1665 "Start a new transaction." },
1666 { "transaction_prepare_commit", (PyCFunction
)py_ldb_transaction_prepare_commit
, METH_NOARGS
,
1667 "S.transaction_prepare_commit() -> None\n"
1668 "prepare to commit a new transaction (2-stage commit)." },
1669 { "transaction_commit", (PyCFunction
)py_ldb_transaction_commit
, METH_NOARGS
,
1670 "S.transaction_commit() -> None\n"
1671 "commit a new transaction." },
1672 { "transaction_cancel", (PyCFunction
)py_ldb_transaction_cancel
, METH_NOARGS
,
1673 "S.transaction_cancel() -> None\n"
1674 "cancel a new transaction." },
1675 { "setup_wellknown_attributes", (PyCFunction
)py_ldb_setup_wellknown_attributes
, METH_NOARGS
,
1677 { "get_root_basedn", (PyCFunction
)py_ldb_get_root_basedn
, METH_NOARGS
,
1679 { "get_schema_basedn", (PyCFunction
)py_ldb_get_schema_basedn
, METH_NOARGS
,
1681 { "get_default_basedn", (PyCFunction
)py_ldb_get_default_basedn
, METH_NOARGS
,
1683 { "get_config_basedn", (PyCFunction
)py_ldb_get_config_basedn
, METH_NOARGS
,
1685 { "connect", (PyCFunction
)py_ldb_connect
, METH_VARARGS
|METH_KEYWORDS
,
1686 "S.connect(url, flags=0, options=None) -> None\n"
1687 "Connect to a LDB URL." },
1688 { "modify", (PyCFunction
)py_ldb_modify
, METH_VARARGS
|METH_KEYWORDS
,
1689 "S.modify(message, controls=None, validate=False) -> None\n"
1690 "Modify an entry." },
1691 { "add", (PyCFunction
)py_ldb_add
, METH_VARARGS
|METH_KEYWORDS
,
1692 "S.add(message, controls=None) -> None\n"
1694 { "delete", (PyCFunction
)py_ldb_delete
, METH_VARARGS
|METH_KEYWORDS
,
1695 "S.delete(dn, controls=None) -> None\n"
1696 "Remove an entry." },
1697 { "rename", (PyCFunction
)py_ldb_rename
, METH_VARARGS
|METH_KEYWORDS
,
1698 "S.rename(old_dn, new_dn, controls=None) -> None\n"
1699 "Rename an entry." },
1700 { "search", (PyCFunction
)py_ldb_search
, METH_VARARGS
|METH_KEYWORDS
,
1701 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1702 "Search in a database.\n"
1704 ":param base: Optional base DN to search\n"
1705 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1706 ":param expression: Optional search expression\n"
1707 ":param attrs: Attributes to return (defaults to all)\n"
1708 ":param controls: Optional list of controls\n"
1709 ":return: Iterator over Message objects\n"
1711 { "schema_attribute_remove", (PyCFunction
)py_ldb_schema_attribute_remove
, METH_VARARGS
,
1713 { "schema_attribute_add", (PyCFunction
)py_ldb_schema_attribute_add
, METH_VARARGS
,
1715 { "schema_format_value", (PyCFunction
)py_ldb_schema_format_value
, METH_VARARGS
,
1717 { "parse_ldif", (PyCFunction
)py_ldb_parse_ldif
, METH_VARARGS
,
1718 "S.parse_ldif(ldif) -> iter(messages)\n"
1719 "Parse a string formatted using LDIF." },
1720 { "write_ldif", (PyCFunction
)py_ldb_write_ldif
, METH_VARARGS
,
1721 "S.write_ldif(message, changetype) -> ldif\n"
1722 "Print the message as a string formatted using LDIF." },
1723 { "msg_diff", (PyCFunction
)py_ldb_msg_diff
, METH_VARARGS
,
1724 "S.msg_diff(Message) -> Message\n"
1725 "Return an LDB Message of the difference between two Message objects." },
1726 { "get_opaque", (PyCFunction
)py_ldb_get_opaque
, METH_VARARGS
,
1727 "S.get_opaque(name) -> value\n"
1728 "Get an opaque value set on this LDB connection. \n"
1729 ":note: The returned value may not be useful in Python."
1731 { "set_opaque", (PyCFunction
)py_ldb_set_opaque
, METH_VARARGS
,
1732 "S.set_opaque(name, value) -> None\n"
1733 "Set an opaque value on this LDB connection. \n"
1734 ":note: Passing incorrect values may cause crashes." },
1735 { "modules", (PyCFunction
)py_ldb_modules
, METH_NOARGS
,
1736 "S.modules() -> list\n"
1737 "Return the list of modules on this LDB connection " },
1738 { "sequence_number", (PyCFunction
)py_ldb_sequence_number
, METH_VARARGS
,
1739 "S.sequence_number(type) -> value\n"
1740 "Return the value of the sequence according to the requested type" },
1744 static PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
)
1746 PyLdbModuleObject
*ret
;
1748 ret
= (PyLdbModuleObject
*)PyLdbModule
.tp_alloc(&PyLdbModule
, 0);
1753 ret
->mem_ctx
= talloc_new(NULL
);
1754 ret
->mod
= talloc_reference(ret
->mem_ctx
, mod
);
1755 return (PyObject
*)ret
;
1758 static PyObject
*py_ldb_get_firstmodule(PyLdbObject
*self
, void *closure
)
1760 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self
)->modules
);
1763 static PyGetSetDef py_ldb_getset
[] = {
1764 { discard_const_p(char, "firstmodule"), (getter
)py_ldb_get_firstmodule
, NULL
, NULL
},
1768 static int py_ldb_contains(PyLdbObject
*self
, PyObject
*obj
)
1770 struct ldb_context
*ldb_ctx
= PyLdb_AsLdbContext(self
);
1772 struct ldb_result
*result
;
1776 if (!PyObject_AsDn(ldb_ctx
, obj
, ldb_ctx
, &dn
)) {
1780 ret
= ldb_search(ldb_ctx
, ldb_ctx
, &result
, dn
, LDB_SCOPE_BASE
, NULL
,
1782 if (ret
!= LDB_SUCCESS
) {
1783 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb_ctx
);
1787 count
= result
->count
;
1789 talloc_free(result
);
1792 PyErr_Format(PyExc_RuntimeError
,
1793 "Searching for [%s] dn gave %u results!",
1794 ldb_dn_get_linearized(dn
),
1802 static PySequenceMethods py_ldb_seq
= {
1803 .sq_contains
= (objobjproc
)py_ldb_contains
,
1806 static PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
)
1810 ret
= (PyLdbObject
*)PyLdb
.tp_alloc(&PyLdb
, 0);
1815 ret
->mem_ctx
= talloc_new(NULL
);
1816 ret
->ldb_ctx
= talloc_reference(ret
->mem_ctx
, ldb_ctx
);
1817 return (PyObject
*)ret
;
1820 static void py_ldb_dealloc(PyLdbObject
*self
)
1822 talloc_free(self
->mem_ctx
);
1823 self
->ob_type
->tp_free(self
);
1826 static PyTypeObject PyLdb
= {
1827 .tp_name
= "ldb.Ldb",
1828 .tp_methods
= py_ldb_methods
,
1829 .tp_repr
= (reprfunc
)py_ldb_repr
,
1830 .tp_new
= py_ldb_new
,
1831 .tp_init
= (initproc
)py_ldb_init
,
1832 .tp_dealloc
= (destructor
)py_ldb_dealloc
,
1833 .tp_getset
= py_ldb_getset
,
1834 .tp_getattro
= PyObject_GenericGetAttr
,
1835 .tp_basicsize
= sizeof(PyLdbObject
),
1836 .tp_doc
= "Connection to a LDB database.",
1837 .tp_as_sequence
= &py_ldb_seq
,
1838 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1841 static void py_ldb_result_dealloc(PyLdbResultObject
*self
)
1843 talloc_free(self
->mem_ctx
);
1844 Py_DECREF(self
->msgs
);
1845 Py_DECREF(self
->referals
);
1846 Py_DECREF(self
->controls
);
1847 self
->ob_type
->tp_free(self
);
1850 static PyObject
*py_ldb_result_get_msgs(PyLdbResultObject
*self
, void *closure
)
1852 Py_INCREF(self
->msgs
);
1856 static PyObject
*py_ldb_result_get_controls(PyLdbResultObject
*self
, void *closure
)
1858 Py_INCREF(self
->controls
);
1859 return self
->controls
;
1862 static PyObject
*py_ldb_result_get_referals(PyLdbResultObject
*self
, void *closure
)
1864 Py_INCREF(self
->referals
);
1865 return self
->referals
;
1868 static PyObject
*py_ldb_result_get_count(PyLdbResultObject
*self
, void *closure
)
1871 if (self
->msgs
== NULL
) {
1872 PyErr_SetString(PyExc_AttributeError
, "Count attribute is meaningless in this context");
1875 size
= PyList_Size(self
->msgs
);
1876 return PyInt_FromLong(size
);
1879 static PyGetSetDef py_ldb_result_getset
[] = {
1880 { discard_const_p(char, "controls"), (getter
)py_ldb_result_get_controls
, NULL
, NULL
},
1881 { discard_const_p(char, "msgs"), (getter
)py_ldb_result_get_msgs
, NULL
, NULL
},
1882 { discard_const_p(char, "referals"), (getter
)py_ldb_result_get_referals
, NULL
, NULL
},
1883 { discard_const_p(char, "count"), (getter
)py_ldb_result_get_count
, NULL
, NULL
},
1887 static PyObject
*py_ldb_result_iter(PyLdbResultObject
*self
)
1889 return PyObject_GetIter(self
->msgs
);
1892 static Py_ssize_t
py_ldb_result_len(PyLdbResultObject
*self
)
1894 return PySequence_Size(self
->msgs
);
1897 static PyObject
*py_ldb_result_find(PyLdbResultObject
*self
, Py_ssize_t idx
)
1899 return PySequence_GetItem(self
->msgs
, idx
);
1902 static PySequenceMethods py_ldb_result_seq
= {
1903 .sq_length
= (lenfunc
)py_ldb_result_len
,
1904 .sq_item
= (ssizeargfunc
)py_ldb_result_find
,
1907 static PyObject
*py_ldb_result_repr(PyLdbObject
*self
)
1909 return PyString_FromFormat("<ldb result>");
1913 static PyTypeObject PyLdbResult
= {
1914 .tp_name
= "ldb.Result",
1915 .tp_repr
= (reprfunc
)py_ldb_result_repr
,
1916 .tp_dealloc
= (destructor
)py_ldb_result_dealloc
,
1917 .tp_iter
= (getiterfunc
)py_ldb_result_iter
,
1918 .tp_getset
= py_ldb_result_getset
,
1919 .tp_getattro
= PyObject_GenericGetAttr
,
1920 .tp_basicsize
= sizeof(PyLdbResultObject
),
1921 .tp_as_sequence
= &py_ldb_result_seq
,
1922 .tp_doc
= "LDB result.",
1923 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1926 static PyObject
*py_ldb_module_repr(PyLdbModuleObject
*self
)
1928 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self
)->ops
->name
);
1931 static PyObject
*py_ldb_module_str(PyLdbModuleObject
*self
)
1933 return PyString_FromString(PyLdbModule_AsModule(self
)->ops
->name
);
1936 static PyObject
*py_ldb_module_start_transaction(PyLdbModuleObject
*self
)
1938 PyLdbModule_AsModule(self
)->ops
->start_transaction(PyLdbModule_AsModule(self
));
1942 static PyObject
*py_ldb_module_end_transaction(PyLdbModuleObject
*self
)
1944 PyLdbModule_AsModule(self
)->ops
->end_transaction(PyLdbModule_AsModule(self
));
1948 static PyObject
*py_ldb_module_del_transaction(PyLdbModuleObject
*self
)
1950 PyLdbModule_AsModule(self
)->ops
->del_transaction(PyLdbModule_AsModule(self
));
1954 static PyObject
*py_ldb_module_search(PyLdbModuleObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1956 PyObject
*py_base
, *py_tree
, *py_attrs
, *py_ret
;
1958 struct ldb_request
*req
;
1959 const char * const kwnames
[] = { "base", "scope", "tree", "attrs", NULL
};
1960 struct ldb_module
*mod
;
1961 const char * const*attrs
;
1963 /* type "int" rather than "enum" for "scope" is intentional */
1964 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OiOO",
1965 discard_const_p(char *, kwnames
),
1966 &py_base
, &scope
, &py_tree
, &py_attrs
))
1971 if (py_attrs
== Py_None
) {
1974 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1979 ret
= ldb_build_search_req(&req
, mod
->ldb
, NULL
, PyLdbDn_AsDn(py_base
),
1980 scope
, NULL
/* expr */, attrs
,
1981 NULL
/* controls */, NULL
, NULL
, NULL
);
1983 talloc_steal(req
, attrs
);
1985 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1987 req
->op
.search
.res
= NULL
;
1989 ret
= mod
->ops
->search(mod
, req
);
1991 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1993 py_ret
= PyLdbResult_FromResult(req
->op
.search
.res
);
2001 static PyObject
*py_ldb_module_add(PyLdbModuleObject
*self
, PyObject
*args
)
2003 struct ldb_request
*req
;
2004 PyObject
*py_message
;
2006 struct ldb_module
*mod
;
2008 if (!PyArg_ParseTuple(args
, "O", &py_message
))
2011 req
= talloc_zero(NULL
, struct ldb_request
);
2012 req
->operation
= LDB_ADD
;
2013 req
->op
.add
.message
= PyLdbMessage_AsMessage(py_message
);
2015 mod
= PyLdbModule_AsModule(self
);
2016 ret
= mod
->ops
->add(mod
, req
);
2018 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
2023 static PyObject
*py_ldb_module_modify(PyLdbModuleObject
*self
, PyObject
*args
)
2026 struct ldb_request
*req
;
2027 PyObject
*py_message
;
2028 struct ldb_module
*mod
;
2030 if (!PyArg_ParseTuple(args
, "O", &py_message
))
2033 req
= talloc_zero(NULL
, struct ldb_request
);
2034 req
->operation
= LDB_MODIFY
;
2035 req
->op
.mod
.message
= PyLdbMessage_AsMessage(py_message
);
2037 mod
= PyLdbModule_AsModule(self
);
2038 ret
= mod
->ops
->modify(mod
, req
);
2040 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
2045 static PyObject
*py_ldb_module_delete(PyLdbModuleObject
*self
, PyObject
*args
)
2048 struct ldb_request
*req
;
2051 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
2054 req
= talloc_zero(NULL
, struct ldb_request
);
2055 req
->operation
= LDB_DELETE
;
2056 req
->op
.del
.dn
= PyLdbDn_AsDn(py_dn
);
2058 ret
= PyLdbModule_AsModule(self
)->ops
->del(PyLdbModule_AsModule(self
), req
);
2060 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2065 static PyObject
*py_ldb_module_rename(PyLdbModuleObject
*self
, PyObject
*args
)
2068 struct ldb_request
*req
;
2069 PyObject
*py_dn1
, *py_dn2
;
2071 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
2074 req
= talloc_zero(NULL
, struct ldb_request
);
2076 req
->operation
= LDB_RENAME
;
2077 req
->op
.rename
.olddn
= PyLdbDn_AsDn(py_dn1
);
2078 req
->op
.rename
.newdn
= PyLdbDn_AsDn(py_dn2
);
2080 ret
= PyLdbModule_AsModule(self
)->ops
->rename(PyLdbModule_AsModule(self
), req
);
2082 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2087 static PyMethodDef py_ldb_module_methods
[] = {
2088 { "search", (PyCFunction
)py_ldb_module_search
, METH_VARARGS
|METH_KEYWORDS
, NULL
},
2089 { "add", (PyCFunction
)py_ldb_module_add
, METH_VARARGS
, NULL
},
2090 { "modify", (PyCFunction
)py_ldb_module_modify
, METH_VARARGS
, NULL
},
2091 { "rename", (PyCFunction
)py_ldb_module_rename
, METH_VARARGS
, NULL
},
2092 { "delete", (PyCFunction
)py_ldb_module_delete
, METH_VARARGS
, NULL
},
2093 { "start_transaction", (PyCFunction
)py_ldb_module_start_transaction
, METH_NOARGS
, NULL
},
2094 { "end_transaction", (PyCFunction
)py_ldb_module_end_transaction
, METH_NOARGS
, NULL
},
2095 { "del_transaction", (PyCFunction
)py_ldb_module_del_transaction
, METH_NOARGS
, NULL
},
2099 static void py_ldb_module_dealloc(PyLdbModuleObject
*self
)
2101 talloc_free(self
->mem_ctx
);
2105 static PyTypeObject PyLdbModule
= {
2106 .tp_name
= "ldb.LdbModule",
2107 .tp_methods
= py_ldb_module_methods
,
2108 .tp_repr
= (reprfunc
)py_ldb_module_repr
,
2109 .tp_str
= (reprfunc
)py_ldb_module_str
,
2110 .tp_basicsize
= sizeof(PyLdbModuleObject
),
2111 .tp_dealloc
= (destructor
)py_ldb_module_dealloc
,
2112 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2117 * Create a ldb_message_element from a Python object.
2119 * This will accept any sequence objects that contains strings, or
2122 * A reference to set_obj will be borrowed.
2124 * @param mem_ctx Memory context
2125 * @param set_obj Python object to convert
2126 * @param flags ldb_message_element flags to set
2127 * @param attr_name Name of the attribute
2128 * @return New ldb_message_element, allocated as child of mem_ctx
2130 static struct ldb_message_element
*PyObject_AsMessageElement(
2131 TALLOC_CTX
*mem_ctx
,
2134 const char *attr_name
)
2136 struct ldb_message_element
*me
;
2138 if (PyLdbMessageElement_Check(set_obj
)) {
2139 PyLdbMessageElementObject
*set_obj_as_me
= (PyLdbMessageElementObject
*)set_obj
;
2140 /* We have to talloc_reference() the memory context, not the pointer
2141 * which may not actually be it's own context */
2142 if (talloc_reference(mem_ctx
, set_obj_as_me
->mem_ctx
)) {
2143 return PyLdbMessageElement_AsMessageElement(set_obj
);
2148 me
= talloc(mem_ctx
, struct ldb_message_element
);
2154 me
->name
= talloc_strdup(me
, attr_name
);
2156 if (PyString_Check(set_obj
)) {
2158 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
2159 me
->values
[0].length
= PyString_Size(set_obj
);
2160 me
->values
[0].data
= talloc_memdup(me
,
2161 (uint8_t *)PyString_AsString(set_obj
), me
->values
[0].length
+1);
2162 } else if (PySequence_Check(set_obj
)) {
2164 me
->num_values
= PySequence_Size(set_obj
);
2165 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
2166 for (i
= 0; i
< me
->num_values
; i
++) {
2167 PyObject
*obj
= PySequence_GetItem(set_obj
, i
);
2168 if (!PyString_Check(obj
)) {
2169 PyErr_Format(PyExc_TypeError
,
2170 "Expected string as element %zd in list", i
);
2175 me
->values
[i
].length
= PyString_Size(obj
);
2176 me
->values
[i
].data
= talloc_memdup(me
,
2177 (uint8_t *)PyString_AsString(obj
), me
->values
[i
].length
+1);
2188 static PyObject
*ldb_msg_element_to_set(struct ldb_context
*ldb_ctx
,
2189 struct ldb_message_element
*me
)
2194 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
2195 result
= PyList_New(me
->num_values
);
2197 for (i
= 0; i
< me
->num_values
; i
++) {
2198 PyList_SetItem(result
, i
,
2199 PyObject_FromLdbValue(&me
->values
[i
]));
2205 static PyObject
*py_ldb_msg_element_get(PyLdbMessageElementObject
*self
, PyObject
*args
)
2208 if (!PyArg_ParseTuple(args
, "I", &i
))
2210 if (i
>= PyLdbMessageElement_AsMessageElement(self
)->num_values
)
2213 return PyObject_FromLdbValue(&(PyLdbMessageElement_AsMessageElement(self
)->values
[i
]));
2216 static PyObject
*py_ldb_msg_element_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
2218 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2219 return PyInt_FromLong(el
->flags
);
2222 static PyObject
*py_ldb_msg_element_set_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
2225 struct ldb_message_element
*el
;
2226 if (!PyArg_ParseTuple(args
, "I", &flags
))
2229 el
= PyLdbMessageElement_AsMessageElement(self
);
2234 static PyMethodDef py_ldb_msg_element_methods
[] = {
2235 { "get", (PyCFunction
)py_ldb_msg_element_get
, METH_VARARGS
, NULL
},
2236 { "set_flags", (PyCFunction
)py_ldb_msg_element_set_flags
, METH_VARARGS
, NULL
},
2237 { "flags", (PyCFunction
)py_ldb_msg_element_flags
, METH_NOARGS
, NULL
},
2241 static Py_ssize_t
py_ldb_msg_element_len(PyLdbMessageElementObject
*self
)
2243 return PyLdbMessageElement_AsMessageElement(self
)->num_values
;
2246 static PyObject
*py_ldb_msg_element_find(PyLdbMessageElementObject
*self
, Py_ssize_t idx
)
2248 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2249 if (idx
< 0 || idx
>= el
->num_values
) {
2250 PyErr_SetString(PyExc_IndexError
, "Out of range");
2253 return PyString_FromStringAndSize((char *)el
->values
[idx
].data
, el
->values
[idx
].length
);
2256 static PySequenceMethods py_ldb_msg_element_seq
= {
2257 .sq_length
= (lenfunc
)py_ldb_msg_element_len
,
2258 .sq_item
= (ssizeargfunc
)py_ldb_msg_element_find
,
2261 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject
*self
, PyLdbMessageElementObject
*other
)
2263 int ret
= ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self
),
2264 PyLdbMessageElement_AsMessageElement(other
));
2268 static PyObject
*py_ldb_msg_element_iter(PyLdbMessageElementObject
*self
)
2270 return PyObject_GetIter(ldb_msg_element_to_set(NULL
, PyLdbMessageElement_AsMessageElement(self
)));
2273 static PyObject
*PyLdbMessageElement_FromMessageElement(struct ldb_message_element
*el
, TALLOC_CTX
*mem_ctx
)
2275 PyLdbMessageElementObject
*ret
;
2276 ret
= PyObject_New(PyLdbMessageElementObject
, &PyLdbMessageElement
);
2281 ret
->mem_ctx
= talloc_new(NULL
);
2282 if (talloc_reference(ret
->mem_ctx
, mem_ctx
) == NULL
) {
2287 return (PyObject
*)ret
;
2290 static PyObject
*py_ldb_msg_element_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
2292 PyObject
*py_elements
= NULL
;
2293 struct ldb_message_element
*el
;
2294 unsigned int flags
= 0;
2296 const char * const kwnames
[] = { "elements", "flags", "name", NULL
};
2297 PyLdbMessageElementObject
*ret
;
2298 TALLOC_CTX
*mem_ctx
;
2300 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OIs",
2301 discard_const_p(char *, kwnames
),
2302 &py_elements
, &flags
, &name
))
2305 mem_ctx
= talloc_new(NULL
);
2306 if (mem_ctx
== NULL
) {
2311 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
2314 talloc_free(mem_ctx
);
2318 if (py_elements
!= NULL
) {
2320 if (PyString_Check(py_elements
)) {
2322 el
->values
= talloc_array(el
, struct ldb_val
, 1);
2323 if (el
->values
== NULL
) {
2324 talloc_free(mem_ctx
);
2328 el
->values
[0].length
= PyString_Size(py_elements
);
2329 el
->values
[0].data
= talloc_memdup(el
->values
,
2330 (uint8_t *)PyString_AsString(py_elements
), el
->values
[0].length
+1);
2331 } else if (PySequence_Check(py_elements
)) {
2332 el
->num_values
= PySequence_Size(py_elements
);
2333 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
2334 if (el
->values
== NULL
) {
2335 talloc_free(mem_ctx
);
2339 for (i
= 0; i
< el
->num_values
; i
++) {
2340 PyObject
*item
= PySequence_GetItem(py_elements
, i
);
2342 talloc_free(mem_ctx
);
2345 if (!PyString_Check(item
)) {
2346 PyErr_Format(PyExc_TypeError
,
2347 "Expected string as element %zd in list", i
);
2348 talloc_free(mem_ctx
);
2351 el
->values
[i
].length
= PyString_Size(item
);
2352 el
->values
[i
].data
= talloc_memdup(el
,
2353 (uint8_t *)PyString_AsString(item
), el
->values
[i
].length
+1);
2356 PyErr_SetString(PyExc_TypeError
,
2357 "Expected string or list");
2358 talloc_free(mem_ctx
);
2364 el
->name
= talloc_strdup(el
, name
);
2366 ret
= PyObject_New(PyLdbMessageElementObject
, type
);
2368 talloc_free(mem_ctx
);
2372 ret
->mem_ctx
= mem_ctx
;
2374 return (PyObject
*)ret
;
2377 static PyObject
*py_ldb_msg_element_repr(PyLdbMessageElementObject
*self
)
2379 char *element_str
= NULL
;
2381 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2384 for (i
= 0; i
< el
->num_values
; i
++) {
2385 PyObject
*o
= py_ldb_msg_element_find(self
, i
);
2386 if (element_str
== NULL
)
2387 element_str
= talloc_strdup(NULL
, PyObject_REPR(o
));
2389 element_str
= talloc_asprintf_append(element_str
, ",%s", PyObject_REPR(o
));
2392 if (element_str
!= NULL
) {
2393 ret
= PyString_FromFormat("MessageElement([%s])", element_str
);
2394 talloc_free(element_str
);
2396 ret
= PyString_FromString("MessageElement([])");
2402 static PyObject
*py_ldb_msg_element_str(PyLdbMessageElementObject
*self
)
2404 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
2406 if (el
->num_values
== 1)
2407 return PyString_FromStringAndSize((char *)el
->values
[0].data
, el
->values
[0].length
);
2412 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject
*self
)
2414 talloc_free(self
->mem_ctx
);
2418 static PyTypeObject PyLdbMessageElement
= {
2419 .tp_name
= "ldb.MessageElement",
2420 .tp_basicsize
= sizeof(PyLdbMessageElementObject
),
2421 .tp_dealloc
= (destructor
)py_ldb_msg_element_dealloc
,
2422 .tp_repr
= (reprfunc
)py_ldb_msg_element_repr
,
2423 .tp_str
= (reprfunc
)py_ldb_msg_element_str
,
2424 .tp_methods
= py_ldb_msg_element_methods
,
2425 .tp_compare
= (cmpfunc
)py_ldb_msg_element_cmp
,
2426 .tp_iter
= (getiterfunc
)py_ldb_msg_element_iter
,
2427 .tp_as_sequence
= &py_ldb_msg_element_seq
,
2428 .tp_new
= py_ldb_msg_element_new
,
2429 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2433 static PyObject
*py_ldb_msg_from_dict(PyTypeObject
*type
, PyObject
*args
)
2438 struct ldb_message
*msg
;
2439 struct ldb_context
*ldb_ctx
;
2440 unsigned int mod_flags
= LDB_FLAG_MOD_REPLACE
;
2442 if (!PyArg_ParseTuple(args
, "O!O!|I",
2443 &PyLdb
, &py_ldb
, &PyDict_Type
, &py_dict
,
2448 /* mask only flags we are going to use */
2449 mod_flags
= LDB_FLAG_MOD_TYPE(mod_flags
);
2451 PyErr_SetString(PyExc_ValueError
,
2452 "FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE"
2453 " expected as mod_flag value");
2457 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
2459 msg
= PyDict_AsMessage(ldb_ctx
, py_dict
, ldb_ctx
, mod_flags
);
2464 py_ret
= PyLdbMessage_FromMessage(msg
);
2466 talloc_unlink(ldb_ctx
, msg
);
2471 static PyObject
*py_ldb_msg_remove_attr(PyLdbMessageObject
*self
, PyObject
*args
)
2474 if (!PyArg_ParseTuple(args
, "s", &name
))
2477 ldb_msg_remove_attr(self
->msg
, name
);
2482 static PyObject
*py_ldb_msg_keys(PyLdbMessageObject
*self
)
2484 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2485 Py_ssize_t i
, j
= 0;
2486 PyObject
*obj
= PyList_New(msg
->num_elements
+(msg
->dn
!= NULL
?1:0));
2487 if (msg
->dn
!= NULL
) {
2488 PyList_SetItem(obj
, j
, PyString_FromString("dn"));
2491 for (i
= 0; i
< msg
->num_elements
; i
++) {
2492 PyList_SetItem(obj
, j
, PyString_FromString(msg
->elements
[i
].name
));
2498 static PyObject
*py_ldb_msg_getitem_helper(PyLdbMessageObject
*self
, PyObject
*py_name
)
2500 struct ldb_message_element
*el
;
2502 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2503 if (!PyString_Check(py_name
)) {
2504 PyErr_SetNone(PyExc_TypeError
);
2507 name
= PyString_AsString(py_name
);
2508 if (!strcmp(name
, "dn"))
2509 return PyLdbDn_FromDn(msg
->dn
);
2510 el
= ldb_msg_find_element(msg
, name
);
2514 return (PyObject
*)PyLdbMessageElement_FromMessageElement(el
, msg
->elements
);
2517 static PyObject
*py_ldb_msg_getitem(PyLdbMessageObject
*self
, PyObject
*py_name
)
2519 PyObject
*ret
= py_ldb_msg_getitem_helper(self
, py_name
);
2521 PyErr_SetString(PyExc_KeyError
, "No such element");
2527 static PyObject
*py_ldb_msg_get(PyLdbMessageObject
*self
, PyObject
*args
)
2529 PyObject
*name
, *ret
, *retobj
;
2531 if (!PyArg_ParseTuple(args
, "O|O", &name
, &retobj
))
2534 ret
= py_ldb_msg_getitem_helper(self
, name
);
2536 if (PyErr_Occurred())
2538 if (retobj
!= NULL
) {
2547 static PyObject
*py_ldb_msg_items(PyLdbMessageObject
*self
)
2549 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2550 Py_ssize_t i
, j
= 0;
2551 PyObject
*l
= PyList_New(msg
->num_elements
+ (msg
->dn
== NULL
?0:1));
2552 if (msg
->dn
!= NULL
) {
2553 PyList_SetItem(l
, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg
->dn
)));
2556 for (i
= 0; i
< msg
->num_elements
; i
++, j
++) {
2557 PyObject
*py_el
= PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
);
2558 PyObject
*value
= Py_BuildValue("(sO)", msg
->elements
[i
].name
, py_el
);
2559 PyList_SetItem(l
, j
, value
);
2564 static PyObject
*py_ldb_msg_elements(PyLdbMessageObject
*self
)
2566 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2568 PyObject
*l
= PyList_New(msg
->num_elements
);
2569 for (i
= 0; i
< msg
->num_elements
; i
++) {
2570 PyList_SetItem(l
, i
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], msg
->elements
));
2575 static PyObject
*py_ldb_msg_add(PyLdbMessageObject
*self
, PyObject
*args
)
2577 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2578 PyLdbMessageElementObject
*py_element
;
2580 struct ldb_message_element
*el
;
2582 if (!PyArg_ParseTuple(args
, "O!", &PyLdbMessageElement
, &py_element
))
2585 el
= talloc_reference(msg
, py_element
->el
);
2591 ret
= ldb_msg_add(msg
, el
, el
->flags
);
2592 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2597 static PyMethodDef py_ldb_msg_methods
[] = {
2598 { "from_dict", (PyCFunction
)py_ldb_msg_from_dict
, METH_CLASS
| METH_VARARGS
,
2599 "Message.from_dict(ldb, dict, mod_flag=FLAG_MOD_REPLACE) -> ldb.Message\n"
2600 "Class method to create ldb.Message object from Dictionary.\n"
2601 "mod_flag is one of FLAG_MOD_ADD, FLAG_MOD_REPLACE or FLAG_MOD_DELETE."},
2602 { "keys", (PyCFunction
)py_ldb_msg_keys
, METH_NOARGS
,
2603 "S.keys() -> list\n\n"
2604 "Return sequence of all attribute names." },
2605 { "remove", (PyCFunction
)py_ldb_msg_remove_attr
, METH_VARARGS
,
2606 "S.remove(name)\n\n"
2607 "Remove all entries for attributes with the specified name."},
2608 { "get", (PyCFunction
)py_ldb_msg_get
, METH_VARARGS
, NULL
},
2609 { "items", (PyCFunction
)py_ldb_msg_items
, METH_NOARGS
, NULL
},
2610 { "elements", (PyCFunction
)py_ldb_msg_elements
, METH_NOARGS
, NULL
},
2611 { "add", (PyCFunction
)py_ldb_msg_add
, METH_VARARGS
,
2612 "S.append(element)\n\n"
2613 "Add an element to this message." },
2617 static PyObject
*py_ldb_msg_iter(PyLdbMessageObject
*self
)
2619 PyObject
*list
, *iter
;
2621 list
= py_ldb_msg_keys(self
);
2622 iter
= PyObject_GetIter(list
);
2627 static int py_ldb_msg_setitem(PyLdbMessageObject
*self
, PyObject
*name
, PyObject
*value
)
2631 if (!PyString_Check(name
)) {
2632 PyErr_SetNone(PyExc_TypeError
);
2636 attr_name
= PyString_AsString(name
);
2637 if (value
== NULL
) {
2639 ldb_msg_remove_attr(self
->msg
, attr_name
);
2641 struct ldb_message_element
*el
= PyObject_AsMessageElement(self
->msg
,
2642 value
, 0, attr_name
);
2645 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self
), attr_name
);
2646 ldb_msg_add(PyLdbMessage_AsMessage(self
), el
, el
->flags
);
2651 static Py_ssize_t
py_ldb_msg_length(PyLdbMessageObject
*self
)
2653 return PyLdbMessage_AsMessage(self
)->num_elements
;
2656 static PyMappingMethods py_ldb_msg_mapping
= {
2657 .mp_length
= (lenfunc
)py_ldb_msg_length
,
2658 .mp_subscript
= (binaryfunc
)py_ldb_msg_getitem
,
2659 .mp_ass_subscript
= (objobjargproc
)py_ldb_msg_setitem
,
2662 static PyObject
*py_ldb_msg_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
2664 const char * const kwnames
[] = { "dn", NULL
};
2665 struct ldb_message
*ret
;
2666 TALLOC_CTX
*mem_ctx
;
2667 PyObject
*pydn
= NULL
;
2668 PyLdbMessageObject
*py_ret
;
2670 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O",
2671 discard_const_p(char *, kwnames
),
2675 mem_ctx
= talloc_new(NULL
);
2676 if (mem_ctx
== NULL
) {
2681 ret
= ldb_msg_new(mem_ctx
);
2683 talloc_free(mem_ctx
);
2690 if (!PyObject_AsDn(NULL
, pydn
, NULL
, &dn
)) {
2691 talloc_free(mem_ctx
);
2694 ret
->dn
= talloc_reference(ret
, dn
);
2697 py_ret
= (PyLdbMessageObject
*)type
->tp_alloc(type
, 0);
2698 if (py_ret
== NULL
) {
2700 talloc_free(mem_ctx
);
2704 py_ret
->mem_ctx
= mem_ctx
;
2706 return (PyObject
*)py_ret
;
2709 static PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
)
2711 PyLdbMessageObject
*ret
;
2713 ret
= (PyLdbMessageObject
*)PyLdbMessage
.tp_alloc(&PyLdbMessage
, 0);
2718 ret
->mem_ctx
= talloc_new(NULL
);
2719 ret
->msg
= talloc_reference(ret
->mem_ctx
, msg
);
2720 return (PyObject
*)ret
;
2723 static PyObject
*py_ldb_msg_get_dn(PyLdbMessageObject
*self
, void *closure
)
2725 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2726 return PyLdbDn_FromDn(msg
->dn
);
2729 static int py_ldb_msg_set_dn(PyLdbMessageObject
*self
, PyObject
*value
, void *closure
)
2731 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
2732 if (!PyLdbDn_Check(value
)) {
2733 PyErr_SetNone(PyExc_TypeError
);
2737 msg
->dn
= talloc_reference(msg
, PyLdbDn_AsDn(value
));
2741 static PyGetSetDef py_ldb_msg_getset
[] = {
2742 { discard_const_p(char, "dn"), (getter
)py_ldb_msg_get_dn
, (setter
)py_ldb_msg_set_dn
, NULL
},
2746 static PyObject
*py_ldb_msg_repr(PyLdbMessageObject
*self
)
2748 PyObject
*dict
= PyDict_New(), *ret
;
2749 if (PyDict_Update(dict
, (PyObject
*)self
) != 0)
2751 ret
= PyString_FromFormat("Message(%s)", PyObject_REPR(dict
));
2756 static void py_ldb_msg_dealloc(PyLdbMessageObject
*self
)
2758 talloc_free(self
->mem_ctx
);
2762 static int py_ldb_msg_compare(PyLdbMessageObject
*py_msg1
,
2763 PyLdbMessageObject
*py_msg2
)
2765 struct ldb_message
*msg1
= PyLdbMessage_AsMessage(py_msg1
),
2766 *msg2
= PyLdbMessage_AsMessage(py_msg2
);
2770 if ((msg1
->dn
!= NULL
) || (msg2
->dn
!= NULL
)) {
2771 ret
= ldb_dn_compare(msg1
->dn
, msg2
->dn
);
2777 ret
= msg1
->num_elements
- msg2
->num_elements
;
2782 for (i
= 0; i
< msg1
->num_elements
; i
++) {
2783 ret
= ldb_msg_element_compare_name(&msg1
->elements
[i
],
2784 &msg2
->elements
[i
]);
2789 ret
= ldb_msg_element_compare(&msg1
->elements
[i
],
2790 &msg2
->elements
[i
]);
2799 static PyTypeObject PyLdbMessage
= {
2800 .tp_name
= "ldb.Message",
2801 .tp_methods
= py_ldb_msg_methods
,
2802 .tp_getset
= py_ldb_msg_getset
,
2803 .tp_as_mapping
= &py_ldb_msg_mapping
,
2804 .tp_basicsize
= sizeof(PyLdbMessageObject
),
2805 .tp_dealloc
= (destructor
)py_ldb_msg_dealloc
,
2806 .tp_new
= py_ldb_msg_new
,
2807 .tp_repr
= (reprfunc
)py_ldb_msg_repr
,
2808 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2809 .tp_iter
= (getiterfunc
)py_ldb_msg_iter
,
2810 .tp_compare
= (cmpfunc
)py_ldb_msg_compare
,
2813 static PyObject
*PyLdbTree_FromTree(struct ldb_parse_tree
*tree
)
2815 PyLdbTreeObject
*ret
;
2817 ret
= (PyLdbTreeObject
*)PyLdbTree
.tp_alloc(&PyLdbTree
, 0);
2823 ret
->mem_ctx
= talloc_new(NULL
);
2824 ret
->tree
= talloc_reference(ret
->mem_ctx
, tree
);
2825 return (PyObject
*)ret
;
2828 static void py_ldb_tree_dealloc(PyLdbTreeObject
*self
)
2830 talloc_free(self
->mem_ctx
);
2834 static PyTypeObject PyLdbTree
= {
2835 .tp_name
= "ldb.Tree",
2836 .tp_basicsize
= sizeof(PyLdbTreeObject
),
2837 .tp_dealloc
= (destructor
)py_ldb_tree_dealloc
,
2838 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2842 static int py_module_search(struct ldb_module
*mod
, struct ldb_request
*req
)
2844 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2845 PyObject
*py_result
, *py_base
, *py_attrs
, *py_tree
;
2847 py_base
= PyLdbDn_FromDn(req
->op
.search
.base
);
2849 if (py_base
== NULL
)
2850 return LDB_ERR_OPERATIONS_ERROR
;
2852 py_tree
= PyLdbTree_FromTree(req
->op
.search
.tree
);
2854 if (py_tree
== NULL
)
2855 return LDB_ERR_OPERATIONS_ERROR
;
2857 if (req
->op
.search
.attrs
== NULL
) {
2861 for (len
= 0; req
->op
.search
.attrs
[len
]; len
++);
2862 py_attrs
= PyList_New(len
);
2863 for (i
= 0; i
< len
; i
++)
2864 PyList_SetItem(py_attrs
, i
, PyString_FromString(req
->op
.search
.attrs
[i
]));
2867 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "search"),
2868 discard_const_p(char, "OiOO"),
2869 py_base
, req
->op
.search
.scope
, py_tree
, py_attrs
);
2871 Py_DECREF(py_attrs
);
2875 if (py_result
== NULL
) {
2876 return LDB_ERR_PYTHON_EXCEPTION
;
2879 req
->op
.search
.res
= PyLdbResult_AsResult(NULL
, py_result
);
2880 if (req
->op
.search
.res
== NULL
) {
2881 return LDB_ERR_PYTHON_EXCEPTION
;
2884 Py_DECREF(py_result
);
2889 static int py_module_add(struct ldb_module
*mod
, struct ldb_request
*req
)
2891 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2892 PyObject
*py_result
, *py_msg
;
2894 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.add
.message
));
2896 if (py_msg
== NULL
) {
2897 return LDB_ERR_OPERATIONS_ERROR
;
2900 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "add"),
2901 discard_const_p(char, "O"),
2906 if (py_result
== NULL
) {
2907 return LDB_ERR_PYTHON_EXCEPTION
;
2910 Py_DECREF(py_result
);
2915 static int py_module_modify(struct ldb_module
*mod
, struct ldb_request
*req
)
2917 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2918 PyObject
*py_result
, *py_msg
;
2920 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.mod
.message
));
2922 if (py_msg
== NULL
) {
2923 return LDB_ERR_OPERATIONS_ERROR
;
2926 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "modify"),
2927 discard_const_p(char, "O"),
2932 if (py_result
== NULL
) {
2933 return LDB_ERR_PYTHON_EXCEPTION
;
2936 Py_DECREF(py_result
);
2941 static int py_module_del(struct ldb_module
*mod
, struct ldb_request
*req
)
2943 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2944 PyObject
*py_result
, *py_dn
;
2946 py_dn
= PyLdbDn_FromDn(req
->op
.del
.dn
);
2949 return LDB_ERR_OPERATIONS_ERROR
;
2951 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "delete"),
2952 discard_const_p(char, "O"),
2955 if (py_result
== NULL
) {
2956 return LDB_ERR_PYTHON_EXCEPTION
;
2959 Py_DECREF(py_result
);
2964 static int py_module_rename(struct ldb_module
*mod
, struct ldb_request
*req
)
2966 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2967 PyObject
*py_result
, *py_olddn
, *py_newdn
;
2969 py_olddn
= PyLdbDn_FromDn(req
->op
.rename
.olddn
);
2971 if (py_olddn
== NULL
)
2972 return LDB_ERR_OPERATIONS_ERROR
;
2974 py_newdn
= PyLdbDn_FromDn(req
->op
.rename
.newdn
);
2976 if (py_newdn
== NULL
)
2977 return LDB_ERR_OPERATIONS_ERROR
;
2979 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "rename"),
2980 discard_const_p(char, "OO"),
2981 py_olddn
, py_newdn
);
2983 Py_DECREF(py_olddn
);
2984 Py_DECREF(py_newdn
);
2986 if (py_result
== NULL
) {
2987 return LDB_ERR_PYTHON_EXCEPTION
;
2990 Py_DECREF(py_result
);
2995 static int py_module_request(struct ldb_module
*mod
, struct ldb_request
*req
)
2997 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2998 PyObject
*py_result
;
3000 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "request"),
3001 discard_const_p(char, ""));
3003 return LDB_ERR_OPERATIONS_ERROR
;
3006 static int py_module_extended(struct ldb_module
*mod
, struct ldb_request
*req
)
3008 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
3009 PyObject
*py_result
;
3011 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "extended"),
3012 discard_const_p(char, ""));
3014 return LDB_ERR_OPERATIONS_ERROR
;
3017 static int py_module_start_transaction(struct ldb_module
*mod
)
3019 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
3020 PyObject
*py_result
;
3022 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "start_transaction"),
3023 discard_const_p(char, ""));
3025 if (py_result
== NULL
) {
3026 return LDB_ERR_PYTHON_EXCEPTION
;
3029 Py_DECREF(py_result
);
3034 static int py_module_end_transaction(struct ldb_module
*mod
)
3036 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
3037 PyObject
*py_result
;
3039 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "end_transaction"),
3040 discard_const_p(char, ""));
3042 if (py_result
== NULL
) {
3043 return LDB_ERR_PYTHON_EXCEPTION
;
3046 Py_DECREF(py_result
);
3051 static int py_module_del_transaction(struct ldb_module
*mod
)
3053 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
3054 PyObject
*py_result
;
3056 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "del_transaction"),
3057 discard_const_p(char, ""));
3059 if (py_result
== NULL
) {
3060 return LDB_ERR_PYTHON_EXCEPTION
;
3063 Py_DECREF(py_result
);
3068 static int py_module_destructor(struct ldb_module
*mod
)
3070 Py_DECREF((PyObject
*)mod
->private_data
);
3074 static int py_module_init(struct ldb_module
*mod
)
3076 PyObject
*py_class
= (PyObject
*)mod
->ops
->private_data
;
3077 PyObject
*py_result
, *py_next
, *py_ldb
;
3079 py_ldb
= PyLdb_FromLdbContext(mod
->ldb
);
3082 return LDB_ERR_OPERATIONS_ERROR
;
3084 py_next
= PyLdbModule_FromModule(mod
->next
);
3086 if (py_next
== NULL
)
3087 return LDB_ERR_OPERATIONS_ERROR
;
3089 py_result
= PyObject_CallFunction(py_class
, discard_const_p(char, "OO"),
3092 if (py_result
== NULL
) {
3093 return LDB_ERR_PYTHON_EXCEPTION
;
3096 mod
->private_data
= py_result
;
3098 talloc_set_destructor(mod
, py_module_destructor
);
3100 return ldb_next_init(mod
);
3103 static PyObject
*py_register_module(PyObject
*module
, PyObject
*args
)
3106 struct ldb_module_ops
*ops
;
3109 if (!PyArg_ParseTuple(args
, "O", &input
))
3112 ops
= talloc_zero(talloc_autofree_context(), struct ldb_module_ops
);
3118 ops
->name
= talloc_strdup(ops
, PyString_AsString(PyObject_GetAttrString(input
, discard_const_p(char, "name"))));
3121 ops
->private_data
= input
;
3122 ops
->init_context
= py_module_init
;
3123 ops
->search
= py_module_search
;
3124 ops
->add
= py_module_add
;
3125 ops
->modify
= py_module_modify
;
3126 ops
->del
= py_module_del
;
3127 ops
->rename
= py_module_rename
;
3128 ops
->request
= py_module_request
;
3129 ops
->extended
= py_module_extended
;
3130 ops
->start_transaction
= py_module_start_transaction
;
3131 ops
->end_transaction
= py_module_end_transaction
;
3132 ops
->del_transaction
= py_module_del_transaction
;
3134 ret
= ldb_register_module(ops
);
3136 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
3141 static PyObject
*py_timestring(PyObject
*module
, PyObject
*args
)
3143 /* most times "time_t" is a signed integer type with 32 or 64 bit:
3144 * http://stackoverflow.com/questions/471248/what-is-ultimately-a-time-t-typedef-to */
3148 if (!PyArg_ParseTuple(args
, "l", &t_val
))
3150 tresult
= ldb_timestring(NULL
, (time_t) t_val
);
3151 ret
= PyString_FromString(tresult
);
3152 talloc_free(tresult
);
3156 static PyObject
*py_string_to_time(PyObject
*module
, PyObject
*args
)
3159 if (!PyArg_ParseTuple(args
, "s", &str
))
3162 return PyInt_FromLong(ldb_string_to_time(str
));
3165 static PyObject
*py_valid_attr_name(PyObject
*self
, PyObject
*args
)
3168 if (!PyArg_ParseTuple(args
, "s", &name
))
3170 return PyBool_FromLong(ldb_valid_attr_name(name
));
3173 static PyMethodDef py_ldb_global_methods
[] = {
3174 { "register_module", py_register_module
, METH_VARARGS
,
3175 "S.register_module(module) -> None\n"
3176 "Register a LDB module."},
3177 { "timestring", py_timestring
, METH_VARARGS
,
3178 "S.timestring(int) -> string\n"
3179 "Generate a LDAP time string from a UNIX timestamp" },
3180 { "string_to_time", py_string_to_time
, METH_VARARGS
,
3181 "S.string_to_time(string) -> int\n"
3182 "Parse a LDAP time string into a UNIX timestamp." },
3183 { "valid_attr_name", py_valid_attr_name
, METH_VARARGS
,
3184 "S.valid_attr_name(name) -> bool\n"
3185 "Check whether the supplied name is a valid attribute name." },
3186 { "open", (PyCFunction
)py_ldb_new
, METH_VARARGS
|METH_KEYWORDS
,
3195 if (PyType_Ready(&PyLdbDn
) < 0)
3198 if (PyType_Ready(&PyLdbMessage
) < 0)
3201 if (PyType_Ready(&PyLdbMessageElement
) < 0)
3204 if (PyType_Ready(&PyLdb
) < 0)
3207 if (PyType_Ready(&PyLdbModule
) < 0)
3210 if (PyType_Ready(&PyLdbTree
) < 0)
3213 if (PyType_Ready(&PyLdbResult
) < 0)
3216 if (PyType_Ready(&PyLdbControl
) < 0)
3219 m
= Py_InitModule3("ldb", py_ldb_global_methods
,
3220 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
3224 PyModule_AddObject(m
, "SEQ_HIGHEST_SEQ", PyInt_FromLong(LDB_SEQ_HIGHEST_SEQ
));
3225 PyModule_AddObject(m
, "SEQ_HIGHEST_TIMESTAMP", PyInt_FromLong(LDB_SEQ_HIGHEST_TIMESTAMP
));
3226 PyModule_AddObject(m
, "SEQ_NEXT", PyInt_FromLong(LDB_SEQ_NEXT
));
3227 PyModule_AddObject(m
, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT
));
3228 PyModule_AddObject(m
, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE
));
3229 PyModule_AddObject(m
, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL
));
3230 PyModule_AddObject(m
, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE
));
3232 PyModule_AddObject(m
, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE
));
3233 PyModule_AddObject(m
, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD
));
3234 PyModule_AddObject(m
, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE
));
3235 PyModule_AddObject(m
, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY
));
3237 PyModule_AddObject(m
, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD
));
3238 PyModule_AddObject(m
, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE
));
3239 PyModule_AddObject(m
, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE
));
3241 PyModule_AddObject(m
, "SUCCESS", PyInt_FromLong(LDB_SUCCESS
));
3242 PyModule_AddObject(m
, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR
));
3243 PyModule_AddObject(m
, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR
));
3244 PyModule_AddObject(m
, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED
));
3245 PyModule_AddObject(m
, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED
));
3246 PyModule_AddObject(m
, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE
));
3247 PyModule_AddObject(m
, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE
));
3248 PyModule_AddObject(m
, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
));
3249 PyModule_AddObject(m
, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED
));
3250 PyModule_AddObject(m
, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL
));
3251 PyModule_AddObject(m
, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED
));
3252 PyModule_AddObject(m
, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
));
3253 PyModule_AddObject(m
, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED
));
3254 PyModule_AddObject(m
, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS
));
3255 PyModule_AddObject(m
, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE
));
3256 PyModule_AddObject(m
, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
));
3257 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING
));
3258 PyModule_AddObject(m
, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION
));
3259 PyModule_AddObject(m
, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
));
3260 PyModule_AddObject(m
, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
));
3261 PyModule_AddObject(m
, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT
));
3262 PyModule_AddObject(m
, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM
));
3263 PyModule_AddObject(m
, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX
));
3264 PyModule_AddObject(m
, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
));
3265 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION
));
3266 PyModule_AddObject(m
, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS
));
3267 PyModule_AddObject(m
, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
));
3268 PyModule_AddObject(m
, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY
));
3269 PyModule_AddObject(m
, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE
));
3270 PyModule_AddObject(m
, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM
));
3271 PyModule_AddObject(m
, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT
));
3272 PyModule_AddObject(m
, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION
));
3273 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION
));
3274 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
));
3275 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN
));
3276 PyModule_AddObject(m
, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS
));
3277 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
));
3278 PyModule_AddObject(m
, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS
));
3279 PyModule_AddObject(m
, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER
));
3281 PyModule_AddObject(m
, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY
));
3282 PyModule_AddObject(m
, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC
));
3283 PyModule_AddObject(m
, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT
));
3284 PyModule_AddObject(m
, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP
));
3286 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
3288 PyExc_LdbError
= PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL
, NULL
);
3289 PyModule_AddObject(m
, "LdbError", PyExc_LdbError
);
3292 Py_INCREF(&PyLdbDn
);
3293 Py_INCREF(&PyLdbModule
);
3294 Py_INCREF(&PyLdbMessage
);
3295 Py_INCREF(&PyLdbMessageElement
);
3296 Py_INCREF(&PyLdbTree
);
3297 Py_INCREF(&PyLdbResult
);
3298 Py_INCREF(&PyLdbControl
);
3300 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PyLdb
);
3301 PyModule_AddObject(m
, "Dn", (PyObject
*)&PyLdbDn
);
3302 PyModule_AddObject(m
, "Message", (PyObject
*)&PyLdbMessage
);
3303 PyModule_AddObject(m
, "MessageElement", (PyObject
*)&PyLdbMessageElement
);
3304 PyModule_AddObject(m
, "Module", (PyObject
*)&PyLdbModule
);
3305 PyModule_AddObject(m
, "Tree", (PyObject
*)&PyLdbTree
);
3306 PyModule_AddObject(m
, "Control", (PyObject
*)&PyLdbControl
);
3308 PyModule_AddObject(m
, "__version__", PyString_FromString(PACKAGE_VERSION
));
3310 #define ADD_LDB_STRING(val) PyModule_AddObject(m, #val, PyString_FromString(val))
3312 ADD_LDB_STRING(LDB_SYNTAX_DN
);
3313 ADD_LDB_STRING(LDB_SYNTAX_DN
);
3314 ADD_LDB_STRING(LDB_SYNTAX_DIRECTORY_STRING
);
3315 ADD_LDB_STRING(LDB_SYNTAX_INTEGER
);
3316 ADD_LDB_STRING(LDB_SYNTAX_BOOLEAN
);
3317 ADD_LDB_STRING(LDB_SYNTAX_OCTET_STRING
);
3318 ADD_LDB_STRING(LDB_SYNTAX_UTC_TIME
);