openldap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2010, Howard Chu, <hyc@openldap.org>
  9. * Copyright (C) 2011 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
  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.haxx.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. #ifndef _LDAP_PVT_H
  67. extern int ldap_pvt_url_scheme2proto(const char *);
  68. extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
  69. LDAP **ld);
  70. #endif
  71. static CURLcode ldap_setup_connection(struct connectdata *conn);
  72. static CURLcode ldap_do(struct connectdata *conn, bool *done);
  73. static CURLcode ldap_done(struct connectdata *conn, CURLcode, bool);
  74. static CURLcode ldap_connect(struct connectdata *conn, bool *done);
  75. static CURLcode ldap_connecting(struct connectdata *conn, bool *done);
  76. static CURLcode ldap_disconnect(struct connectdata *conn, bool dead);
  77. static Curl_recv ldap_recv;
  78. /*
  79. * LDAP protocol handler.
  80. */
  81. const struct Curl_handler Curl_handler_ldap = {
  82. "LDAP", /* scheme */
  83. ldap_setup_connection, /* setup_connection */
  84. ldap_do, /* do_it */
  85. ldap_done, /* done */
  86. ZERO_NULL, /* do_more */
  87. ldap_connect, /* connect_it */
  88. ldap_connecting, /* connecting */
  89. ZERO_NULL, /* doing */
  90. ZERO_NULL, /* proto_getsock */
  91. ZERO_NULL, /* doing_getsock */
  92. ZERO_NULL, /* domore_getsock */
  93. ZERO_NULL, /* perform_getsock */
  94. ldap_disconnect, /* disconnect */
  95. ZERO_NULL, /* readwrite */
  96. ZERO_NULL, /* connection_check */
  97. PORT_LDAP, /* defport */
  98. CURLPROTO_LDAP, /* protocol */
  99. PROTOPT_NONE /* flags */
  100. };
  101. #ifdef USE_SSL
  102. /*
  103. * LDAPS protocol handler.
  104. */
  105. const struct Curl_handler Curl_handler_ldaps = {
  106. "LDAPS", /* scheme */
  107. ldap_setup_connection, /* setup_connection */
  108. ldap_do, /* do_it */
  109. ldap_done, /* done */
  110. ZERO_NULL, /* do_more */
  111. ldap_connect, /* connect_it */
  112. ldap_connecting, /* connecting */
  113. ZERO_NULL, /* doing */
  114. ZERO_NULL, /* proto_getsock */
  115. ZERO_NULL, /* doing_getsock */
  116. ZERO_NULL, /* domore_getsock */
  117. ZERO_NULL, /* perform_getsock */
  118. ldap_disconnect, /* disconnect */
  119. ZERO_NULL, /* readwrite */
  120. ZERO_NULL, /* connection_check */
  121. PORT_LDAPS, /* defport */
  122. CURLPROTO_LDAP, /* protocol */
  123. PROTOPT_SSL /* flags */
  124. };
  125. #endif
  126. static const char *url_errs[] = {
  127. "success",
  128. "out of memory",
  129. "bad parameter",
  130. "unrecognized scheme",
  131. "unbalanced delimiter",
  132. "bad URL",
  133. "bad host or port",
  134. "bad or missing attributes",
  135. "bad or missing scope",
  136. "bad or missing filter",
  137. "bad or missing extensions"
  138. };
  139. typedef struct ldapconninfo {
  140. LDAP *ld;
  141. Curl_recv *recv; /* for stacking SSL handler */
  142. Curl_send *send;
  143. int proto;
  144. int msgid;
  145. bool ssldone;
  146. bool sslinst;
  147. bool didbind;
  148. } ldapconninfo;
  149. typedef struct ldapreqinfo {
  150. int msgid;
  151. int nument;
  152. } ldapreqinfo;
  153. static CURLcode ldap_setup_connection(struct connectdata *conn)
  154. {
  155. ldapconninfo *li;
  156. LDAPURLDesc *lud;
  157. struct Curl_easy *data = conn->data;
  158. int rc, proto;
  159. CURLcode status;
  160. rc = ldap_url_parse(data->change.url, &lud);
  161. if(rc != LDAP_URL_SUCCESS) {
  162. const char *msg = "url parsing problem";
  163. status = CURLE_URL_MALFORMAT;
  164. if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
  165. if(rc == LDAP_URL_ERR_MEM)
  166. status = CURLE_OUT_OF_MEMORY;
  167. msg = url_errs[rc];
  168. }
  169. failf(conn->data, "LDAP local: %s", msg);
  170. return status;
  171. }
  172. proto = ldap_pvt_url_scheme2proto(lud->lud_scheme);
  173. ldap_free_urldesc(lud);
  174. li = calloc(1, sizeof(ldapconninfo));
  175. if(!li)
  176. return CURLE_OUT_OF_MEMORY;
  177. li->proto = proto;
  178. conn->proto.generic = li;
  179. connkeep(conn, "OpenLDAP default");
  180. /* TODO:
  181. * - provide option to choose SASL Binds instead of Simple
  182. */
  183. return CURLE_OK;
  184. }
  185. #ifdef USE_SSL
  186. static Sockbuf_IO ldapsb_tls;
  187. #endif
  188. static CURLcode ldap_connect(struct connectdata *conn, bool *done)
  189. {
  190. ldapconninfo *li = conn->proto.generic;
  191. struct Curl_easy *data = conn->data;
  192. int rc, proto = LDAP_VERSION3;
  193. char hosturl[1024];
  194. char *ptr;
  195. (void)done;
  196. strcpy(hosturl, "ldap");
  197. ptr = hosturl + 4;
  198. if(conn->handler->flags & PROTOPT_SSL)
  199. *ptr++ = 's';
  200. msnprintf(ptr, sizeof(hosturl)-(ptr-hosturl), "://%s:%d",
  201. conn->host.name, conn->remote_port);
  202. #ifdef CURL_OPENLDAP_DEBUG
  203. static int do_trace = 0;
  204. const char *env = getenv("CURL_OPENLDAP_TRACE");
  205. do_trace = (env && strtol(env, NULL, 10) > 0);
  206. if(do_trace) {
  207. ldap_set_option(li->ld, LDAP_OPT_DEBUG_LEVEL, &do_trace);
  208. }
  209. #endif
  210. rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
  211. if(rc) {
  212. failf(data, "LDAP local: Cannot connect to %s, %s",
  213. hosturl, ldap_err2string(rc));
  214. return CURLE_COULDNT_CONNECT;
  215. }
  216. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
  217. #ifdef USE_SSL
  218. if(conn->handler->flags & PROTOPT_SSL) {
  219. CURLcode result;
  220. result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET, &li->ssldone);
  221. if(result)
  222. return result;
  223. }
  224. #endif
  225. return CURLE_OK;
  226. }
  227. static CURLcode ldap_connecting(struct connectdata *conn, bool *done)
  228. {
  229. ldapconninfo *li = conn->proto.generic;
  230. struct Curl_easy *data = conn->data;
  231. LDAPMessage *msg = NULL;
  232. struct timeval tv = {0, 1}, *tvp;
  233. int rc, err;
  234. char *info = NULL;
  235. #ifdef USE_SSL
  236. if(conn->handler->flags & PROTOPT_SSL) {
  237. /* Is the SSL handshake complete yet? */
  238. if(!li->ssldone) {
  239. CURLcode result = Curl_ssl_connect_nonblocking(conn, FIRSTSOCKET,
  240. &li->ssldone);
  241. if(result || !li->ssldone)
  242. return result;
  243. }
  244. /* Have we installed the libcurl SSL handlers into the sockbuf yet? */
  245. if(!li->sslinst) {
  246. Sockbuf *sb;
  247. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  248. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, conn);
  249. li->sslinst = TRUE;
  250. li->recv = conn->recv[FIRSTSOCKET];
  251. li->send = conn->send[FIRSTSOCKET];
  252. }
  253. }
  254. #endif
  255. tvp = &tv;
  256. retry:
  257. if(!li->didbind) {
  258. char *binddn;
  259. struct berval passwd;
  260. if(conn->bits.user_passwd) {
  261. binddn = conn->user;
  262. passwd.bv_val = conn->passwd;
  263. passwd.bv_len = strlen(passwd.bv_val);
  264. }
  265. else {
  266. binddn = NULL;
  267. passwd.bv_val = NULL;
  268. passwd.bv_len = 0;
  269. }
  270. rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
  271. NULL, NULL, &li->msgid);
  272. if(rc)
  273. return CURLE_LDAP_CANNOT_BIND;
  274. li->didbind = TRUE;
  275. if(tvp)
  276. return CURLE_OK;
  277. }
  278. rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, tvp, &msg);
  279. if(rc < 0) {
  280. failf(data, "LDAP local: bind ldap_result %s", ldap_err2string(rc));
  281. return CURLE_LDAP_CANNOT_BIND;
  282. }
  283. if(rc == 0) {
  284. /* timed out */
  285. return CURLE_OK;
  286. }
  287. rc = ldap_parse_result(li->ld, msg, &err, NULL, &info, NULL, NULL, 1);
  288. if(rc) {
  289. failf(data, "LDAP local: bind ldap_parse_result %s", ldap_err2string(rc));
  290. return CURLE_LDAP_CANNOT_BIND;
  291. }
  292. /* Try to fallback to LDAPv2? */
  293. if(err == LDAP_PROTOCOL_ERROR) {
  294. int proto;
  295. ldap_get_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
  296. if(proto == LDAP_VERSION3) {
  297. if(info) {
  298. ldap_memfree(info);
  299. info = NULL;
  300. }
  301. proto = LDAP_VERSION2;
  302. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &proto);
  303. li->didbind = FALSE;
  304. goto retry;
  305. }
  306. }
  307. if(err) {
  308. failf(data, "LDAP remote: bind failed %s %s", ldap_err2string(rc),
  309. info ? info : "");
  310. if(info)
  311. ldap_memfree(info);
  312. return CURLE_LOGIN_DENIED;
  313. }
  314. if(info)
  315. ldap_memfree(info);
  316. conn->recv[FIRSTSOCKET] = ldap_recv;
  317. *done = TRUE;
  318. return CURLE_OK;
  319. }
  320. static CURLcode ldap_disconnect(struct connectdata *conn, bool dead_connection)
  321. {
  322. ldapconninfo *li = conn->proto.generic;
  323. (void) dead_connection;
  324. if(li) {
  325. if(li->ld) {
  326. ldap_unbind_ext(li->ld, NULL, NULL);
  327. li->ld = NULL;
  328. }
  329. conn->proto.generic = NULL;
  330. free(li);
  331. }
  332. return CURLE_OK;
  333. }
  334. static CURLcode ldap_do(struct connectdata *conn, bool *done)
  335. {
  336. ldapconninfo *li = conn->proto.generic;
  337. ldapreqinfo *lr;
  338. CURLcode status = CURLE_OK;
  339. int rc = 0;
  340. LDAPURLDesc *ludp = NULL;
  341. int msgid;
  342. struct Curl_easy *data = conn->data;
  343. connkeep(conn, "OpenLDAP do");
  344. infof(data, "LDAP local: %s\n", data->change.url);
  345. rc = ldap_url_parse(data->change.url, &ludp);
  346. if(rc != LDAP_URL_SUCCESS) {
  347. const char *msg = "url parsing problem";
  348. status = CURLE_URL_MALFORMAT;
  349. if(rc > LDAP_URL_SUCCESS && rc <= LDAP_URL_ERR_BADEXTS) {
  350. if(rc == LDAP_URL_ERR_MEM)
  351. status = CURLE_OUT_OF_MEMORY;
  352. msg = url_errs[rc];
  353. }
  354. failf(conn->data, "LDAP local: %s", msg);
  355. return status;
  356. }
  357. rc = ldap_search_ext(li->ld, ludp->lud_dn, ludp->lud_scope,
  358. ludp->lud_filter, ludp->lud_attrs, 0,
  359. NULL, NULL, NULL, 0, &msgid);
  360. ldap_free_urldesc(ludp);
  361. if(rc != LDAP_SUCCESS) {
  362. failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
  363. return CURLE_LDAP_SEARCH_FAILED;
  364. }
  365. lr = calloc(1, sizeof(ldapreqinfo));
  366. if(!lr)
  367. return CURLE_OUT_OF_MEMORY;
  368. lr->msgid = msgid;
  369. data->req.protop = lr;
  370. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  371. *done = TRUE;
  372. return CURLE_OK;
  373. }
  374. static CURLcode ldap_done(struct connectdata *conn, CURLcode res,
  375. bool premature)
  376. {
  377. ldapreqinfo *lr = conn->data->req.protop;
  378. (void)res;
  379. (void)premature;
  380. if(lr) {
  381. /* if there was a search in progress, abandon it */
  382. if(lr->msgid) {
  383. ldapconninfo *li = conn->proto.generic;
  384. ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
  385. lr->msgid = 0;
  386. }
  387. conn->data->req.protop = NULL;
  388. free(lr);
  389. }
  390. return CURLE_OK;
  391. }
  392. static ssize_t ldap_recv(struct connectdata *conn, int sockindex, char *buf,
  393. size_t len, CURLcode *err)
  394. {
  395. ldapconninfo *li = conn->proto.generic;
  396. struct Curl_easy *data = conn->data;
  397. ldapreqinfo *lr = data->req.protop;
  398. int rc, ret;
  399. LDAPMessage *msg = NULL;
  400. LDAPMessage *ent;
  401. BerElement *ber = NULL;
  402. struct timeval tv = {0, 1};
  403. (void)len;
  404. (void)buf;
  405. (void)sockindex;
  406. rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_RECEIVED, &tv, &msg);
  407. if(rc < 0) {
  408. failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
  409. *err = CURLE_RECV_ERROR;
  410. return -1;
  411. }
  412. *err = CURLE_AGAIN;
  413. ret = -1;
  414. /* timed out */
  415. if(!msg)
  416. return ret;
  417. for(ent = ldap_first_message(li->ld, msg); ent;
  418. ent = ldap_next_message(li->ld, ent)) {
  419. struct berval bv, *bvals;
  420. int binary = 0, msgtype;
  421. CURLcode writeerr;
  422. msgtype = ldap_msgtype(ent);
  423. if(msgtype == LDAP_RES_SEARCH_RESULT) {
  424. int code;
  425. char *info = NULL;
  426. rc = ldap_parse_result(li->ld, ent, &code, NULL, &info, NULL, NULL, 0);
  427. if(rc) {
  428. failf(data, "LDAP local: search ldap_parse_result %s",
  429. ldap_err2string(rc));
  430. *err = CURLE_LDAP_SEARCH_FAILED;
  431. }
  432. else if(code && code != LDAP_SIZELIMIT_EXCEEDED) {
  433. failf(data, "LDAP remote: search failed %s %s", ldap_err2string(rc),
  434. info ? info : "");
  435. *err = CURLE_LDAP_SEARCH_FAILED;
  436. }
  437. else {
  438. /* successful */
  439. if(code == LDAP_SIZELIMIT_EXCEEDED)
  440. infof(data, "There are more than %d entries\n", lr->nument);
  441. data->req.size = data->req.bytecount;
  442. *err = CURLE_OK;
  443. ret = 0;
  444. }
  445. lr->msgid = 0;
  446. ldap_memfree(info);
  447. break;
  448. }
  449. else if(msgtype != LDAP_RES_SEARCH_ENTRY)
  450. continue;
  451. lr->nument++;
  452. rc = ldap_get_dn_ber(li->ld, ent, &ber, &bv);
  453. if(rc < 0) {
  454. /* TODO: verify that this is really how this return code should be
  455. handled */
  456. *err = CURLE_RECV_ERROR;
  457. return -1;
  458. }
  459. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"DN: ", 4);
  460. if(writeerr) {
  461. *err = writeerr;
  462. return -1;
  463. }
  464. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
  465. bv.bv_len);
  466. if(writeerr) {
  467. *err = writeerr;
  468. return -1;
  469. }
  470. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 1);
  471. if(writeerr) {
  472. *err = writeerr;
  473. return -1;
  474. }
  475. data->req.bytecount += bv.bv_len + 5;
  476. for(rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, &bvals);
  477. rc == LDAP_SUCCESS;
  478. rc = ldap_get_attribute_ber(li->ld, ent, ber, &bv, &bvals)) {
  479. int i;
  480. if(bv.bv_val == NULL)
  481. break;
  482. if(bv.bv_len > 7 && !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7))
  483. binary = 1;
  484. else
  485. binary = 0;
  486. if(bvals == NULL) {
  487. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  488. if(writeerr) {
  489. *err = writeerr;
  490. return -1;
  491. }
  492. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
  493. bv.bv_len);
  494. if(writeerr) {
  495. *err = writeerr;
  496. return -1;
  497. }
  498. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":\n", 2);
  499. if(writeerr) {
  500. *err = writeerr;
  501. return -1;
  502. }
  503. data->req.bytecount += bv.bv_len + 3;
  504. continue;
  505. }
  506. for(i = 0; bvals[i].bv_val != NULL; i++) {
  507. int binval = 0;
  508. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\t", 1);
  509. if(writeerr) {
  510. *err = writeerr;
  511. return -1;
  512. }
  513. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)bv.bv_val,
  514. bv.bv_len);
  515. if(writeerr) {
  516. *err = writeerr;
  517. return -1;
  518. }
  519. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)":", 1);
  520. if(writeerr) {
  521. *err = writeerr;
  522. return -1;
  523. }
  524. data->req.bytecount += bv.bv_len + 2;
  525. if(!binary) {
  526. /* check for leading or trailing whitespace */
  527. if(ISSPACE(bvals[i].bv_val[0]) ||
  528. ISSPACE(bvals[i].bv_val[bvals[i].bv_len-1]))
  529. binval = 1;
  530. else {
  531. /* check for unprintable characters */
  532. unsigned int j;
  533. for(j = 0; j<bvals[i].bv_len; j++)
  534. if(!ISPRINT(bvals[i].bv_val[j])) {
  535. binval = 1;
  536. break;
  537. }
  538. }
  539. }
  540. if(binary || binval) {
  541. char *val_b64 = NULL;
  542. size_t val_b64_sz = 0;
  543. /* Binary value, encode to base64. */
  544. CURLcode error = Curl_base64_encode(data,
  545. bvals[i].bv_val,
  546. bvals[i].bv_len,
  547. &val_b64,
  548. &val_b64_sz);
  549. if(error) {
  550. ber_memfree(bvals);
  551. ber_free(ber, 0);
  552. ldap_msgfree(msg);
  553. *err = error;
  554. return -1;
  555. }
  556. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY,
  557. (char *)": ", 2);
  558. if(writeerr) {
  559. *err = writeerr;
  560. return -1;
  561. }
  562. data->req.bytecount += 2;
  563. if(val_b64_sz > 0) {
  564. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, val_b64,
  565. val_b64_sz);
  566. if(writeerr) {
  567. *err = writeerr;
  568. return -1;
  569. }
  570. free(val_b64);
  571. data->req.bytecount += val_b64_sz;
  572. }
  573. }
  574. else {
  575. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)" ", 1);
  576. if(writeerr) {
  577. *err = writeerr;
  578. return -1;
  579. }
  580. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, bvals[i].bv_val,
  581. bvals[i].bv_len);
  582. if(writeerr) {
  583. *err = writeerr;
  584. return -1;
  585. }
  586. data->req.bytecount += bvals[i].bv_len + 1;
  587. }
  588. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  589. if(writeerr) {
  590. *err = writeerr;
  591. return -1;
  592. }
  593. data->req.bytecount++;
  594. }
  595. ber_memfree(bvals);
  596. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  597. if(writeerr) {
  598. *err = writeerr;
  599. return -1;
  600. }
  601. data->req.bytecount++;
  602. }
  603. writeerr = Curl_client_write(conn, CLIENTWRITE_BODY, (char *)"\n", 0);
  604. if(writeerr) {
  605. *err = writeerr;
  606. return -1;
  607. }
  608. data->req.bytecount++;
  609. ber_free(ber, 0);
  610. }
  611. ldap_msgfree(msg);
  612. return ret;
  613. }
  614. #ifdef USE_SSL
  615. static int
  616. ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
  617. {
  618. sbiod->sbiod_pvt = arg;
  619. return 0;
  620. }
  621. static int
  622. ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
  623. {
  624. sbiod->sbiod_pvt = NULL;
  625. return 0;
  626. }
  627. /* We don't need to do anything because libcurl does it already */
  628. static int
  629. ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
  630. {
  631. (void)sbiod;
  632. return 0;
  633. }
  634. static int
  635. ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
  636. {
  637. (void)arg;
  638. if(opt == LBER_SB_OPT_DATA_READY) {
  639. struct connectdata *conn = sbiod->sbiod_pvt;
  640. return Curl_ssl_data_pending(conn, FIRSTSOCKET);
  641. }
  642. return 0;
  643. }
  644. static ber_slen_t
  645. ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  646. {
  647. struct connectdata *conn = sbiod->sbiod_pvt;
  648. ldapconninfo *li = conn->proto.generic;
  649. ber_slen_t ret;
  650. CURLcode err = CURLE_RECV_ERROR;
  651. ret = (li->recv)(conn, FIRSTSOCKET, buf, len, &err);
  652. if(ret < 0 && err == CURLE_AGAIN) {
  653. SET_SOCKERRNO(EWOULDBLOCK);
  654. }
  655. return ret;
  656. }
  657. static ber_slen_t
  658. ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  659. {
  660. struct connectdata *conn = sbiod->sbiod_pvt;
  661. ldapconninfo *li = conn->proto.generic;
  662. ber_slen_t ret;
  663. CURLcode err = CURLE_SEND_ERROR;
  664. ret = (li->send)(conn, FIRSTSOCKET, buf, len, &err);
  665. if(ret < 0 && err == CURLE_AGAIN) {
  666. SET_SOCKERRNO(EWOULDBLOCK);
  667. }
  668. return ret;
  669. }
  670. static Sockbuf_IO ldapsb_tls =
  671. {
  672. ldapsb_tls_setup,
  673. ldapsb_tls_remove,
  674. ldapsb_tls_ctrl,
  675. ldapsb_tls_read,
  676. ldapsb_tls_write,
  677. ldapsb_tls_close
  678. };
  679. #endif /* USE_SSL */
  680. #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */