| OLD | NEW |
| (Empty) |
| 1 // Copyright 2004-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 | |
| 17 #include "omaha/base/user_info.h" | |
| 18 | |
| 19 #include <windows.h> | |
| 20 #include <security.h> | |
| 21 #include <secext.h> | |
| 22 #include <sddl.h> | |
| 23 #include <lmcons.h> | |
| 24 #include <atlsecurity.h> | |
| 25 #include "base/scoped_ptr.h" | |
| 26 #include "omaha/base/utils.h" | |
| 27 #include "omaha/base/constants.h" | |
| 28 #include "omaha/base/debug.h" | |
| 29 #include "omaha/base/error.h" | |
| 30 #include "omaha/base/scoped_any.h" | |
| 31 | |
| 32 namespace omaha { | |
| 33 | |
| 34 namespace user_info { | |
| 35 | |
| 36 HRESULT GetProcessUser(CString* name, CString* domain, CString* sid) { | |
| 37 CSid current_sid; | |
| 38 | |
| 39 HRESULT hr = GetProcessUserSid(¤t_sid); | |
| 40 if (FAILED(hr)) { | |
| 41 return hr; | |
| 42 } | |
| 43 | |
| 44 if (sid != NULL) { | |
| 45 *sid = current_sid.Sid(); | |
| 46 } | |
| 47 if (name != NULL) { | |
| 48 *name = current_sid.AccountName(); | |
| 49 } | |
| 50 if (domain != NULL) { | |
| 51 *domain = current_sid.Domain(); | |
| 52 } | |
| 53 return S_OK; | |
| 54 } | |
| 55 | |
| 56 HRESULT GetProcessUserSid(CSid* sid) { | |
| 57 ASSERT1(sid); | |
| 58 | |
| 59 CAccessToken token; | |
| 60 if (!token.GetProcessToken(TOKEN_QUERY) || !token.GetUser(sid)) { | |
| 61 HRESULT hr = HRESULTFromLastError(); | |
| 62 | |
| 63 // Assert only if thread_sid is populated. This is to eliminate other | |
| 64 // reasons for GetProcessToken/GetUser to fail. | |
| 65 CString thread_sid; | |
| 66 ASSERT(FAILED(GetThreadUserSid(&thread_sid)), | |
| 67 (_T("[Did you mean to call GetThreadUserSid?][0x%x][%s]"), | |
| 68 hr, thread_sid)); | |
| 69 | |
| 70 return hr; | |
| 71 } | |
| 72 | |
| 73 return S_OK; | |
| 74 } | |
| 75 | |
| 76 HRESULT IsLocalSystemUser(bool* is_local_system, CString* user_sid) { | |
| 77 ASSERT1(is_local_system); | |
| 78 | |
| 79 CString sid; | |
| 80 HRESULT hr = GetProcessUser(NULL, NULL, &sid); | |
| 81 if (FAILED(hr)) { | |
| 82 return hr; | |
| 83 } | |
| 84 *is_local_system = sid.CompareNoCase(kLocalSystemSid) == 0; | |
| 85 if (user_sid) { | |
| 86 user_sid->SetString(sid); | |
| 87 } | |
| 88 return S_OK; | |
| 89 } | |
| 90 | |
| 91 HRESULT GetThreadUserSid(CString* sid) { | |
| 92 ASSERT1(sid); | |
| 93 CAccessToken access_token; | |
| 94 CSid user_sid; | |
| 95 if (access_token.GetThreadToken(TOKEN_READ) && | |
| 96 access_token.GetUser(&user_sid)) { | |
| 97 sid->SetString(user_sid.Sid()); | |
| 98 return S_OK; | |
| 99 } else { | |
| 100 HRESULT hr = HRESULTFromLastError(); | |
| 101 UTIL_LOG(L2, (_T("[GetThreadUserSid failed][0x%x]"), hr)); | |
| 102 return hr; | |
| 103 } | |
| 104 } | |
| 105 | |
| 106 HRESULT GetEffectiveUserSid(CString* sid) { | |
| 107 HRESULT hr = GetThreadUserSid(sid); | |
| 108 return SUCCEEDED(hr) ? hr : GetProcessUser(NULL, NULL, sid); | |
| 109 } | |
| 110 | |
| 111 bool IsRunningAsSystem() { | |
| 112 CString sid; | |
| 113 return SUCCEEDED(GetEffectiveUserSid(&sid)) ? IsLocalSystemSid(sid) : false; | |
| 114 } | |
| 115 | |
| 116 bool IsThreadImpersonating() { | |
| 117 CAccessToken access_token; | |
| 118 return access_token.GetThreadToken(TOKEN_READ); | |
| 119 } | |
| 120 | |
| 121 } // namespace user_info | |
| 122 | |
| 123 } // namespace omaha | |
| 124 | |
| OLD | NEW |