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.

330 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. setpwd.c
  5. Abstract:
  6. Test for SamiChangePasswordUser NT security API.
  7. Author:
  8. Ovidiu Temereanca 17-Mar-2000 Initial implementation
  9. Revision History:
  10. --*/
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #undef DOMAIN_ALL_ACCESS // defined in both ntsam.h and ntwinapi.h
  15. #include <ntsam.h>
  16. #include <ntlsa.h>
  17. #include <windef.h>
  18. #include <winbase.h>
  19. #include <align.h>
  20. #include <lm.h>
  21. #include <lmapibuf.h>
  22. #include <lmaccess.h>
  23. #include <lmerr.h>
  24. #include <limits.h>
  25. #include <rpcutil.h>
  26. #include <secobj.h>
  27. #include <stddef.h>
  28. #include <ntdsapi.h>
  29. #include <dsgetdc.h>
  30. #include <windows.h>
  31. #include <setupapi.h>
  32. #include "common.h"
  33. #include "migutil.h"
  34. #include "encrypt.h"
  35. #include <ntsamp.h>
  36. DWORD
  37. CreateLocalAccount (
  38. IN PWSTR User,
  39. IN PWSTR OldPassword,
  40. IN PWSTR NewPassword,
  41. IN BOOL EncryptedPwd
  42. );
  43. HINSTANCE g_hInst;
  44. HANDLE g_hHeap;
  45. typedef BOOL (WINAPI INITROUTINE_PROTOTYPE)(HINSTANCE, DWORD, LPVOID);
  46. INITROUTINE_PROTOTYPE MigUtil_Entry;
  47. BOOL
  48. Init (
  49. VOID
  50. )
  51. {
  52. HINSTANCE hInstance;
  53. DWORD dwReason;
  54. PVOID lpReserved;
  55. //
  56. // Simulate DllMain
  57. //
  58. g_hInst = GetModuleHandle (NULL);
  59. g_hHeap = GetProcessHeap();
  60. hInstance = GetModuleHandle (NULL);
  61. dwReason = DLL_PROCESS_ATTACH;
  62. lpReserved = NULL;
  63. //
  64. // Initialize DLL globals
  65. //
  66. if (!MigUtil_Entry (g_hInst, DLL_PROCESS_ATTACH, NULL)) {
  67. _tprintf (TEXT("MigUtil failed initializing\n"));
  68. return FALSE;
  69. }
  70. return TRUE;
  71. }
  72. VOID
  73. Terminate (
  74. VOID
  75. )
  76. {
  77. HINSTANCE hInstance;
  78. DWORD dwReason;
  79. PVOID lpReserved;
  80. //
  81. // Simulate DllMain
  82. //
  83. hInstance = GetModuleHandle (NULL);
  84. dwReason = DLL_PROCESS_DETACH;
  85. lpReserved = NULL;
  86. //
  87. // Call the exit routine that requires library APIs
  88. //
  89. MigUtil_Entry (g_hInst, DLL_PROCESS_DETACH, NULL);
  90. }
  91. INT
  92. __cdecl
  93. _tmain (
  94. INT argc,
  95. TCHAR *argv[]
  96. )
  97. {
  98. NTSTATUS rc;
  99. PWSTR oldHash, newHash;
  100. BOOL encrypted;
  101. INT i;
  102. if (argc < 4) {
  103. _tprintf (TEXT("Usage:\n")
  104. TEXT(" setpwd [/e] <LocalUserName> <oldpwd_hash> <newpwd_hash>\n")
  105. TEXT(" /e - if specified, password is a hash value; otherwise it's in clear")
  106. TEXT("Use quotes if any arg contains spaces\n")
  107. TEXT("Use dot as a placeholder for the empty password hash value\n")
  108. );
  109. return 1;
  110. }
  111. if (!Init()) {
  112. _tprintf (TEXT("Unable to initialize!\n"));
  113. return 2;
  114. }
  115. if ((argv[1][0] == TEXT('/') || argv[1][0] == TEXT('-')) &&
  116. _totlower(argv[1][1]) == TEXT('e')
  117. ) {
  118. encrypted = TRUE;
  119. i = 2;
  120. } else {
  121. encrypted = FALSE;
  122. i = 1;
  123. }
  124. if (StringMatch (argv[i + 1], TEXT("."))) {
  125. oldHash = NULL;
  126. } else {
  127. oldHash = argv[i + 1];
  128. }
  129. if (StringMatch (argv[i + 2], TEXT("."))) {
  130. newHash = NULL;
  131. } else {
  132. newHash = argv[i + 2];
  133. }
  134. rc = CreateLocalAccount (argv[i], oldHash, newHash, encrypted);
  135. if (rc != NO_ERROR) {
  136. _tprintf (TEXT("CreateLocalAccount failed (status = %lu)\n"), rc);
  137. }
  138. Terminate();
  139. return rc;
  140. }
  141. DWORD
  142. CreateLocalAccount (
  143. IN PWSTR User,
  144. IN PWSTR OldPassword,
  145. IN PWSTR NewPassword,
  146. IN BOOL EncryptedPwd
  147. )
  148. /*++
  149. Routine Description:
  150. CreateLocalAccount creates an account for a local user
  151. Arguments:
  152. Properties - Specifies a set of attributes for a user
  153. User - An optional name to override Properties->User
  154. Return value:
  155. A Win32 error code
  156. --*/
  157. {
  158. USER_INFO_3 ui;
  159. PUSER_INFO_3 ExistingInfo;
  160. DWORD rc;
  161. LONG ErrParam;
  162. //
  163. // Create local account
  164. //
  165. ZeroMemory (&ui, sizeof (ui));
  166. ui.usri3_name = User;
  167. ui.usri3_password = EncryptedPwd ? TEXT("GigiMarga@123456") : NewPassword;
  168. ui.usri3_comment = TEXT("TestAccount");
  169. ui.usri3_full_name = TEXT("Full name");
  170. ui.usri3_priv = USER_PRIV_USER;
  171. ui.usri3_flags = UF_SCRIPT|UF_NORMAL_ACCOUNT;
  172. ui.usri3_acct_expires = TIMEQ_FOREVER;
  173. ui.usri3_max_storage = USER_MAXSTORAGE_UNLIMITED;
  174. ui.usri3_primary_group_id = DOMAIN_GROUP_RID_USERS;
  175. ui.usri3_max_storage = USER_MAXSTORAGE_UNLIMITED;
  176. ui.usri3_acct_expires = TIMEQ_FOREVER;
  177. ui.usri3_password_expired = FALSE;
  178. rc = NetUserDel (NULL, User);
  179. rc = NetUserAdd (NULL, 3, (PBYTE) &ui, &ErrParam);
  180. if (rc == ERROR_SUCCESS) {
  181. if (EncryptedPwd) {
  182. //
  183. // change user's password using encrypted password APIs
  184. //
  185. rc = SetLocalUserEncryptedPassword (
  186. User,
  187. TEXT("aad3b435b51404eeaad3b435b51404ee64d208a23ff2f0482eb02f6f267e97ea"),
  188. TRUE,
  189. NewPassword,
  190. TRUE
  191. );
  192. if (rc != ERROR_SUCCESS) {
  193. DEBUGMSG ((
  194. DBG_WARNING,
  195. "Can't set encrypted password on user %s, rc=%u",
  196. User,
  197. rc
  198. ));
  199. rc = ERROR_SUCCESS;
  200. }
  201. }
  202. } else {
  203. if (rc == NERR_UserExists) {
  204. //
  205. // Try to change password if user already exists and this is the intent
  206. //
  207. DEBUGMSG ((DBG_WARNING, "User %s already exists", User));
  208. if (EncryptedPwd) {
  209. rc = SetLocalUserEncryptedPassword (
  210. User,
  211. TEXT("65c5c4e1e98d8bada13f0882c43aca5810fec09fb8c9d1b9d065c2d6d75fc582"),
  212. TRUE,
  213. NewPassword,
  214. TRUE
  215. );
  216. if (rc != ERROR_SUCCESS) {
  217. DEBUGMSG ((
  218. DBG_WARNING,
  219. "Can't set encrypted password on user %s, rc=%u",
  220. User,
  221. rc
  222. ));
  223. rc = ERROR_SUCCESS;
  224. }
  225. } else {
  226. rc = NetUserGetInfo (NULL, User, 3, (PBYTE *) &ExistingInfo);
  227. if (rc == ERROR_SUCCESS) {
  228. ExistingInfo->usri3_password = ui.usri3_password;
  229. ExistingInfo->usri3_comment = ui.usri3_comment;
  230. ExistingInfo->usri3_full_name = ui.usri3_full_name;
  231. ExistingInfo->usri3_flags = ui.usri3_flags;
  232. ExistingInfo->usri3_password_expired = ui.usri3_password_expired;
  233. rc = NetUserSetInfo (NULL, User, 3, (PBYTE) ExistingInfo, &ErrParam);
  234. NetApiBufferFree ((PVOID) ExistingInfo);
  235. if (rc != ERROR_SUCCESS) {
  236. DEBUGMSG ((
  237. DBG_WARNING,
  238. "Can't set info on user %s, rc=%u, ErrParam=%u",
  239. User,
  240. rc,
  241. ErrParam
  242. ));
  243. rc = ERROR_SUCCESS;
  244. }
  245. } else {
  246. DEBUGMSG ((DBG_WARNING, "Can't get info for user %s, rc=%u", User, rc));
  247. rc = ERROR_SUCCESS;
  248. }
  249. }
  250. }
  251. }
  252. if (rc != ERROR_SUCCESS) {
  253. SetLastError (rc);
  254. LOG ((LOG_ERROR, "NetUserAdd failed for %s. ErrParam=%i.", User, ErrParam));
  255. }
  256. return rc;
  257. }