summaryrefslogtreecommitdiff
path: root/misc-utils/wipefs.c
blob: d3033446f615ff6488e19360a8a07fd6d1f23cbf (plain)
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
/*
 * wipefs - utility to wipe filesystems from device
 *
 * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
 * Written by Karel Zak <kzak@redhat.com>
 *
 * 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 would 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <string.h>
#include <limits.h>

#include <blkid.h>

#include "nls.h"
#include "xalloc.h"
#include "strutils.h"

struct wipe_desc {
	loff_t		offset;		/* magic string offset */
	size_t		len;		/* length of magic string */
	unsigned char	*magic;		/* magic string */

	int		zap;		/* zap this offset? */
	char		*usage;		/* raid, filesystem, ... */
	char		*type;		/* FS type */
	char		*label;		/* FS label */
	char		*uuid;		/* FS uuid */

	struct wipe_desc	*next;
};

#define WP_MODE_PRETTY		0		/* default */
#define WP_MODE_PARSABLE	1

static void
print_pretty(struct wipe_desc *wp, int line)
{
	if (!line) {
		printf("offset               type\n");
		printf("----------------------------------------------------------------\n");
	}

	printf("0x%-17jx  %s   [%s]", wp->offset, wp->type, wp->usage);

	if (wp->label && *wp->label)
		printf("\n%27s %s", "LABEL:", wp->label);
	if (wp->uuid)
		printf("\n%27s %s", "UUID: ", wp->uuid);
	puts("\n");
}

static void
print_parsable(struct wipe_desc *wp, int line)
{
	char enc[256];

	if (!line)
		printf("# offset,uuid,label,type\n");

	printf("0x%jx,", wp->offset);

	if (wp->uuid) {
		blkid_encode_string(wp->uuid, enc, sizeof(enc));
		printf("%s,", enc);
	} else
		fputc(',', stdout);

	if (wp->label) {
		blkid_encode_string(wp->label, enc, sizeof(enc));
		printf("%s,", enc);
	} else
		fputc(',', stdout);

	blkid_encode_string(wp->type, enc, sizeof(enc));
	printf("%s\n", enc);
}

static void
print_all(struct wipe_desc *wp, int mode)
{
	int n = 0;

	while (wp) {
		switch (mode) {
		case WP_MODE_PRETTY:
			print_pretty(wp, n++);
			break;
		case WP_MODE_PARSABLE:
			print_parsable(wp, n++);
			break;
		}
		wp = wp->next;
	}
}

static struct wipe_desc *
add_offset(struct wipe_desc *wp0, loff_t offset, int zap)
{
	struct wipe_desc *wp = wp0;

	while (wp) {
		if (wp->offset == offset)
			return wp;
		wp = wp->next;
	}

	wp = calloc(1, sizeof(struct wipe_desc));
	if (!wp)
		err(EXIT_FAILURE, _("calloc failed"));

	wp->offset = offset;
	wp->next = wp0;
	wp->zap = zap;
	return wp;
}

static struct wipe_desc *
get_offset_from_probe(struct wipe_desc *wp, blkid_probe pr, int zap)
{
	const char *off, *type, *usage, *mag;
	size_t len;

	if (blkid_probe_lookup_value(pr, "TYPE", &type, NULL) == 0 &&
	    blkid_probe_lookup_value(pr, "SBMAGIC_OFFSET", &off, NULL) == 0 &&
	    blkid_probe_lookup_value(pr, "SBMAGIC", &mag, &len) == 0 &&
	    blkid_probe_lookup_value(pr, "USAGE", &usage, NULL) == 0) {

		loff_t offset = strtoll(off, NULL, 10);
		const char *p;

		wp = add_offset(wp, offset, zap);
		if (!wp)
			return NULL;

		wp->usage = xstrdup(usage);
		wp->type = xstrdup(type);

		wp->magic = xmalloc(len);
		memcpy(wp->magic, mag, len);
		wp->len = len;

		if (blkid_probe_lookup_value(pr, "LABEL", &p, NULL) == 0)
			wp->label = xstrdup(p);

		if (blkid_probe_lookup_value(pr, "UUID", &p, NULL) == 0)
			wp->uuid = xstrdup(p);
	}

	return wp;
}

static struct wipe_desc *
read_offsets(struct wipe_desc *wp, const char *fname, int zap)
{
	blkid_probe pr;
	int rc;

	if (!fname)
		return NULL;

	pr = blkid_new_probe_from_filename(fname);
	if (!pr)
		errx(EXIT_FAILURE, _("error: %s: probing initialization failed"), fname);

	blkid_probe_enable_superblocks(pr, 0);	/* enabled by default ;-( */

	blkid_probe_enable_partitions(pr, 1);
	rc = blkid_do_fullprobe(pr);
	blkid_probe_enable_partitions(pr, 0);

	if (rc == 0) {
		const char *type = NULL;
		blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL);
		warnx(_("WARNING: %s: appears to contain '%s' "
				"partition table"), fname, type);
	}

	blkid_probe_enable_superblocks(pr, 1);
	blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_MAGIC |
			BLKID_SUBLKS_TYPE | BLKID_SUBLKS_USAGE |
			BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID);

	while (blkid_do_probe(pr) == 0) {
		wp = get_offset_from_probe(wp, pr, zap);
		if (!wp)
			break;
	}

	blkid_free_probe(pr);
	return wp;
}

