1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, 2013, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011 - 2013, 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"
50 #define _MPRINTF_REPLACE /* use our functions only */
51 #include <curl/mprintf.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(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, /* 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, /* 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(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 conn->bits.close = FALSE;
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;
194 char hosturl[1024], *ptr;
197 strcpy(hosturl, "ldap");
199 if(conn->handler->flags & PROTOPT_SSL)
201 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
202 conn->host.name, conn->remote_port);
204 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
206 failf(data, "LDAP local: Cannot connect to %s, %s",
207 hosturl, ldap_err2string(rc));
208 return CURLE_COULDNT_CONNECT;
211 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
214 if(conn->handler->flags & PROTOPT_SSL) {
216 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
225 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
227 ldapconninfo *li = conn->proto.generic;
228 struct SessionHandle *data=conn->data;
229 LDAPMessage *result = NULL;
230 struct timeval tv = {0,1}, *tvp;
235 if(conn->handler->flags & PROTOPT_SSL) {
236 /* Is the SSL handshake complete yet? */
238 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
240 if(res || !li->ssldone)
243 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
246 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
247 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
249 li->recv = conn->recv[FIRSTSOCKET];
250 li->send = conn->send[FIRSTSOCKET];
260 struct berval passwd;
262 if(conn->bits.user_passwd) {
264 passwd.bv_val = conn->passwd;
265 passwd.bv_len = strlen(passwd.bv_val);
269 passwd.bv_val = NULL;
272 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
273 NULL, NULL, &li->msgid);
275 return CURLE_LDAP_CANNOT_BIND;
281 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
283 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
284 return CURLE_LDAP_CANNOT_BIND;
290 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
292 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
293 return CURLE_LDAP_CANNOT_BIND;
295 /* Try to fallback to LDAPv2? */
296 if(err == LDAP_PROTOCOL_ERROR) {
298 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
299 if(proto == LDAP_VERSION3) {
304 proto = LDAP_VERSION2;
305 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
312 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
316 return CURLE_LOGIN_DENIED;
321 conn->recv[FIRSTSOCKET] = ldap_recv;
326 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
328 ldapconninfo *li = conn->proto.generic;
329 (void) dead_connection;
333 ldap_unbind_ext(li->ld, NULL, NULL);
336 conn->proto.generic = NULL;
342 static CURLcode ldap_do(struct connectdata *conn, bool *done)
344 ldapconninfo *li = conn->proto.generic;
346 CURLcode status = CURLE_OK;
348 LDAPURLDesc *ludp = NULL;
350 struct SessionHandle *data=conn->data;
352 conn->bits.close = FALSE;
354 infof(data, "LDAP local: %s\n", data->change.url);
356 rc = ldap_url_parse(data->change.url, &ludp);
357 if(rc != LDAP_URL_SUCCESS) {
358 const char *msg = "url parsing problem";
359 status = CURLE_URL_MALFORMAT;
360 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
361 if(rc == LDAP_URL_ERR_MEM)
362 status = CURLE_OUT_OF_MEMORY;
365 failf(conn->data, "LDAP local: %s", msg);
369 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
370 ludp->lud_filter, ludp->lud_attrs, 0,
371 NULL, NULL, NULL, 0, &msgid);
372 ldap_free_urldesc(ludp);
373 if(rc != LDAP_SUCCESS) {
374 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
375 return CURLE_LDAP_SEARCH_FAILED;
377 lr = calloc(1,sizeof(ldapreqinfo));
379 return CURLE_OUT_OF_MEMORY;
381 data->req.protop = lr;
382 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
387 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
390 ldapreqinfo *lr = conn->data->req.protop;
395 /* if there was a search in progress, abandon it */
397 ldapconninfo *li = conn->proto.generic;
398 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
401 conn->data->req.protop = NULL;
407 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
408 size_t len, CURLcode *err)
410 ldapconninfo *li = conn->proto.generic;
411 struct SessionHandle *data=conn->data;
412 ldapreqinfo *lr = data->req.protop;
414 LDAPMessage *result = NULL;
416 BerElement *ber = NULL;
417 struct timeval tv = {0,1};
422 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
424 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
425 *err = CURLE_RECV_ERROR;
436 for(ent = ldap_first_message(li->ld, result); ent;
437 ent = ldap_next_message(li->ld, ent)) {
438 struct berval bv, *bvals, **bvp = &bvals;
439 int binary = 0, msgtype;
441 msgtype = ldap_msgtype(ent);
442 if(msgtype == LDAP_RES_SEARCH_RESULT) {
445 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
447 failf(data, "LDAP local: search ldap_parse_result %s",
448 ldap_err2string(rc));
449 *err = CURLE_LDAP_SEARCH_FAILED;
451 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
452 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
454 *err = CURLE_LDAP_SEARCH_FAILED;
458 if(code == LDAP_SIZELIMIT_EXCEEDED)
459 infof(data, "There are more than %d entries\n", lr->nument);
460 data->req.size = data->req.bytecount;
468 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
472 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
474 /* TODO: verify that this is really how this return code should be
476 *err = CURLE_RECV_ERROR;
479 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
480 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
481 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
482 data->req.bytecount += bv.bv_len + 5;
484 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
486 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
489 if(bv.bv_val == NULL) break;
491 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
496 for(i=0; bvals[i].bv_val != NULL; i++) {
498 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
499 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
501 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
502 data->req.bytecount += bv.bv_len + 2;
505 /* check for leading or trailing whitespace */
506 if(ISSPACE(bvals[i].bv_val[0]) ||
507 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
510 /* check for unprintable characters */
512 for(j=0; j<bvals[i].bv_len; j++)
513 if(!ISPRINT(bvals[i].bv_val[j])) {
519 if(binary || binval) {
520 char *val_b64 = NULL;
521 size_t val_b64_sz = 0;
522 /* Binary value, encode to base64. */
523 CURLcode error = Curl_base64_encode(data,
531 ldap_msgfree(result);
535 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
536 data->req.bytecount += 2;
538 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
540 data->req.bytecount += val_b64_sz;
544 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
545 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
547 data->req.bytecount += bvals[i].bv_len + 1;
549 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
550 data->req.bytecount++;
553 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
554 data->req.bytecount++;
556 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
557 data->req.bytecount++;
560 ldap_msgfree(result);
566 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
568 sbiod->sbiod_pvt = arg;
573 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
575 sbiod->sbiod_pvt = NULL;
579 /* We don't need to do anything because libcurl does it already */
581 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
588 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
591 if(opt == LBER_SB_OPT_DATA_READY) {
592 struct connectdata *conn = sbiod->sbiod_pvt;
593 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
599 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
601 struct connectdata *conn = sbiod->sbiod_pvt;
602 ldapconninfo *li = conn->proto.generic;
604 CURLcode err = CURLE_RECV_ERROR;
606 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
607 if(ret < 0 && err == CURLE_AGAIN) {
608 SET_SOCKERRNO(EWOULDBLOCK);
614 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
616 struct connectdata *conn = sbiod->sbiod_pvt;
617 ldapconninfo *li = conn->proto.generic;
619 CURLcode err = CURLE_SEND_ERROR;
621 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
622 if(ret < 0 && err == CURLE_AGAIN) {
623 SET_SOCKERRNO(EWOULDBLOCK);
628 static Sockbuf_IO ldapsb_tls =
639 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */