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.

478 lines
18 KiB

  1. // File: rdswiz.cpp
  2. #include "precomp.h"
  3. #include "confcpl.h"
  4. #include "conf.h"
  5. #include "nmremote.h"
  6. #define NUM_RDSPAGES 4
  7. VOID EnableRDS(BOOL fEnabledRDS);
  8. INT_PTR CALLBACK RemotePasswordDlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  9. extern int MessageBoxResource(HWND hwnd, UINT uMessage, UINT uTitle, UINT uFlags);
  10. INT_PTR CALLBACK RDSWizard0Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  11. INT_PTR CALLBACK RDSWizard1Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  12. INT_PTR CALLBACK RDSWizard2Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  13. INT_PTR CALLBACK RDSWizard3Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam);
  14. typedef struct _RDSWIZINFO
  15. {
  16. BOOL fEnabledRDS;
  17. PBYTE pbHashedPassword;
  18. DWORD cbHashedPassword;
  19. CHash* pHash;
  20. } RDSWIZINFO, *PRDSWIZINFO;
  21. BOOL IntCreateRDSWizard(HWND hwndOwner)
  22. {
  23. UINT uNumPages = 0;
  24. LPPROPSHEETPAGE ppsp = new PROPSHEETPAGE[NUM_RDSPAGES];
  25. if (NULL == ppsp)
  26. {
  27. ERROR_OUT(("IntCreateRDSWizard: fail to allocate memory"));
  28. return FALSE;
  29. }
  30. CHash hashObject;
  31. RDSWIZINFO rdsconfig = { FALSE, NULL, 0, &hashObject };
  32. FillInPropertyPage(&ppsp[uNumPages++], IDD_RDSWIZ_INTRO, RDSWizard0Proc, (LPARAM) &rdsconfig);
  33. FillInPropertyPage(&ppsp[uNumPages++], ::IsWindowsNT() ? IDD_RDSWIZ_NTPASSWORDS : IDD_RDSWIZ_W98PASSWORD, RDSWizard1Proc, (LPARAM) &rdsconfig);
  34. FillInPropertyPage(&ppsp[uNumPages++], IDD_RDSWIZ_SCRNSVR, RDSWizard2Proc, (LPARAM) &rdsconfig);
  35. FillInPropertyPage(&ppsp[uNumPages++], IDD_RDSWIZ_CONGRATS, RDSWizard3Proc, (LPARAM) &rdsconfig);
  36. PROPSHEETHEADER psh;
  37. InitStruct(&psh);
  38. psh.dwFlags = PSH_PROPSHEETPAGE | PSH_WIZARD | PSH_NOAPPLYNOW;
  39. psh.hwndParent = hwndOwner;
  40. psh.nPages = uNumPages;
  41. psh.ppsp = ppsp;
  42. INT_PTR iRet = PropertySheet(&psh);
  43. if (-1 == iRet)
  44. {
  45. ERROR_OUT(("IntCreateRDSWizard: fail to create PropertySheet"));
  46. return FALSE;
  47. }
  48. delete []ppsp;
  49. return TRUE;
  50. }
  51. INT_PTR CALLBACK RDSWizard0Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  52. {
  53. static PRDSWIZINFO prds;
  54. switch(iMsg)
  55. {
  56. case WM_INITDIALOG:
  57. {
  58. prds = (PRDSWIZINFO ) ((PROPSHEETPAGE *)lParam)->lParam;
  59. ASSERT(prds);
  60. return TRUE;
  61. }
  62. case WM_NOTIFY:
  63. {
  64. switch (((NMHDR FAR *) lParam)->code)
  65. {
  66. case PSN_SETACTIVE:
  67. {
  68. PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_NEXT);
  69. break;
  70. }
  71. case PSN_WIZNEXT:
  72. {
  73. prds->fEnabledRDS = TRUE;
  74. break;
  75. }
  76. default:
  77. {
  78. break;
  79. }
  80. }
  81. break;
  82. }
  83. }
  84. return FALSE;
  85. }
  86. const int MAXPASSWORDLENGTH = 36;
  87. const int MINPASSWORDLENGTH = 7;
  88. INT_PTR CALLBACK RDSWizard1Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  89. {
  90. HWND hOldPasswordText, hNewPasswordText, hVerificationText;
  91. int nOldPasswordLength, nNewPasswordLength, nVerificationLength;
  92. CHAR lpOldPassword[MAXPASSWORDLENGTH], lpNewPassword[MAXPASSWORDLENGTH], lpVerification[MAXPASSWORDLENGTH];
  93. WCHAR lpwszOldPassword[MAXPASSWORDLENGTH], lpwszNewPassword[MAXPASSWORDLENGTH];
  94. PBYTE pbRegPassword;
  95. DWORD cbRegPassword;
  96. static PRDSWIZINFO prds;
  97. switch(iMsg)
  98. {
  99. case WM_INITDIALOG:
  100. {
  101. prds = (PRDSWIZINFO )((PROPSHEETPAGE*)lParam)->lParam;
  102. if (!::IsWindowsNT())
  103. {
  104. ASSERT(prds->pHash);
  105. RegEntry reLM(REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  106. cbRegPassword = reLM.GetBinary(REMOTE_REG_PASSWORD, (void **)&pbRegPassword);
  107. if (0 == cbRegPassword)
  108. {
  109. EnableWindow(GetDlgItem(hDlg, IDC_EDIT_RDSPASSWORDO), FALSE);
  110. SetFocus(GetDlgItem(hDlg, IDC_EDIT_RDSPASSWORD));
  111. }
  112. SendDlgItemMessage(hDlg, IDC_EDIT_RDSPASSWORDO, EM_LIMITTEXT,MAXPASSWORDLENGTH - 1, 0);
  113. SendDlgItemMessage(hDlg, IDC_EDIT_RDSPASSWORD, EM_LIMITTEXT,MAXPASSWORDLENGTH - 1, 0);
  114. SendDlgItemMessage(hDlg, IDC_EDIT_RDSPASSWORDV, EM_LIMITTEXT,MAXPASSWORDLENGTH - 1, 0);
  115. }
  116. return TRUE;
  117. }
  118. case WM_NOTIFY:
  119. {
  120. switch (((NMHDR FAR *) lParam)->code)
  121. {
  122. case PSN_SETACTIVE:
  123. {
  124. PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK | PSWIZB_NEXT);
  125. break;
  126. }
  127. case PSN_WIZNEXT:
  128. {
  129. if (!::IsWindowsNT())
  130. {
  131. PBYTE pbHashedPassword = NULL;
  132. DWORD cbHashedPassword = 0;
  133. RegEntry reLM( REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  134. hOldPasswordText = GetDlgItem(hDlg, IDC_EDIT_RDSPASSWORDO);
  135. hNewPasswordText = GetDlgItem(hDlg, IDC_EDIT_RDSPASSWORD);
  136. hVerificationText = GetDlgItem(hDlg, IDC_EDIT_RDSPASSWORDV);
  137. nOldPasswordLength = GetWindowText(hOldPasswordText,lpOldPassword,MAXPASSWORDLENGTH);
  138. MultiByteToWideChar(CP_ACP, 0, lpOldPassword, -1, lpwszOldPassword, MAXPASSWORDLENGTH);
  139. cbRegPassword = reLM.GetBinary(REMOTE_REG_PASSWORD, (void **)&pbRegPassword);
  140. cbHashedPassword = prds->pHash->GetHashedData((LPBYTE)lpwszOldPassword,
  141. sizeof(WCHAR)*strlen(lpOldPassword),
  142. (void **)&pbHashedPassword);
  143. if (0 != cbRegPassword && !(cbHashedPassword == cbRegPassword && 0 == memcmp(pbHashedPassword,pbRegPassword,cbHashedPassword)))
  144. {
  145. // Error Case - Old password incorrect.
  146. MessageBoxResource(hDlg,IDS_REMOTE_OLD_PASSWORD_WRONG_TEXT,IDS_REMOTE_OLD_PASSWORD_WRONG_TITLE,MB_OK | MB_ICONERROR);
  147. SetWindowText(hOldPasswordText,NULL);
  148. SetWindowText(hNewPasswordText,NULL);
  149. SetWindowText(hVerificationText,NULL);
  150. SetFocus(hOldPasswordText);
  151. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  152. break;
  153. }
  154. nNewPasswordLength = GetWindowText(hNewPasswordText,lpNewPassword,MAXPASSWORDLENGTH);
  155. nVerificationLength = GetWindowText(hVerificationText,lpVerification,MAXPASSWORDLENGTH);
  156. if (0 != lstrcmp(lpNewPassword, lpVerification))
  157. {
  158. MessageBoxResource(hDlg,IDS_REMOTE_NEW_PASSWORD_WRONG_TEXT,IDS_REMOTE_NEW_PASSWORD_WRONG_TITLE,MB_OK | MB_ICONERROR);
  159. SetWindowText(hNewPasswordText,NULL);
  160. SetWindowText(hVerificationText,NULL);
  161. SetFocus(hNewPasswordText);
  162. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  163. break;
  164. }
  165. if (nNewPasswordLength < MINPASSWORDLENGTH)
  166. {
  167. MessageBoxResource(hDlg,IDS_REMOTE_NEW_PASSWORD_LENGTH_TEXT,IDS_REMOTE_NEW_PASSWORD_LENGTH_TITLE,MB_OK | MB_ICONERROR);
  168. SetWindowText(hNewPasswordText,NULL);
  169. SetWindowText(hVerificationText,NULL);
  170. SetFocus(hNewPasswordText);
  171. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  172. break;
  173. }
  174. if (!lstrlen(lpVerification)) {
  175. // Don't allow empty password
  176. MessageBoxResource(hDlg,IDS_REMOTE_NEW_PASSWORD_EMPTY,IDS_REMOTE_NEW_PASSWORD_WRONG_TITLE,MB_OK | MB_ICONERROR);
  177. SetWindowText(hNewPasswordText,NULL);
  178. SetWindowText(hVerificationText,NULL);
  179. SetFocus(hNewPasswordText);
  180. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  181. break;
  182. }
  183. if (!FAnsiSz(lpNewPassword)) {
  184. // Error Case - T.120 can't handle UNICODE passwords
  185. MessageBoxResource(hDlg,IDS_REMOTE_NEW_PASSWORD_INVALID_TEXT,IDS_REMOTE_NEW_PASSWORD_WRONG_TITLE,MB_OK | MB_ICONERROR);
  186. SetWindowText(hNewPasswordText,NULL);
  187. SetWindowText(hVerificationText,NULL);
  188. SetFocus(hNewPasswordText);
  189. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, -1);
  190. break;
  191. }
  192. MultiByteToWideChar(CP_ACP, 0, lpNewPassword, -1, lpwszNewPassword, MAXPASSWORDLENGTH);
  193. prds->cbHashedPassword = prds->pHash->GetHashedData((LPBYTE)lpwszNewPassword, sizeof(WCHAR)*lstrlen(lpNewPassword), (void **)&prds->pbHashedPassword);
  194. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, 0);
  195. }
  196. break;
  197. }
  198. default:
  199. {
  200. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, 0);
  201. break;
  202. }
  203. }
  204. return TRUE;
  205. }
  206. }
  207. return FALSE;
  208. }
  209. INT_PTR CALLBACK RDSWizard2Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  210. {
  211. static PRDSWIZINFO prds;
  212. switch(iMsg)
  213. {
  214. case WM_INITDIALOG:
  215. {
  216. prds = (PRDSWIZINFO )((PROPSHEETPAGE*)lParam)->lParam;
  217. SendDlgItemMessage(hDlg, IDC_SCRSAVER_NOW, BM_SETCHECK, 1, 0);
  218. return TRUE;
  219. }
  220. case WM_NOTIFY:
  221. {
  222. switch (((NMHDR FAR *) lParam)->code)
  223. {
  224. case PSN_SETACTIVE:
  225. {
  226. PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK | PSWIZB_NEXT);
  227. break;
  228. }
  229. case PSN_WIZNEXT:
  230. {
  231. if (BST_CHECKED == IsDlgButtonChecked(hDlg, IDC_SCRSAVER_NOW))
  232. {
  233. WinExec("Rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,1", SW_SHOWDEFAULT);
  234. }
  235. break;
  236. }
  237. }
  238. break;
  239. }
  240. }
  241. return FALSE;
  242. }
  243. INT_PTR CALLBACK RDSWizard3Proc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  244. {
  245. static PRDSWIZINFO prds;
  246. switch(iMsg)
  247. {
  248. case WM_INITDIALOG:
  249. {
  250. prds = (PRDSWIZINFO ) ((PROPSHEETPAGE*)lParam)->lParam;
  251. PropSheet_SetWizButtons(hDlg, PSWIZB_FINISH);
  252. return TRUE;
  253. }
  254. case WM_NOTIFY:
  255. {
  256. switch (((NMHDR FAR *) lParam)->code)
  257. {
  258. case PSN_SETACTIVE:
  259. {
  260. PropSheet_SetWizButtons(GetParent(hDlg), PSWIZB_BACK | PSWIZB_FINISH);
  261. break;
  262. }
  263. case PSN_WIZFINISH:
  264. {
  265. ASSERT(prds->fEnabledRDS);
  266. EnableRDS(prds->fEnabledRDS);
  267. if (!::IsWindowsNT())
  268. {
  269. ASSERT(prds->pbHashedPassword);
  270. ASSERT(prds->cbHashedPassword);
  271. RegEntry reLM(REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  272. reLM.SetValue(REMOTE_REG_PASSWORD,prds->pbHashedPassword,prds->cbHashedPassword);
  273. }
  274. break;
  275. }
  276. }
  277. break;
  278. }
  279. }
  280. return FALSE;
  281. }
  282. INT_PTR CALLBACK RDSSettingDlgProc(HWND hDlg, UINT iMsg, WPARAM wParam, LPARAM lParam)
  283. {
  284. switch(iMsg)
  285. {
  286. case WM_INITDIALOG:
  287. {
  288. if (::IsWindowsNT())
  289. {
  290. ShowWindow(GetDlgItem(hDlg, IDC_RDS_PASSWORD), SW_HIDE);
  291. }
  292. RegEntry reLM( REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  293. if (reLM.GetNumber(REMOTE_REG_RUNSERVICE, DEFAULT_REMOTE_RUNSERVICE))
  294. {
  295. SendDlgItemMessage(hDlg, IDC_RDS_RUNRDS, BM_SETCHECK, 1, 0);
  296. }
  297. return TRUE;
  298. }
  299. case WM_COMMAND:
  300. {
  301. switch (LOWORD(wParam))
  302. {
  303. case IDOK:
  304. {
  305. EnableRDS(BST_CHECKED==IsDlgButtonChecked(hDlg, IDC_RDS_RUNRDS));
  306. EndDialog(hDlg, 1);
  307. break;
  308. }
  309. case IDCANCEL:
  310. EndDialog(hDlg, 0);
  311. break;
  312. case IDC_RDS_PASSWORD:
  313. DialogBox(::GetInstanceHandle(), MAKEINTRESOURCE(IDD_REMOTE_PASSWORD), hDlg, RemotePasswordDlgProc);
  314. break;
  315. case IDC_BUTTON_RDSWIZ:
  316. {
  317. IntCreateRDSWizard(hDlg);
  318. RegEntry reLM( REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  319. SendDlgItemMessage(hDlg, IDC_RDS_RUNRDS, BM_SETCHECK, reLM.GetNumber(REMOTE_REG_RUNSERVICE, DEFAULT_REMOTE_RUNSERVICE) ? 1 : 0, 0);
  320. break;
  321. }
  322. default:
  323. break;
  324. }
  325. break;
  326. }
  327. default:
  328. break;
  329. }
  330. return FALSE;
  331. }
  332. VOID EnableRDS(BOOL fEnabledRDS)
  333. {
  334. //
  335. // Can RDS be touched? Check policies and app sharing state.
  336. //
  337. if (ConfPolicies::IsRDSDisabled())
  338. return;
  339. if (::IsWindowsNT())
  340. {
  341. if (!g_fNTDisplayDriverEnabled)
  342. return;
  343. }
  344. else
  345. {
  346. if (ConfPolicies::IsRDSDisabledOnWin9x())
  347. return;
  348. }
  349. {
  350. RegEntry reLM( REMOTECONTROL_KEY, HKEY_LOCAL_MACHINE);
  351. reLM.SetValue(REMOTE_REG_RUNSERVICE, fEnabledRDS ? 1 : 0);
  352. reLM.SetValue(REMOTE_REG_ACTIVATESERVICE, (ULONG)0);
  353. if (::IsWindowsNT())
  354. {
  355. SC_HANDLE hSCManager = NULL;
  356. SC_HANDLE hRemoteControl = NULL;
  357. SERVICE_STATUS serviceStatus;
  358. hSCManager = OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
  359. hRemoteControl = OpenService(hSCManager,REMOTE_CONTROL_NAME,SERVICE_ALL_ACCESS);
  360. if (hSCManager)
  361. {
  362. if (hRemoteControl)
  363. {
  364. ChangeServiceConfig(hRemoteControl,
  365. SERVICE_NO_CHANGE,
  366. fEnabledRDS ? SERVICE_AUTO_START : SERVICE_DEMAND_START,
  367. SERVICE_NO_CHANGE,
  368. NULL,
  369. NULL,
  370. NULL,
  371. NULL,
  372. NULL,
  373. NULL,
  374. NULL);
  375. BOOL fSuccess = QueryServiceStatus(hRemoteControl, &serviceStatus);
  376. if (fEnabledRDS)
  377. {
  378. if (fSuccess && SERVICE_STOPPED == serviceStatus.dwCurrentState)
  379. StartService(hRemoteControl,0,NULL);
  380. }
  381. else
  382. {
  383. if (fSuccess && SERVICE_PAUSED == serviceStatus.dwCurrentState)
  384. {
  385. if (!ControlService(hRemoteControl,
  386. SERVICE_CONTROL_STOP, &serviceStatus))
  387. {
  388. WARNING_OUT(("EnableRDS: ControlService failed %d (%x)",
  389. GetLastError(), GetLastError() ));
  390. }
  391. }
  392. }
  393. CloseServiceHandle(hRemoteControl);
  394. }
  395. else
  396. {
  397. WARNING_OUT(("EnableRDS: OpenService failed %x", GetLastError() ));
  398. }
  399. CloseServiceHandle(hSCManager);
  400. }
  401. else
  402. {
  403. ERROR_OUT(("EnableRDS: OpenSCManager failed: %x", GetLastError() ));
  404. }
  405. }
  406. else
  407. {
  408. RegEntry reServiceKey(WIN95_SERVICE_KEY, HKEY_LOCAL_MACHINE);
  409. HANDLE hServiceEvent = OpenEvent(EVENT_ALL_ACCESS, FALSE, SERVICE_STOP_EVENT);
  410. if ( fEnabledRDS )
  411. {
  412. TCHAR szSystemDir[MAX_PATH];
  413. if (GetSystemDirectory(szSystemDir,MAX_PATH)) {
  414. // Only append '\' if system dir is not root
  415. if ( lstrlen(szSystemDir) > 3 )
  416. lstrcat(szSystemDir, _T("\\"));
  417. lstrcat(szSystemDir,WIN95_SERVICE_APP_NAME);
  418. reServiceKey.SetValue(REMOTE_REG_RUNSERVICE,
  419. szSystemDir);
  420. }
  421. if (NULL == hServiceEvent)
  422. WinExec(WIN95_SERVICE_APP_NAME,SW_SHOWNORMAL);
  423. }
  424. else
  425. {
  426. reServiceKey.DeleteValue(REMOTE_REG_RUNSERVICE);
  427. if (hServiceEvent)
  428. {
  429. SetEvent(hServiceEvent);
  430. }
  431. }
  432. CloseHandle(hServiceEvent);
  433. }
  434. }
  435. }
  436.