File: options.c

package info (click to toggle)
nwipe 0.26-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 372 kB
  • sloc: ansic: 3,838; makefile: 23; sh: 4
file content (464 lines) | stat: -rw-r--r-- 13,436 bytes parent folder | download
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
/*
 *  options.c:  Command line processing routines for nwipe. 
 *
 *  Copyright Darik Horn <dajhorn-dban@vanadac.com>.
 *  
 *  Modifications to original dwipe Copyright Andy Beverley <andy@andybev.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, version 2.
 *
 *  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 "nwipe.h"
#include "context.h"
#include "method.h"
#include "prng.h"
#include "options.h"
#include "logging.h"
#include "version.h"

/* The global options struct. */
nwipe_options_t nwipe_options;

int nwipe_options_parse( int argc, char** argv )
{
	extern char* optarg;  /* The working getopt option argument. */
	extern int   optind;  /* The working getopt index into argv. */
	extern int   optopt;  /* The last unhandled getopt option.   */
	extern int   opterr;  /* The last getopt error number.       */

	extern nwipe_prng_t nwipe_twister;
	extern nwipe_prng_t nwipe_isaac;

	/* The getopt() result holder. */
	int nwipe_opt;

	/* Excluded drive indexes */
	int idx_drive_chr;
	int idx_optarg;
	int idx_drive;

	/* Array index variable. */
	int i;

	/* The list of acceptable short options. */
	char nwipe_options_short [] = "Vhl:hm:p:r:e:";

	/* The list of acceptable long options. */
	static struct option nwipe_options_long [] =
	{
		/* Set when the user wants to wipe without a confirmation prompt. */
		{ "autonuke", no_argument, 0, 0 },

		/* A GNU standard option. Corresponds to the 'h' short option. */
		{ "help", no_argument, 0, 'h' },

		/* The wipe method. Corresponds to the 'm' short option. */
		{ "method", required_argument, 0, 'm' },

		/* Log file. Corresponds to the 'l' short option. */
		{ "logfile", required_argument, 0, 'l' },
	
		/* Exclude devices, comma separated list */
		{ "exclude", required_argument, 0, 'e' },

		/* The Pseudo Random Number Generator. */
		{ "prng", required_argument, 0, 'p' },

		/* The number of times to run the method. */
		{ "rounds", required_argument, 0, 'r' },

		/* Whether to blank the disk after wiping. */
		{ "noblank", no_argument, 0, 0 },

		/* Whether to exit after wiping or wait for a keypress. */
		{ "nowait", no_argument, 0, 0 },

		/* Whether to allow signals to interrupt a wipe. */
		{ "nosignals", no_argument, 0, 0 },

		/* Whether to exit after wiping or wait for a keypress. */
		{ "nogui", no_argument, 0, 0 },

		/* A flag to indicate whether the devices whould be opened in sync mode. */
		{ "sync", no_argument, 0, 0 },

		/* Verify that wipe patterns are being written to the device. */
		{ "verify", required_argument, 0, 0 },

		/* Display program version. */
		{ "version", no_argument, 0, 'V' },

		/* Requisite padding for getopt(). */
		{ 0, 0, 0, 0 }
	};


	/* Set default options. */
	nwipe_options.autonuke = 0;
	nwipe_options.method   = &nwipe_dodshort;
	nwipe_options.prng     = &nwipe_twister;
	nwipe_options.rounds   = 1;
	nwipe_options.noblank  = 0;
	nwipe_options.nowait   = 0;
	nwipe_options.nosignals= 0;
	nwipe_options.nogui    = 0;
	nwipe_options.sync     = 0;
	nwipe_options.verify   = NWIPE_VERIFY_LAST;
	memset( nwipe_options.logfile, '\0', sizeof(nwipe_options.logfile) );

	/* Initialise each of the strings in the excluded drives array */
	for ( i=0; i < MAX_NUMBER_EXCLUDED_DRIVES; i++ )
	{
		nwipe_options.exclude[i][0]=0;
	}

	/* Parse command line options. */
	while( 1 )
	{
		/* Get the next command line option with (3)getopt. */
		nwipe_opt = getopt_long( argc, argv, nwipe_options_short, nwipe_options_long, &i );

		/* Break when we have processed all of the given options. */
		if( nwipe_opt < 0 ) { break; }

		switch( nwipe_opt )
		{
			case 0: /* Long options without short counterparts. */

				if( strcmp( nwipe_options_long[i].name, "autonuke" ) == 0 )
				{
					nwipe_options.autonuke = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "noblank" ) == 0 )
				{
					nwipe_options.noblank = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "nowait" ) == 0 )
				{
					nwipe_options.nowait = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "nosignals" ) == 0 )
				{
					nwipe_options.nosignals = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "nogui" ) == 0 )
				{
					nwipe_options.nogui = 1;
					nwipe_options.nowait = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "sync" ) == 0 )
				{
					nwipe_options.sync = 1;
					break;
				}

				if( strcmp( nwipe_options_long[i].name, "verify" ) == 0 )
				{

					if( strcmp( optarg, "0" ) == 0 || strcmp( optarg, "off" ) == 0 )
					{
						nwipe_options.verify = NWIPE_VERIFY_NONE;
						break;
					}

					if( strcmp( optarg, "1" ) == 0 || strcmp( optarg, "last" ) == 0 )
					{
						nwipe_options.verify = NWIPE_VERIFY_LAST;
						break;
					}

					if( strcmp( optarg, "2" ) == 0 || strcmp( optarg, "all" ) == 0 )
					{
						nwipe_options.verify = NWIPE_VERIFY_ALL;
						break;
					}

					/* Else we do not know this verification level. */
					fprintf( stderr, "Error: Unknown verification level '%s'.\n", optarg );
					exit( EINVAL );

				}


			case 'm':  /* Method option. */

				if( strcmp( optarg, "dod522022m" ) == 0 || strcmp( optarg, "dod" ) == 0 )
				{
					nwipe_options.method = &nwipe_dod522022m;
					break;
				}

				if( strcmp( optarg, "dodshort" ) == 0 || strcmp( optarg, "dod3pass" ) == 0 )
				{
					nwipe_options.method = &nwipe_dodshort;
					break;
				}

				if( strcmp( optarg, "gutmann" ) == 0 )
				{
					nwipe_options.method = &nwipe_gutmann;
					break;
				}

				if( strcmp( optarg, "ops2" ) == 0 )
				{
					nwipe_options.method = &nwipe_ops2;
					break;
				}

				if(  strcmp( optarg, "random" ) == 0
				  || strcmp( optarg, "prng"   ) == 0
				  || strcmp( optarg, "stream" ) == 0
				  )
				{
					nwipe_options.method= &nwipe_random;
					break;
				}

				if( strcmp( optarg, "zero" ) == 0 || strcmp( optarg, "quick" ) == 0 )
				{
					nwipe_options.method = &nwipe_zero;
					break;
				}

				/* Else we do not know this wipe method. */
				fprintf( stderr, "Error: Unknown wipe method '%s'.\n", optarg );
				exit( EINVAL );


			case 'l':  /* Log file option. */
				
				nwipe_options.logfile[strlen(optarg)] = '\0';
				strncpy(nwipe_options.logfile, optarg, sizeof(nwipe_options.logfile));
				break;

			case 'e':  /* exclude drives option */
				
				idx_drive_chr=0;
				idx_optarg=0;
				idx_drive=0;
				
				/* Create an array of excluded drives from the comma separated string */
				while ( optarg[idx_optarg] != 0 && idx_drive<MAX_NUMBER_EXCLUDED_DRIVES )
				{
					/* drop the leading '=' character if used */
					if ( optarg[idx_optarg] == '=' && idx_optarg == 0 )
					{	idx_optarg++;
						continue;
					}
						
					if ( optarg[idx_optarg] == ',' )
					{	
						/* terminate string and move onto next drive */
						nwipe_options.exclude[idx_drive++][idx_drive_chr]=0;
						idx_drive_chr=0;
						idx_optarg++;
					}
					else
					{
						if ( idx_drive_chr < MAX_DRIVE_PATH_LENGTH )
						{
							nwipe_options.exclude[idx_drive][idx_drive_chr++]=optarg[idx_optarg++];
						}
						else
						{	/* This section deals with file names that exceed MAX_DRIVE_PATH_LENGTH */
							nwipe_options.exclude[idx_drive][idx_drive_chr]=0;
							while ( optarg[idx_optarg] != 0 || optarg[idx_optarg] != ',' )
							{
								idx_optarg++;
							}
						}
					}
				}
				break;

			case 'h':  /* Display help. */

				display_help();
				break;

			case 'p':  /* PRNG option. */

				if(  strcmp( optarg, "mersenne" ) == 0
				  || strcmp( optarg, "twister"  ) == 0
				  )
				{
					nwipe_options.prng = &nwipe_twister;
					break;
				}

				if( strcmp( optarg, "isaac" ) == 0 )
				{
					nwipe_options.prng = &nwipe_isaac;
					break;
				}

				/* Else we do not know this PRNG. */
				fprintf( stderr, "Error: Unknown prng '%s'.\n", optarg );
				exit( EINVAL );


			case 'r':  /* Rounds option. */

				if( sscanf( optarg, " %i", &nwipe_options.rounds ) != 1 \
				    || nwipe_options.rounds < 1
				  )
				{
					fprintf( stderr, "Error: The rounds argument must be a positive integer.\n" );
					exit( EINVAL );
				}

				break;

			case 'V':  /* Rounds option. */

				printf ( "%s version %s\n", program_name, version_string );
				exit( EXIT_SUCCESS );

			default:

				/* Bogus command line argument. */
				display_help();
				exit( EINVAL );
			
		} /* method */

	} /* command line options */

	/* Return the number of options that were processed. */
	return optind;

} /* nwipe_options_parse */


