Imported Upstream version 7.48.0
[platform/upstream/curl.git] / lib / curl_ntlm.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2015, 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  * http://davenport.sourceforge.net/ntlm.html
31  * http://www.innovation.ch/java/ntlm.html
32  */
33
34 #define DEBUG_ME 0
35
36 #include "urldata.h"
37 #include "sendf.h"
38 #include "rawstr.h"
39 #include "curl_ntlm.h"
40 #include "curl_ntlm_msgs.h"
41 #include "curl_ntlm_wb.h"
42 #include "curl_sasl.h"
43 #include "url.h"
44 #include "curl_printf.h"
45
46 #if defined(USE_NSS)
47 #include "vtls/nssg.h"
48 #elif defined(USE_WINDOWS_SSPI)
49 #include "curl_sspi.h"
50 #endif
51
52 /* The last #include files should be: */
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
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   CURLcode result = CURLE_OK;
70
71   ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
72
73   if(checkprefix("NTLM", header)) {
74     header += strlen("NTLM");
75
76     while(*header && ISSPACE(*header))
77       header++;
78
79     if(*header) {
80       result = Curl_sasl_decode_ntlm_type2_message(conn->data, header, ntlm);
81       if(result)
82         return result;
83
84       ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */
85     }
86     else {
87       if(ntlm->state == NTLMSTATE_LAST) {
88         infof(conn->data, "NTLM auth restarted\n");
89         Curl_http_ntlm_cleanup(conn);
90       }
91       else if(ntlm->state == NTLMSTATE_TYPE3) {
92         infof(conn->data, "NTLM handshake rejected\n");
93         Curl_http_ntlm_cleanup(conn);
94         ntlm->state = NTLMSTATE_NONE;
95         return CURLE_REMOTE_ACCESS_DENIED;
96       }
97       else if(ntlm->state >= NTLMSTATE_TYPE1) {
98         infof(conn->data, "NTLM handshake failure (internal error)\n");
99         return CURLE_REMOTE_ACCESS_DENIED;
100       }
101
102       ntlm->state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
103     }
104   }
105
106   return result;
107 }
108
109 /*
110  * This is for creating ntlm header output
111  */
112 CURLcode Curl_output_ntlm(struct connectdata *conn, bool proxy)
113 {
114   char *base64 = NULL;
115   size_t len = 0;
116   CURLcode result;
117
118   /* point to the address of the pointer that holds the string to send to the
119      server, which is for a plain host or for a HTTP proxy */
120   char **allocuserpwd;
121
122   /* point to the name and password for this */
123   const char *userp;
124   const char *passwdp;
125
126   /* point to the correct struct with this */
127   struct ntlmdata *ntlm;
128   struct auth *authp;
129
130   DEBUGASSERT(conn);
131   DEBUGASSERT(conn->data);
132
133 #ifdef USE_NSS
134   if(CURLE_OK != Curl_nss_force_init(conn->data))
135     return CURLE_OUT_OF_MEMORY;
136 #endif
137
138   if(proxy) {
139     allocuserpwd = &conn->allocptr.proxyuserpwd;
140     userp = conn->proxyuser;
141     passwdp = conn->proxypasswd;
142     ntlm = &conn->proxyntlm;
143     authp = &conn->data->state.authproxy;
144   }
145   else {
146     allocuserpwd = &conn->allocptr.userpwd;
147     userp = conn->user;
148     passwdp = conn->passwd;
149     ntlm = &conn->ntlm;
150     authp = &conn->data->state.authhost;
151   }
152   authp->done = FALSE;
153
154   /* not set means empty */
155   if(!userp)
156     userp = "";
157
158   if(!passwdp)
159     passwdp = "";
160
161 #ifdef USE_WINDOWS_SSPI
162   if(s_hSecDll == NULL) {
163     /* not thread safe and leaks - use curl_global_init() to avoid */
164     CURLcode err = Curl_sspi_global_init();
165     if(s_hSecDll == NULL)
166       return err;
167   }
168 #endif
169
170   switch(ntlm->state) {
171   case NTLMSTATE_TYPE1:
172   default: /* for the weird cases we (re)start here */
173     /* Create a type-1 message */
174     result = Curl_sasl_create_ntlm_type1_message(userp, passwdp, ntlm, &base64,
175                                                  &len);
176     if(result)
177       return result;
178
179     if(base64) {
180       free(*allocuserpwd);
181       *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
182                               proxy ? "Proxy-" : "",
183                               base64);
184       free(base64);
185       if(!*allocuserpwd)
186         return CURLE_OUT_OF_MEMORY;
187
188       DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd));
189     }
190     break;
191
192   case NTLMSTATE_TYPE2:
193     /* We already received the type-2 message, create a type-3 message */
194     result = Curl_sasl_create_ntlm_type3_message(conn->data, userp, passwdp,
195                                                  ntlm, &base64, &len);
196     if(result)
197       return result;
198
199     if(base64) {
200       free(*allocuserpwd);
201       *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
202                               proxy ? "Proxy-" : "",
203                               base64);
204       free(base64);
205       if(!*allocuserpwd)
206         return CURLE_OUT_OF_MEMORY;
207
208       DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd));
209
210       ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */
211       authp->done = TRUE;
212     }
213     break;
214
215   case NTLMSTATE_TYPE3:
216     /* connection is already authenticated,
217      * don't send a header in future requests */
218     ntlm->state = NTLMSTATE_LAST;
219     /* fall-through */
220   case NTLMSTATE_LAST:
221     Curl_safefree(*allocuserpwd);
222     authp->done = TRUE;
223     break;
224   }
225
226   return CURLE_OK;
227 }
228
229 void Curl_http_ntlm_cleanup(struct connectdata *conn)
230 {
231   Curl_sasl_ntlm_cleanup(&conn->ntlm);
232   Curl_sasl_ntlm_cleanup(&conn->proxyntlm);
233
234 #if defined(NTLM_WB_ENABLED)
235   Curl_ntlm_wb_cleanup(conn);
236 #endif
237 }
238
239 #endif /* !CURL_DISABLE_HTTP && USE_NTLM */