1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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 * https://davenport.sourceforge.io/ntlm.html
31 * https://www.innovation.ch/java/ntlm.html
39 #include "http_ntlm.h"
40 #include "curl_ntlm_core.h"
41 #include "curl_ntlm_wb.h"
42 #include "vauth/vauth.h"
45 /* SSL backend-specific #if branches in this file must be kept in the order
46 documented in curl_ntlm_core. */
47 #if defined(NTLM_NEEDS_NSS_INIT)
48 #include "vtls/nssg.h"
49 #elif defined(USE_WINDOWS_SSPI)
50 #include "curl_sspi.h"
53 /* The last 3 #include files should be in this order */
54 #include "curl_printf.h"
55 #include "curl_memory.h"
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_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
86 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
89 if(ntlm->state == NTLMSTATE_LAST) {
90 infof(conn->data, "NTLM auth restarted\n");
91 Curl_http_ntlm_cleanup(conn);
93 else if(ntlm->state == NTLMSTATE_TYPE3) {
94 infof(conn->data, "NTLM handshake rejected\n");
95 Curl_http_ntlm_cleanup(conn);
96 ntlm->state = NTLMSTATE_NONE;
97 return CURLE_REMOTE_ACCESS_DENIED;
99 else if(ntlm->state >= NTLMSTATE_TYPE1) {
100 infof(conn->data, "NTLM handshake failure (internal error)\n");
101 return CURLE_REMOTE_ACCESS_DENIED;
104 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
112 * This is for creating ntlm header output
114 CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
120 /* point to the address of the pointer that holds the string to send to the
121 server, which is for a plain host or for a HTTP proxy */
124 /* point to the name and password for this */
128 /* point to the correct struct with this */
129 struct ntlmdata *ntlm;
133 DEBUGASSERT(conn->data);
135 #if defined(NTLM_NEEDS_NSS_INIT)
136 if(CURLE_OK != Curl_nss_force_init(conn->data))
137 return CURLE_OUT_OF_MEMORY;
141 allocuserpwd = &conn->allocptr.proxyuserpwd;
142 userp = conn->http_proxy.user;
143 passwdp = conn->http_proxy.passwd;
144 ntlm = &conn->proxyntlm;
145 authp = &conn->data->state.authproxy;
148 allocuserpwd = &conn->allocptr.userpwd;
150 passwdp = conn->passwd;
152 authp = &conn->data->state.authhost;
156 /* not set means empty */
163 #ifdef USE_WINDOWS_SSPI
164 if(s_hSecDll == NULL) {
165 /* not thread safe and leaks - use curl_global_init() to avoid */
166 CURLcode err = Curl_sspi_global_init();
167 if(s_hSecDll == NULL)
172 switch(ntlm->state) {
173 case NTLMSTATE_TYPE1:
174 default: /* for the weird cases we (re)start here */
175 /* Create a type-1 message */
176 result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
177 ntlm, &base64, &len);
183 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
184 proxy ? "Proxy-" : "",
188 return CURLE_OUT_OF_MEMORY;
190 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
194 case NTLMSTATE_TYPE2:
195 /* We already received the type-2 message, create a type-3 message */
196 result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
197 ntlm, &base64, &len);
203 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
204 proxy ? "Proxy-" : "",
208 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 ntlm->state = NTLMSTATE_LAST;
223 Curl_safefree(*allocuserpwd);
231 void Curl_http_ntlm_cleanup(struct connectdata *conn)
233 Curl_auth_ntlm_cleanup(&conn->ntlm);
234 Curl_auth_ntlm_cleanup(&conn->proxyntlm);
236 #if defined(NTLM_WB_ENABLED)
237 Curl_ntlm_wb_cleanup(conn);
241 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */