1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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"
25 #if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
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"
42 #include "curl_sasl.h"
44 #include "curl_memory.h"
46 #define _MPRINTF_REPLACE /* use our functions only */
47 #include <curl/mprintf.h>
50 #include "vtls/nssg.h"
51 #elif defined(USE_WINDOWS_SSPI)
52 #include "curl_sspi.h"
55 /* The last #include file should be: */
59 # define DEBUG_OUT(x) x
61 # define DEBUG_OUT(x) Curl_nop_stmt
64 CURLcode Curl_input_ntlm(struct connectdata *conn,
65 bool proxy, /* if proxy or not */
66 const char *header) /* rest of the www-authenticate:
69 /* point to the correct struct with this */
70 struct ntlmdata *ntlm;
71 CURLcode result = CURLE_OK;
73 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
75 if(checkprefix("NTLM", header)) {
76 header += strlen("NTLM");
78 while(*header && ISSPACE(*header))
82 result = Curl_sasl_decode_ntlm_type2_message(conn->data, header, ntlm);
86 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
89 if(ntlm->state == NTLMSTATE_TYPE3) {
90 infof(conn->data, "NTLM handshake rejected\n");
91 Curl_http_ntlm_cleanup(conn);
92 ntlm->state = NTLMSTATE_NONE;
93 return CURLE_REMOTE_ACCESS_DENIED;
95 else if(ntlm->state >= NTLMSTATE_TYPE1) {
96 infof(conn->data, "NTLM handshake failure (internal error)\n");
97 return CURLE_REMOTE_ACCESS_DENIED;
100 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
108 * This is for creating ntlm header output
110 CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
116 /* point to the address of the pointer that holds the string to send to the
117 server, which is for a plain host or for a HTTP proxy */
120 /* point to the name and password for this */
124 /* point to the correct struct with this */
125 struct ntlmdata *ntlm;
129 DEBUGASSERT(conn->data);
132 if(CURLE_OK != Curl_nss_force_init(conn->data))
133 return CURLE_OUT_OF_MEMORY;
137 allocuserpwd = &conn->allocptr.proxyuserpwd;
138 userp = conn->proxyuser;
139 passwdp = conn->proxypasswd;
140 ntlm = &conn->proxyntlm;
141 authp = &conn->data->state.authproxy;
144 allocuserpwd = &conn->allocptr.userpwd;
146 passwdp = conn->passwd;
148 authp = &conn->data->state.authhost;
152 /* not set means empty */
159 #ifdef USE_WINDOWS_SSPI
160 if(s_hSecDll == NULL) {
161 /* not thread safe and leaks - use curl_global_init() to avoid */
162 CURLcode err = Curl_sspi_global_init();
163 if(s_hSecDll == NULL)
168 switch(ntlm->state) {
169 case NTLMSTATE_TYPE1:
170 default: /* for the weird cases we (re)start here */
171 /* Create a type-1 message */
172 result = Curl_sasl_create_ntlm_type1_message(userp, passwdp, ntlm, &base64,
178 Curl_safefree(*allocuserpwd);
179 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
180 proxy ? "Proxy-" : "",
184 return CURLE_OUT_OF_MEMORY;
186 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
190 case NTLMSTATE_TYPE2:
191 /* We already received the type-2 message, create a type-3 message */
192 result = Curl_sasl_create_ntlm_type3_message(conn->data, userp, passwdp,
193 ntlm, &base64, &len);
198 Curl_safefree(*allocuserpwd);
199 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
200 proxy ? "Proxy-" : "",
204 return CURLE_OUT_OF_MEMORY;
206 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
208 ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
213 case NTLMSTATE_TYPE3:
214 /* connection is already authenticated,
215 * don't send a header in future requests */
216 Curl_safefree(*allocuserpwd);
224 void Curl_http_ntlm_cleanup(struct connectdata *conn)
226 Curl_sasl_ntlm_cleanup(&conn->ntlm);
227 Curl_sasl_ntlm_cleanup(&conn->proxyntlm);
229 #if defined(NTLM_WB_ENABLED)
230 Curl_ntlm_wb_cleanup(conn);
234 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */