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