1 /* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
3 * Copyright (c) 1995, 1996, 1997, 1998, 1999, 2010 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * Copyright (c) 2004 - 2009 Daniel Stenberg
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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
37 #ifndef CURL_DISABLE_FTP
40 #ifdef HAVE_OLD_GSSMIT
41 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
52 #elif defined HAVE_GSSMIT
54 # include <gssapi/gssapi.h>
55 # include <gssapi/gssapi_generic.h>
56 # include <gssapi/gssapi_krb5.h>
63 #include "curl_base64.h"
67 #include "curl_memory.h"
69 #define _MPRINTF_REPLACE /* use our functions only */
70 #include <curl/mprintf.h>
72 /* The last #include file should be: */
75 #define LOCAL_ADDR (&conn->local_addr)
76 #define REMOTE_ADDR conn->ip_addr->ai_addr
79 krb5_init(void *app_data)
81 gss_ctx_id_t *context = app_data;
82 /* Make sure our context is initialized for krb5_end. */
83 *context = GSS_C_NO_CONTEXT;
88 krb5_check_prot(void *app_data, int level)
90 (void)app_data; /* unused */
91 if(level == PROT_CONFIDENTIAL)
97 krb5_decode(void *app_data, void *buf, int len, int level,
98 struct connectdata *conn)
100 gss_ctx_id_t *context = app_data;
102 gss_buffer_desc enc, dec;
110 maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
111 if(maj != GSS_S_COMPLETE) {
117 memcpy(buf, dec.value, dec.length);
119 gss_release_buffer(&min, &dec);
125 krb5_overhead(void *app_data, int level, int len)
127 /* no arguments are used, just init them to prevent compiler warnings */
135 krb5_encode(void *app_data, const void *from, int length, int level, void **to,
136 struct connectdata *conn)
138 gss_ctx_id_t *context = app_data;
139 gss_buffer_desc dec, enc;
147 /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
148 * libraries modify the input buffer in gss_seal()
150 dec.value = (void*)from;
152 maj = gss_seal(&min, *context,
153 level == PROT_PRIVATE,
157 if(maj != GSS_S_COMPLETE)
160 /* malloc a new buffer, in case gss_release_buffer doesn't work as expected */
161 *to = malloc(enc.length);
164 memcpy(*to, enc.value, enc.length);
166 gss_release_buffer(&min, &enc);
171 krb5_auth(void *app_data, struct connectdata *conn)
175 const char *host = conn->host.name;
177 curl_socklen_t l = sizeof(conn->local_addr);
178 struct SessionHandle *data = conn->data;
180 const char *service = "ftp", *srv_host = "host";
181 gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
184 gss_ctx_id_t *context = app_data;
185 struct gss_channel_bindings_struct chan;
187 if(getsockname(conn->sock[FIRSTSOCKET],
188 (struct sockaddr *)LOCAL_ADDR, &l) < 0)
189 perror("getsockname()");
191 chan.initiator_addrtype = GSS_C_AF_INET;
192 chan.initiator_address.length = l - 4;
193 chan.initiator_address.value =
194 &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
195 chan.acceptor_addrtype = GSS_C_AF_INET;
196 chan.acceptor_address.length = l - 4;
197 chan.acceptor_address.value =
198 &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
199 chan.application_data.length = 0;
200 chan.application_data.value = NULL;
202 /* this loop will execute twice (once for service, once for host) */
204 /* this really shouldn't be repeated here, but can't help it */
205 if(service == srv_host) {
206 result = Curl_ftpsendf(conn, "AUTH GSSAPI");
210 if(Curl_GetFTPResponse(&nread, conn, NULL))
213 if(data->state.buffer[0] != '3')
217 input_buffer.value = data->state.buffer;
218 input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
220 maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
222 if(maj != GSS_S_COMPLETE) {
223 gss_release_name(&min, &gssname);
224 if(service == srv_host) {
225 Curl_failf(data, "Error importing service name %s", input_buffer.value);
231 /* We pass NULL as |output_name_type| to avoid a leak. */
232 gss_display_name(&min, gssname, &output_buffer, NULL);
233 Curl_infof(data, "Trying against %s\n", output_buffer.value);
234 gssresp = GSS_C_NO_BUFFER;
235 *context = GSS_C_NO_CONTEXT;
238 /* Release the buffer at each iteration to avoid leaking: the first time
239 we are releasing the memory from gss_display_name. The last item is
240 taken care by a final gss_release_buffer. */
241 gss_release_buffer(&min, &output_buffer);
243 maj = gss_init_sec_context(&min,
248 GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,
258 free(_gssresp.value);
263 Curl_infof(data, "Error creating security context\n");
268 if(output_buffer.length != 0) {
269 if(Curl_base64_encode(data, (char *)output_buffer.value,
270 output_buffer.length, &p) < 1) {
271 Curl_infof(data, "Out of memory base64-encoding\n");
276 result = Curl_ftpsendf(conn, "ADAT %s", p);
285 if(Curl_GetFTPResponse(&nread, conn, NULL)) {
290 if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3'){
291 Curl_infof(data, "Server didn't accept auth data\n");
296 p = data->state.buffer + 4;
297 p = strstr(p, "ADAT=");
299 _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
301 if(_gssresp.length < 1) {
302 Curl_failf(data, "Out of memory base64-encoding\n");
310 } while(maj == GSS_S_CONTINUE_NEEDED);
312 gss_release_name(&min, &gssname);
313 gss_release_buffer(&min, &output_buffer);
316 free(_gssresp.value);
318 if(ret == AUTH_OK || service == srv_host)
326 static void krb5_end(void *app_data)
329 gss_ctx_id_t *context = app_data;
330 if (*context != GSS_C_NO_CONTEXT) {
331 maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
332 DEBUGASSERT(maj == GSS_S_COMPLETE);
336 struct Curl_sec_client_mech Curl_krb5_client_mech = {
338 sizeof(gss_ctx_id_t),
348 #endif /* HAVE_GSSAPI */
349 #endif /* CURL_DISABLE_FTP */