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.

624 lines
14 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 pAccessControlEntry;
  131. delete pDispMgr;
  132. RRETURN_EXP_IF_ERR(hr);
  133. }
  134. //
  135. // ISupportErrorInfo method
  136. //
  137. STDMETHODIMP
  138. CAccessControlEntry::InterfaceSupportsErrorInfo(THIS_ REFIID riid)
  139. {
  140. if (IsEqualIID(riid, IID_IADsAccessControlEntry)) {
  141. return S_OK;
  142. } else {
  143. return S_FALSE;
  144. }
  145. }
  146. //
  147. // IADsAccessControlEntry implementation
  148. //
  149. STDMETHODIMP
  150. CAccessControlEntry::get_AceType(THIS_ long FAR * retval)
  151. {
  152. *retval = _dwAceType;
  153. RRETURN(S_OK);
  154. }
  155. STDMETHODIMP
  156. CAccessControlEntry::put_AceType(THIS_ long lnAceType)
  157. {
  158. _dwAceType = lnAceType;
  159. RRETURN(S_OK);
  160. }
  161. STDMETHODIMP
  162. CAccessControlEntry::get_AceFlags(THIS_ long FAR * retval)
  163. {
  164. *retval = _dwAceFlags;
  165. RRETURN(S_OK);
  166. }
  167. STDMETHODIMP
  168. CAccessControlEntry::put_AceFlags(THIS_ long lnAceFlags)
  169. {
  170. _dwAceFlags = lnAceFlags;
  171. RRETURN(S_OK);
  172. }
  173. STDMETHODIMP
  174. CAccessControlEntry::get_Flags(THIS_ long FAR * retval)
  175. {
  176. *retval = _dwFlags;
  177. RRETURN(S_OK);
  178. }
  179. STDMETHODIMP
  180. CAccessControlEntry::put_Flags(THIS_ long lnFlags)
  181. {
  182. _dwFlags = lnFlags;
  183. RRETURN(S_OK);
  184. }
  185. STDMETHODIMP
  186. CAccessControlEntry::get_AccessMask(THIS_ long FAR * retval)
  187. {
  188. *retval = (long)_dwAccessMask;
  189. RRETURN(S_OK);
  190. }
  191. STDMETHODIMP
  192. CAccessControlEntry::put_AccessMask(THIS_ long lnAccessMask)
  193. {
  194. _dwAccessMask = (DWORD)lnAccessMask;
  195. RRETURN(S_OK);
  196. }
  197. STDMETHODIMP
  198. CAccessControlEntry::get_ObjectType(THIS_ BSTR FAR * retval)
  199. {
  200. HRESULT hr = S_OK;
  201. hr = ADsAllocString(_lpObjectType, retval);
  202. RRETURN_EXP_IF_ERR(hr);
  203. }
  204. STDMETHODIMP
  205. CAccessControlEntry::put_ObjectType(THIS_ BSTR bstrObjectType)
  206. {
  207. if (_lpObjectType) {
  208. FreeADsStr(_lpObjectType);
  209. _lpObjectType = NULL;
  210. }
  211. if (bstrObjectType) {
  212. _lpObjectType = AllocADsStr(bstrObjectType);
  213. if (!_lpObjectType) {
  214. RRETURN_EXP_IF_ERR(E_OUTOFMEMORY);
  215. }
  216. }
  217. RRETURN(S_OK);
  218. }
  219. STDMETHODIMP
  220. CAccessControlEntry::get_InheritedObjectType(THIS_ BSTR FAR * retval)
  221. {
  222. HRESULT hr = S_OK;
  223. hr = ADsAllocString(_lpInheritedObjectType, retval);
  224. RRETURN_EXP_IF_ERR(hr);
  225. }
  226. STDMETHODIMP
  227. CAccessControlEntry::put_InheritedObjectType(THIS_ BSTR bstrInheritedObjectType)
  228. {
  229. if (_lpInheritedObjectType) {
  230. FreeADsStr(_lpInheritedObjectType);
  231. _lpInheritedObjectType = NULL;
  232. }
  233. if (bstrInheritedObjectType) {
  234. _lpInheritedObjectType = AllocADsStr(bstrInheritedObjectType);
  235. if (!_lpInheritedObjectType) {
  236. RRETURN(E_OUTOFMEMORY);
  237. }
  238. }
  239. RRETURN(S_OK);
  240. }
  241. STDMETHODIMP
  242. CAccessControlEntry::get_Trustee(THIS_ BSTR FAR * retval)
  243. {
  244. HRESULT hr = S_OK;
  245. hr = ADsAllocString(_lpTrustee, retval);
  246. RRETURN_EXP_IF_ERR(hr);
  247. }
  248. STDMETHODIMP
  249. CAccessControlEntry::put_Trustee(THIS_ BSTR bstrTrustee)
  250. {
  251. if (!bstrTrustee) {
  252. RRETURN(E_FAIL);
  253. }
  254. if (_lpTrustee) {
  255. FreeADsStr(_lpTrustee);
  256. }
  257. //
  258. // Since we are changing the trustee, we need to make
  259. // sure the sid value is cleared.
  260. //
  261. if (_dwSidLen) {
  262. _dwSidLen = 0;
  263. }
  264. if (_pSid) {
  265. FreeADsMem(_pSid);
  266. _pSid = NULL;
  267. }
  268. _lpTrustee = AllocADsStr(bstrTrustee);
  269. if (!_lpTrustee) {
  270. RRETURN_EXP_IF_ERR(E_OUTOFMEMORY);
  271. }
  272. RRETURN(S_OK);
  273. }
  274. HRESULT
  275. CopyAccessControlEntry(
  276. IADsAccessControlEntry * pSourceAce,
  277. IADsAccessControlEntry ** ppTargetAce
  278. )
  279. {
  280. IADsAccessControlEntry * pTargetAce = NULL;
  281. IADsAcePrivate *pPrivAceTarget = NULL;
  282. IADsAcePrivate *pPrivAceSource = NULL;
  283. BSTR bstrInheritedObjectTypeClsid = NULL;
  284. BSTR bstrObjectTypeClsid = NULL;
  285. BSTR bstrTrustee = NULL;
  286. HRESULT hr = S_OK;
  287. DWORD dwAceType = 0;
  288. DWORD dwAceFlags = 0;
  289. DWORD dwAccessMask = 0;
  290. DWORD dwFlags = 0;
  291. DWORD dwSidLen = 0;
  292. BOOL fSidValid = FALSE;
  293. PSID pSid = NULL;
  294. hr = pSourceAce->get_AceType((LONG *)&dwAceType);
  295. BAIL_ON_FAILURE(hr);
  296. hr = pSourceAce->get_Trustee(&bstrTrustee);
  297. BAIL_ON_FAILURE(hr);
  298. hr = pSourceAce->get_AceFlags((long *)&dwAceFlags);
  299. BAIL_ON_FAILURE(hr);
  300. hr = pSourceAce->get_AccessMask((long *)&dwAccessMask);
  301. BAIL_ON_FAILURE(hr);
  302. hr = pSourceAce->get_Flags((LONG *)&dwFlags);
  303. BAIL_ON_FAILURE(hr);
  304. hr = pSourceAce->get_ObjectType(&bstrObjectTypeClsid);
  305. BAIL_ON_FAILURE(hr);
  306. hr = pSourceAce->get_InheritedObjectType(&bstrInheritedObjectTypeClsid);
  307. BAIL_ON_FAILURE(hr);
  308. hr = CoCreateInstance(
  309. CLSID_AccessControlEntry,
  310. NULL,
  311. CLSCTX_INPROC_SERVER,
  312. IID_IADsAccessControlEntry,
  313. (void **)&pTargetAce
  314. );
  315. BAIL_ON_FAILURE(hr);
  316. hr = pTargetAce->put_AccessMask(dwAccessMask);
  317. BAIL_ON_FAILURE(hr);
  318. hr = pTargetAce->put_AceFlags(dwAceFlags);
  319. BAIL_ON_FAILURE(hr);
  320. hr = pTargetAce->put_AceType(dwAceType);
  321. BAIL_ON_FAILURE(hr);
  322. hr = pTargetAce->put_Trustee(bstrTrustee);
  323. BAIL_ON_FAILURE(hr);
  324. hr = pTargetAce->put_Flags((LONG)dwFlags);
  325. BAIL_ON_FAILURE(hr);
  326. hr = pTargetAce->put_ObjectType(bstrObjectTypeClsid);
  327. BAIL_ON_FAILURE(hr);
  328. hr = pTargetAce->put_InheritedObjectType(bstrInheritedObjectTypeClsid);
  329. BAIL_ON_FAILURE(hr);
  330. hr = pTargetAce->QueryInterface(
  331. IID_IADsAcePrivate,
  332. (void **) &pPrivAceTarget
  333. );
  334. if (SUCCEEDED(hr)) {
  335. hr = pSourceAce->QueryInterface(
  336. IID_IADsAcePrivate,
  337. (void **) &pPrivAceSource
  338. );
  339. if (SUCCEEDED(hr)) {
  340. hr = pPrivAceSource->isSidValid(&fSidValid);
  341. if (SUCCEEDED(hr) && fSidValid) {
  342. //
  343. // Get the sid and try putting it.
  344. //
  345. hr = pPrivAceSource->getSid(
  346. &pSid,
  347. &dwSidLen
  348. );
  349. if (SUCCEEDED(hr)) {
  350. pPrivAceTarget->putSid(
  351. pSid,
  352. dwSidLen
  353. );
  354. }
  355. }
  356. }
  357. }
  358. *ppTargetAce = pTargetAce;
  359. error:
  360. if (bstrTrustee) {
  361. ADsFreeString(bstrTrustee);
  362. }
  363. if (bstrObjectTypeClsid) {
  364. ADsFreeString(bstrObjectTypeClsid);
  365. }
  366. if (bstrInheritedObjectTypeClsid) {
  367. ADsFreeString(bstrInheritedObjectTypeClsid);
  368. }
  369. if (pPrivAceTarget) {
  370. pPrivAceTarget->Release();
  371. }
  372. if (pPrivAceSource) {
  373. pPrivAceSource->Release();
  374. }
  375. if (pSid) {
  376. FreeADsMem(pSid);
  377. }
  378. RRETURN_EXP_IF_ERR(hr);
  379. }
  380. //
  381. // IADsAcePrivate methods.
  382. //
  383. //+---------------------------------------------------------------------------
  384. // Function: CAccessControlEntry::getSid - IADsAcePrivate method.
  385. //
  386. // Synopsis: Returns the SID associated with the trustee on this ACE
  387. // object assuming there is one that is correct.
  388. //
  389. // Arguments: ppSid - Return pointer to SID.
  390. // pdwLength - Return lenght of the SID.
  391. //
  392. // Returns: S_OK or appropriate failure error code.
  393. //
  394. // Modifies: ppSid and pdwLength
  395. //
  396. //----------------------------------------------------------------------------
  397. STDMETHODIMP
  398. CAccessControlEntry::getSid(
  399. OUT PSID *ppSid,
  400. OUT DWORD *pdwLength
  401. )
  402. {
  403. HRESULT hr = S_OK;
  404. PSID pSid = NULL;
  405. if (!ppSid || !pdwLength) {
  406. BAIL_ON_FAILURE(hr = E_ADS_BAD_PARAMETER);
  407. }
  408. if (!_pSid) {
  409. //
  410. // This means that the there is no usable value for the sid.
  411. //
  412. RRETURN(E_FAIL);
  413. }
  414. pSid = (PSID) AllocADsMem(_dwSidLen);
  415. if (!pSid) {
  416. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  417. }
  418. memcpy(pSid, _pSid, _dwSidLen);
  419. *pdwLength = _dwSidLen;
  420. *ppSid = pSid;
  421. error:
  422. RRETURN(hr);
  423. }
  424. //+---------------------------------------------------------------------------
  425. // Function: CAccessControlEntry::putSid - IADsAcePrivate method.
  426. //
  427. // Synopsis: Updates the SID associated with the trustee on this Ace
  428. // object to the value specified.
  429. //
  430. // Arguments: PSID - Pointer to SID.
  431. // dwLength - Lenght of the SID.
  432. //
  433. // Returns: S_OK on success or E_ADS_BAD_PARAMETER, E_OUTOFMEMORY
  434. // on failure.
  435. //
  436. // Modifies: _pSid and _dwSidLen.
  437. //
  438. //----------------------------------------------------------------------------
  439. STDMETHODIMP
  440. CAccessControlEntry::putSid(
  441. IN PSID pSid,
  442. IN DWORD dwLength
  443. )
  444. {
  445. HRESULT hr = S_OK;
  446. PSID pLocalSid = NULL;
  447. if (dwLength && !pSid) {
  448. BAIL_ON_FAILURE(hr = E_ADS_BAD_PARAMETER);
  449. }
  450. if (_pSid) {
  451. FreeADsMem(_pSid);
  452. _pSid = NULL;
  453. _dwSidLen = 0;
  454. }
  455. if (dwLength) {
  456. //
  457. // pSid also has to be valid at this point
  458. //
  459. _pSid = (PSID) AllocADsMem(dwLength);
  460. if (!_pSid) {
  461. BAIL_ON_FAILURE(hr = E_OUTOFMEMORY);
  462. }
  463. memcpy(_pSid, pSid, dwLength);
  464. _dwSidLen = dwLength;
  465. }
  466. error:
  467. RRETURN(hr);
  468. }
  469. //+---------------------------------------------------------------------------
  470. // Function: CAccessControlEntry::isSidValid - IADsAcePrivate method.
  471. //
  472. // Synopsis: Returns status of the sid associated with this ACE's trustee.
  473. //
  474. // Arguments: pfSidValid - Return value.
  475. //
  476. // Returns: S_OK or E_ADS_BAD_PARAMETER.
  477. //
  478. // Modifies: pfSidValid set to TRUE or FALSE as appropriate.
  479. //
  480. //----------------------------------------------------------------------------
  481. STDMETHODIMP
  482. CAccessControlEntry::isSidValid(
  483. OUT BOOL *pfSidValid
  484. )
  485. {
  486. if (!pfSidValid) {
  487. RRETURN(E_ADS_BAD_PARAMETER);
  488. }
  489. //
  490. // If the Sid length is zero, then there is no sid for trustee.
  491. //
  492. *pfSidValid = (_dwSidLen != 0);
  493. RRETURN(S_OK);
  494. }
  495.