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.

1561 lines
48 KiB

  1. //*************************************************************
  2. //
  3. // Create namespace classes
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1995
  7. // All rights reserved
  8. //
  9. // History: 9-Sep-99 SitaramR Created
  10. //
  11. //*************************************************************
  12. #include <windows.h>
  13. #include <wchar.h>
  14. #include <ole2.h>
  15. #include <initguid.h>
  16. #include <wbemcli.h>
  17. #define SECURITY_WIN32
  18. #include <security.h>
  19. #include <aclapi.h>
  20. #include <seopaque.h>
  21. #include <ntdsapi.h>
  22. #include <winldap.h>
  23. #include <ntldap.h>
  24. #include <dsgetdc.h>
  25. #include <lm.h>
  26. #include "smartptr.h"
  27. #include "RsopInc.h"
  28. #include "rsopsec.h"
  29. #include "rsoputil.h"
  30. #include "rsopdbg.h"
  31. #include "stdio.h"
  32. #include "wbemtime.h"
  33. #include <strsafe.h>
  34. #define ARRAYSIZE(a) (sizeof(a)/sizeof(a[0]))
  35. BOOL PrintToString( XPtrST<WCHAR>& xwszValue, WCHAR *pwszString,
  36. WCHAR *pwszParam1, WCHAR *pwszParam2,
  37. DWORD dwParam3 );
  38. //
  39. // b7b1b3dd-ab09-4242-9e30-9980e5d322f7
  40. //
  41. const GUID guidProperty = {0xb7b1b3dd, 0xab09, 0x4242, 0x9e, 0x30, 0x99, 0x80, 0xe5, 0xd3, 0x22, 0xf7};
  42. DWORD
  43. RSoPBuildPrivileges( PSECURITY_DESCRIPTOR pSD, PSECURITY_DESCRIPTOR pAbsoluteSD, LPWSTR*, DWORD );
  44. LPWSTR
  45. GetDomainName();
  46. DWORD
  47. MakeUserName( LPWSTR szDomain, LPWSTR szUser, LPWSTR* pszUserName );
  48. //*************************************************************
  49. //
  50. // CreateNameSpace
  51. //
  52. // Purpose: Creates a new namespace
  53. //
  54. // Parameters: pwszNameSpace - Namespace to create
  55. // pwszParentNS - Parent namespace in which to create pwszNameSpace
  56. //
  57. // pWbemLocator - Wbem locator
  58. //
  59. // Returns: True if successful, false otherwise
  60. //
  61. //*************************************************************
  62. HRESULT
  63. CreateNameSpace( WCHAR *pwszNameSpace, WCHAR *pwszParentNS, IWbemLocator *pWbemLocator )
  64. {
  65. IWbemClassObject *pNSClass = NULL;
  66. IWbemClassObject *pNSInstance = NULL;
  67. IWbemServices *pWbemServices = NULL;
  68. XBStr xParentNameSpace( pwszParentNS );
  69. if ( !xParentNameSpace )
  70. {
  71. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("LogRegistryRsopData: Unable to allocate memory" ));
  72. return E_OUTOFMEMORY;
  73. }
  74. HRESULT hr = pWbemLocator->ConnectServer( xParentNameSpace,
  75. NULL,
  76. NULL,
  77. 0L,
  78. 0L,
  79. NULL,
  80. NULL,
  81. &pWbemServices );
  82. if ( FAILED(hr) )
  83. {
  84. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace::ConnectServer failed with 0x%x" ), hr );
  85. return hr;
  86. }
  87. XInterface<IWbemServices> xWbemServices( pWbemServices );
  88. XBStr xbstrNS( L"__Namespace" );
  89. if ( !xbstrNS )
  90. {
  91. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace::Failed to allocated memory" ));
  92. return E_OUTOFMEMORY;
  93. }
  94. hr = pWbemServices->GetObject( xbstrNS,
  95. 0, NULL, &pNSClass, NULL );
  96. if ( FAILED(hr) )
  97. {
  98. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace::GetObject failed with 0x%x" ), hr );
  99. return hr;
  100. }
  101. XInterface<IWbemClassObject> xNSClass( pNSClass );
  102. hr = pNSClass->SpawnInstance( 0, &pNSInstance );
  103. if ( FAILED(hr) )
  104. {
  105. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace: SpawnInstance failed with 0x%x" ), hr );
  106. return hr;
  107. }
  108. XInterface<IWbemClassObject> xNSInstance( pNSInstance );
  109. XBStr xbstrName( L"Name" );
  110. if ( !xbstrName )
  111. {
  112. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace::Failed to allocated memory" ));
  113. return E_OUTOFMEMORY;
  114. }
  115. XBStr xbstrNameSpace( pwszNameSpace );
  116. if ( !xbstrNameSpace )
  117. {
  118. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace::Failed to allocated memory" ));
  119. return E_OUTOFMEMORY;
  120. }
  121. VARIANT var;
  122. var.vt = VT_BSTR;
  123. var.bstrVal = xbstrNameSpace;
  124. hr = pNSInstance->Put( xbstrName, 0, &var, 0 );
  125. if ( FAILED(hr) )
  126. {
  127. dbg.Msg(DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace: Put failed with 0x%x" ), hr );
  128. return hr;
  129. }
  130. hr = pWbemServices->PutInstance( pNSInstance, WBEM_FLAG_CREATE_ONLY, NULL, NULL );
  131. if ( FAILED(hr) )
  132. {
  133. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateNameSpace: PutInstance failed with 0x%x" ), hr );
  134. return hr;
  135. }
  136. return hr;
  137. }
  138. //*************************************************************
  139. //
  140. // Function: SetupCreationTimeAndCommit
  141. //
  142. // Purpose: Connects to a namespace where it expects to
  143. // find class RSOP_Session as defined in rsop.mof.
  144. // It then instantiates the class and sets the
  145. // data member 'creationTime' to the current
  146. // date and time.
  147. //
  148. // Parameters: pWbemLocator - Pointer to IWbemLocator used to
  149. // connect to the namespace.
  150. // wszNamespace - Name of the Namespace to connect.
  151. //
  152. // Returns: On success, it returns S_OK.
  153. // On failure, it returns an HRESULT error code.
  154. //
  155. // History: 12/07/99 - LeonardM - Created.
  156. //
  157. //*************************************************************
  158. HRESULT SetupCreationTimeAndCommit(IWbemLocator* pWbemLocator, LPWSTR wszNamespace)
  159. {
  160. //
  161. // Check arguments
  162. //
  163. if(!pWbemLocator || !wszNamespace || (wcscmp(wszNamespace, L"") == 0))
  164. {
  165. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function called with invalid argument(s)."));
  166. return WBEM_E_INVALID_PARAMETER;
  167. }
  168. //
  169. // Connect to the namespace
  170. //
  171. XBStr xbstrNamespace = wszNamespace;
  172. if(!xbstrNamespace)
  173. {
  174. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  175. return E_OUTOFMEMORY;
  176. }
  177. XInterface<IWbemServices>xpNamespace;
  178. HRESULT hr = pWbemLocator->ConnectServer( xbstrNamespace,
  179. NULL,
  180. NULL,
  181. NULL,
  182. 0,
  183. NULL,
  184. NULL,
  185. &xpNamespace);
  186. if(FAILED(hr))
  187. {
  188. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: ConnectServer failed. hr=0x%08X"), hr);
  189. return hr;
  190. }
  191. VARIANT var;
  192. VariantInit(&var);
  193. //
  194. // Get class RSOP_Session
  195. //
  196. XBStr xbstrClassName = L"RSOP_Session";
  197. if (!xbstrClassName)
  198. {
  199. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  200. return E_OUTOFMEMORY;
  201. }
  202. XInterface<IWbemClassObject>xpClass;
  203. hr = xpNamespace->GetObject(xbstrClassName, 0, NULL, &xpClass, NULL);
  204. if(FAILED(hr))
  205. {
  206. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: GetObject failed. hr=0x%08X"), hr);
  207. return hr;
  208. }
  209. //
  210. // Spawn an instance of class RSOP_Session
  211. //
  212. XBStr xbstrInstancePath = L"RSOP_Session.id=\"Session1\"";
  213. if(!xbstrInstancePath)
  214. {
  215. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CSessionLogger::Log: Failed to allocate memory."));
  216. return FALSE;
  217. }
  218. XInterface<IWbemClassObject>xpInstance;
  219. hr = xpNamespace->GetObject(xbstrInstancePath, 0, NULL, &xpInstance, NULL);
  220. if(FAILED(hr))
  221. {
  222. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("SetupCreationTimeAndCommit: GetObject failed. trying to spawn instance. hr=0x%08X"), hr);
  223. hr = xpClass->SpawnInstance(0, &xpInstance);
  224. }
  225. if(FAILED(hr))
  226. {
  227. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: SpawnInstance failed. hr=0x%08X"), hr);
  228. return hr;
  229. }
  230. //
  231. // Set the 'id' data member of class RSOP_Session
  232. //
  233. XBStr xbstrPropertyName;
  234. XBStr xbstrPropertyValue;
  235. xbstrPropertyName = L"id";
  236. if(!xbstrPropertyName)
  237. {
  238. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  239. return E_OUTOFMEMORY;
  240. }
  241. xbstrPropertyValue = L"Session1";
  242. if(!xbstrPropertyValue)
  243. {
  244. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  245. return E_OUTOFMEMORY;
  246. }
  247. var.vt = VT_BSTR;
  248. var.bstrVal = xbstrPropertyValue;
  249. hr = xpInstance->Put(xbstrPropertyName, 0, &var, 0);
  250. if(FAILED(hr))
  251. {
  252. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Put failed. hr=0x%08X"), hr);
  253. return hr;
  254. }
  255. //
  256. // Set the 'creationTime' data member of class RSOP_Session
  257. //
  258. xbstrPropertyName = L"creationTime";
  259. if(!xbstrPropertyName)
  260. {
  261. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  262. return E_OUTOFMEMORY;
  263. }
  264. hr = GetCurrentWbemTime(xbstrPropertyValue);
  265. if(FAILED(hr))
  266. {
  267. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: GetCurrentWbemTime. hr=0x%08X"), hr);
  268. return hr;
  269. }
  270. var.vt = VT_BSTR;
  271. var.bstrVal = xbstrPropertyValue;
  272. hr = xpInstance->Put(xbstrPropertyName, 0, &var, 0);
  273. if(FAILED(hr))
  274. {
  275. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Put failed. hr=0x%08X"), hr);
  276. return hr;
  277. }
  278. //
  279. // Set the 'ttlMinutes' data member of class RSOP_Session
  280. //
  281. xbstrPropertyName = L"ttlMinutes";
  282. if(!xbstrPropertyName)
  283. {
  284. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Function failed to allocate memory."));
  285. return E_OUTOFMEMORY;
  286. }
  287. var.vt = VT_I4;
  288. var.lVal = DEFAULT_NAMESPACE_TTL_MINUTES;
  289. hr = xpInstance->Put(xbstrPropertyName, 0, &var, 0);
  290. if(FAILED(hr))
  291. {
  292. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: Put failed. hr=0x%08X"), hr);
  293. return hr;
  294. }
  295. // if any more data integrity checks needs to be done
  296. // it can be done at this point
  297. //
  298. // Put instance of class RSOP_Session
  299. //
  300. hr = xpNamespace->PutInstance(xpInstance, WBEM_FLAG_CREATE_OR_UPDATE, NULL, NULL);
  301. if(FAILED(hr))
  302. {
  303. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupCreationTimeAndCommit: PutInstance failed. hr=0x%08X"), hr);
  304. return hr;
  305. }
  306. return S_OK;
  307. }
  308. //*************************************************************
  309. //
  310. // SetupNameSpaceSecurity
  311. //
  312. // Purpose: Sets namespace security.
  313. //
  314. // Parameters: szNamespace - New namespace returned here
  315. // pSD - source security descriptor
  316. // pWbemLocator - Wbem locator
  317. //
  318. // Returns: True if successful, false otherwise
  319. //
  320. //*************************************************************
  321. HRESULT
  322. SetNameSpaceSecurity( LPCWSTR szNamespace,
  323. PSECURITY_DESCRIPTOR pSD,
  324. IWbemLocator* pWbemLocator)
  325. {
  326. XBStr xNameSpace( (LPWSTR) szNamespace );
  327. if ( !xNameSpace )
  328. {
  329. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNameSpaceSecurity: Unable to allocate memory" ));
  330. return E_FAIL;
  331. }
  332. XInterface<IWbemServices> xptrServices;
  333. HRESULT hr = pWbemLocator->ConnectServer( xNameSpace,
  334. 0,
  335. 0,
  336. 0L,
  337. 0L,
  338. 0,
  339. 0,
  340. &xptrServices );
  341. if ( FAILED(hr) )
  342. {
  343. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNameSpaceSecurity::ConnectServer failed with 0x%x" ), hr );
  344. return hr;
  345. }
  346. return SetNamespaceSD( (SECURITY_DESCRIPTOR*)pSD, xptrServices);
  347. }
  348. //*************************************************************
  349. //
  350. // GetNameSpaceSecurity
  351. //
  352. // Purpose: Sets namespace security.
  353. //
  354. // Parameters: szNamespace - New namespace returned here
  355. // pSD - source security descriptor
  356. // pWbemLocator - Wbem locator
  357. //
  358. // Returns: True if successful, false otherwise
  359. //
  360. //*************************************************************
  361. HRESULT
  362. GetNameSpaceSecurity( LPCWSTR szNamespace,
  363. PSECURITY_DESCRIPTOR *ppSD,
  364. IWbemLocator* pWbemLocator)
  365. {
  366. XBStr xNameSpace( (LPWSTR) szNamespace );
  367. if ( !xNameSpace )
  368. {
  369. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetNameSpaceSecurity: Unable to allocate memory" ));
  370. return E_FAIL;
  371. }
  372. XInterface<IWbemServices> xptrServices;
  373. HRESULT hr = pWbemLocator->ConnectServer( xNameSpace,
  374. 0,
  375. 0,
  376. 0L,
  377. 0L,
  378. 0,
  379. 0,
  380. &xptrServices );
  381. if ( FAILED(hr) )
  382. {
  383. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetNameSpaceSecurity::ConnectServer failed with 0x%x" ), hr );
  384. return hr;
  385. }
  386. return GetNamespaceSD(xptrServices, (SECURITY_DESCRIPTOR **)ppSD);
  387. }
  388. //*************************************************************
  389. //
  390. // CopyNameSpaceSecurity
  391. //
  392. // Purpose: Copies namespace security.
  393. //
  394. // Parameters: pwszSrcNameSpace - Source namespace
  395. // pwszDstNameSpace - Dest namespace
  396. // pWbemLocator - Wbem locator
  397. //
  398. // Returns: HRESULT
  399. //
  400. //*************************************************************
  401. HRESULT CopyNameSpaceSecurity(LPWSTR pwszSrcNameSpace, LPWSTR pwszDstNameSpace, IWbemLocator *pWbemLocator )
  402. {
  403. XHandle xhThreadToken;
  404. HRESULT hr = S_OK;
  405. //
  406. // There is a bug in WMI which destroys the current thread token
  407. // if connectserver is called to the local machine with impersonation.
  408. // The following SetThreadToken needs to be removed once WMI bug 454721 is fixed.
  409. //
  410. if (!OpenThreadToken (GetCurrentThread(), TOKEN_IMPERSONATE | TOKEN_READ,
  411. TRUE, &xhThreadToken)) {
  412. hr = HRESULT_FROM_WIN32(GetLastError());
  413. if(hr != HRESULT_FROM_WIN32(ERROR_NO_TOKEN))
  414. {
  415. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: Openthreadtoken failed with error 0x%x."), hr);
  416. return hr;
  417. }
  418. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CopyNameSpaceSecurity: Openthreadtoken failed with error 0x%x."), hr);
  419. }
  420. // internal function. arg checks not needed
  421. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CopyNameSpaceSecurity: Copying Sec Desc from <%s> -> <%s>."),
  422. pwszSrcNameSpace, pwszDstNameSpace );
  423. //
  424. // Copy to a BStr
  425. //
  426. XBStr xSrcNameSpace(pwszSrcNameSpace);
  427. if (!xSrcNameSpace) {
  428. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: Function failed to allocate memory."));
  429. return E_OUTOFMEMORY;
  430. }
  431. XBStr xDstNameSpace(pwszDstNameSpace);
  432. if (!xDstNameSpace) {
  433. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: Function failed to allocate memory."));
  434. return E_OUTOFMEMORY;
  435. }
  436. //
  437. // Get the Source WBem Service
  438. //
  439. XInterface<IWbemServices> xpSrcSvc;
  440. hr = pWbemLocator->ConnectServer( xSrcNameSpace,
  441. NULL,
  442. NULL,
  443. NULL,
  444. 0,
  445. NULL,
  446. NULL,
  447. &xpSrcSvc);
  448. if(!SetThreadToken(NULL, xhThreadToken))
  449. {
  450. hr = HRESULT_FROM_WIN32(GetLastError());
  451. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: SetThreadToken failed for src. hr=0x%08X"), hr);
  452. return hr;
  453. }
  454. if(FAILED(hr))
  455. {
  456. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: ConnectServer failed for src. hr=0x%08X"), hr);
  457. return hr;
  458. }
  459. //
  460. // Self relative SD on the Source Name Space
  461. //
  462. XPtrLF<SECURITY_DESCRIPTOR> xpSelfRelativeSD;
  463. hr = GetNamespaceSD(xpSrcSvc, &xpSelfRelativeSD);
  464. if(!SetThreadToken(NULL, xhThreadToken))
  465. {
  466. hr = HRESULT_FROM_WIN32(GetLastError());
  467. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: SetThreadToken failed for src. hr=0x%08X"), hr);
  468. return hr;
  469. }
  470. if(FAILED(hr))
  471. {
  472. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: GetNameSpaceSD failed for src. hr=0x%08X"), hr);
  473. return hr;
  474. }
  475. //
  476. // Get the Dest WBem Service
  477. //
  478. XInterface<IWbemServices> xpDstSvc;
  479. hr = pWbemLocator->ConnectServer( xDstNameSpace,
  480. NULL,
  481. NULL,
  482. NULL,
  483. 0,
  484. NULL,
  485. NULL,
  486. &xpDstSvc);
  487. if(!SetThreadToken(NULL, xhThreadToken))
  488. {
  489. hr = HRESULT_FROM_WIN32(GetLastError());
  490. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: SetThreadToken failed for src. hr=0x%08X"), hr);
  491. return hr;
  492. }
  493. if(FAILED(hr))
  494. {
  495. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: ConnectServer failed for Dst. hr=0x%08X"), hr);
  496. return hr;
  497. }
  498. //
  499. // Set the SD already got on the Destination
  500. //
  501. hr = SetNamespaceSD( xpSelfRelativeSD, xpDstSvc);
  502. if(!SetThreadToken(NULL, xhThreadToken))
  503. {
  504. hr = HRESULT_FROM_WIN32(GetLastError());
  505. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CopyNameSpaceSecurity: SetThreadToken failed for src. hr=0x%08X"), hr);
  506. return hr;
  507. }
  508. if(FAILED(hr))
  509. {
  510. dbg.Msg( DEBUG_MESSAGE_VERBOSE, L"CopyNameSpaceSecurity: SetNamespaceSD failed on Dst, 0x%08X", hr );
  511. return hr;
  512. }
  513. // All Done
  514. return S_OK;
  515. }
  516. //*************************************************************
  517. //
  518. // ProviderDeleteRsopNameSpace
  519. //
  520. // Purpose: WMI doesn't provide a mechanism to allow a user to delete a namespace
  521. // unless it has write permissions on the parent
  522. //
  523. // Parameters: pwszNameSpace - Namespace to be deleted
  524. // hToken - Token of the calling user.
  525. // szSidString - String form of the calling user's sid.
  526. // dwFlags - Flag to indicate planning mode or diagnostic mode
  527. //
  528. // Returns: S_OK if successful, HRESULT o/w
  529. //
  530. //*************************************************************
  531. HRESULT ProviderDeleteRsopNameSpace( IWbemLocator *pWbemLocator, LPWSTR szNameSpace, HANDLE hToken, LPWSTR szSidString, DWORD dwFlags)
  532. {
  533. BOOL bDelete = FALSE;
  534. BOOL bFound = FALSE;
  535. HRESULT hr = S_OK;
  536. LPWSTR pStr = szNameSpace;
  537. //
  538. // Make sure that the namespace is under root\rsop
  539. //
  540. for ( ;*pStr; pStr++) {
  541. if (_wcsnicmp(pStr, RSOP_NS_ROOT_CHK, wcslen(RSOP_NS_ROOT_CHK)) == 0) {
  542. bFound = TRUE;
  543. break;
  544. }
  545. }
  546. if (!bFound) {
  547. dbg.Msg( DEBUG_MESSAGE_WARNING, L"ProviderDeleteRsopNameSpace: namespace is not under root\\rsop" );
  548. return E_INVALIDARG;
  549. }
  550. if ( dwFlags & SETUP_NS_SM && IsInteractiveNameSpace(szNameSpace, szSidString)) {
  551. dbg.Msg( DEBUG_MESSAGE_VERBOSE, L"ProviderDeleteRsopNameSpace: interactive namespace for the user." );
  552. bDelete = TRUE;
  553. }
  554. else {
  555. //
  556. // if it is not interactive namespace check access
  557. //
  558. XPtrLF<SECURITY_DESCRIPTOR> xsd;
  559. hr = GetNameSpaceSecurity(szNameSpace, (PSECURITY_DESCRIPTOR *)&xsd, pWbemLocator);
  560. if (FAILED(hr)) {
  561. dbg.Msg( DEBUG_MESSAGE_WARNING, L"ProviderDeleteRsopNameSpace: GetNameSpaceSecurity failed with error 0x%x", hr );
  562. return hr;
  563. }
  564. GENERIC_MAPPING map;
  565. PRIVILEGE_SET ps[3];
  566. DWORD dwSize = 3 * sizeof(PRIVILEGE_SET);
  567. BOOL bResult;
  568. DWORD dwGranted = 0;
  569. map.GenericRead = WMI_GENERIC_READ;
  570. map.GenericWrite = WMI_GENERIC_WRITE;
  571. map.GenericExecute = WMI_GENERIC_EXECUTE;
  572. map.GenericAll = WMI_GENERIC_ALL;
  573. if (!AccessCheck(xsd, hToken, RSOP_ALL_PERMS, &map, ps, &dwSize, &dwGranted, &bResult)) {
  574. hr = HRESULT_FROM_WIN32(GetLastError());
  575. dbg.Msg( DEBUG_MESSAGE_WARNING, L"ProviderDeleteRsopNameSpace: AccessCheck failed with error 0x%x", hr );
  576. return hr;
  577. }
  578. if(bResult && dwGranted) {
  579. dbg.Msg( DEBUG_MESSAGE_VERBOSE, L"ProviderDeleteRsopNameSpace: User has full rights on the child namespace");
  580. bDelete = TRUE;
  581. }
  582. else {
  583. dbg.Msg( DEBUG_MESSAGE_VERBOSE, L"ProviderDeleteRsopNameSpace: This user is not granted access on the namespace", hr );
  584. }
  585. }
  586. if (bDelete) {
  587. hr = DeleteRsopNameSpace(szNameSpace, pWbemLocator);
  588. }
  589. else {
  590. hr = WBEM_E_ACCESS_DENIED;
  591. }
  592. return hr;
  593. }
  594. //*************************************************************
  595. //
  596. // SetupNewNameSpace
  597. //
  598. // Purpose: Creates a new temp namespace and two child namespaces, User and Computer.
  599. // It also copies all the class definitions
  600. // Additionally, it calls SetupCreationTimeAndCommit
  601. // which in turn instantiates RSOP_Session and updates the
  602. // data member 'creationTime' with the current time.
  603. //
  604. // Parameters: pwszNameSpace - New namespace returned here (This is allocated here)
  605. // szRemoteComputer - Remote Computer under which this name space has to be
  606. // created.
  607. // szUserSid - UserSid. Only relevant in Diagnostic mode
  608. // pSid - Sid of the calling User
  609. // pWbemLocator - Wbem locator
  610. // dwFlags - Flag to indicate planning mode or diagnostic mode
  611. // dwExtendedInfo - The extended info to modify appropriately
  612. //
  613. // Returns: True if successful, false otherwise
  614. //
  615. //
  616. // Usage:
  617. // In Diagnostic mode, we copy instances. In planning mode we just copy Classes
  618. //*************************************************************
  619. HRESULT SetupNewNameSpace(
  620. LPWSTR *pwszOutNameSpace,
  621. LPWSTR szRemoteComputer,
  622. LPWSTR szUserSid,
  623. PSID pSid,
  624. IWbemLocator *pWbemLocator,
  625. DWORD dwFlags,
  626. DWORD *pdwExtendedInfo)
  627. {
  628. GUID guid;
  629. XPtrLF<WCHAR> xwszRelNameSpace;
  630. XPtrLF<WCHAR> xwszRootNameSpace;
  631. XPtrLF<WCHAR> xwszSrcNameSpace;
  632. DWORD dwSrcNSLen;
  633. XPtrLF<WCHAR> xwszNameSpace;
  634. LPWSTR szComputerLocal;
  635. HRESULT hr = S_OK, hrUser = S_OK, hrMachine = S_OK;
  636. if ((dwFlags & SETUP_NS_SM_INTERACTIVE) ||
  637. (dwFlags & SETUP_NS_SM_NO_USER) ||
  638. (dwFlags & SETUP_NS_SM_NO_COMPUTER)) {
  639. if (!(dwFlags & SETUP_NS_SM)) {
  640. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::invalid flag parameters"));
  641. return E_INVALIDARG;
  642. }
  643. }
  644. XPtrLF<SECURITY_DESCRIPTOR> xsd;
  645. SECURITY_ATTRIBUTES sa;
  646. CSecDesc Csd;
  647. *pwszOutNameSpace = NULL;
  648. Csd.AddLocalSystem(RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  649. Csd.AddAdministrators(RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  650. if (dwFlags & SETUP_NS_SM)
  651. {
  652. Csd.AddNetworkService(RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  653. }
  654. if (dwFlags & SETUP_NS_SM_INTERACTIVE) {
  655. Csd.AddSid(pSid, RSOP_READ_PERMS, CONTAINER_INHERIT_ACE);
  656. }
  657. else {
  658. Csd.AddSid(pSid, RSOP_ALL_PERMS, CONTAINER_INHERIT_ACE);
  659. }
  660. Csd.AddAdministratorsAsOwner();
  661. Csd.AddAdministratorsAsGroup();
  662. xsd = Csd.MakeSelfRelativeSD();
  663. if (!xsd) {
  664. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::Makeselfrelativesd failed with %d"), GetLastError());
  665. return HRESULT_FROM_WIN32(GetLastError());
  666. }
  667. //
  668. // ignore inheritted perms..
  669. //
  670. if (!SetSecurityDescriptorControl( (SECURITY_DESCRIPTOR *)xsd, SE_DACL_PROTECTED, SE_DACL_PROTECTED )) {
  671. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::SetSecurityDescriptorControl failed with %d"), GetLastError());
  672. return HRESULT_FROM_WIN32(GetLastError());
  673. }
  674. //
  675. // Initialise the out params
  676. //
  677. if ((dwFlags & SETUP_NS_SM) && (!szUserSid))
  678. return E_INVALIDARG;
  679. //
  680. // Calculate the length required for the name spaces
  681. //
  682. DWORD dwLenNS = RSOP_NS_TEMP_LEN;
  683. if ((szRemoteComputer) && (*szRemoteComputer)) {
  684. dwLenNS += lstrlen(szRemoteComputer);
  685. szComputerLocal = szRemoteComputer;
  686. }
  687. else {
  688. szComputerLocal = L".";
  689. }
  690. //
  691. // guid for the Name Space
  692. //
  693. hr = CoCreateGuid( &guid );
  694. if ( FAILED(hr) ) {
  695. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CoCreateGuid failed with 0x%x"), hr );
  696. return hr;
  697. }
  698. //
  699. // Allocate the memory and initialise
  700. //
  701. DWORD dwRootNSLength = lstrlen(szComputerLocal) + RSOP_NS_ROOT_LEN;
  702. xwszRootNameSpace = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * (dwRootNSLength));
  703. if (!xwszRootNameSpace) {
  704. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::Not enough Space. Error - 0x%x"), GetLastError() );
  705. return HRESULT_FROM_WIN32(GetLastError());
  706. }
  707. // allocating max needed
  708. dwSrcNSLen = ( RSOP_NS_ROOT_LEN + lstrlen(szUserSid) + RSOP_NS_MAX_OFFSET_LEN + 10);
  709. if (dwFlags & SETUP_NS_SM)
  710. dwSrcNSLen += lstrlen(szUserSid);
  711. xwszSrcNameSpace = (LPTSTR)LocalAlloc(LPTR, sizeof(WCHAR) * dwSrcNSLen);
  712. if (!xwszSrcNameSpace) {
  713. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::Not enough Space. Error - 0x%x"), GetLastError() );
  714. return HRESULT_FROM_WIN32(GetLastError());
  715. }
  716. hr = StringCchPrintf(xwszRootNameSpace, dwRootNSLength, RSOP_NS_REMOTE_ROOT_FMT, szComputerLocal);
  717. if(FAILED(hr))
  718. return hr;
  719. hr = StringCchCopy(xwszSrcNameSpace, dwSrcNSLen, RSOP_NS_DIAG_ROOT);
  720. if(FAILED(hr))
  721. return hr;
  722. LPTSTR lpEnd = xwszSrcNameSpace + lstrlen(xwszSrcNameSpace);
  723. //
  724. // Create a new Name Space under the root
  725. //
  726. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("SetupNewNameSpace: Creating new NameSpace <%s>"), xwszRootNameSpace);
  727. DWORD dwRelNSLength = 1 + MAX(lstrlen(szUserSid), RSOP_NS_TEMP_LEN);
  728. xwszRelNameSpace = (LPWSTR)LocalAlloc(LPTR, ( dwRelNSLength ) * sizeof(WCHAR));
  729. if (!xwszRelNameSpace) {
  730. hr = E_OUTOFMEMORY;
  731. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::AllocMem failed with 0x%x"), hr );
  732. return hr;
  733. }
  734. if (dwFlags & SETUP_NS_SM_INTERACTIVE) {
  735. XPtrLF<WCHAR> xszWmiName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR) * (lstrlen(szUserSid) + 1));
  736. if (!xszWmiName) {
  737. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CreateNameSpace couldn't allocate memory with error %d"), GetLastError() );
  738. return E_OUTOFMEMORY;
  739. }
  740. ConvertSidToWMIName(szUserSid, xszWmiName);
  741. hr = StringCchPrintf( xwszRelNameSpace,
  742. dwRelNSLength,
  743. L"%s%s",
  744. RSOP_NS_TEMP_PREFIX,
  745. (LPWSTR) xszWmiName);
  746. }
  747. else {
  748. hr = StringCchPrintf( xwszRelNameSpace,
  749. dwRelNSLength,
  750. L"%s%08lX_%04X_%04X_%02X%02X_%02X%02X%02X%02X%02X%02X",
  751. RSOP_NS_TEMP_PREFIX,
  752. guid.Data1,
  753. guid.Data2,
  754. guid.Data3,
  755. guid.Data4[0], guid.Data4[1],
  756. guid.Data4[2], guid.Data4[3],
  757. guid.Data4[4], guid.Data4[5],
  758. guid.Data4[6], guid.Data4[7] );
  759. }
  760. if(FAILED(hr))
  761. return hr;
  762. hr = CreateAndCopyNameSpace(pWbemLocator, xwszSrcNameSpace, xwszRootNameSpace,
  763. xwszRelNameSpace, 0, xsd, &xwszNameSpace);
  764. if ( FAILED(hr) ) {
  765. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CreateNameSpace failed with 0x%x"), hr );
  766. return hr;
  767. }
  768. //
  769. // if it has come till here, the assumption is that we
  770. // could create the namespace in the context that we are running in
  771. //
  772. // In diagnostic interactive mode we have already made sure that the sid is the
  773. // same as the user.
  774. //
  775. if (pdwExtendedInfo) {
  776. *pdwExtendedInfo &= ~RSOP_USER_ACCESS_DENIED;
  777. *pdwExtendedInfo &= ~RSOP_COMPUTER_ACCESS_DENIED;
  778. }
  779. hr = StringCchCat(lpEnd, dwSrcNSLen - (lpEnd - xwszSrcNameSpace), L"\\");
  780. if(FAILED(hr))
  781. return hr;
  782. lpEnd++;
  783. DWORD dwCopyFlags = 0;
  784. if (dwFlags & SETUP_NS_PM) {
  785. //
  786. // if it is planning mode, copy classes from RSOP_NS_USER
  787. //
  788. hr = StringCchCopy(lpEnd, dwSrcNSLen - (lpEnd - xwszSrcNameSpace), RSOP_NS_USER_OFFSET);
  789. if(FAILED(hr))
  790. return hr;
  791. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES;
  792. }
  793. else {
  794. if (dwFlags & SETUP_NS_SM_NO_USER) {
  795. //
  796. // If no user copy classes from root\rsop\user itself
  797. //
  798. hr = StringCchCopy(lpEnd, dwSrcNSLen - (lpEnd - xwszSrcNameSpace), RSOP_NS_SM_USER_OFFSET);
  799. if(FAILED(hr))
  800. return hr;
  801. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES;
  802. }
  803. else {
  804. //
  805. // if it is diagnostic mode, copy classes and instances from RSOP_NS_USER_SId
  806. //
  807. XPtrLF<WCHAR> xszWmiName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(szUserSid)+1));
  808. if (!xszWmiName) {
  809. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CreateNameSpace couldn't allocate memory with error %d"), GetLastError() );
  810. return FALSE;
  811. }
  812. ConvertSidToWMIName(szUserSid, xszWmiName);
  813. hr = StringCchPrintf(lpEnd, dwSrcNSLen - (lpEnd - xwszSrcNameSpace), RSOP_NS_DIAG_USER_OFFSET_FMT, (LPWSTR) xszWmiName);
  814. if(FAILED(hr))
  815. return hr;
  816. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES | NEW_NS_FLAGS_COPY_INSTS;
  817. }
  818. }
  819. hrUser = CreateAndCopyNameSpace(pWbemLocator, xwszSrcNameSpace, xwszNameSpace,
  820. RSOP_NS_SM_USER_OFFSET, dwCopyFlags,
  821. xsd, NULL);
  822. if ( FAILED(hrUser) ) {
  823. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CreateNameSpace failed with 0x%x"), hrUser );
  824. }
  825. //
  826. // for machine only the flags are different. source namespaces are the same
  827. //
  828. if (dwFlags & SETUP_NS_PM) {
  829. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES;
  830. }
  831. else {
  832. if (dwFlags & SETUP_NS_SM_NO_COMPUTER)
  833. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES;
  834. else
  835. dwCopyFlags = NEW_NS_FLAGS_COPY_CLASSES | NEW_NS_FLAGS_COPY_INSTS;
  836. }
  837. hr = StringCchCopy(lpEnd, dwSrcNSLen - (lpEnd - xwszSrcNameSpace), RSOP_NS_MACHINE_OFFSET);
  838. if(FAILED(hr))
  839. return hr;
  840. hrMachine = CreateAndCopyNameSpace(pWbemLocator, xwszSrcNameSpace, xwszNameSpace,
  841. RSOP_NS_DIAG_MACHINE_OFFSET, dwCopyFlags,
  842. xsd, NULL);
  843. if ( FAILED(hrMachine) ) {
  844. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::CreateNameSpace failed with 0x%x"), hrMachine );
  845. }
  846. if (FAILED(hrUser)) {
  847. if (pdwExtendedInfo) {
  848. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::User part of rsop failed with 0x%x"), hrUser );
  849. *pdwExtendedInfo |= RSOP_USER_ACCESS_DENIED;
  850. }
  851. }
  852. if (FAILED(hrMachine)) {
  853. if (pdwExtendedInfo) {
  854. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::computer part of rsop failed with 0x%x"), hrMachine );
  855. *pdwExtendedInfo |= RSOP_COMPUTER_ACCESS_DENIED;
  856. }
  857. }
  858. if (FAILED(hrUser)) {
  859. return hrUser;
  860. }
  861. if (FAILED(hrMachine)) {
  862. return hrMachine;
  863. }
  864. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("SetupNewNameSpace: Returning Successfully"));
  865. //
  866. // Now handover the ownership to the caller.
  867. //
  868. *pwszOutNameSpace = xwszNameSpace.Acquire();
  869. return S_OK;
  870. }
  871. //*************************************************************
  872. // ConvertSidToWMIName
  873. //
  874. // WMI doesn't like '-' in names. Connverting - to '_' blindly
  875. //*************************************************************
  876. void ConvertSidToWMIName(LPTSTR lpSid, LPTSTR lpWmiName)
  877. {
  878. for (;(*lpSid); lpSid++, lpWmiName++) {
  879. if (*lpSid == L'-')
  880. *lpWmiName = L'_';
  881. else
  882. *lpWmiName = *lpSid;
  883. }
  884. *lpWmiName = L'\0';
  885. }
  886. //*************************************************************
  887. // ConvertWMINameToSid
  888. //
  889. // WMI doesn't like '-' in names.
  890. //*************************************************************
  891. void ConvertWMINameToSid(LPTSTR lpWmiName, LPTSTR lpSid )
  892. {
  893. for (;(*lpWmiName); lpSid++, lpWmiName++) {
  894. if (*lpWmiName == L'_')
  895. *lpSid = L'-';
  896. else
  897. *lpSid = *lpWmiName;
  898. }
  899. *lpSid = L'\0';
  900. }
  901. //*************************************************************
  902. //
  903. // DeleteNameSpace
  904. //
  905. // Purpose: Deletes namespace
  906. //
  907. // Parameters: pwszNameSpace - Namespace to delete
  908. // pWbemLocator - Wbem locator pointer
  909. //
  910. // Returns: True if successful, false otherwise
  911. //
  912. //*************************************************************
  913. HRESULT
  914. DeleteNameSpace( WCHAR *pwszNameSpace, WCHAR *pwszParentNameSpace, IWbemLocator *pWbemLocator )
  915. {
  916. XBStr xParentNameSpace( pwszParentNameSpace );
  917. if ( !xParentNameSpace )
  918. {
  919. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("DeleteNameSpace: Unable to allocate memory" ));
  920. return E_OUTOFMEMORY;
  921. }
  922. IWbemServices *pWbemServices = NULL;
  923. HRESULT hr = pWbemLocator->ConnectServer( xParentNameSpace,
  924. NULL,
  925. NULL,
  926. 0L,
  927. 0L,
  928. NULL,
  929. NULL,
  930. &pWbemServices );
  931. if ( FAILED(hr) )
  932. {
  933. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("DeleteNameSpace::ConnectServer failed with 0x%x" ), hr );
  934. return hr;
  935. }
  936. XInterface<IWbemServices> xWbemServices( pWbemServices );
  937. WCHAR wszNSRef[] = L"__Namespace.name=\"%ws\"";
  938. XPtrST<WCHAR> xwszNSValue;
  939. if ( !PrintToString( xwszNSValue, wszNSRef, pwszNameSpace, 0, 0 ) )
  940. {
  941. return E_OUTOFMEMORY;
  942. }
  943. XBStr xbstrNSValue( xwszNSValue );
  944. if ( !xbstrNSValue )
  945. {
  946. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("DeleteNameSpace: Failed to allocate memory" ));
  947. return E_OUTOFMEMORY;
  948. }
  949. VARIANT var;
  950. var.vt = VT_BSTR;
  951. var.bstrVal = xbstrNSValue;
  952. hr = pWbemServices->DeleteInstance( var.bstrVal,
  953. 0L,
  954. NULL,
  955. NULL );
  956. if ( FAILED(hr) )
  957. {
  958. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("DeleteNameSpace: Failed to DeleteInstance with 0x%x"), hr );
  959. return hr;
  960. }
  961. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("DeleteNameSpace: Deleted namespace %s under %s" ), pwszNameSpace, pwszParentNameSpace);
  962. return hr;
  963. }
  964. //*************************************************************
  965. //
  966. // DeleteRsopNameSpace
  967. //
  968. // Purpose: Deletes namespace
  969. //
  970. // Parameters: pwszNameSpace - Namespace to delete (the full path)
  971. // pWbemLocator - Wbem locator pointer
  972. //
  973. // Returns: True if successful, false otherwise
  974. //
  975. //*************************************************************
  976. HRESULT DeleteRsopNameSpace( WCHAR *pwszNameSpace, IWbemLocator *pWbemLocator )
  977. {
  978. LPWSTR pwszChildName = NULL;
  979. HRESULT hr = S_OK;
  980. //
  981. // Generating the parent child name by traversing the name
  982. //
  983. pwszChildName = wcsrchr(pwszNameSpace, L'\\');
  984. if (!pwszChildName) {
  985. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("DeleteRsopNameSpace: Invalid format - %s" ), pwszNameSpace);
  986. return E_INVALIDARG;
  987. }
  988. WCHAR wTmp = *pwszChildName;
  989. *pwszChildName = L'\0';
  990. pwszChildName++;
  991. hr = DeleteNameSpace(pwszChildName, pwszNameSpace, pWbemLocator);
  992. *(pwszChildName-1) = wTmp;
  993. return hr;
  994. }
  995. //*************************************************************
  996. //
  997. // IsInteractiveNameSpace
  998. //
  999. // Purpose: returns whether a namespace is a special namespace
  1000. // specifically created to allow interactive users to get rsop
  1001. // data
  1002. //
  1003. // Parameters: pwszNameSpace - Namespace
  1004. // szSid - Sid of the user
  1005. //
  1006. // Returns: True if successful, false otherwise
  1007. //
  1008. //*************************************************************
  1009. BOOL IsInteractiveNameSpace(WCHAR *pwszNameSpace, WCHAR *szSid)
  1010. {
  1011. LPWSTR pwszChildName = NULL;
  1012. HRESULT hr = S_OK;
  1013. XPtrLF<WCHAR> xwszInteractiveNameSpace;
  1014. BOOL bInteractive = FALSE;
  1015. DWORD dwInteractiveNSLength = 5 + wcslen(RSOP_NS_TEMP_PREFIX) + wcslen(szSid);
  1016. xwszInteractiveNameSpace = (LPWSTR)LocalAlloc(LPTR, ( dwInteractiveNSLength )*sizeof(WCHAR));
  1017. if (!xwszInteractiveNameSpace) {
  1018. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("IsInteractiveNameSpace: Couldn't Allocate memory. Error - %d" ), GetLastError());
  1019. return bInteractive;
  1020. }
  1021. //
  1022. // Generating the parent child name by traversing the name
  1023. //
  1024. pwszChildName = wcsrchr(pwszNameSpace, L'\\');
  1025. if (!pwszChildName) {
  1026. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("IsInteractiveNameSpace: Invalid format - %s" ), pwszNameSpace);
  1027. return bInteractive;
  1028. }
  1029. pwszChildName++;
  1030. XPtrLF<WCHAR> xszWmiName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(szSid)+1));
  1031. if (!xszWmiName) {
  1032. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("IsInteractiveNameSpace::CreateNameSpace couldn't allocate memory with error %d"), GetLastError() );
  1033. return bInteractive;
  1034. }
  1035. ConvertSidToWMIName(szSid, xszWmiName);
  1036. hr = StringCchPrintf( xwszInteractiveNameSpace,
  1037. dwInteractiveNSLength,
  1038. L"%s%s",
  1039. RSOP_NS_TEMP_PREFIX,
  1040. (LPWSTR) xszWmiName );
  1041. if(FAILED(hr))
  1042. return bInteractive;
  1043. if (_wcsicmp(pwszChildName, xwszInteractiveNameSpace) == 0) {
  1044. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("IsInteractiveNameSpace:: Interactive namespace"));
  1045. bInteractive = TRUE;
  1046. }
  1047. return bInteractive;
  1048. }
  1049. //*************************************************************
  1050. //
  1051. // GetInteractiveNameSpace
  1052. //
  1053. // Purpose: returns whether a namespace is a special namespace
  1054. // specifically created to allow interactive users to get rsop
  1055. // data
  1056. //
  1057. // Parameters: pwszNameSpace - Namespace
  1058. // szSid - Sid of the user
  1059. //
  1060. // Returns: True if successful, false otherwise
  1061. //
  1062. //*************************************************************
  1063. HRESULT GetInteractiveNameSpace(WCHAR *szSid, LPWSTR *szNameSpace)
  1064. {
  1065. XPtrLF<WCHAR> xwszInteractiveNameSpace;
  1066. *szNameSpace = NULL;
  1067. DWORD dwInteractiveNSLength = 5 + wcslen(RSOP_NS_TEMP_FMT) + wcslen(szSid);
  1068. xwszInteractiveNameSpace = (LPWSTR)LocalAlloc(LPTR, ( dwInteractiveNSLength )*sizeof(WCHAR));
  1069. if (!xwszInteractiveNameSpace) {
  1070. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetInteractiveNameSpace: Couldn't Allocate memory. Error - %d" ), GetLastError());
  1071. return HRESULT_FROM_WIN32(GetLastError());
  1072. }
  1073. XPtrLF<WCHAR> xszWmiName = (LPTSTR)LocalAlloc(LPTR, sizeof(TCHAR)*(lstrlen(szSid)+1));
  1074. if (!xszWmiName) {
  1075. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("GetInteractiveNameSpace::Couldn't allocate memory with error %d"), GetLastError() );
  1076. return HRESULT_FROM_WIN32(GetLastError());
  1077. }
  1078. ConvertSidToWMIName(szSid, xszWmiName);
  1079. HRESULT hr = StringCchPrintf( xwszInteractiveNameSpace,
  1080. dwInteractiveNSLength,
  1081. RSOP_NS_TEMP_FMT,
  1082. (LPWSTR) xszWmiName);
  1083. if(FAILED(hr))
  1084. return hr;
  1085. *szNameSpace = xwszInteractiveNameSpace.Acquire();
  1086. return S_OK;
  1087. }
  1088. //*************************************************************
  1089. //
  1090. // PrintToString
  1091. //
  1092. // Purpose: Safe swprintf routine
  1093. //
  1094. // Parameters: xwszValue - String returned here
  1095. // wszString - Format string
  1096. // pwszParam1 - Param 1
  1097. // pwszParam2 - Param 2
  1098. // dwParam3 - Param 3
  1099. //
  1100. //*************************************************************
  1101. BOOL PrintToString( XPtrST<WCHAR>& xwszValue, WCHAR *pwszString,
  1102. WCHAR *pwszParam1, WCHAR *pwszParam2,
  1103. DWORD dwParam3 )
  1104. {
  1105. DWORD dwSize = wcslen(pwszString) + 32;
  1106. if ( pwszParam1 )
  1107. {
  1108. dwSize += wcslen( pwszParam1 );
  1109. }
  1110. if ( pwszParam2 )
  1111. {
  1112. dwSize += wcslen( pwszParam2 );
  1113. }
  1114. xwszValue = new WCHAR[dwSize];
  1115. if ( !xwszValue ) {
  1116. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("PrintToString: Failed to allocate memory" ));
  1117. return FALSE;
  1118. }
  1119. HRESULT hr = StringCchPrintf( xwszValue, dwSize, pwszString,
  1120. pwszParam1, pwszParam2, dwParam3 );
  1121. while ( FAILED(hr) ) {
  1122. dwSize *= 2;
  1123. xwszValue = new WCHAR[dwSize];
  1124. if ( !xwszValue ) {
  1125. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("PrintToString: Failed to allocate memory" ));
  1126. return FALSE;
  1127. }
  1128. hr = StringCchPrintf( xwszValue, dwSize, pwszString,
  1129. pwszParam1, pwszParam2, dwParam3 );
  1130. }
  1131. return TRUE;
  1132. }
  1133. //*************************************************************
  1134. //
  1135. // CreateAndCopyNameSpace
  1136. //
  1137. // Purpose: Creates and Copies the name space
  1138. // This does an exact replica of the Src Name Space including
  1139. // copying the security Descriptors from the Source
  1140. //
  1141. // Parameters:
  1142. //
  1143. // Returns: domain name if successful, 0 otherwise
  1144. //
  1145. //*************************************************************
  1146. HRESULT
  1147. CreateAndCopyNameSpace(IWbemLocator *pWbemLocator, LPWSTR szSrcNameSpace, LPWSTR szDstRootNameSpace,
  1148. LPWSTR szDstRelNameSpace, DWORD dwFlags, PSECURITY_DESCRIPTOR pSecDesc, LPWSTR *szDstNameSpaceOut)
  1149. {
  1150. BOOL bOk = TRUE, bAbort = FALSE;
  1151. BOOL bCopyClasses = (dwFlags & NEW_NS_FLAGS_COPY_CLASSES) ? TRUE : FALSE;
  1152. BOOL bCopyInstances = (dwFlags & NEW_NS_FLAGS_COPY_INSTS) ? TRUE : FALSE;
  1153. XPtrLF<WCHAR> xszDstNameSpace;
  1154. HRESULT hr = S_OK;
  1155. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CreateAndCopyNameSpace: New Name space from %s -> %s,%s, flags 0x%x "),
  1156. szSrcNameSpace, szDstRootNameSpace, szDstRelNameSpace, dwFlags);
  1157. if (szDstNameSpaceOut)
  1158. {
  1159. *szDstNameSpaceOut = 0;
  1160. }
  1161. DWORD dwDstNSLength = lstrlen(szDstRootNameSpace) + lstrlen(szDstRelNameSpace) + 5;
  1162. xszDstNameSpace = (LPWSTR)LocalAlloc(LPTR, sizeof(WCHAR) * ( dwDstNSLength ));
  1163. if (!xszDstNameSpace)
  1164. {
  1165. return E_OUTOFMEMORY;
  1166. }
  1167. hr = StringCchCopy(xszDstNameSpace, dwDstNSLength, szDstRootNameSpace);
  1168. if(FAILED(hr))
  1169. return hr;
  1170. hr = StringCchCat(xszDstNameSpace, dwDstNSLength, L"\\");
  1171. if(FAILED(hr))
  1172. return hr;
  1173. hr = StringCchCat(xszDstNameSpace, dwDstNSLength, szDstRelNameSpace);
  1174. if(FAILED(hr))
  1175. return hr;
  1176. hr = CreateNameSpace( szDstRelNameSpace, szDstRootNameSpace, pWbemLocator );
  1177. if ( FAILED( hr ) )
  1178. {
  1179. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateAndCopyNameSpace::CreateNameSpace failed with 0x%x"), hr );
  1180. return hr;
  1181. }
  1182. if (!pSecDesc)
  1183. {
  1184. hr = CopyNameSpaceSecurity(szSrcNameSpace, xszDstNameSpace, pWbemLocator );
  1185. if ( FAILED(hr) )
  1186. {
  1187. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateAndCopyNameSpace::CopyNameSpaceSecurity failed with 0x%x"), hr );
  1188. goto Exit;
  1189. }
  1190. }
  1191. else
  1192. {
  1193. hr = SetNameSpaceSecurity( xszDstNameSpace, pSecDesc, pWbemLocator);
  1194. if ( FAILED(hr) ) {
  1195. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("SetupNewNameSpace::SetNameSpaceSecurity failed with 0x%x"), hr );
  1196. goto Exit;
  1197. }
  1198. }
  1199. if (bCopyClasses) {
  1200. hr = CopyNameSpace( szSrcNameSpace, xszDstNameSpace, bCopyInstances, &bAbort, pWbemLocator );
  1201. if ( FAILED(hr) )
  1202. {
  1203. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateAndCopyNameSpace::CopyNameSpace failed with 0x%x"), hr );
  1204. goto Exit;
  1205. }
  1206. //
  1207. // Instantiate class RSOP_Session and set data member
  1208. // 'creationTime' with current date and time.
  1209. //
  1210. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CreateAndCopyNameSpace: Setting up creation time"));
  1211. hr = SetupCreationTimeAndCommit(pWbemLocator, xszDstNameSpace);
  1212. if(FAILED(hr))
  1213. {
  1214. dbg.Msg( DEBUG_MESSAGE_WARNING, TEXT("CreateAndCopyNameSpace: SetupCreationTimeAndCommit failed with 0x%x"), hr );
  1215. goto Exit;
  1216. }
  1217. }
  1218. dbg.Msg( DEBUG_MESSAGE_VERBOSE, TEXT("CreateAndCopyNameSpace: Returning with Success NameSpace %s "),
  1219. xszDstNameSpace);
  1220. if (szDstNameSpaceOut)
  1221. {
  1222. *szDstNameSpaceOut = xszDstNameSpace.Acquire();
  1223. }
  1224. return hr;
  1225. Exit:
  1226. DeleteNameSpace(szDstRelNameSpace, szDstRootNameSpace, pWbemLocator);
  1227. return hr;
  1228. }