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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
|
/*
* 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"
#include "adm_proto.h"
/******************************************************************
krb5_cache_copy
gets rid of any expired tickets in the secondary cache,
copies the default cache into the secondary cache,
************************************************************************/
void show_credential();
/* modifies only the cc_other, the algorithm may look a bit funny,
but I had to do it this way, since remove function did not come
with k5 beta 3 release.
*/
krb5_error_code krb5_ccache_copy (context, cc_def, cc_other_tag,
primary_principal, cc_out, stored, target_uid)
/* IN */
krb5_context context;
krb5_ccache cc_def;
char *cc_other_tag;
krb5_principal primary_principal;
uid_t target_uid;
/* OUT */
krb5_ccache *cc_out;
krb5_boolean *stored;
{
int i=0;
krb5_ccache * cc_other;
char * cc_def_name;
char * cc_other_name;
krb5_error_code retval=0;
krb5_creds ** cc_def_creds_arr = NULL;
krb5_creds ** cc_other_creds_arr = NULL;
struct stat st_temp;
cc_other = (krb5_ccache *) xcalloc(1, sizeof (krb5_ccache));
if ((retval = krb5_cc_resolve(context, cc_other_tag, cc_other))){
com_err (prog_name, retval, "resolving ccache %s",
cc_other_tag);
return retval;
}
cc_def_name = krb5_cc_get_name(context, cc_def);
cc_other_name = krb5_cc_get_name(context, *cc_other);
if ( ! stat(cc_def_name, &st_temp)){
if((retval = krb5_get_nonexp_tkts(context,cc_def,&cc_def_creds_arr))){
return retval;
}
}
*stored = krb5_find_princ_in_cred_list(context, cc_def_creds_arr,
primary_principal);
#ifdef HAVE_LSTAT
if (!lstat( cc_other_name, &st_temp)) {
#else /*HAVE_LSTAT*/
if (!stat( cc_other_name, &st_temp)) {
#endif
return EINVAL;
}
if (krb5_seteuid(0)||krb5_seteuid(target_uid)) {
return errno;
}
if ((retval = krb5_cc_initialize(context, *cc_other, primary_principal))){
return retval;
}
retval = krb5_store_all_creds(context, * cc_other, cc_def_creds_arr,
cc_other_creds_arr);
if (cc_def_creds_arr){
while (cc_def_creds_arr[i]){
krb5_free_creds(context, cc_def_creds_arr[i]);
i++;
}
}
i=0;
if(cc_other_creds_arr){
while (cc_other_creds_arr[i]){
krb5_free_creds(context, cc_other_creds_arr[i]);
i++;
}
}
*cc_out = *cc_other;
return retval;
}
krb5_error_code krb5_store_all_creds(context, cc, creds_def, creds_other)
krb5_context context;
krb5_ccache cc;
krb5_creds **creds_def;
krb5_creds **creds_other;
{
int i = 0;
krb5_error_code retval = 0;
krb5_creds ** temp_creds= NULL;
if ((creds_def == NULL) && (creds_other == NULL))
return 0;
if ((creds_def == NULL) && (creds_other != NULL))
temp_creds = creds_other;
if ((creds_def != NULL) && (creds_other == NULL))
temp_creds = creds_def;
if (temp_creds){
while(temp_creds[i]){
if ((retval= krb5_cc_store_cred(context, cc,
temp_creds[i]))){
return retval;
}
i++;
}
}
else { /* both arrays have elements in them */
return KRB5KRB_ERR_GENERIC;
/************ while(creds_other[i]){
cmp = FALSE;
j = 0;
while(creds_def[j]){
cmp = compare_creds(creds_other[i],creds_def[j]);
if( cmp == TRUE) break;
j++;
}
if (cmp == FALSE){
if (retval= krb5_cc_store_cred(context, cc,
creds_other[i])){
return retval;
}
}
i ++;
}
i=0;
while(creds_def[i]){
if (retval= krb5_cc_store_cred(context, cc,
creds_def[i])){
return retval;
}
i++;
}
**************/
}
return 0;
}
krb5_boolean compare_creds(context, cred1, cred2)
krb5_context context;
krb5_creds *cred1;
krb5_creds *cred2;
{
krb5_boolean retval;
retval = krb5_principal_compare (context, cred1->client, cred2->client);
if (retval == TRUE)
retval = krb5_principal_compare (context, cred1->server, cred2->server);
return retval;
}
krb5_error_code krb5_get_nonexp_tkts(context, cc, creds_array)
krb5_context context;
krb5_ccache cc;
krb5_creds ***creds_array;
{
krb5_creds creds, temp_tktq, temp_tkt;
krb5_creds **temp_creds;
krb5_error_code retval=0;
krb5_cc_cursor cur;
int count = 0;
int chunk_count = 1;
if ( ! ( temp_creds = (krb5_creds **) malloc( CHUNK * sizeof(krb5_creds *)))){
return errno;
}
memset((char *) &temp_tktq, 0, sizeof(temp_tktq));
memset((char *) &temp_tkt, 0, sizeof(temp_tkt));
memset((char *) &creds, 0, sizeof(creds));
/* initialize the cursor */
if ((retval = krb5_cc_start_seq_get(context, cc, &cur))) {
return retval;
}
while (!(retval = krb5_cc_next_cred(context, cc, &cur, &creds))){
if ((retval = krb5_check_exp(context, creds.times))){
if (retval != KRB5KRB_AP_ERR_TKT_EXPIRED){
return retval;
}
if (auth_debug){
fprintf(stderr,"krb5_ccache_copy: CREDS EXPIRED:\n");
fputs(" Valid starting Expires Service principal\n",stdout);
show_credential(context, &creds, cc);
fprintf(stderr,"\n");
}
}
else { /* these credentials didn't expire */
if ((retval = krb5_copy_creds(context, &creds,
&temp_creds[count]))){
return retval;
}
count ++;
if (count == (chunk_count * CHUNK -1)){
chunk_count ++;
if (!(temp_creds = (krb5_creds **) realloc(temp_creds,
chunk_count * CHUNK * sizeof(krb5_creds *)))){
return errno;
}
}
}
}
temp_creds[count] = NULL;
*creds_array = temp_creds;
if (retval == KRB5_CC_END) {
retval = krb5_cc_end_seq_get(context, cc, &cur);
}
return retval;
}
krb5_error_code krb5_check_exp(context, tkt_time)
krb5_context context;
krb5_ticket_times tkt_time;
{
krb5_error_code retval =0;
krb5_timestamp currenttime;
if ((retval = krb5_timeofday (context, ¤ttime))){
return retval;
}
if (auth_debug){
fprintf(stderr,"krb5_check_exp: the krb5_clockskew is %d \n",
context->clockskew);
fprintf(stderr,"krb5_check_exp: currenttime - endtime %d \n",
(currenttime - tkt_time.endtime ));
}
if (currenttime - tkt_time.endtime > context->clockskew){
retval = KRB5KRB_AP_ERR_TKT_EXPIRED ;
return retval;
}
return 0;
}
char *flags_string(cred)
krb5_creds *cred;
{
static char buf[32];
int i = 0;
if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
buf[i++] = 'F';
if (cred->ticket_flags & TKT_FLG_FORWARDED)
buf[i++] = 'f';
if (cred->ticket_flags & TKT_FLG_PROXIABLE)
buf[i++] = 'P';
if (cred->ticket_flags & TKT_FLG_PROXY)
buf[i++] = 'p';
if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
buf[i++] = 'D';
if (cred->ticket_flags & TKT_FLG_POSTDATED)
buf[i++] = 'd';
if (cred->ticket_flags & TKT_FLG_INVALID)
buf[i++] = 'i';
if (cred->ticket_flags & TKT_FLG_RENEWABLE)
buf[i++] = 'R';
if (cred->ticket_flags & TKT_FLG_INITIAL)
buf[i++] = 'I';
if (cred->ticket_flags & TKT_FLG_HW_AUTH)
buf[i++] = 'H';
if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
buf[i++] = 'A';
buf[i] = '\0';
return(buf);
}
void printtime(tv)
time_t tv;
{
struct tm *stime;
char fmtbuf[18];
char fill;
stime = localtime((time_t *)&tv);
fill = ' ';
if (!krb5_timestamp_to_sfstring((krb5_timestamp) tv,
fmtbuf,
sizeof(fmtbuf),
&fill))
printf(fmtbuf);
}
krb5_error_code
krb5_get_login_princ(luser, princ_list)
const char *luser;
char ***princ_list;
{
struct stat sbuf;
struct passwd *pwd;
char pbuf[MAXPATHLEN];
FILE *fp;
char * linebuf;
char *newline;
int gobble;
char ** buf_out;
struct stat st_temp;
int count = 0, chunk_count = 1;
/* no account => no access */
if ((pwd = getpwnam(luser)) == NULL) {
return 0;
}
(void) strcpy(pbuf, pwd->pw_dir);
(void) strcat(pbuf, "/.k5login");
if (stat(pbuf, &st_temp)) { /* not accessible */
return 0;
}
/* open ~/.k5login */
if ((fp = fopen(pbuf, "r")) == NULL) {
return 0;
}
/*
* For security reasons, the .k5login file must be owned either by
* the user himself, or by root. Otherwise, don't grant access.
*/
if (fstat(fileno(fp), &sbuf)) {
fclose(fp);
return 0;
}
if ((sbuf.st_uid != pwd->pw_uid) && sbuf.st_uid) {
fclose(fp);
return 0;
}
/* check each line */
if( !(linebuf = (char *) calloc (BUFSIZ, sizeof(char)))) return errno;
if (!(buf_out = (char **) malloc( CHUNK * sizeof(char *)))) return errno;
while ( fgets(linebuf, BUFSIZ, fp) != NULL) {
/* null-terminate the input string */
linebuf[BUFSIZ-1] = '\0';
newline = NULL;
/* nuke the newline if it exists */
if ((newline = strchr(linebuf, '\n')))
*newline = '\0';
buf_out[count] = linebuf;
count ++;
if (count == (chunk_count * CHUNK -1)){
chunk_count ++;
if (!(buf_out = (char **) realloc(buf_out,
chunk_count * CHUNK * sizeof(char *)))){
return errno;
}
}
/* clean up the rest of the line if necessary */
if (!newline)
while (((gobble = getc(fp)) != EOF) && gobble != '\n');
if( !(linebuf = (char *) calloc (BUFSIZ, sizeof(char)))) return errno;
}
buf_out[count] = NULL;
*princ_list = buf_out;
fclose(fp);
return 0;
}
void
show_credential(context, cred, cc)
krb5_context context;
krb5_creds *cred;
krb5_ccache cc;
{
krb5_error_code retval;
char *name, *sname, *flags;
int first = 1;
krb5_principal princ;
char * defname;
int show_flags =1;
retval = krb5_unparse_name(context, cred->client, &name);
if (retval) {
com_err(prog_name, retval, "while unparsing client name");
return;
}
retval = krb5_unparse_name(context, cred->server, &sname);
if (retval) {
com_err(prog_name, retval, "while unparsing server name");
free(name);
return;
}
if ((retval = krb5_cc_get_principal(context, cc, &princ))) {
com_err(prog_name, retval, "while retrieving principal name");
return;
}
if ((retval = krb5_unparse_name(context, princ, &defname))) {
com_err(prog_name, retval, "while unparsing principal name");
return;
}
if (!cred->times.starttime)
cred->times.starttime = cred->times.authtime;
printtime(cred->times.starttime);
putchar(' '); putchar(' ');
printtime(cred->times.endtime);
putchar(' '); putchar(' ');
printf("%s\n", sname);
if (strcmp(name, defname)) {
printf("\tfor client %s", name);
first = 0;
}
if (cred->times.renew_till) {
if (first)
fputs("\t",stdout);
else
fputs(", ",stdout);
fputs("renew until ", stdout);
printtime(cred->times.renew_till);
}
if (show_flags) {
flags = flags_string(cred);
if (flags && *flags) {
if (first)
fputs("\t",stdout);
else
fputs(", ",stdout);
printf("Flags: %s", flags);
first = 0;
}
}
putchar('\n');
free(name);
free(sname);
}
int gen_sym(){
static int i = 0;
i ++;
return i;
}
krb5_error_code krb5_ccache_overwrite(context, ccs, cct, primary_principal)
krb5_context context;
krb5_ccache ccs;
krb5_ccache cct;
krb5_principal primary_principal;
{
char * cct_name;
char * ccs_name;
krb5_error_code retval=0;
krb5_principal temp_principal;
krb5_creds ** ccs_creds_arr = NULL;
int i=0;
struct stat st_temp;
ccs_name = krb5_cc_get_name(context, ccs);
cct_name = krb5_cc_get_name(context, cct);
if ( ! stat(ccs_name, &st_temp)){
if ((retval = krb5_get_nonexp_tkts(context, ccs, &ccs_creds_arr))){
return retval;
}
}
if ( ! stat(cct_name, &st_temp)){
if ((retval = krb5_cc_get_principal(context, cct, &temp_principal))){
return retval;
}
}else{
temp_principal = primary_principal;
}
if ((retval = krb5_cc_initialize(context, cct, temp_principal))){
return retval;
}
retval = krb5_store_all_creds(context, cct, ccs_creds_arr, NULL);
if (ccs_creds_arr){
while (ccs_creds_arr[i]){
krb5_free_creds(context, ccs_creds_arr[i]);
i++;
}
}
return retval;
}
krb5_error_code krb5_store_some_creds(context, cc, creds_def, creds_other, prst,
stored)
krb5_context context;
krb5_ccache cc;
krb5_creds **creds_def;
krb5_creds **creds_other;
krb5_principal prst;
krb5_boolean *stored;
{
int i = 0;
krb5_error_code retval = 0;
krb5_creds ** temp_creds= NULL;
krb5_boolean temp_stored = FALSE;
if ((creds_def == NULL) && (creds_other == NULL))
return 0;
if ((creds_def == NULL) && (creds_other != NULL))
temp_creds = creds_other;
if ((creds_def != NULL) && (creds_other == NULL))
temp_creds = creds_def;
if (temp_creds){
while(temp_creds[i]){
if (krb5_principal_compare(context,
temp_creds[i]->client,
prst)== TRUE) {
if ((retval = krb5_cc_store_cred(context,
cc,temp_creds[i]))){
return retval;
}
temp_stored = TRUE;
}
i++;
}
}
else { /* both arrays have elements in them */
return KRB5KRB_ERR_GENERIC;
}
*stored = temp_stored;
return 0;
}
/******************************************************************
krb5_cache_copy_restricted
gets rid of any expired tickets in the secondary cache,
copies the default cache into the secondary cache,
only credentials that are for prst are copied.
the algorithm may look a bit funny,
but I had to do it this way, since cc_remove function did not come
with k5 beta 3 release.
************************************************************************/
krb5_error_code krb5_ccache_copy_restricted (context, cc_def, cc_other_tag,
prst, cc_out, stored, target_uid)
krb5_context context;
krb5_ccache cc_def;
char *cc_other_tag;
krb5_principal prst;
uid_t target_uid;
/* OUT */
krb5_ccache *cc_out;
krb5_boolean *stored;
{
int i=0;
krb5_ccache * cc_other;
char * cc_def_name;
char * cc_other_name;
krb5_error_code retval=0;
krb5_creds ** cc_def_creds_arr = NULL;
krb5_creds ** cc_other_creds_arr = NULL;
struct stat st_temp;
cc_other = (krb5_ccache *) xcalloc(1, sizeof (krb5_ccache));
if ((retval = krb5_cc_resolve(context, cc_other_tag, cc_other))){
com_err (prog_name, retval, "resolving ccache %s",
cc_other_tag);
return retval;
}
cc_def_name = krb5_cc_get_name(context, cc_def);
cc_other_name = krb5_cc_get_name(context, *cc_other);
if ( ! stat(cc_def_name, &st_temp)){
if((retval = krb5_get_nonexp_tkts(context,cc_def,&cc_def_creds_arr))){
return retval;
}
}
#ifdef HAVE_LSTAT
if (!lstat( cc_other_name, &st_temp)) {
#else /*HAVE_LSTAT*/
if (!stat( cc_other_name, &st_temp)) {
#endif
return EINVAL;
}
if (krb5_seteuid(0)||krb5_seteuid(target_uid)) {
return errno;
}
if ((retval = krb5_cc_initialize(context, *cc_other, prst))){
return retval;
}
retval = krb5_store_some_creds(context, * cc_other,
cc_def_creds_arr, cc_other_creds_arr, prst, stored);
if (cc_def_creds_arr){
while (cc_def_creds_arr[i]){
krb5_free_creds(context, cc_def_creds_arr[i]);
i++;
}
}
i=0;
if(cc_other_creds_arr){
while (cc_other_creds_arr[i]){
krb5_free_creds(context, cc_other_creds_arr[i]);
i++;
}
}
*cc_out = *cc_other;
return retval;
}
krb5_error_code krb5_ccache_filter (context, cc, prst)
krb5_context context;
krb5_ccache cc;
krb5_principal prst;
{
int i=0;
krb5_error_code retval=0;
krb5_principal temp_principal;
krb5_creds ** cc_creds_arr = NULL;
char * cc_name;
krb5_boolean stored;
struct stat st_temp;
cc_name = krb5_cc_get_name(context, cc);
if ( ! stat(cc_name, &st_temp)){
if (auth_debug) {
fprintf(stderr,"puting cache %s through a filter for -z option\n", cc_name);
}
if ((retval = krb5_get_nonexp_tkts(context, cc, &cc_creds_arr))){
return retval;
}
if ((retval = krb5_cc_get_principal(context, cc, &temp_principal))){
return retval;
}
if ((retval = krb5_cc_initialize(context, cc, temp_principal))){
return retval;
}
if ((retval = krb5_store_some_creds(context, cc, cc_creds_arr,
NULL, prst, &stored))){
return retval;
}
if (cc_creds_arr){
while (cc_creds_arr[i]){
krb5_free_creds(context, cc_creds_arr[i]);
i++;
}
}
}
return 0;
}
krb5_boolean krb5_find_princ_in_cred_list (context, creds_list, princ)
krb5_context context;
krb5_creds **creds_list;
krb5_principal princ;
{
int i = 0;
krb5_boolean temp_stored = FALSE;
if (creds_list){
while(creds_list[i]){
if (krb5_principal_compare(context,
creds_list[i]->client,
princ)== TRUE){
temp_stored = TRUE;
break;
}
i++;
}
}
return temp_stored;
}
krb5_error_code krb5_find_princ_in_cache (context, cc, princ, found)
krb5_context context;
krb5_ccache cc;
krb5_principal princ;
krb5_boolean *found;
{
krb5_error_code retval;
krb5_creds ** creds_list = NULL;
char * cc_name;
struct stat st_temp;
cc_name = krb5_cc_get_name(context, cc);
if ( ! stat(cc_name, &st_temp)){
if ((retval = krb5_get_nonexp_tkts(context, cc, &creds_list))){
return retval;
}
}
*found = krb5_find_princ_in_cred_list(context, creds_list, princ);
return 0;
}
|