1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011, 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 ***************************************************************************/
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>
46 #include "curl_ldap.h"
47 #include "curl_memory.h"
48 #include "curl_base64.h"
49 #include "http_proxy.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, /* 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, /* perform_getsock */
111 ldap_disconnect, /* disconnect */
112 ZERO_NULL, /* readwrite */
113 PORT_LDAPS, /* defport */
114 CURLPROTO_LDAP, /* protocol */
115 PROTOPT_SSL /* flags */
119 static const char *url_errs[] = {
123 "unrecognized scheme",
124 "unbalanced delimiter",
127 "bad or missing attributes",
128 "bad or missing scope",
129 "bad or missing filter",
130 "bad or missing extensions"
133 typedef struct ldapconninfo {
135 Curl_recv *recv; /* for stacking SSL handler */
144 typedef struct ldapreqinfo {
149 static CURLcode ldap_setup(struct connectdata *conn)
153 struct SessionHandle *data=conn->data;
157 rc = ldap_url_parse(data->change.url, &lud);
158 if(rc != LDAP_URL_SUCCESS) {
159 const char *msg = "url parsing problem";
160 status = CURLE_URL_MALFORMAT;
161 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
162 if(rc == LDAP_URL_ERR_MEM)
163 status = CURLE_OUT_OF_MEMORY;
166 failf(conn->data, "LDAP local: %s", msg);
169 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
170 ldap_free_urldesc(lud);
172 li = calloc(1, sizeof(ldapconninfo));
174 conn->proto.generic = li;
175 conn->bits.close = FALSE;
177 * - provide option to choose SASL Binds instead of Simple
183 static Sockbuf_IO ldapsb_tls;
186 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
188 ldapconninfo *li = conn->proto.generic;
189 struct SessionHandle *data=conn->data;
190 int rc, proto = LDAP_VERSION3;
191 char hosturl[1024], *ptr;
193 strcpy(hosturl, "ldap");
195 if(conn->handler->flags & PROTOPT_SSL)
197 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
198 conn->host.name, conn->remote_port);
200 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
202 failf(data, "LDAP local: Cannot connect to %s, %s",
203 hosturl, ldap_err2string(rc));
204 return CURLE_COULDNT_CONNECT;
207 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
209 if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
210 /* for LDAP over HTTP proxy */
211 struct HTTP http_proxy;
212 ldapconninfo *li_save;
216 /* We want "seamless" LDAP operations through HTTP proxy tunnel */
218 /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the member
219 * conn->proto.http; we want LDAP through HTTP and we have to change the
220 * member temporarily for connecting to the HTTP proxy. After
221 * Curl_proxyCONNECT we have to set back the member to the original struct
224 li_save = data->state.proto.generic;
225 memset(&http_proxy, 0, sizeof(http_proxy));
226 data->state.proto.http = &http_proxy;
227 result = Curl_proxyCONNECT(conn, FIRSTSOCKET,
228 conn->host.name, conn->remote_port);
230 data->state.proto.generic = li_save;
232 if(CURLE_OK != result)
237 if(conn->handler->flags & PROTOPT_SSL) {
239 if(data->state.used_interface == Curl_if_easy) {
240 res = Curl_ssl_connect(conn, FIRSTSOCKET);
246 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
253 if(data->state.used_interface == Curl_if_easy)
254 return ldap_connecting(conn, done);
259 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
261 ldapconninfo *li = conn->proto.generic;
262 struct SessionHandle *data=conn->data;
263 LDAPMessage *result = NULL;
264 struct timeval tv = {0,1}, *tvp;
269 if(conn->handler->flags & PROTOPT_SSL) {
270 /* Is the SSL handshake complete yet? */
272 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
274 if(res || !li->ssldone)
277 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
280 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
281 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
283 li->recv = conn->recv[FIRSTSOCKET];
284 li->send = conn->send[FIRSTSOCKET];
289 if(data->state.used_interface == Curl_if_easy)
290 tvp = NULL; /* let ldap_result block indefinitely */
297 struct berval passwd;
299 if(conn->bits.user_passwd) {
301 passwd.bv_val = conn->passwd;
302 passwd.bv_len = strlen(passwd.bv_val);
306 passwd.bv_val = NULL;
309 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
310 NULL, NULL, &li->msgid);
312 return CURLE_LDAP_CANNOT_BIND;
318 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
320 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
321 return CURLE_LDAP_CANNOT_BIND;
327 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
329 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
330 return CURLE_LDAP_CANNOT_BIND;
332 /* Try to fallback to LDAPv2? */
333 if(err == LDAP_PROTOCOL_ERROR) {
335 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
336 if(proto == LDAP_VERSION3) {
338 proto = LDAP_VERSION2;
339 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
346 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
348 return CURLE_LOGIN_DENIED;
350 conn->recv[FIRSTSOCKET] = ldap_recv;
355 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
357 ldapconninfo *li = conn->proto.generic;
358 (void) dead_connection;
362 ldap_unbind_ext(li->ld, NULL, NULL);
365 conn->proto.generic = NULL;
371 static CURLcode ldap_do(struct connectdata *conn, bool *done)
373 ldapconninfo *li = conn->proto.generic;
375 CURLcode status = CURLE_OK;
377 LDAPURLDesc *ludp = NULL;
379 struct SessionHandle *data=conn->data;
381 conn->bits.close = FALSE;
383 infof(data, "LDAP local: %s\n", data->change.url);
385 rc = ldap_url_parse(data->change.url, &ludp);
386 if(rc != LDAP_URL_SUCCESS) {
387 const char *msg = "url parsing problem";
388 status = CURLE_URL_MALFORMAT;
389 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
390 if(rc == LDAP_URL_ERR_MEM)
391 status = CURLE_OUT_OF_MEMORY;
394 failf(conn->data, "LDAP local: %s", msg);
398 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
399 ludp->lud_filter, ludp->lud_attrs, 0,
400 NULL, NULL, NULL, 0, &msgid);
401 ldap_free_urldesc(ludp);
402 if(rc != LDAP_SUCCESS) {
403 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
404 return CURLE_LDAP_SEARCH_FAILED;
406 lr = calloc(1,sizeof(ldapreqinfo));
408 data->state.proto.generic = lr;
409 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
414 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
417 ldapreqinfo *lr = conn->data->state.proto.generic;
422 /* if there was a search in progress, abandon it */
424 ldapconninfo *li = conn->proto.generic;
425 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
428 conn->data->state.proto.generic = NULL;
434 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
435 size_t len, CURLcode *err)
437 ldapconninfo *li = conn->proto.generic;
438 struct SessionHandle *data=conn->data;
439 ldapreqinfo *lr = data->state.proto.generic;
441 LDAPMessage *result = NULL;
443 BerElement *ber = NULL;
444 struct timeval tv = {0,1};
449 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
451 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
452 *err = CURLE_RECV_ERROR;
463 for(ent = ldap_first_message(li->ld, result); ent;
464 ent = ldap_next_message(li->ld, ent)) {
465 struct berval bv, *bvals, **bvp = &bvals;
466 int binary = 0, msgtype;
468 msgtype = ldap_msgtype(ent);
469 if(msgtype == LDAP_RES_SEARCH_RESULT) {
472 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
474 failf(data, "LDAP local: search ldap_parse_result %s",
475 ldap_err2string(rc));
476 *err = CURLE_LDAP_SEARCH_FAILED;
478 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
479 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
481 *err = CURLE_LDAP_SEARCH_FAILED;
485 if(code == LDAP_SIZELIMIT_EXCEEDED)
486 infof(data, "There are more than %d entries\n", lr->nument);
487 data->req.size = data->req.bytecount;
495 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
499 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
501 /* TODO: verify that this is really how this return code should be
503 *err = CURLE_RECV_ERROR;
506 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
507 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
508 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
509 data->req.bytecount += bv.bv_len + 5;
511 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
513 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
516 if(bv.bv_val == NULL) break;
518 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
523 for(i=0; bvals[i].bv_val != NULL; i++) {
525 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
526 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
528 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
529 data->req.bytecount += bv.bv_len + 2;
532 /* check for leading or trailing whitespace */
533 if(ISSPACE(bvals[i].bv_val[0]) ||
534 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
537 /* check for unprintable characters */
539 for(j=0; j<bvals[i].bv_len; j++)
540 if(!ISPRINT(bvals[i].bv_val[j])) {
546 if(binary || binval) {
548 /* Binary value, encode to base64. */
549 size_t val_b64_sz = Curl_base64_encode(data,
553 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
554 data->req.bytecount += 2;
556 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
558 data->req.bytecount += val_b64_sz;
562 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
563 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
565 data->req.bytecount += bvals[i].bv_len + 1;
567 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
568 data->req.bytecount++;
571 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
572 data->req.bytecount++;
574 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
575 data->req.bytecount++;
578 ldap_msgfree(result);
584 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
586 sbiod->sbiod_pvt = arg;
591 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
593 sbiod->sbiod_pvt = NULL;
597 /* We don't need to do anything because libcurl does it already */
599 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
606 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
609 if(opt == LBER_SB_OPT_DATA_READY) {
610 struct connectdata *conn = sbiod->sbiod_pvt;
611 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
617 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
619 struct connectdata *conn = sbiod->sbiod_pvt;
620 ldapconninfo *li = conn->proto.generic;
622 CURLcode err = CURLE_RECV_ERROR;
624 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
625 if(ret < 0 && err == CURLE_AGAIN) {
626 SET_SOCKERRNO(EWOULDBLOCK);
632 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
634 struct connectdata *conn = sbiod->sbiod_pvt;
635 ldapconninfo *li = conn->proto.generic;
637 CURLcode err = CURLE_SEND_ERROR;
639 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
640 if(ret < 0 && err == CURLE_AGAIN) {
641 SET_SOCKERRNO(EWOULDBLOCK);
646 static Sockbuf_IO ldapsb_tls =
657 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */