xref: /freebsd/crypto/krb5/src/lib/gssapi/mechglue/g_inq_context.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 /* #pragma ident	"@(#)g_inquire_context.c	1.15	04/02/23 SMI" */
2 
3 /*
4  * Copyright 1996 by Sun Microsystems, 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 Sun Microsystems not be used
11  * in advertising or publicity pertaining to distribution of the software
12  * without specific, written prior permission. Sun Microsystems 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  * SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL SUN MICROSYSTEMS 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 /*
26  *  glue routine for gss_inquire_context
27  */
28 
29 #include "mglueP.h"
30 #ifdef HAVE_STDLIB_H
31 #include <stdlib.h>
32 #endif
33 
34 static OM_uint32
35 val_inq_ctx_args(
36     OM_uint32 *minor_status,
37     gss_ctx_id_t context_handle,
38     gss_name_t *src_name,
39     gss_name_t *targ_name,
40     OM_uint32 *lifetime_rec,
41     gss_OID *mech_type,
42     OM_uint32 *ctx_flags,
43     int *locally_initiated,
44     int *opened)
45 {
46 
47     /* Initialize outputs. */
48 
49     if (minor_status != NULL)
50 	*minor_status = 0;
51 
52     if (src_name != NULL)
53 	*src_name = GSS_C_NO_NAME;
54 
55     if (targ_name != NULL)
56 	*targ_name = GSS_C_NO_NAME;
57 
58     if (mech_type != NULL)
59 	*mech_type = GSS_C_NO_OID;
60 
61     /* Validate arguments. */
62 
63     if (minor_status == NULL)
64 	return (GSS_S_CALL_INACCESSIBLE_WRITE);
65 
66     if (context_handle == GSS_C_NO_CONTEXT)
67 	return (GSS_S_CALL_INACCESSIBLE_READ | GSS_S_NO_CONTEXT);
68 
69     return (GSS_S_COMPLETE);
70 }
71 
72 
73 /* Last argument new for V2 */
74 OM_uint32 KRB5_CALLCONV
75 gss_inquire_context(
76 	    OM_uint32 *minor_status,
77 	    gss_ctx_id_t context_handle,
78 	    gss_name_t *src_name,
79 	    gss_name_t *targ_name,
80 	    OM_uint32 *lifetime_rec,
81 	    gss_OID *mech_type,
82 	    OM_uint32 *ctx_flags,
83 	    int *locally_initiated,
84 	    int *opened)
85 {
86     gss_union_ctx_id_t	ctx;
87     gss_mechanism	mech;
88     OM_uint32		status, temp_minor;
89     gss_OID		actual_mech;
90     gss_name_t localTargName = NULL, localSourceName = NULL;
91 
92     status = val_inq_ctx_args(minor_status,
93 			      context_handle,
94 			      src_name, targ_name,
95 			      lifetime_rec,
96 			      mech_type, ctx_flags,
97 			      locally_initiated, opened);
98     if (status != GSS_S_COMPLETE)
99 	return (status);
100 
101     /*
102      * select the approprate underlying mechanism routine and
103      * call it.
104      */
105 
106     ctx = (gss_union_ctx_id_t) context_handle;
107     if (ctx->internal_ctx_id == GSS_C_NO_CONTEXT)
108 	return (GSS_S_NO_CONTEXT);
109     mech = gssint_get_mechanism (ctx->mech_type);
110 
111     if (!mech || !mech->gss_inquire_context || !mech->gss_display_name ||
112 	!mech->gss_release_name) {
113 	return (GSS_S_UNAVAILABLE);
114     }
115 
116     status = mech->gss_inquire_context(
117 			minor_status,
118 			ctx->internal_ctx_id,
119 			(src_name ? &localSourceName : NULL),
120 			(targ_name ? &localTargName : NULL),
121 			lifetime_rec,
122 			&actual_mech,
123 			ctx_flags,
124 			locally_initiated,
125 			opened);
126 
127     if (status != GSS_S_COMPLETE) {
128 	map_error(minor_status, mech);
129 	return status;
130     }
131 
132     /* need to convert names */
133 
134     if (src_name) {
135 	if (localSourceName) {
136 	    status = gssint_convert_name_to_union_name(minor_status, mech,
137 						      localSourceName, src_name);
138 
139 	    if (status != GSS_S_COMPLETE) {
140 		if (localTargName)
141 		    mech->gss_release_name(&temp_minor, &localTargName);
142 		return (status);
143 	    }
144 	} else {
145 	    *src_name = GSS_C_NO_NAME;
146 	}
147     }
148 
149     if (targ_name) {
150         if (localTargName) {
151 	    status = gssint_convert_name_to_union_name(minor_status, mech,
152 						      localTargName, targ_name);
153 
154 	    if (status != GSS_S_COMPLETE) {
155 		if (src_name)
156 		    (void) gss_release_name(&temp_minor, src_name);
157 
158 		return (status);
159 	    }
160         }
161         else {
162             *targ_name = GSS_C_NO_NAME;
163         }
164     }
165 
166     if (mech_type)
167 	*mech_type = gssint_get_public_oid(actual_mech);
168 
169     return(GSS_S_COMPLETE);
170 }
171