2012-11-28 07:03:05 +04:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
* RPC over HTTP (ncacn_http)
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ncacn_http.h"
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/tchar.h>
|
2012-12-14 05:23:37 +04:00
|
|
|
#include <winpr/stream.h>
|
2012-11-28 07:03:05 +04:00
|
|
|
#include <winpr/dsparse.h>
|
2013-12-13 19:11:36 +04:00
|
|
|
#include <winpr/winhttp.h>
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
|
2012-12-14 05:23:37 +04:00
|
|
|
wStream* rpc_ntlm_http_request(rdpRpc* rpc, SecBuffer* ntlm_token, int content_length, TSG_CHANNEL channel)
|
2012-11-28 07:03:05 +04:00
|
|
|
{
|
2012-12-14 05:23:37 +04:00
|
|
|
wStream* s;
|
2012-11-28 07:03:05 +04:00
|
|
|
char* base64_ntlm_token;
|
|
|
|
HttpContext* http_context;
|
|
|
|
HttpRequest* http_request;
|
|
|
|
|
|
|
|
http_request = http_request_new();
|
|
|
|
base64_ntlm_token = crypto_base64_encode(ntlm_token->pvBuffer, ntlm_token->cbBuffer);
|
|
|
|
|
|
|
|
if (channel == TSG_CHANNEL_IN)
|
|
|
|
{
|
|
|
|
http_context = rpc->NtlmHttpIn->context;
|
|
|
|
http_request_set_method(http_request, "RPC_IN_DATA");
|
|
|
|
}
|
|
|
|
else if (channel == TSG_CHANNEL_OUT)
|
|
|
|
{
|
|
|
|
http_context = rpc->NtlmHttpOut->context;
|
|
|
|
http_request_set_method(http_request, "RPC_OUT_DATA");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
http_request->ContentLength = content_length;
|
|
|
|
http_request_set_uri(http_request, http_context->URI);
|
|
|
|
|
|
|
|
http_request_set_auth_scheme(http_request, "NTLM");
|
|
|
|
http_request_set_auth_param(http_request, base64_ntlm_token);
|
|
|
|
|
|
|
|
s = http_request_write(http_context, http_request);
|
|
|
|
http_request_free(http_request);
|
|
|
|
|
|
|
|
free(base64_ntlm_token);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_ncacn_http_send_in_channel_request(rdpRpc* rpc)
|
|
|
|
{
|
2012-12-14 05:23:37 +04:00
|
|
|
wStream* s;
|
2012-11-28 07:03:05 +04:00
|
|
|
int content_length;
|
|
|
|
BOOL continue_needed;
|
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpIn->ntlm;
|
|
|
|
|
|
|
|
continue_needed = ntlm_authenticate(ntlm);
|
|
|
|
|
|
|
|
content_length = (continue_needed) ? 0 : 0x40000000;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
s = rpc_ntlm_http_request(rpc, &ntlm->outputBuffer[0], content_length, TSG_CHANNEL_IN);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2012-12-14 05:23:37 +04:00
|
|
|
DEBUG_RPC("\n%s", Stream_Buffer(s));
|
|
|
|
rpc_in_write(rpc, Stream_Buffer(s), Stream_Length(s));
|
|
|
|
Stream_Free(s, TRUE);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_ncacn_http_recv_in_channel_response(rdpRpc* rpc)
|
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
char* token64;
|
2014-04-19 01:08:34 +04:00
|
|
|
int ntlm_token_length = 0;
|
|
|
|
BYTE* ntlm_token_data = NULL;
|
2012-11-28 07:03:05 +04:00
|
|
|
HttpResponse* http_response;
|
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpIn->ntlm;
|
|
|
|
|
|
|
|
http_response = http_response_recv(rpc->TlsIn);
|
2014-12-12 01:08:22 +03:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!http_response)
|
|
|
|
return -1;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (ListDictionary_Contains(http_response->Authenticates, "NTLM"))
|
2013-12-13 19:11:36 +04:00
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
token64 = ListDictionary_GetItemValue(http_response->Authenticates, "NTLM");
|
|
|
|
|
2014-04-19 01:08:34 +04:00
|
|
|
if (!token64)
|
|
|
|
goto out;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2014-05-12 00:42:01 +04:00
|
|
|
crypto_base64_decode(token64, strlen(token64), &ntlm_token_data, &ntlm_token_length);
|
2013-12-13 19:11:36 +04:00
|
|
|
}
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
out:
|
2014-04-19 01:08:34 +04:00
|
|
|
ntlm->inputBuffer[0].pvBuffer = ntlm_token_data;
|
|
|
|
ntlm->inputBuffer[0].cbBuffer = ntlm_token_length;
|
2012-11-28 07:03:05 +04:00
|
|
|
http_response_free(http_response);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_ncacn_http_ntlm_init(rdpRpc* rpc, TSG_CHANNEL channel)
|
|
|
|
{
|
|
|
|
rdpNtlm* ntlm = NULL;
|
2013-12-07 07:15:45 +04:00
|
|
|
rdpSettings* settings = rpc->settings;
|
|
|
|
freerdp* instance = (freerdp*) rpc->settings->instance;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
if (channel == TSG_CHANNEL_IN)
|
|
|
|
ntlm = rpc->NtlmHttpIn->ntlm;
|
|
|
|
else if (channel == TSG_CHANNEL_OUT)
|
|
|
|
ntlm = rpc->NtlmHttpOut->ntlm;
|
|
|
|
|
2014-05-21 19:32:14 +04:00
|
|
|
if (!settings->GatewayPassword || !settings->GatewayUsername ||
|
|
|
|
!strlen(settings->GatewayPassword) || !strlen(settings->GatewayUsername))
|
2013-12-07 07:15:45 +04:00
|
|
|
{
|
|
|
|
if (instance->GatewayAuthenticate)
|
|
|
|
{
|
2014-05-21 19:32:14 +04:00
|
|
|
BOOL proceed = instance->GatewayAuthenticate(instance, &settings->GatewayUsername,
|
2014-12-12 01:08:22 +03:00
|
|
|
&settings->GatewayPassword, &settings->GatewayDomain);
|
2013-12-07 07:15:45 +04:00
|
|
|
|
|
|
|
if (!proceed)
|
2013-12-13 19:11:36 +04:00
|
|
|
{
|
|
|
|
connectErrorCode = CANCELEDBYUSER;
|
2014-03-21 21:45:43 +04:00
|
|
|
freerdp_set_last_error(instance->context, FREERDP_ERROR_CONNECT_CANCELLED);
|
2013-12-07 07:15:45 +04:00
|
|
|
return 0;
|
2013-12-13 19:11:36 +04:00
|
|
|
}
|
2013-12-07 07:15:45 +04:00
|
|
|
|
|
|
|
if (settings->GatewayUseSameCredentials)
|
|
|
|
{
|
|
|
|
settings->Username = _strdup(settings->GatewayUsername);
|
|
|
|
settings->Domain = _strdup(settings->GatewayDomain);
|
|
|
|
settings->Password = _strdup(settings->GatewayPassword);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
if (!ntlm_client_init(ntlm, TRUE, settings->GatewayUsername,
|
2013-01-09 09:20:08 +04:00
|
|
|
settings->GatewayDomain, settings->GatewayPassword,
|
2013-12-13 19:11:36 +04:00
|
|
|
rpc->TlsIn->Bindings))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
if (!ntlm_client_make_spn(ntlm, _T("HTTP"), settings->GatewayHostname))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
return 1;
|
2012-11-28 07:03:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rpc_ntlm_http_in_connect(rdpRpc* rpc)
|
|
|
|
{
|
2013-12-13 19:11:36 +04:00
|
|
|
BOOL success = FALSE;
|
2014-12-12 01:08:22 +03:00
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpIn->ntlm;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
if (rpc_ncacn_http_ntlm_init(rpc, TSG_CHANNEL_IN) == 1)
|
|
|
|
{
|
|
|
|
success = TRUE;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Send IN Channel Request */
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
rpc_ncacn_http_send_in_channel_request(rpc);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Receive IN Channel Response */
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
rpc_ncacn_http_recv_in_channel_response(rpc);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Send IN Channel Request */
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
rpc_ncacn_http_send_in_channel_request(rpc);
|
|
|
|
|
|
|
|
ntlm_client_uninit(ntlm);
|
|
|
|
}
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
ntlm_free(ntlm);
|
|
|
|
|
2012-12-12 09:49:15 +04:00
|
|
|
rpc->NtlmHttpIn->ntlm = NULL;
|
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
return success;
|
2012-11-28 07:03:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_ncacn_http_send_out_channel_request(rdpRpc* rpc)
|
|
|
|
{
|
2012-12-14 05:23:37 +04:00
|
|
|
wStream* s;
|
2012-11-28 07:03:05 +04:00
|
|
|
int content_length;
|
|
|
|
BOOL continue_needed;
|
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpOut->ntlm;
|
|
|
|
|
|
|
|
continue_needed = ntlm_authenticate(ntlm);
|
|
|
|
|
|
|
|
content_length = (continue_needed) ? 0 : 76;
|
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
s = rpc_ntlm_http_request(rpc, &ntlm->outputBuffer[0], content_length, TSG_CHANNEL_OUT);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2012-12-14 05:23:37 +04:00
|
|
|
DEBUG_RPC("\n%s", Stream_Buffer(s));
|
|
|
|
rpc_out_write(rpc, Stream_Buffer(s), Stream_Length(s));
|
|
|
|
Stream_Free(s, TRUE);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rpc_ncacn_http_recv_out_channel_response(rdpRpc* rpc)
|
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
char* token64;
|
2013-12-07 07:15:45 +04:00
|
|
|
int ntlm_token_length = 0;
|
2014-12-11 19:25:34 +03:00
|
|
|
BYTE* ntlm_token_data = NULL;
|
2012-11-28 07:03:05 +04:00
|
|
|
HttpResponse* http_response;
|
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpOut->ntlm;
|
|
|
|
|
|
|
|
http_response = http_response_recv(rpc->TlsOut);
|
|
|
|
|
2014-12-12 01:08:22 +03:00
|
|
|
if (!http_response)
|
|
|
|
return -1;
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2014-12-12 01:08:22 +03:00
|
|
|
if (ListDictionary_Contains(http_response->Authenticates, "NTLM"))
|
2013-12-07 07:15:45 +04:00
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
token64 = ListDictionary_GetItemValue(http_response->Authenticates, "NTLM");
|
2014-05-12 00:42:01 +04:00
|
|
|
crypto_base64_decode(token64, strlen(token64), &ntlm_token_data, &ntlm_token_length);
|
2013-12-07 07:15:45 +04:00
|
|
|
}
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2013-01-09 09:20:08 +04:00
|
|
|
ntlm->inputBuffer[0].pvBuffer = ntlm_token_data;
|
|
|
|
ntlm->inputBuffer[0].cbBuffer = ntlm_token_length;
|
2014-05-21 19:32:14 +04:00
|
|
|
|
2012-11-28 07:03:05 +04:00
|
|
|
http_response_free(http_response);
|
2014-12-12 01:08:22 +03:00
|
|
|
|
2012-11-28 07:03:05 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL rpc_ntlm_http_out_connect(rdpRpc* rpc)
|
|
|
|
{
|
|
|
|
rdpNtlm* ntlm = rpc->NtlmHttpOut->ntlm;
|
2013-12-13 19:11:36 +04:00
|
|
|
BOOL success = FALSE;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
if (rpc_ncacn_http_ntlm_init(rpc, TSG_CHANNEL_OUT) == 1)
|
|
|
|
{
|
|
|
|
success = TRUE;
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Send OUT Channel Request */
|
|
|
|
rpc_ncacn_http_send_out_channel_request(rpc);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Receive OUT Channel Response */
|
|
|
|
rpc_ncacn_http_recv_out_channel_response(rpc);
|
2012-11-28 07:03:05 +04:00
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
/* Send OUT Channel Request */
|
|
|
|
rpc_ncacn_http_send_out_channel_request(rpc);
|
|
|
|
|
|
|
|
ntlm_client_uninit(ntlm);
|
|
|
|
}
|
2012-11-28 07:03:05 +04:00
|
|
|
|
|
|
|
ntlm_free(ntlm);
|
|
|
|
|
2013-12-13 19:11:36 +04:00
|
|
|
rpc->NtlmHttpOut->ntlm = NULL;
|
|
|
|
|
|
|
|
return success;
|
2012-11-28 07:03:05 +04:00
|
|
|
}
|
|
|
|
|
2012-11-28 07:26:57 +04:00
|
|
|
void rpc_ntlm_http_init_channel(rdpRpc* rpc, rdpNtlmHttp* ntlm_http, TSG_CHANNEL channel)
|
|
|
|
{
|
|
|
|
if (channel == TSG_CHANNEL_IN)
|
|
|
|
http_context_set_method(ntlm_http->context, "RPC_IN_DATA");
|
|
|
|
else if (channel == TSG_CHANNEL_OUT)
|
|
|
|
http_context_set_method(ntlm_http->context, "RPC_OUT_DATA");
|
|
|
|
|
|
|
|
http_context_set_uri(ntlm_http->context, "/rpc/rpcproxy.dll?localhost:3388");
|
|
|
|
http_context_set_accept(ntlm_http->context, "application/rpc");
|
|
|
|
http_context_set_cache_control(ntlm_http->context, "no-cache");
|
|
|
|
http_context_set_connection(ntlm_http->context, "Keep-Alive");
|
|
|
|
http_context_set_user_agent(ntlm_http->context, "MSRPC");
|
|
|
|
http_context_set_host(ntlm_http->context, rpc->settings->GatewayHostname);
|
|
|
|
|
|
|
|
if (channel == TSG_CHANNEL_IN)
|
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
http_context_set_pragma(ntlm_http->context, "ResourceTypeUuid=44e265dd-7daf-42cd-8560-3cdb6e7a2729");
|
2012-11-28 07:26:57 +04:00
|
|
|
}
|
|
|
|
else if (channel == TSG_CHANNEL_OUT)
|
|
|
|
{
|
2014-12-12 01:08:22 +03:00
|
|
|
http_context_set_pragma(ntlm_http->context, "ResourceTypeUuid=44e265dd-7daf-42cd-8560-3cdb6e7a2729, "
|
2012-11-28 07:26:57 +04:00
|
|
|
"SessionId=fbd9c34f-397d-471d-a109-1b08cc554624");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rdpNtlmHttp* ntlm_http_new()
|
|
|
|
{
|
|
|
|
rdpNtlmHttp* ntlm_http;
|
|
|
|
|
2014-12-11 19:25:34 +03:00
|
|
|
ntlm_http = (rdpNtlmHttp*) calloc(1, sizeof(rdpNtlmHttp));
|
2012-11-28 07:26:57 +04:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!ntlm_http)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ntlm_http->ntlm = ntlm_new();
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!ntlm_http->ntlm)
|
|
|
|
goto out_free;
|
|
|
|
|
|
|
|
ntlm_http->context = http_context_new();
|
2014-12-11 19:25:34 +03:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!ntlm_http->context)
|
|
|
|
goto out_free_ntlm;
|
2012-11-28 07:26:57 +04:00
|
|
|
|
|
|
|
return ntlm_http;
|
2014-04-09 18:07:06 +04:00
|
|
|
|
|
|
|
out_free_ntlm:
|
|
|
|
ntlm_free(ntlm_http->ntlm);
|
|
|
|
out_free:
|
2014-04-26 08:08:30 +04:00
|
|
|
free(ntlm_http);
|
2014-04-09 18:07:06 +04:00
|
|
|
return NULL;
|
2012-11-28 07:26:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ntlm_http_free(rdpNtlmHttp* ntlm_http)
|
|
|
|
{
|
2014-04-09 18:07:06 +04:00
|
|
|
if (!ntlm_http)
|
|
|
|
return;
|
2012-12-13 00:55:42 +04:00
|
|
|
|
2014-04-09 18:07:06 +04:00
|
|
|
ntlm_free(ntlm_http->ntlm);
|
|
|
|
http_context_free(ntlm_http->context);
|
|
|
|
|
|
|
|
free(ntlm_http);
|
2012-11-28 07:26:57 +04:00
|
|
|
}
|