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