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"
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>
49 #include "vtls/nssg.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 if(checkprefix("NTLM", header)) {
81 header += strlen("NTLM");
83 while(*header && ISSPACE(*header))
87 result = Curl_ntlm_decode_type2_message(conn->data, header, ntlm);
88 if(CURLE_OK != result)
91 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
94 if(ntlm->state == NTLMSTATE_TYPE3) {
95 infof(conn->data, "NTLM handshake rejected\n");
96 Curl_http_ntlm_cleanup(conn);
97 ntlm->state = NTLMSTATE_NONE;
98 return CURLE_REMOTE_ACCESS_DENIED;
100 else if(ntlm->state >= NTLMSTATE_TYPE1) {
101 infof(conn->data, "NTLM handshake failure (internal error)\n");
102 return CURLE_REMOTE_ACCESS_DENIED;
105 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
113 * This is for creating ntlm header output
115 CURLcode Curl_output_ntlm(struct connectdata *conn,
122 /* point to the address of the pointer that holds the string to send to the
123 server, which is for a plain host or for a HTTP proxy */
126 /* point to the name and password for this */
130 /* point to the correct struct with this */
131 struct ntlmdata *ntlm;
135 DEBUGASSERT(conn->data);
138 if(CURLE_OK != Curl_nss_force_init(conn->data))
139 return CURLE_OUT_OF_MEMORY;
143 allocuserpwd = &conn->allocptr.proxyuserpwd;
144 userp = conn->proxyuser;
145 passwdp = conn->proxypasswd;
146 ntlm = &conn->proxyntlm;
147 authp = &conn->data->state.authproxy;
150 allocuserpwd = &conn->allocptr.userpwd;
152 passwdp = conn->passwd;
154 authp = &conn->data->state.authhost;
158 /* not set means empty */
165 #ifdef USE_WINDOWS_SSPI
166 if(s_hSecDll == NULL) {
167 /* not thread safe and leaks - use curl_global_init() to avoid */
168 CURLcode err = Curl_sspi_global_init();
169 if(s_hSecDll == NULL)
174 switch(ntlm->state) {
175 case NTLMSTATE_TYPE1:
176 default: /* for the weird cases we (re)start here */
177 /* Create a type-1 message */
178 error = Curl_ntlm_create_type1_message(userp, passwdp, ntlm, &base64,
184 Curl_safefree(*allocuserpwd);
185 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
186 proxy ? "Proxy-" : "",
190 return CURLE_OUT_OF_MEMORY;
191 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
195 case NTLMSTATE_TYPE2:
196 /* We already received the type-2 message, create a type-3 message */
197 error = Curl_ntlm_create_type3_message(conn->data, userp, passwdp,
198 ntlm, &base64, &len);
203 Curl_safefree(*allocuserpwd);
204 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
205 proxy ? "Proxy-" : "",
209 return CURLE_OUT_OF_MEMORY;
210 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
212 ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
217 case NTLMSTATE_TYPE3:
218 /* connection is already authenticated,
219 * don't send a header in future requests */
220 Curl_safefree(*allocuserpwd);
228 void Curl_http_ntlm_cleanup(struct connectdata *conn)
230 #ifdef USE_WINDOWS_SSPI
231 Curl_ntlm_sspi_cleanup(&conn->ntlm);
232 Curl_ntlm_sspi_cleanup(&conn->proxyntlm);
233 #elif defined(NTLM_WB_ENABLED)
234 Curl_ntlm_wb_cleanup(conn);
239 #ifndef USE_WINDOWS_SSPI
240 Curl_safefree(conn->ntlm.target_info);
241 conn->ntlm.target_info_len = 0;
243 Curl_safefree(conn->proxyntlm.target_info);
244 conn->proxyntlm.target_info_len = 0;
248 #endif /* USE_NTLM */