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.

472 lines
10 KiB

  1. /* Copyright (c) 2000, Microsoft Corporation, all rights reserved
  2. **
  3. ** utilsys.c
  4. ** Non-UI system helper routines (no HWNDs required)
  5. ** Listed alphabetically
  6. **
  7. ** 12/14/2000 gangz, cut from original ...\rasdlg\util.c to make some system utility funciton to
  8. ** the very base for both rassrvui and rasdlg
  9. */
  10. #include <windows.h> // Win32 root
  11. #include <debug.h> // Trace/Assert library
  12. #include <lmwksta.h> // NetWkstaGetInfo
  13. #include <lmapibuf.h> // NetApiBufferFree
  14. #include <dsrole.h> // machine is a member of a workgroup or domain, etc.
  15. #include <tchar.h>
  16. #include <nouiutil.h>
  17. // Cached workstation and logon information. See GetLogonUser,
  18. // GetLogonDomain, and GetComputer.
  19. //
  20. static TCHAR g_szLogonUser[ UNLEN + 1 ];
  21. static TCHAR g_szLogonDomain[ DNLEN + 1 ];
  22. static TCHAR g_szComputer[ CNLEN + 1 ];
  23. static DWORD g_dwSku, g_dwProductType;
  24. static DSROLE_MACHINE_ROLE g_DsRole;
  25. static BOOL g_fMachineSkuAndRoleInitialized = FALSE;
  26. //-----------------------------------------------------------------------------
  27. // Local helper prototypes (alphabetically)
  28. //-----------------------------------------------------------------------------
  29. DWORD
  30. GetComputerRole(
  31. DSROLE_MACHINE_ROLE* pRole );
  32. DWORD
  33. GetComputerSuite(
  34. LPDWORD lpdwSku );
  35. DWORD
  36. GetComputerSuiteAndProductType(
  37. LPDWORD lpdwSku,
  38. LPDWORD lpdwType);
  39. VOID
  40. GetWkstaUserInfo(
  41. void );
  42. DWORD
  43. LoadSkuAndRole(
  44. void);
  45. //-----------------------------------------------------------------------------
  46. // Utility routines
  47. //-----------------------------------------------------------------------------
  48. TCHAR*
  49. GetLogonUser(
  50. void )
  51. // Returns the address of a static buffer containing the logged on user's
  52. // account name.
  53. //
  54. {
  55. if (g_szLogonUser[ 0 ] == TEXT('\0'))
  56. {
  57. GetWkstaUserInfo();
  58. }
  59. TRACEW1( "GetLogonUser=%s",g_szLogonUser );
  60. return g_szLogonUser;
  61. }
  62. VOID
  63. GetWkstaUserInfo(
  64. void )
  65. // Helper to load statics with NetWkstaUserInfo information. See
  66. // GetLogonUser and GetLogonDomain.
  67. //
  68. {
  69. DWORD dwErr;
  70. WKSTA_USER_INFO_1* pInfo;
  71. pInfo = NULL;
  72. TRACE( "NetWkstaUserGetInfo" );
  73. dwErr = NetWkstaUserGetInfo( NULL, 1, (LPBYTE* )&pInfo );
  74. TRACE1( "NetWkstaUserGetInfo=%d", dwErr );
  75. if (pInfo)
  76. {
  77. if (dwErr == 0)
  78. {
  79. lstrcpyn( g_szLogonUser, pInfo->wkui1_username, UNLEN + 1 );
  80. lstrcpyn( g_szLogonDomain, pInfo->wkui1_logon_domain, DNLEN + 1 );
  81. }
  82. NetApiBufferFree( pInfo );
  83. }
  84. }
  85. TCHAR*
  86. GetLogonDomain(
  87. void )
  88. // Returns the address of a static buffer containing the logged on user's
  89. // domain name.
  90. //
  91. {
  92. if (g_szLogonDomain[ 0 ] == TEXT('\0'))
  93. {
  94. GetWkstaUserInfo();
  95. }
  96. TRACEW1( "GetLogonDomain=%s", g_szLogonDomain );
  97. return g_szLogonDomain;
  98. }
  99. // For whistler 480871
  100. // Prevent Enabling Firewall by default in NWC when RRAS is enabled
  101. //
  102. DWORD
  103. RasSrvIsRRASConfigured(
  104. OUT BOOL * pfConfig)
  105. {
  106. BOOL fRet = FALSE;
  107. DWORD dwErr = NO_ERROR;
  108. const WCHAR pwszServiceKey[] =
  109. L"SYSTEM\\CurrentControlSet\\Services\\RemoteAccess";
  110. const WCHAR pwszValue[] = L"ConfigurationFlags";
  111. HKEY hkParam = NULL;
  112. if ( NULL == pfConfig )
  113. {
  114. return ERROR_INVALID_PARAMETER;
  115. }
  116. do
  117. {
  118. // Attempt to open the service registry key
  119. dwErr = RegOpenKeyExW(
  120. HKEY_LOCAL_MACHINE,
  121. pwszServiceKey,
  122. 0,
  123. KEY_READ | KEY_WRITE,
  124. &hkParam);
  125. // If we opened the key ok, then we can assume
  126. // that the service is installed
  127. if ( ERROR_SUCCESS != dwErr )
  128. {
  129. break;
  130. }
  131. // Query the ConfigurationFlags ( RRAS configured?) Value
  132. {
  133. DWORD dwSize, dwValue, dwType;
  134. dwSize = sizeof(DWORD);
  135. dwValue = 0;
  136. RegQueryValueEx( hkParam,
  137. pwszValue,
  138. 0,
  139. &dwType,
  140. (BYTE*)&dwValue,
  141. &dwSize
  142. );
  143. *pfConfig = dwValue ? TRUE : FALSE;
  144. }
  145. }
  146. while(FALSE);
  147. if ( hkParam )
  148. {
  149. RegCloseKey( hkParam );
  150. }
  151. return dwErr;
  152. }
  153. //Add this for bug 342810 328673 397663
  154. //
  155. //Firewall is available for Personal, Professional
  156. //And Domain membership doesnt affect
  157. //
  158. BOOL
  159. IsFirewallAvailablePlatform(
  160. void)
  161. {
  162. DWORD dwSku, dwType;
  163. BOOL fAvailable = FALSE;
  164. //For whislter bug 417039, Firewall is taken out of 64bit build
  165. //
  166. #ifdef _WIN64
  167. return FALSE;
  168. #endif
  169. if (GetComputerSuiteAndProductType(&dwSku, &dwType) != NO_ERROR)
  170. {
  171. return FALSE;
  172. }
  173. do {
  174. BOOL fConfig = FALSE;
  175. // For whislter 480871 gangz
  176. // Wont enabling configuring PFW in NWC by default if RRAS is
  177. // configured.
  178. //
  179. if( NO_ERROR == RasSrvIsRRASConfigured( & fConfig ) )
  180. {
  181. if( fConfig )
  182. {
  183. fAvailable = FALSE;
  184. break;
  185. }
  186. }
  187. //If it is a personal
  188. //
  189. if ( dwSku & VER_SUITE_PERSONAL )
  190. {
  191. fAvailable = TRUE;
  192. break;
  193. }
  194. //if it is a Professional
  195. //
  196. if ( (VER_NT_WORKSTATION == dwType ) &&
  197. !(dwSku & VER_SUITE_PERSONAL) )
  198. {
  199. fAvailable = TRUE;
  200. break;
  201. }
  202. // For bug 482219
  203. // PFW/ICS are back again to Stander Server and Advanced server
  204. //if it is a standard Server, VER_SUITE_ENTERPRISE is advanced server
  205. //
  206. if ( ( VER_NT_SERVER == dwType ) &&
  207. !(dwSku & VER_SUITE_DATACENTER) &&
  208. !(dwSku & VER_SUITE_BLADE ) &&
  209. !(dwSku & VER_SUITE_BACKOFFICE ) &&
  210. !(dwSku & VER_SUITE_SMALLBUSINESS_RESTRICTED ) &&
  211. !(dwSku & VER_SUITE_SMALLBUSINESS )
  212. )
  213. {
  214. fAvailable = TRUE; // For whistler bug 397663
  215. break;
  216. }
  217. }
  218. while (FALSE);
  219. return fAvailable;
  220. }
  221. BOOL
  222. IsAdvancedServerPlatform(
  223. void)
  224. {
  225. DWORD dwSku;
  226. if (GetComputerSuite(&dwSku) != NO_ERROR)
  227. {
  228. return FALSE;
  229. }
  230. return ( dwSku & VER_SUITE_ENTERPRISE );
  231. }
  232. BOOL
  233. IsPersonalPlatform(
  234. void)
  235. {
  236. DWORD dwSku;
  237. if (GetComputerSuite(&dwSku) != NO_ERROR)
  238. {
  239. return FALSE;
  240. }
  241. return ( dwSku & VER_SUITE_PERSONAL );
  242. }
  243. BOOL
  244. IsStandaloneWKS(
  245. void)
  246. {
  247. DSROLE_MACHINE_ROLE DsRole;
  248. if (GetComputerRole(&DsRole) != NO_ERROR)
  249. {
  250. return FALSE;
  251. }
  252. return ( DsRole == DsRole_RoleStandaloneWorkstation );
  253. }
  254. BOOL
  255. IsConsumerPlatform(
  256. void)
  257. // Returns whether this is a consumer platform so the UI can render itself
  258. // for simpler cases. In Whistler, the consumer platforms were the
  259. // (personal sku) and the (professional sku if the machine wasn't a
  260. // member of a domain)
  261. //
  262. {
  263. return ( IsPersonalPlatform() ||
  264. IsStandaloneWKS() );
  265. }
  266. TCHAR*
  267. GetComputer(
  268. void )
  269. // Returns the address of a static buffer containing the local
  270. // workstation's computer name.
  271. //
  272. {
  273. if (g_szComputer[ 0 ] == TEXT('\0'))
  274. {
  275. DWORD dwErr;
  276. WKSTA_INFO_100* pInfo;
  277. pInfo = NULL;
  278. TRACE( "NetWkstaGetInfo" );
  279. dwErr = NetWkstaGetInfo( NULL, 100, (LPBYTE* )&pInfo );
  280. TRACE1( "NetWkstaGetInfo=%d", dwErr );
  281. if (pInfo)
  282. {
  283. if (dwErr == 0)
  284. {
  285. lstrcpyn(
  286. g_szComputer,
  287. pInfo->wki100_computername,
  288. CNLEN + 1);
  289. }
  290. NetApiBufferFree( pInfo );
  291. }
  292. }
  293. TRACEW1( "GetComputer=%s",g_szComputer );
  294. return g_szComputer;
  295. }
  296. DWORD
  297. GetComputerSuiteAndProductType(
  298. LPDWORD lpdwSku,
  299. LPDWORD lpdwType)
  300. // Returns the machine's product sku
  301. {
  302. DWORD dwErr = NO_ERROR;
  303. if (! g_fMachineSkuAndRoleInitialized)
  304. {
  305. dwErr = LoadSkuAndRole();
  306. if (dwErr != NO_ERROR)
  307. {
  308. return dwErr;
  309. }
  310. }
  311. *lpdwSku = g_dwSku;
  312. *lpdwType = g_dwProductType;
  313. return dwErr;
  314. }
  315. DWORD
  316. GetComputerSuite(
  317. LPDWORD lpdwSku )
  318. // Returns the machine's product sku
  319. {
  320. DWORD dwErr = NO_ERROR;
  321. if (! g_fMachineSkuAndRoleInitialized)
  322. {
  323. dwErr = LoadSkuAndRole();
  324. if (dwErr != NO_ERROR)
  325. {
  326. return dwErr;
  327. }
  328. }
  329. *lpdwSku = g_dwSku;
  330. return dwErr;
  331. }
  332. DWORD
  333. GetComputerRole(
  334. DSROLE_MACHINE_ROLE* pRole )
  335. // Returns whether this machine is a member of domain, etc.
  336. {
  337. DWORD dwErr = NO_ERROR;
  338. if (! g_fMachineSkuAndRoleInitialized)
  339. {
  340. dwErr = LoadSkuAndRole();
  341. if (dwErr != NO_ERROR)
  342. {
  343. return dwErr;
  344. }
  345. }
  346. *pRole = g_DsRole;
  347. return dwErr;
  348. }
  349. DWORD
  350. LoadSkuAndRole(
  351. void)
  352. // Loads the machine's role and it's sku
  353. {
  354. OSVERSIONINFOEX osVer;
  355. PDSROLE_PRIMARY_DOMAIN_INFO_BASIC pInfo = NULL;
  356. DWORD dwErr = NO_ERROR;
  357. // Get the product sku
  358. //
  359. ZeroMemory(&osVer, sizeof(osVer));
  360. osVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  361. if (GetVersionEx((LPOSVERSIONINFO) &osVer))
  362. {
  363. g_dwSku = osVer.wSuiteMask;
  364. g_dwProductType = osVer.wProductType;
  365. }
  366. else
  367. {
  368. return GetLastError();
  369. }
  370. // Get the product role
  371. //
  372. dwErr = DsRoleGetPrimaryDomainInformation(
  373. NULL,
  374. DsRolePrimaryDomainInfoBasic,
  375. (LPBYTE *)&pInfo );
  376. if (dwErr != NO_ERROR)
  377. {
  378. return dwErr;
  379. }
  380. g_DsRole = pInfo->MachineRole;
  381. DsRoleFreeMemory( pInfo );
  382. // Mark the information as having been loaded
  383. //
  384. g_fMachineSkuAndRoleInitialized = TRUE;
  385. return dwErr;
  386. }