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.

332 lines
7.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: user.cxx
  7. //
  8. // Contents: User operations
  9. //
  10. // History: 08-06-96 t-danal created for oledscmd from chgpass, setpass
  11. //
  12. //----------------------------------------------------------------------------
  13. #include "main.hxx"
  14. #include "macro.hxx"
  15. #include "sconv.hxx"
  16. //
  17. // Dispatch Table Defs
  18. //
  19. #include "dispdef.hxx"
  20. DEFEXEC(ExecUserChgPass);
  21. DEFEXEC(ExecUserSetPass);
  22. DEFEXEC(ExecUserGroups);
  23. DEFDISPTABLE(DispTable) = {
  24. {"chgpass", NULL, ExecUserChgPass},
  25. {"setpass", NULL, ExecUserSetPass},
  26. {"groups", NULL, ExecUserGroups}
  27. };
  28. DEFDISPSIZE(nDispTable, DispTable);
  29. //
  30. // Private defines
  31. //
  32. #define MAX_ADS_ENUM 100
  33. //
  34. // Local functions
  35. //
  36. HRESULT
  37. ChangePassword(
  38. LPWSTR szUserName,
  39. LPWSTR szOldPassword,
  40. LPWSTR szNewPassword
  41. );
  42. HRESULT
  43. SetPassword(
  44. LPWSTR szUserName,
  45. LPWSTR szNewPassword
  46. );
  47. HRESULT
  48. SetPassword(
  49. LPWSTR szUserName,
  50. LPWSTR szNewPassword
  51. );
  52. //
  53. // Local function definitions
  54. //
  55. HRESULT
  56. ChangePassword(
  57. LPWSTR szUserName,
  58. LPWSTR szOldPassword,
  59. LPWSTR szNewPassword
  60. )
  61. {
  62. HRESULT hr;
  63. IADsUser * pADsUser = NULL;
  64. hr = ADsGetObject(
  65. szUserName,
  66. IID_IADsUser,
  67. (void **)&pADsUser
  68. );
  69. BAIL_ON_FAILURE(hr);
  70. hr = pADsUser->ChangePassword(
  71. szOldPassword,
  72. szNewPassword
  73. );
  74. pADsUser->Release();
  75. BAIL_ON_FAILURE(hr);
  76. printf("Successfully changed password\n");
  77. return(S_OK);
  78. error:
  79. printf("Failed to change password\n");
  80. return(hr);
  81. }
  82. HRESULT
  83. SetPassword(
  84. LPWSTR szUserName,
  85. LPWSTR szNewPassword
  86. )
  87. {
  88. HRESULT hr;
  89. IADsUser * pADsUser = NULL;
  90. hr = ADsGetObject(
  91. szUserName,
  92. IID_IADsUser,
  93. (void **)&pADsUser
  94. );
  95. BAIL_ON_FAILURE(hr);
  96. hr = pADsUser->SetPassword(
  97. szNewPassword
  98. );
  99. pADsUser->Release();
  100. BAIL_ON_FAILURE(hr);
  101. printf("Successfully set password\n");
  102. return(S_OK);
  103. error:
  104. printf("Failed to set password\n");
  105. return(hr);
  106. }
  107. HRESULT
  108. EnumUserGroups(
  109. LPWSTR szLocation
  110. )
  111. {
  112. HRESULT hr;
  113. IADsUser* pUser = NULL;
  114. IADsMembers* pGroups = NULL;
  115. IEnumVARIANT* penum = NULL;
  116. IUnknown * pUnk = NULL;
  117. DWORD dwObjects = 0, i = 0;
  118. BOOL fContinue = TRUE;
  119. ULONG cElementFetched = 0L;
  120. VARIANT VariantArray[MAX_ADS_ENUM];
  121. IADs *pObject = NULL;
  122. IDispatch *pDispatch = NULL;
  123. BSTR bstrName = NULL;
  124. hr = ADsGetObject(
  125. szLocation,
  126. IID_IADsUser,
  127. (void **)&pUser
  128. );
  129. BAIL_ON_FAILURE(hr);
  130. hr = pUser->Groups(&pGroups);
  131. BAIL_ON_FAILURE(hr);
  132. hr = pGroups->get__NewEnum(&pUnk);
  133. BAIL_ON_FAILURE(hr);
  134. hr = pUnk->QueryInterface(IID_IEnumVARIANT, (void **)&penum);
  135. BAIL_ON_FAILURE(hr);
  136. hr = pUser->get_ADsPath(&bstrName);
  137. BAIL_ON_FAILURE(hr);
  138. printf("User: %ws\n", bstrName);
  139. FREE_BSTR(bstrName);
  140. while (fContinue) {
  141. hr = ADsEnumerateNext(
  142. penum,
  143. MAX_ADS_ENUM,
  144. VariantArray,
  145. &cElementFetched
  146. );
  147. if (hr == S_FALSE)
  148. fContinue = FALSE;
  149. for (i = 0; i < cElementFetched; i++ ) {
  150. pDispatch = VariantArray[i].pdispVal;
  151. hr = pDispatch->QueryInterface(IID_IADs,
  152. (VOID **) &pObject) ;
  153. BAIL_ON_FAILURE(hr);
  154. hr = pObject->get_ADsPath(&bstrName) ;
  155. BAIL_ON_FAILURE(hr);
  156. printf("\tIs in Group: %ws\n", bstrName);
  157. FREE_BSTR(bstrName);
  158. FREE_INTERFACE(pObject);
  159. FREE_INTERFACE(pDispatch);
  160. }
  161. memset(VariantArray, 0, sizeof(VARIANT)*MAX_ADS_ENUM);
  162. dwObjects += cElementFetched;
  163. }
  164. printf("Total Number of Objects enumerated is %d\n", dwObjects);
  165. hr = S_OK;
  166. error:
  167. FREE_INTERFACE(pUser);
  168. FREE_INTERFACE(pGroups);
  169. FREE_INTERFACE(penum);
  170. FREE_INTERFACE(pUnk);
  171. FREE_INTERFACE(pObject);
  172. FREE_INTERFACE(pDispatch);
  173. FREE_BSTR(bstrName);
  174. return(hr);
  175. }
  176. //
  177. // Exec function definitions
  178. //
  179. int
  180. ExecUser(char *szProgName, char *szAction, int argc, char * argv[])
  181. {
  182. if (!argc) {
  183. PrintUsage(szProgName, szAction, DispTable, nDispTable);
  184. return(1);
  185. }
  186. char *szPrevActions = szAction;
  187. szAction = argv[0];
  188. argc--;
  189. argv++;
  190. if (DoHelp(szProgName,
  191. szPrevActions, szAction, NULL,
  192. DispTable, nDispTable,
  193. NULL))
  194. return 0;
  195. return DispatchExec(DispTable, nDispTable,
  196. szProgName,
  197. szPrevActions, szAction,
  198. argc, argv);
  199. }
  200. int
  201. ExecUserChgPass(char* szProgName, char* szAction, int argc, char * argv[])
  202. {
  203. HRESULT hr;
  204. LPWSTR szUserName = NULL;
  205. LPWSTR szOldPassword = NULL;
  206. LPWSTR szNewPassword = NULL;
  207. if (argc != 3) {
  208. PrintUsage(szProgName, szAction,
  209. "<ADsPath> <Old Password> <New Password>");
  210. return(0);
  211. }
  212. szUserName = AllocateUnicodeString(argv[0]);
  213. szOldPassword = AllocateUnicodeString(argv[1]);
  214. szNewPassword = AllocateUnicodeString(argv[2]);
  215. hr = ChangePassword(
  216. szUserName,
  217. szOldPassword,
  218. szNewPassword
  219. );
  220. FreeUnicodeString(szUserName);
  221. FreeUnicodeString(szOldPassword);
  222. FreeUnicodeString(szNewPassword);
  223. if (FAILED(hr)) {
  224. printf("ChangePassword failed with error code %x\n", hr);
  225. return(1);
  226. }
  227. return(0);
  228. }
  229. ExecUserSetPass(char* szProgName, char* szAction, int argc, char * argv[])
  230. {
  231. HRESULT hr;
  232. LPWSTR szUserName = NULL;
  233. LPWSTR szNewPassword = NULL;
  234. if (argc != 2) {
  235. PrintUsage(szProgName, szAction, "<ADsPath> <New Password>");
  236. return(0);
  237. }
  238. szUserName = AllocateUnicodeString(argv[0]);
  239. szNewPassword = AllocateUnicodeString(argv[1]);
  240. hr = SetPassword(
  241. szUserName,
  242. szNewPassword
  243. );
  244. FreeUnicodeString(szUserName);
  245. FreeUnicodeString(szNewPassword);
  246. if (FAILED(hr)) {
  247. printf("ChangePassword failed with error code %x\n", hr);
  248. return 1;
  249. }
  250. return 0;
  251. }
  252. ExecUserGroups(char* szProgName, char* szAction, int argc, char * argv[])
  253. {
  254. HRESULT hr;
  255. LPWSTR szUserName = NULL;
  256. if (argc != 1) {
  257. PrintUsage(szProgName, szAction, "<ADs User Path>");
  258. return(0);
  259. }
  260. szUserName = AllocateUnicodeString(argv[0]);
  261. hr = EnumUserGroups(szUserName);
  262. FreeUnicodeString(szUserName);
  263. if (FAILED(hr)) {
  264. printf("Something failed with error code %x\n", hr);
  265. return 1;
  266. }
  267. return 0;
  268. }