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