51f2a357925dbf4de67470f67cf8d88ce8b09770
[dcpomatic.git] / src / wx / doremi_certificate_dialog.cc
1 /*
2     Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <curl/curl.h>
21 #include <zip.h>
22 #include "lib/compose.hpp"
23 #include "lib/util.h"
24 #include "doremi_certificate_dialog.h"
25 #include "wx_util.h"
26
27 using std::string;
28 using boost::function;
29
30 DoremiCertificateDialog::DoremiCertificateDialog (wxWindow* parent, function<void (boost::filesystem::path)> load)
31         : DownloadCertificateDialog (parent, load)
32 {
33         wxFlexGridSizer* table = new wxFlexGridSizer (2, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
34
35         add_label_to_sizer (table, this, _("Server serial number"), true);
36         _serial = new wxTextCtrl (this, wxID_ANY);
37         table->Add (_serial, 1, wxEXPAND);
38
39         _overall_sizer->Add (table, 1, wxALL, DCPOMATIC_DIALOG_BORDER);
40
41         add_common_widgets ();
42 }
43
44
45
46 static size_t
47 ftp_data (void* buffer, size_t size, size_t nmemb, void* stream)
48 {
49         FILE* f = reinterpret_cast<FILE*> (stream);
50         return fwrite (buffer, size, nmemb, f);
51 }
52
53 void
54 DoremiCertificateDialog::download ()
55 {
56         string const serial = wx_to_std (_serial->GetValue ());
57         if (serial.length() != 6) {
58                 _message->SetLabel (_("Doremi serial numbers must have 6 digits"));
59                 return;
60         }
61         
62         CURL* curl = curl_easy_init ();
63         if (!curl) {
64                 _message->SetLabel (N_("Could not set up libcurl"));
65                 return;
66         }
67         
68         string const url = String::compose (
69                 "ftp://service:t3chn1c1an@ftp.doremilabs.com/Certificates/%1xxx/dcp2000-%2.dcicerts.zip",
70                 serial.substr(0, 3), serial
71                 );
72         
73         curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
74         
75         ScopedTemporary temp_zip;
76         FILE* f = temp_zip.open ("wb");
77         curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, ftp_data);
78         curl_easy_setopt (curl, CURLOPT_WRITEDATA, f);
79
80         _message->SetLabel (_("Downloading certificate from Doremi"));
81         run_gui_loop ();
82
83         CURLcode const cr = curl_easy_perform (curl);
84
85         _gauge->SetValue (50);
86         run_gui_loop ();
87         
88         temp_zip.close ();
89         curl_easy_cleanup (curl);
90         if (cr != CURLE_OK) {
91                 _message->SetLabel (wxString::Format (_("Certificate download failed (%d)"), cr));
92                 return;
93         }
94         
95         _message->SetLabel (_("Unpacking"));
96         run_gui_loop ();
97         
98         struct zip* zip = zip_open (temp_zip.c_str(), 0, 0);
99         if (!zip) {
100                 _message->SetLabel (N_("Could not open certificate ZIP file"));
101                 return;
102         }
103         
104         string const name_in_zip = String::compose ("dcp2000-%1.cert.sha256.pem", serial);
105         struct zip_file* zip_file = zip_fopen (zip, name_in_zip.c_str(), 0);
106         if (!zip_file) {
107                 _message->SetLabel (N_("Could not find certificate in ZIP file"));
108                 return;
109         }
110         
111         ScopedTemporary temp_cert;
112         f = temp_cert.open ("wb");
113         char buffer[4096];
114         while (1) {
115                 int const N = zip_fread (zip_file, buffer, sizeof (buffer));
116                 fwrite (buffer, 1, N, f);
117                 if (N < int (sizeof (buffer))) {
118                         break;
119                 }
120         }
121         temp_cert.close ();
122         
123         _gauge->SetValue (100);
124         _message->SetLabel (_("OK"));
125         _load (temp_cert.file ());
126 }