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.

387 lines
10 KiB

  1. // poddata.cpp, implementation of CPodData class
  2. // Copyright (c)1997-1999 Microsoft Corporation
  3. //
  4. //////////////////////////////////////////////////////////////////////
  5. #include "precomp.h"
  6. #include "poddata.h"
  7. #include <io.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CPodData::CPodData(CRequestObject *pObj, IWbemServices *pNamespace,
  12. IWbemContext *pCtx):CGenericClass(pObj, pNamespace, pCtx)
  13. {
  14. }
  15. CPodData::~CPodData()
  16. {
  17. }
  18. //////////////////////////////////////////////////////////////////////
  19. // CPodData::CreateObject
  20. //
  21. // Create one or more instances for the requested Sample_DataClass class
  22. //////////////////////////////////////////////////////////////////////
  23. HRESULT CPodData::CreateObject(IWbemObjectSink *pHandler, ACTIONTYPE atAction)
  24. {
  25. HRESULT hr = WBEM_S_NO_ERROR;
  26. if ( ACTIONTYPE_ENUM == atAction ) {
  27. //
  28. // do not support enumeration because we don't know the scope of the request
  29. //
  30. hr = WBEM_E_NOT_SUPPORTED;
  31. } else if ( ACTIONTYPE_GET == atAction ||
  32. ACTIONTYPE_DELETE == atAction ) {
  33. // Check the number of keys
  34. // ========================
  35. hr = WBEM_E_INVALID_OBJECT_PATH;
  36. // Check prop name
  37. // ==============
  38. int idxStorePath=-1, idxKey=-1;
  39. for ( int i=0; i<m_pRequest->m_iPropCount; i++ ) {
  40. //
  41. // search for StorePath value
  42. //
  43. if(m_pRequest->m_Property[i] != NULL &&
  44. m_pRequest->m_Value[i] != NULL &&
  45. _wcsicmp(m_pRequest->m_Property[i], pSceStorePath) == 0 ) {
  46. idxStorePath = i;
  47. continue;
  48. }
  49. if(m_pRequest->m_Property[i] != NULL &&
  50. m_pRequest->m_Value[i] != NULL &&
  51. _wcsicmp(m_pRequest->m_Property[i], pKeyName) == 0 ) {
  52. idxKey = i;
  53. continue;
  54. }
  55. if(idxStorePath >= 0 && idxKey >= 0 )
  56. break;
  57. }
  58. if(idxStorePath >= 0 && idxKey >= 0 ) {
  59. // Create the Pod instance
  60. //============================
  61. BSTR bstrPath=NULL;
  62. hr = CheckAndExpandPath(m_pRequest->m_Value[idxStorePath], &bstrPath);
  63. if ( SUCCEEDED(hr) ) {
  64. DWORD dwAttrib = GetFileAttributes(bstrPath);
  65. if ( dwAttrib != -1 ) {
  66. hr = ConstructInstance(pHandler, atAction, bstrPath, m_pRequest->m_Value[idxKey]);
  67. } else {
  68. hr = WBEM_E_NOT_FOUND;
  69. }
  70. }
  71. if ( bstrPath ) SysFreeString(bstrPath);
  72. }
  73. } else {
  74. //
  75. // not supported for now
  76. //
  77. hr = WBEM_E_NOT_SUPPORTED;
  78. }
  79. return hr;
  80. }
  81. //////////////////////////////////////////////////////////////////////
  82. // CPodData::PutInst
  83. //
  84. // Save an instance to the store
  85. //////////////////////////////////////////////////////////////////////
  86. HRESULT CPodData::PutInst(IWbemClassObject *pInst,
  87. IWbemObjectSink *pHandler,
  88. IWbemContext *pCtx)
  89. {
  90. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  91. BSTR bstrStorePath=NULL;
  92. BSTR bstrKey=NULL;
  93. BSTR bstrValue=NULL;
  94. BSTR bstrPath=NULL;
  95. try{
  96. hr = GetProperty(pInst, pSceStorePath, &bstrStorePath);
  97. if ( FAILED(hr) ) throw hr;
  98. hr = CheckAndExpandPath(bstrStorePath, &bstrPath);
  99. if ( FAILED(hr) ) throw hr;
  100. // if the property doesn't exist (NULL or empty), WBEM_S_RESET_TO_DEFAULT is returned
  101. hr = GetProperty(pInst, pKeyName, &bstrKey);
  102. if ( FAILED(hr) ) throw hr;
  103. hr = GetProperty(pInst, pValue, &bstrValue);
  104. if ( FAILED(hr) ) throw hr;
  105. //
  106. // now save the info to file
  107. //
  108. hr = SaveSettingsToStore((PCWSTR)bstrPath,
  109. (PWSTR)bstrKey,
  110. (PWSTR)bstrValue
  111. );
  112. }catch(...){
  113. // Clean up
  114. // ========
  115. if ( bstrStorePath )
  116. SysFreeString(bstrStorePath);
  117. if ( bstrPath )
  118. SysFreeString(bstrPath);
  119. if ( bstrKey )
  120. SysFreeString(bstrKey);
  121. if ( bstrValue )
  122. SysFreeString(bstrValue);
  123. throw;
  124. }
  125. if ( bstrStorePath )
  126. SysFreeString(bstrStorePath);
  127. if ( bstrPath )
  128. SysFreeString(bstrPath);
  129. if ( bstrKey )
  130. SysFreeString(bstrKey);
  131. if ( bstrValue )
  132. SysFreeString(bstrValue);
  133. return hr;
  134. }
  135. //////////////////////////////////////////////////////////////////////
  136. // CPodData::ConstructInstance
  137. //
  138. // construction of one password instance
  139. //////////////////////////////////////////////////////////////////////
  140. HRESULT CPodData::ConstructInstance(IWbemObjectSink *pHandler,
  141. ACTIONTYPE atAction,
  142. LPCWSTR wszStoreName,
  143. LPWSTR KeyName
  144. )
  145. {
  146. HRESULT hr=WBEM_S_NO_ERROR;
  147. bool bName=FALSE;
  148. BSTR bstrValue=NULL;
  149. //
  150. // build object path
  151. //
  152. WCHAR *pPath1=TEXT("Sce_PodData.SceStorePath=\"");
  153. WCHAR *pPath2=TEXT("\",PodID=\"");
  154. WCHAR *pPath3=TEXT("\",PodSection=\"1\",Key=\"");
  155. DWORD Len=wcslen(pPath1)+wcslen(wszStoreName)+wcslen(pPath2)+wcslen(szPodGUID)+wcslen(pPath3)+wcslen(KeyName)+2;
  156. PWSTR tmp=(PWSTR)LocalAlloc(LPTR, Len*sizeof(WCHAR));
  157. if ( !tmp ) return WBEM_E_OUT_OF_MEMORY;
  158. wcscpy(tmp, pPath1);
  159. wcscat(tmp, wszStoreName);
  160. wcscat(tmp, pPath2);
  161. wcscat(tmp, szPodGUID);
  162. wcscat(tmp, pPath3);
  163. wcscat(tmp, KeyName);
  164. wcscat(tmp, L"\"");
  165. BSTR bstrObjectPath=SysAllocString(tmp);
  166. LocalFree(tmp);
  167. if ( !bstrObjectPath ) return WBEM_E_OUT_OF_MEMORY;
  168. IWbemClassObject *pPodObj=NULL;
  169. if ( ACTIONTYPE_DELETE == atAction ) {
  170. hr = m_pNamespace->DeleteInstance(bstrObjectPath, 0, m_pCtx, NULL);
  171. } else {
  172. try{
  173. //
  174. // get the data
  175. //
  176. hr = m_pNamespace->GetObject(bstrObjectPath, 0, m_pCtx, &pPodObj, NULL);
  177. if ( SUCCEEDED(hr) ) {
  178. // get the value property
  179. hr = GetProperty(pPodObj, pValue, &bstrValue);
  180. }
  181. if ( pPodObj ) {
  182. pPodObj->Release();
  183. pPodObj = NULL;
  184. }
  185. if ( FAILED(hr) ) throw hr;
  186. if ( !bstrValue ) throw WBEM_E_NOT_FOUND;
  187. if(FAILED(hr = SpawnAnInstance(&m_pObj))) throw hr;
  188. //----------------------------------------------------
  189. hr = PutKeyProperty(m_pObj, pSceStorePath, (PWSTR)wszStoreName, &bName, m_pRequest);
  190. if ( SUCCEEDED(hr) )
  191. hr = PutKeyProperty(m_pObj, pKeyName, KeyName, &bName, m_pRequest);
  192. if ( SUCCEEDED(hr) )
  193. hr = PutProperty(m_pObj, pValue, bstrValue);
  194. if ( SUCCEEDED(hr) ) {
  195. hr = pHandler->Indicate(1, &m_pObj);
  196. }
  197. m_pObj->Release();
  198. m_pObj = NULL;
  199. }catch(...){
  200. if(m_pObj){
  201. m_pObj->Release();
  202. m_pObj = NULL;
  203. }
  204. // Clean up
  205. // ========
  206. SysFreeString(bstrObjectPath);
  207. if ( pPodObj ) {
  208. pPodObj->Release();
  209. }
  210. throw;
  211. }
  212. }
  213. SysFreeString(bstrObjectPath);
  214. return hr;
  215. }
  216. //////////////////////////////////////////////////////////////////////
  217. // CPodData::SaveSettingsToStore
  218. //
  219. // set/reset the setting for this POD
  220. //////////////////////////////////////////////////////////////////////
  221. HRESULT CPodData::SaveSettingsToStore(PCWSTR wszStoreName,
  222. PWSTR KeyName, PWSTR szValue)
  223. {
  224. HRESULT hr=WBEM_S_NO_ERROR;
  225. //
  226. // get the PodData class
  227. //
  228. BSTR bstrClass=SysAllocString(L"Sce_PodData");
  229. if ( !bstrClass ) hr = WBEM_E_OUT_OF_MEMORY;
  230. IWbemClassObject *pClass=NULL;
  231. IWbemClassObject *pObj=NULL;
  232. bool bName=FALSE;
  233. if ( SUCCEEDED(hr) ) {
  234. try {
  235. //
  236. // create an instance of the log class
  237. //
  238. hr = m_pNamespace->GetObject(bstrClass, 0, m_pCtx, &pClass, NULL);
  239. if ( SUCCEEDED(hr) ) {
  240. hr = pClass->SpawnInstance(0, &pObj);
  241. }
  242. if ( SUCCEEDED(hr) ) {
  243. // fill in the properties of this class
  244. hr = PutKeyProperty(pObj, pSceStorePath, (PWSTR)wszStoreName, &bName, m_pRequest);
  245. if (SUCCEEDED(hr))
  246. hr = PutKeyProperty(pObj, pPodID, (PWSTR)szPodGUID, &bName, m_pRequest);
  247. if (SUCCEEDED(hr) )
  248. hr = PutKeyProperty(pObj, pPodSection, (PWSTR)L"1", &bName, m_pRequest);
  249. if (SUCCEEDED(hr) )
  250. hr = PutKeyProperty(pObj, pKey, (PWSTR)KeyName, &bName, m_pRequest);
  251. if (SUCCEEDED(hr) )
  252. hr = PutProperty(pObj, pValue, szValue);
  253. if ( SUCCEEDED(hr) ) {
  254. // save this instance
  255. hr = m_pNamespace->PutInstance( pObj, 0, m_pCtx, NULL );
  256. }
  257. }
  258. }catch(...){
  259. // Clean up
  260. // ========
  261. if ( bstrClass ) SysFreeString(bstrClass);
  262. if ( pClass ) {
  263. pClass->Release();
  264. }
  265. if ( pObj ) {
  266. pObj->Release();
  267. }
  268. throw;
  269. }
  270. }
  271. if ( bstrClass ) SysFreeString(bstrClass);
  272. if ( pClass ) {
  273. pClass->Release();
  274. }
  275. if ( pObj ) {
  276. pObj->Release();
  277. }
  278. return hr;
  279. }