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.

2749 lines
79 KiB

  1. // ActionCheck.cpp: implementation of the CActionCheck class.
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "precomp.h"
  7. #include "ActionCheck.h"
  8. #include "ExtendString.h"
  9. #include "ExtendQuery.h"
  10. //////////////////////////////////////////////////////////////////////
  11. // Construction/Destruction
  12. //////////////////////////////////////////////////////////////////////
  13. CActionCheck::CActionCheck(CRequestObject *pObj, IWbemServices *pNamespace,
  14. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  15. {
  16. }
  17. CActionCheck::~CActionCheck()
  18. {
  19. }
  20. HRESULT CActionCheck::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  21. {
  22. HRESULT hr = WBEM_S_NO_ERROR;
  23. CRequestObject *pActionRObj = NULL;
  24. CRequestObject *pCheckRObj = NULL;
  25. try{
  26. if(atAction != ACTIONTYPE_ENUM)
  27. {
  28. // we are doing GetObject so we need to be reinitialized
  29. hr = WBEM_E_NOT_FOUND;
  30. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  31. int i;
  32. for(i = 0; i < m_pRequest->m_iPropCount; i++){
  33. if(_wcsicmp(m_pRequest->m_Property[i], L"ACTION") == 0){
  34. pActionRObj = new CRequestObject();
  35. if(!pActionRObj) throw he;
  36. pActionRObj->Initialize(m_pNamespace);
  37. pActionRObj->ParsePath(m_pRequest->m_Value[i]);
  38. break;
  39. }
  40. }
  41. for(i = 0; i < m_pRequest->m_iPropCount; i++){
  42. if(_wcsicmp(m_pRequest->m_Property[i], L"CHECK") == 0){
  43. pCheckRObj = new CRequestObject();
  44. if(!pCheckRObj) throw he;
  45. pCheckRObj->Initialize(m_pNamespace);
  46. pCheckRObj->ParsePath(m_pRequest->m_Value[i]);
  47. break;
  48. }
  49. }
  50. }
  51. if(atAction == ACTIONTYPE_ENUM || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_CreateFolderAction") == 0) &&
  52. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_DirectorySpecification") == 0)))))
  53. if(FAILED(hr = CreateFolderDirectory(pHandler, atAction, pActionRObj, pCheckRObj))){
  54. if(pActionRObj){
  55. pActionRObj->Cleanup();
  56. delete pActionRObj;
  57. pActionRObj = NULL;
  58. }
  59. if(pCheckRObj){
  60. pCheckRObj->Cleanup();
  61. delete pCheckRObj;
  62. pCheckRObj = NULL;
  63. }
  64. return hr;
  65. }
  66. if(atAction == ACTIONTYPE_ENUM || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_DuplicateFileAction") == 0) &&
  67. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_FileSpecification") == 0)))))
  68. if(FAILED(hr = FileDuplicateFile(pHandler, atAction, pActionRObj, pCheckRObj))){
  69. if(pActionRObj){
  70. pActionRObj->Cleanup();
  71. delete pActionRObj;
  72. pActionRObj = NULL;
  73. }
  74. if(pCheckRObj){
  75. pCheckRObj->Cleanup();
  76. delete pCheckRObj;
  77. pCheckRObj = NULL;
  78. }
  79. return hr;
  80. }
  81. if(atAction == ACTIONTYPE_ENUM || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_FontInfoAction") == 0) &&
  82. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_FileSpecification") == 0)))))
  83. if(FAILED(hr = FontInfoFile(pHandler, atAction, pActionRObj, pCheckRObj))){
  84. if(pActionRObj){
  85. pActionRObj->Cleanup();
  86. delete pActionRObj;
  87. pActionRObj = NULL;
  88. }
  89. if(pCheckRObj){
  90. pCheckRObj->Cleanup();
  91. delete pCheckRObj;
  92. pCheckRObj = NULL;
  93. }
  94. return hr;
  95. }
  96. if((atAction == ACTIONTYPE_ENUM) || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_ClassInfoAction") == 0) &&
  97. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_ProgIDSpecification") == 0)))))
  98. if(FAILED(hr = ProgIDSpecificationClass(pHandler, atAction, pActionRObj, pCheckRObj))){
  99. if(pActionRObj){
  100. pActionRObj->Cleanup();
  101. delete pActionRObj;
  102. pActionRObj = NULL;
  103. }
  104. if(pCheckRObj){
  105. pCheckRObj->Cleanup();
  106. delete pCheckRObj;
  107. pCheckRObj = NULL;
  108. }
  109. return hr;
  110. }
  111. if((atAction == ACTIONTYPE_ENUM) || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_RemoveIniAction") == 0) &&
  112. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_DirectorySpecification") == 0)))))
  113. if(FAILED(hr = RemoveIniDirectory(pHandler, atAction, pActionRObj, pCheckRObj))){
  114. if(pActionRObj){
  115. pActionRObj->Cleanup();
  116. delete pActionRObj;
  117. pActionRObj = NULL;
  118. }
  119. if(pCheckRObj){
  120. pCheckRObj->Cleanup();
  121. delete pCheckRObj;
  122. pCheckRObj = NULL;
  123. }
  124. return hr;
  125. }
  126. if((atAction == ACTIONTYPE_ENUM) || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_SelfRegModuleAction") == 0) &&
  127. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_FileSpecification") == 0)))))
  128. if(FAILED(hr = SelfRegModuleFile(pHandler, atAction, pActionRObj, pCheckRObj))){
  129. if(pActionRObj){
  130. pActionRObj->Cleanup();
  131. delete pActionRObj;
  132. pActionRObj = NULL;
  133. }
  134. if(pCheckRObj){
  135. pCheckRObj->Cleanup();
  136. delete pCheckRObj;
  137. pCheckRObj = NULL;
  138. }
  139. return hr;
  140. }
  141. if((atAction == ACTIONTYPE_ENUM) || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_ShortcutAction") == 0) &&
  142. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_DirectorySpecification") == 0)))))
  143. if(FAILED(hr = ShortcutDirectory(pHandler, atAction, pActionRObj, pCheckRObj))){
  144. if(pActionRObj){
  145. pActionRObj->Cleanup();
  146. delete pActionRObj;
  147. pActionRObj = NULL;
  148. }
  149. if(pCheckRObj){
  150. pCheckRObj->Cleanup();
  151. delete pCheckRObj;
  152. pCheckRObj = NULL;
  153. }
  154. return hr;
  155. }
  156. if((atAction == ACTIONTYPE_ENUM) || ((pActionRObj && pActionRObj->m_bstrClass && (_wcsicmp(pActionRObj->m_bstrClass, L"Win32_TypeLibraryInfoAction") == 0) &&
  157. (pCheckRObj && pCheckRObj->m_bstrClass && (_wcsicmp(pCheckRObj->m_bstrClass, L"Win32_DirectorySpecification") == 0)))))
  158. if(FAILED(hr = TypeLibraryDirectory(pHandler, atAction, pActionRObj, pCheckRObj))){
  159. if(pActionRObj){
  160. pActionRObj->Cleanup();
  161. delete pActionRObj;
  162. pActionRObj = NULL;
  163. }
  164. if(pCheckRObj){
  165. pCheckRObj->Cleanup();
  166. delete pCheckRObj;
  167. pCheckRObj = NULL;
  168. }
  169. return hr;
  170. }
  171. if(pActionRObj){
  172. pActionRObj->Cleanup();
  173. delete pActionRObj;
  174. pActionRObj = NULL;
  175. }
  176. if(pCheckRObj){
  177. pCheckRObj->Cleanup();
  178. delete pCheckRObj;
  179. pCheckRObj = NULL;
  180. }
  181. }catch(...){
  182. if(pActionRObj){
  183. pActionRObj->Cleanup();
  184. delete pActionRObj;
  185. pActionRObj = NULL;
  186. }
  187. if(pCheckRObj){
  188. pCheckRObj->Cleanup();
  189. delete pCheckRObj;
  190. pCheckRObj = NULL;
  191. }
  192. }
  193. return hr;
  194. }
  195. HRESULT CActionCheck::TypeLibraryDirectory(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  196. CRequestObject *pActionData, CRequestObject *pCheckData)
  197. {
  198. HRESULT hr = WBEM_S_NO_ERROR;
  199. MSIHANDLE hView = NULL;
  200. MSIHANDLE hRecord = NULL;
  201. MSIHANDLE hDView = NULL;
  202. MSIHANDLE hDRecord = NULL;
  203. int i = -1;
  204. WCHAR wcBuf[BUFF_SIZE];
  205. WCHAR wcDir[BUFF_SIZE];
  206. WCHAR wcProductCode[39];
  207. WCHAR wcCompID[39];
  208. WCHAR wcTestCode[39];
  209. WCHAR wcLibID[BUFF_SIZE];
  210. DWORD dwBufSize;
  211. bool bMatch = false;
  212. bool bTestCode = false;
  213. UINT uiStatus;
  214. WCHAR wcCheck[BUFF_SIZE];
  215. bool bGotCheck = false;
  216. bool bGotAction = false;
  217. CStringExt wcProp;
  218. if(atAction != ACTIONTYPE_ENUM)
  219. {
  220. // we are doing GetObject so we need to be reinitialized
  221. hr = WBEM_E_NOT_FOUND;
  222. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  223. if(pActionData){
  224. for(int i = 0; i < pActionData->m_iPropCount; i++){
  225. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  226. {
  227. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  228. {
  229. //Get the action we're looking for
  230. wcscpy(wcBuf, pActionData->m_Value[i]);
  231. // safe operation if wcslen ( wcBuf ) > 38
  232. if ( wcslen ( wcBuf ) > 38 )
  233. {
  234. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  235. }
  236. else
  237. {
  238. // we are not good to go, they have sent us longer string
  239. throw hr;
  240. }
  241. // safe because lenght has been tested already in condition
  242. GetFirstGUID(pActionData->m_Value[i], wcLibID);
  243. bGotAction = true;
  244. bTestCode = true;
  245. break;
  246. }
  247. else
  248. {
  249. // we are not good to go, they have sent us longer string
  250. throw hr;
  251. }
  252. }
  253. }
  254. }
  255. if(pCheckData){
  256. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  257. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  258. {
  259. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  260. {
  261. //Get the action we're looking for
  262. wcscpy(wcBuf, pCheckData->m_Value[i]);
  263. // safe operation if wcslen ( wcBuf ) > 38
  264. if ( wcslen ( wcBuf ) > 38 )
  265. {
  266. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  267. }
  268. else
  269. {
  270. // we are not good to go, they have sent us longer string
  271. throw hr;
  272. }
  273. // safe because lenght has been tested already in condition
  274. RemoveFinalGUID(pCheckData->m_Value[i], wcCheck);
  275. //we have a componentized directory... do a little more work
  276. if ((wcCheck[wcslen(wcCheck) - 1] == L'}') &&
  277. (wcCheck[wcslen(wcCheck) - 38] == L'{')
  278. )
  279. {
  280. RemoveFinalGUID(wcCheck, wcCheck);
  281. }
  282. bTestCode = true;
  283. bGotCheck = true;
  284. break;
  285. }
  286. else
  287. {
  288. // we are not good to go, they have sent us longer string
  289. throw hr;
  290. }
  291. }
  292. }
  293. }
  294. }
  295. //These will change from class to class
  296. bool bDriver, bAttribute, bDoneFirst, bValidated;
  297. Query wcQuery;
  298. wcQuery.Append ( 1, L"select distinct `LibID`, `Language`, `Directory_`, `Component_` from TypeLib" );
  299. //optimize for GetObject
  300. if ( bGotCheck || bGotAction )
  301. {
  302. if ( bGotAction )
  303. {
  304. wcQuery.Append ( 3, L" where `LibID`=\'", wcLibID, L"\'" );
  305. }
  306. if ( bGotCheck )
  307. {
  308. if ( bGotAction )
  309. {
  310. wcQuery.Append ( 3, L" or `Directory_`=\'", wcCheck, L"\'" );
  311. }
  312. else
  313. {
  314. wcQuery.Append ( 3, L" where `Directory_`=\'", wcCheck, L"\'" );
  315. }
  316. }
  317. }
  318. QueryExt wcQuery1 ( L"select distinct `ComponentId` from Component where `Directory_`=\'" );
  319. LPWSTR Buffer = NULL;
  320. LPWSTR dynBuffer = NULL;
  321. DWORD dwDynBuffer = 0L;
  322. LPWSTR Buffer1 = NULL;
  323. LPWSTR dynBuffer1 = NULL;
  324. DWORD dwDynBuffer1 = 0L;
  325. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  326. {
  327. // safe operation:
  328. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  329. wcscpy(wcProductCode, m_pRequest->Package(i));
  330. if((atAction == ACTIONTYPE_ENUM) ||
  331. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  332. //Open our database
  333. try
  334. {
  335. if ( GetView ( &hView, wcProductCode, wcQuery, L"TypeLib", TRUE, FALSE ) )
  336. {
  337. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  338. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  339. CheckMSI(uiStatus);
  340. bDoneFirst = false;
  341. dwBufSize = BUFF_SIZE;
  342. GetBufferToPut ( hRecord, 3, dwBufSize, wcDir, dwDynBuffer1, dynBuffer1, Buffer1 );
  343. // make query on fly
  344. wcQuery1.Append ( 2, Buffer1, L"\'" );
  345. if(((uiStatus = g_fpMsiDatabaseOpenViewW(msidata.GetDatabase (), wcQuery1, &hDView)) == ERROR_SUCCESS)
  346. || !bDoneFirst){
  347. if((g_fpMsiViewExecute(hDView, 0) == ERROR_SUCCESS) || !bDoneFirst){
  348. try{
  349. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  350. while(!bMatch && (!bDoneFirst || (uiStatus == ERROR_SUCCESS)) && (hr != WBEM_E_CALL_CANCELLED)){
  351. bValidated = false;
  352. if(uiStatus == ERROR_SUCCESS){
  353. dwBufSize = 39;
  354. CheckMSI(g_fpMsiRecordGetStringW(hDRecord, 1, wcCompID, &dwBufSize));
  355. bValidated = ValidateComponentID(wcCompID, wcProductCode);
  356. }
  357. if(((uiStatus != ERROR_SUCCESS) && !bDoneFirst) || (bValidated && (uiStatus != ERROR_NO_MORE_ITEMS))){
  358. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  359. //----------------------------------------------------
  360. dwBufSize = BUFF_SIZE;
  361. GetBufferToPut ( hRecord, 4, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  362. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  363. {
  364. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  365. {
  366. dynBuffer [ 0 ] = 0;
  367. }
  368. dwBufSize = BUFF_SIZE;
  369. CheckMSI(g_fpMsiRecordGetStringW(hRecord, 1, wcBuf, &dwBufSize));
  370. if(wcscmp(wcBuf, L"") != 0)
  371. {
  372. // safe operation
  373. wcProp.Copy(L"Win32_TypeLibraryInfoAction.ActionID=\"");
  374. wcProp.Append(1, wcBuf);
  375. dwBufSize = BUFF_SIZE;
  376. CheckMSI(g_fpMsiRecordGetStringW(hRecord, 2, wcBuf, &dwBufSize));
  377. wcProp.Append (3, wcBuf, wcProductCode, L"\"");
  378. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  379. if ( Buffer1 && Buffer1 [ 0 ] != 0 )
  380. {
  381. // safe operation
  382. wcProp.Copy(L"Win32_DirectorySpecification.CheckID=\"");
  383. if(uiStatus == ERROR_SUCCESS)
  384. {
  385. wcProp.Append (4, Buffer1, wcCompID, wcProductCode, L"\"");
  386. }
  387. else
  388. {
  389. wcProp.Append (3, Buffer1, wcProductCode, L"\"");
  390. }
  391. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  392. //=====================================================
  393. //----------------------------------------------------
  394. if(bDriver && bAttribute) bMatch = true;
  395. if(!(atAction == ACTIONTYPE_GET) || bMatch){
  396. hr = pHandler->Indicate(1, &m_pObj);
  397. }
  398. }
  399. }
  400. }
  401. else
  402. {
  403. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  404. {
  405. dynBuffer [ 0 ] = 0;
  406. }
  407. }
  408. m_pObj->Release();
  409. m_pObj = NULL;
  410. if(!bDoneFirst) bDoneFirst = true;
  411. }
  412. g_fpMsiCloseHandle(hDRecord);
  413. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  414. }
  415. }catch(...){
  416. g_fpMsiCloseHandle(hDRecord);
  417. g_fpMsiViewClose(hDView);
  418. g_fpMsiCloseHandle(hDView);
  419. throw;
  420. }
  421. g_fpMsiCloseHandle(hDRecord);
  422. g_fpMsiViewClose(hDView);
  423. g_fpMsiCloseHandle(hDView);
  424. }
  425. }
  426. g_fpMsiCloseHandle(hRecord);
  427. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  428. if ( dynBuffer1 && dynBuffer1 [ 0 ] != 0 )
  429. {
  430. dynBuffer1 [ 0 ] = 0;
  431. }
  432. }
  433. }
  434. }
  435. catch(...)
  436. {
  437. if ( dynBuffer )
  438. {
  439. delete [] dynBuffer;
  440. dynBuffer = NULL;
  441. }
  442. if ( dynBuffer1 )
  443. {
  444. delete [] dynBuffer1;
  445. dynBuffer1 = NULL;
  446. }
  447. g_fpMsiCloseHandle(hRecord);
  448. g_fpMsiViewClose(hView);
  449. g_fpMsiCloseHandle(hView);
  450. msidata.CloseDatabase ();
  451. if(m_pObj)
  452. {
  453. m_pObj->Release();
  454. m_pObj = NULL;
  455. }
  456. throw;
  457. }
  458. g_fpMsiCloseHandle(hRecord);
  459. g_fpMsiViewClose(hView);
  460. g_fpMsiCloseHandle(hView);
  461. msidata.CloseDatabase ();
  462. }
  463. }
  464. if ( dynBuffer )
  465. {
  466. delete [] dynBuffer;
  467. dynBuffer = NULL;
  468. }
  469. if ( dynBuffer1 )
  470. {
  471. delete [] dynBuffer1;
  472. dynBuffer1 = NULL;
  473. }
  474. return hr;
  475. }
  476. HRESULT CActionCheck::ShortcutDirectory(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  477. CRequestObject *pActionData, CRequestObject *pCheckData)
  478. {
  479. HRESULT hr = WBEM_S_NO_ERROR;
  480. MSIHANDLE hView = NULL;
  481. MSIHANDLE hRecord = NULL;
  482. MSIHANDLE hDView = NULL;
  483. MSIHANDLE hDRecord = NULL;
  484. int i = -1;
  485. WCHAR wcBuf[BUFF_SIZE];
  486. WCHAR wcDir[BUFF_SIZE];
  487. WCHAR wcProductCode[39];
  488. WCHAR wcCompID[39];
  489. WCHAR wcTestCode[39];
  490. WCHAR wcShortcut[BUFF_SIZE];
  491. DWORD dwBufSize;
  492. bool bMatch = false;
  493. bool bTestCode = false;
  494. UINT uiStatus;
  495. WCHAR wcCheck[BUFF_SIZE];
  496. bool bGotCheck = false;
  497. bool bGotAction = false;
  498. CStringExt wcProp;
  499. if(atAction != ACTIONTYPE_ENUM)
  500. {
  501. // we are doing GetObject so we need to be reinitialized
  502. hr = WBEM_E_NOT_FOUND;
  503. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  504. if(pActionData){
  505. for(int i = 0; i < pActionData->m_iPropCount; i++){
  506. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  507. {
  508. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  509. {
  510. //Get the action we're looking for
  511. wcscpy(wcBuf, pActionData->m_Value[i]);
  512. // safe operation if wcslen ( wcBuf ) > 38
  513. if ( wcslen ( wcBuf ) > 38 )
  514. {
  515. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  516. }
  517. else
  518. {
  519. // we are not good to go, they have sent us longer string
  520. throw hr;
  521. }
  522. // safe because lenght has been tested already in condition
  523. RemoveFinalGUID(pActionData->m_Value[i], wcShortcut);
  524. bGotAction = true;
  525. bTestCode = true;
  526. break;
  527. }
  528. else
  529. {
  530. // we are not good to go, they have sent us longer string
  531. throw hr;
  532. }
  533. }
  534. }
  535. }
  536. if(pCheckData){
  537. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  538. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  539. {
  540. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  541. {
  542. //Get the action we're looking for
  543. wcscpy(wcBuf, pCheckData->m_Value[i]);
  544. // safe operation if wcslen ( wcBuf ) > 38
  545. if ( wcslen ( wcBuf ) > 38 )
  546. {
  547. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  548. }
  549. else
  550. {
  551. // we are not good to go, they have sent us longer string
  552. throw hr;
  553. }
  554. // safe because lenght has been tested already in condition
  555. RemoveFinalGUID(pCheckData->m_Value[i], wcCheck);
  556. //we have a componentized directory... do a little more work
  557. if ((wcCheck[wcslen(wcCheck) - 1] == L'}') &&
  558. (wcCheck[wcslen(wcCheck) - 38] == L'{')
  559. )
  560. {
  561. RemoveFinalGUID(wcCheck, wcCheck);
  562. }
  563. bTestCode = true;
  564. bGotCheck = true;
  565. break;
  566. }
  567. else
  568. {
  569. // we are not good to go, they have sent us longer string
  570. throw hr;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. //These will change from class to class
  577. bool bDriver, bAttribute, bDoneFirst, bValidated;
  578. Query wcQuery;
  579. wcQuery.Append ( 1, L"select distinct `Shortcut`, `Component_`, `Directory_` from Shortcut" );
  580. //optimize for GetObject
  581. if ( bGotCheck || bGotAction )
  582. {
  583. if ( bGotAction )
  584. {
  585. wcQuery.Append ( 3, L" where `Shortcut`=\'", wcShortcut, L"\'" );
  586. }
  587. if ( bGotCheck )
  588. {
  589. if ( bGotAction )
  590. {
  591. wcQuery.Append ( 3, L" or `Directory_`=\'", wcCheck, L"\'" );
  592. }
  593. else
  594. {
  595. wcQuery.Append ( 3, L" where `Directory_`=\'", wcCheck, L"\'" );
  596. }
  597. }
  598. }
  599. QueryExt wcQuery1 ( L"select distinct `ComponentId` from Component where `Directory_`=\'" );
  600. LPWSTR Buffer = NULL;
  601. LPWSTR dynBuffer = NULL;
  602. DWORD dwDynBuffer = 0L;
  603. LPWSTR Buffer1 = NULL;
  604. LPWSTR dynBuffer1 = NULL;
  605. DWORD dwDynBuffer1 = 0L;
  606. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  607. {
  608. // safe operation:
  609. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  610. wcscpy(wcProductCode, m_pRequest->Package(i));
  611. if((atAction == ACTIONTYPE_ENUM) ||
  612. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  613. //Open our database
  614. try
  615. {
  616. if ( GetView ( &hView, wcProductCode, wcQuery, L"Shortcut", TRUE, FALSE ) )
  617. {
  618. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  619. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  620. CheckMSI(uiStatus);
  621. bDoneFirst = false;
  622. dwBufSize = BUFF_SIZE;
  623. GetBufferToPut ( hRecord, 3, dwBufSize, wcDir, dwDynBuffer1, dynBuffer1, Buffer1 );
  624. // make query on fly
  625. wcQuery1.Append ( 2, Buffer1, L"\'" );
  626. if(((uiStatus = g_fpMsiDatabaseOpenViewW(msidata.GetDatabase (), wcQuery1, &hDView)) == ERROR_SUCCESS)
  627. || !bDoneFirst){
  628. if((g_fpMsiViewExecute(hDView, 0) == ERROR_SUCCESS) || !bDoneFirst){
  629. try{
  630. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  631. while(!bMatch && (!bDoneFirst || (uiStatus == ERROR_SUCCESS)) && (hr != WBEM_E_CALL_CANCELLED)){
  632. bValidated = false;
  633. if(uiStatus == ERROR_SUCCESS){
  634. dwBufSize = 39;
  635. CheckMSI(g_fpMsiRecordGetStringW(hDRecord, 1, wcCompID, &dwBufSize));
  636. bValidated = ValidateComponentID(wcCompID, wcProductCode);
  637. }
  638. if(((uiStatus != ERROR_SUCCESS) && !bDoneFirst) || (bValidated && (uiStatus != ERROR_NO_MORE_ITEMS))){
  639. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  640. //----------------------------------------------------
  641. dwBufSize = BUFF_SIZE;
  642. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  643. if ( Buffer && Buffer [ 0 ] != 0 )
  644. {
  645. // safe operation
  646. wcProp.Copy(L"Win32_ShortcutAction.ActionID=\"");
  647. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  648. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  649. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  650. {
  651. dynBuffer [ 0 ] = 0;
  652. }
  653. dwBufSize = BUFF_SIZE;
  654. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  655. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  656. {
  657. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  658. {
  659. dynBuffer [ 0 ] = 0;
  660. }
  661. if ( Buffer1 && Buffer1 [ 0 ] != 0 )
  662. {
  663. // safe operation
  664. wcProp.Copy(L"Win32_DirectorySpecification.CheckID=\"");
  665. if(uiStatus == ERROR_SUCCESS)
  666. {
  667. wcProp.Append (4, Buffer1, wcCompID, wcProductCode, L"\"");
  668. }
  669. else
  670. {
  671. wcProp.Append (3, Buffer1, wcProductCode, L"\"");
  672. }
  673. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  674. //=====================================================
  675. //----------------------------------------------------
  676. if(bDriver && bAttribute) bMatch = true;
  677. if(!(atAction == ACTIONTYPE_GET) || bMatch){
  678. hr = pHandler->Indicate(1, &m_pObj);
  679. }
  680. }
  681. }
  682. else
  683. {
  684. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  685. {
  686. dynBuffer [ 0 ] = 0;
  687. }
  688. }
  689. }
  690. m_pObj->Release();
  691. m_pObj = NULL;
  692. if(!bDoneFirst) bDoneFirst = true;
  693. }
  694. g_fpMsiCloseHandle(hDRecord);
  695. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  696. }
  697. }catch(...){
  698. g_fpMsiCloseHandle(hDRecord);
  699. g_fpMsiViewClose(hDView);
  700. g_fpMsiCloseHandle(hDView);
  701. throw;
  702. }
  703. g_fpMsiCloseHandle(hDRecord);
  704. g_fpMsiViewClose(hDView);
  705. g_fpMsiCloseHandle(hDView);
  706. }
  707. }
  708. g_fpMsiCloseHandle(hRecord);
  709. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  710. if ( dynBuffer1 && dynBuffer1 [ 0 ] != 0 )
  711. {
  712. dynBuffer1 [ 0 ] = 0;
  713. }
  714. }
  715. }
  716. }
  717. catch(...)
  718. {
  719. if ( dynBuffer )
  720. {
  721. delete [] dynBuffer;
  722. dynBuffer = NULL;
  723. }
  724. if ( dynBuffer1 )
  725. {
  726. delete [] dynBuffer1;
  727. dynBuffer1 = NULL;
  728. }
  729. g_fpMsiCloseHandle(hRecord);
  730. g_fpMsiViewClose(hView);
  731. g_fpMsiCloseHandle(hView);
  732. msidata.CloseDatabase ();
  733. if(m_pObj)
  734. {
  735. m_pObj->Release();
  736. m_pObj = NULL;
  737. }
  738. throw;
  739. }
  740. g_fpMsiCloseHandle(hRecord);
  741. g_fpMsiViewClose(hView);
  742. g_fpMsiCloseHandle(hView);
  743. msidata.CloseDatabase ();
  744. }
  745. }
  746. if ( dynBuffer )
  747. {
  748. delete [] dynBuffer;
  749. dynBuffer = NULL;
  750. }
  751. if ( dynBuffer1 )
  752. {
  753. delete [] dynBuffer1;
  754. dynBuffer1 = NULL;
  755. }
  756. return hr;
  757. }
  758. HRESULT CActionCheck::SelfRegModuleFile(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  759. CRequestObject *pActionData, CRequestObject *pCheckData)
  760. {
  761. HRESULT hr = WBEM_S_NO_ERROR;
  762. MSIHANDLE hView = NULL, hRecord = NULL;
  763. int i = -1;
  764. WCHAR wcBuf[BUFF_SIZE];
  765. WCHAR wcProductCode[39];
  766. WCHAR wcTestCode[39];
  767. WCHAR wcFile[BUFF_SIZE];
  768. DWORD dwBufSize;
  769. bool bMatch = false;
  770. bool bTestCode = false;
  771. UINT uiStatus;
  772. bool bGotCheck = false;
  773. bool bGotAction = false;
  774. CStringExt wcProp;
  775. if(atAction != ACTIONTYPE_ENUM)
  776. {
  777. // we are doing GetObject so we need to be reinitialized
  778. hr = WBEM_E_NOT_FOUND;
  779. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  780. if(pActionData){
  781. for(int i = 0; i < pActionData->m_iPropCount; i++){
  782. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  783. {
  784. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  785. {
  786. //Get the action we're looking for
  787. wcscpy(wcBuf, pActionData->m_Value[i]);
  788. // safe operation if wcslen ( wcBuf ) > 38
  789. if ( wcslen ( wcBuf ) > 38 )
  790. {
  791. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  792. }
  793. else
  794. {
  795. // we are not good to go, they have sent us longer string
  796. throw hr;
  797. }
  798. // safe because lenght has been tested already in condition
  799. RemoveFinalGUID(pActionData->m_Value[i], wcFile);
  800. bGotAction = true;
  801. bTestCode = true;
  802. break;
  803. }
  804. else
  805. {
  806. // we are not good to go, they have sent us longer string
  807. throw hr;
  808. }
  809. }
  810. }
  811. }
  812. if(pCheckData){
  813. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  814. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  815. {
  816. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  817. {
  818. //Get the action we're looking for
  819. wcscpy(wcBuf, pCheckData->m_Value[i]);
  820. // safe operation if wcslen ( wcBuf ) > 38
  821. if ( wcslen ( wcBuf ) > 38 )
  822. {
  823. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  824. }
  825. else
  826. {
  827. // we are not good to go, they have sent us longer string
  828. throw hr;
  829. }
  830. // safe because lenght has been tested already in condition
  831. RemoveFinalGUID(pCheckData->m_Value[i], wcFile);
  832. bTestCode = true;
  833. bGotCheck = true;
  834. break;
  835. }
  836. else
  837. {
  838. // we are not good to go, they have sent us longer string
  839. throw hr;
  840. }
  841. }
  842. }
  843. }
  844. }
  845. //These will change from class to class
  846. bool bDriver, bAttribute;
  847. Query wcQuery;
  848. wcQuery.Append ( 1, L"select distinct `File_` from SelfReg" );
  849. //optimize for GetObject
  850. if ( bGotCheck || bGotAction )
  851. {
  852. wcQuery.Append ( 3, L" where `File_`=\'", wcFile, L"\'" );
  853. }
  854. LPWSTR Buffer = NULL;
  855. LPWSTR dynBuffer = NULL;
  856. DWORD dwDynBuffer = 0L;
  857. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  858. {
  859. // safe operation:
  860. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  861. wcscpy(wcProductCode, m_pRequest->Package(i));
  862. if((atAction == ACTIONTYPE_ENUM) ||
  863. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  864. //Open our database
  865. try
  866. {
  867. if ( GetView ( &hView, wcProductCode, wcQuery, L"SelfReg", TRUE, FALSE ) )
  868. {
  869. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  870. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  871. CheckMSI(uiStatus);
  872. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  873. //----------------------------------------------------
  874. dwBufSize = BUFF_SIZE;
  875. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  876. if ( Buffer && Buffer [ 0 ] != 0 )
  877. {
  878. // safe operation
  879. wcProp.Copy(L"Win32_SelfRegModuleAction.ActionID=\"");
  880. wcProp.Append (3, Buffer, wcProductCode, L"\"");
  881. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  882. // safe operation
  883. wcProp.Copy(L"Win32_FileSpecification.CheckID=\"");
  884. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  885. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  886. //=====================================================
  887. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  888. {
  889. dynBuffer [ 0 ] = 0;
  890. }
  891. //----------------------------------------------------
  892. if(bDriver && bAttribute) bMatch = true;
  893. if(!(atAction == ACTIONTYPE_GET) || bMatch){
  894. hr = pHandler->Indicate(1, &m_pObj);
  895. }
  896. }
  897. m_pObj->Release();
  898. m_pObj = NULL;
  899. g_fpMsiCloseHandle(hRecord);
  900. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  901. }
  902. }
  903. }
  904. catch(...)
  905. {
  906. if ( dynBuffer )
  907. {
  908. delete [] dynBuffer;
  909. dynBuffer = NULL;
  910. }
  911. if (hRecord)
  912. g_fpMsiCloseHandle(hRecord);
  913. if (hView)
  914. {
  915. g_fpMsiViewClose(hView);
  916. g_fpMsiCloseHandle(hView);
  917. }
  918. msidata.CloseDatabase ();
  919. if(m_pObj)
  920. {
  921. m_pObj->Release();
  922. m_pObj = NULL;
  923. }
  924. throw;
  925. }
  926. if (hRecord)
  927. g_fpMsiCloseHandle(hRecord);
  928. if (hView)
  929. {
  930. g_fpMsiViewClose(hView);
  931. g_fpMsiCloseHandle(hView);
  932. }
  933. msidata.CloseDatabase ();
  934. }
  935. }
  936. if ( dynBuffer )
  937. {
  938. delete [] dynBuffer;
  939. dynBuffer = NULL;
  940. }
  941. return hr;
  942. }
  943. HRESULT CActionCheck::RemoveIniDirectory(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  944. CRequestObject *pActionData, CRequestObject *pCheckData)
  945. {
  946. HRESULT hr = WBEM_S_NO_ERROR;
  947. MSIHANDLE hView = NULL;
  948. MSIHANDLE hRecord = NULL;
  949. MSIHANDLE hDView = NULL;
  950. MSIHANDLE hDRecord = NULL;
  951. int i = -1;
  952. WCHAR wcBuf[BUFF_SIZE];
  953. WCHAR wcDir[BUFF_SIZE];
  954. WCHAR wcProductCode[39];
  955. WCHAR wcCompID[39];
  956. WCHAR wcTestCode[39];
  957. WCHAR wcIniFile[BUFF_SIZE];
  958. DWORD dwBufSize;
  959. bool bMatch = false;
  960. bool bTestCode = false;
  961. UINT uiStatus;
  962. WCHAR wcCheck[BUFF_SIZE];
  963. bool bGotCheck = false;
  964. bool bGotAction = false;
  965. CStringExt wcProp;
  966. if(atAction != ACTIONTYPE_ENUM)
  967. {
  968. // we are doing GetObject so we need to be reinitialized
  969. hr = WBEM_E_NOT_FOUND;
  970. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  971. if(pActionData){
  972. for(int i = 0; i < pActionData->m_iPropCount; i++){
  973. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  974. {
  975. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  976. {
  977. //Get the action we're looking for
  978. wcscpy(wcBuf, pActionData->m_Value[i]);
  979. // safe operation if wcslen ( wcBuf ) > 38
  980. if ( wcslen ( wcBuf ) > 38 )
  981. {
  982. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  983. }
  984. else
  985. {
  986. // we are not good to go, they have sent us longer string
  987. throw hr;
  988. }
  989. // safe because lenght has been tested already in condition
  990. RemoveFinalGUID(pActionData->m_Value[i], wcIniFile);
  991. bGotAction = true;
  992. bTestCode = true;
  993. break;
  994. }
  995. else
  996. {
  997. // we are not good to go, they have sent us longer string
  998. throw hr;
  999. }
  1000. }
  1001. }
  1002. }
  1003. if(pCheckData){
  1004. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  1005. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  1006. {
  1007. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  1008. {
  1009. //Get the action we're looking for
  1010. wcscpy(wcBuf, pCheckData->m_Value[i]);
  1011. // safe operation if wcslen ( wcBuf ) > 38
  1012. if ( wcslen ( wcBuf ) > 38 )
  1013. {
  1014. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1015. }
  1016. else
  1017. {
  1018. // we are not good to go, they have sent us longer string
  1019. throw hr;
  1020. }
  1021. // safe because lenght has been tested already in condition
  1022. RemoveFinalGUID(pCheckData->m_Value[i], wcCheck);
  1023. //we have a componentized directory... do a little more work
  1024. if ((wcCheck[wcslen(wcCheck) - 1] == L'}') &&
  1025. (wcCheck[wcslen(wcCheck) - 38] == L'{')
  1026. )
  1027. {
  1028. RemoveFinalGUID(wcCheck, wcCheck);
  1029. }
  1030. bTestCode = true;
  1031. bGotCheck = true;
  1032. break;
  1033. }
  1034. else
  1035. {
  1036. // we are not good to go, they have sent us longer string
  1037. throw hr;
  1038. }
  1039. }
  1040. }
  1041. }
  1042. }
  1043. //These will change from class to class
  1044. bool bDriver, bAttribute, bDoneFirst, bValidated;
  1045. Query wcQuery;
  1046. wcQuery.Append ( 1, L"select distinct `RemoveIniFile`, `Component_`, `DirProperty` from RemoveIniFile" );
  1047. //optimize for GetObject
  1048. if ( bGotCheck || bGotAction )
  1049. {
  1050. if ( bGotAction )
  1051. {
  1052. wcQuery.Append ( 3, L" where `RemoveIniFile`=\'", wcIniFile, L"\'" );
  1053. }
  1054. if ( bGotCheck )
  1055. {
  1056. if ( bGotAction )
  1057. {
  1058. wcQuery.Append ( 3, L" or `DirProperty`=\'", wcCheck, L"\'" );
  1059. }
  1060. else
  1061. {
  1062. wcQuery.Append ( 3, L" where `DirProperty`=\'", wcCheck, L"\'" );
  1063. }
  1064. }
  1065. }
  1066. QueryExt wcQuery1 ( L"select distinct `ComponentId` from Component where `Directory_`=\'" );
  1067. LPWSTR Buffer = NULL;
  1068. LPWSTR dynBuffer = NULL;
  1069. DWORD dwDynBuffer = 0L;
  1070. LPWSTR Buffer1 = NULL;
  1071. LPWSTR dynBuffer1 = NULL;
  1072. DWORD dwDynBuffer1 = 0L;
  1073. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  1074. {
  1075. // safe operation:
  1076. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  1077. wcscpy(wcProductCode, m_pRequest->Package(i));
  1078. if((atAction == ACTIONTYPE_ENUM) ||
  1079. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  1080. //Open our database
  1081. try
  1082. {
  1083. if ( GetView ( &hView, wcProductCode, wcQuery, L"RemoveIniFile", TRUE, FALSE ) )
  1084. {
  1085. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1086. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  1087. CheckMSI(uiStatus);
  1088. bDoneFirst = false;
  1089. dwBufSize = BUFF_SIZE;
  1090. GetBufferToPut ( hRecord, 3, dwBufSize, wcDir, dwDynBuffer1, dynBuffer1, Buffer1 );
  1091. // make query on fly
  1092. wcQuery1.Append ( 2, Buffer1, L"\'" );
  1093. if(((uiStatus = g_fpMsiDatabaseOpenViewW(msidata.GetDatabase(), wcQuery1, &hDView)) == ERROR_SUCCESS)
  1094. || !bDoneFirst){
  1095. if((g_fpMsiViewExecute(hDView, 0) == ERROR_SUCCESS) || !bDoneFirst){
  1096. try{
  1097. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  1098. while(!bMatch && (!bDoneFirst || (uiStatus == ERROR_SUCCESS)) && (hr != WBEM_E_CALL_CANCELLED)){
  1099. bValidated = false;
  1100. if(uiStatus == ERROR_SUCCESS){
  1101. dwBufSize = 39;
  1102. CheckMSI(g_fpMsiRecordGetStringW(hDRecord, 1, wcCompID, &dwBufSize));
  1103. bValidated = ValidateComponentID(wcCompID, wcProductCode);
  1104. }
  1105. if(((uiStatus != ERROR_SUCCESS) && !bDoneFirst) || (bValidated && (uiStatus != ERROR_NO_MORE_ITEMS))){
  1106. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  1107. //----------------------------------------------------
  1108. dwBufSize = BUFF_SIZE;
  1109. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1110. if ( Buffer && Buffer [ 0 ] != 0 )
  1111. {
  1112. // safe operation
  1113. wcProp.Copy(L"Win32_RemoveIniAction.ActionID=\"");
  1114. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  1115. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  1116. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1117. {
  1118. dynBuffer [ 0 ] = 0;
  1119. }
  1120. dwBufSize = BUFF_SIZE;
  1121. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1122. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  1123. {
  1124. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1125. {
  1126. dynBuffer [ 0 ] = 0;
  1127. }
  1128. if ( Buffer1 && Buffer1 [ 0 ] != 0 )
  1129. {
  1130. // safe operation
  1131. wcProp.Copy(L"Win32_DirectorySpecification.CheckID=\"");
  1132. if(uiStatus == ERROR_SUCCESS)
  1133. {
  1134. wcProp.Append (4, Buffer1, wcCompID, wcProductCode, L"\"");
  1135. }
  1136. else
  1137. {
  1138. wcProp.Append (3, Buffer1, wcProductCode, L"\"");
  1139. }
  1140. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  1141. //=====================================================
  1142. //----------------------------------------------------
  1143. if(bDriver && bAttribute) bMatch = true;
  1144. if(!(atAction == ACTIONTYPE_GET) || bMatch){
  1145. hr = pHandler->Indicate(1, &m_pObj);
  1146. }
  1147. }
  1148. }
  1149. else
  1150. {
  1151. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1152. {
  1153. dynBuffer [ 0 ] = 0;
  1154. }
  1155. }
  1156. }
  1157. m_pObj->Release();
  1158. m_pObj = NULL;
  1159. if(!bDoneFirst) bDoneFirst = true;
  1160. }
  1161. g_fpMsiCloseHandle(hDRecord);
  1162. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  1163. }
  1164. }catch(...){
  1165. g_fpMsiCloseHandle(hDRecord);
  1166. g_fpMsiViewClose(hDView);
  1167. g_fpMsiCloseHandle(hDView);
  1168. throw;
  1169. }
  1170. g_fpMsiCloseHandle(hDRecord);
  1171. g_fpMsiViewClose(hDView);
  1172. g_fpMsiCloseHandle(hDView);
  1173. }
  1174. }
  1175. g_fpMsiCloseHandle(hRecord);
  1176. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1177. if ( dynBuffer1 && dynBuffer1 [ 0 ] != 0 )
  1178. {
  1179. dynBuffer1 [ 0 ] = 0;
  1180. }
  1181. }
  1182. }
  1183. }
  1184. catch(...)
  1185. {
  1186. if ( dynBuffer )
  1187. {
  1188. delete [] dynBuffer;
  1189. dynBuffer = NULL;
  1190. }
  1191. if ( dynBuffer1 )
  1192. {
  1193. delete [] dynBuffer1;
  1194. dynBuffer1 = NULL;
  1195. }
  1196. g_fpMsiCloseHandle(hRecord);
  1197. g_fpMsiViewClose(hView);
  1198. g_fpMsiCloseHandle(hView);
  1199. msidata.CloseDatabase ();
  1200. if(m_pObj)
  1201. {
  1202. m_pObj->Release();
  1203. m_pObj = NULL;
  1204. }
  1205. throw;
  1206. }
  1207. g_fpMsiCloseHandle(hRecord);
  1208. g_fpMsiViewClose(hView);
  1209. g_fpMsiCloseHandle(hView);
  1210. msidata.CloseDatabase ();
  1211. }
  1212. }
  1213. if ( dynBuffer )
  1214. {
  1215. delete [] dynBuffer;
  1216. dynBuffer = NULL;
  1217. }
  1218. if ( dynBuffer1 )
  1219. {
  1220. delete [] dynBuffer1;
  1221. dynBuffer1 = NULL;
  1222. }
  1223. return hr;
  1224. }
  1225. HRESULT CActionCheck::ProgIDSpecificationClass(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  1226. CRequestObject *pActionData, CRequestObject *pCheckData)
  1227. {
  1228. HRESULT hr = WBEM_S_NO_ERROR;
  1229. MSIHANDLE hView = NULL;
  1230. MSIHANDLE hRecord = NULL;
  1231. MSIHANDLE hCView = NULL;
  1232. MSIHANDLE hCRecord = NULL;
  1233. int i = -1;
  1234. WCHAR wcBuf[BUFF_SIZE];
  1235. WCHAR wcProductCode[39];
  1236. WCHAR wcTestCode[39];
  1237. WCHAR wcProgID[BUFF_SIZE];
  1238. DWORD dwBufSize;
  1239. bool bMatch = false;
  1240. bool bTestCode = false;
  1241. UINT uiStatus;
  1242. WCHAR wcCheck[BUFF_SIZE];
  1243. bool bGotCheck = false;
  1244. bool bGotAction = false;
  1245. CStringExt wcProp;
  1246. if(atAction != ACTIONTYPE_ENUM)
  1247. {
  1248. // we are doing GetObject so we need to be reinitialized
  1249. hr = WBEM_E_NOT_FOUND;
  1250. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  1251. if(pActionData){
  1252. for(int i = 0; i < pActionData->m_iPropCount; i++){
  1253. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  1254. {
  1255. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  1256. {
  1257. //Get the action we're looking for
  1258. wcscpy(wcBuf, pActionData->m_Value[i]);
  1259. // safe operation if wcslen ( wcBuf ) > 38
  1260. if ( wcslen ( wcBuf ) > 38 )
  1261. {
  1262. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1263. }
  1264. else
  1265. {
  1266. // we are not good to go, they have sent us longer string
  1267. throw hr;
  1268. }
  1269. // safe because lenght has been tested already in condition
  1270. GetFirstGUID(pActionData->m_Value[i], wcProgID);
  1271. bGotAction = true;
  1272. bTestCode = true;
  1273. break;
  1274. }
  1275. else
  1276. {
  1277. // we are not good to go, they have sent us longer string
  1278. throw hr;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. if(pCheckData){
  1284. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  1285. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  1286. {
  1287. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  1288. {
  1289. //Get the action we're looking for
  1290. wcscpy(wcBuf, pCheckData->m_Value[i]);
  1291. // safe operation if wcslen ( wcBuf ) > 38
  1292. if ( wcslen ( wcBuf ) > 38 )
  1293. {
  1294. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1295. }
  1296. else
  1297. {
  1298. // we are not good to go, they have sent us longer string
  1299. throw hr;
  1300. }
  1301. // safe because lenght has been tested already in condition
  1302. RemoveFinalGUID(pCheckData->m_Value[i], wcCheck);
  1303. bTestCode = true;
  1304. bGotCheck = true;
  1305. break;
  1306. }
  1307. else
  1308. {
  1309. // we are not good to go, they have sent us longer string
  1310. throw hr;
  1311. }
  1312. }
  1313. }
  1314. }
  1315. }
  1316. //These will change from class to class
  1317. bool bDriver, bAttribute;
  1318. Query wcQuery;
  1319. wcQuery.Append ( 1, L"select distinct `ProgId`, `Class_` from ProgId" );
  1320. //optimize for GetObject
  1321. if ( bGotCheck || bGotAction )
  1322. {
  1323. if ( bGotAction )
  1324. {
  1325. wcQuery.Append ( 3, L" where `Class_`=\'", wcProgID, L"\'" );
  1326. }
  1327. if ( bGotCheck )
  1328. {
  1329. if ( bGotAction )
  1330. {
  1331. wcQuery.Append ( 3, L" or `ProgId`=\'", wcCheck, L"\'" );
  1332. }
  1333. else
  1334. {
  1335. wcQuery.Append ( 3, L" where `ProgId`=\'", wcCheck, L"\'" );
  1336. }
  1337. }
  1338. }
  1339. QueryExt wcQuery1 ( L"select distinct `Context`, `Component_` from Class where `CLSID`=\'" );
  1340. LPWSTR Buffer = NULL;
  1341. LPWSTR dynBuffer = NULL;
  1342. DWORD dwDynBuffer = 0L;
  1343. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  1344. {
  1345. // safe operation:
  1346. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  1347. wcscpy(wcProductCode, m_pRequest->Package(i));
  1348. if((atAction == ACTIONTYPE_ENUM) ||
  1349. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  1350. //Open our database
  1351. try{
  1352. if ( GetView ( &hView, wcProductCode, wcQuery, L"ProgId", TRUE, FALSE ) )
  1353. {
  1354. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1355. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  1356. CheckMSI(uiStatus);
  1357. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  1358. //----------------------------------------------------
  1359. dwBufSize = BUFF_SIZE;
  1360. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1361. if ( Buffer && Buffer [ 0 ] != 0 )
  1362. {
  1363. // safe operation
  1364. wcProp.Copy(L"Win32_ProgIDSpecification.CheckID=\"");
  1365. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  1366. PutKeyProperty(m_pObj, pCheck, wcProp, &bDriver, m_pRequest);
  1367. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1368. {
  1369. dynBuffer [ 0 ] = 0;
  1370. }
  1371. wcProp.Copy(L"Win32_ClassInfoAction.ActionID=\"");
  1372. dwBufSize = BUFF_SIZE;
  1373. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1374. if ( Buffer && Buffer [ 0 ] != 0 )
  1375. {
  1376. wcProp.Append ( 1, Buffer );
  1377. // make query on fly
  1378. wcQuery1.Append ( 2, Buffer, L"\'" );
  1379. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1380. {
  1381. dynBuffer [ 0 ] = 0;
  1382. }
  1383. CheckMSI(g_fpMsiDatabaseOpenViewW(msidata.GetDatabase(), wcQuery1, &hCView));
  1384. CheckMSI(g_fpMsiViewExecute(hCView, 0));
  1385. try{
  1386. uiStatus = g_fpMsiViewFetch(hCView, &hCRecord);
  1387. if(uiStatus != ERROR_NO_MORE_ITEMS){
  1388. dwBufSize = BUFF_SIZE;
  1389. GetBufferToPut ( hCRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1390. wcProp.Append (3, Buffer, wcProductCode, L"\"");
  1391. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1392. {
  1393. dynBuffer [ 0 ] = 0;
  1394. }
  1395. dwBufSize = BUFF_SIZE;
  1396. GetBufferToPut ( hCRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1397. if ( ValidateComponentName ( msidata.GetDatabase (), wcProductCode, Buffer ) )
  1398. {
  1399. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1400. {
  1401. dynBuffer [ 0 ] = 0;
  1402. }
  1403. PutKeyProperty(m_pObj, pAction, wcProp, &bAttribute, m_pRequest);
  1404. //----------------------------------------------------
  1405. if(bDriver && bAttribute) bMatch = true;
  1406. if((atAction != ACTIONTYPE_GET) || bMatch){
  1407. hr = pHandler->Indicate(1, &m_pObj);
  1408. }
  1409. }
  1410. else
  1411. {
  1412. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1413. {
  1414. dynBuffer [ 0 ] = 0;
  1415. }
  1416. }
  1417. }else throw WBEM_E_FAILED;
  1418. }catch(...){
  1419. g_fpMsiCloseHandle(hCRecord);
  1420. g_fpMsiViewClose(hCView);
  1421. g_fpMsiCloseHandle(hCView);
  1422. throw;
  1423. }
  1424. g_fpMsiCloseHandle(hCRecord);
  1425. g_fpMsiViewClose(hCView);
  1426. g_fpMsiCloseHandle(hCView);
  1427. }else bMatch = false;
  1428. }
  1429. m_pObj->Release();
  1430. m_pObj = NULL;
  1431. g_fpMsiCloseHandle(hRecord);
  1432. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1433. }
  1434. }
  1435. }
  1436. catch(...)
  1437. {
  1438. if ( dynBuffer )
  1439. {
  1440. delete [] dynBuffer;
  1441. dynBuffer = NULL;
  1442. }
  1443. g_fpMsiCloseHandle(hRecord);
  1444. g_fpMsiViewClose(hView);
  1445. g_fpMsiCloseHandle(hView);
  1446. msidata.CloseDatabase ();
  1447. if(m_pObj)
  1448. {
  1449. m_pObj->Release();
  1450. m_pObj = NULL;
  1451. }
  1452. throw;
  1453. }
  1454. g_fpMsiCloseHandle(hRecord);
  1455. g_fpMsiViewClose(hView);
  1456. g_fpMsiCloseHandle(hView);
  1457. msidata.CloseDatabase ();
  1458. }
  1459. }
  1460. if ( dynBuffer )
  1461. {
  1462. delete [] dynBuffer;
  1463. dynBuffer = NULL;
  1464. }
  1465. return hr;
  1466. }
  1467. HRESULT CActionCheck::FontInfoFile(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  1468. CRequestObject *pActionData, CRequestObject *pCheckData)
  1469. {
  1470. HRESULT hr = WBEM_S_NO_ERROR;
  1471. MSIHANDLE hView = NULL, hRecord = NULL;
  1472. int i = -1;
  1473. WCHAR wcBuf[BUFF_SIZE];
  1474. WCHAR wcProductCode[39];
  1475. WCHAR wcTestCode[39];
  1476. WCHAR wcFile[BUFF_SIZE];
  1477. DWORD dwBufSize;
  1478. bool bMatch = false;
  1479. bool bTestCode = false;
  1480. UINT uiStatus;
  1481. bool bGotCheck = false;
  1482. bool bGotAction = false;
  1483. CStringExt wcProp;
  1484. if(atAction != ACTIONTYPE_ENUM)
  1485. {
  1486. // we are doing GetObject so we need to be reinitialized
  1487. hr = WBEM_E_NOT_FOUND;
  1488. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  1489. if(pActionData){
  1490. for(int i = 0; i < pActionData->m_iPropCount; i++){
  1491. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  1492. {
  1493. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  1494. {
  1495. //Get the action we're looking for
  1496. wcscpy(wcBuf, pActionData->m_Value[i]);
  1497. // safe operation if wcslen ( wcBuf ) > 38
  1498. if ( wcslen ( wcBuf ) > 38 )
  1499. {
  1500. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1501. }
  1502. else
  1503. {
  1504. // we are not good to go, they have sent us longer string
  1505. throw hr;
  1506. }
  1507. // safe because lenght has been tested already in condition
  1508. RemoveFinalGUID(pActionData->m_Value[i], wcFile);
  1509. bGotAction = true;
  1510. bTestCode = true;
  1511. break;
  1512. }
  1513. else
  1514. {
  1515. // we are not good to go, they have sent us longer string
  1516. throw hr;
  1517. }
  1518. }
  1519. }
  1520. }
  1521. if(pCheckData){
  1522. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  1523. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  1524. {
  1525. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  1526. {
  1527. //Get the action we're looking for
  1528. wcscpy(wcBuf, pCheckData->m_Value[i]);
  1529. // safe operation if wcslen ( wcBuf ) > 38
  1530. if ( wcslen ( wcBuf ) > 38 )
  1531. {
  1532. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1533. }
  1534. else
  1535. {
  1536. // we are not good to go, they have sent us longer string
  1537. throw hr;
  1538. }
  1539. // safe because lenght has been tested already in condition
  1540. RemoveFinalGUID(pCheckData->m_Value[i], wcFile);
  1541. bTestCode = true;
  1542. bGotCheck = true;
  1543. break;
  1544. }
  1545. else
  1546. {
  1547. // we are not good to go, they have sent us longer string
  1548. throw hr;
  1549. }
  1550. }
  1551. }
  1552. }
  1553. }
  1554. //These will change from class to class
  1555. bool bDriver, bAttribute;
  1556. Query wcQuery;
  1557. wcQuery.Append ( 1, L"select distinct `File_` from Font" );
  1558. //optimize for GetObject
  1559. if ( bGotCheck || bGotAction )
  1560. {
  1561. wcQuery.Append ( 3, L" where `File_`=\'", wcFile, L"\'" );
  1562. }
  1563. LPWSTR Buffer = NULL;
  1564. LPWSTR dynBuffer = NULL;
  1565. DWORD dwDynBuffer = 0L;
  1566. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  1567. {
  1568. // safe operation:
  1569. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  1570. wcscpy(wcProductCode, m_pRequest->Package(i));
  1571. if((atAction == ACTIONTYPE_ENUM) ||
  1572. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  1573. //Open our database
  1574. try
  1575. {
  1576. if ( GetView ( &hView, wcProductCode, wcQuery, L"Font", TRUE, FALSE ) )
  1577. {
  1578. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1579. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  1580. CheckMSI(uiStatus);
  1581. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  1582. //----------------------------------------------------
  1583. dwBufSize = BUFF_SIZE;
  1584. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1585. if ( Buffer && Buffer [ 0 ] != 0 )
  1586. {
  1587. // safe operation
  1588. wcProp.Copy(L"Win32_FontInfoAction.ActionID=\"");
  1589. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  1590. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  1591. // safe operation
  1592. wcProp.Copy(L"Win32_FileSpecification.CheckID=\"");
  1593. wcProp.Append ( 3, Buffer, wcProductCode, L"\"");
  1594. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  1595. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1596. {
  1597. dynBuffer [ 0 ] = 0;
  1598. }
  1599. //----------------------------------------------------
  1600. if(bDriver && bAttribute) bMatch = true;
  1601. if((atAction != ACTIONTYPE_GET) || bMatch){
  1602. hr = pHandler->Indicate(1, &m_pObj);
  1603. }
  1604. }
  1605. m_pObj->Release();
  1606. m_pObj = NULL;
  1607. g_fpMsiCloseHandle(hRecord);
  1608. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1609. }
  1610. }
  1611. }
  1612. catch(...)
  1613. {
  1614. if ( dynBuffer )
  1615. {
  1616. delete [] dynBuffer;
  1617. dynBuffer = NULL;
  1618. }
  1619. if (hRecord)
  1620. g_fpMsiCloseHandle(hRecord);
  1621. if (hView)
  1622. {
  1623. g_fpMsiViewClose(hView);
  1624. g_fpMsiCloseHandle(hView);
  1625. }
  1626. msidata.CloseDatabase ();
  1627. if(m_pObj)
  1628. {
  1629. m_pObj->Release();
  1630. m_pObj = NULL;
  1631. }
  1632. throw;
  1633. }
  1634. if (hRecord)
  1635. g_fpMsiCloseHandle(hRecord);
  1636. if (hView)
  1637. {
  1638. g_fpMsiViewClose(hView);
  1639. g_fpMsiCloseHandle(hView);
  1640. }
  1641. msidata.CloseDatabase ();
  1642. }
  1643. }
  1644. if ( dynBuffer )
  1645. {
  1646. delete [] dynBuffer;
  1647. dynBuffer = NULL;
  1648. }
  1649. return hr;
  1650. }
  1651. HRESULT CActionCheck::FileDuplicateFile(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  1652. CRequestObject *pActionData, CRequestObject *pCheckData)
  1653. {
  1654. HRESULT hr = WBEM_S_NO_ERROR;
  1655. MSIHANDLE hView = NULL;
  1656. MSIHANDLE hRecord = NULL;
  1657. int i = -1;
  1658. WCHAR wcBuf[BUFF_SIZE];
  1659. WCHAR wcProductCode[39];
  1660. WCHAR wcTestCode[39];
  1661. WCHAR wcFile[BUFF_SIZE];
  1662. DWORD dwBufSize;
  1663. bool bMatch = false;
  1664. bool bTestCode = false;
  1665. UINT uiStatus;
  1666. WCHAR wcCheck[BUFF_SIZE];
  1667. bool bGotCheck = false;
  1668. bool bGotAction = false;
  1669. CStringExt wcProp;
  1670. if(atAction != ACTIONTYPE_ENUM)
  1671. {
  1672. // we are doing GetObject so we need to be reinitialized
  1673. hr = WBEM_E_NOT_FOUND;
  1674. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  1675. if(pActionData){
  1676. for(int i = 0; i < pActionData->m_iPropCount; i++){
  1677. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  1678. {
  1679. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  1680. {
  1681. //Get the action we're looking for
  1682. wcscpy(wcBuf, pActionData->m_Value[i]);
  1683. // safe operation if wcslen ( wcBuf ) > 38
  1684. if ( wcslen ( wcBuf ) > 38 )
  1685. {
  1686. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1687. }
  1688. else
  1689. {
  1690. // we are not good to go, they have sent us longer string
  1691. throw hr;
  1692. }
  1693. // safe because lenght has been tested already in condition
  1694. RemoveFinalGUID(pActionData->m_Value[i], wcFile);
  1695. bGotAction = true;
  1696. bTestCode = true;
  1697. break;
  1698. }
  1699. else
  1700. {
  1701. // we are not good to go, they have sent us longer string
  1702. throw hr;
  1703. }
  1704. }
  1705. }
  1706. }
  1707. if(pCheckData){
  1708. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  1709. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  1710. {
  1711. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  1712. {
  1713. //Get the action we're looking for
  1714. wcscpy(wcBuf, pCheckData->m_Value[i]);
  1715. // safe operation if wcslen ( wcBuf ) > 38
  1716. if ( wcslen ( wcBuf ) > 38 )
  1717. {
  1718. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1719. }
  1720. else
  1721. {
  1722. // we are not good to go, they have sent us longer string
  1723. throw hr;
  1724. }
  1725. // safe because lenght has been tested already in condition
  1726. RemoveFinalGUID(pCheckData->m_Value[i], wcCheck);
  1727. bTestCode = true;
  1728. bGotCheck = true;
  1729. break;
  1730. }
  1731. else
  1732. {
  1733. // we are not good to go, they have sent us longer string
  1734. throw hr;
  1735. }
  1736. }
  1737. }
  1738. }
  1739. }
  1740. //These will change from class to class
  1741. bool bDriver, bAttribute;
  1742. Query wcQuery;
  1743. wcQuery.Append ( 1, L"select distinct `FileKey`, `Component_`, `File_` from DuplicateFile" );
  1744. //optimize for GetObject
  1745. if ( bGotCheck || bGotAction )
  1746. {
  1747. if ( bGotAction )
  1748. {
  1749. wcQuery.Append ( 3, L" where `FileKey`=\'", wcFile, L"\'" );
  1750. }
  1751. if ( bGotCheck )
  1752. {
  1753. if ( bGotAction )
  1754. {
  1755. wcQuery.Append ( 3, L" or `File_`=\'", wcCheck, L"\'" );
  1756. }
  1757. else
  1758. {
  1759. wcQuery.Append ( 3, L" where `File_`=\'", wcCheck, L"\'" );
  1760. }
  1761. }
  1762. }
  1763. LPWSTR Buffer = NULL;
  1764. LPWSTR dynBuffer = NULL;
  1765. DWORD dwDynBuffer = 0L;
  1766. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  1767. {
  1768. // safe operation:
  1769. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  1770. wcscpy(wcProductCode, m_pRequest->Package(i));
  1771. if((atAction == ACTIONTYPE_ENUM) ||
  1772. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  1773. //Open our database
  1774. try
  1775. {
  1776. if ( GetView ( &hView, wcProductCode, wcQuery, L"DuplicateFile", TRUE, FALSE ) )
  1777. {
  1778. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1779. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  1780. CheckMSI(uiStatus);
  1781. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  1782. //----------------------------------------------------
  1783. dwBufSize = BUFF_SIZE;
  1784. GetBufferToPut ( hRecord, 1, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1785. if ( Buffer && Buffer [ 0 ] != 0 )
  1786. {
  1787. // safe operation
  1788. wcProp.Copy(L"Win32_DuplicateFileAction.ActionID=\"");
  1789. wcProp.Append (3, Buffer, wcProductCode, L"\"");
  1790. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  1791. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1792. {
  1793. dynBuffer [ 0 ] = 0;
  1794. }
  1795. dwBufSize = BUFF_SIZE;
  1796. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1797. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  1798. {
  1799. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1800. {
  1801. dynBuffer [ 0 ] = 0;
  1802. }
  1803. dwBufSize = BUFF_SIZE;
  1804. GetBufferToPut ( hRecord, 3, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  1805. if ( Buffer && Buffer [ 0 ] != 0 )
  1806. {
  1807. // safe operation
  1808. wcProp.Copy(L"Win32_FileSpecification.CheckID=\"");
  1809. wcProp.Append (3, Buffer, wcProductCode, L"\"");
  1810. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  1811. //=====================================================
  1812. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1813. {
  1814. dynBuffer [ 0 ] = 0;
  1815. }
  1816. //----------------------------------------------------
  1817. if(bDriver && bAttribute) bMatch = true;
  1818. if((atAction != ACTIONTYPE_GET) || bMatch){
  1819. hr = pHandler->Indicate(1, &m_pObj);
  1820. }
  1821. }
  1822. }
  1823. else
  1824. {
  1825. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  1826. {
  1827. dynBuffer [ 0 ] = 0;
  1828. }
  1829. }
  1830. }
  1831. m_pObj->Release();
  1832. m_pObj = NULL;
  1833. g_fpMsiCloseHandle(hRecord);
  1834. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1835. }
  1836. }
  1837. }
  1838. catch(...)
  1839. {
  1840. if ( dynBuffer )
  1841. {
  1842. delete [] dynBuffer;
  1843. dynBuffer = NULL;
  1844. }
  1845. g_fpMsiCloseHandle(hRecord);
  1846. g_fpMsiViewClose(hView);
  1847. g_fpMsiCloseHandle(hView);
  1848. msidata.CloseDatabase ();
  1849. if(m_pObj)
  1850. {
  1851. m_pObj->Release();
  1852. m_pObj = NULL;
  1853. }
  1854. throw;
  1855. }
  1856. g_fpMsiCloseHandle(hRecord);
  1857. g_fpMsiViewClose(hView);
  1858. g_fpMsiCloseHandle(hView);
  1859. msidata.CloseDatabase ();
  1860. }
  1861. }
  1862. if ( dynBuffer )
  1863. {
  1864. delete [] dynBuffer;
  1865. dynBuffer = NULL;
  1866. }
  1867. return hr;
  1868. }
  1869. HRESULT CActionCheck::CreateFolderDirectory(IWbemObjectSink *pHandler, ACTIONTYPE atAction,
  1870. CRequestObject *pActionData, CRequestObject *pCheckData)
  1871. {
  1872. HRESULT hr = WBEM_S_NO_ERROR;
  1873. MSIHANDLE hView = NULL;
  1874. MSIHANDLE hRecord = NULL;
  1875. MSIHANDLE hDView = NULL;
  1876. MSIHANDLE hDRecord = NULL;
  1877. int i = -1;
  1878. WCHAR wcBuf[BUFF_SIZE];
  1879. WCHAR wcDir[BUFF_SIZE];
  1880. WCHAR wcProductCode[39];
  1881. WCHAR wcCompID[39];
  1882. WCHAR wcTestCode[39];
  1883. WCHAR wcFolder[BUFF_SIZE];
  1884. DWORD dwBufSize;
  1885. bool bMatch = false;
  1886. bool bTestCode = false;
  1887. UINT uiStatus;
  1888. bool bGotCheck = false;
  1889. bool bGotAction = false;
  1890. CStringExt wcProp;
  1891. if(atAction != ACTIONTYPE_ENUM)
  1892. {
  1893. // we are doing GetObject so we need to be reinitialized
  1894. hr = WBEM_E_NOT_FOUND;
  1895. CHeap_Exception he(CHeap_Exception::E_ALLOCATION_ERROR);
  1896. if(pActionData){
  1897. for(int i = 0; i < pActionData->m_iPropCount; i++){
  1898. if(_wcsicmp(pActionData->m_Property[i], L"ActionID") == 0)
  1899. {
  1900. if ( ::SysStringLen ( pActionData->m_Value[i] ) < BUFF_SIZE )
  1901. {
  1902. //Get the action we're looking for
  1903. wcscpy(wcBuf, pActionData->m_Value[i]);
  1904. // safe operation if wcslen ( wcBuf ) > 38
  1905. if ( wcslen ( wcBuf ) > 38 )
  1906. {
  1907. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1908. }
  1909. else
  1910. {
  1911. // we are not good to go, they have sent us longer string
  1912. throw hr;
  1913. }
  1914. // safe because lenght has been tested already in condition
  1915. RemoveFinalGUID(pActionData->m_Value[i], wcFolder);
  1916. bGotAction = true;
  1917. bTestCode = true;
  1918. break;
  1919. }
  1920. else
  1921. {
  1922. // we are not good to go, they have sent us longer string
  1923. throw hr;
  1924. }
  1925. }
  1926. }
  1927. }
  1928. if(pCheckData){
  1929. for(int i = 0; i < pCheckData->m_iPropCount; i++){
  1930. if(_wcsicmp(pCheckData->m_Property[i], L"CheckID") == 0)
  1931. {
  1932. if ( ::SysStringLen ( pCheckData->m_Value[i] ) < BUFF_SIZE )
  1933. {
  1934. //Get the action we're looking for
  1935. wcscpy(wcBuf, pCheckData->m_Value[i]);
  1936. // safe operation if wcslen ( wcBuf ) > 38
  1937. if ( wcslen ( wcBuf ) > 38 )
  1938. {
  1939. wcscpy(wcTestCode, &(wcBuf[(wcslen(wcBuf) - 38)]));
  1940. }
  1941. else
  1942. {
  1943. // we are not good to go, they have sent us longer string
  1944. throw hr;
  1945. }
  1946. // safe because lenght has been tested already in condition
  1947. RemoveFinalGUID(pCheckData->m_Value[i], wcFolder);
  1948. //we have a componentized directory... do a little more work
  1949. if ((wcFolder[wcslen(wcFolder) - 1] == L'}') &&
  1950. (wcFolder[wcslen(wcFolder) - 38] == L'{')
  1951. )
  1952. {
  1953. RemoveFinalGUID(wcFolder, wcFolder);
  1954. }
  1955. bTestCode = true;
  1956. bGotCheck = true;
  1957. break;
  1958. }
  1959. else
  1960. {
  1961. // we are not good to go, they have sent us longer string
  1962. throw hr;
  1963. }
  1964. }
  1965. }
  1966. }
  1967. }
  1968. //These will change from class to class
  1969. bool bDriver, bAttribute, bDoneFirst, bValidated;
  1970. Query wcQuery;
  1971. wcQuery.Append ( 1, L"select distinct `Directory_`, `Component_` from CreateFolder" );
  1972. //optimize for GetObject
  1973. if ( bGotCheck || bGotAction )
  1974. {
  1975. wcQuery.Append ( 3, L" where `Directory_`=\'", wcFolder, L"\'" );
  1976. }
  1977. QueryExt wcQuery1 ( L"select distinct `ComponentId` from Component where `Directory_`=\'" );
  1978. LPWSTR Buffer = NULL;
  1979. LPWSTR dynBuffer = NULL;
  1980. DWORD dwDynBuffer = 0L;
  1981. LPWSTR Buffer1 = NULL;
  1982. LPWSTR dynBuffer1 = NULL;
  1983. DWORD dwDynBuffer1 = 0L;
  1984. while(!bMatch && m_pRequest->Package(++i) && (hr != WBEM_E_CALL_CANCELLED))
  1985. {
  1986. // safe operation:
  1987. // Package ( i ) returns NULL ( tested above ) or valid WCHAR [39]
  1988. wcscpy(wcProductCode, m_pRequest->Package(i));
  1989. if((atAction == ACTIONTYPE_ENUM) ||
  1990. (bTestCode && (_wcsicmp(wcTestCode, wcProductCode) == 0))){
  1991. //Open our database
  1992. try
  1993. {
  1994. if ( GetView ( &hView, wcProductCode, wcQuery, L"CreateFolder", TRUE, FALSE ) )
  1995. {
  1996. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  1997. while(!bMatch && (uiStatus != ERROR_NO_MORE_ITEMS) && (hr != WBEM_E_CALL_CANCELLED)){
  1998. CheckMSI(uiStatus);
  1999. bDoneFirst = false;
  2000. dwBufSize = BUFF_SIZE;
  2001. GetBufferToPut ( hRecord, 1, dwBufSize, wcDir, dwDynBuffer1, dynBuffer1, Buffer1 );
  2002. // make query on fly
  2003. wcQuery1.Append ( 2, Buffer1, L"\'" );
  2004. if(((uiStatus = g_fpMsiDatabaseOpenViewW(msidata.GetDatabase(), wcQuery1, &hDView)) == ERROR_SUCCESS)
  2005. || !bDoneFirst){
  2006. if((g_fpMsiViewExecute(hDView, 0) == ERROR_SUCCESS) || !bDoneFirst){
  2007. try{
  2008. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  2009. while(!bMatch && (!bDoneFirst || (uiStatus == ERROR_SUCCESS)) && (hr != WBEM_E_CALL_CANCELLED)){
  2010. bValidated = false;
  2011. if(uiStatus == ERROR_SUCCESS){
  2012. dwBufSize = 39;
  2013. CheckMSI(g_fpMsiRecordGetStringW(hDRecord, 1, wcCompID, &dwBufSize));
  2014. bValidated = ValidateComponentID(wcCompID, wcProductCode);
  2015. }
  2016. if(((uiStatus != ERROR_SUCCESS) && !bDoneFirst) || (bValidated && (uiStatus != ERROR_NO_MORE_ITEMS))){
  2017. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  2018. //----------------------------------------------------
  2019. if ( Buffer1 && Buffer1 [ 0 ] != 0 )
  2020. {
  2021. // safe operation
  2022. wcProp.Copy(L"Win32_CreateFolderAction.ActionID=\"");
  2023. wcProp.Append (3, Buffer1, wcProductCode, L"\"");
  2024. PutKeyProperty(m_pObj, pAction, wcProp, &bDriver, m_pRequest);
  2025. // safe operation
  2026. wcProp.Copy(L"Win32_DirectorySpecification.CheckID=\"");
  2027. if(uiStatus == ERROR_SUCCESS)
  2028. {
  2029. wcProp.Append (4, Buffer1, wcCompID, wcProductCode, L"\"");
  2030. }
  2031. else
  2032. {
  2033. wcProp.Append (3, Buffer1, wcProductCode, L"\"");
  2034. }
  2035. PutKeyProperty(m_pObj, pCheck, wcProp, &bAttribute, m_pRequest);
  2036. dwBufSize = BUFF_SIZE;
  2037. GetBufferToPut ( hRecord, 2, dwBufSize, wcBuf, dwDynBuffer, dynBuffer, Buffer );
  2038. if ( ValidateComponentName ( msidata.GetDatabase(), wcProductCode, Buffer ) )
  2039. {
  2040. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  2041. {
  2042. dynBuffer [ 0 ] = 0;
  2043. }
  2044. if(bDriver && bAttribute) bMatch = true;
  2045. if((atAction != ACTIONTYPE_GET) || bMatch){
  2046. hr = pHandler->Indicate(1, &m_pObj);
  2047. }
  2048. }
  2049. else
  2050. {
  2051. if ( dynBuffer && dynBuffer [ 0 ] != 0 )
  2052. {
  2053. dynBuffer [ 0 ] = 0;
  2054. }
  2055. }
  2056. }
  2057. m_pObj->Release();
  2058. m_pObj = NULL;
  2059. if(!bDoneFirst) bDoneFirst = true;
  2060. }
  2061. g_fpMsiCloseHandle(hDRecord);
  2062. uiStatus = g_fpMsiViewFetch(hDView, &hDRecord);
  2063. }
  2064. }catch(...){
  2065. g_fpMsiCloseHandle(hDRecord);
  2066. g_fpMsiViewClose(hDView);
  2067. g_fpMsiCloseHandle(hDView);
  2068. throw;
  2069. }
  2070. g_fpMsiCloseHandle(hDRecord);
  2071. g_fpMsiViewClose(hDView);
  2072. g_fpMsiCloseHandle(hDView);
  2073. }
  2074. }
  2075. g_fpMsiCloseHandle(hRecord);
  2076. uiStatus = g_fpMsiViewFetch(hView, &hRecord);
  2077. if ( dynBuffer1 && dynBuffer1 [ 0 ] != 0 )
  2078. {
  2079. dynBuffer1 [ 0 ] = 0;
  2080. }
  2081. }
  2082. }
  2083. }
  2084. catch(...)
  2085. {
  2086. if ( dynBuffer )
  2087. {
  2088. delete [] dynBuffer;
  2089. dynBuffer = NULL;
  2090. }
  2091. if ( dynBuffer1 )
  2092. {
  2093. delete [] dynBuffer1;
  2094. dynBuffer1 = NULL;
  2095. }
  2096. g_fpMsiCloseHandle(hRecord);
  2097. g_fpMsiViewClose(hView);
  2098. g_fpMsiCloseHandle(hView);
  2099. msidata.CloseDatabase ();
  2100. if(m_pObj)
  2101. {
  2102. m_pObj->Release();
  2103. m_pObj = NULL;
  2104. }
  2105. throw;
  2106. }
  2107. g_fpMsiCloseHandle(hRecord);
  2108. g_fpMsiViewClose(hView);
  2109. g_fpMsiCloseHandle(hView);
  2110. msidata.CloseDatabase ();
  2111. }
  2112. }
  2113. if ( dynBuffer )
  2114. {
  2115. delete [] dynBuffer;
  2116. dynBuffer = NULL;
  2117. }
  2118. if ( dynBuffer1 )
  2119. {
  2120. delete [] dynBuffer1;
  2121. dynBuffer1 = NULL;
  2122. }
  2123. return hr;
  2124. }