1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 https://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_printf.h"
47 #include "vtls/nssg.h"
48 #elif defined(USE_WINDOWS_SSPI)
49 #include "curl_sspi.h"
52 /* The last #include files should be: */
53 #include "curl_memory.h"
57 # define DEBUG_OUT(x) x
59 # define DEBUG_OUT(x) Curl_nop_stmt
62 CURLcode Curl_input_ntlm(struct connectdata *conn,
63 bool proxy, /* if proxy or not */
64 const char *header) /* rest of the www-authenticate:
67 /* point to the correct struct with this */
68 struct ntlmdata *ntlm;
69 CURLcode result = CURLE_OK;
71 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
73 if(checkprefix("NTLM", header)) {
74 header += strlen("NTLM");
76 while(*header && ISSPACE(*header))
80 result = Curl_sasl_decode_ntlm_type2_message(conn->data, header, ntlm);
84 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
87 if(ntlm->state == NTLMSTATE_LAST) {
88 infof(conn->data, "NTLM auth restarted\n");
89 Curl_http_ntlm_cleanup(conn);
91 else if(ntlm->state == NTLMSTATE_TYPE3) {
92 infof(conn->data, "NTLM handshake rejected\n");
93 Curl_http_ntlm_cleanup(conn);
94 ntlm->state = NTLMSTATE_NONE;
95 return CURLE_REMOTE_ACCESS_DENIED;
97 else if(ntlm->state >= NTLMSTATE_TYPE1) {
98 infof(conn->data, "NTLM handshake failure (internal error)\n");
99 return CURLE_REMOTE_ACCESS_DENIED;
102 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
110 * This is for creating ntlm header output
112 CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
118 /* point to the address of the pointer that holds the string to send to the
119 server, which is for a plain host or for a HTTP proxy */
122 /* point to the name and password for this */
126 /* point to the correct struct with this */
127 struct ntlmdata *ntlm;
131 DEBUGASSERT(conn->data);
134 if(CURLE_OK != Curl_nss_force_init(conn->data))
135 return CURLE_OUT_OF_MEMORY;
139 allocuserpwd = &conn->allocptr.proxyuserpwd;
140 userp = conn->proxyuser;
141 passwdp = conn->proxypasswd;
142 ntlm = &conn->proxyntlm;
143 authp = &conn->data->state.authproxy;
146 allocuserpwd = &conn->allocptr.userpwd;
148 passwdp = conn->passwd;
150 authp = &conn->data->state.authhost;
154 /* not set means empty */
161 #ifdef USE_WINDOWS_SSPI
162 if(s_hSecDll == NULL) {
163 /* not thread safe and leaks - use curl_global_init() to avoid */
164 CURLcode err = Curl_sspi_global_init();
165 if(s_hSecDll == NULL)
170 switch(ntlm->state) {
171 case NTLMSTATE_TYPE1:
172 default: /* for the weird cases we (re)start here */
173 /* Create a type-1 message */
174 result = Curl_sasl_create_ntlm_type1_message(userp, passwdp, ntlm, &base64,
181 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
182 proxy ? "Proxy-" : "",
186 return CURLE_OUT_OF_MEMORY;
188 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
192 case NTLMSTATE_TYPE2:
193 /* We already received the type-2 message, create a type-3 message */
194 result = Curl_sasl_create_ntlm_type3_message(conn->data, userp, passwdp,
195 ntlm, &base64, &len);
201 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
202 proxy ? "Proxy-" : "",
206 return CURLE_OUT_OF_MEMORY;
208 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
210 ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
215 case NTLMSTATE_TYPE3:
216 /* connection is already authenticated,
217 * don't send a header in future requests */
218 ntlm->state = NTLMSTATE_LAST;
221 Curl_safefree(*allocuserpwd);
229 void Curl_http_ntlm_cleanup(struct connectdata *conn)
231 Curl_sasl_ntlm_cleanup(&conn->ntlm);
232 Curl_sasl_ntlm_cleanup(&conn->proxyntlm);
234 #if defined(NTLM_WB_ENABLED)
235 Curl_ntlm_wb_cleanup(conn);
239 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */