static bool _fill_this_exe(INetFwAuthorizedApplication *this_exe) { size_t size = 256; wchar_t *path = NULL; do { size *= 2; if(!(path = (wchar_t*)(realloc(path, sizeof(wchar_t) * size)))) { log_printf(LOG_ERROR, "Cannot allocate %u bytes for filename!", (unsigned int)(size)); return false; } } while(GetModuleFileNameW(NULL, path, size) == size); BSTR exe_path = SysAllocString(path); BSTR exe_desc = _get_exe_desc(path); bool ok = exe_path && (INetFwAuthorizedApplication_put_ProcessImageFileName(this_exe, exe_path) == S_OK) && (INetFwAuthorizedApplication_put_Name(this_exe, exe_desc ? exe_desc : exe_path) == S_OK); SysFreeString(exe_desc); SysFreeString(exe_path); free(path); if(!ok) { log_printf(LOG_ERROR, "Unknown error while populating INetFwAuthorizedApplication"); } return ok; }
static HRESULT set_firewall( enum firewall_op op ) { static const WCHAR testW[] = {'w','s','d','a','p','i','_','t','e','s','t',0}; HRESULT hr, init; INetFwMgr *mgr = NULL; INetFwPolicy *policy = NULL; INetFwProfile *profile = NULL; INetFwAuthorizedApplication *app = NULL; INetFwAuthorizedApplications *apps = NULL; BSTR name, image = SysAllocStringLen( NULL, MAX_PATH ); if (!GetModuleFileNameW( NULL, image, MAX_PATH )) { SysFreeString( image ); return E_FAIL; } init = CoInitializeEx( 0, COINIT_APARTMENTTHREADED ); hr = CoCreateInstance( &CLSID_NetFwMgr, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr, (void **)&mgr ); ok( hr == S_OK, "got %08x\n", hr ); if (hr != S_OK) goto done; hr = INetFwMgr_get_LocalPolicy( mgr, &policy ); ok( hr == S_OK, "got %08x\n", hr ); if (hr != S_OK) goto done; hr = INetFwPolicy_get_CurrentProfile( policy, &profile ); if (hr != S_OK) goto done; hr = INetFwProfile_get_AuthorizedApplications( profile, &apps ); ok( hr == S_OK, "got %08x\n", hr ); if (hr != S_OK) goto done; hr = CoCreateInstance( &CLSID_NetFwAuthorizedApplication, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwAuthorizedApplication, (void **)&app ); ok( hr == S_OK, "got %08x\n", hr ); if (hr != S_OK) goto done; hr = INetFwAuthorizedApplication_put_ProcessImageFileName( app, image ); if (hr != S_OK) goto done; name = SysAllocString( testW ); hr = INetFwAuthorizedApplication_put_Name( app, name ); SysFreeString( name ); ok( hr == S_OK, "got %08x\n", hr ); if (hr != S_OK) goto done; if (op == APP_ADD) hr = INetFwAuthorizedApplications_Add( apps, app ); else if (op == APP_REMOVE) hr = INetFwAuthorizedApplications_Remove( apps, image ); else hr = E_INVALIDARG; done: if (app) INetFwAuthorizedApplication_Release( app ); if (apps) INetFwAuthorizedApplications_Release( apps ); if (policy) INetFwPolicy_Release( policy ); if (profile) INetFwProfile_Release( profile ); if (mgr) INetFwMgr_Release( mgr ); if (SUCCEEDED( init )) CoUninitialize(); SysFreeString( image ); return hr; }