openldap.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) Howard Chu, <hyc@openldap.org>
  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.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. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #if !defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP)
  27. /*
  28. * Notice that USE_OPENLDAP is only a source code selection switch. When
  29. * libcurl is built with USE_OPENLDAP defined the libcurl source code that
  30. * gets compiled is the code from openldap.c, otherwise the code that gets
  31. * compiled is the code from ldap.c.
  32. *
  33. * When USE_OPENLDAP is defined a recent version of the OpenLDAP library
  34. * might be required for compilation and runtime. In order to use ancient
  35. * OpenLDAP library versions, USE_OPENLDAP shall not be defined.
  36. */
  37. #error #include <ldap.h>
  38. #include "urldata.h"
  39. #include <curl/curl.h>
  40. #include "sendf.h"
  41. #include "vtls/vtls.h"
  42. #include "transfer.h"
  43. #include "curl_ldap.h"
  44. #include "curl_base64.h"
  45. #include "cfilters.h"
  46. #include "connect.h"
  47. #include "curl_sasl.h"
  48. #include "strcase.h"
  49. /* The last 3 #include files should be in this order */
  50. #include "curl_printf.h"
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. /*
  54. * Uncommenting this will enable the built-in debug logging of the openldap
  55. * library. The debug log level can be set using the CURL_OPENLDAP_TRACE
  56. * environment variable. The debug output is written to stderr.
  57. *
  58. * The library supports the following debug flags:
  59. * LDAP_DEBUG_NONE 0x0000
  60. * LDAP_DEBUG_TRACE 0x0001
  61. * LDAP_DEBUG_CONSTRUCT 0x0002
  62. * LDAP_DEBUG_DESTROY 0x0004
  63. * LDAP_DEBUG_PARAMETER 0x0008
  64. * LDAP_DEBUG_ANY 0xffff
  65. *
  66. * For example, use CURL_OPENLDAP_TRACE=0 for no debug,
  67. * CURL_OPENLDAP_TRACE=2 for LDAP_DEBUG_CONSTRUCT messages only,
  68. * CURL_OPENLDAP_TRACE=65535 for all debug message levels.
  69. */
  70. /* #define CURL_OPENLDAP_DEBUG */
  71. /* Machine states. */
  72. typedef enum {
  73. OLDAP_STOP, /* Do nothing state, stops the state machine */
  74. OLDAP_SSL, /* Performing SSL handshake. */
  75. OLDAP_STARTTLS, /* STARTTLS request sent. */
  76. OLDAP_TLS, /* Performing TLS handshake. */
  77. OLDAP_MECHS, /* Get SASL authentication mechanisms. */
  78. OLDAP_SASL, /* SASL binding reply. */
  79. OLDAP_BIND, /* Simple bind reply. */
  80. OLDAP_BINDV2, /* Simple bind reply in protocol version 2. */
  81. OLDAP_LAST /* Never used */
  82. } ldapstate;
  83. #ifndef _LDAP_PVT_H
  84. extern int ldap_pvt_url_scheme2proto(const char *);
  85. extern int ldap_init_fd(ber_socket_t fd, int proto, const char *url,
  86. LDAP **ld);
  87. #endif
  88. static CURLcode oldap_setup_connection(struct Curl_easy *data,
  89. struct connectdata *conn);
  90. static CURLcode oldap_do(struct Curl_easy *data, bool *done);
  91. static CURLcode oldap_done(struct Curl_easy *data, CURLcode, bool);
  92. static CURLcode oldap_connect(struct Curl_easy *data, bool *done);
  93. static CURLcode oldap_connecting(struct Curl_easy *data, bool *done);
  94. static CURLcode oldap_disconnect(struct Curl_easy *data,
  95. struct connectdata *conn, bool dead);
  96. static CURLcode oldap_perform_auth(struct Curl_easy *data, const char *mech,
  97. const struct bufref *initresp);
  98. static CURLcode oldap_continue_auth(struct Curl_easy *data, const char *mech,
  99. const struct bufref *resp);
  100. static CURLcode oldap_cancel_auth(struct Curl_easy *data, const char *mech);
  101. static CURLcode oldap_get_message(struct Curl_easy *data, struct bufref *out);
  102. static Curl_recv oldap_recv;
  103. /*
  104. * LDAP protocol handler.
  105. */
  106. const struct Curl_handler Curl_handler_ldap = {
  107. "LDAP", /* scheme */
  108. oldap_setup_connection, /* setup_connection */
  109. oldap_do, /* do_it */
  110. oldap_done, /* done */
  111. ZERO_NULL, /* do_more */
  112. oldap_connect, /* connect_it */
  113. oldap_connecting, /* connecting */
  114. ZERO_NULL, /* doing */
  115. ZERO_NULL, /* proto_getsock */
  116. ZERO_NULL, /* doing_getsock */
  117. ZERO_NULL, /* domore_getsock */
  118. ZERO_NULL, /* perform_getsock */
  119. oldap_disconnect, /* disconnect */
  120. ZERO_NULL, /* readwrite */
  121. ZERO_NULL, /* connection_check */
  122. ZERO_NULL, /* attach connection */
  123. PORT_LDAP, /* defport */
  124. CURLPROTO_LDAP, /* protocol */
  125. CURLPROTO_LDAP, /* family */
  126. PROTOPT_NONE /* flags */
  127. };
  128. #ifdef USE_SSL
  129. /*
  130. * LDAPS protocol handler.
  131. */
  132. const struct Curl_handler Curl_handler_ldaps = {
  133. "LDAPS", /* scheme */
  134. oldap_setup_connection, /* setup_connection */
  135. oldap_do, /* do_it */
  136. oldap_done, /* done */
  137. ZERO_NULL, /* do_more */
  138. oldap_connect, /* connect_it */
  139. oldap_connecting, /* connecting */
  140. ZERO_NULL, /* doing */
  141. ZERO_NULL, /* proto_getsock */
  142. ZERO_NULL, /* doing_getsock */
  143. ZERO_NULL, /* domore_getsock */
  144. ZERO_NULL, /* perform_getsock */
  145. oldap_disconnect, /* disconnect */
  146. ZERO_NULL, /* readwrite */
  147. ZERO_NULL, /* connection_check */
  148. ZERO_NULL, /* attach connection */
  149. PORT_LDAPS, /* defport */
  150. CURLPROTO_LDAPS, /* protocol */
  151. CURLPROTO_LDAP, /* family */
  152. PROTOPT_SSL /* flags */
  153. };
  154. #endif
  155. /* SASL parameters for the ldap protocol */
  156. static const struct SASLproto saslldap = {
  157. "ldap", /* The service name */
  158. oldap_perform_auth, /* Send authentication command */
  159. oldap_continue_auth, /* Send authentication continuation */
  160. oldap_cancel_auth, /* Send authentication cancellation */
  161. oldap_get_message, /* Get SASL response message */
  162. 0, /* Maximum initial response length (no max) */
  163. LDAP_SASL_BIND_IN_PROGRESS, /* Code received when continuation is expected */
  164. LDAP_SUCCESS, /* Code to receive upon authentication success */
  165. SASL_AUTH_NONE, /* Default mechanisms */
  166. 0 /* Configuration flags */
  167. };
  168. struct ldapconninfo {
  169. struct SASL sasl; /* SASL-related parameters */
  170. LDAP *ld; /* Openldap connection handle. */
  171. Curl_recv *recv; /* For stacking SSL handler */
  172. Curl_send *send;
  173. struct berval *servercred; /* SASL data from server. */
  174. ldapstate state; /* Current machine state. */
  175. int proto; /* LDAP_PROTO_TCP/LDAP_PROTO_UDP/LDAP_PROTO_IPC */
  176. int msgid; /* Current message id. */
  177. };
  178. struct ldapreqinfo {
  179. int msgid;
  180. int nument;
  181. };
  182. /*
  183. * oldap_state()
  184. *
  185. * This is the ONLY way to change LDAP state!
  186. */
  187. static void oldap_state(struct Curl_easy *data, ldapstate newstate)
  188. {
  189. struct ldapconninfo *ldapc = data->conn->proto.ldapc;
  190. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  191. /* for debug purposes */
  192. static const char * const names[] = {
  193. "STOP",
  194. "SSL",
  195. "STARTTLS",
  196. "TLS",
  197. "MECHS",
  198. "SASL",
  199. "BIND",
  200. "BINDV2",
  201. /* LAST */
  202. };
  203. if(ldapc->state != newstate)
  204. infof(data, "LDAP %p state change from %s to %s",
  205. (void *)ldapc, names[ldapc->state], names[newstate]);
  206. #endif
  207. ldapc->state = newstate;
  208. }
  209. /* Map some particular LDAP error codes to CURLcode values. */
  210. static CURLcode oldap_map_error(int rc, CURLcode result)
  211. {
  212. switch(rc) {
  213. case LDAP_NO_MEMORY:
  214. result = CURLE_OUT_OF_MEMORY;
  215. break;
  216. case LDAP_INVALID_CREDENTIALS:
  217. result = CURLE_LOGIN_DENIED;
  218. break;
  219. case LDAP_PROTOCOL_ERROR:
  220. result = CURLE_UNSUPPORTED_PROTOCOL;
  221. break;
  222. case LDAP_INSUFFICIENT_ACCESS:
  223. result = CURLE_REMOTE_ACCESS_DENIED;
  224. break;
  225. }
  226. return result;
  227. }
  228. static CURLcode oldap_url_parse(struct Curl_easy *data, LDAPURLDesc **ludp)
  229. {
  230. CURLcode result = CURLE_OK;
  231. int rc = LDAP_URL_ERR_BADURL;
  232. static const char * const url_errs[] = {
  233. "success",
  234. "out of memory",
  235. "bad parameter",
  236. "unrecognized scheme",
  237. "unbalanced delimiter",
  238. "bad URL",
  239. "bad host or port",
  240. "bad or missing attributes",
  241. "bad or missing scope",
  242. "bad or missing filter",
  243. "bad or missing extensions"
  244. };
  245. *ludp = NULL;
  246. if(!data->state.up.user && !data->state.up.password &&
  247. !data->state.up.options)
  248. rc = ldap_url_parse(data->state.url, ludp);
  249. if(rc != LDAP_URL_SUCCESS) {
  250. const char *msg = "url parsing problem";
  251. result = rc == LDAP_URL_ERR_MEM? CURLE_OUT_OF_MEMORY: CURLE_URL_MALFORMAT;
  252. rc -= LDAP_URL_SUCCESS;
  253. if((size_t) rc < sizeof(url_errs) / sizeof(url_errs[0]))
  254. msg = url_errs[rc];
  255. failf(data, "LDAP local: %s", msg);
  256. }
  257. return result;
  258. }
  259. /* Parse the login options. */
  260. static CURLcode oldap_parse_login_options(struct connectdata *conn)
  261. {
  262. CURLcode result = CURLE_OK;
  263. struct ldapconninfo *li = conn->proto.ldapc;
  264. const char *ptr = conn->options;
  265. while(!result && ptr && *ptr) {
  266. const char *key = ptr;
  267. const char *value;
  268. while(*ptr && *ptr != '=')
  269. ptr++;
  270. value = ptr + 1;
  271. while(*ptr && *ptr != ';')
  272. ptr++;
  273. if(checkprefix("AUTH=", key))
  274. result = Curl_sasl_parse_url_auth_option(&li->sasl, value, ptr - value);
  275. else
  276. result = CURLE_SETOPT_OPTION_SYNTAX;
  277. if(*ptr == ';')
  278. ptr++;
  279. }
  280. return result == CURLE_URL_MALFORMAT? CURLE_SETOPT_OPTION_SYNTAX: result;
  281. }
  282. static CURLcode oldap_setup_connection(struct Curl_easy *data,
  283. struct connectdata *conn)
  284. {
  285. CURLcode result;
  286. LDAPURLDesc *lud;
  287. (void)conn;
  288. /* Early URL syntax check. */
  289. result = oldap_url_parse(data, &lud);
  290. ldap_free_urldesc(lud);
  291. return result;
  292. }
  293. /*
  294. * Get the SASL authentication challenge from the server credential buffer.
  295. */
  296. static CURLcode oldap_get_message(struct Curl_easy *data, struct bufref *out)
  297. {
  298. struct berval *servercred = data->conn->proto.ldapc->servercred;
  299. if(!servercred || !servercred->bv_val)
  300. return CURLE_WEIRD_SERVER_REPLY;
  301. Curl_bufref_set(out, servercred->bv_val, servercred->bv_len, NULL);
  302. return CURLE_OK;
  303. }
  304. /*
  305. * Sends an initial SASL bind request to the server.
  306. */
  307. static CURLcode oldap_perform_auth(struct Curl_easy *data, const char *mech,
  308. const struct bufref *initresp)
  309. {
  310. struct connectdata *conn = data->conn;
  311. struct ldapconninfo *li = conn->proto.ldapc;
  312. CURLcode result = CURLE_OK;
  313. struct berval cred;
  314. struct berval *pcred = &cred;
  315. int rc;
  316. cred.bv_val = (char *) Curl_bufref_ptr(initresp);
  317. cred.bv_len = Curl_bufref_len(initresp);
  318. if(!cred.bv_val)
  319. pcred = NULL;
  320. rc = ldap_sasl_bind(li->ld, NULL, mech, pcred, NULL, NULL, &li->msgid);
  321. if(rc != LDAP_SUCCESS)
  322. result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
  323. return result;
  324. }
  325. /*
  326. * Sends SASL continuation.
  327. */
  328. static CURLcode oldap_continue_auth(struct Curl_easy *data, const char *mech,
  329. const struct bufref *resp)
  330. {
  331. struct connectdata *conn = data->conn;
  332. struct ldapconninfo *li = conn->proto.ldapc;
  333. CURLcode result = CURLE_OK;
  334. struct berval cred;
  335. struct berval *pcred = &cred;
  336. int rc;
  337. cred.bv_val = (char *) Curl_bufref_ptr(resp);
  338. cred.bv_len = Curl_bufref_len(resp);
  339. if(!cred.bv_val)
  340. pcred = NULL;
  341. rc = ldap_sasl_bind(li->ld, NULL, mech, pcred, NULL, NULL, &li->msgid);
  342. if(rc != LDAP_SUCCESS)
  343. result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
  344. return result;
  345. }
  346. /*
  347. * Sends SASL bind cancellation.
  348. */
  349. static CURLcode oldap_cancel_auth(struct Curl_easy *data, const char *mech)
  350. {
  351. struct ldapconninfo *li = data->conn->proto.ldapc;
  352. CURLcode result = CURLE_OK;
  353. int rc = ldap_sasl_bind(li->ld, NULL, LDAP_SASL_NULL, NULL, NULL, NULL,
  354. &li->msgid);
  355. (void)mech;
  356. if(rc != LDAP_SUCCESS)
  357. result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
  358. return result;
  359. }
  360. /* Starts LDAP simple bind. */
  361. static CURLcode oldap_perform_bind(struct Curl_easy *data, ldapstate newstate)
  362. {
  363. CURLcode result = CURLE_OK;
  364. struct connectdata *conn = data->conn;
  365. struct ldapconninfo *li = conn->proto.ldapc;
  366. char *binddn = NULL;
  367. struct berval passwd;
  368. int rc;
  369. passwd.bv_val = NULL;
  370. passwd.bv_len = 0;
  371. if(data->state.aptr.user) {
  372. binddn = conn->user;
  373. passwd.bv_val = conn->passwd;
  374. passwd.bv_len = strlen(passwd.bv_val);
  375. }
  376. rc = ldap_sasl_bind(li->ld, binddn, LDAP_SASL_SIMPLE, &passwd,
  377. NULL, NULL, &li->msgid);
  378. if(rc == LDAP_SUCCESS)
  379. oldap_state(data, newstate);
  380. else
  381. result = oldap_map_error(rc,
  382. data->state.aptr.user?
  383. CURLE_LOGIN_DENIED: CURLE_LDAP_CANNOT_BIND);
  384. return result;
  385. }
  386. /* Query the supported SASL authentication mechanisms. */
  387. static CURLcode oldap_perform_mechs(struct Curl_easy *data)
  388. {
  389. CURLcode result = CURLE_OK;
  390. struct ldapconninfo *li = data->conn->proto.ldapc;
  391. int rc;
  392. static const char * const supportedSASLMechanisms[] = {
  393. "supportedSASLMechanisms",
  394. NULL
  395. };
  396. rc = ldap_search_ext(li->ld, "", LDAP_SCOPE_BASE, "(objectclass=*)",
  397. (char **) supportedSASLMechanisms, 0,
  398. NULL, NULL, NULL, 0, &li->msgid);
  399. if(rc == LDAP_SUCCESS)
  400. oldap_state(data, OLDAP_MECHS);
  401. else
  402. result = oldap_map_error(rc, CURLE_LOGIN_DENIED);
  403. return result;
  404. }
  405. /* Starts SASL bind. */
  406. static CURLcode oldap_perform_sasl(struct Curl_easy *data)
  407. {
  408. saslprogress progress = SASL_IDLE;
  409. struct ldapconninfo *li = data->conn->proto.ldapc;
  410. CURLcode result = Curl_sasl_start(&li->sasl, data, TRUE, &progress);
  411. oldap_state(data, OLDAP_SASL);
  412. if(!result && progress != SASL_INPROGRESS)
  413. result = CURLE_LOGIN_DENIED;
  414. return result;
  415. }
  416. #ifdef USE_SSL
  417. static Sockbuf_IO ldapsb_tls;
  418. static bool ssl_installed(struct connectdata *conn)
  419. {
  420. return conn->proto.ldapc->recv != NULL;
  421. }
  422. static CURLcode oldap_ssl_connect(struct Curl_easy *data, ldapstate newstate)
  423. {
  424. CURLcode result = CURLE_OK;
  425. struct connectdata *conn = data->conn;
  426. struct ldapconninfo *li = conn->proto.ldapc;
  427. bool ssldone = 0;
  428. result = Curl_conn_connect(data, FIRSTSOCKET, FALSE, &ssldone);
  429. if(!result) {
  430. oldap_state(data, newstate);
  431. if(ssldone) {
  432. Sockbuf *sb;
  433. /* Install the libcurl SSL handlers into the sockbuf. */
  434. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  435. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
  436. li->recv = conn->recv[FIRSTSOCKET];
  437. li->send = conn->send[FIRSTSOCKET];
  438. }
  439. }
  440. return result;
  441. }
  442. /* Send the STARTTLS request */
  443. static CURLcode oldap_perform_starttls(struct Curl_easy *data)
  444. {
  445. CURLcode result = CURLE_OK;
  446. struct ldapconninfo *li = data->conn->proto.ldapc;
  447. int rc = ldap_start_tls(li->ld, NULL, NULL, &li->msgid);
  448. if(rc == LDAP_SUCCESS)
  449. oldap_state(data, OLDAP_STARTTLS);
  450. else
  451. result = oldap_map_error(rc, CURLE_USE_SSL_FAILED);
  452. return result;
  453. }
  454. #endif
  455. static CURLcode oldap_connect(struct Curl_easy *data, bool *done)
  456. {
  457. struct connectdata *conn = data->conn;
  458. struct ldapconninfo *li;
  459. static const int version = LDAP_VERSION3;
  460. int rc;
  461. char *hosturl;
  462. #ifdef CURL_OPENLDAP_DEBUG
  463. static int do_trace = -1;
  464. #endif
  465. (void)done;
  466. DEBUGASSERT(!conn->proto.ldapc);
  467. li = calloc(1, sizeof(struct ldapconninfo));
  468. if(!li)
  469. return CURLE_OUT_OF_MEMORY;
  470. else {
  471. CURLcode result;
  472. li->proto = ldap_pvt_url_scheme2proto(data->state.up.scheme);
  473. conn->proto.ldapc = li;
  474. /* Initialize the SASL storage */
  475. Curl_sasl_init(&li->sasl, data, &saslldap);
  476. /* Clear the TLS upgraded flag */
  477. conn->bits.tls_upgraded = FALSE;
  478. result = oldap_parse_login_options(conn);
  479. if(result)
  480. return result;
  481. }
  482. hosturl = aprintf("ldap%s://%s:%d",
  483. conn->handler->flags & PROTOPT_SSL? "s": "",
  484. conn->host.name, conn->remote_port);
  485. if(!hosturl)
  486. return CURLE_OUT_OF_MEMORY;
  487. rc = ldap_init_fd(conn->sock[FIRSTSOCKET], li->proto, hosturl, &li->ld);
  488. if(rc) {
  489. failf(data, "LDAP local: Cannot connect to %s, %s",
  490. hosturl, ldap_err2string(rc));
  491. free(hosturl);
  492. return CURLE_COULDNT_CONNECT;
  493. }
  494. free(hosturl);
  495. #ifdef CURL_OPENLDAP_DEBUG
  496. if(do_trace < 0) {
  497. const char *env = getenv("CURL_OPENLDAP_TRACE");
  498. do_trace = (env && strtol(env, NULL, 10) > 0);
  499. }
  500. if(do_trace)
  501. ldap_set_option(li->ld, LDAP_OPT_DEBUG_LEVEL, &do_trace);
  502. #endif
  503. /* Try version 3 first. */
  504. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  505. /* Do not chase referrals. */
  506. ldap_set_option(li->ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
  507. #ifdef USE_SSL
  508. if(conn->handler->flags & PROTOPT_SSL)
  509. return oldap_ssl_connect(data, OLDAP_SSL);
  510. if(data->set.use_ssl) {
  511. CURLcode result = oldap_perform_starttls(data);
  512. if(!result || data->set.use_ssl != CURLUSESSL_TRY)
  513. return result;
  514. }
  515. #endif
  516. if(li->sasl.prefmech != SASL_AUTH_NONE)
  517. return oldap_perform_mechs(data);
  518. /* Force bind even if anonymous bind is not needed in protocol version 3
  519. to detect missing version 3 support. */
  520. return oldap_perform_bind(data, OLDAP_BIND);
  521. }
  522. /* Handle the supported SASL mechanisms query response */
  523. static CURLcode oldap_state_mechs_resp(struct Curl_easy *data,
  524. LDAPMessage *msg, int code)
  525. {
  526. struct connectdata *conn = data->conn;
  527. struct ldapconninfo *li = conn->proto.ldapc;
  528. int rc;
  529. BerElement *ber = NULL;
  530. CURLcode result = CURLE_OK;
  531. struct berval bv, *bvals;
  532. switch(ldap_msgtype(msg)) {
  533. case LDAP_RES_SEARCH_ENTRY:
  534. /* Got a list of supported SASL mechanisms. */
  535. if(code != LDAP_SUCCESS && code != LDAP_NO_RESULTS_RETURNED)
  536. return CURLE_LOGIN_DENIED;
  537. rc = ldap_get_dn_ber(li->ld, msg, &ber, &bv);
  538. if(rc < 0)
  539. return oldap_map_error(rc, CURLE_BAD_CONTENT_ENCODING);
  540. for(rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals);
  541. rc == LDAP_SUCCESS;
  542. rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals)) {
  543. int i;
  544. if(!bv.bv_val)
  545. break;
  546. if(bvals) {
  547. for(i = 0; bvals[i].bv_val; i++) {
  548. size_t llen;
  549. unsigned short mech = Curl_sasl_decode_mech((char *) bvals[i].bv_val,
  550. bvals[i].bv_len, &llen);
  551. if(bvals[i].bv_len == llen)
  552. li->sasl.authmechs |= mech;
  553. }
  554. ber_memfree(bvals);
  555. }
  556. }
  557. ber_free(ber, 0);
  558. break;
  559. case LDAP_RES_SEARCH_RESULT:
  560. switch(code) {
  561. case LDAP_SIZELIMIT_EXCEEDED:
  562. infof(data, "Too many authentication mechanisms\n");
  563. /* FALLTHROUGH */
  564. case LDAP_SUCCESS:
  565. case LDAP_NO_RESULTS_RETURNED:
  566. if(Curl_sasl_can_authenticate(&li->sasl, data))
  567. result = oldap_perform_sasl(data);
  568. else
  569. result = CURLE_LOGIN_DENIED;
  570. break;
  571. default:
  572. result = oldap_map_error(code, CURLE_LOGIN_DENIED);
  573. break;
  574. }
  575. break;
  576. default:
  577. break;
  578. }
  579. return result;
  580. }
  581. /* Handle a SASL bind response. */
  582. static CURLcode oldap_state_sasl_resp(struct Curl_easy *data,
  583. LDAPMessage *msg, int code)
  584. {
  585. struct connectdata *conn = data->conn;
  586. struct ldapconninfo *li = conn->proto.ldapc;
  587. CURLcode result = CURLE_OK;
  588. saslprogress progress;
  589. int rc;
  590. li->servercred = NULL;
  591. rc = ldap_parse_sasl_bind_result(li->ld, msg, &li->servercred, 0);
  592. if(rc != LDAP_SUCCESS) {
  593. failf(data, "LDAP local: sasl ldap_parse_result %s", ldap_err2string(rc));
  594. result = oldap_map_error(rc, CURLE_LOGIN_DENIED);
  595. }
  596. else {
  597. result = Curl_sasl_continue(&li->sasl, data, code, &progress);
  598. if(!result && progress != SASL_INPROGRESS)
  599. oldap_state(data, OLDAP_STOP);
  600. }
  601. if(li->servercred)
  602. ber_bvfree(li->servercred);
  603. return result;
  604. }
  605. /* Handle a simple bind response. */
  606. static CURLcode oldap_state_bind_resp(struct Curl_easy *data, LDAPMessage *msg,
  607. int code)
  608. {
  609. struct connectdata *conn = data->conn;
  610. struct ldapconninfo *li = conn->proto.ldapc;
  611. CURLcode result = CURLE_OK;
  612. struct berval *bv = NULL;
  613. int rc;
  614. if(code != LDAP_SUCCESS)
  615. return oldap_map_error(code, CURLE_LDAP_CANNOT_BIND);
  616. rc = ldap_parse_sasl_bind_result(li->ld, msg, &bv, 0);
  617. if(rc != LDAP_SUCCESS) {
  618. failf(data, "LDAP local: bind ldap_parse_sasl_bind_result %s",
  619. ldap_err2string(rc));
  620. result = oldap_map_error(rc, CURLE_LDAP_CANNOT_BIND);
  621. }
  622. else
  623. oldap_state(data, OLDAP_STOP);
  624. if(bv)
  625. ber_bvfree(bv);
  626. return result;
  627. }
  628. static CURLcode oldap_connecting(struct Curl_easy *data, bool *done)
  629. {
  630. CURLcode result = CURLE_OK;
  631. struct connectdata *conn = data->conn;
  632. struct ldapconninfo *li = conn->proto.ldapc;
  633. LDAPMessage *msg = NULL;
  634. struct timeval tv = {0, 0};
  635. int code = LDAP_SUCCESS;
  636. int rc;
  637. if(li->state != OLDAP_SSL && li->state != OLDAP_TLS) {
  638. /* Get response to last command. */
  639. rc = ldap_result(li->ld, li->msgid, LDAP_MSG_ONE, &tv, &msg);
  640. switch(rc) {
  641. case 0: /* Timed out. */
  642. return CURLE_OK;
  643. case LDAP_RES_SEARCH_ENTRY:
  644. case LDAP_RES_SEARCH_REFERENCE:
  645. break;
  646. default:
  647. li->msgid = 0; /* Nothing to abandon upon error. */
  648. if(rc < 0) {
  649. failf(data, "LDAP local: connecting ldap_result %s",
  650. ldap_err2string(rc));
  651. return oldap_map_error(rc, CURLE_COULDNT_CONNECT);
  652. }
  653. break;
  654. }
  655. /* Get error code from message. */
  656. rc = ldap_parse_result(li->ld, msg, &code, NULL, NULL, NULL, NULL, 0);
  657. if(rc)
  658. code = rc;
  659. else {
  660. /* store the latest code for later retrieval */
  661. data->info.httpcode = code;
  662. }
  663. /* If protocol version 3 is not supported, fallback to version 2. */
  664. if(code == LDAP_PROTOCOL_ERROR && li->state != OLDAP_BINDV2 &&
  665. #ifdef USE_SSL
  666. (ssl_installed(conn) || data->set.use_ssl <= CURLUSESSL_TRY) &&
  667. #endif
  668. li->sasl.prefmech == SASL_AUTH_NONE) {
  669. static const int version = LDAP_VERSION2;
  670. ldap_set_option(li->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  671. ldap_msgfree(msg);
  672. return oldap_perform_bind(data, OLDAP_BINDV2);
  673. }
  674. }
  675. /* Handle response message according to current state. */
  676. switch(li->state) {
  677. #ifdef USE_SSL
  678. case OLDAP_SSL:
  679. result = oldap_ssl_connect(data, OLDAP_SSL);
  680. if(!result && ssl_installed(conn)) {
  681. if(li->sasl.prefmech != SASL_AUTH_NONE)
  682. result = oldap_perform_mechs(data);
  683. else
  684. result = oldap_perform_bind(data, OLDAP_BIND);
  685. }
  686. break;
  687. case OLDAP_STARTTLS:
  688. if(code != LDAP_SUCCESS) {
  689. if(data->set.use_ssl != CURLUSESSL_TRY)
  690. result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
  691. else if(li->sasl.prefmech != SASL_AUTH_NONE)
  692. result = oldap_perform_mechs(data);
  693. else
  694. result = oldap_perform_bind(data, OLDAP_BIND);
  695. break;
  696. }
  697. /* FALLTHROUGH */
  698. case OLDAP_TLS:
  699. result = oldap_ssl_connect(data, OLDAP_TLS);
  700. if(result && data->set.use_ssl != CURLUSESSL_TRY)
  701. result = oldap_map_error(code, CURLE_USE_SSL_FAILED);
  702. else if(ssl_installed(conn)) {
  703. conn->bits.tls_upgraded = TRUE;
  704. if(li->sasl.prefmech != SASL_AUTH_NONE)
  705. result = oldap_perform_mechs(data);
  706. else if(data->state.aptr.user)
  707. result = oldap_perform_bind(data, OLDAP_BIND);
  708. else {
  709. /* Version 3 supported: no bind required */
  710. oldap_state(data, OLDAP_STOP);
  711. result = CURLE_OK;
  712. }
  713. }
  714. break;
  715. #endif
  716. case OLDAP_MECHS:
  717. result = oldap_state_mechs_resp(data, msg, code);
  718. break;
  719. case OLDAP_SASL:
  720. result = oldap_state_sasl_resp(data, msg, code);
  721. break;
  722. case OLDAP_BIND:
  723. case OLDAP_BINDV2:
  724. result = oldap_state_bind_resp(data, msg, code);
  725. break;
  726. default:
  727. /* internal error */
  728. result = CURLE_COULDNT_CONNECT;
  729. break;
  730. }
  731. ldap_msgfree(msg);
  732. *done = li->state == OLDAP_STOP;
  733. if(*done)
  734. conn->recv[FIRSTSOCKET] = oldap_recv;
  735. if(result && li->msgid) {
  736. ldap_abandon_ext(li->ld, li->msgid, NULL, NULL);
  737. li->msgid = 0;
  738. }
  739. return result;
  740. }
  741. static CURLcode oldap_disconnect(struct Curl_easy *data,
  742. struct connectdata *conn,
  743. bool dead_connection)
  744. {
  745. struct ldapconninfo *li = conn->proto.ldapc;
  746. (void) dead_connection;
  747. #ifndef USE_SSL
  748. (void)data;
  749. #endif
  750. if(li) {
  751. if(li->ld) {
  752. #ifdef USE_SSL
  753. if(ssl_installed(conn)) {
  754. Sockbuf *sb;
  755. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  756. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
  757. }
  758. #endif
  759. ldap_unbind_ext(li->ld, NULL, NULL);
  760. li->ld = NULL;
  761. }
  762. Curl_sasl_cleanup(conn, li->sasl.authused);
  763. conn->proto.ldapc = NULL;
  764. free(li);
  765. }
  766. return CURLE_OK;
  767. }
  768. static CURLcode oldap_do(struct Curl_easy *data, bool *done)
  769. {
  770. struct connectdata *conn = data->conn;
  771. struct ldapconninfo *li = conn->proto.ldapc;
  772. struct ldapreqinfo *lr;
  773. CURLcode result;
  774. int rc;
  775. LDAPURLDesc *lud;
  776. int msgid;
  777. connkeep(conn, "OpenLDAP do");
  778. infof(data, "LDAP local: %s", data->state.url);
  779. result = oldap_url_parse(data, &lud);
  780. if(!result) {
  781. Sockbuf *sb;
  782. /* re-install the libcurl SSL handlers into the sockbuf. */
  783. ldap_get_option(li->ld, LDAP_OPT_SOCKBUF, &sb);
  784. ber_sockbuf_add_io(sb, &ldapsb_tls, LBER_SBIOD_LEVEL_TRANSPORT, data);
  785. rc = ldap_search_ext(li->ld, lud->lud_dn, lud->lud_scope,
  786. lud->lud_filter, lud->lud_attrs, 0,
  787. NULL, NULL, NULL, 0, &msgid);
  788. ldap_free_urldesc(lud);
  789. if(rc != LDAP_SUCCESS) {
  790. failf(data, "LDAP local: ldap_search_ext %s", ldap_err2string(rc));
  791. result = CURLE_LDAP_SEARCH_FAILED;
  792. }
  793. else {
  794. lr = calloc(1, sizeof(struct ldapreqinfo));
  795. if(!lr) {
  796. ldap_abandon_ext(li->ld, msgid, NULL, NULL);
  797. result = CURLE_OUT_OF_MEMORY;
  798. }
  799. else {
  800. lr->msgid = msgid;
  801. data->req.p.ldap = lr;
  802. Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
  803. *done = TRUE;
  804. }
  805. }
  806. }
  807. return result;
  808. }
  809. static CURLcode oldap_done(struct Curl_easy *data, CURLcode res,
  810. bool premature)
  811. {
  812. struct connectdata *conn = data->conn;
  813. struct ldapreqinfo *lr = data->req.p.ldap;
  814. (void)res;
  815. (void)premature;
  816. if(lr) {
  817. /* if there was a search in progress, abandon it */
  818. if(lr->msgid) {
  819. struct ldapconninfo *li = conn->proto.ldapc;
  820. ldap_abandon_ext(li->ld, lr->msgid, NULL, NULL);
  821. lr->msgid = 0;
  822. }
  823. data->req.p.ldap = NULL;
  824. free(lr);
  825. }
  826. return CURLE_OK;
  827. }
  828. static CURLcode client_write(struct Curl_easy *data,
  829. const char *prefix, size_t plen,
  830. const char *value, size_t len,
  831. const char *suffix, size_t slen)
  832. {
  833. CURLcode result = CURLE_OK;
  834. if(prefix) {
  835. /* If we have a zero-length value and the prefix ends with a space
  836. separator, drop the latter. */
  837. if(!len && plen && prefix[plen - 1] == ' ')
  838. plen--;
  839. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) prefix, plen);
  840. }
  841. if(!result && value) {
  842. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) value, len);
  843. }
  844. if(!result && suffix) {
  845. result = Curl_client_write(data, CLIENTWRITE_BODY, (char *) suffix, slen);
  846. }
  847. return result;
  848. }
  849. static ssize_t oldap_recv(struct Curl_easy *data, int sockindex, char *buf,
  850. size_t len, CURLcode *err)
  851. {
  852. struct connectdata *conn = data->conn;
  853. struct ldapconninfo *li = conn->proto.ldapc;
  854. struct ldapreqinfo *lr = data->req.p.ldap;
  855. int rc;
  856. LDAPMessage *msg = NULL;
  857. BerElement *ber = NULL;
  858. struct timeval tv = {0, 0};
  859. struct berval bv, *bvals;
  860. int binary = 0;
  861. CURLcode result = CURLE_AGAIN;
  862. int code;
  863. char *info = NULL;
  864. (void)len;
  865. (void)buf;
  866. (void)sockindex;
  867. rc = ldap_result(li->ld, lr->msgid, LDAP_MSG_ONE, &tv, &msg);
  868. if(rc < 0) {
  869. failf(data, "LDAP local: search ldap_result %s", ldap_err2string(rc));
  870. result = CURLE_RECV_ERROR;
  871. }
  872. *err = result;
  873. /* error or timed out */
  874. if(!msg)
  875. return -1;
  876. result = CURLE_OK;
  877. switch(ldap_msgtype(msg)) {
  878. case LDAP_RES_SEARCH_RESULT:
  879. lr->msgid = 0;
  880. rc = ldap_parse_result(li->ld, msg, &code, NULL, &info, NULL, NULL, 0);
  881. if(rc) {
  882. failf(data, "LDAP local: search ldap_parse_result %s",
  883. ldap_err2string(rc));
  884. result = CURLE_LDAP_SEARCH_FAILED;
  885. break;
  886. }
  887. /* store the latest code for later retrieval */
  888. data->info.httpcode = code;
  889. switch(code) {
  890. case LDAP_SIZELIMIT_EXCEEDED:
  891. infof(data, "There are more than %d entries", lr->nument);
  892. /* FALLTHROUGH */
  893. case LDAP_SUCCESS:
  894. data->req.size = data->req.bytecount;
  895. break;
  896. default:
  897. failf(data, "LDAP remote: search failed %s %s", ldap_err2string(code),
  898. info ? info : "");
  899. result = CURLE_LDAP_SEARCH_FAILED;
  900. break;
  901. }
  902. if(info)
  903. ldap_memfree(info);
  904. break;
  905. case LDAP_RES_SEARCH_ENTRY:
  906. lr->nument++;
  907. rc = ldap_get_dn_ber(li->ld, msg, &ber, &bv);
  908. if(rc < 0) {
  909. result = CURLE_RECV_ERROR;
  910. break;
  911. }
  912. result = client_write(data, STRCONST("DN: "), bv.bv_val, bv.bv_len,
  913. STRCONST("\n"));
  914. if(result)
  915. break;
  916. for(rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals);
  917. rc == LDAP_SUCCESS;
  918. rc = ldap_get_attribute_ber(li->ld, msg, ber, &bv, &bvals)) {
  919. int i;
  920. if(!bv.bv_val)
  921. break;
  922. if(!bvals) {
  923. result = client_write(data, STRCONST("\t"), bv.bv_val, bv.bv_len,
  924. STRCONST(":\n"));
  925. if(result)
  926. break;
  927. continue;
  928. }
  929. binary = bv.bv_len > 7 &&
  930. !strncmp(bv.bv_val + bv.bv_len - 7, ";binary", 7);
  931. for(i = 0; bvals[i].bv_val != NULL; i++) {
  932. int binval = 0;
  933. result = client_write(data, STRCONST("\t"), bv.bv_val, bv.bv_len,
  934. STRCONST(":"));
  935. if(result)
  936. break;
  937. if(!binary) {
  938. /* check for leading or trailing whitespace */
  939. if(ISBLANK(bvals[i].bv_val[0]) ||
  940. ISBLANK(bvals[i].bv_val[bvals[i].bv_len - 1]))
  941. binval = 1;
  942. else {
  943. /* check for unprintable characters */
  944. unsigned int j;
  945. for(j = 0; j < bvals[i].bv_len; j++)
  946. if(!ISPRINT(bvals[i].bv_val[j])) {
  947. binval = 1;
  948. break;
  949. }
  950. }
  951. }
  952. if(binary || binval) {
  953. char *val_b64 = NULL;
  954. size_t val_b64_sz = 0;
  955. /* Binary value, encode to base64. */
  956. if(bvals[i].bv_len)
  957. result = Curl_base64_encode(bvals[i].bv_val, bvals[i].bv_len,
  958. &val_b64, &val_b64_sz);
  959. if(!result)
  960. result = client_write(data, STRCONST(": "), val_b64, val_b64_sz,
  961. STRCONST("\n"));
  962. free(val_b64);
  963. }
  964. else
  965. result = client_write(data, STRCONST(" "),
  966. bvals[i].bv_val, bvals[i].bv_len,
  967. STRCONST("\n"));
  968. if(result)
  969. break;
  970. }
  971. ber_memfree(bvals);
  972. bvals = NULL;
  973. if(!result)
  974. result = client_write(data, STRCONST("\n"), NULL, 0, NULL, 0);
  975. if(result)
  976. break;
  977. }
  978. ber_free(ber, 0);
  979. if(!result)
  980. result = client_write(data, STRCONST("\n"), NULL, 0, NULL, 0);
  981. if(!result)
  982. result = CURLE_AGAIN;
  983. break;
  984. }
  985. ldap_msgfree(msg);
  986. *err = result;
  987. return result? -1: 0;
  988. }
  989. #ifdef USE_SSL
  990. static int
  991. ldapsb_tls_setup(Sockbuf_IO_Desc *sbiod, void *arg)
  992. {
  993. sbiod->sbiod_pvt = arg;
  994. return 0;
  995. }
  996. static int
  997. ldapsb_tls_remove(Sockbuf_IO_Desc *sbiod)
  998. {
  999. sbiod->sbiod_pvt = NULL;
  1000. return 0;
  1001. }
  1002. /* We don't need to do anything because libcurl does it already */
  1003. static int
  1004. ldapsb_tls_close(Sockbuf_IO_Desc *sbiod)
  1005. {
  1006. (void)sbiod;
  1007. return 0;
  1008. }
  1009. static int
  1010. ldapsb_tls_ctrl(Sockbuf_IO_Desc *sbiod, int opt, void *arg)
  1011. {
  1012. (void)arg;
  1013. if(opt == LBER_SB_OPT_DATA_READY) {
  1014. struct Curl_easy *data = sbiod->sbiod_pvt;
  1015. return Curl_conn_data_pending(data, FIRSTSOCKET);
  1016. }
  1017. return 0;
  1018. }
  1019. static ber_slen_t
  1020. ldapsb_tls_read(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  1021. {
  1022. struct Curl_easy *data = sbiod->sbiod_pvt;
  1023. ber_slen_t ret = 0;
  1024. if(data) {
  1025. struct connectdata *conn = data->conn;
  1026. if(conn) {
  1027. struct ldapconninfo *li = conn->proto.ldapc;
  1028. CURLcode err = CURLE_RECV_ERROR;
  1029. ret = (li->recv)(data, FIRSTSOCKET, buf, len, &err);
  1030. if(ret < 0 && err == CURLE_AGAIN) {
  1031. SET_SOCKERRNO(EWOULDBLOCK);
  1032. }
  1033. }
  1034. }
  1035. return ret;
  1036. }
  1037. static ber_slen_t
  1038. ldapsb_tls_write(Sockbuf_IO_Desc *sbiod, void *buf, ber_len_t len)
  1039. {
  1040. struct Curl_easy *data = sbiod->sbiod_pvt;
  1041. ber_slen_t ret = 0;
  1042. if(data) {
  1043. struct connectdata *conn = data->conn;
  1044. if(conn) {
  1045. struct ldapconninfo *li = conn->proto.ldapc;
  1046. CURLcode err = CURLE_SEND_ERROR;
  1047. ret = (li->send)(data, FIRSTSOCKET, buf, len, &err);
  1048. if(ret < 0 && err == CURLE_AGAIN) {
  1049. SET_SOCKERRNO(EWOULDBLOCK);
  1050. }
  1051. }
  1052. }
  1053. return ret;
  1054. }
  1055. static Sockbuf_IO ldapsb_tls =
  1056. {
  1057. ldapsb_tls_setup,
  1058. ldapsb_tls_remove,
  1059. ldapsb_tls_ctrl,
  1060. ldapsb_tls_read,
  1061. ldapsb_tls_write,
  1062. ldapsb_tls_close
  1063. };
  1064. #endif /* USE_SSL */
  1065. #endif /* !CURL_DISABLE_LDAP && USE_OPENLDAP */