/*
- * Copyright 2001-2005 Internet2
- *
+ * Copyright 2001-2009 Internet2
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
#define CORE_PRIVATE
#include <http_core.h>
#include <http_log.h>
+#include <http_request.h>
#ifndef SHIB_APACHE_13
-#include <http_request.h>
#include <apr_strings.h>
#include <apr_pools.h>
#endif
#include <unistd.h> // for getpid()
#endif
-using namespace std;
-using namespace saml;
-using namespace shibboleth;
using namespace shibtarget;
+using namespace shibboleth;
+using namespace saml;
+using namespace std;
extern "C" module MODULE_VAR_EXPORT mod_shib;
char* g_szSHIBConfig = NULL;
char* g_szSchemaDir = NULL;
ShibTargetConfig* g_Config = NULL;
- string g_unsetHeaderValue;
+ string g_unsetHeaderValue,g_spoofKey;
+ set<string> g_allowedSchemes;
+ bool g_checkSpoofing = true;
+ bool g_catchAll = true;
static const char* g_UserDataKey = "_shib_check_user_";
}
int bBasicHijack; // activate for AuthType Basic?
int bRequireSession; // require a session?
int bExportAssertion; // export SAML assertion to the environment?
- int bUseEnvVars; // use environment instead of headers?
+ int bUseEnvVars; // use environment variables?
+ int bUseHeaders; // use HTTP headers?
};
// creates per-directory config structure
dc->szApplicationId = NULL;
dc->szRequireWith = NULL;
dc->bUseEnvVars = -1;
+ dc->bUseHeaders = -1;
return dc;
}
dc->bExportAssertion=((child->bExportAssertion==-1) ? parent->bExportAssertion : child->bExportAssertion);
dc->bRequireAll=((child->bRequireAll==-1) ? parent->bRequireAll : child->bRequireAll);
dc->bUseEnvVars=((child->bUseEnvVars==-1) ? parent->bUseEnvVars : child->bUseEnvVars);
+ dc->bUseHeaders=((child->bUseHeaders==-1) ? parent->bUseHeaders : child->bUseHeaders);
return dc;
}
// per-request module structure
struct shib_request_config
{
- SH_AP_TABLE *env; // environment vars
+ SH_AP_TABLE *env; // environment vars
#ifdef SHIB_DEFERRED_HEADERS
SH_AP_TABLE *hdr_out; // headers to browser
- SH_AP_TABLE *hdr_err; // err headers to browser
#endif
};
shib_request_config* rc=(shib_request_config*)ap_pcalloc(r->pool,sizeof(shib_request_config));
ap_set_module_config (r->request_config, &mod_shib, rc);
memset(rc, 0, sizeof(shib_request_config));
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_init_rc\n");
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_init_rc\n");
return rc;
}
class ShibTargetApache : public ShibTarget
{
public:
- ShibTargetApache(request_rec* req) {
+ ShibTargetApache(request_rec* req, bool handler, bool shib_check_user) : m_handler(handler), m_firsttime(true) {
m_sc = (shib_server_config*)ap_get_module_config(req->server->module_config, &mod_shib);
m_dc = (shib_dir_config*)ap_get_module_config(req->per_dir_config, &mod_shib);
m_rc = (shib_request_config*)ap_get_module_config(req->request_config, &mod_shib);
+ m_req = req;
+
+ if (shib_check_user && m_dc->bUseHeaders != 0) {
+ // Try and see if this request was already processed, to skip spoof checking.
+ if (!ap_is_initial_req(m_req)) {
+ m_firsttime = false;
+ }
+ else if (!g_spoofKey.empty()) {
+ const char* hdr = ap_table_get(m_req->headers_in, "Shib-Spoof-Check");
+ if (hdr && g_spoofKey == hdr)
+ m_firsttime=false;
+ }
+
+ if (!m_firsttime)
+ log(LogLevelDebug, "shib_check_user running more than once");
+ }
init(
m_sc->szScheme ? m_sc->szScheme : ap_http_method(req),
req->connection->remote_ip,
req->method
);
-
- m_req = req;
}
~ShibTargetApache() { }
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_setheader: no_m_rc");
m_rc = init_request_config(m_req);
}
- ap_table_addn(m_rc->hdr_err, "Set-Cookie", val);
- ap_table_addn(m_rc->hdr_out, "Set-Cookie", val);
-#else
- ap_table_addn(m_req->err_headers_out, "Set-Cookie", val);
+ if (m_handler) {
+ if (!m_rc->hdr_out)
+ m_rc->hdr_out = ap_make_table(m_req->pool, 5);
+ ap_table_addn(m_rc->hdr_out, "Set-Cookie", val);
+ }
+ else
#endif
+ ap_table_addn(m_req->err_headers_out, "Set-Cookie", val);
}
virtual string getArgs(void) { return string(m_req->args ? m_req->args : ""); }
virtual string getPostData(void) {
// Read the posted data
- if (ap_setup_client_block(m_req, REQUEST_CHUNKED_ERROR))
+#ifdef SHIB_APACHE_13
+ if (ap_setup_client_block(m_req, REQUEST_CHUNKED_DECHUNK))
throw FatalProfileException("Apache function (setup_client_block) failed while reading profile submission.");
if (!ap_should_client_block(m_req))
throw FatalProfileException("Apache function (should_client_block) failed while reading profile submission.");
if (m_req->remaining > 1024*1024)
throw FatalProfileException("Blocked too-large a submission to profile endpoint.");
+ int len;
string cgistr;
char buff[HUGE_STRING_LEN];
ap_hard_timeout("[mod_shib] getPostData", 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);
- cgistr += buff;
- memset(buff, 0, sizeof(buff));
+ cgistr.append(buff, len);
}
ap_kill_timeout(m_req);
+ return cgistr;
+#else
+ string cgistr;
+ const char *data;
+ apr_size_t len;
+ int seen_eos = 0;
+ apr_bucket_brigade* bb = apr_brigade_create(m_req->pool, m_req->connection->bucket_alloc);
+ do {
+ apr_bucket *bucket;
+ apr_status_t rv = ap_get_brigade(m_req->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN);
+ if (rv != APR_SUCCESS)
+ throw FatalProfileException("Apache function (ap_get_brigade) failed while reading profile submission.");
+
+ for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb); bucket = APR_BUCKET_NEXT(bucket)) {
+ if (APR_BUCKET_IS_EOS(bucket)) {
+ seen_eos = 1;
+ break;
+ }
+
+ /* We can't do much with this. */
+ if (APR_BUCKET_IS_FLUSH(bucket))
+ continue;
+ /* read */
+ apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
+ if (len > 0)
+ cgistr.append(data, len);
+ }
+ apr_brigade_cleanup(bb);
+ } while (!seen_eos);
+ apr_brigade_destroy(bb);
return cgistr;
+#endif
}
virtual void clearHeader(const string &name) {
- if (m_dc->bUseEnvVars==1) {
- // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: env");
- if (m_rc && m_rc->env) ap_table_unset(m_rc->env, name.c_str());
- } else {
- // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: hdr");
- ap_table_unset(m_req->headers_in, name.c_str());
- ap_table_set(m_req->headers_in, name.c_str(), g_unsetHeaderValue.c_str());
+ if (m_dc->bUseEnvVars == 1) {
+ // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: env");
+ if (m_rc && m_rc->env) ap_table_unset(m_rc->env, name.c_str());
+ }
+ if (m_dc->bUseHeaders != 0) {
+ // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: hdr");
+ if (g_checkSpoofing && m_firsttime) {
+ if (m_allhttp.empty()) {
+ // First time, so populate set with "CGI" versions of client-supplied headers.
+#ifdef SHIB_APACHE_13
+ array_header *hdrs_arr = ap_table_elts(m_req->headers_in);
+ table_entry *hdrs = (table_entry *) hdrs_arr->elts;
+#else
+ const apr_array_header_t *hdrs_arr = apr_table_elts(m_req->headers_in);
+ const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
+#endif
+ for (int i = 0; i < hdrs_arr->nelts; ++i) {
+ if (!hdrs[i].key)
+ continue;
+ string cgiversion("HTTP_");
+ const char* pch = hdrs[i].key;
+ while (*pch) {
+ cgiversion += (isalnum(*pch) ? toupper(*pch) : '_');
+ pch++;
+ }
+ m_allhttp.insert(cgiversion);
+ }
+ }
+
+ // Map to the expected CGI variable name.
+ string transformed("HTTP_");
+ const char* pch = name.c_str();
+ while (*pch) {
+ transformed += (isalnum(*pch) ? toupper(*pch) : '_');
+ pch++;
+ }
+ if (m_allhttp.count(transformed) > 0)
+ throw SAMLException("Attempt to spoof header ($1) was detected.", params(1, name.c_str()));
+ }
+
+ ap_table_unset(m_req->headers_in, name.c_str());
+ ap_table_set(m_req->headers_in, name.c_str(), g_unsetHeaderValue.c_str());
}
}
virtual void setHeader(const string &name, const string &value) {
- if (m_dc->bUseEnvVars==1) {
+ if (m_dc->bUseEnvVars == 1) {
if (!m_rc) {
// this happens on subrequests
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_setheader: no_m_rc");
m_rc = init_request_config(m_req);
}
- if (!m_rc->env) m_rc->env = ap_make_table(m_req->pool, 10);
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_env: %s=%s", name.c_str(), value.c_str()?value.c_str():"Null");
- ap_table_set(m_rc->env, name.c_str(), value.c_str()?value.c_str():"");
- } else {
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_hdr: %s=%s", name.c_str(), value.c_str()?value.c_str():"Null");
- ap_table_set(m_req->headers_in, name.c_str(), value.c_str());
+ if (!m_rc->env)
+ m_rc->env = ap_make_table(m_req->pool, 10);
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_env: %s=%s", name.c_str(), value.c_str()?value.c_str():"Null");
+ ap_table_set(m_rc->env, name.c_str(), value.c_str() ? value.c_str() : "");
+ }
+ if (m_dc->bUseHeaders != 0) {
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_hdr: %s=%s", name.c_str(), value.c_str()?value.c_str():"Null");
+ ap_table_set(m_req->headers_in, name.c_str(), value.c_str() ? value.c_str() : "");
}
}
virtual string getHeader(const string &name) {
const char *hdr;
- if (m_dc->bUseEnvVars==1) {
- if (m_rc && m_rc->env) hdr = ap_table_get(m_rc->env, name.c_str());
- else hdr = NULL;
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_get_hdr_env: %s=%s", name.c_str(), hdr?hdr:"NULL");
- } else {
+ if (m_dc->bUseEnvVars == 1) {
+ if (m_rc && m_rc->env)
+ hdr = ap_table_get(m_rc->env, name.c_str());
+ else
+ hdr = NULL;
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_get_hdr_env: %s=%s", name.c_str(), hdr?hdr:"NULL");
+ }
+ else {
hdr = ap_table_get(m_req->headers_in, name.c_str());
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_get_hdr: %s=%s", name.c_str(), hdr?hdr:"NULL");
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_get_hdr: %s=%s", name.c_str(), hdr?hdr:"NULL");
}
return string(hdr ? hdr : "");
}
const string& content_type="text/html",
const Iterator<header_t>& headers=EMPTY(header_t)
) {
+ checkString(content_type, "Detected control character in a response header.");
m_req->content_type = ap_psprintf(m_req->pool, content_type.c_str());
while (headers.hasNext()) {
const header_t& h=headers.next();
+ checkString(h.first, "Detected control character in a response header.");
+ checkString(h.second, "Detected control character in a response header.");
ap_table_set(m_req->headers_out, h.first.c_str(), h.second.c_str());
}
ap_send_http_header(m_req);
return (void*)((code==200) ? DONE : code);
}
virtual void* sendRedirect(const string& url) {
+ checkString(url, "Detected control character in an attempted redirect.");
+ if (g_allowedSchemes.find(url.substr(0, url.find(':'))) == g_allowedSchemes.end())
+ throw FatalProfileException("Invalid scheme in attempted redirect.");
ap_table_set(m_req->headers_out, "Location", url.c_str());
return (void*)REDIRECT;
}
virtual void* returnDecline(void) { return (void*)DECLINED; }
virtual void* returnOK(void) { return (void*)OK; }
+ bool m_handler,m_firsttime;;
request_rec* m_req;
shib_dir_config* m_dc;
shib_server_config* m_sc;
shib_request_config* m_rc;
+ set<string> m_allhttp;
+
+private:
+ void checkString(const string& s, const char* msg) {
+ string::const_iterator e = s.end();
+ for (string::const_iterator i=s.begin(); i!=e; ++i) {
+ if (iscntrl(*i))
+ throw FatalProfileException(msg);
+ }
+ }
};
/********************************************************************************/
extern "C" int shib_check_user(request_rec* r)
{
- // Short-circuit entirely?
- if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
- return DECLINED;
-
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_check_user(%d): ENTER", (int)getpid());
-
- ostringstream threadid;
- threadid << "[" << getpid() << "] shib_check_user" << '\0';
- saml::NDC ndc(threadid.str().c_str());
-
- try {
- ShibTargetApache sta(r);
+ // Short-circuit entirely?
+ if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
+ return DECLINED;
- // Check user authentication and export information, then set the handler bypass
- pair<bool,void*> res = sta.doCheckAuthN(true);
- apr_pool_userdata_setn((const void*)42,g_UserDataKey,NULL,r->pool);
- if (res.first) return (int)(long)res.second;
+ ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_check_user(%d): ENTER", (int)getpid());
- // user auth was okay -- export the assertions now
- res = sta.doExportAssertions();
- if (res.first) return (int)(long)res.second;
+ ostringstream threadid;
+ threadid << "[" << getpid() << "] shib_check_user" << '\0';
+ saml::NDC ndc(threadid.str().c_str());
- // export happened successfully.. this user is ok.
- return OK;
- }
- catch (SAMLException& e) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an exception: %s", e.what());
- return SERVER_ERROR;
- }
-#ifndef _DEBUG
- catch (...) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an uncaught exception!");
- return SERVER_ERROR;
- }
-#endif
+ try {
+ ShibTargetApache sta(r, false, true);
+
+ // Check user authentication and export information, then set the handler bypass
+ pair<bool,void*> res = sta.doCheckAuthN(true);
+ apr_pool_userdata_setn((const void*)42,g_UserDataKey,NULL,r->pool);
+ // If directed, install a spoof key to recognize when we've already cleared headers.
+ if (!g_spoofKey.empty() && (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bUseHeaders!=0))
+ ap_table_set(r->headers_in, "Shib-Spoof-Check", g_spoofKey.c_str());
+ if (res.first) return (int)(long)res.second;
+
+ // user auth was okay -- export the assertions now
+ res = sta.doExportAssertions();
+ if (res.first) return (int)(long)res.second;
+
+ // export happened successfully.. this user is ok.
+ return OK;
+ }
+ catch (exception& e) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an exception: %s", e.what());
+ return SERVER_ERROR;
+ }
+ catch (...) {
+ if (g_catchAll) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an uncaught exception!");
+ return SERVER_ERROR;
+ }
+ throw;
+ }
}
extern "C" int shib_handler(request_rec* r)
{
- // Short-circuit entirely?
- if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
- return DECLINED;
+ // Short-circuit entirely?
+ if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
+ return DECLINED;
- ostringstream threadid;
- threadid << "[" << getpid() << "] shib_handler" << '\0';
- saml::NDC ndc(threadid.str().c_str());
+ ostringstream threadid;
+ threadid << "[" << getpid() << "] shib_handler" << '\0';
+ saml::NDC ndc(threadid.str().c_str());
#ifndef SHIB_APACHE_13
- // With 2.x, this handler always runs, though last.
- // We check if shib_check_user ran, because it will detect a handler request
- // and dispatch it directly.
- void* data;
- apr_pool_userdata_get(&data,g_UserDataKey,r->pool);
- if (data==(const void*)42) {
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler skipped since check_user ran");
- return DECLINED;
- }
+ // With 2.x, this handler always runs, though last.
+ // We check if shib_check_user ran, because it will detect a handler request
+ // and dispatch it directly.
+ void* data;
+ apr_pool_userdata_get(&data,g_UserDataKey,r->pool);
+ if (data==(const void*)42) {
+ ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler skipped since check_user ran");
+ return DECLINED;
+ }
#endif
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler(%d): ENTER: %s", (int)getpid(), r->handler);
+ ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler(%d): ENTER: %s", (int)getpid(), r->handler);
- try {
- ShibTargetApache sta(r);
+ try {
+ ShibTargetApache sta(r, true, false);
- pair<bool,void*> res = sta.doHandler();
- if (res.first) return (int)(long)res.second;
+ pair<bool,void*> res = sta.doHandler();
+ if (res.first) return (int)(long)res.second;
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "doHandler() did not do anything.");
- return SERVER_ERROR;
- }
- catch (SAMLException& e) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an exception: %s", e.what());
- return SERVER_ERROR;
- }
-#ifndef _DEBUG
- catch (...) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an uncaught exception!");
- return SERVER_ERROR;
- }
-#endif
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "doHandler() did not do anything.");
+ return SERVER_ERROR;
+ }
+ catch (exception& e) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an exception: %s", e.what());
+ return SERVER_ERROR;
+ }
+ catch (...) {
+ if (g_catchAll) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an uncaught exception!");
+ return SERVER_ERROR;
+ }
+ throw;
+ }
}
/*
*/
extern "C" int shib_auth_checker(request_rec* r)
{
- // Short-circuit entirely?
- if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
- return DECLINED;
+ // Short-circuit entirely?
+ if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
+ return DECLINED;
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_auth_checker(%d): ENTER", (int)getpid());
+ ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_auth_checker(%d): ENTER", (int)getpid());
- ostringstream threadid;
- threadid << "[" << getpid() << "] shib_auth_checker" << '\0';
- saml::NDC ndc(threadid.str().c_str());
+ ostringstream threadid;
+ threadid << "[" << getpid() << "] shib_auth_checker" << '\0';
+ saml::NDC ndc(threadid.str().c_str());
- try {
- ShibTargetApache sta(r);
+ try {
+ ShibTargetApache sta(r, false, false);
- pair<bool,void*> res = sta.doCheckAuthZ();
- if (res.first) return (int)(long)res.second;
+ pair<bool,void*> res = sta.doCheckAuthZ();
+ if (res.first) return (int)(long)res.second;
- // We're all okay.
- return OK;
- }
- catch (SAMLException& e) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an exception: %s", e.what());
- return SERVER_ERROR;
- }
-#ifndef _DEBUG
- catch (...) {
- ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an uncaught exception!");
- return SERVER_ERROR;
- }
-#endif
+ // We're all okay.
+ return OK;
+ }
+ catch (exception& e) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an exception: %s", e.what());
+ return SERVER_ERROR;
+ }
+ catch (...) {
+ if (g_catchAll) {
+ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an uncaught exception!");
+ return SERVER_ERROR;
+ }
+ throw;
+ }
}
// Access control plugin that enforces htaccess rules
void lock() { m_mapper->lock(); }
void unlock() { m_staKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
Settings getSettings(ShibTarget* st) const;
-
+
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;
ApacheRequestMapper::ApacheRequestMapper(const DOMElement* e) : m_mapper(NULL), m_staKey(NULL), m_propsKey(NULL), m_htaccess(NULL)
{
- IPlugIn* p=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::XMLRequestMapType,e);
+ IPlugIn* p=saml::SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::XMLRequestMapType,e);
m_mapper=dynamic_cast<IRequestMapper*>(p);
if (!m_mapper) {
delete p;
const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
if (sta && !ns) {
// Override Apache-settable boolean properties.
- if (name && !strcmp(name,"requireSession") && sta->m_dc->bRequireSession==1)
- return make_pair(true,true);
- else if (name && !strcmp(name,"exportAssertion") && sta->m_dc->bExportAssertion==1)
- return make_pair(true,true);
+ if (name && !strcmp(name,"requireSession") && sta->m_dc->bRequireSession != -1)
+ return make_pair(true, sta->m_dc->bRequireSession==1);
+ else if (name && !strcmp(name,"exportAssertion") && sta->m_dc->bExportAssertion != -1)
+ return make_pair(true, sta->m_dc->bExportAssertion==1);
}
return s ? s->getBool(name,ns) : make_pair(false,false);
}
int m=sta->m_req->method_number;
bool method_restricted=false;
const char *t, *w;
-
+
const array_header* reqs_arr=ap_requires(sta->m_req);
if (!reqs_arr)
return true;
require_line* reqs=(require_line*)reqs_arr->elts;
-
+
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE nelts: %d", reqs_arr->nelts);
ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE all: %d", sta->m_dc->bRequireAll);
regexp=true;
continue;
}
-
+
if (regexp) {
try {
// To do regex matching, we have to convert from UTF-8.
}
if (!grpstatus)
return false;
-
+
while (*t) {
w=ap_getword_conf(sta->m_req->pool,&t);
if (ap_table_get(grpstatus,w)) {
auto_ptr<RegularExpression> temp(new RegularExpression(trans.get()));
re=temp;
}
-
+
string vals_str(vals);
unsigned int j = 0;
for (unsigned int i = 0; i < vals_str.length(); i++) {
}
}
}
-
+
string val = vals_str.substr(j, vals_str.length()-j);
if (regexp) {
auto_ptr<XMLCh> trans(fromUTF8(val.c_str()));
return false;
}
-
-#ifndef SHIB_APACHE_13
-/*
- * shib_exit()
- * Empty cleanup hook, Apache 2.x doesn't check NULL very well...
- */
-extern "C" apr_status_t shib_exit(void* data)
-{
- if (g_Config) {
- g_Config->shutdown();
- g_Config = NULL;
- }
- ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
- return OK;
-}
-#endif
-
// Initial look at a request - create the per-request structure
static int shib_post_read(request_rec *r)
{
shib_request_config* rc = init_request_config(r);
-
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_post_read: E=%s", rc->env?"env":"hdr");
-
-#ifdef SHIB_DEFERRED_HEADERS
- rc->hdr_out = ap_make_table(r->pool, 5);
- rc->hdr_err = ap_make_table(r->pool, 5);
-#endif
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_post_read");
return DECLINED;
}
{
shib_request_config *rc = (shib_request_config*)ap_get_module_config(r->request_config, &mod_shib);
shib_dir_config *dc = (shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib);
- if (dc->bOff==1 || dc->bUseEnvVars!=1)
+ if (dc->bOff==1 || dc->bUseEnvVars!=1)
return DECLINED;
-
- ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup(%d): ENTER", (int)getpid());
+
+ //ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup(%d): ENTER", (int)getpid());
if (rc==NULL || rc->env==NULL || ap_is_empty_table(rc->env))
return DECLINED;
return OK;
}
-
+#ifdef SHIB_APACHE_13
/*
* shib_child_exit()
* Cleanup the (per-process) pool info.
*/
-#ifdef SHIB_APACHE_13
extern "C" void shib_child_exit(server_rec* s, SH_AP_POOL* p)
{
+ if (g_Config) {
+ 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 = NULL;
+ ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit() done");
+ }
+}
#else
-extern "C" apr_status_t shib_child_exit(void* data)
+/*
+ * shib_exit()
+ * Apache 2.x doesn't allow for per-child cleanup, causes CGI forks to hang.
+ */
+extern "C" apr_status_t shib_exit(void* data)
{
- server_rec* s = NULL;
-#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 = NULL;
- ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit() done");
-
-#ifndef SHIB_APACHE_13
+ if (g_Config) {
+ g_Config->shutdown();
+ g_Config = NULL;
+ }
+ ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
return OK;
-#endif
}
+#endif
-/*
+/*
* shire_child_init()
* Things to do when the child process is initialized.
* (or after the configs are read in apache-2)
ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize libraries");
exit(1);
}
- SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::htAccessControlType,&htAccessFactory);
- SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&ApacheRequestMapFactory);
+ PlugManager& mgr = SAMLConfig::getConfig().getPlugMgr();
+ mgr.regFactory(shibtarget::XML::htAccessControlType,&htAccessFactory);
+ mgr.regFactory(shibtarget::XML::NativeRequestMapType,&ApacheRequestMapFactory);
// We hijack the legacy type so that 1.2 config files will load this plugin
- SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&ApacheRequestMapFactory);
-
+ mgr.regFactory(shibtarget::XML::LegacyRequestMapType,&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);
}
IConfig* conf=g_Config->getINI();
- Locker locker(conf);
+ saml::Locker locker(conf);
const IPropertySet* props=conf->getPropertySet("Local");
if (props) {
- pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
- if (unsetValue.first)
- g_unsetHeaderValue = unsetValue.second;
+ pair<bool,const char*> str=props->getString("unsetHeaderValue");
+ if (str.first)
+ g_unsetHeaderValue = str.second;
+ str=props->getString("allowedSchemes");
+ if (str.first) {
+ string schemes=str.second;
+ unsigned int j=0;
+ for (unsigned int i=0; i < schemes.length(); i++) {
+ if (schemes.at(i)==' ') {
+ g_allowedSchemes.insert(schemes.substr(j, i-j));
+ j = i+1;
+ }
+ }
+ g_allowedSchemes.insert(schemes.substr(j, schemes.length()-j));
+ }
+ pair<bool,bool> flag=props->getBool("checkSpoofing");
+ g_checkSpoofing = !flag.first || flag.second;
+ flag=props->getBool("catchAll");
+ g_catchAll = !flag.first || flag.second;
+ }
+ if (g_allowedSchemes.empty()) {
+ g_allowedSchemes.insert("https");
+ g_allowedSchemes.insert("http");
}
}
- catch (...) {
+ catch (exception&) {
ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize system");
exit(1);
}
// Set the cleanup handler
- apr_pool_cleanup_register(p, NULL, &shib_exit, &shib_child_exit);
+ apr_pool_cleanup_register(p, NULL, &shib_exit, apr_pool_cleanup_null);
ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() done");
}
-// Output filters
+// Output filters
#ifdef SHIB_DEFERRED_HEADERS
static void set_output_filter(request_rec *r)
{
- ap_add_output_filter("SHIB_HEADERS_OUT", NULL, r, r->connection);
+ ap_add_output_filter("SHIB_HEADERS_OUT", NULL, r, r->connection);
}
static void set_error_filter(request_rec *r)
{
- ap_add_output_filter("SHIB_HEADERS_ERR", NULL, r, r->connection);
+ 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;
shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
- if (rc) {
+ if (rc && rc->hdr_out) {
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);
+ // 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);
+ apr_table_do(_table_add,r->headers_out, rc->hdr_out,NULL);
}
/* remove ourselves from the filter chain */
request_rec *r = f->r;
shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
- 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);
+ if (rc && rc->hdr_out) {
+ ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_err_filter: merging %d headers", apr_table_elts(rc->hdr_out)->nelts);
+ // 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);
+ apr_table_do(_table_add,r->err_headers_out, rc->hdr_out,NULL);
}
/* remove ourselves from the filter chain */
{"ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
(void *) XtOffsetOf (shib_server_config, szScheme),
RSRC_CONF, TAKE1, "URL scheme to force into generated URLs for a vhost"},
-
+
{"ShibDisable", (config_fn_t)ap_set_flag_slot,
(void *) XtOffsetOf (shib_dir_config, bOff),
OR_AUTHCFG, FLAG, "Disable all Shib module activity here to save processing effort"},
OR_AUTHCFG, FLAG, "All require directives must match"},
{"ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
(void *) XtOffsetOf (shib_dir_config, bUseEnvVars),
- OR_AUTHCFG, FLAG, "Export data in environment instead of headers"},
+ OR_AUTHCFG, FLAG, "Export attributes using environment variables"},
+ {"ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
+ (void *) XtOffsetOf (shib_dir_config, bUseHeaders),
+ OR_AUTHCFG, FLAG, "Export attributes using custom HTTP headers (default)"},
{NULL}
};
OR_AUTHCFG, "All require directives must match"),
AP_INIT_FLAG("ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
(void *) offsetof (shib_dir_config, bUseEnvVars),
- OR_AUTHCFG, "Export data in environment instead of headers"),
+ OR_AUTHCFG, "Export attributes using environment variables"),
+ AP_INIT_FLAG("ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
+ (void *) offsetof (shib_dir_config, bUseHeaders),
+ OR_AUTHCFG, "Export attributes using custom HTTP headers (default)"),
{NULL}
};