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.

415 lines
11 KiB

  1. /****************************************************************************
  2. PROGRAM: LSA.C
  3. PURPOSE: Utility routines that access the LSA.
  4. ****************************************************************************/
  5. #include "SECEDIT.h"
  6. #include <string.h>
  7. // Module global that holds handle to LSA once it has been opened.
  8. static LSA_HANDLE LsaHandle = NULL;
  9. LSA_HANDLE OpenLsa(VOID);
  10. VOID CloseLsa(LSA_HANDLE);
  11. /****************************************************************************
  12. FUNCTION: LsaInit
  13. PURPOSE: Does any initialization required for this module
  14. RETURNS: TRUE on success, FALSE on failure
  15. ****************************************************************************/
  16. BOOL LsaInit(VOID)
  17. {
  18. #ifdef LSA_AVAILABLE
  19. LsaHandle = OpenLsa();
  20. return (LsaHandle != NULL);
  21. #endif
  22. return (TRUE);
  23. }
  24. /****************************************************************************
  25. FUNCTION: LsaTerminate
  26. PURPOSE: Does any cleanup required for this module
  27. RETURNS: TRUE on success, FALSE on failure
  28. ****************************************************************************/
  29. BOOL LsaTerminate(VOID)
  30. {
  31. #ifdef LSA_AVAILABLE
  32. if (LsaHandle != NULL) {
  33. CloseLsa(LsaHandle);
  34. }
  35. #endif
  36. LsaHandle = NULL;
  37. return(TRUE);
  38. }
  39. #ifdef LSA_AVAILABLE
  40. /****************************************************************************
  41. FUNCTION: OpenLsa
  42. PURPOSE: Opens the Lsa
  43. Returns handle to Lsa or NULL on failure
  44. ****************************************************************************/
  45. LSA_HANDLE OpenLsa(VOID)
  46. {
  47. NTSTATUS Status;
  48. OBJECT_ATTRIBUTES ObjectAttributes;
  49. LSA_HANDLE ConnectHandle = NULL;
  50. SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;
  51. //
  52. // Set up the Security Quality Of Service
  53. //
  54. SecurityQualityOfService.Length = sizeof(SECURITY_QUALITY_OF_SERVICE);
  55. SecurityQualityOfService.ImpersonationLevel = SecurityImpersonation;
  56. SecurityQualityOfService.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  57. SecurityQualityOfService.EffectiveOnly = FALSE;
  58. //
  59. // Set up the object attributes prior to opening the LSA.
  60. //
  61. InitializeObjectAttributes(&ObjectAttributes,
  62. NULL,
  63. 0L,
  64. (HANDLE)NULL,
  65. NULL);
  66. //
  67. // The InitializeObjectAttributes macro presently stores NULL for
  68. // the SecurityQualityOfService field, so we must manually copy that
  69. // structure for now.
  70. //
  71. ObjectAttributes.SecurityQualityOfService = &SecurityQualityOfService;
  72. //
  73. // Open a handle to the LSA. Specifying NULL for the Server means that the
  74. // server is the same as the client.
  75. //
  76. Status = LsaOpenPolicy(NULL,
  77. &ObjectAttributes,
  78. POLICY_LOOKUP_NAMES,
  79. &ConnectHandle
  80. );
  81. if (!NT_SUCCESS(Status)) {
  82. DbgPrint("LSM - Lsa Open failed 0x%lx\n", Status);
  83. return NULL;
  84. }
  85. return(ConnectHandle);
  86. }
  87. /****************************************************************************
  88. FUNCTION: CloseLsa
  89. PURPOSE: Closes the Lsa
  90. ****************************************************************************/
  91. VOID CloseLsa(
  92. LSA_HANDLE LsaHandle)
  93. {
  94. NTSTATUS Status;
  95. Status = LsaClose(LsaHandle);
  96. if (!NT_SUCCESS(Status)) {
  97. DbgPrint("LSM - Lsa Close failed 0x%lx\n", Status);
  98. }
  99. return;
  100. }
  101. #endif
  102. #ifdef LSA_AVAILABLE
  103. /****************************************************************************
  104. FUNCTION: SID2Name
  105. PURPOSE: Converts a SID into a readable string.
  106. RETURNS : TRUE on success otherwise FALSE.
  107. ****************************************************************************/
  108. BOOL SID2Name(
  109. PSID Sid,
  110. LPSTR String,
  111. UINT MaxStringBytes)
  112. {
  113. NTSTATUS Status;
  114. ANSI_STRING AnsiName;
  115. PLSA_REFERENCED_DOMAIN_LIST DomainList;
  116. PLSA_TRANSLATED_NAME NameList;
  117. if (LsaHandle == NULL) {
  118. DbgPrint("SECEDIT : Lsa not open yet\n");
  119. return(FALSE);
  120. }
  121. Status = LsaLookupSids(LsaHandle, 1, &Sid, &DomainList, &NameList);
  122. if (NT_SUCCESS(Status)) {
  123. // Convert to ansi string
  124. RtlUnicodeStringToAnsiString(&AnsiName, &NameList->Name, TRUE);
  125. // Free up the returned data
  126. LsaFreeMemory((PVOID)DomainList);
  127. LsaFreeMemory((PVOID)NameList);
  128. // Copy the ansi string into our local variable
  129. strncpy(String, AnsiName.Buffer, MaxStringBytes);
  130. // Free up the ansi string
  131. RtlFreeAnsiString(&AnsiName);
  132. return(TRUE);
  133. }
  134. return(FALSE);
  135. }
  136. #else
  137. #include "..\..\..\inc\seopaque.h"
  138. /****************************************************************************
  139. FUNCTION: SID2Name
  140. PURPOSE: Converts a SID into a readable string.
  141. RETURNS : TRUE on success otherwise FALSE.
  142. ****************************************************************************/
  143. BOOL SID2Name(
  144. PSID Sid,
  145. LPSTR String,
  146. UINT MaxStringBytes)
  147. {
  148. UCHAR Buffer[128];
  149. UCHAR i;
  150. ULONG Tmp;
  151. PISID iSid = (PISID)Sid; // pointer to opaque structure
  152. PSID NextSid = (PSID)Alloc(RtlLengthRequiredSid(1));
  153. NTSTATUS Status;
  154. ANSI_STRING AnsiName;
  155. if (NextSid == NULL) {
  156. DbgPrint("SECEDIT: SID2Name failed to allocate space for SID\n");
  157. return(FALSE);
  158. }
  159. {
  160. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  161. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  162. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_WORLD_RID;
  163. if (RtlEqualSid(Sid, NextSid)) {
  164. strcpy(String, "World");
  165. Free((PVOID)NextSid);
  166. return(TRUE);
  167. }
  168. }
  169. {
  170. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  171. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  172. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_LOCAL_RID;
  173. if (RtlEqualSid(Sid, NextSid)) {
  174. strcpy(String, "Local");
  175. Free((PVOID)NextSid);
  176. return(TRUE);
  177. }
  178. }
  179. {
  180. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  181. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  182. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_CREATOR_OWNER_RID;
  183. if (RtlEqualSid(Sid, NextSid)) {
  184. strcpy(String, "Creator");
  185. Free((PVOID)NextSid);
  186. return(TRUE);
  187. }
  188. }
  189. {
  190. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
  191. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  192. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_DIALUP_RID;
  193. if (RtlEqualSid(Sid, NextSid)) {
  194. strcpy(String, "Dialup");
  195. Free((PVOID)NextSid);
  196. return(TRUE);
  197. }
  198. }
  199. {
  200. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
  201. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  202. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_NETWORK_RID;
  203. if (RtlEqualSid(Sid, NextSid)) {
  204. strcpy(String, "Network");
  205. Free((PVOID)NextSid);
  206. return(TRUE);
  207. }
  208. }
  209. {
  210. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
  211. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  212. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_BATCH_RID;
  213. if (RtlEqualSid(Sid, NextSid)) {
  214. strcpy(String, "Batch");
  215. Free((PVOID)NextSid);
  216. return(TRUE);
  217. }
  218. }
  219. {
  220. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
  221. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  222. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_INTERACTIVE_RID;
  223. if (RtlEqualSid(Sid, NextSid)) {
  224. strcpy(String, "Interactive");
  225. Free((PVOID)NextSid);
  226. return(TRUE);
  227. }
  228. }
  229. {
  230. SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
  231. RtlInitializeSid(NextSid, &SidAuthority, 1 );
  232. *(RtlSubAuthoritySid(NextSid, 0)) = SECURITY_LOCAL_SYSTEM_RID;
  233. if (RtlEqualSid(Sid, NextSid)) {
  234. strcpy(String, "Local System");
  235. Free((PVOID)NextSid);
  236. return(TRUE);
  237. }
  238. }
  239. Free((PVOID)NextSid);
  240. wsprintf(Buffer, "S-%u-", (USHORT)iSid->Revision );
  241. lstrcpy(String, Buffer);
  242. if ( (iSid->IdentifierAuthority.Value[0] != 0) ||
  243. (iSid->IdentifierAuthority.Value[1] != 0) ){
  244. wsprintf(Buffer, "0x%02hx%02hx%02hx%02hx%02hx%02hx",
  245. (USHORT)iSid->IdentifierAuthority.Value[0],
  246. (USHORT)iSid->IdentifierAuthority.Value[1],
  247. (USHORT)iSid->IdentifierAuthority.Value[2],
  248. (USHORT)iSid->IdentifierAuthority.Value[3],
  249. (USHORT)iSid->IdentifierAuthority.Value[4],
  250. (USHORT)iSid->IdentifierAuthority.Value[5] );
  251. lstrcat(String, Buffer);
  252. } else {
  253. Tmp = (ULONG)iSid->IdentifierAuthority.Value[5] +
  254. (ULONG)(iSid->IdentifierAuthority.Value[4] << 8) +
  255. (ULONG)(iSid->IdentifierAuthority.Value[3] << 16) +
  256. (ULONG)(iSid->IdentifierAuthority.Value[2] << 24);
  257. wsprintf(Buffer, "%lu", Tmp);
  258. lstrcat(String, Buffer);
  259. }
  260. for (i=0;i<iSid->SubAuthorityCount ;i++ ) {
  261. wsprintf(Buffer, "-%lu", iSid->SubAuthority[i]);
  262. lstrcat(String, Buffer);
  263. }
  264. return(TRUE);
  265. }
  266. #endif
  267. /****************************************************************************
  268. FUNCTION: PRIV2Name
  269. PURPOSE: Converts a PRIVILEGE into a readable string.
  270. RETURNS : TRUE on success otherwise FALSE.
  271. ****************************************************************************/
  272. BOOL PRIV2Name(
  273. LUID Privilege,
  274. LPSTR lpstr,
  275. UINT MaxStringBytes)
  276. {
  277. NTSTATUS Status;
  278. STRING String;
  279. PUNICODE_STRING UString;
  280. LSA_HANDLE PolicyHandle;
  281. OBJECT_ATTRIBUTES ObjectAttributes;
  282. InitializeObjectAttributes( &ObjectAttributes, NULL, 0, 0, NULL);
  283. Status = LsaOpenPolicy( NULL, &ObjectAttributes, POLICY_LOOKUP_NAMES, &PolicyHandle);
  284. Status = LsaLookupPrivilegeName(PolicyHandle, &Privilege, &UString);
  285. if (!NT_SUCCESS(Status)) {
  286. DbgPrint("SECEDIT: LsaLookupPrivilegeName failed, status = 0x%lx\n", Status);
  287. strcpy(lpstr, "<Unknown>");
  288. } else {
  289. //
  290. // Convert it to ANSI - because that's what the rest of the app is.
  291. //
  292. if (UString->Length > (USHORT)MaxStringBytes) {
  293. DbgPrint("SECEDIT: Truncating returned privilege name: *%Z*\n", UString);
  294. UString->Length = (USHORT)MaxStringBytes;
  295. DbgPrint(" To: *%Z*\n", UString);
  296. }
  297. String.Length = 0;
  298. String.MaximumLength = (USHORT)MaxStringBytes;
  299. String.Buffer = lpstr;
  300. Status = RtlUnicodeStringToAnsiString( &String, UString, FALSE );
  301. ASSERT(NT_SUCCESS(Status));
  302. LsaFreeMemory( UString );
  303. }
  304. return(TRUE);
  305. }