stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h
[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 #ifdef HAVE_NETDB_H
45 #include <netdb.h>
46 #endif
47
48 #include "urldata.h"
49 #include "curl_base64.h"
50 #include "ftp.h"
51 #include "curl_gssapi.h"
52 #include "sendf.h"
53 #include "krb4.h"
54 #include "curl_memory.h"
55
56 #define _MPRINTF_REPLACE /* use our functions only */
57 #include <curl/mprintf.h>
58
59 /* The last #include file should be: */
60 #include "memdebug.h"
61
62 #define LOCAL_ADDR (&conn->local_addr)
63 #define REMOTE_ADDR conn->ip_addr->ai_addr
64
65 static int
66 krb5_init(void *app_data)
67 {
68   gss_ctx_id_t *context = app_data;
69   /* Make sure our context is initialized for krb5_end. */
70   *context = GSS_C_NO_CONTEXT;
71   return 0;
72 }
73
74 static int
75 krb5_check_prot(void *app_data, int level)
76 {
77   (void)app_data; /* unused */
78   if(level == PROT_CONFIDENTIAL)
79     return -1;
80   return 0;
81 }
82
83 static int
84 krb5_decode(void *app_data, void *buf, int len,
85             int level UNUSED_PARAM,
86             struct connectdata *conn UNUSED_PARAM)
87 {
88   gss_ctx_id_t *context = app_data;
89   OM_uint32 maj, min;
90   gss_buffer_desc enc, dec;
91
92   (void)level;
93   (void)conn;
94
95   enc.value = buf;
96   enc.length = len;
97   maj = gss_unseal(&min, *context, &enc, &dec, NULL, NULL);
98   if(maj != GSS_S_COMPLETE) {
99     if(len >= 4)
100       strcpy(buf, "599 ");
101     return -1;
102   }
103
104   memcpy(buf, dec.value, dec.length);
105   len = dec.length;
106   gss_release_buffer(&min, &dec);
107
108   return len;
109 }
110
111 static int
112 krb5_overhead(void *app_data, int level, int len)
113 {
114   /* no arguments are used */
115   (void)app_data;
116   (void)level;
117   (void)len;
118   return 0;
119 }
120
121 static int
122 krb5_encode(void *app_data, const void *from, int length, int level, void **to,
123             struct connectdata *conn UNUSED_PARAM)
124 {
125   gss_ctx_id_t *context = app_data;
126   gss_buffer_desc dec, enc;
127   OM_uint32 maj, min;
128   int state;
129   int len;
130
131   /* shut gcc up */
132   conn = NULL;
133
134   /* NOTE that the cast is safe, neither of the krb5, gnu gss and heimdal
135    * libraries modify the input buffer in gss_seal()
136    */
137   dec.value = (void*)from;
138   dec.length = length;
139   maj = gss_seal(&min, *context,
140                  level == PROT_PRIVATE,
141                  GSS_C_QOP_DEFAULT,
142                  &dec, &state, &enc);
143
144   if(maj != GSS_S_COMPLETE)
145     return -1;
146
147   /* malloc a new buffer, in case gss_release_buffer doesn't work as
148      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
159 krb5_auth(void *app_data, struct connectdata *conn)
160 {
161   int ret = AUTH_OK;
162   char *p;
163   const char *host = conn->host.name;
164   ssize_t nread;
165   curl_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 input_buffer, output_buffer, _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     input_buffer.value = data->state.buffer;
206     input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
207                                    service, host);
208     maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
209                           &gssname);
210     if(maj != GSS_S_COMPLETE) {
211       gss_release_name(&min, &gssname);
212       if(service == srv_host) {
213         Curl_failf(data, "Error importing service name %s",
214                    input_buffer.value);
215         return AUTH_ERROR;
216       }
217       service = srv_host;
218       continue;
219     }
220     /* We pass NULL as |output_name_type| to avoid a leak. */
221     gss_display_name(&min, gssname, &output_buffer, NULL);
222     Curl_infof(data, "Trying against %s\n", output_buffer.value);
223     gssresp = GSS_C_NO_BUFFER;
224     *context = GSS_C_NO_CONTEXT;
225
226     do {
227       /* Release the buffer at each iteration to avoid leaking: the first time
228          we are releasing the memory from gss_display_name. The last item is
229          taken care by a final gss_release_buffer. */
230       gss_release_buffer(&min, &output_buffer);
231       ret = AUTH_OK;
232       maj = Curl_gss_init_sec_context(&min,
233                                       context,
234                                       gssname,
235                                       &chan,
236                                       gssresp,
237                                       &output_buffer,
238                                       NULL);
239
240       if(gssresp) {
241         free(_gssresp.value);
242         gssresp = NULL;
243       }
244
245       if(GSS_ERROR(maj)) {
246         Curl_infof(data, "Error creating security context\n");
247         ret = AUTH_ERROR;
248         break;
249       }
250
251       if(output_buffer.length != 0) {
252         if(Curl_base64_encode(data, (char *)output_buffer.value,
253                               output_buffer.length, &p) < 1) {
254           Curl_infof(data, "Out of memory base64-encoding\n");
255           ret = AUTH_CONTINUE;
256           break;
257         }
258
259         result = Curl_ftpsendf(conn, "ADAT %s", p);
260
261         free(p);
262
263         if(result) {
264           ret = -2;
265           break;
266         }
267
268         if(Curl_GetFTPResponse(&nread, conn, NULL)) {
269           ret = -1;
270           break;
271         }
272
273         if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3') {
274           Curl_infof(data, "Server didn't accept auth data\n");
275           ret = AUTH_ERROR;
276           break;
277         }
278
279         p = data->state.buffer + 4;
280         p = strstr(p, "ADAT=");
281         if(p) {
282           _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
283                                                &_gssresp.value);
284           if(_gssresp.length < 1) {
285             Curl_failf(data, "Out of memory base64-encoding\n");
286             ret = AUTH_CONTINUE;
287             break;
288           }
289         }
290
291         gssresp = &_gssresp;
292       }
293     } while(maj == GSS_S_CONTINUE_NEEDED);
294
295     gss_release_name(&min, &gssname);
296     gss_release_buffer(&min, &output_buffer);
297
298     if(gssresp)
299       free(_gssresp.value);
300
301     if(ret == AUTH_OK || service == srv_host)
302       return ret;
303
304     service = srv_host;
305   }
306   return ret;
307 }
308
309 static void krb5_end(void *app_data)
310 {
311     OM_uint32 maj, min;
312     gss_ctx_id_t *context = app_data;
313     if(*context != GSS_C_NO_CONTEXT) {
314       maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
315       DEBUGASSERT(maj == GSS_S_COMPLETE);
316     }
317 }
318
319 struct Curl_sec_client_mech Curl_krb5_client_mech = {
320     "GSSAPI",
321     sizeof(gss_ctx_id_t),
322     krb5_init,
323     krb5_auth,
324     krb5_end,
325     krb5_check_prot,
326     krb5_overhead,
327     krb5_encode,
328     krb5_decode
329 };
330
331 #endif /* HAVE_GSSAPI */
332 #endif /* CURL_DISABLE_FTP */