krb5.c 23 KB

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