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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
|
/*
* Copyright (c) 1994 by the University of Southern California
*
* EXPORT OF THIS SOFTWARE from the United States of America may
* require a specific license from the United States Government.
* It is the responsibility of any person or organization contemplating
* export to obtain such a license before exporting.
*
* WITHIN THAT CONSTRAINT, permission to copy, modify, and distribute
* this software and its documentation in source and binary forms is
* hereby granted, provided that any documentation or other materials
* related to such distribution or use acknowledge that the software
* was developed by the University of Southern California.
*
* DISCLAIMER OF WARRANTY. THIS SOFTWARE IS PROVIDED "AS IS". The
* University of Southern California MAKES NO REPRESENTATIONS OR
* WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
* limitation, the University of Southern California MAKES NO
* REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
* PARTICULAR PURPOSE. The University of Southern
* California shall not be held liable for any liability nor for any
* direct, indirect, or consequential damages with respect to any
* claim by the user or distributor of the ksu software.
*
* KSU was writen by: Ari Medvinsky, ari@isi.edu
*/
#include "ksu.h"
static krb5_error_code krb5_verify_tkt_def
(krb5_context,
krb5_principal,
krb5_principal,
krb5_keyblock *,
krb5_data *,
krb5_ticket **);
void plain_dump_principal ();
/*
* Try no preauthentication first; then try the encrypted timestamp
*/
krb5_preauthtype * preauth_ptr = NULL;
krb5_boolean krb5_auth_check(context, client_pname, hostname, options,
target_user, cc, path_passwd, target_uid)
krb5_context context;
krb5_principal client_pname;
char *hostname;
opt_info *options;
char *target_user;
uid_t target_uid;
krb5_ccache cc;
int *path_passwd;
{
krb5_principal client, server;
krb5_creds tgt, tgtq, in_creds, * out_creds;
krb5_creds **tgts = NULL; /* list of ticket granting tickets */
krb5_ticket * target_tkt; /* decrypted ticket for server */
krb5_error_code retval =0;
int got_it = 0;
krb5_boolean zero_password;
*path_passwd = 0;
memset((char *) &tgtq, 0, sizeof(tgtq));
memset((char *) &tgt, 0, sizeof(tgt));
memset((char *) &in_creds, 0, sizeof(krb5_creds));
if ((retval= krb5_copy_principal(context, client_pname, &client))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if (auth_debug) {
dump_principal(context, "krb5_auth_check: Client principal name",
client);
}
if ((retval = krb5_sname_to_principal(context, hostname, NULL,
KRB5_NT_SRV_HST, &server))){
com_err(prog_name, retval,
"while creating server %s principal name", hostname);
krb5_free_principal(context, client);
return (FALSE) ;
}
if (auth_debug) {
dump_principal(context, "krb5_auth_check: Server principal name",
server);
}
/* check if ticket is already in the cache, if it is
then use it.
*/
if( krb5_fast_auth(context, client, server, target_user, cc) == TRUE){
if (auth_debug ){
fprintf (stderr,"Authenticated via fast_auth \n");
}
return TRUE;
}
/* check to see if the local tgt is in the cache */
if ((retval= krb5_copy_principal(context, client, &tgtq.client))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if ((retval = ksu_tgtname(context, krb5_princ_realm(context, client),
krb5_princ_realm(context, client),
&tgtq.server))){
com_err(prog_name, retval, "while creating tgt for local realm");
krb5_free_principal(context, client);
krb5_free_principal(context, server);
return (FALSE) ;
}
if (auth_debug){ dump_principal(context, "local tgt principal name", tgtq.server ); }
retval = krb5_cc_retrieve_cred(context, cc,
KRB5_TC_MATCH_SRV_NAMEONLY | KRB5_TC_SUPPORTED_KTYPES,
&tgtq, &tgt);
if (! retval) retval = krb5_check_exp(context, tgt.times);
if (retval){
if ((retval != KRB5_CC_NOTFOUND) &&
(retval != KRB5KRB_AP_ERR_TKT_EXPIRED)){
com_err(prog_name, retval,
"while retrieving creds from cache");
return (FALSE) ;
}
} else{
got_it = 1;
}
if (! got_it){
#ifdef GET_TGT_VIA_PASSWD
if (krb5_seteuid(0)||krb5_seteuid(target_uid)) {
com_err("ksu", errno, "while switching to target uid");
return FALSE;
}
fprintf(stderr,"WARNING: Your password may be exposed if you enter it here and are logged \n");
fprintf(stderr," in remotely using an unsecure (non-encrypted) channel. \n");
/*get the ticket granting ticket, via passwd(promt for passwd)*/
if (krb5_get_tkt_via_passwd (context, &cc, client, tgtq.server,
options, & zero_password) == FALSE){
krb5_seteuid(0);
return FALSE;
}
*path_passwd = 1;
if (krb5_seteuid(0)) {
com_err("ksu", errno, "while reclaiming root uid");
return FALSE;
}
#else
plain_dump_principal (context, client);
fprintf(stderr,"does not have any appropriate tickets in the cache.\n");
return FALSE;
#endif /* GET_TGT_VIA_PASSWD */
}
if ((retval= krb5_copy_principal(context, client, &in_creds.client))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if ((retval= krb5_copy_principal(context, server, &in_creds.server))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if ((retval = krb5_get_cred_from_kdc(context, cc, &in_creds,
&out_creds, &tgts))){
com_err(prog_name, retval, "while geting credentials from kdc");
return (FALSE);
}
if (auth_debug){
fprintf(stderr,"krb5_auth_check: got ticket for end server \n");
dump_principal(context, "out_creds->server", out_creds->server );
}
if (tgts){
register int i =0;
if (auth_debug){
fprintf(stderr, "krb5_auth_check: went via multiple realms");
}
while (tgts[i]){
if ((retval=krb5_cc_store_cred(context,cc,tgts[i]))) {
com_err(prog_name, retval,
"while storing credentials from cross-realm walk");
return (FALSE);
}
i++;
}
krb5_free_tgt_creds(context, tgts);
}
retval = krb5_verify_tkt_def(context, client, server,
&out_creds->keyblock, &out_creds->ticket,
&target_tkt);
if (retval) {
com_err(prog_name, retval, "while verifying ticket for server");
return (FALSE);
}
if ((retval = krb5_cc_store_cred(context, cc, out_creds))){
com_err(prog_name, retval,
"While storing credentials");
return (FALSE);
}
return (TRUE);
}
/* krb5_fast_auth checks if ticket for the end server is already in
the cache, if it is, we don't need a tgt */
krb5_boolean krb5_fast_auth(context, client, server, target_user, cc)
krb5_context context;
krb5_principal client;
krb5_principal server;
char *target_user;
krb5_ccache cc;
{
krb5_creds tgt, tgtq;
krb5_ticket * target_tkt;
krb5_error_code retval;
memset((char *) &tgtq, 0, sizeof(tgtq));
memset((char *) &tgt, 0, sizeof(tgt));
if ((retval= krb5_copy_principal(context, client, &tgtq.client))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if ((retval= krb5_copy_principal(context, server, &tgtq.server))){
com_err(prog_name, retval,"while copying client principal");
return (FALSE) ;
}
if ((retval = krb5_cc_retrieve_cred(context, cc,
KRB5_TC_MATCH_SRV_NAMEONLY | KRB5_TC_SUPPORTED_KTYPES,
&tgtq, &tgt))){
if (auth_debug)
com_err(prog_name, retval,"While Retrieving credentials");
return (FALSE) ;
}
if ((retval = krb5_verify_tkt_def(context, client, server, &tgt.keyblock,
&tgt.ticket, &target_tkt))){
com_err(prog_name, retval, "while verifing ticket for server");
return (FALSE);
}
return TRUE;
}
static krb5_error_code
krb5_verify_tkt_def(context, client, server, cred_ses_key,
scr_ticket, clear_ticket)
/* IN */
krb5_context context;
krb5_principal client;
krb5_principal server;
krb5_keyblock *cred_ses_key;
krb5_data *scr_ticket;
/* OUT */
krb5_ticket **clear_ticket;
{
krb5_keytab keytabid;
krb5_enctype enctype;
krb5_keytab_entry ktentry;
krb5_keyblock *tkt_key = NULL;
krb5_ticket * tkt = NULL;
krb5_error_code retval =0;
krb5_keyblock * tkt_ses_key;
if ((retval = decode_krb5_ticket(scr_ticket, &tkt))){
return retval;
}
if (auth_debug){
fprintf(stderr,"krb5_verify_tkt_def: verifying target server\n");
dump_principal(context, "server", server);
dump_principal(context, "tkt->server", tkt->server);
}
if (server && !krb5_principal_compare(context, server, tkt->server)){
return KRB5KRB_AP_WRONG_PRINC;
}
/* get the default keytab */
if ((retval = krb5_kt_default(context, &keytabid))){
krb5_free_ticket(context, tkt);
return retval;
}
enctype = tkt->enc_part.enctype;
if ((retval = krb5_kt_get_entry(context, keytabid, server,
tkt->enc_part.kvno, enctype, &ktentry))){
krb5_free_ticket(context, tkt);
return retval;
}
krb5_kt_close(context, keytabid);
if ((retval = krb5_copy_keyblock(context, &ktentry.key, &tkt_key))){
krb5_free_ticket(context, tkt);
krb5_kt_free_entry(context, &ktentry);
return retval;
}
/* decrypt the ticket */
if ((retval = krb5_decrypt_tkt_part(context, tkt_key, tkt))) {
krb5_free_ticket(context, tkt);
krb5_kt_free_entry(context, &ktentry);
krb5_free_keyblock(context, tkt_key);
return(retval);
}
/* Check to make sure ticket hasn't expired */
retval = krb5_check_exp(context, tkt->enc_part2->times);
if (retval) {
if (auth_debug && (retval == KRB5KRB_AP_ERR_TKT_EXPIRED)) {
fprintf(stderr,
"krb5_verify_tkt_def: ticket has expired");
}
krb5_free_ticket(context, tkt);
krb5_kt_free_entry(context, &ktentry);
krb5_free_keyblock(context, tkt_key);
return KRB5KRB_AP_ERR_TKT_EXPIRED;
}
if (!krb5_principal_compare(context, client, tkt->enc_part2->client)) {
krb5_free_ticket(context, tkt);
krb5_kt_free_entry(context, &ktentry);
krb5_free_keyblock(context, tkt_key);
return KRB5KRB_AP_ERR_BADMATCH;
}
if (auth_debug){
fprintf(stderr,
"krb5_verify_tkt_def: verified client's identity\n");
dump_principal(context, "client", client);
dump_principal(context, "tkt->enc_part2->client",tkt->enc_part2->client);
}
tkt_ses_key = tkt->enc_part2->session;
if (cred_ses_key->enctype != tkt_ses_key->enctype ||
cred_ses_key->length != tkt_ses_key->length ||
memcmp((char *)cred_ses_key->contents,
(char *)tkt_ses_key->contents, cred_ses_key->length)) {
krb5_free_ticket(context, tkt);
krb5_kt_free_entry(context, &ktentry);
krb5_free_keyblock(context, tkt_key);
return KRB5KRB_AP_ERR_BAD_INTEGRITY;
}
if (auth_debug){
fprintf(stderr,
"krb5_verify_tkt_def: session keys match \n");
}
*clear_ticket = tkt;
krb5_kt_free_entry(context, &ktentry);
krb5_free_keyblock(context, tkt_key);
return 0;
}
krb5_boolean krb5_get_tkt_via_passwd (context, ccache, client, server,
options, zero_password)
krb5_context context;
krb5_ccache *ccache;
krb5_principal client;
krb5_principal server;
opt_info *options;
krb5_boolean *zero_password;
{
krb5_error_code code;
krb5_creds my_creds;
krb5_timestamp now;
unsigned int pwsize;
char password[255], *client_name, prompt[255];
*zero_password = FALSE;
if ((code = krb5_unparse_name(context, client, &client_name))) {
com_err (prog_name, code, "when unparsing name");
return (FALSE);
}
memset((char *)&my_creds, 0, sizeof(my_creds));
if ((code = krb5_copy_principal(context, client, &my_creds.client))){
com_err (prog_name, code, "while copying principal");
return (FALSE);
}
if ((code = krb5_copy_principal(context, server, &my_creds.server))){
com_err (prog_name, code, "while copying principal");
return (FALSE);
}
if ((code = krb5_timeofday(context, &now))) {
com_err(prog_name, code, "while getting time of day");
return (FALSE);
}
my_creds.times.starttime = 0; /* start timer when request
gets to KDC */
my_creds.times.endtime = now + options->lifetime;
if (options->opt & KDC_OPT_RENEWABLE) {
my_creds.times.renew_till = now + options->rlife;
} else
my_creds.times.renew_till = 0;
if (strlen (client_name) + 80 > sizeof (prompt)) {
fprintf (stderr,
"principal name %s too long for internal buffer space\n",
client_name);
return FALSE;
}
(void) sprintf(prompt,"Kerberos password for %s: ", client_name);
pwsize = sizeof(password);
code = krb5_read_password(context, prompt, 0, password, &pwsize);
if (code ) {
com_err(prog_name, code, "while reading password for '%s'\n",
client_name);
memset(password, 0, sizeof(password));
return (FALSE);
}
if ( pwsize == 0) {
fprintf(stderr, "No password given\n");
*zero_password = TRUE;
memset(password, 0, sizeof(password));
return (FALSE);
}
code = krb5_get_in_tkt_with_password(context, options->opt,
0, NULL, preauth_ptr,
password, *ccache, &my_creds, 0);
memset(password, 0, sizeof(password));
if (code) {
if (code == KRB5KRB_AP_ERR_BAD_INTEGRITY)
fprintf (stderr, "%s: Password incorrect\n", prog_name);
else
com_err (prog_name, code, "while getting initial credentials");
return (FALSE);
}
return (TRUE);
}
void dump_principal (context, str, p)
krb5_context context;
char *str;
krb5_principal p;
{
char * stname;
krb5_error_code retval;
if ((retval = krb5_unparse_name(context, p, &stname))) {
fprintf(stderr, " %s while unparsing name\n", error_message(retval));
}
fprintf(stderr, " %s: %s\n", str, stname);
}
void plain_dump_principal (context, p)
krb5_context context;
krb5_principal p;
{
char * stname;
krb5_error_code retval;
if ((retval = krb5_unparse_name(context, p, &stname)))
fprintf(stderr, " %s while unparsing name\n", error_message(retval));
fprintf(stderr, "%s ", stname);
}
/**********************************************************************
returns the principal that is closest to client. plist contains
a principal list obtained from .k5login and parhaps .k5users file.
This routine gets called before getting the password for a tgt.
A principal is picked that has the best chance of getting in.
**********************************************************************/
krb5_error_code get_best_principal(context, plist, client)
krb5_context context;
char **plist;
krb5_principal *client;
{
krb5_error_code retval =0;
krb5_principal temp_client, best_client = NULL;
int i = 0, nelem;
if (! plist ) return 0;
nelem = krb5_princ_size(context, *client);
while(plist[i]){
if ((retval = krb5_parse_name(context, plist[i], &temp_client))){
return retval;
}
if (data_eq(*krb5_princ_realm(context, *client),
*krb5_princ_realm(context, temp_client))) {
if (nelem &&
krb5_princ_size(context, *client) > 0 &&
krb5_princ_size(context, temp_client) > 0) {
krb5_data *p1 =
krb5_princ_component(context, *client, 0);
krb5_data *p2 =
krb5_princ_component(context, temp_client, 0);
if (data_eq(*p1, *p2)) {
if (auth_debug){
fprintf(stderr,
"get_best_principal: compare with %s\n",
plist[i]);
}
if(best_client){
if(krb5_princ_size(context, best_client) >
krb5_princ_size(context, temp_client)){
best_client = temp_client;
}
}else{
best_client = temp_client;
}
}
}
}
i++;
}
if (best_client) *client = best_client;
return 0;
}
|