Better error reporting through com_err
[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 /* stuff that should be provided by libradsec/libfreeradius-radius */
36 #define VENDORATTR(vendor, attr)            (((vendor) << 16) | (attr))
37
38 #ifndef ATTRID
39 #define ATTRID(attr)                        ((attr) & 0xFFFF)
40 #endif
41
42 static gss_buffer_desc radiusUrnPrefix = {
43     sizeof("urn:x-radius:") - 1,
44     (void *)"urn:x-radius:"
45 };
46
47 static VALUE_PAIR *copyAvps(const VALUE_PAIR *src);
48
49 gss_eap_radius_attr_provider::gss_eap_radius_attr_provider(void)
50 {
51     m_vps = NULL;
52     m_authenticated = false;
53 }
54
55 gss_eap_radius_attr_provider::~gss_eap_radius_attr_provider(void)
56 {
57     if (m_vps != NULL)
58         pairfree(&m_vps);
59 }
60
61 bool
62 gss_eap_radius_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
63                                                       const gss_eap_attr_provider *ctx)
64 {
65     const gss_eap_radius_attr_provider *radius;
66
67     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx))
68         return false;
69
70     radius = static_cast<const gss_eap_radius_attr_provider *>(ctx);
71
72     if (radius->m_vps != NULL)
73         m_vps = copyAvps(const_cast<VALUE_PAIR *>(radius->getAvps()));
74
75     m_authenticated = radius->m_authenticated;
76
77     return true;
78 }
79
80 bool
81 gss_eap_radius_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
82                                                  const gss_cred_id_t gssCred,
83                                                  const gss_ctx_id_t gssCtx)
84 {
85     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
86         return false;
87
88     if (gssCtx != GSS_C_NO_CONTEXT) {
89         if (gssCtx->acceptorCtx.vps != NULL) {
90             m_vps = copyAvps(gssCtx->acceptorCtx.vps);
91             if (m_vps == NULL)
92                 return false;
93
94             /* We assume libradsec validated this for us */
95             assert(pairfind(m_vps, PW_MESSAGE_AUTHENTICATOR) != NULL);
96             m_authenticated = true;
97         }
98     }
99
100     return true;
101 }
102
103 static bool
104 alreadyAddedAttributeP(std::vector <std::string> &attrs, VALUE_PAIR *vp)
105 {
106     for (std::vector<std::string>::const_iterator a = attrs.begin();
107          a != attrs.end();
108          ++a) {
109         if (strcmp(vp->name, (*a).c_str()) == 0)
110             return true;
111     }
112
113     return false;
114 }
115
116 static bool
117 isSecretAttributeP(uint16_t attrid, uint16_t vendor)
118 {
119     bool ret = false;
120
121     switch (vendor) {
122     case VENDORPEC_MS:
123         switch (attrid) {
124         case PW_MS_MPPE_SEND_KEY:
125         case PW_MS_MPPE_RECV_KEY:
126             ret = true;
127             break;
128         default:
129             break;
130         }
131     default:
132         break;
133     }
134
135     return ret;
136 }
137
138 static bool
139 isSecretAttributeP(uint32_t attribute)
140 {
141     return isSecretAttributeP(ATTRID(attribute), VENDOR(attribute));
142 }
143
144 static bool
145 isHiddenAttributeP(uint16_t attrid, uint16_t vendor)
146 {
147     bool ret = false;
148
149     /* should have been filtered */
150     assert(!isSecretAttributeP(attrid, vendor));
151
152     switch (vendor) {
153     case VENDORPEC_UKERNA:
154         ret = true;
155         break;
156     default:
157         break;
158     }
159
160     return ret;
161 }
162
163 static bool
164 isHiddenAttributeP(uint32_t attribute)
165 {
166     return isHiddenAttributeP(ATTRID(attribute), VENDOR(attribute));
167 }
168
169 /*
170  * Copy AVP list, same as paircopy except it filters out attributes
171  * containing keys.
172  */
173 static VALUE_PAIR *
174 copyAvps(const VALUE_PAIR *src)
175 {
176     const VALUE_PAIR *vp;
177     VALUE_PAIR *dst = NULL, **pDst = &dst;
178
179     for (vp = src; vp != NULL; vp = vp->next) {
180         VALUE_PAIR *vpcopy;
181
182         if (isSecretAttributeP(vp->attribute))
183             continue;
184
185         vpcopy = paircopyvp(vp);
186         if (vpcopy == NULL) {
187             pairfree(&dst);
188             throw new std::bad_alloc;
189             return NULL;
190         }
191         *pDst = vpcopy;
192         pDst = &vpcopy->next;
193      }
194
195     return dst;
196 }
197
198 bool
199 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
200 {
201     VALUE_PAIR *vp;
202     std::vector <std::string> seen;
203
204     for (vp = m_vps; vp != NULL; vp = vp->next) {
205         gss_buffer_desc attribute;
206         char attrid[64];
207
208         if (isHiddenAttributeP(vp->attribute))
209             continue;
210
211         if (alreadyAddedAttributeP(seen, vp))
212             continue;
213
214         snprintf(attrid, sizeof(attrid), "%s%d",
215             (char *)radiusUrnPrefix.value, vp->attribute);
216
217         attribute.value = attrid;
218         attribute.length = strlen(attrid);
219
220         if (!addAttribute(this, &attribute, data))
221             return false;
222
223         seen.push_back(std::string(vp->name));
224     }
225
226     return true;
227 }
228
229 void
230 gss_eap_radius_attr_provider::setAttribute(int complete,
231                                            const gss_buffer_t attr,
232                                            const gss_buffer_t value)
233 {
234 }
235
236 void
237 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
238 {
239 }
240
241 bool
242 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
243                                            int *authenticated,
244                                            int *complete,
245                                            gss_buffer_t value,
246                                            gss_buffer_t display_value,
247                                            int *more) const
248 {
249     OM_uint32 tmpMinor;
250     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
251     DICT_ATTR *da;
252     uint32_t attrid;
253     char *s;
254
255     duplicateBuffer(*attr, &strAttr);
256     s = (char *)strAttr.value;
257
258     if (attr->length < radiusUrnPrefix.length ||
259         memcmp(s, radiusUrnPrefix.value, radiusUrnPrefix.length) != 0)
260         return false;
261
262     s += radiusUrnPrefix.length;
263
264     if (isdigit(*s)) {
265         attrid = strtoul(s, NULL, 10);
266     } else {
267         da = dict_attrbyname(s);
268         if (da == NULL) {
269             gss_release_buffer(&tmpMinor, &strAttr);
270             return false;
271         }
272         attrid = da->attr;
273     }
274
275     gss_release_buffer(&tmpMinor, &strAttr);
276
277     return getAttribute(attrid, authenticated, complete,
278                         value, display_value, more);
279 }
280
281 bool
282 gss_eap_radius_attr_provider::getAttribute(uint32_t attrid,
283                                            int *authenticated,
284                                            int *complete,
285                                            gss_buffer_t value,
286                                            gss_buffer_t display_value,
287                                            int *more) const
288 {
289     VALUE_PAIR *vp;
290     int i = *more, count = 0;
291
292     *more = 0;
293
294     if (isHiddenAttributeP(attrid))
295         return false;
296
297     if (i == -1)
298         i = 0;
299
300     for (vp = pairfind(m_vps, attrid);
301          vp != NULL;
302          vp = pairfind(vp->next, attrid)) {
303         if (count++ == i) {
304             if (pairfind(vp->next, attrid) != NULL)
305                 *more = count;
306             break;
307         }
308     }
309
310     if (vp == NULL && *more == 0)
311         return false;
312
313     if (value != GSS_C_NO_BUFFER) {
314         gss_buffer_desc valueBuf;
315
316         valueBuf.value = (void *)vp->vp_octets;
317         valueBuf.length = vp->length;
318
319         duplicateBuffer(valueBuf, value);
320     }
321
322     if (display_value != GSS_C_NO_BUFFER) {
323         char displayString[MAX_STRING_LEN];
324         gss_buffer_desc displayBuf;
325
326         displayBuf.length = vp_prints_value(displayString,
327                                             sizeof(displayString), vp, 0);
328         displayBuf.value = (void *)displayString;
329
330         duplicateBuffer(displayBuf, display_value);
331     }
332
333     if (authenticated != NULL)
334         *authenticated = m_authenticated;
335     if (complete != NULL)
336         *complete = true;
337
338     return true;
339 }
340
341 bool
342 gss_eap_radius_attr_provider::getFragmentedAttribute(uint16_t attribute,
343                                                      uint16_t vendor,
344                                                      int *authenticated,
345                                                      int *complete,
346                                                      gss_buffer_t value) const
347 {
348     OM_uint32 major, minor;
349
350     major = gssEapRadiusGetAvp(&minor, m_vps, attribute, vendor, value, TRUE);
351
352     if (authenticated != NULL)
353         *authenticated = m_authenticated;
354     if (complete != NULL)
355         *complete = true;
356
357     return !GSS_ERROR(major);
358 }
359
360 bool
361 gss_eap_radius_attr_provider::getAttribute(uint16_t attribute,
362                                            uint16_t vendor,
363                                            int *authenticated,
364                                            int *complete,
365                                            gss_buffer_t value,
366                                            gss_buffer_t display_value,
367                                            int *more) const
368 {
369
370     return getAttribute(VENDORATTR(attribute, vendor),
371                         authenticated, complete,
372                         value, display_value, more);
373 }
374
375 gss_any_t
376 gss_eap_radius_attr_provider::mapToAny(int authenticated,
377                                        gss_buffer_t type_id) const
378 {
379     if (authenticated && !m_authenticated)
380         return (gss_any_t)NULL;
381
382     return (gss_any_t)copyAvps(m_vps);
383 }
384
385 void
386 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
387                                                     gss_any_t input) const
388 {
389     pairfree((VALUE_PAIR **)&input);
390 }
391
392 bool
393 gss_eap_radius_attr_provider::init(void)
394 {
395     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
396                                        "urn:ietf:params:gss-eap:radius-avp",
397                                        gss_eap_radius_attr_provider::createAttrContext);
398     return true;
399 }
400
401 void
402 gss_eap_radius_attr_provider::finalize(void)
403 {
404     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
405 }
406
407 gss_eap_attr_provider *
408 gss_eap_radius_attr_provider::createAttrContext(void)
409 {
410     return new gss_eap_radius_attr_provider;
411 }
412
413 OM_uint32
414 gssEapRadiusAddAvp(OM_uint32 *minor,
415                    VALUE_PAIR **vps,
416                    uint16_t attribute,
417                    uint16_t vendor,
418                    gss_buffer_t buffer)
419 {
420     uint32_t attrid = VENDORATTR(vendor, attribute);
421     unsigned char *p = (unsigned char *)buffer->value;
422     size_t remain = buffer->length;
423
424     do {
425         VALUE_PAIR *vp;
426         size_t n = remain;
427
428         if (n > MAX_STRING_LEN)
429             n = MAX_STRING_LEN;
430
431         vp = paircreate(attrid, PW_TYPE_OCTETS);
432         if (vp == NULL) {
433             *minor = ENOMEM;
434             return GSS_S_FAILURE;
435         }
436
437         memcpy(vp->vp_octets, p, n);
438         vp->length = n;
439
440         pairadd(vps, vp);
441
442         p += n;
443         remain -= n;
444     } while (remain != 0);
445
446     return GSS_S_COMPLETE;
447 }
448
449 OM_uint32
450 gssEapRadiusGetRawAvp(OM_uint32 *minor,
451                       VALUE_PAIR *vps,
452                       uint16_t attribute,
453                       uint16_t vendor,
454                       VALUE_PAIR **vp)
455 {
456     uint32_t attr = VENDORATTR(vendor, attribute);
457
458     *vp = pairfind(vps, attr);
459
460     return (*vp == NULL) ? GSS_S_UNAVAILABLE : GSS_S_COMPLETE;
461 }
462
463 OM_uint32
464 gssEapRadiusGetAvp(OM_uint32 *minor,
465                    VALUE_PAIR *vps,
466                    uint16_t attribute,
467                    uint16_t vendor,
468                    gss_buffer_t buffer,
469                    int concat)
470 {
471     VALUE_PAIR *vp;
472     unsigned char *p;
473     uint32_t attr = VENDORATTR(vendor, attribute);
474
475     buffer->length = 0;
476     buffer->value = NULL;
477
478     vp = pairfind(vps, attr);
479     if (vp == NULL)
480         return GSS_S_UNAVAILABLE;
481
482     do {
483         buffer->length += vp->length;
484     } while (concat && (vp = pairfind(vp->next, attr)) != NULL);
485
486     buffer->value = GSSEAP_MALLOC(buffer->length);
487     if (buffer->value == NULL) {
488         *minor = ENOMEM;
489         return GSS_S_FAILURE;
490     }
491
492     p = (unsigned char *)buffer->value;
493
494     for (vp = pairfind(vps, attr);
495          concat && vp != NULL;
496          vp = pairfind(vp->next, attr)) {
497         memcpy(p, vp->vp_octets, vp->length);
498         p += vp->length;
499     }
500
501     *minor = 0;
502     return GSS_S_COMPLETE;
503 }
504
505 OM_uint32
506 gssEapRadiusFreeAvps(OM_uint32 *minor,
507                      VALUE_PAIR **vps)
508 {
509     pairfree(vps);
510     *minor = 0;
511     return GSS_S_COMPLETE;
512 }
513
514 OM_uint32
515 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
516 {
517     if (!gss_eap_radius_attr_provider::init()) {
518         *minor = GSSEAP_RADSEC_INIT_FAILURE;
519         return GSS_S_FAILURE;
520     }
521
522     return GSS_S_COMPLETE;
523 }
524
525 OM_uint32
526 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
527 {
528     gss_eap_radius_attr_provider::finalize();
529     return GSS_S_COMPLETE;
530 }
531
532 /*
533  * Encoding is:
534  * 4 octet NBO attribute ID | 4 octet attribute length | attribute data
535  */
536 static size_t
537 avpSize(const VALUE_PAIR *vp)
538 {
539     size_t size = 4 + 1;
540
541     if (vp != NULL)
542         size += vp->length;
543
544     return size;
545 }
546
547 static bool
548 avpExport(const VALUE_PAIR *vp,
549           unsigned char **pBuffer,
550           size_t *pRemain)
551 {
552     unsigned char *p = *pBuffer;
553     size_t remain = *pRemain;
554
555     assert(remain >= avpSize(vp));
556
557     store_uint32_be(vp->attribute, p);
558
559     switch (vp->type) {
560     case PW_TYPE_INTEGER:
561     case PW_TYPE_IPADDR:
562     case PW_TYPE_DATE:
563         p[4] = 4;
564         store_uint32_be(vp->lvalue, p + 5);
565         break;
566     default:
567         assert(vp->length <= MAX_STRING_LEN);
568         p[4] = (uint8_t)vp->length;
569         memcpy(p + 5, vp->vp_octets, vp->length);
570         break;
571     }
572
573     *pBuffer += 5 + p[4];
574     *pRemain -= 5 + p[4];
575
576     return true;
577
578 }
579
580 static bool
581 avpImport(VALUE_PAIR **pVp,
582           unsigned char **pBuffer,
583           size_t *pRemain)
584 {
585     unsigned char *p = *pBuffer;
586     size_t remain = *pRemain;
587     VALUE_PAIR *vp = NULL;
588     DICT_ATTR *da;
589     uint32_t attrid;
590
591     if (remain < avpSize(NULL))
592         goto fail;
593
594     attrid = load_uint32_be(p);
595     p += 4;
596     remain -= 4;
597
598     da = dict_attrbyvalue(attrid);
599     if (da == NULL)
600         goto fail;
601
602     vp = pairalloc(da);
603     if (vp == NULL) {
604         throw new std::bad_alloc;
605         goto fail;
606     }
607
608     if (remain < p[0])
609         goto fail;
610
611     switch (vp->type) {
612     case PW_TYPE_INTEGER:
613     case PW_TYPE_IPADDR:
614     case PW_TYPE_DATE:
615         if (p[0] != 4)
616             goto fail;
617
618         vp->length = 4;
619         vp->lvalue = load_uint32_be(p + 1);
620         p += 5;
621         remain -= 5;
622         break;
623     case PW_TYPE_STRING:
624         /* check enough room to NUL terminate */
625         if (p[0] == MAX_STRING_LEN)
626             goto fail;
627         else
628         /* fallthrough */
629     default:
630         if (p[0] > MAX_STRING_LEN)
631             goto fail;
632
633         vp->length = (uint32_t)p[0];
634         memcpy(vp->vp_octets, p + 1, vp->length);
635
636         if (vp->type == PW_TYPE_STRING)
637             vp->vp_strvalue[vp->length] = '\0';
638
639         p += 1 + vp->length;
640         remain -= 1 + vp->length;
641         break;
642     }
643
644     *pVp = vp;
645     *pBuffer = p;
646     *pRemain = remain;
647
648     return true;
649
650 fail:
651     pairbasicfree(vp);
652     return false;
653 }
654
655 bool
656 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
657                                              const gss_buffer_t buffer)
658 {
659     unsigned char *p = (unsigned char *)buffer->value;
660     size_t remain = buffer->length;
661     VALUE_PAIR **pNext = &m_vps;
662
663     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
664         return false;
665
666     do {
667         VALUE_PAIR *attr;
668
669         if (!avpImport(&attr, &p, &remain))
670             return false;
671
672         *pNext = attr;
673         pNext = &attr->next;
674     } while (remain != 0);
675
676     return true;
677 }
678
679 void
680 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
681 {
682     VALUE_PAIR *vp;
683     unsigned char *p;
684     size_t remain = 0;
685
686     for (vp = m_vps; vp != NULL; vp = vp->next) {
687         remain += avpSize(vp);
688     }
689
690     buffer->value = GSSEAP_MALLOC(remain);
691     if (buffer->value == NULL) {
692         throw new std::bad_alloc;
693         return;
694     }
695     buffer->length = remain;
696
697     p = (unsigned char *)buffer->value;
698
699     for (vp = m_vps; vp != NULL; vp = vp->next) {
700         avpExport(vp, &p, &remain);
701     }
702
703     assert(remain == 0);
704 }
705
706 time_t
707 gss_eap_radius_attr_provider::getExpiryTime(void) const
708 {
709     VALUE_PAIR *vp;
710
711     vp = pairfind(m_vps, PW_SESSION_TIMEOUT);
712     if (vp == NULL || vp->lvalue == 0)
713         return 0;
714
715     return time(NULL) + vp->lvalue;
716 }
717
718 /* partition error namespace so it does not conflict with krb5 */
719 #define RS_TO_COM_ERR(rse)                  ((rse) == RSE_OK ? 0 : (rse) + ERROR_TABLE_BASE_rse)
720 #define COM_TO_RS_ERR(err)                  ((err) > ERROR_TABLE_BASE_rse && \
721                                              (err) <= (ERROR_TABLE_BASE_rse + RSE_SOME_ERROR) ? \
722                                              (err) - ERROR_TABLE_BASE_rse : RSE_SOME_ERROR)
723
724 OM_uint32
725 gssEapRadiusMapError(OM_uint32 *minor,
726                      struct rs_error *err)
727 {
728     int code = RSE_OK;
729
730     if (err != NULL)
731         code = rs_err_code(err, 0);
732     else
733         code = RSE_SOME_ERROR;
734
735     *minor = RS_TO_COM_ERR(code);
736
737     gssEapSaveStatusInfo(*minor, "%s", rs_err_msg(err, 0));
738
739     rs_err_free(err);
740     return GSS_S_FAILURE;
741 }