remote_webauthn_main.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2021 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. #include "remoting/host/webauthn/remote_webauthn_main.h"
  5. #include "base/at_exit.h"
  6. #include "base/command_line.h"
  7. #include "base/files/file.h"
  8. #include "base/logging.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/message_loop/message_pump_type.h"
  11. #include "base/run_loop.h"
  12. #include "base/task/single_thread_task_executor.h"
  13. #include "base/task/thread_pool/thread_pool_instance.h"
  14. #include "base/threading/thread_task_runner_handle.h"
  15. #include "build/build_config.h"
  16. #include "mojo/core/embedder/embedder.h"
  17. #include "mojo/core/embedder/scoped_ipc_support.h"
  18. #include "remoting/base/auto_thread_task_runner.h"
  19. #include "remoting/base/logging.h"
  20. #include "remoting/host/base/host_exit_codes.h"
  21. #include "remoting/host/chromoting_host_services_client.h"
  22. #include "remoting/host/native_messaging/native_messaging_pipe.h"
  23. #include "remoting/host/native_messaging/pipe_messaging_channel.h"
  24. #include "remoting/host/webauthn/remote_webauthn_caller_security_utils.h"
  25. #include "remoting/host/webauthn/remote_webauthn_native_messaging_host.h"
  26. #if BUILDFLAG(IS_WIN)
  27. #include <windows.h>
  28. #endif // BUILDFLAG(IS_WIN)
  29. namespace remoting {
  30. int RemoteWebAuthnMain(int argc, char** argv) {
  31. base::AtExitManager exit_manager;
  32. base::SingleThreadTaskExecutor task_executor(base::MessagePumpType::IO);
  33. base::ThreadPoolInstance::Create("RemoteWebAuthn");
  34. auto task_runner = base::ThreadTaskRunnerHandle::Get();
  35. base::CommandLine::Init(argc, argv);
  36. InitHostLogging();
  37. if (!IsLaunchedByTrustedProcess()) {
  38. LOG(ERROR) << "Current process is not launched by a trusted process.";
  39. return kNoPermissionExitCode;
  40. }
  41. if (!ChromotingHostServicesClient::Initialize()) {
  42. return kInitializationFailed;
  43. }
  44. mojo::core::Init();
  45. mojo::core::ScopedIPCSupport ipc_support(
  46. task_runner, mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
  47. base::File read_file;
  48. base::File write_file;
  49. #if BUILDFLAG(IS_POSIX)
  50. read_file = base::File(STDIN_FILENO);
  51. write_file = base::File(STDOUT_FILENO);
  52. #elif BUILDFLAG(IS_WIN)
  53. // GetStdHandle() returns pseudo-handles for stdin and stdout even if
  54. // the hosting executable specifies "Windows" subsystem. However the
  55. // returned handles are invalid in that case unless standard input and
  56. // output are redirected to a pipe or file.
  57. read_file = base::File(GetStdHandle(STD_INPUT_HANDLE));
  58. write_file = base::File(GetStdHandle(STD_OUTPUT_HANDLE));
  59. // After the native messaging channel starts, the native messaging reader
  60. // will keep doing blocking read operations on the input named pipe.
  61. // If any other thread tries to perform any operation on STDIN, it will also
  62. // block because the input named pipe is synchronous (non-overlapped).
  63. // It is pretty common for a DLL to query the device info (GetFileType) of
  64. // the STD* handles at startup. So any LoadLibrary request can potentially
  65. // be blocked. To prevent that from happening we close STDIN and STDOUT
  66. // handles as soon as we retrieve the corresponding file handles.
  67. SetStdHandle(STD_INPUT_HANDLE, nullptr);
  68. SetStdHandle(STD_OUTPUT_HANDLE, nullptr);
  69. #endif
  70. base::RunLoop run_loop;
  71. NativeMessagingPipe native_messaging_pipe;
  72. auto channel = std::make_unique<PipeMessagingChannel>(std::move(read_file),
  73. std::move(write_file));
  74. #if BUILDFLAG(IS_POSIX)
  75. PipeMessagingChannel::ReopenStdinStdout();
  76. #endif // BUILDFLAG(IS_POSIX)
  77. auto native_messaging_host =
  78. std::make_unique<RemoteWebAuthnNativeMessagingHost>(
  79. base::MakeRefCounted<AutoThreadTaskRunner>(task_runner,
  80. run_loop.QuitClosure()));
  81. native_messaging_host->Start(&native_messaging_pipe);
  82. native_messaging_pipe.Start(std::move(native_messaging_host),
  83. std::move(channel));
  84. run_loop.Run();
  85. // Block until tasks blocking shutdown have completed their execution.
  86. base::ThreadPoolInstance::Get()->Shutdown();
  87. return kSuccessExitCode;
  88. }
  89. } // namespace remoting