try to propagate RADIUS authentication state
[moonshot.git] / mech_eap / 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
312     shibAttr = getAttribute(attr);
313     if (shibAttr == NULL)
314         return false;
315
316     if (*more == -1) {
317         *more = 0;
318     } else if (*more >= (int)shibAttr->valueCount()) {
319         *more = 0;
320         return true;
321     }
322
323     buf.value = (void *)shibAttr->getString(*more);
324     buf.length = strlen((char *)buf.value);
325
326     duplicateBuffer(buf, value);
327
328     if (display_value != NULL)
329         duplicateBuffer(buf, display_value);
330  
331     *authenticated = m_authenticated;
332     *complete = false;
333
334     return true;
335 }
336
337 gss_any_t
338 gss_eap_shib_attr_provider::mapToAny(int authenticated,
339                                      gss_buffer_t type_id) const
340 {
341     gss_any_t output;
342
343     vector <Attribute *>v = duplicateAttributes(m_attributes);
344
345     output = (gss_any_t)new vector <Attribute *>(v);
346
347     return output;
348 }
349
350 void
351 gss_eap_shib_attr_provider::releaseAnyNameMapping(gss_buffer_t type_id,
352                                                   gss_any_t input) const
353 {
354     vector <Attribute *> *v = ((vector <Attribute *> *)input);
355     delete v;
356 }
357
358 void
359 gss_eap_shib_attr_provider::exportToBuffer(gss_buffer_t buffer) const
360 {
361     DDF obj(NULL);
362     DDF attrs(NULL);
363
364     buffer->length = 0;
365     buffer->value = NULL;
366
367     obj.addmember("version").integer(1);
368     obj.addmember("authenticated").integer(m_authenticated);
369
370     attrs = obj.addmember("attributes").list();
371     for (vector<Attribute*>::const_iterator a = m_attributes.begin();
372          a != m_attributes.end(); ++a) {
373         DDF attr = (*a)->marshall();
374         attrs.add(attr);
375     }
376
377     ostringstream sink;
378     sink << attrs;
379     string str = sink.str();
380
381     duplicateBuffer(str, buffer);
382
383     attrs.destroy();
384 }
385
386 bool
387 gss_eap_shib_attr_provider::initFromBuffer(const gss_eap_attr_ctx *ctx,
388                                            const gss_buffer_t buffer)
389 {
390     if (!gss_eap_attr_provider::initFromBuffer(ctx, buffer))
391         return false;
392
393     if (buffer->length == 0)
394         return true;
395
396     assert(m_authenticated == false);
397     assert(m_attributes.size() == 0);
398
399     DDF obj(NULL);
400     string str((const char *)buffer->value, buffer->length);
401     istringstream source(str);
402
403     source >> obj;
404
405     if (obj["version"].integer() != 1)
406         return false;
407
408     m_authenticated = (obj["authenticated"].integer() != 0);
409
410     DDF attrs = obj["attributes"];
411     DDF attr = attrs.first();
412     while (!attr.isnull()) {
413         Attribute *attribute = Attribute::unmarshall(attr);
414         m_attributes.push_back(attribute);
415         attr = attrs.next();
416     }
417
418     attrs.destroy();
419
420     return true;
421 }
422
423 bool
424 gss_eap_shib_attr_provider::init(void)
425 {
426     if (!ShibbolethResolver::init())
427         return false;
428
429     gss_eap_attr_ctx::registerProvider(ATTR_TYPE_LOCAL,
430                                        NULL,
431                                        gss_eap_shib_attr_provider::createAttrContext);
432
433     return true;
434 }
435
436 void
437 gss_eap_shib_attr_provider::finalize(void)
438 {
439     ShibbolethResolver::term();
440     gss_eap_attr_ctx::unregisterProvider(ATTR_TYPE_LOCAL);
441 }
442
443 gss_eap_attr_provider *
444 gss_eap_shib_attr_provider::createAttrContext(void)
445 {
446     return new gss_eap_shib_attr_provider;
447 }
448
449 Attribute *
450 gss_eap_shib_attr_provider::duplicateAttribute(const Attribute *src)
451 {
452     Attribute *attribute;
453
454     DDF obj = src->marshall();
455     attribute = Attribute::unmarshall(obj);
456     obj.destroy();
457
458     return attribute;
459 }
460
461 vector <Attribute *>
462 gss_eap_shib_attr_provider::duplicateAttributes(const vector <Attribute *>src)
463 {
464     vector <Attribute *> dst;
465
466     for (vector<Attribute *>::const_iterator a = src.begin();
467          a != src.end();
468          ++a)
469         dst.push_back(duplicateAttribute(*a));
470
471     return dst;
472 }