| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/setup/me2me_native_messaging_host.h" | 5 #include "remoting/host/setup/me2me_native_messaging_host.h" |
| 6 | 6 |
| 7 #include <cstdint> | 7 #include <cstdint> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
| 16 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 17 #include "base/logging.h" | 17 #include "base/logging.h" |
| 18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/single_thread_task_runner.h" | 19 #include "base/single_thread_task_runner.h" |
| 20 #include "base/strings/stringize_macros.h" | 20 #include "base/strings/stringize_macros.h" |
| 21 #include "base/time/time.h" |
| 21 #include "base/values.h" | 22 #include "base/values.h" |
| 22 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 23 #include "google_apis/gaia/gaia_oauth_client.h" | 24 #include "google_apis/gaia/gaia_oauth_client.h" |
| 24 #include "google_apis/google_api_keys.h" | 25 #include "google_apis/google_api_keys.h" |
| 25 #include "net/base/network_interfaces.h" | 26 #include "net/base/network_interfaces.h" |
| 26 #include "remoting/base/auto_thread_task_runner.h" | 27 #include "remoting/base/auto_thread_task_runner.h" |
| 27 #include "remoting/base/rsa_key_pair.h" | 28 #include "remoting/base/rsa_key_pair.h" |
| 28 #include "remoting/host/chromoting_host_context.h" | 29 #include "remoting/host/chromoting_host_context.h" |
| 29 #include "remoting/host/native_messaging/log_message_handler.h" | 30 #include "remoting/host/native_messaging/log_message_handler.h" |
| 30 #include "remoting/host/native_messaging/pipe_messaging_channel.h" | |
| 31 #include "remoting/host/pin_hash.h" | 31 #include "remoting/host/pin_hash.h" |
| 32 #include "remoting/host/setup/oauth_client.h" | 32 #include "remoting/host/setup/oauth_client.h" |
| 33 #include "remoting/host/switches.h" | |
| 34 #include "remoting/protocol/pairing_registry.h" | 33 #include "remoting/protocol/pairing_registry.h" |
| 35 | 34 |
| 36 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 37 #include "base/win/scoped_handle.h" | 36 #include "remoting/host/win/elevated_native_messaging_host.h" |
| 38 #include "base/win/win_util.h" | |
| 39 #include "remoting/host/win/launch_native_messaging_host_process.h" | |
| 40 #endif // defined(OS_WIN) | 37 #endif // defined(OS_WIN) |
| 41 | 38 |
| 42 namespace { | 39 namespace { |
| 43 | 40 |
| 44 #if defined(OS_WIN) | 41 #if defined(OS_WIN) |
| 45 const int kElevatedHostTimeoutSeconds = 300; | 42 const int kElevatedHostTimeoutSeconds = 300; |
| 46 #endif // defined(OS_WIN) | 43 #endif // defined(OS_WIN) |
| 47 | 44 |
| 48 // redirect_uri to use when authenticating service accounts (service account | 45 // redirect_uri to use when authenticating service accounts (service account |
| 49 // codes are obtained "out-of-band", i.e., not through an OAuth redirect). | 46 // codes are obtained "out-of-band", i.e., not through an OAuth redirect). |
| (...skipping 489 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 | 536 |
| 540 if (!error_message.empty()) { | 537 if (!error_message.empty()) { |
| 541 LOG(ERROR) << error_message; | 538 LOG(ERROR) << error_message; |
| 542 } | 539 } |
| 543 | 540 |
| 544 // Trigger a host shutdown by sending an empty message. | 541 // Trigger a host shutdown by sending an empty message. |
| 545 client_->CloseChannel(std::string()); | 542 client_->CloseChannel(std::string()); |
| 546 } | 543 } |
| 547 | 544 |
| 548 #if defined(OS_WIN) | 545 #if defined(OS_WIN) |
| 549 Me2MeNativeMessagingHost::ElevatedChannelEventHandler:: | |
| 550 ElevatedChannelEventHandler(extensions::NativeMessageHost::Client* client) | |
| 551 : client_(client) {} | |
| 552 | |
| 553 void Me2MeNativeMessagingHost::ElevatedChannelEventHandler::OnMessage( | |
| 554 std::unique_ptr<base::Value> message) { | |
| 555 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 556 | |
| 557 // Simply pass along the response from the elevated host to the client. | |
| 558 std::string message_json; | |
| 559 base::JSONWriter::Write(*message, &message_json); | |
| 560 client_->PostMessageFromNativeHost(message_json); | |
| 561 } | |
| 562 | |
| 563 void Me2MeNativeMessagingHost::ElevatedChannelEventHandler::OnDisconnect() { | |
| 564 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 565 client_->CloseChannel(std::string()); | |
| 566 } | |
| 567 | 546 |
| 568 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( | 547 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( |
| 569 std::unique_ptr<base::DictionaryValue> message) { | 548 std::unique_ptr<base::DictionaryValue> message) { |
| 570 DCHECK(task_runner()->BelongsToCurrentThread()); | 549 DCHECK(task_runner()->BelongsToCurrentThread()); |
| 571 | |
| 572 EnsureElevatedHostCreated(); | |
| 573 | |
| 574 // elevated_channel_ will be null if user rejects the UAC request. | |
| 575 if (elevated_channel_) | |
| 576 elevated_channel_->SendMessage(std::move(message)); | |
| 577 | |
| 578 return elevated_channel_ != nullptr; | |
| 579 } | |
| 580 | |
| 581 void Me2MeNativeMessagingHost::EnsureElevatedHostCreated() { | |
| 582 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 583 DCHECK(needs_elevation_); | 550 DCHECK(needs_elevation_); |
| 584 | 551 |
| 585 if (elevated_channel_) | 552 if (!elevated_host_) { |
| 586 return; | 553 elevated_host_.reset(new ElevatedNativeMessagingHost( |
| 587 | 554 base::CommandLine::ForCurrentProcess()->GetProgram(), |
| 588 base::win::ScopedHandle read_handle; | 555 parent_window_handle_, |
| 589 base::win::ScopedHandle write_handle; | 556 /*elevate_process=*/true, |
| 590 // Get the name of the binary to launch. | 557 base::TimeDelta::FromSeconds(kElevatedHostTimeoutSeconds), |
| 591 base::FilePath binary = base::CommandLine::ForCurrentProcess()->GetProgram(); | 558 client_)); |
| 592 ProcessLaunchResult result = LaunchNativeMessagingHostProcess( | |
| 593 binary, parent_window_handle_, | |
| 594 /*elevate_process=*/true, &read_handle, &write_handle); | |
| 595 if (result != PROCESS_LAUNCH_RESULT_SUCCESS) { | |
| 596 return; | |
| 597 } | 559 } |
| 598 | 560 |
| 599 // Set up the native messaging channel to talk to the elevated host. | 561 if (elevated_host_->EnsureElevatedHostCreated()) { |
| 600 // Note that input for the elevated channel is output for the elevated host. | 562 elevated_host_->SendMessage(std::move(message)); |
| 601 elevated_channel_.reset(new PipeMessagingChannel( | 563 return true; |
| 602 base::File(read_handle.Take()), base::File(write_handle.Take()))); | 564 } |
| 603 | 565 |
| 604 elevated_channel_event_handler_.reset( | 566 return false; |
| 605 new Me2MeNativeMessagingHost::ElevatedChannelEventHandler(client_)); | |
| 606 elevated_channel_->Start(elevated_channel_event_handler_.get()); | |
| 607 | |
| 608 elevated_host_timer_.Start( | |
| 609 FROM_HERE, base::TimeDelta::FromSeconds(kElevatedHostTimeoutSeconds), | |
| 610 this, &Me2MeNativeMessagingHost::DisconnectElevatedHost); | |
| 611 } | |
| 612 | |
| 613 void Me2MeNativeMessagingHost::DisconnectElevatedHost() { | |
| 614 DCHECK(task_runner()->BelongsToCurrentThread()); | |
| 615 | |
| 616 // This will send an EOF to the elevated host, triggering its shutdown. | |
| 617 elevated_channel_.reset(); | |
| 618 } | 567 } |
| 619 | 568 |
| 620 #else // defined(OS_WIN) | 569 #else // defined(OS_WIN) |
| 621 | 570 |
| 622 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( | 571 bool Me2MeNativeMessagingHost::DelegateToElevatedHost( |
| 623 std::unique_ptr<base::DictionaryValue> message) { | 572 std::unique_ptr<base::DictionaryValue> message) { |
| 624 NOTREACHED(); | 573 NOTREACHED(); |
| 625 return false; | 574 return false; |
| 626 } | 575 } |
| 627 | 576 |
| 628 #endif // !defined(OS_WIN) | 577 #endif // !defined(OS_WIN) |
| 629 | 578 |
| 630 } // namespace remoting | 579 } // namespace remoting |
| OLD | NEW |