ConnectionFromClient.cpp 7.2 KB

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