openldap.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2011 - 2021, Daniel Stenberg, <[email protected]>, et al.
  9. * Copyright (C) 2010, Howard Chu, <[email protected]>
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #if !defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)
  25. /*
  26. * Notice that USE_OPENLDAP is only a source code selection switch. When
  27. * libcurl is built with USE_OPENLDAP defined the libcurl source code that
  28. * gets compiled is the code from openldap.c, otherwise the code that gets
  29. * compiled is the code from ldap.c.
  30. *
  31. * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
  32. * might be required for compilation and runtime. In order to use ancient
  33. * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
  34. */
  35. #include <ldap.h>
  36. #include "urldata.h"
  37. #include <curl/curl.h>
  38. #include "sendf.h"
  39. #include "vtls/vtls.h"
  40. #include "transfer.h"
  41. #include "curl_ldap.h"
  42. #include "curl_base64.h"
  43. #include "connect.h"
  44. /* The last 3 #include files should be in this order */
  45. #include "curl_printf.h"
  46. #include "curl_memory.h"
  47. #include "memdebug.h"
  48. /*
  49. * Uncommenting this will enable the built-in debug logging of the openldap
  50. * library. The debug log level can be set using the CURL_OPENLDAP_TRACE
  51. * environment variable. The debug output is written to stderr.
  52. *
  53. * The library supports the following debug flags:
  54. * LDAP_DEBUG_NONE 0x0000
  55. * LDAP_DEBUG_TRACE 0x0001
  56. * LDAP_DEBUG_CONSTRUCT 0x0002
  57. * LDAP_DEBUG_DESTROY 0x0004
  58. * LDAP_DEBUG_PARAMETER 0x0008
  59. * LDAP_DEBUG_ANY 0xffff
  60. *
  61. * For example, use CURL_OPENLDAP_TRACE=0 for no debug,
  62. * CURL_OPENLDAP_TRACE=2 for LDAP_DEBUG_CONSTRUCT messages only,
  63. * CURL_OPENLDAP_TRACE=65535 for all debug message levels.
  64. */
  65. /* #define CURL_OPENLDAP_DEBUG */
  66. /* Machine states. */
  67. typedef enum {
  68. OLDAP_STOP, /* Do nothing state, stops the state machine */
  69. OLDAP_SSL, /* Performing SSL handshake. */
  70. OLDAP_STARTTLS, /* STARTTLS request sent. */
  71. OLDAP_TLS, /* Performing TLS handshake. */
  72. OLDAP_BIND, /* Simple bind reply. */
  73. OLDAP_BINDV2, /* Simple bind reply in protocol version 2. */
  74. OLDAP_LAST /* Never used */
  75. } ldapstate;
  76. #ifndef _LDAP_PVT_H
  77. extern int ldap_pvt_url_scheme2proto(const char *);
  78. extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
  79. LDAP **ld);
  80. #endif
  81. static CURLcode oldap_setup_connection(struct Curl_easy *data,
  82. struct connectdata *conn);
  83. static CURLcode oldap_do(struct Curl_easy *data, bool *done);
  84. static CURLcode oldap_done(struct Curl_easy *data, CURLcode, bool);
  85. static CURLcode oldap_connect(struct Curl_easy *data, bool *done);
  86. static CURLcode oldap_connecting(struct Curl_easy *data, bool *done);
  87. static CURLcode oldap_disconnect(struct Curl_easy *data,
  88. struct connectdata *conn, bool dead);
  89. static Curl_recv oldap_recv;
  90. /*
  91. * LDAP protocol handler.
  92. */
  93. const struct Curl_handler Curl_handler_ldap = {
  94. "LDAP", /* scheme */
  95. oldap_setup_connection, /* setup_connection */
  96. oldap_do, /* do_it */
  97. oldap_done, /* done */
  98. ZERO_NULL, /* do_more */
  99. oldap_connect, /* connect_it */
  100. oldap_connecting, /* connecting */
  101. ZERO_NULL, /* doing */
  102. ZERO_NULL, /* proto_getsock */
  103. ZERO_NULL, /* doing_getsock */
  104. ZERO_NULL, /* domore_getsock */
  105. ZERO_NULL, /* perform_getsock */
  106. oldap_disconnect, /* disconnect */
  107. ZERO_NULL, /* readwrite */
  108. ZERO_NULL, /* connection_check */
  109. ZERO_NULL, /* attach connection */
  110. PORT_LDAP, /* defport */
  111. CURLPROTO_LDAP, /* protocol */
  112. CURLPROTO_LDAP, /* family */
  113. PROTOPT_NONE /* flags */
  114. };
  115. #ifdef USE_SSL
  116. /*
  117. * LDAPS protocol handler.
  118. */
  119. const struct Curl_handler Curl_handler_ldaps = {
  120. "LDAPS", /* scheme */
  121. oldap_setup_connection, /* setup_connection */
  122. oldap_do, /* do_it */
  123. oldap_done, /* done */
  124. ZERO_NULL, /* do_more */
  125. oldap_connect, /* connect_it */
  126. oldap_connecting, /* connecting */
  127. ZERO_NULL, /* doing */
  128. ZERO_NULL, /* proto_getsock */
  129. ZERO_NULL, /* doing_getsock */
  130. ZERO_NULL, /* domore_getsock */
  131. ZERO_NULL, /* perform_getsock */
  132. oldap_disconnect, /* disconnect */
  133. ZERO_NULL, /* readwrite */
  134. ZERO_NULL, /* connection_check */
  135. ZERO_NULL, /* attach connection */
  136. PORT_LDAPS, /* defport */
  137. CURLPROTO_LDAPS, /* protocol */
  138. CURLPROTO_LDAP, /* family */
  139. PROTOPT_SSL /* flags */
  140. };
  141. #endif
  142. struct ldapconninfo {
  143. LDAP *ld; /* Openldap connection handle. */
  144. Curl_recv *recv; /* For stacking SSL handler */
  145. Curl_send *send;
  146. ldapstate state; /* Current machine state. */
  147. int proto; /* LDAP_PROTO_TCP/LDAP_PROTO_UDP/LDAP_PROTO_IPC */
  148. int msgid; /* Current message id. */
  149. };
  150. struct ldapreqinfo {
  151. int msgid;
  152. int nument;
  153. };
  154. /*
  155. * state()
  156. *
  157. * This is the ONLY way to change LDAP state!
  158. */
  159. static void state(struct Curl_easy *data, ldapstate newstate)
  160. {
  161. struct ldapconninfo *ldapc = data->conn->proto.ldapc;
  162. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  163. /* for debug purposes */
  164. static const char * const names[] = {
  165. "STOP",
  166. "SSL",
  167. "STARTTLS",
  168. "TLS",
  169. "BIND",
  170. "BINDV2",
  171. /* LAST */
  172. };
  173. if(ldapc->state != newstate)
  174. infof(data, "LDAP %p state change from %s to %s",
  175. (void *)ldapc, names[ldapc->state], names[newstate]);
  176. #endif
  177. ldapc->state = newstate;
  178. }
  179. /* Map some particular LDAP error codes to CURLcode values. */
  180. static CURLcode oldap_map_error(int rc, CURLcode result)
  181. {
  182. switch(rc) {
  183. case LDAP_NO_MEMORY:
  184. result = CURLE_OUT_OF_MEMORY;
  185. break;
  186. case LDAP_INVALID_CREDENTIALS:
  187. result = CURLE_LOGIN_DENIED;
  188. break;
  189. case LDAP_PROTOCOL_ERROR:
  190. result = CURLE_UNSUPPORTED_PROTOCOL;
  191. break;
  192. case LDAP_INSUFFICIENT_ACCESS:
  193. result = CURLE_REMOTE_ACCESS_DENIED;
  194. break;
  195. }
  196. return result;
  197. }
  198. static CURLcode oldap_url_parse(struct Curl_easy *data, LDAPURLDesc **ludp)
  199. {
  200. CURLcode result = CURLE_OK;
  201. int rc = LDAP_URL_ERR_BADURL;
  202. static const char * const url_errs[] = {
  203. "success",
  204. "out of memory",
  205. "bad parameter",
  206. "unrecognized scheme",
  207. "unbalanced delimiter",
  208. "bad URL",
  209. "bad host or port",
  210. "bad or missing attributes",
  211. "bad or missing scope",
  212. "bad or missing filter",
  213. "bad or missing extensions"
  214. };
  215. *ludp = NULL;
  216. if(!data->state.up.user && !data->state.up.password &&
  217. !data->state.up.options)
  218. rc = ldap_url_parse(data->state.url, ludp);
  219. if(rc != LDAP_URL_SUCCESS) {
  220. const char *msg = "url parsing problem";
  221. result = rc == LDAP_URL_ERR_MEM? CURLE_OUT_OF_MEMORY: CURLE_URL_MALFORMAT;
  222. rc -= LDAP_URL_SUCCESS;
  223. if((size_t) rc < sizeof(url_errs) / sizeof(url_errs[0]))
  224. msg = url_errs[rc];
  225. failf(data, "LDAP local: %s", msg);
  226. }
  227. return result;
  228. }
  229. static CURLcode oldap_setup_connection(struct Curl_easy *data,
  230. struct connectdata *conn)
  231. {
  232. CURLcode result;
  233. LDAPURLDesc *lud;
  234. struct ldapconninfo *li;
  235. /* Early URL syntax check. */
  236. result = oldap_url_parse(data, &lud);
  237. ldap_free_urldesc(lud);
  238. if(!result) {
  239. li = calloc(1, sizeof(struct ldapconninfo));
  240. if(!li)
  241. result = CURLE_OUT_OF_MEMORY;
  242. else {
  243. li->proto = ldap_pvt_url_scheme2proto(data->state.up.scheme);
  244. conn->proto.ldapc = li;
  245. connkeep(conn, "OpenLDAP default");
  246. /* Clear the TLS upgraded flag */
  247. conn->bits.tls_upgraded = FALSE;
  248. }
  249. }
  250. return result;
  251. }
  252. /* Starts LDAP simple bind. */
  253. static CURLcode oldap_perform_bind(struct Curl_easy *data, ldapstate newstate)
  254. {
  255. CURLcode result = CURLE_OK;
  256. struct connectdata *conn = data->conn;
  257. struct ldapconninfo *li = conn->proto.ldapc;
  258. char *binddn = NULL;
  259. struct berval passwd;
  260. int rc;
  261. passwd.bv_val = NULL;
  262. passwd.bv_len = 0;
  263. if(conn->bits.user_passwd) {
  264. binddn = conn->user;
  265. passwd.bv_val = conn->passwd;
  266. passwd.bv_len = strlen(passwd.bv_val);
  267. }
  268. rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
  269. NULL, NULL, &li->msgid);
  270. if(rc == LDAP_SUCCESS)
  271. state(data, newstate);
  272. else
  273. result = oldap_map_error(rc,
  274. conn->bits.user_passwd?
  275. CURLE_LOGIN_DENIED: CURLE_LDAP_CANNOT_BIND);
  276. return result;
  277. }
  278. #ifdef USE_SSL
  279. static Sockbuf_IO ldapsb_tls;
  280. static bool ssl_installed(struct connectdata *conn)
  281. {
  282. return conn->proto.ldapc->recv != NULL;
  283. }
  284. static CURLcode oldap_ssl_connect(struct Curl_easy *data, ldapstate newstate)
  285. {
  286. CURLcode result = CURLE_OK;
  287. struct connectdata *conn = data->conn;
  288. struct ldapconninfo *li = conn->proto.ldapc;
  289. bool ssldone = 0;
  290. result = Curl_ssl_connect_nonblocking(data, conn, FALSE,
  291. FIRSTSOCKET, &ssldone);
  292. if(!result) {
  293. state(data, newstate);
  294. if(ssldone) {
  295. Sockbuf *sb;
  296. /* Install the libcurl SSL handlers into the sockbuf. */
  297. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  298. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
  299. li->recv = conn->recv[FIRSTSOCKET];
  300. li->send = conn->send[FIRSTSOCKET];
  301. }
  302. }
  303. return result;
  304. }
  305. /* Send the STARTTLS request */
  306. static CURLcode oldap_perform_starttls(struct Curl_easy *data)
  307. {
  308. CURLcode result = CURLE_OK;
  309. struct ldapconninfo *li = data->conn->proto.ldapc;
  310. int rc = ldap_start_tls(li->ld, NULL, NULL, &li->msgid);
  311. if(rc == LDAP_SUCCESS)
  312. state(data, OLDAP_STARTTLS);
  313. else
  314. result = oldap_map_error(rc, CURLE_USE_SSL_FAILED);
  315. return result;
  316. }
  317. #endif
  318. static CURLcode oldap_connect(struct Curl_easy *data, bool *done)
  319. {
  320. struct connectdata *conn = data->conn;
  321. struct ldapconninfo *li = conn->proto.ldapc;
  322. static const int version = LDAP_VERSION3;
  323. int rc;
  324. char *hosturl;
  325. #ifdef CURL_OPENLDAP_DEBUG
  326. static int do_trace = -1;
  327. #endif
  328. (void)done;
  329. hosturl = aprintf("ldap%s://%s:%d",
  330. conn->handler->flags & PROTOPT_SSL? "s": "",
  331. conn->host.name, conn->remote_port);
  332. if(!hosturl)
  333. return CURLE_OUT_OF_MEMORY;
  334. rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
  335. if(rc) {
  336. failf(data, "LDAP local: Cannot connect to %s, %s",
  337. hosturl, ldap_err2string(rc));
  338. free(hosturl);
  339. return CURLE_COULDNT_CONNECT;
  340. }
  341. free(hosturl);
  342. #ifdef CURL_OPENLDAP_DEBUG
  343. if(do_trace < 0) {
  344. const char *env = getenv("CURL_OPENLDAP_TRACE");
  345. do_trace = (env && strtol(env, NULL, 10) > 0);
  346. }
  347. if(do_trace)
  348. ldap_set_option(li->ld, LDAP_OPT_DEBUG_LEVEL, &do_trace);
  349. #endif
  350. /* Try version 3 first. */
  351. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  352. /* Do not chase referrals. */
  353. ldap_set_option(li->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  354. #ifdef USE_SSL
  355. if(conn->handler->flags & PROTOPT_SSL)
  356. return oldap_ssl_connect(data, OLDAP_SSL);
  357. if(data->set.use_ssl) {
  358. CURLcode result = oldap_perform_starttls(data);
  359. if(!result || data->set.use_ssl != CURLUSESSL_TRY)
  360. return result;
  361. }
  362. #endif
  363. /* Force bind even if anonymous bind is not needed in protocol version 3
  364. to detect missing version 3 support. */
  365. return oldap_perform_bind(data, OLDAP_BIND);
  366. }
  367. /* Handle a simple bind response. */
  368. static CURLcode oldap_state_bind_resp(struct Curl_easy *data, LDAPMessage *msg,
  369. int code)
  370. {
  371. struct connectdata *conn = data->conn;
  372. struct ldapconninfo *li = conn->proto.ldapc;
  373. CURLcode result = CURLE_OK;
  374. struct berval *bv = NULL;
  375. int rc;
  376. if(code != LDAP_SUCCESS)
  377. return oldap_map_error(code, CURLE_LDAP_CANNOT_BIND);
  378. rc = ldap_parse_sasl_bind_result(li->ld, msg, &bv, 0);
  379. if(rc != LDAP_SUCCESS) {
  380. failf(data, "LDAP local: bind ldap_parse_sasl_bind_result %s",
  381. ldap_err2string(rc));
  382. result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
  383. }
  384. else
  385. state(data, OLDAP_STOP);
  386. if(bv)
  387. ber_bvfree(bv);
  388. return result;
  389. }
  390. static CURLcode oldap_connecting(struct Curl_easy *data, bool *done)
  391. {
  392. CURLcode result = CURLE_OK;
  393. struct connectdata *conn = data->conn;
  394. struct ldapconninfo *li = conn->proto.ldapc;
  395. LDAPMessage *msg = NULL;
  396. struct timeval tv = {0, 0};
  397. int code = LDAP_SUCCESS;
  398. int rc;
  399. if(li->state != OLDAP_SSL && li->state != OLDAP_TLS) {
  400. /* Get response to last command. */
  401. rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, &tv, &msg);
  402. if(!rc)
  403. return CURLE_OK; /* Timed out. */
  404. if(rc < 0) {
  405. failf(data, "LDAP local: connecting ldap_result %s",
  406. ldap_err2string(rc));
  407. return oldap_map_error(rc, CURLE_COULDNT_CONNECT);
  408. }
  409. /* Get error code from message. */
  410. rc = ldap_parse_result(li->ld, msg, &code, NULL, NULL, NULL, NULL, 0);
  411. if(rc)
  412. code = rc;
  413. else {
  414. /* store the latest code for later retrieval */
  415. data->info.httpcode = code;
  416. }
  417. /* If protocol version 3 is not supported, fallback to version 2. */
  418. if(code == LDAP_PROTOCOL_ERROR && li->state != OLDAP_BINDV2
  419. #ifdef USE_SSL
  420. && (ssl_installed(conn) || data->set.use_ssl <= CURLUSESSL_TRY)
  421. #endif
  422. ) {
  423. static const int version = LDAP_VERSION2;
  424. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  425. ldap_msgfree(msg);
  426. return oldap_perform_bind(data, OLDAP_BINDV2);
  427. }
  428. }
  429. /* Handle response message according to current state. */
  430. switch(li->state) {
  431. #ifdef USE_SSL
  432. case OLDAP_SSL:
  433. result = oldap_ssl_connect(data, OLDAP_SSL);
  434. if(!result && ssl_installed(conn))
  435. result = oldap_perform_bind(data, OLDAP_BIND);
  436. break;
  437. case OLDAP_STARTTLS:
  438. if(code != LDAP_SUCCESS) {
  439. if(data->set.use_ssl != CURLUSESSL_TRY)
  440. result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
  441. else
  442. result = oldap_perform_bind(data, OLDAP_BIND);
  443. break;
  444. }
  445. /* FALLTHROUGH */
  446. case OLDAP_TLS:
  447. result = oldap_ssl_connect(data, OLDAP_TLS);
  448. if(result && data->set.use_ssl != CURLUSESSL_TRY)
  449. result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
  450. else if(ssl_installed(conn)) {
  451. conn->bits.tls_upgraded = TRUE;
  452. if(conn->bits.user_passwd)
  453. result = oldap_perform_bind(data, OLDAP_BIND);
  454. else {
  455. state(data, OLDAP_STOP); /* Version 3 supported: no bind required */
  456. result = CURLE_OK;
  457. }
  458. }
  459. break;
  460. #endif
  461. case OLDAP_BIND:
  462. case OLDAP_BINDV2:
  463. result = oldap_state_bind_resp(data, msg, code);
  464. break;
  465. default:
  466. /* internal error */
  467. result = CURLE_COULDNT_CONNECT;
  468. break;
  469. }
  470. ldap_msgfree(msg);
  471. *done = li->state == OLDAP_STOP;
  472. if(*done)
  473. conn->recv[FIRSTSOCKET] = oldap_recv;
  474. return result;
  475. }
  476. static CURLcode oldap_disconnect(struct Curl_easy *data,
  477. struct connectdata *conn,
  478. bool dead_connection)
  479. {
  480. struct ldapconninfo *li = conn->proto.ldapc;
  481. (void) dead_connection;
  482. if(li) {
  483. if(li->ld) {
  484. #ifdef USE_SSL
  485. if(ssl_installed(conn)) {
  486. Sockbuf *sb;
  487. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  488. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
  489. }
  490. #endif
  491. ldap_unbind_ext(li->ld, NULL, NULL);
  492. li->ld = NULL;
  493. }
  494. conn->proto.ldapc = NULL;
  495. free(li);
  496. }
  497. return CURLE_OK;
  498. }
  499. static CURLcode oldap_do(struct Curl_easy *data, bool *done)
  500. {
  501. struct connectdata *conn = data->conn;
  502. struct ldapconninfo *li = conn->proto.ldapc;
  503. struct ldapreqinfo *lr;
  504. CURLcode result;
  505. int rc;
  506. LDAPURLDesc *lud;
  507. int msgid;
  508. connkeep(conn, "OpenLDAP do");
  509. infof(data, "LDAP local: %s", data->state.url);
  510. result = oldap_url_parse(data, &lud);
  511. if(!result) {
  512. rc = ldap_search_ext(li->ld, lud->lud_dn, lud->lud_scope,
  513. lud->lud_filter, lud->lud_attrs, 0,
  514. NULL, NULL, NULL, 0, &msgid);
  515. ldap_free_urldesc(lud);
  516. if(rc != LDAP_SUCCESS) {
  517. failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
  518. result = CURLE_LDAP_SEARCH_FAILED;
  519. }
  520. else {
  521. lr = calloc(1, sizeof(struct ldapreqinfo));
  522. if(!lr) {
  523. ldap_abandon_ext(li->ld, msgid, NULL, NULL);
  524. result = CURLE_OUT_OF_MEMORY;
  525. }
  526. else {
  527. lr->msgid = msgid;
  528. data->req.p.ldap = lr;
  529. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  530. *done = TRUE;
  531. }
  532. }
  533. }
  534. return result;
  535. }
  536. static CURLcode oldap_done(struct Curl_easy *data, CURLcode res,
  537. bool premature)
  538. {
  539. struct connectdata *conn = data->conn;
  540. struct ldapreqinfo *lr = data->req.p.ldap;
  541. (void)res;
  542. (void)premature;
  543. if(lr) {
  544. /* if there was a search in progress, abandon it */
  545. if(lr->msgid) {
  546. struct ldapconninfo *li = conn->proto.ldapc;
  547. ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
  548. lr->msgid = 0;
  549. }
  550. data->req.p.ldap = NULL;
  551. free(lr);
  552. }
  553. return CURLE_OK;
  554. }
  555. static CURLcode client_write(struct Curl_easy *data, const char *prefix,
  556. const char *value, size_t len, const char *suffix)
  557. {
  558. CURLcode result = CURLE_OK;
  559. size_t l;
  560. if(prefix) {
  561. l = strlen(prefix);
  562. /* If we have a zero-length value and the prefix ends with a space
  563. separator, drop the latter. */
  564. if(!len && l && prefix[l - 1] == ' ')
  565. l--;
  566. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) prefix, l);
  567. if(!result)
  568. data->req.bytecount += l;
  569. }
  570. if(!result && value) {
  571. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) value, len);
  572. if(!result)
  573. data->req.bytecount += len;
  574. }
  575. if(!result && suffix) {
  576. l = strlen(suffix);
  577. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) suffix, l);
  578. if(!result)
  579. data->req.bytecount += l;
  580. }
  581. return result;
  582. }
  583. static ssize_t oldap_recv(struct Curl_easy *data, int sockindex, char *buf,
  584. size_t len, CURLcode *err)
  585. {
  586. struct connectdata *conn = data->conn;
  587. struct ldapconninfo *li = conn->proto.ldapc;
  588. struct ldapreqinfo *lr = data->req.p.ldap;
  589. int rc;
  590. LDAPMessage *msg = NULL;
  591. BerElement *ber = NULL;
  592. struct timeval tv = {0, 0};
  593. struct berval bv, *bvals;
  594. int binary = 0;
  595. CURLcode result = CURLE_AGAIN;
  596. int code;
  597. char *info = NULL;
  598. (void)len;
  599. (void)buf;
  600. (void)sockindex;
  601. rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_ONE, &tv, &msg);
  602. if(rc < 0) {
  603. failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
  604. result = CURLE_RECV_ERROR;
  605. }
  606. *err = result;
  607. /* error or timed out */
  608. if(!msg)
  609. return -1;
  610. result = CURLE_OK;
  611. switch(ldap_msgtype(msg)) {
  612. case LDAP_RES_SEARCH_RESULT:
  613. lr->msgid = 0;
  614. rc = ldap_parse_result(li->ld, msg, &code, NULL, &info, NULL, NULL, 0);
  615. if(rc) {
  616. failf(data, "LDAP local: search ldap_parse_result %s",
  617. ldap_err2string(rc));
  618. result = CURLE_LDAP_SEARCH_FAILED;
  619. break;
  620. }
  621. /* store the latest code for later retrieval */
  622. data->info.httpcode = code;
  623. switch(code) {
  624. case LDAP_SIZELIMIT_EXCEEDED:
  625. infof(data, "There are more than %d entries", lr->nument);
  626. /* FALLTHROUGH */
  627. case LDAP_SUCCESS:
  628. data->req.size = data->req.bytecount;
  629. break;
  630. default:
  631. failf(data, "LDAP remote: search failed %s %s", ldap_err2string(code),
  632. info ? info : "");
  633. result = CURLE_LDAP_SEARCH_FAILED;
  634. break;
  635. }
  636. if(info)
  637. ldap_memfree(info);
  638. break;
  639. case LDAP_RES_SEARCH_ENTRY:
  640. lr->nument++;
  641. rc = ldap_get_dn_ber(li->ld, msg, &ber, &bv);
  642. if(rc < 0) {
  643. result = CURLE_RECV_ERROR;
  644. break;
  645. }
  646. result = client_write(data, "DN: ", bv.bv_val, bv.bv_len, "\n");
  647. if(result)
  648. break;
  649. for(rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals);
  650. rc == LDAP_SUCCESS;
  651. rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals)) {
  652. int i;
  653. if(!bv.bv_val)
  654. break;
  655. if(!bvals) {
  656. result = client_write(data, "\t", bv.bv_val, bv.bv_len, ":\n");
  657. if(result)
  658. break;
  659. continue;
  660. }
  661. binary = bv.bv_len > 7 &&
  662. !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7);
  663. for(i = 0; bvals[i].bv_val != NULL; i++) {
  664. int binval = 0;
  665. result = client_write(data, "\t", bv.bv_val, bv.bv_len, ":");
  666. if(result)
  667. break;
  668. if(!binary) {
  669. /* check for leading or trailing whitespace */
  670. if(ISSPACE(bvals[i].bv_val[0]) ||
  671. ISSPACE(bvals[i].bv_val[bvals[i].bv_len - 1]))
  672. binval = 1;
  673. else {
  674. /* check for unprintable characters */
  675. unsigned int j;
  676. for(j = 0; j < bvals[i].bv_len; j++)
  677. if(!ISPRINT(bvals[i].bv_val[j])) {
  678. binval = 1;
  679. break;
  680. }
  681. }
  682. }
  683. if(binary || binval) {
  684. char *val_b64 = NULL;
  685. size_t val_b64_sz = 0;
  686. /* Binary value, encode to base64. */
  687. if(bvals[i].bv_len)
  688. result = Curl_base64_encode(data, bvals[i].bv_val, bvals[i].bv_len,
  689. &val_b64, &val_b64_sz);
  690. if(!result)
  691. result = client_write(data, ": ", val_b64, val_b64_sz, "\n");
  692. free(val_b64);
  693. }
  694. else
  695. result = client_write(data, " ",
  696. bvals[i].bv_val, bvals[i].bv_len, "\n");
  697. if(result)
  698. break;
  699. }
  700. ber_memfree(bvals);
  701. bvals = NULL;
  702. if(!result)
  703. result = client_write(data, "\n", NULL, 0, NULL);
  704. if(result)
  705. break;
  706. }
  707. ber_free(ber, 0);
  708. if(!result)
  709. result = client_write(data, "\n", NULL, 0, NULL);
  710. if(!result)
  711. result = CURLE_AGAIN;
  712. break;
  713. }
  714. ldap_msgfree(msg);
  715. *err = result;
  716. return result? -1: 0;
  717. }
  718. #ifdef USE_SSL
  719. static int
  720. ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
  721. {
  722. sbiod->sbiod_pvt = arg;
  723. return 0;
  724. }
  725. static int
  726. ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
  727. {
  728. sbiod->sbiod_pvt = NULL;
  729. return 0;
  730. }
  731. /* We don't need to do anything because libcurl does it already */
  732. static int
  733. ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
  734. {
  735. (void)sbiod;
  736. return 0;
  737. }
  738. static int
  739. ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
  740. {
  741. (void)arg;
  742. if(opt == LBER_SB_OPT_DATA_READY) {
  743. struct Curl_easy *data = sbiod->sbiod_pvt;
  744. return Curl_ssl_data_pending(data->conn, FIRSTSOCKET);
  745. }
  746. return 0;
  747. }
  748. static ber_slen_t
  749. ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  750. {
  751. struct Curl_easy *data = sbiod->sbiod_pvt;
  752. ber_slen_t ret = 0;
  753. if(data) {
  754. struct connectdata *conn = data->conn;
  755. if(conn) {
  756. struct ldapconninfo *li = conn->proto.ldapc;
  757. CURLcode err = CURLE_RECV_ERROR;
  758. ret = (li->recv)(data, FIRSTSOCKET, buf, len, &err);
  759. if(ret < 0 && err == CURLE_AGAIN) {
  760. SET_SOCKERRNO(EWOULDBLOCK);
  761. }
  762. }
  763. }
  764. return ret;
  765. }
  766. static ber_slen_t
  767. ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  768. {
  769. struct Curl_easy *data = sbiod->sbiod_pvt;
  770. ber_slen_t ret = 0;
  771. if(data) {
  772. struct connectdata *conn = data->conn;
  773. if(conn) {
  774. struct ldapconninfo *li = conn->proto.ldapc;
  775. CURLcode err = CURLE_SEND_ERROR;
  776. ret = (li->send)(data, FIRSTSOCKET, buf, len, &err);
  777. if(ret < 0 && err == CURLE_AGAIN) {
  778. SET_SOCKERRNO(EWOULDBLOCK);
  779. }
  780. }
  781. }
  782. return ret;
  783. }
  784. static Sockbuf_IO ldapsb_tls =
  785. {
  786. ldapsb_tls_setup,
  787. ldapsb_tls_remove,
  788. ldapsb_tls_ctrl,
  789. ldapsb_tls_read,
  790. ldapsb_tls_write,
  791. ldapsb_tls_close
  792. };
  793. #endif /* USE_SSL */
  794. #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */