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.

300 lines
9.6 KiB

  1. // --------------------------------------------------------------------------
  2. // Module Name: BioLogon.cpp
  3. //
  4. // Copyright (c) 2001, Microsoft Corporation
  5. //
  6. // File that implements a publicly declared import that forwards to an
  7. // implementation in shgina.dll
  8. //
  9. // History: 2001-04-10 vtan created
  10. // --------------------------------------------------------------------------
  11. #include <nt.h>
  12. #include <ntrtl.h>
  13. #include <nturtl.h>
  14. #include <ntlsa.h>
  15. #include <ntmsv1_0.h>
  16. #include <windows.h>
  17. HANDLE g_hLSA = NULL;
  18. // --------------------------------------------------------------------------
  19. // CheckTCBPrivilege
  20. //
  21. // Arguments: <none>
  22. //
  23. // Returns: BOOL
  24. //
  25. // Purpose: Returns whether the thread impersonation token or the process
  26. // level token has SE_TCB_PRIVILEGE.
  27. //
  28. // History: 2001-06-04 vtan created
  29. // --------------------------------------------------------------------------
  30. BOOL CheckTCBPrivilege (void)
  31. {
  32. BOOL fResult;
  33. HANDLE hToken;
  34. fResult = FALSE;
  35. if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, FALSE, &hToken) == FALSE)
  36. {
  37. if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) == FALSE)
  38. {
  39. hToken = NULL;
  40. }
  41. }
  42. if (hToken != NULL)
  43. {
  44. DWORD dwReturnLength;
  45. dwReturnLength = 0;
  46. (BOOL)GetTokenInformation(hToken,
  47. TokenPrivileges,
  48. NULL,
  49. 0,
  50. &dwReturnLength);
  51. if (dwReturnLength != 0)
  52. {
  53. TOKEN_PRIVILEGES *pTokenPrivileges;
  54. pTokenPrivileges = static_cast<TOKEN_PRIVILEGES*>(LocalAlloc(LMEM_FIXED, dwReturnLength));
  55. if (pTokenPrivileges != NULL)
  56. {
  57. if (GetTokenInformation(hToken,
  58. TokenPrivileges,
  59. pTokenPrivileges,
  60. dwReturnLength,
  61. &dwReturnLength) != FALSE)
  62. {
  63. bool fFound;
  64. DWORD dwIndex;
  65. LUID luidPrivilege;
  66. luidPrivilege.LowPart = SE_TCB_PRIVILEGE;
  67. luidPrivilege.HighPart = 0;
  68. for (fFound = false, dwIndex = 0; !fFound && (dwIndex < pTokenPrivileges->PrivilegeCount); ++dwIndex)
  69. {
  70. fFound = RtlEqualLuid(&pTokenPrivileges->Privileges[dwIndex].Luid, &luidPrivilege);
  71. }
  72. if (fFound)
  73. {
  74. fResult = TRUE;
  75. }
  76. else
  77. {
  78. SetLastError(ERROR_PRIVILEGE_NOT_HELD);
  79. }
  80. }
  81. (HLOCAL)LocalFree(pTokenPrivileges);
  82. }
  83. }
  84. (BOOL)CloseHandle(hToken);
  85. }
  86. return(fResult);
  87. }
  88. // --------------------------------------------------------------------------
  89. // ::EnableBlankPasswords
  90. //
  91. // Arguments: <none>
  92. //
  93. // Returns: BOOL
  94. //
  95. // Purpose: Uses the MSV1_0 package via LSA to enable blank passwords for
  96. // this process.
  97. //
  98. // History: 2001-06-04 vtan created
  99. // --------------------------------------------------------------------------
  100. BOOL EnableBlankPasswords (void)
  101. {
  102. NTSTATUS status;
  103. if (g_hLSA == NULL)
  104. {
  105. LSA_OPERATIONAL_MODE LSAOperationalMode;
  106. STRING strLogonProcess;
  107. RtlInitString(&strLogonProcess, "BioLogon");
  108. status = LsaRegisterLogonProcess(&strLogonProcess, &g_hLSA, &LSAOperationalMode);
  109. if (NT_SUCCESS(status))
  110. {
  111. ULONG ulPackageID;
  112. STRING strMSVPackage;
  113. RtlInitString(&strMSVPackage, MSV1_0_PACKAGE_NAME);
  114. status = LsaLookupAuthenticationPackage(g_hLSA,
  115. &strMSVPackage,
  116. &ulPackageID);
  117. if (NT_SUCCESS(status))
  118. {
  119. NTSTATUS statusProtocol;
  120. ULONG ulResponseSize;
  121. MSV1_0_SETPROCESSOPTION_REQUEST request;
  122. void* pResponse;
  123. ZeroMemory(&request, sizeof(request));
  124. request.MessageType = MsV1_0SetProcessOption;
  125. request.ProcessOptions = MSV1_0_OPTION_ALLOW_BLANK_PASSWORD;
  126. request.DisableOptions = FALSE;
  127. status = LsaCallAuthenticationPackage(g_hLSA,
  128. ulPackageID,
  129. &request,
  130. sizeof(request),
  131. &pResponse,
  132. &ulResponseSize,
  133. &statusProtocol);
  134. if (NT_SUCCESS(status))
  135. {
  136. status = statusProtocol;
  137. }
  138. }
  139. }
  140. if (!NT_SUCCESS(status))
  141. {
  142. SetLastError(RtlNtStatusToDosError(status));
  143. }
  144. }
  145. else
  146. {
  147. SetLastError(ERROR_ALREADY_INITIALIZED);
  148. status = STATUS_UNSUCCESSFUL;
  149. }
  150. return(NT_SUCCESS(status));
  151. }
  152. // --------------------------------------------------------------------------
  153. // ::InitializeBioLogon
  154. //
  155. // Arguments: <none>
  156. //
  157. // Returns: BOOL
  158. //
  159. // Purpose: Initialize the biologon DLL. This call is required if you
  160. // want to be able to use blank passwords. This will check that
  161. // the caller has SE_TCB_PRIVILEGE.
  162. //
  163. // History: 2001-06-04 vtan created
  164. // --------------------------------------------------------------------------
  165. EXTERN_C BOOL WINAPI InitializeBioLogon (void)
  166. {
  167. return(CheckTCBPrivilege() && EnableBlankPasswords());
  168. }
  169. // --------------------------------------------------------------------------
  170. // ::InitiateInteractiveLogonWithTimeout
  171. //
  172. // Arguments: pszUsername = User name.
  173. // pszPassword = Password.
  174. // dwTimeout = Time out in milliseconds.
  175. //
  176. // Returns: BOOL
  177. //
  178. // Purpose: External entry point function exported by name to initiate
  179. // an interactive logon with specified timeout.
  180. //
  181. // History: 2001-06-04 vtan created
  182. // --------------------------------------------------------------------------
  183. EXTERN_C BOOL WINAPI InitiateInteractiveLogonWithTimeout (const WCHAR *pszUsername, WCHAR *pszPassword, DWORD dwTimeout)
  184. {
  185. typedef BOOL (WINAPI * PFNIIL) (const WCHAR *pszUsername, WCHAR *pszPassword, DWORD dwTimeout);
  186. BOOL fResult;
  187. static HMODULE s_hModule = reinterpret_cast<HMODULE>(-1);
  188. static PFNIIL s_pfnIIL = NULL;
  189. if (s_hModule == reinterpret_cast<HMODULE>(-1))
  190. {
  191. s_hModule = LoadLibrary(TEXT("shgina.dll"));
  192. if (s_hModule != NULL)
  193. {
  194. s_pfnIIL = reinterpret_cast<PFNIIL>(GetProcAddress(s_hModule, MAKEINTRESOURCEA(6)));
  195. if (s_pfnIIL != NULL)
  196. {
  197. fResult = s_pfnIIL(pszUsername, pszPassword, dwTimeout);
  198. }
  199. else
  200. {
  201. fResult = FALSE;
  202. }
  203. }
  204. else
  205. {
  206. fResult = FALSE;
  207. }
  208. }
  209. else if (s_pfnIIL != NULL)
  210. {
  211. fResult = s_pfnIIL(pszUsername, pszPassword, dwTimeout);
  212. }
  213. else
  214. {
  215. fResult = FALSE;
  216. SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  217. }
  218. return(fResult);
  219. }
  220. // --------------------------------------------------------------------------
  221. // ::InitiateInteractiveLogon
  222. //
  223. // Arguments: pszUsername = User name.
  224. // pszPassword = Password.
  225. //
  226. // Returns: BOOL
  227. //
  228. // Purpose: External entry point function exported by name to initiate
  229. // an interactive logon. This passes an INFINITE timeout. Use
  230. // this function with care.
  231. //
  232. // History: 2001-06-04 vtan created
  233. // --------------------------------------------------------------------------
  234. EXTERN_C BOOL WINAPI InitiateInteractiveLogon (const WCHAR *pszUsername, WCHAR *pszPassword)
  235. {
  236. return(InitiateInteractiveLogonWithTimeout(pszUsername, pszPassword, INFINITE));
  237. }
  238. // --------------------------------------------------------------------------
  239. // ::DllMain
  240. //
  241. // Arguments: See the platform SDK under DllMain.
  242. //
  243. // Returns: BOOL
  244. //
  245. // Purpose: DllMain for the DLL. Recognizes only DLL_PROCESS_DETACH to do
  246. // some clean up.
  247. //
  248. // History: 2001-06-05 vtan created
  249. // --------------------------------------------------------------------------
  250. EXTERN_C BOOL WINAPI DllMain (HINSTANCE hInstance, DWORD dwReason, void *pvReserved)
  251. {
  252. UNREFERENCED_PARAMETER(hInstance);
  253. UNREFERENCED_PARAMETER(pvReserved);
  254. switch (dwReason)
  255. {
  256. case DLL_PROCESS_DETACH:
  257. if (g_hLSA != NULL)
  258. {
  259. (BOOL)CloseHandle(g_hLSA);
  260. g_hLSA = NULL;
  261. }
  262. break;
  263. default:
  264. break;
  265. }
  266. return(TRUE);
  267. }