Remake the certificate chain if any part of it is missing.
[dcpomatic.git] / src / lib / util.cc
index 739a327d6a91ed319db66417e74331622193ea33..badbda4ab685c775f9633eb5ce4b7ebae661f9ad 100644 (file)
@@ -46,6 +46,8 @@
 #include <magick/version.h>
 #include <libdcp/version.h>
 #include <libdcp/util.h>
+#include <libdcp/signer_chain.h>
+#include <libdcp/signer.h>
 extern "C" {
 #include <libavcodec/avcodec.h>
 #include <libavformat/avformat.h>
@@ -54,7 +56,6 @@ extern "C" {
 #include <libpostproc/postprocess.h>
 #include <libavutil/pixfmt.h>
 }
-#include <curl/curl.h>
 #include "util.h"
 #include "exceptions.h"
 #include "scaler.h"
@@ -64,6 +65,7 @@ extern "C" {
 #include "config.h"
 #include "ratio.h"
 #include "job.h"
+#include "cross.h"
 #ifdef DCPOMATIC_WINDOWS
 #include "stack.hpp"
 #endif
@@ -818,52 +820,55 @@ tidy_for_filename (string f)
        return t;
 }
 
-struct EmailState
+shared_ptr<const libdcp::Signer>
+make_signer ()
 {
-       string message;
-       int done;
-};
+       boost::filesystem::path const sd = Config::instance()->signer_chain_directory ();
 
-static size_t
-send_email_function (void* ptr, size_t size, size_t nmemb, void* userdata)
-{
-       EmailState* state = reinterpret_cast<EmailState*> (userdata);
-
-       int const now = min (size * nmemb, state->message.length() - state->done);
-
-       memcpy (ptr, state->message.c_str() + state->done, now);
-       state->done += now;
-
-       return now;
-}
+       /* Remake the chain if any of it is missing */
        
-bool
-send_email (string from, string to, string message)
-{
-       CURL* curl = curl_easy_init ();
-       if (!curl) {
-               return true;
-       }
+       list<boost::filesystem::path> files;
+       files.push_back ("ca.self-signed.pem");
+       files.push_back ("intermediate.signed.pem");
+       files.push_back ("leaf.signed.pem");
+       files.push_back ("leaf.key");
+
+       list<boost::filesystem::path>::const_iterator i = files.begin();
+       while (i != files.end()) {
+               boost::filesystem::path p (sd);
+               sd /= *i;
+               if (!boost::filesystem::exists (sd)) {
+                       boost::filesystem::remove_all (sd);
+                       libdcp::make_signer_chain (sd, openssl_path ());
+                       break;
+               }
 
-       string const url = "smtp://" + Config::instance()->mail_server();
+               ++i;
+       }
+       
+       libdcp::CertificateChain chain;
 
-       curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
-       curl_easy_setopt (curl, CURLOPT_MAIL_FROM, from.c_str ());
-       struct curl_slist* recipients = 0;
-       recipients = curl_slist_append (recipients, to.c_str ());
-       curl_easy_setopt (curl, CURLOPT_READFUNCTION, send_email_function);
+       {
+               boost::filesystem::path p (sd);
+               p /= "ca.self-signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
 
-       EmailState state;
-       state.message = message;
-       state.done = 0;
-       curl_easy_setopt (curl, CURLOPT_READDATA, &state);
+       {
+               boost::filesystem::path p (sd);
+               p /= "intermediate.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
+       }
 
-       if (curl_easy_perform (curl) != CURLE_OK) {
-               return true;
+       {
+               boost::filesystem::path p (sd);
+               p /= "leaf.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p)));
        }
 
-       curl_slist_free_all (recipients);
-       curl_easy_cleanup (curl);
+       boost::filesystem::path signer_key (sd);
+       signer_key /= "leaf.key";
 
-       return false;
+       return shared_ptr<const libdcp::Signer> (new libdcp::Signer (chain, signer_key));
 }
+