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>
41 extern int ldap_pvt_url_scheme2proto(const char *);
42 extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url, LDAP **ld);
45 static CURLcode ldap_setup(struct connectdata *conn);
46 static CURLcode ldap_do(struct connectdata *conn, bool *done);
47 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
48 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
49 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
50 static CURLcode ldap_disconnect(struct connectdata *conn);
52 static Curl_recv ldap_recv;
55 * LDAP protocol handler.
58 const struct Curl_handler Curl_handler_ldap = {
60 ldap_setup, /* setup_connection */
63 ZERO_NULL, /* do_more */
64 ldap_connect, /* connect_it */
65 ldap_connecting, /* connecting */
66 ZERO_NULL, /* doing */
67 ZERO_NULL, /* proto_getsock */
68 ZERO_NULL, /* doing_getsock */
69 ZERO_NULL, /* perform_getsock */
70 ldap_disconnect, /* disconnect */
71 PORT_LDAP, /* defport */
72 PROT_LDAP /* protocol */
77 * LDAPS protocol handler.
80 const struct Curl_handler Curl_handler_ldaps = {
82 ldap_setup, /* setup_connection */
85 ZERO_NULL, /* do_more */
86 ldap_connect, /* connect_it */
87 ldap_connecting, /* connecting */
88 ZERO_NULL, /* doing */
89 ZERO_NULL, /* proto_getsock */
90 ZERO_NULL, /* doing_getsock */
91 ZERO_NULL, /* perform_getsock */
92 ldap_disconnect, /* disconnect */
93 PORT_LDAPS, /* defport */
94 PROT_LDAP | PROT_SSL /* protocol */
98 static const char *url_errs[] = {
102 "unrecognized scheme",
103 "unbalanced delimiter",
106 "bad or missing attributes",
107 "bad or missing scope",
108 "bad or missing filter",
109 "bad or missing extensions"
112 typedef struct ldapconninfo {
114 Curl_recv *recv; /* for stacking SSL handler */
123 typedef struct ldapreqinfo {
128 static CURLcode ldap_setup(struct connectdata *conn)
132 struct SessionHandle *data=conn->data;
136 rc = ldap_url_parse(data->change.url, &lud);
137 if (rc != LDAP_URL_SUCCESS) {
138 const char *msg = "url parsing problem";
139 status = CURLE_URL_MALFORMAT;
140 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
141 if (rc == LDAP_URL_ERR_MEM)
142 status = CURLE_OUT_OF_MEMORY;
145 failf(conn->data, "LDAP local: %s", msg);
148 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
149 ldap_free_urldesc(lud);
151 li = calloc(1, sizeof(ldapconninfo));
153 conn->proto.generic = li;
155 * - provide option to choose SASL Binds instead of Simple
161 static Sockbuf_IO ldapsb_tls;
164 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
166 ldapconninfo *li = conn->proto.generic;
167 struct SessionHandle *data=conn->data;
168 int rc, proto = LDAP_VERSION3;
169 char hosturl[1024], *ptr;
171 strcpy(hosturl, "ldap");
173 if (conn->protocol & PROT_SSL)
175 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
176 conn->host.name, conn->port);
178 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
180 failf(data, "LDAP local: Cannot connect to %s, %s",
181 hosturl, ldap_err2string(rc));
182 return CURLE_COULDNT_CONNECT;
185 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
188 if (conn->protocol & PROT_SSL) {
190 if (data->state.used_interface == Curl_if_easy) {
191 res = Curl_ssl_connect(conn, FIRSTSOCKET);
196 res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
203 if (data->state.used_interface == Curl_if_easy)
204 return ldap_connecting(conn, done);
209 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
211 ldapconninfo *li = conn->proto.generic;
212 struct SessionHandle *data=conn->data;
213 LDAPMessage *result = NULL;
214 struct timeval tv = {0,1}, *tvp;
219 if (conn->protocol & PROT_SSL) {
220 /* Is the SSL handshake complete yet? */
222 CURLcode res = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
223 if (res || !li->ssldone)
226 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
229 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
230 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
232 li->recv = conn->recv[FIRSTSOCKET];
233 li->send = conn->send[FIRSTSOCKET];
238 if (data->state.used_interface == Curl_if_easy)
239 tvp = NULL; /* let ldap_result block indefinitely */
246 struct berval passwd;
248 if (conn->bits.user_passwd) {
250 passwd.bv_val = conn->passwd;
251 passwd.bv_len = strlen(passwd.bv_val);
254 passwd.bv_val = NULL;
257 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
258 NULL, NULL, &li->msgid);
260 return CURLE_LDAP_CANNOT_BIND;
266 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &result);
268 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
269 return CURLE_LDAP_CANNOT_BIND;
275 rc = ldap_parse_result(li->ld, result, &err, NULL, &info, NULL, NULL, 1);
277 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
278 return CURLE_LDAP_CANNOT_BIND;
280 /* Try to fallback to LDAPv2? */
281 if (err == LDAP_PROTOCOL_ERROR) {
283 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
284 if (proto == LDAP_VERSION3) {
286 proto = LDAP_VERSION2;
287 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
294 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
296 return CURLE_LOGIN_DENIED;
298 conn->recv[FIRSTSOCKET] = ldap_recv;
303 static CURLcode ldap_disconnect(struct connectdata *conn)
305 ldapconninfo *li = conn->proto.generic;
309 ldap_unbind_ext(li->ld, NULL, NULL);
312 conn->proto.generic = NULL;
318 static CURLcode ldap_do(struct connectdata *conn, bool *done)
320 ldapconninfo *li = conn->proto.generic;
322 CURLcode status = CURLE_OK;
324 LDAPURLDesc *ludp = NULL;
326 struct SessionHandle *data=conn->data;
328 conn->bits.close = FALSE;
330 infof(data, "LDAP local: %s\n", data->change.url);
332 rc = ldap_url_parse(data->change.url, &ludp);
333 if (rc != LDAP_URL_SUCCESS) {
334 const char *msg = "url parsing problem";
335 status = CURLE_URL_MALFORMAT;
336 if (rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
337 if (rc == LDAP_URL_ERR_MEM)
338 status = CURLE_OUT_OF_MEMORY;
341 failf(conn->data, "LDAP local: %s", msg);
345 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
346 ludp->lud_filter, ludp->lud_attrs, 0,
347 NULL, NULL, NULL, 0, &msgid);
348 ldap_free_urldesc(ludp);
349 if (rc != LDAP_SUCCESS) {
350 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
351 return CURLE_LDAP_SEARCH_FAILED;
353 lr = calloc(1,sizeof(ldapreqinfo));
355 data->state.proto.generic = lr;
356 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
361 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
364 ldapreqinfo *lr = conn->data->state.proto.generic;
369 /* if there was a search in progress, abandon it */
371 ldapconninfo *li = conn->proto.generic;
372 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
375 conn->data->state.proto.generic = NULL;
381 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
382 size_t len, CURLcode *err)
384 ldapconninfo *li = conn->proto.generic;
385 struct SessionHandle *data=conn->data;
386 ldapreqinfo *lr = data->state.proto.generic;
388 LDAPMessage *result = NULL;
390 BerElement *ber = NULL;
391 struct timeval tv = {0,1};
396 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &result);
398 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
399 *err = CURLE_RECV_ERROR;
410 for (ent = ldap_first_message(li->ld, result); ent;
411 ent = ldap_next_message(li->ld, ent)) {
412 struct berval bv, *bvals, **bvp = &bvals;
413 int binary = 0, msgtype;
415 msgtype = ldap_msgtype(ent);
416 if (msgtype == LDAP_RES_SEARCH_RESULT) {
419 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
421 failf(data, "LDAP local: search ldap_parse_result %s", ldap_err2string(rc));
422 *err = CURLE_LDAP_SEARCH_FAILED;
423 } else if (code && code != LDAP_SIZELIMIT_EXCEEDED) {
424 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
426 *err = CURLE_LDAP_SEARCH_FAILED;
429 if (code == LDAP_SIZELIMIT_EXCEEDED)
430 infof(data, "There are more than %d entries\n", lr->nument);
431 data->req.size = data->req.bytecount;
438 } else if (msgtype != LDAP_RES_SEARCH_ENTRY) {
443 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
444 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
445 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
446 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
447 data->req.bytecount += bv.bv_len + 5;
449 for (rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
451 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
454 if (bv.bv_val == NULL) break;
456 if (bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
459 for (i=0; bvals[i].bv_val != NULL; i++) {
461 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
462 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val, bv.bv_len);
463 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
464 data->req.bytecount += bv.bv_len + 2;
467 /* check for leading or trailing whitespace */
468 if (isspace(bvals[i].bv_val[0]) ||
469 isspace(bvals[i].bv_val[bvals[i].bv_len-1])) {
472 /* check for unprintable characters */
474 for (j=0; j<bvals[i].bv_len; j++)
475 if (!isprint(bvals[i].bv_val[j])) {
481 if (binary || binval) {
483 /* Binary value, encode to base64. */
484 size_t val_b64_sz = Curl_base64_encode(data,
488 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
489 data->req.bytecount += 2;
491 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
493 data->req.bytecount += val_b64_sz;
496 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
497 Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
499 data->req.bytecount += bvals[i].bv_len + 1;
501 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
502 data->req.bytecount++;
505 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
506 data->req.bytecount++;
508 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
509 data->req.bytecount++;
512 ldap_msgfree(result);
518 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
520 sbiod->sbiod_pvt = arg;
525 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
527 sbiod->sbiod_pvt = NULL;
531 /* We don't need to do anything because libcurl does it already */
533 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
540 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
543 if (opt == LBER_SB_OPT_DATA_READY) {
544 struct connectdata *conn = sbiod->sbiod_pvt;
545 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
551 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
553 struct connectdata *conn = sbiod->sbiod_pvt;
554 ldapconninfo *li = conn->proto.generic;
556 CURLcode err = CURLE_RECV_ERROR;
558 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
559 if (ret < 0 && err == CURLE_AGAIN) {
560 SET_SOCKERRNO(EWOULDBLOCK);
566 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
568 struct connectdata *conn = sbiod->sbiod_pvt;
569 ldapconninfo *li = conn->proto.generic;
571 CURLcode err = CURLE_SEND_ERROR;
573 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
574 if (ret < 0 && err == CURLE_AGAIN) {
575 SET_SOCKERRNO(EWOULDBLOCK);
580 static Sockbuf_IO ldapsb_tls =
591 #endif /* USE_OPENLDAP */