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.
34 static const char vers_id[] = "packlib.c : v2.3p2 Alec Muffett 18 May 1993";
37 PWOpen(const char *prefix, const char *mode)
40 char iname[STRINGSIZE];
41 char dname[STRINGSIZE];
42 char wname[STRINGSIZE];
47 if (pdesc.header.pih_magic == PIH_MAGIC)
49 fprintf(stderr, "%s: another dictionary already open\n", prefix);
50 return ((PWDICT *) 0);
53 memset(&pdesc, '\0', sizeof(pdesc));
55 sprintf(iname, "%s.pwi", prefix);
56 sprintf(dname, "%s.pwd", prefix);
57 sprintf(wname, "%s.hwm", prefix);
59 if (!(pdesc.dfp = fopen(dname, mode)))
62 return ((PWDICT *) 0);
65 if (!(pdesc.ifp = fopen(iname, mode)))
69 return ((PWDICT *) 0);
72 if ((pdesc.wfp = fopen(wname, mode)) != NULL)
74 pdesc.flags |= PFOR_USEHWMS;
83 pdesc.flags |= PFOR_WRITE;
84 pdesc.header.pih_magic = PIH_MAGIC;
85 pdesc.header.pih_blocklen = NUMWORDS;
86 pdesc.header.pih_numwords = 0;
88 fwrite((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp);
91 pdesc.flags &= ~PFOR_WRITE;
93 if (!fread((char *) &pdesc.header, sizeof(pdesc.header), 1, ifp))
95 fprintf(stderr, "%s: error reading header\n", prefix);
97 pdesc.header.pih_magic = 0;
100 return ((PWDICT *) 0);
103 if (pdesc.header.pih_magic != PIH_MAGIC)
105 fprintf(stderr, "%s: magic mismatch\n", prefix);
107 pdesc.header.pih_magic = 0;
110 return ((PWDICT *) 0);
113 if (pdesc.header.pih_blocklen != NUMWORDS)
115 fprintf(stderr, "%s: size mismatch\n", prefix);
117 pdesc.header.pih_magic = 0;
120 return ((PWDICT *) 0);
123 if (pdesc.flags & PFOR_USEHWMS)
125 if (fread(pdesc.hwms, 1, sizeof(pdesc.hwms), wfp) != sizeof(pdesc.hwms))
127 pdesc.flags &= ~PFOR_USEHWMS;
138 if (pwp->header.pih_magic != PIH_MAGIC)
140 fprintf(stderr, "PWClose: close magic mismatch\n");
144 if (pwp->flags & PFOR_WRITE)
146 pwp->flags |= PFOR_FLUSH;
147 PutPW(pwp, (char *) 0); /* flush last index if necess */
149 if (fseek(pwp->ifp, 0L, 0))
151 fprintf(stderr, "index magic fseek failed\n");
155 if (!fwrite((char *) &pwp->header, sizeof(pwp->header), 1, pwp->ifp))
157 fprintf(stderr, "index magic fwrite failed\n");
161 if (pwp->flags & PFOR_USEHWMS)
164 for (i=1; i<=0xff; i++)
168 pwp->hwms[i] = pwp->hwms[i-1];
171 printf("hwm[%02x] = %d\n", i, pwp->hwms[i]);
174 fwrite(pwp->hwms, 1, sizeof(pwp->hwms), pwp->wfp);
181 pwp->header.pih_magic = 0;
187 PutPW(PWDICT *pwp, const char *string)
189 if (!(pwp->flags & PFOR_WRITE))
196 strncpy(pwp->data[pwp->count], string, MAXWORDLEN);
197 pwp->data[pwp->count][MAXWORDLEN - 1] = '\0';
199 pwp->hwms[string[0] & 0xff]= pwp->header.pih_numwords;
202 ++(pwp->header.pih_numwords);
204 } else if (!(pwp->flags & PFOR_FLUSH))
209 if ((pwp->flags & PFOR_FLUSH) || !(pwp->count % NUMWORDS))
215 datum = (int32) ftell(pwp->dfp);
217 fwrite((char *) &datum, sizeof(datum), 1, pwp->ifp);
219 fputs(pwp->data[0], pwp->dfp);
224 for (i = 1; i < NUMWORDS; i++)
232 for (j = 0; ostr[j] && nstr[j] && (ostr[j] == nstr[j]); j++);
233 putc(j & 0xff, pwp->dfp);
234 fputs(nstr + j, pwp->dfp);
244 memset(pwp->data, '\0', sizeof(pwp->data));
251 GetPW(PWDICT *pwp, int32 number)
258 char buffer[NUMWORDS * MAXWORDLEN];
259 static char data[NUMWORDS][MAXWORDLEN];
262 thisblock = number / NUMWORDS;
264 if (fseek(pwp->ifp, sizeof(struct pi_header) + (thisblock * sizeof(int32)), 0))
266 perror("(index fseek failed)");
270 if (!fread((char *) &datum, sizeof(datum), 1, pwp->ifp))
272 perror("(index fread failed)");
276 if (fseek(pwp->dfp, datum, 0))
278 perror("(data fseek failed)");
282 if (!fread(buffer, 1, sizeof(buffer), pwp->dfp))
284 perror("(data fread failed)");
290 for (ostr = data[0]; (*(ostr++) = *(bptr++)) != '\0'; /* nothing */ );
294 for (i = 1; i < NUMWORDS; i++)
299 ostr = nstr + *(bptr++);
300 while ((*(ostr++) = *(bptr++)) != '\0');
305 return (data[number % NUMWORDS]);
309 FindPW(PWDICT *pwp, const char *string)
313 register int32 middle;
317 if (pwp->flags & PFOR_USEHWMS)
319 idx = string[0] & 0xff;
320 lwm = idx ? pwp->hwms[idx - 1] : 0;
321 hwm = pwp->hwms[idx];
325 hwm = PW_WORDS(pwp) - 1;
329 printf("---- %lu, %lu ----\n", lwm, hwm);
337 printf("%lu, %lu\n", lwm, hwm);
340 middle = lwm + ((hwm - lwm + 1) / 2);
343 * If GetPW returns NULL, we have a corrupt dictionary. It's hard to
344 * figure out the best thing to do here. Returning true for every
345 * password seems better than just crashing the program.
347 this = GetPW(pwp, middle);
352 cmp = strcmp(string, this); /* INLINE ? */
356 /* The following may be a little non-obvious... it's
363 * which is much clearer, but it unfortunately doesn't work
364 * because hwm is unsigned and middle may legitimately be
365 * zero, which would lead to hwm being set to a very high
366 * number. So instead we have...
382 return (PW_WORDS(pwp));