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, *gssresp;
  180. OM_uint32 maj, min;
  181. gss_name_t gssname;
  182. gss_ctx_id_t *context = app_data;
  183. struct gss_channel_bindings_struct chan;
  184. size_t base64_sz = 0;
  185. struct sockaddr_in *remote_addr =
  186. (struct sockaddr_in *)(void *)&conn->remote_addr->curl_sa_addr;
  187. char *stringp;
  188. if(getsockname(conn->sock[FIRSTSOCKET],
  189. (struct sockaddr *)&conn->local_addr, &l) < 0)
  190. perror("getsockname()");
  191. chan.initiator_addrtype = GSS_C_AF_INET;
  192. chan.initiator_address.length = l - 4;
  193. chan.initiator_address.value = &conn->local_addr.sin_addr.s_addr;
  194. chan.acceptor_addrtype = GSS_C_AF_INET;
  195. chan.acceptor_address.length = l - 4;
  196. chan.acceptor_address.value = &remote_addr->sin_addr.s_addr;
  197. chan.application_data.length = 0;
  198. chan.application_data.value = NULL;
  199. /* this loop will execute twice (once for service, once for host) */
  200. for(;;) {
  201. /* this really should not be repeated here, but cannot help it */
  202. if(service == srv_host) {
  203. result = ftpsend(data, conn, "AUTH GSSAPI");
  204. if(result)
  205. return -2;
  206. if(Curl_GetFTPResponse(data, &nread, NULL))
  207. return -1;
  208. else {
  209. struct pingpong *pp = &conn->proto.ftpc.pp;
  210. char *line = Curl_dyn_ptr(&pp->recvbuf);
  211. if(line[0] != '3')
  212. return -1;
  213. }
  214. }
  215. stringp = aprintf("%s@%s", service, host);
  216. if(!stringp)
  217. return -2;
  218. input_buffer.value = stringp;
  219. input_buffer.length = strlen(stringp);
  220. maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
  221. &gssname);
  222. free(stringp);
  223. if(maj != GSS_S_COMPLETE) {
  224. gss_release_name(&min, &gssname);
  225. if(service == srv_host) {
  226. failf(data, "Error importing service name %s@%s", service, host);
  227. return AUTH_ERROR;
  228. }
  229. service = srv_host;
  230. continue;
  231. }
  232. /* We pass NULL as |output_name_type| to avoid a leak. */
  233. gss_display_name(&min, gssname, &output_buffer, NULL);
  234. infof(data, "Trying against %s", (char *)output_buffer.value);
  235. gssresp = GSS_C_NO_BUFFER;
  236. *context = GSS_C_NO_CONTEXT;
  237. do {
  238. /* Release the buffer at each iteration to avoid leaking: the first time
  239. we are releasing the memory from gss_display_name. The last item is
  240. taken care by a final gss_release_buffer. */
  241. gss_release_buffer(&min, &output_buffer);
  242. ret = AUTH_OK;
  243. maj = Curl_gss_init_sec_context(data,
  244. &min,
  245. context,
  246. gssname,
  247. &Curl_krb5_mech_oid,
  248. &chan,
  249. gssresp,
  250. &output_buffer,
  251. TRUE,
  252. NULL);
  253. if(gssresp) {
  254. free(_gssresp.value);
  255. gssresp = NULL;
  256. }
  257. if(GSS_ERROR(maj)) {
  258. infof(data, "Error creating security context");
  259. ret = AUTH_ERROR;
  260. break;
  261. }
  262. if(output_buffer.length) {
  263. char *cmd;
  264. result = Curl_base64_encode((char *)output_buffer.value,
  265. output_buffer.length, &p, &base64_sz);
  266. if(result) {
  267. infof(data, "base64-encoding: %s", curl_easy_strerror(result));
  268. ret = AUTH_ERROR;
  269. break;
  270. }
  271. cmd = aprintf("ADAT %s", p);
  272. if(cmd)
  273. result = ftpsend(data, conn, cmd);
  274. else
  275. result = CURLE_OUT_OF_MEMORY;
  276. free(p);
  277. free(cmd);
  278. if(result) {
  279. ret = -2;
  280. break;
  281. }
  282. if(Curl_GetFTPResponse(data, &nread, NULL)) {
  283. ret = -1;
  284. break;
  285. }
  286. else {
  287. struct pingpong *pp = &conn->proto.ftpc.pp;
  288. size_t len = Curl_dyn_len(&pp->recvbuf);
  289. p = Curl_dyn_ptr(&pp->recvbuf);
  290. if((len < 4) || (p[0] != '2' && p[0] != '3')) {
  291. infof(data, "Server did not accept auth data");
  292. ret = AUTH_ERROR;
  293. break;
  294. }
  295. }
  296. _gssresp.value = NULL; /* make sure it is initialized */
  297. _gssresp.length = 0;
  298. p += 4; /* over '789 ' */
  299. p = strstr(p, "ADAT=");
  300. if(p) {
  301. unsigned char *outptr;
  302. size_t outlen;
  303. result = Curl_base64_decode(p + 5, &outptr, &outlen);
  304. if(result) {
  305. failf(data, "base64-decoding: %s", curl_easy_strerror(result));
  306. ret = AUTH_CONTINUE;
  307. break;
  308. }
  309. _gssresp.value = outptr;
  310. _gssresp.length = outlen;
  311. }
  312. gssresp = &_gssresp;
  313. }
  314. } while(maj == GSS_S_CONTINUE_NEEDED);
  315. gss_release_name(&min, &gssname);
  316. gss_release_buffer(&min, &output_buffer);
  317. if(gssresp)
  318. free(_gssresp.value);
  319. if(ret == AUTH_OK || service == srv_host)
  320. return ret;
  321. service = srv_host;
  322. }
  323. return ret;
  324. }
  325. static void krb5_end(void *app_data)
  326. {
  327. OM_uint32 min;
  328. gss_ctx_id_t *context = app_data;
  329. if(*context != GSS_C_NO_CONTEXT) {
  330. OM_uint32 maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
  331. (void)maj;
  332. DEBUGASSERT(maj == GSS_S_COMPLETE);
  333. }
  334. }
  335. static const struct Curl_sec_client_mech Curl_krb5_client_mech = {
  336. "GSSAPI",
  337. sizeof(gss_ctx_id_t),
  338. krb5_init,
  339. krb5_auth,
  340. krb5_end,
  341. krb5_check_prot,
  342. krb5_encode,
  343. krb5_decode
  344. };
  345. static const struct {
  346. unsigned char level;
  347. const char *name;
  348. } level_names[] = {
  349. { PROT_CLEAR, "clear" },
  350. { PROT_SAFE, "safe" },
  351. { PROT_CONFIDENTIAL, "confidential" },
  352. { PROT_PRIVATE, "private" }
  353. };
  354. static unsigned char name_to_level(const char *name)
  355. {
  356. int i;
  357. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  358. if(curl_strequal(name, level_names[i].name))
  359. return level_names[i].level;
  360. return PROT_NONE;
  361. }
  362. /* Convert a protocol |level| to its char representation.
  363. We take an int to catch programming mistakes. */
  364. static char level_to_char(int level)
  365. {
  366. switch(level) {
  367. case PROT_CLEAR:
  368. return 'C';
  369. case PROT_SAFE:
  370. return 'S';
  371. case PROT_CONFIDENTIAL:
  372. return 'E';
  373. case PROT_PRIVATE:
  374. return 'P';
  375. case PROT_CMD:
  376. default:
  377. /* Those 2 cases should not be reached! */
  378. break;
  379. }
  380. DEBUGASSERT(0);
  381. /* Default to the most secure alternative. */
  382. return 'P';
  383. }
  384. /* Send an FTP command defined by |message| and the optional arguments. The
  385. function returns the ftp_code. If an error occurs, -1 is returned. */
  386. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  387. CURL_PRINTF(2, 3);
  388. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  389. {
  390. int ftp_code;
  391. ssize_t nread = 0;
  392. va_list args;
  393. char print_buffer[50];
  394. va_start(args, message);
  395. mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
  396. va_end(args);
  397. if(ftpsend(data, data->conn, print_buffer)) {
  398. ftp_code = -1;
  399. }
  400. else {
  401. if(Curl_GetFTPResponse(data, &nread, &ftp_code))
  402. ftp_code = -1;
  403. }
  404. (void)nread; /* Unused */
  405. return ftp_code;
  406. }
  407. /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
  408. saying whether an error occurred or CURLE_OK if |len| was read. */
  409. static CURLcode
  410. socket_read(struct Curl_easy *data, int sockindex, void *to, size_t len)
  411. {
  412. char *to_p = to;
  413. CURLcode result;
  414. ssize_t nread = 0;
  415. while(len > 0) {
  416. result = Curl_conn_recv(data, sockindex, to_p, len, &nread);
  417. if(nread > 0) {
  418. len -= nread;
  419. to_p += nread;
  420. }
  421. else {
  422. if(result == CURLE_AGAIN)
  423. continue;
  424. return result;
  425. }
  426. }
  427. return CURLE_OK;
  428. }
  429. /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
  430. CURLcode saying whether an error occurred or CURLE_OK if |len| was
  431. written. */
  432. static CURLcode
  433. socket_write(struct Curl_easy *data, int sockindex, const void *to,
  434. size_t len)
  435. {
  436. const char *to_p = to;
  437. CURLcode result;
  438. size_t written;
  439. while(len > 0) {
  440. result = Curl_conn_send(data, sockindex, to_p, len, FALSE, &written);
  441. if(!result && written > 0) {
  442. len -= written;
  443. to_p += written;
  444. }
  445. else {
  446. if(result == CURLE_AGAIN)
  447. continue;
  448. return result;
  449. }
  450. }
  451. return CURLE_OK;
  452. }
  453. static CURLcode read_data(struct Curl_easy *data, int sockindex,
  454. struct krb5buffer *buf)
  455. {
  456. struct connectdata *conn = data->conn;
  457. int len;
  458. CURLcode result;
  459. int nread;
  460. result = socket_read(data, sockindex, &len, sizeof(len));
  461. if(result)
  462. return result;
  463. if(len) {
  464. len = (int)ntohl((uint32_t)len);
  465. if(len > CURL_MAX_INPUT_LENGTH)
  466. return CURLE_TOO_LARGE;
  467. Curl_dyn_reset(&buf->buf);
  468. }
  469. else
  470. return CURLE_RECV_ERROR;
  471. do {
  472. char buffer[1024];
  473. nread = CURLMIN(len, (int)sizeof(buffer));
  474. result = socket_read(data, sockindex, buffer, (size_t)nread);
  475. if(result)
  476. return result;
  477. result = Curl_dyn_addn(&buf->buf, buffer, nread);
  478. if(result)
  479. return result;
  480. len -= nread;
  481. } while(len);
  482. /* this decodes the dynbuf *in place* */
  483. nread = conn->mech->decode(conn->app_data,
  484. Curl_dyn_ptr(&buf->buf),
  485. len, conn->data_prot, conn);
  486. if(nread < 0)
  487. return CURLE_RECV_ERROR;
  488. Curl_dyn_setlen(&buf->buf, nread);
  489. buf->index = 0;
  490. return CURLE_OK;
  491. }
  492. static size_t
  493. buffer_read(struct krb5buffer *buf, void *data, size_t len)
  494. {
  495. size_t size = Curl_dyn_len(&buf->buf);
  496. if(size - buf->index < len)
  497. len = size - buf->index;
  498. memcpy(data, Curl_dyn_ptr(&buf->buf) + buf->index, len);
  499. buf->index += len;
  500. return len;
  501. }
  502. /* Matches Curl_recv signature */
  503. static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
  504. char *buffer, size_t len, CURLcode *err)
  505. {
  506. size_t bytes_read;
  507. size_t total_read = 0;
  508. struct connectdata *conn = data->conn;
  509. *err = CURLE_OK;
  510. /* Handle clear text response. */
  511. if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR) {
  512. ssize_t nread;
  513. *err = Curl_conn_recv(data, sockindex, buffer, len, &nread);
  514. return nread;
  515. }
  516. if(conn->in_buffer.eof_flag) {
  517. conn->in_buffer.eof_flag = 0;
  518. return 0;
  519. }
  520. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  521. len -= bytes_read;
  522. total_read += bytes_read;
  523. buffer += bytes_read;
  524. while(len > 0) {
  525. if(read_data(data, sockindex, &conn->in_buffer))
  526. return -1;
  527. if(Curl_dyn_len(&conn->in_buffer.buf) == 0) {
  528. if(bytes_read > 0)
  529. conn->in_buffer.eof_flag = 1;
  530. return bytes_read;
  531. }
  532. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  533. len -= bytes_read;
  534. total_read += bytes_read;
  535. buffer += bytes_read;
  536. }
  537. return total_read;
  538. }
  539. /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
  540. and negotiating with the server. |from| can be NULL. */
  541. static void do_sec_send(struct Curl_easy *data, struct connectdata *conn,
  542. curl_socket_t fd, const char *from, int length)
  543. {
  544. int bytes, htonl_bytes; /* 32-bit integers for htonl */
  545. char *buffer = NULL;
  546. char *cmd_buffer;
  547. size_t cmd_size = 0;
  548. CURLcode error;
  549. enum protection_level prot_level = conn->data_prot;
  550. bool iscmd = (prot_level == PROT_CMD) ? TRUE : FALSE;
  551. DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
  552. if(iscmd) {
  553. if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
  554. prot_level = PROT_PRIVATE;
  555. else
  556. prot_level = conn->command_prot;
  557. }
  558. bytes = conn->mech->encode(conn->app_data, from, length, (int)prot_level,
  559. (void **)&buffer);
  560. if(!buffer || bytes <= 0)
  561. return; /* error */
  562. if(iscmd) {
  563. error = Curl_base64_encode(buffer, curlx_sitouz(bytes),
  564. &cmd_buffer, &cmd_size);
  565. if(error) {
  566. free(buffer);
  567. return; /* error */
  568. }
  569. if(cmd_size > 0) {
  570. static const char *enc = "ENC ";
  571. static const char *mic = "MIC ";
  572. if(prot_level == PROT_PRIVATE)
  573. socket_write(data, fd, enc, 4);
  574. else
  575. socket_write(data, fd, mic, 4);
  576. socket_write(data, fd, cmd_buffer, cmd_size);
  577. socket_write(data, fd, "\r\n", 2);
  578. infof(data, "Send: %s%s", prot_level == PROT_PRIVATE ? enc : mic,
  579. cmd_buffer);
  580. free(cmd_buffer);
  581. }
  582. }
  583. else {
  584. htonl_bytes = (int)htonl((OM_uint32)bytes);
  585. socket_write(data, fd, &htonl_bytes, sizeof(htonl_bytes));
  586. socket_write(data, fd, buffer, curlx_sitouz(bytes));
  587. }
  588. free(buffer);
  589. }
  590. static ssize_t sec_write(struct Curl_easy *data, struct connectdata *conn,
  591. curl_socket_t fd, const char *buffer, size_t length)
  592. {
  593. ssize_t tx = 0, len = conn->buffer_size;
  594. if(len <= 0)
  595. len = length;
  596. while(length) {
  597. if(length < (size_t)len)
  598. len = length;
  599. do_sec_send(data, conn, fd, buffer, curlx_sztosi(len));
  600. length -= len;
  601. buffer += len;
  602. tx += len;
  603. }
  604. return tx;
  605. }
  606. /* Matches Curl_send signature */
  607. static ssize_t sec_send(struct Curl_easy *data, int sockindex,
  608. const void *buffer, size_t len, bool eos,
  609. CURLcode *err)
  610. {
  611. struct connectdata *conn = data->conn;
  612. curl_socket_t fd = conn->sock[sockindex];
  613. (void)eos; /* unused */
  614. *err = CURLE_OK;
  615. return sec_write(data, conn, fd, 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 */