xref: /freebsd/crypto/heimdal/lib/gssapi/mech/gss_inquire_cred.c (revision 9f44a47fd07924afc035991af15d84e6585dea4f)
1 /*-
2  * Copyright (c) 2005 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD: src/lib/libgssapi/gss_inquire_cred.c,v 1.1 2005/12/29 14:40:20 dfr Exp $
27  */
28 
29 #include "mech_locl.h"
30 
31 #define AUSAGE 1
32 #define IUSAGE 2
33 
34 static void
35 updateusage(gss_cred_usage_t usage, int *usagemask)
36 {
37     if (usage == GSS_C_BOTH)
38 	*usagemask |= AUSAGE | IUSAGE;
39     else if (usage == GSS_C_ACCEPT)
40 	*usagemask |= AUSAGE;
41     else if (usage == GSS_C_INITIATE)
42 	*usagemask |= IUSAGE;
43 }
44 
45 GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
46 gss_inquire_cred(OM_uint32 *minor_status,
47     const gss_cred_id_t cred_handle,
48     gss_name_t *name_ret,
49     OM_uint32 *lifetime,
50     gss_cred_usage_t *cred_usage,
51     gss_OID_set *mechanisms)
52 {
53 	OM_uint32 major_status;
54 	struct _gss_mech_switch *m;
55 	struct _gss_cred *cred = (struct _gss_cred *) cred_handle;
56 	struct _gss_name *name;
57 	struct _gss_mechanism_name *mn;
58 	OM_uint32 min_lifetime;
59 	int found = 0;
60 	int usagemask = 0;
61 	gss_cred_usage_t usage;
62 
63 	_gss_load_mech();
64 
65 	*minor_status = 0;
66 	if (name_ret)
67 		*name_ret = GSS_C_NO_NAME;
68 	if (lifetime)
69 		*lifetime = 0;
70 	if (cred_usage)
71 		*cred_usage = 0;
72 	if (mechanisms)
73 		*mechanisms = GSS_C_NO_OID_SET;
74 
75 	if (name_ret) {
76 		name = calloc(1, sizeof(*name));
77 		if (name == NULL) {
78 			*minor_status = ENOMEM;
79 			return (GSS_S_FAILURE);
80 		}
81 		HEIM_SLIST_INIT(&name->gn_mn);
82 	} else {
83 		name = NULL;
84 	}
85 
86 	if (mechanisms) {
87 		major_status = gss_create_empty_oid_set(minor_status,
88 		    mechanisms);
89 		if (major_status) {
90 			if (name) free(name);
91 			return (major_status);
92 		}
93 	}
94 
95 	min_lifetime = GSS_C_INDEFINITE;
96 	if (cred) {
97 		struct _gss_mechanism_cred *mc;
98 
99 		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
100 			gss_name_t mc_name;
101 			OM_uint32 mc_lifetime;
102 
103 			major_status = mc->gmc_mech->gm_inquire_cred(minor_status,
104 			    mc->gmc_cred, &mc_name, &mc_lifetime, &usage, NULL);
105 			if (major_status)
106 				continue;
107 
108 			updateusage(usage, &usagemask);
109 			if (name) {
110 				mn = malloc(sizeof(struct _gss_mechanism_name));
111 				if (!mn) {
112 					mc->gmc_mech->gm_release_name(minor_status,
113 					    &mc_name);
114 					continue;
115 				}
116 				mn->gmn_mech = mc->gmc_mech;
117 				mn->gmn_mech_oid = mc->gmc_mech_oid;
118 				mn->gmn_name = mc_name;
119 				HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
120 			} else {
121 				mc->gmc_mech->gm_release_name(minor_status,
122 				    &mc_name);
123 			}
124 
125 			if (mc_lifetime < min_lifetime)
126 				min_lifetime = mc_lifetime;
127 
128 			if (mechanisms)
129 				gss_add_oid_set_member(minor_status,
130 				    mc->gmc_mech_oid, mechanisms);
131 			found++;
132 		}
133 	} else {
134 		HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
135 			gss_name_t mc_name;
136 			OM_uint32 mc_lifetime;
137 
138 			major_status = m->gm_mech.gm_inquire_cred(minor_status,
139 			    GSS_C_NO_CREDENTIAL, &mc_name, &mc_lifetime,
140 			    &usage, NULL);
141 			if (major_status)
142 				continue;
143 
144 			updateusage(usage, &usagemask);
145 			if (name && mc_name) {
146 				mn = malloc(
147 					sizeof(struct _gss_mechanism_name));
148 				if (!mn) {
149 					m->gm_mech.gm_release_name(
150 						minor_status, &mc_name);
151 					continue;
152 				}
153 				mn->gmn_mech = &m->gm_mech;
154 				mn->gmn_mech_oid = &m->gm_mech_oid;
155 				mn->gmn_name = mc_name;
156 				HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
157 			} else if (mc_name) {
158 				m->gm_mech.gm_release_name(minor_status,
159 				    &mc_name);
160 			}
161 
162 			if (mc_lifetime < min_lifetime)
163 				min_lifetime = mc_lifetime;
164 
165 			if (mechanisms)
166 				gss_add_oid_set_member(minor_status,
167 				    &m->gm_mech_oid, mechanisms);
168 			found++;
169 		}
170 	}
171 
172 	if (found == 0) {
173 		gss_name_t n = (gss_name_t)name;
174 		if (n)
175 			gss_release_name(minor_status, &n);
176 		gss_release_oid_set(minor_status, mechanisms);
177 		*minor_status = 0;
178 		return (GSS_S_NO_CRED);
179 	}
180 
181 	*minor_status = 0;
182 	if (name_ret)
183 		*name_ret = (gss_name_t) name;
184 	if (lifetime)
185 		*lifetime = min_lifetime;
186 	if (cred_usage) {
187 		if ((usagemask & (AUSAGE|IUSAGE)) == (AUSAGE|IUSAGE))
188 			*cred_usage = GSS_C_BOTH;
189 		else if (usagemask & IUSAGE)
190 			*cred_usage = GSS_C_INITIATE;
191 		else if (usagemask & AUSAGE)
192 			*cred_usage = GSS_C_ACCEPT;
193 	}
194 	return (GSS_S_COMPLETE);
195 }
196