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.

586 lines
12 KiB

  1. #include "StdAfx.h"
  2. #include "Argument.h"
  3. #include "Parameter.h"
  4. #include "Migration.h"
  5. #include "Switch.h"
  6. #include "MonitorThread.h"
  7. #include "GenerateKey.h"
  8. #include "RegistryHelper.h"
  9. #include "IsAdmin.hpp"
  10. namespace PrintUsage
  11. {
  12. void __stdcall PrintADMTUsage();
  13. void __stdcall PrintUserUsage();
  14. void __stdcall PrintGroupUsage();
  15. void __stdcall PrintComputerUsage();
  16. void __stdcall PrintSecurityUsage();
  17. void __stdcall PrintServiceUsage();
  18. void __stdcall PrintReportUsage();
  19. void __stdcall PrintKeyUsage();
  20. void __stdcall PrintUsage(UINT uId[], UINT cId);
  21. void __stdcall GetString(UINT uId, LPTSTR pszBuffer, int cchBuffer);
  22. }
  23. using namespace PrintUsage;
  24. //---------------------------------------------------------------------------
  25. // Main
  26. //---------------------------------------------------------------------------
  27. int __cdecl _tmain(int argc, LPTSTR argv[])
  28. {
  29. HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  30. if (SUCCEEDED(hr))
  31. {
  32. _RPT0(_CRT_WARN, _T("{ADMT.exe}_tmain() : Enter\n"));
  33. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
  34. try
  35. {
  36. DWORD lRet = MoveRegistry();
  37. if (lRet != ERROR_SUCCESS)
  38. ThrowError(HRESULT_FROM_WIN32(lRet), IDS_E_UPDATE_REGISTRY_FAILED);
  39. CArguments aArgs(argc, argv);
  40. CParameterMap mapParams(aArgs);
  41. long lTask;
  42. if (mapParams.GetValue(SWITCH_TASK, lTask))
  43. {
  44. bool bHelp;
  45. if (!mapParams.GetValue(SWITCH_HELP, bHelp))
  46. {
  47. bHelp = false;
  48. }
  49. switch (lTask)
  50. {
  51. case TASK_USER:
  52. {
  53. if (bHelp)
  54. {
  55. PrintUserUsage();
  56. }
  57. else
  58. {
  59. CUserMigration(CMigration(mapParams), mapParams);
  60. }
  61. break;
  62. }
  63. case TASK_GROUP:
  64. {
  65. if (bHelp)
  66. {
  67. PrintGroupUsage();
  68. }
  69. else
  70. {
  71. CGroupMigration(CMigration(mapParams), mapParams);
  72. }
  73. break;
  74. }
  75. case TASK_COMPUTER:
  76. {
  77. if (bHelp)
  78. {
  79. PrintComputerUsage();
  80. }
  81. else
  82. {
  83. CComputerMigration(CMigration(mapParams), mapParams);
  84. }
  85. break;
  86. }
  87. case TASK_SECURITY:
  88. {
  89. if (bHelp)
  90. {
  91. PrintSecurityUsage();
  92. }
  93. else
  94. {
  95. CSecurityTranslation(CMigration(mapParams), mapParams);
  96. }
  97. break;
  98. }
  99. case TASK_SERVICE:
  100. {
  101. if (bHelp)
  102. {
  103. PrintServiceUsage();
  104. }
  105. else
  106. {
  107. CServiceEnumeration(CMigration(mapParams), mapParams);
  108. }
  109. break;
  110. }
  111. case TASK_REPORT:
  112. {
  113. if (bHelp)
  114. {
  115. PrintReportUsage();
  116. }
  117. else
  118. {
  119. CReportGeneration(CMigration(mapParams), mapParams);
  120. }
  121. break;
  122. }
  123. case TASK_KEY:
  124. {
  125. if (bHelp)
  126. {
  127. PrintKeyUsage();
  128. }
  129. else
  130. {
  131. lRet = IsAdminLocal();
  132. if (lRet != ERROR_SUCCESS)
  133. ThrowError(HRESULT_FROM_WIN32(lRet), IDS_E_LOCAL_ADMIN_CHECK_FAILED);
  134. _bstr_t strKeyId;
  135. if (!mapParams.GetValue(SWITCH_KEY_IDENTIFIER, strKeyId))
  136. {
  137. ThrowError(E_INVALIDARG, IDS_E_NO_KEY_DOMAIN);
  138. }
  139. _bstr_t strDrive;
  140. if (!mapParams.GetValue(SWITCH_KEY_FOLDER, strDrive))
  141. {
  142. ThrowError(E_INVALIDARG, IDS_E_NO_KEY_FOLDER);
  143. }
  144. _bstr_t strPassword;
  145. mapParams.GetValue(SWITCH_KEY_PASSWORD, strPassword);
  146. GeneratePasswordKey(strKeyId, strPassword, strDrive);
  147. }
  148. break;
  149. }
  150. default:
  151. {
  152. _ASSERT(false);
  153. break;
  154. }
  155. }
  156. }
  157. else
  158. {
  159. PrintADMTUsage();
  160. }
  161. }
  162. catch (_com_error& ce)
  163. {
  164. _com_error ceNew(ce);
  165. _bstr_t strDescription = ceNew.Description();
  166. if (!strDescription)
  167. {
  168. IErrorInfo* pErrorInfo = NULL;
  169. if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  170. {
  171. ceNew = _com_error(ceNew.Error(), pErrorInfo);
  172. }
  173. }
  174. strDescription = ceNew.Description();
  175. if (strDescription.length())
  176. {
  177. My_fwprintf(_T("%s (0x%08lX)\n"), (LPCTSTR)strDescription, ceNew.Error());
  178. }
  179. else
  180. {
  181. My_fwprintf(_T("%s (0x%08lX)\n"), (LPCTSTR)ceNew.ErrorMessage(), ceNew.Error());
  182. }
  183. }
  184. catch (...)
  185. {
  186. My_fwprintf(_T("%s (0x%08lX)\n"), _com_error(E_FAIL).ErrorMessage(), E_FAIL);
  187. }
  188. _RPT0(_CRT_WARN, _T("{ADMT.exe}_tmain() : Leave\n"));
  189. CoUninitialize();
  190. }
  191. return 0;
  192. }
  193. // Print Usage --------------------------------------------------------------
  194. namespace PrintUsage
  195. {
  196. void __stdcall PrintADMTUsage()
  197. {
  198. static UINT s_uId[] =
  199. {
  200. IDS_USAGE_SYNTAX,
  201. IDS_USAGE_ADMT,
  202. };
  203. PrintUsage(s_uId, countof(s_uId));
  204. }
  205. void __stdcall PrintUserUsage()
  206. {
  207. static UINT s_uId[] =
  208. {
  209. // user command
  210. IDS_USAGE_SYNTAX,
  211. IDS_USAGE_USER,
  212. // common options
  213. IDS_USAGE_OPTIONFILE,
  214. IDS_USAGE_TESTMIGRATION,
  215. IDS_USAGE_INTRAFOREST,
  216. IDS_USAGE_SOURCEDOMAIN,
  217. IDS_USAGE_SOURCEOU,
  218. IDS_USAGE_TARGETDOMAIN,
  219. IDS_USAGE_TARGETOU,
  220. // user migration options
  221. IDS_USAGE_PASSWORDOPTION,
  222. IDS_USAGE_PASSWORDSERVER,
  223. IDS_USAGE_PASSWORDFILE,
  224. IDS_USAGE_DISABLEOPTION,
  225. IDS_USAGE_SOURCEEXPIRATION,
  226. IDS_USAGE_MIGRATESIDS,
  227. IDS_USAGE_TRANSLATEROAMINGPROFILE,
  228. IDS_USAGE_UPDATEUSERRIGHTS,
  229. IDS_USAGE_MIGRATEGROUPS,
  230. IDS_USAGE_UPDATEPREVIOUSLYMIGRATEDOBJECTS,
  231. IDS_USAGE_FIXGROUPMEMBERSHIP,
  232. IDS_USAGE_MIGRATESERVICEACCOUNTS,
  233. IDS_USAGE_RENAMEOPTION,
  234. IDS_USAGE_RENAMEPREFIXORSUFFIX,
  235. IDS_USAGE_CONFLICTOPTIONS_U,
  236. IDS_USAGE_CONFLICTPREFIXORSUFFIX,
  237. IDS_USAGE_USERPROPERTIESTOEXCLUDE,
  238. IDS_USAGE_INETORGPERSONPROPERTIESTOEXCLUDE,
  239. IDS_USAGE_GROUPPROPERTIESTOEXCLUDE,
  240. // users to migrate
  241. IDS_USAGE_INCLUDE_A,
  242. IDS_USAGE_EXCLUDE,
  243. IDS_USAGE_FOOTER,
  244. };
  245. PrintUsage(s_uId, countof(s_uId));
  246. }
  247. void __stdcall PrintGroupUsage()
  248. {
  249. static UINT s_uId[] =
  250. {
  251. // group command
  252. IDS_USAGE_SYNTAX,
  253. IDS_USAGE_GROUP,
  254. // common options
  255. IDS_USAGE_OPTIONFILE,
  256. IDS_USAGE_TESTMIGRATION,
  257. IDS_USAGE_INTRAFOREST,
  258. IDS_USAGE_SOURCEDOMAIN,
  259. IDS_USAGE_SOURCEOU,
  260. IDS_USAGE_TARGETDOMAIN,
  261. IDS_USAGE_TARGETOU,
  262. // group migration options
  263. IDS_USAGE_UPDATEGROUPRIGHTS,
  264. IDS_USAGE_FIXGROUPMEMBERSHIP,
  265. IDS_USAGE_MIGRATESIDS,
  266. IDS_USAGE_RENAMEOPTION,
  267. IDS_USAGE_RENAMEPREFIXORSUFFIX,
  268. IDS_USAGE_CONFLICTOPTIONS_G,
  269. IDS_USAGE_CONFLICTPREFIXORSUFFIX,
  270. IDS_USAGE_GROUPPROPERTIESTOEXCLUDE,
  271. // member migration options
  272. IDS_USAGE_MIGRATEMEMBERS,
  273. IDS_USAGE_UPDATEPREVIOUSLYMIGRATEDOBJECTS,
  274. IDS_USAGE_PASSWORDOPTION,
  275. IDS_USAGE_PASSWORDSERVER,
  276. IDS_USAGE_PASSWORDFILE,
  277. IDS_USAGE_DISABLEOPTION,
  278. IDS_USAGE_SOURCEEXPIRATION,
  279. IDS_USAGE_TRANSLATEROAMINGPROFILE,
  280. IDS_USAGE_USERPROPERTIESTOEXCLUDE,
  281. IDS_USAGE_INETORGPERSONPROPERTIESTOEXCLUDE,
  282. // groups to migrate
  283. IDS_USAGE_INCLUDE_A,
  284. IDS_USAGE_EXCLUDE,
  285. IDS_USAGE_FOOTER,
  286. };
  287. PrintUsage(s_uId, countof(s_uId));
  288. }
  289. void __stdcall PrintComputerUsage()
  290. {
  291. static UINT s_uId[] =
  292. {
  293. // computer command
  294. IDS_USAGE_SYNTAX,
  295. IDS_USAGE_COMPUTER,
  296. // common options
  297. IDS_USAGE_OPTIONFILE,
  298. IDS_USAGE_TESTMIGRATION,
  299. IDS_USAGE_INTRAFOREST,
  300. IDS_USAGE_SOURCEDOMAIN,
  301. IDS_USAGE_SOURCEOU,
  302. IDS_USAGE_TARGETDOMAIN,
  303. IDS_USAGE_TARGETOU,
  304. // computer migration options
  305. IDS_USAGE_TRANSLATIONOPTION,
  306. IDS_USAGE_TRANSLATEFILESANDFOLDERS,
  307. IDS_USAGE_TRANSLATELOCALGROUPS,
  308. IDS_USAGE_TRANSLATEPRINTERS,
  309. IDS_USAGE_TRANSLATEREGISTRY,
  310. IDS_USAGE_TRANSLATESHARES,
  311. IDS_USAGE_TRANSLATEUSERPROFILES,
  312. IDS_USAGE_TRANSLATEUSERRIGHTS,
  313. IDS_USAGE_RENAMEOPTION,
  314. IDS_USAGE_RENAMEPREFIXORSUFFIX,
  315. IDS_USAGE_CONFLICTOPTIONS_C,
  316. IDS_USAGE_CONFLICTPREFIXORSUFFIX,
  317. IDS_USAGE_RESTARTDELAY,
  318. IDS_USAGE_COMPUTERPROPERTIESTOEXCLUDE,
  319. // computers to migrate
  320. IDS_USAGE_INCLUDE_A,
  321. IDS_USAGE_EXCLUDE,
  322. IDS_USAGE_FOOTER,
  323. };
  324. PrintUsage(s_uId, countof(s_uId));
  325. }
  326. void __stdcall PrintSecurityUsage()
  327. {
  328. static UINT s_uId[] =
  329. {
  330. // security command
  331. IDS_USAGE_SYNTAX,
  332. IDS_USAGE_SECURITY,
  333. // common options
  334. IDS_USAGE_OPTIONFILE,
  335. IDS_USAGE_TESTMIGRATION,
  336. IDS_USAGE_SOURCEDOMAIN,
  337. IDS_USAGE_SOURCEOU,
  338. IDS_USAGE_TARGETDOMAIN,
  339. // security migration options
  340. IDS_USAGE_TRANSLATIONOPTION,
  341. IDS_USAGE_TRANSLATEFILESANDFOLDERS,
  342. IDS_USAGE_TRANSLATELOCALGROUPS,
  343. IDS_USAGE_TRANSLATEPRINTERS,
  344. IDS_USAGE_TRANSLATEREGISTRY,
  345. IDS_USAGE_TRANSLATESHARES,
  346. IDS_USAGE_TRANSLATEUSERPROFILES,
  347. IDS_USAGE_TRANSLATEUSERRIGHTS,
  348. IDS_USAGE_SIDMAPPINGFILE,
  349. // computers to perform security translation on
  350. IDS_USAGE_INCLUDE_C,
  351. IDS_USAGE_EXCLUDE,
  352. IDS_USAGE_FOOTER,
  353. };
  354. PrintUsage(s_uId, countof(s_uId));
  355. }
  356. void __stdcall PrintServiceUsage()
  357. {
  358. static UINT s_uId[] =
  359. {
  360. // security command
  361. IDS_USAGE_SYNTAX,
  362. IDS_USAGE_SERVICE,
  363. // common options
  364. IDS_USAGE_OPTIONFILE,
  365. IDS_USAGE_SOURCEDOMAIN,
  366. IDS_USAGE_SOURCEOU,
  367. // computers to perform security translation on
  368. IDS_USAGE_INCLUDE_C,
  369. IDS_USAGE_EXCLUDE,
  370. IDS_USAGE_FOOTER,
  371. };
  372. PrintUsage(s_uId, countof(s_uId));
  373. }
  374. void __stdcall PrintReportUsage()
  375. {
  376. static UINT s_uId[] =
  377. {
  378. // report command
  379. IDS_USAGE_SYNTAX,
  380. IDS_USAGE_REPORT,
  381. // common options
  382. IDS_USAGE_OPTIONFILE,
  383. IDS_USAGE_SOURCEDOMAIN,
  384. IDS_USAGE_SOURCEOU,
  385. IDS_USAGE_TARGETDOMAIN,
  386. // report options
  387. IDS_USAGE_REPORTTYPE,
  388. IDS_USAGE_REPORTFOLDER,
  389. // computers to generate reports for
  390. IDS_USAGE_INCLUDE_D,
  391. IDS_USAGE_EXCLUDE,
  392. IDS_USAGE_FOOTER,
  393. };
  394. PrintUsage(s_uId, countof(s_uId));
  395. }
  396. void __stdcall PrintKeyUsage()
  397. {
  398. static UINT s_uId[] =
  399. {
  400. IDS_USAGE_SYNTAX,
  401. IDS_USAGE_KEY,
  402. };
  403. PrintUsage(s_uId, countof(s_uId));
  404. }
  405. void __stdcall PrintUsage(UINT uId[], UINT cId)
  406. {
  407. _TCHAR szBuffer[512];
  408. // print this is command syntax line
  409. if (cId > 0)
  410. {
  411. GetString(uId[0], szBuffer, countof(szBuffer));
  412. My_fwprintf(_T("%s\n\n"), szBuffer);
  413. }
  414. // print command
  415. if (cId > 1)
  416. {
  417. GetString(uId[1], szBuffer, countof(szBuffer));
  418. My_fwprintf(_T("%s\n\n"), szBuffer);
  419. }
  420. // print options
  421. if (cId > 2)
  422. {
  423. for (UINT i = 2; i < cId; i++)
  424. {
  425. GetString(uId[i], szBuffer, countof(szBuffer));
  426. My_fwprintf(_T("%s\n"), szBuffer);
  427. }
  428. }
  429. }
  430. void __stdcall GetString(UINT uId, LPTSTR pszBuffer, int cchBuffer)
  431. {
  432. if (pszBuffer)
  433. {
  434. if (LoadString(GetModuleHandle(NULL), uId, pszBuffer, cchBuffer) == 0)
  435. {
  436. pszBuffer[0] = _T('\0');
  437. }
  438. }
  439. }
  440. }
  441. //
  442. // Based on LDIFDE/CSVDE.
  443. //
  444. // Prints Unicode formatted string to console window using WriteConsoleW.
  445. // Note: This My_fwprintf() is used to workaround the problem in c-runtime
  446. // which looks up LC_CTYPE even for Unicode string.
  447. //
  448. int __cdecl
  449. My_fwprintf(
  450. const WCHAR *format,
  451. ...
  452. )
  453. {
  454. DWORD cchChar;
  455. va_list args;
  456. va_start( args, format );
  457. cchChar = My_vfwprintf(format, args);
  458. va_end(args);
  459. return cchChar;
  460. }
  461. int __cdecl
  462. My_vfwprintf(
  463. const WCHAR *format,
  464. va_list argptr
  465. )
  466. {
  467. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  468. DWORD currentMode;
  469. DWORD cchChar;
  470. const DWORD dwBufferMessageSize = 4096;
  471. WCHAR szBufferMessage[dwBufferMessageSize];
  472. _vsnwprintf( szBufferMessage, dwBufferMessageSize, format, argptr );
  473. szBufferMessage[dwBufferMessageSize-1] = L'\0';
  474. cchChar = wcslen(szBufferMessage);
  475. if (GetFileType(hOut) == FILE_TYPE_CHAR && GetConsoleMode(hOut, &currentMode))
  476. {
  477. WriteConsoleW(hOut, szBufferMessage, cchChar, &cchChar, NULL);
  478. }
  479. else
  480. {
  481. int charCount = WideCharToMultiByte(CP_ACP, 0, szBufferMessage, -1, 0, 0, 0, 0);
  482. char* szaStr = new char[charCount];
  483. if (szaStr != NULL)
  484. {
  485. DWORD dwBytesWritten;
  486. WideCharToMultiByte(CP_ACP, 0, szBufferMessage, -1, szaStr, charCount, 0, 0);
  487. WriteFile(hOut, szaStr, charCount - 1, &dwBytesWritten, 0);
  488. delete[] szaStr;
  489. }
  490. else
  491. {
  492. cchChar = 0;
  493. }
  494. }
  495. return cchChar;
  496. }