VirtualBox

source: vbox/trunk/src/libs/curl-7.87.0/lib/krb5.c@ 98326

Last change on this file since 98326 was 98326, checked in by vboxsync, 2 years ago

curl-7.87.0: Applied and adjusted our curl changes to 7.83.1. bugref:10356

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette