2 * This program is copyright Alec Muffett 1993. The author disclaims all
3 * responsibility or liability with respect to it's usage or its effect
4 * upon hardware or computer systems, and maintains copyright as set out
5 * in the "LICENCE" document which accompanies distributions of Crack v4.0
10 * Modified as part of the krb5-strength project as follows:
12 * 2007-03-23 Russ Allbery <eagle@eyrie.org>
13 * - Apply Debian patch to improve the search logic.
14 * - Don't crash if the dictionary is corrupt.
15 * - Additional system includes for other functions.
16 * 2009-10-14 Russ Allbery <eagle@eyrie.org>
17 * - Add ANSI C protototypes for all functions.
18 * - Tweaks for const cleanliness.
19 * - Add parentheses around assignment used for its truth value.
20 * - Make internal functions static.
21 * - Remove unused variables.
22 * 2009-11-18 Russ Allbery <eagle@eyrie.org>
23 * - Fixed the data format output by packer to properly pad the end.
24 * 2013-09-24 Russ Allbery <eagle@eyrie.org>
25 * - Add a missing ANSI C prototype.
26 * - Remove last block optimization in GetPW and start fresh each time.
27 * 2013-12-13 Russ Allbery <eagle@eyrie.org>
28 * - Close the wfp file handle on PWClose if it's open.
29 * 2016-11-06 Russ Allbery <eagle@eyrie.org>
30 * - Remove unused vers_id to silence GCC warnings.
39 PWOpen(const char *prefix, const char *mode)
42 char iname[STRINGSIZE];
43 char dname[STRINGSIZE];
44 char wname[STRINGSIZE];
49 if (pdesc.header.pih_magic == PIH_MAGIC)
51 fprintf(stderr, "%s: another dictionary already open\n", prefix);
52 return ((PWDICT *) 0);
55 memset(&pdesc, '\0', sizeof(pdesc));
57 sprintf(iname, "%s.pwi", prefix);
58 sprintf(dname, "%s.pwd", prefix);
59 sprintf(wname, "%s.hwm", prefix);
61 if (!(pdesc.dfp = fopen(dname, mode)))
64 return ((PWDICT *) 0);
67 if (!(pdesc.ifp = fopen(iname, mode)))
71 return ((PWDICT *) 0);
74 if ((pdesc.wfp = fopen(wname, mode)) != NULL)
76 pdesc.flags |= PFOR_USEHWMS;
85 pdesc.flags |= PFOR_WRITE;
86 pdesc.header.pih_magic = PIH_MAGIC;
87 pdesc.header.pih_blocklen = NUMWORDS;
88 pdesc.header.pih_numwords = 0;
90 fwrite((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp);
93 pdesc.flags &= ~PFOR_WRITE;
95 if (!fread((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp))
97 fprintf(stderr, "%s: error reading header\n", prefix);
99 pdesc.header.pih_magic = 0;
106 return ((PWDICT *) 0);
109 if (pdesc.header.pih_magic != PIH_MAGIC)
111 fprintf(stderr, "%s: magic mismatch\n", prefix);
113 pdesc.header.pih_magic = 0;
120 return ((PWDICT *) 0);
123 if (pdesc.header.pih_blocklen != NUMWORDS)
125 fprintf(stderr, "%s: size mismatch\n", prefix);
127 pdesc.header.pih_magic = 0;
134 return ((PWDICT *) 0);
137 if (pdesc.flags & PFOR_USEHWMS)
139 if (fread(pdesc.hwms, 1, sizeof(pdesc.hwms), wfp) != sizeof(pdesc.hwms))
141 pdesc.flags &= ~PFOR_USEHWMS;
152 if (pwp->header.pih_magic != PIH_MAGIC)
154 fprintf(stderr, "PWClose: close magic mismatch\n");
158 if (pwp->flags & PFOR_WRITE)
160 pwp->flags |= PFOR_FLUSH;
161 PutPW(pwp, (char *) 0); /* flush last index if necess */
163 if (fseek(pwp->ifp, 0L, 0))
165 fprintf(stderr, "index magic fseek failed\n");
169 if (!fwrite((char *) &pwp->header, sizeof(pwp->header), 1, pwp->ifp))
171 fprintf(stderr, "index magic fwrite failed\n");
175 if (pwp->flags & PFOR_USEHWMS)
178 for (i=1; i<=0xff; i++)
182 pwp->hwms[i] = pwp->hwms[i-1];
185 printf("hwm[%02x] = %d\n", i, pwp->hwms[i]);
188 fwrite(pwp->hwms, 1, sizeof(pwp->hwms), pwp->wfp);
194 if (pwp->wfp != NULL)
199 pwp->header.pih_magic = 0;
205 PutPW(PWDICT *pwp, const char *string)
207 if (!(pwp->flags & PFOR_WRITE))
214 strncpy(pwp->data[pwp->count], string, MAXWORDLEN);
215 pwp->data[pwp->count][MAXWORDLEN - 1] = '\0';
217 pwp->hwms[string[0] & 0xff]= pwp->header.pih_numwords;
220 ++(pwp->header.pih_numwords);
222 } else if (!(pwp->flags & PFOR_FLUSH))
227 if ((pwp->flags & PFOR_FLUSH) || !(pwp->count % NUMWORDS))
233 datum = (int32) ftell(pwp->dfp);
235 fwrite((char *) &datum, sizeof(datum), 1, pwp->ifp);
237 fputs(pwp->data[0], pwp->dfp);
242 for (i = 1; i < NUMWORDS; i++)
250 for (j = 0; ostr[j] && nstr[j] && (ostr[j] == nstr[j]); j++);
251 putc(j & 0xff, pwp->dfp);
252 fputs(nstr + j, pwp->dfp);
262 memset(pwp->data, '\0', sizeof(pwp->data));
269 GetPW(PWDICT *pwp, int32 number)
276 char buffer[NUMWORDS * MAXWORDLEN];
277 static char data[NUMWORDS][MAXWORDLEN];
280 thisblock = number / NUMWORDS;
282 if (fseek(pwp->ifp, sizeof(struct pi_header) + (thisblock * sizeof(int32)), 0))
284 perror("(index fseek failed)");
288 if (!fread((char *) &datum, sizeof(datum), 1, pwp->ifp))
290 perror("(index fread failed)");
294 if (fseek(pwp->dfp, datum, 0))
296 perror("(data fseek failed)");
300 if (!fread(buffer, 1, sizeof(buffer), pwp->dfp))
302 perror("(data fread failed)");
308 for (ostr = data[0]; (*(ostr++) = *(bptr++)) != '\0'; /* nothing */ );
312 for (i = 1; i < NUMWORDS; i++)
317 ostr = nstr + *(bptr++);
318 while ((*(ostr++) = *(bptr++)) != '\0');
323 return (data[number % NUMWORDS]);
327 FindPW(PWDICT *pwp, const char *string)
331 register int32 middle;
335 if (pwp->flags & PFOR_USEHWMS)
337 idx = string[0] & 0xff;
338 lwm = idx ? pwp->hwms[idx - 1] : 0;
339 hwm = pwp->hwms[idx];
343 hwm = PW_WORDS(pwp) - 1;
347 printf("---- %lu, %lu ----\n", lwm, hwm);
355 printf("%lu, %lu\n", lwm, hwm);
358 middle = lwm + ((hwm - lwm + 1) / 2);
361 * If GetPW returns NULL, we have a corrupt dictionary. It's hard to
362 * figure out the best thing to do here. Returning true for every
363 * password seems better than just crashing the program.
365 this = GetPW(pwp, middle);
370 cmp = strcmp(string, this); /* INLINE ? */
374 /* The following may be a little non-obvious... it's
381 * which is much clearer, but it unfortunately doesn't work
382 * because hwm is unsigned and middle may legitimately be
383 * zero, which would lead to hwm being set to a very high
384 * number. So instead we have...
400 return (PW_WORDS(pwp));