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.

427 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. rdns.hxx
  5. Abstract:
  6. Reverse DNS service
  7. Author:
  8. Philippe Choquier (phillich) 5-june-1996
  9. --*/
  10. #if !defined(_RDNS_INCLUDE)
  11. #define _RDNS_INCLUDE
  12. typedef LPVOID DNSARG;
  13. typedef void (*DNSFUNC)( DNSARG, BOOL, LPSTR );
  14. #define RDNS_REQUEST_TYPE_IP2DNS 0
  15. #define RDNS_REQUEST_TYPE_DNS2IP 1
  16. #define SIZEOF_IP_ADDRESS (4)
  17. typedef struct _DNSFUNCDESC
  18. {
  19. DWORD dwRequestType;
  20. DNSFUNC pFunc;
  21. } DNSFUNCDESC, *PDNSFUNCDESC;
  22. extern BOOL InitRDns();
  23. extern void TerminateRDns();
  24. BOOL
  25. AsyncHostByAddr(
  26. PDNSFUNCDESC pFunc, // will store DNS name, post dummy completion status
  27. // if NULL ( or g_cMaxThreadLimit==0 ) then sync request
  28. DNSARG pArg, // ptr to be passed to FUNC
  29. struct sockaddr *pHostAddr,
  30. BOOL *pfSync, // updated with TRUE if sync call
  31. LPSTR pName,
  32. DWORD dwMaxNameLen
  33. );
  34. BOOL
  35. AsyncAddrByHost(
  36. PDNSFUNCDESC pFunc, // will store DNS name, post dummy completion status
  37. // if NULL ( or g_cMaxThreadLimit==0 ) then sync request
  38. DNSARG pArg, // ptr to be passed to FUNC
  39. struct sockaddr *pHostAddr,
  40. BOOL *pfSync, // updated with TRUE if sync call
  41. LPSTR pName
  42. );
  43. BOOL
  44. FireUpNewThread(
  45. PDNSFUNCDESC pFunc,
  46. DNSARG pArg,
  47. LPVOID pOvr
  48. );
  49. #define XAR_GRAIN 256
  50. //
  51. // extensible array class
  52. //
  53. class XAR {
  54. public:
  55. XAR() { m_fDidAlloc = FALSE; m_pAlloc = NULL; m_cAlloc = m_cUsed = 0; }
  56. ~XAR() { if ( m_fDidAlloc ) LocalFree( m_pAlloc ); }
  57. BOOL Init( LPBYTE p=NULL, DWORD c=0) { m_fDidAlloc = FALSE; m_pAlloc = p; m_cAlloc = m_cUsed = c; return TRUE; }
  58. VOID Terminate()
  59. {
  60. if ( m_fDidAlloc )
  61. {
  62. LocalFree( m_pAlloc );
  63. }
  64. m_fDidAlloc = FALSE;
  65. m_pAlloc = NULL;
  66. m_cAlloc = m_cUsed = 0;
  67. }
  68. BOOL Resize( DWORD dwDelta );
  69. DWORD GetUsed() { return m_cUsed; }
  70. VOID SetUsed( DWORD c ) { m_cUsed = c; }
  71. VOID AdjustUsed( int c ) { m_cUsed += (DWORD)c; }
  72. LPBYTE GetAlloc() { return m_pAlloc; }
  73. private:
  74. LPBYTE m_pAlloc;
  75. DWORD m_cAlloc;
  76. DWORD m_cUsed;
  77. BOOL m_fDidAlloc;
  78. } ;
  79. //
  80. // This type defines a relocatable index inside a dynamic array.
  81. // to allow easy fixups when part of the array is to be extended/shrinked
  82. // index are identified by setting bit 31 to 1. Other DWORD in the reference
  83. // part of the array are assumed to have bit 31 set to 0.
  84. // The size of the reference part of the array is defined by cRefSize
  85. //
  86. typedef DWORD SELFREFINDEX;
  87. // combine array base address with SELFREFINDEX
  88. #define MAKEPTR(a,b) ((LPBYTE)(a)+((b)&0x7fffffff))
  89. // build a SELFREFINDEX from an offset in array
  90. #define MAKEREF(a) ((a)|0x80000000)
  91. // build an offset from a SELFREFINDEX
  92. #define MAKEOFFSET(a) ((a)&0x7fffffff)
  93. //
  94. // ADDRESS_CHECK_LIST Flags. bit31 must not be used.
  95. //
  96. #define RDNS_FLAG_DODNS2IPCHECK 0x00000001
  97. // uses non-standard extension : zero-sized array in struct
  98. #pragma warning(disable:4200)
  99. // array header
  100. typedef struct _ADDRESS_CHECK_LIST {
  101. SELFREFINDEX iDenyAddr; // address deny list
  102. // points to ADDRESS_HEADER
  103. SELFREFINDEX iGrantAddr; // address grant list
  104. // points to ADDRESS_HEADER
  105. SELFREFINDEX iDenyName; // DNS name deny list
  106. // points to NAME_HEADER
  107. SELFREFINDEX iGrantName; // DNS name grant list
  108. // points to NAME_HEADER
  109. DWORD dwFlags;
  110. DWORD cRefSize; // size of reference area ( in bytes )
  111. } ADDRESS_CHECK_LIST, *PADDRESS_CHECK_LIST;
  112. typedef struct _ADDRESS_LIST_ENTRY {
  113. DWORD iFamily;
  114. DWORD cAddresses;
  115. DWORD cFullBytes;
  116. DWORD LastByte;
  117. SELFREFINDEX iFirstAddress; // points to array of addresses
  118. // which size are derived from iFamily
  119. } ADDRESS_LIST_ENTRY, *PADDRESS_LIST_ENTRY;
  120. typedef struct _ADDRESS_HEADER {
  121. DWORD cEntries; // # of Entries[]
  122. DWORD cAddresses; // total # of addresses in all
  123. // ADDRESS_LIST_ENTRY
  124. ADDRESS_LIST_ENTRY Entries[];
  125. } ADDRESS_HEADER, *PADDRESS_HEADER ;
  126. typedef struct _NAME_LIST_ENTRY {
  127. DWORD cComponents; // # of DNS components
  128. DWORD cNames;
  129. SELFREFINDEX iName[]; // array of references to DNS names
  130. } NAME_LIST_ENTRY, *PNAME_LIST_ENTRY;
  131. typedef struct _NAME_HEADER {
  132. DWORD cEntries;
  133. DWORD cNames; // total # of names for all Entries[]
  134. //NAME_LIST_ENTRY Entries[0]; // array of name classes
  135. } NAME_HEADER, *PNAME_HEADER ;
  136. typedef struct ADDRCMPDESC {
  137. LPBYTE pMask;
  138. UINT cFullBytes;
  139. UINT LastByte;
  140. UINT cSizeAddress;
  141. } ADDRCMPDESC, *PADDRCMPDESC;
  142. typedef struct NAMECMPDESC {
  143. LPVOID pName;
  144. LPBYTE pBase;
  145. } NAMECMPDESC, *PNAMECMPDESC;
  146. typedef LPVOID ADDRCHECKARG;
  147. typedef void (*ADDRCHECKFUNC)(ADDRCHECKARG, BOOL );
  148. typedef void (*ADDRCHECKFUNCEX)(ADDRCHECKARG, BOOL, LPSTR );
  149. typedef int (__cdecl *CMPFUNC)(const void*, const void*, LPVOID);
  150. #define SIZE_FAST_REVERSE_DNS 128
  151. enum AC_RESULT {
  152. AC_NOT_CHECKED,
  153. AC_IN_DENY_LIST,
  154. AC_NOT_IN_DENY_LIST, // deny list present but not in deny list
  155. AC_IN_GRANT_LIST,
  156. AC_NOT_IN_GRANT_LIST, // grant list present but not in grant list
  157. AC_NO_LIST
  158. } ;
  159. #define DNSLIST_FLAG_NOSUBDOMAIN 0x80000000
  160. #define DNSLIST_FLAGS 0x80000000 // bitmask of all flags
  161. class ADDRESS_CHECK {
  162. public:
  163. ADDRESS_CHECK()
  164. {
  165. m_pszDnsName = NULL;
  166. m_fDnsResolved = FALSE;
  167. }
  168. ~ADDRESS_CHECK()
  169. {
  170. if ( m_pszDnsName != NULL )
  171. {
  172. LocalFree( m_pszDnsName );
  173. m_pszDnsName = NULL;
  174. }
  175. }
  176. //
  177. BOOL BindCheckList( LPBYTE p = NULL, DWORD c = 0 );
  178. VOID UnbindCheckList() { m_Storage.Terminate(); }
  179. BOOL BindAddr( struct sockaddr* pAddr )
  180. {
  181. m_pAddr = pAddr;
  182. m_fDnsResolved = FALSE;
  183. m_fIpResolved = FALSE;
  184. m_dwErrorResolving = 0;
  185. return TRUE;
  186. }
  187. VOID UnbindAddr()
  188. {
  189. m_pAddr = NULL;
  190. m_fDnsResolved = FALSE;
  191. }
  192. XAR* GetStorage() { return &m_Storage; }
  193. AC_RESULT CheckAccess(
  194. LPBOOL pfSync,
  195. ADDRCHECKFUNC pFunc,
  196. ADDRCHECKARG pArg
  197. );
  198. //
  199. void AdjustRefs( LPBYTE, DWORD dwCut, DWORD dwAdj );
  200. //
  201. UINT GetAddrSize( DWORD );
  202. VOID MakeAcd( PADDRCMPDESC pacd, LPBYTE pMask, UINT cLen );
  203. // for UI, addr
  204. BOOL AddAddr( BOOL fGrant, DWORD dwFamily, LPBYTE pMask, LPBYTE pAddr );
  205. BOOL DeleteAddr( BOOL fGrant, DWORD iIndex );
  206. BOOL GetAddr( BOOL fGrant, DWORD iIndex, LPDWORD pdwFamily, LPBYTE* pMask, LPBYTE* pAddr );
  207. DWORD GetNbAddr( BOOL fGrant );
  208. BOOL LocateAddr( BOOL fGrant, DWORD iIndex, PADDRESS_HEADER* ppHd, PADDRESS_LIST_ENTRY* pHeader, LPDWORD iIndexInHeader );
  209. BOOL DeleteAllAddr( BOOL fGrant );
  210. BOOL SetFlag( DWORD dwFlag, BOOL fEnable );
  211. DWORD GetFlags();
  212. // test all mask for this family, do bsearch on each
  213. BOOL IsMatchAddr( BOOL fGrant, DWORD dwFamily, LPBYTE pAddr );
  214. AC_RESULT CheckAddress(
  215. struct sockaddr* pAddr
  216. );
  217. BOOL QueryDnsName(
  218. LPBOOL pfSync,
  219. ADDRCHECKFUNCEX pFunc,
  220. ADDRCHECKARG pArg,
  221. LPSTR * ppName
  222. );
  223. AC_RESULT CheckIpAccess( LPBOOL pfNeedDns);
  224. AC_RESULT CheckDnsAccess()
  225. { return CheckName( m_pszDnsName ); }
  226. BOOL IsDnsResolved()
  227. { return m_fDnsResolved; }
  228. LPSTR QueryResolvedDnsName()
  229. { return m_pszDnsName; }
  230. DWORD QueryErrorResolving()
  231. { return m_dwErrorResolving; }
  232. // for UI, name
  233. BOOL AddName( BOOL fGrant, LPSTR pName, DWORD dwFlags = 0 );
  234. BOOL AddReversedName( BOOL fGrant, LPSTR pName );
  235. BOOL DeleteName( BOOL fGrant, DWORD iIndex );
  236. BOOL GetName( BOOL fGrant, DWORD iIndex, LPSTR* ppName, LPDWORD pdwFlags = NULL );
  237. BOOL GetReversedName( BOOL fGrant, DWORD iIndex, LPSTR pName, LPDWORD pdwSize );
  238. DWORD GetNbName( BOOL fGrant );
  239. BOOL LocateName( BOOL fGrant, DWORD iIndex, PNAME_HEADER* ppHd, PNAME_LIST_ENTRY* pHeader, LPDWORD iIndexInHeader );
  240. BOOL DeleteAllName( BOOL fGrant );
  241. DWORD QueryCheckListSize() { return m_Storage.GetUsed(); }
  242. LPBYTE QueryCheckListPtr() { return m_Storage.GetAlloc(); }
  243. UINT GetNbComponent( LPSTR pName );
  244. // test all classes, do bsearch on each
  245. BOOL IsMatchName( BOOL fGrant, LPSTR pName );
  246. BOOL CheckReversedName( LPSTR pName ); // synchronous version
  247. AC_RESULT CheckName( LPSTR pName ); // synchronous version
  248. LPSTR InitReverse( LPSTR pR, LPSTR pTarget, LPBOOL pfAlloc );
  249. VOID TerminateReverse( LPSTR, BOOL );
  250. VOID AddrCheckDnsCallBack(
  251. BOOL fSt,
  252. LPSTR pDns
  253. );
  254. VOID AddrCheckDnsCallBack2(
  255. BOOL fSt,
  256. struct sockaddr* pAddr
  257. );
  258. VOID AddrCheckDnsCallBack3(
  259. BOOL fSt,
  260. struct sockaddr* pAddr
  261. );
  262. VOID ResolveDnsCallBack(
  263. BOOL fSt,
  264. LPSTR pDns
  265. );
  266. #if DBG
  267. VOID DumpAddrAndName( VOID );
  268. VOID DumpAddr( BOOL );
  269. VOID DumpName( BOOL );
  270. #endif
  271. private:
  272. XAR m_Storage;
  273. struct sockaddr * m_pAddr;
  274. struct sockaddr m_ResolvedAddr;
  275. LPSTR m_pszDnsName;
  276. BOOL m_fDnsResolved;
  277. BOOL m_fIpResolved;
  278. DWORD m_dwErrorResolving;
  279. ADDRCHECKFUNC m_HttpReqCallback;
  280. ADDRCHECKFUNCEX m_HttpReqCallbackEx;
  281. ADDRCHECKARG m_HttpReqParam;
  282. } ;
  283. typedef struct _SID_CACHE_ENTRY
  284. {
  285. DWORD tExpire; // now + TTL
  286. DWORD dwSidLen;
  287. BYTE Sid[0];
  288. } SID_CACHE_ENTRY, *PSID_CACHE_ENTRY;
  289. class CSidCache
  290. {
  291. public:
  292. CSidCache() {}
  293. ~CSidCache() {}
  294. //
  295. BOOL Init();
  296. VOID Terminate();
  297. //
  298. BOOL AddToCache( PSID, DWORD dwTTL ); // TTL in seconds
  299. BOOL IsInCache( PSID );
  300. BOOL CheckPresentAndResetTtl( PSID, DWORD );
  301. //
  302. BOOL Scavenger();
  303. private:
  304. XAR xaStore; // buffer of SID_CACHE_ENTRY
  305. CRITICAL_SECTION csLock;
  306. } ;
  307. //
  308. // PEN : Password Expiration Notification API
  309. //
  310. extern CSidCache g_scPen;
  311. BOOL PenAddToCache( PSID, DWORD );
  312. BOOL PenIsInCache( PSID );
  313. BOOL PenCheckPresentAndResetTtl( PSID, DWORD );
  314. #define PEN_TTL (10*60) // in seconds
  315. #if DBG
  316. extern VOID TestAPI();
  317. #endif
  318. #if defined(_RDNS_STANDALONE)
  319. typedef
  320. VOID
  321. (* PFN_SCHED_CALLBACK)(
  322. VOID * pContext
  323. );
  324. DWORD
  325. ScheduleWorkItem(
  326. PFN_SCHED_CALLBACK pfnCallback,
  327. PVOID pContext,
  328. DWORD msecTimeInterval,
  329. BOOL fPeriodic = FALSE
  330. )
  331. {
  332. return 0;
  333. }
  334. BOOL
  335. RemoveWorkItem(
  336. DWORD pdwCookie
  337. )
  338. {
  339. return FALSE;
  340. }
  341. #endif
  342. #endif
  343.