2 * shib-config.cpp -- ShibTarget initialization and finalization routines
4 * Created By: Derek Atkins <derek@ihtfp.com>
9 #include "shib-target.h"
11 #include <log4cpp/PropertyConfigurator.hh>
12 #include <log4cpp/Category.hh>
15 using namespace shibboleth;
16 using namespace shibtarget;
19 #ifndef SHIBTARGET_INIFILE
20 #define SHIBTARGET_INIFILE "/etc/shibboleth.ini"
23 class STConfig : public ShibTargetConfig
26 STConfig(const char* app_name, const char* inifile);
29 ShibINI& getINI() { return *ini; }
40 STConfig * g_Config = NULL;
43 CCache* shibtarget::g_shibTargetCCache = NULL;
45 /****************************************************************************/
49 ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
52 throw runtime_error ("No Application name");
59 g_Config = new STConfig(app_name, inifile);
65 /****************************************************************************/
68 class DummyMapper : public IOriginSiteMapper
73 virtual Iterator<xstring> getHandleServiceNames(const XMLCh* originSite) { return Iterator<xstring>(m_hsnames); }
74 virtual Key* getHandleServiceKey(const XMLCh* handleService) { return NULL; }
75 virtual Iterator<xstring> getSecurityDomains(const XMLCh* originSite);
76 virtual Iterator<X509Certificate*> getTrustedRoots() { return Iterator<X509Certificate*>(); }
79 typedef map<xstring,vector<xstring>*> domains_t;
81 vector<xstring> m_hsnames;
84 DummyMapper::DummyMapper()
86 auto_ptr<XMLCh> buf(XMLString::transcode("wayf.internet2.edu"));
87 m_hsnames.push_back(buf.get());
90 Iterator<xstring> DummyMapper::getSecurityDomains(const XMLCh* originSite)
92 domains_t::iterator i=m_domains.find(originSite);
93 if (i==m_domains.end())
95 vector<xstring>* pv=new vector<xstring>();
96 pv->push_back(originSite);
97 pair<domains_t::iterator,bool> p=m_domains.insert(domains_t::value_type(originSite,pv));
100 return Iterator<xstring>(*(i->second));
103 DummyMapper::~DummyMapper()
105 for (domains_t::iterator i=m_domains.begin(); i!=m_domains.end(); i++)
112 /****************************************************************************/
115 STConfig::STConfig(const char* app_name, const char* inifile)
116 : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig())
118 ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
120 string app = app_name;
123 // Initialize Log4cpp
124 if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
125 cerr << "Trying to load logger configuration: " << tag << "\n";
127 log4cpp::PropertyConfigurator::configure(tag);
128 } catch (log4cpp::ConfigureFailure& e) {
129 cerr << "Error reading configuration: " << e.what() << "\n";
132 log4cpp::Category& category = log4cpp::Category::getRoot();
133 category.setPriority(log4cpp::Priority::DEBUG);
134 cerr << "No logger configuration found\n";
137 log4cpp::Category& log = log4cpp::Category::getInstance("shibtarget.STConfig");
140 if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
141 samlConf.schema_dir = tag;
142 if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
143 samlConf.ssl_certfile = tag;
144 if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
145 samlConf.ssl_keyfile = tag;
146 if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
147 samlConf.ssl_keypass = tag;
148 if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
149 samlConf.ssl_calist = tag;
151 if (!samlConf.init()) {
152 log.error ("Failed to initialize SAML Library");
153 throw runtime_error ("Failed to initialize SAML Library");
155 log.debug ("SAML Initialized");
158 if (! ini->get_tag (app, SHIBTARGET_TAG_SITES, true, &tag)) {
159 log.crit("No Sites File found in configuration");
160 throw runtime_error ("No Sites File found in configuration");
163 shibConf.origin_mapper = new XMLOriginSiteMapper(tag.c_str(),
164 Iterator<X509Certificate*>());
166 if (!shibConf.init()) {
167 log.error ("Failed to initialize Shib library");
168 throw runtime_error ("Failed to initialize Shib Library");
170 log.debug ("Shib Initialized");
172 // Initialize the SHAR Cache
173 if (!strcmp (app_name, SHIBTARGET_SHAR))
174 g_shibTargetCCache = CCache::getInstance();
176 // Load any extensions
177 string ext = "extensions";
178 if (ini->exists(ext)) {
179 saml::NDC ndc("load extensions");
180 ShibINI::Iterator* iter = ini->tag_iterator(ext);
182 for (const string* str = iter->begin(); str; str = iter->next()) {
183 string file = ini->get(ext, *str);
186 samlConf.saml_register_extension(file.c_str());
187 log.debug("%s: loading %s", str->c_str(), file.c_str());
189 catch (SAMLException& e)
191 log.error("%s: %s", str->c_str(), e.what());
198 log.debug("finished");
201 STConfig::~STConfig()
205 if (g_shibTargetCCache)
206 delete g_shibTargetCCache;
208 delete shibConf.origin_mapper;
218 void STConfig::shutdown()