Next integration phase, metadata and trust conversion.
[shibboleth/cpp-sp.git] / xmlproviders / XMLCredentials.cpp
1 /*
2  *  Copyright 2001-2005 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* XMLCredentials.cpp - a credentials implementation that uses an XML file
18
19    Scott Cantor
20    9/27/02
21
22    $History:$
23 */
24
25 #include "internal.h"
26
27 #include <algorithm>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include <log4cpp/Category.hh>
32 #include <shibsp/exceptions.h>
33
34 using namespace shibsp;
35 using namespace shibboleth;
36 using namespace saml;
37 using namespace xmltooling;
38 using namespace log4cpp;
39 using namespace std;
40
41 namespace {
42     
43     class XMLCredentialsImpl : public ReloadableXMLFileImpl
44     {
45     public:
46         XMLCredentialsImpl(const char* pathname) : ReloadableXMLFileImpl(pathname) { init(); }
47         XMLCredentialsImpl(const DOMElement* e) : ReloadableXMLFileImpl(e) { init(); }
48         void init();
49         ~XMLCredentialsImpl();
50         
51         typedef map<string,ICredResolver*> resolvermap_t;
52         resolvermap_t m_resolverMap;
53     };
54
55     class XMLCredentials : public ICredentials, public ReloadableXMLFile
56     {
57     public:
58         XMLCredentials(const DOMElement* e) : ReloadableXMLFile(e) {}
59         ~XMLCredentials() {}
60         
61         const ICredResolver* lookup(const char* id) const;
62
63     protected:
64         virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
65         virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
66     };
67
68 }
69
70 IPlugIn* XMLCredentialsFactory(const DOMElement* e)
71 {
72     auto_ptr<XMLCredentials> creds(new XMLCredentials(e));
73     creds->getImplementation();
74     return creds.release();
75 }
76
77 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const char* pathname, bool first) const
78 {
79     return new XMLCredentialsImpl(pathname);
80 }
81
82 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const DOMElement* e, bool first) const
83 {
84     return new XMLCredentialsImpl(e);
85 }
86
87 void XMLCredentialsImpl::init()
88 {
89 #ifdef _DEBUG
90     saml::NDC ndc("init");
91 #endif
92     Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Credentials");
93
94     try {
95         if (!saml::XML::isElementNamed(m_root,::XML::CREDS_NS,SHIB_L(Credentials))) {
96             log.error("Construction requires a valid creds file: (creds:Credentials as root element)");
97             throw ConfigurationException("Construction requires a valid creds file: (creds:Credentials as root element)");
98         }
99
100         DOMElement* child=saml::XML::getFirstChildElement(m_root);
101         while (child) {
102             string cr_type;
103             auto_ptr<char> id(XMLString::transcode(child->getAttributeNS(NULL,SHIB_L(Id))));
104             
105             if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(FileResolver)))
106                 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
107             else if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(CustomResolver))) {
108                 xmltooling::auto_ptr_char c(child->getAttributeNS(NULL,SHIB_L(Class)));
109                 cr_type=c.get();
110             }
111             
112             if (!cr_type.empty()) {
113                 try {
114                     IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),child);
115                     ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
116                     if (cr)
117                         m_resolverMap[id.get()]=cr;
118                     else {
119                         log.error("plugin was not a credential resolver");
120                         throw UnknownExtensionException("plugin was not a credential resolver");
121                     }
122                 }
123                 catch (exception& e) {
124                     log.error("failed to instantiate credential resolver (%s): %s", id.get(), e.what());
125                     throw;
126                 }
127             }
128             else {
129                 log.error("unknown or unimplemented type of credential resolver (%s)", id.get());
130                 throw UnknownExtensionException("Unknown or unimplemented type of credential resolver");
131             }
132             
133             child=saml::XML::getNextSiblingElement(child);
134         }
135     }
136     catch (exception& e) {
137         log.errorStream() << "Error while parsing creds configuration: " << e.what() << CategoryStream::ENDLINE;
138         this->~XMLCredentialsImpl();
139         throw;
140     }
141 #ifndef _DEBUG
142     catch (...) {
143         log.error("Unexpected error while parsing creds configuration");
144         this->~XMLCredentialsImpl();
145         throw;
146     }
147 #endif
148 }
149
150 XMLCredentialsImpl::~XMLCredentialsImpl()
151 {
152     for_each(m_resolverMap.begin(),m_resolverMap.end(),xmltooling::cleanup_pair<string,ICredResolver>());
153 }
154
155 const ICredResolver* XMLCredentials::lookup(const char* id) const
156 {
157     if (id) {
158         XMLCredentialsImpl* impl=dynamic_cast<XMLCredentialsImpl*>(getImplementation());
159         XMLCredentialsImpl::resolvermap_t::const_iterator i=impl->m_resolverMap.find(id);
160         if (i!=impl->m_resolverMap.end())
161             return i->second;
162     }
163     return NULL;
164 }