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.

186 lines
4.4 KiB

  1. #include "tweakui.h"
  2. /*
  3. * Win9x doesn't have ntdll.dll, but since we only need one function from
  4. * it, let's just define it ourselves.
  5. */
  6. void _RtlInitUnicodeString(
  7. OUT PUNICODE_STRING DestinationString,
  8. IN PCWSTR SourceString)
  9. {
  10. ULONG Length;
  11. DestinationString->Buffer = (PWSTR)SourceString;
  12. Length = lstrlenW(SourceString) * sizeof(WCHAR);
  13. DestinationString->Length = (USHORT)Length;
  14. DestinationString->MaximumLength = (USHORT)(Length + sizeof(UNICODE_NULL));
  15. }
  16. /*
  17. * Win9x also doesn't have the Lsa functions in advapi32, so we need to
  18. * GetProcAddress them on the fly.
  19. */
  20. FARPROC GetAdvapi32Proc(LPCSTR pszName)
  21. {
  22. return GetProcAddress(GetModuleHandle("ADVAPI32"), pszName);
  23. }
  24. #define DELAYLOAD_FUNCTION(fn, args, nargs) \
  25. \
  26. NTSTATUS _##fn args \
  27. { \
  28. NTSTATUS (NTAPI *fn) args = \
  29. (NTSTATUS (NTAPI*)args)GetAdvapi32Proc(#fn); \
  30. if (fn) return fn nargs; \
  31. return STATUS_NOT_IMPLEMENTED; \
  32. }
  33. DELAYLOAD_FUNCTION(LsaOpenPolicy, (
  34. IN PLSA_UNICODE_STRING SystemName OPTIONAL,
  35. IN PLSA_OBJECT_ATTRIBUTES ObjectAttributes,
  36. IN ACCESS_MASK DesiredAccess,
  37. IN OUT PLSA_HANDLE PolicyHandle),
  38. (SystemName, ObjectAttributes, DesiredAccess, PolicyHandle))
  39. DELAYLOAD_FUNCTION(LsaRetrievePrivateData, (
  40. IN LSA_HANDLE PolicyHandle,
  41. IN PLSA_UNICODE_STRING KeyName,
  42. OUT PLSA_UNICODE_STRING * PrivateData),
  43. (PolicyHandle, KeyName, PrivateData))
  44. DELAYLOAD_FUNCTION(LsaStorePrivateData, (
  45. IN LSA_HANDLE PolicyHandle,
  46. IN PLSA_UNICODE_STRING KeyName,
  47. IN PLSA_UNICODE_STRING PrivateData),
  48. (PolicyHandle, KeyName, PrivateData))
  49. DELAYLOAD_FUNCTION(LsaClose, (
  50. IN LSA_HANDLE ObjectHandle),
  51. (ObjectHandle))
  52. DELAYLOAD_FUNCTION(LsaFreeMemory, (
  53. IN PVOID Buffer),
  54. (Buffer))
  55. /****************************************************************************/
  56. #define DEFAULT_PASSWORD_KEY L"DefaultPassword"
  57. NTSTATUS
  58. GetSecretDefaultPassword(
  59. LPWSTR PasswordBuffer, DWORD cchBuf
  60. )
  61. {
  62. NTSTATUS Status = STATUS_SUCCESS;
  63. OBJECT_ATTRIBUTES ObjectAttributes;
  64. LSA_HANDLE LsaHandle = NULL;
  65. UNICODE_STRING SecretName;
  66. PUNICODE_STRING SecretValue = NULL;
  67. InitializeObjectAttributes(
  68. &ObjectAttributes,
  69. NULL,
  70. 0L,
  71. (HANDLE)NULL,
  72. NULL
  73. );
  74. Status = _LsaOpenPolicy(
  75. NULL,
  76. &ObjectAttributes,
  77. POLICY_VIEW_LOCAL_INFORMATION,
  78. &LsaHandle
  79. );
  80. if (!NT_SUCCESS(Status)) {
  81. return Status;
  82. }
  83. _RtlInitUnicodeString(
  84. &SecretName,
  85. DEFAULT_PASSWORD_KEY
  86. );
  87. Status = _LsaRetrievePrivateData(
  88. LsaHandle,
  89. &SecretName,
  90. &SecretValue
  91. );
  92. if (!NT_SUCCESS(Status)) {
  93. _LsaClose(LsaHandle);
  94. return Status;
  95. }
  96. DWORD cchSecret = SecretValue->Length / sizeof(WCHAR); // does not include terminator
  97. lstrcpynW(PasswordBuffer, SecretValue->Buffer, min(cchBuf, cchSecret+1));
  98. if (SecretValue->Buffer != NULL) {
  99. _LsaFreeMemory(SecretValue->Buffer);
  100. }
  101. _LsaFreeMemory(SecretValue);
  102. _LsaClose(LsaHandle);
  103. return STATUS_SUCCESS;
  104. }
  105. NTSTATUS
  106. SetSecretDefaultPassword(
  107. LPWSTR PasswordBuffer
  108. )
  109. {
  110. NTSTATUS Status = STATUS_SUCCESS;
  111. OBJECT_ATTRIBUTES ObjectAttributes;
  112. LSA_HANDLE LsaHandle = NULL;
  113. UNICODE_STRING SecretName;
  114. UNICODE_STRING SecretValue;
  115. InitializeObjectAttributes(
  116. &ObjectAttributes,
  117. NULL,
  118. 0L,
  119. (HANDLE)NULL,
  120. NULL
  121. );
  122. Status = _LsaOpenPolicy(
  123. NULL,
  124. &ObjectAttributes,
  125. POLICY_CREATE_SECRET,
  126. &LsaHandle
  127. );
  128. if (!NT_SUCCESS(Status)) {
  129. return Status;
  130. }
  131. _RtlInitUnicodeString(
  132. &SecretName,
  133. DEFAULT_PASSWORD_KEY
  134. );
  135. _RtlInitUnicodeString(
  136. &SecretValue,
  137. PasswordBuffer
  138. );
  139. Status = _LsaStorePrivateData(
  140. LsaHandle,
  141. &SecretName,
  142. &SecretValue
  143. );
  144. if (!NT_SUCCESS(Status)) {
  145. _LsaClose(LsaHandle);
  146. return Status;
  147. }
  148. _LsaClose(LsaHandle);
  149. return STATUS_SUCCESS;
  150. }