return RADIUS attributes as integers
[mech_eap.git] / util_radius.cpp
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 #include "gssapiP_eap.h"
34
35 VALUE_PAIR *
36 gss_eap_radius_attr_provider::copyAvps(const VALUE_PAIR *src)
37 {
38     const VALUE_PAIR *vp;
39     VALUE_PAIR *dst = NULL, **pDst = &dst;
40
41     for (vp = src; vp != NULL; vp = vp->next) {
42         VALUE_PAIR *vp2;
43
44         vp2 = (VALUE_PAIR *)GSSEAP_CALLOC(1, sizeof(*vp2));
45         if (vp2 == NULL) {
46             rc_avpair_free(dst);
47             return NULL;
48         }
49         memcpy(vp2, vp, sizeof(*vp));
50         vp2->next = NULL;
51         *pDst = vp2;
52         pDst = &vp2->next;
53     }
54
55     return dst;
56 }
57
58 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
59 {
60     m_rh = NULL;
61     m_avps = NULL;
62     m_authenticated = false;
63 }
64
65 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
66 {
67     if (m_rh != NULL)
68         rc_config_free(m_rh);
69     if (m_avps != NULL)
70         rc_avpair_free(m_avps);
71 }
72
73 bool
74 gss_eap_radius_attr_provider::initFromGssCred(const gss_cred_id_t cred)
75 {
76     OM_uint32 minor;
77
78     return !GSS_ERROR(gssEapRadiusAllocHandle(&minor, cred, &m_rh));
79 }
80
81 bool
82 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
83                                                       const gss_eap_attr_provider *ctx)
84 {
85     const gss_eap_radius_attr_provider *radius;
86
87     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
88         return false;
89
90     if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
91         return false;
92
93     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
94     if (radius->m_avps != NULL) {
95         m_avps = copyAvps(radius->getAvps());
96     }
97
98     return true;
99 }
100
101 bool
102 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
103                                                  const gss_cred_id_t gssCred,
104                                                  const gss_ctx_id_t gssCtx)
105 {
106     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
107         return false;
108
109     if (!initFromGssCred(gssCred))
110         return false;
111
112     if (gssCtx != GSS_C_NO_CONTEXT) {
113         if (gssCtx->acceptorCtx.avps != NULL) {
114             m_avps = copyAvps(gssCtx->acceptorCtx.avps);
115             if (m_avps == NULL)
116                 return false;
117         }
118     }
119
120     return true;
121 }
122
123 static bool
124 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
125 {
126     for (std::vector<std::string>::const_iterator a = attrs.begin();
127          a != attrs.end();
128          ++a) {
129         if (strcmp(vp->name, (*a).c_str()) == 0)
130             return true;
131     }
132
133     return false;
134 }
135
136 static bool
137 isHiddenAttributeP(int attrid, int vendor)
138 {
139     bool ret = false;
140
141     switch (vendor) {
142     case RADIUS_VENDOR_ID_MICROSOFT:
143         switch (attrid) {
144         case RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY:
145         case RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY:
146             ret = true;
147             break;
148         default:
149             break;
150         }
151     case RADIUS_VENDOR_ID_GSS_EAP:
152         ret = true;
153         break;
154     default:
155         break;
156     }
157
158     return ret;
159 }
160
161 bool
162 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
163 {
164     VALUE_PAIR *vp;
165     std::vector <std::string> seen;
166
167     for (vp = m_avps; vp != NULL; vp = vp->next) {
168         gss_buffer_desc attribute;
169 #ifndef RADIUS_STRING_ATTRS
170         char attrid[64];
171 #endif
172         if (isHiddenAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute)))
173             continue;
174
175         if (alreadyAddedAttributeP(seen, vp))
176             continue;
177
178 #ifdef RADIUS_STRING_ATTRS
179         attribute.value = (void *)vp->name;
180         attribute.length = strlen(vp->name);
181 #else
182         snprintf(attrid, sizeof(attrid), "%d", vp->attribute);
183
184         attribute.value = attrid;
185         attribute.length = strlen(attrid);
186 #endif /* RADIUS_STRING_ATTRS */
187
188         if (!addAttribute(this, &attribute, data))
189             return false;
190
191         seen.push_back(std::string(vp->name));
192     }
193
194     return true;
195 }
196
197 void
198 gss_eap_radius_attr_provider::setAttribute(int complete,
199                                            const gss_buffer_t attr,
200                                            const gss_buffer_t value)
201 {
202 }
203
204 void
205 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
206 {
207 }
208
209 bool
210 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
211                                            int *authenticated,
212                                            int *complete,
213                                            gss_buffer_t value,
214                                            gss_buffer_t display_value,
215                                            int *more) const
216 {
217     OM_uint32 tmpMinor;
218     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
219     DICT_ATTR *d;
220     int attrid;
221     char *s;
222
223     /* XXX vendor */
224
225     duplicateBuffer(*attr, &strAttr);
226     s = (char *)strAttr.value;
227
228     if (isdigit(((char *)strAttr.value)[0])) {
229         attrid = strtoul(s, NULL, 10);
230     } else {
231         d = rc_dict_findattr(m_rh, (char *)s);
232         if (d == NULL) {
233             gss_release_buffer(&tmpMinor, &strAttr);
234             return false;
235         }
236         attrid = d->value;
237     }
238
239     gss_release_buffer(&tmpMinor, &strAttr);
240
241     return getAttribute(attrid, authenticated, complete,
242                         value, display_value, more);
243 }
244
245 static bool
246 isPrintableAttributeP(VALUE_PAIR *vp)
247 {
248     size_t i;
249     int gotChar = 0;
250
251     for (i = 0; i < sizeof(vp->strvalue); i++) {
252         if (gotChar && vp->strvalue[i] == '\0')
253             return true;
254
255         if (!isprint(vp->strvalue[i]))
256             return false;
257
258         if (!gotChar)
259             gotChar++;
260     }
261
262     return true;
263 }
264
265 bool
266 gss_eap_radius_attr_provider::getAttribute(int attrid,
267                                            int vendor,
268                                            int *authenticated,
269                                            int *complete,
270                                            gss_buffer_t value,
271                                            gss_buffer_t display_value,
272                                            int *more) const
273 {
274     OM_uint32 tmpMinor;
275     VALUE_PAIR *vp;
276     int i = *more, count = 0;
277     char name[NAME_LENGTH + 1];
278     char displayString[AUTH_STRING_LEN + 1];
279     gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
280     gss_buffer_desc displayBuf = GSS_C_EMPTY_BUFFER;
281
282     *more = 0;
283
284     if (isHiddenAttributeP(attrid, vendor))
285         return false;
286
287     if (i == -1)
288         i = 0;
289
290     for (vp = rc_avpair_get(m_avps, attrid, vendor);
291          vp != NULL;
292          vp = rc_avpair_get(vp->next, attrid, vendor)) {
293         if (count++ == i) {
294             if (rc_avpair_get(vp->next, attrid, vendor) != NULL)
295                 *more = count;
296             break;
297         }
298     }
299
300     if (vp == NULL && *more == 0)
301         return false;
302
303     if (vp->type == PW_TYPE_STRING) {
304         valueBuf.value = (void *)vp->strvalue;
305         valueBuf.length = vp->lvalue;
306     } else {
307         valueBuf.value = (void *)&vp->lvalue;
308         valueBuf.length = 4;
309     }
310
311     if (value != GSS_C_NO_BUFFER)
312         duplicateBuffer(valueBuf, value);
313
314     if (display_value != GSS_C_NO_BUFFER &&
315         isPrintableAttributeP(vp)) {
316         if (rc_avpair_tostr(m_rh, vp, name, NAME_LENGTH,
317                             displayString, AUTH_STRING_LEN) != 0) {
318             gss_release_buffer(&tmpMinor, value);
319             return false;
320         }
321
322         displayBuf.value = (void *)displayString;
323         displayBuf.length = strlen(displayString);
324
325         duplicateBuffer(displayBuf, display_value);
326     }
327
328     if (authenticated != NULL)
329         *authenticated = m_authenticated;
330     if (complete != NULL)
331         *complete = true;
332
333     return true;
334 }
335
336 bool
337 gss_eap_radius_attr_provider::getFragmentedAttribute(int attribute,
338                                                      int vendor,
339                                                      int *authenticated,
340                                                      int *complete,
341                                                      gss_buffer_t value) const
342 {
343     OM_uint32 major, minor;
344
345     major = getBufferFromAvps(&minor, m_avps, attribute, vendor, value, TRUE);
346
347     if (authenticated != NULL)
348         *authenticated = m_authenticated;
349     if (complete != NULL)
350         *complete = true;
351
352     return !GSS_ERROR(major);
353 }
354
355 bool
356 gss_eap_radius_attr_provider::getAttribute(int attrid,
357                                            int *authenticated,
358                                            int *complete,
359                                            gss_buffer_t value,
360                                            gss_buffer_t display_value,
361                                            int *more) const
362 {
363
364     return getAttribute(ATTRID(attrid), VENDOR(attrid),
365                         authenticated, complete,
366                         value, display_value, more);
367 }
368
369 gss_any_t
370 gss_eap_radius_attr_provider::mapToAny(int authenticated,
371                                        gss_buffer_t type_id) const
372 {
373     if (authenticated && !m_authenticated)
374         return (gss_any_t)NULL;
375
376     return (gss_any_t)copyAvps(m_avps);
377 }
378
379 void
380 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
381                                                     gss_any_t input) const
382 {
383     rc_avpair_free((VALUE_PAIR *)input);
384 }
385
386 bool
387 gss_eap_radius_attr_provider::init(void)
388 {
389     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
390                                        "urn:ietf:params:gss-eap:radius-avp",
391                                        gss_eap_radius_attr_provider::createAttrContext);
392     return true;
393 }
394
395 void
396 gss_eap_radius_attr_provider::finalize(void)
397 {
398     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
399 }
400
401 gss_eap_attr_provider *
402 gss_eap_radius_attr_provider::createAttrContext(void)
403 {
404     return new gss_eap_radius_attr_provider;
405 }
406
407 OM_uint32
408 addAvpFromBuffer(OM_uint32 *minor,
409                  rc_handle *rh,
410                  VALUE_PAIR **vp,
411                  int type,
412                  int vendor,
413                  gss_buffer_t buffer)
414 {
415     if (rc_avpair_add(rh, vp, type,
416                       buffer->value, buffer->length, vendor) == NULL) {
417         return GSS_S_FAILURE;
418     }
419
420     return GSS_S_COMPLETE;
421 }
422
423 OM_uint32
424 getBufferFromAvps(OM_uint32 *minor,
425                   VALUE_PAIR *vps,
426                   int type,
427                   int vendor,
428                   gss_buffer_t buffer,
429                   int concat)
430 {
431     VALUE_PAIR *vp;
432     unsigned char *p;
433
434     buffer->length = 0;
435     buffer->value = NULL;
436
437     vp = rc_avpair_get(vps, type, vendor);
438     if (vp == NULL)
439         return GSS_S_UNAVAILABLE;
440
441     do {
442         buffer->length += vp->lvalue;
443     } while (concat && (vp = rc_avpair_get(vp->next, type, vendor)) != NULL);
444
445     buffer->value = GSSEAP_MALLOC(buffer->length);
446     if (buffer->value == NULL) {
447         *minor = ENOMEM;
448         return GSS_S_FAILURE;
449     }
450
451     p = (unsigned char *)buffer->value;
452
453     for (vp = rc_avpair_get(vps, type, vendor);
454          concat && vp != NULL;
455          vp = rc_avpair_get(vp->next, type, vendor)) {
456         memcpy(p, vp->strvalue, vp->lvalue);
457         p += vp->lvalue;
458     }
459
460     *minor = 0;
461     return GSS_S_COMPLETE;
462 }
463
464 OM_uint32
465 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
466 {
467     return gss_eap_radius_attr_provider::init()
468         ? GSS_S_COMPLETE : GSS_S_FAILURE;
469 }
470
471 OM_uint32
472 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
473 {
474     gss_eap_radius_attr_provider::finalize();
475     return GSS_S_COMPLETE;
476 }
477
478 OM_uint32
479 gssEapRadiusAllocHandle(OM_uint32 *minor,
480                         const gss_cred_id_t cred,
481                         rc_handle **pHandle)
482 {
483     rc_handle *rh;
484     const char *config = RC_CONFIG_FILE;
485
486     *pHandle = NULL;
487
488     if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
489         config = cred->radiusConfigFile;
490
491     rh = rc_read_config((char *)config);
492     if (rh == NULL) {
493         *minor = errno;
494         rc_config_free(rh);
495         return GSS_S_FAILURE;
496     }
497
498     if (rc_read_dictionary(rh, rc_conf_str(rh, (char *)"dictionary")) != 0) {
499         *minor = errno;
500         return GSS_S_FAILURE;
501     }
502
503     *pHandle = rh;
504     return GSS_S_COMPLETE;
505 }
506
507 /*
508  * This is a super-inefficient coding but the API is going to change
509  * as are the data structures, so not putting a lot of work in now.
510  */
511 static size_t
512 avpSize(const VALUE_PAIR *vp)
513 {
514     return NAME_LENGTH + 1 + 12 + AUTH_STRING_LEN + 1;
515 }
516
517 static bool
518 avpExport(const VALUE_PAIR *vp,
519           unsigned char **pBuffer,
520           size_t *pRemain)
521 {
522     unsigned char *p = *pBuffer;
523     size_t remain = *pRemain;
524
525     assert(remain >= avpSize(vp));
526
527     memcpy(p, vp->name, NAME_LENGTH + 1);
528     p += NAME_LENGTH + 1;
529     remain -= NAME_LENGTH + 1;
530
531     store_uint32_be(vp->attribute, &p[0]);
532     store_uint32_be(vp->type,      &p[4]);
533     store_uint32_be(vp->lvalue,    &p[8]);
534
535     p += 12;
536     remain -= 12;
537
538     memcpy(p, vp->strvalue, AUTH_STRING_LEN + 1);
539     p += AUTH_STRING_LEN + 1;
540     remain -= AUTH_STRING_LEN + 1;
541
542     *pBuffer = p;
543     *pRemain = remain;
544
545     return true;
546
547 }
548
549 static bool
550 avpImport(VALUE_PAIR **pVp,
551           unsigned char **pBuffer,
552           size_t *pRemain)
553 {
554     unsigned char *p = *pBuffer;
555     size_t remain = *pRemain;
556     VALUE_PAIR *vp;
557
558     if (remain < avpSize(NULL)) {
559         return false;
560     }
561
562     vp = (VALUE_PAIR *)GSSEAP_CALLOC(1, sizeof(*vp));
563     if (vp == NULL) {
564         throw new std::bad_alloc;
565         return false;
566     }
567     vp->next = NULL;
568
569     memcpy(vp->name, p, NAME_LENGTH + 1);
570     p += NAME_LENGTH + 1;
571     remain -= NAME_LENGTH + 1;
572
573     vp->attribute = load_uint32_be(&p[0]);
574     vp->type      = load_uint32_be(&p[4]);
575     vp->lvalue    = load_uint32_be(&p[8]);
576
577     p += 12;
578     remain -= 12;
579
580     memcpy(vp->strvalue, p, AUTH_STRING_LEN + 1);
581     p += AUTH_STRING_LEN + 1;
582     remain -= AUTH_STRING_LEN + 1;
583
584     *pVp = vp;
585     *pBuffer = p;
586     *pRemain = remain;
587
588     return true;
589 }
590
591 bool
592 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
593                                              const gss_buffer_t buffer)
594 {
595     unsigned char *p = (unsigned char *)buffer->value;
596     size_t remain = buffer->length;
597     OM_uint32 count;
598     VALUE_PAIR **pNext = &m_avps;
599
600     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
601         return false;
602
603     if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
604         return false;
605
606     if (remain < 4)
607         return false;
608
609     count = load_uint32_be(p);
610     p += 4;
611     remain -= 4;
612
613     do {
614         VALUE_PAIR *attr;
615
616         if (!avpImport(&attr, &p, &remain))
617             return false;
618
619         *pNext = attr;
620         pNext = &attr->next;
621
622         count--;
623     } while (remain != 0);
624
625     if (count != 0)
626         return false;
627
628     return true;
629 }
630
631 void
632 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
633 {
634     OM_uint32 count = 0;
635     VALUE_PAIR *vp;
636     unsigned char *p;
637     size_t remain = 4;
638
639     for (vp = m_avps; vp != NULL; vp = vp->next) {
640         remain += avpSize(vp);
641         count++;
642     }
643
644     buffer->value = GSSEAP_MALLOC(remain);
645     if (buffer->value == NULL) {
646         throw new std::bad_alloc;
647         return;
648     }
649     buffer->length = remain;
650
651     p = (unsigned char *)buffer->value;
652
653     store_uint32_be(count, p);
654     p += 4;
655     remain -= 4;
656
657     for (vp = m_avps; vp != NULL; vp = vp->next) {
658         avpExport(vp, &p, &remain);
659     }
660
661     assert(remain == 0);
662 }