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.

284 lines
7.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. //
  21. // Function Declarations
  22. //
  23. HRESULT DoMove();
  24. HRESULT DoMoveValidation();
  25. int __cdecl _tmain( VOID )
  26. {
  27. int argc;
  28. LPTOKEN pToken = NULL;
  29. HRESULT hr = S_OK;
  30. hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
  31. if(FAILED(hr))
  32. goto exit_gracefully;
  33. if( !GetCommandInput(&argc,&pToken) )
  34. {
  35. if(argc == 1)
  36. {
  37. DisplayMessage(USAGE_DSMOVE);
  38. hr = E_INVALIDARG;
  39. goto exit_gracefully;
  40. }
  41. PARSE_ERROR Error;
  42. if(!ParseCmd(DSMOVE_COMMON_COMMANDS,
  43. argc-1,
  44. pToken+1,
  45. USAGE_DSMOVE,
  46. &Error,
  47. TRUE))
  48. {
  49. if(Error.ErrorSource == ERROR_FROM_PARSER
  50. && Error.Error == PARSE_ERROR_HELP_SWITCH)
  51. {
  52. hr = S_OK;
  53. goto exit_gracefully;
  54. }
  55. DisplayMessage(USAGE_DSMOVE);
  56. hr = E_INVALIDARG;
  57. }
  58. else
  59. {
  60. hr =DoMoveValidation();
  61. if(FAILED(hr))
  62. {
  63. DisplayMessage(USAGE_DSMOVE);
  64. goto exit_gracefully;
  65. }
  66. //
  67. // Command line parsing succeeded
  68. //
  69. hr = DoMove();
  70. }
  71. }
  72. exit_gracefully:
  73. //
  74. // Display the success message
  75. //
  76. if (SUCCEEDED(hr) && !DSMOVE_COMMON_COMMANDS[eCommQuiet].bDefined)
  77. {
  78. DisplaySuccessMessage(g_pszDSCommandName,
  79. DSMOVE_COMMON_COMMANDS[eCommObjectDN].strValue);
  80. }
  81. // Free Command Array
  82. FreeCmd(DSMOVE_COMMON_COMMANDS);
  83. // Free Token
  84. if(pToken)
  85. delete []pToken;
  86. //
  87. // Uninitialize COM
  88. //
  89. CoUninitialize();
  90. return hr;
  91. }
  92. //+--------------------------------------------------------------------------
  93. //
  94. // Function: DoMoveValidation
  95. //
  96. // Synopsis: Does advanced switch dependency validation which parser cannot do.
  97. //
  98. // Arguments:
  99. //
  100. // Returns: HRESULT : S_OK if everything succeeded
  101. //
  102. // History: 07-Sep-2000 Hiteshr Created
  103. //
  104. //---------------------------------------------------------------------------
  105. HRESULT DoMoveValidation()
  106. {
  107. HRESULT hr = S_OK;
  108. if(!DSMOVE_COMMON_COMMANDS[eCommNewParent].bDefined &&
  109. !DSMOVE_COMMON_COMMANDS[eCommNewName].bDefined )
  110. {
  111. return E_INVALIDARG;
  112. }
  113. return hr;
  114. }
  115. //+--------------------------------------------------------------------------
  116. //
  117. // Function: DoMove
  118. //
  119. // Synopsis: Finds the appropriate object in the object table and fills in
  120. // the attribute values and then applies the changes
  121. //
  122. // Arguments:
  123. //
  124. // Returns: HRESULT : S_OK if everything succeeded
  125. // E_INVALIDARG if the object entry wasn't found
  126. // Anything else is a failure code from an ADSI call
  127. //
  128. // History: 07-Sep-2000 JeffJon Created
  129. //
  130. //---------------------------------------------------------------------------
  131. HRESULT DoMove()
  132. {
  133. HRESULT hr = S_OK;
  134. PWSTR pszObjectDN = DSMOVE_COMMON_COMMANDS[eCommObjectDN].strValue;
  135. if (!pszObjectDN)
  136. {
  137. return E_INVALIDARG;
  138. }
  139. CDSCmdCredentialObject credentialObject;
  140. if (DSMOVE_COMMON_COMMANDS[eCommUserName].bDefined &&
  141. DSMOVE_COMMON_COMMANDS[eCommUserName].strValue)
  142. {
  143. credentialObject.SetUsername(DSMOVE_COMMON_COMMANDS[eCommUserName].strValue);
  144. credentialObject.SetUsingCredentials(true);
  145. }
  146. if (DSMOVE_COMMON_COMMANDS[eCommPassword].bDefined &&
  147. DSMOVE_COMMON_COMMANDS[eCommPassword].strValue)
  148. {
  149. credentialObject.SetPassword(DSMOVE_COMMON_COMMANDS[eCommPassword].strValue);
  150. credentialObject.SetUsingCredentials(true);
  151. }
  152. //
  153. // Initialize the base paths info from the command line args
  154. //
  155. CDSCmdBasePathsInfo basePathsInfo;
  156. if (DSMOVE_COMMON_COMMANDS[eCommServer].bDefined &&
  157. DSMOVE_COMMON_COMMANDS[eCommServer].strValue)
  158. {
  159. hr = basePathsInfo.InitializeFromName(credentialObject,
  160. DSMOVE_COMMON_COMMANDS[eCommServer].strValue,
  161. true);
  162. }
  163. else if (DSMOVE_COMMON_COMMANDS[eCommDomain].bDefined &&
  164. DSMOVE_COMMON_COMMANDS[eCommDomain].strValue)
  165. {
  166. hr = basePathsInfo.InitializeFromName(credentialObject,
  167. DSMOVE_COMMON_COMMANDS[eCommDomain].strValue,
  168. false);
  169. }
  170. else
  171. {
  172. hr = basePathsInfo.InitializeFromName(credentialObject, NULL, false);
  173. }
  174. if (FAILED(hr))
  175. {
  176. //
  177. // Display error message and return
  178. //
  179. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  180. return hr;
  181. }
  182. CComBSTR sbstrObjectPath;
  183. basePathsInfo.ComposePathFromDN(pszObjectDN, sbstrObjectPath);
  184. //Get The ParentObjectPath
  185. CComBSTR sbstrParentObjectPath;
  186. if(DSMOVE_COMMON_COMMANDS[eCommNewParent].bDefined &&
  187. DSMOVE_COMMON_COMMANDS[eCommNewParent].strValue )
  188. {
  189. LPWSTR szParentDN = DSMOVE_COMMON_COMMANDS[eCommNewParent].strValue;
  190. basePathsInfo.ComposePathFromDN(szParentDN, sbstrParentObjectPath);
  191. }
  192. else
  193. {
  194. CPathCracker pathCracker;
  195. CComBSTR sbstrParentDN;
  196. hr = pathCracker.GetParentDN(pszObjectDN, sbstrParentDN);
  197. if (FAILED(hr))
  198. {
  199. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  200. return hr;
  201. }
  202. basePathsInfo.ComposePathFromDN(sbstrParentDN,sbstrParentObjectPath);
  203. }
  204. //
  205. //Get the RDN for NewName. User enters the only name. We need to convert it
  206. //into cn=name or ou=name format. To do this strip the leaf node from the
  207. //objectDN and replace the string after "=" by NewName
  208. //
  209. CComBSTR sbstrNewName;
  210. if(DSMOVE_COMMON_COMMANDS[eCommNewName].bDefined &&
  211. DSMOVE_COMMON_COMMANDS[eCommNewName].strValue )
  212. {
  213. CPathCracker pathCracker;
  214. CComBSTR sbstrLeafNode;
  215. hr = pathCracker.GetObjectRDNFromDN(pszObjectDN,sbstrLeafNode);
  216. if (FAILED(hr))
  217. {
  218. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  219. return hr;
  220. }
  221. sbstrNewName.Append(sbstrLeafNode,3);
  222. //Enclose the name in quotes to allow for special names like
  223. //test1,ou=ou1 NTRAID#NTBUG9-275556-2000/11/13-hiteshr
  224. sbstrNewName.Append(L"\"");
  225. sbstrNewName.Append(DSMOVE_COMMON_COMMANDS[eCommNewName].strValue);
  226. sbstrNewName.Append(L"\"");
  227. }
  228. //Get IADsContainer pointer
  229. CComPtr<IADsContainer> spDsContainer;
  230. hr = DSCmdOpenObject(credentialObject,
  231. sbstrParentObjectPath,
  232. IID_IADsContainer,
  233. (void**)&spDsContainer,
  234. true);
  235. if (FAILED(hr))
  236. {
  237. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  238. return hr;
  239. }
  240. IDispatch * pDispObj = NULL;
  241. hr = spDsContainer->MoveHere(sbstrObjectPath,
  242. sbstrNewName,
  243. &pDispObj);
  244. if (FAILED(hr))
  245. {
  246. DisplayErrorMessage(g_pszDSCommandName, pszObjectDN, hr);
  247. return hr;
  248. }
  249. if(pDispObj)
  250. {
  251. pDispObj->Release();
  252. pDispObj = NULL;
  253. }
  254. return hr;
  255. }