krb5.c 24 KB

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