2001-08-17 06:11:46 -04:00
|
|
|
|
/* 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.
|
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* Copyright (c) 1998, 1999 Kungliga Tekniska H<EFBFBD>gskolan
|
|
|
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
2007-11-07 04:21:35 -05:00
|
|
|
|
*
|
2010-05-07 09:05:34 -04:00
|
|
|
|
* Copyright (C) 2001 - 2010, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2007-11-07 04:21:35 -05:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* All rights reserved.
|
2004-06-30 07:51:24 -04:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
2004-06-30 07:51:24 -04:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-06-30 07:51:24 -04:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* 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.
|
2004-06-30 07:51:24 -04:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* 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.
|
2004-06-30 07:51:24 -04:00
|
|
|
|
*
|
2000-09-21 04:46:14 -04:00
|
|
|
|
* 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
|
2001-08-17 06:11:46 -04:00
|
|
|
|
* SUCH DAMAGE. */
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
|
|
|
|
#include "setup.h"
|
|
|
|
|
|
2002-06-11 07:13:01 -04:00
|
|
|
|
#ifndef CURL_DISABLE_FTP
|
2007-07-01 18:01:18 -04:00
|
|
|
|
#if defined(HAVE_KRB4) || defined(HAVE_GSSAPI)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-09-12 20:22:04 -04:00
|
|
|
|
#include <stdarg.h>
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#include <string.h>
|
2008-05-22 15:44:10 -04:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_NETDB_H
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#include <netdb.h>
|
2008-05-22 15:44:10 -04:00
|
|
|
|
#endif
|
2001-01-05 05:11:41 -05:00
|
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2005-03-13 19:00:45 -05:00
|
|
|
|
#include "urldata.h"
|
2008-08-16 20:25:38 -04:00
|
|
|
|
#include "curl_base64.h"
|
2009-04-21 07:46:16 -04:00
|
|
|
|
#include "curl_memory.h"
|
2010-09-12 20:22:04 -04:00
|
|
|
|
#include "krb4.h"
|
|
|
|
|
#include "ftp.h"
|
|
|
|
|
#include "sendf.h"
|
2008-10-23 07:49:19 -04:00
|
|
|
|
#include "rawstr.h"
|
2001-01-05 05:11:41 -05:00
|
|
|
|
|
2000-10-09 07:12:34 -04:00
|
|
|
|
/* The last #include file should be: */
|
|
|
|
|
#include "memdebug.h"
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2004-12-14 20:38:25 -05:00
|
|
|
|
static const struct {
|
2007-11-07 04:21:35 -05:00
|
|
|
|
enum protection_level level;
|
|
|
|
|
const char *name;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
} level_names[] = {
|
2010-11-13 17:42:34 -05:00
|
|
|
|
{ PROT_CLEAR, "clear" },
|
|
|
|
|
{ PROT_SAFE, "safe" },
|
|
|
|
|
{ PROT_CONFIDENTIAL, "confidential" },
|
|
|
|
|
{ PROT_PRIVATE, "private" }
|
2000-09-21 04:46:14 -04:00
|
|
|
|
};
|
|
|
|
|
|
2004-06-30 07:51:24 -04:00
|
|
|
|
static enum protection_level
|
2000-09-21 04:46:14 -04:00
|
|
|
|
name_to_level(const char *name)
|
|
|
|
|
{
|
2001-08-14 04:32:50 -04:00
|
|
|
|
int i;
|
|
|
|
|
for(i = 0; i < (int)sizeof(level_names)/(int)sizeof(level_names[0]); i++)
|
2008-10-22 21:20:57 -04:00
|
|
|
|
if(checkprefix(name, level_names[i].name))
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return level_names[i].level;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
return PROT_NONE;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
/* 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) {
|
2010-11-13 17:42:34 -05:00
|
|
|
|
case PROT_CLEAR:
|
2010-09-12 18:41:44 -04:00
|
|
|
|
return 'C';
|
2010-11-13 17:42:34 -05:00
|
|
|
|
case PROT_SAFE:
|
2010-09-12 18:41:44 -04:00
|
|
|
|
return 'S';
|
2010-11-13 17:42:34 -05:00
|
|
|
|
case PROT_CONFIDENTIAL:
|
2010-09-12 18:41:44 -04:00
|
|
|
|
return 'E';
|
2010-11-13 17:42:34 -05:00
|
|
|
|
case PROT_PRIVATE:
|
2010-09-12 18:41:44 -04:00
|
|
|
|
return 'P';
|
2010-11-13 17:42:34 -05:00
|
|
|
|
case PROT_CMD:
|
2010-09-12 18:41:44 -04:00
|
|
|
|
/* Fall through */
|
|
|
|
|
default:
|
|
|
|
|
/* Those 2 cases should not be reached! */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
DEBUGASSERT(0);
|
|
|
|
|
/* Default to the most secure alternative. */
|
|
|
|
|
return 'P';
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-14 21:32:04 -05:00
|
|
|
|
static const struct Curl_sec_client_mech * const mechs[] = {
|
2010-09-10 02:52:49 -04:00
|
|
|
|
#if defined(HAVE_GSSAPI)
|
2007-11-07 04:21:35 -05:00
|
|
|
|
&Curl_krb5_client_mech,
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#endif
|
2010-09-10 02:52:49 -04:00
|
|
|
|
#if defined(HAVE_KRB4)
|
2007-11-07 04:21:35 -05:00
|
|
|
|
&Curl_krb4_client_mech,
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#endif
|
2007-11-07 04:21:35 -05:00
|
|
|
|
NULL
|
2000-09-21 04:46:14 -04:00
|
|
|
|
};
|
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
/* 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;
|
|
|
|
|
va_list args;
|
2010-09-26 19:17:01 -04:00
|
|
|
|
char print_buffer[50];
|
2010-09-11 15:20:04 -04:00
|
|
|
|
|
|
|
|
|
va_start(args, message);
|
2010-09-26 19:17:01 -04:00
|
|
|
|
vsnprintf(print_buffer, sizeof(print_buffer), message, args);
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
|
|
if(Curl_ftpsendf(conn, print_buffer) != CURLE_OK) {
|
2010-09-11 15:20:04 -04:00
|
|
|
|
ftp_code = -1;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, &ftp_code) != CURLE_OK)
|
|
|
|
|
ftp_code = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(void)nread; /* Unused */
|
|
|
|
|
return ftp_code;
|
|
|
|
|
}
|
2010-09-10 03:07:09 -04:00
|
|
|
|
|
2010-09-22 17:41:28 -04:00
|
|
|
|
/* Read |len| from the socket |fd| and store it in |to|. Return a CURLcode
|
|
|
|
|
saying whether an error occured or CURLE_OK if |len| was read. */
|
2010-09-10 03:07:09 -04:00
|
|
|
|
static CURLcode
|
|
|
|
|
socket_read(curl_socket_t fd, void *to, size_t len)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-10 03:07:09 -04:00
|
|
|
|
char *to_p = to;
|
|
|
|
|
CURLcode code;
|
|
|
|
|
ssize_t nread;
|
|
|
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
|
code = Curl_read_plain(fd, to_p, len, &nread);
|
|
|
|
|
if(code == CURLE_OK) {
|
|
|
|
|
len -= nread;
|
|
|
|
|
to_p += nread;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* FIXME: We are doing a busy wait */
|
|
|
|
|
if(code == CURLE_AGAIN)
|
|
|
|
|
continue;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
2010-09-10 03:07:09 -04:00
|
|
|
|
return CURLE_OK;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 03:17:17 -04:00
|
|
|
|
|
|
|
|
|
/* Write |len| bytes from the buffer |to| to the socket |fd|. Return a
|
2010-09-22 17:41:28 -04:00
|
|
|
|
CURLcode saying whether an error occured or CURLE_OK if |len| was
|
|
|
|
|
written. */
|
2010-09-10 03:17:17 -04:00
|
|
|
|
static CURLcode
|
2010-09-22 17:41:28 -04:00
|
|
|
|
socket_write(struct connectdata *conn, curl_socket_t fd, const void *to,
|
|
|
|
|
size_t len)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-10 03:17:17 -04:00
|
|
|
|
const char *to_p = to;
|
|
|
|
|
CURLcode code;
|
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
|
|
while(len > 0) {
|
|
|
|
|
code = Curl_write_plain(conn, fd, to_p, len, &written);
|
|
|
|
|
if(code == CURLE_OK) {
|
|
|
|
|
len -= written;
|
|
|
|
|
to_p += written;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* FIXME: We are doing a busy wait */
|
|
|
|
|
if(code == CURLE_AGAIN)
|
|
|
|
|
continue;
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
2010-09-10 03:17:17 -04:00
|
|
|
|
return CURLE_OK;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 03:07:09 -04:00
|
|
|
|
static CURLcode read_data(struct connectdata *conn,
|
|
|
|
|
curl_socket_t fd,
|
|
|
|
|
struct krb4buffer *buf)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
int len;
|
2010-09-10 03:07:09 -04:00
|
|
|
|
void* tmp;
|
|
|
|
|
CURLcode ret;
|
|
|
|
|
|
|
|
|
|
ret = socket_read(fd, &len, sizeof(len));
|
|
|
|
|
if (ret != CURLE_OK)
|
|
|
|
|
return ret;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
len = ntohl(len);
|
2010-09-10 03:07:09 -04:00
|
|
|
|
tmp = realloc(buf->data, len);
|
|
|
|
|
if (tmp == NULL)
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
2010-09-27 01:44:42 -04:00
|
|
|
|
buf->data = tmp;
|
2010-09-10 03:07:09 -04:00
|
|
|
|
ret = socket_read(fd, buf->data, len);
|
|
|
|
|
if (ret != CURLE_OK)
|
|
|
|
|
return ret;
|
2010-09-22 17:41:28 -04:00
|
|
|
|
buf->size = conn->mech->decode(conn->app_data, buf->data, len,
|
|
|
|
|
conn->data_prot, conn);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
buf->index = 0;
|
2010-09-10 03:07:09 -04:00
|
|
|
|
return CURLE_OK;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
2010-09-23 18:20:02 -04:00
|
|
|
|
buffer_read(struct krb4buffer *buf, void *data, size_t len)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-23 18:20:02 -04:00
|
|
|
|
if(buf->size - buf->index < len)
|
|
|
|
|
len = buf->size - buf->index;
|
|
|
|
|
memcpy(data, (char*)buf->data + buf->index, len);
|
2007-11-07 04:21:35 -05:00
|
|
|
|
buf->index += len;
|
|
|
|
|
return len;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:38:38 -04:00
|
|
|
|
/* Matches Curl_recv signature */
|
|
|
|
|
static ssize_t sec_recv(struct connectdata *conn, int sockindex,
|
|
|
|
|
char *buffer, size_t len, CURLcode *err)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 19:38:38 -04:00
|
|
|
|
size_t bytes_read;
|
|
|
|
|
size_t total_read = 0;
|
|
|
|
|
curl_socket_t fd = conn->sock[sockindex];
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
|
*err = CURLE_OK;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-09-26 22:16:38 -04:00
|
|
|
|
/* Handle clear text response. */
|
2010-11-13 17:42:34 -05:00
|
|
|
|
if(conn->sec_complete == 0 || conn->data_prot == PROT_CLEAR)
|
2010-09-26 22:16:38 -04:00
|
|
|
|
return read(fd, buffer, len);
|
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
|
if(conn->in_buffer.eof_flag) {
|
2007-11-07 04:21:35 -05:00
|
|
|
|
conn->in_buffer.eof_flag = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:38:38 -04:00
|
|
|
|
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
|
|
|
|
|
len -= bytes_read;
|
|
|
|
|
total_read += bytes_read;
|
|
|
|
|
buffer += bytes_read;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2010-09-12 19:38:38 -04:00
|
|
|
|
while(len > 0) {
|
2010-09-10 03:07:09 -04:00
|
|
|
|
if(read_data(conn, fd, &conn->in_buffer) != CURLE_OK)
|
2007-11-07 04:21:35 -05:00
|
|
|
|
return -1;
|
|
|
|
|
if(conn->in_buffer.size == 0) {
|
2010-09-12 19:38:38 -04:00
|
|
|
|
if(bytes_read > 0)
|
2007-11-07 04:21:35 -05:00
|
|
|
|
conn->in_buffer.eof_flag = 1;
|
2010-09-12 19:38:38 -04:00
|
|
|
|
return bytes_read;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
}
|
2010-09-12 19:38:38 -04:00
|
|
|
|
bytes_read = buffer_read(&conn->in_buffer, buffer, len);
|
|
|
|
|
len -= bytes_read;
|
|
|
|
|
total_read += bytes_read;
|
|
|
|
|
buffer += bytes_read;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
}
|
2010-09-12 19:38:38 -04:00
|
|
|
|
/* FIXME: Check for overflow */
|
|
|
|
|
return total_read;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:25:05 -04:00
|
|
|
|
/* Send |length| bytes from |from| to the |fd| socket taking care of encoding
|
|
|
|
|
and negociating with the server. |from| can be NULL. */
|
|
|
|
|
/* FIXME: We don't check for errors nor report any! */
|
|
|
|
|
static void do_sec_send(struct connectdata *conn, curl_socket_t fd,
|
|
|
|
|
const char *from, int length)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 19:25:05 -04:00
|
|
|
|
size_t bytes;
|
|
|
|
|
size_t htonl_bytes;
|
|
|
|
|
char *buffer;
|
|
|
|
|
char *cmd_buffer;
|
|
|
|
|
enum protection_level prot_level = conn->data_prot;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
bool iscmd = prot_level == PROT_CMD;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
|
2010-11-13 17:42:34 -05:00
|
|
|
|
DEBUGASSERT(prot_level > PROT_NONE && prot_level < PROT_LAST);
|
2010-11-13 15:01:33 -05:00
|
|
|
|
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(iscmd) {
|
|
|
|
|
if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
|
2010-11-13 17:42:34 -05:00
|
|
|
|
prot_level = PROT_PRIVATE;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
else
|
2010-09-12 19:25:05 -04:00
|
|
|
|
prot_level = conn->command_prot;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
}
|
2010-09-22 17:41:28 -04:00
|
|
|
|
bytes = conn->mech->encode(conn->app_data, from, length, prot_level,
|
|
|
|
|
(void**)&buffer, conn);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(iscmd) {
|
2010-09-12 19:25:05 -04:00
|
|
|
|
bytes = Curl_base64_encode(conn->data, buffer, bytes, &cmd_buffer);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(bytes > 0) {
|
2010-09-12 19:25:05 -04:00
|
|
|
|
static const char *enc = "ENC ";
|
|
|
|
|
static const char *mic = "MIC ";
|
2010-11-13 17:42:34 -05:00
|
|
|
|
if(prot_level == PROT_PRIVATE)
|
2010-09-12 19:25:05 -04:00
|
|
|
|
socket_write(conn, fd, enc, 4);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
else
|
2010-09-12 19:25:05 -04:00
|
|
|
|
socket_write(conn, fd, mic, 4);
|
|
|
|
|
|
|
|
|
|
socket_write(conn, fd, cmd_buffer, bytes);
|
2010-09-10 03:17:17 -04:00
|
|
|
|
socket_write(conn, fd, "\r\n", 2);
|
2010-11-13 17:42:34 -05:00
|
|
|
|
infof(conn->data, "Send: %s%s\n", prot_level == PROT_PRIVATE?enc:mic,
|
2010-09-12 19:25:05 -04:00
|
|
|
|
cmd_buffer);
|
|
|
|
|
free(cmd_buffer);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
}
|
2007-11-07 04:21:35 -05:00
|
|
|
|
}
|
|
|
|
|
else {
|
2010-09-12 19:25:05 -04:00
|
|
|
|
htonl_bytes = htonl(bytes);
|
|
|
|
|
socket_write(conn, fd, &htonl_bytes, sizeof(htonl_bytes));
|
|
|
|
|
socket_write(conn, fd, buffer, bytes);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
}
|
2010-09-12 19:25:05 -04:00
|
|
|
|
free(buffer);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:46:09 -04:00
|
|
|
|
static ssize_t sec_write(struct connectdata *conn, curl_socket_t fd,
|
|
|
|
|
const char *buffer, size_t length)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 19:46:09 -04:00
|
|
|
|
/* FIXME: Check for overflow */
|
|
|
|
|
ssize_t len = conn->buffer_size;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
int tx = 0;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2010-09-22 17:41:28 -04:00
|
|
|
|
len -= conn->mech->overhead(conn->app_data, conn->data_prot, len);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(len <= 0)
|
|
|
|
|
len = length;
|
2010-09-12 19:46:09 -04:00
|
|
|
|
while(length) {
|
|
|
|
|
if(len >= 0 || length < (size_t)len) {
|
|
|
|
|
/* FIXME: Check for overflow. */
|
2000-09-21 04:46:14 -04:00
|
|
|
|
len = length;
|
2010-09-12 19:46:09 -04:00
|
|
|
|
}
|
2010-09-12 19:25:05 -04:00
|
|
|
|
do_sec_send(conn, fd, buffer, len);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
length -= len;
|
|
|
|
|
buffer += len;
|
|
|
|
|
tx += len;
|
|
|
|
|
}
|
|
|
|
|
return tx;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:41:45 -04:00
|
|
|
|
/* Matches Curl_send signature */
|
|
|
|
|
static ssize_t sec_send(struct connectdata *conn, int sockindex,
|
|
|
|
|
const void *buffer, size_t len, CURLcode *err)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 19:41:45 -04:00
|
|
|
|
curl_socket_t fd = conn->sock[sockindex];
|
2010-05-07 09:05:34 -04:00
|
|
|
|
*err = CURLE_OK;
|
2010-09-12 19:41:45 -04:00
|
|
|
|
return sec_write(conn, fd, buffer, len);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-13 15:01:33 -05:00
|
|
|
|
int Curl_sec_read_msg(struct connectdata *conn, char *buffer,
|
|
|
|
|
enum protection_level level)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 19:08:52 -04:00
|
|
|
|
/* decoded_len should be size_t or ssize_t but conn->mech->decode returns an
|
|
|
|
|
int */
|
|
|
|
|
int decoded_len;
|
|
|
|
|
char *buf;
|
|
|
|
|
int ret_code;
|
|
|
|
|
|
2010-11-13 17:42:34 -05:00
|
|
|
|
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
|
2010-11-13 15:01:33 -05:00
|
|
|
|
|
2010-09-12 19:08:52 -04:00
|
|
|
|
decoded_len = Curl_base64_decode(buffer + 4, (unsigned char **)&buf);
|
|
|
|
|
if(decoded_len <= 0) {
|
|
|
|
|
free(buf);
|
2005-02-22 07:20:30 -05:00
|
|
|
|
return -1;
|
2010-09-12 19:08:52 -04:00
|
|
|
|
}
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2010-09-22 17:41:28 -04:00
|
|
|
|
decoded_len = conn->mech->decode(conn->app_data, buf, decoded_len,
|
|
|
|
|
level, conn);
|
2010-09-12 19:08:52 -04:00
|
|
|
|
if(decoded_len <= 0) {
|
2000-09-21 04:46:14 -04:00
|
|
|
|
free(buf);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(conn->data->set.verbose) {
|
2010-09-12 19:08:52 -04:00
|
|
|
|
buf[decoded_len] = '\n';
|
|
|
|
|
Curl_debug(conn->data, CURLINFO_HEADER_IN, buf, decoded_len + 1, conn);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 19:08:52 -04:00
|
|
|
|
buf[decoded_len] = '\0';
|
|
|
|
|
DEBUGASSERT(decoded_len > 3);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
if(buf[3] == '-')
|
2010-09-12 19:08:52 -04:00
|
|
|
|
ret_code = 0;
|
|
|
|
|
else {
|
|
|
|
|
/* Check for error? */
|
|
|
|
|
sscanf(buf, "%d", &ret_code);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(buf[decoded_len - 1] == '\n')
|
|
|
|
|
buf[decoded_len - 1] = '\0';
|
|
|
|
|
/* FIXME: Is |buffer| length always greater than |decoded_len|? */
|
|
|
|
|
strcpy(buffer, buf);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
free(buf);
|
2010-09-12 19:08:52 -04:00
|
|
|
|
return ret_code;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
/* FIXME: The error code returned here is never checked. */
|
2010-10-18 04:00:37 -04:00
|
|
|
|
static int sec_set_protection_level(struct connectdata *conn)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-12 18:41:44 -04:00
|
|
|
|
int code;
|
|
|
|
|
char* pbsz;
|
|
|
|
|
static unsigned int buffer_size = 1 << 20; /* 1048576 */
|
|
|
|
|
enum protection_level level = conn->request_data_prot;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-11-13 17:42:34 -05:00
|
|
|
|
DEBUGASSERT(level > PROT_NONE && level < PROT_LAST);
|
2010-11-13 15:01:33 -05:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(!conn->sec_complete) {
|
|
|
|
|
infof(conn->data, "Trying to change the protection level after the"
|
2010-09-26 21:04:48 -04:00
|
|
|
|
"completion of the data exchange.\n");
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
/* Bail out if we try to set up the same level */
|
|
|
|
|
if(conn->data_prot == level)
|
|
|
|
|
return 0;
|
2001-10-31 10:06:38 -05:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(level) {
|
2010-09-26 20:57:03 -04:00
|
|
|
|
code = ftp_send_command(conn, "PBSZ %u", buffer_size);
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(code < 0)
|
2001-09-28 05:25:59 -04:00
|
|
|
|
return -1;
|
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(code/100 != 2) {
|
|
|
|
|
failf(conn->data, "Failed to set the protection's buffer size.");
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2010-09-12 18:41:44 -04:00
|
|
|
|
conn->buffer_size = buffer_size;
|
|
|
|
|
|
|
|
|
|
pbsz = strstr(conn->data->state.buffer, "PBSZ=");
|
|
|
|
|
if(pbsz) {
|
|
|
|
|
/* FIXME: Checks for errors in sscanf? */
|
|
|
|
|
sscanf(pbsz, "PBSZ=%u", &buffer_size);
|
|
|
|
|
if(buffer_size < conn->buffer_size)
|
|
|
|
|
conn->buffer_size = buffer_size;
|
|
|
|
|
}
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
/* Now try to negiociate the protection level. */
|
|
|
|
|
code = ftp_send_command(conn, "PROT %c", level_to_char(level));
|
2001-10-31 10:06:38 -05:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(code < 0)
|
2001-09-28 05:25:59 -04:00
|
|
|
|
return -1;
|
2001-09-28 05:15:19 -04:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
if(code/100 != 2) {
|
|
|
|
|
failf(conn->data, "Failed to set the protection level.");
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
conn->data_prot = level;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
if(level == PROT_PRIVATE)
|
2010-09-12 18:41:44 -04:00
|
|
|
|
conn->command_prot = level;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-09-12 18:41:44 -04:00
|
|
|
|
return 0;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_request_prot(struct connectdata *conn, const char *level)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-11-13 15:01:33 -05:00
|
|
|
|
enum protection_level l = name_to_level(level);
|
2010-11-13 17:42:34 -05:00
|
|
|
|
if(l == PROT_NONE)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return -1;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
DEBUGASSERT(l > PROT_NONE && l < PROT_LAST);
|
2010-11-13 15:01:33 -05:00
|
|
|
|
conn->request_data_prot = l;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
static CURLcode choose_mech(struct connectdata *conn)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2001-08-14 04:32:50 -04:00
|
|
|
|
int ret;
|
2010-09-11 15:20:04 -04:00
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
const struct Curl_sec_client_mech * const *mech;
|
|
|
|
|
void *tmp_allocation;
|
|
|
|
|
const char *mech_name;
|
|
|
|
|
|
|
|
|
|
for(mech = mechs; (*mech); ++mech) {
|
|
|
|
|
mech_name = (*mech)->name;
|
|
|
|
|
/* We have no mechanism with a NULL name but keep this check */
|
|
|
|
|
DEBUGASSERT(mech_name != NULL);
|
|
|
|
|
if(mech_name == NULL) {
|
2010-09-26 21:04:48 -04:00
|
|
|
|
infof(data, "Skipping mechanism with empty name (%p)\n", mech);
|
2001-08-14 04:32:50 -04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-09-11 15:20:04 -04:00
|
|
|
|
tmp_allocation = realloc(conn->app_data, (*mech)->size);
|
|
|
|
|
if(tmp_allocation == NULL) {
|
|
|
|
|
failf(data, "Failed realloc of size %u", (*mech)->size);
|
|
|
|
|
mech = NULL;
|
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
}
|
|
|
|
|
conn->app_data = tmp_allocation;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
if((*mech)->init) {
|
2010-11-12 11:15:01 -05:00
|
|
|
|
ret = (*mech)->init(conn->app_data);
|
2010-09-11 15:20:04 -04:00
|
|
|
|
if(ret != 0) {
|
2010-09-26 21:04:48 -04:00
|
|
|
|
infof(data, "Failed initialization for %s. Skipping it.\n", mech_name);
|
2010-09-11 15:20:04 -04:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
2010-09-26 21:04:48 -04:00
|
|
|
|
infof(data, "Trying mechanism %s...\n", mech_name);
|
2010-09-11 15:20:04 -04:00
|
|
|
|
ret = ftp_send_command(conn, "AUTH %s", mech_name);
|
|
|
|
|
if(ret < 0)
|
|
|
|
|
/* FIXME: This error is too generic but it is OK for now. */
|
|
|
|
|
return CURLE_COULDNT_CONNECT;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
if(ret/100 != 3) {
|
|
|
|
|
switch(ret) {
|
2001-09-28 05:25:59 -04:00
|
|
|
|
case 504:
|
2010-09-11 15:20:04 -04:00
|
|
|
|
infof(data, "Mechanism %s is not supported by the server (server "
|
2010-09-26 21:04:48 -04:00
|
|
|
|
"returned ftp code: 504).\n", mech_name);
|
2001-09-28 05:25:59 -04:00
|
|
|
|
break;
|
|
|
|
|
case 534:
|
2010-09-11 15:20:04 -04:00
|
|
|
|
infof(data, "Mechanism %s was rejected by the server (server returned "
|
2010-09-26 21:04:48 -04:00
|
|
|
|
"ftp code: 534).\n", mech_name);
|
2001-09-28 05:25:59 -04:00
|
|
|
|
break;
|
|
|
|
|
default:
|
2010-09-11 15:20:04 -04:00
|
|
|
|
if(ret/100 == 5) {
|
2010-09-26 21:04:48 -04:00
|
|
|
|
infof(data, "The server does not support the security extensions.\n");
|
2010-09-11 15:20:04 -04:00
|
|
|
|
return CURLE_USE_SSL_FAILED;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
}
|
|
|
|
|
break;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
/* Authenticate */
|
2010-09-22 17:41:28 -04:00
|
|
|
|
ret = (*mech)->auth(conn->app_data, conn);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
if(ret == AUTH_CONTINUE)
|
|
|
|
|
continue;
|
2010-09-11 15:20:04 -04:00
|
|
|
|
else if(ret != AUTH_OK) {
|
|
|
|
|
/* Mechanism has dumped the error to stderr, don't error here. */
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
2010-09-11 15:20:04 -04:00
|
|
|
|
DEBUGASSERT(ret == AUTH_OK);
|
|
|
|
|
|
|
|
|
|
conn->mech = *mech;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
conn->sec_complete = 1;
|
2010-09-26 22:16:38 -04:00
|
|
|
|
conn->recv[FIRSTSOCKET] = sec_recv;
|
|
|
|
|
conn->send[FIRSTSOCKET] = sec_send;
|
|
|
|
|
conn->recv[SECONDARYSOCKET] = sec_recv;
|
|
|
|
|
conn->send[SECONDARYSOCKET] = sec_send;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
conn->command_prot = PROT_SAFE;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
/* Set the requested protection level */
|
|
|
|
|
/* BLOCKING */
|
2010-10-18 04:00:37 -04:00
|
|
|
|
(void)sec_set_protection_level(conn);
|
2001-08-14 04:32:50 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2010-09-11 15:20:04 -04:00
|
|
|
|
return mech != NULL ? CURLE_OK : CURLE_FAILED_INIT;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-26 22:14:50 -04:00
|
|
|
|
CURLcode
|
2010-09-11 15:20:04 -04:00
|
|
|
|
Curl_sec_login(struct connectdata *conn)
|
|
|
|
|
{
|
2010-09-26 22:14:50 -04:00
|
|
|
|
return choose_mech(conn);
|
2010-09-11 15:20:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
void
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_end(struct connectdata *conn)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2010-09-27 01:44:42 -04:00
|
|
|
|
if(conn->mech != NULL && conn->mech->end)
|
|
|
|
|
conn->mech->end(conn->app_data);
|
|
|
|
|
if(conn->app_data) {
|
2001-08-17 06:11:46 -04:00
|
|
|
|
free(conn->app_data);
|
|
|
|
|
conn->app_data = NULL;
|
|
|
|
|
}
|
2010-09-27 01:44:42 -04:00
|
|
|
|
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;
|
|
|
|
|
/* FIXME: Is this really needed? */
|
|
|
|
|
conn->in_buffer.eof_flag = 0;
|
|
|
|
|
}
|
2001-08-17 06:11:46 -04:00
|
|
|
|
conn->sec_complete = 0;
|
2010-11-13 17:42:34 -05:00
|
|
|
|
conn->data_prot = PROT_CLEAR;
|
2010-09-12 19:46:09 -04:00
|
|
|
|
conn->mech = NULL;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-10 02:52:49 -04:00
|
|
|
|
#endif /* HAVE_KRB4 || HAVE_GSSAPI */
|
|
|
|
|
|
2002-06-11 07:13:01 -04:00
|
|
|
|
#endif /* CURL_DISABLE_FTP */
|