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.

310 lines
6.1 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // netbios domain name page
  4. //
  5. // 1-6-98 sburns
  6. #include "headers.hxx"
  7. #include "page.hpp"
  8. #include "NetbiosNamePage.hpp"
  9. #include "common.hpp"
  10. #include "resource.h"
  11. #include "state.hpp"
  12. #include "ds.hpp"
  13. #include <ValidateDomainName.hpp>
  14. #include <ValidateDOmainName.h>
  15. NetbiosNamePage::NetbiosNamePage()
  16. :
  17. DCPromoWizardPage(
  18. IDD_NETBIOS_NAME,
  19. IDS_NETBIOS_NAME_PAGE_TITLE,
  20. IDS_NETBIOS_NAME_PAGE_SUBTITLE)
  21. {
  22. LOG_CTOR(NetbiosNamePage);
  23. }
  24. NetbiosNamePage::~NetbiosNamePage()
  25. {
  26. LOG_DTOR(NetbiosNamePage);
  27. }
  28. void
  29. NetbiosNamePage::OnInit()
  30. {
  31. LOG_FUNCTION(NetbiosNamePage::OnInit);
  32. Win::Edit_LimitText(
  33. Win::GetDlgItem(hwnd, IDC_NETBIOS),
  34. DS::MAX_NETBIOS_NAME_LENGTH);
  35. State& state = State::GetInstance();
  36. if (state.UsingAnswerFile())
  37. {
  38. Win::SetDlgItemText(
  39. hwnd,
  40. IDC_NETBIOS,
  41. state.GetAnswerFileOption(
  42. AnswerFile::OPTION_NEW_DOMAIN_NETBIOS_NAME));
  43. }
  44. }
  45. static
  46. void
  47. enable(HWND dialog)
  48. {
  49. ASSERT(Win::IsWindow(dialog));
  50. int next =
  51. !Win::GetTrimmedDlgItemText(dialog, IDC_NETBIOS).empty()
  52. ? PSWIZB_NEXT : 0;
  53. Win::PropSheet_SetWizButtons(
  54. Win::GetParent(dialog),
  55. PSWIZB_BACK | next);
  56. }
  57. bool
  58. NetbiosNamePage::OnCommand(
  59. HWND /* windowFrom */ ,
  60. unsigned controlIDFrom,
  61. unsigned code)
  62. {
  63. // LOG_FUNCTION(NetbiosNamePage::OnCommand);
  64. switch (controlIDFrom)
  65. {
  66. case IDC_NETBIOS:
  67. {
  68. if (code == EN_CHANGE)
  69. {
  70. SetChanged(controlIDFrom);
  71. enable(hwnd);
  72. }
  73. break;
  74. }
  75. default:
  76. {
  77. // do nothing
  78. break;
  79. }
  80. }
  81. return false;
  82. }
  83. HRESULT
  84. MyDsRoleDnsNameToFlatName(
  85. const String& domainDNSName,
  86. String& result,
  87. bool& nameWasTweaked)
  88. {
  89. LOG_FUNCTION(MyDsRoleDnsNameToFlatName);
  90. ASSERT(!domainDNSName.empty());
  91. nameWasTweaked = false;
  92. result.erase();
  93. LOG(L"Calling DsRoleDnsNameToFlatName");
  94. LOG( L"lpServer : (null)");
  95. LOG(String::format(L"lpDnsName : %1", domainDNSName.c_str()));
  96. PWSTR flatName = 0;
  97. ULONG flags = 0;
  98. HRESULT hr =
  99. Win32ToHresult(
  100. ::DsRoleDnsNameToFlatName(
  101. 0, // this server
  102. domainDNSName.c_str(),
  103. &flatName,
  104. &flags));
  105. LOG_HRESULT(hr);
  106. if (SUCCEEDED(hr) && flatName)
  107. {
  108. LOG(String::format(L"lpFlatName : %1", flatName));
  109. LOG(String::format(L"lpStatusFlag : %1!X!", flags));
  110. result = flatName;
  111. if (result.length() > DNLEN)
  112. {
  113. result.resize(DNLEN);
  114. }
  115. ::DsRoleFreeMemory(flatName);
  116. // the name was tweaked if it is not the default. 338443
  117. nameWasTweaked = !(flags & DSROLE_FLATNAME_DEFAULT);
  118. }
  119. return hr;
  120. }
  121. // return true if the name generated has already been validated, false
  122. // if not.
  123. bool
  124. GenerateDefaultNetbiosName(HWND parent)
  125. {
  126. LOG_FUNCTION(GenerateDefaultNetbiosName);
  127. ASSERT(Win::IsWindow(parent));
  128. Win::CursorSetting cursor(IDC_WAIT);
  129. bool result = false;
  130. String dnsDomainName = State::GetInstance().GetNewDomainDNSName();
  131. bool nameWasTweaked = false;
  132. String generatedName;
  133. HRESULT hr =
  134. MyDsRoleDnsNameToFlatName(
  135. dnsDomainName,
  136. generatedName,
  137. nameWasTweaked);
  138. if (FAILED(hr))
  139. {
  140. // if the api call failed, the name could not have been validated.
  141. result = false;
  142. // fall back to just the first 15 characters of the first label
  143. generatedName =
  144. dnsDomainName.substr(0, min(DNLEN, dnsDomainName.find(L'.')));
  145. LOG(String::format(L"falling back to %1", generatedName.c_str()));
  146. }
  147. else
  148. {
  149. // the api validated the name for us.
  150. result = true;
  151. }
  152. generatedName.to_upper();
  153. if (generatedName.is_numeric())
  154. {
  155. // the generated name is all-numeric. This is not allowed. So we
  156. // toss it out. 368777 bis
  157. generatedName.erase();
  158. nameWasTweaked = false;
  159. }
  160. Win::SetDlgItemText(
  161. parent,
  162. IDC_NETBIOS,
  163. generatedName);
  164. // inform the user that the default NetBIOS name was adjusted due
  165. // to name collision on the network
  166. if (nameWasTweaked)
  167. {
  168. popup.Info(
  169. parent,
  170. String::format(
  171. IDS_GENERATED_NAME_WAS_TWEAKED,
  172. generatedName.c_str()));
  173. }
  174. return result;
  175. }
  176. bool
  177. NetbiosNamePage::OnSetActive()
  178. {
  179. LOG_FUNCTION(NetbiosNamePage::OnSetActive);
  180. Win::PropSheet_SetWizButtons(
  181. Win::GetParent(hwnd),
  182. PSWIZB_BACK);
  183. State& state = State::GetInstance();
  184. if (state.RunHiddenUnattended())
  185. {
  186. int nextPage = Validate();
  187. if (nextPage != -1)
  188. {
  189. GetWizard().SetNextPageID(hwnd, nextPage);
  190. }
  191. else
  192. {
  193. state.ClearHiddenWhileUnattended();
  194. }
  195. }
  196. // do this here instead of in init to regenerate a default name if the
  197. // user has not annointed one already.
  198. if (
  199. !state.UsingAnswerFile()
  200. && state.GetNewDomainNetbiosName().empty())
  201. {
  202. // 338443
  203. if (GenerateDefaultNetbiosName(hwnd))
  204. {
  205. // Clear the changes so we don't validate the generated name: it's
  206. // supposed to already be valid.
  207. ClearChanges();
  208. }
  209. }
  210. enable(hwnd);
  211. return true;
  212. }
  213. int
  214. NetbiosNamePage::Validate()
  215. {
  216. LOG_FUNCTION(NetbiosNamePage::Validate);
  217. int nextPage = IDD_PATHS;
  218. if (WasChanged(IDC_NETBIOS))
  219. {
  220. if (!ValidateDomainNetbiosName(hwnd, IDC_NETBIOS, popup))
  221. {
  222. nextPage = -1;
  223. }
  224. }
  225. if (nextPage != -1)
  226. {
  227. ClearChanges();
  228. State& state = State::GetInstance();
  229. state.SetNewDomainNetbiosName(
  230. Win::GetTrimmedDlgItemText(hwnd, IDC_NETBIOS));
  231. }
  232. return nextPage;
  233. }