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;
20 using namespace log4cpp;
22 #ifndef SHIBTARGET_INIFILE
23 #define SHIBTARGET_INIFILE "/opt/shibboleth/etc/shibboleth/shibboleth.ini"
26 class STConfig : public ShibTargetConfig
29 STConfig(const char* app_name, const char* inifile);
33 ShibINI& getINI() { return *ini; }
35 Iterator<const XMLCh*> getPolicies() { return Iterator<const XMLCh*>(policies); }
44 vector<const XMLCh*> policies;
48 STConfig * g_Config = NULL;
49 Mutex * g_lock = NULL;
52 CCache* shibtarget::g_shibTargetCCache = NULL;
54 /****************************************************************************/
58 void ShibTargetConfig::preinit()
61 g_lock = Mutex::create();
64 ShibTargetConfig& ShibTargetConfig::init(const char* app_name, const char* inifile)
67 throw runtime_error ("ShibTargetConfig not pre-initialized");
70 throw runtime_error ("No Application name");
78 g_Config = new STConfig(app_name, inifile);
83 ShibTargetConfig& ShibTargetConfig::getConfig()
86 throw SAMLException("ShibTargetConfig::getConfig() called with NULL configuration");
90 ShibTargetConfig::~ShibTargetConfig()
94 if (m_SocketName) free(m_SocketName);
98 /****************************************************************************/
101 STConfig::STConfig(const char* app_name, const char* inifile)
102 : samlConf(SAMLConfig::getConfig()), shibConf(ShibConfig::getConfig()),
106 ini = new ShibINI((inifile ? inifile : SHIBTARGET_INIFILE));
108 cerr << "Unable to load the INI file: " <<
109 (inifile ? inifile : SHIBTARGET_INIFILE) << endl;
114 void STConfig::init()
116 string app = m_app_name;
119 // Initialize Log4cpp
120 if (ini->get_tag (app, SHIBTARGET_TAG_LOGGER, true, &tag)) {
121 cerr << "Trying to load logger configuration: " << tag << "\n";
123 PropertyConfigurator::configure(tag);
124 } catch (ConfigureFailure& e) {
125 cerr << "Error reading configuration: " << e.what() << "\n";
128 Category& category = Category::getRoot();
129 category.setPriority(log4cpp::Priority::DEBUG);
130 cerr << "No logger configuration found\n";
133 Category& log = Category::getInstance("shibtarget.STConfig");
135 saml::NDC ndc("STConfig::init");
137 // Init SAML Configuration
138 if (ini->get_tag (app, SHIBTARGET_TAG_SAMLCOMPAT, true, &tag))
139 samlConf.compatibility_mode = ShibINI::boolean(tag);
140 if (ini->get_tag (app, SHIBTARGET_TAG_SCHEMAS, true, &tag))
141 samlConf.schema_dir = tag;
143 // Init SAML Binding Configuration
144 if (ini->get_tag (app, SHIBTARGET_TAG_AATIMEOUT, true, &tag))
145 samlConf.binding_defaults.timeout = atoi(tag.c_str());
146 if (ini->get_tag (app, SHIBTARGET_TAG_AACONNECTTO, true, &tag))
147 samlConf.binding_defaults.conn_timeout = atoi(tag.c_str());
148 if (ini->get_tag (app, SHIBTARGET_TAG_CERTFILE, true, &tag))
149 samlConf.binding_defaults.ssl_certfile = tag;
150 if (ini->get_tag (app, SHIBTARGET_TAG_KEYFILE, true, &tag))
151 samlConf.binding_defaults.ssl_keyfile = tag;
152 if (ini->get_tag (app, SHIBTARGET_TAG_KEYPASS, true, &tag))
153 samlConf.binding_defaults.ssl_keypass = tag;
154 if (ini->get_tag (app, SHIBTARGET_TAG_CALIST, true, &tag))
155 samlConf.binding_defaults.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))
170 shibConf.aapFile=tag;
173 if (!shibConf.init()) {
174 log.fatal ("Failed to initialize Shib library");
175 throw runtime_error ("Failed to initialize Shib Library");
177 log.debug ("Shib Initialized");
179 log.crit ("Failed initializing Shib library.");
183 // Load any SAML extensions
184 string ext = "extensions:saml";
185 if (ini->exists(ext)) {
186 saml::NDC ndc("load_extensions");
187 ShibINI::Iterator* iter = ini->tag_iterator(ext);
189 for (const string* str = iter->begin(); str; str = iter->next()) {
190 string file = ini->get(ext, *str);
193 samlConf.saml_register_extension(file.c_str(),ini);
194 log.debug("%s: loading %s", str->c_str(), file.c_str());
196 catch (SAMLException& e)
198 log.crit("%s: %s", str->c_str(), e.what());
204 // Load the specified metadata.
205 if (ini->get_tag(app, SHIBTARGET_TAG_METADATA, true, &tag) && ini->exists(tag))
207 ShibINI::Iterator* iter=ini->tag_iterator(tag);
208 for (const string* prov=iter->begin(); prov; prov=iter->next())
210 const string source=ini->get(tag,*prov);
211 log.info("registering metadata provider: type=%s, source=%s",prov->c_str(),source.c_str());
212 if (!shibConf.addMetadata(prov->c_str(),source.c_str()))
214 log.crit("error adding metadata provider: type=%s, source=%s",prov->c_str(),source.c_str());
215 if (!strcmp(app.c_str(), SHIBTARGET_SHAR))
216 throw runtime_error("error adding metadata provider");
222 // Load SAML policies.
223 if (ini->exists(SHIBTARGET_POLICIES)) {
224 log.info("loading SAML policies");
225 ShibINI::Iterator* iter = ini->tag_iterator(SHIBTARGET_POLICIES);
227 for (const string* str = iter->begin(); str; str = iter->next()) {
228 policies.push_back(XMLString::transcode(ini->get(SHIBTARGET_POLICIES, *str).c_str()));
233 // Initialize the SHAR Cache
234 if (!strcmp (app.c_str(), SHIBTARGET_SHAR)) {
235 const char * cache_type = NULL;
236 if (ini->get_tag (app, SHIBTARGET_TAG_CACHETYPE, true, &tag))
237 cache_type = tag.c_str();
239 g_shibTargetCCache = CCache::getInstance(cache_type);
242 string sockname=ini->get(SHIBTARGET_GENERAL, "sharsocket");
244 if (sockname.length()>0)
245 m_SocketName=atoi(sockname.c_str());
247 m_SocketName=SHIB_SHAR_SOCKET;
249 if (sockname.length()>0)
250 m_SocketName=strdup(sockname.c_str());
252 m_SocketName=strdup(SHIB_SHAR_SOCKET);
256 log.debug("finished");
259 STConfig::~STConfig()
261 for (vector<const XMLCh*>::iterator i=policies.begin(); i!=policies.end(); i++)
262 delete const_cast<XMLCh*>(*i);
266 if (g_shibTargetCCache)
267 delete g_shibTargetCCache;
278 void STConfig::shutdown()