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.

228 lines
6.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: lsa.c
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include <ntmsv1_0.h>
  14. #include <ntlsa.h>
  15. #include <windows.h>
  16. #include "snaputil.h" // for IsLocalMachine
  17. #define _USTRINGP_NO_UNICODE_STRING
  18. #define _USTRINGP_NO_UNICODE_STRING32
  19. #include "ustringp.h"
  20. #include "lsa.h"
  21. // Useful defines
  22. #define PSZRADIUSSERVER L"RADIUSServer."
  23. #define CCHRADIUSSERVER 13
  24. DWORD
  25. StorePrivateData(
  26. IN OPTIONAL LPCWSTR pszServerName,
  27. IN LPCWSTR pszRadiusServerName,
  28. IN LPCWSTR pszSecret
  29. )
  30. {
  31. LSA_HANDLE hLSA = NULL;
  32. NTSTATUS ntStatus;
  33. LSA_OBJECT_ATTRIBUTES objectAttributes;
  34. LSA_UNICODE_STRING LSAPrivData, LSAPrivDataDesc;
  35. TCHAR tszPrivData[MAX_PATH+1],
  36. tszPrivDataDesc[MAX_PATH+CCHRADIUSSERVER+1];
  37. TCHAR * ptszTemp;
  38. PUNICODE_STRING pSystem;
  39. UNICODE_STRING uszSystemName;
  40. if (IsLocalMachine(pszServerName))
  41. pSystem = NULL;
  42. else
  43. {
  44. SetUnicodeString(&uszSystemName,
  45. pszServerName);
  46. pSystem = &uszSystemName;
  47. }
  48. InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
  49. ntStatus = LsaOpenPolicy(pSystem, &objectAttributes, POLICY_ALL_ACCESS, &hLSA);
  50. if ( !NT_SUCCESS( ntStatus) )
  51. {
  52. return( RtlNtStatusToDosError( ntStatus ) );
  53. }
  54. ZeroMemory(tszPrivDataDesc, sizeof(tszPrivDataDesc));
  55. lstrcpy(tszPrivDataDesc, PSZRADIUSSERVER);
  56. lstrcpyn(tszPrivDataDesc + CCHRADIUSSERVER, pszRadiusServerName, MAX_PATH);
  57. LSAPrivDataDesc.Length = (USHORT)((lstrlen(tszPrivDataDesc) + 1) * sizeof(TCHAR));
  58. LSAPrivDataDesc.MaximumLength = sizeof(tszPrivDataDesc);
  59. LSAPrivDataDesc.Buffer = tszPrivDataDesc;
  60. ZeroMemory(tszPrivData, sizeof(tszPrivData));
  61. lstrcpyn(tszPrivData, pszSecret, MAX_PATH);
  62. LSAPrivData.Length = (USHORT)(lstrlen(tszPrivData) * sizeof(TCHAR));
  63. LSAPrivData.MaximumLength = sizeof(tszPrivData);
  64. LSAPrivData.Buffer = tszPrivData;
  65. ntStatus = LsaStorePrivateData(hLSA, &LSAPrivDataDesc, &LSAPrivData);
  66. ZeroMemory( tszPrivData, sizeof( tszPrivData ) );
  67. LsaClose(hLSA);
  68. return( RtlNtStatusToDosError( ntStatus ) );
  69. }
  70. DWORD
  71. RetrievePrivateData(
  72. IN OPTIONAL LPCWSTR pszServerName,
  73. IN LPCWSTR pszRadiusServerName,
  74. OUT LPWSTR pszSecret,
  75. IN INT cchSecret
  76. )
  77. {
  78. LSA_HANDLE hLSA = NULL;
  79. NTSTATUS ntStatus;
  80. LSA_OBJECT_ATTRIBUTES objectAttributes;
  81. LSA_UNICODE_STRING *pLSAPrivData, LSAPrivDataDesc;
  82. TCHAR tszPrivData[MAX_PATH+1],
  83. tszPrivDataDesc[MAX_PATH+CCHRADIUSSERVER+1];
  84. PUNICODE_STRING pSystem;
  85. UNICODE_STRING uszSystemName;
  86. if (IsLocalMachine(pszServerName))
  87. pSystem = NULL;
  88. else
  89. {
  90. SetUnicodeString(&uszSystemName,
  91. pszServerName);
  92. pSystem = &uszSystemName;
  93. }
  94. InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
  95. ntStatus = LsaOpenPolicy(pSystem, &objectAttributes, POLICY_ALL_ACCESS, &hLSA);
  96. if ( !NT_SUCCESS( ntStatus) )
  97. {
  98. return( RtlNtStatusToDosError( ntStatus ) );
  99. }
  100. ZeroMemory(tszPrivDataDesc, sizeof(tszPrivDataDesc));
  101. lstrcpy(tszPrivDataDesc, PSZRADIUSSERVER);
  102. lstrcpyn(tszPrivDataDesc + CCHRADIUSSERVER, pszRadiusServerName, MAX_PATH);
  103. LSAPrivDataDesc.Length = (USHORT)((lstrlen(tszPrivDataDesc) + 1) * sizeof(TCHAR));
  104. LSAPrivDataDesc.MaximumLength = sizeof(tszPrivDataDesc);
  105. LSAPrivDataDesc.Buffer = tszPrivDataDesc;
  106. ntStatus = LsaRetrievePrivateData(hLSA, &LSAPrivDataDesc, &pLSAPrivData);
  107. if ( !NT_SUCCESS( ntStatus ) )
  108. {
  109. LsaClose(hLSA);
  110. return( RtlNtStatusToDosError( ntStatus ) );
  111. }
  112. else
  113. {
  114. if ((pLSAPrivData->Length + 1) >= cchSecret)
  115. return ERROR_INSUFFICIENT_BUFFER;
  116. ZeroMemory(pszSecret, (pLSAPrivData->Length + 1) * sizeof(TCHAR));
  117. CopyMemory(pszSecret, pLSAPrivData->Buffer, pLSAPrivData->Length);
  118. LsaFreeMemory(pLSAPrivData);
  119. }
  120. return( NO_ERROR );
  121. }
  122. DWORD
  123. DeletePrivateData(
  124. IN OPTIONAL LPCWSTR pszServerName,
  125. IN LPCWSTR pszRadiusServerName
  126. )
  127. {
  128. LSA_HANDLE hLSA = NULL;
  129. NTSTATUS ntStatus;
  130. LSA_OBJECT_ATTRIBUTES objectAttributes;
  131. LSA_UNICODE_STRING LSAPrivDataDesc;
  132. TCHAR tszPrivDataDesc[MAX_PATH+CCHRADIUSSERVER+1];
  133. PUNICODE_STRING pSystem;
  134. UNICODE_STRING uszSystemName;
  135. if (IsLocalMachine(pszServerName))
  136. pSystem = NULL;
  137. else
  138. {
  139. SetUnicodeString(&uszSystemName,
  140. pszServerName);
  141. pSystem = &uszSystemName;
  142. }
  143. InitializeObjectAttributes(&objectAttributes, NULL, 0, NULL, NULL);
  144. ntStatus = LsaOpenPolicy(pSystem, &objectAttributes, POLICY_ALL_ACCESS, &hLSA);
  145. if ( !NT_SUCCESS( ntStatus) )
  146. {
  147. return( RtlNtStatusToDosError( ntStatus ) );
  148. }
  149. ZeroMemory(tszPrivDataDesc, sizeof(tszPrivDataDesc));
  150. lstrcpy(tszPrivDataDesc, PSZRADIUSSERVER);
  151. lstrcpyn(tszPrivDataDesc + CCHRADIUSSERVER, pszRadiusServerName, MAX_PATH);
  152. LSAPrivDataDesc.Length = (USHORT)((lstrlen(tszPrivDataDesc) + 1) * sizeof(TCHAR));
  153. LSAPrivDataDesc.MaximumLength = sizeof(tszPrivDataDesc);
  154. LSAPrivDataDesc.Buffer = tszPrivDataDesc;
  155. ntStatus = LsaStorePrivateData(hLSA, &LSAPrivDataDesc, NULL);
  156. LsaClose(hLSA);
  157. return( RtlNtStatusToDosError( ntStatus ) );
  158. }
  159. // Some helper functions
  160. DWORD RtlEncodeW(PUCHAR pucSeed, LPWSTR pswzString)
  161. {
  162. UNICODE_STRING ustring;
  163. ustring.Length = (USHORT)(lstrlenW(pswzString) * sizeof(WCHAR));
  164. ustring.MaximumLength = ustring.Length;
  165. ustring.Buffer = pswzString;
  166. RtlRunEncodeUnicodeString(pucSeed, &ustring);
  167. return 0;
  168. }
  169. DWORD RtlDecodeW(UCHAR ucSeed, LPWSTR pswzString)
  170. {
  171. UNICODE_STRING ustring;
  172. ustring.Length = (USHORT)(lstrlenW(pswzString) * sizeof(WCHAR));
  173. ustring.MaximumLength = ustring.Length;
  174. ustring.Buffer = pswzString;
  175. RtlRunDecodeUnicodeString(ucSeed, &ustring);
  176. return 0;
  177. }