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