2002-09-03 07:52:59 -04:00
|
|
|
/***************************************************************************
|
1999-12-29 09:20:26 -05:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2004-01-07 04:19:33 -05:00
|
|
|
* Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2002-09-03 07:52:59 -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.
|
|
|
|
*
|
2001-01-03 04:29:33 -05: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
|
2002-09-03 07:52:59 -04:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 09:20:26 -05:00
|
|
|
*
|
2001-01-03 04:29:33 -05:00
|
|
|
* $Id$
|
2002-09-03 07:52:59 -04:00
|
|
|
***************************************************************************/
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2000-08-24 10:26:33 -04:00
|
|
|
#include "setup.h"
|
|
|
|
|
2002-06-11 07:13:01 -04:00
|
|
|
#ifndef CURL_DISABLE_LDAP
|
1999-12-29 09:20:26 -05:00
|
|
|
/* -- WIN32 approved -- */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#if defined(WIN32) && !defined(__GNUC__)
|
|
|
|
#else
|
|
|
|
# ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
# endif
|
|
|
|
# ifdef HAVE_DLFCN_H
|
|
|
|
# include <dlfcn.h>
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "escape.h"
|
2001-10-25 04:28:29 -04:00
|
|
|
#include "transfer.h"
|
2004-01-29 08:56:45 -05:00
|
|
|
#include "ldap.h"
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define DYNA_GET_FUNCTION(type, fnc) \
|
|
|
|
(fnc) = (type)DynaGetFunction(#fnc); \
|
|
|
|
if ((fnc) == NULL) { \
|
2000-05-22 10:12:12 -04:00
|
|
|
return CURLE_FUNCTION_NOT_FOUND; \
|
1999-12-29 09:20:26 -05:00
|
|
|
} \
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*/
|
|
|
|
static void *libldap = NULL;
|
|
|
|
static void *liblber = NULL;
|
|
|
|
|
|
|
|
static void DynaOpen(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
|
|
|
|
if (libldap == NULL) {
|
|
|
|
/*
|
|
|
|
* libldap.so should be able to resolve its dependency on
|
|
|
|
* liblber.so automatically, but since it does not we will
|
|
|
|
* handle it here by opening liblber.so as global.
|
|
|
|
*/
|
2003-07-01 06:12:52 -04:00
|
|
|
liblber = dlopen("liblber.so",
|
2000-05-09 18:23:55 -04:00
|
|
|
#ifdef RTLD_LAZY_GLOBAL /* It turns out some systems use this: */
|
|
|
|
RTLD_LAZY_GLOBAL
|
|
|
|
#else
|
2000-05-22 15:04:18 -04:00
|
|
|
#ifdef RTLD_GLOBAL
|
2000-05-09 18:23:55 -04:00
|
|
|
RTLD_LAZY | RTLD_GLOBAL
|
2000-05-22 15:04:18 -04:00
|
|
|
#else
|
|
|
|
/* and some systems don't have the RTLD_GLOBAL symbol */
|
|
|
|
RTLD_LAZY
|
2000-05-22 15:09:31 -04:00
|
|
|
#endif
|
2000-05-09 18:23:55 -04:00
|
|
|
#endif
|
|
|
|
);
|
1999-12-29 09:20:26 -05:00
|
|
|
libldap = dlopen("libldap.so", RTLD_LAZY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DynaClose(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
|
|
|
|
if (libldap) {
|
|
|
|
dlclose(libldap);
|
2001-11-05 09:04:57 -05:00
|
|
|
libldap=NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
if (liblber) {
|
|
|
|
dlclose(liblber);
|
2001-11-05 09:04:57 -05:00
|
|
|
liblber=NULL;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-14 04:30:43 -04:00
|
|
|
static void * DynaGetFunction(const char *name)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
|
|
|
void *func = NULL;
|
|
|
|
|
|
|
|
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
|
|
|
|
if (libldap) {
|
|
|
|
func = dlsym(libldap, name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*/
|
2003-09-01 04:23:31 -04:00
|
|
|
typedef struct ldap_url_desc {
|
|
|
|
struct ldap_url_desc *lud_next;
|
|
|
|
char *lud_scheme;
|
|
|
|
char *lud_host;
|
|
|
|
int lud_port;
|
|
|
|
char *lud_dn;
|
|
|
|
char **lud_attrs;
|
|
|
|
int lud_scope;
|
|
|
|
char *lud_filter;
|
|
|
|
char **lud_exts;
|
|
|
|
int lud_crit_exts;
|
|
|
|
} LDAPURLDesc;
|
|
|
|
|
|
|
|
|
2001-01-05 05:11:41 -05:00
|
|
|
CURLcode Curl_ldap(struct connectdata *conn)
|
1999-12-29 09:20:26 -05:00
|
|
|
{
|
2000-05-22 10:12:12 -04:00
|
|
|
CURLcode status = CURLE_OK;
|
1999-12-29 09:20:26 -05:00
|
|
|
int rc;
|
2003-09-01 04:23:31 -04:00
|
|
|
void *(*ldap_init)(char *, int);
|
1999-12-29 09:20:26 -05:00
|
|
|
int (*ldap_simple_bind_s)(void *, char *, char *);
|
|
|
|
int (*ldap_unbind_s)(void *);
|
2003-09-01 04:23:31 -04:00
|
|
|
int (*ldap_url_parse)(char *, LDAPURLDesc **);
|
|
|
|
void (*ldap_free_urldesc)(void *);
|
|
|
|
int (*ldap_search_s)(void *, char *, int, char *, char **, int, void **);
|
|
|
|
int (*ldap_search_st)(void *, char *, int, char *, char **, int, void *, void **);
|
1999-12-29 09:20:26 -05:00
|
|
|
void *(*ldap_first_entry)(void *, void *);
|
|
|
|
void *(*ldap_next_entry)(void *, void *);
|
|
|
|
char *(*ldap_err2string)(int);
|
2003-09-01 04:23:31 -04:00
|
|
|
char *(*ldap_get_dn)(void *, void *);
|
|
|
|
char *(*ldap_first_attribute)(void *, void *, void **);
|
|
|
|
char *(*ldap_next_attribute)(void *, void *, void *);
|
|
|
|
char **(*ldap_get_values)(void *, void *, char *);
|
|
|
|
void (*ldap_value_free)(char **);
|
|
|
|
void (*ldap_memfree)(void *);
|
|
|
|
void (*ber_free)(void *, int);
|
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
void *server;
|
2003-09-01 04:23:31 -04:00
|
|
|
LDAPURLDesc *ludp;
|
1999-12-29 09:20:26 -05:00
|
|
|
void *result;
|
|
|
|
void *entryIterator;
|
2003-09-01 04:23:31 -04:00
|
|
|
void *ber;
|
|
|
|
void *attribute;
|
2000-05-22 10:12:12 -04:00
|
|
|
|
1999-12-29 09:20:26 -05:00
|
|
|
int ldaptext;
|
2001-08-30 18:48:34 -04:00
|
|
|
struct SessionHandle *data=conn->data;
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2002-06-12 03:45:11 -04:00
|
|
|
infof(data, "LDAP: %s\n", data->change.url);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
DynaOpen();
|
|
|
|
if (libldap == NULL) {
|
|
|
|
failf(data, "The needed LDAP library/libraries couldn't be opened");
|
2000-05-22 10:12:12 -04:00
|
|
|
return CURLE_LIBRARY_NOT_FOUND;
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
|
2001-08-30 18:48:34 -04:00
|
|
|
ldaptext = data->set.ftp_ascii; /* This is a dirty hack */
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
/* The types are needed because ANSI C distinguishes between
|
|
|
|
* pointer-to-object (data) and pointer-to-function.
|
|
|
|
*/
|
2003-09-01 04:23:31 -04:00
|
|
|
DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_init);
|
1999-12-29 09:20:26 -05:00
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s);
|
2003-09-01 04:23:31 -04:00
|
|
|
DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
|
|
|
|
DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void **), ldap_search_s);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void *, void **), ldap_search_st);
|
1999-12-29 09:20:26 -05:00
|
|
|
DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry);
|
|
|
|
DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry);
|
|
|
|
DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string);
|
2003-09-01 04:23:31 -04:00
|
|
|
DYNA_GET_FUNCTION(char *(*)(void *, void *), ldap_get_dn);
|
|
|
|
DYNA_GET_FUNCTION(char *(*)(void *, void *, void **), ldap_first_attribute);
|
|
|
|
DYNA_GET_FUNCTION(char *(*)(void *, void *, void *), ldap_next_attribute);
|
|
|
|
DYNA_GET_FUNCTION(char **(*)(void *, void *, char *), ldap_get_values);
|
|
|
|
DYNA_GET_FUNCTION(void (*)(char **), ldap_value_free);
|
|
|
|
DYNA_GET_FUNCTION(void (*)(void *), ldap_memfree);
|
|
|
|
DYNA_GET_FUNCTION(void (*)(void *, int), ber_free);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
2003-09-01 04:23:31 -04:00
|
|
|
server = ldap_init(conn->hostname, conn->port);
|
1999-12-29 09:20:26 -05:00
|
|
|
if (server == NULL) {
|
|
|
|
failf(data, "LDAP: Cannot connect to %s:%d",
|
2001-02-20 12:35:51 -05:00
|
|
|
conn->hostname, conn->port);
|
2000-05-22 10:12:12 -04:00
|
|
|
status = CURLE_COULDNT_CONNECT;
|
1999-12-29 09:20:26 -05:00
|
|
|
} else {
|
2001-11-05 09:04:57 -05:00
|
|
|
rc = ldap_simple_bind_s(server,
|
2003-07-19 19:56:33 -04:00
|
|
|
conn->bits.user_passwd?conn->user:NULL,
|
|
|
|
conn->bits.user_passwd?conn->passwd:NULL);
|
1999-12-29 09:20:26 -05:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2000-05-22 10:12:12 -04:00
|
|
|
status = CURLE_LDAP_CANNOT_BIND;
|
1999-12-29 09:20:26 -05:00
|
|
|
} else {
|
2003-09-01 04:23:31 -04:00
|
|
|
rc = ldap_url_parse(data->change.url, &ludp);
|
1999-12-29 09:20:26 -05:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2003-09-01 04:23:31 -04:00
|
|
|
status = CURLE_LDAP_INVALID_URL;
|
1999-12-29 09:20:26 -05:00
|
|
|
} else {
|
2003-09-01 04:23:31 -04:00
|
|
|
rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope, ludp->lud_filter, ludp->lud_attrs, 0, &result);
|
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
|
|
|
status = CURLE_LDAP_SEARCH_FAILED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (entryIterator = ldap_first_entry(server, result);
|
|
|
|
entryIterator;
|
|
|
|
entryIterator = ldap_next_entry(server, entryIterator))
|
|
|
|
{
|
|
|
|
char *dn = ldap_get_dn(server, entryIterator);
|
|
|
|
char **vals;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, dn, 0);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
|
|
|
|
for(attribute = ldap_first_attribute(server, entryIterator,
|
|
|
|
&ber);
|
|
|
|
attribute;
|
|
|
|
attribute = ldap_next_attribute(server, entryIterator,
|
|
|
|
ber) ) {
|
|
|
|
vals = ldap_get_values(server, entryIterator, attribute);
|
|
|
|
if (vals != NULL) {
|
|
|
|
for(i = 0; (vals[i] != NULL); i++) {
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char*)"\t", 1);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, attribute, 0);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, vals[i], 0);
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free memory used to store values */
|
|
|
|
ldap_value_free(vals);
|
|
|
|
}
|
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
|
|
|
|
|
|
|
|
ldap_memfree(attribute);
|
|
|
|
ldap_memfree(dn);
|
|
|
|
if (ber) ber_free(ber, 0);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
2003-09-01 04:23:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ldap_free_urldesc(ludp);
|
1999-12-29 09:20:26 -05:00
|
|
|
}
|
|
|
|
ldap_unbind_s(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DynaClose();
|
2001-10-25 04:28:29 -04:00
|
|
|
|
|
|
|
/* no data to transfer */
|
|
|
|
Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
|
1999-12-29 09:20:26 -05:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2002-06-11 07:13:01 -04:00
|
|
|
#endif
|