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.

303 lines
7.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1996 - 1999
  6. //
  7. // File: dllmain.cpp
  8. //
  9. // Contents: Microsoft Internet Security Trust Provider
  10. //
  11. // Functions: DllMain
  12. // DllRegisterServer
  13. // DllUnregisterServer
  14. //
  15. // History: 28-May-1997 pberkman created
  16. //
  17. //--------------------------------------------------------------------------
  18. #include "global.hxx"
  19. #include "ossfunc.h"
  20. HANDLE hMeDLL = NULL;
  21. //
  22. // provider lists
  23. //
  24. LIST_LOCK sProvLock;
  25. //
  26. // store lists
  27. //
  28. LIST_LOCK sStoreLock;
  29. HANDLE hStoreEvent;
  30. CCatalogCache g_CatalogCache;
  31. // The following is set for a successful DLL_PROCESS_DETACH.
  32. static BOOL g_fEnableProcessDetach = FALSE;
  33. //////////////////////////////////////////////////////////////////////////////////////
  34. //
  35. // standard DLL exports ...
  36. //
  37. //
  38. extern BOOL WINAPI WintrustDllMain (HANDLE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  39. extern BOOL WINAPI SoftpubDllMain (HANDLE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  40. extern BOOL WINAPI mssip32DllMain (HANDLE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  41. extern BOOL WINAPI mscat32DllMain (HANDLE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  42. typedef BOOL (WINAPI *PFN_DLL_MAIN_FUNC) (
  43. HANDLE hInstDLL,
  44. DWORD fdwReason,
  45. LPVOID lpvReserved
  46. );
  47. // For process/thread attach, called in the following order. For process/thread
  48. // detach, called in reverse order.
  49. const PFN_DLL_MAIN_FUNC rgpfnDllMain[] = {
  50. WintrustDllMain,
  51. SoftpubDllMain,
  52. mssip32DllMain,
  53. mscat32DllMain,
  54. };
  55. #define DLL_MAIN_FUNC_COUNT (sizeof(rgpfnDllMain) / sizeof(rgpfnDllMain[0]))
  56. STDAPI WintrustDllRegisterServer(void);
  57. STDAPI WintrustDllUnregisterServer(void);
  58. STDAPI SoftpubDllRegisterServer(void);
  59. STDAPI SoftpubDllUnregisterServer(void);
  60. STDAPI mssip32DllRegisterServer(void);
  61. STDAPI mssip32DllUnregisterServer(void);
  62. STDAPI mscat32DllRegisterServer(void);
  63. STDAPI mscat32DllUnregisterServer(void);
  64. typedef HRESULT (STDAPICALLTYPE *PFN_DLL_REGISTER_SERVER) (void);
  65. const PFN_DLL_REGISTER_SERVER rgpfnDllRegisterServer[] = {
  66. WintrustDllRegisterServer,
  67. SoftpubDllRegisterServer,
  68. mssip32DllRegisterServer,
  69. mscat32DllRegisterServer,
  70. };
  71. #define DLL_REGISTER_SERVER_COUNT \
  72. (sizeof(rgpfnDllRegisterServer) / sizeof(rgpfnDllRegisterServer[0]))
  73. typedef HRESULT (STDAPICALLTYPE *PFN_DLL_UNREGISTER_SERVER) (void);
  74. const PFN_DLL_UNREGISTER_SERVER rgpfnDllUnregisterServer[] = {
  75. WintrustDllUnregisterServer,
  76. SoftpubDllUnregisterServer,
  77. mssip32DllUnregisterServer,
  78. mscat32DllUnregisterServer,
  79. };
  80. #define DLL_UNREGISTER_SERVER_COUNT \
  81. (sizeof(rgpfnDllUnregisterServer) / sizeof(rgpfnDllUnregisterServer[0]))
  82. #if DBG
  83. #include <crtdbg.h>
  84. #ifndef _CRTDBG_LEAK_CHECK_DF
  85. #define _CRTDBG_LEAK_CHECK_DF 0x20
  86. #endif
  87. #define DEBUG_MASK_LEAK_CHECK _CRTDBG_LEAK_CHECK_DF /* 0x20 */
  88. static int WINAPI DbgGetDebugFlags()
  89. {
  90. char *pszEnvVar;
  91. char *p;
  92. int iDebugFlags = 0;
  93. if (pszEnvVar = getenv("DEBUG_MASK"))
  94. iDebugFlags = strtol(pszEnvVar, &p, 16);
  95. return iDebugFlags;
  96. }
  97. #endif
  98. WINAPI
  99. I_IsProcessDetachFreeLibrary(
  100. LPVOID lpvReserved // Third parameter passed to DllMain
  101. )
  102. {
  103. if (NULL == lpvReserved)
  104. return TRUE;
  105. #if DBG
  106. if (DbgGetDebugFlags() & DEBUG_MASK_LEAK_CHECK)
  107. return TRUE;
  108. #endif
  109. return FALSE;
  110. }
  111. BOOL WINAPI DllMain(
  112. HANDLE hInstDLL,
  113. DWORD fdwReason,
  114. LPVOID lpvReserved
  115. )
  116. {
  117. BOOL fReturn = TRUE;
  118. int i,j;
  119. switch (fdwReason) {
  120. case DLL_PROCESS_DETACH:
  121. if (!g_fEnableProcessDetach)
  122. return TRUE;
  123. else
  124. g_fEnableProcessDetach = FALSE;
  125. //
  126. // This is to prevent unloading the dlls at process exit
  127. //
  128. if (!I_IsProcessDetachFreeLibrary(lpvReserved))
  129. {
  130. return TRUE;
  131. }
  132. // fall through if not process exit and unload the dlls
  133. case DLL_THREAD_DETACH:
  134. for (i = DLL_MAIN_FUNC_COUNT - 1; i >= 0; i--)
  135. fReturn &= rgpfnDllMain[i](hInstDLL, fdwReason, lpvReserved);
  136. break;
  137. case DLL_PROCESS_ATTACH:
  138. case DLL_THREAD_ATTACH:
  139. default:
  140. for (i = 0; i < DLL_MAIN_FUNC_COUNT; i++)
  141. {
  142. if (!rgpfnDllMain[i](hInstDLL, fdwReason, lpvReserved))
  143. {
  144. //
  145. // force the dllmain's which already succeeded to clean up
  146. //
  147. for (j = i-1; j >= 0; j--)
  148. {
  149. rgpfnDllMain[j](hInstDLL, DLL_PROCESS_DETACH, lpvReserved);
  150. }
  151. fReturn = FALSE;
  152. break;
  153. }
  154. }
  155. if ((DLL_PROCESS_ATTACH == fdwReason) && fReturn)
  156. g_fEnableProcessDetach = TRUE;
  157. break;
  158. }
  159. return(fReturn);
  160. }
  161. STDAPI DllRegisterServer(void)
  162. {
  163. HRESULT hr = S_OK;
  164. int i;
  165. for (i = 0; i < DLL_REGISTER_SERVER_COUNT; i++) {
  166. HRESULT hr2;
  167. hr2 = rgpfnDllRegisterServer[i]();
  168. if (S_OK == hr)
  169. hr = hr2;
  170. }
  171. return hr;
  172. }
  173. STDAPI DllUnregisterServer(void)
  174. {
  175. HRESULT hr = S_OK;
  176. int i;
  177. for (i = 0; i < DLL_UNREGISTER_SERVER_COUNT; i++) {
  178. HRESULT hr2;
  179. hr2 = rgpfnDllUnregisterServer[i]();
  180. if (S_OK == hr)
  181. hr = hr2;
  182. }
  183. return hr;
  184. }
  185. BOOL WINAPI WintrustDllMain(HANDLE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
  186. {
  187. switch (fdwReason)
  188. {
  189. case DLL_PROCESS_ATTACH:
  190. //
  191. // assign me so that further calls to WVT that would load me will just
  192. // use this handle.... otherwise, we would deadlock on detatch!
  193. //
  194. hMeDLL = hInstDLL;
  195. DisableThreadLibraryCalls((HINSTANCE)hInstDLL);
  196. //
  197. // Initialize critical section to protect lists.
  198. //
  199. if (!(InitializeListLock(&sProvLock, DBG_SS_TRUST)))
  200. {
  201. return(FALSE);
  202. }
  203. if (!(InitializeListLock(&sStoreLock, DBG_SS_TRUST)))
  204. {
  205. LockFree(&sProvLock);
  206. return(FALSE);
  207. }
  208. if (!(InitializeListEvent(&hStoreEvent)))
  209. {
  210. LockFree(&sProvLock);
  211. LockFree(&sStoreLock);
  212. return(FALSE);
  213. }
  214. if ( g_CatalogCache.Initialize() == FALSE )
  215. {
  216. LockFree(&sProvLock);
  217. LockFree(&sStoreLock);
  218. EventFree(hStoreEvent);
  219. return( FALSE );
  220. }
  221. //
  222. // we want to open the stores the first time accessed.
  223. //
  224. SetListEvent(hStoreEvent);
  225. break;
  226. case DLL_PROCESS_DETACH:
  227. g_CatalogCache.Uninitialize();
  228. WintrustUnloadProviderList();
  229. StoreProviderUnload();
  230. LockFree(&sProvLock);
  231. LockFree(&sStoreLock);
  232. EventFree(hStoreEvent);
  233. break;
  234. }
  235. return(ASNDllMain((HINSTANCE)hInstDLL, fdwReason, lpvReserved));
  236. }
  237. STDAPI WintrustDllRegisterServer(void)
  238. {
  239. //
  240. // register our ASN routines
  241. //
  242. return(ASNRegisterServer(W_MY_NAME));
  243. }
  244. STDAPI WintrustDllUnregisterServer(void)
  245. {
  246. return(ASNUnregisterServer());
  247. }