-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfirmation.rs
More file actions
342 lines (294 loc) · 9.22 KB
/
Copy pathconfirmation.rs
File metadata and controls
342 lines (294 loc) · 9.22 KB
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
// SPDX-License-Identifier: MPL-2.0
//! Confirmation prompts for destructive operations
//!
//! SAFETY CRITICAL: Prevents accidental data destruction
use anyhow::Result;
use colored::Colorize;
use std::io::{self, Write};
/// Confirmation level for operations
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfirmationLevel {
/// File-level deletion (single file)
File,
/// Directory tree deletion (multiple files)
Tree,
/// Device-level erase (ENTIRE DEVICE)
Device,
}
/// Confirm destructive operation with typed challenge
pub fn confirm_destructive_operation(
level: ConfirmationLevel,
target: &str,
method: &str,
) -> Result<bool> {
match level {
ConfirmationLevel::File => confirm_file_deletion(target, method),
ConfirmationLevel::Tree => confirm_tree_deletion(target, method),
ConfirmationLevel::Device => confirm_device_erase(target, method),
}
}
fn confirm_file_deletion(path: &str, method: &str) -> Result<bool> {
println!();
println!("{}", "⚠️ SECURE FILE DELETION".yellow().bold());
println!();
println!(" File: {}", path.bright_white());
println!(" Method: {} (NIST SP 800-88)", method.bright_cyan());
println!();
println!(
"{}",
" This operation is IRREVERSIBLE!".bright_red().bold()
);
println!(" The file cannot be recovered after deletion.");
println!();
let confirmation = prompt_yes_no("Proceed with secure deletion?")?;
println!();
Ok(confirmation)
}
fn confirm_tree_deletion(path: &str, method: &str) -> Result<bool> {
// Count files in tree
let file_count = count_files_in_tree(path)?;
println!();
println!("{}", "⚠️ SECURE TREE DELETION".yellow().bold());
println!();
println!(" Directory: {}", path.bright_white());
println!(
" Files: {} files will be destroyed",
file_count.to_string().bright_red().bold()
);
println!(" Method: {} (NIST SP 800-88)", method.bright_cyan());
println!();
println!(
"{}",
" THIS OPERATION IS IRREVERSIBLE!".bright_red().bold()
);
println!(" All {} files will be permanently destroyed.", file_count);
println!();
let confirmation = prompt_yes_no("Proceed with secure tree deletion?")?;
println!();
Ok(confirmation)
}
fn confirm_device_erase(device: &str, method: &str) -> Result<bool> {
// Get device info
let device_info = get_device_info(device)?;
println!();
println!(
"{}",
"🚨 CRITICAL WARNING - DEVICE-LEVEL SECURE ERASE 🚨"
.bright_red()
.bold()
);
println!();
println!("{}", "═".repeat(60).bright_red());
println!();
println!(
" {}",
"THIS WILL ERASE THE ENTIRE DEVICE!".bright_red().bold()
);
println!(
" {}",
"ALL DATA ON THE DEVICE WILL BE PERMANENTLY DESTROYED!"
.bright_red()
.bold()
);
println!();
println!("{}", "═".repeat(60).bright_red());
println!();
println!(" Device: {}", device.bright_white().bold());
println!(" Type: {}", device_info.drive_type.bright_yellow());
println!(" Size: {}", device_info.size.bright_yellow());
println!(
" Method: {} (NIST SP 800-88 Purge)",
method.bright_cyan()
);
println!();
println!(" Mounted partitions:");
for mount in &device_info.mounts {
println!(
" {} → {}",
mount.partition.bright_red(),
mount.mount_point.bright_white()
);
}
println!();
println!("{}", " SAFETY CHECKS:".bright_yellow().bold());
println!(
" {} System drive check",
if device_info.is_system_drive {
"❌ SYSTEM DRIVE DETECTED!".bright_red().bold()
} else {
"✓ Not system drive".green()
}
);
println!(
" {} Mount check",
if device_info.mounts.is_empty() {
"✓ Device unmounted".green()
} else {
"⚠️ Device has mounted partitions!".bright_red().bold()
}
);
println!();
if device_info.is_system_drive {
println!(
"{}",
"❌ ABORTED: Cannot erase system drive!".bright_red().bold()
);
println!();
return Ok(false);
}
if !device_info.mounts.is_empty() {
println!(
"{}",
"⚠️ WARNING: Device has mounted partitions!"
.bright_yellow()
.bold()
);
println!(" You must unmount all partitions before secure erase.");
println!();
let force = prompt_yes_no("Attempt to unmount automatically?")?;
if !force {
return Ok(false);
}
}
println!("{}", "═".repeat(60).bright_red());
println!();
println!(" {}", "FINAL CONFIRMATION REQUIRED".bright_red().bold());
println!();
print!(" Type the device name exactly to confirm: ");
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim();
if input != device {
println!();
println!(
"{}",
"❌ Device name mismatch - operation CANCELLED"
.bright_red()
.bold()
);
println!();
return Ok(false);
}
println!();
println!("{}", "⚠️ LAST CHANCE TO ABORT!".bright_red().bold());
let final_confirm = prompt_yes_no("PERMANENTLY ERASE ALL DATA?")?;
println!();
Ok(final_confirm)
}
fn prompt_yes_no(prompt: &str) -> Result<bool> {
print!(" {} [y/N]: ", prompt);
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
let input = input.trim().to_lowercase();
Ok(input == "y" || input == "yes")
}
fn count_files_in_tree(path: &str) -> Result<usize> {
let mut count = 0;
fn visit_dirs(path: &std::path::Path, count: &mut usize) -> Result<()> {
if path.is_dir() {
for entry in std::fs::read_dir(path)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
visit_dirs(&path, count)?;
} else {
*count += 1;
}
}
} else {
*count += 1;
}
Ok(())
}
visit_dirs(std::path::Path::new(path), &mut count)?;
Ok(count)
}
struct DeviceInfo {
drive_type: String,
size: String,
mounts: Vec<MountInfo>,
is_system_drive: bool,
}
struct MountInfo {
partition: String,
mount_point: String,
}
fn get_device_info(device: &str) -> Result<DeviceInfo> {
// Get device size
let size = get_device_size(device)?;
// Get drive type
let drive_type = detect_drive_type_string(device)?;
// Get mounted partitions
let mounts = get_mounted_partitions(device)?;
// Check if system drive
let is_system_drive = is_system_device(device)?;
Ok(DeviceInfo {
drive_type,
size,
mounts,
is_system_drive,
})
}
fn get_device_size(device: &str) -> Result<String> {
let output = std::process::Command::new("lsblk")
.arg("-dno")
.arg("SIZE")
.arg(device)
.output()?;
Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
fn detect_drive_type_string(device: &str) -> Result<String> {
use crate::secure_erase::detect_drive_type;
let drive_type = detect_drive_type(device)?;
Ok(match drive_type {
crate::secure_erase::DriveType::HDD => "Hard Disk Drive (magnetic)".to_string(),
crate::secure_erase::DriveType::SataSSD => "SATA Solid State Drive".to_string(),
crate::secure_erase::DriveType::NVMeSSD => "NVMe Solid State Drive".to_string(),
crate::secure_erase::DriveType::Unknown => "Unknown".to_string(),
})
}
fn get_mounted_partitions(device: &str) -> Result<Vec<MountInfo>> {
let output = std::process::Command::new("lsblk")
.arg("-no")
.arg("NAME,MOUNTPOINT")
.arg(device)
.output()?;
let mut mounts = Vec::new();
let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines().skip(1) {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() >= 2 && !parts[1].is_empty() {
mounts.push(MountInfo {
partition: format!("/dev/{}", parts[0]),
mount_point: parts[1].to_string(),
});
}
}
Ok(mounts)
}
fn is_system_device(device: &str) -> Result<bool> {
// Check if root filesystem is on this device
let output = std::process::Command::new("df").arg("/").output()?;
let stdout = String::from_utf8_lossy(&output.stdout);
// Extract device from df output
for line in stdout.lines().skip(1) {
if let Some(root_device) = line.split_whitespace().next() {
// Check if root device starts with our device
// e.g., /dev/sda1 starts with /dev/sda
if root_device.starts_with(device) {
return Ok(true);
}
}
}
Ok(false)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count_files() {
let _ = count_files_in_tree("/tmp").unwrap_or(0);
}
}