xref: /freebsd/lib/libgssapi/gss_add_cred.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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$
27  */
28 
29 #include <gssapi/gssapi.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 
33 #include "mech_switch.h"
34 #include "cred.h"
35 #include "name.h"
36 
37 static struct _gss_mechanism_cred *
38 _gss_copy_cred(struct _gss_mechanism_cred *mc)
39 {
40 	struct _gss_mechanism_cred *new_mc;
41 	struct _gss_mech_switch *m = mc->gmc_mech;
42 	OM_uint32 major_status, minor_status;
43 	gss_name_t name;
44 	gss_cred_id_t cred;
45 	OM_uint32 initiator_lifetime, acceptor_lifetime;
46 	gss_cred_usage_t cred_usage;
47 
48 	major_status = m->gm_inquire_cred_by_mech(&minor_status,
49 	    mc->gmc_cred, mc->gmc_mech_oid,
50 	    &name, &initiator_lifetime, &acceptor_lifetime, &cred_usage);
51 	if (major_status)
52 		return (0);
53 
54 	major_status = m->gm_add_cred(&minor_status,
55 	    GSS_C_NO_CREDENTIAL, name, mc->gmc_mech_oid,
56 	    cred_usage, initiator_lifetime, acceptor_lifetime,
57 	    &cred, 0, 0, 0);
58 	m->gm_release_name(&minor_status, &name);
59 
60 	if (major_status)
61 		return (0);
62 
63 	new_mc = malloc(sizeof(struct _gss_mechanism_cred));
64 	if (!new_mc) {
65 		m->gm_release_cred(&minor_status, &cred);
66 		return (0);
67 	}
68 	new_mc->gmc_mech = m;
69 	new_mc->gmc_mech_oid = &m->gm_mech_oid;
70 	new_mc->gmc_cred = cred;
71 
72 	return (new_mc);
73 }
74 
75 OM_uint32
76 gss_add_cred(OM_uint32 *minor_status,
77     const gss_cred_id_t input_cred_handle,
78     const gss_name_t desired_name,
79     const gss_OID desired_mech,
80     gss_cred_usage_t cred_usage,
81     OM_uint32 initiator_time_req,
82     OM_uint32 acceptor_time_req,
83     gss_cred_id_t *output_cred_handle,
84     gss_OID_set *actual_mechs,
85     OM_uint32 *initiator_time_rec,
86     OM_uint32 *acceptor_time_rec)
87 {
88 	OM_uint32 major_status;
89 	struct _gss_mech_switch *m;
90 	gss_OID_set_desc set;
91 	struct _gss_name *name = (struct _gss_name *) desired_name;
92 	struct _gss_cred *cred = (struct _gss_cred *) input_cred_handle;
93 	struct _gss_cred *new_cred;
94 	struct _gss_mechanism_cred *mc, *target_mc, *copy_mc;
95 	struct _gss_mechanism_name *mn;
96 	OM_uint32 min_time, time, junk;
97 	int i;
98 
99 	*output_cred_handle = 0;
100 	*minor_status = 0;
101 
102 	new_cred = malloc(sizeof(struct _gss_cred));
103 	if (!new_cred) {
104 		*minor_status = ENOMEM;
105 		return (GSS_S_FAILURE);
106 	}
107 	new_cred->gc_usage = cred_usage;
108 	SLIST_INIT(&new_cred->gc_mc);
109 
110 	/*
111 	 * We go through all the mc attached to the input_cred_handle
112 	 * and check the mechanism. If it matches, we call
113 	 * gss_add_cred for that mechanism, otherwise we copy the mc
114 	 * to new_cred.
115 	 */
116 	target_mc = 0;
117 	if (cred) {
118 		SLIST_FOREACH(mc, &cred->gc_mc, gmc_link) {
119 			if (_gss_oid_equal(mc->gmc_mech, desired_mech)) {
120 				target_mc = mc;
121 			}
122 			copy_mc = _gss_copy_cred(mc);
123 			if (!copy_mc) {
124 				gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
125 				*minor_status = ENOMEM;
126 				return (GSS_S_FAILURE);
127 			}
128 			SLIST_INSERT_HEAD(&new_cred->gc_mc, copy_mc, gmc_link);
129 		}
130 	}
131 
132 	/*
133 	 * Figure out a suitable mn, if any.
134 	 */
135 	if (desired_name) {
136 		mn = _gss_find_mn((struct _gss_name *) desired_name,
137 			desired_mech);
138 		if (!mn) {
139 			free(new_cred);
140 			return (GSS_S_BAD_NAME);
141 		}
142 	} else {
143 		mn = 0;
144 	}
145 
146 	m = _gss_find_mech_switch(desired_mech);
147 
148 	mc = malloc(sizeof(struct _gss_mechanism_cred));
149 	if (!mc) {
150 		gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
151 		*minor_status = ENOMEM;
152 		return (GSS_S_FAILURE);
153 	}
154 	mc->gmc_mech = m;
155 	mc->gmc_mech_oid = &m->gm_mech_oid;
156 
157 	major_status = m->gm_add_cred(minor_status,
158 	    target_mc ? target_mc->gmc_cred : GSS_C_NO_CREDENTIAL,
159 	    desired_name ? mn->gmn_name : GSS_C_NO_NAME,
160 	    desired_mech,
161 	    cred_usage,
162 	    initiator_time_req,
163 	    acceptor_time_req,
164 	    &mc->gmc_cred,
165 	    actual_mechs,
166 	    initiator_time_rec,
167 	    acceptor_time_rec);
168 
169 	if (major_status) {
170 		gss_release_cred(&junk, (gss_cred_id_t*) &new_cred);
171 		free(mc);
172 		return (major_status);
173 	}
174 	SLIST_INSERT_HEAD(&new_cred->gc_mc, mc, gmc_link);
175 	*output_cred_handle = (gss_cred_id_t) new_cred;
176 
177 	return (GSS_S_COMPLETE);
178 }
179 
180