1 | /* |
---|
2 | * |
---|
3 | * Copyright 1987, 1988 by the Massachusetts Institute of Technology. |
---|
4 | * |
---|
5 | * For copying and distribution information, please see the file |
---|
6 | * <mit-copyright.h>. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #include <mit-copyright.h> |
---|
11 | |
---|
12 | #include <stdio.h> |
---|
13 | #include <string.h> |
---|
14 | #include <sys/types.h> |
---|
15 | #include <sys/file.h> |
---|
16 | #include <sys/time.h> |
---|
17 | #include <sys/stat.h> |
---|
18 | #include <sys/wait.h> |
---|
19 | #include <signal.h> |
---|
20 | #ifdef POSIX |
---|
21 | #include <stdlib.h> |
---|
22 | #else |
---|
23 | extern char *malloc (); |
---|
24 | #endif |
---|
25 | |
---|
26 | #include <des.h> |
---|
27 | #include <krb.h> |
---|
28 | #include <krb_db.h> |
---|
29 | |
---|
30 | #define TRUE 1 |
---|
31 | #define FALSE 0 |
---|
32 | |
---|
33 | static C_Block master_key; |
---|
34 | static C_Block session_key; |
---|
35 | static Key_schedule master_key_schedule; |
---|
36 | char progname[] = "ext_srvtab"; |
---|
37 | char realm[REALM_SZ]; |
---|
38 | |
---|
39 | main(argc, argv) |
---|
40 | int argc; |
---|
41 | char *argv[]; |
---|
42 | { |
---|
43 | FILE *fout; |
---|
44 | char fname[1024]; |
---|
45 | int fopen_errs = 0; |
---|
46 | int arg; |
---|
47 | Principal princs[40]; |
---|
48 | int more; |
---|
49 | int prompt = TRUE; |
---|
50 | register int n, i; |
---|
51 | char *dbname, *kfile; |
---|
52 | |
---|
53 | memset(realm, 0, sizeof(realm)); |
---|
54 | |
---|
55 | /* Parse commandline arguments */ |
---|
56 | if (argc < 2) |
---|
57 | usage(); |
---|
58 | else { |
---|
59 | for (i = 1; i < argc; i++) { |
---|
60 | if (strcmp(argv[i], "-n") == 0) |
---|
61 | prompt = FALSE; |
---|
62 | else if (strcmp(argv[i], "-r") == 0) { |
---|
63 | if (++i >= argc) |
---|
64 | usage(); |
---|
65 | else { |
---|
66 | strcpy(realm, argv[i]); |
---|
67 | /* |
---|
68 | * This is to humor the broken way commandline |
---|
69 | * argument parsing is done. Later, this |
---|
70 | * program ignores everything that starts with -. |
---|
71 | */ |
---|
72 | argv[i][0] = '-'; |
---|
73 | } |
---|
74 | } |
---|
75 | else if (strcmp(argv[i], "-d") == 0) { |
---|
76 | if (++i >= argc) |
---|
77 | usage(); |
---|
78 | else { |
---|
79 | dbname = (char *) malloc(strlen(argv[i])+1); |
---|
80 | strcpy(dbname, argv[i]); |
---|
81 | if(kerb_db_set_name(dbname) != 0) { |
---|
82 | fprintf(stderr, "Could not set alternate database name (%s)\n", argv[i]); |
---|
83 | usage(); |
---|
84 | } |
---|
85 | argv[i][0] = '-'; |
---|
86 | } |
---|
87 | } |
---|
88 | else if (strcmp(argv[i], "-k") == 0) { |
---|
89 | if (++i >= argc) |
---|
90 | usage(); |
---|
91 | else { |
---|
92 | kfile = (char *) malloc(strlen(argv[i])+1); |
---|
93 | strcpy(kfile, argv[i]); |
---|
94 | argv[i][0] = '-'; |
---|
95 | } |
---|
96 | } |
---|
97 | else if (argv[i][0] == '-') |
---|
98 | usage(); |
---|
99 | else |
---|
100 | if (!k_isinst(argv[i])) { |
---|
101 | fprintf(stderr, "%s: bad instance name: %s\n", |
---|
102 | progname, argv[i]); |
---|
103 | usage(); |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | if (kdb_get_master_key_from (prompt, master_key, master_key_schedule, 0, kfile) != 0) { |
---|
109 | fprintf (stderr, "Couldn't read master key.\n"); |
---|
110 | fflush (stderr); |
---|
111 | exit(1); |
---|
112 | } |
---|
113 | |
---|
114 | if (kdb_verify_master_key (master_key, master_key_schedule, stderr) < 0) { |
---|
115 | exit(1); |
---|
116 | } |
---|
117 | |
---|
118 | /* For each arg, search for instances of arg, and produce */ |
---|
119 | /* srvtab file */ |
---|
120 | if (!realm[0]) |
---|
121 | if (krb_get_lrealm(realm, 1) != KSUCCESS) { |
---|
122 | fprintf(stderr, "%s: couldn't get local realm\n", progname); |
---|
123 | exit(1); |
---|
124 | } |
---|
125 | (void) umask(077); |
---|
126 | |
---|
127 | for (arg = 1; arg < argc; arg++) { |
---|
128 | if (argv[arg][0] == '-') |
---|
129 | continue; |
---|
130 | sprintf(fname, "%s-new-srvtab", argv[arg]); |
---|
131 | if ((fout = fopen(fname, "w")) == NULL) { |
---|
132 | fprintf(stderr, "Couldn't create file '%s'.\n", fname); |
---|
133 | fopen_errs++; |
---|
134 | continue; |
---|
135 | } |
---|
136 | printf("Generating '%s'....\n", fname); |
---|
137 | n = kerb_get_principal("*", argv[arg], &princs[0], 40, &more); |
---|
138 | if (more) |
---|
139 | fprintf(stderr, "More than 40 found...\n"); |
---|
140 | for (i = 0; i < n; i++) { |
---|
141 | FWrite(princs[i].name, strlen(princs[i].name) + 1, 1, fout); |
---|
142 | FWrite(princs[i].instance, strlen(princs[i].instance) + 1, |
---|
143 | 1, fout); |
---|
144 | FWrite(realm, strlen(realm) + 1, 1, fout); |
---|
145 | FWrite(&princs[i].key_version, |
---|
146 | sizeof(princs[i].key_version), 1, fout); |
---|
147 | memcpy(session_key, &princs[i].key_low, sizeof(KRB_INT32)); |
---|
148 | memcpy(session_key + sizeof(KRB_INT32), &princs[i].key_high, |
---|
149 | sizeof(KRB_INT32)); |
---|
150 | kdb_encrypt_key (session_key, session_key, |
---|
151 | master_key, master_key_schedule, DES_DECRYPT); |
---|
152 | FWrite(session_key, sizeof session_key, 1, fout); |
---|
153 | } |
---|
154 | fclose(fout); |
---|
155 | } |
---|
156 | |
---|
157 | StampOutSecrets(); |
---|
158 | |
---|
159 | exit(fopen_errs); /* 0 errors if successful */ |
---|
160 | |
---|
161 | } |
---|
162 | |
---|
163 | Die() |
---|
164 | { |
---|
165 | StampOutSecrets(); |
---|
166 | exit(1); |
---|
167 | } |
---|
168 | |
---|
169 | FWrite(p, size, n, f) |
---|
170 | char *p; |
---|
171 | int size; |
---|
172 | int n; |
---|
173 | FILE *f; |
---|
174 | { |
---|
175 | if (fwrite(p, size, n, f) != n) { |
---|
176 | printf("Error writing output file. Terminating.\n"); |
---|
177 | Die(); |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | StampOutSecrets() |
---|
182 | { |
---|
183 | memset(master_key, 0, sizeof master_key); |
---|
184 | memset(session_key, 0, sizeof session_key); |
---|
185 | memset(master_key_schedule, 0, sizeof master_key_schedule); |
---|
186 | } |
---|
187 | |
---|
188 | usage() |
---|
189 | { |
---|
190 | fprintf(stderr, |
---|
191 | "Usage: %s [-n] [-r realm] [-k master key file] [-d database] instance [instance ...]\n", progname); |
---|
192 | exit(1); |
---|
193 | } |
---|