process_info.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.
  4. //
  5. // Information about the current process.
  6. #include <string>
  7. #include "rlz/win/lib/process_info.h"
  8. #include <windows.h>
  9. #include <stddef.h>
  10. #include "base/process/process_info.h"
  11. #include "base/win/scoped_handle.h"
  12. #include "base/win/win_util.h"
  13. #include "base/win/windows_version.h"
  14. #include "rlz/lib/assert.h"
  15. namespace {
  16. HRESULT GetElevationType(PTOKEN_ELEVATION_TYPE elevation) {
  17. if (!elevation)
  18. return E_POINTER;
  19. *elevation = TokenElevationTypeDefault;
  20. HANDLE process_token;
  21. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &process_token))
  22. return HRESULT_FROM_WIN32(GetLastError());
  23. base::win::ScopedHandle scoped_process_token(process_token);
  24. DWORD size;
  25. TOKEN_ELEVATION_TYPE elevation_type;
  26. if (!GetTokenInformation(process_token, TokenElevationType, &elevation_type,
  27. sizeof(elevation_type), &size)) {
  28. return HRESULT_FROM_WIN32(GetLastError());
  29. }
  30. *elevation = elevation_type;
  31. return S_OK;
  32. }
  33. } //anonymous
  34. namespace rlz_lib {
  35. bool ProcessInfo::IsRunningAsSystem() {
  36. static std::wstring user_sid;
  37. if (user_sid.empty()) {
  38. if (!base::win::GetUserSidString(&user_sid))
  39. return false;
  40. }
  41. return (user_sid == L"S-1-5-18");
  42. }
  43. bool ProcessInfo::HasAdminRights() {
  44. static bool evaluated = false;
  45. static bool has_rights = false;
  46. if (!evaluated) {
  47. if (IsRunningAsSystem()) {
  48. has_rights = true;
  49. } else {
  50. TOKEN_ELEVATION_TYPE elevation;
  51. if (SUCCEEDED(GetElevationType(&elevation))) {
  52. base::IntegrityLevel level = base::GetCurrentProcessIntegrityLevel();
  53. if (level != base::INTEGRITY_UNKNOWN) {
  54. has_rights = (elevation == TokenElevationTypeFull) ||
  55. (level == base::HIGH_INTEGRITY);
  56. }
  57. }
  58. }
  59. }
  60. evaluated = true;
  61. if (!has_rights)
  62. ASSERT_STRING("ProcessInfo::HasAdminRights: Does not have admin rights.");
  63. return has_rights;
  64. }
  65. } // namespace rlz_lib