diff options
| author | @syxhe <https://t.me/syxhe> | 2025-01-07 17:27:53 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-01-07 17:27:53 -0600 |
| commit | 2be40dd8d9930ad66b26cfaa42d1176dd55b42e8 (patch) | |
| tree | e5da6aba4be3df8ef45bb69fcbc885be979d3f23 /src/encryption.c | |
| parent | 5a5e604eac5cf7473c2d129b38b517dc1968c441 (diff) | |
For posterity
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 50 |
1 files changed, 39 insertions, 11 deletions
diff --git a/src/encryption.c b/src/encryption.c index f7fdcd4..c7928d8 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -1,11 +1,17 @@ | |||
| 1 | #define _GNU_SOURCE | ||
| 2 | |||
| 1 | #include "encryption.h" | 3 | #include "encryption.h" |
| 2 | #include "shared.h" | 4 | #include "shared.h" |
| 3 | 5 | ||
| 4 | #include <sodium.h> | 6 | #include <sodium.h> |
| 5 | 7 | ||
| 8 | #include <sys/types.h> | ||
| 9 | #include <sys/stat.h> | ||
| 6 | #include <stdarg.h> | 10 | #include <stdarg.h> |
| 11 | #include <unistd.h> | ||
| 7 | #include <errno.h> | 12 | #include <errno.h> |
| 8 | #include <error.h> | 13 | #include <error.h> |
| 14 | #include <fcntl.h> | ||
| 9 | #include <stdio.h> | 15 | #include <stdio.h> |
| 10 | 16 | ||
| 11 | int checkSodium(void) { | 17 | int checkSodium(void) { |
| @@ -30,25 +36,47 @@ int checkSodium(void) { | |||
| 30 | 36 | ||
| 31 | 37 | ||
| 32 | int maketmp(const char *dest, const char *format, ...) { | 38 | int maketmp(const char *dest, const char *format, ...) { |
| 39 | char *filename = NULL, *fullpath = NULL; | ||
| 40 | struct stat fb; | ||
| 41 | int fd = -1; | ||
| 33 | va_list ap; | 42 | va_list ap; |
| 34 | va_start(ap, format); | ||
| 35 | 43 | ||
| 36 | 44 | // Make sure the destination exists and is a directory | |
| 45 | if(stat(dest, &fb) < 0) | ||
| 46 | return -1; | ||
| 47 | if(!S_ISDIR(fb.st_mode)) | ||
| 48 | return -2; | ||
| 37 | 49 | ||
| 50 | // Get the first half of the filename | ||
| 51 | va_start(ap, format); | ||
| 52 | if(vasprintf(&filename, format, ap) < 0) | ||
| 53 | return -3; | ||
| 38 | va_end(ap); | 54 | va_end(ap); |
| 39 | return 0; | 55 | |
| 56 | // Get the second half of the filename | ||
| 57 | int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)` | ||
| 58 | free(filename); | ||
| 59 | if(fps < 0) | ||
| 60 | return -4; | ||
| 61 | |||
| 62 | // Open the temp file | ||
| 63 | if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0) | ||
| 64 | fd = -5; | ||
| 65 | |||
| 66 | free(fullpath); | ||
| 67 | return fd; | ||
| 40 | } | 68 | } |
| 41 | 69 | ||
| 42 | 70 | ||
| 43 | 71 | ||
| 44 | int main() { | 72 | int main(int argc, char *argv[]) { |
| 45 | char *test = NULL; | 73 | if(argc != 3) |
| 46 | 74 | error(1, 0, "USAGE: <dest> <filename>"); | |
| 47 | // Turns out GNU did this for me, and I trust their code more than my own, so I'm using this now | 75 | |
| 48 | if(asprintf(&test, "We do a little trolling %d", 900) < 0) | 76 | |
| 49 | error(1, ENOMEM, "asprintf call failed"); | 77 | int fd = maketmp(argv[1], "%s.test", argv[2]); |
| 78 | if(fd < 0) | ||
| 79 | error(1, errno, "Couldn't open temp file"); | ||
| 50 | 80 | ||
| 51 | printf("%s\n", test); | ||
| 52 | |||
| 53 | return 0; | 81 | return 0; |
| 54 | } \ No newline at end of file | 82 | } \ No newline at end of file |
