OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "remoting/host/sas_sender_win.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/file_path.h" | |
11 #include "base/native_library.h" | |
12 #include "base/path_service.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "base/win/registry.h" | |
15 #include "base/win/windows_version.h" | |
16 | |
17 namespace remoting { | |
18 | |
19 namespace { | |
20 | |
21 // Names of the API and library implementing software SAS generation. | |
22 const FilePath::CharType kSasDllFileName[] = | |
23 FILE_PATH_LITERAL("sas.dll"); | |
24 const char kSendSasName[] = "SendSAS"; | |
25 | |
26 // The prototype of SendSAS(). | |
27 typedef VOID (WINAPI *SendSasFunc)(BOOL); | |
28 | |
29 // The registry key and value holding the policy controlling software SAS | |
30 // generation. | |
31 const char kSystemPolicyKeyName[] = | |
32 "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System"; | |
33 const char kSoftwareSasValueName[] = "SoftwareSASGeneration"; | |
34 | |
35 const DWORD kEnableSoftwareSasByServices = 1; | |
36 | |
37 // Toggles the default software SAS generation policy to enable SAS generation | |
38 // by services. Non-default policy is not channged. | |
39 class ScopedSoftwareSasPolicy { | |
40 public: | |
41 ScopedSoftwareSasPolicy(); | |
42 ~ScopedSoftwareSasPolicy(); | |
43 | |
44 bool Apply(); | |
45 | |
46 private: | |
47 // The handle of the registry key were SoftwareSASGeneration policy is stored. | |
48 base::win::RegKey system_policy_; | |
49 | |
50 // Name of the registry value holding the policy. | |
51 string16 value_name_; | |
52 | |
53 // True if the policy needs to be restored. | |
54 bool restore_policy_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(ScopedSoftwareSasPolicy); | |
57 }; | |
58 | |
59 ScopedSoftwareSasPolicy::ScopedSoftwareSasPolicy() | |
60 : restore_policy_(false) { | |
61 } | |
62 | |
63 ScopedSoftwareSasPolicy::~ScopedSoftwareSasPolicy() { | |
64 // Restore the default policy by deleting the value that we have set. | |
65 if (restore_policy_) { | |
66 LONG result = system_policy_.DeleteValue(value_name_.c_str()); | |
67 if (result != ERROR_SUCCESS) { | |
68 SetLastError(result); | |
69 LOG_GETLASTERROR(ERROR) | |
70 << "Failed to restore the software SAS generation policy"; | |
71 } | |
72 } | |
73 } | |
74 | |
75 bool ScopedSoftwareSasPolicy::Apply() { | |
76 // Query the currently set SoftwareSASGeneration policy. | |
77 LONG result = system_policy_.Open(HKEY_LOCAL_MACHINE, | |
78 ASCIIToUTF16(kSystemPolicyKeyName).c_str(), | |
79 KEY_QUERY_VALUE | KEY_SET_VALUE | | |
80 KEY_WOW64_64KEY); | |
81 if (result != ERROR_SUCCESS) { | |
82 SetLastError(result); | |
83 LOG_GETLASTERROR(ERROR) << "Failed to open 'HKLM\\" | |
84 << kSystemPolicyKeyName << "'"; | |
85 return false; | |
86 } | |
87 | |
88 value_name_ = ASCIIToUTF16(kSoftwareSasValueName); | |
89 bool custom_policy = system_policy_.HasValue(value_name_.c_str()); | |
90 | |
91 // Override the default policy (i.e. there is no value in the registry) only. | |
92 if (!custom_policy) { | |
93 result = system_policy_.WriteValue(value_name_.c_str(), | |
94 kEnableSoftwareSasByServices); | |
95 if (result != ERROR_SUCCESS) { | |
96 SetLastError(result); | |
97 LOG_GETLASTERROR(ERROR) | |
98 << "Failed to enable software SAS generation by services"; | |
99 return false; | |
100 } else { | |
101 restore_policy_ = true; | |
102 } | |
103 } | |
104 | |
105 return true; | |
106 } | |
107 | |
108 } // namespace | |
109 | |
110 // Sends the security attention sequence using the SendSAS() function from | |
111 // sas.dll. This library is shipped starting from Win7/W2K8 R2 only. However | |
112 // Win7 SDK includes a redistributable verion of the same library that works on | |
113 // Vista/W2K8. We install the latter along with our binaries. | |
114 class SasSenderVista : public SasSender { | |
Wez
2012/03/07 01:56:13
Why is this SasSanderVista rather than SasSenderWi
alexeypa (please no reviews)
2012/03/07 19:59:08
Because, potentially, we can have a different impl
Wez
2012/03/08 00:01:33
That's fine - this version is SasSenderWin, and on
alexeypa (please no reviews)
2012/03/08 01:52:54
Once we do that the naming will become odd: why is
| |
115 public: | |
116 SasSenderVista(); | |
117 virtual ~SasSenderVista(); | |
118 | |
119 // SasSender implementation. | |
120 virtual bool Send() OVERRIDE; | |
121 | |
122 private: | |
123 base::NativeLibrary sas_dll_; | |
124 SendSasFunc send_sas_; | |
125 }; | |
126 | |
127 SasSenderVista::SasSenderVista() : sas_dll_(NULL), send_sas_(NULL) { | |
128 } | |
129 | |
130 SasSenderVista::~SasSenderVista() { | |
131 if (sas_dll_ != NULL) { | |
132 base::UnloadNativeLibrary(sas_dll_); | |
133 } | |
134 } | |
135 | |
136 bool SasSenderVista::Send() { | |
137 // Load sas.dll. The library is expected to be in | |
138 // the same folder as this binary. | |
Wez
2012/03/07 01:56:13
nit: Premature line-wrap.
alexeypa (please no reviews)
2012/03/07 19:59:08
Done.
| |
139 if (sas_dll_ == NULL) { | |
140 FilePath exe_path; | |
141 if (!PathService::Get(base::FILE_EXE, &exe_path)) { | |
142 LOG(ERROR) << "Failed to get the executable file name."; | |
143 return false; | |
144 } | |
145 | |
146 std::string error; | |
147 sas_dll_ = base::LoadNativeLibrary( | |
148 exe_path.DirName().Append(kSasDllFileName), | |
149 &error); | |
150 if (sas_dll_ == NULL) { | |
151 LOG(ERROR) << "Failed to load '" << kSasDllFileName << "'"; | |
152 return false; | |
153 } | |
154 } | |
155 | |
156 // Get the pointer to sas!SendSAS(). | |
157 if (send_sas_ == NULL) { | |
158 send_sas_ = reinterpret_cast<SendSasFunc>( | |
159 base::GetFunctionPointerFromNativeLibrary(sas_dll_, kSendSasName)); | |
160 if (send_sas_ == NULL) { | |
161 LOG(ERROR) << "Failed to retrieve the address of '" << kSendSasName | |
162 << "()'"; | |
163 return false; | |
164 } | |
165 } | |
166 | |
167 // Enable software SAS generation by services and send SAS. SAS can still fail | |
168 // if the policy does not applow services to generate software SAS. | |
Wez
2012/03/07 01:56:13
typo: allow
alexeypa (please no reviews)
2012/03/07 19:59:08
Done.
| |
169 ScopedSoftwareSasPolicy enable_sas; | |
170 if (enable_sas.Apply()) { | |
171 (*send_sas_)(FALSE); | |
Wez
2012/03/07 01:56:13
Is it a problem for us to call SendSAS if the poli
alexeypa (please no reviews)
2012/03/07 19:59:08
Yes. It does nothing if the policy is not enabled.
Wez
2012/03/08 00:01:33
So we shouldn't be testing the result of |enable_s
alexeypa (please no reviews)
2012/03/08 01:52:54
No, we should. False means 'something is badly wro
| |
172 } | |
173 | |
174 return true; | |
Wez
2012/03/07 01:56:13
Do you mean to return true whether or not the poli
alexeypa (please no reviews)
2012/03/07 19:59:08
Yes. False is returned only if any of the operatio
Wez
2012/03/08 00:01:33
Is it worth returning false, in that case, since t
alexeypa (please no reviews)
2012/03/08 01:52:54
We should (and do) behave differently. See above.
| |
175 } | |
176 | |
177 scoped_ptr<SasSender> SasSender::Create() { | |
178 if (base::win::OSInfo::GetInstance()->version() >= base::win::VERSION_VISTA) { | |
179 return scoped_ptr<SasSender>(new SasSenderVista()); | |
180 } | |
181 | |
182 return scoped_ptr<SasSender>(); | |
183 } | |
184 | |
185 } // namespace remoting | |
OLD | NEW |