* limitations under the License.
*/
-/*
- * mod_apache.cpp -- the core Apache Module code
- *
- * Created by: Derek Atkins <derek@ihtfp.com>
- *
- * $Id$
+/**
+ * mod_apache.cpp
+ *
+ * Apache module implementation
*/
+#define SHIBSP_LITE
+
#ifdef SOLARIS2
#undef _XOPEN_SOURCE // causes gethostname conflict in unistd.h
#endif
#include <shibsp/ServiceProvider.h>
#include <shibsp/SessionCache.h>
#include <shibsp/attribute/Attribute.h>
-
+#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/regx/RegularExpression.hpp>
#include <xmltooling/XMLToolingConfig.h>
#include <xmltooling/util/NDC.h>
#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_buckets.h>
#include <apr_strings.h>
#include <apr_pools.h>
#endif
char* g_szSchemaDir = SHIBSP_SCHEMAS;
SPConfig* g_Config = NULL;
string g_unsetHeaderValue;
+ bool g_checkSpoofing = true;
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);
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
};
class ShibTargetApache : public AbstractSPRequest
{
+ bool m_handler;
mutable string m_body;
mutable bool m_gotBody;
- vector<string> m_certs;
+ mutable vector<string> m_certs;
+ set<string> m_allhttp;
public:
request_rec* m_req;
shib_server_config* m_sc;
shib_request_config* m_rc;
- ShibTargetApache(request_rec* req) : m_gotBody(false) {
+ ShibTargetApache(request_rec* req, bool handler) : m_handler(handler), m_gotBody(false) {
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);
const char* getRequestBody() const {
if (m_gotBody || m_req->method_number==M_GET)
return m_body.c_str();
+#ifdef SHIB_APACHE_13
// Read the posted data
if (ap_setup_client_block(m_req, REQUEST_CHUNKED_DECHUNK) != OK) {
m_gotBody=true;
if (m_req->remaining > 1024*1024)
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);
+#else
+ 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) {
+ log(SPError, "Apache function (ap_get_brigade) failed while reading request body.");
+ break;
+ }
+
+ 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)
+ m_body.append(data, len);
+ }
+ apr_brigade_cleanup(bb);
+ } while (!seen_eos);
+ apr_brigade_destroy(bb);
+ m_gotBody=true;
+#endif
return m_body.c_str();
}
- void clearHeader(const char* name) {
+ void clearHeader(const char* rawname, const char* cginame) {
if (m_dc->bUseEnvVars!=0) {
// ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: env\n");
- if (m_rc && m_rc->env) ap_table_unset(m_rc->env, name);
+ if (m_rc && m_rc->env) ap_table_unset(m_rc->env, rawname);
} else {
// ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: hdr\n");
- ap_table_unset(m_req->headers_in, name);
- ap_table_set(m_req->headers_in, name, g_unsetHeaderValue.c_str());
+ if (g_checkSpoofing && ap_is_initial_req(m_req)) {
+ 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);
+ }
+ }
+
+ if (m_allhttp.count(cginame) > 0)
+ throw opensaml::SecurityPolicyException("Attempt to spoof header ($1) was detected.", params(1, rawname));
+ }
+ ap_table_unset(m_req->headers_in, rawname);
+ ap_table_set(m_req->headers_in, rawname, g_unsetHeaderValue.c_str());
}
}
void setHeader(const char* name, const char* value) {
if (!m_rc)
// this happens on subrequests
m_rc = init_request_config(m_req);
- ap_table_add(m_rc->hdr_err, name, value);
- ap_table_add(m_rc->hdr_out, name, value);
-#else
- ap_table_add(m_req->err_headers_out, name, value);
+ if (m_handler)
+ ap_table_add(m_rc->hdr_out, name, value);
+ else
#endif
+ ap_table_add(m_req->err_headers_out, name, value);
}
long sendResponse(istream& in, long status) {
ap_send_http_header(m_req);
in.read(buf,1024);
ap_rwrite(buf,in.gcount(),m_req);
}
- return ((status==XMLTOOLING_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<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; }
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\n", (int)getpid());
+ 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';
xmltooling::NDC ndc(threadid.str().c_str());
try {
- ShibTargetApache sta(r);
+ ShibTargetApache sta(r,false);
// Check user authentication and export information, then set the handler bypass
pair<bool,long> res = sta.getServiceProvider().doAuthentication(sta,true);
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);
+ ShibTargetApache sta(r,true);
pair<bool,long> res = sta.getServiceProvider().doHandler(sta);
if (res.first) return res.second;
xmltooling::NDC ndc(threadid.str().c_str());
try {
- ShibTargetApache sta(r);
+ ShibTargetApache sta(r,false);
pair<bool,long> res = sta.getServiceProvider().doAuthorization(sta);
if (res.first) return res.second;
void unlock() { m_staKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
Settings getSettings(const SPRequest& request) const;
+ const PropertySet* getParent() const { return NULL; }
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;
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->term();
- 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)
#ifdef SHIB_DEFERRED_HEADERS
rc->hdr_out = ap_make_table(r->pool, 5);
- rc->hdr_err = ap_make_table(r->pool, 5);
#endif
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->term();
+ 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->term();
- 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->term();
+ g_Config = NULL;
+ }
+ ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
return OK;
-#endif
}
+#endif
/*
* shire_child_init()
pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
if (unsetValue.first)
g_unsetHeaderValue = unsetValue.second;
+ pair<bool,bool> checkSpoofing=props->getBool("checkSpoofing");
+ if (checkSpoofing.first && !checkSpoofing.second)
+ g_checkSpoofing = false;
}
// 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");
}
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_do(_table_add,r->err_headers_out, rc->hdr_err,NULL);
+ 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);
+ apr_table_do(_table_add,r->err_headers_out, rc->hdr_out,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);
+ //apr_table_overlap(r->err_headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
}
/* remove ourselves from the filter chain */
(void *) offsetof (shib_dir_config, szRedirectToSSL),
OR_AUTHCFG, "Redirect non-SSL requests to designated port"),
AP_INIT_TAKE1("AuthGroupFile", (config_fn_t)shib_ap_set_file_slot,
- (void *) offsetof (shib_dir_config, szAuthGrpFile),
- OR_AUTHCFG, "Text file containing group names and member user IDs"),
+ (void *) offsetof (shib_dir_config, szAuthGrpFile),
+ OR_AUTHCFG, "Text file containing group names and member user IDs"),
AP_INIT_FLAG("ShibRequireAll", (config_fn_t)ap_set_flag_slot,
(void *) offsetof (shib_dir_config, bRequireAll),
OR_AUTHCFG, "All require directives must match"),
};
#else
-#error "undefined APACHE version"
+#error "unsupported Apache version"
#endif
}