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, bool dead_connection);
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;
168 conn->bits.close = FALSE;
170 * - provide option to choose SASL Binds instead of Simple
176 static Sockbuf_IO ldapsb_tls;
179 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
181 ldapconninfo *li = conn->proto.generic;
182 struct SessionHandle *data=conn->data;
183 int rc, proto = LDAP_VERSION3;
184 char hosturl[1024], *ptr;
186 strcpy(hosturl, "ldap");
188 if (conn->protocol & PROT_SSL)
190 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
191 conn->host.name, conn->remote_port);
193 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
195 failf(data, "LDAP local: Cannot connect to %s, %s",
196 hosturl, ldap_err2string(rc));
197 return CURLE_COULDNT_CONNECT;
200 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
202 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_PROXY)
203 if(conn->bits.tunnel_proxy && conn->bits.httpproxy) {
204 /* for LDAP over HTTP proxy */
205 struct HTTP http_proxy;
206 ldapconninfo *li_save;
210 /* We want "seamless" LDAP operations through HTTP proxy tunnel */
212 /* Curl_proxyCONNECT is based on a pointer to a struct HTTP at the member
213 * conn->proto.http; we want LDAP through HTTP and we have to change the
214 * member temporarily for connecting to the HTTP proxy. After
215 * Curl_proxyCONNECT we have to set back the member to the original struct
218 li_save = data->state.proto.generic;
219 memset(&http_proxy, 0, sizeof(http_proxy));
220 data->state.proto.http = &http_proxy;
221 result = Curl_proxyCONNECT(conn, FIRSTSOCKET,
222 conn->host.name, conn->remote_port);
224 data->state.proto.generic = li_save;
226 if(CURLE_OK != result)
229 #endif /* !CURL_DISABLE_HTTP && !CURL_DISABLE_PROXY */
232 if (conn->protocol & PROT_SSL) {
234 if (data->state.used_interface == Curl_if_easy) {
235 res = Curl_ssl_connect(conn, FIRSTSOCKET);
240 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
247 if (data->state.used_interface == Curl_if_easy)
248 return ldap_connecting(conn, done);
253 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
255 ldapconninfo *li = conn->proto.generic;
256 struct SessionHandle *data=conn->data;
257 LDAPMessage *result = NULL;
258 struct timeval tv = {0,1}, *tvp;
263 if (conn->protocol & PROT_SSL) {
264 /* Is the SSL handshake complete yet? */
266 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
267 if (res || !li->ssldone)
270 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
273 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
274 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
276 li->recv = conn->recv[FIRSTSOCKET];
277 li->send = conn->send[FIRSTSOCKET];
282 if (data->state.used_interface == Curl_if_easy)
283 tvp = NULL; /* let ldap_result block indefinitely */
290 struct berval passwd;
292 if (conn->bits.user_passwd) {
294 passwd.bv_val = conn->passwd;
295 passwd.bv_len = strlen(passwd.bv_val);
298 passwd.bv_val = NULL;
301 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
302 NULL, NULL, &li->msgid);
304 return CURLE_LDAP_CANNOT_BIND;
310 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
312 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
313 return CURLE_LDAP_CANNOT_BIND;
319 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
321 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
322 return CURLE_LDAP_CANNOT_BIND;
324 /* Try to fallback to LDAPv2? */
325 if (err == LDAP_PROTOCOL_ERROR) {
327 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
328 if (proto == LDAP_VERSION3) {
330 proto = LDAP_VERSION2;
331 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
338 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
340 return CURLE_LOGIN_DENIED;
342 conn->recv[FIRSTSOCKET] = ldap_recv;
347 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
349 ldapconninfo *li = conn->proto.generic;
350 (void) dead_connection;
354 ldap_unbind_ext(li->ld, NULL, NULL);
357 conn->proto.generic = NULL;
363 static CURLcode ldap_do(struct connectdata *conn, bool *done)
365 ldapconninfo *li = conn->proto.generic;
367 CURLcode status = CURLE_OK;
369 LDAPURLDesc *ludp = NULL;
371 struct SessionHandle *data=conn->data;
373 conn->bits.close = FALSE;
375 infof(data, "LDAP local: %s\n", data->change.url);
377 rc = ldap_url_parse(data->change.url, &ludp);
378 if (rc != LDAP_URL_SUCCESS) {
379 const char *msg = "url parsing problem";
380 status = CURLE_URL_MALFORMAT;
381 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
382 if (rc == LDAP_URL_ERR_MEM)
383 status = CURLE_OUT_OF_MEMORY;
386 failf(conn->data, "LDAP local: %s", msg);
390 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
391 ludp->lud_filter, ludp->lud_attrs, 0,
392 NULL, NULL, NULL, 0, &msgid);
393 ldap_free_urldesc(ludp);
394 if (rc != LDAP_SUCCESS) {
395 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
396 return CURLE_LDAP_SEARCH_FAILED;
398 lr = calloc(1,sizeof(ldapreqinfo));
400 data->state.proto.generic = lr;
401 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
406 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
409 ldapreqinfo *lr = conn->data->state.proto.generic;
414 /* if there was a search in progress, abandon it */
416 ldapconninfo *li = conn->proto.generic;
417 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
420 conn->data->state.proto.generic = NULL;
426 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
427 size_t len, CURLcode *err)
429 ldapconninfo *li = conn->proto.generic;
430 struct SessionHandle *data=conn->data;
431 ldapreqinfo *lr = data->state.proto.generic;
433 LDAPMessage *result = NULL;
435 BerElement *ber = NULL;
436 struct timeval tv = {0,1};
441 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
443 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
444 *err = CURLE_RECV_ERROR;
455 for (ent = ldap_first_message(li->ld, result); ent;
456 ent = ldap_next_message(li->ld, ent)) {
457 struct berval bv, *bvals, **bvp = &bvals;
458 int binary = 0, msgtype;
460 msgtype = ldap_msgtype(ent);
461 if (msgtype == LDAP_RES_SEARCH_RESULT) {
464 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
466 failf(data, "LDAP local: search ldap_parse_result %s", ldap_err2string(rc));
467 *err = CURLE_LDAP_SEARCH_FAILED;
468 } else if (code && code != LDAP_SIZELIMIT_EXCEEDED) {
469 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
471 *err = CURLE_LDAP_SEARCH_FAILED;
474 if (code == LDAP_SIZELIMIT_EXCEEDED)
475 infof(data, "There are more than %d entries\n", lr->nument);
476 data->req.size = data->req.bytecount;
483 } else if (msgtype != LDAP_RES_SEARCH_ENTRY) {
488 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
489 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
490 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
491 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
492 data->req.bytecount += bv.bv_len + 5;
494 for (rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
496 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
499 if (bv.bv_val == NULL) break;
501 if (bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
506 for (i=0; bvals[i].bv_val != NULL; i++) {
508 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
509 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
510 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
511 data->req.bytecount += bv.bv_len + 2;
514 /* check for leading or trailing whitespace */
515 if (ISSPACE(bvals[i].bv_val[0]) ||
516 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1])) {
519 /* check for unprintable characters */
521 for (j=0; j<bvals[i].bv_len; j++)
522 if (!ISPRINT(bvals[i].bv_val[j])) {
528 if (binary || binval) {
530 /* Binary value, encode to base64. */
531 size_t val_b64_sz = Curl_base64_encode(data,
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;
543 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
544 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
546 data->req.bytecount += bvals[i].bv_len + 1;
548 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
549 data->req.bytecount++;
552 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
553 data->req.bytecount++;
555 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
556 data->req.bytecount++;
559 ldap_msgfree(result);
565 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
567 sbiod->sbiod_pvt = arg;
572 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
574 sbiod->sbiod_pvt = NULL;
578 /* We don't need to do anything because libcurl does it already */
580 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
587 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
590 if (opt == LBER_SB_OPT_DATA_READY) {
591 struct connectdata *conn = sbiod->sbiod_pvt;
592 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
598 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
600 struct connectdata *conn = sbiod->sbiod_pvt;
601 ldapconninfo *li = conn->proto.generic;
603 CURLcode err = CURLE_RECV_ERROR;
605 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
606 if (ret < 0 && err == CURLE_AGAIN) {
607 SET_SOCKERRNO(EWOULDBLOCK);
613 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
615 struct connectdata *conn = sbiod->sbiod_pvt;
616 ldapconninfo *li = conn->proto.generic;
618 CURLcode err = CURLE_SEND_ERROR;
620 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
621 if (ret < 0 && err == CURLE_AGAIN) {
622 SET_SOCKERRNO(EWOULDBLOCK);
627 static Sockbuf_IO ldapsb_tls =
638 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */