krb5.c 23 KB

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