2 * shib-config.cpp -- ShibTarget initialization and finalization routines
4 * Created By: Derek Atkins <derek@ihtfp.com>
9 #include "shib-target.h"
10 #include <shib/shib-threads.h>
12 #include <log4cpp/PropertyConfigurator.hh>
13 #include <log4cpp/Category.hh>
16 using namespace shibboleth;
17 using namespace shibtarget;
20 #ifndef SHIBTARGET_INIFILE
21 #define SHIBTARGET_INIFILE "/opt/shibboleth/etc/shibboleth/shibboleth.ini"
24 class STConfig : public ShibTargetConfig
27 STConfig(const char* app_name, const char* inifile);
31 ShibINI& getINI() { return *ini; }
33 Iterator<const XMLCh*> getPolicies() { return Iterator<const XMLCh*>(policies); }
42 vector<const XMLCh*> policies;
46 STConfig * g_Config = NULL;
47 Mutex * g_lock = NULL;
50 CCache* shibtarget::g_shibTargetCCache = NULL;
52 /****************************************************************************/
56 void ShibTargetConfig::preinit()
59 g_lock = Mutex::create();
62 ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
65 throw runtime_error ("ShibTargetConfig not pre-initialized");
68 throw runtime_error ("No Application name");
76 g_Config = new STConfig(app_name, inifile);
81 ShibTargetConfig& ShibTargetConfig::getConfig()
84 throw SAMLException("ShibTargetConfig::getConfig() called with NULL configuration");
88 ShibTargetConfig::~ShibTargetConfig()
92 if (m_SocketName) free(m_SocketName);
96 /****************************************************************************/
99 STConfig::STConfig(const char* app_name, const char* inifile)
100 : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig()),
104 ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
106 cerr << "Unable to load the INI file: " <<
107 (inifile ? inifile : SHIBTARGET_INIFILE) << endl;
112 extern "C" SAMLAttribute* ScopedFactory(DOMElement* e)
114 return new ScopedAttribute(e);
117 extern "C" SAMLAttribute* SimpleFactory(DOMElement* e)
119 return new SimpleAttribute(e);
122 void STConfig::init()
124 string app = m_app_name;
127 // Initialize Log4cpp
128 if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
129 cerr << "Trying to load logger configuration: " << tag << "\n";
131 log4cpp::PropertyConfigurator::configure(tag);
132 } catch (log4cpp::ConfigureFailure& e) {
133 cerr << "Error reading configuration: " << e.what() << "\n";
136 log4cpp::Category& category = log4cpp::Category::getRoot();
137 category.setPriority(log4cpp::Priority::DEBUG);
138 cerr << "No logger configuration found\n";
141 log4cpp::Category& log = log4cpp::Category::getInstance("shibtarget.STConfig");
143 saml::NDC ndc("STConfig::init");
146 if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
147 samlConf.schema_dir = tag;
148 if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
149 samlConf.ssl_certfile = tag;
150 if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
151 samlConf.ssl_keyfile = tag;
152 if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
153 samlConf.ssl_keypass = tag;
154 if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
155 samlConf.ssl_calist = tag;
158 if (!samlConf.init()) {
159 log.fatal ("Failed to initialize SAML Library");
160 throw runtime_error ("Failed to initialize SAML Library");
162 log.debug ("SAML Initialized");
164 log.crit ("Died initializing SAML Library");
169 if (ini->get_tag(app, SHIBTARGET_TAG_AAP, true, &tag))
172 if (! ini->get_tag (app, SHIBTARGET_TAG_SITES, true, &tag)) {
173 log.fatal("No Sites File found in configuration");
174 throw runtime_error ("No Sites File found in configuration");
177 shibConf.mapperURL=tag;
179 if (ini->get_tag (app, SHIBTARGET_TAG_SITESCERT, true, &tag)) {
180 shibConf.mapperCert = new X509Certificate (X509Certificate::PEM, tag.c_str());
183 log.crit ("Can not read the x509 certificate.");
188 if (ini->get_tag (app, SHIBTARGET_TAG_SITESREFRESH, true, &tag)) {
189 shibConf.mapperRefreshInterval = atoi(tag.c_str());
192 log.crit ("Can not read the mapper refresh interval.");
197 if (!shibConf.init()) {
198 log.fatal ("Failed to initialize Shib library");
199 throw runtime_error ("Failed to initialize Shib Library");
201 log.debug ("Shib Initialized");
203 log.crit ("Failed initializing Shib library.");
207 // Initialize the SHAR Cache
208 if (!strcmp (app.c_str(), SHIBTARGET_SHAR)) {
209 const char * cache_type = NULL;
210 if (ini->get_tag (app, SHIBTARGET_TAG_CACHETYPE, true, &tag))
211 cache_type = tag.c_str();
213 g_shibTargetCCache = CCache::getInstance(cache_type);
216 // Load any SAML extensions
217 string ext = "extensions:saml";
218 if (ini->exists(ext)) {
219 saml::NDC ndc("load_extensions");
220 ShibINI::Iterator* iter = ini->tag_iterator(ext);
222 for (const string* str = iter->begin(); str; str = iter->next()) {
223 string file = ini->get(ext, *str);
226 samlConf.saml_register_extension(file.c_str(),ini);
227 log.debug("%s: loading %s", str->c_str(), file.c_str());
229 catch (SAMLException& e)
231 log.crit("%s: %s", str->c_str(), e.what());
237 // Register attributes based on built-in classes.
238 if (ini->exists("attributes")) {
239 ShibINI::Iterator* iter=ini->tag_iterator("attributes");
240 for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
242 const string factory=ini->get("attributes",*attrname);
243 if (factory=="scoped")
245 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
246 SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&ScopedFactory);
248 else if (factory=="simple")
250 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
251 SAMLAttribute::regFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI,&SimpleFactory);
257 // Load SAML policies.
258 if (ini->exists(SHIBTARGET_POLICIES)) {
259 log.debug("loading SAML policies");
260 ShibINI::Iterator* iter = ini->tag_iterator(SHIBTARGET_POLICIES);
262 for (const string* str = iter->begin(); str; str = iter->next()) {
263 policies.push_back(XMLString::transcode(ini->get(SHIBTARGET_POLICIES, *str).c_str()));
268 string sockname=ini->get(SHIBTARGET_GENERAL, "sharsocket");
270 if (sockname.length()>0)
271 m_SocketName=atoi(sockname.c_str());
273 m_SocketName=SHIB_SHAR_SOCKET;
275 if (sockname.length()>0)
276 m_SocketName=strdup(sockname.c_str());
278 m_SocketName=strdup(SHIB_SHAR_SOCKET);
282 log.debug("finished");
285 STConfig::~STConfig()
287 for (vector<const XMLCh*>::iterator i=policies.begin(); i!=policies.end(); i++)
288 delete const_cast<XMLCh*>(*i);
290 // Unregister attributes based on built-in classes.
291 if (ini && ini->exists("attributes")) {
292 ShibINI::Iterator* iter=ini->tag_iterator("attributes");
293 for (const string* attrname=iter->begin(); attrname; attrname=iter->next())
295 const string factory=ini->get("attributes",*attrname);
296 if (factory=="scoped")
298 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
299 SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
301 else if (factory=="simple")
303 auto_ptr<XMLCh> temp(XMLString::transcode(attrname->c_str()));
304 SAMLAttribute::unregFactory(temp.get(),shibboleth::Constants::SHIB_ATTRIBUTE_NAMESPACE_URI);
312 if (g_shibTargetCCache)
313 delete g_shibTargetCCache;
324 void STConfig::shutdown()