2 * shib-config.cpp -- ShibTarget initialization and finalization routines
4 * Created By: Derek Atkins <derek@ihtfp.com>
9 #include "shib-target.h"
10 #include "ccache-utils.h"
11 #include <shib/shib-threads.h>
13 #include <log4cpp/PropertyConfigurator.hh>
14 #include <log4cpp/Category.hh>
17 using namespace shibboleth;
18 using namespace shibtarget;
21 #ifndef SHIBTARGET_INIFILE
22 #define SHIBTARGET_INIFILE "/opt/shibboleth/etc/shibboleth/shibboleth.ini"
25 class STConfig : public ShibTargetConfig
28 STConfig(const char* app_name, const char* inifile);
32 ShibINI& getINI() { return *ini; }
34 Iterator<const XMLCh*> getPolicies() { return Iterator<const XMLCh*>(policies); }
43 vector<const XMLCh*> policies;
47 STConfig * g_Config = NULL;
48 Mutex * g_lock = NULL;
51 CCache* shibtarget::g_shibTargetCCache = NULL;
53 /****************************************************************************/
57 void ShibTargetConfig::preinit()
60 g_lock = Mutex::create();
63 ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
66 throw runtime_error ("ShibTargetConfig not pre-initialized");
69 throw runtime_error ("No Application name");
77 g_Config = new STConfig(app_name, inifile);
82 ShibTargetConfig& ShibTargetConfig::getConfig()
85 throw SAMLException("ShibTargetConfig::getConfig() called with NULL configuration");
89 ShibTargetConfig::~ShibTargetConfig()
93 if (m_SocketName) free(m_SocketName);
97 /****************************************************************************/
100 STConfig::STConfig(const char* app_name, const char* inifile)
101 : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig()),
105 ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
107 cerr << "Unable to load the INI file: " <<
108 (inifile ? inifile : SHIBTARGET_INIFILE) << endl;
113 extern "C" SAMLAttribute* ScopedFactory(DOMElement* e)
115 return new ScopedAttribute(e);
118 extern "C" SAMLAttribute* SimpleFactory(DOMElement* e)
120 return new SimpleAttribute(e);
123 void STConfig::init()
125 string app = m_app_name;
128 // Initialize Log4cpp
129 if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
130 cerr << "Trying to load logger configuration: " << tag << "\n";
132 log4cpp::PropertyConfigurator::configure(tag);
133 } catch (log4cpp::ConfigureFailure& e) {
134 cerr << "Error reading configuration: " << e.what() << "\n";
137 log4cpp::Category& category = log4cpp::Category::getRoot();
138 category.setPriority(log4cpp::Priority::DEBUG);
139 cerr << "No logger configuration found\n";
142 log4cpp::Category& log = log4cpp::Category::getInstance("shibtarget.STConfig");
144 saml::NDC ndc("STConfig::init");
147 if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
148 samlConf.schema_dir = tag;
149 if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
150 samlConf.ssl_certfile = tag;
151 if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
152 samlConf.ssl_keyfile = tag;
153 if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
154 samlConf.ssl_keypass = tag;
155 if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
156 samlConf.ssl_calist = tag;
159 if (!samlConf.init()) {
160 log.fatal ("Failed to initialize SAML Library");
161 throw runtime_error ("Failed to initialize SAML Library");
163 log.debug ("SAML Initialized");
165 log.crit ("Died initializing SAML Library");
170 if (ini->get_tag(app, SHIBTARGET_TAG_AAP, true, &tag))
173 if (! ini->get_tag (app, SHIBTARGET_TAG_SITES, true, &tag)) {
174 log.fatal("No Sites File found in configuration");
175 throw runtime_error ("No Sites File found in configuration");
178 shibConf.mapperURL=tag;
180 if (ini->get_tag (app, SHIBTARGET_TAG_SITESCERT, true, &tag)) {
181 shibConf.mapperCert = new X509Certificate (X509Certificate::PEM, tag.c_str());
184 log.crit ("Can not read the x509 certificate.");
189 if (ini->get_tag (app, SHIBTARGET_TAG_SITESREFRESH, true, &tag)) {
190 shibConf.mapperRefreshInterval = atoi(tag.c_str());
193 log.crit ("Can not read the mapper refresh interval.");
198 if (!shibConf.init()) {
199 log.fatal ("Failed to initialize Shib library");
200 throw runtime_error ("Failed to initialize Shib Library");
202 log.debug ("Shib Initialized");
204 log.crit ("Failed initializing Shib library.");
208 // Initialize the SHAR Cache
209 if (!strcmp (app.c_str(), SHIBTARGET_SHAR)) {
210 const char * cache_type = NULL;
211 if (ini->get_tag (app, SHIBTARGET_TAG_CACHETYPE, true, &tag))
212 cache_type = tag.c_str();
214 g_shibTargetCCache = CCache::getInstance(cache_type);
217 // Load any SAML extensions
218 string ext = "extensions:saml";
219 if (ini->exists(ext)) {
220 saml::NDC ndc("load_extensions");
221 ShibINI::Iterator* iter = ini->tag_iterator(ext);
223 for (const string* str = iter->begin(); str; str = iter->next()) {
224 string file = ini->get(ext, *str);
227 samlConf.saml_register_extension(file.c_str(),ini);
228 log.debug("%s: loading %s", str->c_str(), file.c_str());
230 catch (SAMLException& e)
232 log.crit("%s: %s", str->c_str(), e.what());
238 // Register attributes based on built-in classes.
239 if (ini->exists("attributes")) {
240 log.info("registering attributes");
241 ShibINI::Iterator* iter=ini->tag_iterator("attributes");
242 for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
244 const string factory=ini->get("attributes",*attrname);
245 if (factory=="scoped")
247 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
248 SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&ScopedFactory);
249 log.info("registered scoped attribute (%s)",attrname->c_str());
251 else if (factory=="simple")
253 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
254 SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&SimpleFactory);
255 log.info("registered simple attribute (%s)",attrname->c_str());
261 // Load SAML policies.
262 if (ini->exists(SHIBTARGET_POLICIES)) {
263 log.info("loading SAML policies");
264 ShibINI::Iterator* iter = ini->tag_iterator(SHIBTARGET_POLICIES);
266 for (const string* str = iter->begin(); str; str = iter->next()) {
267 policies.push_back(XMLString::transcode(ini->get(SHIBTARGET_POLICIES, *str).c_str()));
272 string sockname=ini->get(SHIBTARGET_GENERAL, "sharsocket");
274 if (sockname.length()>0)
275 m_SocketName=atoi(sockname.c_str());
277 m_SocketName=SHIB_SHAR_SOCKET;
279 if (sockname.length()>0)
280 m_SocketName=strdup(sockname.c_str());
282 m_SocketName=strdup(SHIB_SHAR_SOCKET);
286 log.debug("finished");
289 STConfig::~STConfig()
291 for (vector<const XMLCh*>::iterator i=policies.begin(); i!=policies.end(); i++)
292 delete const_cast<XMLCh*>(*i);
294 // Unregister attributes based on built-in classes.
295 if (ini && ini->exists("attributes")) {
296 ShibINI::Iterator* iter=ini->tag_iterator("attributes");
297 for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
299 const string factory=ini->get("attributes",*attrname);
300 if (factory=="scoped")
302 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
303 SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
305 else if (factory=="simple")
307 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
308 SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
316 if (g_shibTargetCCache)
317 delete g_shibTargetCCache;
328 void STConfig::shutdown()