00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025
00026 #ifndef CURL_DISABLE_LDAP
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <stdarg.h>
00031 #include <stdlib.h>
00032 #include <ctype.h>
00033 #ifdef NEED_MALLOC_H
00034 #include <malloc.h>
00035 #endif
00036 #include <errno.h>
00037
00038 #if defined(WIN32)
00039 # include <winldap.h>
00040 #endif
00041
00042 #ifdef HAVE_UNISTD_H
00043 # include <unistd.h>
00044 #endif
00045
00046 #ifdef HAVE_DLFCN_H
00047 # include <dlfcn.h>
00048 #endif
00049
00050 #include "urldata.h"
00051 #include <curl/curl.h>
00052 #include "sendf.h"
00053 #include "escape.h"
00054 #include "transfer.h"
00055 #include "strequal.h"
00056 #include "strtok.h"
00057 #include "ldap.h"
00058 #include "memory.h"
00059 #include "base64.h"
00060
00061 #define _MPRINTF_REPLACE
00062 #include <curl/mprintf.h>
00063
00064 #include "memdebug.h"
00065
00066
00067
00068
00069
00070 #if !defined(WIN32) && !defined(__cdecl)
00071 #define __cdecl
00072 #endif
00073
00074 #ifndef LDAP_SIZELIMIT_EXCEEDED
00075 #define LDAP_SIZELIMIT_EXCEEDED 4
00076 #endif
00077 #ifndef LDAP_VERSION2
00078 #define LDAP_VERSION2 2
00079 #endif
00080 #ifndef LDAP_VERSION3
00081 #define LDAP_VERSION3 3
00082 #endif
00083 #ifndef LDAP_OPT_PROTOCOL_VERSION
00084 #define LDAP_OPT_PROTOCOL_VERSION 0x0011
00085 #endif
00086
00087 #define DLOPEN_MODE RTLD_LAZY
00090 #if defined(RTLD_LAZY_GLOBAL)
00091 # undef DLOPEN_MODE
00092 # define DLOPEN_MODE RTLD_LAZY_GLOBAL
00093 #elif defined(RTLD_GLOBAL)
00094 # undef DLOPEN_MODE
00095 # define DLOPEN_MODE (RTLD_LAZY | RTLD_GLOBAL)
00096 #endif
00097
00098 #define DYNA_GET_FUNCTION(type, fnc) do { \
00099 (fnc) = (type)DynaGetFunction(#fnc); \
00100 if ((fnc) == NULL) \
00101 return CURLE_FUNCTION_NOT_FOUND; \
00102 } while (0)
00103
00107 #if defined(WIN32)
00108 #undef HAVE_DLOPEN
00109 #undef HAVE_LIBDL
00110 #endif
00111
00112
00113
00114
00115
00116
00117 #define ZERO_NULL 0
00118
00119 typedef void * (*dynafunc)(void *input);
00120
00121
00122
00123 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL) || defined(WIN32)
00124 static void *libldap = NULL;
00125 #if defined(DL_LBER_FILE)
00126 static void *liblber = NULL;
00127 #endif
00128 #endif
00129
00130 struct bv {
00131 unsigned long bv_len;
00132 char *bv_val;
00133 };
00134
00135 static int DynaOpen(const char **mod_name)
00136 {
00137 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
00138 if (libldap == NULL) {
00139
00140
00141
00142
00143
00144 #ifdef DL_LBER_FILE
00145 *mod_name = DL_LBER_FILE;
00146 liblber = dlopen(*mod_name, DLOPEN_MODE);
00147 if (!liblber)
00148 return 0;
00149 #endif
00150
00151
00152
00153 *mod_name = DL_LDAP_FILE;
00154 libldap = dlopen(*mod_name, RTLD_LAZY);
00155 }
00156 return (libldap != NULL);
00157
00158 #elif defined(WIN32)
00159 *mod_name = DL_LDAP_FILE;
00160 if (!libldap)
00161 libldap = (void*)LoadLibrary(*mod_name);
00162 return (libldap != NULL);
00163
00164 #else
00165 *mod_name = "";
00166 return (0);
00167 #endif
00168 }
00169
00170 static void DynaClose(void)
00171 {
00172 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
00173 if (libldap) {
00174 dlclose(libldap);
00175 libldap=NULL;
00176 }
00177 #ifdef DL_LBER_FILE
00178 if (liblber) {
00179 dlclose(liblber);
00180 liblber=NULL;
00181 }
00182 #endif
00183 #elif defined(WIN32)
00184 if (libldap) {
00185 FreeLibrary ((HMODULE)libldap);
00186 libldap = NULL;
00187 }
00188 #endif
00189 }
00190
00191 static dynafunc DynaGetFunction(const char *name)
00192 {
00193 dynafunc func = (dynafunc)ZERO_NULL;
00194
00195 #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
00196 if (libldap) {
00197
00198
00199
00200
00201 *(void**) (&func) = dlsym(libldap, name);
00202 }
00203 #ifdef DL_LBER_FILE
00204 if (!func && liblber) {
00205 *(void**) (&func) = dlsym(liblber, name);
00206 }
00207 #endif
00208 #elif defined(WIN32)
00209 if (libldap) {
00210 func = (dynafunc)GetProcAddress((HINSTANCE)libldap, name);
00211 }
00212 #else
00213 (void) name;
00214 #endif
00215 return func;
00216 }
00217
00218
00219
00220 typedef struct ldap_url_desc {
00221 struct ldap_url_desc *lud_next;
00222 char *lud_scheme;
00223 char *lud_host;
00224 int lud_port;
00225 char *lud_dn;
00226 char **lud_attrs;
00227 int lud_scope;
00228 char *lud_filter;
00229 char **lud_exts;
00230 int lud_crit_exts;
00231 } LDAPURLDesc;
00232
00233 #ifdef WIN32
00234 static int _ldap_url_parse (const struct connectdata *conn,
00235 LDAPURLDesc **ludp);
00236 static void _ldap_free_urldesc (LDAPURLDesc *ludp);
00237
00238 static void (*ldap_free_urldesc)(LDAPURLDesc *) = _ldap_free_urldesc;
00239 #endif
00240
00241 #ifdef DEBUG_LDAP
00242 #define LDAP_TRACE(x) do { \
00243 _ldap_trace ("%u: ", __LINE__); \
00244 _ldap_trace x; \
00245 } while (0)
00246
00247 static void _ldap_trace (const char *fmt, ...);
00248 #else
00249 #define LDAP_TRACE(x) ((void)0)
00250 #endif
00251
00252
00253 CURLcode Curl_ldap(struct connectdata *conn, bool *done)
00254 {
00255 CURLcode status = CURLE_OK;
00256 int rc = 0;
00257 #ifndef WIN32
00258 int (*ldap_url_parse)(char *, LDAPURLDesc **);
00259 void (*ldap_free_urldesc)(void *);
00260 #endif
00261 void *(__cdecl *ldap_init)(char *, int);
00262 int (__cdecl *ldap_simple_bind_s)(void *, char *, char *);
00263 int (__cdecl *ldap_unbind_s)(void *);
00264 int (__cdecl *ldap_search_s)(void *, char *, int, char *, char **,
00265 int, void **);
00266 void *(__cdecl *ldap_first_entry)(void *, void *);
00267 void *(__cdecl *ldap_next_entry)(void *, void *);
00268 char *(__cdecl *ldap_err2string)(int);
00269 char *(__cdecl *ldap_get_dn)(void *, void *);
00270 char *(__cdecl *ldap_first_attribute)(void *, void *, void **);
00271 char *(__cdecl *ldap_next_attribute)(void *, void *, void *);
00272 void **(__cdecl *ldap_get_values_len)(void *, void *, const char *);
00273 void (__cdecl *ldap_value_free_len)(void **);
00274 void (__cdecl *ldap_memfree)(void *);
00275 void (__cdecl *ber_free)(void *, int);
00276 int (__cdecl *ldap_set_option)(void *, int, void *);
00277
00278 void *server;
00279 LDAPURLDesc *ludp = NULL;
00280 const char *mod_name;
00281 void *result;
00282 void *entryIterator;
00283 int num = 0;
00284 struct SessionHandle *data=conn->data;
00285 int ldap_proto;
00286 char *val_b64;
00287 size_t val_b64_sz;
00288
00289 *done = TRUE;
00290 infof(data, "LDAP local: %s\n", data->change.url);
00291
00292 if (!DynaOpen(&mod_name)) {
00293 failf(data, "The %s LDAP library/libraries couldn't be opened", mod_name);
00294 return CURLE_LIBRARY_NOT_FOUND;
00295 }
00296
00297
00298
00299
00300 DYNA_GET_FUNCTION(void *(__cdecl *)(char *, int), ldap_init);
00301 DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, char *),
00302 ldap_simple_bind_s);
00303 DYNA_GET_FUNCTION(int (__cdecl *)(void *), ldap_unbind_s);
00304 #ifndef WIN32
00305 DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
00306 DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
00307 #endif
00308 DYNA_GET_FUNCTION(int (__cdecl *)(void *, char *, int, char *, char **, int,
00309 void **), ldap_search_s);
00310 DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_first_entry);
00311 DYNA_GET_FUNCTION(void *(__cdecl *)(void *, void *), ldap_next_entry);
00312 DYNA_GET_FUNCTION(char *(__cdecl *)(int), ldap_err2string);
00313 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *), ldap_get_dn);
00314 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void **),
00315 ldap_first_attribute);
00316 DYNA_GET_FUNCTION(char *(__cdecl *)(void *, void *, void *),
00317 ldap_next_attribute);
00318 DYNA_GET_FUNCTION(void **(__cdecl *)(void *, void *, const char *),
00319 ldap_get_values_len);
00320 DYNA_GET_FUNCTION(void (__cdecl *)(void **), ldap_value_free_len);
00321 DYNA_GET_FUNCTION(void (__cdecl *)(void *), ldap_memfree);
00322 DYNA_GET_FUNCTION(void (__cdecl *)(void *, int), ber_free);
00323 DYNA_GET_FUNCTION(int (__cdecl *)(void *, int, void *), ldap_set_option);
00324
00325 server = (*ldap_init)(conn->host.name, (int)conn->port);
00326 if (server == NULL) {
00327 failf(data, "LDAP local: Cannot connect to %s:%d",
00328 conn->host.name, conn->port);
00329 status = CURLE_COULDNT_CONNECT;
00330 goto quit;
00331 }
00332
00333 ldap_proto = LDAP_VERSION3;
00334 (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
00335 rc = (*ldap_simple_bind_s)(server,
00336 conn->bits.user_passwd ? conn->user : NULL,
00337 conn->bits.user_passwd ? conn->passwd : NULL);
00338 if (rc != 0) {
00339 ldap_proto = LDAP_VERSION2;
00340 (*ldap_set_option)(server, LDAP_OPT_PROTOCOL_VERSION, &ldap_proto);
00341 rc = (*ldap_simple_bind_s)(server,
00342 conn->bits.user_passwd ? conn->user : NULL,
00343 conn->bits.user_passwd ? conn->passwd : NULL);
00344 }
00345 if (rc != 0) {
00346 failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
00347 status = CURLE_LDAP_CANNOT_BIND;
00348 goto quit;
00349 }
00350
00351 #ifdef WIN32
00352 rc = _ldap_url_parse(conn, &ludp);
00353 #else
00354 rc = (*ldap_url_parse)(data->change.url, &ludp);
00355 #endif
00356
00357 if (rc != 0) {
00358 failf(data, "LDAP local: %s", (*ldap_err2string)(rc));
00359 status = CURLE_LDAP_INVALID_URL;
00360 goto quit;
00361 }
00362
00363 rc = (*ldap_search_s)(server, ludp->lud_dn, ludp->lud_scope,
00364 ludp->lud_filter, ludp->lud_attrs, 0, &result);
00365
00366 if (rc != 0 && rc != LDAP_SIZELIMIT_EXCEEDED) {
00367 failf(data, "LDAP remote: %s", (*ldap_err2string)(rc));
00368 status = CURLE_LDAP_SEARCH_FAILED;
00369 goto quit;
00370 }
00371
00372 for(num = 0, entryIterator = (*ldap_first_entry)(server, result);
00373 entryIterator;
00374 entryIterator = (*ldap_next_entry)(server, entryIterator), num++)
00375 {
00376 void *ber = NULL;
00377 void *attribute;
00378 char *dn = (*ldap_get_dn)(server, entryIterator);
00379 int i;
00380
00381 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
00382 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)dn, 0);
00383 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
00384
00385 for (attribute = (*ldap_first_attribute)(server, entryIterator, &ber);
00386 attribute;
00387 attribute = (*ldap_next_attribute)(server, entryIterator, ber))
00388 {
00389 struct bv **vals = (struct bv **)
00390 (*ldap_get_values_len)(server, entryIterator, attribute);
00391
00392 if (vals != NULL)
00393 {
00394 for (i = 0; (vals[i] != NULL); i++)
00395 {
00396 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
00397 Curl_client_write(conn, CLIENTWRITE_BODY, (char *) attribute, 0);
00398 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)": ", 2);
00399 if ((strlen(attribute) > 7) &&
00400 (strcmp(";binary",
00401 (char *)attribute +
00402 (strlen((char *)attribute) - 7)) == 0)) {
00403
00404 val_b64_sz = Curl_base64_encode(conn->data,
00405 vals[i]->bv_val,
00406 vals[i]->bv_len,
00407 &val_b64);
00408 if (val_b64_sz > 0) {
00409 Curl_client_write(conn, CLIENTWRITE_BODY, val_b64, val_b64_sz);
00410 free(val_b64);
00411 }
00412 } else
00413 Curl_client_write(conn, CLIENTWRITE_BODY, vals[i]->bv_val,
00414 vals[i]->bv_len);
00415 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
00416 }
00417
00418
00419 (*ldap_value_free_len)((void **)vals);
00420 }
00421 Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
00422
00423 (*ldap_memfree)(attribute);
00424 }
00425 (*ldap_memfree)(dn);
00426 if (ber)
00427 (*ber_free)(ber, 0);
00428 }
00429
00430 quit:
00431 LDAP_TRACE (("Received %d entries\n", num));
00432 if (rc == LDAP_SIZELIMIT_EXCEEDED)
00433 infof(data, "There are more than %d entries\n", num);
00434 if (ludp)
00435 (*ldap_free_urldesc)(ludp);
00436 if (server)
00437 (*ldap_unbind_s)(server);
00438
00439 DynaClose();
00440
00441
00442 Curl_setup_transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
00443 conn->bits.close = TRUE;
00444
00445 return status;
00446 }
00447
00448 #ifdef DEBUG_LDAP
00449 static void _ldap_trace (const char *fmt, ...)
00450 {
00451 static int do_trace = -1;
00452 va_list args;
00453
00454 if (do_trace == -1) {
00455 const char *env = getenv("CURL_TRACE");
00456 do_trace = (env && atoi(env) > 0);
00457 }
00458 if (!do_trace)
00459 return;
00460
00461 va_start (args, fmt);
00462 vfprintf (stderr, fmt, args);
00463 va_end (args);
00464 }
00465 #endif
00466
00467 #ifdef WIN32
00468
00469
00470
00471 static int str2scope (const char *p)
00472 {
00473 if (!stricmp(p, "one"))
00474 return LDAP_SCOPE_ONELEVEL;
00475 if (!stricmp(p, "onetree"))
00476 return LDAP_SCOPE_ONELEVEL;
00477 if (!stricmp(p, "base"))
00478 return LDAP_SCOPE_BASE;
00479 if (!stricmp(p, "sub"))
00480 return LDAP_SCOPE_SUBTREE;
00481 if (!stricmp( p, "subtree"))
00482 return LDAP_SCOPE_SUBTREE;
00483 return (-1);
00484 }
00485
00486
00487
00488
00489
00490 static char **split_str (char *str)
00491 {
00492 char **res, *lasts, *s;
00493 int i;
00494
00495 for (i = 2, s = strchr(str,','); s; i++)
00496 s = strchr(++s,',');
00497
00498 res = calloc(i, sizeof(char*));
00499 if (!res)
00500 return NULL;
00501
00502 for (i = 0, s = strtok_r(str, ",", &lasts); s;
00503 s = strtok_r(NULL, ",", &lasts), i++)
00504 res[i] = s;
00505 return res;
00506 }
00507
00508
00509
00510
00511 static bool unescape_elements (void *data, LDAPURLDesc *ludp)
00512 {
00513 int i;
00514
00515 if (ludp->lud_filter) {
00516 ludp->lud_filter = curl_easy_unescape(data, ludp->lud_filter, 0, NULL);
00517 if (!ludp->lud_filter)
00518 return (FALSE);
00519 }
00520
00521 for (i = 0; ludp->lud_attrs && ludp->lud_attrs[i]; i++) {
00522 ludp->lud_attrs[i] = curl_easy_unescape(data, ludp->lud_attrs[i], 0, NULL);
00523 if (!ludp->lud_attrs[i])
00524 return (FALSE);
00525 }
00526
00527 for (i = 0; ludp->lud_exts && ludp->lud_exts[i]; i++) {
00528 ludp->lud_exts[i] = curl_easy_unescape(data, ludp->lud_exts[i], 0, NULL);
00529 if (!ludp->lud_exts[i])
00530 return (FALSE);
00531 }
00532
00533 if (ludp->lud_dn) {
00534 char *dn = ludp->lud_dn;
00535 char *new_dn = curl_easy_unescape(data, dn, 0, NULL);
00536
00537 free(dn);
00538 ludp->lud_dn = new_dn;
00539 if (!new_dn)
00540 return (FALSE);
00541 }
00542 return (TRUE);
00543 }
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559 static int _ldap_url_parse2 (const struct connectdata *conn, LDAPURLDesc *ludp)
00560 {
00561 char *p, *q;
00562 int i;
00563
00564 if (!conn->data ||
00565 !conn->data->reqdata.path ||
00566 conn->data->reqdata.path[0] != '/' ||
00567 !checkprefix(conn->protostr, conn->data->change.url))
00568 return LDAP_INVALID_SYNTAX;
00569
00570 ludp->lud_scope = LDAP_SCOPE_BASE;
00571 ludp->lud_port = conn->remote_port;
00572 ludp->lud_host = conn->host.name;
00573
00574
00575
00576 ludp->lud_dn = strdup(conn->data->reqdata.path+1);
00577 if (!ludp->lud_dn)
00578 return LDAP_NO_MEMORY;
00579
00580 p = strchr(ludp->lud_dn, '?');
00581 LDAP_TRACE (("DN '%.*s'\n", p ? (size_t)(p-ludp->lud_dn) :
00582 strlen(ludp->lud_dn), ludp->lud_dn));
00583
00584 if (!p)
00585 goto success;
00586
00587 *p++ = '\0';
00588
00589
00590
00591 q = strchr(p, '?');
00592 if (q)
00593 *q++ = '\0';
00594
00595 if (*p && *p != '?') {
00596 ludp->lud_attrs = split_str(p);
00597 if (!ludp->lud_attrs)
00598 return LDAP_NO_MEMORY;
00599
00600 for (i = 0; ludp->lud_attrs[i]; i++)
00601 LDAP_TRACE (("attr[%d] '%s'\n", i, ludp->lud_attrs[i]));
00602 }
00603
00604 p = q;
00605 if (!p)
00606 goto success;
00607
00608
00609
00610 q = strchr(p, '?');
00611 if (q)
00612 *q++ = '\0';
00613
00614 if (*p && *p != '?') {
00615 ludp->lud_scope = str2scope(p);
00616 if (ludp->lud_scope == -1)
00617 return LDAP_INVALID_SYNTAX;
00618 LDAP_TRACE (("scope %d\n", ludp->lud_scope));
00619 }
00620
00621 p = q;
00622 if (!p)
00623 goto success;
00624
00625
00626
00627 q = strchr(p, '?');
00628 if (q)
00629 *q++ = '\0';
00630 if (!*p)
00631 return LDAP_INVALID_SYNTAX;
00632
00633 ludp->lud_filter = p;
00634 LDAP_TRACE (("filter '%s'\n", ludp->lud_filter));
00635
00636 p = q;
00637 if (!p)
00638 goto success;
00639
00640
00641
00642 ludp->lud_exts = split_str(p);
00643 if (!ludp->lud_exts)
00644 return LDAP_NO_MEMORY;
00645
00646 for (i = 0; ludp->lud_exts[i]; i++)
00647 LDAP_TRACE (("exts[%d] '%s'\n", i, ludp->lud_exts[i]));
00648
00649 success:
00650 if (!unescape_elements(conn->data, ludp))
00651 return LDAP_NO_MEMORY;
00652 return LDAP_SUCCESS;
00653 }
00654
00655 static int _ldap_url_parse (const struct connectdata *conn,
00656 LDAPURLDesc **ludpp)
00657 {
00658 LDAPURLDesc *ludp = calloc(sizeof(*ludp), 1);
00659 int rc;
00660
00661 *ludpp = NULL;
00662 if (!ludp)
00663 return LDAP_NO_MEMORY;
00664
00665 rc = _ldap_url_parse2 (conn, ludp);
00666 if (rc != LDAP_SUCCESS) {
00667 _ldap_free_urldesc(ludp);
00668 ludp = NULL;
00669 }
00670 *ludpp = ludp;
00671 return (rc);
00672 }
00673
00674 static void _ldap_free_urldesc (LDAPURLDesc *ludp)
00675 {
00676 int i;
00677
00678 if (!ludp)
00679 return;
00680
00681 if (ludp->lud_dn)
00682 free(ludp->lud_dn);
00683
00684 if (ludp->lud_filter)
00685 free(ludp->lud_filter);
00686
00687 if (ludp->lud_attrs) {
00688 for (i = 0; ludp->lud_attrs[i]; i++)
00689 free(ludp->lud_attrs[i]);
00690 free(ludp->lud_attrs);
00691 }
00692
00693 if (ludp->lud_exts) {
00694 for (i = 0; ludp->lud_exts[i]; i++)
00695 free(ludp->lud_exts[i]);
00696 free(ludp->lud_exts);
00697 }
00698 free (ludp);
00699 }
00700 #endif
00701 #endif