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.

596 lines
15 KiB

  1. // RequestObject.cpp: implementation of the CRequestObject class.
  2. // Copyright (c)1997-1999 Microsoft Corporation
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #include "precomp.h"
  6. #include "requestobject.h"
  7. #include <stdio.h>
  8. //Classes
  9. #include "poddata.h"
  10. #include "podbase.h"
  11. //Associations
  12. CHeap_Exception CRequestObject::m_he(CHeap_Exception::E_ALLOCATION_ERROR);
  13. //////////////////////////////////////////////////////////////////////
  14. // Construction/Destruction
  15. //////////////////////////////////////////////////////////////////////
  16. CRequestObject::CRequestObject()
  17. {
  18. m_cRef = 0;
  19. m_bstrPath = NULL;
  20. m_bstrClass = NULL;
  21. }
  22. CRequestObject::~CRequestObject()
  23. {
  24. }
  25. //***************************************************************************
  26. //
  27. // CRequestObject::QueryInterface
  28. // CRequestObject::AddRef
  29. // CRequestObject::Release
  30. //
  31. // Purpose: IUnknown members for CRequestObject object.
  32. //***************************************************************************
  33. /*
  34. STDMETHODIMP CRequestObject::QueryInterface(REFIID riid, PPVOID ppv)
  35. {
  36. return E_NOINTERFACE;
  37. }
  38. STDMETHODIMP_(ULONG) CRequestObject::AddRef(void)
  39. {
  40. InterlockedIncrement((long *)&m_cRef);
  41. return m_cRef;
  42. }
  43. STDMETHODIMP_(ULONG) CRequestObject::Release(void)
  44. {
  45. ULONG nNewCount = InterlockedDecrement((long *)&m_cRef);
  46. // if(0L == nNewCount) delete this;
  47. return nNewCount;
  48. }
  49. */
  50. void CRequestObject::Initialize(IWbemServices *pNamespace)
  51. {
  52. m_pNamespace = pNamespace;
  53. m_pHandler = NULL;
  54. m_bstrClass = NULL;
  55. m_bstrPath = NULL;
  56. m_iPropCount = m_iValCount = 0;
  57. for(int i = 0; i < POD_KEY_LIST_SIZE; i++) m_Property[i] = m_Value[i] = NULL;
  58. }
  59. HRESULT CRequestObject::CreateObject(BSTR bstrPath, IWbemObjectSink *pHandler, IWbemContext *pCtx)
  60. {
  61. HRESULT hr = WBEM_S_NO_ERROR;
  62. CGenericClass *pClass = NULL;
  63. m_bstrPath = SysAllocString(bstrPath);
  64. if(!m_bstrPath)
  65. throw m_he;
  66. if(ParsePath(bstrPath)){
  67. try{
  68. //create the appropriate class
  69. if(SUCCEEDED(hr = CreateClass(&pClass, pCtx))){
  70. if(!pClass)
  71. throw m_he;
  72. //get the requested object
  73. hr = pClass->CreateObject(pHandler, ACTIONTYPE_GET);
  74. }
  75. }catch(...){
  76. if(pClass){
  77. pClass->CleanUp();
  78. delete pClass;
  79. }
  80. throw;
  81. }
  82. if(pClass){
  83. pClass->CleanUp();
  84. delete pClass;
  85. }
  86. }else hr = WBEM_E_FAILED;
  87. return hr;
  88. }
  89. HRESULT CRequestObject::CreateClass(CGenericClass **pClass, IWbemContext *pCtx)
  90. {
  91. HRESULT hr = WBEM_S_NO_ERROR;
  92. //Create the appropriate class
  93. /////////////
  94. // Classes //
  95. /////////////
  96. if(0 == _wcsicmp(m_bstrClass, L"Sample_DataClass")){
  97. *pClass = new CPodData(this, m_pNamespace, pCtx);
  98. } else if(0 == _wcsicmp(m_bstrClass, L"Sample_BaseClass")){
  99. *pClass = new CPodBase(this, m_pNamespace, pCtx);
  100. //////////////////
  101. // Associations //
  102. //////////////////
  103. }else return WBEM_E_NOT_FOUND;
  104. if(!(*pClass)) throw m_he;
  105. return hr;
  106. };
  107. HRESULT CRequestObject::CreateObjectEnum(BSTR bstrPath, IWbemObjectSink *pHandler, IWbemContext *pCtx)
  108. {
  109. HRESULT hr = WBEM_S_NO_ERROR;
  110. CGenericClass *pClass = NULL;
  111. m_bstrPath = SysAllocString(bstrPath);
  112. if(!m_bstrPath) throw m_he;
  113. if(ParsePath(bstrPath)){
  114. try{
  115. //Create the appropriate class
  116. if(SUCCEEDED(hr = CreateClass(&pClass, pCtx))){
  117. if(!pClass) throw m_he;
  118. //Enumerate the objects
  119. hr = pClass->CreateObject(pHandler, ACTIONTYPE_ENUM);
  120. }
  121. }catch(...){
  122. if(pClass){
  123. pClass->CleanUp();
  124. delete pClass;
  125. }
  126. throw;
  127. }
  128. if(pClass){
  129. pClass->CleanUp();
  130. delete pClass;
  131. }
  132. }else hr = WBEM_E_FAILED;
  133. return hr;
  134. }
  135. HRESULT CRequestObject::PutObject(IWbemClassObject *pInst, IWbemObjectSink *pHandler, IWbemContext *pCtx)
  136. {
  137. HRESULT hr = WBEM_S_NO_ERROR;
  138. CGenericClass *pClass = NULL;
  139. VARIANT v;
  140. VariantInit(&v);
  141. if(SUCCEEDED(pInst->Get(L"__RELPATH", 0, &v, NULL, NULL))){
  142. m_bstrPath = SysAllocString(V_BSTR(&v));
  143. if(!m_bstrPath) throw m_he;
  144. if(!ParsePath(V_BSTR(&v))) hr = WBEM_E_FAILED;
  145. }else hr = WBEM_E_FAILED;
  146. if(SUCCEEDED(pInst->Get(L"__CLASS", 0, &v, NULL, NULL))){
  147. try{
  148. m_bstrClass = SysAllocString(V_BSTR(&v));
  149. if(!m_bstrClass) throw m_he;
  150. //Create the appropriate class
  151. if(SUCCEEDED(hr = CreateClass(&pClass, pCtx))){
  152. if(!pClass) throw m_he;
  153. //Enumerate the objects
  154. hr = pClass->PutInst(pInst, pHandler, pCtx);
  155. }
  156. }catch(...){
  157. if(pClass){
  158. pClass->CleanUp();
  159. delete pClass;
  160. }
  161. throw;
  162. }
  163. if(pClass){
  164. pClass->CleanUp();
  165. delete pClass;
  166. }
  167. }else hr = WBEM_E_FAILED;
  168. VariantClear(&v);
  169. return hr;
  170. }
  171. HRESULT CRequestObject::ExecMethod(BSTR bstrPath, BSTR bstrMethod, IWbemClassObject *pInParams,
  172. IWbemObjectSink *pHandler, IWbemContext *pCtx)
  173. {
  174. HRESULT hr = WBEM_S_NO_ERROR;
  175. CGenericClass *pClass = NULL;
  176. m_bstrPath = SysAllocString(bstrPath);
  177. if(!m_bstrPath) throw m_he;
  178. if(ParsePath(bstrPath)){
  179. try{
  180. //Create the appropriate class
  181. if(SUCCEEDED(hr = CreateClass(&pClass, pCtx))){
  182. if(!pClass) throw m_he;
  183. //execute the method
  184. hr = pClass->ExecMethod(bstrMethod, IsInstance(), pInParams, pHandler, pCtx);
  185. }
  186. }catch(...){
  187. if(pClass){
  188. pClass->CleanUp();
  189. delete pClass;
  190. }
  191. throw;
  192. }
  193. if(pClass){
  194. pClass->CleanUp();
  195. delete pClass;
  196. }
  197. }else
  198. return WBEM_E_FAILED;
  199. return hr;
  200. }
  201. HRESULT CRequestObject::DeleteObject(BSTR bstrPath, IWbemObjectSink *pHandler, IWbemContext *pCtx)
  202. {
  203. HRESULT hr = WBEM_S_NO_ERROR;
  204. CGenericClass *pClass = NULL;
  205. m_bstrPath = SysAllocString(bstrPath);
  206. if(!m_bstrPath) throw m_he;
  207. if(ParsePath(bstrPath)){
  208. try{
  209. //Create the appropriate class
  210. if(SUCCEEDED(hr = CreateClass(&pClass, pCtx))){
  211. if(!pClass) throw m_he;
  212. //delete the object
  213. hr = pClass->CreateObject(pHandler, ACTIONTYPE_DELETE);
  214. }
  215. }catch(...){
  216. if(pClass){
  217. pClass->CleanUp();
  218. delete pClass;
  219. }
  220. throw;
  221. }
  222. if(pClass){
  223. pClass->CleanUp();
  224. delete pClass;
  225. }
  226. }else hr = WBEM_E_FAILED;
  227. return hr;
  228. }
  229. bool CRequestObject::ParsePath(BSTR bstrPath)
  230. {
  231. if(wcslen(bstrPath) < 1) return false;
  232. WCHAR wcTest[(BUFF_SIZE) + 1];
  233. wcscpy(wcTest, bstrPath);
  234. WCHAR *pwcTest = NULL;
  235. WCHAR *pwcClassStart = wcTest;
  236. WCHAR *pwcNamespace = NULL;
  237. WCHAR *pwcStart = NULL;
  238. WCHAR *pwcStrip = NULL;
  239. WCHAR wcPrevious = NULL;
  240. int iNumQuotes = 0;
  241. bool bClass = false;
  242. bool bDoubles = false;
  243. //Main Parsing Loop
  244. for(pwcTest = wcTest; *pwcTest; pwcTest++){
  245. if((*pwcTest == L'\\') && !bClass){
  246. for(pwcNamespace = pwcTest; *pwcNamespace != L':'; pwcNamespace++){}
  247. pwcClassStart = pwcNamespace + 1;
  248. pwcTest = pwcNamespace;
  249. }else if(*pwcTest == L'.'){
  250. if(iNumQuotes == 0){
  251. // issolate the class name.
  252. *pwcTest = NULL;
  253. if(m_bstrClass){
  254. SysFreeString(m_bstrClass);
  255. m_bstrClass = NULL;
  256. }
  257. m_bstrClass = SysAllocString(pwcClassStart);
  258. if(!m_bstrClass) throw m_he;
  259. bClass = true;
  260. pwcStart = (pwcTest + 1);
  261. }
  262. }else if(*pwcTest == L'='){
  263. if(iNumQuotes == 0){
  264. if(!bClass){
  265. // issolate the class name.
  266. *pwcTest = NULL;
  267. if(m_bstrClass){
  268. SysFreeString(m_bstrClass);
  269. m_bstrClass = NULL;
  270. }
  271. m_bstrClass = SysAllocString(pwcClassStart);
  272. if(!m_bstrClass) throw m_he;
  273. bClass = true;
  274. pwcStart = (pwcTest + 1);
  275. }else{
  276. // issolate the property name.
  277. *pwcTest = NULL;
  278. if(pwcStart != NULL){
  279. m_Property[m_iPropCount] = SysAllocString(pwcStart);
  280. if(!m_Property[m_iPropCount++]) throw m_he;
  281. pwcStart = (pwcTest + 1);
  282. }else pwcStart = (pwcTest + 1);
  283. }
  284. }
  285. }else if(*pwcTest == L','){
  286. if(iNumQuotes != 1){
  287. // issolate the property value.
  288. *pwcTest = NULL;
  289. if(pwcStart != NULL){
  290. m_Value[m_iValCount] = SysAllocString(pwcStart);
  291. if(!m_Value[m_iValCount++]) throw m_he;
  292. pwcStart = (pwcTest + 1);
  293. }else return false;
  294. }
  295. }else if(*pwcTest == L'\"'){
  296. if(wcPrevious != L'\\'){
  297. // deal with quotes in path.
  298. iNumQuotes++;
  299. if(iNumQuotes == 1) pwcStart = (pwcTest + 1);
  300. else if(iNumQuotes == 2){
  301. *pwcTest = NULL;
  302. iNumQuotes = 0;
  303. }
  304. }else if(iNumQuotes == 1){
  305. //deal with embedded quotes
  306. for(pwcStrip = (--pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  307. *pwcStrip = NULL;
  308. }
  309. }else if((*pwcTest == L'\\') && (wcPrevious == L'\\') && bClass && !bDoubles){
  310. for(pwcStrip = (--pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  311. *pwcStrip = NULL;
  312. }
  313. #ifdef _STRIP_ESCAPED_CHARS
  314. else if(*pwcTest == L'%'){
  315. //deal with escaped URL characters
  316. if(*(pwcTest + 1) == L'0'){
  317. if(*(pwcTest + 2) == L'7'){
  318. //bell
  319. *pwcTest = L'\\';
  320. *(++pwcTest) = L'a';
  321. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  322. *pwcStrip = NULL;
  323. }else if(*(pwcTest + 2) == L'8'){
  324. //backspace
  325. *pwcTest = L'\\';
  326. *(++pwcTest) = L'b';
  327. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  328. *pwcStrip = NULL;
  329. }else if(*(pwcTest + 2) == L'9'){
  330. //horizontal tab
  331. *pwcTest = L'\\';
  332. *(++pwcTest) = L't';
  333. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  334. *pwcStrip = NULL;
  335. }else if((*(pwcTest + 2) == L'A') || (*(pwcTest + 2) == L'a')){
  336. //newline
  337. *pwcTest = L'\\';
  338. *(++pwcTest) = L'n';
  339. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  340. *pwcStrip = NULL;
  341. }else if((*(pwcTest + 2) == L'B') || (*(pwcTest + 2) == L'b')){
  342. //vertical tab
  343. *pwcTest = L'\\';
  344. *(++pwcTest) = L'v';
  345. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  346. *pwcStrip = NULL;
  347. }else if((*(pwcTest + 2) == L'C') || (*(pwcTest + 2) == L'c')){
  348. //formfeed
  349. *pwcTest = L'\\';
  350. *(++pwcTest) = L'f';
  351. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  352. *pwcStrip = NULL;
  353. }else if((*(pwcTest + 2) == L'D') || (*(pwcTest + 2) == L'd')){
  354. //carriage return
  355. *pwcTest = L'\\';
  356. *(++pwcTest) = L'r';
  357. for(pwcStrip = (++pwcTest); *pwcStrip; pwcStrip++) *pwcStrip = *(pwcStrip + 1);
  358. *pwcStrip = NULL;
  359. }else return false;
  360. }else if(*(pwcTest + 1) == L'1'){
  361. return false;
  362. }else if(*(pwcTest + 1) == L'2'){
  363. if(*(pwcTest + 2) == L'0'){
  364. //space
  365. *pwcTest++ = L' ';
  366. for(int ip = 0; ip < 2; ip++)
  367. for(pwcStrip = (pwcTest); *pwcStrip; pwcStrip++)
  368. *pwcStrip = *(pwcStrip + 1);
  369. *pwcStrip = NULL;
  370. }else return false;
  371. }
  372. }
  373. #endif //_STRIP_ESCAPED_CHARS
  374. if((wcPrevious == *pwcTest) && !bDoubles) bDoubles = true;
  375. else bDoubles = false;
  376. wcPrevious = *pwcTest;
  377. }
  378. // if we still have values to add, do so now
  379. if(pwcStart != NULL){
  380. m_Value[m_iValCount] = SysAllocString(pwcStart);
  381. if(!m_Value[m_iValCount++]) throw m_he;
  382. }else if((m_iPropCount < 1) && (m_iValCount < 1)){
  383. if(m_bstrClass){
  384. SysFreeString(m_bstrClass);
  385. m_bstrClass = NULL;
  386. }
  387. m_bstrClass = SysAllocString(pwcClassStart);
  388. if(!m_bstrClass) throw m_he;
  389. }
  390. if(iNumQuotes != 0) return false;
  391. if(m_iValCount != m_iPropCount){
  392. if(m_iValCount > m_iPropCount){ if(m_iValCount != 1) return false; }
  393. else return false;
  394. }
  395. if(!m_bstrClass) return false;
  396. return true;
  397. }
  398. bool CRequestObject::Cleanup()
  399. {
  400. //Let's destroy our list and clear up some space
  401. if(m_bstrClass != NULL) SysFreeString(m_bstrClass);
  402. if(m_bstrPath != NULL) SysFreeString(m_bstrPath);
  403. for(int i = 0; i < POD_KEY_LIST_SIZE; i++){
  404. if(m_Property[i] != NULL) SysFreeString(m_Property[i]);
  405. if(m_Value[i] != NULL) SysFreeString(m_Value[i]);
  406. }
  407. return true;
  408. }
  409. bool CRequestObject::IsInstance()
  410. {
  411. if((m_iPropCount > 0) || (m_iValCount > 0)) return true;
  412. return false;
  413. }
  414. //Properties
  415. /////////////////////
  416. const WCHAR *pSceStorePath = L"SceStorePath";
  417. const WCHAR *pLogFilePath = L"LogFilePath";
  418. const WCHAR *pLogFileRecord = L"LogFileRecord";
  419. const WCHAR *pLogArea = L"LogArea";
  420. const WCHAR *pLogErrorCode = L"LogErrorCode";
  421. const WCHAR *pKeyName = L"KeyName";
  422. const WCHAR *pKey = L"Key";
  423. const WCHAR *pValue = L"Value";
  424. const WCHAR *pPodID = L"PodID";
  425. const WCHAR *pPodSection = L"PodSection";
  426. const WCHAR *szPodGUID = L"{c5f6cc21_6195_4555_b9d8_3ef327763cae}";