Merge branch 'master' of ssh://moonshot.suchdamage.org:822/srv/git/moonshot
[mech_eap.git] / util_cred.c
1 /*
2  * Copyright (c) 2010, JANET(UK)
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 JANET(UK) 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 THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND 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 THE COPYRIGHT HOLDER 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 /*
34  * Utility routines for credential handles.
35  */
36
37 #include "gssapiP_eap.h"
38
39 OM_uint32
40 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred)
41 {
42     OM_uint32 tmpMinor;
43     gss_cred_id_t cred;
44
45     *pCred = GSS_C_NO_CREDENTIAL;
46
47     cred = (gss_cred_id_t)GSSEAP_CALLOC(1, sizeof(*cred));
48     if (cred == NULL) {
49         *minor = ENOMEM;
50         return GSS_S_FAILURE;
51     }
52
53     if (GSSEAP_MUTEX_INIT(&cred->mutex) != 0) {
54         *minor = errno;
55         gssEapReleaseCred(&tmpMinor, &cred);
56         return GSS_S_FAILURE;
57     }
58
59     *pCred = cred;
60
61     *minor = 0;
62     return GSS_S_COMPLETE;
63 }
64
65 OM_uint32
66 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred)
67 {
68     OM_uint32 tmpMinor;
69     gss_cred_id_t cred = *pCred;
70     krb5_context krbContext = NULL;
71
72     if (cred == GSS_C_NO_CREDENTIAL) {
73         return GSS_S_COMPLETE;
74     }
75
76     GSSEAP_KRB_INIT(&krbContext);
77
78     gssEapReleaseName(&tmpMinor, &cred->name);
79
80     if (cred->password.value != NULL) {
81         memset(cred->password.value, 0, cred->password.length);
82         GSSEAP_FREE(cred->password.value);
83     }
84
85     if (cred->radiusConfigFile != NULL)
86         GSSEAP_FREE(cred->radiusConfigFile);
87     if (cred->radiusConfigStanza != NULL)
88         GSSEAP_FREE(cred->radiusConfigStanza);
89
90 #ifdef GSSEAP_ENABLE_REAUTH
91     if (cred->krbCredCache != NULL) {
92         if (cred->flags & CRED_FLAG_DEFAULT_CCACHE)
93             krb5_cc_close(krbContext, cred->krbCredCache);
94         else
95             krb5_cc_destroy(krbContext, cred->krbCredCache);
96     }
97     if (cred->krbCred != GSS_C_NO_CREDENTIAL)
98         gssReleaseCred(&tmpMinor, &cred->krbCred);
99 #endif
100
101     GSSEAP_MUTEX_DESTROY(&cred->mutex);
102     memset(cred, 0, sizeof(*cred));
103     GSSEAP_FREE(cred);
104     *pCred = NULL;
105
106     *minor = 0;
107     return GSS_S_COMPLETE;
108 }
109
110 OM_uint32
111 gssEapAcquireCred(OM_uint32 *minor,
112                   const gss_name_t desiredName,
113                   const gss_buffer_t password,
114                   OM_uint32 timeReq,
115                   const gss_OID_set desiredMechs,
116                   int credUsage,
117                   gss_cred_id_t *pCred,
118                   gss_OID_set *pActualMechs,
119                   OM_uint32 *timeRec)
120 {
121     OM_uint32 major, tmpMinor;
122     gss_cred_id_t cred;
123
124     /* XXX TODO validate with changed set_cred_option API */
125     *pCred = GSS_C_NO_CREDENTIAL;
126
127     major = gssEapAllocCred(minor, &cred);
128     if (GSS_ERROR(major))
129         goto cleanup;
130
131     if (desiredName != GSS_C_NO_NAME) {
132         GSSEAP_MUTEX_LOCK(&desiredName->mutex);
133
134         major = gssEapDuplicateName(minor, desiredName, &cred->name);
135         if (GSS_ERROR(major)) {
136             GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
137             goto cleanup;
138         }
139
140         GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
141     } else {
142         gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
143         gss_OID nameType = GSS_C_NO_OID;
144
145         if (cred->flags & CRED_FLAG_ACCEPT) {
146             char serviceName[5 + MAXHOSTNAMELEN] = "host@";
147
148             /* default host-based service is host@localhost */
149             if (gethostname(&serviceName[5], MAXHOSTNAMELEN) != 0) {
150                 major = GSS_S_FAILURE;
151                 *minor = GSSEAP_NO_HOSTNAME;
152                 goto cleanup;
153             }
154
155             nameBuf.value = serviceName;
156             nameBuf.length = strlen((char *)nameBuf.value);
157
158             nameType = GSS_C_NT_HOSTBASED_SERVICE;
159         } else if (cred->flags & CRED_FLAG_INITIATE) {
160             nameBuf.value = getlogin(); /* XXX */
161             nameBuf.length = strlen((char *)nameBuf.value);
162
163             nameType = GSS_C_NT_USER_NAME;
164         }
165
166         if (nameBuf.length != 0) {
167             major = gssEapImportName(minor, &nameBuf, nameType, &cred->name);
168             if (GSS_ERROR(major))
169                 goto cleanup;
170         }
171
172         cred->flags |= CRED_FLAG_DEFAULT_IDENTITY;
173     }
174
175     if (password != GSS_C_NO_BUFFER) {
176         major = duplicateBuffer(minor, password, &cred->password);
177         if (GSS_ERROR(major))
178             goto cleanup;
179
180         cred->flags |= CRED_FLAG_PASSWORD;
181     } else if (credUsage == GSS_C_INITIATE) {
182         /*
183          * OK, here we need to ask the supplicant if we have creds or it
184          * will acquire them, so GS2 can know whether to prompt for a
185          * password or not.
186          */
187 #if 0
188         && !gssEapCanReauthP(cred, GSS_C_NO_NAME, timeReq)
189 #endif
190         major = GSS_S_CRED_UNAVAIL;
191         goto cleanup;
192     }
193
194     switch (credUsage) {
195     case GSS_C_BOTH:
196         cred->flags |= CRED_FLAG_INITIATE | CRED_FLAG_ACCEPT;
197         break;
198     case GSS_C_INITIATE:
199         cred->flags |= CRED_FLAG_INITIATE;
200         break;
201     case GSS_C_ACCEPT:
202         cred->flags |= CRED_FLAG_ACCEPT;
203         break;
204     default:
205         major = GSS_S_FAILURE;
206         *minor = GSSEAP_BAD_USAGE;
207         goto cleanup;
208         break;
209     }
210
211     major = gssEapValidateMechs(minor, desiredMechs);
212     if (GSS_ERROR(major))
213         goto cleanup;
214
215     major = duplicateOidSet(minor, desiredMechs, &cred->mechanisms);
216     if (GSS_ERROR(major))
217         goto cleanup;
218
219     if (pActualMechs != NULL) {
220         major = duplicateOidSet(minor, cred->mechanisms, pActualMechs);
221         if (GSS_ERROR(major))
222             goto cleanup;
223     }
224
225     if (timeRec != NULL)
226         *timeRec = GSS_C_INDEFINITE;
227
228     *pCred = cred;
229
230     major = GSS_S_COMPLETE;
231     *minor = 0;
232
233 cleanup:
234     if (GSS_ERROR(major))
235         gssEapReleaseCred(&tmpMinor, &cred);
236
237     return major;
238 }
239
240 /*
241  * Return TRUE if cred available for mechanism. Caller need no acquire
242  * lock because mechanisms list is immutable.
243  */
244 int
245 gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech)
246 {
247     OM_uint32 minor;
248     int present = 0;
249
250     assert(mech != GSS_C_NO_OID);
251
252     if (cred == GSS_C_NO_CREDENTIAL || cred->mechanisms == GSS_C_NO_OID_SET)
253         return TRUE;
254
255     gss_test_oid_set_member(&minor, mech, cred->mechanisms, &present);
256
257     return present;
258 }