summaryrefslogtreecommitdiff
path: root/third_party/heimdal/lib/gssapi/krb5/inquire_cred.c
blob: b7b67f783f6bf85000d494900f960122d9e85c59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (c) 1997, 2003 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include "gsskrb5_locl.h"

OM_uint32 GSSAPI_CALLCONV _gsskrb5_inquire_cred
(OM_uint32 * minor_status,
 gss_const_cred_id_t cred_handle,
 gss_name_t * output_name,
 OM_uint32 * lifetime,
 gss_cred_usage_t * cred_usage,
 gss_OID_set * mechanisms
    )
{
    krb5_context context;
    gss_cred_id_t aqcred_init = GSS_C_NO_CREDENTIAL;
    gss_cred_id_t aqcred_accept = GSS_C_NO_CREDENTIAL;
    gsskrb5_cred cred = (gsskrb5_cred)cred_handle;
    gss_OID_set amechs = GSS_C_NO_OID_SET;
    gss_OID_set imechs = GSS_C_NO_OID_SET;
    OM_uint32 junk;
    OM_uint32 aminor;
    OM_uint32 ret;
    OM_uint32 aret;
    OM_uint32 alife = GSS_C_INDEFINITE;
    OM_uint32 ilife = GSS_C_INDEFINITE;

    /*
     * XXX This function is more complex than it has to be.  It should call
     * _gsskrb5_inquire_cred_by_mech() twice and merge the results in the
     * cred_handle == GSS_C_NO_CREDENTIAL case, but since
     * _gsskrb5_inquire_cred_by_mech() is implemented in terms of this
     * function, first we must fix _gsskrb5_inquire_cred_by_mech().
     */

    *minor_status = 0;

    if (output_name)
        *output_name = GSS_C_NO_NAME;
    if (cred_usage)
        *cred_usage = GSS_C_BOTH; /* There's no NONE */
    if (mechanisms)
        *mechanisms = GSS_C_NO_OID_SET;

    GSSAPI_KRB5_INIT (&context);

    if (cred_handle == GSS_C_NO_CREDENTIAL) {
        /*
         * From here to the end of this if we should refactor into a separate
         * function.
         */
        /* Get the info for the default ACCEPT credential */
        aret = _gsskrb5_acquire_cred_from(&aminor,
					  GSS_C_NO_NAME,
					  GSS_C_INDEFINITE,
					  GSS_C_NO_OID_SET,
					  GSS_C_ACCEPT,
					  GSS_C_NO_CRED_STORE,
					  &aqcred_accept,
					  NULL,
					  NULL);
        if (aret == GSS_S_COMPLETE) {
            aret = _gsskrb5_inquire_cred(&aminor,
                                        aqcred_accept,
                                        output_name,
                                        &alife,
                                        NULL,
                                        &amechs);
            (void) _gsskrb5_release_cred(&junk, &aqcred_accept);
            if (aret == GSS_S_COMPLETE) {
                output_name = NULL; /* Can't merge names; output only one */
                if (cred_usage)
                    *cred_usage = GSS_C_ACCEPT;
                if (lifetime)
                    *lifetime = alife;
                if (mechanisms) {
                    *mechanisms = amechs;
                    amechs = GSS_C_NO_OID_SET;
                }
                (void) gss_release_oid_set(&junk, &amechs);
            } else if (aret != GSS_S_NO_CRED) {
                *minor_status = aminor;
                return aret;
            } else {
                alife = GSS_C_INDEFINITE;
            }
        }

        /* Get the info for the default INITIATE credential */
        ret = _gsskrb5_acquire_cred_from(minor_status,
					 GSS_C_NO_NAME,
					 GSS_C_INDEFINITE,
					 GSS_C_NO_OID_SET,
					 GSS_C_INITIATE,
					 GSS_C_NO_CRED_STORE,
					 &aqcred_init,
					 NULL,
					 NULL);
        if (ret == GSS_S_COMPLETE) {
            ret = _gsskrb5_inquire_cred(minor_status,
                                        aqcred_init,
                                        output_name,
                                        &ilife,
                                        NULL,
                                        &imechs);
            (void) _gsskrb5_release_cred(&junk, &aqcred_init);
            if (ret == GSS_S_COMPLETE) {
                /*
                 * Merge results for INITIATE with ACCEPT if we had ACCEPT and
                 * for those outputs that are desired.
                 */
                if (cred_usage) {
                    *cred_usage = (*cred_usage == GSS_C_ACCEPT) ?
                        GSS_C_BOTH : GSS_C_INITIATE;
                }
                if (lifetime)
                    *lifetime = min(alife, ilife);
                if (mechanisms) {
                    /*
                     * This is just one mechanism (IAKERB and such would live
                     * elsewhere).  imechs will be equal to amechs, though not
                     * ==.
                     */
                    if (aret != GSS_S_COMPLETE) {
                        *mechanisms = imechs;
                        imechs = GSS_C_NO_OID_SET;
                    }
                }
                (void) gss_release_oid_set(&junk, &amechs);
            } else if (ret != GSS_S_NO_CRED) {
                *minor_status = aminor;
                return aret;
            }
        }

        if (aret != GSS_S_COMPLETE && ret != GSS_S_COMPLETE) {
            *minor_status = aminor;
            return aret;
        }
        *minor_status = 0; /* Even though 0 is not specified to be special */
        return GSS_S_COMPLETE;
    }

    HEIMDAL_MUTEX_lock(&cred->cred_id_mutex);

    if (output_name != NULL) {
        if (cred->principal != NULL) {
            gss_name_t name = (gss_name_t)cred->principal;
            ret = _gsskrb5_duplicate_name(minor_status, name, output_name);
            if (ret)
                goto out;
        } else if (cred->usage == GSS_C_ACCEPT) {
            /*
             * Keytab case, princ may not be set (yet, ever, whatever).
             *
             * We used to unconditionally output the krb5_sname_to_principal()
             * of the host service for the hostname, but we didn't know if we
             * had keytab entries for it, so it was incorrect.  We can't be
             * breaking anything in tree by outputting GSS_C_NO_NAME, but we
             * might be breaking other callers.
             */
            *output_name = GSS_C_NO_NAME;
        } else {
            /* This shouldn't happen */
            *minor_status = KRB5_NOCREDS_SUPPLIED; /* XXX */
            ret = GSS_S_NO_CRED;
            goto out;
        }
    }
    if (lifetime != NULL) {
        ret = _gsskrb5_lifetime_left(minor_status,
                                     context,
                                     cred->endtime,
                                     lifetime);
        if (ret)
            goto out;
    }
    if (cred_usage != NULL)
        *cred_usage = cred->usage;
    if (mechanisms != NULL) {
        ret = gss_create_empty_oid_set(minor_status, mechanisms);
        if (ret)
            goto out;
        ret = gss_add_oid_set_member(minor_status,
                                     &cred->mechanisms->elements[0],
                                     mechanisms);
        if (ret)
            goto out;
    }
    ret = GSS_S_COMPLETE;

out:
    HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex);
    return ret;
}