bb7e5360f967e1bec1e340656b6deb2d1d226a11
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / http_ntlm.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2022, 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
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  */
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((const char *) Curl_bufref_ptr(&ntlmmsg),
217                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
218       if(!result) {
219         free(*allocuserpwd);
220         *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
221                                 proxy ? "Proxy-" : "",
222                                 base64);
223         free(base64);
224         if(!*allocuserpwd)
225           result = CURLE_OUT_OF_MEMORY;
226       }
227     }
228     break;
229
230   case NTLMSTATE_TYPE2:
231     /* We already received the type-2 message, create a type-3 message */
232     result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
233                                                  ntlm, &ntlmmsg);
234     if(!result && Curl_bufref_len(&ntlmmsg)) {
235       result = Curl_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
236                                   Curl_bufref_len(&ntlmmsg), &base64, &len);
237       if(!result) {
238         free(*allocuserpwd);
239         *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
240                                 proxy ? "Proxy-" : "",
241                                 base64);
242         free(base64);
243         if(!*allocuserpwd)
244           result = CURLE_OUT_OF_MEMORY;
245         else {
246           *state = NTLMSTATE_TYPE3; /* we send a type-3 */
247           authp->done = TRUE;
248         }
249       }
250     }
251     break;
252
253   case NTLMSTATE_LAST:
254     Curl_safefree(*allocuserpwd);
255     authp->done = TRUE;
256     break;
257   }
258   Curl_bufref_free(&ntlmmsg);
259
260   return result;
261 }
262
263 void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
264 {
265   Curl_auth_cleanup_ntlm(&conn->ntlm);
266   Curl_auth_cleanup_ntlm(&conn->proxyntlm);
267
268 #if defined(NTLM_WB_ENABLED)
269   Curl_http_auth_cleanup_ntlm_wb(conn);
270 #endif
271 }
272
273 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */