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
|
// Copyright (c) 2011 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.
// Most of this code is copied from various classes in
// src/chrome/browser/policy. In particular, look at
//
// configuration_policy_provider_delegate_win.{h,cc}
// configuration_policy_loader_win.{h,cc}
//
// This is a reduction of the functionality in those classes.
#include "remoting/host/plugin/policy_hack/nat_policy.h"
#include <userenv.h>
#include "base/compiler_specific.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/message_loop_proxy.h"
#include "base/scoped_ptr.h"
#include "base/synchronization/waitable_event.h"
#include "base/values.h"
#include "base/win/object_watcher.h"
// userenv.dll is required for RegisterGPNotification().
#pragma comment(lib, "userenv.lib")
using base::win::RegKey;
namespace remoting {
namespace policy_hack {
namespace {
#if defined(OS_WIN)
#if defined(GOOGLE_CHROME_BUILD)
const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Google\\Chrome";
#else
const wchar_t kRegistrySubKey[] = L"SOFTWARE\\Policies\\Chromium";
#endif
#endif
} // namespace
class NatPolicyWin :
public NatPolicy,
public base::win::ObjectWatcher::Delegate {
public:
explicit NatPolicyWin(base::MessageLoopProxy* message_loop_proxy)
: NatPolicy(message_loop_proxy),
user_policy_changed_event_(false, false),
machine_policy_changed_event_(false, false),
user_policy_watcher_failed_(false),
machine_policy_watcher_failed_(false) {
}
virtual ~NatPolicyWin() {
}
virtual void StartWatchingInternal() OVERRIDE {
DCHECK(OnPolicyThread());
if (!RegisterGPNotification(user_policy_changed_event_.handle(), false)) {
PLOG(WARNING) << "Failed to register user group policy notification";
user_policy_watcher_failed_ = true;
}
if (!RegisterGPNotification(machine_policy_changed_event_.handle(), true)) {
PLOG(WARNING) << "Failed to register machine group policy notification.";
machine_policy_watcher_failed_ = true;
}
Reload();
}
virtual void StopWatchingInternal() OVERRIDE {
DCHECK(OnPolicyThread());
if (!UnregisterGPNotification(user_policy_changed_event_.handle())) {
PLOG(WARNING) << "Failed to unregister user group policy notification";
}
if (!UnregisterGPNotification(machine_policy_changed_event_.handle())) {
PLOG(WARNING) <<
"Failed to unregister machine group policy notification.";
}
user_policy_watcher_.StopWatching();
machine_policy_watcher_.StopWatching();
}
private:
// Updates the watchers and schedules the reload task if appropriate.
void SetupWatches() {
DCHECK(OnPolicyThread());
if (!user_policy_watcher_failed_ &&
!user_policy_watcher_.GetWatchedObject() &&
!user_policy_watcher_.StartWatching(
user_policy_changed_event_.handle(), this)) {
LOG(WARNING) << "Failed to start watch for user policy change event";
user_policy_watcher_failed_ = true;
}
if (!machine_policy_watcher_failed_ &&
!machine_policy_watcher_.GetWatchedObject() &&
!machine_policy_watcher_.StartWatching(
machine_policy_changed_event_.handle(), this)) {
LOG(WARNING) << "Failed to start watch for machine policy change event";
machine_policy_watcher_failed_ = true;
}
if (user_policy_watcher_failed_ || machine_policy_watcher_failed_) {
ScheduleFallbackReloadTask();
}
}
bool GetRegistryPolicyInteger(const string16& value_name,
uint32* result) const {
DWORD value = 0;
RegKey policy_key(HKEY_LOCAL_MACHINE, kRegistrySubKey, KEY_READ);
if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) {
*result = value;
return true;
}
if (policy_key.Open(HKEY_CURRENT_USER, kRegistrySubKey, KEY_READ) ==
ERROR_SUCCESS) {
if (policy_key.ReadValueDW(value_name.c_str(), &value) == ERROR_SUCCESS) {
*result = value;
return true;
}
}
return false;
}
bool GetRegistryPolicyBoolean(const string16& value_name,
bool* result) const {
uint32 local_result = 0;
bool ret = GetRegistryPolicyInteger(value_name, &local_result);
if (ret)
*result = local_result != 0;
return ret;
}
base::DictionaryValue* Load() {
base::DictionaryValue* policy = new base::DictionaryValue();
bool bool_value;
const string16 name(ASCIIToUTF16(kNatPolicyName));
if (GetRegistryPolicyBoolean(name, &bool_value)) {
policy->SetBoolean(kNatPolicyName, bool_value);
}
return policy;
}
// Post a reload notification and update the watch machinery.
void Reload() {
DCHECK(OnPolicyThread());
SetupWatches();
scoped_ptr<DictionaryValue> new_policy(Load());
UpdateNatPolicy(new_policy.get());
}
// ObjectWatcher::Delegate overrides:
virtual void OnObjectSignaled(HANDLE object) {
DCHECK(OnPolicyThread());
DCHECK(object == user_policy_changed_event_.handle() ||
object == machine_policy_changed_event_.handle())
<< "unexpected object signaled policy reload, obj = "
<< std::showbase << std::hex << object;
Reload();
}
base::WaitableEvent user_policy_changed_event_;
base::WaitableEvent machine_policy_changed_event_;
base::win::ObjectWatcher user_policy_watcher_;
base::win::ObjectWatcher machine_policy_watcher_;
bool user_policy_watcher_failed_;
bool machine_policy_watcher_failed_;
};
NatPolicy* NatPolicy::Create(base::MessageLoopProxy* message_loop_proxy) {
return new NatPolicyWin(message_loop_proxy);
}
} // namespace policy_hack
} // namespace remoting
|