ConnectionFromClient.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2021, timmot <tiwwot@protonmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. // FIXME: LibIPC Decoder and Encoder are sensitive to include order here
  7. // clang-format off
  8. #include <LibGUI/ConnectionToWindowServer.h>
  9. // clang-format on
  10. #include <AK/Debug.h>
  11. #include <FileSystemAccessServer/ConnectionFromClient.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/IODevice.h>
  14. #include <LibGUI/Application.h>
  15. #include <LibGUI/FilePicker.h>
  16. #include <LibGUI/MessageBox.h>
  17. namespace FileSystemAccessServer {
  18. static HashMap<int, NonnullRefPtr<ConnectionFromClient>> s_connections;
  19. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
  20. : IPC::ConnectionFromClient<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket), 1)
  21. {
  22. s_connections.set(1, *this);
  23. }
  24. void ConnectionFromClient::die()
  25. {
  26. s_connections.remove(client_id());
  27. GUI::Application::the()->quit();
  28. }
  29. RefPtr<GUI::Window> ConnectionFromClient::create_dummy_child_window(i32 window_server_client_id, i32 parent_window_id)
  30. {
  31. auto window = GUI::Window::construct();
  32. window->set_opacity(0);
  33. window->set_frameless(true);
  34. auto rect = GUI::ConnectionToWindowServer::the().get_window_rect_from_client(window_server_client_id, parent_window_id);
  35. window->set_rect(rect);
  36. window->show();
  37. GUI::ConnectionToWindowServer::the().async_set_window_parent_from_client(window_server_client_id, parent_window_id, window->window_id());
  38. return window;
  39. }
  40. void ConnectionFromClient::request_file_handler(i32 window_server_client_id, i32 parent_window_id, String const& path, Core::OpenMode const& requested_access, ShouldPrompt prompt)
  41. {
  42. VERIFY(path.starts_with("/"sv));
  43. bool approved = false;
  44. auto maybe_permissions = m_approved_files.get(path);
  45. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  46. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  47. if (maybe_permissions.has_value())
  48. approved = has_flag(maybe_permissions.value(), relevant_permissions);
  49. if (!approved) {
  50. String access_string;
  51. if (has_flag(requested_access, Core::OpenMode::ReadWrite))
  52. access_string = "read and write";
  53. else if (has_flag(requested_access, Core::OpenMode::ReadOnly))
  54. access_string = "read from";
  55. else if (has_flag(requested_access, Core::OpenMode::WriteOnly))
  56. access_string = "write to";
  57. auto pid = this->socket().peer_pid().release_value_but_fixme_should_propagate_errors();
  58. auto exe_link = LexicalPath("/proc").append(String::number(pid)).append("exe").string();
  59. auto exe_path = Core::File::real_path_for(exe_link);
  60. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  61. if (prompt == ShouldPrompt::Yes) {
  62. auto exe_name = LexicalPath::basename(exe_path);
  63. auto result = GUI::MessageBox::show(main_window, String::formatted("Allow {} ({}) to {} \"{}\"?", exe_name, pid, access_string, path), "File Permissions Requested", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNo);
  64. approved = result == GUI::MessageBox::ExecResult::Yes;
  65. } else {
  66. approved = true;
  67. }
  68. if (approved) {
  69. auto new_permissions = relevant_permissions;
  70. if (maybe_permissions.has_value())
  71. new_permissions |= maybe_permissions.value();
  72. m_approved_files.set(path, new_permissions);
  73. }
  74. }
  75. if (approved) {
  76. auto file = Core::File::open(path, requested_access);
  77. if (file.is_error()) {
  78. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", path, file.error());
  79. async_handle_prompt_end(errno, Optional<IPC::File> {}, path);
  80. } else {
  81. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), path);
  82. }
  83. } else {
  84. async_handle_prompt_end(-1, Optional<IPC::File> {}, path);
  85. }
  86. }
  87. void ConnectionFromClient::request_file_read_only_approved(i32 window_server_client_id, i32 parent_window_id, String const& path)
  88. {
  89. request_file_handler(window_server_client_id, parent_window_id, path, Core::OpenMode::ReadOnly, ShouldPrompt::No);
  90. }
  91. void ConnectionFromClient::request_file(i32 window_server_client_id, i32 parent_window_id, String const& path, Core::OpenMode const& requested_access)
  92. {
  93. request_file_handler(window_server_client_id, parent_window_id, path, requested_access, ShouldPrompt::Yes);
  94. }
  95. void ConnectionFromClient::prompt_open_file(i32 window_server_client_id, i32 parent_window_id, String const& window_title, String const& path_to_view, Core::OpenMode const& requested_access)
  96. {
  97. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  98. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  99. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  100. auto user_picked_file = GUI::FilePicker::get_open_filepath(main_window, window_title, path_to_view);
  101. prompt_helper(user_picked_file, requested_access);
  102. }
  103. void ConnectionFromClient::prompt_save_file(i32 window_server_client_id, i32 parent_window_id, String const& name, String const& ext, String const& path_to_view, Core::OpenMode const& requested_access)
  104. {
  105. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  106. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  107. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  108. auto user_picked_file = GUI::FilePicker::get_save_filepath(main_window, name, ext, path_to_view);
  109. prompt_helper(user_picked_file, requested_access);
  110. }
  111. void ConnectionFromClient::prompt_helper(Optional<String> const& user_picked_file, Core::OpenMode const& requested_access)
  112. {
  113. if (user_picked_file.has_value()) {
  114. VERIFY(user_picked_file->starts_with("/"sv));
  115. auto file = Core::File::open(user_picked_file.value(), requested_access);
  116. if (file.is_error()) {
  117. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", user_picked_file.value(), file.error());
  118. async_handle_prompt_end(errno, Optional<IPC::File> {}, user_picked_file);
  119. } else {
  120. auto maybe_permissions = m_approved_files.get(user_picked_file.value());
  121. auto new_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  122. if (maybe_permissions.has_value())
  123. new_permissions |= maybe_permissions.value();
  124. m_approved_files.set(user_picked_file.value(), new_permissions);
  125. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), user_picked_file);
  126. }
  127. } else {
  128. async_handle_prompt_end(-1, Optional<IPC::File> {}, Optional<String> {});
  129. }
  130. }
  131. Messages::FileSystemAccessServer::ExposeWindowServerClientIdResponse ConnectionFromClient::expose_window_server_client_id()
  132. {
  133. return GUI::ConnectionToWindowServer::the().expose_client_id();
  134. }
  135. }