Tizen 2.0 Release
[external/tizen-coreutils.git] / src / shred.c
1 /* shred.c - overwrite files and devices to make it harder to recover data
2
3    Copyright (C) 1999-2006 Free Software Foundation, Inc.
4    Copyright (C) 1997, 1998, 1999 Colin Plumb.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20    Written by Colin Plumb.  */
21
22 /* TODO:
23    - use consistent non-capitalization in error messages
24    - add standard GNU copyleft comment
25
26   - Add -r/-R/--recursive
27   - Add -i/--interactive
28   - Reserve -d
29   - Add -L
30   - Add an unlink-all option to emulate rm.
31  */
32
33 /*
34  * Do a more secure overwrite of given files or devices, to make it harder
35  * for even very expensive hardware probing to recover the data.
36  *
37  * Although this process is also known as "wiping", I prefer the longer
38  * name both because I think it is more evocative of what is happening and
39  * because a longer name conveys a more appropriate sense of deliberateness.
40  *
41  * For the theory behind this, see "Secure Deletion of Data from Magnetic
42  * and Solid-State Memory", on line at
43  * http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html
44  *
45  * Just for the record, reversing one or two passes of disk overwrite
46  * is not terribly difficult with hardware help.  Hook up a good-quality
47  * digitizing oscilloscope to the output of the head preamplifier and copy
48  * the high-res digitized data to a computer for some off-line analysis.
49  * Read the "current" data and average all the pulses together to get an
50  * "average" pulse on the disk.  Subtract this average pulse from all of
51  * the actual pulses and you can clearly see the "echo" of the previous
52  * data on the disk.
53  *
54  * Real hard drives have to balance the cost of the media, the head,
55  * and the read circuitry.  They use better-quality media than absolutely
56  * necessary to limit the cost of the read circuitry.  By throwing that
57  * assumption out, and the assumption that you want the data processed
58  * as fast as the hard drive can spin, you can do better.
59  *
60  * If asked to wipe a file, this also unlinks it, renaming it to in a
61  * clever way to try to leave no trace of the original filename.
62  *
63  * This was inspired by a desire to improve on some code titled:
64  * Wipe V1.0-- Overwrite and delete files.  S. 2/3/96
65  * but I've rewritten everything here so completely that no trace of
66  * the original remains.
67  *
68  * Thanks to:
69  * Bob Jenkins, for his good RNG work and patience with the FSF copyright
70  * paperwork.
71  * Jim Meyering, for his work merging this into the GNU fileutils while
72  * still letting me feel a sense of ownership and pride.  Getting me to
73  * tolerate the GNU brace style was quite a feat of diplomacy.
74  * Paul Eggert, for lots of useful discussion and code.  I disagree with
75  * an awful lot of his suggestions, but they're disagreements worth having.
76  *
77  * Things to think about:
78  * - Security: Is there any risk to the race
79  *   between overwriting and unlinking a file?  Will it do anything
80  *   drastically bad if told to attack a named pipe or socket?
81  */
82
83 /* The official name of this program (e.g., no `g' prefix).  */
84 #define PROGRAM_NAME "shred"
85
86 #define AUTHORS "Colin Plumb"
87
88 #include <config.h>
89
90 #include <getopt.h>
91 #include <stdio.h>
92 #include <assert.h>
93 #include <setjmp.h>
94 #include <sys/types.h>
95
96 #include "system.h"
97 #include "xstrtol.h"
98 #include "error.h"
99 #include "fcntl--.h"
100 #include "getpagesize.h"
101 #include "human.h"
102 #include "inttostr.h"
103 #include "quotearg.h"           /* For quotearg_colon */
104 #include "quote.h"              /* For quotearg_colon */
105 #include "randint.h"
106 #include "randread.h"
107
108 /* Default number of times to overwrite.  */
109 enum { DEFAULT_PASSES = 25 };
110
111 /* How many seconds to wait before checking whether to output another
112    verbose output line.  */
113 enum { VERBOSE_UPDATE = 5 };
114
115 /* Sector size and corresponding mask, for recovering after write failures.
116    The size must be a power of 2.  */
117 enum { SECTOR_SIZE = 512 };
118 enum { SECTOR_MASK = SECTOR_SIZE - 1 };
119 verify (0 < SECTOR_SIZE && (SECTOR_SIZE & SECTOR_MASK) == 0);
120
121 struct Options
122 {
123   bool force;           /* -f flag: chmod files if necessary */
124   size_t n_iterations;  /* -n flag: Number of iterations */
125   off_t size;           /* -s flag: size of file */
126   bool remove_file;     /* -u flag: remove file after shredding */
127   bool verbose;         /* -v flag: Print progress */
128   bool exact;           /* -x flag: Do not round up file size */
129   bool zero_fill;       /* -z flag: Add a final zero pass */
130 };
131
132 /* For long options that have no equivalent short option, use a
133    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
134 enum
135 {
136   RANDOM_SOURCE_OPTION = CHAR_MAX + 1
137 };
138
139 static struct option const long_opts[] =
140 {
141   {"exact", no_argument, NULL, 'x'},
142   {"force", no_argument, NULL, 'f'},
143   {"iterations", required_argument, NULL, 'n'},
144   {"size", required_argument, NULL, 's'},
145   {"random-source", required_argument, NULL, RANDOM_SOURCE_OPTION},
146   {"remove", no_argument, NULL, 'u'},
147   {"verbose", no_argument, NULL, 'v'},
148   {"zero", no_argument, NULL, 'z'},
149   {GETOPT_HELP_OPTION_DECL},
150   {GETOPT_VERSION_OPTION_DECL},
151   {NULL, 0, NULL, 0}
152 };
153
154 /* Global variable for error printing purposes */
155 char const *program_name; /* Initialized before any possible use */
156
157 void
158 usage (int status)
159 {
160   if (status != EXIT_SUCCESS)
161     fprintf (stderr, _("Try `%s --help' for more information.\n"),
162              program_name);
163   else
164     {
165       printf (_("Usage: %s [OPTIONS] FILE [...]\n"), program_name);
166       fputs (_("\
167 Overwrite the specified FILE(s) repeatedly, in order to make it harder\n\
168 for even very expensive hardware probing to recover the data.\n\
169 \n\
170 "), stdout);
171       fputs (_("\
172 Mandatory arguments to long options are mandatory for short options too.\n\
173 "), stdout);
174       printf (_("\
175   -f, --force    change permissions to allow writing if necessary\n\
176   -n, --iterations=N  Overwrite N times instead of the default (%d)\n\
177       --random-source=FILE  get random bytes from FILE (default /dev/urandom)\n\
178   -s, --size=N   shred this many bytes (suffixes like K, M, G accepted)\n\
179 "), DEFAULT_PASSES);
180       fputs (_("\
181   -u, --remove   truncate and remove file after overwriting\n\
182   -v, --verbose  show progress\n\
183   -x, --exact    do not round file sizes up to the next full block;\n\
184                    this is the default for non-regular files\n\
185   -z, --zero     add a final overwrite with zeros to hide shredding\n\
186 "), stdout);
187       fputs (HELP_OPTION_DESCRIPTION, stdout);
188       fputs (VERSION_OPTION_DESCRIPTION, stdout);
189       fputs (_("\
190 \n\
191 If FILE is -, shred standard output.\n\
192 \n\
193 Delete FILE(s) if --remove (-u) is specified.  The default is not to remove\n\
194 the files because it is common to operate on device files like /dev/hda,\n\
195 and those files usually should not be removed.  When operating on regular\n\
196 files, most people use the --remove option.\n\
197 \n\
198 "), stdout);
199       fputs (_("\
200 CAUTION: Note that shred relies on a very important assumption:\n\
201 that the file system overwrites data in place.  This is the traditional\n\
202 way to do things, but many modern file system designs do not satisfy this\n\
203 assumption.  The following are examples of file systems on which shred is\n\
204 not effective, or is not guaranteed to be effective in all file system modes:\n\
205 \n\
206 "), stdout);
207       fputs (_("\
208 * log-structured or journaled file systems, such as those supplied with\n\
209 AIX and Solaris (and JFS, ReiserFS, XFS, Ext3, etc.)\n\
210 \n\
211 * file systems that write redundant data and carry on even if some writes\n\
212 fail, such as RAID-based file systems\n\
213 \n\
214 * file systems that make snapshots, such as Network Appliance's NFS server\n\
215 \n\
216 "), stdout);
217       fputs (_("\
218 * file systems that cache in temporary locations, such as NFS\n\
219 version 3 clients\n\
220 \n\
221 * compressed file systems\n\
222 \n\
223 "), stdout);
224       fputs (_("\
225 In the case of ext3 file systems, the above disclaimer applies\n\
226 (and shred is thus of limited effectiveness) only in data=journal mode,\n\
227 which journals file data in addition to just metadata.  In both the\n\
228 data=ordered (default) and data=writeback modes, shred works as usual.\n\
229 Ext3 journaling modes can be changed by adding the data=something option\n\
230 to the mount options for a particular file system in the /etc/fstab file,\n\
231 as documented in the mount man page (man mount).\n\
232 \n\
233 "), stdout);
234       fputs (_("\
235 In addition, file system backups and remote mirrors may contain copies\n\
236 of the file that cannot be removed, and that will allow a shredded file\n\
237 to be recovered later.\n\
238 "), stdout);
239       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
240     }
241   exit (status);
242 }
243
244
245 /*
246  * Fill a buffer with a fixed pattern.
247  *
248  * The buffer must be at least 3 bytes long, even if
249  * size is less.  Larger sizes are filled exactly.
250  */
251 static void
252 fillpattern (int type, unsigned char *r, size_t size)
253 {
254   size_t i;
255   unsigned int bits = type & 0xfff;
256
257   bits |= bits << 12;
258   r[0] = (bits >> 4) & 255;
259   r[1] = (bits >> 8) & 255;
260   r[2] = bits & 255;
261   for (i = 3; i < size / 2; i *= 2)
262     memcpy (r + i, r, i);
263   if (i < size)
264     memcpy (r + i, r, size - i);
265
266   /* Invert the first bit of every sector. */
267   if (type & 0x1000)
268     for (i = 0; i < size; i += SECTOR_SIZE)
269       r[i] ^= 0x80;
270 }
271
272 /*
273  * Generate a 6-character (+ nul) pass name string
274  * FIXME: allow translation of "random".
275  */
276 #define PASS_NAME_SIZE 7
277 static void
278 passname (unsigned char const *data, char name[PASS_NAME_SIZE])
279 {
280   if (data)
281     sprintf (name, "%02x%02x%02x", data[0], data[1], data[2]);
282   else
283     memcpy (name, "random", PASS_NAME_SIZE);
284 }
285
286 /* Request that all data for FD be transferred to the corresponding
287    storage device.  QNAME is the file name (quoted for colons).
288    Report any errors found.  Return 0 on success, -1
289    (setting errno) on failure.  It is not an error if fdatasync and/or
290    fsync is not supported for this file, or if the file is not a
291    writable file descriptor.  */
292 static int
293 dosync (int fd, char const *qname)
294 {
295   int err;
296
297 #if HAVE_FDATASYNC
298   if (fdatasync (fd) == 0)
299     return 0;
300   err = errno;
301   if (err != EINVAL && err != EBADF)
302     {
303       error (0, err, _("%s: fdatasync failed"), qname);
304       errno = err;
305       return -1;
306     }
307 #endif
308
309   if (fsync (fd) == 0)
310     return 0;
311   err = errno;
312   if (err != EINVAL && err != EBADF)
313     {
314       error (0, err, _("%s: fsync failed"), qname);
315       errno = err;
316       return -1;
317     }
318
319   sync ();
320   return 0;
321 }
322
323 /* Turn on or off direct I/O mode for file descriptor FD, if possible.
324    Try to turn it on if ENABLE is true.  Otherwise, try to turn it off.  */
325 static void
326 direct_mode (int fd, bool enable)
327 {
328   if (O_DIRECT)
329     {
330       int fd_flags = fcntl (fd, F_GETFL);
331       if (0 < fd_flags)
332         {
333           int new_flags = (enable
334                            ? (fd_flags | O_DIRECT)
335                            : (fd_flags & ~O_DIRECT));
336           if (new_flags != fd_flags)
337             fcntl (fd, F_SETFL, new_flags);
338         }
339     }
340
341 #if HAVE_DIRECTIO && defined DIRECTIO_ON && defined DIRECTIO_OFF
342   /* This is Solaris-specific.  See the following for details:
343      http://docs.sun.com/db/doc/816-0213/6m6ne37so?q=directio&a=view  */
344   directio (fd, enable ? DIRECTIO_ON : DIRECTIO_OFF);
345 #endif
346 }
347
348 /*
349  * Do pass number k of n, writing "size" bytes of the given pattern "type"
350  * to the file descriptor fd.   Qname, k and n are passed in only for verbose
351  * progress message purposes.  If n == 0, no progress messages are printed.
352  *
353  * If *sizep == -1, the size is unknown, and it will be filled in as soon
354  * as writing fails.
355  *
356  * Return 1 on write error, -1 on other error, 0 on success.
357  */
358 static int
359 dopass (int fd, char const *qname, off_t *sizep, int type,
360         struct randread_source *s, unsigned long int k, unsigned long int n)
361 {
362   off_t size = *sizep;
363   off_t offset;                 /* Current file posiiton */
364   time_t thresh IF_LINT (= 0);  /* Time to maybe print next status update */
365   time_t now = 0;               /* Current time */
366   size_t lim;                   /* Amount of data to try writing */
367   size_t soff;                  /* Offset into buffer for next write */
368   ssize_t ssize;                /* Return value from write */
369
370   /* Fill pattern buffer.  Aligning it to a 32-bit boundary speeds up randread
371      in some cases.  */
372   typedef uint32_t fill_pattern_buffer[3 * 1024];
373   union
374   {
375     fill_pattern_buffer buffer;
376     char c[sizeof (fill_pattern_buffer)];
377     unsigned char u[sizeof (fill_pattern_buffer)];
378   } r;
379
380   off_t sizeof_r = sizeof r;
381   char pass_string[PASS_NAME_SIZE];     /* Name of current pass */
382   bool write_error = false;
383   bool first_write = true;
384
385   /* Printable previous offset into the file */
386   char previous_offset_buf[LONGEST_HUMAN_READABLE + 1];
387   char const *previous_human_offset IF_LINT (= 0);
388
389   if (lseek (fd, 0, SEEK_SET) == -1)
390     {
391       error (0, errno, _("%s: cannot rewind"), qname);
392       return -1;
393     }
394
395   /* Constant fill patterns need only be set up once. */
396   if (type >= 0)
397     {
398       lim = (0 <= size && size < sizeof_r ? size : sizeof r);
399       fillpattern (type, r.u, lim);
400       passname (r.u, pass_string);
401     }
402   else
403     {
404       passname (0, pass_string);
405     }
406
407   /* Set position if first status update */
408   if (n)
409     {
410       error (0, 0, _("%s: pass %lu/%lu (%s)..."), qname, k, n, pass_string);
411       thresh = time (NULL) + VERBOSE_UPDATE;
412       previous_human_offset = "";
413     }
414
415   offset = 0;
416   for (;;)
417     {
418       /* How much to write this time? */
419       lim = sizeof r;
420       if (0 <= size && size - offset < sizeof_r)
421         {
422           if (size < offset)
423             break;
424           lim = size - offset;
425           if (!lim)
426             break;
427         }
428       if (type < 0)
429         randread (s, &r, lim);
430       /* Loop to retry partial writes. */
431       for (soff = 0; soff < lim; soff += ssize, first_write = false)
432         {
433           ssize = write (fd, r.c + soff, lim - soff);
434           if (ssize <= 0)
435             {
436               if (size < 0 && (ssize == 0 || errno == ENOSPC))
437                 {
438                   /* Ah, we have found the end of the file */
439                   *sizep = size = offset + soff;
440                   break;
441                 }
442               else
443                 {
444                   int errnum = errno;
445                   char buf[INT_BUFSIZE_BOUND (uintmax_t)];
446
447                   /* If the first write of the first pass for a given file
448                      has just failed with EINVAL, turn off direct mode I/O
449                      and try again.  This works around a bug in linux-2.4
450                      whereby opening with O_DIRECT would succeed for some
451                      file system types (e.g., ext3), but any attempt to
452                      access a file through the resulting descriptor would
453                      fail with EINVAL.  */
454                   if (k == 1 && first_write && errno == EINVAL)
455                     {
456                       direct_mode (fd, false);
457                       ssize = 0;
458                       continue;
459                     }
460                   error (0, errnum, _("%s: error writing at offset %s"),
461                          qname, umaxtostr (offset + soff, buf));
462
463                   /* 'shred' is often used on bad media, before throwing it
464                      out.  Thus, it shouldn't give up on bad blocks.  This
465                      code works because lim is always a multiple of
466                      SECTOR_SIZE, except at the end.  */
467                   verify (sizeof r % SECTOR_SIZE == 0);
468                   if (errnum == EIO && 0 <= size && (soff | SECTOR_MASK) < lim)
469                     {
470                       size_t soff1 = (soff | SECTOR_MASK) + 1;
471                       if (lseek (fd, offset + soff1, SEEK_SET) != -1)
472                         {
473                           /* Arrange to skip this block. */
474                           ssize = soff1 - soff;
475                           write_error = true;
476                           continue;
477                         }
478                       error (0, errno, _("%s: lseek failed"), qname);
479                     }
480                   return -1;
481                 }
482             }
483         }
484
485       /* Okay, we have written "soff" bytes. */
486
487       if (offset + soff < offset)
488         {
489           error (0, 0, _("%s: file too large"), qname);
490           return -1;
491         }
492
493       offset += soff;
494
495       /* Time to print progress? */
496       if (n
497           && ((offset == size && *previous_human_offset)
498               || thresh <= (now = time (NULL))))
499         {
500           char offset_buf[LONGEST_HUMAN_READABLE + 1];
501           char size_buf[LONGEST_HUMAN_READABLE + 1];
502           int human_progress_opts = (human_autoscale | human_SI
503                                      | human_base_1024 | human_B);
504           char const *human_offset
505             = human_readable (offset, offset_buf,
506                               human_floor | human_progress_opts, 1, 1);
507
508           if (offset == size
509               || !STREQ (previous_human_offset, human_offset))
510             {
511               if (size < 0)
512                 error (0, 0, _("%s: pass %lu/%lu (%s)...%s"),
513                        qname, k, n, pass_string, human_offset);
514               else
515                 {
516                   uintmax_t off = offset;
517                   int percent = (size == 0
518                                  ? 100
519                                  : (off <= TYPE_MAXIMUM (uintmax_t) / 100
520                                     ? off * 100 / size
521                                     : off / (size / 100)));
522                   char const *human_size
523                     = human_readable (size, size_buf,
524                                       human_ceiling | human_progress_opts,
525                                       1, 1);
526                   if (offset == size)
527                     human_offset = human_size;
528                   error (0, 0, _("%s: pass %lu/%lu (%s)...%s/%s %d%%"),
529                          qname, k, n, pass_string, human_offset, human_size,
530                          percent);
531                 }
532
533               strcpy (previous_offset_buf, human_offset);
534               previous_human_offset = previous_offset_buf;
535               thresh = now + VERBOSE_UPDATE;
536
537               /*
538                * Force periodic syncs to keep displayed progress accurate
539                * FIXME: Should these be present even if -v is not enabled,
540                * to keep the buffer cache from filling with dirty pages?
541                * It's a common problem with programs that do lots of writes,
542                * like mkfs.
543                */
544               if (dosync (fd, qname) != 0)
545                 {
546                   if (errno != EIO)
547                     return -1;
548                   write_error = true;
549                 }
550             }
551         }
552     }
553
554   /* Force what we just wrote to hit the media. */
555   if (dosync (fd, qname) != 0)
556     {
557       if (errno != EIO)
558         return -1;
559       write_error = true;
560     }
561
562   return write_error;
563 }
564
565 /*
566  * The passes start and end with a random pass, and the passes in between
567  * are done in random order.  The idea is to deprive someone trying to
568  * reverse the process of knowledge of the overwrite patterns, so they
569  * have the additional step of figuring out what was done to the disk
570  * before they can try to reverse or cancel it.
571  *
572  * First, all possible 1-bit patterns.  There are two of them.
573  * Then, all possible 2-bit patterns.  There are four, but the two
574  * which are also 1-bit patterns can be omitted.
575  * Then, all possible 3-bit patterns.  Likewise, 8-2 = 6.
576  * Then, all possible 4-bit patterns.  16-4 = 12.
577  *
578  * The basic passes are:
579  * 1-bit: 0x000, 0xFFF
580  * 2-bit: 0x555, 0xAAA
581  * 3-bit: 0x249, 0x492, 0x924, 0x6DB, 0xB6D, 0xDB6 (+ 1-bit)
582  *        100100100100         110110110110
583  *           9   2   4            D   B   6
584  * 4-bit: 0x111, 0x222, 0x333, 0x444, 0x666, 0x777,
585  *        0x888, 0x999, 0xBBB, 0xCCC, 0xDDD, 0xEEE (+ 1-bit, 2-bit)
586  * Adding three random passes at the beginning, middle and end
587  * produces the default 25-pass structure.
588  *
589  * The next extension would be to 5-bit and 6-bit patterns.
590  * There are 30 uncovered 5-bit patterns and 64-8-2 = 46 uncovered
591  * 6-bit patterns, so they would increase the time required
592  * significantly.  4-bit patterns are enough for most purposes.
593  *
594  * The main gotcha is that this would require a trickier encoding,
595  * since lcm(2,3,4) = 12 bits is easy to fit into an int, but
596  * lcm(2,3,4,5) = 60 bits is not.
597  *
598  * One extension that is included is to complement the first bit in each
599  * 512-byte block, to alter the phase of the encoded data in the more
600  * complex encodings.  This doesn't apply to MFM, so the 1-bit patterns
601  * are considered part of the 3-bit ones and the 2-bit patterns are
602  * considered part of the 4-bit patterns.
603  *
604  *
605  * How does the generalization to variable numbers of passes work?
606  *
607  * Here's how...
608  * Have an ordered list of groups of passes.  Each group is a set.
609  * Take as many groups as will fit, plus a random subset of the
610  * last partial group, and place them into the passes list.
611  * Then shuffle the passes list into random order and use that.
612  *
613  * One extra detail: if we can't include a large enough fraction of the
614  * last group to be interesting, then just substitute random passes.
615  *
616  * If you want more passes than the entire list of groups can
617  * provide, just start repeating from the beginning of the list.
618  */
619 static int const
620   patterns[] =
621 {
622   -2,                           /* 2 random passes */
623   2, 0x000, 0xFFF,              /* 1-bit */
624   2, 0x555, 0xAAA,              /* 2-bit */
625   -1,                           /* 1 random pass */
626   6, 0x249, 0x492, 0x6DB, 0x924, 0xB6D, 0xDB6,  /* 3-bit */
627   12, 0x111, 0x222, 0x333, 0x444, 0x666, 0x777,
628   0x888, 0x999, 0xBBB, 0xCCC, 0xDDD, 0xEEE,     /* 4-bit */
629   -1,                           /* 1 random pass */
630         /* The following patterns have the frst bit per block flipped */
631   8, 0x1000, 0x1249, 0x1492, 0x16DB, 0x1924, 0x1B6D, 0x1DB6, 0x1FFF,
632   14, 0x1111, 0x1222, 0x1333, 0x1444, 0x1555, 0x1666, 0x1777,
633   0x1888, 0x1999, 0x1AAA, 0x1BBB, 0x1CCC, 0x1DDD, 0x1EEE,
634   -1,                           /* 1 random pass */
635   0                             /* End */
636 };
637
638 /*
639  * Generate a random wiping pass pattern with num passes.
640  * This is a two-stage process.  First, the passes to include
641  * are chosen, and then they are shuffled into the desired
642  * order.
643  */
644 static void
645 genpattern (int *dest, size_t num, struct randint_source *s)
646 {
647   size_t randpasses;
648   int const *p;
649   int *d;
650   size_t n;
651   size_t accum, top, swap;
652   int k;
653
654   if (!num)
655     return;
656
657   /* Stage 1: choose the passes to use */
658   p = patterns;
659   randpasses = 0;
660   d = dest;                     /* Destination for generated pass list */
661   n = num;                      /* Passes remaining to fill */
662
663   for (;;)
664     {
665       k = *p++;                 /* Block descriptor word */
666       if (!k)
667         {                       /* Loop back to the beginning */
668           p = patterns;
669         }
670       else if (k < 0)
671         {                       /* -k random passes */
672           k = -k;
673           if ((size_t) k >= n)
674             {
675               randpasses += n;
676               n = 0;
677               break;
678             }
679           randpasses += k;
680           n -= k;
681         }
682       else if ((size_t) k <= n)
683         {                       /* Full block of patterns */
684           memcpy (d, p, k * sizeof (int));
685           p += k;
686           d += k;
687           n -= k;
688         }
689       else if (n < 2 || 3 * n < (size_t) k)
690         {                       /* Finish with random */
691           randpasses += n;
692           break;
693         }
694       else
695         {                       /* Pad out with k of the n available */
696           do
697             {
698               if (n == (size_t) k || randint_choose (s, k) < n)
699                 {
700                   *d++ = *p;
701                   n--;
702                 }
703               p++;
704             }
705           while (n);
706           break;
707         }
708     }
709   top = num - randpasses;       /* Top of initialized data */
710   /* assert (d == dest+top); */
711
712   /*
713    * We now have fixed patterns in the dest buffer up to
714    * "top", and we need to scramble them, with "randpasses"
715    * random passes evenly spaced among them.
716    *
717    * We want one at the beginning, one at the end, and
718    * evenly spaced in between.  To do this, we basically
719    * use Bresenham's line draw (a.k.a DDA) algorithm
720    * to draw a line with slope (randpasses-1)/(num-1).
721    * (We use a positive accumulator and count down to
722    * do this.)
723    *
724    * So for each desired output value, we do the following:
725    * - If it should be a random pass, copy the pass type
726    *   to top++, out of the way of the other passes, and
727    *   set the current pass to -1 (random).
728    * - If it should be a normal pattern pass, choose an
729    *   entry at random between here and top-1 (inclusive)
730    *   and swap the current entry with that one.
731    */
732   randpasses--;                 /* To speed up later math */
733   accum = randpasses;           /* Bresenham DDA accumulator */
734   for (n = 0; n < num; n++)
735     {
736       if (accum <= randpasses)
737         {
738           accum += num - 1;
739           dest[top++] = dest[n];
740           dest[n] = -1;
741         }
742       else
743         {
744           swap = n + randint_choose (s, top - n);
745           k = dest[n];
746           dest[n] = dest[swap];
747           dest[swap] = k;
748         }
749       accum -= randpasses;
750     }
751   /* assert (top == num); */
752 }
753
754 /*
755  * The core routine to actually do the work.  This overwrites the first
756  * size bytes of the given fd.  Return true if successful.
757  */
758 static bool
759 do_wipefd (int fd, char const *qname, struct randint_source *s,
760            struct Options const *flags)
761 {
762   size_t i;
763   struct stat st;
764   off_t size;                   /* Size to write, size to read */
765   unsigned long int n;          /* Number of passes for printing purposes */
766   int *passarray;
767   bool ok = true;
768   struct randread_source *rs;
769
770   n = 0;                /* dopass takes n -- 0 to mean "don't print progress" */
771   if (flags->verbose)
772     n = flags->n_iterations + flags->zero_fill;
773
774   if (fstat (fd, &st))
775     {
776       error (0, errno, _("%s: fstat failed"), qname);
777       return false;
778     }
779
780   /* If we know that we can't possibly shred the file, give up now.
781      Otherwise, we may go into a infinite loop writing data before we
782      find that we can't rewind the device.  */
783   if ((S_ISCHR (st.st_mode) && isatty (fd))
784       || S_ISFIFO (st.st_mode)
785       || S_ISSOCK (st.st_mode))
786     {
787       error (0, 0, _("%s: invalid file type"), qname);
788       return false;
789     }
790
791   direct_mode (fd, true);
792
793   /* Allocate pass array */
794   passarray = xnmalloc (flags->n_iterations, sizeof *passarray);
795
796   size = flags->size;
797   if (size == -1)
798     {
799       /* Accept a length of zero only if it's a regular file.
800          For any other type of file, try to get the size another way.  */
801       if (S_ISREG (st.st_mode))
802         {
803           size = st.st_size;
804           if (size < 0)
805             {
806               error (0, 0, _("%s: file has negative size"), qname);
807               return false;
808             }
809         }
810       else
811         {
812           size = lseek (fd, 0, SEEK_END);
813           if (size <= 0)
814             {
815               /* We are unable to determine the length, up front.
816                  Let dopass do that as part of its first iteration.  */
817               size = -1;
818             }
819         }
820
821       /* Allow `rounding up' only for regular files.  */
822       if (0 <= size && !(flags->exact) && S_ISREG (st.st_mode))
823         {
824           size += ST_BLKSIZE (st) - 1 - (size - 1) % ST_BLKSIZE (st);
825
826           /* If in rounding up, we've just overflowed, use the maximum.  */
827           if (size < 0)
828             size = TYPE_MAXIMUM (off_t);
829         }
830     }
831
832   /* Schedule the passes in random order. */
833   genpattern (passarray, flags->n_iterations, s);
834
835   rs = randint_get_source (s);
836
837   /* Do the work */
838   for (i = 0; i < flags->n_iterations; i++)
839     {
840       int err = dopass (fd, qname, &size, passarray[i], rs, i + 1, n);
841       if (err)
842         {
843           if (err < 0)
844             {
845               memset (passarray, 0, flags->n_iterations * sizeof (int));
846               free (passarray);
847               return false;
848             }
849           ok = false;
850         }
851     }
852
853   memset (passarray, 0, flags->n_iterations * sizeof (int));
854   free (passarray);
855
856   if (flags->zero_fill)
857     {
858       int err = dopass (fd, qname, &size, 0, rs, flags->n_iterations + 1, n);
859       if (err)
860         {
861           if (err < 0)
862             return false;
863           ok = false;
864         }
865     }
866
867   /* Okay, now deallocate the data.  The effect of ftruncate on
868      non-regular files is unspecified, so don't worry about any
869      errors reported for them.  */
870   if (flags->remove_file && ftruncate (fd, 0) != 0
871       && S_ISREG (st.st_mode))
872     {
873       error (0, errno, _("%s: error truncating"), qname);
874       return false;
875     }
876
877   return ok;
878 }
879
880 /* A wrapper with a little more checking for fds on the command line */
881 static bool
882 wipefd (int fd, char const *qname, struct randint_source *s,
883         struct Options const *flags)
884 {
885   int fd_flags = fcntl (fd, F_GETFL);
886
887   if (fd_flags < 0)
888     {
889       error (0, errno, _("%s: fcntl failed"), qname);
890       return false;
891     }
892   if (fd_flags & O_APPEND)
893     {
894       error (0, 0, _("%s: cannot shred append-only file descriptor"), qname);
895       return false;
896     }
897   return do_wipefd (fd, qname, s, flags);
898 }
899
900 /* --- Name-wiping code --- */
901
902 /* Characters allowed in a file name - a safe universal set.  */
903 static char const nameset[] =
904 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.";
905
906 /* Increment NAME (with LEN bytes).  NAME must be a big-endian base N
907    number with the digits taken from nameset.  Return true if
908    successful if not (because NAME already has the greatest possible
909    value.  */
910
911 static bool
912 incname (char *name, size_t len)
913 {
914   while (len--)
915     {
916       char const *p = strchr (nameset, name[len]);
917
918       /* If this character has a successor, use it.  */
919       if (p[1])
920         {
921           name[len] = p[1];
922           return true;
923         }
924
925       /* Otherwise, set this digit to 0 and increment the prefix.  */
926       name[len] = nameset[0];
927     }
928
929   return false;
930 }
931
932 /*
933  * Repeatedly rename a file with shorter and shorter names,
934  * to obliterate all traces of the file name on any system that
935  * adds a trailing delimiter to on-disk file names and reuses
936  * the same directory slot.  Finally, unlink it.
937  * The passed-in filename is modified in place to the new filename.
938  * (Which is unlinked if this function succeeds, but is still present if
939  * it fails for some reason.)
940  *
941  * The main loop is written carefully to not get stuck if all possible
942  * names of a given length are occupied.  It counts down the length from
943  * the original to 0.  While the length is non-zero, it tries to find an
944  * unused file name of the given length.  It continues until either the
945  * name is available and the rename succeeds, or it runs out of names
946  * to try (incname wraps and returns 1).  Finally, it unlinks the file.
947  *
948  * The unlink is Unix-specific, as ANSI-standard remove has more
949  * portability problems with C libraries making it "safe".  rename
950  * is ANSI-standard.
951  *
952  * To force the directory data out, we try to open the directory and
953  * invoke fdatasync and/or fsync on it.  This is non-standard, so don't
954  * insist that it works: just fall back to a global sync in that case.
955  * This is fairly significantly Unix-specific.  Of course, on any
956  * file system with synchronous metadata updates, this is unnecessary.
957  */
958 static bool
959 wipename (char *oldname, char const *qoldname, struct Options const *flags)
960 {
961   char *newname = xstrdup (oldname);
962   char *base = last_component (newname);
963   size_t len = base_len (base);
964   char *dir = dir_name (newname);
965   char *qdir = xstrdup (quotearg_colon (dir));
966   bool first = true;
967   bool ok = true;
968
969   int dir_fd = open (dir, O_RDONLY | O_DIRECTORY | O_NOCTTY | O_NONBLOCK);
970
971   if (flags->verbose)
972     error (0, 0, _("%s: removing"), qoldname);
973
974   while (len)
975     {
976       memset (base, nameset[0], len);
977       base[len] = 0;
978       do
979         {
980           struct stat st;
981           if (lstat (newname, &st) < 0)
982             {
983               if (rename (oldname, newname) == 0)
984                 {
985                   if (0 <= dir_fd && dosync (dir_fd, qdir) != 0)
986                     ok = false;
987                   if (flags->verbose)
988                     {
989                       /*
990                        * People seem to understand this better than talking
991                        * about renaming oldname.  newname doesn't need
992                        * quoting because we picked it.  oldname needs to
993                        * be quoted only the first time.
994                        */
995                       char const *old = (first ? qoldname : oldname);
996                       error (0, 0, _("%s: renamed to %s"), old, newname);
997                       first = false;
998                     }
999                   memcpy (oldname + (base - newname), base, len + 1);
1000                   break;
1001                 }
1002               else
1003                 {
1004                   /* The rename failed: give up on this length.  */
1005                   break;
1006                 }
1007             }
1008           else
1009             {
1010               /* newname exists, so increment BASE so we use another */
1011             }
1012         }
1013       while (incname (base, len));
1014       len--;
1015     }
1016   if (unlink (oldname) != 0)
1017     {
1018       error (0, errno, _("%s: failed to remove"), qoldname);
1019       ok = false;
1020     }
1021   else if (flags->verbose)
1022     error (0, 0, _("%s: removed"), qoldname);
1023   if (0 <= dir_fd)
1024     {
1025       if (dosync (dir_fd, qdir) != 0)
1026         ok = false;
1027       if (close (dir_fd) != 0)
1028         {
1029           error (0, errno, _("%s: failed to close"), qdir);
1030           ok = false;
1031         }
1032     }
1033   free (newname);
1034   free (dir);
1035   free (qdir);
1036   return ok;
1037 }
1038
1039 /*
1040  * Finally, the function that actually takes a filename and grinds
1041  * it into hamburger.
1042  *
1043  * FIXME
1044  * Detail to note: since we do not restore errno to EACCES after
1045  * a failed chmod, we end up printing the error code from the chmod.
1046  * This is actually the error that stopped us from proceeding, so
1047  * it's arguably the right one, and in practice it'll be either EACCES
1048  * again or EPERM, which both give similar error messages.
1049  * Does anyone disagree?
1050  */
1051 static bool
1052 wipefile (char *name, char const *qname,
1053           struct randint_source *s, struct Options const *flags)
1054 {
1055   bool ok;
1056   int fd;
1057
1058   fd = open (name, O_WRONLY | O_NOCTTY | O_BINARY);
1059   if (fd < 0
1060       && (errno == EACCES && flags->force)
1061       && chmod (name, S_IWUSR) == 0)
1062     fd = open (name, O_WRONLY | O_NOCTTY | O_BINARY);
1063   if (fd < 0)
1064     {
1065       error (0, errno, _("%s: failed to open for writing"), qname);
1066       return false;
1067     }
1068
1069   ok = do_wipefd (fd, qname, s, flags);
1070   if (close (fd) != 0)
1071     {
1072       error (0, errno, _("%s: failed to close"), qname);
1073       ok = false;
1074     }
1075   if (ok && flags->remove_file)
1076     ok = wipename (name, qname, flags);
1077   return ok;
1078 }
1079
1080
1081 /* Buffers for random data.  */
1082 static struct randint_source *randint_source;
1083
1084 /* Just on general principles, wipe buffers containing information
1085    that may be related to the possibly-pseudorandom values used during
1086    shredding.  */
1087 static void
1088 clear_random_data (void)
1089 {
1090   randint_all_free (randint_source);
1091 }
1092
1093
1094 int
1095 main (int argc, char **argv)
1096 {
1097   bool ok = true;
1098   struct Options flags = { 0, };
1099   char **file;
1100   int n_files;
1101   int c;
1102   int i;
1103   char const *random_source = NULL;
1104
1105   initialize_main (&argc, &argv);
1106   program_name = argv[0];
1107   setlocale (LC_ALL, "");
1108   bindtextdomain (PACKAGE, LOCALEDIR);
1109   textdomain (PACKAGE);
1110
1111   atexit (close_stdout);
1112
1113   flags.n_iterations = DEFAULT_PASSES;
1114   flags.size = -1;
1115
1116   while ((c = getopt_long (argc, argv, "fn:s:uvxz", long_opts, NULL)) != -1)
1117     {
1118       switch (c)
1119         {
1120         case 'f':
1121           flags.force = true;
1122           break;
1123
1124         case 'n':
1125           {
1126             uintmax_t tmp;
1127             if (xstrtoumax (optarg, NULL, 10, &tmp, NULL) != LONGINT_OK
1128                 || MIN (UINT32_MAX, SIZE_MAX / sizeof (int)) < tmp)
1129               {
1130                 error (EXIT_FAILURE, 0, _("%s: invalid number of passes"),
1131                        quotearg_colon (optarg));
1132               }
1133             flags.n_iterations = tmp;
1134           }
1135           break;
1136
1137         case RANDOM_SOURCE_OPTION:
1138           if (random_source && !STREQ (random_source, optarg))
1139             error (EXIT_FAILURE, 0, _("multiple random sources specified"));
1140           random_source = optarg;
1141           break;
1142
1143         case 'u':
1144           flags.remove_file = true;
1145           break;
1146
1147         case 's':
1148           {
1149             uintmax_t tmp;
1150             if (xstrtoumax (optarg, NULL, 0, &tmp, "cbBkKMGTPEZY0")
1151                 != LONGINT_OK)
1152               {
1153                 error (EXIT_FAILURE, 0, _("%s: invalid file size"),
1154                        quotearg_colon (optarg));
1155               }
1156             flags.size = tmp;
1157           }
1158           break;
1159
1160         case 'v':
1161           flags.verbose = true;
1162           break;
1163
1164         case 'x':
1165           flags.exact = true;
1166           break;
1167
1168         case 'z':
1169           flags.zero_fill = true;
1170           break;
1171
1172         case_GETOPT_HELP_CHAR;
1173
1174         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1175
1176         default:
1177           usage (EXIT_FAILURE);
1178         }
1179     }
1180
1181   file = argv + optind;
1182   n_files = argc - optind;
1183
1184   if (n_files == 0)
1185     {
1186       error (0, 0, _("missing file operand"));
1187       usage (EXIT_FAILURE);
1188     }
1189
1190   randint_source = randint_all_new (random_source, SIZE_MAX);
1191   if (! randint_source)
1192     error (EXIT_FAILURE, errno, "%s", quotearg_colon (random_source));
1193   atexit (clear_random_data);
1194
1195   for (i = 0; i < n_files; i++)
1196     {
1197       char *qname = xstrdup (quotearg_colon (file[i]));
1198       if (STREQ (file[i], "-"))
1199         {
1200           ok &= wipefd (STDOUT_FILENO, qname, randint_source, &flags);
1201         }
1202       else
1203         {
1204           /* Plain filename - Note that this overwrites *argv! */
1205           ok &= wipefile (file[i], qname, randint_source, &flags);
1206         }
1207       free (qname);
1208     }
1209
1210   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
1211 }
1212 /*
1213  * vim:sw=2:sts=2:
1214  */