2
0

krb5.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
  2. *
  3. * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
  4. * (Royal Institute of Technology, Stockholm, Sweden).
  5. * Copyright (C) Daniel Stenberg
  6. * All rights reserved.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. Neither the name of the Institute nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  26. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35. * SUCH DAMAGE. */
  36. #include "curl_setup.h"
  37. #if defined(HAVE_GSSAPI) && !defined(CURL_DISABLE_FTP)
  38. #ifdef HAVE_NETDB_H
  39. #include <netdb.h>
  40. #endif
  41. #ifdef HAVE_ARPA_INET_H
  42. #include <arpa/inet.h>
  43. #endif
  44. #include "urldata.h"
  45. #include "cfilters.h"
  46. #include "cf-socket.h"
  47. #include "curl_base64.h"
  48. #include "ftp.h"
  49. #include "curl_gssapi.h"
  50. #include "sendf.h"
  51. #include "curl_krb5.h"
  52. #include "warnless.h"
  53. #include "strcase.h"
  54. #include "strdup.h"
  55. /* The last 3 #include files should be in this order */
  56. #include "curl_printf.h"
  57. #include "curl_memory.h"
  58. #include "memdebug.h"
  59. static CURLcode ftpsend(struct Curl_easy *data, struct connectdata *conn,
  60. const char *cmd)
  61. {
  62. ssize_t bytes_written;
  63. #define SBUF_SIZE 1024
  64. char s[SBUF_SIZE];
  65. size_t write_len;
  66. char *sptr = s;
  67. CURLcode result = CURLE_OK;
  68. #ifdef HAVE_GSSAPI
  69. enum protection_level data_sec = conn->data_prot;
  70. #endif
  71. if(!cmd)
  72. return CURLE_BAD_FUNCTION_ARGUMENT;
  73. write_len = strlen(cmd);
  74. if(!write_len || write_len > (sizeof(s) -3))
  75. return CURLE_BAD_FUNCTION_ARGUMENT;
  76. memcpy(&s, cmd, write_len);
  77. strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
  78. write_len += 2;
  79. bytes_written = 0;
  80. for(;;) {
  81. #ifdef HAVE_GSSAPI
  82. conn->data_prot = PROT_CMD;
  83. #endif
  84. result = Curl_write(data, conn->sock[FIRSTSOCKET], sptr, write_len,
  85. &bytes_written);
  86. #ifdef HAVE_GSSAPI
  87. DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
  88. conn->data_prot = data_sec;
  89. #endif
  90. if(result)
  91. break;
  92. Curl_debug(data, CURLINFO_HEADER_OUT, sptr, (size_t)bytes_written);
  93. if(bytes_written != (ssize_t)write_len) {
  94. write_len -= bytes_written;
  95. sptr += bytes_written;
  96. }
  97. else
  98. break;
  99. }
  100. return result;
  101. }
  102. static int
  103. krb5_init(void *app_data)
  104. {
  105. gss_ctx_id_t *context = app_data;
  106. /* Make sure our context is initialized for krb5_end. */
  107. *context = GSS_C_NO_CONTEXT;
  108. return 0;
  109. }
  110. static int
  111. krb5_check_prot(void *app_data, int level)
  112. {
  113. (void)app_data; /* unused */
  114. if(level == PROT_CONFIDENTIAL)
  115. return -1;
  116. return 0;
  117. }
  118. static int
  119. krb5_decode(void *app_data, void *buf, int len,
  120. int level UNUSED_PARAM,
  121. struct connectdata *conn UNUSED_PARAM)
  122. {
  123. gss_ctx_id_t *context = app_data;
  124. OM_uint32 maj, min;
  125. gss_buffer_desc enc, dec;
  126. (void)level;
  127. (void)conn;
  128. enc.value = buf;
  129. enc.length = len;
  130. maj = gss_unwrap(&min, *context, &enc, &dec, NULL, NULL);
  131. if(maj != GSS_S_COMPLETE)
  132. return -1;
  133. memcpy(buf, dec.value, dec.length);
  134. len = curlx_uztosi(dec.length);
  135. gss_release_buffer(&min, &dec);
  136. return len;
  137. }
  138. static int
  139. krb5_encode(void *app_data, const void *from, int length, int level, void **to)
  140. {
  141. gss_ctx_id_t *context = app_data;
  142. gss_buffer_desc dec, enc;
  143. OM_uint32 maj, min;
  144. int state;
  145. int len;
  146. /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
  147. * libraries modify the input buffer in gss_wrap()
  148. */
  149. dec.value = (void *)from;
  150. dec.length = length;
  151. maj = gss_wrap(&min, *context,
  152. level == PROT_PRIVATE,
  153. GSS_C_QOP_DEFAULT,
  154. &dec, &state, &enc);
  155. if(maj != GSS_S_COMPLETE)
  156. return -1;
  157. /* malloc a new buffer, in case gss_release_buffer doesn't work as
  158. expected */
  159. *to = malloc(enc.length);
  160. if(!*to)
  161. return -1;
  162. memcpy(*to, enc.value, enc.length);
  163. len = curlx_uztosi(enc.length);
  164. gss_release_buffer(&min, &enc);
  165. return len;
  166. }
  167. static int
  168. krb5_auth(void *app_data, struct Curl_easy *data, struct connectdata *conn)
  169. {
  170. int ret = AUTH_OK;
  171. char *p;
  172. const char *host = conn->host.name;
  173. ssize_t nread;
  174. curl_socklen_t l = sizeof(conn->local_addr);
  175. CURLcode result;
  176. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  177. data->set.str[STRING_SERVICE_NAME] :
  178. "ftp";
  179. const char *srv_host = "host";
  180. gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
  181. OM_uint32 maj, min;
  182. gss_name_t gssname;
  183. gss_ctx_id_t *context = app_data;
  184. struct gss_channel_bindings_struct chan;
  185. size_t base64_sz = 0;
  186. struct sockaddr_in *remote_addr =
  187. (struct sockaddr_in *)(void *)&conn->remote_addr->sa_addr;
  188. char *stringp;
  189. if(getsockname(conn->sock[FIRSTSOCKET],
  190. (struct sockaddr *)&conn->local_addr, &l) < 0)
  191. perror("getsockname()");
  192. chan.initiator_addrtype = GSS_C_AF_INET;
  193. chan.initiator_address.length = l - 4;
  194. chan.initiator_address.value = &conn->local_addr.sin_addr.s_addr;
  195. chan.acceptor_addrtype = GSS_C_AF_INET;
  196. chan.acceptor_address.length = l - 4;
  197. chan.acceptor_address.value = &remote_addr->sin_addr.s_addr;
  198. chan.application_data.length = 0;
  199. chan.application_data.value = NULL;
  200. /* this loop will execute twice (once for service, once for host) */
  201. for(;;) {
  202. /* this really shouldn't be repeated here, but can't help it */
  203. if(service == srv_host) {
  204. result = ftpsend(data, conn, "AUTH GSSAPI");
  205. if(result)
  206. return -2;
  207. if(Curl_GetFTPResponse(data, &nread, NULL))
  208. return -1;
  209. if(data->state.buffer[0] != '3')
  210. return -1;
  211. }
  212. stringp = aprintf("%s@%s", service, host);
  213. if(!stringp)
  214. return -2;
  215. input_buffer.value = stringp;
  216. input_buffer.length = strlen(stringp);
  217. maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
  218. &gssname);
  219. free(stringp);
  220. if(maj != GSS_S_COMPLETE) {
  221. gss_release_name(&min, &gssname);
  222. if(service == srv_host) {
  223. failf(data, "Error importing service name %s@%s", service, host);
  224. return AUTH_ERROR;
  225. }
  226. service = srv_host;
  227. continue;
  228. }
  229. /* We pass NULL as |output_name_type| to avoid a leak. */
  230. gss_display_name(&min, gssname, &output_buffer, NULL);
  231. infof(data, "Trying against %s", (char *)output_buffer.value);
  232. gssresp = GSS_C_NO_BUFFER;
  233. *context = GSS_C_NO_CONTEXT;
  234. do {
  235. /* Release the buffer at each iteration to avoid leaking: the first time
  236. we are releasing the memory from gss_display_name. The last item is
  237. taken care by a final gss_release_buffer. */
  238. gss_release_buffer(&min, &output_buffer);
  239. ret = AUTH_OK;
  240. maj = Curl_gss_init_sec_context(data,
  241. &min,
  242. context,
  243. gssname,
  244. &Curl_krb5_mech_oid,
  245. &chan,
  246. gssresp,
  247. &output_buffer,
  248. TRUE,
  249. NULL);
  250. if(gssresp) {
  251. free(_gssresp.value);
  252. gssresp = NULL;
  253. }
  254. if(GSS_ERROR(maj)) {
  255. infof(data, "Error creating security context");
  256. ret = AUTH_ERROR;
  257. break;
  258. }
  259. if(output_buffer.length) {
  260. char *cmd;
  261. result = Curl_base64_encode((char *)output_buffer.value,
  262. output_buffer.length, &p, &base64_sz);
  263. if(result) {
  264. infof(data, "base64-encoding: %s", curl_easy_strerror(result));
  265. ret = AUTH_ERROR;
  266. break;
  267. }
  268. cmd = aprintf("ADAT %s", p);
  269. if(cmd)
  270. result = ftpsend(data, conn, cmd);
  271. else
  272. result = CURLE_OUT_OF_MEMORY;
  273. free(p);
  274. free(cmd);
  275. if(result) {
  276. ret = -2;
  277. break;
  278. }
  279. if(Curl_GetFTPResponse(data, &nread, NULL)) {
  280. ret = -1;
  281. break;
  282. }
  283. if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
  284. infof(data, "Server didn't accept auth data");
  285. ret = AUTH_ERROR;
  286. break;
  287. }
  288. _gssresp.value = NULL; /* make sure it is initialized */
  289. p = data->state.buffer + 4;
  290. p = strstr(p, "ADAT=");
  291. if(p) {
  292. result = Curl_base64_decode(p + 5,
  293. (unsigned char **)&_gssresp.value,
  294. &_gssresp.length);
  295. if(result) {
  296. failf(data, "base64-decoding: %s", curl_easy_strerror(result));
  297. ret = AUTH_CONTINUE;
  298. break;
  299. }
  300. }
  301. gssresp = &_gssresp;
  302. }
  303. } while(maj == GSS_S_CONTINUE_NEEDED);
  304. gss_release_name(&min, &gssname);
  305. gss_release_buffer(&min, &output_buffer);
  306. if(gssresp)
  307. free(_gssresp.value);
  308. if(ret == AUTH_OK || service == srv_host)
  309. return ret;
  310. service = srv_host;
  311. }
  312. return ret;
  313. }
  314. static void krb5_end(void *app_data)
  315. {
  316. OM_uint32 min;
  317. gss_ctx_id_t *context = app_data;
  318. if(*context != GSS_C_NO_CONTEXT) {
  319. OM_uint32 maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
  320. (void)maj;
  321. DEBUGASSERT(maj == GSS_S_COMPLETE);
  322. }
  323. }
  324. static const struct Curl_sec_client_mech Curl_krb5_client_mech = {
  325. "GSSAPI",
  326. sizeof(gss_ctx_id_t),
  327. krb5_init,
  328. krb5_auth,
  329. krb5_end,
  330. krb5_check_prot,
  331. krb5_encode,
  332. krb5_decode
  333. };
  334. static const struct {
  335. enum protection_level level;
  336. const char *name;
  337. } level_names[] = {
  338. { PROT_CLEAR, "clear" },
  339. { PROT_SAFE, "safe" },
  340. { PROT_CONFIDENTIAL, "confidential" },
  341. { PROT_PRIVATE, "private" }
  342. };
  343. static enum protection_level
  344. name_to_level(const char *name)
  345. {
  346. int i;
  347. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  348. if(curl_strequal(name, level_names[i].name))
  349. return level_names[i].level;
  350. return PROT_NONE;
  351. }
  352. /* Convert a protocol |level| to its char representation.
  353. We take an int to catch programming mistakes. */
  354. static char level_to_char(int level)
  355. {
  356. switch(level) {
  357. case PROT_CLEAR:
  358. return 'C';
  359. case PROT_SAFE:
  360. return 'S';
  361. case PROT_CONFIDENTIAL:
  362. return 'E';
  363. case PROT_PRIVATE:
  364. return 'P';
  365. case PROT_CMD:
  366. /* Fall through */
  367. default:
  368. /* Those 2 cases should not be reached! */
  369. break;
  370. }
  371. DEBUGASSERT(0);
  372. /* Default to the most secure alternative. */
  373. return 'P';
  374. }
  375. /* Send an FTP command defined by |message| and the optional arguments. The
  376. function returns the ftp_code. If an error occurs, -1 is returned. */
  377. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  378. {
  379. int ftp_code;
  380. ssize_t nread = 0;
  381. va_list args;
  382. char print_buffer[50];
  383. va_start(args, message);
  384. mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
  385. va_end(args);
  386. if(ftpsend(data, data->conn, print_buffer)) {
  387. ftp_code = -1;
  388. }
  389. else {
  390. if(Curl_GetFTPResponse(data, &nread, &ftp_code))
  391. ftp_code = -1;
  392. }
  393. (void)nread; /* Unused */
  394. return ftp_code;
  395. }
  396. /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
  397. saying whether an error occurred or CURLE_OK if |len| was read. */
  398. static CURLcode
  399. socket_read(struct Curl_easy *data, int sockindex, void *to, size_t len)
  400. {
  401. char *to_p = to;
  402. CURLcode result;
  403. ssize_t nread = 0;
  404. while(len > 0) {
  405. nread = Curl_conn_recv(data, sockindex, to_p, len, &result);
  406. if(nread > 0) {
  407. len -= nread;
  408. to_p += nread;
  409. }
  410. else {
  411. if(result == CURLE_AGAIN)
  412. continue;
  413. return result;
  414. }
  415. }
  416. return CURLE_OK;
  417. }
  418. /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
  419. CURLcode saying whether an error occurred or CURLE_OK if |len| was
  420. written. */
  421. static CURLcode
  422. socket_write(struct Curl_easy *data, int sockindex, const void *to,
  423. size_t len)
  424. {
  425. const char *to_p = to;
  426. CURLcode result;
  427. ssize_t written;
  428. while(len > 0) {
  429. written = Curl_conn_send(data, sockindex, to_p, len, &result);
  430. if(written > 0) {
  431. len -= written;
  432. to_p += written;
  433. }
  434. else {
  435. if(result == CURLE_AGAIN)
  436. continue;
  437. return result;
  438. }
  439. }
  440. return CURLE_OK;
  441. }
  442. static CURLcode read_data(struct Curl_easy *data, int sockindex,
  443. struct krb5buffer *buf)
  444. {
  445. struct connectdata *conn = data->conn;
  446. int len;
  447. CURLcode result;
  448. int nread;
  449. result = socket_read(data, sockindex, &len, sizeof(len));
  450. if(result)
  451. return result;
  452. if(len) {
  453. /* only realloc if there was a length */
  454. len = ntohl(len);
  455. if(len > CURL_MAX_INPUT_LENGTH)
  456. len = 0;
  457. else
  458. buf->data = Curl_saferealloc(buf->data, len);
  459. }
  460. if(!len || !buf->data)
  461. return CURLE_OUT_OF_MEMORY;
  462. result = socket_read(data, sockindex, buf->data, len);
  463. if(result)
  464. return result;
  465. nread = conn->mech->decode(conn->app_data, buf->data, len,
  466. conn->data_prot, conn);
  467. if(nread < 0)
  468. return CURLE_RECV_ERROR;
  469. buf->size = (size_t)nread;
  470. buf->index = 0;
  471. return CURLE_OK;
  472. }
  473. static size_t
  474. buffer_read(struct krb5buffer *buf, void *data, size_t len)
  475. {
  476. if(buf->size - buf->index < len)
  477. len = buf->size - buf->index;
  478. memcpy(data, (char *)buf->data + buf->index, len);
  479. buf->index += len;
  480. return len;
  481. }
  482. /* Matches Curl_recv signature */
  483. static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
  484. char *buffer, size_t len, CURLcode *err)
  485. {
  486. size_t bytes_read;
  487. size_t total_read = 0;
  488. struct connectdata *conn = data->conn;
  489. *err = CURLE_OK;
  490. /* Handle clear text response. */
  491. if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
  492. return Curl_conn_recv(data, sockindex, buffer, len, err);
  493. if(conn->in_buffer.eof_flag) {
  494. conn->in_buffer.eof_flag = 0;
  495. return 0;
  496. }
  497. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  498. len -= bytes_read;
  499. total_read += bytes_read;
  500. buffer += bytes_read;
  501. while(len > 0) {
  502. if(read_data(data, sockindex, &conn->in_buffer))
  503. return -1;
  504. if(conn->in_buffer.size == 0) {
  505. if(bytes_read > 0)
  506. conn->in_buffer.eof_flag = 1;
  507. return bytes_read;
  508. }
  509. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  510. len -= bytes_read;
  511. total_read += bytes_read;
  512. buffer += bytes_read;
  513. }
  514. return total_read;
  515. }
  516. /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
  517. and negotiating with the server. |from| can be NULL. */
  518. static void do_sec_send(struct Curl_easy *data, struct connectdata *conn,
  519. curl_socket_t fd, const char *from, int length)
  520. {
  521. int bytes, htonl_bytes; /* 32-bit integers for htonl */
  522. char *buffer = NULL;
  523. char *cmd_buffer;
  524. size_t cmd_size = 0;
  525. CURLcode error;
  526. enum protection_level prot_level = conn->data_prot;
  527. bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
  528. DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
  529. if(iscmd) {
  530. if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
  531. prot_level = PROT_PRIVATE;
  532. else
  533. prot_level = conn->command_prot;
  534. }
  535. bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
  536. (void **)&buffer);
  537. if(!buffer || bytes <= 0)
  538. return; /* error */
  539. if(iscmd) {
  540. error = Curl_base64_encode(buffer, curlx_sitouz(bytes),
  541. &cmd_buffer, &cmd_size);
  542. if(error) {
  543. free(buffer);
  544. return; /* error */
  545. }
  546. if(cmd_size > 0) {
  547. static const char *enc = "ENC ";
  548. static const char *mic = "MIC ";
  549. if(prot_level == PROT_PRIVATE)
  550. socket_write(data, fd, enc, 4);
  551. else
  552. socket_write(data, fd, mic, 4);
  553. socket_write(data, fd, cmd_buffer, cmd_size);
  554. socket_write(data, fd, "\r\n", 2);
  555. infof(data, "Send: %s%s", prot_level == PROT_PRIVATE?enc:mic,
  556. cmd_buffer);
  557. free(cmd_buffer);
  558. }
  559. }
  560. else {
  561. htonl_bytes = htonl(bytes);
  562. socket_write(data, fd, &htonl_bytes, sizeof(htonl_bytes));
  563. socket_write(data, fd, buffer, curlx_sitouz(bytes));
  564. }
  565. free(buffer);
  566. }
  567. static ssize_t sec_write(struct Curl_easy *data, struct connectdata *conn,
  568. curl_socket_t fd, const char *buffer, size_t length)
  569. {
  570. ssize_t tx = 0, len = conn->buffer_size;
  571. if(len <= 0)
  572. len = length;
  573. while(length) {
  574. if(length < (size_t)len)
  575. len = length;
  576. do_sec_send(data, conn, fd, buffer, curlx_sztosi(len));
  577. length -= len;
  578. buffer += len;
  579. tx += len;
  580. }
  581. return tx;
  582. }
  583. /* Matches Curl_send signature */
  584. static ssize_t sec_send(struct Curl_easy *data, int sockindex,
  585. const void *buffer, size_t len, CURLcode *err)
  586. {
  587. struct connectdata *conn = data->conn;
  588. curl_socket_t fd = conn->sock[sockindex];
  589. *err = CURLE_OK;
  590. return sec_write(data, conn, fd, buffer, len);
  591. }
  592. int Curl_sec_read_msg(struct Curl_easy *data, struct connectdata *conn,
  593. char *buffer, enum protection_level level)
  594. {
  595. /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
  596. int */
  597. int decoded_len;
  598. char *buf;
  599. int ret_code = 0;
  600. size_t decoded_sz = 0;
  601. CURLcode error;
  602. (void) data;
  603. if(!conn->mech)
  604. /* not initialized, return error */
  605. return -1;
  606. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  607. error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
  608. if(error || decoded_sz == 0)
  609. return -1;
  610. if(decoded_sz > (size_t)INT_MAX) {
  611. free(buf);
  612. return -1;
  613. }
  614. decoded_len = curlx_uztosi(decoded_sz);
  615. decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
  616. level, conn);
  617. if(decoded_len <= 0) {
  618. free(buf);
  619. return -1;
  620. }
  621. {
  622. buf[decoded_len] = '\n';
  623. Curl_debug(data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
  624. }
  625. buf[decoded_len] = '\0';
  626. if(decoded_len <= 3)
  627. /* suspiciously short */
  628. return 0;
  629. if(buf[3] != '-')
  630. ret_code = atoi(buf);
  631. if(buf[decoded_len - 1] == '\n')
  632. buf[decoded_len - 1] = '\0';
  633. strcpy(buffer, buf);
  634. free(buf);
  635. return ret_code;
  636. }
  637. static int sec_set_protection_level(struct Curl_easy *data)
  638. {
  639. int code;
  640. struct connectdata *conn = data->conn;
  641. enum protection_level level = conn->request_data_prot;
  642. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  643. if(!conn->sec_complete) {
  644. infof(data, "Trying to change the protection level after the"
  645. " completion of the data exchange.");
  646. return -1;
  647. }
  648. /* Bail out if we try to set up the same level */
  649. if(conn->data_prot == level)
  650. return 0;
  651. if(level) {
  652. char *pbsz;
  653. unsigned int buffer_size = 1 << 20; /* 1048576 */
  654. code = ftp_send_command(data, "PBSZ %u", buffer_size);
  655. if(code < 0)
  656. return -1;
  657. if(code/100 != 2) {
  658. failf(data, "Failed to set the protection's buffer size.");
  659. return -1;
  660. }
  661. conn->buffer_size = buffer_size;
  662. pbsz = strstr(data->state.buffer, "PBSZ=");
  663. if(pbsz) {
  664. /* stick to default value if the check fails */
  665. if(!strncmp(pbsz, "PBSZ=", 5) && ISDIGIT(pbsz[5]))
  666. buffer_size = atoi(&pbsz[5]);
  667. if(buffer_size < conn->buffer_size)
  668. conn->buffer_size = buffer_size;
  669. }
  670. }
  671. /* Now try to negotiate the protection level. */
  672. code = ftp_send_command(data, "PROT %c", level_to_char(level));
  673. if(code < 0)
  674. return -1;
  675. if(code/100 != 2) {
  676. failf(data, "Failed to set the protection level.");
  677. return -1;
  678. }
  679. conn->data_prot = level;
  680. if(level == PROT_PRIVATE)
  681. conn->command_prot = level;
  682. return 0;
  683. }
  684. int
  685. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  686. {
  687. enum protection_level l = name_to_level(level);
  688. if(l == PROT_NONE)
  689. return -1;
  690. DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
  691. conn->request_data_prot = l;
  692. return 0;
  693. }
  694. static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
  695. {
  696. int ret;
  697. void *tmp_allocation;
  698. const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
  699. tmp_allocation = realloc(conn->app_data, mech->size);
  700. if(!tmp_allocation) {
  701. failf(data, "Failed realloc of size %zu", mech->size);
  702. mech = NULL;
  703. return CURLE_OUT_OF_MEMORY;
  704. }
  705. conn->app_data = tmp_allocation;
  706. if(mech->init) {
  707. ret = mech->init(conn->app_data);
  708. if(ret) {
  709. infof(data, "Failed initialization for %s. Skipping it.",
  710. mech->name);
  711. return CURLE_FAILED_INIT;
  712. }
  713. }
  714. infof(data, "Trying mechanism %s...", mech->name);
  715. ret = ftp_send_command(data, "AUTH %s", mech->name);
  716. if(ret < 0)
  717. return CURLE_COULDNT_CONNECT;
  718. if(ret/100 != 3) {
  719. switch(ret) {
  720. case 504:
  721. infof(data, "Mechanism %s is not supported by the server (server "
  722. "returned ftp code: 504).", mech->name);
  723. break;
  724. case 534:
  725. infof(data, "Mechanism %s was rejected by the server (server returned "
  726. "ftp code: 534).", mech->name);
  727. break;
  728. default:
  729. if(ret/100 == 5) {
  730. infof(data, "server does not support the security extensions");
  731. return CURLE_USE_SSL_FAILED;
  732. }
  733. break;
  734. }
  735. return CURLE_LOGIN_DENIED;
  736. }
  737. /* Authenticate */
  738. ret = mech->auth(conn->app_data, data, conn);
  739. if(ret != AUTH_CONTINUE) {
  740. if(ret != AUTH_OK) {
  741. /* Mechanism has dumped the error to stderr, don't error here. */
  742. return CURLE_USE_SSL_FAILED;
  743. }
  744. DEBUGASSERT(ret == AUTH_OK);
  745. conn->mech = mech;
  746. conn->sec_complete = 1;
  747. conn->recv[FIRSTSOCKET] = sec_recv;
  748. conn->send[FIRSTSOCKET] = sec_send;
  749. conn->recv[SECONDARYSOCKET] = sec_recv;
  750. conn->send[SECONDARYSOCKET] = sec_send;
  751. conn->command_prot = PROT_SAFE;
  752. /* Set the requested protection level */
  753. /* BLOCKING */
  754. (void)sec_set_protection_level(data);
  755. }
  756. return CURLE_OK;
  757. }
  758. CURLcode
  759. Curl_sec_login(struct Curl_easy *data, struct connectdata *conn)
  760. {
  761. return choose_mech(data, conn);
  762. }
  763. void
  764. Curl_sec_end(struct connectdata *conn)
  765. {
  766. if(conn->mech && conn->mech->end)
  767. conn->mech->end(conn->app_data);
  768. free(conn->app_data);
  769. conn->app_data = NULL;
  770. if(conn->in_buffer.data) {
  771. free(conn->in_buffer.data);
  772. conn->in_buffer.data = NULL;
  773. conn->in_buffer.size = 0;
  774. conn->in_buffer.index = 0;
  775. conn->in_buffer.eof_flag = 0;
  776. }
  777. conn->sec_complete = 0;
  778. conn->data_prot = PROT_CLEAR;
  779. conn->mech = NULL;
  780. }
  781. #endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */