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>
31 * $Source: /cvsroot/curl/curl/lib/ldap.c,v $
32 * $Revision: 1.1.1.1 $
33 * $Date: 1999-12-29 14:21:32 $
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 URG_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.
92 dlopen("liblber.so", RTLD_LAZY
| RTLD_GLOBAL
);
93 libldap
= dlopen("libldap.so", RTLD_LAZY
);
98 static void DynaClose(void)
100 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
110 static void * DynaGetFunction(char *name
)
114 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
116 func
= dlsym(libldap
, name
);
123 static int WriteProc(void *param
, char *text
, int len
)
125 struct UrlData
*data
= (struct UrlData
*)param
;
127 printf("%s\n", text
);
131 /***********************************************************************
133 UrgError
ldap(struct UrlData
*data
, char *path
, long *bytecount
)
135 UrgError status
= URG_OK
;
137 void *(*ldap_open
)(char *, int);
138 int (*ldap_simple_bind_s
)(void *, char *, char *);
139 int (*ldap_unbind_s
)(void *);
140 int (*ldap_url_search_s
)(void *, char *, int, void **);
141 void *(*ldap_first_entry
)(void *, void *);
142 void *(*ldap_next_entry
)(void *, void *);
143 char *(*ldap_err2string
)(int);
144 int (*ldap_entry2text
)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long);
145 int (*ldap_entry2html
)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *);
158 infof(data
, "LDAP: %s %s\n", data
->url
);
161 if (libldap
== NULL
) {
162 failf(data
, "The needed LDAP library/libraries couldn't be opened");
163 return URG_LIBRARY_NOT_FOUND
;
166 ldaptext
= data
->conf
& CONF_FTPASCII
; /* This is a dirty hack */
168 /* The types are needed because ANSI C distinguishes between
169 * pointer-to-object (data) and pointer-to-function.
171 DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_open
);
172 DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s
);
173 DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s
);
174 DYNA_GET_FUNCTION(int (*)(void *, char *, int, void **), ldap_url_search_s
);
175 DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry
);
176 DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry
);
177 DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string
);
178 DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long), ldap_entry2text
);
179 DYNA_GET_FUNCTION(int (*)(void *, char *, void *, void *, char **, char **, int (*)(void *, char *, int), void *, char *, int, unsigned long, char *, char *), ldap_entry2html
);
181 server
= ldap_open(data
->hostname
, data
->port
);
182 if (server
== NULL
) {
183 failf(data
, "LDAP: Cannot connect to %s:%d",
184 data
->hostname
, data
->port
);
185 status
= URG_COULDNT_CONNECT
;
187 rc
= ldap_simple_bind_s(server
, data
->user
, data
->passwd
);
189 failf(data
, "LDAP: %s", ldap_err2string(rc
));
190 status
= URG_LDAP_CANNOT_BIND
;
192 rc
= ldap_url_search_s(server
, data
->url
, 0, &result
);
194 failf(data
, "LDAP: %s", ldap_err2string(rc
));
195 status
= URG_LDAP_SEARCH_FAILED
;
197 for (entryIterator
= ldap_first_entry(server
, result
);
199 entryIterator
= ldap_next_entry(server
, entryIterator
))
202 rc
= ldap_entry2text(server
, NULL
, entryIterator
, NULL
,
203 NULL
, NULL
, WriteProc
, data
,
206 failf(data
, "LDAP: %s", ldap_err2string(rc
));
207 status
= URG_LDAP_SEARCH_FAILED
;
210 rc
= ldap_entry2html(server
, NULL
, entryIterator
, NULL
,
211 NULL
, NULL
, WriteProc
, data
,
212 "", 0, 0, NULL
, NULL
);
214 failf(data
, "LDAP: %s", ldap_err2string(rc
));
215 status
= URG_LDAP_SEARCH_FAILED
;
220 ldap_unbind_s(server
);