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