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) 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. #error #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. unsigned char 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_nwrite(data, 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. unsigned char 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 unsigned char name_to_level(const char *name)
  344. {
  345. int i;
  346. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  347. if(curl_strequal(name, level_names[i].name))
  348. return level_names[i].level;
  349. return PROT_NONE;
  350. }
  351. /* Convert a protocol |level| to its char representation.
  352. We take an int to catch programming mistakes. */
  353. static char level_to_char(int level)
  354. {
  355. switch(level) {
  356. case PROT_CLEAR:
  357. return 'C';
  358. case PROT_SAFE:
  359. return 'S';
  360. case PROT_CONFIDENTIAL:
  361. return 'E';
  362. case PROT_PRIVATE:
  363. return 'P';
  364. case PROT_CMD:
  365. /* Fall through */
  366. default:
  367. /* Those 2 cases should not be reached! */
  368. break;
  369. }
  370. DEBUGASSERT(0);
  371. /* Default to the most secure alternative. */
  372. return 'P';
  373. }
  374. /* Send an FTP command defined by |message| and the optional arguments. The
  375. function returns the ftp_code. If an error occurs, -1 is returned. */
  376. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  377. {
  378. int ftp_code;
  379. ssize_t nread = 0;
  380. va_list args;
  381. char print_buffer[50];
  382. va_start(args, message);
  383. mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
  384. va_end(args);
  385. if(ftpsend(data, data->conn, print_buffer)) {
  386. ftp_code = -1;
  387. }
  388. else {
  389. if(Curl_GetFTPResponse(data, &nread, &ftp_code))
  390. ftp_code = -1;
  391. }
  392. (void)nread; /* Unused */
  393. return ftp_code;
  394. }
  395. /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
  396. saying whether an error occurred or CURLE_OK if |len| was read. */
  397. static CURLcode
  398. socket_read(struct Curl_easy *data, int sockindex, void *to, size_t len)
  399. {
  400. char *to_p = to;
  401. CURLcode result;
  402. ssize_t nread = 0;
  403. while(len > 0) {
  404. nread = Curl_conn_recv(data, sockindex, to_p, len, &result);
  405. if(nread > 0) {
  406. len -= nread;
  407. to_p += nread;
  408. }
  409. else {
  410. if(result == CURLE_AGAIN)
  411. continue;
  412. return result;
  413. }
  414. }
  415. return CURLE_OK;
  416. }
  417. /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
  418. CURLcode saying whether an error occurred or CURLE_OK if |len| was
  419. written. */
  420. static CURLcode
  421. socket_write(struct Curl_easy *data, int sockindex, const void *to,
  422. size_t len)
  423. {
  424. const char *to_p = to;
  425. CURLcode result;
  426. ssize_t written;
  427. while(len > 0) {
  428. written = Curl_conn_send(data, sockindex, to_p, len, &result);
  429. if(written > 0) {
  430. len -= written;
  431. to_p += written;
  432. }
  433. else {
  434. if(result == CURLE_AGAIN)
  435. continue;
  436. return result;
  437. }
  438. }
  439. return CURLE_OK;
  440. }
  441. static CURLcode read_data(struct Curl_easy *data, int sockindex,
  442. struct krb5buffer *buf)
  443. {
  444. struct connectdata *conn = data->conn;
  445. int len;
  446. CURLcode result;
  447. int nread;
  448. result = socket_read(data, sockindex, &len, sizeof(len));
  449. if(result)
  450. return result;
  451. if(len) {
  452. /* only realloc if there was a length */
  453. len = ntohl(len);
  454. if(len > CURL_MAX_INPUT_LENGTH)
  455. len = 0;
  456. else
  457. buf->data = Curl_saferealloc(buf->data, len);
  458. }
  459. if(!len || !buf->data)
  460. return CURLE_OUT_OF_MEMORY;
  461. result = socket_read(data, sockindex, buf->data, len);
  462. if(result)
  463. return result;
  464. nread = conn->mech->decode(conn->app_data, buf->data, len,
  465. conn->data_prot, conn);
  466. if(nread < 0)
  467. return CURLE_RECV_ERROR;
  468. buf->size = (size_t)nread;
  469. buf->index = 0;
  470. return CURLE_OK;
  471. }
  472. static size_t
  473. buffer_read(struct krb5buffer *buf, void *data, size_t len)
  474. {
  475. if(buf->size - buf->index < len)
  476. len = buf->size - buf->index;
  477. memcpy(data, (char *)buf->data + buf->index, len);
  478. buf->index += len;
  479. return len;
  480. }
  481. /* Matches Curl_recv signature */
  482. static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
  483. char *buffer, size_t len, CURLcode *err)
  484. {
  485. size_t bytes_read;
  486. size_t total_read = 0;
  487. struct connectdata *conn = data->conn;
  488. *err = CURLE_OK;
  489. /* Handle clear text response. */
  490. if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
  491. return Curl_conn_recv(data, sockindex, buffer, len, err);
  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(data, sockindex, &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. ret_code = atoi(buf);
  630. if(buf[decoded_len - 1] == '\n')
  631. buf[decoded_len - 1] = '\0';
  632. strcpy(buffer, buf);
  633. free(buf);
  634. return ret_code;
  635. }
  636. static int sec_set_protection_level(struct Curl_easy *data)
  637. {
  638. int code;
  639. struct connectdata *conn = data->conn;
  640. unsigned char level = conn->request_data_prot;
  641. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  642. if(!conn->sec_complete) {
  643. infof(data, "Trying to change the protection level after the"
  644. " completion of the data exchange.");
  645. return -1;
  646. }
  647. /* Bail out if we try to set up the same level */
  648. if(conn->data_prot == level)
  649. return 0;
  650. if(level) {
  651. char *pbsz;
  652. unsigned int buffer_size = 1 << 20; /* 1048576 */
  653. code = ftp_send_command(data, "PBSZ %u", buffer_size);
  654. if(code < 0)
  655. return -1;
  656. if(code/100 != 2) {
  657. failf(data, "Failed to set the protection's buffer size.");
  658. return -1;
  659. }
  660. conn->buffer_size = buffer_size;
  661. pbsz = strstr(data->state.buffer, "PBSZ=");
  662. if(pbsz) {
  663. /* stick to default value if the check fails */
  664. if(!strncmp(pbsz, "PBSZ=", 5) && ISDIGIT(pbsz[5]))
  665. buffer_size = atoi(&pbsz[5]);
  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. unsigned char 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 */