From fa458059e57801dceb63a4ce0ddaef8ad5a49eb2 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Thu, 9 Jan 2025 19:02:29 -0600 Subject: Figure out why linking wasn't working --- src/encryption.c | 63 ++++++++++++++++++++++++++++++++++++-------------------- src/encryption.h | 3 +++ 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/encryption.c b/src/encryption.c index 6da9603..828bde2 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -23,43 +23,60 @@ int checkSodium(void) { } // 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 +// 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 -- DONE || NOT NECESSARY + // 1.2- Create the temp file -- DONE +// 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, with the right name -- +// 4- Delete the original file -- +// 5- Delete the temp file -- int maketmp(const char *dest) { - int fd = open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); - if(fd < 0) - error(1, errno, "Couldn't open temp file at %s", dest); + return open(dest, (O_TMPFILE | O_WRONLY | O_CLOEXEC | O_SYNC), (S_IRUSR | S_IWUSR)); +} + +int encrypttotmp(const char *toencrypt) { + struct stat esb; + int efd = -1; + + // Make sure the file is real and an actual file that can be encrypted + if(stat(toencrypt, &esb) < 0) + return -1; + if(!S_ISREG(esb.st_mode)) + return -2; - return fd; + // Open the file as read-only + if((efd = open(toencrypt, O_RDONLY)) < 0) + return -3; + + // Need to get a secret key from a password and then set up cryptostream from libsodium + + return 0; } +#define TESTING +#ifdef TESTING #include int main(void) { - const char *testmsg = "we do a little testing\n"; + const char *dir = ".", *testmsg = "we do a little testing\n"; + char *path = NULL; - int fd = maketmp("."); + int fd = maketmp(dir); + if(fd < 0) + error(1, errno, "Couldn't make temp file at %s", dir); if(write(fd, testmsg, strlen(testmsg)) < 0) error(1, errno, "write broke"); - - // if(linkat(fd, "", AT_FDCWD, "kys/now", AT_EMPTY_PATH) < 0) - // error(1, errno, "linkat broke"); - - char *path = NULL; + asprintf(&path, "/proc/self/fd/%d", fd); linkat(AT_FDCWD, path, AT_FDCWD, "./test", AT_SYMLINK_FOLLOW); + free(path); // Apparently, I don't have the CAP_DAC_READ_SEARCH capibility. Thanks for the solution, linux man pages @@ -67,4 +84,6 @@ int main(void) { error(1, errno, "close broke"); return 0; -} \ No newline at end of file +} + +#endif \ No newline at end of file diff --git a/src/encryption.h b/src/encryption.h index 945f73d..b609d52 100644 --- a/src/encryption.h +++ b/src/encryption.h @@ -4,4 +4,7 @@ // Checks if sodium is initialized. Initializes it if not int checkSodium(void); +// open() with the flags O_TMPFILE, O_WRONLY, O_CLOEXEC, and O_SYNC. Opened with mode S_IRUSR, S_IWUSR +int maketmp(const char *dest); + #endif \ No newline at end of file -- cgit v1.2.3