OLD | NEW |
| (Empty) |
1 // Copyright 2004-2009 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 // Enumeration of the user accounts on the PC. | |
17 | |
18 #include "omaha/base/accounts.h" | |
19 | |
20 #include <sddl.h> | |
21 #include "base/basictypes.h" | |
22 #include "omaha/base/debug.h" | |
23 #include "omaha/base/error.h" | |
24 #include "omaha/base/reg_key.h" | |
25 | |
26 namespace omaha { | |
27 | |
28 namespace accounts { | |
29 | |
30 const wchar_t kActiveProfilesKey[] = | |
31 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList"; | |
32 | |
33 HRESULT GetAllUserSids(CSimpleArray<CString> *sid_array) { | |
34 ASSERT(sid_array, (L"")); | |
35 | |
36 RegKey key_profiles; | |
37 HRESULT hr = key_profiles.Open(HKEY_LOCAL_MACHINE, kActiveProfilesKey); | |
38 if (FAILED(hr)) { | |
39 return hr; | |
40 } | |
41 | |
42 sid_array->RemoveAll(); | |
43 | |
44 uint32 total_keys = key_profiles.GetSubkeyCount(); | |
45 | |
46 for (uint32 i = 0 ; i < total_keys ; ++i) { | |
47 CString possible_user_sid, name, domain; | |
48 SID_NAME_USE user_type; | |
49 | |
50 if (SUCCEEDED(key_profiles.GetSubkeyNameAt(i, &possible_user_sid))) { | |
51 if (SUCCEEDED(GetUserInfo(possible_user_sid, &name, | |
52 &domain, &user_type)) && | |
53 user_type == SidTypeUser) { | |
54 sid_array->Add(possible_user_sid); | |
55 } | |
56 } | |
57 } | |
58 | |
59 return hr; | |
60 } | |
61 | |
62 HRESULT GetUserInfo(const wchar_t *sid_str, CString *name, | |
63 CString *domain, SID_NAME_USE *user_type) { | |
64 ASSERT(sid_str, (L"")); | |
65 ASSERT(name, (L"")); | |
66 ASSERT(domain, (L"")); | |
67 ASSERT(user_type, (L"")); | |
68 | |
69 PSID sid = NULL; | |
70 HRESULT ret = E_FAIL; | |
71 if (ConvertStringSidToSid(sid_str, &sid)) { | |
72 DWORD name_size = 0, domain_size = 0; | |
73 if (!LookupAccountSid(NULL, sid, NULL, &name_size, NULL, | |
74 &domain_size, user_type) && | |
75 ERROR_INSUFFICIENT_BUFFER != GetLastError()) { | |
76 ret = GetCurError(); | |
77 LocalFree(sid); | |
78 return ret; | |
79 } | |
80 | |
81 ASSERT(name_size, (L"")); | |
82 ASSERT(domain_size, (L"")); | |
83 if (!domain_size || !name_size) { | |
84 LocalFree(sid); | |
85 return E_UNEXPECTED; | |
86 } | |
87 | |
88 wchar_t* c_name = new wchar_t[name_size]; | |
89 ASSERT(c_name, (L"")); | |
90 if (!c_name) { | |
91 LocalFree(sid); | |
92 return E_OUTOFMEMORY; | |
93 } | |
94 | |
95 wchar_t* c_domain = new wchar_t[domain_size]; | |
96 ASSERT(c_domain, (L"")); | |
97 if (!c_domain) { | |
98 delete[] c_name; | |
99 LocalFree(sid); | |
100 return E_OUTOFMEMORY; | |
101 } | |
102 | |
103 if (LookupAccountSid(NULL, sid, c_name, &name_size, c_domain, | |
104 &domain_size, user_type)) { | |
105 ret = S_OK; | |
106 name->SetString(c_name); | |
107 domain->SetString(c_domain); | |
108 } else { | |
109 ret = GetCurError(); | |
110 } | |
111 | |
112 delete[] c_name; | |
113 delete[] c_domain; | |
114 LocalFree(sid); | |
115 } | |
116 | |
117 return ret; | |
118 } | |
119 | |
120 } // namespace accounts | |
121 | |
122 } // namespace omaha | |
123 | |
OLD | NEW |