Esempio n. 1
0
BOOL EnumSiteCompareBinding(
    __in IAppHostElement* pSite,
    __in LPVOID pContext
    )
{
    BOOL fFound = FALSE;
    HRESULT hr = S_OK;
    SCA_WEB7* psw = (SCA_WEB7*)pContext;
    IAppHostChildElementCollection *pSiteChildren = NULL;
    IAppHostElement *pBindings = NULL;
    IAppHostElementCollection *pBindingsCollection = NULL;
    IAppHostElement *pBinding = NULL;
    VARIANT vtProp;
    VariantInit(&vtProp);

    hr = pSite->get_ChildElements(&pSiteChildren);
    ExitOnFailure(hr, "Failed get site child elements collection");

    vtProp.vt = VT_BSTR;
    vtProp.bstrVal = ::SysAllocString(IIS_CONFIG_BINDINGS);
    hr = pSiteChildren->get_Item(vtProp, &pBindings);
    ExitOnFailure(hr, "Failed get bindings element");

    hr = pBindings->get_Collection(&pBindingsCollection);
    ExitOnFailure(hr, "Failed get bindings collection");

    WcaLog(LOGMSG_VERBOSE, "Searching for site with binding %ls:%d:%ls", psw->swaBinding.wzIP, psw->swaBinding.iPort, psw->swaBinding.wzHeader);

    hr = Iis7EnumAppHostElements(pBindingsCollection, CompareBinding, psw, &pBinding, NULL);
    ExitOnFailure(hr, "Failed search bindings collection");

    fFound = NULL != pBinding;
LExit:
    VariantClear(&vtProp);
    ReleaseNullObject(pSiteChildren);
    ReleaseNullObject(pBindings);
    ReleaseNullObject(pBindingsCollection);
    ReleaseNullObject(pBinding);
    return fFound;
}
	void cleanup() 
	{
			if ( pElemProp != NULL )
			{
				pElemProp->Release();
				pElemProp = NULL;
			}
			if ( pElemProps != NULL )
			{
				pElemProps->Release(); 
				pElemProps = NULL;
			}
			if ( pElem != NULL )
			{
				pElem->Release();
				pElem = NULL;
			}
			if ( pElemColl != NULL )
			{
				pElemColl->Release(); 
				pElemColl = NULL;
			}
			if ( pParentElem != NULL )
			{
				pParentElem->Release(); 
				pParentElem = NULL;
			}
			if ( pMgr != NULL )
			{
				pMgr->Release(); 
				pMgr = NULL;
			}

			SysFreeString( bstrConfigCommitPath );
			SysFreeString( bstrSectionName );
			SysFreeString( bstrPropertyName );

			CoUninitialize();
	}
