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.

3489 lines
121 KiB

  1. /*---------------------------------------------------------------------------
  2. File: MgeDB.cpp
  3. Comments: Implementation of DBManager COM object.
  4. This is interface that the Domain Migrator uses to communicate to the
  5. Database (PROTAR.MDB). This interface allows Domain Migrator to Save and
  6. later retrieve information/Setting to run the Migration process.
  7. (c) Copyright 1999, Mission Critical Software, Inc., All Rights Reserved
  8. Proprietary and confidential to Mission Critical Software, Inc.
  9. REVISION LOG ENTRY
  10. Revision By: Sham Chauthani
  11. Revised on 07/02/99 12:40:00
  12. ---------------------------------------------------------------------------
  13. */
  14. #include "stdafx.h"
  15. #include "mcs.h"
  16. #include "ErrDct.hpp"
  17. #include "DBMgr.h"
  18. #include "MgeDB.h"
  19. #include <share.h>
  20. #include <comdef.h>
  21. #include <lm.h>
  22. #include "UString.hpp"
  23. #include "TxtSid.h"
  24. #include "LSAUtils.h"
  25. #include "HrMsg.h"
  26. #include "StringConversion.h"
  27. #import "msado21.tlb" no_namespace implementation_only rename("EOF", "EndOfFile")
  28. #import "msadox.dll" implementation_only exclude("DataTypeEnum")
  29. //#import <msjro.dll> no_namespace implementation_only
  30. #include <msjro.tlh>
  31. #include <msjro.tli>
  32. #ifdef _DEBUG
  33. #define new DEBUG_NEW
  34. #undef THIS_FILE
  35. static char THIS_FILE[] = __FILE__;
  36. #endif
  37. TErrorDct err;
  38. using namespace _com_util;
  39. #define MAX_BUF_LEN 255
  40. #ifndef JETDBENGINETYPE_JET4X
  41. #define JETDBENGINETYPE_JET4X 0x05 // from MSJetOleDb.h
  42. #endif
  43. StringLoader gString;
  44. /////////////////////////////////////////////////////////////////////////////
  45. // CIManageDB
  46. TError dct;
  47. TError& errCommon = dct;
  48. //----------------------------------------------------------------------------
  49. // Constructor / Destructor
  50. //----------------------------------------------------------------------------
  51. CIManageDB::CIManageDB()
  52. {
  53. }
  54. CIManageDB::~CIManageDB()
  55. {
  56. }
  57. //----------------------------------------------------------------------------
  58. // FinalConstruct
  59. //----------------------------------------------------------------------------
  60. HRESULT CIManageDB::FinalConstruct()
  61. {
  62. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  63. HRESULT hr = S_OK;
  64. try
  65. {
  66. _bstr_t sMissing(L"");
  67. _bstr_t sUser(L"Admin");
  68. WCHAR sConnect[LEN_Path];
  69. WCHAR sDir[LEN_Path];
  70. // Get the path to the MDB file from the registry
  71. TRegKey key;
  72. DWORD rc = key.Open(sKeyBase);
  73. if ( !rc )
  74. rc = key.ValueGetStr(L"Directory", sDir, LEN_Path);
  75. if ( rc != 0 )
  76. wcscpy(sDir, L"");
  77. // Now build the connect string.
  78. wsprintf(sConnect, L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sprotar.mdb;", sDir);
  79. CheckError(m_cn.CreateInstance(__uuidof(Connection)));
  80. m_cn->Open(sConnect, sUser, sMissing, adConnectUnspecified);
  81. m_vtConn = (IDispatch *) m_cn;
  82. // if necessary, upgrade database to 4.x
  83. long lEngineType = m_cn->Properties->Item[_T("Jet OLEDB:Engine Type")]->Value;
  84. if (lEngineType < JETDBENGINETYPE_JET4X)
  85. {
  86. m_cn->Close();
  87. UpgradeDatabase(sDir);
  88. m_cn->Open(sConnect, sUser, sMissing, adConnectUnspecified);
  89. }
  90. reportStruct * prs = NULL;
  91. _variant_t var;
  92. // Migrated accounts report information
  93. CheckError(m_pQueryMapping.CreateInstance(__uuidof(VarSet)));
  94. m_pQueryMapping->put(L"MigratedAccounts", L"Select SourceDomain, TargetDomain, Type, SourceAdsPath, TargetAdsPath from MigratedObjects where Type <> 'computer' order by time");
  95. prs = new reportStruct();
  96. if (!prs)
  97. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  98. prs->sReportName = GET_BSTR(IDS_REPORT_MigratedAccounts);
  99. prs->arReportFields[0] = GET_BSTR(IDS_TABLE_FIELD_SourceDomain);
  100. prs->arReportSize[0] = 10;
  101. prs->arReportFields[1] = GET_BSTR(IDS_TABLE_FIELD_TargetDomain);
  102. prs->arReportSize[1] = 10;
  103. prs->arReportFields[2] = GET_BSTR(IDS_TABLE_FIELD_Type);
  104. prs->arReportSize[2] = 10;
  105. prs->arReportFields[3] = GET_BSTR(IDS_TABLE_FIELD_SourceAdsPath);
  106. prs->arReportSize[3] = 35;
  107. prs->arReportFields[4] = GET_BSTR(IDS_TABLE_FIELD_TargetAdsPath);
  108. prs->arReportSize[4] = 35;
  109. prs->colsFilled = 5;
  110. var.vt = VT_BYREF | VT_UI1;
  111. var.pbVal = (unsigned char *)prs;
  112. m_pQueryMapping->putObject(L"MigratedAccounts.DispInfo", var);
  113. // Migrated computers report information
  114. m_pQueryMapping->put(L"MigratedComputers", L"Select SourceDomain, TargetDomain, Type, SourceAdsPath, TargetAdsPath from MigratedObjects where Type = 'computer' order by time");
  115. prs = new reportStruct();
  116. if (!prs)
  117. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  118. prs->sReportName = GET_BSTR(IDS_REPORT_MigratedComputers);
  119. prs->arReportFields[0] = GET_BSTR(IDS_TABLE_FIELD_SourceDomain);
  120. prs->arReportSize[0] = 10;
  121. prs->arReportFields[1] = GET_BSTR(IDS_TABLE_FIELD_TargetDomain);
  122. prs->arReportSize[1] = 10;
  123. prs->arReportFields[2] = GET_BSTR(IDS_TABLE_FIELD_Type);
  124. prs->arReportSize[2] = 10;
  125. prs->arReportFields[3] = GET_BSTR(IDS_TABLE_FIELD_SourceAdsPath);
  126. prs->arReportSize[3] = 35;
  127. prs->arReportFields[4] = GET_BSTR(IDS_TABLE_FIELD_TargetAdsPath);
  128. prs->arReportSize[4] = 35;
  129. prs->colsFilled = 5;
  130. var.vt = VT_BYREF | VT_UI1;
  131. var.pbVal = (unsigned char *)prs;
  132. m_pQueryMapping->putObject(L"MigratedComputers.DispInfo", var);
  133. // Expired computers report information
  134. m_pQueryMapping->put(L"ExpiredComputers", L"Select Time, DomainName, CompName, Description, int(pwdage/86400) & ' days' as 'Password Age' from PasswordAge where pwdage > 2592000 order by DomainName, CompName");
  135. prs = new reportStruct();
  136. if (!prs)
  137. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  138. prs->sReportName = GET_BSTR(IDS_REPORT_ExpiredComputers);
  139. prs->arReportFields[0] = GET_BSTR(IDS_TABLE_FIELD_Time);
  140. prs->arReportSize[0] = 20;
  141. prs->arReportFields[1] = GET_BSTR(IDS_TABLE_FIELD_DomainName);
  142. prs->arReportSize[1] = 15;
  143. prs->arReportFields[2] = GET_BSTR(IDS_TABLE_FIELD_CompName);
  144. prs->arReportSize[2] = 15;
  145. prs->arReportFields[3] = GET_BSTR(IDS_TABLE_FIELD_Description);
  146. prs->arReportSize[3] = 35;
  147. prs->arReportFields[4] = GET_BSTR(IDS_TABLE_FIELD_PwdAge);
  148. prs->arReportSize[4] = 15;
  149. prs->colsFilled = 5;
  150. var.vt = VT_BYREF | VT_UI1;
  151. var.pbVal = (unsigned char *)prs;
  152. m_pQueryMapping->putObject(L"ExpiredComputers.DispInfo", var);
  153. // Account reference report informaiton.
  154. m_pQueryMapping->put(L"AccountReferences", L"Select DomainName, Account, AccountSid, Server, RefCount as '# of Ref', RefType As ReferenceType from AccountRefs where RefCount > 0 order by DomainName, Account, Server");
  155. prs = new reportStruct();
  156. if (!prs)
  157. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  158. prs->sReportName = GET_BSTR(IDS_REPORT_AccountReferences);
  159. prs->arReportFields[0] = GET_BSTR(IDS_TABLE_FIELD_DomainName);
  160. prs->arReportSize[0] = 15;
  161. prs->arReportFields[1] = GET_BSTR(IDS_TABLE_FIELD_Account);
  162. prs->arReportSize[1] = 20;
  163. prs->arReportFields[2] = GET_BSTR(IDS_TABLE_FIELD_AccountSid);
  164. prs->arReportSize[2] = 25;
  165. prs->arReportFields[3] = GET_BSTR(IDS_TABLE_FIELD_Server);
  166. prs->arReportSize[3] = 15;
  167. prs->arReportFields[4] = GET_BSTR(IDS_TABLE_FIELD_RefCount);
  168. prs->arReportSize[4] = 10;
  169. prs->arReportFields[5] = GET_BSTR(IDS_TABLE_FIELD_RefType);
  170. prs->arReportSize[5] = 15;
  171. prs->colsFilled = 6;
  172. var.vt = VT_BYREF | VT_UI1;
  173. var.pbVal = (unsigned char *)prs;
  174. m_pQueryMapping->putObject(L"AccountReferences.DispInfo", var);
  175. // Name conflict report information
  176. m_pQueryMapping->put(L"NameConflicts",
  177. L"SELECT"
  178. L" SourceAccounts.Name,"
  179. L" SourceAccounts.RDN,"
  180. L" SourceAccounts.Type,"
  181. L" TargetAccounts.Type,"
  182. L" IIf(SourceAccounts.Name=TargetAccounts.Name,'" +
  183. GET_BSTR(IDS_TABLE_SAM_CONFLICT_VALUE) +
  184. L"','') +"
  185. L" IIf(SourceAccounts.Name=TargetAccounts.Name And SourceAccounts.RDN=TargetAccounts.RDN,',','') +"
  186. L" IIf(SourceAccounts.RDN=TargetAccounts.RDN,'" +
  187. GET_BSTR(IDS_TABLE_RDN_CONFLICT_VALUE) +
  188. L"',''),"
  189. L" TargetAccounts.[Canonical Name] "
  190. L"FROM SourceAccounts, TargetAccounts "
  191. L"WHERE"
  192. L" SourceAccounts.Name=TargetAccounts.Name OR SourceAccounts.RDN=TargetAccounts.RDN "
  193. L"ORDER BY"
  194. L" SourceAccounts.Name, TargetAccounts.Name");
  195. // m_pQueryMapping->put(L"NameConflicts", L"SELECT SourceAccounts.Name as AccountName, SourceAccounts.Type as SourceType, TargetAccounts.Type as TargetType, SourceAccounts.Description as \
  196. // SourceDescription, TargetAccounts.Description as TargetDescription, SourceAccounts.FullName as SourceFullName, TargetAccounts.FullName as TargetFullName \
  197. // FROM SourceAccounts, TargetAccounts WHERE (((SourceAccounts.Name)=[TargetAccounts].[Name])) ORDER BY SourceAccounts.Name");
  198. prs = new reportStruct();
  199. if (!prs)
  200. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  201. prs->sReportName = GET_BSTR(IDS_REPORT_NameConflicts);
  202. prs->arReportFields[0] = GET_BSTR(IDS_TABLE_FIELD_Account);
  203. prs->arReportSize[0] = 20;
  204. prs->arReportFields[1] = GET_BSTR(IDS_TABLE_FIELD_SourceRDN);
  205. prs->arReportSize[1] = 20;
  206. prs->arReportFields[2] = GET_BSTR(IDS_TABLE_FIELD_SourceType);
  207. prs->arReportSize[2] = 10;
  208. prs->arReportFields[3] = GET_BSTR(IDS_TABLE_FIELD_TargetType);
  209. prs->arReportSize[3] = 10;
  210. prs->arReportFields[4] = GET_BSTR(IDS_TABLE_FIELD_ConflictAtt);
  211. prs->arReportSize[4] = 15;
  212. prs->arReportFields[5] = GET_BSTR(IDS_TABLE_FIELD_TargetCanonicalName);
  213. prs->arReportSize[5] = 25;
  214. prs->colsFilled = 6;
  215. var.vt = VT_BYREF | VT_UI1;
  216. var.pbVal = (unsigned char *)prs;
  217. m_pQueryMapping->putObject(L"NameConflicts.DispInfo", var);
  218. // we will handle the cleanup ourselves.
  219. VariantInit(&var);
  220. CheckError(m_rsAccounts.CreateInstance(__uuidof(Recordset)));
  221. }
  222. catch (_com_error& ce)
  223. {
  224. hr = Error((LPCOLESTR)ce.Description(), ce.GUID(), ce.Error());
  225. }
  226. catch (...)
  227. {
  228. hr = E_FAIL;
  229. }
  230. return hr;
  231. }
  232. //----------------------------------------------------------------------------
  233. // FinalRelease
  234. //----------------------------------------------------------------------------
  235. void CIManageDB::FinalRelease()
  236. {
  237. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  238. try
  239. {
  240. if (m_rsAccounts)
  241. {
  242. m_rsAccounts.Release();
  243. }
  244. if (m_pQueryMapping)
  245. {
  246. // we need to cleanup all the reportStruct objects.
  247. _variant_t var;
  248. reportStruct * pRs;
  249. // Cleanup the MigratedAccounts information
  250. var = m_pQueryMapping->get(L"MigratedAccounts.DispInfo");
  251. if ( var.vt == (VT_BYREF | VT_UI1) )
  252. {
  253. pRs = (reportStruct*) var.pbVal;
  254. delete pRs;
  255. }
  256. // Cleanup the MigratedComputers information
  257. var = m_pQueryMapping->get(L"MigratedComputers.DispInfo");
  258. if ( var.vt == (VT_BYREF | VT_UI1) )
  259. {
  260. pRs = (reportStruct*)var.pbVal;
  261. delete pRs;
  262. }
  263. // Cleanup the ExpiredComputers information
  264. var = m_pQueryMapping->get(L"ExpiredComputers.DispInfo");
  265. if ( var.vt == (VT_BYREF | VT_UI1) )
  266. {
  267. pRs = (reportStruct*)var.pbVal;
  268. delete pRs;
  269. }
  270. // Cleanup the AccountReferences information
  271. var = m_pQueryMapping->get(L"AccountReferences.DispInfo");
  272. if ( var.vt == (VT_BYREF | VT_UI1) )
  273. {
  274. pRs = (reportStruct*)var.pbVal;
  275. delete pRs;
  276. }
  277. // Cleanup the NameConflicts information
  278. var = m_pQueryMapping->get(L"NameConflicts.DispInfo");
  279. if ( var.vt == (VT_BYREF | VT_UI1) )
  280. {
  281. pRs = (reportStruct*)var.pbVal;
  282. delete pRs;
  283. }
  284. m_pQueryMapping.Release();
  285. }
  286. if (m_cn)
  287. {
  288. m_cn.Release();
  289. }
  290. }
  291. catch (...)
  292. {
  293. //eat it
  294. }
  295. }
  296. //---------------------------------------------------------------------------------------------
  297. // SetVarsetToDB : Saves a varset into the table identified as sTableName. ActionID is also
  298. // stored if one is provided.
  299. //---------------------------------------------------------------------------------------------
  300. STDMETHODIMP CIManageDB::SetVarsetToDB(IUnknown *pUnk, BSTR sTableName, VARIANT ActionID)
  301. {
  302. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  303. HRESULT hr = S_OK;
  304. try
  305. {
  306. IVarSetPtr pVSIn = pUnk;
  307. IVarSetPtr pVS(__uuidof(VarSet));
  308. IEnumVARIANTPtr varEnum;
  309. _bstr_t keyName;
  310. _variant_t value;
  311. _variant_t varKey;
  312. _variant_t vTable = sTableName;
  313. _variant_t vtConn;
  314. _variant_t varAction;
  315. DWORD nGot = 0;
  316. long lActionID;
  317. pVS->ImportSubTree(L"", pVSIn);
  318. ClipVarset(pVS);
  319. if (ActionID.vt == VT_I4)
  320. lActionID = ActionID.lVal;
  321. else
  322. lActionID = -1;
  323. // Open the recordset object.
  324. _RecordsetPtr rs(__uuidof(Recordset));
  325. rs->Open(vTable, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  326. // we are now going to enumerate through the varset and put the values into the DB
  327. // Get the IEnumVARIANT pointer to enumerate
  328. varEnum = pVS->_NewEnum;
  329. if (varEnum)
  330. {
  331. value.vt = VT_EMPTY;
  332. // For each value in the varset get the property name and put it into the
  333. // database with the string representation of its value with its type.
  334. while ( (hr = varEnum->Next(1,&varKey,&nGot)) == S_OK )
  335. {
  336. if ( nGot > 0 )
  337. {
  338. keyName = V_BSTR(&varKey);
  339. value = pVS->get(keyName);
  340. rs->AddNew();
  341. if ( lActionID > -1 )
  342. {
  343. // This is going to be actionID information
  344. // So lets put in the actionID in the database.
  345. varAction.vt = VT_I4;
  346. varAction.lVal = lActionID;
  347. rs->Fields->GetItem(L"ActionID")->Value = varAction;
  348. }
  349. rs->Fields->GetItem(L"Property")->Value = keyName;
  350. hr = PutVariantInDB(rs, value);
  351. rs->Update();
  352. if (FAILED(hr))
  353. _com_issue_errorex(hr, pVS, __uuidof(VarSet));
  354. }
  355. }
  356. varEnum.Release();
  357. }
  358. // Cleanup
  359. rs->Close();
  360. }
  361. catch (_com_error& ce)
  362. {
  363. hr = ce.Error();
  364. }
  365. catch (...)
  366. {
  367. hr = E_FAIL;
  368. }
  369. return hr;
  370. }
  371. //---------------------------------------------------------------------------------------------
  372. // PutVariantInDB : Stores a variant into a DB table by decoding it.
  373. //---------------------------------------------------------------------------------------------
  374. HRESULT CIManageDB::PutVariantInDB(_RecordsetPtr pRs, _variant_t val)
  375. {
  376. // This function puts the value passed as a variant into the current record of the recordset
  377. // It updates the VarType and the Value fields of the given property
  378. _variant_t varType; // Numeric value for the type of value
  379. _variant_t varVal; // String representation of the value field
  380. WCHAR strTemp[255];
  381. varType.vt = VT_UI4;
  382. varType.lVal = val.vt;
  383. switch ( val.vt )
  384. {
  385. case VT_BSTR : varVal = val;
  386. break;
  387. case VT_UI4 : wsprintf(strTemp, L"%d", val.lVal);
  388. varVal = strTemp;
  389. break;
  390. case VT_I4 : wsprintf(strTemp, L"%d", val.lVal);
  391. varVal = strTemp;
  392. break;
  393. case VT_EMPTY : break;
  394. case VT_NULL: break;
  395. default : MCSASSERT(FALSE); // What ever this type is we are not supporting it
  396. // so put the support in for this.
  397. return E_INVALIDARG;
  398. }
  399. pRs->Fields->GetItem(L"VarType")->Value = varType;
  400. pRs->Fields->GetItem(L"Value")->Value = varVal;
  401. return S_OK;
  402. }
  403. //---------------------------------------------------------------------------------------------
  404. // ClearTable : Deletes a table indicated by sTableName and applies a filter if provided.
  405. //---------------------------------------------------------------------------------------------
  406. STDMETHODIMP CIManageDB::ClearTable(BSTR sTableName, VARIANT Filter)
  407. {
  408. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  409. HRESULT hr = S_OK;
  410. try
  411. {
  412. // Build a SQL string to Clear the table.
  413. WCHAR sSQL[2000];
  414. WCHAR sFilter[2000];
  415. _variant_t varSQL;
  416. if (Filter.vt == VT_BSTR)
  417. wcscpy(sFilter, (WCHAR*)Filter.bstrVal);
  418. else
  419. wcscpy(sFilter, L"");
  420. wsprintf(sSQL, L"Delete from %s", sTableName);
  421. if ( wcslen(sFilter) > 0 )
  422. {
  423. wcscat(sSQL, L" where ");
  424. wcscat(sSQL, sFilter);
  425. }
  426. varSQL = sSQL;
  427. _RecordsetPtr pRs(__uuidof(Recordset));
  428. pRs->Open(varSQL, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  429. }
  430. catch (_com_error& ce)
  431. {
  432. hr = ce.Error();
  433. }
  434. catch (...)
  435. {
  436. hr = E_FAIL;
  437. }
  438. return hr;
  439. }
  440. //---------------------------------------------------------------------------------------------
  441. // SaveSettings : This method saves the GUI setting varset into the Settings table.
  442. //---------------------------------------------------------------------------------------------
  443. STDMETHODIMP CIManageDB::SaveSettings(IUnknown *pUnk)
  444. {
  445. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  446. HRESULT hr = S_OK;
  447. try
  448. {
  449. // This function uses the IUnknown pointer to the varset to save its
  450. // settings in the database in Settings table.
  451. // First clear the whole table
  452. CheckError(ClearTable(L"Settings"));
  453. // Update the values in the database.
  454. CheckError(SetVarsetToDB(pUnk, L"Settings"));
  455. }
  456. catch (_com_error& ce)
  457. {
  458. hr = ce.Error();
  459. }
  460. catch (...)
  461. {
  462. hr = E_FAIL;
  463. }
  464. return hr;
  465. }
  466. //---------------------------------------------------------------------------------------------
  467. // GetVarFromDB : Retrieves a variant from the DB table by encoding it.
  468. //---------------------------------------------------------------------------------------------
  469. HRESULT CIManageDB::GetVarFromDB(_RecordsetPtr pRec, _variant_t& val)
  470. {
  471. HRESULT hr = S_OK;
  472. try
  473. {
  474. // retrieve data type
  475. VARTYPE vt = VARTYPE(long(pRec->Fields->GetItem(L"VarType")->Value));
  476. // if data type is empty or null...
  477. if ((vt == VT_EMPTY) || (vt == VT_NULL))
  478. {
  479. // then clear value
  480. val.Clear();
  481. }
  482. else
  483. {
  484. // otherwise retrieve value and convert to given data type
  485. _variant_t vntValue = pRec->Fields->GetItem(L"Value")->Value;
  486. val.ChangeType(vt, &vntValue);
  487. }
  488. }
  489. catch (_com_error& ce)
  490. {
  491. hr = ce.Error();
  492. }
  493. catch (...)
  494. {
  495. hr = E_FAIL;
  496. }
  497. return hr;
  498. }
  499. //---------------------------------------------------------------------------------------------
  500. // GetVarsetFromDB : Retrieves a varset from the specified table. and fills the argument
  501. //---------------------------------------------------------------------------------------------
  502. STDMETHODIMP CIManageDB::GetVarsetFromDB(BSTR sTable, IUnknown **ppVarset, VARIANT ActionID)
  503. {
  504. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  505. HRESULT hr = S_OK;
  506. try
  507. {
  508. IVarSetPtr pVS = *ppVarset;
  509. _bstr_t sKeyName;
  510. _variant_t val;
  511. _variant_t varQuery;
  512. WCHAR sQuery[1000];
  513. long lActionID;
  514. if (ActionID.vt == VT_I4)
  515. lActionID = ActionID.lVal;
  516. else
  517. lActionID = -1;
  518. if ( lActionID == -1 )
  519. wsprintf(sQuery, L"Select * from %s", sTable);
  520. else
  521. wsprintf(sQuery, L"Select * from %s where ActionID = %d", sTable, lActionID);
  522. varQuery = sQuery;
  523. _RecordsetPtr pRs(__uuidof(Recordset));
  524. pRs->Open(varQuery, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  525. if (!pRs->EndOfFile)
  526. {
  527. pRs->MoveFirst();
  528. while ( !pRs->EndOfFile )
  529. {
  530. val = pRs->Fields->GetItem(L"Property")->Value;
  531. sKeyName = val.bstrVal;
  532. hr = GetVarFromDB(pRs, val);
  533. if ( FAILED(hr) )
  534. _com_issue_errorex(hr, pRs, __uuidof(_Recordset));
  535. pVS->put(sKeyName, val);
  536. pRs->MoveNext();
  537. }
  538. RestoreVarset(pVS);
  539. }
  540. }
  541. catch (_com_error& ce)
  542. {
  543. hr = ce.Error();
  544. }
  545. catch (...)
  546. {
  547. hr = E_FAIL;
  548. }
  549. return hr;
  550. }
  551. //---------------------------------------------------------------------------------------------
  552. // GetSettings : Retrieves the settings from the Settings table and fills up the varset
  553. //---------------------------------------------------------------------------------------------
  554. STDMETHODIMP CIManageDB::GetSettings(IUnknown **ppUnk)
  555. {
  556. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  557. // Get the varset from the Settings table and return it.
  558. return GetVarsetFromDB(L"Settings", ppUnk);
  559. }
  560. //---------------------------------------------------------------------------------------------
  561. // SetActionHistory : Saves action history information into the Action history table.
  562. //---------------------------------------------------------------------------------------------
  563. STDMETHODIMP CIManageDB::SetActionHistory(long lActionID, IUnknown *pUnk)
  564. {
  565. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  566. // Call the set varset method to set the values into the database.
  567. _variant_t ActionID = lActionID;
  568. SetVarsetToDB(pUnk, L"ActionHistory", ActionID);
  569. return S_OK;
  570. }
  571. //---------------------------------------------------------------------------------------------
  572. // GetActionHistory : Retrieves action history information into the varset
  573. //---------------------------------------------------------------------------------------------
  574. STDMETHODIMP CIManageDB::GetActionHistory(long lActionID, IUnknown **ppUnk)
  575. {
  576. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  577. // Get the varset from the database
  578. _variant_t ActionID = lActionID;
  579. // GetVarsetFromDB(L"ActionHistory", ppUnk, ActionID);
  580. // return S_OK;
  581. return GetVarsetFromDB(L"ActionHistory", ppUnk, ActionID);
  582. }
  583. //---------------------------------------------------------------------------------------------
  584. // GetNextActionID : Rotates the Action ID between 1 and MAXID as specified in the system table
  585. //---------------------------------------------------------------------------------------------
  586. STDMETHODIMP CIManageDB::GetNextActionID(long *pActionID)
  587. {
  588. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  589. HRESULT hr = S_OK;
  590. // We open the system table and look at the NextActionID field.
  591. // if the value of the NextActionID is greater than value in MaxID field
  592. // then we return the nextactionid = 1.
  593. try
  594. {
  595. _RecordsetPtr pRs(__uuidof(Recordset));
  596. _variant_t vtSource = L"System";
  597. _variant_t next, max, curr;
  598. WCHAR sActionID[LEN_Path];
  599. next.vt = VT_I4;
  600. max.vt = VT_I4;
  601. curr.vt = VT_I4;
  602. pRs->Filter = L"";
  603. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  604. if (pRs->GetRecordCount() > 0)
  605. {
  606. pRs->MoveFirst();
  607. next.lVal = pRs->Fields->GetItem(L"NextActionID")->Value;
  608. max.lVal = pRs->Fields->GetItem(L"MaxID")->Value;
  609. if ( next.lVal > max.lVal )
  610. next.lVal = 1;
  611. long currentID = next.lVal;
  612. *pActionID = currentID;
  613. curr.lVal = currentID;
  614. next.lVal++;
  615. pRs->Fields->GetItem(L"NextActionID")->Value = next;
  616. pRs->Fields->GetItem(L"CurrentActionID")->Value = curr;
  617. pRs->Update();
  618. // Delete all entries for this pirticular action.
  619. wsprintf(sActionID, L"ActionID=%d", currentID);
  620. _variant_t ActionID = sActionID;
  621. ClearTable(L"ActionHistory", ActionID);
  622. //TODO:: Add code to delete entries from any other tables if needed
  623. // Since we are deleting the actionID in the the ActionHistory table we can
  624. // not undo this stuff. But we still need to keep it around so that the report
  625. // and the GUI can work with it. I am going to set all actionIDs to -1 if actionID is
  626. // cleared
  627. SetActionIDInMigratedObjects(sActionID);
  628. }
  629. else
  630. {
  631. hr = S_FALSE;
  632. }
  633. }
  634. catch (_com_error& ce)
  635. {
  636. hr = ce.Error();
  637. }
  638. catch (...)
  639. {
  640. hr = E_FAIL;
  641. }
  642. return hr;
  643. }
  644. //---------------------------------------------------------------------------------------------
  645. // SaveMigratedObject : Saves information about a object that is migrated.
  646. //---------------------------------------------------------------------------------------------
  647. STDMETHODIMP CIManageDB::SaveMigratedObject(long lActionID, IUnknown *pUnk)
  648. {
  649. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  650. HRESULT hr = S_OK;
  651. // This fucntion updates the migrated objects table in the DB with the
  652. // information in the varset. If the information is not found in the Varset
  653. // then an error may occur.
  654. try
  655. {
  656. _variant_t var;
  657. time_t tm;
  658. COleDateTime dt(time(&tm));
  659. //dt= COleDateTime::GetCurrentTime();
  660. _RecordsetPtr pRs(__uuidof(Recordset));
  661. _variant_t vtSource;
  662. IVarSetPtr pVs = pUnk;
  663. WCHAR sQuery[LEN_Path];
  664. WCHAR sSource[LEN_Path], sTarget[LEN_Path], sDomain[LEN_Path];
  665. HRESULT hr = S_OK;
  666. bool bComp = false;
  667. WCHAR sTemp[LEN_Path];
  668. _bstr_t tempName;
  669. // Delete the record if one already exists in the table. In case it is remigrated/replaced.
  670. var = pVs->get(GET_BSTR(DB_SourceDomain));
  671. wcscpy(sSource, (WCHAR*)V_BSTR(&var));
  672. var = pVs->get(GET_BSTR(DB_TargetDomain));
  673. wcscpy(sTarget, (WCHAR*)V_BSTR(&var));
  674. var = pVs->get(GET_BSTR(DB_SourceSamName));
  675. wcscpy(sDomain, (WCHAR*)V_BSTR(&var));
  676. wsprintf(sQuery, L"delete from MigratedObjects where SourceDomain=\"%s\" and TargetDomain=\"%s\" and SourceSamName=\"%s\"",
  677. sSource, sTarget, sDomain);
  678. vtSource = _bstr_t(sQuery);
  679. hr = pRs->raw_Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  680. vtSource = L"MigratedObjects";
  681. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  682. pRs->AddNew();
  683. var.vt = VT_UI4;
  684. var.lVal = lActionID;
  685. pRs->Fields->GetItem(L"ActionID")->Value = var;
  686. pRs->Fields->GetItem(L"Time")->Value = DATE(dt);
  687. var = pVs->get(GET_BSTR(DB_SourceDomain));
  688. // make the string into an uppercase string.
  689. if ( var.vt == VT_BSTR )
  690. var.bstrVal = UStrUpr((WCHAR*) var.bstrVal);
  691. pRs->Fields->GetItem(L"SourceDomain")->Value = var;
  692. var = pVs->get(GET_BSTR(DB_TargetDomain));
  693. // make the string into an uppercase string.
  694. if ( var.vt == VT_BSTR )
  695. var.bstrVal = UStrUpr((WCHAR*) var.bstrVal);
  696. pRs->Fields->GetItem(L"TargetDomain")->Value = var;
  697. var = pVs->get(GET_BSTR(DB_SourceAdsPath));
  698. pRs->Fields->GetItem(L"SourceAdsPath")->Value = var;
  699. var = pVs->get(GET_BSTR(DB_TargetAdsPath));
  700. pRs->Fields->GetItem(L"TargetAdsPath")->Value = var;
  701. var = pVs->get(GET_BSTR(DB_status));
  702. pRs->Fields->GetItem(L"status")->Value = var;
  703. var = pVs->get(GET_BSTR(DB_SourceDomainSid));
  704. pRs->Fields->GetItem(L"SourceDomainSid")->Value = var;
  705. var = pVs->get(GET_BSTR(DB_Type));
  706. // make the string into an uppercase string.
  707. if ( var.vt == VT_BSTR )
  708. {
  709. var.bstrVal = UStrLwr((WCHAR*) var.bstrVal);
  710. if ( !_wcsicmp(L"computer", (WCHAR*) var.bstrVal) )
  711. bComp = true;
  712. else
  713. bComp = false;
  714. }
  715. pRs->Fields->GetItem(L"Type")->Value = var;
  716. var = pVs->get(GET_BSTR(DB_SourceSamName));
  717. // for computer accounts make sure the good old $ sign is there.
  718. if (bComp)
  719. {
  720. wcscpy(sTemp, (WCHAR*) var.bstrVal);
  721. if ( sTemp[wcslen(sTemp) - 1] != L'$' )
  722. {
  723. tempName = sTemp;
  724. tempName += L"$";
  725. var = tempName;
  726. }
  727. }
  728. pRs->Fields->GetItem(L"SourceSamName")->Value = var;
  729. var = pVs->get(GET_BSTR(DB_TargetSamName));
  730. // for computer accounts make sure the good old $ sign is there.
  731. if (bComp)
  732. {
  733. wcscpy(sTemp, (WCHAR*) var.bstrVal);
  734. if ( sTemp[wcslen(sTemp) - 1] != L'$' )
  735. {
  736. tempName = sTemp;
  737. tempName += L"$";
  738. var = tempName;
  739. }
  740. }
  741. pRs->Fields->GetItem(L"TargetSamName")->Value = var;
  742. var = pVs->get(GET_BSTR(DB_GUID));
  743. pRs->Fields->GetItem(L"GUID")->Value = var;
  744. var = pVs->get(GET_BSTR(DB_SourceRid));
  745. if ( var.vt == VT_UI4 || var.vt == VT_I4 )
  746. pRs->Fields->GetItem("SourceRid")->Value = var;
  747. else
  748. pRs->Fields->GetItem("SourceRid")->Value = _variant_t((long)0);
  749. var = pVs->get(GET_BSTR(DB_TargetRid));
  750. if ( var.vt == VT_UI4 || var.vt == VT_I4 )
  751. pRs->Fields->GetItem("TargetRid")->Value = var;
  752. else
  753. pRs->Fields->GetItem("TargetRid")->Value = _variant_t((long)0);
  754. pRs->Update();
  755. }
  756. catch (_com_error& ce)
  757. {
  758. hr = ce.Error();
  759. }
  760. catch (...)
  761. {
  762. hr = E_FAIL;
  763. }
  764. return hr;
  765. }
  766. //---------------------------------------------------------------------------------------------
  767. // GetMigratedObjects : Retrieves information about previously migrated objects withis a given
  768. // action or as a whole
  769. //---------------------------------------------------------------------------------------------
  770. STDMETHODIMP CIManageDB::GetMigratedObjects(long lActionID, IUnknown ** ppUnk)
  771. {
  772. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  773. HRESULT hr = S_OK;
  774. // This function returns all migrated objects and their information related
  775. // to a pirticular Action ID. This is going to return nothing if the actionID is
  776. // empty.
  777. try
  778. {
  779. _RecordsetPtr pRs(__uuidof(Recordset));
  780. _variant_t vtSource = L"MigratedObjects";
  781. IVarSetPtr pVs = *ppUnk;
  782. WCHAR sActionInfo[255];
  783. long lCnt = 0;
  784. if ( lActionID != -1 )
  785. {
  786. // If a valid ActionID is specified then we only return the data for that one.
  787. // but if -1 is passed in then we return all migrated objects.
  788. wsprintf(sActionInfo, L"ActionID=%d", lActionID);
  789. pRs->Filter = sActionInfo;
  790. }
  791. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  792. if (pRs->GetRecordCount() > 0)
  793. {
  794. pRs->MoveFirst();
  795. while ( !pRs->EndOfFile )
  796. {
  797. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_ActionID));
  798. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  799. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Time));
  800. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  801. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomain));
  802. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  803. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetDomain));
  804. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  805. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceAdsPath));
  806. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  807. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetAdsPath));
  808. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  809. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_status));
  810. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  811. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceSamName));
  812. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  813. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetSamName));
  814. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  815. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Type));
  816. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  817. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_GUID));
  818. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  819. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceRid));
  820. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  821. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetRid));
  822. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  823. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomainSid));
  824. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  825. pRs->MoveNext();
  826. lCnt++;
  827. }
  828. pVs->put(L"MigratedObjects", lCnt);
  829. }
  830. else
  831. {
  832. hr = S_FALSE;
  833. }
  834. }
  835. catch (_com_error& ce)
  836. {
  837. hr = ce.Error();
  838. }
  839. catch (...)
  840. {
  841. hr = E_FAIL;
  842. }
  843. return hr;
  844. }
  845. //---------------------------------------------------------------------------------------------
  846. // GetMigratedObjectsWithSSid : Retrieves information about previously migrated objects within
  847. // a given action or as a whole with a valid Source Domain Sid
  848. //---------------------------------------------------------------------------------------------
  849. STDMETHODIMP CIManageDB::GetMigratedObjectsWithSSid(long lActionID, IUnknown ** ppUnk)
  850. {
  851. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  852. HRESULT hr = S_OK;
  853. // This function returns all migrated objects and their information related
  854. // to a pirticular Action ID. This is going to return nothing if the actionID is
  855. // empty.
  856. try
  857. {
  858. _RecordsetPtr pRs(__uuidof(Recordset));
  859. _variant_t vtSource = L"MigratedObjects";
  860. IVarSetPtr pVs = *ppUnk;
  861. WCHAR sActionInfo[255];
  862. long lCnt = 0;
  863. if ( lActionID != -1 )
  864. {
  865. // If a valid ActionID is specified then we only return the data for that one.
  866. // but if -1 is passed in then we return all migrated objects.
  867. wsprintf(sActionInfo, L"ActionID=%d", lActionID);
  868. pRs->Filter = sActionInfo;
  869. }
  870. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomainSid IS NOT NULL");
  871. vtSource = sActionInfo;
  872. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  873. if (pRs->GetRecordCount() > 0)
  874. {
  875. pRs->MoveFirst();
  876. while ( !pRs->EndOfFile )
  877. {
  878. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_ActionID));
  879. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  880. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Time));
  881. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  882. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomain));
  883. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  884. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetDomain));
  885. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  886. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceAdsPath));
  887. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  888. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetAdsPath));
  889. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  890. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_status));
  891. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  892. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceSamName));
  893. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  894. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetSamName));
  895. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  896. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Type));
  897. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  898. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_GUID));
  899. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  900. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceRid));
  901. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  902. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetRid));
  903. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  904. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomainSid));
  905. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  906. pRs->MoveNext();
  907. lCnt++;
  908. }
  909. pVs->put(L"MigratedObjects", lCnt);
  910. }
  911. else
  912. {
  913. hr = S_FALSE;
  914. }
  915. }
  916. catch (_com_error& ce)
  917. {
  918. hr = ce.Error();
  919. }
  920. catch (...)
  921. {
  922. hr = E_FAIL;
  923. }
  924. return hr;
  925. }
  926. //---------------------------------------------------------------------------------------------
  927. // SetActionIDInMigratedObjects : For a discarded actionID sets its ActionID to -1 in MO table.
  928. //---------------------------------------------------------------------------------------------
  929. void CIManageDB::SetActionIDInMigratedObjects(_bstr_t sFilter)
  930. {
  931. _bstr_t sQuery = _bstr_t(L"Update MigratedObjects Set ActionID = -1 where ") + sFilter;
  932. _variant_t vt = sQuery;
  933. try
  934. {
  935. _RecordsetPtr pRs(__uuidof(Recordset));
  936. pRs->Open(vt, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  937. }
  938. catch (...)
  939. {
  940. ;
  941. }
  942. }
  943. //---------------------------------------------------------------------------------------------
  944. // GetRSForReport : Returns a recordset for a given report.
  945. //---------------------------------------------------------------------------------------------
  946. STDMETHODIMP CIManageDB::GetRSForReport(BSTR sReport, IUnknown **pprsData)
  947. {
  948. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  949. HRESULT hr = S_OK;
  950. try
  951. {
  952. // For a given report we have a mapping in the varset. We can get the query
  953. // from that varset and execute it and return the varset.
  954. _variant_t var = m_pQueryMapping->get(sReport);
  955. if ( var.vt == VT_BSTR )
  956. {
  957. _RecordsetPtr pRs(__uuidof(Recordset));
  958. pRs->Open(var, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  959. // Now that we have the recordset pointer we can get IUnknown pointer to it and return that
  960. *pprsData = IUnknownPtr(pRs).Detach();
  961. }
  962. else
  963. {
  964. hr = E_NOTIMPL;
  965. }
  966. }
  967. catch (_com_error& ce)
  968. {
  969. hr = ce.Error();
  970. }
  971. catch (...)
  972. {
  973. hr = E_FAIL;
  974. }
  975. return hr;
  976. }
  977. //---------------------------------------------------------------------------------------------
  978. //
  979. //---------------------------------------------------------------------------------------------
  980. STDMETHODIMP CIManageDB::SaveSCMPasswords(IUnknown *pUnk)
  981. {
  982. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  983. return SetVarsetToDB(pUnk, L"SCMPasswords");
  984. }
  985. //---------------------------------------------------------------------------------------------
  986. //
  987. //---------------------------------------------------------------------------------------------
  988. STDMETHODIMP CIManageDB::GetSCMPasswords(IUnknown **ppUnk)
  989. {
  990. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  991. return GetVarsetFromDB(L"SCMPasswords", ppUnk);
  992. }
  993. //---------------------------------------------------------------------------------------------
  994. //
  995. //---------------------------------------------------------------------------------------------
  996. STDMETHODIMP CIManageDB::ClearSCMPasswords()
  997. {
  998. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  999. ClearTable(L"SCMPasswords");
  1000. return S_OK;
  1001. }
  1002. //---------------------------------------------------------------------------------------------
  1003. // GetCurrentActionID : Retrieves the actionID currently in use.
  1004. //---------------------------------------------------------------------------------------------
  1005. STDMETHODIMP CIManageDB::GetCurrentActionID(long *pActionID)
  1006. {
  1007. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1008. HRESULT hr = S_OK;
  1009. try
  1010. {
  1011. _RecordsetPtr pRs(__uuidof(Recordset));
  1012. _variant_t vtSource = L"System";
  1013. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  1014. if (pRs->GetRecordCount() > 0)
  1015. {
  1016. pRs->MoveFirst();
  1017. *pActionID = pRs->Fields->GetItem(L"CurrentActionID")->Value;
  1018. }
  1019. else
  1020. {
  1021. hr = S_FALSE;
  1022. }
  1023. }
  1024. catch (_com_error& ce)
  1025. {
  1026. hr = ce.Error();
  1027. }
  1028. catch (...)
  1029. {
  1030. hr = E_FAIL;
  1031. }
  1032. return hr;
  1033. }
  1034. //---------------------------------------------------------------------------------------------
  1035. // GetAMigratedObject : Given the source name, and the domain information retrieves info about
  1036. // a previous migration.
  1037. //---------------------------------------------------------------------------------------------
  1038. STDMETHODIMP CIManageDB::GetAMigratedObject(BSTR sSrcSamName, BSTR sSrcDomain, BSTR sTgtDomain, IUnknown **ppUnk)
  1039. {
  1040. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1041. HRESULT hr = S_OK;
  1042. try
  1043. {
  1044. _RecordsetPtr pRs(__uuidof(Recordset));
  1045. _variant_t vtSource = L"MigratedObjects";
  1046. IVarSetPtr pVs = *ppUnk;
  1047. WCHAR sActionInfo[LEN_Path];
  1048. long lCnt = 0;
  1049. _bstr_t sName;
  1050. // If the parameters are not correct then we need to return an error
  1051. if ( (wcslen(sSrcSamName) == 0) || (wcslen(sSrcDomain) == 0))
  1052. _com_issue_error(E_INVALIDARG);
  1053. /* wsprintf(sActionInfo, L"SourceDomain=\"%s\" AND SourceSamName=\"%s\" AND TargetDomain=\"%s\"", sSrcDomain, sSrcSamName, sTgtDomain);
  1054. pRs->Filter = sActionInfo;
  1055. */
  1056. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomain=\"%s\" AND SourceSamName=\"%s\" AND TargetDomain=\"%s\"", sSrcDomain, sSrcSamName, sTgtDomain);
  1057. vtSource = sActionInfo;
  1058. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1059. if (pRs->GetRecordCount() > 0)
  1060. {
  1061. // We want the latest move.
  1062. pRs->MoveLast();
  1063. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_ActionID));
  1064. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  1065. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Time));
  1066. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  1067. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomain));
  1068. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  1069. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetDomain));
  1070. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  1071. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceAdsPath));
  1072. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  1073. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetAdsPath));
  1074. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  1075. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_status));
  1076. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  1077. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceSamName));
  1078. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  1079. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetSamName));
  1080. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  1081. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Type));
  1082. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  1083. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_GUID));
  1084. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  1085. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceRid));
  1086. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  1087. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetRid));
  1088. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  1089. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomainSid));
  1090. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  1091. }
  1092. else
  1093. {
  1094. hr = S_FALSE;
  1095. }
  1096. }
  1097. catch (_com_error& ce)
  1098. {
  1099. hr = ce.Error();
  1100. }
  1101. catch (...)
  1102. {
  1103. hr = E_FAIL;
  1104. }
  1105. return hr;
  1106. }
  1107. //---------------------------------------------------------------------------------------------
  1108. // GetAMigratedObjectToAnyDomain : Given the source name, and the domain information retrieves info about
  1109. // a previous migration.
  1110. //---------------------------------------------------------------------------------------------
  1111. STDMETHODIMP CIManageDB::GetAMigratedObjectToAnyDomain(BSTR sSrcSamName, BSTR sSrcDomain, IUnknown **ppUnk)
  1112. {
  1113. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1114. HRESULT hr = S_OK;
  1115. try
  1116. {
  1117. _RecordsetPtr pRs(__uuidof(Recordset));
  1118. _variant_t vtSource = L"MigratedObjects";
  1119. IVarSetPtr pVs = *ppUnk;
  1120. WCHAR sActionInfo[LEN_Path];
  1121. long lCnt = 0;
  1122. _bstr_t sName;
  1123. // If the parameters are not correct then we need to return an error
  1124. if ( (wcslen(sSrcSamName) == 0) || (wcslen(sSrcDomain) == 0))
  1125. _com_issue_error(E_INVALIDARG);
  1126. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomain=\"%s\" AND SourceSamName=\"%s\" Order by Time", sSrcDomain, sSrcSamName);
  1127. // pRs->Filter = sActionInfo;
  1128. // wcscpy(sActionInfo, L"Time");
  1129. // pRs->Sort = sActionInfo;
  1130. vtSource = _bstr_t(sActionInfo);
  1131. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1132. if (pRs->GetRecordCount() > 0)
  1133. {
  1134. // We want the latest move.
  1135. pRs->MoveLast();
  1136. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_ActionID));
  1137. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  1138. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Time));
  1139. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  1140. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomain));
  1141. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  1142. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetDomain));
  1143. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  1144. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceAdsPath));
  1145. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  1146. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetAdsPath));
  1147. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  1148. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_status));
  1149. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  1150. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceSamName));
  1151. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  1152. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetSamName));
  1153. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  1154. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Type));
  1155. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  1156. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_GUID));
  1157. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  1158. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceRid));
  1159. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  1160. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetRid));
  1161. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  1162. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomainSid));
  1163. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  1164. }
  1165. else
  1166. {
  1167. hr = S_FALSE;
  1168. }
  1169. }
  1170. catch (_com_error& ce)
  1171. {
  1172. hr = ce.Error();
  1173. }
  1174. catch (...)
  1175. {
  1176. hr = E_FAIL;
  1177. }
  1178. return hr;
  1179. }
  1180. //---------------------------------------------------------------------------------------------
  1181. // GenerateReport Generates an HTML report for the given Query and saves it in the File.
  1182. //---------------------------------------------------------------------------------------------
  1183. STDMETHODIMP CIManageDB::GenerateReport(BSTR sReportName, BSTR sFileName, BSTR sSrcDomain, BSTR sTgtDomain, LONG bSourceNT4)
  1184. {
  1185. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1186. HRESULT hr = S_OK;
  1187. FILE* logFile = NULL;
  1188. try
  1189. {
  1190. _RecordsetPtr pRs;
  1191. IUnknownPtr pUnk;
  1192. _variant_t var;
  1193. WCHAR sKey[LEN_Path];
  1194. CString reportingTitle;
  1195. CString srcDm = (WCHAR*) sSrcDomain;
  1196. CString tgtDm = (WCHAR*) sTgtDomain;
  1197. //convert source and target domain names, only used in the name conflict report,
  1198. //to uppercase
  1199. srcDm.MakeUpper();
  1200. tgtDm.MakeUpper();
  1201. CheckError(GetRSForReport(sReportName, &pUnk));
  1202. pRs = pUnk;
  1203. // Now that we have the recordset we need to get the number of columns
  1204. int numFields = pRs->Fields->Count;
  1205. int size = 100 / numFields;
  1206. reportingTitle.LoadString(IDS_ReportingTitle);
  1207. // Open the html file to write to
  1208. logFile = fopen(_bstr_t(sFileName), "wb");
  1209. if ( !logFile )
  1210. _com_issue_error(HRESULT_FROM_WIN32(GetLastError())); //TODO: stream i/o doesn't set last error
  1211. //Put the header information into the File.
  1212. fputs("<HTML>\r\n", logFile);
  1213. fputs("<HEAD>\r\n", logFile);
  1214. fputs("<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; CHARSET=utf-8\">\r\n", logFile);
  1215. fprintf(logFile, "<TITLE>%s</TITLE>\r\n", WTUTF8(reportingTitle.GetBuffer(0)));
  1216. fputs("</HEAD>\r\n", logFile);
  1217. fputs("<BODY TEXT=\"#000000\" BGCOLOR=\"#ffffff\">\r\n", logFile);
  1218. fprintf(logFile, "<B><FONT SIZE=5><P ALIGN=\"CENTER\">%s</P>\r\n", WTUTF8(reportingTitle.GetBuffer(0)));
  1219. // Get the display information for the report
  1220. // I know I did not need to do all this elaborate setup to get the fieldnames and the report names
  1221. // I could have gotten this information dynamically but had to change it because we need to get the
  1222. // info from the Res dll for internationalization.
  1223. wsprintf(sKey, L"%s.DispInfo", (WCHAR*) sReportName);
  1224. _variant_t v1;
  1225. reportStruct * prs;
  1226. v1 = m_pQueryMapping->get(sKey);
  1227. prs = (reportStruct *) v1.pbVal;
  1228. VariantInit(&v1);
  1229. fprintf(logFile, "</FONT><FONT SIZE=4><P ALIGN=\"CENTER\">%s</P>\r\n", WTUTF8(prs->sReportName));
  1230. fputs("<P ALIGN=\"CENTER\"><CENTER><TABLE WIDTH=90%%>\r\n", logFile);
  1231. fputs("<TR>\r\n", logFile);
  1232. for (int i = 0; i < numFields; i++)
  1233. {
  1234. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" BGCOLOR=\"#000080\">\r\n", prs->arReportSize[i]);
  1235. //if Canonical Name column, left align text since the name can be really long
  1236. if (i==5)
  1237. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#00ff00\"><P ALIGN=\"LEFT\">%s</B></FONT></TD>\r\n", WTUTF8(prs->arReportFields[i]));
  1238. else
  1239. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#00ff00\"><P ALIGN=\"CENTER\">%s</B></FONT></TD>\r\n", WTUTF8(prs->arReportFields[i]));
  1240. }
  1241. fputs("</TR>\r\n", logFile);
  1242. //if name conflict report, add domains to the top of the report
  1243. if (wcscmp((WCHAR*) sReportName, L"NameConflicts") == 0)
  1244. {
  1245. fputs("</TR>\r\n", logFile);
  1246. //add "Source Domain ="
  1247. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[0]);
  1248. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</B></FONT></TD>\r\n", WTUTF8(GET_STRING(IDS_TABLE_NC_SDOMAIN)));
  1249. //add %SourceDomainName%
  1250. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[1]);
  1251. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\"> = %s</B></FONT></TD>\r\n", WTUTF8(LPCTSTR(srcDm)));
  1252. fputs("<TD>\r\n", logFile);
  1253. fputs("<TD>\r\n", logFile);
  1254. //add "Target Domain ="
  1255. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[4]);
  1256. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</B></FONT></TD>\r\n", WTUTF8(GET_STRING(IDS_TABLE_NC_TDOMAIN)));
  1257. //add %TargetDomainName%
  1258. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[5]);
  1259. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\"> = %s</B></FONT></TD>\r\n", WTUTF8(LPCTSTR(tgtDm)));
  1260. fputs("</TR>\r\n", logFile);
  1261. }
  1262. //write Account Reference report here since we need to build lists and
  1263. //categorize
  1264. if (wcscmp((WCHAR*) sReportName, L"AccountReferences") == 0)
  1265. {
  1266. CStringList inMotList;
  1267. CString accountName;
  1268. CString domainName;
  1269. CString listName;
  1270. POSITION currentPos;
  1271. //add "Migrated by ADMT" as section header for Account Reference report
  1272. fputs("</TR>\r\n", logFile);
  1273. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[0]);
  1274. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</B></FONT></TD>\r\n", WTUTF8(GET_STRING(IDS_TABLE_AR_MOT_HDR)));
  1275. fputs("</TR>\r\n", logFile);
  1276. //look at each entry in the recordset and add the the migrated list if it was
  1277. //migrated and in the MOT
  1278. while ( !pRs->EndOfFile )
  1279. {
  1280. //retrieve the domain and account name for this entry
  1281. var = pRs->Fields->Item[(long)0]->GetValue();
  1282. domainName = (WCHAR*)V_BSTR(&var);
  1283. var = pRs->Fields->Item[(long)1]->GetValue();
  1284. accountName = (WCHAR*)V_BSTR(&var);
  1285. //see if this account is in the Migrated Objects table
  1286. IVarSetPtr pVsMot(__uuidof(VarSet));
  1287. IUnknown * pMotUnk;
  1288. pVsMot->QueryInterface(IID_IUnknown, (void**) &pMotUnk);
  1289. HRESULT hrFind = GetAMigratedObjectToAnyDomain(accountName.AllocSysString(),
  1290. domainName.AllocSysString(), &pMotUnk);
  1291. pMotUnk->Release();
  1292. //if this entry was in the MOT, save in the list
  1293. if ( hrFind == S_OK )
  1294. {
  1295. //list stores the account in the form domain\account
  1296. listName = domainName;
  1297. listName += L"\\";
  1298. listName += accountName;
  1299. //add the name to the list, if not already in it
  1300. currentPos = inMotList.Find(listName);
  1301. if (currentPos == NULL)
  1302. inMotList.AddTail(listName);
  1303. }
  1304. pRs->MoveNext();
  1305. }//end while build MOT list
  1306. //go back to the top of the recordset and print each entry that is in the
  1307. //list created above
  1308. pRs->MoveFirst();
  1309. while ( !pRs->EndOfFile )
  1310. {
  1311. BOOL bInList = FALSE;
  1312. //retrieve the domain and account name for this entry
  1313. var = pRs->Fields->Item[(long)0]->GetValue();
  1314. domainName = (WCHAR*)V_BSTR(&var);
  1315. var = pRs->Fields->Item[(long)1]->GetValue();
  1316. accountName = (WCHAR*)V_BSTR(&var);
  1317. //list stored the accounts in the form domain\account
  1318. listName = domainName;
  1319. listName += L"\\";
  1320. listName += accountName;
  1321. //see if this entry name is in the list, if so, print it
  1322. if (inMotList.Find(listName) != NULL)
  1323. {
  1324. fputs("<TR>\r\n", logFile);
  1325. for (int i = 0; i < numFields; i++)
  1326. {
  1327. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[i]);
  1328. var = pRs->Fields->Item[(long) i]->GetValue();
  1329. if ( var.vt == VT_BSTR )
  1330. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(V_BSTR(&var)));
  1331. else
  1332. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"CENTER\">%d</FONT></TD>\r\n", var.lVal);
  1333. }
  1334. fputs("</TR>\r\n", logFile);
  1335. }//end if in list and need to print
  1336. pRs->MoveNext();
  1337. }//end while print those in MOT
  1338. //add "Not Migrated by ADMT" as section header for Account Reference report
  1339. fputs("</TR>\r\n", logFile);
  1340. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[0]);
  1341. fprintf(logFile, "<B><FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</B></FONT></TD>\r\n", WTUTF8(GET_STRING(IDS_TABLE_AR_NOTMOT_HDR)));
  1342. fputs("</TR>\r\n", logFile);
  1343. //go back to the top of the recordset and print each entry that is NOT
  1344. //in the list created above
  1345. pRs->MoveFirst();
  1346. while ( !pRs->EndOfFile )
  1347. {
  1348. BOOL bInList = FALSE;
  1349. //retrieve the domain and account name for this entry
  1350. var = pRs->Fields->Item[(long)0]->GetValue();
  1351. domainName = (WCHAR*)V_BSTR(&var);
  1352. var = pRs->Fields->Item[(long)1]->GetValue();
  1353. accountName = (WCHAR*)V_BSTR(&var);
  1354. //list stored the accounts in the form domain\account
  1355. listName = domainName;
  1356. listName += L"\\";
  1357. listName += accountName;
  1358. //see if this entry name is in the list, if not, print it
  1359. if (inMotList.Find(listName) == NULL)
  1360. {
  1361. fputs("<TR>\r\n", logFile);
  1362. for (int i = 0; i < numFields; i++)
  1363. {
  1364. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[i]);
  1365. var = pRs->Fields->Item[(long) i]->GetValue();
  1366. if ( var.vt == VT_BSTR )
  1367. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(V_BSTR(&var)));
  1368. else
  1369. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"CENTER\">%d</FONT></TD>\r\n", var.lVal);
  1370. }
  1371. fputs("</TR>\r\n", logFile);
  1372. }//end if NOT in list and need to print
  1373. pRs->MoveNext();
  1374. }//end while print those NOT in Mot
  1375. inMotList.RemoveAll(); //free the list
  1376. }//end if Account Ref report
  1377. while ((!pRs->EndOfFile) && (wcscmp((WCHAR*) sReportName, L"AccountReferences")))
  1378. {
  1379. fputs("<TR>\r\n", logFile);
  1380. for (int i = 0; i < numFields; i++)
  1381. {
  1382. bool bTranslateType = false;
  1383. bool bHideRDN = false;
  1384. fprintf(logFile, "<TD WIDTH=\"%d%%\" VALIGN=\"TOP\" >\r\n", prs->arReportSize[i]);
  1385. var = pRs->Fields->Item[(long) i]->GetValue();
  1386. if ( var.vt == VT_BSTR )
  1387. {
  1388. //set flag for translating type fields to localizable strings
  1389. if ((!wcscmp((WCHAR*) sReportName, L"NameConflicts")) && ((i==2) || (i==3)))
  1390. bTranslateType = true;
  1391. if ((!wcscmp((WCHAR*) sReportName, L"MigratedComputers")) && (i==2))
  1392. bTranslateType = true;
  1393. if ((!wcscmp((WCHAR*) sReportName, L"MigratedAccounts")) && (i==2))
  1394. bTranslateType = true;
  1395. //clear flag for not displaying RDN for NT 4.0 Source domains
  1396. if ((!wcscmp((WCHAR*) sReportName, L"NameConflicts")) && (i==1) && bSourceNT4)
  1397. bHideRDN = true;
  1398. if (bTranslateType)
  1399. {
  1400. //convert type from English only to a localizable string
  1401. CString atype;
  1402. if (!_wcsicmp((WCHAR*)V_BSTR(&var), L"user"))
  1403. atype = GET_STRING(IDS_TypeUser);
  1404. else if (!_wcsicmp((WCHAR*)V_BSTR(&var), L"group"))
  1405. atype = GET_STRING(IDS_TypeGroup);
  1406. else if (!_wcsicmp((WCHAR*)V_BSTR(&var), L"computer"))
  1407. atype = GET_STRING(IDS_TypeComputer);
  1408. else
  1409. atype = GET_STRING(IDS_TypeUnknown);
  1410. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(LPCTSTR(atype)));
  1411. }
  1412. //replace hard-coded "days" with a localizable string
  1413. else if((!wcscmp((WCHAR*) sReportName, L"ExpiredComputers")) && (i==4))
  1414. {
  1415. CString apwdage;
  1416. WCHAR * ndx;
  1417. if ((ndx = wcsstr((WCHAR*)V_BSTR(&var), L"days")) != NULL)
  1418. {
  1419. *ndx = L'\0';
  1420. apwdage = (WCHAR*)V_BSTR(&var);
  1421. apwdage += GET_STRING(IDS_PwdAgeDays);
  1422. }
  1423. else
  1424. apwdage = (WCHAR*)V_BSTR(&var);
  1425. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(LPCTSTR(apwdage)));
  1426. }
  1427. //else if NT 4.0 Source do not show our fabricated RDN
  1428. else if (bHideRDN)
  1429. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(L""));
  1430. else
  1431. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"LEFT\">%s</FONT></TD>\r\n", WTUTF8(V_BSTR(&var)));
  1432. }
  1433. else
  1434. if ( var.vt == VT_DATE )
  1435. {
  1436. _variant_t v1;
  1437. VariantChangeType(&v1, &var, VARIANT_NOVALUEPROP, VT_BSTR);
  1438. WCHAR sMsg[LEN_Path];
  1439. wcscpy(sMsg, (WCHAR*) V_BSTR(&v1));
  1440. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"CENTER\">%s</FONT></TD>\r\n", WTUTF8(LPCTSTR(sMsg)));
  1441. }
  1442. else
  1443. {
  1444. //TODO :: The types need more work
  1445. fprintf(logFile, "<FONT SIZE=3 COLOR=\"#000000\"><P ALIGN=\"CENTER\">%d</FONT></TD>\r\n", var.lVal);
  1446. }
  1447. }
  1448. fputs("</TR>\r\n", logFile);
  1449. pRs->MoveNext();
  1450. }
  1451. fputs("</TABLE>\r\n", logFile);
  1452. fputs("</CENTER></P>\r\n", logFile);
  1453. fputs("<B><FONT SIZE=5><P ALIGN=\"CENTER\"></P></B></FONT></BODY>\r\n", logFile);
  1454. fputs("</HTML>\r\n", logFile);
  1455. }
  1456. catch (_com_error& ce)
  1457. {
  1458. hr = ce.Error();
  1459. }
  1460. catch (...)
  1461. {
  1462. hr = E_FAIL;
  1463. }
  1464. if (logFile)
  1465. {
  1466. fclose(logFile);
  1467. }
  1468. return hr;
  1469. }
  1470. //---------------------------------------------------------------------------------------------
  1471. // AddDistributedAction : Adds a distributed action record to the DistributedAction table.
  1472. //---------------------------------------------------------------------------------------------
  1473. STDMETHODIMP CIManageDB::AddDistributedAction(BSTR sServerName, BSTR sResultFile, long lStatus, BSTR sText)
  1474. {
  1475. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1476. HRESULT hr = S_OK;
  1477. try
  1478. {
  1479. // Get the current action ID.
  1480. long lActionID;
  1481. CheckError(GetCurrentActionID(&lActionID));
  1482. _RecordsetPtr pRs(__uuidof(Recordset));
  1483. _variant_t vtSource = L"DistributedAction";
  1484. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  1485. pRs->AddNew();
  1486. pRs->Fields->GetItem(L"ActionID")->Value = lActionID;
  1487. pRs->Fields->GetItem(L"ServerName")->Value = sServerName;
  1488. pRs->Fields->GetItem(L"ResultFile")->Value = sResultFile;
  1489. pRs->Fields->GetItem(L"Status")->Value = lStatus;
  1490. pRs->Fields->GetItem(L"StatusText")->Value = sText;
  1491. pRs->Update();
  1492. pRs->Close();
  1493. }
  1494. catch (_com_error& ce)
  1495. {
  1496. hr = ce.Error();
  1497. }
  1498. catch (...)
  1499. {
  1500. hr = E_FAIL;
  1501. }
  1502. return hr;
  1503. }
  1504. //---------------------------------------------------------------------------------------------
  1505. // GetFailedDistributedActions : Returns all the failed distributed action
  1506. //---------------------------------------------------------------------------------------------
  1507. STDMETHODIMP CIManageDB::GetFailedDistributedActions(long lActionID, IUnknown ** pUnk)
  1508. {
  1509. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1510. HRESULT hr = S_OK;
  1511. try
  1512. {
  1513. IVarSetPtr pVs = * pUnk;
  1514. WCHAR sQuery[LEN_Path];
  1515. int nCnt = 0, nCntActionID = 1;
  1516. WCHAR sKey[LEN_Path];
  1517. _variant_t var;
  1518. // The failed action has the 0x80000000 bit set so we check for that (2147483648)
  1519. if ( lActionID == -1 )
  1520. wcscpy(sQuery, L"Select * from DistributedAction where status < 0");
  1521. else
  1522. wsprintf(sQuery, L"Select * from DistributedAction where ActionID=%d and status < 0", lActionID);
  1523. _variant_t vtSource = _bstr_t(sQuery);
  1524. _RecordsetPtr pRs(__uuidof(Recordset));
  1525. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1526. while (!pRs->EndOfFile)
  1527. {
  1528. wsprintf(sKey, L"DA.%d.ActionID", nCnt);
  1529. pVs->put(sKey, pRs->Fields->GetItem(L"ActionID")->Value);
  1530. wsprintf(sKey, L"DA.%d.Server", nCnt);
  1531. pVs->put(sKey, pRs->Fields->GetItem(L"ServerName")->Value);
  1532. wsprintf(sKey, L"DA.%d.Status", nCnt);
  1533. pVs->put(sKey, pRs->Fields->GetItem(L"Status")->Value);
  1534. wsprintf(sKey, L"DA.%d.JobFile", nCnt);
  1535. pVs->put(sKey, pRs->Fields->GetItem(L"ResultFile")->Value);
  1536. wsprintf(sKey, L"DA.%d.StatusText", nCnt);
  1537. pVs->put(sKey, pRs->Fields->GetItem(L"StatusText")->Value);
  1538. nCnt++;
  1539. pRs->MoveNext();
  1540. }
  1541. pVs->put(L"DA", (long) nCnt);
  1542. }
  1543. catch (_com_error& ce)
  1544. {
  1545. hr = ce.Error();
  1546. }
  1547. catch (...)
  1548. {
  1549. hr = E_FAIL;
  1550. }
  1551. return hr;
  1552. }
  1553. //---------------------------------------------------------------------------------------------
  1554. // SetServiceAccount : This method is saves the account info for the Service on a pirticular
  1555. // machine.
  1556. //---------------------------------------------------------------------------------------------
  1557. STDMETHODIMP CIManageDB::SetServiceAccount(
  1558. BSTR System, //in- System name
  1559. BSTR Service, //in- Service name
  1560. BSTR ServiceDisplayName, // in - Display name for service
  1561. BSTR Account //in- Account used by this service
  1562. )
  1563. {
  1564. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1565. HRESULT hr = S_OK;
  1566. try
  1567. {
  1568. // Create a new record and save the information
  1569. _variant_t var;
  1570. WCHAR sFilter[LEN_Path];
  1571. wsprintf(sFilter, L"System = \"%s\" and Service = \"%s\"", System, Service);
  1572. var = sFilter;
  1573. ClearTable(L"ServiceAccounts", var);
  1574. _RecordsetPtr pRs(__uuidof(Recordset));
  1575. _variant_t vtSource = L"ServiceAccounts";
  1576. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  1577. pRs->AddNew();
  1578. var = _bstr_t(System);
  1579. pRs->Fields->GetItem(L"System")->Value = var;
  1580. var = _bstr_t(Service);
  1581. pRs->Fields->GetItem(L"Service")->Value = var;
  1582. var = _bstr_t(ServiceDisplayName);
  1583. pRs->Fields->GetItem(L"ServiceDisplayName")->Value = var;
  1584. var = _bstr_t(Account);
  1585. pRs->Fields->GetItem(L"Account")->Value = var;
  1586. pRs->Update();
  1587. }
  1588. catch (_com_error& ce)
  1589. {
  1590. hr = ce.Error();
  1591. }
  1592. catch (...)
  1593. {
  1594. hr = E_FAIL;
  1595. }
  1596. return hr;
  1597. }
  1598. //---------------------------------------------------------------------------------------------
  1599. // GetServiceAccount : This method gets all the Services referencing the Account specified. The
  1600. // values are returned in System.Service format in the VarSet.
  1601. //---------------------------------------------------------------------------------------------
  1602. STDMETHODIMP CIManageDB::GetServiceAccount(
  1603. BSTR Account, //in- The account to lookup
  1604. IUnknown ** pUnk //out-Varset containing Services
  1605. )
  1606. {
  1607. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1608. HRESULT hr = S_OK;
  1609. try
  1610. {
  1611. IVarSetPtr pVs = * pUnk;
  1612. _bstr_t sQuery;
  1613. _bstr_t sKey;
  1614. WCHAR key[500];
  1615. _variant_t var;
  1616. long ndx = 0;
  1617. _RecordsetPtr pRs(__uuidof(Recordset));
  1618. // Set up the query to lookup a pirticular account or all accounts
  1619. if ( wcslen((WCHAR*)Account) == 0 )
  1620. sQuery = _bstr_t(L"Select * from ServiceAccounts order by System, Service");
  1621. else
  1622. sQuery = _bstr_t(L"Select * from ServiceAccounts where Account = \"") + _bstr_t(Account) + _bstr_t(L"\" order by System, Service");
  1623. var = sQuery;
  1624. // Get the data, Setup the varset and then return the info
  1625. pRs->Open(var, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1626. while (!pRs->EndOfFile)
  1627. {
  1628. // computer name
  1629. swprintf(key,L"Computer.%ld",ndx);
  1630. var = pRs->Fields->GetItem("System")->Value;
  1631. pVs->put(key,var);
  1632. // service name
  1633. swprintf(key,L"Service.%ld",ndx);
  1634. var = pRs->Fields->GetItem("Service")->Value;
  1635. pVs->put(key,var);
  1636. swprintf(key,L"ServiceDisplayName.%ld",ndx);
  1637. var = pRs->Fields->GetItem("ServiceDisplayName")->Value;
  1638. pVs->put(key,var);
  1639. // account name
  1640. swprintf(key,L"ServiceAccount.%ld",ndx);
  1641. var = pRs->Fields->GetItem("Account")->Value;
  1642. pVs->put(key, var);
  1643. swprintf(key,L"ServiceAccountStatus.%ld",ndx);
  1644. var = pRs->Fields->GetItem("Status")->Value;
  1645. pVs->put(key,var);
  1646. pRs->MoveNext();
  1647. ndx++;
  1648. pVs->put(L"ServiceAccountEntries",ndx);
  1649. }
  1650. }
  1651. catch (_com_error& ce)
  1652. {
  1653. hr = ce.Error();
  1654. }
  1655. catch (...)
  1656. {
  1657. hr = E_FAIL;
  1658. }
  1659. return hr;
  1660. }
  1661. //---------------------------------------------------------------------------------------------
  1662. // SavePasswordAge : Saves the password age of the computer account at a given time.
  1663. // It also stores the computer description.
  1664. //---------------------------------------------------------------------------------------------
  1665. STDMETHODIMP CIManageDB::SavePasswordAge(BSTR sDomain, BSTR sComp, BSTR sDesc, long lAge)
  1666. {
  1667. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1668. HRESULT hr = S_OK;
  1669. try
  1670. {
  1671. _bstr_t sQuery;
  1672. WCHAR sTemp[LEN_Path];
  1673. _variant_t var;
  1674. time_t tm;
  1675. COleDateTime dt(time(&tm));
  1676. // Delete the entry if one exists.
  1677. wsprintf(sTemp, L"DomainName=\"%s\" and compname=\"%s\"", (WCHAR*) sDomain, (WCHAR*) sComp);
  1678. var = sTemp;
  1679. ClearTable(L"PasswordAge", var);
  1680. var = L"PasswordAge";
  1681. _RecordsetPtr pRs(__uuidof(Recordset));
  1682. pRs->Open(var, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  1683. pRs->AddNew();
  1684. pRs->Fields->GetItem(L"Time")->Value = DATE(dt);
  1685. wcscpy((WCHAR*) sDomain, UStrUpr((WCHAR*)sDomain));
  1686. pRs->Fields->GetItem(L"DomainName")->Value = sDomain;
  1687. pRs->Fields->GetItem(L"CompName")->Value = sComp;
  1688. pRs->Fields->GetItem(L"Description")->Value = sDesc;
  1689. pRs->Fields->GetItem(L"PwdAge")->Value = lAge;
  1690. pRs->Update();
  1691. }
  1692. catch (_com_error& ce)
  1693. {
  1694. hr = ce.Error();
  1695. }
  1696. catch (...)
  1697. {
  1698. hr = E_FAIL;
  1699. }
  1700. return hr;
  1701. }
  1702. //---------------------------------------------------------------------------------------------
  1703. // GetPasswordAge : Gets the password age and description of a given computer
  1704. //---------------------------------------------------------------------------------------------
  1705. STDMETHODIMP CIManageDB::GetPasswordAge(BSTR sDomain, BSTR sComp, BSTR *sDesc, long *lAge, long *lTime)
  1706. {
  1707. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1708. HRESULT hr = S_OK;
  1709. try
  1710. {
  1711. _bstr_t sQuery;
  1712. WCHAR sTemp[LEN_Path];
  1713. _variant_t var;
  1714. time_t tm;
  1715. COleDateTime dt(time(&tm));
  1716. DATE val;
  1717. wsprintf(sTemp, L"DomainName =\"%s\" AND CompName = \"%s\"", (WCHAR*) sDomain, (WCHAR*) sComp);
  1718. sQuery = _bstr_t(L"Select * from PasswordAge where ") + _bstr_t(sTemp);
  1719. var = sQuery;
  1720. _RecordsetPtr pRs(__uuidof(Recordset));
  1721. pRs->Open(var, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1722. if ( ! pRs->EndOfFile )
  1723. {
  1724. val = pRs->Fields->GetItem(L"Time")->Value;
  1725. *sDesc = pRs->Fields->GetItem(L"Description")->Value.bstrVal;
  1726. *lAge = pRs->Fields->GetItem(L"PwdAge")->Value;
  1727. }
  1728. else
  1729. {
  1730. hr = S_FALSE;
  1731. }
  1732. pRs->Close();
  1733. }
  1734. catch (_com_error& ce)
  1735. {
  1736. hr = ce.Error();
  1737. }
  1738. catch (...)
  1739. {
  1740. hr = E_FAIL;
  1741. }
  1742. return hr;
  1743. }
  1744. //---------------------------------------------------------------------------------------------
  1745. // SetServiceAcctEntryStatus : Sets the Account and the status for a given service on a given
  1746. // computer.
  1747. //---------------------------------------------------------------------------------------------
  1748. STDMETHODIMP CIManageDB::SetServiceAcctEntryStatus(BSTR sComp, BSTR sSvc, BSTR sAcct, long Status)
  1749. {
  1750. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1751. HRESULT hr = S_OK;
  1752. try
  1753. {
  1754. _variant_t var;
  1755. _bstr_t sQuery;
  1756. WCHAR sTemp[LEN_Path];
  1757. wsprintf(sTemp, L"Select * from ServiceAccounts where System = \"%s\" and Service = \"%s\"", (WCHAR*) sComp, (WCHAR*) sSvc);
  1758. sQuery = sTemp;
  1759. _variant_t vtSource = sQuery;
  1760. _RecordsetPtr pRs(__uuidof(Recordset));
  1761. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1762. if ( !pRs->EndOfFile )
  1763. {
  1764. if ( sAcct )
  1765. {
  1766. var = _bstr_t(sAcct);
  1767. pRs->Fields->GetItem(L"Account")->Value = var;
  1768. }
  1769. var = Status;
  1770. pRs->Fields->GetItem(L"Status")->Value = var;
  1771. pRs->Update();
  1772. }
  1773. else
  1774. {
  1775. hr = E_INVALIDARG;
  1776. }
  1777. }
  1778. catch (_com_error& ce)
  1779. {
  1780. hr = ce.Error();
  1781. }
  1782. catch (...)
  1783. {
  1784. hr = E_FAIL;
  1785. }
  1786. return hr;
  1787. }
  1788. //---------------------------------------------------------------------------------------------
  1789. // SetDistActionStatus : Sets the Distributed action's status and its message.
  1790. //---------------------------------------------------------------------------------------------
  1791. STDMETHODIMP CIManageDB::SetDistActionStatus(long lActionID, BSTR sComp, long lStatus, BSTR sText)
  1792. {
  1793. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1794. HRESULT hr = S_OK;
  1795. try
  1796. {
  1797. _variant_t var;
  1798. _bstr_t sQuery;
  1799. WCHAR sTemp[LEN_Path];
  1800. if ( lActionID == -1 )
  1801. {
  1802. // lookup by the job filename
  1803. wsprintf(sTemp,L"Select * from DistributedAction where ResultFile = \"%s\"",(WCHAR*) sComp);
  1804. }
  1805. else
  1806. {
  1807. // lookup by action ID and computer name
  1808. wsprintf(sTemp, L"Select * from DistributedAction where ServerName = \"%s\" and ActionID = %d", (WCHAR*) sComp, lActionID);
  1809. }
  1810. sQuery = sTemp;
  1811. _variant_t vtSource = sQuery;
  1812. _RecordsetPtr pRs(__uuidof(Recordset));
  1813. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  1814. if ( !pRs->EndOfFile )
  1815. {
  1816. var = _bstr_t(sText);
  1817. pRs->Fields->GetItem(L"StatusText")->Value = var;
  1818. var = lStatus;
  1819. pRs->Fields->GetItem(L"Status")->Value = var;
  1820. pRs->Update();
  1821. }
  1822. else
  1823. {
  1824. hr = E_INVALIDARG;
  1825. }
  1826. }
  1827. catch (_com_error& ce)
  1828. {
  1829. hr = ce.Error();
  1830. }
  1831. catch (...)
  1832. {
  1833. hr = E_FAIL;
  1834. }
  1835. return hr;
  1836. }
  1837. //---------------------------------------------------------------------------------------------
  1838. // CancelDistributedAction : Deletes a pirticular distributed action
  1839. //---------------------------------------------------------------------------------------------
  1840. STDMETHODIMP CIManageDB::CancelDistributedAction(long lActionID, BSTR sComp)
  1841. {
  1842. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1843. WCHAR sFilter[LEN_Path];
  1844. wsprintf(sFilter, L"ActionID = %d and ServerName = \"%s\"", lActionID, (WCHAR*) sComp);
  1845. _variant_t Filter = sFilter;
  1846. return ClearTable(L"DistributedAction", Filter);
  1847. }
  1848. //---------------------------------------------------------------------------------------------
  1849. // AddAcctRef : Adds an account reference record.
  1850. //---------------------------------------------------------------------------------------------
  1851. STDMETHODIMP CIManageDB::AddAcctRef(BSTR sDomain, BSTR sAcct, BSTR sAcctSid, BSTR sComp, long lCount, BSTR sType)
  1852. {
  1853. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  1854. HRESULT hr = S_OK;
  1855. try
  1856. {
  1857. time_t tm;
  1858. COleDateTime dt(time(&tm));
  1859. _variant_t var;
  1860. WCHAR sFilter[LEN_Path];
  1861. VARIANT_BOOL bSidColumn = VARIANT_FALSE;
  1862. //find out if the new sid column is there, if not, don't try
  1863. //writing to it
  1864. SidColumnInARTable(&bSidColumn);
  1865. wsprintf(sFilter, L"DomainName = \"%s\" and Server = \"%s\" and Account = \"%s\" and RefType = \"%s\"", sDomain, sComp, sAcct, sType);
  1866. var = sFilter;
  1867. ClearTable(L"AccountRefs", var);
  1868. _RecordsetPtr pRs(__uuidof(Recordset));
  1869. _variant_t vtSource = L"AccountRefs";
  1870. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  1871. pRs->AddNew();
  1872. pRs->Fields->GetItem(L"Time")->Value = DATE(dt);
  1873. if (wcscmp(sDomain, GET_STRING(IDS_UnknownSid)))
  1874. wcscpy((WCHAR*) sDomain, UStrUpr((WCHAR*)sDomain));
  1875. pRs->Fields->GetItem(L"DomainName")->Value = sDomain;
  1876. wcscpy((WCHAR*) sComp, UStrUpr((WCHAR*)sComp));
  1877. pRs->Fields->GetItem(L"Server")->Value = sComp;
  1878. pRs->Fields->GetItem(L"Account")->Value = sAcct;
  1879. pRs->Fields->GetItem(L"RefCount")->Value = lCount;
  1880. pRs->Fields->GetItem(L"RefType")->Value = sType;
  1881. if (bSidColumn)
  1882. {
  1883. wcscpy((WCHAR*) sAcctSid, UStrUpr((WCHAR*)sAcctSid));
  1884. pRs->Fields->GetItem(L"AccountSid")->Value = sAcctSid;
  1885. }
  1886. pRs->Update();
  1887. }
  1888. catch (_com_error& ce)
  1889. {
  1890. hr = ce.Error();
  1891. }
  1892. catch (...)
  1893. {
  1894. hr = E_FAIL;
  1895. }
  1896. return hr;
  1897. }
  1898. void CIManageDB::ClipVarset(IVarSetPtr pVS)
  1899. {
  1900. HRESULT hr = S_OK;
  1901. _bstr_t sTemp, keyName, sTempKey;
  1902. long offset = 0;
  1903. bool cont = true;
  1904. WCHAR sKeyName[MAX_BUF_LEN];
  1905. _variant_t varKey, value;
  1906. IEnumVARIANT * varEnum;
  1907. DWORD nGot = 0;
  1908. IUnknown * pEnum = NULL;
  1909. CString strTemp;
  1910. int len;
  1911. // we are now going to enumerate through the varset and clip the strings if larger then MAX_BUFFER
  1912. hr = pVS->get__NewEnum(&pEnum);
  1913. if ( SUCCEEDED(hr) )
  1914. {
  1915. // Get the IEnumVARIANT pointer to enumerate
  1916. hr = pEnum->QueryInterface(IID_IEnumVARIANT,(void**)&varEnum);
  1917. pEnum->Release();
  1918. pEnum = NULL;
  1919. }
  1920. if ( SUCCEEDED(hr))
  1921. {
  1922. while ( (hr = varEnum->Next(1,&varKey,&nGot)) == S_OK )
  1923. {
  1924. if ( nGot > 0 )
  1925. {
  1926. keyName = V_BSTR(&varKey);
  1927. value = pVS->get(keyName);
  1928. if ( value.vt == VT_BSTR )
  1929. {
  1930. sTemp = value;
  1931. if ( sTemp.length() > MAX_BUF_LEN )
  1932. {
  1933. CString str((WCHAR*) sTemp);
  1934. // This won't fit in the buffer. We need to break it up and save
  1935. while (cont)
  1936. {
  1937. cont = false;
  1938. strTemp = str.Mid((offset*255), 255);
  1939. len = strTemp.GetLength();
  1940. if ( len )
  1941. {
  1942. offset++;
  1943. wsprintf(sKeyName, L"BROKEN.%s.%d", (WCHAR*) keyName, offset);
  1944. sTempKey = sKeyName;
  1945. sTemp = strTemp;
  1946. pVS->put(sTempKey, sTemp);
  1947. cont = (len == 255);
  1948. }
  1949. }
  1950. pVS->put(keyName, L"DIVIDED_KEY");
  1951. wsprintf(sKeyName, L"BROKEN.%s", (WCHAR*) keyName);
  1952. sTempKey = sKeyName;
  1953. pVS->put(sTempKey, offset);
  1954. cont = true;
  1955. offset = 0;
  1956. }
  1957. }
  1958. }
  1959. }
  1960. varEnum->Release();
  1961. }
  1962. }
  1963. void CIManageDB::RestoreVarset(IVarSetPtr pVS)
  1964. {
  1965. HRESULT hr = S_OK;
  1966. _bstr_t sTemp, keyName, sTempKey;
  1967. long offset = 0;
  1968. bool cont = true;
  1969. WCHAR sKeyName[MAX_BUF_LEN];
  1970. _variant_t varKey, value;
  1971. IEnumVARIANT * varEnum;
  1972. DWORD nGot = 0;
  1973. IUnknown * pEnum = NULL;
  1974. _bstr_t strTemp;
  1975. // we are now going to enumerate through the varset and clip the strings if larger then MAX_BUFFER
  1976. hr = pVS->get__NewEnum(&pEnum);
  1977. if ( SUCCEEDED(hr) )
  1978. {
  1979. // Get the IEnumVARIANT pointer to enumerate
  1980. hr = pEnum->QueryInterface(IID_IEnumVARIANT,(void**)&varEnum);
  1981. pEnum->Release();
  1982. pEnum = NULL;
  1983. }
  1984. if ( SUCCEEDED(hr))
  1985. {
  1986. while ( (hr = varEnum->Next(1,&varKey,&nGot)) == S_OK )
  1987. {
  1988. if ( nGot > 0 )
  1989. {
  1990. keyName = V_BSTR(&varKey);
  1991. value = pVS->get(keyName);
  1992. if ( value.vt == VT_BSTR )
  1993. {
  1994. sTemp = value;
  1995. if (!_wcsicmp((WCHAR*)sTemp, L"DIVIDED_KEY"))
  1996. {
  1997. wsprintf(sKeyName, L"BROKEN.%s", (WCHAR*) keyName);
  1998. sTempKey = sKeyName;
  1999. value = pVS->get(sTempKey);
  2000. if ( value.vt == VT_I4 )
  2001. {
  2002. offset = value.lVal;
  2003. for ( long x = 1; x <= offset; x++ )
  2004. {
  2005. wsprintf(sKeyName, L"BROKEN.%s.%d", (WCHAR*) keyName, x);
  2006. sTempKey = sKeyName;
  2007. value = pVS->get(sTempKey);
  2008. if ( value.vt == VT_BSTR )
  2009. {
  2010. sTemp = value;
  2011. strTemp += V_BSTR(&value);
  2012. }
  2013. }
  2014. pVS->put(keyName, strTemp);
  2015. strTemp = L"";
  2016. }
  2017. }
  2018. }
  2019. }
  2020. }
  2021. varEnum->Release();
  2022. }
  2023. }
  2024. STDMETHODIMP CIManageDB::AddSourceObject(BSTR sDomain, BSTR sSAMName, BSTR sType, BSTR sRDN, BSTR sCanonicalName, LONG bSource)
  2025. {
  2026. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2027. HRESULT hr = S_OK;
  2028. try
  2029. {
  2030. m_rsAccounts->AddNew();
  2031. wcscpy((WCHAR*) sDomain, UStrUpr((WCHAR*)sDomain));
  2032. m_rsAccounts->Fields->GetItem(L"Domain")->Value = sDomain;
  2033. m_rsAccounts->Fields->GetItem(L"Name")->Value = sSAMName;
  2034. wcscpy((WCHAR*) sType, UStrLwr((WCHAR*)sType));
  2035. m_rsAccounts->Fields->GetItem(L"Type")->Value = sType;
  2036. m_rsAccounts->Fields->GetItem(L"RDN")->Value = sRDN;
  2037. m_rsAccounts->Fields->GetItem(L"Canonical Name")->Value = sCanonicalName;
  2038. m_rsAccounts->Update();
  2039. }
  2040. catch (_com_error& ce)
  2041. {
  2042. hr = ce.Error();
  2043. }
  2044. catch (...)
  2045. {
  2046. hr = E_FAIL;
  2047. }
  2048. return hr;
  2049. }
  2050. STDMETHODIMP CIManageDB::OpenAccountsTable(LONG bSource)
  2051. {
  2052. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2053. HRESULT hr = S_OK;
  2054. try
  2055. {
  2056. if (m_rsAccounts->State == adStateClosed)
  2057. {
  2058. _variant_t vtSource;
  2059. if ( bSource )
  2060. vtSource = L"SourceAccounts";
  2061. else
  2062. vtSource = L"TargetAccounts";
  2063. //if not modified already, modify the table
  2064. if (!NCTablesColumnsChanged(bSource))
  2065. hr = ChangeNCTableColumns(bSource);
  2066. if (SUCCEEDED(hr))
  2067. m_rsAccounts->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2068. }
  2069. else
  2070. hr = S_FALSE;
  2071. }
  2072. catch (_com_error& ce)
  2073. {
  2074. hr = ce.Error();
  2075. }
  2076. catch (...)
  2077. {
  2078. hr = E_FAIL;
  2079. }
  2080. return hr;
  2081. }
  2082. STDMETHODIMP CIManageDB::CloseAccountsTable()
  2083. {
  2084. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2085. HRESULT hr = S_OK;
  2086. try
  2087. {
  2088. if (m_rsAccounts->State == adStateOpen)
  2089. {
  2090. m_rsAccounts->Close();
  2091. }
  2092. }
  2093. catch (_com_error& ce)
  2094. {
  2095. hr = ce.Error();
  2096. }
  2097. catch (...)
  2098. {
  2099. hr = E_FAIL;
  2100. }
  2101. return hr;
  2102. }
  2103. // Returns the number of entries in the migratedobjects table.
  2104. STDMETHODIMP CIManageDB::AreThereAnyMigratedObjects(long *count)
  2105. {
  2106. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2107. HRESULT hr = S_OK;
  2108. try
  2109. {
  2110. _RecordsetPtr pRs(__uuidof(Recordset));
  2111. _variant_t vtSource = L"MigratedObjects";
  2112. WCHAR sActionInfo[LEN_Path];
  2113. _variant_t var;
  2114. wcscpy(sActionInfo, L"Select count(*) as NUM from MigratedObjects");
  2115. vtSource = _bstr_t(sActionInfo);
  2116. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2117. var = pRs->Fields->GetItem((long)0)->Value;
  2118. * count = var.lVal;
  2119. }
  2120. catch (_com_error& ce)
  2121. {
  2122. hr = ce.Error();
  2123. }
  2124. catch (...)
  2125. {
  2126. hr = E_FAIL;
  2127. }
  2128. return hr;
  2129. }
  2130. STDMETHODIMP CIManageDB::GetActionHistoryKey(long lActionID, BSTR sKeyName, VARIANT *pVar)
  2131. {
  2132. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2133. HRESULT hr = S_OK;
  2134. try
  2135. {
  2136. _RecordsetPtr pRs(__uuidof(Recordset));
  2137. _variant_t vtSource;
  2138. WCHAR sActionInfo[LEN_Path];
  2139. _variant_t var;
  2140. wsprintf(sActionInfo, L"Select * from ActionHistory where Property = \"%s\" and ActionID = %d", (WCHAR*) sKeyName, lActionID);
  2141. vtSource = _bstr_t(sActionInfo);
  2142. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2143. if ((pRs->BOF == VARIANT_FALSE) && (pRs->EndOfFile == VARIANT_FALSE))
  2144. {
  2145. GetVarFromDB(pRs, var);
  2146. }
  2147. *pVar = var.Detach();
  2148. }
  2149. catch (_com_error& ce)
  2150. {
  2151. hr = ce.Error();
  2152. }
  2153. catch (...)
  2154. {
  2155. hr = E_FAIL;
  2156. }
  2157. return hr;
  2158. }
  2159. STDMETHODIMP CIManageDB::GetMigratedObjectBySourceDN(BSTR sSourceDN, IUnknown **ppUnk)
  2160. {
  2161. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2162. HRESULT hr = S_OK;
  2163. try
  2164. {
  2165. _RecordsetPtr pRs(__uuidof(Recordset));
  2166. _variant_t vtSource = L"MigratedObjects";
  2167. IVarSetPtr pVs = *ppUnk;
  2168. WCHAR sActionInfo[LEN_Path];
  2169. long lCnt = 0;
  2170. _bstr_t sName;
  2171. // If the parameters are not correct then we need to return an error
  2172. if ( (wcslen(sSourceDN) == 0) )
  2173. _com_issue_error(E_INVALIDARG);
  2174. wsprintf(sActionInfo, L"SELECT * FROM MigratedObjects WHERE SourceAdsPath Like '%%%s'", (WCHAR*) sSourceDN);
  2175. vtSource = sActionInfo;
  2176. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2177. if (pRs->GetRecordCount() > 0)
  2178. {
  2179. // We want the latest move.
  2180. pRs->MoveLast();
  2181. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_ActionID));
  2182. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  2183. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Time));
  2184. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  2185. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomain));
  2186. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  2187. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetDomain));
  2188. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  2189. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceAdsPath));
  2190. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  2191. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetAdsPath));
  2192. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  2193. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_status));
  2194. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  2195. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceSamName));
  2196. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  2197. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetSamName));
  2198. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  2199. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Type));
  2200. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  2201. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_GUID));
  2202. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  2203. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceRid));
  2204. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  2205. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetRid));
  2206. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  2207. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomainSid));
  2208. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  2209. }
  2210. else
  2211. {
  2212. hr = S_FALSE;
  2213. }
  2214. }
  2215. catch (_com_error& ce)
  2216. {
  2217. hr = ce.Error();
  2218. }
  2219. catch (...)
  2220. {
  2221. hr = E_FAIL;
  2222. }
  2223. return hr;
  2224. }
  2225. STDMETHODIMP CIManageDB::SaveUserProps(IUnknown * pUnk)
  2226. {
  2227. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2228. HRESULT hr = S_OK;
  2229. try
  2230. {
  2231. _RecordsetPtr pRs(__uuidof(Recordset));
  2232. _variant_t vtSource;
  2233. IVarSetPtr pVs = pUnk;
  2234. WCHAR sQuery[LEN_Path];
  2235. WCHAR sSource[LEN_Path], sDomain[LEN_Path];
  2236. HRESULT hr = S_OK;
  2237. bool bComp = false;
  2238. _variant_t var;
  2239. var = pVs->get(GET_BSTR(DCTVS_Options_SourceDomain));
  2240. wcscpy(sDomain, (WCHAR*)V_BSTR(&var));
  2241. var = pVs->get(GET_BSTR(DCTVS_CopiedAccount_SourceSam));
  2242. wcscpy(sSource, (WCHAR*)V_BSTR(&var));
  2243. wsprintf(sQuery, L"delete from UserProps where SourceDomain=\"%s\" and SourceSam=\"%s\"",
  2244. sDomain, sSource);
  2245. vtSource = _bstr_t(sQuery);
  2246. hr = pRs->raw_Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2247. vtSource = L"UserProps";
  2248. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2249. pRs->AddNew();
  2250. pRs->Fields->GetItem(L"ActionID")->Value = pVs->get(GET_BSTR(DB_ActionID));
  2251. pRs->Fields->GetItem(L"SourceDomain")->Value = sDomain;
  2252. pRs->Fields->GetItem(L"SourceSam")->Value = sSource;
  2253. pRs->Fields->GetItem(L"Flags")->Value = pVs->get(GET_BSTR(DCTVS_CopiedAccount_UserFlags));
  2254. pRs->Fields->GetItem(L"Expires")->Value = pVs->get(GET_BSTR(DCTVS_CopiedAccount_ExpDate));
  2255. pRs->Update();
  2256. }
  2257. catch (_com_error& ce)
  2258. {
  2259. hr = ce.Error();
  2260. }
  2261. catch (...)
  2262. {
  2263. hr = E_FAIL;
  2264. }
  2265. return hr;
  2266. }
  2267. STDMETHODIMP CIManageDB::GetUserProps(BSTR sDom, BSTR sSam, IUnknown **ppUnk)
  2268. {
  2269. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2270. HRESULT hr = S_OK;
  2271. try
  2272. {
  2273. _RecordsetPtr pRs(__uuidof(Recordset));
  2274. _variant_t vtSource = L"UserProps";
  2275. IVarSetPtr pVs = *ppUnk;
  2276. WCHAR sActionInfo[LEN_Path];
  2277. long lCnt = 0;
  2278. _bstr_t sName;
  2279. // If the parameters are not correct then we need to return an error
  2280. if ( !wcslen((WCHAR*)sDom) && !wcslen((WCHAR*)sSam) )
  2281. _com_issue_error(E_INVALIDARG);
  2282. wsprintf(sActionInfo, L"SELECT * FROM UserProps WHERE SourceDomain='%s' and SourceSam='%s'", (WCHAR*)sDom, (WCHAR*)sSam);
  2283. vtSource = sActionInfo;
  2284. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2285. if (pRs->GetRecordCount() > 0)
  2286. {
  2287. // We want the latest move.
  2288. pRs->MoveLast();
  2289. pVs->put(L"ActionID",pRs->Fields->GetItem(L"ActionID")->Value);
  2290. pVs->put(L"SourceDomain",pRs->Fields->GetItem(L"SourceDomain")->Value);
  2291. pVs->put(L"SourceSam",pRs->Fields->GetItem(L"SourceSam")->Value);
  2292. pVs->put(GET_BSTR(DCTVS_CopiedAccount_UserFlags),pRs->Fields->GetItem(L"Flags")->Value);
  2293. pVs->put(GET_BSTR(DCTVS_CopiedAccount_ExpDate),pRs->Fields->GetItem(L"Expires")->Value);
  2294. }
  2295. else
  2296. {
  2297. hr = S_FALSE;
  2298. }
  2299. }
  2300. catch (_com_error& ce)
  2301. {
  2302. hr = ce.Error();
  2303. }
  2304. catch (...)
  2305. {
  2306. hr = E_FAIL;
  2307. }
  2308. return hr;
  2309. }
  2310. /*********************************************************************
  2311. * *
  2312. * Written by: Paul Thompson *
  2313. * Date: 18 AUG 2000 *
  2314. * *
  2315. * This protected member function of the CIManageDB checks to see*
  2316. * if the new Source domain SID column is in the MigratedObjects *
  2317. * table. *
  2318. * *
  2319. *********************************************************************/
  2320. //BEGIN SrcSidColumnInMigratedObjectsTable
  2321. STDMETHODIMP CIManageDB::SrcSidColumnInMigratedObjectsTable(VARIANT_BOOL *pbFound)
  2322. {
  2323. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2324. HRESULT hr = S_OK;
  2325. *pbFound = VARIANT_FALSE;
  2326. try
  2327. {
  2328. _RecordsetPtr pRs(__uuidof(Recordset));
  2329. _variant_t vtSource = L"MigratedObjects";
  2330. long numColumns;
  2331. long ndx = 0;
  2332. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2333. //get the number of columns
  2334. numColumns = pRs->Fields->GetCount();
  2335. //look for new column's name in each column header
  2336. while ((ndx < numColumns) && (*pbFound == VARIANT_FALSE))
  2337. {
  2338. //get the column name
  2339. _variant_t var(ndx);
  2340. _bstr_t columnName = pRs->Fields->GetItem(var)->Name;
  2341. //if this is the Src Sid column then set return value flag to true
  2342. if (!_wcsicmp((WCHAR*)columnName, GET_BSTR(DB_SourceDomainSid)))
  2343. *pbFound = VARIANT_TRUE;
  2344. ndx++;
  2345. }
  2346. }
  2347. catch (_com_error& ce)
  2348. {
  2349. hr = ce.Error();
  2350. }
  2351. catch (...)
  2352. {
  2353. hr = E_FAIL;
  2354. }
  2355. return hr;
  2356. }
  2357. //END SrcSidColumnInMigratedObjectsTable
  2358. /*********************************************************************
  2359. * *
  2360. * Written by: Paul Thompson *
  2361. * Date: 18 AUG 2000 *
  2362. * *
  2363. * This protected member function of the CIManageDB retrieves *
  2364. * information about previously migrated objects, from a MOT missing *
  2365. * the source sid column, within a given action or as a whole. *
  2366. * *
  2367. *********************************************************************/
  2368. //BEGIN GetMigratedObjectsFromOldMOT
  2369. STDMETHODIMP CIManageDB::GetMigratedObjectsFromOldMOT(long lActionID, IUnknown ** ppUnk)
  2370. {
  2371. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2372. HRESULT hr = S_OK;
  2373. // This function returns all migrated objects and their information related
  2374. // to a pirticular Action ID. This is going to return nothing if the actionID is
  2375. // empty.
  2376. try
  2377. {
  2378. _RecordsetPtr pRs(__uuidof(Recordset));
  2379. _variant_t vtSource = L"MigratedObjects";
  2380. IVarSetPtr pVs = *ppUnk;
  2381. WCHAR sActionInfo[255];
  2382. long lCnt = 0;
  2383. if ( lActionID != -1 )
  2384. {
  2385. // If a valid ActionID is specified then we only return the data for that one.
  2386. // but if -1 is passed in then we return all migrated objects.
  2387. wsprintf(sActionInfo, L"ActionID=%d", lActionID);
  2388. pRs->Filter = sActionInfo;
  2389. }
  2390. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2391. if (pRs->GetRecordCount() > 0)
  2392. {
  2393. pRs->MoveFirst();
  2394. while ( !pRs->EndOfFile )
  2395. {
  2396. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_ActionID));
  2397. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  2398. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Time));
  2399. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  2400. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomain));
  2401. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  2402. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetDomain));
  2403. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  2404. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceAdsPath));
  2405. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  2406. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetAdsPath));
  2407. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  2408. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_status));
  2409. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  2410. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceSamName));
  2411. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  2412. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetSamName));
  2413. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  2414. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Type));
  2415. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  2416. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_GUID));
  2417. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  2418. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceRid));
  2419. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  2420. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetRid));
  2421. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  2422. pRs->MoveNext();
  2423. lCnt++;
  2424. }
  2425. pVs->put(L"MigratedObjects", lCnt);
  2426. }
  2427. else
  2428. {
  2429. hr = S_FALSE;
  2430. }
  2431. }
  2432. catch (_com_error& ce)
  2433. {
  2434. hr = ce.Error();
  2435. }
  2436. catch (...)
  2437. {
  2438. hr = E_FAIL;
  2439. }
  2440. return hr;
  2441. }
  2442. //END GetMigratedObjectsFromOldMOT
  2443. /*********************************************************************
  2444. * *
  2445. * Written by: Paul Thompson *
  2446. * Date: 18 AUG 2000 *
  2447. * *
  2448. * This protected member function of the CIManageDB adds the *
  2449. * source domain SID column to the MigratedObjects table. *
  2450. * *
  2451. *********************************************************************/
  2452. //BEGIN CreateSrcSidColumnInMOT
  2453. STDMETHODIMP CIManageDB::CreateSrcSidColumnInMOT(VARIANT_BOOL *pbCreated)
  2454. {
  2455. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2456. /* local constants */
  2457. const long COLUMN_MAX_CHARS = 255;
  2458. /* local variables */
  2459. HRESULT hr = S_OK;
  2460. /* function body */
  2461. *pbCreated = VARIANT_FALSE;
  2462. try
  2463. {
  2464. ADOX::_CatalogPtr m_pCatalog(__uuidof(ADOX::Catalog));
  2465. ADOX::_TablePtr m_pTable = NULL;
  2466. WCHAR sConnect[MAX_PATH];
  2467. WCHAR sDir[MAX_PATH];
  2468. // Get the path to the MDB file from the registry
  2469. TRegKey key;
  2470. DWORD rc = key.Open(sKeyBase);
  2471. if ( !rc )
  2472. rc = key.ValueGetStr(L"Directory", sDir, MAX_PATH);
  2473. if ( rc != 0 )
  2474. wcscpy(sDir, L"");
  2475. // Now build the connect string.
  2476. wsprintf(sConnect, L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sprotar.mdb;", sDir);
  2477. //Open the catalog
  2478. m_pCatalog->PutActiveConnection(sConnect);
  2479. //get a pointer to the database's MigratedObjects Table
  2480. m_pTable = m_pCatalog->Tables->Item[L"MigratedObjects"];
  2481. //append a new column to the end of the MOT
  2482. m_pTable->Columns->Append(L"SourceDomainSid", adVarWChar, COLUMN_MAX_CHARS);
  2483. //set the column to be nullable
  2484. // ADOX::_ColumnPtr pColumn = m_pTable->Columns->Item[L"SourceDomainSid"];
  2485. // pColumn->Attributes = ADOX::adColNullable;
  2486. *pbCreated = VARIANT_TRUE;
  2487. }
  2488. catch (_com_error& ce)
  2489. {
  2490. hr = ce.Error();
  2491. }
  2492. catch (...)
  2493. {
  2494. hr = E_FAIL;
  2495. }
  2496. return hr;
  2497. }
  2498. //END CreateSrcSidColumnInMOT
  2499. /*********************************************************************
  2500. * *
  2501. * Written by: Paul Thompson *
  2502. * Date: 22 AUG 2000 *
  2503. * *
  2504. * This protected member function of the CIManageDB deletes the *
  2505. * source domain SID column from the MigratedObjects table. *
  2506. * *
  2507. *********************************************************************/
  2508. //BEGIN DeleteSrcSidColumnInMOT
  2509. STDMETHODIMP CIManageDB::DeleteSrcSidColumnInMOT(VARIANT_BOOL *pbDeleted)
  2510. {
  2511. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2512. /* local constants */
  2513. /* local variables */
  2514. HRESULT hr = S_OK;
  2515. /* function body */
  2516. *pbDeleted = VARIANT_FALSE;
  2517. try
  2518. {
  2519. ADOX::_CatalogPtr m_pCatalog(__uuidof(ADOX::Catalog));
  2520. ADOX::_TablePtr m_pTable = NULL;
  2521. WCHAR sConnect[MAX_PATH];
  2522. WCHAR sDir[MAX_PATH];
  2523. // Get the path to the MDB file from the registry
  2524. TRegKey key;
  2525. DWORD rc = key.Open(sKeyBase);
  2526. if ( !rc )
  2527. rc = key.ValueGetStr(L"Directory", sDir, MAX_PATH);
  2528. if ( rc != 0 )
  2529. wcscpy(sDir, L"");
  2530. // Now build the connect string.
  2531. wsprintf(sConnect, L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sprotar.mdb;", sDir);
  2532. //Open the catalog
  2533. m_pCatalog->PutActiveConnection(sConnect);
  2534. //get a pointer to the database's MigratedObjects Table
  2535. m_pTable = m_pCatalog->Tables->Item[L"MigratedObjects"];
  2536. //delete the column from the MOT
  2537. m_pTable->Columns->Delete(L"SourceDomainSid");
  2538. *pbDeleted = VARIANT_TRUE;
  2539. }
  2540. catch (_com_error& ce)
  2541. {
  2542. hr = ce.Error();
  2543. }
  2544. catch (...)
  2545. {
  2546. hr = E_FAIL;
  2547. }
  2548. return hr;
  2549. }
  2550. //END DeleteSrcSidColumnInMOT
  2551. /*********************************************************************
  2552. * *
  2553. * Written by: Paul Thompson *
  2554. * Date: 21 AUG 2000 *
  2555. * *
  2556. * This protected member function of the CIManageDB populates the*
  2557. * new Source domain SID column in the MigratedObjects table for all *
  2558. * entries from the given domain. If the domain cannot be reached no*
  2559. * entry is added. *
  2560. * *
  2561. *********************************************************************/
  2562. //BEGIN PopulateSrcSidColumnByDomain
  2563. STDMETHODIMP CIManageDB::PopulateSrcSidColumnByDomain(BSTR sDomainName,
  2564. BSTR sSid,
  2565. VARIANT_BOOL * pbPopulated)
  2566. {
  2567. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2568. /* local variables */
  2569. HRESULT hr = S_OK;
  2570. PSID pSid = NULL;
  2571. DWORD rc = 0;
  2572. WCHAR * domctrl = NULL;
  2573. WCHAR txtSid[MAX_PATH];
  2574. DWORD lenTxt = DIM(txtSid);
  2575. /* function body */
  2576. *pbPopulated = VARIANT_FALSE; //init flag to false
  2577. try
  2578. {
  2579. _RecordsetPtr pRs(__uuidof(Recordset));
  2580. _variant_t vtSource = L"MigratedObjects";
  2581. WCHAR sActionInfo[MAX_PATH];
  2582. //if we don't already know the source sid then find it
  2583. wcscpy(txtSid, (WCHAR*)sSid);
  2584. if (!wcscmp(txtSid, L""))
  2585. {
  2586. //get the sid for this domain
  2587. if ( sDomainName[0] != L'\\' )
  2588. {
  2589. rc = NetGetDCName(NULL,(WCHAR*)sDomainName,(LPBYTE*)&domctrl);
  2590. }
  2591. if ( rc )
  2592. return hr;
  2593. rc = GetDomainSid(domctrl,&pSid);
  2594. NetApiBufferFree(domctrl);
  2595. if ( !GetTextualSid(pSid,txtSid,&lenTxt) )
  2596. {
  2597. if (pSid)
  2598. FreeSid(pSid);
  2599. return hr;
  2600. }
  2601. if (pSid)
  2602. FreeSid(pSid);
  2603. }
  2604. //save the sid in a variant
  2605. _variant_t vName = txtSid;
  2606. //find each entry in the MigratedObjects table from this source domain
  2607. //and store the source domain sid in that column
  2608. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomain=\"%s\"", (WCHAR*)sDomainName);
  2609. vtSource = _bstr_t(sActionInfo);
  2610. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2611. if (pRs->GetRecordCount() > 0)
  2612. {
  2613. pRs->MoveFirst();
  2614. while ( !pRs->EndOfFile )
  2615. {
  2616. pRs->Fields->GetItem(L"SourceDomainSid")->Value = vName;
  2617. pRs->MoveNext();
  2618. }
  2619. }
  2620. *pbPopulated = VARIANT_TRUE; //set flag since populated
  2621. }
  2622. catch (_com_error& ce)
  2623. {
  2624. hr = ce.Error();
  2625. }
  2626. catch (...)
  2627. {
  2628. hr = E_FAIL;
  2629. }
  2630. return hr;
  2631. }
  2632. //END PopulateSrcSidColumnByDomain
  2633. /*********************************************************************
  2634. * *
  2635. * Written by: Paul Thompson *
  2636. * Date: 26 SEPT 2000 *
  2637. * *
  2638. * This protected member function of the CIManageDB checks to see*
  2639. * if the new Account SID column is in the Account References table. *
  2640. * *
  2641. *********************************************************************/
  2642. //BEGIN SidColumnInARTable
  2643. STDMETHODIMP CIManageDB::SidColumnInARTable(VARIANT_BOOL *pbFound)
  2644. {
  2645. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2646. HRESULT hr = S_OK;
  2647. *pbFound = VARIANT_FALSE;
  2648. try
  2649. {
  2650. _RecordsetPtr pRs(__uuidof(Recordset));
  2651. _variant_t vtSource = L"AccountRefs";
  2652. long numColumns;
  2653. long ndx = 0;
  2654. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2655. //get the number of columns
  2656. numColumns = pRs->Fields->GetCount();
  2657. //look for new column's name in each column header
  2658. while ((ndx < numColumns) && (*pbFound == VARIANT_FALSE))
  2659. {
  2660. //get the column name
  2661. _variant_t var(ndx);
  2662. _bstr_t columnName = pRs->Fields->GetItem(var)->Name;
  2663. //if this is the Src Sid column then set return value flag to true
  2664. if (!_wcsicmp((WCHAR*)columnName, L"AccountSid"))
  2665. *pbFound = VARIANT_TRUE;
  2666. ndx++;
  2667. }
  2668. }
  2669. catch (_com_error& ce)
  2670. {
  2671. hr = ce.Error();
  2672. }
  2673. catch (...)
  2674. {
  2675. hr = E_FAIL;
  2676. }
  2677. return hr;
  2678. }
  2679. //END SidColumnInARTable
  2680. /*********************************************************************
  2681. * *
  2682. * Written by: Paul Thompson *
  2683. * Date: 26 SEPT 2000 *
  2684. * *
  2685. * This protected member function of the CIManageDB adds the *
  2686. * SID column to the Account Reference table, if it is not already *
  2687. * there. *
  2688. * *
  2689. *********************************************************************/
  2690. //BEGIN CreateSidColumnInAR
  2691. STDMETHODIMP CIManageDB::CreateSidColumnInAR()
  2692. {
  2693. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2694. /* local constants */
  2695. const long COLUMN_MAX_CHARS = 255;
  2696. /* local variables */
  2697. HRESULT hr = S_OK;
  2698. /* function body */
  2699. try
  2700. {
  2701. ADOX::_CatalogPtr m_pCatalog(__uuidof(ADOX::Catalog));
  2702. ADOX::_TablePtr m_pTable = NULL;
  2703. WCHAR sConnect[MAX_PATH];
  2704. WCHAR sDir[MAX_PATH];
  2705. // Get the path to the MDB file from the registry
  2706. TRegKey key;
  2707. DWORD rc = key.Open(sKeyBase);
  2708. if ( !rc )
  2709. rc = key.ValueGetStr(L"Directory", sDir, MAX_PATH);
  2710. if ( rc != 0 )
  2711. wcscpy(sDir, L"");
  2712. // Now build the connect string.
  2713. wsprintf(sConnect, L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sprotar.mdb;", sDir);
  2714. //Open the catalog
  2715. m_pCatalog->PutActiveConnection(sConnect);
  2716. //get a pointer to the database's MigratedObjects Table
  2717. m_pTable = m_pCatalog->Tables->Item[L"AccountRefs"];
  2718. //append a new column to the end of the MOT
  2719. m_pTable->Columns->Append(L"AccountSid", adVarWChar, COLUMN_MAX_CHARS);
  2720. }
  2721. catch (_com_error& ce)
  2722. {
  2723. hr = ce.Error();
  2724. }
  2725. catch (...)
  2726. {
  2727. hr = E_FAIL;
  2728. }
  2729. return hr;
  2730. }
  2731. //END CreateSidColumnInAR
  2732. //---------------------------------------------------------------------------
  2733. // UpgradeDatabase
  2734. //
  2735. // Upgrades Protar.mdb database from version 3.x to 4.x. Version 4.x adds
  2736. // UNICODE support.
  2737. //
  2738. // 2001-02-13 Mark Oluper - initial
  2739. //---------------------------------------------------------------------------
  2740. void CIManageDB::UpgradeDatabase(LPCTSTR pszFolder)
  2741. {
  2742. try
  2743. {
  2744. _bstr_t strFolder = pszFolder;
  2745. _bstr_t strDatabase = strFolder + _T("Protar.mdb");
  2746. _bstr_t strDatabase3x = strFolder + _T("Protar3x.mdb");
  2747. _bstr_t strDatabase4x = strFolder + _T("Protar4x.mdb");
  2748. _bstr_t strConnectionPrefix = _T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=");
  2749. _bstr_t strSourceConnection = strConnectionPrefix + strDatabase;
  2750. _bstr_t strTargetConnection = strConnectionPrefix + strDatabase4x + _T(";Jet OLEDB:Engine Type=5");
  2751. IJetEnginePtr spJetEngine(__uuidof(JetEngine));
  2752. HRESULT hr = spJetEngine->raw_CompactDatabase(strSourceConnection, strTargetConnection);
  2753. if (FAILED(hr))
  2754. {
  2755. AdmtThrowError(
  2756. hr,
  2757. _Module.GetResourceInstance(),
  2758. IDS_E_UPGRADE_TO_TEMPORARY,
  2759. (LPCTSTR)strDatabase,
  2760. (LPCTSTR)strDatabase4x
  2761. );
  2762. }
  2763. if (!MoveFileEx(strDatabase, strDatabase3x, MOVEFILE_WRITE_THROUGH))
  2764. {
  2765. DWORD dwError = GetLastError();
  2766. DeleteFile(strDatabase4x);
  2767. AdmtThrowError(
  2768. HRESULT_FROM_WIN32(dwError),
  2769. _Module.GetResourceInstance(),
  2770. IDS_E_UPGRADE_RENAME_ORIGINAL,
  2771. (LPCTSTR)strDatabase,
  2772. (LPCTSTR)strDatabase3x
  2773. );
  2774. }
  2775. if (!MoveFileEx(strDatabase4x, strDatabase, MOVEFILE_WRITE_THROUGH))
  2776. {
  2777. DWORD dwError = GetLastError();
  2778. MoveFileEx(strDatabase3x, strDatabase, MOVEFILE_WRITE_THROUGH);
  2779. DeleteFile(strDatabase4x);
  2780. AdmtThrowError(
  2781. HRESULT_FROM_WIN32(dwError),
  2782. _Module.GetResourceInstance(),
  2783. IDS_E_UPGRADE_RENAME_UPGRADED,
  2784. (LPCTSTR)strDatabase4x,
  2785. (LPCTSTR)strDatabase
  2786. );
  2787. }
  2788. }
  2789. catch (_com_error& ce)
  2790. {
  2791. AdmtThrowError(ce, _Module.GetResourceInstance(), IDS_E_UPGRADE_TO_4X);
  2792. }
  2793. catch (...)
  2794. {
  2795. AdmtThrowError(E_FAIL, _Module.GetResourceInstance(), IDS_E_UPGRADE_TO_4X);
  2796. }
  2797. }
  2798. //---------------------------------------------------------------------------------------------
  2799. // GetMigratedObjectByType : Given the type of object this function retrieves info about
  2800. // all previously migrated objects of this type. The scope of the
  2801. // search can be limited by optional ActionID (not -1) or optional
  2802. // source domain (not empty).
  2803. //---------------------------------------------------------------------------------------------
  2804. STDMETHODIMP CIManageDB::GetMigratedObjectByType(long lActionID, BSTR sSrcDomain, BSTR sType, IUnknown **ppUnk)
  2805. {
  2806. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2807. HRESULT hr = S_OK;
  2808. try
  2809. {
  2810. _RecordsetPtr pRs(__uuidof(Recordset));
  2811. _variant_t vtSource = L"MigratedObjects";
  2812. IVarSetPtr pVs = *ppUnk;
  2813. WCHAR sActionInfo[LEN_Path];
  2814. long lCnt = 0;
  2815. _bstr_t sName;
  2816. // If the type parameter is not correct then we need to return an error
  2817. if (wcslen((WCHAR*)sType) == 0)
  2818. _com_issue_error(E_INVALIDARG);
  2819. // If a valid ActionID is specified then we only return the data for that one.
  2820. // but if -1 is passed in then we return all migrated objects of the specified type.
  2821. if ( lActionID != -1 )
  2822. {
  2823. wsprintf(sActionInfo, L"Select * from MigratedObjects where ActionID = %d AND Type=\"%s\" Order by Time", lActionID, sType);
  2824. }
  2825. //else if source domain specified, get objects of the specified type from that domain
  2826. else if (wcslen((WCHAR*)sSrcDomain) != 0)
  2827. {
  2828. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomain=\"%s\" AND Type=\"%s\" Order by Time", sSrcDomain, sType);
  2829. }
  2830. else //else get all objects of the specified type
  2831. {
  2832. wsprintf(sActionInfo, L"Select * from MigratedObjects where Type=\"%s\" Order by Time", sType);
  2833. }
  2834. vtSource = _bstr_t(sActionInfo);
  2835. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2836. if (pRs->GetRecordCount() > 0)
  2837. {
  2838. pRs->MoveFirst();
  2839. while ( !pRs->EndOfFile )
  2840. {
  2841. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_ActionID));
  2842. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  2843. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Time));
  2844. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  2845. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomain));
  2846. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  2847. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetDomain));
  2848. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  2849. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceAdsPath));
  2850. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  2851. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetAdsPath));
  2852. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  2853. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_status));
  2854. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  2855. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceSamName));
  2856. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  2857. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetSamName));
  2858. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  2859. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_Type));
  2860. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  2861. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_GUID));
  2862. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  2863. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceRid));
  2864. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  2865. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_TargetRid));
  2866. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  2867. wsprintf(sActionInfo, L"MigratedObjects.%d.%s", lCnt, GET_STRING(DB_SourceDomainSid));
  2868. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  2869. pRs->MoveNext();
  2870. lCnt++;
  2871. }
  2872. pVs->put(L"MigratedObjects", lCnt);
  2873. }
  2874. else
  2875. {
  2876. hr = S_FALSE;
  2877. }
  2878. }
  2879. catch (_com_error& ce)
  2880. {
  2881. hr = ce.Error();
  2882. }
  2883. catch (...)
  2884. {
  2885. hr = E_FAIL;
  2886. }
  2887. return hr;
  2888. }
  2889. //---------------------------------------------------------------------------------------------
  2890. // GetAMigratedObjectBySidAndRid : Given a source domain Sid and account Rid this function
  2891. // retrieves info about that migrated object, if any.
  2892. //---------------------------------------------------------------------------------------------
  2893. STDMETHODIMP CIManageDB::GetAMigratedObjectBySidAndRid(BSTR sSrcDomainSid, BSTR sRid, IUnknown **ppUnk)
  2894. {
  2895. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2896. HRESULT hr = S_OK;
  2897. try
  2898. {
  2899. _RecordsetPtr pRs(__uuidof(Recordset));
  2900. _variant_t vtSource = L"MigratedObjects";
  2901. IVarSetPtr pVs = *ppUnk;
  2902. WCHAR sActionInfo[LEN_Path];
  2903. // If the type parameter is not correct then we need to return an error
  2904. if ((wcslen((WCHAR*)sSrcDomainSid) == 0) || (wcslen((WCHAR*)sRid) == 0))
  2905. _com_issue_error(E_INVALIDARG);
  2906. int nRid = _wtoi(sRid);
  2907. wsprintf(sActionInfo, L"Select * from MigratedObjects where SourceDomainSid=\"%s\" AND SourceRid=%d Order by Time", sSrcDomainSid, nRid);
  2908. vtSource = _bstr_t(sActionInfo);
  2909. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdText);
  2910. if (pRs->GetRecordCount() > 0)
  2911. {
  2912. // We want the latest move.
  2913. pRs->MoveLast();
  2914. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_ActionID));
  2915. pVs->put(sActionInfo, pRs->Fields->GetItem(L"ActionID")->Value);
  2916. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Time));
  2917. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Time")->Value);
  2918. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomain));
  2919. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomain")->Value);
  2920. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetDomain));
  2921. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetDomain")->Value);
  2922. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceAdsPath));
  2923. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceAdsPath")->Value);
  2924. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetAdsPath));
  2925. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetAdsPath")->Value);
  2926. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_status));
  2927. pVs->put(sActionInfo, pRs->Fields->GetItem(L"status")->Value);
  2928. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceSamName));
  2929. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceSamName")->Value);
  2930. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetSamName));
  2931. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetSamName")->Value);
  2932. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_Type));
  2933. pVs->put(sActionInfo, pRs->Fields->GetItem(L"Type")->Value);
  2934. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_GUID));
  2935. pVs->put(sActionInfo, pRs->Fields->GetItem(L"GUID")->Value);
  2936. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceRid));
  2937. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceRid")->Value);
  2938. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_TargetRid));
  2939. pVs->put(sActionInfo, pRs->Fields->GetItem(L"TargetRid")->Value);
  2940. wsprintf(sActionInfo, L"MigratedObjects.%s", GET_STRING(DB_SourceDomainSid));
  2941. pVs->put(sActionInfo, pRs->Fields->GetItem(L"SourceDomainSid")->Value);
  2942. }
  2943. else
  2944. {
  2945. hr = S_FALSE;
  2946. }
  2947. }
  2948. catch (_com_error& ce)
  2949. {
  2950. hr = ce.Error();
  2951. }
  2952. catch (...)
  2953. {
  2954. hr = E_FAIL;
  2955. }
  2956. return hr;
  2957. }
  2958. /*********************************************************************
  2959. * *
  2960. * Written by: Paul Thompson *
  2961. * Date: 22 MAR 2001 *
  2962. * *
  2963. * This private member function of the CIManageDB checks to see *
  2964. * if the "Description" column in the Source Accounts table has been *
  2965. * changed to "RDN". If so, then we have modified both the Source *
  2966. * and Target Accounts tables for the new form of the "Name Conflict"*
  2967. * report. *
  2968. * *
  2969. *********************************************************************/
  2970. //BEGIN NCTablesColumnsChanged
  2971. BOOL CIManageDB::NCTablesColumnsChanged(BOOL bSource)
  2972. {
  2973. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  2974. HRESULT hr = S_OK;
  2975. BOOL bFound = FALSE;
  2976. try
  2977. {
  2978. _RecordsetPtr pRs(__uuidof(Recordset));
  2979. _variant_t vtSource;
  2980. long numColumns;
  2981. long ndx = 0;
  2982. if (bSource)
  2983. vtSource = L"SourceAccounts";
  2984. else
  2985. vtSource = L"TargetAccounts";
  2986. pRs->Open(vtSource, m_vtConn, adOpenStatic, adLockOptimistic, adCmdTable);
  2987. //get the number of columns
  2988. numColumns = pRs->Fields->GetCount();
  2989. //look for new column's name in each column header
  2990. while ((ndx < numColumns) && (bFound == FALSE))
  2991. {
  2992. //get the column name
  2993. _variant_t var(ndx);
  2994. _bstr_t columnName = pRs->Fields->GetItem(var)->Name;
  2995. //if this is the Src Sid column then set return value flag to true
  2996. if (!_wcsicmp((WCHAR*)columnName, L"RDN"))
  2997. bFound = TRUE;
  2998. ndx++;
  2999. }
  3000. }
  3001. catch (_com_error& ce)
  3002. {
  3003. hr = ce.Error();
  3004. }
  3005. catch (...)
  3006. {
  3007. hr = E_FAIL;
  3008. }
  3009. return bFound;
  3010. }
  3011. //END NCTablesColumnsChanged
  3012. /*********************************************************************
  3013. * *
  3014. * Written by: Paul Thompson *
  3015. * Date: 22 MAR 2001 *
  3016. * *
  3017. * This private member function of the CIManageDB modifies *
  3018. * several of the columns in the Source and Target Accounts tables. *
  3019. * It changes several column names and one column type to support new*
  3020. * changes to the "Name Conflict" report. *
  3021. * *
  3022. *********************************************************************/
  3023. //BEGIN ChangeNCTableColumns
  3024. HRESULT CIManageDB::ChangeNCTableColumns(BOOL bSource)
  3025. {
  3026. AFX_MANAGE_STATE(AfxGetStaticModuleState())
  3027. /* local constants */
  3028. const long COLUMN_MAX_CHARS = 255;
  3029. /* local variables */
  3030. HRESULT hr = S_OK;
  3031. /* function body */
  3032. try
  3033. {
  3034. ADOX::_CatalogPtr m_pCatalog(__uuidof(ADOX::Catalog));
  3035. ADOX::_TablePtr m_pTable = NULL;
  3036. WCHAR sConnect[MAX_PATH];
  3037. WCHAR sDir[MAX_PATH];
  3038. // Get the path to the MDB file from the registry
  3039. TRegKey key;
  3040. DWORD rc = key.Open(sKeyBase);
  3041. if ( !rc )
  3042. rc = key.ValueGetStr(L"Directory", sDir, MAX_PATH);
  3043. if ( rc != 0 )
  3044. wcscpy(sDir, L"");
  3045. // Now build the connect string.
  3046. wsprintf(sConnect, L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%sprotar.mdb;", sDir);
  3047. //Open the catalog
  3048. m_pCatalog->PutActiveConnection(sConnect);
  3049. //get a pointer to the database's Source or Target Accounts Table
  3050. if (bSource)
  3051. m_pTable = m_pCatalog->Tables->Item[L"SourceAccounts"];
  3052. else
  3053. m_pTable = m_pCatalog->Tables->Item[L"TargetAccounts"];
  3054. if (m_pTable)
  3055. {
  3056. //remove the old Description column
  3057. m_pTable->Columns->Delete(L"Description");
  3058. //remove the old FullName column
  3059. m_pTable->Columns->Delete(L"FullName");
  3060. //append the RDN column to the end of the Table
  3061. m_pTable->Columns->Append(L"RDN", adVarWChar, COLUMN_MAX_CHARS);
  3062. //append the Canonical Name column to the end of the Table
  3063. m_pTable->Columns->Append(L"Canonical Name", adLongVarWChar, COLUMN_MAX_CHARS);
  3064. }
  3065. }
  3066. catch (_com_error& ce)
  3067. {
  3068. hr = ce.Error();
  3069. }
  3070. catch (...)
  3071. {
  3072. hr = E_FAIL;
  3073. }
  3074. return hr;
  3075. }
  3076. //END ChangeNCTableColumns