1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
27 * Bjørn Reese <breese@mail1.stofanet.dk>
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 /* -- WIN32 approved -- */
47 #include <sys/types.h>
53 #if defined(WIN32) && !defined(__GNUC__)
64 #include <curl/curl.h>
68 #define _MPRINTF_REPLACE /* use our functions only */
69 #include <curl/mprintf.h>
72 #define DYNA_GET_FUNCTION(type, fnc) \
73 (fnc) = (type)DynaGetFunction(#fnc); \
74 if ((fnc) == NULL) { \
75 return CURLE_FUNCTION_NOT_FOUND; \
78 /***********************************************************************
80 static void *libldap = NULL;
81 static void *liblber = NULL;
83 static void DynaOpen(void)
85 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
86 if (libldap == NULL) {
88 * libldap.so should be able to resolve its dependency on
89 * liblber.so automatically, but since it does not we will
90 * handle it here by opening liblber.so as global.
93 #ifdef RTLD_LAZY_GLOBAL /* It turns out some systems use this: */
96 RTLD_LAZY | RTLD_GLOBAL
99 libldap = dlopen("libldap.so", RTLD_LAZY);
104 static void DynaClose(void)
106 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
116 static void * DynaGetFunction(char *name)
120 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
122 func = dlsym(libldap, name);
129 static int WriteProc(void *param, char *text, int len)
131 struct UrlData *data = (struct UrlData *)param;
133 data->fwrite(text, 1, strlen(text), data->out);
137 CURLcode ldap_done(struct connectdata *conn)
142 /***********************************************************************
144 CURLcode ldap(struct connectdata *conn)
146 CURLcode status = CURLE_OK;
148 void *(*ldap_open)(char *, int);
149 int (*ldap_simple_bind_s)(void *, char *, char *);
150 int (*ldap_unbind_s)(void *);
151 int (*ldap_url_search_s)(void *, char *, int, void **);
152 void *(*ldap_first_entry)(void *, void *);
153 void *(*ldap_next_entry)(void *, void *);
154 char *(*ldap_err2string)(int);
155 int (*ldap_entry2text)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long);
156 int (*ldap_entry2html)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *);
162 struct UrlData *data=conn->data;
164 infof(data, "LDAP: %s %s\n", data->url);
167 if (libldap == NULL) {
168 failf(data, "The needed LDAP library/libraries couldn't be opened");
169 return CURLE_LIBRARY_NOT_FOUND;
172 ldaptext = data->bits.ftp_ascii; /* This is a dirty hack */
174 /* The types are needed because ANSI C distinguishes between
175 * pointer-to-object (data) and pointer-to-function.
177 DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_open);
178 DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s);
179 DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s);
180 DYNA_GET_FUNCTION(int (*)(void *, char *, int, void **), ldap_url_search_s);
181 DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry);
182 DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry);
183 DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string);
184 DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long), ldap_entry2text);
185 DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *), ldap_entry2html);
187 server = ldap_open(data->hostname, data->port);
188 if (server == NULL) {
189 failf(data, "LDAP: Cannot connect to %s:%d",
190 data->hostname, data->port);
191 status = CURLE_COULDNT_CONNECT;
193 rc = ldap_simple_bind_s(server, data->user, data->passwd);
195 failf(data, "LDAP: %s", ldap_err2string(rc));
196 status = CURLE_LDAP_CANNOT_BIND;
198 rc = ldap_url_search_s(server, data->url, 0, &result);
200 failf(data, "LDAP: %s", ldap_err2string(rc));
201 status = CURLE_LDAP_SEARCH_FAILED;
203 for (entryIterator = ldap_first_entry(server, result);
205 entryIterator = ldap_next_entry(server, entryIterator))
208 rc = ldap_entry2text(server, NULL, entryIterator, NULL,
209 NULL, NULL, WriteProc, data,
212 failf(data, "LDAP: %s", ldap_err2string(rc));
213 status = CURLE_LDAP_SEARCH_FAILED;
216 rc = ldap_entry2html(server, NULL, entryIterator, NULL,
217 NULL, NULL, WriteProc, data,
218 "", 0, 0, NULL, NULL);
220 failf(data, "LDAP: %s", ldap_err2string(rc));
221 status = CURLE_LDAP_SEARCH_FAILED;
226 ldap_unbind_s(server);