Move ServerDescription into its own header.
[dcpomatic.git] / src / lib / config.cc
index 7e3de966b4bfc79a40cc3d64508c74a47fbe2c27..6a3734dcc7091171290d070a8ffba0c8e0823c54 100644 (file)
@@ -22,6 +22,7 @@
 #include "filter.h"
 #include "ratio.h"
 #include "types.h"
+#include "log.h"
 #include "dcp_content_type.h"
 #include "cinema_sound_processor.h"
 #include "colour_conversion.h"
@@ -35,6 +36,7 @@
 #include <glib.h>
 #include <boost/filesystem.hpp>
 #include <boost/algorithm/string.hpp>
+#include <boost/foreach.hpp>
 #include <cstdlib>
 #include <fstream>
 
@@ -68,6 +70,7 @@ Config::set_defaults ()
        _server_port_base = 6192;
        _use_any_servers = true;
        _servers.clear ();
+       _only_servers_encode = false;
        _tms_protocol = PROTOCOL_SCP;
        _tms_ip = "";
        _tms_path = ".";
@@ -118,11 +121,12 @@ Config::restore_defaults ()
 void
 Config::read ()
 {
-       if (!boost::filesystem::exists (file ())) {
+       if (!have_existing ()) {
                /* Make a new set of signing certificates and key */
                _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
-               /* And decryption keys */
-               make_decryption_keys ();
+               /* And similar for decryption of KDMs */
+               _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
+               write ();
                return;
        }
 
@@ -153,6 +157,7 @@ Config::read ()
                }
        }
 
+       _only_servers_encode = f.optional_bool_child ("OnlyServersEncode").get_value_or (false);
        _tms_protocol = static_cast<Protocol> (f.optional_number_child<int> ("TMSProtocol").get_value_or (static_cast<int> (PROTOCOL_SCP)));
        _tms_ip = f.string_child ("TMSIP");
        _tms_path = f.string_child ("TMSPath");
@@ -186,6 +191,8 @@ Config::read ()
                _dcp_issuer = f.string_child ("DCPIssuer");
        }
 
+       _dcp_creator = f.optional_string_child ("DCPCreator").get_value_or ("");
+
        if (version && version.get() >= 2) {
                _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
        } else {
@@ -236,9 +243,8 @@ Config::read ()
        if (signer) {
                shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
                /* Read the signing certificates and private key in from the config file */
-               list<cxml::NodePtr> certificates = signer->node_children ("Certificate");
-               for (list<cxml::NodePtr>::const_iterator i = certificates.begin(); i != certificates.end(); ++i) {
-                       c->add (dcp::Certificate ((*i)->content ()));
+               BOOST_FOREACH (cxml::NodePtr i, signer->node_children ("Certificate")) {
+                       c->add (dcp::Certificate (i->content ()));
                }
                c->set_key (signer->string_child ("PrivateKey"));
                _signer_chain = c;
@@ -247,31 +253,22 @@ Config::read ()
                _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
        }
 
-       if (f.optional_string_child ("DecryptionCertificate")) {
-               _decryption_certificate = dcp::Certificate (f.string_child ("DecryptionCertificate"));
-       }
-
-       if (f.optional_string_child ("DecryptionPrivateKey")) {
-               _decryption_private_key = f.string_child ("DecryptionPrivateKey");
-       }
-
-       if (!f.optional_string_child ("DecryptionCertificate") || !f.optional_string_child ("DecryptionPrivateKey")) {
-               /* Generate our own decryption certificate and key if either is not present in config */
-               make_decryption_keys ();
+       cxml::NodePtr decryption = f.optional_node_child ("Decryption");
+       if (decryption) {
+               shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
+               BOOST_FOREACH (cxml::NodePtr i, decryption->node_children ("Certificate")) {
+                       c->add (dcp::Certificate (i->content ()));
+               }
+               c->set_key (signer->string_child ("PrivateKey"));
+               _decryption_chain = c;
+       } else {
+               _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
        }
 }
 
-void
-Config::make_decryption_keys ()
-{
-       dcp::CertificateChain c (openssl_path ());
-       _decryption_certificate = c.leaf ();
-       _decryption_private_key = c.key().get ();
-}
-
 /** @return Filename to write configuration to */
 boost::filesystem::path
-Config::file () const
+Config::file ()
 {
        boost::filesystem::path p;
 #ifdef DCPOMATIC_OSX
@@ -302,9 +299,9 @@ Config::instance ()
                        /* configuration load failed; never mind, just
                           stick with the default.
                        */
-                       cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
+                       cerr << "dcpomatic: warning: configuration did not load (" << e.what() << "); using defaults\n";
                } catch (...) {
-                       cerr << "dcpomatic: failed to load configuration\n";
+                       cerr << "dcpomatic: warning: configuration did not load; using defaults\n";
                }
        }
 
@@ -328,6 +325,7 @@ Config::write () const
                root->add_child("Server")->add_child_text (*i);
        }
 
+       root->add_child("OnlyServersEncode")->add_child_text (_only_servers_encode ? "1" : "0");
        root->add_child("TMSProtocol")->add_child_text (raw_convert<string> (_tms_protocol));
        root->add_child("TMSIP")->add_child_text (_tms_ip);
        root->add_child("TMSPath")->add_child_text (_tms_path);
@@ -346,6 +344,7 @@ Config::write () const
                root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
        }
        root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
+       root->add_child("DCPCreator")->add_child_text (_dcp_creator);
 
        _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
 
@@ -378,14 +377,16 @@ Config::write () const
 #endif
 
        xmlpp::Element* signer = root->add_child ("Signer");
-       dcp::CertificateChain::List certs = _signer_chain->root_to_leaf ();
-       for (dcp::CertificateChain::List::const_iterator i = certs.begin(); i != certs.end(); ++i) {
-               signer->add_child("Certificate")->add_child_text (i->certificate (true));
+       BOOST_FOREACH (dcp::Certificate const & i, _signer_chain->root_to_leaf ()) {
+               signer->add_child("Certificate")->add_child_text (i.certificate (true));
        }
        signer->add_child("PrivateKey")->add_child_text (_signer_chain->key().get ());
 
-       root->add_child("DecryptionCertificate")->add_child_text (_decryption_certificate.certificate (true));
-       root->add_child("DecryptionPrivateKey")->add_child_text (_decryption_private_key);
+       xmlpp::Element* decryption = root->add_child ("Decryption");
+       BOOST_FOREACH (dcp::Certificate const & i, _decryption_chain->root_to_leaf ()) {
+               decryption->add_child("Certificate")->add_child_text (i.certificate (true));
+       }
+       decryption->add_child("PrivateKey")->add_child_text (_decryption_chain->key().get ());
 
        for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
                root->add_child("History")->add_child_text (i->string ());
@@ -462,3 +463,9 @@ Config::add_to_history (boost::filesystem::path p)
 
        changed ();
 }
+
+bool
+Config::have_existing ()
+{
+       return boost::filesystem::exists (file ());
+}