summaryrefslogtreecommitdiff
path: root/src/encryption.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encryption.c')
-rw-r--r--src/encryption.c152
1 files changed, 48 insertions, 104 deletions
diff --git a/src/encryption.c b/src/encryption.c
index 2ead9cf..8526f15 100644
--- a/src/encryption.c
+++ b/src/encryption.c
@@ -226,17 +226,6 @@ int linkto(const char * const target, int tgfd) {
226} 226}
227 227
228 228
229static void __ucl_close(void *fd) {
230 if(!fd) return;
231 close(*(int*)fd);
232 return;
233}
234
235static void __ucl_fclose(void *file) {
236 if(!file) return;
237 fclose((FILE*)file);
238 return;
239}
240 229
241/** 230/**
242 * @brief Encrypt src to dst using libsodium's xchacha encryption suite 231 * @brief Encrypt src to dst using libsodium's xchacha encryption suite
@@ -369,71 +358,45 @@ int encryptviatmp(const char * const target, const char * const output, const un
369 checksodium(); 358 checksodium();
370 #endif 359 #endif
371 360
372 cleanup_CREATE(10); 361 int fd = -1, tfd = -1, res = -1;
373 int fd = -1, tfd = -1;
374 FILE *src, *dst; 362 FILE *src, *dst;
375 char *targetdir; 363 char *targetdir;
376 364
377 // Open the target file 365 // Open the target file
378 if((fd = open(target, O_RDONLY)) < 0) 366 if((fd = open(target, O_RDONLY)) < 0) return -1;
379 return -1;
380 cleanup_REGISTER(__ucl_close, (void*)&fd);
381 367
382 // Create a temp file for writing 368 // Create a temp file for writing
383 targetdir = vxdirname(output); 369 targetdir = vxdirname(output);
384 if(!targetdir) 370 if(!targetdir) goto CLEANUP_encryptviatmp;
385 cleanup_MARK();
386 cleanup_CNDREGISTER(free, targetdir);
387 371
388 // Actually get the file descriptor for the temp file 372 // Actually get the file descriptor for the temp file
389 cleanup_CNDEXEC( 373 tfd = maketmp(targetdir);
390 tfd = maketmp(targetdir); 374 if(tfd < 0) goto CLEANUP_encryptviatmp;
391 if(tfd < 0)
392 cleanup_MARK();
393 cleanup_CNDREGISTER(__ucl_close, (void*)&tfd);
394 );
395 375
396 // Create a FILE* version of the source fd 376 // Create a FILE* version of the source fd
397 cleanup_CNDEXEC( 377 if(!(src = fdopen(fd, "rb"))) goto CLEANUP_encryptviatmp;
398 if(!(src = fdopen(fd, "rb"))) {
399 WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , target);
400 cleanup_MARK();
401 }
402 cleanup_CNDREGISTER(__ucl_fclose, (void*)&src);
403 )
404 378
405 // Create a FILE* version of the target fd 379 // Create a FILE* version of the target fd
406 cleanup_CNDEXEC( 380 if(!(dst = fdopen(tfd, "wb"))) goto CLEANUP_encryptviatmp;
407 if(!(dst = fdopen(tfd, "wb"))) {
408 WARN(errno, "<encryptviatmp> Couldn't open \"%s\"", , output);
409 cleanup_MARK();
410 }
411 cleanup_CNDREGISTER(__ucl_fclose, (void*)dst);
412 );
413
414 381
415 // Do the encryption now that everything has been set up 382 // Do the encryption now that everything has been set up
416 cleanup_CNDEXEC( 383 if(encrypttofile(src, dst, key) < 0) // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't
417 // Not going to bother changing this, it probably is catastrophic if an error happens when it shouldn't 384 ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",);
418 if(encrypttofile(src, dst, key) < 0)
419 ERROR(1, ENOTRECOVERABLE, "<encryptviatmp> I don't even have a way to cause an error here. How did you do it?",);
420 385
421 // Link the temp file into the system 386 // Link the temp file into the system
422 if(linkto(output, tfd) < 0) 387 if(linkto(output, tfd) < 0)
423 WARN(errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output); 388 WARN(errno, "<encryptviatmp> Could not link \"%s\" into system after encryption", , output);
424 389
425 free(targetdir); 390 res = 0;
426 fclose(dst);
427 fclose(src);
428 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors
429 );
430 391
392CLEANUP_encryptviatmp:
393 free(targetdir);
394 fclose(src);
395 fclose(dst);
396 close(fd);
397 close(tfd);
431 398
432 cleanup_CNDFIRE(); 399 return res;
433 if(cleanup_ERRORFLAGGED)
434 return -1;
435
436 return 0;
437} 400}
438 401
439/** 402/**
@@ -450,61 +413,43 @@ int decryptto(const char * const target, const char * const output, const unsign
450 checksodium(); 413 checksodium();
451 #endif 414 #endif
452 415
453 cleanup_CREATE(10);
454 FILE *src, *dst; 416 FILE *src, *dst;
455 int fdst; 417 int fdst, eflag = -1, res = -1;
456 418
457 // Open the source file 419 // Open the source file
458 if(!(src = fopen(target, "rb"))) { 420 if(!(src = fopen(target, "rb"))) {eflag = 0; goto CLEANUP_decryptto;}
459 WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , target);
460 return -1;
461 }
462 cleanup_REGISTER(__ucl_fclose, src);
463 421
464 // Get a temp descriptor for the temp file 422 // Get a temp descriptor for the temp file
465 cleanup_CNDEXEC( 423 if(!(fdst = maketmp(output))) {eflag = 1; goto CLEANUP_decryptto;}
466 fdst = maketmp(output);
467 if(!fdst) {
468 WARN(errno, "<decryptto> Could not get temp file for decryption", );
469 cleanup_MARK();
470 }
471 cleanup_CNDREGISTER(__ucl_close, (void*)&fdst);
472 );
473 424
474 // Open a FILE* version of the temp file 425 // Open a FILE* version of the temp file
475 cleanup_CNDEXEC( 426 if(!(dst = fdopen(fdst, "wb"))) {eflag = 2; goto CLEANUP_decryptto;}
476 if(!(dst = fdopen(fdst, "wb"))) {
477 WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , output);
478 cleanup_MARK();
479 }
480 cleanup_CNDREGISTER(__ucl_fclose, dst);
481 );
482 427
483 // Follow through with the rest of the decryption 428 // Follow through with the rest of the decryption
484 cleanup_CNDEXEC( 429 if(decrypttofile(src, dst, key) < 0) {eflag = 3; goto CLEANUP_decryptto;}
485 if(decrypttofile(src, dst, key) < 0) 430
486 ERROR(1, errno, "<decryptto> How did you even cause an error?",); 431 // Link temp into system
487 432 if(linkto(output, fdst) < 0) {eflag = 4; goto CLEANUP_decryptto;}
488 // Link temp into system 433
489 if(linkto(output, fdst) < 0) 434 res = 0;
490 WARN(errno, "<decryptto> Could not link \"%s\" into system", , output); 435
491 436CLEANUP_decryptto:
492 fclose(dst); 437 fclose(src);
493 fclose(src); 438 fclose(dst);
494 // fclose alco closes fd and tfd, as fdopen does not dup the file descriptors 439 close(fdst);
495 ); 440
496 441 if(___VXGG___VERBOSE_ERRORS___) {
497 cleanup_CNDFIRE(); 442 switch (eflag) {
498 if(cleanup_ERRORFLAGGED) 443 case 0: WARN(errno, "<decryptto> Could not open \"%s\" for decryption", , target); break;
499 return -1; 444 case 1: WARN(errno, "<decryptto> Could not get temp file for decryption", ); break;
500 445 case 2: WARN(errno, "<decryptto> Could not open \"%s\" for writing decrypted data", , output); break;
501 // Note: If an error were to theoretically occur, which shouldn't be possible but I'm covering my bases here, after the 446 case 3: ERROR(1, errno, "<decryptto> How did you even cause an error?",); break;
502 // `dst = fdopen` line, a double close on the temp file descriptor would occur. I've been told that this is not catastrophic, 447 case 4: WARN(errno, "<decryptto> Could not link \"%s\" into system", , output); break;
503 // and considering how my multithreading works it *should* be fine, but it very well could cause problems. The easy solution is 448 default: WARN(errno, "<decryptto> Ran into an error", ); break;
504 // to man up and just write another cleanup function to pop the last thing off the stack, but again this is an error I can't 449 }
505 // actually trigger so it's fine for now 450 }
506 451
507 return 0; 452 return res;
508} 453}
509 454
510/** 455/**
@@ -618,11 +563,10 @@ ctqueue * cryptscan() {
618 ctqueue *res = ctqueue_init(TPSIZE), *working = ctqueue_init(TPSIZE); 563 ctqueue *res = ctqueue_init(TPSIZE), *working = ctqueue_init(TPSIZE);
619 if(!res || !working) ERRRET(errno, NULL); 564 if(!res || !working) ERRRET(errno, NULL);
620 565
621 task *start = task_init(__cscan_worker, free, NULL); 566 task *start = task_new(__cscan_worker, free, NULL);
622 if(!start) ERRRET(errno, NULL); 567 if(!start) ERRRET(errno, NULL);
623 ctqueue_waitpush(working, start); 568 ctqueue_waitpush(working, start);
624 569
625
626 return res; 570 return res;
627} 571}
628 572