Debian Codegraph @

src/_bonsai/ldapconnectiter.c

raw symbols SCIP
1 #include "ldapconnectiter.h"
2
3 #include "utils.h"
4
5 #ifdef WIN32
6 //MS Windows
7
8 /* Poll the answer of the separate thread that runs the binding process.
9 Returns NULL in case of error, Py_None for timeout, and the LDAPConnection
10 object if successfully finished the binding. */
11 static PyObject *
12 binding(LDAPConnectIter *self) {
13 int rc;
14 char buff[1];
15 PyObject *value = NULL;
16
17 DEBUG("binding [state:%d]", self->state);
18 if (self->state == 3) {
19 /* First call of bind. */
20 rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
21 NULL, &(self->message_id));
22 if (rc != LDAP_SUCCESS) {
23 set_exception(self->conn->ld, rc);
24 return NULL;
25 }
26 self->state = 4;
27 Py_RETURN_NONE;
28 } else {
29 if (self->conn->async == 0) {
30 rc = WaitForSingleObject(self->info->thread, self->timeout);
31 } else {
32 rc = WaitForSingleObject(self->info->thread, 10);
33 }
34 switch (rc) {
35 case WAIT_TIMEOUT:
36 if (self->conn->async == 0) {
37 TerminateThread(self->info->thread, -1);
38 CloseHandle(self->info->thread);
39 ldap_unbind_ext(self->conn->ld, NULL, NULL);
40 set_exception(NULL, LDAP_TIMEOUT);
41 return NULL;
42 }
43 Py_RETURN_NONE;
44 case WAIT_OBJECT_0:
45 GetExitCodeThread(self->info->thread, &rc);
46 CloseHandle(self->info->thread);
47 if (rc != LDAP_SUCCESS) {
48 /* The ldap_connect is failed. Set a Python error. */
49 set_exception(self->conn->ld, rc);
50 return NULL;
51 }
52 if (self->conn->csock != -1) {
53 /* Read and drop the data from the dummy socket. */
54 if (recv(self->conn->csock, buff, 1, 0) == -1) return NULL;
55 /* Dummy sockets are no longer needed. */
56 self->conn->csock = -1;
57 close_socketpair(self->conn->socketpair);
58 }
59 /* The binding is successfully finished. */
60 self->state = 5;
61 self->conn->closed = 0;
62 if (self->conn->ppolicy == 1) {
63 /* Create (result, ctrl) tuple as return value.
64 Because WinLDAP does not support password policy control,
65 it is always None. */
66 value = Py_BuildValue("(O,O)", self->conn, Py_None);
67 if (value == NULL) return NULL;
68 return value;
69 }
70 else {
71 Py_INCREF(self->conn);
72 return (PyObject *)self->conn;
73 }
74 default:
75 /* The thread is failed. */
76 PyErr_BadInternalCall();
77 return NULL;
78 }
79 }
80 }
81
82
83 /* Dummy function, because there are no options for setting
84 CA or client certs for WinLDAP. */
85 static int
86 set_certificates(LDAPConnectIter *self) {
87 return 0;
88 }
89
90 static int
91 check_tls_result(LDAP *ld, HANDLE msgid, int timeout, char async, SOCKET csock) {
92 int rc = 0;
93 char buff[1];
94 DWORD x = 0;
95
96 DEBUG("check_tls_result (ld:%p, msgid:%p, timeout:%d, async:%d, csock:%d)",
97 ld, msgid, timeout, async, (int)csock);
98 if (async == 0) {
99 rc = WaitForSingleObject(msgid, timeout);
100 } else {
101 rc = WaitForSingleObject(msgid, 10);
102 }
103 switch (rc) {
104 case WAIT_TIMEOUT:
105 if (async == 0) {
106 TerminateThread(msgid, -1);
107 CloseHandle(msgid);
108 set_exception(NULL, LDAP_TIMEOUT);
109 return -1;
110 }
111 return 0;
112 case WAIT_OBJECT_0:
113 GetExitCodeThread(msgid, &rc);
114 CloseHandle(msgid);
115 if (rc != LDAP_SUCCESS) {
116 /* The ldap_connect is failed. Set a Python error. */
117 set_exception(ld, rc);
118 return -1;
119 }
120 if (csock != -1) {
121 /* Read and drop the data from the dummy socket. */
122 if (recv(csock, buff, 1, 0) == -1) return -1;
123 }
124 return 1;
125 default:
126 /* The thread is failed. */
127 PyErr_BadInternalCall();
128 return -1;
129 }
130 }
131
132 #else
133
134 /* Poll the answer of the async function calls of the binding process.
135 Returns NULL in case of error, Py_None for timeout, and the LDAPConnection
136 object if successfully finished the binding. */
137 static PyObject *
138 binding(LDAPConnectIter *self) {
139 int rc = -1;
140 int err = 0;
141 int ppres = 0;
142 unsigned int pperr = 0;
143 struct timeval polltime;
144 LDAPControl **returned_ctrls = NULL;
145 LDAPMessage *res;
146 PyObject *ctrl_obj = NULL;
147 PyObject *value = NULL;
148
149 if (self->timeout == -1) {
150 polltime.tv_sec = 0L;
151 polltime.tv_usec = 10L;
152 } else {
153 polltime.tv_sec = self->timeout / 1000;
154 polltime.tv_usec = (self->timeout % 1000) * 1000;
155 }
156
157 DEBUG("binding [state:%d]", self->state);
158 if (self->state == 3) {
159 /* First call of bind. */
160 rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
161 NULL, &(self->message_id));
162 if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS && rc != LDAP_X_CONNECTING) {
163 close_socketpair(self->conn->socketpair);
164 set_exception(self->conn->ld, rc);
165 return NULL;
166 }
167 if (self->conn->csock != -1) {
168 /* Dummy sockets are no longer needed. Dispose. */
169 self->conn->csock = -1;
170 close_socketpair(self->conn->socketpair);
171 }
172 self->state = 4;
173 Py_RETURN_NONE;
174 } else {
175 if (self->conn->async == 0) {
176 /* Block until the server response. */
177 if (self->timeout == -1) {
178 rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, NULL, &res);
179 } else {
180 rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
181 }
182 } else {
183 /* Binding is already in progress, poll result from the server. */
184 rc = ldap_result(self->conn->ld, self->message_id, LDAP_MSG_ALL, &polltime, &res);
185 }
186 switch (rc) {
187 case -1:
188 /* Error occurred during the operation. */
189 ldap_msgfree(res);
190 set_exception(self->conn->ld, 0);
191 return NULL;
192 case 0:
193 /* Timeout exceeded.*/
194 ldap_msgfree(res);
195 if (self->conn->async == 0) {
196 set_exception(self->conn->ld, -5);
197 return NULL;
198 }
199 Py_RETURN_NONE;
200 case LDAP_RES_BIND:
201 /* Response is arrived from the server. */
202 rc = ldap_parse_result(self->conn->ld, res, &err, NULL, NULL, NULL, &returned_ctrls, 0);
203 if (rc != LDAP_SUCCESS) {
204 ldap_msgfree(res);
205 return NULL;
206 }
207
208 ppres = create_ppolicy_control(self->conn->ld, returned_ctrls,
209 &ctrl_obj, &pperr);
210 if (returned_ctrls != NULL) ldap_controls_free(returned_ctrls);
211 if (ppres == -1) return NULL;
212
213 if (err != LDAP_SASL_BIND_IN_PROGRESS && err != LDAP_SUCCESS) {
214 /* Connection is failed. */
215 ldap_msgfree(res);
216 if (ppres == 1 && pperr != 65535) set_ppolicy_err(pperr, ctrl_obj);
217 else set_exception(self->conn->ld, err);
218
219 return NULL;
220 }
221
222 if (strcmp(self->info->mech, "SIMPLE") != 0) {
223 /* Continue SASL binding procedure. */
224 rc = _ldap_bind(self->conn->ld, self->info, self->conn->ppolicy,
225 res, &(self->message_id));
226
227 if (rc != LDAP_SUCCESS && rc != LDAP_SASL_BIND_IN_PROGRESS) {
228 set_exception(self->conn->ld, rc);
229 return NULL;
230 }
231
232 if (rc == LDAP_SASL_BIND_IN_PROGRESS) {
233 Py_RETURN_NONE;
234 }
235 } else {
236 ldap_msgfree(res);
237 }
238
239 if (rc == LDAP_SUCCESS) {
240 /* The binding is successfully finished. */
241 self->state = 5;
242 self->conn->closed = 0;
243 if (self->conn->ppolicy == 1) {
244 /* If ppolicy is not available set control to None. */
245 if (ppres != 1) ctrl_obj = Py_None;
246
247 /* Create (result, ctrl) tuple as return value. */
248 value = Py_BuildValue("(O,O)", self->conn, ctrl_obj);
249 Py_DECREF(ctrl_obj);
250 if (value == NULL) return NULL;
251 return value;
252 } else {
253 Py_INCREF(self->conn);
254 return (PyObject *)self->conn;
255 }
256 }
257 Py_RETURN_NONE;
258 default:
259 /* Invalid return value, it never should happen. */
260 PyErr_BadInternalCall();
261 return NULL;
262 }
263 }
264 }
265
266 /* Return a char* attribute from the LDAPClient object. */
267 static int
268 get_tls_attribute(PyObject *client, const char *name, char **value) {
269 PyObject *tmp = NULL;
270
271 tmp = PyObject_GetAttrString(client, name);
272 if (tmp == NULL) return -1;
273
274 if (tmp == Py_None) *value = NULL;
275 else *value= PyObject2char(tmp);
276 Py_DECREF(tmp);
277
278 return 0;
279 }
280
281 /* Set CA and client certificates for the connection. */
282 static int
283 set_certificates(LDAPConnectIter* self) {
284 int rc = 0;
285 const int true_val = 1;
286 char *ca_cert_dir = NULL;
287 char *ca_cert = NULL;
288 char *client_cert = NULL;
289 char *client_key = NULL;
290
291 DEBUG("set_certificates (self:%p)", self);
292 /* Set CA cert directory from LDAPClient. */
293 rc = get_tls_attribute(self->conn->client, "ca_cert_dir", &ca_cert_dir);
294 if (rc != 0) goto error;
295 /* Set CA cert from LDAPClient. */
296 rc = get_tls_attribute(self->conn->client, "ca_cert", &ca_cert);
297 if (rc != 0) goto error;
298 /* Set client cert from LDAPClient. */
299 rc = get_tls_attribute(self->conn->client, "client_cert", &client_cert);
300 if (rc != 0) goto error;
301 /* Set client key from LDAPClient. */
302 rc = get_tls_attribute(self->conn->client, "client_key", &client_key);
303 if (rc != 0) goto error;
304
305 if (ca_cert_dir == NULL || strcmp(ca_cert_dir, "") != 0) {
306 ldap_set_option(self->conn->ld, LDAP_OPT_X_TLS_CACERTDIR, ca_cert_dir);
307 }
308 if (ca_cert == NULL || strcmp(ca_cert, "") != 0) {
309 ldap_set_option(self->conn->ld, LDAP_OPT_X_TLS_CACERTFILE, ca_cert);
310 }
311 if (client_cert == NULL || strcmp(client_cert, "") != 0) {
312 ldap_set_option(self->conn->ld, LDAP_OPT_X_TLS_CERTFILE, client_cert);
313 }
314 if (client_key == NULL || strcmp(client_key, "") != 0) {
315 ldap_set_option(self->conn->ld, LDAP_OPT_X_TLS_KEYFILE, client_key);
316 }
317 /* Force libldap to create new context for the connection. */
318 ldap_set_option(self->conn->ld, LDAP_OPT_X_TLS_NEWCTX, &true_val);
319
320 error:
321 free(ca_cert);
322 free(ca_cert_dir);
323 free(client_cert);
324 free(client_key);
325 return rc;
326 }
327
328 static int
329 check_tls_result(LDAP *ld, int msgid, int timeout, char async, SOCKET csock) {
330 int rc = 0;
331 int err = 0;
332 char *errstr = NULL;
333 LDAPMessage *res;
334 LDAPControl **ctrls = NULL;
335 struct timeval polltime;
336 PyObject *ldaperror = NULL, *errmsg = NULL;
337
338 DEBUG("check_tls_result (ld:%p, msgid:%d, timeout:%d, async:%d, csock:%d)",
339 ld, msgid, timeout, async, csock);
340 if (timeout == -1) {
341 polltime.tv_sec = 0L;
342 polltime.tv_usec = 10L;
343 } else {
344 polltime.tv_sec = timeout / 1000;
345 polltime.tv_usec = (timeout % 1000) * 1000;
346 }
347
348 if (async == 0) {
349 Py_BEGIN_ALLOW_THREADS
350 if (timeout == -1) {
351 rc = ldap_result(ld, msgid, LDAP_MSG_ALL, NULL, &res);
352 } else {
353 rc = ldap_result(ld, msgid, LDAP_MSG_ALL, &polltime, &res);
354 }
355 Py_END_ALLOW_THREADS
356 } else {
357 rc = ldap_result(ld, msgid, LDAP_MSG_ALL, &polltime, &res);
358 }
359
360 switch (rc) {
361 case -1:
362 /* Error occurred during the operation. */
363 ldap_msgfree(res);
364 set_exception(ld, 0);
365 return -1;
366 case 0:
367 /* Timeout exceeded.*/
368 ldap_msgfree(res);
369 if (async == 0) {
370 set_exception(ld, -5);
371 return -1;
372 }
373 return 0;
374 case LDAP_RES_EXTENDED:
375 rc = ldap_parse_result(ld, res, &err, NULL, &errstr, NULL, &ctrls, 0);
376 if (rc != LDAP_SUCCESS || err != LDAP_SUCCESS) {
377 ldaperror = get_error_by_code(err);
378 if (ldaperror == NULL) return -1;
379 if (errstr != NULL) errmsg = PyUnicode_FromFormat("%s.", errstr);
380 if (errmsg != NULL) {
381 PyErr_SetObject(ldaperror, errmsg);
382 Py_DECREF(errmsg);
383 } else PyErr_SetString(ldaperror, "");
384 Py_DECREF(ldaperror);
385 return -1;
386 }
387
388 rc = ldap_parse_extended_result(ld, res, NULL, NULL, 1);
389 if (rc != LDAP_SUCCESS) {
390 set_exception(ld, rc);
391 return -1;
392 }
393 rc = ldap_install_tls(ld);
394 if (rc != LDAP_SUCCESS) {
395 set_exception(ld, rc);
396 return -1;
397 }
398 return 1;
399 default:
400 PyErr_BadInternalCall();
401 return -1;
402 }
403 }
404
405 #endif
406
407 /* Dealloc the LDAPConnectIter object. */
408 static void
409 ldapconnectiter_dealloc(LDAPConnectIter* self) {
410 DEBUG("ldapconnectiter_dealloc (self:%p)", self);
411 Py_XDECREF(self->conn);
412 if (self->info != NULL) dealloc_conn_info(self->info);
413 Py_TYPE(self)->tp_free((PyObject*)self);
414 }
415
416 /* Create a new LDAPConnectIter object. */
417 static PyObject *
418 ldapconnectiter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
419 LDAPConnectIter *self = NULL;
420 PyObject *ts_empty_tuple = PyTuple_New(0);
421 PyObject *ts_empty_dict = PyDict_New();
422
423 if (ts_empty_tuple == NULL || ts_empty_dict == NULL) {
424 Py_XDECREF(ts_empty_tuple);
425 Py_XDECREF(ts_empty_dict);
426 return NULL;
427 }
428
429 self = (LDAPConnectIter *)PyBaseObject_Type.tp_new(type, ts_empty_tuple,
430 ts_empty_dict);
431
432 if (self != NULL) {
433 self->conn = NULL;
434 self->state = 0;
435 self->message_id = 0;
436 self->init_thread_data = NULL;
437 self->init_thread = 0;
438 self->timeout = -1;
439 self->tls = 0;
440 }
441
442 Py_DECREF(ts_empty_tuple);
443 Py_DECREF(ts_empty_dict);
444 DEBUG("ldapconnectiter_new [self:%p]", self);
445 return (PyObject *)self;
446 }
447
448 /* Create and return a ldapInitThreadData struct for the initialisation thread. */
449 static ldapInitThreadData *
450 create_init_thread_data(PyObject *client, SOCKET sock) {
451 ldapInitThreadData *data = NULL;
452 PyObject *url = NULL;
453 PyObject *tmp = NULL;
454
455 DEBUG("create_init_thread_data (client:%p, sock:%d)", client, (int)sock);
456 data = (ldapInitThreadData *)malloc(sizeof(ldapInitThreadData));
457 if (data == NULL) {
458 PyErr_NoMemory();
459 return NULL;
460 }
461 data->url = NULL;
462
463 /* Get URL policy from LDAPClient. */
464 url = PyObject_GetAttrString(client, "url");
465 if (url == NULL) goto error;
466
467 /* Get URL address information from the LDAPClient's LDAPURL object. */
468 tmp = PyObject_CallMethod(url, "get_address", NULL);
469 Py_DECREF(url);
470 if (tmp == NULL) goto error;
471 data->url = PyObject2char(tmp);
472 Py_DECREF(tmp);
473 if (data->url == NULL) goto error;
474
475 /* Set cert policy from LDAPClient. */
476 tmp = PyObject_GetAttrString(client, "cert_policy");
477 if (tmp == NULL) return NULL;
478 data->cert_policy = (int)PyLong_AsLong(tmp);
479 Py_DECREF(tmp);
480
481 /* Set referrals from LDAPClient. */
482 tmp = PyObject_GetAttrString(client, "server_chase_referrals");
483 if (tmp == NULL) goto error;
484 data->referrals = PyObject_IsTrue(tmp);
485 Py_DECREF(tmp);
486
487 /* Set sasl sec properties from LDAPClient. */
488 tmp = PyObject_GetAttrString(client, "sasl_security_properties");
489 if (tmp == NULL) goto error;
490 if (tmp != Py_None) {
491 data->sasl_sec_props = PyObject2char(tmp);
492 } else {
493 data->sasl_sec_props = NULL;
494 }
495 Py_DECREF(tmp);
496
497 data->ld = NULL;
498 data->sock = sock;
499 data->retval = 0;
500 return data;
501 error:
502 free(data->url);
503 free(data->sasl_sec_props);
504 free(data);
505 PyErr_BadInternalCall();
506 return NULL;
507 }
508
509 /* Create a new LDAPConnectIter object for internal use. */
510 LDAPConnectIter *
511 LDAPConnectIter_New(LDAPConnection *conn, ldap_conndata_t *info, SOCKET sock) {
512 PyObject *tmp = NULL;
513 LDAPConnectIter *self =
514 (LDAPConnectIter *)LDAPConnectIterType.tp_new(&LDAPConnectIterType,
515 NULL, NULL);
516
517 if (conn != NULL && self != NULL) {
518 Py_INCREF(conn);
519 self->conn = conn;
520 self->info = info;
521
522 /* Check the TLS state. */
523 tmp = PyObject_GetAttrString(self->conn->client, "tls");
524 if (tmp == NULL) return NULL;
525 self->tls = PyObject_IsTrue(tmp);
526 Py_DECREF(tmp);
527
528
529 self->init_thread_data = create_init_thread_data(self->conn->client, sock);
530 if (self->init_thread_data == NULL) return NULL;
531
532 if (create_init_thread(self->init_thread_data, self->info, &(self->init_thread))
533 != 0) return NULL;
534
535 self->timeout = -1;
536 }
537
538 return self;
539 }
540
541 /* Step the connection process into the next stage. */
542 PyObject *
543 LDAPConnectIter_Next(LDAPConnectIter *self, int timeout) {
544 int rc = -1;
545 PyObject *val = NULL;
546 char buff[1];
547
548 /* The connection is already binded. */
549 if (self->conn->closed == 0) {
550 return PyErr_Format(PyExc_StopIteration, "Connection is already open.");
551 }
552
553 DEBUG("LDAPConnectIter_Next (self:%p, timeout:%d)"
554 " [tls:%d, state:%d]", self, timeout, self->tls, self->state);
555 if (self->timeout == -1 && timeout >= 0) {
556 self->timeout = timeout;
557 }
558
559 /* Initialise LDAP struct. */
560 if (self->state == 0) {
561 rc = _ldap_finish_init_thread(self->conn->async, self->init_thread, &(self->timeout),
562 self->init_thread_data, &(self->conn->ld));
563 if (rc == -1) return NULL; /* Error is happened. */
564 if (rc == 1) {
565 /* Initialisation is finished. */
566 self->state = 1;
567 if (self->conn->csock != -1) {
568 /* Read and drop the data from the dummy socket. */
569 if (recv(self->conn->csock, buff, 1, 0) == -1) return NULL;
570 }
571 /* Set CA cert dir, CA cert and client cert. */
572 if (set_certificates(self) != 0) {
573 PyErr_BadInternalCall();
574 return NULL;
575 }
576 }
577 }
578
579 /* Start building TLS Connection, if needed. */
580 if (self->state == 1) {
581 if (self->tls == 1) {
582 rc = ldap_start_tls(self->conn->ld, NULL, NULL, &(self->tls_id));
583 if (rc == LDAP_SUCCESS) {
584 self->state = 2;
585 } else {
586 set_exception(self->conn->ld, rc);
587 return NULL;
588 }
589 } else {
590 /* TLS connection is not needed. */
591 self->state = 3;
592 }
593 }
594
595 /* Finish building TLS connection. */
596 if (self->state == 2) {
597 rc = check_tls_result(self->conn->ld, self->tls_id, self->timeout,
598 self->conn->async, self->conn->csock);
599 if (rc == -1) return NULL;
600 if (rc == 1) {
601 self->state = 3;
602 }
603 }
604
605 /* Start binding procedure. */
606 if (self->state > 2) {
607 val = binding(self);
608 if (val == NULL) return NULL; /* It is an error. */
609 if (val != Py_None) return val;
610 Py_DECREF(val);
611 }
612
613 if (self->conn->async == 0) {
614 /* If the function is blocking, then call next() */
615 /* automatically, until an error or the LDAPConnection occurs. */
616 return LDAPConnectIter_Next(self, self->timeout);
617 } else {
618 Py_RETURN_NONE;
619 }
620 }
621
622 PyTypeObject LDAPConnectIterType = {
623 PyVarObject_HEAD_INIT(NULL, 0)
624 "_bonsai.ldapconnectiter", /* tp_name */
625 sizeof(LDAPConnectIter), /* tp_basicsize */
626 0, /* tp_itemsize */
627 (destructor)ldapconnectiter_dealloc, /* tp_dealloc */
628 0, /* tp_print */
629 0, /* tp_getattr */
630 0, /* tp_setattr */
631 0, /* tp_reserved */
632 0, /* tp_repr */
633 0, /* tp_as_number */
634 0, /* tp_as_sequence */
635 0, /* tp_as_mapping */
636 0, /* tp_hash */
637 0, /* tp_call */
638 0, /* tp_str */
639 0, /* tp_getattro */
640 0, /* tp_setattro */
641 0, /* tp_as_buffer */
642 Py_TPFLAGS_DEFAULT |
643 Py_TPFLAGS_BASETYPE, /* tp_flags */
644 "ldapconnectiter object, implemented in C.", /* tp_doc */
645 0, /* tp_traverse */
646 0, /* tp_clear */
647 0, /* tp_richcompare */
648 0, /* tp_weaklistoffset */
649 0, /* tp_iter */
650 0, /* tp_iternext */
651 0, /* tp_methods */
652 0, /* tp_members */
653 0, /* tp_getset */
654 0, /* tp_base */
655 0, /* tp_dict */
656 0, /* tp_descr_get */
657 0, /* tp_descr_set */
658 0, /* tp_dictoffset */
659 0, /* tp_init */
660 0, /* tp_alloc */
661 ldapconnectiter_new, /* tp_new */
662 };