2013-01-04 02:50:28 +01:00
|
|
|
|
/* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
|
2007-07-02 00:01:18 +02:00
|
|
|
|
*
|
2011-03-16 00:08:32 +01:00
|
|
|
|
* Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H<EFBFBD>gskolan
|
2007-07-02 00:01:18 +02:00
|
|
|
|
* (Royal Institute of Technology, Stockholm, Sweden).
|
2013-01-04 02:50:28 +01:00
|
|
|
|
* Copyright (c) 2004 - 2012 Daniel Stenberg
|
2007-07-02 00:01:18 +02:00
|
|
|
|
* 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. */
|
|
|
|
|
|
2013-01-06 19:06:49 +01:00
|
|
|
|
#include "curl_setup.h"
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
#ifndef CURL_DISABLE_FTP
|
|
|
|
|
#ifdef HAVE_GSSAPI
|
|
|
|
|
|
2008-04-07 11:26:30 +02:00
|
|
|
|
#ifdef HAVE_OLD_GSSMIT
|
2007-07-12 23:34:42 +02:00
|
|
|
|
#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
|
2011-07-28 23:03:07 +02:00
|
|
|
|
#define NCOMPAT 1
|
2007-07-12 23:34:42 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
2007-07-02 00:01:18 +02:00
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#endif
|
2009-11-11 22:15:34 +01:00
|
|
|
|
|
2013-01-04 02:50:28 +01:00
|
|
|
|
#include "urldata.h"
|
2008-08-17 02:25:38 +02:00
|
|
|
|
#include "curl_base64.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
|
#include "ftp.h"
|
2011-07-13 22:54:54 +02:00
|
|
|
|
#include "curl_gssapi.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
|
#include "sendf.h"
|
|
|
|
|
#include "krb4.h"
|
2009-04-21 13:46:16 +02:00
|
|
|
|
#include "curl_memory.h"
|
2013-01-04 02:50:28 +01:00
|
|
|
|
#include "warnless.h"
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
2008-05-22 21:44:10 +02:00
|
|
|
|
#define _MPRINTF_REPLACE /* use our functions only */
|
|
|
|
|
#include <curl/mprintf.h>
|
|
|
|
|
|
2007-07-02 00:01:18 +02:00
|
|
|
|
/* The last #include file should be: */
|
2013-01-04 02:50:28 +01:00
|
|
|
|
#include "memdebug.h"
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
#define LOCAL_ADDR (&conn->local_addr)
|
|
|
|
|
#define REMOTE_ADDR conn->ip_addr->ai_addr
|
|
|
|
|
|
2010-10-02 09:33:24 +02:00
|
|
|
|
static int
|
|
|
|
|
krb5_init(void *app_data)
|
|
|
|
|
{
|
|
|
|
|
gss_ctx_id_t *context = app_data;
|
|
|
|
|
/* Make sure our context is initialized for krb5_end. */
|
|
|
|
|
*context = GSS_C_NO_CONTEXT;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-02 00:01:18 +02:00
|
|
|
|
static int
|
|
|
|
|
krb5_check_prot(void *app_data, int level)
|
|
|
|
|
{
|
2010-10-02 09:33:24 +02:00
|
|
|
|
(void)app_data; /* unused */
|
2010-11-13 23:42:34 +01:00
|
|
|
|
if(level == PROT_CONFIDENTIAL)
|
2007-07-02 00:01:18 +02:00
|
|
|
|
return -1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2011-07-12 23:24:52 +02:00
|
|
|
|
krb5_decode(void *app_data, void *buf, int len,
|
|
|
|
|
int level UNUSED_PARAM,
|
|
|
|
|
struct connectdata *conn UNUSED_PARAM)
|
2007-07-02 00:01:18 +02:00
|
|
|
|
{
|
|
|
|
|
gss_ctx_id_t *context = app_data;
|
|
|
|
|
OM_uint32 maj, min;
|
|
|
|
|
gss_buffer_desc enc, dec;
|
|
|
|
|
|
2011-07-12 23:24:52 +02:00
|
|
|
|
(void)level;
|
|
|
|
|
(void)conn;
|
2007-07-07 00:14:29 +02:00
|
|
|
|
|
2007-07-02 00:01:18 +02:00
|
|
|
|
enc.value = buf;
|
|
|
|
|
enc.length = len;
|
|
|
|
|
maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
|
|
|
|
|
if(maj != GSS_S_COMPLETE) {
|
|
|
|
|
if(len >= 4)
|
|
|
|
|
strcpy(buf, "599 ");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
memcpy(buf, dec.value, dec.length);
|
2012-09-28 23:57:04 +02:00
|
|
|
|
len = curlx_uztosi(dec.length);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
gss_release_buffer(&min, &dec);
|
|
|
|
|
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
krb5_overhead(void *app_data, int level, int len)
|
|
|
|
|
{
|
2011-03-16 00:08:32 +01:00
|
|
|
|
/* no arguments are used */
|
|
|
|
|
(void)app_data;
|
|
|
|
|
(void)level;
|
|
|
|
|
(void)len;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2008-05-09 13:27:54 +02:00
|
|
|
|
krb5_encode(void *app_data, const void *from, int length, int level, void **to,
|
2011-07-12 23:24:52 +02:00
|
|
|
|
struct connectdata *conn UNUSED_PARAM)
|
2007-07-02 00:01:18 +02:00
|
|
|
|
{
|
|
|
|
|
gss_ctx_id_t *context = app_data;
|
|
|
|
|
gss_buffer_desc dec, enc;
|
|
|
|
|
OM_uint32 maj, min;
|
|
|
|
|
int state;
|
|
|
|
|
int len;
|
|
|
|
|
|
2007-07-07 00:14:29 +02:00
|
|
|
|
/* shut gcc up */
|
|
|
|
|
conn = NULL;
|
|
|
|
|
|
2009-02-27 09:53:10 +01:00
|
|
|
|
/* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
|
2008-05-09 13:27:54 +02:00
|
|
|
|
* libraries modify the input buffer in gss_seal()
|
|
|
|
|
*/
|
|
|
|
|
dec.value = (void*)from;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
dec.length = length;
|
|
|
|
|
maj = gss_seal(&min, *context,
|
2010-11-13 23:42:34 +01:00
|
|
|
|
level == PROT_PRIVATE,
|
2009-02-27 09:53:10 +01:00
|
|
|
|
GSS_C_QOP_DEFAULT,
|
|
|
|
|
&dec, &state, &enc);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
if(maj != GSS_S_COMPLETE)
|
|
|
|
|
return -1;
|
|
|
|
|
|
2011-04-20 15:17:42 +02:00
|
|
|
|
/* malloc a new buffer, in case gss_release_buffer doesn't work as
|
|
|
|
|
expected */
|
2007-07-02 00:01:18 +02:00
|
|
|
|
*to = malloc(enc.length);
|
|
|
|
|
if(!*to)
|
|
|
|
|
return -1;
|
|
|
|
|
memcpy(*to, enc.value, enc.length);
|
2012-09-28 23:57:04 +02:00
|
|
|
|
len = curlx_uztosi(enc.length);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
gss_release_buffer(&min, &enc);
|
|
|
|
|
return len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
krb5_auth(void *app_data, struct connectdata *conn)
|
|
|
|
|
{
|
2010-10-02 09:39:51 +02:00
|
|
|
|
int ret = AUTH_OK;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
char *p;
|
2010-04-16 20:22:46 +02:00
|
|
|
|
const char *host = conn->host.name;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
ssize_t nread;
|
2009-05-02 04:37:32 +02:00
|
|
|
|
curl_socklen_t l = sizeof(conn->local_addr);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
|
CURLcode result;
|
|
|
|
|
const char *service = "ftp", *srv_host = "host";
|
2010-10-02 09:54:34 +02:00
|
|
|
|
gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
OM_uint32 maj, min;
|
|
|
|
|
gss_name_t gssname;
|
|
|
|
|
gss_ctx_id_t *context = app_data;
|
|
|
|
|
struct gss_channel_bindings_struct chan;
|
2011-08-24 08:07:36 +02:00
|
|
|
|
size_t base64_sz = 0;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
if(getsockname(conn->sock[FIRSTSOCKET],
|
|
|
|
|
(struct sockaddr *)LOCAL_ADDR, &l) < 0)
|
|
|
|
|
perror("getsockname()");
|
|
|
|
|
|
|
|
|
|
chan.initiator_addrtype = GSS_C_AF_INET;
|
|
|
|
|
chan.initiator_address.length = l - 4;
|
2009-02-27 09:53:10 +01:00
|
|
|
|
chan.initiator_address.value =
|
|
|
|
|
&((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
chan.acceptor_addrtype = GSS_C_AF_INET;
|
|
|
|
|
chan.acceptor_address.length = l - 4;
|
2009-02-27 09:53:10 +01:00
|
|
|
|
chan.acceptor_address.value =
|
|
|
|
|
&((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
chan.application_data.length = 0;
|
|
|
|
|
chan.application_data.value = NULL;
|
|
|
|
|
|
|
|
|
|
/* this loop will execute twice (once for service, once for host) */
|
2011-09-03 16:06:10 +02:00
|
|
|
|
for(;;) {
|
2007-07-02 00:01:18 +02:00
|
|
|
|
/* this really shouldn't be repeated here, but can't help it */
|
|
|
|
|
if(service == srv_host) {
|
|
|
|
|
result = Curl_ftpsendf(conn, "AUTH GSSAPI");
|
|
|
|
|
|
|
|
|
|
if(result)
|
2009-02-27 09:53:10 +01:00
|
|
|
|
return -2;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, NULL))
|
2009-02-27 09:53:10 +01:00
|
|
|
|
return -1;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
if(data->state.buffer[0] != '3')
|
2009-02-27 09:53:10 +01:00
|
|
|
|
return -1;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-02 09:54:34 +02:00
|
|
|
|
input_buffer.value = data->state.buffer;
|
|
|
|
|
input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
|
|
|
|
|
service, host);
|
|
|
|
|
maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
|
|
|
|
|
&gssname);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
if(maj != GSS_S_COMPLETE) {
|
|
|
|
|
gss_release_name(&min, &gssname);
|
|
|
|
|
if(service == srv_host) {
|
2011-04-20 15:17:42 +02:00
|
|
|
|
Curl_failf(data, "Error importing service name %s",
|
|
|
|
|
input_buffer.value);
|
2009-02-27 09:53:10 +01:00
|
|
|
|
return AUTH_ERROR;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
service = srv_host;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2010-10-02 09:54:34 +02:00
|
|
|
|
/* We pass NULL as |output_name_type| to avoid a leak. */
|
|
|
|
|
gss_display_name(&min, gssname, &output_buffer, NULL);
|
|
|
|
|
Curl_infof(data, "Trying against %s\n", output_buffer.value);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
gssresp = GSS_C_NO_BUFFER;
|
|
|
|
|
*context = GSS_C_NO_CONTEXT;
|
|
|
|
|
|
|
|
|
|
do {
|
2010-10-02 09:54:34 +02:00
|
|
|
|
/* Release the buffer at each iteration to avoid leaking: the first time
|
|
|
|
|
we are releasing the memory from gss_display_name. The last item is
|
|
|
|
|
taken care by a final gss_release_buffer. */
|
|
|
|
|
gss_release_buffer(&min, &output_buffer);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
ret = AUTH_OK;
|
2011-07-19 19:10:43 +02:00
|
|
|
|
maj = Curl_gss_init_sec_context(data,
|
|
|
|
|
&min,
|
2011-06-10 17:16:06 +02:00
|
|
|
|
context,
|
|
|
|
|
gssname,
|
|
|
|
|
&chan,
|
|
|
|
|
gssresp,
|
|
|
|
|
&output_buffer,
|
|
|
|
|
NULL);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
if(gssresp) {
|
2009-02-27 09:53:10 +01:00
|
|
|
|
free(_gssresp.value);
|
|
|
|
|
gssresp = NULL;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-11-12 17:20:50 +01:00
|
|
|
|
if(GSS_ERROR(maj)) {
|
|
|
|
|
Curl_infof(data, "Error creating security context\n");
|
2009-02-27 09:53:10 +01:00
|
|
|
|
ret = AUTH_ERROR;
|
|
|
|
|
break;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-02 09:54:34 +02:00
|
|
|
|
if(output_buffer.length != 0) {
|
2011-08-24 08:07:36 +02:00
|
|
|
|
result = Curl_base64_encode(data, (char *)output_buffer.value,
|
2011-08-24 11:56:23 +02:00
|
|
|
|
output_buffer.length, &p, &base64_sz);
|
2011-08-24 08:07:36 +02:00
|
|
|
|
if(result) {
|
|
|
|
|
Curl_infof(data,"base64-encoding: %s\n", curl_easy_strerror(result));
|
2009-02-27 09:53:10 +01:00
|
|
|
|
ret = AUTH_CONTINUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = Curl_ftpsendf(conn, "ADAT %s", p);
|
|
|
|
|
|
|
|
|
|
free(p);
|
|
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
|
ret = -2;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(Curl_GetFTPResponse(&nread, conn, NULL)) {
|
|
|
|
|
ret = -1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-04 22:10:32 +02:00
|
|
|
|
if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
|
2009-02-27 09:53:10 +01:00
|
|
|
|
Curl_infof(data, "Server didn't accept auth data\n");
|
|
|
|
|
ret = AUTH_ERROR;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p = data->state.buffer + 4;
|
|
|
|
|
p = strstr(p, "ADAT=");
|
|
|
|
|
if(p) {
|
2011-08-24 08:07:36 +02:00
|
|
|
|
result = Curl_base64_decode(p + 5,
|
|
|
|
|
(unsigned char **)&_gssresp.value,
|
|
|
|
|
&_gssresp.length);
|
|
|
|
|
if(result) {
|
|
|
|
|
Curl_failf(data,"base64-decoding: %s", curl_easy_strerror(result));
|
2009-02-27 09:53:10 +01:00
|
|
|
|
ret = AUTH_CONTINUE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gssresp = &_gssresp;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
} while(maj == GSS_S_CONTINUE_NEEDED);
|
|
|
|
|
|
|
|
|
|
gss_release_name(&min, &gssname);
|
2010-10-02 09:54:34 +02:00
|
|
|
|
gss_release_buffer(&min, &output_buffer);
|
2007-07-02 00:01:18 +02:00
|
|
|
|
|
|
|
|
|
if(gssresp)
|
|
|
|
|
free(_gssresp.value);
|
|
|
|
|
|
|
|
|
|
if(ret == AUTH_OK || service == srv_host)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
service = srv_host;
|
|
|
|
|
}
|
2010-10-02 09:39:51 +02:00
|
|
|
|
return ret;
|
2007-07-02 00:01:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2010-10-02 09:33:24 +02:00
|
|
|
|
static void krb5_end(void *app_data)
|
|
|
|
|
{
|
2012-01-02 19:18:53 +01:00
|
|
|
|
OM_uint32 min;
|
2010-10-02 09:33:24 +02:00
|
|
|
|
gss_ctx_id_t *context = app_data;
|
2011-04-20 15:17:42 +02:00
|
|
|
|
if(*context != GSS_C_NO_CONTEXT) {
|
2012-01-02 19:18:53 +01:00
|
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
|
OM_uint32 maj =
|
|
|
|
|
#endif
|
|
|
|
|
gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
|
2010-10-02 09:33:24 +02:00
|
|
|
|
DEBUGASSERT(maj == GSS_S_COMPLETE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-07-02 00:01:18 +02:00
|
|
|
|
struct Curl_sec_client_mech Curl_krb5_client_mech = {
|
|
|
|
|
"GSSAPI",
|
|
|
|
|
sizeof(gss_ctx_id_t),
|
2010-10-02 09:33:24 +02:00
|
|
|
|
krb5_init,
|
2007-07-02 00:01:18 +02:00
|
|
|
|
krb5_auth,
|
2010-10-02 09:33:24 +02:00
|
|
|
|
krb5_end,
|
2007-07-02 00:01:18 +02:00
|
|
|
|
krb5_check_prot,
|
|
|
|
|
krb5_overhead,
|
|
|
|
|
krb5_encode,
|
|
|
|
|
krb5_decode
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif /* HAVE_GSSAPI */
|
|
|
|
|
#endif /* CURL_DISABLE_FTP */
|