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