aboutsummaryrefslogtreecommitdiff
path: root/src/lib/gssapi/krb5/acquire_cred.c
blob: 2c620b940567fcd6540b2927aca2733c39d66994 (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
/*
 * Copyright 2000 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * 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 use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 */
/*
 * Copyright 1993 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.
 */

/*
 * Copyright (C) 1998 by the FundsXpress, INC.
 * 
 * All rights reserved.
 * 
 * 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 use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of FundsXpress. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  FundsXpress makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include "k5-int.h"
#include "gssapiP_krb5.h"
#ifdef HAVE_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif

static char *krb5_gss_keytab = NULL;

/* Heimdal calls this gsskrb5_register_acceptor_identity. */
OM_uint32 KRB5_CALLCONV
krb5_gss_register_acceptor_identity(const char *keytab)
{
    size_t	len;

    if (keytab == NULL)
	return GSS_S_FAILURE;
    if (krb5_gss_keytab != NULL)
	free(krb5_gss_keytab);

    len = strlen(keytab);
    krb5_gss_keytab = malloc(len);
    if (krb5_gss_keytab == NULL)
	return GSS_S_FAILURE;

    strcpy(krb5_gss_keytab, keytab);

    return GSS_S_COMPLETE;
}

/* get credentials corresponding to a key in the krb5 keytab.
   If the default name is requested, return the name in output_princ.
     If output_princ is non-NULL, the caller will use or free it, regardless
     of the return value.
   If successful, set the keytab-specific fields in cred
   */

static OM_uint32 
acquire_accept_cred(context, minor_status, desired_name, output_princ, cred)
     krb5_context context;
     OM_uint32 *minor_status;
     gss_name_t desired_name;
     krb5_principal *output_princ;
     krb5_gss_cred_id_rec *cred;
{
   krb5_error_code code;
   krb5_principal princ;
   krb5_keytab kt;
   krb5_keytab_entry entry;

   *output_princ = NULL;
   cred->keytab = NULL;

   /* open the default keytab */

   if (krb5_gss_keytab != NULL)
      code = krb5_kt_resolve(context, krb5_gss_keytab, &kt);
   else
      code = krb5_kt_default(context, &kt);

   if (code) {
      *minor_status = code;
      return(GSS_S_CRED_UNAVAIL);
   }

   if (desired_name != GSS_C_NO_NAME) {
      princ = (krb5_principal) desired_name;
      if ((code = krb5_kt_get_entry(context, kt, princ, 0, 0, &entry))) {
	 (void) krb5_kt_close(context, kt);
	 if (code == KRB5_KT_NOTFOUND)
	    *minor_status = KG_KEYTAB_NOMATCH;
	 else
	    *minor_status = code;
	 return(GSS_S_CRED_UNAVAIL);
      }
      krb5_kt_free_entry(context, &entry);

      /* Open the replay cache for this principal. */
      if ((code = krb5_get_server_rcache(context,
					 krb5_princ_component(context, princ, 0),
					 &cred->rcache))) {
	 *minor_status = code;
	 return(GSS_S_FAILURE);
      }

   }

/* hooray.  we made it */

   cred->keytab = kt;

   return(GSS_S_COMPLETE);
}

/* get credentials corresponding to the default credential cache.
   If the default name is requested, return the name in output_princ.
     If output_princ is non-NULL, the caller will use or free it, regardless
     of the return value.
   If successful, set the ccache-specific fields in cred.
   */

static OM_uint32 
acquire_init_cred(context, minor_status, desired_name, output_princ, cred)
     krb5_context context;
     OM_uint32 *minor_status;
     gss_name_t desired_name;
     krb5_principal *output_princ;
     krb5_gss_cred_id_rec *cred;
{
   krb5_error_code code;
   krb5_ccache ccache;
   krb5_principal princ, tmp_princ;
   krb5_flags flags;
   krb5_cc_cursor cur;
   krb5_creds creds;
   int got_endtime;

   cred->ccache = NULL;

   /* open the default credential cache */

   if ((code = krb5int_cc_default(context, &ccache))) {
      *minor_status = code;
      return(GSS_S_CRED_UNAVAIL);
   }

   /* turn off OPENCLOSE mode while extensive frobbing is going on */

   flags = 0;		/* turns off OPENCLOSE mode */
   if ((code = krb5_cc_set_flags(context, ccache, flags))) {
      *minor_status = code;
      return(GSS_S_CRED_UNAVAIL);
   }

   /* get out the principal name and see if it matches */

   if ((code = krb5_cc_get_principal(context, ccache, &princ))) {
      (void)krb5_cc_close(context, ccache);
      *minor_status = code;
      return(GSS_S_FAILURE);
   }

   if (desired_name != (gss_name_t) NULL) {
      if (! krb5_principal_compare(context, princ, (krb5_principal) desired_name)) {
	 (void)krb5_free_principal(context, princ);
	 (void)krb5_cc_close(context, ccache);
	 *minor_status = KG_CCACHE_NOMATCH;
	 return(GSS_S_CRED_UNAVAIL);
      }
      (void)krb5_free_principal(context, princ);
      princ = (krb5_principal) desired_name;
   } else {
      *output_princ = princ;
   }

   /* iterate over the ccache, find the tgt */

   if ((code = krb5_cc_start_seq_get(context, ccache, &cur))) {
      (void)krb5_cc_close(context, ccache);
      *minor_status = code;
      return(GSS_S_FAILURE);
   }

   /* this is hairy.  If there's a tgt for the principal's local realm
      in here, that's what we want for the expire time.  But if
      there's not, then we want to use the first key.  */

   got_endtime = 0;

   code = krb5_build_principal_ext(context, &tmp_princ,
				   krb5_princ_realm(context, princ)->length,
				   krb5_princ_realm(context, princ)->data,
				   6, "krbtgt",
				   krb5_princ_realm(context, princ)->length,
				   krb5_princ_realm(context, princ)->data,
				   0);
   if (code) {
      (void)krb5_cc_close(context, ccache);
      *minor_status = code;
      return(GSS_S_FAILURE);
   }
   while (!(code = krb5_cc_next_cred(context, ccache, &cur, &creds))) {
      if (krb5_principal_compare(context, tmp_princ, creds.server)) {
	 cred->tgt_expire = creds.times.endtime;
	 got_endtime = 1;
	 *minor_status = 0;
	 code = 0;
	 krb5_free_cred_contents(context, &creds);
	 break;
      }
      if (got_endtime == 0) {
	 cred->tgt_expire = creds.times.endtime;
	 got_endtime = 1;
      }
      krb5_free_cred_contents(context, &creds);
   }
   krb5_free_principal(context, tmp_princ);

   if (code && code != KRB5_CC_END) {
      /* this means some error occurred reading the ccache */
      (void)krb5_cc_end_seq_get(context, ccache, &cur);
      (void)krb5_cc_close(context, ccache);
      *minor_status = code;
      return(GSS_S_FAILURE);
   } else if (! got_endtime) {
      /* this means the ccache was entirely empty */
      (void)krb5_cc_end_seq_get(context, ccache, &cur);
      (void)krb5_cc_close(context, ccache);
      *minor_status = KG_EMPTY_CCACHE;
      return(GSS_S_FAILURE);
   } else {
      /* this means that we found an endtime to use. */
      if ((code = krb5_cc_end_seq_get(context, ccache, &cur))) {
	 (void)krb5_cc_close(context, ccache);
	 *minor_status = code;
	 return(GSS_S_FAILURE);
      }
      flags = KRB5_TC_OPENCLOSE;	/* turns on OPENCLOSE mode */
      if ((code = krb5_cc_set_flags(context, ccache, flags))) {
	 (void)krb5_cc_close(context, ccache);
	 *minor_status = code;
	 return(GSS_S_FAILURE);
      }
   }

   /* the credentials match and are valid */

   cred->ccache = ccache;
   /* minor_status is set while we are iterating over the ccache */
   return(GSS_S_COMPLETE);
}
   
/*ARGSUSED*/
OM_uint32
krb5_gss_acquire_cred(minor_status, desired_name, time_req,
		      desired_mechs, cred_usage, output_cred_handle,
		      actual_mechs, time_rec)
     OM_uint32 *minor_status;
     gss_name_t desired_name;
     OM_uint32 time_req;
     gss_OID_set desired_mechs;
     gss_cred_usage_t cred_usage;
     gss_cred_id_t *output_cred_handle;
     gss_OID_set *actual_mechs;
     OM_uint32 *time_rec;
{
   krb5_context context;
   size_t i;
   krb5_gss_cred_id_t cred;
   gss_OID_set ret_mechs;
   int req_old, req_new;
   OM_uint32 ret;
   krb5_error_code code;

   if (GSS_ERROR(kg_get_context(minor_status, &context)))
      return(GSS_S_FAILURE);

   /* make sure all outputs are valid */

   *output_cred_handle = NULL;
   if (actual_mechs)
      *actual_mechs = NULL;
   if (time_rec)
      *time_rec = 0;

   /* validate the name */

   /*SUPPRESS 29*/
   if ((desired_name != (gss_name_t) NULL) &&
       (! kg_validate_name(desired_name))) {
      *minor_status = (OM_uint32) G_VALIDATE_FAILED;
      return(GSS_S_CALL_BAD_STRUCTURE|GSS_S_BAD_NAME);
   }

   /* verify that the requested mechanism set is the default, or
      contains krb5 */

   if (desired_mechs == GSS_C_NULL_OID_SET) {
      req_old = 1;
      req_new = 1;
   } else {
      req_old = 0;
      req_new = 0;

      for (i=0; i<desired_mechs->count; i++) {
	 if (g_OID_equal(gss_mech_krb5_old, &(desired_mechs->elements[i])))
	    req_old++;
	 if (g_OID_equal(gss_mech_krb5, &(desired_mechs->elements[i])))
	    req_new++;
      }

      if (!req_old && !req_new) {
	 *minor_status = 0;
	 return(GSS_S_BAD_MECH);
      }
   }

   /* create the gss cred structure */

   if ((cred =
	(krb5_gss_cred_id_t) xmalloc(sizeof(krb5_gss_cred_id_rec))) == NULL) {
      *minor_status = ENOMEM;
      return(GSS_S_FAILURE);
   }
   memset(cred, 0, sizeof(krb5_gss_cred_id_rec));

   cred->usage = cred_usage;
   cred->princ = NULL;
   cred->prerfc_mech = req_old;
   cred->rfc_mech = req_new;

   cred->keytab = NULL;
   cred->ccache = NULL;

   if ((cred_usage != GSS_C_INITIATE) &&
       (cred_usage != GSS_C_ACCEPT) &&
       (cred_usage != GSS_C_BOTH)) {
      xfree(cred);
      *minor_status = (OM_uint32) G_BAD_USAGE;
      return(GSS_S_FAILURE);
   }

   /* if requested, acquire credentials for accepting */
   /* this will fill in cred->princ if the desired_name is not specified */

   if ((cred_usage == GSS_C_ACCEPT) ||
       (cred_usage == GSS_C_BOTH))
      if ((ret = acquire_accept_cred(context, minor_status, desired_name,
				     &(cred->princ), cred))
	  != GSS_S_COMPLETE) {
	 if (cred->princ)
	    krb5_free_principal(context, cred->princ);
	 xfree(cred);
	 /* minor_status set by acquire_accept_cred() */
	 return(ret);
      }

   /* if requested, acquire credentials for initiation */
   /* this will fill in cred->princ if it wasn't set above, and
      the desired_name is not specified */

   if ((cred_usage == GSS_C_INITIATE) ||
       (cred_usage == GSS_C_BOTH))
      if ((ret =
	   acquire_init_cred(context, minor_status,
			     cred->princ?(gss_name_t)cred->princ:desired_name,
			     &(cred->princ), cred))
	  != GSS_S_COMPLETE) {
	 if (cred->keytab)
	    krb5_kt_close(context, cred->keytab);
	 if (cred->princ)
	    krb5_free_principal(context, cred->princ);
	 xfree(cred);
	 /* minor_status set by acquire_init_cred() */
	 return(ret);
      }

   /* if the princ wasn't filled in already, fill it in now */

   if (!cred->princ && (desired_name != GSS_C_NO_CREDENTIAL))
      if ((code = krb5_copy_principal(context, (krb5_principal) desired_name,
				      &(cred->princ)))) {
	 if (cred->ccache)
	    (void)krb5_cc_close(context, cred->ccache);
	 if (cred->keytab)
	    (void)krb5_kt_close(context, cred->keytab);
	 xfree(cred);
	 *minor_status = code;
	 return(GSS_S_FAILURE);
      }

   /*** at this point, the cred structure has been completely created */

   /* compute time_rec */

   if (cred_usage == GSS_C_ACCEPT) {
      if (time_rec)
	 *time_rec = GSS_C_INDEFINITE;
   } else {
      krb5_timestamp now;

      if ((code = krb5_timeofday(context, &now))) {
	 if (cred->ccache)
	    (void)krb5_cc_close(context, cred->ccache);
	 if (cred->keytab)
	    (void)krb5_kt_close(context, cred->keytab);
	 if (cred->princ)
	    krb5_free_principal(context, cred->princ);
	 xfree(cred);
	 *minor_status = code;
	 return(GSS_S_FAILURE);
      }

      if (time_rec)
	 *time_rec = (cred->tgt_expire > now) ? (cred->tgt_expire - now) : 0;
   }

   /* create mechs */

   if (actual_mechs) {
       if (GSS_ERROR(ret = generic_gss_create_empty_oid_set(minor_status,
							    &ret_mechs)) ||
	   (cred->prerfc_mech &&
	    GSS_ERROR(ret = generic_gss_add_oid_set_member(minor_status,
							   (gss_OID) gss_mech_krb5_old,
							   &ret_mechs))) ||
	   (cred->rfc_mech &&
	    GSS_ERROR(ret = generic_gss_add_oid_set_member(minor_status,
							   (gss_OID) gss_mech_krb5,
							   &ret_mechs)))) {
	   if (cred->ccache)
	       (void)krb5_cc_close(context, cred->ccache);
	   if (cred->keytab)
	       (void)krb5_kt_close(context, cred->keytab);
	   if (cred->princ)
	       krb5_free_principal(context, cred->princ);
	   xfree(cred);
	   /* *minor_status set above */
	   return(ret);
       }
   }

   /* intern the credential handle */

   if (! kg_save_cred_id((gss_cred_id_t) cred)) {
      free(ret_mechs->elements);
      free(ret_mechs);
      if (cred->ccache)
	 (void)krb5_cc_close(context, cred->ccache);
      if (cred->keytab)
	 (void)krb5_kt_close(context, cred->keytab);
      if (cred->princ)
	 krb5_free_principal(context, cred->princ);
      xfree(cred);
      *minor_status = (OM_uint32) G_VALIDATE_FAILED;
      return(GSS_S_FAILURE);
   }

   /* return success */

   *minor_status = 0;
   *output_cred_handle = (gss_cred_id_t) cred;
   if (actual_mechs)
      *actual_mechs = ret_mechs;

   return(GSS_S_COMPLETE);
}