1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011 - 2016, 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 https://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 ***************************************************************************/
24 #include "curl_setup.h"
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>
44 #include "vtls/vtls.h"
46 #include "curl_ldap.h"
47 #include "curl_base64.h"
49 /* The last 3 #include files should be in this order */
50 #include "curl_printf.h"
51 #include "curl_memory.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,
60 static CURLcode ldap_setup_connection(struct connectdata *conn);
61 static CURLcode ldap_do(struct connectdata *conn, bool *done);
62 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
63 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
64 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
65 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
67 static Curl_recv ldap_recv;
70 * LDAP protocol handler.
73 const struct Curl_handler Curl_handler_ldap = {
75 ldap_setup_connection, /* setup_connection */
78 ZERO_NULL, /* do_more */
79 ldap_connect, /* connect_it */
80 ldap_connecting, /* connecting */
81 ZERO_NULL, /* doing */
82 ZERO_NULL, /* proto_getsock */
83 ZERO_NULL, /* doing_getsock */
84 ZERO_NULL, /* domore_getsock */
85 ZERO_NULL, /* perform_getsock */
86 ldap_disconnect, /* disconnect */
87 ZERO_NULL, /* readwrite */
88 PORT_LDAP, /* defport */
89 CURLPROTO_LDAP, /* protocol */
90 PROTOPT_NONE /* flags */
95 * LDAPS protocol handler.
98 const struct Curl_handler Curl_handler_ldaps = {
100 ldap_setup_connection, /* setup_connection */
102 ldap_done, /* done */
103 ZERO_NULL, /* do_more */
104 ldap_connect, /* connect_it */
105 ldap_connecting, /* connecting */
106 ZERO_NULL, /* doing */
107 ZERO_NULL, /* proto_getsock */
108 ZERO_NULL, /* doing_getsock */
109 ZERO_NULL, /* domore_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_connection(struct connectdata *conn)
153 struct Curl_easy *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 return CURLE_OUT_OF_MEMORY;
176 conn->proto.generic = li;
177 connkeep(conn, "OpenLDAP default");
179 * - provide option to choose SASL Binds instead of Simple
185 static Sockbuf_IO ldapsb_tls;
188 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
190 ldapconninfo *li = conn->proto.generic;
191 struct Curl_easy *data = conn->data;
192 int rc, proto = LDAP_VERSION3;
198 strcpy(hosturl, "ldap");
200 if(conn->handler->flags & PROTOPT_SSL)
202 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
203 conn->host.name, conn->remote_port);
205 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
207 failf(data, "LDAP local: Cannot connect to %s, %s",
208 hosturl, ldap_err2string(rc));
209 return CURLE_COULDNT_CONNECT;
212 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
215 if(conn->handler->flags & PROTOPT_SSL) {
217 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
226 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
228 ldapconninfo *li = conn->proto.generic;
229 struct Curl_easy *data = conn->data;
230 LDAPMessage *msg = NULL;
231 struct timeval tv = {0, 1}, *tvp;
236 if(conn->handler->flags & PROTOPT_SSL) {
237 /* Is the SSL handshake complete yet? */
239 CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
241 if(result || !li->ssldone)
245 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
248 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
249 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
251 li->recv = conn->recv[FIRSTSOCKET];
252 li->send = conn->send[FIRSTSOCKET];
262 struct berval passwd;
264 if(conn->bits.user_passwd) {
266 passwd.bv_val = conn->passwd;
267 passwd.bv_len = strlen(passwd.bv_val);
271 passwd.bv_val = NULL;
274 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
275 NULL, NULL, &li->msgid);
277 return CURLE_LDAP_CANNOT_BIND;
283 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
285 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
286 return CURLE_LDAP_CANNOT_BIND;
293 rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
295 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
296 return CURLE_LDAP_CANNOT_BIND;
299 /* Try to fallback to LDAPv2? */
300 if(err == LDAP_PROTOCOL_ERROR) {
302 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
303 if(proto == LDAP_VERSION3) {
308 proto = LDAP_VERSION2;
309 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
316 failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
320 return CURLE_LOGIN_DENIED;
325 conn->recv[FIRSTSOCKET] = ldap_recv;
331 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
333 ldapconninfo *li = conn->proto.generic;
334 (void) dead_connection;
338 ldap_unbind_ext(li->ld, NULL, NULL);
341 conn->proto.generic = NULL;
347 static CURLcode ldap_do(struct connectdata *conn, bool *done)
349 ldapconninfo *li = conn->proto.generic;
351 CURLcode status = CURLE_OK;
353 LDAPURLDesc *ludp = NULL;
355 struct Curl_easy *data=conn->data;
357 connkeep(conn, "OpenLDAP do");
359 infof(data, "LDAP local: %s\n", data->change.url);
361 rc = ldap_url_parse(data->change.url, &ludp);
362 if(rc != LDAP_URL_SUCCESS) {
363 const char *msg = "url parsing problem";
364 status = CURLE_URL_MALFORMAT;
365 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
366 if(rc == LDAP_URL_ERR_MEM)
367 status = CURLE_OUT_OF_MEMORY;
370 failf(conn->data, "LDAP local: %s", msg);
374 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
375 ludp->lud_filter, ludp->lud_attrs, 0,
376 NULL, NULL, NULL, 0, &msgid);
377 ldap_free_urldesc(ludp);
378 if(rc != LDAP_SUCCESS) {
379 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
380 return CURLE_LDAP_SEARCH_FAILED;
382 lr = calloc(1, sizeof(ldapreqinfo));
384 return CURLE_OUT_OF_MEMORY;
386 data->req.protop = lr;
387 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
392 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
395 ldapreqinfo *lr = conn->data->req.protop;
401 /* if there was a search in progress, abandon it */
403 ldapconninfo *li = conn->proto.generic;
404 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
407 conn->data->req.protop = NULL;
414 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
415 size_t len, CURLcode *err)
417 ldapconninfo *li = conn->proto.generic;
418 struct Curl_easy *data = conn->data;
419 ldapreqinfo *lr = data->req.protop;
421 LDAPMessage *msg = NULL;
423 BerElement *ber = NULL;
424 struct timeval tv = {0, 1};
430 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
432 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
433 *err = CURLE_RECV_ERROR;
444 for(ent = ldap_first_message(li->ld, msg); ent;
445 ent = ldap_next_message(li->ld, ent)) {
446 struct berval bv, *bvals, **bvp = &bvals;
447 int binary = 0, msgtype;
450 msgtype = ldap_msgtype(ent);
451 if(msgtype == LDAP_RES_SEARCH_RESULT) {
454 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
456 failf(data, "LDAP local: search ldap_parse_result %s",
457 ldap_err2string(rc));
458 *err = CURLE_LDAP_SEARCH_FAILED;
460 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
461 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
463 *err = CURLE_LDAP_SEARCH_FAILED;
467 if(code == LDAP_SIZELIMIT_EXCEEDED)
468 infof(data, "There are more than %d entries\n", lr->nument);
469 data->req.size = data->req.bytecount;
477 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
481 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
483 /* TODO: verify that this is really how this return code should be
485 *err = CURLE_RECV_ERROR;
488 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
494 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
501 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
506 data->req.bytecount += bv.bv_len + 5;
508 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
510 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
513 if(bv.bv_val == NULL) break;
515 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
520 for(i=0; bvals[i].bv_val != NULL; i++) {
522 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
528 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
535 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
540 data->req.bytecount += bv.bv_len + 2;
543 /* check for leading or trailing whitespace */
544 if(ISSPACE(bvals[i].bv_val[0]) ||
545 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
548 /* check for unprintable characters */
550 for(j=0; j<bvals[i].bv_len; j++)
551 if(!ISPRINT(bvals[i].bv_val[j])) {
557 if(binary || binval) {
558 char *val_b64 = NULL;
559 size_t val_b64_sz = 0;
560 /* Binary value, encode to base64. */
561 CURLcode error = Curl_base64_encode(data,
573 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY,
580 data->req.bytecount += 2;
582 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
589 data->req.bytecount += val_b64_sz;
593 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
599 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
606 data->req.bytecount += bvals[i].bv_len + 1;
608 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
614 data->req.bytecount++;
617 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
622 data->req.bytecount++;
624 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
629 data->req.bytecount++;
638 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
640 sbiod->sbiod_pvt = arg;
645 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
647 sbiod->sbiod_pvt = NULL;
651 /* We don't need to do anything because libcurl does it already */
653 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
660 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
663 if(opt == LBER_SB_OPT_DATA_READY) {
664 struct connectdata *conn = sbiod->sbiod_pvt;
665 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
671 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
673 struct connectdata *conn = sbiod->sbiod_pvt;
674 ldapconninfo *li = conn->proto.generic;
676 CURLcode err = CURLE_RECV_ERROR;
678 ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
679 if(ret < 0 && err == CURLE_AGAIN) {
680 SET_SOCKERRNO(EWOULDBLOCK);
686 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
688 struct connectdata *conn = sbiod->sbiod_pvt;
689 ldapconninfo *li = conn->proto.generic;
691 CURLcode err = CURLE_SEND_ERROR;
693 ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
694 if(ret < 0 && err == CURLE_AGAIN) {
695 SET_SOCKERRNO(EWOULDBLOCK);
700 static Sockbuf_IO ldapsb_tls =
711 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */