Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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