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