openldap.c 18 KB

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