1/* GSSAPI/krb5 support for FTP - loosely based on old krb4.c
2 *
3 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska H�gskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * Copyright (c) 2004 - 2011 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 "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 "krb4.h"
55#include "curl_memory.h"
56
57#define _MPRINTF_REPLACE /* use our functions only */
58#include <curl/mprintf.h>
59
60/* The last #include file should be: */
61#include "memdebug.h"
62
63#define LOCAL_ADDR (&conn->local_addr)
64#define REMOTE_ADDR conn->ip_addr->ai_addr
65
66static int
67krb5_init(void *app_data)
68{
69  gss_ctx_id_t *context = app_data;
70  /* Make sure our context is initialized for krb5_end. */
71  *context = GSS_C_NO_CONTEXT;
72  return 0;
73}
74
75static int
76krb5_check_prot(void *app_data, int level)
77{
78  (void)app_data; /* unused */
79  if(level == PROT_CONFIDENTIAL)
80    return -1;
81  return 0;
82}
83
84static int
85krb5_decode(void *app_data, void *buf, int len,
86            int level UNUSED_PARAM,
87            struct connectdata *conn UNUSED_PARAM)
88{
89  gss_ctx_id_t *context = app_data;
90  OM_uint32 maj, min;
91  gss_buffer_desc enc, dec;
92
93  (void)level;
94  (void)conn;
95
96  enc.value = buf;
97  enc.length = len;
98  maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
99  if(maj != GSS_S_COMPLETE) {
100    if(len >= 4)
101      strcpy(buf, "599 ");
102    return -1;
103  }
104
105  memcpy(buf, dec.value, dec.length);
106  len = dec.length;
107  gss_release_buffer(&min, &dec);
108
109  return len;
110}
111
112static int
113krb5_overhead(void *app_data, int level, int len)
114{
115  /* no arguments are used */
116  (void)app_data;
117  (void)level;
118  (void)len;
119  return 0;
120}
121
122static int
123krb5_encode(void *app_data, const void *from, int length, int level, void **to,
124            struct connectdata *conn UNUSED_PARAM)
125{
126  gss_ctx_id_t *context = app_data;
127  gss_buffer_desc dec, enc;
128  OM_uint32 maj, min;
129  int state;
130  int len;
131
132  /* shut gcc up */
133  conn = NULL;
134
135  /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
136   * libraries modify the input buffer in gss_seal()
137   */
138  dec.value = (void*)from;
139  dec.length = length;
140  maj = gss_seal(&min, *context,
141                 level == PROT_PRIVATE,
142                 GSS_C_QOP_DEFAULT,
143                 &dec, &state, &enc);
144
145  if(maj != GSS_S_COMPLETE)
146    return -1;
147
148  /* malloc a new buffer, in case gss_release_buffer doesn't work as
149     expected */
150  *to = malloc(enc.length);
151  if(!*to)
152    return -1;
153  memcpy(*to, enc.value, enc.length);
154  len = enc.length;
155  gss_release_buffer(&min, &enc);
156  return len;
157}
158
159static int
160krb5_auth(void *app_data, struct connectdata *conn)
161{
162  int ret = AUTH_OK;
163  char *p;
164  const char *host = conn->host.name;
165  ssize_t nread;
166  curl_socklen_t l = sizeof(conn->local_addr);
167  struct SessionHandle *data = conn->data;
168  CURLcode result;
169  const char *service = "ftp", *srv_host = "host";
170  gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
171  OM_uint32 maj, min;
172  gss_name_t gssname;
173  gss_ctx_id_t *context = app_data;
174  struct gss_channel_bindings_struct chan;
175  size_t base64_sz = 0;
176
177  if(getsockname(conn->sock[FIRSTSOCKET],
178                 (struct sockaddr *)LOCAL_ADDR, &l) < 0)
179    perror("getsockname()");
180
181  chan.initiator_addrtype = GSS_C_AF_INET;
182  chan.initiator_address.length = l - 4;
183  chan.initiator_address.value =
184    &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
185  chan.acceptor_addrtype = GSS_C_AF_INET;
186  chan.acceptor_address.length = l - 4;
187  chan.acceptor_address.value =
188    &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
189  chan.application_data.length = 0;
190  chan.application_data.value = NULL;
191
192  /* this loop will execute twice (once for service, once for host) */
193  for(;;) {
194    /* this really shouldn't be repeated here, but can't help it */
195    if(service == srv_host) {
196      result = Curl_ftpsendf(conn, "AUTH GSSAPI");
197
198      if(result)
199        return -2;
200      if(Curl_GetFTPResponse(&nread, conn, NULL))
201        return -1;
202
203      if(data->state.buffer[0] != '3')
204        return -1;
205    }
206
207    input_buffer.value = data->state.buffer;
208    input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
209                                   service, host);
210    maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
211                          &gssname);
212    if(maj != GSS_S_COMPLETE) {
213      gss_release_name(&min, &gssname);
214      if(service == srv_host) {
215        Curl_failf(data, "Error importing service name %s",
216                   input_buffer.value);
217        return AUTH_ERROR;
218      }
219      service = srv_host;
220      continue;
221    }
222    /* We pass NULL as |output_name_type| to avoid a leak. */
223    gss_display_name(&min, gssname, &output_buffer, NULL);
224    Curl_infof(data, "Trying against %s\n", output_buffer.value);
225    gssresp = GSS_C_NO_BUFFER;
226    *context = GSS_C_NO_CONTEXT;
227
228    do {
229      /* Release the buffer at each iteration to avoid leaking: the first time
230         we are releasing the memory from gss_display_name. The last item is
231         taken care by a final gss_release_buffer. */
232      gss_release_buffer(&min, &output_buffer);
233      ret = AUTH_OK;
234      maj = Curl_gss_init_sec_context(data,
235                                      &min,
236                                      context,
237                                      gssname,
238                                      &chan,
239                                      gssresp,
240                                      &output_buffer,
241                                      NULL);
242
243      if(gssresp) {
244        free(_gssresp.value);
245        gssresp = NULL;
246      }
247
248      if(GSS_ERROR(maj)) {
249        Curl_infof(data, "Error creating security context\n");
250        ret = AUTH_ERROR;
251        break;
252      }
253
254      if(output_buffer.length != 0) {
255        result = Curl_base64_encode(data, (char *)output_buffer.value,
256                                    output_buffer.length, &p, &base64_sz);
257        if(result) {
258          Curl_infof(data,"base64-encoding: %s\n", curl_easy_strerror(result));
259          ret = AUTH_CONTINUE;
260          break;
261        }
262
263        result = Curl_ftpsendf(conn, "ADAT %s", p);
264
265        free(p);
266
267        if(result) {
268          ret = -2;
269          break;
270        }
271
272        if(Curl_GetFTPResponse(&nread, conn, NULL)) {
273          ret = -1;
274          break;
275        }
276
277        if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
278          Curl_infof(data, "Server didn't accept auth data\n");
279          ret = AUTH_ERROR;
280          break;
281        }
282
283        p = data->state.buffer + 4;
284        p = strstr(p, "ADAT=");
285        if(p) {
286          result = Curl_base64_decode(p + 5,
287                                      (unsigned char **)&_gssresp.value,
288                                      &_gssresp.length);
289          if(result) {
290            Curl_failf(data,"base64-decoding: %s", curl_easy_strerror(result));
291            ret = AUTH_CONTINUE;
292            break;
293          }
294        }
295
296        gssresp = &_gssresp;
297      }
298    } while(maj == GSS_S_CONTINUE_NEEDED);
299
300    gss_release_name(&min, &gssname);
301    gss_release_buffer(&min, &output_buffer);
302
303    if(gssresp)
304      free(_gssresp.value);
305
306    if(ret == AUTH_OK || service == srv_host)
307      return ret;
308
309    service = srv_host;
310  }
311  return ret;
312}
313
314static void krb5_end(void *app_data)
315{
316    OM_uint32 maj, min;
317    gss_ctx_id_t *context = app_data;
318    if(*context != GSS_C_NO_CONTEXT) {
319      maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
320      DEBUGASSERT(maj == GSS_S_COMPLETE);
321    }
322}
323
324struct Curl_sec_client_mech Curl_krb5_client_mech = {
325    "GSSAPI",
326    sizeof(gss_ctx_id_t),
327    krb5_init,
328    krb5_auth,
329    krb5_end,
330    krb5_check_prot,
331    krb5_overhead,
332    krb5_encode,
333    krb5_decode
334};
335
336#endif /* HAVE_GSSAPI */
337#endif /* CURL_DISABLE_FTP */
338