Added Curl_gss_init_sec_context.
[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 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 #endif
43
44 #include <stdlib.h>
45 #ifdef HAVE_NETDB_H
46 #include <netdb.h>
47 #endif
48 #include <string.h>
49
50 #include "urldata.h"
51 #include "curl_base64.h"
52 #include "ftp.h"
53 #include "gssapi.h"
54 #include "sendf.h"
55 #include "krb4.h"
56 #include "curl_memory.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, int level,
87             struct connectdata *conn)
88 {
89   gss_ctx_id_t *context = app_data;
90   OM_uint32 maj, min;
91   gss_buffer_desc enc, dec;
92
93   /* shut gcc up */
94   level = 0;
95   conn = NULL;
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 = 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             struct connectdata *conn)
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 = enc.length;
156   gss_release_buffer(&min, &enc);
157   return len;
158 }
159
160 static int
161 krb5_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
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   while(1) {
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(&min,
235                                       GSS_C_NO_CREDENTIAL,
236                                       context,
237                                       gssname,
238                                       GSS_C_NO_OID,
239                                       GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,
240                                       0,
241                                       &chan,
242                                       gssresp,
243                                       NULL,
244                                       &output_buffer,
245                                       NULL,
246                                       NULL);
247
248       if(gssresp) {
249         free(_gssresp.value);
250         gssresp = NULL;
251       }
252
253       if(GSS_ERROR(maj)) {
254         Curl_infof(data, "Error creating security context\n");
255         ret = AUTH_ERROR;
256         break;
257       }
258
259       if(output_buffer.length != 0) {
260         if(Curl_base64_encode(data, (char *)output_buffer.value,
261                               output_buffer.length, &p) < 1) {
262           Curl_infof(data, "Out of memory base64-encoding\n");
263           ret = AUTH_CONTINUE;
264           break;
265         }
266
267         result = Curl_ftpsendf(conn, "ADAT %s", p);
268
269         free(p);
270
271         if(result) {
272           ret = -2;
273           break;
274         }
275
276         if(Curl_GetFTPResponse(&nread, conn, NULL)) {
277           ret = -1;
278           break;
279         }
280
281         if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
282           Curl_infof(data, "Server didn't accept auth data\n");
283           ret = AUTH_ERROR;
284           break;
285         }
286
287         p = data->state.buffer + 4;
288         p = strstr(p, "ADAT=");
289         if(p) {
290           _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
291                                                &_gssresp.value);
292           if(_gssresp.length < 1) {
293             Curl_failf(data, "Out of memory base64-encoding\n");
294             ret = AUTH_CONTINUE;
295             break;
296           }
297         }
298
299         gssresp = &_gssresp;
300       }
301     } while(maj == GSS_S_CONTINUE_NEEDED);
302
303     gss_release_name(&min, &gssname);
304     gss_release_buffer(&min, &output_buffer);
305
306     if(gssresp)
307       free(_gssresp.value);
308
309     if(ret == AUTH_OK || service == srv_host)
310       return ret;
311
312     service = srv_host;
313   }
314   return ret;
315 }
316
317 static void krb5_end(void *app_data)
318 {
319     OM_uint32 maj, min;
320     gss_ctx_id_t *context = app_data;
321     if(*context != GSS_C_NO_CONTEXT) {
322       maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
323       DEBUGASSERT(maj == GSS_S_COMPLETE);
324     }
325 }
326
327 struct Curl_sec_client_mech Curl_krb5_client_mech = {
328     "GSSAPI",
329     sizeof(gss_ctx_id_t),
330     krb5_init,
331     krb5_auth,
332     krb5_end,
333     krb5_check_prot,
334     krb5_overhead,
335     krb5_encode,
336     krb5_decode
337 };
338
339 #endif /* HAVE_GSSAPI */
340 #endif /* CURL_DISABLE_FTP */