//+-------------------------------------------------------------------------- // File: config.cpp // Contents: CConfigStorage implements read/write to CA configuration data // currently stored under HKLM\System\CCS\Services\Certsvc\ // Configuration //--------------------------------------------------------------------------- #include #include #pragma hdrstop using namespace CertSrv; HRESULT CConfigStorage::InitMachine(LPCWSTR pcwszMachine) { m_pwszMachine = new WCHAR[wcslen(pcwszMachine)+3]; if(!m_pwszMachine) { return E_OUTOFMEMORY; } m_pwszMachine[0] = L'\0'; if(pcwszMachine[0]!=L'\\' && pcwszMachine[1]!=L'\\') { wcscpy(m_pwszMachine, L"\\\\"); } wcscat(m_pwszMachine, pcwszMachine); return S_OK; } CConfigStorage::~CConfigStorage() { if(m_hRemoteHKLM) RegCloseKey(m_hRemoteHKLM); if(m_hRootConfigKey) RegCloseKey(m_hRootConfigKey); if(m_hCAKey) RegCloseKey(m_hCAKey); if(m_pwszMachine) delete[] m_pwszMachine; } // Retrieve a CA configuration value. If no authority name is specified, the // node path must be NULL and value is queried from the Configuration root. // If an authority name is passed in, the value is retrieved from the authority // node; if a node path is passed in, it is used relative to the authority node // to read the value. // For example, to read Configuration\DBDirectory, call: // // GetEntry(NULL, NULL, L"DBDirectory", &var) // // To read Configuration\MyCA\CAServerName, call: // // GetEntry(L"MyCA", NULL, L"CAServerName", &var) // // To read Configuration\MyCA\CSP\HashAlgorithm, call: // // GetEntry(L"MyCA", L"CSP", L"HashAlgorithm" // // // If pcwszValue is null, getentry returns a VT_ARRAY|VT_BSTR with a list // of subkey names. HRESULT CConfigStorage::GetEntry( LPCWSTR pcwszAuthorityName, LPCWSTR pcwszRelativeNodePath, LPCWSTR pcwszValue, VARIANT *pVariant) { HRESULT hr = S_OK; HKEY hKey = NULL; BOOL fRet; LPBYTE pData = NULL, pTmp; DWORD cData = 0; HKEY hKeyTmp = NULL; DWORD dwType; DWORD nIndex; DWORD cName; DWORD cKeys; if(EmptyString(pcwszAuthorityName)) { if(!EmptyString(pcwszRelativeNodePath)) { hr = E_INVALIDARG; _JumpError(hr, error, "CConfigStorage::GetEntry"); } hr = InitRootKey(); _JumpIfError(hr, error, "CConfigStorage::InitRootKey"); hKey = m_hRootConfigKey; } else { hr = InitCAKey(pcwszAuthorityName); _JumpIfError(hr, error, "CConfigStorage::InitCAKey"); hKey = m_hCAKey; } CSASSERT(hKey); if(!EmptyString(pcwszRelativeNodePath)) { hr = RegOpenKeyEx( hKey, pcwszRelativeNodePath, 0, KEY_ALL_ACCESS, &hKeyTmp); _JumpIfError(hr, error, "RegOpenKeyEx"); hKey = hKeyTmp; } if(EmptyString(pcwszValue)) { dwType = REG_MULTI_SZ; cData = 2; hr = RegQueryInfoKey( hKey, NULL,NULL,NULL, &cKeys, &cName, NULL,NULL,NULL,NULL,NULL,NULL); _JumpIfError(hr, error, "RegQueryInfoKey"); cData = (cName+1)*cKeys*sizeof(WCHAR); pData = (LPBYTE)LocalAlloc(LMEM_FIXED, cData); if(!pData) { hr = E_OUTOFMEMORY; _JumpError(hr, error, "LocalAlloc"); } pTmp = pData; for(nIndex=0;nIndex