-
Notifications
You must be signed in to change notification settings - Fork 18
/
wipe.c
1750 lines (1474 loc) · 52.6 KB
/
wipe.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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* wipe
*
* by Berke Durak
*
* With many thanks to the following contributors:
* Jason Axley
* Alexey Marinichev
* Chris L. Mason
* Karako Miklos
* Jim Paris
* Thomas Schoepf
*
* Author may be contacted at 'echo berke1lambda-diode|com 12 @.'
*
* URL for wipe: http://lambda-diode.com/software/wipe
* Git repository: http://github.com/berke/wipe
* git clone https://github.com/berke/wipe.git
*
* Securely erase files from magnetic media
* Based on data from article "Secure Deletion of Data from Magnetic
* and Solid-State Memory" from Peter Gutmann.
*
*/
/*** defines */
#define WIPE_VERSION "0.23"
#define WIPE_DATE "2016-11-03"
/* exit codes */
#define WIPE_EXIT_COMPLETE_SUCCESS 0
#define WIPE_EXIT_FAILURE 1
#define WIPE_EXIT_MINOR_ERROR 2
#define WIPE_EXIT_USER_ABORT 3
#define WIPE_EXIT_MANIPULATION_ERROR 4
/* NAME_MAX_TRIES is the maximum number of attempts made to find a free file
* name in the current directory, by checking for random file names.
*/
#define NAME_MAX_TRIES 10
/* NAME_MAX_PASSES is the maximum number of times a filename is renamed.
* The actual rename() calls made on a file might be less if during
* the wiping process wipe fails to find a free file name after NAME_TRIES.
*/
#define NAME_MAX_PASSES 1
/* BUFSIZE determines the default buffer size */
#define BUFLG2 14
#define BUFSIZE (1<<BUFLG2)
/* 64M, 2^12 bytes blocksize : 57.66 s
* 2^16 55.39
* 2^20 60.08
*/
/* defines ***/
/*** includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#ifdef FIND_DEVICE_SIZE_BY_BLKGETSIZE
#include <linux/fs.h>
#ifdef memset
#undef memset
#endif
#endif
#ifdef HAVE_GETOPT
#include <getopt.h>
#endif
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include "random.h"
#include "misc.h"
#include "version.h"
/* includes ***/
/*** more defines */
#define DEVRANDOM "/dev/urandom"
#define QUICKPASSES 4
/* Fix sent by Jason Axley: NAME_MAX is not defined under Solaris 2.6 so we need to
* define it to be something reasonable for Solaris and UFS. I define
* it here to be MAXNAMLEN, since that seems reasonable (much more
* reasonable than the POSIX value of 14!) and its the same thing done
* under linux glibc. The quote from the Solaris /usr/include/limits.h
* is included below for your edification:
*
* "POSIX 1003.1a, section 2.9.5, table 2-5 contains [NAME_MAX] and the
* related text states:
*
* A definition of one of the values from Table 2-5 shall be omitted from the
* <limits.h> on specific implementations where the corresponding value is
* equal to or greater than the stated minimum, but where the value can vary
* depending on the file to which it is applied. The actual value supported for
* a specific pathname shall be provided by the pathconf() (5.7.1) function.
*
* This is clear that any machine supporting multiple file system types
* and/or a network can not include this define, regardless of protection
* by the _POSIX_SOURCE and _POSIX_C_SOURCE flags.
*
* #define NAME_MAX 14".
*/
#ifndef NAME_MAX
#define NAME_MAX MAXNAMLEN
#endif
/* more defines ***/
/*** passinfo table */
/* P.Gutmann, in his article, was recommening to make
* the DETERMINISTIC passes (i.e. those that don't write random data)
* in random order, and to make 4 random passes before and after these.
*
* Here, the deterministic and the 8 random passes are made in random
* order, unless, of course, the "quick" mode is selected.
*/
struct passinfo_s {
int type;
int len;
char *pat;
} passinfo[] = {
{ 0, 0, 0, }, /* 1 random */
{ 0, 0, 0, }, /* 2 random */
{ 0, 0, 0, }, /* 3 random */
{ 0, 0, 0, }, /* 4 random */
#define FIRST_DETERMINISTIC_PASS 4
{ 1, 1, "\x55", }, /* 5 RLL MFM */
{ 1, 1, "\xaa", }, /* 6 RLL MFM */
{ 1, 3, "\x92\x49\x24", }, /* 7 RLL MFM */
{ 1, 3, "\x49\x24\x92", }, /* 8 RLL MFM */
{ 1, 3, "\x24\x92\x49", }, /* 9 RLL MFM */
{ 1, 1, "\x00", }, /* 10 RLL */
{ 1, 1, "\x11", }, /* 11 RLL */
{ 1, 1, "\x22", }, /* 12 RLL */
{ 1, 1, "\x33", }, /* 13 RLL */
{ 1, 1, "\x44", }, /* 14 RLL */
{ 1, 1, "\x55", }, /* 15 RLL */
{ 1, 1, "\x66", }, /* 16 RLL */
{ 1, 1, "\x77", }, /* 17 RLL */
{ 1, 1, "\x88", }, /* 18 RLL */
{ 1, 1, "\x99", }, /* 19 RLL */
{ 1, 1, "\xaa", }, /* 20 RLL */
{ 1, 1, "\xbb", }, /* 21 RLL */
{ 1, 1, "\xcc", }, /* 22 RLL */
{ 1, 1, "\xdd", }, /* 23 RLL */
{ 1, 1, "\xee", }, /* 24 RLL */
{ 1, 1, "\xff", }, /* 25 RLL */
{ 1, 3, "\x92\x49\x24", }, /* 26 RLL MFM */
{ 1, 3, "\x49\x24\x92", }, /* 27 RLL MFM */
{ 1, 3, "\x24\x92\x49", }, /* 28 RLL MFM */
{ 1, 3, "\x6d\xb6\xdb", }, /* 29 RLL */
{ 1, 3, "\xb6\xdb\x6d", }, /* 30 RLL */
{ 1, 3, "\xdb\x6d\xb6", }, /* 31 RLL */
#define LAST_DETERMINISTIC_PASS 30
{ 0, 0, 0, }, /* 32 random */
{ 0, 0, 0, }, /* 33 random */
{ 0, 0, 0, }, /* 34 random */
{ 0, 0, 0, }, /* 35 random */
};
#define NUM_DETERMINISTIC_PASSES (LAST_DETERMINISTIC_PASS - FIRST_DETERMINISTIC_PASS + 1)
#define MAX_PASSES (sizeof(passinfo)/(sizeof(*passinfo)))
#define BUFT_RANDOM (1<<0)
#define BUFT_USED (1<<1)
/* Passinfo table ***/
/*** errno, num_* statistics, middle_of_line */
extern int errno;
int num_errors = 0;
int num_files = 0;
int num_dirs = 0;
int num_spec = 0;
int num_symlinks = 0;
int middle_of_line = 0;
/* errno, num_* statistics, middle_of_line ***/
/*** options */
char *progname = 0;
int o_force = 0;
int o_dochmod = 0;
int o_errorabort = 0;
int o_recurse = 0;
int o_dereference_symlinks = 0;
int o_quick = 0;
int o_quick_passes = QUICKPASSES;
int o_quick_passes_set = 0;
int o_verbose = 0;
int o_silent = 0;
char *o_devrandom = DEVRANDOM;
int o_randseed = RANDS_DEVRANDOM;
int o_randalgo = RANDA_ARCFOUR;
int o_randseed_set = 0;
int o_no_remove = 0;
int o_dont_wipe_filenames = 0;
int o_name_max_tries = NAME_MAX_TRIES;
int o_name_max_passes = NAME_MAX_PASSES;
int o_dont_wipe_filesizes = 0;
off_t o_wipe_length;
off_t o_wipe_offset = 0;
int o_lg2_buffer_size = BUFLG2;
int o_buffer_size = 1<<BUFLG2;
int o_wipe_length_set = 0;
int o_wipe_exact_size = 0;
int o_skip_passes = 0;
int o_pass_order[MAX_PASSES] = { -1 };
/* End of Options ***/
static int wipe_filename_and_remove (char *fn);
/*** do_remove */
int do_remove (char *fn)
{
if (!o_no_remove) {
if (o_dont_wipe_filenames) return remove (fn);
else return wipe_filename_and_remove (fn);
} else return 0;
}
/* do_remove ***/
/*** signal_handler */
void (*abort_handler)(void *) = NULL;
void *abort_handler_arg = NULL;
#ifdef SIGHANDLER_RETURNS_INT
int signal_handler (int i)
#else
void signal_handler (int i)
#endif
{
fflush (stdout);
fflush (stderr);
if (middle_of_line) { fputc ('\n', stderr); middle_of_line = 0; }
fprintf (stderr, "*** Interrupted by signal %d\n", i);
if(abort_handler) abort_handler(abort_handler_arg);
/* exit () does fflush () */
exit (i == SIGINT?WIPE_EXIT_USER_ABORT:WIPE_EXIT_FAILURE);
}
/* signal_handler ***/
/*** fill_random_from_table */
/* This function is used to create random filenames */
inline static void fill_random_from_table (char *b,
int n, char *table, int mask)
{
u32 l;
for (; n >= 4; n -= 4) {
l = rand_Get32 ();
*(b++) = table[l & mask]; l >>=8;
*(b++) = table[l & mask]; l >>=8;
*(b++) = table[l & mask]; l >>=8;
*(b++) = table[l & mask];
}
if (n) {
l = rand_Get32 ();
while (n--) {
*(b++) = table[l & mask]; l >>= 8;
}
}
}
/* fill_random_from_table ***/
/*** fill_random */
/* fills buffer b with n (pseudo-)random bytes. */
inline static void fill_random (char *b, int n)
{
rand_Fill ((u8 *) b, n);
}
/* fill_random ***/
/*** fill_pattern */
/* this could be seriously optimised */
void fill_pattern (char *b, int n, char *p, int l)
{
int i;
char *p1, *p2;
for (p1 = p, p2 = p + l, i = 0; i<n; i++) {
*(b++) = *(p1++);
if (p1 == p2) p1 = p;
}
}
/* fill_pattern ***/
/*** pattern buffers and wipe info declarations */
struct wipe_pattern_buffer {
int type;
char *buffer;
int pat_len;
};
#define MAX_BUFFERS 30
#define RANDOM_BUFFERS 16
struct wipe_info {
int random_length;
int n_passes;
int n_buffers;
int current_pass;
struct wipe_pattern_buffer random_buffers[RANDOM_BUFFERS];
struct wipe_pattern_buffer buffers[MAX_BUFFERS];
struct wipe_pattern_buffer *passes[MAX_PASSES];
int p[MAX_PASSES];
};
/* pattern buffers and wipe info declarations ***/
/*** shut_wipe_info */
void shut_wipe_info (struct wipe_info *wi)
{
int i;
for (i = 0; i<RANDOM_BUFFERS; free (wi->random_buffers[i++].buffer));
for (i = 0; i<wi->n_buffers; free (wi->buffers[i++].buffer));
}
/* shut_wipe_info ***/
/*** dirty_all_buffers */
void dirty_all_buffers (struct wipe_info *wi)
{
int i;
for (i = 0; i<RANDOM_BUFFERS; wi->random_buffers[i++].type |= BUFT_USED);
}
/* dirty_all_buffers ***/
/*** revitalize_random_buffers */
int revitalize_random_buffers (struct wipe_info *wi)
{
int i;
for (i = 0; i<RANDOM_BUFFERS; i++) {
if (wi->random_buffers[i].type & BUFT_USED) {
fill_random (wi->random_buffers[i].buffer, wi->random_length);
wi->random_buffers[i].type &= ~BUFT_USED;
return 0;
}
}
return -1; /* there were no buffers to revitalize */
}
/* revitalize_random_buffers ***/
/*** wipe_pattern_buffer */
struct wipe_pattern_buffer *get_random_buffer (struct wipe_info *wi)
{
int i;
for (i = 0; i<RANDOM_BUFFERS; i++) {
if (!(wi->random_buffers[i].type & BUFT_USED)) {
wi->random_buffers[i].type |= BUFT_USED;
return &wi->random_buffers[i];
}
}
/* no random buffers available, revitalize buffer 0 */
fill_random (wi->random_buffers[0].buffer, wi->random_length);
/* no need to set used flag */
return &wi->random_buffers[0];
}
/* wipe_pattern_buffer ***/
/*** init_wipe_info */
void init_wipe_info (struct wipe_info *wi)
{
int i, j;
wi->n_passes = o_quick?o_quick_passes:MAX_PASSES;
/* allocate buffers for random patterns */
for (i = 0; i<RANDOM_BUFFERS; i ++) {
wi->random_buffers[i].type = BUFT_RANDOM;
wi->random_buffers[i].buffer = malloc (o_buffer_size);
if (!wi->random_buffers[i].buffer) {
fprintf (stderr, "could not allocate buffer [1]");
exit (EXIT_FAILURE);
}
}
/* allocate buffers for periodic patterns */
if (!o_quick)
for (wi->n_buffers = 0, i = 0; i<wi->n_passes; i++) {
if (!passinfo[i].type) {
wi->passes[i] = 0; /* random pass... */
} else {
/* look if this pattern has already been allocated */
for (j = 0; j<wi->n_buffers; j++) {
if (wi->buffers[i].pat_len == passinfo[i].len &&
!memcmp (wi->buffers[i].buffer, passinfo[i].pat, passinfo[i].len))
break;
}
if (j >= wi->n_buffers) {
/* unfortunately we'll have to allocate a new buffers */
j = wi->n_buffers ++;
wi->buffers[j].type = 1; /* periodic */
wi->buffers[j].buffer = malloc (o_buffer_size);
if (!wi->buffers[j].buffer) {
fprintf (stderr, "could not allocate buffer [2]");
exit (EXIT_FAILURE);
}
wi->buffers[j].pat_len = passinfo[i].len;
/* fill in pattern */
fill_pattern (wi->buffers[j].buffer,
o_buffer_size, passinfo[i].pat, passinfo[i].len);
}
wi->passes[i] = &wi->buffers[j];
}
}
}
/* init_wipe_info ***/
#define fnerror(x) { num_errors++; if(middle_of_line) fputc('\n', stderr); fprintf (stderr, "\r%.32s: " x ": %.32s\n", fn, strerror (errno)); }
#define fnerrorq(x) { num_errors++; if(middle_of_line) fputc('\n', stderr); fprintf (stderr, "\r%.32s: " x "\n", fn); }
#define FLUSH_MIDDLE if (middle_of_line) { fputc ('\n', stderr); \
middle_of_line = 0; }
/*** directory_name */
#if 0
inline static char *directory_name (char *fn)
{
char *dn;
char *sp;
int dl;
sp = strrchr (fn, '/');
if (!sp) return strdup ("");
dl = sp - fn + 1 + 1;
dn = malloc (dl);
if (!dn) return 0;
memcpy (dn, fn, dl - 1);
dn[dl] = 0;
return dn;
}
#else
inline static int directory_name_length (char *fn)
{
char *bn;
bn = strrchr (fn, '/');
if (!bn) return 0;
else return bn - fn + 1;
}
#endif
/* directory_name ***/
static char valid_filename_chars[64] =
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-.";
/*** wipe_filename_and_remove */
/* actually, after renaming a file, the only way to make sure that the
* name change is physically carried out is to call sync (), which flushes
* out ALL the disk caches of the system, whereas for
* reading and writing one can use the O_SYNC bit to get syncrhonous
* I/O for one file. as sync () is very slow, calling sync () after
* every rename () makes wipe extremely slow.
*/
static int wipe_filename_and_remove (char *fn)
{
int i, j, k, l;
int r = -1;
int fn_l, dn_l;
/* char *dn; */
char *buf[2];
struct stat st;
int t_l; /* target length */
/* dn = directory_name (fn); */
fn_l = strlen (fn);
dn_l = directory_name_length (fn);
buf[0] = malloc (fn_l + NAME_MAX + 1);
buf[1] = malloc (fn_l + NAME_MAX + 1);
r = 0;
t_l = fn_l - dn_l; /* first target length */
if (buf[0] && buf[1]) {
strcpy (buf[0], fn);
strcpy (buf[1], fn);
for (j = 1, i = 0; i < o_name_max_passes; j ^= 1, i++) {
for (k = o_name_max_tries; k; k--) {
l = t_l;
fill_random_from_table (buf[j] + dn_l, l,
valid_filename_chars, 0x3f);
buf[j][dn_l + l] = 0;
if (stat (buf[j], &st)) break;
}
if (k) {
if (!o_silent) {
fprintf (stderr, "\rRenaming %32.32s -> %32.32s", buf[j^1], buf[j]);
middle_of_line = 1;
fflush (stderr);
}
if (rename (buf[j^1], buf[j])) {
FLUSH_MIDDLE
fprintf (stderr, "%.32s: could not rename '%s' to '%s': %s (%d)\n",
fn, buf[j^1], buf[j], strerror (errno), errno);
r = -1;
break;
}
(void) sync ();
} else {
/* we could not find a target name of desired length, so
* increase target length until we find one. */
t_l ++;
j ^= 1;
}
}
if (remove (buf[j^1])) r = -1;
}
free (buf[0]); free (buf[1]);
return r;
}
/* wipe_filename_and_remove ***/
#include <sys/time.h>
static double
get_time_of_day(void)
{
struct timeval tv;
gettimeofday(&tv, 0);
return (tv.tv_usec * 1e-6 + tv.tv_sec);
}
static double eta_start_time;
static void
eta_begin()
{
eta_start_time = get_time_of_day();
}
static void
eta_progress(char *buf, size_t bufsiz, double frac)
{
double now;
double elapsed;
double total;
double seconds_per_year;
double remaining;
long lsec;
int sec;
long lmin;
int min;
long lhours;
int hours;
long ldays;
int days;
int weeks;
now = get_time_of_day();
elapsed = now - eta_start_time;
buf[0] = '\0';
if (elapsed < 5 && frac < 0.1)
return;
total = elapsed / frac;
seconds_per_year = 365.25 * 24 * 60 * 60;
remaining = total - elapsed;
if (remaining > seconds_per_year)
{
snprintf(buf, bufsiz, " ETA %g years", remaining / seconds_per_year);
return;
}
lsec = remaining;
sec = lsec % 60;
lmin = lsec / 60;
min = lmin % 60;
lhours = lmin / 60;
hours = lhours % 24;
ldays = lhours / 24;
days = ldays % 7;
weeks = ldays / 7;
if (weeks > 0)
{
snprintf(buf, bufsiz, " ETA %dw %dd", weeks, days);
return;
}
if (days > 0)
{
snprintf(buf, bufsiz, " ETA %dd %dh", days, hours);
return;
}
if (hours > 0)
{
snprintf(buf, bufsiz, " ETA %dh%02dm", hours, min);
return;
}
snprintf(buf, bufsiz, " ETA %dm%02ds", min, sec);
}
static void
pad(char *buf, size_t bufsiz)
{
size_t len;
len = strlen(buf);
while (len + 1 < bufsiz)
buf[len++] = ' ';
buf[len] = '\0';
}
static void
backspace(char *dst, const char *src)
{
while (*src++)
*dst++ = '\b';
*dst = '\0';
}
void wipe_continuation_message(void *arg)
{
int i;
struct wipe_info *wi = arg;
fprintf (stderr, "*** If you want to resume wiping while preserving the pass order, use these options:\n");
fprintf (stderr, "*** -X %d -x ", wi->current_pass);
for (i = 0; i<wi->n_passes; i++) {
if (i > 0) fprintf (stderr, ",");
fprintf (stderr, "%d", wi->p[i]);
}
fprintf(stderr, "\n");
fflush (stderr);
}
/*** dothejob -- nonrecursive wiping of a single file or device */
/* determine parameters of region to be wiped
* file descriptor,
* offset,
* size.
*
*
* compute the number of entire buffers needed to cover the region,
* and the size of the remaining area.
*
* if pattern buffers have not been computed, compute them.
*
* compute one pattern buffer (either fill it or fetch it).
* send an asynchronous write.
*
* select
*/
#define max(x,y) ((x>y)?x:y)
static int dothejob (char *fn)
{
int fd;
int bpi = 0; /* block progress indicator enabled ? */
int i;
off_t j;
static struct wipe_info wi;
static int wipe_info_initialized = 0;
struct wipe_pattern_buffer *wpb = 0;
int *p = wi.p;
struct stat st;
off_t buffers_to_wipe; /* number of buffers to write on device */
int first_buffer_size;
int last_buffer_size;
int this_buffer_size;
time_t lt = 0, t;
fd_set w_fd;
/* passing a null filename pointer means: free your internal buffers, please. */
/* thanks to Thomas Schoepf and Alexey Marinichev for pointing this out */
if (!fn) {
if (wipe_info_initialized)
shut_wipe_info (&wi);
abort_handler = NULL;
return 0;
}
/* to do a cryptographically strong random permutation on the
* order of the deterministic passes, we need
* lg_2(NUM_DETERMINISTIC_PASSES!) bits of entropy:
* this means about 128 bits.
*
* rand_seed () should give 31 bits of entropy per call
* we need a maximum of lg_2(NUM_DETERMNISTIC_PASSES) bits of entropy per random
* value. therefore we should call rand_seed () at least for
* every 31/lg_2(NUM_DETERMNISTIC_PASSES) ~= 6 random values.
*
* on the other way, as we'll constantly force disk access,
* /dev/urandom should have no difficulty providing us good random
* values.
*/
if (!o_quick && o_pass_order[0] < 0) {
for (i = 0; i<MAX_PASSES; p[i]=i, i++);
for (i = 0; i<NUM_DETERMINISTIC_PASSES-2; i++) {
int a, b;
/* a \in { 0, 1, ... NUM_DETERMINISTIC_PASSES-i-1 } */
/* since NUM_DETERMINISTIC_PASSES-i is not necessarily a divisor of
* 2^32, we won't get uniform distribution with this. however,
* since MAX_PASSES is very small compared to RAND_MAX, ti
*/
a = rand_Get32 () % (NUM_DETERMINISTIC_PASSES-i);
b = p[FIRST_DETERMINISTIC_PASS+i+a];
p[FIRST_DETERMINISTIC_PASS+i+a] = p[FIRST_DETERMINISTIC_PASS+i];
p[FIRST_DETERMINISTIC_PASS+i] = b;
}
}
if (o_pass_order[0] >= 0) {
for (i = 0; i<MAX_PASSES; i++) {
p[i] = o_pass_order[i];
}
}
/* see what kind of file it is */
if (o_dereference_symlinks ? stat (fn, &st) : lstat (fn, &st)) {
fnerror("stat or lstat error");
return -1;
}
if (S_ISDIR(st.st_mode)) {
if (!o_silent) fprintf (stderr, "Skipping directory %.32s...\n", fn);
return 1;
}
/* checks if the file is a regular file, block device or character
* device. used to refuse to wipe block/char devices. thanks
* to Dan Hollis for pointing out this.
*/
if (S_ISREG(st.st_mode) || S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
#ifdef HAVE_OSYNC
fd = open (fn, O_WRONLY | O_SYNC | O_NONBLOCK);
#else
fd = open (fn, O_WRONLY | O_NONBLOCK);
#endif
if (fd < 0) {
if (errno == EACCES) {
if (o_force || o_dochmod) {
if (chmod (fn, 0700)) {
fnerror("chmod error");
return -1;
} else fd = open (fn, O_WRONLY);
} else { fnerror("permission error: try -c"); return -1; }
} else { fnerror("open error"); return -1; }
}
if (fd < 0) { fnerror("open error even with chmod"); return -1; }
if (!o_wipe_length_set) {
if (S_ISBLK(st.st_mode)) {
#ifdef FIND_DEVICE_SIZE_BY_BLKGETSIZE
/* get device size in blocks */
{
#ifndef SIXTYFOUR
long l;
if (ioctl (fd, BLKGETSIZE, &l)) {
fnerror ("could not get device block size via ioctl BLKGETSIZE; check option -l");
return -1;
}
o_wipe_length = l << 9; /* assume 512-byte blocks */
#else
long long l;
if (ioctl (fd, BLKGETSIZE64, &l)) {
fnerror ("could not get device block size via ioctl BLKGETSIZE64; check option -l");
return -1;
}
o_wipe_length = l; /* BLKGETSIZE64 returns bytes */
#endif
}
#else
{
#ifdef SIXTYFOUR
long long l;
/* find device size by seeking... might work on some devices */
l = lseek64 (fd, 0, SEEK_END);
if (!l) {
fnerror ("could not get device block size with lseek; check option -l");
}
debugf ("lseek -> %d", l);
o_wipe_length = l;
#else
off_t l;
/* find device size by seeking... might work on some devices */
l = lseek (fd, 0, SEEK_END);
if (!l) {
fnerror ("could not get device block size with lseek; check option -l");
}
debugf ("lseek -> %d", l);
o_wipe_length = l;
#endif
}
#endif
debugf ("block device block size %d", st.st_blksize);
} else {
/* not a block device */
o_wipe_length = st.st_size;
if (!o_wipe_exact_size) {
o_wipe_length += st.st_blksize - (o_wipe_length % st.st_blksize);
}
}
o_wipe_length -= o_wipe_offset;
}
/* don't do anything to zero-sized files */
if (!o_wipe_length) {
goto skip_wipe;
}
#if SIXTYFOUR
debugf ("o_wipe_length = %Ld", o_wipe_length);
#else
debugf ("o_wipe_length = %ld", o_wipe_length);
#endif
/* compute number of writes... */
{
int fb, lb;
fb = o_wipe_offset >> o_lg2_buffer_size;
lb = (o_wipe_offset + o_wipe_length + o_buffer_size - 1) >> o_lg2_buffer_size;
buffers_to_wipe = lb - fb;
debugf ("fb = %d lb = %d", fb, lb);
if (buffers_to_wipe == 1) {
last_buffer_size = first_buffer_size = o_wipe_length;
} else {
first_buffer_size = o_buffer_size - (o_wipe_offset & (o_buffer_size - 1));
last_buffer_size = (o_wipe_offset + o_wipe_length) & (o_buffer_size - 1);
if (!last_buffer_size) last_buffer_size = o_buffer_size;
}
}
debugf ("buffers_to_wipe = %d first_buffer_size = %d last_buffer_size = %d",
buffers_to_wipe, first_buffer_size, last_buffer_size);
/* initialize wipe info */
if (!wipe_info_initialized) {
init_wipe_info (&wi);
wipe_info_initialized = 1;
abort_handler = wipe_continuation_message;
abort_handler_arg = &wi;
}
/* if the possibly existing leftover random buffers
* from the last wipe are shorter than what we need for
* now, mark all of them as used.
*
* ok it's possible to have a smarter random buffer management,
* but it only helps on small files (i.e. files smaller than the
* buffer size) so it's not really worth it (in other words:
* you can't escape from linear asymptotic complexity:)
*/
{
int x;
if (buffers_to_wipe <= 2)
x = max (first_buffer_size, last_buffer_size);
else
x = o_buffer_size;
if (x > wi.random_length)
dirty_all_buffers (&wi);
wi.random_length = x;
}
debugf ("buffers_to_wipe = %d, o_buffer_size = %d, wi.n_passes = %d",
buffers_to_wipe, o_buffer_size, wi.n_passes);
/* do the passes */
eta_begin();
for (i = o_skip_passes; i<wi.n_passes; i++) {
ssize_t wr;
wi.current_pass = i;
if (!o_silent) {
if (o_quick)
fprintf (stderr, "\rWipoing %.32s, pass %d in quick mode ", fn, i);
else
fprintf (stderr, "\rWiping %.32s, pass %-2d (%-2d) ", fn, i, p[i]);
middle_of_line = 1;
}
lseek (fd, o_wipe_offset, SEEK_SET);
if (!o_silent) lt = time (0);
for (j = 0; j<buffers_to_wipe; j ++) {
if (!j) this_buffer_size = first_buffer_size;
else if (j + 1 == buffers_to_wipe) this_buffer_size = last_buffer_size;
else this_buffer_size = o_buffer_size;
if (!o_silent) {
t = time (0);
if ((bpi && (t-lt)) || ((t-lt>2) && j<(buffers_to_wipe>>1))) {
char buf1[30];
char buf1_bs[sizeof (buf1)];
char buf2[18];
char buf2_bs[sizeof(buf2)];
snprintf(buf1, sizeof(buf1),
"[%8ld / %8ld]", (long) j, (long)buffers_to_wipe);
backspace(buf1_bs, buf1);
eta_progress(buf2, sizeof(buf2),