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