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