-
Notifications
You must be signed in to change notification settings - Fork 15
/
storage.cpp
500 lines (407 loc) · 14.9 KB
/
storage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <boost/iterator/filter_iterator.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ini_parser.hpp>
#include <boost/interprocess/streams/vectorstream.hpp>
#include "storage.h"
#include "tools.h"
#include "exceptions.h"
namespace fs = boost::filesystem;
typedef boost::property_tree::ptree config_t;
typedef boost::filter_iterator<std::function<bool(const fs::directory_entry&)>, fs::directory_iterator> files_iterator;
const std::string fs_driver_c = "fs";
const std::string fuse_driver_c = "fuse";
const std::string shell_driver_c = "shell";
const std::string crypt_driver_c = "crypt";
const std::string meta_c = ".soft-pkcs.meta";
std::string metaname(const std::string& file) {
return "." + file + ".meta";
};
const std::map<std::string, std::function<attribute_t(const std::string& val)> > load_attribute = {
{"id", [](const std::string& val){return attribute_t(CKA_ID, boost::lexical_cast<CK_ULONG>(val));}}
};
const std::map<CK_ATTRIBUTE_TYPE, std::function<boost::property_tree::ptree::value_type(const attribute_t& attr)> > dump_attribute = {
{CKA_ID, [](const attribute_t& attr){return std::make_pair("id", boost::property_tree::ptree(boost::lexical_cast<std::string>(attr.to_id())));}}
};
Attributes parse_meta(const Bytes& bytes) {
Attributes attributes;
boost::property_tree::ptree ini;
boost::interprocess::basic_vectorstream<Bytes> stream(bytes);
boost::property_tree::ini_parser::read_ini(stream, ini);
BOOST_FOREACH(auto attr, ini) {
auto it = load_attribute.find(attr.first);
if (it != load_attribute.end()) {
auto a = it->second(attr.second.data());
attributes[a->type] = a;
}
}
return attributes;
}
Bytes write_meta(const Attributes& attributes) {
boost::property_tree::ptree ini;
BOOST_FOREACH(auto& attr, attributes) {
auto it = dump_attribute.find(attr.first);
if (it != dump_attribute.end()) {
ini.push_back(it->second(attr.second));
}
}
boost::interprocess::basic_vectorstream<Bytes> stream;
boost::property_tree::ini_parser::write_ini(stream, ini);
return stream.vector();
}
/*
std::list<item_t> storage_t::items()
{
std::list<item_t> items = do_items();
// try {
// MetaAttributesList metalist = parse_meta(do_read(meta_c).data);
// BOOST_FOREACH(item_t& item, items) {
// item.meta = metalist[item.filename];
// }
// }
// catch (...) {};
return items;
}
item_t storage_t::read(const std::string& fn)
{
item_t item = do_read(fn);
// try {
// MetaAttributesList metalist = parse_meta(do_read(meta_c).data);
// item.meta = metalist[item.filename];
// }
// catch(...){}
return item;
}
item_t storage_t::write(const item_t& item)
{
// try {
// MetaAttributesList metalist = parse_meta(do_read(meta_c).data);
// metalist[item.filename] = item.meta;
// auto data = write_meta(metalist);
// do_write(item_t(meta_c, data));
// }
// catch(...) {}
return do_write(item);
}*/
struct fs_storage_t : storage_t {
fs_storage_t(const config_t& c, const std::string& pin, std::shared_ptr<storage_t> s)
: storage_t(fs_driver_c, c, s)
{
set_pin(pin);
path = config_.get<std::string>("path");
st_logf("Path : %s\n", path.c_str());
}
files_iterator files_begin() {
if (fs::exists(path) && fs::is_directory(path)) {
return files_iterator(
[](const fs::directory_entry& d){return fs::is_regular_file(d.status());},
fs::directory_iterator(path)
);
}
return files_end();
}
files_iterator files_end() const {
return files_iterator(fs::directory_iterator());
}
virtual void set_pin(const std::string& pin) {
if (prev) prev->set_pin(pin);
}
virtual bool present() const {
return fs::exists(path) && fs::is_directory(path);
}
virtual std::list<item_t> items() {
std::list<item_t> result;
for(auto it = files_begin(); it != files_end(); ++it) {
std::ifstream stream(it->path().string());
if (!boost::algorithm::ends_with(it->path().filename().string(), ".meta")) {
const std::string fn = it->path().filename().string();
result.push_back(
item_t(
fn,
read_file((fs::directory_entry(path).path() / fn).string()),
parse_meta(read_file((fs::directory_entry(path).path() / metaname(fn)).string()))
)
);
}
}
return result;
};
virtual item_t read(const std::string& fn) {
return item_t(
fn,
read_file((fs::directory_entry(path).path() / fn).string()),
parse_meta(read_file((fs::directory_entry(path).path() / metaname(fn)).string()))
);
throw std::runtime_error("such file not found");
};
virtual item_t write(const item_t& item) {
write_file((fs::directory_entry(path).path() / (item.filename)).string(), item.data);
write_file((fs::directory_entry(path).path() / metaname(item.filename)).string(), write_meta(item.attributes));
return read(item.filename);
}
Bytes read_file(const std::string& filepath) {
try {
std::ifstream stream(filepath);
return Bytes((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
}
catch(...) {
return Bytes();
}
}
void write_file(const std::string& filepath, const Bytes& data) {
std::shared_ptr<FILE> file(
::fopen(filepath.c_str(), "w+"),
::fclose
);
if (!file.get()) {
throw std::runtime_error("can't open file for writing");
}
int res = ::fwrite(data.data(), 1, data.size(), file.get());
if (res != data.size()) {
throw std::runtime_error("can't write file");
}
file.reset();
}
std::string path;
};
struct mount_t {
mount_t(const std::string& m, const std::string& u, const std::string& pass)
: umount(u)
{
system(umount.c_str());
if (start(m.c_str(), std::vector<char>(pass.begin(), pass.end())) != 0) {
throw std::runtime_error("Can't mount: " + m);
}
}
~mount_t() {
st_logf("Umounting: %s\n", umount.c_str());
system(umount.c_str());
}
std::string umount;
};
struct fuse_storage_t : fs_storage_t {
fuse_storage_t(const config_t& c, const std::string& pin, std::shared_ptr<storage_t> s = std::shared_ptr<storage_t>())
: fs_storage_t(c, pin, s)
{
name_ = fuse_driver_c;
set_pin(pin);
}
virtual void set_pin(const std::string& pin) {
const std::string mount = config_.get<std::string>("mount");
const std::string umount = config_.get<std::string>("umount");
st_logf("Mount: %s, Umount: %s\n", mount.c_str(), umount.c_str());
m_.reset(new mount_t(mount, umount, pin));
if (prev) prev->set_pin(pin);
}
std::shared_ptr<mount_t> m_;
};
struct shell_storage_t : storage_t {
shell_storage_t(const config_t& c, const std::string& pin, std::shared_ptr<storage_t> s = std::shared_ptr<storage_t>())
: storage_t(shell_driver_c, c, s)
, timestamp_(0), last_present_(false)
{
present_ = c.get<std::string>("present");
list_ = c.get<std::string>("list");
read_ = c.get<std::string>("read");
write_ = c.get<std::string>("write");
set_pin(pin);
}
virtual void set_pin(const std::string& pin) {
if (prev) prev->set_pin(pin);
}
virtual bool present() const {
time_t current = ::time(NULL);
if (current - timestamp_ < 3) {
if (last_present_) return true;
}
last_present_ = (start(present_) == 0);
st_logf("Shell storage present: %d\n", last_present_);
timestamp_ = ::time(NULL);
return last_present_;
}
virtual std::list<item_t> items() {
std::list<item_t> result;
std::vector<std::string> files;
try {
auto data = piped(list_);
boost::split(files, data, boost::is_any_of("\n"));
}
catch (const std::exception& e) {
timestamp_ = 0;
if (present()) {
throw pkcs11_exception_t(CKR_DEVICE_ERROR, std::string("failed to list files: ") + e.what());
} else {
throw pkcs11_exception_t(CKR_DEVICE_REMOVED, "device removed");
}
}
for(auto file: files) {
if (file.empty() or file == meta_c) continue;
const item_t item = read(file);
assert(!item.data.empty());
result.push_back(item);
}
return result;
}
virtual item_t read(const std::string& fn) {
std::string read = read_;
boost::replace_all(read, "%FILE%", fn);
std::string read_meta = read_;
boost::replace_all(read_meta, "%FILE%", metaname(fn));
try {
return item_t {
fn,
piped(read),
parse_meta(piped(read_meta))
};
}
catch(const std::exception& e) {
timestamp_ = 0;
if (present()) {
throw pkcs11_exception_t(CKR_DEVICE_ERROR, std::string("failed to read file: ") + e.what());
} else {
throw pkcs11_exception_t(CKR_DEVICE_REMOVED, "device removed");
}
}
}
virtual item_t write(const item_t& item) {
std::string write_cmd, write_meta_cmd = write_;
boost::replace_all(write_cmd, "%FILE%", item.filename);
boost::replace_all(write_meta_cmd, "%FILE%", metaname(item.filename));
try {
piped(write_cmd, item.data);
piped(write_meta_cmd, write_meta(item.attributes));
}
catch(const std::exception& e) {
timestamp_ = 0;
if (present()) {
throw pkcs11_exception_t(CKR_DEVICE_ERROR, std::string("failed to write file: ") + e.what());
} else {
throw pkcs11_exception_t(CKR_DEVICE_REMOVED, "device removed");
}
}
return read(item.filename);
}
std::string present_;
std::string list_;
std::string read_;
std::string write_;
mutable time_t timestamp_;
mutable bool last_present_;
};
struct crypt_storage_t : storage_t {
crypt_storage_t(const config_t& c, const std::string& pin, std::shared_ptr<storage_t> s)
: storage_t(crypt_driver_c, c, s)
{
set_pin(pin);
}
virtual ~crypt_storage_t() {
}
virtual void set_pin(const std::string& pin) {
encrypt_ = config_.get<std::string>("encrypt");
decrypt_ = config_.get<std::string>("decrypt");
boost::replace_all(encrypt_, "%PIN%", pin);
boost::replace_all(decrypt_, "%PIN%", pin);
if (prev) prev->set_pin(pin);
}
virtual bool present() const {
return prev->present();
}
virtual std::list<item_t> items() {
std::list<item_t> result;
for(auto item: prev->items()) {
try {
const item_t d = decrypt(item);
if (!d.data.empty()) {
result.push_back(d);
}
} catch (std::exception& e) {
st_logf("Can't decrypt file %s: %s\n", item.filename.c_str(), e.what());
}
}
return result;
}
virtual item_t read(const std::string& fn) {
return decrypt(prev->read(fn));
}
virtual item_t write(const item_t& item) {
return decrypt(prev->write(encrypt(item)));
}
item_t decrypt(const item_t& item) const {
try {
return item_t {
item.filename,
piped(decrypt_, item.data),
item.attributes
};
} catch(const std::exception& e) {
throw pkcs11_exception_t(CKR_DEVICE_ERROR, std::string("failed to decrypt file: ") + e.what());
}
}
item_t encrypt(const item_t& item) const {
try {
return item_t {
item.filename,
piped(encrypt_, item.data),
item.attributes
};
} catch(const std::exception& e) {
throw pkcs11_exception_t(CKR_DEVICE_ERROR, std::string("failed to decrypt file: ") + e.what());
}
}
std::string decrypt_;
std::string encrypt_;
};
std::shared_ptr<storage_t> storage_t::create(const config_t& config, const std::string& pin)
{
std::shared_ptr<storage_t> storage;
BOOST_FOREACH(auto p, config) {
if (p.second.size() > 0) {
if (p.second.get<std::string>("driver") == fs_driver_c) {
storage.reset(new fs_storage_t(p.second, pin, storage));
}
else if (p.second.get<std::string>("driver") == fuse_driver_c) {
storage.reset(new fuse_storage_t(p.second, pin, storage));
}
else if (p.second.get<std::string>("driver") == crypt_driver_c) {
storage.reset(new crypt_storage_t(p.second, pin, storage));
}
else if (p.second.get<std::string>("driver") == shell_driver_c) {
storage.reset(new shell_storage_t(p.second, pin, storage));
}
}
}
return storage;
}
struct to_object_id : std::unary_function<const fs::directory_entry&, CK_OBJECT_HANDLE> {
CK_OBJECT_HANDLE operator() (const fs::directory_entry& d) const {
return static_cast<CK_OBJECT_HANDLE>(hash(d.path().filename().c_str()));
}
CK_OBJECT_HANDLE operator() (const std::string& filename) const {
return static_cast<CK_OBJECT_HANDLE>(hash(filename));
}
private:
std::hash<std::string> hash;
};
descriptor_t::descriptor_t(const item_t& it)
: item(it)
{
if (item.data.empty()) {
throw std::runtime_error("There is no data in item");
}
const std::string str(item.data.begin(), item.data.end());
std::stringstream stream(str);
std::getline(stream, first_line, '\n');
stream.seekg (0, stream.beg);
id = to_object_id()(item.filename);
void* src = const_cast<char*>(item.data.data());
file.reset(
::fmemopen(src, item.data.size(), "r"),
::fclose
);
if (!file.get()) {
throw std::runtime_error("Can't memopen data");
}
};