#define _GNU_SOURCE #include "encryption.h" #include "shared.h" #include #include #include #include #include #include #include #include #include int checkSodium(void) { int ret = sodium_init(); if(ret < 0) error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); return ret; } // To encrypt: // 1- Create a temp file with the correct name in the root folder of the partition being encrypted // 1.1- Detect the partition and find the root folder // 1.2- Create the temp file with the correct name // 2- Encrypt the file's contents to the temp file // 2.1- Open the file // 2.2- Stream the file's contents into some encryption algo // 2.3- Pipe the output of the encryption into the temp file // 3- Once the file has been encrypted, hard link it back to the original location // 4- Delete the original file // 5- Delete the temp file int maketmp(const char *dest, const char *format, ...) { char *filename = NULL, *fullpath = NULL; struct stat fb; int fd = -1; va_list ap; // Make sure the destination exists and is a directory if(stat(dest, &fb) < 0) return -1; if(!S_ISDIR(fb.st_mode)) return -2; // Get the first half of the filename va_start(ap, format); if(vasprintf(&filename, format, ap) < 0) return -3; va_end(ap); // Get the second half of the filename int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)` free(filename); if(fps < 0) return -4; // Open the temp file if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0) fd = -5; free(fullpath); return fd; } int main(int argc, char *argv[]) { if(argc != 3) error(1, 0, "USAGE: "); int fd = maketmp(argv[1], "%s.test", argv[2]); if(fd < 0) error(1, errno, "Couldn't open temp file"); return 0; }