blob: fd5540b5d06025c35fd4761060f87b0ea2b30d3b [file] [log] [blame]
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07001/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
Alex Deymo486467e2017-12-19 19:04:07 +01008 * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -07009 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
Alex Deymo8f1a2142016-06-28 14:49:26 -070012 * are also available at https://curl.haxx.se/docs/copyright.html.
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070013 *
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 *
Elliott Hughes82be86d2017-09-20 17:00:17 -070030 * https://davenport.sourceforge.io/ntlm.html
Alex Deymo8f1a2142016-06-28 14:49:26 -070031 * https://www.innovation.ch/java/ntlm.html
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070032 */
33
34#define DEBUG_ME 0
35
36#include "urldata.h"
37#include "sendf.h"
Elliott Hughescee03382017-06-23 12:17:18 -070038#include "strcase.h"
Alex Deymo8f1a2142016-06-28 14:49:26 -070039#include "http_ntlm.h"
Alex Deymo486467e2017-12-19 19:04:07 +010040#include "curl_ntlm_core.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070041#include "curl_ntlm_wb.h"
Alex Deymo8f1a2142016-06-28 14:49:26 -070042#include "vauth/vauth.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070043#include "url.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070044
Alex Deymo486467e2017-12-19 19:04:07 +010045/* 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)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070048#include "vtls/nssg.h"
49#elif defined(USE_WINDOWS_SSPI)
50#include "curl_sspi.h"
51#endif
52
Alex Deymo8f1a2142016-06-28 14:49:26 -070053/* The last 3 #include files should be in this order */
54#include "curl_printf.h"
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070055#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 CURLcode result = CURLE_OK;
72
73 ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
74
75 if(checkprefix("NTLM", header)) {
76 header += strlen("NTLM");
77
78 while(*header && ISSPACE(*header))
79 header++;
80
81 if(*header) {
Alex Deymo8f1a2142016-06-28 14:49:26 -070082 result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070083 if(result)
84 return result;
85
86 ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
87 }
88 else {
Alex Deymo8f1a2142016-06-28 14:49:26 -070089 if(ntlm->state == NTLMSTATE_LAST) {
90 infof(conn->data, "NTLM auth restarted\n");
91 Curl_http_ntlm_cleanup(conn);
92 }
93 else if(ntlm->state == NTLMSTATE_TYPE3) {
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -070094 infof(conn->data, "NTLM handshake rejected\n");
95 Curl_http_ntlm_cleanup(conn);
96 ntlm->state = NTLMSTATE_NONE;
97 return CURLE_REMOTE_ACCESS_DENIED;
98 }
99 else if(ntlm->state >= NTLMSTATE_TYPE1) {
100 infof(conn->data, "NTLM handshake failure (internal error)\n");
101 return CURLE_REMOTE_ACCESS_DENIED;
102 }
103
104 ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
105 }
106 }
107
108 return result;
109}
110
111/*
112 * This is for creating ntlm header output
113 */
114CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
115{
116 char *base64 = NULL;
117 size_t len = 0;
118 CURLcode result;
119
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 */
122 char **allocuserpwd;
123
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700124 /* point to the username, password, service and host */
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700125 const char *userp;
126 const char *passwdp;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700127 const char *service = NULL;
128 const char *hostname = NULL;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700129
130 /* point to the correct struct with this */
131 struct ntlmdata *ntlm;
132 struct auth *authp;
133
134 DEBUGASSERT(conn);
135 DEBUGASSERT(conn->data);
136
Alex Deymo486467e2017-12-19 19:04:07 +0100137#if defined(NTLM_NEEDS_NSS_INIT)
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700138 if(CURLE_OK != Curl_nss_force_init(conn->data))
139 return CURLE_OUT_OF_MEMORY;
140#endif
141
142 if(proxy) {
143 allocuserpwd = &conn->allocptr.proxyuserpwd;
Elliott Hughescee03382017-06-23 12:17:18 -0700144 userp = conn->http_proxy.user;
145 passwdp = conn->http_proxy.passwd;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700146 service = conn->data->set.str[STRING_PROXY_SERVICE_NAME] ?
147 conn->data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
148 hostname = conn->http_proxy.host.name;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700149 ntlm = &conn->proxyntlm;
150 authp = &conn->data->state.authproxy;
151 }
152 else {
153 allocuserpwd = &conn->allocptr.userpwd;
154 userp = conn->user;
155 passwdp = conn->passwd;
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700156 service = conn->data->set.str[STRING_SERVICE_NAME] ?
157 conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
158 hostname = conn->host.name;
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700159 ntlm = &conn->ntlm;
160 authp = &conn->data->state.authhost;
161 }
162 authp->done = FALSE;
163
164 /* not set means empty */
165 if(!userp)
166 userp = "";
167
168 if(!passwdp)
169 passwdp = "";
170
171#ifdef USE_WINDOWS_SSPI
172 if(s_hSecDll == NULL) {
173 /* not thread safe and leaks - use curl_global_init() to avoid */
174 CURLcode err = Curl_sspi_global_init();
175 if(s_hSecDll == NULL)
176 return err;
177 }
178#endif
179
180 switch(ntlm->state) {
181 case NTLMSTATE_TYPE1:
182 default: /* for the weird cases we (re)start here */
183 /* Create a type-1 message */
Alex Deymo486467e2017-12-19 19:04:07 +0100184 result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
Elliott Hughes1ef06ba2018-05-30 15:43:58 -0700185 service, hostname,
186 ntlm, &base64,
187 &len);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700188 if(result)
189 return result;
190
191 if(base64) {
192 free(*allocuserpwd);
193 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
194 proxy ? "Proxy-" : "",
195 base64);
196 free(base64);
197 if(!*allocuserpwd)
198 return CURLE_OUT_OF_MEMORY;
199
200 DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
201 }
202 break;
203
204 case NTLMSTATE_TYPE2:
205 /* We already received the type-2 message, create a type-3 message */
Alex Deymo8f1a2142016-06-28 14:49:26 -0700206 result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700207 ntlm, &base64, &len);
208 if(result)
209 return result;
210
211 if(base64) {
212 free(*allocuserpwd);
213 *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
214 proxy ? "Proxy-" : "",
215 base64);
216 free(base64);
217 if(!*allocuserpwd)
218 return CURLE_OUT_OF_MEMORY;
219
220 DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
221
222 ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
223 authp->done = TRUE;
224 }
225 break;
226
227 case NTLMSTATE_TYPE3:
228 /* connection is already authenticated,
229 * don't send a header in future requests */
Alex Deymo8f1a2142016-06-28 14:49:26 -0700230 ntlm->state = NTLMSTATE_LAST;
231 /* fall-through */
232 case NTLMSTATE_LAST:
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700233 Curl_safefree(*allocuserpwd);
234 authp->done = TRUE;
235 break;
236 }
237
238 return CURLE_OK;
239}
240
241void Curl_http_ntlm_cleanup(struct connectdata *conn)
242{
Alex Deymo8f1a2142016-06-28 14:49:26 -0700243 Curl_auth_ntlm_cleanup(&conn->ntlm);
244 Curl_auth_ntlm_cleanup(&conn->proxyntlm);
Bertrand SIMONNETe6cd7382015-07-01 15:39:44 -0700245
246#if defined(NTLM_WB_ENABLED)
247 Curl_ntlm_wb_cleanup(conn);
248#endif
249}
250
251#endif /* !CURL_DISABLE_HTTP && USE_NTLM */