-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdm.cpp
More file actions
162 lines (133 loc) · 3.38 KB
/
dm.cpp
File metadata and controls
162 lines (133 loc) · 3.38 KB
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
#include <cstdarg>
#include <cstdio>
#include <format>
#include <memory>
#include <mutex>
#include <boost/uuid/uuid_io.hpp>
#include "dm.hpp"
#include "errors.hpp"
#include "libdevmapper.h"
namespace fluks {
namespace {
std::string log_output;
extern "C" void
dm_logger(int level, const char *file, int line, const char *f, ...) {
if (level > 3) return;
const int SZ = 100;
std::unique_ptr<char> buf{new char[SZ]};
int sz = SZ;
va_list ap;
std::string out = "\n\t";
for (;;) {
va_start(ap, f);
int n = vsnprintf(buf.get(), sz, f, ap);
va_end(ap);
if (n >= 0) {
if (n < sz) {
// the string fit into the buffer
out += buf.get();
break;
}
// buffer not big enough, resize to the proper size
// (only performed once)
sz = n + 1;
buf.reset(new char[sz]);
} else {
out += std::format("vsnprintf() failed; {}: {}", file, f);
break;
}
}
log_output += out;
}
void
dm_setup_log() {
static std::once_flag flag;
std::call_once(flag, []() {
dm_log_init(dm_logger);
});
}
class Device_mapper {
public:
Device_mapper(int type) {
dm_setup_log();
log_output = "";
if (!(_task = dm_task_create(type)))
throw Dm_error(log_output);
}
~Device_mapper() {
dm_task_destroy(_task);
}
void set_name(const std::string &name) {
log_output = "";
if (!dm_task_set_name(_task, name.c_str()))
throw Dm_error(log_output);
}
void set_uuid(const boost::uuids::uuid &uuid);
void run() {
log_output = "";
if (!dm_task_run(_task))
throw Dm_error(log_output);
}
void add_crypt_target(
uint64_t start_sector, uint64_t num_sectors,
std::string_view cipher_spec,
const uint8_t *key, size_t sz_key,
std::string_view device_path);
private:
struct dm_task *_task;
};
void
Device_mapper::add_crypt_target(
uint64_t start_sector, uint64_t num_sectors,
std::string_view cipher_spec,
const uint8_t *key, size_t sz_key,
std::string_view device_path) {
std::string param_out;
// format of param argument:
// CIPHER KEY IV_OFFSET DEVICE_PATH OFFSET
// CIPHER (e.g. serpent-cbc-essiv:tgr192)
param_out += std::format("{} ", cipher_spec);
// KEY (hex-encoded master key)
for (size_t i = 0; i < sz_key; i++)
param_out += std::format("{:02x}", key[i]);
// IV_OFFSET (number added to sector number for each IV)
// DEVICE_PATH (e.g. /dev/sda7)
// OFFSET (start sector number [header is at sector 0])
param_out += std::format(" 0 {} {}", device_path, start_sector);
log_output = "";
if (!dm_task_add_target(_task,
0, // logical start sector
num_sectors,
"crypt", // DM target
param_out.c_str()))
throw Dm_error(log_output);
}
void
Device_mapper::set_uuid(const boost::uuids::uuid &uuid) {
auto uuid_hex = boost::uuids::to_string(uuid);
log_output = "";
if (!dm_task_set_uuid(_task, uuid_hex.c_str()))
throw Dm_error(log_output);
}
} // end anon namespace
}
void
fluks::dm_close(const std::string &name) {
Device_mapper task(DM_DEVICE_REMOVE);
task.set_name(name);
task.run();
}
void
fluks::dm_open(const std::string &name,
uint64_t start_sector, uint64_t num_sectors,
std::string_view cipher_spec,
const uint8_t *key, size_t sz_key,
const boost::uuids::uuid &uuid,
std::string_view device_path) {
Device_mapper task(DM_DEVICE_CREATE);
task.set_name(name);
task.set_uuid(uuid);
task.add_crypt_target(start_sector, num_sectors, cipher_spec,
key, sz_key, device_path);
task.run();
}