remove debugging statement
[moonshot.git] / mech_eap / 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 #if 0
160     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
161     OM_uint32 minor;
162 #endif
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     auto_ptr<ShibbolethResolver> resolver(ShibbolethResolver::create());
172
173     /*
174      * For now, leave ApplicationID defaulted.
175      * Later on, we could allow this via config option to the mechanism
176      * or rely on an SPRequest interface to pass in a URI identifying the
177      * acceptor.
178      */
179 #if 0
180     if (gssCred != GSS_C_NO_CREDENTIAL &&
181         gssEapDisplayName(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE) {
182         resolver->setApplicationID((const char *)nameBuf.value);
183         gss_release_buffer(&minor, &nameBuf);
184     }
185 #endif
186
187     m_authenticated = false;
188
189     if (radius != NULL) {
190         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
191         m_authenticated = radius->authenticated();
192     }
193
194     if (saml != NULL && saml->getAssertion() != NULL) {
195         resolver->addToken(saml->getAssertion());
196         if (m_authenticated)
197             m_authenticated = saml->authenticated();
198     }
199
200     try {
201         resolver->resolve();
202         m_attributes = resolver->getResolvedAttributes();
203         resolver->getResolvedAttributes().clear();
204     } catch (exception &e) {
205         //fprintf(stderr, "%s", e.what());
206     }
207
208     return true;
209 }
210
211 ssize_t
212 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
213 {
214     int i = 0;
215
216     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
217          a != m_attributes.end();
218          ++a)
219     {
220         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
221              s != (*a)->getAliases().end();
222              ++s) {
223             if (attr->length == (*s).length() &&
224                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
225                 return i;
226             }
227         }
228     }
229
230     return -1;
231 }
232
233 bool
234 gss_eap_shib_attr_provider::setAttribute(int complete GSSEAP_UNUSED,
235                                          const gss_buffer_t attr,
236                                          const gss_buffer_t value)
237 {
238     string attrStr((char *)attr->value, attr->length);
239     vector <string> ids(1, attrStr);
240     SimpleAttribute *a = new SimpleAttribute(ids);
241
242     if (value->length != 0) {
243         string valueStr((char *)value->value, value->length);
244
245         a->getValues().push_back(valueStr);
246     }
247
248     m_attributes.push_back(a);
249     m_authenticated = false;
250
251     return true;
252 }
253
254 bool
255 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
256 {
257     int i;
258
259     i = getAttributeIndex(attr);
260     if (i >= 0)
261         m_attributes.erase(m_attributes.begin() + i);
262
263     m_authenticated = false;
264
265     return true;
266 }
267
268 bool
269 gss_eap_shib_attr_provider::getAttributeTypes(gss_eap_attr_enumeration_cb addAttribute,
270                                               void *data) const
271 {
272     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
273         a != m_attributes.end();
274         ++a)
275     {
276         gss_buffer_desc attribute;
277
278         attribute.value = (void *)((*a)->getId());
279         attribute.length = strlen((char *)attribute.value);
280
281         if (!addAttribute(this, &attribute, data))
282             return false;
283     }
284
285     return true;
286 }
287
288 const Attribute *
289 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr) const
290 {
291     const Attribute *ret = NULL;
292
293     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
294          a != m_attributes.end();
295          ++a)
296     {
297         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
298              s != (*a)->getAliases().end();
299              ++s) {
300             if (attr->length == (*s).length() &&
301                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
302                 ret = *a;
303                 break;
304             }
305         }
306         if (ret != NULL)
307             break;
308     }
309
310     return ret;
311 }
312
313 bool
314 gss_eap_shib_attr_provider::getAttribute(const gss_buffer_t attr,
315                                          int *authenticated,
316                                          int *complete,
317                                          gss_buffer_t value,
318                                          gss_buffer_t display_value,
319                                          int *more) const
320 {
321     const Attribute *shibAttr = NULL;
322     gss_buffer_desc buf;
323     int nvalues, i = *more;
324
325     *more = 0;
326
327     shibAttr = getAttribute(attr);
328     if (shibAttr == NULL)
329         return false;
330
331     nvalues = shibAttr->valueCount();
332
333     if (i == -1)
334         i = 0;
335     else if (i >= nvalues)
336         return false;
337
338     buf.value = (void *)shibAttr->getSerializedValues()[*more].c_str();
339     buf.length = strlen((char *)buf.value);
340
341     if (buf.length != 0) {
342         if (value != NULL)
343             duplicateBuffer(buf, value);
344
345         if (display_value != NULL)
346             duplicateBuffer(buf, display_value);
347     }
348
349     if (authenticated != NULL)
350         *authenticated = m_authenticated;
351     if (complete != NULL)
352         *complete = false;
353
354     if (nvalues > ++i)
355         *more = i;
356
357     return true;
358 }
359
360 gss_any_t
361 gss_eap_shib_attr_provider::mapToAny(int authenticated,
362                                      gss_buffer_t type_id GSSEAP_UNUSED) const
363 {
364     gss_any_t output;
365
366     if (authenticated && !m_authenticated)
367         return (gss_any_t)NULL;
368
369     vector <Attribute *>v = duplicateAttributes(m_attributes);
370
371     output = (gss_any_t)new vector <Attribute *>(v);
372
373     return output;
374 }
375
376 void
377 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id GSSEAP_UNUSED,
378                                                   gss_any_t input) const
379 {
380     vector <Attribute *> *v = ((vector <Attribute *> *)input);
381     delete v;
382 }
383
384 const char *
385 gss_eap_shib_attr_provider::marshallingKey(void) const
386 {
387     return "local";
388 }
389
390 DDF
391 gss_eap_shib_attr_provider::marshall(void) const
392 {
393     DDF obj(NULL);
394
395     obj.addmember("authenticated").integer(m_authenticated);
396
397     DDF attrs = obj.addmember("attributes").list();
398     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
399          a != m_attributes.end(); ++a) {
400         DDF attr = (*a)->marshall();
401         attrs.add(attr);
402     }
403
404     return obj;
405 }
406
407 bool
408 gss_eap_shib_attr_provider::unmarshallAndInit(const gss_eap_attr_ctx *ctx,
409                                               DDF &obj)
410 {
411     if (!gss_eap_attr_provider::unmarshallAndInit(ctx, obj))
412         return false;
413
414     assert(m_authenticated == false);
415     assert(m_attributes.size() == 0);
416
417     m_authenticated = (obj["authenticated"].integer() != 0);
418
419     DDF attrs = obj["attributes"];
420     DDF attr = attrs.first();
421     while (!attr.isnull()) {
422         Attribute *attribute = Attribute::unmarshall(attr);
423         m_attributes.push_back(attribute);
424         attr = attrs.next();
425     }
426
427     return true;
428 }
429
430 bool
431 gss_eap_shib_attr_provider::init(void)
432 {
433     if (!ShibbolethResolver::init())
434         return false;
435
436     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL, NULL, createAttrContext);
437
438     return true;
439 }
440
441 void
442 gss_eap_shib_attr_provider::finalize(void)
443 {
444     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
445     ShibbolethResolver::term();
446 }
447
448 OM_uint32
449 gss_eap_shib_attr_provider::mapException(OM_uint32 *minor,
450                                          std::exception &e) const
451 {
452     if (typeid(e) == typeid(AttributeException))
453         *minor = GSSEAP_SHIB_ATTR_FAILURE;
454     else if (typeid(e) == typeid(AttributeExtractionException))
455         *minor = GSSEAP_SHIB_ATTR_EXTRACT_FAILURE;
456     else if (typeid(e) == typeid(AttributeFilteringException))
457         *minor = GSSEAP_SHIB_ATTR_FILTER_FAILURE;
458     else if (typeid(e) == typeid(AttributeResolutionException))
459         *minor = GSSEAP_SHIB_ATTR_RESOLVE_FAILURE;
460     else if (typeid(e) == typeid(ConfigurationException))
461         *minor = GSSEAP_SHIB_CONFIG_FAILURE;
462     else if (typeid(e) == typeid(ListenerException))
463         *minor = GSSEAP_SHIB_LISTENER_FAILURE;
464     else
465         return GSS_S_CONTINUE_NEEDED;
466
467     return GSS_S_FAILURE;
468 }
469
470 gss_eap_attr_provider *
471 gss_eap_shib_attr_provider::createAttrContext(void)
472 {
473     return new gss_eap_shib_attr_provider;
474 }
475
476 Attribute *
477 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
478 {
479     DDF obj = src->marshall();
480     Attribute *attribute = Attribute::unmarshall(obj);
481     obj.destroy();
482
483     return attribute;
484 }
485
486 vector <Attribute *>
487 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
488 {
489     vector <Attribute *> dst;
490
491     for (vector<Attribute *>::const_iterator a = src.begin();
492          a != src.end();
493          ++a)
494         dst.push_back(duplicateAttribute(*a));
495
496     return dst;
497 }
498
499 OM_uint32
500 gssEapLocalAttrProviderInit(OM_uint32 *minor)
501 {
502     if (!gss_eap_shib_attr_provider::init()) {
503         *minor = GSSEAP_SHIB_INIT_FAILURE;
504         return GSS_S_FAILURE;
505     }
506     return GSS_S_COMPLETE;
507 }
508
509 OM_uint32
510 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
511 {
512     gss_eap_shib_attr_provider::finalize();
513
514     *minor = 0;
515     return GSS_S_COMPLETE;
516 }