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