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.

219 lines
5.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows NT Security
  4. // Copyright (C) Microsoft Corporation, 1997 - 1999
  5. //
  6. // File: cryptnet.cpp
  7. //
  8. // Contents: DllMain for CRYPTNET.DLL
  9. //
  10. // History: 24-Jul-97 kirtd Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "windows.h"
  14. #include "crtem.h"
  15. #include "unicode.h"
  16. //
  17. // DllMain stuff
  18. //
  19. extern BOOL WINAPI RPORDllMain (HMODULE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  20. extern BOOL WINAPI DpsDllMain (HMODULE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  21. extern BOOL WINAPI DemandLoadDllMain (HMODULE hInstDLL, DWORD fdwReason, LPVOID lpvReserved);
  22. typedef BOOL (WINAPI *PFN_DLL_MAIN_FUNC) (
  23. HMODULE hInstDLL,
  24. DWORD fdwReason,
  25. LPVOID lpvReserved
  26. );
  27. HMODULE g_hModule;
  28. // The following is set for a successful DLL_PROCESS_DETACH.
  29. static BOOL g_fEnableProcessDetach = FALSE;
  30. // For process/thread attach, called in the following order. For process/thread
  31. // detach, called in reverse order.
  32. static const PFN_DLL_MAIN_FUNC rgpfnDllMain[] = {
  33. DemandLoadDllMain,
  34. RPORDllMain
  35. };
  36. #define DLL_MAIN_FUNC_COUNT (sizeof(rgpfnDllMain) / sizeof(rgpfnDllMain[0]))
  37. //
  38. // DllRegisterServer and DllUnregisterServer stuff
  39. //
  40. extern HRESULT WINAPI DpsDllRegUnregServer (HMODULE hInstDLL, BOOL fRegUnreg);
  41. extern HRESULT WINAPI RPORDllRegUnregServer (HMODULE hInstDLL, BOOL fRegUnreg);
  42. typedef HRESULT (WINAPI *PFN_DLL_REGUNREGSERVER_FUNC) (
  43. HMODULE hInstDLL,
  44. BOOL fRegUnreg
  45. );
  46. static const PFN_DLL_REGUNREGSERVER_FUNC rgpfnDllRegUnregServer[] = {
  47. RPORDllRegUnregServer
  48. };
  49. #define DLL_REGUNREGSERVER_FUNC_COUNT (sizeof(rgpfnDllRegUnregServer) / \
  50. sizeof(rgpfnDllRegUnregServer[0]))
  51. #define ENV_LEN 32
  52. #if DBG
  53. #include <crtdbg.h>
  54. #ifndef _CRTDBG_LEAK_CHECK_DF
  55. #define _CRTDBG_LEAK_CHECK_DF 0x20
  56. #endif
  57. #define DEBUG_MASK_LEAK_CHECK _CRTDBG_LEAK_CHECK_DF /* 0x20 */
  58. static int WINAPI DbgGetDebugFlags()
  59. {
  60. int iDebugFlags = 0;
  61. char rgch[ENV_LEN + 1];
  62. DWORD cch;
  63. cch = GetEnvironmentVariableA(
  64. "DEBUG_MASK",
  65. rgch,
  66. ENV_LEN
  67. );
  68. if (cch && cch <= ENV_LEN) {
  69. rgch[cch] = '\0';
  70. iDebugFlags = atoi(rgch);
  71. }
  72. return iDebugFlags;
  73. }
  74. #endif
  75. //+-------------------------------------------------------------------------
  76. // Return TRUE if DLL_PROCESS_DETACH is called for FreeLibrary instead
  77. // of ProcessExit. The third parameter, lpvReserved, passed to DllMain
  78. // is NULL for FreeLibrary and non-NULL for ProcessExit.
  79. //
  80. // Also for debugging purposes, check the following environment variables:
  81. // CRYPT_DEBUG_FORCE_FREE_LIBRARY != 0 (retail and checked)
  82. // DEBUG_MASK & 0x20 (only checked)
  83. //
  84. // If either of the above environment variables is present and satisfies
  85. // the expression, TRUE is returned.
  86. //--------------------------------------------------------------------------
  87. BOOL
  88. WINAPI
  89. I_CryptnetIsProcessDetachFreeLibrary(
  90. LPVOID lpvReserved // Third parameter passed to DllMain
  91. )
  92. {
  93. char rgch[ENV_LEN + 1];
  94. DWORD cch;
  95. if (NULL == lpvReserved)
  96. return TRUE;
  97. cch = GetEnvironmentVariableA(
  98. "CRYPT_DEBUG_FORCE_FREE_LIBRARY",
  99. rgch,
  100. ENV_LEN
  101. );
  102. if (cch && cch <= ENV_LEN) {
  103. long lValue;
  104. rgch[cch] = '\0';
  105. lValue = atol(rgch);
  106. if (lValue)
  107. return TRUE;
  108. }
  109. #if DBG
  110. if (DbgGetDebugFlags() & DEBUG_MASK_LEAK_CHECK)
  111. return TRUE;
  112. #endif
  113. return FALSE;
  114. }
  115. //+-------------------------------------------------------------------------
  116. // Dll initialization
  117. //--------------------------------------------------------------------------
  118. BOOL WINAPI DllMain(
  119. HMODULE hInstDLL,
  120. DWORD fdwReason,
  121. LPVOID lpvReserved
  122. )
  123. {
  124. BOOL fReturn = TRUE;
  125. int i;
  126. switch (fdwReason) {
  127. case DLL_PROCESS_DETACH:
  128. if (!g_fEnableProcessDetach)
  129. return TRUE;
  130. else
  131. g_fEnableProcessDetach = FALSE;
  132. //
  133. // This is to prevent unloading the dlls at process exit
  134. //
  135. if (!I_CryptnetIsProcessDetachFreeLibrary(lpvReserved))
  136. {
  137. return TRUE;
  138. }
  139. case DLL_THREAD_DETACH:
  140. for (i = DLL_MAIN_FUNC_COUNT - 1; i >= 0; i--)
  141. fReturn &= rgpfnDllMain[i](hInstDLL, fdwReason, lpvReserved);
  142. break;
  143. case DLL_PROCESS_ATTACH:
  144. g_hModule = hInstDLL;
  145. case DLL_THREAD_ATTACH:
  146. default:
  147. for (i = 0; i < DLL_MAIN_FUNC_COUNT; i++)
  148. fReturn &= rgpfnDllMain[i](hInstDLL, fdwReason, lpvReserved);
  149. if ((DLL_PROCESS_ATTACH == fdwReason) && fReturn)
  150. g_fEnableProcessDetach = TRUE;
  151. break;
  152. }
  153. return(fReturn);
  154. }
  155. STDAPI DllRegisterServer ()
  156. {
  157. HRESULT hr = 0;
  158. ULONG cCount;
  159. for ( cCount = 0; cCount < DLL_REGUNREGSERVER_FUNC_COUNT; cCount++ )
  160. {
  161. hr = rgpfnDllRegUnregServer[cCount]( g_hModule, TRUE );
  162. if ( hr != S_OK )
  163. {
  164. break;
  165. }
  166. }
  167. return( hr );
  168. }
  169. STDAPI DllUnregisterServer ()
  170. {
  171. HRESULT hr = 0;
  172. ULONG cCount;
  173. for ( cCount = 0; cCount < DLL_REGUNREGSERVER_FUNC_COUNT; cCount++ )
  174. {
  175. hr = rgpfnDllRegUnregServer[cCount]( g_hModule, FALSE );
  176. if ( hr != S_OK )
  177. {
  178. break;
  179. }
  180. }
  181. return( hr );
  182. }