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.

346 lines
11 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. logon.cxx
  5. Abstract:
  6. logon
  7. Author:
  8. Larry Zhu (LZhu) December 1, 2001 Created
  9. Environment:
  10. User Mode
  11. Revision History:
  12. --*/
  13. #include "precomp.hxx"
  14. #pragma hdrstop
  15. #include "subauth.hxx"
  16. #include "logon.hxx"
  17. VOID
  18. Usage(
  19. IN PCSTR pszApp
  20. )
  21. {
  22. DebugPrintf(SSPI_ERROR, "\n\nUsage: %s [-p<package>] "
  23. "[-c<clientName>] [-C<clientRealm>] [-k<password>] [-n] "
  24. "[-t<logonType>] [-v<provider>] [-a<application>] "
  25. "[-i<processIdWhoseTokenIsUsedToImpersonate>] [-s<subAuthId>] "
  26. "[-l] [-2] [-f<flags>] [-g] [-o<processoptions>]\n"
  27. "Remarks: package default to NTLM, use -l to use LsaLogonUser, "
  28. "-2 to use NTLMv2, -n to use new subauthstyle -g use generic passthrough\n\n",
  29. pszApp);
  30. exit(-1);
  31. }
  32. VOID __cdecl
  33. main(
  34. IN INT argc,
  35. IN PSTR argv[]
  36. )
  37. {
  38. TNtStatus Status = STATUS_SUCCESS;
  39. UNICODE_STRING ClientName = {0};
  40. UNICODE_STRING ClientRealm = {0};
  41. UNICODE_STRING Password = {0};
  42. UNICODE_STRING Application = {0};
  43. UNICODE_STRING Workstation = {0};
  44. DWORD dwLogonProvider = LOGON32_PROVIDER_DEFAULT;
  45. SECURITY_LOGON_TYPE LogonType = Interactive;
  46. BOOLEAN bUseLsaLogonUser = FALSE;
  47. PCSTR pszPackageName = NTLMSP_NAME_A;
  48. HANDLE hToken = NULL;
  49. BOOLEAN bUseNtlmv2 = FALSE;
  50. BOOLEAN bUseGenericPassThrough = FALSE;
  51. ULONG SubAuthId = 0;
  52. BOOLEAN bUseNewSubAuthStyle = FALSE;
  53. HANDLE hLsa = NULL;
  54. ULONG PackageId = 0;
  55. ULONG Flags = 0;
  56. ULONG ProcessOptions = 0;
  57. ULONG ProcessIdTokenUsedByClient = 0;
  58. TImpersonation* pImpersonation = NULL;
  59. HANDLE hImpToken = NULL;
  60. for (INT i = 1; NT_SUCCESS(Status) && (i < argc); i++)
  61. {
  62. if ((*argv[i] == '-') || (*argv[i] == '/'))
  63. {
  64. switch (argv[i][1])
  65. {
  66. case 'c':
  67. Status DBGCHK = CreateUnicodeStringFromAsciiz(argv[i] + 2, &ClientName);
  68. break;
  69. case 'C':
  70. Status DBGCHK = CreateUnicodeStringFromAsciiz(argv[i] + 2, &ClientRealm);
  71. break;
  72. case 'a':
  73. Status DBGCHK = CreateUnicodeStringFromAsciiz(argv[i] + 2, &Application);
  74. break;
  75. case 'g':
  76. bUseGenericPassThrough = TRUE;
  77. break;
  78. case 'k':
  79. Status DBGCHK = CreateUnicodeStringFromAsciiz(argv[i] + 2, &Password);
  80. break;
  81. case 'i':
  82. ProcessIdTokenUsedByClient = strtol(argv[i] + 2, NULL, 0);
  83. break;
  84. case 'l':
  85. bUseLsaLogonUser = TRUE;
  86. break;
  87. case 't':
  88. LogonType = (SECURITY_LOGON_TYPE) strtol(argv[i] + 2, NULL, 0);
  89. break;
  90. case 'v':
  91. dwLogonProvider = (SECURITY_LOGON_TYPE) strtol(argv[i] + 2, NULL, 0);
  92. break;
  93. case 'f':
  94. Flags = strtol(argv[i] + 2, NULL, 0);
  95. break;
  96. case 'o':
  97. ProcessOptions = strtol(argv[i] + 2, NULL, 0);
  98. break;
  99. case 'p':
  100. pszPackageName = argv[i] + 2;
  101. break;
  102. case 's':
  103. SubAuthId = strtol(argv[i] + 2, NULL, 0);
  104. //
  105. // SubAuthId can not be zero
  106. //
  107. Status DBGCHK = SubAuthId ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
  108. break;
  109. case '2':
  110. bUseNtlmv2 = TRUE;
  111. break;
  112. case 'n':
  113. bUseNewSubAuthStyle = TRUE;
  114. break;
  115. case 'w':
  116. Status DBGCHK = CreateUnicodeStringFromAsciiz(argv[i] + 2, &Workstation);
  117. break;
  118. case 'h':
  119. case '?':
  120. default:
  121. Usage(argv[0]);
  122. break;
  123. }
  124. }
  125. else
  126. {
  127. Usage(argv[0]);
  128. }
  129. }
  130. if (NT_SUCCESS(Status) && ProcessIdTokenUsedByClient && (ProcessIdTokenUsedByClient != -1))
  131. {
  132. Status DBGCHK = GetProcessTokenByProcessId(ProcessIdTokenUsedByClient, &hImpToken);
  133. }
  134. if (NT_SUCCESS(Status) && hImpToken)
  135. {
  136. pImpersonation = new TImpersonation(hImpToken);
  137. Status DBGCHK = pImpersonation ? pImpersonation->Validate() : E_OUTOFMEMORY;
  138. if (NT_SUCCESS(Status))
  139. {
  140. DebugPrintf(SSPI_LOG, "************** check client token data %p ******\n", hImpToken);
  141. Status DBGCHK = CheckUserData();
  142. }
  143. }
  144. if (NT_SUCCESS(Status) && ProcessOptions && (0 == _stricmp(NTLMSP_NAME_A, pszPackageName)))
  145. {
  146. Status DBGCHK = GetLsaHandleAndPackageId(
  147. pszPackageName,
  148. &hLsa,
  149. &PackageId
  150. );
  151. if (NT_SUCCESS(Status))
  152. {
  153. Status DBGCHK = SetProcessOptions(hLsa, PackageId, ProcessOptions);
  154. }
  155. }
  156. if (NT_SUCCESS(Status) && (ClientName.Length || ClientRealm.Length || Password.Length))
  157. {
  158. if (!bUseLsaLogonUser)
  159. {
  160. Status DBGCHK = LogonUserWrapper(
  161. ClientName.Buffer,
  162. ClientRealm.Buffer,
  163. Password.Buffer,
  164. (DWORD) LogonType,
  165. dwLogonProvider,
  166. &hToken
  167. );
  168. }
  169. else
  170. {
  171. if (!hLsa)
  172. {
  173. Status DBGCHK = GetLsaHandleAndPackageId(
  174. pszPackageName,
  175. &hLsa,
  176. &PackageId
  177. );
  178. }
  179. if (NT_SUCCESS(Status))
  180. {
  181. if (0 == _stricmp(NTLMSP_NAME_A, pszPackageName))
  182. {
  183. if (SubAuthId)
  184. {
  185. if (bUseGenericPassThrough)
  186. {
  187. Status DBGCHK = MsvSubAuthLogon(
  188. hLsa,
  189. PackageId,
  190. SubAuthId,
  191. &ClientName,
  192. &ClientRealm,
  193. &Password,
  194. &Workstation
  195. );
  196. }
  197. else
  198. {
  199. Status DBGCHK = MsvSubAuthLsaLogon(
  200. hLsa,
  201. PackageId,
  202. LogonType,
  203. SubAuthId,
  204. bUseNewSubAuthStyle,
  205. &ClientName,
  206. &ClientRealm,
  207. &Password,
  208. &Workstation,
  209. &hToken
  210. );
  211. }
  212. }
  213. else
  214. {
  215. Status DBGCHK = MsvLsaLogonUser(
  216. hLsa,
  217. PackageId,
  218. LogonType,
  219. &ClientName,
  220. &ClientRealm,
  221. &Password,
  222. &Workstation,
  223. bUseNtlmv2 ? kNetworkLogonNtlmv2 : kNetworkLogonNtlmv1,
  224. &hToken
  225. );
  226. }
  227. }
  228. else if (0 == _stricmp(MICROSOFT_KERBEROS_NAME_A, pszPackageName))
  229. {
  230. Status DBGCHK = KrbLsaLogonUser(
  231. hLsa,
  232. PackageId,
  233. LogonType,
  234. &ClientName,
  235. &ClientRealm,
  236. &Password,
  237. Flags,
  238. &hToken
  239. );
  240. }
  241. else
  242. {
  243. DebugPrintf(SSPI_WARN, "Using Msv wrapper for %s\n", pszPackageName);
  244. Status DBGCHK = MsvLsaLogonUser(
  245. hLsa,
  246. PackageId,
  247. LogonType,
  248. &ClientName,
  249. &ClientRealm,
  250. &Password,
  251. &Workstation,
  252. bUseNtlmv2 ? kNetworkLogonNtlmv2 : kNetworkLogonNtlmv1,
  253. &hToken
  254. );
  255. }
  256. }
  257. }
  258. if (NT_SUCCESS(Status))
  259. {
  260. Status DBGCHK = CheckUserToken(hToken);
  261. }
  262. }
  263. if (NT_SUCCESS(Status) && Application.Length && Application.Buffer && (hToken || hImpToken))
  264. {
  265. Status DBGCHK = StartInteractiveClientProcessAsUser(hToken ? hToken : hImpToken, Application.Buffer);
  266. }
  267. if (NT_SUCCESS(Status))
  268. {
  269. DebugPrintf(SSPI_LOG, "Operation succeeded\n");
  270. }
  271. else
  272. {
  273. DebugPrintf(SSPI_ERROR, "Operation failed\n");
  274. }
  275. if (hLsa)
  276. {
  277. LsaDeregisterLogonProcess(hLsa);
  278. }
  279. if (hToken)
  280. {
  281. CloseHandle(hToken);
  282. }
  283. if (pImpersonation)
  284. {
  285. delete pImpersonation;
  286. }
  287. RtlFreeUnicodeString(&ClientName);
  288. RtlFreeUnicodeString(&ClientRealm);
  289. RtlFreeUnicodeString(&Password);
  290. RtlFreeUnicodeString(&Workstation);
  291. RtlFreeUnicodeString(&Application);
  292. }