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.

335 lines
7.2 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. if (state.IsLastDCInDomain())
  75. {
  76. id = IDS_PARENT_DEMOTE_CREDENTIALS;
  77. }
  78. else
  79. {
  80. arg = computer.GetDomainDnsName();
  81. id = IDS_DOMAIN_DEMOTE_CREDENTIALS;
  82. }
  83. }
  84. else
  85. {
  86. // dc is for forest root or tree root domain
  87. arg = computer.GetForestDnsName();
  88. id = IDS_ROOT_DOMAIN_CREDENTIALS;
  89. }
  90. break;
  91. }
  92. case State::ABORT_BDC_UPGRADE:
  93. {
  94. id = IDS_ABORT_BDC_UPGRADE_CREDENTIALS;
  95. arg.erase();
  96. break;
  97. }
  98. case State::FOREST:
  99. case State::NONE:
  100. default:
  101. {
  102. // This may happen if the promote APIs can't create files, in
  103. // which case, the APIs should return an appropriate message.
  104. break;
  105. }
  106. }
  107. if (id)
  108. {
  109. return String::format(id, arg.c_str());
  110. }
  111. return String();
  112. }
  113. String
  114. GetDefaultUserDomainName()
  115. {
  116. LOG_FUNCTION(GetDefaultUserDomainName);
  117. String def;
  118. State& state = State::GetInstance();
  119. switch (state.GetOperation())
  120. {
  121. case State::REPLICA:
  122. {
  123. def = state.GetReplicaDomainDNSName();
  124. break;
  125. }
  126. case State::TREE:
  127. case State::CHILD:
  128. {
  129. def = state.GetParentDomainDnsName();
  130. break;
  131. }
  132. case State::DEMOTE:
  133. {
  134. String parent =
  135. GetParentDomainDnsName(
  136. State::GetInstance().GetComputer().GetDomainDnsName(), false);
  137. if (state.IsLastDCInDomain() && !parent.empty())
  138. {
  139. def = parent;
  140. }
  141. else
  142. {
  143. def = state.GetComputer().GetDomainDnsName();
  144. }
  145. break;
  146. }
  147. case State::FOREST:
  148. case State::ABORT_BDC_UPGRADE:
  149. {
  150. // no default.
  151. break;
  152. }
  153. case State::NONE:
  154. default:
  155. {
  156. ASSERT(false);
  157. break;
  158. }
  159. }
  160. return def;
  161. }
  162. void
  163. GetCredentialsDialog::OnInit()
  164. {
  165. LOG_FUNCTION(GetCredentialsDialog::OnInit);
  166. HWND hwndCred = Win::GetDlgItem(hwnd, IDC_CRED);
  167. Credential_SetUserNameMaxChars(hwndCred, DS::MAX_USER_NAME_LENGTH);
  168. Credential_SetPasswordMaxChars(hwndCred, DS::MAX_PASSWORD_LENGTH);
  169. // Only use the smartcard flag when the machine is joined to a domain. On a
  170. // standalone machine, the smartcard won't have access to any domain
  171. // authority to authenticate it.
  172. // NTRAID#NTBUG9-287538-2001/01/23-sburns
  173. State& state = State::GetInstance();
  174. Computer& computer = state.GetComputer();
  175. DWORD flags = CRS_NORMAL | CRS_USERNAMES;
  176. if (
  177. computer.IsJoinedToDomain()
  178. // can only use smartcards on replica promotions
  179. // NTRAID#NTBUG9-311150-2001/02/19-sburns
  180. && state.GetOperation() == State::REPLICA)
  181. {
  182. flags |= CRS_SMARTCARDS;
  183. }
  184. Credential_InitStyle(hwndCred, flags);
  185. Win::Edit_LimitText(
  186. Win::GetDlgItem(hwnd, IDC_DOMAIN),
  187. Dns::MAX_NAME_LENGTH);
  188. Win::SetDlgItemText(hwnd, IDC_FAILURE_MESSAGE, failureMessage);
  189. Win::SetDlgItemText(hwnd, IDC_MESSAGE, GetCredentialMessage());
  190. String domain = state.GetUserDomainName();
  191. if (domain.empty())
  192. {
  193. domain = GetDefaultUserDomainName();
  194. }
  195. Win::SetDlgItemText(hwnd, IDC_DOMAIN, domain);
  196. CredUi::SetUsername(hwndCred, state.GetUsername());
  197. CredUi::SetPassword(hwndCred, state.GetPassword());
  198. Enable();
  199. }
  200. bool
  201. GetCredentialsDialog::OnCommand(
  202. HWND /* windowFrom */ ,
  203. unsigned controlIDFrom,
  204. unsigned code)
  205. {
  206. // LOG_FUNCTION(GetCredentialsDialog::OnCommand);
  207. switch (controlIDFrom)
  208. {
  209. case IDOK:
  210. {
  211. if (code == BN_CLICKED)
  212. {
  213. // transfer the dialog contents to the state object.
  214. State& state = State::GetInstance();
  215. HWND hwndCred = Win::GetDlgItem(hwnd, IDC_CRED);
  216. state.SetUsername(CredUi::GetUsername(hwndCred));
  217. state.SetPassword(CredUi::GetPassword(hwndCred));
  218. state.SetUserDomainName(
  219. Win::GetTrimmedDlgItemText(hwnd, IDC_DOMAIN));
  220. HRESULT unused = Win::EndDialog(hwnd, controlIDFrom);
  221. ASSERT(SUCCEEDED(unused));
  222. return true;
  223. }
  224. break;
  225. }
  226. case IDCANCEL:
  227. {
  228. if (code == BN_CLICKED)
  229. {
  230. HRESULT unused = Win::EndDialog(hwnd, controlIDFrom);
  231. ASSERT(SUCCEEDED(unused));
  232. return true;
  233. }
  234. break;
  235. }
  236. case IDC_DOMAIN:
  237. {
  238. if (code == EN_CHANGE)
  239. {
  240. SetChanged(controlIDFrom);
  241. Enable();
  242. return true;
  243. }
  244. break;
  245. }
  246. case IDC_CRED:
  247. {
  248. if (code == CRN_USERNAMECHANGE)
  249. {
  250. SetChanged(controlIDFrom);
  251. Enable();
  252. return true;
  253. }
  254. break;
  255. }
  256. default:
  257. {
  258. // do nothing
  259. break;
  260. }
  261. }
  262. return false;
  263. }