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.

412 lines
12 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright(C) 1999-2000 Microsoft Corporation all rights reserved.
  4. //
  5. // Module: utils.cpp
  6. //
  7. // Project: Windows 2000 IAS
  8. //
  9. // Description: IAS NT 4 to IAS W2K Migration Utility Functions
  10. //
  11. // Author: TLP 1/13/1999
  12. //
  13. //
  14. // Revision 02/24/2000 Moved to a separate dll
  15. //
  16. // TODO: IsWhistler() put the correct minorversion for Whistler RTM.
  17. // i.e. if that's Win2k 5.1 ?
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "lm.h"
  21. #ifndef celems
  22. #define celems(_x) (sizeof(_x) / sizeof(_x[0]))
  23. #endif
  24. const WCHAR *CUtils::m_FilesToDelete[] =
  25. {
  26. L"_adminui.mdb",
  27. L"_adminui.ldb",
  28. L"actlapi.dll",
  29. L"actlapi2.dll",
  30. L"adminui.chm",
  31. L"adminui.cnt",
  32. L"adminui.exe",
  33. L"adminui.gid",
  34. L"adminui.hlp",
  35. L"authdll.dll",
  36. L"authdll2.dll",
  37. L"authodbc.dll",
  38. L"authperf.dll",
  39. L"authperf.h",
  40. L"authperf.ini",
  41. L"authsam.dll",
  42. L"authsrv.exe",
  43. L"client",
  44. L"clients",
  45. L"dbcon.dll",
  46. L"dictionary",
  47. L"hhctrl.ocx",
  48. L"hhwrap.dll",
  49. L"iasconf.dll",
  50. L"radcfg.dll",
  51. L"radlog.dll",
  52. L"radstp.dll",
  53. L"user",
  54. L"users",
  55. };
  56. const int CUtils::m_NbFilesToDelete = celems(m_FilesToDelete);
  57. const WCHAR CUtils::IAS_KEY[] = L"SYSTEM\\CurrentControlSet\\Services\\"
  58. L"RemoteAccess\\Policy";
  59. const WCHAR CUtils::AUTHSRV_PARAMETERS_KEY[] = L"SYSTEM\\CurrentControlSet\\"
  60. L"Services\\AuthSrv\\Parameters";
  61. const WCHAR CUtils::SERVICES_KEY[] = L"SYSTEM\\CurrentControlSet\\"
  62. L"Services";
  63. CUtils CUtils::_instance;
  64. CUtils& CUtils::Instance()
  65. {
  66. return _instance;
  67. }
  68. //////////////////////////////////////////////////////////////////////////////
  69. // Constructor
  70. // Init the BOOL static variables
  71. //////////////////////////////////////////////////////////////////////////////
  72. CUtils::CUtils()
  73. :m_IsNT4ISP(FALSE),
  74. m_IsNT4CORP(FALSE),
  75. m_OverrideUserNameSet(FALSE),
  76. m_UserIdentityAttributeSet(FALSE),
  77. m_UserIdentityAttribute(RADIUS_ATTRIBUTE_USER_NAME)
  78. {
  79. GetVersion();
  80. GetRealmParameters();
  81. };
  82. //////////////////////////////////////////////////////////////////////////////
  83. // GetAuthSrvDirectory
  84. //////////////////////////////////////////////////////////////////////////////
  85. LONG CUtils::GetAuthSrvDirectory(/*[out]*/ _bstr_t& pszDir) const
  86. {
  87. static _bstr_t AuthSrvDirString;
  88. static BOOL AuthSrvDirValid = FALSE;
  89. LONG lResult = ERROR_SUCCESS;
  90. if ( !AuthSrvDirValid )
  91. {
  92. CRegKey Key;
  93. lResult = Key.Open(
  94. HKEY_LOCAL_MACHINE,
  95. AUTHSRV_PARAMETERS_KEY,
  96. KEY_READ
  97. );
  98. if ( lResult == ERROR_SUCCESS )
  99. {
  100. DWORD dwAuthSrvDirLength = MAX_PATH;
  101. WCHAR TempString[MAX_PATH];
  102. lResult = Key.QueryValue(
  103. TempString,
  104. L"DefaultDirectory",
  105. &dwAuthSrvDirLength
  106. );
  107. if ( lResult == ERROR_SUCCESS )
  108. {
  109. AuthSrvDirString = TempString;
  110. AuthSrvDirValid = TRUE;
  111. }
  112. Key.Close();
  113. }
  114. }
  115. if ( lResult == ERROR_SUCCESS )
  116. {
  117. pszDir = AuthSrvDirString;
  118. }
  119. return lResult;
  120. }
  121. //////////////////////////////////////////////////////////////////////////////
  122. // GetIASDirectory
  123. //////////////////////////////////////////////////////////////////////////////
  124. LONG CUtils::GetIAS2Directory(/*[in]*/ _bstr_t& pszDir) const
  125. {
  126. static _bstr_t IASDirString;
  127. static BOOL IASDirValid = FALSE;
  128. LONG lResult = ERROR_SUCCESS;
  129. if ( ! IASDirValid )
  130. {
  131. CRegKey Key;
  132. lResult = Key.Open(
  133. HKEY_LOCAL_MACHINE,
  134. IAS_KEY,
  135. KEY_READ
  136. );
  137. if ( lResult == ERROR_SUCCESS )
  138. {
  139. DWORD IASDirLength = MAX_PATH;
  140. WCHAR TempString[MAX_PATH];
  141. lResult = Key.QueryValue(
  142. TempString,
  143. L"ProductDir",
  144. &IASDirLength
  145. );
  146. if ( lResult == ERROR_SUCCESS )
  147. {
  148. IASDirString = TempString;
  149. IASDirValid = TRUE;
  150. }
  151. Key.Close();
  152. }
  153. }
  154. if ( lResult == ERROR_SUCCESS )
  155. {
  156. pszDir = IASDirString;
  157. }
  158. return lResult;
  159. }
  160. //////////////////////////////////////////////////////////////////////////////
  161. // DeleteOldIASFiles
  162. //////////////////////////////////////////////////////////////////////////////
  163. void CUtils::DeleteOldIASFiles()
  164. {
  165. do
  166. {
  167. _bstr_t szAdminuiDirectory;
  168. LONG lResult = GetAuthSrvDirectory(szAdminuiDirectory);
  169. if ( lResult != ERROR_SUCCESS )
  170. {
  171. break;
  172. }
  173. _bstr_t szAdminuiMDB = szAdminuiDirectory;
  174. szAdminuiMDB += L"\\";
  175. _bstr_t szTempString;
  176. for (int i=0; i < m_NbFilesToDelete; ++i)
  177. {
  178. szTempString = szAdminuiMDB;
  179. szTempString += m_FilesToDelete[i];
  180. DeleteFile(szTempString); // result ignored
  181. }
  182. RemoveDirectoryW(szAdminuiDirectory); // result ignored
  183. // Delete the share
  184. // Return value ignored (nothing to do if that doesn't work)
  185. // const string used when LPWSTR expected
  186. NetShareDel(NULL, L"IAS1$", 0);
  187. }
  188. while (FALSE);
  189. }
  190. //////////////////////////////////////////////////////////////////////////////
  191. // GetVersion
  192. //
  193. // No Key = Win2k or whistler
  194. // Version = CORP: IAS 1.0 without Proxy
  195. // Version = ISP: IAS 1.0 MCIS (Proxy)
  196. //////////////////////////////////////////////////////////////////////////////
  197. void CUtils::GetVersion()
  198. {
  199. CRegKey Key;
  200. LONG lResult = Key.Open(
  201. HKEY_LOCAL_MACHINE,
  202. AUTHSRV_PARAMETERS_KEY,
  203. KEY_READ
  204. );
  205. if ( lResult != ERROR_SUCCESS )
  206. {
  207. // IsWhistler will really check what version of IAS is installed
  208. // the only sure thing here is that it isn't NT4
  209. return;
  210. }
  211. DWORD dwAuthSrvDirLength = MAX_PATH;
  212. WCHAR TempString[MAX_PATH];
  213. lResult = Key.QueryValue(
  214. TempString,
  215. L"Version",
  216. &dwAuthSrvDirLength
  217. );
  218. if ( lResult == ERROR_SUCCESS )
  219. {
  220. _bstr_t Value = TempString;
  221. _bstr_t Isp = L"ISP";
  222. _bstr_t Corp = L"CORP";
  223. if ( Value == Isp )
  224. {
  225. m_IsNT4ISP = TRUE;
  226. }
  227. else if ( Value == Corp )
  228. {
  229. m_IsNT4CORP = TRUE;
  230. }
  231. // else it isn't NT4
  232. }
  233. Key.Close();
  234. }
  235. //////////////////////////////////////////////////////////////////////////////
  236. // IsNT4Corp
  237. //////////////////////////////////////////////////////////////////////////////
  238. BOOL CUtils::IsNT4Corp() const
  239. {
  240. return m_IsNT4CORP;
  241. }
  242. //////////////////////////////////////////////////////////////////////////////
  243. // IsNT4Isp
  244. //////////////////////////////////////////////////////////////////////////////
  245. BOOL CUtils::IsNT4Isp() const
  246. {
  247. return m_IsNT4ISP;
  248. }
  249. //////////////////////////////////////////////////////////////////////////////
  250. // IsWhistler
  251. //
  252. // cheapper to check the OS version than check the Database Version number.
  253. // TODO: put the correct minorversion for Whistler RTM.
  254. // i.e. if that's Win2k 5.1 ?
  255. //////////////////////////////////////////////////////////////////////////////
  256. BOOL CUtils::IsWhistler() const
  257. {
  258. OSVERSIONINFOEX osvi;
  259. DWORDLONG dwlConditionMask = 0;
  260. // Initialize the OSVERSIONINFOEX structure.
  261. ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
  262. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  263. osvi.dwMajorVersion = 5;
  264. osvi.dwMinorVersion = 0;
  265. osvi.dwBuildNumber = 2195;
  266. // Initialize the condition mask.
  267. // at least Win2k RTM
  268. VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL);
  269. VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL);
  270. // but with a greater build number than RTM
  271. // that will have to be set properly for Whistler RTM
  272. VER_SET_CONDITION(dwlConditionMask, VER_BUILDNUMBER, VER_GREATER);
  273. // Perform the test.
  274. return VerifyVersionInfo(
  275. &osvi,
  276. VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER,
  277. dwlConditionMask);
  278. }
  279. //////////////////////////////////////////////////////////////////////////////
  280. // OverrideUserNameSet
  281. //////////////////////////////////////////////////////////////////////////////
  282. BOOL CUtils::OverrideUserNameSet() const
  283. {
  284. return m_OverrideUserNameSet;
  285. }
  286. //////////////////////////////////////////////////////////////////////////////
  287. // GetUserIdentityAttribute
  288. //////////////////////////////////////////////////////////////////////////////
  289. DWORD CUtils::GetUserIdentityAttribute() const
  290. {
  291. return m_UserIdentityAttribute;
  292. }
  293. //////////////////////////////////////////////////////////////////////////////
  294. // GetRealmParameters
  295. //////////////////////////////////////////////////////////////////////////////
  296. void CUtils::GetRealmParameters()
  297. {
  298. const WCHAR USER_IDENTITY_ATTRIBUTE[] = L"User Identity Attribute";
  299. const WCHAR OVERRIDE_USER_NAME[] = L"Override User-Name";
  300. CRegKey Key;
  301. LONG lResult = Key.Open(
  302. HKEY_LOCAL_MACHINE,
  303. IAS_KEY,
  304. KEY_READ
  305. );
  306. if ( lResult == ERROR_SUCCESS )
  307. {
  308. lResult = Key.QueryValue(
  309. m_UserIdentityAttribute,
  310. USER_IDENTITY_ATTRIBUTE
  311. );
  312. if ( lResult == ERROR_SUCCESS )
  313. {
  314. m_UserIdentityAttributeSet = TRUE;
  315. }
  316. DWORD Override;
  317. lResult = Key.QueryValue(
  318. Override,
  319. OVERRIDE_USER_NAME
  320. );
  321. if ( lResult == ERROR_SUCCESS )
  322. {
  323. Override ? m_OverrideUserNameSet = TRUE
  324. : m_OverrideUserNameSet = FALSE;
  325. }
  326. Key.Close();
  327. }
  328. }
  329. //////////////////////////////////////////////////////////////////////////////
  330. // NewGetAuthSrvParameter
  331. //////////////////////////////////////////////////////////////////////////////
  332. void CUtils::NewGetAuthSrvParameter(
  333. /*[in]*/ LPCWSTR szParameterName,
  334. /*[out]*/ DWORD& DwordValue
  335. ) const
  336. {
  337. CRegKey Key;
  338. LONG Result = Key.Open(
  339. HKEY_LOCAL_MACHINE,
  340. AUTHSRV_PARAMETERS_KEY,
  341. KEY_READ
  342. );
  343. if ( Result != ERROR_SUCCESS )
  344. {
  345. _com_issue_error(E_ABORT);
  346. }
  347. Result = Key.QueryValue(DwordValue, szParameterName);
  348. if ( Result != ERROR_SUCCESS )
  349. {
  350. _com_issue_error(E_ABORT);
  351. }
  352. }
  353. //////////////////////////////////////////////////////////////////////////////
  354. // UserIdentityAttributeSet
  355. //////////////////////////////////////////////////////////////////////////////
  356. BOOL CUtils::UserIdentityAttributeSet() const
  357. {
  358. return m_UserIdentityAttributeSet;
  359. }