ClientConnection.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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(i32 window_server_client_id, i32 parent_window_id, String const& path, Core::OpenMode const& requested_access)
  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. 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);
  66. approved = result == GUI::MessageBox::ExecYes;
  67. if (approved) {
  68. auto new_permissions = relevant_permissions;
  69. if (maybe_permissions.has_value())
  70. new_permissions |= maybe_permissions.value();
  71. m_approved_files.set(path, new_permissions);
  72. }
  73. }
  74. if (approved) {
  75. auto file = Core::File::open(path, requested_access);
  76. if (file.is_error()) {
  77. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", path, file.error());
  78. async_handle_prompt_end(errno, Optional<IPC::File> {}, path);
  79. } else {
  80. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), path);
  81. }
  82. } else {
  83. async_handle_prompt_end(-1, Optional<IPC::File> {}, path);
  84. }
  85. }
  86. 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)
  87. {
  88. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  89. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  90. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  91. auto user_picked_file = GUI::FilePicker::get_open_filepath(main_window, window_title, path_to_view);
  92. prompt_helper(user_picked_file, requested_access);
  93. }
  94. 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)
  95. {
  96. auto relevant_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  97. VERIFY(relevant_permissions != Core::OpenMode::NotOpen);
  98. auto main_window = create_dummy_child_window(window_server_client_id, parent_window_id);
  99. auto user_picked_file = GUI::FilePicker::get_save_filepath(main_window, name, ext, path_to_view);
  100. prompt_helper(user_picked_file, requested_access);
  101. }
  102. void ClientConnection::prompt_helper(Optional<String> const& user_picked_file, Core::OpenMode const& requested_access)
  103. {
  104. if (user_picked_file.has_value()) {
  105. VERIFY(user_picked_file->starts_with("/"sv));
  106. auto file = Core::File::open(user_picked_file.value(), requested_access);
  107. if (file.is_error()) {
  108. dbgln("FileSystemAccessServer: Couldn't open {}, error {}", user_picked_file.value(), file.error());
  109. async_handle_prompt_end(errno, Optional<IPC::File> {}, user_picked_file);
  110. } else {
  111. auto maybe_permissions = m_approved_files.get(user_picked_file.value());
  112. auto new_permissions = requested_access & (Core::OpenMode::ReadOnly | Core::OpenMode::WriteOnly);
  113. if (maybe_permissions.has_value())
  114. new_permissions |= maybe_permissions.value();
  115. m_approved_files.set(user_picked_file.value(), new_permissions);
  116. async_handle_prompt_end(0, IPC::File(file.value()->leak_fd(), IPC::File::CloseAfterSending), user_picked_file);
  117. }
  118. } else {
  119. async_handle_prompt_end(-1, Optional<IPC::File> {}, Optional<String> {});
  120. }
  121. }
  122. Messages::FileSystemAccessServer::ExposeWindowServerClientIdResponse ClientConnection::expose_window_server_client_id()
  123. {
  124. return GUI::WindowServerConnection::the().expose_client_id();
  125. }
  126. }