openldap.c 18 KB

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