xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/mech/inq_context.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * Copyright 1993 by OpenVision Technologies, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software
7  * and its documentation for any purpose is hereby granted without fee,
8  * provided that the above copyright notice appears in all copies and
9  * that both that copyright notice and this permission notice appear in
10  * supporting documentation, and that the name of OpenVision not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. OpenVision makes no
13  * representations about the suitability of this software for any
14  * purpose.  It is provided "as is" without express or implied warranty.
15  *
16  * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
20  * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
21  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24 
25 #include "gssapiP_krb5.h"
26 
27 OM_uint32
28 krb5_gss_inquire_context(minor_status, context_handle, initiator_name,
29 			 acceptor_name, lifetime_rec, mech_type, ret_flags,
30 			 locally_initiated, open)
31      OM_uint32 *minor_status;
32      gss_ctx_id_t context_handle;
33      gss_name_t *initiator_name;
34      gss_name_t *acceptor_name;
35      OM_uint32 *lifetime_rec;
36      gss_OID *mech_type;
37      OM_uint32 *ret_flags;
38      int *locally_initiated;
39      int *open;
40 {
41    krb5_context context;
42    krb5_error_code code;
43    krb5_gss_ctx_id_rec *ctx;
44    krb5_principal init, accept;
45    krb5_timestamp now;
46    krb5_deltat lifetime;
47 
48    if (initiator_name)
49       *initiator_name = (gss_name_t) NULL;
50    if (acceptor_name)
51       *acceptor_name = (gss_name_t) NULL;
52 
53    /* validate the context handle */
54    if (! kg_validate_ctx_id(context_handle)) {
55       *minor_status = (OM_uint32) G_VALIDATE_FAILED;
56       return(GSS_S_NO_CONTEXT);
57    }
58 
59    ctx = (krb5_gss_ctx_id_rec *) context_handle;
60 
61    if (! ctx->established) {
62       *minor_status = KG_CTX_INCOMPLETE;
63       return(GSS_S_NO_CONTEXT);
64    }
65 
66    init = NULL;
67    accept = NULL;
68    context = ctx->k5_context;
69 
70    if ((code = krb5_timeofday(context, &now))) {
71       *minor_status = code;
72       return(GSS_S_FAILURE);
73    }
74 
75    if ((lifetime = ctx->endtime - now) < 0)
76       lifetime = 0;
77 
78    if (initiator_name) {
79       if ((code = krb5_copy_principal(context,
80 				      ctx->initiate?ctx->here:ctx->there,
81 				      &init))) {
82 	 *minor_status = code;
83 	 return(GSS_S_FAILURE);
84       }
85       if (! kg_save_name((gss_name_t) init)) {
86 	 krb5_free_principal(context, init);
87 	 *minor_status = (OM_uint32) G_VALIDATE_FAILED;
88 	 return(GSS_S_FAILURE);
89       }
90    }
91 
92    if (acceptor_name) {
93       if ((code = krb5_copy_principal(context,
94 				      ctx->initiate?ctx->there:ctx->here,
95 				      &accept))) {
96 	 if (init) krb5_free_principal(context, init);
97 	 *minor_status = code;
98 	 return(GSS_S_FAILURE);
99       }
100       if (! kg_save_name((gss_name_t) accept)) {
101 	 krb5_free_principal(context, accept);
102 	 if (init) {
103 	    kg_delete_name((gss_name_t) accept);
104 	    krb5_free_principal(context, init);
105 	 }
106 	 *minor_status = (OM_uint32) G_VALIDATE_FAILED;
107 	 return(GSS_S_FAILURE);
108       }
109    }
110 
111    if (initiator_name)
112       *initiator_name = (gss_name_t) init;
113 
114    if (acceptor_name)
115       *acceptor_name = (gss_name_t) accept;
116 
117    if (lifetime_rec)
118       *lifetime_rec = lifetime;
119 
120    if (mech_type)
121       *mech_type = (gss_OID) ctx->mech_used;
122 
123    if (ret_flags)
124       *ret_flags = ctx->gss_flags;
125 
126    if (locally_initiated)
127       *locally_initiated = ctx->initiate;
128 
129    if (open)
130       *open = ctx->established;
131 
132    *minor_status = 0;
133    return((lifetime == 0)?GSS_S_CONTEXT_EXPIRED:GSS_S_COMPLETE);
134 }
135