2010-05-24 18:44:42 -04:00
|
|
|
/***************************************************************************
|
2013-01-03 20:50:28 -05:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
2010-05-24 18:44:42 -04:00
|
|
|
*
|
2015-03-03 06:36:18 -05:00
|
|
|
* Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
|
|
|
|
* Copyright (C) 2011 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2010-05-24 18:44:42 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* 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-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2010-06-01 11:25:03 -04: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-03 20:50:28 -05:00
|
|
|
* gets compiled is the code from openldap.c, otherwise the code that gets
|
|
|
|
* compiled is the code from ldap.c.
|
2010-06-01 11:25:03 -04: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-24 18:44:42 -04:00
|
|
|
|
2010-06-02 05:23:30 -04:00
|
|
|
#include <ldap.h>
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
2010-05-24 18:44:42 -04:00
|
|
|
#include <curl/curl.h>
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "sendf.h"
|
2013-12-17 17:32:47 -05:00
|
|
|
#include "vtls/vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "transfer.h"
|
2010-06-02 05:23:30 -04:00
|
|
|
#include "curl_ldap.h"
|
2010-05-24 18:44:42 -04:00
|
|
|
#include "curl_base64.h"
|
2014-05-20 04:32:23 -04:00
|
|
|
#include "connect.h"
|
2015-03-03 06:36:18 -05:00
|
|
|
#include "curl_printf.h"
|
2015-03-24 18:12:03 -04:00
|
|
|
|
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2010-05-27 16:37:00 -04:00
|
|
|
#ifndef _LDAP_PVT_H
|
|
|
|
extern int ldap_pvt_url_scheme2proto(const char *);
|
2011-04-20 09:17:42 -04:00
|
|
|
extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
|
|
|
|
LDAP **ld);
|
2010-05-27 16:37:00 -04:00
|
|
|
#endif
|
|
|
|
|
2014-12-24 12:14:02 -05:00
|
|
|
static CURLcode ldap_setup_connection(struct connectdata *conn);
|
2010-05-24 18:44:42 -04:00
|
|
|
static CURLcode ldap_do(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
|
|
|
|
static CURLcode ldap_connect(struct connectdata *conn, bool *done);
|
|
|
|
static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
|
2011-04-20 09:17:42 -04:00
|
|
|
static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
static Curl_recv ldap_recv;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* LDAP protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ldap = {
|
|
|
|
"LDAP", /* scheme */
|
2014-12-24 12:14:02 -05:00
|
|
|
ldap_setup_connection, /* setup_connection */
|
2010-05-24 18:44:42 -04:00
|
|
|
ldap_do, /* do_it */
|
|
|
|
ldap_done, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
ldap_connect, /* connect_it */
|
|
|
|
ldap_connecting, /* connecting */
|
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
ZERO_NULL, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-24 18:44:42 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
ldap_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2010-05-24 18:44:42 -04:00
|
|
|
PORT_LDAP, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_LDAP, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_NONE /* flags */
|
2010-05-24 18:44:42 -04:00
|
|
|
};
|
|
|
|
|
2010-05-28 06:22:35 -04:00
|
|
|
#ifdef USE_SSL
|
2010-05-24 18:44:42 -04:00
|
|
|
/*
|
|
|
|
* LDAPS protocol handler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const struct Curl_handler Curl_handler_ldaps = {
|
|
|
|
"LDAPS", /* scheme */
|
2014-12-24 12:14:02 -05:00
|
|
|
ldap_setup_connection, /* setup_connection */
|
2010-05-24 18:44:42 -04:00
|
|
|
ldap_do, /* do_it */
|
|
|
|
ldap_done, /* done */
|
|
|
|
ZERO_NULL, /* do_more */
|
|
|
|
ldap_connect, /* connect_it */
|
|
|
|
ldap_connecting, /* connecting */
|
|
|
|
ZERO_NULL, /* doing */
|
|
|
|
ZERO_NULL, /* proto_getsock */
|
|
|
|
ZERO_NULL, /* doing_getsock */
|
2011-10-21 17:36:54 -04:00
|
|
|
ZERO_NULL, /* domore_getsock */
|
2010-05-24 18:44:42 -04:00
|
|
|
ZERO_NULL, /* perform_getsock */
|
|
|
|
ldap_disconnect, /* disconnect */
|
2011-05-05 10:27:03 -04:00
|
|
|
ZERO_NULL, /* readwrite */
|
2010-05-24 18:44:42 -04:00
|
|
|
PORT_LDAPS, /* defport */
|
2011-03-14 17:52:14 -04:00
|
|
|
CURLPROTO_LDAP, /* protocol */
|
2011-03-14 17:22:22 -04:00
|
|
|
PROTOPT_SSL /* flags */
|
2010-05-24 18:44:42 -04:00
|
|
|
};
|
2010-05-28 06:22:35 -04:00
|
|
|
#endif
|
2010-05-24 18:44:42 -04: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"
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ldapconninfo {
|
|
|
|
LDAP *ld;
|
|
|
|
Curl_recv *recv; /* for stacking SSL handler */
|
|
|
|
Curl_send *send;
|
|
|
|
int proto;
|
|
|
|
int msgid;
|
|
|
|
bool ssldone;
|
|
|
|
bool sslinst;
|
|
|
|
bool didbind;
|
|
|
|
} ldapconninfo;
|
|
|
|
|
|
|
|
typedef struct ldapreqinfo {
|
|
|
|
int msgid;
|
|
|
|
int nument;
|
|
|
|
} ldapreqinfo;
|
|
|
|
|
2014-12-24 12:14:02 -05:00
|
|
|
static CURLcode ldap_setup_connection(struct connectdata *conn)
|
2010-05-24 18:44:42 -04:00
|
|
|
{
|
|
|
|
ldapconninfo *li;
|
|
|
|
LDAPURLDesc *lud;
|
|
|
|
struct SessionHandle *data=conn->data;
|
|
|
|
int rc, proto;
|
|
|
|
CURLcode status;
|
|
|
|
|
|
|
|
rc = ldap_url_parse(data->change.url, &lud);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc != LDAP_URL_SUCCESS) {
|
2010-05-24 18:44:42 -04:00
|
|
|
const char *msg = "url parsing problem";
|
|
|
|
status = CURLE_URL_MALFORMAT;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
|
|
|
|
if(rc == LDAP_URL_ERR_MEM)
|
2010-05-24 18:44:42 -04:00
|
|
|
status = CURLE_OUT_OF_MEMORY;
|
|
|
|
msg = url_errs[rc];
|
|
|
|
}
|
|
|
|
failf(conn->data, "LDAP local: %s", msg);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
|
|
|
|
ldap_free_urldesc(lud);
|
|
|
|
|
|
|
|
li = calloc(1, sizeof(ldapconninfo));
|
2012-06-08 14:56:22 -04:00
|
|
|
if(!li)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2010-05-24 18:44:42 -04:00
|
|
|
li->proto = proto;
|
|
|
|
conn->proto.generic = li;
|
2014-05-20 04:32:23 -04:00
|
|
|
connkeep(conn, "OpenLDAP default");
|
2010-05-24 18:44:42 -04:00
|
|
|
/* TODO:
|
|
|
|
* - provide option to choose SASL Binds instead of Simple
|
|
|
|
*/
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-28 06:22:35 -04:00
|
|
|
#ifdef USE_SSL
|
2010-05-24 18:44:42 -04:00
|
|
|
static Sockbuf_IO ldapsb_tls;
|
2010-05-28 06:22:35 -04:00
|
|
|
#endif
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
static CURLcode ldap_connect(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
2014-12-27 16:22:45 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
2010-05-24 18:44:42 -04:00
|
|
|
int rc, proto = LDAP_VERSION3;
|
2014-12-27 16:22:45 -05:00
|
|
|
char hosturl[1024];
|
|
|
|
char *ptr;
|
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
(void)done;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
strcpy(hosturl, "ldap");
|
|
|
|
ptr = hosturl+4;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL)
|
2010-05-24 18:44:42 -04:00
|
|
|
*ptr++ = 's';
|
|
|
|
snprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
|
2010-11-22 17:59:59 -05:00
|
|
|
conn->host.name, conn->remote_port);
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP local: Cannot connect to %s, %s",
|
|
|
|
hosturl, ldap_err2string(rc));
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
|
|
|
}
|
|
|
|
|
|
|
|
ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
|
|
|
|
|
2010-05-28 06:22:35 -04:00
|
|
|
#ifdef USE_SSL
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL) {
|
2014-12-27 16:22:45 -05:00
|
|
|
CURLcode result;
|
|
|
|
result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2010-05-24 18:44:42 -04:00
|
|
|
}
|
2010-05-28 06:22:35 -04:00
|
|
|
#endif
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
2014-12-27 16:22:45 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
2014-12-27 16:07:53 -05:00
|
|
|
LDAPMessage *msg = NULL;
|
2015-03-17 08:41:49 -04:00
|
|
|
struct timeval tv = {0, 1}, *tvp;
|
2010-05-24 18:44:42 -04:00
|
|
|
int rc, err;
|
|
|
|
char *info = NULL;
|
|
|
|
|
2010-05-28 06:22:35 -04:00
|
|
|
#ifdef USE_SSL
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->handler->flags & PROTOPT_SSL) {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* Is the SSL handshake complete yet? */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!li->ssldone) {
|
2014-12-27 16:22:45 -05:00
|
|
|
CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
|
|
|
|
&li->ssldone);
|
|
|
|
if(result || !li->ssldone)
|
|
|
|
return result;
|
2010-05-24 18:44:42 -04:00
|
|
|
}
|
2014-12-27 16:22:45 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
/* Have we installed the libcurl SSL handlers into the sockbuf yet? */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!li->sslinst) {
|
2010-05-24 18:44:42 -04:00
|
|
|
Sockbuf *sb;
|
|
|
|
ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
|
|
|
|
ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
|
|
|
|
li->sslinst = TRUE;
|
|
|
|
li->recv = conn->recv[FIRSTSOCKET];
|
|
|
|
li->send = conn->send[FIRSTSOCKET];
|
|
|
|
}
|
|
|
|
}
|
2010-05-28 06:22:35 -04:00
|
|
|
#endif
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2013-01-17 06:59:23 -05:00
|
|
|
tvp = &tv;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
retry:
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!li->didbind) {
|
2010-05-24 18:44:42 -04:00
|
|
|
char *binddn;
|
|
|
|
struct berval passwd;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->bits.user_passwd) {
|
2010-05-24 18:44:42 -04:00
|
|
|
binddn = conn->user;
|
|
|
|
passwd.bv_val = conn->passwd;
|
|
|
|
passwd.bv_len = strlen(passwd.bv_val);
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-05-24 18:44:42 -04:00
|
|
|
binddn = NULL;
|
|
|
|
passwd.bv_val = NULL;
|
|
|
|
passwd.bv_len = 0;
|
|
|
|
}
|
|
|
|
rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
|
|
|
|
NULL, NULL, &li->msgid);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc)
|
2010-05-24 18:44:42 -04:00
|
|
|
return CURLE_LDAP_CANNOT_BIND;
|
|
|
|
li->didbind = TRUE;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(tvp)
|
2010-05-24 18:44:42 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2014-12-27 16:07:53 -05:00
|
|
|
rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc < 0) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
|
|
|
|
return CURLE_LDAP_CANNOT_BIND;
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc == 0) {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* timed out */
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2014-12-27 16:07:53 -05:00
|
|
|
|
|
|
|
rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
|
|
|
|
return CURLE_LDAP_CANNOT_BIND;
|
|
|
|
}
|
2014-12-27 16:22:45 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
/* Try to fallback to LDAPv2? */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(err == LDAP_PROTOCOL_ERROR) {
|
2010-05-24 18:44:42 -04:00
|
|
|
int proto;
|
|
|
|
ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(proto == LDAP_VERSION3) {
|
2012-01-18 10:06:29 -05:00
|
|
|
if(info) {
|
|
|
|
ldap_memfree(info);
|
|
|
|
info = NULL;
|
|
|
|
}
|
2010-05-24 18:44:42 -04:00
|
|
|
proto = LDAP_VERSION2;
|
|
|
|
ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
|
|
|
|
li->didbind = FALSE;
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(err) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
|
|
|
|
info ? info : "");
|
2012-01-18 10:06:29 -05:00
|
|
|
if(info)
|
|
|
|
ldap_memfree(info);
|
2010-05-24 18:44:42 -04:00
|
|
|
return CURLE_LOGIN_DENIED;
|
|
|
|
}
|
2012-01-18 10:06:29 -05:00
|
|
|
|
|
|
|
if(info)
|
|
|
|
ldap_memfree(info);
|
2010-05-24 18:44:42 -04:00
|
|
|
conn->recv[FIRSTSOCKET] = ldap_recv;
|
|
|
|
*done = TRUE;
|
2014-12-27 16:22:45 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-11-19 07:43:20 -05:00
|
|
|
static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
|
2010-05-24 18:44:42 -04:00
|
|
|
{
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
2010-11-19 07:43:20 -05:00
|
|
|
(void) dead_connection;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(li) {
|
|
|
|
if(li->ld) {
|
2010-05-27 16:37:00 -04:00
|
|
|
ldap_unbind_ext(li->ld, NULL, NULL);
|
2010-05-24 18:44:42 -04:00
|
|
|
li->ld = NULL;
|
|
|
|
}
|
|
|
|
conn->proto.generic = NULL;
|
|
|
|
free(li);
|
|
|
|
}
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ldap_do(struct connectdata *conn, bool *done)
|
|
|
|
{
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
|
|
|
ldapreqinfo *lr;
|
|
|
|
CURLcode status = CURLE_OK;
|
|
|
|
int rc = 0;
|
|
|
|
LDAPURLDesc *ludp = NULL;
|
|
|
|
int msgid;
|
|
|
|
struct SessionHandle *data=conn->data;
|
|
|
|
|
2014-05-20 04:32:23 -04:00
|
|
|
connkeep(conn, "OpenLDAP do");
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
infof(data, "LDAP local: %s\n", data->change.url);
|
|
|
|
|
|
|
|
rc = ldap_url_parse(data->change.url, &ludp);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc != LDAP_URL_SUCCESS) {
|
2010-05-24 18:44:42 -04:00
|
|
|
const char *msg = "url parsing problem";
|
|
|
|
status = CURLE_URL_MALFORMAT;
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
|
|
|
|
if(rc == LDAP_URL_ERR_MEM)
|
2010-05-24 18:44:42 -04:00
|
|
|
status = CURLE_OUT_OF_MEMORY;
|
|
|
|
msg = url_errs[rc];
|
|
|
|
}
|
|
|
|
failf(conn->data, "LDAP local: %s", msg);
|
|
|
|
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 09:17:42 -04:00
|
|
|
if(rc != LDAP_SUCCESS) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
|
|
|
|
return CURLE_LDAP_SEARCH_FAILED;
|
|
|
|
}
|
2015-03-17 08:41:49 -04:00
|
|
|
lr = calloc(1, sizeof(ldapreqinfo));
|
2012-06-08 14:56:22 -04:00
|
|
|
if(!lr)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2010-05-24 18:44:42 -04:00
|
|
|
lr->msgid = msgid;
|
2013-08-05 04:32:08 -04:00
|
|
|
data->req.protop = lr;
|
2010-05-24 18:44:42 -04:00
|
|
|
Curl_setup_transfer(conn, FIRSTSOCKET, -1, FALSE, NULL, -1, NULL);
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
|
|
|
|
bool premature)
|
|
|
|
{
|
2013-08-05 04:32:08 -04:00
|
|
|
ldapreqinfo *lr = conn->data->req.protop;
|
2014-12-27 16:22:45 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
(void)res;
|
|
|
|
(void)premature;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(lr) {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* if there was a search in progress, abandon it */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(lr->msgid) {
|
2010-05-24 18:44:42 -04:00
|
|
|
ldapconninfo *li = conn->proto.generic;
|
2010-05-27 16:37:00 -04:00
|
|
|
ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
|
2010-05-24 18:44:42 -04:00
|
|
|
lr->msgid = 0;
|
|
|
|
}
|
2013-08-05 04:32:08 -04:00
|
|
|
conn->data->req.protop = NULL;
|
2010-05-24 18:44:42 -04:00
|
|
|
free(lr);
|
|
|
|
}
|
2014-12-27 16:22:45 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
|
|
|
|
size_t len, CURLcode *err)
|
|
|
|
{
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
2014-12-27 16:07:53 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
2013-08-05 04:32:08 -04:00
|
|
|
ldapreqinfo *lr = data->req.protop;
|
2010-05-24 18:44:42 -04:00
|
|
|
int rc, ret;
|
2014-12-27 16:07:53 -05:00
|
|
|
LDAPMessage *msg = NULL;
|
2010-05-24 18:44:42 -04:00
|
|
|
LDAPMessage *ent;
|
|
|
|
BerElement *ber = NULL;
|
2015-03-17 08:41:49 -04:00
|
|
|
struct timeval tv = {0, 1};
|
2014-12-27 16:07:53 -05:00
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
(void)len;
|
|
|
|
(void)buf;
|
|
|
|
(void)sockindex;
|
|
|
|
|
2014-12-27 16:07:53 -05:00
|
|
|
rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc < 0) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
|
|
|
|
*err = CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*err = CURLE_AGAIN;
|
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
/* timed out */
|
2014-12-27 16:07:53 -05:00
|
|
|
if(!msg)
|
2010-05-24 18:44:42 -04:00
|
|
|
return ret;
|
|
|
|
|
2014-12-27 16:07:53 -05:00
|
|
|
for(ent = ldap_first_message(li->ld, msg); ent;
|
2010-05-24 18:44:42 -04:00
|
|
|
ent = ldap_next_message(li->ld, ent)) {
|
|
|
|
struct berval bv, *bvals, **bvp = &bvals;
|
|
|
|
int binary = 0, msgtype;
|
|
|
|
|
|
|
|
msgtype = ldap_msgtype(ent);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(msgtype == LDAP_RES_SEARCH_RESULT) {
|
2010-05-24 18:44:42 -04:00
|
|
|
int code;
|
|
|
|
char *info = NULL;
|
|
|
|
rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc) {
|
2011-03-15 19:16:34 -04:00
|
|
|
failf(data, "LDAP local: search ldap_parse_result %s",
|
|
|
|
ldap_err2string(rc));
|
2010-05-24 18:44:42 -04:00
|
|
|
*err = CURLE_LDAP_SEARCH_FAILED;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
|
2010-05-24 18:44:42 -04:00
|
|
|
failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
|
2011-04-20 09:17:42 -04:00
|
|
|
info ? info : "");
|
2010-05-24 18:44:42 -04:00
|
|
|
*err = CURLE_LDAP_SEARCH_FAILED;
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* successful */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(code == LDAP_SIZELIMIT_EXCEEDED)
|
2010-05-24 18:44:42 -04:00
|
|
|
infof(data, "There are more than %d entries\n", lr->nument);
|
|
|
|
data->req.size = data->req.bytecount;
|
|
|
|
*err = CURLE_OK;
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
lr->msgid = 0;
|
|
|
|
ldap_memfree(info);
|
|
|
|
break;
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
else if(msgtype != LDAP_RES_SEARCH_ENTRY)
|
|
|
|
continue;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
|
|
|
lr->nument++;
|
|
|
|
rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
|
2011-03-15 19:16:34 -04:00
|
|
|
if(rc < 0) {
|
|
|
|
/* TODO: verify that this is really how this return code should be
|
|
|
|
handled */
|
|
|
|
*err = CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
|
|
|
|
bv.bv_len);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount += bv.bv_len + 5;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp);
|
2010-05-24 18:44:42 -04:00
|
|
|
rc == LDAP_SUCCESS;
|
|
|
|
rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, bvp)) {
|
|
|
|
int i;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(bv.bv_val == NULL) break;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
|
2010-05-24 18:44:42 -04:00
|
|
|
binary = 1;
|
2010-11-05 10:53:41 -04:00
|
|
|
else
|
|
|
|
binary = 0;
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
for(i=0; bvals[i].bv_val != NULL; i++) {
|
2010-05-24 18:44:42 -04:00
|
|
|
int binval = 0;
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
|
|
|
|
bv.bv_len);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount += bv.bv_len + 2;
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!binary) {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* check for leading or trailing whitespace */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(ISSPACE(bvals[i].bv_val[0]) ||
|
|
|
|
ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
|
2010-05-24 18:44:42 -04:00
|
|
|
binval = 1;
|
2011-04-20 09:17:42 -04:00
|
|
|
else {
|
2010-05-24 18:44:42 -04:00
|
|
|
/* check for unprintable characters */
|
|
|
|
unsigned int j;
|
2011-04-20 09:17:42 -04:00
|
|
|
for(j=0; j<bvals[i].bv_len; j++)
|
|
|
|
if(!ISPRINT(bvals[i].bv_val[j])) {
|
2010-05-24 18:44:42 -04:00
|
|
|
binval = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
if(binary || binval) {
|
2011-08-24 02:07:36 -04:00
|
|
|
char *val_b64 = NULL;
|
|
|
|
size_t val_b64_sz = 0;
|
2010-05-24 18:44:42 -04:00
|
|
|
/* Binary value, encode to base64. */
|
2011-08-24 02:07:36 -04:00
|
|
|
CURLcode error = Curl_base64_encode(data,
|
|
|
|
bvals[i].bv_val,
|
|
|
|
bvals[i].bv_len,
|
|
|
|
&val_b64,
|
|
|
|
&val_b64_sz);
|
|
|
|
if(error) {
|
|
|
|
ber_memfree(bvals);
|
|
|
|
ber_free(ber, 0);
|
2014-12-27 16:07:53 -05:00
|
|
|
ldap_msgfree(msg);
|
2011-08-24 02:07:36 -04:00
|
|
|
*err = error;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount += 2;
|
|
|
|
if(val_b64_sz > 0) {
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
|
|
|
|
val_b64_sz);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
2010-05-24 18:44:42 -04:00
|
|
|
free(val_b64);
|
|
|
|
data->req.bytecount += val_b64_sz;
|
|
|
|
}
|
2011-04-20 09:17:42 -04:00
|
|
|
}
|
|
|
|
else {
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
|
|
|
|
bvals[i].bv_len);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount += bvals[i].bv_len + 1;
|
|
|
|
}
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
|
|
|
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount++;
|
|
|
|
}
|
|
|
|
ber_memfree(bvals);
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount++;
|
|
|
|
}
|
2014-12-08 09:30:14 -05:00
|
|
|
*err = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
|
|
|
|
if(*err)
|
|
|
|
return -1;
|
2010-05-24 18:44:42 -04:00
|
|
|
data->req.bytecount++;
|
|
|
|
ber_free(ber, 0);
|
|
|
|
}
|
2014-12-27 16:07:53 -05:00
|
|
|
ldap_msgfree(msg);
|
2010-05-24 18:44:42 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-28 06:22:35 -04:00
|
|
|
#ifdef USE_SSL
|
2010-05-24 18:44:42 -04: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 09:17:42 -04:00
|
|
|
if(opt == LBER_SB_OPT_DATA_READY) {
|
2010-05-24 18:44:42 -04:00
|
|
|
struct connectdata *conn = sbiod->sbiod_pvt;
|
|
|
|
return Curl_ssl_data_pending(conn, FIRSTSOCKET);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ber_slen_t
|
|
|
|
ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = sbiod->sbiod_pvt;
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
|
|
|
ber_slen_t ret;
|
|
|
|
CURLcode err = CURLE_RECV_ERROR;
|
|
|
|
|
|
|
|
ret = li->recv(conn, FIRSTSOCKET, buf, len, &err);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(ret < 0 && err == CURLE_AGAIN) {
|
2010-05-24 18:44:42 -04:00
|
|
|
SET_SOCKERRNO(EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ber_slen_t
|
|
|
|
ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
|
|
|
|
{
|
|
|
|
struct connectdata *conn = sbiod->sbiod_pvt;
|
|
|
|
ldapconninfo *li = conn->proto.generic;
|
|
|
|
ber_slen_t ret;
|
|
|
|
CURLcode err = CURLE_SEND_ERROR;
|
|
|
|
|
|
|
|
ret = li->send(conn, FIRSTSOCKET, buf, len, &err);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(ret < 0 && err == CURLE_AGAIN) {
|
2010-05-24 18:44:42 -04:00
|
|
|
SET_SOCKERRNO(EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
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 06:22:35 -04:00
|
|
|
#endif /* USE_SSL */
|
2010-05-24 18:44:42 -04:00
|
|
|
|
2010-06-01 11:25:03 -04:00
|
|
|
#endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */
|