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.

150 lines
3.8 KiB

  1. // this file provides a wrapper api to get to the NT specific LSA routines
  2. #include "stdafx.h"
  3. #include "KMLsa.h"
  4. //=============================================================
  5. //-------------------------------------------------------------
  6. // pass in a NULL pszwServer name to open the local machine
  7. HANDLE HOpenLSAPolicy( WCHAR *pszwServer, DWORD *pErr )
  8. {
  9. NTSTATUS ntStatus;
  10. LSA_OBJECT_ATTRIBUTES objectAttributs;
  11. LSA_HANDLE hPolicy;
  12. LSA_UNICODE_STRING unicodeServer;
  13. // prepare the object attributes
  14. InitializeObjectAttributes( &objectAttributs, NULL, 0L, NULL, NULL );
  15. // prepare the lsa_unicode name of the server
  16. if ( pszwServer )
  17. {
  18. unicodeServer.Buffer = pszwServer;
  19. unicodeServer.Length = wcslen(pszwServer) * sizeof(WCHAR);
  20. unicodeServer.MaximumLength = unicodeServer.Length + sizeof(WCHAR);
  21. }
  22. // attempt to open the policy
  23. ntStatus = LsaOpenPolicy( pszwServer ? &unicodeServer : NULL,
  24. &objectAttributs, POLICY_ALL_ACCESS, &hPolicy );
  25. // check for an error
  26. if ( !NT_SUCCESS(ntStatus) )
  27. {
  28. *pErr = LsaNtStatusToWinError( ntStatus );
  29. return NULL;
  30. }
  31. // success, so return the policy handle as a regular handle
  32. *pErr = 0;
  33. return hPolicy;
  34. }
  35. //-------------------------------------------------------------
  36. BOOL FCloseLSAPolicy( HANDLE hPolicy, DWORD *pErr )
  37. {
  38. NTSTATUS ntStatus;
  39. // close the policy
  40. ntStatus = LsaClose( hPolicy );
  41. // check for an error
  42. if ( !NT_SUCCESS(ntStatus) )
  43. {
  44. *pErr = LsaNtStatusToWinError( ntStatus );
  45. return FALSE;
  46. }
  47. // success, so return the policy handle as a regular handle
  48. *pErr = 0;
  49. return TRUE;
  50. }
  51. //-------------------------------------------------------------
  52. // passing NULL in for pvData deletes the secret
  53. BOOL FStoreLSASecret( HANDLE hPolicy, WCHAR* pszwSecretName, void* pvData, WORD cbData, DWORD *pErr )
  54. {
  55. LSA_UNICODE_STRING unicodeSecretName;
  56. LSA_UNICODE_STRING unicodeData;
  57. NTSTATUS ntStatus;
  58. // make sure we have a policy and a secret name
  59. if ( !hPolicy || !pszwSecretName )
  60. {
  61. *pErr = 1;
  62. return FALSE;
  63. }
  64. // prepare the lsa_unicode name of the server
  65. unicodeSecretName.Buffer = pszwSecretName;
  66. unicodeSecretName.Length = wcslen(pszwSecretName) * sizeof(WCHAR);
  67. unicodeSecretName.MaximumLength = unicodeSecretName.Length + sizeof(WCHAR);
  68. // prepare the unicode data record
  69. if ( pvData )
  70. {
  71. unicodeData.Buffer = (WCHAR*)pvData;
  72. unicodeData.Length = cbData;
  73. unicodeData.MaximumLength = cbData;
  74. }
  75. // it is now time to store the secret
  76. ntStatus = LsaStorePrivateData( hPolicy, &unicodeSecretName, pvData ? &unicodeData : NULL );
  77. // check for an error
  78. if ( !NT_SUCCESS(ntStatus) )
  79. {
  80. *pErr = LsaNtStatusToWinError( ntStatus );
  81. return FALSE;
  82. }
  83. // success, so return the policy handle as a regular handle
  84. *pErr = 0;
  85. return TRUE;
  86. }
  87. //-------------------------------------------------------------
  88. // passing NULL in for pvData deletes the secret
  89. PLSA_UNICODE_STRING FRetrieveLSASecret( HANDLE hPolicy, WCHAR* pszwSecretName, DWORD *pErr )
  90. {
  91. LSA_UNICODE_STRING unicodeSecretName;
  92. LSA_UNICODE_STRING* pUnicodeData = NULL;
  93. NTSTATUS ntStatus;
  94. // make sure we have a policy and a secret name
  95. if ( !hPolicy || !pszwSecretName )
  96. {
  97. *pErr = 1;
  98. return FALSE;
  99. }
  100. // prepare the lsa_unicode name of the server
  101. unicodeSecretName.Buffer = pszwSecretName;
  102. unicodeSecretName.Length = wcslen(pszwSecretName) * sizeof(WCHAR);
  103. unicodeSecretName.MaximumLength = unicodeSecretName.Length + sizeof(WCHAR);
  104. // it is now time to store the secret
  105. ntStatus = LsaRetrievePrivateData( hPolicy, &unicodeSecretName, &pUnicodeData );
  106. // check for an error
  107. if ( !NT_SUCCESS(ntStatus) )
  108. {
  109. *pErr = LsaNtStatusToWinError( ntStatus );
  110. return NULL;
  111. }
  112. // success, so return the policy handle as a regular handle
  113. *pErr = 0;
  114. return (PLSA_UNICODE_STRING)pUnicodeData;
  115. }