|
1 | 1 | /* |
2 | 2 | * This file is part of John the Ripper password cracker, |
3 | 3 | * Copyright (c) 1996-2003,2005,2006,2009,2010,2013,2017 by Solar Designer |
4 | | - * |
5 | | - * ...with changes in the jumbo patch, by JimF and magnum. |
| 4 | + * Copyright (c) 2009-2026, magnum |
| 5 | + * Copyright (c) 2009-2018, JimF |
6 | 6 | * |
7 | 7 | * Redistribution and use in source and binary forms, with or without |
8 | 8 | * modification, are permitted. |
@@ -763,3 +763,82 @@ uint64_t rec_read_cands(char *session) |
763 | 763 |
|
764 | 764 | return ret; |
765 | 765 | } |
| 766 | + |
| 767 | +void rec_add_files(char *session) |
| 768 | +{ |
| 769 | + char *catchup_name; |
| 770 | + FILE *catchup_file; |
| 771 | + int64_t catchup_size = 0; |
| 772 | + char suffix[1 + 20 + sizeof(RECOVERY_SUFFIX)]; |
| 773 | + const char *sfx = RECOVERY_SUFFIX; |
| 774 | + |
| 775 | + if (!john_main_process && options.node_min) { |
| 776 | + snprintf(suffix, sizeof(suffix), ".%u%s", options.node_min, RECOVERY_SUFFIX); |
| 777 | + sfx = suffix; |
| 778 | + } |
| 779 | + catchup_name = path_session(session, sfx); |
| 780 | + |
| 781 | + if (!(catchup_file = fopen(catchup_name, "r+"))) |
| 782 | + pexit("fopen catch-up file: '%s'", catchup_name); |
| 783 | + |
| 784 | +#if !(__MINGW32__ || _MSC_VER) |
| 785 | + if (jtr_lock(fileno(catchup_file), F_SETLK, F_WRLCK, catchup_name)) |
| 786 | + error_msg("Error: Catch-up session-file '%s' is locked\n", catchup_name); |
| 787 | +#endif |
| 788 | + |
| 789 | + /* Check the original session-file's size */ |
| 790 | + if (jtr_fseek64(catchup_file, 0, SEEK_END)) |
| 791 | + pexit("fseek"); |
| 792 | + if ((catchup_size = jtr_ftell64(catchup_file)) == -1) |
| 793 | + pexit("ftell"); |
| 794 | + if (jtr_fseek64(catchup_file, 0, SEEK_SET)) |
| 795 | + pexit("fseek"); |
| 796 | + if (catchup_size <= 0 || catchup_size > SIZE_MAX) |
| 797 | + error_msg("Error: %s invalid size", catchup_name); |
| 798 | + |
| 799 | + /* Read original session-file data into memory */ |
| 800 | + char *catchup_data = mem_alloc_tiny(catchup_size + 1, MEM_ALIGN_NONE); |
| 801 | + catchup_data[catchup_size] = '\0'; |
| 802 | + |
| 803 | + if (fread(catchup_data, 1, (size_t)catchup_size, catchup_file) != catchup_size) { |
| 804 | + if (ferror(catchup_file)) |
| 805 | + pexit("fread"); |
| 806 | + error_msg("Error: fread: Unexpected EOF in %s\n", rec_name); |
| 807 | + } |
| 808 | + |
| 809 | + /* Parse session header */ |
| 810 | + char magic[16]; |
| 811 | + int argc, offset; |
| 812 | + if (sscanf(catchup_data, "%15s\n%d\n%n", magic, &argc, &offset) != 2 || argc < 2) |
| 813 | + error_msg("Error: %s invalid session-file format\n", rec_name); |
| 814 | + |
| 815 | + if (offset > catchup_size) |
| 816 | + error_msg("Error: %s corrupt offset\n", rec_name); |
| 817 | + |
| 818 | + if (jtr_fseek64(catchup_file, 0, SEEK_SET)) |
| 819 | + pexit("fseek"); |
| 820 | + |
| 821 | + /* Rewrite header */ |
| 822 | + fprintf(catchup_file, "%s\n%d\n", magic, argc + (int)options.passwd->count); |
| 823 | + |
| 824 | + /* Append new file entries */ |
| 825 | + struct list_entry *current; |
| 826 | + for (current = options.passwd->head; current; current = current->next) |
| 827 | + fprintf(catchup_file, "%s\n", current->data); |
| 828 | + |
| 829 | + /* Append original tail */ |
| 830 | + fprintf(catchup_file, "%s", catchup_data + offset); |
| 831 | + fclose(catchup_file); |
| 832 | + |
| 833 | + /* Delete this session's file */ |
| 834 | + const char *full_rec_name = path_expand(rec_name); |
| 835 | + if (unlink(full_rec_name)) |
| 836 | + pexit("unlink: %s", full_rec_name); |
| 837 | + |
| 838 | + /* Close this session's file */ |
| 839 | + if (rec_file) { |
| 840 | + if (fclose(rec_file)) |
| 841 | + pexit("fclose"); |
| 842 | + rec_file = NULL; |
| 843 | + } |
| 844 | +} |
0 commit comments