krb5: merged security.c and krb specific FTP functions in here

These two files were always tightly connected and it was hard to
understand what went into which. This also allows us to make the
ftpsend() function static (moved from ftp.c).

Removed security.c
Renamed curl_sec.h to krb5.h

Closes #5987
This commit is contained in:
Daniel Stenberg 2020-09-21 13:59:33 +02:00
parent 6434a73984
commit f4873ebd0b
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2
9 changed files with 595 additions and 682 deletions

View File

@ -326,9 +326,9 @@ FTP
Kerberos
========
Kerberos support is mainly in `lib/krb5.c` and `lib/security.c` but also
`curl_sasl_sspi.c` and `curl_sasl_gssapi.c` for the email protocols and
`socks_gssapi.c` and `socks_sspi.c` for SOCKS5 proxy specifics.
Kerberos support is mainly in `lib/krb5.c` but also `curl_sasl_sspi.c` and
`curl_sasl_gssapi.c` for the email protocols and `socks_gssapi.c` and
`socks_sspi.c` for SOCKS5 proxy specifics.
<a name="telnet"></a>
TELNET

View File

@ -50,15 +50,6 @@ endif()
# # strtoofft.c - specify later
# )
# # if we have Kerberos 4, right now this is never on
# #OPTION(CURL_KRB4 "Use Kerberos 4" OFF)
# IF(CURL_KRB4)
# SET(CSOURCES ${CSOURCES}
# krb4.c
# security.c
# )
# ENDIF(CURL_KRB4)
# #OPTION(CURL_MALLOC_DEBUG "Debug mallocs in Curl" OFF)
# MARK_AS_ADVANCED(CURL_MALLOC_DEBUG)
# IF(CURL_MALLOC_DEBUG)

View File

@ -56,7 +56,7 @@ LIB_CFILES = altsvc.c amigaos.c asyn-ares.c asyn-thread.c base64.c \
http_ntlm.c http_proxy.c idn_win32.c if2ip.c imap.c inet_ntop.c inet_pton.c \
krb5.c ldap.c llist.c md4.c md5.c memdebug.c mime.c mprintf.c mqtt.c \
multi.c netrc.c non-ascii.c nonblock.c openldap.c parsedate.c pingpong.c \
pop3.c progress.c psl.c doh.c rand.c rename.c rtsp.c security.c select.c \
pop3.c progress.c psl.c doh.c rand.c rename.c rtsp.c select.c \
sendf.c setopt.c sha256.c share.c slist.c smb.c smtp.c socketpair.c socks.c \
socks_gssapi.c socks_sspi.c speedcheck.c splay.c strcase.c strdup.c \
strerror.c strtok.c strtoofft.c system_win32.c telnet.c tftp.c timeval.c \
@ -68,7 +68,7 @@ LIB_HFILES = altsvc.h amigaos.h arpa_telnet.h asyn.h conncache.h connect.h \
curl_des.h curl_endian.h curl_fnmatch.h curl_get_line.h curl_gethostname.h \
curl_gssapi.h curl_hmac.h curl_ldap.h curl_md4.h curl_md5.h curl_memory.h \
curl_memrchr.h curl_multibyte.h curl_ntlm_core.h curl_ntlm_wb.h curl_path.h \
curl_printf.h curl_range.h curl_rtmp.h curl_sasl.h curl_sec.h curl_setup.h \
curl_printf.h curl_range.h curl_rtmp.h curl_sasl.h krb5.h curl_setup.h \
curl_setup_once.h curl_sha256.h curl_sspi.h curl_threads.h curlx.h dict.h \
dotdot.h easyif.h escape.h file.h fileinfo.h formdata.h ftp.h url.h \
ftplistparser.h getinfo.h gopher.h hash.h hostcheck.h hostip.h http.h \

View File

