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.

623 lines
13 KiB

  1. //---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1995
  5. //
  6. // File: cAccessControlEntry.cxx
  7. //
  8. // Contents: AccessControlEntry object
  9. //
  10. // History: 11-1-95 krishnag Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "oleds.hxx"
  14. #pragma hdrstop
  15. // Class CAccessControlEntry
  16. DEFINE_IDispatch_Implementation(CAccessControlEntry)
  17. CAccessControlEntry::CAccessControlEntry():
  18. _pDispMgr(NULL),
  19. _dwAceType(0),
  20. _dwAccessMask(0),
  21. _dwAceFlags(0),
  22. _dwFlags(0),
  23. _lpObjectType(NULL),
  24. _lpInheritedObjectType(NULL),
  25. _lpTrustee(NULL),
  26. _pSid(NULL),
  27. _dwSidLen(0)
  28. {
  29. ENLIST_TRACKING(CAccessControlEntry);
  30. }
  31. HRESULT
  32. CAccessControlEntry::CreateAccessControlEntry(
  33. REFIID riid,
  34. void **ppvObj
  35. )
  36. {
  37. CAccessControlEntry FAR * pAccessControlEntry = NULL;
  38. HRESULT hr = S_OK;
  39. hr = AllocateAccessControlEntryObject(&pAccessControlEntry);
  40. BAIL_ON_FAILURE(hr);
  41. hr = pAccessControlEntry->QueryInterface(riid, ppvObj);
  42. BAIL_ON_FAILURE(hr);
  43. pAccessControlEntry->Release();
  44. RRETURN(hr);
  45. error:
  46. delete pAccessControlEntry;
  47. RRETURN_EXP_IF_ERR(hr);
  48. }
  49. CAccessControlEntry::~CAccessControlEntry( )
  50. {
  51. delete _pDispMgr;
  52. if (_lpInheritedObjectType) {
  53. FreeADsStr(_lpInheritedObjectType);
  54. _lpInheritedObjectType = NULL;
  55. }
  56. if (_lpObjectType) {
  57. FreeADsStr(_lpObjectType);
  58. _lpObjectType = NULL;
  59. }
  60. if (_lpTrustee) {
  61. FreeADsStr(_lpTrustee);
  62. }
  63. if (_pSid) {
  64. FreeADsMem(_pSid);
  65. }
  66. }
  67. STDMETHODIMP
  68. CAccessControlEntry::QueryInterface(
  69. REFIID iid,
  70. LPVOID FAR* ppv
  71. )
  72. {
  73. if (IsEqualIID(iid, IID_IUnknown))
  74. {
  75. *ppv = (IADsAccessControlEntry FAR *) this;
  76. }
  77. else if (IsEqualIID(iid, IID_IADsAccessControlEntry))
  78. {
  79. *ppv = (IADsAccessControlEntry FAR *) this;
  80. }
  81. else if (IsEqualIID(iid, IID_IDispatch))
  82. {
  83. *ppv = (IADsAccessControlEntry FAR *) this;
  84. }
  85. else if (IsEqualIID(iid, IID_ISupportErrorInfo))
  86. {
  87. *ppv = (ISupportErrorInfo FAR *) this;
  88. }
  89. else if (IsEqualIID(iid, IID_IADsAcePrivate)) {
  90. *ppv = (IADsAcePrivate FAR *) this;
  91. }
  92. else
  93. {
  94. *ppv = NULL;
  95. return E_NOINTERFACE;
  96. }
  97. AddRef();
  98. return NOERROR;
  99. }
  100. HRESULT
  101. CAccessControlEntry::AllocateAccessControlEntryObject(
  102. CAccessControlEntry ** ppAccessControlEntry
  103. )
  104. {
  105. CAccessControlEntry FAR * pAccessControlEntry = NULL;
  106. CDispatchMgr FAR * pDispMgr = NULL;
  107. HRESULT hr = S_OK;
  108. pAccessControlEntry = new CAccessControlEntry();
  109. if (pAccessControlEntry == NULL) {
  110. hr = E_OUTOFMEMORY;
  111. }
  112. BAIL_ON_FAILURE(hr);
  113. pDispMgr = new CDispatchMgr;
  114. if (pDispMgr == NULL) {
  115. hr = E_OUTOFMEMORY;
  116. }
  117. BAIL_ON_FAILURE(hr);
  118. hr = LoadTypeInfoEntry(
  119. pDispMgr,
  120. LIBID_ADs,
  121. IID_IADsAccessControlEntry,
  122. (IADsAccessControlEntry *)pAccessControlEntry,
  123. DISPID_REGULAR
  124. );
  125. BAIL_ON_FAILURE(hr);
  126. pAccessControlEntry->_pDispMgr = pDispMgr;
  127. *ppAccessControlEntry = pAccessControlEntry;
  128. RRETURN(hr);
  129. error:
  130. delete pDispMgr;
  131. RRETURN_EXP_IF_ERR(hr);
  132. }
  133. //
  134. // ISupportErrorInfo method
  135. //
  136. STDMETHODIMP
  137. CAccessControlEntry::InterfaceSupportsErrorInfo(THIS_ REFIID riid)
  138. {
  139. if (IsEqualIID(riid, IID_IADsAccessControlEntry)) {
  140. return S_OK;
  141. } else {
  142. return S_FALSE;
  143. }
  144. }
  145. //
  146. // IADsAccessControlEntry implementation
  147. //
  148. STDMETHODIMP
  149. CAccessControlEntry::get_AceType(THIS_ long FAR * retval)
  150. {
  151. *retval = _dwAceType;
  152. RRETURN(S_OK);
  153. }
  154. STDMETHODIMP
  155. CAccessControlEntry::put_AceType(THIS_ long lnAceType)
  156. {
  157. _dwAceType = lnAceType;
  158. RRETURN(S_OK);
  159. }
  160. STDMETHODIMP
  161. CAccessControlEntry::get_AceFlags(THIS_ long FAR * retval)
  162. {
  163. *retval = _dwAceFlags;
  164. RRETURN(S_OK);
  165. }
  166. STDMETHODIMP
  167. CAccessControlEntry::put_AceFlags(THIS_ long lnAceFlags)
  168. {
  169. _dwAceFlags = lnAceFlags;
  170. RRETURN(S_OK);
  171. }
  172. STDMETHODIMP
  173. CAccessControlEntry::get_Flags(THIS_ long FAR * retval)
  174. {
  175. *retval = _dwFlags;
  176. RRETURN(S_OK);
  177. }
  178. STDMETHODIMP
  179. CAccessControlEntry::put_Flags(THIS_ long lnFlags)
  180. {
  181. _dwFlags = lnFlags;
  182. RRETURN(S_OK);
  183. }
  184. STDMETHODIMP
  185. CAccessControlEntry::get_AccessMask(THIS_ long FAR * retval)
  186. {
  187. *retval = (long)_dwAccessMask;
  188. RRETURN(S_OK);
  189. }
  190. STDMETHODIMP
  191. CAccessControlEntry::put_AccessMask(THIS_ long lnAccessMask)
  192. {
  193. _dwAccessMask = (DWORD)lnAccessMask;
  194. RRETURN(S_OK);
  195. }
  196. STDMETHODIMP
  197. CAccessControlEntry::get_ObjectType(THIS_ BSTR FAR * retval)
  198. {
  199. HRESULT hr = S_OK;
  200. hr = ADsAllocString(_lpObjectType, retval);
  201. RRETURN_EXP_IF_ERR(hr);
  202. }
  203. STDMETHODIMP
  204. CAccessControlEntry::put_ObjectType(THIS_ BSTR bstrObjectType)
  205. {
  206. if (_lpObjectType) {
  207. FreeADsStr(_lpObjectType);
  208. _lpObjectType = NULL;
  209. }
  210. if (bstrObjectType) {
  211. _lpObjectType = AllocADsStr(bstrObjectType);
  212. if (!_lpObjectType) {
  213. RRETURN_EXP_IF_ERR(E_OUTOFMEMORY);
  214. }
  215. }
  216. RRETURN(S_OK);
  217. }
  218. STDMETHODIMP
  219. CAccessControlEntry::get_InheritedObjectType(THIS_ BSTR FAR * retval)
  220. {
  221. HRESULT hr = S_OK;
  222. hr = ADsAllocString(_lpInheritedObjectType, retval);
  223. RRETURN_EXP_IF_ERR(hr);
  224. }
  225. STDMETHODIMP
  226. CAccessControlEntry::put_InheritedObjectType(THIS_ BSTR bstrInheritedObjectType)
  227. {
  228. if (_lpInheritedObjectType) {
  229. FreeADsStr(_lpInheritedObjectType);
  230. _lpInheritedObjectType = NULL;
  231. }
  232. if (bstrInheritedObjectType) {
  233. _lpInheritedObjectType = AllocADsStr(bstrInheritedObjectType);
  234. if (!_lpInheritedObjectType) {
  235. RRETURN(E_OUTOFMEMORY);
  236. }
  237. }
  238. RRETURN(S_OK);
  239. }
  240. STDMETHODIMP
  241. CAccessControlEntry::get_Trustee(THIS_ BSTR FAR * retval)
  242. {
  243. HRESULT hr = S_OK;
  244. hr = ADsAllocString(_lpTrustee, retval);
  245. RRETURN_EXP_IF_ERR(hr);
  246. }
  247. STDMETHODIMP
  248. CAccessControlEntry::put_Trustee(THIS_ BSTR bstrTrustee)
  249. {
  250. if (!bstrTrustee) {
  251. RRETURN(E_FAIL);
  252. }
  253. if (_lpTrustee) {
  254. FreeADsStr(_lpTrustee);
  255. }
  256. //
  257. // Since we are changing the trustee, we need to make
  258. // sure the sid value is cleared.
  259. //
  260. if (_dwSidLen) {
  261. _dwSidLen = 0;
  262. }
  263. if (_pSid) {
  264. FreeADsMem(_pSid);
  265. _pSid = NULL;
  266. }
  267. _lpTrustee = AllocADsStr(bstrTrustee);
  268. if (!_lpTrustee) {
  269. RRETURN_EXP_IF_ERR(E_OUTOFMEMORY);
  270. }
  271. RRETURN(S_OK);
  272. }
  273. HRESULT
  274. CopyAccessControlEntry(
  275. IADsAccessControlEntry * pSourceAce,
  276. IADsAccessControlEntry ** ppTargetAce
  277. )
  278. {
  279. IADsAccessControlEntry * pTargetAce = NULL;
  280. IADsAcePrivate *pPrivAceTarget = NULL;
  281. IADsAcePrivate *pPrivAceSource = NULL;
  282. BSTR bstrInheritedObjectTypeClsid = NULL;
  283. BSTR bstrObjectTypeClsid = NULL;
  284. BSTR bstrTrustee = NULL;
  285. HRESULT hr = S_OK;
  286. DWORD dwAceType = 0;
  287. DWORD dwAceFlags = 0;
  288. DWORD dwAccessMask = 0;
  289. DWORD dwFlags = 0;
  290. DWORD dwSidLen = 0;
  291. BOOL fSidValid = FALSE;
  292. PSID pSid = NULL;
  293. hr = pSourceAce->get_AceType((LONG *)&dwAceType);
  294. BAIL_ON_FAILURE(hr);
  295. hr = pSourceAce->get_Trustee(&bstrTrustee);
  296. BAIL_ON_FAILURE(hr);
  297. hr = pSourceAce->get_AceFlags((long *)&dwAceFlags);
  298. BAIL_ON_FAILURE(hr);
  299. hr = pSourceAce->get_AccessMask((long *)&dwAccessMask);
  300. BAIL_ON_FAILURE(hr);
  301. hr = pSourceAce->get_Flags((LONG *)&dwFlags);
  302. BAIL_ON_FAILURE(hr);
  303. hr = pSourceAce->get_ObjectType(&bstrObjectTypeClsid);
  304. BAIL_ON_FAILURE(hr);
  305. hr = pSourceAce->get_InheritedObjectType(&bstrInheritedObjectTypeClsid);
  306. BAIL_ON_FAILURE(hr);
  307. hr = CoCreateInstance(
  308. CLSID_AccessControlEntry,
  309. NULL,
  310. CLSCTX_INPROC_SERVER,
  311. IID_IADsAccessControlEntry,
  312. (void **)&pTargetAce
  313. );
  314. BAIL_ON_FAILURE(hr);
  315. hr = pTargetAce->put_AccessMask(dwAccessMask);
  316. BAIL_ON_FAILURE(hr);
  317. hr = pTargetAce->put_AceFlags(dwAceFlags);
  318. BAIL_ON_FAILURE(hr);
  319. hr = pTargetAce->put_AceType(dwAceType);
  320. BAIL_ON_FAILURE(hr);
  321. hr = pTargetAce->put_Trustee(bstrTrustee);
  322. BAIL_ON_FAILURE(hr);
  323. hr = pTargetAce->put_Flags((LONG)dwFlags);
  324. BAIL_ON_FAILURE(hr);
  325. hr = pTargetAce->put_ObjectType(bstrObjectTypeClsid);
  326. BAIL_ON_FAILURE(hr);
  327. hr = pTargetAce->put_InheritedObjectType(bstrInheritedObjectTypeClsid);
  328. BAIL_ON_FAILURE(hr);
  329. hr = pTargetAce->QueryInterface(
  330. IID_IADsAcePrivate,
  331. (void **) &pPrivAceTarget
  332. );
  333. if (SUCCEEDED(hr)) {
  334. hr = pSourceAce->QueryInterface(
  335. IID_IADsAcePrivate,
  336. (void **) &pPrivAceSource
  337. );
  338. if (SUCCEEDED(hr)) {
  339. hr = pPrivAceSource->isSidValid(&fSidValid);
  340. if (SUCCEEDED(hr) && fSidValid) {
  341. //
  342. // Get the sid and try putting it.
  343. //
  344. hr = pPrivAceSource->getSid(
  345. &pSid,
  346. &dwSidLen
  347. );
  348. if (SUCCEEDED(hr)) {
  349. pPrivAceTarget->putSid(
  350. pSid,
  351. dwSidLen
  352. );
  353. }
  354. }
  355. }
  356. }
  357. *ppTargetAce = pTargetAce;
  358. error:
  359. if (bstrTrustee) {
  360. ADsFreeString(bstrTrustee);
  361. }
  362. if (bstrObjectTypeClsid) {
  363. ADsFreeString(bstrObjectTypeClsid);
  364. }
  365. if (bstrInheritedObjectTypeClsid) {
  366. ADsFreeString(bstrInheritedObjectTypeClsid);
  367. }
  368. if (pPrivAceTarget) {
  369. pPrivAceTarget->Release();
  370. }
  371. if (pPrivAceSource) {
  372. pPrivAceSource->Release();
  373. }
  374. if (pSid) {
  375. FreeADsMem(pSid);
  376. }
  377. RRETURN_EXP_IF_ERR(hr);
  378. }
  379. //
  380. // IADsAcePrivate methods.
  381. //
  382. //+---------------------------------------------------------------------------
  383. // Function: CAccessControlEntry::getSid - IADsAcePrivate method.
  384. //
  385. // Synopsis: Returns the SID associated with the trustee on this ACE
  386. // object assuming there is one that is correct.
  387. //
  388. // Arguments: ppSid - Return pointer to SID.
  389. // pdwLength - Return lenght of the SID.
  390. //
  391. // Returns: S_OK or appropriate failure error code.
  392. //
  393. // Modifies: ppSid and pdwLength
  394. //
  395. //----------------------------------------------------------------------------
  396. STDMETHODIMP
  397. CAccessControlEntry::getSid(
  398. OUT PSID *ppSid,
  399. OUT DWORD *pdwLength
  400. )
  401. {
  402. HRESULT hr = S_OK;
  403. PSID pSid = NULL;
  404. if (!ppSid || !pdwLength) {
  405. BAIL_ON_FAILURE(hr = E_ADS_BAD_PARAMETER);
  406. }
  407. if (!_pSid) {
  408. //
  409. // This means that the there is no usable value for the sid.
  410. //
  411. RRETURN(E_FAIL);
  412. }
  413. pSid = (PSID) AllocADsMem(_dwSidLen);
  414. if (!pSid) {
  415. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  416. }
  417. memcpy(pSid, _pSid, _dwSidLen);
  418. *pdwLength = _dwSidLen;
  419. *ppSid = pSid;
  420. error:
  421. RRETURN(hr);
  422. }
  423. //+---------------------------------------------------------------------------
  424. // Function: CAccessControlEntry::putSid - IADsAcePrivate method.
  425. //
  426. // Synopsis: Updates the SID associated with the trustee on this Ace
  427. // object to the value specified.
  428. //
  429. // Arguments: PSID - Pointer to SID.
  430. // dwLength - Lenght of the SID.
  431. //
  432. // Returns: S_OK on success or E_ADS_BAD_PARAMETER, E_OUTOFMEMORY
  433. // on failure.
  434. //
  435. // Modifies: _pSid and _dwSidLen.
  436. //
  437. //----------------------------------------------------------------------------
  438. STDMETHODIMP
  439. CAccessControlEntry::putSid(
  440. IN PSID pSid,
  441. IN DWORD dwLength
  442. )
  443. {
  444. HRESULT hr = S_OK;
  445. PSID pLocalSid = NULL;
  446. if (dwLength && !pSid) {
  447. BAIL_ON_FAILURE(hr = E_ADS_BAD_PARAMETER);
  448. }
  449. if (_pSid) {
  450. FreeADsMem(_pSid);
  451. _pSid = NULL;
  452. _dwSidLen = 0;
  453. }
  454. if (dwLength) {
  455. //
  456. // pSid also has to be valid at this point
  457. //
  458. _pSid = (PSID) AllocADsMem(dwLength);
  459. if (!_pSid) {
  460. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  461. }
  462. memcpy(_pSid, pSid, dwLength);
  463. _dwSidLen = dwLength;
  464. }
  465. error:
  466. RRETURN(hr);
  467. }
  468. //+---------------------------------------------------------------------------
  469. // Function: CAccessControlEntry::isSidValid - IADsAcePrivate method.
  470. //
  471. // Synopsis: Returns status of the sid associated with this ACE's trustee.
  472. //
  473. // Arguments: pfSidValid - Return value.
  474. //
  475. // Returns: S_OK or E_ADS_BAD_PARAMETER.
  476. //
  477. // Modifies: pfSidValid set to TRUE or FALSE as appropriate.
  478. //
  479. //----------------------------------------------------------------------------
  480. STDMETHODIMP
  481. CAccessControlEntry::isSidValid(
  482. OUT BOOL *pfSidValid
  483. )
  484. {
  485. if (!pfSidValid) {
  486. RRETURN(E_ADS_BAD_PARAMETER);
  487. }
  488. //
  489. // If the Sid length is zero, then there is no sid for trustee.
  490. //
  491. *pfSidValid = (_dwSidLen != 0);
  492. RRETURN(S_OK);
  493. }
  494.