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
34static void
35updateusage(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
45GSSAPI_LIB_FUNCTION OM_uint32 GSSAPI_LIB_CALL
46gss_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 = _gss_create_name(NULL, NULL);
77		if (name == NULL) {
78			*minor_status = ENOMEM;
79			return (GSS_S_FAILURE);
80		}
81	} else {
82		name = NULL;
83	}
84
85	if (mechanisms) {
86		major_status = gss_create_empty_oid_set(minor_status,
87		    mechanisms);
88		if (major_status) {
89			if (name) free(name);
90			return (major_status);
91		}
92	}
93
94	min_lifetime = GSS_C_INDEFINITE;
95	if (cred) {
96		struct _gss_mechanism_cred *mc;
97
98		HEIM_SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
99			gss_name_t mc_name;
100			OM_uint32 mc_lifetime;
101
102			major_status = mc->gmc_mech->gm_inquire_cred(minor_status,
103			    mc->gmc_cred, &mc_name, &mc_lifetime, &usage, NULL);
104			if (major_status)
105				continue;
106
107			updateusage(usage, &usagemask);
108			if (name) {
109				mn = malloc(sizeof(struct _gss_mechanism_name));
110				if (!mn) {
111					mc->gmc_mech->gm_release_name(minor_status,
112					    &mc_name);
113					continue;
114				}
115				mn->gmn_mech = mc->gmc_mech;
116				mn->gmn_mech_oid = mc->gmc_mech_oid;
117				mn->gmn_name = mc_name;
118				HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
119			} else {
120				mc->gmc_mech->gm_release_name(minor_status,
121				    &mc_name);
122			}
123
124			if (mc_lifetime < min_lifetime)
125				min_lifetime = mc_lifetime;
126
127			if (mechanisms)
128				gss_add_oid_set_member(minor_status,
129				    mc->gmc_mech_oid, mechanisms);
130			found++;
131		}
132	} else {
133		HEIM_SLIST_FOREACH(m, &_gss_mechs, gm_link) {
134			gss_name_t mc_name;
135			OM_uint32 mc_lifetime;
136
137			if (m->gm_mech.gm_inquire_cred == NULL)
138				continue;
139
140			major_status = m->gm_mech.gm_inquire_cred(minor_status,
141			    GSS_C_NO_CREDENTIAL, &mc_name, &mc_lifetime,
142			    &usage, NULL);
143			if (major_status)
144				continue;
145
146			updateusage(usage, &usagemask);
147			if (name && mc_name) {
148				mn = malloc(
149					sizeof(struct _gss_mechanism_name));
150				if (!mn) {
151					m->gm_mech.gm_release_name(
152						minor_status, &mc_name);
153					continue;
154				}
155				mn->gmn_mech = &m->gm_mech;
156				mn->gmn_mech_oid = &m->gm_mech_oid;
157				mn->gmn_name = mc_name;
158				HEIM_SLIST_INSERT_HEAD(&name->gn_mn, mn, gmn_link);
159			} else if (mc_name) {
160				m->gm_mech.gm_release_name(minor_status,
161				    &mc_name);
162			}
163
164			if (mc_lifetime < min_lifetime)
165				min_lifetime = mc_lifetime;
166
167			if (mechanisms)
168				gss_add_oid_set_member(minor_status,
169				    &m->gm_mech_oid, mechanisms);
170			found++;
171		}
172	}
173
174	if (found == 0 || min_lifetime == 0) {
175		gss_name_t n = (gss_name_t)name;
176		if (n)
177			gss_release_name(minor_status, &n);
178		gss_release_oid_set(minor_status, mechanisms);
179		*minor_status = 0;
180		if (min_lifetime == 0)
181			return (GSS_S_CREDENTIALS_EXPIRED);
182		return (GSS_S_NO_CRED);
183	}
184
185	*minor_status = 0;
186	if (name_ret)
187		*name_ret = (gss_name_t) name;
188	if (lifetime)
189		*lifetime = min_lifetime;
190	if (cred_usage) {
191		if ((usagemask & (AUSAGE|IUSAGE)) == (AUSAGE|IUSAGE))
192			*cred_usage = GSS_C_BOTH;
193		else if (usagemask & IUSAGE)
194			*cred_usage = GSS_C_INITIATE;
195		else if (usagemask & AUSAGE)
196			*cred_usage = GSS_C_ACCEPT;
197	}
198
199	return (GSS_S_COMPLETE);
200}
201