gss_add_cred.c revision 168340
1153838Sdfr/*-
2153838Sdfr * Copyright (c) 2005 Doug Rabson
3153838Sdfr * All rights reserved.
4153838Sdfr *
5153838Sdfr * Redistribution and use in source and binary forms, with or without
6153838Sdfr * modification, are permitted provided that the following conditions
7153838Sdfr * are met:
8153838Sdfr * 1. Redistributions of source code must retain the above copyright
9153838Sdfr *    notice, this list of conditions and the following disclaimer.
10153838Sdfr * 2. Redistributions in binary form must reproduce the above copyright
11153838Sdfr *    notice, this list of conditions and the following disclaimer in the
12153838Sdfr *    documentation and/or other materials provided with the distribution.
13153838Sdfr *
14153838Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15153838Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16153838Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17153838Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18153838Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19153838Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20153838Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21153838Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22153838Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23153838Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24153838Sdfr * SUCH DAMAGE.
25153838Sdfr *
26153838Sdfr *	$FreeBSD: head/lib/libgssapi/gss_add_cred.c 168340 2007-04-04 02:40:59Z kan $
27153838Sdfr */
28153838Sdfr
29153838Sdfr#include <gssapi/gssapi.h>
30168340Skan#include <stdlib.h>
31153838Sdfr#include <errno.h>
32153838Sdfr
33153838Sdfr#include "mech_switch.h"
34153838Sdfr#include "cred.h"
35153838Sdfr#include "name.h"
36153838Sdfr
37153838Sdfrstatic struct _gss_mechanism_cred *
38153838Sdfr_gss_copy_cred(struct _gss_mechanism_cred *mc)
39153838Sdfr{
40153838Sdfr	struct _gss_mechanism_cred *new_mc;
41153838Sdfr	struct _gss_mech_switch *m = mc->gmc_mech;
42153838Sdfr	OM_uint32 major_status, minor_status;
43153838Sdfr	gss_name_t name;
44153838Sdfr	gss_cred_id_t cred;
45153838Sdfr	OM_uint32 initiator_lifetime, acceptor_lifetime;
46153838Sdfr	gss_cred_usage_t cred_usage;
47153838Sdfr
48153838Sdfr	major_status = m->gm_inquire_cred_by_mech(&minor_status,
49153838Sdfr	    mc->gmc_cred, mc->gmc_mech_oid,
50153838Sdfr	    &name, &initiator_lifetime, &acceptor_lifetime, &cred_usage);
51153838Sdfr	if (major_status)
52153838Sdfr		return (0);
53153838Sdfr
54153838Sdfr	major_status = m->gm_add_cred(&minor_status,
55153838Sdfr	    GSS_C_NO_CREDENTIAL, name, mc->gmc_mech_oid,
56153838Sdfr	    cred_usage, initiator_lifetime, acceptor_lifetime,
57153838Sdfr	    &cred, 0, 0, 0);
58153838Sdfr	m->gm_release_name(&minor_status, &name);
59153838Sdfr
60153838Sdfr	if (major_status)
61153838Sdfr		return (0);
62153838Sdfr
63153838Sdfr	new_mc = malloc(sizeof(struct _gss_mechanism_cred));
64153838Sdfr	if (!new_mc) {
65153838Sdfr		m->gm_release_cred(&minor_status, &cred);
66153838Sdfr		return (0);
67153838Sdfr	}
68153838Sdfr	new_mc->gmc_mech = m;
69153838Sdfr	new_mc->gmc_mech_oid = &m->gm_mech_oid;
70153838Sdfr	new_mc->gmc_cred = cred;
71153838Sdfr
72153838Sdfr	return (new_mc);
73153838Sdfr}
74153838Sdfr
75153838SdfrOM_uint32
76153838Sdfrgss_add_cred(OM_uint32 *minor_status,
77153838Sdfr    const gss_cred_id_t input_cred_handle,
78153838Sdfr    const gss_name_t desired_name,
79153838Sdfr    const gss_OID desired_mech,
80153838Sdfr    gss_cred_usage_t cred_usage,
81153838Sdfr    OM_uint32 initiator_time_req,
82153838Sdfr    OM_uint32 acceptor_time_req,
83153838Sdfr    gss_cred_id_t *output_cred_handle,
84153838Sdfr    gss_OID_set *actual_mechs,
85153838Sdfr    OM_uint32 *initiator_time_rec,
86153838Sdfr    OM_uint32 *acceptor_time_rec)
87153838Sdfr{
88153838Sdfr	OM_uint32 major_status;
89153838Sdfr	struct _gss_mech_switch *m;
90153838Sdfr	gss_OID_set_desc set;
91153838Sdfr	struct _gss_name *name = (struct _gss_name *) desired_name;
92153838Sdfr	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
93153838Sdfr	struct _gss_cred *new_cred;
94153838Sdfr	struct _gss_mechanism_cred *mc, *target_mc, *copy_mc;
95153838Sdfr	struct _gss_mechanism_name *mn;
96153838Sdfr	OM_uint32 min_time, time, junk;
97153838Sdfr	int i;
98153838Sdfr
99153838Sdfr	*output_cred_handle = 0;
100153838Sdfr	*minor_status = 0;
101153838Sdfr
102153838Sdfr	new_cred = malloc(sizeof(struct _gss_cred));
103153838Sdfr	if (!new_cred) {
104153838Sdfr		*minor_status = ENOMEM;
105153838Sdfr		return (GSS_S_FAILURE);
106153838Sdfr	}
107153838Sdfr	new_cred->gc_usage = cred_usage;
108153838Sdfr	SLIST_INIT(&new_cred->gc_mc);
109153838Sdfr
110153838Sdfr	/*
111153838Sdfr	 * We go through all the mc attached to the input_cred_handle
112153838Sdfr	 * and check the mechanism. If it matches, we call
113153838Sdfr	 * gss_add_cred for that mechanism, otherwise we copy the mc
114153838Sdfr	 * to new_cred.
115153838Sdfr	 */
116153838Sdfr	target_mc = 0;
117153838Sdfr	if (cred) {
118153838Sdfr		SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
119153838Sdfr			if (_gss_oid_equal(mc->gmc_mech, desired_mech)) {
120153838Sdfr				target_mc = mc;
121153838Sdfr			}
122153838Sdfr			copy_mc = _gss_copy_cred(mc);
123153838Sdfr			if (!copy_mc) {
124153838Sdfr				gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
125153838Sdfr				*minor_status = ENOMEM;
126153838Sdfr				return (GSS_S_FAILURE);
127153838Sdfr			}
128153838Sdfr			SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
129153838Sdfr		}
130153838Sdfr	}
131153838Sdfr
132153838Sdfr	/*
133153838Sdfr	 * Figure out a suitable mn, if any.
134153838Sdfr	 */
135153838Sdfr	if (desired_name) {
136153838Sdfr		mn = _gss_find_mn((struct _gss_name *) desired_name,
137153838Sdfr			desired_mech);
138153838Sdfr		if (!mn) {
139153838Sdfr			free(new_cred);
140153838Sdfr			return (GSS_S_BAD_NAME);
141153838Sdfr		}
142153838Sdfr	} else {
143153838Sdfr		mn = 0;
144153838Sdfr	}
145153838Sdfr
146153838Sdfr	m = _gss_find_mech_switch(desired_mech);
147153838Sdfr
148153838Sdfr	mc = malloc(sizeof(struct _gss_mechanism_cred));
149153838Sdfr	if (!mc) {
150153838Sdfr		gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
151153838Sdfr		*minor_status = ENOMEM;
152153838Sdfr		return (GSS_S_FAILURE);
153153838Sdfr	}
154153838Sdfr	mc->gmc_mech = m;
155153838Sdfr	mc->gmc_mech_oid = &m->gm_mech_oid;
156153838Sdfr
157153838Sdfr	major_status = m->gm_add_cred(minor_status,
158153838Sdfr	    target_mc ? target_mc->gmc_cred : GSS_C_NO_CREDENTIAL,
159153838Sdfr	    desired_name ? mn->gmn_name : GSS_C_NO_NAME,
160153838Sdfr	    desired_mech,
161153838Sdfr	    cred_usage,
162153838Sdfr	    initiator_time_req,
163153838Sdfr	    acceptor_time_req,
164153838Sdfr	    &mc->gmc_cred,
165153838Sdfr	    actual_mechs,
166153838Sdfr	    initiator_time_rec,
167153838Sdfr	    acceptor_time_rec);
168153838Sdfr
169153838Sdfr	if (major_status) {
170153838Sdfr		gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
171153838Sdfr		free(mc);
172153838Sdfr		return (major_status);
173153838Sdfr	}
174153838Sdfr	SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
175153838Sdfr	*output_cred_handle = (gss_cred_id_t) new_cred;
176153838Sdfr
177153838Sdfr	return (GSS_S_COMPLETE);
178153838Sdfr}
179153838Sdfr
180