void nwipe_options_log( void )
{
/**
 *  Prints a manifest of options to the log.
 *
 */

	nwipe_log( NWIPE_LOG_NOTICE, "Program options are set as follows..." );

	if( nwipe_options.autonuke )
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  autonuke = %i (on)", nwipe_options.autonuke );
	}

	else
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  autonuke = %i (off)", nwipe_options.autonuke );
	}

	if( nwipe_options.noblank )
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  do not perform a final blank pass" );
	}

	if( nwipe_options.nowait )
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  do not wait for a key before exiting" );
	}

	if( nwipe_options.nosignals )
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  do not allow signals to interrupt a wipe" );
	}

	if( nwipe_options.nogui )
	{
		nwipe_log( NWIPE_LOG_NOTICE, "  do not show GUI interface" );
	}

	nwipe_log( NWIPE_LOG_NOTICE, "  banner   = %s", banner );
	nwipe_log( NWIPE_LOG_NOTICE, "  method   = %s", nwipe_method_label( nwipe_options.method ) );
	nwipe_log( NWIPE_LOG_NOTICE, "  rounds   = %i", nwipe_options.rounds );
	nwipe_log( NWIPE_LOG_NOTICE, "  sync     = %i", nwipe_options.sync );

	switch( nwipe_options.verify )
	{
		case NWIPE_VERIFY_NONE:
			nwipe_log( NWIPE_LOG_NOTICE, "  verify   = %i (off)", nwipe_options.verify );
			break;

		case NWIPE_VERIFY_LAST:
			nwipe_log( NWIPE_LOG_NOTICE, "  verify   = %i (last pass)", nwipe_options.verify );
			break;

		case NWIPE_VERIFY_ALL:
			nwipe_log( NWIPE_LOG_NOTICE, "  verify   = %i (all passes)", nwipe_options.verify );
			break;

		default:
			nwipe_log( NWIPE_LOG_NOTICE, "  verify   = %i", nwipe_options.verify );
			break;
	}

} /* nwipe_options_log */

