r19604: This is a massive commit, and I appologise in advance for it's size.
[ab/samba.git/.git] / source4 / heimdal / lib / gssapi / krb5 / set_cred_option.c
1 /*
2  * Copyright (c) 2004, PADL Software Pty Ltd.
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  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of PADL Software nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "krb5/gsskrb5_locl.h"
34
35 RCSID("$Id: set_cred_option.c,v 1.4 2006/10/24 20:14:13 lha Exp $");
36
37 static gss_OID_desc gss_krb5_import_cred_x_oid_desc =
38 {9, (void *)"\x2b\x06\x01\x04\x01\xa9\x4a\x13\x04"}; /* XXX */
39
40 gss_OID GSS_KRB5_IMPORT_CRED_X = &gss_krb5_import_cred_x_oid_desc;
41
42 static OM_uint32
43 import_cred(OM_uint32 *minor_status,
44             gss_cred_id_t *cred_handle,
45             const gss_buffer_t value)
46 {
47     OM_uint32 major_stat;
48     krb5_error_code ret;
49     krb5_principal keytab_principal = NULL;
50     krb5_keytab keytab = NULL;
51     krb5_storage *sp = NULL;
52     krb5_ccache id = NULL;
53     char *str;
54
55     if (cred_handle == NULL || *cred_handle != GSS_C_NO_CREDENTIAL) {
56         *minor_status = 0;
57         return GSS_S_FAILURE;
58     }
59
60     sp = krb5_storage_from_mem(value->value, value->length);
61     if (sp == NULL) {
62         *minor_status = 0;
63         return GSS_S_FAILURE;
64     }
65
66     /* credential cache name */
67     ret = krb5_ret_string(sp, &str);
68     if (ret) {
69         *minor_status = ret;
70         major_stat =  GSS_S_FAILURE;
71         goto out;
72     }
73     if (str[0]) {
74         ret = krb5_cc_resolve(_gsskrb5_context, str, &id);
75         if (ret) {
76             *minor_status = ret;
77             major_stat =  GSS_S_FAILURE;
78             goto out;
79         }
80     }
81     free(str);
82     str = NULL;
83
84     /* keytab principal name */
85     ret = krb5_ret_string(sp, &str);
86     if (ret == 0 && str[0])
87         ret = krb5_parse_name(_gsskrb5_context, str, &keytab_principal);
88     if (ret) {
89         *minor_status = ret;
90         major_stat = GSS_S_FAILURE;
91         goto out;
92     }
93     free(str);
94     str = NULL;
95
96     /* keytab principal */
97     ret = krb5_ret_string(sp, &str);
98     if (ret) {
99         *minor_status = ret;
100         major_stat =  GSS_S_FAILURE;
101         goto out;
102     }
103     if (str[0]) {
104         ret = krb5_kt_resolve(_gsskrb5_context, str, &keytab);
105         if (ret) {
106             *minor_status = ret;
107             major_stat =  GSS_S_FAILURE;
108             goto out;
109         }
110     }
111     free(str);
112     str = NULL;
113
114     major_stat = _gsskrb5_import_cred(minor_status, id, keytab_principal,
115                                       keytab, cred_handle);
116 out:
117     if (id)
118         krb5_cc_close(_gsskrb5_context, id);
119     if (keytab_principal)
120         krb5_free_principal(_gsskrb5_context, keytab_principal);
121     if (keytab)
122         krb5_kt_close(_gsskrb5_context, keytab);
123     if (str)
124         free(str);
125     if (sp)
126         krb5_storage_free(sp);
127
128     return major_stat;
129 }
130
131
132 OM_uint32
133 _gsskrb5_set_cred_option
134            (OM_uint32 *minor_status,
135             gss_cred_id_t *cred_handle,
136             const gss_OID desired_object,
137             const gss_buffer_t value)
138 {
139     GSSAPI_KRB5_INIT ();
140
141     if (value == GSS_C_NO_BUFFER) {
142         *minor_status = EINVAL;
143         return GSS_S_FAILURE;
144     }
145
146     if (gss_oid_equal(desired_object, GSS_KRB5_IMPORT_CRED_X)) {
147         return import_cred(minor_status, cred_handle, value);
148     }
149
150     *minor_status = EINVAL;
151     return GSS_S_FAILURE;
152 }