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);
30 ShibINI& getINI() { return *ini; }
32 Iterator<const XMLCh*> getPolicies() { return Iterator<const XMLCh*>(policies); }
40 vector<const XMLCh*> policies;
44 STConfig * g_Config = NULL;
45 Mutex * g_lock = NULL;
48 CCache* shibtarget::g_shibTargetCCache = NULL;
50 /****************************************************************************/
54 void ShibTargetConfig::preinit()
57 g_lock = Mutex::create();
60 ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
63 throw runtime_error ("ShibTargetConfig not pre-initialized");
66 throw runtime_error ("No Application name");
75 g_Config = new STConfig(app_name, inifile);
79 ShibTargetConfig& ShibTargetConfig::getConfig()
82 throw SAMLException("ShibTargetConfig::getConfig() called with NULL configuration");
86 ShibTargetConfig::~ShibTargetConfig()
90 if (m_SocketName) free(m_SocketName);
94 /****************************************************************************/
97 STConfig::STConfig(const char* app_name, const char* inifile)
98 : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig())
101 ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
103 cerr << "Unable to load the INI file: " <<
104 (inifile ? inifile : SHIBTARGET_INIFILE) << endl;
108 string app = app_name;
111 // Initialize Log4cpp
112 if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
113 cerr << "Trying to load logger configuration: " << tag << "\n";
115 log4cpp::PropertyConfigurator::configure(tag);
116 } catch (log4cpp::ConfigureFailure& e) {
117 cerr << "Error reading configuration: " << e.what() << "\n";
120 log4cpp::Category& category = log4cpp::Category::getRoot();
121 category.setPriority(log4cpp::Priority::DEBUG);
122 cerr << "No logger configuration found\n";
125 log4cpp::Category& log = log4cpp::Category::getInstance("shibtarget.STConfig");
128 if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
129 samlConf.schema_dir = tag;
130 if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
131 samlConf.ssl_certfile = tag;
132 if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
133 samlConf.ssl_keyfile = tag;
134 if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
135 samlConf.ssl_keypass = tag;
136 if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
137 samlConf.ssl_calist = tag;
140 if (!samlConf.init()) {
141 log.fatal ("Failed to initialize SAML Library");
142 throw runtime_error ("Failed to initialize SAML Library");
144 log.debug ("SAML Initialized");
146 log.crit ("Died initializing SAML Library");
151 if (! ini->get_tag (app, SHIBTARGET_TAG_SITES, true, &tag)) {
152 log.fatal("No Sites File found in configuration");
153 throw runtime_error ("No Sites File found in configuration");
156 shibConf.mapperURL=tag;
158 if (ini->get_tag (app, SHIBTARGET_TAG_SITESCERT, true, &tag)) {
159 shibConf.mapperCert = new X509Certificate (X509Certificate::PEM, tag.c_str());
162 log.crit ("Can not read the x509 certificate.");
167 if (!shibConf.init()) {
168 log.fatal ("Failed to initialize Shib library");
169 throw runtime_error ("Failed to initialize Shib Library");
171 log.debug ("Shib Initialized");
173 log.crit ("Failed initializing Shib library.");
177 // Initialize the SHAR Cache
178 if (!strcmp (app_name, SHIBTARGET_SHAR))
179 g_shibTargetCCache = CCache::getInstance(NULL);
181 // Load any SAML extensions
182 string ext = "extensions:saml";
183 if (ini->exists(ext)) {
184 saml::NDC ndc("load extensions");
185 ShibINI::Iterator* iter = ini->tag_iterator(ext);
187 for (const string* str = iter->begin(); str; str = iter->next()) {
188 string file = ini->get(ext, *str);
191 samlConf.saml_register_extension(file.c_str(),ini);
192 log.debug("%s: loading %s", str->c_str(), file.c_str());
194 catch (SAMLException& e)
196 log.crit("%s: %s", str->c_str(), e.what());
202 // Load SAML policies.
203 if (ini->exists(ext)) {
204 log.debug("loading SAML policies");
205 ShibINI::Iterator* iter = ini->tag_iterator(SHIBTARGET_POLICIES);
207 for (const string* str = iter->begin(); str; str = iter->next()) {
208 policies.push_back(XMLString::transcode(ini->get(ext, *str).c_str()));
213 string sockname=ini->get(SHIBTARGET_GENERAL, "sharsocket");
215 if (sockname.length()>0)
216 m_SocketName=atoi(sockname.c_str());
218 m_SocketName=SHIB_SHAR_SOCKET;
220 if (sockname.length()>0)
221 m_SocketName=strdup(sockname.c_str());
223 m_SocketName=strdup(SHIB_SHAR_SOCKET);
227 log.debug("finished");
230 STConfig::~STConfig()
232 for (vector<const XMLCh*>::iterator i=policies.begin(); i!=policies.end(); i++)
233 delete const_cast<XMLCh*>(*i);
237 if (g_shibTargetCCache)
238 delete g_shibTargetCCache;
249 void STConfig::shutdown()