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/elevated_controller_win.h" | 5 #include "remoting/host/win/elevated_controller.h" |
6 | 6 |
7 #include <sddl.h> | 7 #include <sddl.h> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/file_version_info.h" | 10 #include "base/file_version_info.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
13 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
16 #include "base/process_util.h" | 16 #include "base/process_util.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.h" |
20 #include "remoting/host/branding.h" | 20 #include "remoting/host/branding.h" |
21 #include "remoting/host/elevated_controller_resource.h" | |
22 #include "remoting/host/usage_stats_consent.h" | 21 #include "remoting/host/usage_stats_consent.h" |
23 #include "remoting/host/verify_config_window_win.h" | 22 #include "remoting/host/verify_config_window_win.h" |
| 23 #include "remoting/host/win/elevated_controller_resource.h" |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 // The maximum size of the configuration file. "1MB ought to be enough" for any | 27 // The maximum size of the configuration file. "1MB ought to be enough" for any |
28 // reasonable configuration we will ever need. 1MB is low enough to make | 28 // reasonable configuration we will ever need. 1MB is low enough to make |
29 // the probability of out of memory situation fairly low. OOM is still possible | 29 // the probability of out of memory situation fairly low. OOM is still possible |
30 // and we will crash if it occurs. | 30 // and we will crash if it occurs. |
31 const size_t kMaxConfigFileSize = 1024 * 1024; | 31 const size_t kMaxConfigFileSize = 1024 * 1024; |
32 | 32 |
33 // The host configuration file name. | 33 // The host configuration file name. |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 return hr; | 288 return hr; |
289 } | 289 } |
290 | 290 |
291 return S_OK; | 291 return S_OK; |
292 } | 292 } |
293 | 293 |
294 } // namespace | 294 } // namespace |
295 | 295 |
296 namespace remoting { | 296 namespace remoting { |
297 | 297 |
298 ElevatedControllerWin::ElevatedControllerWin() : owner_window_(NULL) { | 298 ElevatedController::ElevatedController() : owner_window_(NULL) { |
299 } | 299 } |
300 | 300 |
301 HRESULT ElevatedControllerWin::FinalConstruct() { | 301 HRESULT ElevatedController::FinalConstruct() { |
302 return S_OK; | 302 return S_OK; |
303 } | 303 } |
304 | 304 |
305 void ElevatedControllerWin::FinalRelease() { | 305 void ElevatedController::FinalRelease() { |
306 } | 306 } |
307 | 307 |
308 STDMETHODIMP ElevatedControllerWin::GetConfig(BSTR* config_out) { | 308 STDMETHODIMP ElevatedController::GetConfig(BSTR* config_out) { |
309 FilePath config_dir = remoting::GetConfigDir(); | 309 FilePath config_dir = remoting::GetConfigDir(); |
310 | 310 |
311 // Read the unprivileged part of host configuration. | 311 // Read the unprivileged part of host configuration. |
312 scoped_ptr<base::DictionaryValue> config; | 312 scoped_ptr<base::DictionaryValue> config; |
313 HRESULT hr = ReadConfig(config_dir.Append(kUnprivilegedConfigFileName), | 313 HRESULT hr = ReadConfig(config_dir.Append(kUnprivilegedConfigFileName), |
314 &config); | 314 &config); |
315 if (FAILED(hr)) { | 315 if (FAILED(hr)) { |
316 return hr; | 316 return hr; |
317 } | 317 } |
318 | 318 |
319 // Convert the config back to a string and return it to the caller. | 319 // Convert the config back to a string and return it to the caller. |
320 std::string file_content; | 320 std::string file_content; |
321 base::JSONWriter::Write(config.get(), &file_content); | 321 base::JSONWriter::Write(config.get(), &file_content); |
322 | 322 |
323 *config_out = ::SysAllocString(UTF8ToUTF16(file_content).c_str()); | 323 *config_out = ::SysAllocString(UTF8ToUTF16(file_content).c_str()); |
324 if (config_out == NULL) { | 324 if (config_out == NULL) { |
325 return E_OUTOFMEMORY; | 325 return E_OUTOFMEMORY; |
326 } | 326 } |
327 | 327 |
328 return S_OK; | 328 return S_OK; |
329 } | 329 } |
330 | 330 |
331 STDMETHODIMP ElevatedControllerWin::GetVersion(BSTR* version_out) { | 331 STDMETHODIMP ElevatedController::GetVersion(BSTR* version_out) { |
332 // Report the product version number of the daemon controller binary as | 332 // Report the product version number of the daemon controller binary as |
333 // the host version. | 333 // the host version. |
334 HMODULE binary = base::GetModuleFromAddress( | 334 HMODULE binary = base::GetModuleFromAddress( |
335 reinterpret_cast<void*>(&ReadConfig)); | 335 reinterpret_cast<void*>(&ReadConfig)); |
336 scoped_ptr<FileVersionInfo> version_info( | 336 scoped_ptr<FileVersionInfo> version_info( |
337 FileVersionInfo::CreateFileVersionInfoForModule(binary)); | 337 FileVersionInfo::CreateFileVersionInfoForModule(binary)); |
338 | 338 |
339 string16 version; | 339 string16 version; |
340 if (version_info.get()) { | 340 if (version_info.get()) { |
341 version = version_info->product_version(); | 341 version = version_info->product_version(); |
342 } | 342 } |
343 | 343 |
344 *version_out = ::SysAllocString(version.c_str()); | 344 *version_out = ::SysAllocString(version.c_str()); |
345 if (version_out == NULL) { | 345 if (version_out == NULL) { |
346 return E_OUTOFMEMORY; | 346 return E_OUTOFMEMORY; |
347 } | 347 } |
348 | 348 |
349 return S_OK; | 349 return S_OK; |
350 } | 350 } |
351 | 351 |
352 STDMETHODIMP ElevatedControllerWin::SetConfig(BSTR config) { | 352 STDMETHODIMP ElevatedController::SetConfig(BSTR config) { |
353 // Determine the config directory path and create it if necessary. | 353 // Determine the config directory path and create it if necessary. |
354 FilePath config_dir = remoting::GetConfigDir(); | 354 FilePath config_dir = remoting::GetConfigDir(); |
355 if (!file_util::CreateDirectory(config_dir)) { | 355 if (!file_util::CreateDirectory(config_dir)) { |
356 return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED); | 356 return HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED); |
357 } | 357 } |
358 | 358 |
359 std::string file_content = UTF16ToUTF8( | 359 std::string file_content = UTF16ToUTF8( |
360 string16(static_cast<char16*>(config), ::SysStringLen(config))); | 360 string16(static_cast<char16*>(config), ::SysStringLen(config))); |
361 | 361 |
362 return WriteConfig(file_content.c_str(), file_content.size(), owner_window_); | 362 return WriteConfig(file_content.c_str(), file_content.size(), owner_window_); |
363 } | 363 } |
364 | 364 |
365 STDMETHODIMP ElevatedControllerWin::SetOwnerWindow(LONG_PTR window_handle) { | 365 STDMETHODIMP ElevatedController::SetOwnerWindow(LONG_PTR window_handle) { |
366 owner_window_ = reinterpret_cast<HWND>(window_handle); | 366 owner_window_ = reinterpret_cast<HWND>(window_handle); |
367 return S_OK; | 367 return S_OK; |
368 } | 368 } |
369 | 369 |
370 STDMETHODIMP ElevatedControllerWin::StartDaemon() { | 370 STDMETHODIMP ElevatedController::StartDaemon() { |
371 ScopedScHandle service; | 371 ScopedScHandle service; |
372 HRESULT hr = OpenService(&service); | 372 HRESULT hr = OpenService(&service); |
373 if (FAILED(hr)) { | 373 if (FAILED(hr)) { |
374 return hr; | 374 return hr; |
375 } | 375 } |
376 | 376 |
377 // Change the service start type to 'auto'. | 377 // Change the service start type to 'auto'. |
378 if (!::ChangeServiceConfigW(service, | 378 if (!::ChangeServiceConfigW(service, |
379 SERVICE_NO_CHANGE, | 379 SERVICE_NO_CHANGE, |
380 SERVICE_AUTO_START, | 380 SERVICE_AUTO_START, |
(...skipping 19 matching lines...) Expand all Loading... |
400 LOG_GETLASTERROR(ERROR) | 400 LOG_GETLASTERROR(ERROR) |
401 << "Failed to start the '" << kWindowsServiceName << "'service"; | 401 << "Failed to start the '" << kWindowsServiceName << "'service"; |
402 | 402 |
403 return HRESULT_FROM_WIN32(error); | 403 return HRESULT_FROM_WIN32(error); |
404 } | 404 } |
405 } | 405 } |
406 | 406 |
407 return S_OK; | 407 return S_OK; |
408 } | 408 } |
409 | 409 |
410 STDMETHODIMP ElevatedControllerWin::StopDaemon() { | 410 STDMETHODIMP ElevatedController::StopDaemon() { |
411 ScopedScHandle service; | 411 ScopedScHandle service; |
412 HRESULT hr = OpenService(&service); | 412 HRESULT hr = OpenService(&service); |
413 if (FAILED(hr)) { | 413 if (FAILED(hr)) { |
414 return hr; | 414 return hr; |
415 } | 415 } |
416 | 416 |
417 // Change the service start type to 'manual'. | 417 // Change the service start type to 'manual'. |
418 if (!::ChangeServiceConfigW(service, | 418 if (!::ChangeServiceConfigW(service, |
419 SERVICE_NO_CHANGE, | 419 SERVICE_NO_CHANGE, |
420 SERVICE_DEMAND_START, | 420 SERVICE_DEMAND_START, |
(...skipping 19 matching lines...) Expand all Loading... |
440 if (error != ERROR_SERVICE_NOT_ACTIVE) { | 440 if (error != ERROR_SERVICE_NOT_ACTIVE) { |
441 LOG_GETLASTERROR(ERROR) | 441 LOG_GETLASTERROR(ERROR) |
442 << "Failed to stop the '" << kWindowsServiceName << "'service"; | 442 << "Failed to stop the '" << kWindowsServiceName << "'service"; |
443 return HRESULT_FROM_WIN32(error); | 443 return HRESULT_FROM_WIN32(error); |
444 } | 444 } |
445 } | 445 } |
446 | 446 |
447 return S_OK; | 447 return S_OK; |
448 } | 448 } |
449 | 449 |
450 STDMETHODIMP ElevatedControllerWin::UpdateConfig(BSTR config) { | 450 STDMETHODIMP ElevatedController::UpdateConfig(BSTR config) { |
451 // Parse the config. | 451 // Parse the config. |
452 std::string config_str = UTF16ToUTF8( | 452 std::string config_str = UTF16ToUTF8( |
453 string16(static_cast<char16*>(config), ::SysStringLen(config))); | 453 string16(static_cast<char16*>(config), ::SysStringLen(config))); |
454 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_str)); | 454 scoped_ptr<base::Value> config_value(base::JSONReader::Read(config_str)); |
455 if (!config_value.get()) { | 455 if (!config_value.get()) { |
456 return E_FAIL; | 456 return E_FAIL; |
457 } | 457 } |
458 base::DictionaryValue* config_dict = NULL; | 458 base::DictionaryValue* config_dict = NULL; |
459 if (!config_value->GetAsDictionary(&config_dict)) { | 459 if (!config_value->GetAsDictionary(&config_dict)) { |
460 return E_FAIL; | 460 return E_FAIL; |
(...skipping 13 matching lines...) Expand all Loading... |
474 } | 474 } |
475 // Merge items from the given config into the old config. | 475 // Merge items from the given config into the old config. |
476 config_old->MergeDictionary(config_dict); | 476 config_old->MergeDictionary(config_dict); |
477 // Write the updated config. | 477 // Write the updated config. |
478 std::string config_updated_str; | 478 std::string config_updated_str; |
479 base::JSONWriter::Write(config_old.get(), &config_updated_str); | 479 base::JSONWriter::Write(config_old.get(), &config_updated_str); |
480 return WriteConfig(config_updated_str.c_str(), config_updated_str.size(), | 480 return WriteConfig(config_updated_str.c_str(), config_updated_str.size(), |
481 owner_window_); | 481 owner_window_); |
482 } | 482 } |
483 | 483 |
484 STDMETHODIMP ElevatedControllerWin::GetUsageStatsConsent(BOOL* allowed, | 484 STDMETHODIMP ElevatedController::GetUsageStatsConsent(BOOL* allowed, |
485 BOOL* set_by_policy) { | 485 BOOL* set_by_policy) { |
486 bool local_allowed; | 486 bool local_allowed; |
487 bool local_set_by_policy; | 487 bool local_set_by_policy; |
488 if (remoting::GetUsageStatsConsent(&local_allowed, &local_set_by_policy)) { | 488 if (remoting::GetUsageStatsConsent(&local_allowed, &local_set_by_policy)) { |
489 *allowed = local_allowed; | 489 *allowed = local_allowed; |
490 *set_by_policy = local_set_by_policy; | 490 *set_by_policy = local_set_by_policy; |
491 return S_OK; | 491 return S_OK; |
492 } else { | 492 } else { |
493 return E_FAIL; | 493 return E_FAIL; |
494 } | 494 } |
495 } | 495 } |
496 | 496 |
497 STDMETHODIMP ElevatedControllerWin::SetUsageStatsConsent(BOOL allowed) { | 497 STDMETHODIMP ElevatedController::SetUsageStatsConsent(BOOL allowed) { |
498 if (remoting::SetUsageStatsConsent(!!allowed)) { | 498 if (remoting::SetUsageStatsConsent(!!allowed)) { |
499 return S_OK; | 499 return S_OK; |
500 } else { | 500 } else { |
501 return E_FAIL; | 501 return E_FAIL; |
502 } | 502 } |
503 } | 503 } |
504 | 504 |
505 HRESULT ElevatedControllerWin::OpenService(ScopedScHandle* service_out) { | 505 HRESULT ElevatedController::OpenService(ScopedScHandle* service_out) { |
506 DWORD error; | 506 DWORD error; |
507 | 507 |
508 ScopedScHandle scmanager( | 508 ScopedScHandle scmanager( |
509 ::OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASE, | 509 ::OpenSCManagerW(NULL, SERVICES_ACTIVE_DATABASE, |
510 SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE)); | 510 SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE)); |
511 if (!scmanager.IsValid()) { | 511 if (!scmanager.IsValid()) { |
512 error = GetLastError(); | 512 error = GetLastError(); |
513 LOG_GETLASTERROR(ERROR) | 513 LOG_GETLASTERROR(ERROR) |
514 << "Failed to connect to the service control manager"; | 514 << "Failed to connect to the service control manager"; |
515 | 515 |
(...skipping 10 matching lines...) Expand all Loading... |
526 << "Failed to open to the '" << kWindowsServiceName << "' service"; | 526 << "Failed to open to the '" << kWindowsServiceName << "' service"; |
527 | 527 |
528 return HRESULT_FROM_WIN32(error); | 528 return HRESULT_FROM_WIN32(error); |
529 } | 529 } |
530 | 530 |
531 service_out->Set(service.Take()); | 531 service_out->Set(service.Take()); |
532 return S_OK; | 532 return S_OK; |
533 } | 533 } |
534 | 534 |
535 } // namespace remoting | 535 } // namespace remoting |
OLD | NEW |