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.

274 lines
9.2 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Scheduling Agent Service
  4. //
  5. // Microsoft Windows
  6. // Copyright (C) Microsoft Corporation, 1992 - 1996
  7. //
  8. // File: atsec.cxx
  9. //
  10. // Contents: Net Schedule API access checking routines.
  11. //
  12. // Functions: AtCheckSecurity
  13. // AtCreateSecurityObject
  14. // AtDeleteSecurityObject
  15. //
  16. // History: 06-Nov-92 vladimv created.
  17. // 30-May-96 EricB adapted for the scheduling agent.
  18. //
  19. //----------------------------------------------------------------------------
  20. //
  21. // Some NT header definitions conflict with some of the standard windows
  22. // definitions. Thus, the project precompiled header can't be used.
  23. //
  24. extern "C" {
  25. #include <nt.h> // NT definitions
  26. #include <ntrtl.h> // NT runtime library definitions
  27. #include <nturtl.h>
  28. #include <netevent.h>
  29. }
  30. #include <windef.h> // Win32 type definitions
  31. #include <winbase.h> // Win32 base API prototypes
  32. #include <winsvc.h> // Win32 service control APIs
  33. #include <winreg.h> // HKEY
  34. #include <lmcons.h> // LAN Manager common definitions
  35. #include <lmerr.h> // LAN Manager network error definitions
  36. #include <netlib.h> // LAN Man utility routines
  37. #include <netlibnt.h> // NetpNtStatusToApiStatus
  38. #include <rpc.h> // DataTypes and runtime APIs
  39. #include <rpcutil.h> // Prototypes for MIDL user functions
  40. #include <secobj.h> // ACE_DATA
  41. #include <..\..\..\smdebug\smdebug.h>
  42. #include <debug.hxx>
  43. #include "atsec.hxx"
  44. //
  45. // Security descriptor to control user access to the AT schedule service
  46. // configuration information.
  47. //
  48. PSECURITY_DESCRIPTOR AtGlobalSecurityDescriptor = NULL;
  49. #define AT_JOB_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | \
  50. AT_JOB_ADD | \
  51. AT_JOB_DEL | \
  52. AT_JOB_ENUM | \
  53. AT_JOB_GET_INFO)
  54. //
  55. // Structure that describes the mapping of Generic access rights to
  56. // object specific access rights for the AT schedule service security object.
  57. //
  58. GENERIC_MAPPING AtGlobalInformationMapping = {
  59. STANDARD_RIGHTS_READ | // Generic read
  60. AT_JOB_ENUM |
  61. AT_JOB_GET_INFO,
  62. STANDARD_RIGHTS_WRITE | // Generic write
  63. AT_JOB_ADD |
  64. AT_JOB_DEL,
  65. STANDARD_RIGHTS_EXECUTE, // Generic execute
  66. AT_JOB_ALL_ACCESS // Generic all
  67. };
  68. //+---------------------------------------------------------------------------
  69. //
  70. // Function: AtCheckSecurity
  71. //
  72. // Synopsis: Verify that the caller has the proper privilege.
  73. //
  74. // Arguments: [DesiredAccess] - the type of access.
  75. //
  76. // Returns: NERR_Success or reason for failure.
  77. //
  78. // Notes: This routine checks if an rpc caller is allowed to perform a
  79. // given AT service operation. Members of the groups LocalAdmin
  80. // and LocalBackupOperators are allowed to do all operations and
  81. // everybody else is not allowed to do anything.
  82. //
  83. //----------------------------------------------------------------------------
  84. NET_API_STATUS
  85. AtCheckSecurity(ACCESS_MASK DesiredAccess)
  86. {
  87. NTSTATUS NtStatus;
  88. NET_API_STATUS Status;
  89. HANDLE ClientToken;
  90. LPWSTR StringArray[2];
  91. WCHAR ErrorCodeString[25];
  92. if ((Status = RpcImpersonateClient(NULL)) != NERR_Success)
  93. {
  94. ERR_OUT("RpcImpersonateClient", Status);
  95. return Status;
  96. }
  97. NtStatus = NtOpenThreadToken(NtCurrentThread(),
  98. TOKEN_QUERY,
  99. (BOOLEAN)TRUE,
  100. &ClientToken);
  101. if (NtStatus != STATUS_SUCCESS)
  102. {
  103. ERR_OUT("NtOpenThreadToken", NtStatus);
  104. }
  105. else
  106. {
  107. PRIVILEGE_SET PrivilegeSet;
  108. DWORD PrivilegeSetLength;
  109. ACCESS_MASK GrantedAccess;
  110. NTSTATUS AccessStatus;
  111. PrivilegeSetLength = sizeof( PrivilegeSet);
  112. // NtAccessCheck() returns STATUS_SUCCESS if parameters
  113. // are correct. Whether or not access is allowed is
  114. // governed by the returned value of AccessStatus.
  115. NtStatus = NtAccessCheck(
  116. AtGlobalSecurityDescriptor, // SecurityDescriptor
  117. ClientToken, // ClientToken
  118. DesiredAccess, // DesiredAccess
  119. &AtGlobalInformationMapping, // GenericMapping
  120. &PrivilegeSet,
  121. &PrivilegeSetLength,
  122. &GrantedAccess, // GrantedAccess
  123. &AccessStatus); // AccessStatus
  124. if (NtStatus != STATUS_SUCCESS)
  125. {
  126. ERR_OUT("NtAccessCheck", NtStatus);
  127. }
  128. else
  129. {
  130. NtStatus = AccessStatus;
  131. }
  132. NtClose(ClientToken);
  133. }
  134. if ((Status = RpcRevertToSelf()) != NERR_Success)
  135. {
  136. ERR_OUT("RpcRevertToSelf", Status);
  137. return Status;
  138. }
  139. return(NetpNtStatusToApiStatus(NtStatus));
  140. }
  141. //+----------------------------------------------------------------------------
  142. //
  143. // Function: AtCreateSecurityObject
  144. //
  145. // Synopsis: Creates the scheduler user-mode configuration information
  146. // object which is represented by a security descriptor.
  147. //
  148. // Returns: NERR_Success or reason for failure.
  149. //
  150. //-----------------------------------------------------------------------------
  151. NET_API_STATUS
  152. AtCreateSecurityObject(VOID)
  153. {
  154. DWORD SubmitControl;
  155. NTSTATUS status;
  156. DWORD type;
  157. DWORD Length;
  158. HKEY LsaKey;
  159. //
  160. // Server operators are permitted to manage the AT schedule service only if
  161. // the key exists and the proper flag is set. In all other case we do not
  162. // permit server operators to manage the AT schedule service.
  163. //
  164. status = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  165. SCH_LSA_REGISTRY_PATH,
  166. 0L,
  167. KEY_READ,
  168. &LsaKey);
  169. if (status != ERROR_SUCCESS)
  170. {
  171. ERR_OUT("RegOpenKeyEx(LsaKey)", status);
  172. return status;
  173. }
  174. Length = sizeof(SubmitControl);
  175. status = RegQueryValueEx(LsaKey,
  176. SCH_LSA_SUBMIT_CONTROL,
  177. NULL,
  178. &type,
  179. (LPBYTE)&SubmitControl,
  180. &Length);
  181. RegCloseKey(LsaKey);
  182. if (status != ERROR_SUCCESS ||
  183. type != REG_DWORD ||
  184. Length != sizeof(SubmitControl))
  185. {
  186. DBG_OUT3("SubmitControl reg value not found, "
  187. "ServerOps not enabled for AT cmd.");
  188. SubmitControl = 0;
  189. }
  190. status = NetpCreateWellKnownSids(NULL);
  191. if (!NT_SUCCESS(status))
  192. {
  193. ERR_OUT("Failure to create security object", 0);
  194. return NetpNtStatusToApiStatus(status);
  195. }
  196. //
  197. // Order matters! These ACEs are inserted into the DACL in the
  198. // following order. Security access is granted or denied based on
  199. // the order of the ACEs in the DACL.
  200. //
  201. // In win3.1 both LocalGroupAdmins and LocalGroupSystemOps were
  202. // allowed to perform all Schedule Service operations. In win3.5
  203. // LocalGroupSystemOps may be disallowed (this is the default case).
  204. //
  205. ACE_DATA aceData[] = {
  206. {ACCESS_ALLOWED_ACE_TYPE, 0, 0, GENERIC_ALL, &AliasAdminsSid},
  207. {ACCESS_ALLOWED_ACE_TYPE, 0, 0, GENERIC_ALL, &AliasSystemOpsSid}
  208. };
  209. status = NetpCreateSecurityObject(
  210. aceData, // pAceData
  211. (SubmitControl & SCH_SERVER_OPS) ? 2 : 1, // countAceData
  212. NULL, // OwnerSid
  213. NULL, // PrimaryGroupSid
  214. &AtGlobalInformationMapping,
  215. &AtGlobalSecurityDescriptor); // ppNewDescriptor
  216. if (!NT_SUCCESS(status))
  217. {
  218. ERR_OUT("Failure to create security object", 0);
  219. return NetpNtStatusToApiStatus(status);
  220. }
  221. return NERR_Success;
  222. }
  223. //+---------------------------------------------------------------------------
  224. //
  225. // Function: AtDeleteSecurityObject
  226. //
  227. // Synopsis: Destroys the schedule service user-mode configuration
  228. // information object.
  229. //
  230. // Returns: NERR_Success or reason for failure.
  231. //
  232. //----------------------------------------------------------------------------
  233. void
  234. AtDeleteSecurityObject(VOID)
  235. {
  236. if (AtGlobalSecurityDescriptor != NULL)
  237. {
  238. NetpDeleteSecurityObject(&AtGlobalSecurityDescriptor);
  239. NetpFreeWellKnownSids();
  240. }
  241. }