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.

273 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. 1998 Seagate Software, Inc. All rights reserved
  4. Module Name:
  5. WsbAccnt.cpp
  6. Abstract:
  7. This is the implementation of account helper functions.
  8. Author:
  9. Rohde Wakefield [rohde] 10-Apr-1997
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. #include "lm.h"
  14. HRESULT
  15. WsbGetAccountDomainName(
  16. OLECHAR * szDomainName,
  17. DWORD cSize
  18. )
  19. /*++
  20. Routine Description:
  21. This routine is called to find out what domain the current process's
  22. account belongs to. An array of cSize wide chars is required.
  23. This is recommended to be MAX_COMPUTERNAMELENGTH.
  24. Arguments:
  25. hInst - HINSTANCE of this dll.
  26. ulReason - Context of the attaching/detaching
  27. Return Value:
  28. S_OK - Success
  29. E_* - Problem occured, error passed down.
  30. --*/
  31. {
  32. HRESULT hr = S_OK;
  33. HANDLE hToken = 0;
  34. try {
  35. if( !OpenThreadToken( GetCurrentThread(), TOKEN_QUERY, TRUE, &hToken ) ) {
  36. //
  37. // Ensure failure was because no token existed
  38. //
  39. WsbAffirm( GetLastError() == ERROR_NO_TOKEN, E_FAIL );
  40. //
  41. // attempt to open the process token, since no thread token
  42. // exists
  43. //
  44. WsbAffirmStatus( OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &hToken ) );
  45. }
  46. DWORD dw;
  47. BYTE buf[ 512 ];
  48. TOKEN_USER * pTokenUser = (TOKEN_USER*)buf;
  49. WsbAffirmStatus( GetTokenInformation( hToken, TokenUser, buf, 512, &dw ) );
  50. WCHAR szName[ 256 ];
  51. DWORD cName = 256;
  52. DWORD cDomain = cSize;
  53. SID_NAME_USE snu;
  54. WsbAffirmStatus( LookupAccountSid( 0, pTokenUser->User.Sid, szName, &cName, szDomainName, &cDomain, &snu ) );
  55. } WsbCatch( hr );
  56. if( hToken ) {
  57. CloseHandle( hToken );
  58. }
  59. return( hr );
  60. }
  61. HRESULT
  62. WsbGetServiceInfo(
  63. IN GUID guidApp,
  64. OUT OLECHAR ** pszServiceName, OPTIONAL
  65. OUT OLECHAR ** pszAccountName OPTIONAL
  66. )
  67. /*++
  68. Routine Description:
  69. This function retrieves the name of the service, as well as the
  70. account a COM service runs under. The returned strings are
  71. WsbAlloc'd so they must be freed by the caller.
  72. Arguments:
  73. guidApp - app id of the service to get the account of.
  74. pszServiceName - the name of the service.
  75. pszAccountName - the full account name to set on the account.
  76. Return Value:
  77. S_OK - Success
  78. E_* - Problem occured, error passed down.
  79. --*/
  80. {
  81. HRESULT hr = S_OK;
  82. try {
  83. CWsbStringPtr serviceName;
  84. CWsbStringPtr accountName;
  85. if( pszServiceName ) *pszServiceName = 0;
  86. if( pszAccountName ) *pszAccountName = 0;
  87. //
  88. // Find the service in the registry
  89. //
  90. CWsbStringPtr regPath = L"SOFTWARE\\Classes\\AppID\\";
  91. regPath.Append( CWsbStringPtr( guidApp ) );
  92. //
  93. // Get the name of the service
  94. //
  95. if( pszServiceName ) {
  96. serviceName.Realloc( 255 );
  97. WsbAffirmHr( WsbGetRegistryValueString( 0, regPath, L"LocalService", serviceName, 255, 0 ) );
  98. }
  99. //
  100. // Get the account for it to run under
  101. //
  102. if( pszAccountName ) {
  103. accountName.Realloc( 255 );
  104. hr = WsbGetRegistryValueString( 0, regPath, L"RunAs", accountName, 255, 0 ) ;
  105. if( HRESULT_FROM_WIN32( ERROR_FILE_NOT_FOUND ) == hr ) {
  106. WsbGetLocalSystemName( accountName );
  107. hr = S_OK;
  108. } else {
  109. WsbAffirmHr( hr );
  110. }
  111. }
  112. //
  113. // Wait till end to do final assignments in case error
  114. // occurs, in which case smart pointers automatically
  115. // cleanup for us, and OUT params are not set.
  116. //
  117. if( pszServiceName ) serviceName.GiveTo( pszServiceName );
  118. if( pszAccountName ) accountName.GiveTo( pszAccountName );
  119. } WsbCatch( hr );
  120. return( hr );
  121. }
  122. HRESULT
  123. WsbGetComputerName(
  124. OUT CWsbStringPtr & String
  125. )
  126. /*++
  127. Routine Description:
  128. This routine retrieves the name of the computer into a CWsbStringPtr.
  129. Arguments:
  130. String - String object to fill in with the name.
  131. Return Value:
  132. S_OK - Success
  133. E_* - Problem occured, error passed down.
  134. --*/
  135. {
  136. HRESULT hr = S_OK;
  137. try {
  138. //
  139. // Force allocation of enough characters and call Win32
  140. //
  141. DWORD cbName = MAX_COMPUTERNAME_LENGTH + 1;
  142. WsbAffirmHr( String.Realloc( cbName ) );
  143. WsbAffirmStatus( GetComputerName( String, &cbName ) );
  144. } WsbCatch( hr );
  145. return( hr );
  146. }
  147. HRESULT
  148. WsbGetLocalSystemName(
  149. OUT CWsbStringPtr & String
  150. )
  151. /*++
  152. Routine Description:
  153. This routine retrieves the name of the account for LocalSystem.
  154. Arguments:
  155. String - String object to fill in with the name.
  156. Return Value:
  157. S_OK - Success
  158. E_* - Problem occured, error passed down.
  159. --*/
  160. {
  161. HRESULT hr = S_OK;
  162. try {
  163. //
  164. // For now, hardcode. May need to lookup name of
  165. // SECURITY_LOCAL_SYSTEM_RID
  166. //
  167. String = L"LocalSystem";
  168. } WsbCatch( hr );
  169. return( hr );
  170. }