* $Id$
*/
+#define SHIBSP_LITE
+
#ifdef SOLARIS2
#undef _XOPEN_SOURCE // causes gethostname conflict in unistd.h
#endif
#include <shibsp/exceptions.h>
#include <shibsp/RequestMapper.h>
#include <shibsp/SPConfig.h>
+#include <shibsp/ServiceProvider.h>
+#include <shibsp/SessionCache.h>
+#include <shibsp/attribute/Attribute.h>
-// SAML Runtime
-#include <saml/saml.h>
-#include <shib/shib.h>
-#include <shib-target/shib-target.h>
#include <xercesc/util/regx/RegularExpression.hpp>
+#include <xmltooling/XMLToolingConfig.h>
#include <xmltooling/util/NDC.h>
#include <xmltooling/util/Threads.h>
+#include <xmltooling/util/XMLConstants.h>
+#include <xmltooling/util/XMLHelper.h>
#ifdef WIN32
# include <winsock.h>
using namespace shibsp;
using namespace xmltooling;
using namespace std;
+using xercesc::RegularExpression;
+using xercesc::XMLException;
extern "C" module MODULE_VAR_EXPORT mod_shib;
namespace {
- char* g_szSHIBConfig = NULL;
- char* g_szSchemaDir = NULL;
- shibtarget::ShibTargetConfig* g_Config = NULL;
+ char* g_szSHIBConfig = SHIBSP_CONFIG;
+ char* g_szSchemaDir = SHIBSP_SCHEMAS;
+ SPConfig* g_Config = NULL;
string g_unsetHeaderValue;
static const char* g_UserDataKey = "_shib_check_user_";
+ static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
+ static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
}
/* Apache 2.2.x headers must be accumulated and set in the output filter.
{
mutable string m_body;
mutable bool m_gotBody;
- vector<XSECCryptoX509*> m_certs;
+ mutable vector<string> m_certs;
public:
request_rec* m_req;
}
const char* getQueryString() const { return m_req->args; }
const char* getRequestBody() const {
- if (m_gotBody)
+ if (m_gotBody || m_req->method_number==M_GET)
return m_body.c_str();
// Read the posted data
- if (ap_setup_client_block(m_req, REQUEST_CHUNKED_ERROR))
- throw opensaml::BindingException("Apache function (setup_client_block) failed while reading POST request body.");
- if (!ap_should_client_block(m_req))
- throw opensaml::BindingException("Apache function (should_client_block) failed while reading POST request body.");
+ if (ap_setup_client_block(m_req, REQUEST_CHUNKED_DECHUNK) != OK) {
+ m_gotBody=true;
+ log(SPError, "Apache function (setup_client_block) failed while reading request body.");
+ return m_body.c_str();
+ }
+ if (!ap_should_client_block(m_req)) {
+ m_gotBody=true;
+ log(SPError, "Apache function (should_client_block) failed while reading request body.");
+ return m_body.c_str();
+ }
if (m_req->remaining > 1024*1024)
- throw opensaml::BindingException("Blocked POST request body larger than size limit.");
+ throw opensaml::SecurityPolicyException("Blocked request body larger than 1M size limit.");
m_gotBody=true;
+ int len;
char buff[HUGE_STRING_LEN];
ap_hard_timeout("[mod_shib] getRequestBody", m_req);
- memset(buff, 0, sizeof(buff));
- while (ap_get_client_block(m_req, buff, sizeof(buff)-1) > 0) {
+ while ((len=ap_get_client_block(m_req, buff, sizeof(buff))) > 0) {
ap_reset_timeout(m_req);
- m_body += buff;
- memset(buff, 0, sizeof(buff));
+ m_body.append(buff, len);
}
ap_kill_timeout(m_req);
return m_body.c_str();
in.read(buf,1024);
ap_rwrite(buf,in.gcount(),m_req);
}
- return ((status==SAML_HTTP_STATUS_OK) ? DONE : status);
+ if (status!=XMLTOOLING_HTTP_STATUS_OK)
+ m_req->status = status;
+ return DONE;
}
long sendRedirect(const char* url) {
ap_table_set(m_req->headers_out, "Location", url);
return REDIRECT;
}
- const vector<XSECCryptoX509*>& getClientCertificates() const {
+ const vector<string>& getClientCertificates() const {
+ if (m_certs.empty()) {
+ const char* cert = ap_table_get(m_req->subprocess_env, "SSL_CLIENT_CERT");
+ if (cert)
+ m_certs.push_back(cert);
+ int i = 0;
+ do {
+ cert = ap_table_get(m_req->subprocess_env, ap_psprintf(m_req->pool, "SSL_CLIENT_CERT_CHAIN_%d", i++));
+ if (cert)
+ m_certs.push_back(cert);
+ } while (cert);
+ }
return m_certs;
}
long returnDecline(void) { return DECLINED; }
Lockable* lock() {return this;}
void unlock() {}
bool authorized(const SPRequest& request, const Session* session) const;
+private:
+ bool checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const;
};
-AccessControl* htAccessFactory(const DOMElement* const & e)
+AccessControl* htAccessFactory(const xercesc::DOMElement* const & e)
{
return new htAccessControl();
}
class ApacheRequestMapper : public virtual RequestMapper, public virtual PropertySet
{
public:
- ApacheRequestMapper(const DOMElement* e);
+ ApacheRequestMapper(const xercesc::DOMElement* e);
~ApacheRequestMapper() { delete m_mapper; delete m_htaccess; delete m_staKey; delete m_propsKey; }
Lockable* lock() { return m_mapper->lock(); }
void unlock() { m_staKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
Settings getSettings(const SPRequest& request) const;
+ void setParent(const PropertySet*) {}
pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
- const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
- const DOMElement* getElement() const;
+ const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:2.0:native:sp:config") const;
+ const xercesc::DOMElement* getElement() const;
private:
RequestMapper* m_mapper;
AccessControl* m_htaccess;
};
-RequestMapper* ApacheRequestMapFactory(const DOMElement* const & e)
+RequestMapper* ApacheRequestMapFactory(const xercesc::DOMElement* const & e)
{
return new ApacheRequestMapper(e);
}
-ApacheRequestMapper::ApacheRequestMapper(const DOMElement* e) : m_mapper(NULL), m_staKey(NULL), m_propsKey(NULL), m_htaccess(NULL)
+ApacheRequestMapper::ApacheRequestMapper(const xercesc::DOMElement* e) : m_mapper(NULL), m_staKey(NULL), m_propsKey(NULL), m_htaccess(NULL)
{
m_mapper=SPConfig::getConfig().RequestMapperManager.newPlugin(XML_REQUEST_MAPPER,e);
m_htaccess=new htAccessControl();
return s ? s->getPropertySet(name,ns) : NULL;
}
-const DOMElement* ApacheRequestMapper::getElement() const
+const xercesc::DOMElement* ApacheRequestMapper::getElement() const
{
const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
return s ? s->getElement() : NULL;
return grps;
}
+bool htAccessControl::checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const
+{
+ bool caseSensitive = attr->isCaseSensitive();
+ const vector<string>& vals = attr->getSerializedValues();
+ for (vector<string>::const_iterator v=vals.begin(); v!=vals.end(); ++v) {
+ if (re) {
+ auto_ptr<XMLCh> trans(fromUTF8(v->c_str()));
+ if (re->matches(trans.get())) {
+ request.log(SPRequest::SPDebug,
+ string("htAccessControl plugin expecting regexp ") + toMatch + ", got " + *v + ": authorization granted"
+ );
+ return true;
+ }
+ }
+ else if ((caseSensitive && *v == toMatch) || (!caseSensitive && !strcasecmp(v->c_str(), toMatch))) {
+ request.log(SPRequest::SPDebug,
+ string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization granted."
+ );
+ return true;
+ }
+ else {
+ request.log(SPRequest::SPDebug,
+ string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization not granted."
+ );
+ }
+ }
+ return false;
+}
+
bool htAccessControl::authorized(const SPRequest& request, const Session* session) const
{
// Make sure the object is our type.
grpstatus=groups_for_user(sta->m_req,remote_user.c_str(),sta->m_dc->szAuthGrpFile);
}
if (!grpstatus)
- return false;
+ continue;
while (*t) {
w=ap_getword_conf(sta->m_req->pool,&t);
}
}
else {
- saml::Iterator<shibboleth::IAAP*> provs=dynamic_cast<const shibtarget::IApplication&>(request.getApplication()).getAAPProviders();
- shibboleth::AAP wrapper(provs,w);
- if (wrapper.fail()) {
- request.log(SPRequest::SPWarn, string("htAccessControl plugin didn't recognize require rule: ") + w);
+ // Map alias in rule to the attribute.
+ if (!session) {
+ request.log(SPRequest::SPError, "htAccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
+ continue;
+ }
+
+ // Find the attribute(s) matching the require rule.
+ pair<multimap<string,Attribute*>::const_iterator,multimap<string,Attribute*>::const_iterator> attrs =
+ session->getAttributes().equal_range(w);
+ if (attrs.first == attrs.second) {
+ request.log(SPRequest::SPWarn, string("htAccessControl rule requires attribute (") + w + "), not found in session");
continue;
}
bool regexp=false;
- const char* vals;
- if (!strcmp(wrapper->getHeader(),"REMOTE_USER"))
- vals=remote_user.c_str();
- else
- if (sta->m_dc->bUseEnvVars!=0) {
- if (sta->m_rc && sta->m_rc->env) vals=ap_table_get(sta->m_rc->env,wrapper->getHeader());
- else vals = NULL;
- } else {
- vals=ap_table_get(sta->m_req->headers_in,wrapper->getHeader());
- }
- while (*t && vals && *vals) {
+ while (!auth_OK[x] && *t) {
w=ap_getword_conf(sta->m_req->pool,&t);
if (*w=='~') {
regexp=true;
if (regexp) {
delete re.release();
auto_ptr<XMLCh> trans(fromUTF8(w));
- auto_ptr<RegularExpression> temp(new RegularExpression(trans.get()));
+ auto_ptr<xercesc::RegularExpression> temp(new xercesc::RegularExpression(trans.get()));
re=temp;
}
- string vals_str(vals);
- int j = 0;
- for (unsigned int i = 0; i < vals_str.length(); i++) {
- if (vals_str.at(i) == ';') {
- if (i == 0) {
- request.log(SPRequest::SPError, string("htAccessControl plugin found invalid header encoding (") +
- vals + "): starts with a semicolon");
- throw saml::SAMLException("Invalid information supplied to authorization plugin.");
- }
-
- if (vals_str.at(i-1) == '\\') {
- vals_str.erase(i-1, 1);
- i--;
- continue;
- }
-
- string val = vals_str.substr(j, i-j);
- j = i+1;
- if (regexp) {
- auto_ptr<XMLCh> trans(fromUTF8(val.c_str()));
- if (re->matches(trans.get())) {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authorization granted");
- SHIB_AP_CHECK_IS_OK;
- }
- }
- else if ((wrapper->getCaseSensitive() && val==w) || (!wrapper->getCaseSensitive() && !strcasecmp(val.c_str(),w))) {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authorization granted.");
- SHIB_AP_CHECK_IS_OK;
- }
- else {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authoritzation not granted.");
- }
- }
- }
-
- string val = vals_str.substr(j, vals_str.length()-j);
- if (regexp) {
- auto_ptr<XMLCh> trans(fromUTF8(val.c_str()));
- if (re->matches(trans.get())) {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authorization granted.");
+ for (; !auth_OK[x] && attrs.first!=attrs.second; ++attrs.first) {
+ if (checkAttribute(request, attrs.first->second, w, regexp ? re.get() : NULL)) {
SHIB_AP_CHECK_IS_OK;
}
}
- else if ((wrapper->getCaseSensitive() && val==w) || (!wrapper->getCaseSensitive() && !strcasecmp(val.c_str(),w))) {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authorization granted");
- SHIB_AP_CHECK_IS_OK;
- }
- else {
- request.log(SPRequest::SPDebug, string("htAccessControl plugin expecting ") + w +
- ", got " + val + ": authorization not granted");
- }
}
catch (XMLException& ex) {
auto_ptr_char tmp(ex.getMessage());
- request.log(SPRequest::SPError, string("htAccessControl plugin caught exception while parsing regular expression (")
- + w + "): " + tmp.get());
+ request.log(SPRequest::SPError,
+ string("htAccessControl plugin caught exception while parsing regular expression (") + w + "): " + tmp.get()
+ );
}
}
}
extern "C" apr_status_t shib_exit(void* data)
{
if (g_Config) {
- g_Config->shutdown();
+ g_Config->term();
g_Config = NULL;
}
ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
#endif
ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit(%d) dealing with g_Config..", (int)getpid());
- g_Config->shutdown();
+ g_Config->term();
g_Config = NULL;
ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit() done");
exit(1);
}
+ g_Config=&SPConfig::getConfig();
+ g_Config->setFeatures(
+ SPConfig::Listener |
+ SPConfig::Caching |
+ SPConfig::RequestMapping |
+ SPConfig::InProcess |
+ SPConfig::Logging
+ );
+ if (!g_Config->init(g_szSchemaDir)) {
+ ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize libraries");
+ exit(1);
+ }
+ g_Config->AccessControlManager.registerFactory(HT_ACCESS_CONTROL,&htAccessFactory);
+ g_Config->RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&ApacheRequestMapFactory);
+
try {
- g_Config=&shibtarget::ShibTargetConfig::getConfig();
- SPConfig::getConfig().setFeatures(
- SPConfig::Caching |
- SPConfig::Listener |
- SPConfig::Metadata |
- SPConfig::AAP |
- SPConfig::RequestMapping |
- SPConfig::InProcess |
- SPConfig::Logging
- );
- if (!g_Config->init(g_szSchemaDir)) {
- ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize libraries");
- exit(1);
- }
- SPConfig::getConfig().AccessControlManager.registerFactory(HT_ACCESS_CONTROL,&htAccessFactory);
- SPConfig::getConfig().RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&ApacheRequestMapFactory);
-
- if (!g_Config->load(g_szSHIBConfig)) {
- ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to load configuration");
- exit(1);
- }
-
- ServiceProvider* conf=SPConfig::getConfig().getServiceProvider();
- xmltooling::Locker locker(conf);
- const PropertySet* props=conf->getPropertySet("Local");
- if (props) {
- pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
- if (unsetValue.first)
- g_unsetHeaderValue = unsetValue.second;
- }
+ xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
+ XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
+ xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
+ auto_ptr_XMLCh src(g_szSHIBConfig);
+ dummy->setAttributeNS(NULL,path,src.get());
+ dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
+
+ g_Config->setServiceProvider(g_Config->ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
+ g_Config->getServiceProvider()->init();
}
- catch (...) {
- ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize system");
+ catch (exception& ex) {
+ ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),ex.what());
+ ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to load configuration");
exit(1);
}
+ ServiceProvider* sp=g_Config->getServiceProvider();
+ xmltooling::Locker locker(sp);
+ const PropertySet* props=sp->getPropertySet("Local");
+ if (props) {
+ pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
+ if (unsetValue.first)
+ g_unsetHeaderValue = unsetValue.second;
+ }
+
// Set the cleanup handler
apr_pool_cleanup_register(p, NULL, &shib_exit, &shib_child_exit);
ap_add_output_filter("SHIB_HEADERS_ERR", NULL, r, r->connection);
}
+static int _table_add(void *v, const char *key, const char *value)
+{
+ apr_table_addn((apr_table_t*)v, key, value);
+ return 1;
+}
+
static apr_status_t do_output_filter(ap_filter_t *f, apr_bucket_brigade *in)
{
request_rec *r = f->r;
if (rc) {
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_out_filter: merging %d headers", apr_table_elts(rc->hdr_out)->nelts);
- apr_table_overlap(r->headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
+ apr_table_do(_table_add,r->headers_out, rc->hdr_out,NULL);
+ // can't use overlap call because it will collapse Set-Cookie headers
+ //apr_table_overlap(r->headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
}
/* remove ourselves from the filter chain */
if (rc) {
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_err_filter: merging %d headers", apr_table_elts(rc->hdr_err)->nelts);
- apr_table_overlap(r->err_headers_out, rc->hdr_err, APR_OVERLAP_TABLES_MERGE);
+ apr_table_do(_table_add,r->err_headers_out, rc->hdr_err,NULL);
+ // can't use overlap call because it will collapse Set-Cookie headers
+ //apr_table_overlap(r->err_headers_out, rc->hdr_err, APR_OVERLAP_TABLES_MERGE);
}
/* remove ourselves from the filter chain */
static command_rec shire_cmds[] = {
{"ShibConfig", (config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
RSRC_CONF, TAKE1, "Path to shibboleth.xml config file"},
+ {"ShibCatalogs", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
+ RSRC_CONF, TAKE1, "Paths of XML schema catalogs"},
{"ShibSchemaDir", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
- RSRC_CONF, TAKE1, "Path to Shibboleth XML schema directory"},
+ RSRC_CONF, TAKE1, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"},
{"ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
(void *) XtOffsetOf (shib_server_config, szScheme),
AP_INIT_TAKE1("ShibConfig",
(config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
RSRC_CONF, "Path to shibboleth.xml config file"),
+ AP_INIT_TAKE1("ShibCatalogs",
+ (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
+ RSRC_CONF, "Paths of XML schema catalogs"),
AP_INIT_TAKE1("ShibSchemaDir",
(config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
- RSRC_CONF, "Path to Shibboleth XML schema directory"),
+ RSRC_CONF, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"),
AP_INIT_TAKE1("ShibURLScheme",
(config_fn_t)shib_set_server_string_slot,