openldap.c 19 KB

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