summaryrefslogtreecommitdiffstats
path: root/src/clients/ksu/heuristic.c
blob: 80482bebbca9ec7031b1ea225ad1aad1ff106b68 (plain)
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
/*
 * 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"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif


/*******************************************************************
get_all_princ_from_file - retrieves all principal names
			from file pointed to by fp.

*******************************************************************/
static void close_time (int, FILE *, int, FILE *);
static krb5_boolean find_str_in_list (char **, char *);

krb5_error_code get_all_princ_from_file (fp, plist)
    FILE *fp;
    char ***plist;
{

    krb5_error_code retval;
    char * line, * fprinc, * lp, ** temp_list = NULL;
    int count = 0, chunk_count = 1;

    if (!(temp_list = (char **) malloc( CHUNK * sizeof(char *))))
	return ENOMEM;

    retval = get_line(fp, &line);
    if (retval)
	return retval;	

    while (line){
	fprinc = get_first_token (line, &lp);
		
	if (fprinc ){
	    temp_list[count] = xstrdup(fprinc);
	    count ++;
	}

	if(count == (chunk_count * CHUNK -1)){
	    chunk_count ++;
	    if (!(temp_list = (char **) realloc(temp_list,
						chunk_count * CHUNK * sizeof(char *)))){
		return ENOMEM;
	    }
	}


	free (line);
	retval = get_line(fp, &line);
	if (retval)
	    return retval;
    }

    temp_list[count] = NULL;

    *plist = temp_list;
    return 0;
}

/*************************************************************
list_union - combines list1 and list2 into combined_list.
	     the  space for list1 and list2 is either freed
	     or used by combined_list.
**************************************************************/

krb5_error_code list_union(list1, list2, combined_list)
    char **list1;
    char **list2;
    char ***combined_list;
{

    unsigned int c1 =0, c2 = 0, i=0, j=0;
    char ** tlist;

    if (! list1){   	
	*combined_list = list2;   	
	return 0;
    }

    if (! list2){   	
	*combined_list = list1;   	
	return 0;
    }

    while (list1[c1]) c1++;
    while (list2[c2]) c2++;
	
    if (!(tlist = (char **) calloc( c1 + c2 + 1, sizeof ( char *))))
	return ENOMEM;

    i = 0;
    while(list1[i]) {
	tlist[i] = list1[i];
	i++;
    }
    j = 0;
    while(list2[j]){
	if(find_str_in_list(list1, list2[j])==FALSE){   	
	    tlist[i] = list2[j];
	    i++;
	}
	j++;
    }

    free (list1); 	
    free (list2); 	
	
    tlist[i]= NULL;

    *combined_list = tlist;
    return 0;
}

krb5_error_code
filter(fp, cmd, k5users_list, k5users_filt_list)
    FILE *fp;
    char *cmd;
    char **k5users_list;
    char ***k5users_filt_list;
{

    krb5_error_code retval =0;
    krb5_boolean found = FALSE;
    char * out_cmd = NULL;
    unsigned int i=0, j=0, found_count = 0, k=0;	
    char ** temp_filt_list;

    *k5users_filt_list = NULL;

    if (! k5users_list){		
	return 0;
    }

    while(k5users_list[i]){	

	retval= k5users_lookup(fp, k5users_list[i], cmd, &found, &out_cmd);
	if (retval)
	    return retval;

	if (found == FALSE){ 		
	    free (k5users_list[i]);
	    k5users_list[i] = NULL;
	    if (out_cmd) gb_err = out_cmd;
	} else
	    found_count ++;	

	i++;
    }

    if (! (temp_filt_list = (char **) calloc(found_count +1, sizeof (char*))))
	return ENOMEM;

    for(j= 0, k=0; j < i; j++ ) {	
	if (k5users_list[j]){
	    temp_filt_list[k] = k5users_list[j]; 		
	    k++;
	}
    }

    temp_filt_list[k] = NULL;

    free (k5users_list);	

    *k5users_filt_list = temp_filt_list;
    return 0;
}

krb5_error_code
get_authorized_princ_names(luser, cmd, princ_list)
    const char *luser;
    char *cmd;
    char ***princ_list;
{

    struct passwd *pwd;
    int k5login_flag =0;
    int k5users_flag =0;
    FILE * login_fp = NULL , * users_fp = NULL;
    char **  k5login_list = NULL, ** k5users_list = NULL;
    char ** k5users_filt_list = NULL;
    char ** combined_list = NULL;
    struct stat tb;
    krb5_error_code retval;	

    *princ_list = NULL; 	

    /* no account => no access */

    if ((pwd = getpwnam(luser)) == NULL)
	return 0;

    k5login_flag = stat(k5login_path, &tb);
    k5users_flag = stat(k5users_path, &tb);

    if (!k5login_flag){
        if ((login_fp = fopen(k5login_path, "r")) == NULL)
	    return 0;
        if ( fowner(login_fp, pwd->pw_uid) == FALSE){
	    close_time(1 /*k5users_flag*/, (FILE *) 0 /*users_fp*/, 
		       k5login_flag,login_fp);
	    return 0;
        }
    }
    if (!k5users_flag){
        if ((users_fp = fopen(k5users_path, "r")) == NULL)
	    return 0;

        if ( fowner(users_fp, pwd->pw_uid) == FALSE){
	    close_time(k5users_flag,users_fp, k5login_flag,login_fp);
	    return 0;
        }

	retval = get_all_princ_from_file (users_fp, &k5users_list);
   	if(retval) {
	    close_time(k5users_flag,users_fp, k5login_flag,login_fp);
	    return retval;
   	}

	rewind(users_fp);	
	
	retval = filter(users_fp,cmd, k5users_list, &k5users_filt_list);
	if(retval) {
	    close_time(k5users_flag,users_fp, k5login_flag, login_fp);
	    return retval;
	}
    }
 	
    if (!k5login_flag){
	retval = get_all_princ_from_file (login_fp, &k5login_list);
   	if(retval) {
	    close_time(k5users_flag,users_fp, k5login_flag,login_fp);
	    return retval;
   	}
    }	

    close_time(k5users_flag,users_fp, k5login_flag, login_fp);

    if (cmd) {
      retval = list_union(k5login_list, k5users_filt_list, &combined_list);
      if (retval){
	  close_time(k5users_flag,users_fp, k5login_flag,login_fp);
	  return retval;
      }
      *princ_list = combined_list;
      return 0;
    } else {
	if (k5users_filt_list != NULL)
	    free(k5users_filt_list);
	*princ_list = k5login_list;
	return 0;
    }
}

static void close_time(k5users_flag, users_fp, k5login_flag, login_fp)
    int k5users_flag;
    FILE *users_fp;
    int k5login_flag;
    FILE *login_fp;
{

    if (!k5users_flag) fclose(users_fp);
    if (!k5login_flag) fclose(login_fp);

}

static krb5_boolean find_str_in_list(list , elm)
    char **list;
    char *elm;
{

    int i=0;
    krb5_boolean found = FALSE;

    if (!list) return found;

    while (list[i] ){
        if (!strcmp(list[i], elm)){
	    found = TRUE;
	    break;
        }
        i++;
    }

    return found;
}

/**********************************************************************
returns the principal that is closes to client (can be the the client
himself). plist contains
a principal list obtained from .k5login and .k5users file.
A principal is picked that has the best chance of getting in.

**********************************************************************/


krb5_error_code get_closest_principal(context, plist, client, found)
    krb5_context context;
    char **plist;
    krb5_principal *client;
    krb5_boolean *found;
{
    krb5_error_code retval =0;
    krb5_principal temp_client, best_client = NULL;
    int i = 0, j=0, cnelem, pnelem;
    krb5_boolean got_one;
	
    *found = FALSE;

    if (! plist ) return 0;

    cnelem = krb5_princ_size(context, *client);

    while(plist[i]){

	retval = krb5_parse_name(context, plist[i], &temp_client);
	if (retval)
	    return retval;
	
	pnelem = krb5_princ_size(context, temp_client);
	
	if ( cnelem > pnelem){	
	    i++;
	    continue;
	}
	
	if (data_eq(*krb5_princ_realm(context, *client),
		    *krb5_princ_realm(context, temp_client))) {
	    
	    got_one = TRUE;
	    for(j =0; j < cnelem; j ++){
		krb5_data *p1 =
		    krb5_princ_component(context, *client, j);
		krb5_data *p2 =
		    krb5_princ_component(context, temp_client, j);
		
		if (!p1 || !p2 || !data_eq(*p1, *p2)) {
		    got_one = FALSE;
		    break;
		}
	    }
	    if (got_one == TRUE){		
		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) {
	*found = TRUE;
	*client = best_client;
    }

    return 0;
}

/****************************************************************
find_either_ticket checks to see whether there is a ticket for the
   end server or tgt, if neither is there the return FALSE,
*****************************************************************/

krb5_error_code find_either_ticket (context, cc, client, end_server, found)
    krb5_context context;
    krb5_ccache cc;
    krb5_principal client;
    krb5_principal end_server;
    krb5_boolean *found;
{

    krb5_principal kdc_server;
    krb5_error_code retval;
    krb5_boolean temp_found = FALSE;
    const char * cc_source_name;
    struct stat st_temp;

    cc_source_name = krb5_cc_get_name(context, cc);

    if ( ! stat(cc_source_name, &st_temp)){

	retval = find_ticket(context, cc, client, end_server, &temp_found);
	if (retval)
	    return retval;
 	
	if (temp_found == FALSE){
	    retval = ksu_tgtname(context,
				  krb5_princ_realm(context, client),
				  krb5_princ_realm(context, client),
				  &kdc_server);
	    if (retval)
		return retval;

	    retval = find_ticket(context, cc,client, kdc_server, &temp_found);
	    if(retval)
		return retval;
	}
	else if (auth_debug)
	    printf("find_either_ticket: found end server ticket\n");
    }

    *found = temp_found;

    return 0;
}


krb5_error_code find_ticket (context, cc, client, server, found)
    krb5_context context;
    krb5_ccache cc;
    krb5_principal client;
    krb5_principal server;
    krb5_boolean *found;
{

    krb5_creds tgt, tgtq;
    krb5_error_code retval;
	
    *found = FALSE;

    memset((char *) &tgtq, 0, sizeof(tgtq));
    memset((char *) &tgt, 0, sizeof(tgt));

    retval= krb5_copy_principal(context,  client, &tgtq.client);
    if (retval)
	return retval; 	

    retval= krb5_copy_principal(context,  server, &tgtq.server);
    if (retval)
	return retval ; 	

    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)){
	    return retval ;
	}
    } else{
	*found = TRUE;
	return 0;	
    }

    free(tgtq.server);
    free(tgtq.client);

    return 0;
}



krb5_error_code find_princ_in_list (context, princ, plist, found)
    krb5_context context;
    krb5_principal princ;
    char **plist;
    krb5_boolean *found;
{

    int i=0;
    char * princname;
    krb5_error_code retval;

    *found = FALSE;

    if (!plist) return 0;

    retval = krb5_unparse_name(context, princ, &princname);
    if (retval)
	return retval;

    while (plist[i] ){
	if (!strcmp(plist[i], princname)){
	    *found = TRUE;
	    break;
	}
	i++;
    }

    return 0;

}

typedef struct princ_info {
	krb5_principal p;
	krb5_boolean found;
}princ_info;

/**********************************************************************
get_best_princ_for_target -

sets the client name, path_out gets set, if authorization is not possible
path_out gets set to ...

***********************************************************************/

