Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

667 lines
16 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. cgenericlogger.cpp
  5. Abstract:
  6. This file contains base class definitions for logging RSOP security extension data to WMI.
  7. Author:
  8. Vishnu Patankar (VishnuP) 7-April-2000
  9. Environment:
  10. User Mode - Win32
  11. Revision History:
  12. --*/
  13. ///////////////////////////////////////////////////////////////////////////////
  14. // //
  15. // Includes //
  16. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #include "CGenericLogger.h"
  19. #include "scedllrc.h"
  20. extern HINSTANCE MyModuleHandle;
  21. /////////////////////////////////////////////////////////////////////
  22. // Construction/Destruction
  23. //////////////////////////////////////////////////////////////////////
  24. CGenericLogger::CGenericLogger(IWbemServices *pNamespace, PWSTR pwszGPOName, const PWSTR pwszSOMID)
  25. : m_bInitialized(FALSE),
  26. m_pHr(WBEM_S_NO_ERROR),
  27. m_pNamespace(NULL),
  28. m_pClassForSpawning(NULL),
  29. m_pObj(NULL)
  30. {
  31. m_pNamespace = pNamespace;
  32. m_pHr = WBEM_S_NO_ERROR;
  33. m_xbstrId = L"Id";
  34. if (!m_xbstrId) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  35. m_xbstrGPO = L"GPOID";
  36. if (!m_xbstrGPO) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  37. m_xbstrSOM = L"SOMID";
  38. if (!m_xbstrSOM) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  39. m_xbstrPrecedence = L"precedence";
  40. if (!m_xbstrPrecedence) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  41. m_xbstrStatus = L"Status";
  42. if (!m_xbstrStatus) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  43. m_xbstrErrorCode = L"ErrorCode";
  44. if (!m_xbstrErrorCode) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  45. m_xbstrCanonicalGPOName = pwszGPOName;
  46. if (!m_xbstrCanonicalGPOName) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  47. m_xbstrSOMID = pwszSOMID;
  48. if (!m_xbstrSOMID) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  49. done:
  50. return;
  51. }
  52. CGenericLogger::~CGenericLogger()
  53. {
  54. // Free class spawner instance and object instance (if need be)
  55. if (m_pObj) {
  56. m_pHr = m_pObj->Release();
  57. m_pObj = NULL;
  58. if ( FAILED(m_pHr) ) goto done;
  59. }
  60. if (m_pClassForSpawning) {
  61. m_pHr = m_pClassForSpawning->Release();
  62. m_pClassForSpawning = NULL;
  63. if ( FAILED(m_pHr) ) goto done;
  64. }
  65. done:
  66. m_pHr = WBEM_S_NO_ERROR;
  67. m_pNamespace = NULL;
  68. //doesn't matter if we cannot release
  69. m_bInitialized = FALSE;
  70. return;
  71. }
  72. /////////////////////////////////////////////////////////////////////
  73. // Set error method
  74. //////////////////////////////////////////////////////////////////////
  75. void CGenericLogger::SetError(HRESULT hr){
  76. m_pHr = hr;
  77. }
  78. /////////////////////////////////////////////////////////////////////
  79. // Get error method
  80. //////////////////////////////////////////////////////////////////////
  81. HRESULT CGenericLogger::GetError(){
  82. return m_pHr;
  83. }
  84. /////////////////////////////////////////////////////////////////////
  85. // Log properties common for all settings
  86. //////////////////////////////////////////////////////////////////////
  87. HRESULT CGenericLogger::PutGenericProperties(){
  88. GUID guid;
  89. WCHAR pwszGuid[MAX_GUID_STRING_LEN];
  90. // create unique guid for this instance
  91. m_pHr = CoCreateGuid( &guid );
  92. if ( FAILED(m_pHr) ) goto done;
  93. if (SCEP_NULL_GUID(guid)) goto done;
  94. SCEP_GUID_TO_STRING(guid, pwszGuid);
  95. // log generic properties
  96. m_pHr = PutProperty(m_pObj, m_xbstrId, pwszGuid);
  97. if ( FAILED(m_pHr) ) goto done;
  98. m_pHr = PutProperty(m_pObj, m_xbstrGPO, m_xbstrCanonicalGPOName);
  99. if ( FAILED(m_pHr) ) goto done;
  100. m_pHr = PutProperty(m_pObj, m_xbstrSOM, m_xbstrSOMID);
  101. if ( FAILED(m_pHr) ) goto done;
  102. m_pHr = PutProperty(m_pObj, m_xbstrStatus, (int)0);
  103. if ( FAILED(m_pHr) ) goto done;
  104. m_pHr = PutProperty(m_pObj, m_xbstrErrorCode, (int)ERROR_SUCCESS);
  105. if ( FAILED(m_pHr) ) goto done;
  106. done:
  107. return m_pHr;
  108. }
  109. /////////////////////////////////////////////////////////////////////
  110. // Commit the instance to the database and free resources
  111. //////////////////////////////////////////////////////////////////////
  112. HRESULT CGenericLogger::PutInstAndFreeObj(){
  113. m_pHr = m_pNamespace->PutInstance( m_pObj, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL );
  114. if ( FAILED(m_pHr) ) goto done;
  115. done:
  116. m_pHr = m_pObj->Release();
  117. m_pObj = NULL;
  118. return m_pHr;
  119. }
  120. /////////////////////////////////////////////////////////////////////
  121. // Get
  122. // (a) an instance of spawner class (if needed) and
  123. // (b) an instance of a schema object from (a)
  124. //////////////////////////////////////////////////////////////////////
  125. HRESULT CGenericLogger::SpawnAnInstance(IWbemClassObject **pObj)
  126. {
  127. if(!m_pClassForSpawning){
  128. if(FAILED(m_pHr = m_pNamespace->GetObject(m_xbstrClassName, 0, NULL,
  129. &m_pClassForSpawning, NULL))){
  130. *pObj = NULL;
  131. return m_pHr;
  132. }
  133. }
  134. m_pHr = m_pClassForSpawning->SpawnInstance(0, pObj);
  135. return m_pHr;
  136. }
  137. /////////////////////////////////////////////////////////////////////
  138. // Overloaded logging functions
  139. //////////////////////////////////////////////////////////////////////
  140. /////////////////////////////////////////////////////////////////////
  141. // String logging
  142. //////////////////////////////////////////////////////////////////////
  143. HRESULT CGenericLogger::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, WCHAR *wcValue)
  144. {
  145. BSTR bstrName = SysAllocString(wcProperty);
  146. if (!bstrName) {m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  147. VARIANT *vp = new VARIANT;
  148. if (vp == NULL ) { SysFreeString(bstrName); m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  149. VariantInit(vp);
  150. V_VT(vp) = VT_BSTR;
  151. V_BSTR(vp) = SysAllocString(wcValue);
  152. if(!V_BSTR(vp)){
  153. SysFreeString(bstrName);
  154. m_pHr = WBEM_E_OUT_OF_MEMORY;
  155. goto done;
  156. }
  157. if( wcValue != NULL ) {
  158. m_pHr = pObj->Put(bstrName, 0, vp, NULL);
  159. if(FAILED(m_pHr)){
  160. SysFreeString(bstrName);
  161. VariantClear(vp);
  162. delete vp;
  163. m_pHr = WBEM_E_OUT_OF_MEMORY;
  164. goto done;
  165. }
  166. }else m_pHr = WBEM_E_FAILED;
  167. SysFreeString(bstrName);
  168. VariantClear(vp);
  169. delete vp;
  170. done:
  171. return m_pHr;
  172. }
  173. /////////////////////////////////////////////////////////////////////
  174. // Integer logging
  175. //////////////////////////////////////////////////////////////////////
  176. HRESULT CGenericLogger::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, int iValue)
  177. {
  178. BSTR bstrName = SysAllocString(wcProperty);
  179. if (!bstrName) {m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  180. VARIANT *pv = new VARIANT;
  181. if (pv == NULL ) { SysFreeString(bstrName); m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  182. if(iValue != SCE_NO_VALUE){
  183. VariantInit(pv);
  184. V_VT(pv) = VT_I4;
  185. V_I4(pv) = iValue;
  186. m_pHr = pObj->Put(bstrName, 0, pv, NULL);
  187. VariantClear(pv);
  188. if(FAILED(m_pHr)){
  189. SysFreeString(bstrName);
  190. delete pv;
  191. if ( FAILED(m_pHr) ) goto done;
  192. }
  193. }else m_pHr = WBEM_E_FAILED;
  194. SysFreeString(bstrName);
  195. delete pv;
  196. done:
  197. return m_pHr;
  198. }
  199. /////////////////////////////////////////////////////////////////////
  200. // Boolean logging
  201. //////////////////////////////////////////////////////////////////////
  202. HRESULT CGenericLogger::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, bool bValue)
  203. {
  204. BSTR bstrName = SysAllocString(wcProperty);
  205. if (!bstrName) {m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  206. VARIANT *pv = new VARIANT;
  207. if (pv == NULL ) { SysFreeString(bstrName); m_pHr = WBEM_E_OUT_OF_MEMORY; return m_pHr;}
  208. VariantInit(pv);
  209. V_VT(pv) = VT_BOOL;
  210. if(bValue) V_BOOL(pv) = VARIANT_TRUE;
  211. else V_BOOL(pv) = VARIANT_FALSE;
  212. m_pHr = pObj->Put(bstrName, 0, pv, NULL);
  213. SysFreeString(bstrName);
  214. VariantClear(pv);
  215. delete pv;
  216. return m_pHr;
  217. }
  218. /////////////////////////////////////////////////////////////////////
  219. // List logging
  220. //////////////////////////////////////////////////////////////////////
  221. HRESULT CGenericLogger::PutProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, PSCE_NAME_LIST strList)
  222. {
  223. long lCount=0;
  224. PSCE_NAME_LIST pTemp;
  225. for ( pTemp = strList; pTemp != NULL; pTemp=pTemp->Next,lCount++);
  226. if ( lCount == 0 ) return m_pHr; // nothing to save
  227. BSTR bstrName = SysAllocString(wcProperty);
  228. if (!bstrName) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  229. VARIANT v;
  230. SAFEARRAYBOUND sbArrayBounds ;
  231. sbArrayBounds.cElements = lCount;
  232. sbArrayBounds.lLbound = 0;
  233. if(V_ARRAY(&v) = SafeArrayCreate(VT_BSTR, 1, &sbArrayBounds)){
  234. V_VT(&v) = VT_BSTR | VT_ARRAY ;
  235. BSTR bstrVal;
  236. long j;
  237. //get names in the SCE_NAME_LIST structure into the VARIANT
  238. for(j=0, pTemp = strList; j < lCount && pTemp != NULL ; j++, pTemp=pTemp->Next){
  239. bstrVal = SysAllocString(pTemp->Name);
  240. SafeArrayPutElement(V_ARRAY(&v), &j, bstrVal);
  241. SysFreeString(bstrVal);
  242. }
  243. m_pHr = pObj->Put(bstrName, 0, &v, NULL);
  244. if ( FAILED(m_pHr) ) {
  245. SysFreeString(bstrName);
  246. VariantClear(&v);
  247. if ( FAILED(m_pHr) ) goto done;
  248. }
  249. }else m_pHr = WBEM_E_FAILED;
  250. SysFreeString(bstrName);
  251. VariantClear(&v);
  252. done:
  253. return m_pHr;
  254. }
  255. /////////////////////////////////////////////////////////////////////
  256. // Get method
  257. //////////////////////////////////////////////////////////////////////
  258. HRESULT CGenericLogger::GetProperty(IWbemClassObject *pObj, const WCHAR *wcProperty, int *piValue)
  259. {
  260. VARIANT v;
  261. BSTR bstrProp = SysAllocString(wcProperty);
  262. if(!bstrProp) {m_pHr = WBEM_E_OUT_OF_MEMORY; goto done;}
  263. VariantInit(&v);
  264. if(SUCCEEDED(m_pHr = pObj->Get(bstrProp, 0, &v, NULL, NULL))){
  265. if(V_VT(&v) == VT_I4) *piValue = V_I4(&v);
  266. else if(V_VT(&v) == VT_EMPTY || V_VT(&v) == VT_NULL ) m_pHr = WBEM_S_RESET_TO_DEFAULT;
  267. else *piValue = 0;
  268. }
  269. SysFreeString(bstrProp);
  270. VariantClear(&v);
  271. done:
  272. return m_pHr;
  273. }
  274. HRESULT DeleteInstances( WCHAR *pwszClass, IWbemServices *pWbemServices )
  275. {
  276. HRESULT hr = WBEM_E_OUT_OF_MEMORY;
  277. try {
  278. IEnumWbemClassObject *pEnum = NULL;
  279. XBStr xbstrClass( pwszClass );
  280. if ( !xbstrClass )
  281. return WBEM_E_OUT_OF_MEMORY;
  282. hr = pWbemServices->CreateInstanceEnum( xbstrClass,
  283. WBEM_FLAG_SHALLOW,
  284. NULL,
  285. &pEnum );
  286. if ( FAILED(hr) )
  287. return hr;
  288. XInterface<IEnumWbemClassObject> xEnum( pEnum );
  289. XBStr xbstrPath( L"__PATH" );
  290. if ( !xbstrPath )
  291. return WBEM_E_OUT_OF_MEMORY;
  292. IWbemClassObject *pInstance = NULL;
  293. ULONG ulReturned = 1;
  294. LONG TIMEOUT = -1;
  295. while ( ulReturned == 1 ) {
  296. hr = pEnum->Next( TIMEOUT,
  297. 1,
  298. &pInstance,
  299. &ulReturned );
  300. if ( hr == WBEM_S_NO_ERROR && ulReturned == 1 ) {
  301. XInterface<IWbemClassObject> xInstance( pInstance );
  302. VARIANT var;
  303. VariantInit( &var );
  304. hr = pInstance->Get( xbstrPath,
  305. 0L,
  306. &var,
  307. NULL,
  308. NULL );
  309. if ( FAILED(hr) )
  310. return hr;
  311. hr = pWbemServices->DeleteInstance( var.bstrVal,
  312. 0L,
  313. NULL,
  314. NULL );
  315. VariantClear( &var );
  316. if ( FAILED(hr) )
  317. return hr;
  318. }
  319. }
  320. } catch (...) {
  321. // do nothing
  322. }
  323. return hr;
  324. }
  325. /////////////////////////////////////////////////////////////////////
  326. // Error code conversion routines
  327. //////////////////////////////////////////////////////////////////////
  328. /////////////////////////////////////////////////////////////////////
  329. // ESCAPE to DOS
  330. //////////////////////////////////////////////////////////////////////
  331. /*
  332. DWORD
  333. ScepSceStatusToDosError(
  334. IN SCESTATUS SceStatus
  335. )
  336. {
  337. switch(SceStatus) {
  338. case SCESTATUS_SUCCESS:
  339. return(NO_ERROR);
  340. case SCESTATUS_OTHER_ERROR:
  341. return(ERROR_EXTENDED_ERROR);
  342. case SCESTATUS_INVALID_PARAMETER:
  343. return(ERROR_INVALID_PARAMETER);
  344. case SCESTATUS_RECORD_NOT_FOUND:
  345. return(ERROR_NO_MORE_ITEMS);
  346. case SCESTATUS_NO_MAPPING:
  347. return(ERROR_NONE_MAPPED);
  348. case SCESTATUS_TRUST_FAIL:
  349. return(ERROR_TRUSTED_DOMAIN_FAILURE);
  350. case SCESTATUS_INVALID_DATA:
  351. return(ERROR_INVALID_DATA);
  352. case SCESTATUS_OBJECT_EXIST:
  353. return(ERROR_FILE_EXISTS);
  354. case SCESTATUS_BUFFER_TOO_SMALL:
  355. return(ERROR_INSUFFICIENT_BUFFER);
  356. case SCESTATUS_PROFILE_NOT_FOUND:
  357. return(ERROR_FILE_NOT_FOUND);
  358. case SCESTATUS_BAD_FORMAT:
  359. return(ERROR_BAD_FORMAT);
  360. case SCESTATUS_NOT_ENOUGH_RESOURCE:
  361. return(ERROR_NOT_ENOUGH_MEMORY);
  362. case SCESTATUS_ACCESS_DENIED:
  363. return(ERROR_ACCESS_DENIED);
  364. case SCESTATUS_CANT_DELETE:
  365. return(ERROR_CURRENT_DIRECTORY);
  366. case SCESTATUS_PREFIX_OVERFLOW:
  367. return(ERROR_BUFFER_OVERFLOW);
  368. case SCESTATUS_ALREADY_RUNNING:
  369. return(ERROR_SERVICE_ALREADY_RUNNING);
  370. case SCESTATUS_SERVICE_NOT_SUPPORT:
  371. return(ERROR_NOT_SUPPORTED);
  372. case SCESTATUS_MOD_NOT_FOUND:
  373. return(ERROR_MOD_NOT_FOUND);
  374. case SCESTATUS_EXCEPTION_IN_SERVER:
  375. return(ERROR_EXCEPTION_IN_SERVICE);
  376. default:
  377. return(ERROR_EXTENDED_ERROR);
  378. }
  379. }
  380. */
  381. /////////////////////////////////////////////////////////////////////
  382. // DOS to WBEM
  383. //////////////////////////////////////////////////////////////////////
  384. HRESULT
  385. ScepDosErrorToWbemError(
  386. IN DWORD rc
  387. )
  388. {
  389. switch(rc) {
  390. case NO_ERROR:
  391. return(WBEM_S_NO_ERROR);
  392. case ERROR_INVALID_PARAMETER:
  393. return(WBEM_E_INVALID_PARAMETER);
  394. case ERROR_NO_MORE_ITEMS:
  395. case ERROR_NONE_MAPPED:
  396. case ERROR_NOT_FOUND:
  397. case ERROR_FILE_NOT_FOUND:
  398. case ERROR_MOD_NOT_FOUND:
  399. return(WBEM_E_NOT_FOUND);
  400. case ERROR_INVALID_DATA:
  401. case ERROR_BAD_FORMAT:
  402. return(WBEM_E_INVALID_CONTEXT);
  403. case ERROR_FILE_EXISTS:
  404. case ERROR_SERVICE_ALREADY_RUNNING:
  405. return(WBEM_S_ALREADY_EXISTS);
  406. case ERROR_INSUFFICIENT_BUFFER:
  407. return(WBEM_E_BUFFER_TOO_SMALL);
  408. case ERROR_NOT_ENOUGH_MEMORY:
  409. return(WBEM_E_OUT_OF_MEMORY);
  410. case ERROR_ACCESS_DENIED:
  411. return(WBEM_E_ACCESS_DENIED);
  412. case ERROR_BUFFER_OVERFLOW:
  413. return(WBEM_E_QUEUE_OVERFLOW);
  414. case ERROR_NOT_SUPPORTED:
  415. return(WBEM_E_NOT_SUPPORTED);
  416. case ERROR_NO_MATCH:
  417. return(WBEM_E_INVALID_CLASS);
  418. default:
  419. return(WBEM_E_FAILED);
  420. }
  421. }
  422. /////////////////////////////////////////////////////////////////////
  423. // WBEM to DOS
  424. //////////////////////////////////////////////////////////////////////
  425. DWORD
  426. ScepWbemErrorToDosError(
  427. IN HRESULT m_pHr
  428. )
  429. {
  430. switch(m_pHr) {
  431. case WBEM_S_NO_ERROR:
  432. // this is a boolean used while enumeration etc. - so really a tautology check (not a real error)
  433. case WBEM_S_FALSE:
  434. return(NO_ERROR);
  435. case WBEM_E_INVALID_PARAMETER:
  436. return(ERROR_INVALID_PARAMETER);
  437. case WBEM_E_INVALID_CLASS:
  438. return(ERROR_NO_MATCH);
  439. case WBEM_E_NOT_FOUND:
  440. return(ERROR_NOT_FOUND);
  441. case WBEM_E_INVALID_CONTEXT:
  442. return(ERROR_BAD_FORMAT);
  443. case WBEM_S_ALREADY_EXISTS:
  444. return(ERROR_FILE_EXISTS);
  445. case WBEM_E_BUFFER_TOO_SMALL:
  446. return(ERROR_INSUFFICIENT_BUFFER);
  447. case WBEM_E_OUT_OF_MEMORY:
  448. return(ERROR_NOT_ENOUGH_MEMORY);
  449. case WBEM_E_ACCESS_DENIED:
  450. return(ERROR_ACCESS_DENIED);
  451. case WBEM_E_QUEUE_OVERFLOW:
  452. return(ERROR_BUFFER_OVERFLOW);
  453. case WBEM_E_NOT_SUPPORTED:
  454. return(ERROR_NOT_SUPPORTED);
  455. default:
  456. return(ERROR_INVALID_PARAMETER);
  457. }
  458. }