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