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.

1040 lines
24 KiB

  1. /*++
  2. Copyright (C) 1997-2001 Microsoft Corporation
  3. Module Name:
  4. GENUTILS.CPP
  5. Abstract:
  6. Defines various utilities.
  7. History:
  8. a-davj 21-June-97 Created.
  9. --*/
  10. #include "precomp.h"
  11. #include "corepol.h"
  12. #include "arena.h"
  13. #include <wbemidl.h>
  14. #include <arrtempl.h>
  15. #include "reg.h"
  16. #include "genutils.h"
  17. #include "wbemutil.h"
  18. #include "var.h"
  19. #include <TCHAR.H>
  20. #define IsSlash(x) (x == L'\\' || x== L'/')
  21. #ifndef EOAC_STATIC_CLOAKING
  22. #define EOAC_STATIC_CLOAKING 0x20
  23. #define EOAC_DYNAMIC_CLOAKING 0x40
  24. #endif
  25. //***************************************************************************
  26. //
  27. // BOOL IsNT
  28. //
  29. // DESCRIPTION:
  30. //
  31. // Returns true if running windows NT.
  32. //
  33. // RETURN VALUE:
  34. //
  35. // see description.
  36. //
  37. //***************************************************************************
  38. POLARITY BOOL IsNT(void)
  39. {
  40. OSVERSIONINFO os;
  41. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  42. if(!GetVersionEx(&os))
  43. return FALSE; // should never happen
  44. return os.dwPlatformId == VER_PLATFORM_WIN32_NT;
  45. }
  46. POLARITY BOOL IsWin95(void)
  47. {
  48. OSVERSIONINFO os;
  49. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  50. if(!GetVersionEx(&os))
  51. return FALSE; // should never happen
  52. return ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) && ( os.dwMinorVersion == 0 ) ;
  53. }
  54. POLARITY BOOL IsNT351(void)
  55. {
  56. OSVERSIONINFO os;
  57. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  58. if(!GetVersionEx(&os))
  59. return FALSE; // should never happen
  60. return ( os.dwPlatformId == VER_PLATFORM_WIN32_NT ) && ( os.dwMajorVersion == 3 ) && ( os.dwMinorVersion == 51 ) ;
  61. }
  62. POLARITY BOOL IsW2KOrMore(void)
  63. {
  64. OSVERSIONINFO os;
  65. os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  66. if(!GetVersionEx(&os))
  67. return FALSE; // should never happen
  68. return ( os.dwPlatformId == VER_PLATFORM_WIN32_NT ) && ( os.dwMajorVersion >= 5 ) ;
  69. }
  70. POLARITY BOOL IsWinMgmt(void)
  71. {
  72. //
  73. // Retrieve EXE path
  74. //
  75. WCHAR wszExePath[MAX_PATH+1];
  76. if(GetModuleFileNameW(NULL, wszExePath, MAX_PATH+1) == 0)
  77. return FALSE;
  78. //
  79. // Extract the file-name portion
  80. //
  81. WCHAR* pwcFileName = wcsrchr(wszExePath, L'\\');
  82. if(pwcFileName == NULL)
  83. pwcFileName = wszExePath;
  84. else
  85. pwcFileName++;
  86. if(_wcsnicmp(pwcFileName, FILENAME_PREFIX_EXE_W, wcslen(FILENAME_PREFIX_EXE_W)))
  87. return FALSE;
  88. return TRUE;
  89. }
  90. //***************************************************************************
  91. //
  92. // BOOL SetObjectAccess
  93. //
  94. // DESCRIPTION:
  95. //
  96. // Allows global access to an object.
  97. //
  98. // PARAMETERS:
  99. //
  100. // hObj Object to set access on.
  101. //
  102. // RETURN VALUE:
  103. //
  104. // Returns TRUE if OK.
  105. //
  106. //***************************************************************************
  107. POLARITY BOOL SetObjectAccess(
  108. IN HANDLE hObj)
  109. {
  110. PSECURITY_DESCRIPTOR pSD;
  111. DWORD dwLastErr = 0;
  112. BOOL bRet = FALSE;
  113. // no point if we arnt on nt
  114. if(!IsNT())
  115. {
  116. return TRUE;
  117. }
  118. pSD = (PSECURITY_DESCRIPTOR)CWin32DefaultArena::WbemMemAlloc(SECURITY_DESCRIPTOR_MIN_LENGTH);
  119. if(pSD == NULL)
  120. return FALSE;
  121. ZeroMemory(pSD, SECURITY_DESCRIPTOR_MIN_LENGTH);
  122. if(!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
  123. goto Cleanup;
  124. if(!SetSecurityDescriptorDacl(pSD, TRUE, NULL, FALSE))
  125. goto Cleanup;
  126. bRet = SetKernelObjectSecurity(hObj, DACL_SECURITY_INFORMATION, pSD);
  127. Cleanup:
  128. if(bRet == FALSE)
  129. dwLastErr = GetLastError();
  130. CWin32DefaultArena::WbemMemFree(pSD);
  131. return bRet;
  132. }
  133. //***************************************************************************
  134. //
  135. // void RegisterDLL
  136. //
  137. // DESCRIPTION:
  138. //
  139. // Adds the current dll to the registry as an inproc server.
  140. //
  141. // PARAMETERS:
  142. //
  143. // guid GUILD that this supports
  144. // pDesc Text description for this object.
  145. //
  146. //***************************************************************************
  147. POLARITY void RegisterDLL(IN HMODULE hModule, IN GUID guid, IN TCHAR * pDesc, TCHAR * pModel,
  148. TCHAR * pProgID)
  149. {
  150. // char szID[128];
  151. TCHAR wcID[128];
  152. TCHAR szCLSID[128];
  153. TCHAR szModule[MAX_PATH];
  154. HKEY hKey1 = NULL, hKey2 = NULL;
  155. // Create the path.
  156. wchar_t strCLSID[128];
  157. if(0 ==StringFromGUID2(guid, strCLSID, 128))
  158. return;
  159. #ifdef UNICODE
  160. lstrcpy(wcID, strCLSID);
  161. #else
  162. wcstombs(wcID, strCLSID, 128);
  163. #endif
  164. // wcstombs(szID, wcID, 128);
  165. lstrcpy(szCLSID, __TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  166. lstrcat(szCLSID, wcID);
  167. // Create entries under CLSID
  168. if(ERROR_SUCCESS != RegCreateKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey1))
  169. return;
  170. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pDesc, 2*(lstrlen(pDesc)+1));
  171. if(ERROR_SUCCESS != RegCreateKey(hKey1,__TEXT("InprocServer32"),&hKey2))
  172. return;
  173. if(0 == GetModuleFileName(hModule, szModule, MAX_PATH))
  174. return;
  175. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)szModule,
  176. 2*(lstrlen(szModule)+1));
  177. RegSetValueEx(hKey2, __TEXT("ThreadingModel"), 0, REG_SZ,
  178. (BYTE *)pModel, 2*(lstrlen(pModel)+1));
  179. RegCloseKey(hKey1);
  180. RegCloseKey(hKey2);
  181. // If there is a progid, then add it too
  182. if(pProgID)
  183. {
  184. wsprintf(wcID, __TEXT("SOFTWARE\\CLASSES\\%s"), pProgID);
  185. if(ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, wcID, &hKey1))
  186. {
  187. RegSetValueEx(hKey1, NULL, 0, REG_SZ, (BYTE *)pDesc , 2*(lstrlen(pDesc)+1));
  188. if(ERROR_SUCCESS == RegCreateKey(hKey1,__TEXT("CLSID"),&hKey2))
  189. {
  190. // wcstombs(szID, wcID, 128);
  191. RegSetValueEx(hKey2, NULL, 0, REG_SZ, (BYTE *)wcID,
  192. 2*(lstrlen(wcID)+1));
  193. RegCloseKey(hKey2);
  194. hKey2 = NULL;
  195. }
  196. RegCloseKey(hKey1);
  197. }
  198. }
  199. return;
  200. }
  201. //***************************************************************************
  202. //
  203. // void UnRegisterDLL
  204. //
  205. // DESCRIPTION:
  206. //
  207. // Removes an in proc guid from the clsid section
  208. //
  209. // PARAMETERS:
  210. //
  211. // guid guild to be removed.
  212. //
  213. //***************************************************************************
  214. POLARITY void UnRegisterDLL(GUID guid, TCHAR * pProgID)
  215. {
  216. // char szID[128];
  217. TCHAR wcID[128];
  218. TCHAR szCLSID[128];
  219. HKEY hKey;
  220. // Create the path using the CLSID
  221. wchar_t strCLSID[128];
  222. if(0 ==StringFromGUID2(guid, strCLSID, 128))
  223. return;
  224. #ifdef UNICODE
  225. lstrcpy(wcID, strCLSID);
  226. #else
  227. wcstombs(wcID, strCLSID, 128);
  228. #endif
  229. // wcstombs(szID, wcID, 128);
  230. lstrcpy(szCLSID, __TEXT("SOFTWARE\\CLASSES\\CLSID\\"));
  231. lstrcat(szCLSID, wcID);
  232. // First delete the InProcServer subkey.
  233. DWORD dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, szCLSID, &hKey);
  234. if(dwRet == NO_ERROR)
  235. {
  236. RegDeleteKey(hKey, __TEXT("InProcServer32"));
  237. RegCloseKey(hKey);
  238. }
  239. dwRet = RegOpenKey(HKEY_LOCAL_MACHINE, __TEXT("SOFTWARE\\CLASSES\\CLSID"), &hKey);
  240. if(dwRet == NO_ERROR)
  241. {
  242. RegDeleteKey(hKey,wcID);
  243. RegCloseKey(hKey);
  244. }
  245. if(pProgID)
  246. {
  247. HKEY hKey2;
  248. DWORD dwRet2 = RegOpenKey(HKEY_LOCAL_MACHINE, pProgID, &hKey2);
  249. if(dwRet2 == NO_ERROR)
  250. {
  251. RegDeleteKey(hKey2, __TEXT("CLSID"));
  252. RegCloseKey(hKey2);
  253. }
  254. RegDeleteKey(HKEY_LOCAL_MACHINE, pProgID);
  255. }
  256. }
  257. //***************************************************************************
  258. //
  259. // HRESULT WbemVariantChangeType
  260. //
  261. // DESCRIPTION:
  262. //
  263. // Just like VariantChangeType, but deals with arrays as well.
  264. //
  265. // PARAMETERS:
  266. //
  267. // VARIANT pvDest Destination variant
  268. // VARIANT pvSrc Source variant (can be the same as pvDest)
  269. // VARTYPE vtNew The type to coerce to.
  270. //
  271. //***************************************************************************
  272. POLARITY HRESULT WbemVariantChangeType(VARIANT* pvDest, VARIANT* pvSrc,
  273. VARTYPE vtNew)
  274. {
  275. HRESULT hres;
  276. if(V_VT(pvSrc) == VT_NULL)
  277. {
  278. return VariantCopy(pvDest, pvSrc);
  279. }
  280. if(vtNew & VT_ARRAY)
  281. {
  282. // It's an array, we have to do our own conversion
  283. // ===============================================
  284. if((V_VT(pvSrc) & VT_ARRAY) == 0)
  285. return DISP_E_TYPEMISMATCH;
  286. SAFEARRAY* psaSrc = V_ARRAY(pvSrc);
  287. SAFEARRAYBOUND aBounds[1];
  288. long lLBound;
  289. SafeArrayGetLBound(psaSrc, 1, &lLBound);
  290. long lUBound;
  291. SafeArrayGetUBound(psaSrc, 1, &lUBound);
  292. aBounds[0].cElements = lUBound - lLBound + 1;
  293. aBounds[0].lLbound = lLBound;
  294. SAFEARRAY* psaDest = SafeArrayCreate(vtNew & ~VT_ARRAY, 1, aBounds);
  295. // Stuff the individual data pieces
  296. // ================================
  297. for(long lIndex = lLBound; lIndex <= lUBound; lIndex++)
  298. {
  299. // Load the initial data element into a VARIANT
  300. // ============================================
  301. VARIANT vSrcEl;
  302. V_VT(&vSrcEl) = V_VT(pvSrc) & ~VT_ARRAY;
  303. SafeArrayGetElement(psaSrc, &lIndex, &V_UI1(&vSrcEl));
  304. // Cast it to the new type
  305. // =======================
  306. hres = VariantChangeType(&vSrcEl, &vSrcEl, 0, vtNew & ~VT_ARRAY);
  307. if(FAILED(hres))
  308. {
  309. SafeArrayDestroy(psaDest);
  310. return hres;
  311. }
  312. // Put it into the new array
  313. // =========================
  314. if(V_VT(&vSrcEl) == VT_BSTR)
  315. {
  316. hres = SafeArrayPutElement(psaDest, &lIndex, V_BSTR(&vSrcEl));
  317. }
  318. else
  319. {
  320. hres = SafeArrayPutElement(psaDest, &lIndex, &V_UI1(&vSrcEl));
  321. }
  322. if(FAILED(hres))
  323. {
  324. SafeArrayDestroy(psaDest);
  325. return hres;
  326. }
  327. }
  328. if(pvDest == pvSrc)
  329. {
  330. VariantClear(pvSrc);
  331. }
  332. V_VT(pvDest) = vtNew;
  333. V_ARRAY(pvDest) = psaDest;
  334. return TRUE;
  335. }
  336. else
  337. {
  338. // Not an array. Can use OLE functions
  339. // ===================================
  340. return VariantChangeType(pvDest, pvSrc, VARIANT_NOVALUEPROP, vtNew);
  341. }
  342. }
  343. //***************************************************************************
  344. //
  345. // BOOL ReadI64
  346. //
  347. // DESCRIPTION:
  348. //
  349. // Reads a signed 64-bit value from a string
  350. //
  351. // PARAMETERS:
  352. //
  353. // LPCWSTR wsz String to read from
  354. // __int64& i64 Destination for the value
  355. //
  356. //***************************************************************************
  357. POLARITY BOOL ReadI64(LPCWSTR wsz, UNALIGNED __int64& ri64)
  358. {
  359. __int64 i64 = 0;
  360. const WCHAR* pwc = wsz;
  361. // Check for a NULL pointer
  362. if ( NULL == wsz )
  363. {
  364. return FALSE;
  365. }
  366. int nSign = 1;
  367. if(*pwc == L'-')
  368. {
  369. nSign = -1;
  370. pwc++;
  371. }
  372. while(i64 >= 0 && i64 < 0x7FFFFFFFFFFFFFFF / 8 &&
  373. *pwc >= L'0' && *pwc <= L'9')
  374. {
  375. i64 = i64 * 10 + (*pwc - L'0');
  376. pwc++;
  377. }
  378. if(*pwc)
  379. return FALSE;
  380. if(i64 < 0)
  381. {
  382. // Special case --- largest negative number
  383. // ========================================
  384. if(nSign == -1 && i64 == (__int64)0x8000000000000000)
  385. {
  386. ri64 = i64;
  387. return TRUE;
  388. }
  389. return FALSE;
  390. }
  391. ri64 = i64 * nSign;
  392. return TRUE;
  393. }
  394. //***************************************************************************
  395. //
  396. // BOOL ReadUI64
  397. //
  398. // DESCRIPTION:
  399. //
  400. // Reads an unsigned 64-bit value from a string
  401. //
  402. // PARAMETERS:
  403. //
  404. // LPCWSTR wsz String to read from
  405. // unsigned __int64& i64 Destination for the value
  406. //
  407. //***************************************************************************
  408. POLARITY BOOL ReadUI64(LPCWSTR wsz, UNALIGNED unsigned __int64& rui64)
  409. {
  410. unsigned __int64 ui64 = 0;
  411. const WCHAR* pwc = wsz;
  412. // Check for a NULL pointer
  413. if ( NULL == wsz )
  414. {
  415. return FALSE;
  416. }
  417. while(ui64 < 0xFFFFFFFFFFFFFFFF / 8 && *pwc >= L'0' && *pwc <= L'9')
  418. {
  419. unsigned __int64 ui64old = ui64;
  420. ui64 = ui64 * 10 + (*pwc - L'0');
  421. if(ui64 < ui64old)
  422. return FALSE;
  423. pwc++;
  424. }
  425. if(*pwc)
  426. {
  427. return FALSE;
  428. }
  429. rui64 = ui64;
  430. return TRUE;
  431. }
  432. POLARITY HRESULT ChangeVariantToCIMTYPE(VARIANT* pvDest, VARIANT* pvSource,
  433. CIMTYPE ct)
  434. {
  435. if(ct == CIM_CHAR16)
  436. {
  437. //
  438. // Special case --- use CVar's code
  439. //
  440. CVar v;
  441. try
  442. {
  443. v.SetVariant(pvSource);
  444. if(!v.ToSingleChar())
  445. return WBEM_E_TYPE_MISMATCH;
  446. v.FillVariant(pvDest);
  447. return WBEM_S_NO_ERROR;
  448. }
  449. catch(...)
  450. {
  451. return WBEM_E_OUT_OF_MEMORY;
  452. }
  453. }
  454. VARTYPE vt;
  455. switch(ct)
  456. {
  457. case CIM_UINT8:
  458. vt = VT_UI1;
  459. break;
  460. case CIM_SINT8:
  461. case CIM_SINT16:
  462. vt = VT_I2;
  463. break;
  464. case CIM_UINT16:
  465. case CIM_SINT32:
  466. vt = VT_I4;
  467. break;
  468. case CIM_UINT32:
  469. case CIM_UINT64:
  470. case CIM_SINT64:
  471. case CIM_STRING:
  472. case CIM_DATETIME:
  473. case CIM_REFERENCE:
  474. vt = VT_BSTR;
  475. break;
  476. case CIM_REAL32:
  477. vt = VT_R4;
  478. break;
  479. case CIM_REAL64:
  480. vt = VT_R8;
  481. break;
  482. case CIM_OBJECT:
  483. vt = VT_UNKNOWN;
  484. break;
  485. case CIM_BOOLEAN:
  486. vt = VT_BOOL;
  487. break;
  488. default:
  489. return WBEM_E_TYPE_MISMATCH;
  490. }
  491. HRESULT hres = WbemVariantChangeType(pvDest, pvSource, vt);
  492. if(FAILED(hres))
  493. return hres;
  494. if(ct == CIM_SINT8)
  495. {
  496. if(V_I2(pvDest) > 127 || V_I2(pvDest) < -128)
  497. hres = WBEM_E_TYPE_MISMATCH;
  498. }
  499. else if(ct == CIM_UINT16)
  500. {
  501. if(V_I4(pvDest) > 65535 || V_I4(pvDest) < 0)
  502. hres = WBEM_E_TYPE_MISMATCH;
  503. }
  504. else if(ct == CIM_UINT32)
  505. {
  506. __int64 i64;
  507. if(!ReadI64(V_BSTR(pvDest), i64))
  508. hres = WBEM_E_INVALID_QUERY;
  509. else if(i64 < 0 || i64 >= (__int64)1 << 32)
  510. hres = WBEM_E_TYPE_MISMATCH;
  511. }
  512. else if(ct == CIM_UINT64)
  513. {
  514. unsigned __int64 ui64;
  515. if(!ReadUI64(V_BSTR(pvDest), ui64))
  516. hres = WBEM_E_INVALID_QUERY;
  517. }
  518. else if(ct == CIM_SINT64)
  519. {
  520. __int64 i64;
  521. if(!ReadI64(V_BSTR(pvDest), i64))
  522. hres = WBEM_E_INVALID_QUERY;
  523. }
  524. if(FAILED(hres))
  525. {
  526. VariantClear(pvDest);
  527. }
  528. return hres;
  529. }
  530. static HANDLE g_hSecurityMutex = NULL;
  531. static BOOL g_bUseSecurityMutex = FALSE;
  532. static DWORD g_dwSecurityMutexThreadId = 0xFFFFFFFF;
  533. POLARITY void SecurityMutexRequest()
  534. {
  535. if (g_bUseSecurityMutex)
  536. {
  537. WaitForSingleObject(g_hSecurityMutex, INFINITE);
  538. // Store the owning Thread Id
  539. g_dwSecurityMutexThreadId = GetCurrentThreadId();
  540. }
  541. }
  542. POLARITY void SecurityMutexClear()
  543. {
  544. if (g_bUseSecurityMutex)
  545. {
  546. // Clear the owning thread id for debugging
  547. g_dwSecurityMutexThreadId = 0xFFFFFFFF;
  548. ReleaseMutex(g_hSecurityMutex);
  549. }
  550. }
  551. //***************************************************************************
  552. //
  553. // bool IsStandAloneWin9X
  554. //
  555. // DESCRIPTION:
  556. //
  557. // returns true if we are a win95 box and we want standalone security.
  558. //
  559. //***************************************************************************
  560. POLARITY bool IsStandAloneWin9X()
  561. {
  562. if(IsNT())
  563. return false;
  564. Registry r(HKEY_LOCAL_MACHINE, KEY_READ, WBEM_REG_WINMGMT);
  565. DWORD dwEnabled = 0;
  566. int iRes = r.GetDWORDStr(__TEXT("EnableAnonConnections"), &dwEnabled);
  567. if(iRes == 0 && dwEnabled == 1)
  568. return true;
  569. else
  570. return false;
  571. }
  572. //***************************************************************************
  573. //
  574. // WCHAR *ExtractMachineName
  575. //
  576. // DESCRIPTION:
  577. //
  578. // Takes a path of form "\\machine\xyz... and returns the
  579. // "machine" portion in a newly allocated WCHAR. The return value should
  580. // be freed via delete. NULL is returned if there is an error.
  581. //
  582. //
  583. // PARAMETERS:
  584. //
  585. // pPath Path to be parsed.
  586. //
  587. // RETURN VALUE:
  588. //
  589. // see description.
  590. //
  591. //***************************************************************************
  592. POLARITY WCHAR *ExtractMachineName ( IN BSTR a_Path )
  593. {
  594. WCHAR *t_MachineName = NULL;
  595. //todo, according to the help file, the path can be null which is
  596. // default to current machine, however Ray's mail indicated that may
  597. // not be so.
  598. if ( a_Path == NULL )
  599. {
  600. t_MachineName = new WCHAR [ 2 ] ;
  601. if ( t_MachineName )
  602. {
  603. wcscpy ( t_MachineName , L"." ) ;
  604. }
  605. return t_MachineName ;
  606. }
  607. // First make sure there is a path and determine how long it is.
  608. if ( ! IsSlash ( a_Path [ 0 ] ) || ! IsSlash ( a_Path [ 1 ] ) || wcslen ( a_Path ) < 3 )
  609. {
  610. t_MachineName = new WCHAR [ 2 ] ;
  611. if ( t_MachineName )
  612. {
  613. wcscpy ( t_MachineName , L"." ) ;
  614. }
  615. return t_MachineName ;
  616. }
  617. WCHAR *t_ThirdSlash ;
  618. for ( t_ThirdSlash = a_Path + 2 ; *t_ThirdSlash ; t_ThirdSlash ++ )
  619. {
  620. if ( IsSlash ( *t_ThirdSlash ) )
  621. break ;
  622. }
  623. if ( t_ThirdSlash == &a_Path [2] )
  624. {
  625. return NULL;
  626. }
  627. // allocate some memory
  628. t_MachineName = new WCHAR [ t_ThirdSlash - a_Path - 1 ] ;
  629. if ( t_MachineName == NULL )
  630. {
  631. return t_MachineName ;
  632. }
  633. // temporarily replace the third slash with a null and then copy
  634. WCHAR t_SlashCharacter = *t_ThirdSlash ;
  635. *t_ThirdSlash = NULL;
  636. wcscpy ( t_MachineName , a_Path + 2 ) ;
  637. *t_ThirdSlash = t_SlashCharacter ; // restore it.
  638. return t_MachineName ;
  639. }
  640. //***************************************************************************
  641. //
  642. // BOOL bAreWeLocal
  643. //
  644. // DESCRIPTION:
  645. //
  646. // Determines if the connection is to the current machine.
  647. //
  648. // PARAMETERS:
  649. //
  650. // pwcServerName Server name as extracted from the path.
  651. //
  652. // RETURN VALUE:
  653. //
  654. // True if we are local
  655. //
  656. //***************************************************************************
  657. POLARITY BOOL bAreWeLocal(WCHAR * pServerMachine)
  658. {
  659. BOOL bRet = FALSE;
  660. if(pServerMachine == NULL)
  661. return TRUE;
  662. if(!wbem_wcsicmp(pServerMachine,L"."))
  663. return TRUE;
  664. if ( IsNT () )
  665. {
  666. wchar_t wczMyName[MAX_PATH];
  667. DWORD dwSize = MAX_PATH;
  668. if(!GetComputerNameW(wczMyName,&dwSize))
  669. return FALSE;
  670. bRet = !wbem_wcsicmp(wczMyName,pServerMachine);
  671. }
  672. else
  673. {
  674. TCHAR tcMyName[MAX_PATH];
  675. DWORD dwSize = MAX_PATH;
  676. if(!GetComputerName(tcMyName,&dwSize))
  677. return FALSE;
  678. #ifdef UNICODE
  679. bRet = !wbem_wcsicmp(tcMyName,pServerMachine);
  680. #else
  681. WCHAR wWide[MAX_PATH];
  682. mbstowcs(wWide, tcMyName, MAX_PATH-1);
  683. bRet = !wbem_wcsicmp(wWide,pServerMachine);
  684. #endif
  685. }
  686. return bRet;
  687. }
  688. POLARITY HRESULT WbemSetDynamicCloaking(IUnknown* pProxy,
  689. DWORD dwAuthnLevel, DWORD dwImpLevel)
  690. {
  691. HRESULT hres;
  692. if(!IsW2KOrMore())
  693. {
  694. // Not NT5 --- don't bother
  695. // ========================
  696. return WBEM_S_FALSE;
  697. }
  698. // Try to get IClientSecurity from it
  699. // ==================================
  700. IClientSecurity* pSec;
  701. hres = pProxy->QueryInterface(IID_IClientSecurity, (void**)&pSec);
  702. if(FAILED(hres))
  703. {
  704. // Not a proxy --- not a problem
  705. // =============================
  706. return WBEM_S_FALSE;
  707. }
  708. hres = pSec->SetBlanket(pProxy, RPC_C_AUTHN_WINNT,
  709. RPC_C_AUTHZ_NONE, NULL, dwAuthnLevel,
  710. dwImpLevel, NULL, EOAC_DYNAMIC_CLOAKING);
  711. pSec->Release();
  712. return hres;
  713. }
  714. POLARITY DWORD GetCurrentImpersonationLevel()
  715. {
  716. // Open thread token
  717. // =================
  718. HANDLE hThreadToken;
  719. BOOL bRes = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE,
  720. &hThreadToken);
  721. if (bRes == FALSE)
  722. {
  723. long lRes = GetLastError();
  724. if(lRes == ERROR_NO_IMPERSONATION_TOKEN || lRes == ERROR_NO_TOKEN)
  725. {
  726. // This is the basic process thread.
  727. // =================================
  728. return RPC_C_IMP_LEVEL_DELEGATE;
  729. }
  730. else if(lRes == ERROR_CANT_OPEN_ANONYMOUS)
  731. {
  732. // Anonymous call
  733. // ==============
  734. return RPC_C_IMP_LEVEL_ANONYMOUS;
  735. }
  736. else
  737. {
  738. // Unknown error
  739. // =============
  740. return RPC_C_IMP_LEVEL_ANONYMOUS;
  741. }
  742. }
  743. // Find out token info.
  744. // =====================
  745. DWORD dwTmp = 0, dwBytesReturned = 0;
  746. bRes = GetTokenInformation(
  747. hThreadToken,
  748. TokenImpersonationLevel,
  749. &dwTmp,
  750. sizeof(DWORD),
  751. &dwBytesReturned
  752. );
  753. // We don't need this anymore
  754. CloseHandle(hThreadToken);
  755. hThreadToken = NULL;
  756. if (bRes == FALSE)
  757. {
  758. return RPC_C_IMP_LEVEL_ANONYMOUS;
  759. }
  760. switch (dwTmp)
  761. {
  762. case SecurityAnonymous:
  763. return RPC_C_IMP_LEVEL_ANONYMOUS;
  764. case SecurityIdentification:
  765. return RPC_C_IMP_LEVEL_IDENTIFY;
  766. case SecurityImpersonation:
  767. return RPC_C_IMP_LEVEL_IMPERSONATE;
  768. case SecurityDelegation:
  769. return RPC_C_IMP_LEVEL_DELEGATE;
  770. default:
  771. return RPC_C_IMP_LEVEL_ANONYMOUS;
  772. }
  773. }
  774. POLARITY HRESULT EnableAllPrivileges(DWORD dwTokenType)
  775. {
  776. // Open thread token
  777. // =================
  778. HANDLE hToken = NULL;
  779. BOOL bRes;
  780. switch (dwTokenType)
  781. {
  782. case TOKEN_THREAD:
  783. bRes = OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, TRUE, &hToken);
  784. break;
  785. case TOKEN_PROCESS:
  786. bRes = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken);
  787. break;
  788. }
  789. if(!bRes)
  790. return WBEM_E_ACCESS_DENIED;
  791. // Get the privileges
  792. // ==================
  793. DWORD dwLen;
  794. TOKEN_USER tu;
  795. memset(&tu,0,sizeof(TOKEN_USER));
  796. bRes = GetTokenInformation(hToken, TokenPrivileges, &tu, sizeof(TOKEN_USER), &dwLen);
  797. BYTE* pBuffer = new BYTE[dwLen];
  798. if(pBuffer == NULL)
  799. {
  800. CloseHandle(hToken);
  801. return WBEM_E_OUT_OF_MEMORY;
  802. }
  803. bRes = GetTokenInformation(hToken, TokenPrivileges, pBuffer, dwLen,
  804. &dwLen);
  805. if(!bRes)
  806. {
  807. CloseHandle(hToken);
  808. delete [] pBuffer;
  809. return WBEM_E_ACCESS_DENIED;
  810. }
  811. // Iterate through all the privileges and enable them all
  812. // ======================================================
  813. TOKEN_PRIVILEGES* pPrivs = (TOKEN_PRIVILEGES*)pBuffer;
  814. for(DWORD i = 0; i < pPrivs->PrivilegeCount; i++)
  815. {
  816. pPrivs->Privileges[i].Attributes |= SE_PRIVILEGE_ENABLED;
  817. }
  818. // Store the information back into the token
  819. // =========================================
  820. bRes = AdjustTokenPrivileges(hToken, FALSE, pPrivs, 0, NULL, NULL);
  821. delete [] pBuffer;
  822. CloseHandle(hToken);
  823. if(!bRes)
  824. return WBEM_E_ACCESS_DENIED;
  825. else
  826. return WBEM_S_NO_ERROR;
  827. }
  828. POLARITY bool IsPrivilegePresent(HANDLE hToken, LPCTSTR pName)
  829. {
  830. if(pName == NULL)
  831. return false;
  832. LUID PrivilegeRequired ;
  833. if(!LookupPrivilegeValue(NULL, pName, &PrivilegeRequired))
  834. return FALSE;
  835. // Get the privileges
  836. // ==================
  837. DWORD dwLen;
  838. TOKEN_USER tu;
  839. memset(&tu,0,sizeof(TOKEN_USER));
  840. BOOL bRes = GetTokenInformation(hToken, TokenPrivileges, &tu, sizeof(TOKEN_USER), &dwLen);
  841. BYTE* pBuffer = new BYTE[dwLen];
  842. if(pBuffer == NULL)
  843. return false;
  844. CDeleteMe<BYTE> dm(pBuffer);
  845. bRes = GetTokenInformation(hToken, TokenPrivileges, pBuffer, dwLen,
  846. &dwLen);
  847. if(!bRes)
  848. return false;
  849. // Iterate through all the privileges and look for the one in question.
  850. // ======================================================
  851. TOKEN_PRIVILEGES* pPrivs = (TOKEN_PRIVILEGES*)pBuffer;
  852. for(DWORD i = 0; i < pPrivs->PrivilegeCount; i++)
  853. {
  854. if(pPrivs->Privileges[i].Luid.LowPart == PrivilegeRequired.LowPart &&
  855. pPrivs->Privileges[i].Luid.HighPart == PrivilegeRequired.HighPart )
  856. return true;
  857. }
  858. return false;
  859. }