OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 The Chromium OS 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 #include "chromeos_login_helpers.h" // NOLINT | |
6 | |
7 #include <base/basictypes.h> | |
8 #include <base/logging.h> | |
9 #include <base/string_util.h> | |
10 #include <dbus/dbus-glib-lowlevel.h> | |
11 #include <chromeos/dbus/dbus.h> | |
12 #include <chromeos/dbus/service_constants.h> | |
13 #include <chromeos/glib/object.h> | |
14 #include <chromeos/string.h> | |
15 #include <vector> | |
16 | |
17 #include "marshal.glibmarshal.h" // NOLINT | |
18 #include "chromeos_login.h" // NOLINT | |
19 | |
20 namespace chromeos { // NOLINT | |
21 | |
22 #define SCOPED_SAFE_MESSAGE(e) (e->message ? e->message : "unknown error") | |
23 | |
24 ChromeOSLoginHelpers::ChromeOSLoginHelpers() {} | |
25 | |
26 ChromeOSLoginHelpers::~ChromeOSLoginHelpers() {} | |
27 | |
28 // static | |
29 dbus::Proxy ChromeOSLoginHelpers::CreateProxy() { | |
30 dbus::BusConnection bus = dbus::GetSystemBusConnection(); | |
31 return dbus::Proxy(bus, | |
32 login_manager::kSessionManagerServiceName, | |
33 login_manager::kSessionManagerServicePath, | |
34 login_manager::kSessionManagerInterface); | |
35 } | |
36 | |
37 // Memory allocated by this method should be freed with delete, not delete []. | |
38 // static | |
39 uint8* ChromeOSLoginHelpers::NewBufferCopy(const uint8* x, int len) { | |
40 uint8* result = static_cast<uint8*>(::operator new(len)); | |
zel
2010/10/28 20:02:46
what's that magic cast here for? what's wrong with
Chris Masone
2010/10/28 20:09:08
To stay in line with what's done in NewStringCopy
| |
41 std::memcpy(result, x, len); | |
42 return result; | |
43 } | |
44 | |
45 // static | |
46 GArray* ChromeOSLoginHelpers::CreateGArrayFromBytes(const uint8* in, | |
47 const int in_len) { | |
48 GArray* ary = g_array_sized_new(FALSE, FALSE, 1, in_len); | |
49 g_array_append_vals(ary, in, in_len); | |
50 return ary; | |
51 } | |
52 | |
53 // static | |
54 bool ChromeOSLoginHelpers::CheckWhitelistHelper(const char* email, | |
55 GArray** sig) { | |
56 dbus::Proxy proxy = CreateProxy(); | |
57 glib::ScopedError error; | |
58 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
59 login_manager::kSessionManagerCheckWhitelist, | |
60 &Resetter(&error).lvalue(), | |
61 G_TYPE_STRING, email, | |
62 G_TYPE_INVALID, | |
63 DBUS_TYPE_G_UCHAR_ARRAY, sig, | |
64 G_TYPE_INVALID)) { | |
65 LOG(WARNING) << login_manager::kSessionManagerCheckWhitelist << " failed: " | |
66 << SCOPED_SAFE_MESSAGE(error); | |
67 return false; | |
68 } | |
69 return true; | |
70 } | |
71 | |
72 // static | |
73 bool ChromeOSLoginHelpers::EnumerateWhitelistedHelper(gchar*** whitelisted) { | |
74 dbus::Proxy proxy = ChromeOSLoginHelpers::CreateProxy(); | |
75 glib::ScopedError error; | |
76 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
77 login_manager::kSessionManagerEnumerateWhitelisted, | |
78 &Resetter(&error).lvalue(), | |
79 G_TYPE_INVALID, | |
80 G_TYPE_STRV, whitelisted, | |
81 G_TYPE_INVALID)) { | |
82 LOG(WARNING) << login_manager::kSessionManagerEnumerateWhitelisted | |
83 << " failed: " << SCOPED_SAFE_MESSAGE(error); | |
84 return false; | |
85 } | |
86 return true; | |
87 } | |
88 | |
89 // static | |
90 bool ChromeOSLoginHelpers::RetrievePropertyHelper(const char* name, | |
91 gchar** value, | |
92 GArray** sig) { | |
93 dbus::Proxy proxy = ChromeOSLoginHelpers::CreateProxy(); | |
94 glib::ScopedError error; | |
95 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
96 login_manager::kSessionManagerRetrieveProperty, | |
97 &Resetter(&error).lvalue(), | |
98 G_TYPE_STRING, name, | |
99 G_TYPE_INVALID, | |
100 G_TYPE_STRING, value, | |
101 DBUS_TYPE_G_UCHAR_ARRAY, sig, | |
102 G_TYPE_INVALID)) { | |
103 LOG(WARNING) << login_manager::kSessionManagerRetrieveProperty | |
104 << " failed: " << SCOPED_SAFE_MESSAGE(error); | |
105 return false; | |
106 } | |
107 return true; | |
108 } | |
109 | |
110 // static | |
111 bool ChromeOSLoginHelpers::SetOwnerKeyHelper(GArray* key_der) { | |
112 dbus::Proxy proxy = ChromeOSLoginHelpers::CreateProxy(); | |
113 glib::ScopedError error; | |
114 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
115 login_manager::kSessionManagerSetOwnerKey, | |
116 &Resetter(&error).lvalue(), | |
117 DBUS_TYPE_G_UCHAR_ARRAY, key_der, | |
118 G_TYPE_INVALID, | |
119 G_TYPE_INVALID)) { | |
120 LOG(WARNING) << login_manager::kSessionManagerSetOwnerKey << " failed: " | |
121 << SCOPED_SAFE_MESSAGE(error); | |
122 return false; | |
123 } | |
124 return true; | |
125 } | |
126 | |
127 // static | |
128 bool ChromeOSLoginHelpers::StorePropertyHelper(const char* name, | |
129 const char* value, | |
130 GArray* sig) { | |
131 dbus::Proxy proxy = ChromeOSLoginHelpers::CreateProxy(); | |
132 glib::ScopedError error; | |
133 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
134 login_manager::kSessionManagerStoreProperty, | |
135 &Resetter(&error).lvalue(), | |
136 G_TYPE_STRING, name, | |
137 G_TYPE_STRING, value, | |
138 DBUS_TYPE_G_UCHAR_ARRAY, sig, | |
139 G_TYPE_INVALID, | |
140 G_TYPE_INVALID)) { | |
141 LOG(WARNING) << login_manager::kSessionManagerStoreProperty << " failed: " | |
142 << SCOPED_SAFE_MESSAGE(error); | |
143 return false; | |
144 } | |
145 return true; | |
146 } | |
147 | |
148 // static | |
149 bool ChromeOSLoginHelpers::WhitelistOpHelper( | |
150 const char* op, | |
151 const char* email, | |
152 const std::vector<uint8>& signature) { | |
153 dbus::Proxy proxy = ChromeOSLoginHelpers::CreateProxy(); | |
154 glib::ScopedError error; | |
155 GArray* sig = CreateGArrayFromBytes(&signature[0], signature.size()); | |
156 | |
157 bool rv = true; | |
158 if (!::dbus_g_proxy_call(proxy.gproxy(), | |
159 op, | |
160 &Resetter(&error).lvalue(), | |
161 G_TYPE_STRING, email, | |
162 DBUS_TYPE_G_UCHAR_ARRAY, sig, | |
163 G_TYPE_INVALID, | |
164 G_TYPE_INVALID)) { | |
165 LOG(WARNING) << op << " failed: " << SCOPED_SAFE_MESSAGE(error); | |
166 rv = false; | |
167 } | |
168 g_array_free(sig, TRUE); | |
169 return rv; | |
170 } | |
171 | |
172 // static | |
173 CryptoBlob* ChromeOSLoginHelpers::CreateCryptoBlob(GArray* in) { | |
174 CryptoBlob* blob = new CryptoBlob; | |
175 blob->length = in->len; | |
176 blob->data = ChromeOSLoginHelpers::NewBufferCopy( | |
177 reinterpret_cast<const uint8*>(in->data), in->len); | |
178 return blob; | |
179 } | |
180 | |
181 // static | |
182 Property* ChromeOSLoginHelpers::CreateProperty(const char* name, | |
183 const gchar* value, | |
184 GArray* sig) { | |
185 Property* prop = new Property; | |
186 prop->signature = CreateCryptoBlob(sig); | |
187 prop->name = NewStringCopy(name); | |
188 prop->value = NewStringCopy(value); | |
189 return prop; | |
190 } | |
191 | |
192 // static | |
193 UserList* ChromeOSLoginHelpers::CreateUserList(const char* const* users) { | |
194 UserList* list = new UserList; | |
195 list->users = NULL; | |
196 for (list->num_users = 0; users[list->num_users] != NULL; list->num_users++) | |
197 ; | |
198 list->users = static_cast<const char**>( | |
199 ::operator new(sizeof(char*) * (list->num_users + 1))); | |
200 list->users[list->num_users] = NULL; | |
201 for (int i = 0; users[i] != NULL; ++i) | |
202 list->users[i] = NewStringCopy(users[i]); | |
203 return list; | |
204 } | |
205 | |
206 // These Free* methods all use delete (as opposed to delete []) on purpose, | |
207 // following the pattern established by code that uses NewStringCopy. | |
208 // static | |
209 void ChromeOSLoginHelpers::FreeCryptoBlob(CryptoBlob* blob) { | |
210 delete blob->data; | |
211 delete blob; | |
212 } | |
213 | |
214 // static | |
215 void ChromeOSLoginHelpers::FreeProperty(Property* property) { | |
216 FreeCryptoBlob(property->signature); | |
217 delete property->name; | |
218 delete property->value; | |
219 } | |
220 | |
221 // static | |
222 void ChromeOSLoginHelpers::FreeUserList(UserList* userlist) { | |
223 for (int i = 0; i < userlist->num_users; ++i) | |
224 delete userlist->users[i]; | |
225 delete userlist->users; | |
226 delete userlist; | |
227 } | |
228 } // namespace chromeos | |
OLD | NEW |