1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/browser_policy_connector.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/file_path.h"
#include "base/path_service.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/cloud_policy_client.h"
#include "chrome/browser/policy/cloud_policy_provider.h"
#include "chrome/browser/policy/cloud_policy_service.h"
#include "chrome/browser/policy/cloud_policy_subsystem.h"
#include "chrome/browser/policy/configuration_policy_provider.h"
#include "chrome/browser/policy/device_management_service.h"
#include "chrome/browser/policy/policy_service_impl.h"
#include "chrome/browser/policy/user_cloud_policy_manager.h"
#include "chrome/browser/policy/user_policy_cache.h"
#include "chrome/browser/policy/user_policy_token_cache.h"
#include "chrome/browser/signin/token_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/gaia/gaia_constants.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "grit/generated_resources.h"
#include "policy/policy_constants.h"
#if defined(OS_WIN)
#include "chrome/browser/policy/configuration_policy_provider_win.h"
#elif defined(OS_MACOSX)
#include "chrome/browser/policy/configuration_policy_provider_mac.h"
#elif defined(OS_POSIX)
#include "chrome/browser/policy/config_dir_policy_provider.h"
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/system/statistics_provider.h"
#include "chrome/browser/policy/app_pack_updater.h"
#include "chrome/browser/policy/cros_user_policy_cache.h"
#include "chrome/browser/policy/device_policy_cache.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#endif
using content::BrowserThread;
namespace policy {
namespace {
// Subdirectory in the user's profile for storing user policies.
const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
// File in the above directory for stroing user policy dmtokens.
const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
// File in the above directory for storing user policy data.
const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
// The following constants define delays applied before the initial policy fetch
// on startup. (So that displaying Chrome's GUI does not get delayed.)
// Delay in milliseconds from startup.
const int64 kServiceInitializationStartupDelay = 5000;
#if defined(OS_CHROMEOS)
// MachineInfo key names.
const char kMachineInfoSystemHwqual[] = "hardware_class";
// These are the machine serial number keys that we check in order until we
// find a non-empty serial number. The VPD spec says the serial number should be
// in the "serial_number" key for v2+ VPDs. However, we cannot check this first,
// since we'd get the "serial_number" value from the SMBIOS (yes, there's a name
// clash here!), which is different from the serial number we want and not
// actually per-device. So, we check the legacy keys first. If we find a
// serial number for these, we use it, otherwise we must be on a newer device
// that provides the correct data in "serial_number".
const char* kMachineInfoSerialNumberKeys[] = {
"sn", // ZGB
"Product_S/N", // Alex
"Product_SN", // Mario
"serial_number" // VPD v2+ devices
};
#endif
} // namespace
BrowserPolicyConnector::BrowserPolicyConnector()
: ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {}
BrowserPolicyConnector::~BrowserPolicyConnector() {
// Shutdown device cloud policy.
#if defined(OS_CHROMEOS)
if (device_cloud_policy_subsystem_.get())
device_cloud_policy_subsystem_->Shutdown();
// The AppPackUpdater may be observing the |device_cloud_policy_subsystem_|.
// Delete it first.
app_pack_updater_.reset();
device_cloud_policy_subsystem_.reset();
device_data_store_.reset();
#endif
// Shutdown user cloud policy.
if (user_cloud_policy_subsystem_.get())
user_cloud_policy_subsystem_->Shutdown();
user_cloud_policy_subsystem_.reset();
user_policy_token_cache_.reset();
user_data_store_.reset();
if (user_cloud_policy_manager_.get())
user_cloud_policy_manager_->Shutdown();
user_cloud_policy_manager_.reset();
device_management_service_.reset();
}
void BrowserPolicyConnector::Init() {
managed_platform_provider_.reset(CreateManagedPlatformProvider());
recommended_platform_provider_.reset(CreateRecommendedPlatformProvider());
#if defined(OS_CHROMEOS)
// The CloudPolicyProvider blocks asynchronous Profile creation until a login
// is performed. This is used to ensure that the Profile's PrefService sees
// managed preferences on managed Chrome OS devices. However, this also
// prevents creation of new Profiles in Desktop Chrome. The implementation of
// cloud policy on the Desktop requires a refactoring of the cloud provider,
// but for now it just isn't created.
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kDeviceManagementUrl)) {
device_management_service_.reset(
new DeviceManagementService(
command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl)));
if (!command_line->HasSwitch(switches::kEnableCloudPolicyService)) {
managed_cloud_provider_.reset(new CloudPolicyProvider(
this,
GetChromePolicyDefinitionList(),
POLICY_LEVEL_MANDATORY));
recommended_cloud_provider_.reset(new CloudPolicyProvider(
this,
GetChromePolicyDefinitionList(),
POLICY_LEVEL_RECOMMENDED));
}
}
InitializeDevicePolicy();
// Create the AppPackUpdater to start updating the cache. It requires the
// system request context, which isn't available yet; therefore it is
// created only once the loops are running.
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(base::IgnoreResult(&BrowserPolicyConnector::GetAppPackUpdater),
weak_ptr_factory_.GetWeakPtr()));
#endif
}
PolicyService* BrowserPolicyConnector::CreatePolicyService(
Profile* profile) {
// |providers| in decreasing order of priority.
PolicyServiceImpl::Providers providers;
if (managed_platform_provider_.get())
providers.push_back(managed_platform_provider_.get());
if (managed_cloud_provider_.get())
providers.push_back(managed_cloud_provider_.get());
if (recommended_platform_provider_.get())
providers.push_back(recommended_platform_provider_.get());
if (recommended_cloud_provider_.get())
providers.push_back(recommended_cloud_provider_.get());
// The global policy service uses the proxy provider to allow for swapping in
// user policy after startup, while profiles use |user_cloud_policy_manager_|
// directly as their provider, which may also block initialization on a policy
// fetch at login time.
if (profile) {
if (user_cloud_policy_manager_.get())
providers.push_back(user_cloud_policy_manager_.get());
} else {
providers.push_back(&user_cloud_policy_provider_);
}
return new PolicyServiceImpl(providers);
}
void BrowserPolicyConnector::RegisterForDevicePolicy(
const std::string& owner_email,
const std::string& token,
bool known_machine_id) {
#if defined(OS_CHROMEOS)
if (device_data_store_.get()) {
if (!device_data_store_->device_token().empty()) {
LOG(ERROR) << "Device policy data store already has a DMToken; "
<< "RegisterForDevicePolicy won't trigger a new registration.";
}
device_data_store_->set_user_name(owner_email);
device_data_store_->set_known_machine_id(known_machine_id);
device_data_store_->set_policy_fetching_enabled(false);
device_data_store_->SetOAuthToken(token);
}
#endif
}
bool BrowserPolicyConnector::IsEnterpriseManaged() {
#if defined(OS_CHROMEOS)
return install_attributes_.get() && install_attributes_->IsEnterpriseDevice();
#else
return false;
#endif
}
EnterpriseInstallAttributes::LockResult
BrowserPolicyConnector::LockDevice(const std::string& user) {
#if defined(OS_CHROMEOS)
if (install_attributes_.get()) {
return install_attributes_->LockDevice(user,
device_data_store_->device_mode(),
device_data_store_->device_id());
}
#endif
return EnterpriseInstallAttributes::LOCK_BACKEND_ERROR;
}
// static
std::string BrowserPolicyConnector::GetSerialNumber() {
std::string serial_number;
#if defined(OS_CHROMEOS)
chromeos::system::StatisticsProvider* provider =
chromeos::system::StatisticsProvider::GetInstance();
for (size_t i = 0; i < arraysize(kMachineInfoSerialNumberKeys); i++) {
if (provider->GetMachineStatistic(kMachineInfoSerialNumberKeys[i],
&serial_number) &&
!serial_number.empty()) {
break;
}
}
#endif
return serial_number;
}
std::string BrowserPolicyConnector::GetEnterpriseDomain() {
#if defined(OS_CHROMEOS)
if (install_attributes_.get())
return install_attributes_->GetDomain();
#endif
return std::string();
}
DeviceMode BrowserPolicyConnector::GetDeviceMode() {
#if defined(OS_CHROMEOS)
if (install_attributes_.get())
return install_attributes_->GetMode();
else
return DEVICE_MODE_NOT_SET;
#endif
// We only have the notion of "enterprise" device on ChromeOS for now.
return DEVICE_MODE_CONSUMER;
}
void BrowserPolicyConnector::ResetDevicePolicy() {
#if defined(OS_CHROMEOS)
if (device_cloud_policy_subsystem_.get())
device_cloud_policy_subsystem_->Reset();
#endif
}
void BrowserPolicyConnector::FetchCloudPolicy() {
#if defined(OS_CHROMEOS)
if (device_cloud_policy_subsystem_.get())
device_cloud_policy_subsystem_->RefreshPolicies(false);
if (user_cloud_policy_subsystem_.get())
user_cloud_policy_subsystem_->RefreshPolicies(true); // wait_for_auth_token
#endif
}
void BrowserPolicyConnector::ScheduleServiceInitialization(
int64 delay_milliseconds) {
if (device_management_service_.get())
device_management_service_->ScheduleInitialization(delay_milliseconds);
if (user_cloud_policy_subsystem_.get()) {
user_cloud_policy_subsystem_->
ScheduleServiceInitialization(delay_milliseconds);
}
#if defined(OS_CHROMEOS)
if (device_cloud_policy_subsystem_.get()) {
device_cloud_policy_subsystem_->
ScheduleServiceInitialization(delay_milliseconds);
}
#endif
}
void BrowserPolicyConnector::InitializeUserPolicy(
const std::string& user_name,
bool wait_for_policy_fetch) {
// Throw away the old backend.
user_cloud_policy_manager_.reset();
user_cloud_policy_subsystem_.reset();
user_policy_token_cache_.reset();
user_data_store_.reset();
token_service_ = NULL;
registrar_.RemoveAll();
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kDeviceManagementUrl)) {
int64 startup_delay =
wait_for_policy_fetch ? 0 : kServiceInitializationStartupDelay;
if (command_line->HasSwitch(switches::kEnableCloudPolicyService)) {
#if defined(OS_CHROMEOS)
user_cloud_policy_manager_ =
UserCloudPolicyManager::Create(wait_for_policy_fetch);
user_cloud_policy_manager_->Initialize(g_browser_process->local_state(),
device_management_service_.get(),
GetUserAffiliation(user_name));
user_cloud_policy_provider_.SetDelegate(user_cloud_policy_manager_.get());
device_management_service_->ScheduleInitialization(startup_delay);
#endif
} else {
FilePath profile_dir;
PathService::Get(chrome::DIR_USER_DATA, &profile_dir);
#if defined(OS_CHROMEOS)
profile_dir = profile_dir.Append(
command_line->GetSwitchValuePath(switches::kLoginProfile));
#endif
const FilePath policy_dir = profile_dir.Append(kPolicyDir);
const FilePath policy_cache_file = policy_dir.Append(kPolicyCacheFile);
const FilePath token_cache_file = policy_dir.Append(kTokenCacheFile);
CloudPolicyCacheBase* user_policy_cache = NULL;
user_data_store_.reset(CloudPolicyDataStore::CreateForUserPolicies());
#if defined(OS_CHROMEOS)
user_policy_cache =
new CrosUserPolicyCache(
chromeos::DBusThreadManager::Get()->GetSessionManagerClient(),
user_data_store_.get(),
wait_for_policy_fetch,
token_cache_file,
policy_cache_file);
#else
user_policy_cache = new UserPolicyCache(policy_cache_file,
wait_for_policy_fetch);
user_policy_token_cache_.reset(
new UserPolicyTokenCache(user_data_store_.get(), token_cache_file));
// Initiate the DM-Token load.
user_policy_token_cache_->Load();
#endif
user_cloud_policy_subsystem_.reset(new CloudPolicySubsystem(
user_data_store_.get(),
user_policy_cache));
user_data_store_->set_user_name(user_name);
user_data_store_->set_user_affiliation(GetUserAffiliation(user_name));
user_cloud_policy_subsystem_->CompleteInitialization(
prefs::kUserPolicyRefreshRate,
startup_delay);
managed_cloud_provider_->SetUserPolicyCache(user_policy_cache);
recommended_cloud_provider_->SetUserPolicyCache(user_policy_cache);
}
}
}
void BrowserPolicyConnector::SetUserPolicyTokenService(
TokenService* token_service) {
token_service_ = token_service;
registrar_.Add(this,
chrome::NOTIFICATION_TOKEN_AVAILABLE,
content::Source<TokenService>(token_service_));
if (token_service_->HasTokenForService(
GaiaConstants::kDeviceManagementService)) {
user_data_store_->SetGaiaToken(token_service_->GetTokenForService(
GaiaConstants::kDeviceManagementService));
}
}
void BrowserPolicyConnector::RegisterForUserPolicy(
const std::string& oauth_token) {
if (oauth_token.empty()) {
// An attempt to fetch the dm service oauth token has failed. Notify
// the user policy cache of this, so that a potential blocked login
// proceeds without waiting for user policy.
if (user_cloud_policy_subsystem_.get()) {
user_cloud_policy_subsystem_->GetCloudPolicyCacheBase()->
SetFetchingDone();
}
} else {
if (user_data_store_.get())
user_data_store_->SetOAuthToken(oauth_token);
}
if (user_cloud_policy_manager_.get()) {
CloudPolicyService* service =
user_cloud_policy_manager_->cloud_policy_service();
if (service->client() &&
!service->client()->is_registered() &&
!oauth_token.empty()) {
service->client()->Register(oauth_token);
} else {
user_cloud_policy_manager_->CancelWaitForPolicyFetch();
}
}
}
CloudPolicyDataStore* BrowserPolicyConnector::GetDeviceCloudPolicyDataStore() {
#if defined(OS_CHROMEOS)
return device_data_store_.get();
#else
return NULL;
#endif
}
CloudPolicyDataStore* BrowserPolicyConnector::GetUserCloudPolicyDataStore() {
return user_data_store_.get();
}
const ConfigurationPolicyHandlerList*
BrowserPolicyConnector::GetHandlerList() const {
return &handler_list_;
}
UserAffiliation BrowserPolicyConnector::GetUserAffiliation(
const std::string& user_name) {
#if defined(OS_CHROMEOS)
if (install_attributes_.get()) {
size_t pos = user_name.find('@');
if (pos != std::string::npos &&
user_name.substr(pos + 1) == install_attributes_->GetDomain()) {
return USER_AFFILIATION_MANAGED;
}
}
#endif
return USER_AFFILIATION_NONE;
}
AppPackUpdater* BrowserPolicyConnector::GetAppPackUpdater() {
#if defined(OS_CHROMEOS)
if (!app_pack_updater_.get()) {
// system_request_context() is NULL in unit tests.
net::URLRequestContextGetter* request_context =
g_browser_process->system_request_context();
if (request_context)
app_pack_updater_.reset(new AppPackUpdater(request_context, this));
}
return app_pack_updater_.get();
#else
return NULL;
#endif
}
void BrowserPolicyConnector::Observe(
int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (type == chrome::NOTIFICATION_TOKEN_AVAILABLE) {
const TokenService* token_source =
content::Source<const TokenService>(source).ptr();
DCHECK_EQ(token_service_, token_source);
const TokenService::TokenAvailableDetails* token_details =
content::Details<const TokenService::TokenAvailableDetails>(details).
ptr();
if (token_details->service() == GaiaConstants::kDeviceManagementService) {
if (user_data_store_.get()) {
user_data_store_->SetGaiaToken(token_details->token());
}
}
} else {
NOTREACHED();
}
}
void BrowserPolicyConnector::InitializeDevicePolicy() {
#if defined(OS_CHROMEOS)
// Throw away the old backend.
device_cloud_policy_subsystem_.reset();
device_data_store_.reset();
CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kEnableDevicePolicy)) {
if (command_line->HasSwitch(switches::kEnableCloudPolicyService)) {
// TODO(mnissler): Initialize new-style device policy here once it's
// implemented.
} else {
device_data_store_.reset(CloudPolicyDataStore::CreateForDevicePolicies());
chromeos::CryptohomeLibrary* cryptohome =
chromeos::CrosLibrary::Get()->GetCryptohomeLibrary();
install_attributes_.reset(new EnterpriseInstallAttributes(cryptohome));
DevicePolicyCache* device_policy_cache =
new DevicePolicyCache(device_data_store_.get(),
install_attributes_.get());
managed_cloud_provider_->SetDevicePolicyCache(device_policy_cache);
recommended_cloud_provider_->SetDevicePolicyCache(device_policy_cache);
device_cloud_policy_subsystem_.reset(new CloudPolicySubsystem(
device_data_store_.get(),
device_policy_cache));
// Initialize the subsystem once the message loops are spinning.
MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&BrowserPolicyConnector::CompleteInitialization,
weak_ptr_factory_.GetWeakPtr()));
}
}
#endif
}
void BrowserPolicyConnector::CompleteInitialization() {
#if defined(OS_CHROMEOS)
if (device_cloud_policy_subsystem_.get()) {
// Read serial number and machine model. This must be done before we call
// CompleteInitialization() below such that the serial number is available
// for re-submission in case we're doing serial number recovery.
if (device_data_store_->machine_id().empty() ||
device_data_store_->machine_model().empty()) {
chromeos::system::StatisticsProvider* provider =
chromeos::system::StatisticsProvider::GetInstance();
std::string machine_model;
if (!provider->GetMachineStatistic(kMachineInfoSystemHwqual,
&machine_model)) {
LOG(ERROR) << "Failed to get machine model.";
}
std::string machine_id = GetSerialNumber();
if (machine_id.empty())
LOG(ERROR) << "Failed to get machine serial number.";
device_data_store_->set_machine_id(machine_id);
device_data_store_->set_machine_model(machine_model);
}
device_cloud_policy_subsystem_->CompleteInitialization(
prefs::kDevicePolicyRefreshRate,
kServiceInitializationStartupDelay);
}
device_data_store_->set_device_status_collector(
new DeviceStatusCollector(
g_browser_process->local_state(),
chromeos::system::StatisticsProvider::GetInstance(),
NULL));
#endif
}
// static
ConfigurationPolicyProvider*
BrowserPolicyConnector::CreateManagedPlatformProvider() {
const PolicyDefinitionList* policy_list = GetChromePolicyDefinitionList();
#if defined(OS_WIN)
return new ConfigurationPolicyProviderWin(policy_list);
#elif defined(OS_MACOSX)
return new ConfigurationPolicyProviderMac(policy_list);
#elif defined(OS_POSIX)
FilePath config_dir_path;
if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
return new ConfigDirPolicyProvider(
policy_list,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_MACHINE,
config_dir_path.Append(FILE_PATH_LITERAL("managed")));
} else {
return NULL;
}
#else
return NULL;
#endif
}
// static
ConfigurationPolicyProvider*
BrowserPolicyConnector::CreateRecommendedPlatformProvider() {
#if defined(OS_POSIX) && !defined(OS_MACOSX)
const PolicyDefinitionList* policy_list = GetChromePolicyDefinitionList();
FilePath config_dir_path;
if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) {
return new ConfigDirPolicyProvider(
policy_list,
POLICY_LEVEL_RECOMMENDED,
POLICY_SCOPE_MACHINE,
config_dir_path.Append(FILE_PATH_LITERAL("recommended")));
} else {
return NULL;
}
#else
return NULL;
#endif
}
} // namespace policy
|