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.

795 lines
17 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // Tab state
  4. //
  5. // 3-11-98 sburns
  6. #include "precomp.h"
  7. #include "state.h"
  8. #include "resource.h"
  9. #include "common.h"
  10. //#include "cred.h"
  11. class Settings
  12. {
  13. public:
  14. void Refresh();
  15. CHString ComputerDomainDNSName;
  16. CHString DomainName;
  17. CHString FullComputerName;
  18. CHString ShortComputerName;
  19. CHString NetBIOSComputerName;
  20. bool SyncDNSNames;
  21. bool JoinedToWorkgroup;
  22. bool NeedsReboot;
  23. };
  24. //=========================================================
  25. static bool machine_is_dc = false;
  26. static bool networking_installed = false;
  27. static Settings original;
  28. static Settings current;
  29. static const CHString SYNC_KEY(
  30. TEXT("System\\CurrentControlSet\\Services\\Tcpip\\Parameters"));
  31. static const CHString SYNC_VALUE(TEXT("SyncDomainWithMembership"));
  32. //=========================================================
  33. bool readSyncFlag()
  34. {
  35. bool retval = true;
  36. /*
  37. HKEY hKey = 0;
  38. do
  39. {
  40. LONG result =
  41. Win::RegOpenKeyEx(
  42. HKEY_LOCAL_MACHINE,
  43. SYNC_KEY,
  44. KEY_READ,
  45. hKey);
  46. if (result != ERROR_SUCCESS)
  47. {
  48. break;
  49. }
  50. // default is to sync.
  51. DWORD data = 1;
  52. DWORD data_size = sizeof(data);
  53. result = Win::RegQueryValueEx(
  54. hKey,
  55. SYNC_VALUE,
  56. 0,
  57. (BYTE*) &data,
  58. &data_size);
  59. if (result != ERROR_SUCCESS)
  60. {
  61. break;
  62. }
  63. retval = data ? true : false;
  64. }
  65. while (0);
  66. if (hKey)
  67. {
  68. Win::RegCloseKey(hKey);
  69. }
  70. */
  71. return retval;
  72. }
  73. //=========================================================
  74. void writeSyncFlag(bool flag)
  75. {
  76. /*
  77. HKEY hKey = 0;
  78. do
  79. {
  80. LONG result =
  81. Win::RegCreateKeyEx(
  82. HKEY_LOCAL_MACHINE,
  83. SYNC_KEY,
  84. REG_OPTION_NON_VOLATILE,
  85. KEY_WRITE,
  86. 0,
  87. hKey,
  88. 0);
  89. if (result != ERROR_SUCCESS)
  90. {
  91. break;
  92. }
  93. DWORD data = flag ? 1 : 0;
  94. DWORD data_size = sizeof(data);
  95. Win::RegSetValueEx(
  96. hKey,
  97. SYNC_VALUE,
  98. REG_DWORD,
  99. (BYTE*) &data,
  100. data_size);
  101. }
  102. while (0);
  103. if (hKey)
  104. {
  105. Win::RegCloseKey(hKey);
  106. }
  107. */
  108. }
  109. //=========================================================
  110. bool isNetworkingInstalled()
  111. {
  112. /* SC_HANDLE handle = ::OpenSCManager(0, 0, GENERIC_READ);
  113. if (!handle)
  114. {
  115. TRACE(TEXT("can't open SCM"));
  116. return false;
  117. }
  118. SC_HANDLE wks =
  119. ::OpenService(
  120. handle,
  121. TEXT("LanmanWorkstation"),
  122. SERVICE_QUERY_STATUS);
  123. if (!wks)
  124. {
  125. TRACE(TEXT("can't open workstation service: not installed"));
  126. ::CloseServiceHandle(handle);
  127. return false;
  128. }
  129. bool result = false;
  130. SERVICE_STATUS status;
  131. memset(&status, 0, sizeof(status));
  132. if (::QueryServiceStatus(wks, &status))
  133. {
  134. if (status.dwCurrentState == SERVICE_RUNNING)
  135. {
  136. result = true;
  137. }
  138. }
  139. ::CloseServiceHandle(wks);
  140. ::CloseServiceHandle(handle);
  141. TRACE(
  142. CHString::format(
  143. TEXT("workstation service %1 running"),
  144. result ? TEXT("is") : TEXT("is NOT")));
  145. return result;
  146. */
  147. return true;
  148. }
  149. //---------------------------------------------------------
  150. State::State()
  151. : must_reboot(false)
  152. {
  153. original.Refresh();
  154. current = original;
  155. }
  156. //---------------------------------------------------------
  157. State::~State()
  158. {
  159. }
  160. //=========================================================
  161. void Settings::Refresh()
  162. {
  163. /*
  164. CHString unknown = CHString::load(IDS_UNKNOWN);
  165. ComputerDomainDNSName = unknown;
  166. DomainName = unknown;
  167. FullComputerName = unknown;
  168. ShortComputerName = unknown;
  169. SyncDNSNames = readSyncFlag();
  170. JoinedToWorkgroup = true;
  171. DSROLE_PRIMARY_DOMAIN_INFO_BASIC* info = 0;
  172. DWORD err = MyDsRoleGetPrimaryDomainInformation(0, info);
  173. if (err == NO_ERROR)
  174. {
  175. if (info->DomainNameDns)
  176. {
  177. DomainName = info->DomainNameDns;
  178. }
  179. else if (info->DomainNameFlat)
  180. {
  181. DomainName = info->DomainNameFlat;
  182. }
  183. // this is the workgroup name iff JoinedToWorkgroup == true
  184. switch (info->MachineRole)
  185. {
  186. case DsRole_RoleBackupDomainController:
  187. case DsRole_RolePrimaryDomainController:
  188. {
  189. machine_is_dc = true;
  190. JoinedToWorkgroup = false;
  191. break;
  192. }
  193. case DSRole_RoleStandaloneWorkstation:
  194. case DsRole_RoleStandaloneServer:
  195. {
  196. machine_is_dc = false;
  197. JoinedToWorkgroup = true;
  198. if (DomainName.empty())
  199. {
  200. DomainName = CHString::load(IDS_DEFAULT_WORKGROUP);
  201. }
  202. break;
  203. }
  204. case DsRole_RoleMemberWorkstation:
  205. case DsRole_RoleMemberServer:
  206. {
  207. machine_is_dc = false;
  208. JoinedToWorkgroup = false;
  209. break;
  210. }
  211. default:
  212. {
  213. assert(false);
  214. break;
  215. }
  216. }
  217. ::DsRoleFreeMemory(info);
  218. }
  219. else
  220. {
  221. AppError(0, HRESULT_FROM_WIN32(err),
  222. CHString::load(IDS_ERROR_READING_MEMBERSHIP));
  223. }
  224. networking_installed = isNetworkingInstalled();
  225. bool tcp_installed = networking_installed && IsTCPIPInstalled();
  226. CHString active_full_name;
  227. HKEY hkey = 0;
  228. LONG result =
  229. Win::RegOpenKeyEx(
  230. HKEY_LOCAL_MACHINE,
  231. TEXT("System\\CurrentControlSet\\Control\\ComputerName\\ComputerName"),
  232. KEY_READ,
  233. hkey);
  234. if (result == ERROR_SUCCESS)
  235. {
  236. NetBIOSComputerName = Win::RegQueryValueSz(hkey, TEXT("ComputerName"));
  237. }
  238. Win::RegCloseKey(hkey);
  239. if (tcp_installed)
  240. {
  241. HKEY hkey = 0;
  242. LONG result =
  243. Win::RegOpenKeyEx(
  244. HKEY_LOCAL_MACHINE,
  245. TEXT("System\\CurrentControlSet\\Services\\Tcpip\\Parameters"),
  246. KEY_READ,
  247. hkey);
  248. if (result == ERROR_SUCCESS)
  249. {
  250. CHString active_short_name =
  251. Win::RegQueryValueSz(hkey, TEXT("Hostname"));
  252. CHString short_name =
  253. Win::RegQueryValueSz(hkey, TEXT("NV Hostname"));
  254. ShortComputerName =
  255. short_name.empty() ? active_short_name : short_name;
  256. CHString active_domain_name =
  257. Win::RegQueryValueSz(hkey, TEXT("Domain"));
  258. CHString domain_name =
  259. Win::RegQueryValueSz(hkey, TEXT("NV Domain"));
  260. ComputerDomainDNSName =
  261. domain_name.empty() ? active_domain_name : domain_name;
  262. FullComputerName =
  263. ShortComputerName + TEXT(".") + ComputerDomainDNSName;
  264. active_full_name =
  265. active_short_name + TEXT(".") + active_domain_name;
  266. }
  267. Win::RegCloseKey(hkey);
  268. }
  269. else
  270. {
  271. LONG result =
  272. Win::RegOpenKeyEx(
  273. HKEY_LOCAL_MACHINE,
  274. TEXT("System\\CurrentControlSet\\Control\\ComputerName\\ActiveComputerName"),
  275. KEY_READ,
  276. hkey);
  277. if (result == ERROR_SUCCESS)
  278. {
  279. active_full_name = Win::RegQueryValueSz(hkey, TEXT("ComputerName"));
  280. }
  281. Win::RegCloseKey(hkey);
  282. ShortComputerName = NetBIOSComputerName;
  283. FullComputerName = ShortComputerName;
  284. }
  285. NeedsReboot = active_full_name != FullComputerName;
  286. */
  287. }
  288. //---------------------------------------------------------
  289. void State::Init(CWbemClassObject &computer,
  290. CWbemClassObject &os,
  291. CWbemClassObject dns)
  292. {
  293. m_computer = computer;
  294. m_OS = os;
  295. m_DNS = dns;
  296. }
  297. //---------------------------------------------------------
  298. void State::Refresh()
  299. {
  300. }
  301. //---------------------------------------------------------
  302. bool State::NeedsReboot() const
  303. {
  304. return original.NeedsReboot;
  305. }
  306. //---------------------------------------------------------
  307. bool State::IsMachineDC() const
  308. {
  309. return machine_is_dc;
  310. }
  311. //---------------------------------------------------------
  312. bool State::IsNetworkingInstalled() const
  313. {
  314. return networking_installed;
  315. }
  316. //---------------------------------------------------------
  317. CHString State::GetFullComputerName() const
  318. {
  319. return current.FullComputerName;
  320. }
  321. //---------------------------------------------------------
  322. CHString State::GetDomainName() const
  323. {
  324. return current.DomainName;
  325. }
  326. //---------------------------------------------------------
  327. void State::SetDomainName(const CHString& name)
  328. {
  329. current.DomainName = name;
  330. }
  331. //---------------------------------------------------------
  332. bool State::IsMemberOfWorkgroup() const
  333. {
  334. return current.JoinedToWorkgroup;
  335. }
  336. //---------------------------------------------------------
  337. void State::SetIsMemberOfWorkgroup(bool yesNo)
  338. {
  339. current.JoinedToWorkgroup = yesNo;
  340. }
  341. //---------------------------------------------------------
  342. CHString State::GetShortComputerName() const
  343. {
  344. return current.ShortComputerName;
  345. }
  346. //---------------------------------------------------------
  347. void State::SetShortComputerName(const CHString& name)
  348. {
  349. current.ShortComputerName = name;
  350. // current.NetBIOSComputerName = DNS::HostnameToNetBIOSName(name);
  351. setFullComputerName();
  352. }
  353. //---------------------------------------------------------
  354. bool State::WasShortComputerNameChanged() const
  355. {
  356. return true;
  357. //original.ShortComputerName.icompare(current.ShortComputerName) != 0;
  358. }
  359. //---------------------------------------------------------
  360. CHString State::GetComputerDomainDNSName() const
  361. {
  362. return current.ComputerDomainDNSName;
  363. }
  364. //---------------------------------------------------------
  365. void State::SetComputerDomainDNSName(const CHString& newName)
  366. {
  367. current.ComputerDomainDNSName = newName;
  368. setFullComputerName();
  369. }
  370. //---------------------------------------------------------
  371. void State::setFullComputerName()
  372. {
  373. current.FullComputerName =
  374. current.ShortComputerName
  375. + TEXT(".")
  376. + current.ComputerDomainDNSName;
  377. }
  378. //---------------------------------------------------------
  379. bool State::WasMembershipChanged() const
  380. {
  381. return true;
  382. // original.DomainName.icompare(current.DomainName) != 0
  383. // || original.JoinedToWorkgroup != current.JoinedToWorkgroup;
  384. }
  385. //---------------------------------------------------------
  386. bool State::ChangesNeedSaving() const
  387. {
  388. /* if (
  389. original.ComputerDomainDNSName.icompare(
  390. current.ComputerDomainDNSName) != 0
  391. || WasMembershipChanged()
  392. || WasShortComputerNameChanged()
  393. || SyncDNSNamesWasChanged())
  394. {
  395. return true;
  396. }
  397. */
  398. return false;
  399. }
  400. //---------------------------------------------------------
  401. bool State::GetSyncDNSNames() const
  402. {
  403. return current.SyncDNSNames;
  404. }
  405. //---------------------------------------------------------
  406. void State::SetSyncDNSNames(bool yesNo)
  407. {
  408. current.SyncDNSNames = yesNo;
  409. }
  410. //---------------------------------------------------------
  411. bool State::SyncDNSNamesWasChanged() const
  412. {
  413. return original.SyncDNSNames != current.SyncDNSNames;
  414. }
  415. //---------------------------------------------------------
  416. CHString massageUserName(const CHString& domainName, const CHString& userName)
  417. {
  418. /* if (!domainName.IsEmpty() && !userName.IsEmpty())
  419. {
  420. static const CHString DOMAIN_SEP_CHAR = TEXT("\\");
  421. CHString name = userName;
  422. int pos = userName.find(DOMAIN_SEP_CHAR);
  423. if (pos == CHString::npos)
  424. {
  425. return domainName + DOMAIN_SEP_CHAR + name;
  426. }
  427. }
  428. */
  429. return userName;
  430. }
  431. //=======================================================
  432. NET_API_STATUS myNetJoinDomain(
  433. const CHString& domain,
  434. const CHString& username,
  435. const CHString& password,
  436. ULONG flags)
  437. {
  438. /* assert(!domain.empty());
  439. NET_API_STATUS status =
  440. ::NetJoinDomain(
  441. 0, // this machine
  442. domain.c_str(),
  443. 0, // default OU
  444. username.empty() ? 0 : username.c_str(),
  445. password.c_str(),
  446. flags);
  447. TRACE(CHString::format(TEXT("Error 0x%1!X! (!0 => error)"), status));
  448. return status;
  449. */
  450. return 0;
  451. }
  452. //=======================================================
  453. HRESULT join(HWND dialog, const CHString& name, bool isWorkgroupJoin)
  454. {
  455. /* assert(!name.empty());
  456. assert(Win::IsWindow(dialog));
  457. Win::CursorSetting cursor(IDC_WAIT);
  458. State& state = State::GetInstance();
  459. CHString username = massageUserName(name, state.GetUsername());
  460. CHString password = state.GetPassword();
  461. ULONG flags = 0;
  462. if (!isWorkgroupJoin)
  463. {
  464. flags =
  465. NETSETUP_JOIN_DOMAIN
  466. | NETSETUP_ACCT_CREATE
  467. | NETSETUP_DOMAIN_JOIN_IF_JOINED;
  468. }
  469. NET_API_STATUS status =
  470. myNetJoinDomain(name, username, password, flags);
  471. if (
  472. status == ERROR_ACCESS_DENIED
  473. && (flags & NETSETUP_ACCT_CREATE) )
  474. {
  475. // retry without account create flag for the case where the account
  476. // already exists
  477. TRACE(TEXT("Retry without account create flag"));
  478. status =
  479. myNetJoinDomain(
  480. name,
  481. username,
  482. password,
  483. flags & ~NETSETUP_ACCT_CREATE);
  484. }
  485. if (status == NERR_Success)
  486. {
  487. AppMessage(
  488. dialog,
  489. CHString::format(
  490. isWorkgroupJoin ? IDS_WORKGROUP_WELCOME : IDS_DOMAIN_WELCOME,
  491. name.c_str()));
  492. }
  493. return HRESULT_FROM_WIN32(status);
  494. */
  495. return 0;
  496. }
  497. //=======================================================
  498. HRESULT rename(HWND dialog, const CHString& newName)
  499. {
  500. /* assert(!newName.empty());
  501. assert(Win::IsWindow(dialog));
  502. Win::CursorSetting cursor(IDC_WAIT);
  503. State& state = State::GetInstance();
  504. CHString username =
  505. massageUserName(state.GetDomainName(), state.GetUsername());
  506. CHString password = state.GetPassword();
  507. ULONG flags = NETSETUP_ACCT_CREATE;
  508. TRACE(TEXT("Calling NetRenameMachineInDomain"));
  509. TRACE( TEXT("lpServer : (null)"));
  510. TRACE(CHString::format(TEXT("lpNewMachineName : %1"), newName.c_str()));
  511. TRACE(CHString::format(TEXT("lpAccount : %1"), username.c_str()));
  512. TRACE(CHString::format(TEXT("fRenameOptions : 0x%1!X!"), flags));
  513. NET_API_STATUS status =
  514. ::NetRenameMachineInDomain(
  515. 0, // this machine
  516. newName.c_str(),
  517. username.empty() ? 0 : username.c_str(),
  518. password.c_str(),
  519. flags);
  520. TRACE(CHString::format(TEXT("Error 0x%1!X! (!0 => error)"), status));
  521. // if (status == NERR_Success)
  522. // {
  523. // AppMessage(dialog, IDS_NAME_CHANGED);
  524. // state.SetMustRebootFlag(true);
  525. // }
  526. return HRESULT_FROM_WIN32(status);
  527. */
  528. return 0;
  529. }
  530. //=======================================================
  531. static NET_API_STATUS myNetUnjoinDomain(ULONG flags)
  532. {
  533. return 0;
  534. }
  535. //=======================================================
  536. HRESULT unjoin(HWND dialog, const CHString& domain)
  537. {
  538. return S_OK;
  539. }
  540. //=======================================================
  541. HRESULT setDomainDNSName(HWND dialog, const CHString& newDomainDNSName)
  542. {
  543. return S_OK;
  544. }
  545. //=======================================================
  546. HRESULT setShortName(HWND dialog, const CHString& newShortName)
  547. {
  548. return S_OK;
  549. }
  550. //=======================================================
  551. bool getCredentials(HWND dialog, int promptResID = 0)
  552. {
  553. return true;
  554. }
  555. //------------------------------------------------------
  556. bool State::doSaveDomainChange(HWND dialog)
  557. {
  558. return true;
  559. }
  560. //------------------------------------------------------
  561. bool State::doSaveWorkgroupChange(HWND dialog)
  562. {
  563. return true;
  564. }
  565. //------------------------------------------------------
  566. bool State::doSaveNameChange(HWND dialog)
  567. {
  568. return true;
  569. }
  570. //------------------------------------------------------
  571. bool State::SaveChanges(HWND dialog)
  572. {
  573. return true;
  574. }
  575. //------------------------------------------------------
  576. CHString State::GetUsername() const
  577. {
  578. return username;
  579. }
  580. //------------------------------------------------------
  581. CHString State::GetPassword() const
  582. {
  583. return password;
  584. }
  585. //------------------------------------------------------
  586. void State::SetMustRebootFlag(bool yesNo)
  587. {
  588. must_reboot = yesNo;
  589. }
  590. //------------------------------------------------------
  591. bool State::MustReboot() const
  592. {
  593. return must_reboot;
  594. }
  595. //------------------------------------------------------
  596. CHString State::GetNetBIOSComputerName() const
  597. {
  598. return current.NetBIOSComputerName;
  599. }
  600. //------------------------------------------------------
  601. CHString State::GetOriginalShortComputerName() const
  602. {
  603. return original.ShortComputerName;
  604. }
  605. //------------------------------------------------------
  606. void State::SetUsername(const CHString& name)
  607. {
  608. username = name;
  609. }
  610. //------------------------------------------------------
  611. void State::SetPassword(const CHString& pass)
  612. {
  613. password = pass;
  614. }