Replaced all uses of sprintf() with the safer snprintf(). It is just a
[platform/upstream/curl.git] / lib / http_negotiate.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2004, 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  * $Id$
22  ***************************************************************************/
23 #include "setup.h"
24
25 #ifdef HAVE_GSSAPI
26 #ifdef HAVE_GSSMIT
27 #define GSS_C_NT_HOSTBASED_SERVICE gss_nt_service_name
28 #endif
29
30 #ifndef CURL_DISABLE_HTTP
31  /* -- WIN32 approved -- */
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <errno.h>
38
39 #include "urldata.h"
40 #include "sendf.h"
41 #include "strequal.h"
42 #include "base64.h"
43 #include "http_negotiate.h"
44 #include "memory.h"
45
46 #define _MPRINTF_REPLACE /* use our functions only */
47 #include <curl/mprintf.h>
48
49 /* The last #include file should be: */
50 #include "memdebug.h"
51
52 static int
53 get_gss_name(struct connectdata *conn, gss_name_t *server)
54 {
55   struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
56   OM_uint32 major_status, minor_status;
57   gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
58   char name[2048];
59   const char* service;
60
61   /* GSSAPI implementation by Globus (known as GSI) requires the name to be
62      of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
63      of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
64      Change following lines if you want to use GSI */
65
66   /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name */
67
68   if (neg_ctx->gss)
69     service = "khttp";
70   else
71     service = "http";
72
73   token.length = strlen(service) + 1 + strlen(conn->host.name) + 1;
74   if (token.length + 1 > sizeof(name))
75     return EMSGSIZE;
76
77   snprintf(name, sizeof(name), "%s@%s", service, conn->host.name);
78
79   token.value = (void *) name;
80   major_status = gss_import_name(&minor_status,
81                                  &token,
82                                  GSS_C_NT_HOSTBASED_SERVICE,
83                                  server);
84
85   return GSS_ERROR(major_status) ? -1 : 0;
86 }
87
88 static void
89 log_gss_error(struct connectdata *conn, OM_uint32 error_status, char *prefix)
90 {
91   OM_uint32 maj_stat, min_stat;
92   OM_uint32 msg_ctx = 0;
93   gss_buffer_desc status_string;
94   char buf[1024];
95   size_t len;
96
97   snprintf(buf, sizeof(buf), "%s", prefix);
98   len = strlen(buf);
99   do {
100     maj_stat = gss_display_status (&min_stat,
101                                    error_status,
102                                    GSS_C_MECH_CODE,
103                                    GSS_C_NO_OID,
104                                    &msg_ctx,
105                                    &status_string);
106       if (sizeof(buf) > len + status_string.length + 1) {
107         snprintf(buf + len, sizeof(buf) - len,
108                  ": %s", (char*) status_string.value);
109       len += status_string.length;
110     }
111     gss_release_buffer(&min_stat, &status_string);
112   } while (!GSS_ERROR(maj_stat) && msg_ctx != 0);
113
114   infof(conn->data, buf);
115 }
116
117 int Curl_input_negotiate(struct connectdata *conn, char *header)
118 {
119   struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
120   OM_uint32 major_status, minor_status, minor_status2;
121   gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
122   gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
123   int ret;
124   size_t len;
125   bool gss;
126   const char* protocol;
127
128   while(*header && isspace((int)*header))
129     header++;
130   if(checkprefix("GSS-Negotiate", header)) {
131     protocol = "GSS-Negotiate";
132     gss = TRUE;
133   }
134   else if (checkprefix("Negotiate", header)) {
135     protocol = "Negotiate";
136     gss = FALSE;
137   }
138   else
139     return -1;
140
141   if (neg_ctx->context) {
142     if (neg_ctx->gss != gss) {
143       return -1;
144     }
145   }
146   else {
147     neg_ctx->protocol = protocol;
148     neg_ctx->gss = gss;
149   }
150
151   if (neg_ctx->context && neg_ctx->status == GSS_S_COMPLETE) {
152     /* We finished succesfully our part of authentication, but server
153      * rejected it (since we're again here). Exit with an error since we
154      * can't invent anything better */
155     Curl_cleanup_negotiate(conn->data);
156     return -1;
157   }
158
159   if (neg_ctx->server_name == NULL &&
160       (ret = get_gss_name(conn, &neg_ctx->server_name)))
161     return ret;
162
163   header += strlen(neg_ctx->protocol);
164   while(*header && isspace((int)*header))
165     header++;
166
167   len = strlen(header);
168   if (len > 0) {
169     int rawlen;
170     input_token.length = (len+3)/4 * 3;
171     input_token.value = malloc(input_token.length);
172     if (input_token.value == NULL)
173       return ENOMEM;
174     rawlen = Curl_base64_decode(header, input_token.value);
175     if (rawlen < 0)
176       return -1;
177     input_token.length = rawlen;
178
179 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
180     if (checkprefix("Negotiate", header)) {
181         ASN1_OBJECT *   object            = NULL;
182         int             rc                = 1;
183         unsigned char * spnegoToken       = NULL;
184         size_t          spnegoTokenLength = 0;
185         unsigned char * mechToken         = NULL;
186         size_t          mechTokenLength   = 0;
187
188         spnegoToken = malloc(input_token.length);
189         if (input_token.value == NULL)
190           return ENOMEM;
191         spnegoTokenLength = input_token.length;
192
193         object = OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
194         if (!parseSpnegoTargetToken(spnegoToken,
195                                     spnegoTokenLength,
196                                     NULL,
197                                     NULL,
198                                     &mechToken,
199                                     &mechTokenLength,
200                                     NULL,
201                                     NULL)) {
202           free(spnegoToken);
203           spnegoToken = NULL;
204           infof(conn->data, "Parse SPNEGO Target Token failed\n");
205         }
206         else {
207           free(input_token.value);
208           input_token.value = NULL;
209           input_token.value = malloc(mechTokenLength);
210           memcpy(input_token.value, mechToken,mechTokenLength);
211           input_token.length = mechTokenLength;
212           free(mechToken);
213           mechToken = NULL;
214           infof(conn->data, "Parse SPNEGO Target Token succeded\n");
215         }
216     }
217 #endif
218   }
219
220   major_status = gss_init_sec_context(&minor_status,
221                                       GSS_C_NO_CREDENTIAL,
222                                       &neg_ctx->context,
223                                       neg_ctx->server_name,
224                                       GSS_C_NO_OID,
225                                       GSS_C_DELEG_FLAG,
226                                       0,
227                                       GSS_C_NO_CHANNEL_BINDINGS,
228                                       &input_token,
229                                       NULL,
230                                       &output_token,
231                                       NULL,
232                                       NULL);
233   if (input_token.length > 0)
234     gss_release_buffer(&minor_status2, &input_token);
235   neg_ctx->status = major_status;
236   if (GSS_ERROR(major_status)) {
237     /* Curl_cleanup_negotiate(conn->data) ??? */
238     log_gss_error(conn, minor_status,
239                   (char *)"gss_init_sec_context() failed: ");
240     return -1;
241   }
242
243   if (output_token.length == 0) {
244     return -1;
245   }
246
247   neg_ctx->output_token = output_token;
248   /* conn->bits.close = FALSE; */
249
250   return 0;
251 }
252
253
254 CURLcode Curl_output_negotiate(struct connectdata *conn)
255 {
256   struct negotiatedata *neg_ctx = &conn->data->state.negotiate;
257   OM_uint32 minor_status;
258   char *encoded = NULL;
259   int len;
260
261 #ifdef HAVE_SPNEGO /* Handle SPNEGO */
262   if (checkprefix("Negotiate",neg_ctx->protocol)) {
263     ASN1_OBJECT *   object            = NULL;
264     int             rc                = 1;
265     unsigned char * spnegoToken       = NULL;
266     size_t          spnegoTokenLength = 0;
267     unsigned char * responseToken       = NULL;
268     size_t          responseTokenLength = 0;
269
270     responseToken = malloc(neg_ctx->output_token.length);
271     if ( responseToken == NULL)
272       return CURLE_OUT_OF_MEMORY;
273     memcpy(responseToken, neg_ctx->output_token.value,
274            neg_ctx->output_token.length);
275     responseTokenLength = neg_ctx->output_token.length;
276
277     object=OBJ_txt2obj ("1.2.840.113554.1.2.2", 1);
278     if (!makeSpnegoInitialToken (object,
279                                  responseToken,
280                                  responseTokenLength,
281                                  &spnegoToken,
282                                  &spnegoTokenLength)) {
283       free(responseToken);
284       responseToken = NULL;
285       infof(conn->data, "Make SPNEGO Initial Token failed\n");
286     }
287     else {
288       free(neg_ctx->output_token.value);
289       responseToken = NULL;
290       neg_ctx->output_token.value = malloc(spnegoTokenLength);
291       memcpy(neg_ctx->output_token.value, spnegoToken,spnegoTokenLength);
292       neg_ctx->output_token.length = spnegoTokenLength;
293       free(spnegoToken);
294       spnegoToken = NULL;
295       infof(conn->data, "Make SPNEGO Initial Token succeded\n");
296     }
297   }
298 #endif
299   len = Curl_base64_encode(neg_ctx->output_token.value,
300                            neg_ctx->output_token.length,
301                            &encoded);
302
303   if (len < 0)
304     return CURLE_OUT_OF_MEMORY;
305
306   conn->allocptr.userpwd =
307     aprintf("Authorization: %s %s\r\n", neg_ctx->protocol, encoded);
308   free(encoded);
309   gss_release_buffer(&minor_status, &neg_ctx->output_token);
310   return (conn->allocptr.userpwd == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
311 }
312
313 void Curl_cleanup_negotiate(struct SessionHandle *data)
314 {
315   OM_uint32 minor_status;
316   struct negotiatedata *neg_ctx = &data->state.negotiate;
317
318   if (neg_ctx->context != GSS_C_NO_CONTEXT)
319     gss_delete_sec_context(&minor_status, &neg_ctx->context, GSS_C_NO_BUFFER);
320
321   if (neg_ctx->output_token.length != 0)
322     gss_release_buffer(&minor_status, &neg_ctx->output_token);
323
324   if (neg_ctx->server_name != GSS_C_NO_NAME)
325     gss_release_name(&minor_status, &neg_ctx->server_name);
326
327   memset(neg_ctx, 0, sizeof(*neg_ctx));
328 }
329
330
331 #endif
332 #endif