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