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.

439 lines
17 KiB

  1. //---------------------------------------------------------------------------
  2. // DisableTarget.cpp
  3. //
  4. // Comment: This is a COM object extension for the MCS DCTAccountReplicator.
  5. // This object implements the IExtendAccountMigration interface. In
  6. // the process method this object disables the Source and the Target
  7. // accounts depending on the settings in the VarSet.
  8. //
  9. // (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  10. //
  11. // Proprietary and confidential to Mission Critical Software, Inc.
  12. //---------------------------------------------------------------------------
  13. #include "stdafx.h"
  14. #include "ResStr.h"
  15. #include <lm.h>
  16. #include <activeds.h>
  17. #include "AcctDis.h"
  18. #include "DisAcct.h"
  19. #include "ARExt.h"
  20. #include "ARExt_i.c"
  21. #include "ErrDCT.hpp"
  22. //#import "\bin\McsVarSetMin.tlb" no_namespace
  23. //#import "\bin\DBManager.tlb" no_namespace
  24. #import "VarSet.tlb" no_namespace rename("property", "aproperty")
  25. #import "DBMgr.tlb" no_namespace
  26. const int LEN_Path = 255;
  27. StringLoader gString;
  28. /////////////////////////////////////////////////////////////////////////////
  29. // CDisableTarget
  30. //---------------------------------------------------------------------------
  31. // Get and set methods for the properties.
  32. //---------------------------------------------------------------------------
  33. STDMETHODIMP CDisableTarget::get_sName(BSTR *pVal)
  34. {
  35. *pVal = m_sName;
  36. return S_OK;
  37. }
  38. STDMETHODIMP CDisableTarget::put_sName(BSTR newVal)
  39. {
  40. m_sName = newVal;
  41. return S_OK;
  42. }
  43. STDMETHODIMP CDisableTarget::get_sDesc(BSTR *pVal)
  44. {
  45. *pVal = m_sDesc;
  46. return S_OK;
  47. }
  48. STDMETHODIMP CDisableTarget::put_sDesc(BSTR newVal)
  49. {
  50. m_sDesc = newVal;
  51. return S_OK;
  52. }
  53. //---------------------------------------------------------------------------
  54. // ProcessObject : This method doesn't do anything.
  55. //---------------------------------------------------------------------------
  56. STDMETHODIMP CDisableTarget::PreProcessObject(
  57. IUnknown *pSource, //in- Pointer to the source AD object
  58. IUnknown *pTarget, //in- Pointer to the target AD object
  59. IUnknown *pMainSettings, //in- Varset filled with the settings supplied by user
  60. IUnknown **ppPropsToSet //in,out - Varset filled with Prop-Value pairs that will be set
  61. // once all extension objects are executed.
  62. )
  63. {
  64. // Check if the object is of user type. if not then there is no point in disabling that account.
  65. IVarSetPtr pVs = pMainSettings;
  66. _bstr_t sType = pVs->get(GET_BSTR(DCTVS_CopiedAccount_Type));
  67. if (!sType.length())
  68. return HRESULT_FROM_WIN32(ERROR_NOT_ENOUGH_MEMORY);
  69. if (UStrICmp((WCHAR*)sType,L"user"))
  70. return S_OK;
  71. if ( pSource )
  72. {
  73. // HRESULT hr = S_OK;
  74. _variant_t vtExp;
  75. _variant_t vtFlag;
  76. _bstr_t sSourceType;
  77. IIManageDBPtr pDb = pVs->get(GET_BSTR(DCTVS_DBManager));
  78. sSourceType = pVs->get(GET_BSTR(DCTVS_CopiedAccount_Type));
  79. if ( !_wcsicmp((WCHAR*) sSourceType, L"user") )
  80. {
  81. // Get the expiration date and put it into the AR Node.
  82. _bstr_t sSam = pVs->get(GET_BSTR(DCTVS_CopiedAccount_SourceSam));
  83. _bstr_t sComp = pVs->get(GET_BSTR(DCTVS_Options_SourceServer));
  84. USER_INFO_3 * pInfo = NULL;
  85. DWORD rc = NetUserGetInfo((WCHAR*) sComp, (WCHAR*)sSam, 3, (LPBYTE*)&pInfo);
  86. if ( !rc )
  87. {
  88. vtExp = (long)pInfo->usri3_acct_expires;
  89. pVs->put(GET_BSTR(DCTVS_CopiedAccount_ExpDate), vtExp);
  90. // Get the ControlFlag and store it into the AR Node.
  91. vtFlag = (long)pInfo->usri3_flags;
  92. pVs->put(GET_BSTR(DCTVS_CopiedAccount_UserFlags), vtFlag);
  93. if ( pInfo ) NetApiBufferFree(pInfo);
  94. }
  95. }
  96. pDb->raw_SaveUserProps(pMainSettings);
  97. }
  98. return S_OK;
  99. }
  100. //---------------------------------------------------------------------------
  101. // ProcessObject : This method checks in varset if it needs to disable any
  102. // accounts. If it does then it disables those accounts.
  103. //---------------------------------------------------------------------------
  104. STDMETHODIMP CDisableTarget::ProcessObject(
  105. IUnknown *pSource, //in- Pointer to the source AD object
  106. IUnknown *pTarget, //in- Pointer to the target AD object
  107. IUnknown *pMainSettings, //in- Varset filled with the settings supplied by user
  108. IUnknown **ppPropsToSet //in,out - Varset filled with Prop-Value pairs that will be set
  109. // once all extension objects are executed.
  110. )
  111. {
  112. IVarSetPtr pVarSet = pMainSettings;
  113. _variant_t var;
  114. DWORD paramErr;
  115. USER_INFO_3 * info = NULL;
  116. long rc;
  117. WCHAR strDomain[LEN_Path];
  118. WCHAR strAcct[LEN_Path];
  119. HRESULT hr = S_OK;
  120. TErrorDct err;
  121. WCHAR fileName[LEN_Path];
  122. BOOL bDisableSource = FALSE;
  123. BOOL bExpireSource = FALSE;
  124. _bstr_t temp;
  125. time_t expireTime = 0;
  126. _bstr_t bstrSameForest;
  127. BOOL bSameAsSource = FALSE;
  128. BOOL bDisableTarget = FALSE;
  129. BOOL bGotSrcState = FALSE;
  130. BOOL bSrcDisabled = FALSE;
  131. bstrSameForest = pVarSet->get(GET_BSTR(DCTVS_Options_IsIntraforest));
  132. if (! UStrICmp((WCHAR*)bstrSameForest,GET_STRING(IDS_YES)) )
  133. {
  134. // in the intra-forest case, we are moving the user accounts, not
  135. // copying them, so these disabling/expiring options don't make any sense
  136. return S_OK;
  137. }
  138. // Get the Error log filename from the Varset
  139. var = pVarSet->get(GET_BSTR(DCTVS_Options_Logfile));
  140. wcscpy(fileName, (WCHAR*)V_BSTR(&var));
  141. VariantInit(&var);
  142. // Open the error log
  143. err.LogOpen(fileName, 1);
  144. // Check if the object is of user type. if not then there is no point in disabling that account.
  145. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_Type));
  146. if ( UStrICmp(var.bstrVal,L"user"))
  147. {
  148. return S_OK;
  149. }
  150. //set flags based on user selections
  151. temp = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_DisableSourceAccounts));
  152. if ( ! UStrICmp(temp,GET_STRING(IDS_YES)) )
  153. {
  154. bDisableSource = TRUE;
  155. }
  156. temp = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_DisableCopiedAccounts));
  157. if ( ! UStrICmp(temp,GET_STRING(IDS_YES)) )
  158. {
  159. bDisableTarget = TRUE;
  160. }
  161. temp = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_TgtStateSameAsSrc));
  162. if ( ! UStrICmp(temp,GET_STRING(IDS_YES)) )
  163. {
  164. bSameAsSource = TRUE;
  165. }
  166. /* process the source account */
  167. //if expire source accounts was set, retrieve the expire time, now given to us in
  168. //number of days from now
  169. temp = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_ExpireSourceAccounts));
  170. if ( temp.length() )
  171. {
  172. long oneDay = 24 * 60 * 60; // number of seconds in 1 day
  173. //get days until expire
  174. long lExpireDays = _wtol(temp);
  175. //get the current time
  176. time_t currentTime = time(NULL);
  177. //convert current time to local time
  178. struct tm * convtm;
  179. convtm = localtime(&currentTime);
  180. //rollback to this morning
  181. convtm->tm_hour = 0;
  182. convtm->tm_min = 0;
  183. convtm->tm_sec = 0;
  184. //convert this time back to GMT
  185. expireTime = mktime(convtm);
  186. //move forward to tonight at midnight
  187. expireTime += oneDay;
  188. //now add the desired number of days
  189. expireTime += lExpireDays * oneDay;
  190. bExpireSource = TRUE;
  191. }
  192. //get source account state
  193. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_SourceSam));
  194. wcscpy(strAcct, (WCHAR*)V_BSTR(&var));
  195. var = pVarSet->get(GET_BSTR(DCTVS_Options_SourceServer));
  196. wcscpy(strDomain, (WCHAR*)V_BSTR(&var));
  197. // we will use the net APIs to disable the source account
  198. rc = NetUserGetInfo(strDomain, strAcct, 3, (LPBYTE *)&info);
  199. if (rc != 0)
  200. {
  201. hr = S_FALSE;
  202. err.SysMsgWrite(ErrW, rc, DCT_MSG_DISABLE_SOURCE_FAILED_SD, strAcct, rc);
  203. }
  204. else
  205. {
  206. //set current source account state
  207. if (info->usri3_flags & UF_ACCOUNTDISABLE)
  208. bSrcDisabled = TRUE;
  209. //also save the flags in the varset to be used in setpass ARExt
  210. _variant_t vtFlag = (long)info->usri3_flags;
  211. pVarSet->put(GET_BSTR(DCTVS_CopiedAccount_UserFlags), vtFlag);
  212. //disable the source account if requested
  213. if (bDisableSource)
  214. {
  215. // Set the disable flag
  216. info->usri3_flags |= UF_ACCOUNTDISABLE;
  217. err.MsgWrite(0,DCT_MSG_SOURCE_DISABLED_S, strAcct);
  218. }
  219. //expire the account in given timeframe, if requested
  220. if ( bExpireSource )
  221. {
  222. if (((time_t)info->usri3_acct_expires == TIMEQ_FOREVER)
  223. || ((time_t)info->usri3_acct_expires > expireTime))
  224. {
  225. info->usri3_acct_expires = (DWORD)expireTime;
  226. err.MsgWrite(0,DCT_MSG_SOURCE_EXPIRED_S,strAcct);
  227. }
  228. else
  229. err.MsgWrite(0, DCT_MSG_SOURCE_EXPIRATION_EARLY_S, strAcct);
  230. }
  231. //if changed, set the source information into the Domain.
  232. if (bDisableSource || bExpireSource)
  233. {
  234. rc = NetUserSetInfo(strDomain,strAcct, 3, (LPBYTE)info, &paramErr);
  235. if ( rc )
  236. err.SysMsgWrite(0,rc,DCT_MSG_ACCOUNT_DISABLE_OR_EXPIRE_FAILED_SD,strAcct,rc);
  237. }
  238. NetApiBufferFree((LPVOID) info);
  239. }//if got current src account state
  240. /* process the target account */
  241. //get the target state
  242. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_TargetSam));
  243. wcscpy(strAcct, (WCHAR*)V_BSTR(&var));
  244. var = pVarSet->get(GET_BSTR(DCTVS_Options_TargetServer));
  245. wcscpy(strDomain, (WCHAR*)V_BSTR(&var));
  246. // we will use the net APIs to disable the target account
  247. rc = NetUserGetInfo(strDomain, strAcct, 3, (LPBYTE *)&info);
  248. if (rc != 0)
  249. {
  250. hr = S_FALSE;
  251. err.SysMsgWrite(ErrW, rc, DCT_MSG_DISABLE_TARGET_FAILED_SD, strAcct, rc);
  252. }
  253. else
  254. {
  255. //disable the target if requested
  256. if (bDisableTarget)
  257. {
  258. // Set the disable flag
  259. info->usri3_flags |= UF_ACCOUNTDISABLE;
  260. // Set the information into the Domain.
  261. rc = NetUserSetInfo(strDomain, strAcct, 3, (LPBYTE)info, &paramErr);
  262. err.MsgWrite(0,DCT_MSG_TARGET_DISABLED_S, strAcct);
  263. }
  264. //else make target same state as source was
  265. else if (bSameAsSource)
  266. {
  267. //if the source was disabled, disable the target
  268. if (bSrcDisabled)
  269. {
  270. //disable the target
  271. info->usri3_flags |= UF_ACCOUNTDISABLE;
  272. // Set the information into the Domain.
  273. rc = NetUserSetInfo( strDomain, strAcct, 3, (LPBYTE)info, &paramErr);
  274. err.MsgWrite(0,DCT_MSG_TARGET_DISABLED_S, strAcct);
  275. }
  276. else //else make sure target is enabled and not set to expire
  277. {
  278. info->usri3_flags &= ~UF_ACCOUNTDISABLE;
  279. rc = NetUserSetInfo(strDomain,strAcct,3,(LPBYTE)info,&paramErr);
  280. }
  281. }
  282. else //else make sure target is enabled and not set to expire
  283. {
  284. info->usri3_flags &= ~UF_ACCOUNTDISABLE;
  285. rc = NetUserSetInfo(strDomain,strAcct,3,(LPBYTE)info,&paramErr);
  286. }
  287. NetApiBufferFree((LPVOID) info);
  288. }
  289. return hr;
  290. }
  291. //---------------------------------------------------------------------------
  292. // ProcessUndo : This function Enables the accounts that were previously
  293. // disabled..
  294. //---------------------------------------------------------------------------
  295. STDMETHODIMP CDisableTarget::ProcessUndo(
  296. IUnknown *pSource, //in- Pointer to the source AD object
  297. IUnknown *pTarget, //in- Pointer to the target AD object
  298. IUnknown *pMainSettings, //in- Varset filled with the settings supplied by user
  299. IUnknown **ppPropsToSet //in,out - Varset filled with Prop-Value pairs that will be set
  300. // once all extension objects are executed.
  301. )
  302. {
  303. IVarSetPtr pVarSet = pMainSettings;
  304. IIManageDBPtr pDb = pVarSet->get(GET_BSTR(DCTVS_DBManager));
  305. _variant_t var;
  306. DWORD paramErr;
  307. USER_INFO_3 * info;
  308. long rc;
  309. WCHAR strDomain[LEN_Path];
  310. WCHAR strAcct[LEN_Path];
  311. HRESULT hr = S_OK;
  312. TErrorDct err;
  313. IUnknown * pUnk = NULL;
  314. _bstr_t sSourceName, sSourceDomain, sTgtDomain;
  315. WCHAR fileName[LEN_Path];
  316. IVarSetPtr pVs(__uuidof(VarSet));
  317. _variant_t vtExp, vtFlag;
  318. _bstr_t sDomainName = pVarSet->get(GET_BSTR(DCTVS_Options_SourceDomain));
  319. pVs->QueryInterface(IID_IUnknown, (void**)&pUnk);
  320. sSourceName = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_SourceSam));
  321. sSourceDomain = pVarSet->get(GET_BSTR(DCTVS_Options_SourceDomain));
  322. sTgtDomain = pVarSet->get(GET_BSTR(DCTVS_Options_TargetDomain));
  323. hr = pDb->raw_GetUserProps(sSourceDomain, sSourceName, &pUnk);
  324. if ( pUnk ) pUnk->Release();
  325. if ( hr == S_OK )
  326. {
  327. vtExp = pVs->get(GET_BSTR(DCTVS_CopiedAccount_ExpDate));
  328. vtFlag = pVs->get(GET_BSTR(DCTVS_CopiedAccount_UserFlags));
  329. }
  330. // Get the Error log filename from the Varset
  331. var = pVarSet->get(GET_BSTR(DCTVS_Options_Logfile));
  332. wcscpy(fileName, (WCHAR*)V_BSTR(&var));
  333. VariantInit(&var);
  334. // Open the error log
  335. err.LogOpen(fileName, 1);
  336. // Check if the object is of user type. if not then there is no point in disabling that account.
  337. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_Type));
  338. if ( _wcsicmp((WCHAR*)V_BSTR(&var),L"user") != 0 )
  339. return S_OK;
  340. _bstr_t sDis = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_DisableSourceAccounts));
  341. _bstr_t sExp = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_ExpireSourceAccounts));
  342. if ( !wcscmp((WCHAR*)sDis,GET_STRING(IDS_YES)) || sExp.length() )
  343. {
  344. // Reset the flag and the expiration date for the source account.
  345. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_SourceSam));
  346. wcscpy(strAcct, (WCHAR*)V_BSTR(&var));
  347. var = pVarSet->get(GET_BSTR(DCTVS_Options_SourceServer));
  348. wcscpy(strDomain, (WCHAR*)V_BSTR(&var));
  349. // we will use the net APIs to disable the source account
  350. rc = NetUserGetInfo( strDomain, strAcct, 3, (LPBYTE *)&info);
  351. if (rc != 0)
  352. {
  353. hr = S_FALSE;
  354. err.SysMsgWrite(ErrW, rc, DCT_MSG_ENABLE_SOURCE_FAILED_SD, strAcct, rc);
  355. }
  356. else
  357. {
  358. // Set the disable flag
  359. info->usri3_flags = vtFlag.lVal;
  360. info->usri3_acct_expires = vtExp.lVal;
  361. // Set the information into the Domain.
  362. rc = NetUserSetInfo(strDomain,strAcct, 3, (LPBYTE)info, &paramErr);
  363. NetApiBufferFree((LPVOID) info);
  364. err.MsgWrite(0,DCT_MSG_SOURCE_ENABLED_S, (WCHAR*)strAcct);
  365. }
  366. }
  367. // Process the target account if the Varset is set
  368. var = pVarSet->get(GET_BSTR(DCTVS_AccountOptions_DisableCopiedAccounts));
  369. if ( (var.vt == VT_BSTR) && (_wcsicmp((WCHAR*)V_BSTR(&var),GET_STRING(IDS_YES)) == 0) )
  370. {
  371. var = pVarSet->get(GET_BSTR(DCTVS_CopiedAccount_TargetSam));
  372. wcscpy(strAcct, (WCHAR*)V_BSTR(&var));
  373. var = pVarSet->get(GET_BSTR(DCTVS_Options_TargetServer));
  374. wcscpy(strDomain, (WCHAR*)V_BSTR(&var));
  375. // we will use the net APIs to disable the target account
  376. rc = NetUserGetInfo( strDomain, strAcct, 3, (LPBYTE *)&info);
  377. if (rc != 0)
  378. {
  379. hr = S_FALSE;
  380. err.SysMsgWrite(ErrW, rc, DCT_MSG_ENABLE_TARGET_FAILED_SD, strAcct, rc);
  381. }
  382. else
  383. {
  384. // Set the disable flag
  385. info->usri3_flags &= !(UF_ACCOUNTDISABLE);
  386. // Set the information into the Domain.
  387. rc = NetUserSetInfo( strDomain, strAcct, 3, (LPBYTE)info, &paramErr);
  388. NetApiBufferFree((LPVOID) info);
  389. err.MsgWrite(0,DCT_MSG_TARGET_ENABLED_S, strAcct);
  390. }
  391. }
  392. WCHAR sFilter[5000];
  393. wsprintf(sFilter, L"SourceDomain='%s' and SourceSam='%s'", (WCHAR*)sDomainName, strAcct);
  394. _variant_t Filter = sFilter;
  395. pDb->raw_ClearTable(L"UserProps", Filter);
  396. err.LogClose();
  397. return hr;
  398. }