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.

480 lines
16 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1991 - 1992
  6. //
  7. // File: SPMGR.H
  8. //
  9. // Contents: Common structures and functions for the SPMgr
  10. //
  11. //
  12. // History: 20 May 92 RichardW Documented existing stuff
  13. // 22 Jul 93 RichardW Revised to be the one include file
  14. // for the spm directory
  15. //
  16. //------------------------------------------------------------------------
  17. #ifndef __SPMGR_H__
  18. #define __SPMGR_H__
  19. #define SECURITY_WIN32
  20. #define SECURITY_PACKAGE
  21. #include <security.h>
  22. #include <secint.h>
  23. #include <spmlpc.h>
  24. #include <lsaperf.h>
  25. #include <lsapmsgs.h> // event log events
  26. // SPM-wide structure definitions:
  27. // This is the function table for a security package. All functions are
  28. // dispatched through this table.
  29. struct _DLL_BINDING;
  30. // This is the Security Package Control structure. All control information
  31. // relating to packages is stored here.
  32. typedef struct _LSAP_SECURITY_PACKAGE {
  33. ULONG_PTR dwPackageID; // Assigned package ID
  34. DWORD PackageIndex; // Package Index in DLL
  35. DWORD fPackage; // Flags about the package
  36. DWORD fCapabilities; // Capabilities that the package reported
  37. DWORD dwRPCID; // RPC ID
  38. DWORD Version;
  39. DWORD TokenSize;
  40. DWORD ContextHandles ; // Number of outstanding contexts
  41. DWORD CredentialHandles ; // ditto for credentials
  42. LONG CallsInProgress ; // Number of calls to this package
  43. SECURITY_STRING Name; // Name of the package
  44. SECURITY_STRING Comment;
  45. struct _DLL_BINDING * pBinding; // Binding of DLL
  46. PSECPKG_EXTENDED_INFORMATION Thunks ; // Thunked Context levels
  47. LIST_ENTRY ScavengerList ;
  48. SECURITY_STRING WowClientDll ;
  49. SECPKG_FUNCTION_TABLE FunctionTable; // Dispatch table
  50. #ifdef TRACK_MEM
  51. PVOID pvMemStats; // Memory statistics
  52. #endif
  53. } LSAP_SECURITY_PACKAGE, * PLSAP_SECURITY_PACKAGE;
  54. #define SP_INVALID 0x00000001 // Package is now invalid for use
  55. #define SP_UNLOAD 0x00000002 // Package is being unloaded
  56. #define SP_INTERNAL 0x00000008 // Package is internal, do not unload
  57. #define SP_PREFERRED 0x00000020 // The preferred package
  58. #define SP_DELETE_PEND 0x00000040 // Package Delete pending
  59. #define SP_INFO 0x00000080 // Supports Extended Info
  60. #define SP_CONTEXT_INFO 0x00000100 // Wants some info levels thunked
  61. #define SP_SHUTDOWN_PENDING 0x00000200 // Shutdown has been called
  62. #define SP_SHUTDOWN 0x00000400 // Shutdown has completed
  63. #define SP_WOW_SUPPORT 0x00000800 // Package can support WOW6432 clients
  64. #define StartCallToPackage( p ) \
  65. InterlockedIncrement( &((PLSAP_SECURITY_PACKAGE) p)->CallsInProgress )
  66. #define EndCallToPackage( p ) \
  67. InterlockedDecrement( &((PLSAP_SECURITY_PACKAGE) p)->CallsInProgress )
  68. typedef struct _DLL_BINDING {
  69. DWORD Flags; // Flags about the DLL
  70. HANDLE hInstance; // Instance Handle
  71. SECURITY_STRING Filename; // Full path name
  72. DWORD RefCount; // Reference Count
  73. DWORD PackageCount; // Number of Packages in DLL
  74. LSAP_SECURITY_PACKAGE Packages[1];
  75. } DLL_BINDING, * PDLL_BINDING;
  76. #define DLL_DEFAULT_MEM 0x00000001 // pPackages is self allocated
  77. #define DLL_BUILTIN 0x00000002 // DLL is really built-in code
  78. #define DLL_AUTHPKG 0x00000004 // DLL is an old auth package
  79. #define DLL_SIGNED 0x00000008 // DLL is signed
  80. //
  81. // Ordinals to the function pointers, for validating calls
  82. //
  83. #define SP_ORDINAL_LSA_INIT 0
  84. #define SP_ORDINAL_LOGONUSER 1
  85. #define SP_ORDINAL_CALLPACKAGE 2
  86. #define SP_ORDINAL_LOGONTERMINATED 3
  87. #define SP_ORDINAL_CALLPACKAGEUNTRUSTED 4
  88. #define SP_ORDINAL_CALLPACKAGEPASSTHROUGH 5
  89. #define SP_ORDINAL_LOGONUSEREX 6
  90. #define SP_ORDINAL_LOGONUSEREX2 7
  91. #define SP_ORDINAL_INITIALIZE 8
  92. #define SP_ORDINAL_SHUTDOWN 9
  93. #define SP_ORDINAL_GETINFO 10
  94. #define SP_ORDINAL_ACCEPTCREDS 11
  95. #define SP_ORDINAL_ACQUIRECREDHANDLE 12
  96. #define SP_ORDINAL_QUERYCREDATTR 13
  97. #define SP_ORDINAL_FREECREDHANDLE 14
  98. #define SP_ORDINAL_SAVECRED 15
  99. #define SP_ORDINAL_GETCRED 16
  100. #define SP_ORDINAL_DELETECRED 17
  101. #define SP_ORDINAL_INITLSAMODECTXT 18
  102. #define SP_ORDINAL_ACCEPTLSAMODECTXT 19
  103. #define SP_ORDINAL_DELETECTXT 20
  104. #define SP_ORDINAL_APPLYCONTROLTOKEN 21
  105. #define SP_ORDINAL_GETUSERINFO 22
  106. #define SP_ORDINAL_GETEXTENDEDINFORMATION 23
  107. #define SP_ORDINAL_QUERYCONTEXTATTRIBUTES 24
  108. #define SP_ORDINAL_ADDCREDENTIALS 25
  109. #define SP_ORDINAL_SETEXTENDEDINFORMATION 26
  110. #define SP_ORDINAL_SETCONTEXTATTRIBUTES 27
  111. #define SP_MAX_TABLE_ORDINAL (SP_ORDINAL_SETCONTEXTATTRIBUTES + 1)
  112. #define SP_MAX_AUTHPKG_ORDINAL (SP_ORDINAL_LOGONUSEREX)
  113. #define SP_ORDINAL_MASK 0x0000FFFF
  114. #define SP_ITERATE_FILTER_WOW 0x00010000
  115. #define SP_ORDINAL_INSTANCEINIT 32
  116. typedef struct _LsaState {
  117. DWORD fState ;
  118. DWORD cPackages ;
  119. DWORD cNewPackages ;
  120. } LsaState ;
  121. typedef enum _SECHANDLE_OPS {
  122. HandleSet, // Just set the new handle
  123. HandleReplace, // Replace the existing one
  124. HandleRemoveReplace // Remove provided, replace with provided
  125. } SECHANDLE_OPS ;
  126. typedef struct _LSA_TUNING_PARAMETERS {
  127. ULONG ThreadLifespan ; // lifespan for threads in gen. pool
  128. ULONG SubQueueLifespan ; // lifespan for dedicated threads
  129. ULONG Options ; // Option flags
  130. BOOL ShrinkOn ; // Thread pool is idle
  131. ULONG ShrinkCount ;
  132. ULONG ShrinkSkip ;
  133. } LSA_TUNING_PARAMETERS, * PLSA_TUNING_PARAMETERS ;
  134. #define TUNE_SRV_HIGH_PRIORITY 0x00000001
  135. #define TUNE_TRIM_WORKING_SET 0x00000002
  136. #define TUNE_ALLOW_PERFMON 0x00000004
  137. #define TUNE_RM_THREAD 0x00000008
  138. #define TUNE_PRIVATE_HEAP 0x00000010
  139. //
  140. // Redefine IsOkayToExec
  141. //
  142. #define IsOkayToExec(x)
  143. #ifdef TRACK_MEM
  144. #define TRACK_MEM_LEAK
  145. #define MEMHOOK_PACKAGE_LOAD 1
  146. void MemTrackHook(DWORD Type, DWORD Package);
  147. #else
  148. #define MemTrackHook(x,y)
  149. #endif
  150. // For some tracking purposes, the package ID for the SPMgr is a well known
  151. // constant:
  152. #define SPMGR_ID ((LSA_SEC_HANDLE) INVALID_HANDLE_VALUE)
  153. #define SPMGR_PKG_ID ((LSA_SEC_HANDLE) INVALID_HANDLE_VALUE)
  154. //
  155. // Value to pass to shutdown handler
  156. //
  157. #define SPM_SHUTDOWN_VALUE 0xD0
  158. //
  159. // Creating process name for LSA sessions
  160. //
  161. #define LSA_PROCESS_NAME L"LSA Server"
  162. //
  163. // ID of the primary package
  164. //
  165. #define PRIMARY_ID 0
  166. typedef struct _SpmExceptDbg {
  167. DWORD ThreadId;
  168. PVOID pInstruction;
  169. PVOID pMemory;
  170. ULONG_PTR Access;
  171. } SpmExceptDbg, * PSpmExceptDbg;
  172. // Internal Exception Handling:
  173. //
  174. // If we hit an exception in a debug build, we store away some useful stuff
  175. // otherwise, we go to the default case:
  176. LONG SpExceptionFilter(PVOID, EXCEPTION_POINTERS *);
  177. #define SP_EXCEPTION SpExceptionFilter(GetCurrentSession(), GetExceptionInformation())
  178. //
  179. // Include other component header files
  180. //
  181. #ifdef __cplusplus
  182. extern "C" {
  183. #endif
  184. #include "sesmgr.h" // Session manager support
  185. #include "sphelp.h" // Internal helper functions
  186. #include "protos.h" // Internal Prototypes
  187. #include "debug.h" // Debugging Support:
  188. #ifdef __cplusplus
  189. }
  190. #endif
  191. typedef struct _LSAP_DBG_LOG_CONTEXT {
  192. PSession Session ; // Session used
  193. SecHandle Handle ; // Handle used
  194. } LSAP_DBG_LOG_CONTEXT, *PLSAP_DBG_LOG_CONTEXT ;
  195. typedef struct _LSAP_API_LOG_ENTRY {
  196. ULONG MessageId ; // LPC Message ID
  197. ULONG ThreadId ; // Thread ID handling call
  198. PVOID pvMessage ; // LPC Message
  199. PVOID WorkItem ; // Work item for API
  200. LARGE_INTEGER QueueTime ; // Time Queued
  201. LARGE_INTEGER WorkTime ; // Work Time
  202. PVOID Reserved ; // Alignment
  203. LSAP_DBG_LOG_CONTEXT Context ; // Context
  204. } LSAP_API_LOG_ENTRY, * PLSAP_API_LOG_ENTRY ;
  205. typedef struct _LSAP_API_LOG {
  206. ULONG TotalSize ;
  207. ULONG Current ;
  208. ULONG ModSize ;
  209. ULONG Align ;
  210. LSAP_API_LOG_ENTRY Entries[ 1 ];
  211. } LSAP_API_LOG, * PLSAP_API_LOG ;
  212. PLSAP_API_LOG
  213. ApiLogCreate(
  214. ULONG Entries
  215. );
  216. PLSAP_API_LOG_ENTRY
  217. ApiLogAlloc(
  218. PLSAP_API_LOG Log
  219. );
  220. PLSAP_API_LOG_ENTRY
  221. ApiLogLocate(
  222. PLSAP_API_LOG Log,
  223. ULONG MessageId
  224. );
  225. #define DEFAULT_LOG_SIZE 32
  226. //#if DBG
  227. #define DBG_TRACK_API 1
  228. //#endif
  229. #if DBG_TRACK_API
  230. #define DBG_DISPATCH_PROLOGUE_EX( Entry, pMessage, CallInfo ) \
  231. if ( Entry ) \
  232. { \
  233. Entry->ThreadId = GetCurrentThreadId() ; \
  234. CallInfo.LogContext = & Entry->Context ; \
  235. GetSystemTimeAsFileTime( (LPFILETIME) &Entry->WorkTime ) ; \
  236. } \
  237. else \
  238. { \
  239. CallInfo.LogContext = NULL ; \
  240. }
  241. #define DBG_DISPATCH_PROLOGUE( Table, pMessage, CallInfo ) \
  242. PLSAP_API_LOG_ENTRY Entry ; \
  243. \
  244. Entry = ApiLogLocate( Table, ((PPORT_MESSAGE) pMessage)->MessageId ); \
  245. DBG_DISPATCH_PROLOGUE_EX( Entry, pMessage, CallInfo ) \
  246. #define DBG_DISPATCH_POSTLOGUE( Status, ApiCode ) \
  247. if ( Entry ) \
  248. { \
  249. LARGE_INTEGER EndTime ; \
  250. GetSystemTimeAsFileTime( (LPFILETIME) & EndTime ); \
  251. Entry->Reserved = ULongToPtr(Entry->ThreadId); \
  252. Entry->ThreadId = (DWORD) 0xFFFFFFFF ; \
  253. Entry->WorkItem = (PVOID) Status ; \
  254. Entry->pvMessage = (PVOID) ApiCode ; \
  255. Entry->QueueTime.QuadPart = EndTime.QuadPart ; \
  256. Entry->WorkTime.QuadPart = EndTime.QuadPart - Entry->WorkTime.QuadPart ; \
  257. }
  258. #else
  259. #define DBG_DISPATCH_PROLOGUE_EX( Entry, pMessage, CallInfo ) CallInfo.LogContext = NULL
  260. #define DBG_DISPATCH_PROLOGUE( Table, pApi, CallInfo ) CallInfo.LogContext = NULL
  261. #define DBG_DISPATCH_POSTLOGUE( Status, ApiCode )
  262. #endif
  263. #define MAX_BUFFERS_IN_CALL 8
  264. typedef struct _LSA_CALL_INFO {
  265. PSPM_LPC_MESSAGE Message ;
  266. struct _LSA_CALL_INFO * PreviousCall ;
  267. PSession Session ;
  268. PLSAP_DBG_LOG_CONTEXT LogContext ;
  269. SECPKG_CALL_INFO CallInfo ;
  270. //
  271. // LogonId, ImpersonationLevel, Impersonating, Restricted
  272. // are considered valid CachedTokenInfo is TRUE
  273. //
  274. LUID LogonId ;
  275. SECURITY_IMPERSONATION_LEVEL ImpersonationLevel;
  276. BOOLEAN Impersonating;
  277. BOOLEAN Restricted;
  278. BOOLEAN CachedTokenInfo;
  279. HANDLE InProcToken ;
  280. BOOL InProcCall ;
  281. ULONG Flags ;
  282. ULONG Allocs ;
  283. PKSEC_LSA_MEMORY_HEADER KMap ;
  284. PVOID Buffers[ MAX_BUFFERS_IN_CALL ];
  285. } LSA_CALL_INFO, * PLSA_CALL_INFO ;
  286. #define LsapGetCurrentCall() ((PLSA_CALL_INFO) TlsGetValue( dwCallInfo ))
  287. #define LsapSetCurrentCall(x) TlsSetValue( dwCallInfo, x )
  288. #define CALL_FLAG_IMPERSONATING 0x00000001
  289. #define CALL_FLAG_IN_PROC_CALL 0x00000002
  290. #define CALL_FLAG_SUPRESS_AUDIT 0x00000004
  291. #define CALL_FLAG_NO_HANDLE_CHK 0x00000008
  292. #define CALL_FLAG_KERNEL_POOL 0x00000010 // Kernel mode call, using pool
  293. #define CALL_FLAG_KMAP_USED 0x00000020 // KMap is valid
  294. //
  295. //BOOL
  296. //LsapIsBlockInKMap( KMap, Block )
  297. //
  298. #define LsapIsBlockInKMap( KMap, Block ) \
  299. ( KMap ? (((ULONG_PTR) KMap ^ (ULONG_PTR) Block ) < (ULONG_PTR) KMap->Commit) : FALSE )
  300. NTSTATUS
  301. InitializeDirectDispatcher(
  302. VOID
  303. );
  304. VOID
  305. LsapInitializeCallInfo(
  306. PLSA_CALL_INFO CallInfo,
  307. BOOL InProcess
  308. );
  309. NTSTATUS
  310. LsapBuildCallInfo(
  311. PSPM_LPC_MESSAGE pApiMessage,
  312. PLSA_CALL_INFO CallInfo,
  313. PHANDLE Impersonated,
  314. PSession * NewSession,
  315. PSession * OldSession
  316. );
  317. VOID
  318. LsapInternalBreak(
  319. VOID
  320. );
  321. #define LsapLogCallInfo( CallInfo, pSession, cHandle ) \
  322. if ( CallInfo && ( CallInfo->LogContext ) ) \
  323. { \
  324. CallInfo->LogContext->Session = pSession ; \
  325. CallInfo->LogContext->Handle = cHandle; \
  326. } \
  327. //
  328. // Global variables
  329. //
  330. extern HANDLE hLsaInst; // Instance handle of app
  331. extern LSA_SECPKG_FUNCTION_TABLE LsapSecpkgFunctionTable;
  332. // Dispatch table of helper functions
  333. extern LUID SystemLogonId; // System LogonID for packages.
  334. extern SECURITY_STRING MachineName; // Computer name
  335. extern HANDLE hStateChangeEvent; // Event set when the system state is changed
  336. extern HANDLE hShutdownEvent;
  337. extern HANDLE hPrelimShutdownEvent; // Event to tell Domain cache
  338. // manager that system is shutting
  339. // down
  340. extern HANDLE hRMStartupEvent;
  341. extern HANDLE hKSEvent;
  342. extern LSA_CALL_INFO LsapDefaultCallInfo ;
  343. extern ULONG LsapPageSize ; // Set to the page size during init
  344. extern ULONG_PTR LsapUserModeLimit ; // Set the to max user mode address
  345. // Thread Local Storage variables
  346. //
  347. // These are actually all indices into the tls area, accessed through the
  348. // TlsXxx functions. These are all initialized by the InitThreadData()
  349. // function
  350. extern DWORD dwThreadContext; // CallerContext pointer
  351. extern DWORD dwSession; // Session pointer
  352. extern DWORD dwLastError; // Last error value
  353. extern DWORD dwExceptionInfo; // Gets a pointer to exception info
  354. extern DWORD dwThreadPackage; // Package ID for thread
  355. extern DWORD dwCallInfo ; // CallInfo pointer
  356. extern DWORD dwThreadHeap; // Heap assigned to current thread.
  357. // Last known workstation status:
  358. extern int LastWkstaStatus;
  359. extern PSession pSpmgrSession; // SPMgr's session
  360. extern BOOLEAN DomainDsExists; // Has state been set to DS_DC?
  361. extern WCHAR szDsRegPath[];
  362. extern BOOLEAN SetupPhase; // If true, setup is running
  363. extern BOOL fShrinkMemory;
  364. extern BOOL ShutdownBegun ; // when true, shutdown is running
  365. extern LSA_TUNING_PARAMETERS LsaTuningParameters ;
  366. extern LsaState lsState ;
  367. extern PWSTR * ppszPackages; // Contains a null terminated array of dll names
  368. extern PWSTR * ppszOldPkgs; // Contains a null terminated array of old pkgs
  369. #endif // __SPMGR_H__