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 ***************************************************************************/
28 #include <curl/curl.h>
33 #include "curl_base64.h"
35 #define _MPRINTF_REPLACE /* use our functions only */
36 #include <curl/mprintf.h>
40 static CURLcode ldap_setup(struct connectdata *conn);
41 static CURLcode ldap_do(struct connectdata *conn, bool *done);
42 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
43 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
44 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
45 static CURLcode ldap_disconnect(struct connectdata *conn);
47 static Curl_recv ldap_recv;
50 * LDAP protocol handler.
53 const struct Curl_handler Curl_handler_ldap = {
55 ldap_setup, /* setup_connection */
58 ZERO_NULL, /* do_more */
59 ldap_connect, /* connect_it */
60 ldap_connecting, /* connecting */
61 ZERO_NULL, /* doing */
62 ZERO_NULL, /* proto_getsock */
63 ZERO_NULL, /* doing_getsock */
64 ZERO_NULL, /* perform_getsock */
65 ldap_disconnect, /* disconnect */
66 PORT_LDAP, /* defport */
67 PROT_LDAP /* protocol */
71 * LDAPS protocol handler.
74 const struct Curl_handler Curl_handler_ldaps = {
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, /* perform_getsock */
86 ldap_disconnect, /* disconnect */
87 PORT_LDAPS, /* defport */
88 PROT_LDAP | PROT_SSL /* protocol */
91 static const char *url_errs[] = {
95 "unrecognized scheme",
96 "unbalanced delimiter",
99 "bad or missing attributes",
100 "bad or missing scope",
101 "bad or missing filter",
102 "bad or missing extensions"
105 typedef struct ldapconninfo {
107 Curl_recv *recv; /* for stacking SSL handler */
116 typedef struct ldapreqinfo {
121 static CURLcode ldap_setup(struct connectdata *conn)
125 struct SessionHandle *data=conn->data;
129 rc = ldap_url_parse(data->change.url, &lud);
130 if (rc != LDAP_URL_SUCCESS) {
131 const char *msg = "url parsing problem";
132 status = CURLE_URL_MALFORMAT;
133 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
134 if (rc == LDAP_URL_ERR_MEM)
135 status = CURLE_OUT_OF_MEMORY;
138 failf(conn->data, "LDAP local: %s", msg);
141 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
142 ldap_free_urldesc(lud);
144 li = calloc(1, sizeof(ldapconninfo));
146 conn->proto.generic = li;
148 * - provide option to choose SASL Binds instead of Simple
153 static Sockbuf_IO ldapsb_tls;
155 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
157 ldapconninfo *li = conn->proto.generic;
158 struct SessionHandle *data=conn->data;
159 int rc, proto = LDAP_VERSION3;
160 char hosturl[1024], *ptr;
162 strcpy(hosturl, "ldap");
164 if (conn->protocol & PROT_SSL)
166 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
167 conn->host.name, conn->port);
169 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
171 failf(data, "LDAP local: Cannot connect to %s, %s",
172 hosturl, ldap_err2string(rc));
173 return CURLE_COULDNT_CONNECT;
176 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
178 if (conn->protocol & PROT_SSL) {
180 if (data->state.used_interface == Curl_if_easy) {
181 res = Curl_ssl_connect(conn, FIRSTSOCKET);
186 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
192 if (data->state.used_interface == Curl_if_easy)
193 return ldap_connecting(conn, done);
198 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
200 ldapconninfo *li = conn->proto.generic;
201 struct SessionHandle *data=conn->data;
202 LDAPMessage *result = NULL;
203 struct timeval tv = {0,1}, *tvp;
207 if (conn->protocol & PROT_SSL) {
208 /* Is the SSL handshake complete yet? */
210 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
211 if (res || !li->ssldone)
214 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
217 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
218 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
220 li->recv = conn->recv[FIRSTSOCKET];
221 li->send = conn->send[FIRSTSOCKET];
225 if (data->state.used_interface == Curl_if_easy)
226 tvp = NULL; /* let ldap_result block indefinitely */
233 struct berval passwd;
235 if (conn->bits.user_passwd) {
237 passwd.bv_val = conn->passwd;
238 passwd.bv_len = strlen(passwd.bv_val);
241 passwd.bv_val = NULL;
244 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
245 NULL, NULL, &li->msgid);
247 return CURLE_LDAP_CANNOT_BIND;
253 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
255 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
256 return CURLE_LDAP_CANNOT_BIND;
262 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
264 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
265 return CURLE_LDAP_CANNOT_BIND;
267 /* Try to fallback to LDAPv2? */
268 if (err == LDAP_PROTOCOL_ERROR) {
270 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
271 if (proto == LDAP_VERSION3) {
273 proto = LDAP_VERSION2;
274 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
281 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
283 return CURLE_LOGIN_DENIED;
285 conn->recv[FIRSTSOCKET] = ldap_recv;
290 static CURLcode ldap_disconnect(struct connectdata *conn)
292 ldapconninfo *li = conn->proto.generic;
299 conn->proto.generic = NULL;
305 static CURLcode ldap_do(struct connectdata *conn, bool *done)
307 ldapconninfo *li = conn->proto.generic;
309 CURLcode status = CURLE_OK;
311 LDAPURLDesc *ludp = NULL;
313 struct SessionHandle *data=conn->data;
315 conn->bits.close = FALSE;
317 infof(data, "LDAP local: %s\n", data->change.url);
319 rc = ldap_url_parse(data->change.url, &ludp);
320 if (rc != LDAP_URL_SUCCESS) {
321 const char *msg = "url parsing problem";
322 status = CURLE_URL_MALFORMAT;
323 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
324 if (rc == LDAP_URL_ERR_MEM)
325 status = CURLE_OUT_OF_MEMORY;
328 failf(conn->data, "LDAP local: %s", msg);
332 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
333 ludp->lud_filter, ludp->lud_attrs, 0,
334 NULL, NULL, NULL, 0, &msgid);
335 ldap_free_urldesc(ludp);
336 if (rc != LDAP_SUCCESS) {
337 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
338 return CURLE_LDAP_SEARCH_FAILED;
340 lr = calloc(1,sizeof(ldapreqinfo));
342 data->state.proto.generic = lr;
343 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
348 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
351 ldapreqinfo *lr = conn->data->state.proto.generic;
356 /* if there was a search in progress, abandon it */
358 ldapconninfo *li = conn->proto.generic;
359 ldap_abandon(li->ld, lr->msgid);
362 conn->data->state.proto.generic = NULL;
368 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
369 size_t len, CURLcode *err)
371 ldapconninfo *li = conn->proto.generic;
372 struct SessionHandle *data=conn->data;
373 ldapreqinfo *lr = data->state.proto.generic;
375 LDAPMessage *result = NULL;
377 BerElement *ber = NULL;
378 struct timeval tv = {0,1};
383 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
385 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
386 *err = CURLE_RECV_ERROR;
397 for (ent = ldap_first_message(li->ld, result); ent;
398 ent = ldap_next_message(li->ld, ent)) {
399 struct berval bv, *bvals, **bvp = &bvals;
400 int binary = 0, msgtype;
402 msgtype = ldap_msgtype(ent);
403 if (msgtype == LDAP_RES_SEARCH_RESULT) {
406 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
408 failf(data, "LDAP local: search ldap_parse_result %s", ldap_err2string(rc));
409 *err = CURLE_LDAP_SEARCH_FAILED;
410 } else if (code && code != LDAP_SIZELIMIT_EXCEEDED) {
411 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
413 *err = CURLE_LDAP_SEARCH_FAILED;
416 if (code == LDAP_SIZELIMIT_EXCEEDED)
417 infof(data, "There are more than %d entries\n", lr->nument);
418 data->req.size = data->req.bytecount;
425 } else if (msgtype != LDAP_RES_SEARCH_ENTRY) {
430 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
431 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
432 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
433 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
434 data->req.bytecount += bv.bv_len + 5;
436 for (rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
438 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
441 if (bv.bv_val == NULL) break;
443 if (bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
446 for (i=0; bvals[i].bv_val != NULL; i++) {
448 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
449 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
450 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
451 data->req.bytecount += bv.bv_len + 2;
454 /* check for leading or trailing whitespace */
455 if (isspace(bvals[i].bv_val[0]) ||
456 isspace(bvals[i].bv_val[bvals[i].bv_len-1])) {
459 /* check for unprintable characters */
461 for (j=0; j<bvals[i].bv_len; j++)
462 if (!isprint(bvals[i].bv_val[j])) {
468 if (binary || binval) {
470 /* Binary value, encode to base64. */
471 size_t val_b64_sz = Curl_base64_encode(data,
475 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
476 data->req.bytecount += 2;
478 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
480 data->req.bytecount += val_b64_sz;
483 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
484 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
486 data->req.bytecount += bvals[i].bv_len + 1;
488 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
489 data->req.bytecount++;
492 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
493 data->req.bytecount++;
495 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
496 data->req.bytecount++;
499 ldap_msgfree(result);
504 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
506 sbiod->sbiod_pvt = arg;
511 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
513 sbiod->sbiod_pvt = NULL;
517 /* We don't need to do anything because libcurl does it already */
519 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
526 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
529 if (opt == LBER_SB_OPT_DATA_READY) {
530 struct connectdata *conn = sbiod->sbiod_pvt;
531 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
537 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
539 struct connectdata *conn = sbiod->sbiod_pvt;
540 ldapconninfo *li = conn->proto.generic;
542 CURLcode err = CURLE_RECV_ERROR;
544 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
545 if (ret < 0 && err == CURLE_AGAIN) {
546 SET_SOCKERRNO(EWOULDBLOCK);
552 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
554 struct connectdata *conn = sbiod->sbiod_pvt;
555 ldapconninfo *li = conn->proto.generic;
557 CURLcode err = CURLE_SEND_ERROR;
559 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
560 if (ret < 0 && err == CURLE_AGAIN) {
561 SET_SOCKERRNO(EWOULDBLOCK);
566 static Sockbuf_IO ldapsb_tls =
576 #endif /* USE_OPENLDAP */