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