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.

178 lines
5.9 KiB

  1. #include "stdafx.h"
  2. #include "resource.h"
  3. #include "shlwapi.h"
  4. #include "validate.h"
  5. int CValidate::ValidateParams(HWND hDlg, HINSTANCE hInst, TCHAR *szDesc, BOOL bServer)
  6. {
  7. TCHAR szTitle[MAX_PATH] = _T(""), szText[MAX_PATH] = _T("");
  8. LoadString(hInst, IDS_MAINWINDOWTITLE, szTitle, SIZEOF_TCHARBUFFER(szTitle));
  9. LoadString(hInst, bServer ? IDS_INVALID_SERVER_NAME : IDS_INVALID_DOMAIN_NAME,
  10. szText, SIZEOF_TCHARBUFFER(szText));
  11. if (!*szDesc)
  12. {
  13. TCHAR szText[MAX_PATH] = _T("");
  14. LoadString(hInst, IDS_E_SPECIFY_SRV, szText, SIZEOF_TCHARBUFFER(szText));
  15. MessageBox(hDlg, szText, szTitle, MB_OK|MB_ICONSTOP);
  16. return 1;
  17. }
  18. if (bServer) //The characters cannot contain even spaces and tabs.
  19. {
  20. if ( (NULL != _tcschr(szDesc, _T(' '))) || (NULL != _tcschr(szDesc, _T('\t'))) )
  21. {
  22. MessageBox(hDlg, szText, szTitle, MB_OK|MB_ICONSTOP);
  23. return 1;
  24. }
  25. }
  26. //Kill all the leading spaces and trailing spaces.
  27. StrTrim(szDesc, L" \t");
  28. //Not all characters can be white spaces. Now that all the leading and
  29. //trailing spaces are removed, verify if the string has become empty.
  30. if (!*szDesc)
  31. {
  32. TCHAR szText[MAX_PATH] = _T("");
  33. LoadString(hInst, IDS_ALL_SPACES, szText, SIZEOF_TCHARBUFFER(szText));
  34. MessageBox(hDlg, szText, szTitle, MB_OK|MB_ICONSTOP);
  35. return 1;
  36. }
  37. //The characters ; : " < > * + = \ | ? , are illegal in machine name.
  38. while (*szDesc)
  39. {
  40. if ( (*szDesc == _T(';')) || (*szDesc == _T(':')) || (*szDesc == _T('"')) || (*szDesc == _T('<')) ||
  41. (*szDesc == _T('>')) || (*szDesc == _T('*')) || (*szDesc == _T('+')) || (*szDesc == _T('=')) ||
  42. (*szDesc == _T('\\')) || (*szDesc == _T('|')) || (*szDesc == _T(',')) || (*szDesc == _T('?')) )
  43. {
  44. MessageBox(hDlg, szText, szTitle, MB_OK|MB_ICONSTOP);
  45. return 1;
  46. }
  47. szDesc = CharNext(szDesc);
  48. }
  49. return 0;
  50. }
  51. BOOL CValidate::IsValidUserName(TCHAR *szDesc)
  52. {
  53. //A user name cannot consist solely of periods (.) and spaces.
  54. //NOTE:- " " is invalid. ")))))" is invalid.
  55. //But "(((.) )" is a valid string. (Confusion?)
  56. TCHAR szTemp[CL_MAX_USERNAME_LENGTH + 1] = _T("");
  57. lstrcpy(szTemp, szDesc);
  58. StrTrim(szTemp, _T(" "));
  59. if (!*szTemp)
  60. return FALSE;
  61. lstrcpy(szTemp, szDesc);
  62. StrTrim(szTemp, _T("."));
  63. if (!*szTemp)
  64. return FALSE;
  65. lstrcpy(szTemp, szDesc);
  66. StrTrim(szTemp, _T("("));
  67. if (!*szTemp)
  68. return FALSE;
  69. lstrcpy(szTemp, szDesc);
  70. StrTrim(szTemp, _T(")"));
  71. if (!*szTemp)
  72. return FALSE;
  73. return TRUE; //OK
  74. }
  75. int CValidate::ValidateUserName(HWND hwnd, HINSTANCE hInst, TCHAR *szDesc)
  76. {
  77. TCHAR szTitle[MAX_PATH] = _T(""), szText[MAX_PATH] = _T("");
  78. LoadString(hInst, IDS_MAINWINDOWTITLE, szTitle, SIZEOF_TCHARBUFFER(szTitle));
  79. //At this stage the user name can be empty.
  80. if (!*szDesc)
  81. return 0; //No problems.
  82. if (*szDesc)
  83. {
  84. if (!IsValidUserName(szDesc))
  85. {
  86. LoadString(hInst, IDS_INVALID_PARAMS, szText, SIZEOF_TCHARBUFFER(szText));
  87. MessageBox(hwnd, szText, szTitle, MB_OK|MB_ICONSTOP);
  88. return 1;
  89. }
  90. }
  91. //The characters " / \ [ ] : ; | = , + * ? < > are illegal in user name.
  92. while (*szDesc)
  93. {
  94. if ( (*szDesc == _T('"')) || (*szDesc == _T('/')) || (*szDesc == _T('\\')) || (*szDesc == _T('[')) ||
  95. (*szDesc == _T(']')) || (*szDesc == _T(':')) || (*szDesc == _T(';')) || (*szDesc == _T('|')) ||
  96. (*szDesc == _T('=')) || (*szDesc == _T(',')) || (*szDesc == _T('+')) ||(*szDesc == _T('*')) ||
  97. (*szDesc == _T('?')) || (*szDesc == _T('<')) || (*szDesc == _T('>')))
  98. {
  99. LoadString(hInst, IDS_INVALID_USER_NAME, szText, SIZEOF_TCHARBUFFER(szText));
  100. MessageBox(hwnd, szText, szTitle, MB_OK|MB_ICONSTOP);
  101. return 1;
  102. }
  103. szDesc = CharNext(szDesc);
  104. }
  105. return 0;
  106. }
  107. BOOL
  108. CValidate::Validate(HWND hDlg, HINSTANCE hInst)
  109. {
  110. //Validate the description.
  111. TCHAR szBuf[MAX_PATH] = _T("");
  112. GetDlgItemText(hDlg, IDC_DESCRIPTION, szBuf, SIZEOF_TCHARBUFFER(szBuf) - 1);
  113. TCHAR szTitle[MAX_PATH] = _T("");
  114. LoadString(hInst, IDS_MAINWINDOWTITLE, szTitle, SIZEOF_TCHARBUFFER(szTitle));
  115. //Do a validation for the server name entered.
  116. GetDlgItemText(hDlg, IDC_SERVER, szBuf, SIZEOF_TCHARBUFFER(szBuf) - 1);
  117. if (0 < ValidateParams(hDlg, hInst, szBuf, TRUE))
  118. {
  119. HWND hEdit = GetDlgItem(hDlg, IDC_SERVER);
  120. SetFocus(hEdit);
  121. SendMessage(hEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) -1);
  122. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, TRUE);
  123. return FALSE;
  124. }
  125. GetDlgItemText(hDlg, IDC_USERNAME, szBuf, SIZEOF_TCHARBUFFER(szBuf) - 1);
  126. if (0 < ValidateUserName(hDlg, hInst, szBuf))
  127. {
  128. HWND hEdit= GetDlgItem(hDlg, IDC_USERNAME);
  129. SendMessage(hEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) -1);
  130. SetFocus(hEdit);
  131. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, TRUE);
  132. return FALSE;
  133. }
  134. GetDlgItemText(hDlg, IDC_DOMAIN, szBuf, SIZEOF_TCHARBUFFER(szBuf) - 1);
  135. if (*szBuf)
  136. {
  137. if (0 < ValidateParams(hDlg, hInst, szBuf))
  138. {
  139. HWND hEdit = GetDlgItem(hDlg, IDC_DOMAIN);
  140. //Now the domain name is stripped off any leading and trailing
  141. //spaces and tabs. Set this as the new text.
  142. SetDlgItemText(hDlg, IDC_DOMAIN, szBuf);
  143. SetFocus(hEdit);
  144. SendMessage(hEdit, EM_SETSEL, (WPARAM) 0, (LPARAM) -1);
  145. SetWindowLongPtr(hDlg, DWLP_MSGRESULT, TRUE);
  146. return FALSE;
  147. }
  148. SetDlgItemText(hDlg, IDC_DOMAIN, szBuf);
  149. }
  150. return TRUE;
  151. }