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