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.

398 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: dnsutil.h
  8. //
  9. //--------------------------------------------------------------------------
  10. #ifndef _DNSUTIL_H
  11. #define _DNSUTIL_H
  12. //#define NTRAID_628931
  13. /////////////////////////////////////////////////////////////////
  14. // C++ helper classes wrapping the raw DNS RPC API's
  15. #define DNS_TYPE_UNK DNS_TYPE_NULL // we treat the two as the same
  16. #ifdef USE_NDNC
  17. typedef enum
  18. {
  19. none = 0, // not AD integrated
  20. forest,
  21. domain,
  22. w2k,
  23. custom
  24. } ReplicationType;
  25. #endif
  26. //////////////////////////////////////////////////////////////
  27. // macros defining the DNS server version
  28. #define DNS_SRV_MAJOR_VERSION(dw) (LOBYTE(LOWORD(dw)))
  29. #define DNS_SRV_MINOR_VERSION(dw) (HIBYTE(LOWORD(dw)))
  30. #define DNS_SRV_BUILD_NUMBER(dw) (HIWORD(dw))
  31. #define DNS_SRV_MAJOR_VERSION_NT_4 (0x4)
  32. #define DNS_SRV_MAJOR_VERSION_NT_5 (0x5)
  33. #define DNS_SRV_MINOR_VERSION_WIN2K (0x0)
  34. #define DNS_SRV_MINOR_VERSION_WHISTLER (0x1)
  35. #define DNS_SRV_BUILD_NUMBER_WHISTLER (2230)
  36. #define DNS_SRV_BUILD_NUMBER_WHISTLER_NEW_SECURITY_SETTINGS (2474)
  37. #define DNS_SRV_VERSION_NT_4 DNS_SRV_MAJOR_VERSION_NT_4
  38. //////////////////////////////////////////////////////////////
  39. // MACROS & DEFINES
  40. #define MAX_DNS_NAME_LEN 255
  41. #define IP_OCTET_COUNT 4
  42. // record enumeration
  43. #define NEXT_DWORD(cb) ((cb + 3) & ~3)
  44. //#define IS_DWORD_ALIGNED(pv) (((int)(void *)pv & 3) == 0)
  45. #define DNS_SIZE_OF_DNS_RPC_RR(pDnsRecord)\
  46. (SIZEOF_DNS_RPC_RECORD_HEADER + pDnsRecord->wDataLength)
  47. #define DNS_NEXT_RECORD(pDnsRecord) \
  48. (DNS_RPC_RECORD *)((BYTE*)pDnsRecord + ((DNS_SIZE_OF_DNS_RPC_RR(pDnsRecord) + 3) & ~3))
  49. // RR field manipulation
  50. //#define INET_NTOA(s) (inet_ntoa (*(in_addr *)&(s)))
  51. #define REVERSE_WORD_BYTES(w) \
  52. MAKEWORD(HIBYTE(w), LOBYTE(w))
  53. // DNS keywords
  54. #define INADDR_ARPA_SUFFIX _T(".in-addr.arpa")
  55. #define ARPA_SUFFIX _T(".arpa")
  56. #define IP6_INT_SUFFIX _T(".ipv6.int")
  57. #define AUTOCREATED_0 _T("0.in-addr.arpa")
  58. #define AUTOCREATED_127 _T("127.in-addr.arpa")
  59. #define AUTOCREATED_255 _T("255.in-addr.arpa")
  60. #define QUESTION_MARK_PREFIX _T("?")
  61. // formatting of IPv4 address to string
  62. extern LPCWSTR g_szIpStringFmt;
  63. #define IP_STRING_MAX_LEN 20 // safe size for a wsprintf buffer
  64. #define IP_STRING_FMT_ARGS(x) \
  65. FOURTH_IPADDRESS(x), THIRD_IPADDRESS(x), SECOND_IPADDRESS(x), FIRST_IPADDRESS(x)
  66. //////////////////////////////////////////////////////////////
  67. // macros and functions for UTF8 <-> UNICODE conversion
  68. // modified from the ATL 1.1 ones in atlbase.h
  69. inline LPWSTR WINAPI DnsUtf8ToWHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  70. {
  71. _ASSERTE(lpa != NULL);
  72. _ASSERTE(lpw != NULL);
  73. // verify that no illegal character present
  74. // since lpw was allocated based on the size of lpa
  75. // don't worry about the number of chars
  76. lpw[0] = '\0';
  77. MultiByteToWideChar(CP_UTF8, 0, lpa, -1, lpw, nChars);
  78. return lpw;
  79. }
  80. inline LPSTR WINAPI DnsWToUtf8Helper(LPSTR lpa, LPCWSTR lpw, int nChars)
  81. {
  82. _ASSERTE(lpw != NULL);
  83. _ASSERTE(lpa != NULL);
  84. // verify that no illegal character present
  85. // since lpa was allocated based on the size of lpw
  86. // don't worry about the number of chars
  87. lpa[0] = '\0';
  88. WideCharToMultiByte(CP_UTF8, 0, lpw, -1, lpa, nChars, NULL, NULL);
  89. return lpa;
  90. }
  91. inline void WINAPI DnsUtf8ToWCStringHelper(CString& sz, LPCSTR lpa, int nBytes)
  92. {
  93. // Make sure we return an empty string when either the string is null or
  94. // the first character is null or the string length is defined as zero
  95. if ( (lpa == NULL) ||
  96. ((lpa != NULL) && (lpa[0] == L'\0')) ||
  97. (nBytes <= 0) )
  98. {
  99. sz.Empty();
  100. return;
  101. }
  102. // allocate buffer in string (worst case scenario + NULL)
  103. int nWideChars = nBytes + 1;
  104. LPWSTR lpw = sz.GetBuffer(nWideChars);
  105. int nWideCharsLen = MultiByteToWideChar(CP_UTF8, 0, lpa, nBytes, lpw, nWideChars);
  106. sz.ReleaseBuffer(nWideCharsLen);
  107. }
  108. #define UTF8_LEN lstrlenA
  109. #define UTF8_TO_W(lpa) (\
  110. ((LPCSTR)lpa == NULL) ? NULL : (\
  111. _convert = (lstrlenA(lpa)+1),\
  112. DnsUtf8ToWHelper((LPWSTR) alloca(_convert*2), lpa, _convert)))
  113. #define W_TO_UTF8(lpw) (\
  114. ((LPCWSTR)lpw == NULL) ? NULL : (\
  115. _convert = (lstrlenW(lpw)+1)*4,\
  116. DnsWToUtf8Helper((LPSTR) alloca(_convert), lpw, _convert)))
  117. #define UTF8_TO_CW(lpa) ((LPCWSTR)UTF8_TO_W(lpa))
  118. #define W_TO_CUTF8(lpw) ((LPCSTR)W_TO_UTF8(lpw))
  119. //////////////////////////////////////////////////////////////
  120. // estimate UTF8 len of a UNICODE string
  121. inline int UTF8StringLen(LPCWSTR lpszWideString)
  122. {
  123. USES_CONVERSION;
  124. LPSTR lpszUTF8 = W_TO_UTF8(lpszWideString);
  125. return UTF8_LEN(lpszUTF8);
  126. }
  127. ///////////////////////////////////////////////////////////////
  128. // General Purpose Utility Functions
  129. WORD CharToNumber(WCHAR ch);
  130. BYTE HexCharToByte(WCHAR ch);
  131. BOOL IsValidIPString(LPCWSTR lpsz);
  132. DNS_STATUS ValidateDnsNameAgainstServerFlags(LPCWSTR lpszName,
  133. DNS_NAME_FORMAT format,
  134. DWORD serverNameChecking);
  135. DWORD IPStringToAddr(LPCWSTR lpsz);
  136. inline void FormatIpAddress(LPWSTR lpszBuf, DWORD dwIpAddr)
  137. {
  138. wsprintf(lpszBuf, g_szIpStringFmt, IP_STRING_FMT_ARGS(dwIpAddr));
  139. }
  140. inline void FormatIpAddress(CString& szBuf, DWORD dwIpAddr)
  141. {
  142. LPWSTR lpszBuf = szBuf.GetBuffer(IP_STRING_MAX_LEN);
  143. wsprintf(lpszBuf, g_szIpStringFmt, IP_STRING_FMT_ARGS(dwIpAddr));
  144. szBuf.ReleaseBuffer();
  145. }
  146. void ReverseString(LPWSTR p, LPWSTR q);
  147. int ReverseIPString(LPWSTR lpsz);
  148. BOOL RemoveInAddrArpaSuffix(LPWSTR lpsz);
  149. BOOL IsValidDnsZoneName(CString& szName, BOOL bFwd);
  150. #ifdef NTRAID_628931
  151. HRESULT GetWideCharZoneRootHints(CString& zoneRootHints);
  152. #endif //NTRAID_628931
  153. ///////////////////////////////////////////////////////////////
  154. // definitions and helper functions for IPv6 format
  155. void FormatIPv6Addr(CString& szAddr, IPV6_ADDRESS* ipv6Addr);
  156. ///////////////////////////////////////////////////////////////
  157. // flags for different states and options of DNS objects
  158. // use LOWORD only because used in CTreeNode::m_dwNodeFlags
  159. #define TN_FLAG_DNS_RECORD_FULL_NAME (0x01) // use full name to display resource records
  160. #define TN_FLAG_DNS_RECORD_SHOW_TTL (0x02) // show TLL in record properties
  161. typedef CArray<IP_ADDRESS,IP_ADDRESS> CIpAddressArray;
  162. //////////////////////////////////////////////////////////////////////////////
  163. // CBlob
  164. template <class T> class CBlob
  165. {
  166. public:
  167. CBlob()
  168. {
  169. m_pData = NULL;
  170. m_nSize = 0;
  171. }
  172. ~CBlob()
  173. {
  174. Empty();
  175. }
  176. UINT GetSize() { return m_nSize;}
  177. T* GetData() { return m_pData;}
  178. void Set(T* pData, UINT nSize)
  179. {
  180. ASSERT(pData != NULL);
  181. Empty();
  182. if (nSize > 0 &&
  183. sizeof(T) > 0)
  184. {
  185. m_pData = (BYTE*)malloc(sizeof(T)*nSize);
  186. ASSERT(m_pData != NULL);
  187. if (m_pData != NULL)
  188. {
  189. m_nSize = nSize;
  190. memcpy(m_pData, pData, sizeof(T)*nSize);
  191. }
  192. }
  193. }
  194. UINT Get(T* pData)
  195. {
  196. ASSERT(pData != NULL);
  197. memcpy(pData, m_pData, sizeof(T)*m_nSize);
  198. return m_nSize;
  199. }
  200. void Empty()
  201. {
  202. if (m_pData != NULL)
  203. {
  204. free(m_pData);
  205. m_pData = NULL;
  206. m_nSize = 0;
  207. }
  208. }
  209. private:
  210. T* m_pData;
  211. UINT m_nSize;
  212. };
  213. typedef CBlob<BYTE> CByteBlob;
  214. typedef CBlob<WORD> CWordBlob;
  215. //////////////////////////////////////////////////////////////////////////////
  216. // CDNSServerInfoEx
  217. // definitions for the extended server flags (NT 5.0) only
  218. #define SERVER_REGKEY_ARR_SIZE 6
  219. #define SERVER_REGKEY_ARR_INDEX_NO_RECURSION 0
  220. #define SERVER_REGKEY_ARR_INDEX_BIND_SECONDARIES 1
  221. #define SERVER_REGKEY_ARR_INDEX_STRICT_FILE_PARSING 2
  222. #define SERVER_REGKEY_ARR_INDEX_ROUND_ROBIN 3
  223. #define SERVER_REGKEY_ARR_LOCAL_NET_PRIORITY 4
  224. #define SERVER_REGKEY_ARR_CACHE_POLLUTION 5
  225. extern LPCSTR _DnsServerRegkeyStringArr[];
  226. class CDNSServerInfoEx : public CObjBase
  227. {
  228. public:
  229. CDNSServerInfoEx();
  230. ~CDNSServerInfoEx();
  231. DNS_STATUS Query(LPCTSTR lpszServerName);
  232. BOOL HasData(){ return m_pServInfo != NULL;}
  233. void FreeInfo();
  234. // obtained from DnsGetServerInfo() RPC call (NT 4.0)
  235. DNS_RPC_SERVER_INFO* m_pServInfo;
  236. // error codes for query
  237. DNS_STATUS m_errServInfo;
  238. private:
  239. void QueryRegKeyOptionsHelper(LPCSTR lpszAnsiServerName);
  240. };
  241. //////////////////////////////////////////////////////////////////////////////
  242. // CDNSZoneInfoEx
  243. class CDNSZoneInfoEx : public CObjBase
  244. {
  245. public:
  246. CDNSZoneInfoEx();
  247. ~CDNSZoneInfoEx();
  248. DNS_STATUS Query(LPCTSTR lpszServerName, LPCTSTR lpszZoneName, DWORD dwServerVersion);
  249. BOOL HasData(){ return m_pZoneInfo != NULL;}
  250. void FreeInfo();
  251. // struct obtained from DnssrvGetZoneInfo() 5.0 RPC call (NT 4.0 format)
  252. DNS_RPC_ZONE_INFO* m_pZoneInfo;
  253. // obtained from DnssrvQueryZoneDwordProperty() (NT 5.0)
  254. // UINT m_nAllowsDynamicUpdate;
  255. // error codes for query
  256. DNS_STATUS m_errZoneInfo;
  257. // DNS_STATUS m_errAllowsDynamicUpdate;
  258. };
  259. ///////////////////////////////////////////////////////////////////////////////
  260. //////////////////// ERROR MESSAGES HANDLING //////////////////////////////////
  261. ///////////////////////////////////////////////////////////////////////////////
  262. int DNSMessageBox(LPCTSTR lpszText, UINT nType = MB_OK);
  263. int DNSMessageBox(UINT nIDPrompt, UINT nType = MB_OK);
  264. int DNSErrorDialog(DNS_STATUS err, LPCTSTR lpszErrorMsg = NULL);
  265. int DNSErrorDialog(DNS_STATUS err, UINT nErrorMsgID);
  266. void DNSDisplaySystemError(DWORD dwErr);
  267. void DNSCreateErrorMessage(DNS_STATUS err, UINT nErrorMsgID, CString& refszMessage);
  268. int DNSConfirmOperation(UINT nMsgID, CTreeNode* p);
  269. class CDNSErrorInfo
  270. {
  271. public:
  272. static BOOL GetErrorString(DNS_STATUS err, CString& szError);
  273. static BOOL GetErrorStringFromTable(DNS_STATUS err, CString& szError);
  274. static BOOL GetErrorStringFromWin32(DNS_STATUS err, CString& szError);
  275. };
  276. ///////////////////////////////////////////////////////////////////////////////
  277. //
  278. // Security KEY, SIG 6-bit values to base64 character mapping
  279. //
  280. #define SECURITY_PAD_CHAR (L'=')
  281. WCHAR Dns_SecurityBase64CharToBits(IN WCHAR wch64);
  282. DNS_STATUS Dns_SecurityBase64StringToKey(OUT PBYTE pKey,
  283. OUT PDWORD pKeyLength,
  284. IN PWSTR pchString,
  285. IN DWORD cchLength);
  286. PWSTR Dns_SecurityKeyToBase64String(IN PBYTE pKey,
  287. IN DWORD KeyLength,
  288. OUT PWSTR pchBuffer);
  289. // NOTICE-2002/04/24-artm ntraid#ntbug9-547641
  290. // Unused functions need to be removed. I've left in comments in
  291. // case they are later needed.
  292. //DNS_STATUS Dns_SecurityHexToKey(OUT PBYTE pKey,
  293. // OUT PDWORD pKeyLength,
  294. // IN PWSTR pchString,
  295. // IN DWORD cchLength);
  296. //
  297. //void Dns_SecurityKeyToHexString(PBYTE pKey,
  298. // DWORD KeyLength,
  299. // CString& strref);
  300. void TimetToFileTime( time_t t, LPFILETIME pft );
  301. DWORD FileTimeToTimet(FILETIME* pft);
  302. void ConvertTTLToSystemTime(TIME_ZONE_INFORMATION*,
  303. DWORD dwTTL,
  304. SYSTEMTIME* pSysTime);
  305. DWORD ConvertSystemTimeToTTL(SYSTEMTIME* pSysTime);
  306. BOOL ConvertTTLToLocalTimeString(const DWORD dwTTL,
  307. CString& strref);
  308. CString Base64BLOBToString(PBYTE blob, DWORD blobSizeInBytes);
  309. CString Base64BLOBToString(CByteBlob& blob);
  310. #endif // _DNSUTIL_H