| 1 |
/* |
| 2 |
* Copyright (c) 1997, 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 "gsskrb5_locl.h" |
| 35 |
|
| 36 |
OM_uint32 GSSAPI_CALLCONV _gsskrb5_inquire_cred |
| 37 |
(OM_uint32 * minor_status, |
| 38 |
gss_const_cred_id_t cred_handle, |
| 39 |
gss_name_t * output_name, |
| 40 |
OM_uint32 * lifetime, |
| 41 |
gss_cred_usage_t * cred_usage, |
| 42 |
gss_OID_set * mechanisms |
| 43 |
) |
| 44 |
{ |
| 45 |
krb5_context context; |
| 46 |
gss_cred_id_t aqcred_init = GSS_C_NO_CREDENTIAL; |
| 47 |
gss_cred_id_t aqcred_accept = GSS_C_NO_CREDENTIAL; |
| 48 |
gsskrb5_cred cred = (gsskrb5_cred)cred_handle; |
| 49 |
gss_OID_set amechs = GSS_C_NO_OID_SET; |
| 50 |
gss_OID_set imechs = GSS_C_NO_OID_SET; |
| 51 |
OM_uint32 junk; |
| 52 |
OM_uint32 aminor; |
| 53 |
OM_uint32 ret; |
| 54 |
OM_uint32 aret; |
| 55 |
OM_uint32 alife = GSS_C_INDEFINITE; |
| 56 |
OM_uint32 ilife = GSS_C_INDEFINITE; |
| 57 |
|
| 58 |
/* |
| 59 |
* XXX This function is more complex than it has to be. It should call |
| 60 |
* _gsskrb5_inquire_cred_by_mech() twice and merge the results in the |
| 61 |
* cred_handle == GSS_C_NO_CREDENTIAL case, but since |
| 62 |
* _gsskrb5_inquire_cred_by_mech() is implemented in terms of this |
| 63 |
* function, first we must fix _gsskrb5_inquire_cred_by_mech(). |
| 64 |
*/ |
| 65 |
|
| 66 |
*minor_status = 0; |
| 67 |
|
| 68 |
if (output_name) |
| 69 |
*output_name = GSS_C_NO_NAME; |
| 70 |
if (cred_usage) |
| 71 |
*cred_usage = GSS_C_BOTH; /* There's no NONE */ |
| 72 |
if (mechanisms) |
| 73 |
*mechanisms = GSS_C_NO_OID_SET; |
| 74 |
|
| 75 |
GSSAPI_KRB5_INIT (&context); |
| 76 |
|
| 77 |
if (cred_handle == GSS_C_NO_CREDENTIAL) { |
| 78 |
/* |
| 79 |
* From here to the end of this if we should refactor into a separate |
| 80 |
* function. |
| 81 |
*/ |
| 82 |
/* Get the info for the default ACCEPT credential */ |
| 83 |
aret = _gsskrb5_acquire_cred(&aminor, |
| 84 |
GSS_C_NO_NAME, |
| 85 |
GSS_C_INDEFINITE, |
| 86 |
GSS_C_NO_OID_SET, |
| 87 |
GSS_C_ACCEPT, |
| 88 |
&aqcred_accept, |
| 89 |
NULL, |
| 90 |
NULL); |
| 91 |
if (aret == GSS_S_COMPLETE) { |
| 92 |
aret = _gsskrb5_inquire_cred(&aminor, |
| 93 |
aqcred_accept, |
| 94 |
output_name, |
| 95 |
&alife, |
| 96 |
NULL, |
| 97 |
&amechs); |
| 98 |
(void) _gsskrb5_release_cred(&junk, &aqcred_accept); |
| 99 |
if (aret == GSS_S_COMPLETE) { |
| 100 |
output_name = NULL; /* Can't merge names; output only one */ |
| 101 |
if (cred_usage) |
| 102 |
*cred_usage = GSS_C_ACCEPT; |
| 103 |
if (lifetime) |
| 104 |
*lifetime = alife; |
| 105 |
if (mechanisms) { |
| 106 |
*mechanisms = amechs; |
| 107 |
amechs = GSS_C_NO_OID_SET; |
| 108 |
} |
| 109 |
(void) gss_release_oid_set(&junk, &amechs); |
| 110 |
} else if (aret != GSS_S_NO_CRED) { |
| 111 |
*minor_status = aminor; |
| 112 |
return aret; |
| 113 |
} else { |
| 114 |
alife = GSS_C_INDEFINITE; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/* Get the info for the default INITIATE credential */ |
| 119 |
ret = _gsskrb5_acquire_cred(minor_status, |
| 120 |
GSS_C_NO_NAME, |
| 121 |
GSS_C_INDEFINITE, |
| 122 |
GSS_C_NO_OID_SET, |
| 123 |
GSS_C_INITIATE, |
| 124 |
&aqcred_init, |
| 125 |
NULL, |
| 126 |
NULL); |
| 127 |
if (ret == GSS_S_COMPLETE) { |
| 128 |
ret = _gsskrb5_inquire_cred(minor_status, |
| 129 |
aqcred_init, |
| 130 |
output_name, |
| 131 |
&ilife, |
| 132 |
NULL, |
| 133 |
&imechs); |
| 134 |
(void) _gsskrb5_release_cred(&junk, &aqcred_init); |
| 135 |
if (ret == GSS_S_COMPLETE) { |
| 136 |
/* |
| 137 |
* Merge results for INITIATE with ACCEPT if we had ACCEPT and |
| 138 |
* for those outputs that are desired. |
| 139 |
*/ |
| 140 |
if (cred_usage) { |
| 141 |
*cred_usage = (*cred_usage == GSS_C_ACCEPT) ? |
| 142 |
GSS_C_BOTH : GSS_C_INITIATE; |
| 143 |
} |
| 144 |
if (lifetime) |
| 145 |
*lifetime = min(alife, ilife); |
| 146 |
if (mechanisms) { |
| 147 |
/* |
| 148 |
* This is just one mechanism (IAKERB and such would live |
| 149 |
* elsewhere). imechs will be equal to amechs, though not |
| 150 |
* ==. |
| 151 |
*/ |
| 152 |
if (aret != GSS_S_COMPLETE) { |
| 153 |
*mechanisms = imechs; |
| 154 |
imechs = GSS_C_NO_OID_SET; |
| 155 |
} |
| 156 |
} |
| 157 |
(void) gss_release_oid_set(&junk, &amechs); |
| 158 |
} else if (ret != GSS_S_NO_CRED) { |
| 159 |
*minor_status = aminor; |
| 160 |
return aret; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
if (aret != GSS_S_COMPLETE && ret != GSS_S_COMPLETE) { |
| 165 |
*minor_status = aminor; |
| 166 |
return aret; |
| 167 |
} |
| 168 |
*minor_status = 0; /* Even though 0 is not specified to be special */ |
| 169 |
return GSS_S_COMPLETE; |
| 170 |
} |
| 171 |
|
| 172 |
HEIMDAL_MUTEX_lock(&cred->cred_id_mutex); |
| 173 |
|
| 174 |
if (output_name != NULL) { |
| 175 |
if (cred->principal != NULL) { |
| 176 |
gss_name_t name = (gss_name_t)cred->principal; |
| 177 |
ret = _gsskrb5_duplicate_name(minor_status, name, output_name); |
| 178 |
if (ret) |
| 179 |
goto out; |
| 180 |
} else if (cred->usage == GSS_C_ACCEPT) { |
| 181 |
/* |
| 182 |
* Keytab case, princ may not be set (yet, ever, whatever). |
| 183 |
* |
| 184 |
* We used to unconditionally output the krb5_sname_to_principal() |
| 185 |
* of the host service for the hostname, but we didn't know if we |
| 186 |
* had keytab entries for it, so it was incorrect. We can't be |
| 187 |
* breaking anything in tree by outputting GSS_C_NO_NAME, but we |
| 188 |
* might be breaking other callers. |
| 189 |
*/ |
| 190 |
*output_name = GSS_C_NO_NAME; |
| 191 |
} else { |
| 192 |
/* This shouldn't happen */ |
| 193 |
*minor_status = KRB5_NOCREDS_SUPPLIED; /* XXX */ |
| 194 |
ret = GSS_S_NO_CRED; |
| 195 |
goto out; |
| 196 |
} |
| 197 |
} |
| 198 |
if (lifetime != NULL) { |
| 199 |
ret = _gsskrb5_lifetime_left(minor_status, |
| 200 |
context, |
| 201 |
cred->endtime, |
| 202 |
lifetime); |
| 203 |
if (ret) |
| 204 |
goto out; |
| 205 |
} |
| 206 |
if (cred_usage != NULL) |
| 207 |
*cred_usage = cred->usage; |
| 208 |
if (mechanisms != NULL) { |
| 209 |
ret = gss_create_empty_oid_set(minor_status, mechanisms); |
| 210 |
if (ret) |
| 211 |
goto out; |
| 212 |
ret = gss_add_oid_set_member(minor_status, |
| 213 |
&cred->mechanisms->elements[0], |
| 214 |
mechanisms); |
| 215 |
if (ret) |
| 216 |
goto out; |
| 217 |
} |
| 218 |
ret = GSS_S_COMPLETE; |
| 219 |
|
| 220 |
out: |
| 221 |
HEIMDAL_MUTEX_unlock(&cred->cred_id_mutex); |
| 222 |
return ret; |
| 223 |
} |