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