krb5.c 23 KB

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