@ -59,7 +59,7 @@
#include "fileinfo.h"
#include "ftplistparser.h"
#include "curl_range.h"
#include "curl_sec.h"
#include "krb5.h"
#include "strtoofft.h"
#include "strcase.h"
#include "vtls/vtls.h"
@ -3947,64 +3947,6 @@ static CURLcode ftp_do(struct connectdata *conn, bool *done)
return result;
}
CURLcode Curl_ftpsend(struct connectdata *conn, const char *cmd)
{
ssize_t bytes_written;
#define SBUF_SIZE 1024
char s[SBUF_SIZE];
size_t write_len;
char *sptr = s;
CURLcode result = CURLE_OK;
#ifdef HAVE_GSSAPI
enum protection_level data_sec = conn->data_prot;
#endif
if(!cmd)
return CURLE_BAD_FUNCTION_ARGUMENT;
write_len = strlen(cmd);
if(!write_len || write_len > (sizeof(s) -3))
return CURLE_BAD_FUNCTION_ARGUMENT;
memcpy(&s, cmd, write_len);
strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
write_len += 2;
bytes_written = 0;
result = Curl_convert_to_network(conn->data, s, write_len);
/* Curl_convert_to_network calls failf if unsuccessful */
if(result)
return result;
for(;;) {
#ifdef HAVE_GSSAPI
conn->data_prot = PROT_CMD;
#endif
result = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
&bytes_written);
#ifdef HAVE_GSSAPI
DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
conn->data_prot = data_sec;
#endif
if(result)
break;
if(conn->data->set.verbose)
Curl_debug(conn->data, CURLINFO_HEADER_OUT, sptr, (size_t)bytes_written);
if(bytes_written != (ssize_t)write_len) {
write_len -= bytes_written;
sptr += bytes_written;
}
else
break;
}
return result;
}
/***********************************************************************
*
* ftp_quit()
@ -4068,22 +4010,14 @@ static CURLcode ftp_disconnect(struct connectdata *conn, bool dead_connection)
if(data->state.most_recent_ftp_entrypath == ftpc->entrypath) {
data->state.most_recent_ftp_entrypath = NULL;
}
free(ftpc->entrypath);
ftpc->entrypath = NULL;
Curl_safefree(ftpc->entrypath);
}
freedirs(ftpc);
free(ftpc->prevpath);
ftpc->prevpath = NULL;
free(ftpc->server_os);
ftpc->server_os = NULL;
Curl_safefree(ftpc->prevpath);
Curl_safefree(ftpc->server_os);
Curl_pp_disconnect(pp);
#ifdef HAVE_GSSAPI
Curl_sec_end(conn);
#endif
return CURLE_OK;
}

View File

@ -31,7 +31,6 @@ extern const struct Curl_handler Curl_handler_ftp;
extern const struct Curl_handler Curl_handler_ftps;
#endif
CURLcode Curl_ftpsend(struct connectdata *, const char *cmd);
CURLcode Curl_GetFTPResponse(ssize_t *nread, struct connectdata *conn,
int *ftpcode);
#endif /* CURL_DISABLE_FTP */

View File

