krb5.c 24 KB

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