/
crypto_cache.c
384 lines (319 loc) · 9.75 KB
/
crypto_cache.c
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
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2013 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* crypto_cache.c Encryption key caching functions
*
* Marco van Wieringen, April 2012
*/
#include "bareos.h"
#include "crypto_cache.h"
static pthread_mutex_t crypto_cache_lock = PTHREAD_MUTEX_INITIALIZER;
static dlist *cached_crypto_keys = NULL;
static s_crypto_cache_hdr crypto_cache_hdr = {
"BAREOS Crypto Cache\n",
1,
0
};
/*
* Read the content of the crypto cache from the filesystem.
*/
void read_crypto_cache(const char *cache_file)
{
int fd, cnt;
ssize_t status;
bool ok = false;
s_crypto_cache_hdr hdr;
int hdr_size = sizeof(hdr);
crypto_cache_entry_t *cce = NULL;
if ((fd = open(cache_file, O_RDONLY|O_BINARY)) < 0) {
berrno be;
Dmsg2(010, "Could not open crypto cache file. %s ERR=%s\n", cache_file, be.bstrerror());
goto bail_out;
}
if ((status = read(fd, &hdr, hdr_size)) != hdr_size) {
berrno be;
Dmsg4(010, "Could not read crypto cache file. fd=%d status=%d size=%d: ERR=%s\n",
fd, (int)status, hdr_size, be.bstrerror());
goto bail_out;
}
if (hdr.version != crypto_cache_hdr.version) {
Dmsg2(010, "Crypto cache bad hdr version. Wanted %d got %d\n",
crypto_cache_hdr.version, hdr.version);
goto bail_out;
}
hdr.id[20] = 0;
if (!bstrcmp(hdr.id, crypto_cache_hdr.id)) {
Dmsg0(000, "Crypto cache file header id invalid.\n");
goto bail_out;
}
if (!cached_crypto_keys) {
cached_crypto_keys = New(dlist(cce, &cce->link));
}
/*
* Read as many crypto cache entries as available.
*/
cnt = 0;
cce = (crypto_cache_entry_t *)malloc(sizeof(crypto_cache_entry_t));
while (read(fd, cce, sizeof(crypto_cache_entry_t)) == sizeof(crypto_cache_entry_t)) {
cnt++;
cached_crypto_keys->append(cce);
cce = (crypto_cache_entry_t *)malloc(sizeof(crypto_cache_entry_t));
}
/*
* We always allocate a dangling crypto_cache_entry_t structure in
* the way that we malloc before the loop and in the loop. So drop
* the last unused entry.
*/
free(cce);
/*
* Check if we read the number of entries the header said are in the file.
*/
if (cnt == hdr.nr_entries) {
ok = true;
Dmsg2(010, "Crypto cache read %d entries in file %s\n", cnt, cache_file);
} else {
Dmsg3(000, "Crypto cache read %d entries while %d entries should be in file %s\n",
cnt, hdr.nr_entries, cache_file);
}
bail_out:
if (fd >= 0) {
close(fd);
}
if (!ok) {
unlink(cache_file);
if (cached_crypto_keys) {
cached_crypto_keys->destroy();
delete cached_crypto_keys;
cached_crypto_keys = NULL;
}
}
}
void read_crypto_cache(const char *dir, const char *progname, int port)
{
POOLMEM *fname = get_pool_memory(PM_FNAME);
Mmsg(&fname, "%s/%s.%d.cryptoc", dir, progname, port);
read_crypto_cache(fname);
free_pool_memory(fname);
}
/*
* Write the content of the crypto cache to the filesystem.
*/
void write_crypto_cache(const char *cache_file)
{
int fd;
bool ok = false;
crypto_cache_entry_t *cce;
if (!cached_crypto_keys) {
return;
}
/*
* Lock the cache.
*/
P(crypto_cache_lock);
unlink(cache_file);
if ((fd = open(cache_file, O_CREAT | O_WRONLY | O_BINARY, 0640)) < 0) {
berrno be;
Dmsg2(000, "Could not create crypto cache file. %s ERR=%s\n", cache_file, be.bstrerror());
Emsg2(M_ERROR, 0, _("Could not create crypto cache file. %s ERR=%s\n"), cache_file, be.bstrerror());
goto bail_out;
}
crypto_cache_hdr.nr_entries = cached_crypto_keys->size();
if (write(fd, &crypto_cache_hdr, sizeof(crypto_cache_hdr)) != sizeof(crypto_cache_hdr)) {
berrno be;
Dmsg1(000, "Write hdr error: ERR=%s\n", be.bstrerror());
goto bail_out;
}
foreach_dlist(cce, cached_crypto_keys) {
if (write(fd, cce, sizeof(crypto_cache_entry_t)) != sizeof(crypto_cache_entry_t)) {
berrno be;
Dmsg1(000, "Write record error: ERR=%s\n", be.bstrerror());
goto bail_out;
}
}
ok = true;
bail_out:
if (fd >= 0) {
close(fd);
}
if (!ok) {
unlink(cache_file);
}
V(crypto_cache_lock);
}
void write_crypto_cache(const char *dir, const char *progname, int port)
{
POOLMEM *fname = get_pool_memory(PM_FNAME);
Mmsg(&fname, "%s/%s.%d.cryptoc", dir, progname, port);
write_crypto_cache(fname);
free_pool_memory(fname);
}
/*
* Update the internal cache with new data. When the cache gets
* modified by a new entry or by expiring old data the return
* value gives an indication of that.
* Returns: true - cache was updated with new data.
* false - cache was not updated with new data.
*/
bool update_crypto_cache(const char *VolumeName, const char *EncryptionKey)
{
time_t now;
bool found;
bool retval = false;
crypto_cache_entry_t *cce = NULL;
crypto_cache_entry_t *next_cce;
/*
* Lock the cache.
*/
P(crypto_cache_lock);
/*
* See if there are any cached encryption keys.
*/
if (!cached_crypto_keys) {
cached_crypto_keys = New(dlist(cce, &cce->link));
cce = (crypto_cache_entry_t *)malloc(sizeof(crypto_cache_entry_t));
bstrncpy(cce->VolumeName, VolumeName, sizeof(cce->VolumeName));
bstrncpy(cce->EncryptionKey, EncryptionKey, sizeof(cce->EncryptionKey));
cce->added = time(NULL);
cached_crypto_keys->append(cce);
retval = true;
} else {
found = false;
now = time(NULL);
cce = (crypto_cache_entry_t *)cached_crypto_keys->first();
while (cce) {
next_cce = (crypto_cache_entry_t *)cached_crypto_keys->next(cce);
if (bstrcmp(cce->VolumeName, VolumeName)) {
found = true;
/*
* If the key changed update the cached entry.
*/
if (!bstrcmp(cce->EncryptionKey, EncryptionKey)) {
bstrncpy(cce->EncryptionKey, EncryptionKey, sizeof(cce->EncryptionKey));
retval = true;
}
cce->added = time(NULL);
cce = next_cce;
continue;
}
/*
* Validate the entry.
* Any entry older the CRYPTO_CACHE_MAX_AGE seconds is removed.
*/
if ((cce->added + CRYPTO_CACHE_MAX_AGE) < now) {
cached_crypto_keys->remove(cce);
retval = true;
}
cce = next_cce;
}
/*
* New entry.
*/
if (!found) {
cce = (crypto_cache_entry_t *)malloc(sizeof(crypto_cache_entry_t));
bstrncpy(cce->VolumeName, VolumeName, sizeof(cce->VolumeName));
bstrncpy(cce->EncryptionKey, EncryptionKey, sizeof(cce->EncryptionKey));
cce->added = time(NULL);
cached_crypto_keys->append(cce);
retval = true;
}
}
V(crypto_cache_lock);
return retval;
}
/*
* Lookup a cache entry for the given VolumeName.
* Returns: string dupped encryption key must be freed by caller.
*/
char *lookup_crypto_cache_entry(const char *VolumeName)
{
crypto_cache_entry_t *cce;
if (!cached_crypto_keys) {
return NULL;
}
/*
* Lock the cache.
*/
P(crypto_cache_lock);
foreach_dlist(cce, cached_crypto_keys) {
if (bstrcmp(cce->VolumeName, VolumeName)) {
V(crypto_cache_lock);
return bstrdup(cce->EncryptionKey);
}
}
V(crypto_cache_lock);
return NULL;
}
/*
* Dump the content of the crypto cache to a filedescriptor.
*/
void dump_crypto_cache(int fd)
{
int len;
int max_vol_length = 0;
int max_key_length = 0;
crypto_cache_entry_t *cce;
char dt1[MAX_TIME_LENGTH],
dt2[MAX_TIME_LENGTH];
POOL_MEM msg(PM_MESSAGE);
if (!cached_crypto_keys) {
return;
}
/*
* Lock the cache.
*/
P(crypto_cache_lock);
/*
* See how long the biggest volumename and key are.
*/
foreach_dlist(cce, cached_crypto_keys) {
if (strlen(cce->VolumeName) > max_vol_length) {
max_vol_length = strlen(cce->VolumeName);
}
if (strlen(cce->EncryptionKey) > max_key_length) {
max_key_length = strlen(cce->EncryptionKey);
}
}
len = Mmsg(msg, "%*s %*s %-20s %-20s\n", max_vol_length, _("Volumename"), max_key_length,
_("EncryptionKey"), _("Added"), _("Expires"));
write(fd, msg.c_str(), len);
foreach_dlist(cce, cached_crypto_keys) {
bstrutime(dt1, sizeof(dt1), cce->added);
bstrutime(dt2, sizeof(dt2), cce->added + CRYPTO_CACHE_MAX_AGE);
len = Mmsg(msg, "%*s %*s %-20s %-20s\n", max_vol_length, cce->VolumeName, max_key_length,
cce->EncryptionKey, dt1, dt2);
write(fd, msg.c_str(), len);
}
V(crypto_cache_lock);
}
/*
* Flush the date from the internal cache.
*/
void flush_crypto_cache(void)
{
if (!cached_crypto_keys) {
return;
}
/*
* Lock the cache.
*/
P(crypto_cache_lock);
cached_crypto_keys->destroy();
delete cached_crypto_keys;
cached_crypto_keys = NULL;
V(crypto_cache_lock);
}