1 /* $NetBSD: copy_ccache.c,v 1.2 2017/01/28 21:31:46 christos Exp $ */
2
3 /*
4 * Copyright (c) 2000 - 2001, 2003 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "gsskrb5_locl.h"
37
38 #if 0
39 OM_uint32
40 gss_krb5_copy_ccache(OM_uint32 *minor_status,
41 krb5_context context,
42 gss_cred_id_t cred,
43 krb5_ccache out)
44 {
45 krb5_error_code kret;
46
47 HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);
48
49 if (cred->ccache == NULL) {
50 HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
51 *minor_status = EINVAL;
52 return GSS_S_FAILURE;
53 }
54
55 kret = krb5_cc_copy_cache(context, cred->ccache, out);
56 HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
57 if (kret) {
58 *minor_status = kret;
59 return GSS_S_FAILURE;
60 }
61 *minor_status = 0;
62 return GSS_S_COMPLETE;
63 }
64 #endif
65
66
67 OM_uint32
_gsskrb5_krb5_import_cred(OM_uint32 * minor_status,krb5_ccache id,krb5_principal keytab_principal,krb5_keytab keytab,gss_cred_id_t * cred)68 _gsskrb5_krb5_import_cred(OM_uint32 *minor_status,
69 krb5_ccache id,
70 krb5_principal keytab_principal,
71 krb5_keytab keytab,
72 gss_cred_id_t *cred)
73 {
74 krb5_context context;
75 krb5_error_code kret;
76 gsskrb5_cred handle;
77 OM_uint32 ret;
78
79 *cred = NULL;
80
81 GSSAPI_KRB5_INIT (&context);
82
83 handle = calloc(1, sizeof(*handle));
84 if (handle == NULL) {
85 _gsskrb5_clear_status ();
86 *minor_status = ENOMEM;
87 return (GSS_S_FAILURE);
88 }
89 HEIMDAL_MUTEX_init(&handle->cred_id_mutex);
90
91 handle->usage = 0;
92
93 if (id) {
94 time_t now;
95 OM_uint32 left;
96 char *str;
97
98 handle->usage |= GSS_C_INITIATE;
99
100 kret = krb5_cc_get_principal(context, id,
101 &handle->principal);
102 if (kret) {
103 free(handle);
104 *minor_status = kret;
105 return GSS_S_FAILURE;
106 }
107
108 if (keytab_principal) {
109 krb5_boolean match;
110
111 match = krb5_principal_compare(context,
112 handle->principal,
113 keytab_principal);
114 if (match == FALSE) {
115 krb5_free_principal(context, handle->principal);
116 free(handle);
117 _gsskrb5_clear_status ();
118 *minor_status = EINVAL;
119 return GSS_S_FAILURE;
120 }
121 }
122
123 krb5_timeofday(context, &now);
124 ret = __gsskrb5_ccache_lifetime(minor_status,
125 context,
126 id,
127 handle->principal,
128 &left);
129 if (ret != GSS_S_COMPLETE) {
130 krb5_free_principal(context, handle->principal);
131 free(handle);
132 return ret;
133 }
134 handle->endtime = now + left;
135
136 kret = krb5_cc_get_full_name(context, id, &str);
137 if (kret)
138 goto out;
139
140 kret = krb5_cc_resolve(context, str, &handle->ccache);
141 free(str);
142 if (kret)
143 goto out;
144 }
145
146
147 if (keytab) {
148 char *str;
149
150 handle->usage |= GSS_C_ACCEPT;
151
152 if (keytab_principal && handle->principal == NULL) {
153 kret = krb5_copy_principal(context,
154 keytab_principal,
155 &handle->principal);
156 if (kret)
157 goto out;
158 }
159
160 kret = krb5_kt_get_full_name(context, keytab, &str);
161 if (kret)
162 goto out;
163
164 kret = krb5_kt_resolve(context, str, &handle->keytab);
165 free(str);
166 if (kret)
167 goto out;
168 }
169
170
171 if (id || keytab) {
172 ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
173 if (ret == GSS_S_COMPLETE)
174 ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
175 &handle->mechanisms);
176 if (ret != GSS_S_COMPLETE) {
177 kret = *minor_status;
178 goto out;
179 }
180 }
181
182 *minor_status = 0;
183 *cred = (gss_cred_id_t)handle;
184 return GSS_S_COMPLETE;
185
186 out:
187 gss_release_oid_set(minor_status, &handle->mechanisms);
188 if (handle->ccache)
189 krb5_cc_close(context, handle->ccache);
190 if (handle->keytab)
191 krb5_kt_close(context, handle->keytab);
192 if (handle->principal)
193 krb5_free_principal(context, handle->principal);
194 HEIMDAL_MUTEX_destroy(&handle->cred_id_mutex);
195 free(handle);
196 *minor_status = kret;
197 return GSS_S_FAILURE;
198 }
199