1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2019, 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.haxx.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 "vauth/vauth.h"
43#include "url.h"
44
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"
51#endif
52
53/* The last 3 #include files should be in this order */
54#include "curl_printf.h"
55#include "curl_memory.h"
56#include "memdebug.h"
57
58#if DEBUG_ME
59# define DEBUG_OUT(x) x
60#else
61# define DEBUG_OUT(x) Curl_nop_stmt
62#endif
63
64CURLcode Curl_input_ntlm(struct connectdata *conn,
65 bool proxy, /* if proxy or not */
66 const char *header) /* rest of the www-authenticate:
67 header */
68{
69 /* point to the correct struct with this */
70 struct ntlmdata *ntlm;
71 curlntlm *state;
72 CURLcode result = CURLE_OK;
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 result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
85 if(result)
86 return result;
87
88 *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
89 }
90 else {
91 if(*state == NTLMSTATE_LAST) {
92 infof(conn->data, "NTLM auth restarted\n");
93 Curl_http_auth_cleanup_ntlm(conn);
94 }
95 else if(*state == NTLMSTATE_TYPE3) {
96 infof(conn->data, "NTLM handshake rejected\n");
97 Curl_http_auth_cleanup_ntlm(conn);
98 *state = NTLMSTATE_NONE;
99 return CURLE_REMOTE_ACCESS_DENIED;
100 }
101 else if(*state >= NTLMSTATE_TYPE1) {
102 infof(conn->data, "NTLM handshake failure (internal error)\n");
103 return CURLE_REMOTE_ACCESS_DENIED;
104 }
105
106 *state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
107 }
108 }
109
110 return result;
111}
112
113/*
114 * This is for creating ntlm header output
115 */
116CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
117{
118 char *base64 = NULL;
119 size_t len = 0;
120 CURLcode result;
121
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 */
124 char **allocuserpwd;
125
126 /* point to the username, password, service and host */
127 const char *userp;
128 const char *passwdp;
129 const char *service = NULL;
130 const char *hostname = NULL;
131
132 /* point to the correct struct with this */
133 struct ntlmdata *ntlm;
134 curlntlm *state;
135 struct auth *authp;
136
137 DEBUGASSERT(conn);
138 DEBUGASSERT(conn->data);
139
140#if defined(NTLM_NEEDS_NSS_INIT)
141 if(CURLE_OK != Curl_nss_force_init(conn->data))
142 return CURLE_OUT_OF_MEMORY;
143#endif
144
145 if(proxy) {
146 allocuserpwd = &conn->allocptr.proxyuserpwd;
147 userp = conn->http_proxy.user;
148 passwdp = conn->http_proxy.passwd;
149 service = conn->data->set.str[STRING_PROXY_SERVICE_NAME] ?
150 conn->data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
151 hostname = conn->http_proxy.host.name;
152 ntlm = &conn->proxyntlm;
153 state = &conn->proxy_ntlm_state;
154 authp = &conn->data->state.authproxy;
155 }
156 else {
157 allocuserpwd = &conn->allocptr.userpwd;
158 userp = conn->user;
159 passwdp = conn->passwd;
160 service = conn->data->set.str[STRING_SERVICE_NAME] ?
161 conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
162 hostname = conn->host.name;
163 ntlm = &conn->ntlm;
164 state = &conn->http_ntlm_state;
165 authp = &conn->data->state.authhost;
166 }
167 authp->done = FALSE;
168
169 /* not set means empty */
170 if(!userp)
171 userp = "";
172
173 if(!passwdp)
174 passwdp = "";
175
176#ifdef USE_WINDOWS_SSPI
177 if(s_hSecDll == NULL) {
178 /* not thread safe and leaks - use curl_global_init() to avoid */
179 CURLcode err = Curl_sspi_global_init();
180 if(s_hSecDll == NULL)
181 return err;
182 }
183#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
184 ntlm->sslContext = conn->sslContext;
185#endif
186#endif
187
188 switch(*state) {
189 case NTLMSTATE_TYPE1:
190 default: /* for the weird cases we (re)start here */
191 /* Create a type-1 message */
192 result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
193 service, hostname,
194 ntlm, &base64,
195 &len);
196 if(result)
197 return result;
198
199 if(base64) {
200 free(*allocuserpwd);
201 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
202 proxy ? "Proxy-" : "",
203 base64);
204 free(base64);
205 if(!*allocuserpwd)
206 return CURLE_OUT_OF_MEMORY;
207
208 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
209 }
210 break;
211
212 case NTLMSTATE_TYPE2:
213 /* We already received the type-2 message, create a type-3 message */
214 result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
215 ntlm, &base64, &len);
216 if(result)
217 return result;
218
219 if(base64) {
220 free(*allocuserpwd);
221 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
222 proxy ? "Proxy-" : "",
223 base64);
224 free(base64);
225 if(!*allocuserpwd)
226 return CURLE_OUT_OF_MEMORY;
227
228 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
229
230 *state = NTLMSTATE_TYPE3; /* we send a type-3 */
231 authp->done = TRUE;
232 }
233 break;
234
235 case NTLMSTATE_TYPE3:
236 /* connection is already authenticated,
237 * don't send a header in future requests */
238 *state = NTLMSTATE_LAST;
239 /* FALLTHROUGH */
240 case NTLMSTATE_LAST:
241 Curl_safefree(*allocuserpwd);
242 authp->done = TRUE;
243 break;
244 }
245
246 return CURLE_OK;
247}
248
249void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
250{
251 Curl_auth_cleanup_ntlm(&conn->ntlm);
252 Curl_auth_cleanup_ntlm(&conn->proxyntlm);
253
254#if defined(NTLM_WB_ENABLED)
255 Curl_http_auth_cleanup_ntlm_wb(conn);
256#endif
257}
258
259#endif /* !CURL_DISABLE_HTTP && USE_NTLM */
260