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.

419 lines
9.1 KiB

  1. //+-----------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (c) Microsoft Corporation 1992 - 1993
  6. //
  7. // File: suppcred.cxx
  8. //
  9. // Contents: Code to retrieve/store supplemental credentials
  10. //
  11. //
  12. // History: 9/23/93 Created MikeSw
  13. //
  14. //------------------------------------------------------------------------
  15. #include <lsapch.hxx>
  16. extern "C"
  17. {
  18. #include "sesmgr.h" // PSession
  19. #include "suppcred.h" // supp. cred. apis
  20. }
  21. typedef struct _DomainSuppCreds {
  22. UNICODE_STRING ssUserName;
  23. UNICODE_STRING ssDomainName;
  24. HANDLE hClientToken;
  25. SECPKG_SUPPLEMENTAL_CRED SupplementalCredential;
  26. } DomainSuppCreds, *PDomainSuppCreds;
  27. //+-------------------------------------------------------------------------
  28. //
  29. // Function: LsapSaveSupplementalCredentials
  30. //
  31. // Synopsis: Saves supplemental credentials
  32. //
  33. // Effects:
  34. //
  35. // Arguments:
  36. //
  37. // Requires:
  38. //
  39. // Returns:
  40. //
  41. // Notes:
  42. //
  43. //
  44. //--------------------------------------------------------------------------
  45. NTSTATUS SEC_ENTRY
  46. LsapSaveSupplementalCredentials(
  47. IN PLUID LogonId,
  48. IN ULONG SupplementalCredSize,
  49. IN PVOID SupplementalCreds,
  50. IN BOOLEAN Synchronous
  51. )
  52. {
  53. //
  54. // obsolete by credmgr
  55. //
  56. return(STATUS_SUCCESS);
  57. }
  58. //+-------------------------------------------------------------------------
  59. //
  60. // Function: WLsaSaveSupplementalCredentials
  61. //
  62. // Synopsis: worker function to call package to set supp. creds
  63. //
  64. // Effects:
  65. //
  66. // Arguments:
  67. //
  68. // Requires:
  69. //
  70. // Returns:
  71. //
  72. // Notes:
  73. //
  74. //
  75. //--------------------------------------------------------------------------
  76. extern "C"
  77. NTSTATUS
  78. WLsaSaveSupplementalCredentials(
  79. IN PCredHandle pCredHandle,
  80. IN PSecBuffer pCredentials
  81. )
  82. {
  83. NTSTATUS scRet;
  84. PLSAP_SECURITY_PACKAGE pspPackage;
  85. PSession pSession = GetCurrentSession();
  86. //
  87. // Make sure we can exec.
  88. //
  89. IsOkayToExec(0);
  90. pspPackage = SpmpValidRequest( pCredHandle->dwLower, SP_ORDINAL_SAVECRED);
  91. if (!pspPackage)
  92. {
  93. return( STATUS_INVALID_HANDLE );
  94. }
  95. SetCurrentPackageId(pCredHandle->dwLower);
  96. DebugLog((DEB_TRACE,"WLsaSaveSupplementalCredentials %x,%x\n",
  97. pCredHandle->dwUpper,pCredHandle->dwLower));
  98. DebugLog((DEB_TRACE_VERB, "\tPackage = %ws\n", pspPackage->Name.Buffer));
  99. __try
  100. {
  101. scRet = pspPackage->FunctionTable.SaveCredentials(
  102. pCredHandle->dwUpper,
  103. pCredentials);
  104. }
  105. __except (SP_EXCEPTION)
  106. {
  107. scRet = GetExceptionCode();
  108. scRet = SPException(scRet, pspPackage->dwPackageID);
  109. }
  110. DebugLog((DEB_TRACE_VERB,"WLsaSaveSupplementalCredentials returning %x\n",scRet));
  111. return(scRet);
  112. }
  113. //+-------------------------------------------------------------------------
  114. //
  115. // Function: WLsaGetSupplementalCredentials
  116. //
  117. // Synopsis: worker function to call package to get supp. credentials
  118. //
  119. // Effects:
  120. //
  121. // Arguments:
  122. //
  123. // Requires:
  124. //
  125. // Returns:
  126. //
  127. // Notes: allocates virtual memory in client process
  128. //
  129. //
  130. //--------------------------------------------------------------------------
  131. extern "C"
  132. NTSTATUS
  133. WLsaGetSupplementalCredentials(
  134. PCredHandle pCredHandle,
  135. PSecBuffer pCreds)
  136. {
  137. NTSTATUS scRet;
  138. PLSAP_SECURITY_PACKAGE pspPackage;
  139. PSession pSession = GetCurrentSession();
  140. //
  141. // Make sure we can exec.
  142. //
  143. IsOkayToExec(0);
  144. pspPackage = SpmpValidRequest( pCredHandle->dwLower, SP_ORDINAL_GETCRED);
  145. if (!pspPackage)
  146. {
  147. return( STATUS_INVALID_HANDLE );
  148. }
  149. SetCurrentPackageId(pCredHandle->dwLower);
  150. DebugLog((DEB_TRACE,"WLsaGetSupplementalCredentials %x,%x\n",
  151. pCredHandle->dwUpper,pCredHandle->dwLower));
  152. DebugLog((DEB_TRACE_VERB, "\tPackage = %ws\n", pspPackage->Name.Buffer));
  153. __try
  154. {
  155. scRet = pspPackage->FunctionTable.GetCredentials(
  156. pCredHandle->dwUpper,
  157. pCreds);
  158. }
  159. __except (SP_EXCEPTION)
  160. {
  161. scRet = GetExceptionCode();
  162. scRet = SPException(scRet, pspPackage->dwPackageID);
  163. }
  164. DebugLog((DEB_TRACE_VERB,"WLsaGetSupplementalCredentials returning %x\n",scRet));
  165. return(scRet);
  166. }
  167. //+-------------------------------------------------------------------------
  168. //
  169. // Function: WLsaDeleteSupplementalCredentials
  170. //
  171. // Synopsis: worker function to call package to delete credentials
  172. //
  173. // Effects:
  174. //
  175. // Arguments:
  176. //
  177. // Requires:
  178. //
  179. // Returns:
  180. //
  181. // Notes:
  182. //
  183. //
  184. //--------------------------------------------------------------------------
  185. extern "C"
  186. NTSTATUS
  187. WLsaDeleteSupplementalCredentials(
  188. PCredHandle pCredHandle,
  189. PSecBuffer pKey)
  190. {
  191. NTSTATUS scRet;
  192. PLSAP_SECURITY_PACKAGE pspPackage;
  193. PSession pSession = GetCurrentSession();
  194. //
  195. // Make sure we can exec.
  196. //
  197. IsOkayToExec(0);
  198. pspPackage = SpmpValidRequest( pCredHandle->dwLower, SP_ORDINAL_DELETECRED);
  199. if (!pspPackage)
  200. {
  201. return( STATUS_INVALID_HANDLE );
  202. }
  203. SetCurrentPackageId(pCredHandle->dwLower);
  204. DebugLog((DEB_TRACE,"WLsaDeleteSupplementalCredentials %x,%x\n",
  205. pCredHandle->dwUpper,pCredHandle->dwLower));
  206. DebugLog((DEB_TRACE_VERB, "\tPackage = %ws\n", pspPackage->Name.Buffer));
  207. __try
  208. {
  209. scRet = pspPackage->FunctionTable.DeleteCredentials(
  210. pCredHandle->dwUpper,
  211. pKey);
  212. }
  213. __except (SP_EXCEPTION)
  214. {
  215. scRet = GetExceptionCode();
  216. scRet = SPException(scRet, pspPackage->dwPackageID);
  217. }
  218. DebugLog((DEB_TRACE_VERB,"WLsaDeleteSupplementalCredentials returning %x\n",scRet));
  219. return(scRet);
  220. }
  221. //+-------------------------------------------------------------------------
  222. //
  223. // Function: FreeSupplementalCredentials
  224. //
  225. // Synopsis: frees supplemental credentials
  226. //
  227. // Effects:
  228. //
  229. // Arguments:
  230. //
  231. // Requires:
  232. //
  233. // Returns:
  234. //
  235. // Notes:
  236. //
  237. //
  238. //--------------------------------------------------------------------------
  239. void
  240. LsapFreeSupplementalCredentials(
  241. IN ULONG CredentialCount,
  242. IN PSECPKG_SUPPLEMENTAL_CRED pCredArray
  243. )
  244. {
  245. ULONG cIndex;
  246. if ((pCredArray == NULL) || (CredentialCount == 0))
  247. {
  248. return;
  249. }
  250. for (cIndex = 0; cIndex < CredentialCount ; cIndex++)
  251. {
  252. if (pCredArray[cIndex].PackageName.Buffer != NULL)
  253. {
  254. LsapFreeLsaHeap(pCredArray[cIndex].PackageName.Buffer);
  255. }
  256. if (pCredArray[cIndex].Credentials != NULL)
  257. {
  258. LsapFreeLsaHeap(pCredArray[cIndex].Credentials);
  259. }
  260. }
  261. LsapFreeLsaHeap(pCredArray);
  262. }
  263. //+-------------------------------------------------------------------------
  264. //
  265. // Function: ReformatSupplementalCredentials
  266. //
  267. // Synopsis: Takes a an array of SupplementalCred structures and
  268. // converts it to the CREDENTIAL** used by WLsaLogonUser.
  269. //
  270. //
  271. // Effects:
  272. //
  273. // Arguments:
  274. //
  275. // Requires:
  276. //
  277. // Returns:
  278. //
  279. // Notes:
  280. //
  281. //
  282. //--------------------------------------------------------------------------
  283. NTSTATUS
  284. LsapReformatSupplementalCredentials(
  285. IN ULONG cSupplementalCreds,
  286. IN PSECPKG_SUPPLEMENTAL_CRED pSupplementalCreds,
  287. OUT PULONG CredentialCount,
  288. OUT PSECPKG_SUPPLEMENTAL_CRED * Credentials
  289. )
  290. {
  291. NTSTATUS scRet;
  292. ULONG cIndex;
  293. ULONG cCredIndex;
  294. PLSAP_SECURITY_PACKAGE pPackage;
  295. PSECPKG_SUPPLEMENTAL_CRED TempSuppCreds = NULL;
  296. TempSuppCreds = (PSECPKG_SUPPLEMENTAL_CRED) LsapAllocateLsaHeap(
  297. sizeof(SECPKG_SUPPLEMENTAL_CRED) * lsState.cPackages);
  298. if (TempSuppCreds == NULL)
  299. {
  300. scRet = STATUS_INSUFFICIENT_RESOURCES;
  301. goto Cleanup;
  302. }
  303. RtlZeroMemory(
  304. TempSuppCreds,
  305. sizeof(SECPKG_SUPPLEMENTAL_CRED) * lsState.cPackages
  306. );
  307. //
  308. // Scan through the packages looking for matching credentials
  309. //
  310. pPackage = SpmpIteratePackages( NULL );
  311. while (pPackage)
  312. {
  313. cIndex = pPackage->dwPackageID;
  314. //
  315. // Scan through the credentials looking for the one matching
  316. // the package name
  317. //
  318. for (cCredIndex = 0; cCredIndex < cSupplementalCreds ; cCredIndex++ )
  319. {
  320. if ( RtlCompareUnicodeString(
  321. &pPackage->Name,
  322. &pSupplementalCreds[cCredIndex].PackageName,
  323. TRUE // CaseInsensitive
  324. ) == 0 )
  325. {
  326. DebugLog((DEB_TRACE_CRED, "Read credentials for packages %wZ\n",
  327. &pPackage->Name));
  328. TempSuppCreds[cIndex] = pSupplementalCreds[cCredIndex];
  329. }
  330. }
  331. pPackage = SpmpIteratePackages( pPackage );
  332. }
  333. *Credentials = TempSuppCreds;
  334. *CredentialCount = lsState.cPackages;
  335. scRet = STATUS_SUCCESS;
  336. Cleanup:
  337. return(scRet);
  338. }