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.

252 lines
5.9 KiB

  1. /*+
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1997 - 1998.
  5. *
  6. * Name : seclogon.cxx
  7. * Author:Jeffrey Richter (v-jeffrr)
  8. *
  9. * Abstract:
  10. * This is the service DLL for Secondary Logon Service
  11. * This service supports the CreateProcessWithLogon API implemented
  12. * in advapi32.dll
  13. *
  14. * Revision History:
  15. * PraeritG 10/8/97 To integrate this in to services.exe
  16. *
  17. -*/
  18. #define STRICT
  19. #include <Windows.h>
  20. #include <userenv.h>
  21. #include <lm.h>
  22. #include <dsgetdc.h>
  23. #include <sddl.h>
  24. PTOKEN_USER
  25. SlpGetTokenUser(
  26. HANDLE TokenHandle,
  27. PLUID AuthenticationId OPTIONAL
  28. )
  29. /*++
  30. Routine Description:
  31. This routine returns the TOKEN_USER structure for the
  32. current user, and optionally, the AuthenticationId from his
  33. token.
  34. Arguments:
  35. AuthenticationId - Supplies an optional pointer to return the
  36. AuthenticationId.
  37. Return Value:
  38. On success, returns a pointer to a TOKEN_USER structure.
  39. On failure, returns NULL. Call GetLastError() for more
  40. detailed error information.
  41. --*/
  42. {
  43. ULONG ReturnLength;
  44. TOKEN_STATISTICS TokenStats;
  45. PTOKEN_USER pTokenUser = NULL;
  46. BOOLEAN b = FALSE;
  47. if(!GetTokenInformation (
  48. TokenHandle,
  49. TokenUser,
  50. NULL,
  51. 0,
  52. &ReturnLength
  53. ))
  54. {
  55. pTokenUser = (PTOKEN_USER)HeapAlloc( GetProcessHeap(), 0,
  56. ReturnLength );
  57. if (pTokenUser) {
  58. if ( GetTokenInformation (
  59. TokenHandle,
  60. TokenUser,
  61. pTokenUser,
  62. ReturnLength,
  63. &ReturnLength
  64. ))
  65. {
  66. if (AuthenticationId) {
  67. if(GetTokenInformation (
  68. TokenHandle,
  69. TokenStatistics,
  70. (PVOID)&TokenStats,
  71. sizeof( TOKEN_STATISTICS ),
  72. &ReturnLength
  73. ))
  74. {
  75. *AuthenticationId = TokenStats.AuthenticationId;
  76. b = TRUE;
  77. }
  78. } else {
  79. //
  80. // We're done, mark that everything worked
  81. //
  82. b = TRUE;
  83. }
  84. }
  85. if (!b) {
  86. //
  87. // Something failed, clean up what we were going to return
  88. //
  89. HeapFree( GetProcessHeap(), 0, pTokenUser );
  90. pTokenUser = NULL;
  91. }
  92. }
  93. }
  94. return( pTokenUser );
  95. }
  96. DWORD
  97. SlpGetUserName(
  98. IN HANDLE TokenHandle,
  99. OUT LPTSTR UserName,
  100. IN OUT PDWORD UserNameLen,
  101. OUT LPTSTR DomainName,
  102. IN OUT PDWORD DomNameLen
  103. )
  104. /*++
  105. Routine Description:
  106. This routine is the LSA Server worker routine for the LsaGetUserName
  107. API.
  108. WARNING: This routine allocates memory for its output. The caller is
  109. responsible for freeing this memory after use. See description of the
  110. Names parameter.
  111. Arguments:
  112. UserName - Receives name of the current user.
  113. DomainName - Optionally receives domain name of the current user.
  114. Return Values:
  115. NTSTATUS - Standard Nt Result Code
  116. STATUS_SUCCESS - The call completed successfully and all Sids have
  117. been translated to names.
  118. STATUS_INSUFFICIENT_RESOURCES - Insufficient system resources
  119. such as memory to complete the call.
  120. --*/
  121. {
  122. LUID LogonId;
  123. PTOKEN_USER TokenUserInformation = NULL;
  124. SID_NAME_USE Use;
  125. //
  126. // Let's see if we're trying to look up the currently logged on
  127. // user.
  128. //
  129. //
  130. // TokenUserInformation from this call must be freed by calling
  131. // HeapFree().
  132. //
  133. TokenUserInformation = SlpGetTokenUser( TokenHandle, &LogonId );
  134. if ( TokenUserInformation ) {
  135. //
  136. // Simply do LookupAccountSid...
  137. //
  138. if(LookupAccountSid(NULL, TokenUserInformation->User.Sid,
  139. UserName, UserNameLen, DomainName, DomNameLen,
  140. &Use))
  141. {
  142. HeapFree( GetProcessHeap(), 0, TokenUserInformation );
  143. return ERROR_SUCCESS;
  144. }
  145. HeapFree( GetProcessHeap(), 0, TokenUserInformation );
  146. return GetLastError();
  147. }
  148. return GetLastError();
  149. }
  150. BOOL
  151. SlpIsDomainUser(
  152. HANDLE Token,
  153. PBOOLEAN IsDomain
  154. )
  155. /*++
  156. Routine Description:
  157. Determines if the current user is logged on to a domain account
  158. or a local machine account.
  159. Arguments:
  160. IsDomain - Returns TRUE if the current user is logged on to a domain
  161. account, FALSE otherwise.
  162. Return Value:
  163. TRUE on success, FALSE on failure.
  164. --*/
  165. {
  166. TCHAR UserName[MAX_PATH];
  167. DWORD UserNameLen = MAX_PATH;
  168. TCHAR Domain[MAX_PATH];
  169. DWORD DomNameLen = MAX_PATH;
  170. DWORD Status;
  171. WCHAR pwszMachineName[(MAX_COMPUTERNAME_LENGTH + 1) * sizeof( WCHAR )];
  172. DWORD nSize = MAX_COMPUTERNAME_LENGTH + 1;
  173. BOOL b = FALSE;
  174. *IsDomain = FALSE;
  175. Status = SlpGetUserName( Token, UserName, &UserNameLen,
  176. Domain, &DomNameLen );
  177. if (Status == ERROR_SUCCESS) {
  178. if (GetComputerName ( pwszMachineName, &nSize )) {
  179. *IsDomain = (lstrcmp( pwszMachineName, Domain ) != 0) ? 1 : 0;
  180. b = TRUE;
  181. }
  182. }
  183. return( b );
  184. }