• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Curl_input_ntlm(struct Curl_easy * data,bool proxy,const char * header)63 CURLcode 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  */
Curl_output_ntlm(struct Curl_easy * data,bool proxy)127 CURLcode 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 
202   /* connection is already authenticated, don't send a header in future
203    * requests so go directly to NTLMSTATE_LAST */
204   if(*state == NTLMSTATE_TYPE3)
205     *state = NTLMSTATE_LAST;
206 
207   switch(*state) {
208   case NTLMSTATE_TYPE1:
209   default: /* for the weird cases we (re)start here */
210     /* Create a type-1 message */
211     result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp,
212                                                  service, hostname,
213                                                  ntlm, &ntlmmsg);
214     if(!result) {
215       DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
216       result = Curl_base64_encode(data,
217                                   (const char *) Curl_bufref_ptr(&ntlmmsg),
218                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
219       if(!result) {
220         free(*allocuserpwd);
221         *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
222                                 proxy ? "Proxy-" : "",
223                                 base64);
224         free(base64);
225         if(!*allocuserpwd)
226           result = CURLE_OUT_OF_MEMORY;
227       }
228     }
229     break;
230 
231   case NTLMSTATE_TYPE2:
232     /* We already received the type-2 message, create a type-3 message */
233     result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
234                                                  ntlm, &ntlmmsg);
235     if(!result && Curl_bufref_len(&ntlmmsg)) {
236       result = Curl_base64_encode(data,
237                                   (const char *) Curl_bufref_ptr(&ntlmmsg),
238                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
239       if(!result) {
240         free(*allocuserpwd);
241         *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
242                                 proxy ? "Proxy-" : "",
243                                 base64);
244         free(base64);
245         if(!*allocuserpwd)
246           result = CURLE_OUT_OF_MEMORY;
247         else {
248           *state = NTLMSTATE_TYPE3; /* we send a type-3 */
249           authp->done = TRUE;
250         }
251       }
252     }
253     break;
254 
255   case NTLMSTATE_LAST:
256     Curl_safefree(*allocuserpwd);
257     authp->done = TRUE;
258     break;
259   }
260   Curl_bufref_free(&ntlmmsg);
261 
262   return result;
263 }
264 
Curl_http_auth_cleanup_ntlm(struct connectdata * conn)265 void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
266 {
267   Curl_auth_cleanup_ntlm(&conn->ntlm);
268   Curl_auth_cleanup_ntlm(&conn->proxyntlm);
269 
270 #if defined(NTLM_WB_ENABLED)
271   Curl_http_auth_cleanup_ntlm_wb(conn);
272 #endif
273 }
274 
275 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */
276