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.

179 lines
4.7 KiB

  1. /*--
  2. Copyright (c) 1987-1993 Microsoft Corporation
  3. Module Name:
  4. refrshpw.c
  5. Abstract:
  6. refresh the cached password for the specified account in the LSA on a
  7. node. derived from test program for the NtLmSsp service.
  8. Author:
  9. 28-Jun-1993 (cliffv)
  10. Environment:
  11. User mode only.
  12. Contains NT-specific code.
  13. Requires ANSI C extensions: slash-slash comments, long external names.
  14. Revision History:
  15. 26-Jul-99 (charlwi) Charlie Wickham
  16. modified to handle updating a password (from Mike Swift or Scott Field)
  17. --*/
  18. //
  19. // Common include files.
  20. //
  21. #define SECURITY_WIN32
  22. #include <nt.h>
  23. #include <ntrtl.h>
  24. #include <nturtl.h>
  25. #include <ntlsa.h>
  26. #include <windef.h>
  27. #include <winbase.h>
  28. #include <stdio.h> // printf
  29. #include <ntmsv1_0.h>
  30. #include "cluspw.h"
  31. DWORD
  32. ChangeCachedPassword(
  33. IN LPWSTR AccountName,
  34. IN LPWSTR DomainName,
  35. IN LPWSTR NewPassword
  36. )
  37. {
  38. PMSV1_0_CHANGEPASSWORD_REQUEST Request = NULL;
  39. HANDLE LogonHandle = NULL;
  40. ULONG Dummy;
  41. ULONG RequestSize = 0;
  42. ULONG PackageId = 0;
  43. PVOID Response;
  44. ULONG ResponseSize;
  45. NTSTATUS SubStatus = STATUS_SUCCESS, Status = STATUS_SUCCESS;
  46. BOOLEAN Trusted = TRUE;
  47. BOOLEAN WasEnabled;
  48. PBYTE Where;
  49. STRING Name;
  50. //
  51. // Turn on the TCB privilege
  52. //
  53. PrintMsg(MsgSeverityVerbose, "Adjusting TCB privilege\n");
  54. Status = RtlAdjustPrivilege(SE_TCB_PRIVILEGE, TRUE, FALSE, &WasEnabled);
  55. if (!NT_SUCCESS(Status)) {
  56. PrintMsg(MsgSeverityInfo, "Failed to Adjust TCB privilege. status %X\n", Status);
  57. Trusted = FALSE;
  58. }
  59. RtlInitString( &Name, "Cluspw" );
  60. PrintMsg(MsgSeverityVerbose, "Connecting to LSA\n");
  61. if (Trusted) {
  62. Status = LsaRegisterLogonProcess( &Name, &LogonHandle, &Dummy );
  63. }
  64. else {
  65. Status = LsaConnectUntrusted( &LogonHandle );
  66. }
  67. if (!NT_SUCCESS(Status)) {
  68. PrintMsg(MsgSeverityFatal,
  69. "Failed to register cluspw as a logon process: %08X\n",
  70. Status);
  71. return Status;
  72. }
  73. RtlInitString( &Name, MSV1_0_PACKAGE_NAME );
  74. PrintMsg(MsgSeverityVerbose, "Looking up authentication package\n");
  75. Status = LsaLookupAuthenticationPackage( LogonHandle, &Name, &PackageId );
  76. if (!NT_SUCCESS(Status)) {
  77. PrintMsg(MsgSeverityFatal,
  78. "Failed to lookup authentication package %Z: %08X\n",
  79. &Name,
  80. Status);
  81. return Status;
  82. }
  83. RequestSize = sizeof(MSV1_0_CHANGEPASSWORD_REQUEST) +
  84. (wcslen(AccountName) +
  85. wcslen(DomainName) +
  86. wcslen(NewPassword) + 3) * sizeof(WCHAR);
  87. Request = (PMSV1_0_CHANGEPASSWORD_REQUEST) LocalAlloc(LMEM_ZEROINIT,RequestSize);
  88. if ( Request == NULL ) {
  89. PrintMsg(MsgSeverityFatal, "Failed to allocate memory for cache update request.\n");
  90. return ERROR_NOT_ENOUGH_MEMORY;
  91. }
  92. Where = (PBYTE) (Request + 1);
  93. Request->MessageType = MsV1_0ChangeCachedPassword;
  94. wcscpy( (LPWSTR) Where, DomainName );
  95. RtlInitUnicodeString( &Request->DomainName, (LPWSTR) Where );
  96. Where += Request->DomainName.MaximumLength;
  97. wcscpy((LPWSTR) Where, AccountName );
  98. RtlInitUnicodeString( &Request->AccountName, (LPWSTR) Where );
  99. Where += Request->AccountName.MaximumLength;
  100. wcscpy((LPWSTR) Where, NewPassword );
  101. RtlInitUnicodeString( &Request->NewPassword, (LPWSTR) Where );
  102. Where += Request->NewPassword.MaximumLength;
  103. //
  104. // Make the call
  105. //
  106. PrintMsg(MsgSeverityVerbose, "Calling LSA to update the cache\n");
  107. Status = LsaCallAuthenticationPackage(LogonHandle,
  108. PackageId,
  109. Request,
  110. RequestSize,
  111. &Response,
  112. &ResponseSize,
  113. &SubStatus);
  114. if (!NT_SUCCESS(Status) || !NT_SUCCESS(SubStatus)) {
  115. DWORD winStatus;
  116. DWORD winSubStatus;
  117. winStatus = RtlNtStatusToDosError( Status );
  118. winSubStatus = RtlNtStatusToDosError( SubStatus );
  119. if ( winStatus == ERROR_MR_MID_NOT_FOUND ) {
  120. winStatus = Status;
  121. }
  122. if ( winSubStatus == ERROR_MR_MID_NOT_FOUND ) {
  123. winSubStatus = SubStatus;
  124. }
  125. PrintMsg(MsgSeverityFatal,
  126. "Updating password cache failed: status = 0x%08X, substatus = 0x%08X\n",
  127. winStatus,
  128. winSubStatus);
  129. if ( Status == STATUS_SUCCESS ) {
  130. Status = SubStatus;
  131. }
  132. }
  133. else {
  134. PrintMsg(MsgSeverityVerbose, "LSA returned success\n");
  135. }
  136. if (LogonHandle != NULL) {
  137. LsaDeregisterLogonProcess(LogonHandle);
  138. }
  139. return Status;
  140. }