OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // Information about the current process. | 5 // Information about the current process. |
6 | 6 |
7 #include "rlz/win/lib/process_info.h" | 7 #include "rlz/win/lib/process_info.h" |
8 | 8 |
9 #include <windows.h> | 9 #include <windows.h> |
10 #include <Sddl.h> // For ConvertSidToStringSid. | 10 #include <Sddl.h> // For ConvertSidToStringSid. |
(...skipping 15 matching lines...) Expand all Loading... |
26 | 26 |
27 // Get the current username & domain the hard way. (GetUserNameEx would be | 27 // Get the current username & domain the hard way. (GetUserNameEx would be |
28 // nice, but unfortunately requires connectivity to a domain controller. | 28 // nice, but unfortunately requires connectivity to a domain controller. |
29 // Useless.) | 29 // Useless.) |
30 | 30 |
31 // (Following call doesn't work if running as a Service - because a Service | 31 // (Following call doesn't work if running as a Service - because a Service |
32 // runs under special accounts like LOCAL_SYSTEM, not as the logged in user. | 32 // runs under special accounts like LOCAL_SYSTEM, not as the logged in user. |
33 // In which case, search for and use the process handle of a running | 33 // In which case, search for and use the process handle of a running |
34 // Explorer.exe.) | 34 // Explorer.exe.) |
35 HANDLE token; | 35 HANDLE token; |
36 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) | 36 |
37 return E_FAIL; | 37 CHECK(::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)); |
38 | 38 |
39 base::win::ScopedHandle scoped_process_token(token); | 39 base::win::ScopedHandle scoped_process_token(token); |
40 | 40 |
41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the | 41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the |
42 // required size.) | 42 // required size.) |
43 scoped_ptr<char[]> token_user_bytes; | 43 scoped_ptr<char[]> token_user_bytes; |
44 DWORD token_user_size; | 44 DWORD token_user_size; |
45 DWORD token_user_size2; | 45 DWORD token_user_size2; |
46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, | 46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, |
47 &token_user_size); | 47 &token_user_size); |
48 err = ::GetLastError(); | 48 err = ::GetLastError(); |
49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); | 49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); |
50 | 50 |
51 token_user_bytes.reset(new char[token_user_size]); | 51 token_user_bytes.reset(new char[token_user_size]); |
52 if (!token_user_bytes.get()) | 52 CHECK(token_user_bytes.get()); |
53 return E_OUTOFMEMORY; | |
54 | 53 |
55 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(), | 54 CHECK(::GetTokenInformation(token, TokenUser, token_user_bytes.get(), |
56 token_user_size, &token_user_size2)) { | 55 token_user_size, &token_user_size2)); |
57 return E_FAIL; | |
58 } | |
59 | 56 |
60 WCHAR user_name[UNLEN + 1]; // max username length | 57 WCHAR user_name[UNLEN + 1]; // max username length |
61 WCHAR domain_name[UNLEN + 1]; | 58 WCHAR domain_name[UNLEN + 1]; |
62 DWORD user_name_size = UNLEN + 1; | 59 DWORD user_name_size = UNLEN + 1; |
63 DWORD domain_name_size = UNLEN + 1; | 60 DWORD domain_name_size = UNLEN + 1; |
64 SID_NAME_USE sid_type; | 61 SID_NAME_USE sid_type; |
65 TOKEN_USER* token_user = | 62 TOKEN_USER* token_user = |
66 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); | 63 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); |
67 if (!token_user) | 64 CHECK(token_user); |
68 return E_FAIL; | 65 |
69 PSID user_sid = token_user->User.Sid; | 66 PSID user_sid = token_user->User.Sid; |
70 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, | 67 CHECK(::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, |
71 domain_name, &domain_name_size, &sid_type)) { | 68 domain_name, &domain_name_size, &sid_type)); |
72 return E_FAIL; | |
73 } | |
74 | 69 |
75 if (name != NULL) { | 70 if (name != NULL) { |
76 *name = user_name; | 71 *name = user_name; |
77 } | 72 } |
78 if (domain != NULL) { | 73 if (domain != NULL) { |
79 *domain = domain_name; | 74 *domain = domain_name; |
80 } | 75 } |
81 if (sid != NULL) { | 76 if (sid != NULL) { |
82 LPWSTR string_sid; | 77 LPWSTR string_sid; |
83 ConvertSidToStringSidW(user_sid, &string_sid); | 78 ConvertSidToStringSidW(user_sid, &string_sid); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 } | 181 } |
187 | 182 |
188 evaluated = true; | 183 evaluated = true; |
189 if (!has_rights) | 184 if (!has_rights) |
190 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); | 185 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); |
191 | 186 |
192 return has_rights; | 187 return has_rights; |
193 } | 188 } |
194 | 189 |
195 }; // namespace | 190 }; // namespace |
OLD | NEW |