@ -2,7 +2,7 @@
*
* Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
* Copyright (c) 2004 - 2019 Daniel Stenberg
* Copyright (c) 2004 - 2020 Daniel Stenberg
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -45,14 +45,74 @@
#include "ftp.h"
#include "curl_gssapi.h"
#include "sendf.h"
#include "curl_sec.h"
#include "krb5.h"
#include "warnless.h"
#include "non-ascii.h"
#include "strcase.h"
#include "strdup.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
static CURLcode ftpsend(struct connectdata *conn, const char *cmd)
{
ssize_t bytes_written;
#define SBUF_SIZE 1024
char s[SBUF_SIZE];
size_t write_len;
char *sptr = s;
CURLcode result = CURLE_OK;
#ifdef HAVE_GSSAPI
enum protection_level data_sec = conn->data_prot;
#endif
if(!cmd)
return CURLE_BAD_FUNCTION_ARGUMENT;
write_len = strlen(cmd);
if(!write_len || write_len > (sizeof(s) -3))
return CURLE_BAD_FUNCTION_ARGUMENT;
memcpy(&s, cmd, write_len);
strcpy(&s[write_len], "\r\n"); /* append a trailing CRLF */
write_len += 2;
bytes_written = 0;
result = Curl_convert_to_network(conn->data, s, write_len);
/* Curl_convert_to_network calls failf if unsuccessful */
if(result)
return result;
for(;;) {
#ifdef HAVE_GSSAPI
conn->data_prot = PROT_CMD;
#endif
result = Curl_write(conn, conn->sock[FIRSTSOCKET], sptr, write_len,
&bytes_written);
#ifdef HAVE_GSSAPI
DEBUGASSERT(data_sec > PROT_NONE && data_sec < PROT_LAST);
conn->data_prot = data_sec;
#endif
if(result)
break;
if(conn->data->set.verbose)
Curl_debug(conn->data, CURLINFO_HEADER_OUT, sptr, (size_t)bytes_written);
if(bytes_written != (ssize_t)write_len) {
write_len -= bytes_written;
sptr += bytes_written;
}
else
break;
}
return result;
}
static int
krb5_init(void *app_data)
{
@ -183,7 +243,7 @@ krb5_auth(void *app_data, struct connectdata *conn)
for(;;) {
/* this really shouldn't be repeated here, but can't help it */
if(service == srv_host) {
result = Curl_ftpsend(conn, "AUTH GSSAPI");
result = ftpsend(conn, "AUTH GSSAPI");
if(result)
return -2;
@ -260,7 +320,7 @@ krb5_auth(void *app_data, struct connectdata *conn)
cmd = aprintf("ADAT %s", p);
if(cmd)
result = Curl_ftpsend(conn, cmd);
result = ftpsend(conn, cmd);
else
result = CURLE_OUT_OF_MEMORY;
@ -326,16 +386,524 @@ static void krb5_end(void *app_data)
}
}
struct Curl_sec_client_mech Curl_krb5_client_mech = {
"GSSAPI",
sizeof(gss_ctx_id_t),
krb5_init,
krb5_auth,
krb5_end,
krb5_check_prot,
krb5_overhead,
krb5_encode,
krb5_decode
static struct Curl_sec_client_mech Curl_krb5_client_mech = {
"GSSAPI",
sizeof(gss_ctx_id_t),
krb5_init,
krb5_auth,
krb5_end,
krb5_check_prot,
krb5_overhead,
krb5_encode,
krb5_decode
};
static const struct {
enum protection_level level;
const char *name;
} level_names[] = {
{ PROT_CLEAR, "clear" },
{ PROT_SAFE, "safe" },
{ PROT_CONFIDENTIAL, "confidential" },
{ PROT_PRIVATE, "private" }
};
static enum protection_level
name_to_level(const char *name)
{
int i;
for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
if(checkprefix(name, level_names[i].name))
return level_names[i].level;
return PROT_NONE;
}
/* Convert a protocol |level| to its char representation.
We take an int to catch programming mistakes. */
static char level_to_char(int level)
{
switch(level) {
case PROT_CLEAR:
return 'C';
case PROT_SAFE:
return 'S';
case PROT_CONFIDENTIAL:
return 'E';
case PROT_PRIVATE:
return 'P';
case PROT_CMD:
/* Fall through */
default:
/* Those 2 cases should not be reached! */
break;
}
DEBUGASSERT(0);
/* Default to the most secure alternative. */
return 'P';
}
/* Send an FTP command defined by |message| and the optional arguments. The
function returns the ftp_code. If an error occurs, -1 is returned. */
static int ftp_send_command(struct connectdata *conn, const char *message, ...)
{
int ftp_code;
ssize_t nread = 0;
va_list args;
char print_buffer[50];
va_start(args, message);
mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
va_end(args);
if(ftpsend(conn, print_buffer)) {
ftp_code = -1;
}
else {
if(Curl_GetFTPResponse(&nread, conn, &ftp_code))
ftp_code = -1;
}
(void)nread; /* Unused */
return ftp_code;
}
/* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
saying whether an error occurred or CURLE_OK if |len| was read. */
static CURLcode
socket_read(curl_socket_t fd, void *to, size_t len)
{
char *to_p = to;
CURLcode result;
ssize_t nread = 0;
while(len > 0) {
result = Curl_read_plain(fd, to_p, len, &nread);
if(!result) {
len -= nread;
to_p += nread;
}
else {
if(result == CURLE_AGAIN)
continue;
return result;
}
}
return CURLE_OK;
}
/* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
CURLcode saying whether an error occurred or CURLE_OK if |len| was
written. */
static CURLcode
socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
size_t len)
{
const char *to_p = to;
CURLcode result;
ssize_t written;
while(len > 0) {
result = Curl_write_plain(conn, fd, to_p, len, &written);
if(!result) {
len -= written;
to_p += written;
}
else {
if(result == CURLE_AGAIN)
continue;
return result;
}
}
return CURLE_OK;
}
static CURLcode read_data(struct connectdata *conn,
curl_socket_t fd,
struct krb5buffer *buf)
{
int len;
CURLcode result;
result = socket_read(fd, &len, sizeof(len));
if(result)
return result;
if(len) {
/* only realloc if there was a length */
len = ntohl(len);
buf->data = Curl_saferealloc(buf->data, len);
}
if(!len || !buf->data)
return CURLE_OUT_OF_MEMORY;
result = socket_read(fd, buf->data, len);
if(result)
return result;
buf->size = conn->mech->decode(conn->app_data, buf->data, len,
conn->data_prot, conn);
buf->index = 0;
return CURLE_OK;
}
static size_t
buffer_read(struct krb5buffer *buf, void *data, size_t len)
{
if(buf->size - buf->index < len)
len = buf->size - buf->index;
memcpy(data, (char *)buf->data + buf->index, len);
buf->index += len;
return len;
}
/* Matches Curl_recv signature */
static ssize_t sec_recv(struct connectdata *conn, int sockindex,
char *buffer, size_t len, CURLcode *err)
{
size_t bytes_read;
size_t total_read = 0;
curl_socket_t fd = conn->sock[sockindex];
*err = CURLE_OK;
/* Handle clear text response. */
if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
return sread(fd, buffer, len);
if(conn->in_buffer.eof_flag) {
conn->in_buffer.eof_flag = 0;
return 0;
}
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
len -= bytes_read;
total_read += bytes_read;
buffer += bytes_read;
while(len > 0) {
if(read_data(conn, fd, &conn->in_buffer))
return -1;
if(conn->in_buffer.size == 0) {
if(bytes_read > 0)
conn->in_buffer.eof_flag = 1;
return bytes_read;
}
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
len -= bytes_read;
total_read += bytes_read;
buffer += bytes_read;
}
return total_read;
}
/* Send |length| bytes from |from| to the |fd| socket taking care of encoding
and negotiating with the server. |from| can be NULL. */
static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
const char *from, int length)
{
int bytes, htonl_bytes; /* 32-bit integers for htonl */
char *buffer = NULL;
char *cmd_buffer;
size_t cmd_size = 0;
CURLcode error;
enum protection_level prot_level = conn->data_prot;
bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
if(iscmd) {
if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
prot_level = PROT_PRIVATE;
else
prot_level = conn->command_prot;
}
bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
(void **)&buffer);
if(!buffer || bytes <= 0)
return; /* error */
if(iscmd) {
error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes),
&cmd_buffer, &cmd_size);
if(error) {
free(buffer);
return; /* error */
}
if(cmd_size > 0) {
static const char *enc = "ENC ";
static const char *mic = "MIC ";
if(prot_level == PROT_PRIVATE)
socket_write(conn, fd, enc, 4);
else
socket_write(conn, fd, mic, 4);
socket_write(conn, fd, cmd_buffer, cmd_size);
socket_write(conn, fd, "\r\n", 2);
infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
cmd_buffer);
free(cmd_buffer);
}
}
else {
htonl_bytes = htonl(bytes);
socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
socket_write(conn, fd, buffer, curlx_sitouz(bytes));
}
free(buffer);
}
static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
const char *buffer, size_t length)
{
ssize_t tx = 0, len = conn->buffer_size;
len -= conn->mech->overhead(conn->app_data, conn->data_prot,
curlx_sztosi(len));
if(len <= 0)
len = length;
while(length) {
if(length < (size_t)len)
len = length;
do_sec_send(conn, fd, buffer, curlx_sztosi(len));
length -= len;
buffer += len;
tx += len;
}
return tx;
}
/* Matches Curl_send signature */
static ssize_t sec_send(struct connectdata *conn, int sockindex,
const void *buffer, size_t len, CURLcode *err)
{
curl_socket_t fd = conn->sock[sockindex];
*err = CURLE_OK;
return sec_write(conn, fd, buffer, len);
}
int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
enum protection_level level)
{
/* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
int */
int decoded_len;
char *buf;
int ret_code = 0;
size_t decoded_sz = 0;
CURLcode error;
if(!conn->mech)
/* not inititalized, return error */
return -1;
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
if(error || decoded_sz == 0)
return -1;
if(decoded_sz > (size_t)INT_MAX) {
free(buf);
return -1;
}
decoded_len = curlx_uztosi(decoded_sz);
decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
level, conn);
if(decoded_len <= 0) {
free(buf);
return -1;
}
if(conn->data->set.verbose) {
buf[decoded_len] = '\n';
Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
}
buf[decoded_len] = '\0';
if(decoded_len <= 3)
/* suspiciously short */
return 0;
if(buf[3] != '-')
/* safe to ignore return code */
(void)sscanf(buf, "%d", &ret_code);
if(buf[decoded_len - 1] == '\n')
buf[decoded_len - 1] = '\0';
strcpy(buffer, buf);
free(buf);
return ret_code;
}
static int sec_set_protection_level(struct connectdata *conn)
{
int code;
enum protection_level level = conn->request_data_prot;
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
if(!conn->sec_complete) {
infof(conn->data, "Trying to change the protection level after the"
" completion of the data exchange.\n");
return -1;
}
/* Bail out if we try to set up the same level */
if(conn->data_prot == level)
return 0;
if(level) {
char *pbsz;
static unsigned int buffer_size = 1 << 20; /* 1048576 */
code = ftp_send_command(conn, "PBSZ %u", buffer_size);
if(code < 0)
return -1;
if(code/100 != 2) {
failf(conn->data, "Failed to set the protection's buffer size.");
return -1;
}
conn->buffer_size = buffer_size;
pbsz = strstr(conn->data->state.buffer, "PBSZ=");
if(pbsz) {
/* ignore return code, use default value if it fails */
(void)sscanf(pbsz, "PBSZ=%u", &buffer_size);
if(buffer_size < conn->buffer_size)
conn->buffer_size = buffer_size;
}
}
/* Now try to negiociate the protection level. */
code = ftp_send_command(conn, "PROT %c", level_to_char(level));
if(code < 0)
return -1;
if(code/100 != 2) {
failf(conn->data, "Failed to set the protection level.");
return -1;
}
conn->data_prot = level;
if(level == PROT_PRIVATE)
conn->command_prot = level;
return 0;
}
int
Curl_sec_request_prot(struct connectdata *conn, const char *level)
{
enum protection_level l = name_to_level(level);
if(l == PROT_NONE)
return -1;
DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
conn->request_data_prot = l;
return 0;
}
static CURLcode choose_mech(struct connectdata *conn)
{
int ret;
struct Curl_easy *data = conn->data;
void *tmp_allocation;
const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
tmp_allocation = realloc(conn->app_data, mech->size);
if(tmp_allocation == NULL) {
failf(data, "Failed realloc of size %zu", mech->size);
mech = NULL;
return CURLE_OUT_OF_MEMORY;
}
conn->app_data = tmp_allocation;
if(mech->init) {
ret = mech->init(conn->app_data);
if(ret) {
infof(data, "Failed initialization for %s. Skipping it.\n",
mech->name);
return CURLE_FAILED_INIT;
}
}
infof(data, "Trying mechanism %s...\n", mech->name);
ret = ftp_send_command(conn, "AUTH %s", mech->name);
if(ret < 0)
return CURLE_COULDNT_CONNECT;
if(ret/100 != 3) {
switch(ret) {
case 504:
infof(data, "Mechanism %s is not supported by the server (server "
"returned ftp code: 504).\n", mech->name);
break;
case 534:
infof(data, "Mechanism %s was rejected by the server (server returned "
"ftp code: 534).\n", mech->name);
break;
default:
if(ret/100 == 5) {
infof(data, "server does not support the security extensions\n");
return CURLE_USE_SSL_FAILED;
}
break;
}
return CURLE_LOGIN_DENIED;
}
/* Authenticate */
ret = mech->auth(conn->app_data, conn);
if(ret != AUTH_CONTINUE) {
if(ret != AUTH_OK) {
/* Mechanism has dumped the error to stderr, don't error here. */
return CURLE_USE_SSL_FAILED;
}
DEBUGASSERT(ret == AUTH_OK);
conn->mech = mech;
conn->sec_complete = 1;
conn->recv[FIRSTSOCKET] = sec_recv;
conn->send[FIRSTSOCKET] = sec_send;
conn->recv[SECONDARYSOCKET] = sec_recv;
conn->send[SECONDARYSOCKET] = sec_send;
conn->command_prot = PROT_SAFE;
/* Set the requested protection level */
/* BLOCKING */
(void)sec_set_protection_level(conn);
}
return CURLE_OK;
}
CURLcode
Curl_sec_login(struct connectdata *conn)
{
return choose_mech(conn);
}
void
Curl_sec_end(struct connectdata *conn)
{
if(conn->mech != NULL && conn->mech->end)
conn->mech->end(conn->app_data);
free(conn->app_data);
conn->app_data = NULL;
if(conn->in_buffer.data) {
free(conn->in_buffer.data);
conn->in_buffer.data = NULL;
conn->in_buffer.size = 0;
conn->in_buffer.index = 0;
conn->in_buffer.eof_flag = 0;
}
conn->sec_complete = 0;
conn->data_prot = PROT_CLEAR;
conn->mech = NULL;
}
#endif /* HAVE_GSSAPI && !CURL_DISABLE_FTP */

