ClientConnection.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/WindowServerConnection.h>
  9. // clang-format on
  10. #include <AK/Debug.h>
  11. #include <FileSystemAccessServer/ClientConnection.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<ClientConnection>> s_connections;
  19. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> socket, int client_id)
  20. : IPC::ClientConnection<FileSystemAccessClientEndpoint, FileSystemAccessServerEndpoint>(*this, move(socket), client_id)
  21. {
  22. s_connections.set(client_id, *this);
  23. }
  24. ClientConnection::~ClientConnection()
  25. {
  26. }
  27. void ClientConnection::die()
  28. {
  29. s_connections.remove(client_id());
  30. GUI::Application::the()->quit();
  31. }
  32. RefPtr<GUI::Window> ClientConnection::create_dummy_child_window(i32 window_server_client_id, i32 parent_window_id)
  33. {
  34. auto window = GUI::Window::construct();
  35. window->set_opacity(0);
  36. window->set_frameless(true);
  37. auto rect = GUI::WindowServerConnection::the().get_window_rect_from_client(window_server_client_id, parent_window_id);
  38. window->set_rect(rect);
  39. window->show();
  40. GUI::WindowServerConnection::the().async_set_window_parent_from_client(window_server_client_id, parent_window_id, window->window_id());
  41. return window;
  42. }
  43. void ClientConnection::request_file_handler(i32 window_server_client_id, i32 parent_window_id, String const& path, Core::OpenMode const& requested_access, ShouldPrompt prompt)
  44. {
  45. VERIFY(path.starts_with("/"sv));
  46. bool approved = false;
  47. auto maybe_permissions = m_approved_files.get(path);
  48. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  49. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  50. if (maybe_permissions.has_value())
  51. approved = has_flag(maybe_permissions.value(), relevant_permissions);
  52. if (!approved) {
  53. String access_string;
  54. if (has_flag(requested_access, Core::OpenMode::ReadWrite))
  55. access_string = "read and write";
  56. else if (has_flag(requested_access, Core::OpenMode::ReadOnly))
  57. access_string = "read from";
  58. else if (has_flag(requested_access, Core::OpenMode::WriteOnly))
  59. access_string = "write to";
  60. auto pid = this->socket().peer_pid();
  61. auto exe_link = LexicalPath("/proc").append(String::number(pid)).append("exe").string();
  62. auto exe_path = Core::File::real_path_for(exe_link);
  63. auto exe_name = LexicalPath::basename(exe_path);
  64. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  65. if (prompt == ShouldPrompt::Yes) {
  66. 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);
  67. approved = result == GUI::MessageBox::ExecYes;
  68. } else {
  69. approved = true;
  70. }
  71. if (approved) {
  72. auto new_permissions = relevant_permissions;
  73. if (maybe_permissions.has_value())
  74. new_permissions |= maybe_permissions.value();
  75. m_approved_files.set(path, new_permissions);
  76. }
  77. }
  78. if (approved) {
  79. auto file = Core::File::open(path, requested_access);
  80. if (file.is_error()) {
  81. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", path, file.error());
  82. async_handle_prompt_end(errno, Optional<IPC::File> {}, path);
  83. } else {
  84. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), path);
  85. }
  86. } else {
  87. async_handle_prompt_end(-1, Optional<IPC::File> {}, path);
  88. }
  89. }
  90. void ClientConnection::request_file_read_only_approved(i32 window_server_client_id, i32 parent_window_id, String const& path)
  91. {
  92. request_file_handler(window_server_client_id, parent_window_id, path, Core::OpenMode::ReadOnly, ShouldPrompt::No);
  93. }
  94. void ClientConnection::request_file(i32 window_server_client_id, i32 parent_window_id, String const& path, Core::OpenMode const& requested_access)
  95. {
  96. request_file_handler(window_server_client_id, parent_window_id, path, requested_access, ShouldPrompt::Yes);
  97. }
  98. void ClientConnection::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)
  99. {
  100. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  101. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  102. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  103. auto user_picked_file = GUI::FilePicker::get_open_filepath(main_window, window_title, path_to_view);
  104. prompt_helper(user_picked_file, requested_access);
  105. }
  106. void ClientConnection::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)
  107. {
  108. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  109. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  110. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  111. auto user_picked_file = GUI::FilePicker::get_save_filepath(main_window, name, ext, path_to_view);
  112. prompt_helper(user_picked_file, requested_access);
  113. }
  114. void ClientConnection::prompt_helper(Optional<String> const& user_picked_file, Core::OpenMode const& requested_access)
  115. {
  116. if (user_picked_file.has_value()) {
  117. VERIFY(user_picked_file->starts_with("/"sv));
  118. auto file = Core::File::open(user_picked_file.value(), requested_access);
  119. if (file.is_error()) {
  120. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", user_picked_file.value(), file.error());
  121. async_handle_prompt_end(errno, Optional<IPC::File> {}, user_picked_file);
  122. } else {
  123. auto maybe_permissions = m_approved_files.get(user_picked_file.value());
  124. auto new_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  125. if (maybe_permissions.has_value())
  126. new_permissions |= maybe_permissions.value();
  127. m_approved_files.set(user_picked_file.value(), new_permissions);
  128. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), user_picked_file);
  129. }
  130. } else {
  131. async_handle_prompt_end(-1, Optional<IPC::File> {}, Optional<String> {});
  132. }
  133. }
  134. Messages::FileSystemAccessServer::ExposeWindowServerClientIdResponse ClientConnection::expose_window_server_client_id()
  135. {
  136. return GUI::WindowServerConnection::the().expose_client_id();
  137. }
  138. }