bcrypt: Make format_gnutls_signature() static.
[wine.git] / dlls / jsproxy / main.c
blob905d2d93c169d7fabf618b8c7a8f628b58eca5fa
1 /*
2 * Copyright 2014 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
20 #include <sys/types.h>
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winsock2.h"
25 #include "ws2ipdef.h"
26 #include "ws2tcpip.h"
27 #include "winnls.h"
28 #include "wininet.h"
29 #define COBJMACROS
30 #include "ole2.h"
31 #include "dispex.h"
32 #include "activscp.h"
33 #include "wine/debug.h"
34 #include "wine/heap.h"
36 static HINSTANCE instance;
38 WINE_DEFAULT_DEBUG_CHANNEL(jsproxy);
40 static CRITICAL_SECTION cs_jsproxy;
41 static CRITICAL_SECTION_DEBUG critsect_debug =
43 0, 0, &cs_jsproxy,
44 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
45 0, 0, { (DWORD_PTR)(__FILE__ ": cs_jsproxy") }
47 static CRITICAL_SECTION cs_jsproxy = { &critsect_debug, -1, 0, 0, 0, 0 };
49 static const WCHAR global_funcsW[] = {'g','l','o','b','a','l','_','f','u','n','c','s',0};
50 static const WCHAR dns_resolveW[] = {'d','n','s','_','r','e','s','o','l','v','e',0};
52 /******************************************************************
53 * DllMain (jsproxy.@)
55 BOOL WINAPI DllMain( HINSTANCE hinst, DWORD reason, LPVOID reserved )
57 switch (reason)
59 case DLL_PROCESS_ATTACH:
60 instance = hinst;
61 DisableThreadLibraryCalls( hinst );
62 break;
64 case DLL_PROCESS_DETACH:
65 break;
67 return TRUE;
70 static inline WCHAR *strdupAW( const char *src, int len )
72 WCHAR *dst = NULL;
73 if (src)
75 int dst_len = MultiByteToWideChar( CP_ACP, 0, src, len, NULL, 0 );
76 if ((dst = heap_alloc( (dst_len + 1) * sizeof(WCHAR) )))
78 len = MultiByteToWideChar( CP_ACP, 0, src, len, dst, dst_len );
79 dst[dst_len] = 0;
82 return dst;
85 static inline char *strdupWA( const WCHAR *src )
87 char *dst = NULL;
88 if (src)
90 int len = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
91 if ((dst = heap_alloc( len ))) WideCharToMultiByte( CP_ACP, 0, src, -1, dst, len, NULL, NULL );
93 return dst;
96 static struct pac_script
98 WCHAR *text;
99 } pac_script;
100 static struct pac_script *global_script = &pac_script;
102 /******************************************************************
103 * InternetDeInitializeAutoProxyDll (jsproxy.@)
105 BOOL WINAPI InternetDeInitializeAutoProxyDll( LPSTR mime, DWORD reserved )
107 TRACE( "%s, %u\n", debugstr_a(mime), reserved );
109 EnterCriticalSection( &cs_jsproxy );
111 heap_free( global_script->text );
112 global_script->text = NULL;
114 LeaveCriticalSection( &cs_jsproxy );
115 return TRUE;
118 static WCHAR *load_script( const char *filename )
120 HANDLE handle;
121 DWORD size, bytes_read;
122 char *buffer;
123 int len;
124 WCHAR *script = NULL;
126 handle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0 );
127 if (handle == INVALID_HANDLE_VALUE) return NULL;
129 size = GetFileSize( handle, NULL );
130 if (!(buffer = heap_alloc( size ))) goto done;
131 if (!ReadFile( handle, buffer, size, &bytes_read, NULL ) || bytes_read != size) goto done;
133 len = MultiByteToWideChar( CP_ACP, 0, buffer, size, NULL, 0 );
134 if (!(script = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
135 MultiByteToWideChar( CP_ACP, 0, buffer, size, script, len );
136 script[len] = 0;
138 done:
139 CloseHandle( handle );
140 heap_free( buffer );
141 return script;
144 /******************************************************************
145 * InternetInitializeAutoProxyDll (jsproxy.@)
147 BOOL WINAPI JSPROXY_InternetInitializeAutoProxyDll( DWORD version, LPSTR tmpfile, LPSTR mime,
148 AutoProxyHelperFunctions *callbacks,
149 LPAUTO_PROXY_SCRIPT_BUFFER buffer )
151 BOOL ret = FALSE;
153 TRACE( "%u, %s, %s, %p, %p\n", version, debugstr_a(tmpfile), debugstr_a(mime), callbacks, buffer );
155 if (callbacks) FIXME( "callbacks not supported\n" );
157 EnterCriticalSection( &cs_jsproxy );
159 if (buffer && buffer->dwStructSize == sizeof(*buffer) && buffer->lpszScriptBuffer)
161 if (!buffer->dwScriptBufferSize)
163 SetLastError( ERROR_INVALID_PARAMETER );
164 LeaveCriticalSection( &cs_jsproxy );
165 return FALSE;
167 heap_free( global_script->text );
168 if ((global_script->text = strdupAW( buffer->lpszScriptBuffer,
169 buffer->dwScriptBufferSize ))) ret = TRUE;
171 else
173 heap_free( global_script->text );
174 if ((global_script->text = load_script( tmpfile ))) ret = TRUE;
177 LeaveCriticalSection( &cs_jsproxy );
178 return ret;
181 static HRESULT WINAPI dispex_QueryInterface(
182 IDispatchEx *iface, REFIID riid, void **ppv )
184 *ppv = NULL;
186 if (IsEqualGUID( riid, &IID_IUnknown ) ||
187 IsEqualGUID( riid, &IID_IDispatch ) ||
188 IsEqualGUID( riid, &IID_IDispatchEx ))
189 *ppv = iface;
190 else
191 return E_NOINTERFACE;
193 return S_OK;
196 static ULONG WINAPI dispex_AddRef(
197 IDispatchEx *iface )
199 return 2;
202 static ULONG WINAPI dispex_Release(
203 IDispatchEx *iface )
205 return 1;
208 static HRESULT WINAPI dispex_GetTypeInfoCount(
209 IDispatchEx *iface, UINT *info )
211 return E_NOTIMPL;
214 static HRESULT WINAPI dispex_GetTypeInfo(
215 IDispatchEx *iface, UINT info, LCID lcid, ITypeInfo **type_info )
217 return E_NOTIMPL;
220 static HRESULT WINAPI dispex_GetIDsOfNames(
221 IDispatchEx *iface, REFIID riid, LPOLESTR *names, UINT count, LCID lcid, DISPID *id )
223 return E_NOTIMPL;
226 static HRESULT WINAPI dispex_Invoke(
227 IDispatchEx *iface, DISPID member, REFIID riid, LCID lcid, WORD flags,
228 DISPPARAMS *params, VARIANT *result, EXCEPINFO *excep, UINT *err )
230 return E_NOTIMPL;
233 static HRESULT WINAPI dispex_DeleteMemberByName(
234 IDispatchEx *iface, BSTR name, DWORD flags )
236 return E_NOTIMPL;
239 static HRESULT WINAPI dispex_DeleteMemberByDispID(
240 IDispatchEx *iface, DISPID id )
242 return E_NOTIMPL;
245 static HRESULT WINAPI dispex_GetMemberProperties(
246 IDispatchEx *iface, DISPID id, DWORD flags_fetch, DWORD *flags )
248 return E_NOTIMPL;
251 static HRESULT WINAPI dispex_GetMemberName(
252 IDispatchEx *iface, DISPID id, BSTR *name )
254 return E_NOTIMPL;
257 static HRESULT WINAPI dispex_GetNextDispID(
258 IDispatchEx *iface, DWORD flags, DISPID id, DISPID *next )
260 return E_NOTIMPL;
263 static HRESULT WINAPI dispex_GetNameSpaceParent(
264 IDispatchEx *iface, IUnknown **unk )
266 return E_NOTIMPL;
269 #define DISPID_GLOBAL_DNSRESOLVE 0x1000
271 static HRESULT WINAPI dispex_GetDispID(
272 IDispatchEx *iface, BSTR name, DWORD flags, DISPID *id )
274 if (!lstrcmpW( name, dns_resolveW ))
276 *id = DISPID_GLOBAL_DNSRESOLVE;
277 return S_OK;
279 return DISP_E_UNKNOWNNAME;
282 static char *get_computer_name( COMPUTER_NAME_FORMAT format )
284 char *ret;
285 DWORD size = 0;
287 GetComputerNameExA( format, NULL, &size );
288 if (GetLastError() != ERROR_MORE_DATA) return NULL;
289 if (!(ret = heap_alloc( size ))) return NULL;
290 if (!GetComputerNameExA( format, ret, &size ))
292 heap_free( ret );
293 return NULL;
295 return ret;
298 static void printf_addr( const WCHAR *fmt, WCHAR *buf, SIZE_T size, struct sockaddr_in *addr )
300 swprintf( buf, size, fmt,
301 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 24 & 0xff),
302 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 16 & 0xff),
303 (unsigned int)(ntohl( addr->sin_addr.s_addr ) >> 8 & 0xff),
304 (unsigned int)(ntohl( addr->sin_addr.s_addr ) & 0xff) );
307 static HRESULT dns_resolve( const WCHAR *hostname, VARIANT *result )
309 static const WCHAR fmtW[] = {'%','u','.','%','u','.','%','u','.','%','u',0};
310 WCHAR addr[16];
311 struct addrinfo *ai, *elem;
312 char *hostnameA;
313 int res;
315 if (hostname[0])
316 hostnameA = strdupWA( hostname );
317 else
318 hostnameA = get_computer_name( ComputerNamePhysicalDnsFullyQualified );
320 if (!hostnameA) return E_OUTOFMEMORY;
321 res = getaddrinfo( hostnameA, NULL, NULL, &ai );
322 heap_free( hostnameA );
323 if (res) return S_FALSE;
325 elem = ai;
326 while (elem && elem->ai_family != AF_INET) elem = elem->ai_next;
327 if (!elem)
329 freeaddrinfo( ai );
330 return S_FALSE;
332 printf_addr( fmtW, addr, ARRAY_SIZE(addr), (struct sockaddr_in *)elem->ai_addr );
333 freeaddrinfo( ai );
334 V_VT( result ) = VT_BSTR;
335 V_BSTR( result ) = SysAllocString( addr );
336 return S_OK;
339 static HRESULT WINAPI dispex_InvokeEx(
340 IDispatchEx *iface, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
341 VARIANT *result, EXCEPINFO *exep, IServiceProvider *caller )
343 if (id == DISPID_GLOBAL_DNSRESOLVE)
345 if (params->cArgs != 1) return DISP_E_BADPARAMCOUNT;
346 if (V_VT(&params->rgvarg[0]) != VT_BSTR) return DISP_E_BADVARTYPE;
347 return dns_resolve( V_BSTR(&params->rgvarg[0]), result );
349 return DISP_E_MEMBERNOTFOUND;
352 static const IDispatchExVtbl dispex_vtbl =
354 dispex_QueryInterface,
355 dispex_AddRef,
356 dispex_Release,
357 dispex_GetTypeInfoCount,
358 dispex_GetTypeInfo,
359 dispex_GetIDsOfNames,
360 dispex_Invoke,
361 dispex_GetDispID,
362 dispex_InvokeEx,
363 dispex_DeleteMemberByName,
364 dispex_DeleteMemberByDispID,
365 dispex_GetMemberProperties,
366 dispex_GetMemberName,
367 dispex_GetNextDispID,
368 dispex_GetNameSpaceParent
371 static IDispatchEx global_dispex = { &dispex_vtbl };
373 static HRESULT WINAPI site_QueryInterface(
374 IActiveScriptSite *iface, REFIID riid, void **ppv )
376 *ppv = NULL;
378 if (IsEqualGUID( &IID_IUnknown, riid ))
379 *ppv = iface;
380 else if (IsEqualGUID( &IID_IActiveScriptSite, riid ))
381 *ppv = iface;
382 else
383 return E_NOINTERFACE;
385 IUnknown_AddRef( (IUnknown *)*ppv );
386 return S_OK;
389 static ULONG WINAPI site_AddRef(
390 IActiveScriptSite *iface )
392 return 2;
395 static ULONG WINAPI site_Release(
396 IActiveScriptSite *iface )
398 return 1;
401 static HRESULT WINAPI site_GetLCID(
402 IActiveScriptSite *iface, LCID *lcid )
404 return E_NOTIMPL;
407 static HRESULT WINAPI site_GetItemInfo(
408 IActiveScriptSite *iface, LPCOLESTR name, DWORD mask,
409 IUnknown **item, ITypeInfo **type_info )
411 if (!lstrcmpW( name, global_funcsW ) && mask == SCRIPTINFO_IUNKNOWN)
413 *item = (IUnknown *)&global_dispex;
414 return S_OK;
416 return E_NOTIMPL;
419 static HRESULT WINAPI site_GetDocVersionString(
420 IActiveScriptSite *iface, BSTR *version )
422 return E_NOTIMPL;
425 static HRESULT WINAPI site_OnScriptTerminate(
426 IActiveScriptSite *iface, const VARIANT *result, const EXCEPINFO *info )
428 return E_NOTIMPL;
431 static HRESULT WINAPI site_OnStateChange(
432 IActiveScriptSite *iface, SCRIPTSTATE state )
434 return E_NOTIMPL;
437 static HRESULT WINAPI site_OnScriptError(
438 IActiveScriptSite *iface, IActiveScriptError *error )
440 return E_NOTIMPL;
443 static HRESULT WINAPI site_OnEnterScript(
444 IActiveScriptSite *iface )
446 return E_NOTIMPL;
449 static HRESULT WINAPI site_OnLeaveScript(
450 IActiveScriptSite *iface )
452 return E_NOTIMPL;
455 static const IActiveScriptSiteVtbl site_vtbl =
457 site_QueryInterface,
458 site_AddRef,
459 site_Release,
460 site_GetLCID,
461 site_GetItemInfo,
462 site_GetDocVersionString,
463 site_OnScriptTerminate,
464 site_OnStateChange,
465 site_OnScriptError,
466 site_OnEnterScript,
467 site_OnLeaveScript
470 static IActiveScriptSite script_site = { &site_vtbl };
472 static BSTR include_pac_utils( const WCHAR *script )
474 static const WCHAR pacjsW[] = {'p','a','c','.','j','s',0};
475 HMODULE hmod = GetModuleHandleA( "jsproxy.dll" );
476 HRSRC rsrc;
477 DWORD size;
478 const char *data;
479 BSTR ret;
480 int len;
482 if (!(rsrc = FindResourceW( hmod, pacjsW, (LPCWSTR)40 ))) return NULL;
483 size = SizeofResource( hmod, rsrc );
484 data = LoadResource( hmod, rsrc );
486 len = MultiByteToWideChar( CP_ACP, 0, data, size, NULL, 0 );
487 if (!(ret = SysAllocStringLen( NULL, len + lstrlenW( script ) + 1 ))) return NULL;
488 MultiByteToWideChar( CP_ACP, 0, data, size, ret, len );
489 lstrcpyW( ret + len, script );
490 return ret;
493 #ifdef _WIN64
494 #define IActiveScriptParse_Release IActiveScriptParse64_Release
495 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
496 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
497 #else
498 #define IActiveScriptParse_Release IActiveScriptParse32_Release
499 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
500 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
501 #endif
503 static BOOL run_script( const WCHAR *script, const WCHAR *url, const WCHAR *hostname, char **result_str, DWORD *result_len )
505 static const WCHAR jscriptW[] = {'J','S','c','r','i','p','t',0};
506 static const WCHAR findproxyW[] = {'F','i','n','d','P','r','o','x','y','F','o','r','U','R','L',0};
507 IActiveScriptParse *parser = NULL;
508 IActiveScript *engine = NULL;
509 IDispatch *dispatch = NULL;
510 BOOL ret = FALSE;
511 CLSID clsid;
512 DISPID dispid;
513 BSTR func = NULL, full_script = NULL;
514 VARIANT args[2], retval;
515 DISPPARAMS params;
516 HRESULT hr, init;
518 init = CoInitialize( NULL );
519 hr = CLSIDFromProgID( jscriptW, &clsid );
520 if (hr != S_OK) goto done;
522 hr = CoCreateInstance( &clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
523 &IID_IActiveScript, (void **)&engine );
524 if (hr != S_OK) goto done;
526 hr = IActiveScript_QueryInterface( engine, &IID_IActiveScriptParse, (void **)&parser );
527 if (hr != S_OK) goto done;
529 hr = IActiveScriptParse_InitNew( parser );
530 if (hr != S_OK) goto done;
532 hr = IActiveScript_SetScriptSite( engine, &script_site );
533 if (hr != S_OK) goto done;
535 hr = IActiveScript_AddNamedItem( engine, global_funcsW, SCRIPTITEM_GLOBALMEMBERS );
536 if (hr != S_OK) goto done;
538 if (!(full_script = include_pac_utils( script ))) goto done;
540 hr = IActiveScriptParse_ParseScriptText( parser, full_script, NULL, NULL, NULL, 0, 0, 0, NULL, NULL );
541 if (hr != S_OK) goto done;
543 hr = IActiveScript_SetScriptState( engine, SCRIPTSTATE_STARTED );
544 if (hr != S_OK) goto done;
546 hr = IActiveScript_GetScriptDispatch( engine, NULL, &dispatch );
547 if (hr != S_OK) goto done;
549 if (!(func = SysAllocString( findproxyW ))) goto done;
550 hr = IDispatch_GetIDsOfNames( dispatch, &IID_NULL, &func, 1, LOCALE_SYSTEM_DEFAULT, &dispid );
551 if (hr != S_OK) goto done;
553 V_VT( &args[0] ) = VT_BSTR;
554 V_BSTR( &args[0] ) = SysAllocString( hostname );
555 V_VT( &args[1] ) = VT_BSTR;
556 V_BSTR( &args[1] ) = SysAllocString( url );
558 params.rgvarg = args;
559 params.rgdispidNamedArgs = NULL;
560 params.cArgs = 2;
561 params.cNamedArgs = 0;
562 hr = IDispatch_Invoke( dispatch, dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD,
563 &params, &retval, NULL, NULL );
564 VariantClear( &args[0] );
565 VariantClear( &args[1] );
566 if (hr != S_OK)
568 WARN("script failed 0x%08x\n", hr);
569 goto done;
571 if ((*result_str = strdupWA( V_BSTR( &retval ) )))
573 TRACE( "result: %s\n", debugstr_a(*result_str) );
574 *result_len = strlen( *result_str ) + 1;
575 ret = TRUE;
577 VariantClear( &retval );
579 done:
580 SysFreeString( full_script );
581 SysFreeString( func );
582 if (dispatch) IDispatch_Release( dispatch );
583 if (parser) IActiveScriptParse_Release( parser );
584 if (engine) IActiveScript_Release( engine );
585 if (SUCCEEDED( init )) CoUninitialize();
586 return ret;
589 /******************************************************************
590 * InternetGetProxyInfo (jsproxy.@)
592 BOOL WINAPI InternetGetProxyInfo( LPCSTR url, DWORD len_url, LPCSTR hostname, DWORD len_hostname, LPSTR *proxy,
593 LPDWORD len_proxy )
595 WCHAR *urlW = NULL, *hostnameW = NULL;
596 BOOL ret = FALSE;
598 TRACE( "%s, %u, %s, %u, %p, %p\n", debugstr_a(url), len_url, hostname, len_hostname, proxy, len_proxy );
600 EnterCriticalSection( &cs_jsproxy );
602 if (!global_script->text)
604 SetLastError( ERROR_CAN_NOT_COMPLETE );
605 goto done;
607 if (hostname && len_hostname < strlen( hostname ))
609 SetLastError( ERROR_INSUFFICIENT_BUFFER );
610 goto done;
612 if (!(urlW = strdupAW( url, -1 ))) goto done;
613 if (hostname && !(hostnameW = strdupAW( hostname, -1 ))) goto done;
615 TRACE( "%s\n", debugstr_w(global_script->text) );
616 ret = run_script( global_script->text, urlW, hostnameW, proxy, len_proxy );
618 done:
619 heap_free( hostnameW );
620 heap_free( urlW );
621 LeaveCriticalSection( &cs_jsproxy );
622 return ret;