Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
9.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 2000
  5. //
  6. // File: dsmove.cpp
  7. //
  8. // Contents: Defines the main function and parser tables for the dsmove
  9. // command line utility
  10. //
  11. // History: 06-Sep-2000 hiteshr Created
  12. //
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "pch.h"
  16. #include "stdio.h"
  17. #include "cstrings.h"
  18. #include "usage.h"
  19. #include "movetable.h"
  20. #include "resource.h"
  21. //
  22. //Usage Table for Dsmove
  23. //
  24. UINT USAGE_DSMOVE[] =
  25. {
  26. USAGE_DSMOVE_DESCRIPTION,
  27. USAGE_DSMOVE_SYNTAX,
  28. USAGE_DSMOVE_PARAMETERS,
  29. USAGE_DSMOVE_REMARKS,
  30. USAGE_DSMOVE_EXAMPLES,
  31. USAGE_END,
  32. };
  33. //
  34. // Function Declarations
  35. //
  36. HRESULT DoMove();
  37. HRESULT DoMoveValidation();
  38. int __cdecl _tmain( VOID )
  39. {
  40. int argc;
  41. LPTOKEN pToken = NULL;
  42. HRESULT hr = S_OK;
  43. hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  44. if(FAILED(hr))
  45. {
  46. DisplayErrorMessage(g_pszDSCommandName,
  47. NULL,
  48. hr);
  49. goto exit_gracefully;
  50. }
  51. if( !GetCommandInput(&argc,&pToken) )
  52. {
  53. if(argc == 1)
  54. {
  55. DisplayMessage(USAGE_DSMOVE,TRUE);
  56. hr = E_INVALIDARG;
  57. goto exit_gracefully;
  58. }
  59. PARSE_ERROR Error;
  60. if(!ParseCmd(g_pszDSCommandName,
  61. DSMOVE_COMMON_COMMANDS,
  62. argc-1,
  63. pToken+1,
  64. USAGE_DSMOVE,
  65. &Error,
  66. TRUE))
  67. {
  68. //ParseCmd did not display any error. Error should
  69. //be handled here. Check DisplayParseError for the
  70. //cases where Error is not shown by ParseCmd
  71. if(!Error.MessageShown)
  72. {
  73. hr = E_INVALIDARG;
  74. DisplayErrorMessage(g_pszDSCommandName,
  75. NULL,
  76. hr);
  77. goto exit_gracefully;
  78. }
  79. if(Error.ErrorSource == ERROR_FROM_PARSER
  80. && Error.Error == PARSE_ERROR_HELP_SWITCH)
  81. {
  82. hr = S_OK;
  83. goto exit_gracefully;
  84. }
  85. hr = E_INVALIDARG;
  86. goto exit_gracefully;
  87. }
  88. else
  89. {
  90. hr =DoMoveValidation();
  91. if(FAILED(hr))
  92. {
  93. goto exit_gracefully;
  94. }
  95. //
  96. // Command line parsing succeeded
  97. //
  98. hr = DoMove();
  99. }
  100. }
  101. exit_gracefully:
  102. //
  103. // Display the success message
  104. //
  105. if (SUCCEEDED(hr) && !DSMOVE_COMMON_COMMANDS[eCommQuiet].bDefined)
  106. {
  107. DisplaySuccessMessage(g_pszDSCommandName,
  108. DSMOVE_COMMON_COMMANDS[eCommObjectDN].strValue);
  109. }
  110. // Free Command Array
  111. FreeCmd(DSMOVE_COMMON_COMMANDS);
  112. // Free Token
  113. if(pToken)
  114. delete []pToken;
  115. //
  116. // Uninitialize COM
  117. //
  118. CoUninitialize();
  119. return hr;
  120. }
  121. //+--------------------------------------------------------------------------
  122. //
  123. // Function: DoMoveValidation
  124. //
  125. // Synopsis: Does advanced switch dependency validation which parser cannot do.
  126. //
  127. // Arguments:
  128. //
  129. // Returns: HRESULT : S_OK if everything succeeded
  130. //
  131. // History: 07-Sep-2000 Hiteshr Created
  132. //
  133. //---------------------------------------------------------------------------
  134. HRESULT DoMoveValidation()
  135. {
  136. HRESULT hr = S_OK;
  137. // Check to be sure the server and domain switches
  138. // are mutually exclusive
  139. if (DSMOVE_COMMON_COMMANDS[eCommServer].bDefined &&
  140. DSMOVE_COMMON_COMMANDS[eCommDomain].bDefined)
  141. {
  142. hr = E_INVALIDARG;
  143. DisplayErrorMessage(g_pszDSCommandName,
  144. NULL,
  145. hr);
  146. return hr;
  147. }
  148. if(!DSMOVE_COMMON_COMMANDS[eCommNewParent].bDefined &&
  149. !DSMOVE_COMMON_COMMANDS[eCommNewName].bDefined )
  150. {
  151. hr = E_INVALIDARG;
  152. DisplayErrorMessage(g_pszDSCommandName,
  153. NULL,
  154. hr,
  155. IDS_PARENT_OR_NAME_REQUIRED);
  156. return hr;
  157. }
  158. return hr;
  159. }
  160. //+--------------------------------------------------------------------------
  161. //
  162. // Function: DoMove
  163. //
  164. // Synopsis: Finds the appropriate object in the object table and fills in
  165. // the attribute values and then applies the changes
  166. //
  167. // Arguments:
  168. //
  169. // Returns: HRESULT : S_OK if everything succeeded
  170. // E_INVALIDARG if the object entry wasn't found
  171. // Anything else is a failure code from an ADSI call
  172. //
  173. // History: 07-Sep-2000 JeffJon Created
  174. //
  175. //---------------------------------------------------------------------------
  176. HRESULT DoMove()
  177. {
  178. HRESULT hr = S_OK;
  179. PWSTR pszObjectDN = DSMOVE_COMMON_COMMANDS[eCommObjectDN].strValue;
  180. if (!pszObjectDN)
  181. {
  182. return E_INVALIDARG;
  183. }
  184. CDSCmdCredentialObject credentialObject;
  185. if (DSMOVE_COMMON_COMMANDS[eCommUserName].bDefined &&
  186. DSMOVE_COMMON_COMMANDS[eCommUserName].strValue)
  187. {
  188. credentialObject.SetUsername(DSMOVE_COMMON_COMMANDS[eCommUserName].strValue);
  189. credentialObject.SetUsingCredentials(true);
  190. }
  191. if (DSMOVE_COMMON_COMMANDS[eCommPassword].bDefined &&
  192. DSMOVE_COMMON_COMMANDS[eCommPassword].strValue)
  193. {
  194. //Security Review:pCommandArgs[eCommPassword].strValue is encrypted.
  195. //Decrypt pCommandArgs[eCommPassword].strValue and then pass it to the
  196. //credentialObject.SetPassword.
  197. //See NTRAID#NTBUG9-571544-2000/11/13-hiteshr
  198. credentialObject.SetEncryptedPassword(&DSMOVE_COMMON_COMMANDS[eCommPassword].encryptedDataBlob);
  199. credentialObject.SetUsingCredentials(true);
  200. }
  201. //
  202. // Initialize the base paths info from the command line args
  203. //
  204. CDSCmdBasePathsInfo basePathsInfo;
  205. if (DSMOVE_COMMON_COMMANDS[eCommServer].bDefined &&
  206. DSMOVE_COMMON_COMMANDS[eCommServer].strValue)
  207. {
  208. hr = basePathsInfo.InitializeFromName(credentialObject,
  209. DSMOVE_COMMON_COMMANDS[eCommServer].strValue,
  210. true);
  211. }
  212. else if (DSMOVE_COMMON_COMMANDS[eCommDomain].bDefined &&
  213. DSMOVE_COMMON_COMMANDS[eCommDomain].strValue)
  214. {
  215. hr = basePathsInfo.InitializeFromName(credentialObject,
  216. DSMOVE_COMMON_COMMANDS[eCommDomain].strValue,
  217. false);
  218. }
  219. else
  220. {
  221. hr = basePathsInfo.InitializeFromName(credentialObject, NULL, false);
  222. }
  223. if (FAILED(hr))
  224. {
  225. //
  226. // Display error message and return
  227. //
  228. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  229. return hr;
  230. }
  231. CComBSTR sbstrObjectPath;
  232. basePathsInfo.ComposePathFromDN(pszObjectDN, sbstrObjectPath);
  233. //Get The ParentObjectPath
  234. CComBSTR sbstrParentObjectPath;
  235. if(DSMOVE_COMMON_COMMANDS[eCommNewParent].bDefined &&
  236. DSMOVE_COMMON_COMMANDS[eCommNewParent].strValue )
  237. {
  238. LPWSTR szParentDN = DSMOVE_COMMON_COMMANDS[eCommNewParent].strValue;
  239. basePathsInfo.ComposePathFromDN(szParentDN, sbstrParentObjectPath);
  240. }
  241. else
  242. {
  243. CPathCracker pathCracker;
  244. CComBSTR sbstrParentDN;
  245. hr = pathCracker.GetParentDN(pszObjectDN, sbstrParentDN);
  246. if (FAILED(hr))
  247. {
  248. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  249. return hr;
  250. }
  251. basePathsInfo.ComposePathFromDN(sbstrParentDN,sbstrParentObjectPath);
  252. }
  253. //
  254. //Get the RDN for NewName. User enters the only name. We need to convert it
  255. //into cn=name or ou=name format. To do this strip the leaf node from the
  256. //objectDN and replace the string after "=" by NewName
  257. //
  258. CComBSTR sbstrNewName;
  259. if(DSMOVE_COMMON_COMMANDS[eCommNewName].bDefined &&
  260. DSMOVE_COMMON_COMMANDS[eCommNewName].strValue )
  261. {
  262. CPathCracker pathCracker;
  263. CComBSTR sbstrLeafNode;
  264. hr = pathCracker.GetObjectRDNFromDN(pszObjectDN,sbstrLeafNode);
  265. if (FAILED(hr))
  266. {
  267. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  268. return hr;
  269. }
  270. sbstrNewName.Append(sbstrLeafNode,3);
  271. //Enclose the name in quotes to allow for special names like
  272. //test1,ou=ou1 NTRAID#NTBUG9-275556-2000/11/13-hiteshr
  273. sbstrNewName.Append(L"\"");
  274. sbstrNewName.Append(DSMOVE_COMMON_COMMANDS[eCommNewName].strValue);
  275. sbstrNewName.Append(L"\"");
  276. }
  277. //Get IADsContainer pointer
  278. CComPtr<IADsContainer> spDsContainer;
  279. hr = DSCmdOpenObject(credentialObject,
  280. sbstrParentObjectPath,
  281. IID_IADsContainer,
  282. (void**)&spDsContainer,
  283. true);
  284. if (FAILED(hr))
  285. {
  286. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  287. return hr;
  288. }
  289. IDispatch * pDispObj = NULL;
  290. hr = spDsContainer->MoveHere(sbstrObjectPath,
  291. sbstrNewName,
  292. &pDispObj);
  293. if (FAILED(hr))
  294. {
  295. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  296. return hr;
  297. }
  298. if(pDispObj)
  299. {
  300. pDispObj->Release();
  301. pDispObj = NULL;
  302. }
  303. return hr;
  304. }