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.

288 lines
8.3 KiB

  1. /******************************************************************************
  2. Copyright (C) Microsoft Corporation
  3. Module Name:
  4. ProcessOwner.CPP
  5. Abstract:
  6. This module deals with Query functionality of OpenFiles.exe
  7. NT command line utility.
  8. Author:
  9. Akhil Gokhale (akhil.gokhale@wipro.com) 25-APRIL-2001
  10. Revision History:
  11. Akhil Gokhale (akhil.gokhale@wipro.com) 25-APRIL-2001 : Created It.
  12. *****************************************************************************/
  13. #include "pch.h"
  14. #include "OpenFiles.h"
  15. #define SAFE_CLOSE_HANDLE(hHandle) \
  16. if( NULL != hHandle) \
  17. {\
  18. CloseHandle(hHandle);\
  19. hHandle = NULL;\
  20. }\
  21. 1
  22. #define SAFE_FREE_GLOBAL_ALLOC(block) \
  23. if( NULL != block)\
  24. {\
  25. delete block;\
  26. block = NULL;\
  27. }\
  28. 1
  29. #define SAFE_FREE_ARRAY(arr) \
  30. if( NULL != arr)\
  31. {\
  32. delete [] arr;\
  33. arr = NULL;\
  34. }\
  35. 1
  36. BOOL
  37. GetProcessOwner(
  38. OUT LPTSTR pszUserName,
  39. IN DWORD hProcessID
  40. )
  41. /*++
  42. Routine Description:
  43. This function returns the owener (username) of the file.
  44. If a user is Owner of a process, then the file opened by this process will
  45. be owned by this user.
  46. Arguments:
  47. [out] pszUserName : User Name.
  48. [in] hProcessID : Process Handle.
  49. Return Value:
  50. TRUE : If function returns successfully.
  51. FALSE : Otherwise.
  52. --*/
  53. {
  54. DWORD dwRtnCode = 0;
  55. PSID pSidOwner;
  56. BOOL bRtnBool = TRUE;
  57. LPTSTR pszDomainName = NULL,pszAcctName = NULL;
  58. DWORD dwAcctName = 1, dwDomainName = 1;
  59. SID_NAME_USE snuUse = SidTypeUnknown;
  60. PSECURITY_DESCRIPTOR pSD=0;
  61. HANDLE hHandle = GetCurrentProcess();
  62. HANDLE hDynHandle = NULL;
  63. HANDLE hDynToken = NULL;
  64. LUID luidValue;
  65. BOOL bResult = FALSE;
  66. HANDLE hToken = NULL;
  67. TOKEN_PRIVILEGES tkp;
  68. // access token associated with the process
  69. bResult = OpenProcessToken( GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES|
  70. TOKEN_QUERY,&hToken);
  71. if( FALSE == bResult)
  72. {
  73. return FALSE;
  74. }
  75. bResult = LookupPrivilegeValue(NULL,SE_SECURITY_NAME,&luidValue );
  76. if( FALSE == bResult)
  77. {
  78. SAFE_CLOSE_HANDLE(hToken);
  79. return FALSE;
  80. }
  81. // Prepare the token privilege structure
  82. tkp.PrivilegeCount = 0;
  83. tkp.Privileges[0].Luid = luidValue;
  84. tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED|
  85. SE_PRIVILEGE_USED_FOR_ACCESS;
  86. // Now enable the debug privileges in token
  87. bResult = AdjustTokenPrivileges(hToken, FALSE, &tkp,
  88. sizeof(TOKEN_PRIVILEGES),
  89. (PTOKEN_PRIVILEGES) NULL,
  90. (PDWORD)NULL);
  91. if( FALSE == bResult)
  92. {
  93. SAFE_CLOSE_HANDLE(hToken);
  94. return FALSE;
  95. }
  96. // Here you can give any valid process ids..
  97. hDynHandle = OpenProcess(PROCESS_QUERY_INFORMATION,FALSE,hProcessID);
  98. if(NULL == hDynHandle)
  99. {
  100. return FALSE;
  101. }
  102. bResult = OpenProcessToken(hDynHandle,TOKEN_QUERY,&hDynToken);
  103. if( FALSE == bResult)
  104. {
  105. SAFE_CLOSE_HANDLE(hDynHandle);
  106. return FALSE;
  107. }
  108. TOKEN_USER * pUser = NULL;
  109. DWORD cb = 0;
  110. // determine size of the buffer needed to receive all information
  111. if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &cb))
  112. {
  113. if ( ERROR_INSUFFICIENT_BUFFER != GetLastError())
  114. {
  115. SAFE_CLOSE_HANDLE(hToken);
  116. SAFE_CLOSE_HANDLE(hDynHandle);
  117. SAFE_CLOSE_HANDLE(hDynToken);
  118. return FALSE;
  119. }
  120. }
  121. try
  122. {
  123. // '_alloca' can throw exception.
  124. pUser = (TOKEN_USER *)_alloca(cb);
  125. if( NULL == pUser)
  126. {
  127. SAFE_CLOSE_HANDLE(hToken);
  128. SAFE_CLOSE_HANDLE(hDynHandle);
  129. SAFE_CLOSE_HANDLE(hDynToken);
  130. return FALSE;
  131. }
  132. }
  133. catch(...)
  134. {
  135. SAFE_CLOSE_HANDLE(hToken);
  136. SAFE_CLOSE_HANDLE(hDynHandle);
  137. SAFE_CLOSE_HANDLE(hDynToken);
  138. return FALSE;
  139. }
  140. if (!GetTokenInformation(hDynToken, TokenUser, pUser, cb, &cb))
  141. {
  142. SAFE_CLOSE_HANDLE(hToken);
  143. SAFE_CLOSE_HANDLE(hDynHandle);
  144. SAFE_CLOSE_HANDLE(hDynToken);
  145. return FALSE;
  146. }
  147. PSID pSid = pUser->User.Sid;
  148. // Allocate memory for the SID structure.
  149. pSidOwner = new SID;
  150. // Allocate memory for the security descriptor structure.
  151. pSD = new SECURITY_DESCRIPTOR;
  152. if( NULL == pSidOwner || NULL == pSD)
  153. {
  154. SAFE_CLOSE_HANDLE(hToken);
  155. SAFE_CLOSE_HANDLE(hDynHandle);
  156. SAFE_CLOSE_HANDLE(hDynToken);
  157. SAFE_FREE_GLOBAL_ALLOC(pSD);
  158. SAFE_FREE_GLOBAL_ALLOC(pSidOwner);
  159. return FALSE;
  160. }
  161. // First call to LookupAccountSid to get the buffer sizes.
  162. bRtnBool = LookupAccountSid(
  163. NULL, // local computer
  164. pUser->User.Sid,
  165. NULL, // AcctName
  166. (LPDWORD)&dwAcctName,
  167. NULL, // DomainName
  168. (LPDWORD)&dwDomainName,
  169. &snuUse);
  170. pszAcctName = new TCHAR[dwAcctName+1];
  171. pszDomainName = new TCHAR[dwDomainName+1];
  172. if( NULL == pszAcctName|| NULL == pszDomainName)
  173. {
  174. SAFE_CLOSE_HANDLE(hToken);
  175. SAFE_CLOSE_HANDLE(hDynHandle);
  176. SAFE_CLOSE_HANDLE(hDynToken);
  177. SAFE_FREE_ARRAY(pszAcctName);
  178. SAFE_FREE_ARRAY(pszDomainName);
  179. return FALSE;
  180. }
  181. // Second call to LookupAccountSid to get the account name.
  182. bRtnBool = LookupAccountSid(
  183. NULL, // name of local or remote computer
  184. pUser->User.Sid, // security identifier
  185. pszAcctName, // account name buffer
  186. (LPDWORD)&dwAcctName, // size of account name buffer
  187. pszDomainName, // domain name
  188. (LPDWORD)&dwDomainName, // size of domain name buffer
  189. &snuUse); // SID type
  190. SAFE_CLOSE_HANDLE(hDynHandle);
  191. SAFE_CLOSE_HANDLE(hDynToken);
  192. SAFE_FREE_GLOBAL_ALLOC(pSD);
  193. SAFE_FREE_GLOBAL_ALLOC(pSidOwner);
  194. // Check GetLastError for LookupAccountSid error condition.
  195. if ( FALSE == bRtnBool)
  196. {
  197. SAFE_CLOSE_HANDLE(hToken);
  198. SAFE_CLOSE_HANDLE(hDynHandle);
  199. SAFE_CLOSE_HANDLE(hDynToken);
  200. SAFE_FREE_ARRAY(pszAcctName);
  201. SAFE_FREE_ARRAY(pszDomainName);
  202. return FALSE;
  203. } else
  204. {
  205. // Check if user is "NT AUTHORITY".
  206. if(CSTR_EQUAL == CompareString(MAKELCID( MAKELANGID(LANG_ENGLISH,
  207. SUBLANG_ENGLISH_US),
  208. SORT_DEFAULT),
  209. NORM_IGNORECASE,
  210. pszDomainName,
  211. StringLength(pszDomainName,0),
  212. NTAUTHORITY_USER ,
  213. StringLength(NTAUTHORITY_USER, 0)
  214. ))
  215. {
  216. SAFE_CLOSE_HANDLE(hToken);
  217. SAFE_CLOSE_HANDLE(hDynHandle);
  218. SAFE_CLOSE_HANDLE(hDynToken);
  219. SAFE_FREE_ARRAY(pszAcctName);
  220. SAFE_FREE_ARRAY(pszDomainName);
  221. return FALSE;
  222. }
  223. else
  224. {
  225. StringCopy(pszUserName,pszAcctName,MIN_MEMORY_REQUIRED);
  226. SAFE_CLOSE_HANDLE(hToken);
  227. SAFE_CLOSE_HANDLE(hDynHandle);
  228. SAFE_CLOSE_HANDLE(hDynToken);
  229. SAFE_FREE_ARRAY(pszAcctName);
  230. SAFE_FREE_ARRAY(pszDomainName);
  231. return TRUE;
  232. }
  233. }
  234. // Release memory.
  235. SAFE_FREE_ARRAY(pszAcctName);
  236. SAFE_FREE_ARRAY(pszDomainName);
  237. SAFE_CLOSE_HANDLE(hDynHandle);
  238. SAFE_CLOSE_HANDLE(hDynToken);
  239. SAFE_CLOSE_HANDLE(hToken);
  240. SAFE_FREE_GLOBAL_ALLOC(pSD);
  241. SAFE_FREE_GLOBAL_ALLOC(pSidOwner);
  242. return FALSE;
  243. }