7b5ac5f597f6f448c84d95fe381442e615ff30ab
[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
33 using namespace shibboleth;
34 using namespace saml;
35 using namespace log4cpp;
36 using namespace std;
37
38 namespace {
39     
40     class XMLCredentialsImpl : public ReloadableXMLFileImpl
41     {
42     public:
43         XMLCredentialsImpl(const char* pathname) : ReloadableXMLFileImpl(pathname) { init(); }
44         XMLCredentialsImpl(const DOMElement* e) : ReloadableXMLFileImpl(e) { init(); }
45         void init();
46         ~XMLCredentialsImpl();
47         
48         typedef map<string,ICredResolver*> resolvermap_t;
49         resolvermap_t m_resolverMap;
50     };
51
52     class XMLCredentials : public ICredentials, public ReloadableXMLFile
53     {
54     public:
55         XMLCredentials(const DOMElement* e) : ReloadableXMLFile(e) {}
56         ~XMLCredentials() {}
57         
58         const ICredResolver* lookup(const char* id) const;
59
60     protected:
61         virtual ReloadableXMLFileImpl* newImplementation(const char* pathname, bool first=true) const;
62         virtual ReloadableXMLFileImpl* newImplementation(const DOMElement* e, bool first=true) const;
63     };
64
65 }
66
67 IPlugIn* XMLCredentialsFactory(const DOMElement* e)
68 {
69     auto_ptr<XMLCredentials> creds(new XMLCredentials(e));
70     creds->getImplementation();
71     return creds.release();
72 }
73
74 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const char* pathname, bool first) const
75 {
76     return new XMLCredentialsImpl(pathname);
77 }
78
79 ReloadableXMLFileImpl* XMLCredentials::newImplementation(const DOMElement* e, bool first) const
80 {
81     return new XMLCredentialsImpl(e);
82 }
83
84 void XMLCredentialsImpl::init()
85 {
86 #ifdef _DEBUG
87     saml::NDC ndc("init");
88 #endif
89     Category& log=Category::getInstance(XMLPROVIDERS_LOGCAT".Credentials");
90
91     try {
92         if (!saml::XML::isElementNamed(m_root,::XML::CREDS_NS,SHIB_L(Credentials))) {
93             log.error("Construction requires a valid creds file: (creds:Credentials as root element)");
94             throw CredentialException("Construction requires a valid creds file: (creds:Credentials as root element)");
95         }
96
97         DOMElement* child=saml::XML::getFirstChildElement(m_root);
98         while (child) {
99             string cr_type;
100             auto_ptr<char> id(XMLString::transcode(child->getAttributeNS(NULL,SHIB_L(Id))));
101             
102             if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(FileResolver)))
103                 cr_type="edu.internet2.middleware.shibboleth.common.Credentials.FileCredentialResolver";
104             else if (saml::XML::isElementNamed(child,::XML::CREDS_NS,SHIB_L(CustomResolver))) {
105                 auto_ptr_char c(child->getAttributeNS(NULL,SHIB_L(Class)));
106                 cr_type=c.get();
107             }
108             
109             if (!cr_type.empty()) {
110                 try {
111                     IPlugIn* plugin=SAMLConfig::getConfig().getPlugMgr().newPlugin(cr_type.c_str(),child);
112                     ICredResolver* cr=dynamic_cast<ICredResolver*>(plugin);
113                     if (cr)
114                         m_resolverMap[id.get()]=cr;
115                     else {
116                         log.error("plugin was not a credential resolver");
117                         throw UnsupportedExtensionException("plugin was not a credential resolver");
118                     }
119                 }
120                 catch (SAMLException& e) {
121                     log.error("failed to instantiate credential resolver (%s): %s", id.get(), e.what());
122                     throw;
123                 }
124             }
125             else {
126                 log.error("unknown or unimplemented type of credential resolver (%s)", id.get());
127                 throw CredentialException("Unknown or unimplemented type of credential resolver");
128             }
129             
130             child=saml::XML::getNextSiblingElement(child);
131         }
132     }
133     catch (SAMLException& e) {
134         log.errorStream() << "Error while parsing creds configuration: " << e.what() << CategoryStream::ENDLINE;
135         this->~XMLCredentialsImpl();
136         throw;
137     }
138 #ifndef _DEBUG
139     catch (...) {
140         log.error("Unexpected error while parsing creds configuration");
141         this->~XMLCredentialsImpl();
142         throw;
143     }
144 #endif
145 }
146
147 XMLCredentialsImpl::~XMLCredentialsImpl()
148 {
149     for_each(m_resolverMap.begin(),m_resolverMap.end(),xmltooling::cleanup_pair<string,ICredResolver>());
150 }
151
152 const ICredResolver* XMLCredentials::lookup(const char* id) const
153 {
154     if (id) {
155         XMLCredentialsImpl* impl=dynamic_cast<XMLCredentialsImpl*>(getImplementation());
156         XMLCredentialsImpl::resolvermap_t::const_iterator i=impl->m_resolverMap.find(id);
157         if (i!=impl->m_resolverMap.end())
158             return i->second;
159     }
160     return NULL;
161 }