1
|
1 /**
|
|
2 * Cloud storage plugins for Orthanc
|
|
3 * Copyright (C) 2017-2020 Osimis S.A., Belgium
|
|
4 *
|
|
5 * This program is free software: you can redistribute it and/or
|
|
6 * modify it under the terms of the GNU Affero General Public License
|
|
7 * as published by the Free Software Foundation, either version 3 of
|
|
8 * the License, or (at your option) any later version.
|
|
9 *
|
|
10 * This program is distributed in the hope that it will be useful, but
|
|
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 * Affero General Public License for more details.
|
|
14 *
|
|
15 * You should have received a copy of the GNU Affero General Public License
|
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
17 **/
|
|
18
|
|
19 #if GOOGLE_STORAGE_PLUGIN==1
|
|
20 #include "../Google/GoogleStoragePlugin.h"
|
|
21 #define StoragePluginFactory GoogleStoragePluginFactory
|
|
22 #elif AZURE_STORAGE_PLUGIN==1
|
|
23 #include "../Azure/AzureBlobStoragePlugin.h"
|
|
24 #define StoragePluginFactory AzureBlobStoragePluginFactory
|
|
25 #elif AWS_STORAGE_PLUGIN==1
|
|
26 #include "../Aws/AwsS3StoragePlugin.h"
|
|
27 #define StoragePluginFactory AwsS3StoragePluginFactory
|
|
28 #else
|
|
29 #pragma message(error "define a plugin")
|
|
30 #endif
|
|
31
|
|
32 #include <string.h>
|
|
33 #include <stdio.h>
|
|
34 #include <string>
|
|
35
|
|
36 #include <iostream>
|
|
37 #include "../Common/EncryptionHelpers.h"
|
|
38 #include "../Common/EncryptionConfigurator.h"
|
|
39
|
|
40 static std::unique_ptr<IStoragePlugin> plugin;
|
|
41
|
|
42 static std::unique_ptr<EncryptionHelpers> crypto;
|
|
43 static bool cryptoEnabled = false;
|
|
44
|
|
45
|
|
46 static OrthancPluginErrorCode StorageCreate(const char* uuid,
|
|
47 const void* content,
|
|
48 int64_t size,
|
|
49 OrthancPluginContentType type)
|
|
50 {
|
|
51 try
|
|
52 {
|
|
53 std::unique_ptr<IStoragePlugin::IWriter> writer(plugin->GetWriterForObject(uuid, type, cryptoEnabled));
|
|
54
|
|
55 if (cryptoEnabled)
|
|
56 {
|
|
57 std::string encryptedFile;
|
|
58
|
|
59 try
|
|
60 {
|
|
61 crypto->Encrypt(encryptedFile, (const char*)content, size);
|
|
62 }
|
|
63 catch (EncryptionException& ex)
|
|
64 {
|
|
65 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while encrypting object " + std::string(uuid) + ": " + ex.what());
|
|
66 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
67 }
|
|
68
|
|
69 writer->Write(encryptedFile.data(), encryptedFile.size());
|
|
70 }
|
|
71 else
|
|
72 {
|
|
73 writer->Write(reinterpret_cast<const char*>(content), size);
|
|
74 }
|
|
75 }
|
|
76 catch (StoragePluginException& ex)
|
|
77 {
|
|
78 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while creating object " + std::string(uuid) + ": " + ex.what());
|
|
79 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
80 }
|
|
81
|
|
82 return OrthancPluginErrorCode_Success;
|
|
83 }
|
|
84
|
|
85
|
|
86 static OrthancPluginErrorCode StorageRead(void** content,
|
|
87 int64_t* size,
|
|
88 const char* uuid,
|
|
89 OrthancPluginContentType type)
|
|
90 {
|
|
91 try
|
|
92 {
|
|
93 std::unique_ptr<IStoragePlugin::IReader> reader(plugin->GetReaderForObject(uuid, type, cryptoEnabled));
|
|
94
|
|
95 size_t fileSize = reader->GetSize();
|
|
96
|
|
97 if (cryptoEnabled)
|
|
98 {
|
|
99 *size = fileSize - crypto->OVERHEAD_SIZE;
|
|
100 }
|
|
101 else
|
|
102 {
|
|
103 *size = fileSize;
|
|
104 }
|
|
105
|
|
106 if (*size <= 0)
|
|
107 {
|
|
108 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ", size of file is too small: " + boost::lexical_cast<std::string>(fileSize) + " bytes");
|
|
109 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
110 }
|
|
111
|
|
112 *content = malloc(static_cast<uint64_t>(*size));
|
|
113 if (*content == nullptr)
|
|
114 {
|
|
115 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while reading object " + std::string(uuid) + ", cannot allocate memory of size " + boost::lexical_cast<std::string>(*size) + " bytes");
|
|
116 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
117 }
|
|
118
|
|
119 if (cryptoEnabled)
|
|
120 {
|
|
121 std::vector<char> encrypted(fileSize);
|
|
122 reader->Read(encrypted.data(), fileSize);
|
|
123
|
|
124 try
|
|
125 {
|
|
126 crypto->Decrypt((char*)(*content), encrypted.data(), fileSize);
|
|
127 }
|
|
128 catch (EncryptionException& ex)
|
|
129 {
|
|
130 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while decrypting object " + std::string(uuid) + ": " + ex.what());
|
|
131 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
132 }
|
|
133 }
|
|
134 else
|
|
135 {
|
|
136 reader->Read(*(reinterpret_cast<char**>(content)), fileSize);
|
|
137 }
|
|
138 }
|
|
139 catch (StoragePluginException& ex)
|
|
140 {
|
|
141 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while creating object " + std::string(uuid) + ": " + ex.what());
|
|
142 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
143 }
|
|
144
|
|
145 return OrthancPluginErrorCode_Success;
|
|
146
|
|
147 }
|
|
148
|
|
149
|
|
150 static OrthancPluginErrorCode StorageRemove(const char* uuid,
|
|
151 OrthancPluginContentType type)
|
|
152 {
|
|
153 try
|
|
154 {
|
|
155 plugin->DeleteObject(uuid, type, cryptoEnabled);
|
|
156 }
|
|
157 catch (StoragePluginException& ex)
|
|
158 {
|
|
159 OrthancPlugins::LogError(std::string(StoragePluginFactory::GetStoragePluginName()) + ": error while deleting object " + std::string(uuid) + ": " + ex.what());
|
|
160 return OrthancPluginErrorCode_StorageAreaPlugin;
|
|
161 }
|
|
162
|
|
163 return OrthancPluginErrorCode_Success;
|
|
164 }
|
|
165
|
|
166
|
|
167 extern "C"
|
|
168 {
|
|
169 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* context)
|
|
170 {
|
|
171 OrthancPlugins::SetGlobalContext(context);
|
|
172
|
|
173 OrthancPlugins::OrthancConfiguration orthancConfig;
|
|
174
|
|
175 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + " plugin is initializing");
|
|
176
|
|
177 /* Check the version of the Orthanc core */
|
|
178 if (OrthancPluginCheckVersion(context) == 0)
|
|
179 {
|
|
180 char info[1024];
|
|
181 sprintf(info, "Your version of Orthanc (%s) must be above %d.%d.%d to run this plugin",
|
|
182 context->orthancVersion,
|
|
183 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
|
|
184 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
|
|
185 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
|
|
186 OrthancPlugins::LogError(info);
|
|
187 return -1;
|
|
188 }
|
|
189
|
|
190 plugin.reset(StoragePluginFactory::CreateStoragePlugin(orthancConfig));
|
|
191
|
8
|
192 if (plugin.get() == nullptr)
|
|
193 {
|
|
194 return -1;
|
|
195 }
|
|
196
|
1
|
197 static const char* const ENCRYPTION_SECTION = "StorageEncryption";
|
|
198
|
|
199 if (orthancConfig.IsSection(ENCRYPTION_SECTION))
|
|
200 {
|
|
201 OrthancPlugins::OrthancConfiguration cryptoSection;
|
|
202 orthancConfig.GetSection(cryptoSection, ENCRYPTION_SECTION);
|
|
203
|
|
204 crypto.reset(EncryptionConfigurator::CreateEncryptionHelpers(cryptoSection));
|
|
205 cryptoEnabled = crypto.get() != nullptr;
|
|
206 }
|
|
207 else
|
|
208 {
|
|
209 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + ": client-side encryption is disabled");
|
|
210 }
|
|
211
|
|
212 OrthancPluginRegisterStorageArea(context, StorageCreate, StorageRead, StorageRemove);
|
|
213
|
|
214 return 0;
|
|
215 }
|
|
216
|
|
217
|
|
218 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
|
|
219 {
|
|
220 OrthancPlugins::LogWarning(std::string(StoragePluginFactory::GetStoragePluginName()) + " plugin is finalizing");
|
|
221 }
|
|
222
|
|
223
|
|
224 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
|
|
225 {
|
|
226 return StoragePluginFactory::GetStoragePluginName();
|
|
227 }
|
|
228
|
|
229
|
|
230 ORTHANC_PLUGINS_API const char* OrthancPluginGetVersion()
|
|
231 {
|
|
232 return PLUGIN_VERSION;
|
|
233 }
|
|
234 }
|
|
235
|