krb5.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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 = 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 doesn't 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 shouldn't be repeated here, but can't 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 didn't 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. /* only realloc if there was a length */
  462. len = ntohl(len);
  463. if(len > CURL_MAX_INPUT_LENGTH)
  464. len = 0;
  465. else
  466. buf->data = Curl_saferealloc(buf->data, len);
  467. }
  468. if(!len || !buf->data)
  469. return CURLE_OUT_OF_MEMORY;
  470. result = socket_read(data, sockindex, buf->data, len);
  471. if(result)
  472. return result;
  473. nread = conn->mech->decode(conn->app_data, buf->data, len,
  474. conn->data_prot, conn);
  475. if(nread < 0)
  476. return CURLE_RECV_ERROR;
  477. buf->size = (size_t)nread;
  478. buf->index = 0;
  479. return CURLE_OK;
  480. }
  481. static size_t
  482. buffer_read(struct krb5buffer *buf, void *data, size_t len)
  483. {
  484. if(buf->size - buf->index < len)
  485. len = buf->size - buf->index;
  486. memcpy(data, (char *)buf->data + buf->index, len);
  487. buf->index += len;
  488. return len;
  489. }
  490. /* Matches Curl_recv signature */
  491. static ssize_t sec_recv(struct Curl_easy *data, int sockindex,
  492. char *buffer, size_t len, CURLcode *err)
  493. {
  494. size_t bytes_read;
  495. size_t total_read = 0;
  496. struct connectdata *conn = data->conn;
  497. *err = CURLE_OK;
  498. /* Handle clear text response. */
  499. if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR) {
  500. ssize_t nread;
  501. *err = Curl_conn_recv(data, sockindex, buffer, len, &nread);
  502. return nread;
  503. }
  504. if(conn->in_buffer.eof_flag) {
  505. conn->in_buffer.eof_flag = 0;
  506. return 0;
  507. }
  508. bytes_read = buffer_read(&conn->in_buffer, buffer, len);
  509. len -= bytes_read;
  510. total_read += bytes_read;
  511. buffer += bytes_read;
  512. while(len > 0) {
  513. if(read_data(data, sockindex, &conn->in_buffer))
  514. return -1;
  515. if(conn->in_buffer.size == 0) {
  516. if(bytes_read > 0)
  517. conn->in_buffer.eof_flag = 1;
  518. return bytes_read;
  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. }
  525. return total_read;
  526. }
  527. /* Send |length| bytes from |from| to the |fd| socket taking care of encoding
  528. and negotiating with the server. |from| can be NULL. */
  529. static void do_sec_send(struct Curl_easy *data, struct connectdata *conn,
  530. curl_socket_t fd, const char *from, int length)
  531. {
  532. int bytes, htonl_bytes; /* 32-bit integers for htonl */
  533. char *buffer = NULL;
  534. char *cmd_buffer;
  535. size_t cmd_size = 0;
  536. CURLcode error;
  537. enum protection_level prot_level = conn->data_prot;
  538. bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
  539. DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
  540. if(iscmd) {
  541. if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
  542. prot_level = PROT_PRIVATE;
  543. else
  544. prot_level = conn->command_prot;
  545. }
  546. bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
  547. (void **)&buffer);
  548. if(!buffer || bytes <= 0)
  549. return; /* error */
  550. if(iscmd) {
  551. error = Curl_base64_encode(buffer, curlx_sitouz(bytes),
  552. &cmd_buffer, &cmd_size);
  553. if(error) {
  554. free(buffer);
  555. return; /* error */
  556. }
  557. if(cmd_size > 0) {
  558. static const char *enc = "ENC ";
  559. static const char *mic = "MIC ";
  560. if(prot_level == PROT_PRIVATE)
  561. socket_write(data, fd, enc, 4);
  562. else
  563. socket_write(data, fd, mic, 4);
  564. socket_write(data, fd, cmd_buffer, cmd_size);
  565. socket_write(data, fd, "\r\n", 2);
  566. infof(data, "Send: %s%s", prot_level == PROT_PRIVATE?enc:mic,
  567. cmd_buffer);
  568. free(cmd_buffer);
  569. }
  570. }
  571. else {
  572. htonl_bytes = htonl(bytes);
  573. socket_write(data, fd, &htonl_bytes, sizeof(htonl_bytes));
  574. socket_write(data, fd, buffer, curlx_sitouz(bytes));
  575. }
  576. free(buffer);
  577. }
  578. static ssize_t sec_write(struct Curl_easy *data, struct connectdata *conn,
  579. curl_socket_t fd, const char *buffer, size_t length)
  580. {
  581. ssize_t tx = 0, len = conn->buffer_size;
  582. if(len <= 0)
  583. len = length;
  584. while(length) {
  585. if(length < (size_t)len)
  586. len = length;
  587. do_sec_send(data, conn, fd, buffer, curlx_sztosi(len));
  588. length -= len;
  589. buffer += len;
  590. tx += len;
  591. }
  592. return tx;
  593. }
  594. /* Matches Curl_send signature */
  595. static ssize_t sec_send(struct Curl_easy *data, int sockindex,
  596. const void *buffer, size_t len, CURLcode *err)
  597. {
  598. struct connectdata *conn = data->conn;
  599. curl_socket_t fd = conn->sock[sockindex];
  600. *err = CURLE_OK;
  601. return sec_write(data, conn, fd, buffer, len);
  602. }
  603. int Curl_sec_read_msg(struct Curl_easy *data, struct connectdata *conn,
  604. char *buffer, enum protection_level level)
  605. {
  606. /* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
  607. int */
  608. int decoded_len;
  609. char *buf;
  610. int ret_code = 0;
  611. size_t decoded_sz = 0;
  612. CURLcode error;
  613. (void) data;
  614. if(!conn->mech)
  615. /* not initialized, return error */
  616. return -1;
  617. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  618. error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
  619. if(error || decoded_sz == 0)
  620. return -1;
  621. if(decoded_sz > (size_t)INT_MAX) {
  622. free(buf);
  623. return -1;
  624. }
  625. decoded_len = curlx_uztosi(decoded_sz);
  626. decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
  627. level, conn);
  628. if(decoded_len <= 0) {
  629. free(buf);
  630. return -1;
  631. }
  632. {
  633. buf[decoded_len] = '\n';
  634. Curl_debug(data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
  635. }
  636. buf[decoded_len] = '\0';
  637. if(decoded_len <= 3)
  638. /* suspiciously short */
  639. return 0;
  640. if(buf[3] != '-')
  641. ret_code = atoi(buf);
  642. if(buf[decoded_len - 1] == '\n')
  643. buf[decoded_len - 1] = '\0';
  644. strcpy(buffer, buf);
  645. free(buf);
  646. return ret_code;
  647. }
  648. static int sec_set_protection_level(struct Curl_easy *data)
  649. {
  650. int code;
  651. struct connectdata *conn = data->conn;
  652. unsigned char level = conn->request_data_prot;
  653. DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
  654. if(!conn->sec_complete) {
  655. infof(data, "Trying to change the protection level after the"
  656. " completion of the data exchange.");
  657. return -1;
  658. }
  659. /* Bail out if we try to set up the same level */
  660. if(conn->data_prot == level)
  661. return 0;
  662. if(level) {
  663. char *pbsz;
  664. unsigned int buffer_size = 1 << 20; /* 1048576 */
  665. struct pingpong *pp = &conn->proto.ftpc.pp;
  666. char *line;
  667. code = ftp_send_command(data, "PBSZ %u", buffer_size);
  668. if(code < 0)
  669. return -1;
  670. if(code/100 != 2) {
  671. failf(data, "Failed to set the protection's buffer size.");
  672. return -1;
  673. }
  674. conn->buffer_size = buffer_size;
  675. line = Curl_dyn_ptr(&pp->recvbuf);
  676. pbsz = strstr(line, "PBSZ=");
  677. if(pbsz) {
  678. /* stick to default value if the check fails */
  679. if(ISDIGIT(pbsz[5]))
  680. buffer_size = atoi(&pbsz[5]);
  681. if(buffer_size < conn->buffer_size)
  682. conn->buffer_size = buffer_size;
  683. }
  684. }
  685. /* Now try to negotiate the protection level. */
  686. code = ftp_send_command(data, "PROT %c", level_to_char(level));
  687. if(code < 0)
  688. return -1;
  689. if(code/100 != 2) {
  690. failf(data, "Failed to set the protection level.");
  691. return -1;
  692. }
  693. conn->data_prot = level;
  694. if(level == PROT_PRIVATE)
  695. conn->command_prot = level;
  696. return 0;
  697. }
  698. int
  699. Curl_sec_request_prot(struct connectdata *conn, const char *level)
  700. {
  701. unsigned char l = name_to_level(level);
  702. if(l == PROT_NONE)
  703. return -1;
  704. DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
  705. conn->request_data_prot = l;
  706. return 0;
  707. }
  708. static CURLcode choose_mech(struct Curl_easy *data, struct connectdata *conn)
  709. {
  710. int ret;
  711. void *tmp_allocation;
  712. const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
  713. tmp_allocation = realloc(conn->app_data, mech->size);
  714. if(!tmp_allocation) {
  715. failf(data, "Failed realloc of size %zu", mech->size);
  716. mech = NULL;
  717. return CURLE_OUT_OF_MEMORY;
  718. }
  719. conn->app_data = tmp_allocation;
  720. if(mech->init) {
  721. ret = mech->init(conn->app_data);
  722. if(ret) {
  723. infof(data, "Failed initialization for %s. Skipping it.",
  724. mech->name);
  725. return CURLE_FAILED_INIT;
  726. }
  727. }
  728. infof(data, "Trying mechanism %s...", mech->name);
  729. ret = ftp_send_command(data, "AUTH %s", mech->name);
  730. if(ret < 0)
  731. return CURLE_COULDNT_CONNECT;
  732. if(ret/100 != 3) {
  733. switch(ret) {
  734. case 504:
  735. infof(data, "Mechanism %s is not supported by the server (server "
  736. "returned ftp code: 504).", mech->name);
  737. break;
  738. case 534:
  739. infof(data, "Mechanism %s was rejected by the server (server returned "
  740. "ftp code: 534).", mech->name);
  741. break;
  742. default:
  743. if(ret/100 == 5) {
  744. infof(data, "server does not support the security extensions");
  745. return CURLE_USE_SSL_FAILED;
  746. }
  747. break;
  748. }
  749. return CURLE_LOGIN_DENIED;
  750. }
  751. /* Authenticate */
  752. ret = mech->auth(conn->app_data, data, conn);
  753. if(ret != AUTH_CONTINUE) {
  754. if(ret != AUTH_OK) {
  755. /* Mechanism has dumped the error to stderr, don't error here. */
  756. return CURLE_USE_SSL_FAILED;
  757. }
  758. DEBUGASSERT(ret == AUTH_OK);
  759. conn->mech = mech;
  760. conn->sec_complete = 1;
  761. conn->recv[FIRSTSOCKET] = sec_recv;
  762. conn->send[FIRSTSOCKET] = sec_send;
  763. conn->recv[SECONDARYSOCKET] = sec_recv;
  764. conn->send[SECONDARYSOCKET] = sec_send;
  765. conn->command_prot = PROT_SAFE;
  766. /* Set the requested protection level */
  767. /* BLOCKING */
  768. (void)sec_set_protection_level(data);
  769. }
  770. return CURLE_OK;
  771. }
  772. CURLcode
  773. Curl_sec_login(struct Curl_easy *data, struct connectdata *conn)
  774. {
  775. return choose_mech(data, conn);
  776. }
  777. void
  778. Curl_sec_end(struct connectdata *conn)
  779. {
  780. if(conn->mech && conn->mech->end)
  781. conn->mech->end(conn->app_data);
  782. free(conn->app_data);
  783. conn->app_data = NULL;
  784. if(conn->in_buffer.data) {
  785. free(conn->in_buffer.data);
  786. conn->in_buffer.data = NULL;
  787. conn->in_buffer.size = 0;
  788. conn->in_buffer.index = 0;
  789. conn->in_buffer.eof_flag = 0;
  790. }
  791. conn->sec_complete = 0;
  792. conn->data_prot = PROT_CLEAR;
  793. conn->mech = NULL;
  794. }
  795. #endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */