1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* Copyright 1994 by OpenVision Technologies, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appears in all copies and
* that both that copyright notice and this permission notice appear in
* supporting documentation, and that the name of OpenVision not be used
* in advertising or publicity pertaining to distribution of the software
* without specific, written prior permission. OpenVision makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
*
* OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <krb5/krb5.h>
#include <krb5/asn1.h>
#include <krb5/kdb.h>
#include <krb5/kdb_dbm.h>
#ifdef PROVIDE_DES_CBC_CRC
#include <krb5/mit-des.h>
#endif
#include <krb.h>
#include "krb524.h"
#define TIMEOUT 60
#define TKT_BUFSIZ 2048
#define MSGSIZE 8192
char *whoami;
int signalled = 0;
static int debug = 0;
int use_keytab;
char *keytab = NULL;
krb5_keytab kt;
int use_master;
krb5_principal master_princ;
krb5_encrypt_block master_encblock;
krb5_keyblock master_keyblock;
void init_keytab(), init_master();
krb5_error_code do_connection(), lookup_service_key(), kdc_get_server_key();
void usage()
{
fprintf(stderr, "Usage: %s [-m[aster]] [-k[eytab]]\n", whoami);
cleanup_and_exit(1);
}
int request_exit()
{
signalled = 1;
}
int krb5_free_keyblock_contents(krb5_keyblock *key)
{
memset(key->contents, 0, key->length);
krb5_xfree(key->contents);
return 0;
}
main(int argc, char **argv)
{
struct servent *serv;
struct sockaddr_in saddr;
struct timeval timeout;
int ret, s, conn;
fd_set rfds;
krb5_init_ets();
whoami = ((whoami = strrchr(argv[0], '/')) ? whoami + 1 : argv[0]);
argv++; argc--;
use_master = use_keytab = 0;
while (argc) {
if (strncmp(*argv, "-k", 2) == 0)
use_keytab = 1;
else if (strncmp(*argv, "-m", 2) == 0)
use_master = 1;
else
break;
argv++; argc--;
}
if (argc || use_keytab + use_master > 1 ||
use_keytab + use_master == 0)
usage();
signal(SIGINT, request_exit);
signal(SIGHUP, request_exit);
signal(SIGTERM, request_exit);
if (use_keytab)
init_keytab();
if (use_master)
init_master();
memset((char *) &saddr, 0, sizeof(struct sockaddr_in));
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = INADDR_ANY;
serv = getservbyname(KRB524_SERVICE, "udp");
if (serv == NULL) {
com_err(whoami, 0, "service entry not found, using %d", KRB524_PORT);
saddr.sin_port = htons(KRB524_PORT);
} else
saddr.sin_port = serv->s_port;
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
com_err(whoami, errno, "creating main socket");
cleanup_and_exit(1);
}
if ((ret = bind(s, (struct sockaddr *) &saddr,
sizeof(struct sockaddr_in))) < 0) {
com_err(whoami, errno, "binding main socket");
cleanup_and_exit(1);
}
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
while (1) {
FD_ZERO(&rfds);
FD_SET(s, &rfds);
ret = select(s+1, &rfds, NULL, NULL, &timeout);
if (signalled)
cleanup_and_exit(0);
else if (ret == 0) {
if (use_master) {
ret = krb5_dbm_db_fini();
if (ret && ret != KRB5_KDB_DBNOTINITED) {
com_err(whoami, ret, "closing kerberos database");
cleanup_and_exit(1);
}
}
} else if (ret < 0 && errno != EINTR) {
com_err(whoami, errno, "in select");
cleanup_and_exit(1);
} else if (FD_ISSET(s, &rfds)) {
if (debug)
printf("received packet\n");
if (ret = do_connection(s)) {
com_err(whoami, ret, "handling packet");
}
} else
com_err(whoami, 0, "impossible situation occurred!");
}
return cleanup_and_exit(0);
}
int cleanup_and_exit(int ret)
{
if (use_master) {
krb5_finish_key(&master_encblock);
memset((char *)&master_encblock, 0, sizeof(master_encblock));
(void) krb5_db_fini();
}
exit(ret);
}
void init_keytab()
{
int ret;
if (keytab == NULL) {
if (ret = krb5_kt_default(&kt)) {
com_err(whoami, ret, "while opening default keytab");
cleanup_and_exit(1);
}
} else {
if (ret = krb5_kt_resolve(keytab, &kt)) {
com_err(whoami, ret, "while resolving keytab %s",
keytab);
cleanup_and_exit(1);
}
}
}
void init_master()
{
int ret;
char *realm;
if (ret = krb5_get_default_realm(&realm)) {
com_err(whoami, ret, "getting default realm");
cleanup_and_exit(1);
}
if (ret = krb5_db_setup_mkey_name(NULL, realm, (char **) 0,
&master_princ)) {
com_err(whoami, ret, "while setting up master key name");
cleanup_and_exit(1);
}
#ifdef PROVIDE_DES_CBC_CRC
master_encblock.crypto_entry = &mit_des_cryptosystem_entry;
#else
error(You gotta figure out what cryptosystem to use in the KDC);
#endif
master_keyblock.keytype = KEYTYPE_DES;
if (ret = krb5_db_fetch_mkey(master_princ, &master_encblock,
FALSE, /* non-manual type-in */
FALSE, /* irrelevant, given prev. arg */
0, &master_keyblock)) {
com_err(whoami, ret, "while fetching master key");
cleanup_and_exit(1);
}
if (ret = krb5_db_init()) {
com_err(whoami, ret, "while initializing master database");
cleanup_and_exit(1);
}
if (ret = krb5_process_key(&master_encblock, &master_keyblock)) {
krb5_db_fini();
com_err(whoami, ret, "while processing master key");
cleanup_and_exit(1);
}
}
krb5_error_code do_connection(int s)
{
struct sockaddr saddr;
krb5_ticket *v5tkt;
KTEXT_ST v4tkt;
krb5_keyblock service_key;
krb5_data msgdata, tktdata;
char msgbuf[MSGSIZE], tktbuf[TKT_BUFSIZ], *p;
int n, ret, saddrlen;
msgdata.data = msgbuf;
msgdata.length = MSGSIZE;
saddrlen = sizeof(struct sockaddr);
ret = recvfrom(s, msgdata.data, msgdata.length, 0, &saddr, &saddrlen);
if (ret < 0) {
ret = errno;
goto error;
}
if (debug)
printf("message received\n");
if (ret = decode_krb5_ticket(&msgdata, &v5tkt))
goto error;
if (debug)
printf("V5 ticket decoded\n");
if (ret = lookup_service_key(v5tkt->server, &service_key))
goto error;
if (debug)
printf("service key retrieved\n");
ret = krb524_convert_tkt_skey(v5tkt, &v4tkt, &service_key);
if (ret)
goto error;
krb5_free_keyblock_contents(&service_key);
krb5_free_ticket(v5tkt);
if (debug)
printf("credentials converted\n");
tktdata.data = tktbuf;
tktdata.length = TKT_BUFSIZ;
ret = encode_v4tkt(&v4tkt, tktdata.data, &tktdata.length);
if (ret)
goto error;
if (debug)
printf("v4 credentials encoded\n");
error:
/* create the reply */
p = msgdata.data;
msgdata.length = 0;
n = htonl(ret);
memcpy(p, (char *) &n, sizeof(int));
p += sizeof(int);
msgdata.length += sizeof(int);
if (ret)
goto write_msg;
n = htonl(v5tkt->enc_part.kvno);
memcpy(p, (char *) &n, sizeof(int));
p += sizeof(int);
msgdata.length += sizeof(int);
memcpy(p, tktdata.data, tktdata.length);
p += tktdata.length;
msgdata.length += tktdata.length;
write_msg:
if (ret)
(void) sendto(s, msgdata.data, msgdata.length, 0, &saddr, saddrlen);
else
if (sendto(s, msgdata.data, msgdata.length, 0, &saddr, saddrlen)<0)
ret = errno;
if (debug)
printf("reply written\n");
return ret;
}
krb5_error_code lookup_service_key(krb5_principal p, krb5_keyblock *key)
{
int ret;
krb5_keytab_entry entry;
if (use_keytab) {
if (ret = krb5_kt_get_entry(kt, p, 0, &entry))
return ret;
memcpy(key, (char *) &entry.key, sizeof(krb5_keyblock));
return 0;
} else if (use_master) {
if (ret = krb5_dbm_db_init())
return ret;
return kdc_get_server_key(p, key, NULL);
}
}
/* taken from kdc/kdc_util.c, and modified somewhat */
krb5_error_code kdc_get_server_key(service, key, kvno)
krb5_principal service;
krb5_keyblock *key;
krb5_kvno *kvno;
{
krb5_error_code ret;
int nprincs;
krb5_db_entry server;
krb5_boolean more;
nprincs = 1;
if (ret = krb5_db_get_principal(service, &server, &nprincs, &more))
return(ret);
if (more) {
krb5_db_free_principal(&server, nprincs);
return(KRB5KDC_ERR_PRINCIPAL_NOT_UNIQUE);
} else if (nprincs != 1) {
krb5_db_free_principal(&server, nprincs);
return(KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN);
}
/*
* convert server.key into a real key (it is encrypted in the
* database)
*/
ret = KDB_CONVERT_KEY_OUTOF_DB(&server.key, key);
if (kvno)
*kvno = server.kvno;
krb5_db_free_principal(&server, nprincs);
return ret;
}
|