new
[libcurl.git] / lib / ldap.c
blob9f92cf65918250930e8959f2b857654d74a19ebc
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
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
16 * under the License.
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 * ------------------------------------------------------------
26 * Contributor(s):
27 * Bjørn Reese <breese@mail1.stofanet.dk>
29 * http://curl.haxx.nu
31 * $Source: /cvsroot/curl/curl/lib/ldap.c,v $
32 * $Revision: 1.1.1.1 $
33 * $Date: 1999-12-29 14:21:32 $
34 * $Author: bagder $
35 * $State: Exp $
36 * $Locker: $
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 /* -- WIN32 approved -- */
42 #include <stdio.h>
43 #include <string.h>
44 #include <stdarg.h>
45 #include <stdlib.h>
46 #include <ctype.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <errno.h>
51 #include "setup.h"
53 #if defined(WIN32) && !defined(__GNUC__)
54 #else
55 # ifdef HAVE_UNISTD_H
56 # include <unistd.h>
57 # endif
58 # ifdef HAVE_DLFCN_H
59 # include <dlfcn.h>
60 # endif
61 #endif
63 #include "urldata.h"
64 #include <curl/curl.h>
65 #include "sendf.h"
66 #include "escape.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; \
76 } \
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);
95 #endif
98 static void DynaClose(void)
100 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
101 if (libldap) {
102 dlclose(libldap);
104 if (liblber) {
105 dlclose(liblber);
107 #endif
110 static void * DynaGetFunction(char *name)
112 void *func = NULL;
114 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
115 if (libldap) {
116 func = dlsym(libldap, name);
118 #endif
120 return func;
123 static int WriteProc(void *param, char *text, int len)
125 struct UrlData *data = (struct UrlData *)param;
127 printf("%s\n", text);
128 return 0;
131 /***********************************************************************
133 UrgError ldap(struct UrlData *data, char *path, long *bytecount)
135 UrgError status = URG_OK;
136 int rc;
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 *);
146 void *server;
147 void *result;
148 void *entryIterator;
149 #if 0
150 char *dn;
151 char **attrArray;
152 char *attrIterator;
153 char *attrString;
154 void *dummy;
155 #endif
156 int ldaptext;
158 infof(data, "LDAP: %s %s\n", data->url);
160 DynaOpen();
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;
186 } else {
187 rc = ldap_simple_bind_s(server, data->user, data->passwd);
188 if (rc != 0) {
189 failf(data, "LDAP: %s", ldap_err2string(rc));
190 status = URG_LDAP_CANNOT_BIND;
191 } else {
192 rc = ldap_url_search_s(server, data->url, 0, &result);
193 if (rc != 0) {
194 failf(data, "LDAP: %s", ldap_err2string(rc));
195 status = URG_LDAP_SEARCH_FAILED;
196 } else {
197 for (entryIterator = ldap_first_entry(server, result);
198 entryIterator;
199 entryIterator = ldap_next_entry(server, entryIterator))
201 if (ldaptext) {
202 rc = ldap_entry2text(server, NULL, entryIterator, NULL,
203 NULL, NULL, WriteProc, data,
204 "", 0, 0);
205 if (rc != 0) {
206 failf(data, "LDAP: %s", ldap_err2string(rc));
207 status = URG_LDAP_SEARCH_FAILED;
209 } else {
210 rc = ldap_entry2html(server, NULL, entryIterator, NULL,
211 NULL, NULL, WriteProc, data,
212 "", 0, 0, NULL, NULL);
213 if (rc != 0) {
214 failf(data, "LDAP: %s", ldap_err2string(rc));
215 status = URG_LDAP_SEARCH_FAILED;
220 ldap_unbind_s(server);
223 DynaClose();
225 return status;