1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
30 * http://davenport.sourceforge.net/ntlm.html
31 * http://www.innovation.ch/java/ntlm.html
39 #include "curl_ntlm.h"
40 #include "curl_ntlm_msgs.h"
41 #include "curl_ntlm_wb.h"
43 #include "curl_memory.h"
45 #define _MPRINTF_REPLACE /* use our functions only */
46 #include <curl/mprintf.h>
50 #elif defined(USE_WINDOWS_SSPI)
51 #include "curl_sspi.h"
54 /* The last #include file should be: */
58 # define DEBUG_OUT(x) x
60 # define DEBUG_OUT(x) Curl_nop_stmt
63 CURLcode Curl_input_ntlm(struct connectdata *conn,
64 bool proxy, /* if proxy or not */
65 const char *header) /* rest of the www-authenticate:
68 /* point to the correct struct with this */
69 struct ntlmdata *ntlm;
70 CURLcode result = CURLE_OK;
73 result = Curl_nss_force_init(conn->data);
78 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
80 /* skip initial whitespaces */
81 while(*header && ISSPACE(*header))
84 if(checkprefix("NTLM", header)) {
85 header += strlen("NTLM");
87 while(*header && ISSPACE(*header))
91 result = Curl_ntlm_decode_type2_message(conn->data, header, ntlm);
92 if(CURLE_OK != result)
95 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
98 if(ntlm->state == NTLMSTATE_TYPE3) {
99 infof(conn->data, "NTLM handshake rejected\n");
100 Curl_http_ntlm_cleanup(conn);
101 ntlm->state = NTLMSTATE_NONE;
102 return CURLE_REMOTE_ACCESS_DENIED;
104 else if(ntlm->state >= NTLMSTATE_TYPE1) {
105 infof(conn->data, "NTLM handshake failure (internal error)\n");
106 return CURLE_REMOTE_ACCESS_DENIED;
109 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
117 * This is for creating ntlm header output
119 CURLcode Curl_output_ntlm(struct connectdata *conn,
126 /* point to the address of the pointer that holds the string to send to the
127 server, which is for a plain host or for a HTTP proxy */
130 /* point to the name and password for this */
134 /* point to the correct struct with this */
135 struct ntlmdata *ntlm;
139 DEBUGASSERT(conn->data);
142 if(CURLE_OK != Curl_nss_force_init(conn->data))
143 return CURLE_OUT_OF_MEMORY;
147 allocuserpwd = &conn->allocptr.proxyuserpwd;
148 userp = conn->proxyuser;
149 passwdp = conn->proxypasswd;
150 ntlm = &conn->proxyntlm;
151 authp = &conn->data->state.authproxy;
154 allocuserpwd = &conn->allocptr.userpwd;
156 passwdp = conn->passwd;
158 authp = &conn->data->state.authhost;
162 /* not set means empty */
169 #ifdef USE_WINDOWS_SSPI
170 if(s_hSecDll == NULL) {
171 /* not thread safe and leaks - use curl_global_init() to avoid */
172 CURLcode err = Curl_sspi_global_init();
173 if(s_hSecDll == NULL)
178 switch(ntlm->state) {
179 case NTLMSTATE_TYPE1:
180 default: /* for the weird cases we (re)start here */
181 /* Create a type-1 message */
182 error = Curl_ntlm_create_type1_message(userp, passwdp, ntlm, &base64,
188 Curl_safefree(*allocuserpwd);
189 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
190 proxy ? "Proxy-" : "",
194 return CURLE_OUT_OF_MEMORY;
195 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
199 case NTLMSTATE_TYPE2:
200 /* We already received the type-2 message, create a type-3 message */
201 error = Curl_ntlm_create_type3_message(conn->data, userp, passwdp,
202 ntlm, &base64, &len);
207 Curl_safefree(*allocuserpwd);
208 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
209 proxy ? "Proxy-" : "",
213 return CURLE_OUT_OF_MEMORY;
214 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
216 ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
221 case NTLMSTATE_TYPE3:
222 /* connection is already authenticated,
223 * don't send a header in future requests */
224 Curl_safefree(*allocuserpwd);
232 void Curl_http_ntlm_cleanup(struct connectdata *conn)
234 #ifdef USE_WINDOWS_SSPI
235 Curl_ntlm_sspi_cleanup(&conn->ntlm);
236 Curl_ntlm_sspi_cleanup(&conn->proxyntlm);
237 #elif defined(NTLM_WB_ENABLED)
238 Curl_ntlm_wb_cleanup(conn);
244 #endif /* USE_NTLM */