/****************************************************************** AddPortException ********************************************************************/ static HRESULT AddPortException( __in LPCWSTR wzName, __in int iProfile, __in_opt LPCWSTR wzRemoteAddresses, __in BOOL fIgnoreFailures, __in LPCWSTR wzPort, __in int iProtocol, __in LPCWSTR wzDescription ) { HRESULT hr = S_OK; BSTR bstrName = NULL; INetFwRules* pNetFwRules = NULL; INetFwRule* pNetFwRule = NULL; // convert to BSTRs to make COM happy bstrName = ::SysAllocString(wzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for name"); // get the collection of firewall rules hr = GetFirewallRules(fIgnoreFailures, &pNetFwRules); ExitOnFailure(hr, "failed to get firewall rules object"); if (S_FALSE == hr) // user or package author chose to ignore missing firewall { ExitFunction(); } // try to find it (i.e., support reinstall) hr = pNetFwRules->Item(bstrName, &pNetFwRule); if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr) { hr = CreateFwRuleObject(bstrName, iProfile, wzRemoteAddresses, wzPort, iProtocol, wzDescription, &pNetFwRule); ExitOnFailure(hr, "failed to create FwRule object"); // enable it hr = pNetFwRule->put_Enabled(VARIANT_TRUE); ExitOnFailure(hr, "failed to to enable port exception"); // add it to the list of authorized ports hr = pNetFwRules->Add(pNetFwRule); ExitOnFailure(hr, "failed to add app to the authorized ports list"); } else { // we found an existing port exception (if we succeeded, that is) ExitOnFailure(hr, "failed trying to find existing port rule"); // enable it (just in case it was disabled) pNetFwRule->put_Enabled(VARIANT_TRUE); } LExit: ReleaseBSTR(bstrName); ReleaseObject(pNetFwRules); ReleaseObject(pNetFwRule); return fIgnoreFailures ? S_OK : hr; }
/****************************************************************** RemoveException - Removes the exception rule with the given name. ********************************************************************/ static HRESULT RemoveException( __in LPCWSTR wzName, __in BOOL fIgnoreFailures ) { HRESULT hr = S_OK;; INetFwRules* pNetFwRules = NULL; // convert to BSTRs to make COM happy BSTR bstrName = ::SysAllocString(wzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for path"); // get the collection of firewall rules hr = GetFirewallRules(fIgnoreFailures, &pNetFwRules); ExitOnFailure(hr, "failed to get firewall rules object"); if (S_FALSE == hr) // user or package author chose to ignore missing firewall { ExitFunction(); } hr = pNetFwRules->Remove(bstrName); ExitOnFailure(hr, "failed to remove authorized app"); LExit: ReleaseBSTR(bstrName); ReleaseObject(pNetFwRules); return fIgnoreFailures ? S_OK : hr; }
/****************************************************************** RemoveApplicationExceptionFromCurrentProfile ********************************************************************/ static HRESULT RemoveApplicationExceptionFromCurrentProfile( __in LPCWSTR wzFile, __in BOOL fIgnoreFailures ) { HRESULT hr = S_OK; INetFwProfile* pfwProfile = NULL; INetFwAuthorizedApplications* pfwApps = NULL; // convert to BSTRs to make COM happy BSTR bstrFile = ::SysAllocString(wzFile); ExitOnNull(bstrFile, hr, E_OUTOFMEMORY, "failed SysAllocString for path"); // get the firewall profile, which is our entry point for removing exceptions hr = GetCurrentFirewallProfile(fIgnoreFailures, &pfwProfile); ExitOnFailure(hr, "failed to get firewall profile"); if (S_FALSE == hr) // user or package author chose to ignore missing firewall { ExitFunction(); } // now get the list of app exceptions and remove the one hr = pfwProfile->get_AuthorizedApplications(&pfwApps); ExitOnFailure(hr, "failed to get list of authorized apps"); hr = pfwApps->Remove(bstrFile); ExitOnFailure(hr, "failed to remove authorized app"); LExit: ReleaseBSTR(bstrFile); ReleaseObject(pfwApps); ReleaseObject(pfwProfile); return fIgnoreFailures ? S_OK : hr; }
HRESULT CpiPutCollectionObjectValue( ICatalogObject* piObj, LPCWSTR pwzPropName, LPCWSTR pwzValue ) { HRESULT hr = S_OK; BSTR bstrPropName = NULL; VARIANT vtVal; ::VariantInit(&vtVal); // allocate property name string bstrPropName = ::SysAllocString(pwzPropName); ExitOnNull(bstrPropName, hr, E_OUTOFMEMORY, "Failed to allocate property name string"); // prepare value variant vtVal.vt = VT_BSTR; vtVal.bstrVal = ::SysAllocString(pwzValue); ExitOnNull(vtVal.bstrVal, hr, E_OUTOFMEMORY, "Failed to allocate property value string"); // put value hr = piObj->put_Value(bstrPropName, vtVal); ExitOnFailure(hr, "Failed to put property value"); hr = S_OK; LExit: // clean up ReleaseBSTR(bstrPropName); ::VariantClear(&vtVal); return hr; }
extern "C" HRESULT DAPI Iis7GetPropertyVariant( __in IAppHostElement *pElement, __in LPCWSTR wzPropName, __in VARIANT* vtGet ) { HRESULT hr = S_OK; IAppHostProperty *pProperty = NULL; BSTR bstrPropName = NULL; bstrPropName = ::SysAllocString(wzPropName); ExitOnNull(bstrPropName, hr, E_OUTOFMEMORY, "failed SysAllocString"); hr = pElement->GetPropertyByName(bstrPropName, &pProperty); ExitOnFailure1(hr, "Failed to get property object for %ls", wzPropName); hr = pProperty->get_Value(vtGet); ExitOnFailure1(hr, "Failed to get property value for %ls", wzPropName); LExit: ReleaseBSTR(bstrPropName); // caller responsible for cleaning up variant vtGet ReleaseObject(pProperty); return hr; }
HRESULT CpiGetCatalogCollection( ICatalogCollection* piColl, ICatalogObject* piObj, LPCWSTR pwzName, ICatalogCollection** ppiColl ) { HRESULT hr = S_OK; ICOMAdminCatalog* piCatalog = NULL; IDispatch* piDisp = NULL; BSTR bstrName = NULL; VARIANT vtKey; ::VariantInit(&vtKey); // copy name string bstrName = ::SysAllocString(pwzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "Failed to allocate BSTR for collection name"); // get catalog hr = CpiGetAdminCatalog(&piCatalog); ExitOnFailure(hr, "Failed to get COM+ admin catalog"); // get key hr = piObj->get_Key(&vtKey); ExitOnFailure(hr, "Failed to get object key"); // get collecton from catalog hr = piColl->GetCollection(bstrName, vtKey, &piDisp); ExitOnFailure(hr, "Failed to get collection"); hr = piDisp->QueryInterface(IID_ICatalogCollection, (void**)ppiColl); ExitOnFailure(hr, "Failed to get IID_ICatalogCollection interface"); // populate collection hr = (*ppiColl)->Populate(); if (COMADMIN_E_OBJECTERRORS == hr) CpiLogCatalogErrorInfo(); ExitOnFailure(hr, "Failed to populate collection"); hr = S_OK; LExit: // clean up ReleaseObject(piCatalog); ReleaseObject(piDisp); ReleaseBSTR(bstrName); ::VariantClear(&vtKey); return hr; }
static HRESULT ParseWxlControl( __in IXMLDOMNode* pixn, __in DWORD dwIdx, __in WIX_LOCALIZATION* pWixLoc ) { HRESULT hr = S_OK; LOC_CONTROL* pLocControl = NULL; BSTR bstrText = NULL; pLocControl = pWixLoc->rgLocControls + dwIdx; // Id hr = XmlGetAttribute(pixn, L"Control", &bstrText); ExitOnFailure(hr, "Failed to get Xml attribute Control in Wxl file."); hr = StrAllocString(&pLocControl->wzControl, bstrText, 0); ExitOnFailure(hr, "Failed to duplicate Xml attribute Control in Wxl file."); ReleaseNullBSTR(bstrText); // X pLocControl->nX = LOC_CONTROL_NOT_SET; hr = XmlGetAttributeNumber(pixn, L"X", reinterpret_cast<DWORD*>(&pLocControl->nX)); ExitOnFailure(hr, "Failed to get control X attribute."); // Y pLocControl->nY = LOC_CONTROL_NOT_SET; hr = XmlGetAttributeNumber(pixn, L"Y", reinterpret_cast<DWORD*>(&pLocControl->nY)); ExitOnFailure(hr, "Failed to get control Y attribute."); // Width pLocControl->nWidth = LOC_CONTROL_NOT_SET; hr = XmlGetAttributeNumber(pixn, L"Width", reinterpret_cast<DWORD*>(&pLocControl->nWidth)); ExitOnFailure(hr, "Failed to get control width attribute."); // Height pLocControl->nHeight = LOC_CONTROL_NOT_SET; hr = XmlGetAttributeNumber(pixn, L"Height", reinterpret_cast<DWORD*>(&pLocControl->nHeight)); ExitOnFailure(hr, "Failed to get control height attribute."); // Text hr = XmlGetText(pixn, &bstrText); ExitOnFailure(hr, "Failed to get control text in Wxl file."); hr = StrAllocString(&pLocControl->wzText, bstrText, 0); ExitOnFailure(hr, "Failed to duplicate control text in Wxl file."); LExit: ReleaseBSTR(bstrText); return hr; }
HRESULT CpiResetObjectProperty( ICatalogCollection* piColl, ICatalogObject* piObj, LPCWSTR pwzPropName ) { HRESULT hr = S_OK; BSTR bstrPropName = NULL; long lChanges = 0; VARIANT vtVal; ::VariantInit(&vtVal); // allocate property name string bstrPropName = ::SysAllocString(pwzPropName); ExitOnNull(bstrPropName, hr, E_OUTOFMEMORY, "Failed to allocate deleteable property name string"); // get value hr = piObj->get_Value(bstrPropName, &vtVal); ExitOnFailure(hr, "Failed to get deleteable property value"); hr = ::VariantChangeType(&vtVal, &vtVal, 0, VT_BOOL); ExitOnFailure(hr, "Failed to change variant type"); // if the deleteable property is set if (VARIANT_FALSE == vtVal.boolVal) { // clear property vtVal.boolVal = VARIANT_TRUE; hr = piObj->put_Value(bstrPropName, vtVal); ExitOnFailure(hr, "Failed to get property value"); // save changes hr = piColl->SaveChanges(&lChanges); if (COMADMIN_E_OBJECTERRORS == hr) CpiLogCatalogErrorInfo(); ExitOnFailure(hr, "Failed to save changes"); } hr = S_OK; LExit: // clean up ReleaseBSTR(bstrPropName); ::VariantClear(&vtVal); return hr; }
static HRESULT ParseWxlString( __in IXMLDOMNode* pixn, __in DWORD dwIdx, __in WIX_LOCALIZATION* pWixLoc ) { HRESULT hr = S_OK; LOC_STRING* pLocString = NULL; BSTR bstrText = NULL; pLocString = pWixLoc->rgLocStrings + dwIdx; // Id hr = XmlGetAttribute(pixn, L"Id", &bstrText); ExitOnFailure(hr, "Failed to get Xml attribute Id in Wxl file."); hr = StrAllocFormatted(&pLocString->wzId, L"#(loc.%s)", bstrText); ExitOnFailure(hr, "Failed to duplicate Xml attribute Id in Wxl file."); ReleaseNullBSTR(bstrText); // Overrideable hr = XmlGetAttribute(pixn, L"Overridable", &bstrText); ExitOnFailure(hr, "Failed to get Xml attribute Overridable."); if (S_OK == hr) { pLocString->bOverridable = CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, bstrText, -1, L"yes", -1); } ReleaseNullBSTR(bstrText); // Text hr = XmlGetText(pixn, &bstrText); ExitOnFailure(hr, "Failed to get Xml text in Wxl file."); hr = StrAllocString(&pLocString->wzText, bstrText, 0); ExitOnFailure(hr, "Failed to duplicate Xml text in Wxl file."); LExit: ReleaseBSTR(bstrText); return hr; }
extern "C" HRESULT DAPI XmlCreateElement( __in IXMLDOMDocument *pixdDocument, __in_z LPCWSTR wzElementName, __out IXMLDOMElement **ppixnElement ) { if (!ppixnElement || !pixdDocument) { return E_INVALIDARG; } HRESULT hr = S_OK; BSTR bstrElementName = ::SysAllocString(wzElementName); ExitOnNull(bstrElementName, hr, E_OUTOFMEMORY, "failed SysAllocString"); hr = pixdDocument->createElement(bstrElementName, ppixnElement); LExit: ReleaseBSTR(bstrElementName); return hr; }
HRESULT CpiGetCatalogCollection( LPCWSTR pwzName, ICatalogCollection** ppiColl ) { HRESULT hr = S_OK; ICOMAdminCatalog* piCatalog = NULL; IDispatch* piDisp = NULL; BSTR bstrName = NULL; // copy name string bstrName = ::SysAllocString(pwzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "Failed to allocate BSTR for collection name"); // get catalog hr = CpiGetAdminCatalog(&piCatalog); ExitOnFailure(hr, "Failed to get COM+ admin catalog"); // get collecton from catalog hr = piCatalog->GetCollection(bstrName, &piDisp); ExitOnFailure(hr, "Failed to get collection"); hr = piDisp->QueryInterface(IID_ICatalogCollection, (void**)ppiColl); ExitOnFailure(hr, "Failed to get IID_ICatalogCollection interface"); // populate collection hr = (*ppiColl)->Populate(); ExitOnFailure(hr, "Failed to populate collection"); hr = S_OK; LExit: // clean up ReleaseObject(piCatalog); ReleaseObject(piDisp); ReleaseBSTR(bstrName); return hr; }
/******************************************************************** XmlReportParseError - *********************************************************************/ static void XmlReportParseError( __in IXMLDOMParseError* pixpe ) { HRESULT hr = S_OK; long lNumber = 0; BSTR bstr = NULL; Trace(REPORT_STANDARD, "Failed to parse XML. IXMLDOMParseError reports:"); hr = pixpe->get_errorCode(&lNumber); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.errorCode."); Trace1(REPORT_STANDARD, "errorCode = 0x%x", lNumber); hr = pixpe->get_filepos(&lNumber); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.filepos."); Trace1(REPORT_STANDARD, "filepos = %d", lNumber); hr = pixpe->get_line(&lNumber); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.line."); Trace1(REPORT_STANDARD, "line = %d", lNumber); hr = pixpe->get_linepos(&lNumber); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.linepos."); Trace1(REPORT_STANDARD, "linepos = %d", lNumber); hr = pixpe->get_reason(&bstr); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.reason."); Trace1(REPORT_STANDARD, "reason = %ls", bstr); ReleaseNullBSTR(bstr); hr = pixpe->get_srcText (&bstr); ExitOnFailure(hr, "Failed to query IXMLDOMParseError.srcText ."); Trace1(REPORT_STANDARD, "srcText = %ls", bstr); ReleaseNullBSTR(bstr); LExit: ReleaseBSTR(bstr); }
HRESULT CpiGetCollectionObjectValue( ICatalogObject* piObj, LPCWSTR szPropName, LPWSTR* ppwzValue ) { HRESULT hr = S_OK; BSTR bstrPropName = NULL; VARIANT vtVal; ::VariantInit(&vtVal); // allocate property name string bstrPropName = ::SysAllocString(szPropName); ExitOnNull(bstrPropName, hr, E_OUTOFMEMORY, "Failed to allocate property name string"); // get value hr = piObj->get_Value(bstrPropName, &vtVal); ExitOnFailure(hr, "Failed to get property value"); hr = ::VariantChangeType(&vtVal, &vtVal, 0, VT_BSTR); ExitOnFailure(hr, "Failed to change variant type"); hr = StrAllocString(ppwzValue, vtVal.bstrVal, ::SysStringLen(vtVal.bstrVal)); ExitOnFailure(hr, "Failed to allocate memory for value string"); hr = S_OK; LExit: // clean up ReleaseBSTR(bstrPropName); ::VariantClear(&vtVal); return hr; }
/******************************************************************** XmlLoadDocumentEx - *********************************************************************/ extern "C" HRESULT DAPI XmlLoadDocumentEx( __in_z LPCWSTR wzDocument, __in DWORD dwAttributes, __out IXMLDOMDocument** ppixdDocument ) { HRESULT hr = S_OK; VARIANT_BOOL vbSuccess = 0; // RELEASEME IXMLDOMDocument* pixd = NULL; IXMLDOMParseError* pixpe = NULL; BSTR bstrLoad = NULL; if (!wzDocument || !*wzDocument) { hr = E_UNEXPECTED; ExitOnFailure(hr, "string must be non-null"); } hr = XmlCreateDocument(NULL, &pixd); if (hr == S_FALSE) { hr = E_FAIL; } ExitOnFailure(hr, "failed XmlCreateDocument"); if (dwAttributes & XML_LOAD_PRESERVE_WHITESPACE) { hr = pixd->put_preserveWhiteSpace(VARIANT_TRUE); ExitOnFailure(hr, "failed put_preserveWhiteSpace"); } // Security issue. Avoid triggering anything external. hr = pixd->put_validateOnParse(VARIANT_FALSE); ExitOnFailure(hr, "failed put_validateOnParse"); hr = pixd->put_resolveExternals(VARIANT_FALSE); ExitOnFailure(hr, "failed put_resolveExternals"); bstrLoad = ::SysAllocString(wzDocument); ExitOnNull(bstrLoad, hr, E_OUTOFMEMORY, "failed to allocate bstr for Load in XmlLoadDocumentEx"); hr = pixd->loadXML(bstrLoad, &vbSuccess); if (S_FALSE == hr) { hr = HRESULT_FROM_WIN32(ERROR_OPEN_FAILED); } if (FAILED(hr) && S_OK == pixd->get_parseError(&pixpe)) { XmlReportParseError(pixpe); } ExitOnFailure(hr, "failed loadXML"); hr = S_OK; LExit: if (ppixdDocument) { *ppixdDocument = pixd; pixd = NULL; } ReleaseBSTR(bstrLoad); ReleaseObject(pixd); ReleaseObject(pixpe); return hr; }
/****************************************************************** AddPortExceptionOnCurrentProfile ********************************************************************/ static HRESULT AddPortExceptionOnCurrentProfile( __in LPCWSTR wzName, __in_opt LPCWSTR wzRemoteAddresses, __in BOOL fIgnoreFailures, __in int iPort, __in int iProtocol ) { HRESULT hr = S_OK; BSTR bstrName = NULL; BSTR bstrRemoteAddresses = NULL; INetFwProfile* pfwProfile = NULL; INetFwOpenPorts* pfwPorts = NULL; INetFwOpenPort* pfwPort = NULL; // convert to BSTRs to make COM happy bstrName = ::SysAllocString(wzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for name"); bstrRemoteAddresses = ::SysAllocString(wzRemoteAddresses); ExitOnNull(bstrRemoteAddresses, hr, E_OUTOFMEMORY, "failed SysAllocString for remote addresses"); // create and initialize a new open port object hr = ::CoCreateInstance(__uuidof(NetFwOpenPort), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwOpenPort), reinterpret_cast<void**>(&pfwPort)); ExitOnFailure(hr, "failed to create new open port"); hr = pfwPort->put_Port(iPort); ExitOnFailure(hr, "failed to set exception port"); hr = pfwPort->put_Protocol(static_cast<NET_FW_IP_PROTOCOL>(iProtocol)); ExitOnFailure(hr, "failed to set exception protocol"); if (bstrRemoteAddresses && *bstrRemoteAddresses) { hr = pfwPort->put_RemoteAddresses(bstrRemoteAddresses); ExitOnFailure1(hr, "failed to set exception remote addresses '%ls'", bstrRemoteAddresses); } hr = pfwPort->put_Name(bstrName); ExitOnFailure(hr, "failed to set exception name"); // get the firewall profile, its current list of open ports, and add ours hr = GetCurrentFirewallProfile(fIgnoreFailures, &pfwProfile); ExitOnFailure(hr, "failed to get firewall profile"); if (S_FALSE == hr) // user or package author chose to ignore missing firewall { ExitFunction(); } hr = pfwProfile->get_GloballyOpenPorts(&pfwPorts); ExitOnFailure(hr, "failed to get open ports"); hr = pfwPorts->Add(pfwPort); ExitOnFailure(hr, "failed to add exception to global list"); LExit: ReleaseBSTR(bstrRemoteAddresses); ReleaseBSTR(bstrName); ReleaseObject(pfwProfile); ReleaseObject(pfwPorts); ReleaseObject(pfwPort); return fIgnoreFailures ? S_OK : hr; }
HRESULT CpiGetApplicationsCollection( LPCWSTR pwzPartID, ICatalogCollection** ppiAppColl ) { HRESULT hr = S_OK; ICOMAdminCatalog* piCatalog = NULL; ICOMAdminCatalog2* piCatalog2 = NULL; BSTR bstrGlobPartID = NULL; ICatalogCollection* piPartColl = NULL; ICatalogObject* piPartObj = NULL; // get catalog hr = CpiGetAdminCatalog(&piCatalog); ExitOnFailure(hr, "Failed to get COM+ admin catalog"); // get ICOMAdminCatalog2 interface hr = piCatalog->QueryInterface(IID_ICOMAdminCatalog2, (void**)&piCatalog2); // COM+ 1.5 or later if (E_NOINTERFACE != hr) { ExitOnFailure(hr, "Failed to get IID_ICOMAdminCatalog2 interface"); // partition id if (!pwzPartID || !*pwzPartID) { // get global partition id hr = piCatalog2->get_GlobalPartitionID(&bstrGlobPartID); ExitOnFailure(hr, "Failed to get global partition id"); } // get partitions collection hr = CpiGetPartitionsCollection(&piPartColl); ExitOnFailure(hr, "Failed to get partitions collection"); // find object hr = CpiFindCollectionObjectByStringKey(piPartColl, bstrGlobPartID ? bstrGlobPartID : pwzPartID, &piPartObj); ExitOnFailure(hr, "Failed to find collection object"); if (S_FALSE == hr) ExitFunction(); // partition not found, exit with hr = S_FALSE // get applications collection hr = CpiGetCatalogCollection(piPartColl, piPartObj, L"Applications", ppiAppColl); ExitOnFailure(hr, "Failed to get catalog collection for partition"); } // COM+ pre 1.5 else { // this version of COM+ does not support partitions, make sure a partition was not specified if (pwzPartID && *pwzPartID) ExitOnFailure(hr = E_FAIL, "Partitions are not supported by this version of COM+"); // get applications collection hr = CpiGetCatalogCollection(L"Applications", ppiAppColl); ExitOnFailure(hr, "Failed to get catalog collection"); } hr = S_OK; LExit: // clean up ReleaseObject(piCatalog); ReleaseObject(piCatalog2); ReleaseBSTR(bstrGlobPartID); ReleaseObject(piPartColl); ReleaseObject(piPartObj); return hr; }
/****************************************************************** AddApplicationExceptionOnCurrentProfile ********************************************************************/ static HRESULT AddApplicationExceptionOnCurrentProfile( __in LPCWSTR wzFile, __in LPCWSTR wzName, __in_opt LPCWSTR wzRemoteAddresses, __in BOOL fIgnoreFailures ) { HRESULT hr = S_OK; BSTR bstrFile = NULL; BSTR bstrName = NULL; BSTR bstrRemoteAddresses = NULL; INetFwProfile* pfwProfile = NULL; INetFwAuthorizedApplications* pfwApps = NULL; INetFwAuthorizedApplication* pfwApp = NULL; // convert to BSTRs to make COM happy bstrFile = ::SysAllocString(wzFile); ExitOnNull(bstrFile, hr, E_OUTOFMEMORY, "failed SysAllocString for path"); bstrName = ::SysAllocString(wzName); ExitOnNull(bstrName, hr, E_OUTOFMEMORY, "failed SysAllocString for name"); bstrRemoteAddresses = ::SysAllocString(wzRemoteAddresses); ExitOnNull(bstrRemoteAddresses, hr, E_OUTOFMEMORY, "failed SysAllocString for remote addresses"); // get the firewall profile, which is our entry point for adding exceptions hr = GetCurrentFirewallProfile(fIgnoreFailures, &pfwProfile); ExitOnFailure(hr, "failed to get firewall profile"); if (S_FALSE == hr) // user or package author chose to ignore missing firewall { ExitFunction(); } // first, let's see if the app is already on the exception list hr = pfwProfile->get_AuthorizedApplications(&pfwApps); ExitOnFailure(hr, "failed to get list of authorized apps"); // try to find it (i.e., support reinstall) hr = pfwApps->Item(bstrFile, &pfwApp); if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr) { // not found, so we get to add it hr = ::CoCreateInstance(__uuidof(NetFwAuthorizedApplication), NULL, CLSCTX_INPROC_SERVER, __uuidof(INetFwAuthorizedApplication), reinterpret_cast<void**>(&pfwApp)); ExitOnFailure(hr, "failed to create authorized app"); // set the display name hr = pfwApp->put_Name(bstrName); ExitOnFailure(hr, "failed to set authorized app name"); // set path hr = pfwApp->put_ProcessImageFileName(bstrFile); ExitOnFailure(hr, "failed to set authorized app path"); // set the allowed remote addresses if (bstrRemoteAddresses && *bstrRemoteAddresses) { hr = pfwApp->put_RemoteAddresses(bstrRemoteAddresses); ExitOnFailure(hr, "failed to set authorized app remote addresses"); } // add it to the list of authorized apps hr = pfwApps->Add(pfwApp); ExitOnFailure(hr, "failed to add app to the authorized apps list"); } else { // we found an existing app exception (if we succeeded, that is) ExitOnFailure(hr, "failed trying to find existing app"); // enable it (just in case it was disabled) pfwApp->put_Enabled(VARIANT_TRUE); } LExit: ReleaseBSTR(bstrRemoteAddresses); ReleaseBSTR(bstrName); ReleaseBSTR(bstrFile); ReleaseObject(pfwApp); ReleaseObject(pfwApps); ReleaseObject(pfwProfile); return fIgnoreFailures ? S_OK : hr; }
/****************************************************************** CreateFwRuleObject - CoCreate a firewall rule, and set the common set of properties which are shared between port and application firewall rules ********************************************************************/ static HRESULT CreateFwRuleObject( __in BSTR bstrName, __in int iProfile, __in_opt LPCWSTR wzRemoteAddresses, __in LPCWSTR wzPort, __in int iProtocol, __in LPCWSTR wzDescription, __out INetFwRule** ppNetFwRule ) { HRESULT hr = S_OK; BSTR bstrRemoteAddresses = NULL; BSTR bstrPort = NULL; BSTR bstrDescription = NULL; INetFwRule* pNetFwRule = NULL; *ppNetFwRule = NULL; // convert to BSTRs to make COM happy bstrRemoteAddresses = ::SysAllocString(wzRemoteAddresses); ExitOnNull(bstrRemoteAddresses, hr, E_OUTOFMEMORY, "failed SysAllocString for remote addresses"); bstrPort = ::SysAllocString(wzPort); ExitOnNull(bstrPort, hr, E_OUTOFMEMORY, "failed SysAllocString for port"); bstrDescription = ::SysAllocString(wzDescription); ExitOnNull(bstrDescription, hr, E_OUTOFMEMORY, "failed SysAllocString for description"); hr = ::CoCreateInstance(__uuidof(NetFwRule), NULL, CLSCTX_ALL, __uuidof(INetFwRule), (void**)&pNetFwRule); ExitOnFailure(hr, "failed to create NetFwRule object"); hr = pNetFwRule->put_Name(bstrName); ExitOnFailure(hr, "failed to set exception name"); hr = pNetFwRule->put_Profiles(static_cast<NET_FW_PROFILE_TYPE2>(iProfile)); ExitOnFailure(hr, "failed to set exception profile"); if (MSI_NULL_INTEGER != iProtocol) { hr = pNetFwRule->put_Protocol(static_cast<NET_FW_IP_PROTOCOL>(iProtocol)); ExitOnFailure(hr, "failed to set exception protocol"); } if (bstrPort && *bstrPort) { hr = pNetFwRule->put_LocalPorts(bstrPort); ExitOnFailure(hr, "failed to set exception port"); } if (bstrRemoteAddresses && *bstrRemoteAddresses) { hr = pNetFwRule->put_RemoteAddresses(bstrRemoteAddresses); ExitOnFailure1(hr, "failed to set exception remote addresses '%ls'", bstrRemoteAddresses); } if (bstrDescription && *bstrDescription) { hr = pNetFwRule->put_Description(bstrDescription); ExitOnFailure1(hr, "failed to set exception description '%ls'", bstrDescription); } *ppNetFwRule = pNetFwRule; pNetFwRule = NULL; LExit: ReleaseBSTR(bstrRemoteAddresses); ReleaseBSTR(bstrPort); ReleaseBSTR(bstrDescription); ReleaseObject(pNetFwRule); return hr; }
HRESULT CpiGetApplicationsCollection( ICatalogCollection** ppiAppColl ) { HRESULT hr = S_OK; ICOMAdminCatalog* piCatalog = NULL; ICOMAdminCatalog2* piCatalog2 = NULL; ICatalogCollection* piPartColl = NULL; ICatalogObject* piPartObj = NULL; BSTR bstrGlobPartID = NULL; if (!gpiAppColl) { // get catalog hr = CpiGetAdminCatalog(&piCatalog); ExitOnFailure(hr, "Failed to get COM+ admin catalog"); // get ICOMAdminCatalog2 interface hr = piCatalog->QueryInterface(IID_ICOMAdminCatalog2, (void**)&piCatalog2); // COM+ 1.5 or later if (E_NOINTERFACE != hr) { ExitOnFailure(hr, "Failed to get IID_ICOMAdminCatalog2 interface"); // get global partition id hr = piCatalog2->get_GlobalPartitionID(&bstrGlobPartID); ExitOnFailure(hr, "Failed to get global partition id"); // get partitions collection hr = CpiGetPartitionsCollection(&piPartColl); ExitOnFailure(hr, "Failed to get partitions collection"); // find object hr = CpiFindCollectionObject(piPartColl, bstrGlobPartID, NULL, &piPartObj); ExitOnFailure(hr, "Failed to find collection object"); if (S_FALSE == hr) ExitFunction(); // partition not found, exit with hr = S_FALSE // get applications collection hr = CpiGetCatalogCollection(piPartColl, piPartObj, L"Applications", &gpiAppColl); ExitOnFailure(hr, "Failed to get applications collection"); } // COM+ pre 1.5 else { // get applications collection hr = CpiGetCatalogCollection(L"Applications", &gpiAppColl); ExitOnFailure(hr, "Failed to get applications collection"); } } // return value gpiAppColl->AddRef(); *ppiAppColl = gpiAppColl; hr = S_OK; LExit: // clean up ReleaseObject(piCatalog); ReleaseObject(piCatalog2); ReleaseObject(piPartColl); ReleaseObject(piPartObj); ReleaseBSTR(bstrGlobPartID); return hr; }
/******************************************************************** ShelExecUnelevated() - executes a target unelevated. *******************************************************************/ extern "C" HRESULT DAPI ShelExecUnelevated( __in_z LPCWSTR wzTargetPath, __in_z_opt LPCWSTR wzParameters, __in_z_opt LPCWSTR wzVerb, __in_z_opt LPCWSTR wzWorkingDirectory, __in int nShowCmd ) { HRESULT hr = S_OK; BSTR bstrTargetPath = NULL; VARIANT vtParameters = { }; VARIANT vtVerb = { }; VARIANT vtWorkingDirectory = { }; VARIANT vtShow = { }; IShellView* psv = NULL; IShellDispatch2* psd = NULL; bstrTargetPath = ::SysAllocString(wzTargetPath); ExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate target path BSTR."); if (wzParameters && *wzParameters) { vtParameters.vt = VT_BSTR; vtParameters.bstrVal = ::SysAllocString(wzParameters); ExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate parameters BSTR."); } if (wzVerb && *wzVerb) { vtVerb.vt = VT_BSTR; vtVerb.bstrVal = ::SysAllocString(wzVerb); ExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate verb BSTR."); } if (wzWorkingDirectory && *wzWorkingDirectory) { vtWorkingDirectory.vt = VT_BSTR; vtWorkingDirectory.bstrVal = ::SysAllocString(wzWorkingDirectory); ExitOnNull(bstrTargetPath, hr, E_OUTOFMEMORY, "Failed to allocate working directory BSTR."); } vtShow.vt = VT_INT; vtShow.intVal = nShowCmd; hr = GetDesktopShellView(IID_PPV_ARGS(&psv)); ExitOnFailure(hr, "Failed to get desktop shell view."); hr = GetShellDispatchFromView(psv, IID_PPV_ARGS(&psd)); ExitOnFailure(hr, "Failed to get shell dispatch from view."); hr = psd->ShellExecute(bstrTargetPath, vtParameters, vtWorkingDirectory, vtVerb, vtShow); if (S_FALSE == hr) { hr = HRESULT_FROM_WIN32(ERROR_CANCELLED); } ExitOnRootFailure1(hr, "Failed to launch unelevate executable: %ls", bstrTargetPath); LExit: ReleaseObject(psd); ReleaseObject(psv); ReleaseBSTR(vtWorkingDirectory.bstrVal); ReleaseBSTR(vtVerb.bstrVal); ReleaseBSTR(vtParameters.bstrVal); ReleaseBSTR(bstrTargetPath); return hr; }
/******************************************************************** SqlSessionExecuteQuery - executes a query and returns the results if desired NOTE: ppirs and pcRoes and pbstrErrorDescription are optional ********************************************************************/ extern "C" HRESULT DAPI SqlSessionExecuteQuery( __in IDBCreateSession* pidbSession, __in __sql_command LPCWSTR wzSql, __out_opt IRowset** ppirs, __out_opt DBROWCOUNT* pcRows, __out_opt BSTR* pbstrErrorDescription ) { Assert(pidbSession); HRESULT hr = S_OK; IDBCreateCommand* pidbCommand = NULL; ICommandText* picmdText = NULL; ICommand* picmd = NULL; DBROWCOUNT cRows = 0; if (pcRows) { *pcRows = NULL; } // // create the command // hr = pidbSession->CreateSession(NULL, IID_IDBCreateCommand, (IUnknown**)&pidbCommand); ExitOnFailure(hr, "failed to create database session"); hr = pidbCommand->CreateCommand(NULL, IID_ICommand, (IUnknown**)&picmd); ExitOnFailure(hr, "failed to create command to execute session"); // // set the sql text into the command // hr = picmd->QueryInterface(IID_ICommandText, (LPVOID*)&picmdText); ExitOnFailure(hr, "failed to get command text object for command"); hr = picmdText->SetCommandText(DBGUID_DEFAULT , wzSql); ExitOnFailure1(hr, "failed to set SQL string: %ls", wzSql); // // execute the command // hr = picmd->Execute(NULL, (ppirs) ? IID_IRowset : IID_NULL, NULL, &cRows, reinterpret_cast<IUnknown**>(ppirs)); ExitOnFailure1(hr, "failed to execute SQL string: %ls", wzSql); if (DB_S_ERRORSOCCURRED == hr) { hr = E_FAIL; } if (pcRows) { *pcRows = cRows; } LExit: if (FAILED(hr) && picmd && pbstrErrorDescription) { HRESULT hrGetErrors = SqlGetErrorInfo(picmd, IID_ICommandText, 0x409, NULL, pbstrErrorDescription); // TODO: use current locale instead of always American-English if (FAILED(hrGetErrors)) { ReleaseBSTR(*pbstrErrorDescription); } } ReleaseObject(picmd); ReleaseObject(picmdText); ReleaseObject(pidbCommand); return hr; }
HRESULT ScaWebSearch7( __in SCA_WEB7* psw, __deref_out_z_opt LPWSTR* pswWeb, __out_opt BOOL* pfFound ) { HRESULT hr = S_OK; BOOL fInitializedCom = FALSE; BSTR bstrSites = NULL; BSTR bstrAppHostRoot = NULL; IAppHostAdminManager *pAdminMgr = NULL; IAppHostElement *pSites = NULL; IAppHostElementCollection *pCollection = NULL; IAppHostElement *pSite = NULL; if (NULL != pswWeb) { ReleaseNullStr(*pswWeb); } if (NULL != pfFound) { *pfFound = FALSE; } hr = ::CoInitialize(NULL); ExitOnFailure(hr, "Failed to initialize COM"); fInitializedCom = TRUE; hr = CoCreateInstance(__uuidof(AppHostAdminManager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IAppHostAdminManager), reinterpret_cast<void**> (&pAdminMgr)); if (REGDB_E_CLASSNOTREG == hr) { WcaLog(LOGMSG_VERBOSE, "AppHostAdminManager was not registered, cannot find site."); hr = S_OK; ExitFunction(); } ExitOnFailure(hr, "Failed to CoCreate IAppHostAdminManager"); bstrSites = ::SysAllocString(IIS_CONFIG_SITES_SECTION); ExitOnNull(bstrSites, hr, E_OUTOFMEMORY, "Failed to allocate sites string."); bstrAppHostRoot = ::SysAllocString(IIS_CONFIG_APPHOST_ROOT); ExitOnNull(bstrAppHostRoot, hr, E_OUTOFMEMORY, "Failed to allocate host root string."); hr = pAdminMgr->GetAdminSection(bstrSites, bstrAppHostRoot, &pSites); ExitOnFailure(hr, "Failed get sites section"); ExitOnNull(pSites, hr, ERROR_FILE_NOT_FOUND, "Failed get sites section object"); hr = pSites->get_Collection(&pCollection); ExitOnFailure(hr, "Failed get sites collection"); // not explicitly doing a Description search if (-1 != psw->iSiteId) { if (MSI_NULL_INTEGER == psw->iSiteId) { // Enumerate sites & determine if the binding matches hr = Iis7EnumAppHostElements(pCollection, EnumSiteCompareBinding, psw, &pSite, NULL); ExitOnFailure(hr, "Failed locate site by ID"); } else { // Find a site with ID matches hr = Iis7FindAppHostElementInteger(pCollection, IIS_CONFIG_SITE, IIS_CONFIG_ID, psw->iSiteId, &pSite, NULL); ExitOnFailure(hr, "Failed locate site by ID"); } } if (NULL == pSite) { // Find a site with Name that matches hr = Iis7FindAppHostElementString(pCollection, IIS_CONFIG_SITE, IIS_CONFIG_NAME, psw->wzDescription, &pSite, NULL); ExitOnFailure(hr, "Failed locate site by ID"); } if (NULL != pSite) { if (NULL != pfFound) { *pfFound = TRUE; } if (NULL != pswWeb) { // We found a site, return its description hr = Iis7GetPropertyString(pSite, IIS_CONFIG_NAME, pswWeb); ExitOnFailure(hr, "Failed get site name"); } } LExit: ReleaseNullObject(pAdminMgr); ReleaseNullObject(pSites); ReleaseNullObject(pCollection); ReleaseNullObject(pSite); ReleaseBSTR(bstrAppHostRoot); ReleaseBSTR(bstrSites); if (fInitializedCom) { ::CoUninitialize(); } return hr; }