stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setup_once.h
[platform/upstream/curl.git] / lib / http_negotiate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "setup.h"
24
25 #ifdef HAVE_GSSAPI
26 #ifdef HAVE_OLD_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
28 #endif
29
30 #ifndef CURL_DISABLE_HTTP
31
32 #include "urldata.h"
33 #include "sendf.h"
34 #include "curl_gssapi.h"
35 #include "rawstr.h"
36 #include "curl_base64.h"
37 #include "http_negotiate.h"
38 #include "curl_memory.h"
39
40 #ifdef HAVE_SPNEGO
41 #  include <spnegohelp.h>
42 #  ifdef USE_SSLEAY
43 #    ifdef USE_OPENSSL
44 #      include <openssl/objects.h>
45 #    else
46 #      include <objects.h>
47 #    endif
48 #  else
49 #    error "Can't compile SPNEGO support without OpenSSL."
50 #  endif
51 #endif
52
53 #define _MPRINTF_REPLACE /* use our functions only */
54 #include <curl/mprintf.h>
55
56 /* The last #include file should be: */
57 #include "memdebug.h"
58
59 static int
60 get_gss_name(struct connectdata *conn, bool proxy, gss_name_t *server)
61 {
62   struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
63     &conn->data->state.negotiate;
64   OM_uint32 major_status, minor_status;
65   gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
66   char name[2048];
67   const char* service;
68
69   /* GSSAPI implementation by Globus (known as GSI) requires the name to be
70      of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
71      of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
72      Change following lines if you want to use GSI */
73
74   /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
75
76   if(neg_ctx->gss)
77     service = "KHTTP";
78   else
79     service = "HTTP";
80
81   token.length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
82                                               conn->host.name) + 1;
83   if(token.length + 1 > sizeof(name))
84     return EMSGSIZE;
85
86   snprintf(name, sizeof(name), "%s@%s", service, proxy ? conn->proxy.name :
87            conn->host.name);
88
89   token.value = (void *) name;
90   major_status = gss_import_name(&minor_status,
91                                  &token,
92                                  GSS_C_NT_HOSTBASED_SERVICE,
93                                  server);
94
95   return GSS_ERROR(major_status) ? -1 : 0;
96 }
97
98 static void
99 log_gss_error(struct connectdata *conn, OM_uint32 error_status,
100               const char *prefix)
101 {
102   OM_uint32 maj_stat, min_stat;
103   OM_uint32 msg_ctx = 0;
104   gss_buffer_desc status_string;
105   char buf[1024];
106   size_t len;
107
108   snprintf(buf, sizeof(buf), "%s", prefix);
109   len = strlen(buf);
110   do {
111     maj_stat = gss_display_status(&min_stat,
112                                   error_status,
113                                   GSS_C_MECH_CODE,
114                                   GSS_C_NO_OID,
115                                   &msg_ctx,
116                                   &status_string);
117       if(sizeof(buf) > len + status_string.length + 1) {
118         snprintf(buf + len, sizeof(buf) - len,
119                  ": %s", (char*) status_string.value);
120       len += status_string.length;
121     }
122     gss_release_buffer(&min_stat, &status_string);
123   } while(!GSS_ERROR(maj_stat) && msg_ctx != 0);
124
125   infof(conn->data, "%s", buf);
126 }
127
128 /* returning zero (0) means success, everything else is treated as "failure"
129    with no care exactly what the failure was */
130 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
131                          const char *header)
132 {
133   struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
134     &conn->data->state.negotiate;
135   OM_uint32 major_status, minor_status, minor_status2;
136   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
137   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
138   int ret;
139   size_t len, rawlen;
140   bool gss;
141   const char* protocol;
142
143   while(*header && ISSPACE(*header))
144     header++;
145   if(checkprefix("GSS-Negotiate", header)) {
146     protocol = "GSS-Negotiate";
147     gss = TRUE;
148   }
149   else if(checkprefix("Negotiate", header)) {
150     protocol = "Negotiate";
151     gss = FALSE;
152   }
153   else
154     return -1;
155
156   if(neg_ctx->context) {
157     if(neg_ctx->gss != gss) {
158       return -1;
159     }
160   }
161   else {
162     neg_ctx->protocol = protocol;
163     neg_ctx->gss = gss;
164   }
165
166   if(neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
167     /* We finished successfully our part of authentication, but server
168      * rejected it (since we're again here). Exit with an error since we
169      * can't invent anything better */
170     Curl_cleanup_negotiate(conn->data);
171     return -1;
172   }
173
174   if(neg_ctx->server_name == NULL &&
175       (ret = get_gss_name(conn, proxy, &neg_ctx->server_name)))
176     return ret;
177
178   header += strlen(neg_ctx->protocol);
179   while(*header && ISSPACE(*header))
180     header++;
181
182   len = strlen(header);
183   if(len > 0) {
184     rawlen = Curl_base64_decode(header,
185                                 (unsigned char **)&input_token.value);
186     if(rawlen == 0)
187       return -1;
188     input_token.length = rawlen;
189
190 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
191     if(checkprefix("Negotiate", header)) {
192       ASN1_OBJECT *   object            = NULL;
193       int             rc                = 1;
194       unsigned char * spnegoToken       = NULL;
195       size_t          spnegoTokenLength = 0;
196       unsigned char * mechToken         = NULL;
197       size_t          mechTokenLength   = 0;
198
199       if(input_token.value == NULL)
200         return CURLE_OUT_OF_MEMORY;
201
202       spnegoToken = malloc(input_token.length);
203       if(spnegoToken == NULL)
204         return CURLE_OUT_OF_MEMORY;
205
206       spnegoTokenLength = input_token.length;
207
208       object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
209       if(!parseSpnegoTargetToken(spnegoToken,
210                                  spnegoTokenLength,
211                                  NULL,
212                                  NULL,
213                                  &mechToken,
214                                  &mechTokenLength,
215                                  NULL,
216                                  NULL)) {
217         free(spnegoToken);
218         spnegoToken = NULL;
219         infof(conn->data, "Parse SPNEGO Target Token failed\n");
220       }
221       else {
222         free(input_token.value);
223         input_token.value = malloc(mechTokenLength);
224         if(input_token.value == NULL)
225           return CURLE_OUT_OF_MEMORY;
226
227         memcpy(input_token.value, mechToken,mechTokenLength);
228         input_token.length = mechTokenLength;
229         free(mechToken);
230         mechToken = NULL;
231         infof(conn->data, "Parse SPNEGO Target Token succeeded\n");
232       }
233     }
234 #endif
235   }
236
237   major_status = Curl_gss_init_sec_context(&minor_status,
238                                            &neg_ctx->context,
239                                            neg_ctx->server_name,
240                                            GSS_C_NO_CHANNEL_BINDINGS,
241                                            &input_token,
242                                            &output_token,
243                                            NULL);
244   if(input_token.length > 0)
245     gss_release_buffer(&minor_status2, &input_token);
246   neg_ctx->status = major_status;
247   if(GSS_ERROR(major_status)) {
248     /* Curl_cleanup_negotiate(conn->data) ??? */
249     log_gss_error(conn, minor_status,
250                   "gss_init_sec_context() failed: ");
251     return -1;
252   }
253
254   if(output_token.length == 0) {
255     return -1;
256   }
257
258   neg_ctx->output_token = output_token;
259   /* conn->bits.close = FALSE; */
260
261   return 0;
262 }
263
264
265 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
266 {
267   struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
268     &conn->data->state.negotiate;
269   char *encoded = NULL;
270   size_t len;
271   char *userp;
272
273 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
274   if(checkprefix("Negotiate", neg_ctx->protocol)) {
275     ASN1_OBJECT *   object            = NULL;
276     int             rc                = 1;
277     unsigned char * spnegoToken       = NULL;
278     size_t          spnegoTokenLength = 0;
279     unsigned char * responseToken       = NULL;
280     size_t          responseTokenLength = 0;
281
282     responseToken = malloc(neg_ctx->output_token.length);
283     if(responseToken == NULL)
284       return CURLE_OUT_OF_MEMORY;
285     memcpy(responseToken, neg_ctx->output_token.value,
286            neg_ctx->output_token.length);
287     responseTokenLength = neg_ctx->output_token.length;
288
289     object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
290     if(!makeSpnegoInitialToken (object,
291                                  responseToken,
292                                  responseTokenLength,
293                                  &spnegoToken,
294                                  &spnegoTokenLength)) {
295       free(responseToken);
296       responseToken = NULL;
297       infof(conn->data, "Make SPNEGO Initial Token failed\n");
298     }
299     else {
300       free(responseToken);
301       responseToken = NULL;
302       free(neg_ctx->output_token.value);
303       neg_ctx->output_token.value = malloc(spnegoTokenLength);
304       if(neg_ctx->output_token.value == NULL) {
305         free(spnegoToken);
306         spnegoToken = NULL;
307         return CURLE_OUT_OF_MEMORY;
308       }
309       memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
310       neg_ctx->output_token.length = spnegoTokenLength;
311       free(spnegoToken);
312       spnegoToken = NULL;
313       infof(conn->data, "Make SPNEGO Initial Token succeeded\n");
314     }
315   }
316 #endif
317   len = Curl_base64_encode(conn->data,
318                            neg_ctx->output_token.value,
319                            neg_ctx->output_token.length,
320                            &encoded);
321
322   if(len == 0)
323     return CURLE_OUT_OF_MEMORY;
324
325   userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
326                   neg_ctx->protocol, encoded);
327
328   if(proxy)
329     conn->allocptr.proxyuserpwd = userp;
330   else
331     conn->allocptr.userpwd = userp;
332   free(encoded);
333   Curl_cleanup_negotiate (conn->data);
334   return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
335 }
336
337 static void cleanup(struct negotiatedata *neg_ctx)
338 {
339   OM_uint32 minor_status;
340   if(neg_ctx->context != GSS_C_NO_CONTEXT)
341     gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
342
343   if(neg_ctx->output_token.length != 0)
344     gss_release_buffer(&minor_status, &neg_ctx->output_token);
345
346   if(neg_ctx->server_name != GSS_C_NO_NAME)
347     gss_release_name(&minor_status, &neg_ctx->server_name);
348
349   memset(neg_ctx, 0, sizeof(*neg_ctx));
350 }
351
352 void Curl_cleanup_negotiate(struct SessionHandle *data)
353 {
354   cleanup(&data->state.negotiate);
355   cleanup(&data->state.proxyneg);
356 }
357
358
359 #endif
360 #endif