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.

490 lines
12 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 netcfg.cpp.
  15. //
  16. // Author: kumarp 28-September-98
  17. //
  18. // vijayj 12-November-2000
  19. // - Adapt it for WinPE network installation
  20. //
  21. //----------------------------------------------------------------------------
  22. #include "pch.h"
  23. #pragma hdrstop
  24. #include "netcfg.h"
  25. #include <string>
  26. #include "msg.h"
  27. #include <libmsg.h>
  28. // ----------------------------------------------------------------------
  29. // Global vars
  30. //
  31. BOOL g_fVerbose=FALSE;
  32. BOOL MiniNTMode = FALSE;
  33. static WCHAR* optarg;
  34. //
  35. // Global variables used to get formatted message for this program.
  36. //
  37. HMODULE ThisModule = NULL;
  38. WCHAR Message[4096];
  39. // ----------------------------------------------------------------------
  40. void ShowUsage();
  41. WCHAR getopt(ULONG Argc, WCHAR* Argv[], WCHAR* Opts);
  42. enum NetClass MapToNetClass(WCHAR ch);
  43. INT
  44. MainEntry(
  45. IN INT argc,
  46. IN WCHAR *argv[]
  47. );
  48. DWORD
  49. InstallWinPENetworkComponents(
  50. IN INT Argc,
  51. IN WCHAR *Argv[]
  52. );
  53. // ----------------------------------------------------------------------
  54. //
  55. // Function: wmain
  56. //
  57. // Purpose: The main function
  58. //
  59. // Arguments: standard main args
  60. //
  61. // Returns: 0 on success, non-zero otherwise
  62. //
  63. // Author: kumarp 25-December-97
  64. //
  65. // Notes:
  66. //
  67. EXTERN_C int __cdecl wmain(int argc, WCHAR* argv[])
  68. {
  69. INT Result = 0;
  70. ThisModule = GetModuleHandle(NULL);
  71. if ((argc > 1) && (argc < 4)) {
  72. Result = (INT)InstallWinPENetworkComponents(argc, argv);
  73. if (Result == ERROR_INVALID_DATA) {
  74. Result = MainEntry(argc, argv);
  75. }
  76. } else {
  77. Result = MainEntry(argc, argv);
  78. }
  79. return Result;
  80. }
  81. INT
  82. MainEntry(
  83. IN INT argc,
  84. IN WCHAR *argv[]
  85. )
  86. {
  87. HRESULT hr=S_OK;
  88. WCHAR ch;
  89. enum NetClass nc=NC_Unknown;
  90. // use simple cmd line parsing to get parameters for actions
  91. // we want to perform. the order of parameters supplied is significant.
  92. static const WCHAR c_szValidOptions[] =
  93. L"hH?c:C:l:L:i:I:u:U:vVp:P:s:S:b:B:q:Q:";
  94. WCHAR szFileFullPath[MAX_PATH+1];
  95. PWSTR szFileComponent;
  96. MiniNTMode = IsMiniNTMode();
  97. while (_istprint(ch = getopt(argc, argv, (WCHAR*) c_szValidOptions)))
  98. {
  99. switch (tolower(ch))
  100. {
  101. case 'q':
  102. FindIfComponentInstalled(optarg);
  103. break;
  104. case 'b':
  105. hr = HrShowBindingPathsOfComponent(optarg);
  106. break;
  107. case 'c':
  108. nc = MapToNetClass(optarg[0]);
  109. break;
  110. case 'l':
  111. wcscpy(szFileFullPath, optarg);
  112. break;
  113. case 'i':
  114. if (nc != NC_Unknown)
  115. {
  116. hr = HrInstallNetComponent(optarg, nc, szFileFullPath);
  117. }
  118. else
  119. {
  120. ShowUsage();
  121. exit(-1);
  122. }
  123. break;
  124. case 'u':
  125. hr = HrUninstallNetComponent(optarg);
  126. break;
  127. case 's':
  128. switch(tolower(optarg[0]))
  129. {
  130. case 'a':
  131. hr = HrShowNetAdapters();
  132. break;
  133. case 'n':
  134. hr = HrShowNetComponents();
  135. break;
  136. default:
  137. ShowUsage();
  138. exit(-1);
  139. break;
  140. }
  141. break;
  142. case 'v':
  143. g_fVerbose = TRUE;
  144. break;
  145. case EOF:
  146. break;
  147. default:
  148. case 'h':
  149. case '?':
  150. ShowUsage();
  151. exit(0);
  152. break;
  153. }
  154. }
  155. return hr;
  156. }
  157. //+---------------------------------------------------------------------------
  158. //
  159. // Function: MapToNetClass
  160. //
  161. // Purpose: Map a character to the corresponding net class enum
  162. //
  163. // Arguments:
  164. // ch [in] char to map
  165. //
  166. // Returns: enum for net class
  167. //
  168. // Author: kumarp 06-October-98
  169. //
  170. // Notes:
  171. //
  172. enum NetClass MapToNetClass(WCHAR ch)
  173. {
  174. switch(tolower(ch))
  175. {
  176. case 'a':
  177. return NC_NetAdapter;
  178. case 'p':
  179. return NC_NetProtocol;
  180. case 's':
  181. return NC_NetService;
  182. case 'c':
  183. return NC_NetClient;
  184. default:
  185. return NC_Unknown;
  186. }
  187. }
  188. // ----------------------------------------------------------------------
  189. //
  190. // Function: ShowUsage
  191. //
  192. // Purpose: Display program usage help
  193. //
  194. // Arguments: None
  195. //
  196. // Returns: None
  197. //
  198. // Author: kumarp 24-December-97
  199. //
  200. // Notes:
  201. //
  202. void ShowUsage()
  203. {
  204. _putts( GetFormattedMessage( ThisModule,
  205. FALSE,
  206. Message,
  207. sizeof(Message)/sizeof(Message[0]),
  208. MSG_PGM_USAGE ) );
  209. }
  210. //+---------------------------------------------------------------------------
  211. //
  212. // Function: getopt
  213. //
  214. // Purpose: Parse cmdline and return one argument each time
  215. // this function is called.
  216. //
  217. // Arguments:
  218. // Argc [in] standard main argc
  219. // Argv [in] standard main argv
  220. // Opts [in] valid options
  221. //
  222. // Returns:
  223. //
  224. // Author: kumarp 06-October-98
  225. //
  226. // Notes:
  227. //
  228. WCHAR getopt (ULONG Argc, WCHAR* Argv[], WCHAR* Opts)
  229. {
  230. static ULONG optind=1;
  231. static ULONG optcharind;
  232. static ULONG hyphen=0;
  233. WCHAR ch;
  234. WCHAR* indx;
  235. do {
  236. if (optind >= Argc) {
  237. return EOF;
  238. }
  239. ch = Argv[optind][optcharind++];
  240. if (ch == '\0') {
  241. optind++; optcharind=0;
  242. hyphen = 0;
  243. continue;
  244. }
  245. if ( hyphen || (ch == '-') || (ch == '/')) {
  246. if (!hyphen) {
  247. ch = Argv[optind][optcharind++];
  248. if (ch == '\0') {
  249. optind++;
  250. return EOF;
  251. }
  252. } else if (ch == '\0') {
  253. optind++;
  254. optcharind = 0;
  255. continue;
  256. }
  257. indx = wcschr(Opts, ch);
  258. if (indx == NULL) {
  259. continue;
  260. }
  261. if (*(indx+1) == ':') {
  262. if (Argv[optind][optcharind] != '\0'){
  263. optarg = &Argv[optind][optcharind];
  264. } else {
  265. if ((optind + 1) >= Argc ||
  266. (Argv[optind+1][0] == '-' ||
  267. Argv[optind+1][0] == '/' )) {
  268. return 0;
  269. }
  270. optarg = Argv[++optind];
  271. }
  272. optind++;
  273. hyphen = optcharind = 0;
  274. return ch;
  275. }
  276. hyphen = 1;
  277. return ch;
  278. } else {
  279. return EOF;
  280. }
  281. } while (1);
  282. }
  283. BOOL
  284. IsMiniNTMode(
  285. VOID
  286. )
  287. /*++
  288. Routine Description:
  289. Finds out if we are running under MiniNT environment
  290. Arguments:
  291. none
  292. Return value:
  293. TRUE if we are running under MiniNT environment
  294. otherwise FALSE
  295. --*/
  296. {
  297. BOOL Result = FALSE;
  298. TCHAR *MiniNTKeyName = TEXT("SYSTEM\\CurrentControlSet\\Control\\MiniNT");
  299. HKEY MiniNTKey = NULL;
  300. LONG RegResult;
  301. RegResult = RegOpenKey(HKEY_LOCAL_MACHINE,
  302. MiniNTKeyName,
  303. &MiniNTKey);
  304. if (RegResult == ERROR_SUCCESS) {
  305. Result = TRUE;
  306. RegCloseKey(MiniNTKey);
  307. }
  308. return Result;
  309. }
  310. DWORD
  311. InstallWinPENetworkComponents(
  312. IN INT Argc,
  313. IN WCHAR *Argv[]
  314. )
  315. /*++
  316. Routine Description:
  317. Installs the required network components for
  318. WinPE environment
  319. - TCP/IP Stack
  320. - NETBIOS Stack
  321. - MS Client
  322. Note : This basically calls into MainEntry(...)
  323. manipulating the arguments as though user had
  324. entered them.
  325. Arguments:
  326. Argc - Argument Count
  327. Argv - Arguments
  328. Return value:
  329. Win32 Error code
  330. --*/
  331. {
  332. DWORD Result = ERROR_INVALID_DATA;
  333. WCHAR *NetArgs[] = {
  334. TEXT("-l"),
  335. TEXT("\\inf\\nettcpip.inf"),
  336. TEXT("-c"),
  337. TEXT("p"),
  338. TEXT("-i"),
  339. TEXT("ms_tcpip"),
  340. TEXT("-l"),
  341. TEXT("\\inf\\netnb.inf"),
  342. TEXT("-c"),
  343. TEXT("s"),
  344. TEXT("-i"),
  345. TEXT("ms_netbios"),
  346. TEXT("-l"),
  347. TEXT("\\inf\\netmscli.inf"),
  348. TEXT("-c"),
  349. TEXT("c"),
  350. TEXT("-i"),
  351. TEXT("ms_msclient") };
  352. ULONG TcpIpInfIdx = 1;
  353. ULONG NetNbInfIdx = 7;
  354. ULONG MsCliInfIdx = 13;
  355. if (Argc && Argv) {
  356. bool IsWinPE = false;
  357. bool VerboseInstall = false;
  358. for (ULONG Index = 1; Argv[Index]; Index++) {
  359. if (!_wcsicmp(Argv[Index], TEXT("-winpe"))) {
  360. IsWinPE = true;
  361. } else if (!_wcsicmp(Argv[Index], TEXT("-v"))) {
  362. VerboseInstall = true;
  363. }
  364. }
  365. if (IsWinPE) {
  366. WCHAR WinDir[MAX_PATH] = {0};
  367. PWSTR VerboseArg = TEXT("-v");
  368. if (GetWindowsDirectory(WinDir, sizeof(WinDir)/sizeof(WinDir[0]))) {
  369. std::wstring TcpIpFullPath = WinDir;
  370. std::wstring NetNbFullPath = WinDir;
  371. std::wstring MsClientFullPath = WinDir;
  372. TcpIpFullPath += NetArgs[TcpIpInfIdx];
  373. NetArgs[TcpIpInfIdx] = (PWSTR)TcpIpFullPath.c_str();
  374. NetNbFullPath += NetArgs[NetNbInfIdx];
  375. NetArgs[NetNbInfIdx] = (PWSTR)NetNbFullPath.c_str();
  376. MsClientFullPath += NetArgs[MsCliInfIdx];
  377. NetArgs[MsCliInfIdx] = (PWSTR)MsClientFullPath.c_str();
  378. ULONG ArgsSize = (sizeof(NetArgs) + (sizeof(PWSTR) * 3));
  379. PWSTR *Args = (PWSTR *)(new char[ArgsSize]);
  380. ULONG NumArgs = ArgsSize / sizeof(PWSTR);
  381. if (Args) {
  382. Index = 0;
  383. Args[Index++] = Argv[0];
  384. if (VerboseInstall) {
  385. Args[Index++] = VerboseArg;
  386. }
  387. for (ULONG TempIndex = 0;
  388. (TempIndex < (sizeof(NetArgs)/sizeof(PWSTR)));
  389. TempIndex++) {
  390. Args[Index++] = NetArgs[TempIndex];
  391. }
  392. ULONG ArgCount = Index;
  393. Args[Index++] = NULL;
  394. Result = MainEntry(ArgCount, Args);
  395. delete [](PSTR)Args;
  396. } else {
  397. Result = GetLastError();
  398. }
  399. } else {
  400. Result = GetLastError();
  401. }
  402. }
  403. }
  404. return Result;
  405. }