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. | |
11 #include <LMCons.h> // For UNLEN | |
12 | 10 |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
15 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
| 13 #include "base/strings/string16.h" |
16 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
| 15 #include "base/win/win_util.h" |
17 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
18 #include "rlz/lib/assert.h" | 17 #include "rlz/lib/assert.h" |
19 | 18 |
20 namespace { | 19 namespace { |
21 | 20 |
22 HRESULT GetCurrentUser(std::wstring* name, | |
23 std::wstring* domain, | |
24 std::wstring* sid) { | |
25 DWORD err; | |
26 | |
27 // Get the current username & domain the hard way. (GetUserNameEx would be | |
28 // nice, but unfortunately requires connectivity to a domain controller. | |
29 // Useless.) | |
30 | |
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. | |
33 // In which case, search for and use the process handle of a running | |
34 // Explorer.exe.) | |
35 HANDLE token; | |
36 | |
37 CHECK(::OpenProcessToken(::GetCurrentProcess(), TOKEN_QUERY, &token)); | |
38 | |
39 base::win::ScopedHandle scoped_process_token(token); | |
40 | |
41 // (Following call will fail with ERROR_INSUFFICIENT_BUFFER and give us the | |
42 // required size.) | |
43 scoped_ptr<char[]> token_user_bytes; | |
44 DWORD token_user_size; | |
45 DWORD token_user_size2; | |
46 BOOL result = ::GetTokenInformation(token, TokenUser, NULL, 0, | |
47 &token_user_size); | |
48 err = ::GetLastError(); | |
49 CHECK(!result && err == ERROR_INSUFFICIENT_BUFFER); | |
50 | |
51 token_user_bytes.reset(new char[token_user_size]); | |
52 CHECK(token_user_bytes.get()); | |
53 | |
54 CHECK(::GetTokenInformation(token, TokenUser, token_user_bytes.get(), | |
55 token_user_size, &token_user_size2)); | |
56 | |
57 WCHAR user_name[UNLEN + 1]; // max username length | |
58 WCHAR domain_name[UNLEN + 1]; | |
59 DWORD user_name_size = UNLEN + 1; | |
60 DWORD domain_name_size = UNLEN + 1; | |
61 SID_NAME_USE sid_type; | |
62 TOKEN_USER* token_user = | |
63 reinterpret_cast<TOKEN_USER*>(token_user_bytes.get()); | |
64 CHECK(token_user); | |
65 | |
66 PSID user_sid = token_user->User.Sid; | |
67 CHECK(::LookupAccountSidW(NULL, user_sid, user_name, &user_name_size, | |
68 domain_name, &domain_name_size, &sid_type)); | |
69 | |
70 if (name != NULL) { | |
71 *name = user_name; | |
72 } | |
73 if (domain != NULL) { | |
74 *domain = domain_name; | |
75 } | |
76 if (sid != NULL) { | |
77 LPWSTR string_sid; | |
78 ConvertSidToStringSidW(user_sid, &string_sid); | |
79 *sid = string_sid; // copy out to cstring | |
80 // free memory, as documented for ConvertSidToStringSid | |
81 LocalFree(string_sid); | |
82 } | |
83 | |
84 return S_OK; | |
85 } | |
86 | |
87 HRESULT GetElevationType(PTOKEN_ELEVATION_TYPE elevation) { | 21 HRESULT GetElevationType(PTOKEN_ELEVATION_TYPE elevation) { |
88 if (!elevation) | 22 if (!elevation) |
89 return E_POINTER; | 23 return E_POINTER; |
90 | 24 |
91 *elevation = TokenElevationTypeDefault; | 25 *elevation = TokenElevationTypeDefault; |
92 | 26 |
93 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 27 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
94 return E_FAIL; | 28 return E_FAIL; |
95 | 29 |
96 HANDLE process_token; | 30 HANDLE process_token; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 } | 76 } |
143 | 77 |
144 return group != 0; | 78 return group != 0; |
145 } | 79 } |
146 } //anonymous | 80 } //anonymous |
147 | 81 |
148 | 82 |
149 namespace rlz_lib { | 83 namespace rlz_lib { |
150 | 84 |
151 bool ProcessInfo::IsRunningAsSystem() { | 85 bool ProcessInfo::IsRunningAsSystem() { |
152 static std::wstring name; | 86 static base::string16 user_sid; |
153 static std::wstring domain; | 87 if (user_sid.empty()) { |
154 static std::wstring sid; | 88 if (!base::win::GetUserSidString(&user_sid)) |
155 if (name.empty()) | 89 return false; |
156 CHECK(SUCCEEDED(GetCurrentUser(&name, &domain, &sid))); | 90 } |
157 | 91 return (user_sid == L"S-1-5-18"); |
158 return (name == L"SYSTEM"); | |
159 } | 92 } |
160 | 93 |
161 bool ProcessInfo::HasAdminRights() { | 94 bool ProcessInfo::HasAdminRights() { |
162 static bool evaluated = false; | 95 static bool evaluated = false; |
163 static bool has_rights = false; | 96 static bool has_rights = false; |
164 | 97 |
165 if (!evaluated) { | 98 if (!evaluated) { |
166 if (IsRunningAsSystem()) { | 99 if (IsRunningAsSystem()) { |
167 has_rights = true; | 100 has_rights = true; |
168 } else if (base::win::GetVersion() >= base::win::VERSION_VISTA) { | 101 } else if (base::win::GetVersion() >= base::win::VERSION_VISTA) { |
(...skipping 12 matching lines...) Expand all Loading... |
181 } | 114 } |
182 | 115 |
183 evaluated = true; | 116 evaluated = true; |
184 if (!has_rights) | 117 if (!has_rights) |
185 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); | 118 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights."); |
186 | 119 |
187 return has_rights; | 120 return has_rights; |
188 } | 121 } |
189 | 122 |
190 }; // namespace | 123 }; // namespace |
OLD | NEW |