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.

353 lines
8.0 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1993 **/
  4. /**********************************************************************/
  5. /*
  6. globals.cxx
  7. This module contains global variable definitions shared by the
  8. various FTPD Service components.
  9. Functions exported by this module:
  10. InitializeGlobals
  11. TerminateGlobals
  12. ReadParamsFromRegistry
  13. WriteParamsToRegistry
  14. FILE HISTORY:
  15. KeithMo 07-Mar-1993 Created.
  16. MuraliK 11-April-1995 Created new global ftp server config object
  17. */
  18. #include "ftpdp.hxx"
  19. #include "acptctxt.hxx"
  20. #include <timer.h>
  21. //
  22. // Private constants.
  23. //
  24. #define DEFAULT_TRACE_FLAGS 0
  25. #define DEFAULT_NO_EXTENDED_FILENAME 0
  26. #define DEFAULT_MAX_AD_CACHE_TIME (60*10) // in seconds
  27. #define ACCEPT_CONTEXTS_PER_ENTRY 60
  28. #define DEFAULT_MAX_ACCEPT_ENTRIES (1*ACCEPT_CONTEXTS_PER_ENTRY)
  29. //
  30. // Socket transfer buffer size.
  31. //
  32. DWORD g_SocketBufferSize = 0;
  33. //
  34. // Statistics.
  35. //
  36. //
  37. // FTP Statistics structure.
  38. //
  39. LPFTP_SERVER_STATISTICS g_pFTPStats = NULL;
  40. //
  41. // Miscellaneous data.
  42. //
  43. //
  44. // The FTP Server sign-on string.
  45. //
  46. LPSTR g_FtpServiceNameString = NULL;
  47. //
  48. // key for the registry to read parameters
  49. //
  50. HKEY g_hkeyParams = NULL;
  51. //
  52. // List holding all the PASV accept context entries
  53. //
  54. LIST_ENTRY g_AcceptContextList;
  55. //
  56. // CS to guard manipulation of the accept context list
  57. //
  58. CRITICAL_SECTION g_AcceptContextCS;
  59. //
  60. // maximum number of passive accept context containers (60 events per container)
  61. //
  62. DWORD g_dwMaxAcceptContextEntries = 1;
  63. //
  64. // The number of threads currently blocked in Synchronous sockets
  65. // calls, like recv()
  66. //
  67. DWORD g_ThreadsBlockedInSyncCalls = 0;
  68. //
  69. // The maximum number of threads that will be allowed to block in
  70. // Synchronous sockets calls. Magic # that will be changed if the max # of
  71. // pool threads is less than 100.
  72. //
  73. DWORD g_MaxThreadsBlockedInSyncCalls = 100;
  74. #ifdef KEEP_COMMAND_STATS
  75. //
  76. // Lock protecting per-command statistics.
  77. //
  78. CRITICAL_SECTION g_CommandStatisticsLock;
  79. #endif // KEEP_COMMAND_STATS
  80. //
  81. // By default, extended characters are allowed for file/directory names
  82. // in the data transfer commands. Reg key can disable this.
  83. //
  84. DWORD g_fNoExtendedChars = FALSE;
  85. //
  86. // The maximum time in seconds to use a cached DS property before mandatory refresh
  87. //
  88. ULONGLONG g_MaxAdPropCacheTime = (ULONGLONG)DEFAULT_MAX_AD_CACHE_TIME*10000000;
  89. #if DBG
  90. //
  91. // Debug-specific data.
  92. //
  93. //
  94. // Debug output control flags.
  95. //
  96. #endif // DBG
  97. //
  98. // Public functions.
  99. //
  100. /*******************************************************************
  101. NAME: InitializeGlobals
  102. SYNOPSIS: Initializes global shared variables. Some values are
  103. initialized with constants, others are read from the
  104. configuration registry.
  105. RETURNS: APIERR - NO_ERROR if successful, otherwise a Win32
  106. error code.
  107. NOTES: This routine may only be called by a single thread
  108. of execution; it is not necessarily multi-thread safe.
  109. Also, this routine is called before the event logging
  110. routines have been initialized. Therefore, event
  111. logging is not available.
  112. HISTORY:
  113. KeithMo 07-Mar-1993 Created.
  114. MuraliK 05-April-1995 Added FTP server config object
  115. ********************************************************************/
  116. APIERR
  117. InitializeGlobals(
  118. VOID
  119. )
  120. {
  121. APIERR err = NO_ERROR;
  122. UINT_PTR dwMaxThreads = 0;
  123. #ifdef KEEP_COMMAND_STATS
  124. INITIALIZE_CRITICAL_SECTION( &g_CommandStatisticsLock );
  125. #endif // KEEP_COMMAND_STATS
  126. //
  127. // Setup the service sign-on string.
  128. //
  129. g_FtpServiceNameString = "Microsoft FTP Service\0"; // must be double \0 terminated
  130. //
  131. // Set up PASV accept event variables
  132. //
  133. InitializeListHead( &g_AcceptContextList );
  134. INITIALIZE_CRITICAL_SECTION( &g_AcceptContextCS );
  135. if ( err = CreateAcceptContext() )
  136. {
  137. return err;
  138. }
  139. //
  140. // configure the PassivePortRange object
  141. //
  142. if ( !FTP_PASV_PORT::Configure() )
  143. {
  144. return ERROR_DLL_INIT_FAILED;
  145. }
  146. //
  147. // Create an FTP server object and load values from registry.
  148. //
  149. //
  150. // Connect to the registry.
  151. //
  152. err = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  153. FTPD_PARAMETERS_KEY,
  154. 0,
  155. KEY_READ,
  156. &g_hkeyParams );
  157. if( err != NO_ERROR )
  158. {
  159. DBGPRINTF(( DBG_CONTEXT,
  160. "cannot open registry key %s, error %lu\n",
  161. FTPD_PARAMETERS_KEY,
  162. err ));
  163. //
  164. // Load Default values
  165. //
  166. err = NO_ERROR;
  167. }
  168. //
  169. // Figure out how many pool threads we can create maximally - if less than built-in
  170. // number, adjust accordingly
  171. //
  172. dwMaxThreads = AtqGetInfo( AtqMaxThreadLimit );
  173. if ( dwMaxThreads/2 < (UINT_PTR)g_MaxThreadsBlockedInSyncCalls )
  174. {
  175. g_MaxThreadsBlockedInSyncCalls = (DWORD)(dwMaxThreads/2);
  176. }
  177. DBGPRINTF((DBG_CONTEXT,
  178. "Max # of threads allowed to be blocked in sync calls : %d\n",
  179. g_MaxThreadsBlockedInSyncCalls));
  180. g_fNoExtendedChars = ReadRegistryDword( g_hkeyParams,
  181. FTPD_NO_EXTENDED_FILENAME,
  182. DEFAULT_NO_EXTENDED_FILENAME );
  183. g_MaxAdPropCacheTime = (ULONGLONG) ReadRegistryDword( g_hkeyParams,
  184. FTPD_DS_CACHE_REFRESH,
  185. DEFAULT_MAX_AD_CACHE_TIME ) * 10000000;
  186. DWORD dwVal = ReadRegistryDword( g_hkeyParams,
  187. FTPD_MAX_ACCEPT_EVENTS,
  188. DEFAULT_MAX_ACCEPT_ENTRIES ) ;
  189. // round it up to nearest multiple of events per entry, between 1 and 5.
  190. dwVal = (dwVal + ACCEPT_CONTEXTS_PER_ENTRY - 1) / ACCEPT_CONTEXTS_PER_ENTRY;
  191. if (dwVal < 1) {
  192. dwVal = 1;
  193. }
  194. if (dwVal > 5) {
  195. dwVal = 5;
  196. }
  197. g_dwMaxAcceptContextEntries = dwVal;
  198. g_pFTPStats = new FTP_SERVER_STATISTICS;
  199. if ( g_pFTPStats == NULL )
  200. {
  201. err = ERROR_NOT_ENOUGH_MEMORY;
  202. }
  203. FTP_PASV_PORT::Init();
  204. AD_IO::Initialize();
  205. return ( err);
  206. } // InitializeGlobals()
  207. /*******************************************************************
  208. NAME: TerminateGlobals
  209. SYNOPSIS: Terminate global shared variables.
  210. NOTES: This routine may only be called by a single thread
  211. of execution; it is not necessarily multi-thread safe.
  212. Also, this routine is called after the event logging
  213. routines have been terminated. Therefore, event
  214. logging is not available.
  215. HISTORY:
  216. KeithMo 07-Mar-1993 Created.
  217. ********************************************************************/
  218. VOID
  219. TerminateGlobals(
  220. VOID
  221. )
  222. {
  223. if ( g_hkeyParams != NULL) {
  224. RegCloseKey( g_hkeyParams);
  225. g_hkeyParams = NULL;
  226. }
  227. if ( g_pFTPStats )
  228. {
  229. delete g_pFTPStats;
  230. g_pFTPStats = NULL;
  231. }
  232. AD_IO::Terminate();
  233. FTP_PASV_PORT::Terminate();
  234. DeleteAcceptContexts();
  235. DeleteCriticalSection( &g_AcceptContextCS );
  236. #ifdef KEEP_COMMAND_STATS
  237. DeleteCriticalSection( &g_CommandStatisticsLock );
  238. #endif // KEEP_COMMAND_STATS
  239. } // TerminateGlobals
  240. /************************ End Of File ************************/