1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, 2013, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
11 * This software is licensed as described in the file COPYING, which
12 * you should have received as part of this distribution. The terms
13 * are also available at http://curl.haxx.se/docs/copyright.html.
15 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16 * copies of the Software, and permit persons to whom the Software is
17 * furnished to do so, under the terms of the COPYING file.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 ***************************************************************************/
24 #include "curl_setup.h"
26 #if !defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)
29 * Notice that USE_OPENLDAP is only a source code selection switch. When
30 * libcurl is built with USE_OPENLDAP defined the libcurl source code that
31 * gets compiled is the code from openldap.c, otherwise the code that gets
32 * compiled is the code from ldap.c.
34 * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
35 * might be required for compilation and runtime. In order to use ancient
36 * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
42 #include <curl/curl.h>
44 #include "vtls/vtls.h"
46 #include "curl_ldap.h"
47 #include "curl_memory.h"
48 #include "curl_base64.h"
51 #define _MPRINTF_REPLACE /* use our functions only */
52 #include <curl/mprintf.h>
57 extern int ldap_pvt_url_scheme2proto(const char *);
58 extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
62 static CURLcode ldap_setup_connection(struct connectdata *conn);
63 static CURLcode ldap_do(struct connectdata *conn, bool *done);
64 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
65 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
66 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
67 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
69 static Curl_recv ldap_recv;
72 * LDAP protocol handler.
75 const struct Curl_handler Curl_handler_ldap = {
77 ldap_setup_connection, /* setup_connection */
80 ZERO_NULL, /* do_more */
81 ldap_connect, /* connect_it */
82 ldap_connecting, /* connecting */
83 ZERO_NULL, /* doing */
84 ZERO_NULL, /* proto_getsock */
85 ZERO_NULL, /* doing_getsock */
86 ZERO_NULL, /* domore_getsock */
87 ZERO_NULL, /* perform_getsock */
88 ldap_disconnect, /* disconnect */
89 ZERO_NULL, /* readwrite */
90 PORT_LDAP, /* defport */
91 CURLPROTO_LDAP, /* protocol */
92 PROTOPT_NONE /* flags */
97 * LDAPS protocol handler.
100 const struct Curl_handler Curl_handler_ldaps = {
101 "LDAPS", /* scheme */
102 ldap_setup_connection, /* setup_connection */
104 ldap_done, /* done */
105 ZERO_NULL, /* do_more */
106 ldap_connect, /* connect_it */
107 ldap_connecting, /* connecting */
108 ZERO_NULL, /* doing */
109 ZERO_NULL, /* proto_getsock */
110 ZERO_NULL, /* doing_getsock */
111 ZERO_NULL, /* domore_getsock */
112 ZERO_NULL, /* perform_getsock */
113 ldap_disconnect, /* disconnect */
114 ZERO_NULL, /* readwrite */
115 PORT_LDAPS, /* defport */
116 CURLPROTO_LDAP, /* protocol */
117 PROTOPT_SSL /* flags */
121 static const char *url_errs[] = {
125 "unrecognized scheme",
126 "unbalanced delimiter",
129 "bad or missing attributes",
130 "bad or missing scope",
131 "bad or missing filter",
132 "bad or missing extensions"
135 typedef struct ldapconninfo {
137 Curl_recv *recv; /* for stacking SSL handler */
146 typedef struct ldapreqinfo {
151 static CURLcode ldap_setup_connection(struct connectdata *conn)
155 struct SessionHandle *data=conn->data;
159 rc = ldap_url_parse(data->change.url, &lud);
160 if(rc != LDAP_URL_SUCCESS) {
161 const char *msg = "url parsing problem";
162 status = CURLE_URL_MALFORMAT;
163 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
164 if(rc == LDAP_URL_ERR_MEM)
165 status = CURLE_OUT_OF_MEMORY;
168 failf(conn->data, "LDAP local: %s", msg);
171 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
172 ldap_free_urldesc(lud);
174 li = calloc(1, sizeof(ldapconninfo));
176 return CURLE_OUT_OF_MEMORY;
178 conn->proto.generic = li;
179 connkeep(conn, "OpenLDAP default");
181 * - provide option to choose SASL Binds instead of Simple
187 static Sockbuf_IO ldapsb_tls;
190 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
192 ldapconninfo *li = conn->proto.generic;
193 struct SessionHandle *data = conn->data;
194 int rc, proto = LDAP_VERSION3;
200 strcpy(hosturl, "ldap");
202 if(conn->handler->flags & PROTOPT_SSL)
204 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
205 conn->host.name, conn->remote_port);
207 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
209 failf(data, "LDAP local: Cannot connect to %s, %s",
210 hosturl, ldap_err2string(rc));
211 return CURLE_COULDNT_CONNECT;
214 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
217 if(conn->handler->flags & PROTOPT_SSL) {
219 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
228 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
230 ldapconninfo *li = conn->proto.generic;
231 struct SessionHandle *data = conn->data;
232 LDAPMessage *msg = NULL;
233 struct timeval tv = {0,1}, *tvp;
238 if(conn->handler->flags & PROTOPT_SSL) {
239 /* Is the SSL handshake complete yet? */
241 CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
243 if(result || !li->ssldone)
247 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
250 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
251 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
253 li->recv = conn->recv[FIRSTSOCKET];
254 li->send = conn->send[FIRSTSOCKET];
264 struct berval passwd;
266 if(conn->bits.user_passwd) {
268 passwd.bv_val = conn->passwd;
269 passwd.bv_len = strlen(passwd.bv_val);
273 passwd.bv_val = NULL;
276 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
277 NULL, NULL, &li->msgid);
279 return CURLE_LDAP_CANNOT_BIND;
285 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
287 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
288 return CURLE_LDAP_CANNOT_BIND;
295 rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
297 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
298 return CURLE_LDAP_CANNOT_BIND;
301 /* Try to fallback to LDAPv2? */
302 if(err == LDAP_PROTOCOL_ERROR) {
304 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
305 if(proto == LDAP_VERSION3) {
310 proto = LDAP_VERSION2;
311 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
318 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
322 return CURLE_LOGIN_DENIED;
327 conn->recv[FIRSTSOCKET] = ldap_recv;
333 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
335 ldapconninfo *li = conn->proto.generic;
336 (void) dead_connection;
340 ldap_unbind_ext(li->ld, NULL, NULL);
343 conn->proto.generic = NULL;
349 static CURLcode ldap_do(struct connectdata *conn, bool *done)
351 ldapconninfo *li = conn->proto.generic;
353 CURLcode status = CURLE_OK;
355 LDAPURLDesc *ludp = NULL;
357 struct SessionHandle *data=conn->data;
359 connkeep(conn, "OpenLDAP do");
361 infof(data, "LDAP local: %s\n", data->change.url);
363 rc = ldap_url_parse(data->change.url, &ludp);
364 if(rc != LDAP_URL_SUCCESS) {
365 const char *msg = "url parsing problem";
366 status = CURLE_URL_MALFORMAT;
367 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
368 if(rc == LDAP_URL_ERR_MEM)
369 status = CURLE_OUT_OF_MEMORY;
372 failf(conn->data, "LDAP local: %s", msg);
376 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
377 ludp->lud_filter, ludp->lud_attrs, 0,
378 NULL, NULL, NULL, 0, &msgid);
379 ldap_free_urldesc(ludp);
380 if(rc != LDAP_SUCCESS) {
381 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
382 return CURLE_LDAP_SEARCH_FAILED;
384 lr = calloc(1,sizeof(ldapreqinfo));
386 return CURLE_OUT_OF_MEMORY;
388 data->req.protop = lr;
389 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
394 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
397 ldapreqinfo *lr = conn->data->req.protop;
403 /* if there was a search in progress, abandon it */
405 ldapconninfo *li = conn->proto.generic;
406 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
409 conn->data->req.protop = NULL;
416 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
417 size_t len, CURLcode *err)
419 ldapconninfo *li = conn->proto.generic;
420 struct SessionHandle *data = conn->data;
421 ldapreqinfo *lr = data->req.protop;
423 LDAPMessage *msg = NULL;
425 BerElement *ber = NULL;
426 struct timeval tv = {0,1};
432 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
434 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
435 *err = CURLE_RECV_ERROR;
446 for(ent = ldap_first_message(li->ld, msg); ent;
447 ent = ldap_next_message(li->ld, ent)) {
448 struct berval bv, *bvals, **bvp = &bvals;
449 int binary = 0, msgtype;
451 msgtype = ldap_msgtype(ent);
452 if(msgtype == LDAP_RES_SEARCH_RESULT) {
455 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
457 failf(data, "LDAP local: search ldap_parse_result %s",
458 ldap_err2string(rc));
459 *err = CURLE_LDAP_SEARCH_FAILED;
461 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
462 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
464 *err = CURLE_LDAP_SEARCH_FAILED;
468 if(code == LDAP_SIZELIMIT_EXCEEDED)
469 infof(data, "There are more than %d entries\n", lr->nument);
470 data->req.size = data->req.bytecount;
478 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
482 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
484 /* TODO: verify that this is really how this return code should be
486 *err = CURLE_RECV_ERROR;
489 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
493 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
498 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
501 data->req.bytecount += bv.bv_len + 5;
503 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
505 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
508 if(bv.bv_val == NULL) break;
510 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
515 for(i=0; bvals[i].bv_val != NULL; i++) {
517 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
521 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
526 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
529 data->req.bytecount += bv.bv_len + 2;
532 /* check for leading or trailing whitespace */
533 if(ISSPACE(bvals[i].bv_val[0]) ||
534 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
537 /* check for unprintable characters */
539 for(j=0; j<bvals[i].bv_len; j++)
540 if(!ISPRINT(bvals[i].bv_val[j])) {
546 if(binary || binval) {
547 char *val_b64 = NULL;
548 size_t val_b64_sz = 0;
549 /* Binary value, encode to base64. */
550 CURLcode error = Curl_base64_encode(data,
562 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
566 data->req.bytecount += 2;
568 *err = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
573 data->req.bytecount += val_b64_sz;
577 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
581 *err = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
586 data->req.bytecount += bvals[i].bv_len + 1;
588 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
592 data->req.bytecount++;
595 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
598 data->req.bytecount++;
600 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
603 data->req.bytecount++;
612 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
614 sbiod->sbiod_pvt = arg;
619 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
621 sbiod->sbiod_pvt = NULL;
625 /* We don't need to do anything because libcurl does it already */
627 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
634 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
637 if(opt == LBER_SB_OPT_DATA_READY) {
638 struct connectdata *conn = sbiod->sbiod_pvt;
639 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
645 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
647 struct connectdata *conn = sbiod->sbiod_pvt;
648 ldapconninfo *li = conn->proto.generic;
650 CURLcode err = CURLE_RECV_ERROR;
652 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
653 if(ret < 0 && err == CURLE_AGAIN) {
654 SET_SOCKERRNO(EWOULDBLOCK);
660 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
662 struct connectdata *conn = sbiod->sbiod_pvt;
663 ldapconninfo *li = conn->proto.generic;
665 CURLcode err = CURLE_SEND_ERROR;
667 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
668 if(ret < 0 && err == CURLE_AGAIN) {
669 SET_SOCKERRNO(EWOULDBLOCK);
674 static Sockbuf_IO ldapsb_tls =
685 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */