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. |
11 #include <LMCons.h> // For UNLEN | 11 #include <LMCons.h> // For UNLEN |
12 | 12 |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
16 #include "base/win/scoped_handle.h" | 16 #include "base/win/scoped_handle.h" |
17 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
18 #include "rlz/lib/assert.h" | 18 #include "rlz/lib/assert.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 HRESULT GetCurrentUser(std::wstring* name, | 22 HRESULT GetCurrentUser(std::wstring* name, |
23 std::wstring* domain, | 23 std::wstring* domain, |
24 std::wstring* sid) { | 24 std::wstring* sid) { |
25 DWORD err; | 25 DWORD err; |
26 DWORD zero = 0; | |
26 | 27 |
27 // Get the current username & domain the hard way. (GetUserNameEx would be | 28 // Get the current username & domain the hard way. (GetUserNameEx would be |
28 // nice, but unfortunately requires connectivity to a domain controller. | 29 // nice, but unfortunately requires connectivity to a domain controller. |
29 // Useless.) | 30 // Useless.) |
30 | 31 |
31 // (Following call doesn't work if running as a Service - because a Service | 32 // (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. | 33 // 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 | 34 // In which case, search for and use the process handle of a running |
34 // Explorer.exe.) | 35 // Explorer.exe.) |
35 HANDLE token; | 36 HANDLE token; |
36 if (!::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)) | 37 |
37 return E_FAIL; | 38 HANDLE process_handle = ::GetCurrentProcess(); |
39 if (!process_handle) | |
40 CHECK(process_handle); | |
41 | |
42 if (!::OpenProcessToken(process_handle, TOKEN_QUERY, &token)) { | |
43 err = ::GetLastError(); | |
44 // Check should fail. | |
45 CHECK_EQ(err, zero); | |
46 return err; | |
47 } | |
Roger Tawa OOO till Jul 10th
2014/02/28 19:34:00
From the docs, I don't believe getting a handle to
yao
2014/02/28 21:28:19
Done.
| |
38 | 48 |
39 base::win::ScopedHandle scoped_process_token(token); | 49 base::win::ScopedHandle scoped_process_token(token); |
40 | 50 |
41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the | 51 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the |
42 // required size.) | 52 // required size.) |
43 scoped_ptr<char[]> token_user_bytes; | 53 scoped_ptr<char[]> token_user_bytes; |
44 DWORD token_user_size; | 54 DWORD token_user_size; |
45 DWORD token_user_size2; | 55 DWORD token_user_size2; |
46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, | 56 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, |
47 &token_user_size); | 57 &token_user_size); |
48 err = ::GetLastError(); | 58 err = ::GetLastError(); |
49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); | 59 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); |
50 | 60 |
51 token_user_bytes.reset(new char[token_user_size]); | 61 token_user_bytes.reset(new char[token_user_size]); |
52 if (!token_user_bytes.get()) | 62 if (!token_user_bytes.get()) { |
63 // Check should fail. | |
64 CHECK_EQ(E_OUTOFMEMORY, 0); | |
53 return E_OUTOFMEMORY; | 65 return E_OUTOFMEMORY; |
66 } | |
Roger Tawa OOO till Jul 10th
2014/02/28 19:34:00
Can probably replace lines 62-66 with:
CHECK(t
yao
2014/02/28 21:28:19
Done.
| |
54 | 67 |
55 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(), | 68 if (!::GetTokenInformation(token, TokenUser, token_user_bytes.get(), |
56 token_user_size, &token_user_size2)) { | 69 token_user_size, &token_user_size2)) { |
57 return E_FAIL; | 70 err = ::GetLastError(); |
71 // Check should fail. | |
72 CHECK_EQ(err, zero); | |
73 return err; | |
58 } | 74 } |
59 | 75 |
60 WCHAR user_name[UNLEN + 1]; // max username length | 76 WCHAR user_name[UNLEN + 1]; // max username length |
61 WCHAR domain_name[UNLEN + 1]; | 77 WCHAR domain_name[UNLEN + 1]; |
62 DWORD user_name_size = UNLEN + 1; | 78 DWORD user_name_size = UNLEN + 1; |
63 DWORD domain_name_size = UNLEN + 1; | 79 DWORD domain_name_size = UNLEN + 1; |
64 SID_NAME_USE sid_type; | 80 SID_NAME_USE sid_type; |
65 TOKEN_USER* token_user = | 81 TOKEN_USER* token_user = |
66 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); | 82 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); |
67 if (!token_user) | 83 if (!token_user) { |
68 return E_FAIL; | 84 err = ::GetLastError(); |
85 // Check should fail. | |
86 CHECK_EQ(err, zero); | |
87 return err; | |
88 } | |
69 PSID user_sid = token_user->User.Sid; | 89 PSID user_sid = token_user->User.Sid; |
70 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, | 90 if (!::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, |
71 domain_name, &domain_name_size, &sid_type)) { | 91 domain_name, &domain_name_size, &sid_type)) { |
72 return E_FAIL; | 92 err = ::GetLastError(); |
93 // Check should fail. | |
94 CHECK_EQ(err, zero); | |
95 return err; | |
73 } | 96 } |
74 | 97 |
75 if (name != NULL) { | 98 if (name != NULL) { |
76 *name = user_name; | 99 *name = user_name; |
77 } | 100 } |
78 if (domain != NULL) { | 101 if (domain != NULL) { |
79 *domain = domain_name; | 102 *domain = domain_name; |
80 } | 103 } |
81 if (sid != NULL) { | 104 if (sid != NULL) { |
82 LPWSTR string_sid; | 105 LPWSTR string_sid; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 } | 209 } |
187 | 210 |
188 evaluated = true; | 211 evaluated = true; |
189 if (!has_rights) | 212 if (!has_rights) |
190 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); | 213 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); |
191 | 214 |
192 return has_rights; | 215 return has_rights; |
193 } | 216 } |
194 | 217 |
195 }; // namespace | 218 }; // namespace |
OLD | NEW |