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.

298 lines
9.1 KiB

  1. //
  2. // System include files.
  3. //
  4. #include <stdlib.h>
  5. #include <nt.h>
  6. #include <ntrtl.h>
  7. #include <nturtl.h>
  8. // #include <lmcons.h>
  9. #include <ntsam.h>
  10. #include <ntlsa.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <windef.h>
  14. #include <winbase.h>
  15. #include <winnetwk.h>
  16. #include <crt\ctype.h>
  17. // #include "seopaque.h"
  18. #include <lmaccess.h>
  19. #define USAGE_ARG 0
  20. #define INVALID_ACC 1
  21. #define INVALID_ARG 2
  22. #define INVALID_PTH 3
  23. #define INVALID_SWT 4
  24. #define INVALID_FIL 5
  25. #define HELP 6
  26. #define MAXARGS 4
  27. #define LSA_WIN_STANDARD_BUFFER_SIZE 0x000000200L
  28. #define STANDARD_BUFFER_SIZE 512
  29. char *MESSAGES[] =
  30. {
  31. "PERMS [domain\\|computer\\]user path [/i] [/s] [/?]\n",
  32. "User on domain or computer can't be located or accessed.",
  33. "Invalid argument: \n",
  34. "Path to file is not valid.",
  35. "Invalid switch.",
  36. "File name can't be located: ",
  37. "Displays a user's permissions to specified files and directories.\n\n"
  38. "PERMS [domain\\|computer\\]username path [/i] [/s] [/?] \n\n"
  39. " [domain\\|computer\\]username\n"
  40. " Name of user whose permissions are to be checked. If \n"
  41. " no domain is given, defaults to local computer.\n\n"
  42. " path A file or directory, wildcards (*,?) are accepted.\n\n"
  43. " /i Assumes the specified user is logged on interactively\n"
  44. " to computer where the file/directory resides.\n"
  45. " With this switch, PERMS assumes the user is a member\n"
  46. " of the INTERACTIVE group. Without this switch, PERMS\n"
  47. " assumes the user is a member of the NETWORK group.\n\n"
  48. " /s Check permissions on files in subdirectories.\n\n"
  49. "The following letters indicate granted access types:\n\n"
  50. " R Read \n"
  51. " W Write \n"
  52. " X Execute \n"
  53. " D Delete \n"
  54. " P Change Permissions \n"
  55. " O Take Ownership \n\n"
  56. " A General All \n"
  57. " - No Access \n\n"
  58. "* The specified user is the owner of the file or directory.\n"
  59. "# A group the user is a member of owns the file or directory.\n\n"
  60. "? The user's access permisssions can not be determined or the information\n"
  61. " may not exist (if the file system is FAT).\n"
  62. };
  63. static SID_IDENTIFIER_AUTHORITY SepNullSidAuthority = SECURITY_NULL_SID_AUTHORITY;
  64. static SID_IDENTIFIER_AUTHORITY SepWorldSidAuthority = SECURITY_WORLD_SID_AUTHORITY;
  65. static SID_IDENTIFIER_AUTHORITY SepLocalSidAuthority = SECURITY_LOCAL_SID_AUTHORITY;
  66. static SID_IDENTIFIER_AUTHORITY SepCreatorSidAuthority = SECURITY_CREATOR_SID_AUTHORITY;
  67. static SID_IDENTIFIER_AUTHORITY SepNtAuthority = SECURITY_NT_AUTHORITY;
  68. //
  69. // Universal well known SIDs
  70. //
  71. PSID SeNullSid;
  72. PSID SeWorldSid;
  73. PSID SeLocalSid;
  74. PSID SeCreatorOwnerSid;
  75. PSID SeCreatorGroupSid;
  76. //
  77. // Sids defined by NT
  78. //
  79. PSID SeNtAuthoritySid;
  80. PSID SeDialupSid;
  81. PSID SeNetworkSid;
  82. PSID SeBatchSid;
  83. PSID SeInteractiveSid;
  84. PSID SeServiceSid;
  85. PSID SeLocalSystemSid;
  86. PSID SeAliasAdminsSid;
  87. PSID SeAliasUsersSid;
  88. PSID SeAliasGuestsSid;
  89. PSID SeAliasPowerUsersSid;
  90. PSID SeAliasAccountOpsSid;
  91. PSID SeAliasSystemOpsSid;
  92. PSID SeAliasPrintOpsSid;
  93. PSID SeAliasBackupOpsSid;
  94. //
  95. // System default DACL
  96. //
  97. PACL SeSystemDefaultDacl;
  98. PACL SePublicDefaultDacl;
  99. #define TstAllocatePool(IgnoredPoolType,NumberOfBytes) \
  100. RtlAllocateHeap(RtlProcessHeap(), 0, NumberOfBytes)
  101. #define TstDeallocatePool(Pointer) \
  102. RtlFreeHeap(RtlProcessHeap(), 0, Pointer)
  103. OBJECT_ATTRIBUTES ObjectAttributes;
  104. SECURITY_QUALITY_OF_SERVICE SecurityQualityOfService;
  105. //
  106. // Globally Visible Table of Sids.
  107. //
  108. PSID AccountDomainSid = NULL;
  109. PSID PrimaryDomainSid = NULL;
  110. PSID *TrustedDomainSids = NULL;
  111. BOOL
  112. VariableInitialization();
  113. BOOL
  114. LookupSidsInSamDomain(
  115. IN OPTIONAL PUNICODE_STRING WorkstationName,
  116. IN PUNICODE_STRING DomainControllerName,
  117. IN PUNICODE_STRING SamDomainName
  118. );
  119. BOOL
  120. GeneralBuildSid(
  121. PSID *Sid,
  122. PSID DomainSid,
  123. ULONG RelativeId
  124. );
  125. VOID
  126. InitObjectAttributes(
  127. IN POBJECT_ATTRIBUTES ObjectAttributes,
  128. IN PSECURITY_QUALITY_OF_SERVICE SecurityQualityOfService
  129. );
  130. VOID usage(INT message_num, PCHAR string_val);
  131. BOOL LookupAllUserSidsWS(LPSTR lpSystemName);
  132. VOID DisplayPerms(IN LPTSTR filename,
  133. IN BOOL valid_access);
  134. BOOL GetFilePermissions(
  135. PSECURITY_DESCRIPTOR SecurityDescriptor,
  136. PSID UserAccountSids);
  137. BOOL IsDomainName(
  138. LPSTR TestDomainName,
  139. LPSTR DomainNameBuff);
  140. BOOL ProcessAcl(
  141. PACL Acl
  142. );
  143. BOOL SetBackOperatorPriv(HANDLE TokenHandle);
  144. BOOL GetTokenHandle(PHANDLE TokenHandle);
  145. BOOL GetFileSecurityBackup(
  146. LPSTR lpFileName,
  147. SECURITY_INFORMATION RequestedInformation,
  148. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  149. DWORD nLength,
  150. LPDWORD lpnLengthNeeded,
  151. BOOL BackUpPrivFlag
  152. );
  153. BOOL GetFileSecurityBackupW(
  154. LPWSTR lpFileName,
  155. SECURITY_INFORMATION RequestedInformation,
  156. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  157. DWORD nLength,
  158. LPDWORD lpnLengthNeeded,
  159. BOOL UseBackUp
  160. );
  161. VOID QuerySecAccessMask(
  162. IN SECURITY_INFORMATION SecurityInformation,
  163. OUT LPDWORD DesiredAccess
  164. );
  165. BOOL CleanUpSource(IN LPTSTR InSting,
  166. OUT LPTSTR OutString,
  167. OUT BOOL *DirectoryFlag);
  168. BOOL IsDirectory(IN LPTSTR InTestFile,
  169. IN BOOL *ValidFile);
  170. BOOL IsWildCard(IN LPSTR psz);
  171. BOOL SetSlash(IN LPTSTR InString,
  172. IN OUT LPTSTR TestString);
  173. BOOL RemoveEndSlash(LPSTR psz);
  174. BOOL AddDotSlash(LPSTR TestString);
  175. BOOL AddWildCards(LPSTR TestString);
  176. BOOL IsLastCharSlash(LPSTR TestString);
  177. BOOL StripRootDir(IN LPTSTR InDir,
  178. OUT LPTSTR OutRootDir);
  179. BOOL RemoveEndDot(LPSTR TestString);
  180. BOOL IsRelativeString(LPSTR TestString);
  181. //
  182. // Macros for calculating the address of the components of a security
  183. // descriptor. This will calculate the address of the field regardless
  184. // of whether the security descriptor is absolute or self-relative form.
  185. // A null value indicates the specified field is not present in the
  186. // security descriptor.
  187. //
  188. #define SepOwnerAddrSecurityDescriptor( SD ) \
  189. ( ((SD)->Owner == NULL) ? (PSID)NULL : \
  190. ( ((SD)->Control & SE_SELF_RELATIVE) ? \
  191. (PSID)RtlOffsetToPointer((SD), (SD)->Owner) : \
  192. (PSID)((SD)->Owner) \
  193. ) \
  194. )
  195. #define SepGroupAddrSecurityDescriptor( SD ) \
  196. ( ((SD)->Group == NULL) ? (PSID)NULL : \
  197. ( ((SD)->Control & SE_SELF_RELATIVE) ? \
  198. (PSID)RtlOffsetToPointer((SD), (SD)->Group) : \
  199. (PSID)((SD)->Group) \
  200. ) \
  201. )
  202. #define SepSaclAddrSecurityDescriptor( SD ) \
  203. ( (!((SD)->Control & SE_SACL_PRESENT) || ((SD)->Sacl == NULL) ) ? \
  204. (PACL)NULL : \
  205. ( ((SD)->Control & SE_SELF_RELATIVE) ? \
  206. (PACL)RtlOffsetToPointer((SD), (SD)->Sacl) : \
  207. (PACL)((SD)->Sacl) \
  208. ) \
  209. )
  210. #define SepDaclAddrSecurityDescriptor( SD ) \
  211. ( (!((SD)->Control & SE_DACL_PRESENT) || ((SD)->Dacl == NULL) ) ? \
  212. (PACL)NULL : \
  213. ( ((SD)->Control & SE_SELF_RELATIVE) ? \
  214. (PACL)RtlOffsetToPointer((SD), (SD)->Dacl) : \
  215. (PACL)((SD)->Dacl) \
  216. ) \
  217. )
  218. BOOL RecurseSubs(IN LPTSTR FileName,
  219. IN LPTSTR FilePath,
  220. IN PSID UserSid,
  221. IN BOOL BackPriv,
  222. IN BOOL Recurse);
  223. VOID syserror(IN DWORD error_val);
  224. #define LARGEPSID 2048
  225. #define FILE_GEN_ALL 0x001f01ff
  226. #define FirstAce(Acl) ((PVOID)((PUCHAR)(Acl) + sizeof(ACL)))
  227. #define NextAce(Ace) ((PVOID)((PUCHAR)(Ace) + ((PACE_HEADER)(Ace))->AceSize))