openldap.c 19 KB

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