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.

480 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 cRef = InterlockedDecrement(&m_cRef);
  195. if (0 != cRef)
  196. {
  197. _ASSERT(cRef > 0);
  198. return cRef;
  199. }
  200. delete this;
  201. return 0;
  202. }
  203. //***************************************************************************
  204. // HRESULT CSWbemPrivilege::InterfaceSupportsErrorInfo
  205. //
  206. // DESCRIPTION:
  207. //
  208. // Standard Com ISupportErrorInfo functions.
  209. //
  210. //***************************************************************************
  211. STDMETHODIMP CSWbemPrivilege::InterfaceSupportsErrorInfo (IN REFIID riid)
  212. {
  213. return (IID_ISWbemPrivilege == riid) ? S_OK : S_FALSE;
  214. }
  215. //***************************************************************************
  216. //
  217. // SCODE CSWbemPrivilege::get_Identifier
  218. //
  219. // DESCRIPTION:
  220. //
  221. // Retrieve the privilege identifier
  222. //
  223. // PARAMETERS:
  224. //
  225. // pIsEnabled holds the value on return
  226. //
  227. // RETURN VALUES:
  228. //
  229. // WBEM_S_NO_ERROR success
  230. // WBEM_E_INVALID_PARAMETER bad input parameters
  231. // WBEM_E_FAILED otherwise
  232. //
  233. //***************************************************************************
  234. HRESULT CSWbemPrivilege::get_Identifier (
  235. WbemPrivilegeEnum *pPrivilege
  236. )
  237. {
  238. HRESULT hr = WBEM_E_FAILED;
  239. ResetLastErrors ();
  240. if (NULL == pPrivilege)
  241. hr = WBEM_E_INVALID_PARAMETER;
  242. else
  243. {
  244. *pPrivilege = m_privilege;
  245. hr = WBEM_S_NO_ERROR;
  246. }
  247. if (FAILED(hr))
  248. m_Dispatch.RaiseException (hr);
  249. return hr;
  250. }
  251. //***************************************************************************
  252. //
  253. // SCODE CSWbemPrivilege::get_IsEnabled
  254. //
  255. // DESCRIPTION:
  256. //
  257. // Retrieve the override state
  258. //
  259. // PARAMETERS:
  260. //
  261. // pIsEnabled holds the value on return
  262. //
  263. // RETURN VALUES:
  264. //
  265. // WBEM_S_NO_ERROR success
  266. // WBEM_E_INVALID_PARAMETER bad input parameters
  267. // WBEM_E_FAILED otherwise
  268. //
  269. //***************************************************************************
  270. HRESULT CSWbemPrivilege::get_IsEnabled (
  271. VARIANT_BOOL *pIsEnabled
  272. )
  273. {
  274. HRESULT hr = WBEM_E_FAILED;
  275. ResetLastErrors ();
  276. if (NULL == pIsEnabled)
  277. hr = WBEM_E_INVALID_PARAMETER;
  278. else
  279. {
  280. *pIsEnabled = (m_bIsEnabled) ? VARIANT_TRUE : VARIANT_FALSE;
  281. hr = WBEM_S_NO_ERROR;
  282. }
  283. if (FAILED(hr))
  284. m_Dispatch.RaiseException (hr);
  285. return hr;
  286. }
  287. //***************************************************************************
  288. //
  289. // SCODE CSWbemPrivilege::put_IsEnabled
  290. //
  291. // DESCRIPTION:
  292. //
  293. // Set the override state
  294. //
  295. // PARAMETERS:
  296. //
  297. // bIsEnabled the new value
  298. //
  299. // RETURN VALUES:
  300. //
  301. // WBEM_S_NO_ERROR success
  302. // WBEM_E_INVALID_PARAMETER bad input parameters
  303. // WBEM_E_FAILED otherwise
  304. //
  305. //***************************************************************************
  306. HRESULT CSWbemPrivilege::put_IsEnabled (
  307. VARIANT_BOOL bIsEnabled
  308. )
  309. {
  310. m_bIsEnabled = (bIsEnabled) ? true : false;
  311. return WBEM_S_NO_ERROR;
  312. }
  313. //***************************************************************************
  314. //
  315. // SCODE CSWbemPrivilege::get_Name
  316. //
  317. // DESCRIPTION:
  318. //
  319. // Retrieve the privilege name
  320. //
  321. // PARAMETERS:
  322. //
  323. // pName holds the value on return
  324. //
  325. // RETURN VALUES:
  326. //
  327. // WBEM_S_NO_ERROR success
  328. // WBEM_E_INVALID_PARAMETER bad input parameters
  329. // WBEM_E_FAILED otherwise
  330. //
  331. //***************************************************************************
  332. HRESULT CSWbemPrivilege::get_Name (
  333. BSTR *pName
  334. )
  335. {
  336. HRESULT hr = WBEM_E_FAILED;
  337. ResetLastErrors ();
  338. if (NULL == pName)
  339. hr = WBEM_E_INVALID_PARAMETER;
  340. else
  341. {
  342. TCHAR *tName = GetNameFromId (m_privilege);
  343. if (tName)
  344. {
  345. // Have a valid name - now copy it to a BSTR
  346. #ifdef _UNICODE
  347. if (*pName = SysAllocString (tName))
  348. hr = WBEM_S_NO_ERROR;
  349. else
  350. hr = WBEM_E_OUT_OF_MEMORY;
  351. #else
  352. size_t tNameLen = strlen (tName);
  353. OLECHAR *nameW = new OLECHAR [tNameLen + 1];
  354. if (nameW)
  355. {
  356. mbstowcs (nameW, tName, tNameLen);
  357. nameW [tNameLen] = NULL;
  358. *pName = SysAllocString (nameW);
  359. delete [] nameW;
  360. hr = WBEM_S_NO_ERROR;
  361. }
  362. else
  363. hr = WBEM_E_OUT_OF_MEMORY;
  364. #endif
  365. }
  366. }
  367. if (FAILED(hr))
  368. m_Dispatch.RaiseException (hr);
  369. return hr;
  370. }
  371. //***************************************************************************
  372. //
  373. // SCODE CSWbemPrivilege::get_DisplayName
  374. //
  375. // DESCRIPTION:
  376. //
  377. // Retrieve the privilege display name
  378. //
  379. // PARAMETERS:
  380. //
  381. // pDisplayName holds the value on return
  382. //
  383. // RETURN VALUES:
  384. //
  385. // WBEM_S_NO_ERROR success
  386. // WBEM_E_INVALID_PARAMETER bad input parameters
  387. // WBEM_E_FAILED otherwise
  388. //
  389. //***************************************************************************
  390. HRESULT CSWbemPrivilege::get_DisplayName (
  391. BSTR *pDisplayName
  392. )
  393. {
  394. HRESULT hr = WBEM_E_FAILED;
  395. ResetLastErrors ();
  396. if (NULL == pDisplayName)
  397. hr = WBEM_E_INVALID_PARAMETER;
  398. else
  399. {
  400. TCHAR *tName = GetNameFromId (m_privilege);
  401. if (tName)
  402. {
  403. CSWbemSecurity::LookupPrivilegeDisplayName (tName, pDisplayName);
  404. hr = WBEM_S_NO_ERROR;
  405. }
  406. }
  407. if (FAILED(hr))
  408. m_Dispatch.RaiseException (hr);
  409. return hr;
  410. }
  411. void CSWbemPrivilege::GetLUID (PLUID pLuid)
  412. {
  413. if (pLuid)
  414. *pLuid = m_Luid;
  415. }