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.

320 lines
6.5 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. //
  3. // Dlg to get credentials for browsing domain forest
  4. //
  5. // 1-8-98 sburns
  6. #include "headers.hxx"
  7. #include "GetCredentialsDialog.hpp"
  8. #include "resource.h"
  9. #include "state.hpp"
  10. #include "ds.hpp"
  11. #include "common.hpp"
  12. #include "CredentialUiHelpers.hpp"
  13. static const DWORD HELP_MAP[] =
  14. {
  15. 0, 0
  16. };
  17. GetCredentialsDialog::GetCredentialsDialog(const String& failureMessage_)
  18. :
  19. Dialog(IDD_NEW_CREDENTIALS, HELP_MAP),
  20. failureMessage(failureMessage_)
  21. {
  22. LOG_CTOR(GetCredentialsDialog);
  23. ASSERT(!failureMessage.empty());
  24. CredUIInitControls();
  25. }
  26. GetCredentialsDialog::~GetCredentialsDialog()
  27. {
  28. LOG_DTOR(GetCredentialsDialog);
  29. }
  30. void
  31. GetCredentialsDialog::Enable()
  32. {
  33. // LOG_FUNCTION(GetCredentialsDialog::Enable);
  34. bool okEnabled =
  35. !CredUi::GetUsername(Win::GetDlgItem(hwnd, IDC_CRED)).empty()
  36. && !Win::GetTrimmedDlgItemText(hwnd, IDC_DOMAIN).empty();
  37. Win::EnableWindow(Win::GetDlgItem(hwnd, IDOK), okEnabled);
  38. }
  39. String
  40. GetCredentialMessage()
  41. {
  42. LOG_FUNCTION(GetCredentialMessage);
  43. int id = 0;
  44. String arg;
  45. State& state = State::GetInstance();
  46. switch (state.GetOperation())
  47. {
  48. case State::REPLICA:
  49. {
  50. id = IDS_REPLICA_CREDENTIALS;
  51. arg = state.GetReplicaDomainDNSName();
  52. break;
  53. }
  54. case State::TREE:
  55. {
  56. id = IDS_SIBLING_CREDENTIALS;
  57. arg = state.GetParentDomainDnsName();
  58. break;
  59. }
  60. case State::CHILD:
  61. {
  62. id = IDS_PARENT_CREDENTIALS;
  63. arg = state.GetParentDomainDnsName();
  64. break;
  65. }
  66. case State::DEMOTE:
  67. {
  68. const Computer& computer = state.GetComputer();
  69. arg =
  70. GetParentDomainDnsName(computer.GetDomainDnsName(), false);
  71. if (!arg.empty())
  72. {
  73. // dc is for a child domain
  74. id = IDS_PARENT_DEMOTE_CREDENTIALS;
  75. }
  76. else
  77. {
  78. // dc is for forest root or tree root domain
  79. arg = computer.GetForestDnsName();
  80. id = IDS_ROOT_DOMAIN_CREDENTIALS;
  81. }
  82. break;
  83. }
  84. case State::ABORT_BDC_UPGRADE:
  85. {
  86. id = IDS_ABORT_BDC_UPGRADE_CREDENTIALS;
  87. arg.erase();
  88. break;
  89. }
  90. case State::FOREST:
  91. case State::NONE:
  92. default:
  93. {
  94. ASSERT(false);
  95. break;
  96. }
  97. }
  98. if (id)
  99. {
  100. return String::format(id, arg.c_str());
  101. }
  102. return String();
  103. }
  104. String
  105. GetDefaultUserDomainName()
  106. {
  107. LOG_FUNCTION(GetDefaultUserDomainName);
  108. String def;
  109. State& state = State::GetInstance();
  110. switch (state.GetOperation())
  111. {
  112. case State::REPLICA:
  113. {
  114. def = state.GetReplicaDomainDNSName();
  115. break;
  116. }
  117. case State::TREE:
  118. case State::CHILD:
  119. {
  120. def = state.GetParentDomainDnsName();
  121. break;
  122. }
  123. case State::DEMOTE:
  124. {
  125. String parent =
  126. GetParentDomainDnsName(
  127. State::GetInstance().GetComputer().GetDomainDnsName(), false);
  128. if (state.IsLastDCInDomain() && !parent.empty())
  129. {
  130. def = parent;
  131. }
  132. else
  133. {
  134. def = state.GetComputer().GetDomainDnsName();
  135. }
  136. break;
  137. }
  138. case State::FOREST:
  139. case State::ABORT_BDC_UPGRADE:
  140. case State::NONE:
  141. default:
  142. {
  143. ASSERT(false);
  144. break;
  145. }
  146. }
  147. return def;
  148. }
  149. void
  150. GetCredentialsDialog::OnInit()
  151. {
  152. LOG_FUNCTION(GetCredentialsDialog::OnInit);
  153. HWND hwndCred = Win::GetDlgItem(hwnd, IDC_CRED);
  154. Credential_SetUserNameMaxChars(hwndCred, DS::MAX_USER_NAME_LENGTH);
  155. Credential_SetPasswordMaxChars(hwndCred, DS::MAX_PASSWORD_LENGTH);
  156. // Only use the smartcard flag when the machine is joined to a domain. On a
  157. // standalone machine, the smartcard won't have access to any domain
  158. // authority to authenticate it.
  159. // NTRAID#NTBUG9-287538-2001/01/23-sburns
  160. State& state = State::GetInstance();
  161. Computer& computer = state.GetComputer();
  162. DWORD flags = CRS_NORMAL | CRS_USERNAMES;
  163. if (
  164. computer.IsJoinedToDomain()
  165. // can only use smartcards on replica promotions
  166. // NTRAID#NTBUG9-311150-2001/02/19-sburns
  167. && state.GetOperation() == State::REPLICA)
  168. {
  169. flags |= CRS_SMARTCARDS;
  170. }
  171. Credential_InitStyle(hwndCred, flags);
  172. Win::Edit_LimitText(
  173. Win::GetDlgItem(hwnd, IDC_DOMAIN),
  174. Dns::MAX_NAME_LENGTH);
  175. Win::SetDlgItemText(hwnd, IDC_FAILURE_MESSAGE, failureMessage);
  176. Win::SetDlgItemText(hwnd, IDC_MESSAGE, GetCredentialMessage());
  177. String domain = state.GetUserDomainName();
  178. if (domain.empty())
  179. {
  180. domain = GetDefaultUserDomainName();
  181. }
  182. Win::SetDlgItemText(hwnd, IDC_DOMAIN, domain);
  183. CredUi::SetUsername(hwndCred, state.GetUsername());
  184. CredUi::SetPassword(hwndCred, state.GetPassword());
  185. Enable();
  186. }
  187. bool
  188. GetCredentialsDialog::OnCommand(
  189. HWND /* windowFrom */ ,
  190. unsigned controlIDFrom,
  191. unsigned code)
  192. {
  193. // LOG_FUNCTION(GetCredentialsDialog::OnCommand);
  194. switch (controlIDFrom)
  195. {
  196. case IDOK:
  197. {
  198. if (code == BN_CLICKED)
  199. {
  200. // transfer the dialog contents to the state object.
  201. State& state = State::GetInstance();
  202. HWND hwndCred = Win::GetDlgItem(hwnd, IDC_CRED);
  203. state.SetUsername(CredUi::GetUsername(hwndCred));
  204. state.SetPassword(CredUi::GetPassword(hwndCred));
  205. state.SetUserDomainName(
  206. Win::GetTrimmedDlgItemText(hwnd, IDC_DOMAIN));
  207. HRESULT unused = Win::EndDialog(hwnd, controlIDFrom);
  208. ASSERT(SUCCEEDED(unused));
  209. return true;
  210. }
  211. break;
  212. }
  213. case IDCANCEL:
  214. {
  215. if (code == BN_CLICKED)
  216. {
  217. HRESULT unused = Win::EndDialog(hwnd, controlIDFrom);
  218. ASSERT(SUCCEEDED(unused));
  219. return true;
  220. }
  221. break;
  222. }
  223. case IDC_DOMAIN:
  224. {
  225. if (code == EN_CHANGE)
  226. {
  227. SetChanged(controlIDFrom);
  228. Enable();
  229. return true;
  230. }
  231. break;
  232. }
  233. case IDC_CRED:
  234. {
  235. if (code == CRN_USERNAMECHANGE)
  236. {
  237. SetChanged(controlIDFrom);
  238. Enable();
  239. return true;
  240. }
  241. break;
  242. }
  243. default:
  244. {
  245. // do nothing
  246. break;
  247. }
  248. }
  249. return false;
  250. }