• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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(NTLM_NEEDS_NSS_INIT)
48 #include "vtls/nssg.h"
49 #elif defined(USE_WINDOWS_SSPI)
50 #include "curl_sspi.h"
51 #endif
52 
53 /* The last 3 #include files should be in this order */
54 #include "curl_printf.h"
55 #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 
Curl_input_ntlm(struct connectdata * conn,bool proxy,const char * header)64 CURLcode 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) {
82       result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm);
83       if(result)
84         return result;
85 
86       ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
87     }
88     else {
89       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) {
94         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  */
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   struct auth *authp;
133 
134   DEBUGASSERT(conn);
135   DEBUGASSERT(conn->data);
136 
137 #if defined(NTLM_NEEDS_NSS_INIT)
138   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;
144     userp = conn->http_proxy.user;
145     passwdp = conn->http_proxy.passwd;
146     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;
149     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;
156     service = conn->data->set.str[STRING_SERVICE_NAME] ?
157               conn->data->set.str[STRING_SERVICE_NAME] : "HTTP";
158     hostname = conn->host.name;
159     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 #ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
179   ntlm->sslContext = conn->sslContext;
180 #endif
181 #endif
182 
183   switch(ntlm->state) {
184   case NTLMSTATE_TYPE1:
185   default: /* for the weird cases we (re)start here */
186     /* Create a type-1 message */
187     result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp,
188                                                  service, hostname,
189                                                  ntlm, &base64,
190                                                  &len);
191     if(result)
192       return result;
193 
194     if(base64) {
195       free(*allocuserpwd);
196       *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
197                               proxy ? "Proxy-" : "",
198                               base64);
199       free(base64);
200       if(!*allocuserpwd)
201         return CURLE_OUT_OF_MEMORY;
202 
203       DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
204     }
205     break;
206 
207   case NTLMSTATE_TYPE2:
208     /* We already received the type-2 message, create a type-3 message */
209     result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp,
210                                                  ntlm, &base64, &len);
211     if(result)
212       return result;
213 
214     if(base64) {
215       free(*allocuserpwd);
216       *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
217                               proxy ? "Proxy-" : "",
218                               base64);
219       free(base64);
220       if(!*allocuserpwd)
221         return CURLE_OUT_OF_MEMORY;
222 
223       DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
224 
225       ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
226       authp->done = TRUE;
227     }
228     break;
229 
230   case NTLMSTATE_TYPE3:
231     /* connection is already authenticated,
232      * don't send a header in future requests */
233     ntlm->state = NTLMSTATE_LAST;
234     /* FALLTHROUGH */
235   case NTLMSTATE_LAST:
236     Curl_safefree(*allocuserpwd);
237     authp->done = TRUE;
238     break;
239   }
240 
241   return CURLE_OK;
242 }
243 
Curl_http_ntlm_cleanup(struct connectdata * conn)244 void Curl_http_ntlm_cleanup(struct connectdata *conn)
245 {
246   Curl_auth_ntlm_cleanup(&conn->ntlm);
247   Curl_auth_ntlm_cleanup(&conn->proxyntlm);
248 
249 #if defined(NTLM_WB_ENABLED)
250   Curl_ntlm_wb_cleanup(conn);
251 #endif
252 }
253 
254 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */
255