krb5.c 23 KB

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