remove gcc34
[dragonfly.git] / crypto / heimdal-0.6.3 / lib / gssapi / add_cred.c
1 /*
2  * Copyright (c) 2003 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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 
31  * SUCH DAMAGE. 
32  */
33
34 #include "gssapi_locl.h"
35
36 RCSID("$Id: add_cred.c,v 1.2.2.1 2003/10/21 21:00:47 lha Exp $");
37
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)
50 {
51     OM_uint32 ret, lifetime;
52     gss_cred_id_t cred, handle;
53
54     handle = NULL;
55     cred = input_cred_handle;
56
57     if (gss_oid_equal(desired_mech, GSS_KRB5_MECHANISM) == 0) {
58         *minor_status = 0;
59         return GSS_S_BAD_MECH;
60     }
61
62     if (cred == GSS_C_NO_CREDENTIAL && output_cred_handle == NULL) {
63         *minor_status = 0;
64         return GSS_S_NO_CRED;
65     }
66
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);
72     }
73         
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) {
78         *minor_status = 0;
79         return GSS_S_BAD_NAME;
80     }
81
82     /* make a copy */
83     if (output_cred_handle) {
84
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);
89         }
90
91         memset(handle, 0, sizeof (*handle));
92
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;
99         
100         ret = GSS_S_FAILURE;
101
102         ret = gss_duplicate_name(minor_status, cred->principal,
103                                  &handle->principal);
104         if (ret) {
105             free(handle);
106             *minor_status = ENOMEM;
107             return GSS_S_FAILURE;
108         }
109
110         if (cred->keytab) {
111             krb5_error_code kret;
112             char name[KRB5_KT_PREFIX_MAX_LEN + MAXPATHLEN];
113             int len;
114             
115             ret = GSS_S_FAILURE;
116
117             kret = krb5_kt_get_type(gssapi_krb5_context, cred->keytab,
118                                     name, KRB5_KT_PREFIX_MAX_LEN);
119             if (kret) {
120                 *minor_status = kret;
121                 goto failure;
122             }
123             len = strlen(name);
124             name[len++] = ':';
125
126             kret = krb5_kt_get_name(gssapi_krb5_context, cred->keytab,
127                                     name + len, 
128                                     sizeof(name) - len);
129             if (kret) {
130                 *minor_status = kret;
131                 goto failure;
132             }
133
134             kret = krb5_kt_resolve(gssapi_krb5_context, name,
135                                    &handle->keytab);
136             if (kret){
137                 *minor_status = kret;
138                 goto failure;
139             }
140         }
141
142         if (cred->ccache) {
143             krb5_error_code kret;
144             const char *type, *name;
145             char *type_name;
146
147             ret = GSS_S_FAILURE;
148
149             type = krb5_cc_get_type(gssapi_krb5_context, cred->ccache);
150             if (type == NULL){
151                 *minor_status = ENOMEM;
152                 goto failure;
153             }
154
155             if (strcmp(type, "MEMORY") == 0) {
156                 ret = krb5_cc_gen_new(gssapi_krb5_context, &krb5_mcc_ops,
157                                       &handle->ccache);
158                 if (ret) {
159                     *minor_status = ret;
160                     goto failure;
161                 }
162
163                 ret = krb5_cc_copy_cache(gssapi_krb5_context, cred->ccache,
164                                          handle->ccache);
165                 if (ret) {
166                     *minor_status = ret;
167                     goto failure;
168                 }
169
170             } else {
171
172                 name = krb5_cc_get_name(gssapi_krb5_context, cred->ccache);
173                 if (name == NULL) {
174                     *minor_status = ENOMEM;
175                     goto failure;
176                 }
177                 
178                 asprintf(&type_name, "%s:%s", type, name);
179                 if (type_name == NULL) {
180                     *minor_status = ENOMEM;
181                     goto failure;
182                 }
183                 
184                 kret = krb5_cc_resolve(gssapi_krb5_context, type_name,
185                                        &handle->ccache);
186                 free(type_name);
187                 if (kret) {
188                     *minor_status = kret;
189                     goto failure;
190                 }           
191             }
192         }
193
194         ret = gss_create_empty_oid_set(minor_status, &handle->mechanisms);
195         if (ret)
196             goto failure;
197
198         ret = gss_add_oid_set_member(minor_status, GSS_KRB5_MECHANISM,
199                                      &handle->mechanisms);
200         if (ret)
201             goto failure;
202     }
203
204     ret = gss_inquire_cred(minor_status, cred, NULL, &lifetime,
205                            NULL, actual_mechs);
206     if (ret)
207         goto failure;
208
209     if (initiator_time_rec)
210         *initiator_time_rec = lifetime;
211     if (acceptor_time_rec)
212         *acceptor_time_rec = lifetime;
213
214     if (output_cred_handle)
215         *output_cred_handle = handle;
216
217     *minor_status = 0;
218     return ret;
219
220  failure:
221
222     if (handle) {
223         if (handle->principal)
224             gss_release_name(NULL, &handle->principal);
225         if (handle->keytab)
226             krb5_kt_close(gssapi_krb5_context, handle->keytab);
227         if (handle->ccache)
228             krb5_cc_destroy(gssapi_krb5_context, handle->ccache);
229         if (handle->mechanisms)
230             gss_release_oid_set(NULL, &handle->mechanisms);
231         free(handle);
232     }
233     return ret;
234 }