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.

336 lines
8.2 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. // FILE : FxsValid.cpp //
  3. // //
  4. // DESCRIPTION : Fax Validity checks. //
  5. // //
  6. // AUTHOR : yossg //
  7. // //
  8. // HISTORY : //
  9. // Mar 29 2000 yossg Create //
  10. // Jul 4 2000 yossg Add IsLocalServerName //
  11. // //
  12. // Copyright (C) 2000 Microsoft Corporation All Rights Reserved //
  13. /////////////////////////////////////////////////////////////////////////////
  14. #include "StdAfx.h"
  15. #include "FxsValid.h"
  16. #include <windns.h> //DNS_MAX_NAME_BUFFER_LENGTH
  17. /*
  18. - IsNotEmptyString
  19. -
  20. * Purpose:
  21. * To validate that a general string is not empty.
  22. *
  23. * Arguments:
  24. * [in] bstrGenStr - input BSTR
  25. *
  26. * Return:
  27. * TRUE string is not length 0 or spaces only
  28. * FALSE if not
  29. */
  30. BOOL IsNotEmptyString(CComBSTR bstrGenStr)
  31. {
  32. DEBUG_FUNCTION_NAME( _T("IsValidGeneralString"));
  33. int iLen = bstrGenStr.Length();
  34. if (iLen > 0)
  35. {
  36. for(int i = 0; i < iLen; i++ )
  37. {
  38. if( !iswspace( bstrGenStr[i] ) )
  39. {
  40. return(TRUE);
  41. }
  42. }
  43. DebugPrintEx(DEBUG_ERR,
  44. _T("String contains only spaces."));
  45. return FALSE;
  46. }
  47. else
  48. {
  49. DebugPrintEx(DEBUG_ERR,
  50. _T("String length is zero."));
  51. return FALSE;
  52. }
  53. }
  54. /*
  55. - IsValidServerNameString
  56. -
  57. * Purpose:
  58. * To validate string as a server name string.
  59. * This level will return a detailed error message IDS.
  60. *
  61. * Arguments:
  62. * [in] bstrServerName - input BSTR
  63. * [out] puIds - pointer to IDS with error message.
  64. *
  65. * Return:
  66. * TRUE - the string is a valid server name string
  67. * FALSE - if not
  68. */
  69. BOOL IsValidServerNameString(CComBSTR bstrServerName, UINT * puIds, BOOL fIsDNSName /*= FALSE*/)
  70. {
  71. DEBUG_FUNCTION_NAME( _T("IsValidServerNameString"));
  72. int iCount, i, iLength;
  73. BOOL bFirstNonSpaceIsFound = FALSE;
  74. ATLASSERT(bstrServerName);
  75. ATLASSERT(puIds);
  76. //
  77. // Length == 0
  78. //
  79. if ( 0 == ( iCount = bstrServerName.Length() ) )
  80. {
  81. DebugPrintEx(
  82. DEBUG_ERR,
  83. _T("Server name is empty"));
  84. *puIds = IDS_SERVERNAME_EMPTY_STRING;
  85. return FALSE;
  86. }
  87. //
  88. // Length
  89. //
  90. if ( fIsDNSName == FALSE )
  91. {
  92. iLength = MAX_COMPUTERNAME_LENGTH;
  93. }
  94. else
  95. {
  96. iLength = DNS_MAX_NAME_BUFFER_LENGTH;
  97. }
  98. if ( ( iCount = bstrServerName.Length() ) > iLength )
  99. {
  100. DebugPrintEx(
  101. DEBUG_ERR,
  102. _T("Server name is too long"));
  103. *puIds = IDS_SERVERNAME_TOO_LONG;
  104. return FALSE;
  105. }
  106. //
  107. // search for: \ / tabs , ; : " < > * + = | [ ] ?
  108. //
  109. for (i = 0; i < iCount; i++)
  110. {
  111. if (
  112. (bstrServerName[i] == '\\')
  113. ||
  114. (bstrServerName[i] == '/')
  115. ||
  116. (bstrServerName[i] == '\t')
  117. ||
  118. (bstrServerName[i] == ',')
  119. ||
  120. (bstrServerName[i] == ';')
  121. ||
  122. (bstrServerName[i] == ':')
  123. ||
  124. (bstrServerName[i] == '"')
  125. ||
  126. (bstrServerName[i] == '<')
  127. ||
  128. (bstrServerName[i] == '>')
  129. ||
  130. (bstrServerName[i] == '*')
  131. ||
  132. (bstrServerName[i] == '+')
  133. ||
  134. (bstrServerName[i] == '=')
  135. ||
  136. (bstrServerName[i] == '|')
  137. ||
  138. (bstrServerName[i] == '?')
  139. ||
  140. (bstrServerName[i] == '[')
  141. ||
  142. (bstrServerName[i] == ']')
  143. )
  144. {
  145. DebugPrintEx(
  146. DEBUG_ERR,
  147. _T("Server name contains an invalid character."));
  148. *puIds = IDS_SERVERNAME_STRING_CONTAINS_INVALID_CHARACTERS;
  149. return FALSE;
  150. }
  151. //
  152. // At the same loop see if all string is spaces
  153. //
  154. if (!bFirstNonSpaceIsFound)
  155. {
  156. if (bstrServerName[i] != ' ' )
  157. {
  158. bFirstNonSpaceIsFound = TRUE;
  159. }
  160. }
  161. }
  162. if (!bFirstNonSpaceIsFound)
  163. {
  164. DebugPrintEx(
  165. DEBUG_ERR,
  166. _T("Server name string includes only spaces."));
  167. *puIds = IDS_SERVERNAME_EMPTY_STRING;
  168. return FALSE;
  169. }
  170. return TRUE;
  171. }
  172. /*
  173. - IsValidPortNumber
  174. -
  175. * Purpose:
  176. * To validate that string contains a valid port number.
  177. * This level will return a detailed error message IDS.
  178. *
  179. * Arguments:
  180. * [in] bstrPort - input BSTR
  181. * [out] pdwPortVal - pointer to DWORD port value
  182. * in case of success.
  183. * [out] puIds - pointer to IDS with error message
  184. * in case of failure.
  185. *
  186. * Return:
  187. * TRUE - the string containts a valid port number
  188. * FALSE - if not.
  189. */
  190. BOOL IsValidPortNumber(CComBSTR bstrPort, DWORD * pdwPortVal, UINT * puIds)
  191. {
  192. DEBUG_FUNCTION_NAME( _T("IsValidPortNumber"));
  193. DWORD dwPort;
  194. ATLASSERT(bstrPort);
  195. //
  196. // Length == 0
  197. //
  198. if (0 == bstrPort.Length())
  199. {
  200. DebugPrintEx(
  201. DEBUG_ERR,
  202. _T("Port string is empty"));
  203. *puIds = IDS_PORT_EMPTY_STRING;
  204. return FALSE;
  205. }
  206. //
  207. // numerical value;
  208. //
  209. if (1 != swscanf (bstrPort, _T("%ld"), &dwPort))
  210. {
  211. *puIds = IDS_PORT_NOT_NUMERIC;
  212. DebugPrintEx(
  213. DEBUG_ERR,
  214. _T("port string is not a number"));
  215. return FALSE;
  216. }
  217. //
  218. // MIN_PORT_NUM <= dwPort <= MAX_PORT_NUM
  219. //
  220. if ( ((int)dwPort > FXS_MAX_PORT_NUM) || ((int)dwPort < FXS_MIN_PORT_NUM))
  221. {
  222. DebugPrintEx(
  223. DEBUG_ERR,
  224. _T("Port number is out off allowed values"));
  225. *puIds = IDS_INVALID_PORT_NUM;
  226. return FALSE;
  227. }
  228. *pdwPortVal = dwPort;
  229. return TRUE;
  230. }
  231. /*
  232. + IsLocalComputer
  233. +
  234. * Purpose:
  235. * To see if the server name is the local computer name.
  236. *
  237. * Arguments:
  238. * [in] lpszComputer : the machine name.
  239. *
  240. - Return:
  241. - TRUE or FALSE
  242. */
  243. BOOL IsLocalServerName(IN LPCTSTR lpszComputer)
  244. {
  245. DEBUG_FUNCTION_NAME( _T("IsLocalComputer"));
  246. //
  247. // Pre conditions
  248. //
  249. ATLASSERT(lpszComputer);
  250. if (!lpszComputer || !*lpszComputer)
  251. {
  252. return TRUE;
  253. }
  254. if ( _tcslen(lpszComputer) > 2 && ( 0 == wcsncmp( lpszComputer , _T("\\\\") , 2 )) )
  255. {
  256. lpszComputer = _tcsninc(lpszComputer, 2);
  257. }
  258. //
  259. // Computer Name Compare
  260. //
  261. BOOL bReturn = FALSE;
  262. DWORD dwErr = 0;
  263. TCHAR szBuffer[DNS_MAX_NAME_BUFFER_LENGTH];
  264. DWORD dwSize = DNS_MAX_NAME_BUFFER_LENGTH;
  265. // 1st: compare against local Netbios computer name
  266. if ( !GetComputerNameEx(ComputerNameNetBIOS, szBuffer, &dwSize) )
  267. {
  268. dwErr = GetLastError();
  269. }
  270. else
  271. {
  272. bReturn = (0 == lstrcmpi(szBuffer, lpszComputer));
  273. if (!bReturn)
  274. {
  275. // 2nd: compare against local Dns computer name
  276. dwSize = DNS_MAX_NAME_BUFFER_LENGTH;
  277. if (GetComputerNameEx(ComputerNameDnsFullyQualified, szBuffer, &dwSize))
  278. {
  279. bReturn = (0 == lstrcmpi(szBuffer, lpszComputer));
  280. }
  281. else
  282. {
  283. dwErr = GetLastError();
  284. }
  285. }
  286. }
  287. if (dwErr)
  288. {
  289. DebugPrintEx(DEBUG_ERR,
  290. _T("Failed to discover if is a local server (ec = %x)"), dwErr);
  291. }
  292. return bReturn;
  293. }