ldap.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. #include "setup.h"
  24. #ifndef CURL_DISABLE_LDAP
  25. /* -- WIN32 approved -- */
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <errno.h>
  34. #if defined(WIN32) && !defined(__GNUC__)
  35. #else
  36. # ifdef HAVE_UNISTD_H
  37. # include <unistd.h>
  38. # endif
  39. # ifdef HAVE_DLFCN_H
  40. # include <dlfcn.h>
  41. # endif
  42. #endif
  43. #include "urldata.h"
  44. #include <curl/curl.h>
  45. #include "sendf.h"
  46. #include "escape.h"
  47. #include "transfer.h"
  48. #include "ldap.h"
  49. #define _MPRINTF_REPLACE /* use our functions only */
  50. #include <curl/mprintf.h>
  51. typedef void * (*dynafunc)(void *input);
  52. #define DYNA_GET_FUNCTION(type, fnc) \
  53. (fnc) = (type)DynaGetFunction(#fnc); \
  54. if ((fnc) == NULL) { \
  55. return CURLE_FUNCTION_NOT_FOUND; \
  56. }
  57. /***********************************************************************
  58. */
  59. static void *libldap = NULL;
  60. static void *liblber = NULL;
  61. static void DynaOpen(void)
  62. {
  63. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  64. if (libldap == NULL) {
  65. /*
  66. * libldap.so should be able to resolve its dependency on
  67. * liblber.so automatically, but since it does not we will
  68. * handle it here by opening liblber.so as global.
  69. */
  70. liblber = dlopen("liblber.so",
  71. #ifdef RTLD_LAZY_GLOBAL /* It turns out some systems use this: */
  72. RTLD_LAZY_GLOBAL
  73. #else
  74. #ifdef RTLD_GLOBAL
  75. RTLD_LAZY | RTLD_GLOBAL
  76. #else
  77. /* and some systems don't have the RTLD_GLOBAL symbol */
  78. RTLD_LAZY
  79. #endif
  80. #endif
  81. );
  82. libldap = dlopen("libldap.so", RTLD_LAZY);
  83. }
  84. #endif
  85. }
  86. static void DynaClose(void)
  87. {
  88. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  89. if (libldap) {
  90. dlclose(libldap);
  91. libldap=NULL;
  92. }
  93. if (liblber) {
  94. dlclose(liblber);
  95. liblber=NULL;
  96. }
  97. #endif
  98. }
  99. static dynafunc DynaGetFunction(const char *name)
  100. {
  101. dynafunc func = (dynafunc)NULL;
  102. #if defined(HAVE_DLOPEN) || defined(HAVE_LIBDL)
  103. if (libldap) {
  104. func = (dynafunc) dlsym(libldap, name);
  105. }
  106. #endif
  107. return func;
  108. }
  109. /***********************************************************************
  110. */
  111. typedef struct ldap_url_desc {
  112. struct ldap_url_desc *lud_next;
  113. char *lud_scheme;
  114. char *lud_host;
  115. int lud_port;
  116. char *lud_dn;
  117. char **lud_attrs;
  118. int lud_scope;
  119. char *lud_filter;
  120. char **lud_exts;
  121. int lud_crit_exts;
  122. } LDAPURLDesc;
  123. CURLcode Curl_ldap(struct connectdata *conn)
  124. {
  125. CURLcode status = CURLE_OK;
  126. int rc;
  127. void *(*ldap_init)(char *, int);
  128. int (*ldap_simple_bind_s)(void *, char *, char *);
  129. int (*ldap_unbind_s)(void *);
  130. int (*ldap_url_parse)(char *, LDAPURLDesc **);
  131. void (*ldap_free_urldesc)(void *);
  132. int (*ldap_search_s)(void *, char *, int, char *, char **, int, void **);
  133. int (*ldap_search_st)(void *, char *, int, char *, char **, int, void *, void **);
  134. void *(*ldap_first_entry)(void *, void *);
  135. void *(*ldap_next_entry)(void *, void *);
  136. char *(*ldap_err2string)(int);
  137. char *(*ldap_get_dn)(void *, void *);
  138. char *(*ldap_first_attribute)(void *, void *, void **);
  139. char *(*ldap_next_attribute)(void *, void *, void *);
  140. char **(*ldap_get_values)(void *, void *, char *);
  141. void (*ldap_value_free)(char **);
  142. void (*ldap_memfree)(void *);
  143. void (*ber_free)(void *, int);
  144. void *server;
  145. LDAPURLDesc *ludp;
  146. void *result;
  147. void *entryIterator;
  148. void *ber;
  149. void *attribute;
  150. struct SessionHandle *data=conn->data;
  151. infof(data, "LDAP: %s\n", data->change.url);
  152. DynaOpen();
  153. if (libldap == NULL) {
  154. failf(data, "The needed LDAP library/libraries couldn't be opened");
  155. return CURLE_LIBRARY_NOT_FOUND;
  156. }
  157. /* The types are needed because ANSI C distinguishes between
  158. * pointer-to-object (data) and pointer-to-function.
  159. */
  160. DYNA_GET_FUNCTION(void *(*)(char *, int), ldap_init);
  161. DYNA_GET_FUNCTION(int (*)(void *, char *, char *), ldap_simple_bind_s);
  162. DYNA_GET_FUNCTION(int (*)(void *), ldap_unbind_s);
  163. DYNA_GET_FUNCTION(int (*)(char *, LDAPURLDesc **), ldap_url_parse);
  164. DYNA_GET_FUNCTION(void (*)(void *), ldap_free_urldesc);
  165. DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void **), ldap_search_s);
  166. DYNA_GET_FUNCTION(int (*)(void *, char *, int, char *, char **, int, void *, void **), ldap_search_st);
  167. DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_first_entry);
  168. DYNA_GET_FUNCTION(void *(*)(void *, void *), ldap_next_entry);
  169. DYNA_GET_FUNCTION(char *(*)(int), ldap_err2string);
  170. DYNA_GET_FUNCTION(char *(*)(void *, void *), ldap_get_dn);
  171. DYNA_GET_FUNCTION(char *(*)(void *, void *, void **), ldap_first_attribute);
  172. DYNA_GET_FUNCTION(char *(*)(void *, void *, void *), ldap_next_attribute);
  173. DYNA_GET_FUNCTION(char **(*)(void *, void *, char *), ldap_get_values);
  174. DYNA_GET_FUNCTION(void (*)(char **), ldap_value_free);
  175. DYNA_GET_FUNCTION(void (*)(void *), ldap_memfree);
  176. DYNA_GET_FUNCTION(void (*)(void *, int), ber_free);
  177. server = ldap_init(conn->hostname, conn->port);
  178. if (server == NULL) {
  179. failf(data, "LDAP: Cannot connect to %s:%d",
  180. conn->hostname, conn->port);
  181. status = CURLE_COULDNT_CONNECT;
  182. }
  183. else {
  184. rc = ldap_simple_bind_s(server,
  185. conn->bits.user_passwd?conn->user:NULL,
  186. conn->bits.user_passwd?conn->passwd:NULL);
  187. if (rc != 0) {
  188. failf(data, "LDAP: %s", ldap_err2string(rc));
  189. status = CURLE_LDAP_CANNOT_BIND;
  190. }
  191. else {
  192. rc = ldap_url_parse(data->change.url, &ludp);
  193. if (rc != 0) {
  194. failf(data, "LDAP: %s", ldap_err2string(rc));
  195. status = CURLE_LDAP_INVALID_URL;
  196. }
  197. else {
  198. rc = ldap_search_s(server, ludp->lud_dn, ludp->lud_scope,
  199. ludp->lud_filter, ludp->lud_attrs, 0, &result);
  200. if (rc != 0) {
  201. failf(data, "LDAP: %s", ldap_err2string(rc));
  202. status = CURLE_LDAP_SEARCH_FAILED;
  203. }
  204. else {
  205. for (entryIterator = ldap_first_entry(server, result);
  206. entryIterator;
  207. entryIterator = ldap_next_entry(server, entryIterator)) {
  208. char *dn = ldap_get_dn(server, entryIterator);
  209. char **vals;
  210. int i;
  211. Curl_client_write(data, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  212. Curl_client_write(data, CLIENTWRITE_BODY, dn, 0);
  213. Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  214. for(attribute = ldap_first_attribute(server, entryIterator,
  215. &ber);
  216. attribute;
  217. attribute = ldap_next_attribute(server, entryIterator,
  218. ber) ) {
  219. vals = ldap_get_values(server, entryIterator, attribute);
  220. if (vals != NULL) {
  221. for(i = 0; (vals[i] != NULL); i++) {
  222. Curl_client_write(data, CLIENTWRITE_BODY, (char*)"\t", 1);
  223. Curl_client_write(data, CLIENTWRITE_BODY, attribute, 0);
  224. Curl_client_write(data, CLIENTWRITE_BODY, (char *)": ", 2);
  225. Curl_client_write(data, CLIENTWRITE_BODY, vals[i], 0);
  226. Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 0);
  227. }
  228. }
  229. /* Free memory used to store values */
  230. ldap_value_free(vals);
  231. }
  232. Curl_client_write(data, CLIENTWRITE_BODY, (char *)"\n", 1);
  233. ldap_memfree(attribute);
  234. ldap_memfree(dn);
  235. if (ber) ber_free(ber, 0);
  236. }
  237. }
  238. ldap_free_urldesc(ludp);
  239. }
  240. ldap_unbind_s(server);
  241. }
  242. }
  243. DynaClose();
  244. /* no data to transfer */
  245. Curl_Transfer(conn, -1, -1, FALSE, NULL, -1, NULL);
  246. return status;
  247. }
  248. #endif