openldap.c 18 KB

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