1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
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.se/docs/copyright.html.
13 *
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.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
26
27/*
28 * NTLM details:
29 *
30 * https://davenport.sourceforge.io/ntlm.html
31 * https://www.innovation.ch/java/ntlm.html
32 */
33
34#define DEBUG_ME 0
35
36#include "urldata.h"
37#include "sendf.h"
38#include "strcase.h"
39#include "http_ntlm.h"
40#include "curl_ntlm_core.h"
41#include "curl_ntlm_wb.h"
42#include "curl_base64.h"
43#include "vauth/vauth.h"
44#include "url.h"
45
46/* SSL backend-specific #if branches in this file must be kept in the order
47 documented in curl_ntlm_core. */
48#if defined(USE_WINDOWS_SSPI)
49#include "curl_sspi.h"
50#endif
51
52/* The last 3 #include files should be in this order */
53#include "curl_printf.h"
54#include "curl_memory.h"
55#include "memdebug.h"
56
57#if DEBUG_ME
58# define DEBUG_OUT(x) x
59#else
60# define DEBUG_OUT(x) Curl_nop_stmt
61#endif
62
63CURLcode Curl_input_ntlm(struct Curl_easy *data,
64 bool proxy, /* if proxy or not */
65 const char *header) /* rest of the www-authenticate:
66 header */
67{
68 /* point to the correct struct with this */
69 struct ntlmdata *ntlm;
70 curlntlm *state;
71 CURLcode result = CURLE_OK;
72 struct connectdata *conn = data->conn;
73
74 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
75 state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
76
77 if(checkprefix("NTLM", header)) {
78 header += strlen("NTLM");
79
80 while(*header && ISSPACE(*header))
81 header++;
82
83 if(*header) {
84 unsigned char *hdr;
85 size_t hdrlen;
86
87 result = Curl_base64_decode(header, &hdr, &hdrlen);
88 if(!result) {
89 struct bufref hdrbuf;
90
91 Curl_bufref_init(&hdrbuf);
92 Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
93 result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
94 Curl_bufref_free(&hdrbuf);
95 }
96 if(result)
97 return result;
98
99 *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
100 }
101 else {
102 if(*state == NTLMSTATE_LAST) {
103 infof(data, "NTLM auth restarted");
104 Curl_http_auth_cleanup_ntlm(conn);
105 }
106 else if(*state == NTLMSTATE_TYPE3) {
107 infof(data, "NTLM handshake rejected");
108 Curl_http_auth_cleanup_ntlm(conn);
109 *state = NTLMSTATE_NONE;
110 return CURLE_REMOTE_ACCESS_DENIED;
111 }
112 else if(*state >= NTLMSTATE_TYPE1) {
113 infof(data, "NTLM handshake failure (internal error)");
114 return CURLE_REMOTE_ACCESS_DENIED;
115 }
116
117 *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
118 }
119 }
120
121 return result;
122}
123
124/*
125 * This is for creating ntlm header output
126 */
127CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
128{
129 char *base64 = NULL;
130 size_t len = 0;
131 CURLcode result = CURLE_OK;
132 struct bufref ntlmmsg;
133
134 /* point to the address of the pointer that holds the string to send to the
135 server, which is for a plain host or for a HTTP proxy */
136 char **allocuserpwd;
137
138 /* point to the username, password, service and host */
139 const char *userp;
140 const char *passwdp;
141 const char *service = NULL;
142 const char *hostname = NULL;
143
144 /* point to the correct struct with this */
145 struct ntlmdata *ntlm;
146 curlntlm *state;
147 struct auth *authp;
148 struct connectdata *conn = data->conn;
149
150 DEBUGASSERT(conn);
151 DEBUGASSERT(data);
152
153 if(proxy) {
154#ifndef CURL_DISABLE_PROXY
155 allocuserpwd = &data->state.aptr.proxyuserpwd;
156 userp = data->state.aptr.proxyuser;
157 passwdp = data->state.aptr.proxypasswd;
158 service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
159 data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
160 hostname = conn->http_proxy.host.name;
161 ntlm = &conn->proxyntlm;
162 state = &conn->proxy_ntlm_state;
163 authp = &data->state.authproxy;
164#else
165 return CURLE_NOT_BUILT_IN;
166#endif
167 }
168 else {
169 allocuserpwd = &data->state.aptr.userpwd;
170 userp = data->state.aptr.user;
171 passwdp = data->state.aptr.passwd;
172 service = data->set.str[STRING_SERVICE_NAME] ?
173 data->set.str[STRING_SERVICE_NAME] : "HTTP";
174 hostname = conn->host.name;
175 ntlm = &conn->ntlm;
176 state = &conn->http_ntlm_state;
177 authp = &data->state.authhost;
178 }
179 authp->done = FALSE;
180
181 /* not set means empty */
182 if(!userp)
183 userp = "";
184
185 if(!passwdp)
186 passwdp = "";
187
188#ifdef USE_WINDOWS_SSPI
189 if(!s_hSecDll) {
190 /* not thread safe and leaks - use curl_global_init() to avoid */
191 CURLcode err = Curl_sspi_global_init();
192 if(!s_hSecDll)
193 return err;
194 }
195#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
196 ntlm->sslContext = conn->sslContext;
197#endif
198#endif
199
200 Curl_bufref_init(&ntlmmsg);
201 switch(*state) {
202 case NTLMSTATE_TYPE1:
203 default: /* for the weird cases we (re)start here */
204 /* Create a type-1 message */
205 result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp,
206 service, hostname,
207 ntlm, &ntlmmsg);
208 if(!result) {
209 DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
210 result = Curl_base64_encode(data,
211 (const char *) Curl_bufref_ptr(&ntlmmsg),
212 Curl_bufref_len(&ntlmmsg), &base64, &len);
213 if(!result) {
214 free(*allocuserpwd);
215 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
216 proxy ? "Proxy-" : "",
217 base64);
218 free(base64);
219 if(!*allocuserpwd)
220 result = CURLE_OUT_OF_MEMORY;
221 }
222 }
223 break;
224
225 case NTLMSTATE_TYPE2:
226 /* We already received the type-2 message, create a type-3 message */
227 result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
228 ntlm, &ntlmmsg);
229 if(!result && Curl_bufref_len(&ntlmmsg)) {
230 result = Curl_base64_encode(data,
231 (const char *) Curl_bufref_ptr(&ntlmmsg),
232 Curl_bufref_len(&ntlmmsg), &base64, &len);
233 if(!result) {
234 free(*allocuserpwd);
235 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
236 proxy ? "Proxy-" : "",
237 base64);
238 free(base64);
239 if(!*allocuserpwd)
240 result = CURLE_OUT_OF_MEMORY;
241 else {
242 *state = NTLMSTATE_TYPE3; /* we send a type-3 */
243 authp->done = TRUE;
244 }
245 }
246 }
247 break;
248
249 case NTLMSTATE_TYPE3:
250 /* connection is already authenticated,
251 * don't send a header in future requests */
252 *state = NTLMSTATE_LAST;
253 /* FALLTHROUGH */
254 case NTLMSTATE_LAST:
255 Curl_safefree(*allocuserpwd);
256 authp->done = TRUE;
257 break;
258 }
259 Curl_bufref_free(&ntlmmsg);
260
261 return result;
262}
263
264void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
265{
266 Curl_auth_cleanup_ntlm(&conn->ntlm);
267 Curl_auth_cleanup_ntlm(&conn->proxyntlm);
268
269#if defined(NTLM_WB_ENABLED)
270 Curl_http_auth_cleanup_ntlm_wb(conn);
271#endif
272}
273
274#endif /* !CURL_DISABLE_HTTP && USE_NTLM */
275