1a18911eb71c77445edb09ea517ce7eafc339857
[mech_eap.orig] / util_cred.c
1 /*
2  * Copyright (c) 2011, 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 #include <pwd.h>
40
41 OM_uint32
42 gssEapAllocCred(OM_uint32 *minor, gss_cred_id_t *pCred)
43 {
44     OM_uint32 tmpMinor;
45     gss_cred_id_t cred;
46
47     *pCred = GSS_C_NO_CREDENTIAL;
48
49     cred = (gss_cred_id_t)GSSEAP_CALLOC(1, sizeof(*cred));
50     if (cred == NULL) {
51         *minor = ENOMEM;
52         return GSS_S_FAILURE;
53     }
54
55     if (GSSEAP_MUTEX_INIT(&cred->mutex) != 0) {
56         *minor = errno;
57         gssEapReleaseCred(&tmpMinor, &cred);
58         return GSS_S_FAILURE;
59     }
60
61     *pCred = cred;
62
63     *minor = 0;
64     return GSS_S_COMPLETE;
65 }
66
67 OM_uint32
68 gssEapReleaseCred(OM_uint32 *minor, gss_cred_id_t *pCred)
69 {
70     OM_uint32 tmpMinor;
71     gss_cred_id_t cred = *pCred;
72     krb5_context krbContext = NULL;
73
74     if (cred == GSS_C_NO_CREDENTIAL) {
75         return GSS_S_COMPLETE;
76     }
77
78     GSSEAP_KRB_INIT(&krbContext);
79
80     gssEapReleaseName(&tmpMinor, &cred->name);
81
82     if (cred->password.value != NULL) {
83         memset(cred->password.value, 0, cred->password.length);
84         GSSEAP_FREE(cred->password.value);
85     }
86
87     if (cred->radiusConfigFile != NULL)
88         GSSEAP_FREE(cred->radiusConfigFile);
89     if (cred->radiusConfigStanza != NULL)
90         GSSEAP_FREE(cred->radiusConfigStanza);
91
92 #ifdef GSSEAP_ENABLE_REAUTH
93     if (cred->krbCredCache != NULL) {
94         if (cred->flags & CRED_FLAG_DEFAULT_CCACHE)
95             krb5_cc_close(krbContext, cred->krbCredCache);
96         else
97             krb5_cc_destroy(krbContext, cred->krbCredCache);
98     }
99     if (cred->krbCred != GSS_C_NO_CREDENTIAL)
100         gssReleaseCred(&tmpMinor, &cred->krbCred);
101 #endif
102
103     GSSEAP_MUTEX_DESTROY(&cred->mutex);
104     memset(cred, 0, sizeof(*cred));
105     GSSEAP_FREE(cred);
106     *pCred = NULL;
107
108     *minor = 0;
109     return GSS_S_COMPLETE;
110 }
111
112 static OM_uint32
113 readDefaultIdentityAndCreds(OM_uint32 *minor,
114                             gss_buffer_t defaultIdentity,
115                             gss_buffer_t defaultCreds)
116 {
117     OM_uint32 major;
118     FILE *fp = NULL;
119     char pwbuf[BUFSIZ], buf[BUFSIZ];
120     char *ccacheName;
121     struct passwd *pw = NULL, pwd;
122
123     defaultIdentity->length = 0;
124     defaultIdentity->value = NULL;
125
126     defaultCreds->length = 0;
127     defaultCreds->value = NULL;
128
129     ccacheName = getenv("GSSEAP_IDENTITY");
130     if (ccacheName == NULL) {
131         if (getpwuid_r(getuid(), &pwd, pwbuf, sizeof(pwbuf), &pw) != 0 ||
132             pw == NULL || pw->pw_dir == NULL) {
133             major = GSS_S_CRED_UNAVAIL;
134             *minor = errno;
135             goto cleanup;
136         }
137
138         snprintf(buf, sizeof(buf), "%s/.gss_eap_id", pw->pw_dir);
139         ccacheName = buf;
140     }
141
142     fp = fopen(ccacheName, "r");
143     if (fp == NULL) {
144         *minor = GSSEAP_NO_DEFAULT_CRED;
145         major = GSS_S_CRED_UNAVAIL;
146         goto cleanup;
147     }
148
149     while (fgets(buf, sizeof(buf), fp) != NULL) {
150         gss_buffer_desc src, *dst;
151
152         src.length = strlen(buf);
153         src.value = buf;
154
155         if (src.length == 0)
156             break;
157
158         if (buf[src.length - 1] == '\n') {
159             buf[src.length - 1] = '\0';
160             if (--src.length == 0)
161                 break;
162         }
163
164         if (defaultIdentity->value == NULL)
165             dst = defaultIdentity;
166         else if (defaultCreds->value == NULL)
167             dst = defaultCreds;
168         else
169             break;
170
171         major = duplicateBuffer(minor, &src, dst);
172         if (GSS_ERROR(major))
173             goto cleanup;
174     }
175
176     if (defaultIdentity->length == 0) {
177         major = GSS_S_CRED_UNAVAIL;
178         *minor = GSSEAP_NO_DEFAULT_CRED;
179         goto cleanup;
180     }
181
182     major = GSS_S_COMPLETE;
183     *minor = 0;
184
185 cleanup:
186     if (fp != NULL)
187         fclose(fp);
188
189     return major;
190 }
191
192 OM_uint32
193 gssEapAcquireCred(OM_uint32 *minor,
194                   const gss_name_t desiredName,
195                   const gss_buffer_t password,
196                   OM_uint32 timeReq GSSEAP_UNUSED,
197                   const gss_OID_set desiredMechs,
198                   int credUsage,
199                   gss_cred_id_t *pCred,
200                   gss_OID_set *pActualMechs,
201                   OM_uint32 *timeRec)
202 {
203     OM_uint32 major, tmpMinor;
204     gss_cred_id_t cred;
205     gss_buffer_desc defaultCreds = GSS_C_EMPTY_BUFFER;
206
207     /* XXX TODO validate with changed set_cred_option API */
208     *pCred = GSS_C_NO_CREDENTIAL;
209
210     major = gssEapAllocCred(minor, &cred);
211     if (GSS_ERROR(major))
212         goto cleanup;
213
214     switch (credUsage) {
215     case GSS_C_BOTH:
216         cred->flags |= CRED_FLAG_INITIATE | CRED_FLAG_ACCEPT;
217         break;
218     case GSS_C_INITIATE:
219         cred->flags |= CRED_FLAG_INITIATE;
220         break;
221     case GSS_C_ACCEPT:
222         cred->flags |= CRED_FLAG_ACCEPT;
223         break;
224     default:
225         major = GSS_S_FAILURE;
226         *minor = GSSEAP_BAD_USAGE;
227         goto cleanup;
228         break;
229     }
230
231     if (desiredName != GSS_C_NO_NAME) {
232         GSSEAP_MUTEX_LOCK(&desiredName->mutex);
233
234         major = gssEapDuplicateName(minor, desiredName, &cred->name);
235         if (GSS_ERROR(major)) {
236             GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
237             goto cleanup;
238         }
239
240         GSSEAP_MUTEX_UNLOCK(&desiredName->mutex);
241     } else {
242         gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
243         gss_OID nameType = GSS_C_NO_OID;
244         char serviceName[5 + MAXHOSTNAMELEN];
245
246         if (cred->flags & CRED_FLAG_ACCEPT) {
247             /* default host-based service is host@localhost */
248             memcpy(serviceName, "host@", 5);
249             if (gethostname(&serviceName[5], MAXHOSTNAMELEN) != 0) {
250                 major = GSS_S_FAILURE;
251                 *minor = GSSEAP_NO_HOSTNAME;
252                 goto cleanup;
253             }
254
255             nameBuf.value = serviceName;
256             nameBuf.length = strlen((char *)nameBuf.value);
257
258             nameType = GSS_C_NT_HOSTBASED_SERVICE;
259         } else if (cred->flags & CRED_FLAG_INITIATE) {
260             major = readDefaultIdentityAndCreds(minor, &nameBuf, &defaultCreds);
261             if (GSS_ERROR(major))
262                 goto cleanup;
263
264             nameType = GSS_C_NT_USER_NAME;
265         }
266
267         if (nameBuf.length != 0) {
268             gss_OID mech = GSS_C_NO_OID;
269
270             if (cred->mechanisms != GSS_C_NO_OID_SET &&
271                 cred->mechanisms->count == 1)
272                 mech = &cred->mechanisms->elements[0];
273
274             major = gssEapImportName(minor, &nameBuf, nameType, mech, &cred->name);
275             if (GSS_ERROR(major))
276                 goto cleanup;
277         }
278
279         if (nameBuf.value != serviceName)
280             gss_release_buffer(&tmpMinor, &nameBuf);
281
282         cred->flags |= CRED_FLAG_DEFAULT_IDENTITY;
283     }
284
285     if (password != GSS_C_NO_BUFFER) {
286         major = duplicateBuffer(minor, password, &cred->password);
287         if (GSS_ERROR(major))
288             goto cleanup;
289
290         cred->flags |= CRED_FLAG_PASSWORD;
291     } else if (defaultCreds.value != NULL) {
292         cred->password = defaultCreds;
293
294         defaultCreds.length = 0;
295         defaultCreds.value = NULL;
296
297         cred->flags |= CRED_FLAG_PASSWORD;
298     } else if (cred->flags & CRED_FLAG_INITIATE) {
299         /*
300          * OK, here we need to ask the supplicant if we have creds or it
301          * will acquire them, so GS2 can know whether to prompt for a
302          * password or not.
303          */
304 #if 0
305         && !gssEapCanReauthP(cred, GSS_C_NO_NAME, timeReq)
306 #endif
307         major = GSS_S_CRED_UNAVAIL;
308         *minor = GSSEAP_NO_DEFAULT_CRED;
309         goto cleanup;
310     }
311
312     major = gssEapValidateMechs(minor, desiredMechs);
313     if (GSS_ERROR(major))
314         goto cleanup;
315
316     major = duplicateOidSet(minor, desiredMechs, &cred->mechanisms);
317     if (GSS_ERROR(major))
318         goto cleanup;
319
320     if (pActualMechs != NULL) {
321         major = duplicateOidSet(minor, cred->mechanisms, pActualMechs);
322         if (GSS_ERROR(major))
323             goto cleanup;
324     }
325
326     if (timeRec != NULL)
327         *timeRec = GSS_C_INDEFINITE;
328
329     *pCred = cred;
330
331     major = GSS_S_COMPLETE;
332     *minor = 0;
333
334 cleanup:
335     if (GSS_ERROR(major))
336         gssEapReleaseCred(&tmpMinor, &cred);
337     if (defaultCreds.value != NULL) {
338         memset(defaultCreds.value, 0, defaultCreds.length);
339         gss_release_buffer(&tmpMinor, &defaultCreds);
340     }
341
342     return major;
343 }
344
345 /*
346  * Return TRUE if cred available for mechanism. Caller need no acquire
347  * lock because mechanisms list is immutable.
348  */
349 int
350 gssEapCredAvailable(gss_cred_id_t cred, gss_OID mech)
351 {
352     OM_uint32 minor;
353     int present = 0;
354
355     assert(mech != GSS_C_NO_OID);
356
357     if (cred == GSS_C_NO_CREDENTIAL || cred->mechanisms == GSS_C_NO_OID_SET)
358         return TRUE;
359
360     gss_test_oid_set_member(&minor, mech, cred->mechanisms, &present);
361
362     return present;
363 }