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
|
/*
* blkid probe utilities
*
* Copyright (C) 2018-2019 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU 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.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "utils_blkid.h"
#include "utils_io.h"
#ifdef HAVE_BLKID
#include <blkid/blkid.h>
/* make bad checksums flag optional */
#ifndef BLKID_SUBLKS_BADCSUM
#define BLKID_SUBLKS_BADCSUM 0
#endif
struct blkid_handle {
int fd;
blkid_probe pr;
};
#ifndef HAVE_BLKID_WIPE
static size_t crypt_getpagesize(void)
{
long r = sysconf(_SC_PAGESIZE);
return r <= 0 ? 4096 : (size_t)r;
}
#endif
#endif
void blk_set_chains_for_wipes(struct blkid_handle *h)
{
#ifdef HAVE_BLKID
blkid_probe_enable_partitions(h->pr, 1);
blkid_probe_set_partitions_flags(h->pr, 0
#ifdef HAVE_BLKID_WIPE
| BLKID_PARTS_MAGIC
#endif
);
blkid_probe_enable_superblocks(h->pr, 1);
blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_LABEL |
BLKID_SUBLKS_UUID |
BLKID_SUBLKS_TYPE |
BLKID_SUBLKS_USAGE |
BLKID_SUBLKS_VERSION |
BLKID_SUBLKS_MAGIC |
BLKID_SUBLKS_BADCSUM);
#endif
}
void blk_set_chains_for_full_print(struct blkid_handle *h)
{
blk_set_chains_for_wipes(h);
}
void blk_set_chains_for_fast_detection(struct blkid_handle *h)
{
#ifdef HAVE_BLKID
blkid_probe_enable_partitions(h->pr, 1);
blkid_probe_set_partitions_flags(h->pr, 0);
blkid_probe_enable_superblocks(h->pr, 1);
blkid_probe_set_superblocks_flags(h->pr, BLKID_SUBLKS_TYPE);
#endif
}
int blk_init_by_path(struct blkid_handle **h, const char *path)
{
int r = -ENOTSUP;
#ifdef HAVE_BLKID
struct blkid_handle *tmp = malloc(sizeof(*tmp));
if (!tmp)
return -ENOMEM;
tmp->fd = -1;
tmp->pr = blkid_new_probe_from_filename(path);
if (!tmp->pr) {
free(tmp);
return -EINVAL;
}
*h = tmp;
r = 0;
#endif
return r;
}
int blk_init_by_fd(struct blkid_handle **h, int fd)
{
int r = -ENOTSUP;
#ifdef HAVE_BLKID
struct blkid_handle *tmp = malloc(sizeof(*tmp));
if (!tmp)
return -ENOMEM;
tmp->pr = blkid_new_probe();
if (!tmp->pr) {
free(tmp);
return -EINVAL;
}
if (blkid_probe_set_device(tmp->pr, fd, 0, 0)) {
blkid_free_probe(tmp->pr);
free(tmp);
return -EINVAL;
}
tmp->fd = fd;
*h = tmp;
r = 0;
#endif
return r;
}
int blk_superblocks_filter_luks(struct blkid_handle *h)
{
int r = -ENOTSUP;
#ifdef HAVE_BLKID
char luks[] = "crypto_LUKS";
char *luks_filter[] = {
luks,
NULL
};
r = blkid_probe_filter_superblocks_type(h->pr, BLKID_FLTR_NOTIN, luks_filter);
#endif
return r;
}
blk_probe_status blk_probe(struct blkid_handle *h)
{
blk_probe_status pr = PRB_FAIL;
#ifdef HAVE_BLKID
int r = blkid_do_probe(h->pr);
if (r == 0)
pr = PRB_OK;
else if (r == 1)
pr = PRB_EMPTY;
#endif
return pr;
}
blk_probe_status blk_safeprobe(struct blkid_handle *h)
{
int r = -1;
#ifdef HAVE_BLKID
r = blkid_do_safeprobe(h->pr);
#endif
switch (r) {
case -2:
return PRB_AMBIGUOUS;
case 1:
return PRB_EMPTY;
case 0:
return PRB_OK;
default:
return PRB_FAIL;
}
}
int blk_is_partition(struct blkid_handle *h)
{
int r = 0;
#ifdef HAVE_BLKID
r = blkid_probe_has_value(h->pr, "PTTYPE");
#endif
return r;
}
int blk_is_superblock(struct blkid_handle *h)
{
int r = 0;
#ifdef HAVE_BLKID
r = blkid_probe_has_value(h->pr, "TYPE");
#endif
return r;
}
const char *blk_get_partition_type(struct blkid_handle *h)
{
const char *value = NULL;
#ifdef HAVE_BLKID
(void) blkid_probe_lookup_value(h->pr, "PTTYPE", &value, NULL);
#endif
return value;
}
const char *blk_get_superblock_type(struct blkid_handle *h)
{
const char *value = NULL;
#ifdef HAVE_BLKID
(void) blkid_probe_lookup_value(h->pr, "TYPE", &value, NULL);
#endif
return value;
}
void blk_free(struct blkid_handle *h)
{
#ifdef HAVE_BLKID
if (!h)
return;
if (h->pr)
blkid_free_probe(h->pr);
free(h);
#endif
}
#ifdef HAVE_BLKID
#ifndef HAVE_BLKID_WIPE
static int blk_step_back(struct blkid_handle *h)
{
#ifdef HAVE_BLKID_STEP_BACK
return blkid_probe_step_back(h->pr);
#else
blkid_reset_probe(h->pr);
blkid_probe_set_device(h->pr, h->fd, 0, 0);
return 0;
#endif
}
#endif /* not HAVE_BLKID_WIPE */
#endif /* HAVE_BLKID */
int blk_do_wipe(struct blkid_handle *h)
{
#ifdef HAVE_BLKID
#ifdef HAVE_BLKID_WIPE
return blkid_do_wipe(h->pr, 0);
#else
const char *offset;
off_t offset_val;
void *buf;
ssize_t ret;
size_t alignment, len, bsize = blkid_probe_get_sectorsize(h->pr);
if (h->fd < 0 || !bsize)
return -EINVAL;
if (blk_is_partition(h)) {
if (blkid_probe_lookup_value(h->pr, "PTMAGIC_OFFSET", &offset, NULL))
return -EINVAL;
if (blkid_probe_lookup_value(h->pr, "PTMAGIC", NULL, &len))
return -EINVAL;
} else if (blk_is_superblock(h)) {
if (blkid_probe_lookup_value(h->pr, "SBMAGIC_OFFSET", &offset, NULL))
return -EINVAL;
if (blkid_probe_lookup_value(h->pr, "SBMAGIC", NULL, &len))
return -EINVAL;
} else
return 0;
alignment = crypt_getpagesize();
if (posix_memalign(&buf, alignment, len))
return -EINVAL;
memset(buf, 0, len);
offset_val = strtoll(offset, NULL, 10);
/* TODO: missing crypt_wipe_fd() */
ret = write_lseek_blockwise(h->fd, bsize, alignment, buf, len, offset_val);
free(buf);
if (ret < 0)
return -EIO;
if ((size_t)ret == len) {
blk_step_back(h);
return 0;
}
return -EIO;
#endif
#else /* HAVE_BLKID */
return -ENOTSUP;
#endif
}
int blk_supported(void)
{
int r = 0;
#ifdef HAVE_BLKID
r = 1;
#endif
return r;
}
|