cleanup name exporting for internal use
[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 #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 <saml/saml1/core/Assertions.h>
62 #include <saml/saml2/core/Assertions.h>
63 #include <saml/saml2/metadata/Metadata.h>
64 #include <xercesc/util/XMLUniDefs.hpp>
65 #include <xmltooling/XMLToolingConfig.h>
66 #include <xmltooling/util/XMLHelper.h>
67
68 #include "resolver.h"
69
70 using namespace shibsp;
71 using namespace shibresolver;
72 using namespace opensaml::saml2md;
73 using namespace opensaml;
74 using namespace xmltooling::logging;
75 using namespace xmltooling;
76 using namespace xercesc;
77 using namespace std;
78
79 bool
80 gss_eap_shib_attr_provider::initFromExistingContext(const gss_eap_attr_ctx *manager,
81                                                     const gss_eap_attr_provider *ctx)
82 {
83     const gss_eap_shib_attr_provider *shib;
84
85     if (!gss_eap_attr_provider::initFromExistingContext(manager, ctx)) {
86         return false;
87     }
88
89     m_authenticated = false;
90
91     shib = static_cast<const gss_eap_shib_attr_provider *>(ctx);
92     if (shib != NULL) {
93         m_attributes = duplicateAttributes(shib->getAttributes());
94         m_authenticated = shib->authenticated();
95     }
96
97     return true;
98 }
99
100 bool
101 addRadiusAttribute(const gss_eap_attr_provider *provider,
102                    const gss_buffer_t attribute,
103                    void *data)
104 {
105     const gss_eap_shib_attr_provider *shib;
106     const gss_eap_radius_attr_provider *radius;
107     int authenticated, complete, more = -1;
108     vector <string> attributeIds(1);
109     SimpleAttribute *a;
110
111     radius = static_cast<const gss_eap_radius_attr_provider *>(provider);
112     shib = static_cast<const gss_eap_shib_attr_provider *>(data);
113
114     assert(radius != NULL && shib != NULL);
115
116     string attributeName =
117         gss_eap_attr_ctx::composeAttributeName(ATTR_TYPE_RADIUS, attribute);
118
119     attributeIds.push_back(attributeName);
120     a = new SimpleAttribute(attributeIds);
121     if (a == NULL)
122         return false;
123
124     while (more != 0) {
125         gss_buffer_desc value = GSS_C_EMPTY_BUFFER;
126         OM_uint32 minor;
127
128         if (!radius->getAttribute(attribute,
129                                   &authenticated,
130                                   &complete,
131                                   &value,
132                                   NULL,
133                                   &more))
134             return false;
135
136         string attributeValue((char *)value.value, value.length);
137         a->getValues().push_back(attributeValue);
138
139         gss_release_buffer(&minor, &value);
140     }
141
142     shib->getAttributes().push_back(a);
143
144     return true;
145 }
146
147 bool
148 gss_eap_shib_attr_provider::initFromGssContext(const gss_eap_attr_ctx *manager,
149                                                const gss_cred_id_t gssCred,
150                                                const gss_ctx_id_t gssCtx)
151 {
152     const gss_eap_saml_assertion_provider *saml;
153     const gss_eap_radius_attr_provider *radius;
154     gss_buffer_desc nameBuf = GSS_C_EMPTY_BUFFER;
155     ShibbolethResolver *resolver = NULL;
156     OM_uint32 minor;
157
158     if (!gss_eap_attr_provider::initFromGssContext(manager, gssCred, gssCtx))
159         return false;
160
161     saml = static_cast<const gss_eap_saml_assertion_provider *>
162         (manager->getProvider(ATTR_TYPE_SAML_ASSERTION));
163     radius = static_cast<const gss_eap_radius_attr_provider *>
164         (manager->getProvider(ATTR_TYPE_RADIUS));
165
166     if (gssCred != GSS_C_NO_CREDENTIAL &&
167         gss_display_name(&minor, gssCred->name, &nameBuf, NULL) == GSS_S_COMPLETE)
168         resolver->setApplicationID((const char *)nameBuf.value);
169
170     m_authenticated = false;
171
172     if (radius != NULL) {
173         radius->getAttributeTypes(addRadiusAttribute, (void *)this);
174         m_authenticated = radius->authenticated();
175     }
176
177     if (saml != NULL && saml->getAssertion() != NULL) {
178         resolver->addToken(saml->getAssertion());
179         if (m_authenticated)
180             m_authenticated = saml->authenticated();
181     }
182
183     resolver->resolveAttributes(m_attributes);
184
185     gss_release_buffer(&minor, &nameBuf);
186
187     delete resolver;
188
189     return true;
190 }
191
192 gss_eap_shib_attr_provider::~gss_eap_shib_attr_provider(void)
193 {
194     for_each(m_attributes.begin(),
195              m_attributes.end(),
196              xmltooling::cleanup<Attribute>())
197         ;
198 }
199
200 int
201 gss_eap_shib_attr_provider::getAttributeIndex(const gss_buffer_t attr) const
202 {
203     int i = 0;
204
205     for (vector<Attribute *>::const_iterator a = m_attributes.begin();
206          a != m_attributes.end();
207          ++a)
208     {
209         for (vector<string>::const_iterator s = (*a)->getAliases().begin();
210              s != (*a)->getAliases().end();
211              ++s) {
212             if (attr->length == (*s).length() &&
213                 memcmp((*s).c_str(), attr->value, attr->length) == 0) {
214                 return i;
215             }
216         }
217     }
218
219     return -1;
220 }
221
222 void
223 gss_eap_shib_attr_provider::setAttribute(int complete,
224                                          const gss_buffer_t attr,
225                                          const gss_buffer_t value)
226 {
227     string attrStr((char *)attr->value, attr->length);
228     vector <string> ids(1);
229
230     ids.push_back(attrStr);
231
232     SimpleAttribute *a = new SimpleAttribute(ids);
233
234     if (value->length != 0) {
235         string valueStr((char *)value->value, value->length);
236
237         a->getValues().push_back(valueStr);        
238     }
239
240     m_attributes.push_back(a);
241     m_authenticated = false;
242 }
243
244 void
245 gss_eap_shib_attr_provider::deleteAttribute(const gss_buffer_t attr)
246 {
247     int i;
248
249     i = getAttributeIndex(attr);
250     if (i >= 0)
251         m_attributes.erase(m_attributes.begin() + i);
252
253     m_authenticated = false;
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     *authenticated = m_authenticated;
338     *complete = false;
339
340     if (nvalues > ++i)
341         *more = i;
342
343     return true;
344 }
345
346 gss_any_t
347 gss_eap_shib_attr_provider::mapToAny(int authenticated,
348                                      gss_buffer_t type_id) const
349 {
350     gss_any_t output;
351
352     vector <Attribute *>v = duplicateAttributes(m_attributes);
353
354     output = (gss_any_t)new vector <Attribute *>(v);
355
356     return output;
357 }
358
359 void
360 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
361                                                   gss_any_t input) const
362 {
363     vector <Attribute *> *v = ((vector <Attribute *> *)input);
364     delete v;
365 }
366
367 void
368 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
369 {
370     DDF obj(NULL);
371     DDF attrs(NULL);
372
373     buffer->length = 0;
374     buffer->value = NULL;
375
376     obj.addmember("version").integer(1);
377     obj.addmember("authenticated").integer(m_authenticated);
378
379     attrs = obj.addmember("attributes").list();
380     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
381          a != m_attributes.end(); ++a) {
382         DDF attr = (*a)->marshall();
383         attrs.add(attr);
384     }
385
386     ostringstream sink;
387     sink << attrs;
388     string str = sink.str();
389
390     duplicateBuffer(str, buffer);
391
392     attrs.destroy();
393 }
394
395 bool
396 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
397                                            const gss_buffer_t buffer)
398 {
399     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
400         return false;
401
402     if (buffer->length == 0)
403         return true;
404
405     assert(m_authenticated == false);
406     assert(m_attributes.size() == 0);
407
408     DDF obj(NULL);
409     string str((const char *)buffer->value, buffer->length);
410     istringstream source(str);
411
412     source >> obj;
413
414     if (obj["version"].integer() != 1)
415         return false;
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     attrs.destroy();
428
429     return true;
430 }
431
432 bool
433 gss_eap_shib_attr_provider::init(void)
434 {
435     if (!ShibbolethResolver::init())
436         return false;
437
438     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
439                                        NULL,
440                                        gss_eap_shib_attr_provider::createAttrContext);
441
442     return true;
443 }
444
445 void
446 gss_eap_shib_attr_provider::finalize(void)
447 {
448     ShibbolethResolver::term();
449     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
450 }
451
452 gss_eap_attr_provider *
453 gss_eap_shib_attr_provider::createAttrContext(void)
454 {
455     return new gss_eap_shib_attr_provider;
456 }
457
458 Attribute *
459 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
460 {
461     Attribute *attribute;
462
463     DDF obj = src->marshall();
464     attribute = Attribute::unmarshall(obj);
465     obj.destroy();
466
467     return attribute;
468 }
469
470 vector <Attribute *>
471 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
472 {
473     vector <Attribute *> dst;
474
475     for (vector<Attribute *>::const_iterator a = src.begin();
476          a != src.end();
477          ++a)
478         dst.push_back(duplicateAttribute(*a));
479
480     return dst;
481 }