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