2 Copyright (C) 2013-2022 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
22 /** @file src/tools/dcpomatic_kdm_cli.cc
23 * @brief Command-line program to generate KDMs.
28 #include "cinema_list.h"
30 #include "dkdm_wrapper.h"
32 #include "exceptions.h"
34 #include "kdm_with_metadata.h"
37 #include <dcp/certificate.h>
38 #include <dcp/decrypted_kdm.h>
39 #include <dcp/encrypted_kdm.h>
40 #include <dcp/filesystem.h>
44 using std::dynamic_pointer_cast;
46 using std::make_shared;
48 using std::runtime_error;
49 using std::shared_ptr;
52 using boost::optional;
54 #if BOOST_VERSION >= 106100
55 using namespace boost::placeholders;
57 using namespace dcpomatic;
61 help (std::function<void (string)> out)
63 out (String::compose("Syntax: %1 [OPTION] [COMMAND] <FILM|CPL-ID|DKDM>", program_name));
65 out ("create create KDMs; default if no other command is specified");
66 out (variant::insert_dcpomatic("list-cinemas list known cinemas from %1 settings"));
67 out (variant::insert_dcpomatic("list-dkdm-cpls list CPLs for which %1 has DKDMs"));
68 out (variant::insert_dcpomatic("add-dkdm add DKDM to %1's list"));
69 out (" -h, --help show this help");
70 out (" -o, --output <path> output file or directory");
71 out (" -K, --filename-format <format> filename format for KDMs");
72 out (" -Z, --container-name-format <format> filename format for ZIP containers");
73 out (" -f, --valid-from <time> valid from time (e.g. \"2013-09-28T01:41:51+04:00\", \"2018-01-01T12:00:30\") or \"now\"");
74 out (" -t, --valid-to <time> valid to time (e.g. \"2014-09-28T01:41:51\")");
75 out (" -d, --valid-duration <duration> valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")");
76 out (" -F, --formulation <formulation> modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]");
77 out (" -p, --disable-forensic-marking-picture disable forensic marking of pictures essences");
78 out (" -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)");
79 out (" -e, --email email KDMs to cinemas");
80 out (" -z, --zip ZIP each cinema's KDMs into its own file");
81 out (" -v, --verbose be verbose");
82 out (" -c, --cinema <name|email> cinema name (when using -C) or name/email (to filter cinemas)");
83 out (" -S, --screen <name> screen name (when using -C) or screen name (to filter screens when using -c)");
84 out (" -C, --projector-certificate <file> file containing projector certificate");
85 out (" -T, --trusted-device-certificate <file> file containing a trusted device's certificate");
86 out (" --decryption-key <file> file containing the private key which can decrypt the given DKDM");
87 out (variant::insert_dcpomatic(" (%1's configured private key will be used otherwise)"));
88 out (" --cinemas-file <file> use the given file as a list of cinemas instead of the current configuration");
90 out (variant::insert_dcpomatic("CPL-ID must be the ID of a CPL that is mentioned in %1's DKDM list."));
94 out ("Create KDMs for my_great_movie to play in all of Fred's Cinema's screens for the next two weeks and zip them up.");
95 out (variant::insert_dcpomatic("(Fred's Cinema must have been set up in %1's KDM window)"));
97 out (String::compose("\t%1 -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie", program_name));
101 class KDMCLIError : public std::runtime_error
104 KDMCLIError (std::string message)
105 : std::runtime_error (String::compose("%1: %2", program_name, message).c_str())
110 static boost::posix_time::time_duration
111 duration_from_string (string d)
114 char unit_buf[64] = "\0";
115 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
116 string const unit (unit_buf);
119 throw KDMCLIError (String::compose("could not understand duration \"%1\"", d));
122 if (unit == "year" || unit == "years") {
123 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
124 } else if (unit == "week" || unit == "weeks") {
125 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
126 } else if (unit == "day" || unit == "days") {
127 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
128 } else if (unit == "hour" || unit == "hours") {
129 return boost::posix_time::time_duration (N, 0, 0, 0);
132 throw KDMCLIError (String::compose("could not understand duration \"%1\"", d));
146 list<KDMWithMetadataPtr> kdms,
148 boost::filesystem::path output,
149 dcp::NameFormat container_name_format,
150 dcp::NameFormat filename_format,
152 std::function<void (string)> out
156 int const N = write_zip_files (
159 container_name_format,
161 bind (&always_overwrite)
165 out (String::compose("Wrote %1 ZIP files to %2", N, output));
168 int const N = write_files (
169 kdms, output, filename_format,
170 bind (&always_overwrite)
174 out (String::compose("Wrote %1 KDM files to %2", N, output));
183 ScreenDetails(CinemaID const& cinema_id, Cinema const& cinema, Screen const& screen)
184 : cinema_id(cinema_id)
198 vector<ScreenDetails> const& screens,
199 boost::filesystem::path film_dir,
201 boost::filesystem::path output,
202 dcp::NameFormat container_name_format,
203 dcp::NameFormat filename_format,
204 dcp::LocalTime valid_from,
205 dcp::LocalTime valid_to,
206 dcp::Formulation formulation,
207 bool disable_forensic_marking_picture,
208 optional<int> disable_forensic_marking_audio,
211 std::function<void (string)> out
214 shared_ptr<Film> film;
216 film = make_shared<Film>(film_dir);
217 film->read_metadata ();
219 out (String::compose("Read film %1", film->name()));
221 } catch (std::exception& e) {
222 throw KDMCLIError (String::compose("error reading film \"%1\" (%2)", film_dir.string(), e.what()));
225 /* XXX: allow specification of this */
226 vector<CPLSummary> cpls = film->cpls ();
228 throw KDMCLIError ("no CPLs found in film");
229 } else if (cpls.size() > 1) {
230 throw KDMCLIError ("more than one CPL found in film");
233 auto cpl = cpls.front().cpl_file;
235 std::vector<KDMCertificatePeriod> period_checks;
238 list<KDMWithMetadataPtr> kdms;
239 for (auto screen_details: screens) {
240 std::function<dcp::DecryptedKDM (dcp::LocalTime, dcp::LocalTime)> make_kdm = [film, cpl](dcp::LocalTime begin, dcp::LocalTime end) {
241 return film->make_kdm(cpl, begin, end);
243 auto p = kdm_for_screen(
245 screen_details.cinema_id,
246 screen_details.cinema,
247 screen_details.screen,
251 disable_forensic_marking_picture,
252 disable_forensic_marking_audio,
261 period_checks.begin(),
263 [](KDMCertificatePeriod const& p) { return p.overlap == KDMCertificateOverlap::KDM_OUTSIDE_CERTIFICATE; }
264 ) != period_checks.end()) {
266 "Some KDMs would have validity periods which are completely outside the recipient certificate periods. Such KDMs are very unlikely to work, so will not be created."
271 period_checks.begin(),
273 [](KDMCertificatePeriod const& p) { return p.overlap == KDMCertificateOverlap::KDM_OVERLAPS_CERTIFICATE; }
274 ) != period_checks.end()) {
275 out("For some of these KDMs the recipient certificate's validity period will not cover the whole of the KDM validity period. This might cause problems with the KDMs.");
278 write_files (kdms, zip, output, container_name_format, filename_format, verbose, out);
280 send_emails ({kdms}, container_name_format, filename_format, film->dcp_name(), {});
282 } catch (FileError& e) {
283 throw KDMCLIError (String::compose("%1 (%2)", e.what(), e.file().string()));
289 optional<dcp::EncryptedKDM>
290 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
292 for (auto i: group->children()) {
293 auto g = dynamic_pointer_cast<DKDMGroup>(i);
295 auto dkdm = sub_find_dkdm (g, cpl_id);
300 auto d = dynamic_pointer_cast<DKDM>(i);
302 if (d->dkdm().cpl_id() == cpl_id) {
313 optional<dcp::EncryptedKDM>
314 find_dkdm (string cpl_id)
316 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
323 dcp::DecryptedKDM dkdm,
324 dcp::Certificate target,
325 vector<string> trusted_devices,
326 dcp::LocalTime valid_from,
327 dcp::LocalTime valid_to,
328 dcp::Formulation formulation,
329 bool disable_forensic_marking_picture,
330 optional<int> disable_forensic_marking_audio
333 /* Signer for new KDM */
334 auto signer = Config::instance()->signer_chain ();
335 if (!signer->valid ()) {
336 throw KDMCLIError ("signing certificate chain is invalid.");
339 /* Make a new empty KDM and add the keys from the DKDM to it */
340 dcp::DecryptedKDM kdm (
343 dkdm.annotation_text().get_value_or(""),
344 dkdm.content_title_text(),
345 dcp::LocalTime().as_string()
348 for (auto const& j: dkdm.keys()) {
352 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
359 vector<ScreenDetails> const& screens,
360 dcp::DecryptedKDM dkdm,
362 boost::filesystem::path output,
363 dcp::NameFormat container_name_format,
364 dcp::NameFormat filename_format,
365 dcp::LocalTime valid_from,
366 dcp::LocalTime valid_to,
367 dcp::Formulation formulation,
368 bool disable_forensic_marking_picture,
369 optional<int> disable_forensic_marking_audio,
372 std::function<void (string)> out
375 dcp::NameFormat::Map values;
378 list<KDMWithMetadataPtr> kdms;
379 for (auto const& screen_details: screens) {
380 if (!screen_details.screen.recipient) {
384 auto const kdm = kdm_from_dkdm(
386 screen_details.screen.recipient.get(),
387 screen_details.screen.trusted_device_thumbprints(),
391 disable_forensic_marking_picture,
392 disable_forensic_marking_audio
395 dcp::NameFormat::Map name_values;
396 name_values['c'] = screen_details.cinema.name;
397 name_values['s'] = screen_details.screen.name;
398 name_values['f'] = kdm.content_title_text();
399 name_values['b'] = valid_from.date() + " " + valid_from.time_of_day(true, false);
400 name_values['e'] = valid_to.date() + " " + valid_to.time_of_day(true, false);
401 name_values['i'] = kdm.cpl_id();
403 kdms.push_back(make_shared<KDMWithMetadata>(name_values, screen_details.cinema_id, screen_details.cinema.emails, kdm));
405 write_files (kdms, zip, output, container_name_format, filename_format, verbose, out);
407 send_emails ({kdms}, container_name_format, filename_format, dkdm.annotation_text().get_value_or(""), {});
409 } catch (FileError& e) {
410 throw KDMCLIError (String::compose("%1 (%2)", e.what(), e.file().string()));
417 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent, std::function<void (string)> out)
419 auto const indent_string = string(indent, ' ');
422 out (indent_string + group->name());
424 for (auto i: group->children()) {
425 auto g = dynamic_pointer_cast<DKDMGroup>(i);
427 dump_dkdm_group (g, indent + 2, out);
429 auto d = dynamic_pointer_cast<DKDM>(i);
431 out (indent_string + d->dkdm().cpl_id());
439 time_from_string(string time)
445 if (time.length() > 10 && time[10] == ' ') {
449 return dcp::LocalTime(time);
454 kdm_cli (int argc, char* argv[], std::function<void (string)> out)
457 boost::filesystem::path output = dcp::filesystem::current_path();
458 auto container_name_format = Config::instance()->kdm_container_name_format();
459 auto filename_format = Config::instance()->kdm_filename_format();
460 /* either a cinema name to search for, or the name of a cinema to associate with certificate */
461 optional<string> cinema_name;
462 /* either a screen name to search for, or the name of a screen to associate with certificate */
463 optional<string> screen_name;
464 /* a certificate that we will use to make up a temporary cinema and screen */
465 optional<boost::filesystem::path> projector_certificate;
466 optional<boost::filesystem::path> decryption_key;
467 /* trusted devices that we will use to make up a temporary cinema and screen */
468 vector<TrustedDevice> trusted_devices;
469 optional<dcp::EncryptedKDM> dkdm;
470 optional<dcp::LocalTime> valid_from;
471 optional<dcp::LocalTime> valid_to;
473 string command = "create";
474 optional<string> duration_string;
475 bool verbose = false;
476 dcp::Formulation formulation = dcp::Formulation::MODIFIED_TRANSITIONAL_1;
477 bool disable_forensic_marking_picture = false;
478 optional<int> disable_forensic_marking_audio;
480 optional<boost::filesystem::path> cinemas_file;
482 program_name = argv[0];
484 /* Reset getopt() so we can call this method several times in one test process */
487 int option_index = 0;
489 static struct option long_options[] = {
490 { "help", no_argument, 0, 'h'},
491 { "output", required_argument, 0, 'o'},
492 { "filename-format", required_argument, 0, 'K'},
493 { "container-name-format", required_argument, 0, 'Z'},
494 { "valid-from", required_argument, 0, 'f'},
495 { "valid-to", required_argument, 0, 't'},
496 { "valid-duration", required_argument, 0, 'd'},
497 { "formulation", required_argument, 0, 'F' },
498 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
499 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
500 { "email", no_argument, 0, 'e' },
501 { "zip", no_argument, 0, 'z' },
502 { "verbose", no_argument, 0, 'v' },
503 { "cinema", required_argument, 0, 'c' },
504 { "screen", required_argument, 0, 'S' },
505 { "projector-certificate", required_argument, 0, 'C' },
506 { "trusted-device-certificate", required_argument, 0, 'T' },
507 { "decryption-key", required_argument, 0, 'G' },
508 { "cinemas-file", required_argument, 0, 'E' },
512 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pae::zvc:S:C:T:E:G:", long_options, &option_index);
526 filename_format = dcp::NameFormat (optarg);
529 container_name_format = dcp::NameFormat (optarg);
532 valid_from = time_from_string(optarg);
535 valid_to = dcp::LocalTime(optarg);
538 duration_string = optarg;
541 if (string(optarg) == "modified-transitional-1") {
542 formulation = dcp::Formulation::MODIFIED_TRANSITIONAL_1;
543 } else if (string(optarg) == "multiple-modified-transitional-1") {
544 formulation = dcp::Formulation::MULTIPLE_MODIFIED_TRANSITIONAL_1;
545 } else if (string(optarg) == "dci-any") {
546 formulation = dcp::Formulation::DCI_ANY;
547 } else if (string(optarg) == "dci-specific") {
548 formulation = dcp::Formulation::DCI_SPECIFIC;
550 throw KDMCLIError ("unrecognised KDM formulation " + string (optarg));
554 disable_forensic_marking_picture = true;
557 disable_forensic_marking_audio = 0;
558 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
559 disable_forensic_marking_audio = atoi (argv[optind++]);
561 disable_forensic_marking_audio = atoi (optarg);
574 cinema_name = optarg;
577 screen_name = optarg;
580 projector_certificate = optarg;
583 trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
586 decryption_key = optarg;
589 cinemas_file = optarg;
594 vector<string> commands = {
601 if (optind < argc - 1) {
602 /* Command with some KDM / CPL / whever specified afterwards */
603 command = argv[optind++];
604 } else if (optind < argc) {
605 /* Look for a valid command, hoping that it's not the name of the KDM / CPL / whatever */
606 if (std::find(commands.begin(), commands.end(), argv[optind]) != commands.end()) {
607 command = argv[optind++];
611 if (std::find(commands.begin(), commands.end(), command) == commands.end()) {
612 throw KDMCLIError(String::compose("Unrecognised command %1", command));
616 Config::instance()->set_cinemas_file(*cinemas_file);
619 /* If we've been given a certificate we can make up a temporary cinema and screen (not written to the
620 * database) to then use for making KDMs.
622 optional<Cinema> temp_cinema;
623 optional<Screen> temp_screen;
624 if (projector_certificate) {
625 temp_cinema = Cinema(cinema_name.get_value_or(""), {}, "", dcp::UTCOffset());
626 dcp::CertificateChain chain(dcp::file_to_string(*projector_certificate));
627 temp_screen = Screen(screen_name.get_value_or(""), "", chain.leaf(), boost::none, trusted_devices);
630 if (command == "list-cinemas") {
632 for (auto const& cinema: cinemas.cinemas()) {
633 out(String::compose("%1 (%2)", cinema.second.name, Email::address_list(cinema.second.emails)));
638 if (command == "list-dkdm-cpls") {
639 dump_dkdm_group (Config::instance()->dkdms(), 0, out);
643 if (optind >= argc) {
644 throw KDMCLIError("no film, CPL ID or DKDM specified");
647 if (command == "add-dkdm") {
648 auto dkdms = Config::instance()->dkdms();
649 dkdms->add(make_shared<DKDM>(dcp::EncryptedKDM(dcp::file_to_string(argv[optind]))));
650 Config::instance()->write_config();
654 if (!duration_string && !valid_to) {
655 throw KDMCLIError ("you must specify a --valid-duration or --valid-to");
659 throw KDMCLIError ("you must specify --valid-from");
662 if (optind >= argc) {
663 throw KDMCLIError ("no film, CPL ID or DKDM specified");
666 vector<ScreenDetails> screens;
670 throw KDMCLIError("you must specify either a cinema or one or more screens using certificate files");
673 CinemaList cinema_list;
674 if (auto cinema = cinema_list.cinema_by_name_or_email(*cinema_name)) {
676 for (auto screen: cinema_list.screens_by_cinema_and_name(cinema->first, *screen_name)) {
677 screens.push_back({cinema->first, cinema->second, screen.second});
680 for (auto screen: cinema_list.screens(cinema->first)) {
681 screens.push_back({cinema->first, cinema->second, screen.second});
686 DCPOMATIC_ASSERT(temp_screen);
687 screens.push_back({CinemaID(0), *temp_cinema, *temp_screen});
690 if (duration_string) {
691 valid_to = valid_from.get();
692 valid_to->add(duration_from_string(*duration_string));
696 out(String::compose("Making KDMs valid from %1 to %2", valid_from->as_string(), valid_to->as_string()));
699 string const thing = argv[optind];
700 if (dcp::filesystem::is_directory(thing) && dcp::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
706 container_name_format,
711 disable_forensic_marking_picture,
712 disable_forensic_marking_audio,
718 if (dcp::filesystem::is_regular_file(thing)) {
719 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
721 dkdm = find_dkdm (thing);
725 throw KDMCLIError ("could not find film or CPL ID corresponding to " + thing);
728 string const key = decryption_key ? dcp::file_to_string(*decryption_key) : Config::instance()->decryption_chain()->key().get();
732 dcp::DecryptedKDM(*dkdm, key),
735 container_name_format,
740 disable_forensic_marking_picture,
741 disable_forensic_marking_audio,
749 } catch (std::exception& e) {
750 return string(e.what());