check radsec config when acquiring acceptor cred
[mech_eap.orig] / mech_eap / util_radius.cpp
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  * RADIUS attribute provider implementation.
35  */
36
37 #include "gssapiP_eap.h"
38
39 /* stuff that should be provided by libradsec/libfreeradius-radius */
40 #define VENDORATTR(vendor, attr)            (((vendor) << 16) | (attr))
41
42 #ifndef ATTRID
43 #define ATTRID(attr)                        ((attr) & 0xFFFF)
44 #endif
45
46 static gss_buffer_desc radiusUrnPrefix = {
47     sizeof("urn:x-radius:") - 1,
48     (void *)"urn:x-radius:"
49 };
50
51 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
52
53 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
54 {
55     m_vps = NULL;
56     m_authenticated = false;
57 }
58
59 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
60 {
61     if (m_vps != NULL)
62         pairfree(&m_vps);
63 }
64
65 bool
66 gss_eap_radius_attr_provider::initWithExistingContext(const gss_eap_attr_ctx *manager,
67                                                       const gss_eap_attr_provider *ctx)
68 {
69     const gss_eap_radius_attr_provider *radius;
70
71     if (!gss_eap_attr_provider::initWithExistingContext(manager, ctx))
72         return false;
73
74     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
75
76     if (radius->m_vps != NULL)
77         m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
78
79     m_authenticated = radius->m_authenticated;
80
81     return true;
82 }
83
84 bool
85 gss_eap_radius_attr_provider::initWithGssContext(const gss_eap_attr_ctx *manager,
86                                                  const gss_cred_id_t gssCred,
87                                                  const gss_ctx_id_t gssCtx)
88 {
89     if (!gss_eap_attr_provider::initWithGssContext(manager, gssCred, gssCtx))
90         return false;
91
92     if (gssCtx != GSS_C_NO_CONTEXT) {
93         if (gssCtx->acceptorCtx.vps != NULL) {
94             m_vps = copyAvps(gssCtx->acceptorCtx.vps);
95             if (m_vps == NULL)
96                 return false;
97
98             /* We assume libradsec validated this for us */
99             GSSEAP_ASSERT(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
100             m_authenticated = true;
101         }
102     }
103
104     return true;
105 }
106
107 static bool
108 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
109 {
110     for (std::vector<std::string>::const_iterator a = attrs.begin();
111          a != attrs.end();
112          ++a) {
113         if (strcmp(vp->name, (*a).c_str()) == 0)
114             return true;
115     }
116
117     return false;
118 }
119
120 static bool
121 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
122 {
123     bool bSecretAttribute = false;
124
125     switch (vendor) {
126     case VENDORPEC_MS:
127         switch (attrid) {
128         case PW_MS_MPPE_SEND_KEY:
129         case PW_MS_MPPE_RECV_KEY:
130             bSecretAttribute = true;
131             break;
132         default:
133             break;
134         }
135     default:
136         break;
137     }
138
139     return bSecretAttribute;
140 }
141
142 static bool
143 isSecretAttributeP(uint32_t attribute)
144 {
145     return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
146 }
147
148 static bool
149 isInternalAttributeP(uint16_t attrid, uint16_t vendor)
150 {
151     bool bInternalAttribute = false;
152
153     /* should have been filtered */
154     GSSEAP_ASSERT(!isSecretAttributeP(attrid, vendor));
155
156     switch (vendor) {
157     case VENDORPEC_UKERNA:
158         switch (attrid) {
159         case PW_GSS_ACCEPTOR_SERVICE_NAME:
160         case PW_GSS_ACCEPTOR_HOST_NAME:
161         case PW_GSS_ACCEPTOR_SERVICE_SPECIFIC:
162         case PW_GSS_ACCEPTOR_REALM_NAME:
163         case PW_SAML_AAA_ASSERTION:
164             bInternalAttribute = true;
165             break;
166         default:
167             break;
168         }
169         break;
170     default:
171         break;
172     }
173
174     return bInternalAttribute;
175 }
176
177 static bool
178 isInternalAttributeP(uint32_t attribute)
179 {
180     return isInternalAttributeP(ATTRID(attribute), VENDOR(attribute));
181 }
182
183 static bool
184 isFragmentedAttributeP(uint16_t attrid, uint16_t vendor)
185 {
186     /* A bit of a hack for the PAC for now. Should be configurable. */
187     return (vendor == VENDORPEC_UKERNA) &&
188         !isInternalAttributeP(attrid, vendor);
189 }
190
191 static bool
192 isFragmentedAttributeP(uint32_t attribute)
193 {
194     return isFragmentedAttributeP(ATTRID(attribute), VENDOR(attribute));
195 }
196
197 /*
198  * Copy AVP list, same as paircopy except it filters out attributes
199  * containing keys.
200  */
201 static VALUE_PAIR *
202 copyAvps(const VALUE_PAIR *src)
203 {
204     const VALUE_PAIR *vp;
205     VALUE_PAIR *dst = NULL, **pDst = &dst;
206
207     for (vp = src; vp != NULL; vp = vp->next) {
208         VALUE_PAIR *vpcopy;
209
210         if (isSecretAttributeP(vp->attribute))
211             continue;
212
213         vpcopy = paircopyvp(vp);
214         if (vpcopy == NULL) {
215             pairfree(&dst);
216             throw std::bad_alloc();
217         }
218         *pDst = vpcopy;
219         pDst = &vpcopy->next;
220      }
221
222     return dst;
223 }
224
225 bool
226 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
227                                                 void *data) const
228 {
229     VALUE_PAIR *vp;
230     std::vector <std::string> seen;
231
232     for (vp = m_vps; vp != NULL; vp = vp->next) {
233         gss_buffer_desc attribute;
234         char attrid[64];
235
236         /* Don't advertise attributes that are internal to the GSS-EAP mechanism */
237         if (isInternalAttributeP(vp->attribute))
238             continue;
239
240         if (alreadyAddedAttributeP(seen, vp))
241             continue;
242
243         snprintf(attrid, sizeof(attrid), "%s%d",
244             (char *)radiusUrnPrefix.value, vp->attribute);
245
246         attribute.value = attrid;
247         attribute.length = strlen(attrid);
248
249         if (!addAttribute(m_manager, this, &attribute, data))
250             return false;
251
252         seen.push_back(std::string(vp->name));
253     }
254
255     return true;
256 }
257
258 uint32_t
259 getAttributeId(const gss_buffer_t attr)
260 {
261     OM_uint32 tmpMinor;
262     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
263     DICT_ATTR *da;
264     char *s;
265     uint32_t attrid = 0;
266
267     if (attr->length < radiusUrnPrefix.length ||
268         memcmp(attr->value, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
269         return 0;
270
271     /* need to duplicate because attr may not be NUL terminated */
272     duplicateBuffer(*attr, &strAttr);
273     s = (char *)strAttr.value + radiusUrnPrefix.length;
274
275     if (isdigit(*s)) {
276         attrid = strtoul(s, NULL, 10);
277     } else {
278         da = dict_attrbyname(s);
279         if (da != NULL)
280             attrid = da->attr;
281     }
282
283     gss_release_buffer(&tmpMinor, &strAttr);
284
285     return attrid;
286 }
287
288 bool
289 gss_eap_radius_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
290                                            uint32_t attrid,
291                                            const gss_buffer_t value)
292 {
293     OM_uint32 major = GSS_S_UNAVAILABLE, minor;
294
295     if (!isSecretAttributeP(attrid) &&
296         !isInternalAttributeP(attrid)) {
297         deleteAttribute(attrid);
298
299         major = gssEapRadiusAddAvp(&minor, &m_vps,
300                                    ATTRID(attrid), VENDOR(attrid), 
301                                    value);
302     }
303
304     return !GSS_ERROR(major);
305 }
306
307 bool
308 gss_eap_radius_attr_provider::setAttribute(int complete,
309                                            const gss_buffer_t attr,
310                                            const gss_buffer_t value)
311 {
312     uint32_t attrid = getAttributeId(attr);
313
314     if (!attrid)
315         return false;
316
317     return setAttribute(complete, attrid, value);
318 }
319
320 bool
321 gss_eap_radius_attr_provider::deleteAttribute(uint32_t attrid)
322 {
323     if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid) ||
324         pairfind(m_vps, attrid) == NULL)
325         return false;
326
327     pairdelete(&m_vps, attrid);
328
329     return true;
330 }
331
332 bool
333 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t attr)
334 {
335     uint32_t attrid = getAttributeId(attr);
336
337     if (!attrid)
338         return false;
339
340     return deleteAttribute(attrid);
341 }
342
343 bool
344 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
345                                            int *authenticated,
346                                            int *complete,
347                                            gss_buffer_t value,
348                                            gss_buffer_t display_value,
349                                            int *more) const
350 {
351     uint32_t attrid;
352
353     attrid = getAttributeId(attr);
354     if (!attrid)
355         return false;
356
357     return getAttribute(attrid, authenticated, complete,
358                         value, display_value, more);
359 }
360
361 bool
362 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
363                                            int *authenticated,
364                                            int *complete,
365                                            gss_buffer_t value,
366                                            gss_buffer_t display_value,
367                                            int *more) const
368 {
369     VALUE_PAIR *vp;
370     int i = *more, count = 0;
371
372     *more = 0;
373
374     if (i == -1)
375         i = 0;
376
377     if (isSecretAttributeP(attrid) || isInternalAttributeP(attrid)) {
378         return false;
379     } else if (isFragmentedAttributeP(attrid)) {
380         return getFragmentedAttribute(attrid,
381                                       authenticated,
382                                       complete,
383                                       value);
384     }
385
386     for (vp = pairfind(m_vps, attrid);
387          vp != NULL;
388          vp = pairfind(vp->next, attrid)) {
389         if (count++ == i) {
390             if (pairfind(vp->next, attrid) != NULL)
391                 *more = count;
392             break;
393         }
394     }
395
396     if (vp == NULL && *more == 0)
397         return false;
398
399     if (value != GSS_C_NO_BUFFER) {
400         gss_buffer_desc valueBuf;
401
402         valueBuf.value = (void *)vp->vp_octets;
403         valueBuf.length = vp->length;
404
405         duplicateBuffer(valueBuf, value);
406     }
407
408     if (display_value != GSS_C_NO_BUFFER &&
409         vp->type != PW_TYPE_OCTETS) {
410         char displayString[MAX_STRING_LEN];
411         gss_buffer_desc displayBuf;
412
413         displayBuf.length = vp_prints_value(displayString,
414                                             sizeof(displayString), vp, 0);
415         displayBuf.value = (void *)displayString;
416
417         duplicateBuffer(displayBuf, display_value);
418     }
419
420     if (authenticated != NULL)
421         *authenticated = m_authenticated;
422     if (complete != NULL)
423         *complete = true;
424
425     return true;
426 }
427
428 bool
429 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
430                                                      uint16_t vendor,
431                                                      int *authenticated,
432                                                      int *complete,
433                                                      gss_buffer_t value) const
434 {
435     OM_uint32 major, minor;
436
437     major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
438
439     if (authenticated != NULL)
440         *authenticated = m_authenticated;
441     if (complete != NULL)
442         *complete = true;
443
444     return !GSS_ERROR(major);
445 }
446
447 bool
448 gss_eap_radius_attr_provider::getFragmentedAttribute(uint32_t attrid,
449                                                      int *authenticated,
450                                                      int *complete,
451                                                      gss_buffer_t value) const
452 {
453     return getFragmentedAttribute(ATTRID(attrid), VENDOR(attrid),
454                                   authenticated, complete, value);
455 }
456
457 bool
458 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
459                                            uint16_t vendor,
460                                            int *authenticated,
461                                            int *complete,
462                                            gss_buffer_t value,
463                                            gss_buffer_t display_value,
464                                            int *more) const
465 {
466
467     return getAttribute(VENDORATTR(attribute, vendor),
468                         authenticated, complete,
469                         value, display_value, more);
470 }
471
472 gss_any_t
473 gss_eap_radius_attr_provider::mapToAny(int authenticated,
474                                        gss_buffer_t type_id GSSEAP_UNUSED) const
475 {
476     if (authenticated && !m_authenticated)
477         return (gss_any_t)NULL;
478
479     return (gss_any_t)copyAvps(m_vps);
480 }
481
482 void
483 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
484                                                     gss_any_t input) const
485 {
486     VALUE_PAIR *vp = (VALUE_PAIR *)input;
487     pairfree(&vp);
488 }
489
490 bool
491 gss_eap_radius_attr_provider::init(void)
492 {
493     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS, createAttrContext);
494
495     return true;
496 }
497
498 void
499 gss_eap_radius_attr_provider::finalize(void)
500 {
501     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
502 }
503
504 gss_eap_attr_provider *
505 gss_eap_radius_attr_provider::createAttrContext(void)
506 {
507     return new gss_eap_radius_attr_provider;
508 }
509
510 OM_uint32
511 gssEapRadiusAddAvp(OM_uint32 *minor,
512                    VALUE_PAIR **vps,
513                    uint16_t attribute,
514                    uint16_t vendor,
515                    const gss_buffer_t buffer)
516 {
517     uint32_t attrid = VENDORATTR(vendor, attribute);
518     unsigned char *p = (unsigned char *)buffer->value;
519     size_t remain = buffer->length;
520
521     do {
522         VALUE_PAIR *vp;
523         size_t n = remain;
524
525         /*
526          * There's an extra byte of padding; RADIUS AVPs can only
527          * be 253 octets.
528          */
529         if (n >= MAX_STRING_LEN)
530             n = MAX_STRING_LEN - 1;
531
532         vp = paircreate(attrid, PW_TYPE_OCTETS);
533         if (vp == NULL) {
534             *minor = ENOMEM;
535             return GSS_S_FAILURE;
536         }
537
538         memcpy(vp->vp_octets, p, n);
539         vp->length = n;
540
541         pairadd(vps, vp);
542
543         p += n;
544         remain -= n;
545     } while (remain != 0);
546
547     return GSS_S_COMPLETE;
548 }
549
550 OM_uint32
551 gssEapRadiusGetRawAvp(OM_uint32 *minor,
552                       VALUE_PAIR *vps,
553                       uint16_t attribute,
554                       uint16_t vendor,
555                       VALUE_PAIR **vp)
556 {
557     uint32_t attr = VENDORATTR(vendor, attribute);
558
559     *vp = pairfind(vps, attr);
560     if (*vp == NULL) {
561         *minor = GSSEAP_NO_SUCH_ATTR;
562         return GSS_S_UNAVAILABLE;
563     }
564
565     return GSS_S_COMPLETE;
566 }
567
568 OM_uint32
569 gssEapRadiusGetAvp(OM_uint32 *minor,
570                    VALUE_PAIR *vps,
571                    uint16_t attribute,
572                    uint16_t vendor,
573                    gss_buffer_t buffer,
574                    int concat)
575 {
576     VALUE_PAIR *vp;
577     unsigned char *p;
578     uint32_t attr = VENDORATTR(vendor, attribute);
579
580     if (buffer != GSS_C_NO_BUFFER) {
581         buffer->length = 0;
582         buffer->value = NULL;
583     }
584
585     vp = pairfind(vps, attr);
586     if (vp == NULL) {
587         *minor = GSSEAP_NO_SUCH_ATTR;
588         return GSS_S_UNAVAILABLE;
589     }
590
591     if (buffer != GSS_C_NO_BUFFER) {
592         do {
593             buffer->length += vp->length;
594         } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
595
596         buffer->value = GSSEAP_MALLOC(buffer->length);
597         if (buffer->value == NULL) {
598             *minor = ENOMEM;
599             return GSS_S_FAILURE;
600         }
601
602         p = (unsigned char *)buffer->value;
603
604         for (vp = pairfind(vps, attr);
605              concat && vp != NULL;
606              vp = pairfind(vp->next, attr)) {
607             memcpy(p, vp->vp_octets, vp->length);
608             p += vp->length;
609         }
610     }
611
612     *minor = 0;
613     return GSS_S_COMPLETE;
614 }
615
616 OM_uint32
617 gssEapRadiusFreeAvps(OM_uint32 *minor,
618                      VALUE_PAIR **vps)
619 {
620     pairfree(vps);
621     *minor = 0;
622     return GSS_S_COMPLETE;
623 }
624
625 OM_uint32
626 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
627 {
628     if (!gss_eap_radius_attr_provider::init()) {
629         *minor = GSSEAP_RADSEC_INIT_FAILURE;
630         return GSS_S_FAILURE;
631     }
632
633     return GSS_S_COMPLETE;
634 }
635
636 OM_uint32
637 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
638 {
639     gss_eap_radius_attr_provider::finalize();
640
641     *minor = 0;
642     return GSS_S_COMPLETE;
643 }
644
645 static JSONObject
646 avpToJson(const VALUE_PAIR *vp)
647 {
648     JSONObject obj;
649
650     GSSEAP_ASSERT(vp->length <= MAX_STRING_LEN);
651
652     switch (vp->type) {
653     case PW_TYPE_INTEGER:
654     case PW_TYPE_IPADDR:
655     case PW_TYPE_DATE:
656         obj.set("value", vp->lvalue);
657         break;
658     case PW_TYPE_STRING:
659         obj.set("value", vp->vp_strvalue);
660         break;
661     default: {
662         char *b64;
663
664         if (base64Encode(vp->vp_octets, vp->length, &b64) < 0)
665             throw std::bad_alloc();
666
667         obj.set("value", b64);
668         GSSEAP_FREE(b64);
669         break;
670     }
671     }
672
673     obj.set("type", vp->attribute);
674
675     return obj;
676 }
677
678 static bool
679 jsonToAvp(VALUE_PAIR **pVp, JSONObject &obj)
680 {
681     VALUE_PAIR *vp = NULL;
682     DICT_ATTR *da;
683     uint32_t attrid;
684
685     JSONObject type = obj["type"];
686     JSONObject value = obj["value"];
687
688     if (!type.isInteger())
689         goto fail;
690
691     attrid = type.integer();
692     da = dict_attrbyvalue(attrid);
693     if (da != NULL) {
694         vp = pairalloc(da);
695     } else {
696         int type = base64Valid(value.string()) ?
697             PW_TYPE_OCTETS : PW_TYPE_STRING;
698         vp = paircreate(attrid, type);
699     }
700     if (vp == NULL)
701         throw std::bad_alloc();
702
703     switch (vp->type) {
704     case PW_TYPE_INTEGER:
705     case PW_TYPE_IPADDR:
706     case PW_TYPE_DATE:
707         if (!value.isInteger())
708             goto fail;
709
710         vp->length = 4;
711         vp->lvalue = value.integer();
712         break;
713     case PW_TYPE_STRING: {
714         if (!value.isString())
715             goto fail;
716
717         const char *str = value.string();
718         size_t len = strlen(str);
719
720         if (len >= MAX_STRING_LEN)
721             goto fail;
722
723         vp->length = len;
724         memcpy(vp->vp_strvalue, str, len + 1);
725         break;
726     }
727     case PW_TYPE_OCTETS:
728     default: {
729         if (!value.isString())
730             goto fail;
731
732         const char *str = value.string();
733         ssize_t len = strlen(str);
734
735         /* this optimization requires base64Decode only understand packed encoding */
736         if (len >= BASE64_EXPAND(MAX_STRING_LEN))
737             goto fail;
738
739         len = base64Decode(str, vp->vp_octets);
740         if (len < 0)
741             goto fail;
742
743         vp->length = len;
744         break;
745     }
746     }
747
748     *pVp = vp;
749
750     return true;
751
752 fail:
753     if (vp != NULL)
754         pairbasicfree(vp);
755     *pVp = NULL;
756     return false;
757 }
758
759 const char *
760 gss_eap_radius_attr_provider::name(void) const
761 {
762     return "radius";
763 }
764
765 bool
766 gss_eap_radius_attr_provider::initWithJsonObject(const gss_eap_attr_ctx *ctx,
767                                                  JSONObject &obj)
768 {
769     VALUE_PAIR **pNext = &m_vps;
770
771     if (!gss_eap_attr_provider::initWithJsonObject(ctx, obj))
772         return false;
773
774     JSONObject attrs = obj["attributes"];
775     size_t nelems = attrs.size();
776
777     for (size_t i = 0; i < nelems; i++) {
778         JSONObject attr = attrs[i];
779         VALUE_PAIR *vp;
780
781         if (!jsonToAvp(&vp, attr))
782             return false;
783
784         *pNext = vp;
785         pNext = &vp->next;
786     }
787
788     m_authenticated = obj["authenticated"].integer() ? true : false;
789
790     return true;
791 }
792
793 const char *
794 gss_eap_radius_attr_provider::prefix(void) const
795 {
796     return "urn:ietf:params:gss-eap:radius-avp";
797 }
798
799 JSONObject
800 gss_eap_radius_attr_provider::jsonRepresentation(void) const
801 {
802     JSONObject obj, attrs = JSONObject::array();
803
804     for (VALUE_PAIR *vp = m_vps; vp != NULL; vp = vp->next) {
805         JSONObject attr = avpToJson(vp);
806         attrs.append(attr);
807     }
808
809     obj.set("attributes", attrs);
810
811     obj.set("authenticated", m_authenticated);
812
813     return obj;
814 }
815
816 time_t
817 gss_eap_radius_attr_provider::getExpiryTime(void) const
818 {
819     VALUE_PAIR *vp;
820
821     vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
822     if (vp == NULL || vp->lvalue == 0)
823         return 0;
824
825     return time(NULL) + vp->lvalue;
826 }
827
828 OM_uint32
829 gssEapRadiusMapError(OM_uint32 *minor,
830                      struct rs_error *err)
831 {
832     int code;
833
834     GSSEAP_ASSERT(err != NULL);
835
836     code = rs_err_code(err, 0);
837
838     if (code == RSE_OK) {
839         *minor = 0;
840         return GSS_S_COMPLETE;
841     }
842
843     *minor = ERROR_TABLE_BASE_rse + code;
844
845     gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err));
846     rs_err_free(err);
847
848     return GSS_S_FAILURE;
849 }
850
851 OM_uint32
852 gssEapCreateRadiusContext(OM_uint32 *minor,
853                           gss_cred_id_t cred,
854                           struct rs_context **pRadContext)
855 {
856     const char *configFile = RS_CONFIG_FILE;
857     struct rs_context *radContext;
858     struct rs_alloc_scheme ralloc;
859     struct rs_error *err;
860     OM_uint32 major;
861
862     *pRadContext = NULL;
863
864     if (rs_context_create(&radContext) != 0) {
865         *minor = GSSEAP_RADSEC_CONTEXT_FAILURE;
866         return GSS_S_FAILURE;
867     }
868
869     if (cred->radiusConfigFile.value != NULL)
870         configFile = (const char *)cred->radiusConfigFile.value;
871
872     ralloc.calloc  = GSSEAP_CALLOC;
873     ralloc.malloc  = GSSEAP_MALLOC;
874     ralloc.free    = GSSEAP_FREE;
875     ralloc.realloc = GSSEAP_REALLOC;
876
877     rs_context_set_alloc_scheme(radContext, &ralloc);
878
879     if (rs_context_read_config(radContext, configFile) != 0) {
880         err = rs_err_ctx_pop(radContext);
881         goto fail;
882     }
883
884     if (rs_context_init_freeradius_dict(radContext, NULL) != 0) {
885         err = rs_err_ctx_pop(radContext);
886         goto fail;
887     }
888
889     *pRadContext = radContext;
890
891     *minor = 0;
892     return GSS_S_COMPLETE;
893
894 fail:
895     major = gssEapRadiusMapError(minor, err);
896     rs_context_destroy(radContext);
897
898     return major;
899 }