Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 314 lines (260 sloc) 8.89 kb
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
7f472618 »
2012-01-01 Curl_input_negotiate: use the correct buffer for input
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
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 ***************************************************************************/
f1586cb4 »
2011-07-26 stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in s…
22
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
23 #include "setup.h"
24
25 #ifdef USE_WINDOWS_SSPI
26
27 #ifndef CURL_DISABLE_HTTP
28
29 #include "urldata.h"
30 #include "sendf.h"
31 #include "rawstr.h"
8e82ef9c »
2012-01-13 http_negotiate_sspi.c: fix compiler warning
32 #include "warnless.h"
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
33 #include "curl_base64.h"
34 #include "http_negotiate.h"
35 #include "curl_memory.h"
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
36 #include "curl_multibyte.h"
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
37
38 #define _MPRINTF_REPLACE /* use our functions only */
39 #include <curl/mprintf.h>
40
41 /* The last #include file should be: */
42 #include "memdebug.h"
43
44 static int
2e2e5f24 »
2011-06-29 get_gss_name: proxy auth without proxy set equals error
45 get_gss_name(struct connectdata *conn, bool proxy,
46 struct negotiatedata *neg_ctx)
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
47 {
48 const char* service;
49 size_t length;
50
2e2e5f24 »
2011-06-29 get_gss_name: proxy auth without proxy set equals error
51 if(proxy && !conn->proxy.name)
52 /* proxy auth requested but no given proxy name, error out! */
53 return -1;
54
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
55 /* GSSAPI implementation by Globus (known as GSI) requires the name to be
56 of form "<service>/<fqdn>" instead of <service>@<fqdn> (ie. slash instead
57 of at-sign). Also GSI servers are often identified as 'host' not 'khttp'.
58 Change following lines if you want to use GSI */
59
60 /* IIS uses the <service>@<fqdn> form but uses 'http' as the service name,
61 and SSPI then generates an NTLM token. When using <service>/<fqdn> a
62 Kerberos token is generated. */
63
64 if(neg_ctx->gss)
65 service = "KHTTP";
66 else
67 service = "HTTP";
68
69 length = strlen(service) + 1 + strlen(proxy ? conn->proxy.name :
70 conn->host.name) + 1;
71 if(length + 1 > sizeof(neg_ctx->server_name))
72 return EMSGSIZE;
73
2e2e5f24 »
2011-06-29 get_gss_name: proxy auth without proxy set equals error
74 snprintf(neg_ctx->server_name, sizeof(neg_ctx->server_name), "%s/%s",
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
75 service, proxy ? conn->proxy.name : conn->host.name);
76
77 return 0;
78 }
79
80 /* returning zero (0) means success, everything else is treated as "failure"
81 with no care exactly what the failure was */
82 int Curl_input_negotiate(struct connectdata *conn, bool proxy,
83 const char *header)
84 {
85 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
86 &conn->data->state.negotiate;
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
87 BYTE *input_token = 0;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
88 SecBufferDesc out_buff_desc;
89 SecBuffer out_sec_buff;
90 SecBufferDesc in_buff_desc;
91 SecBuffer in_sec_buff;
92 ULONG context_attributes;
93 TimeStamp lifetime;
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
94 LPTSTR sname;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
95 int ret;
96 size_t len = 0, input_token_len = 0;
97 bool gss = FALSE;
98 const char* protocol;
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
99 CURLcode error;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
100
101 while(*header && ISSPACE(*header))
102 header++;
103
104 if(checkprefix("GSS-Negotiate", header)) {
105 protocol = "GSS-Negotiate";
106 gss = TRUE;
107 }
108 else if(checkprefix("Negotiate", header)) {
109 protocol = "Negotiate";
110 gss = FALSE;
111 }
112 else
113 return -1;
114
115 if(neg_ctx->context) {
116 if(neg_ctx->gss != gss) {
117 return -1;
118 }
119 }
120 else {
121 neg_ctx->protocol = protocol;
122 neg_ctx->gss = gss;
123 }
124
125 if(neg_ctx->context && neg_ctx->status == SEC_E_OK) {
1702a2c0 »
2011-04-19 Fix a couple of spelling errors in lib/
126 /* We finished successfully our part of authentication, but server
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
127 * rejected it (since we're again here). Exit with an error since we
128 * can't invent anything better */
129 Curl_cleanup_negotiate(conn->data);
130 return -1;
131 }
132
08b05efd »
2011-08-14 http_negotiate_sspi.c: fix compiler warning
133 if(0 == strlen(neg_ctx->server_name)) {
134 ret = get_gss_name(conn, proxy, neg_ctx);
135 if(ret)
136 return ret;
137 }
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
138
48a40f04 »
2011-05-16 negotiate sspi: fix sequential requests
139 if(!neg_ctx->output_token) {
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
140 PSecPkgInfo SecurityPackage;
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
141 ret = s_pSecFn->QuerySecurityPackageInfo(TEXT("Negotiate"),
8fa7b8cb »
2011-01-14 Added casts to silent gcc warnings.
142 &SecurityPackage);
b903186f »
2011-04-20 source cleanup: unify look, style and indent levels
143 if(ret != SEC_E_OK)
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
144 return -1;
145
146 /* Allocate input and output buffers according to the max token size
147 as indicated by the security package */
148 neg_ctx->max_token_length = SecurityPackage->cbMaxToken;
65a9fa59 »
2011-06-02 Remove unnecessary typecast
149 neg_ctx->output_token = malloc(neg_ctx->max_token_length);
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
150 s_pSecFn->FreeContextBuffer(SecurityPackage);
151 }
152
153 /* Obtain the input token, if any */
154 header += strlen(neg_ctx->protocol);
155 while(*header && ISSPACE(*header))
156 header++;
157
158 len = strlen(header);
48a40f04 »
2011-05-16 negotiate sspi: fix sequential requests
159 if(!len) {
160 /* first call in a new negotation, we have to acquire credentials,
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
161 and allocate memory for the context */
162
65a9fa59 »
2011-06-02 Remove unnecessary typecast
163 neg_ctx->credentials = malloc(sizeof(CredHandle));
164 neg_ctx->context = malloc(sizeof(CtxtHandle));
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
165
889d1e97 »
2011-04-22 whitespace cleanup: no space first in conditionals
166 if(!neg_ctx->credentials || !neg_ctx->context)
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
167 return -1;
168
169 neg_ctx->status =
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
170 s_pSecFn->AcquireCredentialsHandle(NULL, TEXT("Negotiate"),
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
171 SECPKG_CRED_OUTBOUND, NULL, NULL,
172 NULL, NULL, neg_ctx->credentials,
173 &lifetime);
889d1e97 »
2011-04-22 whitespace cleanup: no space first in conditionals
174 if(neg_ctx->status != SEC_E_OK)
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
175 return -1;
176 }
48a40f04 »
2011-05-16 negotiate sspi: fix sequential requests
177 else {
178 input_token = malloc(neg_ctx->max_token_length);
179 if(!input_token)
180 return -1;
181
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
182 error = Curl_base64_decode(header,
183 (unsigned char **)&input_token,
184 &input_token_len);
185 if(error || input_token_len == 0)
48a40f04 »
2011-05-16 negotiate sspi: fix sequential requests
186 return -1;
187 }
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
188
189 /* prepare the output buffers, and input buffers if present */
190 out_buff_desc.ulVersion = 0;
191 out_buff_desc.cBuffers = 1;
192 out_buff_desc.pBuffers = &out_sec_buff;
193
8e82ef9c »
2012-01-13 http_negotiate_sspi.c: fix compiler warning
194 out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->max_token_length);
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
195 out_sec_buff.BufferType = SECBUFFER_TOKEN;
196 out_sec_buff.pvBuffer = neg_ctx->output_token;
197
198
b903186f »
2011-04-20 source cleanup: unify look, style and indent levels
199 if(input_token) {
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
200 in_buff_desc.ulVersion = 0;
201 in_buff_desc.cBuffers = 1;
7f472618 »
2012-01-01 Curl_input_negotiate: use the correct buffer for input
202 in_buff_desc.pBuffers = &in_sec_buff;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
203
8e82ef9c »
2012-01-13 http_negotiate_sspi.c: fix compiler warning
204 in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
205 in_sec_buff.BufferType = SECBUFFER_TOKEN;
206 in_sec_buff.pvBuffer = input_token;
207 }
208
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
209 #ifdef UNICODE
210 sname = Curl_convert_UTF8_to_wchar(neg_ctx->server_name);
a96fa00f »
2012-06-15 SSPI related code: Unicode support for WinCE - commit 46480bb follow-up
211 if(!sname)
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
212 return CURLE_OUT_OF_MEMORY;
213 #else
214 sname = neg_ctx->server_name;
215 #endif
216
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
217 neg_ctx->status = s_pSecFn->InitializeSecurityContext(
218 neg_ctx->credentials,
219 input_token ? neg_ctx->context : 0,
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
220 sname,
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
221 ISC_REQ_CONFIDENTIALITY,
222 0,
223 SECURITY_NATIVE_DREP,
224 input_token ? &in_buff_desc : 0,
225 0,
226 neg_ctx->context,
227 &out_buff_desc,
228 &context_attributes,
229 &lifetime);
230
46480bb9 »
2012-06-15 SSPI related code: Unicode support for WinCE
231 #ifdef UNICODE
232 free(sname);
233 #endif
234
889d1e97 »
2011-04-22 whitespace cleanup: no space first in conditionals
235 if(GSS_ERROR(neg_ctx->status))
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
236 return -1;
237
889d1e97 »
2011-04-22 whitespace cleanup: no space first in conditionals
238 if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
239 neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
240 neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
241 &out_buff_desc);
889d1e97 »
2011-04-22 whitespace cleanup: no space first in conditionals
242 if(GSS_ERROR(neg_ctx->status))
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
243 return -1;
244 }
245
246 neg_ctx->output_token_length = out_sec_buff.cbBuffer;
247
248 return 0;
249 }
250
251
252 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
253 {
254 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
255 &conn->data->state.negotiate;
256 char *encoded = NULL;
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
257 size_t len = 0;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
258 char *userp;
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
259 CURLcode error;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
260
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
261 error = Curl_base64_encode(conn->data,
262 (const char*)neg_ctx->output_token,
263 neg_ctx->output_token_length,
264 &encoded, &len);
265 if(error)
266 return error;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
267
268 if(len == 0)
fd00b382 »
2011-08-24 base64: fix Curl_base64_encode and Curl_base64_decode interfaces
269 return CURLE_REMOTE_ACCESS_DENIED;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
270
271 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
272 neg_ctx->protocol, encoded);
273
274 if(proxy)
275 conn->allocptr.proxyuserpwd = userp;
276 else
277 conn->allocptr.userpwd = userp;
278 free(encoded);
279 Curl_cleanup_negotiate (conn->data);
280 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
281 }
282
283 static void cleanup(struct negotiatedata *neg_ctx)
284 {
285 if(neg_ctx->context) {
286 s_pSecFn->DeleteSecurityContext(neg_ctx->context);
287 free(neg_ctx->context);
288 neg_ctx->context = 0;
289 }
290
291 if(neg_ctx->credentials) {
292 s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
293 free(neg_ctx->credentials);
294 neg_ctx->credentials = 0;
295 }
296
297 if(neg_ctx->output_token) {
298 free(neg_ctx->output_token);
299 neg_ctx->output_token = 0;
300 }
48a40f04 »
2011-05-16 negotiate sspi: fix sequential requests
301
302 neg_ctx->max_token_length = 0;
1d28efb9 »
2011-01-05 HTTP: HTTP Negotiate authentication using SSPI
303 }
304
305 void Curl_cleanup_negotiate(struct SessionHandle *data)
306 {
307 cleanup(&data->state.negotiate);
308 cleanup(&data->state.proxyneg);
309 }
310
311
312 #endif
313 #endif
Something went wrong with that request. Please try again.