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 - 2008 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 #endif
43 
44 #include <stdlib.h>
45 #ifdef HAVE_NETDB_H
46 #include <netdb.h>
47 #endif
48 #include <string.h>
49 #ifdef HAVE_GSSMIT
50 /* MIT style */
51 #include <gssapi/gssapi.h>
52 #include <gssapi/gssapi_generic.h>
53 #include <gssapi/gssapi_krb5.h>
54 #else
55 /* Heimdal-style */
56 #include <gssapi.h>
57 #endif
58 
59 #include "urldata.h"
60 #include "curl_base64.h"
61 #include "ftp.h"
62 #include "sendf.h"
63 #include "krb4.h"
64 #include "memory.h"
65 
66 #define _MPRINTF_REPLACE /* use our functions only */
67 #include <curl/mprintf.h>
68 
69 /* The last #include file should be: */
70 #include "memdebug.h"
71 
72 #define LOCAL_ADDR (&conn->local_addr)
73 #define REMOTE_ADDR conn->ip_addr->ai_addr
74 
75 static int
krb5_check_prot(void * app_data,int level)76 krb5_check_prot(void *app_data, int level)
77 {
78   app_data = NULL; /* prevent compiler warning */
79   if(level == prot_confidential)
80     return -1;
81   return 0;
82 }
83 
84 static int
krb5_decode(void * app_data,void * buf,int len,int level,struct connectdata * conn)85 krb5_decode(void *app_data, void *buf, int len, int level,
86             struct connectdata *conn)
87 {
88   gss_ctx_id_t *context = app_data;
89   OM_uint32 maj, min;
90   gss_buffer_desc enc, dec;
91 
92   /* shut gcc up */
93   level = 0;
94   conn = NULL;
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 
112 static int
krb5_overhead(void * app_data,int level,int len)113 krb5_overhead(void *app_data, int level, int len)
114 {
115   /* no arguments are used, just init them to prevent compiler warnings */
116   app_data = NULL;
117   level = 0;
118   len = 0;
119   return 0;
120 }
121 
122 static int
krb5_encode(void * app_data,const void * from,int length,int level,void ** to,struct connectdata * conn)123 krb5_encode(void *app_data, const void *from, int length, int level, void **to,
124             struct connectdata *conn)
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 expected */
149   *to = malloc(enc.length);
150   if(!*to)
151     return -1;
152   memcpy(*to, enc.value, enc.length);
153   len = enc.length;
154   gss_release_buffer(&min, &enc);
155   return len;
156 }
157 
158 static int
krb5_auth(void * app_data,struct connectdata * conn)159 krb5_auth(void *app_data, struct connectdata *conn)
160 {
161   int ret;
162   char *p;
163   const char *host = conn->dns_entry->addr->ai_canonname;
164   ssize_t nread;
165   socklen_t l = sizeof(conn->local_addr);
166   struct SessionHandle *data = conn->data;
167   CURLcode result;
168   const char *service = "ftp", *srv_host = "host";
169   gss_buffer_desc gssbuf, _gssresp, *gssresp;
170   OM_uint32 maj, min;
171   gss_name_t gssname;
172   gss_ctx_id_t *context = app_data;
173   struct gss_channel_bindings_struct chan;
174 
175   if(getsockname(conn->sock[FIRSTSOCKET],
176                  (struct sockaddr *)LOCAL_ADDR, &l) < 0)
177     perror("getsockname()");
178 
179   chan.initiator_addrtype = GSS_C_AF_INET;
180   chan.initiator_address.length = l - 4;
181   chan.initiator_address.value =
182     &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
183   chan.acceptor_addrtype = GSS_C_AF_INET;
184   chan.acceptor_address.length = l - 4;
185   chan.acceptor_address.value =
186     &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
187   chan.application_data.length = 0;
188   chan.application_data.value = NULL;
189 
190   /* this loop will execute twice (once for service, once for host) */
191   while(1) {
192     /* this really shouldn't be repeated here, but can't help it */
193     if(service == srv_host) {
194       result = Curl_ftpsendf(conn, "AUTH GSSAPI");
195 
196       if(result)
197         return -2;
198       if(Curl_GetFTPResponse(&nread, conn, NULL))
199         return -1;
200 
201       if(data->state.buffer[0] != '3')
202         return -1;
203     }
204 
205     gssbuf.value = data->state.buffer;
206     gssbuf.length = snprintf(gssbuf.value, BUFSIZE, "%s@%s", service, host);
207     maj = gss_import_name(&min, &gssbuf, GSS_C_NT_HOSTBASED_SERVICE, &gssname);
208     if(maj != GSS_S_COMPLETE) {
209       gss_release_name(&min, &gssname);
210       if(service == srv_host) {
211         Curl_failf(data, "Error importing service name %s", gssbuf.value);
212         return AUTH_ERROR;
213       }
214       service = srv_host;
215       continue;
216     }
217     {
218       gss_OID t;
219       gss_display_name(&min, gssname, &gssbuf, &t);
220       Curl_infof(data, "Trying against %s\n", gssbuf.value);
221       gss_release_buffer(&min, &gssbuf);
222     }
223     gssresp = GSS_C_NO_BUFFER;
224     *context = GSS_C_NO_CONTEXT;
225 
226     do {
227       ret = AUTH_OK;
228       maj = gss_init_sec_context(&min,
229                                  GSS_C_NO_CREDENTIAL,
230                                  context,
231                                  gssname,
232                                  GSS_C_NO_OID,
233                                  GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,
234                                  0,
235                                  &chan,
236                                  gssresp,
237                                  NULL,
238                                  &gssbuf,
239                                  NULL,
240                                  NULL);
241 
242       if(gssresp) {
243         free(_gssresp.value);
244         gssresp = NULL;
245       }
246 
247       if(maj != GSS_S_COMPLETE && maj != GSS_S_CONTINUE_NEEDED) {
248         Curl_infof(data, "Error creating security context");
249         ret = AUTH_ERROR;
250         break;
251       }
252 
253       if(gssbuf.length != 0) {
254         if(Curl_base64_encode(data, (char *)gssbuf.value, gssbuf.length, &p)
255            < 1) {
256           Curl_infof(data, "Out of memory base64-encoding");
257           ret = AUTH_CONTINUE;
258           break;
259         }
260 
261         result = Curl_ftpsendf(conn, "ADAT %s", p);
262 
263         free(p);
264 
265         if(result) {
266           ret = -2;
267           break;
268         }
269 
270         if(Curl_GetFTPResponse(&nread, conn, NULL)) {
271           ret = -1;
272           break;
273         }
274 
275         if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3'){
276           Curl_infof(data, "Server didn't accept auth data\n");
277           ret = AUTH_ERROR;
278           break;
279         }
280 
281         p = data->state.buffer + 4;
282         p = strstr(p, "ADAT=");
283         if(p) {
284           _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
285                                                &_gssresp.value);
286           if(_gssresp.length < 1) {
287             Curl_failf(data, "Out of memory base64-encoding");
288             ret = AUTH_CONTINUE;
289             break;
290           }
291         }
292 
293         gssresp = &_gssresp;
294       }
295     } while(maj == GSS_S_CONTINUE_NEEDED);
296 
297     gss_release_name(&min, &gssname);
298 
299     if(gssresp)
300       free(_gssresp.value);
301 
302     if(ret == AUTH_OK || service == srv_host)
303       return ret;
304 
305     service = srv_host;
306   }
307 }
308 
309 struct Curl_sec_client_mech Curl_krb5_client_mech = {
310     "GSSAPI",
311     sizeof(gss_ctx_id_t),
312     NULL, /* init */
313     krb5_auth,
314     NULL, /* end */
315     krb5_check_prot,
316     krb5_overhead,
317     krb5_encode,
318     krb5_decode
319 };
320 
321 #endif /* HAVE_GSSAPI */
322 #endif /* CURL_DISABLE_FTP */
323