a0d7bb4f07cd0dda161554d95dc91700be008cf1
[platform/upstream/curl.git] / lib / krb5.c
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 - 2014 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
67 static int
68 krb5_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
76 static int
77 krb5_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
85 static int
86 krb5_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
113 static int
114 krb5_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
123 static int
124 krb5_encode(void *app_data, const void *from, int length, int level, void **to)
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   /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
133    * libraries modify the input buffer in gss_seal()
134    */
135   dec.value = (void*)from;
136   dec.length = length;
137   maj = gss_seal(&min, *context,
138                  level == PROT_PRIVATE,
139                  GSS_C_QOP_DEFAULT,
140                  &dec, &state, &enc);
141
142   if(maj != GSS_S_COMPLETE)
143     return -1;
144
145   /* malloc a new buffer, in case gss_release_buffer doesn't work as
146      expected */
147   *to = malloc(enc.length);
148   if(!*to)
149     return -1;
150   memcpy(*to, enc.value, enc.length);
151   len = curlx_uztosi(enc.length);
152   gss_release_buffer(&min, &enc);
153   return len;
154 }
155
156 static int
157 krb5_auth(void *app_data, struct connectdata *conn)
158 {
159   int ret = AUTH_OK;
160   char *p;
161   const char *host = conn->host.name;
162   ssize_t nread;
163   curl_socklen_t l = sizeof(conn->local_addr);
164   struct SessionHandle *data = conn->data;
165   CURLcode result;
166   const char *service = "ftp", *srv_host = "host";
167   gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
168   OM_uint32 maj, min;
169   gss_name_t gssname;
170   gss_ctx_id_t *context = app_data;
171   struct gss_channel_bindings_struct chan;
172   size_t base64_sz = 0;
173
174   if(getsockname(conn->sock[FIRSTSOCKET],
175                  (struct sockaddr *)LOCAL_ADDR, &l) < 0)
176     perror("getsockname()");
177
178   chan.initiator_addrtype = GSS_C_AF_INET;
179   chan.initiator_address.length = l - 4;
180   chan.initiator_address.value =
181     &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
182   chan.acceptor_addrtype = GSS_C_AF_INET;
183   chan.acceptor_address.length = l - 4;
184   chan.acceptor_address.value =
185     &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
186   chan.application_data.length = 0;
187   chan.application_data.value = NULL;
188
189   /* this loop will execute twice (once for service, once for host) */
190   for(;;) {
191     /* this really shouldn't be repeated here, but can't help it */
192     if(service == srv_host) {
193       result = Curl_ftpsendf(conn, "AUTH GSSAPI");
194
195       if(result)
196         return -2;
197       if(Curl_GetFTPResponse(&nread, conn, NULL))
198         return -1;
199
200       if(data->state.buffer[0] != '3')
201         return -1;
202     }
203
204     input_buffer.value = data->state.buffer;
205     input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
206                                    service, host);
207     maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
208                           &gssname);
209     if(maj != GSS_S_COMPLETE) {
210       gss_release_name(&min, &gssname);
211       if(service == srv_host) {
212         Curl_failf(data, "Error importing service name %s",
213                    input_buffer.value);
214         return AUTH_ERROR;
215       }
216       service = srv_host;
217       continue;
218     }
219     /* We pass NULL as |output_name_type| to avoid a leak. */
220     gss_display_name(&min, gssname, &output_buffer, NULL);
221     Curl_infof(data, "Trying against %s\n", output_buffer.value);
222     gssresp = GSS_C_NO_BUFFER;
223     *context = GSS_C_NO_CONTEXT;
224
225     do {
226       /* Release the buffer at each iteration to avoid leaking: the first time
227          we are releasing the memory from gss_display_name. The last item is
228          taken care by a final gss_release_buffer. */
229       gss_release_buffer(&min, &output_buffer);
230       ret = AUTH_OK;
231       maj = Curl_gss_init_sec_context(data,
232                                       &min,
233                                       context,
234                                       gssname,
235                                       &Curl_krb5_mech_oid,
236                                       &chan,
237                                       gssresp,
238                                       &output_buffer,
239                                       TRUE,
240                                       NULL);
241
242       if(gssresp) {
243         free(_gssresp.value);
244         gssresp = NULL;
245       }
246
247       if(GSS_ERROR(maj)) {
248         Curl_infof(data, "Error creating security context\n");
249         ret = AUTH_ERROR;
250         break;
251       }
252
253       if(output_buffer.length != 0) {
254         result = Curl_base64_encode(data, (char *)output_buffer.value,
255                                     output_buffer.length, &p, &base64_sz);
256         if(result) {
257           Curl_infof(data,"base64-encoding: %s\n", curl_easy_strerror(result));
258           ret = AUTH_CONTINUE;
259           break;
260         }
261
262         result = Curl_ftpsendf(conn, "ADAT %s", p);
263
264         free(p);
265
266         if(result) {
267           ret = -2;
268           break;
269         }
270
271         if(Curl_GetFTPResponse(&nread, conn, NULL)) {
272           ret = -1;
273           break;
274         }
275
276         if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
277           Curl_infof(data, "Server didn't accept auth data\n");
278           ret = AUTH_ERROR;
279           break;
280         }
281
282         p = data->state.buffer + 4;
283         p = strstr(p, "ADAT=");
284         if(p) {
285           result = Curl_base64_decode(p + 5,
286                                       (unsigned char **)&_gssresp.value,
287                                       &_gssresp.length);
288           if(result) {
289             Curl_failf(data,"base64-decoding: %s", curl_easy_strerror(result));
290             ret = AUTH_CONTINUE;
291             break;
292           }
293         }
294
295         gssresp = &_gssresp;
296       }
297     } while(maj == GSS_S_CONTINUE_NEEDED);
298
299     gss_release_name(&min, &gssname);
300     gss_release_buffer(&min, &output_buffer);
301
302     if(gssresp)
303       free(_gssresp.value);
304
305     if(ret == AUTH_OK || service == srv_host)
306       return ret;
307
308     service = srv_host;
309   }
310   return ret;
311 }
312
313 static void krb5_end(void *app_data)
314 {
315     OM_uint32 min;
316     gss_ctx_id_t *context = app_data;
317     if(*context != GSS_C_NO_CONTEXT) {
318 #ifdef DEBUGBUILD
319       OM_uint32 maj =
320 #endif
321       gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
322       DEBUGASSERT(maj == GSS_S_COMPLETE);
323     }
324 }
325
326 struct Curl_sec_client_mech Curl_krb5_client_mech = {
327     "GSSAPI",
328     sizeof(gss_ctx_id_t),
329     krb5_init,
330     krb5_auth,
331     krb5_end,
332     krb5_check_prot,
333     krb5_overhead,
334     krb5_encode,
335     krb5_decode
336 };
337
338 #endif /* HAVE_GSSAPI */
339 #endif /* CURL_DISABLE_FTP */