use shibresolver API properly
[mech_eap.orig] / 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 "gssapiP_eap.h"
49
50 #include <shibsp/Application.h>
51 #include <shibsp/exceptions.h>
52 #include <shibsp/SPConfig.h>
53 #include <shibsp/ServiceProvider.h>
54 #include <shibsp/attribute/Attribute.h>
55 #include <shibsp/attribute/SimpleAttribute.h>
56 #include <shibsp/attribute/resolver/ResolutionContext.h>
57 #include <shibsp/handler/AssertionConsumerService.h>
58 #include <shibsp/metadata/MetadataProviderCriteria.h>
59 #include <shibsp/util/SPConstants.h>
60
61 #include <shibresolver/resolver.h>
62
63 using namespace shibsp;
64 using namespace shibresolver;
65 using namespace opensaml::saml2md;
66 using namespace opensaml;
67 using namespace xmltooling::logging;
68 using namespace xmltooling;
69 using namespace xercesc;
70 using namespace std;
71
72 gss_eap_shib_attr_provider::gss_eap_shib_attr_provider(void)
73 {
74     m_authenticated = false;
75 }
76
77 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
78 {
79     for_each(m_attributes.begin(),
80              m_attributes.end(),
81              xmltooling::cleanup<Attribute>())
82         ;
83 }
84
85 bool
86 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
87                                                     const gss_eap_attr_provider *ctx)
88 {
89     const gss_eap_shib_attr_provider *shib;
90
91     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
92         return false;
93     }
94
95     m_authenticated = false;
96
97     shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
98     if (shib != NULL) {
99         m_attributes = duplicateAttributes(shib->getAttributes());
100         m_authenticated = shib->authenticated();
101     }
102
103     return true;
104 }
105
106 bool
107 addRadiusAttribute(const gss_eap_attr_provider *provider,
108                    const gss_buffer_t attribute,
109                    void *data)
110 {
111     const gss_eap_shib_attr_provider *shib;
112     const gss_eap_radius_attr_provider *radius;
113     int authenticated, complete, more = -1;
114     vector <string> attributeIds(1);
115     SimpleAttribute *a;
116
117     radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
118     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
119
120     assert(radius != NULL && shib != NULL);
121
122     string attributeName =
123         gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
124
125     attributeIds.push_back(attributeName);
126     a = new SimpleAttribute(attributeIds);
127     if (a == NULL)
128         return false;
129
130     while (more != 0) {
131         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
132         OM_uint32 minor;
133
134         if (!radius->getAttribute(attribute,
135                                   &authenticated,
136                                   &complete,
137                                   &value,
138                                   NULL,
139                                   &more))
140             return false;
141
142         string attributeValue((char *)value.value, value.length);
143         a->getValues().push_back(attributeValue);
144
145         gss_release_buffer(&minor, &value);
146     }
147
148     shib->getAttributes().push_back(a);
149
150     return true;
151 }
152
153 bool
154 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
155                                                const gss_cred_id_t gssCred,
156                                                const gss_ctx_id_t gssCtx)
157 {
158     const gss_eap_saml_assertion_provider *saml;
159     const gss_eap_radius_attr_provider *radius;
160     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
161     ShibbolethResolver *resolver;
162     OM_uint32 minor;
163
164     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
165         return false;
166
167     saml = static_cast<const gss_eap_saml_assertion_provider *>
168         (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
169     radius = static_cast<const gss_eap_radius_attr_provider *>
170         (manager->getProvider(ATTR_TYPE_RADIUS));
171
172     resolver = ShibbolethResolver::create();
173
174     if (gssCred != GSS_C_NO_CREDENTIAL &&
175         gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
176         resolver->setApplicationID((const char *)nameBuf.value);
177
178     m_authenticated = false;
179
180     if (radius != NULL) {
181         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
182         m_authenticated = radius->authenticated();
183     }
184
185     if (saml != NULL && saml->getAssertion() != NULL) {
186         resolver->addToken(saml->getAssertion());
187         if (m_authenticated)
188             m_authenticated = saml->authenticated();
189     }
190
191     resolver->resolve();
192
193     m_attributes = resolver->getResolvedAttributes();
194     resolver->getResolvedAttributes().clear();
195
196     gss_release_buffer(&minor, &nameBuf);
197
198     delete resolver;
199
200 #ifdef GSSEAP_DEBUG
201     gss_buffer_desc testattr = {
202         sizeof("urn:greet:greeting") - 1, (void *)"urn:greet:greeting" };
203     gss_buffer_desc testval =
204         { sizeof("Hello, GSS EAP.") - 1, (void *)"Hello, GSS EAP." };
205     setAttribute(true, &testattr, &testval);
206 #endif /* GSSEAP_DEBUG */
207
208     return true;
209 }
210
211 int
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 void
234 gss_eap_shib_attr_provider::setAttribute(int complete,
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
252 void
253 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
254 {
255     int i;
256
257     i = getAttributeIndex(attr);
258     if (i >= 0)
259         m_attributes.erase(m_attributes.begin() + i);
260
261     m_authenticated = false;
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) const
359 {
360     gss_any_t output;
361
362     vector <Attribute *>v = duplicateAttributes(m_attributes);
363
364     output = (gss_any_t)new vector <Attribute *>(v);
365
366     return output;
367 }
368
369 void
370 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
371                                                   gss_any_t input) const
372 {
373     vector <Attribute *> *v = ((vector <Attribute *> *)input);
374     delete v;
375 }
376
377 void
378 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
379 {
380     DDF obj(NULL);
381     DDF attrs(NULL);
382
383     buffer->length = 0;
384     buffer->value = NULL;
385
386     obj.addmember("version").integer(1);
387     obj.addmember("authenticated").integer(m_authenticated);
388
389     attrs = obj.addmember("attributes").list();
390     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
391          a != m_attributes.end(); ++a) {
392         DDF attr = (*a)->marshall();
393         attrs.add(attr);
394     }
395
396     ostringstream sink;
397     sink << attrs;
398     string str = sink.str();
399
400     duplicateBuffer(str, buffer);
401
402     attrs.destroy();
403 }
404
405 bool
406 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
407                                            const gss_buffer_t buffer)
408 {
409     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
410         return false;
411
412     if (buffer->length == 0)
413         return true;
414
415     assert(m_authenticated == false);
416     assert(m_attributes.size() == 0);
417
418     DDF obj(NULL);
419     string str((const char *)buffer->value, buffer->length);
420     istringstream source(str);
421
422     source >> obj;
423
424     if (obj["version"].integer() != 1)
425         return false;
426
427     m_authenticated = (obj["authenticated"].integer() != 0);
428
429     DDF attrs = obj["attributes"];
430     DDF attr = attrs.first();
431     while (!attr.isnull()) {
432         Attribute *attribute = Attribute::unmarshall(attr);
433         m_attributes.push_back(attribute);
434         attr = attrs.next();
435     }
436
437     attrs.destroy();
438
439     return true;
440 }
441
442 bool
443 gss_eap_shib_attr_provider::init(void)
444 {
445 #if 1
446     SPConfig& conf=SPConfig::getConfig();
447     conf.setFeatures(
448         SPConfig::Metadata |
449         SPConfig::Trust |
450         SPConfig::AttributeResolution |
451         SPConfig::Credentials |
452         SPConfig::OutOfProcess
453         );
454     if (!conf.init())
455         return false;
456     if (!conf.instantiate()) {
457         conf.term();
458         return false;
459     }
460 #else
461     if (!ShibbolethResolver::init())
462         return false;
463 #endif
464
465     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
466                                        NULL,
467                                        gss_eap_shib_attr_provider::createAttrContext);
468
469     return true;
470 }
471
472 void
473 gss_eap_shib_attr_provider::finalize(void)
474 {
475     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
476 //    ShibbolethResolver::term();
477 }
478
479 gss_eap_attr_provider *
480 gss_eap_shib_attr_provider::createAttrContext(void)
481 {
482     return new gss_eap_shib_attr_provider;
483 }
484
485 Attribute *
486 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
487 {
488     DDF obj = src->marshall();
489     Attribute *attribute = Attribute::unmarshall(obj);
490     obj.destroy();
491
492     return attribute;
493 }
494
495 vector <Attribute *>
496 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
497 {
498     vector <Attribute *> dst;
499
500     for (vector<Attribute *>::const_iterator a = src.begin();
501          a != src.end();
502          ++a)
503         dst.push_back(duplicateAttribute(*a));
504
505     return dst;
506 }
507
508 OM_uint32
509 gssEapLocalAttrProviderInit(OM_uint32 *minor)
510 {
511     return gss_eap_shib_attr_provider::init()
512         ? GSS_S_COMPLETE : GSS_S_FAILURE;
513 }
514
515 OM_uint32
516 gssEapLocalAttrProviderFinalize(OM_uint32 *minor)
517 {
518     gss_eap_shib_attr_provider::finalize();
519     return GSS_S_COMPLETE;
520 }