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(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, /* 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, /* 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(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;
195 char hosturl[1024], *ptr;
198 strcpy(hosturl, "ldap");
200 if(conn->handler->flags & PROTOPT_SSL)
202 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
203 conn->host.name, conn->remote_port);
205 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
207 failf(data, "LDAP local: Cannot connect to %s, %s",
208 hosturl, ldap_err2string(rc));
209 return CURLE_COULDNT_CONNECT;
212 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
215 if(conn->handler->flags & PROTOPT_SSL) {
217 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
226 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
228 ldapconninfo *li = conn->proto.generic;
229 struct SessionHandle *data=conn->data;
230 LDAPMessage *result = NULL;
231 struct timeval tv = {0,1}, *tvp;
236 if(conn->handler->flags & PROTOPT_SSL) {
237 /* Is the SSL handshake complete yet? */
239 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
241 if(res || !li->ssldone)
244 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
247 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
248 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
250 li->recv = conn->recv[FIRSTSOCKET];
251 li->send = conn->send[FIRSTSOCKET];
261 struct berval passwd;
263 if(conn->bits.user_passwd) {
265 passwd.bv_val = conn->passwd;
266 passwd.bv_len = strlen(passwd.bv_val);
270 passwd.bv_val = NULL;
273 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
274 NULL, NULL, &li->msgid);
276 return CURLE_LDAP_CANNOT_BIND;
282 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
284 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
285 return CURLE_LDAP_CANNOT_BIND;
291 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
293 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
294 return CURLE_LDAP_CANNOT_BIND;
296 /* Try to fallback to LDAPv2? */
297 if(err == LDAP_PROTOCOL_ERROR) {
299 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
300 if(proto == LDAP_VERSION3) {
305 proto = LDAP_VERSION2;
306 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
313 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
317 return CURLE_LOGIN_DENIED;
322 conn->recv[FIRSTSOCKET] = ldap_recv;
327 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
329 ldapconninfo *li = conn->proto.generic;
330 (void) dead_connection;
334 ldap_unbind_ext(li->ld, NULL, NULL);
337 conn->proto.generic = NULL;
343 static CURLcode ldap_do(struct connectdata *conn, bool *done)
345 ldapconninfo *li = conn->proto.generic;
347 CURLcode status = CURLE_OK;
349 LDAPURLDesc *ludp = NULL;
351 struct SessionHandle *data=conn->data;
353 connkeep(conn, "OpenLDAP do");
355 infof(data, "LDAP local: %s\n", data->change.url);
357 rc = ldap_url_parse(data->change.url, &ludp);
358 if(rc != LDAP_URL_SUCCESS) {
359 const char *msg = "url parsing problem";
360 status = CURLE_URL_MALFORMAT;
361 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
362 if(rc == LDAP_URL_ERR_MEM)
363 status = CURLE_OUT_OF_MEMORY;
366 failf(conn->data, "LDAP local: %s", msg);
370 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
371 ludp->lud_filter, ludp->lud_attrs, 0,
372 NULL, NULL, NULL, 0, &msgid);
373 ldap_free_urldesc(ludp);
374 if(rc != LDAP_SUCCESS) {
375 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
376 return CURLE_LDAP_SEARCH_FAILED;
378 lr = calloc(1,sizeof(ldapreqinfo));
380 return CURLE_OUT_OF_MEMORY;
382 data->req.protop = lr;
383 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
388 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
391 ldapreqinfo *lr = conn->data->req.protop;
396 /* if there was a search in progress, abandon it */
398 ldapconninfo *li = conn->proto.generic;
399 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
402 conn->data->req.protop = NULL;
408 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
409 size_t len, CURLcode *err)
411 ldapconninfo *li = conn->proto.generic;
412 struct SessionHandle *data=conn->data;
413 ldapreqinfo *lr = data->req.protop;
415 LDAPMessage *result = NULL;
417 BerElement *ber = NULL;
418 struct timeval tv = {0,1};
423 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
425 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
426 *err = CURLE_RECV_ERROR;
437 for(ent = ldap_first_message(li->ld, result); ent;
438 ent = ldap_next_message(li->ld, ent)) {
439 struct berval bv, *bvals, **bvp = &bvals;
440 int binary = 0, msgtype;
442 msgtype = ldap_msgtype(ent);
443 if(msgtype == LDAP_RES_SEARCH_RESULT) {
446 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
448 failf(data, "LDAP local: search ldap_parse_result %s",
449 ldap_err2string(rc));
450 *err = CURLE_LDAP_SEARCH_FAILED;
452 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
453 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
455 *err = CURLE_LDAP_SEARCH_FAILED;
459 if(code == LDAP_SIZELIMIT_EXCEEDED)
460 infof(data, "There are more than %d entries\n", lr->nument);
461 data->req.size = data->req.bytecount;
469 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
473 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
475 /* TODO: verify that this is really how this return code should be
477 *err = CURLE_RECV_ERROR;
480 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
481 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
482 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
483 data->req.bytecount += bv.bv_len + 5;
485 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
487 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
490 if(bv.bv_val == NULL) break;
492 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
497 for(i=0; bvals[i].bv_val != NULL; i++) {
499 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
500 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
502 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
503 data->req.bytecount += bv.bv_len + 2;
506 /* check for leading or trailing whitespace */
507 if(ISSPACE(bvals[i].bv_val[0]) ||
508 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
511 /* check for unprintable characters */
513 for(j=0; j<bvals[i].bv_len; j++)
514 if(!ISPRINT(bvals[i].bv_val[j])) {
520 if(binary || binval) {
521 char *val_b64 = NULL;
522 size_t val_b64_sz = 0;
523 /* Binary value, encode to base64. */
524 CURLcode error = Curl_base64_encode(data,
532 ldap_msgfree(result);
536 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
537 data->req.bytecount += 2;
539 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
541 data->req.bytecount += val_b64_sz;
545 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
546 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
548 data->req.bytecount += bvals[i].bv_len + 1;
550 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
551 data->req.bytecount++;
554 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
555 data->req.bytecount++;
557 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
558 data->req.bytecount++;
561 ldap_msgfree(result);
567 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
569 sbiod->sbiod_pvt = arg;
574 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
576 sbiod->sbiod_pvt = NULL;
580 /* We don't need to do anything because libcurl does it already */
582 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
589 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
592 if(opt == LBER_SB_OPT_DATA_READY) {
593 struct connectdata *conn = sbiod->sbiod_pvt;
594 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
600 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
602 struct connectdata *conn = sbiod->sbiod_pvt;
603 ldapconninfo *li = conn->proto.generic;
605 CURLcode err = CURLE_RECV_ERROR;
607 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
608 if(ret < 0 && err == CURLE_AGAIN) {
609 SET_SOCKERRNO(EWOULDBLOCK);
615 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
617 struct connectdata *conn = sbiod->sbiod_pvt;
618 ldapconninfo *li = conn->proto.generic;
620 CURLcode err = CURLE_SEND_ERROR;
622 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
623 if(ret < 0 && err == CURLE_AGAIN) {
624 SET_SOCKERRNO(EWOULDBLOCK);
629 static Sockbuf_IO ldapsb_tls =
640 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */