-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery.c
330 lines (276 loc) · 7.81 KB
/
recovery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* This file is part of John the Ripper password cracker,
* Copyright (c) 1996-2003,2005,2006,2009,2010,2013,2017 by Solar Designer
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted.
*
* There's ABSOLUTELY NO WARRANTY, express or implied.
*/
#ifndef __FreeBSD__
#define _XOPEN_SOURCE 500 /* for fdopen(3), fileno(3), fsync(2), ftruncate(2) */
#endif
#define NEED_OS_FLOCK
#include "os.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#include <string.h>
#include "arch.h"
#include "misc.h"
#include "params.h"
#include "path.h"
#include "memory.h"
#include "options.h"
#include "loader.h"
#include "logger.h"
#include "status.h"
#include "recovery.h"
#include "john.h"
char *rec_name = RECOVERY_NAME;
int rec_name_completed = 0;
int rec_version = 0;
int rec_argc = 0;
char **rec_argv;
unsigned int rec_check;
int rec_restoring_now = 0;
static int rec_fd;
static FILE *rec_file = NULL;
static struct db_main *rec_db;
static void (*rec_save_mode)(FILE *file);
static void rec_name_complete(void)
{
if (rec_name_completed)
return;
if (options.fork && !john_main_process) {
char *suffix = mem_alloc(1 + 20 + strlen(RECOVERY_SUFFIX) + 1);
sprintf(suffix, ".%u%s", options.node_min, RECOVERY_SUFFIX);
rec_name = path_session(rec_name, suffix);
MEM_FREE(suffix);
} else {
rec_name = path_session(rec_name, RECOVERY_SUFFIX);
}
rec_name_completed = 1;
}
#if OS_FLOCK
static void rec_lock(void)
{
if (flock(rec_fd, LOCK_EX | LOCK_NB)) {
if (errno == EWOULDBLOCK) {
fprintf(stderr, "Crash recovery file is locked: %s\n",
path_expand(rec_name));
error();
} else
pexit("flock(LOCK_EX)");
}
}
static void rec_unlock(void)
{
if (flock(rec_fd, LOCK_UN))
perror("flock(LOCK_UN)");
}
#else
#define rec_lock() \
{}
#define rec_unlock() \
{}
#endif
void rec_init(struct db_main *db, void (*save_mode)(FILE *file))
{
rec_done(1);
if (!rec_argc) return;
rec_name_complete();
if ((rec_fd = open(path_expand(rec_name), O_RDWR | O_CREAT, 0600)) < 0)
pexit("open: %s", path_expand(rec_name));
rec_lock();
if (!(rec_file = fdopen(rec_fd, "w"))) pexit("fdopen");
rec_db = db;
rec_save_mode = save_mode;
}
void rec_save(void)
{
int save_format;
long size;
char **opt;
log_flush();
if (!rec_file) return;
if (fseek(rec_file, 0, SEEK_SET)) pexit("fseek");
#ifdef __CYGWIN__
if (ftruncate(rec_fd, 0)) pexit("ftruncate");
#endif
save_format = !options.format && rec_db->loaded;
fprintf(rec_file, RECOVERY_V "\n%d\n",
rec_argc + (save_format ? 1 : 0));
opt = rec_argv;
while (*++opt)
fprintf(rec_file, "%s\n", *opt);
if (save_format)
fprintf(rec_file, "--format=%s\n",
rec_db->format->params.label);
fprintf(rec_file, "%u\n%u\n%x\n%x\n%x\n%x\n%x\n%x\n%x\n"
"%d\n%d\n%d\n%x\n",
status_get_time() + 1,
status.guess_count,
(unsigned int)(status.combs & 0xffffffffU),
(unsigned int)(status.combs >> 32),
status.combs_ehi,
(unsigned int)(status.crypts & 0xffffffffU),
(unsigned int)(status.crypts >> 32),
(unsigned int)(status.cands & 0xffffffffU),
(unsigned int)(status.cands >> 32),
status.compat,
status.pass,
status_get_progress ? status_get_progress() : -1,
rec_check);
if (rec_save_mode) rec_save_mode(rec_file);
if (ferror(rec_file)) pexit("fprintf");
if ((size = ftell(rec_file)) < 0) pexit("ftell");
if (fflush(rec_file)) pexit("fflush");
if (ftruncate(rec_fd, size)) pexit("ftruncate");
#ifndef __CYGWIN__
if (!options.fork && fsync(rec_fd))
pexit("fsync");
#endif
}
/* See the comment in recovery.h on how the "save" parameter is used */
void rec_done(int save)
{
if (!rec_file)
return;
/*
* If we're the main process for a --fork'ed group of children, leave our .rec
* file around until the children terminate (at which time we may be called
* again with save < 0, meaning forced non-saving).
*/
if (!save && options.fork && john_main_process) {
rec_save();
return;
}
if (save > 0)
rec_save();
else
log_flush();
if (fclose(rec_file))
pexit("fclose");
rec_file = NULL;
if ((!save || save == -1) && unlink(path_expand(rec_name)))
pexit("unlink: %s", path_expand(rec_name));
}
static void rec_format_error(char *fn)
{
if (fn && errno && ferror(rec_file))
pexit("%s", fn);
else {
fprintf(stderr, "Incorrect crash recovery file: %s\n",
path_expand(rec_name));
error();
}
}
void rec_restore_args(int lock)
{
char line[LINE_BUFFER_SIZE];
int index, argc;
char **argv;
char *save_rec_name;
unsigned int combs_lo, combs_hi;
rec_name_complete();
if (!(rec_file = fopen(path_expand(rec_name), "r+"))) {
if (options.fork && !john_main_process && errno == ENOENT) {
fprintf(stderr, "%u Session completed\n",
options.node_min);
if (options.flags & FLG_STATUS_CHK)
return;
log_event("No crash recovery file, terminating");
log_done();
exit(0);
}
pexit("fopen: %s", path_expand(rec_name));
}
rec_fd = fileno(rec_file);
if (lock) rec_lock();
if (!fgetl(line, sizeof(line), rec_file)) rec_format_error("fgets");
rec_version = 0;
if (!strcmp(line, RECOVERY_V4)) rec_version = 4; else
if (!strcmp(line, RECOVERY_V3)) rec_version = 3; else
if (!strcmp(line, RECOVERY_V2)) rec_version = 2; else
if (!strcmp(line, RECOVERY_V1)) rec_version = 1; else
if (strcmp(line, RECOVERY_V0)) rec_format_error(NULL);
if (fscanf(rec_file, "%d\n", &argc) != 1)
rec_format_error("fscanf");
if (argc < 2)
rec_format_error(NULL);
argv = mem_alloc_tiny(sizeof(char *) * (argc + 1), MEM_ALIGN_WORD);
argv[0] = "john";
for (index = 1; index < argc; index++)
if (fgetl(line, sizeof(line), rec_file))
argv[index] = str_alloc_copy(line);
else
rec_format_error("fgets");
argv[argc] = NULL;
save_rec_name = rec_name;
opt_init(argv[0], argc, argv);
rec_name = save_rec_name;
rec_name_completed = 1;
if (fscanf(rec_file, "%u\n%u\n%x\n%x\n",
&status_restored_time,
&status.guess_count,
&combs_lo, &combs_hi) != 4)
rec_format_error("fscanf");
status.combs = ((uint64_t)combs_hi << 32) | combs_lo;
if (!status_restored_time)
status_restored_time = 1;
if (rec_version >= 4) {
unsigned int crypts_lo, crypts_hi, cands_lo, cands_hi;
if (fscanf(rec_file, "%x\n%x\n%x\n%x\n%x\n%d\n",
&status.combs_ehi,
&crypts_lo, &crypts_hi,
&cands_lo, &cands_hi,
&status.compat) != 6)
rec_format_error("fscanf");
status.crypts = ((uint64_t)crypts_hi << 32) | crypts_lo;
status.cands = ((uint64_t)cands_hi << 32) | cands_lo;
} else {
/* Historically, we were reusing what became the combs field for candidates
* count when in --stdout mode */
status.cands = status.combs;
status.compat = 1;
}
if (rec_version == 0) {
status.pass = 0;
status.progress = -1;
} else
if (fscanf(rec_file, "%d\n%d\n", &status.pass, &status.progress) != 2)
rec_format_error("fscanf");
if (status.pass < 0 || status.pass > 3)
rec_format_error(NULL);
if (rec_version < 3)
rec_check = 0;
else
if (fscanf(rec_file, "%x\n", &rec_check) != 1)
rec_format_error("fscanf");
rec_restoring_now = 1;
}
void rec_restore_mode(int (*restore_mode)(FILE *file))
{
rec_name_complete();
if (!rec_file) return;
if (restore_mode)
if (restore_mode(rec_file)) rec_format_error("fscanf");
/*
* Unlocking the file explicitly is normally not necessary since we're about to
* close it anyway (which would normally release the lock). However, when
* we're the main process running with --fork, our newborn children may hold a
* copy of the fd for a moment (until they close the fd themselves). Thus, if
* we don't explicitly remove the lock, there may be a race condition between
* our children closing the fd and us proceeding to re-open and re-lock it.
*/
rec_unlock();
if (fclose(rec_file)) pexit("fclose");
rec_file = NULL;
rec_restoring_now = 0;
}