1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2005, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
22 ***************************************************************************/
26 #ifndef CURL_DISABLE_LDAP
27 /* -- WIN32 approved -- */
33 #ifdef HAVE_SYS_TYPES_H
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
55 #include <curl/curl.h>
65 #define _MPRINTF_REPLACE /* use our functions only */
66 #include <curl/mprintf.h>
70 /* WLdap32.dll functions are *not* stdcall. Must call these via __cdecl
71 * pointers in case libcurl was compiled as fastcall (cl -Gr). Watcom
72 * uses fastcall by default.
74 #if !defined(WIN32) && !defined(__cdecl)
78 #ifndef LDAP_SIZELIMIT_EXCEEDED
79 #define LDAP_SIZELIMIT_EXCEEDED 4
82 #define LDAP_VERSION2 2
85 #define LDAP_VERSION3 3
87 #ifndef LDAP_OPT_PROTOCOL_VERSION
88 #define LDAP_OPT_PROTOCOL_VERSION 0x0011
91 #define DLOPEN_MODE RTLD_LAZY /*! assume all dlopen() implementations have
94 #if defined(RTLD_LAZY_GLOBAL) /* It turns out some systems use this: */
96 # define DLOPEN_MODE RTLD_LAZY_GLOBAL
97 #elif defined(RTLD_GLOBAL)
99 # define DLOPEN_MODE (RTLD_LAZY | RTLD_GLOBAL)
102 #define DYNA_GET_FUNCTION(type, fnc) do { \
103 (fnc) = (type)DynaGetFunction(#fnc); \
105 return CURLE_FUNCTION_NOT_FOUND; \
108 /*! CygWin etc. configure could set these, but we don't want it.
109 * Must use WLdap32.dll code.
116 typedef void * (*dynafunc)(void *input);
118 /***********************************************************************
120 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL) || defined(WIN32)
121 static void *libldap = NULL;
122 #if defined(DL_LBER_FILE)
123 static void *liblber = NULL;
128 unsigned long bv_len;
132 static int DynaOpen(const char **mod_name)
134 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
135 if (libldap == NULL) {
137 * libldap.so can normally resolve its dependency on liblber.so
138 * automatically, but in broken installation it does not so
139 * handle it here by opening liblber.so as global.
142 *mod_name = DL_LBER_FILE;
143 liblber = dlopen(*mod_name, DLOPEN_MODE);
148 /* Assume loading libldap.so will fail if loading of liblber.so failed
150 *mod_name = DL_LDAP_FILE;
151 libldap = dlopen(*mod_name, RTLD_LAZY);
153 return (libldap != NULL);
156 *mod_name = DL_LDAP_FILE;
158 libldap = (void*)LoadLibrary(*mod_name);
159 return (libldap != NULL);
167 static void DynaClose(void)
169 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
182 FreeLibrary ((HMODULE)libldap);
188 static dynafunc DynaGetFunction(const char *name)
190 dynafunc func = (dynafunc)NULL;
192 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
194 /* This typecast magic below was brought by Joe Halpin. In ISO C, you
195 * cannot typecast a data pointer to a function pointer, but that's
196 * exactly what we need to do here to avoid compiler warnings on picky
198 *(void**) (&func) = dlsym(libldap, name);
202 func = (dynafunc)GetProcAddress((HINSTANCE)libldap, name);
210 /***********************************************************************
212 typedef struct ldap_url_desc {
213 struct ldap_url_desc *lud_next;
226 static int _ldap_url_parse (const struct connectdata *conn,
228 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
230 static void (*ldap_free_urldesc)(LDAPURLDesc *) = _ldap_free_urldesc;
234 #define LDAP_TRACE(x) do { \
235 _ldap_trace ("%u: ", __LINE__); \
239 static void _ldap_trace (const char *fmt, ...);
241 #define LDAP_TRACE(x) ((void)0)
245 CURLcode Curl_ldap(struct connectdata *conn, bool *done)
247 CURLcode status = CURLE_OK;
250 int (*ldap_url_parse)(char *, LDAPURLDesc **);
251 void (*ldap_free_urldesc)(void *);
253 void *(__cdecl *ldap_init)(char *, int);
254 int (__cdecl *ldap_simple_bind_s)(void *, char *, char *);
255 int (__cdecl *ldap_unbind_s)(void *);
256 int (__cdecl *ldap_search_s)(void *, char *, int, char *, char **,
258 void *(__cdecl *ldap_first_entry)(void *, void *);
259 void *(__cdecl *ldap_next_entry)(void *, void *);
260 char *(__cdecl *ldap_err2string)(int);
261 char *(__cdecl *ldap_get_dn)(void *, void *);
262 char *(__cdecl *ldap_first_attribute)(void *, void *, void **);
263 char *(__cdecl *ldap_next_attribute)(void *, void *, void *);
264 void **(__cdecl *ldap_get_values_len)(void *, void *, const char *);
265 void (__cdecl *ldap_value_free_len)(void **);
266 void (__cdecl *ldap_memfree)(void *);
267 void (__cdecl *ber_free)(void *, int);
268 int (__cdecl *ldap_set_option)(void *, int, void *);
271 LDAPURLDesc *ludp = NULL;
272 const char *mod_name;
274 void *entryIterator; /*! type should be 'LDAPMessage *' */
276 struct SessionHandle *data=conn->data;
281 *done = TRUE; /* unconditionally */
282 infof(data, "LDAP local: %s\n", data->change.url);
284 if (!DynaOpen(&mod_name)) {
285 failf(data, "The %s LDAP library/libraries couldn't be opened", mod_name);
286 return CURLE_LIBRARY_NOT_FOUND;
289 /* The types are needed because ANSI C distinguishes between
290 * pointer-to-object (data) and pointer-to-function.
292 DYNA_GET_FUNCTION(void *(__cdecl *)(char *, int), ldap_init);
293 DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, char *),
295 DYNA_GET_FUNCTION(int (__cdecl *)(void *), ldap_unbind_s);
297 DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
298 DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
300 DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, int, char *, char **, int,
301 void **), ldap_search_s);
302 DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_first_entry);
303 DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_next_entry);
304 DYNA_GET_FUNCTION(char *(__cdecl *)(int), ldap_err2string);
305 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *), ldap_get_dn);
306 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **),
307 ldap_first_attribute);
308 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *),
309 ldap_next_attribute);
310 DYNA_GET_FUNCTION(void **(__cdecl *)(void *, void *, const char *),
311 ldap_get_values_len);
312 DYNA_GET_FUNCTION(void (__cdecl *)(void **), ldap_value_free_len);
313 DYNA_GET_FUNCTION(void (__cdecl *)(void *), ldap_memfree);
314 DYNA_GET_FUNCTION(void (__cdecl *)(void *, int), ber_free);
315 DYNA_GET_FUNCTION(int (__cdecl *)(void *, int, void *), ldap_set_option);
317 server = (*ldap_init)(conn->host.name, (int)conn->port);
318 if (server == NULL) {
319 failf(data, "LDAP local: Cannot connect to %s:%d",
320 conn->host.name, conn->port);
321 status = CURLE_COULDNT_CONNECT;
325 ldap_proto = LDAP_VERSION3;
326 (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
327 rc = (*ldap_simple_bind_s)(server,
328 conn->bits.user_passwd ? conn->user : NULL,
329 conn->bits.user_passwd ? conn->passwd : NULL);
331 ldap_proto = LDAP_VERSION2;
332 (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
333 rc = (*ldap_simple_bind_s)(server,
334 conn->bits.user_passwd ? conn->user : NULL,
335 conn->bits.user_passwd ? conn->passwd : NULL);
338 failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
339 status = CURLE_LDAP_CANNOT_BIND;
344 rc = _ldap_url_parse(conn, &ludp);
346 rc = (*ldap_url_parse)(data->change.url, &ludp);
350 failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
351 status = CURLE_LDAP_INVALID_URL;
355 rc = (*ldap_search_s)(server, ludp->lud_dn, ludp->lud_scope,
356 ludp->lud_filter, ludp->lud_attrs, 0, &result);
358 if (rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
359 failf(data, "LDAP remote: %s", (*ldap_err2string)(rc));
360 status = CURLE_LDAP_SEARCH_FAILED;
364 for(num = 0, entryIterator = (*ldap_first_entry)(server, result);
366 entryIterator = (*ldap_next_entry)(server, entryIterator), num++)
368 void *ber = NULL; /*! is really 'BerElement **' */
369 void *attribute; /*! suspicious that this isn't 'const' */
370 char *dn = (*ldap_get_dn)(server, entryIterator);
373 Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
374 Curl_client_write(data, CLIENTWRITE_BODY, (char *)dn, 0);
375 Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
377 for (attribute = (*ldap_first_attribute)(server, entryIterator, &ber);
379 attribute = (*ldap_next_attribute)(server, entryIterator, ber))
381 struct bv **vals = (struct bv **)
382 (*ldap_get_values_len)(server, entryIterator, attribute);
386 for (i = 0; (vals[i] != NULL); i++)
388 Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\t", 1);
389 Curl_client_write(data, CLIENTWRITE_BODY, (char *) attribute, 0);
390 Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
391 if ((strlen(attribute) > 7) &&
394 (strlen((char *)attribute) - 7)) == 0)) {
395 /* Binary attribute, encode to base64. */
396 val_b64_sz = Curl_base64_encode(vals[i]->bv_val, vals[i]->bv_len,
398 if (val_b64_sz > 0) {
399 Curl_client_write(data, CLIENTWRITE_BODY, val_b64, val_b64_sz);
403 Curl_client_write(data, CLIENTWRITE_BODY, vals[i]->bv_val,
405 Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
408 /* Free memory used to store values */
409 (*ldap_value_free_len)((void **)vals);
411 Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
413 (*ldap_memfree)(attribute);
421 LDAP_TRACE (("Received %d entries\n", num));
422 if (rc == LDAP_SIZELIMIT_EXCEEDED)
423 infof(data, "There are more than %d entries\n", num);
425 (*ldap_free_urldesc)(ludp);
427 (*ldap_unbind_s)(server);
431 /* no data to transfer */
432 Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
433 conn->bits.close = TRUE;
439 static void _ldap_trace (const char *fmt, ...)
441 static int do_trace = -1;
444 if (do_trace == -1) {
445 const char *env = getenv("CURL_TRACE");
446 do_trace = (env && atoi(env) > 0);
451 va_start (args, fmt);
452 vfprintf (stderr, fmt, args);
459 * Return scope-value for a scope-string.
461 static int str2scope (const char *p)
463 if (!stricmp(p, "one"))
464 return LDAP_SCOPE_ONELEVEL;
465 if (!stricmp(p, "onetree"))
466 return LDAP_SCOPE_ONELEVEL;
467 if (!stricmp(p, "base"))
468 return LDAP_SCOPE_BASE;
469 if (!stricmp(p, "sub"))
470 return LDAP_SCOPE_SUBTREE;
471 if (!stricmp( p, "subtree"))
472 return LDAP_SCOPE_SUBTREE;
477 * Split 'str' into strings separated by commas.
478 * Note: res[] points into 'str'.
480 static char **split_str (char *str)
482 char **res, *lasts, *s;
485 for (i = 2, s = strchr(str,','); s; i++)
488 res = calloc(i, sizeof(char*));
492 for (i = 0, s = strtok_r(str, ",", &lasts); s;
493 s = strtok_r(NULL, ",", &lasts), i++)
499 * Unescape the LDAP-URL components
501 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
505 if (ludp->lud_filter) {
506 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0);
507 if (!ludp->lud_filter)
511 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
512 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0);
513 if (!ludp->lud_attrs[i])
517 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
518 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0);
519 if (!ludp->lud_exts[i])
524 char *dn = ludp->lud_dn;
525 char *new_dn = curl_easy_unescape(data, dn, 0);
528 ludp->lud_dn = new_dn;
536 * Break apart the pieces of an LDAP URL.
538 * ldap://<hostname>:<port>/<base_dn>?<attributes>?<scope>?<filter>?<ext>
540 * <hostname> already known from 'conn->host.name'.
541 * <port> already known from 'conn->remote_port'.
542 * extract the rest from 'conn->path+1'. All fields are optional. e.g.
543 * ldap://<hostname>:<port>/?<attributes>?<scope>?<filter> yields ludp->lud_dn = "".
545 * Ref. http://developer.netscape.com/docs/manuals/dirsdk/csdk30/url.htm#2831915
547 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
552 if (!conn->path || conn->path[0] != '/' ||
553 !checkprefix(conn->protostr, conn->data->change.url))
554 return LDAP_INVALID_SYNTAX;
556 ludp->lud_scope = LDAP_SCOPE_BASE;
557 ludp->lud_port = conn->remote_port;
558 ludp->lud_host = conn->host.name;
560 /* parse DN (Distinguished Name).
562 ludp->lud_dn = strdup(conn->path+1);
564 return LDAP_NO_MEMORY;
566 p = strchr(ludp->lud_dn, '?');
567 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
568 strlen(ludp->lud_dn), ludp->lud_dn));
575 /* parse attributes. skip "??".
581 if (*p && *p != '?') {
582 ludp->lud_attrs = split_str(p);
583 if (!ludp->lud_attrs)
584 return LDAP_NO_MEMORY;
586 for (i = 0; ludp->lud_attrs[i]; i++)
587 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
594 /* parse scope. skip "??"
600 if (*p && *p != '?') {
601 ludp->lud_scope = str2scope(p);
602 if (ludp->lud_scope == -1)
603 return LDAP_INVALID_SYNTAX;
604 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
617 return LDAP_INVALID_SYNTAX;
619 ludp->lud_filter = p;
620 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
628 ludp->lud_exts = split_str(p);
630 return LDAP_NO_MEMORY;
632 for (i = 0; ludp->lud_exts[i]; i++)
633 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
636 if (!unescape_elements(conn->data, ludp))
637 return LDAP_NO_MEMORY;
641 static int _ldap_url_parse (const struct connectdata *conn,
644 LDAPURLDesc *ludp = calloc(sizeof(*ludp), 1);
649 return LDAP_NO_MEMORY;
651 rc = _ldap_url_parse2 (conn, ludp);
652 if (rc != LDAP_SUCCESS) {
653 _ldap_free_urldesc(ludp);
660 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
670 if (ludp->lud_filter)
671 free(ludp->lud_filter);
673 if (ludp->lud_attrs) {
674 for (i = 0; ludp->lud_attrs[i]; i++)
675 free(ludp->lud_attrs[i]);
676 free(ludp->lud_attrs);
679 if (ludp->lud_exts) {
680 for (i = 0; ludp->lud_exts[i]; i++)
681 free(ludp->lud_exts[i]);
682 free(ludp->lud_exts);
687 #endif /* CURL_DISABLE_LDAP */