-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecure_delete.c
212 lines (184 loc) · 4.57 KB
/
secure_delete.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#define FILE_TYPE 1
#define DIRECTORY_TYPE 2
#define PATH_MAX 4096
static void take_action(char *filepath);
static int overwrite_file(char *filepath);
static int delete_file(char *filepath);
static int delete_directory(char *filepath);
static int get_file_type(char *filepath);
static void handle_error(const char *message, const char *filepath);
int main(int argc, char **argv)
{
if (argc != 2)
{
handle_error("This command only accepts one arguments.", NULL);
exit(1);
}
take_action(argv[1]);
exit(0);
}
static void handle_error(const char *message, const char *filepath)
{
if (filepath)
{
fprintf(stderr, "%s: %s\n", message, filepath);
}
else
{
fprintf(stderr, "%s\n", message);
}
exit(1);
}
static void take_action(char *filepath)
{
switch (get_file_type(filepath))
{
case FILE_TYPE:
delete_file(filepath);
break;
case DIRECTORY_TYPE:
delete_directory(filepath);
break;
default:
handle_error("Invalid file type", filepath);
exit(1);
}
}
// Returns 0 on success
static int overwrite_file(char *filepath)
{
FILE *file;
long file_size;
char *overwrite_value;
// Check if file exists
if (!(file = fopen(filepath, "rb+")))
{
handle_error("File does not exist", filepath);
return 1;
}
// Get size of file
fseek(file, 0, SEEK_END);
if ((file_size = ftell(file)) == -1L)
{
handle_error("Error determining file size", filepath);
fclose(file);
return 1;
}
else if (!file_size)
{
handle_error("Invalid file size", filepath);
fclose(file);
return 1;
}
fseek(file, 0, SEEK_SET);
// Replace file contents
if (!(overwrite_value = malloc(file_size)))
{
handle_error("Failed to allocate memory for overwrite", filepath);
free(overwrite_value);
fclose(file);
return 1;
}
// Pass 1: Overwrite with zeros
memset(overwrite_value, 0, file_size);
if (!fwrite(overwrite_value, file_size, 1, file))
{
handle_error("Error overwriting file", filepath);
free(overwrite_value);
fclose(file);
return 1;
}
// Pass 2: Overwrite with binary ones
memset(overwrite_value, 0xFF, file_size);
if (!fwrite(overwrite_value, file_size, 1, file))
{
handle_error("Error overwriting file", filepath);
free(overwrite_value);
fclose(file);
return 1;
}
// Pass 3: Overwrite with random data
for (long i = 0; i < file_size; i++)
{
overwrite_value[i] = (char)rand();
}
if (!fwrite(overwrite_value, file_size, 1, file))
{
handle_error("Error overwriting file", filepath);
free(overwrite_value);
fclose(file);
return 1;
}
// Clear resources
free(overwrite_value);
fclose(file);
return 0;
}
static int delete_file(char *filepath)
{
if (overwrite_file(filepath))
{
handle_error("Terminating program, unable to overwrite", filepath);
exit(1);
}
if (remove(filepath))
{
handle_error("Error deleting file", filepath);
return 1;
}
printf("File deleted successfully: %s\n", filepath);
return 0;
}
static int delete_directory(char *filepath)
{
DIR *directory;
struct dirent *directory_obj;
char fullpath[PATH_MAX];
// Check if directory exists
if (!(directory = opendir(filepath)))
{
handle_error("Invalid directory", filepath);
return 1;
}
while ((directory_obj = readdir(directory)))
{
if (strcmp(directory_obj->d_name, ".") == 0 || strcmp(directory_obj->d_name, "..") == 0)
{
continue;
}
snprintf(fullpath, sizeof(fullpath), "%s/%s", filepath, directory_obj->d_name);
take_action(fullpath);
}
closedir(directory);
// Remove directory now
if (rmdir(filepath))
{
handle_error("Error deleting directory", filepath);
return 1;
}
fprintf(stdout, "Directory deleted successfully: %s\n", filepath);
return 0;
}
static int get_file_type(char *filepath)
{
struct stat file_info;
if (!stat(filepath, &file_info))
{
if (S_ISREG(file_info.st_mode))
{
return FILE_TYPE;
}
else if (S_ISDIR(file_info.st_mode))
{
return DIRECTORY_TYPE;
}
}
return -1;
}