static int
write_all(int fd, const void *buf, size_t count)
{
	while(count) {
		ssize_t tmp;

		errno = 0;
		tmp = write(fd, buf, count);
		if (tmp > 0) {
			count -= tmp;
			if (count)
				buf += tmp;
		} else if (errno != EINTR && errno != EAGAIN)
			return -1;
	}
	return 0;
}

static int
do_wipe_offset(int fd, struct wipe_desc *wp, const char *fname, int noact)
{
	char buf[BUFSIZ];
	int i;
	off_t l;
	size_t len;

	if (!wp->type) {
		warnx(_("no magic string found at offset "
			"0x%jx -- ignored"), wp->offset);
		return 0;
	}

	l = lseek(fd, wp->offset, SEEK_SET);
	if (l == (off_t) -1)
		err(EXIT_FAILURE, _("%s: failed to seek to offset 0x%jx"),
				fname, wp->offset);

	len = wp->len > sizeof(buf) ? sizeof(buf) : wp->len;

	memset(buf, 0, len);
	if (noact == 0 && write_all(fd, buf, len))
		err(EXIT_FAILURE, _("%s: write failed"), fname);

	printf(_("%zd bytes ["), wp->len);

	for (i = 0; i < len; i++) {
		printf("%02x", wp->magic[i]);
		if (i + 1 < len)
			fputc(' ', stdout);
	}

	printf(_("] erased at offset 0x%jx (%s)\n"), wp->offset, wp->type);
	return 0;
}

static int
do_wipe(struct wipe_desc *wp, const char *fname, int noact)
{
	int fd;

	fd = open(fname, O_WRONLY);
	if (fd < 0)
		err(EXIT_FAILURE, _("%s: open failed"), fname);

	while (wp) {
		if (wp->zap)
			do_wipe_offset(fd, wp, fname, noact);
		wp = wp->next;
	}

	close(fd);
	return 0;
}

static void
free_wipe(struct wipe_desc *wp)
{
	while (wp) {
		struct wipe_desc *next = wp->next;

		free(wp->usage);
		free(wp->type);
		free(wp->magic);
		free(wp->label);
		free(wp->uuid);
		free(wp);

		wp = next;
	}
}

static loff_t
strtoll_offset(const char *str)
{
	uintmax_t sz;

	if (strtosize(str, &sz))
		errx(EXIT_FAILURE, _("invalid offset value '%s' specified"), str);
	return sz;
}


static void __attribute__((__noreturn__))
usage(FILE *out)
{
	fprintf(out, _("Usage: %s [options] <device>\n\nOptions:\n"),
			program_invocation_short_name);

	fprintf(out, _(
	" -a, --all           wipe all magic strings (BE CAREFUL!)\n"
	" -h, --help          show this help text\n"
	" -n, --no-act        do everything except the actual write() call\n"
	" -o, --offset <num>  offset to erase, in bytes\n"
	" -p, --parsable      print out in parsable instead of printable format\n"));

	fprintf(out, _("\nFor more information see wipefs(8).\n"));

	exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}


int
main(int argc, char **argv)
{
	struct wipe_desc *wp = NULL;
	int c, all = 0, has_offset = 0, noact = 0, mode = 0;
	const char *fname;

	struct option longopts[] = {
	    { "all",       0, 0, 'a' },
	    { "help",      0, 0, 'h' },
	    { "no-act",    0, 0, 'n' },
	    { "offset",    1, 0, 'o' },
	    { "parsable",  0, 0, 'p' },
	    { NULL,        0, 0, 0 }
	};

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, LOCALEDIR);
	textdomain(PACKAGE);

	while ((c = getopt_long(argc, argv, "ahno:p", longopts, NULL)) != -1) {
		switch(c) {
		case 'a':
			all++;
			break;
		case 'h':
			usage(stdout);
			break;
		case 'n':
			noact++;
			break;
		case 'o':
			wp = add_offset(wp, strtoll_offset(optarg), 1);
			has_offset++;
			break;
		case 'p':
			mode = WP_MODE_PARSABLE;
			break;
		default:
			usage(stderr);
			break;
		}
	}

	if (wp && all)
		errx(EXIT_FAILURE, _("--offset and --all are mutually exclusive"));
	if (optind == argc)
		usage(stderr);

	fname = argv[optind++];

	wp = read_offsets(wp, fname, all);

	if (wp) {
		if (has_offset || all)
			do_wipe(wp, fname, noact);
		else
			print_all(wp, mode);

		free_wipe(wp);
	}
	return EXIT_SUCCESS;
}