1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
25 #if !defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)
28 * Notice that USE_OPENLDAP is only a source code selection switch. When
29 * libcurl is built with USE_OPENLDAP defined the libcurl source code that
30 * gets compiled is the code from openldap.c, otherwise the code that gets
31 * compiled is the code from ldap.c.
33 * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
34 * might be required for compilation and runtime. In order to use ancient
35 * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
41 #include <curl/curl.h>
45 #include "curl_ldap.h"
46 #include "curl_memory.h"
47 #include "curl_base64.h"
49 #define _MPRINTF_REPLACE /* use our functions only */
50 #include <curl/mprintf.h>
55 extern int ldap_pvt_url_scheme2proto(const char *);
56 extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url, LDAP **ld);
59 static CURLcode ldap_setup(struct connectdata *conn);
60 static CURLcode ldap_do(struct connectdata *conn, bool *done);
61 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
62 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
63 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
64 static CURLcode ldap_disconnect(struct connectdata *conn);
66 static Curl_recv ldap_recv;
69 * LDAP protocol handler.
72 const struct Curl_handler Curl_handler_ldap = {
74 ldap_setup, /* setup_connection */
77 ZERO_NULL, /* do_more */
78 ldap_connect, /* connect_it */
79 ldap_connecting, /* connecting */
80 ZERO_NULL, /* doing */
81 ZERO_NULL, /* proto_getsock */
82 ZERO_NULL, /* doing_getsock */
83 ZERO_NULL, /* perform_getsock */
84 ldap_disconnect, /* disconnect */
85 PORT_LDAP, /* defport */
86 PROT_LDAP /* protocol */
91 * LDAPS protocol handler.
94 const struct Curl_handler Curl_handler_ldaps = {
96 ldap_setup, /* setup_connection */
99 ZERO_NULL, /* do_more */
100 ldap_connect, /* connect_it */
101 ldap_connecting, /* connecting */
102 ZERO_NULL, /* doing */
103 ZERO_NULL, /* proto_getsock */
104 ZERO_NULL, /* doing_getsock */
105 ZERO_NULL, /* perform_getsock */
106 ldap_disconnect, /* disconnect */
107 PORT_LDAPS, /* defport */
108 PROT_LDAP | PROT_SSL /* protocol */
112 static const char *url_errs[] = {
116 "unrecognized scheme",
117 "unbalanced delimiter",
120 "bad or missing attributes",
121 "bad or missing scope",
122 "bad or missing filter",
123 "bad or missing extensions"
126 typedef struct ldapconninfo {
128 Curl_recv *recv; /* for stacking SSL handler */
137 typedef struct ldapreqinfo {
142 static CURLcode ldap_setup(struct connectdata *conn)
146 struct SessionHandle *data=conn->data;
150 rc = ldap_url_parse(data->change.url, &lud);
151 if (rc != LDAP_URL_SUCCESS) {
152 const char *msg = "url parsing problem";
153 status = CURLE_URL_MALFORMAT;
154 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
155 if (rc == LDAP_URL_ERR_MEM)
156 status = CURLE_OUT_OF_MEMORY;
159 failf(conn->data, "LDAP local: %s", msg);
162 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
163 ldap_free_urldesc(lud);
165 li = calloc(1, sizeof(ldapconninfo));
167 conn->proto.generic = li;
169 * - provide option to choose SASL Binds instead of Simple
175 static Sockbuf_IO ldapsb_tls;
178 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
180 ldapconninfo *li = conn->proto.generic;
181 struct SessionHandle *data=conn->data;
182 int rc, proto = LDAP_VERSION3;
183 char hosturl[1024], *ptr;
185 strcpy(hosturl, "ldap");
187 if (conn->protocol & PROT_SSL)
189 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
190 conn->host.name, conn->port);
192 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
194 failf(data, "LDAP local: Cannot connect to %s, %s",
195 hosturl, ldap_err2string(rc));
196 return CURLE_COULDNT_CONNECT;
199 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
202 if (conn->protocol & PROT_SSL) {
204 if (data->state.used_interface == Curl_if_easy) {
205 res = Curl_ssl_connect(conn, FIRSTSOCKET);
210 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
217 if (data->state.used_interface == Curl_if_easy)
218 return ldap_connecting(conn, done);
223 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
225 ldapconninfo *li = conn->proto.generic;
226 struct SessionHandle *data=conn->data;
227 LDAPMessage *result = NULL;
228 struct timeval tv = {0,1}, *tvp;
233 if (conn->protocol & PROT_SSL) {
234 /* Is the SSL handshake complete yet? */
236 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
237 if (res || !li->ssldone)
240 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
243 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
244 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
246 li->recv = conn->recv[FIRSTSOCKET];
247 li->send = conn->send[FIRSTSOCKET];
252 if (data->state.used_interface == Curl_if_easy)
253 tvp = NULL; /* let ldap_result block indefinitely */
260 struct berval passwd;
262 if (conn->bits.user_passwd) {
264 passwd.bv_val = conn->passwd;
265 passwd.bv_len = strlen(passwd.bv_val);
268 passwd.bv_val = NULL;
271 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
272 NULL, NULL, &li->msgid);
274 return CURLE_LDAP_CANNOT_BIND;
280 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
282 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
283 return CURLE_LDAP_CANNOT_BIND;
289 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
291 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
292 return CURLE_LDAP_CANNOT_BIND;
294 /* Try to fallback to LDAPv2? */
295 if (err == LDAP_PROTOCOL_ERROR) {
297 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
298 if (proto == LDAP_VERSION3) {
300 proto = LDAP_VERSION2;
301 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
308 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
310 return CURLE_LOGIN_DENIED;
312 conn->recv[FIRSTSOCKET] = ldap_recv;
317 static CURLcode ldap_disconnect(struct connectdata *conn)
319 ldapconninfo *li = conn->proto.generic;
323 ldap_unbind_ext(li->ld, NULL, NULL);
326 conn->proto.generic = NULL;
332 static CURLcode ldap_do(struct connectdata *conn, bool *done)
334 ldapconninfo *li = conn->proto.generic;
336 CURLcode status = CURLE_OK;
338 LDAPURLDesc *ludp = NULL;
340 struct SessionHandle *data=conn->data;
342 conn->bits.close = FALSE;
344 infof(data, "LDAP local: %s\n", data->change.url);
346 rc = ldap_url_parse(data->change.url, &ludp);
347 if (rc != LDAP_URL_SUCCESS) {
348 const char *msg = "url parsing problem";
349 status = CURLE_URL_MALFORMAT;
350 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
351 if (rc == LDAP_URL_ERR_MEM)
352 status = CURLE_OUT_OF_MEMORY;
355 failf(conn->data, "LDAP local: %s", msg);
359 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
360 ludp->lud_filter, ludp->lud_attrs, 0,
361 NULL, NULL, NULL, 0, &msgid);
362 ldap_free_urldesc(ludp);
363 if (rc != LDAP_SUCCESS) {
364 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
365 return CURLE_LDAP_SEARCH_FAILED;
367 lr = calloc(1,sizeof(ldapreqinfo));
369 data->state.proto.generic = lr;
370 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
375 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
378 ldapreqinfo *lr = conn->data->state.proto.generic;
383 /* if there was a search in progress, abandon it */
385 ldapconninfo *li = conn->proto.generic;
386 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
389 conn->data->state.proto.generic = NULL;
395 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
396 size_t len, CURLcode *err)
398 ldapconninfo *li = conn->proto.generic;
399 struct SessionHandle *data=conn->data;
400 ldapreqinfo *lr = data->state.proto.generic;
402 LDAPMessage *result = NULL;
404 BerElement *ber = NULL;
405 struct timeval tv = {0,1};
410 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
412 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
413 *err = CURLE_RECV_ERROR;
424 for (ent = ldap_first_message(li->ld, result); ent;
425 ent = ldap_next_message(li->ld, ent)) {
426 struct berval bv, *bvals, **bvp = &bvals;
427 int binary = 0, msgtype;
429 msgtype = ldap_msgtype(ent);
430 if (msgtype == LDAP_RES_SEARCH_RESULT) {
433 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
435 failf(data, "LDAP local: search ldap_parse_result %s", ldap_err2string(rc));
436 *err = CURLE_LDAP_SEARCH_FAILED;
437 } else if (code && code != LDAP_SIZELIMIT_EXCEEDED) {
438 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
440 *err = CURLE_LDAP_SEARCH_FAILED;
443 if (code == LDAP_SIZELIMIT_EXCEEDED)
444 infof(data, "There are more than %d entries\n", lr->nument);
445 data->req.size = data->req.bytecount;
452 } else if (msgtype != LDAP_RES_SEARCH_ENTRY) {
457 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
458 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
459 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
460 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
461 data->req.bytecount += bv.bv_len + 5;
463 for (rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
465 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
468 if (bv.bv_val == NULL) break;
470 if (bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
473 for (i=0; bvals[i].bv_val != NULL; i++) {
475 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
476 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
477 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
478 data->req.bytecount += bv.bv_len + 2;
481 /* check for leading or trailing whitespace */
482 if (isspace(bvals[i].bv_val[0]) ||
483 isspace(bvals[i].bv_val[bvals[i].bv_len-1])) {
486 /* check for unprintable characters */
488 for (j=0; j<bvals[i].bv_len; j++)
489 if (!isprint(bvals[i].bv_val[j])) {
495 if (binary || binval) {
497 /* Binary value, encode to base64. */
498 size_t val_b64_sz = Curl_base64_encode(data,
502 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
503 data->req.bytecount += 2;
505 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
507 data->req.bytecount += val_b64_sz;
510 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
511 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
513 data->req.bytecount += bvals[i].bv_len + 1;
515 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
516 data->req.bytecount++;
519 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
520 data->req.bytecount++;
522 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
523 data->req.bytecount++;
526 ldap_msgfree(result);
532 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
534 sbiod->sbiod_pvt = arg;
539 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
541 sbiod->sbiod_pvt = NULL;
545 /* We don't need to do anything because libcurl does it already */
547 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
554 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
557 if (opt == LBER_SB_OPT_DATA_READY) {
558 struct connectdata *conn = sbiod->sbiod_pvt;
559 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
565 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
567 struct connectdata *conn = sbiod->sbiod_pvt;
568 ldapconninfo *li = conn->proto.generic;
570 CURLcode err = CURLE_RECV_ERROR;
572 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
573 if (ret < 0 && err == CURLE_AGAIN) {
574 SET_SOCKERRNO(EWOULDBLOCK);
580 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
582 struct connectdata *conn = sbiod->sbiod_pvt;
583 ldapconninfo *li = conn->proto.generic;
585 CURLcode err = CURLE_SEND_ERROR;
587 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
588 if (ret < 0 && err == CURLE_AGAIN) {
589 SET_SOCKERRNO(EWOULDBLOCK);
594 static Sockbuf_IO ldapsb_tls =
605 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */