2010-05-25 06:44:42 +08:00
|
|
|
/***************************************************************************
|
2013-01-04 09:50:28 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
2010-05-25 06:44:42 +08:00
|
|
|
*
|
2021-01-09 00:58:15 +08:00
|
|
|
* Copyright (C) 2011 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-03-07 21:43:12 +08:00
|
|
|
* Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
|
2010-05-25 06:44:42 +08:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2010-05-25 06:44:42 +08:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2010-06-01 23:25:03 +08:00
|
|
|
#if !defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notice that USE_OPENLDAP is only a source code selection switch. When
|
|
|
|
* libcurl is built with USE_OPENLDAP defined the libcurl source code that
|
2013-01-04 09:50:28 +08:00
|
|
|
* gets compiled is the code from openldap.c, otherwise the code that gets
|
|
|
|
* compiled is the code from ldap.c.
|
2010-06-01 23:25:03 +08:00
|
|
|
*
|
|
|
|
* When USE_OPENLDAP is defined a recent version of the OpenLDAP library
|
|
|
|
* might be required for compilation and runtime. In order to use ancient
|
|
|
|
* OpenLDAP library versions, USE_OPENLDAP shall not be defined.
|
|
|
|
*/
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2010-06-02 17:23:30 +08:00
|
|
|
#include <ldap.h>
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
2010-05-25 06:44:42 +08:00
|
|
|
#include <curl/curl.h>
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "sendf.h"
|
2013-12-18 06:32:47 +08:00
|
|
|
#include "vtls/vtls.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "transfer.h"
|
2010-06-02 17:23:30 +08:00
|
|
|
#include "curl_ldap.h"
|
2010-05-25 06:44:42 +08:00
|
|
|
#include "curl_base64.h"
|
2014-05-20 16:32:23 +08:00
|
|
|
#include "connect.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 19:36:18 +08:00
|
|
|
#include "curl_printf.h"
|
2015-03-25 06:12:03 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2017-12-06 15:54:35 +08:00
|
|
|
/*
|
|
|
|
* Uncommenting this will enable the built-in debug logging of the openldap
|
|
|
|
* library. The debug log level can be set using the CURL_OPENLDAP_TRACE
|
|
|
|
* environment variable. The debug output is written to stderr.
|
2017-12-06 21:58:26 +08:00
|
|
|
*
|
|
|
|
* The library supports the following debug flags:
|
2017-12-06 15:54:35 +08:00
|
|
|
* LDAP_DEBUG_NONE 0x0000
|
|
|
|
* LDAP_DEBUG_TRACE 0x0001
|
|
|
|
* LDAP_DEBUG_CONSTRUCT 0x0002
|
|
|
|
* LDAP_DEBUG_DESTROY 0x0004
|
|
|
|
* LDAP_DEBUG_PARAMETER 0x0008
|
2017-12-06 21:58:26 +08:00
|
|
|
* LDAP_DEBUG_ANY 0xffff
|
|
|
|
*
|
2017-12-06 15:54:35 +08:00
|
|
|
* For example, use CURL_OPENLDAP_TRACE=0 for no debug,
|
|
|
|
* CURL_OPENLDAP_TRACE=2 for LDAP_DEBUG_CONSTRUCT messages only,
|
|
|
|
* CURL_OPENLDAP_TRACE=65535 for all debug message levels.
|
|
|
|
*/
|
|
|
|
/* #define CURL_OPENLDAP_DEBUG */
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Machine states. */
|
|
|
|
typedef enum {
|
|
|
|
OLDAP_STOP, /* Do nothing state, stops the state machine */
|
|
|
|
OLDAP_SSL, /* Performing SSL handshake. */
|
2021-12-01 00:48:28 +08:00
|
|
|
OLDAP_STARTTLS, /* STARTTLS request sent. */
|
|
|
|
OLDAP_TLS, /* Performing TLS handshake. */
|
2021-11-25 22:03:56 +08:00
|
|
|
OLDAP_BIND, /* Simple bind reply. */
|
|
|
|
OLDAP_BINDV2, /* Simple bind reply in protocol version 2. */
|
|
|
|
OLDAP_LAST /* Never used */
|
|
|
|
} ldapstate;
|
|
|
|
|
2010-05-28 04:37:00 +08:00
|
|
|
#ifndef _LDAP_PVT_H
|
|
|
|
extern int ldap_pvt_url_scheme2proto(const char *);
|
2011-04-20 21:17:42 +08:00
|
|
|
extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
|
|
|
|
LDAP **ld);
|
2010-05-28 04:37:00 +08:00
|
|
|
#endif
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn);
|
|
|
|
static CURLcode oldap_do(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode oldap_done(struct Curl_easy *data, CURLcode, bool);
|
|
|
|
static CURLcode oldap_connect(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode oldap_connecting(struct Curl_easy *data, bool *done);
|
|
|
|
static CURLcode oldap_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, bool dead);
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static Curl_recv oldap_recv;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LDAP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ldap = {
|
|
|
|
"LDAP", /* scheme */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_setup_connection, /* setup_connection */
|
|
|
|
oldap_do, /* do_it */
|
|
|
|
oldap_done, /* done */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* do_more */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_connect, /* connect_it */
|
|
|
|
oldap_connecting, /* connecting */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
ZERO_NULL, /* doing_getsock */
|
2011-10-22 05:36:54 +08:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_disconnect, /* disconnect */
|
2011-05-05 22:27:03 +08:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 19:09:56 +08:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 14:54:00 +08:00
|
|
|
ZERO_NULL, /* attach connection */
|
2010-05-25 06:44:42 +08:00
|
|
|
PORT_LDAP, /* defport */
|
2011-03-15 05:52:14 +08:00
|
|
|
CURLPROTO_LDAP, /* protocol */
|
2020-09-21 19:45:24 +08:00
|
|
|
CURLPROTO_LDAP, /* family */
|
2011-03-15 05:22:22 +08:00
|
|
|
PROTOPT_NONE /* flags */
|
2010-05-25 06:44:42 +08:00
|
|
|
};
|
|
|
|
|
2010-05-28 18:22:35 +08:00
|
|
|
#ifdef USE_SSL
|
2010-05-25 06:44:42 +08:00
|
|
|
/*
|
|
|
|
* LDAPS protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ldaps = {
|
|
|
|
"LDAPS", /* scheme */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_setup_connection, /* setup_connection */
|
|
|
|
oldap_do, /* do_it */
|
|
|
|
oldap_done, /* done */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* do_more */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_connect, /* connect_it */
|
|
|
|
oldap_connecting, /* connecting */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
ZERO_NULL, /* doing_getsock */
|
2011-10-22 05:36:54 +08:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-25 06:44:42 +08:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
2021-05-04 22:14:13 +08:00
|
|
|
oldap_disconnect, /* disconnect */
|
2011-05-05 22:27:03 +08:00
|
|
|
ZERO_NULL, /* readwrite */
|
2017-05-31 19:09:56 +08:00
|
|
|
ZERO_NULL, /* connection_check */
|
2021-05-17 14:54:00 +08:00
|
|
|
ZERO_NULL, /* attach connection */
|
2010-05-25 06:44:42 +08:00
|
|
|
PORT_LDAPS, /* defport */
|
2020-09-21 19:45:24 +08:00
|
|
|
CURLPROTO_LDAPS, /* protocol */
|
|
|
|
CURLPROTO_LDAP, /* family */
|
2011-03-15 05:22:22 +08:00
|
|
|
PROTOPT_SSL /* flags */
|
2010-05-25 06:44:42 +08:00
|
|
|
};
|
2010-05-28 18:22:35 +08:00
|
|
|
#endif
|
2010-05-25 06:44:42 +08:00
|
|
|
|
|
|
|
static const char *url_errs[] = {
|
|
|
|
"success",
|
|
|
|
"out of memory",
|
|
|
|
"bad parameter",
|
|
|
|
"unrecognized scheme",
|
|
|
|
"unbalanced delimiter",
|
|
|
|
"bad URL",
|
|
|
|
"bad host or port",
|
|
|
|
"bad or missing attributes",
|
|
|
|
"bad or missing scope",
|
|
|
|
"bad or missing filter",
|
|
|
|
"bad or missing extensions"
|
|
|
|
};
|
|
|
|
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo {
|
2021-11-25 22:03:56 +08:00
|
|
|
LDAP *ld; /* Openldap connection handle. */
|
|
|
|
Curl_recv *recv; /* For stacking SSL handler */
|
2010-05-25 06:44:42 +08:00
|
|
|
Curl_send *send;
|
2021-11-25 22:03:56 +08:00
|
|
|
ldapstate state; /* Current machine state. */
|
|
|
|
int proto; /* LDAP_PROTO_TCP/LDAP_PROTO_UDP/LDAP_PROTO_IPC */
|
|
|
|
int msgid; /* Current message id. */
|
2019-09-03 19:31:44 +08:00
|
|
|
};
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2020-05-14 06:05:04 +08:00
|
|
|
struct ldapreqinfo {
|
2010-05-25 06:44:42 +08:00
|
|
|
int msgid;
|
|
|
|
int nument;
|
2020-05-14 06:05:04 +08:00
|
|
|
};
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/*
|
|
|
|
* state()
|
|
|
|
*
|
|
|
|
* This is the ONLY way to change LDAP state!
|
|
|
|
*/
|
|
|
|
static void state(struct Curl_easy *data, ldapstate newstate)
|
|
|
|
{
|
|
|
|
struct ldapconninfo *ldapc = data->conn->proto.ldapc;
|
|
|
|
|
|
|
|
#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
|
|
|
|
/* for debug purposes */
|
|
|
|
static const char * const names[] = {
|
|
|
|
"STOP",
|
|
|
|
"SSL",
|
2021-12-01 00:48:28 +08:00
|
|
|
"STARTTLS",
|
|
|
|
"TLS",
|
2021-11-25 22:03:56 +08:00
|
|
|
"BIND",
|
|
|
|
"BINDV2",
|
|
|
|
/* LAST */
|
|
|
|
};
|
|
|
|
|
|
|
|
if(ldapc->state != newstate)
|
|
|
|
infof(data, "LDAP %p state change from %s to %s",
|
|
|
|
(void *)ldapc, names[ldapc->state], names[newstate]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ldapc->state = newstate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Map some particular LDAP error codes to CURLcode values. */
|
|
|
|
static CURLcode oldap_map_error(int rc, CURLcode result)
|
|
|
|
{
|
|
|
|
switch(rc) {
|
|
|
|
case LDAP_NO_MEMORY:
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
case LDAP_INVALID_CREDENTIALS:
|
|
|
|
result = CURLE_LOGIN_DENIED;
|
|
|
|
break;
|
|
|
|
case LDAP_PROTOCOL_ERROR:
|
|
|
|
result = CURLE_UNSUPPORTED_PROTOCOL;
|
|
|
|
break;
|
|
|
|
case LDAP_INSUFFICIENT_ACCESS:
|
|
|
|
result = CURLE_REMOTE_ACCESS_DENIED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_setup_connection(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li;
|
2010-05-25 06:44:42 +08:00
|
|
|
LDAPURLDesc *lud;
|
|
|
|
int rc, proto;
|
|
|
|
CURLcode status;
|
|
|
|
|
2021-03-26 21:25:45 +08:00
|
|
|
rc = ldap_url_parse(data->state.url, &lud);
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc != LDAP_URL_SUCCESS) {
|
2010-05-25 06:44:42 +08:00
|
|
|
const char *msg = "url parsing problem";
|
|
|
|
status = CURLE_URL_MALFORMAT;
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
|
|
|
|
if(rc == LDAP_URL_ERR_MEM)
|
2010-05-25 06:44:42 +08:00
|
|
|
status = CURLE_OUT_OF_MEMORY;
|
|
|
|
msg = url_errs[rc];
|
|
|
|
}
|
2021-01-25 06:41:58 +08:00
|
|
|
failf(data, "LDAP local: %s", msg);
|
2010-05-25 06:44:42 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
|
|
|
|
ldap_free_urldesc(lud);
|
|
|
|
|
2019-09-03 19:31:44 +08:00
|
|
|
li = calloc(1, sizeof(struct ldapconninfo));
|
2012-06-09 02:56:22 +08:00
|
|
|
if(!li)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2010-05-25 06:44:42 +08:00
|
|
|
li->proto = proto;
|
2019-09-03 19:31:44 +08:00
|
|
|
conn->proto.ldapc = li;
|
2014-05-20 16:32:23 +08:00
|
|
|
connkeep(conn, "OpenLDAP default");
|
2021-12-01 00:48:28 +08:00
|
|
|
|
|
|
|
/* Clear the TLS upgraded flag */
|
|
|
|
conn->bits.tls_upgraded = FALSE;
|
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Starts LDAP simple bind. */
|
|
|
|
static CURLcode oldap_perform_bind(struct Curl_easy *data, ldapstate newstate)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
|
|
|
char *binddn = NULL;
|
|
|
|
struct berval passwd;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
passwd.bv_val = NULL;
|
|
|
|
passwd.bv_len = 0;
|
|
|
|
|
|
|
|
if(conn->bits.user_passwd) {
|
|
|
|
binddn = conn->user;
|
|
|
|
passwd.bv_val = conn->passwd;
|
|
|
|
passwd.bv_len = strlen(passwd.bv_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
|
|
|
|
NULL, NULL, &li->msgid);
|
|
|
|
if(rc == LDAP_SUCCESS)
|
|
|
|
state(data, newstate);
|
|
|
|
else
|
|
|
|
result = oldap_map_error(rc,
|
|
|
|
conn->bits.user_passwd?
|
|
|
|
CURLE_LOGIN_DENIED: CURLE_LDAP_CANNOT_BIND);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-05-28 18:22:35 +08:00
|
|
|
#ifdef USE_SSL
|
2010-05-25 06:44:42 +08:00
|
|
|
static Sockbuf_IO ldapsb_tls;
|
2021-11-25 22:03:56 +08:00
|
|
|
|
|
|
|
static bool ssl_installed(struct connectdata *conn)
|
|
|
|
{
|
|
|
|
return conn->proto.ldapc->recv != NULL;
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:48:28 +08:00
|
|
|
static CURLcode oldap_ssl_connect(struct Curl_easy *data, ldapstate newstate)
|
2021-11-25 22:03:56 +08:00
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
|
|
|
bool ssldone = 0;
|
|
|
|
|
|
|
|
result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
|
|
|
|
FIRSTSOCKET, &ssldone);
|
|
|
|
if(!result) {
|
2021-12-01 00:48:28 +08:00
|
|
|
state(data, newstate);
|
2021-11-25 22:03:56 +08:00
|
|
|
|
|
|
|
if(ssldone) {
|
|
|
|
Sockbuf *sb;
|
|
|
|
|
|
|
|
/* Install the libcurl SSL handlers into the sockbuf. */
|
|
|
|
ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
|
|
|
|
ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
|
|
|
|
li->recv = conn->recv[FIRSTSOCKET];
|
|
|
|
li->send = conn->send[FIRSTSOCKET];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2021-12-01 00:48:28 +08:00
|
|
|
|
|
|
|
/* Send the STARTTLS request */
|
|
|
|
static CURLcode oldap_perform_starttls(struct Curl_easy *data)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct ldapconninfo *li = data->conn->proto.ldapc;
|
|
|
|
int rc = ldap_start_tls(li->ld, NULL, NULL, &li->msgid);
|
|
|
|
|
|
|
|
if(rc == LDAP_SUCCESS)
|
|
|
|
state(data, OLDAP_STARTTLS);
|
|
|
|
else
|
|
|
|
result = oldap_map_error(rc, CURLE_USE_SSL_FAILED);
|
|
|
|
return result;
|
|
|
|
}
|
2010-05-28 18:22:35 +08:00
|
|
|
#endif
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_connect(struct Curl_easy *data, bool *done)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2010-05-25 06:44:42 +08:00
|
|
|
int rc, proto = LDAP_VERSION3;
|
2014-12-28 05:22:45 +08:00
|
|
|
char hosturl[1024];
|
|
|
|
char *ptr;
|
2021-11-25 22:03:56 +08:00
|
|
|
#ifdef CURL_OPENLDAP_DEBUG
|
|
|
|
static int do_trace = -1;
|
|
|
|
#endif
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2013-01-17 19:59:23 +08:00
|
|
|
(void)done;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
|
|
|
strcpy(hosturl, "ldap");
|
2017-09-10 05:55:08 +08:00
|
|
|
ptr = hosturl + 4;
|
2011-04-20 21:17:42 +08:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL)
|
2010-05-25 06:44:42 +08:00
|
|
|
*ptr++ = 's';
|
2018-11-22 16:01:24 +08:00
|
|
|
msnprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
|
|
|
|
conn->host.name, conn->remote_port);
|
2010-05-25 06:44:42 +08:00
|
|
|
|
|
|
|
rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc) {
|
2010-05-25 06:44:42 +08:00
|
|
|
failf(data, "LDAP local: Cannot connect to %s, %s",
|
|
|
|
hosturl, ldap_err2string(rc));
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
#ifdef CURL_OPENLDAP_DEBUG
|
|
|
|
if(do_trace < 0) {
|
|
|
|
const char *env = getenv("CURL_OPENLDAP_TRACE");
|
|
|
|
do_trace = (env && strtol(env, NULL, 10) > 0);
|
|
|
|
}
|
|
|
|
if(do_trace)
|
|
|
|
ldap_set_option(li->ld, LDAP_OPT_DEBUG_LEVEL, &do_trace);
|
|
|
|
#endif
|
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
|
|
|
|
|
2010-05-28 18:22:35 +08:00
|
|
|
#ifdef USE_SSL
|
2021-11-25 22:03:56 +08:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL)
|
2021-12-01 00:48:28 +08:00
|
|
|
return oldap_ssl_connect(data, OLDAP_SSL);
|
|
|
|
|
|
|
|
if(data->set.use_ssl) {
|
|
|
|
CURLcode result = oldap_perform_starttls(data);
|
|
|
|
|
|
|
|
if(!result || data->set.use_ssl != CURLUSESSL_TRY)
|
|
|
|
return result;
|
|
|
|
}
|
2010-05-28 18:22:35 +08:00
|
|
|
#endif
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Force bind even if anonymous bind is not needed in protocol version 3
|
|
|
|
to detect missing version 3 support. */
|
|
|
|
return oldap_perform_bind(data, OLDAP_BIND);
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Handle a simple bind response. */
|
|
|
|
static CURLcode oldap_state_bind_resp(struct Curl_easy *data, LDAPMessage *msg,
|
|
|
|
int code)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2021-11-25 22:03:56 +08:00
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct berval *bv = NULL;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if(code != LDAP_SUCCESS)
|
|
|
|
return oldap_map_error(code, CURLE_LDAP_CANNOT_BIND);
|
|
|
|
|
|
|
|
rc = ldap_parse_sasl_bind_result(li->ld, msg, &bv, 0);
|
|
|
|
if(rc != LDAP_SUCCESS) {
|
|
|
|
failf(data, "LDAP local: bind ldap_parse_sasl_bind_result %s",
|
|
|
|
ldap_err2string(rc));
|
|
|
|
result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state(data, OLDAP_STOP);
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
if(bv)
|
|
|
|
ber_bvfree(bv);
|
|
|
|
return result;
|
|
|
|
}
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
static CURLcode oldap_connecting(struct Curl_easy *data, bool *done)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
|
|
|
LDAPMessage *msg = NULL;
|
|
|
|
struct timeval tv = {0, 0};
|
|
|
|
int code = LDAP_SUCCESS;
|
|
|
|
int rc;
|
|
|
|
|
2021-12-01 00:48:28 +08:00
|
|
|
if(li->state != OLDAP_SSL && li->state != OLDAP_TLS) {
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Get response to last command. */
|
|
|
|
rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, &tv, &msg);
|
|
|
|
if(!rc)
|
|
|
|
return CURLE_OK; /* Timed out. */
|
|
|
|
if(rc < 0) {
|
|
|
|
failf(data, "LDAP local: connecting ldap_result %s",
|
|
|
|
ldap_err2string(rc));
|
|
|
|
return oldap_map_error(rc, CURLE_COULDNT_CONNECT);
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Get error code from message. */
|
|
|
|
rc = ldap_parse_result(li->ld, msg, &code, NULL, NULL, NULL, NULL, 0);
|
|
|
|
if(rc)
|
|
|
|
code = rc;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* If protocol version 3 is not supported, fallback to version 2. */
|
2021-12-01 00:48:28 +08:00
|
|
|
if(code == LDAP_PROTOCOL_ERROR && li->state != OLDAP_BINDV2
|
|
|
|
#ifdef USE_SSL
|
|
|
|
&& (ssl_installed(conn) || data->set.use_ssl <= CURLUSESSL_TRY)
|
|
|
|
#endif
|
|
|
|
) {
|
2021-11-25 22:03:56 +08:00
|
|
|
static const int version = LDAP_VERSION2;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
|
|
|
|
ldap_msgfree(msg);
|
|
|
|
return oldap_perform_bind(data, OLDAP_BINDV2);
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
/* Handle response message according to current state. */
|
|
|
|
switch(li->state) {
|
2014-12-28 05:07:53 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
#ifdef USE_SSL
|
|
|
|
case OLDAP_SSL:
|
2021-12-01 00:48:28 +08:00
|
|
|
result = oldap_ssl_connect(data, OLDAP_SSL);
|
2021-11-25 22:03:56 +08:00
|
|
|
if(!result && ssl_installed(conn))
|
|
|
|
result = oldap_perform_bind(data, OLDAP_BIND);
|
|
|
|
break;
|
2021-12-01 00:48:28 +08:00
|
|
|
case OLDAP_STARTTLS:
|
|
|
|
if(code != LDAP_SUCCESS) {
|
|
|
|
if(data->set.use_ssl != CURLUSESSL_TRY)
|
|
|
|
result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
|
|
|
|
else
|
|
|
|
result = oldap_perform_bind(data, OLDAP_BIND);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case OLDAP_TLS:
|
|
|
|
result = oldap_ssl_connect(data, OLDAP_TLS);
|
|
|
|
if(result && data->set.use_ssl != CURLUSESSL_TRY)
|
|
|
|
result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
|
|
|
|
else if(ssl_installed(conn)) {
|
|
|
|
conn->bits.tls_upgraded = TRUE;
|
|
|
|
if(conn->bits.user_passwd)
|
|
|
|
result = oldap_perform_bind(data, OLDAP_BIND);
|
|
|
|
else {
|
|
|
|
state(data, OLDAP_STOP); /* Version 3 supported: no bind required */
|
|
|
|
result = CURLE_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-11-25 22:03:56 +08:00
|
|
|
#endif
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
case OLDAP_BIND:
|
|
|
|
case OLDAP_BINDV2:
|
|
|
|
result = oldap_state_bind_resp(data, msg, code);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* internal error */
|
|
|
|
result = CURLE_COULDNT_CONNECT;
|
|
|
|
break;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
ldap_msgfree(msg);
|
2012-01-18 23:06:29 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
*done = li->state == OLDAP_STOP;
|
|
|
|
if(*done)
|
|
|
|
conn->recv[FIRSTSOCKET] = oldap_recv;
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2021-11-25 22:03:56 +08:00
|
|
|
return result;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_disconnect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
|
|
|
bool dead_connection)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2010-11-19 20:43:20 +08:00
|
|
|
(void) dead_connection;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
if(li) {
|
|
|
|
if(li->ld) {
|
2021-04-16 04:47:09 +08:00
|
|
|
#ifdef USE_SSL
|
2021-11-25 22:03:56 +08:00
|
|
|
if(ssl_installed(conn)) {
|
2021-03-28 17:43:11 +08:00
|
|
|
Sockbuf *sb;
|
|
|
|
ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
|
|
|
|
ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
|
|
|
|
}
|
2021-04-16 04:47:09 +08:00
|
|
|
#endif
|
2010-05-28 04:37:00 +08:00
|
|
|
ldap_unbind_ext(li->ld, NULL, NULL);
|
2010-05-25 06:44:42 +08:00
|
|
|
li->ld = NULL;
|
|
|
|
}
|
2019-09-03 19:31:44 +08:00
|
|
|
conn->proto.ldapc = NULL;
|
2010-05-25 06:44:42 +08:00
|
|
|
free(li);
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_do(struct Curl_easy *data, bool *done)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2020-05-14 06:05:04 +08:00
|
|
|
struct ldapreqinfo *lr;
|
2010-05-25 06:44:42 +08:00
|
|
|
CURLcode status = CURLE_OK;
|
|
|
|
int rc = 0;
|
|
|
|
LDAPURLDesc *ludp = NULL;
|
|
|
|
int msgid;
|
|
|
|
|
2014-05-20 16:32:23 +08:00
|
|
|
connkeep(conn, "OpenLDAP do");
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-07-06 23:05:17 +08:00
|
|
|
infof(data, "LDAP local: %s", data->state.url);
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-03-26 21:25:45 +08:00
|
|
|
rc = ldap_url_parse(data->state.url, &ludp);
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc != LDAP_URL_SUCCESS) {
|
2010-05-25 06:44:42 +08:00
|
|
|
const char *msg = "url parsing problem";
|
|
|
|
status = CURLE_URL_MALFORMAT;
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
|
|
|
|
if(rc == LDAP_URL_ERR_MEM)
|
2010-05-25 06:44:42 +08:00
|
|
|
status = CURLE_OUT_OF_MEMORY;
|
|
|
|
msg = url_errs[rc];
|
|
|
|
}
|
2021-01-25 06:41:58 +08:00
|
|
|
failf(data, "LDAP local: %s", msg);
|
2010-05-25 06:44:42 +08:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
|
|
|
|
ludp->lud_filter, ludp->lud_attrs, 0,
|
|
|
|
NULL, NULL, NULL, 0, &msgid);
|
|
|
|
ldap_free_urldesc(ludp);
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc != LDAP_SUCCESS) {
|
2010-05-25 06:44:42 +08:00
|
|
|
failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
|
|
|
|
return CURLE_LDAP_SEARCH_FAILED;
|
|
|
|
}
|
2020-05-14 06:05:04 +08:00
|
|
|
lr = calloc(1, sizeof(struct ldapreqinfo));
|
2012-06-09 02:56:22 +08:00
|
|
|
if(!lr)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2010-05-25 06:44:42 +08:00
|
|
|
lr->msgid = msgid;
|
2020-11-23 15:32:41 +08:00
|
|
|
data->req.p.ldap = lr;
|
2019-02-28 18:36:26 +08:00
|
|
|
Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
|
2010-05-25 06:44:42 +08:00
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static CURLcode oldap_done(struct Curl_easy *data, CURLcode res,
|
|
|
|
bool premature)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
struct ldapreqinfo *lr = data->req.p.ldap;
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
(void)res;
|
|
|
|
(void)premature;
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
if(lr) {
|
2010-05-25 06:44:42 +08:00
|
|
|
/* if there was a search in progress, abandon it */
|
2011-04-20 21:17:42 +08:00
|
|
|
if(lr->msgid) {
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2010-05-28 04:37:00 +08:00
|
|
|
ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
|
2010-05-25 06:44:42 +08:00
|
|
|
lr->msgid = 0;
|
|
|
|
}
|
2021-01-25 06:41:58 +08:00
|
|
|
data->req.p.ldap = NULL;
|
2010-05-25 06:44:42 +08:00
|
|
|
free(lr);
|
|
|
|
}
|
2014-12-28 05:22:45 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-12-12 08:24:06 +08:00
|
|
|
static CURLcode client_write(struct Curl_easy *data, const char *prefix,
|
|
|
|
const char *value, size_t len, const char *suffix)
|
|
|
|
{
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
size_t l;
|
|
|
|
|
|
|
|
if(prefix) {
|
|
|
|
l = strlen(prefix);
|
|
|
|
/* If we have a zero-length value and the prefix ends with a space
|
|
|
|
separator, drop the latter. */
|
|
|
|
if(!len && l && prefix[l - 1] == ' ')
|
|
|
|
l--;
|
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) prefix, l);
|
|
|
|
if(!result)
|
|
|
|
data->req.bytecount += l;
|
|
|
|
}
|
|
|
|
if(!result && value) {
|
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) value, len);
|
|
|
|
if(!result)
|
|
|
|
data->req.bytecount += len;
|
|
|
|
}
|
|
|
|
if(!result && suffix) {
|
|
|
|
l = strlen(suffix);
|
|
|
|
result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) suffix, l);
|
|
|
|
if(!result)
|
|
|
|
data->req.bytecount += l;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:14:13 +08:00
|
|
|
static ssize_t oldap_recv(struct Curl_easy *data, int sockindex, char *buf,
|
|
|
|
size_t len, CURLcode *err)
|
2010-05-25 06:44:42 +08:00
|
|
|
{
|
2021-01-09 00:58:15 +08:00
|
|
|
struct connectdata *conn = data->conn;
|
2019-09-03 19:31:44 +08:00
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
2020-11-23 15:32:41 +08:00
|
|
|
struct ldapreqinfo *lr = data->req.p.ldap;
|
2021-12-06 18:13:28 +08:00
|
|
|
int rc;
|
2014-12-28 05:07:53 +08:00
|
|
|
LDAPMessage *msg = NULL;
|
2010-05-25 06:44:42 +08:00
|
|
|
BerElement *ber = NULL;
|
2015-03-17 20:41:49 +08:00
|
|
|
struct timeval tv = {0, 1};
|
2021-12-06 18:13:28 +08:00
|
|
|
struct berval bv, *bvals;
|
|
|
|
int binary = 0;
|
|
|
|
CURLcode result = CURLE_AGAIN;
|
|
|
|
int code;
|
|
|
|
char *info = NULL;
|
2014-12-28 05:07:53 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
(void)len;
|
|
|
|
(void)buf;
|
|
|
|
(void)sockindex;
|
|
|
|
|
2021-12-06 18:13:28 +08:00
|
|
|
rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_ONE, &tv, &msg);
|
2011-04-20 21:17:42 +08:00
|
|
|
if(rc < 0) {
|
2010-05-25 06:44:42 +08:00
|
|
|
failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
|
2021-12-06 18:13:28 +08:00
|
|
|
result = CURLE_RECV_ERROR;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:13:28 +08:00
|
|
|
*err = result;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-12-06 18:13:28 +08:00
|
|
|
/* error or timed out */
|
2014-12-28 05:07:53 +08:00
|
|
|
if(!msg)
|
2021-12-06 18:13:28 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = CURLE_OK;
|
|
|
|
|
|
|
|
switch(ldap_msgtype(msg)) {
|
|
|
|
case LDAP_RES_SEARCH_RESULT:
|
|
|
|
lr->msgid = 0;
|
|
|
|
rc = ldap_parse_result(li->ld, msg, &code, NULL, &info, NULL, NULL, 0);
|
|
|
|
if(rc) {
|
|
|
|
failf(data, "LDAP local: search ldap_parse_result %s",
|
|
|
|
ldap_err2string(rc));
|
|
|
|
result = CURLE_LDAP_SEARCH_FAILED;
|
2010-05-25 06:44:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-12-06 18:13:28 +08:00
|
|
|
switch(code) {
|
|
|
|
case LDAP_SIZELIMIT_EXCEEDED:
|
|
|
|
infof(data, "There are more than %d entries", lr->nument);
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case LDAP_SUCCESS:
|
|
|
|
data->req.size = data->req.bytecount;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
failf(data, "LDAP remote: search failed %s %s", ldap_err2string(code),
|
|
|
|
info ? info : "");
|
|
|
|
result = CURLE_LDAP_SEARCH_FAILED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(info)
|
|
|
|
ldap_memfree(info);
|
|
|
|
break;
|
|
|
|
case LDAP_RES_SEARCH_ENTRY:
|
2010-05-25 06:44:42 +08:00
|
|
|
lr->nument++;
|
2021-12-06 18:13:28 +08:00
|
|
|
rc = ldap_get_dn_ber(li->ld, msg, &ber, &bv);
|
2011-03-16 07:16:34 +08:00
|
|
|
if(rc < 0) {
|
2021-12-06 18:13:28 +08:00
|
|
|
result = CURLE_RECV_ERROR;
|
|
|
|
break;
|
2015-09-18 00:36:07 +08:00
|
|
|
}
|
2014-12-08 22:30:14 +08:00
|
|
|
|
2021-12-12 08:24:06 +08:00
|
|
|
result = client_write(data, "DN: ", bv.bv_val, bv.bv_len, "\n");
|
2021-12-06 18:13:28 +08:00
|
|
|
if(result)
|
|
|
|
break;
|
2014-12-08 22:30:14 +08:00
|
|
|
|
2021-12-06 18:13:28 +08:00
|
|
|
for(rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals);
|
2018-03-19 02:13:27 +08:00
|
|
|
rc == LDAP_SUCCESS;
|
2021-12-06 18:13:28 +08:00
|
|
|
rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals)) {
|
2010-05-25 06:44:42 +08:00
|
|
|
int i;
|
|
|
|
|
2021-04-19 16:46:11 +08:00
|
|
|
if(!bv.bv_val)
|
2018-03-07 21:43:12 +08:00
|
|
|
break;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-04-19 16:46:11 +08:00
|
|
|
if(!bvals) {
|
2021-12-12 08:24:06 +08:00
|
|
|
result = client_write(data, "\t", bv.bv_val, bv.bv_len, ":\n");
|
2021-12-06 18:13:28 +08:00
|
|
|
if(result)
|
|
|
|
break;
|
2018-03-19 02:13:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-12-12 08:24:06 +08:00
|
|
|
binary = bv.bv_len > 7 &&
|
|
|
|
!strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7);
|
|
|
|
|
2017-09-10 05:09:06 +08:00
|
|
|
for(i = 0; bvals[i].bv_val != NULL; i++) {
|
2010-05-25 06:44:42 +08:00
|
|
|
int binval = 0;
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2021-12-12 08:24:06 +08:00
|
|
|
result = client_write(data, "\t", bv.bv_val, bv.bv_len, ":");
|
2021-12-06 18:13:28 +08:00
|
|
|
if(result)
|
|
|
|
break;
|
|
|
|
|
2011-04-20 21:17:42 +08:00
|
|
|
if(!binary) {
|
2010-05-25 06:44:42 +08:00
|
|
|
/* check for leading or trailing whitespace */
|
2011-04-20 21:17:42 +08:00
|
|
|
if(ISSPACE(bvals[i].bv_val[0]) ||
|
2021-12-06 18:13:28 +08:00
|
|
|
ISSPACE(bvals[i].bv_val[bvals[i].bv_len - 1]))
|
2010-05-25 06:44:42 +08:00
|
|
|
binval = 1;
|
2011-04-20 21:17:42 +08:00
|
|
|
else {
|
2010-05-25 06:44:42 +08:00
|
|
|
/* check for unprintable characters */
|
|
|
|
unsigned int j;
|
2021-12-06 18:13:28 +08:00
|
|
|
for(j = 0; j < bvals[i].bv_len; j++)
|
2011-04-20 21:17:42 +08:00
|
|
|
if(!ISPRINT(bvals[i].bv_val[j])) {
|
2010-05-25 06:44:42 +08:00
|
|
|
binval = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 21:17:42 +08:00
|
|
|
if(binary || binval) {
|
2011-08-24 14:07:36 +08:00
|
|
|
char *val_b64 = NULL;
|
|
|
|
size_t val_b64_sz = 0;
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
/* Binary value, encode to base64. */
|
2021-12-12 08:24:06 +08:00
|
|
|
if(bvals[i].bv_len)
|
|
|
|
result = Curl_base64_encode(data, bvals[i].bv_val, bvals[i].bv_len,
|
|
|
|
&val_b64, &val_b64_sz);
|
2021-12-06 18:13:28 +08:00
|
|
|
if(!result)
|
2021-12-12 08:24:06 +08:00
|
|
|
result = client_write(data, ": ", val_b64, val_b64_sz, "\n");
|
2021-12-06 18:13:28 +08:00
|
|
|
free(val_b64);
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
2021-12-12 08:24:06 +08:00
|
|
|
else
|
|
|
|
result = client_write(data, " ",
|
|
|
|
bvals[i].bv_val, bvals[i].bv_len, "\n");
|
2021-12-06 18:13:28 +08:00
|
|
|
if(result)
|
|
|
|
break;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
ber_memfree(bvals);
|
2021-12-12 08:24:06 +08:00
|
|
|
bvals = NULL;
|
2021-12-06 18:13:28 +08:00
|
|
|
if(!result)
|
2021-12-12 08:24:06 +08:00
|
|
|
result = client_write(data, "\n", NULL, 0, NULL);
|
2021-12-06 18:13:28 +08:00
|
|
|
if(result)
|
|
|
|
break;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2010-05-25 06:44:42 +08:00
|
|
|
ber_free(ber, 0);
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2021-12-12 08:24:06 +08:00
|
|
|
if(!result)
|
|
|
|
result = client_write(data, "\n", NULL, 0, NULL);
|
2021-12-06 18:13:28 +08:00
|
|
|
if(!result)
|
|
|
|
result = CURLE_AGAIN;
|
|
|
|
break;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
2021-12-06 18:13:28 +08:00
|
|
|
|
2014-12-28 05:07:53 +08:00
|
|
|
ldap_msgfree(msg);
|
2021-12-06 18:13:28 +08:00
|
|
|
*err = result;
|
|
|
|
return result? -1: 0;
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
|
2010-05-28 18:22:35 +08:00
|
|
|
#ifdef USE_SSL
|
2010-05-25 06:44:42 +08:00
|
|
|
static int
|
|
|
|
ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
|
|
|
|
{
|
|
|
|
sbiod->sbiod_pvt = arg;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
|
|
|
|
{
|
|
|
|
sbiod->sbiod_pvt = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't need to do anything because libcurl does it already */
|
|
|
|
static int
|
|
|
|
ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
|
|
|
|
{
|
|
|
|
(void)sbiod;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
|
|
|
|
{
|
|
|
|
(void)arg;
|
2011-04-20 21:17:42 +08:00
|
|
|
if(opt == LBER_SB_OPT_DATA_READY) {
|
2021-02-15 16:41:22 +08:00
|
|
|
struct Curl_easy *data = sbiod->sbiod_pvt;
|
|
|
|
return Curl_ssl_data_pending(data->conn, FIRSTSOCKET);
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ber_slen_t
|
|
|
|
ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
|
|
|
|
{
|
2021-02-15 16:41:22 +08:00
|
|
|
struct Curl_easy *data = sbiod->sbiod_pvt;
|
2021-03-23 16:28:07 +08:00
|
|
|
ber_slen_t ret = 0;
|
|
|
|
if(data) {
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
if(conn) {
|
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
|
|
|
CURLcode err = CURLE_RECV_ERROR;
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2021-03-23 16:28:07 +08:00
|
|
|
ret = (li->recv)(data, FIRSTSOCKET, buf, len, &err);
|
|
|
|
if(ret < 0 && err == CURLE_AGAIN) {
|
|
|
|
SET_SOCKERRNO(EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ber_slen_t
|
|
|
|
ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
|
|
|
|
{
|
2021-02-15 16:41:22 +08:00
|
|
|
struct Curl_easy *data = sbiod->sbiod_pvt;
|
2021-03-23 16:28:07 +08:00
|
|
|
ber_slen_t ret = 0;
|
|
|
|
if(data) {
|
|
|
|
struct connectdata *conn = data->conn;
|
|
|
|
if(conn) {
|
|
|
|
struct ldapconninfo *li = conn->proto.ldapc;
|
|
|
|
CURLcode err = CURLE_SEND_ERROR;
|
|
|
|
ret = (li->send)(data, FIRSTSOCKET, buf, len, &err);
|
|
|
|
if(ret < 0 && err == CURLE_AGAIN) {
|
|
|
|
SET_SOCKERRNO(EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 06:44:42 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Sockbuf_IO ldapsb_tls =
|
|
|
|
{
|
|
|
|
ldapsb_tls_setup,
|
|
|
|
ldapsb_tls_remove,
|
|
|
|
ldapsb_tls_ctrl,
|
|
|
|
ldapsb_tls_read,
|
|
|
|
ldapsb_tls_write,
|
|
|
|
ldapsb_tls_close
|
|
|
|
};
|
2010-05-28 18:22:35 +08:00
|
|
|
#endif /* USE_SSL */
|
2010-05-25 06:44:42 +08:00
|
|
|
|
2010-06-01 23:25:03 +08:00
|
|
|
#endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */
|