krb5.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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, &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->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. p += 4; /* over '789 ' */
  298. p = strstr(p, "ADAT=");
  299. if(p) {
  300. result = Curl_base64_decode(p + 5,
  301. (unsigned char **)&_gssresp.value,
  302. &_gssresp.length);
  303. if(result) {
  304. failf(data, "base64-decoding: %s", curl_easy_strerror(result));
  305. ret = AUTH_CONTINUE;
  306. break;
  307. }
  308. }
  309. gssresp = &_gssresp;
  310. }
  311. } while(maj == GSS_S_CONTINUE_NEEDED);
  312. gss_release_name(&min, &gssname);
  313. gss_release_buffer(&min, &output_buffer);
  314. if(gssresp)
  315. free(_gssresp.value);
  316. if(ret == AUTH_OK || service == srv_host)
  317. return ret;
  318. service = srv_host;
  319. }
  320. return ret;
  321. }
  322. static void krb5_end(void *app_data)
  323. {
  324. OM_uint32 min;
  325. gss_ctx_id_t *context = app_data;
  326. if(*context != GSS_C_NO_CONTEXT) {
  327. OM_uint32 maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
  328. (void)maj;
  329. DEBUGASSERT(maj == GSS_S_COMPLETE);
  330. }
  331. }
  332. static const struct Curl_sec_client_mech Curl_krb5_client_mech = {
  333. "GSSAPI",
  334. sizeof(gss_ctx_id_t),
  335. krb5_init,
  336. krb5_auth,
  337. krb5_end,
  338. krb5_check_prot,
  339. krb5_encode,
  340. krb5_decode
  341. };
  342. static const struct {
  343. unsigned char level;
  344. const char *name;
  345. } level_names[] = {
  346. { PROT_CLEAR, "clear" },
  347. { PROT_SAFE, "safe" },
  348. { PROT_CONFIDENTIAL, "confidential" },
  349. { PROT_PRIVATE, "private" }
  350. };
  351. static unsigned char name_to_level(const char *name)
  352. {
  353. int i;
  354. for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
  355. if(curl_strequal(name, level_names[i].name))
  356. return level_names[i].level;
  357. return PROT_NONE;
  358. }
  359. /* Convert a protocol |level| to its char representation.
  360. We take an int to catch programming mistakes. */
  361. static char level_to_char(int level)
  362. {
  363. switch(level) {
  364. case PROT_CLEAR:
  365. return 'C';
  366. case PROT_SAFE:
  367. return 'S';
  368. case PROT_CONFIDENTIAL:
  369. return 'E';
  370. case PROT_PRIVATE:
  371. return 'P';
  372. case PROT_CMD:
  373. default:
  374. /* Those 2 cases should not be reached! */
  375. break;
  376. }
  377. DEBUGASSERT(0);
  378. /* Default to the most secure alternative. */
  379. return 'P';
  380. }
  381. /* Send an FTP command defined by |message| and the optional arguments. The
  382. function returns the ftp_code. If an error occurs, -1 is returned. */
  383. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  384. CURL_PRINTF(2, 3);
  385. static int ftp_send_command(struct Curl_easy *data, const char *message, ...)
  386. {
  387. int ftp_code;
  388. ssize_t nread = 0;
  389. va_list args;
  390. char print_buffer[50];
  391. va_start(args, message);
  392. mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
  393. va_end(args);
  394. if(ftpsend(data, data->conn, print_buffer)) {
  395. ftp_code = -1;
  396. }
  397. else {
  398. if(Curl_GetFTPResponse(data, &nread, &ftp_code))
  399. ftp_code = -1;
  400. }
  401. (void)nread; /* Unused */
  402. return ftp_code;
  403. }
  404. /* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
  405. saying whether an error occurred or CURLE_OK if |len| was read. */
  406. static CURLcode
  407. socket_read(struct Curl_easy *data, int sockindex, void *to, size_t len)
  408. {
  409. char *to_p = to;
  410. CURLcode result;
  411. ssize_t nread = 0;
  412. while(len > 0) {
  413. result = Curl_conn_recv(data, sockindex, to_p, len, &nread);
  414. if(nread > 0) {
  415. len -= nread;
  416. to_p += nread;
  417. }
  418. else {
  419. if(result == CURLE_AGAIN)
  420. continue;
  421. return result;
  422. }
  423. }
  424. return CURLE_OK;
  425. }
  426. /* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
  427. CURLcode saying whether an error occurred or CURLE_OK if |len| was
  428. written. */
  429. static CURLcode
  430. socket_write(struct Curl_easy *data, int sockindex, const void *to,
  431. size_t len)
  432. {
  433. const char *to_p = to;
  434. CURLcode result;
  435. size_t written;
  436. while(len > 0) {
  437. result = Curl_conn_send(data, sockindex, to_p, len, &written);
  438. if(!result && written > 0) {
  439. len -= written;
  440. to_p += written;
  441. }
  442. else {
  443. if(result == CURLE_AGAIN)
  444. continue;
  445. return result;
  446. }
  447. }
  448. return CURLE_OK;
  449. }
  450. static CURLcode read_data(struct Curl_easy *data, int sockindex,
  451. struct krb5buffer *buf)
  452. {
  453. struct connectdata *conn = data->conn;
  454. int len;
  455. CURLcode result;
  456. int nread;
  457. result = socket_read(data, sockindex, &len, sizeof(len));
  458. if(result)
  459. return result;
  460. if(len) {
  461. len = (int)ntohl((uint32_t)len);
  462. if(len > CURL_MAX_INPUT_LENGTH)
  463. return CURLE_TOO_LARGE;
  464. Curl_dyn_reset(&buf->buf);
  465. }
  466. else
  467. return CURLE_RECV_ERROR;
  468. do {
  469. char buffer[1024];
  470. nread = CURLMIN(len, (int)sizeof(buffer));
  471. result = socket_read(data, sockindex, buffer, (size_t)nread);
  472. if(result)
  473. return result;
  474. result = Curl_dyn_addn(&buf->buf, buffer, nread);
  475. if(result)
  476. return result;
  477. len -= nread;
  478. } while(len);
  479. /* this decodes the dynbuf *in place* */
  480. nread = conn->mech->decode(conn->app_data,
  481. Curl_dyn_ptr(&buf->buf),
  482. len, conn->data_prot, conn);
  483. if(nread < 0)
  484. return CURLE_RECV_ERROR;
  485. Curl_dyn_setlen(&buf->buf, nread);
  486. buf->index = 0;
  487. return CURLE_OK;
  488. }
  489. static size_t
  490. buffer_read(struct krb5buffer *buf, void *data, size_t len)
  491. {
  492. size_t size = Curl_dyn_len(&buf->buf);
  493. if(size - buf->index < len)
  494. len = size - buf->index;
  495. memcpy(data, Curl_dyn_ptr(&buf->buf) + buf->index, len);
  496. buf->index += len;
  497. return len;
  498. }
  499. /* Matches Curl_recv signature */
  500. static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
  501. char *buffer, size_t len, CURLcode *err)
  502. {
  503. size_t bytes_read;
  504. size_t total_read = 0;
  505. struct connectdata *conn = data->conn;
  506. *err = CURLE_OK;
  507. /* Handle clear text response. */
  508. if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR) {
  509. ssize_t nread;
  510. *err = Curl_conn_recv(data, sockindex, buffer, len, &nread);
  511. return nread;
  512. }
  513. if(conn->in_buffer.eof_flag) {
  514. conn->in_buffer.eof_flag = 0;
  515. return 0;
  516. }
  517. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  518. len -= bytes_read;
  519. total_read += bytes_read;
  520. buffer += bytes_read;
  521. while(len > 0) {
  522. if(read_data(data, sockindex, &conn->in_buffer))
  523. return -1;
  524. if(Curl_dyn_len(&conn->in_buffer.buf) == 0) {
  525. if(bytes_read > 0)
  526. conn->in_buffer.eof_flag = 1;
  527. return bytes_read;
  528. }
  529. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  530. len -= bytes_read;
  531. total_read += bytes_read;
  532. buffer += bytes_read;
  533. }
  534. return total_read;
  535. }
  536. /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
  537. and negotiating with the server. |from| can be NULL. */
  538. static void do_sec_send(struct Curl_easy *data, struct connectdata *conn,
  539. curl_socket_t fd, const char *from, int length)
  540. {
  541. int bytes, htonl_bytes; /* 32-bit integers for htonl */
  542. char *buffer = NULL;
  543. char *cmd_buffer;
  544. size_t cmd_size = 0;
  545. CURLcode error;
  546. enum protection_level prot_level = conn->data_prot;
  547. bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
  548. DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
  549. if(iscmd) {
  550. if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
  551. prot_level = PROT_PRIVATE;
  552. else
  553. prot_level = conn->command_prot;
  554. }
  555. bytes = conn->mech->encode(conn->app_data, from, length, (int)prot_level,
  556. (void **)&buffer);
  557. if(!buffer || bytes <= 0)
  558. return; /* error */
  559. if(iscmd) {
  560. error = Curl_base64_encode(buffer, curlx_sitouz(bytes),
  561. &cmd_buffer, &cmd_size);
  562. if(error) {
  563. free(buffer);
  564. return; /* error */
  565. }
  566. if(cmd_size > 0) {
  567. static const char *enc = "ENC ";
  568. static const char *mic = "MIC ";
  569. if(prot_level == PROT_PRIVATE)
  570. socket_write(data, fd, enc, 4);
  571. else
  572. socket_write(data, fd, mic, 4);
  573. socket_write(data, fd, cmd_buffer, cmd_size);
  574. socket_write(data, fd, "\r\n", 2);
  575. infof(data, "Send: %s%s", prot_level == PROT_PRIVATE?enc:mic,
  576. cmd_buffer);
  577. free(cmd_buffer);
  578. }
  579. }
  580. else {
  581. htonl_bytes = (int)htonl((OM_uint32)bytes);
  582. socket_write(data, fd, &htonl_bytes, sizeof(htonl_bytes));
  583. socket_write(data, fd, buffer, curlx_sitouz(bytes));
  584. }
  585. free(buffer);
  586. }
  587. static ssize_t sec_write(struct Curl_easy *data, struct connectdata *conn,
  588. curl_socket_t fd, const char *buffer, size_t length)
  589. {
  590. ssize_t tx = 0, len = conn->buffer_size;
  591. if(len <= 0)
  592. len = length;
  593. while(length) {
  594. if(length < (size_t)len)
  595. len = length;
  596. do_sec_send(data, conn, fd, buffer, curlx_sztosi(len));
  597. length -= len;
  598. buffer += len;
  599. tx += len;
  600. }
  601. return tx;
  602. }
  603. /* Matches Curl_send signature */
  604. static ssize_t sec_send(struct Curl_easy *data, int sockindex,
  605. const void *buffer, size_t len, CURLcode *err)
  606. {
  607. struct connectdata *conn = data->conn;
  608. curl_socket_t fd = conn->sock[sockindex];
  609. *err = CURLE_OK;
  610. return sec_write(data, conn, fd, buffer, len);
  611. }
  612. int Curl_sec_read_msg(struct Curl_easy *data, struct connectdata *conn,
  613. char *buffer, enum protection_level level)
  614. {
  615. /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
  616. int */
  617. int decoded_len;
  618. char *buf;
  619. int ret_code = 0;
  620. size_t decoded_sz = 0;
  621. CURLcode error;
  622. (void) data;
  623. if(!conn->mech)
  624. /* not initialized, return error */
  625. return -1;
  626. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  627. error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
  628. if(error || decoded_sz == 0)
  629. return -1;
  630. if(decoded_sz > (size_t)INT_MAX) {
  631. free(buf);
  632. return -1;
  633. }
  634. decoded_len = curlx_uztosi(decoded_sz);
  635. decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
  636. (int)level, conn);
  637. if(decoded_len <= 0) {
  638. free(buf);
  639. return -1;
  640. }
  641. {
  642. buf[decoded_len] = '\n';
  643. Curl_debug(data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
  644. }
  645. buf[decoded_len] = '\0';
  646. if(decoded_len <= 3)
  647. /* suspiciously short */
  648. return 0;
  649. if(buf[3] != '-')
  650. ret_code = atoi(buf);
  651. if(buf[decoded_len - 1] == '\n')
  652. buf[decoded_len - 1] = '\0';
  653. strcpy(buffer, buf);
  654. free(buf);
  655. return ret_code;
  656. }
  657. static int sec_set_protection_level(struct Curl_easy *data)
  658. {
  659. int code;
  660. struct connectdata *conn = data->conn;
  661. unsigned char level = conn->request_data_prot;
  662. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  663. if(!conn->sec_complete) {
  664. infof(data, "Trying to change the protection level after the"
  665. " completion of the data exchange.");
  666. return -1;
  667. }
  668. /* Bail out if we try to set up the same level */
  669. if(conn->data_prot == level)
  670. return 0;
  671. if(level) {
  672. char *pbsz;
  673. unsigned int buffer_size = 1 << 20; /* 1048576 */
  674. struct pingpong *pp = &conn->proto.ftpc.pp;
  675. char *line;
  676. code = ftp_send_command(data, "PBSZ %u", buffer_size);
  677. if(code < 0)
  678. return -1;
  679. if(code/100 != 2) {
  680. failf(data, "Failed to set the protection's buffer size.");
  681. return -1;
  682. }
  683. conn->buffer_size = buffer_size;
  684. line = Curl_dyn_ptr(&pp->recvbuf);
  685. pbsz = strstr(line, "PBSZ=");
  686. if(pbsz) {
  687. /* stick to default value if the check fails */
  688. if(ISDIGIT(pbsz[5]))
  689. buffer_size = (unsigned int)atoi(&pbsz[5]);
  690. if(buffer_size < conn->buffer_size)
  691. conn->buffer_size = buffer_size;
  692. }
  693. }
  694. /* Now try to negotiate the protection level. */
  695. code = ftp_send_command(data, "PROT %c", level_to_char(level));
  696. if(code < 0)
  697. return -1;
  698. if(code/100 != 2) {
  699. failf(data, "Failed to set the protection level.");
  700. return -1;
  701. }
  702. conn->data_prot = level;
  703. if(level == PROT_PRIVATE)
  704. conn->command_prot = level;
  705. return 0;
  706. }
  707. int
  708. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  709. {
  710. unsigned char l = name_to_level(level);
  711. if(l == PROT_NONE)
  712. return -1;
  713. DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
  714. conn->request_data_prot = l;
  715. return 0;
  716. }
  717. static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
  718. {
  719. int ret;
  720. void *tmp_allocation;
  721. const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
  722. tmp_allocation = realloc(conn->app_data, mech->size);
  723. if(!tmp_allocation) {
  724. failf(data, "Failed realloc of size %zu", mech->size);
  725. mech = NULL;
  726. return CURLE_OUT_OF_MEMORY;
  727. }
  728. conn->app_data = tmp_allocation;
  729. if(mech->init) {
  730. ret = mech->init(conn->app_data);
  731. if(ret) {
  732. infof(data, "Failed initialization for %s. Skipping it.",
  733. mech->name);
  734. return CURLE_FAILED_INIT;
  735. }
  736. Curl_dyn_init(&conn->in_buffer.buf, CURL_MAX_INPUT_LENGTH);
  737. }
  738. infof(data, "Trying mechanism %s...", mech->name);
  739. ret = ftp_send_command(data, "AUTH %s", mech->name);
  740. if(ret < 0)
  741. return CURLE_COULDNT_CONNECT;
  742. if(ret/100 != 3) {
  743. switch(ret) {
  744. case 504:
  745. infof(data, "Mechanism %s is not supported by the server (server "
  746. "returned ftp code: 504).", mech->name);
  747. break;
  748. case 534:
  749. infof(data, "Mechanism %s was rejected by the server (server returned "
  750. "ftp code: 534).", mech->name);
  751. break;
  752. default:
  753. if(ret/100 == 5) {
  754. infof(data, "server does not support the security extensions");
  755. return CURLE_USE_SSL_FAILED;
  756. }
  757. break;
  758. }
  759. return CURLE_LOGIN_DENIED;
  760. }
  761. /* Authenticate */
  762. ret = mech->auth(conn->app_data, data, conn);
  763. if(ret != AUTH_CONTINUE) {
  764. if(ret != AUTH_OK) {
  765. /* Mechanism has dumped the error to stderr, do not error here. */
  766. return CURLE_USE_SSL_FAILED;
  767. }
  768. DEBUGASSERT(ret == AUTH_OK);
  769. conn->mech = mech;
  770. conn->sec_complete = 1;
  771. conn->recv[FIRSTSOCKET] = sec_recv;
  772. conn->send[FIRSTSOCKET] = sec_send;
  773. conn->recv[SECONDARYSOCKET] = sec_recv;
  774. conn->send[SECONDARYSOCKET] = sec_send;
  775. conn->command_prot = PROT_SAFE;
  776. /* Set the requested protection level */
  777. /* BLOCKING */
  778. (void)sec_set_protection_level(data);
  779. }
  780. return CURLE_OK;
  781. }
  782. CURLcode
  783. Curl_sec_login(struct Curl_easy *data, struct connectdata *conn)
  784. {
  785. return choose_mech(data, conn);
  786. }
  787. void
  788. Curl_sec_end(struct connectdata *conn)
  789. {
  790. if(conn->mech && conn->mech->end)
  791. conn->mech->end(conn->app_data);
  792. Curl_safefree(conn->app_data);
  793. Curl_dyn_free(&conn->in_buffer.buf);
  794. conn->in_buffer.index = 0;
  795. conn->in_buffer.eof_flag = 0;
  796. conn->sec_complete = 0;
  797. conn->data_prot = PROT_CLEAR;
  798. conn->mech = NULL;
  799. }
  800. #endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */