fix printable predicate
[mech_eap.orig] / 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 isSecretAttributeP(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     default:
152         break;
153     }
154
155     return ret;
156 }
157
158 bool
159 gss_eap_radius_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute, void *data) const
160 {
161     VALUE_PAIR *vp;
162     std::vector <std::string> seen;
163
164     for (vp = m_avps; vp != NULL; vp = vp->next) {
165         gss_buffer_desc attribute;
166
167         if (isSecretAttributeP(ATTRID(vp->attribute), VENDOR(vp->attribute)))
168             continue;
169
170         if (alreadyAddedAttributeP(seen, vp))
171             continue;
172
173         attribute.value = (void *)vp->name;
174         attribute.length = strlen(vp->name);
175
176         if (!addAttribute(this, &attribute, data))
177             return false;
178
179         seen.push_back(std::string(vp->name));
180     }
181
182     return true;
183 }
184
185 void
186 gss_eap_radius_attr_provider::setAttribute(int complete,
187                                            const gss_buffer_t attr,
188                                            const gss_buffer_t value)
189 {
190 }
191
192 void
193 gss_eap_radius_attr_provider::deleteAttribute(const gss_buffer_t value)
194 {
195 }
196
197 bool
198 gss_eap_radius_attr_provider::getAttribute(const gss_buffer_t attr,
199                                            int *authenticated,
200                                            int *complete,
201                                            gss_buffer_t value,
202                                            gss_buffer_t display_value,
203                                            int *more) const
204 {
205     OM_uint32 tmpMinor;
206     gss_buffer_desc strAttr = GSS_C_EMPTY_BUFFER;
207     DICT_ATTR *d;
208     int attrid;
209     char *s;
210
211     /* XXX vendor */
212
213     duplicateBuffer(*attr, &strAttr);
214     s = (char *)strAttr.value;
215
216     if (isdigit(((char *)strAttr.value)[0])) {
217         attrid = strtoul(s, NULL, 10);
218     } else {
219         d = rc_dict_findattr(m_rh, (char *)s);
220         if (d == NULL) {
221             gss_release_buffer(&tmpMinor, &strAttr);
222             return false;
223         }
224         attrid = d->value;
225     }
226
227     gss_release_buffer(&tmpMinor, &strAttr);
228
229     return getAttribute(attrid, authenticated, complete,
230                         value, display_value, more);
231 }
232
233 static bool
234 isPrintableAttributeP(VALUE_PAIR *vp)
235 {
236     size_t i;
237     int gotChar = 0;
238
239     for (i = 0; i < sizeof(vp->strvalue); i++) {
240         if (gotChar && vp->strvalue[i] == '\0')
241             return true;
242
243         if (!isprint(vp->strvalue[i]))
244             return false;
245
246         if (!gotChar)
247             gotChar++;
248     }
249
250     return true;
251 }
252
253 bool
254 gss_eap_radius_attr_provider::getAttribute(int attrid,
255                                            int vendor,
256                                            int *authenticated,
257                                            int *complete,
258                                            gss_buffer_t value,
259                                            gss_buffer_t display_value,
260                                            int *more) const
261 {
262     OM_uint32 tmpMinor;
263     VALUE_PAIR *vp;
264     int i = *more;
265     int max = 0;
266     char name[NAME_LENGTH + 1];
267     char displayString[AUTH_STRING_LEN + 1];
268     gss_buffer_desc valueBuf = GSS_C_EMPTY_BUFFER;
269     gss_buffer_desc displayBuf = GSS_C_EMPTY_BUFFER;
270
271     *more = 0;
272
273     if (isSecretAttributeP(attrid, vendor))
274         return false;
275
276     vp = rc_avpair_get(m_avps, attrid, vendor);
277     if (vp == NULL)
278         return false;
279
280     if (i == -1)
281         i = 0;
282
283     do {
284         if (i == max)
285             break;
286
287         max++;
288     } while ((vp = rc_avpair_get(vp->next, attrid, vendor)) != NULL);
289
290     if (i > max)
291         return false;
292
293     if (vp->type == PW_TYPE_STRING) {
294         valueBuf.value = (void *)vp->strvalue;
295         valueBuf.length = vp->lvalue;
296     } else {
297         valueBuf.value = (void *)&vp->lvalue;
298         valueBuf.length = 4;
299     }
300
301     if (value != GSS_C_NO_BUFFER)
302         duplicateBuffer(valueBuf, value);
303
304     if (display_value != GSS_C_NO_BUFFER &&
305         isPrintableAttributeP(vp)) {
306         if (rc_avpair_tostr(m_rh, vp, name, NAME_LENGTH,
307                             displayString, AUTH_STRING_LEN) != 0) {
308             gss_release_buffer(&tmpMinor, value);
309             return false;
310         }
311
312         displayBuf.value = (void *)displayString;
313         displayBuf.length = strlen(displayString);
314
315         duplicateBuffer(displayBuf, display_value);
316     }
317
318     if (authenticated != NULL)
319         *authenticated = m_authenticated;
320     if (complete != NULL)
321         *complete = true;
322
323     if (max > i)
324         *more = i;
325
326     return true;
327 }
328
329 bool
330 gss_eap_radius_attr_provider::getAttribute(int attrid,
331                                            int *authenticated,
332                                            int *complete,
333                                            gss_buffer_t value,
334                                            gss_buffer_t display_value,
335                                            int *more) const
336 {
337
338     return getAttribute(ATTRID(attrid), VENDOR(attrid),
339                         authenticated, complete,
340                         value, display_value, more);
341 }
342
343 gss_any_t
344 gss_eap_radius_attr_provider::mapToAny(int authenticated,
345                                        gss_buffer_t type_id) const
346 {
347     if (authenticated && !m_authenticated)
348         return (gss_any_t)NULL;
349
350     return (gss_any_t)copyAvps(m_avps);
351 }
352
353 void
354 gss_eap_radius_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
355                                                     gss_any_t input) const
356 {
357     rc_avpair_free((VALUE_PAIR *)input);
358 }
359
360 bool
361 gss_eap_radius_attr_provider::init(void)
362 {
363     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_RADIUS,
364                                        "urn:ietf:params:gss-eap:radius-avp",
365                                        gss_eap_radius_attr_provider::createAttrContext);
366     return true;
367 }
368
369 void
370 gss_eap_radius_attr_provider::finalize(void)
371 {
372     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_RADIUS);
373 }
374
375 gss_eap_attr_provider *
376 gss_eap_radius_attr_provider::createAttrContext(void)
377 {
378     return new gss_eap_radius_attr_provider;
379 }
380
381 OM_uint32
382 addAvpFromBuffer(OM_uint32 *minor,
383                  rc_handle *rh,
384                  VALUE_PAIR **vp,
385                  int type,
386                  int vendor,
387                  gss_buffer_t buffer)
388 {
389     if (rc_avpair_add(rh, vp, type,
390                       buffer->value, buffer->length, vendor) == NULL) {
391         return GSS_S_FAILURE;
392     }
393
394     return GSS_S_COMPLETE;
395 }
396
397 OM_uint32
398 getBufferFromAvps(OM_uint32 *minor,
399                   VALUE_PAIR *vps,
400                   int type,
401                   int vendor,
402                   gss_buffer_t buffer,
403                   int concat)
404 {
405     VALUE_PAIR *vp;
406     unsigned char *p;
407
408     buffer->length = 0;
409     buffer->value = NULL;
410
411     vp = rc_avpair_get(vps, type, vendor);
412     if (vp == NULL)
413         return GSS_S_UNAVAILABLE;
414
415     do {
416         buffer->length += vp->lvalue;
417     } while (concat && (vp = rc_avpair_get(vp->next, type, vendor)) != NULL);
418
419     buffer->value = GSSEAP_MALLOC(buffer->length);
420     if (buffer->value == NULL) {
421         *minor = ENOMEM;
422         return GSS_S_FAILURE;
423     }
424
425     p = (unsigned char *)buffer->value;
426
427     for (vp = rc_avpair_get(vps, type, vendor);
428          concat && vp != NULL;
429          vp = rc_avpair_get(vp->next, type, vendor)) {
430         memcpy(p, vp->strvalue, vp->lvalue);
431         p += vp->lvalue;
432     }
433
434     *minor = 0;
435     return GSS_S_COMPLETE;
436 }
437
438 OM_uint32
439 gssEapRadiusAttrProviderInit(OM_uint32 *minor)
440 {
441     return gss_eap_radius_attr_provider::init()
442         ? GSS_S_COMPLETE : GSS_S_FAILURE;
443 }
444
445 OM_uint32
446 gssEapRadiusAttrProviderFinalize(OM_uint32 *minor)
447 {
448     gss_eap_radius_attr_provider::finalize();
449     return GSS_S_COMPLETE;
450 }
451
452 OM_uint32
453 gssEapRadiusAllocHandle(OM_uint32 *minor,
454                         const gss_cred_id_t cred,
455                         rc_handle **pHandle)
456 {
457     rc_handle *rh;
458     const char *config = RC_CONFIG_FILE;
459
460     *pHandle = NULL;
461
462     if (cred != GSS_C_NO_CREDENTIAL && cred->radiusConfigFile != NULL)
463         config = cred->radiusConfigFile;
464
465     rh = rc_read_config((char *)config);
466     if (rh == NULL) {
467         *minor = errno;
468         rc_config_free(rh);
469         return GSS_S_FAILURE;
470     }
471
472     if (rc_read_dictionary(rh, rc_conf_str(rh, (char *)"dictionary")) != 0) {
473         *minor = errno;
474         return GSS_S_FAILURE;
475     }
476
477     *pHandle = rh;
478     return GSS_S_COMPLETE;
479 }
480
481 /*
482  * This is a super-inefficient coding but the API is going to change
483  * as are the data structures, so not putting a lot of work in now.
484  */
485 static size_t
486 avpSize(const VALUE_PAIR *vp)
487 {
488     return NAME_LENGTH + 1 + 12 + AUTH_STRING_LEN + 1;
489 }
490
491 static bool
492 avpExport(const VALUE_PAIR *vp,
493           unsigned char **pBuffer,
494           size_t *pRemain)
495 {
496     unsigned char *p = *pBuffer;
497     size_t remain = *pRemain;
498
499     assert(remain >= avpSize(vp));
500
501     memcpy(p, vp->name, NAME_LENGTH + 1);
502     p += NAME_LENGTH + 1;
503     remain -= NAME_LENGTH + 1;
504
505     store_uint32_be(vp->attribute, &p[0]);
506     store_uint32_be(vp->type,      &p[4]);
507     store_uint32_be(vp->lvalue,    &p[8]);
508
509     p += 12;
510     remain -= 12;
511
512     memcpy(p, vp->strvalue, AUTH_STRING_LEN + 1);
513     p += AUTH_STRING_LEN + 1;
514     remain -= AUTH_STRING_LEN + 1;
515
516     *pBuffer = p;
517     *pRemain = remain;
518
519     return true;
520
521 }
522
523 static bool
524 avpImport(VALUE_PAIR **pVp,
525           unsigned char **pBuffer,
526           size_t *pRemain)
527 {
528     unsigned char *p = *pBuffer;
529     size_t remain = *pRemain;
530     VALUE_PAIR *vp;
531
532     if (remain < avpSize(NULL)) {
533         return false;
534     }
535
536     vp = (VALUE_PAIR *)GSSEAP_CALLOC(1, sizeof(*vp));
537     if (vp == NULL) {
538         throw new std::bad_alloc;
539         return false;
540     }
541     vp->next = NULL;
542
543     memcpy(vp->name, p, NAME_LENGTH + 1);
544     p += NAME_LENGTH + 1;
545     remain -= NAME_LENGTH + 1;
546
547     vp->attribute = load_uint32_be(&p[0]);
548     vp->type      = load_uint32_be(&p[4]);
549     vp->lvalue    = load_uint32_be(&p[8]);
550
551     p += 12;
552     remain -= 12;
553
554     memcpy(vp->strvalue, p, AUTH_STRING_LEN + 1);
555     p += AUTH_STRING_LEN + 1;
556     remain -= AUTH_STRING_LEN + 1;
557
558     *pVp = vp;
559     *pBuffer = p;
560     *pRemain = remain;
561
562     return true;
563 }
564
565 bool
566 gss_eap_radius_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
567                                              const gss_buffer_t buffer)
568 {
569     unsigned char *p = (unsigned char *)buffer->value;
570     size_t remain = buffer->length;
571     OM_uint32 count;
572     VALUE_PAIR **pNext = &m_avps;
573
574     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
575         return false;
576
577     if (!initFromGssCred(GSS_C_NO_CREDENTIAL))
578         return false;
579
580     if (remain < 4)
581         return false;
582
583     count = load_uint32_be(p);
584     p += 4;
585     remain -= 4;
586
587     do {
588         VALUE_PAIR *attr;
589
590         if (!avpImport(&attr, &p, &remain))
591             return false;
592
593         *pNext = attr;
594         pNext = &attr->next;
595
596         count--;
597     } while (remain != 0);
598
599     if (count != 0)
600         return false;
601
602     return true;
603 }
604
605 void
606 gss_eap_radius_attr_provider::exportToBuffer(gss_buffer_t buffer) const
607 {
608     OM_uint32 count = 0;
609     VALUE_PAIR *vp;
610     unsigned char *p;
611     size_t remain = 4;
612
613     for (vp = m_avps; vp != NULL; vp = vp->next) {
614         remain += avpSize(vp);
615         count++;
616     }
617
618     buffer->value = GSSEAP_MALLOC(remain);
619     if (buffer->value == NULL) {
620         throw new std::bad_alloc;
621         return;
622     }
623     buffer->length = remain;
624
625     p = (unsigned char *)buffer->value;
626
627     store_uint32_be(count, p);
628     p += 4;
629     remain -= 4;
630
631     for (vp = m_avps; vp != NULL; vp = vp->next) {
632         avpExport(vp, &p, &remain);
633     }
634
635     assert(remain == 0);
636 }