1 /*
2  * This file is part of John the Ripper password cracker,
3  * Copyright (c) 1996-99,2003,2004,2006,2010,2012,2013 by Solar Designer
4  *
5  * ...with changes in the jumbo patch, by magnum & JimF.
6  */
7 
8 #define NEED_OS_FORK
9 #include "os.h"
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 #include "misc.h"
15 #include "params.h"
16 #include "common.h"
17 #include "memory.h"
18 #include "signals.h"
19 #include "loader.h"
20 #include "logger.h"
21 #include "status.h"
22 #include "recovery.h"
23 #include "options.h"
24 #include "rpp.h"
25 #include "rules.h"
26 #include "external.h"
27 #include "cracker.h"
28 #include "john.h"
29 #include "unicode.h"
30 #include "config.h"
31 #include "opencl_common.h"
32 
33 /* List with optional global words to add for every salt */
34 struct list_main *single_seed;
35 
36 static double progress = 0;
37 static int rec_rule[2];
38 
39 static struct db_main *single_db;
40 static int rule_number, rule_count;
41 static int length, key_count;
42 static struct db_keys *guessed_keys;
43 static struct rpp_context *rule_ctx;
44 
45 static int words_pair_max;
46 static int retest_guessed;
47 static int recurse_depth, max_recursion;
48 static int orig_max_len, orig_min_kpc;
49 static int stacked_rule_count = 1;
50 static rule_stack single_rule_stack;
51 
52 #if HAVE_OPENCL || HAVE_ZTEX
53 static int acc_fmt, prio_resume;
54 #if HAVE_OPENCL
55 static int ocl_fmt;
56 #endif /* HAVE_OPENCL */
57 #endif /* HAVE_OPENCL || HAVE_ZTEX */
58 
59 int single_disabled_recursion;
60 
save_state(FILE * file)61 static void save_state(FILE *file)
62 {
63 	fprintf(file, "%d\n", rec_rule[0]);
64 	if (options.rule_stack)
65 		fprintf(file, "%d\n", rec_rule[1]);
66 }
67 
restore_rule_number(void)68 static int restore_rule_number(void)
69 {
70 	if (rule_ctx)
71 		for (rule_number = 0; rule_number < rec_rule[0]; rule_number++)
72 			if (!rpp_next(rule_ctx))
73 				return 1;
74 
75 	if (options.rule_stack) {
76 		single_rule_stack.rule = single_rule_stack.stack_rule->head;
77 		rules_stacked_number = 0;
78 		while (rules_stacked_number < rec_rule[1])
79 			if (!rules_advance_stack(&single_rule_stack, 1))
80 				return 1;
81 		log_event("+ Stacked Rule #%u: '%.100s' accepted",
82 		          rules_stacked_number + 1, single_rule_stack.rule->data);
83 	}
84 
85 	return 0;
86 }
87 
restore_state(FILE * file)88 static int restore_state(FILE *file)
89 {
90 	if (fscanf(file, "%d\n", &rec_rule[0]) != 1)
91 		return 1;
92 	if (options.rule_stack && fscanf(file, "%d\n", &rec_rule[1]) != 1)
93 		return 1;
94 	return restore_rule_number();
95 }
96 
get_progress(void)97 static double get_progress(void)
98 {
99 	double tot_rules, tot_rule_number;
100 
101 	emms();
102 
103 	tot_rules = rule_count * stacked_rule_count;
104 	tot_rule_number = rules_stacked_number * rule_count + rule_number;
105 
106 	return progress ? progress :
107 		100.0 * tot_rule_number / (tot_rules + 1);
108 }
109 
calc_buf_size(int length,int min_kpc)110 static uint64_t calc_buf_size(int length, int min_kpc)
111 {
112 	uint64_t res = sizeof(struct db_keys_hash) +
113 		sizeof(struct db_keys_hash_entry) * (min_kpc - 1);
114 
115 	res += (sizeof(struct db_keys) - 1 + length * min_kpc);
116 
117 	return res * single_db->salt_count;
118 }
119 
single_alloc_keys(struct db_keys ** keys)120 static void single_alloc_keys(struct db_keys **keys)
121 {
122 	int hash_size = sizeof(struct db_keys_hash) +
123 		sizeof(struct db_keys_hash_entry) * (key_count - 1);
124 
125 	if (!*keys) {
126 		*keys = mem_alloc_tiny(
127 			sizeof(struct db_keys) - 1 + length * key_count,
128 			MEM_ALIGN_WORD);
129 		(*keys)->hash = mem_alloc_tiny(hash_size, MEM_ALIGN_WORD);
130 	}
131 
132 	(*keys)->count = (*keys)->count_from_guesses = 0;
133 	(*keys)->ptr = (*keys)->buffer;
134 	(*keys)->have_words = 1; /* assume yes; we'll see for real later */
135 	(*keys)->rule[0] = rule_number;
136 	(*keys)->rule[1] = rules_stacked_number;
137 	(*keys)->lock = 0;
138 	memset((*keys)->hash, -1, hash_size);
139 }
140 
141 #undef log2
142 #define log2 jtr_log2
143 
log2(uint32_t val)144 static uint32_t log2(uint32_t val)
145 {
146 	uint32_t res = 0;
147 
148 	while (val >>= 1)
149 		res++;
150 
151 	return res;
152 }
153 
single_init(void)154 static void single_init(void)
155 {
156 	struct db_salt *salt;
157 	int lim_kpc, max_buffer_GB;
158 	uint64_t my_buf_share;
159 
160 #if HAVE_OPENCL || HAVE_ZTEX
161 	prio_resume = cfg_get_bool(SECTION_OPTIONS, NULL, "SinglePrioResume", 0);
162 
163 	acc_fmt = strcasestr(single_db->format->params.label, "-opencl") ||
164 		strcasestr(single_db->format->params.label, "-ztex");
165 #endif
166 #if HAVE_OPENCL
167 	ocl_fmt = acc_fmt && strcasestr(single_db->format->params.label, "-opencl");
168 #endif
169 
170 	log_event("Proceeding with \"single crack\" mode");
171 
172 	if ((options.flags & FLG_BATCH_CHK || rec_restored) && john_main_process) {
173 		fprintf(stderr, "Proceeding with single, rules:");
174 		if (options.rule_stack)
175 			fprintf(stderr, "(%s x %s)",
176 			        options.activesinglerules, options.rule_stack);
177 		else
178 			fprintf(stderr, "%s", options.activesinglerules);
179 		if (options.req_minlength >= 0 || options.req_maxlength)
180 			fprintf(stderr, ", lengths:%d-%d", options.eff_minlength,
181 			        options.eff_maxlength);
182 		fprintf(stderr, "\n");
183 	}
184 
185 	if (options.single_retest_guess)
186 		retest_guessed = parse_bool(options.single_retest_guess);
187 	else
188 		retest_guessed = cfg_get_bool(SECTION_OPTIONS, NULL,
189 		                              "SingleRetestGuessed", 1);
190 
191 	if (retest_guessed == -1)
192 		error_msg("Expected boolean value for --single-retest-guess=BOOL\n");
193 
194 	if ((words_pair_max = cfg_get_int(SECTION_OPTIONS, NULL,
195 	                                  "SingleWordsPairMax")) < 0)
196 		words_pair_max = SINGLE_WORDS_PAIR_MAX;
197 
198 	if ((max_recursion = cfg_get_int(SECTION_OPTIONS, NULL,
199 	                                 "SingleMaxRecursionDepth")) < 0)
200 		max_recursion = 10000;
201 
202 	if ((max_buffer_GB = cfg_get_int(SECTION_OPTIONS, NULL,
203 	                                  "SingleMaxBufferSize")) < 0)
204 		max_buffer_GB = SINGLE_MAX_WORD_BUFFER;
205 
206 	my_buf_share = (uint64_t)max_buffer_GB << 30;
207 
208 #if HAVE_MPI
209 	if (mpi_p_local > 1)
210 		my_buf_share /= mpi_p_local;
211 	else
212 #endif
213 #if OS_FORK
214 	if (options.fork)
215 		my_buf_share /= options.fork;
216 #endif
217 
218 	progress = 0;
219 
220 	length = options.eff_maxlength;
221 	key_count = single_db->format->params.min_keys_per_crypt;
222 	if (key_count < SINGLE_HASH_MIN)
223 		key_count = SINGLE_HASH_MIN;
224 /*
225  * We use "short" for buffered key indices and "unsigned short" for buffered
226  * key offsets - make sure these don't overflow.
227  *
228  * Jumbo now uses SINGLE_KEYS_TYPE and SINGLE_KEYS_UTYPE for this,
229  * from params.h and they may (individually) be 32-bit, eg. for OpenCL
230  */
231 	if (key_count > SINGLE_IDX_MAX)
232 		key_count = SINGLE_IDX_MAX;
233 	while (key_count > SINGLE_BUF_MAX / length + 1)
234 #if HAVE_OPENCL
235 		if (ocl_fmt)
236 			key_count -= MIN(key_count >> 1, local_work_size * ocl_v_width);
237 		else
238 #endif
239 			key_count >>= 1;
240 
241 	if (key_count < single_db->format->params.min_keys_per_crypt) {
242 		if (john_main_process) {
243 			fprintf(stderr,
244 "Note: Performance for this format/device may be lower due to single mode\n"
245 "      constraints. Format wanted %d keys per crypt but was limited to %d.\n",
246 			        single_db->format->params.min_keys_per_crypt,
247 			        key_count);
248 		}
249 		log_event(
250 "- Min KPC decreased from %d to %d due to single mode constraints.",
251 			single_db->format->params.min_keys_per_crypt,
252 			key_count);
253 	}
254 
255 /*
256  * For large salt counts, we need to limit total memory use as well.
257  */
258 	lim_kpc = key_count;
259 
260 	while (key_count >= 2 * SINGLE_HASH_MIN &&
261 	       calc_buf_size(length, key_count) > my_buf_share) {
262 		if (!options.req_maxlength && length >= 32 &&
263 		    (length >> 1) >= options.eff_minlength)
264 			length >>= 1;
265 		else if (!options.req_maxlength && length > 16 &&
266 		         (length - 1) >= options.eff_minlength)
267 			length--;
268 #if HAVE_OPENCL
269 		else if (ocl_fmt)
270 			key_count -= MIN(key_count >> 1, local_work_size * ocl_v_width);
271 #endif
272 		else
273 			key_count >>= 1;
274 	}
275 
276 	if (length < options.eff_maxlength) {
277 		if (john_main_process)
278 			fprintf(stderr,
279 "Note: Max. length decreased from %d to %d due to single mode buffer size\n"
280 "      limit of %sB. Use --max-length=N option to override, or increase\n"
281 "      SingleMaxBufferSize in john.conf (%sB needed).\n",
282 			        options.eff_maxlength,
283 			        length,
284 			        human_prefix(my_buf_share),
285 			        human_prefix(calc_buf_size(options.eff_maxlength,
286 			                                   key_count)));
287 		log_event(
288 "- Max. length decreased from %d to %d due to buffer size limit of %sB.",
289 			options.eff_maxlength,
290 			length,
291 			human_prefix(my_buf_share));
292 	}
293 
294 	if (calc_buf_size(length, key_count) > my_buf_share) {
295 		if (john_main_process) {
296 			fprintf(stderr,
297 "Note: Can't run single mode with this many salts due to single mode buffer\n"
298 "      size limit of %sB (%d keys per batch would use %sB, decreased to\n"
299 "      %d for %sB). To work around this, increase SingleMaxBufferSize in\n"
300 "      john.conf (if you have enough RAM) or load fewer salts at a time.\n",
301 			        human_prefix(my_buf_share),
302 			        lim_kpc,
303 			        human_prefix(calc_buf_size(length, lim_kpc)),
304 			        key_count,
305 			        human_prefix(calc_buf_size(length, key_count)));
306 		}
307 		if (lim_kpc < single_db->format->params.min_keys_per_crypt)
308 			log_event(
309 "- Min KPC decreased further to %d (%sB), can't meet buffer size limit of %sB.",
310 			key_count,
311 			human_prefix(calc_buf_size(length, key_count)),
312 			human_prefix(my_buf_share));
313 		else
314 			log_event(
315 "- Min KPC decreased from %d to %d (%sB), can't meet buffer size limit of %sB.",
316 			lim_kpc,
317 			key_count,
318 			human_prefix(calc_buf_size(length, key_count)),
319 			human_prefix(my_buf_share));
320 		error();
321 	}
322 
323 	if (key_count < lim_kpc) {
324 		if (john_main_process) {
325 			fprintf(stderr,
326 "Note: Performance for this many salts may be lower due to single mode buffer\n"
327 "      size limit of %sB (%d keys per batch would use %sB, decreased to\n"
328 "      %d for %sB). To work around this, ",
329 			        human_prefix(my_buf_share),
330 			        lim_kpc,
331 			        human_prefix(calc_buf_size(length, lim_kpc)),
332 			        key_count,
333 			        human_prefix(calc_buf_size(length, key_count)));
334 			if (options.eff_maxlength > 8)
335 				fprintf(stderr, "%s --max-length and/or ",
336 				        options.req_maxlength ?
337 				        "decrease" : "use");
338 			fprintf(stderr,
339 			        "increase%sSingleMaxBufferSize in john.conf.\n",
340 			        options.eff_maxlength > 8 ? "\n      " : " ");
341 		}
342 		if (lim_kpc < single_db->format->params.min_keys_per_crypt)
343 			log_event(
344 "- Min KPC decreased further to %d due to buffer size limit of %sB.",
345 			key_count,
346 			human_prefix(my_buf_share));
347 		else
348 			log_event(
349 "- Min KPC decreased from %d to %d due to buffer size limit of %sB.",
350 			lim_kpc,
351 			key_count,
352 			human_prefix(my_buf_share));
353 	}
354 
355 	if (rpp_init(rule_ctx, options.activesinglerules)) {
356 		log_event("! No \"%s\" mode rules found",
357 		          options.activesinglerules);
358 		if (john_main_process)
359 			fprintf(stderr, "No \"%s\" mode rules found in %s\n",
360 			        options.activesinglerules, cfg_name);
361 		error();
362 	}
363 
364 	/*
365 	 * Now set our possibly capped figures as global in order to get
366 	 * proper function and less warnings. We reset them in single_done
367 	 * for in case we run batch mode
368 	 */
369 	orig_max_len = options.eff_maxlength;
370 	options.eff_maxlength = length;
371 	orig_min_kpc = single_db->format->params.min_keys_per_crypt;
372 	single_db->format->params.min_keys_per_crypt = key_count;
373 
374 	if (single_seed->count) {
375 		words_pair_max += single_seed->count;
376 		log_event("- SingleWordsPairMax increased for %d global seed words",
377 		          single_seed->count);
378 	}
379 	if (log2(key_count) > words_pair_max) {
380 		words_pair_max = log2(key_count);
381 		log_event("- SingleWordsPairMax increased to %d for high KPC (%d)",
382 		          log2(key_count), key_count);
383 	}
384 
385 	log_event("- SingleWordsPairMax used is %d", words_pair_max);
386 	log_event("- SingleRetestGuessed = %s",retest_guessed ? "true" : "false");
387 	log_event("- SingleMaxBufferSize = %sB%s", human_prefix(my_buf_share),
388 #if HAVE_MPI
389 	          (mpi_p_local > 1 || options.fork)
390 #elif OS_FORK
391 	          options.fork
392 #else
393 	          0
394 #endif
395 	          ? " (per local process)" : "");
396 #if HAVE_OPENCL || HAVE_ZTEX
397 	log_event("- SinglePrioResume = %s", prio_resume ?
398 	          "Y (prioritize resumability over speed)" :
399 	          "N (prioritize speed over resumability)");
400 #endif
401 
402 	rules_init(single_db, length);
403 	rec_rule[0] = rule_number = 0;
404 	rec_rule[1] = rules_stacked_number = 0;
405 	rule_count = rules_count(rule_ctx, 0);
406 
407 	stacked_rule_count = rules_init_stack(options.rule_stack,
408 	                                      &single_rule_stack, single_db);
409 
410 	if (options.rule_stack)
411 		log_event("- Total %u (%d x %u) preprocessed word mangling rules",
412 		          rule_count * stacked_rule_count,
413 		          rule_count, stacked_rule_count);
414 	else
415 		log_event("- %d preprocessed word mangling rules", rule_count);
416 
417 	rules_stacked_after = (stacked_rule_count > 0);
418 
419 	if (!stacked_rule_count)
420 		stacked_rule_count = 1;
421 
422 	status_init(get_progress, 0);
423 
424 	rec_restore_mode(restore_state);
425 	rec_init(single_db, save_state);
426 
427 	salt = single_db->salts;
428 	do {
429 		single_alloc_keys(&salt->keys);
430 	} while ((salt = salt->next));
431 
432 	if (key_count > 1)
433 		log_event("- Allocated %d buffer%s of %d candidate passwords"
434 		          "%s (total %sB)",
435 		          single_db->salt_count,
436 		          single_db->salt_count != 1 ? "s" : "",
437 		          key_count,
438 		          single_db->salt_count != 1 ? " each" : "",
439 		          human_prefix(calc_buf_size(length, key_count)));
440 
441 	guessed_keys = NULL;
442 	single_alloc_keys(&guessed_keys);
443 
444 	crk_init(single_db, NULL, guessed_keys);
445 }
446 
single_key_hash(char * key)447 static MAYBE_INLINE int single_key_hash(char *key)
448 {
449 	unsigned int hash, extra, pos;
450 
451 	hash = (unsigned char)key[0];
452 	if (!hash)
453 		goto out;
454 	extra = (unsigned char)key[1];
455 	if (!extra)
456 		goto out_and;
457 
458 	pos = 3;
459 	if (length & 1) {
460 		while (key[2]) {
461 			hash += (unsigned char)key[2];
462 			if (!key[3] || pos >= length) break;
463 			extra += (unsigned char)key[3];
464 			key += 2;
465 			pos += 2;
466 		}
467 	} else {
468 		while (key[2] && pos < length) {
469 			hash += (unsigned char)key[2];
470 			if (!key[3]) break;
471 			extra += (unsigned char)key[3];
472 			key += 2;
473 			pos += 2;
474 		}
475 	}
476 
477 	hash -= extra + pos;
478 #if SINGLE_HASH_LOG > 6
479 	hash ^= extra << 6;
480 #endif
481 
482 out_and:
483 	hash &= SINGLE_HASH_SIZE - 1;
484 out:
485 	return hash;
486 }
487 
488 static int single_process_buffer(struct db_salt *salt);
489 
single_add_key(struct db_salt * salt,char * key,int is_from_guesses)490 static int single_add_key(struct db_salt *salt, char *key, int is_from_guesses)
491 {
492 	struct db_keys *keys = salt->keys;
493 	int index, new_hash, reuse_hash;
494 	struct db_keys_hash_entry *entry;
495 
496 	if (options.rule_stack)
497 		if (!(key = rules_process_stack(key, &single_rule_stack)))
498 			return 0;
499 
500 /* Check if this is a known duplicate, and reject it if so */
501 	if ((index = keys->hash->hash[new_hash = single_key_hash(key)]) >= 0)
502 	do {
503 		entry = &keys->hash->list[index];
504 		if (!strncmp(key, &keys->buffer[entry->offset], length))
505 			return 0;
506 	} while ((index = entry->next) >= 0);
507 
508 /* Update the hash table removing the list entry we're about to reuse */
509 	index = keys->hash->hash[reuse_hash = single_key_hash(keys->ptr)];
510 	if (index == keys->count)
511 		keys->hash->hash[reuse_hash] = keys->hash->list[index].next;
512 	else
513 	if (index >= 0) {
514 		entry = &keys->hash->list[index];
515 		while ((index = entry->next) >= 0) {
516 			if (index == keys->count) {
517 				entry->next = keys->hash->list[index].next;
518 				break;
519 			}
520 			entry = &keys->hash->list[index];
521 		}
522 	}
523 
524 /* Add the new entry */
525 	index = keys->hash->hash[new_hash];
526 	entry = &keys->hash->list[keys->count];
527 	entry->next = index;
528 	entry->offset = keys->ptr - keys->buffer;
529 	keys->hash->hash[new_hash] = keys->count;
530 
531 	strnfcpy(keys->ptr, key, length);
532 	keys->ptr += length;
533 
534 	keys->count_from_guesses += is_from_guesses;
535 
536 	if (++(keys->count) >= key_count)
537 		return single_process_buffer(salt);
538 
539 	return 0;
540 }
541 
single_process_buffer(struct db_salt * salt)542 static int single_process_buffer(struct db_salt *salt)
543 {
544 	struct db_salt *current;
545 	struct db_keys *keys;
546 	size_t size;
547 
548 	if (++recurse_depth > max_recursion && retest_guessed) {
549 		log_event("- Disabled SingleRetestGuessed due to deep recursion");
550 		if (john_main_process)
551 			fprintf(stderr,
552 "Warning: Disabled SingleRetestGuessed due to deep recursion. You can run\n"
553 "         '--loopback --rules=none' later on instead.\n");
554 
555 		retest_guessed = 0;
556 		single_disabled_recursion = 1;
557 	}
558 
559 	if (crk_process_salt(salt))
560 		return 1;
561 
562 /*
563  * Flush the keys list (since we've just processed the keys), but not the hash
564  * table to allow for more effective checking for duplicates.  We could flush
565  * the hash table too, such as by calling single_alloc_keys() here, which would
566  * allow us to drop the update-hash-before-list-entry-reuse code from
567  * single_add_key().  This would speed things up in terms of this source file's
568  * code overhead, however it would allow more duplicates to pass.  The apparent
569  * c/s rate (counting duplicates as if they were distinct combinations) would
570  * be higher, but the number of passwords cracked per unit of time might be
571  * lower or higher depending on many things including the relative speed of
572  * password hash computations vs. the "overhead".
573  */
574 	keys = salt->keys;
575 	keys->count = keys->count_from_guesses = 0;
576 	keys->ptr = keys->buffer;
577 	keys->lock++;
578 
579 	if (retest_guessed)
580 	if (guessed_keys->count) {
581 		keys = mem_alloc(size = sizeof(struct db_keys) - 1 +
582 			length * guessed_keys->count);
583 		memcpy(keys, guessed_keys, size);
584 
585 		keys->ptr = keys->buffer;
586 		do {
587 			current = single_db->salts;
588 			do {
589 				if (current == salt || !current->list)
590 					continue;
591 
592 				if (single_add_key(current, keys->ptr, 1)) {
593 					MEM_FREE(keys);
594 					return 1;
595 				}
596 			} while ((current = current->next));
597 			keys->ptr += length;
598 		} while (--keys->count);
599 
600 		MEM_FREE(keys);
601 	}
602 
603 	keys = salt->keys;
604 	keys->lock--;
605 	if (!keys->count && !keys->lock) {
606 		keys->rule[0] = rule_number;
607 		keys->rule[1] = rules_stacked_number;
608 	}
609 
610 	recurse_depth--;
611 	return 0;
612 }
613 
single_process_pw(struct db_salt * salt,struct db_password * pw,char * rule)614 static int single_process_pw(struct db_salt *salt, struct db_password *pw,
615 	char *rule)
616 {
617 	struct list_entry *first, *second;
618 	struct list_entry *global_head = single_seed->head;
619 	int first_global, second_global;
620 	int first_number, second_number;
621 	char pair[RULE_WORD_SIZE];
622 	int split;
623 	char *key;
624 
625 	if (!(first = pw->words->head))
626 		return -1;
627 
628 	first_number = first_global = 0;
629 	do {
630 		if (first == global_head)
631 			first_global = 1;
632 		if ((key = rules_apply(first->data, rule, 0, NULL)))
633 		if (ext_filter(key))
634 		if (single_add_key(salt, key, 0))
635 			return 1;
636 		if (!salt->list)
637 			return 2;
638 		if (!pw->binary)
639 			return 0;
640 
641 		if (++first_number > words_pair_max)
642 			continue;
643 
644 		if (!CP_isLetter[(unsigned char)first->data[0]])
645 			continue;
646 
647 		second_number = second_global = 0;
648 		second = pw->words->head;
649 
650 		do {
651 			if (second == global_head)
652 				second_global = 1;
653 			if (first == second || (first_global && second_global))
654 				continue;
655 			if ((split = strlen(first->data)) < length) {
656 				strnzcpy(pair, first->data, RULE_WORD_SIZE);
657 				strnzcat(pair, second->data, RULE_WORD_SIZE);
658 
659 				if ((key = rules_apply(pair, rule, split, NULL)))
660 				if (ext_filter(key))
661 				if (single_add_key(salt, key, 0))
662 					return 1;
663 				if (!salt->list)
664 					return 2;
665 				if (!pw->binary)
666 					return 0;
667 			}
668 
669 			if (!first_global && first->data[1]) {
670 				pair[0] = first->data[0];
671 				pair[1] = 0;
672 				strnzcat(pair, second->data, RULE_WORD_SIZE);
673 
674 				if ((key = rules_apply(pair, rule, 1, NULL)))
675 				if (ext_filter(key))
676 				if (single_add_key(salt, key, 0))
677 					return 1;
678 				if (!salt->list)
679 					return 2;
680 				if (!pw->binary)
681 					return 0;
682 			}
683 		} while (++second_number <= words_pair_max &&
684 			(second = second->next));
685 	} while ((first = first->next));
686 
687 	return 0;
688 }
689 
690 #define tot_rule_no (rules_stacked_number * rule_count + rule_number)
691 #define tot_rule_now (keys->rule[1] * rule_count + keys->rule[0])
692 
single_process_salt(struct db_salt * salt,char * rule)693 static int single_process_salt(struct db_salt *salt, char *rule)
694 {
695 	struct db_keys *keys;
696 	struct db_password *pw, **last;
697 	int status, have_words = 0;
698 
699 	keys = salt->keys;
700 
701 	if (!keys->have_words)
702 		goto no_own_words;
703 
704 	last = &salt->list;
705 	pw = *last;
706 	do {
707 /*
708  * "binary" is set to NULL on entries marked for removal (so we remove them
709  * here) or already removed (yet we might hit them once in some obscure cases).
710  */
711 		if (pw->binary) {
712 			if (!(status = single_process_pw(salt, pw, rule))) {
713 				have_words = 1;
714 				goto next;
715 			}
716 			if (status < 0) /* no words for this hash */
717 				goto next;
718 			if (status == 2) /* no hashes left for this salt */
719 				return 0;
720 			return 1; /* no hashes left to crack for all salts */
721 		} else {
722 			*last = pw->next; /* remove */
723 		}
724 next:
725 		last = &pw->next;
726 	} while ((pw = pw->next));
727 
728 #if HAVE_OPENCL || HAVE_ZTEX
729 	if (!acc_fmt || prio_resume)
730 #endif
731 	if (keys->count && tot_rule_no - tot_rule_now > (key_count << 1))
732 		if (single_process_buffer(salt))
733 			return 1;
734 
735 	if (!keys->count) {
736 		keys->rule[0] = rule_number;
737 		keys->rule[1] = rules_stacked_number;
738 	}
739 
740 	if (!have_words) {
741 		keys->have_words = 0;
742 no_own_words:
743 		if (keys->count && single_process_buffer(salt))
744 			return 1;
745 	}
746 
747 	return 0;
748 }
749 
single_run(void)750 static void single_run(void)
751 {
752 	char *prerule, *rule;
753 	struct db_salt *salt;
754 	int min[2], saved_min[2];
755 	int have_words;
756 
757 	saved_min[0] = rec_rule[0];
758 	saved_min[1] = rec_rule[1];
759 	rpp_real_run = 1;
760 
761 	do {
762 		rec_rule[1] = min[1] = rules_stacked_number;
763 		while ((prerule = rpp_next(rule_ctx))) {
764 			if (options.node_count) {
765 				int for_node = rule_number % options.node_count + 1;
766 				if (for_node < options.node_min ||
767 				    for_node > options.node_max) {
768 					rule_number++;
769 					continue;
770 				}
771 			}
772 
773 			if (!(rule = rules_reject(prerule, 0, NULL, single_db))) {
774 				rule_number++;
775 				if (options.verbosity >= VERB_DEFAULT &&
776 				    !rules_mute &&
777 				    strncmp(prerule, "!!", 2))
778 					log_event("- Rule #%d: '%.100s' rejected",
779 					          rule_number, prerule);
780 				continue;
781 			}
782 
783 			if (!rules_mute) {
784 				if (strcmp(prerule, rule)) {
785 					log_event("- Rule #%d: '%.100s' accepted as '%.100s'",
786 					          rule_number + 1, prerule, rule);
787 				} else {
788 					log_event("- Rule #%d: '%.100s' accepted",
789 					          rule_number + 1, prerule);
790 				}
791 			}
792 
793 			if (saved_min[0] != rec_rule[0] || saved_min[1] != rec_rule[1]) {
794 				if (!rules_mute) {
795 					if (options.rule_stack)
796 						log_event("- Oldest still in use rules are now "
797 						          "base #%d, stacked #%d",
798 						          rec_rule[0] + 1, rec_rule[1] + 1);
799 					else
800 						log_event("- Oldest still in use is now rule #%d",
801 						          rec_rule[0] + 1);
802 				}
803 				saved_min[0] = rec_rule[0];
804 				saved_min[1] = rec_rule[1];
805 			}
806 
807 			have_words = 0;
808 
809 			min[0] = rule_number;
810 
811 			/* pot reload might have removed the salt */
812 			if (!(salt = single_db->salts))
813 				return;
814 			do {
815 				if (!salt->list)
816 					continue;
817 				if (single_process_salt(salt, rule))
818 					return;
819 				if (!salt->keys->have_words)
820 					continue;
821 				have_words = 1;
822 				if (salt->keys->rule[0] < min[0])
823 					min[0] = salt->keys->rule[0];
824 				if (salt->keys->rule[1] < min[1])
825 					min[1] = salt->keys->rule[1];
826 			} while ((salt = salt->next));
827 
828 			if (event_reload && single_db->salts)
829 				crk_reload_pot();
830 
831 			rec_rule[0] = min[0];
832 			rule_number++;
833 
834 			if (have_words)
835 				continue;
836 
837 			log_event("- No information to base%s candidate passwords on",
838 			          rule_number > 1 ? " further" : "");
839 			return;
840 		}
841 
842 		if (rules_stacked_after) {
843 			saved_min[0] = rule_number = 0;
844 			rpp_init(rule_ctx, options.activesinglerules);
845 			if (!rules_mute && options.verbosity <= VERB_DEFAULT) {
846 				rules_mute = 1;
847 				if (john_main_process) {
848 					log_event(
849 "- Some rule logging suppressed. Re-enable with --verbosity=%d or greater",
850 					          VERB_LEGACY);
851 				}
852 			}
853 		}
854 
855 	} while (rules_stacked_after && rules_advance_stack(&single_rule_stack, 0));
856 }
857 
single_done(void)858 static void single_done(void)
859 {
860 	struct db_salt *salt;
861 
862 	if (!event_abort) {
863 		if ((salt = single_db->salts)) {
864 			log_event("- Processing the remaining buffered "
865 				"candidate passwords, if any");
866 
867 			if (options.verbosity >= VERB_DEFAULT)
868 				fprintf(stderr, "Almost done: Processing the remaining "
869 				        "buffered candidate passwords, if any.\n");
870 
871 			do {
872 				if (!salt->list)
873 					continue;
874 				if (salt->keys->count)
875 					if (single_process_buffer(salt))
876 						break;
877 			} while ((salt = salt->next));
878 		}
879 
880 		progress = 100;
881 	}
882 
883 	options.eff_maxlength = orig_max_len;
884 	single_db->format->params.min_keys_per_crypt = orig_min_kpc;
885 
886 	rec_done(event_abort || (status.pass && single_db->salts));
887 	crk_done();
888 }
889 
do_single_crack(struct db_main * db)890 void do_single_crack(struct db_main *db)
891 {
892 	struct rpp_context ctx;
893 
894 	single_db = db;
895 	rule_ctx = &ctx;
896 	single_init();
897 	single_run();
898 	single_done();
899 	rule_ctx = NULL; /* Just for good measure */
900 }
901