| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/linux/certificate_watcher.h" | 5 #include "remoting/host/linux/certificate_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_util.h" |
| 10 #include "base/hash.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 14 #include "base/threading/thread_checker.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
| 13 | 16 |
| 14 namespace remoting { | 17 namespace remoting { |
| 15 | 18 |
| 16 // Delay time to restart the host when a change of certificate is detected. | 19 namespace { |
| 17 // This is to avoid repeating restarts when continuous writes to the database | 20 |
| 18 // occur. | 21 // Delay before re-reading the cert DB when a change is detected. |
| 19 const int kRestartDelayInSecond = 2; | 22 const int kReadDelayInSeconds = 2; |
| 20 | 23 |
| 21 // Full Path: $HOME/.pki/nssdb | 24 // Full Path: $HOME/.pki/nssdb |
| 22 const char kCertDirectoryPath[] = ".pki/nssdb"; | 25 const char kCertDirectoryPath[] = ".pki/nssdb"; |
| 23 | 26 |
| 27 const char* const kCertFiles[] = {"cert9.db", "key4.db", "pkcs11.txt"}; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 // This class lives on the IO thread, watches the certificate database files, |
| 32 // and notifies the caller_task_runner thread of any updates. |
| 33 class CertDbContentWatcher { |
| 34 public: |
| 35 CertDbContentWatcher( |
| 36 base::WeakPtr<CertificateWatcher> watcher, |
| 37 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 38 base::FilePath cert_watch_path, |
| 39 base::TimeDelta read_delay); |
| 40 ~CertDbContentWatcher(); |
| 41 |
| 42 void StartWatching(); |
| 43 |
| 44 private: |
| 45 base::ThreadChecker thread_checker_; |
| 46 |
| 47 // size_t is the return type of base::HashInts() which is used to accumulate |
| 48 // a hash-code while iterating over the database files. |
| 49 typedef size_t HashValue; |
| 50 |
| 51 // Called by the FileWatcher when it detects any changes. |
| 52 void OnCertDirectoryChanged(const base::FilePath& path, bool error); |
| 53 |
| 54 // Called by |read_timer_| to trigger re-reading the DB content. |
| 55 void OnTimer(); |
| 56 |
| 57 // Reads the certificate database files and returns a hash of their contents. |
| 58 HashValue ComputeHash(); |
| 59 |
| 60 base::WeakPtr<CertificateWatcher> watcher_; |
| 61 |
| 62 // The TaskRunner to be notified of any changes. |
| 63 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 64 |
| 65 // The file watcher to watch changes inside the certificate folder. |
| 66 std::unique_ptr<base::FilePathWatcher> file_watcher_; |
| 67 |
| 68 // Path of the certificate files/directories. |
| 69 base::FilePath cert_watch_path_; |
| 70 |
| 71 // Timer to delay reading the DB files after a change notification from the |
| 72 // FileWatcher. This is done to avoid triggering multiple notifications when |
| 73 // the DB is written to. It also avoids a false notification in case the NSS |
| 74 // DB content is quickly changed and reverted. |
| 75 std::unique_ptr<base::DelayTimer> read_timer_; |
| 76 |
| 77 // The time to wait before re-reading the DB files after a change is |
| 78 // detected. |
| 79 base::TimeDelta delay_; |
| 80 |
| 81 // The hash code of the current certificate database contents. When the |
| 82 // FileWatcher detects changes, the code is re-computed and compared with |
| 83 // this stored value. |
| 84 HashValue current_hash_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(CertDbContentWatcher); |
| 87 }; |
| 88 |
| 89 CertDbContentWatcher::CertDbContentWatcher( |
| 90 base::WeakPtr<CertificateWatcher> watcher, |
| 91 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 92 base::FilePath cert_watch_path, |
| 93 base::TimeDelta read_delay) |
| 94 : watcher_(watcher), |
| 95 caller_task_runner_(caller_task_runner), |
| 96 cert_watch_path_(cert_watch_path), |
| 97 delay_(read_delay) { |
| 98 thread_checker_.DetachFromThread(); |
| 99 } |
| 100 |
| 101 CertDbContentWatcher::~CertDbContentWatcher() {} |
| 102 |
| 103 void CertDbContentWatcher::StartWatching() { |
| 104 DCHECK(!cert_watch_path_.empty()); |
| 105 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 |
| 107 file_watcher_.reset(new base::FilePathWatcher()); |
| 108 |
| 109 // Initialize hash value. |
| 110 current_hash_ = ComputeHash(); |
| 111 |
| 112 // base::Unretained() is safe since this class owns the FileWatcher. |
| 113 file_watcher_->Watch(cert_watch_path_, true, |
| 114 base::Bind(&CertDbContentWatcher::OnCertDirectoryChanged, |
| 115 base::Unretained(this))); |
| 116 |
| 117 read_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, |
| 118 &CertDbContentWatcher::OnTimer)); |
| 119 } |
| 120 |
| 121 void CertDbContentWatcher::OnCertDirectoryChanged(const base::FilePath& path, |
| 122 bool error) { |
| 123 DCHECK(path == cert_watch_path_); |
| 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 125 |
| 126 if (error) { |
| 127 LOG(FATAL) << "Error occurred while watching for changes of file: " |
| 128 << cert_watch_path_.MaybeAsASCII(); |
| 129 } |
| 130 |
| 131 read_timer_->Reset(); |
| 132 } |
| 133 |
| 134 void CertDbContentWatcher::OnTimer() { |
| 135 DCHECK(thread_checker_.CalledOnValidThread()); |
| 136 |
| 137 HashValue new_hash = ComputeHash(); |
| 138 if (new_hash != current_hash_) { |
| 139 current_hash_ = new_hash; |
| 140 caller_task_runner_->PostTask( |
| 141 FROM_HERE, base::Bind(&CertificateWatcher::DatabaseChanged, watcher_)); |
| 142 } else { |
| 143 VLOG(1) << "Directory changed but contents are the same."; |
| 144 } |
| 145 } |
| 146 |
| 147 CertDbContentWatcher::HashValue CertDbContentWatcher::ComputeHash() { |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); |
| 149 |
| 150 HashValue result = 0; |
| 151 |
| 152 for (const char* file : kCertFiles) { |
| 153 base::FilePath path = cert_watch_path_.AppendASCII(file); |
| 154 std::string content; |
| 155 HashValue file_hash = 0; |
| 156 |
| 157 // It's possible the file might not exist. Compute the overall hash in a |
| 158 // consistent way for the set of files that do exist. If a new file comes |
| 159 // into existence, the resulting hash-code should change. |
| 160 if (base::ReadFileToString(path, &content)) { |
| 161 file_hash = base::Hash(content); |
| 162 } |
| 163 result = base::HashInts(result, file_hash); |
| 164 } |
| 165 return result; |
| 166 } |
| 167 |
| 24 CertificateWatcher::CertificateWatcher( | 168 CertificateWatcher::CertificateWatcher( |
| 25 const base::Closure& restart_action, | 169 const base::Closure& restart_action, |
| 26 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 170 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 27 : restart_action_(restart_action), | 171 : restart_action_(restart_action), |
| 28 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 172 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 29 io_task_runner_(io_task_runner), | 173 io_task_runner_(io_task_runner), |
| 30 delay_(base::TimeDelta::FromSeconds(kRestartDelayInSecond)), | 174 delay_(base::TimeDelta::FromSeconds(kReadDelayInSeconds)), |
| 31 weak_factory_(this) { | 175 weak_factory_(this) { |
| 32 if (!base::PathService::Get(base::DIR_HOME, &cert_watch_path_)) { | 176 if (!base::PathService::Get(base::DIR_HOME, &cert_watch_path_)) { |
| 33 LOG(FATAL) << "Failed to get path of the home directory."; | 177 LOG(FATAL) << "Failed to get path of the home directory."; |
| 34 } | 178 } |
| 35 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); | 179 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); |
| 36 } | 180 } |
| 37 | 181 |
| 38 CertificateWatcher::~CertificateWatcher() { | 182 CertificateWatcher::~CertificateWatcher() { |
| 39 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 183 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 40 | 184 |
| 41 if (!is_started()) { | 185 if (!is_started()) { |
| 42 return; | 186 return; |
| 43 } | 187 } |
| 44 if (monitor_) { | 188 if (monitor_) { |
| 45 monitor_->RemoveStatusObserver(this); | 189 monitor_->RemoveStatusObserver(this); |
| 46 } | 190 } |
| 47 io_task_runner_->DeleteSoon(FROM_HERE, file_watcher_.release()); | 191 io_task_runner_->DeleteSoon(FROM_HERE, content_watcher_.release()); |
| 48 | 192 |
| 49 VLOG(1) << "Stopped watching certificate changes."; | 193 VLOG(1) << "Stopped watching certificate changes."; |
| 50 } | 194 } |
| 51 | 195 |
| 52 void CertificateWatcher::Start() { | 196 void CertificateWatcher::Start() { |
| 53 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 197 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 54 DCHECK(!cert_watch_path_.empty()); | 198 DCHECK(!cert_watch_path_.empty()); |
| 55 | 199 |
| 56 file_watcher_.reset(new base::FilePathWatcher()); | 200 content_watcher_.reset(new CertDbContentWatcher(weak_factory_.GetWeakPtr(), |
| 201 caller_task_runner_, |
| 202 cert_watch_path_, delay_)); |
| 203 |
| 57 io_task_runner_->PostTask( | 204 io_task_runner_->PostTask( |
| 58 FROM_HERE, | 205 FROM_HERE, base::Bind(&CertDbContentWatcher::StartWatching, |
| 59 base::Bind(base::IgnoreResult(&base::FilePathWatcher::Watch), | 206 base::Unretained(content_watcher_.get()))); |
| 60 base::Unretained(file_watcher_.get()), cert_watch_path_, true, | |
| 61 base::Bind(&CertificateWatcher::OnCertDirectoryChanged, | |
| 62 caller_task_runner_, weak_factory_.GetWeakPtr()))); | |
| 63 restart_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, | |
| 64 &CertificateWatcher::OnTimer)); | |
| 65 | 207 |
| 66 VLOG(1) << "Started watching certificate changes."; | 208 VLOG(1) << "Started watching certificate changes."; |
| 67 } | 209 } |
| 68 | 210 |
| 69 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { | 211 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { |
| 70 DCHECK(is_started()); | 212 DCHECK(is_started()); |
| 71 if (monitor_) { | 213 if (monitor_) { |
| 72 monitor_->RemoveStatusObserver(this); | 214 monitor_->RemoveStatusObserver(this); |
| 73 } | 215 } |
| 74 monitor->AddStatusObserver(this); | 216 monitor->AddStatusObserver(this); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 96 delay_ = delay; | 238 delay_ = delay; |
| 97 } | 239 } |
| 98 | 240 |
| 99 void CertificateWatcher::SetWatchPathForTests( | 241 void CertificateWatcher::SetWatchPathForTests( |
| 100 const base::FilePath& watch_path) { | 242 const base::FilePath& watch_path) { |
| 101 DCHECK(!is_started()); | 243 DCHECK(!is_started()); |
| 102 cert_watch_path_ = watch_path; | 244 cert_watch_path_ = watch_path; |
| 103 } | 245 } |
| 104 | 246 |
| 105 bool CertificateWatcher::is_started() const { | 247 bool CertificateWatcher::is_started() const { |
| 106 return file_watcher_ != nullptr; | 248 return content_watcher_ != nullptr; |
| 107 } | 249 } |
| 108 | 250 |
| 109 // static | 251 void CertificateWatcher::DatabaseChanged() { |
| 110 void CertificateWatcher::OnCertDirectoryChanged( | |
| 111 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 112 base::WeakPtr<CertificateWatcher> watcher_, | |
| 113 const base::FilePath& path, | |
| 114 bool error) { | |
| 115 network_task_runner->PostTask( | |
| 116 FROM_HERE, | |
| 117 base::Bind(&CertificateWatcher::DirectoryChanged, watcher_, path, error)); | |
| 118 } | |
| 119 | |
| 120 void CertificateWatcher::DirectoryChanged(const base::FilePath& path, | |
| 121 bool error) { | |
| 122 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | |
| 123 DCHECK(path == cert_watch_path_); | |
| 124 | |
| 125 if (error) { | |
| 126 LOG(FATAL) << "Error occurs when watching changes of file " | |
| 127 << cert_watch_path_.MaybeAsASCII(); | |
| 128 } | |
| 129 | |
| 130 restart_timer_->Reset(); | |
| 131 } | |
| 132 | |
| 133 void CertificateWatcher::OnTimer() { | |
| 134 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 252 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 135 | 253 |
| 136 if (inhibit_mode_) { | 254 if (inhibit_mode_) { |
| 137 restart_pending_ = true; | 255 restart_pending_ = true; |
| 138 return; | 256 return; |
| 139 } | 257 } |
| 140 | 258 |
| 141 VLOG(1) << "Certificate was updated. Calling restart..."; | 259 VLOG(1) << "Certificate was updated. Calling restart..."; |
| 142 restart_action_.Run(); | 260 restart_action_.Run(); |
| 143 } | 261 } |
| 144 | 262 |
| 145 } // namespace remoting | 263 } // namespace remoting |
| OLD | NEW |