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