1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011 - 2015, 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_base64.h"
49 #include "curl_printf.h"
51 /* The last #include files should be: */
52 #include "curl_memory.h"
56 extern int ldap_pvt_url_scheme2proto(const char *);
57 extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
61 static CURLcode ldap_setup_connection(struct connectdata *conn);
62 static CURLcode ldap_do(struct connectdata *conn, bool *done);
63 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
64 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
65 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
66 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
68 static Curl_recv ldap_recv;
71 * LDAP protocol handler.
74 const struct Curl_handler Curl_handler_ldap = {
76 ldap_setup_connection, /* setup_connection */
79 ZERO_NULL, /* do_more */
80 ldap_connect, /* connect_it */
81 ldap_connecting, /* connecting */
82 ZERO_NULL, /* doing */
83 ZERO_NULL, /* proto_getsock */
84 ZERO_NULL, /* doing_getsock */
85 ZERO_NULL, /* domore_getsock */
86 ZERO_NULL, /* perform_getsock */
87 ldap_disconnect, /* disconnect */
88 ZERO_NULL, /* readwrite */
89 PORT_LDAP, /* defport */
90 CURLPROTO_LDAP, /* protocol */
91 PROTOPT_NONE /* flags */
96 * LDAPS protocol handler.
99 const struct Curl_handler Curl_handler_ldaps = {
100 "LDAPS", /* scheme */
101 ldap_setup_connection, /* setup_connection */
103 ldap_done, /* done */
104 ZERO_NULL, /* do_more */
105 ldap_connect, /* connect_it */
106 ldap_connecting, /* connecting */
107 ZERO_NULL, /* doing */
108 ZERO_NULL, /* proto_getsock */
109 ZERO_NULL, /* doing_getsock */
110 ZERO_NULL, /* domore_getsock */
111 ZERO_NULL, /* perform_getsock */
112 ldap_disconnect, /* disconnect */
113 ZERO_NULL, /* readwrite */
114 PORT_LDAPS, /* defport */
115 CURLPROTO_LDAP, /* protocol */
116 PROTOPT_SSL /* flags */
120 static const char *url_errs[] = {
124 "unrecognized scheme",
125 "unbalanced delimiter",
128 "bad or missing attributes",
129 "bad or missing scope",
130 "bad or missing filter",
131 "bad or missing extensions"
134 typedef struct ldapconninfo {
136 Curl_recv *recv; /* for stacking SSL handler */
145 typedef struct ldapreqinfo {
150 static CURLcode ldap_setup_connection(struct connectdata *conn)
154 struct SessionHandle *data=conn->data;
158 rc = ldap_url_parse(data->change.url, &lud);
159 if(rc != LDAP_URL_SUCCESS) {
160 const char *msg = "url parsing problem";
161 status = CURLE_URL_MALFORMAT;
162 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
163 if(rc == LDAP_URL_ERR_MEM)
164 status = CURLE_OUT_OF_MEMORY;
167 failf(conn->data, "LDAP local: %s", msg);
170 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
171 ldap_free_urldesc(lud);
173 li = calloc(1, sizeof(ldapconninfo));
175 return CURLE_OUT_OF_MEMORY;
177 conn->proto.generic = li;
178 connkeep(conn, "OpenLDAP default");
180 * - provide option to choose SASL Binds instead of Simple
186 static Sockbuf_IO ldapsb_tls;
189 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
191 ldapconninfo *li = conn->proto.generic;
192 struct SessionHandle *data = conn->data;
193 int rc, proto = LDAP_VERSION3;
199 strcpy(hosturl, "ldap");
201 if(conn->handler->flags & PROTOPT_SSL)
203 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
204 conn->host.name, conn->remote_port);
206 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
208 failf(data, "LDAP local: Cannot connect to %s, %s",
209 hosturl, ldap_err2string(rc));
210 return CURLE_COULDNT_CONNECT;
213 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
216 if(conn->handler->flags & PROTOPT_SSL) {
218 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
227 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
229 ldapconninfo *li = conn->proto.generic;
230 struct SessionHandle *data = conn->data;
231 LDAPMessage *msg = NULL;
232 struct timeval tv = {0, 1}, *tvp;
237 if(conn->handler->flags & PROTOPT_SSL) {
238 /* Is the SSL handshake complete yet? */
240 CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
242 if(result || !li->ssldone)
246 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
249 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
250 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
252 li->recv = conn->recv[FIRSTSOCKET];
253 li->send = conn->send[FIRSTSOCKET];
263 struct berval passwd;
265 if(conn->bits.user_passwd) {
267 passwd.bv_val = conn->passwd;
268 passwd.bv_len = strlen(passwd.bv_val);
272 passwd.bv_val = NULL;
275 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
276 NULL, NULL, &li->msgid);
278 return CURLE_LDAP_CANNOT_BIND;
284 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
286 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
287 return CURLE_LDAP_CANNOT_BIND;
294 rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
296 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
297 return CURLE_LDAP_CANNOT_BIND;
300 /* Try to fallback to LDAPv2? */
301 if(err == LDAP_PROTOCOL_ERROR) {
303 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
304 if(proto == LDAP_VERSION3) {
309 proto = LDAP_VERSION2;
310 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
317 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
321 return CURLE_LOGIN_DENIED;
326 conn->recv[FIRSTSOCKET] = ldap_recv;
332 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
334 ldapconninfo *li = conn->proto.generic;
335 (void) dead_connection;
339 ldap_unbind_ext(li->ld, NULL, NULL);
342 conn->proto.generic = NULL;
348 static CURLcode ldap_do(struct connectdata *conn, bool *done)
350 ldapconninfo *li = conn->proto.generic;
352 CURLcode status = CURLE_OK;
354 LDAPURLDesc *ludp = NULL;
356 struct SessionHandle *data=conn->data;
358 connkeep(conn, "OpenLDAP do");
360 infof(data, "LDAP local: %s\n", data->change.url);
362 rc = ldap_url_parse(data->change.url, &ludp);
363 if(rc != LDAP_URL_SUCCESS) {
364 const char *msg = "url parsing problem";
365 status = CURLE_URL_MALFORMAT;
366 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
367 if(rc == LDAP_URL_ERR_MEM)
368 status = CURLE_OUT_OF_MEMORY;
371 failf(conn->data, "LDAP local: %s", msg);
375 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
376 ludp->lud_filter, ludp->lud_attrs, 0,
377 NULL, NULL, NULL, 0, &msgid);
378 ldap_free_urldesc(ludp);
379 if(rc != LDAP_SUCCESS) {
380 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
381 return CURLE_LDAP_SEARCH_FAILED;
383 lr = calloc(1, sizeof(ldapreqinfo));
385 return CURLE_OUT_OF_MEMORY;
387 data->req.protop = lr;
388 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
393 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
396 ldapreqinfo *lr = conn->data->req.protop;
402 /* if there was a search in progress, abandon it */
404 ldapconninfo *li = conn->proto.generic;
405 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
408 conn->data->req.protop = NULL;
415 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
416 size_t len, CURLcode *err)
418 ldapconninfo *li = conn->proto.generic;
419 struct SessionHandle *data = conn->data;
420 ldapreqinfo *lr = data->req.protop;
422 LDAPMessage *msg = NULL;
424 BerElement *ber = NULL;
425 struct timeval tv = {0, 1};
431 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
433 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
434 *err = CURLE_RECV_ERROR;
445 for(ent = ldap_first_message(li->ld, msg); ent;
446 ent = ldap_next_message(li->ld, ent)) {
447 struct berval bv, *bvals, **bvp = &bvals;
448 int binary = 0, msgtype;
450 msgtype = ldap_msgtype(ent);
451 if(msgtype == LDAP_RES_SEARCH_RESULT) {
454 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
456 failf(data, "LDAP local: search ldap_parse_result %s",
457 ldap_err2string(rc));
458 *err = CURLE_LDAP_SEARCH_FAILED;
460 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
461 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
463 *err = CURLE_LDAP_SEARCH_FAILED;
467 if(code == LDAP_SIZELIMIT_EXCEEDED)
468 infof(data, "There are more than %d entries\n", lr->nument);
469 data->req.size = data->req.bytecount;
477 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
481 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
483 /* TODO: verify that this is really how this return code should be
485 *err = CURLE_RECV_ERROR;
488 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
492 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
497 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
500 data->req.bytecount += bv.bv_len + 5;
502 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
504 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
507 if(bv.bv_val == NULL) break;
509 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
514 for(i=0; bvals[i].bv_val != NULL; i++) {
516 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
520 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
525 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
528 data->req.bytecount += bv.bv_len + 2;
531 /* check for leading or trailing whitespace */
532 if(ISSPACE(bvals[i].bv_val[0]) ||
533 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
536 /* check for unprintable characters */
538 for(j=0; j<bvals[i].bv_len; j++)
539 if(!ISPRINT(bvals[i].bv_val[j])) {
545 if(binary || binval) {
546 char *val_b64 = NULL;
547 size_t val_b64_sz = 0;
548 /* Binary value, encode to base64. */
549 CURLcode error = Curl_base64_encode(data,
561 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
565 data->req.bytecount += 2;
567 *err = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
572 data->req.bytecount += val_b64_sz;
576 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
580 *err = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
585 data->req.bytecount += bvals[i].bv_len + 1;
587 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
591 data->req.bytecount++;
594 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
597 data->req.bytecount++;
599 *err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
602 data->req.bytecount++;
611 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
613 sbiod->sbiod_pvt = arg;
618 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
620 sbiod->sbiod_pvt = NULL;
624 /* We don't need to do anything because libcurl does it already */
626 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
633 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
636 if(opt == LBER_SB_OPT_DATA_READY) {
637 struct connectdata *conn = sbiod->sbiod_pvt;
638 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
644 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
646 struct connectdata *conn = sbiod->sbiod_pvt;
647 ldapconninfo *li = conn->proto.generic;
649 CURLcode err = CURLE_RECV_ERROR;
651 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
652 if(ret < 0 && err == CURLE_AGAIN) {
653 SET_SOCKERRNO(EWOULDBLOCK);
659 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
661 struct connectdata *conn = sbiod->sbiod_pvt;
662 ldapconninfo *li = conn->proto.generic;
664 CURLcode err = CURLE_SEND_ERROR;
666 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
667 if(ret < 0 && err == CURLE_AGAIN) {
668 SET_SOCKERRNO(EWOULDBLOCK);
673 static Sockbuf_IO ldapsb_tls =
684 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */