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

476 lines
11 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. //
  5. // PRIVILEGE.CPP
  6. //
  7. // alanbos 30-Sep-98 Created.
  8. //
  9. // Defines the implementation of CSWbemPrivilege
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #ifndef _UNICODE
  14. #include <mbstring.h>
  15. #endif
  16. typedef struct PrivilegeDef {
  17. WbemPrivilegeEnum privilege;
  18. TCHAR *tName;
  19. OLECHAR *monikerName;
  20. } PrivilegeDef;
  21. #define WBEMS_MAX_NUM_PRIVILEGE 27
  22. static PrivilegeDef s_privilegeDefMap [WBEMS_MAX_NUM_PRIVILEGE] = {
  23. { wbemPrivilegeCreateToken, SE_CREATE_TOKEN_NAME, L"CreateToken" },
  24. { wbemPrivilegePrimaryToken, SE_ASSIGNPRIMARYTOKEN_NAME, L"PrimaryToken" },
  25. { wbemPrivilegeLockMemory, SE_LOCK_MEMORY_NAME, L"LockMemory" },
  26. { wbemPrivilegeIncreaseQuota, SE_INCREASE_QUOTA_NAME, L"IncreaseQuota" },
  27. { wbemPrivilegeMachineAccount, SE_MACHINE_ACCOUNT_NAME, L"MachineAccount" },
  28. { wbemPrivilegeTcb, SE_TCB_NAME, L"Tcb" },
  29. { wbemPrivilegeSecurity, SE_SECURITY_NAME, L"Security" },
  30. { wbemPrivilegeTakeOwnership, SE_TAKE_OWNERSHIP_NAME, L"TakeOwnership" },
  31. { wbemPrivilegeLoadDriver, SE_LOAD_DRIVER_NAME, L"LoadDriver" },
  32. { wbemPrivilegeSystemProfile, SE_SYSTEM_PROFILE_NAME, L"SystemProfile" },
  33. { wbemPrivilegeSystemtime, SE_SYSTEMTIME_NAME, L"SystemTime" },
  34. { wbemPrivilegeProfileSingleProcess, SE_PROF_SINGLE_PROCESS_NAME, L"ProfileSingleProcess" },
  35. { wbemPrivilegeIncreaseBasePriority, SE_INC_BASE_PRIORITY_NAME, L"IncreaseBasePriority" },
  36. { wbemPrivilegeCreatePagefile, SE_CREATE_PAGEFILE_NAME, L"CreatePagefile" },
  37. { wbemPrivilegeCreatePermanent, SE_CREATE_PERMANENT_NAME, L"CreatePermanent" },
  38. { wbemPrivilegeBackup, SE_BACKUP_NAME, L"Backup" },
  39. { wbemPrivilegeRestore, SE_RESTORE_NAME, L"Restore" },
  40. { wbemPrivilegeShutdown, SE_SHUTDOWN_NAME, L"Shutdown" },
  41. { wbemPrivilegeDebug, SE_DEBUG_NAME, L"Debug" },
  42. { wbemPrivilegeAudit, SE_AUDIT_NAME, L"Audit" },
  43. { wbemPrivilegeSystemEnvironment, SE_SYSTEM_ENVIRONMENT_NAME, L"SystemEnvironment" },
  44. { wbemPrivilegeChangeNotify, SE_CHANGE_NOTIFY_NAME, L"ChangeNotify" },
  45. { wbemPrivilegeRemoteShutdown, SE_REMOTE_SHUTDOWN_NAME, L"RemoteShutdown" },
  46. { wbemPrivilegeUndock, SE_UNDOCK_NAME, L"Undock" },
  47. { wbemPrivilegeSyncAgent, SE_SYNC_AGENT_NAME, L"SyncAgent" },
  48. { wbemPrivilegeEnableDelegation, SE_ENABLE_DELEGATION_NAME, L"EnableDelegation" },
  49. { wbemPrivilegeManageVolume, SE_MANAGE_VOLUME_NAME, L"ManageVolume" }
  50. };
  51. TCHAR *CSWbemPrivilege::GetNameFromId (WbemPrivilegeEnum iPrivilege)
  52. {
  53. DWORD i = iPrivilege - 1;
  54. return (WBEMS_MAX_NUM_PRIVILEGE > i) ?
  55. s_privilegeDefMap [i].tName : NULL;
  56. }
  57. OLECHAR *CSWbemPrivilege::GetMonikerNameFromId (WbemPrivilegeEnum iPrivilege)
  58. {
  59. DWORD i = iPrivilege - 1;
  60. return (WBEMS_MAX_NUM_PRIVILEGE > i) ?
  61. s_privilegeDefMap [i].monikerName : NULL;
  62. }
  63. bool CSWbemPrivilege::GetIdFromMonikerName (OLECHAR *pName, WbemPrivilegeEnum &iPrivilege)
  64. {
  65. bool status = false;
  66. if (pName)
  67. {
  68. for (DWORD i = 0; i < WBEMS_MAX_NUM_PRIVILEGE; i++)
  69. {
  70. if (0 == _wcsnicmp (pName, s_privilegeDefMap [i].monikerName,
  71. wcslen (s_privilegeDefMap [i].monikerName)))
  72. {
  73. // Success
  74. iPrivilege = s_privilegeDefMap [i].privilege;
  75. status = true;
  76. break;
  77. }
  78. }
  79. }
  80. return status;
  81. }
  82. bool CSWbemPrivilege::GetIdFromName (BSTR bsName, WbemPrivilegeEnum &iPrivilege)
  83. {
  84. bool status = false;
  85. if (bsName)
  86. {
  87. #ifdef _UNICODE
  88. for (DWORD i = 0; i < WBEMS_MAX_NUM_PRIVILEGE; i++)
  89. {
  90. if (0 == _wcsicmp (bsName, s_privilegeDefMap [i].tName))
  91. {
  92. // Success
  93. iPrivilege = s_privilegeDefMap [i].privilege;
  94. status = true;
  95. break;
  96. }
  97. }
  98. #else
  99. // Convert bsName to a multibyte string
  100. size_t mbsNameLen = wcstombs (NULL, bsName, 0);
  101. char *mbsName = new char [mbsNameLen + 1];
  102. if (mbsName)
  103. {
  104. wcstombs (mbsName, bsName, mbsNameLen);
  105. mbsName [mbsNameLen] = NULL;
  106. for (DWORD i = 0; i < WBEMS_MAX_NUM_PRIVILEGE; i++)
  107. {
  108. if (0 == _mbsicmp ((unsigned char *)mbsName, (unsigned char *)(s_privilegeDefMap [i].tName)))
  109. {
  110. // Success
  111. iPrivilege = s_privilegeDefMap [i].privilege;
  112. status = true;
  113. break;
  114. }
  115. }
  116. delete [] mbsName;
  117. }
  118. #endif
  119. }
  120. return status;
  121. }
  122. //***************************************************************************
  123. //
  124. // CSWbemPrivilege::CSWbemPrivilege
  125. //
  126. // CONSTRUCTOR
  127. //
  128. //***************************************************************************
  129. CSWbemPrivilege::CSWbemPrivilege (
  130. WbemPrivilegeEnum iPrivilege,
  131. LUID &luid,
  132. bool bIsEnabled
  133. )
  134. {
  135. m_Dispatch.SetObj (this, IID_ISWbemPrivilege,
  136. CLSID_SWbemPrivilege, L"SWbemPrivilege");
  137. m_cRef=1;
  138. m_privilege = iPrivilege;
  139. m_Luid = luid;
  140. m_bIsEnabled = bIsEnabled;
  141. InterlockedIncrement(&g_cObj);
  142. }
  143. //***************************************************************************
  144. //
  145. // CSWbemPrivilege::~CSWbemPrivilege
  146. //
  147. // DESTRUCTOR
  148. //
  149. //***************************************************************************
  150. CSWbemPrivilege::~CSWbemPrivilege (void)
  151. {
  152. InterlockedDecrement(&g_cObj);
  153. }
  154. //***************************************************************************
  155. // HRESULT CSWbemPrivilege::QueryInterface
  156. // long CSWbemPrivilege::AddRef
  157. // long CSWbemPrivilege::Release
  158. //
  159. // DESCRIPTION:
  160. //
  161. // Standard Com IUNKNOWN functions.
  162. //
  163. //***************************************************************************
  164. STDMETHODIMP CSWbemPrivilege::QueryInterface (
  165. IN REFIID riid,
  166. OUT LPVOID *ppv
  167. )
  168. {
  169. *ppv=NULL;
  170. if (IID_IUnknown==riid)
  171. *ppv = reinterpret_cast<IUnknown*>(this);
  172. else if (IID_ISWbemPrivilege==riid)
  173. *ppv = (ISWbemPrivilege *)this;
  174. else if (IID_IDispatch==riid)
  175. *ppv = (IDispatch *)this;
  176. else if (IID_ISupportErrorInfo==riid)
  177. *ppv = (ISupportErrorInfo *)this;
  178. else if (IID_IProvideClassInfo==riid)
  179. *ppv = (IProvideClassInfo *)this;
  180. if (NULL!=*ppv)
  181. {
  182. ((LPUNKNOWN)*ppv)->AddRef();
  183. return NOERROR;
  184. }
  185. return ResultFromScode(E_NOINTERFACE);
  186. }
  187. STDMETHODIMP_(ULONG) CSWbemPrivilege::AddRef(void)
  188. {
  189. long l = InterlockedIncrement(&m_cRef);
  190. return l;
  191. }
  192. STDMETHODIMP_(ULONG) CSWbemPrivilege::Release(void)
  193. {
  194. long l = InterlockedDecrement(&m_cRef);
  195. if (0L!=l)
  196. return l;
  197. delete this;
  198. return 0;
  199. }
  200. //***************************************************************************
  201. // HRESULT CSWbemPrivilege::InterfaceSupportsErrorInfo
  202. //
  203. // DESCRIPTION:
  204. //
  205. // Standard Com ISupportErrorInfo functions.
  206. //
  207. //***************************************************************************
  208. STDMETHODIMP CSWbemPrivilege::InterfaceSupportsErrorInfo (IN REFIID riid)
  209. {
  210. return (IID_ISWbemPrivilege == riid) ? S_OK : S_FALSE;
  211. }
  212. //***************************************************************************
  213. //
  214. // SCODE CSWbemPrivilege::get_Identifier
  215. //
  216. // DESCRIPTION:
  217. //
  218. // Retrieve the privilege identifier
  219. //
  220. // PARAMETERS:
  221. //
  222. // pIsEnabled holds the value on return
  223. //
  224. // RETURN VALUES:
  225. //
  226. // WBEM_S_NO_ERROR success
  227. // WBEM_E_INVALID_PARAMETER bad input parameters
  228. // WBEM_E_FAILED otherwise
  229. //
  230. //***************************************************************************
  231. HRESULT CSWbemPrivilege::get_Identifier (
  232. WbemPrivilegeEnum *pPrivilege
  233. )
  234. {
  235. HRESULT hr = WBEM_E_FAILED;
  236. ResetLastErrors ();
  237. if (NULL == pPrivilege)
  238. hr = WBEM_E_INVALID_PARAMETER;
  239. else
  240. {
  241. *pPrivilege = m_privilege;
  242. hr = WBEM_S_NO_ERROR;
  243. }
  244. if (FAILED(hr))
  245. m_Dispatch.RaiseException (hr);
  246. return hr;
  247. }
  248. //***************************************************************************
  249. //
  250. // SCODE CSWbemPrivilege::get_IsEnabled
  251. //
  252. // DESCRIPTION:
  253. //
  254. // Retrieve the override state
  255. //
  256. // PARAMETERS:
  257. //
  258. // pIsEnabled holds the value on return
  259. //
  260. // RETURN VALUES:
  261. //
  262. // WBEM_S_NO_ERROR success
  263. // WBEM_E_INVALID_PARAMETER bad input parameters
  264. // WBEM_E_FAILED otherwise
  265. //
  266. //***************************************************************************
  267. HRESULT CSWbemPrivilege::get_IsEnabled (
  268. VARIANT_BOOL *pIsEnabled
  269. )
  270. {
  271. HRESULT hr = WBEM_E_FAILED;
  272. ResetLastErrors ();
  273. if (NULL == pIsEnabled)
  274. hr = WBEM_E_INVALID_PARAMETER;
  275. else
  276. {
  277. *pIsEnabled = (m_bIsEnabled) ? VARIANT_TRUE : VARIANT_FALSE;
  278. hr = WBEM_S_NO_ERROR;
  279. }
  280. if (FAILED(hr))
  281. m_Dispatch.RaiseException (hr);
  282. return hr;
  283. }
  284. //***************************************************************************
  285. //
  286. // SCODE CSWbemPrivilege::put_IsEnabled
  287. //
  288. // DESCRIPTION:
  289. //
  290. // Set the override state
  291. //
  292. // PARAMETERS:
  293. //
  294. // bIsEnabled the new value
  295. //
  296. // RETURN VALUES:
  297. //
  298. // WBEM_S_NO_ERROR success
  299. // WBEM_E_INVALID_PARAMETER bad input parameters
  300. // WBEM_E_FAILED otherwise
  301. //
  302. //***************************************************************************
  303. HRESULT CSWbemPrivilege::put_IsEnabled (
  304. VARIANT_BOOL bIsEnabled
  305. )
  306. {
  307. m_bIsEnabled = (bIsEnabled) ? true : false;
  308. return WBEM_S_NO_ERROR;
  309. }
  310. //***************************************************************************
  311. //
  312. // SCODE CSWbemPrivilege::get_Name
  313. //
  314. // DESCRIPTION:
  315. //
  316. // Retrieve the privilege name
  317. //
  318. // PARAMETERS:
  319. //
  320. // pName holds the value on return
  321. //
  322. // RETURN VALUES:
  323. //
  324. // WBEM_S_NO_ERROR success
  325. // WBEM_E_INVALID_PARAMETER bad input parameters
  326. // WBEM_E_FAILED otherwise
  327. //
  328. //***************************************************************************
  329. HRESULT CSWbemPrivilege::get_Name (
  330. BSTR *pName
  331. )
  332. {
  333. HRESULT hr = WBEM_E_FAILED;
  334. ResetLastErrors ();
  335. if (NULL == pName)
  336. hr = WBEM_E_INVALID_PARAMETER;
  337. else
  338. {
  339. TCHAR *tName = GetNameFromId (m_privilege);
  340. if (tName)
  341. {
  342. // Have a valid name - now copy it to a BSTR
  343. #ifdef _UNICODE
  344. if (*pName = SysAllocString (tName))
  345. hr = WBEM_S_NO_ERROR;
  346. else
  347. hr = WBEM_E_OUT_OF_MEMORY;
  348. #else
  349. size_t tNameLen = strlen (tName);
  350. OLECHAR *nameW = new OLECHAR [tNameLen + 1];
  351. if (nameW)
  352. {
  353. mbstowcs (nameW, tName, tNameLen);
  354. nameW [tNameLen] = NULL;
  355. *pName = SysAllocString (nameW);
  356. delete [] nameW;
  357. hr = WBEM_S_NO_ERROR;
  358. }
  359. else
  360. hr = WBEM_E_OUT_OF_MEMORY;
  361. #endif
  362. }
  363. }
  364. if (FAILED(hr))
  365. m_Dispatch.RaiseException (hr);
  366. return hr;
  367. }
  368. //***************************************************************************
  369. //
  370. // SCODE CSWbemPrivilege::get_DisplayName
  371. //
  372. // DESCRIPTION:
  373. //
  374. // Retrieve the privilege display name
  375. //
  376. // PARAMETERS:
  377. //
  378. // pDisplayName holds the value on return
  379. //
  380. // RETURN VALUES:
  381. //
  382. // WBEM_S_NO_ERROR success
  383. // WBEM_E_INVALID_PARAMETER bad input parameters
  384. // WBEM_E_FAILED otherwise
  385. //
  386. //***************************************************************************
  387. HRESULT CSWbemPrivilege::get_DisplayName (
  388. BSTR *pDisplayName
  389. )
  390. {
  391. HRESULT hr = WBEM_E_FAILED;
  392. ResetLastErrors ();
  393. if (NULL == pDisplayName)
  394. hr = WBEM_E_INVALID_PARAMETER;
  395. else
  396. {
  397. TCHAR *tName = GetNameFromId (m_privilege);
  398. if (tName)
  399. {
  400. CSWbemSecurity::LookupPrivilegeDisplayName (tName, pDisplayName);
  401. hr = WBEM_S_NO_ERROR;
  402. }
  403. }
  404. if (FAILED(hr))
  405. m_Dispatch.RaiseException (hr);
  406. return hr;
  407. }
  408. void CSWbemPrivilege::GetLUID (PLUID pLuid)
  409. {
  410. if (pLuid)
  411. *pLuid = m_Luid;
  412. }