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
|
|
|
|
*
|
2008-05-25 21:59:00 -04:00
|
|
|
|
* Copyright (C) 2001 - 2008, 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
|
|
|
|
|
2001-01-05 05:11:41 -05:00
|
|
|
|
#define _MPRINTF_REPLACE /* we want curl-functions instead of native ones */
|
2000-09-28 06:36:31 -04:00
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#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"
|
|
|
|
|
#include "krb4.h"
|
2008-08-16 20:25:38 -04:00
|
|
|
|
#include "curl_base64.h"
|
2001-01-05 05:11:41 -05:00
|
|
|
|
#include "sendf.h"
|
|
|
|
|
#include "ftp.h"
|
2004-05-11 07:30:23 -04:00
|
|
|
|
#include "memory.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[] = {
|
2007-11-07 04:21:35 -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;
|
|
|
|
|
return (enum protection_level)-1;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2004-12-14 21:32:04 -05:00
|
|
|
|
static const struct Curl_sec_client_mech * const mechs[] = {
|
2007-07-01 18:01:18 -04:00
|
|
|
|
#ifdef HAVE_GSSAPI
|
2007-11-07 04:21:35 -05:00
|
|
|
|
&Curl_krb5_client_mech,
|
2000-09-21 04:46:14 -04:00
|
|
|
|
#endif
|
2003-12-02 08:27:29 -05:00
|
|
|
|
#ifdef 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
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_getc(struct connectdata *conn, FILE *F)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
if(conn->sec_complete && conn->data_prot) {
|
|
|
|
|
char c;
|
2001-08-17 06:11:46 -04:00
|
|
|
|
if(Curl_sec_read(conn, fileno(F), &c, 1) <= 0)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return EOF;
|
|
|
|
|
return c;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return getc(F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
block_read(int fd, void *buf, size_t len)
|
|
|
|
|
{
|
2001-08-14 04:32:50 -04:00
|
|
|
|
unsigned char *p = buf;
|
|
|
|
|
int b;
|
|
|
|
|
while(len) {
|
|
|
|
|
b = read(fd, p, len);
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(b == 0)
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return 0;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
else if(b < 0 && (errno == EINTR || errno == EAGAIN))
|
2007-07-01 18:01:18 -04:00
|
|
|
|
continue;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
else if(b < 0)
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
|
|
|
|
len -= b;
|
|
|
|
|
p += b;
|
|
|
|
|
}
|
|
|
|
|
return p - (unsigned char*)buf;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2007-07-06 18:14:29 -04:00
|
|
|
|
block_write(int fd, const void *buf, size_t len)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2007-07-06 18:14:29 -04:00
|
|
|
|
const unsigned char *p = buf;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
int b;
|
|
|
|
|
while(len) {
|
|
|
|
|
b = write(fd, p, len);
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(b < 0 && (errno == EINTR || errno == EAGAIN))
|
2007-07-01 18:01:18 -04:00
|
|
|
|
continue;
|
|
|
|
|
else if(b < 0)
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
|
|
|
|
len -= b;
|
|
|
|
|
p += b;
|
|
|
|
|
}
|
|
|
|
|
return p - (unsigned char*)buf;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
sec_get_data(struct connectdata *conn,
|
2001-08-14 04:32:50 -04:00
|
|
|
|
int fd, struct krb4buffer *buf)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
int len;
|
|
|
|
|
int b;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
b = block_read(fd, &len, sizeof(len));
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(b == 0)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return 0;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
else if(b < 0)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return -1;
|
|
|
|
|
len = ntohl(len);
|
|
|
|
|
buf->data = realloc(buf->data, len);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
b = buf->data ? block_read(fd, buf->data, len) : -1;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(b == 0)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return 0;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
else if(b < 0)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return -1;
|
2001-08-17 06:11:46 -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;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
buffer_read(struct krb4buffer *buf, void *data, size_t len)
|
|
|
|
|
{
|
2008-05-25 21:59:00 -04:00
|
|
|
|
if(buf->size - buf->index < len)
|
|
|
|
|
len = buf->size - buf->index;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
memcpy(data, (char*)buf->data + buf->index, len);
|
|
|
|
|
buf->index += len;
|
|
|
|
|
return len;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
buffer_write(struct krb4buffer *buf, void *data, size_t len)
|
|
|
|
|
{
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(buf->index + len > buf->size) {
|
|
|
|
|
void *tmp;
|
|
|
|
|
if(buf->data == NULL)
|
|
|
|
|
tmp = malloc(1024);
|
|
|
|
|
else
|
|
|
|
|
tmp = realloc(buf->data, buf->index + len);
|
|
|
|
|
if(tmp == NULL)
|
|
|
|
|
return -1;
|
|
|
|
|
buf->data = tmp;
|
|
|
|
|
buf->size = buf->index + len;
|
|
|
|
|
}
|
|
|
|
|
memcpy((char*)buf->data + buf->index, data, len);
|
|
|
|
|
buf->index += len;
|
|
|
|
|
return len;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_read(struct connectdata *conn, int fd, void *buffer, int length)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2007-11-07 04:21:35 -05:00
|
|
|
|
size_t len;
|
|
|
|
|
int rx = 0;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(conn->sec_complete == 0 || conn->data_prot == 0)
|
|
|
|
|
return read(fd, buffer, length);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(conn->in_buffer.eof_flag){
|
|
|
|
|
conn->in_buffer.eof_flag = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
len = buffer_read(&conn->in_buffer, buffer, length);
|
|
|
|
|
length -= len;
|
|
|
|
|
rx += len;
|
|
|
|
|
buffer = (char*)buffer + len;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2007-11-07 04:21:35 -05:00
|
|
|
|
while(length) {
|
|
|
|
|
if(sec_get_data(conn, fd, &conn->in_buffer) < 0)
|
|
|
|
|
return -1;
|
|
|
|
|
if(conn->in_buffer.size == 0) {
|
|
|
|
|
if(rx)
|
|
|
|
|
conn->in_buffer.eof_flag = 1;
|
|
|
|
|
return rx;
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
len = buffer_read(&conn->in_buffer, buffer, length);
|
|
|
|
|
length -= len;
|
|
|
|
|
rx += len;
|
|
|
|
|
buffer = (char*)buffer + len;
|
2007-11-07 04:21:35 -05:00
|
|
|
|
}
|
|
|
|
|
return rx;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-09 07:27:54 -04:00
|
|
|
|
sec_send(struct connectdata *conn, int fd, const char *from, int length)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
int bytes;
|
|
|
|
|
void *buf;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
enum protection_level protlevel = conn->data_prot;
|
|
|
|
|
int iscmd = protlevel == prot_cmd;
|
|
|
|
|
|
|
|
|
|
if(iscmd) {
|
|
|
|
|
if(!strncmp(from, "PASS ", 5) || !strncmp(from, "ACCT ", 5))
|
|
|
|
|
protlevel = prot_private;
|
|
|
|
|
else
|
|
|
|
|
protlevel = conn->command_prot;
|
|
|
|
|
}
|
|
|
|
|
bytes = (conn->mech->encode)(conn->app_data, from, length, protlevel,
|
2001-08-17 06:11:46 -04:00
|
|
|
|
&buf, conn);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(iscmd) {
|
|
|
|
|
char *cmdbuf;
|
|
|
|
|
|
|
|
|
|
bytes = Curl_base64_encode(conn->data, (char *)buf, bytes, &cmdbuf);
|
|
|
|
|
if(bytes > 0) {
|
|
|
|
|
if(protlevel == prot_private)
|
2007-11-07 04:21:35 -05:00
|
|
|
|
block_write(fd, "ENC ", 4);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
else
|
2007-11-07 04:21:35 -05:00
|
|
|
|
block_write(fd, "MIC ", 4);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
block_write(fd, cmdbuf, bytes);
|
|
|
|
|
block_write(fd, "\r\n", 2);
|
2007-11-07 04:21:35 -05:00
|
|
|
|
Curl_infof(conn->data, "%s %s\n",
|
|
|
|
|
protlevel == prot_private ? "ENC" : "MIC", cmdbuf);
|
2007-07-01 18:01:18 -04:00
|
|
|
|
free(cmdbuf);
|
|
|
|
|
}
|
2007-11-07 04:21:35 -05:00
|
|
|
|
}
|
|
|
|
|
else {
|
2007-07-01 18:01:18 -04:00
|
|
|
|
bytes = htonl(bytes);
|
|
|
|
|
block_write(fd, &bytes, sizeof(bytes));
|
|
|
|
|
block_write(fd, buf, ntohl(bytes));
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
free(buf);
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_fflush_fd(struct connectdata *conn, int fd)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
if(conn->data_prot != prot_clear) {
|
|
|
|
|
if(conn->out_buffer.index > 0){
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_write(conn, fd,
|
2007-11-07 04:21:35 -05:00
|
|
|
|
conn->out_buffer.data, conn->out_buffer.index);
|
2000-09-25 18:15:56 -04:00
|
|
|
|
conn->out_buffer.index = 0;
|
|
|
|
|
}
|
|
|
|
|
sec_send(conn, fd, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
int
|
2008-05-09 07:27:54 -04:00
|
|
|
|
Curl_sec_write(struct connectdata *conn, int fd, const char *buffer, int length)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
int len = conn->buffer_size;
|
|
|
|
|
int tx = 0;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
if(conn->data_prot == prot_clear)
|
|
|
|
|
return write(fd, buffer, length);
|
|
|
|
|
|
2001-08-17 06:11:46 -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;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
while(length){
|
|
|
|
|
if(length < len)
|
|
|
|
|
len = length;
|
|
|
|
|
sec_send(conn, fd, buffer, len);
|
|
|
|
|
length -= len;
|
|
|
|
|
buffer += len;
|
|
|
|
|
tx += len;
|
|
|
|
|
}
|
|
|
|
|
return tx;
|
|
|
|
|
}
|
|
|
|
|
|
2006-11-11 16:34:43 -05:00
|
|
|
|
ssize_t
|
2008-05-09 07:27:54 -04:00
|
|
|
|
Curl_sec_send(struct connectdata *conn, int num, const char *buffer, int length)
|
2006-11-11 16:34:43 -05:00
|
|
|
|
{
|
|
|
|
|
curl_socket_t fd = conn->sock[num];
|
|
|
|
|
return (ssize_t)Curl_sec_write(conn, fd, buffer, length);
|
|
|
|
|
}
|
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_putc(struct connectdata *conn, int c, FILE *F)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
char ch = c;
|
|
|
|
|
if(conn->data_prot == prot_clear)
|
|
|
|
|
return putc(c, F);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2000-09-21 04:46:14 -04:00
|
|
|
|
buffer_write(&conn->out_buffer, &ch, 1);
|
|
|
|
|
if(c == '\n' || conn->out_buffer.index >= 1024 /* XXX */) {
|
2003-01-09 06:50:34 -05:00
|
|
|
|
Curl_sec_write(conn, fileno(F), conn->out_buffer.data,
|
|
|
|
|
conn->out_buffer.index);
|
2000-09-21 04:46:14 -04:00
|
|
|
|
conn->out_buffer.index = 0;
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_read_msg(struct connectdata *conn, char *s, int level)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2004-06-30 07:51:24 -04:00
|
|
|
|
int len;
|
2005-02-22 07:20:30 -05:00
|
|
|
|
unsigned char *buf;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
int code;
|
|
|
|
|
|
2005-02-22 07:20:30 -05:00
|
|
|
|
len = Curl_base64_decode(s + 4, &buf); /* XXX */
|
|
|
|
|
if(len > 0)
|
|
|
|
|
len = (conn->mech->decode)(conn->app_data, buf, len, level, conn);
|
|
|
|
|
else
|
|
|
|
|
return -1;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
|
|
|
|
if(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) {
|
|
|
|
|
buf[len] = '\n';
|
|
|
|
|
Curl_debug(conn->data, CURLINFO_HEADER_IN, (char *)buf, len + 1, conn);
|
|
|
|
|
}
|
|
|
|
|
|
2004-06-30 07:51:24 -04:00
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
|
|
|
|
|
if(buf[3] == '-')
|
|
|
|
|
code = 0;
|
|
|
|
|
else
|
2005-02-22 07:20:30 -05:00
|
|
|
|
sscanf((char *)buf, "%d", &code);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
if(buf[len-1] == '\n')
|
|
|
|
|
buf[len-1] = '\0';
|
2005-02-22 07:20:30 -05:00
|
|
|
|
strcpy(s, (char *)buf);
|
2004-06-30 07:51:24 -04:00
|
|
|
|
free(buf);
|
|
|
|
|
return code;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2001-08-17 06:11:46 -04:00
|
|
|
|
enum protection_level
|
|
|
|
|
Curl_set_command_prot(struct connectdata *conn, enum protection_level level)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2004-06-30 07:51:24 -04:00
|
|
|
|
enum protection_level old = conn->command_prot;
|
|
|
|
|
conn->command_prot = level;
|
|
|
|
|
return old;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
sec_prot_internal(struct connectdata *conn, int level)
|
|
|
|
|
{
|
2001-08-14 04:32:50 -04:00
|
|
|
|
char *p;
|
|
|
|
|
unsigned int s = 1048576;
|
|
|
|
|
ssize_t nread;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
if(!conn->sec_complete){
|
|
|
|
|
infof(conn->data, "No security data exchange has taken place.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
if(level){
|
2002-12-03 05:25:31 -05:00
|
|
|
|
int code;
|
2001-10-31 10:06:38 -05:00
|
|
|
|
if(Curl_ftpsendf(conn, "PBSZ %u", s))
|
|
|
|
|
return -1;
|
|
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, &code))
|
2001-09-28 05:25:59 -04:00
|
|
|
|
return -1;
|
|
|
|
|
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(code/100 != 2){
|
2001-12-11 08:13:01 -05:00
|
|
|
|
failf(conn->data, "Failed to set protection buffer size.");
|
2000-09-21 04:46:14 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2001-08-14 04:32:50 -04:00
|
|
|
|
conn->buffer_size = s;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
|
|
|
|
p = strstr(conn->data->state.buffer, "PBSZ=");
|
2001-08-14 04:32:50 -04:00
|
|
|
|
if(p)
|
|
|
|
|
sscanf(p, "PBSZ=%u", &s);
|
|
|
|
|
if(s < conn->buffer_size)
|
|
|
|
|
conn->buffer_size = s;
|
|
|
|
|
}
|
|
|
|
|
|
2001-10-31 10:06:38 -05:00
|
|
|
|
if(Curl_ftpsendf(conn, "PROT %c", level["CSEP"]))
|
|
|
|
|
return -1;
|
|
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, NULL))
|
2001-09-28 05:25:59 -04:00
|
|
|
|
return -1;
|
2001-09-28 05:15:19 -04:00
|
|
|
|
|
2001-09-28 05:25:59 -04:00
|
|
|
|
if(conn->data->state.buffer[0] != '2'){
|
2001-12-11 08:13:01 -05:00
|
|
|
|
failf(conn->data, "Failed to set protection level.");
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return -1;
|
|
|
|
|
}
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
conn->data_prot = (enum protection_level)level;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
if(level == prot_private)
|
|
|
|
|
conn->command_prot = (enum protection_level)level;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return 0;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_set_protection_level(struct connectdata *conn)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
|
|
|
|
if(conn->sec_complete && conn->data_prot != conn->request_data_prot)
|
|
|
|
|
sec_prot_internal(conn, conn->request_data_prot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
int l = name_to_level(level);
|
|
|
|
|
if(l == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
conn->request_data_prot = (enum protection_level)l;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2001-08-17 06:11:46 -04:00
|
|
|
|
Curl_sec_login(struct connectdata *conn)
|
2000-09-21 04:46:14 -04:00
|
|
|
|
{
|
2001-08-14 04:32:50 -04:00
|
|
|
|
int ret;
|
2004-12-14 21:32:04 -05:00
|
|
|
|
const struct Curl_sec_client_mech * const *m;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
ssize_t nread;
|
2001-08-30 18:48:34 -04:00
|
|
|
|
struct SessionHandle *data=conn->data;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
int ftpcode;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
for(m = mechs; *m && (*m)->name; m++) {
|
|
|
|
|
void *tmp;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
tmp = realloc(conn->app_data, (*m)->size);
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(tmp == NULL) {
|
2001-08-14 04:32:50 -04:00
|
|
|
|
failf (data, "realloc %u failed", (*m)->size);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
conn->app_data = tmp;
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
if((*m)->init && (*(*m)->init)(conn->app_data) != 0) {
|
|
|
|
|
infof(data, "Skipping %s...\n", (*m)->name);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
infof(data, "Trying %s...\n", (*m)->name);
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
2001-10-31 10:06:38 -05:00
|
|
|
|
if(Curl_ftpsendf(conn, "AUTH %s", (*m)->name))
|
|
|
|
|
return -1;
|
2001-09-28 05:25:59 -04:00
|
|
|
|
|
2002-12-03 05:25:31 -05:00
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, &ftpcode))
|
2001-09-28 05:25:59 -04:00
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
if(conn->data->state.buffer[0] != '3'){
|
|
|
|
|
switch(ftpcode) {
|
|
|
|
|
case 504:
|
2001-08-14 04:32:50 -04:00
|
|
|
|
infof(data,
|
|
|
|
|
"%s is not supported by the server.\n", (*m)->name);
|
2001-09-28 05:25:59 -04:00
|
|
|
|
break;
|
|
|
|
|
case 534:
|
2001-08-14 04:32:50 -04:00
|
|
|
|
infof(data, "%s rejected as security mechanism.\n", (*m)->name);
|
2001-09-28 05:25:59 -04:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
if(conn->data->state.buffer[0] == '5') {
|
|
|
|
|
infof(data, "The server doesn't support the FTP "
|
|
|
|
|
"security extensions.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-09-21 04:46:14 -04:00
|
|
|
|
|
2001-09-28 05:25:59 -04:00
|
|
|
|
ret = (*(*m)->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;
|
|
|
|
|
else if(ret != AUTH_OK){
|
|
|
|
|
/* mechanism is supposed to output error string */
|
|
|
|
|
return -1;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
2001-08-17 06:11:46 -04:00
|
|
|
|
conn->mech = *m;
|
2001-08-14 04:32:50 -04:00
|
|
|
|
conn->sec_complete = 1;
|
|
|
|
|
conn->command_prot = prot_safe;
|
2007-07-01 18:01:18 -04:00
|
|
|
|
/* Set the requested protection level */
|
|
|
|
|
/* BLOCKING */
|
|
|
|
|
Curl_sec_set_protection_level(conn);
|
2001-08-14 04:32:50 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
2004-06-30 07:51:24 -04:00
|
|
|
|
|
2001-08-14 04:32:50 -04:00
|
|
|
|
return *m == NULL;
|
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
|
|
|
|
{
|
2007-11-07 04:21:35 -05:00
|
|
|
|
if(conn->mech != NULL) {
|
2001-08-17 06:11:46 -04:00
|
|
|
|
if(conn->mech->end)
|
|
|
|
|
(conn->mech->end)(conn->app_data);
|
|
|
|
|
memset(conn->app_data, 0, conn->mech->size);
|
|
|
|
|
free(conn->app_data);
|
|
|
|
|
conn->app_data = NULL;
|
|
|
|
|
}
|
|
|
|
|
conn->sec_complete = 0;
|
|
|
|
|
conn->data_prot = (enum protection_level)0;
|
|
|
|
|
conn->mech=NULL;
|
2000-09-21 04:46:14 -04:00
|
|
|
|
}
|
|
|
|
|
|
2003-12-02 08:27:29 -05:00
|
|
|
|
#endif /* HAVE_KRB4 */
|
2002-06-11 07:13:01 -04:00
|
|
|
|
#endif /* CURL_DISABLE_FTP */
|