Incomplete encryption of private keys.
[dcpomatic.git] / src / lib / config.cc
index 99115f2d765d7923a043f9e7f7b88d08743a5262..e61eea3a6f89f7cd916be30b9cb8a54aa595dc95 100644 (file)
@@ -176,7 +176,7 @@ Config::set_defaults ()
        _player_watermark_theatre = "";
        _player_watermark_period = 1;
        _player_watermark_duration = 50;
-       _allow_spl_editing = true;
+       _player_lock_file = boost::none;
 #endif
 
        _allowed_dcp_frame_rates.clear ();
@@ -233,7 +233,7 @@ void
 Config::read ()
 try
 {
-#ifdef DCPOMATIC_VARIANT_SWAROOP
+#if defined(DCPOMATIC_VARIANT_SWAROOP) && defined(DCPOMATIC_LINUX)
        if (geteuid() == 0) {
                /* Take ownership of the config file if we're root */
                chown (config_file().string().c_str(), 0, 0);
@@ -528,7 +528,7 @@ try
        BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("RequiredMonitor")) {
                _required_monitors.push_back(Monitor(i));
        }
-       _allow_spl_editing = f.optional_bool_child("AllowSPLEditing").get_value_or(true);
+       _player_lock_file = f.optional_string_child("PlayerLockFile");
 #endif
 
        /* Replace any cinemas from config.xml with those from the configured file */
@@ -656,8 +656,8 @@ Config::write_config () const
        }
        if (_default_container) {
                /* [XML:opt] DefaultContainer ID of default container
-                * to use when creating new films (<code>185</code>,<code>239</code> or
-                * <code>190</code>).
+                  to use when creating new films (<code>185</code>,<code>239</code> or
+                  <code>190</code>).
                */
                root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
        }
@@ -771,7 +771,18 @@ Config::write_config () const
        BOOST_FOREACH (dcp::Certificate const & i, _signer_chain->unordered()) {
                signer->add_child("Certificate")->add_child_text (i.certificate (true));
        }
+#ifdef DCPOMATIC_SWAROOP
+       FILE* f = fopen_boost (path("private"), "wb");
+       if (!f) {
+               throw FileError ("Could not open file for writing", path("private"));
+       }
+       shared_array<uint8_t> iv = dcpomatic::random_iv ();
+       dcp::Data encrypted_key = dcpomatic::encrypt (_signer_chain->key().get(), key, iv);
+       fwrite (encrypted_key.data().get(), encrypted_key.data().size(), 1, f);
+       fclose (f);
+#else  
        signer->add_child("PrivateKey")->add_child_text (_signer_chain->key().get ());
+#endif 
 
        /* [XML] Decryption Certificate chain and private key to use when decrypting KDMs */
        xmlpp::Element* decryption = root->add_child ("Decryption");
@@ -941,15 +952,28 @@ Config::write_config () const
        BOOST_FOREACH (Monitor i, _required_monitors) {
                i.as_xml(root->add_child("RequiredMonitor"));
        }
-       root->add_child("AllowSPLEditing")->add_child_text(_allow_spl_editing ? "1" : "0");
+       if (_player_lock_file) {
+               root->add_child("PlayerLockFile")->add_child_text(_player_lock_file->string());
+       }
 #endif
 
        try {
-               doc.write_to_file_formatted(config_file().string());
+               string const s = doc.write_to_string_formatted ();
+               boost::filesystem::path const cf = config_file ();
+               FILE* f = fopen_boost (cf, "w");
+               if (!f) {
+                       throw FileError (_("Could not open file for writing"), cf);
+               }
+               size_t const w = fwrite (s.c_str(), 1, s.length(), f);
+               if (w != s.length()) {
+                       fclose (f);
+                       throw FileError (_("Could not write whole file"), cf);
+               }
+               fclose (f);
        } catch (xmlpp::exception& e) {
                string s = e.what ();
                trim (s);
-               throw FileError (s, path("config.xml"));
+               throw FileError (s, config_file());
        }
 }