|
| 1 | +/* See http://www.python-ldap.org/ for details. |
| 2 | + * $Id: functions.c,v 1.27 2009/08/17 05:00:57 leonard Exp $ */ |
| 3 | + |
| 4 | +#include "common.h" |
| 5 | +#include "functions.h" |
| 6 | +#include "LDAPObject.h" |
| 7 | +#include "berval.h" |
| 8 | +#include "errors.h" |
| 9 | +#include "options.h" |
| 10 | + |
| 11 | +/* ldap_initialize */ |
| 12 | + |
| 13 | +static PyObject* |
| 14 | +l_ldap_initialize(PyObject* unused, PyObject *args) |
| 15 | +{ |
| 16 | + char *uri; |
| 17 | + LDAP *ld = NULL; |
| 18 | + int ret; |
| 19 | + |
| 20 | + if (!PyArg_ParseTuple(args, "s", &uri)) |
| 21 | + return NULL; |
| 22 | + |
| 23 | + Py_BEGIN_ALLOW_THREADS |
| 24 | + ret = ldap_initialize(&ld, uri); |
| 25 | + Py_END_ALLOW_THREADS |
| 26 | + if (ret != LDAP_SUCCESS) |
| 27 | + return LDAPerror(ld, "ldap_initialize"); |
| 28 | + return (PyObject*)newLDAPObject(ld); |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +/* ldap_str2dn */ |
| 33 | + |
| 34 | +static PyObject* |
| 35 | +l_ldap_str2dn( PyObject* unused, PyObject *args ) |
| 36 | +{ |
| 37 | + struct berval str; |
| 38 | + LDAPDN dn; |
| 39 | + int flags = 0; |
| 40 | + PyObject *result = NULL, *tmp; |
| 41 | + int res, i, j; |
| 42 | + Py_ssize_t str_len; |
| 43 | + |
| 44 | + /* |
| 45 | + * From a DN string such as "a=b,c=d;e=f", build |
| 46 | + * a list-equivalent of AVA structures; namely: |
| 47 | + * ((('a','b',1),('c','d',1)),(('e','f',1),)) |
| 48 | + * The integers are a bit combination of the AVA_* flags |
| 49 | + */ |
| 50 | + if (!PyArg_ParseTuple( args, "z#|i:str2dn", |
| 51 | + &str.bv_val, &str_len, &flags )) |
| 52 | + return NULL; |
| 53 | + str.bv_len = (ber_len_t) str_len; |
| 54 | + |
| 55 | + res = ldap_bv2dn(&str, &dn, flags); |
| 56 | + if (res != LDAP_SUCCESS) |
| 57 | + return LDAPerr(res); |
| 58 | + |
| 59 | + tmp = PyList_New(0); |
| 60 | + if (!tmp) |
| 61 | + goto failed; |
| 62 | + |
| 63 | + for (i = 0; dn[i]; i++) { |
| 64 | + LDAPRDN rdn; |
| 65 | + PyObject *rdnlist; |
| 66 | + |
| 67 | + rdn = dn[i]; |
| 68 | + rdnlist = PyList_New(0); |
| 69 | + if (!rdnlist) |
| 70 | + goto failed; |
| 71 | + if (PyList_Append(tmp, rdnlist) == -1) { |
| 72 | + Py_DECREF(rdnlist); |
| 73 | + goto failed; |
| 74 | + } |
| 75 | + |
| 76 | + for (j = 0; rdn[j]; j++) { |
| 77 | + LDAPAVA *ava = rdn[j]; |
| 78 | + PyObject *tuple; |
| 79 | + |
| 80 | + tuple = Py_BuildValue("(O&O&i)", |
| 81 | + LDAPberval_to_object, &ava->la_attr, |
| 82 | + LDAPberval_to_object, &ava->la_value, |
| 83 | + ava->la_flags & ~(LDAP_AVA_FREE_ATTR|LDAP_AVA_FREE_VALUE)); |
| 84 | + if (!tuple) { |
| 85 | + Py_DECREF(rdnlist); |
| 86 | + goto failed; |
| 87 | + } |
| 88 | + |
| 89 | + if (PyList_Append(rdnlist, tuple) == -1) { |
| 90 | + Py_DECREF(tuple); |
| 91 | + goto failed; |
| 92 | + } |
| 93 | + Py_DECREF(tuple); |
| 94 | + } |
| 95 | + Py_DECREF(rdnlist); |
| 96 | + } |
| 97 | + |
| 98 | + result = tmp; |
| 99 | + tmp = NULL; |
| 100 | + |
| 101 | +failed: |
| 102 | + Py_XDECREF(tmp); |
| 103 | + ldap_dnfree(dn); |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +/* ldap_set_option (global options) */ |
| 108 | + |
| 109 | +static PyObject* |
| 110 | +l_ldap_set_option(PyObject* self, PyObject *args) |
| 111 | +{ |
| 112 | + PyObject *value; |
| 113 | + int option; |
| 114 | + |
| 115 | + if (!PyArg_ParseTuple(args, "iO:set_option", &option, &value)) |
| 116 | + return NULL; |
| 117 | + if (!LDAP_set_option(NULL, option, value)) |
| 118 | + return NULL; |
| 119 | + Py_INCREF(Py_None); |
| 120 | + return Py_None; |
| 121 | +} |
| 122 | + |
| 123 | +/* ldap_get_option (global options) */ |
| 124 | + |
| 125 | +static PyObject* |
| 126 | +l_ldap_get_option(PyObject* self, PyObject *args) |
| 127 | +{ |
| 128 | + int option; |
| 129 | + |
| 130 | + if (!PyArg_ParseTuple(args, "i:get_option", &option)) |
| 131 | + return NULL; |
| 132 | + return LDAP_get_option(NULL, option); |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +/* methods */ |
| 137 | + |
| 138 | +static PyMethodDef methods[] = { |
| 139 | + { "initialize", (PyCFunction)l_ldap_initialize, METH_VARARGS }, |
| 140 | + { "str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS }, |
| 141 | + { "set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS }, |
| 142 | + { "get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS }, |
| 143 | + { NULL, NULL } |
| 144 | +}; |
| 145 | + |
| 146 | +/* initialisation */ |
| 147 | + |
| 148 | +void |
| 149 | +LDAPinit_functions( PyObject* d ) { |
| 150 | + LDAPadd_methods( d, methods ); |
| 151 | +} |
0 commit comments