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.

182 lines
6.8 KiB

  1. //////////////////////////////////////////////////////////////
  2. //
  3. // NewUserDlg.cpp
  4. //
  5. // Implementation of the "Add Mailbox" dialog
  6. //
  7. //////////////////////////////////////////////////////////////
  8. #include "stdafx.h"
  9. #include "NewUserDlg.h"
  10. #include "NewUserConfirmDlg.h"
  11. LRESULT CNewUserDlg::OnInitDialog( UINT mMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
  12. {
  13. // initialize the checkbox based on the passed in default
  14. CheckDlgButton( IDC_USER_CREATEUSER, ((m_bCreateUser && !m_bHashPW) ? BST_CHECKED : BST_UNCHECKED) );
  15. Prefix_EnableWindow( m_hWnd, IDC_USER_CREATEUSER, !m_bHashPW );
  16. ::ShowWindow( GetDlgItem(IDC_USER_CREATEUSER), (m_bHashPW ? SW_HIDE : SW_SHOW) );
  17. Prefix_EnableWindow( m_hWnd, IDC_PASSWORD, (m_bCreateUser || m_bHashPW) );
  18. Prefix_EnableWindow( m_hWnd, IDC_CONFIRM, (m_bCreateUser || m_bHashPW) );
  19. Prefix_EnableWindow( m_hWnd, IDC_PASSWORD_STATIC, (m_bCreateUser || m_bHashPW) );
  20. Prefix_EnableWindow( m_hWnd, IDC_CONFIRM_STATIC, (m_bCreateUser || m_bHashPW) );
  21. // Max Text length of 40 for all three boxes
  22. SendDlgItemMessage( IDC_USER_NAME, EM_LIMITTEXT, m_bSAM ? 20 : 64, 0 );
  23. SendDlgItemMessage( IDC_PASSWORD, EM_LIMITTEXT, 40, 0 );
  24. SendDlgItemMessage( IDC_CONFIRM, EM_LIMITTEXT, 40, 0 );
  25. HWND hwndAlias = GetDlgItem(IDC_USER_NAME);
  26. if( hwndAlias && ::IsWindow(hwndAlias) )
  27. {
  28. m_wndAlias.SubclassWindow( hwndAlias );
  29. }
  30. return 0;
  31. }
  32. LRESULT CNewUserDlg::OnEditChange( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled )
  33. {
  34. EnableButtons();
  35. return 0;
  36. }
  37. LRESULT CNewUserDlg::OnCreateClicked( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled )
  38. {
  39. BOOL bChecked = (IsDlgButtonChecked(IDC_USER_CREATEUSER) == BST_CHECKED);
  40. Prefix_EnableWindow( m_hWnd, IDC_PASSWORD, bChecked );
  41. Prefix_EnableWindow( m_hWnd, IDC_CONFIRM, bChecked );
  42. Prefix_EnableWindow( m_hWnd, IDC_PASSWORD_STATIC, bChecked );
  43. Prefix_EnableWindow( m_hWnd, IDC_CONFIRM_STATIC, bChecked );
  44. EnableButtons();
  45. return 0;
  46. }
  47. LRESULT CNewUserDlg::OnClose( WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled )
  48. {
  49. m_bCreateUser = (IsDlgButtonChecked(IDC_USER_CREATEUSER) == BST_CHECKED);
  50. StrGetEditText( m_hWnd, IDC_USER_NAME, m_strName );
  51. if( wID == IDOK )
  52. {
  53. tstring strPassword = _T("");
  54. tstring strConfirm = _T("");
  55. StrGetEditText( m_hWnd, IDC_PASSWORD, strPassword );
  56. StrGetEditText( m_hWnd, IDC_CONFIRM, strConfirm );
  57. // Verify the password
  58. if( _tcscmp(strPassword.c_str(), strConfirm.c_str()) != 0 )
  59. {
  60. tstring strMessage = StrLoadString(IDS_ERROR_PASSNOMATCH);
  61. tstring strTitle = StrLoadString(IDS_SNAPINNAME);
  62. ::MessageBox( m_hWnd, strMessage.c_str(), strTitle.c_str(), MB_OK | MB_ICONWARNING );
  63. SecureZeroMemory( (LPTSTR)strPassword.c_str(), sizeof(TCHAR)*strPassword.length() );
  64. SecureZeroMemory( (LPTSTR)strConfirm.c_str(), sizeof(TCHAR)*strConfirm.length() );
  65. return -1;
  66. }
  67. // Create the account
  68. HRESULT hr = S_OK;
  69. if( m_bCreateUser || m_bHashPW )
  70. {
  71. CComBSTR bstrName = m_strName.c_str();
  72. CComBSTR bstrPass = strPassword.c_str();
  73. hr = m_spUsers->AddEx( bstrName, bstrPass );
  74. SecureZeroMemory( (LPOLESTR)bstrPass.m_str, sizeof(OLECHAR)*bstrPass.Length() );
  75. }
  76. else
  77. {
  78. CComBSTR bstrName = m_strName.c_str();
  79. hr = m_spUsers->Add( bstrName );
  80. }
  81. SecureZeroMemory( (LPTSTR)strPassword.c_str(), sizeof(TCHAR)*strPassword.length() );
  82. SecureZeroMemory( (LPTSTR)strConfirm.c_str(), sizeof(TCHAR)*strConfirm.length() );
  83. if ( S_OK == hr )
  84. { // Do we need confirmation text?
  85. BOOL bSAMNameDifferent = FALSE;
  86. VARIANT v;
  87. CComPtr<IP3User> spUser;
  88. VariantInit( &v );
  89. V_VT( &v ) = VT_BSTR;
  90. V_BSTR( &v ) = SysAllocString( m_strName.c_str() );
  91. if ( NULL == V_BSTR( &v ))
  92. hr = E_OUTOFMEMORY;
  93. if ( S_OK == hr )
  94. hr = m_spUsers->get_Item( v, &spUser );
  95. VariantClear( &v );
  96. if ( S_OK == hr )
  97. {
  98. BSTR bstrSAMName = NULL;
  99. hr = spUser->get_SAMName( &bstrSAMName );
  100. if ( S_OK == hr )
  101. {
  102. if ( 0 != _wcsicmp( bstrSAMName, m_strName.c_str() ))
  103. bSAMNameDifferent = TRUE;
  104. SysFreeString( bstrSAMName );
  105. }
  106. else if ( HRESULT_FROM_WIN32( ERROR_DS_INAPPROPRIATE_AUTH ) == hr )
  107. hr = S_OK;
  108. }
  109. if ( S_OK == hr && ( m_bConfirm || bSAMNameDifferent ))
  110. { // Get confirmation text
  111. BSTR bstrConfirm;
  112. hr = spUser->get_ClientConfigDesc( &bstrConfirm );
  113. if ( S_OK == hr )
  114. {
  115. CNewUserConfirmDlg dlgConfirm( bstrConfirm, (m_bConfirm && !bSAMNameDifferent)?false:true);
  116. if ( IDOK == dlgConfirm.DoModal() && !bSAMNameDifferent )
  117. m_bConfirm = !dlgConfirm.isHideDoNotShow();
  118. SysFreeString( bstrConfirm );
  119. }
  120. }
  121. }
  122. else
  123. {
  124. // Failed to add the user
  125. tstring strMessage = StrLoadString(IDS_ERROR_CREATEMAIL);
  126. tstring strTitle = StrLoadString(IDS_SNAPINNAME);
  127. if(HRESULT_FROM_WIN32(ERROR_FILE_EXISTS) == hr)
  128. hr = HRESULT_FROM_WIN32(ERROR_USER_EXISTS);
  129. DisplayError( m_hWnd, strMessage.c_str(), strTitle.c_str(), hr );
  130. return -1;
  131. }
  132. }
  133. EndDialog( wID );
  134. return 0;
  135. }
  136. void CNewUserDlg::EnableButtons()
  137. {
  138. // Check for Password match and Name length
  139. BOOL bPasswordValid = FALSE;
  140. BOOL bChecked = (IsDlgButtonChecked(IDC_USER_CREATEUSER) == BST_CHECKED);
  141. // Get the length of the name
  142. int nNameLen = SendDlgItemMessage( IDC_USER_NAME, WM_GETTEXTLENGTH );
  143. if( !m_bHashPW && !bChecked )
  144. {
  145. bPasswordValid = TRUE;
  146. }
  147. else
  148. {
  149. int nPasswordLen = SendDlgItemMessage( IDC_PASSWORD, WM_GETTEXTLENGTH );
  150. int nConfirmLen = SendDlgItemMessage( IDC_PASSWORD, WM_GETTEXTLENGTH );
  151. bPasswordValid = ((nPasswordLen > 0) || (nConfirmLen > 0));
  152. }
  153. Prefix_EnableWindow( m_hWnd, IDOK, ((nNameLen > 0) && bPasswordValid) );
  154. }