doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ldap.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  * $Id: ldap.c,v 1.32 2004/02/12 09:51:43 bagder Exp $
22  ***************************************************************************/
23 
24 #include "setup.h"
25 
26 #ifndef CURL_DISABLE_LDAP
27 /* -- WIN32 approved -- */
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <errno.h>
36 
37 #if defined(WIN32) && !defined(__GNUC__)
38 #else
39 # ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 # endif
42 # ifdef HAVE_DLFCN_H
43 # include <dlfcn.h>
44 # endif
45 #endif
46 
47 #include "urldata.h"
48 #include <curl/curl.h>
49 #include "sendf.h"
50 #include "escape.h"
51 #include "transfer.h"
52 #include "ldap.h"
53 
54 #define _MPRINTF_REPLACE /* use our functions only */
55 #include <curl/mprintf.h>
56 
57 typedef void * (*dynafunc)(void *input);
58 
59 #define DYNA_GET_FUNCTION(type, fnc) \
60  (fnc) = (type)DynaGetFunction(#fnc); \
61  if ((fnc) == NULL) { \
62  return CURLE_FUNCTION_NOT_FOUND; \
63  }
64 
65 /***********************************************************************
66  */
67 static void *libldap = NULL;
68 static void *liblber = NULL;
69 
70 static void DynaOpen(void)
71 {
72 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
73  if (libldap == NULL) {
74  /*
75  * libldap.so should be able to resolve its dependency on
76  * liblber.so automatically, but since it does not we will
77  * handle it here by opening liblber.so as global.
78  */
79  liblber = dlopen("liblber.so",
80 #ifdef RTLD_LAZY_GLOBAL /* It turns out some systems use this: */
81  RTLD_LAZY_GLOBAL
82 #else
83 #ifdef RTLD_GLOBAL
84  RTLD_LAZY | RTLD_GLOBAL
85 #else
86  /* and some systems don't have the RTLD_GLOBAL symbol */
87  RTLD_LAZY
88 #endif
89 #endif
90  );
91  libldap = dlopen("libldap.so", RTLD_LAZY);
92  }
93 #endif
94 }
95 
96 static void DynaClose(void)
97 {
98 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
99  if (libldap) {
100  dlclose(libldap);
101  libldap=NULL;
102  }
103  if (liblber) {
104  dlclose(liblber);
105  liblber=NULL;
106  }
107 #endif
108 }
109 
110 static dynafunc DynaGetFunction(const char *name)
111 {
112  dynafunc func = (dynafunc)NULL;
113 
114 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
115  if (libldap) {
116  func = (dynafunc) dlsym(libldap, name);
117  }
118 #endif
119 
120  return func;
121 }
122 
123 /***********************************************************************
124  */
125 typedef struct ldap_url_desc {
127  char *lud_scheme;
128  char *lud_host;
129  int lud_port;
130  char *lud_dn;
131  char **lud_attrs;
133  char *lud_filter;
134  char **lud_exts;
136 } LDAPURLDesc;
137 
138 
140 {
141  CURLcode status = CURLE_OK;
142  int rc;
143  void *(*ldap_init)(char *, int);
144  int (*ldap_simple_bind_s)(void *, char *, char *);
145  int (*ldap_unbind_s)(void *);
146  int (*ldap_url_parse)(char *, LDAPURLDesc **);
147  void (*ldap_free_urldesc)(void *);
148  int (*ldap_search_s)(void *, char *, int, char *, char **, int, void **);
149  int (*ldap_search_st)(void *, char *, int, char *, char **, int, void *, void **);
150  void *(*ldap_first_entry)(void *, void *);
151  void *(*ldap_next_entry)(void *, void *);
152  char *(*ldap_err2string)(int);
153  char *(*ldap_get_dn)(void *, void *);
154  char *(*ldap_first_attribute)(void *, void *, void **);
155  char *(*ldap_next_attribute)(void *, void *, void *);
156  char **(*ldap_get_values)(void *, void *, char *);
157  void (*ldap_value_free)(char **);
158  void (*ldap_memfree)(void *);
159  void (*ber_free)(void *, int);
160 
161  void *server;
162  LDAPURLDesc *ludp;
163  void *result;
164  void *entryIterator;
165  void *ber;
166  void *attribute;
167 
168  struct SessionHandle *data=conn->data;
169 
170  infof(data, "LDAP: %s\n", data->change.url);
171 
172  DynaOpen();
173  if (libldap == NULL) {
174  failf(data, "The needed LDAP library/libraries couldn't be opened");
176  }
177 
178  /* The types are needed because ANSI C distinguishes between
179  * pointer-to-object (data) and pointer-to-function.
180  */
181  DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_init);
182  DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s);
183  DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s);
184  DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
185  DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
186  DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void **), ldap_search_s);
187  DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void *, void **), ldap_search_st);
188  DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry);
189  DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry);
190  DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string);
191  DYNA_GET_FUNCTION(char *(*)(void *, void *), ldap_get_dn);
192  DYNA_GET_FUNCTION(char *(*)(void *, void *, void **), ldap_first_attribute);
193  DYNA_GET_FUNCTION(char *(*)(void *, void *, void *), ldap_next_attribute);
194  DYNA_GET_FUNCTION(char **(*)(void *, void *, char *), ldap_get_values);
195  DYNA_GET_FUNCTION(void (*)(char **), ldap_value_free);
196  DYNA_GET_FUNCTION(void (*)(void *), ldap_memfree);
197  DYNA_GET_FUNCTION(void (*)(void *, int), ber_free);
198 
199  server = ldap_init(conn->hostname, conn->port);
200  if (server == NULL) {
201  failf(data, "LDAP: Cannot connect to %s:%d",
202  conn->hostname, conn->port);
203  status = CURLE_COULDNT_CONNECT;
204  }
205  else {
206  rc = ldap_simple_bind_s(server,
207  conn->bits.user_passwd?conn->user:NULL,
208  conn->bits.user_passwd?conn->passwd:NULL);
209  if (rc != 0) {
210  failf(data, "LDAP: %s", ldap_err2string(rc));
211  status = CURLE_LDAP_CANNOT_BIND;
212  }
213  else {
214  rc = ldap_url_parse(data->change.url, &ludp);
215  if (rc != 0) {
216  failf(data, "LDAP: %s", ldap_err2string(rc));
217  status = CURLE_LDAP_INVALID_URL;
218  }
219  else {
220  rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
221  ludp->lud_filter, ludp->lud_attrs, 0, &result);
222  if (rc != 0) {
223  failf(data, "LDAP: %s", ldap_err2string(rc));
224  status = CURLE_LDAP_SEARCH_FAILED;
225  }
226  else {
227  for (entryIterator = ldap_first_entry(server, result);
228  entryIterator;
229  entryIterator = ldap_next_entry(server, entryIterator)) {
230  char *dn = ldap_get_dn(server, entryIterator);
231  char **vals;
232  int i;
233 
234  Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
235  Curl_client_write(data, CLIENTWRITE_BODY, dn, 0);
236  Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
237  for(attribute = ldap_first_attribute(server, entryIterator,
238  &ber);
239  attribute;
240  attribute = ldap_next_attribute(server, entryIterator,
241  ber) ) {
242  vals = ldap_get_values(server, entryIterator, attribute);
243  if (vals != NULL) {
244  for(i = 0; (vals[i] != NULL); i++) {
245  Curl_client_write(data, CLIENTWRITE_BODY, (char*)"\t", 1);
246  Curl_client_write(data, CLIENTWRITE_BODY, attribute, 0);
247  Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
248  Curl_client_write(data, CLIENTWRITE_BODY, vals[i], 0);
249  Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
250  }
251  }
252 
253  /* Free memory used to store values */
254  ldap_value_free(vals);
255  }
256  Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
257 
258  ldap_memfree(attribute);
259  ldap_memfree(dn);
260  if (ber) ber_free(ber, 0);
261  }
262  }
263 
264  ldap_free_urldesc(ludp);
265  }
266  ldap_unbind_s(server);
267  }
268  }
269  DynaClose();
270 
271  /* no data to transfer */
272  Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
273 
274  return status;
275 }
276 #endif
#define CLIENTWRITE_BODY
Definition: sendf.h:34
struct ConnectBits bits
Definition: urldata.h:462
char * lud_scheme
Definition: ldap.c:127
struct DynamicStatic change
Definition: urldata.h:899
char * lud_host
Definition: ldap.c:128
#define failf
Definition: sendf.h:32
case const int
Definition: Callbacks.cpp:52
CURLcode Curl_client_write(struct SessionHandle *data, int type, char *ptr, size_t len)
Definition: sendf.c:319
CURLcode
Definition: curl.h:209
int i
Definition: process.py:33
Boolean result
CURLcode Curl_ldap(struct connectdata *conn)
Definition: ldap.c:139
char ** lud_exts
Definition: ldap.c:134
char * passwd
Definition: urldata.h:447
char * hostname
Definition: urldata.h:432
char * lud_filter
Definition: ldap.c:133
char * url
Definition: urldata.h:734
#define NULL
Definition: Lib.h:88
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:2853
int lud_scope
Definition: ldap.c:132
struct ldap_url_desc LDAPURLDesc
GLenum GLenum GLenum input
Definition: glext.h:4803
CURLcode Curl_Transfer(struct connectdata *c_conn, int sockindex, curl_off_t size, bool getheader, curl_off_t *bytecountp, int writesockindex, curl_off_t *writecountp)
Definition: transfer.c:1998
int lud_port
Definition: ldap.c:129
Definition: curl.h:210
struct SessionHandle * data
Definition: urldata.h:403
char ** lud_attrs
Definition: ldap.c:131
void *(* dynafunc)(void *input)
Definition: ldap.c:57
char * lud_dn
Definition: ldap.c:130
struct ldap_url_desc * lud_next
Definition: ldap.c:126
const GLcharARB * name
Definition: glext.h:3629
#define DYNA_GET_FUNCTION(type, fnc)
Definition: ldap.c:59
#define infof
Definition: sendf.h:31
typedef void(APIENTRYP PFNGLBLENDCOLORPROC)(GLclampf red
#define FALSE
Definition: mprintf.c:70
long port
Definition: urldata.h:433
bool user_passwd
Definition: urldata.h:282
char * user
Definition: urldata.h:446
int lud_crit_exts
Definition: ldap.c:135