1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
9 * Copyright (C) 2011 - 2018, 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 * Uncommenting this will enable the built-in debug logging of the openldap
56 * library. The debug log level can be set using the CURL_OPENLDAP_TRACE
57 * environment variable. The debug output is written to stderr.
59 * The library supports the following debug flags:
60 * LDAP_DEBUG_NONE 0x0000
61 * LDAP_DEBUG_TRACE 0x0001
62 * LDAP_DEBUG_CONSTRUCT 0x0002
63 * LDAP_DEBUG_DESTROY 0x0004
64 * LDAP_DEBUG_PARAMETER 0x0008
65 * LDAP_DEBUG_ANY 0xffff
67 * For example, use CURL_OPENLDAP_TRACE=0 for no debug,
68 * CURL_OPENLDAP_TRACE=2 for LDAP_DEBUG_CONSTRUCT messages only,
69 * CURL_OPENLDAP_TRACE=65535 for all debug message levels.
71 /* #define CURL_OPENLDAP_DEBUG */
74 extern int ldap_pvt_url_scheme2proto(const char *);
75 extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
79 static CURLcode ldap_setup_connection(struct connectdata *conn);
80 static CURLcode ldap_do(struct connectdata *conn, bool *done);
81 static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
82 static CURLcode ldap_connect(struct connectdata *conn, bool *done);
83 static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
84 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
86 static Curl_recv ldap_recv;
89 * LDAP protocol handler.
92 const struct Curl_handler Curl_handler_ldap = {
94 ldap_setup_connection, /* setup_connection */
97 ZERO_NULL, /* do_more */
98 ldap_connect, /* connect_it */
99 ldap_connecting, /* connecting */
100 ZERO_NULL, /* doing */
101 ZERO_NULL, /* proto_getsock */
102 ZERO_NULL, /* doing_getsock */
103 ZERO_NULL, /* domore_getsock */
104 ZERO_NULL, /* perform_getsock */
105 ldap_disconnect, /* disconnect */
106 ZERO_NULL, /* readwrite */
107 ZERO_NULL, /* connection_check */
108 PORT_LDAP, /* defport */
109 CURLPROTO_LDAP, /* protocol */
110 PROTOPT_NONE /* flags */
115 * LDAPS protocol handler.
118 const struct Curl_handler Curl_handler_ldaps = {
119 "LDAPS", /* scheme */
120 ldap_setup_connection, /* setup_connection */
122 ldap_done, /* done */
123 ZERO_NULL, /* do_more */
124 ldap_connect, /* connect_it */
125 ldap_connecting, /* connecting */
126 ZERO_NULL, /* doing */
127 ZERO_NULL, /* proto_getsock */
128 ZERO_NULL, /* doing_getsock */
129 ZERO_NULL, /* domore_getsock */
130 ZERO_NULL, /* perform_getsock */
131 ldap_disconnect, /* disconnect */
132 ZERO_NULL, /* readwrite */
133 ZERO_NULL, /* connection_check */
134 PORT_LDAPS, /* defport */
135 CURLPROTO_LDAP, /* protocol */
136 PROTOPT_SSL /* flags */
140 static const char *url_errs[] = {
144 "unrecognized scheme",
145 "unbalanced delimiter",
148 "bad or missing attributes",
149 "bad or missing scope",
150 "bad or missing filter",
151 "bad or missing extensions"
154 typedef struct ldapconninfo {
156 Curl_recv *recv; /* for stacking SSL handler */
165 typedef struct ldapreqinfo {
170 static CURLcode ldap_setup_connection(struct connectdata *conn)
174 struct Curl_easy *data = conn->data;
178 rc = ldap_url_parse(data->change.url, &lud);
179 if(rc != LDAP_URL_SUCCESS) {
180 const char *msg = "url parsing problem";
181 status = CURLE_URL_MALFORMAT;
182 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
183 if(rc == LDAP_URL_ERR_MEM)
184 status = CURLE_OUT_OF_MEMORY;
187 failf(conn->data, "LDAP local: %s", msg);
190 proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
191 ldap_free_urldesc(lud);
193 li = calloc(1, sizeof(ldapconninfo));
195 return CURLE_OUT_OF_MEMORY;
197 conn->proto.generic = li;
198 connkeep(conn, "OpenLDAP default");
200 * - provide option to choose SASL Binds instead of Simple
206 static Sockbuf_IO ldapsb_tls;
209 static CURLcode ldap_connect(struct connectdata *conn, bool *done)
211 ldapconninfo *li = conn->proto.generic;
212 struct Curl_easy *data = conn->data;
213 int rc, proto = LDAP_VERSION3;
219 strcpy(hosturl, "ldap");
221 if(conn->handler->flags & PROTOPT_SSL)
223 snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
224 conn->host.name, conn->remote_port);
226 #ifdef CURL_OPENLDAP_DEBUG
227 static int do_trace = 0;
228 const char *env = getenv("CURL_OPENLDAP_TRACE");
229 do_trace = (env && strtol(env, NULL, 10) > 0);
231 ldap_set_option(li->ld, LDAP_OPT_DEBUG_LEVEL, &do_trace);
235 rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
237 failf(data, "LDAP local: Cannot connect to %s, %s",
238 hosturl, ldap_err2string(rc));
239 return CURLE_COULDNT_CONNECT;
242 ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
245 if(conn->handler->flags & PROTOPT_SSL) {
247 result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
256 static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
258 ldapconninfo *li = conn->proto.generic;
259 struct Curl_easy *data = conn->data;
260 LDAPMessage *msg = NULL;
261 struct timeval tv = {0, 1}, *tvp;
266 if(conn->handler->flags & PROTOPT_SSL) {
267 /* Is the SSL handshake complete yet? */
269 CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
271 if(result || !li->ssldone)
275 /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
278 ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
279 ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
281 li->recv = conn->recv[FIRSTSOCKET];
282 li->send = conn->send[FIRSTSOCKET];
292 struct berval passwd;
294 if(conn->bits.user_passwd) {
296 passwd.bv_val = conn->passwd;
297 passwd.bv_len = strlen(passwd.bv_val);
301 passwd.bv_val = NULL;
304 rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
305 NULL, NULL, &li->msgid);
307 return CURLE_LDAP_CANNOT_BIND;
313 rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
315 failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
316 return CURLE_LDAP_CANNOT_BIND;
323 rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
325 failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
326 return CURLE_LDAP_CANNOT_BIND;
329 /* Try to fallback to LDAPv2? */
330 if(err == LDAP_PROTOCOL_ERROR) {
332 ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
333 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),
350 return CURLE_LOGIN_DENIED;
355 conn->recv[FIRSTSOCKET] = ldap_recv;
361 static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
363 ldapconninfo *li = conn->proto.generic;
364 (void) dead_connection;
368 ldap_unbind_ext(li->ld, NULL, NULL);
371 conn->proto.generic = NULL;
377 static CURLcode ldap_do(struct connectdata *conn, bool *done)
379 ldapconninfo *li = conn->proto.generic;
381 CURLcode status = CURLE_OK;
383 LDAPURLDesc *ludp = NULL;
385 struct Curl_easy *data = conn->data;
387 connkeep(conn, "OpenLDAP do");
389 infof(data, "LDAP local: %s\n", data->change.url);
391 rc = ldap_url_parse(data->change.url, &ludp);
392 if(rc != LDAP_URL_SUCCESS) {
393 const char *msg = "url parsing problem";
394 status = CURLE_URL_MALFORMAT;
395 if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
396 if(rc == LDAP_URL_ERR_MEM)
397 status = CURLE_OUT_OF_MEMORY;
400 failf(conn->data, "LDAP local: %s", msg);
404 rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
405 ludp->lud_filter, ludp->lud_attrs, 0,
406 NULL, NULL, NULL, 0, &msgid);
407 ldap_free_urldesc(ludp);
408 if(rc != LDAP_SUCCESS) {
409 failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
410 return CURLE_LDAP_SEARCH_FAILED;
412 lr = calloc(1, sizeof(ldapreqinfo));
414 return CURLE_OUT_OF_MEMORY;
416 data->req.protop = lr;
417 Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
422 static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
425 ldapreqinfo *lr = conn->data->req.protop;
431 /* if there was a search in progress, abandon it */
433 ldapconninfo *li = conn->proto.generic;
434 ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
437 conn->data->req.protop = NULL;
444 static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
445 size_t len, CURLcode *err)
447 ldapconninfo *li = conn->proto.generic;
448 struct Curl_easy *data = conn->data;
449 ldapreqinfo *lr = data->req.protop;
451 LDAPMessage *msg = NULL;
453 BerElement *ber = NULL;
454 struct timeval tv = {0, 1};
460 rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
462 failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
463 *err = CURLE_RECV_ERROR;
474 for(ent = ldap_first_message(li->ld, msg); ent;
475 ent = ldap_next_message(li->ld, ent)) {
476 struct berval bv, *bvals;
477 int binary = 0, msgtype;
480 msgtype = ldap_msgtype(ent);
481 if(msgtype == LDAP_RES_SEARCH_RESULT) {
484 rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
486 failf(data, "LDAP local: search ldap_parse_result %s",
487 ldap_err2string(rc));
488 *err = CURLE_LDAP_SEARCH_FAILED;
490 else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
491 failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
493 *err = CURLE_LDAP_SEARCH_FAILED;
497 if(code == LDAP_SIZELIMIT_EXCEEDED)
498 infof(data, "There are more than %d entries\n", lr->nument);
499 data->req.size = data->req.bytecount;
507 else if(msgtype != LDAP_RES_SEARCH_ENTRY)
511 rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
513 /* TODO: verify that this is really how this return code should be
515 *err = CURLE_RECV_ERROR;
518 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
524 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
531 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
536 data->req.bytecount += bv.bv_len + 5;
538 for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, &bvals);
539 (rc == LDAP_SUCCESS) && bvals;
540 rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, &bvals)) {
543 if(bv.bv_val == NULL)
546 if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
551 for(i = 0; bvals[i].bv_val != NULL; i++) {
553 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
559 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
566 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
571 data->req.bytecount += bv.bv_len + 2;
574 /* check for leading or trailing whitespace */
575 if(ISSPACE(bvals[i].bv_val[0]) ||
576 ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
579 /* check for unprintable characters */
581 for(j = 0; j<bvals[i].bv_len; j++)
582 if(!ISPRINT(bvals[i].bv_val[j])) {
588 if(binary || binval) {
589 char *val_b64 = NULL;
590 size_t val_b64_sz = 0;
591 /* Binary value, encode to base64. */
592 CURLcode error = Curl_base64_encode(data,
604 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY,
611 data->req.bytecount += 2;
613 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
620 data->req.bytecount += val_b64_sz;
624 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
630 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
637 data->req.bytecount += bvals[i].bv_len + 1;
639 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
645 data->req.bytecount++;
648 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
653 data->req.bytecount++;
655 writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
660 data->req.bytecount++;
669 ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
671 sbiod->sbiod_pvt = arg;
676 ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
678 sbiod->sbiod_pvt = NULL;
682 /* We don't need to do anything because libcurl does it already */
684 ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
691 ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
694 if(opt == LBER_SB_OPT_DATA_READY) {
695 struct connectdata *conn = sbiod->sbiod_pvt;
696 return Curl_ssl_data_pending(conn, FIRSTSOCKET);
702 ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
704 struct connectdata *conn = sbiod->sbiod_pvt;
705 ldapconninfo *li = conn->proto.generic;
707 CURLcode err = CURLE_RECV_ERROR;
709 ret = (li->recv)(conn, FIRSTSOCKET, buf, len, &err);
710 if(ret < 0 && err == CURLE_AGAIN) {
711 SET_SOCKERRNO(EWOULDBLOCK);
717 ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
719 struct connectdata *conn = sbiod->sbiod_pvt;
720 ldapconninfo *li = conn->proto.generic;
722 CURLcode err = CURLE_SEND_ERROR;
724 ret = (li->send)(conn, FIRSTSOCKET, buf, len, &err);
725 if(ret < 0 && err == CURLE_AGAIN) {
726 SET_SOCKERRNO(EWOULDBLOCK);
731 static Sockbuf_IO ldapsb_tls =
742 #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */