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.

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