Source code of Windows XP (NT5)
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.

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