Leaked source code of windows server 2003
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.

2354 lines
72 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation 1996-2001.
  5. //
  6. // File: wmihooks.cpp
  7. //
  8. // Contents: implementation of CWMIRsop
  9. //
  10. //----------------------------------------------------------------------------
  11. #include <stdafx.h>
  12. #include <wmihooks.h>
  13. #include "util.h"
  14. WCHAR const QUERY_LANG[] = L"WQL";
  15. WCHAR const WMI_CLASS[] = L"__CLASS";
  16. WCHAR const RSOP_NAME_SPACE[] = L"root\\rsop\\computer";
  17. WCHAR const RSOP_PREC_ONE_QUERY[] = L"select * from RSOP_SecuritySettings where precedence=1";
  18. WCHAR const RSOP_ALL_QUERY[] = L"select * from RSOP_SecuritySettings";
  19. WCHAR const RSOP_STATUS[] = L"Status";
  20. WCHAR const RSOP_ERROR[] = L"ErrorCode";
  21. WCHAR const RSOP_PRECEDENCE[] = L"precedence";
  22. WCHAR const RSOP_GPOID[] = L"GPOID";
  23. WCHAR const RSOP_KEYNAME[] = L"KeyName";
  24. WCHAR const RSOP_SETTING[] = L"Setting";
  25. WCHAR const RSOP_USERRIGHT[] = L"UserRight";
  26. WCHAR const RSOP_ACCOUNTLIST[] = L"AccountList";
  27. WCHAR const RSOP_EVENTLOG_TYPE[] = L"Type";
  28. //String Constants for RSOP_classNames
  29. WCHAR const RSOP_SEC_NUM[] = TEXT("RSOP_SecuritySettingNumeric");
  30. WCHAR const RSOP_SEC_BOOL[] = TEXT("RSOP_SecuritySettingBoolean");
  31. WCHAR const RSOP_SCE_STRING[] = TEXT("RSOP_SecuritySettingString");
  32. WCHAR const RSOP_AUDIT[] = TEXT("RSOP_AuditPolicy");
  33. WCHAR const RSOP_EVENT_NUM[] = TEXT("RSOP_SecurityEventLogSettingNumeric");
  34. WCHAR const RSOP_EVENT_BOOL[] = TEXT("RSOP_SecurityEventLogSettingBoolean");
  35. WCHAR const RSOP_REG_VAL[] = TEXT("RSOP_RegistryValue");
  36. WCHAR const RSOP_USER_RIGHT[] = TEXT("RSOP_UserPrivilegeRight");
  37. WCHAR const RSOP_RGROUPS[] = TEXT("RSOP_RestrictedGroup");
  38. WCHAR const RSOP_SERVICE[] = TEXT("RSOP_SystemService");
  39. WCHAR const RSOP_FILE[] = TEXT("RSOP_File");
  40. WCHAR const RSOP_REG[] = TEXT("RSOP_RegistryKey");
  41. //KeyNames
  42. WCHAR const MIN_PASS_AGE[] = TEXT("MinimumPasswordAge");
  43. WCHAR const MAX_PASS_AGE[] = TEXT("MaximumPasswordAge");
  44. WCHAR const MIN_PASS_LEN[] = TEXT("MinimumPasswordLength");
  45. WCHAR const PASS_HIS_SIZE[] = TEXT("PasswordHistorySize");
  46. WCHAR const REQUIRE_LOGON_TO_CHANGE_PASS[] = TEXT("RequireLogonToChangePassword");
  47. WCHAR const LOCKOUT_COUNT[] = TEXT("LockoutBadCount");
  48. WCHAR const RESET_LOCKOUT_COUNT[] = TEXT("ResetLockoutCount");
  49. WCHAR const LOCKOUT_DURATION[] = TEXT("LockoutDuration");
  50. WCHAR const MAX_TICKET_AGE[] = TEXT("MaxTicketAge");
  51. WCHAR const MAX_RENEW_AGE[] = TEXT("MaxRenewAge");
  52. WCHAR const MAX_SERVICE_AGE[] = TEXT("MaxServiceAge");
  53. WCHAR const MAX_CLOCK_SKEW[] = TEXT("MaxClockSkew");
  54. WCHAR const VALIDATE_CLIENT[] = TEXT("TicketValidateClient");
  55. WCHAR const PASS_COMPLEX[] = TEXT("PasswordComplexity");
  56. WCHAR const FORCE_LOGOFF[] = TEXT("ForceLogOffWhenHourExpire");
  57. WCHAR const ENABLE_ADMIN[] = TEXT("EnableAdminAccount");
  58. WCHAR const ENABLE_GUEST[] = TEXT("EnableGuestAccount");
  59. WCHAR const LSA_ANON_LOOKUP[] = TEXT("LSAAnonymousNameLookup");
  60. WCHAR const CLEAR_TEXT_PASS[] = TEXT("ClearTextPassword");
  61. WCHAR const AUDIT_SYSTEM_EVENTS[] = TEXT("AuditSystemEvents");
  62. WCHAR const AUDIT_LOGON_EVENTS[] = TEXT("AuditLogonEvents");
  63. WCHAR const AUDIT_OBJECT_ACCESS[] = TEXT("AuditObjectAccess");
  64. WCHAR const AUDIT_PRIVILEGE_USE[] = TEXT("AuditPrivilegeUse");
  65. WCHAR const AUDIT_POLICY_CHANGE[] = TEXT("AuditPolicyChange");
  66. WCHAR const AUDIT_ACCOUNT_MANAGE[] = TEXT("AuditAccountManage");
  67. WCHAR const AUDIT_PROCESS_TRAKING[] = TEXT("AuditProcessTracking");
  68. WCHAR const AUDIT_DS_ACCESS[] = TEXT("AuditDSAccess");
  69. WCHAR const AUDIT_ACCOUNT_LOGON[] = TEXT("AuditAccountLogon");
  70. WCHAR const MAX_LOG_SIZE[] = TEXT("MaximumLogSize");
  71. WCHAR const AUDIT_LOG_RETENTION_PERIOD[] = TEXT ("AuditLogRetentionPeriod");
  72. WCHAR const RETENTION_DAYS[] = TEXT ("RetentionDays");
  73. WCHAR const RESTRICT_GUEST_ACCESS[] = TEXT ("RestrictGuestAccess");
  74. WCHAR const NEW_GUEST_NAME[] = TEXT ("NewGuestName");
  75. WCHAR const NEW_ADMINISTRATOR_NAME[] = TEXT ("NewAdministratorName");
  76. VOID FreeRI(PRSOP_INFO ptr)
  77. {
  78. if(ptr)
  79. {
  80. if(ptr->pszGPOID)
  81. LocalFree(ptr->pszGPOID);
  82. LocalFree(ptr);
  83. }
  84. }
  85. VOID InitWMI_SEC_PROFILE_INFO(PWMI_SCE_PROFILE_INFO pProfileInfo)
  86. {
  87. memset(pProfileInfo,0,sizeof(SCE_PROFILE_INFO));
  88. pProfileInfo->MinimumPasswordAge = SCE_NO_VALUE;
  89. pProfileInfo->MaximumPasswordAge = SCE_NO_VALUE;
  90. pProfileInfo->MinimumPasswordLength = SCE_NO_VALUE;
  91. pProfileInfo->PasswordComplexity = SCE_NO_VALUE;
  92. pProfileInfo->PasswordHistorySize = SCE_NO_VALUE;
  93. pProfileInfo->LockoutBadCount = SCE_NO_VALUE;
  94. pProfileInfo->ResetLockoutCount = SCE_NO_VALUE;
  95. pProfileInfo->LockoutDuration = SCE_NO_VALUE;
  96. pProfileInfo->RequireLogonToChangePassword = SCE_NO_VALUE;
  97. pProfileInfo->ForceLogoffWhenHourExpire = SCE_NO_VALUE;
  98. pProfileInfo->NewAdministratorName = 0;
  99. pProfileInfo->NewGuestName = 0;
  100. pProfileInfo->EnableAdminAccount = SCE_NO_VALUE;
  101. pProfileInfo->EnableGuestAccount = SCE_NO_VALUE;
  102. pProfileInfo->LSAAnonymousNameLookup = SCE_NO_VALUE;
  103. pProfileInfo->ClearTextPassword = SCE_NO_VALUE;
  104. pProfileInfo->AuditDSAccess = SCE_NO_VALUE;
  105. pProfileInfo->AuditAccountLogon = SCE_NO_VALUE;
  106. pProfileInfo->MaximumLogSize[0] = SCE_NO_VALUE;
  107. pProfileInfo->MaximumLogSize[1] = SCE_NO_VALUE;
  108. pProfileInfo->MaximumLogSize[2] = SCE_NO_VALUE;
  109. pProfileInfo->AuditLogRetentionPeriod[0] = SCE_NO_VALUE;
  110. pProfileInfo->AuditLogRetentionPeriod[1] = SCE_NO_VALUE;
  111. pProfileInfo->AuditLogRetentionPeriod[2] = SCE_NO_VALUE;
  112. pProfileInfo->RetentionDays[0] = SCE_NO_VALUE;
  113. pProfileInfo->RetentionDays[1] = SCE_NO_VALUE;
  114. pProfileInfo->RetentionDays[2] = SCE_NO_VALUE;
  115. pProfileInfo->RestrictGuestAccess[0] = SCE_NO_VALUE;
  116. pProfileInfo->RestrictGuestAccess[1] = SCE_NO_VALUE;
  117. pProfileInfo->RestrictGuestAccess[2] = SCE_NO_VALUE;
  118. pProfileInfo->AuditSystemEvents = SCE_NO_VALUE;
  119. pProfileInfo->AuditLogonEvents = SCE_NO_VALUE;
  120. pProfileInfo->AuditObjectAccess = SCE_NO_VALUE;
  121. pProfileInfo->AuditPrivilegeUse = SCE_NO_VALUE;
  122. pProfileInfo->AuditPolicyChange = SCE_NO_VALUE;
  123. pProfileInfo->AuditAccountManage = SCE_NO_VALUE;
  124. pProfileInfo->AuditProcessTracking = SCE_NO_VALUE;
  125. pProfileInfo->pInfo=NULL;
  126. pProfileInfo->pRIMinimumPasswordAge=NULL;
  127. pProfileInfo->pRIMaximumPasswordAge=NULL;
  128. pProfileInfo->pRIMinimumPasswordLength=NULL;
  129. pProfileInfo->pRIPasswordComplexity=NULL;
  130. pProfileInfo->pRIPasswordHistorySize=NULL;
  131. pProfileInfo->pRILockoutBadCount=NULL;
  132. pProfileInfo->pRIResetLockoutCount=NULL;
  133. pProfileInfo->pRILockoutDuration=NULL;
  134. pProfileInfo->pRIRequireLogonToChangePassword=NULL;
  135. pProfileInfo->pRIForceLogoffWhenHourExpire=NULL;
  136. pProfileInfo->pRIEnableAdminAccount=NULL;
  137. pProfileInfo->pRIEnableGuestAccount=NULL;
  138. pProfileInfo->pRILSAAnonymousNameLookup=NULL;
  139. pProfileInfo->pRINewAdministratorName=NULL;
  140. pProfileInfo->pRINewGuestName=NULL;
  141. pProfileInfo->pRIClearTextPassword=NULL;
  142. pProfileInfo->pRIMaxTicketAge=NULL;
  143. pProfileInfo->pRIMaxRenewAge=NULL;
  144. pProfileInfo->pRIMaxServiceAge=NULL;
  145. pProfileInfo->pRIMaxClockSkew=NULL;
  146. pProfileInfo->pRITicketValidateClient=NULL;
  147. pProfileInfo->pRIAuditSystemEvents=NULL;
  148. pProfileInfo->pRIAuditLogonEvents=NULL;
  149. pProfileInfo->pRIAuditObjectAccess=NULL;
  150. pProfileInfo->pRIAuditPrivilegeUse=NULL;
  151. pProfileInfo->pRIAuditPolicyChange=NULL;
  152. pProfileInfo->pRIAuditAccountManage=NULL;
  153. pProfileInfo->pRIAuditProcessTracking=NULL;
  154. pProfileInfo->pRIAuditDSAccess=NULL;
  155. pProfileInfo->pRIAuditAccountLogon=NULL;
  156. pProfileInfo->pRIMaximumLogSize[0]=NULL;
  157. pProfileInfo->pRIMaximumLogSize[1]=NULL;
  158. pProfileInfo->pRIMaximumLogSize[2]=NULL;
  159. pProfileInfo->pRIAuditLogRetentionPeriod[0]=NULL;
  160. pProfileInfo->pRIAuditLogRetentionPeriod[1]=NULL;
  161. pProfileInfo->pRIAuditLogRetentionPeriod[2]=NULL;
  162. pProfileInfo->pRIRetentionDays[0]=NULL;
  163. pProfileInfo->pRIRetentionDays[1]=NULL;
  164. pProfileInfo->pRIRetentionDays[2]=NULL;
  165. pProfileInfo->pRIRestrictGuestAccess[0]=NULL;
  166. pProfileInfo->pRIRestrictGuestAccess[1]=NULL;
  167. pProfileInfo->pRIRestrictGuestAccess[2]=NULL;
  168. }
  169. VOID FreeList(list<PRSOP_INFO> * li)
  170. {
  171. for(list<PRSOP_INFO>::iterator i = li->begin();
  172. i != li->end();
  173. ++i )
  174. {
  175. FreeRI(*i);
  176. }
  177. li->erase(li->begin(),li->end());
  178. }
  179. VOID FreeVector(vector<PRSOP_INFO> * li)
  180. {
  181. for(vector<PRSOP_INFO>::iterator i = li->begin();
  182. i != li->end();
  183. ++i )
  184. {
  185. FreeRI(*i);
  186. }
  187. li->erase(li->begin(),li->end());
  188. }
  189. VOID FreeWMI_SCE_PROFILE_INFO(PWMI_SCE_PROFILE_INFO pProfileInfo)
  190. {
  191. if (!pProfileInfo) {
  192. return;
  193. }
  194. //TODO
  195. //Use the code to Free SCE_PROFILE_INFO
  196. FreeRI(pProfileInfo->pInfo);
  197. FreeRI(pProfileInfo->pRIMinimumPasswordAge);
  198. FreeRI(pProfileInfo->pRIMaximumPasswordAge);
  199. FreeRI(pProfileInfo->pRIMinimumPasswordLength);
  200. FreeRI(pProfileInfo->pRIPasswordComplexity);
  201. FreeRI(pProfileInfo->pRIPasswordHistorySize);
  202. FreeRI(pProfileInfo->pRILockoutBadCount);
  203. FreeRI(pProfileInfo->pRIResetLockoutCount);
  204. FreeRI(pProfileInfo->pRILockoutDuration);
  205. FreeRI(pProfileInfo->pRIRequireLogonToChangePassword);
  206. FreeRI(pProfileInfo->pRIForceLogoffWhenHourExpire);
  207. FreeRI(pProfileInfo->pRIEnableAdminAccount);
  208. FreeRI(pProfileInfo->pRIEnableGuestAccount);
  209. FreeRI(pProfileInfo->pRILSAAnonymousNameLookup);
  210. FreeRI(pProfileInfo->pRINewAdministratorName);
  211. FreeRI(pProfileInfo->pRINewGuestName);
  212. FreeRI(pProfileInfo->pRIClearTextPassword);
  213. FreeRI(pProfileInfo->pRIMaxTicketAge);
  214. FreeRI(pProfileInfo->pRIMaxRenewAge);
  215. FreeRI(pProfileInfo->pRIMaxServiceAge);
  216. FreeRI(pProfileInfo->pRIMaxClockSkew);
  217. FreeRI(pProfileInfo->pRITicketValidateClient);
  218. FreeRI(pProfileInfo->pRIAuditSystemEvents);
  219. FreeRI(pProfileInfo->pRIAuditLogonEvents);
  220. FreeRI(pProfileInfo->pRIAuditObjectAccess);
  221. FreeRI(pProfileInfo->pRIAuditPrivilegeUse);
  222. FreeRI(pProfileInfo->pRIAuditPolicyChange);
  223. FreeRI(pProfileInfo->pRIAuditAccountManage);
  224. FreeRI(pProfileInfo->pRIAuditProcessTracking);
  225. FreeRI(pProfileInfo->pRIAuditDSAccess);
  226. FreeRI(pProfileInfo->pRIAuditAccountLogon);
  227. FreeRI(pProfileInfo->pRIMaximumLogSize[0]);
  228. FreeRI(pProfileInfo->pRIMaximumLogSize[1]);
  229. FreeRI(pProfileInfo->pRIMaximumLogSize[2]);
  230. FreeRI(pProfileInfo->pRIAuditLogRetentionPeriod[0]);
  231. FreeRI(pProfileInfo->pRIAuditLogRetentionPeriod[1]);
  232. FreeRI(pProfileInfo->pRIAuditLogRetentionPeriod[2]);
  233. FreeRI(pProfileInfo->pRIRetentionDays[0]);
  234. FreeRI(pProfileInfo->pRIRetentionDays[1]);
  235. FreeRI(pProfileInfo->pRIRetentionDays[2]);
  236. FreeRI(pProfileInfo->pRIRestrictGuestAccess[0]);
  237. FreeRI(pProfileInfo->pRIRestrictGuestAccess[1]);
  238. FreeRI(pProfileInfo->pRIRestrictGuestAccess[2]);
  239. FreeList(&(pProfileInfo->listRIInfPrivilegeAssignedTo));
  240. FreeList(&(pProfileInfo->listRIGroupMemebership));
  241. FreeList(&(pProfileInfo->listRIServices));
  242. FreeVector(&(pProfileInfo->vecRIFiles));
  243. FreeVector(&(pProfileInfo->vecRIReg));
  244. SceFreeProfileMemory(pProfileInfo);
  245. }
  246. CWMIRsop::~CWMIRsop() {
  247. if (m_vecAllRSOPCache) {
  248. for(vector<PWMI_SCE_PROFILE_INFO>::iterator i = m_vecAllRSOPCache->begin();
  249. i != m_vecAllRSOPCache->end();
  250. ++i )
  251. {
  252. PWMI_SCE_PROFILE_INFO pProfileInfo = *i;
  253. FreeWMI_SCE_PROFILE_INFO(pProfileInfo);
  254. }
  255. }
  256. delete m_vecAllRSOPCache;
  257. m_vecAllRSOPCache = NULL; // be extra carefull because the old code is not to delete it at all.
  258. if (m_pSvc) {
  259. m_pSvc->Release();
  260. }
  261. }
  262. HRESULT CWMIRsop::Initialize()
  263. {
  264. HRESULT hr = S_OK;
  265. IWbemLocator *pLoc = NULL;
  266. //Already initialized
  267. if(m_pSvc)
  268. return hr;
  269. if (!m_pRSOPInformation)
  270. {
  271. return E_FAIL;
  272. }
  273. //This is a safe usage.
  274. hr = CoCreateInstance(CLSID_WbemLocator,
  275. 0,
  276. CLSCTX_INPROC_SERVER,
  277. IID_IWbemLocator,
  278. (LPVOID *) &pLoc);
  279. if (FAILED(hr))
  280. {
  281. return hr;
  282. }
  283. const int cchMaxLength = 512;
  284. WCHAR szNameSpace[cchMaxLength];//LPOLESTR pszNameSpace = (LPOLESTR) LocalAlloc (LPTR, cchMaxLength * sizeof (WCHAR));
  285. hr = m_pRSOPInformation->GetNamespace (
  286. GPO_SECTION_MACHINE,
  287. szNameSpace,
  288. cchMaxLength);
  289. szNameSpace[cchMaxLength - 1] = L'\0';
  290. if (SUCCEEDED(hr))
  291. {
  292. BSTR bstrNameSpace = SysAllocString (szNameSpace);
  293. if (bstrNameSpace)
  294. {
  295. hr = pLoc->ConnectServer(bstrNameSpace,
  296. NULL,
  297. NULL,
  298. 0,
  299. NULL,
  300. 0,
  301. 0,
  302. &m_pSvc
  303. );
  304. SysFreeString(bstrNameSpace);
  305. }
  306. else
  307. hr = E_OUTOFMEMORY;
  308. }
  309. if (SUCCEEDED(hr))
  310. {
  311. // Set the proxy so that impersonation of the client occurs.
  312. hr = CoSetProxyBlanket(m_pSvc,
  313. RPC_C_AUTHN_WINNT,
  314. RPC_C_AUTHZ_NONE,
  315. NULL,
  316. RPC_C_AUTHN_LEVEL_CALL,
  317. RPC_C_IMP_LEVEL_IMPERSONATE,
  318. NULL,
  319. EOAC_NONE);
  320. }
  321. pLoc->Release();
  322. return hr; // Program successfully completed.
  323. }
  324. HRESULT CWMIRsop::EnumeratePrecedenceOne(IEnumWbemClassObject **ppEnum)
  325. {
  326. HRESULT hr = m_pSvc->ExecQuery(_bstr_t(QUERY_LANG),
  327. _bstr_t(RSOP_PREC_ONE_QUERY),
  328. WBEM_FLAG_FORWARD_ONLY,
  329. NULL,
  330. ppEnum);
  331. return hr;
  332. }
  333. HRESULT CWMIRsop::EnumerateAll(IEnumWbemClassObject **ppEnum)
  334. {
  335. HRESULT hr = m_pSvc->ExecQuery(_bstr_t(QUERY_LANG),
  336. _bstr_t(RSOP_ALL_QUERY),
  337. WBEM_FLAG_FORWARD_ONLY,
  338. NULL,
  339. ppEnum);
  340. return hr;
  341. }
  342. HRESULT CWMIRsop::GetNextInstance(IEnumWbemClassObject *pEnum,
  343. IWbemClassObject** rsopInstance)
  344. {
  345. HRESULT hr = (HRESULT)WBEM_S_FALSE; //Prefast warning: Implicit cast between semantically different integer types. Comments: It is expected.
  346. ULONG returnedNum = 0;
  347. if(pEnum)
  348. {
  349. hr = pEnum->Next(WBEM_INFINITE,
  350. 1,
  351. rsopInstance,
  352. &returnedNum);
  353. if( FAILED(hr) )
  354. return hr;
  355. if (returnedNum == 0)
  356. hr = (HRESULT)WBEM_S_FALSE; //Prefast warning: Implicit cast between semantically different integer types. Comments: It is expected.
  357. }
  358. return hr;
  359. }
  360. HRESULT CWMIRsop::GetClass(IWbemClassObject* rsopInstance,
  361. LPWSTR *ppClass)
  362. {
  363. PTSTR pszClassName;
  364. VARIANT value;
  365. HRESULT hr = rsopInstance->Get(_bstr_t(WMI_CLASS),
  366. 0,
  367. &value,
  368. NULL,
  369. NULL);
  370. if(FAILED(hr) || value.vt != VT_BSTR || value.bstrVal == NULL)
  371. return hr;
  372. pszClassName = (PTSTR) V_BSTR(&value);
  373. ULONG uLen = wcslen(pszClassName);
  374. *ppClass = (LPWSTR)LocalAlloc(LPTR, (uLen + 1) * sizeof(WCHAR));
  375. if( *ppClass == NULL )
  376. {
  377. VariantClear(&value);
  378. return E_OUTOFMEMORY;
  379. }
  380. //This may not be a safe usage. pszClassName is PTSTR. Consider fix.
  381. wcscpy(*ppClass,pszClassName);
  382. VariantClear(&value);
  383. return hr;
  384. }
  385. HRESULT CWMIRsop::GetRSOPInfo(IWbemClassObject *rsopInstance,
  386. PRSOP_INFO pInfo)
  387. {
  388. HRESULT hr = S_OK;
  389. PTSTR pszGPOID = 0;
  390. UINT status = 0;
  391. UINT precedence = 0;
  392. VARIANT value;
  393. //Get Status
  394. hr = rsopInstance->Get(_bstr_t(RSOP_STATUS),
  395. 0,
  396. &value,
  397. NULL,
  398. NULL);
  399. if(FAILED(hr))
  400. return hr;
  401. pInfo->status = (ULONG) V_UINT(&value);
  402. VariantClear(&value);
  403. //Get Error
  404. hr = rsopInstance->Get(_bstr_t(RSOP_ERROR),
  405. 0,
  406. &value,
  407. NULL,
  408. NULL);
  409. if(FAILED(hr))
  410. return hr;
  411. pInfo->error = (ULONG) V_UINT(&value);
  412. VariantClear(&value);
  413. //Get Precedence
  414. hr = rsopInstance->Get(_bstr_t(RSOP_PRECEDENCE),
  415. 0,
  416. &value,
  417. NULL,
  418. NULL);
  419. if(FAILED(hr))
  420. return hr;
  421. pInfo->precedence = (ULONG) V_UINT(&value);
  422. VariantClear(&value);
  423. //Get GPOID
  424. hr = rsopInstance->Get(_bstr_t(RSOP_GPOID),
  425. 0,
  426. &value,
  427. NULL,
  428. NULL);
  429. if(FAILED(hr) || value.vt != VT_BSTR || (value.vt == VT_BSTR && value.bstrVal == NULL) )
  430. return hr;
  431. pszGPOID = (PTSTR) V_BSTR(&value);
  432. ULONG uLen = wcslen(pszGPOID);
  433. pInfo->pszGPOID = (LPWSTR)LocalAlloc(LPTR, (uLen + 1) * sizeof(WCHAR));
  434. if( pInfo->pszGPOID == NULL )
  435. {
  436. VariantClear(&value);
  437. return E_OUTOFMEMORY;
  438. }
  439. //This may not be a safe usage. pszGPOID is PTSTR. Consider fix.
  440. wcscpy(pInfo->pszGPOID, pszGPOID);
  441. VariantClear(&value);
  442. return hr;
  443. }
  444. HRESULT CWMIRsop::GetPrecedenceOneRSOPInfo(PWMI_SCE_PROFILE_INFO *ppProfileInfo)
  445. {
  446. HRESULT hr = S_OK;
  447. IWbemClassObject *rsopInstance = NULL;
  448. IEnumWbemClassObject *pEnumObject = NULL;
  449. PWMI_SCE_PROFILE_INFO pTempProfileInfo = NULL;
  450. PRSOP_INFO pInfo = NULL;
  451. hr = Initialize();
  452. if( FAILED(hr) )
  453. goto exit_gracefully;
  454. hr = EnumeratePrecedenceOne(&pEnumObject);
  455. if( FAILED(hr) )
  456. goto exit_gracefully;
  457. pTempProfileInfo = new WMI_SCE_PROFILE_INFO;
  458. if( !pTempProfileInfo )
  459. {
  460. hr = E_OUTOFMEMORY;
  461. goto exit_gracefully;
  462. }
  463. InitWMI_SEC_PROFILE_INFO(pTempProfileInfo);
  464. //Get each instance
  465. //Prefast warning: Implicit cast between semantically different integer types. Comments: It is expected.
  466. while( ((hr = GetNextInstance(pEnumObject, &rsopInstance)) != (HRESULT)WBEM_S_FALSE) && !FAILED(hr) )
  467. {
  468. pInfo = (PRSOP_INFO)LocalAlloc(LPTR, sizeof(RSOP_INFO));
  469. if(pInfo == NULL)
  470. {
  471. hr = E_OUTOFMEMORY;
  472. goto exit_gracefully;
  473. }
  474. //Get RSOP_INFO
  475. hr = GetRSOPInfo(rsopInstance, pInfo);
  476. if( FAILED(hr) )
  477. goto exit_gracefully;
  478. hr = AddInstance(rsopInstance,
  479. pInfo,
  480. pTempProfileInfo);
  481. if( FAILED(hr) )
  482. goto exit_gracefully;
  483. rsopInstance->Release();
  484. rsopInstance = NULL;
  485. pInfo = NULL;
  486. }
  487. exit_gracefully:
  488. if(FAILED(hr))
  489. {
  490. if (rsopInstance)
  491. rsopInstance->Release(); // if while loop somehow terminate to here, rsopInstance is never released there
  492. if(pEnumObject)
  493. pEnumObject->Release();
  494. FreeRI(pInfo);
  495. //Free pTempProfileInfo
  496. FreeWMI_SCE_PROFILE_INFO(pTempProfileInfo);
  497. pTempProfileInfo = NULL;
  498. }
  499. *ppProfileInfo = pTempProfileInfo;
  500. return hr;
  501. }
  502. PWMI_SCE_PROFILE_INFO SearchProfileInList(vector<PWMI_SCE_PROFILE_INFO> *vecInfo,
  503. PRSOP_INFO pInfo)
  504. {
  505. for(vector<PWMI_SCE_PROFILE_INFO>::iterator i = vecInfo->begin();
  506. i != vecInfo->end();
  507. ++i )
  508. {
  509. PWMI_SCE_PROFILE_INFO pProfileInfo = *i;
  510. if(_wcsicmp(pProfileInfo->pInfo->pszGPOID,pInfo->pszGPOID) == 0 )
  511. return pProfileInfo;
  512. }
  513. return NULL;
  514. }
  515. //Function Object Used for sorting vector
  516. struct less_mag : public binary_function<PWMI_SCE_PROFILE_INFO, PWMI_SCE_PROFILE_INFO, bool> {
  517. bool operator()(PWMI_SCE_PROFILE_INFO x, PWMI_SCE_PROFILE_INFO y)
  518. { return x->pInfo->precedence < y->pInfo->precedence; }
  519. };
  520. HRESULT CWMIRsop::GetAllRSOPInfo(vector<PWMI_SCE_PROFILE_INFO> *vecInfo)
  521. {
  522. HRESULT hr = S_OK;
  523. IWbemClassObject *rsopInstance = NULL;
  524. IEnumWbemClassObject *pEnumObject = NULL;
  525. PRSOP_INFO pInfo = NULL;
  526. PWMI_SCE_PROFILE_INFO pProfileInfo = NULL;
  527. if (NULL == vecInfo) {
  528. return E_INVALIDARG;
  529. }
  530. //
  531. // If we've alrady cached the info from WMI then just return it
  532. // don't try and get it again
  533. //
  534. if (m_vecAllRSOPCache)
  535. {
  536. for(vector<PWMI_SCE_PROFILE_INFO>::iterator i = m_vecAllRSOPCache->begin();
  537. i != m_vecAllRSOPCache->end();
  538. ++i )
  539. {
  540. vecInfo->push_back(*i);
  541. }
  542. return S_OK;
  543. }
  544. hr = Initialize();
  545. if( FAILED(hr) )
  546. goto exit_gracefully;
  547. hr = EnumerateAll(&pEnumObject);
  548. if( FAILED(hr) )
  549. goto exit_gracefully;
  550. //Get each instance
  551. //Prefast warning: Implicit cast between semantically different integer types. Comments: It is expected.
  552. while( ((hr = GetNextInstance(pEnumObject, &rsopInstance)) != (HRESULT)WBEM_S_FALSE) && !FAILED(hr) )
  553. {
  554. pInfo = (PRSOP_INFO)LocalAlloc(LPTR, sizeof(RSOP_INFO));
  555. if(pInfo == NULL)
  556. {
  557. hr = E_OUTOFMEMORY;
  558. goto exit_gracefully;
  559. }
  560. //Get RSOP_INFO
  561. hr = GetRSOPInfo(rsopInstance, pInfo);
  562. if( FAILED(hr) )
  563. goto exit_gracefully;
  564. pProfileInfo = SearchProfileInList(vecInfo,pInfo);
  565. if(!pProfileInfo)
  566. {
  567. pProfileInfo = new WMI_SCE_PROFILE_INFO;
  568. if(!pProfileInfo)
  569. {
  570. hr = E_OUTOFMEMORY;
  571. goto exit_gracefully;
  572. }
  573. InitWMI_SEC_PROFILE_INFO(pProfileInfo);
  574. pProfileInfo->pInfo = (PRSOP_INFO)LocalAlloc(LPTR,sizeof(RSOP_INFO));
  575. if(!pProfileInfo->pInfo)
  576. {
  577. hr = E_OUTOFMEMORY;
  578. goto exit_gracefully;
  579. }
  580. pProfileInfo->pInfo->pszGPOID = (LPWSTR)LocalAlloc(LPTR, (wcslen(pInfo->pszGPOID)+1)*sizeof(WCHAR));
  581. if(!pProfileInfo->pInfo->pszGPOID)
  582. {
  583. hr = E_OUTOFMEMORY;
  584. goto exit_gracefully;
  585. }
  586. //This is a safe usage.
  587. wcscpy(pProfileInfo->pInfo->pszGPOID,pInfo->pszGPOID);
  588. vecInfo->push_back(pProfileInfo);
  589. }
  590. if( pProfileInfo->pInfo->precedence < pInfo->precedence )
  591. pProfileInfo->pInfo->precedence = pInfo->precedence;
  592. hr = AddInstance(rsopInstance,
  593. pInfo,
  594. pProfileInfo);
  595. if(FAILED(hr))
  596. goto exit_gracefully;
  597. rsopInstance->Release();
  598. rsopInstance = NULL;
  599. pInfo = NULL;
  600. }
  601. sort(vecInfo->begin(),vecInfo->end(),less_mag());
  602. m_vecAllRSOPCache = new vector<PWMI_SCE_PROFILE_INFO>;
  603. if (m_vecAllRSOPCache)
  604. {
  605. for(vector<PWMI_SCE_PROFILE_INFO>::iterator i = vecInfo->begin();
  606. i != vecInfo->end();
  607. ++i )
  608. {
  609. m_vecAllRSOPCache->push_back(*i);
  610. }
  611. }
  612. exit_gracefully:
  613. if(FAILED(hr))
  614. {
  615. FreeRI(pInfo);
  616. //Free the vector
  617. for(vector<PWMI_SCE_PROFILE_INFO>::iterator i = vecInfo->begin();
  618. i != vecInfo->end();
  619. ++i )
  620. {
  621. PWMI_SCE_PROFILE_INFO pProfileInfoLoc = *i; //Raid #prefast
  622. FreeWMI_SCE_PROFILE_INFO(pProfileInfoLoc);
  623. }
  624. }
  625. if(pEnumObject)
  626. pEnumObject->Release();
  627. if(rsopInstance)
  628. rsopInstance->Release();
  629. return hr;
  630. }
  631. HRESULT CWMIRsop::AddNumericSetting(IWbemClassObject *rsopInstance,
  632. PRSOP_INFO pInfo,
  633. PWMI_SCE_PROFILE_INFO pProfileInfo)
  634. {
  635. HRESULT hr = S_OK;
  636. VARIANT bKeyName;
  637. PTSTR keyName;
  638. VARIANT bSettingValue;
  639. DWORD settingValue;
  640. _TRACE (1, L"Entering CWMIRsop::AddNumericSetting\n");
  641. hr = rsopInstance->Get((BSTR)RSOP_KEYNAME,
  642. 0,
  643. &bKeyName,
  644. NULL,
  645. NULL);
  646. if( FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL)
  647. goto exit_gracefully;
  648. hr = rsopInstance->Get((BSTR)RSOP_SETTING,
  649. 0,
  650. &bSettingValue,
  651. NULL,
  652. NULL);
  653. if( FAILED(hr) )
  654. goto exit_gracefully;
  655. keyName = V_BSTR(&bKeyName);
  656. settingValue = V_UINT(&bSettingValue);
  657. if(!_wcsicmp(keyName, MIN_PASS_AGE))
  658. {
  659. pProfileInfo->MinimumPasswordAge = settingValue;
  660. pProfileInfo->pRIMinimumPasswordAge = pInfo;
  661. }
  662. else if(!_wcsicmp(keyName, MAX_PASS_AGE))
  663. {
  664. pProfileInfo->MaximumPasswordAge = settingValue;
  665. pProfileInfo->pRIMaximumPasswordAge = pInfo;
  666. }
  667. else if(!_wcsicmp(keyName, MIN_PASS_LEN))
  668. {
  669. pProfileInfo->MinimumPasswordLength = settingValue;
  670. pProfileInfo->pRIMinimumPasswordLength = pInfo;
  671. }
  672. else if(!_wcsicmp(keyName, PASS_HIS_SIZE))
  673. {
  674. pProfileInfo->PasswordHistorySize = settingValue;
  675. pProfileInfo->pRIPasswordHistorySize = pInfo;
  676. }
  677. else if(!_wcsicmp(keyName, LOCKOUT_COUNT))
  678. {
  679. pProfileInfo->LockoutBadCount = settingValue;
  680. pProfileInfo->pRILockoutBadCount = pInfo;
  681. }
  682. else if(!_wcsicmp(keyName, RESET_LOCKOUT_COUNT))
  683. {
  684. pProfileInfo->ResetLockoutCount = settingValue;
  685. pProfileInfo->pRIResetLockoutCount = pInfo;
  686. }
  687. else if(!_wcsicmp(keyName, LOCKOUT_DURATION))
  688. {
  689. pProfileInfo->LockoutDuration = settingValue;
  690. pProfileInfo->pRILockoutDuration = pInfo;
  691. }
  692. else if(!_wcsicmp(keyName, MAX_TICKET_AGE))
  693. {
  694. if(!pProfileInfo->pKerberosInfo)
  695. {
  696. pProfileInfo->pKerberosInfo =
  697. (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR, sizeof(SCE_KERBEROS_TICKET_INFO));
  698. if(pProfileInfo->pKerberosInfo == NULL)
  699. {
  700. hr = E_OUTOFMEMORY;
  701. goto exit_gracefully;
  702. }
  703. }
  704. pProfileInfo->pKerberosInfo->MaxTicketAge = settingValue;
  705. pProfileInfo->pRIMaxTicketAge = pInfo;
  706. }
  707. else if(!_wcsicmp(keyName, MAX_RENEW_AGE))
  708. {
  709. if(!pProfileInfo->pKerberosInfo)
  710. {
  711. pProfileInfo->pKerberosInfo =
  712. (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR, sizeof(SCE_KERBEROS_TICKET_INFO));
  713. if(pProfileInfo->pKerberosInfo == NULL)
  714. {
  715. hr = E_OUTOFMEMORY;
  716. goto exit_gracefully;
  717. }
  718. }
  719. pProfileInfo->pKerberosInfo->MaxRenewAge = settingValue;
  720. pProfileInfo->pRIMaxRenewAge = pInfo;
  721. }
  722. else if(!_wcsicmp(keyName, MAX_SERVICE_AGE))
  723. {
  724. if(!pProfileInfo->pKerberosInfo)
  725. {
  726. pProfileInfo->pKerberosInfo =
  727. (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR, sizeof(SCE_KERBEROS_TICKET_INFO));
  728. if(pProfileInfo->pKerberosInfo == NULL)
  729. {
  730. hr = E_OUTOFMEMORY;
  731. goto exit_gracefully;
  732. }
  733. }
  734. pProfileInfo->pKerberosInfo->MaxServiceAge = settingValue;
  735. pProfileInfo->pRIMaxServiceAge = pInfo;
  736. }
  737. else if(!_wcsicmp(keyName, MAX_CLOCK_SKEW))
  738. {
  739. if(!pProfileInfo->pKerberosInfo)
  740. {
  741. pProfileInfo->pKerberosInfo =
  742. (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR, sizeof(SCE_KERBEROS_TICKET_INFO));
  743. if(pProfileInfo->pKerberosInfo == NULL)
  744. {
  745. hr = E_OUTOFMEMORY;
  746. goto exit_gracefully;
  747. }
  748. }
  749. pProfileInfo->pKerberosInfo->MaxClockSkew = settingValue;
  750. pProfileInfo->pRIMaxClockSkew = pInfo;
  751. }
  752. else
  753. {
  754. _ASSERT (FALSE); // key name not accounted for
  755. }
  756. exit_gracefully:
  757. VariantClear(&bKeyName);
  758. VariantClear(&bSettingValue);
  759. _TRACE (-1,L"Leaving CWMIRsop::AddNumericSetting\n");
  760. return hr;
  761. }
  762. HRESULT CWMIRsop::AddEventLogNumericSetting(IWbemClassObject *rsopInstance,
  763. PRSOP_INFO pInfo,
  764. PWMI_SCE_PROFILE_INFO pProfileInfo)
  765. {
  766. VARIANT bKeyName;
  767. VARIANT bSettingValue;
  768. VARIANT bType;
  769. _TRACE (1, L"Entering CWMIRsop::AddEventLogNumericSetting\n");
  770. HRESULT hr = rsopInstance->Get((BSTR)RSOP_KEYNAME,
  771. 0,
  772. &bKeyName,
  773. NULL,
  774. NULL);
  775. if( FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL)
  776. goto exit_gracefully;
  777. hr = rsopInstance->Get((BSTR)RSOP_SETTING,
  778. 0,
  779. &bSettingValue,
  780. NULL,
  781. NULL);
  782. if( FAILED(hr) )
  783. goto exit_gracefully;
  784. hr = rsopInstance->Get((BSTR)RSOP_EVENTLOG_TYPE,
  785. 0,
  786. &bType,
  787. NULL,
  788. NULL);
  789. if( FAILED(hr) )
  790. goto exit_gracefully;
  791. PTSTR keyName = V_BSTR(&bKeyName);
  792. DWORD settingValue = V_UINT(&bSettingValue);
  793. PTSTR typeValue = V_BSTR(&bType);
  794. unsigned long ulType = wcstoul (typeValue, L'\0', 10);
  795. ASSERT (ulType <= 2);
  796. if ( ulType <= 2 )
  797. {
  798. if ( !_wcsicmp(keyName, MAX_LOG_SIZE) )
  799. {
  800. pProfileInfo->MaximumLogSize[ulType] = settingValue;
  801. pProfileInfo->pRIMaximumLogSize[ulType] = pInfo;
  802. }
  803. else if ( !_wcsicmp(keyName, AUDIT_LOG_RETENTION_PERIOD) )
  804. {
  805. pProfileInfo->AuditLogRetentionPeriod[ulType] = settingValue;
  806. pProfileInfo->pRIAuditLogRetentionPeriod[ulType] = pInfo;
  807. }
  808. else if ( !_wcsicmp(keyName, RETENTION_DAYS) )
  809. {
  810. //Raid 599943, yanggao.
  811. //if RetentionDays is 0xffffffff then the retention method is "don't overwrite ..." and RetentionDays setting is not defined.
  812. //if RetentionDays is 0 then retention method is "overwrite as needed ..." and RetentionDays setting is not defined.
  813. //if RetentionDays has another value then retention method is "by days" and the number of days is the value for RetentionDays setting.
  814. //pProfileInfo->RetentionDays[ulType] = settingValue;
  815. //pProfileInfo->pRIRetentionDays[ulType] = pInfo;
  816. //Set related method setting.
  817. PRSOP_INFO pMethodInfo = NULL;
  818. pMethodInfo = (PRSOP_INFO)LocalAlloc(LPTR, sizeof(RSOP_INFO));
  819. if(pMethodInfo == NULL)
  820. {
  821. hr = E_OUTOFMEMORY;
  822. goto exit_gracefully;
  823. }
  824. hr = GetRSOPInfo(rsopInstance, pMethodInfo);
  825. if( FAILED(hr) )
  826. {
  827. FreeRI(pMethodInfo);
  828. goto exit_gracefully;
  829. }
  830. switch(settingValue)
  831. {
  832. case 0:
  833. pProfileInfo->AuditLogRetentionPeriod[ulType] = SCE_RETAIN_AS_NEEDED;
  834. FreeRI(pInfo);
  835. break;
  836. case SCE_FOREVER_VALUE:
  837. pProfileInfo->AuditLogRetentionPeriod[ulType] = SCE_RETAIN_MANUALLY;
  838. FreeRI(pInfo);
  839. break;
  840. default:
  841. pProfileInfo->AuditLogRetentionPeriod[ulType] = SCE_RETAIN_BY_DAYS;
  842. pProfileInfo->RetentionDays[ulType] = settingValue;
  843. pProfileInfo->pRIRetentionDays[ulType] = pInfo;
  844. break;
  845. }
  846. pProfileInfo->pRIAuditLogRetentionPeriod[ulType] = pMethodInfo;
  847. pMethodInfo = NULL;
  848. }
  849. else
  850. {
  851. _ASSERT (FALSE); // key name not accounted for
  852. }
  853. }
  854. else
  855. hr = E_FAIL;
  856. exit_gracefully:
  857. VariantClear (&bKeyName);
  858. VariantClear (&bSettingValue);
  859. VariantClear (&bType);
  860. _TRACE (-1,L"Leaving CWMIRsop::AddEventLogNumericSetting\n");
  861. return hr;
  862. }
  863. HRESULT CWMIRsop::AddEventLogBooleanSetting(IWbemClassObject *rsopInstance,
  864. PRSOP_INFO pInfo,
  865. PWMI_SCE_PROFILE_INFO pProfileInfo)
  866. {
  867. HRESULT hr = S_OK;
  868. VARIANT bKeyName;
  869. PTSTR keyName = 0;
  870. VARIANT bSettingValue;
  871. DWORD settingValue;
  872. BOOL boolVal = FALSE;
  873. VARIANT bType;
  874. PTSTR typeValue = 0;
  875. _TRACE (1, L"Entering CWMIRsop::AddEventLogBooleanSetting\n");
  876. hr = rsopInstance->Get((BSTR)RSOP_KEYNAME,
  877. 0,
  878. &bKeyName,
  879. NULL,
  880. NULL);
  881. if( FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL)
  882. goto exit_gracefully;
  883. hr = rsopInstance->Get((BSTR)RSOP_SETTING,
  884. 0,
  885. &bSettingValue,
  886. NULL,
  887. NULL);
  888. if( FAILED(hr) )
  889. goto exit_gracefully;
  890. hr = rsopInstance->Get((BSTR)RSOP_EVENTLOG_TYPE,
  891. 0,
  892. &bType,
  893. NULL,
  894. NULL);
  895. if( FAILED(hr) )
  896. goto exit_gracefully;
  897. keyName = V_BSTR(&bKeyName);
  898. boolVal = V_BOOL(&bSettingValue);
  899. settingValue = (boolVal)? 1:0;
  900. typeValue = V_BSTR(&bType);
  901. unsigned long ulType = wcstoul (typeValue, L'\0', 10);
  902. ASSERT (ulType <= 2);
  903. if ( ulType <= 2 )
  904. {
  905. if ( !_wcsicmp(keyName, RESTRICT_GUEST_ACCESS) )
  906. {
  907. pProfileInfo->RestrictGuestAccess[ulType] = settingValue;
  908. pProfileInfo->pRIRestrictGuestAccess[ulType] = pInfo;
  909. }
  910. else
  911. {
  912. _ASSERT (FALSE); // key name not accounted for
  913. }
  914. }
  915. else
  916. hr = E_FAIL;
  917. exit_gracefully:
  918. VariantClear (&bKeyName);
  919. VariantClear (&bSettingValue);
  920. VariantClear (&bType);
  921. _TRACE (-1,L"Leaving CWMIRsop::AddEventLogBooleanSetting\n");
  922. return hr;
  923. }
  924. HRESULT CWMIRsop::AddBooleanSetting(IWbemClassObject *rsopInstance,
  925. PRSOP_INFO pInfo,
  926. PWMI_SCE_PROFILE_INFO pProfileInfo)
  927. {
  928. VARIANT bKeyName;
  929. PTSTR keyName;
  930. VARIANT bSettingValue;
  931. BOOL boolVal;
  932. DWORD settingValue;
  933. HRESULT hr = S_OK;
  934. _TRACE (1, L"Entering CWMIRsop::AddBooleanSetting\n");
  935. hr = rsopInstance->Get((BSTR)RSOP_KEYNAME,
  936. 0,
  937. &bKeyName,
  938. NULL,
  939. NULL);
  940. if(FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL)
  941. goto exit_gracefully;
  942. hr = rsopInstance->Get((BSTR)RSOP_SETTING,
  943. 0,
  944. &bSettingValue,
  945. NULL,
  946. NULL);
  947. if(FAILED(hr))
  948. goto exit_gracefully;
  949. keyName = (PTSTR) V_BSTR(&bKeyName);
  950. boolVal = V_BOOL(&bSettingValue);
  951. settingValue = (boolVal)? 1:0;
  952. if(!_wcsicmp(keyName, PASS_COMPLEX))
  953. {
  954. pProfileInfo->PasswordComplexity = settingValue;
  955. pProfileInfo->pRIPasswordComplexity = pInfo;
  956. }
  957. else if(!_wcsicmp(keyName, FORCE_LOGOFF))
  958. {
  959. pProfileInfo->ForceLogoffWhenHourExpire = settingValue;
  960. pProfileInfo->pRIForceLogoffWhenHourExpire = pInfo;
  961. }
  962. else if(!_wcsicmp(keyName, ENABLE_ADMIN))
  963. {
  964. pProfileInfo->EnableAdminAccount = settingValue;
  965. pProfileInfo->pRIEnableAdminAccount = pInfo;
  966. }
  967. else if(!_wcsicmp(keyName, ENABLE_GUEST))
  968. {
  969. pProfileInfo->EnableGuestAccount = settingValue;
  970. pProfileInfo->pRIEnableGuestAccount = pInfo;
  971. }
  972. else if(!_wcsicmp(keyName, LSA_ANON_LOOKUP))
  973. {
  974. pProfileInfo->LSAAnonymousNameLookup = settingValue;
  975. pProfileInfo->pRILSAAnonymousNameLookup = pInfo;
  976. }
  977. else if(!_wcsicmp(keyName, CLEAR_TEXT_PASS))
  978. {
  979. pProfileInfo->ClearTextPassword = settingValue;
  980. pProfileInfo->pRIClearTextPassword = pInfo;
  981. }
  982. else if (!_wcsicmp(keyName, REQUIRE_LOGON_TO_CHANGE_PASS))
  983. {
  984. pProfileInfo->RequireLogonToChangePassword = settingValue;
  985. pProfileInfo->pRIRequireLogonToChangePassword = pInfo;
  986. }
  987. else if(!_wcsicmp(keyName, VALIDATE_CLIENT))
  988. {
  989. if(!pProfileInfo->pKerberosInfo)
  990. {
  991. pProfileInfo->pKerberosInfo =
  992. (PSCE_KERBEROS_TICKET_INFO) LocalAlloc(LPTR, sizeof(SCE_KERBEROS_TICKET_INFO));
  993. if(pProfileInfo->pKerberosInfo == NULL)
  994. {
  995. hr = E_OUTOFMEMORY;
  996. goto exit_gracefully;
  997. }
  998. }
  999. pProfileInfo->pKerberosInfo->TicketValidateClient = settingValue;
  1000. pProfileInfo->pRITicketValidateClient = pInfo;
  1001. }
  1002. else
  1003. {
  1004. _ASSERT (FALSE); // key name not accounted for
  1005. }
  1006. exit_gracefully:
  1007. VariantClear(&bKeyName);
  1008. VariantClear(&bSettingValue);
  1009. _TRACE (-1,L"Leaving CWMIRsop::AddBooleanSetting\n");
  1010. return hr;
  1011. }
  1012. HRESULT CWMIRsop::AddAuditSetting(IWbemClassObject *rsopInstance,
  1013. PRSOP_INFO pInfo,
  1014. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1015. {
  1016. VARIANT bCategoryName;
  1017. PTSTR categoryName;
  1018. VARIANT vSuccessVal;
  1019. VARIANT vFailVal;
  1020. BOOL successVal;
  1021. BOOL failVal;
  1022. DWORD settingVal = 0;
  1023. HRESULT hr = S_OK;
  1024. _TRACE (1, L"Entering CWMIRsop::AddAuditSetting\n");
  1025. hr = rsopInstance->Get(_bstr_t(TEXT("Category")),
  1026. 0,
  1027. &bCategoryName,
  1028. NULL,
  1029. NULL
  1030. );
  1031. if(FAILED(hr) || bCategoryName.vt != VT_BSTR || bCategoryName.bstrVal == NULL)
  1032. goto exit_gracefully;
  1033. hr = rsopInstance->Get(_bstr_t(TEXT("Success")),
  1034. 0,
  1035. &vSuccessVal,
  1036. NULL,
  1037. NULL
  1038. );
  1039. if(FAILED(hr))
  1040. goto exit_gracefully;
  1041. hr = rsopInstance->Get(_bstr_t(TEXT("Failure")),
  1042. 0,
  1043. &vFailVal,
  1044. NULL,
  1045. NULL
  1046. );
  1047. if(FAILED(hr))
  1048. goto exit_gracefully;
  1049. categoryName = (PTSTR) V_BSTR(&bCategoryName);
  1050. successVal = V_BOOL(&vSuccessVal);
  1051. failVal = V_BOOL(&vFailVal);
  1052. if (successVal)
  1053. settingVal |= 1;
  1054. if (failVal)
  1055. settingVal |= 2;
  1056. if(!_wcsicmp(categoryName, AUDIT_SYSTEM_EVENTS))
  1057. {
  1058. pProfileInfo->AuditSystemEvents = settingVal;
  1059. pProfileInfo->pRIAuditSystemEvents = pInfo;
  1060. }
  1061. else if(!_wcsicmp(categoryName, AUDIT_LOGON_EVENTS))
  1062. {
  1063. pProfileInfo->AuditLogonEvents = settingVal;
  1064. pProfileInfo->pRIAuditLogonEvents = pInfo;
  1065. }
  1066. else if(!_wcsicmp(categoryName, AUDIT_OBJECT_ACCESS))
  1067. {
  1068. pProfileInfo->AuditObjectAccess = settingVal;
  1069. pProfileInfo->pRIAuditObjectAccess = pInfo;
  1070. }
  1071. else if (!_wcsicmp(categoryName, AUDIT_PRIVILEGE_USE))
  1072. {
  1073. pProfileInfo->AuditPrivilegeUse = settingVal;
  1074. pProfileInfo->pRIAuditPrivilegeUse = pInfo;
  1075. }
  1076. else if(!_wcsicmp(categoryName, AUDIT_POLICY_CHANGE))
  1077. {
  1078. pProfileInfo->AuditPolicyChange = settingVal;
  1079. pProfileInfo->pRIAuditPolicyChange = pInfo;
  1080. }
  1081. else if(!_wcsicmp(categoryName, AUDIT_ACCOUNT_MANAGE))
  1082. {
  1083. pProfileInfo->AuditAccountManage = settingVal;
  1084. pProfileInfo->pRIAuditAccountManage = pInfo;
  1085. }
  1086. else if(!_wcsicmp(categoryName, AUDIT_PROCESS_TRAKING))
  1087. {
  1088. pProfileInfo->AuditProcessTracking = settingVal;
  1089. pProfileInfo->pRIAuditProcessTracking = pInfo;
  1090. }
  1091. else if(!_wcsicmp(categoryName, AUDIT_DS_ACCESS))
  1092. {
  1093. pProfileInfo->AuditDSAccess = settingVal;
  1094. pProfileInfo->pRIAuditDSAccess = pInfo;
  1095. }
  1096. else if(!_wcsicmp(categoryName, AUDIT_ACCOUNT_LOGON))
  1097. {
  1098. pProfileInfo->AuditAccountLogon = settingVal;
  1099. pProfileInfo->pRIAuditAccountLogon = pInfo;
  1100. }
  1101. else
  1102. {
  1103. _ASSERT (FALSE); // key name not accounted for
  1104. }
  1105. exit_gracefully:
  1106. VariantClear(&bCategoryName);
  1107. VariantClear(&vSuccessVal);
  1108. VariantClear(&vFailVal);
  1109. _TRACE (-1,L"Leaving CWMIRsop::AddAuditSetting\n");
  1110. return hr;
  1111. }
  1112. HRESULT CWMIRsop::AddUserRightSetting(
  1113. IWbemClassObject *rsopInstance,
  1114. PRSOP_INFO pInfo,
  1115. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1116. {
  1117. VARIANT bUserRight;
  1118. PTSTR userRight = NULL;
  1119. VARIANT vAccountList;
  1120. HRESULT hr = S_OK;
  1121. _TRACE (1, L"Entering CWMIRsop::AddUserRightSetting\n");
  1122. hr = rsopInstance->Get(_bstr_t(RSOP_USERRIGHT),
  1123. 0,
  1124. &bUserRight,
  1125. NULL,
  1126. NULL);
  1127. if( FAILED(hr) || bUserRight.vt != VT_BSTR || bUserRight.bstrVal == NULL )
  1128. goto exit_gracefully;
  1129. hr = rsopInstance->Get(_bstr_t(RSOP_ACCOUNTLIST),
  1130. 0,
  1131. &vAccountList,
  1132. NULL,
  1133. NULL);
  1134. if( FAILED(hr) )
  1135. goto exit_gracefully;
  1136. DWORD len;
  1137. len = wcslen((PTSTR) V_BSTR(&bUserRight));
  1138. userRight = (PTSTR) LocalAlloc(LPTR, (len+1) * sizeof(WCHAR) );
  1139. if(!userRight)
  1140. {
  1141. hr = E_OUTOFMEMORY;
  1142. goto exit_gracefully;
  1143. }
  1144. //This may not be a safe usage. userRight is PTSTR. Consider fix.
  1145. wcscpy(userRight, (PTSTR) V_BSTR(&bUserRight));
  1146. PSCE_PRIVILEGE_ASSIGNMENT head;
  1147. head = pProfileInfo->OtherInfo.scp.u.pInfPrivilegeAssignedTo;
  1148. if (!head)
  1149. {
  1150. head = (pProfileInfo->OtherInfo.scp.u.pInfPrivilegeAssignedTo =
  1151. (PSCE_PRIVILEGE_ASSIGNMENT) LocalAlloc(LPTR, sizeof(SCE_PRIVILEGE_ASSIGNMENT)));
  1152. if(!head)
  1153. {
  1154. hr = E_OUTOFMEMORY;
  1155. goto exit_gracefully;
  1156. }
  1157. }
  1158. else
  1159. {
  1160. PSCE_PRIVILEGE_ASSIGNMENT current;
  1161. current = (PSCE_PRIVILEGE_ASSIGNMENT) LocalAlloc(LPTR, sizeof(SCE_PRIVILEGE_ASSIGNMENT));
  1162. if(!current)
  1163. {
  1164. hr = E_OUTOFMEMORY;
  1165. goto exit_gracefully;
  1166. }
  1167. current->Next = head;
  1168. head = current;
  1169. pProfileInfo->OtherInfo.scp.u.pInfPrivilegeAssignedTo = head;
  1170. }
  1171. head->Name = userRight;
  1172. userRight = NULL;
  1173. if(V_VT(&vAccountList) != VT_NULL)
  1174. {
  1175. SAFEARRAY* ptempArray;
  1176. ptempArray = NULL;
  1177. BSTR tempString;
  1178. long lowerBoundray=0, upperBoundray=0, loopCount=0;
  1179. ptempArray = V_ARRAY(&vAccountList);
  1180. if ( FAILED(SafeArrayGetLBound(ptempArray, 1, &lowerBoundray)) ) lowerBoundray = 0;
  1181. if ( FAILED(SafeArrayGetUBound(ptempArray, 1, &upperBoundray)) ) upperBoundray = 0;
  1182. PSCE_NAME_LIST nameHead = head->AssignedTo;
  1183. for (loopCount = lowerBoundray; loopCount <= upperBoundray; loopCount++)
  1184. {
  1185. hr = SafeArrayGetElement(ptempArray,
  1186. &loopCount,
  1187. &tempString);
  1188. if ( FAILED(hr) ) goto exit_gracefully;
  1189. if(!nameHead)
  1190. {
  1191. nameHead =
  1192. (head->AssignedTo = (PSCE_NAME_LIST) LocalAlloc(LPTR, sizeof(SCE_NAME_LIST)));
  1193. if(!nameHead)
  1194. {
  1195. hr = E_OUTOFMEMORY;
  1196. goto exit_gracefully;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. PSCE_NAME_LIST currentName =
  1202. (PSCE_NAME_LIST) LocalAlloc(LPTR, sizeof(SCE_NAME_LIST));
  1203. if(!currentName)
  1204. {
  1205. hr = E_OUTOFMEMORY;
  1206. goto exit_gracefully;
  1207. }
  1208. currentName->Next = nameHead;
  1209. nameHead = currentName;
  1210. head->AssignedTo = nameHead;
  1211. }
  1212. DWORD nameLen = wcslen((PTSTR) tempString);
  1213. nameHead->Name = (PTSTR) LocalAlloc(LPTR, (nameLen+1) *sizeof(WCHAR) );
  1214. if(!nameHead->Name)
  1215. {
  1216. hr = E_OUTOFMEMORY;
  1217. goto exit_gracefully;
  1218. }
  1219. //This may not be a safe usage. nameHead->Name is PWSTR. Consider fix.
  1220. wcscpy(nameHead->Name, (PTSTR) tempString);
  1221. }
  1222. }
  1223. //Set other info
  1224. pProfileInfo->listRIInfPrivilegeAssignedTo.push_front(pInfo);
  1225. exit_gracefully:
  1226. if(FAILED(hr) && (userRight != NULL))
  1227. {
  1228. LocalFree(userRight);
  1229. }
  1230. VariantClear(&bUserRight);
  1231. VariantClear(&vAccountList);
  1232. _TRACE (-1,L"Leaving CWMIRsop::AddUserRightSetting\n");
  1233. return hr;
  1234. }
  1235. HRESULT CWMIRsop::AddRegValSetting(IWbemClassObject *rsopInstance,
  1236. PRSOP_INFO pInfo,
  1237. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1238. {
  1239. VARIANT bKeyName;
  1240. PTSTR keyName;
  1241. VARIANT vSettingValue;
  1242. VARIANT vType;
  1243. PTSTR settingValue;
  1244. DWORD type;
  1245. HRESULT hr = S_OK;
  1246. _TRACE (1, L"Entering CWMIRsop::AddRegValSetting\n");
  1247. hr = rsopInstance->Get(_bstr_t(TEXT("Path")),
  1248. 0,
  1249. &bKeyName,
  1250. NULL,
  1251. NULL);
  1252. if(FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL )
  1253. goto exit_gracefully;
  1254. hr = rsopInstance->Get(_bstr_t(TEXT("Data")),
  1255. 0,
  1256. &vSettingValue,
  1257. NULL,
  1258. NULL);
  1259. if(FAILED(hr) || vSettingValue.vt != VT_BSTR || vSettingValue.bstrVal == NULL )
  1260. goto exit_gracefully;
  1261. hr = rsopInstance->Get(_bstr_t(TEXT("Type")),
  1262. 0,
  1263. &vType,
  1264. NULL,
  1265. NULL);
  1266. if(FAILED(hr))
  1267. goto exit_gracefully;
  1268. DWORD len;
  1269. len = wcslen((PTSTR) V_BSTR(&bKeyName));
  1270. keyName = (PTSTR) LocalAlloc(LPTR, (len+1) *sizeof(WCHAR));
  1271. if(!keyName)
  1272. {
  1273. hr = E_OUTOFMEMORY;
  1274. goto exit_gracefully;
  1275. }
  1276. //This is not a safe usage.
  1277. wcscpy(keyName,(PTSTR) V_BSTR(&bKeyName));
  1278. len = wcslen((PTSTR) V_BSTR(&vSettingValue));
  1279. settingValue = (PTSTR) LocalAlloc(LPTR, (len+1) *sizeof(WCHAR));
  1280. if(!settingValue)
  1281. {
  1282. hr = E_OUTOFMEMORY;
  1283. goto exit_gracefully;
  1284. }
  1285. //This is a safe usage.
  1286. wcscpy(settingValue,(PTSTR) V_BSTR(&vSettingValue));
  1287. type = (DWORD) V_I4(&vType);
  1288. DWORD arrayIndex;
  1289. if ((arrayIndex = pProfileInfo->RegValueCount) == 0)
  1290. {
  1291. pProfileInfo->aRegValues =
  1292. (PSCE_REGISTRY_VALUE_INFO) LocalAlloc(LPTR, sizeof(SCE_REGISTRY_VALUE_INFO)* m_cRegValueSize);
  1293. if(!pProfileInfo->aRegValues)
  1294. {
  1295. hr = E_OUTOFMEMORY;
  1296. goto exit_gracefully;
  1297. }
  1298. }
  1299. //double the array preserving the content
  1300. if( arrayIndex >= m_cRegValueSize )
  1301. {
  1302. PSCE_REGISTRY_VALUE_INFO temp = (PSCE_REGISTRY_VALUE_INFO) LocalAlloc(LPTR, sizeof(SCE_REGISTRY_VALUE_INFO)* m_cRegValueSize * 2);
  1303. if(!temp || pProfileInfo->aRegValues == NULL) //Raid #553113, yanggao.
  1304. {
  1305. hr = E_OUTOFMEMORY;
  1306. goto exit_gracefully;
  1307. }
  1308. //This is not a safe usage. arrayIndex should not be greater than m_cRegValueSize*2 and pProfileInfo->aRegValues need to be validated. Raid #571332. yanggao.
  1309. memcpy(temp,pProfileInfo->aRegValues,sizeof(SCE_REGISTRY_VALUE_INFO)*arrayIndex);
  1310. LocalFree(pProfileInfo->aRegValues);
  1311. pProfileInfo->aRegValues = temp;
  1312. m_cRegValueSize *= 2;
  1313. }
  1314. pProfileInfo->aRegValues[arrayIndex].FullValueName = keyName;
  1315. pProfileInfo->aRegValues[arrayIndex].Value = settingValue;
  1316. pProfileInfo->aRegValues[arrayIndex].ValueType = type;
  1317. pProfileInfo->RegValueCount += 1;
  1318. //Store RSOP_INFO
  1319. pProfileInfo->vecRIRegValues.push_back(pInfo);
  1320. exit_gracefully:
  1321. VariantClear(&bKeyName);
  1322. VariantClear(&vSettingValue);
  1323. VariantClear(&vType);
  1324. _TRACE (-1,L"Leaving CWMIRsop::AddRegValSetting\n");
  1325. return hr;
  1326. }
  1327. HRESULT CWMIRsop::AddRestrictedGroupSetting(IWbemClassObject *rsopInstance,
  1328. PRSOP_INFO pInfo,
  1329. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1330. {
  1331. VARIANT bRGroup;
  1332. PTSTR RGroup;
  1333. VARIANT vMembers;
  1334. HRESULT hr = S_OK;
  1335. _TRACE (1, L"Entering CWMIRsop::AddRestrictedGroupSetting\n");
  1336. hr = rsopInstance->Get(_bstr_t(TEXT("GroupName")),
  1337. 0,
  1338. &bRGroup,
  1339. NULL,
  1340. NULL
  1341. );
  1342. if(FAILED(hr) || bRGroup.vt != VT_BSTR || bRGroup.bstrVal == NULL )
  1343. goto exit_gracefully;
  1344. hr = rsopInstance->Get(_bstr_t(TEXT("Members")),
  1345. 0,
  1346. &vMembers,
  1347. NULL,
  1348. NULL
  1349. );
  1350. if(FAILED(hr))
  1351. goto exit_gracefully;
  1352. DWORD len;
  1353. len = wcslen((PTSTR) V_BSTR(&bRGroup));
  1354. RGroup = (PTSTR) LocalAlloc(LPTR, (len+1) * sizeof(WCHAR));
  1355. if(!RGroup)
  1356. {
  1357. hr = E_OUTOFMEMORY;
  1358. goto exit_gracefully;
  1359. }
  1360. //This is a safe usage.
  1361. wcscpy(RGroup, (PTSTR) V_BSTR(&bRGroup));
  1362. PSCE_GROUP_MEMBERSHIP head;
  1363. head = pProfileInfo->pGroupMembership;
  1364. if (!head)
  1365. {
  1366. head = (pProfileInfo->pGroupMembership =
  1367. (PSCE_GROUP_MEMBERSHIP) LocalAlloc(LPTR, sizeof(SCE_GROUP_MEMBERSHIP)));
  1368. if(!head)
  1369. {
  1370. hr = E_OUTOFMEMORY;
  1371. goto exit_gracefully;
  1372. }
  1373. }
  1374. else
  1375. {
  1376. PSCE_GROUP_MEMBERSHIP current =
  1377. (PSCE_GROUP_MEMBERSHIP) LocalAlloc(LPTR, sizeof(SCE_GROUP_MEMBERSHIP));
  1378. if(!current)
  1379. {
  1380. hr = E_OUTOFMEMORY;
  1381. goto exit_gracefully;
  1382. }
  1383. current->Next = head;
  1384. head = current;
  1385. pProfileInfo->pGroupMembership = head;
  1386. }
  1387. head->GroupName = RGroup;
  1388. if(V_VT(&vMembers) != VT_NULL)
  1389. {
  1390. SAFEARRAY* ptempArray = NULL;
  1391. BSTR tempString;
  1392. long lowerBoundray=0, upperBoundray=0, loopCount=0;
  1393. ptempArray = V_ARRAY(&vMembers);
  1394. if ( FAILED(SafeArrayGetLBound(ptempArray, 1, &lowerBoundray)) ) lowerBoundray = 0;
  1395. if ( FAILED(SafeArrayGetUBound(ptempArray, 1, &upperBoundray)) ) upperBoundray = 0;
  1396. PSCE_NAME_LIST nameHead = head->pMembers;
  1397. for (loopCount = lowerBoundray; loopCount <= upperBoundray; loopCount++){
  1398. hr = SafeArrayGetElement(ptempArray,
  1399. &loopCount,
  1400. &tempString);
  1401. if ( FAILED(hr) ) goto exit_gracefully;
  1402. if(!nameHead)
  1403. {
  1404. nameHead =
  1405. (head->pMembers = (PSCE_NAME_LIST) LocalAlloc(LPTR, sizeof(SCE_NAME_LIST)));
  1406. if(!nameHead)
  1407. {
  1408. hr = E_OUTOFMEMORY;
  1409. goto exit_gracefully;
  1410. }
  1411. }
  1412. else
  1413. {
  1414. PSCE_NAME_LIST currentName =
  1415. (PSCE_NAME_LIST) LocalAlloc(LPTR, sizeof(SCE_NAME_LIST));
  1416. if(!currentName)
  1417. {
  1418. hr = E_OUTOFMEMORY;
  1419. goto exit_gracefully;
  1420. }
  1421. currentName->Next = nameHead;
  1422. nameHead = currentName;
  1423. head->pMembers = nameHead;
  1424. }
  1425. DWORD nameLen = wcslen((PTSTR) tempString);
  1426. nameHead->Name = (PTSTR) LocalAlloc(LPTR, (nameLen+1) *sizeof(WCHAR) );
  1427. if ( !(nameHead->Name) ) {
  1428. hr = E_OUTOFMEMORY;
  1429. goto exit_gracefully;
  1430. }
  1431. //This may not be a safe usage. nameHead->Name is PWSTR. Consider fix.
  1432. wcscpy(nameHead->Name, (PTSTR) tempString);
  1433. }
  1434. }
  1435. //Add RSOP info
  1436. pProfileInfo->listRIGroupMemebership.push_front(pInfo);
  1437. exit_gracefully:
  1438. VariantClear(&bRGroup);
  1439. VariantClear(&vMembers);
  1440. _TRACE (-1,L"Leaving CWMIRsop::AddRestrictedGroupSetting\n");
  1441. return hr;
  1442. }
  1443. HRESULT CWMIRsop::AddServiceSetting(IWbemClassObject *rsopInstance,
  1444. PRSOP_INFO pInfo,
  1445. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1446. {
  1447. VARIANT bServiceName;
  1448. PTSTR serviceName;
  1449. VARIANT vSDDL;
  1450. VARIANT vStartup;
  1451. PSECURITY_DESCRIPTOR SDDL = NULL;
  1452. DWORD startup;
  1453. HRESULT hr = S_OK;
  1454. _TRACE (1, L"Entering CWMIRsop::AddServiceSetting\n");
  1455. hr = rsopInstance->Get(_bstr_t(TEXT("Service")),
  1456. 0,
  1457. &bServiceName,
  1458. NULL,
  1459. NULL);
  1460. if(FAILED(hr) || bServiceName.vt != VT_BSTR || bServiceName.bstrVal == NULL )
  1461. goto exit_gracefully;
  1462. hr = rsopInstance->Get(_bstr_t(TEXT("SDDLString")),
  1463. 0,
  1464. &vSDDL,
  1465. NULL,
  1466. NULL);
  1467. if(FAILED(hr))
  1468. goto exit_gracefully;
  1469. hr = rsopInstance->Get((BSTR)(TEXT("StartupMode")),
  1470. 0,
  1471. &vStartup,
  1472. NULL,
  1473. NULL);
  1474. if(FAILED(hr))
  1475. goto exit_gracefully;
  1476. DWORD len;
  1477. len = wcslen((PTSTR) V_BSTR(&bServiceName));
  1478. serviceName = (PTSTR) LocalAlloc(LPTR, (len+1) *sizeof(WCHAR));
  1479. if(!serviceName)
  1480. {
  1481. hr = E_OUTOFMEMORY;
  1482. goto exit_gracefully;
  1483. }
  1484. //This is a safe usage.
  1485. wcscpy(serviceName,(PTSTR) V_BSTR(&bServiceName));
  1486. ULONG sdLen;
  1487. sdLen = 0;
  1488. // This is a safe usage. vSDDL comes from WMI.
  1489. if( !ConvertStringSecurityDescriptorToSecurityDescriptor(
  1490. (PTSTR) V_BSTR(&vSDDL),
  1491. SDDL_REVISION_1,
  1492. &SDDL,
  1493. &sdLen
  1494. ) )
  1495. {
  1496. hr = HRESULT_FROM_WIN32(GetLastError());
  1497. goto exit_gracefully;
  1498. }
  1499. startup = (DWORD) V_I4(&vStartup);
  1500. PSCE_SERVICES head;
  1501. head = pProfileInfo->pServices;
  1502. if (!head)
  1503. {
  1504. head = (pProfileInfo->pServices =
  1505. (PSCE_SERVICES) LocalAlloc(LPTR, sizeof(SCE_SERVICES)));
  1506. if(!head)
  1507. {
  1508. hr = E_OUTOFMEMORY;
  1509. goto exit_gracefully;
  1510. }
  1511. }
  1512. else
  1513. {
  1514. PSCE_SERVICES current =
  1515. (PSCE_SERVICES) LocalAlloc(LPTR, sizeof(SCE_SERVICES));
  1516. if(!current)
  1517. {
  1518. hr = E_OUTOFMEMORY;
  1519. goto exit_gracefully;
  1520. }
  1521. current->Next = head;
  1522. head = current;
  1523. pProfileInfo->pServices = head;
  1524. }
  1525. head->ServiceName = serviceName;
  1526. head->General.pSecurityDescriptor = SDDL;
  1527. head->Startup = (BYTE) startup; //no data loss value is <= 4
  1528. //Add RSOP info
  1529. pProfileInfo->listRIServices.push_front(pInfo);
  1530. exit_gracefully:
  1531. VariantClear(&bServiceName);
  1532. VariantClear(&vSDDL);
  1533. VariantClear(&vStartup);
  1534. _TRACE (-1,L"Leaving CWMIRsop::AddServiceSetting\n");
  1535. return hr;
  1536. }
  1537. HRESULT CWMIRsop::AddFileSetting(IWbemClassObject *rsopInstance,
  1538. PRSOP_INFO pInfo,
  1539. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1540. {
  1541. VARIANT bFileName;
  1542. PTSTR fileName = NULL;
  1543. VARIANT vSDDL;
  1544. VARIANT vMode;
  1545. PSECURITY_DESCRIPTOR SDDL = NULL;
  1546. DWORD mode = 0;
  1547. _TRACE (1, L"Entering CWMIRsop::AddFileSetting\n");
  1548. HRESULT hr = S_OK;
  1549. hr = rsopInstance->Get((BSTR)(TEXT("Path")),
  1550. 0,
  1551. &bFileName,
  1552. NULL,
  1553. NULL
  1554. );
  1555. if(FAILED(hr) || bFileName.vt != VT_BSTR || bFileName.bstrVal == NULL )
  1556. goto exit_gracefully;
  1557. hr = rsopInstance->Get((BSTR)(TEXT("Mode")),
  1558. 0,
  1559. &vMode,
  1560. NULL,
  1561. NULL
  1562. );
  1563. if(FAILED(hr))
  1564. goto exit_gracefully;
  1565. hr = rsopInstance->Get((BSTR)(TEXT("SDDLString")),
  1566. 0,
  1567. &vSDDL,
  1568. NULL,
  1569. NULL
  1570. );
  1571. if(FAILED(hr))
  1572. goto exit_gracefully;
  1573. DWORD len;
  1574. len = wcslen((PTSTR) V_BSTR(&bFileName));
  1575. fileName = (PTSTR) LocalAlloc(LPTR, len * sizeof(TCHAR) + 2);
  1576. if(!fileName)
  1577. {
  1578. hr = E_OUTOFMEMORY;
  1579. goto exit_gracefully;
  1580. }
  1581. //This may not be a safe usage. fileName is PTSTR. Consider fix.
  1582. wcscpy(fileName, (PTSTR) V_BSTR(&bFileName));
  1583. mode = (DWORD) V_I4(&vMode);
  1584. if (mode != 1)
  1585. {
  1586. ULONG sdLen = 0;
  1587. //This is a safe usage. vSDDL comes from WMI.
  1588. if( !ConvertStringSecurityDescriptorToSecurityDescriptor(
  1589. (PTSTR) V_BSTR(&vSDDL),
  1590. SDDL_REVISION_1,
  1591. &SDDL,
  1592. &sdLen
  1593. ))
  1594. {
  1595. hr = HRESULT_FROM_WIN32(GetLastError());
  1596. goto exit_gracefully;
  1597. }
  1598. }
  1599. PSCE_OBJECT_ARRAY head;
  1600. head = pProfileInfo->pFiles.pAllNodes;
  1601. if(!head)
  1602. {
  1603. head = (pProfileInfo->pFiles.pAllNodes =
  1604. (PSCE_OBJECT_ARRAY) LocalAlloc(LPTR, sizeof(SCE_OBJECT_ARRAY)));
  1605. if(!head)
  1606. {
  1607. hr = E_OUTOFMEMORY;
  1608. goto exit_gracefully;
  1609. }
  1610. head->pObjectArray =
  1611. (PSCE_OBJECT_SECURITY*) LocalAlloc(LPTR, sizeof(PSCE_OBJECT_SECURITY)*m_cFileSize);
  1612. if(!head->pObjectArray)
  1613. {
  1614. hr = E_OUTOFMEMORY;
  1615. goto exit_gracefully;
  1616. }
  1617. }
  1618. else if(head->Count >= m_cFileSize)
  1619. {
  1620. PSCE_OBJECT_SECURITY* temp = (PSCE_OBJECT_SECURITY*) LocalAlloc(LPTR, sizeof(PSCE_OBJECT_SECURITY)*m_cFileSize*2);
  1621. if(!temp || head->pObjectArray == NULL) //Raid #553113, yanggao.
  1622. {
  1623. hr = E_OUTOFMEMORY;
  1624. goto exit_gracefully;
  1625. }
  1626. //This is not a safe usage. head->Count should not be greater than m_cFileSize*2 and head->pObjectArray need to be validated. Raid #571332. yanggao.
  1627. memcpy(temp,head->pObjectArray,head->Count *sizeof(PSCE_OBJECT_SECURITY));
  1628. LocalFree(head->pObjectArray);
  1629. head->pObjectArray = temp;
  1630. m_cFileSize *=2;
  1631. }
  1632. DWORD index;
  1633. index = head->Count;
  1634. head->pObjectArray[index] =
  1635. (PSCE_OBJECT_SECURITY) LocalAlloc(LPTR, sizeof(SCE_OBJECT_SECURITY));
  1636. if(!head->pObjectArray[index])
  1637. {
  1638. hr = E_OUTOFMEMORY;
  1639. goto exit_gracefully;
  1640. }
  1641. head->pObjectArray[index]->Name = fileName;
  1642. head->pObjectArray[index]->pSecurityDescriptor = SDDL;
  1643. head->pObjectArray[index]->Status = (BYTE) mode;
  1644. head->Count++;
  1645. //SET RSOP INFO
  1646. pProfileInfo->vecRIFiles.push_back(pInfo);
  1647. exit_gracefully:
  1648. VariantClear(&bFileName);
  1649. VariantClear(&vMode);
  1650. if(mode != 1){
  1651. VariantClear(&vSDDL);
  1652. }
  1653. _TRACE (-1,L"Leaving CWMIRsop::AddFileSetting\n");
  1654. return hr;
  1655. }
  1656. HRESULT CWMIRsop::AddRegSetting(IWbemClassObject *rsopInstance,
  1657. PRSOP_INFO pInfo,
  1658. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1659. {
  1660. PTSTR gpoID = NULL;
  1661. VARIANT bFileName;
  1662. PTSTR fileName = NULL;
  1663. VARIANT vSDDL;
  1664. VARIANT vMode;
  1665. PSECURITY_DESCRIPTOR SDDL = NULL;
  1666. DWORD mode = 0;
  1667. static DWORD multiplier = 1;
  1668. HRESULT hr = S_OK;
  1669. _TRACE (1, L"Entering CWMIRsop::AddRegSetting\n");
  1670. hr = rsopInstance->Get((BSTR)(TEXT("Path")),
  1671. 0,
  1672. &bFileName,
  1673. NULL,
  1674. NULL
  1675. );
  1676. if(FAILED(hr) || bFileName.vt != VT_BSTR || bFileName.bstrVal == NULL )
  1677. goto exit_gracefully;
  1678. hr = rsopInstance->Get((BSTR)(TEXT("Mode")),
  1679. 0,
  1680. &vMode,
  1681. NULL,
  1682. NULL
  1683. );
  1684. if(FAILED(hr))
  1685. goto exit_gracefully;
  1686. hr = rsopInstance->Get((BSTR)(TEXT("SDDLString")),
  1687. 0,
  1688. &vSDDL,
  1689. NULL,
  1690. NULL
  1691. );
  1692. if(FAILED(hr))
  1693. goto exit_gracefully;
  1694. DWORD len;
  1695. len = wcslen((PTSTR) V_BSTR(&bFileName));
  1696. fileName = (PTSTR) LocalAlloc(LPTR, len * sizeof(TCHAR) + 2);
  1697. if(!fileName)
  1698. {
  1699. hr = E_OUTOFMEMORY;
  1700. goto exit_gracefully;
  1701. }
  1702. //This may not be a safe usage. fileName is PTSTR. Consider fix.
  1703. wcscpy(fileName, (PTSTR) V_BSTR(&bFileName));
  1704. mode = (DWORD) V_I4(&vMode);
  1705. if (mode != 1)
  1706. {
  1707. ULONG sdLen = 0;
  1708. // This is a safe usage. vSDDL comes from WMI.
  1709. if( !ConvertStringSecurityDescriptorToSecurityDescriptor(
  1710. (PTSTR) V_BSTR(&vSDDL),
  1711. SDDL_REVISION_1,
  1712. &SDDL,
  1713. &sdLen
  1714. ))
  1715. {
  1716. hr = HRESULT_FROM_WIN32(GetLastError());
  1717. goto exit_gracefully;
  1718. }
  1719. }
  1720. PSCE_OBJECT_ARRAY head;
  1721. head = pProfileInfo->pRegistryKeys.pAllNodes;
  1722. if(!head){
  1723. head = (pProfileInfo->pRegistryKeys.pAllNodes =
  1724. (PSCE_OBJECT_ARRAY) LocalAlloc(LPTR, sizeof(SCE_OBJECT_ARRAY)));
  1725. if(!head)
  1726. {
  1727. hr = E_OUTOFMEMORY;
  1728. goto exit_gracefully;
  1729. }
  1730. head->pObjectArray =
  1731. (PSCE_OBJECT_SECURITY*) LocalAlloc(LPTR, sizeof(PSCE_OBJECT_SECURITY)*m_cRegArrayCount);
  1732. if(!head->pObjectArray)
  1733. {
  1734. hr = E_OUTOFMEMORY;
  1735. goto exit_gracefully;
  1736. }
  1737. }
  1738. else if(head->Count >= m_cRegArrayCount){
  1739. PSCE_OBJECT_SECURITY* temp = head->pObjectArray;
  1740. head->pObjectArray =
  1741. (PSCE_OBJECT_SECURITY*) LocalAlloc(LPTR, sizeof(PSCE_OBJECT_SECURITY)*m_cRegArrayCount*2);
  1742. if(!head->pObjectArray || !temp) //Raid #553113, yanggao.
  1743. {
  1744. hr = E_OUTOFMEMORY;
  1745. goto exit_gracefully;
  1746. }
  1747. //This is not a safe usage. temp need to be validated and head->Count should not be greater than m_cRegArrayCount*2. Raid #571332. yanggao.
  1748. memcpy(head->pObjectArray,temp,head->Count *sizeof(PSCE_OBJECT_SECURITY));
  1749. LocalFree(temp);
  1750. m_cRegArrayCount *= 2;
  1751. }
  1752. //
  1753. // Can't initialize at declaration since this is skipped by gotos
  1754. //
  1755. DWORD index;
  1756. index = head->Count;
  1757. head->pObjectArray[index] =
  1758. (PSCE_OBJECT_SECURITY) LocalAlloc(LPTR, sizeof(SCE_OBJECT_SECURITY));
  1759. if (head->pObjectArray[index]) {
  1760. head->pObjectArray[index]->Name = fileName;
  1761. head->pObjectArray[index]->pSecurityDescriptor = SDDL;
  1762. head->pObjectArray[index]->Status = (BYTE) mode;
  1763. head->Count++;
  1764. }
  1765. //add rsop info
  1766. pProfileInfo->vecRIReg.push_back(pInfo);
  1767. exit_gracefully:
  1768. VariantClear(&bFileName);
  1769. VariantClear(&vMode);
  1770. if(mode != 1){
  1771. VariantClear(&vSDDL);
  1772. }
  1773. _TRACE (-1, L"Leaving CWMIRsop::AddRegSetting\n");
  1774. return hr;
  1775. }
  1776. HRESULT CWMIRsop::AddStringSetting (IWbemClassObject *rsopInstance,
  1777. PRSOP_INFO pInfo,
  1778. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1779. {
  1780. HRESULT hr = S_OK;
  1781. VARIANT bKeyName;
  1782. PWSTR keyName = 0;
  1783. VARIANT bSettingValue;
  1784. PWSTR settingValue = 0;
  1785. _TRACE (1, L"Entering CWMIRsop::AddStringSetting\n");
  1786. hr = rsopInstance->Get((BSTR)RSOP_KEYNAME,
  1787. 0,
  1788. &bKeyName,
  1789. NULL,
  1790. NULL);
  1791. if( FAILED(hr) || bKeyName.vt != VT_BSTR || bKeyName.bstrVal == NULL)
  1792. goto exit_gracefully;
  1793. hr = rsopInstance->Get((BSTR)RSOP_SETTING,
  1794. 0,
  1795. &bSettingValue,
  1796. NULL,
  1797. NULL);
  1798. if( FAILED(hr) || bSettingValue.vt != VT_BSTR || bSettingValue.bstrVal == NULL)
  1799. goto exit_gracefully;
  1800. keyName = V_BSTR(&bKeyName);
  1801. settingValue = V_BSTR(&bSettingValue);
  1802. if (!_wcsicmp(keyName, NEW_GUEST_NAME))
  1803. {
  1804. if ( 0 != pProfileInfo->NewGuestName )
  1805. {
  1806. LocalFree (pProfileInfo->NewGuestName);
  1807. pProfileInfo->NewGuestName = 0;
  1808. }
  1809. size_t len = wcslen (settingValue);
  1810. pProfileInfo->NewGuestName = (PWSTR) LocalAlloc (LPTR, (len + 1) * sizeof (TCHAR));
  1811. if ( pProfileInfo->NewGuestName )
  1812. {
  1813. //This may not be a safe usage. Using WCHAR in LocalAlloc() instead of TCHAR. Consider fix.
  1814. wcscpy (pProfileInfo->NewGuestName, settingValue);
  1815. }
  1816. else
  1817. hr = E_OUTOFMEMORY;
  1818. pProfileInfo->pRINewGuestName = pInfo;
  1819. }
  1820. else if (!_wcsicmp(keyName, NEW_ADMINISTRATOR_NAME))
  1821. {
  1822. if ( 0 != pProfileInfo->NewAdministratorName )
  1823. {
  1824. LocalFree (pProfileInfo->NewAdministratorName);
  1825. pProfileInfo->NewAdministratorName = 0;
  1826. }
  1827. size_t len = wcslen (settingValue);
  1828. pProfileInfo->NewAdministratorName = (PWSTR) LocalAlloc (LPTR, (len + 1) * sizeof (TCHAR));
  1829. if ( pProfileInfo->NewAdministratorName )
  1830. {
  1831. //This may not be a safe usage. Using WCHAR in LocalAlloc() instead of TCHAR. Consider fix.
  1832. wcscpy (pProfileInfo->NewAdministratorName, settingValue);
  1833. }
  1834. else
  1835. hr = E_OUTOFMEMORY;
  1836. pProfileInfo->pRINewAdministratorName = pInfo;
  1837. }
  1838. else
  1839. {
  1840. _ASSERT (FALSE); // key name not accounted for
  1841. }
  1842. exit_gracefully:
  1843. VariantClear(&bKeyName);
  1844. VariantClear(&bSettingValue);
  1845. _TRACE (-1,L"Leaving CWMIRsop::AddStringSetting\n");
  1846. return hr;
  1847. }
  1848. HRESULT CWMIRsop::AddInstance(IWbemClassObject *rsopInstance,
  1849. PRSOP_INFO pInfo,
  1850. PWMI_SCE_PROFILE_INFO pProfileInfo)
  1851. {
  1852. HRESULT hr = S_OK;
  1853. LPWSTR className = NULL;
  1854. _TRACE (1, L"Entering CWMIRsop::AddInstance\n");
  1855. //Get RSOP_Class Name
  1856. hr = GetClass(rsopInstance, &className);
  1857. if( FAILED(hr) )
  1858. goto exit_gracefully;
  1859. if(!_wcsicmp(className, RSOP_SEC_NUM))
  1860. {
  1861. hr = AddNumericSetting(rsopInstance,
  1862. pInfo,
  1863. pProfileInfo);
  1864. }
  1865. else if(!_wcsicmp(className, RSOP_SEC_BOOL))
  1866. {
  1867. hr = AddBooleanSetting(rsopInstance,
  1868. pInfo,
  1869. pProfileInfo);
  1870. }
  1871. else if(!_wcsicmp(className, RSOP_SCE_STRING))
  1872. {
  1873. hr = AddStringSetting(rsopInstance,pInfo,pProfileInfo);
  1874. }
  1875. else if(!_wcsicmp(className, RSOP_AUDIT))
  1876. {
  1877. hr = AddAuditSetting(rsopInstance,pInfo,pProfileInfo);
  1878. }
  1879. else if(!_wcsicmp(className, RSOP_EVENT_NUM))
  1880. {
  1881. hr = AddEventLogNumericSetting (rsopInstance, pInfo, pProfileInfo);
  1882. }
  1883. else if(!_wcsicmp(className, RSOP_EVENT_BOOL))
  1884. {
  1885. hr = AddEventLogBooleanSetting(rsopInstance,pInfo,pProfileInfo);
  1886. }
  1887. else if(!_wcsicmp(className, RSOP_REG_VAL))
  1888. {
  1889. hr = AddRegValSetting(rsopInstance,pInfo,pProfileInfo);
  1890. }
  1891. else if(!_wcsicmp(className, RSOP_USER_RIGHT))
  1892. {
  1893. hr = AddUserRightSetting(rsopInstance,pInfo,pProfileInfo);
  1894. }
  1895. else if(!_wcsicmp(className, RSOP_RGROUPS))
  1896. {
  1897. hr = AddRestrictedGroupSetting(rsopInstance,pInfo,pProfileInfo);
  1898. }
  1899. else if(!_wcsicmp(className, RSOP_SERVICE))
  1900. {
  1901. hr = AddServiceSetting(rsopInstance,pInfo,pProfileInfo);
  1902. }
  1903. else if(!_wcsicmp(className, RSOP_FILE))
  1904. {
  1905. hr = AddFileSetting(rsopInstance,pInfo,pProfileInfo);
  1906. }
  1907. else if(!_wcsicmp(className, RSOP_REG))
  1908. {
  1909. hr = AddRegSetting(rsopInstance,pInfo,pProfileInfo);
  1910. }
  1911. else
  1912. {
  1913. _ASSERT (FALSE); // class not accounted for
  1914. }
  1915. exit_gracefully:
  1916. _TRACE (-1,L"Exiting CWMIRsop::AddInstance\n");
  1917. LocalFree(className);
  1918. return hr;
  1919. }
  1920. HRESULT
  1921. CWMIRsop::GetGPOFriendlyName (PWSTR lpGPOID, PWSTR *pGPOName)
  1922. {
  1923. BSTR pQuery = NULL, pName = NULL;
  1924. LPTSTR lpQuery = NULL;
  1925. IEnumWbemClassObject * pEnum = NULL;
  1926. IWbemClassObject *pObjects[2];
  1927. HRESULT hr;
  1928. ULONG ulRet;
  1929. VARIANT varGPOName;
  1930. //
  1931. // Set the default
  1932. //
  1933. *pGPOName = NULL;
  1934. //
  1935. // Build the query
  1936. //
  1937. lpQuery = (LPTSTR) LocalAlloc (LPTR, ((lstrlen(lpGPOID) + 50) * sizeof(TCHAR)));
  1938. if (!lpQuery)
  1939. {
  1940. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to allocate memory for unicode query");
  1941. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1942. goto Exit;
  1943. }
  1944. //This is a safe usage.
  1945. wsprintf (lpQuery, TEXT("SELECT name, id FROM RSOP_GPO where id=\"%s\""), lpGPOID);
  1946. pQuery = SysAllocString (lpQuery);
  1947. if (!pQuery)
  1948. {
  1949. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to allocate memory for query");
  1950. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1951. goto Exit;
  1952. }
  1953. //
  1954. // Allocate BSTRs for the property names we want to retreive
  1955. //
  1956. pName = SysAllocString (TEXT("name"));
  1957. if (!pName)
  1958. {
  1959. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to allocate memory for name");
  1960. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  1961. goto Exit;
  1962. }
  1963. //
  1964. // Execute the query
  1965. //
  1966. hr = m_pSvc->ExecQuery (_bstr_t(QUERY_LANG), pQuery,
  1967. WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
  1968. NULL, &pEnum);
  1969. if (FAILED(hr))
  1970. {
  1971. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to query for %s with 0x%x\n",
  1972. pQuery, hr);
  1973. goto Exit;
  1974. }
  1975. //
  1976. // Loop through the results
  1977. //
  1978. hr = pEnum->Next(WBEM_INFINITE, 1, pObjects, &ulRet);
  1979. if (FAILED(hr))
  1980. {
  1981. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to get first item in query results for %s with 0x%x\n",
  1982. pQuery, hr);
  1983. goto Exit;
  1984. }
  1985. //
  1986. // Check for the "data not available case"
  1987. //
  1988. if (ulRet == 0)
  1989. {
  1990. hr = S_OK;
  1991. goto Exit;
  1992. }
  1993. //
  1994. // Get the name
  1995. //
  1996. VariantInit(&varGPOName);
  1997. hr = pObjects[0]->Get (pName, 0, &varGPOName, NULL, NULL);
  1998. if (FAILED(hr))
  1999. {
  2000. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to get gponame in query results for %s with 0x%x\n",
  2001. pQuery, hr);
  2002. goto Exit;
  2003. }
  2004. //
  2005. // Save the name
  2006. //
  2007. *pGPOName = (LPTSTR) LocalAlloc (LPTR, (lstrlen(varGPOName.bstrVal) + 1) * sizeof(TCHAR));
  2008. if (!(*pGPOName))
  2009. {
  2010. _TRACE (0, L"CWMIRsop::GetGPOFriendlyName: Failed to allocate memory for GPO Name");
  2011. hr = HRESULT_FROM_WIN32(ERROR_OUTOFMEMORY);
  2012. goto Exit;
  2013. }
  2014. //This may not be a safe usage. pGPOName is PWSTR, using WCHAR in LocalAlloc() instead of TCHAR. Consider fix.
  2015. lstrcpy (*pGPOName, varGPOName.bstrVal);
  2016. hr = S_OK;
  2017. Exit:
  2018. VariantClear (&varGPOName);
  2019. if (pEnum)
  2020. {
  2021. pEnum->Release();
  2022. }
  2023. if (pQuery)
  2024. {
  2025. SysFreeString (pQuery);
  2026. }
  2027. if (lpQuery)
  2028. {
  2029. LocalFree (lpQuery);
  2030. }
  2031. if (pName)
  2032. {
  2033. SysFreeString (pName);
  2034. }
  2035. return hr;
  2036. }