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.

455 lines
8.7 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name :
  4. dll_manager.h
  5. Abstract:
  6. IIS Plus ISAPI Handler. Dll management classes.
  7. Author:
  8. Taylor Weiss (TaylorW) 03-Feb-2000
  9. Wade A. Hilmo (WadeH) 08-Mar-2001
  10. Project:
  11. w3isapi.dll
  12. --*/
  13. #ifndef _DLL_MANAGER_H_
  14. #define _DLL_MANAGER_H_
  15. #define ISAPI_DLL_SIGNATURE (DWORD)'LDSI'
  16. #define ISAPI_DLL_SIGNATURE_FREE (DWORD)'fDSI'
  17. /************************************************************
  18. * Include Headers
  19. ************************************************************/
  20. #include <lkrhash.h>
  21. #include <reftrace.h>
  22. #include <acache.hxx>
  23. /************************************************************
  24. * Declarations
  25. ************************************************************/
  26. /*++
  27. class ISAPI_DLL
  28. Encapsulate an ISAPI dll.
  29. --*/
  30. class ISAPI_DLL
  31. {
  32. friend class ISAPI_DLL_MANAGER;
  33. public:
  34. //
  35. // ACACHE and ref tracing goo
  36. //
  37. VOID *
  38. operator new(
  39. size_t size
  40. )
  41. {
  42. //
  43. // Make warning level 4 happy and touch
  44. // the size argument in both free and checked
  45. // builds...
  46. //
  47. if ( size != sizeof( ISAPI_DLL ) )
  48. {
  49. DBG_ASSERT( size == sizeof( ISAPI_DLL ) );
  50. }
  51. DBG_ASSERT( sm_pachIsapiDlls != NULL );
  52. return sm_pachIsapiDlls->Alloc();
  53. }
  54. VOID
  55. operator delete(
  56. VOID * pIsapiDll
  57. )
  58. {
  59. DBG_ASSERT( pIsapiDll != NULL );
  60. DBG_ASSERT( sm_pachIsapiDlls != NULL );
  61. DBG_REQUIRE( sm_pachIsapiDlls->Free( pIsapiDll ) );
  62. }
  63. BOOL
  64. CheckSignature()
  65. {
  66. return ( m_Signature == ISAPI_DLL_SIGNATURE );
  67. }
  68. static
  69. HRESULT
  70. Initialize(
  71. VOID
  72. );
  73. static
  74. VOID
  75. Terminate(
  76. VOID
  77. );
  78. //
  79. // Construction and destruction
  80. //
  81. ISAPI_DLL()
  82. : m_nRefs(1),
  83. m_pfnGetExtensionVersion( NULL ),
  84. m_pfnTerminateExtension( NULL ),
  85. m_pfnHttpExtensionProc( NULL ),
  86. m_hModule( NULL ),
  87. m_pFastSid( NULL ),
  88. m_fIsLoaded( FALSE ),
  89. m_Signature( ISAPI_DLL_SIGNATURE )
  90. {
  91. IF_DEBUG( DLL_MANAGER )
  92. {
  93. DBGPRINTF((
  94. DBG_CONTEXT,
  95. "Created new ISAPI_DLL %p.\r\n",
  96. this
  97. ));
  98. }
  99. INITIALIZE_CRITICAL_SECTION( &m_csLock );
  100. }
  101. VOID
  102. ReferenceIsapiDll(
  103. VOID
  104. )
  105. {
  106. LONG nRefs;
  107. //
  108. // Don't go from 0 to 1 refs
  109. //
  110. DBG_ASSERT( m_nRefs != 0 );
  111. nRefs = InterlockedIncrement( &m_nRefs );
  112. if ( sm_pTraceLog != NULL )
  113. {
  114. WriteRefTraceLog( sm_pTraceLog,
  115. nRefs,
  116. this );
  117. }
  118. }
  119. VOID
  120. DereferenceIsapiDll(
  121. VOID
  122. )
  123. {
  124. LONG nRefs;
  125. nRefs = InterlockedDecrement( &m_nRefs );
  126. if ( sm_pTraceLog != NULL )
  127. {
  128. WriteRefTraceLog( sm_pTraceLog,
  129. nRefs,
  130. this );
  131. }
  132. if ( nRefs == 0 )
  133. {
  134. delete this;
  135. }
  136. }
  137. HRESULT
  138. SetName(
  139. const WCHAR *szModuleName,
  140. HANDLE hImpersonation
  141. );
  142. HRESULT
  143. Load(
  144. IN HANDLE hImpersonation,
  145. IN PSID pSid
  146. );
  147. HRESULT
  148. SetFastSid(
  149. IN PSID pSid
  150. );
  151. PSID
  152. QueryFastSid(
  153. VOID
  154. ) const
  155. {
  156. return m_pFastSid;
  157. }
  158. //
  159. // Accessors
  160. //
  161. const WCHAR *
  162. QueryModuleName( VOID ) const
  163. {
  164. return m_strModuleName.QueryStr();
  165. }
  166. PFN_GETEXTENSIONVERSION
  167. QueryGetExtensionVersion( VOID ) const
  168. {
  169. return m_pfnGetExtensionVersion;
  170. }
  171. PFN_TERMINATEEXTENSION
  172. QueryTerminateExtension( VOID ) const
  173. {
  174. return m_pfnTerminateExtension;
  175. }
  176. PFN_HTTPEXTENSIONPROC
  177. QueryHttpExtensionProc( VOID ) const
  178. {
  179. return m_pfnHttpExtensionProc;
  180. }
  181. BOOL
  182. IsMatch(
  183. IN const WCHAR * szModuleName
  184. )
  185. {
  186. return (_wcsicmp( szModuleName, m_strModuleName.QueryStr() ) == 0);
  187. }
  188. PSECURITY_DESCRIPTOR
  189. QuerySecDesc( VOID ) const
  190. {
  191. return (PSECURITY_DESCRIPTOR) m_buffSD.QueryPtr();
  192. }
  193. BOOL
  194. AccessCheck(
  195. IN HANDLE hImpersonation,
  196. IN PSID pSid
  197. );
  198. private:
  199. //
  200. // Avoid c++ errors
  201. //
  202. ISAPI_DLL( const ISAPI_DLL & ) {}
  203. ISAPI_DLL & operator = ( const ISAPI_DLL & ) { return *this; }
  204. ~ISAPI_DLL()
  205. {
  206. DBG_ASSERT( CheckSignature() );
  207. m_Signature = ISAPI_DLL_SIGNATURE_FREE;
  208. //
  209. // If this gets moved then we need to alter
  210. // cleanup paths during load.
  211. //
  212. Unload();
  213. DeleteCriticalSection( &m_csLock );
  214. IF_DEBUG( DLL_MANAGER )
  215. {
  216. DBGPRINTF((
  217. DBG_CONTEXT,
  218. "Deleted ISAPI_DLL %p.\r\n",
  219. this
  220. ));
  221. }
  222. }
  223. VOID
  224. Unload( VOID );
  225. DWORD m_Signature;
  226. LONG m_nRefs;
  227. STRU m_strModuleName;
  228. STRU m_strAntiCanonModuleName;
  229. CRITICAL_SECTION m_csLock;
  230. volatile BOOL m_fIsLoaded;
  231. static PTRACE_LOG sm_pTraceLog;
  232. static ALLOC_CACHE_HANDLER * sm_pachIsapiDlls;
  233. //
  234. // ISAPI Entry Points
  235. //
  236. PFN_GETEXTENSIONVERSION m_pfnGetExtensionVersion;
  237. PFN_TERMINATEEXTENSION m_pfnTerminateExtension;
  238. PFN_HTTPEXTENSIONPROC m_pfnHttpExtensionProc;
  239. //
  240. // Security members
  241. //
  242. HMODULE m_hModule;
  243. BUFFER m_buffSD;
  244. //
  245. // Fast check sid (the SID of the user which originally accessed DLL)
  246. //
  247. PSID m_pFastSid;
  248. BYTE m_abFastSid[ 64 ];
  249. HRESULT LoadAcl( STRU &strModuleName );
  250. VOID
  251. Lock( VOID )
  252. {
  253. EnterCriticalSection( &m_csLock );
  254. }
  255. VOID
  256. Unlock( VOID )
  257. {
  258. LeaveCriticalSection( &m_csLock );
  259. }
  260. };
  261. //
  262. // Hash Table for ISAPI extension lookup
  263. //
  264. class ISAPI_DLL_HASH
  265. : public CTypedHashTable<
  266. ISAPI_DLL_HASH,
  267. ISAPI_DLL,
  268. LPCWSTR
  269. >
  270. {
  271. public:
  272. ISAPI_DLL_HASH()
  273. : CTypedHashTable< ISAPI_DLL_HASH,
  274. ISAPI_DLL,
  275. LPCWSTR > ( "ISAPI_DLL_HASH" )
  276. {
  277. }
  278. static
  279. LPCWSTR
  280. ExtractKey(
  281. const ISAPI_DLL * pEntry
  282. )
  283. {
  284. return pEntry->QueryModuleName();
  285. }
  286. static
  287. DWORD
  288. CalcKeyHash(
  289. LPCWSTR pszKey
  290. )
  291. {
  292. int cchKey = (int)wcslen(pszKey);
  293. return HashStringNoCase(pszKey, cchKey);
  294. }
  295. static
  296. bool
  297. EqualKeys(
  298. LPCWSTR pszKey1,
  299. LPCWSTR pszKey2
  300. )
  301. {
  302. return _wcsicmp( pszKey1, pszKey2 ) == 0;
  303. }
  304. static
  305. void
  306. AddRefRecord(
  307. ISAPI_DLL * pEntry,
  308. int nIncr
  309. )
  310. {
  311. if ( nIncr == +1 )
  312. {
  313. pEntry->ReferenceIsapiDll();
  314. }
  315. else if ( nIncr == - 1)
  316. {
  317. pEntry->DereferenceIsapiDll();
  318. }
  319. }
  320. private:
  321. //
  322. // Avoid c++ errors
  323. //
  324. ISAPI_DLL_HASH( const ISAPI_DLL_HASH & )
  325. : CTypedHashTable< ISAPI_DLL_HASH,
  326. ISAPI_DLL,
  327. LPCWSTR > ( "ISAPI_DLL_HASH" )
  328. {
  329. }
  330. ISAPI_DLL_HASH & operator = ( const ISAPI_DLL_HASH & ) { return *this; }
  331. };
  332. class ISAPI_DLL_MANAGER
  333. {
  334. public:
  335. ISAPI_DLL_MANAGER( BOOL fInproc )
  336. {
  337. m_fInproc = fInproc;
  338. }
  339. ~ISAPI_DLL_MANAGER()
  340. {
  341. }
  342. HRESULT
  343. GetIsapi(
  344. IN const WCHAR * szModuleName,
  345. OUT ISAPI_DLL ** ppIsapiDll,
  346. IN HANDLE hImpersonation,
  347. IN PSID pSid
  348. );
  349. BOOL
  350. IsInproc()
  351. {
  352. return m_fInproc;
  353. }
  354. private:
  355. //
  356. // Avoid c++ errors
  357. //
  358. ISAPI_DLL_MANAGER( const ISAPI_DLL_MANAGER & ) {}
  359. ISAPI_DLL_MANAGER & operator = ( const ISAPI_DLL_MANAGER & ) { return *this; }
  360. ISAPI_DLL_HASH m_IsapiHash;
  361. BOOL m_fInproc;
  362. };
  363. extern ISAPI_DLL_MANAGER * g_pDllManager;
  364. #endif // _DLL_MANAGER_H_