1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/*
* Copyright 2014 Hans Leidekker for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <stdarg.h>
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_NETDB_H
# include <netdb.h>
#endif
#if defined(__MINGW32__) || defined (_MSC_VER)
# include <ws2tcpip.h>
#else
# define closesocket close
# define ioctlsocket ioctl
#endif
#include "windef.h"
#include "winbase.h"
#ifndef __MINGW32__
#define USE_WS_PREFIX
#endif
#include "winsock2.h"
#include "ws2ipdef.h"
#include "winnls.h"
#include "wininet.h"
#define COBJMACROS
#include "ole2.h"
#include "dispex.h"
#include "activscp.h"
#include "wine/debug.h"
#include "wine/heap.h"
#include "wine/unicode.h"
static HINSTANCE instance;
WINE_DEFAULT_DEBUG_CHANNEL(jsproxy);
static CRITICAL_SECTION cs_jsproxy;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &cs_jsproxy,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": cs_jsproxy") }
};
static CRITICAL_SECTION cs_jsproxy = { &critsect_debug, -1, 0, 0, 0, 0 };
static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
/******************************************************************
* DllMain (jsproxy.@)
*/
BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
instance = hinst;
DisableThreadLibraryCalls( hinst );
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
static inline WCHAR *strdupAW( const char *src, int len )
{
WCHAR *dst = NULL;
if (src)
{
int dst_len = MultiByteToWideChar( CP_ACP, 0, src, len, NULL, 0 );
if ((dst = heap_alloc( (dst_len + 1) * sizeof(WCHAR) )))
{
len = MultiByteToWideChar( CP_ACP, 0, src, len, dst, dst_len );
dst[dst_len] = 0;
}
}
return dst;
}
static inline char *strdupWA( const WCHAR *src )
{
char *dst = NULL;
if (src)
{
int len = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
if ((dst = heap_alloc( len ))) WideCharToMultiByte( CP_ACP, 0, src, -1, dst, len, NULL, NULL );
}
return dst;
}
static struct pac_script
{
WCHAR *text;
} pac_script;
static struct pac_script *global_script = &pac_script;
/******************************************************************
* InternetDeInitializeAutoProxyDll (jsproxy.@)
*/
BOOL WINAPI InternetDeInitializeAutoProxyDll( LPSTR mime, DWORD reserved )
{
TRACE( "%s, %u\n", debugstr_a(mime), reserved );
EnterCriticalSection( &cs_jsproxy );
heap_free( global_script->text );
global_script->text = NULL;
LeaveCriticalSection( &cs_jsproxy );
return TRUE;
}
static WCHAR *load_script( const char *filename )
{
HANDLE handle;
DWORD size, bytes_read;
char *buffer;
int len;
WCHAR *script = NULL;
handle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
if (handle == INVALID_HANDLE_VALUE) return NULL;
size = GetFileSize( handle, NULL );
if (!(buffer = heap_alloc( size ))) goto done;
if (!ReadFile( handle, buffer, size, &bytes_read, NULL ) || bytes_read != size) goto done;
len = MultiByteToWideChar( CP_ACP, 0, buffer, size, NULL, 0 );
if (!(script = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
MultiByteToWideChar( CP_ACP, 0, buffer, size, script, len );
script[len] = 0;
done:
CloseHandle( handle );
heap_free( buffer );
return script;
}
/******************************************************************
* InternetInitializeAutoProxyDll (jsproxy.@)
*/
BOOL WINAPI JSPROXY_InternetInitializeAutoProxyDll( DWORD version, LPSTR tmpfile, LPSTR mime,
AutoProxyHelperFunctions *callbacks,
LPAUTO_PROXY_SCRIPT_BUFFER buffer )
{
BOOL ret = FALSE;
TRACE( "%u, %s, %s, %p, %p\n", version, debugstr_a(tmpfile), debugstr_a(mime), callbacks, buffer );
if (callbacks) FIXME( "callbacks not supported\n" );
EnterCriticalSection( &cs_jsproxy );
if (buffer && buffer->dwStructSize == sizeof(*buffer) && buffer->lpszScriptBuffer)
{
if (!buffer->dwScriptBufferSize)
{
SetLastError( ERROR_INVALID_PARAMETER );
LeaveCriticalSection( &cs_jsproxy );
return FALSE;
}
heap_free( global_script->text );
if ((global_script->text = strdupAW( buffer->lpszScriptBuffer,
buffer->dwScriptBufferSize ))) ret = TRUE;
}
else
{
heap_free( global_script->text );
if ((global_script->text = load_script( tmpfile ))) ret = TRUE;
}
LeaveCriticalSection( &cs_jsproxy );
return ret;
}
static HRESULT WINAPI dispex_QueryInterface(
IDispatchEx *iface, REFIID riid, void **ppv )
{
*ppv = NULL;
if (IsEqualGUID( riid, &IID_IUnknown ) ||
IsEqualGUID( riid, &IID_IDispatch ) ||
IsEqualGUID( riid, &IID_IDispatchEx ))
*ppv = iface;
else
return E_NOINTERFACE;
return S_OK;
}
static ULONG WINAPI dispex_AddRef(
IDispatchEx *iface )
{
return 2;
}
static ULONG WINAPI dispex_Release(
IDispatchEx *iface )
{
return 1;
}
static HRESULT WINAPI dispex_GetTypeInfoCount(
IDispatchEx *iface, UINT *info )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetTypeInfo(
IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetIDsOfNames(
IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_Invoke(
IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_DeleteMemberByName(
IDispatchEx *iface, BSTR name, DWORD flags )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_DeleteMemberByDispID(
IDispatchEx *iface, DISPID id )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetMemberProperties(
IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetMemberName(
IDispatchEx *iface, DISPID id, BSTR *name )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetNextDispID(
IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
{
return E_NOTIMPL;
}
static HRESULT WINAPI dispex_GetNameSpaceParent(
IDispatchEx *iface, IUnknown **unk )
{
return E_NOTIMPL;
}
#define DISPID_GLOBAL_DNSRESOLVE 0x1000
static HRESULT WINAPI dispex_GetDispID(
IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
{
if (!strcmpW( name, dns_resolveW ))
{
*id = DISPID_GLOBAL_DNSRESOLVE;
return S_OK;
}
return DISP_E_UNKNOWNNAME;
}
static char *get_computer_name( COMPUTER_NAME_FORMAT format )
{
char *ret;
DWORD size = 0;
GetComputerNameExA( format, NULL, &size );
if (GetLastError() != ERROR_MORE_DATA) return NULL;
if (!(ret = heap_alloc( size ))) return NULL;
if (!GetComputerNameExA( format, ret, &size ))
{
heap_free( ret );
return NULL;
}
return ret;
}
static void printf_addr( const WCHAR *fmt, WCHAR *buf, struct sockaddr_in *addr )
{
sprintfW( buf, fmt,
(unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
(unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
(unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
(unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
}
static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
{
#ifdef HAVE_GETADDRINFO
static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
WCHAR addr[16];
struct addrinfo *ai, *elem;
char *hostnameA;
int res;
if (hostname[0])
hostnameA = strdupWA( hostname );
else
hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
if (!hostnameA) return E_OUTOFMEMORY;
res = getaddrinfo( hostnameA, NULL, NULL, &ai );
heap_free( hostnameA );
if (res) return S_FALSE;
elem = ai;
while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
if (!elem)
{
freeaddrinfo( ai );
return S_FALSE;
}
printf_addr( fmtW, addr, (struct sockaddr_in *)elem->ai_addr );
freeaddrinfo( ai );
V_VT( result ) = VT_BSTR;
V_BSTR( result ) = SysAllocString( addr );
return S_OK;
#else
FIXME("getaddrinfo not found at build time\n");
return S_FALSE;
#endif
}
static HRESULT WINAPI dispex_InvokeEx(
IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
{
if (id == DISPID_GLOBAL_DNSRESOLVE)
{
if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
if (V_VT(¶ms->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
return dns_resolve( V_BSTR(¶ms->rgvarg[0]), result );
}
return DISP_E_MEMBERNOTFOUND;
}
static const IDispatchExVtbl dispex_vtbl =
{
dispex_QueryInterface,
dispex_AddRef,
dispex_Release,
dispex_GetTypeInfoCount,
dispex_GetTypeInfo,
dispex_GetIDsOfNames,
dispex_Invoke,
dispex_GetDispID,
dispex_InvokeEx,
dispex_DeleteMemberByName,
dispex_DeleteMemberByDispID,
dispex_GetMemberProperties,
dispex_GetMemberName,
dispex_GetNextDispID,
dispex_GetNameSpaceParent
};
static IDispatchEx global_dispex = { &dispex_vtbl };
static HRESULT WINAPI site_QueryInterface(
IActiveScriptSite *iface, REFIID riid, void **ppv )
{
*ppv = NULL;
if (IsEqualGUID( &IID_IUnknown, riid ))
*ppv = iface;
else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
*ppv = iface;
else
return E_NOINTERFACE;
IUnknown_AddRef( (IUnknown *)*ppv );
return S_OK;
}
static ULONG WINAPI site_AddRef(
IActiveScriptSite *iface )
{
return 2;
}
static ULONG WINAPI site_Release(
IActiveScriptSite *iface )
{
return 1;
}
static HRESULT WINAPI site_GetLCID(
IActiveScriptSite *iface, LCID *lcid )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_GetItemInfo(
IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
IUnknown **item, ITypeInfo **type_info )
{
if (!strcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
{
*item = (IUnknown *)&global_dispex;
return S_OK;
}
return E_NOTIMPL;
}
static HRESULT WINAPI site_GetDocVersionString(
IActiveScriptSite *iface, BSTR *version )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_OnScriptTerminate(
IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_OnStateChange(
IActiveScriptSite *iface, SCRIPTSTATE state )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_OnScriptError(
IActiveScriptSite *iface, IActiveScriptError *error )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_OnEnterScript(
IActiveScriptSite *iface )
{
return E_NOTIMPL;
}
static HRESULT WINAPI site_OnLeaveScript(
IActiveScriptSite *iface )
{
return E_NOTIMPL;
}
static const IActiveScriptSiteVtbl site_vtbl =
{
site_QueryInterface,
site_AddRef,
site_Release,
site_GetLCID,
site_GetItemInfo,
site_GetDocVersionString,
site_OnScriptTerminate,
site_OnStateChange,
site_OnScriptError,
site_OnEnterScript,
site_OnLeaveScript
};
static IActiveScriptSite script_site = { &site_vtbl };
static BSTR include_pac_utils( const WCHAR *script )
{
static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
HMODULE hmod = GetModuleHandleA( "jsproxy.dll" );
HRSRC rsrc;
DWORD size;
const char *data;
BSTR ret;
int len;
if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
size = SizeofResource( hmod, rsrc );
data = LoadResource( hmod, rsrc );
len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
if (!(ret = SysAllocStringLen( NULL, len + strlenW( script ) + 1 ))) return NULL;
MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
strcpyW( ret + len, script );
return ret;
}
#ifdef _WIN64
#define IActiveScriptParse_Release IActiveScriptParse64_Release
#define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
#define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
#else
#define IActiveScriptParse_Release IActiveScriptParse32_Release
#define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
#define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
#endif
static BOOL run_script( const WCHAR *script, const WCHAR *url, const WCHAR *hostname, char **result_str, DWORD *result_len )
{
static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
IActiveScriptParse *parser = NULL;
IActiveScript *engine = NULL;
IDispatch *dispatch = NULL;
BOOL ret = FALSE;
CLSID clsid;
DISPID dispid;
BSTR func = NULL, full_script = NULL;
VARIANT args[2], retval;
DISPPARAMS params;
HRESULT hr, init;
init = CoInitialize( NULL );
hr = CLSIDFromProgID( jscriptW, &clsid );
if (hr != S_OK) goto done;
hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
&IID_IActiveScript, (void **)&engine );
if (hr != S_OK) goto done;
hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
if (hr != S_OK) goto done;
hr = IActiveScriptParse_InitNew( parser );
if (hr != S_OK) goto done;
hr = IActiveScript_SetScriptSite( engine, &script_site );
if (hr != S_OK) goto done;
hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
if (hr != S_OK) goto done;
if (!(full_script = include_pac_utils( script ))) goto done;
hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
if (hr != S_OK) goto done;
hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
if (hr != S_OK) goto done;
hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
if (hr != S_OK) goto done;
if (!(func = SysAllocString( findproxyW ))) goto done;
hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
if (hr != S_OK) goto done;
V_VT( &args[0] ) = VT_BSTR;
V_BSTR( &args[0] ) = SysAllocString( hostname );
V_VT( &args[1] ) = VT_BSTR;
V_BSTR( &args[1] ) = SysAllocString( url );
params.rgvarg = args;
params.rgdispidNamedArgs = NULL;
params.cArgs = 2;
params.cNamedArgs = 0;
hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
¶ms, &retval, NULL, NULL );
VariantClear( &args[0] );
VariantClear( &args[1] );
if (hr != S_OK)
{
WARN("script failed 0x%08x\n", hr);
goto done;
}
if ((*result_str = strdupWA( V_BSTR( &retval ) )))
{
TRACE( "result: %s\n", debugstr_a(*result_str) );
*result_len = strlen( *result_str ) + 1;
ret = TRUE;
}
VariantClear( &retval );
done:
SysFreeString( full_script );
SysFreeString( func );
if (dispatch) IDispatch_Release( dispatch );
if (parser) IActiveScriptParse_Release( parser );
if (engine) IActiveScript_Release( engine );
if (SUCCEEDED( init )) CoUninitialize();
return ret;
}
/******************************************************************
* InternetGetProxyInfo (jsproxy.@)
*/
BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
LPDWORD len_proxy )
{
WCHAR *urlW = NULL, *hostnameW = NULL;
BOOL ret = FALSE;
TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url), len_url, hostname, len_hostname, proxy, len_proxy );
EnterCriticalSection( &cs_jsproxy );
if (!global_script->text)
{
SetLastError( ERROR_CAN_NOT_COMPLETE );
goto done;
}
if (hostname && len_hostname < strlen( hostname ))
{
SetLastError( ERROR_INSUFFICIENT_BUFFER );
goto done;
}
if (!(urlW = strdupAW( url, -1 ))) goto done;
if (hostname && !(hostnameW = strdupAW( hostname, -1 ))) goto done;
TRACE( "%s\n", debugstr_w(global_script->text) );
ret = run_script( global_script->text, urlW, hostnameW, proxy, len_proxy );
done:
heap_free( hostnameW );
heap_free( urlW );
LeaveCriticalSection( &cs_jsproxy );
return ret;
}
|