2 * Copyright (c) 2003 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "gssapi_locl.h"
36 RCSID("$Id: add_cred.c,v 1.2.2.1 2003/10/21 21:00:47 lha Exp $");
38 OM_uint32 gss_add_cred (
39 OM_uint32 *minor_status,
40 const gss_cred_id_t input_cred_handle,
41 const gss_name_t desired_name,
42 const gss_OID desired_mech,
43 gss_cred_usage_t cred_usage,
44 OM_uint32 initiator_time_req,
45 OM_uint32 acceptor_time_req,
46 gss_cred_id_t *output_cred_handle,
47 gss_OID_set *actual_mechs,
48 OM_uint32 *initiator_time_rec,
49 OM_uint32 *acceptor_time_rec)
51 OM_uint32 ret, lifetime;
52 gss_cred_id_t cred, handle;
55 cred = input_cred_handle;
57 if (gss_oid_equal(desired_mech, GSS_KRB5_MECHANISM) == 0) {
59 return GSS_S_BAD_MECH;
62 if (cred == GSS_C_NO_CREDENTIAL && output_cred_handle == NULL) {
67 /* check if requested output usage is compatible with output usage */
68 if (output_cred_handle != NULL &&
69 (cred->usage != cred_usage && cred->usage != GSS_C_BOTH)) {
70 *minor_status = GSS_KRB5_S_G_BAD_USAGE;
71 return(GSS_S_FAILURE);
74 /* check that we have the same name */
75 if (desired_name != GSS_C_NO_NAME &&
76 krb5_principal_compare(gssapi_krb5_context, desired_name,
77 cred->principal) != FALSE) {
79 return GSS_S_BAD_NAME;
83 if (output_cred_handle) {
85 handle = (gss_cred_id_t)malloc(sizeof(*handle));
86 if (handle == GSS_C_NO_CREDENTIAL) {
87 *minor_status = ENOMEM;
88 return (GSS_S_FAILURE);
91 memset(handle, 0, sizeof (*handle));
93 handle->usage = cred_usage;
94 handle->lifetime = cred->lifetime;
95 handle->principal = NULL;
96 handle->keytab = NULL;
97 handle->ccache = NULL;
98 handle->mechanisms = NULL;
102 ret = gss_duplicate_name(minor_status, cred->principal,
106 *minor_status = ENOMEM;
107 return GSS_S_FAILURE;
111 krb5_error_code kret;
112 char name[KRB5_KT_PREFIX_MAX_LEN + MAXPATHLEN];
117 kret = krb5_kt_get_type(gssapi_krb5_context, cred->keytab,
118 name, KRB5_KT_PREFIX_MAX_LEN);
120 *minor_status = kret;
126 kret = krb5_kt_get_name(gssapi_krb5_context, cred->keytab,
130 *minor_status = kret;
134 kret = krb5_kt_resolve(gssapi_krb5_context, name,
137 *minor_status = kret;
143 krb5_error_code kret;
144 const char *type, *name;
149 type = krb5_cc_get_type(gssapi_krb5_context, cred->ccache);
151 *minor_status = ENOMEM;
155 if (strcmp(type, "MEMORY") == 0) {
156 ret = krb5_cc_gen_new(gssapi_krb5_context, &krb5_mcc_ops,
163 ret = krb5_cc_copy_cache(gssapi_krb5_context, cred->ccache,
172 name = krb5_cc_get_name(gssapi_krb5_context, cred->ccache);
174 *minor_status = ENOMEM;
178 asprintf(&type_name, "%s:%s", type, name);
179 if (type_name == NULL) {
180 *minor_status = ENOMEM;
184 kret = krb5_cc_resolve(gssapi_krb5_context, type_name,
188 *minor_status = kret;
194 ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
198 ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
199 &handle->mechanisms);
204 ret = gss_inquire_cred(minor_status, cred, NULL, &lifetime,
209 if (initiator_time_rec)
210 *initiator_time_rec = lifetime;
211 if (acceptor_time_rec)
212 *acceptor_time_rec = lifetime;
214 if (output_cred_handle)
215 *output_cred_handle = handle;
223 if (handle->principal)
224 gss_release_name(NULL, &handle->principal);
226 krb5_kt_close(gssapi_krb5_context, handle->keytab);
228 krb5_cc_destroy(gssapi_krb5_context, handle->ccache);
229 if (handle->mechanisms)
230 gss_release_oid_set(NULL, &handle->mechanisms);