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.

1717 lines
63 KiB

  1. // WinPop3.cpp: implementation of the CWinPop3 class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "WinPop3.h"
  6. #include "resource.h"
  7. #include "resource2.h"
  8. #include <P3Admin.h>
  9. #include <atlbase.h>
  10. #include <comdef.h>
  11. #include <tchar.h>
  12. #include <POP3Server.h>
  13. #include <AuthID.h>
  14. #include <AuthUtil.h>
  15. #include <inetinfo.h>
  16. #include <smtpinet.h>
  17. #include <stdio.h>
  18. #include <ras.h> // For PWLEN
  19. //////////////////////////////////////////////////////////////////////
  20. // Construction/Destruction
  21. //////////////////////////////////////////////////////////////////////
  22. CWinPop3::CWinPop3() :
  23. m_bSuppressPrintError( false )
  24. {
  25. }
  26. CWinPop3::~CWinPop3()
  27. {
  28. }
  29. //////////////////////////////////////////////////////////////////////
  30. // Implementation : public
  31. //////////////////////////////////////////////////////////////////////
  32. int CWinPop3::Add(int argc, wchar_t *argv[])
  33. {
  34. HRESULT hr;
  35. bool bAddUser = false;
  36. _bstr_t _bstrDomainName;
  37. _bstr_t _bstrUserName;
  38. _bstr_t _bstrPassword;
  39. WCHAR sBuffer[MAX_PATH*2];
  40. WCHAR sPassword[PWLEN];
  41. CComPtr<IP3Config> spIConfig;
  42. CComPtr<IP3Domains> spDomains;
  43. CComPtr<IP3Domain> spDomain;
  44. CComPtr<IP3Users> spUsers;
  45. if ( (3 != argc) && (5 != argc ))
  46. return -1;
  47. if ( (5 == argc) && !(( 0 == _wcsicmp( L"/CREATEUSER", argv[3] )||( 0 == _wcsicmp( L"-CREATEUSER", argv[3] )))))
  48. return -1;
  49. if ( (sizeof(sBuffer)/sizeof(WCHAR)) < (wcslen( argv[2] ) + 1) )
  50. return E_INVALIDARG;
  51. if ( ( 5 == argc ) && (sizeof(sPassword)/sizeof(WCHAR)) < (wcslen( argv[4] ) + 1) )
  52. return E_INVALIDARG;
  53. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  54. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  55. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  56. if ( NULL == ps )
  57. {
  58. _bstrDomainName = sBuffer;
  59. }
  60. else
  61. {
  62. *ps = 0x0;
  63. _bstrUserName = sBuffer;
  64. if ( 0 == _bstrUserName.length() )
  65. return E_INVALIDARG;
  66. ps++;
  67. if ( NULL != *ps )
  68. _bstrDomainName = ps;
  69. else
  70. return E_INVALIDARG;
  71. }
  72. if ( 5 == argc )
  73. {
  74. if ( 0 == _bstrUserName.length() )
  75. return -1;
  76. // Check password for DBCS characters
  77. WCHAR *ps = argv[4];
  78. BOOL bDBCS = false;
  79. while ( !bDBCS && 0x0 != *ps )
  80. {
  81. bDBCS = (256 < *ps);
  82. ps++;
  83. }
  84. if ( bDBCS )
  85. return E_INVALIDARG;
  86. _bstrPassword = argv[4];
  87. bAddUser = true;
  88. }
  89. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  90. if ( S_OK == hr )
  91. {
  92. SetMachineName( spIConfig );
  93. hr = spIConfig->get_Domains( &spDomains );
  94. }
  95. if ( S_OK == hr )
  96. {
  97. if ( 0 == _bstrUserName.length() )
  98. { // Add a domain
  99. hr = spDomains->Add( _bstrDomainName );
  100. if ( S_OK == hr || ERROR_FILE_EXISTS == hr )
  101. {
  102. PrintMessage( IDS_SUCCESS_ADDDOMAIN );
  103. if ( ERROR_FILE_EXISTS == hr )
  104. {
  105. PrintMessage( IDS_WARNING_ADDDOMAIN_FILEALREADYEXISTS );
  106. m_bSuppressPrintError = true;
  107. }
  108. }
  109. else
  110. {
  111. if ( HRESULT_FROM_WIN32( ERROR_DOMAIN_EXISTS ) == hr )
  112. {
  113. PrintMessage( IDS_ERROR_ADDDOMAIN_ALREADYEXISTS );
  114. m_bSuppressPrintError = true;
  115. }
  116. else
  117. PrintMessage( IDS_ERROR_ADDDOMAIN_FAILED );
  118. }
  119. }
  120. else
  121. { // Add a user
  122. _variant_t _v( _bstrDomainName );
  123. CComPtr<IP3User> spUser;
  124. hr = spDomains->get_Item( _v, &spDomain );
  125. if ( S_OK == hr )
  126. hr = spDomain->get_Users( &spUsers );
  127. if ( S_OK == hr )
  128. {
  129. if ( !bAddUser )
  130. hr = spUsers->Add( _bstrUserName );
  131. else
  132. hr = spUsers->AddEx( _bstrUserName, _bstrPassword );
  133. }
  134. if ( S_OK == hr )
  135. {
  136. BOOL bConfirm;
  137. PrintMessage( IDS_SUCCESS_ADDMAILBOX );
  138. // Do we need confirmation text?
  139. hr = spIConfig->get_ConfirmAddUser( &bConfirm );
  140. if ( S_OK == hr )
  141. {
  142. _v = _bstrUserName;
  143. hr = spUsers->get_Item( _v, &spUser );
  144. }
  145. if ( S_OK == hr && !bConfirm )
  146. {
  147. BSTR bstrSAMName = NULL;
  148. hr = spUser->get_SAMName( &bstrSAMName );
  149. if ( S_OK == hr )
  150. {
  151. if ( 0 != _wcsicmp( bstrSAMName, _bstrUserName ))
  152. bConfirm = TRUE;
  153. SysFreeString( bstrSAMName );
  154. }
  155. else if ( HRESULT_FROM_WIN32( ERROR_DS_INAPPROPRIATE_AUTH ) == hr )
  156. hr = S_OK;
  157. }
  158. if ( S_OK == hr && bConfirm )
  159. { // Get confirmation text
  160. BSTR bstrConfirm;
  161. hr = spUser->get_ClientConfigDesc( &bstrConfirm );
  162. if ( S_OK == hr )
  163. {
  164. PrintMessage( bstrConfirm );
  165. SysFreeString( bstrConfirm );
  166. }
  167. }
  168. }
  169. else
  170. {
  171. if ( HRESULT_FROM_WIN32( ERROR_FILE_EXISTS ) == hr )
  172. {
  173. PrintMessage( IDS_ERROR_ADDMAILBOX_ALREADYEXISTS );
  174. m_bSuppressPrintError = true;
  175. }
  176. else if ( HRESULT_FROM_WIN32( WSAENAMETOOLONG ) == hr )
  177. {
  178. PrintMessage( IDS_ERROR_ADDMAILBOX_FAILED );
  179. PrintMessage( IDS_ERROR_ADDMAILBOX_SAMNAMETOOLONG );
  180. m_bSuppressPrintError = true;
  181. }
  182. else if ( HRESULT_FROM_WIN32( ERROR_NONE_MAPPED ) == hr )
  183. {
  184. PrintMessage( IDS_SUCCESS_ADDMAILBOX );
  185. PrintMessage( IDS_ERROR_CREATEQUOTAFILE_FAILED );
  186. }
  187. else
  188. PrintMessage( IDS_ERROR_ADDMAILBOX_FAILED );
  189. }
  190. }
  191. }
  192. return hr;
  193. }
  194. int CWinPop3::AddUserToAD(int argc, wchar_t *argv[])
  195. {
  196. HRESULT hr;
  197. _bstr_t _bstrDomainName;
  198. _bstr_t _bstrUserName;
  199. _bstr_t _bstrPassword;
  200. WCHAR sBuffer[MAX_PATH*2];
  201. CComPtr<IP3Config> spIConfig;
  202. CComPtr<IP3Domains> spDomains;
  203. CComPtr<IP3Domain> spDomain;
  204. CComPtr<IP3Users> spUsers;
  205. CComPtr<IP3User> spUser;
  206. if ( 3 != argc )
  207. return -1;
  208. if ( (sizeof(sBuffer)/sizeof(WCHAR)) < (wcslen( argv[2] ) + 1) )
  209. return E_INVALIDARG;
  210. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  211. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  212. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  213. if ( NULL == ps )
  214. {
  215. return E_INVALIDARG;
  216. }
  217. else
  218. {
  219. *ps = 0x0;
  220. _bstrUserName = sBuffer;
  221. if ( 0 == _bstrUserName.length() )
  222. return E_INVALIDARG;
  223. ps++;
  224. if ( NULL != *ps )
  225. _bstrDomainName = ps;
  226. else
  227. return E_INVALIDARG;
  228. }
  229. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  230. if ( S_OK == hr )
  231. {
  232. SetMachineName( spIConfig );
  233. hr = spIConfig->get_Domains( &spDomains );
  234. }
  235. if ( S_OK == hr )
  236. { // migrate user
  237. _variant_t _v( _bstrDomainName );
  238. hr = spDomains->get_Item( _v, &spDomain );
  239. if ( S_OK == hr )
  240. hr = spDomain->get_Users( &spUsers );
  241. if ( S_OK == hr )
  242. {
  243. _v = _bstrUserName;
  244. hr = spUsers->get_Item( _v, &spUser );
  245. }
  246. if ( S_OK == hr )
  247. {
  248. BSTR bstrAccount;
  249. char chPassword[MAX_PATH];
  250. _variant_t _vPassword;
  251. CComPtr<IAuthMethod> spIAuthMethod;
  252. if ( S_OK == hr )
  253. { // Only valid for MD5 Auth
  254. CComPtr<IAuthMethods> spIAuthMethods;
  255. BSTR bstrID;
  256. _variant_t _v;
  257. long lValue;
  258. WCHAR sBuffer[MAX_PATH];
  259. hr = spIConfig->get_Authentication( &spIAuthMethods );
  260. if ( S_OK == hr )
  261. hr = spIAuthMethods->get_CurrentAuthMethod( &_v );
  262. if ( S_OK == hr )
  263. hr = spIAuthMethods->get_Item( _v, &spIAuthMethod );
  264. if ( S_OK == hr )
  265. hr = spIAuthMethod->get_ID( &bstrID );
  266. if ( S_OK == hr )
  267. {
  268. if ( 0 != _wcsicmp( bstrID, SZ_AUTH_ID_MD5_HASH ))
  269. hr = HRESULT_FROM_WIN32( ERROR_DS_INAPPROPRIATE_AUTH );
  270. SysFreeString( bstrID );
  271. }
  272. if ( S_OK == hr ) // Get the AD AuthMethod
  273. {
  274. bool bFound = false;
  275. long lCount;
  276. hr = spIAuthMethods->get_Count( &lCount );
  277. for ( V_I4( &_v ) = 1; (S_OK == hr) && !bFound && (V_I4( &_v ) <= lCount); V_I4( &_v )++ )
  278. {
  279. hr = spIAuthMethods->get_Item( _v, &spIAuthMethod );
  280. if ( S_OK == hr )
  281. {
  282. hr = spIAuthMethod->get_ID( &bstrID );
  283. if ( S_OK == hr )
  284. {
  285. if ( 0 == _wcsicmp( bstrID, SZ_AUTH_ID_DOMAIN_AD ))
  286. bFound = true;
  287. else
  288. spIAuthMethod.Release();
  289. SysFreeString( bstrID );
  290. }
  291. }
  292. }
  293. if ( !bFound && S_OK == hr )
  294. hr = HRESULT_FROM_WIN32( ERROR_DS_DS_REQUIRED );
  295. }
  296. }
  297. if ( S_OK == hr )
  298. hr = spUser->get_EmailName( &bstrAccount );
  299. if ( S_OK == hr )
  300. {
  301. hr = GetMD5Password( bstrAccount, chPassword );
  302. if ( S_OK == hr )
  303. {
  304. _vPassword = chPassword;
  305. hr = spIAuthMethod->CreateUser( bstrAccount, _vPassword );
  306. }
  307. SysFreeString( bstrAccount );
  308. }
  309. }
  310. if ( S_OK == hr )
  311. PrintMessage( IDS_SUCCESS_MIGRATETOAD );
  312. else
  313. PrintMessage( IDS_ERROR_MIGRATETOAD );
  314. }
  315. return hr;
  316. }
  317. int CWinPop3::CreateQuotaFile(int argc, wchar_t *argv[])
  318. {
  319. HRESULT hr;
  320. bool bAddUser = false;
  321. _bstr_t _bstrDomainName;
  322. _bstr_t _bstrUserName;
  323. _bstr_t _bstrMachineName, _bstrNTUserName;
  324. WCHAR sBuffer[MAX_PATH*2];
  325. CComPtr<IP3Config> spIConfig;
  326. CComPtr<IP3Domains> spDomains;
  327. CComPtr<IP3Domain> spDomain;
  328. CComPtr<IP3Users> spUsers;
  329. CComPtr<IP3User> spUser;
  330. LPWSTR psMachine = NULL;
  331. LPWSTR psUser = NULL;
  332. if ( (3 != argc) && (4 != argc ) && (5 != argc ))
  333. return -1;
  334. if ( 4 == argc || 5 == argc )
  335. {
  336. if ( ( 0 == _wcsnicmp( L"/MACHINE:", argv[3], 9 )||( 0 == _wcsnicmp( L"-MACHINE:", argv[3], 9 ))) && 9 < wcslen( argv[3] ))
  337. {
  338. psMachine = argv[3];
  339. psMachine += 9;
  340. }
  341. if ( ( 0 == _wcsnicmp( L"/USER:", argv[3], 6 )||( 0 == _wcsnicmp( L"-USER:", argv[3], 6 ))) && 6 < wcslen( argv[3] ))
  342. {
  343. psUser = argv[3];
  344. psUser += 6;
  345. }
  346. if ( NULL == psMachine && NULL == psUser )
  347. return -1;
  348. }
  349. if ( 5 == argc )
  350. {
  351. if ( ( 0 == _wcsnicmp( L"/MACHINE:", argv[4], 9 )||( 0 == _wcsnicmp( L"-MACHINE:", argv[4], 9 ))) && 9 < wcslen( argv[4] ))
  352. {
  353. psMachine = argv[4];
  354. psMachine += 9;
  355. }
  356. if ( ( 0 == _wcsnicmp( L"/USER:", argv[4], 6 )||( 0 == _wcsnicmp( L"-USER:", argv[4], 6 ))) && 6 < wcslen( argv[4] ))
  357. {
  358. psUser = argv[4];
  359. psUser += 6;
  360. }
  361. if ( NULL == psMachine || NULL == psUser )
  362. return -1;
  363. }
  364. if ( (sizeof(sBuffer)/sizeof(WCHAR)) < (wcslen( argv[2] ) + 1) )
  365. return E_INVALIDARG;
  366. _bstrMachineName = psMachine;
  367. _bstrNTUserName = psUser;
  368. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  369. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  370. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  371. if ( NULL == ps )
  372. return -1;
  373. else
  374. {
  375. *ps = 0x0;
  376. _bstrUserName = sBuffer;
  377. if ( 0 == _bstrUserName.length() )
  378. return E_INVALIDARG;
  379. ps++;
  380. if ( NULL != *ps )
  381. _bstrDomainName = ps;
  382. else
  383. return E_INVALIDARG;
  384. }
  385. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  386. if ( S_OK == hr )
  387. {
  388. SetMachineName( spIConfig );
  389. hr = spIConfig->get_Domains( &spDomains );
  390. }
  391. if ( S_OK == hr )
  392. {
  393. _variant_t _v( _bstrDomainName );
  394. hr = spDomains->get_Item( _v, &spDomain );
  395. if ( S_OK == hr )
  396. hr = spDomain->get_Users( &spUsers );
  397. _v.Detach();
  398. _v = _bstrUserName;
  399. if ( S_OK == hr )
  400. hr = spUsers->get_Item( _v, &spUser );
  401. _v.Detach();
  402. if ( S_OK == hr )
  403. hr = spUser->CreateQuotaFile( _bstrMachineName, _bstrNTUserName );
  404. if ( S_OK == hr )
  405. PrintMessage( IDS_SUCCESS_CREATEQUOTAFILE );
  406. else
  407. PrintMessage( IDS_ERROR_CREATEQUOTAFILE_FAILED );
  408. }
  409. return hr;
  410. }
  411. int CWinPop3::Del(int argc, wchar_t *argv[])
  412. {
  413. HRESULT hr = S_OK;
  414. bool bConfirm = true;
  415. bool bDeleteUser = false;
  416. _bstr_t _bstrDomainName;
  417. _bstr_t _bstrUserName;
  418. WCHAR sBuffer[MAX_PATH*2];
  419. if (( 3 > argc ) || ( 5 < argc ))
  420. return -1;
  421. if ( (sizeof(sBuffer)/sizeof(WCHAR)) < (wcslen( argv[2] ) + 1) )
  422. return E_INVALIDARG;
  423. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  424. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  425. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  426. if ( NULL == ps )
  427. {
  428. _bstrDomainName = sBuffer;
  429. }
  430. else
  431. {
  432. *ps = 0x0;
  433. _bstrUserName = sBuffer;
  434. if ( 0 == _bstrUserName.length() )
  435. return E_INVALIDARG;
  436. ps++;
  437. if ( NULL != *ps )
  438. _bstrDomainName = ps;
  439. else
  440. return E_INVALIDARG;
  441. }
  442. for ( int i = 3; S_OK == hr && i < argc; i++ )
  443. {
  444. if ( (0 == _wcsicmp( L"/Y", argv[i] )) || (0 == _wcsicmp( L"-Y", argv[i] )) )
  445. {
  446. if ( bConfirm )
  447. bConfirm = false;
  448. else
  449. hr = -1;
  450. }
  451. else if ( ( 0 == _wcsicmp( L"/DELETEUSER", argv[i] )) || ( 0 == _wcsicmp( L"-DELETEUSER", argv[i] )))
  452. {
  453. if ( !bDeleteUser )
  454. bDeleteUser = true;
  455. else
  456. hr = -1;
  457. }
  458. else
  459. hr = -1;
  460. }
  461. if ( S_OK == hr )
  462. {
  463. CComPtr<IP3Config> spIConfig;
  464. CComPtr<IP3Domains> spDomains;
  465. CComPtr<IP3Domain> spDomain;
  466. CComPtr<IP3Users> spUsers;
  467. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  468. if ( S_OK == hr )
  469. {
  470. SetMachineName( spIConfig );
  471. hr = spIConfig->get_Domains( &spDomains );
  472. }
  473. if ( S_OK == hr )
  474. {
  475. if ( 0 == _bstrUserName.length() )
  476. { // Delete a domain
  477. hr = spDomains->Remove( _bstrDomainName );
  478. if ( S_OK == hr || ERROR_PATH_NOT_FOUND == hr )
  479. {
  480. PrintMessage( IDS_SUCCESS_DELDOMAIN );
  481. if ( ERROR_PATH_NOT_FOUND == hr )
  482. {
  483. PrintMessage( IDS_WARNING_DELDOMAIN_PATHNOTFOUND );
  484. m_bSuppressPrintError = true;
  485. }
  486. }
  487. else
  488. PrintMessage( IDS_ERROR_DELDOMAIN_FAILED );
  489. }
  490. else
  491. { // Delete a user
  492. variant_t _v(_bstrDomainName);
  493. hr = spDomains->get_Item( _v, &spDomain );
  494. if ( S_OK == hr )
  495. hr = spDomain->get_Users( &spUsers );
  496. if ( S_OK == hr )
  497. {
  498. if ( !bDeleteUser )
  499. hr = spUsers->Remove( _bstrUserName );
  500. else
  501. hr = spUsers->RemoveEx( _bstrUserName );
  502. }
  503. if ( S_OK == hr )
  504. PrintMessage( IDS_SUCCESS_DELMAILBOX );
  505. else
  506. PrintMessage( IDS_ERROR_DELMAILBOX_FAILED );
  507. }
  508. }
  509. }
  510. return hr;
  511. }
  512. int CWinPop3::Get(int argc, wchar_t *argv[])
  513. {
  514. HRESULT hr;
  515. long lValue;
  516. bool bPrintValue = true;
  517. CComPtr<IP3Config> spIConfig;
  518. CComPtr<IP3Service> spIService;
  519. if ( 3 != argc )
  520. return -2;
  521. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  522. if ( S_OK == hr )
  523. {
  524. SetMachineName( spIConfig );
  525. hr = spIConfig->get_Service( &spIService );
  526. }
  527. if ( S_OK == hr )
  528. {
  529. if ( 0 == _wcsicmp( L"PORT", argv[2] ))
  530. hr = spIService->get_Port( &lValue );
  531. else if ( 0 == _wcsicmp( L"LOGGING", argv[2] ))
  532. {
  533. hr = spIConfig->get_LoggingLevel( &lValue );
  534. if ( S_OK == hr )
  535. {
  536. WCHAR sBuffer[MAX_PATH], sBuffer2[MAX_PATH];
  537. bPrintValue = false;
  538. // 0 None
  539. if ( !LoadString( NULL, IDS_CMD_LOGNONE, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  540. sBuffer2[0] = 0;
  541. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 0 %s %s", ( 0 == lValue )?L"*":L" ", sBuffer2 ))
  542. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  543. PrintMessage( sBuffer );
  544. // 1 Minimum
  545. if ( !LoadString( NULL, IDS_CMD_LOGMINIMUM, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  546. sBuffer2[0] = 0;
  547. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 1 %s %s", ( 1 == lValue )?L"*":L" ", sBuffer2 ))
  548. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  549. PrintMessage( sBuffer );
  550. // 2 Medium
  551. if ( !LoadString( NULL, IDS_CMD_LOGMEDIUM, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  552. sBuffer2[0] = 0;
  553. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 2 %s %s", ( 2 == lValue )?L"*":L" ", sBuffer2 ))
  554. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  555. PrintMessage( sBuffer );
  556. // 3 IDS_CMD_LOGMAXIMUM
  557. if ( !LoadString( NULL, IDS_CMD_LOGMAXIMUM, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  558. sBuffer2[0] = 0;
  559. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 3 %s %s", ( 2 < lValue )?L"*":L" ", sBuffer2 ))
  560. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  561. PrintMessage( sBuffer );
  562. }
  563. }
  564. else if ( 0 == _wcsicmp( L"MAILROOT", argv[2] ))
  565. {
  566. BSTR bstrMailRoot;
  567. hr = spIConfig->get_MailRoot( &bstrMailRoot );
  568. if ( S_OK == hr )
  569. {
  570. PrintMessage( bstrMailRoot );
  571. SysFreeString( bstrMailRoot );
  572. }
  573. bPrintValue = false;
  574. }
  575. else if ( 0 == _wcsicmp( L"SOCKET", argv[2] ))
  576. {
  577. argv[2] = L"SOCKETBACKLOG";
  578. hr = Get( 3, argv );
  579. argv[2] = L"SOCKETMAX";
  580. hr = Get( 3, argv );
  581. argv[2] = L"SOCKETMIN";
  582. hr = Get( 3, argv );
  583. hr = spIService->get_SocketsThreshold( &lValue );
  584. }
  585. else if ( 0 == _wcsicmp( L"SOCKETMIN", argv[2] ))
  586. hr = spIService->get_SocketsMin( &lValue );
  587. else if ( 0 == _wcsicmp( L"SOCKETMAX", argv[2] ))
  588. hr = spIService->get_SocketsMax( &lValue );
  589. else if ( 0 == _wcsicmp( L"SOCKETTHRESHOLD", argv[2] ))
  590. hr = spIService->get_SocketsThreshold( &lValue );
  591. else if ( 0 == _wcsicmp( L"SOCKETBACKLOG", argv[2] ))
  592. hr = spIService->get_SocketsBacklog( &lValue );
  593. else if ( 0 == _wcsicmp( L"SPAREQUIRED", argv[2] ))
  594. {
  595. WCHAR sBuffer[MAX_PATH], sBuffer2[MAX_PATH];
  596. hr = spIService->get_SPARequired( reinterpret_cast<PBOOL>( &lValue ));
  597. if ( S_OK == hr )
  598. {
  599. bPrintValue = false;
  600. // 0 value
  601. if ( !LoadString( NULL, IDS_CMD_SPAREQUIRED0, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  602. sBuffer2[0] = 0;
  603. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 0 %s %s", ( 0 == lValue )?L"*":L" ", sBuffer2 ))
  604. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  605. PrintMessage( sBuffer );
  606. // 1 value
  607. if ( !LoadString( NULL, IDS_CMD_SPAREQUIRED1, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  608. sBuffer2[0] = 0;
  609. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L" 1 %s %s", ( 0 != lValue )?L"*":L" ", sBuffer2 ))
  610. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  611. PrintMessage( sBuffer );
  612. }
  613. }
  614. else if ( 0 == _wcsicmp( L"THREADCOUNT", argv[2] ))
  615. hr = spIService->get_ThreadCountPerCPU( &lValue );
  616. else if (( 0 == _wcsicmp( L"AUTHENTICATION", argv[2] )) || ( 0 == _wcsicmp( L"AUTH", argv[2] )))
  617. {
  618. CComPtr<IAuthMethods> spIAuthMethods;
  619. CComPtr<IAuthMethod> spIAuthMethod;
  620. BSTR bstrName;
  621. _variant_t _v;
  622. long lCount;
  623. WCHAR sBuffer[MAX_PATH];
  624. hr = spIConfig->get_Authentication( &spIAuthMethods );
  625. if ( S_OK == hr )
  626. hr = spIAuthMethods->get_Count( &lCount );
  627. if ( S_OK == hr )
  628. hr = spIAuthMethods->get_CurrentAuthMethod( &_v );
  629. if ( S_OK == hr )
  630. {
  631. lValue = V_I4( &_v );
  632. }
  633. else if ( HRESULT_FROM_WIN32(ERROR_DS_AUTH_METHOD_NOT_SUPPORTED) == hr )
  634. {
  635. lValue = 0;
  636. _v = lValue; // Set the variant type
  637. hr = S_OK;
  638. }
  639. for ( V_I4( &_v ) = 1; (S_OK == hr) && (V_I4( &_v ) <= lCount); V_I4( &_v )++ )
  640. {
  641. hr = spIAuthMethods->get_Item( _v, &spIAuthMethod );
  642. if ( S_OK == hr )
  643. {
  644. hr = spIAuthMethod->get_Name( &bstrName );
  645. if ( S_OK == hr )
  646. {
  647. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%2d %s %s", V_I4( &_v ), ( V_I4( &_v ) == lValue )?L"*":L" ", bstrName ))
  648. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  649. PrintMessage( sBuffer );
  650. SysFreeString( bstrName );
  651. }
  652. spIAuthMethod.Release();
  653. }
  654. }
  655. bPrintValue = false;
  656. }
  657. else
  658. {
  659. hr = -2;
  660. bPrintValue = false;
  661. }
  662. }
  663. if ( S_OK == hr && bPrintValue )
  664. {
  665. WCHAR sBuffer[MAX_PATH];
  666. _itow( lValue, sBuffer, 10 );
  667. PrintMessage( sBuffer );
  668. }
  669. return hr;
  670. }
  671. int CWinPop3::Init(int argc, wchar_t *argv[])
  672. {
  673. HRESULT hr;
  674. BOOL bRegister = TRUE;
  675. CComPtr<IP3Config> spIConfig;
  676. if ( 2 != argc && 3 != argc )
  677. return -1;
  678. if ( 3 == argc )
  679. {
  680. if ( 0 == wcscmp( L"0", argv[2] ))
  681. bRegister = FALSE;
  682. else if ( 0 != wcscmp( L"1", argv[2] ))
  683. return -1;
  684. }
  685. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  686. // Set the IIS Meta base settings
  687. if SUCCEEDED( hr )
  688. hr = spIConfig->IISConfig( bRegister );
  689. return hr;
  690. }
  691. int CWinPop3::List(int argc, wchar_t *argv[])
  692. {
  693. HRESULT hr;
  694. VARIANT v;
  695. BSTR bstrName;
  696. _bstr_t _bstrDomainName;
  697. CComPtr<IP3Config> spIConfig;
  698. CComPtr<IP3Domains> spIDomains;
  699. CComPtr<IP3Domain> spIDomain = NULL;
  700. CComPtr<IEnumVARIANT> spIEnumVARIANT;
  701. if ( 3 != argc && 2 != argc )
  702. return -1;
  703. if ( 3 == argc )
  704. _bstrDomainName = argv[2];
  705. VariantInit( &v );
  706. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  707. if ( S_OK == hr )
  708. {
  709. SetMachineName( spIConfig );
  710. hr = spIConfig->get_Domains( &spIDomains );
  711. }
  712. if ( S_OK == hr )
  713. {
  714. if ( 0 == _bstrDomainName.length() )
  715. { // List domains
  716. hr = spIDomains->get__NewEnum( &spIEnumVARIANT );
  717. if ( S_OK == hr )
  718. {
  719. PrintMessage( IDS_CMD_LISTDOMAINS );
  720. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  721. }
  722. while ( S_OK == hr )
  723. {
  724. if ( VT_DISPATCH != V_VT( &v ))
  725. hr = E_UNEXPECTED;
  726. else
  727. {
  728. if ( NULL != spIDomain.p )
  729. spIDomain.Release();
  730. hr = V_DISPATCH( &v )->QueryInterface( __uuidof( IP3Domain ), reinterpret_cast<void**>( &spIDomain ));
  731. }
  732. if ( S_OK == hr )
  733. {
  734. hr = spIDomain->get_Name( &bstrName );
  735. if ( S_OK == hr )
  736. {
  737. PrintMessage( bstrName );
  738. SysFreeString( bstrName );
  739. }
  740. }
  741. VariantClear( &v );
  742. if ( S_OK == hr )
  743. {
  744. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  745. }
  746. }
  747. if ( S_FALSE == hr )
  748. hr = S_OK; // Reached end of enumeration
  749. if ( S_OK == hr )
  750. {
  751. long lCount;
  752. WCHAR sBuffer[MAX_PATH], sBuffer2[MAX_PATH];
  753. hr = spIDomains->get_Count( &lCount );
  754. if ( S_OK == hr && LoadString( NULL, IDS_CMD_LISTDOMAINSEND, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  755. {
  756. if ( 0 < _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), sBuffer2, lCount ))
  757. PrintMessage( sBuffer );
  758. }
  759. }
  760. }
  761. else
  762. { // List users
  763. CComPtr<IP3Users> spIUsers;
  764. CComPtr<IP3User> spIUser = NULL;
  765. _variant_t _v( _bstrDomainName );
  766. hr = spIDomains->get_Item( _v, &spIDomain );
  767. if ( S_OK == hr )
  768. {
  769. hr = spIDomain->get_Users( &spIUsers );
  770. if ( S_OK == hr )
  771. { // List users
  772. hr = spIUsers->get__NewEnum( &spIEnumVARIANT );
  773. if ( S_OK == hr )
  774. {
  775. PrintMessage( IDS_CMD_LISTUSERS, false );
  776. PrintMessage( _bstrDomainName );
  777. PrintMessage( L" " );
  778. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  779. }
  780. while ( S_OK == hr )
  781. {
  782. if ( VT_DISPATCH != V_VT( &v ))
  783. hr = E_UNEXPECTED;
  784. else
  785. {
  786. if ( NULL != spIUser.p )
  787. spIUser.Release();
  788. hr = V_DISPATCH( &v )->QueryInterface( __uuidof( IP3User ), reinterpret_cast<void**>( &spIUser ));
  789. }
  790. if ( S_OK == hr )
  791. {
  792. hr = spIUser->get_Name( &bstrName );
  793. if ( S_OK == hr )
  794. {
  795. PrintMessage( bstrName );
  796. SysFreeString( bstrName );
  797. }
  798. }
  799. VariantClear( &v );
  800. if ( S_OK == hr )
  801. {
  802. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  803. }
  804. }
  805. if ( S_FALSE == hr )
  806. hr = S_OK; // Reached end of enumeration
  807. if ( S_OK == hr )
  808. {
  809. long lCount;
  810. WCHAR sBuffer[MAX_PATH], sBuffer2[MAX_PATH];
  811. hr = spIUsers->get_Count( &lCount );
  812. if ( S_OK == hr && LoadString( NULL, IDS_CMD_LISTUSERSEND, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  813. {
  814. if ( 0 < _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), sBuffer2, lCount ))
  815. PrintMessage( sBuffer );
  816. }
  817. }
  818. }
  819. }
  820. }
  821. }
  822. return hr;
  823. }
  824. int CWinPop3::Lock(int argc, wchar_t *argv[], BOOL bLock)
  825. {
  826. HRESULT hr;
  827. _bstr_t _bstrDomainName;
  828. _bstr_t _bstrUserName;
  829. WCHAR sBuffer[MAX_PATH*2];
  830. CComPtr<IP3Config> spIConfig;
  831. CComPtr<IP3Domains> spIDomains;
  832. CComPtr<IP3Domain> spIDomain;
  833. CComPtr<IEnumVARIANT> spIEnumVARIANT;
  834. if ( 3 != argc )
  835. return -1;
  836. if ( (sizeof(sBuffer)/sizeof(WCHAR)) < (wcslen( argv[2] ) + 1) )
  837. return E_INVALIDARG;
  838. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  839. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  840. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  841. if ( NULL == ps )
  842. {
  843. _bstrDomainName = sBuffer;
  844. }
  845. else
  846. {
  847. *ps = 0x0;
  848. _bstrUserName = sBuffer;
  849. if ( 0 == _bstrUserName.length() )
  850. return E_INVALIDARG;
  851. ps++;
  852. if ( NULL != *ps )
  853. _bstrDomainName = ps;
  854. else
  855. return E_INVALIDARG;
  856. }
  857. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  858. if ( S_OK == hr )
  859. {
  860. SetMachineName( spIConfig );
  861. hr = spIConfig->get_Domains( &spIDomains );
  862. }
  863. if ( S_OK == hr )
  864. {
  865. _variant_t _v( _bstrDomainName );
  866. hr = spIDomains->get_Item( _v, &spIDomain );
  867. if ( S_OK == hr )
  868. {
  869. if ( 0 == _bstrUserName.length() )
  870. {
  871. hr = spIDomain->put_Lock( bLock ); // Lock the domain
  872. if ( S_OK == hr )
  873. {
  874. if ( bLock )
  875. PrintMessage( IDS_SUCCESS_LOCKDOMAIN );
  876. else
  877. PrintMessage( IDS_SUCCESS_UNLOCKDOMAIN );
  878. }
  879. else
  880. {
  881. if ( bLock )
  882. {
  883. if ( HRESULT_FROM_WIN32( ERROR_LOCKED ) == hr )
  884. {
  885. PrintMessage( IDS_ERROR_LOCKDOMAIN_ALREADYLOCKED );
  886. m_bSuppressPrintError = true;
  887. }
  888. else
  889. PrintMessage( IDS_ERROR_LOCKDOMAIN_FAILED );
  890. }
  891. else
  892. {
  893. if ( HRESULT_FROM_WIN32( ERROR_NOT_LOCKED ) == hr )
  894. {
  895. PrintMessage( IDS_ERROR_UNLOCKDOMAIN_ALREADYUNLOCKED );
  896. m_bSuppressPrintError = true;
  897. }
  898. else
  899. PrintMessage( IDS_ERROR_UNLOCKDOMAIN_FAILED );
  900. }
  901. }
  902. }
  903. else
  904. { // Lock user
  905. CComPtr<IP3Users> spIUsers;
  906. CComPtr<IP3User> spIUser;
  907. hr = spIDomain->get_Users( &spIUsers );
  908. if ( S_OK == hr )
  909. { // List users
  910. _v = _bstrUserName;
  911. hr = spIUsers->get_Item( _v, &spIUser );
  912. if ( S_OK == hr )
  913. hr = spIUser->put_Lock( bLock );
  914. }
  915. if ( S_OK == hr )
  916. {
  917. if ( bLock )
  918. PrintMessage( IDS_SUCCESS_LOCKMAILBOX );
  919. else
  920. PrintMessage( IDS_SUCCESS_UNLOCKMAILBOX );
  921. }
  922. else
  923. {
  924. if ( bLock )
  925. {
  926. if ( HRESULT_FROM_WIN32( ERROR_LOCKED ) == hr )
  927. {
  928. PrintMessage( IDS_ERROR_LOCKMAILBOX_ALREADYLOCKED );
  929. m_bSuppressPrintError = true;
  930. }
  931. else
  932. PrintMessage( IDS_ERROR_LOCKMAILBOX_FAILED );
  933. }
  934. else
  935. {
  936. if ( HRESULT_FROM_WIN32( ERROR_NOT_LOCKED ) == hr )
  937. {
  938. PrintMessage( IDS_ERROR_UNLOCKMAILBOX_ALREADYUNLOCKED );
  939. m_bSuppressPrintError = true;
  940. }
  941. else
  942. PrintMessage( IDS_ERROR_UNLOCKMAILBOX_FAILED );
  943. }
  944. }
  945. }
  946. }
  947. }
  948. return hr;
  949. }
  950. int CWinPop3::Net(int argc, wchar_t *argv[])
  951. {
  952. HRESULT hr;
  953. CComPtr<IP3Config> spIConfig;
  954. CComPtr<IP3Service> spIService;
  955. if ( (4 != argc) )
  956. return -1;
  957. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_INPROC_SERVER, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  958. if ( S_OK == hr )
  959. {
  960. SetMachineName( spIConfig );
  961. hr = spIConfig->get_Service( &spIService );
  962. }
  963. if ( S_OK == hr )
  964. {
  965. if ( 0 == _wcsicmp( L"START", argv[2] ))
  966. {
  967. if ( 0 == _wcsicmp( IISADMIN_SERVICE_NAME, argv[3] ))
  968. hr = spIService->StartIISAdminService();
  969. else if ( 0 == _wcsicmp( POP3_SERVICE_NAME, argv[3] ))
  970. hr = spIService->StartPOP3Service();
  971. else if ( 0 == _wcsicmp( SMTP_SERVICE_NAME, argv[3] ))
  972. hr = spIService->StartSMTPService();
  973. else if ( 0 == _wcsicmp( W3_SERVICE_NAME, argv[3] ))
  974. hr = spIService->StartW3Service();
  975. else
  976. hr = -1;
  977. }
  978. else if ( 0 == _wcsicmp( L"STOP", argv[2] ))
  979. {
  980. if ( 0 == _wcsicmp( IISADMIN_SERVICE_NAME, argv[3] ))
  981. hr = spIService->StopIISAdminService();
  982. else if ( 0 == _wcsicmp( POP3_SERVICE_NAME, argv[3] ))
  983. hr = spIService->StopPOP3Service();
  984. else if ( 0 == _wcsicmp( SMTP_SERVICE_NAME, argv[3] ))
  985. hr = spIService->StopSMTPService();
  986. else if ( 0 == _wcsicmp( W3_SERVICE_NAME, argv[3] ))
  987. hr = spIService->StopW3Service();
  988. else
  989. hr = -1;
  990. }
  991. else if ( 0 == _wcsicmp( L"STATUS", argv[2] ))
  992. {
  993. long lStatus;
  994. if ( 0 == _wcsicmp( IISADMIN_SERVICE_NAME, argv[3] ))
  995. hr = spIService->get_IISAdminServiceStatus( &lStatus );
  996. else if ( 0 == _wcsicmp( POP3_SERVICE_NAME, argv[3] ))
  997. hr = spIService->get_POP3ServiceStatus( &lStatus );
  998. else if ( 0 == _wcsicmp( SMTP_SERVICE_NAME, argv[3] ))
  999. hr = spIService->get_SMTPServiceStatus( &lStatus );
  1000. else if ( 0 == _wcsicmp( W3_SERVICE_NAME, argv[3] ))
  1001. hr = spIService->get_W3ServiceStatus( &lStatus );
  1002. else
  1003. hr = -1;
  1004. if ( S_OK == hr )
  1005. PrintMessage( IDS_SERVICESTATUS_DESCRIPTIONS + lStatus );
  1006. }
  1007. else if ( 0 == _wcsicmp( L"PAUSE", argv[2] ))
  1008. {
  1009. if ( 0 == _wcsicmp( IISADMIN_SERVICE_NAME, argv[3] ))
  1010. hr = spIService->PauseIISAdminService();
  1011. else if ( 0 == _wcsicmp( POP3_SERVICE_NAME, argv[3] ))
  1012. hr = spIService->PausePOP3Service();
  1013. else if ( 0 == _wcsicmp( SMTP_SERVICE_NAME, argv[3] ))
  1014. hr = spIService->PauseSMTPService();
  1015. else if ( 0 == _wcsicmp( W3_SERVICE_NAME, argv[3] ))
  1016. hr = spIService->PauseW3Service();
  1017. else
  1018. hr = -1;
  1019. }
  1020. else if ( 0 == _wcsicmp( L"RESUME", argv[2] ))
  1021. {
  1022. if ( 0 == _wcsicmp( IISADMIN_SERVICE_NAME, argv[3] ))
  1023. hr = spIService->ResumeIISAdminService();
  1024. else if ( 0 == _wcsicmp( POP3_SERVICE_NAME, argv[3] ))
  1025. hr = spIService->ResumePOP3Service();
  1026. else if ( 0 == _wcsicmp( SMTP_SERVICE_NAME, argv[3] ))
  1027. hr = spIService->ResumeSMTPService();
  1028. else if ( 0 == _wcsicmp( W3_SERVICE_NAME, argv[3] ))
  1029. hr = spIService->ResumeW3Service();
  1030. else
  1031. hr = -1;
  1032. }
  1033. else
  1034. hr = -1;
  1035. }
  1036. return hr;
  1037. }
  1038. #define RESTART_POP3SVC 1
  1039. #define RESTART_POP3SVC_SMTP 2
  1040. int CWinPop3::Set(int argc, wchar_t *argv[])
  1041. {
  1042. HRESULT hr;
  1043. long lValue;
  1044. int iServiceRestart = 0;
  1045. CComPtr<IP3Config> spIConfig;
  1046. CComPtr<IP3Service> spIService;
  1047. if ( (4 != argc) && (7 != argc ))
  1048. return -2;
  1049. if ( 0 == _wcsicmp( L"SOCKET", argv[2] ))
  1050. {
  1051. if ( 7 != argc ) return -2;
  1052. if ( ! ( StrIsDigit( argv[3] ) && StrIsDigit( argv[4] ) && StrIsDigit( argv[5] ) && StrIsDigit( argv[6] )))
  1053. return E_INVALIDARG;
  1054. }
  1055. if ( 0 != _wcsicmp( L"MAILROOT", argv[2] ) && !StrIsDigit( argv[3] ))
  1056. return E_INVALIDARG;
  1057. lValue = _wtol( argv[3] );
  1058. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_INPROC_SERVER, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  1059. if ( S_OK == hr )
  1060. {
  1061. SetMachineName( spIConfig );
  1062. hr = spIConfig->get_Service( &spIService );
  1063. }
  1064. if ( S_OK == hr )
  1065. {
  1066. if ( 0 == _wcsicmp( L"PORT", argv[2] ))
  1067. {
  1068. hr = spIService->put_Port( lValue );
  1069. iServiceRestart = RESTART_POP3SVC;
  1070. }
  1071. else if ( 0 == _wcsicmp( L"LOGGING", argv[2] ))
  1072. {
  1073. hr = spIConfig->put_LoggingLevel( lValue );
  1074. iServiceRestart = RESTART_POP3SVC_SMTP;
  1075. }
  1076. else if ( 0 == _wcsicmp( L"MAILROOT", argv[2] ))
  1077. {
  1078. _bstr_t _bstrMailRoot = argv[3];
  1079. HRESULT hr2;
  1080. long lCount;
  1081. CComPtr<IP3Domains> spIDomains;
  1082. hr2 = spIConfig->get_Domains( &spIDomains );
  1083. if ( S_OK == hr2 )
  1084. hr2 = spIDomains->get_Count( &lCount );
  1085. hr = spIConfig->put_MailRoot( _bstrMailRoot );
  1086. if ( S_OK == hr )
  1087. {
  1088. PrintMessage( IDS_SUCCESS_SETMAILROOT );
  1089. PrintMessage( IDS_MESSAGE_POP3SVC_STMP_RESTART );
  1090. if ( S_OK == hr2 && 0 < lCount )
  1091. {
  1092. PrintMessage( IDS_WARNING_SETMAILROOT );
  1093. }
  1094. }
  1095. }
  1096. else if ( 0 == _wcsicmp( L"SOCKET", argv[2] ) && (7 == argc) )
  1097. {
  1098. long lBacklog, lMax, lMin, lThreshold;
  1099. lBacklog = lValue;
  1100. lMax = _wtol( argv[4] );
  1101. lMin = _wtol( argv[5] );
  1102. lThreshold = _wtol( argv[6] );
  1103. hr = spIService->SetSockets( lMax, lMin, lThreshold, lBacklog);
  1104. iServiceRestart = RESTART_POP3SVC;
  1105. }
  1106. else if ( 0 == _wcsnicmp( L"SOCKET", argv[2], 6 ))
  1107. {
  1108. long lBacklog, lMax, lMin, lThreshold;
  1109. hr = spIService->get_SocketsMin( &lMin );
  1110. if ( S_OK == hr )
  1111. hr = spIService->get_SocketsMax( &lMax );
  1112. if ( S_OK == hr )
  1113. hr = spIService->get_SocketsThreshold( &lThreshold );
  1114. if ( S_OK == hr )
  1115. hr = spIService->get_SocketsBacklog( &lBacklog );
  1116. if ( S_OK == hr )
  1117. {
  1118. if ( 0 == _wcsicmp( L"SOCKETMIN", argv[2] ))
  1119. hr = spIService->SetSockets( lMax, lValue, lThreshold, lBacklog );
  1120. else if ( 0 == _wcsicmp( L"SOCKETMAX", argv[2] ))
  1121. hr = spIService->SetSockets( lValue, lMin, lThreshold, lBacklog );
  1122. else if ( 0 == _wcsicmp( L"SOCKETTHRESHOLD", argv[2] ))
  1123. hr = spIService->SetSockets( lMax, lMin, lValue, lBacklog );
  1124. else if ( 0 == _wcsicmp( L"SOCKETBACKLOG", argv[2] ))
  1125. hr = spIService->SetSockets( lMax, lMin, lThreshold, lValue );
  1126. else
  1127. hr = -2;
  1128. }
  1129. iServiceRestart = RESTART_POP3SVC;
  1130. }
  1131. else if ( 0 == _wcsicmp( L"SPAREQUIRED", argv[2] ))
  1132. {
  1133. hr = spIService->put_SPARequired( lValue );
  1134. iServiceRestart = RESTART_POP3SVC;
  1135. }
  1136. else if ( 0 == _wcsicmp( L"THREADCOUNT", argv[2] ))
  1137. {
  1138. hr = spIService->put_ThreadCountPerCPU( lValue );
  1139. iServiceRestart = RESTART_POP3SVC;
  1140. }
  1141. else if (( 0 == _wcsicmp( L"AUTHENTICATION", argv[2] )) || ( 0 == _wcsicmp( L"AUTH", argv[2] )))
  1142. {
  1143. CComPtr<IAuthMethods> spIAuthMethods;
  1144. CComPtr<IAuthMethod> spIAuthMethod;
  1145. BSTR bstrName;
  1146. _variant_t _v;
  1147. WCHAR sBuffer[MAX_PATH];
  1148. V_VT( &_v ) = VT_I4;
  1149. V_I4( &_v ) = lValue;
  1150. hr = spIConfig->get_Authentication( &spIAuthMethods );
  1151. if ( S_OK == hr )
  1152. {
  1153. hr = spIAuthMethods->put_CurrentAuthMethod( _v );
  1154. if ( STG_E_ACCESSDENIED == hr )
  1155. {
  1156. PrintMessage( IDS_ERROR_SETAUTH_FAILED );
  1157. m_bSuppressPrintError = true;
  1158. }
  1159. }
  1160. if ( S_OK == hr )
  1161. hr = spIAuthMethods->Save();
  1162. if ( S_OK == hr )
  1163. {
  1164. _v.Clear();
  1165. hr = spIAuthMethods->get_CurrentAuthMethod( &_v );
  1166. }
  1167. if ( S_OK == hr )
  1168. hr = spIAuthMethods->get_Item( _v, &spIAuthMethod );
  1169. if ( S_OK == hr )
  1170. {
  1171. hr = spIAuthMethod->get_Name( &bstrName );
  1172. if ( S_OK == hr )
  1173. {
  1174. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%2d %s %s", V_I4( &_v ), ( V_I4( &_v ) == lValue )?L"*":L" ", bstrName ))
  1175. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1176. PrintMessage( sBuffer );
  1177. SysFreeString( bstrName );
  1178. }
  1179. }
  1180. }
  1181. else
  1182. hr = -2;
  1183. }
  1184. if ( S_OK == hr )
  1185. {
  1186. if ( RESTART_POP3SVC == iServiceRestart )
  1187. PrintMessage( IDS_MESSAGE_POP3SVC_RESTART );
  1188. if ( RESTART_POP3SVC_SMTP == iServiceRestart )
  1189. PrintMessage( IDS_MESSAGE_POP3SVC_STMP_RESTART );
  1190. }
  1191. return hr;
  1192. }
  1193. int CWinPop3::SetPassword(int argc, wchar_t *argv[])
  1194. {
  1195. HRESULT hr;
  1196. _bstr_t _bstrAccount;
  1197. _variant_t _vNewPassword, _vOldPassword;
  1198. CComPtr<IP3Config> spIConfig;
  1199. CComPtr<IP3Service> spIService;
  1200. if ( 4 != argc )
  1201. return -1;
  1202. _bstrAccount = argv[2];
  1203. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_INPROC_SERVER, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  1204. // Validate this mailbox before we do anything else
  1205. if ( S_OK == hr )
  1206. {
  1207. _bstr_t _bstrDomainName;
  1208. _bstr_t _bstrUserName;
  1209. WCHAR sBuffer[MAX_PATH*2];
  1210. CComPtr<IP3Domains> spDomains;
  1211. CComPtr<IP3Domain> spDomain;
  1212. CComPtr<IP3Users> spUsers;
  1213. CComPtr<IP3User> spUser;
  1214. sBuffer[(sizeof(sBuffer)/sizeof(WCHAR))-1] = 0;
  1215. wcsncpy( sBuffer, argv[2], (sizeof(sBuffer)/sizeof(WCHAR))-1 );
  1216. WCHAR *ps = wcsrchr( sBuffer, L'@' );
  1217. if ( NULL == ps )
  1218. hr = E_INVALIDARG;
  1219. else
  1220. {
  1221. *ps = 0x0;
  1222. _bstrUserName = sBuffer;
  1223. if ( 0 == _bstrUserName.length() )
  1224. return E_INVALIDARG;
  1225. ps++;
  1226. if ( NULL != *ps )
  1227. _bstrDomainName = ps;
  1228. else
  1229. return E_INVALIDARG;
  1230. }
  1231. if ( S_OK == hr )
  1232. { // Check password for DBCS characters
  1233. WCHAR *ps = argv[3];
  1234. BOOL bDBCS = false;
  1235. while ( !bDBCS && 0x0 != *ps )
  1236. {
  1237. bDBCS = (256 < *ps);
  1238. ps++;
  1239. }
  1240. if ( bDBCS )
  1241. hr = E_INVALIDARG;
  1242. }
  1243. if ( S_OK == hr )
  1244. {
  1245. hr = spIConfig->get_Domains( &spDomains );
  1246. if ( S_OK == hr )
  1247. {
  1248. _variant_t _v( _bstrDomainName );
  1249. hr = spDomains->get_Item( _v, &spDomain );
  1250. if ( S_OK == hr )
  1251. hr = spDomain->get_Users( &spUsers );
  1252. if ( S_OK == hr )
  1253. {
  1254. _v = _bstrUserName;
  1255. hr = spUsers->get_Item( _v, &spUser );
  1256. }
  1257. }
  1258. }
  1259. }
  1260. _vNewPassword = argv[3];
  1261. V_VT( &_vOldPassword ) = VT_BSTR;
  1262. V_BSTR( &_vOldPassword ) = NULL;
  1263. if ( S_OK == hr )
  1264. {
  1265. CComPtr<IAuthMethods> spIAuthMethods;
  1266. CComPtr<IAuthMethod> spIAuthMethod;
  1267. _variant_t _v;
  1268. hr = spIConfig->get_Authentication( &spIAuthMethods );
  1269. if ( S_OK == hr )
  1270. hr = spIAuthMethods->get_CurrentAuthMethod( &_v );
  1271. if ( S_OK == hr )
  1272. hr = spIAuthMethods->get_Item( _v, &spIAuthMethod );
  1273. if ( S_OK == hr )
  1274. hr = spIAuthMethod->ChangePassword( _bstrAccount, _vNewPassword, _vOldPassword );
  1275. }
  1276. if ( S_OK != hr )
  1277. PrintMessage( IDS_ERROR_SETPASSWORD_FAILED );
  1278. return hr;
  1279. }
  1280. int CWinPop3::Stat(int argc, wchar_t *argv[])
  1281. {
  1282. HRESULT hr;
  1283. _bstr_t _bstrDomainName;
  1284. CComPtr<IP3Config> spIConfig;
  1285. CComPtr<IP3Domains> spIDomains;
  1286. CComPtr<IP3Domain> spIDomain = NULL;
  1287. CComPtr<IP3Users> spIUsers;
  1288. CComPtr<IEnumVARIANT> spIEnumVARIANT;
  1289. VARIANT v;
  1290. BOOL bIsLocked;
  1291. BSTR bstrName;
  1292. WCHAR sBuffer[MAX_PATH*2];
  1293. WCHAR sLocked[MAX_PATH];
  1294. long lCount, lDiskUsage, lFactor, lMailboxes;
  1295. __int64 i64DiskUsage, i64Factor, i64Mailboxes, i64Messages;
  1296. if ( 3 != argc && 2 != argc )
  1297. return -1;
  1298. if ( 3 == argc )
  1299. _bstrDomainName = argv[2];
  1300. i64DiskUsage = i64Factor = i64Mailboxes = i64Messages = 0;
  1301. if ( 0 == LoadString( NULL, IDS_LOCKED, sLocked, sizeof( sLocked )/sizeof(WCHAR) ))
  1302. wcscpy( sLocked, L"L" );
  1303. VariantInit( &v );
  1304. hr = CoCreateInstance( __uuidof( P3Config ), NULL, CLSCTX_ALL, __uuidof( IP3Config ),reinterpret_cast<LPVOID *>( &spIConfig ));
  1305. if ( S_OK == hr )
  1306. {
  1307. SetMachineName( spIConfig );
  1308. hr = spIConfig->get_Domains( &spIDomains );
  1309. }
  1310. if ( S_OK == hr )
  1311. {
  1312. if ( 0 == _bstrDomainName.length() )
  1313. { // List domains
  1314. hr = spIDomains->get__NewEnum( &spIEnumVARIANT );
  1315. if ( S_OK == hr )
  1316. { // Headings
  1317. WCHAR sBuffer2[128], sBuffer3[128], sBuffer4[128], sBuffer5[128];
  1318. if ( LoadString( NULL, IDS_CMD_STATDOMAINSMAILBOXES, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ) &&
  1319. LoadString( NULL, IDS_CMD_STATDOMAINSDISKUSAGE, sBuffer3, sizeof( sBuffer3 )/sizeof(WCHAR) ) &&
  1320. LoadString( NULL, IDS_CMD_STATDOMAINSMESSAGES, sBuffer4, sizeof( sBuffer4 )/sizeof(WCHAR) ) &&
  1321. LoadString( NULL, IDS_CMD_STATDOMAINS, sBuffer5, sizeof( sBuffer5 )/sizeof(WCHAR) )
  1322. )
  1323. {
  1324. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"\n %s\n\n%10s %10s %10s", sBuffer5, sBuffer2, sBuffer3, sBuffer4 ))
  1325. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1326. PrintMessage( sBuffer );
  1327. }
  1328. else
  1329. PrintMessage( IDS_CMD_STATDOMAINS );
  1330. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  1331. }
  1332. while ( S_OK == hr )
  1333. {
  1334. if ( VT_DISPATCH != V_VT( &v ))
  1335. hr = E_UNEXPECTED;
  1336. else
  1337. {
  1338. if ( NULL != spIDomain.p )
  1339. spIDomain.Release();
  1340. hr = V_DISPATCH( &v )->QueryInterface( __uuidof( IP3Domain ), reinterpret_cast<void**>( &spIDomain ));
  1341. }
  1342. VariantClear( &v );
  1343. if ( S_OK == hr )
  1344. { // Name
  1345. hr = spIDomain->get_Name( &bstrName );
  1346. if ( S_OK == hr )
  1347. { // Mailboxes
  1348. hr = spIDomain->get_Users( &spIUsers );
  1349. if ( S_OK == hr )
  1350. hr = spIUsers->get_Count( &lMailboxes );
  1351. // Lock Status
  1352. if ( S_OK == hr )
  1353. hr = spIDomain->get_Lock( &bIsLocked );
  1354. // MessageDiskUsage
  1355. if ( S_OK == hr )
  1356. hr = spIDomain->get_MessageDiskUsage( &lFactor, &lDiskUsage );
  1357. // MessageCount
  1358. if ( S_OK == hr )
  1359. hr = spIDomain->get_MessageCount( &lCount );
  1360. if ( S_OK == hr )
  1361. { // Got everything
  1362. i64Mailboxes += lMailboxes;
  1363. i64DiskUsage += lFactor * lDiskUsage;
  1364. i64Messages += lCount;
  1365. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%10d %10d %10d %15s %s", lMailboxes, lFactor * lDiskUsage, lCount, (bIsLocked)?sLocked:L" ", bstrName ))
  1366. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1367. PrintMessage( sBuffer );
  1368. }
  1369. else
  1370. { // Got the domain name, had problem somewhere else, let's just list the domain name
  1371. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%10s %10s %10s %15s %s", L" ", L" ", L" ", L" ", bstrName ))
  1372. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1373. PrintMessage( sBuffer );
  1374. hr = S_OK;
  1375. }
  1376. SysFreeString( bstrName );
  1377. }
  1378. }
  1379. if ( S_OK == hr )
  1380. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  1381. }
  1382. if ( S_FALSE == hr )
  1383. hr = S_OK; // Reached end of enumeration
  1384. if ( S_OK == hr )
  1385. {
  1386. long lCount;
  1387. WCHAR sBuffer2[MAX_PATH];
  1388. hr = spIDomains->get_Count( &lCount );
  1389. if ( S_OK == hr && LoadString( NULL, IDS_CMD_LISTDOMAINSEND, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  1390. {
  1391. if ( 0 < _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), sBuffer2 + 1, lCount ))
  1392. {
  1393. wcsncpy( sBuffer2, sBuffer, sizeof( sBuffer2 )/sizeof(WCHAR) );
  1394. sBuffer2[(sizeof(sBuffer2)/sizeof(WCHAR))-1] = 0;
  1395. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"\n%10I64d %10I64d %10I64d %14s %s ", i64Mailboxes, i64DiskUsage, i64Messages, L" ", sBuffer2 ))
  1396. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1397. PrintMessage( sBuffer );
  1398. }
  1399. }
  1400. }
  1401. }
  1402. else
  1403. { // List users
  1404. CComPtr<IP3Users> spIUsers;
  1405. CComPtr<IP3User> spIUser = NULL;
  1406. _variant_t _v( _bstrDomainName );
  1407. hr = spIDomains->get_Item( _v, &spIDomain );
  1408. if ( S_OK == hr )
  1409. {
  1410. hr = spIDomain->get_Users( &spIUsers );
  1411. if ( S_OK == hr )
  1412. { // List users
  1413. hr = spIUsers->get__NewEnum( &spIEnumVARIANT );
  1414. if ( S_OK == hr )
  1415. { // Headings
  1416. WCHAR sBuffer2[128], sBuffer3[128], sBuffer4[128];
  1417. if ( LoadString( NULL, IDS_CMD_STATUSERSDISKUSAGE, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ) &&
  1418. LoadString( NULL, IDS_CMD_STATUSERSMESSAGES, sBuffer3, sizeof( sBuffer3 )/sizeof(WCHAR) ) &&
  1419. LoadString( NULL, IDS_CMD_STATUSERS, sBuffer4, sizeof( sBuffer4 )/sizeof(WCHAR) )
  1420. )
  1421. {
  1422. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"\n %s%s\n\n %10s %10s", sBuffer4, static_cast<LPWSTR>( _bstrDomainName ), sBuffer2, sBuffer3 ))
  1423. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1424. PrintMessage( sBuffer );
  1425. }
  1426. else
  1427. PrintMessage( IDS_CMD_STATUSERS );
  1428. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  1429. }
  1430. while ( S_OK == hr )
  1431. {
  1432. if ( VT_DISPATCH != V_VT( &v ))
  1433. hr = E_UNEXPECTED;
  1434. else
  1435. {
  1436. if ( NULL != spIUser.p )
  1437. spIUser.Release();
  1438. hr = V_DISPATCH( &v )->QueryInterface( __uuidof( IP3User ), reinterpret_cast<void**>( &spIUser ));
  1439. }
  1440. if ( S_OK == hr )
  1441. {
  1442. hr = spIUser->get_Name( &bstrName );
  1443. if ( S_OK == hr )
  1444. { // Lock Status
  1445. hr = spIUser->get_Lock( &bIsLocked );
  1446. // MessageDiskUsage
  1447. if ( S_OK == hr )
  1448. hr = spIUser->get_MessageDiskUsage( &lFactor, &lDiskUsage );
  1449. // MessageCount
  1450. if ( S_OK == hr )
  1451. hr = spIUser->get_MessageCount( &lCount );
  1452. if ( S_OK == hr )
  1453. { // Got everything
  1454. i64DiskUsage += lFactor * lDiskUsage;
  1455. i64Messages += lCount;
  1456. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%10d %10d %15s %s", lFactor * lDiskUsage, lCount, (bIsLocked)?sLocked:L" ", bstrName ))
  1457. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1458. PrintMessage( sBuffer );
  1459. }
  1460. else
  1461. { // Got the domain name, had problem somewhere else, let's just list the domain name
  1462. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"%10s %10s %15s %s", L" ", L" ", L" ", bstrName ))
  1463. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1464. PrintMessage( sBuffer );
  1465. hr = S_OK;
  1466. }
  1467. SysFreeString( bstrName );
  1468. }
  1469. }
  1470. VariantClear( &v );
  1471. if ( S_OK == hr )
  1472. hr = spIEnumVARIANT->Next( 1, &v, NULL );
  1473. }
  1474. if ( S_FALSE == hr )
  1475. hr = S_OK; // Reached end of enumeration
  1476. if ( S_OK == hr )
  1477. {
  1478. long lCount;
  1479. WCHAR sBuffer2[MAX_PATH];
  1480. hr = spIUsers->get_Count( &lCount );
  1481. if ( S_OK == hr && LoadString( NULL, IDS_CMD_LISTUSERSEND, sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR) ))
  1482. {
  1483. if ( 0 < _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), sBuffer2 + 1, lCount ))
  1484. {
  1485. wcsncpy( sBuffer2, sBuffer, sizeof( sBuffer2 )/sizeof(WCHAR) );
  1486. sBuffer2[(sizeof(sBuffer2)/sizeof(WCHAR))-1] = 0;
  1487. if ( 0 > _snwprintf( sBuffer, sizeof( sBuffer )/sizeof(WCHAR), L"\n%10I64d %10I64d %14s %s ", i64DiskUsage, i64Messages, L" ", sBuffer2 ))
  1488. sBuffer[(sizeof( sBuffer )/sizeof(WCHAR))-1] = 0;
  1489. PrintMessage( sBuffer );
  1490. }
  1491. }
  1492. }
  1493. }
  1494. }
  1495. }
  1496. }
  1497. return hr;
  1498. }
  1499. void CWinPop3::PrintError( int iRC )
  1500. {
  1501. if ( m_bSuppressPrintError || (E_FAIL == iRC) )
  1502. {
  1503. m_bSuppressPrintError = false;
  1504. return;
  1505. }
  1506. LPVOID lpMsgBuf;
  1507. if ( FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, iRC, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>( &lpMsgBuf ), 0, NULL ))
  1508. {
  1509. wprintf( reinterpret_cast<LPWSTR>( lpMsgBuf ) );
  1510. LocalFree( lpMsgBuf );
  1511. }
  1512. else
  1513. {
  1514. WCHAR sBuffer[32];
  1515. PrintMessage( IDS_ERROR_UNKNOWN );
  1516. wsprintf( sBuffer, L"%x", iRC );
  1517. PrintMessage( sBuffer );
  1518. }
  1519. }
  1520. void CWinPop3::PrintMessage( LPWSTR psMessage, bool bCRLF /*= true*/ )
  1521. {
  1522. wprintf( psMessage );
  1523. if ( bCRLF )
  1524. wprintf( L"\r\n" );
  1525. }
  1526. void CWinPop3::PrintMessage( int iID, bool bCRLF /*= true*/ )
  1527. {
  1528. WCHAR sBuffer[512];
  1529. if ( LoadString( NULL, iID, sBuffer, sizeof( sBuffer )/sizeof(WCHAR) ))
  1530. PrintMessage( sBuffer, bCRLF );
  1531. else
  1532. assert( false );
  1533. }
  1534. void CWinPop3::PrintUsage()
  1535. {
  1536. WCHAR sBuffer[512];
  1537. for ( int i = IDS_WINPOP_USAGE1; i < IDS_WINPOP_USAGEEND; i++ )
  1538. {
  1539. if ( LoadString( NULL, i, sBuffer, sizeof( sBuffer )/sizeof(WCHAR) ))
  1540. {
  1541. if ( IDS_WINPOP_USAGE2 == i )
  1542. {
  1543. WCHAR sBuffer2[512];
  1544. if ( 0 > _snwprintf( sBuffer2, sizeof( sBuffer2 )/sizeof(WCHAR), sBuffer, POP3_SERVER_NAME_L ))
  1545. sBuffer2[(sizeof( sBuffer2 )/sizeof(WCHAR))-1] = 0;
  1546. wcscpy( sBuffer, sBuffer2 );
  1547. }
  1548. wprintf( sBuffer );
  1549. }
  1550. }
  1551. }
  1552. void CWinPop3::PrintUsageGetSet()
  1553. {
  1554. WCHAR sBuffer[512];
  1555. for ( int i = IDS_WINPOP_GETSET1; i < IDS_WINPOP_GETSETEND; i++ )
  1556. {
  1557. if ( LoadString( NULL, i, sBuffer, sizeof( sBuffer )/sizeof(WCHAR) ))
  1558. {
  1559. wprintf( sBuffer );
  1560. }
  1561. }
  1562. }
  1563. //////////////////////////////////////////////////////////////////////
  1564. // Implementation : protected
  1565. //////////////////////////////////////////////////////////////////////
  1566. void CWinPop3::SetMachineName( IP3Config *pIConfig )
  1567. {
  1568. if ( NULL == pIConfig )
  1569. return;
  1570. HRESULT hr;
  1571. HKEY hKey;
  1572. DWORD dwType = REG_SZ;
  1573. long lRC;
  1574. WCHAR sBuffer[MAX_PATH];
  1575. DWORD dwSize = MAX_PATH;
  1576. _bstr_t _bstrMachineName;
  1577. lRC = RegOpenKeyEx( HKEY_LOCAL_MACHINE, POP3SERVER_SOFTWARE_SUBKEY, 0, KEY_QUERY_VALUE, &hKey );
  1578. if ( ERROR_SUCCESS == lRC )
  1579. {
  1580. lRC = RegQueryValueEx( hKey, L"WinpopRemoteAdmin", 0, &dwType, reinterpret_cast<LPBYTE>( sBuffer ), &dwSize );
  1581. RegCloseKey( hKey );
  1582. }
  1583. if ( ERROR_SUCCESS == lRC )
  1584. {
  1585. _bstrMachineName = sBuffer;
  1586. hr = pIConfig->put_MachineName( _bstrMachineName );
  1587. }
  1588. }
  1589. bool CWinPop3::StrIsDigit( LPWSTR ps )
  1590. {
  1591. if ( NULL == ps )
  1592. return false;
  1593. bool bRC = true;
  1594. while ( bRC && 0 != *ps )
  1595. {
  1596. bRC = iswdigit( *ps ) ? true : false;
  1597. ps++;
  1598. }
  1599. return bRC;
  1600. }