Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | * |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 30 | * https://davenport.sourceforge.io/ntlm.html |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 31 | * https://www.innovation.ch/java/ntlm.html |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 32 | */ |
| 33 | |
| 34 | #define DEBUG_ME 0 |
| 35 | |
| 36 | #include "urldata.h" |
| 37 | #include "sendf.h" |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 38 | #include "strcase.h" |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 39 | #include "http_ntlm.h" |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 40 | #include "curl_ntlm_core.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 41 | #include "curl_ntlm_wb.h" |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 42 | #include "vauth/vauth.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 43 | #include "url.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 44 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 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) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 48 | #include "vtls/nssg.h" |
| 49 | #elif defined(USE_WINDOWS_SSPI) |
| 50 | #include "curl_sspi.h" |
| 51 | #endif |
| 52 | |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 53 | /* The last 3 #include files should be in this order */ |
| 54 | #include "curl_printf.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | |
| 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) { |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 82 | result = Curl_auth_decode_ntlm_type2_message(conn->data, header, ntlm); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 83 | if(result) |
| 84 | return result; |
| 85 | |
| 86 | ntlm->state = NTLMSTATE_TYPE2; /* We got a type-2 message */ |
| 87 | } |
| 88 | else { |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 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) { |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | */ |
| 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 | |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 124 | /* point to the username, password, service and host */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 125 | const char *userp; |
| 126 | const char *passwdp; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 127 | const char *service = NULL; |
| 128 | const char *hostname = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 137 | #if defined(NTLM_NEEDS_NSS_INIT) |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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; |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 144 | userp = conn->http_proxy.user; |
| 145 | passwdp = conn->http_proxy.passwd; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 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; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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; |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 156 | service = conn->data->set.str[STRING_SERVICE_NAME] ? |
| 157 | conn->data->set.str[STRING_SERVICE_NAME] : "HTTP"; |
| 158 | hostname = conn->host.name; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 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 | #endif |
| 179 | |
| 180 | switch(ntlm->state) { |
| 181 | case NTLMSTATE_TYPE1: |
| 182 | default: /* for the weird cases we (re)start here */ |
| 183 | /* Create a type-1 message */ |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 184 | result = Curl_auth_create_ntlm_type1_message(conn->data, userp, passwdp, |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 185 | service, hostname, |
| 186 | ntlm, &base64, |
| 187 | &len); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 188 | if(result) |
| 189 | return result; |
| 190 | |
| 191 | if(base64) { |
| 192 | free(*allocuserpwd); |
| 193 | *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n", |
| 194 | proxy ? "Proxy-" : "", |
| 195 | base64); |
| 196 | free(base64); |
| 197 | if(!*allocuserpwd) |
| 198 | return CURLE_OUT_OF_MEMORY; |
| 199 | |
| 200 | DEBUG_OUT(fprintf(stderr, "**** Header %s\n ", *allocuserpwd)); |
| 201 | } |
| 202 | break; |
| 203 | |
| 204 | case NTLMSTATE_TYPE2: |
| 205 | /* We already received the type-2 message, create a type-3 message */ |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 206 | result = Curl_auth_create_ntlm_type3_message(conn->data, userp, passwdp, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 207 | ntlm, &base64, &len); |
| 208 | if(result) |
| 209 | return result; |
| 210 | |
| 211 | if(base64) { |
| 212 | free(*allocuserpwd); |
| 213 | *allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n", |
| 214 | proxy ? "Proxy-" : "", |
| 215 | base64); |
| 216 | free(base64); |
| 217 | if(!*allocuserpwd) |
| 218 | return CURLE_OUT_OF_MEMORY; |
| 219 | |
| 220 | DEBUG_OUT(fprintf(stderr, "**** %s\n ", *allocuserpwd)); |
| 221 | |
| 222 | ntlm->state = NTLMSTATE_TYPE3; /* we send a type-3 */ |
| 223 | authp->done = TRUE; |
| 224 | } |
| 225 | break; |
| 226 | |
| 227 | case NTLMSTATE_TYPE3: |
| 228 | /* connection is already authenticated, |
| 229 | * don't send a header in future requests */ |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 230 | ntlm->state = NTLMSTATE_LAST; |
| 231 | /* fall-through */ |
| 232 | case NTLMSTATE_LAST: |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 233 | Curl_safefree(*allocuserpwd); |
| 234 | authp->done = TRUE; |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | return CURLE_OK; |
| 239 | } |
| 240 | |
| 241 | void Curl_http_ntlm_cleanup(struct connectdata *conn) |
| 242 | { |
Alex Deymo | 8f1a214 | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 243 | Curl_auth_ntlm_cleanup(&conn->ntlm); |
| 244 | Curl_auth_ntlm_cleanup(&conn->proxyntlm); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 245 | |
| 246 | #if defined(NTLM_WB_ENABLED) |
| 247 | Curl_ntlm_wb_cleanup(conn); |
| 248 | #endif |
| 249 | } |
| 250 | |
| 251 | #endif /* !CURL_DISABLE_HTTP && USE_NTLM */ |