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.

169 lines
4.2 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Defines functions for loading and storing the database configuration.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "nt.h"
  11. #include "ntlsa.h"
  12. #include "ntrtl.h"
  13. #include "nturtl.h"
  14. #include "windows.h"
  15. #include "oaidl.h"
  16. #include "dbconfig.h"
  17. static const wchar_t privateKeyName[] = L"NetPolAccountingDbConfig";
  18. HRESULT
  19. WINAPI
  20. IASLoadDatabaseConfig(
  21. PCWSTR server,
  22. BSTR* initString,
  23. BSTR* dataSourceName
  24. )
  25. {
  26. OBJECT_ATTRIBUTES objAttrs;
  27. HANDLE lsa;
  28. NTSTATUS status;
  29. LSA_UNICODE_STRING keyName, systemName, *data;
  30. BSTR bstr1, bstr2;
  31. ULONG winError;
  32. // Validate and initialize the out parameters.
  33. if ((initString == 0) || (dataSourceName == 0))
  34. {
  35. return E_POINTER;
  36. }
  37. *initString = 0;
  38. *dataSourceName = 0;
  39. // Open a connection to the LSA.
  40. RtlInitUnicodeString(&systemName, server);
  41. InitializeObjectAttributes(&objAttrs, 0, 0, 0, 0);
  42. status = LsaOpenPolicy(
  43. &systemName,
  44. &objAttrs,
  45. POLICY_GET_PRIVATE_INFORMATION,
  46. &lsa
  47. );
  48. if (NT_SUCCESS(status))
  49. {
  50. RtlInitUnicodeString(&keyName, privateKeyName);
  51. // Retrieve the private data.
  52. status = LsaRetrievePrivateData(lsa, &keyName, &data);
  53. if (NT_SUCCESS(status) && (data != 0))
  54. {
  55. if (data->Length > 0)
  56. {
  57. // Data is two adjacent null-terminated strings.
  58. bstr1 = SysAllocString(data->Buffer);
  59. bstr2 = SysAllocString(data->Buffer + wcslen(data->Buffer) + 1);
  60. if ((initString == 0) || (dataSourceName == 0))
  61. {
  62. SysFreeString(bstr1);
  63. SysFreeString(bstr2);
  64. status = STATUS_NO_MEMORY;
  65. }
  66. else
  67. {
  68. *initString = bstr1;
  69. *dataSourceName = bstr2;
  70. }
  71. }
  72. LsaFreeMemory(data);
  73. }
  74. else if (status == STATUS_OBJECT_NAME_NOT_FOUND)
  75. {
  76. status = STATUS_SUCCESS;
  77. }
  78. LsaClose(lsa);
  79. }
  80. winError = LsaNtStatusToWinError(status);
  81. return HRESULT_FROM_WIN32(winError);
  82. }
  83. HRESULT
  84. WINAPI
  85. IASStoreDatabaseConfig(
  86. PCWSTR server,
  87. PCWSTR initString,
  88. PCWSTR dataSourceName
  89. )
  90. {
  91. LSA_UNICODE_STRING data, systemName, keyName;
  92. size_t initStringLen, dataSourceNameLen, nbyte;
  93. OBJECT_ATTRIBUTES objAttribs;
  94. NTSTATUS status;
  95. HANDLE lsa;
  96. ULONG winError;
  97. if ((initString != 0) && (dataSourceName != 0))
  98. {
  99. // Allocate memory to hold the data.
  100. initStringLen = wcslen(initString) + 1;
  101. dataSourceNameLen = wcslen(dataSourceName) + 1;
  102. nbyte = (initStringLen + dataSourceNameLen) * sizeof(wchar_t);
  103. data.Buffer = CoTaskMemAlloc(nbyte);
  104. if (data.Buffer == 0)
  105. {
  106. return E_OUTOFMEMORY;
  107. }
  108. // Data is two adjacent null terminated strings.
  109. memcpy(
  110. data.Buffer,
  111. initString,
  112. (initStringLen * sizeof(wchar_t))
  113. );
  114. memcpy(
  115. (data.Buffer + initStringLen),
  116. dataSourceName,
  117. (dataSourceNameLen * sizeof(wchar_t))
  118. );
  119. data.Length = (USHORT)nbyte;
  120. data.MaximumLength = (USHORT)nbyte;
  121. }
  122. else
  123. {
  124. // Config is null, so just zero out the data.
  125. data.Buffer = 0;
  126. data.Length = 0;
  127. data.MaximumLength = 0;
  128. }
  129. // Open a connection to the LSA.
  130. RtlInitUnicodeString(&systemName, server);
  131. InitializeObjectAttributes(&objAttribs, 0, 0, 0, 0);
  132. status = LsaOpenPolicy(
  133. &systemName,
  134. &objAttribs,
  135. POLICY_CREATE_SECRET,
  136. &lsa
  137. );
  138. if (NT_SUCCESS(status))
  139. {
  140. RtlInitUnicodeString(&keyName, privateKeyName);
  141. // Store the config.
  142. status = LsaStorePrivateData(lsa, &keyName, &data);
  143. LsaClose(lsa);
  144. }
  145. CoTaskMemFree(data.Buffer);
  146. winError = LsaNtStatusToWinError(status);
  147. return HRESULT_FROM_WIN32(winError);
  148. }