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.

291 lines
6.5 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: context.c
  7. //
  8. // Contents: Context manipulation functions
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 2-26-97 RichardW Created
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "xtcbpkg.h"
  18. CRITICAL_SECTION XtcbContextLock ;
  19. //+---------------------------------------------------------------------------
  20. //
  21. // Function: XtcbInitializeContexts
  22. //
  23. // Synopsis: Initialization function
  24. //
  25. // Arguments: (none)
  26. //
  27. // History: 8-15-98 RichardW Created
  28. //
  29. // Notes:
  30. //
  31. //----------------------------------------------------------------------------
  32. NTSTATUS
  33. XtcbInitializeContexts(
  34. VOID
  35. )
  36. {
  37. NTSTATUS Status ;
  38. Status = STATUS_SUCCESS ;
  39. try
  40. {
  41. InitializeCriticalSection( &XtcbContextLock );
  42. }
  43. except ( EXCEPTION_EXECUTE_HANDLER )
  44. {
  45. Status = GetExceptionCode();
  46. }
  47. return Status ;
  48. }
  49. //+---------------------------------------------------------------------------
  50. //
  51. // Function: XtcbCreateContextRecord
  52. //
  53. // Synopsis: Create a context record for use during authentication
  54. //
  55. // Arguments: [Type] -- Type of context
  56. // [Handle] -- Credential handle that this context is derived from
  57. //
  58. // History: 2-26-97 RichardW Created
  59. //
  60. // Notes:
  61. //
  62. //----------------------------------------------------------------------------
  63. PXTCB_CONTEXT
  64. XtcbCreateContextRecord(
  65. XTCB_CONTEXT_TYPE Type,
  66. PXTCB_CRED_HANDLE Handle
  67. )
  68. {
  69. PXTCB_CONTEXT Context ;
  70. Context = (PXTCB_CONTEXT) LocalAlloc( LMEM_FIXED, sizeof( XTCB_CONTEXT) );
  71. if ( Context )
  72. {
  73. Context->Core.Check = XTCB_CONTEXT_CHECK ;
  74. Context->Core.Type = Type ;
  75. Context->Core.State = ContextFirstCall ;
  76. Context->CredHandle = (LSA_SEC_HANDLE) Handle ;
  77. XtcbRefCredHandle( Handle );
  78. //
  79. // Set initial count to 2, one for the context handle
  80. // that will be returned, and one for the reference that
  81. // indicates that we are currently working on it.
  82. //
  83. Context->Core.RefCount = 2 ;
  84. }
  85. return Context ;
  86. }
  87. //+---------------------------------------------------------------------------
  88. //
  89. // Function: XtcbDeleteContextRecord
  90. //
  91. // Synopsis: Deletes a security context record
  92. //
  93. // Arguments: [Context] -- Context
  94. //
  95. // History: 2-26-97 RichardW Created
  96. //
  97. // Notes:
  98. //
  99. //----------------------------------------------------------------------------
  100. VOID
  101. XtcbDeleteContextRecord(
  102. PXTCB_CONTEXT Context
  103. )
  104. {
  105. #if DBG
  106. if ( Context->Core.Check != XTCB_CONTEXT_CHECK )
  107. {
  108. DebugLog(( DEB_ERROR, "DeleteContext: not a valid context record: %x\n",
  109. Context ));
  110. return;
  111. }
  112. #endif
  113. XtcbDerefCredHandle( (PXTCB_CRED_HANDLE) Context->CredHandle );
  114. LocalFree( Context );
  115. }
  116. VOID
  117. XtcbDerefContextRecordEx(
  118. PXTCB_CONTEXT Context,
  119. LONG RefBy
  120. )
  121. {
  122. LONG RefCount ;
  123. EnterCriticalSection( &XtcbContextLock );
  124. Context->Core.RefCount -= RefBy ;
  125. RefCount = Context->Core.RefCount ;
  126. LeaveCriticalSection( &XtcbContextLock );
  127. if ( RefCount )
  128. {
  129. return ;
  130. }
  131. #if DBG
  132. if ( RefCount < 0 )
  133. {
  134. DebugLog(( DEB_ERROR, "Refcount below 0\n" ));
  135. }
  136. #endif
  137. XtcbDeleteContextRecord( Context );
  138. }
  139. BOOL
  140. XtcbRefContextRecord(
  141. PXTCB_CONTEXT Context
  142. )
  143. {
  144. BOOL Ret ;
  145. Ret = FALSE ;
  146. EnterCriticalSection( &XtcbContextLock );
  147. try
  148. {
  149. if ( Context->Core.Check == XTCB_CONTEXT_CHECK )
  150. {
  151. if ( Context->Core.RefCount > 0 )
  152. {
  153. Context->Core.RefCount++ ;
  154. Ret = TRUE ;
  155. }
  156. }
  157. }
  158. except (EXCEPTION_EXECUTE_HANDLER)
  159. {
  160. Ret = FALSE ;
  161. }
  162. LeaveCriticalSection( &XtcbContextLock );
  163. return Ret ;
  164. }
  165. //+---------------------------------------------------------------------------
  166. //
  167. // Function: XtcbMapContextToUser
  168. //
  169. // Synopsis: Prepares a context to be mapped to usermode by the LSA
  170. //
  171. // Arguments: [Context] --
  172. // [ContextBuffer] --
  173. //
  174. // History: 3-28-97 RichardW Created
  175. //
  176. // Notes:
  177. //
  178. //----------------------------------------------------------------------------
  179. NTSTATUS
  180. XtcbMapContextToUser(
  181. PXTCB_CONTEXT Context,
  182. PSecBuffer ContextBuffer
  183. )
  184. {
  185. PXTCB_CONTEXT_CORE NewContext ;
  186. NTSTATUS Status ;
  187. HANDLE DupHandle ;
  188. NewContext = LsaTable->AllocateLsaHeap( sizeof( XTCB_CONTEXT_CORE ) );
  189. if ( NewContext )
  190. {
  191. CopyMemory( NewContext, &Context->Core, sizeof( XTCB_CONTEXT_CORE ) );
  192. switch ( Context->Core.Type )
  193. {
  194. case XtcbContextClient:
  195. NewContext->Type = XtcbContextClientMapped ;
  196. Context->Core.Type = XtcbContextClientMapped ;
  197. break;
  198. case XtcbContextServer:
  199. NewContext->Type = XtcbContextServerMapped ;
  200. Context->Core.Type = XtcbContextClientMapped ;
  201. Status = LsaTable->DuplicateHandle( Context->Token,
  202. &DupHandle );
  203. DebugLog(( DEB_TRACE, "New token = %x\n", DupHandle ));
  204. if ( !NT_SUCCESS( Status ) )
  205. {
  206. DebugLog(( DEB_ERROR, "Failed to dup handle, %x\n",
  207. Status ));
  208. goto MapContext_Cleanup ;
  209. }
  210. NewContext->CoreTokenHandle = (ULONG) ((ULONG_PTR)DupHandle) ;
  211. CloseHandle( Context->Token );
  212. Context->Token = NULL ;
  213. break;
  214. default:
  215. Status = SEC_E_INVALID_TOKEN ;
  216. goto MapContext_Cleanup ;
  217. break;
  218. }
  219. ContextBuffer->pvBuffer = NewContext ;
  220. ContextBuffer->cbBuffer = sizeof( XTCB_CONTEXT_CORE );
  221. return SEC_E_OK ;
  222. }
  223. else
  224. {
  225. Status = SEC_E_INSUFFICIENT_MEMORY ;
  226. }
  227. MapContext_Cleanup:
  228. if ( NewContext )
  229. {
  230. LsaTable->FreeLsaHeap( NewContext );
  231. }
  232. return Status ;
  233. }