1999-12-29 22:20:26 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* Copyright (C) 2000, Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* In order to be useful for every potential user, curl and libcurl are
|
|
|
|
* dual-licensed under the MPL and the MIT/X-derivate licenses.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +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 MPL or the MIT/X-derivate
|
|
|
|
* licenses. You may pick one of these licenses.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* $Id$
|
|
|
|
*****************************************************************************/
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2000-08-24 22:26:33 +08:00
|
|
|
#include "setup.h"
|
|
|
|
|
1999-12-29 22:20:26 +08: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"
|
|
|
|
|
|
|
|
#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 22:12:12 +08:00
|
|
|
return CURLE_FUNCTION_NOT_FOUND; \
|
1999-12-29 22:20:26 +08: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.
|
|
|
|
*/
|
2000-05-10 06:23:55 +08:00
|
|
|
dlopen("liblber.so",
|
|
|
|
#ifdef RTLD_LAZY_GLOBAL /* It turns out some systems use this: */
|
|
|
|
RTLD_LAZY_GLOBAL
|
|
|
|
#else
|
2000-05-23 03:04:18 +08:00
|
|
|
#ifdef RTLD_GLOBAL
|
2000-05-10 06:23:55 +08:00
|
|
|
RTLD_LAZY | RTLD_GLOBAL
|
2000-05-23 03:04:18 +08:00
|
|
|
#else
|
|
|
|
/* and some systems don't have the RTLD_GLOBAL symbol */
|
|
|
|
RTLD_LAZY
|
2000-05-23 03:09:31 +08:00
|
|
|
#endif
|
2000-05-10 06:23:55 +08:00
|
|
|
#endif
|
|
|
|
);
|
1999-12-29 22:20:26 +08:00
|
|
|
libldap = dlopen("libldap.so", RTLD_LAZY);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DynaClose(void)
|
|
|
|
{
|
|
|
|
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
|
|
|
|
if (libldap) {
|
|
|
|
dlclose(libldap);
|
|
|
|
}
|
|
|
|
if (liblber) {
|
|
|
|
dlclose(liblber);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-08-14 16:30:43 +08:00
|
|
|
static void * DynaGetFunction(const char *name)
|
1999-12-29 22:20:26 +08:00
|
|
|
{
|
|
|
|
void *func = NULL;
|
|
|
|
|
|
|
|
#if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
|
|
|
|
if (libldap) {
|
|
|
|
func = dlsym(libldap, name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return func;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int WriteProc(void *param, char *text, int len)
|
|
|
|
{
|
2001-08-31 06:48:34 +08:00
|
|
|
struct SessionHandle *data = (struct SessionHandle *)param;
|
2001-08-14 16:30:43 +08:00
|
|
|
len = 0; /* prevent compiler warning */
|
2001-01-05 18:11:41 +08:00
|
|
|
Curl_client_write(data, CLIENTWRITE_BODY, text, 0);
|
1999-12-29 22:20:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
*/
|
2001-01-05 18:11:41 +08:00
|
|
|
CURLcode Curl_ldap(struct connectdata *conn)
|
1999-12-29 22:20:26 +08:00
|
|
|
{
|
2000-05-22 22:12:12 +08:00
|
|
|
CURLcode status = CURLE_OK;
|
1999-12-29 22:20:26 +08:00
|
|
|
int rc;
|
|
|
|
void *(*ldap_open)(char *, int);
|
|
|
|
int (*ldap_simple_bind_s)(void *, char *, char *);
|
|
|
|
int (*ldap_unbind_s)(void *);
|
|
|
|
int (*ldap_url_search_s)(void *, char *, int, void **);
|
|
|
|
void *(*ldap_first_entry)(void *, void *);
|
|
|
|
void *(*ldap_next_entry)(void *, void *);
|
|
|
|
char *(*ldap_err2string)(int);
|
|
|
|
int (*ldap_entry2text)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long);
|
|
|
|
int (*ldap_entry2html)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *);
|
|
|
|
void *server;
|
|
|
|
void *result;
|
|
|
|
void *entryIterator;
|
2000-05-22 22:12:12 +08:00
|
|
|
|
1999-12-29 22:20:26 +08:00
|
|
|
int ldaptext;
|
2001-08-31 06:48:34 +08:00
|
|
|
struct SessionHandle *data=conn->data;
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2001-08-31 06:48:34 +08:00
|
|
|
infof(data, "LDAP: %s %s\n", data->change.url);
|
1999-12-29 22:20:26 +08:00
|
|
|
|
|
|
|
DynaOpen();
|
|
|
|
if (libldap == NULL) {
|
|
|
|
failf(data, "The needed LDAP library/libraries couldn't be opened");
|
2000-05-22 22:12:12 +08:00
|
|
|
return CURLE_LIBRARY_NOT_FOUND;
|
1999-12-29 22:20:26 +08:00
|
|
|
}
|
|
|
|
|
2001-08-31 06:48:34 +08:00
|
|
|
ldaptext = data->set.ftp_ascii; /* This is a dirty hack */
|
1999-12-29 22:20:26 +08:00
|
|
|
|
|
|
|
/* The types are needed because ANSI C distinguishes between
|
|
|
|
* pointer-to-object (data) and pointer-to-function.
|
|
|
|
*/
|
|
|
|
DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_open);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, int, void **), ldap_url_search_s);
|
|
|
|
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);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long), ldap_entry2text);
|
|
|
|
DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *), ldap_entry2html);
|
|
|
|
|
2001-02-21 01:35:51 +08:00
|
|
|
server = ldap_open(conn->hostname, conn->port);
|
1999-12-29 22:20:26 +08:00
|
|
|
if (server == NULL) {
|
|
|
|
failf(data, "LDAP: Cannot connect to %s:%d",
|
2001-02-21 01:35:51 +08:00
|
|
|
conn->hostname, conn->port);
|
2000-05-22 22:12:12 +08:00
|
|
|
status = CURLE_COULDNT_CONNECT;
|
1999-12-29 22:20:26 +08:00
|
|
|
} else {
|
2001-08-31 06:48:34 +08:00
|
|
|
rc = ldap_simple_bind_s(server, data->state.user, data->state.passwd);
|
1999-12-29 22:20:26 +08:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2000-05-22 22:12:12 +08:00
|
|
|
status = CURLE_LDAP_CANNOT_BIND;
|
1999-12-29 22:20:26 +08:00
|
|
|
} else {
|
2001-08-31 06:48:34 +08:00
|
|
|
rc = ldap_url_search_s(server, data->change.url, 0, &result);
|
1999-12-29 22:20:26 +08:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2000-05-22 22:12:12 +08:00
|
|
|
status = CURLE_LDAP_SEARCH_FAILED;
|
1999-12-29 22:20:26 +08:00
|
|
|
} else {
|
|
|
|
for (entryIterator = ldap_first_entry(server, result);
|
|
|
|
entryIterator;
|
|
|
|
entryIterator = ldap_next_entry(server, entryIterator))
|
|
|
|
{
|
|
|
|
if (ldaptext) {
|
|
|
|
rc = ldap_entry2text(server, NULL, entryIterator, NULL,
|
|
|
|
NULL, NULL, WriteProc, data,
|
2001-08-14 16:30:43 +08:00
|
|
|
(char *)"", 0, 0);
|
1999-12-29 22:20:26 +08:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2000-05-22 22:12:12 +08:00
|
|
|
status = CURLE_LDAP_SEARCH_FAILED;
|
1999-12-29 22:20:26 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rc = ldap_entry2html(server, NULL, entryIterator, NULL,
|
|
|
|
NULL, NULL, WriteProc, data,
|
2001-08-14 16:30:43 +08:00
|
|
|
(char *)"", 0, 0, NULL, NULL);
|
1999-12-29 22:20:26 +08:00
|
|
|
if (rc != 0) {
|
|
|
|
failf(data, "LDAP: %s", ldap_err2string(rc));
|
2000-05-22 22:12:12 +08:00
|
|
|
status = CURLE_LDAP_SEARCH_FAILED;
|
1999-12-29 22:20:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ldap_unbind_s(server);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DynaClose();
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2001-09-07 12:01:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* local variables:
|
|
|
|
* eval: (load-file "../curl-mode.el")
|
|
|
|
* end:
|
|
|
|
* vim600: et sw=2 ts=2 sts=2 tw=78 fdm=marker
|
|
|
|
* vim<600: et sw=2 ts=2 sts=2 tw=78
|
|
|
|
*/
|