コード例 #1
0
ファイル: discovery.c プロジェクト: ccpgames/wine
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;
}
コード例 #2
0
ファイル: firewall.c プロジェクト: gvsurenderreddy/ipxwrapper
void add_self_to_firewall(void)
{
	if(!IsUserAnAdmin())
	{
		log_printf(LOG_ERROR, "Cannot add firewall exception, not running as an administrator");
		return;
	}
	
	CoInitialize(NULL);
	
	/* We need to go deeper. */
	
	INetFwMgr *fw_mgr;
	HRESULT err = CoCreateInstance(&CLSID_NetFwMgr_s, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwMgr_s, (void**)(&fw_mgr));
	if(err == S_OK)
	{
		INetFwPolicy *fw_policy;
		if((err = INetFwMgr_get_LocalPolicy(fw_mgr, &fw_policy)) == S_OK)
		{
			INetFwProfile *fw_profile;
			if((err = INetFwPolicy_get_CurrentProfile(fw_policy, &fw_profile)) == S_OK)
			{
				INetFwAuthorizedApplications *fw_apps;
				if((err = INetFwProfile_get_AuthorizedApplications(fw_profile, &fw_apps)) == S_OK)
				{
					/* Create an instance of INetFwAuthorizedApplication and
					 * put the current executable in it.
					*/
					
					INetFwAuthorizedApplication *this_exe;
					if((err = CoCreateInstance(&CLSID_NetFwAuthorizedApplication_s, NULL, CLSCTX_INPROC_SERVER, &IID_INetFwAuthorizedApplication_s, (void**)(&this_exe))) == S_OK)
					{
						if(_fill_this_exe(this_exe))
						{
							/* Add the new INetFwAuthorizedApplication
							 * to the active profile.
							*/
							
							if((err = INetFwAuthorizedApplications_Add(fw_apps, this_exe)) != S_OK)
							{
								log_printf(LOG_ERROR, "Could not add firewall exception (error %u)", (unsigned int)(err));
							}
						}
						
						INetFwAuthorizedApplication_Release(this_exe);
					}
					else{
						log_printf(LOG_ERROR, "Could not create INetFwAuthorizedApplication (error %u)", (unsigned int)(err));
					}
					
					INetFwAuthorizedApplications_Release(fw_apps);
				}
				else{
					log_printf(LOG_ERROR, "Could not get INetFwAuthorizedApplications object (error %u)", (unsigned int)(err));
				}
				
				INetFwProfile_Release(fw_profile);
			}
			else{
				log_printf(LOG_ERROR, "Could not get INetFwProfile object (error %u)", (unsigned int)(err));
			}
			
			INetFwPolicy_Release(fw_policy);
		}
		else{
			log_printf(LOG_ERROR, "Could not get INetFwPolicy object (error %u)", (unsigned int)(err));
		}
		
		INetFwMgr_Release(fw_mgr);
	}
	else{
		log_printf(LOG_ERROR, "Could not create INetFwMgr object (error %u)", (unsigned int)(err));
	}
	
	CoUninitialize();
}