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