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