Fix response of the disk writer when polkit authorization fails on
[dcpomatic.git] / src / tools / dcpomatic_disk_writer.cc
1 /*
2     Copyright (C) 2019-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/compose.hpp"
22 #include "lib/cross.h"
23 #include "lib/dcpomatic_log.h"
24 #include "lib/digester.h"
25 #include "lib/disk_writer_messages.h"
26 #include "lib/exceptions.h"
27 #include "lib/ext.h"
28 #include "lib/file_log.h"
29 #include "lib/nanomsg.h"
30 #include "lib/version.h"
31 #include "lib/warnings.h"
32
33 #ifdef DCPOMATIC_POSIX
34 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #endif
38
39 #ifdef DCPOMATIC_OSX
40 #include "lib/stdout_log.h"
41 #undef nil
42 extern "C" {
43 #include <lwext4/file_dev.h>
44 }
45 #include <xpc/xpc.h>
46 #endif
47
48 #ifdef DCPOMATIC_LINUX
49 #include <polkit/polkit.h>
50 #include <poll.h>
51 #endif
52
53 #ifdef DCPOMATIC_WINDOWS
54 extern "C" {
55 #include <lwext4/file_windows.h>
56 }
57 #endif
58
59 DCPOMATIC_DISABLE_WARNINGS
60 #include <glibmm.h>
61 DCPOMATIC_ENABLE_WARNINGS
62
63 #include <unistd.h>
64 #include <sys/types.h>
65 #include <boost/filesystem.hpp>
66 #include <boost/algorithm/string.hpp>
67 #include <iostream>
68
69
70 using std::cin;
71 using std::min;
72 using std::string;
73 using std::runtime_error;
74 using std::exception;
75 using std::vector;
76 using boost::optional;
77
78
79 #define SHORT_TIMEOUT 100
80 #define LONG_TIMEOUT 2000
81
82
83 #ifdef DCPOMATIC_LINUX
84 static PolkitAuthority* polkit_authority = 0;
85 #endif
86 static Nanomsg* nanomsg = 0;
87
88 struct Parameters
89 {
90         boost::filesystem::path dcp_path;
91         std::string device;
92         std::string posix_partition;
93 };
94
95 #ifdef DCPOMATIC_LINUX
96 static
97 void
98 polkit_callback (GObject *, GAsyncResult* res, gpointer data)
99 {
100         auto parameters = reinterpret_cast<Parameters*> (data);
101         GError* error = nullptr;
102         auto result = polkit_authority_check_authorization_finish (polkit_authority, res, &error);
103         bool failed = false;
104
105         if (error) {
106                 LOG_DISK("polkit authority check failed (check_authorization_finish failed with %1)", error->message);
107                 failed = true;
108         } else {
109                 if (polkit_authorization_result_get_is_authorized(result)) {
110                         dcpomatic::write (parameters->dcp_path, parameters->device, parameters->posix_partition, nanomsg);
111                 } else {
112                         failed = true;
113                         if (polkit_authorization_result_get_is_challenge(result)) {
114                                 LOG_DISK_NC("polkit authority check failed (challenge)");
115                         } else {
116                                 LOG_DISK_NC("polkit authority check failed (not authorized)");
117                         }
118                 }
119         }
120
121         if (failed && nanomsg) {
122                 nanomsg->send(DISK_WRITER_ERROR "\nCould not obtain authorization to write to the drive\n", LONG_TIMEOUT);
123         }
124
125         delete parameters;
126
127         if (result) {
128                 g_object_unref (result);
129         }
130 }
131 #endif
132
133
134 bool
135 idle ()
136 try
137 {
138         using namespace boost::algorithm;
139
140         optional<string> s = nanomsg->receive (0);
141         if (!s) {
142                 return true;
143         }
144
145         LOG_DISK("Writer receives command: %1", *s);
146
147         if (*s == DISK_WRITER_QUIT) {
148                 exit (EXIT_SUCCESS);
149         } else if (*s == DISK_WRITER_PING) {
150                 nanomsg->send(DISK_WRITER_PONG "\n", LONG_TIMEOUT);
151         } else if (*s == DISK_WRITER_UNMOUNT) {
152                 /* XXX: should do Linux polkit stuff here */
153                 optional<string> xml_head = nanomsg->receive (LONG_TIMEOUT);
154                 optional<string> xml_body = nanomsg->receive (LONG_TIMEOUT);
155                 if (!xml_head || !xml_body) {
156                         LOG_DISK_NC("Failed to receive unmount request");
157                         throw CommunicationFailedError ();
158                 }
159                 bool const success = Drive(*xml_head + *xml_body).unmount();
160                 if (!nanomsg->send (success ? (DISK_WRITER_OK "\n") : (DISK_WRITER_ERROR "\n"), LONG_TIMEOUT)) {
161                         LOG_DISK_NC("CommunicationFailedError in unmount_finished");
162                         throw CommunicationFailedError ();
163                 }
164         } else if (*s == DISK_WRITER_WRITE) {
165                 optional<string> dcp_path = nanomsg->receive (LONG_TIMEOUT);
166                 optional<string> device = nanomsg->receive (LONG_TIMEOUT);
167                 if (!dcp_path || !device) {
168                         LOG_DISK_NC("Failed to receive write request");
169                         throw CommunicationFailedError();
170                 }
171
172                 /* Do some basic sanity checks; this is a bit belt-and-braces but it can't hurt... */
173
174 #ifdef DCPOMATIC_OSX
175                 if (!starts_with(*device, "/dev/disk")) {
176                         LOG_DISK ("Will not write to %1", *device);
177                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
178                         return true;
179                 }
180 #endif
181 #ifdef DCPOMATIC_LINUX
182                 if (!starts_with(*device, "/dev/sd") && !starts_with(*device, "/dev/hd")) {
183                         LOG_DISK ("Will not write to %1", *device);
184                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
185                         return true;
186                 }
187 #endif
188 #ifdef DCPOMATIC_WINDOWS
189                 if (!starts_with(*device, "\\\\.\\PHYSICALDRIVE")) {
190                         LOG_DISK ("Will not write to %1", *device);
191                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
192                         return true;
193                 }
194 #endif
195
196                 bool on_drive_list = false;
197                 bool mounted = false;
198                 for (auto const& i: Drive::get()) {
199                         if (i.device() == *device) {
200                                 on_drive_list = true;
201                                 mounted = i.mounted();
202                         }
203                 }
204
205                 if (!on_drive_list) {
206                         LOG_DISK ("Will not write to %1 as it's not recognised as a drive", *device);
207                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
208                         return true;
209                 }
210                 if (mounted) {
211                         LOG_DISK ("Will not write to %1 as it's mounted", *device);
212                         nanomsg->send(DISK_WRITER_ERROR "\nRefusing to write to this drive\n1\n", LONG_TIMEOUT);
213                         return true;
214                 }
215
216                 LOG_DISK ("Here we go writing %1 to %2", *dcp_path, *device);
217
218 #if defined(DCPOMATIC_LINUX)
219                 polkit_authority = polkit_authority_get_sync (0, 0);
220                 PolkitSubject* subject = polkit_unix_process_new_for_owner (getppid(), 0, -1);
221                 Parameters* parameters = new Parameters;
222                 parameters->dcp_path = *dcp_path;
223                 parameters->device = *device;
224                 parameters->posix_partition = *device;
225                 /* XXX: don't know if this logic is sensible */
226                 if (parameters->posix_partition.size() > 0 && isdigit(parameters->posix_partition[parameters->posix_partition.length() - 1])) {
227                         parameters->posix_partition += "p1";
228                 } else {
229                         parameters->posix_partition += "1";
230                 }
231                 polkit_authority_check_authorization (
232                         polkit_authority, subject, "com.dcpomatic.write-drive", 0, POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION, 0, polkit_callback, parameters
233                         );
234 #elif defined(DCPOMATIC_OSX)
235                 string fast_device = boost::algorithm::replace_first_copy (*device, "/dev/disk", "/dev/rdisk");
236                 dcpomatic::write (*dcp_path, fast_device, fast_device + "s1", nanomsg);
237 #elif defined(DCPOMATIC_WINDOWS)
238                 dcpomatic::write (*dcp_path, *device, "", nanomsg);
239 #endif
240         }
241
242         return true;
243 } catch (exception& e) {
244         LOG_DISK("Exception (from idle): %1", e.what());
245         return true;
246 }
247
248 int
249 main ()
250 {
251 #ifdef DCPOMATIC_OSX
252         /* On macOS this is running as root, so config_path() will be somewhere in root's
253          * home.  Instead, just write to stdout as the macOS process control stuff will
254          * redirect this to a file in /var/log
255          */
256         dcpomatic_log.reset(new StdoutLog(LogEntry::TYPE_DISK));
257         LOG_DISK("dcpomatic_disk_writer %1 started", dcpomatic_git_commit);
258 #else
259         /* XXX: this is a hack, but I expect we'll need logs and I'm not sure if there's
260          * a better place to put them.
261          */
262         dcpomatic_log.reset(new FileLog(config_path() / "disk_writer.log", LogEntry::TYPE_DISK));
263         LOG_DISK_NC("dcpomatic_disk_writer started");
264 #endif
265
266 #ifdef DCPOMATIC_OSX
267         /* I *think* this consumes the notifyd event that we used to start the process, so we only
268          * get started once per notification.
269          */
270         xpc_set_event_stream_handler("com.apple.notifyd.matching", DISPATCH_TARGET_QUEUE_DEFAULT, ^(xpc_object_t) {});
271 #endif
272
273         try {
274                 nanomsg = new Nanomsg (false);
275         } catch (runtime_error& e) {
276                 LOG_DISK_NC("Could not set up nanomsg socket");
277                 exit (EXIT_FAILURE);
278         }
279
280         Glib::RefPtr<Glib::MainLoop> ml = Glib::MainLoop::create ();
281         Glib::signal_timeout().connect(sigc::ptr_fun(&idle), 500);
282         ml->run ();
283 }