properly support multiple attribute statements
[moonshot.git] / mech_eap / util_shib.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  * Copyright 2001-2009 Internet2
34  *
35  * Licensed under the Apache License, Version 2.0 (the "License");
36  * you may not use this file except in compliance with the License.
37  * You may obtain a copy of the License at
38  *
39  *     http://www.apache.org/licenses/LICENSE-2.0
40  *
41  * Unless required by applicable law or agreed to in writing, software
42  * distributed under the License is distributed on an "AS IS" BASIS,
43  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44  * See the License for the specific language governing permissions and
45  * limitations under the License.
46  */
47
48 #include <shibsp/exceptions.h>
49 #include <shibsp/attribute/SimpleAttribute.h>
50
51 #include <shibresolver/resolver.h>
52
53 #include <sstream>
54
55 #include "gssapiP_eap.h"
56
57 using namespace shibsp;
58 using namespace shibresolver;
59 using namespace opensaml::saml2md;
60 using namespace opensaml;
61 using namespace xmltooling;
62 using namespace std;
63
64 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
65 {
66     m_authenticated = false;
67 }
68
69 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
70 {
71     for_each(m_attributes.begin(),
72              m_attributes.end(),
73              xmltooling::cleanup<Attribute>())
74         ;
75 }
76
77 bool
78 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
79                                                     const gss_eap_attr_provider *ctx)
80 {
81     const gss_eap_shib_attr_provider *shib;
82
83     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
84         return false;
85     }
86
87     m_authenticated = false;
88
89     shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
90     if (shib != NULL) {
91         m_attributes = duplicateAttributes(shib->getAttributes());
92         m_authenticated = shib->authenticated();
93     }
94
95     return true;
96 }
97
98 bool
99 addRadiusAttribute(const gss_eap_attr_provider *provider,
100                    const gss_buffer_t attribute,
101                    void *data)
102 {
103     const gss_eap_shib_attr_provider *shib;
104     const gss_eap_radius_attr_provider *radius;
105     int authenticated, complete, more = -1;
106     vector <string> attributeIds(1);
107     SimpleAttribute *a;
108
109     radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
110     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
111
112     assert(radius != NULL && shib != NULL);
113
114     string attributeName =
115         gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
116
117     attributeIds.push_back(attributeName);
118     a = new SimpleAttribute(attributeIds);
119     if (a == NULL)
120         return false;
121
122     while (more != 0) {
123         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
124         OM_uint32 minor;
125
126         if (!radius->getAttribute(attribute,
127                                   &authenticated,
128                                   &complete,
129                                   &value,
130                                   NULL,
131                                   &more))
132             return false;
133
134         string attributeValue((char *)value.value, value.length);
135         a->getValues().push_back(attributeValue);
136
137         gss_release_buffer(&minor, &value);
138     }
139
140     shib->getAttributes().push_back(a);
141
142     return true;
143 }
144
145 bool
146 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
147                                                const gss_cred_id_t gssCred,
148                                                const gss_ctx_id_t gssCtx)
149 {
150     const gss_eap_saml_assertion_provider *saml;
151     const gss_eap_radius_attr_provider *radius;
152     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
153     ShibbolethResolver *resolver;
154     OM_uint32 minor;
155
156     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
157         return false;
158
159     saml = static_cast<const gss_eap_saml_assertion_provider *>
160         (m_manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
161     radius = static_cast<const gss_eap_radius_attr_provider *>
162         (m_manager->getProvider(ATTR_TYPE_RADIUS));
163
164     resolver = ShibbolethResolver::create();
165
166     if (gssCred != GSS_C_NO_CREDENTIAL &&
167         gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
168         resolver->setApplicationID((const char *)nameBuf.value);
169
170     m_authenticated = false;
171
172     if (radius != NULL) {
173         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
174         m_authenticated = radius->authenticated();
175     }
176
177     if (saml != NULL && saml->getAssertion() != NULL) {
178         resolver->addToken(saml->getAssertion());
179         if (m_authenticated)
180             m_authenticated = saml->authenticated();
181     }
182
183     resolver->resolve();
184
185     m_attributes = resolver->getResolvedAttributes();
186     resolver->getResolvedAttributes().clear();
187
188     gss_release_buffer(&minor, &nameBuf);
189
190     delete resolver;
191
192     return true;
193 }
194
195 ssize_t
196 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
197 {
198     int i = 0;
199
200     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
201          a != m_attributes.end();
202          ++a)
203     {
204         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
205              s != (*a)->getAliases().end();
206              ++s) {
207             if (attr->length == (*s).length() &&
208                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
209                 return i;
210             }
211         }
212     }
213
214     return -1;
215 }
216
217 bool
218 gss_eap_shib_attr_provider::setAttribute(int complete,
219                                          const gss_buffer_t attr,
220                                          const gss_buffer_t value)
221 {
222     string attrStr((char *)attr->value, attr->length);
223     vector <string> ids(1, attrStr);
224     SimpleAttribute *a = new SimpleAttribute(ids);
225
226     if (value->length != 0) {
227         string valueStr((char *)value->value, value->length);
228
229         a->getValues().push_back(valueStr);
230     }
231
232     m_attributes.push_back(a);
233     m_authenticated = false;
234
235     return true;
236 }
237
238 bool
239 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
240 {
241     int i;
242
243     i = getAttributeIndex(attr);
244     if (i >= 0)
245         m_attributes.erase(m_attributes.begin() + i);
246
247     m_authenticated = false;
248
249     return true;
250 }
251
252 bool
253 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
254                                               void *data) const
255 {
256     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
257         a != m_attributes.end();
258         ++a)
259     {
260         gss_buffer_desc attribute;
261
262         attribute.value = (void *)((*a)->getId());
263         attribute.length = strlen((char *)attribute.value);
264
265         if (!addAttribute(this, &attribute, data))
266             return false;
267     }
268
269     return true;
270 }
271
272 const Attribute *
273 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
274 {
275     const Attribute *ret = NULL;
276
277     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
278          a != m_attributes.end();
279          ++a)
280     {
281         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
282              s != (*a)->getAliases().end();
283              ++s) {
284             if (attr->length == (*s).length() &&
285                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
286                 ret = *a;
287                 break;
288             }
289         }
290         if (ret != NULL)
291             break;
292     }
293
294     return ret;
295 }
296
297 bool
298 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
299                                          int *authenticated,
300                                          int *complete,
301                                          gss_buffer_t value,
302                                          gss_buffer_t display_value,
303                                          int *more) const
304 {
305     const Attribute *shibAttr = NULL;
306     gss_buffer_desc buf;
307     int nvalues, i = *more;
308
309     *more = 0;
310
311     shibAttr = getAttribute(attr);
312     if (shibAttr == NULL)
313         return false;
314
315     nvalues = shibAttr->valueCount();
316
317     if (i == -1)
318         i = 0;
319     else if (i >= nvalues)
320         return false;
321
322     buf.value = (void *)shibAttr->getString(*more);
323     buf.length = strlen((char *)buf.value);
324
325     if (buf.length != 0) {
326         if (value != NULL)
327             duplicateBuffer(buf, value);
328
329         if (display_value != NULL)
330             duplicateBuffer(buf, display_value);
331     }
332
333     if (authenticated != NULL)
334         *authenticated = m_authenticated;
335     if (complete != NULL)
336         *complete = false;
337
338     if (nvalues > ++i)
339         *more = i;
340
341     return true;
342 }
343
344 gss_any_t
345 gss_eap_shib_attr_provider::mapToAny(int authenticated,
346                                      gss_buffer_t type_id) const
347 {
348     gss_any_t output;
349
350     if (authenticated && !m_authenticated)
351         return (gss_any_t)NULL;
352
353     vector <Attribute *>v = duplicateAttributes(m_attributes);
354
355     output = (gss_any_t)new vector <Attribute *>(v);
356
357     return output;
358 }
359
360 void
361 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
362                                                   gss_any_t input) const
363 {
364     vector <Attribute *> *v = ((vector <Attribute *> *)input);
365     delete v;
366 }
367
368 void
369 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
370 {
371     DDF obj(NULL);
372     DDF attrs(NULL);
373
374     buffer->length = 0;
375     buffer->value = NULL;
376
377     obj.addmember("version").integer(1);
378     obj.addmember("authenticated").integer(m_authenticated);
379
380     attrs = obj.addmember("attributes").list();
381     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
382          a != m_attributes.end(); ++a) {
383         DDF attr = (*a)->marshall();
384         attrs.add(attr);
385     }
386
387     ostringstream sink;
388     sink << attrs;
389     string str = sink.str();
390
391     duplicateBuffer(str, buffer);
392
393     attrs.destroy();
394 }
395
396 bool
397 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
398                                            const gss_buffer_t buffer)
399 {
400     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
401         return false;
402
403     if (buffer->length == 0)
404         return true;
405
406     assert(m_authenticated == false);
407     assert(m_attributes.size() == 0);
408
409     DDF obj(NULL);
410     string str((const char *)buffer->value, buffer->length);
411     istringstream source(str);
412
413     source >> obj;
414
415     if (obj["version"].integer() != 1)
416         return false;
417
418     m_authenticated = (obj["authenticated"].integer() != 0);
419
420     DDF attrs = obj["attributes"];
421     DDF attr = attrs.first();
422     while (!attr.isnull()) {
423         Attribute *attribute = Attribute::unmarshall(attr);
424         m_attributes.push_back(attribute);
425         attr = attrs.next();
426     }
427
428     attrs.destroy();
429
430     return true;
431 }
432
433 bool
434 gss_eap_shib_attr_provider::init(void)
435 {
436     if (!ShibbolethResolver::init())
437         return false;
438
439     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
440                                        NULL,
441                                        gss_eap_shib_attr_provider::createAttrContext);
442
443     return true;
444 }
445
446 void
447 gss_eap_shib_attr_provider::finalize(void)
448 {
449     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
450     ShibbolethResolver::term();
451 }
452
453 gss_eap_attr_provider *
454 gss_eap_shib_attr_provider::createAttrContext(void)
455 {
456     return new gss_eap_shib_attr_provider;
457 }
458
459 Attribute *
460 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
461 {
462     DDF obj = src->marshall();
463     Attribute *attribute = Attribute::unmarshall(obj);
464     obj.destroy();
465
466     return attribute;
467 }
468
469 vector <Attribute *>
470 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
471 {
472     vector <Attribute *> dst;
473
474     for (vector<Attribute *>::const_iterator a = src.begin();
475          a != src.end();
476          ++a)
477         dst.push_back(duplicateAttribute(*a));
478
479     return dst;
480 }
481
482 OM_uint32
483 gssEapLocalAttrProviderInit(OM_uint32 *minor)
484 {
485     if (!gss_eap_shib_attr_provider::init()) {
486         *minor = GSSEAP_SHIB_INIT_FAILURE;
487         return GSS_S_FAILURE;
488     }
489     return GSS_S_COMPLETE;
490 }
491
492 OM_uint32
493 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
494 {
495     gss_eap_shib_attr_provider::finalize();
496     return GSS_S_COMPLETE;
497 }