xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/mech/store_cred.c (revision e8031f0a8ed0e45c6d8847c5e09424e66fd34a4b)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #pragma ident	"%Z%%M%	%I%	%E% SMI"
7 
8 #include <k5-int.h>
9 #include <gssapiP_krb5.h>
10 #include <memory.h>
11 #include <assert.h>
12 
13 static
14 OM_uint32
15 store_init_cred(ct, minor_status, cred, dflt)
16 krb5_context ct;
17 OM_uint32 *minor_status;
18 const krb5_gss_cred_id_t cred;
19 int dflt;
20 {
21 	OM_uint32 maj = GSS_S_COMPLETE;
22 	krb5_error_code code;
23 	krb5_ccache ccache = NULL; /* current [file] ccache */
24 	krb5_principal ccprinc = NULL; /* default princ of current ccache */
25 
26 	if (minor_status == NULL)
27 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
28 	*minor_status = 0;
29 
30 	/* Get current ccache -- respect KRB5CCNAME, or use OS default */
31 	if ((code = krb5_cc_default(ct, &ccache))) {
32 		*minor_status = code;
33 		return (GSS_S_FAILURE);
34 	}
35 
36 	/*
37 	 * Here we should do something like:
38 	 *
39 	 * a) take all the initial tickets from the current ccache for
40 	 * client principals other than the given cred's
41 	 * b) copy them to a tmp MEMORY ccache
42 	 * c) copy the given cred's tickets to that same tmp ccache
43 	 * d) initialize the current ccache with either the same default
44 	 * princ as before (!dflt) or with the input cred's princ as the
45 	 * default princ (dflt) and copy the tmp ccache's creds to it.
46 	 *
47 	 * However, for now we just initialize the current ccache, if
48 	 * (dflt), and copy the input cred's tickets to it.
49 	 *
50 	 * To support the above ideal we'd need a variant of
51 	 * krb5_cc_copy_creds().  But then, preserving any tickets from
52 	 * the current ccache may be problematic if the ccache has many,
53 	 * many service tickets in it as that makes ccache enumeration
54 	 * really, really slow; we might want to address ccache perf
55 	 * first.
56 	 *
57 	 * So storing of non-default credentials is not supported.
58 	 */
59 	if (dflt) {
60 		/* Treat this as "caller asks to initialize ccache" */
61 		/* LINTED */
62 		if ((code = krb5_cc_initialize(ct, ccache, cred->princ))) {
63 			*minor_status = code;
64 			maj = GSS_S_FAILURE;
65 			goto cleanup;
66 		}
67 	} else {
68 		*minor_status = (OM_uint32) G_STORE_NON_DEFAULT_CRED_NOSUPP;
69 		maj = GSS_S_FAILURE;
70 		goto cleanup;
71 	}
72 
73 	if ((code = krb5_cc_copy_creds(ct, cred->ccache, ccache))) {
74 		*minor_status = code;
75 		maj = GSS_S_FAILURE;
76 		goto cleanup;
77 	}
78 
79 cleanup:
80 	if (ccprinc != NULL)
81 		krb5_free_principal(ct, ccprinc);
82 	if (ccache != NULL)
83 		/* LINTED */
84 		krb5_cc_close(ct, ccache);
85 
86 	return (maj);
87 }
88 
89 OM_uint32
90 krb5_gss_store_cred(ct, minor_status, input_cred, cred_usage, desired_mech,
91 			overwrite_cred, default_cred, elements_stored,
92 			cred_usage_stored)
93 void *ct;
94 OM_uint32 *minor_status;
95 const gss_cred_id_t input_cred;
96 gss_cred_usage_t cred_usage;
97 gss_OID desired_mech;
98 OM_uint32 overwrite_cred;
99 OM_uint32 default_cred;
100 gss_OID_set *elements_stored;
101 gss_cred_usage_t *cred_usage_stored;
102 {
103 	OM_uint32 ret;
104 	mutex_lock(&krb5_mutex);
105 	ret = krb5_gss_store_cred_no_lock(ct, minor_status, input_cred,
106 			cred_usage, desired_mech, overwrite_cred, default_cred,
107 			elements_stored, cred_usage_stored);
108 	mutex_unlock(&krb5_mutex);
109 	return (ret);
110 }
111 
112 OM_uint32
113 krb5_gss_store_cred_no_lock(ct, minor_status, input_cred, cred_usage,
114 		desired_mech, overwrite_cred, default_cred, elements_stored,
115 		cred_usage_stored)
116 void *ct;
117 OM_uint32 *minor_status;
118 const gss_cred_id_t input_cred;
119 gss_cred_usage_t cred_usage;
120 gss_OID desired_mech;
121 OM_uint32 overwrite_cred;
122 OM_uint32 default_cred;
123 gss_OID_set *elements_stored;
124 gss_cred_usage_t *cred_usage_stored;
125 {
126 	OM_uint32 maj, maj2, min;
127 	krb5_context ctx = (krb5_context)ct;
128 	krb5_gss_cred_id_t cred = (krb5_gss_cred_id_t)input_cred;
129 	krb5_gss_cred_id_t cur_cred = (krb5_gss_cred_id_t)GSS_C_NO_CREDENTIAL;
130 	gss_OID_set desired_mechs = GSS_C_NULL_OID_SET;
131 	OM_uint32 in_time_rec;			/* lifetime of input cred */
132 	OM_uint32 cur_time_rec;			/* lifetime of current cred */
133 	gss_cred_usage_t in_usage;		/* usage of input cred */
134 	gss_name_t in_name = GSS_C_NO_NAME;	/* name of input cred */
135 
136 	if (input_cred == GSS_C_NO_CREDENTIAL)
137 		return (GSS_S_CALL_INACCESSIBLE_READ);
138 
139 	/* Initialize output parameters */
140 	if (minor_status == NULL)
141 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
142 	*minor_status = 0;
143 
144 	if (elements_stored != NULL)
145 		*elements_stored = GSS_C_NULL_OID_SET;
146 
147 	if (cred_usage_stored != NULL)
148 		*cred_usage_stored = -1; /* need GSS_C_NEITHER! */
149 
150 	/* Sanity check cred_usage */
151 	if (cred_usage != GSS_C_BOTH && cred_usage != GSS_C_INITIATE &&
152 	    cred_usage != GSS_C_ACCEPT) {
153 		*minor_status = (OM_uint32) G_BAD_USAGE;
154 		return (GSS_S_CALL_BAD_STRUCTURE);
155 	}
156 
157 	/* Not supported: storing acceptor creds -- short cut now */
158 	if (cred_usage == GSS_C_ACCEPT) {
159 		*minor_status = (OM_uint32) G_STORE_ACCEPTOR_CRED_NOSUPP;
160 		return (GSS_S_FAILURE);
161 	}
162 	if (cred_usage == GSS_C_BOTH)
163 		cred_usage = GSS_C_INITIATE;
164 
165 	/* * Find out the name, lifetime and cred usage of the input cred */
166 	maj = krb5_gss_inquire_cred_no_lock(ctx, minor_status, input_cred,
167 			&in_name, &in_time_rec, &in_usage, NULL);
168 	if (GSS_ERROR(maj))
169 		goto cleanup;
170 
171 	/* Check that the input cred isn't expired */
172 	if (in_time_rec == 0) {
173 		maj = GSS_S_CREDENTIALS_EXPIRED;
174 		goto cleanup;
175 	}
176 
177 	/* The requested and input cred usage must agree */
178 	if (in_usage != cred_usage && cred_usage != GSS_C_BOTH) {
179 		*minor_status = (OM_uint32) G_CRED_USAGE_MISMATCH;
180 		maj = GSS_S_NO_CRED;
181 		goto cleanup;
182 	}
183 
184 	if (in_usage == GSS_C_ACCEPT) {
185 		*minor_status = (OM_uint32) G_STORE_ACCEPTOR_CRED_NOSUPP;
186 		maj = GSS_S_FAILURE;
187 		goto cleanup;
188 	}
189 
190 	/* Get current cred, if any */
191 	if (desired_mech != GSS_C_NULL_OID) {
192 		/* assume that libgss gave us one of our mech OIDs */
193 		maj = gss_create_empty_oid_set(minor_status, &desired_mechs);
194 		if (GSS_ERROR(maj))
195 			return (maj);
196 
197 		maj = gss_add_oid_set_member(minor_status, desired_mech,
198 				&desired_mechs);
199 		if (GSS_ERROR(maj))
200 			goto cleanup;
201 	}
202 
203 	/*
204 	 * Handle overwrite_cred option.  If overwrite_cred == FALSE
205 	 * then we must be careful not to overwrite an existing
206 	 * unexpired credential.
207 	 */
208 	maj2 = krb5_gss_acquire_cred_no_lock(ctx, &min,
209 			(default_cred) ?  GSS_C_NO_NAME : in_name,
210 			0, desired_mechs, cred_usage,
211 			(gss_cred_id_t *)&cur_cred, NULL, &cur_time_rec);
212 
213 	if (GSS_ERROR(maj2))
214 		overwrite_cred = 1; /* nothing to overwrite */
215 
216 	if (cur_time_rec > 0 && !overwrite_cred) {
217 		maj = GSS_S_DUPLICATE_ELEMENT; /* would overwrite */
218 		goto cleanup;
219 	}
220 
221 	/* Ready to store -- store_init_cred() handles default_cred */
222 	maj = store_init_cred(ctx, minor_status, cred, default_cred);
223 	if (GSS_ERROR(maj))
224 		goto cleanup;
225 
226 	/* Output parameters */
227 	if (cred_usage_stored != NULL)
228 		*cred_usage_stored = GSS_C_INITIATE;
229 
230 	if (elements_stored != NULL) {
231 		maj = gss_create_empty_oid_set(minor_status, elements_stored);
232 		if (GSS_ERROR(maj))
233 			goto cleanup;
234 
235 		maj = gss_add_oid_set_member(minor_status,
236 			    (const gss_OID)gss_mech_krb5, elements_stored);
237 		if (GSS_ERROR(maj)) {
238 			(void) gss_release_oid_set(&min, elements_stored);
239 			*elements_stored = GSS_C_NULL_OID_SET;
240 			goto cleanup;
241 		}
242 	}
243 
244 cleanup:
245 	if (desired_mechs != GSS_C_NULL_OID_SET)
246 		(void) gss_release_oid_set(&min, &desired_mechs);
247 	if (cur_cred != (krb5_gss_cred_id_t)GSS_C_NO_CREDENTIAL)
248 		(void) krb5_gss_release_cred_no_lock(ctx, &min,
249 				    (gss_cred_id_t *)&cur_cred);
250 	if (in_name != GSS_C_NO_NAME)
251 		(void) krb5_gss_release_name_no_lock(ctx, &min, &in_name);
252 
253 	return (maj);
254 }
255