HRESULT CModuleConfiguration::CreateNodeEnvironment(IHttpContext* ctx, DWORD debugPort, PCH namedPipe, PCH* env)
{
	HRESULT hr;
	LPCH currentEnvironment = NULL;
	LPCH tmpStart, tmpIndex = NULL;
	DWORD tmpSize;
	DWORD environmentSize;
	IAppHostElement* section = NULL;
	IAppHostElementCollection* appSettings = NULL;
	IAppHostElement* entry = NULL;
	IAppHostPropertyCollection* properties = NULL;
	IAppHostProperty* prop = NULL;
	BSTR keyPropertyName = NULL;
	BSTR valuePropertyName = NULL;
	VARIANT vKeyPropertyName;
	VARIANT vValuePropertyName;
	DWORD count;
	BSTR propertyValue;
	int propertySize;

	CheckNull(env);
	*env = NULL;

	// this is a zero terminated list of zero terminated strings of the form <var>=<value>

	// calculate size of current environment

	ErrorIf(NULL == (currentEnvironment = GetEnvironmentStrings()), GetLastError());
	environmentSize = 0;
	do {
		while (*(currentEnvironment + environmentSize++) != 0);
	} while (*(currentEnvironment + environmentSize++) != 0);

	// allocate memory for new environment variables

	tmpSize = 32767 - environmentSize;
	ErrorIf(NULL == (tmpIndex = tmpStart = new char[tmpSize]), ERROR_NOT_ENOUGH_MEMORY);
	RtlZeroMemory(tmpIndex, tmpSize);

	// set PORT and IISNODE_VERSION variables

	ErrorIf(tmpSize < (strlen(namedPipe) + strlen(IISNODE_VERSION) + 6 + 17), ERROR_NOT_ENOUGH_MEMORY);
	sprintf(tmpIndex, "PORT=%s", namedPipe);
	tmpIndex += strlen(namedPipe) + 6;
	sprintf(tmpIndex, "IISNODE_VERSION=%s", IISNODE_VERSION);
	tmpIndex += strlen(IISNODE_VERSION) + 17;

	// set DEBUGPORT environment variable if requested (used by node-inspector)

	if (debugPort > 0)
	{
		char debugPortS[64];
		sprintf(debugPortS, "%d", debugPort);
		ErrorIf((tmpSize - (tmpIndex - tmpStart)) < (strlen(debugPortS) + 11), ERROR_NOT_ENOUGH_MEMORY);
		sprintf(tmpIndex, "DEBUGPORT=%s", debugPortS);
		tmpIndex += strlen(debugPortS) + 11;
	}
		
	// add environment variables from the appSettings section of config

	ErrorIf(NULL == (keyPropertyName = SysAllocString(L"key")), ERROR_NOT_ENOUGH_MEMORY);
	vKeyPropertyName.vt = VT_BSTR;
	vKeyPropertyName.bstrVal = keyPropertyName;
	ErrorIf(NULL == (valuePropertyName = SysAllocString(L"value")), ERROR_NOT_ENOUGH_MEMORY);
	vValuePropertyName.vt = VT_BSTR;
	vValuePropertyName.bstrVal = valuePropertyName;
	CheckError(GetConfigSection(ctx, &section, L"appSettings"));
	CheckError(section->get_Collection(&appSettings));
	CheckError(appSettings->get_Count(&count));

	for (USHORT i = 0; i < count; i++)
	{
		VARIANT index;
		index.vt = VT_I2;
		index.iVal = i;		

		CheckError(appSettings->get_Item(index, &entry));
		CheckError(entry->get_Properties(&properties));
		
		CheckError(properties->get_Item(vKeyPropertyName, &prop));
		CheckError(prop->get_StringValue(&propertyValue));
		ErrorIf(0 == (propertySize = WideCharToMultiByte(CP_ACP, 0, propertyValue, wcslen(propertyValue), NULL, 0, NULL, NULL)), E_FAIL);
		ErrorIf((propertySize + 2) > (tmpSize - (tmpStart - tmpIndex)), ERROR_NOT_ENOUGH_MEMORY);
		ErrorIf(propertySize != WideCharToMultiByte(CP_ACP, 0, propertyValue, wcslen(propertyValue), tmpIndex, propertySize, NULL, NULL), E_FAIL);
		tmpIndex[propertySize] = '=';
		tmpIndex += propertySize + 1;
		SysFreeString(propertyValue);
		propertyValue = NULL;
		prop->Release();
		prop = NULL;

		CheckError(properties->get_Item(vValuePropertyName, &prop));
		CheckError(prop->get_StringValue(&propertyValue));
		ErrorIf(0 == (propertySize = WideCharToMultiByte(CP_ACP, 0, propertyValue, wcslen(propertyValue), NULL, 0, NULL, NULL)), E_FAIL);
		ErrorIf((propertySize + 1) > (tmpSize - (tmpStart - tmpIndex)), ERROR_NOT_ENOUGH_MEMORY);
		ErrorIf(propertySize != WideCharToMultiByte(CP_ACP, 0, propertyValue, wcslen(propertyValue), tmpIndex, propertySize, NULL, NULL), E_FAIL);
		tmpIndex += propertySize + 1;
		SysFreeString(propertyValue);
		propertyValue = NULL;
		prop->Release();
		prop = NULL;

		properties->Release();
		properties = NULL;
		entry->Release();
		entry = NULL;
	}

	// concatenate new environment variables with the current environment block

	ErrorIf(NULL == (*env = (LPCH)new char[environmentSize + (tmpIndex - tmpStart)]), ERROR_NOT_ENOUGH_MEMORY);	
	memcpy(*env, tmpStart, (tmpIndex - tmpStart));
	memcpy(*env + (tmpIndex - tmpStart), currentEnvironment, environmentSize);

	// cleanup

	FreeEnvironmentStrings(currentEnvironment);
	section->Release();
	appSettings->Release();
	SysFreeString(keyPropertyName);
	SysFreeString(valuePropertyName);
	delete [] tmpStart;

	return S_OK;
Error:

	if (currentEnvironment)
	{
		FreeEnvironmentStrings(currentEnvironment);
		currentEnvironment = NULL;
	}

	if (section)
	{
		section->Release();
		section = NULL;
	}

	if (appSettings)
	{
		appSettings->Release();
		appSettings = NULL;
	}

	if (keyPropertyName)
	{
		SysFreeString(keyPropertyName);
		keyPropertyName = NULL;
	}

	if (valuePropertyName)
	{
		SysFreeString(valuePropertyName);
		valuePropertyName = NULL;
	}

	if (entry)
	{
		entry->Release();
		entry = NULL;
	}

	if (properties)
	{
		properties->Release();
		properties = NULL;
	}

	if (prop)
	{
		prop->Release();
		prop = NULL;
	}

	if (propertyValue)
	{
		SysFreeString(propertyValue);
		propertyValue = NULL;
	}

	if (tmpStart)
	{
		delete [] tmpStart;
		tmpStart = NULL;
	}

	return hr;
}
HRESULT CModuleConfiguration::GetConfig(IHttpContext* context, CModuleConfiguration** config)
{
	HRESULT hr;
	CModuleConfiguration* c = NULL;
	IAppHostElement* section = NULL;
	LPWSTR commandLine = NULL;
	size_t i;

	CheckNull(config);

	*config = (CModuleConfiguration*)context->GetMetadata()->GetModuleContextContainer()->GetModuleContext(moduleId);

	if (NULL == *config)
	{
		ErrorIf(NULL == (c = new CModuleConfiguration()), ERROR_NOT_ENOUGH_MEMORY);
		
		CheckError(GetConfigSection(context, &section));		
		CheckError(GetDWORD(section, L"maxPendingRequestsPerApplication", &c->maxPendingRequestsPerApplication));
		CheckError(GetDWORD(section, L"asyncCompletionThreadCount", &c->asyncCompletionThreadCount));
		CheckError(GetDWORD(section, L"maxProcessCountPerApplication", &c->maxProcessCountPerApplication));
		CheckError(GetDWORD(section, L"maxConcurrentRequestsPerProcess", &c->maxConcurrentRequestsPerProcess));
		CheckError(GetDWORD(section, L"maxNamedPipeConnectionRetry", &c->maxNamedPipeConnectionRetry));
		CheckError(GetDWORD(section, L"namedPipeConnectionRetryDelay", &c->namedPipeConnectionRetryDelay));
		CheckError(GetDWORD(section, L"initialRequestBufferSize", &c->initialRequestBufferSize));
		CheckError(GetDWORD(section, L"maxRequestBufferSize", &c->maxRequestBufferSize));
		CheckError(GetDWORD(section, L"uncFileChangesPollingInterval", &c->uncFileChangesPollingInterval));
		CheckError(GetDWORD(section, L"gracefulShutdownTimeout", &c->gracefulShutdownTimeout));
		CheckError(GetDWORD(section, L"logFileFlushInterval", &c->logFileFlushInterval));
		CheckError(GetDWORD(section, L"maxLogFileSizeInKB", &c->maxLogFileSizeInKB));
		CheckError(GetBOOL(section, L"loggingEnabled", &c->loggingEnabled));
		CheckError(GetBOOL(section, L"appendToExistingLog", &c->appendToExistingLog));
		CheckError(GetString(section, L"logDirectoryNameSuffix", &c->logDirectoryNameSuffix));
		CheckError(GetString(section, L"debuggerPortRange", &c->debugPortRange));
		CheckError(GetString(section, L"debuggerPathSegment", &c->debuggerPathSegment));
		c->debuggerPathSegmentLength = wcslen(c->debuggerPathSegment);
		CheckError(GetString(section, L"nodeProcessCommandLine", &commandLine));
		ErrorIf(NULL == (c->nodeProcessCommandLine = new char[MAX_PATH]), ERROR_NOT_ENOUGH_MEMORY);
		ErrorIf(0 != wcstombs_s(&i, c->nodeProcessCommandLine, (size_t)MAX_PATH, commandLine, _TRUNCATE), ERROR_INVALID_PARAMETER);
		delete [] commandLine;
		commandLine = NULL;
		
		section->Release();
		section = NULL;
		
		// CR: check for ERROR_ALREADY_ASSIGNED to detect a race in creation of this section 
		// CR: refcounting may be needed if synchronous code paths proove too long (race with config changes)
		context->GetMetadata()->GetModuleContextContainer()->SetModuleContext(c, moduleId);
		*config = c;
		c = NULL;
	}

	return S_OK;
Error:

	if (NULL != section)
	{
		section->Release();
		section = NULL;
	}

	if (NULL != commandLine)
	{
		delete [] commandLine;
		commandLine = NULL;
	}

	if (NULL != c)
	{
		delete c;
		c = NULL;
	}

	return hr;
}
	REQUEST_NOTIFICATION_STATUS OnSendResponse( IN IHttpContext * pHttpContext, IN ISendResponseProvider * pProvider )
	{
			UNREFERENCED_PARAMETER( pProvider );

			pMgr        = NULL;
			pParentElem = NULL;
			pElemColl   = NULL;
			pElem       = NULL;
			pElemProps  = NULL;
			pElemProp   = NULL;

			hr                   = S_OK;
			bstrConfigCommitPath = SysAllocString( L"MACHINE/WEBROOT/APPHOST" );
			bstrSectionName      = SysAllocString( L"system.webServer/stripHeaders" );
			bstrPropertyName     = SysAllocString( L"name" );
			dwElementCount       = 0;
			vtPropertyName.vt            = VT_BSTR;
			vtPropertyName.bstrVal       = bstrPropertyName;

			// Initialize
			hr = CoInitializeEx( NULL, COINIT_MULTITHREADED );
			if ( FAILED( hr ) )
			{
				// Set the error status.
				pProvider->SetErrorStatus( hr );
				// cleanup
				cleanup();
				// End additional processing.
				return RQ_NOTIFICATION_FINISH_REQUEST;
			}

			// Create
			hr = CoCreateInstance( __uuidof( AppHostAdminManager ), NULL, 
					CLSCTX_INPROC_SERVER,
					__uuidof( IAppHostAdminManager ), (void**) &pMgr );
			if( FAILED( hr ) )
			{
				pProvider->SetErrorStatus( hr );
				cleanup();
				return RQ_NOTIFICATION_FINISH_REQUEST;
			}

			// Get the admin section
			hr = pMgr->GetAdminSection( bstrSectionName, bstrConfigCommitPath, &pParentElem );
			if ( FAILED( hr ) || ( &pParentElem == NULL ) )
			{
				pProvider->SetErrorStatus( hr );
				cleanup();
				return RQ_NOTIFICATION_FINISH_REQUEST;
			}


			// Get the site collection
			hr = pParentElem->get_Collection( &pElemColl );
			if ( FAILED ( hr ) || ( &pElemColl == NULL ) )
			{
				pProvider->SetErrorStatus( hr );
				cleanup();
				return RQ_NOTIFICATION_FINISH_REQUEST;
			}

			// Get the elements
			hr = pElemColl->get_Count( &dwElementCount );
			for ( USHORT i = 0; i < dwElementCount; i++ )
			{
				VARIANT vtItemIndex;
				vtItemIndex.vt = VT_I2;
				vtItemIndex.iVal = i;

				// Add a new section group
				hr = pElemColl->get_Item( vtItemIndex, &pElem );
				if ( FAILED( hr ) || ( &pElem == NULL ) )
				{
					pProvider->SetErrorStatus( hr );
					cleanup();
					return RQ_NOTIFICATION_FINISH_REQUEST;
				}

				// Get the child elements
				hr = pElem->get_Properties( &pElemProps );
				if ( FAILED( hr ) || ( &pElemProps == NULL ) )
				{
					pProvider->SetErrorStatus( hr );
					cleanup();
					return RQ_NOTIFICATION_FINISH_REQUEST;
				}

				hr = pElemProps->get_Item( vtPropertyName, &pElemProp );
				if ( FAILED( hr ) || ( pElemProp == NULL ) )
				{
					pProvider->SetErrorStatus( hr );
					cleanup();
					return RQ_NOTIFICATION_FINISH_REQUEST;
				}

				hr = pElemProp->get_Value( &vtValue );
				if ( FAILED( hr ) )
				{
					pProvider->SetErrorStatus( hr );
					cleanup();
					return RQ_NOTIFICATION_FINISH_REQUEST;
				}

				// Retrieve a pointer to the response.
				IHttpResponse * pHttpResponse = pHttpContext->GetResponse();

				// Test for an error.
				if ( pHttpResponse != NULL )
				{
					// convert bstr to string in order to delete header
					_bstr_t header( vtValue.bstrVal );

					// delete header
					hr = pHttpResponse->DeleteHeader( (char *)header );

					// Test for an error.
					if ( FAILED( hr ) )
					{
						// Set the error status.
						pProvider->SetErrorStatus( hr );
						// cleanup
						cleanup();
						// End additional processing.
						return RQ_NOTIFICATION_FINISH_REQUEST;
					}
				}

				// loop_cleanup
				if ( pElem != NULL )
				{
					pElem->Release(); 
					pElem = NULL;
				}
			}

			cleanup();
			// Return processing to the pipeline.
			return RQ_NOTIFICATION_CONTINUE;
		}
