|
6 | 6 | #include "berval.h" |
7 | 7 | #include "constants.h" |
8 | 8 | #include "options.h" |
| 9 | +#include <stdio.h> |
9 | 10 |
|
10 | 11 | /* ldap_initialize */ |
11 | 12 |
|
@@ -160,6 +161,145 @@ l_ldap_str2dn(PyObject *unused, PyObject *args) |
160 | 161 | return result; |
161 | 162 | } |
162 | 163 |
|
| 164 | +/* ldap_dn2str */ |
| 165 | + |
| 166 | +static PyObject * |
| 167 | +l_ldap_dn2str(PyObject *unused, PyObject *args) |
| 168 | +{ |
| 169 | + struct berval str; |
| 170 | + LDAPDN dn = NULL; |
| 171 | + LDAPRDN rdn = NULL; |
| 172 | + int flags = 0; |
| 173 | + PyObject *result = NULL, *tmp = NULL, *dn_list = NULL; |
| 174 | + int res, i, j; |
| 175 | + char *type_error_message = "expected List[List[Tuple[str, str, int]]]"; |
| 176 | + |
| 177 | + /* |
| 178 | + * From a list-equivalent of AVA structures; namely: |
| 179 | + * ((('a', 'b', 1), ('c', 'd', 1)), (('e', 'f', 1),)), build |
| 180 | + * a DN string such as "a=b,c=d;e=f". |
| 181 | + * The integers are a bit combination of the AVA_* flags |
| 182 | + */ |
| 183 | + if (!PyArg_ParseTuple(args, "Oi:dn2str", &dn_list, &flags)) |
| 184 | + return NULL; |
| 185 | + Py_XDECREF(args); |
| 186 | + Py_INCREF(dn_list); |
| 187 | + |
| 188 | + PyObject *iter = PyObject_GetIter(dn_list); |
| 189 | + if (!iter) { |
| 190 | + PyErr_SetString(PyExc_TypeError, type_error_message); |
| 191 | + goto failed; |
| 192 | + } |
| 193 | + |
| 194 | + Py_ssize_t nrdns = PyObject_Length(dn_list); |
| 195 | + if (nrdns == -1) { |
| 196 | + // can't happen |
| 197 | + goto failed; |
| 198 | + } |
| 199 | + dn = malloc(sizeof(LDAPRDN *) * (nrdns + 1)); |
| 200 | + |
| 201 | + i = 0; |
| 202 | + while (1) { |
| 203 | + PyObject *inext = PyIter_Next(iter); |
| 204 | + if (!inext) { |
| 205 | + break; |
| 206 | + } |
| 207 | + |
| 208 | + if (PyList_Check(inext)) { |
| 209 | + inext = PyList_AsTuple(inext); |
| 210 | + } |
| 211 | + if (!PyTuple_Check(inext)) { |
| 212 | + PyErr_SetString(PyExc_TypeError, type_error_message); |
| 213 | + Py_XDECREF(iter); |
| 214 | + Py_XDECREF(inext); |
| 215 | + goto failed; |
| 216 | + } |
| 217 | + |
| 218 | + PyObject *iiter = PyObject_GetIter(inext); |
| 219 | + |
| 220 | + Py_ssize_t navas = PyObject_Length(inext); |
| 221 | + rdn = malloc(sizeof(LDAPRDN) * (navas + 1)); |
| 222 | + |
| 223 | + j = 0; |
| 224 | + while (1) { |
| 225 | + PyObject *next = PyIter_Next(iiter); |
| 226 | + if (!next) { |
| 227 | + break; |
| 228 | + } |
| 229 | + |
| 230 | + if (PyList_Check(next)) { |
| 231 | + next = PyList_AsTuple(next); |
| 232 | + } |
| 233 | + if (!PyTuple_Check(next) || PyTuple_Size(next) != 3) { |
| 234 | + PyErr_SetString(PyExc_TypeError, type_error_message); |
| 235 | + Py_XDECREF(iter); |
| 236 | + Py_XDECREF(iiter); |
| 237 | + Py_XDECREF(next); |
| 238 | + goto failed; |
| 239 | + } |
| 240 | + |
| 241 | + PyObject *name, *value, *encoding; |
| 242 | + |
| 243 | + name = PyTuple_GetItem(next, 0); |
| 244 | + value = PyTuple_GetItem(next, 1); |
| 245 | + encoding = PyTuple_GetItem(next, 2); |
| 246 | + |
| 247 | + if (!PyUnicode_Check(name) || !PyUnicode_Check(value) || !PyLong_Check(encoding)) { |
| 248 | + PyErr_SetString(PyExc_TypeError, type_error_message); |
| 249 | + Py_XDECREF(iter); |
| 250 | + Py_XDECREF(iiter); |
| 251 | + Py_XDECREF(next); |
| 252 | + Py_XDECREF(name); |
| 253 | + Py_XDECREF(value); |
| 254 | + Py_XDECREF(encoding); |
| 255 | + goto failed; |
| 256 | + } |
| 257 | + |
| 258 | + LDAPAVA *ava = malloc(sizeof(LDAPAVA)); |
| 259 | + |
| 260 | + ava->la_attr.bv_val = (char *) PyUnicode_AsUTF8AndSize(name, (long int*) &ava->la_attr.bv_len); |
| 261 | + ava->la_value.bv_val = (char *) PyUnicode_AsUTF8AndSize(value, (long int*) &ava->la_value.bv_len); |
| 262 | + ava->la_flags = (int)PyLong_AsLong(encoding); |
| 263 | + |
| 264 | + Py_XDECREF(next); |
| 265 | + // TODO: segfault when activating the following: |
| 266 | + //Py_XDECREF(name); |
| 267 | + //Py_XDECREF(value); |
| 268 | + //Py_XDECREF(encoding); |
| 269 | + |
| 270 | + rdn[j] = ava; |
| 271 | + j++; |
| 272 | + } |
| 273 | + Py_XDECREF(iiter); |
| 274 | + Py_XDECREF(inext); |
| 275 | + rdn[j] = NULL; |
| 276 | + |
| 277 | + dn[i] = rdn; |
| 278 | + i++; |
| 279 | + } |
| 280 | + dn[i] = NULL; |
| 281 | + |
| 282 | + res = ldap_dn2bv(dn, &str, flags); |
| 283 | + if (res != LDAP_SUCCESS) |
| 284 | + return LDAPerr(res); // TODO: no attr set |
| 285 | + |
| 286 | + tmp = PyUnicode_FromString(str.bv_val); |
| 287 | + if (!tmp) |
| 288 | + goto failed; |
| 289 | + |
| 290 | + result = tmp; |
| 291 | + //Py_XDECREF(tmp); |
| 292 | + tmp = NULL; |
| 293 | + |
| 294 | + failed: |
| 295 | + Py_XDECREF(tmp); |
| 296 | + Py_XDECREF(iter); |
| 297 | + Py_XDECREF(dn_list); |
| 298 | + //Py_XDECREF(result); |
| 299 | + // ldap_dnfree(dn); |
| 300 | + return result; |
| 301 | +} |
| 302 | + |
163 | 303 | /* ldap_set_option (global options) */ |
164 | 304 |
|
165 | 305 | static PyObject * |
@@ -196,6 +336,7 @@ static PyMethodDef methods[] = { |
196 | 336 | {"initialize_fd", (PyCFunction)l_ldap_initialize_fd, METH_VARARGS}, |
197 | 337 | #endif |
198 | 338 | {"str2dn", (PyCFunction)l_ldap_str2dn, METH_VARARGS}, |
| 339 | + {"dn2str", (PyCFunction)l_ldap_dn2str, METH_VARARGS}, |
199 | 340 | {"set_option", (PyCFunction)l_ldap_set_option, METH_VARARGS}, |
200 | 341 | {"get_option", (PyCFunction)l_ldap_get_option, METH_VARARGS}, |
201 | 342 | {NULL, NULL} |
|
0 commit comments