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.

237 lines
5.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. //
  4. // Copyright (C) Microsoft
  5. //
  6. // File: securd.cpp
  7. //
  8. // History: 30-March-2000 a-skuzin Created
  9. //
  10. //--------------------------------------------------------------------------
  11. #include "stdafx.h"
  12. //
  13. // #include <windows.h>
  14. // #include <ntsecapi.h>
  15. //
  16. #ifndef NT_SUCCESS
  17. #define NT_SUCCESS(Status) ((NTSTATUS)(Status) >= 0)
  18. #endif
  19. NTSTATUS ChangePrivilegeOnAccount(IN BOOL addPrivilage, IN LPWSTR wszServer, IN LPWSTR wszPrivilegeName, IN PSID pSid);
  20. // NTSTATUS OpenPolicy(IN LPWSTR wszServer,IN DWORD DesiredAccess,OUT PLSA_HANDLE pPolicyHandle );
  21. void InitLsaString(OUT PLSA_UNICODE_STRING LsaString,IN LPWSTR String);
  22. BOOL SetPrivilegeInAccessToken(LPCTSTR PrivilegeName,DWORD dwAttributes) ;
  23. /*****************************************************************************
  24. *
  25. * GrantRemotePrivilegeToEveryone
  26. *
  27. * Grants "SeRemoteInteractiveLogonRight" privilege to "Everyone SID"
  28. *
  29. * ENTRY:
  30. * BOOL addPrivilage - if TRUE, we are adding privilege, else, we are remving privilage
  31. *
  32. *
  33. * NOTES:
  34. *
  35. *
  36. * EXIT:
  37. * Returns: 0 if success, error code if failure
  38. *
  39. *
  40. *
  41. ****************************************************************************/
  42. DWORD
  43. GrantRemotePrivilegeToEveryone( BOOL addPrivilege)
  44. {
  45. USES_CONVERSION;
  46. SID_IDENTIFIER_AUTHORITY WorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  47. PSID pWorldSid;
  48. if(!AllocateAndInitializeSid( &WorldSidAuthority, 1,
  49. SECURITY_WORLD_RID,
  50. 0, 0, 0, 0, 0, 0, 0,
  51. &pWorldSid ))
  52. {
  53. return GetLastError();
  54. }
  55. NTSTATUS Status = ChangePrivilegeOnAccount(addPrivilege, NULL, T2W(SE_REMOTE_INTERACTIVE_LOGON_NAME),pWorldSid);
  56. FreeSid(pWorldSid);
  57. return (DWORD)LsaNtStatusToWinError(Status);
  58. }
  59. /*****************************************************************************
  60. *
  61. * ChangePrivilegeOnAccount
  62. *
  63. * Grants or Remove privelege represented by wszPrivilegeName to account represented by pSid
  64. *
  65. * ENTRY:
  66. * BOOL addPrivilage - If TRUE, we are adding privilage, else, we are removing privilage
  67. * LPCWSTR wszServer - name of the server on which the privilege is being set
  68. * LPCWSTR wszPrivilegeName - name of the privilege
  69. * PSID pSid - pointer to hte SID of the user (or group)
  70. *
  71. *
  72. * NOTES:
  73. *
  74. *
  75. * EXIT:
  76. * Returns: NTSTATUS code of an error if failure
  77. *
  78. *
  79. *
  80. ****************************************************************************/
  81. NTSTATUS
  82. ChangePrivilegeOnAccount(
  83. IN BOOL addPrivilege, // add or remove
  84. IN LPWSTR wszServer,
  85. IN LPWSTR wszPrivilegeName,
  86. IN PSID pSid)
  87. {
  88. NTSTATUS Status;
  89. LSA_HANDLE PolicyHandle = NULL;
  90. Status = OpenPolicy(wszServer,POLICY_WRITE|POLICY_LOOKUP_NAMES,&PolicyHandle);
  91. if(!NT_SUCCESS(Status))
  92. {
  93. return Status;
  94. }
  95. LSA_UNICODE_STRING PrivilegeString;
  96. //
  97. // Create a LSA_UNICODE_STRING for the privilege name.
  98. //
  99. InitLsaString(&PrivilegeString, wszPrivilegeName);
  100. //
  101. // grant the privilege
  102. //
  103. if ( addPrivilege)
  104. {
  105. Status=LsaAddAccountRights(
  106. PolicyHandle, // open policy handle
  107. pSid, // target SID
  108. &PrivilegeString, // privileges
  109. 1 // privilege count
  110. );
  111. }
  112. else
  113. {
  114. Status=LsaRemoveAccountRights(
  115. PolicyHandle, // open policy handle
  116. pSid, // target SID
  117. FALSE, // we are NOT removing all rights
  118. &PrivilegeString, // privileges
  119. 1 // privilege count
  120. );
  121. }
  122. LsaClose(PolicyHandle);
  123. return Status;
  124. }
  125. #if 0
  126. /*****************************************************************************
  127. *
  128. * OpenPolicy
  129. *
  130. * Opens LSA policy
  131. *
  132. * ENTRY:
  133. * IN LPWSTR wszServer
  134. * IN DWORD DesiredAccess
  135. * OUT PLSA_HANDLE pPolicyHandle
  136. *
  137. *
  138. * NOTES:
  139. *
  140. *
  141. * EXIT:
  142. * Returns: NTSTATUS code of an error if failure
  143. *
  144. *
  145. *
  146. ****************************************************************************/
  147. NTSTATUS
  148. OpenPolicy(
  149. IN LPWSTR wszServer,
  150. IN DWORD DesiredAccess,
  151. OUT PLSA_HANDLE pPolicyHandle )
  152. {
  153. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  154. LSA_UNICODE_STRING ServerString;
  155. //
  156. // Always initialize the object attributes to all zeroes.
  157. //
  158. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  159. //
  160. // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  161. //
  162. InitLsaString(&ServerString, wszServer);
  163. //
  164. // Attempt to open the policy.
  165. //
  166. return LsaOpenPolicy(
  167. &ServerString,
  168. &ObjectAttributes,
  169. DesiredAccess,
  170. pPolicyHandle);
  171. }
  172. /*****************************************************************************
  173. *
  174. * InitLsaString
  175. *
  176. * Makes a LSA_UNICODE_STRING out of the LPWSTR passed in
  177. *
  178. * ENTRY:
  179. * OUT PLSA_UNICODE_STRING LsaString
  180. * IN LPWSTR String
  181. *
  182. *
  183. * NOTES:
  184. *
  185. *
  186. * EXIT:
  187. * NONE
  188. *
  189. *
  190. *
  191. ****************************************************************************/
  192. void
  193. InitLsaString(
  194. OUT PLSA_UNICODE_STRING LsaString,
  195. IN LPWSTR String)
  196. {
  197. DWORD StringLength;
  198. if (String == NULL)
  199. {
  200. LsaString->Buffer = NULL;
  201. LsaString->Length = 0;
  202. LsaString->MaximumLength = 0;
  203. return;
  204. }
  205. StringLength = wcslen(String);
  206. LsaString->Buffer = String;
  207. LsaString->Length = (USHORT) StringLength * sizeof(WCHAR);
  208. LsaString->MaximumLength=(USHORT)(StringLength+1) * sizeof(WCHAR);
  209. }
  210. #endif