/**
 * display_help 
 * displays the help section to STDOUT and exits
 */  
void 
display_help()
{
  printf("Usage: %s [options] [device1] [device2] ...\n", program_name);
  printf("Options:\n"                    );
  puts("  -V, --version           Prints the version number");
  puts("  -h, --help              Prints this help");
  puts("      --autonuke          If no devices have been specified on the command line, starts wiping all");
  puts("                          devices immediately. If devices have been specified, starts wiping only");
  puts("                          those specified devices immediately.");
  puts("      --sync              Open devices in sync mode");
  puts("      --verify=TYPE       Whether to perform verification of erasure (default: last)");
  puts("                          off   - Do not verify");
  puts("                          last  - Verify after the last pass");
  puts("                          all   - Verify every pass");  
  puts("  -m, --method=METHOD     The wiping method (default: dodshort). See man page for more details.");
  puts("                          dod522022m / dod       - 7 pass DOD 5220.22-M method");
  puts("                          dodshort / dod3pass    - 3 pass DOD method");
  puts("                          gutmann                - Peter Gutmann's Algorithm");
  puts("                          ops2                   - RCMP TSSIT OPS-II");
  puts("                          random / prng / stream - PRNG Stream");
  puts("                          zero / quick           - Overwrite with zeros");
  puts("  -l, --logfile=FILE      Filename to log to. Default is STDOUT");
  puts("  -p, --prng=METHOD       PRNG option (mersenne|twister|isaac)" );
  puts("  -r, --rounds=NUM        Number of times to wipe the device using the selected method (default: 1)" );
  puts("      --noblank           Do not blank disk after wipe (default is to complete a final blank pass)" );
  puts("      --nowait            Do not wait for a key before exiting (default is to wait)" );
  puts("      --nosignals         Do not allow signals to interrupt a wipe (default is to allow)" );
  puts("      --nogui             Do not show the GUI interface. Automatically invokes the nowait option" );
  puts("                          Must be used with --autonuke option. Send SIGUSR1 to log current stats");
  puts("  -e, --exclude=DEVICES   Up to ten comma separted devices to be excluded, examples:");
  puts("                          --exclude=/dev/sdc");
  puts("                          --exclude=/dev/sdc,/dev/sdd");
  puts("                          --exclude=/dev/sdc,/dev/sdd,/dev/mapper/cryptswap1");
  puts("");
  exit( EXIT_SUCCESS );
}

/* eof */