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-2009 Jelmer Vernooij <jelmer@samba.org>
9 Copyright (C) 2009 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/>.
30 #include "ldb_private.h"
34 /* There's no Py_ssize_t in 2.4, apparently */
35 #if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5
36 typedef int Py_ssize_t
;
37 typedef inquiry lenfunc
;
38 typedef intargfunc ssizeargfunc
;
41 #ifndef Py_RETURN_NONE
42 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
45 static void PyErr_SetLdbError(PyObject
*error
, int ret
, struct ldb_context
*ldb_ctx
)
47 if (ret
== LDB_ERR_PYTHON_EXCEPTION
)
48 return; /* Python exception should already be set, just keep that */
50 PyErr_SetObject(error
,
51 Py_BuildValue(discard_const_p(char, "(i,s)"), ret
,
52 ldb_ctx
== NULL
?ldb_strerror(ret
):ldb_errstring(ldb_ctx
)));
55 static PyObject
*PyExc_LdbError
;
57 PyAPI_DATA(PyTypeObject
) PyLdbMessage
;
58 PyAPI_DATA(PyTypeObject
) PyLdbModule
;
59 PyAPI_DATA(PyTypeObject
) PyLdbDn
;
60 PyAPI_DATA(PyTypeObject
) PyLdb
;
61 PyAPI_DATA(PyTypeObject
) PyLdbMessageElement
;
62 PyAPI_DATA(PyTypeObject
) PyLdbTree
;
64 static PyObject
*PyObject_FromLdbValue(struct ldb_context
*ldb_ctx
,
65 struct ldb_message_element
*el
,
68 struct ldb_val new_val
;
69 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
74 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
82 * Obtain a ldb DN from a Python object.
84 * @param mem_ctx Memory context
85 * @param object Python object
86 * @param ldb_ctx LDB context
87 * @return Whether or not the conversion succeeded
89 bool PyObject_AsDn(TALLOC_CTX
*mem_ctx
, PyObject
*object
,
90 struct ldb_context
*ldb_ctx
, struct ldb_dn
**dn
)
94 if (ldb_ctx
!= NULL
&& PyString_Check(object
)) {
95 odn
= ldb_dn_new(mem_ctx
, ldb_ctx
, PyString_AsString(object
));
100 if (PyLdbDn_Check(object
)) {
101 *dn
= PyLdbDn_AsDn(object
);
105 PyErr_SetString(PyExc_TypeError
, "Expected DN");
110 * Create a Python object from a ldb_result.
112 * @param result LDB result to convert
113 * @return Python object with converted result (a list object)
115 static PyObject
*PyLdbResult_FromResult(struct ldb_result
*result
)
119 if (result
== NULL
) {
122 ret
= PyList_New(result
->count
);
123 for (i
= 0; i
< result
->count
; i
++) {
124 PyList_SetItem(ret
, i
, PyLdbMessage_FromMessage(result
->msgs
[i
])
131 * Create a LDB Result from a Python object.
132 * If conversion fails, NULL will be returned and a Python exception set.
134 * @param mem_ctx Memory context in which to allocate the LDB Result
135 * @param obj Python object to convert
136 * @return a ldb_result, or NULL if the conversion failed
138 static struct ldb_result
*PyLdbResult_AsResult(TALLOC_CTX
*mem_ctx
,
141 struct ldb_result
*res
;
147 res
= talloc_zero(mem_ctx
, struct ldb_result
);
148 res
->count
= PyList_Size(obj
);
149 res
->msgs
= talloc_array(res
, struct ldb_message
*, res
->count
);
150 for (i
= 0; i
< res
->count
; i
++) {
151 PyObject
*item
= PyList_GetItem(obj
, i
);
152 res
->msgs
[i
] = PyLdbMessage_AsMessage(item
);
157 static PyObject
*py_ldb_dn_validate(PyLdbDnObject
*self
)
159 return PyBool_FromLong(ldb_dn_validate(self
->dn
));
162 static PyObject
*py_ldb_dn_is_valid(PyLdbDnObject
*self
)
164 return PyBool_FromLong(ldb_dn_is_valid(self
->dn
));
167 static PyObject
*py_ldb_dn_is_special(PyLdbDnObject
*self
)
169 return PyBool_FromLong(ldb_dn_is_special(self
->dn
));
172 static PyObject
*py_ldb_dn_is_null(PyLdbDnObject
*self
)
174 return PyBool_FromLong(ldb_dn_is_null(self
->dn
));
177 static PyObject
*py_ldb_dn_get_casefold(PyLdbDnObject
*self
)
179 return PyString_FromString(ldb_dn_get_casefold(self
->dn
));
182 static PyObject
*py_ldb_dn_get_linearized(PyLdbDnObject
*self
)
184 return PyString_FromString(ldb_dn_get_linearized(self
->dn
));
187 static PyObject
*py_ldb_dn_canonical_str(PyLdbDnObject
*self
)
189 return PyString_FromString(ldb_dn_canonical_string(self
->dn
, self
->dn
));
192 static PyObject
*py_ldb_dn_canonical_ex_str(PyLdbDnObject
*self
)
194 return PyString_FromString(ldb_dn_canonical_ex_string(self
->dn
, self
->dn
));
197 static PyObject
*py_ldb_dn_repr(PyLdbDnObject
*self
)
199 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self
->dn
))));
202 static PyObject
*py_ldb_dn_check_special(PyLdbDnObject
*self
, PyObject
*args
)
206 if (!PyArg_ParseTuple(args
, "s", &name
))
209 return ldb_dn_check_special(self
->dn
, name
)?Py_True
:Py_False
;
212 static int py_ldb_dn_compare(PyLdbDnObject
*dn1
, PyLdbDnObject
*dn2
)
215 ret
= ldb_dn_compare(dn1
->dn
, dn2
->dn
);
216 if (ret
< 0) ret
= -1;
217 if (ret
> 0) ret
= 1;
221 static PyObject
*py_ldb_dn_get_parent(PyLdbDnObject
*self
)
223 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
);
224 struct ldb_dn
*parent
;
225 PyLdbDnObject
*py_ret
;
226 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
228 parent
= ldb_dn_get_parent(mem_ctx
, dn
);
229 if (parent
== NULL
) {
230 talloc_free(mem_ctx
);
234 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
235 if (py_ret
== NULL
) {
237 talloc_free(mem_ctx
);
240 py_ret
->mem_ctx
= mem_ctx
;
242 return (PyObject
*)py_ret
;
245 #define dn_ldb_ctx(dn) ((struct ldb_context *)dn)
247 static PyObject
*py_ldb_dn_add_child(PyLdbDnObject
*self
, PyObject
*args
)
250 struct ldb_dn
*dn
, *other
;
251 if (!PyArg_ParseTuple(args
, "O", &py_other
))
254 dn
= PyLdbDn_AsDn((PyObject
*)self
);
256 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
259 return ldb_dn_add_child(dn
, other
)?Py_True
:Py_False
;
262 static PyObject
*py_ldb_dn_add_base(PyLdbDnObject
*self
, PyObject
*args
)
265 struct ldb_dn
*other
, *dn
;
266 if (!PyArg_ParseTuple(args
, "O", &py_other
))
269 dn
= PyLdbDn_AsDn((PyObject
*)self
);
271 if (!PyObject_AsDn(NULL
, py_other
, dn_ldb_ctx(dn
), &other
))
274 return ldb_dn_add_base(dn
, other
)?Py_True
:Py_False
;
277 static PyMethodDef py_ldb_dn_methods
[] = {
278 { "validate", (PyCFunction
)py_ldb_dn_validate
, METH_NOARGS
,
279 "S.validate() -> bool\n"
280 "Validate DN is correct." },
281 { "is_valid", (PyCFunction
)py_ldb_dn_is_valid
, METH_NOARGS
,
282 "S.is_valid() -> bool\n" },
283 { "is_special", (PyCFunction
)py_ldb_dn_is_special
, METH_NOARGS
,
284 "S.is_special() -> bool\n"
285 "Check whether this is a special LDB DN." },
286 { "is_null", (PyCFunction
)py_ldb_dn_is_null
, METH_NOARGS
,
287 "Check whether this is a null DN." },
288 { "get_casefold", (PyCFunction
)py_ldb_dn_get_casefold
, METH_NOARGS
,
290 { "get_linearized", (PyCFunction
)py_ldb_dn_get_linearized
, METH_NOARGS
,
292 { "canonical_str", (PyCFunction
)py_ldb_dn_canonical_str
, METH_NOARGS
,
293 "S.canonical_str() -> string\n"
294 "Canonical version of this DN (like a posix path)." },
295 { "canonical_ex_str", (PyCFunction
)py_ldb_dn_canonical_ex_str
, METH_NOARGS
,
296 "S.canonical_ex_str() -> string\n"
297 "Canonical version of this DN (like a posix path, with terminating newline)." },
298 { "check_special", (PyCFunction
)py_ldb_dn_is_special
, METH_VARARGS
,
300 { "parent", (PyCFunction
)py_ldb_dn_get_parent
, METH_NOARGS
,
302 "Get the parent for this DN." },
303 { "add_child", (PyCFunction
)py_ldb_dn_add_child
, METH_VARARGS
,
304 "S.add_child(dn) -> None\n"
305 "Add a child DN to this DN." },
306 { "add_base", (PyCFunction
)py_ldb_dn_add_base
, METH_VARARGS
,
307 "S.add_base(dn) -> None\n"
308 "Add a base DN to this DN." },
309 { "check_special", (PyCFunction
)py_ldb_dn_check_special
, METH_VARARGS
,
314 static Py_ssize_t
py_ldb_dn_len(PyLdbDnObject
*self
)
316 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject
*)self
));
319 static PyObject
*py_ldb_dn_concat(PyLdbDnObject
*self
, PyObject
*py_other
)
321 struct ldb_dn
*dn
= PyLdbDn_AsDn((PyObject
*)self
),
323 PyLdbDnObject
*py_ret
;
325 if (!PyObject_AsDn(NULL
, py_other
, NULL
, &other
))
328 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
329 if (py_ret
== NULL
) {
333 py_ret
->mem_ctx
= talloc_new(NULL
);
334 py_ret
->dn
= ldb_dn_copy(py_ret
->mem_ctx
, dn
);
335 ldb_dn_add_child(py_ret
->dn
, other
);
336 return (PyObject
*)py_ret
;
339 static PySequenceMethods py_ldb_dn_seq
= {
340 .sq_length
= (lenfunc
)py_ldb_dn_len
,
341 .sq_concat
= (binaryfunc
)py_ldb_dn_concat
,
344 static PyObject
*py_ldb_dn_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
349 struct ldb_context
*ldb_ctx
;
351 PyLdbDnObject
*py_ret
;
352 const char * const kwnames
[] = { "ldb", "dn", NULL
};
354 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Os",
355 discard_const_p(char *, kwnames
),
359 ldb_ctx
= PyLdb_AsLdbContext(py_ldb
);
361 mem_ctx
= talloc_new(NULL
);
362 if (mem_ctx
== NULL
) {
367 ret
= ldb_dn_new(mem_ctx
, ldb_ctx
, str
);
369 if (ret
== NULL
|| !ldb_dn_validate(ret
)) {
370 talloc_free(mem_ctx
);
371 PyErr_SetString(PyExc_ValueError
, "unable to parse dn string");
375 py_ret
= (PyLdbDnObject
*)type
->tp_alloc(type
, 0);
377 talloc_free(mem_ctx
);
381 py_ret
->mem_ctx
= mem_ctx
;
383 return (PyObject
*)py_ret
;
386 PyObject
*PyLdbDn_FromDn(struct ldb_dn
*dn
)
388 PyLdbDnObject
*py_ret
;
394 py_ret
= (PyLdbDnObject
*)PyLdbDn
.tp_alloc(&PyLdbDn
, 0);
395 if (py_ret
== NULL
) {
399 py_ret
->mem_ctx
= talloc_new(NULL
);
400 py_ret
->dn
= talloc_reference(py_ret
->mem_ctx
, dn
);
401 return (PyObject
*)py_ret
;
404 static void py_ldb_dn_dealloc(PyLdbDnObject
*self
)
406 talloc_free(self
->mem_ctx
);
407 self
->ob_type
->tp_free(self
);
410 PyTypeObject PyLdbDn
= {
412 .tp_methods
= py_ldb_dn_methods
,
413 .tp_str
= (reprfunc
)py_ldb_dn_get_linearized
,
414 .tp_repr
= (reprfunc
)py_ldb_dn_repr
,
415 .tp_compare
= (cmpfunc
)py_ldb_dn_compare
,
416 .tp_as_sequence
= &py_ldb_dn_seq
,
417 .tp_doc
= "A LDB distinguished name.",
418 .tp_new
= py_ldb_dn_new
,
419 .tp_dealloc
= (destructor
)py_ldb_dn_dealloc
,
420 .tp_basicsize
= sizeof(PyLdbObject
),
421 .tp_flags
= Py_TPFLAGS_DEFAULT
,
425 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
) PRINTF_ATTRIBUTE(3, 0);
426 static void py_ldb_debug(void *context
, enum ldb_debug_level level
, const char *fmt
, va_list ap
)
428 PyObject
*fn
= (PyObject
*)context
;
429 PyObject_CallFunction(fn
, discard_const_p(char, "(i,O)"), level
, PyString_FromFormatV(fmt
, ap
));
432 static PyObject
*py_ldb_set_debug(PyLdbObject
*self
, PyObject
*args
)
436 if (!PyArg_ParseTuple(args
, "O", &cb
))
440 /* FIXME: Where do we DECREF cb ? */
441 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_set_debug(self
->ldb_ctx
, py_ldb_debug
, cb
), PyLdb_AsLdbContext(self
));
446 static PyObject
*py_ldb_set_create_perms(PyTypeObject
*self
, PyObject
*args
)
449 if (!PyArg_ParseTuple(args
, "I", &perms
))
452 ldb_set_create_perms(PyLdb_AsLdbContext(self
), perms
);
457 static PyObject
*py_ldb_set_modules_dir(PyTypeObject
*self
, PyObject
*args
)
460 if (!PyArg_ParseTuple(args
, "s", &modules_dir
))
463 ldb_set_modules_dir(PyLdb_AsLdbContext(self
), modules_dir
);
468 static PyObject
*py_ldb_transaction_start(PyLdbObject
*self
)
470 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_start(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
474 static PyObject
*py_ldb_transaction_commit(PyLdbObject
*self
)
476 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_commit(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
480 static PyObject
*py_ldb_transaction_cancel(PyLdbObject
*self
)
482 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_transaction_cancel(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
486 static PyObject
*py_ldb_setup_wellknown_attributes(PyLdbObject
*self
)
488 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self
)), PyLdb_AsLdbContext(self
));
492 static PyObject
*py_ldb_repr(PyLdbObject
*self
)
494 return PyString_FromFormat("<ldb connection>");
497 static PyObject
*py_ldb_get_root_basedn(PyLdbObject
*self
)
499 struct ldb_dn
*dn
= ldb_get_root_basedn(PyLdb_AsLdbContext(self
));
502 return PyLdbDn_FromDn(dn
);
506 static PyObject
*py_ldb_get_schema_basedn(PyLdbObject
*self
)
508 struct ldb_dn
*dn
= ldb_get_schema_basedn(PyLdb_AsLdbContext(self
));
511 return PyLdbDn_FromDn(dn
);
514 static PyObject
*py_ldb_get_config_basedn(PyLdbObject
*self
)
516 struct ldb_dn
*dn
= ldb_get_config_basedn(PyLdb_AsLdbContext(self
));
519 return PyLdbDn_FromDn(dn
);
522 static PyObject
*py_ldb_get_default_basedn(PyLdbObject
*self
)
524 struct ldb_dn
*dn
= ldb_get_default_basedn(PyLdb_AsLdbContext(self
));
527 return PyLdbDn_FromDn(dn
);
530 static const char **PyList_AsStringList(TALLOC_CTX
*mem_ctx
, PyObject
*list
,
531 const char *paramname
)
535 if (!PyList_Check(list
)) {
536 PyErr_Format(PyExc_TypeError
, "%s is not a list", paramname
);
539 ret
= talloc_array(NULL
, const char *, PyList_Size(list
)+1);
540 for (i
= 0; i
< PyList_Size(list
); i
++) {
541 PyObject
*item
= PyList_GetItem(list
, i
);
542 if (!PyString_Check(item
)) {
543 PyErr_Format(PyExc_TypeError
, "%s should be strings", paramname
);
546 ret
[i
] = talloc_strndup(ret
, PyString_AsString(item
),
547 PyString_Size(item
));
553 static int py_ldb_init(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
555 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
557 PyObject
*py_options
= Py_None
;
558 const char **options
;
561 struct ldb_context
*ldb
;
563 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO:Ldb.__init__",
564 discard_const_p(char *, kwnames
),
565 &url
, &flags
, &py_options
))
568 ldb
= PyLdb_AsLdbContext(self
);
570 if (py_options
== Py_None
) {
573 options
= PyList_AsStringList(ldb
, py_options
, "options");
579 ret
= ldb_connect(ldb
, url
, flags
, options
);
580 if (ret
!= LDB_SUCCESS
) {
581 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb
);
586 talloc_free(options
);
590 static PyObject
*py_ldb_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
593 struct ldb_context
*ldb
;
594 ret
= (PyLdbObject
*)type
->tp_alloc(type
, 0);
599 ret
->mem_ctx
= talloc_new(NULL
);
600 ldb
= ldb_init(ret
->mem_ctx
, NULL
);
608 return (PyObject
*)ret
;
611 static PyObject
*py_ldb_connect(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
615 PyObject
*py_options
= Py_None
;
617 const char **options
;
618 const char * const kwnames
[] = { "url", "flags", "options", NULL
};
620 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|ziO",
621 discard_const_p(char *, kwnames
),
622 &url
, &flags
, &py_options
))
625 if (py_options
== Py_None
) {
628 options
= PyList_AsStringList(NULL
, py_options
, "options");
633 ret
= ldb_connect(PyLdb_AsLdbContext(self
), url
, flags
, options
);
634 talloc_free(options
);
636 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
641 static PyObject
*py_ldb_modify(PyLdbObject
*self
, PyObject
*args
)
645 if (!PyArg_ParseTuple(args
, "O", &py_msg
))
648 if (!PyLdbMessage_Check(py_msg
)) {
649 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message");
653 ret
= ldb_modify(PyLdb_AsLdbContext(self
), PyLdbMessage_AsMessage(py_msg
));
654 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
659 static PyObject
*py_ldb_add(PyLdbObject
*self
, PyObject
*args
)
663 Py_ssize_t dict_pos
, msg_pos
;
664 struct ldb_message_element
*msgel
;
665 struct ldb_message
*msg
;
666 struct ldb_context
*ldb_ctx
;
667 struct ldb_request
*req
;
668 PyObject
*key
, *value
;
669 PyObject
*py_controls
= Py_None
;
671 struct ldb_control
**parsed_controls
;
673 if (!PyArg_ParseTuple(args
, "O|O", &py_msg
, &py_controls
))
675 ldb_ctx
= PyLdb_AsLdbContext(self
);
677 mem_ctx
= talloc_new(NULL
);
678 if (py_controls
== Py_None
) {
679 parsed_controls
= NULL
;
681 const char **controls
= PyList_AsStringList(ldb_ctx
, py_controls
, "controls");
682 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, ldb_ctx
, controls
);
683 talloc_free(controls
);
685 if (PyDict_Check(py_msg
)) {
686 PyObject
*dn_value
= PyDict_GetItemString(py_msg
, "dn");
687 msg
= ldb_msg_new(mem_ctx
);
688 msg
->elements
= talloc_zero_array(msg
, struct ldb_message_element
, PyDict_Size(py_msg
));
689 msg_pos
= dict_pos
= 0;
691 if (!PyObject_AsDn(msg
, dn_value
, ldb_ctx
, &msg
->dn
)) {
692 PyErr_SetString(PyExc_TypeError
, "unable to import dn object");
693 talloc_free(mem_ctx
);
696 if (msg
->dn
== NULL
) {
697 PyErr_SetString(PyExc_TypeError
, "dn set but not found");
698 talloc_free(mem_ctx
);
703 while (PyDict_Next(py_msg
, &dict_pos
, &key
, &value
)) {
704 char *key_str
= PyString_AsString(key
);
705 if (strcmp(key_str
, "dn") != 0) {
706 msgel
= PyObject_AsMessageElement(msg
->elements
, value
, 0, key_str
);
708 PyErr_SetString(PyExc_TypeError
, "unable to import element");
709 talloc_free(mem_ctx
);
712 memcpy(&msg
->elements
[msg_pos
], msgel
, sizeof(*msgel
));
717 if (msg
->dn
== NULL
) {
718 PyErr_SetString(PyExc_TypeError
, "no dn set");
719 talloc_free(mem_ctx
);
723 msg
->num_elements
= msg_pos
;
725 msg
= PyLdbMessage_AsMessage(py_msg
);
728 ret
= ldb_msg_sanity_check(ldb_ctx
, msg
);
729 if (ret
!= LDB_SUCCESS
) {
730 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
731 talloc_free(mem_ctx
);
735 ret
= ldb_build_add_req(&req
, ldb_ctx
, ldb_ctx
,
739 ldb_op_default_callback
,
742 if (ret
!= LDB_SUCCESS
) {
743 PyErr_SetString(PyExc_TypeError
, "failed to build request");
744 talloc_free(mem_ctx
);
748 /* do request and autostart a transaction */
749 /* Then let's LDB handle the message error in case of pb as they are meaningful */
751 ret
= ldb_transaction_start(ldb_ctx
);
752 if (ret
!= LDB_SUCCESS
) {
754 talloc_free(mem_ctx
);
755 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
758 ret
= ldb_request(ldb_ctx
, req
);
759 if (ret
== LDB_SUCCESS
) {
760 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
763 if (ret
== LDB_SUCCESS
) {
764 ret
= ldb_transaction_commit(ldb_ctx
);
766 ldb_transaction_cancel(ldb_ctx
);
767 if (ldb_ctx
->err_string
== NULL
) {
768 /* no error string was setup by the backend */
769 ldb_asprintf_errstring(ldb_ctx
, "%s (%d)", ldb_strerror(ret
), ret
);
773 talloc_free(mem_ctx
);
774 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
779 static PyObject
*py_ldb_delete(PyLdbObject
*self
, PyObject
*args
)
784 struct ldb_context
*ldb
;
785 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
788 ldb
= PyLdb_AsLdbContext(self
);
790 if (!PyObject_AsDn(NULL
, py_dn
, ldb
, &dn
))
793 ret
= ldb_delete(ldb
, dn
);
794 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
799 static PyObject
*py_ldb_rename(PyLdbObject
*self
, PyObject
*args
)
801 PyObject
*py_dn1
, *py_dn2
;
802 struct ldb_dn
*dn1
, *dn2
;
804 struct ldb_context
*ldb
;
806 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
809 mem_ctx
= talloc_new(NULL
);
810 if (mem_ctx
== NULL
) {
814 ldb
= PyLdb_AsLdbContext(self
);
815 if (!PyObject_AsDn(mem_ctx
, py_dn1
, ldb
, &dn1
)) {
816 talloc_free(mem_ctx
);
820 if (!PyObject_AsDn(mem_ctx
, py_dn2
, ldb
, &dn2
)) {
821 talloc_free(mem_ctx
);
825 ret
= ldb_rename(ldb
, dn1
, dn2
);
826 talloc_free(mem_ctx
);
827 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb
);
832 static PyObject
*py_ldb_schema_attribute_remove(PyLdbObject
*self
, PyObject
*args
)
835 if (!PyArg_ParseTuple(args
, "s", &name
))
838 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self
), name
);
843 static PyObject
*py_ldb_schema_attribute_add(PyLdbObject
*self
, PyObject
*args
)
845 char *attribute
, *syntax
;
848 if (!PyArg_ParseTuple(args
, "sIs", &attribute
, &flags
, &syntax
))
851 ret
= ldb_schema_attribute_add(PyLdb_AsLdbContext(self
), attribute
, flags
, syntax
);
853 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, PyLdb_AsLdbContext(self
));
858 static PyObject
*ldb_ldif_to_pyobject(struct ldb_ldif
*ldif
)
863 /* We don't want this attached to the 'ldb' any more */
864 return Py_BuildValue(discard_const_p(char, "(iO)"),
866 PyLdbMessage_FromMessage(ldif
->msg
));
871 static PyObject
*py_ldb_write_ldif(PyLdbMessageObject
*self
, PyObject
*args
)
875 struct ldb_ldif ldif
;
880 if (!PyArg_ParseTuple(args
, "Oi", &py_msg
, &changetype
))
883 if (!PyLdbMessage_Check(py_msg
)) {
884 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for msg");
888 ldif
.msg
= PyLdbMessage_AsMessage(py_msg
);
889 ldif
.changetype
= changetype
;
891 mem_ctx
= talloc_new(NULL
);
893 string
= ldb_ldif_write_string(PyLdb_AsLdbContext(self
), mem_ctx
, &ldif
);
895 PyErr_SetString(PyExc_KeyError
, "Failed to generate LDIF");
899 ret
= PyString_FromString(string
);
901 talloc_free(mem_ctx
);
906 static PyObject
*py_ldb_parse_ldif(PyLdbObject
*self
, PyObject
*args
)
909 struct ldb_ldif
*ldif
;
914 if (!PyArg_ParseTuple(args
, "s", &s
))
917 mem_ctx
= talloc_new(NULL
);
922 list
= PyList_New(0);
923 while (s
&& *s
!= '\0') {
924 ldif
= ldb_ldif_read_string(self
->ldb_ctx
, &s
);
925 talloc_steal(mem_ctx
, ldif
);
927 PyList_Append(list
, ldb_ldif_to_pyobject(ldif
));
929 PyErr_SetString(PyExc_ValueError
, "unable to parse ldif string");
930 talloc_free(mem_ctx
);
934 talloc_free(mem_ctx
); /* The pyobject already has a reference to the things it needs */
935 return PyObject_GetIter(list
);
938 static PyObject
*py_ldb_msg_diff(PyLdbObject
*self
, PyObject
*args
)
940 PyObject
*py_msg_old
;
941 PyObject
*py_msg_new
;
942 struct ldb_message
*diff
;
945 if (!PyArg_ParseTuple(args
, "OO", &py_msg_old
, &py_msg_new
))
948 if (!PyLdbMessage_Check(py_msg_old
)) {
949 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for old message");
953 if (!PyLdbMessage_Check(py_msg_new
)) {
954 PyErr_SetString(PyExc_TypeError
, "Expected Ldb Message for new message");
958 diff
= ldb_msg_diff(PyLdb_AsLdbContext(self
), PyLdbMessage_AsMessage(py_msg_old
), PyLdbMessage_AsMessage(py_msg_new
));
960 PyErr_SetString(PyExc_RuntimeError
, "Failed to generate the Ldb Message diff");
964 py_ret
= PyLdbMessage_FromMessage(diff
);
969 static PyObject
*py_ldb_schema_format_value(PyLdbObject
*self
, PyObject
*args
)
971 const struct ldb_schema_attribute
*a
;
972 struct ldb_val old_val
;
973 struct ldb_val new_val
;
979 if (!PyArg_ParseTuple(args
, "sO", &element_name
, &val
))
982 mem_ctx
= talloc_new(NULL
);
984 old_val
.data
= (uint8_t *)PyString_AsString(val
);
985 old_val
.length
= PyString_Size(val
);
987 a
= ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self
), element_name
);
993 if (a
->syntax
->ldif_write_fn(PyLdb_AsLdbContext(self
), mem_ctx
, &old_val
, &new_val
) != 0) {
994 talloc_free(mem_ctx
);
998 ret
= PyString_FromStringAndSize((const char *)new_val
.data
, new_val
.length
);
1000 talloc_free(mem_ctx
);
1005 static PyObject
*py_ldb_search(PyLdbObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1007 PyObject
*py_base
= Py_None
;
1008 enum ldb_scope scope
= LDB_SCOPE_DEFAULT
;
1010 PyObject
*py_attrs
= Py_None
;
1011 PyObject
*py_controls
= Py_None
;
1012 const char * const kwnames
[] = { "base", "scope", "expression", "attrs", "controls", NULL
};
1014 struct ldb_result
*res
;
1015 struct ldb_request
*req
;
1017 struct ldb_context
*ldb_ctx
;
1018 struct ldb_control
**parsed_controls
;
1019 struct ldb_dn
*base
;
1022 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OizOO",
1023 discard_const_p(char *, kwnames
),
1024 &py_base
, &scope
, &expr
, &py_attrs
, &py_controls
))
1027 ldb_ctx
= PyLdb_AsLdbContext(self
);
1029 if (py_attrs
== Py_None
) {
1032 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1037 if (py_base
== Py_None
) {
1038 base
= ldb_get_default_basedn(ldb_ctx
);
1040 if (!PyObject_AsDn(ldb_ctx
, py_base
, ldb_ctx
, &base
)) {
1046 if (py_controls
== Py_None
) {
1047 parsed_controls
= NULL
;
1049 const char **controls
= PyList_AsStringList(ldb_ctx
, py_controls
, "controls");
1050 parsed_controls
= ldb_parse_control_strings(ldb_ctx
, ldb_ctx
, controls
);
1051 talloc_free(controls
);
1054 res
= talloc_zero(ldb_ctx
, struct ldb_result
);
1061 ret
= ldb_build_search_req(&req
, ldb_ctx
, ldb_ctx
,
1068 ldb_search_default_callback
,
1071 talloc_steal(req
, attrs
);
1073 if (ret
!= LDB_SUCCESS
) {
1075 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1079 ret
= ldb_request(ldb_ctx
, req
);
1081 if (ret
== LDB_SUCCESS
) {
1082 ret
= ldb_wait(req
->handle
, LDB_WAIT_ALL
);
1087 if (ret
!= LDB_SUCCESS
) {
1089 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, ldb_ctx
);
1093 py_ret
= PyLdbResult_FromResult(res
);
1100 static PyObject
*py_ldb_get_opaque(PyLdbObject
*self
, PyObject
*args
)
1105 if (!PyArg_ParseTuple(args
, "s", &name
))
1108 data
= ldb_get_opaque(PyLdb_AsLdbContext(self
), name
);
1113 /* FIXME: More interpretation */
1118 static PyObject
*py_ldb_set_opaque(PyLdbObject
*self
, PyObject
*args
)
1123 if (!PyArg_ParseTuple(args
, "sO", &name
, &data
))
1126 /* FIXME: More interpretation */
1128 ldb_set_opaque(PyLdb_AsLdbContext(self
), name
, data
);
1133 static PyObject
*py_ldb_modules(PyLdbObject
*self
)
1135 struct ldb_context
*ldb
= PyLdb_AsLdbContext(self
);
1136 PyObject
*ret
= PyList_New(0);
1137 struct ldb_module
*mod
;
1139 for (mod
= ldb
->modules
; mod
; mod
= mod
->next
) {
1140 PyList_Append(ret
, PyLdbModule_FromModule(mod
));
1146 static PyMethodDef py_ldb_methods
[] = {
1147 { "set_debug", (PyCFunction
)py_ldb_set_debug
, METH_VARARGS
,
1148 "S.set_debug(callback) -> None\n"
1149 "Set callback for LDB debug messages.\n"
1150 "The callback should accept a debug level and debug text." },
1151 { "set_create_perms", (PyCFunction
)py_ldb_set_create_perms
, METH_VARARGS
,
1152 "S.set_create_perms(mode) -> None\n"
1153 "Set mode to use when creating new LDB files." },
1154 { "set_modules_dir", (PyCFunction
)py_ldb_set_modules_dir
, METH_VARARGS
,
1155 "S.set_modules_dir(path) -> None\n"
1156 "Set path LDB should search for modules" },
1157 { "transaction_start", (PyCFunction
)py_ldb_transaction_start
, METH_NOARGS
,
1158 "S.transaction_start() -> None\n"
1159 "Start a new transaction." },
1160 { "transaction_commit", (PyCFunction
)py_ldb_transaction_commit
, METH_NOARGS
,
1161 "S.transaction_commit() -> None\n"
1162 "commit a new transaction." },
1163 { "transaction_cancel", (PyCFunction
)py_ldb_transaction_cancel
, METH_NOARGS
,
1164 "S.transaction_cancel() -> None\n"
1165 "cancel a new transaction." },
1166 { "setup_wellknown_attributes", (PyCFunction
)py_ldb_setup_wellknown_attributes
, METH_NOARGS
,
1168 { "get_root_basedn", (PyCFunction
)py_ldb_get_root_basedn
, METH_NOARGS
,
1170 { "get_schema_basedn", (PyCFunction
)py_ldb_get_schema_basedn
, METH_NOARGS
,
1172 { "get_default_basedn", (PyCFunction
)py_ldb_get_default_basedn
, METH_NOARGS
,
1174 { "get_config_basedn", (PyCFunction
)py_ldb_get_config_basedn
, METH_NOARGS
,
1176 { "connect", (PyCFunction
)py_ldb_connect
, METH_VARARGS
|METH_KEYWORDS
,
1177 "S.connect(url, flags=0, options=None) -> None\n"
1178 "Connect to a LDB URL." },
1179 { "modify", (PyCFunction
)py_ldb_modify
, METH_VARARGS
,
1180 "S.modify(message) -> None\n"
1181 "Modify an entry." },
1182 { "add", (PyCFunction
)py_ldb_add
, METH_VARARGS
,
1183 "S.add(message) -> None\n"
1185 { "delete", (PyCFunction
)py_ldb_delete
, METH_VARARGS
,
1186 "S.delete(dn) -> None\n"
1187 "Remove an entry." },
1188 { "rename", (PyCFunction
)py_ldb_rename
, METH_VARARGS
,
1189 "S.rename(old_dn, new_dn) -> None\n"
1190 "Rename an entry." },
1191 { "search", (PyCFunction
)py_ldb_search
, METH_VARARGS
|METH_KEYWORDS
,
1192 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n"
1193 "Search in a database.\n"
1195 ":param base: Optional base DN to search\n"
1196 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n"
1197 ":param expression: Optional search expression\n"
1198 ":param attrs: Attributes to return (defaults to all)\n"
1199 ":param controls: Optional list of controls\n"
1200 ":return: Iterator over Message objects\n"
1202 { "schema_attribute_remove", (PyCFunction
)py_ldb_schema_attribute_remove
, METH_VARARGS
,
1204 { "schema_attribute_add", (PyCFunction
)py_ldb_schema_attribute_add
, METH_VARARGS
,
1206 { "schema_format_value", (PyCFunction
)py_ldb_schema_format_value
, METH_VARARGS
,
1208 { "parse_ldif", (PyCFunction
)py_ldb_parse_ldif
, METH_VARARGS
,
1209 "S.parse_ldif(ldif) -> iter(messages)\n"
1210 "Parse a string formatted using LDIF." },
1211 { "write_ldif", (PyCFunction
)py_ldb_write_ldif
, METH_VARARGS
,
1212 "S.write_ldif(message, changetype) -> ldif\n"
1213 "Print the message as a string formatted using LDIF." },
1214 { "msg_diff", (PyCFunction
)py_ldb_msg_diff
, METH_VARARGS
,
1215 "S.msg_diff(Message) -> Message\n"
1216 "Return an LDB Message of the difference between two Message objects." },
1217 { "get_opaque", (PyCFunction
)py_ldb_get_opaque
, METH_VARARGS
,
1218 "S.get_opaque(name) -> value\n"
1219 "Get an opaque value set on this LDB connection. \n"
1220 ":note: The returned value may not be useful in Python."
1222 { "set_opaque", (PyCFunction
)py_ldb_set_opaque
, METH_VARARGS
,
1223 "S.set_opaque(name, value) -> None\n"
1224 "Set an opaque value on this LDB connection. \n"
1225 ":note: Passing incorrect values may cause crashes." },
1226 { "modules", (PyCFunction
)py_ldb_modules
, METH_NOARGS
,
1227 "S.modules() -> list\n"
1228 "Return the list of modules on this LDB connection " },
1232 PyObject
*PyLdbModule_FromModule(struct ldb_module
*mod
)
1234 PyLdbModuleObject
*ret
;
1236 ret
= (PyLdbModuleObject
*)PyLdbModule
.tp_alloc(&PyLdbModule
, 0);
1241 ret
->mem_ctx
= talloc_new(NULL
);
1242 ret
->mod
= talloc_reference(ret
->mem_ctx
, mod
);
1243 return (PyObject
*)ret
;
1246 static PyObject
*py_ldb_get_firstmodule(PyLdbObject
*self
, void *closure
)
1248 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self
)->modules
);
1251 static PyGetSetDef py_ldb_getset
[] = {
1252 { discard_const_p(char, "firstmodule"), (getter
)py_ldb_get_firstmodule
, NULL
, NULL
},
1256 static int py_ldb_contains(PyLdbObject
*self
, PyObject
*obj
)
1258 struct ldb_context
*ldb_ctx
= PyLdb_AsLdbContext(self
);
1260 struct ldb_result
*result
;
1264 if (!PyObject_AsDn(ldb_ctx
, obj
, ldb_ctx
, &dn
))
1267 ret
= ldb_search(ldb_ctx
, ldb_ctx
, &result
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
1268 if (ret
!= LDB_SUCCESS
) {
1269 PyErr_SetLdbError(PyExc_LdbError
, ret
, ldb_ctx
);
1273 count
= result
->count
;
1275 talloc_free(result
);
1280 static PySequenceMethods py_ldb_seq
= {
1281 .sq_contains
= (objobjproc
)py_ldb_contains
,
1284 PyObject
*PyLdb_FromLdbContext(struct ldb_context
*ldb_ctx
)
1288 ret
= (PyLdbObject
*)PyLdb
.tp_alloc(&PyLdb
, 0);
1293 ret
->mem_ctx
= talloc_new(NULL
);
1294 ret
->ldb_ctx
= talloc_reference(ret
->mem_ctx
, ldb_ctx
);
1295 return (PyObject
*)ret
;
1298 static void py_ldb_dealloc(PyLdbObject
*self
)
1300 talloc_free(self
->mem_ctx
);
1301 self
->ob_type
->tp_free(self
);
1304 PyTypeObject PyLdb
= {
1306 .tp_methods
= py_ldb_methods
,
1307 .tp_repr
= (reprfunc
)py_ldb_repr
,
1308 .tp_new
= py_ldb_new
,
1309 .tp_init
= (initproc
)py_ldb_init
,
1310 .tp_dealloc
= (destructor
)py_ldb_dealloc
,
1311 .tp_getset
= py_ldb_getset
,
1312 .tp_getattro
= PyObject_GenericGetAttr
,
1313 .tp_basicsize
= sizeof(PyLdbObject
),
1314 .tp_doc
= "Connection to a LDB database.",
1315 .tp_as_sequence
= &py_ldb_seq
,
1316 .tp_flags
= Py_TPFLAGS_DEFAULT
|Py_TPFLAGS_BASETYPE
,
1319 static PyObject
*py_ldb_module_repr(PyLdbModuleObject
*self
)
1321 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self
)->ops
->name
);
1324 static PyObject
*py_ldb_module_str(PyLdbModuleObject
*self
)
1326 return PyString_FromString(PyLdbModule_AsModule(self
)->ops
->name
);
1329 static PyObject
*py_ldb_module_start_transaction(PyLdbModuleObject
*self
)
1331 PyLdbModule_AsModule(self
)->ops
->start_transaction(PyLdbModule_AsModule(self
));
1335 static PyObject
*py_ldb_module_end_transaction(PyLdbModuleObject
*self
)
1337 PyLdbModule_AsModule(self
)->ops
->end_transaction(PyLdbModule_AsModule(self
));
1341 static PyObject
*py_ldb_module_del_transaction(PyLdbModuleObject
*self
)
1343 PyLdbModule_AsModule(self
)->ops
->del_transaction(PyLdbModule_AsModule(self
));
1347 static PyObject
*py_ldb_module_search(PyLdbModuleObject
*self
, PyObject
*args
, PyObject
*kwargs
)
1349 PyObject
*py_base
, *py_tree
, *py_attrs
, *py_ret
;
1351 struct ldb_request
*req
;
1352 const char * const kwnames
[] = { "base", "scope", "tree", "attrs", NULL
};
1353 struct ldb_module
*mod
;
1354 const char * const*attrs
;
1356 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "OiOO",
1357 discard_const_p(char *, kwnames
),
1358 &py_base
, &scope
, &py_tree
, &py_attrs
))
1363 if (py_attrs
== Py_None
) {
1366 attrs
= PyList_AsStringList(NULL
, py_attrs
, "attrs");
1371 ret
= ldb_build_search_req(&req
, mod
->ldb
, NULL
, PyLdbDn_AsDn(py_base
),
1372 scope
, NULL
/* expr */, attrs
,
1373 NULL
/* controls */, NULL
, NULL
, NULL
);
1375 talloc_steal(req
, attrs
);
1377 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1379 req
->op
.search
.res
= NULL
;
1381 ret
= mod
->ops
->search(mod
, req
);
1383 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1385 py_ret
= PyLdbResult_FromResult(req
->op
.search
.res
);
1393 static PyObject
*py_ldb_module_add(PyLdbModuleObject
*self
, PyObject
*args
)
1395 struct ldb_request
*req
;
1396 PyObject
*py_message
;
1398 struct ldb_module
*mod
;
1400 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1403 req
= talloc_zero(NULL
, struct ldb_request
);
1404 req
->operation
= LDB_ADD
;
1405 req
->op
.add
.message
= PyLdbMessage_AsMessage(py_message
);
1407 mod
= PyLdbModule_AsModule(self
);
1408 ret
= mod
->ops
->add(mod
, req
);
1410 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1415 static PyObject
*py_ldb_module_modify(PyLdbModuleObject
*self
, PyObject
*args
)
1418 struct ldb_request
*req
;
1419 PyObject
*py_message
;
1420 struct ldb_module
*mod
;
1422 if (!PyArg_ParseTuple(args
, "O", &py_message
))
1425 req
= talloc_zero(NULL
, struct ldb_request
);
1426 req
->operation
= LDB_MODIFY
;
1427 req
->op
.mod
.message
= PyLdbMessage_AsMessage(py_message
);
1429 mod
= PyLdbModule_AsModule(self
);
1430 ret
= mod
->ops
->modify(mod
, req
);
1432 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, mod
->ldb
);
1437 static PyObject
*py_ldb_module_delete(PyLdbModuleObject
*self
, PyObject
*args
)
1440 struct ldb_request
*req
;
1443 if (!PyArg_ParseTuple(args
, "O", &py_dn
))
1446 req
= talloc_zero(NULL
, struct ldb_request
);
1447 req
->operation
= LDB_DELETE
;
1448 req
->op
.del
.dn
= PyLdbDn_AsDn(py_dn
);
1450 ret
= PyLdbModule_AsModule(self
)->ops
->del(PyLdbModule_AsModule(self
), req
);
1452 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1457 static PyObject
*py_ldb_module_rename(PyLdbModuleObject
*self
, PyObject
*args
)
1460 struct ldb_request
*req
;
1461 PyObject
*py_dn1
, *py_dn2
;
1463 if (!PyArg_ParseTuple(args
, "OO", &py_dn1
, &py_dn2
))
1466 req
= talloc_zero(NULL
, struct ldb_request
);
1468 req
->operation
= LDB_RENAME
;
1469 req
->op
.rename
.olddn
= PyLdbDn_AsDn(py_dn1
);
1470 req
->op
.rename
.newdn
= PyLdbDn_AsDn(py_dn2
);
1472 ret
= PyLdbModule_AsModule(self
)->ops
->rename(PyLdbModule_AsModule(self
), req
);
1474 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
1479 static PyMethodDef py_ldb_module_methods
[] = {
1480 { "search", (PyCFunction
)py_ldb_module_search
, METH_VARARGS
|METH_KEYWORDS
, NULL
},
1481 { "add", (PyCFunction
)py_ldb_module_add
, METH_VARARGS
, NULL
},
1482 { "modify", (PyCFunction
)py_ldb_module_modify
, METH_VARARGS
, NULL
},
1483 { "rename", (PyCFunction
)py_ldb_module_rename
, METH_VARARGS
, NULL
},
1484 { "delete", (PyCFunction
)py_ldb_module_delete
, METH_VARARGS
, NULL
},
1485 { "start_transaction", (PyCFunction
)py_ldb_module_start_transaction
, METH_NOARGS
, NULL
},
1486 { "end_transaction", (PyCFunction
)py_ldb_module_end_transaction
, METH_NOARGS
, NULL
},
1487 { "del_transaction", (PyCFunction
)py_ldb_module_del_transaction
, METH_NOARGS
, NULL
},
1491 static void py_ldb_module_dealloc(PyLdbModuleObject
*self
)
1493 talloc_free(self
->mem_ctx
);
1494 self
->ob_type
->tp_free(self
);
1497 PyTypeObject PyLdbModule
= {
1498 .tp_name
= "LdbModule",
1499 .tp_methods
= py_ldb_module_methods
,
1500 .tp_repr
= (reprfunc
)py_ldb_module_repr
,
1501 .tp_str
= (reprfunc
)py_ldb_module_str
,
1502 .tp_basicsize
= sizeof(PyLdbModuleObject
),
1503 .tp_dealloc
= (destructor
)py_ldb_module_dealloc
,
1504 .tp_flags
= Py_TPFLAGS_DEFAULT
,
1509 * Create a ldb_message_element from a Python object.
1511 * This will accept any sequence objects that contains strings, or
1514 * A reference to set_obj will be borrowed.
1516 * @param mem_ctx Memory context
1517 * @param set_obj Python object to convert
1518 * @param flags ldb_message_element flags to set
1519 * @param attr_name Name of the attribute
1520 * @return New ldb_message_element, allocated as child of mem_ctx
1522 struct ldb_message_element
*PyObject_AsMessageElement(TALLOC_CTX
*mem_ctx
,
1523 PyObject
*set_obj
, int flags
,
1524 const char *attr_name
)
1526 struct ldb_message_element
*me
;
1528 if (PyLdbMessageElement_Check(set_obj
))
1529 return talloc_reference(mem_ctx
,
1530 PyLdbMessageElement_AsMessageElement(set_obj
));
1532 me
= talloc(mem_ctx
, struct ldb_message_element
);
1534 me
->name
= talloc_strdup(me
, attr_name
);
1536 if (PyString_Check(set_obj
)) {
1538 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1539 me
->values
[0].length
= PyString_Size(set_obj
);
1540 me
->values
[0].data
= talloc_memdup(me
,
1541 (uint8_t *)PyString_AsString(set_obj
), me
->values
[0].length
);
1542 } else if (PySequence_Check(set_obj
)) {
1544 me
->num_values
= PySequence_Size(set_obj
);
1545 me
->values
= talloc_array(me
, struct ldb_val
, me
->num_values
);
1546 for (i
= 0; i
< me
->num_values
; i
++) {
1547 PyObject
*obj
= PySequence_GetItem(set_obj
, i
);
1549 me
->values
[i
].length
= PyString_Size(obj
);
1550 me
->values
[i
].data
= talloc_memdup(me
,
1551 (uint8_t *)PyString_AsString(obj
), me
->values
[i
].length
);
1562 static PyObject
*ldb_msg_element_to_set(struct ldb_context
*ldb_ctx
,
1563 struct ldb_message_element
*me
)
1568 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */
1569 result
= PyList_New(me
->num_values
);
1571 for (i
= 0; i
< me
->num_values
; i
++) {
1572 PyList_SetItem(result
, i
,
1573 PyObject_FromLdbValue(ldb_ctx
, me
, &me
->values
[i
]));
1579 static PyObject
*py_ldb_msg_element_get(PyLdbMessageElementObject
*self
, PyObject
*args
)
1582 if (!PyArg_ParseTuple(args
, "i", &i
))
1584 if (i
< 0 || i
>= PyLdbMessageElement_AsMessageElement(self
)->num_values
)
1587 return PyObject_FromLdbValue(NULL
, PyLdbMessageElement_AsMessageElement(self
),
1588 &(PyLdbMessageElement_AsMessageElement(self
)->values
[i
]));
1591 static PyObject
*py_ldb_msg_element_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1593 struct ldb_message_element
*el
;
1595 el
= PyLdbMessageElement_AsMessageElement(self
);
1596 return PyInt_FromLong(el
->flags
);
1599 static PyObject
*py_ldb_msg_element_set_flags(PyLdbMessageElementObject
*self
, PyObject
*args
)
1602 struct ldb_message_element
*el
;
1603 if (!PyArg_ParseTuple(args
, "i", &flags
))
1606 el
= PyLdbMessageElement_AsMessageElement(self
);
1611 static PyMethodDef py_ldb_msg_element_methods
[] = {
1612 { "get", (PyCFunction
)py_ldb_msg_element_get
, METH_VARARGS
, NULL
},
1613 { "set_flags", (PyCFunction
)py_ldb_msg_element_set_flags
, METH_VARARGS
, NULL
},
1614 { "flags", (PyCFunction
)py_ldb_msg_element_flags
, METH_NOARGS
, NULL
},
1618 static Py_ssize_t
py_ldb_msg_element_len(PyLdbMessageElementObject
*self
)
1620 return PyLdbMessageElement_AsMessageElement(self
)->num_values
;
1623 static PyObject
*py_ldb_msg_element_find(PyLdbMessageElementObject
*self
, Py_ssize_t idx
)
1625 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1626 if (idx
< 0 || idx
>= el
->num_values
) {
1627 PyErr_SetString(PyExc_IndexError
, "Out of range");
1630 return PyString_FromStringAndSize((char *)el
->values
[idx
].data
, el
->values
[idx
].length
);
1633 static PySequenceMethods py_ldb_msg_element_seq
= {
1634 .sq_length
= (lenfunc
)py_ldb_msg_element_len
,
1635 .sq_item
= (ssizeargfunc
)py_ldb_msg_element_find
,
1638 static int py_ldb_msg_element_cmp(PyLdbMessageElementObject
*self
, PyLdbMessageElementObject
*other
)
1640 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self
),
1641 PyLdbMessageElement_AsMessageElement(other
));
1644 static PyObject
*py_ldb_msg_element_iter(PyLdbMessageElementObject
*self
)
1646 return PyObject_GetIter(ldb_msg_element_to_set(NULL
, PyLdbMessageElement_AsMessageElement(self
)));
1649 PyObject
*PyLdbMessageElement_FromMessageElement(struct ldb_message_element
*el
, TALLOC_CTX
*mem_ctx
)
1651 PyLdbMessageElementObject
*ret
;
1652 ret
= (PyLdbMessageElementObject
*)PyLdbMessageElement
.tp_alloc(&PyLdbMessageElement
, 0);
1657 ret
->mem_ctx
= talloc_new(NULL
);
1658 if (talloc_reference(ret
->mem_ctx
, mem_ctx
) == NULL
) {
1663 return (PyObject
*)ret
;
1666 static PyObject
*py_ldb_msg_element_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1668 PyObject
*py_elements
= NULL
;
1669 struct ldb_message_element
*el
;
1672 const char * const kwnames
[] = { "elements", "flags", "name", NULL
};
1673 PyLdbMessageElementObject
*ret
;
1674 TALLOC_CTX
*mem_ctx
;
1676 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|Ois",
1677 discard_const_p(char *, kwnames
),
1678 &py_elements
, &flags
, &name
))
1681 mem_ctx
= talloc_new(NULL
);
1682 if (mem_ctx
== NULL
) {
1687 el
= talloc_zero(mem_ctx
, struct ldb_message_element
);
1689 if (py_elements
!= NULL
) {
1691 if (PyString_Check(py_elements
)) {
1693 el
->values
= talloc_array(el
, struct ldb_val
, 1);
1694 el
->values
[0].length
= PyString_Size(py_elements
);
1695 el
->values
[0].data
= talloc_memdup(el
,
1696 (uint8_t *)PyString_AsString(py_elements
), el
->values
[0].length
);
1697 } else if (PySequence_Check(py_elements
)) {
1698 el
->num_values
= PySequence_Size(py_elements
);
1699 el
->values
= talloc_array(el
, struct ldb_val
, el
->num_values
);
1700 for (i
= 0; i
< el
->num_values
; i
++) {
1701 PyObject
*item
= PySequence_GetItem(py_elements
, i
);
1702 if (!PyString_Check(item
)) {
1703 PyErr_Format(PyExc_TypeError
,
1704 "Expected string as element %d in list",
1706 talloc_free(mem_ctx
);
1709 el
->values
[i
].length
= PyString_Size(item
);
1710 el
->values
[i
].data
= talloc_memdup(el
,
1711 (uint8_t *)PyString_AsString(item
), el
->values
[i
].length
);
1714 PyErr_SetString(PyExc_TypeError
,
1715 "Expected string or list");
1716 talloc_free(mem_ctx
);
1722 el
->name
= talloc_strdup(el
, name
);
1724 ret
= (PyLdbMessageElementObject
*)PyLdbMessageElement
.tp_alloc(&PyLdbMessageElement
, 0);
1727 talloc_free(mem_ctx
);
1731 ret
->mem_ctx
= mem_ctx
;
1733 return (PyObject
*)ret
;
1736 static PyObject
*py_ldb_msg_element_repr(PyLdbMessageElementObject
*self
)
1738 char *element_str
= NULL
;
1740 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1743 for (i
= 0; i
< el
->num_values
; i
++) {
1744 PyObject
*o
= py_ldb_msg_element_find(self
, i
);
1745 if (element_str
== NULL
)
1746 element_str
= talloc_strdup(NULL
, PyObject_REPR(o
));
1748 element_str
= talloc_asprintf_append(element_str
, ",%s", PyObject_REPR(o
));
1751 ret
= PyString_FromFormat("MessageElement([%s])", element_str
);
1753 talloc_free(element_str
);
1758 static PyObject
*py_ldb_msg_element_str(PyLdbMessageElementObject
*self
)
1760 struct ldb_message_element
*el
= PyLdbMessageElement_AsMessageElement(self
);
1762 if (el
->num_values
== 1)
1763 return PyString_FromStringAndSize((char *)el
->values
[0].data
, el
->values
[0].length
);
1768 static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject
*self
)
1770 talloc_free(self
->mem_ctx
);
1771 self
->ob_type
->tp_free(self
);
1774 PyTypeObject PyLdbMessageElement
= {
1775 .tp_name
= "MessageElement",
1776 .tp_basicsize
= sizeof(PyLdbMessageElementObject
),
1777 .tp_dealloc
= (destructor
)py_ldb_msg_element_dealloc
,
1778 .tp_repr
= (reprfunc
)py_ldb_msg_element_repr
,
1779 .tp_str
= (reprfunc
)py_ldb_msg_element_str
,
1780 .tp_methods
= py_ldb_msg_element_methods
,
1781 .tp_compare
= (cmpfunc
)py_ldb_msg_element_cmp
,
1782 .tp_iter
= (getiterfunc
)py_ldb_msg_element_iter
,
1783 .tp_as_sequence
= &py_ldb_msg_element_seq
,
1784 .tp_new
= py_ldb_msg_element_new
,
1785 .tp_flags
= Py_TPFLAGS_DEFAULT
,
1788 static PyObject
*py_ldb_msg_remove_attr(PyLdbMessageObject
*self
, PyObject
*args
)
1791 if (!PyArg_ParseTuple(args
, "s", &name
))
1794 ldb_msg_remove_attr(self
->msg
, name
);
1799 static PyObject
*py_ldb_msg_keys(PyLdbMessageObject
*self
)
1801 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
1803 PyObject
*obj
= PyList_New(msg
->num_elements
+(msg
->dn
!= NULL
?1:0));
1804 if (msg
->dn
!= NULL
) {
1805 PyList_SetItem(obj
, j
, PyString_FromString("dn"));
1808 for (i
= 0; i
< msg
->num_elements
; i
++) {
1809 PyList_SetItem(obj
, j
, PyString_FromString(msg
->elements
[i
].name
));
1815 static PyObject
*py_ldb_msg_getitem_helper(PyLdbMessageObject
*self
, PyObject
*py_name
)
1817 struct ldb_message_element
*el
;
1819 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
1820 if (!PyString_Check(py_name
)) {
1821 PyErr_SetNone(PyExc_TypeError
);
1824 name
= PyString_AsString(py_name
);
1825 if (!strcmp(name
, "dn"))
1826 return PyLdbDn_FromDn(msg
->dn
);
1827 el
= ldb_msg_find_element(msg
, name
);
1831 return (PyObject
*)PyLdbMessageElement_FromMessageElement(el
, msg
);
1834 static PyObject
*py_ldb_msg_getitem(PyLdbMessageObject
*self
, PyObject
*py_name
)
1836 PyObject
*ret
= py_ldb_msg_getitem_helper(self
, py_name
);
1838 PyErr_SetString(PyExc_KeyError
, "No such element");
1844 static PyObject
*py_ldb_msg_get(PyLdbMessageObject
*self
, PyObject
*args
)
1846 PyObject
*name
, *ret
;
1847 if (!PyArg_ParseTuple(args
, "O", &name
))
1850 ret
= py_ldb_msg_getitem_helper(self
, name
);
1852 if (PyErr_Occurred())
1859 static PyObject
*py_ldb_msg_items(PyLdbMessageObject
*self
)
1861 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
1863 PyObject
*l
= PyList_New(msg
->num_elements
+ (msg
->dn
== NULL
?0:1));
1865 if (msg
->dn
!= NULL
) {
1866 PyList_SetItem(l
, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg
->dn
)));
1869 for (i
= 0; i
< msg
->num_elements
; i
++, j
++) {
1870 PyList_SetItem(l
, j
, Py_BuildValue("(sO)", msg
->elements
[i
].name
, PyLdbMessageElement_FromMessageElement(&msg
->elements
[i
], self
->msg
)));
1875 static PyMethodDef py_ldb_msg_methods
[] = {
1876 { "keys", (PyCFunction
)py_ldb_msg_keys
, METH_NOARGS
, NULL
},
1877 { "remove", (PyCFunction
)py_ldb_msg_remove_attr
, METH_VARARGS
, NULL
},
1878 { "get", (PyCFunction
)py_ldb_msg_get
, METH_VARARGS
, NULL
},
1879 { "items", (PyCFunction
)py_ldb_msg_items
, METH_NOARGS
, NULL
},
1883 static PyObject
*py_ldb_msg_iter(PyLdbMessageObject
*self
)
1885 PyObject
*list
, *iter
;
1887 list
= py_ldb_msg_keys(self
);
1888 iter
= PyObject_GetIter(list
);
1893 static int py_ldb_msg_setitem(PyLdbMessageObject
*self
, PyObject
*name
, PyObject
*value
)
1897 if (!PyString_Check(name
)) {
1898 PyErr_SetNone(PyExc_TypeError
);
1902 attr_name
= PyString_AsString(name
);
1903 if (value
== NULL
) {
1905 ldb_msg_remove_attr(self
->msg
, attr_name
);
1907 struct ldb_message_element
*el
= PyObject_AsMessageElement(self
->msg
,
1908 value
, 0, attr_name
);
1911 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self
), attr_name
);
1912 ldb_msg_add(PyLdbMessage_AsMessage(self
), el
, el
->flags
);
1917 static Py_ssize_t
py_ldb_msg_length(PyLdbMessageObject
*self
)
1919 return PyLdbMessage_AsMessage(self
)->num_elements
;
1922 static PyMappingMethods py_ldb_msg_mapping
= {
1923 .mp_length
= (lenfunc
)py_ldb_msg_length
,
1924 .mp_subscript
= (binaryfunc
)py_ldb_msg_getitem
,
1925 .mp_ass_subscript
= (objobjargproc
)py_ldb_msg_setitem
,
1928 static PyObject
*py_ldb_msg_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwargs
)
1930 const char * const kwnames
[] = { "dn", NULL
};
1931 struct ldb_message
*ret
;
1932 TALLOC_CTX
*mem_ctx
;
1933 PyObject
*pydn
= NULL
;
1934 PyLdbMessageObject
*py_ret
;
1936 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|O",
1937 discard_const_p(char *, kwnames
),
1941 mem_ctx
= talloc_new(NULL
);
1942 if (mem_ctx
== NULL
) {
1947 ret
= ldb_msg_new(mem_ctx
);
1949 talloc_free(mem_ctx
);
1956 if (!PyObject_AsDn(NULL
, pydn
, NULL
, &dn
)) {
1957 talloc_free(mem_ctx
);
1960 ret
->dn
= talloc_reference(ret
, dn
);
1963 py_ret
= (PyLdbMessageObject
*)type
->tp_alloc(type
, 0);
1964 if (py_ret
== NULL
) {
1966 talloc_free(mem_ctx
);
1970 py_ret
->mem_ctx
= mem_ctx
;
1972 return (PyObject
*)py_ret
;
1975 PyObject
*PyLdbMessage_FromMessage(struct ldb_message
*msg
)
1977 PyLdbMessageObject
*ret
;
1979 ret
= (PyLdbMessageObject
*)PyLdbMessage
.tp_alloc(&PyLdbMessage
, 0);
1984 ret
->mem_ctx
= talloc_new(NULL
);
1985 ret
->msg
= talloc_reference(ret
->mem_ctx
, msg
);
1986 return (PyObject
*)ret
;
1989 static PyObject
*py_ldb_msg_get_dn(PyLdbMessageObject
*self
, void *closure
)
1991 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
1992 return PyLdbDn_FromDn(msg
->dn
);
1995 static int py_ldb_msg_set_dn(PyLdbMessageObject
*self
, PyObject
*value
, void *closure
)
1997 struct ldb_message
*msg
= PyLdbMessage_AsMessage(self
);
1998 if (!PyLdbDn_Check(value
)) {
1999 PyErr_SetNone(PyExc_TypeError
);
2003 msg
->dn
= talloc_reference(msg
, PyLdbDn_AsDn(value
));
2007 static PyGetSetDef py_ldb_msg_getset
[] = {
2008 { discard_const_p(char, "dn"), (getter
)py_ldb_msg_get_dn
, (setter
)py_ldb_msg_set_dn
, NULL
},
2012 static PyObject
*py_ldb_msg_repr(PyLdbMessageObject
*self
)
2014 PyObject
*dict
= PyDict_New(), *ret
;
2015 if (PyDict_Update(dict
, (PyObject
*)self
) != 0)
2017 ret
= PyString_FromFormat("Message(%s)", PyObject_REPR(dict
));
2022 static void py_ldb_msg_dealloc(PyLdbMessageObject
*self
)
2024 talloc_free(self
->mem_ctx
);
2025 self
->ob_type
->tp_free(self
);
2028 PyTypeObject PyLdbMessage
= {
2029 .tp_name
= "Message",
2030 .tp_methods
= py_ldb_msg_methods
,
2031 .tp_getset
= py_ldb_msg_getset
,
2032 .tp_as_mapping
= &py_ldb_msg_mapping
,
2033 .tp_basicsize
= sizeof(PyLdbMessageObject
),
2034 .tp_dealloc
= (destructor
)py_ldb_msg_dealloc
,
2035 .tp_new
= py_ldb_msg_new
,
2036 .tp_repr
= (reprfunc
)py_ldb_msg_repr
,
2037 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2038 .tp_iter
= (getiterfunc
)py_ldb_msg_iter
,
2041 PyObject
*PyLdbTree_FromTree(struct ldb_parse_tree
*tree
)
2043 PyLdbTreeObject
*ret
;
2045 ret
= (PyLdbTreeObject
*)PyLdbTree
.tp_alloc(&PyLdbTree
, 0);
2051 ret
->mem_ctx
= talloc_new(NULL
);
2052 ret
->tree
= talloc_reference(ret
->mem_ctx
, tree
);
2053 return (PyObject
*)ret
;
2056 static void py_ldb_tree_dealloc(PyLdbTreeObject
*self
)
2058 talloc_free(self
->mem_ctx
);
2059 self
->ob_type
->tp_free(self
);
2062 PyTypeObject PyLdbTree
= {
2064 .tp_basicsize
= sizeof(PyLdbTreeObject
),
2065 .tp_dealloc
= (destructor
)py_ldb_tree_dealloc
,
2066 .tp_flags
= Py_TPFLAGS_DEFAULT
,
2070 static int py_module_search(struct ldb_module
*mod
, struct ldb_request
*req
)
2072 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2073 PyObject
*py_result
, *py_base
, *py_attrs
, *py_tree
;
2075 py_base
= PyLdbDn_FromDn(req
->op
.search
.base
);
2077 if (py_base
== NULL
)
2078 return LDB_ERR_OPERATIONS_ERROR
;
2080 py_tree
= PyLdbTree_FromTree(req
->op
.search
.tree
);
2082 if (py_tree
== NULL
)
2083 return LDB_ERR_OPERATIONS_ERROR
;
2085 if (req
->op
.search
.attrs
== NULL
) {
2089 for (len
= 0; req
->op
.search
.attrs
[len
]; len
++);
2090 py_attrs
= PyList_New(len
);
2091 for (i
= 0; i
< len
; i
++)
2092 PyList_SetItem(py_attrs
, i
, PyString_FromString(req
->op
.search
.attrs
[i
]));
2095 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "search"),
2096 discard_const_p(char, "OiOO"),
2097 py_base
, req
->op
.search
.scope
, py_tree
, py_attrs
);
2099 Py_DECREF(py_attrs
);
2103 if (py_result
== NULL
) {
2104 return LDB_ERR_PYTHON_EXCEPTION
;
2107 req
->op
.search
.res
= PyLdbResult_AsResult(NULL
, py_result
);
2108 if (req
->op
.search
.res
== NULL
) {
2109 return LDB_ERR_PYTHON_EXCEPTION
;
2112 Py_DECREF(py_result
);
2117 static int py_module_add(struct ldb_module
*mod
, struct ldb_request
*req
)
2119 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2120 PyObject
*py_result
, *py_msg
;
2122 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.add
.message
));
2124 if (py_msg
== NULL
) {
2125 return LDB_ERR_OPERATIONS_ERROR
;
2128 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "add"),
2129 discard_const_p(char, "O"),
2134 if (py_result
== NULL
) {
2135 return LDB_ERR_PYTHON_EXCEPTION
;
2138 Py_DECREF(py_result
);
2143 static int py_module_modify(struct ldb_module
*mod
, struct ldb_request
*req
)
2145 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2146 PyObject
*py_result
, *py_msg
;
2148 py_msg
= PyLdbMessage_FromMessage(discard_const_p(struct ldb_message
, req
->op
.mod
.message
));
2150 if (py_msg
== NULL
) {
2151 return LDB_ERR_OPERATIONS_ERROR
;
2154 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "modify"),
2155 discard_const_p(char, "O"),
2160 if (py_result
== NULL
) {
2161 return LDB_ERR_PYTHON_EXCEPTION
;
2164 Py_DECREF(py_result
);
2169 static int py_module_del(struct ldb_module
*mod
, struct ldb_request
*req
)
2171 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2172 PyObject
*py_result
, *py_dn
;
2174 py_dn
= PyLdbDn_FromDn(req
->op
.del
.dn
);
2177 return LDB_ERR_OPERATIONS_ERROR
;
2179 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "delete"),
2180 discard_const_p(char, "O"),
2183 if (py_result
== NULL
) {
2184 return LDB_ERR_PYTHON_EXCEPTION
;
2187 Py_DECREF(py_result
);
2192 static int py_module_rename(struct ldb_module
*mod
, struct ldb_request
*req
)
2194 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2195 PyObject
*py_result
, *py_olddn
, *py_newdn
;
2197 py_olddn
= PyLdbDn_FromDn(req
->op
.rename
.olddn
);
2199 if (py_olddn
== NULL
)
2200 return LDB_ERR_OPERATIONS_ERROR
;
2202 py_newdn
= PyLdbDn_FromDn(req
->op
.rename
.newdn
);
2204 if (py_newdn
== NULL
)
2205 return LDB_ERR_OPERATIONS_ERROR
;
2207 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "rename"),
2208 discard_const_p(char, "OO"),
2209 py_olddn
, py_newdn
);
2211 Py_DECREF(py_olddn
);
2212 Py_DECREF(py_newdn
);
2214 if (py_result
== NULL
) {
2215 return LDB_ERR_PYTHON_EXCEPTION
;
2218 Py_DECREF(py_result
);
2223 static int py_module_request(struct ldb_module
*mod
, struct ldb_request
*req
)
2225 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2226 PyObject
*py_result
;
2228 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "request"),
2229 discard_const_p(char, ""));
2231 return LDB_ERR_OPERATIONS_ERROR
;
2234 static int py_module_extended(struct ldb_module
*mod
, struct ldb_request
*req
)
2236 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2237 PyObject
*py_result
;
2239 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "extended"),
2240 discard_const_p(char, ""));
2242 return LDB_ERR_OPERATIONS_ERROR
;
2245 static int py_module_start_transaction(struct ldb_module
*mod
)
2247 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2248 PyObject
*py_result
;
2250 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "start_transaction"),
2251 discard_const_p(char, ""));
2253 if (py_result
== NULL
) {
2254 return LDB_ERR_PYTHON_EXCEPTION
;
2257 Py_DECREF(py_result
);
2262 static int py_module_end_transaction(struct ldb_module
*mod
)
2264 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2265 PyObject
*py_result
;
2267 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "end_transaction"),
2268 discard_const_p(char, ""));
2270 if (py_result
== NULL
) {
2271 return LDB_ERR_PYTHON_EXCEPTION
;
2274 Py_DECREF(py_result
);
2279 static int py_module_del_transaction(struct ldb_module
*mod
)
2281 PyObject
*py_ldb
= (PyObject
*)mod
->private_data
;
2282 PyObject
*py_result
;
2284 py_result
= PyObject_CallMethod(py_ldb
, discard_const_p(char, "del_transaction"),
2285 discard_const_p(char, ""));
2287 if (py_result
== NULL
) {
2288 return LDB_ERR_PYTHON_EXCEPTION
;
2291 Py_DECREF(py_result
);
2296 static int py_module_destructor(struct ldb_module
*mod
)
2298 Py_DECREF((PyObject
*)mod
->private_data
);
2302 static int py_module_init(struct ldb_module
*mod
)
2304 PyObject
*py_class
= (PyObject
*)mod
->ops
->private_data
;
2305 PyObject
*py_result
, *py_next
, *py_ldb
;
2307 py_ldb
= PyLdb_FromLdbContext(mod
->ldb
);
2310 return LDB_ERR_OPERATIONS_ERROR
;
2312 py_next
= PyLdbModule_FromModule(mod
->next
);
2314 if (py_next
== NULL
)
2315 return LDB_ERR_OPERATIONS_ERROR
;
2317 py_result
= PyObject_CallFunction(py_class
, discard_const_p(char, "OO"),
2320 if (py_result
== NULL
) {
2321 return LDB_ERR_PYTHON_EXCEPTION
;
2324 mod
->private_data
= py_result
;
2326 talloc_set_destructor(mod
, py_module_destructor
);
2328 return ldb_next_init(mod
);
2331 static PyObject
*py_register_module(PyObject
*module
, PyObject
*args
)
2334 struct ldb_module_ops
*ops
;
2337 if (!PyArg_ParseTuple(args
, "O", &input
))
2340 ops
= talloc_zero(talloc_autofree_context(), struct ldb_module_ops
);
2346 ops
->name
= talloc_strdup(ops
, PyString_AsString(PyObject_GetAttrString(input
, discard_const_p(char, "name"))));
2349 ops
->private_data
= input
;
2350 ops
->init_context
= py_module_init
;
2351 ops
->search
= py_module_search
;
2352 ops
->add
= py_module_add
;
2353 ops
->modify
= py_module_modify
;
2354 ops
->del
= py_module_del
;
2355 ops
->rename
= py_module_rename
;
2356 ops
->request
= py_module_request
;
2357 ops
->extended
= py_module_extended
;
2358 ops
->start_transaction
= py_module_start_transaction
;
2359 ops
->end_transaction
= py_module_end_transaction
;
2360 ops
->del_transaction
= py_module_del_transaction
;
2362 ret
= ldb_register_module(ops
);
2364 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError
, ret
, NULL
);
2369 static PyObject
*py_timestring(PyObject
*module
, PyObject
*args
)
2374 if (!PyArg_ParseTuple(args
, "L", &t
))
2376 tresult
= ldb_timestring(NULL
, t
);
2377 ret
= PyString_FromString(tresult
);
2378 talloc_free(tresult
);
2382 static PyObject
*py_string_to_time(PyObject
*module
, PyObject
*args
)
2385 if (!PyArg_ParseTuple(args
, "s", &str
))
2388 return PyInt_FromLong(ldb_string_to_time(str
));
2391 static PyObject
*py_valid_attr_name(PyObject
*self
, PyObject
*args
)
2394 if (!PyArg_ParseTuple(args
, "s", &name
))
2396 return PyBool_FromLong(ldb_valid_attr_name(name
));
2399 static PyMethodDef py_ldb_global_methods
[] = {
2400 { "register_module", py_register_module
, METH_VARARGS
,
2401 "S.register_module(module) -> None\n"
2402 "Register a LDB module."},
2403 { "timestring", py_timestring
, METH_VARARGS
,
2404 "S.timestring(int) -> string\n"
2405 "Generate a LDAP time string from a UNIX timestamp" },
2406 { "string_to_time", py_string_to_time
, METH_VARARGS
,
2407 "S.string_to_time(string) -> int\n"
2408 "Parse a LDAP time string into a UNIX timestamp." },
2409 { "valid_attr_name", py_valid_attr_name
, METH_VARARGS
,
2410 "S.valid_attr_name(name) -> bool\n"
2411 "Check whether the supplied name is a valid attribute name." },
2412 { "open", (PyCFunction
)py_ldb_new
, METH_VARARGS
|METH_KEYWORDS
,
2421 if (PyType_Ready(&PyLdbDn
) < 0)
2424 if (PyType_Ready(&PyLdbMessage
) < 0)
2427 if (PyType_Ready(&PyLdbMessageElement
) < 0)
2430 if (PyType_Ready(&PyLdb
) < 0)
2433 if (PyType_Ready(&PyLdbModule
) < 0)
2436 if (PyType_Ready(&PyLdbTree
) < 0)
2439 m
= Py_InitModule3("ldb", py_ldb_global_methods
,
2440 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server.");
2444 PyModule_AddObject(m
, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT
));
2445 PyModule_AddObject(m
, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE
));
2446 PyModule_AddObject(m
, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL
));
2447 PyModule_AddObject(m
, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE
));
2449 PyModule_AddObject(m
, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE
));
2450 PyModule_AddObject(m
, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD
));
2451 PyModule_AddObject(m
, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE
));
2452 PyModule_AddObject(m
, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY
));
2454 PyModule_AddObject(m
, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD
));
2455 PyModule_AddObject(m
, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE
));
2456 PyModule_AddObject(m
, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE
));
2458 PyModule_AddObject(m
, "SUCCESS", PyInt_FromLong(LDB_SUCCESS
));
2459 PyModule_AddObject(m
, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR
));
2460 PyModule_AddObject(m
, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR
));
2461 PyModule_AddObject(m
, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED
));
2462 PyModule_AddObject(m
, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED
));
2463 PyModule_AddObject(m
, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE
));
2464 PyModule_AddObject(m
, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE
));
2465 PyModule_AddObject(m
, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED
));
2466 PyModule_AddObject(m
, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED
));
2467 PyModule_AddObject(m
, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL
));
2468 PyModule_AddObject(m
, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED
));
2469 PyModule_AddObject(m
, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION
));
2470 PyModule_AddObject(m
, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED
));
2471 PyModule_AddObject(m
, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS
));
2472 PyModule_AddObject(m
, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE
));
2473 PyModule_AddObject(m
, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE
));
2474 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING
));
2475 PyModule_AddObject(m
, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION
));
2476 PyModule_AddObject(m
, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS
));
2477 PyModule_AddObject(m
, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX
));
2478 PyModule_AddObject(m
, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT
));
2479 PyModule_AddObject(m
, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM
));
2480 PyModule_AddObject(m
, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX
));
2481 PyModule_AddObject(m
, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM
));
2482 PyModule_AddObject(m
, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION
));
2483 PyModule_AddObject(m
, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS
));
2484 PyModule_AddObject(m
, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS
));
2485 PyModule_AddObject(m
, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY
));
2486 PyModule_AddObject(m
, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE
));
2487 PyModule_AddObject(m
, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM
));
2488 PyModule_AddObject(m
, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT
));
2489 PyModule_AddObject(m
, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION
));
2490 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION
));
2491 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF
));
2492 PyModule_AddObject(m
, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN
));
2493 PyModule_AddObject(m
, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS
));
2494 PyModule_AddObject(m
, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED
));
2495 PyModule_AddObject(m
, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS
));
2496 PyModule_AddObject(m
, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER
));
2498 PyModule_AddObject(m
, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY
));
2499 PyModule_AddObject(m
, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC
));
2500 PyModule_AddObject(m
, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT
));
2501 PyModule_AddObject(m
, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP
));
2504 PyModule_AddObject(m
, "__docformat__", PyString_FromString("restructuredText"));
2506 PyExc_LdbError
= PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL
, NULL
);
2507 PyModule_AddObject(m
, "LdbError", PyExc_LdbError
);
2510 Py_INCREF(&PyLdbDn
);
2511 Py_INCREF(&PyLdbModule
);
2512 Py_INCREF(&PyLdbMessage
);
2513 Py_INCREF(&PyLdbMessageElement
);
2514 Py_INCREF(&PyLdbTree
);
2516 PyModule_AddObject(m
, "Ldb", (PyObject
*)&PyLdb
);
2517 PyModule_AddObject(m
, "Dn", (PyObject
*)&PyLdbDn
);
2518 PyModule_AddObject(m
, "Message", (PyObject
*)&PyLdbMessage
);
2519 PyModule_AddObject(m
, "MessageElement", (PyObject
*)&PyLdbMessageElement
);
2520 PyModule_AddObject(m
, "Module", (PyObject
*)&PyLdbModule
);
2521 PyModule_AddObject(m
, "Tree", (PyObject
*)&PyLdbTree
);