Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 309 lines (255 sloc) 8.882 kb
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
7f47261 @bagder Curl_input_negotiate: use the correct buffer for input
authored
8 * Copyright (C) 1998 - 2012, Daniel Stenberg, <daniel@haxx.se>, et al.
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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 ***************************************************************************/
f1586cb @yangtse stdio.h, stdlib.h, string.h, stdarg.h and ctype.h inclusion done in setu...
yangtse authored
22
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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"
8e82ef9 @yangtse http_negotiate_sspi.c: fix compiler warning
yangtse authored
32 #include "warnless.h"
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
33 #include "curl_base64.h"
34 #include "http_negotiate.h"
35 #include "curl_memory.h"
46480bb @masali-hp SSPI related code: Unicode support for WinCE
masali-hp authored
36 #include "curl_multibyte.h"
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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
2e2e5f2 @bagder get_gss_name: proxy auth without proxy set equals error
authored
45 get_gss_name(struct connectdata *conn, bool proxy,
46 struct negotiatedata *neg_ctx)
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
47 {
48 const char* service;
49 size_t length;
50
2e2e5f2 @bagder get_gss_name: proxy auth without proxy set equals error
authored
51 if(proxy && !conn->proxy.name)
52 /* proxy auth requested but no given proxy name, error out! */
53 return -1;
54
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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
2e2e5f2 @bagder get_gss_name: proxy auth without proxy set equals error
authored
74 snprintf(neg_ctx->server_name, sizeof(neg_ctx->server_name), "%s/%s",
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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;
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
87 BYTE *input_token = 0;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
88 SecBufferDesc out_buff_desc;
89 SecBuffer out_sec_buff;
90 SecBufferDesc in_buff_desc;
91 SecBuffer in_sec_buff;
d56e8bc @yangtse Win32: downplay MS bazillion type synonyms game
yangtse authored
92 unsigned long context_attributes;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
93 TimeStamp lifetime;
d56e8bc @yangtse Win32: downplay MS bazillion type synonyms game
yangtse authored
94 TCHAR *sname;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
95 int ret;
96 size_t len = 0, input_token_len = 0;
97 bool gss = FALSE;
98 const char* protocol;
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
99 CURLcode error;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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) {
1702a2c Fix a couple of spelling errors in lib/
Fabian Keil authored
126 /* We finished successfully our part of authentication, but server
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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
08b05ef @yangtse http_negotiate_sspi.c: fix compiler warning
yangtse authored
133 if(0 == strlen(neg_ctx->server_name)) {
134 ret = get_gss_name(conn, proxy, neg_ctx);
135 if(ret)
136 return ret;
137 }
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
138
48a40f0 negotiate sspi: fix sequential requests
Marcel Roelofs authored
139 if(!neg_ctx->output_token) {
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
140 PSecPkgInfo SecurityPackage;
d56e8bc @yangtse Win32: downplay MS bazillion type synonyms game
yangtse authored
141 ret = s_pSecFn->QuerySecurityPackageInfo((TCHAR *) TEXT("Negotiate"),
8fa7b8c @gknauf Added casts to silent gcc warnings.
gknauf authored
142 &SecurityPackage);
b903186 @bagder source cleanup: unify look, style and indent levels
authored
143 if(ret != SEC_E_OK)
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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;
65a9fa5 @yangtse Remove unnecessary typecast
yangtse authored
149 neg_ctx->output_token = malloc(neg_ctx->max_token_length);
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
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);
48a40f0 negotiate sspi: fix sequential requests
Marcel Roelofs authored
159 if(!len) {
160 /* first call in a new negotation, we have to acquire credentials,
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
161 and allocate memory for the context */
162
65a9fa5 @yangtse Remove unnecessary typecast
yangtse authored
163 neg_ctx->credentials = malloc(sizeof(CredHandle));
164 neg_ctx->context = malloc(sizeof(CtxtHandle));
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
165
889d1e9 @bagder whitespace cleanup: no space first in conditionals
authored
166 if(!neg_ctx->credentials || !neg_ctx->context)
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
167 return -1;
168
169 neg_ctx->status =
ac3e356 @yangtse SSPI related code: Unicode support for WinCE - kill compiler warnings
yangtse authored
170 s_pSecFn->AcquireCredentialsHandle(NULL,
d56e8bc @yangtse Win32: downplay MS bazillion type synonyms game
yangtse authored
171 (TCHAR *) TEXT("Negotiate"),
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
172 SECPKG_CRED_OUTBOUND, NULL, NULL,
173 NULL, NULL, neg_ctx->credentials,
174 &lifetime);
889d1e9 @bagder whitespace cleanup: no space first in conditionals
authored
175 if(neg_ctx->status != SEC_E_OK)
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
176 return -1;
177 }
48a40f0 negotiate sspi: fix sequential requests
Marcel Roelofs authored
178 else {
179 input_token = malloc(neg_ctx->max_token_length);
180 if(!input_token)
181 return -1;
182
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
183 error = Curl_base64_decode(header,
184 (unsigned char **)&input_token,
185 &input_token_len);
186 if(error || input_token_len == 0)
48a40f0 negotiate sspi: fix sequential requests
Marcel Roelofs authored
187 return -1;
188 }
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
189
190 /* prepare the output buffers, and input buffers if present */
191 out_buff_desc.ulVersion = 0;
192 out_buff_desc.cBuffers = 1;
193 out_buff_desc.pBuffers = &out_sec_buff;
194
8e82ef9 @yangtse http_negotiate_sspi.c: fix compiler warning
yangtse authored
195 out_sec_buff.cbBuffer = curlx_uztoul(neg_ctx->max_token_length);
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
196 out_sec_buff.BufferType = SECBUFFER_TOKEN;
197 out_sec_buff.pvBuffer = neg_ctx->output_token;
198
199
b903186 @bagder source cleanup: unify look, style and indent levels
authored
200 if(input_token) {
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
201 in_buff_desc.ulVersion = 0;
202 in_buff_desc.cBuffers = 1;
7f47261 @bagder Curl_input_negotiate: use the correct buffer for input
authored
203 in_buff_desc.pBuffers = &in_sec_buff;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
204
8e82ef9 @yangtse http_negotiate_sspi.c: fix compiler warning
yangtse authored
205 in_sec_buff.cbBuffer = curlx_uztoul(input_token_len);
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
206 in_sec_buff.BufferType = SECBUFFER_TOKEN;
207 in_sec_buff.pvBuffer = input_token;
208 }
209
e77d867 @yangtse unicode NTLM SSPI: cleanup
yangtse authored
210 sname = Curl_convert_UTF8_to_tchar(neg_ctx->server_name);
a96fa00 @masali-hp SSPI related code: Unicode support for WinCE - commit 46480bb9 follow-up
masali-hp authored
211 if(!sname)
46480bb @masali-hp SSPI related code: Unicode support for WinCE
masali-hp authored
212 return CURLE_OUT_OF_MEMORY;
213
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
214 neg_ctx->status = s_pSecFn->InitializeSecurityContext(
215 neg_ctx->credentials,
216 input_token ? neg_ctx->context : 0,
46480bb @masali-hp SSPI related code: Unicode support for WinCE
masali-hp authored
217 sname,
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
218 ISC_REQ_CONFIDENTIALITY,
219 0,
220 SECURITY_NATIVE_DREP,
221 input_token ? &in_buff_desc : 0,
222 0,
223 neg_ctx->context,
224 &out_buff_desc,
225 &context_attributes,
226 &lifetime);
227
e77d867 @yangtse unicode NTLM SSPI: cleanup
yangtse authored
228 Curl_unicodefree(sname);
46480bb @masali-hp SSPI related code: Unicode support for WinCE
masali-hp authored
229
889d1e9 @bagder whitespace cleanup: no space first in conditionals
authored
230 if(GSS_ERROR(neg_ctx->status))
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
231 return -1;
232
889d1e9 @bagder whitespace cleanup: no space first in conditionals
authored
233 if(neg_ctx->status == SEC_I_COMPLETE_NEEDED ||
234 neg_ctx->status == SEC_I_COMPLETE_AND_CONTINUE) {
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
235 neg_ctx->status = s_pSecFn->CompleteAuthToken(neg_ctx->context,
236 &out_buff_desc);
889d1e9 @bagder whitespace cleanup: no space first in conditionals
authored
237 if(GSS_ERROR(neg_ctx->status))
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
238 return -1;
239 }
240
241 neg_ctx->output_token_length = out_sec_buff.cbBuffer;
242
243 return 0;
244 }
245
246
247 CURLcode Curl_output_negotiate(struct connectdata *conn, bool proxy)
248 {
249 struct negotiatedata *neg_ctx = proxy?&conn->data->state.proxyneg:
250 &conn->data->state.negotiate;
251 char *encoded = NULL;
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
252 size_t len = 0;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
253 char *userp;
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
254 CURLcode error;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
255
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
256 error = Curl_base64_encode(conn->data,
257 (const char*)neg_ctx->output_token,
258 neg_ctx->output_token_length,
259 &encoded, &len);
260 if(error)
261 return error;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
262
263 if(len == 0)
fd00b38 @yangtse base64: fix Curl_base64_encode and Curl_base64_decode interfaces
yangtse authored
264 return CURLE_REMOTE_ACCESS_DENIED;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
265
266 userp = aprintf("%sAuthorization: %s %s\r\n", proxy ? "Proxy-" : "",
267 neg_ctx->protocol, encoded);
268
269 if(proxy)
270 conn->allocptr.proxyuserpwd = userp;
271 else
272 conn->allocptr.userpwd = userp;
273 free(encoded);
274 Curl_cleanup_negotiate (conn->data);
275 return (userp == NULL) ? CURLE_OUT_OF_MEMORY : CURLE_OK;
276 }
277
278 static void cleanup(struct negotiatedata *neg_ctx)
279 {
280 if(neg_ctx->context) {
281 s_pSecFn->DeleteSecurityContext(neg_ctx->context);
282 free(neg_ctx->context);
283 neg_ctx->context = 0;
284 }
285
286 if(neg_ctx->credentials) {
287 s_pSecFn->FreeCredentialsHandle(neg_ctx->credentials);
288 free(neg_ctx->credentials);
289 neg_ctx->credentials = 0;
290 }
291
292 if(neg_ctx->output_token) {
293 free(neg_ctx->output_token);
294 neg_ctx->output_token = 0;
295 }
48a40f0 negotiate sspi: fix sequential requests
Marcel Roelofs authored
296
297 neg_ctx->max_token_length = 0;
1d28efb HTTP: HTTP Negotiate authentication using SSPI
Marcel Roelofs authored
298 }
299
300 void Curl_cleanup_negotiate(struct SessionHandle *data)
301 {
302 cleanup(&data->state.negotiate);
303 cleanup(&data->state.proxyneg);
304 }
305
306
307 #endif
308 #endif
Something went wrong with that request. Please try again.