View File

@ -7,7 +7,7 @@
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
* Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
@ -44,8 +44,8 @@ int Curl_sec_read_msg(struct connectdata *conn, char *,
void Curl_sec_end(struct connectdata *);
CURLcode Curl_sec_login(struct connectdata *);
int Curl_sec_request_prot(struct connectdata *conn, const char *level);
extern struct Curl_sec_client_mech Curl_krb5_client_mech;
#else
#define Curl_sec_end(x)
#endif
#endif /* HEADER_CURL_SECURITY_H */

View File

@ -1,579 +0,0 @@
/* This source code was modified by Martin Hedenfalk <mhe@stacken.kth.se> for
* use in Curl. His latest changes were done 2000-09-18.
*
* It has since been patched and modified a lot by Daniel Stenberg
* <daniel@haxx.se> to make it better applied to curl conditions, and to make
* it not use globals, pollute name space and more. This source code awaits a
* rewrite to work around the paragraph 2 in the BSD licenses as explained
* below.
*
* Copyright (c) 1998, 1999 Kungliga Tekniska Högskolan
* (Royal Institute of Technology, Stockholm, Sweden).
*
* Copyright (C) 2001 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. */
#include "curl_setup.h"
#ifndef CURL_DISABLE_FTP
#ifdef HAVE_GSSAPI
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#include <limits.h>
#include "urldata.h"
#include "curl_base64.h"
#include "curl_memory.h"
#include "curl_sec.h"
#include "ftp.h"
#include "sendf.h"
#include "strcase.h"
#include "warnless.h"
#include "strdup.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
static const struct {
enum protection_level level;
const char *name;
} level_names[] = {
{ PROT_CLEAR, "clear" },
{ PROT_SAFE, "safe" },
{ PROT_CONFIDENTIAL, "confidential" },
{ PROT_PRIVATE, "private" }
};
static enum protection_level
name_to_level(const char *name)
{
int i;
for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
if(checkprefix(name, level_names[i].name))
return level_names[i].level;
return PROT_NONE;
}
/* Convert a protocol |level| to its char representation.
We take an int to catch programming mistakes. */
static char level_to_char(int level)
{
switch(level) {
case PROT_CLEAR:
return 'C';
case PROT_SAFE:
return 'S';
case PROT_CONFIDENTIAL:
return 'E';
case PROT_PRIVATE:
return 'P';
case PROT_CMD:
/* Fall through */
default:
/* Those 2 cases should not be reached! */
break;
}
DEBUGASSERT(0);
/* Default to the most secure alternative. */
return 'P';
}
/* Send an FTP command defined by |message| and the optional arguments. The
function returns the ftp_code. If an error occurs, -1 is returned. */
static int ftp_send_command(struct connectdata *conn, const char *message, ...)
{
int ftp_code;
ssize_t nread = 0;
va_list args;
char print_buffer[50];
va_start(args, message);
mvsnprintf(print_buffer, sizeof(print_buffer), message, args);
va_end(args);
if(Curl_ftpsend(conn, print_buffer)) {
ftp_code = -1;
}
else {
if(Curl_GetFTPResponse(&nread, conn, &ftp_code))
ftp_code = -1;
}
(void)nread; /* Unused */
return ftp_code;
}
/* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
saying whether an error occurred or CURLE_OK if |len| was read. */
static CURLcode
socket_read(curl_socket_t fd, void *to, size_t len)
{
char *to_p = to;
CURLcode result;
ssize_t nread = 0;
while(len > 0) {
result = Curl_read_plain(fd, to_p, len, &nread);
if(!result) {
len -= nread;
to_p += nread;
}
else {
if(result == CURLE_AGAIN)
continue;
return result;
}
}
return CURLE_OK;
}
/* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
CURLcode saying whether an error occurred or CURLE_OK if |len| was
written. */
static CURLcode
socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
size_t len)
{
const char *to_p = to;
CURLcode result;
ssize_t written;
while(len > 0) {
result = Curl_write_plain(conn, fd, to_p, len, &written);
if(!result) {
len -= written;
to_p += written;
}
else {
if(result == CURLE_AGAIN)
continue;
return result;
}
}
return CURLE_OK;
}
static CURLcode read_data(struct connectdata *conn,
curl_socket_t fd,
struct krb5buffer *buf)
{
int len;
CURLcode result;
result = socket_read(fd, &len, sizeof(len));
if(result)
return result;
if(len) {
/* only realloc if there was a length */
len = ntohl(len);
buf->data = Curl_saferealloc(buf->data, len);
}
if(!len || !buf->data)
return CURLE_OUT_OF_MEMORY;
result = socket_read(fd, buf->data, len);
if(result)
return result;
buf->size = conn->mech->decode(conn->app_data, buf->data, len,
conn->data_prot, conn);
buf->index = 0;
return CURLE_OK;
}
static size_t
buffer_read(struct krb5buffer *buf, void *data, size_t len)
{
if(buf->size - buf->index < len)
len = buf->size - buf->index;
memcpy(data, (char *)buf->data + buf->index, len);
buf->index += len;
return len;
}
/* Matches Curl_recv signature */
static ssize_t sec_recv(struct connectdata *conn, int sockindex,
char *buffer, size_t len, CURLcode *err)
{
size_t bytes_read;
size_t total_read = 0;
curl_socket_t fd = conn->sock[sockindex];
*err = CURLE_OK;
/* Handle clear text response. */
if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
return sread(fd, buffer, len);
if(conn->in_buffer.eof_flag) {
conn->in_buffer.eof_flag = 0;
return 0;
}
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
len -= bytes_read;
total_read += bytes_read;
buffer += bytes_read;
while(len > 0) {
if(read_data(conn, fd, &conn->in_buffer))
return -1;
if(conn->in_buffer.size == 0) {
if(bytes_read > 0)
conn->in_buffer.eof_flag = 1;
return bytes_read;
}
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
len -= bytes_read;
total_read += bytes_read;
buffer += bytes_read;
}
return total_read;
}
/* Send |length| bytes from |from| to the |fd| socket taking care of encoding
and negotiating with the server. |from| can be NULL. */
static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
const char *from, int length)
{
int bytes, htonl_bytes; /* 32-bit integers for htonl */
char *buffer = NULL;
char *cmd_buffer;
size_t cmd_size = 0;
CURLcode error;
enum protection_level prot_level = conn->data_prot;
bool iscmd = (prot_level == PROT_CMD)?TRUE:FALSE;
DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
if(iscmd) {
if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
prot_level = PROT_PRIVATE;
else
prot_level = conn->command_prot;
}
bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
(void **)&buffer);
if(!buffer || bytes <= 0)
return; /* error */
if(iscmd) {
error = Curl_base64_encode(conn->data, buffer, curlx_sitouz(bytes),
&cmd_buffer, &cmd_size);
if(error) {
free(buffer);
return; /* error */
}
if(cmd_size > 0) {
static const char *enc = "ENC ";
static const char *mic = "MIC ";
if(prot_level == PROT_PRIVATE)
socket_write(conn, fd, enc, 4);
else
socket_write(conn, fd, mic, 4);
socket_write(conn, fd, cmd_buffer, cmd_size);
socket_write(conn, fd, "\r\n", 2);
infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
cmd_buffer);
free(cmd_buffer);
}
}
else {
htonl_bytes = htonl(bytes);
socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
socket_write(conn, fd, buffer, curlx_sitouz(bytes));
}
free(buffer);
}
static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
const char *buffer, size_t length)
{
ssize_t tx = 0, len = conn->buffer_size;
len -= conn->mech->overhead(conn->app_data, conn->data_prot,
curlx_sztosi(len));
if(len <= 0)
len = length;
while(length) {
if(length < (size_t)len)
len = length;
do_sec_send(conn, fd, buffer, curlx_sztosi(len));
length -= len;
buffer += len;
tx += len;
}
return tx;
}
/* Matches Curl_send signature */
static ssize_t sec_send(struct connectdata *conn, int sockindex,
const void *buffer, size_t len, CURLcode *err)
{
curl_socket_t fd = conn->sock[sockindex];
*err = CURLE_OK;
return sec_write(conn, fd, buffer, len);
}
int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
enum protection_level level)
{
/* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
int */
int decoded_len;
char *buf;
int ret_code = 0;
size_t decoded_sz = 0;
CURLcode error;
if(!conn->mech)
/* not inititalized, return error */
return -1;
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
error = Curl_base64_decode(buffer + 4, (unsigned char **)&buf, &decoded_sz);
if(error || decoded_sz == 0)
return -1;
if(decoded_sz > (size_t)INT_MAX) {
free(buf);
return -1;
}
decoded_len = curlx_uztosi(decoded_sz);
decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
level, conn);
if(decoded_len <= 0) {
free(buf);
return -1;
}
if(conn->data->set.verbose) {
buf[decoded_len] = '\n';
Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1);
}
buf[decoded_len] = '\0';
if(decoded_len <= 3)
/* suspiciously short */
return 0;
if(buf[3] != '-')
/* safe to ignore return code */
(void)sscanf(buf, "%d", &ret_code);
if(buf[decoded_len - 1] == '\n')
buf[decoded_len - 1] = '\0';
strcpy(buffer, buf);
free(buf);
return ret_code;
}
static int sec_set_protection_level(struct connectdata *conn)
{
int code;
enum protection_level level = conn->request_data_prot;
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
if(!conn->sec_complete) {
infof(conn->data, "Trying to change the protection level after the"
" completion of the data exchange.\n");
return -1;
}
/* Bail out if we try to set up the same level */
if(conn->data_prot == level)
return 0;
if(level) {
char *pbsz;
static unsigned int buffer_size = 1 << 20; /* 1048576 */
code = ftp_send_command(conn, "PBSZ %u", buffer_size);
if(code < 0)
return -1;
if(code/100 != 2) {
failf(conn->data, "Failed to set the protection's buffer size.");
return -1;
}
conn->buffer_size = buffer_size;
pbsz = strstr(conn->data->state.buffer, "PBSZ=");
if(pbsz) {
/* ignore return code, use default value if it fails */
(void)sscanf(pbsz, "PBSZ=%u", &buffer_size);
if(buffer_size < conn->buffer_size)
conn->buffer_size = buffer_size;
}
}
/* Now try to negiociate the protection level. */
code = ftp_send_command(conn, "PROT %c", level_to_char(level));
if(code < 0)
return -1;
if(code/100 != 2) {
failf(conn->data, "Failed to set the protection level.");
return -1;
}
conn->data_prot = level;
if(level == PROT_PRIVATE)
conn->command_prot = level;
return 0;
}
int
Curl_sec_request_prot(struct connectdata *conn, const char *level)
{
enum protection_level l = name_to_level(level);
if(l == PROT_NONE)
return -1;
DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
conn->request_data_prot = l;
return 0;
}
static CURLcode choose_mech(struct connectdata *conn)
{
int ret;
struct Curl_easy *data = conn->data;
void *tmp_allocation;
const struct Curl_sec_client_mech *mech = &Curl_krb5_client_mech;
tmp_allocation = realloc(conn->app_data, mech->size);
if(tmp_allocation == NULL) {
failf(data, "Failed realloc of size %zu", mech->size);
mech = NULL;
return CURLE_OUT_OF_MEMORY;
}
conn->app_data = tmp_allocation;
if(mech->init) {
ret = mech->init(conn->app_data);
if(ret) {
infof(data, "Failed initialization for %s. Skipping it.\n",
mech->name);
return CURLE_FAILED_INIT;
}
}
infof(data, "Trying mechanism %s...\n", mech->name);
ret = ftp_send_command(conn, "AUTH %s", mech->name);
if(ret < 0)
return CURLE_COULDNT_CONNECT;
if(ret/100 != 3) {
switch(ret) {
case 504:
infof(data, "Mechanism %s is not supported by the server (server "
"returned ftp code: 504).\n", mech->name);
break;
case 534:
infof(data, "Mechanism %s was rejected by the server (server returned "
"ftp code: 534).\n", mech->name);
break;
default:
if(ret/100 == 5) {
infof(data, "server does not support the security extensions\n");
return CURLE_USE_SSL_FAILED;
}
break;
}
return CURLE_LOGIN_DENIED;
}
/* Authenticate */
ret = mech->auth(conn->app_data, conn);
if(ret != AUTH_CONTINUE) {
if(ret != AUTH_OK) {
/* Mechanism has dumped the error to stderr, don't error here. */
return CURLE_USE_SSL_FAILED;
}
DEBUGASSERT(ret == AUTH_OK);
conn->mech = mech;
conn->sec_complete = 1;
conn->recv[FIRSTSOCKET] = sec_recv;
conn->send[FIRSTSOCKET] = sec_send;
conn->recv[SECONDARYSOCKET] = sec_recv;
conn->send[SECONDARYSOCKET] = sec_send;
conn->command_prot = PROT_SAFE;
/* Set the requested protection level */
/* BLOCKING */
(void)sec_set_protection_level(conn);
}
return CURLE_OK;
}
CURLcode
Curl_sec_login(struct connectdata *conn)
{
return choose_mech(conn);
}
void
Curl_sec_end(struct connectdata *conn)
{
if(conn->mech != NULL && conn->mech->end)
conn->mech->end(conn->app_data);
free(conn->app_data);
conn->app_data = NULL;
if(conn->in_buffer.data) {
free(conn->in_buffer.data);
conn->in_buffer.data = NULL;
conn->in_buffer.size = 0;
conn->in_buffer.index = 0;
conn->in_buffer.eof_flag = 0;
}
conn->sec_complete = 0;
conn->data_prot = PROT_CLEAR;
conn->mech = NULL;
}
#endif /* HAVE_GSSAPI */
#endif /* CURL_DISABLE_FTP */

View File

@ -24,7 +24,7 @@ SOURCE \
file.c timeval.c base64.c hostip.c progress.c formdata.c \
cookie.c http.c sendf.c ftp.c url.c dict.c if2ip.c speedcheck.c \
ldap.c vtls/openssl.c version.c getenv.c escape.c mprintf.c telnet.c \
netrc.c getinfo.c transfer.c strequal.c easy.c security.c krb4.c \
netrc.c getinfo.c transfer.c strequal.c easy.c krb4.c \
curl_fnmatch.c fileinfo.c ftplistparser.c wildcard.c krb5.c \
memdebug.c http_chunks.c strtok.c connect.c llist.c hash.c multi.c \
content_encoding.c share.c http_digest.c md4.c md5.c curl_rand.c \