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.

995 lines
22 KiB

  1. // domain.cpp : Implementation of CsmtpadmApp and DLL registration.
  2. #include "stdafx.h"
  3. #include "iadm.h"
  4. #include "iiscnfg.h"
  5. #include "smtpadm.h"
  6. #include "domain.h"
  7. #include "oleutil.h"
  8. #include "metautil.h"
  9. #include "listmacr.h"
  10. #include <lmapibuf.h>
  11. #include "smtpcmn.h"
  12. #include "smtpprop.h"
  13. // Must define THIS_FILE_* macros to use SmtpCreateException()
  14. #define THIS_FILE_HELP_CONTEXT 0
  15. #define THIS_FILE_PROG_ID _T("Smtpadm.Domain.1")
  16. #define THIS_FILE_IID IID_ISmtpAdminDomain
  17. #define UNASSIGNED_DOMAIN_ID ( DWORD( -1 ) )
  18. /////////////////////////////////////////////////////////////////////////////
  19. //
  20. //
  21. // Use a macro to define all the default methods
  22. //
  23. DECLARE_METHOD_IMPLEMENTATION_FOR_STANDARD_EXTENSION_INTERFACES(SmtpAdminDomain, CSmtpAdminDomain, IID_ISmtpAdminDomain)
  24. STDMETHODIMP CSmtpAdminDomain::InterfaceSupportsErrorInfo(REFIID riid)
  25. {
  26. static const IID* arr[] =
  27. {
  28. &IID_ISmtpAdminDomain,
  29. };
  30. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  31. {
  32. if (InlineIsEqualGUID(*arr[i],riid))
  33. return S_OK;
  34. }
  35. return S_FALSE;
  36. }
  37. CSmtpAdminDomain::CSmtpAdminDomain ()
  38. // CComBSTR's are initialized to NULL by default.
  39. {
  40. m_lCount = 0;
  41. m_dwActionType = SMTP_DELIVER;
  42. m_fAllowEtrn = FALSE;
  43. m_dwDomainId = UNASSIGNED_DOMAIN_ID;
  44. m_dwMaxDomainId = 0;
  45. m_fEnumerated = FALSE;
  46. m_pCurrentDomainEntry = NULL;
  47. m_pDefaultDomainEntry = NULL;
  48. InitializeListHead( &m_list );
  49. m_iadsImpl.SetService ( MD_SERVICE_NAME );
  50. m_iadsImpl.SetName ( _T("Domain") );
  51. m_iadsImpl.SetClass ( _T("IIsSmtpDomain") );
  52. }
  53. CSmtpAdminDomain::~CSmtpAdminDomain ()
  54. {
  55. EmptyList();
  56. // All CComBSTR's are freed automatically.
  57. }
  58. void CSmtpAdminDomain::EmptyList()
  59. {
  60. PLIST_ENTRY pHead;
  61. PLIST_ENTRY pEntry;
  62. DomainEntry* pDomainEntry;
  63. for( pHead=&m_list, pEntry=pHead->Flink; pEntry!=pHead; pEntry=pHead->Flink )
  64. {
  65. pDomainEntry = CONTAINING_RECORD(pEntry, DomainEntry, list);
  66. RemoveEntryList(pEntry);
  67. delete pDomainEntry;
  68. }
  69. }
  70. //
  71. // IADs methods:
  72. //
  73. DECLARE_SIMPLE_IADS_IMPLEMENTATION(CSmtpAdminDomain,m_iadsImpl)
  74. //////////////////////////////////////////////////////////////////////
  75. // Properties:
  76. //////////////////////////////////////////////////////////////////////
  77. // enumeration
  78. STDMETHODIMP CSmtpAdminDomain::get_Count ( long * plCount )
  79. {
  80. return StdPropertyGet ( m_lCount, plCount );
  81. }
  82. // current Domain Properties:
  83. STDMETHODIMP CSmtpAdminDomain::get_DomainName ( BSTR * pstrDomainName )
  84. {
  85. return StdPropertyGet ( m_strDomainName, pstrDomainName );
  86. }
  87. STDMETHODIMP CSmtpAdminDomain::put_DomainName ( BSTR strDomainName )
  88. {
  89. return StdPropertyPut ( &m_strDomainName, strDomainName );
  90. }
  91. STDMETHODIMP CSmtpAdminDomain::get_ActionType( long * plActionType )
  92. {
  93. return StdPropertyGet ( m_dwActionType, plActionType );
  94. }
  95. STDMETHODIMP CSmtpAdminDomain::put_ActionType( long lActionType )
  96. {
  97. return StdPropertyPut ( &m_dwActionType, lActionType );
  98. }
  99. // drop IsDefault!!
  100. STDMETHODIMP CSmtpAdminDomain::get_IsDefault ( BOOL * pfIsDefault )
  101. {
  102. *pfIsDefault = m_dwActionType == SMTP_DEFAULT;
  103. return NOERROR;
  104. }
  105. STDMETHODIMP CSmtpAdminDomain::put_IsDefault ( BOOL fIsDefault )
  106. {
  107. return E_NOTIMPL;
  108. }
  109. STDMETHODIMP CSmtpAdminDomain::get_IsLocal ( BOOL * pfIsLocal )
  110. {
  111. return E_NOTIMPL;
  112. }
  113. STDMETHODIMP CSmtpAdminDomain::put_IsLocal ( BOOL fIsLocal )
  114. {
  115. return E_NOTIMPL;
  116. }
  117. // if local
  118. STDMETHODIMP CSmtpAdminDomain::get_LDAPServer ( BSTR * pstrLDAPServer )
  119. {
  120. return E_NOTIMPL;
  121. //return StdPropertyGet ( m_strLDAPServer, pstrLDAPServer );
  122. }
  123. STDMETHODIMP CSmtpAdminDomain::put_LDAPServer ( BSTR strLDAPServer )
  124. {
  125. return E_NOTIMPL;
  126. //return StdPropertyPut ( &m_strLDAPServer, strLDAPServer );
  127. }
  128. STDMETHODIMP CSmtpAdminDomain::get_Account ( BSTR * pstrAccount )
  129. {
  130. return E_NOTIMPL;
  131. //return StdPropertyGet ( m_strAccount, pstrAccount );
  132. }
  133. STDMETHODIMP CSmtpAdminDomain::put_Account ( BSTR strAccount )
  134. {
  135. return E_NOTIMPL;
  136. //return StdPropertyPut ( &m_strAccount, strAccount );
  137. }
  138. STDMETHODIMP CSmtpAdminDomain::get_Password ( BSTR * pstrPassword )
  139. {
  140. return E_NOTIMPL;
  141. //return StdPropertyGet ( m_strPassword, pstrPassword );
  142. }
  143. STDMETHODIMP CSmtpAdminDomain::put_Password ( BSTR strPassword )
  144. {
  145. return E_NOTIMPL;
  146. //return StdPropertyPut ( &m_strPassword, strPassword );
  147. }
  148. STDMETHODIMP CSmtpAdminDomain::get_LDAPContainer ( BSTR * pstrLDAPContainer )
  149. {
  150. return E_NOTIMPL;
  151. //return StdPropertyGet ( m_strLDAPContainer, pstrLDAPContainer );
  152. }
  153. STDMETHODIMP CSmtpAdminDomain::put_LDAPContainer ( BSTR strLDAPContainer )
  154. {
  155. return E_NOTIMPL;
  156. //return StdPropertyPut ( &m_strLDAPContainer, strLDAPContainer );
  157. }
  158. // if remote
  159. STDMETHODIMP CSmtpAdminDomain::get_UseSSL ( BOOL * pfUseSSL )
  160. {
  161. return E_NOTIMPL;
  162. }
  163. STDMETHODIMP CSmtpAdminDomain::put_UseSSL ( BOOL fUseSSL )
  164. {
  165. return E_NOTIMPL;
  166. }
  167. STDMETHODIMP CSmtpAdminDomain::get_EnableETRN ( BOOL * pfEnableETRN )
  168. {
  169. return E_NOTIMPL;
  170. }
  171. STDMETHODIMP CSmtpAdminDomain::put_EnableETRN ( BOOL fEnableETRN )
  172. {
  173. return E_NOTIMPL;
  174. }
  175. STDMETHODIMP CSmtpAdminDomain::get_DropDir ( BSTR * pstrDropDir )
  176. {
  177. return StdPropertyGet ( m_strActionString, pstrDropDir );
  178. }
  179. STDMETHODIMP CSmtpAdminDomain::put_DropDir ( BSTR strDropDir )
  180. {
  181. return StdPropertyPut ( &m_strActionString, strDropDir );
  182. }
  183. STDMETHODIMP CSmtpAdminDomain::get_RoutingDomain ( BSTR * pstrRoutingDomain )
  184. {
  185. return StdPropertyGet ( m_strActionString, pstrRoutingDomain );
  186. }
  187. STDMETHODIMP CSmtpAdminDomain::put_RoutingDomain ( BSTR strRoutingDomain )
  188. {
  189. return StdPropertyPut ( &m_strActionString, strRoutingDomain );
  190. }
  191. //////////////////////////////////////////////////////////////////////
  192. // Methods:
  193. //////////////////////////////////////////////////////////////////////
  194. STDMETHODIMP CSmtpAdminDomain::Default ( )
  195. {
  196. TraceFunctEnter ( "CSmtpAdminDomain::Default" );
  197. m_dwActionType = SMTP_DELIVER;
  198. m_fAllowEtrn = FALSE;
  199. m_dwDomainId = UNASSIGNED_DOMAIN_ID;
  200. m_strDomainName.Empty();
  201. m_strActionString.Empty();
  202. m_pCurrentDomainEntry = NULL;
  203. TraceFunctLeave ();
  204. return NOERROR;
  205. }
  206. STDMETHODIMP CSmtpAdminDomain::Add ( )
  207. {
  208. TraceFunctEnter ( "CSmtpAdminDomain::Add" );
  209. HRESULT hr = NOERROR;
  210. DomainEntry* pOldDef = NULL;
  211. DomainEntry* pNewDomain = new DomainEntry;
  212. if( !pNewDomain )
  213. {
  214. ErrorTrace ( (LPARAM) this, "Out of memory" );
  215. hr = E_OUTOFMEMORY;
  216. goto Exit;
  217. }
  218. hr = GetFromMetabase();
  219. if( FAILED(hr) )
  220. {
  221. goto Exit;
  222. }
  223. lstrcpyW( pNewDomain-> m_strDomainName, (LPCWSTR)m_strDomainName );
  224. lstrcpyW( pNewDomain-> m_strActionString, (LPCWSTR)m_strActionString );
  225. pNewDomain-> m_dwActionType = m_dwActionType;
  226. pNewDomain-> m_fAllowEtrn = m_fAllowEtrn;
  227. // deal with default domain
  228. if( m_dwActionType == SMTP_DEFAULT )
  229. {
  230. pOldDef = m_pDefaultDomainEntry;
  231. pOldDef-> m_dwActionType = pOldDef->m_strActionString[0] ? SMTP_DROP : SMTP_DELIVER;
  232. InsertHeadList( &m_list, &pNewDomain->list );
  233. m_pDefaultDomainEntry = pNewDomain;
  234. }
  235. else
  236. {
  237. InsertTailList( &m_list, &pNewDomain->list );
  238. }
  239. hr = SaveToMetabase();
  240. if( FAILED(hr) )
  241. {
  242. RemoveEntryList( &pNewDomain->list );
  243. ErrorTrace ( (LPARAM) this, "Failed to remove domain: %x", hr );
  244. delete pNewDomain;
  245. goto Exit;
  246. }
  247. m_pCurrentDomainEntry = pNewDomain;
  248. m_lCount++;
  249. Exit:
  250. TraceFunctLeave ();
  251. return hr;
  252. }
  253. STDMETHODIMP CSmtpAdminDomain::Remove ( )
  254. {
  255. TraceFunctEnter ( "CSmtpAdminDomain::Remove" );
  256. HRESULT hr = NOERROR;
  257. // need to call get() first
  258. _ASSERT( m_pCurrentDomainEntry );
  259. _ASSERT( !lstrcmpiW( m_strDomainName, m_pCurrentDomainEntry->m_strDomainName ) );
  260. if( !m_pCurrentDomainEntry ||
  261. lstrcmpiW( m_strDomainName, m_pCurrentDomainEntry->m_strDomainName ) )
  262. {
  263. hr = SmtpCreateException (IDS_SMTPEXCEPTION_DIDNT_CALL_GET);
  264. goto Exit;
  265. }
  266. //Can't remove default domain
  267. if( m_pCurrentDomainEntry == m_pDefaultDomainEntry )
  268. {
  269. hr = SmtpCreateException (IDS_SMTPEXCEPTION_CANT_DEL_DEFAULT_DOMAIN);
  270. goto Exit;
  271. }
  272. RemoveEntryList( &m_pCurrentDomainEntry->list );
  273. hr = SaveToMetabase();
  274. Exit:
  275. TraceFunctLeave ();
  276. return hr;
  277. }
  278. STDMETHODIMP CSmtpAdminDomain::Get ( )
  279. {
  280. TraceFunctEnter ( "CSmtpAdminDomain::Get" );
  281. HRESULT hr = NOERROR;
  282. hr = GetFromMetabase();
  283. if( FAILED(hr) )
  284. {
  285. goto Exit;
  286. }
  287. // given domain name, find the entry
  288. m_pCurrentDomainEntry = FindDomainEntry( m_strDomainName );
  289. if( !m_pCurrentDomainEntry )
  290. {
  291. hr = SmtpCreateException( IDS_SMTPEXCEPTION_INVALID_ADDRESS );
  292. goto Exit;
  293. }
  294. LoadDomainProperty( m_pCurrentDomainEntry );
  295. Exit:
  296. TraceFunctLeave ();
  297. return hr;
  298. }
  299. STDMETHODIMP CSmtpAdminDomain::Set ( )
  300. {
  301. TraceFunctEnter ( "CSmtpAdminDomain::Set" );
  302. HRESULT hr = NOERROR;
  303. DomainEntry* pOldDef = NULL;
  304. // need to call get() first
  305. _ASSERT( m_pCurrentDomainEntry );
  306. if( !m_pCurrentDomainEntry )
  307. {
  308. hr = SmtpCreateException (IDS_SMTPEXCEPTION_DIDNT_CALL_GET);
  309. goto Exit;
  310. }
  311. lstrcpyW( m_pCurrentDomainEntry->m_strDomainName, m_strDomainName );
  312. lstrcpyW( m_pCurrentDomainEntry->m_strActionString, m_strActionString );
  313. m_pCurrentDomainEntry-> m_dwActionType = m_dwActionType;
  314. m_pCurrentDomainEntry-> m_fAllowEtrn = m_fAllowEtrn;
  315. // deal with default domain
  316. if( m_dwActionType == SMTP_DEFAULT && m_pDefaultDomainEntry != m_pCurrentDomainEntry )
  317. {
  318. pOldDef = m_pDefaultDomainEntry;
  319. pOldDef-> m_dwActionType = pOldDef->m_strActionString[0] ? SMTP_DROP : SMTP_DELIVER;
  320. m_pDefaultDomainEntry = m_pCurrentDomainEntry;
  321. RemoveEntryList( &m_pCurrentDomainEntry->list );
  322. InsertHeadList( &m_list, &m_pCurrentDomainEntry->list );
  323. }
  324. hr = SaveToMetabase();
  325. Exit:
  326. TraceFunctLeave ();
  327. return hr;
  328. }
  329. STDMETHODIMP CSmtpAdminDomain::Enumerate ( )
  330. {
  331. TraceFunctEnter ( "CSmtpAdminDomain::EnumDomains" );
  332. HRESULT hr = NOERROR;
  333. hr = GetFromMetabase();
  334. m_fEnumerated = TRUE;
  335. TraceFunctLeave ();
  336. return hr;
  337. }
  338. STDMETHODIMP CSmtpAdminDomain::GetNth( long lIndex )
  339. {
  340. TraceFunctEnter ( "CSmtpAdminDomain::GetNth" );
  341. HRESULT hr = NOERROR;
  342. PLIST_ENTRY pEntry;
  343. if( !m_fEnumerated )
  344. {
  345. hr = SmtpCreateException( IDS_SMTPEXCEPTION_DIDNT_ENUMERATE );
  346. goto Exit;
  347. }
  348. if( lIndex < 0 || lIndex >= m_lCount )
  349. {
  350. hr = SmtpCreateException( IDS_SMTPEXCEPTION_INVALID_INDEX );
  351. goto Exit;
  352. }
  353. pEntry = m_list.Flink;
  354. while( lIndex -- )
  355. {
  356. pEntry = pEntry-> Flink;
  357. _ASSERT( pEntry != &m_list );
  358. }
  359. m_pCurrentDomainEntry = CONTAINING_RECORD(pEntry, DomainEntry, list);
  360. LoadDomainProperty( m_pCurrentDomainEntry );
  361. Exit:
  362. TraceFunctLeave ();
  363. return hr;
  364. }
  365. STDMETHODIMP CSmtpAdminDomain::GetDefaultDomain ( )
  366. {
  367. TraceFunctEnter ( "CSmtpAdminDomain::GetDefaultDomain" );
  368. HRESULT hr = NOERROR;
  369. if( !m_pDefaultDomainEntry )
  370. {
  371. hr = GetFromMetabase();
  372. if( FAILED(hr) )
  373. {
  374. goto Exit;
  375. }
  376. }
  377. LoadDomainProperty( m_pDefaultDomainEntry );
  378. Exit:
  379. TraceFunctLeave ();
  380. return hr;
  381. }
  382. DomainEntry* CSmtpAdminDomain::FindDomainEntry( LPCWSTR lpName )
  383. {
  384. TraceFunctEnter ( "CSmtpAdminDomain::FindDomainEntry" );
  385. DomainEntry* pDomainEntry = NULL;
  386. PLIST_ENTRY pHead;
  387. PLIST_ENTRY pEntry;
  388. for( pHead=&m_list, pEntry=pHead->Flink; pEntry!=pHead; pEntry=pEntry->Flink )
  389. {
  390. pDomainEntry = CONTAINING_RECORD(pEntry, DomainEntry, list);
  391. if( !lstrcmpiW( pDomainEntry->m_strDomainName, lpName ) )
  392. {
  393. TraceFunctLeave ();
  394. return pDomainEntry;
  395. }
  396. }
  397. TraceFunctLeave ();
  398. return NULL;
  399. }
  400. STDMETHODIMP CSmtpAdminDomain::SetAsDefaultDomain ( )
  401. {
  402. TraceFunctEnter ( "CSmtpAdminDomain::SetAsDefaultDomain" );
  403. HRESULT hr = NOERROR;
  404. if( !m_pDefaultDomainEntry )
  405. {
  406. hr = GetFromMetabase();
  407. if( FAILED(hr) )
  408. {
  409. goto Exit;
  410. }
  411. }
  412. // not in the list
  413. if( m_dwDomainId == UNASSIGNED_DOMAIN_ID )
  414. {
  415. hr = Add();
  416. if( FAILED(hr) )
  417. {
  418. goto Exit;
  419. }
  420. }
  421. _ASSERT( m_dwDomainId == m_pCurrentDomainEntry->m_dwDomainId );
  422. m_pDefaultDomainEntry = m_pCurrentDomainEntry;
  423. hr = SaveToMetabase();
  424. Exit:
  425. TraceFunctLeave ();
  426. return hr;
  427. }
  428. BOOL CSmtpAdminDomain::LoadDomainProperty(DomainEntry* pDomainEntry)
  429. {
  430. TraceFunctEnter ( "CSmtpAdminDomain::LoadDomainProperty" );
  431. _ASSERT( pDomainEntry );
  432. m_strDomainName = pDomainEntry-> m_strDomainName;
  433. m_dwActionType = pDomainEntry-> m_dwActionType;
  434. m_strActionString = pDomainEntry-> m_strActionString;
  435. m_fAllowEtrn = pDomainEntry-> m_fAllowEtrn;
  436. m_dwDomainId = pDomainEntry-> m_dwDomainId;
  437. m_pCurrentDomainEntry = pDomainEntry;
  438. TraceFunctLeave ();
  439. return TRUE;
  440. }
  441. BOOL CSmtpAdminDomain::ConstructListFromMetabaseValues()
  442. {
  443. TraceFunctEnter ( "CSmtpAdminDomain::ConstructListFromMetabaseValues" );
  444. DomainEntry* pDomainEntry;
  445. TCHAR* pCh;
  446. TCHAR* wszCurrent;
  447. DWORD i;
  448. DWORD cCount = m_mszDomainRouting.Count( );
  449. EmptyList();
  450. m_lCount = 0;
  451. pCh = (TCHAR*)(LPCWSTR)m_mszDomainRouting;
  452. for( wszCurrent = pCh, i = 0;
  453. i < cCount;
  454. i++, wszCurrent += lstrlen (wszCurrent) + 1 )
  455. {
  456. pDomainEntry = new DomainEntry;
  457. if( NULL == pDomainEntry )
  458. {
  459. goto Exit;
  460. }
  461. pDomainEntry-> FromString( wszCurrent );
  462. InsertHeadList( &m_list, &pDomainEntry->list );
  463. }
  464. m_lCount += cCount;
  465. if( !m_strDefaultDomain.m_str || !m_strDefaultDomain.m_str[0] )
  466. {
  467. _ASSERT( FALSE );
  468. goto Exit;
  469. }
  470. m_pDefaultDomainEntry = new DomainEntry;
  471. if( NULL == m_pDefaultDomainEntry )
  472. {
  473. goto Exit;
  474. }
  475. lstrcpy( m_pDefaultDomainEntry-> m_strDomainName, m_strDefaultDomain.m_str );
  476. if( !m_strDropDir )
  477. {
  478. m_pDefaultDomainEntry-> m_strActionString[0] = _T('\0');
  479. }
  480. else
  481. {
  482. lstrcpy( m_pDefaultDomainEntry-> m_strActionString, m_strDropDir.m_str );
  483. }
  484. m_pDefaultDomainEntry-> m_dwActionType = SMTP_DEFAULT;
  485. InsertHeadList( &m_list, &m_pDefaultDomainEntry->list );
  486. m_lCount ++;
  487. Exit:
  488. TraceFunctLeave ();
  489. return TRUE;
  490. }
  491. BOOL CSmtpAdminDomain::ParseListToMetabaseValues() // called by SaveData()
  492. {
  493. TraceFunctEnter ( "CSmtpAdminDomain::ParseListToMetabaseValues" );
  494. BOOL fRet = TRUE;
  495. // change string list to multisz
  496. DomainEntry* pDomainEntry = NULL;
  497. PLIST_ENTRY pHead;
  498. PLIST_ENTRY pEntry;
  499. DWORD cb = 0;
  500. WCHAR* pBuf;
  501. WCHAR* p;
  502. // the first one is default domain
  503. _ASSERT( CONTAINING_RECORD( m_list.Flink, DomainEntry, list ) == m_pDefaultDomainEntry );
  504. for( pHead=&m_list, pEntry=pHead->Flink->Flink; pEntry!=pHead; pEntry=pEntry->Flink )
  505. {
  506. pDomainEntry = CONTAINING_RECORD(pEntry, DomainEntry, list);
  507. cb += lstrlenW( pDomainEntry-> m_strDomainName );
  508. cb += lstrlenW( pDomainEntry-> m_strActionString );
  509. cb += sizeof(DWORD)*2;
  510. cb += 10; // 4 commas and NULL
  511. }
  512. // two more NULL's
  513. cb += 4;
  514. pBuf = new WCHAR[cb];
  515. if( !pBuf )
  516. {
  517. ErrorTrace ( (LPARAM) this, "Out of memory" );
  518. fRet = FALSE;
  519. goto Exit;
  520. }
  521. p = pBuf;
  522. // Note: the first entry is the default domain
  523. for( pHead=&m_list, pEntry=pHead->Flink->Flink; pEntry!=pHead; pEntry=pEntry->Flink )
  524. {
  525. pDomainEntry = CONTAINING_RECORD(pEntry, DomainEntry, list);
  526. pDomainEntry->ToString( p );
  527. p += lstrlenW(p);
  528. p ++;
  529. }
  530. // add two more NULL
  531. *p = L'\0';
  532. *(p+1) = L'\0';
  533. m_mszDomainRouting.Empty();
  534. m_mszDomainRouting.Attach( pBuf );
  535. Exit:
  536. TraceFunctLeave ();
  537. return fRet;
  538. }
  539. HRESULT CSmtpAdminDomain::GetFromMetabase()
  540. {
  541. TraceFunctEnter ( "CSmtpAdminDomain::GetFromMetabase" );
  542. HRESULT hr = NOERROR;
  543. BOOL fRet = TRUE;
  544. CComPtr<IMSAdminBase> pmetabase;
  545. TCHAR szPath[METADATA_MAX_NAME_LEN+2] = {0};
  546. TCHAR szDropDir[256] = {0};
  547. TCHAR szBuf[256] = {0};
  548. hr = m_mbFactory.GetMetabaseObject ( m_iadsImpl.QueryComputer(), &pmetabase );
  549. if ( FAILED(hr) ) {
  550. return hr;
  551. }
  552. CMetabaseKey hMB( pmetabase );
  553. GetMDInstancePath( szPath, m_iadsImpl.QueryInstance() );
  554. hr = hMB.Open( szPath, METADATA_PERMISSION_READ );
  555. if( FAILED(hr) )
  556. {
  557. hr = SmtpCreateExceptionFromWin32Error( GetLastError() );
  558. goto Exit;
  559. }
  560. m_strDefaultDomain.Empty();
  561. m_strDropDir.Empty();
  562. m_mszDomainRouting.Empty();
  563. fRet = StdGetMetabaseProp ( &hMB, MD_DOMAIN_ROUTING, DEFAULT_DOMAIN_ROUTING, &m_mszDomainRouting);
  564. fRet = StdGetMetabaseProp ( &hMB, MD_DEFAULT_DOMAIN_VALUE, DEFAULT_DEFAULT_DOMAIN, &m_strDefaultDomain ) && fRet;
  565. fRet = StdGetMetabaseProp ( &hMB, MD_MAIL_DROP_DIR, DEFAULT_DROP_DIR, &m_strDropDir ) && fRet;
  566. if( !fRet )
  567. {
  568. hr = SmtpCreateExceptionFromWin32Error( GetLastError() );
  569. goto Exit;
  570. }
  571. ConstructListFromMetabaseValues();
  572. Exit:
  573. TraceFunctLeave ();
  574. return hr;
  575. }
  576. HRESULT CSmtpAdminDomain::SaveToMetabase()
  577. {
  578. TraceFunctEnter ( "CSmtpAdminDomain::SaveToMetabase" );
  579. ParseListToMetabaseValues();
  580. // these two are for default domain,
  581. // default domain needs special care,
  582. // by default, it's computed by smtpsvc from TCP/IP configuration,
  583. // don't set this key if not changed
  584. BOOL fDefChanged = FALSE;
  585. BOOL fDropChanged = FALSE;
  586. _ASSERT( m_pDefaultDomainEntry && m_pDefaultDomainEntry-> m_dwActionType == SMTP_DEFAULT );
  587. if( m_pDefaultDomainEntry )
  588. {
  589. fDefChanged = lstrcmpiW( m_strDefaultDomain, m_pDefaultDomainEntry-> m_strDomainName );
  590. fDropChanged = lstrcmpiW( m_strActionString, m_pDefaultDomainEntry-> m_strActionString );
  591. if( fDefChanged )
  592. {
  593. m_strDefaultDomain.Empty();
  594. m_strDefaultDomain = m_pDefaultDomainEntry-> m_strDomainName;
  595. }
  596. if( fDropChanged )
  597. {
  598. m_strDropDir.Empty();
  599. m_strDropDir = m_pDefaultDomainEntry-> m_strActionString;
  600. }
  601. }
  602. HRESULT hr = NOERROR;
  603. BOOL fRet = TRUE;
  604. CComPtr<IMSAdminBase> pmetabase;
  605. TCHAR szPath[METADATA_MAX_NAME_LEN+2] = {0};
  606. TCHAR szDropDir[256] = {0};
  607. TCHAR szBuf[256] = {0};
  608. hr = m_mbFactory.GetMetabaseObject ( m_iadsImpl.QueryComputer(), &pmetabase );
  609. if ( FAILED(hr) ) {
  610. return hr;
  611. }
  612. CMetabaseKey hMB( pmetabase );
  613. GetMDInstancePath( szPath, m_iadsImpl.QueryInstance() );
  614. hr = hMB.Open( szPath, METADATA_PERMISSION_WRITE );
  615. if( FAILED(hr) )
  616. {
  617. hr = SmtpCreateExceptionFromWin32Error( GetLastError() );
  618. goto Exit;
  619. }
  620. if( fDefChanged )
  621. {
  622. fRet = StdPutMetabaseProp ( &hMB, MD_DEFAULT_DOMAIN_VALUE, m_pDefaultDomainEntry-> m_strDomainName ) && fRet;
  623. }
  624. if( fDropChanged )
  625. {
  626. fRet = StdPutMetabaseProp ( &hMB, MD_MAIL_DROP_DIR, m_pDefaultDomainEntry-> m_strActionString ) && fRet;
  627. }
  628. fRet = StdPutMetabaseProp ( &hMB, MD_DOMAIN_ROUTING, &m_mszDomainRouting) && fRet;
  629. if( !fRet )
  630. {
  631. hr = SmtpCreateExceptionFromWin32Error( GetLastError() );
  632. goto Exit;
  633. }
  634. // hr = hMB.Close();
  635. // BAIL_ON_FAILURE(hr);
  636. hMB.Close();
  637. hr = pmetabase-> SaveData();
  638. BAIL_ON_FAILURE(hr);
  639. Exit:
  640. TraceFunctLeave ();
  641. return hr;
  642. }
  643. BOOL DomainEntry::FromString( LPCTSTR lpDomainString )
  644. {
  645. TraceFunctEnter ( "DomainEntry::FromString" );
  646. TCHAR szT[256] = {0};
  647. WCHAR* pCh = (WCHAR*)lpDomainString;
  648. WCHAR* pT;
  649. m_dwDomainId = UNASSIGNED_DOMAIN_ID;
  650. m_fAllowEtrn = FALSE;
  651. ZeroMemory( szT, sizeof(szT) );
  652. pT = szT;
  653. while( *pCh ) //
  654. {
  655. if( iswdigit( *pCh ) )
  656. {
  657. *pT++ = *pCh;
  658. pCh ++;
  659. continue;
  660. }
  661. if( *pCh == ',' )
  662. {
  663. pCh ++;
  664. break;
  665. }
  666. return FALSE;
  667. }
  668. if( !*pCh )
  669. return FALSE;
  670. m_dwActionType = (DWORD) _wtoi( szT );
  671. /*
  672. if( m_dwActionType >= LAST_SMTP_ACTION )
  673. {
  674. _ASSERT( FALSE );
  675. m_dwActionType = SMTP_DROP; // assume local drop domain
  676. }
  677. */
  678. ZeroMemory( m_strDomainName, sizeof(m_strDomainName) );
  679. pT = m_strDomainName;
  680. while( *pCh )
  681. {
  682. if( *pCh == ',' )
  683. {
  684. pCh ++;
  685. break;
  686. }
  687. *pT++ = *pCh++;
  688. }
  689. if( !*pCh )
  690. return FALSE;
  691. ZeroMemory( m_strActionString, sizeof(m_strActionString) );
  692. pT = m_strActionString;
  693. while( *pCh )
  694. {
  695. if( *pCh == ',' )
  696. {
  697. pCh ++;
  698. break;
  699. }
  700. *pT++ = *pCh++;
  701. }
  702. if( !*pCh )
  703. return FALSE;
  704. ZeroMemory( szT, sizeof(szT) );
  705. pT = szT;
  706. while( *pCh ) //
  707. {
  708. if( iswdigit( *pCh ) )
  709. {
  710. *pT++ = *pCh;
  711. pCh ++;
  712. continue;
  713. }
  714. if( *pCh == ',' )
  715. {
  716. pCh ++;
  717. break;
  718. }
  719. return FALSE;
  720. }
  721. m_fAllowEtrn = !! ((DWORD) _wtoi( szT ));
  722. if( !*pCh )
  723. {
  724. return FALSE;
  725. }
  726. ZeroMemory( szT, sizeof(szT) );
  727. pT = szT;
  728. while( *pCh ) //
  729. {
  730. if( iswdigit( *pCh ) )
  731. {
  732. *pT++ = *pCh;
  733. pCh ++;
  734. continue;
  735. }
  736. if( *pCh == ',' )
  737. {
  738. pCh ++;
  739. break;
  740. }
  741. return FALSE;
  742. }
  743. m_dwDomainId = (DWORD) _wtoi( szT );
  744. // ignore any other chars
  745. TraceFunctLeave ();
  746. return TRUE;
  747. }
  748. BOOL DomainEntry::ToString( LPTSTR lpDomainString )
  749. {
  750. TraceFunctEnter ( "DomainEntry::FromString" );
  751. wsprintfW( lpDomainString, L"%d,%s,%s,%d",m_dwActionType, m_strDomainName,
  752. m_strActionString, m_fAllowEtrn);
  753. TraceFunctLeave ();
  754. return TRUE;
  755. }