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.

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