1/* 2 Unix SMB/CIFS implementation. 3 4 Python interface to ldb. 5 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 10 11 ** NOTE! The following LGPL license applies to the ldb 12 ** library. This does NOT imply that all of Samba is released 13 ** under the LGPL 14 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. 19 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. 24 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/>. 27*/ 28 29#include "replace.h" 30#include "ldb_private.h" 31#include <Python.h> 32#include "pyldb.h" 33 34/* There's no Py_ssize_t in 2.4, apparently */ 35#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION < 5 36typedef int Py_ssize_t; 37typedef inquiry lenfunc; 38typedef intargfunc ssizeargfunc; 39#endif 40 41#ifndef Py_RETURN_NONE 42#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None 43#endif 44 45static void PyErr_SetLdbError(PyObject *error, int ret, struct ldb_context *ldb_ctx) 46{ 47 if (ret == LDB_ERR_PYTHON_EXCEPTION) 48 return; /* Python exception should already be set, just keep that */ 49 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))); 53} 54 55static PyObject *PyExc_LdbError; 56 57PyAPI_DATA(PyTypeObject) PyLdbMessage; 58PyAPI_DATA(PyTypeObject) PyLdbModule; 59PyAPI_DATA(PyTypeObject) PyLdbDn; 60PyAPI_DATA(PyTypeObject) PyLdb; 61PyAPI_DATA(PyTypeObject) PyLdbMessageElement; 62PyAPI_DATA(PyTypeObject) PyLdbTree; 63 64static PyObject *PyObject_FromLdbValue(struct ldb_context *ldb_ctx, 65 struct ldb_message_element *el, 66 struct ldb_val *val) 67{ 68 struct ldb_val new_val; 69 TALLOC_CTX *mem_ctx = talloc_new(NULL); 70 PyObject *ret; 71 72 new_val = *val; 73 74 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length); 75 76 talloc_free(mem_ctx); 77 78 return ret; 79} 80 81/** 82 * Obtain a ldb DN from a Python object. 83 * 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 88 */ 89bool PyObject_AsDn(TALLOC_CTX *mem_ctx, PyObject *object, 90 struct ldb_context *ldb_ctx, struct ldb_dn **dn) 91{ 92 struct ldb_dn *odn; 93 94 if (ldb_ctx != NULL && PyString_Check(object)) { 95 odn = ldb_dn_new(mem_ctx, ldb_ctx, PyString_AsString(object)); 96 *dn = odn; 97 return true; 98 } 99 100 if (PyLdbDn_Check(object)) { 101 *dn = PyLdbDn_AsDn(object); 102 return true; 103 } 104 105 PyErr_SetString(PyExc_TypeError, "Expected DN"); 106 return false; 107} 108 109/** 110 * Create a Python object from a ldb_result. 111 * 112 * @param result LDB result to convert 113 * @return Python object with converted result (a list object) 114 */ 115static PyObject *PyLdbResult_FromResult(struct ldb_result *result) 116{ 117 PyObject *ret; 118 int i; 119 if (result == NULL) { 120 Py_RETURN_NONE; 121 } 122 ret = PyList_New(result->count); 123 for (i = 0; i < result->count; i++) { 124 PyList_SetItem(ret, i, PyLdbMessage_FromMessage(result->msgs[i]) 125 ); 126 } 127 return ret; 128} 129 130/** 131 * Create a LDB Result from a Python object. 132 * If conversion fails, NULL will be returned and a Python exception set. 133 * 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 137 */ 138static struct ldb_result *PyLdbResult_AsResult(TALLOC_CTX *mem_ctx, 139 PyObject *obj) 140{ 141 struct ldb_result *res; 142 int i; 143 144 if (obj == Py_None) 145 return NULL; 146 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); 153 } 154 return res; 155} 156 157static PyObject *py_ldb_dn_validate(PyLdbDnObject *self) 158{ 159 return PyBool_FromLong(ldb_dn_validate(self->dn)); 160} 161 162static PyObject *py_ldb_dn_is_valid(PyLdbDnObject *self) 163{ 164 return PyBool_FromLong(ldb_dn_is_valid(self->dn)); 165} 166 167static PyObject *py_ldb_dn_is_special(PyLdbDnObject *self) 168{ 169 return PyBool_FromLong(ldb_dn_is_special(self->dn)); 170} 171 172static PyObject *py_ldb_dn_is_null(PyLdbDnObject *self) 173{ 174 return PyBool_FromLong(ldb_dn_is_null(self->dn)); 175} 176 177static PyObject *py_ldb_dn_get_casefold(PyLdbDnObject *self) 178{ 179 return PyString_FromString(ldb_dn_get_casefold(self->dn)); 180} 181 182static PyObject *py_ldb_dn_get_linearized(PyLdbDnObject *self) 183{ 184 return PyString_FromString(ldb_dn_get_linearized(self->dn)); 185} 186 187static PyObject *py_ldb_dn_canonical_str(PyLdbDnObject *self) 188{ 189 return PyString_FromString(ldb_dn_canonical_string(self->dn, self->dn)); 190} 191 192static PyObject *py_ldb_dn_canonical_ex_str(PyLdbDnObject *self) 193{ 194 return PyString_FromString(ldb_dn_canonical_ex_string(self->dn, self->dn)); 195} 196 197static PyObject *py_ldb_dn_repr(PyLdbDnObject *self) 198{ 199 return PyString_FromFormat("Dn(%s)", PyObject_REPR(PyString_FromString(ldb_dn_get_linearized(self->dn)))); 200} 201 202static PyObject *py_ldb_dn_check_special(PyLdbDnObject *self, PyObject *args) 203{ 204 char *name; 205 206 if (!PyArg_ParseTuple(args, "s", &name)) 207 return NULL; 208 209 return ldb_dn_check_special(self->dn, name)?Py_True:Py_False; 210} 211 212static int py_ldb_dn_compare(PyLdbDnObject *dn1, PyLdbDnObject *dn2) 213{ 214 int ret; 215 ret = ldb_dn_compare(dn1->dn, dn2->dn); 216 if (ret < 0) ret = -1; 217 if (ret > 0) ret = 1; 218 return ret; 219} 220 221static PyObject *py_ldb_dn_get_parent(PyLdbDnObject *self) 222{ 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); 227 228 parent = ldb_dn_get_parent(mem_ctx, dn); 229 if (parent == NULL) { 230 talloc_free(mem_ctx); 231 Py_RETURN_NONE; 232 } 233 234 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0); 235 if (py_ret == NULL) { 236 PyErr_NoMemory(); 237 talloc_free(mem_ctx); 238 return NULL; 239 } 240 py_ret->mem_ctx = mem_ctx; 241 py_ret->dn = parent; 242 return (PyObject *)py_ret; 243} 244 245#define dn_ldb_ctx(dn) ((struct ldb_context *)dn) 246 247static PyObject *py_ldb_dn_add_child(PyLdbDnObject *self, PyObject *args) 248{ 249 PyObject *py_other; 250 struct ldb_dn *dn, *other; 251 if (!PyArg_ParseTuple(args, "O", &py_other)) 252 return NULL; 253 254 dn = PyLdbDn_AsDn((PyObject *)self); 255 256 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other)) 257 return NULL; 258 259 return ldb_dn_add_child(dn, other)?Py_True:Py_False; 260} 261 262static PyObject *py_ldb_dn_add_base(PyLdbDnObject *self, PyObject *args) 263{ 264 PyObject *py_other; 265 struct ldb_dn *other, *dn; 266 if (!PyArg_ParseTuple(args, "O", &py_other)) 267 return NULL; 268 269 dn = PyLdbDn_AsDn((PyObject *)self); 270 271 if (!PyObject_AsDn(NULL, py_other, dn_ldb_ctx(dn), &other)) 272 return NULL; 273 274 return ldb_dn_add_base(dn, other)?Py_True:Py_False; 275} 276 277static 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, 289 NULL }, 290 { "get_linearized", (PyCFunction)py_ldb_dn_get_linearized, METH_NOARGS, 291 NULL }, 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, 299 NULL }, 300 { "parent", (PyCFunction)py_ldb_dn_get_parent, METH_NOARGS, 301 "S.parent() -> dn\n" 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, 310 NULL }, 311 { NULL } 312}; 313 314static Py_ssize_t py_ldb_dn_len(PyLdbDnObject *self) 315{ 316 return ldb_dn_get_comp_num(PyLdbDn_AsDn((PyObject *)self)); 317} 318 319static PyObject *py_ldb_dn_concat(PyLdbDnObject *self, PyObject *py_other) 320{ 321 struct ldb_dn *dn = PyLdbDn_AsDn((PyObject *)self), 322 *other; 323 PyLdbDnObject *py_ret; 324 325 if (!PyObject_AsDn(NULL, py_other, NULL, &other)) 326 return NULL; 327 328 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0); 329 if (py_ret == NULL) { 330 PyErr_NoMemory(); 331 return NULL; 332 } 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; 337} 338 339static PySequenceMethods py_ldb_dn_seq = { 340 .sq_length = (lenfunc)py_ldb_dn_len, 341 .sq_concat = (binaryfunc)py_ldb_dn_concat, 342}; 343 344static PyObject *py_ldb_dn_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 345{ 346 struct ldb_dn *ret; 347 char *str; 348 PyObject *py_ldb; 349 struct ldb_context *ldb_ctx; 350 TALLOC_CTX *mem_ctx; 351 PyLdbDnObject *py_ret; 352 const char * const kwnames[] = { "ldb", "dn", NULL }; 353 354 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os", 355 discard_const_p(char *, kwnames), 356 &py_ldb, &str)) 357 return NULL; 358 359 ldb_ctx = PyLdb_AsLdbContext(py_ldb); 360 361 mem_ctx = talloc_new(NULL); 362 if (mem_ctx == NULL) { 363 PyErr_NoMemory(); 364 return NULL; 365 } 366 367 ret = ldb_dn_new(mem_ctx, ldb_ctx, str); 368 369 if (ret == NULL || !ldb_dn_validate(ret)) { 370 talloc_free(mem_ctx); 371 PyErr_SetString(PyExc_ValueError, "unable to parse dn string"); 372 return NULL; 373 } 374 375 py_ret = (PyLdbDnObject *)type->tp_alloc(type, 0); 376 if (ret == NULL) { 377 talloc_free(mem_ctx); 378 PyErr_NoMemory(); 379 return NULL; 380 } 381 py_ret->mem_ctx = mem_ctx; 382 py_ret->dn = ret; 383 return (PyObject *)py_ret; 384} 385 386PyObject *PyLdbDn_FromDn(struct ldb_dn *dn) 387{ 388 PyLdbDnObject *py_ret; 389 390 if (dn == NULL) { 391 Py_RETURN_NONE; 392 } 393 394 py_ret = (PyLdbDnObject *)PyLdbDn.tp_alloc(&PyLdbDn, 0); 395 if (py_ret == NULL) { 396 PyErr_NoMemory(); 397 return NULL; 398 } 399 py_ret->mem_ctx = talloc_new(NULL); 400 py_ret->dn = talloc_reference(py_ret->mem_ctx, dn); 401 return (PyObject *)py_ret; 402} 403 404static void py_ldb_dn_dealloc(PyLdbDnObject *self) 405{ 406 talloc_free(self->mem_ctx); 407 self->ob_type->tp_free(self); 408} 409 410PyTypeObject PyLdbDn = { 411 .tp_name = "Dn", 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, 422}; 423 424/* Debug */ 425static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(3, 0); 426static void py_ldb_debug(void *context, enum ldb_debug_level level, const char *fmt, va_list ap) 427{ 428 PyObject *fn = (PyObject *)context; 429 PyObject_CallFunction(fn, discard_const_p(char, "(i,O)"), level, PyString_FromFormatV(fmt, ap)); 430} 431 432static PyObject *py_ldb_set_debug(PyLdbObject *self, PyObject *args) 433{ 434 PyObject *cb; 435 436 if (!PyArg_ParseTuple(args, "O", &cb)) 437 return NULL; 438 439 Py_INCREF(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)); 442 443 Py_RETURN_NONE; 444} 445 446static PyObject *py_ldb_set_create_perms(PyTypeObject *self, PyObject *args) 447{ 448 unsigned int perms; 449 if (!PyArg_ParseTuple(args, "I", &perms)) 450 return NULL; 451 452 ldb_set_create_perms(PyLdb_AsLdbContext(self), perms); 453 454 Py_RETURN_NONE; 455} 456 457static PyObject *py_ldb_set_modules_dir(PyTypeObject *self, PyObject *args) 458{ 459 char *modules_dir; 460 if (!PyArg_ParseTuple(args, "s", &modules_dir)) 461 return NULL; 462 463 ldb_set_modules_dir(PyLdb_AsLdbContext(self), modules_dir); 464 465 Py_RETURN_NONE; 466} 467 468static PyObject *py_ldb_transaction_start(PyLdbObject *self) 469{ 470 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_start(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self)); 471 Py_RETURN_NONE; 472} 473 474static PyObject *py_ldb_transaction_commit(PyLdbObject *self) 475{ 476 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_commit(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self)); 477 Py_RETURN_NONE; 478} 479 480static PyObject *py_ldb_transaction_cancel(PyLdbObject *self) 481{ 482 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_transaction_cancel(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self)); 483 Py_RETURN_NONE; 484} 485 486static PyObject *py_ldb_setup_wellknown_attributes(PyLdbObject *self) 487{ 488 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ldb_setup_wellknown_attributes(PyLdb_AsLdbContext(self)), PyLdb_AsLdbContext(self)); 489 Py_RETURN_NONE; 490} 491 492static PyObject *py_ldb_repr(PyLdbObject *self) 493{ 494 return PyString_FromFormat("<ldb connection>"); 495} 496 497static PyObject *py_ldb_get_root_basedn(PyLdbObject *self) 498{ 499 struct ldb_dn *dn = ldb_get_root_basedn(PyLdb_AsLdbContext(self)); 500 if (dn == NULL) 501 Py_RETURN_NONE; 502 return PyLdbDn_FromDn(dn); 503} 504 505 506static PyObject *py_ldb_get_schema_basedn(PyLdbObject *self) 507{ 508 struct ldb_dn *dn = ldb_get_schema_basedn(PyLdb_AsLdbContext(self)); 509 if (dn == NULL) 510 Py_RETURN_NONE; 511 return PyLdbDn_FromDn(dn); 512} 513 514static PyObject *py_ldb_get_config_basedn(PyLdbObject *self) 515{ 516 struct ldb_dn *dn = ldb_get_config_basedn(PyLdb_AsLdbContext(self)); 517 if (dn == NULL) 518 Py_RETURN_NONE; 519 return PyLdbDn_FromDn(dn); 520} 521 522static PyObject *py_ldb_get_default_basedn(PyLdbObject *self) 523{ 524 struct ldb_dn *dn = ldb_get_default_basedn(PyLdb_AsLdbContext(self)); 525 if (dn == NULL) 526 Py_RETURN_NONE; 527 return PyLdbDn_FromDn(dn); 528} 529 530static const char **PyList_AsStringList(TALLOC_CTX *mem_ctx, PyObject *list, 531 const char *paramname) 532{ 533 const char **ret; 534 int i; 535 if (!PyList_Check(list)) { 536 PyErr_Format(PyExc_TypeError, "%s is not a list", paramname); 537 return NULL; 538 } 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); 544 return NULL; 545 } 546 ret[i] = talloc_strndup(ret, PyString_AsString(item), 547 PyString_Size(item)); 548 } 549 ret[i] = NULL; 550 return ret; 551} 552 553static int py_ldb_init(PyLdbObject *self, PyObject *args, PyObject *kwargs) 554{ 555 const char * const kwnames[] = { "url", "flags", "options", NULL }; 556 char *url = NULL; 557 PyObject *py_options = Py_None; 558 const char **options; 559 int flags = 0; 560 int ret; 561 struct ldb_context *ldb; 562 563 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO:Ldb.__init__", 564 discard_const_p(char *, kwnames), 565 &url, &flags, &py_options)) 566 return -1; 567 568 ldb = PyLdb_AsLdbContext(self); 569 570 if (py_options == Py_None) { 571 options = NULL; 572 } else { 573 options = PyList_AsStringList(ldb, py_options, "options"); 574 if (options == NULL) 575 return -1; 576 } 577 578 if (url != NULL) { 579 ret = ldb_connect(ldb, url, flags, options); 580 if (ret != LDB_SUCCESS) { 581 PyErr_SetLdbError(PyExc_LdbError, ret, ldb); 582 return -1; 583 } 584 } 585 586 talloc_free(options); 587 return 0; 588} 589 590static PyObject *py_ldb_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 591{ 592 PyLdbObject *ret; 593 struct ldb_context *ldb; 594 ret = (PyLdbObject *)type->tp_alloc(type, 0); 595 if (ret == NULL) { 596 PyErr_NoMemory(); 597 return NULL; 598 } 599 ret->mem_ctx = talloc_new(NULL); 600 ldb = ldb_init(ret->mem_ctx, NULL); 601 602 if (ldb == NULL) { 603 PyErr_NoMemory(); 604 return NULL; 605 } 606 607 ret->ldb_ctx = ldb; 608 return (PyObject *)ret; 609} 610 611static PyObject *py_ldb_connect(PyLdbObject *self, PyObject *args, PyObject *kwargs) 612{ 613 char *url; 614 int flags = 0; 615 PyObject *py_options = Py_None; 616 int ret; 617 const char **options; 618 const char * const kwnames[] = { "url", "flags", "options", NULL }; 619 620 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ziO", 621 discard_const_p(char *, kwnames), 622 &url, &flags, &py_options)) 623 return NULL; 624 625 if (py_options == Py_None) { 626 options = NULL; 627 } else { 628 options = PyList_AsStringList(NULL, py_options, "options"); 629 if (options == NULL) 630 return NULL; 631 } 632 633 ret = ldb_connect(PyLdb_AsLdbContext(self), url, flags, options); 634 talloc_free(options); 635 636 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self)); 637 638 Py_RETURN_NONE; 639} 640 641static PyObject *py_ldb_modify(PyLdbObject *self, PyObject *args) 642{ 643 PyObject *py_msg; 644 int ret; 645 if (!PyArg_ParseTuple(args, "O", &py_msg)) 646 return NULL; 647 648 if (!PyLdbMessage_Check(py_msg)) { 649 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message"); 650 return NULL; 651 } 652 653 ret = ldb_modify(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg)); 654 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self)); 655 656 Py_RETURN_NONE; 657} 658 659static PyObject *py_ldb_add(PyLdbObject *self, PyObject *args) 660{ 661 PyObject *py_msg; 662 int ret; 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; 670 TALLOC_CTX *mem_ctx; 671 struct ldb_control **parsed_controls; 672 673 if (!PyArg_ParseTuple(args, "O|O", &py_msg, &py_controls )) 674 return NULL; 675 ldb_ctx = PyLdb_AsLdbContext(self); 676 677 mem_ctx = talloc_new(NULL); 678 if (py_controls == Py_None) { 679 parsed_controls = NULL; 680 } else { 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); 684 } 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; 690 if (dn_value) { 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); 694 return NULL; 695 } 696 if (msg->dn == NULL) { 697 PyErr_SetString(PyExc_TypeError, "dn set but not found"); 698 talloc_free(mem_ctx); 699 return NULL; 700 } 701 } 702 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); 707 if (msgel == NULL) { 708 PyErr_SetString(PyExc_TypeError, "unable to import element"); 709 talloc_free(mem_ctx); 710 return NULL; 711 } 712 memcpy(&msg->elements[msg_pos], msgel, sizeof(*msgel)); 713 msg_pos++; 714 } 715 } 716 717 if (msg->dn == NULL) { 718 PyErr_SetString(PyExc_TypeError, "no dn set"); 719 talloc_free(mem_ctx); 720 return NULL; 721 } 722 723 msg->num_elements = msg_pos; 724 } else { 725 msg = PyLdbMessage_AsMessage(py_msg); 726 } 727 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); 732 return NULL; 733 } 734 735 ret = ldb_build_add_req(&req, ldb_ctx, ldb_ctx, 736 msg, 737 parsed_controls, 738 NULL, 739 ldb_op_default_callback, 740 NULL); 741 742 if (ret != LDB_SUCCESS) { 743 PyErr_SetString(PyExc_TypeError, "failed to build request"); 744 talloc_free(mem_ctx); 745 return NULL; 746 } 747 748 /* do request and autostart a transaction */ 749 /* Then let's LDB handle the message error in case of pb as they are meaningful */ 750 751 ret = ldb_transaction_start(ldb_ctx); 752 if (ret != LDB_SUCCESS) { 753 talloc_free(req); 754 talloc_free(mem_ctx); 755 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self)); 756 } 757 758 ret = ldb_request(ldb_ctx, req); 759 if (ret == LDB_SUCCESS) { 760 ret = ldb_wait(req->handle, LDB_WAIT_ALL); 761 } 762 763 if (ret == LDB_SUCCESS) { 764 ret = ldb_transaction_commit(ldb_ctx); 765 } else { 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); 770 } 771 } 772 talloc_free(req); 773 talloc_free(mem_ctx); 774 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self)); 775 776 Py_RETURN_NONE; 777} 778 779static PyObject *py_ldb_delete(PyLdbObject *self, PyObject *args) 780{ 781 PyObject *py_dn; 782 struct ldb_dn *dn; 783 int ret; 784 struct ldb_context *ldb; 785 if (!PyArg_ParseTuple(args, "O", &py_dn)) 786 return NULL; 787 788 ldb = PyLdb_AsLdbContext(self); 789 790 if (!PyObject_AsDn(NULL, py_dn, ldb, &dn)) 791 return NULL; 792 793 ret = ldb_delete(ldb, dn); 794 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb); 795 796 Py_RETURN_NONE; 797} 798 799static PyObject *py_ldb_rename(PyLdbObject *self, PyObject *args) 800{ 801 PyObject *py_dn1, *py_dn2; 802 struct ldb_dn *dn1, *dn2; 803 int ret; 804 struct ldb_context *ldb; 805 TALLOC_CTX *mem_ctx; 806 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2)) 807 return NULL; 808 809 mem_ctx = talloc_new(NULL); 810 if (mem_ctx == NULL) { 811 PyErr_NoMemory(); 812 return NULL; 813 } 814 ldb = PyLdb_AsLdbContext(self); 815 if (!PyObject_AsDn(mem_ctx, py_dn1, ldb, &dn1)) { 816 talloc_free(mem_ctx); 817 return NULL; 818 } 819 820 if (!PyObject_AsDn(mem_ctx, py_dn2, ldb, &dn2)) { 821 talloc_free(mem_ctx); 822 return NULL; 823 } 824 825 ret = ldb_rename(ldb, dn1, dn2); 826 talloc_free(mem_ctx); 827 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb); 828 829 Py_RETURN_NONE; 830} 831 832static PyObject *py_ldb_schema_attribute_remove(PyLdbObject *self, PyObject *args) 833{ 834 char *name; 835 if (!PyArg_ParseTuple(args, "s", &name)) 836 return NULL; 837 838 ldb_schema_attribute_remove(PyLdb_AsLdbContext(self), name); 839 840 Py_RETURN_NONE; 841} 842 843static PyObject *py_ldb_schema_attribute_add(PyLdbObject *self, PyObject *args) 844{ 845 char *attribute, *syntax; 846 unsigned int flags; 847 int ret; 848 if (!PyArg_ParseTuple(args, "sIs", &attribute, &flags, &syntax)) 849 return NULL; 850 851 ret = ldb_schema_attribute_add(PyLdb_AsLdbContext(self), attribute, flags, syntax); 852 853 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, PyLdb_AsLdbContext(self)); 854 855 Py_RETURN_NONE; 856} 857 858static PyObject *ldb_ldif_to_pyobject(struct ldb_ldif *ldif) 859{ 860 if (ldif == NULL) { 861 Py_RETURN_NONE; 862 } else { 863 /* We don't want this attached to the 'ldb' any more */ 864 return Py_BuildValue(discard_const_p(char, "(iO)"), 865 ldif->changetype, 866 PyLdbMessage_FromMessage(ldif->msg)); 867 } 868} 869 870 871static PyObject *py_ldb_write_ldif(PyLdbMessageObject *self, PyObject *args) 872{ 873 int changetype; 874 PyObject *py_msg; 875 struct ldb_ldif ldif; 876 PyObject *ret; 877 char *string; 878 TALLOC_CTX *mem_ctx; 879 880 if (!PyArg_ParseTuple(args, "Oi", &py_msg, &changetype)) 881 return NULL; 882 883 if (!PyLdbMessage_Check(py_msg)) { 884 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for msg"); 885 return NULL; 886 } 887 888 ldif.msg = PyLdbMessage_AsMessage(py_msg); 889 ldif.changetype = changetype; 890 891 mem_ctx = talloc_new(NULL); 892 893 string = ldb_ldif_write_string(PyLdb_AsLdbContext(self), mem_ctx, &ldif); 894 if (!string) { 895 PyErr_SetString(PyExc_KeyError, "Failed to generate LDIF"); 896 return NULL; 897 } 898 899 ret = PyString_FromString(string); 900 901 talloc_free(mem_ctx); 902 903 return ret; 904} 905 906static PyObject *py_ldb_parse_ldif(PyLdbObject *self, PyObject *args) 907{ 908 PyObject *list; 909 struct ldb_ldif *ldif; 910 const char *s; 911 912 TALLOC_CTX *mem_ctx; 913 914 if (!PyArg_ParseTuple(args, "s", &s)) 915 return NULL; 916 917 mem_ctx = talloc_new(NULL); 918 if (!mem_ctx) { 919 Py_RETURN_NONE; 920 } 921 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); 926 if (ldif) { 927 PyList_Append(list, ldb_ldif_to_pyobject(ldif)); 928 } else { 929 PyErr_SetString(PyExc_ValueError, "unable to parse ldif string"); 930 talloc_free(mem_ctx); 931 return NULL; 932 } 933 } 934 talloc_free(mem_ctx); /* The pyobject already has a reference to the things it needs */ 935 return PyObject_GetIter(list); 936} 937 938static PyObject *py_ldb_msg_diff(PyLdbObject *self, PyObject *args) 939{ 940 PyObject *py_msg_old; 941 PyObject *py_msg_new; 942 struct ldb_message *diff; 943 PyObject *py_ret; 944 945 if (!PyArg_ParseTuple(args, "OO", &py_msg_old, &py_msg_new)) 946 return NULL; 947 948 if (!PyLdbMessage_Check(py_msg_old)) { 949 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for old message"); 950 return NULL; 951 } 952 953 if (!PyLdbMessage_Check(py_msg_new)) { 954 PyErr_SetString(PyExc_TypeError, "Expected Ldb Message for new message"); 955 return NULL; 956 } 957 958 diff = ldb_msg_diff(PyLdb_AsLdbContext(self), PyLdbMessage_AsMessage(py_msg_old), PyLdbMessage_AsMessage(py_msg_new)); 959 if (diff == NULL) 960 return NULL; 961 962 py_ret = PyLdbMessage_FromMessage(diff); 963 964 return py_ret; 965} 966 967static PyObject *py_ldb_schema_format_value(PyLdbObject *self, PyObject *args) 968{ 969 const struct ldb_schema_attribute *a; 970 struct ldb_val old_val; 971 struct ldb_val new_val; 972 TALLOC_CTX *mem_ctx; 973 PyObject *ret; 974 char *element_name; 975 PyObject *val; 976 977 if (!PyArg_ParseTuple(args, "sO", &element_name, &val)) 978 return NULL; 979 980 mem_ctx = talloc_new(NULL); 981 982 old_val.data = (uint8_t *)PyString_AsString(val); 983 old_val.length = PyString_Size(val); 984 985 a = ldb_schema_attribute_by_name(PyLdb_AsLdbContext(self), element_name); 986 987 if (a == NULL) { 988 Py_RETURN_NONE; 989 } 990 991 if (a->syntax->ldif_write_fn(PyLdb_AsLdbContext(self), mem_ctx, &old_val, &new_val) != 0) { 992 talloc_free(mem_ctx); 993 Py_RETURN_NONE; 994 } 995 996 ret = PyString_FromStringAndSize((const char *)new_val.data, new_val.length); 997 998 talloc_free(mem_ctx); 999 1000 return ret; 1001} 1002 1003static PyObject *py_ldb_search(PyLdbObject *self, PyObject *args, PyObject *kwargs) 1004{ 1005 PyObject *py_base = Py_None; 1006 enum ldb_scope scope = LDB_SCOPE_DEFAULT; 1007 char *expr = NULL; 1008 PyObject *py_attrs = Py_None; 1009 PyObject *py_controls = Py_None; 1010 const char * const kwnames[] = { "base", "scope", "expression", "attrs", "controls", NULL }; 1011 int ret; 1012 struct ldb_result *res; 1013 struct ldb_request *req; 1014 const char **attrs; 1015 struct ldb_context *ldb_ctx; 1016 struct ldb_control **parsed_controls; 1017 struct ldb_dn *base; 1018 PyObject *py_ret; 1019 1020 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OizOO", 1021 discard_const_p(char *, kwnames), 1022 &py_base, &scope, &expr, &py_attrs, &py_controls)) 1023 return NULL; 1024 1025 ldb_ctx = PyLdb_AsLdbContext(self); 1026 1027 if (py_attrs == Py_None) { 1028 attrs = NULL; 1029 } else { 1030 attrs = PyList_AsStringList(NULL, py_attrs, "attrs"); 1031 if (attrs == NULL) 1032 return NULL; 1033 } 1034 1035 if (py_base == Py_None) { 1036 base = ldb_get_default_basedn(ldb_ctx); 1037 } else { 1038 if (!PyObject_AsDn(ldb_ctx, py_base, ldb_ctx, &base)) { 1039 talloc_free(attrs); 1040 return NULL; 1041 } 1042 } 1043 1044 if (py_controls == Py_None) { 1045 parsed_controls = NULL; 1046 } else { 1047 const char **controls = PyList_AsStringList(ldb_ctx, py_controls, "controls"); 1048 parsed_controls = ldb_parse_control_strings(ldb_ctx, ldb_ctx, controls); 1049 talloc_free(controls); 1050 } 1051 1052 res = talloc_zero(ldb_ctx, struct ldb_result); 1053 if (res == NULL) { 1054 PyErr_NoMemory(); 1055 talloc_free(attrs); 1056 return NULL; 1057 } 1058 1059 ret = ldb_build_search_req(&req, ldb_ctx, ldb_ctx, 1060 base, 1061 scope, 1062 expr, 1063 attrs, 1064 parsed_controls, 1065 res, 1066 ldb_search_default_callback, 1067 NULL); 1068 1069 talloc_steal(req, attrs); 1070 1071 if (ret != LDB_SUCCESS) { 1072 talloc_free(res); 1073 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx); 1074 return NULL; 1075 } 1076 1077 ret = ldb_request(ldb_ctx, req); 1078 1079 if (ret == LDB_SUCCESS) { 1080 ret = ldb_wait(req->handle, LDB_WAIT_ALL); 1081 } 1082 1083 talloc_free(req); 1084 1085 if (ret != LDB_SUCCESS) { 1086 talloc_free(res); 1087 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, ldb_ctx); 1088 return NULL; 1089 } 1090 1091 py_ret = PyLdbResult_FromResult(res); 1092 1093 talloc_free(res); 1094 1095 return py_ret; 1096} 1097 1098static PyObject *py_ldb_get_opaque(PyLdbObject *self, PyObject *args) 1099{ 1100 char *name; 1101 void *data; 1102 1103 if (!PyArg_ParseTuple(args, "s", &name)) 1104 return NULL; 1105 1106 data = ldb_get_opaque(PyLdb_AsLdbContext(self), name); 1107 1108 if (data == NULL) 1109 Py_RETURN_NONE; 1110 1111 /* FIXME: More interpretation */ 1112 1113 return Py_True; 1114} 1115 1116static PyObject *py_ldb_set_opaque(PyLdbObject *self, PyObject *args) 1117{ 1118 char *name; 1119 PyObject *data; 1120 1121 if (!PyArg_ParseTuple(args, "sO", &name, &data)) 1122 return NULL; 1123 1124 /* FIXME: More interpretation */ 1125 1126 ldb_set_opaque(PyLdb_AsLdbContext(self), name, data); 1127 1128 Py_RETURN_NONE; 1129} 1130 1131static PyObject *py_ldb_modules(PyLdbObject *self) 1132{ 1133 struct ldb_context *ldb = PyLdb_AsLdbContext(self); 1134 PyObject *ret = PyList_New(0); 1135 struct ldb_module *mod; 1136 1137 for (mod = ldb->modules; mod; mod = mod->next) { 1138 PyList_Append(ret, PyLdbModule_FromModule(mod)); 1139 } 1140 1141 return ret; 1142} 1143 1144static PyMethodDef py_ldb_methods[] = { 1145 { "set_debug", (PyCFunction)py_ldb_set_debug, METH_VARARGS, 1146 "S.set_debug(callback) -> None\n" 1147 "Set callback for LDB debug messages.\n" 1148 "The callback should accept a debug level and debug text." }, 1149 { "set_create_perms", (PyCFunction)py_ldb_set_create_perms, METH_VARARGS, 1150 "S.set_create_perms(mode) -> None\n" 1151 "Set mode to use when creating new LDB files." }, 1152 { "set_modules_dir", (PyCFunction)py_ldb_set_modules_dir, METH_VARARGS, 1153 "S.set_modules_dir(path) -> None\n" 1154 "Set path LDB should search for modules" }, 1155 { "transaction_start", (PyCFunction)py_ldb_transaction_start, METH_NOARGS, 1156 "S.transaction_start() -> None\n" 1157 "Start a new transaction." }, 1158 { "transaction_commit", (PyCFunction)py_ldb_transaction_commit, METH_NOARGS, 1159 "S.transaction_commit() -> None\n" 1160 "commit a new transaction." }, 1161 { "transaction_cancel", (PyCFunction)py_ldb_transaction_cancel, METH_NOARGS, 1162 "S.transaction_cancel() -> None\n" 1163 "cancel a new transaction." }, 1164 { "setup_wellknown_attributes", (PyCFunction)py_ldb_setup_wellknown_attributes, METH_NOARGS, 1165 NULL }, 1166 { "get_root_basedn", (PyCFunction)py_ldb_get_root_basedn, METH_NOARGS, 1167 NULL }, 1168 { "get_schema_basedn", (PyCFunction)py_ldb_get_schema_basedn, METH_NOARGS, 1169 NULL }, 1170 { "get_default_basedn", (PyCFunction)py_ldb_get_default_basedn, METH_NOARGS, 1171 NULL }, 1172 { "get_config_basedn", (PyCFunction)py_ldb_get_config_basedn, METH_NOARGS, 1173 NULL }, 1174 { "connect", (PyCFunction)py_ldb_connect, METH_VARARGS|METH_KEYWORDS, 1175 "S.connect(url, flags=0, options=None) -> None\n" 1176 "Connect to a LDB URL." }, 1177 { "modify", (PyCFunction)py_ldb_modify, METH_VARARGS, 1178 "S.modify(message) -> None\n" 1179 "Modify an entry." }, 1180 { "add", (PyCFunction)py_ldb_add, METH_VARARGS, 1181 "S.add(message) -> None\n" 1182 "Add an entry." }, 1183 { "delete", (PyCFunction)py_ldb_delete, METH_VARARGS, 1184 "S.delete(dn) -> None\n" 1185 "Remove an entry." }, 1186 { "rename", (PyCFunction)py_ldb_rename, METH_VARARGS, 1187 "S.rename(old_dn, new_dn) -> None\n" 1188 "Rename an entry." }, 1189 { "search", (PyCFunction)py_ldb_search, METH_VARARGS|METH_KEYWORDS, 1190 "S.search(base=None, scope=None, expression=None, attrs=None, controls=None) -> msgs\n" 1191 "Search in a database.\n" 1192 "\n" 1193 ":param base: Optional base DN to search\n" 1194 ":param scope: Search scope (SCOPE_BASE, SCOPE_ONELEVEL or SCOPE_SUBTREE)\n" 1195 ":param expression: Optional search expression\n" 1196 ":param attrs: Attributes to return (defaults to all)\n" 1197 ":param controls: Optional list of controls\n" 1198 ":return: Iterator over Message objects\n" 1199 }, 1200 { "schema_attribute_remove", (PyCFunction)py_ldb_schema_attribute_remove, METH_VARARGS, 1201 NULL }, 1202 { "schema_attribute_add", (PyCFunction)py_ldb_schema_attribute_add, METH_VARARGS, 1203 NULL }, 1204 { "schema_format_value", (PyCFunction)py_ldb_schema_format_value, METH_VARARGS, 1205 NULL }, 1206 { "parse_ldif", (PyCFunction)py_ldb_parse_ldif, METH_VARARGS, 1207 "S.parse_ldif(ldif) -> iter(messages)\n" 1208 "Parse a string formatted using LDIF." }, 1209 { "write_ldif", (PyCFunction)py_ldb_write_ldif, METH_VARARGS, 1210 "S.write_ldif(message, changetype) -> ldif\n" 1211 "Print the message as a string formatted using LDIF." }, 1212 { "msg_diff", (PyCFunction)py_ldb_msg_diff, METH_VARARGS, 1213 "S.msg_diff(Message) -> Message\n" 1214 "Return an LDB Message of the difference between two Message objects." }, 1215 { "get_opaque", (PyCFunction)py_ldb_get_opaque, METH_VARARGS, 1216 "S.get_opaque(name) -> value\n" 1217 "Get an opaque value set on this LDB connection. \n" 1218 ":note: The returned value may not be useful in Python." 1219 }, 1220 { "set_opaque", (PyCFunction)py_ldb_set_opaque, METH_VARARGS, 1221 "S.set_opaque(name, value) -> None\n" 1222 "Set an opaque value on this LDB connection. \n" 1223 ":note: Passing incorrect values may cause crashes." }, 1224 { "modules", (PyCFunction)py_ldb_modules, METH_NOARGS, 1225 "S.modules() -> list\n" 1226 "Return the list of modules on this LDB connection " }, 1227 { NULL }, 1228}; 1229 1230PyObject *PyLdbModule_FromModule(struct ldb_module *mod) 1231{ 1232 PyLdbModuleObject *ret; 1233 1234 ret = (PyLdbModuleObject *)PyLdbModule.tp_alloc(&PyLdbModule, 0); 1235 if (ret == NULL) { 1236 PyErr_NoMemory(); 1237 return NULL; 1238 } 1239 ret->mem_ctx = talloc_new(NULL); 1240 ret->mod = talloc_reference(ret->mem_ctx, mod); 1241 return (PyObject *)ret; 1242} 1243 1244static PyObject *py_ldb_get_firstmodule(PyLdbObject *self, void *closure) 1245{ 1246 return PyLdbModule_FromModule(PyLdb_AsLdbContext(self)->modules); 1247} 1248 1249static PyGetSetDef py_ldb_getset[] = { 1250 { discard_const_p(char, "firstmodule"), (getter)py_ldb_get_firstmodule, NULL, NULL }, 1251 { NULL } 1252}; 1253 1254static int py_ldb_contains(PyLdbObject *self, PyObject *obj) 1255{ 1256 struct ldb_context *ldb_ctx = PyLdb_AsLdbContext(self); 1257 struct ldb_dn *dn; 1258 struct ldb_result *result; 1259 int ret; 1260 int count; 1261 1262 if (!PyObject_AsDn(ldb_ctx, obj, ldb_ctx, &dn)) 1263 return -1; 1264 1265 ret = ldb_search(ldb_ctx, ldb_ctx, &result, dn, LDB_SCOPE_BASE, NULL, NULL); 1266 if (ret != LDB_SUCCESS) { 1267 PyErr_SetLdbError(PyExc_LdbError, ret, ldb_ctx); 1268 return -1; 1269 } 1270 1271 count = result->count; 1272 1273 talloc_free(result); 1274 1275 return count; 1276} 1277 1278static PySequenceMethods py_ldb_seq = { 1279 .sq_contains = (objobjproc)py_ldb_contains, 1280}; 1281 1282PyObject *PyLdb_FromLdbContext(struct ldb_context *ldb_ctx) 1283{ 1284 PyLdbObject *ret; 1285 1286 ret = (PyLdbObject *)PyLdb.tp_alloc(&PyLdb, 0); 1287 if (ret == NULL) { 1288 PyErr_NoMemory(); 1289 return NULL; 1290 } 1291 ret->mem_ctx = talloc_new(NULL); 1292 ret->ldb_ctx = talloc_reference(ret->mem_ctx, ldb_ctx); 1293 return (PyObject *)ret; 1294} 1295 1296static void py_ldb_dealloc(PyLdbObject *self) 1297{ 1298 talloc_free(self->mem_ctx); 1299 self->ob_type->tp_free(self); 1300} 1301 1302PyTypeObject PyLdb = { 1303 .tp_name = "Ldb", 1304 .tp_methods = py_ldb_methods, 1305 .tp_repr = (reprfunc)py_ldb_repr, 1306 .tp_new = py_ldb_new, 1307 .tp_init = (initproc)py_ldb_init, 1308 .tp_dealloc = (destructor)py_ldb_dealloc, 1309 .tp_getset = py_ldb_getset, 1310 .tp_getattro = PyObject_GenericGetAttr, 1311 .tp_basicsize = sizeof(PyLdbObject), 1312 .tp_doc = "Connection to a LDB database.", 1313 .tp_as_sequence = &py_ldb_seq, 1314 .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, 1315}; 1316 1317static PyObject *py_ldb_module_repr(PyLdbModuleObject *self) 1318{ 1319 return PyString_FromFormat("<ldb module '%s'>", PyLdbModule_AsModule(self)->ops->name); 1320} 1321 1322static PyObject *py_ldb_module_str(PyLdbModuleObject *self) 1323{ 1324 return PyString_FromString(PyLdbModule_AsModule(self)->ops->name); 1325} 1326 1327static PyObject *py_ldb_module_start_transaction(PyLdbModuleObject *self) 1328{ 1329 PyLdbModule_AsModule(self)->ops->start_transaction(PyLdbModule_AsModule(self)); 1330 Py_RETURN_NONE; 1331} 1332 1333static PyObject *py_ldb_module_end_transaction(PyLdbModuleObject *self) 1334{ 1335 PyLdbModule_AsModule(self)->ops->end_transaction(PyLdbModule_AsModule(self)); 1336 Py_RETURN_NONE; 1337} 1338 1339static PyObject *py_ldb_module_del_transaction(PyLdbModuleObject *self) 1340{ 1341 PyLdbModule_AsModule(self)->ops->del_transaction(PyLdbModule_AsModule(self)); 1342 Py_RETURN_NONE; 1343} 1344 1345static PyObject *py_ldb_module_search(PyLdbModuleObject *self, PyObject *args, PyObject *kwargs) 1346{ 1347 PyObject *py_base, *py_tree, *py_attrs, *py_ret; 1348 int ret, scope; 1349 struct ldb_request *req; 1350 const char * const kwnames[] = { "base", "scope", "tree", "attrs", NULL }; 1351 struct ldb_module *mod; 1352 const char * const*attrs; 1353 1354 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "OiOO", 1355 discard_const_p(char *, kwnames), 1356 &py_base, &scope, &py_tree, &py_attrs)) 1357 return NULL; 1358 1359 mod = self->mod; 1360 1361 if (py_attrs == Py_None) { 1362 attrs = NULL; 1363 } else { 1364 attrs = PyList_AsStringList(NULL, py_attrs, "attrs"); 1365 if (attrs == NULL) 1366 return NULL; 1367 } 1368 1369 ret = ldb_build_search_req(&req, mod->ldb, NULL, PyLdbDn_AsDn(py_base), 1370 scope, NULL /* expr */, attrs, 1371 NULL /* controls */, NULL, NULL, NULL); 1372 1373 talloc_steal(req, attrs); 1374 1375 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb); 1376 1377 req->op.search.res = NULL; 1378 1379 ret = mod->ops->search(mod, req); 1380 1381 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb); 1382 1383 py_ret = PyLdbResult_FromResult(req->op.search.res); 1384 1385 talloc_free(req); 1386 1387 return py_ret; 1388} 1389 1390 1391static PyObject *py_ldb_module_add(PyLdbModuleObject *self, PyObject *args) 1392{ 1393 struct ldb_request *req; 1394 PyObject *py_message; 1395 int ret; 1396 struct ldb_module *mod; 1397 1398 if (!PyArg_ParseTuple(args, "O", &py_message)) 1399 return NULL; 1400 1401 req = talloc_zero(NULL, struct ldb_request); 1402 req->operation = LDB_ADD; 1403 req->op.add.message = PyLdbMessage_AsMessage(py_message); 1404 1405 mod = PyLdbModule_AsModule(self); 1406 ret = mod->ops->add(mod, req); 1407 1408 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb); 1409 1410 Py_RETURN_NONE; 1411} 1412 1413static PyObject *py_ldb_module_modify(PyLdbModuleObject *self, PyObject *args) 1414{ 1415 int ret; 1416 struct ldb_request *req; 1417 PyObject *py_message; 1418 struct ldb_module *mod; 1419 1420 if (!PyArg_ParseTuple(args, "O", &py_message)) 1421 return NULL; 1422 1423 req = talloc_zero(NULL, struct ldb_request); 1424 req->operation = LDB_MODIFY; 1425 req->op.mod.message = PyLdbMessage_AsMessage(py_message); 1426 1427 mod = PyLdbModule_AsModule(self); 1428 ret = mod->ops->modify(mod, req); 1429 1430 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, mod->ldb); 1431 1432 Py_RETURN_NONE; 1433} 1434 1435static PyObject *py_ldb_module_delete(PyLdbModuleObject *self, PyObject *args) 1436{ 1437 int ret; 1438 struct ldb_request *req; 1439 PyObject *py_dn; 1440 1441 if (!PyArg_ParseTuple(args, "O", &py_dn)) 1442 return NULL; 1443 1444 req = talloc_zero(NULL, struct ldb_request); 1445 req->operation = LDB_DELETE; 1446 req->op.del.dn = PyLdbDn_AsDn(py_dn); 1447 1448 ret = PyLdbModule_AsModule(self)->ops->del(PyLdbModule_AsModule(self), req); 1449 1450 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL); 1451 1452 Py_RETURN_NONE; 1453} 1454 1455static PyObject *py_ldb_module_rename(PyLdbModuleObject *self, PyObject *args) 1456{ 1457 int ret; 1458 struct ldb_request *req; 1459 PyObject *py_dn1, *py_dn2; 1460 1461 if (!PyArg_ParseTuple(args, "OO", &py_dn1, &py_dn2)) 1462 return NULL; 1463 1464 req = talloc_zero(NULL, struct ldb_request); 1465 1466 req->operation = LDB_RENAME; 1467 req->op.rename.olddn = PyLdbDn_AsDn(py_dn1); 1468 req->op.rename.newdn = PyLdbDn_AsDn(py_dn2); 1469 1470 ret = PyLdbModule_AsModule(self)->ops->rename(PyLdbModule_AsModule(self), req); 1471 1472 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL); 1473 1474 Py_RETURN_NONE; 1475} 1476 1477static PyMethodDef py_ldb_module_methods[] = { 1478 { "search", (PyCFunction)py_ldb_module_search, METH_VARARGS|METH_KEYWORDS, NULL }, 1479 { "add", (PyCFunction)py_ldb_module_add, METH_VARARGS, NULL }, 1480 { "modify", (PyCFunction)py_ldb_module_modify, METH_VARARGS, NULL }, 1481 { "rename", (PyCFunction)py_ldb_module_rename, METH_VARARGS, NULL }, 1482 { "delete", (PyCFunction)py_ldb_module_delete, METH_VARARGS, NULL }, 1483 { "start_transaction", (PyCFunction)py_ldb_module_start_transaction, METH_NOARGS, NULL }, 1484 { "end_transaction", (PyCFunction)py_ldb_module_end_transaction, METH_NOARGS, NULL }, 1485 { "del_transaction", (PyCFunction)py_ldb_module_del_transaction, METH_NOARGS, NULL }, 1486 { NULL }, 1487}; 1488 1489static void py_ldb_module_dealloc(PyLdbModuleObject *self) 1490{ 1491 talloc_free(self->mem_ctx); 1492 self->ob_type->tp_free(self); 1493} 1494 1495PyTypeObject PyLdbModule = { 1496 .tp_name = "LdbModule", 1497 .tp_methods = py_ldb_module_methods, 1498 .tp_repr = (reprfunc)py_ldb_module_repr, 1499 .tp_str = (reprfunc)py_ldb_module_str, 1500 .tp_basicsize = sizeof(PyLdbModuleObject), 1501 .tp_dealloc = (destructor)py_ldb_module_dealloc, 1502 .tp_flags = Py_TPFLAGS_DEFAULT, 1503}; 1504 1505 1506/** 1507 * Create a ldb_message_element from a Python object. 1508 * 1509 * This will accept any sequence objects that contains strings, or 1510 * a string object. 1511 * 1512 * A reference to set_obj will be borrowed. 1513 * 1514 * @param mem_ctx Memory context 1515 * @param set_obj Python object to convert 1516 * @param flags ldb_message_element flags to set 1517 * @param attr_name Name of the attribute 1518 * @return New ldb_message_element, allocated as child of mem_ctx 1519 */ 1520struct ldb_message_element *PyObject_AsMessageElement(TALLOC_CTX *mem_ctx, 1521 PyObject *set_obj, int flags, 1522 const char *attr_name) 1523{ 1524 struct ldb_message_element *me; 1525 1526 if (PyLdbMessageElement_Check(set_obj)) 1527 return talloc_reference(mem_ctx, 1528 PyLdbMessageElement_AsMessageElement(set_obj)); 1529 1530 me = talloc(mem_ctx, struct ldb_message_element); 1531 1532 me->name = talloc_strdup(me, attr_name); 1533 me->flags = flags; 1534 if (PyString_Check(set_obj)) { 1535 me->num_values = 1; 1536 me->values = talloc_array(me, struct ldb_val, me->num_values); 1537 me->values[0].length = PyString_Size(set_obj); 1538 me->values[0].data = talloc_memdup(me, 1539 (uint8_t *)PyString_AsString(set_obj), me->values[0].length); 1540 } else if (PySequence_Check(set_obj)) { 1541 int i; 1542 me->num_values = PySequence_Size(set_obj); 1543 me->values = talloc_array(me, struct ldb_val, me->num_values); 1544 for (i = 0; i < me->num_values; i++) { 1545 PyObject *obj = PySequence_GetItem(set_obj, i); 1546 1547 me->values[i].length = PyString_Size(obj); 1548 me->values[i].data = talloc_memdup(me, 1549 (uint8_t *)PyString_AsString(obj), me->values[i].length); 1550 } 1551 } else { 1552 talloc_free(me); 1553 me = NULL; 1554 } 1555 1556 return me; 1557} 1558 1559 1560static PyObject *ldb_msg_element_to_set(struct ldb_context *ldb_ctx, 1561 struct ldb_message_element *me) 1562{ 1563 int i; 1564 PyObject *result; 1565 1566 /* Python << 2.5 doesn't have PySet_New and PySet_Add. */ 1567 result = PyList_New(me->num_values); 1568 1569 for (i = 0; i < me->num_values; i++) { 1570 PyList_SetItem(result, i, 1571 PyObject_FromLdbValue(ldb_ctx, me, &me->values[i])); 1572 } 1573 1574 return result; 1575} 1576 1577static PyObject *py_ldb_msg_element_get(PyLdbMessageElementObject *self, PyObject *args) 1578{ 1579 int i; 1580 if (!PyArg_ParseTuple(args, "i", &i)) 1581 return NULL; 1582 if (i < 0 || i >= PyLdbMessageElement_AsMessageElement(self)->num_values) 1583 Py_RETURN_NONE; 1584 1585 return PyObject_FromLdbValue(NULL, PyLdbMessageElement_AsMessageElement(self), 1586 &(PyLdbMessageElement_AsMessageElement(self)->values[i])); 1587} 1588 1589static PyObject *py_ldb_msg_element_flags(PyLdbMessageElementObject *self, PyObject *args) 1590{ 1591 struct ldb_message_element *el; 1592 1593 el = PyLdbMessageElement_AsMessageElement(self); 1594 return PyInt_FromLong(el->flags); 1595} 1596 1597static PyObject *py_ldb_msg_element_set_flags(PyLdbMessageElementObject *self, PyObject *args) 1598{ 1599 int flags; 1600 struct ldb_message_element *el; 1601 if (!PyArg_ParseTuple(args, "i", &flags)) 1602 return NULL; 1603 1604 el = PyLdbMessageElement_AsMessageElement(self); 1605 el->flags = flags; 1606 Py_RETURN_NONE; 1607} 1608 1609static PyMethodDef py_ldb_msg_element_methods[] = { 1610 { "get", (PyCFunction)py_ldb_msg_element_get, METH_VARARGS, NULL }, 1611 { "set_flags", (PyCFunction)py_ldb_msg_element_set_flags, METH_VARARGS, NULL }, 1612 { "flags", (PyCFunction)py_ldb_msg_element_flags, METH_NOARGS, NULL }, 1613 { NULL }, 1614}; 1615 1616static Py_ssize_t py_ldb_msg_element_len(PyLdbMessageElementObject *self) 1617{ 1618 return PyLdbMessageElement_AsMessageElement(self)->num_values; 1619} 1620 1621static PyObject *py_ldb_msg_element_find(PyLdbMessageElementObject *self, Py_ssize_t idx) 1622{ 1623 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self); 1624 if (idx < 0 || idx >= el->num_values) { 1625 PyErr_SetString(PyExc_IndexError, "Out of range"); 1626 return NULL; 1627 } 1628 return PyString_FromStringAndSize((char *)el->values[idx].data, el->values[idx].length); 1629} 1630 1631static PySequenceMethods py_ldb_msg_element_seq = { 1632 .sq_length = (lenfunc)py_ldb_msg_element_len, 1633 .sq_item = (ssizeargfunc)py_ldb_msg_element_find, 1634}; 1635 1636static int py_ldb_msg_element_cmp(PyLdbMessageElementObject *self, PyLdbMessageElementObject *other) 1637{ 1638 return ldb_msg_element_compare(PyLdbMessageElement_AsMessageElement(self), 1639 PyLdbMessageElement_AsMessageElement(other)); 1640} 1641 1642static PyObject *py_ldb_msg_element_iter(PyLdbMessageElementObject *self) 1643{ 1644 return PyObject_GetIter(ldb_msg_element_to_set(NULL, PyLdbMessageElement_AsMessageElement(self))); 1645} 1646 1647PyObject *PyLdbMessageElement_FromMessageElement(struct ldb_message_element *el, TALLOC_CTX *mem_ctx) 1648{ 1649 PyLdbMessageElementObject *ret; 1650 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0); 1651 if (ret == NULL) { 1652 PyErr_NoMemory(); 1653 return NULL; 1654 } 1655 ret->mem_ctx = talloc_new(NULL); 1656 if (talloc_reference(ret->mem_ctx, mem_ctx) == NULL) { 1657 PyErr_NoMemory(); 1658 return NULL; 1659 } 1660 ret->el = el; 1661 return (PyObject *)ret; 1662} 1663 1664static PyObject *py_ldb_msg_element_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 1665{ 1666 PyObject *py_elements = NULL; 1667 struct ldb_message_element *el; 1668 int flags = 0; 1669 char *name = NULL; 1670 const char * const kwnames[] = { "elements", "flags", "name", NULL }; 1671 PyLdbMessageElementObject *ret; 1672 TALLOC_CTX *mem_ctx; 1673 1674 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Ois", 1675 discard_const_p(char *, kwnames), 1676 &py_elements, &flags, &name)) 1677 return NULL; 1678 1679 mem_ctx = talloc_new(NULL); 1680 if (mem_ctx == NULL) { 1681 PyErr_NoMemory(); 1682 return NULL; 1683 } 1684 1685 el = talloc_zero(mem_ctx, struct ldb_message_element); 1686 1687 if (py_elements != NULL) { 1688 int i; 1689 if (PyString_Check(py_elements)) { 1690 el->num_values = 1; 1691 el->values = talloc_array(el, struct ldb_val, 1); 1692 el->values[0].length = PyString_Size(py_elements); 1693 el->values[0].data = talloc_memdup(el, 1694 (uint8_t *)PyString_AsString(py_elements), el->values[0].length); 1695 } else if (PySequence_Check(py_elements)) { 1696 el->num_values = PySequence_Size(py_elements); 1697 el->values = talloc_array(el, struct ldb_val, el->num_values); 1698 for (i = 0; i < el->num_values; i++) { 1699 PyObject *item = PySequence_GetItem(py_elements, i); 1700 if (!PyString_Check(item)) { 1701 PyErr_Format(PyExc_TypeError, 1702 "Expected string as element %d in list", 1703 i); 1704 talloc_free(mem_ctx); 1705 return NULL; 1706 } 1707 el->values[i].length = PyString_Size(item); 1708 el->values[i].data = talloc_memdup(el, 1709 (uint8_t *)PyString_AsString(item), el->values[i].length); 1710 } 1711 } else { 1712 PyErr_SetString(PyExc_TypeError, 1713 "Expected string or list"); 1714 talloc_free(mem_ctx); 1715 return NULL; 1716 } 1717 } 1718 1719 el->flags = flags; 1720 el->name = talloc_strdup(el, name); 1721 1722 ret = (PyLdbMessageElementObject *)PyLdbMessageElement.tp_alloc(&PyLdbMessageElement, 0); 1723 if (ret == NULL) { 1724 PyErr_NoMemory(); 1725 talloc_free(mem_ctx); 1726 return NULL; 1727 } 1728 1729 ret->mem_ctx = mem_ctx; 1730 ret->el = el; 1731 return (PyObject *)ret; 1732} 1733 1734static PyObject *py_ldb_msg_element_repr(PyLdbMessageElementObject *self) 1735{ 1736 char *element_str = NULL; 1737 int i; 1738 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self); 1739 PyObject *ret; 1740 1741 for (i = 0; i < el->num_values; i++) { 1742 PyObject *o = py_ldb_msg_element_find(self, i); 1743 if (element_str == NULL) 1744 element_str = talloc_strdup(NULL, PyObject_REPR(o)); 1745 else 1746 element_str = talloc_asprintf_append(element_str, ",%s", PyObject_REPR(o)); 1747 } 1748 1749 ret = PyString_FromFormat("MessageElement([%s])", element_str); 1750 1751 talloc_free(element_str); 1752 1753 return ret; 1754} 1755 1756static PyObject *py_ldb_msg_element_str(PyLdbMessageElementObject *self) 1757{ 1758 struct ldb_message_element *el = PyLdbMessageElement_AsMessageElement(self); 1759 1760 if (el->num_values == 1) 1761 return PyString_FromStringAndSize((char *)el->values[0].data, el->values[0].length); 1762 else 1763 Py_RETURN_NONE; 1764} 1765 1766static void py_ldb_msg_element_dealloc(PyLdbMessageElementObject *self) 1767{ 1768 talloc_free(self->mem_ctx); 1769 self->ob_type->tp_free(self); 1770} 1771 1772PyTypeObject PyLdbMessageElement = { 1773 .tp_name = "MessageElement", 1774 .tp_basicsize = sizeof(PyLdbMessageElementObject), 1775 .tp_dealloc = (destructor)py_ldb_msg_element_dealloc, 1776 .tp_repr = (reprfunc)py_ldb_msg_element_repr, 1777 .tp_str = (reprfunc)py_ldb_msg_element_str, 1778 .tp_methods = py_ldb_msg_element_methods, 1779 .tp_compare = (cmpfunc)py_ldb_msg_element_cmp, 1780 .tp_iter = (getiterfunc)py_ldb_msg_element_iter, 1781 .tp_as_sequence = &py_ldb_msg_element_seq, 1782 .tp_new = py_ldb_msg_element_new, 1783 .tp_flags = Py_TPFLAGS_DEFAULT, 1784}; 1785 1786static PyObject *py_ldb_msg_remove_attr(PyLdbMessageObject *self, PyObject *args) 1787{ 1788 char *name; 1789 if (!PyArg_ParseTuple(args, "s", &name)) 1790 return NULL; 1791 1792 ldb_msg_remove_attr(self->msg, name); 1793 1794 Py_RETURN_NONE; 1795} 1796 1797static PyObject *py_ldb_msg_keys(PyLdbMessageObject *self) 1798{ 1799 struct ldb_message *msg = PyLdbMessage_AsMessage(self); 1800 int i, j = 0; 1801 PyObject *obj = PyList_New(msg->num_elements+(msg->dn != NULL?1:0)); 1802 if (msg->dn != NULL) { 1803 PyList_SetItem(obj, j, PyString_FromString("dn")); 1804 j++; 1805 } 1806 for (i = 0; i < msg->num_elements; i++) { 1807 PyList_SetItem(obj, j, PyString_FromString(msg->elements[i].name)); 1808 j++; 1809 } 1810 return obj; 1811} 1812 1813static PyObject *py_ldb_msg_getitem_helper(PyLdbMessageObject *self, PyObject *py_name) 1814{ 1815 struct ldb_message_element *el; 1816 char *name; 1817 struct ldb_message *msg = PyLdbMessage_AsMessage(self); 1818 if (!PyString_Check(py_name)) { 1819 PyErr_SetNone(PyExc_TypeError); 1820 return NULL; 1821 } 1822 name = PyString_AsString(py_name); 1823 if (!strcmp(name, "dn")) 1824 return PyLdbDn_FromDn(msg->dn); 1825 el = ldb_msg_find_element(msg, name); 1826 if (el == NULL) { 1827 return NULL; 1828 } 1829 return (PyObject *)PyLdbMessageElement_FromMessageElement(el, msg); 1830} 1831 1832static PyObject *py_ldb_msg_getitem(PyLdbMessageObject *self, PyObject *py_name) 1833{ 1834 PyObject *ret = py_ldb_msg_getitem_helper(self, py_name); 1835 if (ret == NULL) { 1836 PyErr_SetString(PyExc_KeyError, "No such element"); 1837 return NULL; 1838 } 1839 return ret; 1840} 1841 1842static PyObject *py_ldb_msg_get(PyLdbMessageObject *self, PyObject *args) 1843{ 1844 PyObject *name, *ret; 1845 if (!PyArg_ParseTuple(args, "O", &name)) 1846 return NULL; 1847 1848 ret = py_ldb_msg_getitem_helper(self, name); 1849 if (ret == NULL) { 1850 if (PyErr_Occurred()) 1851 return NULL; 1852 Py_RETURN_NONE; 1853 } 1854 return ret; 1855} 1856 1857static PyObject *py_ldb_msg_items(PyLdbMessageObject *self) 1858{ 1859 struct ldb_message *msg = PyLdbMessage_AsMessage(self); 1860 int i, j; 1861 PyObject *l = PyList_New(msg->num_elements + (msg->dn == NULL?0:1)); 1862 j = 0; 1863 if (msg->dn != NULL) { 1864 PyList_SetItem(l, 0, Py_BuildValue("(sO)", "dn", PyLdbDn_FromDn(msg->dn))); 1865 j++; 1866 } 1867 for (i = 0; i < msg->num_elements; i++, j++) { 1868 PyList_SetItem(l, j, Py_BuildValue("(sO)", msg->elements[i].name, PyLdbMessageElement_FromMessageElement(&msg->elements[i], self->msg))); 1869 } 1870 return l; 1871} 1872 1873static PyMethodDef py_ldb_msg_methods[] = { 1874 { "keys", (PyCFunction)py_ldb_msg_keys, METH_NOARGS, NULL }, 1875 { "remove", (PyCFunction)py_ldb_msg_remove_attr, METH_VARARGS, NULL }, 1876 { "get", (PyCFunction)py_ldb_msg_get, METH_VARARGS, NULL }, 1877 { "items", (PyCFunction)py_ldb_msg_items, METH_NOARGS, NULL }, 1878 { NULL }, 1879}; 1880 1881static PyObject *py_ldb_msg_iter(PyLdbMessageObject *self) 1882{ 1883 PyObject *list, *iter; 1884 1885 list = py_ldb_msg_keys(self); 1886 iter = PyObject_GetIter(list); 1887 Py_DECREF(list); 1888 return iter; 1889} 1890 1891static int py_ldb_msg_setitem(PyLdbMessageObject *self, PyObject *name, PyObject *value) 1892{ 1893 char *attr_name; 1894 1895 if (!PyString_Check(name)) { 1896 PyErr_SetNone(PyExc_TypeError); 1897 return -1; 1898 } 1899 1900 attr_name = PyString_AsString(name); 1901 if (value == NULL) { 1902 /* delitem */ 1903 ldb_msg_remove_attr(self->msg, attr_name); 1904 } else { 1905 struct ldb_message_element *el = PyObject_AsMessageElement(self->msg, 1906 value, 0, attr_name); 1907 if (el == NULL) 1908 return -1; 1909 ldb_msg_remove_attr(PyLdbMessage_AsMessage(self), attr_name); 1910 ldb_msg_add(PyLdbMessage_AsMessage(self), el, el->flags); 1911 } 1912 return 0; 1913} 1914 1915static Py_ssize_t py_ldb_msg_length(PyLdbMessageObject *self) 1916{ 1917 return PyLdbMessage_AsMessage(self)->num_elements; 1918} 1919 1920static PyMappingMethods py_ldb_msg_mapping = { 1921 .mp_length = (lenfunc)py_ldb_msg_length, 1922 .mp_subscript = (binaryfunc)py_ldb_msg_getitem, 1923 .mp_ass_subscript = (objobjargproc)py_ldb_msg_setitem, 1924}; 1925 1926static PyObject *py_ldb_msg_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) 1927{ 1928 const char * const kwnames[] = { "dn", NULL }; 1929 struct ldb_message *ret; 1930 TALLOC_CTX *mem_ctx; 1931 PyObject *pydn = NULL; 1932 PyLdbMessageObject *py_ret; 1933 1934 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", 1935 discard_const_p(char *, kwnames), 1936 &pydn)) 1937 return NULL; 1938 1939 mem_ctx = talloc_new(NULL); 1940 if (mem_ctx == NULL) { 1941 PyErr_NoMemory(); 1942 return NULL; 1943 } 1944 1945 ret = ldb_msg_new(mem_ctx); 1946 if (ret == NULL) { 1947 talloc_free(mem_ctx); 1948 PyErr_NoMemory(); 1949 return NULL; 1950 } 1951 1952 if (pydn != NULL) { 1953 struct ldb_dn *dn; 1954 if (!PyObject_AsDn(NULL, pydn, NULL, &dn)) { 1955 talloc_free(mem_ctx); 1956 return NULL; 1957 } 1958 ret->dn = talloc_reference(ret, dn); 1959 } 1960 1961 py_ret = (PyLdbMessageObject *)type->tp_alloc(type, 0); 1962 if (py_ret == NULL) { 1963 PyErr_NoMemory(); 1964 talloc_free(mem_ctx); 1965 return NULL; 1966 } 1967 1968 py_ret->mem_ctx = mem_ctx; 1969 py_ret->msg = ret; 1970 return (PyObject *)py_ret; 1971} 1972 1973PyObject *PyLdbMessage_FromMessage(struct ldb_message *msg) 1974{ 1975 PyLdbMessageObject *ret; 1976 1977 ret = (PyLdbMessageObject *)PyLdbMessage.tp_alloc(&PyLdbMessage, 0); 1978 if (ret == NULL) { 1979 PyErr_NoMemory(); 1980 return NULL; 1981 } 1982 ret->mem_ctx = talloc_new(NULL); 1983 ret->msg = talloc_reference(ret->mem_ctx, msg); 1984 return (PyObject *)ret; 1985} 1986 1987static PyObject *py_ldb_msg_get_dn(PyLdbMessageObject *self, void *closure) 1988{ 1989 struct ldb_message *msg = PyLdbMessage_AsMessage(self); 1990 return PyLdbDn_FromDn(msg->dn); 1991} 1992 1993static int py_ldb_msg_set_dn(PyLdbMessageObject *self, PyObject *value, void *closure) 1994{ 1995 struct ldb_message *msg = PyLdbMessage_AsMessage(self); 1996 if (!PyLdbDn_Check(value)) { 1997 PyErr_SetNone(PyExc_TypeError); 1998 return -1; 1999 } 2000 2001 msg->dn = talloc_reference(msg, PyLdbDn_AsDn(value)); 2002 return 0; 2003} 2004 2005static PyGetSetDef py_ldb_msg_getset[] = { 2006 { discard_const_p(char, "dn"), (getter)py_ldb_msg_get_dn, (setter)py_ldb_msg_set_dn, NULL }, 2007 { NULL } 2008}; 2009 2010static PyObject *py_ldb_msg_repr(PyLdbMessageObject *self) 2011{ 2012 PyObject *dict = PyDict_New(), *ret; 2013 if (PyDict_Update(dict, (PyObject *)self) != 0) 2014 return NULL; 2015 ret = PyString_FromFormat("Message(%s)", PyObject_REPR(dict)); 2016 Py_DECREF(dict); 2017 return ret; 2018} 2019 2020static void py_ldb_msg_dealloc(PyLdbMessageObject *self) 2021{ 2022 talloc_free(self->mem_ctx); 2023 self->ob_type->tp_free(self); 2024} 2025 2026PyTypeObject PyLdbMessage = { 2027 .tp_name = "Message", 2028 .tp_methods = py_ldb_msg_methods, 2029 .tp_getset = py_ldb_msg_getset, 2030 .tp_as_mapping = &py_ldb_msg_mapping, 2031 .tp_basicsize = sizeof(PyLdbMessageObject), 2032 .tp_dealloc = (destructor)py_ldb_msg_dealloc, 2033 .tp_new = py_ldb_msg_new, 2034 .tp_repr = (reprfunc)py_ldb_msg_repr, 2035 .tp_flags = Py_TPFLAGS_DEFAULT, 2036 .tp_iter = (getiterfunc)py_ldb_msg_iter, 2037}; 2038 2039PyObject *PyLdbTree_FromTree(struct ldb_parse_tree *tree) 2040{ 2041 PyLdbTreeObject *ret; 2042 2043 ret = (PyLdbTreeObject *)PyLdbTree.tp_alloc(&PyLdbTree, 0); 2044 if (ret == NULL) { 2045 PyErr_NoMemory(); 2046 return NULL; 2047 } 2048 2049 ret->mem_ctx = talloc_new(NULL); 2050 ret->tree = talloc_reference(ret->mem_ctx, tree); 2051 return (PyObject *)ret; 2052} 2053 2054static void py_ldb_tree_dealloc(PyLdbTreeObject *self) 2055{ 2056 talloc_free(self->mem_ctx); 2057 self->ob_type->tp_free(self); 2058} 2059 2060PyTypeObject PyLdbTree = { 2061 .tp_name = "Tree", 2062 .tp_basicsize = sizeof(PyLdbTreeObject), 2063 .tp_dealloc = (destructor)py_ldb_tree_dealloc, 2064 .tp_flags = Py_TPFLAGS_DEFAULT, 2065}; 2066 2067/* Ldb_module */ 2068static int py_module_search(struct ldb_module *mod, struct ldb_request *req) 2069{ 2070 PyObject *py_ldb = (PyObject *)mod->private_data; 2071 PyObject *py_result, *py_base, *py_attrs, *py_tree; 2072 2073 py_base = PyLdbDn_FromDn(req->op.search.base); 2074 2075 if (py_base == NULL) 2076 return LDB_ERR_OPERATIONS_ERROR; 2077 2078 py_tree = PyLdbTree_FromTree(req->op.search.tree); 2079 2080 if (py_tree == NULL) 2081 return LDB_ERR_OPERATIONS_ERROR; 2082 2083 if (req->op.search.attrs == NULL) { 2084 py_attrs = Py_None; 2085 } else { 2086 int i, len; 2087 for (len = 0; req->op.search.attrs[len]; len++); 2088 py_attrs = PyList_New(len); 2089 for (i = 0; i < len; i++) 2090 PyList_SetItem(py_attrs, i, PyString_FromString(req->op.search.attrs[i])); 2091 } 2092 2093 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "search"), 2094 discard_const_p(char, "OiOO"), 2095 py_base, req->op.search.scope, py_tree, py_attrs); 2096 2097 Py_DECREF(py_attrs); 2098 Py_DECREF(py_tree); 2099 Py_DECREF(py_base); 2100 2101 if (py_result == NULL) { 2102 return LDB_ERR_PYTHON_EXCEPTION; 2103 } 2104 2105 req->op.search.res = PyLdbResult_AsResult(NULL, py_result); 2106 if (req->op.search.res == NULL) { 2107 return LDB_ERR_PYTHON_EXCEPTION; 2108 } 2109 2110 Py_DECREF(py_result); 2111 2112 return LDB_SUCCESS; 2113} 2114 2115static int py_module_add(struct ldb_module *mod, struct ldb_request *req) 2116{ 2117 PyObject *py_ldb = (PyObject *)mod->private_data; 2118 PyObject *py_result, *py_msg; 2119 2120 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.add.message)); 2121 2122 if (py_msg == NULL) { 2123 return LDB_ERR_OPERATIONS_ERROR; 2124 } 2125 2126 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "add"), 2127 discard_const_p(char, "O"), 2128 py_msg); 2129 2130 Py_DECREF(py_msg); 2131 2132 if (py_result == NULL) { 2133 return LDB_ERR_PYTHON_EXCEPTION; 2134 } 2135 2136 Py_DECREF(py_result); 2137 2138 return LDB_SUCCESS; 2139} 2140 2141static int py_module_modify(struct ldb_module *mod, struct ldb_request *req) 2142{ 2143 PyObject *py_ldb = (PyObject *)mod->private_data; 2144 PyObject *py_result, *py_msg; 2145 2146 py_msg = PyLdbMessage_FromMessage(discard_const_p(struct ldb_message, req->op.mod.message)); 2147 2148 if (py_msg == NULL) { 2149 return LDB_ERR_OPERATIONS_ERROR; 2150 } 2151 2152 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "modify"), 2153 discard_const_p(char, "O"), 2154 py_msg); 2155 2156 Py_DECREF(py_msg); 2157 2158 if (py_result == NULL) { 2159 return LDB_ERR_PYTHON_EXCEPTION; 2160 } 2161 2162 Py_DECREF(py_result); 2163 2164 return LDB_SUCCESS; 2165} 2166 2167static int py_module_del(struct ldb_module *mod, struct ldb_request *req) 2168{ 2169 PyObject *py_ldb = (PyObject *)mod->private_data; 2170 PyObject *py_result, *py_dn; 2171 2172 py_dn = PyLdbDn_FromDn(req->op.del.dn); 2173 2174 if (py_dn == NULL) 2175 return LDB_ERR_OPERATIONS_ERROR; 2176 2177 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "delete"), 2178 discard_const_p(char, "O"), 2179 py_dn); 2180 2181 if (py_result == NULL) { 2182 return LDB_ERR_PYTHON_EXCEPTION; 2183 } 2184 2185 Py_DECREF(py_result); 2186 2187 return LDB_SUCCESS; 2188} 2189 2190static int py_module_rename(struct ldb_module *mod, struct ldb_request *req) 2191{ 2192 PyObject *py_ldb = (PyObject *)mod->private_data; 2193 PyObject *py_result, *py_olddn, *py_newdn; 2194 2195 py_olddn = PyLdbDn_FromDn(req->op.rename.olddn); 2196 2197 if (py_olddn == NULL) 2198 return LDB_ERR_OPERATIONS_ERROR; 2199 2200 py_newdn = PyLdbDn_FromDn(req->op.rename.newdn); 2201 2202 if (py_newdn == NULL) 2203 return LDB_ERR_OPERATIONS_ERROR; 2204 2205 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "rename"), 2206 discard_const_p(char, "OO"), 2207 py_olddn, py_newdn); 2208 2209 Py_DECREF(py_olddn); 2210 Py_DECREF(py_newdn); 2211 2212 if (py_result == NULL) { 2213 return LDB_ERR_PYTHON_EXCEPTION; 2214 } 2215 2216 Py_DECREF(py_result); 2217 2218 return LDB_SUCCESS; 2219} 2220 2221static int py_module_request(struct ldb_module *mod, struct ldb_request *req) 2222{ 2223 PyObject *py_ldb = (PyObject *)mod->private_data; 2224 PyObject *py_result; 2225 2226 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "request"), 2227 discard_const_p(char, "")); 2228 2229 return LDB_ERR_OPERATIONS_ERROR; 2230} 2231 2232static int py_module_extended(struct ldb_module *mod, struct ldb_request *req) 2233{ 2234 PyObject *py_ldb = (PyObject *)mod->private_data; 2235 PyObject *py_result; 2236 2237 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "extended"), 2238 discard_const_p(char, "")); 2239 2240 return LDB_ERR_OPERATIONS_ERROR; 2241} 2242 2243static int py_module_start_transaction(struct ldb_module *mod) 2244{ 2245 PyObject *py_ldb = (PyObject *)mod->private_data; 2246 PyObject *py_result; 2247 2248 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "start_transaction"), 2249 discard_const_p(char, "")); 2250 2251 if (py_result == NULL) { 2252 return LDB_ERR_PYTHON_EXCEPTION; 2253 } 2254 2255 Py_DECREF(py_result); 2256 2257 return LDB_SUCCESS; 2258} 2259 2260static int py_module_end_transaction(struct ldb_module *mod) 2261{ 2262 PyObject *py_ldb = (PyObject *)mod->private_data; 2263 PyObject *py_result; 2264 2265 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "end_transaction"), 2266 discard_const_p(char, "")); 2267 2268 if (py_result == NULL) { 2269 return LDB_ERR_PYTHON_EXCEPTION; 2270 } 2271 2272 Py_DECREF(py_result); 2273 2274 return LDB_SUCCESS; 2275} 2276 2277static int py_module_del_transaction(struct ldb_module *mod) 2278{ 2279 PyObject *py_ldb = (PyObject *)mod->private_data; 2280 PyObject *py_result; 2281 2282 py_result = PyObject_CallMethod(py_ldb, discard_const_p(char, "del_transaction"), 2283 discard_const_p(char, "")); 2284 2285 if (py_result == NULL) { 2286 return LDB_ERR_PYTHON_EXCEPTION; 2287 } 2288 2289 Py_DECREF(py_result); 2290 2291 return LDB_SUCCESS; 2292} 2293 2294static int py_module_destructor(struct ldb_module *mod) 2295{ 2296 Py_DECREF((PyObject *)mod->private_data); 2297 return 0; 2298} 2299 2300static int py_module_init(struct ldb_module *mod) 2301{ 2302 PyObject *py_class = (PyObject *)mod->ops->private_data; 2303 PyObject *py_result, *py_next, *py_ldb; 2304 2305 py_ldb = PyLdb_FromLdbContext(mod->ldb); 2306 2307 if (py_ldb == NULL) 2308 return LDB_ERR_OPERATIONS_ERROR; 2309 2310 py_next = PyLdbModule_FromModule(mod->next); 2311 2312 if (py_next == NULL) 2313 return LDB_ERR_OPERATIONS_ERROR; 2314 2315 py_result = PyObject_CallFunction(py_class, discard_const_p(char, "OO"), 2316 py_ldb, py_next); 2317 2318 if (py_result == NULL) { 2319 return LDB_ERR_PYTHON_EXCEPTION; 2320 } 2321 2322 mod->private_data = py_result; 2323 2324 talloc_set_destructor(mod, py_module_destructor); 2325 2326 return ldb_next_init(mod); 2327} 2328 2329static PyObject *py_register_module(PyObject *module, PyObject *args) 2330{ 2331 int ret; 2332 struct ldb_module_ops *ops; 2333 PyObject *input; 2334 2335 if (!PyArg_ParseTuple(args, "O", &input)) 2336 return NULL; 2337 2338 ops = talloc_zero(talloc_autofree_context(), struct ldb_module_ops); 2339 if (ops == NULL) { 2340 PyErr_NoMemory(); 2341 return NULL; 2342 } 2343 2344 ops->name = talloc_strdup(ops, PyString_AsString(PyObject_GetAttrString(input, discard_const_p(char, "name")))); 2345 2346 Py_INCREF(input); 2347 ops->private_data = input; 2348 ops->init_context = py_module_init; 2349 ops->search = py_module_search; 2350 ops->add = py_module_add; 2351 ops->modify = py_module_modify; 2352 ops->del = py_module_del; 2353 ops->rename = py_module_rename; 2354 ops->request = py_module_request; 2355 ops->extended = py_module_extended; 2356 ops->start_transaction = py_module_start_transaction; 2357 ops->end_transaction = py_module_end_transaction; 2358 ops->del_transaction = py_module_del_transaction; 2359 2360 ret = ldb_register_module(ops); 2361 2362 PyErr_LDB_ERROR_IS_ERR_RAISE(PyExc_LdbError, ret, NULL); 2363 2364 Py_RETURN_NONE; 2365} 2366 2367static PyObject *py_timestring(PyObject *module, PyObject *args) 2368{ 2369 time_t t; 2370 char *tresult; 2371 PyObject *ret; 2372 if (!PyArg_ParseTuple(args, "L", &t)) 2373 return NULL; 2374 tresult = ldb_timestring(NULL, t); 2375 ret = PyString_FromString(tresult); 2376 talloc_free(tresult); 2377 return ret; 2378} 2379 2380static PyObject *py_string_to_time(PyObject *module, PyObject *args) 2381{ 2382 char *str; 2383 if (!PyArg_ParseTuple(args, "s", &str)) 2384 return NULL; 2385 2386 return PyInt_FromLong(ldb_string_to_time(str)); 2387} 2388 2389static PyObject *py_valid_attr_name(PyObject *self, PyObject *args) 2390{ 2391 char *name; 2392 if (!PyArg_ParseTuple(args, "s", &name)) 2393 return NULL; 2394 return PyBool_FromLong(ldb_valid_attr_name(name)); 2395} 2396 2397static PyMethodDef py_ldb_global_methods[] = { 2398 { "register_module", py_register_module, METH_VARARGS, 2399 "S.register_module(module) -> None\n" 2400 "Register a LDB module."}, 2401 { "timestring", py_timestring, METH_VARARGS, 2402 "S.timestring(int) -> string\n" 2403 "Generate a LDAP time string from a UNIX timestamp" }, 2404 { "string_to_time", py_string_to_time, METH_VARARGS, 2405 "S.string_to_time(string) -> int\n" 2406 "Parse a LDAP time string into a UNIX timestamp." }, 2407 { "valid_attr_name", py_valid_attr_name, METH_VARARGS, 2408 "S.valid_attr_name(name) -> bool\n" 2409 "Check whether the supplied name is a valid attribute name." }, 2410 { "open", (PyCFunction)py_ldb_new, METH_VARARGS|METH_KEYWORDS, 2411 NULL }, 2412 { NULL } 2413}; 2414 2415void initldb(void) 2416{ 2417 PyObject *m; 2418 2419 if (PyType_Ready(&PyLdbDn) < 0) 2420 return; 2421 2422 if (PyType_Ready(&PyLdbMessage) < 0) 2423 return; 2424 2425 if (PyType_Ready(&PyLdbMessageElement) < 0) 2426 return; 2427 2428 if (PyType_Ready(&PyLdb) < 0) 2429 return; 2430 2431 if (PyType_Ready(&PyLdbModule) < 0) 2432 return; 2433 2434 if (PyType_Ready(&PyLdbTree) < 0) 2435 return; 2436 2437 m = Py_InitModule3("ldb", py_ldb_global_methods, 2438 "An interface to LDB, a LDAP-like API that can either to talk an embedded database (TDB-based) or a standards-compliant LDAP server."); 2439 if (m == NULL) 2440 return; 2441 2442 PyModule_AddObject(m, "SCOPE_DEFAULT", PyInt_FromLong(LDB_SCOPE_DEFAULT)); 2443 PyModule_AddObject(m, "SCOPE_BASE", PyInt_FromLong(LDB_SCOPE_BASE)); 2444 PyModule_AddObject(m, "SCOPE_ONELEVEL", PyInt_FromLong(LDB_SCOPE_ONELEVEL)); 2445 PyModule_AddObject(m, "SCOPE_SUBTREE", PyInt_FromLong(LDB_SCOPE_SUBTREE)); 2446 2447 PyModule_AddObject(m, "CHANGETYPE_NONE", PyInt_FromLong(LDB_CHANGETYPE_NONE)); 2448 PyModule_AddObject(m, "CHANGETYPE_ADD", PyInt_FromLong(LDB_CHANGETYPE_ADD)); 2449 PyModule_AddObject(m, "CHANGETYPE_DELETE", PyInt_FromLong(LDB_CHANGETYPE_DELETE)); 2450 PyModule_AddObject(m, "CHANGETYPE_MODIFY", PyInt_FromLong(LDB_CHANGETYPE_MODIFY)); 2451 2452 PyModule_AddObject(m, "FLAG_MOD_ADD", PyInt_FromLong(LDB_FLAG_MOD_ADD)); 2453 PyModule_AddObject(m, "FLAG_MOD_REPLACE", PyInt_FromLong(LDB_FLAG_MOD_REPLACE)); 2454 PyModule_AddObject(m, "FLAG_MOD_DELETE", PyInt_FromLong(LDB_FLAG_MOD_DELETE)); 2455 2456 PyModule_AddObject(m, "SUCCESS", PyInt_FromLong(LDB_SUCCESS)); 2457 PyModule_AddObject(m, "ERR_OPERATIONS_ERROR", PyInt_FromLong(LDB_ERR_OPERATIONS_ERROR)); 2458 PyModule_AddObject(m, "ERR_PROTOCOL_ERROR", PyInt_FromLong(LDB_ERR_PROTOCOL_ERROR)); 2459 PyModule_AddObject(m, "ERR_TIME_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_TIME_LIMIT_EXCEEDED)); 2460 PyModule_AddObject(m, "ERR_SIZE_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_SIZE_LIMIT_EXCEEDED)); 2461 PyModule_AddObject(m, "ERR_COMPARE_FALSE", PyInt_FromLong(LDB_ERR_COMPARE_FALSE)); 2462 PyModule_AddObject(m, "ERR_COMPARE_TRUE", PyInt_FromLong(LDB_ERR_COMPARE_TRUE)); 2463 PyModule_AddObject(m, "ERR_AUTH_METHOD_NOT_SUPPORTED", PyInt_FromLong(LDB_ERR_AUTH_METHOD_NOT_SUPPORTED)); 2464 PyModule_AddObject(m, "ERR_STRONG_AUTH_REQUIRED", PyInt_FromLong(LDB_ERR_STRONG_AUTH_REQUIRED)); 2465 PyModule_AddObject(m, "ERR_REFERRAL", PyInt_FromLong(LDB_ERR_REFERRAL)); 2466 PyModule_AddObject(m, "ERR_ADMIN_LIMIT_EXCEEDED", PyInt_FromLong(LDB_ERR_ADMIN_LIMIT_EXCEEDED)); 2467 PyModule_AddObject(m, "ERR_UNSUPPORTED_CRITICAL_EXTENSION", PyInt_FromLong(LDB_ERR_UNSUPPORTED_CRITICAL_EXTENSION)); 2468 PyModule_AddObject(m, "ERR_CONFIDENTIALITY_REQUIRED", PyInt_FromLong(LDB_ERR_CONFIDENTIALITY_REQUIRED)); 2469 PyModule_AddObject(m, "ERR_SASL_BIND_IN_PROGRESS", PyInt_FromLong(LDB_ERR_SASL_BIND_IN_PROGRESS)); 2470 PyModule_AddObject(m, "ERR_NO_SUCH_ATTRIBUTE", PyInt_FromLong(LDB_ERR_NO_SUCH_ATTRIBUTE)); 2471 PyModule_AddObject(m, "ERR_UNDEFINED_ATTRIBUTE_TYPE", PyInt_FromLong(LDB_ERR_UNDEFINED_ATTRIBUTE_TYPE)); 2472 PyModule_AddObject(m, "ERR_INAPPROPRIATE_MATCHING", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_MATCHING)); 2473 PyModule_AddObject(m, "ERR_CONSTRAINT_VIOLATION", PyInt_FromLong(LDB_ERR_CONSTRAINT_VIOLATION)); 2474 PyModule_AddObject(m, "ERR_ATTRIBUTE_OR_VALUE_EXISTS", PyInt_FromLong(LDB_ERR_ATTRIBUTE_OR_VALUE_EXISTS)); 2475 PyModule_AddObject(m, "ERR_INVALID_ATTRIBUTE_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_ATTRIBUTE_SYNTAX)); 2476 PyModule_AddObject(m, "ERR_NO_SUCH_OBJECT", PyInt_FromLong(LDB_ERR_NO_SUCH_OBJECT)); 2477 PyModule_AddObject(m, "ERR_ALIAS_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_PROBLEM)); 2478 PyModule_AddObject(m, "ERR_INVALID_DN_SYNTAX", PyInt_FromLong(LDB_ERR_INVALID_DN_SYNTAX)); 2479 PyModule_AddObject(m, "ERR_ALIAS_DEREFERINCING_PROBLEM", PyInt_FromLong(LDB_ERR_ALIAS_DEREFERENCING_PROBLEM)); 2480 PyModule_AddObject(m, "ERR_INAPPROPRIATE_AUTHENTICATION", PyInt_FromLong(LDB_ERR_INAPPROPRIATE_AUTHENTICATION)); 2481 PyModule_AddObject(m, "ERR_INVALID_CREDENTIALS", PyInt_FromLong(LDB_ERR_INVALID_CREDENTIALS)); 2482 PyModule_AddObject(m, "ERR_INSUFFICIENT_ACCESS_RIGHTS", PyInt_FromLong(LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS)); 2483 PyModule_AddObject(m, "ERR_BUSY", PyInt_FromLong(LDB_ERR_BUSY)); 2484 PyModule_AddObject(m, "ERR_UNAVAILABLE", PyInt_FromLong(LDB_ERR_UNAVAILABLE)); 2485 PyModule_AddObject(m, "ERR_UNWILLING_TO_PERFORM", PyInt_FromLong(LDB_ERR_UNWILLING_TO_PERFORM)); 2486 PyModule_AddObject(m, "ERR_LOOP_DETECT", PyInt_FromLong(LDB_ERR_LOOP_DETECT)); 2487 PyModule_AddObject(m, "ERR_NAMING_VIOLATION", PyInt_FromLong(LDB_ERR_NAMING_VIOLATION)); 2488 PyModule_AddObject(m, "ERR_OBJECT_CLASS_VIOLATION", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_VIOLATION)); 2489 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_NON_LEAF", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_NON_LEAF)); 2490 PyModule_AddObject(m, "ERR_NOT_ALLOWED_ON_RDN", PyInt_FromLong(LDB_ERR_NOT_ALLOWED_ON_RDN)); 2491 PyModule_AddObject(m, "ERR_ENTRY_ALREADY_EXISTS", PyInt_FromLong(LDB_ERR_ENTRY_ALREADY_EXISTS)); 2492 PyModule_AddObject(m, "ERR_OBJECT_CLASS_MODS_PROHIBITED", PyInt_FromLong(LDB_ERR_OBJECT_CLASS_MODS_PROHIBITED)); 2493 PyModule_AddObject(m, "ERR_AFFECTS_MULTIPLE_DSAS", PyInt_FromLong(LDB_ERR_AFFECTS_MULTIPLE_DSAS)); 2494 PyModule_AddObject(m, "ERR_OTHER", PyInt_FromLong(LDB_ERR_OTHER)); 2495 2496 PyModule_AddObject(m, "FLG_RDONLY", PyInt_FromLong(LDB_FLG_RDONLY)); 2497 PyModule_AddObject(m, "FLG_NOSYNC", PyInt_FromLong(LDB_FLG_NOSYNC)); 2498 PyModule_AddObject(m, "FLG_RECONNECT", PyInt_FromLong(LDB_FLG_RECONNECT)); 2499 PyModule_AddObject(m, "FLG_NOMMAP", PyInt_FromLong(LDB_FLG_NOMMAP)); 2500 2501 2502 PyModule_AddObject(m, "__docformat__", PyString_FromString("restructuredText")); 2503 2504 PyExc_LdbError = PyErr_NewException(discard_const_p(char, "_ldb.LdbError"), NULL, NULL); 2505 PyModule_AddObject(m, "LdbError", PyExc_LdbError); 2506 2507 Py_INCREF(&PyLdb); 2508 Py_INCREF(&PyLdbDn); 2509 Py_INCREF(&PyLdbModule); 2510 Py_INCREF(&PyLdbMessage); 2511 Py_INCREF(&PyLdbMessageElement); 2512 Py_INCREF(&PyLdbTree); 2513 2514 PyModule_AddObject(m, "Ldb", (PyObject *)&PyLdb); 2515 PyModule_AddObject(m, "Dn", (PyObject *)&PyLdbDn); 2516 PyModule_AddObject(m, "Message", (PyObject *)&PyLdbMessage); 2517 PyModule_AddObject(m, "MessageElement", (PyObject *)&PyLdbMessageElement); 2518 PyModule_AddObject(m, "Module", (PyObject *)&PyLdbModule); 2519 PyModule_AddObject(m, "Tree", (PyObject *)&PyLdbTree); 2520} 2521