Git init
[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, 2010 Kungliga Tekniska Högskolan
4  * (Royal Institute of Technology, Stockholm, Sweden).
5  * Copyright (c) 2004 - 2009 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, just init them to prevent compiler warnings */
128   app_data = NULL;
129   level = 0;
130   len = 0;
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 expected */
161   *to = malloc(enc.length);
162   if(!*to)
163     return -1;
164   memcpy(*to, enc.value, enc.length);
165   len = enc.length;
166   gss_release_buffer(&min, &enc);
167   return len;
168 }
169
170 static int
171 krb5_auth(void *app_data, struct connectdata *conn)
172 {
173   int ret = AUTH_OK;
174   char *p;
175   const char *host = conn->host.name;
176   ssize_t nread;
177   curl_socklen_t l = sizeof(conn->local_addr);
178   struct SessionHandle *data = conn->data;
179   CURLcode result;
180   const char *service = "ftp", *srv_host = "host";
181   gss_buffer_desc input_buffer, output_buffer, _gssresp, *gssresp;
182   OM_uint32 maj, min;
183   gss_name_t gssname;
184   gss_ctx_id_t *context = app_data;
185   struct gss_channel_bindings_struct chan;
186
187   if(getsockname(conn->sock[FIRSTSOCKET],
188                  (struct sockaddr *)LOCAL_ADDR, &l) < 0)
189     perror("getsockname()");
190
191   chan.initiator_addrtype = GSS_C_AF_INET;
192   chan.initiator_address.length = l - 4;
193   chan.initiator_address.value =
194     &((struct sockaddr_in *)LOCAL_ADDR)->sin_addr.s_addr;
195   chan.acceptor_addrtype = GSS_C_AF_INET;
196   chan.acceptor_address.length = l - 4;
197   chan.acceptor_address.value =
198     &((struct sockaddr_in *)REMOTE_ADDR)->sin_addr.s_addr;
199   chan.application_data.length = 0;
200   chan.application_data.value = NULL;
201
202   /* this loop will execute twice (once for service, once for host) */
203   while(1) {
204     /* this really shouldn't be repeated here, but can't help it */
205     if(service == srv_host) {
206       result = Curl_ftpsendf(conn, "AUTH GSSAPI");
207
208       if(result)
209         return -2;
210       if(Curl_GetFTPResponse(&nread, conn, NULL))
211         return -1;
212
213       if(data->state.buffer[0] != '3')
214         return -1;
215     }
216
217     input_buffer.value = data->state.buffer;
218     input_buffer.length = snprintf(input_buffer.value, BUFSIZE, "%s@%s",
219                                    service, host);
220     maj = gss_import_name(&min, &input_buffer, GSS_C_NT_HOSTBASED_SERVICE,
221                           &gssname);
222     if(maj != GSS_S_COMPLETE) {
223       gss_release_name(&min, &gssname);
224       if(service == srv_host) {
225         Curl_failf(data, "Error importing service name %s", input_buffer.value);
226         return AUTH_ERROR;
227       }
228       service = srv_host;
229       continue;
230     }
231     /* We pass NULL as |output_name_type| to avoid a leak. */
232     gss_display_name(&min, gssname, &output_buffer, NULL);
233     Curl_infof(data, "Trying against %s\n", output_buffer.value);
234     gssresp = GSS_C_NO_BUFFER;
235     *context = GSS_C_NO_CONTEXT;
236
237     do {
238       /* Release the buffer at each iteration to avoid leaking: the first time
239          we are releasing the memory from gss_display_name. The last item is
240          taken care by a final gss_release_buffer. */
241       gss_release_buffer(&min, &output_buffer);
242       ret = AUTH_OK;
243       maj = gss_init_sec_context(&min,
244                                  GSS_C_NO_CREDENTIAL,
245                                  context,
246                                  gssname,
247                                  GSS_C_NO_OID,
248                                  GSS_C_MUTUAL_FLAG | GSS_C_REPLAY_FLAG,
249                                  0,
250                                  &chan,
251                                  gssresp,
252                                  NULL,
253                                  &output_buffer,
254                                  NULL,
255                                  NULL);
256
257       if(gssresp) {
258         free(_gssresp.value);
259         gssresp = NULL;
260       }
261
262       if(GSS_ERROR(maj)) {
263         Curl_infof(data, "Error creating security context\n");
264         ret = AUTH_ERROR;
265         break;
266       }
267
268       if(output_buffer.length != 0) {
269         if(Curl_base64_encode(data, (char *)output_buffer.value,
270                               output_buffer.length, &p) < 1) {
271           Curl_infof(data, "Out of memory base64-encoding\n");
272           ret = AUTH_CONTINUE;
273           break;
274         }
275
276         result = Curl_ftpsendf(conn, "ADAT %s", p);
277
278         free(p);
279
280         if(result) {
281           ret = -2;
282           break;
283         }
284
285         if(Curl_GetFTPResponse(&nread, conn, NULL)) {
286           ret = -1;
287           break;
288         }
289
290         if(data->state.buffer[0] != '2' && data->state.buffer[0] != '3'){
291           Curl_infof(data, "Server didn't accept auth data\n");
292           ret = AUTH_ERROR;
293           break;
294         }
295
296         p = data->state.buffer + 4;
297         p = strstr(p, "ADAT=");
298         if(p) {
299           _gssresp.length = Curl_base64_decode(p + 5, (unsigned char **)
300                                                &_gssresp.value);
301           if(_gssresp.length < 1) {
302             Curl_failf(data, "Out of memory base64-encoding\n");
303             ret = AUTH_CONTINUE;
304             break;
305           }
306         }
307
308         gssresp = &_gssresp;
309       }
310     } while(maj == GSS_S_CONTINUE_NEEDED);
311
312     gss_release_name(&min, &gssname);
313     gss_release_buffer(&min, &output_buffer);
314
315     if(gssresp)
316       free(_gssresp.value);
317
318     if(ret == AUTH_OK || service == srv_host)
319       return ret;
320
321     service = srv_host;
322   }
323   return ret;
324 }
325
326 static void krb5_end(void *app_data)
327 {
328     OM_uint32 maj, min;
329     gss_ctx_id_t *context = app_data;
330     if (*context != GSS_C_NO_CONTEXT) {
331       maj = gss_delete_sec_context(&min, context, GSS_C_NO_BUFFER);
332       DEBUGASSERT(maj == GSS_S_COMPLETE);
333     }
334 }
335
336 struct Curl_sec_client_mech Curl_krb5_client_mech = {
337     "GSSAPI",
338     sizeof(gss_ctx_id_t),
339     krb5_init,
340     krb5_auth,
341     krb5_end,
342     krb5_check_prot,
343     krb5_overhead,
344     krb5_encode,
345     krb5_decode
346 };
347
348 #endif /* HAVE_GSSAPI */
349 #endif /* CURL_DISABLE_FTP */