cmake/Utilities/cmcurl/lib/http_ntlm.c

271 lines
7.7 KiB
C
Raw Normal View History

2015-04-27 22:25:09 +02:00
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
2023-07-02 19:51:09 +02:00
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
2015-04-27 22:25:09 +02:00
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
2021-09-14 00:13:48 +02:00
* are also available at https://curl.se/docs/copyright.html.
2015-04-27 22:25:09 +02:00
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
2022-11-16 20:14:03 +01:00
* SPDX-License-Identifier: curl
*
2015-04-27 22:25:09 +02:00
***************************************************************************/
#include "curl_setup.h"
2015-11-17 17:22:37 +01:00
#if !defined(CURL_DISABLE_HTTP) && defined(USE_NTLM)
2015-04-27 22:25:09 +02:00
/*
* NTLM details:
*
2022-11-16 20:14:03 +01:00
* https://davenport.sourceforge.net/ntlm.html
2016-09-11 17:22:32 +02:00
* https://www.innovation.ch/java/ntlm.html
2015-04-27 22:25:09 +02:00
*/
#define DEBUG_ME 0
#include "urldata.h"
#include "sendf.h"
2017-04-14 19:02:05 +02:00
#include "strcase.h"
2016-09-11 17:22:32 +02:00
#include "http_ntlm.h"
2018-01-26 17:06:56 +01:00
#include "curl_ntlm_core.h"
2021-09-14 00:13:48 +02:00
#include "curl_base64.h"
2016-09-11 17:22:32 +02:00
#include "vauth/vauth.h"
2015-04-27 22:25:09 +02:00
#include "url.h"
2018-01-26 17:06:56 +01:00
/* SSL backend-specific #if branches in this file must be kept in the order
documented in curl_ntlm_core. */
2020-08-30 11:54:41 +02:00
#if defined(USE_WINDOWS_SSPI)
2015-04-27 22:25:09 +02:00
#include "curl_sspi.h"
#endif
2016-09-11 17:22:32 +02:00
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
2015-11-17 17:22:37 +01:00
#include "curl_memory.h"
2015-04-27 22:25:09 +02:00
#include "memdebug.h"
#if DEBUG_ME
# define DEBUG_OUT(x) x
#else
# define DEBUG_OUT(x) Curl_nop_stmt
#endif
2021-09-14 00:13:48 +02:00
CURLcode Curl_input_ntlm(struct Curl_easy *data,
2015-04-27 22:25:09 +02:00
bool proxy, /* if proxy or not */
const char *header) /* rest of the www-authenticate:
header */
{
/* point to the correct struct with this */
struct ntlmdata *ntlm;
2019-11-11 23:01:05 +01:00
curlntlm *state;
2015-04-27 22:25:09 +02:00
CURLcode result = CURLE_OK;
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2015-04-27 22:25:09 +02:00
ntlm = proxy ? &conn->proxyntlm : &conn->ntlm;
2019-11-11 23:01:05 +01:00
state = proxy ? &conn->proxy_ntlm_state : &conn->http_ntlm_state;
2015-04-27 22:25:09 +02:00
if(checkprefix("NTLM", header)) {
header += strlen("NTLM");
while(*header && ISSPACE(*header))
header++;
if(*header) {
2021-09-14 00:13:48 +02:00
unsigned char *hdr;
size_t hdrlen;
result = Curl_base64_decode(header, &hdr, &hdrlen);
if(!result) {
struct bufref hdrbuf;
Curl_bufref_init(&hdrbuf);
Curl_bufref_set(&hdrbuf, hdr, hdrlen, curl_free);
result = Curl_auth_decode_ntlm_type2_message(data, &hdrbuf, ntlm);
Curl_bufref_free(&hdrbuf);
}
2015-11-17 17:22:37 +01:00
if(result)
2015-04-27 22:25:09 +02:00
return result;
2019-11-11 23:01:05 +01:00
*state = NTLMSTATE_TYPE2; /* We got a type-2 message */
2015-04-27 22:25:09 +02:00
}
else {
2019-11-11 23:01:05 +01:00
if(*state == NTLMSTATE_LAST) {
2021-11-20 13:41:27 +01:00
infof(data, "NTLM auth restarted");
2019-11-11 23:01:05 +01:00
Curl_http_auth_cleanup_ntlm(conn);
2015-11-17 17:22:37 +01:00
}
2019-11-11 23:01:05 +01:00
else if(*state == NTLMSTATE_TYPE3) {
2021-11-20 13:41:27 +01:00
infof(data, "NTLM handshake rejected");
2019-11-11 23:01:05 +01:00
Curl_http_auth_cleanup_ntlm(conn);
*state = NTLMSTATE_NONE;
2015-04-27 22:25:09 +02:00
return CURLE_REMOTE_ACCESS_DENIED;
}
2019-11-11 23:01:05 +01:00
else if(*state >= NTLMSTATE_TYPE1) {
2021-11-20 13:41:27 +01:00
infof(data, "NTLM handshake failure (internal error)");
2015-04-27 22:25:09 +02:00
return CURLE_REMOTE_ACCESS_DENIED;
}
2019-11-11 23:01:05 +01:00
*state = NTLMSTATE_TYPE1; /* We should send away a type-1 */
2015-04-27 22:25:09 +02:00
}
}
return result;
}
/*
* This is for creating ntlm header output
*/
2021-09-14 00:13:48 +02:00
CURLcode Curl_output_ntlm(struct Curl_easy *data, bool proxy)
2015-04-27 22:25:09 +02:00
{
char *base64 = NULL;
size_t len = 0;
2021-09-14 00:13:48 +02:00
CURLcode result = CURLE_OK;
struct bufref ntlmmsg;
2015-04-27 22:25:09 +02:00
/* point to the address of the pointer that holds the string to send to the
2023-05-23 16:38:00 +02:00
server, which is for a plain host or for an HTTP proxy */
2015-04-27 22:25:09 +02:00
char **allocuserpwd;
2018-08-09 18:06:22 +02:00
/* point to the username, password, service and host */
2015-04-27 22:25:09 +02:00
const char *userp;
const char *passwdp;
2018-08-09 18:06:22 +02:00
const char *service = NULL;
const char *hostname = NULL;
2015-04-27 22:25:09 +02:00
/* point to the correct struct with this */
struct ntlmdata *ntlm;
2019-11-11 23:01:05 +01:00
curlntlm *state;
2015-04-27 22:25:09 +02:00
struct auth *authp;
2021-09-14 00:13:48 +02:00
struct connectdata *conn = data->conn;
2015-04-27 22:25:09 +02:00
2020-08-30 11:54:41 +02:00
DEBUGASSERT(conn);
DEBUGASSERT(data);
2015-04-27 22:25:09 +02:00
if(proxy) {
2020-08-30 11:54:41 +02:00
#ifndef CURL_DISABLE_PROXY
allocuserpwd = &data->state.aptr.proxyuserpwd;
2021-09-14 00:13:48 +02:00
userp = data->state.aptr.proxyuser;
passwdp = data->state.aptr.proxypasswd;
service = data->set.str[STRING_PROXY_SERVICE_NAME] ?
data->set.str[STRING_PROXY_SERVICE_NAME] : "HTTP";
2018-08-09 18:06:22 +02:00
hostname = conn->http_proxy.host.name;
2015-04-27 22:25:09 +02:00
ntlm = &conn->proxyntlm;
2019-11-11 23:01:05 +01:00
state = &conn->proxy_ntlm_state;
2021-09-14 00:13:48 +02:00
authp = &data->state.authproxy;
2020-08-30 11:54:41 +02:00
#else
return CURLE_NOT_BUILT_IN;
#endif
2015-04-27 22:25:09 +02:00
}
else {
2020-08-30 11:54:41 +02:00
allocuserpwd = &data->state.aptr.userpwd;
2021-09-14 00:13:48 +02:00
userp = data->state.aptr.user;
passwdp = data->state.aptr.passwd;
service = data->set.str[STRING_SERVICE_NAME] ?
data->set.str[STRING_SERVICE_NAME] : "HTTP";
2018-08-09 18:06:22 +02:00
hostname = conn->host.name;
2015-04-27 22:25:09 +02:00
ntlm = &conn->ntlm;
2019-11-11 23:01:05 +01:00
state = &conn->http_ntlm_state;
2021-09-14 00:13:48 +02:00
authp = &data->state.authhost;
2015-04-27 22:25:09 +02:00
}
authp->done = FALSE;
/* not set means empty */
if(!userp)
userp = "";
if(!passwdp)
passwdp = "";
#ifdef USE_WINDOWS_SSPI
2021-09-14 00:13:48 +02:00
if(!s_hSecDll) {
2015-04-27 22:25:09 +02:00
/* not thread safe and leaks - use curl_global_init() to avoid */
CURLcode err = Curl_sspi_global_init();
2021-09-14 00:13:48 +02:00
if(!s_hSecDll)
2015-04-27 22:25:09 +02:00
return err;
}
2019-11-11 23:01:05 +01:00
#ifdef SECPKG_ATTR_ENDPOINT_BINDINGS
ntlm->sslContext = conn->sslContext;
#endif
2015-04-27 22:25:09 +02:00
#endif
2021-09-14 00:13:48 +02:00
Curl_bufref_init(&ntlmmsg);
2022-03-29 21:10:50 +02:00
/* connection is already authenticated, don't send a header in future
* requests so go directly to NTLMSTATE_LAST */
if(*state == NTLMSTATE_TYPE3)
*state = NTLMSTATE_LAST;
2019-11-11 23:01:05 +01:00
switch(*state) {
2015-04-27 22:25:09 +02:00
case NTLMSTATE_TYPE1:
default: /* for the weird cases we (re)start here */
/* Create a type-1 message */
2021-09-14 00:13:48 +02:00
result = Curl_auth_create_ntlm_type1_message(data, userp, passwdp,
2018-08-09 18:06:22 +02:00
service, hostname,
2021-09-14 00:13:48 +02:00
ntlm, &ntlmmsg);
if(!result) {
DEBUGASSERT(Curl_bufref_len(&ntlmmsg) != 0);
2022-08-04 22:12:04 +02:00
result = Curl_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
2021-09-14 00:13:48 +02:00
Curl_bufref_len(&ntlmmsg), &base64, &len);
if(!result) {
free(*allocuserpwd);
*allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
proxy ? "Proxy-" : "",
base64);
free(base64);
if(!*allocuserpwd)
result = CURLE_OUT_OF_MEMORY;
}
2015-04-27 22:25:09 +02:00
}
break;
case NTLMSTATE_TYPE2:
/* We already received the type-2 message, create a type-3 message */
2021-09-14 00:13:48 +02:00
result = Curl_auth_create_ntlm_type3_message(data, userp, passwdp,
ntlm, &ntlmmsg);
if(!result && Curl_bufref_len(&ntlmmsg)) {
2022-08-04 22:12:04 +02:00
result = Curl_base64_encode((const char *) Curl_bufref_ptr(&ntlmmsg),
2021-09-14 00:13:48 +02:00
Curl_bufref_len(&ntlmmsg), &base64, &len);
if(!result) {
free(*allocuserpwd);
*allocuserpwd = aprintf("%sAuthorization: NTLM %s\r\n",
proxy ? "Proxy-" : "",
base64);
free(base64);
if(!*allocuserpwd)
result = CURLE_OUT_OF_MEMORY;
else {
*state = NTLMSTATE_TYPE3; /* we send a type-3 */
authp->done = TRUE;
}
}
2015-04-27 22:25:09 +02:00
}
break;
2015-11-17 17:22:37 +01:00
case NTLMSTATE_LAST:
2015-04-27 22:25:09 +02:00
Curl_safefree(*allocuserpwd);
authp->done = TRUE;
break;
}
2021-09-14 00:13:48 +02:00
Curl_bufref_free(&ntlmmsg);
2015-04-27 22:25:09 +02:00
2021-09-14 00:13:48 +02:00
return result;
2015-04-27 22:25:09 +02:00
}
2019-11-11 23:01:05 +01:00
void Curl_http_auth_cleanup_ntlm(struct connectdata *conn)
2015-04-27 22:25:09 +02:00
{
2019-11-11 23:01:05 +01:00
Curl_auth_cleanup_ntlm(&conn->ntlm);
Curl_auth_cleanup_ntlm(&conn->proxyntlm);
2015-04-27 22:25:09 +02:00
}
2015-11-17 17:22:37 +01:00
#endif /* !CURL_DISABLE_HTTP && USE_NTLM */