1/* GSSAPI/krb5 support for FTP - loosely based on old krb4.c 2 * 3 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2013 Kungliga Tekniska H�gskolan 4 * (Royal Institute of Technology, Stockholm, Sweden). 5 * Copyright (c) 2004 - 2012 Daniel Stenberg 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of the Institute nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. */ 34 35#include "curl_setup.h" 36 37#ifndef CURL_DISABLE_FTP 38#ifdef HAVE_GSSAPI 39 40#ifdef HAVE_OLD_GSSMIT 41#define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name 42#define NCOMPAT 1 43#endif 44 45#ifdef HAVE_NETDB_H 46#include <netdb.h> 47#endif 48 49#include "urldata.h" 50#include "curl_base64.h" 51#include "ftp.h" 52#include "curl_gssapi.h" 53#include "sendf.h" 54#include "curl_sec.h" 55#include "curl_memory.h" 56#include "warnless.h" 57 58#define _MPRINTF_REPLACE /* use our functions only */ 59#include <curl/mprintf.h> 60 61/* The last #include file should be: */ 62#include "memdebug.h" 63 64#define LOCAL_ADDR (&conn->local_addr) 65#define REMOTE_ADDR conn->ip_addr->ai_addr 66 67static int 68krb5_init(void *app_data) 69{ 70 gss_ctx_id_t *context = app_data; 71 /* Make sure our context is initialized for krb5_end. */ 72 *context = GSS_C_NO_CONTEXT; 73 return 0; 74} 75 76static int 77krb5_check_prot(void *app_data, int level) 78{ 79 (void)app_data; /* unused */ 80 if(level == PROT_CONFIDENTIAL) 81 return -1; 82 return 0; 83} 84 85static int 86krb5_decode(void *app_data, void *buf, int len, 87 int level UNUSED_PARAM, 88 struct connectdata *conn UNUSED_PARAM) 89{ 90 gss_ctx_id_t *context = app_data; 91 OM_uint32 maj, min; 92 gss_buffer_desc enc, dec; 93 94 (void)level; 95 (void)conn; 96 97 enc.value = buf; 98 enc.length = len; 99 maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL); 100 if(maj != GSS_S_COMPLETE) { 101 if(len >= 4) 102 strcpy(buf, "599 "); 103 return -1; 104 } 105 106 memcpy(buf, dec.value, dec.length); 107 len = curlx_uztosi(dec.length); 108 gss_release_buffer(&min, &dec); 109 110 return len; 111} 112 113static int 114krb5_overhead(void *app_data, int level, int len) 115{ 116 /* no arguments are used */ 117 (void)app_data; 118 (void)level; 119 (void)len; 120 return 0; 121} 122 123static int 124krb5_encode(void *app_data, const void *from, int length, int level, void **to, 125 struct connectdata *conn UNUSED_PARAM) 126{ 127 gss_ctx_id_t *context = app_data; 128 gss_buffer_desc dec, enc; 129 OM_uint32 maj, min; 130 int state; 131 int len; 132 133 /* shut gcc up */ 134 conn = NULL; 135 136 /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal 137 * libraries modify the input buffer in gss_seal() 138 */ 139 dec.value = (void*)from; 140 dec.length = length; 141 maj = gss_seal(&min, *context, 142 level == PROT_PRIVATE, 143 GSS_C_QOP_DEFAULT, 144 &dec, &state, &enc); 145 146 if(maj != GSS_S_COMPLETE) 147 return -1; 148 149 /* malloc a new buffer, in case gss_release_buffer doesn't work as 150 expected */ 151 *to = malloc(enc.length); 152 if(!*to) 153 return -1; 154 memcpy(*to, enc.value, enc.length); 155 len = curlx_uztosi(enc.length); 156 gss_release_buffer(&min, &enc); 157 return len; 158} 159 160static int 161krb5_auth(void *app_data, struct connectdata *conn) 162{ 163 int ret = AUTH_OK; 164 char *p; 165 const char *host = conn->host.name; 166 ssize_t nread; 167 curl_socklen_t l = sizeof(conn->local_addr); 168 struct SessionHandle *data = conn->data; 169 CURLcode result; 170 const char *service = "ftp", *srv_host = "host"; 171 gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp; 172 OM_uint32 maj, min; 173 gss_name_t gssname; 174 gss_ctx_id_t *context = app_data; 175 struct gss_channel_bindings_struct chan; 176 size_t base64_sz = 0; 177 178 if(getsockname(conn->sock[FIRSTSOCKET], 179 (struct sockaddr *)LOCAL_ADDR, &l) < 0) 180 perror("getsockname()"); 181 182 chan.initiator_addrtype = GSS_C_AF_INET; 183 chan.initiator_address.length = l - 4; 184 chan.initiator_address.value = 185 &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr; 186 chan.acceptor_addrtype = GSS_C_AF_INET; 187 chan.acceptor_address.length = l - 4; 188 chan.acceptor_address.value = 189 &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr; 190 chan.application_data.length = 0; 191 chan.application_data.value = NULL; 192 193 /* this loop will execute twice (once for service, once for host) */ 194 for(;;) { 195 /* this really shouldn't be repeated here, but can't help it */ 196 if(service == srv_host) { 197 result = Curl_ftpsendf(conn, "AUTH GSSAPI"); 198 199 if(result) 200 return -2; 201 if(Curl_GetFTPResponse(&nread, conn, NULL)) 202 return -1; 203 204 if(data->state.buffer[0] != '3') 205 return -1; 206 } 207 208 input_buffer.value = data->state.buffer; 209 input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s", 210 service, host); 211 maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE, 212 &gssname); 213 if(maj != GSS_S_COMPLETE) { 214 gss_release_name(&min, &gssname); 215 if(service == srv_host) { 216 Curl_failf(data, "Error importing service name %s", 217 input_buffer.value); 218 return AUTH_ERROR; 219 } 220 service = srv_host; 221 continue; 222 } 223 /* We pass NULL as |output_name_type| to avoid a leak. */ 224 gss_display_name(&min, gssname, &output_buffer, NULL); 225 Curl_infof(data, "Trying against %s\n", output_buffer.value); 226 gssresp = GSS_C_NO_BUFFER; 227 *context = GSS_C_NO_CONTEXT; 228 229 do { 230 /* Release the buffer at each iteration to avoid leaking: the first time 231 we are releasing the memory from gss_display_name. The last item is 232 taken care by a final gss_release_buffer. */ 233 gss_release_buffer(&min, &output_buffer); 234 ret = AUTH_OK; 235 maj = Curl_gss_init_sec_context(data, 236 &min, 237 context, 238 gssname, 239 &chan, 240 gssresp, 241 &output_buffer, 242 NULL); 243 244 if(gssresp) { 245 free(_gssresp.value); 246 gssresp = NULL; 247 } 248 249 if(GSS_ERROR(maj)) { 250 Curl_infof(data, "Error creating security context\n"); 251 ret = AUTH_ERROR; 252 break; 253 } 254 255 if(output_buffer.length != 0) { 256 result = Curl_base64_encode(data, (char *)output_buffer.value, 257 output_buffer.length, &p, &base64_sz); 258 if(result) { 259 Curl_infof(data,"base64-encoding: %s\n", curl_easy_strerror(result)); 260 ret = AUTH_CONTINUE; 261 break; 262 } 263 264 result = Curl_ftpsendf(conn, "ADAT %s", p); 265 266 free(p); 267 268 if(result) { 269 ret = -2; 270 break; 271 } 272 273 if(Curl_GetFTPResponse(&nread, conn, NULL)) { 274 ret = -1; 275 break; 276 } 277 278 if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') { 279 Curl_infof(data, "Server didn't accept auth data\n"); 280 ret = AUTH_ERROR; 281 break; 282 } 283 284 p = data->state.buffer + 4; 285 p = strstr(p, "ADAT="); 286 if(p) { 287 result = Curl_base64_decode(p + 5, 288 (unsigned char **)&_gssresp.value, 289 &_gssresp.length); 290 if(result) { 291 Curl_failf(data,"base64-decoding: %s", curl_easy_strerror(result)); 292 ret = AUTH_CONTINUE; 293 break; 294 } 295 } 296 297 gssresp = &_gssresp; 298 } 299 } while(maj == GSS_S_CONTINUE_NEEDED); 300 301 gss_release_name(&min, &gssname); 302 gss_release_buffer(&min, &output_buffer); 303 304 if(gssresp) 305 free(_gssresp.value); 306 307 if(ret == AUTH_OK || service == srv_host) 308 return ret; 309 310 service = srv_host; 311 } 312 return ret; 313} 314 315static void krb5_end(void *app_data) 316{ 317 OM_uint32 min; 318 gss_ctx_id_t *context = app_data; 319 if(*context != GSS_C_NO_CONTEXT) { 320#ifdef DEBUGBUILD 321 OM_uint32 maj = 322#endif 323 gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER); 324 DEBUGASSERT(maj == GSS_S_COMPLETE); 325 } 326} 327 328struct Curl_sec_client_mech Curl_krb5_client_mech = { 329 "GSSAPI", 330 sizeof(gss_ctx_id_t), 331 krb5_init, 332 krb5_auth, 333 krb5_end, 334 krb5_check_prot, 335 krb5_overhead, 336 krb5_encode, 337 krb5_decode 338}; 339 340#endif /* HAVE_GSSAPI */ 341#endif /* CURL_DISABLE_FTP */ 342