krb5_error_code get_best_princ_for_target(context, source_uid, target_uid,
					  source_user, target_user,
					  cc_source, options, cmd,
					  hostname, client, path_out)
    krb5_context context;
    uid_t source_uid;
    uid_t target_uid;
    char *source_user;
    char *target_user;
    krb5_ccache cc_source;
    opt_info *options;
    char *cmd;
    char *hostname;
    krb5_principal *client;
    int *path_out;
{

    princ_info princ_trials[10];
    const char * cc_source_name;
    krb5_principal cc_def_princ = NULL;
    krb5_principal temp_client;
    krb5_principal target_client;
    krb5_principal source_client;
    krb5_principal end_server;
    krb5_error_code retval;
    char ** aplist =NULL;
    krb5_boolean found = FALSE;
    struct stat tb;
    int count =0;
    int i;
    struct stat st_temp;

    *path_out = 0;

    /* -n option was specified client is set we are done */
    if (options->princ)
	return 0;

    cc_source_name = krb5_cc_get_name(context, cc_source);

	
    if (! stat(cc_source_name, &st_temp)) {
	retval = krb5_cc_get_principal(context, cc_source, &cc_def_princ);
	if (retval)
	    return retval;
    }

    retval=krb5_parse_name(context, target_user, &target_client);
    if (retval)
	return retval;

    retval=krb5_parse_name(context, source_user, &source_client);
    if (retval)
	return retval;

    if (source_uid == 0){
	if (target_uid != 0)
	    *client = target_client; /* this will be used to restrict
					the cache copty */   	
	else {
	    if(cc_def_princ)
		*client = cc_def_princ;
	    else
		*client = target_client;
	}

	if (auth_debug)
	    printf(" GET_best_princ_for_target: via source_uid == 0\n");

	return 0;
    }

    /* from here on, the code is for source_uid !=  0 */

    if (source_uid && (source_uid == target_uid)){
	if(cc_def_princ)
	    *client = cc_def_princ;
	else
	    *client = target_client;
	if (auth_debug)
	    printf("GET_best_princ_for_target: via source_uid == target_uid\n");
	return 0;
    }

    /* Become root, then target for looking at .k5login.*/
    if (krb5_seteuid(0) || krb5_seteuid(target_uid) ) {
      return errno;
    }
    
    /* if .k5users and .k5login do not exist */  	
    if (stat(k5login_path, &tb) && stat(k5users_path, &tb) ){
	*client = target_client;

	if (cmd)
	    *path_out = NOT_AUTHORIZED;

	if (auth_debug)
	    printf(" GET_best_princ_for_target: via no auth files path\n");

	return 0;	
    }else{
	retval = get_authorized_princ_names(target_user, cmd, &aplist);
	if (retval)
	    return retval;

	/* .k5users or .k5login exist, but no authorization */
	if ((!aplist) || (!aplist[0])) {
	    *path_out = NOT_AUTHORIZED;
	    if (auth_debug)
		printf("GET_best_princ_for_target: via empty auth files path\n");
	    return 0;	
	}
    }

    retval = krb5_sname_to_principal(context, hostname, NULL,
				     KRB5_NT_SRV_HST, &end_server);
    if (retval)
	return retval;


    /* first see if default principal of the source cache
     * can get us in, then the target_user@realm, then the
     * source_user@realm. If all of them fail, try any
     * other ticket in the cache. */

    if (cc_def_princ)
	princ_trials[count ++].p = cc_def_princ;
    else
	princ_trials[count ++].p = NULL;

    princ_trials[count ++].p = target_client;
    princ_trials[count ++].p = source_client;

    for (i= 0; i < count; i ++)
	princ_trials[i].found = FALSE;

    for (i= 0; i < count; i ++){
	if(princ_trials[i].p) {	
	    retval= find_princ_in_list(context, princ_trials[i].p, aplist, 
				       &found);
	    if (retval)
		return retval;	
	
	    if (found == TRUE){
		princ_trials[i].found = TRUE;

		retval = find_either_ticket (context, cc_source, 
					     princ_trials[i].p,
					     end_server, &found);
 		if (retval)
		    return retval;
		if (found == TRUE){
		    *client = princ_trials[i].p;
		    if (auth_debug)
			printf("GET_best_princ_for_target: via ticket file, choice #%d\n", i);
		    return 0;	
		}
	    }	
	}
    }

    /* out of preferred principals, see if there is any ticket that will
       get us in */

    i=0;
    while (aplist[i]){
	retval = krb5_parse_name(context, aplist[i], &temp_client);
 	if (retval)
	    return retval;

	retval = find_either_ticket (context, cc_source, temp_client,
				     end_server, &found);
	if (retval)
	    return retval;

	if (found == TRUE){
	    if (auth_debug)
		printf("GET_best_princ_for_target: via ticket file, choice: any ok ticket \n" );
	    *client = temp_client;
	    return 0;	
	}

	krb5_free_principal(context, temp_client);

	i++;
    }

    /* no tickets qualified, select a principal, that may be used
       for password promting */


    for (i=0; i < count; i ++){
	if (princ_trials[i].found == TRUE){
	    *client = princ_trials[i].p;

	    if (auth_debug)
		printf("GET_best_princ_for_target: via prompt passwd list choice #%d \n",i);
	    return  0;	
	}
    }

#ifdef PRINC_LOOK_AHEAD
    for (i=0; i < count; i ++){
	if (princ_trials[i].p){
	    retval=krb5_copy_principal(context, princ_trials[i].p,
				       &temp_client);
	    if(retval)
		return retval; 	

	    /* get the client name that is the closest
	       to the three princ in trials */

	    retval=get_closest_principal(context, aplist, &temp_client, 
					 &found);
	    if(retval)
		return retval; 	

	    if (found == TRUE){
		*client = temp_client; 	
		if (auth_debug)
		    printf("GET_best_princ_for_target: via prompt passwd list choice: approximation of princ in trials # %d \n",i);
		return 0;
	    }
	    krb5_free_principal(context, temp_client);
	}
    }

#endif /* PRINC_LOOK_AHEAD */


    if(auth_debug)
	printf( "GET_best_princ_for_target: out of luck, can't get appropriate default principal\n");

    *path_out = NOT_AUTHORIZED;
    return 0;
}