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.

473 lines
13 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1991 - 1992
  6. //
  7. // File: credapi.c
  8. //
  9. // Contents: Credential related APIs to the SPMgr
  10. // - LsaEstablishCreds
  11. // - LsaLogonUser
  12. // - LsaAcquireCredHandle
  13. // - LsaFreeCredHandle
  14. //
  15. //
  16. // History: 20 May 92 RichardW Commented existing code
  17. //
  18. //------------------------------------------------------------------------
  19. #include <lsapch.hxx>
  20. extern "C"
  21. {
  22. #include "adtp.h"
  23. #include "msaudite.h" // LsaAuditLogon
  24. }
  25. //+-------------------------------------------------------------------------
  26. //
  27. // Function: WLsaAcquireCredHandle
  28. //
  29. // Synopsis:
  30. //
  31. // Effects:
  32. //
  33. // Arguments:
  34. //
  35. // Requires:
  36. //
  37. // Returns:
  38. //
  39. // Notes:
  40. //
  41. //--------------------------------------------------------------------------
  42. NTSTATUS
  43. WLsaAcquireCredHandle( PSECURITY_STRING pPrincipal,
  44. PSECURITY_STRING pSecPackage,
  45. DWORD fCredentialUse,
  46. PLUID pLogonID,
  47. PVOID pvAuthData,
  48. PVOID pvGetKeyFn,
  49. PVOID pvGetKeyArgument,
  50. PCredHandle phCredential,
  51. PTimeStamp ptsExpiry)
  52. {
  53. PLSAP_SECURITY_PACKAGE pspPackage;
  54. NTSTATUS scRet;
  55. LUID CallerLogonID;
  56. PSession pSession = GetCurrentSession();
  57. SECPKG_CLIENT_INFO ClientInfo;
  58. PLSA_CALL_INFO CallInfo = LsapGetCurrentCall();
  59. //
  60. // Check if the caller is restricted
  61. //
  62. scRet = LsapGetClientInfo(&ClientInfo);
  63. if (!NT_SUCCESS(scRet))
  64. {
  65. DebugLog((DEB_ERROR,"Failed to get client info: 0x%x\n",scRet));
  66. return(scRet);
  67. }
  68. //
  69. // If the caller is restricted, fail the call for now. This should change
  70. // if packages are able to support restrictions. In that case, the call
  71. // should check the package capabilities for handling restrictions and
  72. // if it supports restrictions, allow the call to continue.
  73. //
  74. if (ClientInfo.Restricted)
  75. {
  76. DebugLog((DEB_WARN, "Trying to acquire credentials with a restrictred token\n"));
  77. scRet = SEC_E_NO_CREDENTIALS;
  78. return (scRet);
  79. }
  80. //
  81. // Todds - 08/02
  82. //
  83. // We used to disallow tokens with identification level or lower to
  84. // AcquireCredentialsHandle, thus prevent it from elevating to impersonation
  85. // level - but this check has been moved to the packages to allow some
  86. // s4uproxy scenarios to work w/o TCB.
  87. //
  88. #if DBG
  89. if (pPrincipal->Length)
  90. {
  91. DebugLog((DEB_TRACE_WAPI, "[%x] AcquireCredentialHandle(%ws, %ws)\n",
  92. pSession->dwProcessID, pPrincipal->Buffer, pSecPackage->Buffer));
  93. }
  94. else
  95. {
  96. DebugLog((DEB_TRACE_WAPI, "[%x] AcquireCredHandle(%x:%x, %ws)\n",
  97. pSession->dwProcessID, pLogonID->HighPart, pLogonID->LowPart,
  98. pSecPackage->Buffer));
  99. }
  100. #endif // DBG
  101. phCredential->dwUpper = 0;
  102. phCredential->dwLower = 0xFFFFFFFF;
  103. ptsExpiry->LowPart = 0;
  104. ptsExpiry->HighPart = 0;
  105. pspPackage = SpmpLookupPackageAndRequest(pSecPackage,
  106. SP_ORDINAL_ACQUIRECREDHANDLE);
  107. if (!pspPackage)
  108. {
  109. return(SEC_E_SECPKG_NOT_FOUND);
  110. }
  111. SetCurrentPackageId(pspPackage->dwPackageID);
  112. CallerLogonID = *pLogonID;
  113. StartCallToPackage( pspPackage );
  114. __try
  115. {
  116. scRet = pspPackage->FunctionTable.AcquireCredentialsHandle(pPrincipal,
  117. fCredentialUse,
  118. &CallerLogonID,
  119. pvAuthData,
  120. pvGetKeyFn,
  121. pvGetKeyArgument,
  122. &phCredential->dwUpper,
  123. ptsExpiry);
  124. }
  125. __except (SP_EXCEPTION)
  126. {
  127. scRet = GetExceptionCode();
  128. scRet = SPException(scRet, pspPackage->dwPackageID);
  129. }
  130. EndCallToPackage( pspPackage );
  131. if (FAILED(scRet))
  132. {
  133. DebugLog((DEB_WARN, "Failed to acquire cred handle for %ws with %ws\n",
  134. pPrincipal->Buffer, pSecPackage->Buffer));
  135. return(scRet);
  136. }
  137. phCredential->dwLower = pspPackage->dwPackageID;
  138. if(!AddCredHandle(pSession, phCredential, 0))
  139. {
  140. DebugLog(( DEB_ERROR, "Failed adding credential handle %p:%p to session %p\n",
  141. phCredential->dwUpper, phCredential->dwLower,
  142. pSession ));
  143. pspPackage = SpmpLookupPackageAndRequest(pSecPackage,
  144. SP_ORDINAL_FREECREDHANDLE);
  145. if( pspPackage )
  146. {
  147. ULONG OldCallCount = CallInfo->CallInfo.CallCount;
  148. CallInfo->CallInfo.CallCount = 1 ;
  149. //
  150. // remove the handle from the underlying package.
  151. //
  152. StartCallToPackage( pspPackage );
  153. __try
  154. {
  155. pspPackage->FunctionTable.FreeCredentialsHandle(
  156. phCredential->dwUpper
  157. );
  158. }
  159. __except (SP_EXCEPTION)
  160. {
  161. NOTHING;
  162. }
  163. EndCallToPackage( pspPackage );
  164. CallInfo->CallInfo.CallCount = OldCallCount;
  165. }
  166. phCredential->dwLower = 0;
  167. phCredential->dwUpper = 0;
  168. return SEC_E_INSUFFICIENT_MEMORY;
  169. }
  170. LsapLogCallInfo( CallInfo, pSession, *phCredential );
  171. return(scRet);
  172. }
  173. NTSTATUS
  174. WLsaAddCredentials(
  175. PCredHandle phCredential,
  176. PSECURITY_STRING pPrincipal,
  177. PSECURITY_STRING pSecPackage,
  178. DWORD fCredentialUse,
  179. PVOID pvAuthData,
  180. PVOID pvGetKeyFn,
  181. PVOID pvGetKeyArgument,
  182. PTimeStamp ptsExpiry)
  183. {
  184. PLSAP_SECURITY_PACKAGE pspPackage;
  185. NTSTATUS scRet;
  186. LUID CallerLogonID;
  187. PSession pSession = GetCurrentSession();
  188. SECPKG_CLIENT_INFO ClientInfo;
  189. PLSA_CALL_INFO CallInfo = LsapGetCurrentCall();
  190. PVOID CredKey ;
  191. //
  192. // Check if the caller is restricted
  193. //
  194. scRet = LsapGetClientInfo(&ClientInfo);
  195. if (!NT_SUCCESS(scRet))
  196. {
  197. DebugLog((DEB_ERROR,"Failed to get client info: 0x%x\n",scRet));
  198. return(scRet);
  199. }
  200. //
  201. // If the caller is restricted, fail the call for now. This should change
  202. // if packages are able to support restrictions. In that case, the call
  203. // should check the package capabilities for handling restrictions and
  204. // if it supports restrictions, allow the call to continue.
  205. //
  206. if (ClientInfo.Restricted)
  207. {
  208. DebugLog((DEB_WARN,"Trying to acquire credentials with a restrictred token\n"));
  209. scRet = SEC_E_NO_CREDENTIALS;
  210. return(scRet);
  211. }
  212. #if DBG
  213. if (pPrincipal->Length)
  214. {
  215. DebugLog((DEB_TRACE_WAPI, "[%x] AddCredentials(%ws, %ws)\n",
  216. pSession->dwProcessID, pPrincipal->Buffer, pSecPackage->Buffer));
  217. }
  218. else
  219. {
  220. DebugLog((DEB_TRACE_WAPI, "[%x] AddCredentials(%ws)\n",
  221. pSession->dwProcessID,
  222. pSecPackage->Buffer));
  223. }
  224. #endif // DBG
  225. ptsExpiry->LowPart = 0;
  226. ptsExpiry->HighPart = 0;
  227. LsapLogCallInfo( CallInfo, pSession, *phCredential );
  228. scRet = ValidateCredHandle(
  229. pSession,
  230. phCredential,
  231. &CredKey );
  232. if ( NT_SUCCESS( scRet ) )
  233. {
  234. pspPackage = SpmpValidRequest( phCredential->dwLower,
  235. SP_ORDINAL_ADDCREDENTIALS );
  236. }
  237. else
  238. {
  239. DsysAssert( (pSession->fSession & SESFLAG_KERNEL) == 0 );
  240. return( SEC_E_INVALID_HANDLE );
  241. }
  242. if (!pspPackage)
  243. {
  244. return(SEC_E_SECPKG_NOT_FOUND);
  245. }
  246. SetCurrentPackageId(pspPackage->dwPackageID);
  247. StartCallToPackage( pspPackage );
  248. __try
  249. {
  250. scRet = pspPackage->FunctionTable.AddCredentials(
  251. phCredential->dwUpper,
  252. pPrincipal,
  253. pSecPackage,
  254. fCredentialUse,
  255. pvAuthData,
  256. pvGetKeyFn,
  257. pvGetKeyArgument,
  258. ptsExpiry);
  259. }
  260. __except (SP_EXCEPTION)
  261. {
  262. scRet = GetExceptionCode();
  263. scRet = SPException(scRet, pspPackage->dwPackageID);
  264. }
  265. EndCallToPackage( pspPackage );
  266. if (FAILED(scRet))
  267. {
  268. DebugLog((DEB_WARN, "Failed to add credentials for %ws with %ws\n",
  269. pPrincipal->Buffer, pSecPackage->Buffer));
  270. return(scRet);
  271. }
  272. LsapLogCallInfo( CallInfo, pSession, *phCredential );
  273. return(scRet);
  274. }
  275. //+-------------------------------------------------------------------------
  276. //
  277. // Function: WLsaFreeCredHandle
  278. //
  279. // Synopsis: Worker function to free a cred handle,
  280. //
  281. // Effects: calls into a package to free the handle
  282. //
  283. // Arguments:
  284. //
  285. // Requires:
  286. //
  287. // Returns:
  288. //
  289. // Notes:
  290. //
  291. //--------------------------------------------------------------------------
  292. NTSTATUS
  293. WLsaFreeCredHandle( PCredHandle phCreds)
  294. {
  295. NTSTATUS scRet;
  296. PSession pSession = GetCurrentSession();
  297. PLSA_CALL_INFO CallInfo = LsapGetCurrentCall();
  298. PLSAP_SECURITY_PACKAGE pPackage;
  299. IsOkayToExec(0);
  300. DebugLog((DEB_TRACE_WAPI, "[%x] WLsaFreeCredHandle(%p : %p)\n",
  301. pSession->dwProcessID, phCreds->dwUpper, phCreds->dwLower));
  302. scRet = ValidateAndDerefCredHandle( pSession, phCreds );
  303. if ( !NT_SUCCESS( scRet ) )
  304. {
  305. if ( ( CallInfo->Flags & CALL_FLAG_NO_HANDLE_CHK ) == 0 )
  306. {
  307. DsysAssert( (pSession->fSession & SESFLAG_KERNEL) == 0 );
  308. }
  309. }
  310. LsapLogCallInfo( CallInfo, pSession, *phCreds );
  311. if (SUCCEEDED(scRet))
  312. {
  313. phCreds->dwUpper = phCreds->dwLower = 0xFFFFFFFF;
  314. }
  315. return(scRet);
  316. }
  317. //+-------------------------------------------------------------------------
  318. //
  319. // Function: WLsaQueryCredAttributes
  320. //
  321. // Synopsis: SPMgr worker to query credential attributes
  322. //
  323. // Effects:
  324. //
  325. // Arguments:
  326. //
  327. // Requires:
  328. //
  329. // Returns:
  330. //
  331. // Notes:
  332. //
  333. //
  334. //--------------------------------------------------------------------------
  335. NTSTATUS
  336. WLsaQueryCredAttributes(
  337. PCredHandle phCredentials,
  338. ULONG ulAttribute,
  339. PVOID pBuffer
  340. )
  341. {
  342. NTSTATUS scRet;
  343. PSession pSession = GetCurrentSession();
  344. PLSA_CALL_INFO CallInfo = LsapGetCurrentCall();
  345. PLSAP_SECURITY_PACKAGE pPackage;
  346. PVOID CredKey = NULL ;
  347. DebugLog((DEB_TRACE_WAPI, "[%x] WLsaQueryCredAttributes(%p : %p)\n",
  348. pSession->dwProcessID, phCredentials->dwUpper, phCredentials->dwLower));
  349. LsapLogCallInfo( CallInfo, pSession, *phCredentials );
  350. scRet = ValidateCredHandle(
  351. pSession,
  352. phCredentials,
  353. &CredKey );
  354. if ( !NT_SUCCESS( scRet ) )
  355. {
  356. DsysAssert( (pSession->fSession & SESFLAG_KERNEL) == 0 );
  357. return( scRet );
  358. }
  359. pPackage = SpmpValidRequest(phCredentials->dwLower,
  360. SP_ORDINAL_QUERYCREDATTR );
  361. if (pPackage)
  362. {
  363. SetCurrentPackageId(phCredentials->dwLower);
  364. StartCallToPackage( pPackage );
  365. __try
  366. {
  367. scRet = pPackage->FunctionTable.QueryCredentialsAttributes(
  368. phCredentials->dwUpper,
  369. ulAttribute,
  370. pBuffer
  371. );
  372. }
  373. __except (SP_EXCEPTION)
  374. {
  375. scRet = GetExceptionCode();
  376. scRet = SPException(scRet, phCredentials->dwLower);
  377. }
  378. EndCallToPackage( pPackage );
  379. }
  380. else
  381. {
  382. scRet = SEC_E_INVALID_HANDLE;
  383. }
  384. DerefCredHandle( pSession, NULL, CredKey );
  385. return(scRet);
  386. }