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.

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