1 /*
2 * This file is part of John the Ripper password cracker,
3 * Copyright (c) 1996-99,2003 by Solar Designer
4 *
5 * ...with changes in the jumbo patch, by JimF and magnum.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted.
9 *
10 * There's ABSOLUTELY NO WARRANTY, express or implied.
11 */
12
13 #include <stdio.h>
14 #define NEED_OS_FORK
15 #include "os.h"
16 #if (!AC_BUILT || HAVE_UNISTD_H) && !_MSC_VER
17 #include <unistd.h>
18 #endif
19 #ifdef _MSC_VER
20 #include <io.h>
21 #pragma warning ( disable : 4996 )
22 #endif
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #include "memory.h"
30 #include "logger.h"
31 #include "params.h"
32 #include "misc.h"
33 #include "options.h"
34
35 #include "john_mpi.h"
36
real_error(const char * file,int line)37 void real_error(const char *file, int line)
38 {
39 #ifndef _JOHN_MISC_NO_LOG
40 log_event("Terminating on error, %s:%d", file, line);
41 log_done();
42 #else
43 fprintf(stderr, "Terminating on error, %s:%d\n", file, line);
44 #endif
45
46 exit(1);
47 }
48
real_error_msg(const char * file,int line,const char * format,...)49 void real_error_msg(const char *file, int line, const char *format, ...)
50 {
51 va_list args;
52
53 #if defined(HAVE_MPI) && !defined(_JOHN_MISC_NO_LOG)
54 if (mpi_p > 1)
55 fprintf(stderr, "%u@%s: ", mpi_id + 1, mpi_name);
56 else
57 #elif OS_FORK && !defined(_JOHN_MISC_NO_LOG)
58 if (options.fork)
59 fprintf(stderr, "%u: ", options.node_min);
60 #endif
61 va_start(args, format);
62 vfprintf(stderr, format, args);
63 va_end(args);
64
65 real_error(file, line);
66 }
67
real_pexit(const char * file,int line,const char * format,...)68 void real_pexit(const char *file, int line, const char *format, ...)
69 {
70 va_list args;
71
72 #if !defined(_JOHN_MISC_NO_LOG)
73 #if HAVE_MPI
74 if (mpi_p > 1)
75 fprintf(stderr, "%u@%s: ", mpi_id + 1, mpi_name);
76 #endif
77 #if HAVE_MPI && OS_FORK
78 else
79 #endif
80 #if OS_FORK
81 if (options.fork)
82 fprintf(stderr, "%u: ", options.node_min);
83 #endif
84 #endif
85
86 va_start(args, format);
87 vfprintf(stderr, format, args);
88 va_end(args);
89
90 fprintf(stderr, ": %s\n", strerror(errno));
91
92 real_error(file, line);
93 }
94
write_loop(int fd,const char * buffer,int count)95 int write_loop(int fd, const char *buffer, int count)
96 {
97 int offset, block;
98
99 offset = 0;
100 while (count > 0) {
101 block = write(fd, &buffer[offset], count);
102
103 /* If any write(2) fails, we consider that the entire write_loop() has
104 * failed to do its job, unless we were interrupted by a signal. */
105 if (block < 0) {
106 if (errno == EINTR) continue;
107 return block;
108 }
109
110 offset += block;
111 count -= block;
112 }
113
114 /* Should be equal to the requested size, unless our kernel got crazy. */
115 return offset;
116 }
117
fgetl(char * s,int size,FILE * stream)118 char *fgetl(char *s, int size, FILE *stream)
119 {
120 char *res, *pos;
121 int c;
122
123 if ((res = fgets(s, size, stream))) {
124 if (!*res) return res;
125
126 pos = res + strlen(res) - 1;
127 if (*pos == '\n') {
128 *pos = 0;
129 if (pos > res)
130 if (*--pos == '\r') *pos = 0;
131 return res;
132 }
133 if ((pos-res) + 1 < size) {
134 /* There was a NULL byte in this line.
135 Look for \n past the 'read' null byte */
136 while ((++pos - res) + 1 < size)
137 if (*pos == '\n')
138 return res; /* found it read no more */
139 }
140 if ((c = getc(stream)) == '\n') {
141 if (*pos == '\r') *pos = 0;
142 } else
143 while (c != EOF && c != '\n')
144 /* Line was longer than our buffer. discard extra */
145 c = getc(stream);
146 }
147
148 return res;
149 }
150
fgetll(char * s,size_t size,FILE * stream)151 char *fgetll(char *s, size_t size, FILE *stream)
152 {
153 size_t len;
154 int c;
155 char *cp;
156
157 /* fgets' size arg is a signed int! */
158 assert(size <= INT32_MAX);
159
160 if (!fgets(s, size, stream))
161 return NULL;
162
163 len = strlen(s);
164
165 if (!len)
166 return s;
167
168 if (s[len-1] == '\n') {
169 s[--len] = 0;
170 while (len && (s[len-1] == '\n' || s[len-1] == '\r'))
171 s[--len] = 0;
172 return s;
173 }
174 else if (s[len-1] == '\r') {
175 s[--len] = 0;
176 while (len && (s[len-1] == '\n' || s[len-1] == '\r'))
177 s[--len] = 0;
178 /* we may have gotten the first byte of \r\n */
179 c = getc(stream);
180 if (c == EOF)
181 return s;
182 if (c != '\n')
183 ungetc(c, stream);
184 return s;
185 }
186 else if ((len + 1) < size) { /* We read a null byte */
187 /* first check past the null byte, looking for the \n */
188 while (++len < size)
189 if (s[len] == '\n')
190 return s; /* found it. Read no more. */
191 /* did not find the \n, so read and discard rest of line */
192 do {
193 c = getc(stream);
194 } while (c != EOF && c != '\n');
195 return s;
196 }
197
198 cp = strdup(s);
199
200 while (1) {
201 int increase = MIN((((len >> 12) + 1) << 12), 0x40000000);
202 size_t chunk_len;
203 void *new_cp;
204
205 new_cp = realloc(cp, len + increase);
206
207 while (!new_cp) {
208 increase >>= 2;
209 if (increase < 0x10000)
210 pexit("realloc");
211 new_cp = realloc(cp, len + increase);
212 }
213
214 cp = new_cp;
215
216 /* We get an EOF if there is no trailing \n on the last line */
217 if (!fgets(&cp[len], increase, stream))
218 return cp;
219
220 chunk_len = strlen(&cp[len]);
221 len += chunk_len;
222
223 if (cp[len-1] == '\n') {
224 cp[--len] = 0;
225 while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
226 cp[--len] = 0;
227 return cp;
228 }
229 else if (cp[len-1] == '\r') {
230 cp[--len] = 0;
231 while (len && (cp[len-1] == '\n' || cp[len-1] == '\r'))
232 cp[--len] = 0;
233 /* we may have gotten the first byte of \r\n */
234 c = getc(stream);
235 if (c == EOF)
236 return cp;
237 if (c != '\n')
238 ungetc(c, stream);
239 return cp;
240 }
241 else if ((chunk_len + 1) < increase) { /* We read a null byte */
242 /* first check past the null byte, looking for the \n */
243 while (++chunk_len < increase)
244 if (cp[++len] == '\n')
245 return cp; /* found it. read no more*/
246 /* did not find the \n, so read and discard rest of line */
247 do {
248 c = getc(stream);
249 } while (c != EOF && c != '\n');
250 return cp;
251 }
252 }
253 }
254
strncpy_pad(void * dst,const void * src,size_t size,uint8_t pad)255 void *strncpy_pad(void *dst, const void *src, size_t size, uint8_t pad)
256 {
257 uint8_t *d = dst;
258 const uint8_t *s = src;
259
260 // logically the same as: if (size < 1) IF size were signed.
261 if (!size || size > (((size_t)-1) >> 1))
262 return dst;
263
264 while (*s && size) {
265 *d++ = *s++;
266 --size;
267 }
268 while (size--)
269 *d++ = pad;
270
271 return dst;
272 }
273
strnfcpy(char * dst,const char * src,int size)274 char *strnfcpy(char *dst, const char *src, int size)
275 {
276 char *dptr;
277
278 if (size < 1)
279 return dst;
280 dptr = dst;
281
282 while (size--)
283 if (!(*dptr++ = *src++))
284 break;
285
286 return dst;
287 }
288
strnzcpy(char * dst,const char * src,int size)289 char *strnzcpy(char *dst, const char *src, int size)
290 {
291 char *dptr;
292
293 if (size < 1)
294 return dst;
295 dptr = dst;
296
297 while (--size)
298 if (!(*dptr++ = *src++))
299 return dst;
300 *dptr = 0;
301
302 return dst;
303 }
304
strnzcpylwr(char * dst,const char * src,int size)305 char *strnzcpylwr(char *dst, const char *src, int size)
306 {
307 char *dptr;
308
309 if (size < 1)
310 return dst;
311 dptr = dst;
312
313 while (--size) {
314 if (*src >= 'A' && *src <= 'Z') {
315 *dptr = *src | 0x20;
316 } else {
317 *dptr = *src;
318 if (!*src)
319 return dst;
320 }
321 dptr++;
322 src++;
323 }
324 *dptr = 0;
325
326 return dst;
327 }
328
strnzcpyn(char * dst,const char * src,int size)329 int strnzcpyn(char *dst, const char *src, int size)
330 {
331 char *dptr;
332
333 if (size < 1)
334 return 0;
335 dptr = dst;
336
337 while (--size)
338 if (!(*dptr++ = *src++))
339 return (dptr-dst)-1;
340 *dptr = 0;
341
342 return (dptr-dst);
343 }
344
strnzcpylwrn(char * dst,const char * src,int size)345 int strnzcpylwrn(char *dst, const char *src, int size)
346 {
347 char *dptr;
348
349 if (size < 1)
350 return 0;
351 dptr = dst;
352
353 while (--size) {
354 if (*src >= 'A' && *src <= 'Z') {
355 *dptr = *src | 0x20;
356 } else {
357 *dptr = *src;
358 if (!*src)
359 return (dptr-dst);
360 }
361 dptr++;
362 src++;
363 }
364 *dptr = 0;
365
366 return (dptr-dst);
367
368 }
369
strnzcat(char * dst,const char * src,int size)370 char *strnzcat(char *dst, const char *src, int size)
371 {
372 char *dptr;
373
374 if (size < 1)
375 return dst;
376 dptr = dst;
377
378 while (size && *dptr) {
379 size--; dptr++;
380 }
381 if (size)
382 while (--size)
383 if (!(*dptr++ = *src++))
384 break;
385 *dptr = 0;
386
387 return dst;
388 }
389
390 /*
391 * similar to strncat, but this one protects the dst buffer, AND it
392 * assures that dst is properly NULL terminated upon completion.
393 */
strnzcatn(char * dst,int size,const char * src,int src_max)394 char *strnzcatn(char *dst, int size, const char *src, int src_max)
395 {
396 char *dptr;
397
398 if (size < 1)
399 return dst;
400 dptr = dst;
401
402 while (size && *dptr)
403 size--, dptr++;
404 if (size)
405 while (--size && src_max--)
406 if (!(*dptr++ = *src++))
407 break;
408 *dptr = 0;
409 return dst;
410 }
411
412
413 /*
414 * strtok code, BUT returns empty token "" for adjacent delimiters. It also
415 * returns leading and trailing tokens for leading and trailing delimiters
416 * (strtok strips them away and does not return them). Several other issues
417 * in strtok also impact this code
418 * - static pointer, not thread safe
419 * - mangled input string (requires a copy)
420 * These 'bugs' were left in, so that this function is a straight drop in for
421 * strtok, with the caveat of returning empty tokens for the 3 conditions.
422 * Other than not being able to properly remove multiple adjacent tokens in
423 * data such as arbitrary white space removal of text files, this is really
424 * is what strtok should have been written to do from the beginning (IMHO).
425 * A strtokm_r() should be trivial to write if we need thread safety, or need
426 * to have multiple strtokm calls working at the same time, by just passing
427 * in the *last pointer.
428 * JimF, March 2015.
429 */
strtokm(char * s1,const char * delims)430 char *strtokm(char *s1, const char *delims)
431 {
432 static char *last = NULL;
433 char *endp;
434
435 if (!s1)
436 s1 = last;
437 if (!s1 || *s1 == 0)
438 return last = NULL;
439 endp = strpbrk(s1, delims);
440 if (endp) {
441 *endp = '\0';
442 last = endp + 1;
443 } else
444 last = NULL;
445 return s1;
446 }
447
atou(const char * src)448 unsigned int atou(const char *src)
449 {
450 unsigned val;
451
452 sscanf(src, "%u", &val);
453 return val;
454 }
455
456 /*
457 * _itoa replacement(s) but smarter/safer/better. _itoa is super useful, BUT
458 * not a standard C function, and it does not protect buffer, and also has
459 * what I deem strange behavior for negative numbers in non-base10 radix,
460 * however, that output does 'mimic' what is often seen in some programming
461 * type calculators.
462 *
463 * NOTE, differences between this function, and the _itoa found in visual C:
464 * 1. we pass in buffer size, and protect the buffer (not in the VC interface of itoa)
465 * 2. we handle all integers up to 63 bits
466 * 3. vc only - signs return on base 10. So _ltoa -666 base 16 return -29a, while
467 * vc's _itoa -666 base 16 returns fffffd66 (only 32 bit number for vc). I find
468 * our return much more in line, since the VC return is really forcing _itoa to
469 * return unsigned extension of the signed value, and not overly helpful. Both
470 * versions would return -666 for num of -666 if base was 10.
471 */
_lltoa(int64_t num,char * ret,int ret_sz,int base)472 MAYBE_INLINE const char *_lltoa(int64_t num, char *ret, int ret_sz, int base)
473 {
474 char *p = ret, *p1 = ret;
475 int64_t t;
476 // first 35 bytes handle neg digits. byte 36 handles 0, and last 35 handle positive digits.
477 const char bc[] = "zyxwvutsrqponmlkjihgfedcba987654321"
478 "0"
479 "123456789abcdefghijklmnopqrstuvwxyz";
480
481 if (--ret_sz < 1) // reduce ret_sz by 1 to handle the null
482 return ""; // we can not touch ret, it is 0 bytes long.
483 *ret = 0;
484 // if we can not handle this base, bail.
485 if (base < 2 || base > 36) return ret;
486 // handle the possible '-' char also. (reduces ret_sz to fit that char)
487 if (num < 0 && --ret_sz < 1)
488 return ret;
489 do {
490 // build our string reversed.
491 t = num;
492 num /= base;
493 *p++ = bc[35 + (t - num * base)];
494 if (num && p - ret == ret_sz)
495 break; // truncated MSB's but safe of buffer overflow.
496 } while (num);
497
498 if (t < 0) *p++ = '-'; // Apply negative sign
499 *p-- = 0;
500 // fast string-rev
501 for (; p > p1; ++p1, --p) {
502 *p1 ^= *p; *p ^= *p1; *p1 ^= *p;
503 }
504 return ret;
505 }
506
507 /*
508 * similar to _lltoa, but for unsigned types. there were enough changes that I did not
509 * want to make a single 'common' function. Would have added many more if's to
510 * and already semi-complex function.
511 */
_ulltoa(uint64_t num,char * ret,int ret_sz,int base)512 MAYBE_INLINE const char *_ulltoa(uint64_t num, char *ret, int ret_sz, int base)
513 {
514 char *p = ret, *p1 = ret;
515 uint64_t t;
516 const char bc[] = "0123456789abcdefghijklmnopqrstuvwxyz";
517
518 if (--ret_sz < 1)
519 return "";
520 *ret = 0;
521 if (base < 2 || base > 36) return ret;
522 do {
523 t = num;
524 num /= base;
525 *p++ = bc[t - num * base];
526 if (num && p - ret == ret_sz)
527 break; // truncated MSB's but safe of buffer overflow.
528 } while (num);
529
530 *p-- = 0;
531 // fast string-rev
532 for (; p > p1; ++p1, --p) {
533 *p1 ^= *p; *p ^= *p1; *p1 ^= *p;
534 }
535 return ret;
536 }
537
538 /*
539 * these are the functions 'external' that other code in JtR can use. These
540 * just call the 'common' code in the 2 inline functions.
541 */
jtr_itoa(int val,char * result,int rlen,int base)542 const char *jtr_itoa(int val, char *result, int rlen, int base)
543 {
544 return _lltoa(val, result, rlen, base);
545 }
546
jtr_utoa(unsigned int val,char * result,int rlen,int base)547 const char *jtr_utoa(unsigned int val, char *result, int rlen, int base)
548 {
549 return _ulltoa((uint64_t)val, result, rlen, base);
550 }
551
jtr_lltoa(int64_t val,char * result,int rlen,int base)552 const char *jtr_lltoa(int64_t val, char *result, int rlen, int base)
553 {
554 return _lltoa(val, result, rlen, base);
555 }
556
jtr_ulltoa(uint64_t val,char * result,int rlen,int base)557 const char *jtr_ulltoa(uint64_t val, char *result, int rlen, int base)
558 {
559 return _ulltoa(val, result, rlen, base);
560 }
561
human_prefix(uint64_t num)562 char *human_prefix(uint64_t num)
563 {
564 char *out = mem_alloc_tiny(16, MEM_ALIGN_NONE);
565 char prefixes[] = "\0KMGTPEZY";
566 char *p = prefixes;
567
568 while (p[1] && (num >= (100 << 10) ||
569 (!(num & 1023) && num >= (1 << 10)))) {
570 num = (num >> 10);
571 p++;
572 }
573
574 if (*p)
575 snprintf(out, 16, "%u %c", (uint32_t)num, *p);
576 else
577 snprintf(out, 16, "%u ", (uint32_t)num);
578
579 return out;
580 }
581
lcm(unsigned int x,unsigned int y)582 unsigned int lcm(unsigned int x, unsigned int y)
583 {
584 unsigned int tmp, a, b;
585
586 a = MAX(x, y);
587 b = MIN(x, y);
588
589 while (b) {
590 tmp = b;
591 b = a % b;
592 a = tmp;
593 }
594 return x / a * y;
595 }
596
ltrim(char * str)597 char *ltrim(char *str)
598 {
599 char *out = str;
600
601 while (*out == ' ' || *out == '\t')
602 out++;
603
604 return out;
605 }
606
rtrim(char * str)607 char *rtrim(char *str)
608 {
609 char *out = str + strlen(str) - 1;
610
611 while (out >= str && (*out == ' ' || *out == '\t'))
612 out--;
613
614 *(out+1) = '\0';
615 return str;
616 }
617