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.

378 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: M A I N . C P P
  7. //
  8. // Contents: Code to provide a simple cmdline interface to
  9. // the sample code functions
  10. //
  11. // Notes: The code in this file is not required to access any
  12. // netcfg functionality. It merely provides a simple cmdline
  13. // interface to the sample code functions provided in
  14. // file snetcfg.cpp.
  15. //
  16. // Author: kumarp 28-September-98
  17. //
  18. //----------------------------------------------------------------------------
  19. #include "pch.h"
  20. #pragma hdrstop
  21. #include "snetcfg.h"
  22. #include "error.h"
  23. #include <wbemcli.h>
  24. #include <winnls.h>
  25. #include "tracelog.h"
  26. BOOL g_fVerbose = FALSE;
  27. BOOL WlbsCheckSystemVersion ();
  28. BOOL WlbsCheckFiles ();
  29. HRESULT WlbsRegisterDlls ();
  30. HRESULT WlbsCompileMof ();
  31. // ----------------------------------------------------------------------
  32. //
  33. // Function: wmain
  34. //
  35. // Purpose: This is the main function for NLBWizard, which is for W2K
  36. // installations of NLB. Making this application compatible
  37. // with post-W2K releases will require changes, e.g., in
  38. // functions WlbsCheckFiles and WlbsCheckSystemVersion.
  39. //
  40. // Arguments: standard main args
  41. //
  42. // Returns: 0 on success, non-zero otherwise
  43. //
  44. // Author: kumarp 25-December-97
  45. //
  46. // Notes:
  47. //
  48. EXTERN_C int __cdecl wmain (int argc, WCHAR * argv[]) {
  49. HRESULT hr = S_OK;
  50. WCHAR ch;
  51. enum NetClass nc = NC_Unknown;
  52. WCHAR szFileFullPath[MAX_PATH+1];
  53. WCHAR szFileFullPathDest[MAX_PATH+1];
  54. PWCHAR pwc;
  55. PWSTR szFileComponent;
  56. TRACELogRegister(L"wlbs");
  57. LOG_INFO("Checking Windows version information.");
  58. if (!WlbsCheckSystemVersion()) {
  59. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_INVALID_OS);
  60. LOG_ERROR("The NLB install pack can only be used on Windows 2000 Server Service Pack 1 or higher.");
  61. goto error;
  62. }
  63. LOG_INFO("Checking for previous NLB installations.");
  64. hr = FindIfComponentInstalled(_TEXT("ms_wlbs"));
  65. if (hr == S_OK) {
  66. /* AppCenter request on 1.9.01 to revert to S_FALSE in this case due to RTM proximity. */
  67. /* hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_ALREADY_INSTALLED); */
  68. hr = S_FALSE;
  69. LOG_ERROR("Network Load Balancing Service is already installed.");
  70. goto error;
  71. }
  72. if (FAILED(hr)) {
  73. LOG_ERROR("Warning: Error querying for Network Load Balancing Service. There may be errors in this installtion.");
  74. }
  75. LOG_INFO("Checking for necessary files.");
  76. if (!WlbsCheckFiles()) {
  77. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_FILES_MISSING);
  78. LOG_ERROR1("Please install the NLB hotfix before running %ls", argv[0]);
  79. goto error;
  80. }
  81. GetModuleFileName(NULL, szFileFullPath, MAX_PATH + 1);
  82. pwc = wcsrchr(szFileFullPath, L'\\');
  83. * (pwc + 1) = L'\0';
  84. wcscat(szFileFullPath, L"netwlbs.inf");
  85. LOG_INFO("Checking language version.");
  86. switch (GetSystemDefaultLangID()) {
  87. case 0x0409: case 0x0809: case 0x0c09: case 0x1009: case 0x1409: case 0x1809: case 0x1c09:
  88. case 0x2009: case 0x2409: case 0x2809: case 0x2c09:
  89. LOG_INFO1("English version detected 0x%x.", GetSystemDefaultLangID());
  90. wcscat (szFileFullPath, L".eng");
  91. break;
  92. case 0x0411:
  93. LOG_INFO1("Japanese version detected 0x%x.", GetSystemDefaultLangID());
  94. wcscat (szFileFullPath, L".jpn");
  95. break;
  96. case 0x0407: case 0x0807: case 0x0c07: case 0x1007: case 0x1407:
  97. LOG_INFO1("German version detected 0x%x.", GetSystemDefaultLangID());
  98. wcscat (szFileFullPath, L".ger");
  99. break;
  100. case 0x040c: case 0x080c: case 0x0c0c: case 0x100c: case 0x140c:
  101. LOG_INFO1("French version detected 0x%x.", GetSystemDefaultLangID());
  102. wcscat (szFileFullPath, L".fr");
  103. break;
  104. default:
  105. LOG_INFO1("Unsupported Language.Please contact PSS for a new %ls.", argv[0]);
  106. wcscat (szFileFullPath, L".eng");
  107. break;
  108. }
  109. /* First copy the .inf file. */
  110. if (GetWindowsDirectory(szFileFullPathDest, MAX_PATH + 1) == 0)
  111. {
  112. hr = HRESULT_FROM_WIN32(GetLastError());
  113. goto error;
  114. }
  115. wcscat(szFileFullPathDest, L"\\INF\\netwlbs.inf");
  116. LOG_INFO("Copying the NLB .inf file.");
  117. if (!CopyFile(szFileFullPath, szFileFullPathDest, FALSE)) {
  118. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_INF_FAILURE);
  119. LOG_ERROR2("Warning: Unable to copy the inf file %ls %ls.", szFileFullPath, szFileFullPathDest);
  120. goto error;
  121. }
  122. /* Now install the service. */
  123. hr = HrInstallNetComponent(L"ms_wlbs", NC_NetService, szFileFullPathDest);
  124. if (!SUCCEEDED(hr)) {
  125. LOG_ERROR("Error installing Network Load Balancing.");
  126. goto error;
  127. } else {
  128. LOG_INFO("Installation of Network Load Balancing done.");
  129. }
  130. /* Change working directory to %TEMP%. Needed because of a IMofCopiler::CompileFile error on Win2K. */
  131. WCHAR * szTempDir = _wgetenv(L"TEMP");
  132. _wchdir(szTempDir);
  133. LOG_INFO("Registering NLB Dlls.");
  134. /* Register the provider .dll here. */
  135. hr = WlbsRegisterDlls();
  136. if (!SUCCEEDED(hr)) {
  137. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_REGISTER_DLL);
  138. LOG_ERROR("Error registering NLB Dlls.");
  139. goto error;
  140. }
  141. LOG_INFO("Compiling the NLB MOF.");
  142. /* Compile wlbsprov.mof here */
  143. hr = WlbsCompileMof();
  144. if (!SUCCEEDED(hr)) {
  145. hr = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_NLB, NLB_E_COMPILE_MOF);
  146. LOG_ERROR("Error compiling the NLB MOF.");
  147. goto error;
  148. }
  149. LOG_INFO("WLBS Setup successful.");
  150. return hr;
  151. error:
  152. LOG_ERROR1("WLBS Setup failed 0x%x", hr);
  153. return hr;
  154. }
  155. /* This checks whether the system on which NLB is being installed is a W2K Server or not. */
  156. BOOL WlbsCheckSystemVersion () {
  157. OSVERSIONINFOEX osinfo;
  158. osinfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  159. if (!GetVersionEx((LPOSVERSIONINFO)&osinfo)) return FALSE;
  160. /* For install, we return TRUE only if its Windows 2000 Server. */
  161. if ((osinfo.dwMajorVersion == 5) &&
  162. (osinfo.dwMinorVersion == 0) &&
  163. (osinfo.wProductType == VER_NT_SERVER) &&
  164. !(osinfo.wSuiteMask & VER_SUITE_ENTERPRISE) &&
  165. !(osinfo.wSuiteMask & VER_SUITE_DATACENTER))
  166. return TRUE;
  167. return FALSE;
  168. }
  169. BOOL WlbsCheckFiles () {
  170. WCHAR * FileList [] = {
  171. L"\\system32\\drivers\\wlbs.sys",
  172. L"\\system32\\wlbs.exe",
  173. L"\\help\\wlbs.chm",
  174. L"\\help\\wlbs.hlp",
  175. L"\\system32\\wlbsctrl.dll",
  176. L"\\system32\\wbem\\wlbsprov.dll",
  177. L"\\system32\\wbem\\wlbsprov.mof",
  178. L"\\system32\\wbem\\wlbsprov.mfl",
  179. L"\\inf\\netwlbsm.inf",
  180. NULL
  181. };
  182. WCHAR wszPath [MAX_PATH + 1];
  183. PWCHAR pwc;
  184. INT i = 0;
  185. BOOL first = FALSE;
  186. WIN32_FIND_DATA FileFind;
  187. HANDLE hdl;
  188. while (FileList [i] != NULL) {
  189. if (GetWindowsDirectory(wszPath, MAX_PATH + 1) == 0)
  190. {
  191. //
  192. // This function returns the inverse of "first" as the status. Since we have a failure here, we set first = TRUE.
  193. //
  194. first = TRUE;
  195. break;
  196. }
  197. wcscat(wszPath, FileList [i]);
  198. hdl = FindFirstFile(wszPath, & FileFind);
  199. if (hdl == INVALID_HANDLE_VALUE) {
  200. if (!first) {
  201. first = TRUE;
  202. LOG_ERROR("Error: The following files were not found:");
  203. }
  204. LOG_ERROR1("%\tls",wszPath);
  205. }
  206. if (hdl != INVALID_HANDLE_VALUE)
  207. FindClose(hdl);
  208. i++;
  209. }
  210. return !first;
  211. }
  212. HRESULT WlbsRegisterDlls () {
  213. WCHAR * DllList [] = {
  214. L"\\wbem\\wlbsprov.dll",
  215. NULL
  216. };
  217. INT i = 0;
  218. WCHAR pszDllPath [MAX_PATH + 1];
  219. HINSTANCE hLib;
  220. CHAR * pszDllEntryPoint = "DllRegisterServer";
  221. HRESULT (STDAPICALLTYPE * lpDllEntryPoint)(void);
  222. HRESULT hr = S_OK;
  223. if (!GetSystemDirectory(pszDllPath, MAX_PATH + 1)) {
  224. hr = E_UNEXPECTED;
  225. LOG_ERROR("GetSystemDirectoryFailed.");
  226. return hr;
  227. }
  228. wcscat(pszDllPath, DllList [0]);
  229. if (FAILED(hr = OleInitialize(NULL))) {
  230. LOG_ERROR("OleInitialize Failed.");
  231. return hr;
  232. }
  233. hLib = LoadLibrary(pszDllPath);
  234. if (hLib == NULL) {
  235. hr = E_UNEXPECTED;
  236. LOG_ERROR("LoadLibrary for wlbsprov.dll Failed.");
  237. goto CleanOle;
  238. }
  239. (FARPROC &)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint);
  240. if (lpDllEntryPoint == NULL) {
  241. hr = E_UNEXPECTED;
  242. LOG_ERROR("DllRegisterServer was not found.");
  243. goto CleanLib;
  244. }
  245. if (FAILED(hr = (*lpDllEntryPoint)())) {
  246. LOG_ERROR("DllRegisterServer failed.");
  247. goto CleanLib;
  248. }
  249. LOG_INFO("Dll Registration Succeeded.");
  250. CleanLib:
  251. FreeLibrary(hLib);
  252. CleanOle:
  253. OleUninitialize();
  254. return hr;
  255. }
  256. HRESULT WlbsCompileMof () {
  257. WCHAR * MofList [] = {
  258. L"\\wbem\\wlbsprov.mof",
  259. NULL
  260. };
  261. IMofCompiler * pMofComp = NULL;
  262. WBEM_COMPILE_STATUS_INFO Info;
  263. HRESULT hr = S_OK;
  264. WCHAR pszMofPath [MAX_PATH + 1];
  265. if (!GetSystemDirectory(pszMofPath, MAX_PATH + 1)) {
  266. hr = E_UNEXPECTED;
  267. LOG_ERROR("GetSystemDirectoryFailed.");
  268. return hr;
  269. }
  270. wcscat(pszMofPath, MofList [0]);
  271. hr = CoInitialize(NULL);
  272. if (FAILED(hr)) {
  273. LOG_ERROR("CoInitialize failed.");
  274. return hr;
  275. }
  276. hr = CoCreateInstance(CLSID_MofCompiler, NULL, CLSCTX_INPROC_SERVER, IID_IMofCompiler, (LPVOID *)&pMofComp);
  277. if (FAILED(hr)) {
  278. LOG_ERROR("CoCreateInstance Failed.");
  279. switch (hr) {
  280. case REGDB_E_CLASSNOTREG:
  281. LOG_ERROR("Not registered.");
  282. break;
  283. case CLASS_E_NOAGGREGATION:
  284. LOG_ERROR("No aggregration.");
  285. break;
  286. default:
  287. LOG_ERROR1("Error ox%x.", hr);
  288. break;
  289. }
  290. CoUninitialize();
  291. return hr;
  292. }
  293. hr = pMofComp->CompileFile(pszMofPath, NULL, NULL, NULL, NULL, 0, 0, 0, &Info);
  294. if (hr != WBEM_S_NO_ERROR)
  295. LOG_ERROR("Compile Failed.");
  296. pMofComp->Release();
  297. CoUninitialize();
  298. return Info.hRes;
  299. }