• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2020, 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(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 connectdata * conn,bool proxy,const char * header)62 CURLcode Curl_input_ntlm(struct connectdata *conn,
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 
72   ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
73   state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
74 
75   if(checkprefix("NTLM", header)) {
76     header += strlen("NTLM");
77 
78     while(*header && ISSPACE(*header))
79       header++;
80 
81     if(*header) {
82       result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
83       if(result)
84         return result;
85 
86       *state = NTLMSTATE_TYPE2; /* We got a type-2 message */
87     }
88     else {
89       if(*state == NTLMSTATE_LAST) {
90         infof(conn->data, "NTLM auth restarted\n");
91         Curl_http_auth_cleanup_ntlm(conn);
92       }
93       else if(*state == NTLMSTATE_TYPE3) {
94         infof(conn->data, "NTLM handshake rejected\n");
95         Curl_http_auth_cleanup_ntlm(conn);
96         *state = NTLMSTATE_NONE;
97         return CURLE_REMOTE_ACCESS_DENIED;
98       }
99       else if(*state >= NTLMSTATE_TYPE1) {
100         infof(conn->data, "NTLM handshake failure (internal error)\n");
101         return CURLE_REMOTE_ACCESS_DENIED;
102       }
103 
104       *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  */
Curl_output_ntlm(struct connectdata * conn,bool proxy)114 CURLcode 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 
124   /* point to the username, password, service and host */
125   const char *userp;
126   const char *passwdp;
127   const char *service = NULL;
128   const char *hostname = NULL;
129 
130   /* point to the correct struct with this */
131   struct ntlmdata *ntlm;
132   curlntlm *state;
133   struct auth *authp;
134   struct Curl_easy *data = conn->data;
135 
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 = conn->data->set.str[STRING_PROXY_SERVICE_NAME] ?
146               conn->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 = &conn->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 = conn->data->set.str[STRING_SERVICE_NAME] ?
160               conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
161     hostname = conn->host.name;
162     ntlm = &conn->ntlm;
163     state = &conn->http_ntlm_state;
164     authp = &conn->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(conn->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(conn->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