OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/host/sas_injector.h" | 5 #include "remoting/host/sas_injector.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <sas.h> | |
8 | 9 |
9 #include <string> | |
10 | |
11 #include "base/files/file_path.h" | |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "base/macros.h" | 11 #include "base/macros.h" |
14 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
15 #include "base/path_service.h" | |
16 #include "base/scoped_native_library.h" | |
17 #include "base/strings/utf_string_conversions.h" | |
18 #include "base/win/registry.h" | 13 #include "base/win/registry.h" |
19 #include "base/win/windows_version.h" | |
20 #include "third_party/webrtc/modules/desktop_capture/win/desktop.h" | |
21 #include "third_party/webrtc/modules/desktop_capture/win/scoped_thread_desktop.h " | |
22 | 14 |
23 namespace remoting { | 15 namespace remoting { |
24 | 16 |
25 namespace { | 17 namespace { |
26 | 18 |
27 // Names of the API and library implementing software SAS generation. | |
28 const base::FilePath::CharType kSasDllFileName[] = FILE_PATH_LITERAL("sas.dll"); | |
29 const char kSendSasName[] = "SendSAS"; | |
30 | |
31 // The prototype of SendSAS(). | |
32 typedef VOID (WINAPI *SendSasFunc)(BOOL); | |
33 | |
34 // The registry key and value holding the policy controlling software SAS | 19 // The registry key and value holding the policy controlling software SAS |
35 // generation. | 20 // generation. |
36 const wchar_t kSystemPolicyKeyName[] = | 21 const wchar_t kSystemPolicyKeyName[] = |
37 L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"; | 22 L"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"; |
38 const wchar_t kSoftwareSasValueName[] = L"SoftwareSASGeneration"; | 23 const wchar_t kSoftwareSasValueName[] = L"SoftwareSASGeneration"; |
39 | 24 |
40 const DWORD kEnableSoftwareSasByServices = 1; | 25 const DWORD kEnableSoftwareSasByServices = 1; |
41 | 26 |
42 // Toggles the default software SAS generation policy to enable SAS generation | 27 // Toggles the default software SAS generation policy to enable SAS generation |
43 // by services. Non-default policy is not changed. | 28 // by services. Non-default policy is not changed. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 } else { | 83 } else { |
99 restore_policy_ = true; | 84 restore_policy_ = true; |
100 } | 85 } |
101 } | 86 } |
102 | 87 |
103 return true; | 88 return true; |
104 } | 89 } |
105 | 90 |
106 } // namespace | 91 } // namespace |
107 | 92 |
108 // Sends Secure Attention Sequence using the SendSAS() function from sas.dll. | 93 // Sends Secure Attention Sequence using the SendSAS() method. Checks the |
Jamie
2016/06/03 17:49:31
I'm not sure that mentioning the API in the commen
joedow
2016/06/03 18:11:02
Done.
| |
109 // This library is shipped starting from Win7/W2K8 R2 only. However Win7 SDK | 94 // current policy before attempting to send the sequence. |
110 // includes a redistributable verion of the same library that works on | |
111 // Vista/W2K8. We install the latter along with our binaries. | |
112 class SasInjectorWin : public SasInjector { | 95 class SasInjectorWin : public SasInjector { |
113 public: | 96 public: |
114 SasInjectorWin(); | 97 SasInjectorWin(); |
115 ~SasInjectorWin() override; | 98 ~SasInjectorWin() override; |
116 | 99 |
117 // SasInjector implementation. | 100 // SasInjector implementation. |
118 bool InjectSas() override; | 101 bool InjectSas() override; |
119 | 102 |
120 private: | 103 private: |
121 base::ScopedNativeLibrary sas_dll_; | 104 DISALLOW_COPY_AND_ASSIGN(SasInjectorWin); |
122 SendSasFunc send_sas_; | |
123 }; | 105 }; |
124 | 106 |
125 // Emulates Secure Attention Sequence (Ctrl+Alt+Del) by switching to | 107 SasInjectorWin::SasInjectorWin() {} |
126 // the Winlogon desktop and injecting Ctrl+Alt+Del as a hot key. | |
127 // N.B. Windows XP/W2K3 only. | |
128 class SasInjectorXp : public SasInjector { | |
129 public: | |
130 SasInjectorXp(); | |
131 ~SasInjectorXp() override; | |
132 | |
133 // SasInjector implementation. | |
134 bool InjectSas() override; | |
135 }; | |
136 | |
137 SasInjectorWin::SasInjectorWin() : send_sas_(nullptr) { | |
138 } | |
139 | 108 |
140 SasInjectorWin::~SasInjectorWin() { | 109 SasInjectorWin::~SasInjectorWin() { |
141 } | 110 } |
142 | 111 |
143 bool SasInjectorWin::InjectSas() { | 112 bool SasInjectorWin::InjectSas() { |
144 // Load sas.dll. The library is expected to be in the same folder as this | 113 // Enable software SAS generation by services and send SAS. SAS can still fail |
145 // binary. | 114 // if the policy does not allow services to generate software SAS. |
146 if (!sas_dll_.is_valid()) { | 115 ScopedSoftwareSasPolicy enable_sas; |
147 base::FilePath dir_path; | 116 if (!enable_sas.Apply()) { |
148 if (!PathService::Get(base::DIR_EXE, &dir_path)) { | |
149 LOG(ERROR) << "Failed to get the executable file name."; | |
150 return false; | |
151 } | |
152 | |
153 sas_dll_.Reset(base::LoadNativeLibrary(dir_path.Append(kSasDllFileName), | |
154 nullptr)); | |
155 } | |
156 if (!sas_dll_.is_valid()) { | |
157 LOG(ERROR) << "Failed to load '" << kSasDllFileName << "'"; | |
158 return false; | 117 return false; |
159 } | 118 } |
160 | 119 |
161 // Get the pointer to sas!SendSAS(). | 120 SendSAS(/*AsUser=*/FALSE); |
162 if (send_sas_ == nullptr) { | |
163 send_sas_ = reinterpret_cast<SendSasFunc>( | |
164 sas_dll_.GetFunctionPointer(kSendSasName)); | |
165 } | |
166 if (send_sas_ == nullptr) { | |
167 LOG(ERROR) << "Failed to retrieve the address of '" << kSendSasName | |
168 << "()'"; | |
169 return false; | |
170 } | |
171 | |
172 // Enable software SAS generation by services and send SAS. SAS can still fail | |
173 // if the policy does not allow services to generate software SAS. | |
174 ScopedSoftwareSasPolicy enable_sas; | |
175 if (!enable_sas.Apply()) | |
176 return false; | |
177 | |
178 (*send_sas_)(FALSE); | |
179 return true; | |
180 } | |
181 | |
182 SasInjectorXp::SasInjectorXp() { | |
183 } | |
184 | |
185 SasInjectorXp::~SasInjectorXp() { | |
186 } | |
187 | |
188 bool SasInjectorXp::InjectSas() { | |
189 const wchar_t kWinlogonDesktopName[] = L"Winlogon"; | |
190 const wchar_t kSasWindowClassName[] = L"SAS window class"; | |
191 const wchar_t kSasWindowTitle[] = L"SAS window"; | |
192 | |
193 std::unique_ptr<webrtc::Desktop> winlogon_desktop( | |
194 webrtc::Desktop::GetDesktop(kWinlogonDesktopName)); | |
195 if (!winlogon_desktop.get()) { | |
196 PLOG(ERROR) << "Failed to open '" << kWinlogonDesktopName << "' desktop"; | |
197 return false; | |
198 } | |
199 | |
200 webrtc::ScopedThreadDesktop desktop; | |
201 if (!desktop.SetThreadDesktop(winlogon_desktop.release())) { | |
202 PLOG(ERROR) << "Failed to switch to '" << kWinlogonDesktopName | |
203 << "' desktop"; | |
204 return false; | |
205 } | |
206 | |
207 HWND window = FindWindow(kSasWindowClassName, kSasWindowTitle); | |
208 if (!window) { | |
209 PLOG(ERROR) << "Failed to find '" << kSasWindowTitle << "' window"; | |
210 return false; | |
211 } | |
212 | |
213 if (PostMessage(window, | |
214 WM_HOTKEY, | |
215 0, | |
216 MAKELONG(MOD_ALT | MOD_CONTROL, VK_DELETE)) == 0) { | |
217 PLOG(ERROR) << "Failed to post WM_HOTKEY message"; | |
218 return false; | |
219 } | |
220 | |
221 return true; | 121 return true; |
222 } | 122 } |
223 | 123 |
224 std::unique_ptr<SasInjector> SasInjector::Create() { | 124 std::unique_ptr<SasInjector> SasInjector::Create() { |
225 if (base::win::GetVersion() < base::win::VERSION_VISTA) { | 125 return base::WrapUnique(new SasInjectorWin()); |
226 return base::WrapUnique(new SasInjectorXp()); | |
227 } else { | |
228 return base::WrapUnique(new SasInjectorWin()); | |
229 } | |
230 } | 126 } |
231 | 127 |
232 } // namespace remoting | 128 } // namespace remoting |
OLD | NEW |