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
3 // found in the LICENSE file.
5 // Information about the current process.
7 #include "rlz/win/lib/process_info.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/process/process_handle.h"
13 #include "base/strings/string16.h"
14 #include "base/win/scoped_handle.h"
15 #include "base/win/win_util.h"
16 #include "base/win/windows_version.h"
17 #include "rlz/lib/assert.h"
21 HRESULT GetElevationType(PTOKEN_ELEVATION_TYPE elevation) {
25 *elevation = TokenElevationTypeDefault;
27 if (base::win::GetVersion() < base::win::VERSION_VISTA)
31 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token))
32 return HRESULT_FROM_WIN32(GetLastError());
34 base::win::ScopedHandle scoped_process_token(process_token);
37 TOKEN_ELEVATION_TYPE elevation_type;
38 if (!GetTokenInformation(process_token, TokenElevationType, &elevation_type,
39 sizeof(elevation_type), &size)) {
40 return HRESULT_FROM_WIN32(GetLastError());
43 *elevation = elevation_type;
47 // based on http://msdn2.microsoft.com/en-us/library/aa376389.aspx
48 bool GetUserGroup(long* group) {
54 // groups are listed in DECREASING order of importance
55 // (eg. If a user is a member of both the admin group and
56 // the power user group, it is more useful to list the user
58 DWORD user_groups[] = {DOMAIN_ALIAS_RID_ADMINS,
59 DOMAIN_ALIAS_RID_POWER_USERS};
60 SID_IDENTIFIER_AUTHORITY nt_authority = SECURITY_NT_AUTHORITY;
62 for (int i = 0; i < arraysize(user_groups) && *group == 0; ++i) {
64 if (AllocateAndInitializeSid(&nt_authority, 2,
65 SECURITY_BUILTIN_DOMAIN_RID,
66 user_groups[i], 0, 0, 0, 0,
67 0, 0, ¤t_group)) {
69 if (CheckTokenMembership(NULL, current_group, ¤t_level) &&
71 *group = user_groups[i];
74 FreeSid(current_group);
85 bool ProcessInfo::IsRunningAsSystem() {
86 static base::string16 user_sid;
87 if (user_sid.empty()) {
88 if (!base::win::GetUserSidString(&user_sid))
91 return (user_sid == L"S-1-5-18");
94 bool ProcessInfo::HasAdminRights() {
95 static bool evaluated = false;
96 static bool has_rights = false;
99 if (IsRunningAsSystem()) {
101 } else if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
102 TOKEN_ELEVATION_TYPE elevation;
103 base::IntegrityLevel level;
105 if (SUCCEEDED(GetElevationType(&elevation)) &&
106 base::GetProcessIntegrityLevel(base::GetCurrentProcessHandle(), &level))
107 has_rights = (elevation == TokenElevationTypeFull) ||
108 (level == base::HIGH_INTEGRITY);
111 if (GetUserGroup(&group))
112 has_rights = (group == DOMAIN_ALIAS_RID_ADMINS);
118 ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights.");