Esempio n. 6
0
HRESULT
GetSharedConfigEnabled(
    BOOL * pfIsSharedConfig
)
/*++

Routine Description:
   Search the configuration for the shared configuration property.

Arguments:

    pfIsSharedConfig - true if shared configuration is enabled

Return Value:
    HRESULT

--*/
{
    HRESULT                 hr = S_OK;
    IAppHostAdminManager    *pAdminManager = NULL;

    BSTR                    bstrSectionName = NULL;
    BSTR                    bstrConfigPath = NULL;

    IAppHostElement *       pConfigRedirSection = NULL;


    bstrSectionName = SysAllocString( L"configurationRedirection" );

    if ( bstrSectionName == NULL )
    {
        hr = E_OUTOFMEMORY;
        DBGERROR_HR(hr);
        goto exit;
    }

    bstrConfigPath = SysAllocString( L"MACHINE/REDIRECTION" );
    if ( bstrConfigPath == NULL )
    {
        hr = E_OUTOFMEMORY;
        DBGERROR_HR(hr);
        goto exit;
    }

    hr = CoCreateInstance( CLSID_AppHostAdminManager,
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IAppHostAdminManager,
                           (VOID **)&pAdminManager );
    if( FAILED(hr) )
    {
        DBGERROR_HR(hr);
        goto exit;
    }

    hr = pAdminManager->GetAdminSection( bstrSectionName,
                                         bstrConfigPath,
                                         &pConfigRedirSection );
    if( FAILED(hr) )
    {
        DBGERROR_HR(hr);
        goto exit;
    }

    hr = GetElementBoolProperty( pConfigRedirSection,
                                 L"enabled",
                                 pfIsSharedConfig );

    if ( FAILED( hr ) )
    {
        DBGERROR_HR(hr);
        goto exit;
    }

    pConfigRedirSection->Release();
    pConfigRedirSection = NULL;


exit:

    //
    // dump config exception to setup log file (if available)
    //

    if ( pConfigRedirSection != NULL )
    {
        pConfigRedirSection->Release();
    }

    if ( pAdminManager != NULL )
    {
        pAdminManager->Release();
    }

    if ( bstrConfigPath != NULL )
    {
        SysFreeString( bstrConfigPath );
    }

    if ( bstrSectionName != NULL )
    {
        SysFreeString( bstrSectionName );
    }

    return hr;
}
Esempio n. 7
0
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;
}