1 | /* |
---|
2 | * build_pwfile.c --- build a table of bad passwords, keyed by their |
---|
3 | * des equivalents. |
---|
4 | * |
---|
5 | * Written by Theodore Ts'o |
---|
6 | * |
---|
7 | * Copyright 1988 by the Massachusetts Institute of Technology. |
---|
8 | * |
---|
9 | * For copying and distribution information, please see the file |
---|
10 | * <mit-copyright.h>. |
---|
11 | */ |
---|
12 | |
---|
13 | #include <mit-copyright.h> |
---|
14 | #include <stdio.h> |
---|
15 | #include <sys/types.h> |
---|
16 | #include <sys/param.h> |
---|
17 | #include <sys/file.h> |
---|
18 | #include <string.h> |
---|
19 | |
---|
20 | #include <kadm.h> |
---|
21 | #include <kadm_err.h> |
---|
22 | #include <krb_db.h> |
---|
23 | #include "kadm_server.h" |
---|
24 | |
---|
25 | #ifdef NEED_SYS_FCNTL_H |
---|
26 | #include <sys/fcntl.h> |
---|
27 | #endif |
---|
28 | |
---|
29 | #ifdef NDBM |
---|
30 | #include <ndbm.h> |
---|
31 | #else /*NDBM*/ |
---|
32 | #include <dbm.h> |
---|
33 | #endif /*NDBM*/ |
---|
34 | |
---|
35 | /* Macros to convert ndbm names to dbm names. |
---|
36 | * Note that dbm_nextkey() cannot be simply converted using a macro, since |
---|
37 | * it is invoked giving the database, and nextkey() needs the previous key. |
---|
38 | * |
---|
39 | * Instead, all routines call "dbm_next" instead. |
---|
40 | */ |
---|
41 | #ifndef NDBM |
---|
42 | typedef char DBM; |
---|
43 | #define dbm_open(file, flags, mode) ((dbminit(file) == 0)?"":((char *)0)) |
---|
44 | #define dbm_fetch(db, key) fetch(key) |
---|
45 | #define dbm_store(db, key, content, flag) store(key, content) |
---|
46 | #define dbm_firstkey(db) firstkey() |
---|
47 | #define dbm_next(db,key) nextkey(key) |
---|
48 | #define dbm_close(db) dbmclose() |
---|
49 | #else |
---|
50 | #define dbm_next(db,key) dbm_nextkey(db) |
---|
51 | #endif |
---|
52 | |
---|
53 | main(argc, argv) |
---|
54 | int argc; |
---|
55 | char **argv; |
---|
56 | { |
---|
57 | DBM *pwfile; |
---|
58 | FILE *f; |
---|
59 | datum passwd, entry; |
---|
60 | des_cblock key; |
---|
61 | char word[1024]; |
---|
62 | int len, filenum, i; |
---|
63 | int wptr; |
---|
64 | |
---|
65 | if (argc != 2) { |
---|
66 | fprintf(stderr, "%s: Usage: %s filename\n", argv[0], argv[0]); |
---|
67 | exit(1); |
---|
68 | } |
---|
69 | if (!(f = fopen(argv[1], "r"))) { |
---|
70 | perror(argv[1]); |
---|
71 | exit(1); |
---|
72 | } |
---|
73 | pwfile = dbm_open(PW_CHECK_FILE, O_RDWR|O_CREAT, 0644); |
---|
74 | if (!pwfile) { |
---|
75 | fprintf(stderr, "Couldn't open %s for writing.\n", |
---|
76 | PW_CHECK_FILE); |
---|
77 | perror("dbm_open"); |
---|
78 | exit(1); |
---|
79 | } |
---|
80 | filenum = 0; |
---|
81 | do { |
---|
82 | filenum++; |
---|
83 | passwd.dptr = (char *) &i; |
---|
84 | passwd.dsize = sizeof(filenum); |
---|
85 | entry.dptr = argv[1]; |
---|
86 | entry.dsize = strlen(argv[1])+1; |
---|
87 | } while (dbm_store(pwfile, passwd, entry, DBM_INSERT)); |
---|
88 | i = 0; |
---|
89 | while (!feof(f)) { |
---|
90 | i++; |
---|
91 | wptr = (filenum << 24) + i; |
---|
92 | fgets(word, sizeof(word), f); |
---|
93 | len = strlen(word); |
---|
94 | if (len > 0 && word[len-1] == '\n') |
---|
95 | word[--len] = '\0'; |
---|
96 | #ifdef NOENCRYPTION |
---|
97 | memset((char *) key, 0, sizeof(key)); |
---|
98 | #else |
---|
99 | des_string_to_key(word, key); |
---|
100 | #endif |
---|
101 | passwd.dptr = (char *) key; |
---|
102 | passwd.dsize = 8; |
---|
103 | entry.dptr = (char *) &wptr; |
---|
104 | #ifdef notdef |
---|
105 | entry.dsize = sizeof(wptr); |
---|
106 | #else |
---|
107 | entry.dsize = 0; |
---|
108 | #endif |
---|
109 | dbm_store(pwfile, passwd, entry, DBM_REPLACE); |
---|
110 | } |
---|
111 | dbm_close(pwfile); |
---|
112 | exit(0); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | |
---|