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.

2517 lines
62 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. ftpconf.cxx
  5. Abstract:
  6. This module contains functions for FTP Server configuration
  7. class (FTP_SERVER_CONFIG).
  8. Author:
  9. Murali R. Krishnan (MuraliK) 21-March-1995
  10. Project:
  11. FTP Server DLL
  12. Functions Exported:
  13. FTP_SERVER_CONFIG::FTP_SERVER_CONFIG()
  14. FTP_SERVER_CONFIG::~FTP_SERVER_CONFIG()
  15. FTP_SERVER_CONFIG::InitFromRegistry()
  16. FTP_SERVER_CONFIG::GetConfigInformation()
  17. FTP_SERVER_CONFIG::SetConfigInformation()
  18. FTP_SERVER_CONFIG::AllocNewConnection()
  19. FTP_SERVER_CONFIG::RemoveConnection()
  20. FTP_SERVER_CONFIG::DisconnectAllConnections()
  21. FTP_SERVER_CONFIG::Print()
  22. Revisions:
  23. MuraliK 26-July-1995 Added Allocation caching of client conns.
  24. --*/
  25. # include "ftpdp.hxx"
  26. #include <ole2.h>
  27. #include <imd.h>
  28. #include <iiscnfgp.h>
  29. #include <mb.hxx>
  30. #include <mbstring.h>
  31. # include <tchar.h>
  32. #include <timer.h>
  33. extern "C"
  34. {
  35. #include "ntlsa.h"
  36. } // extern "C"
  37. /************************************************************
  38. * Symbolic Constants
  39. ************************************************************/
  40. #define DEFAULT_ALLOW_ANONYMOUS TRUE
  41. #define DEFAULT_ALLOW_GUEST_ACCESS TRUE
  42. #define DEFAULT_ANONYMOUS_ONLY FALSE
  43. #define DEFAULT_READ_ACCESS_MASK 0
  44. #define DEFAULT_WRITE_ACCESS_MASK 0
  45. #define DEFAULT_MSDOS_DIR_OUTPUT TRUE
  46. #define DEFAULT_USE_SUBAUTH TRUE
  47. #define DEFAULT_LOGON_METHOD LOGON32_LOGON_INTERACTIVE
  48. #define DEFAULT_ANONYMOUS_PWD ""
  49. const TCHAR DEFAULT_EXIT_MESSAGE[] = TEXT("Goodbye.");
  50. # define CCH_DEFAULT_EXIT_MESSAGE (lstrlen( DEFAULT_EXIT_MESSAGE) + 1)
  51. const TCHAR DEFAULT_MAX_CLIENTS_MSG[] =
  52. TEXT("Maximum clients reached, service unavailable.");
  53. # define CCH_DEFAULT_MAX_CLIENTS_MSG (lstrlen( DEFAULT_MAX_CLIENTS_MSG) + 1)
  54. // this should be a double null terminated null terminated sequence.
  55. const TCHAR DEFAULT_GREETING_MESSAGE[2] = { '\0', '\0' };
  56. # define CCH_DEFAULT_GREETING_MESSAGE ( 2)
  57. // this should be a double null terminated null terminated sequence.
  58. const TCHAR DEFAULT_BANNER_MESSAGE[2] = { '\0', '\0' };
  59. # define CCH_DEFAULT_BANNER_MESSAGE ( 2)
  60. #define DEFAULT_ANNOTATE_DIRS FALSE
  61. #define DEFAULT_LOWERCASE_FILES FALSE
  62. #define DEFAULT_LISTEN_BACKLOG 1 /* reduce listen backlog */
  63. #define DEFAULT_ENABLE_LICENSING FALSE
  64. #define DEFAULT_DEFAULT_LOGON_DOMAIN NULL // NULL == use primary domain
  65. #define DEFAULT_ENABLE_PORT_ATTACK FALSE
  66. #define DEFAULT_ENABLE_PASV_THEFT FALSE
  67. #define DEFAULT_ALLOW_REPLACE_ON_RENAME FALSE
  68. #define DEFAULT_SHOW_4_DIGIT_YEAR FALSE
  69. # define SC_NOTIFY_INTERVAL 3000 // milliseconds
  70. # define CLEANUP_POLL_INTERVAL 2000 // milliseconds
  71. # define CLEANUP_RETRY_COUNT 12 // iterations
  72. //
  73. // Private Prototypes
  74. //
  75. APIERR
  76. GetDefaultDomainName(
  77. CHAR * pszDomainName,
  78. DWORD cchDomainName
  79. );
  80. BOOL
  81. FtpdReadRegString(
  82. IN HKEY hkey,
  83. OUT TCHAR * * ppchstr,
  84. IN LPCTSTR pchValue,
  85. IN LPCTSTR pchDefault,
  86. IN DWORD cchDefault
  87. );
  88. BOOL
  89. GenMessageWithLineFeed(IN LPSTR pszzMessage,
  90. IN LPSTR * ppszMessageWithLineFeed);
  91. #if DBG
  92. static CHAR * p_AccessTypes[] = { "read",
  93. "write",
  94. "create",
  95. "delete" };
  96. #endif // DBG
  97. /************************************************************
  98. * Member Functions of FTP_SERVER_INSTANCE
  99. ************************************************************/
  100. FTP_SERVER_INSTANCE::FTP_SERVER_INSTANCE(
  101. IN PFTP_IIS_SERVICE pService,
  102. IN DWORD dwInstanceId,
  103. IN USHORT sPort,
  104. IN LPCSTR lpszRegParamKey,
  105. IN LPWSTR lpwszAnonPasswordSecretName,
  106. IN LPWSTR lpwszRootPasswordSecretName,
  107. IN BOOL fMigrateVroots
  108. )
  109. /*++
  110. Description:
  111. Constructor Function for Ftp server Configuration object
  112. ( Initializes all members to be NULL)
  113. The valid flag may be initialized to TRUE only after reading values
  114. from registry.
  115. --*/
  116. : IIS_SERVER_INSTANCE(
  117. pService,
  118. dwInstanceId,
  119. sPort,
  120. lpszRegParamKey,
  121. lpwszAnonPasswordSecretName,
  122. lpwszRootPasswordSecretName,
  123. fMigrateVroots
  124. ),
  125. m_cCurrentConnections ( 0),
  126. m_cMaxCurrentConnections ( 0),
  127. m_fValid ( FALSE),
  128. m_fAllowAnonymous ( TRUE),
  129. m_fAnonymousOnly ( FALSE),
  130. m_fAllowGuestAccess ( TRUE),
  131. m_fAnnotateDirectories ( FALSE),
  132. m_fLowercaseFiles ( FALSE),
  133. m_fMsdosDirOutput ( FALSE),
  134. m_fFourDigitYear ( FALSE),
  135. m_fEnableLicensing ( FALSE),
  136. m_fEnablePortAttack ( FALSE),
  137. m_fEnablePasvTheft ( FALSE),
  138. m_pszGreetingMessageWithLineFeed( NULL),
  139. m_pszBannerMessageWithLineFeed( NULL),
  140. m_pszLocalHostName (NULL),
  141. m_dwUserFlags ( 0),
  142. m_ListenBacklog ( DEFAULT_LISTEN_BACKLOG),
  143. m_pFTPStats (NULL)
  144. {
  145. InitializeListHead( &m_ActiveConnectionsList);
  146. INITIALIZE_CRITICAL_SECTION( &m_csLock);
  147. InitializeListHead( &m_FreeConnectionsList);
  148. INITIALIZE_CRITICAL_SECTION( &m_csConnectionsList);
  149. if( QueryServerState() == MD_SERVER_STATE_INVALID ) {
  150. return;
  151. }
  152. m_pFTPStats = new FTP_SERVER_STATISTICS;
  153. if ( m_pFTPStats == NULL )
  154. {
  155. SetServerState( MD_SERVER_STATE_INVALID, ERROR_NOT_ENOUGH_MEMORY );
  156. SetLastError( ERROR_NOT_ENOUGH_MEMORY );
  157. }
  158. return;
  159. } // FTP_SERVER_INSTANCE::FTP_SERVER_INSTANCE()
  160. FTP_SERVER_INSTANCE::~FTP_SERVER_INSTANCE( VOID)
  161. /*++
  162. Description:
  163. Destructor function for server config object.
  164. ( Frees all dynamically allocated storage space)
  165. --*/
  166. {
  167. HRESULT hRes;
  168. //
  169. // delete statistics object
  170. //
  171. if( m_pFTPStats != NULL )
  172. {
  173. delete m_pFTPStats;
  174. m_pFTPStats = NULL;
  175. }
  176. //
  177. // The strings are automatically freed by a call to destructor
  178. //
  179. if ( m_pszLocalHostName != NULL) {
  180. delete [] ( m_pszLocalHostName);
  181. }
  182. if ( m_pszGreetingMessageWithLineFeed != NULL) {
  183. TCP_FREE( m_pszGreetingMessageWithLineFeed);
  184. m_pszGreetingMessageWithLineFeed = NULL;
  185. }
  186. if ( m_pszBannerMessageWithLineFeed != NULL) {
  187. TCP_FREE( m_pszBannerMessageWithLineFeed);
  188. m_pszBannerMessageWithLineFeed = NULL;
  189. }
  190. m_rfAccessCheck.Reset( (IMDCOM*)m_Service->QueryMDObject() );
  191. DBG_ASSERT( m_cCurrentConnections == 0);
  192. DBG_ASSERT( IsListEmpty( &m_ActiveConnectionsList));
  193. LockConnectionsList();
  194. DBG_REQUIRE(FreeAllocCachedClientConn());
  195. UnlockConnectionsList();
  196. DBG_ASSERT( IsListEmpty( &m_FreeConnectionsList));
  197. //
  198. // Delete the critical section object
  199. //
  200. DeleteCriticalSection( &m_csLock);
  201. DeleteCriticalSection( &m_csConnectionsList);
  202. } /* FTP_SERVER_INSTANCE::~FTP_SERVER_INSTANCE() */
  203. DWORD
  204. FTP_SERVER_INSTANCE::StartInstance()
  205. {
  206. IF_DEBUG(INSTANCE) {
  207. DBGPRINTF((
  208. DBG_CONTEXT,
  209. "FTP_SERVER_INSTANCE::StartInstance called for %p. Current state %d\n",
  210. this,
  211. QueryServerState()
  212. ));
  213. }
  214. DBG_ASSERT(m_pFTPStats);
  215. m_pFTPStats->UpdateStopTime();
  216. DWORD dwError = IIS_SERVER_INSTANCE::StartInstance();
  217. if ( dwError)
  218. {
  219. IF_DEBUG(INSTANCE) {
  220. DBGPRINTF((
  221. DBG_CONTEXT,
  222. "FTO_SERVER_INSTANCE - IIS_SERVER_INSTANCE Failed. StartInstance returned 0x%x",
  223. dwError
  224. ));
  225. }
  226. return dwError;
  227. }
  228. dwError = InitFromRegistry( FC_FTP_ALL & ~FC_FTP_LOG_IN_UTF_8 & ~FC_FTP_USER_ISOLATION);
  229. if( dwError == NO_ERROR ) {
  230. dwError = ReadAuthentInfo();
  231. }
  232. if (dwError == NO_ERROR)
  233. {
  234. m_pFTPStats->UpdateStartTime();
  235. }
  236. return dwError;
  237. }
  238. DWORD
  239. FTP_SERVER_INSTANCE::StopInstance()
  240. {
  241. DBG_ASSERT(m_pFTPStats);
  242. m_pFTPStats->UpdateStopTime();
  243. return IIS_SERVER_INSTANCE::StopInstance();
  244. }
  245. DWORD
  246. FTP_SERVER_INSTANCE::SetLocalHostName(IN LPCSTR pszHost)
  247. /*++
  248. This function copies the host name specified in the given string to
  249. configuration object.
  250. Arguments:
  251. pszHost pointer to string containing the local host name.
  252. Returns:
  253. NO_ERROR on success and ERROR_NOT_ENOUGH_MEMORY when no memory.
  254. ERROR_ALREADY_ASSIGNED if value is already present.
  255. --*/
  256. {
  257. //
  258. // if already a host name exists, return error.
  259. // otherwise allocate memory and copy the local host name.
  260. //
  261. if ( m_pszLocalHostName != NULL) {
  262. return (ERROR_ALREADY_ASSIGNED);
  263. } else {
  264. m_pszLocalHostName = new CHAR[lstrlenA(pszHost) + 1];
  265. if ( m_pszLocalHostName == NULL) {
  266. return (ERROR_NOT_ENOUGH_MEMORY);
  267. }
  268. lstrcpyA( m_pszLocalHostName, pszHost);
  269. }
  270. return (NO_ERROR);
  271. } // FTP_SERVER_INSTANCE::SetLocalHostName()
  272. DWORD
  273. FTP_SERVER_INSTANCE::InitFromRegistry(
  274. IN FIELD_CONTROL FieldsToRead)
  275. /*++
  276. Description:
  277. Initializes server configuration data from registry.
  278. Some values are also initialized with constants.
  279. If invalid registry key or load data from registry fails,
  280. then use default values.
  281. Arguments:
  282. hkeyReg handle to registry key
  283. FieldsToRead
  284. bitmask indicating the fields to read from the registry.
  285. This is useful when we try to read the new values after
  286. modifying the registry information as a result of
  287. SetAdminInfo call from the Admin UI
  288. Returns:
  289. NO_ERROR if there are no errors.
  290. Win32 error codes otherwise
  291. Limitations:
  292. No validity check is performed on the data present in registry.
  293. --*/
  294. {
  295. BOOL fSuccess = TRUE;
  296. DWORD err = NO_ERROR;
  297. IMDCOM* pMBCom;
  298. METADATA_HANDLE hMB;
  299. HRESULT hRes;
  300. METADATA_RECORD mdRecord;
  301. DWORD dwRequiredLen;
  302. BOOL fMustRel;
  303. HKEY hkeyReg = NULL;
  304. MB mb( (IMDCOM*)g_pInetSvc->QueryMDObject() );
  305. DWORD tmp;
  306. err = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  307. QueryRegParamKey(),
  308. 0,
  309. KEY_ALL_ACCESS,
  310. &hkeyReg );
  311. if ( hkeyReg == INVALID_HANDLE_VALUE ||
  312. hkeyReg == NULL) {
  313. //
  314. // Invalid Registry handle given
  315. //
  316. SetLastError( ERROR_INVALID_PARAMETER);
  317. return ( FALSE);
  318. }
  319. LockConfig();
  320. //
  321. // Read metabase data.
  322. //
  323. if( !mb.Open( QueryMDPath() ) ) {
  324. RegCloseKey( hkeyReg );
  325. UnLockConfig();
  326. return FALSE;
  327. }
  328. if( IsFieldSet( FieldsToRead, FC_FTP_EXIT_MESSAGE ) ) {
  329. if( !mb.GetStr( "",
  330. MD_EXIT_MESSAGE,
  331. IIS_MD_UT_SERVER,
  332. &m_ExitMessage,
  333. METADATA_INHERIT,
  334. DEFAULT_EXIT_MESSAGE ) ) {
  335. fSuccess = FALSE;
  336. err = GetLastError();
  337. }
  338. }
  339. if( fSuccess && IsFieldSet( FieldsToRead, FC_FTP_GREETING_MESSAGE ) ) {
  340. if( !mb.GetMultisz( "",
  341. MD_GREETING_MESSAGE,
  342. IIS_MD_UT_SERVER,
  343. &m_GreetingMessage ) ) {
  344. if( !m_GreetingMessage.Copy(
  345. DEFAULT_GREETING_MESSAGE,
  346. CCH_DEFAULT_GREETING_MESSAGE
  347. ) ) {
  348. fSuccess = FALSE;
  349. err = GetLastError();
  350. }
  351. }
  352. //
  353. // The m_pszGreetingMessage as read is a double null terminated
  354. // seq of strings (with one string per line)
  355. // A local copy of the string in the form suited for RPC Admin
  356. // should be generated.
  357. //
  358. if( fSuccess ) {
  359. fSuccess = GenMessageWithLineFeed( m_GreetingMessage.QueryStr(),
  360. &m_pszGreetingMessageWithLineFeed);
  361. if( !fSuccess ) {
  362. err = GetLastError();
  363. }
  364. }
  365. }
  366. if( fSuccess && IsFieldSet( FieldsToRead, FC_FTP_BANNER_MESSAGE ) ) {
  367. if( !mb.GetMultisz( "",
  368. MD_BANNER_MESSAGE,
  369. IIS_MD_UT_SERVER,
  370. &m_BannerMessage ) ) {
  371. if( !m_BannerMessage.Copy(
  372. DEFAULT_BANNER_MESSAGE,
  373. CCH_DEFAULT_BANNER_MESSAGE
  374. ) ) {
  375. fSuccess = FALSE;
  376. err = GetLastError();
  377. }
  378. }
  379. //
  380. // The m_pszBannerMessage as read is a double null terminated
  381. // seq of strings (with one string per line)
  382. // A local copy of the string in the form suited for RPC Admin
  383. // should be generated.
  384. //
  385. if( fSuccess ) {
  386. fSuccess = GenMessageWithLineFeed( m_BannerMessage.QueryStr(),
  387. &m_pszBannerMessageWithLineFeed);
  388. if( !fSuccess ) {
  389. err = GetLastError();
  390. }
  391. }
  392. }
  393. if( fSuccess && IsFieldSet( FieldsToRead, FC_FTP_MAX_CLIENTS_MESSAGE ) ) {
  394. if( !mb.GetStr( "",
  395. MD_MAX_CLIENTS_MESSAGE,
  396. IIS_MD_UT_SERVER,
  397. &m_MaxClientsMessage,
  398. METADATA_INHERIT,
  399. DEFAULT_MAX_CLIENTS_MSG ) ) {
  400. fSuccess = FALSE;
  401. err = GetLastError();
  402. }
  403. }
  404. if( IsFieldSet( FieldsToRead, FC_FTP_MSDOS_DIR_OUTPUT ) ) {
  405. if( !mb.GetDword( "",
  406. MD_MSDOS_DIR_OUTPUT,
  407. IIS_MD_UT_SERVER,
  408. &tmp ) ) {
  409. tmp = DEFAULT_MSDOS_DIR_OUTPUT;
  410. }
  411. m_fMsdosDirOutput = !!tmp;
  412. // clear and then set the MSDOS_DIR_OUTPUT in user flags
  413. m_dwUserFlags &= ~UF_MSDOS_DIR_OUTPUT;
  414. m_dwUserFlags |= (m_fMsdosDirOutput) ? UF_MSDOS_DIR_OUTPUT : 0;
  415. }
  416. if( IsFieldSet( FieldsToRead, FC_FTP_SHOW_4_DIGIT_YEAR) ) {
  417. if( !mb.GetDword( "",
  418. MD_SHOW_4_DIGIT_YEAR,
  419. IIS_MD_UT_SERVER,
  420. &tmp ) ) {
  421. tmp = DEFAULT_SHOW_4_DIGIT_YEAR;
  422. }
  423. m_fFourDigitYear = !!tmp;
  424. // clear and then set the 4_DIGIT_YEAR in user flags
  425. m_dwUserFlags &= ~UF_4_DIGIT_YEAR;
  426. m_dwUserFlags |= (m_fFourDigitYear) ? UF_4_DIGIT_YEAR : 0;
  427. }
  428. if( IsFieldSet( FieldsToRead, FC_FTP_ALLOW_ANONYMOUS ) ) {
  429. if( !mb.GetDword( "",
  430. MD_ALLOW_ANONYMOUS,
  431. IIS_MD_UT_SERVER,
  432. &tmp ) ) {
  433. tmp = DEFAULT_ALLOW_ANONYMOUS;
  434. }
  435. m_fAllowAnonymous = !!tmp;
  436. }
  437. if( IsFieldSet( FieldsToRead, FC_FTP_ANONYMOUS_ONLY ) ) {
  438. if( !mb.GetDword( "",
  439. MD_ANONYMOUS_ONLY,
  440. IIS_MD_UT_SERVER,
  441. &tmp ) ) {
  442. tmp = DEFAULT_ANONYMOUS_ONLY;
  443. }
  444. m_fAnonymousOnly = !!tmp;
  445. }
  446. if( IsFieldSet( FieldsToRead, FC_FTP_ALLOW_REPLACE_ON_RENAME ) ) {
  447. if( !mb.GetDword( "",
  448. MD_ALLOW_REPLACE_ON_RENAME,
  449. IIS_MD_UT_SERVER,
  450. &tmp ) ) {
  451. tmp = DEFAULT_ALLOW_REPLACE_ON_RENAME;
  452. }
  453. m_fAllowReplaceOnRename = !!tmp;
  454. }
  455. //
  456. // Read registry data.
  457. //
  458. if( IsFieldSet( FieldsToRead, FC_FTP_LISTEN_BACKLOG ) )
  459. {
  460. m_ListenBacklog = ReadRegistryDword( hkeyReg,
  461. FTPD_LISTEN_BACKLOG,
  462. DEFAULT_LISTEN_BACKLOG );
  463. }
  464. if( IsFieldSet( FieldsToRead, FC_FTP_ALLOW_GUEST_ACCESS ) ) {
  465. m_fAllowGuestAccess = !!ReadRegistryDword( hkeyReg,
  466. FTPD_ALLOW_GUEST_ACCESS,
  467. DEFAULT_ALLOW_GUEST_ACCESS );
  468. }
  469. if( IsFieldSet( FieldsToRead, FC_FTP_ANNOTATE_DIRECTORIES ) ) {
  470. m_fAnnotateDirectories = !!ReadRegistryDword( hkeyReg,
  471. FTPD_ANNOTATE_DIRS,
  472. DEFAULT_ANNOTATE_DIRS );
  473. // clear and then set the ANNOTATE_DIRS in user flags
  474. m_dwUserFlags &= ~UF_ANNOTATE_DIRS;
  475. m_dwUserFlags |= (m_fAnnotateDirectories) ? UF_ANNOTATE_DIRS : 0;
  476. }
  477. if( IsFieldSet( FieldsToRead, FC_FTP_LOWERCASE_FILES ) ) {
  478. m_fLowercaseFiles = !!ReadRegistryDword( hkeyReg,
  479. FTPD_LOWERCASE_FILES,
  480. DEFAULT_LOWERCASE_FILES );
  481. }
  482. // fEnablePortAttack is not controlled by RPC yet.
  483. m_fEnablePortAttack = !!ReadRegistryDword(hkeyReg,
  484. FTPD_ENABLE_PORT_ATTACK,
  485. DEFAULT_ENABLE_PORT_ATTACK);
  486. // fEnablePasvTheft is not controlled by RPC yet.
  487. m_fEnablePasvTheft = !!ReadRegistryDword(hkeyReg,
  488. FTPD_ENABLE_PASV_THEFT,
  489. DEFAULT_ENABLE_PASV_THEFT);
  490. if( fSuccess ) {
  491. //
  492. // The following field is not supported in the admin API.
  493. //
  494. m_fEnableLicensing = !!ReadRegistryDword( hkeyReg,
  495. FTPD_ENABLE_LICENSING,
  496. DEFAULT_ENABLE_LICENSING );
  497. }
  498. if ( fSuccess )
  499. {
  500. m_rfAccessCheck.Reset( (IMDCOM*)m_Service->QueryMDObject() );
  501. pMBCom = (IMDCOM*)m_Service->QueryMDObject();
  502. hRes = pMBCom->ComMDOpenMetaObject( METADATA_MASTER_ROOT_HANDLE,
  503. (BYTE *) QueryMDVRPath(),
  504. METADATA_PERMISSION_READ,
  505. 5000,
  506. &hMB );
  507. if ( SUCCEEDED( hRes ) )
  508. {
  509. mdRecord.dwMDIdentifier = MD_IP_SEC;
  510. mdRecord.dwMDAttributes = METADATA_INHERIT | METADATA_REFERENCE;
  511. mdRecord.dwMDUserType = IIS_MD_UT_FILE;
  512. mdRecord.dwMDDataType = BINARY_METADATA;
  513. mdRecord.dwMDDataLen = 0;
  514. mdRecord.pbMDData = (PBYTE)NULL;
  515. hRes = pMBCom->ComMDGetMetaData( hMB,
  516. (LPBYTE)"",
  517. &mdRecord,
  518. &dwRequiredLen );
  519. if ( SUCCEEDED( hRes ) && mdRecord.dwMDDataTag )
  520. {
  521. m_rfAccessCheck.Set( mdRecord.pbMDData,
  522. mdRecord.dwMDDataLen,
  523. mdRecord.dwMDDataTag );
  524. }
  525. DBG_REQUIRE( SUCCEEDED(pMBCom->ComMDCloseMetaObject( hMB )) );
  526. }
  527. else
  528. if( HRESULTTOWIN32( hRes ) != ERROR_PATH_NOT_FOUND )
  529. {
  530. fSuccess = FALSE;
  531. err = HRESULTTOWIN32( hRes );
  532. }
  533. }
  534. UnLockConfig();
  535. IF_DEBUG( CONFIG) {
  536. Print();
  537. }
  538. m_fValid = TRUE;
  539. RegCloseKey( hkeyReg );
  540. return ( err);
  541. } // FTP_SERVER_INSTANCE::InitFromRegistry()
  542. static BOOL
  543. RemoveInvalidsInPath( IN OUT TCHAR * pszPath)
  544. /*++
  545. Eliminate path components consisting of '.' and '..'
  546. to prevent security from being overridden
  547. Arguments:
  548. pszPath pointer to string containing path
  549. Returns:
  550. TRUE on success and
  551. FALSE if there is any failure
  552. --*/
  553. {
  554. int idest;
  555. TCHAR * pszScan;
  556. //
  557. // Check and eliminate the invalid path components
  558. //
  559. for( pszScan = pszPath; *pszScan != TEXT( '\0'); pszScan++) {
  560. if ( *pszScan == TEXT( '/')) {
  561. //
  562. // Check and kill invalid path components before next "\"
  563. //
  564. if ( *(pszScan + 1) == TEXT( '.')) {
  565. if ( *(pszScan +2) == TEXT('/')) {
  566. pszScan += 2; // skip the /./ pattern
  567. } else
  568. if ( *(pszScan +2) == TEXT('.') &&
  569. *(pszScan +3) == TEXT('/')) {
  570. //
  571. // We found the pattern /../, elimiate it
  572. //
  573. pszScan += 3;
  574. }
  575. *pszPath++ = *pszScan;
  576. continue;
  577. } // found a single /.
  578. }
  579. *pszPath++ = *pszScan;
  580. } // for
  581. *pszPath = TEXT( '\0');
  582. return ( TRUE);
  583. } // RemoveInvalidsInPath()
  584. VOID
  585. FTP_SERVER_INSTANCE::Print( VOID) const
  586. /*++
  587. Description:
  588. Prints the configuration information for this server.
  589. To be used in debugging mode for verification.
  590. Returns:
  591. None.
  592. --*/
  593. {
  594. DBGPRINTF(( DBG_CONTEXT,
  595. "FTP Server Configuration ( %08x).\n", this ));
  596. #if 0
  597. READ_LOCK_INST();
  598. DBGPRINTF(( DBG_CONTEXT,
  599. " AnonymousUser = %s\n",
  600. g_pInetSvc->QueryAnonUserName() ));
  601. UNLOCK_INST();
  602. DBGPRINTF(( DBG_CONTEXT,
  603. " %s = %d\n"
  604. " %s = %d\n"
  605. " %s = %u\n"
  606. " %s = %u\n"
  607. " %s = %u\n"
  608. " %s = %u\n"
  609. " %s = %u\n",
  610. FTPD_ALLOW_ANONYMOUS,
  611. m_fAllowAnonymous,
  612. FTPD_ALLOW_GUEST_ACCESS,
  613. m_fAllowGuestAccess,
  614. FTPD_ANONYMOUS_ONLY,
  615. m_fAnonymousOnly,
  616. FTPD_ENABLE_PORT_ATTACK,
  617. m_fEnablePortAttack,
  618. FTPD_ENABLE_PASV_THEFT,
  619. m_fEnablePasvTheft,
  620. "LogAnonymous",
  621. g_pInetSvc->QueryLogAnonymous(),
  622. "LogNonAnonymous",
  623. g_pInetSvc->QueryLogNonAnonymous()
  624. ));
  625. DBGPRINTF(( DBG_CONTEXT,
  626. " %s = %d\n",
  627. FTPD_ENABLE_LICENSING,
  628. m_fEnableLicensing ));
  629. DBGPRINTF(( DBG_CONTEXT,
  630. " MaxConnections = %lu\n",
  631. g_pInetSvc->QueryMaxConnections() ));
  632. DBGPRINTF(( DBG_CONTEXT,
  633. " ConnectionTimeout = %lu\n",
  634. g_pInetSvc->QueryConnectionTimeout() ));
  635. DBGPRINTF(( DBG_CONTEXT,
  636. " %s = %d\n",
  637. FTPD_MSDOS_DIR_OUTPUT,
  638. m_fMsdosDirOutput ));
  639. DBGPRINTF(( DBG_CONTEXT,
  640. " %s = %d\n",
  641. FTPD_4_DIGIT_YEAR,
  642. m_f4DigitYear ));
  643. DBGPRINTF(( DBG_CONTEXT,
  644. " %s = %d\n",
  645. FTPD_ANNOTATE_DIRS,
  646. m_fAnnotateDirectories ));
  647. DBGPRINTF(( DBG_CONTEXT,
  648. " %s = %08lX\n",
  649. FTPD_DEBUG_FLAGS,
  650. GET_DEBUG_FLAGS()));
  651. READ_LOCK_INST();
  652. DBGPRINTF(( DBG_CONTEXT,
  653. " LogFileDirectory = %s\n",
  654. g_pInetSvc->QueryLogFileDirectory() ));
  655. UNLOCK_INST();
  656. DBGPRINTF(( DBG_CONTEXT,
  657. " LogFileAccess = %lu\n",
  658. g_pInetSvc->QueryLogFileType() ));
  659. DBGPRINTF(( DBG_CONTEXT,
  660. " %s = %u\n",
  661. FTPD_LISTEN_BACKLOG,
  662. m_ListenBacklog ));
  663. DBGPRINTF(( DBG_CONTEXT,
  664. " DefaultLogonDomain = %s\n",
  665. m_DefaultLogonDomain ));
  666. #endif
  667. return;
  668. } // FTP_SERVER_INSTANCE::Print()
  669. PICLIENT_CONNECTION
  670. FTP_SERVER_INSTANCE::AllocNewConnection()
  671. /*++
  672. This function first checks that there is room for more connections
  673. as per the configured max connections.
  674. If there is no more connections allowed, it returns NULL
  675. with *pfMaxExceeded = TRUE
  676. Otherwise:
  677. This function creates a new CLIENT_CONNECTION (USER_DATA) object.
  678. The creation maybe fresh from heap or from cached free list.
  679. It increments the counter of currnet connections and returns
  680. the allocated object (if non NULL).
  681. We enter a critical section to avoid race condition
  682. among different threads. (this can be improved NYI).
  683. Returns:
  684. TRUE on success and
  685. FALSE if there is max Connections exceeded.
  686. --*/
  687. {
  688. PICLIENT_CONNECTION pConn = NULL;
  689. LockConnectionsList();
  690. //
  691. // We can add this new connection
  692. //
  693. pConn = AllocClientConnFromAllocCache();
  694. if ( pConn != NULL) {
  695. //
  696. // Increment the count of connected users
  697. //
  698. m_cCurrentConnections++;
  699. IF_DEBUG( CLIENT) {
  700. DBGPRINTF((DBG_CONTEXT, " CurrentConnections = %u\n",
  701. m_cCurrentConnections));
  702. }
  703. //
  704. // Update the current maximum connections
  705. //
  706. if ( m_cCurrentConnections > m_cMaxCurrentConnections) {
  707. m_cMaxCurrentConnections = m_cCurrentConnections;
  708. }
  709. //
  710. // Insert into the list of connected users.
  711. //
  712. InsertTailList( &m_ActiveConnectionsList, &pConn->QueryListEntry());
  713. }
  714. UnlockConnectionsList();
  715. return ( pConn);
  716. } // FTP_SERVER_INSTANCE::AllocNewConnection()
  717. VOID
  718. FTP_SERVER_INSTANCE::RemoveConnection(
  719. IN OUT PICLIENT_CONNECTION pcc
  720. )
  721. /*++
  722. --*/
  723. {
  724. LockConnectionsList();
  725. //
  726. // Remove from list of connections
  727. //
  728. RemoveEntryList( &pcc->QueryListEntry());
  729. //
  730. // Decrement count of current users
  731. //
  732. m_cCurrentConnections--;
  733. IF_DEBUG( CLIENT) {
  734. DBGPRINTF((DBG_CONTEXT, " CurrentConnections = %u\n",
  735. m_cCurrentConnections));
  736. }
  737. //
  738. // move the free connection to free list
  739. //
  740. FreeClientConnToAllocCache( pcc);
  741. UnlockConnectionsList();
  742. } // FTP_SERVER_INSTANCE::RemoveConnection()
  743. VOID
  744. FTP_SERVER_INSTANCE::DisconnectAllConnections( VOID)
  745. /*++
  746. Disconnects all user connections.
  747. Arguments:
  748. Instance - If NULL, then all users are disconnected. If !NULL, then
  749. only those users associated with the specified instance are
  750. disconnected.
  751. --*/
  752. {
  753. #ifdef CHECK_DBG
  754. CHAR rgchBuffer[90];
  755. #endif // CHECK_DBG
  756. DWORD dwLastTick = GetTickCount();
  757. DWORD dwCurrentTick;
  758. PLIST_ENTRY pEntry;
  759. PLIST_ENTRY pEntryNext;
  760. DBGPRINTF( ( DBG_CONTEXT,
  761. "Entering FTP_SERVER_INSTANCE::DisconnectAllConnections()\n"));
  762. //
  763. // Let's empty the connection list immediately while in the lock to avoid a
  764. // shutdown deadlock
  765. //
  766. LockConnectionsList();
  767. pEntry = m_ActiveConnectionsList.Flink;
  768. InitializeListHead( &m_ActiveConnectionsList);
  769. UnlockConnectionsList();
  770. //
  771. // close down all the active sockets.
  772. //
  773. for( pEntryNext = pEntry->Flink;
  774. pEntry != &m_ActiveConnectionsList;
  775. pEntry = pEntryNext) {
  776. PICLIENT_CONNECTION pConn =
  777. GET_USER_DATA_FROM_LIST_ENTRY( pEntry);
  778. pEntryNext = pEntry->Flink; // cache next entry since pConn may die
  779. ASSERT( pConn != NULL);
  780. # ifdef CHECK_DBG
  781. wsprintfA( rgchBuffer, "Kill UID=%u. Ref=%u\n",
  782. pConn->QueryId(), pConn->QueryReference());
  783. OutputDebugString( rgchBuffer);
  784. # endif // CHECK_DBG
  785. dwCurrentTick = GetTickCount();
  786. if ( (dwCurrentTick - dwLastTick) >= ( SC_NOTIFY_INTERVAL)) {
  787. //
  788. // We seem to take longer time for cleaning up than
  789. // expected. Let us ask service controller to wait for us.
  790. //
  791. g_pInetSvc->
  792. DelayCurrentServiceCtrlOperation(SC_NOTIFY_INTERVAL * 2);
  793. dwLastTick = dwCurrentTick;
  794. }
  795. pConn->Reference();
  796. pConn->DisconnectUserWithError( ERROR_SERVER_DISABLED, TRUE);
  797. DBG_REQUIRE( pConn->DeReference() > 0 ); // remove ref added above
  798. if( pConn->RemoveActiveReference() ) {
  799. //
  800. // This connection is due for deletion. Kill it.
  801. //
  802. // Remove from list of connections
  803. //
  804. pConn->Cleanup();
  805. RemoveEntryList( &pConn->QueryListEntry());
  806. //
  807. // Decrement count of current users
  808. //
  809. m_cCurrentConnections--;
  810. // move the connection to free list
  811. FreeClientConnToAllocCache( pConn);
  812. }
  813. } // for
  814. //
  815. // Wait for the users to die.
  816. // The connection objects should be automatically freed because the
  817. // socket has been closed. Subsequent requests will fail
  818. // and cause a blowaway of the connection objects.
  819. //
  820. //
  821. // Wait for the users to die.
  822. //
  823. for( int i = 0 ;
  824. ( i < CLEANUP_RETRY_COUNT ) && ( m_cCurrentConnections > 0);
  825. i++ )
  826. {
  827. DBGPRINTF(( DBG_CONTEXT, "Sleep Iteration %d; Time=%u millisecs."
  828. " CurrentConn=%d.\n",
  829. i, CLEANUP_POLL_INTERVAL, m_cCurrentConnections));
  830. g_pInetSvc->
  831. DelayCurrentServiceCtrlOperation( CLEANUP_POLL_INTERVAL * 2);
  832. Sleep( CLEANUP_POLL_INTERVAL );
  833. }
  834. return;
  835. } // FTP_SERVER_INSTANCE::DisconnectAllConnections()
  836. BOOL
  837. FTP_SERVER_INSTANCE::EnumerateConnection(
  838. IN PFN_CLIENT_CONNECTION_ENUM pfnConnEnum,
  839. IN LPVOID pContext,
  840. IN DWORD dwConnectionId)
  841. /*++
  842. This function iterates through all the connections in the current connected
  843. users list and enumerates each of them. If the connectionId matches then
  844. given callback function is called. If the ConnectionId is 0, then the
  845. callback is called for each and every connection active currently.
  846. During such a call the reference count of the connection is bumped up.
  847. Call this function after obtaining the ConnectionsList Lock.
  848. Arguments:
  849. pfnConnEnum pointer to function to be called when a match is found.
  850. pContext pointer to context information to be passed in
  851. for callback
  852. dwConnectionId DWORD containing the Connection Id. IF 0 match all the
  853. connections.
  854. Returns:
  855. FALSE if no match is found
  856. TRUE if atleast one match is found.
  857. --*/
  858. {
  859. BOOL fReturn = FALSE;
  860. BOOL fFoundOne = FALSE;
  861. PLIST_ENTRY pEntry;
  862. PLIST_ENTRY pEntryNext;
  863. DBG_ASSERT( pfnConnEnum != NULL);
  864. //
  865. // Loop through the list of connections and call the callback
  866. // for each connection that matches condition
  867. //
  868. for ( pEntry = m_ActiveConnectionsList.Flink,
  869. pEntryNext = &m_ActiveConnectionsList;
  870. pEntry != &m_ActiveConnectionsList;
  871. pEntry = pEntryNext
  872. ) {
  873. PICLIENT_CONNECTION pConn =
  874. GET_USER_DATA_FROM_LIST_ENTRY( pEntry);
  875. pEntryNext = pEntry->Flink; // cache next entry since pConn may die
  876. if ( dwConnectionId == 0 || dwConnectionId == pConn->QueryId()) {
  877. pConn->Reference();
  878. fReturn = ( pfnConnEnum)( pConn, pContext);
  879. if ( !pConn->DeReference()) {
  880. // Blowaway the connection and update the count of entries.
  881. //
  882. // Remove from list of connections
  883. //
  884. pConn->Cleanup();
  885. RemoveEntryList( &pConn->QueryListEntry());
  886. //
  887. // Decrement count of current users
  888. //
  889. m_cCurrentConnections--;
  890. IF_DEBUG( CLIENT) {
  891. DBGPRINTF((DBG_CONTEXT, " CurrentConnections = %u\n",
  892. m_cCurrentConnections));
  893. }
  894. }
  895. if (!fReturn) {
  896. break;
  897. }
  898. fFoundOne = TRUE;
  899. }
  900. } // for
  901. //
  902. // If we didn't find any, assume that there was no match.
  903. //
  904. if ( !fFoundOne ) {
  905. SetLastError( ERROR_NO_MORE_ITEMS );
  906. fReturn = FALSE;
  907. }
  908. return ( fReturn);
  909. } // FTP_SERVER_INSTANCE::EnumerateConnection()
  910. DWORD
  911. FTP_SERVER_INSTANCE::GetConfigInformation(OUT LPFTP_CONFIG_INFO pConfig)
  912. /*++
  913. This function copies the ftp server configuration into the given
  914. structure (pointed to).
  915. Arguments:
  916. pConfig -- pointer to FTP_CONFIG_INFO which on success will contain
  917. the ftp server configuration
  918. Returns:
  919. Win32 error code. NO_ERROR on success.
  920. --*/
  921. {
  922. DWORD dwError = NO_ERROR;
  923. memset( pConfig, 0, sizeof(*pConfig) );
  924. pConfig->FieldControl = FC_FTP_ALL;
  925. //
  926. // mask out the properties that are not supported in FTP 5.1
  927. //
  928. pConfig->FieldControl &= ~FC_FTP_LOG_IN_UTF_8 & ~FC_FTP_USER_ISOLATION;
  929. LockConfig();
  930. pConfig->fAllowAnonymous = m_fAllowAnonymous;
  931. pConfig->fAllowGuestAccess = m_fAllowGuestAccess;
  932. pConfig->fAnnotateDirectories = m_fAnnotateDirectories;
  933. pConfig->fAnonymousOnly = m_fAnonymousOnly;
  934. pConfig->dwListenBacklog = m_ListenBacklog;
  935. pConfig->fLowercaseFiles = m_fLowercaseFiles;
  936. pConfig->fMsdosDirOutput = m_fMsdosDirOutput;
  937. if( !ConvertStringToRpc( &pConfig->lpszExitMessage,
  938. QueryExitMsg() ) ||
  939. !ConvertStringToRpc( &pConfig->lpszGreetingMessage,
  940. m_pszGreetingMessageWithLineFeed ) ||
  941. !ConvertStringToRpc( &pConfig->lpszBannerMessage,
  942. m_pszBannerMessageWithLineFeed ) ||
  943. !ConvertStringToRpc( &pConfig->lpszMaxClientsMessage,
  944. QueryMaxClientsMsg() ) )
  945. {
  946. dwError = GetLastError();
  947. }
  948. UnLockConfig();
  949. if ( dwError == NO_ERROR) {
  950. pConfig->lpszHomeDirectory = NULL; // use query virtual roots.
  951. }
  952. if ( dwError != NO_ERROR) {
  953. FreeRpcString( pConfig->lpszExitMessage );
  954. FreeRpcString( pConfig->lpszGreetingMessage );
  955. FreeRpcString( pConfig->lpszBannerMessage );
  956. FreeRpcString( pConfig->lpszHomeDirectory );
  957. FreeRpcString( pConfig->lpszMaxClientsMessage );
  958. }
  959. return (dwError);
  960. } // FTP_SERVER_INSTANCE::GetConfigurationInformation()
  961. // Private Functions ...
  962. BOOL
  963. FTP_SERVER_INSTANCE::FreeAllocCachedClientConn( VOID)
  964. /*++
  965. This function frees all the alloc cached client connections
  966. It walks through the list of alloc cached entries and frees them.
  967. This function should be called when Server module is terminated and when
  968. no other thread can interfere in processing a shared object.
  969. Arguments:
  970. NONE
  971. Returns:
  972. TRUE on success and FALSE on failure.
  973. --*/
  974. {
  975. register PLIST_ENTRY pEntry;
  976. register PLIST_ENTRY pEntryNext;
  977. register PICLIENT_CONNECTION pConn;
  978. for( pEntry = m_FreeConnectionsList.Flink;
  979. pEntry != &m_FreeConnectionsList; ) {
  980. PICLIENT_CONNECTION pConn =
  981. GET_USER_DATA_FROM_LIST_ENTRY( pEntry);
  982. pEntryNext = pEntry->Flink; // cache next entry since pConn may die
  983. DBG_ASSERT( pConn->QueryReference() == 0);
  984. RemoveEntryList( pEntry ); // Remove this context from list
  985. // delete the object itself
  986. delete pConn;
  987. pEntry = pEntryNext;
  988. } // for
  989. return (TRUE);
  990. } // USER_DATA::FreeAllocCachedClientConn()
  991. PICLIENT_CONNECTION
  992. FTP_SERVER_INSTANCE::AllocClientConnFromAllocCache(
  993. VOID
  994. )
  995. /*++
  996. This function attempts to allocate a client connection object from
  997. the allocation cache, using the free list of connections available.
  998. If none is available, then a new object is allocated using new ()
  999. and returned to the caller.
  1000. Eventually the object will enter free list and will be available
  1001. for free use.
  1002. Arguments:
  1003. None
  1004. Returns:
  1005. On success a valid pointer to client connection object.
  1006. Issues:
  1007. This function should be called while holding the ConnectionsLock.
  1008. --*/
  1009. {
  1010. PLIST_ENTRY pEntry = m_FreeConnectionsList.Flink;
  1011. PICLIENT_CONNECTION pConn;
  1012. if ( pEntry != &m_FreeConnectionsList) {
  1013. pConn = GET_USER_DATA_FROM_LIST_ENTRY( pEntry);
  1014. DBG_ASSERT( pConn != NULL);
  1015. RemoveEntryList( pEntry); // remove entry from free list
  1016. DBG_ASSERT( pConn->QueryInstance() == NULL );
  1017. pConn->SetInstance( this );
  1018. } else {
  1019. //
  1020. // create a new object, since allocation cache is empty
  1021. //
  1022. pConn = new USER_DATA( this );
  1023. }
  1024. return (pConn);
  1025. } // FTP_SERVER_INSTANCE::AllocClientConnFromAllocCache()
  1026. VOID
  1027. FTP_SERVER_INSTANCE::FreeClientConnToAllocCache(
  1028. IN PICLIENT_CONNECTION pClient
  1029. )
  1030. /*++
  1031. This function releases the given Client connection to the allocation cache.
  1032. It adds the given object to allocation cache.
  1033. Arguments:
  1034. pClient pointer to client connection object which needs to be freed.
  1035. Returns:
  1036. None
  1037. Issues:
  1038. This function should be called after holding the ConnectionsList
  1039. critical section.
  1040. Should we limit the number of items that can be on free list and
  1041. to release the remaining to global pool? NYI (depends on # CPUs)
  1042. --*/
  1043. {
  1044. PLIST_ENTRY pEntry = &pClient->QueryListEntry();
  1045. //
  1046. // empty out instance pointer
  1047. //
  1048. pClient->QueryInstance()->DecrementCurrentConnections();
  1049. pClient->QueryInstance()->Dereference( );
  1050. pClient->SetInstance( NULL );
  1051. InsertHeadList( &m_FreeConnectionsList, pEntry);
  1052. return;
  1053. } // FTP_SERVER_INSTANCE::FreeClientConnToAllocCache()
  1054. /*******************************************************************
  1055. NAME: GetDefaultDomainName
  1056. SYNOPSIS: Fills in the given array with the name of the default
  1057. domain to use for logon validation.
  1058. ENTRY: pszDomainName - Pointer to a buffer that will receive
  1059. the default domain name.
  1060. cchDomainName - The size (in charactesr) of the domain
  1061. name buffer.
  1062. RETURNS: APIERR - 0 if successful, !0 if not.
  1063. HISTORY:
  1064. KeithMo 05-Dec-1994 Created.
  1065. ********************************************************************/
  1066. APIERR
  1067. GetDefaultDomainName(
  1068. STR * pstrDomainName
  1069. )
  1070. {
  1071. OBJECT_ATTRIBUTES ObjectAttributes;
  1072. NTSTATUS NtStatus;
  1073. INT Result;
  1074. APIERR err = 0;
  1075. LSA_HANDLE LsaPolicyHandle = NULL;
  1076. PPOLICY_ACCOUNT_DOMAIN_INFO DomainInfo = NULL;
  1077. //
  1078. // Open a handle to the local machine's LSA policy object.
  1079. //
  1080. InitializeObjectAttributes( &ObjectAttributes, // object attributes
  1081. NULL, // name
  1082. 0L, // attributes
  1083. NULL, // root directory
  1084. NULL ); // security descriptor
  1085. NtStatus = LsaOpenPolicy( NULL, // system name
  1086. &ObjectAttributes, // object attributes
  1087. POLICY_EXECUTE, // access mask
  1088. &LsaPolicyHandle ); // policy handle
  1089. if( !NT_SUCCESS( NtStatus ) )
  1090. {
  1091. DBGPRINTF(( DBG_CONTEXT,
  1092. "cannot open lsa policy, error %08lX\n",
  1093. NtStatus ));
  1094. err = LsaNtStatusToWinError( NtStatus );
  1095. goto Cleanup;
  1096. }
  1097. //
  1098. // Query the domain information from the policy object.
  1099. //
  1100. NtStatus = LsaQueryInformationPolicy( LsaPolicyHandle,
  1101. PolicyAccountDomainInformation,
  1102. (PVOID *)&DomainInfo );
  1103. if( !NT_SUCCESS( NtStatus ) )
  1104. {
  1105. DBGPRINTF(( DBG_CONTEXT,
  1106. "cannot query lsa policy info, error %08lX\n",
  1107. NtStatus ));
  1108. err = LsaNtStatusToWinError( NtStatus );
  1109. goto Cleanup;
  1110. }
  1111. //
  1112. // Compute the required length of the ANSI name.
  1113. //
  1114. Result = WideCharToMultiByte( CP_ACP,
  1115. 0, // dwFlags
  1116. (LPCWSTR)DomainInfo->DomainName.Buffer,
  1117. DomainInfo->DomainName.Length /sizeof(WCHAR),
  1118. NULL, // lpMultiByteStr
  1119. 0, // cchMultiByte
  1120. NULL, // lpDefaultChar
  1121. NULL // lpUsedDefaultChar
  1122. );
  1123. if( Result <= 0 )
  1124. {
  1125. err = GetLastError();
  1126. goto Cleanup;
  1127. }
  1128. //
  1129. // Resize the output string as appropriate, including room for the
  1130. // terminating '\0'.
  1131. //
  1132. if( !pstrDomainName->Resize( (UINT)Result + 1 ) )
  1133. {
  1134. err = GetLastError();
  1135. goto Cleanup;
  1136. }
  1137. //
  1138. // Convert the name from UNICODE to ANSI.
  1139. //
  1140. Result = WideCharToMultiByte( CP_ACP,
  1141. 0, // flags
  1142. (LPCWSTR)DomainInfo->DomainName.Buffer,
  1143. DomainInfo->DomainName.Length /sizeof(WCHAR),
  1144. pstrDomainName->QueryStr(),
  1145. pstrDomainName->QuerySize() - 1, // for '\0'
  1146. NULL,
  1147. NULL
  1148. );
  1149. if( Result <= 0 )
  1150. {
  1151. err = GetLastError();
  1152. DBGPRINTF(( DBG_CONTEXT,
  1153. "cannot convert domain name to ANSI, error %d\n",
  1154. err ));
  1155. goto Cleanup;
  1156. }
  1157. //
  1158. // Ensure the ANSI string is zero terminated.
  1159. //
  1160. DBG_ASSERT( (DWORD)Result < pstrDomainName->QuerySize() );
  1161. pstrDomainName->QueryStr()[Result] = '\0';
  1162. //
  1163. // Success!
  1164. //
  1165. DBG_ASSERT( err == 0 );
  1166. IF_DEBUG( CONFIG )
  1167. {
  1168. DBGPRINTF(( DBG_CONTEXT,
  1169. "GetDefaultDomainName: default domain = %s\n",
  1170. pstrDomainName->QueryStr() ));
  1171. }
  1172. Cleanup:
  1173. if( DomainInfo != NULL )
  1174. {
  1175. LsaFreeMemory( (PVOID)DomainInfo );
  1176. }
  1177. if( LsaPolicyHandle != NULL )
  1178. {
  1179. LsaClose( LsaPolicyHandle );
  1180. }
  1181. return err;
  1182. } // GetDefaultDomainName()
  1183. BOOL
  1184. GenMessageWithLineFeed(IN LPTSTR pszzMessage,
  1185. IN LPTSTR * ppszMessageWithLineFeed)
  1186. {
  1187. DWORD cchLen = 0;
  1188. DWORD cchLen2;
  1189. DWORD nLines = 0;
  1190. LPCTSTR pszNext = pszzMessage;
  1191. LPTSTR pszDst = NULL;
  1192. DBG_ASSERT( ppszMessageWithLineFeed != NULL);
  1193. //
  1194. // 1. Find the length of the the complete message
  1195. //
  1196. for ( cchLen = _tcslen( pszzMessage), nLines = 0;
  1197. *(pszzMessage + cchLen + 1) != TEXT('\0');
  1198. cchLen += 1+_tcslen( pszzMessage + cchLen + 1), nLines++
  1199. )
  1200. ;
  1201. //
  1202. // 2. Allocate sufficient space to hold the data
  1203. //
  1204. if ( *ppszMessageWithLineFeed != NULL) {
  1205. TCP_FREE( *ppszMessageWithLineFeed);
  1206. }
  1207. *ppszMessageWithLineFeed = (TCHAR *) TCP_ALLOC((cchLen + nLines + 3)
  1208. * sizeof(TCHAR));
  1209. if ( *ppszMessageWithLineFeed == NULL) {
  1210. SetLastError( ERROR_NOT_ENOUGH_MEMORY);
  1211. return (FALSE);
  1212. }
  1213. //
  1214. // 3.
  1215. // Copy the message from double null terminated string to the
  1216. // new string, taking care to replace the nulls with \n for linefeed
  1217. //
  1218. pszDst = * ppszMessageWithLineFeed;
  1219. _tcscpy( pszDst, pszzMessage);
  1220. cchLen2 = _tcslen( pszzMessage) + 1;
  1221. *(pszDst+cchLen2 - 1) = TEXT('\n'); // replacing the '\0' with '\n'
  1222. for( pszNext = pszzMessage + cchLen2;
  1223. *pszNext != '\0';
  1224. pszNext = pszzMessage + cchLen2
  1225. ) {
  1226. _tcscpy( pszDst + cchLen2, pszNext);
  1227. cchLen2 += _tcslen(pszNext) + 1;
  1228. *(pszDst + cchLen2 - 1) = TCHAR('\n'); // replacing the '\0' with '\n'
  1229. } // for
  1230. //
  1231. // Reset the last line feed.
  1232. //
  1233. *(pszDst + cchLen2 - 1) = TCHAR('\0');
  1234. // if following assertion is not true, we are writing into heap!!!
  1235. if ( cchLen + nLines + 3 <= cchLen2) {
  1236. return ( FALSE);
  1237. }
  1238. DBG_ASSERT( cchLen + nLines + 3 >= cchLen2);
  1239. return ( TRUE);
  1240. } // GenMessageWithLineFeed()
  1241. TCHAR *
  1242. FtpdReadRegistryString(IN HKEY hkey,
  1243. IN LPCTSTR pszValueName,
  1244. IN LPCTSTR pchDefaultValue,
  1245. IN DWORD cbDefaultValue)
  1246. /*++
  1247. This function reads a string (REG_SZ/REG_MULTI_SZ/REG_EXPAND_SZ) without
  1248. expanding the same. It allocates memory for reading the data from registry.
  1249. Arguments:
  1250. hkey handle for the registry key.
  1251. pszValueName pointer to string containing the name of value to be read.
  1252. pchDefaultValue pointer to default value to be used for reading the string
  1253. this may be double null terminated sequence of string for
  1254. REG_MULTI_SZ strings
  1255. cchDefaultValue count of characters in default value string,
  1256. including double null characters.
  1257. Return:
  1258. pointer to newly allocated string containing the data read from registry
  1259. or the default string.
  1260. --*/
  1261. {
  1262. TCHAR * pszBuffer1 = NULL;
  1263. DWORD err;
  1264. if( hkey == NULL ) {
  1265. //
  1266. // Pretend the key wasn't found.
  1267. //
  1268. err = ERROR_FILE_NOT_FOUND;
  1269. } else {
  1270. DWORD cbBuffer = 0;
  1271. DWORD dwType;
  1272. //
  1273. // Determine the buffer size.
  1274. //
  1275. err = RegQueryValueEx( hkey,
  1276. pszValueName,
  1277. NULL,
  1278. &dwType,
  1279. NULL,
  1280. &cbBuffer );
  1281. if( ( err == NO_ERROR ) || ( err == ERROR_MORE_DATA ) ) {
  1282. if(( dwType != REG_SZ ) &&
  1283. ( dwType != REG_MULTI_SZ ) &&
  1284. ( dwType != REG_EXPAND_SZ )
  1285. ) {
  1286. //
  1287. // Type mismatch, registry data NOT a string.
  1288. // Use default.
  1289. //
  1290. err = ERROR_FILE_NOT_FOUND;
  1291. } else {
  1292. //
  1293. // Item found, allocate a buffer.
  1294. //
  1295. pszBuffer1 = (TCHAR *) TCP_ALLOC( cbBuffer+sizeof(TCHAR) );
  1296. if( pszBuffer1 == NULL ) {
  1297. err = GetLastError();
  1298. } else {
  1299. //
  1300. // Now read the value into the buffer.
  1301. //
  1302. err = RegQueryValueEx( hkey,
  1303. pszValueName,
  1304. NULL,
  1305. NULL,
  1306. (LPBYTE)pszBuffer1,
  1307. &cbBuffer );
  1308. }
  1309. }
  1310. }
  1311. }
  1312. if( err == ERROR_FILE_NOT_FOUND ) {
  1313. //
  1314. // Item not found, use default value.
  1315. //
  1316. err = NO_ERROR;
  1317. if( pchDefaultValue != NULL ) {
  1318. if ( pszBuffer1 != NULL) {
  1319. TCP_FREE( pszBuffer1);
  1320. }
  1321. pszBuffer1 = (TCHAR *)TCP_ALLOC((cbDefaultValue) *
  1322. sizeof(TCHAR));
  1323. if( pszBuffer1 == NULL ) {
  1324. err = GetLastError();
  1325. } else {
  1326. memcpy(pszBuffer1, pchDefaultValue,
  1327. cbDefaultValue*sizeof(TCHAR) );
  1328. }
  1329. }
  1330. }
  1331. if( err != NO_ERROR ) {
  1332. //
  1333. // Something tragic happend; free any allocated buffers
  1334. // and return NULL to the caller, indicating failure.
  1335. //
  1336. if( pszBuffer1 != NULL ) {
  1337. TCP_FREE( pszBuffer1 );
  1338. pszBuffer1 = NULL;
  1339. }
  1340. SetLastError( err);
  1341. }
  1342. return pszBuffer1;
  1343. } // FtpdReadRegistryString()
  1344. BOOL
  1345. FtpdReadRegString(
  1346. IN HKEY hkey,
  1347. OUT TCHAR * * ppchstr,
  1348. IN LPCTSTR pchValue,
  1349. IN LPCTSTR pchDefault,
  1350. IN DWORD cchDefault
  1351. )
  1352. /*++
  1353. Description
  1354. Gets the specified string from the registry. If *ppchstr is not NULL,
  1355. then the value is freed. If the registry call fails, *ppchstr is
  1356. restored to its previous value.
  1357. Arguments:
  1358. hkey - Handle to open key
  1359. ppchstr - Receives pointer of allocated memory of the new value of the
  1360. string
  1361. pchValue - Which registry value to retrieve
  1362. pchDefault - Default string if value isn't found
  1363. cchDefault - count of characters in default value
  1364. Note:
  1365. --*/
  1366. {
  1367. CHAR * pch = *ppchstr;
  1368. *ppchstr = FtpdReadRegistryString(hkey,
  1369. pchValue,
  1370. pchDefault,
  1371. cchDefault);
  1372. if ( !*ppchstr )
  1373. {
  1374. *ppchstr = pch;
  1375. return FALSE;
  1376. }
  1377. if ( pch ) {
  1378. //
  1379. // use TCP_FREE since FtpdReadRegistryString() uses TCP_ALLOC
  1380. // to allocate the chunk of memory
  1381. //
  1382. TCP_FREE( pch );
  1383. }
  1384. return TRUE;
  1385. } // FtpdReadRegString()
  1386. BOOL
  1387. FTP_IIS_SERVICE::AddInstanceInfo(
  1388. IN DWORD dwInstance,
  1389. IN BOOL fMigrateRoots
  1390. )
  1391. {
  1392. PFTP_SERVER_INSTANCE pInstance;
  1393. CHAR szHost[MAXGETHOSTSTRUCT];
  1394. IF_DEBUG(SERVICE_CTRL) {
  1395. DBGPRINTF(( DBG_CONTEXT,
  1396. "AddInstanceInfo: instance %d reg %s\n", dwInstance, QueryRegParamKey() ));
  1397. }
  1398. // Guard against startup race where another thread might be adding
  1399. // instances before the MSFTPSVC thread is finished with initializing
  1400. // the g_pInetSvc pointer.
  1401. if ( g_pInetSvc == NULL )
  1402. {
  1403. SetLastError( ERROR_NOT_SUPPORTED );
  1404. return FALSE;
  1405. }
  1406. //
  1407. // Get the current host name.
  1408. //
  1409. if( gethostname( szHost, sizeof(szHost) ) < 0 ) {
  1410. return FALSE;
  1411. }
  1412. //
  1413. // Create the new instance
  1414. //
  1415. pInstance = new FTP_SERVER_INSTANCE(
  1416. this,
  1417. dwInstance,
  1418. IPPORT_FTP,
  1419. QueryRegParamKey(),
  1420. FTPD_ANONYMOUS_SECRET_W,
  1421. FTPD_ROOT_SECRET_W,
  1422. fMigrateRoots
  1423. );
  1424. if (pInstance == NULL) {
  1425. SetLastError( E_OUTOFMEMORY );
  1426. return FALSE;
  1427. }
  1428. if( !AddInstanceInfoHelper( pInstance ) ) {
  1429. return FALSE;
  1430. }
  1431. if( pInstance->SetLocalHostName( szHost ) != NO_ERROR ) {
  1432. RemoveServerInstance( pInstance );
  1433. return FALSE;
  1434. }
  1435. return TRUE;
  1436. } // FTP_IIS_SERVICE::AddInstanceInfo
  1437. DWORD
  1438. FTP_IIS_SERVICE::DisconnectUsersByInstance(
  1439. IN IIS_SERVER_INSTANCE * pInstance
  1440. )
  1441. /*++
  1442. Virtual callback invoked by IIS_SERVER_INSTANCE::StopInstance() to
  1443. disconnect all users associated with the given instance.
  1444. Arguments:
  1445. pInstance - All users associated with this instance will be
  1446. forcibly disconnected.
  1447. --*/
  1448. {
  1449. ((FTP_SERVER_INSTANCE *)pInstance)->DisconnectAllConnections();
  1450. return NO_ERROR;
  1451. } // FTP_IIS_SERVICE::DisconnectUsersByInstance
  1452. VOID
  1453. FTP_SERVER_INSTANCE::MDChangeNotify(
  1454. MD_CHANGE_OBJECT * pco
  1455. )
  1456. /*++
  1457. Handles metabase change notifications.
  1458. Arguments:
  1459. pco - Path and ID that changed.
  1460. --*/
  1461. {
  1462. FIELD_CONTROL control = 0;
  1463. DWORD i;
  1464. DWORD err;
  1465. DWORD id;
  1466. PCSTR pszURL;
  1467. DWORD dwURLLength;
  1468. //
  1469. // Let the parent deal with it.
  1470. //
  1471. IIS_SERVER_INSTANCE::MDChangeNotify( pco );
  1472. //
  1473. // Now flush the metacache and relevant file handle cache entries.
  1474. //
  1475. TsFlushMetaCache(METACACHE_FTP_SERVER_ID, FALSE);
  1476. if ( !_mbsnbicmp((PUCHAR)pco->pszMDPath, (PUCHAR)QueryMDVRPath(),
  1477. _mbslen( (PUCHAR)QueryMDVRPath() )) )
  1478. {
  1479. pszURL = (CHAR *)pco->pszMDPath + QueryMDVRPathLen() - 1;
  1480. //
  1481. // Figure out the length of the URL. Unless this is the root,
  1482. // we want to strip the trailing slash.
  1483. if (memcmp(pszURL, "/", sizeof("/")) != 0)
  1484. {
  1485. dwURLLength = strlen(pszURL) - 1;
  1486. }
  1487. else
  1488. {
  1489. dwURLLength = sizeof("/") - 1;
  1490. }
  1491. }
  1492. else
  1493. {
  1494. //
  1495. // Presumably this is for a change above the root URL level, i.e. a
  1496. // change of a property at the service level. Since this affects
  1497. // everything, flush starting at the root.
  1498. //
  1499. pszURL = "/";
  1500. dwURLLength = sizeof("/") - 1;
  1501. }
  1502. DBG_ASSERT(pszURL != NULL);
  1503. DBG_ASSERT(*pszURL != '\0');
  1504. TsFlushURL(GetTsvcCache(), pszURL, dwURLLength, RESERVED_DEMUX_URI_INFO);
  1505. //
  1506. // Interpret the changes.
  1507. //
  1508. for( i = 0 ; i < pco->dwMDNumDataIDs ; i++ ) {
  1509. id = pco->pdwMDDataIDs[i];
  1510. switch( id ) {
  1511. case MD_EXIT_MESSAGE :
  1512. control |= FC_FTP_EXIT_MESSAGE;
  1513. break;
  1514. case MD_GREETING_MESSAGE :
  1515. control |= FC_FTP_GREETING_MESSAGE;
  1516. break;
  1517. case MD_BANNER_MESSAGE :
  1518. control |= FC_FTP_BANNER_MESSAGE;
  1519. break;
  1520. case MD_MAX_CLIENTS_MESSAGE :
  1521. control |= FC_FTP_MAX_CLIENTS_MESSAGE;
  1522. break;
  1523. case MD_MSDOS_DIR_OUTPUT :
  1524. control |= FC_FTP_MSDOS_DIR_OUTPUT;
  1525. break;
  1526. case MD_ALLOW_ANONYMOUS :
  1527. control |= FC_FTP_ALLOW_ANONYMOUS;
  1528. break;
  1529. case MD_ANONYMOUS_ONLY :
  1530. control |= FC_FTP_ANONYMOUS_ONLY;
  1531. break;
  1532. case MD_ALLOW_REPLACE_ON_RENAME :
  1533. control |= FC_FTP_ALLOW_REPLACE_ON_RENAME;
  1534. break;
  1535. case MD_SHOW_4_DIGIT_YEAR :
  1536. control |= FC_FTP_SHOW_4_DIGIT_YEAR;
  1537. break;
  1538. case MD_ANONYMOUS_USER_NAME :
  1539. case MD_ANONYMOUS_PWD :
  1540. case MD_ANONYMOUS_USE_SUBAUTH :
  1541. case MD_DEFAULT_LOGON_DOMAIN :
  1542. case MD_LOGON_METHOD :
  1543. err = ReadAuthentInfo( FALSE, id );
  1544. if( err != NO_ERROR ) {
  1545. DBGPRINTF((
  1546. DBG_CONTEXT,
  1547. "FTP_SERVER_INSTANCE::MDChangeNotify() cannot read authentication info, error %d\n",
  1548. err
  1549. ));
  1550. }
  1551. break;
  1552. }
  1553. }
  1554. if( control != 0 ) {
  1555. err = InitFromRegistry( control );
  1556. if( err != NO_ERROR ) {
  1557. DBGPRINTF((
  1558. DBG_CONTEXT,
  1559. "FTP_SERVER_INSTANCE::MDChangeNotify() cannot read config, error %lx\n",
  1560. err
  1561. ));
  1562. }
  1563. }
  1564. } // FTP_SERVER_INSTANCE::MDChangeNotify
  1565. DWORD
  1566. FTP_SERVER_INSTANCE::ReadAuthentInfo(
  1567. IN BOOL ReadAll,
  1568. IN DWORD SingleItemToRead
  1569. )
  1570. /*++
  1571. Reads per-instance authentication info from the metabase.
  1572. Arguments:
  1573. ReadAll - If TRUE, then all authentication related items are
  1574. read from the metabase. If FALSE, then only a single item
  1575. is read.
  1576. SingleItemToRead - The single authentication item to read if
  1577. ReadAll is FALSE. Ignored if ReadAll is TRUE.
  1578. Returns:
  1579. DWORD - 0 if successful, !0 otherwise.
  1580. --*/
  1581. {
  1582. DWORD tmp;
  1583. DWORD err = NO_ERROR;
  1584. BOOL rebuildAcctDesc = FALSE;
  1585. MB mb( (IMDCOM*)g_pInetSvc->QueryMDObject() );
  1586. //
  1587. // Lock our configuration (since we'll presumably be making changes)
  1588. // and open the metabase.
  1589. //
  1590. LockConfig();
  1591. if( !mb.Open( QueryMDPath() ) ) {
  1592. err = GetLastError();
  1593. DBGPRINTF((
  1594. DBG_CONTEXT,
  1595. "ReadAuthentInfo: cannot open metabase, error %lx\n",
  1596. err
  1597. ));
  1598. }
  1599. //
  1600. // Read the anonymous username if necessary. Note this is a REQUIRED
  1601. // property. If it is missing from the metabase, bail.
  1602. //
  1603. if( err == NO_ERROR &&
  1604. ( ReadAll || SingleItemToRead == MD_ANONYMOUS_USER_NAME ) ) {
  1605. if( mb.GetStr(
  1606. "",
  1607. MD_ANONYMOUS_USER_NAME,
  1608. IIS_MD_UT_SERVER,
  1609. &m_TcpAuthentInfo.strAnonUserName
  1610. ) ) {
  1611. rebuildAcctDesc = TRUE;
  1612. } else {
  1613. err = GetLastError();
  1614. DBGPRINTF((
  1615. DBG_CONTEXT,
  1616. "ReadAuthentInfo: cannot read MD_ANONYMOUS_USER_NAME, error %lx\n",
  1617. err
  1618. ));
  1619. }
  1620. }
  1621. //
  1622. // Read the "use subauthenticator" flag if necessary. This is an
  1623. // optional property.
  1624. //
  1625. if( err == NO_ERROR &&
  1626. ( ReadAll || SingleItemToRead == MD_ANONYMOUS_USE_SUBAUTH ) ) {
  1627. if( !mb.GetDword(
  1628. "",
  1629. MD_ANONYMOUS_USE_SUBAUTH,
  1630. IIS_MD_UT_SERVER,
  1631. &tmp
  1632. ) ) {
  1633. tmp = DEFAULT_USE_SUBAUTH;
  1634. }
  1635. m_TcpAuthentInfo.fDontUseAnonSubAuth = !tmp;
  1636. }
  1637. //
  1638. // Read the anonymous password if necessary. This is an optional
  1639. // property.
  1640. //
  1641. if( err == NO_ERROR &&
  1642. ( ReadAll || SingleItemToRead == MD_ANONYMOUS_PWD ) ) {
  1643. if( mb.GetStr(
  1644. "",
  1645. MD_ANONYMOUS_PWD,
  1646. IIS_MD_UT_SERVER,
  1647. &m_TcpAuthentInfo.strAnonUserPassword,
  1648. METADATA_INHERIT,
  1649. DEFAULT_ANONYMOUS_PWD
  1650. ) ) {
  1651. rebuildAcctDesc = TRUE;
  1652. } else {
  1653. //
  1654. // Since we provided a default value to mb.GetStr(), it should
  1655. // only fail if something catastrophic occurred, such as an
  1656. // out of memory condition.
  1657. //
  1658. err = GetLastError();
  1659. DBGPRINTF((
  1660. DBG_CONTEXT,
  1661. "ReadAuthentInfo: cannot read MD_ANONYMOUS_PWD, error %lx\n",
  1662. err
  1663. ));
  1664. }
  1665. }
  1666. //
  1667. // Read the default logon domain if necessary. This is an optional
  1668. // property.
  1669. //
  1670. if( err == NO_ERROR &&
  1671. ( ReadAll || SingleItemToRead == MD_DEFAULT_LOGON_DOMAIN ) ) {
  1672. if( !mb.GetStr(
  1673. "",
  1674. MD_DEFAULT_LOGON_DOMAIN,
  1675. IIS_MD_UT_SERVER,
  1676. &m_TcpAuthentInfo.strDefaultLogonDomain
  1677. ) ) {
  1678. //
  1679. // Generate a default domain name.
  1680. //
  1681. err = GetDefaultDomainName( &m_TcpAuthentInfo.strDefaultLogonDomain );
  1682. if( err != NO_ERROR ) {
  1683. DBGPRINTF((
  1684. DBG_CONTEXT,
  1685. "ReadAuthentInfo: cannot get default domain name, error %d\n",
  1686. err
  1687. ));
  1688. }
  1689. }
  1690. }
  1691. //
  1692. // Read the logon method if necessary. This is an optional property.
  1693. //
  1694. if( err == NO_ERROR &&
  1695. ( ReadAll || SingleItemToRead == MD_LOGON_METHOD ) ) {
  1696. if( !mb.GetDword(
  1697. "",
  1698. MD_LOGON_METHOD,
  1699. IIS_MD_UT_SERVER,
  1700. &tmp
  1701. ) ) {
  1702. tmp = DEFAULT_LOGON_METHOD;
  1703. }
  1704. m_TcpAuthentInfo.dwLogonMethod = tmp;
  1705. }
  1706. //
  1707. // If anything changed that could affect the anonymous account
  1708. // descriptor, then rebuild the descriptor.
  1709. //
  1710. if( err == NO_ERROR && rebuildAcctDesc ) {
  1711. if( !BuildAnonymousAcctDesc( &m_TcpAuthentInfo ) ) {
  1712. DBGPRINTF((
  1713. DBG_CONTEXT,
  1714. "ReadAuthentInfo: BuildAnonymousAcctDesc() failed\n"
  1715. ));
  1716. err = ERROR_NOT_ENOUGH_MEMORY; // SWAG
  1717. }
  1718. }
  1719. UnLockConfig();
  1720. return err;
  1721. } // FTP_SERVER_INSTANCE::ReadAuthentInfo
  1722. BOOL
  1723. FTP_IIS_SERVICE::GetGlobalStatistics(
  1724. IN DWORD dwLevel,
  1725. OUT PCHAR *pBuffer
  1726. )
  1727. {
  1728. APIERR err = NO_ERROR;
  1729. switch( dwLevel ) {
  1730. case 0 : {
  1731. LPFTP_STATISTICS_0 pstats0;
  1732. pstats0 = (LPFTP_STATISTICS_0)
  1733. MIDL_user_allocate(sizeof(FTP_STATISTICS_0));
  1734. if( pstats0 == NULL ) {
  1735. err = ERROR_NOT_ENOUGH_MEMORY;
  1736. } else {
  1737. ATQ_STATISTICS atqStat;
  1738. ZeroMemory( pstats0, sizeof( FTP_STATISTICS_0 ) );
  1739. g_pFTPStats->CopyToStatsBuffer( pstats0 );
  1740. *pBuffer = (PCHAR)pstats0;
  1741. }
  1742. }
  1743. break;
  1744. default :
  1745. err = ERROR_INVALID_LEVEL;
  1746. break;
  1747. }
  1748. SetLastError(err);
  1749. return(err == NO_ERROR);
  1750. } // FTP_IIS_SERVICE::GetGlobalStatistics
  1751. BOOL
  1752. FTP_IIS_SERVICE::AggregateStatistics(
  1753. IN PCHAR pDestination,
  1754. IN PCHAR pSource
  1755. )
  1756. {
  1757. LPFTP_STATISTICS_0 pStatDest = (LPFTP_STATISTICS_0) pDestination;
  1758. LPFTP_STATISTICS_0 pStatSrc = (LPFTP_STATISTICS_0) pSource;
  1759. if ((NULL == pDestination) || (NULL == pSource))
  1760. {
  1761. return FALSE;
  1762. }
  1763. pStatDest->TotalBytesSent.QuadPart += pStatSrc->TotalBytesSent.QuadPart;
  1764. pStatDest->TotalBytesReceived.QuadPart += pStatSrc->TotalBytesReceived.QuadPart;
  1765. pStatDest->TotalFilesSent += pStatSrc->TotalFilesSent;
  1766. pStatDest->TotalFilesReceived += pStatSrc->TotalFilesReceived;
  1767. pStatDest->CurrentAnonymousUsers += pStatSrc->CurrentAnonymousUsers;
  1768. pStatDest->CurrentNonAnonymousUsers += pStatSrc->CurrentNonAnonymousUsers;
  1769. pStatDest->TotalAnonymousUsers += pStatSrc->TotalAnonymousUsers;
  1770. pStatDest->MaxAnonymousUsers += pStatSrc->MaxAnonymousUsers;
  1771. pStatDest->MaxNonAnonymousUsers += pStatSrc->MaxNonAnonymousUsers;
  1772. pStatDest->CurrentConnections += pStatSrc->CurrentConnections;
  1773. pStatDest->MaxConnections += pStatSrc->MaxConnections;
  1774. pStatDest->ConnectionAttempts += pStatSrc->ConnectionAttempts;
  1775. pStatDest->LogonAttempts += pStatSrc->LogonAttempts;
  1776. pStatDest->ServiceUptime = pStatSrc->ServiceUptime;
  1777. // bandwidth throttling info. Not relevant for FTP
  1778. /*
  1779. pStatDest->CurrentBlockedRequests += pStatSrc->CurrentBlockedRequests;
  1780. pStatDest->TotalBlockedRequests += pStatSrc->TotalBlockedRequests;
  1781. pStatDest->TotalAllowedRequests += pStatSrc->TotalAllowedRequests;
  1782. pStatDest->TotalRejectedRequests += pStatSrc->TotalRejectedRequests;
  1783. pStatDest->MeasuredBandwidth += pStatSrc->MeasuredBandwidth;
  1784. */
  1785. return TRUE;
  1786. }