openldap.c 21 KB

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