2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Apache module implementation
26 #undef _XOPEN_SOURCE // causes gethostname conflict in unistd.h
30 # define _CRT_NONSTDC_NO_DEPRECATE 1
31 # define _CRT_SECURE_NO_DEPRECATE 1
34 #include <shibsp/AbstractSPRequest.h>
35 #include <shibsp/AccessControl.h>
36 #include <shibsp/exceptions.h>
37 #include <shibsp/RequestMapper.h>
38 #include <shibsp/SPConfig.h>
39 #include <shibsp/ServiceProvider.h>
40 #include <shibsp/SessionCache.h>
41 #include <shibsp/attribute/Attribute.h>
42 #include <xercesc/util/XMLUniDefs.hpp>
43 #include <xercesc/util/regx/RegularExpression.hpp>
44 #include <xmltooling/XMLToolingConfig.h>
45 #include <xmltooling/util/NDC.h>
46 #include <xmltooling/util/Threads.h>
47 #include <xmltooling/util/XMLConstants.h>
48 #include <xmltooling/util/XMLHelper.h>
56 // Apache specific header files
58 #include <http_config.h>
59 #include <http_protocol.h>
60 #include <http_main.h>
62 #include <http_core.h>
64 #include <http_request.h>
66 #ifndef SHIB_APACHE_13
67 #include <apr_buckets.h>
68 #include <apr_strings.h>
69 #include <apr_pools.h>
76 #include <unistd.h> // for getpid()
79 using namespace shibsp;
80 using namespace xmltooling;
82 using xercesc::RegularExpression;
83 using xercesc::XMLException;
85 extern "C" module MODULE_VAR_EXPORT mod_shib;
88 char* g_szSHIBConfig = SHIBSP_CONFIG;
89 char* g_szSchemaDir = SHIBSP_SCHEMAS;
90 SPConfig* g_Config = NULL;
91 string g_unsetHeaderValue;
92 bool g_checkSpoofing = true;
93 static const char* g_UserDataKey = "_shib_check_user_";
94 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
95 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
98 /* Apache 2.2.x headers must be accumulated and set in the output filter.
99 Apache 2.0.49+ supports the filter method.
100 Apache 1.3.x and lesser 2.0.x must write the headers directly. */
102 #if (defined(SHIB_APACHE_20) || defined(SHIB_APACHE_22)) && AP_MODULE_MAGIC_AT_LEAST(20020903,6)
103 #define SHIB_DEFERRED_HEADERS
106 /********************************************************************************/
107 // Basic Apache Configuration code.
110 // per-server module configuration structure
111 struct shib_server_config
116 // creates the per-server configuration
117 extern "C" void* create_shib_server_config(SH_AP_POOL* p, server_rec* s)
119 shib_server_config* sc=(shib_server_config*)ap_pcalloc(p,sizeof(shib_server_config));
124 // overrides server configuration in virtual servers
125 extern "C" void* merge_shib_server_config (SH_AP_POOL* p, void* base, void* sub)
127 shib_server_config* sc=(shib_server_config*)ap_pcalloc(p,sizeof(shib_server_config));
128 shib_server_config* parent=(shib_server_config*)base;
129 shib_server_config* child=(shib_server_config*)sub;
132 sc->szScheme=ap_pstrdup(p,child->szScheme);
133 else if (parent->szScheme)
134 sc->szScheme=ap_pstrdup(p,parent->szScheme);
141 // per-dir module configuration structure
142 struct shib_dir_config
144 SH_AP_TABLE* tSettings; // generic table of extensible settings
147 char* szAuthGrpFile; // Auth GroupFile name
148 int bRequireAll; // all require directives must match, otherwise OR logic
150 // Content Configuration
151 char* szApplicationId; // Shib applicationId value
152 char* szRequireWith; // require a session using a specific initiator?
153 char* szRedirectToSSL; // redirect non-SSL requests to SSL port
154 int bOff; // flat-out disable all Shib processing
155 int bBasicHijack; // activate for AuthType Basic?
156 int bRequireSession; // require a session?
157 int bExportAssertion; // export SAML assertion to the environment?
158 int bUseEnvVars; // use environment?
159 int bUseHeaders; // use headers?
162 // creates per-directory config structure
163 extern "C" void* create_shib_dir_config (SH_AP_POOL* p, char* d)
165 shib_dir_config* dc=(shib_dir_config*)ap_pcalloc(p,sizeof(shib_dir_config));
166 dc->tSettings = NULL;
167 dc->szAuthGrpFile = NULL;
168 dc->bRequireAll = -1;
169 dc->szApplicationId = NULL;
170 dc->szRequireWith = NULL;
171 dc->szRedirectToSSL = NULL;
173 dc->bBasicHijack = -1;
174 dc->bRequireSession = -1;
175 dc->bExportAssertion = -1;
176 dc->bUseEnvVars = -1;
177 dc->bUseHeaders = -1;
181 // overrides server configuration in directories
182 extern "C" void* merge_shib_dir_config (SH_AP_POOL* p, void* base, void* sub)
184 shib_dir_config* dc=(shib_dir_config*)ap_pcalloc(p,sizeof(shib_dir_config));
185 shib_dir_config* parent=(shib_dir_config*)base;
186 shib_dir_config* child=(shib_dir_config*)sub;
188 // The child supersedes any matching table settings in the parent.
189 dc->tSettings = NULL;
190 if (parent->tSettings)
191 dc->tSettings = ap_copy_table(p, parent->tSettings);
192 if (child->tSettings) {
194 ap_overlap_tables(dc->tSettings, child->tSettings, AP_OVERLAP_TABLES_SET);
196 dc->tSettings = ap_copy_table(p, child->tSettings);
199 if (child->szAuthGrpFile)
200 dc->szAuthGrpFile=ap_pstrdup(p,child->szAuthGrpFile);
201 else if (parent->szAuthGrpFile)
202 dc->szAuthGrpFile=ap_pstrdup(p,parent->szAuthGrpFile);
204 dc->szAuthGrpFile=NULL;
206 if (child->szApplicationId)
207 dc->szApplicationId=ap_pstrdup(p,child->szApplicationId);
208 else if (parent->szApplicationId)
209 dc->szApplicationId=ap_pstrdup(p,parent->szApplicationId);
211 dc->szApplicationId=NULL;
213 if (child->szRequireWith)
214 dc->szRequireWith=ap_pstrdup(p,child->szRequireWith);
215 else if (parent->szRequireWith)
216 dc->szRequireWith=ap_pstrdup(p,parent->szRequireWith);
218 dc->szRequireWith=NULL;
220 if (child->szRedirectToSSL)
221 dc->szRedirectToSSL=ap_pstrdup(p,child->szRedirectToSSL);
222 else if (parent->szRedirectToSSL)
223 dc->szRedirectToSSL=ap_pstrdup(p,parent->szRedirectToSSL);
225 dc->szRedirectToSSL=NULL;
227 dc->bOff=((child->bOff==-1) ? parent->bOff : child->bOff);
228 dc->bBasicHijack=((child->bBasicHijack==-1) ? parent->bBasicHijack : child->bBasicHijack);
229 dc->bRequireSession=((child->bRequireSession==-1) ? parent->bRequireSession : child->bRequireSession);
230 dc->bExportAssertion=((child->bExportAssertion==-1) ? parent->bExportAssertion : child->bExportAssertion);
231 dc->bRequireAll=((child->bRequireAll==-1) ? parent->bRequireAll : child->bRequireAll);
232 dc->bUseEnvVars=((child->bUseEnvVars==-1) ? parent->bUseEnvVars : child->bUseEnvVars);
233 dc->bUseHeaders=((child->bUseHeaders==-1) ? parent->bUseHeaders : child->bUseHeaders);
237 // per-request module structure
238 struct shib_request_config
240 SH_AP_TABLE *env; // environment vars
241 #ifdef SHIB_DEFERRED_HEADERS
242 SH_AP_TABLE *hdr_out; // headers to browser
246 // create a request record
247 static shib_request_config *init_request_config(request_rec *r)
249 shib_request_config* rc=(shib_request_config*)ap_pcalloc(r->pool,sizeof(shib_request_config));
250 ap_set_module_config (r->request_config, &mod_shib, rc);
251 memset(rc, 0, sizeof(shib_request_config));
252 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_init_rc");
256 // generic global slot handlers
257 extern "C" const char* ap_set_global_string_slot(cmd_parms* parms, void*, const char* arg)
259 *((char**)(parms->info))=ap_pstrdup(parms->pool,arg);
263 extern "C" const char* shib_set_server_string_slot(cmd_parms* parms, void*, const char* arg)
265 char* base=(char*)ap_get_module_config(parms->server->module_config,&mod_shib);
266 size_t offset=(size_t)parms->info;
267 *((char**)(base + offset))=ap_pstrdup(parms->pool,arg);
271 extern "C" const char* shib_ap_set_file_slot(cmd_parms* parms,
272 #ifdef SHIB_APACHE_13
273 char* arg1, char* arg2
275 void* arg1, const char* arg2
279 ap_set_file_slot(parms, arg1, arg2);
283 extern "C" const char* shib_table_set(cmd_parms* parms, shib_dir_config* dc, const char* arg1, const char* arg2)
286 dc->tSettings = ap_make_table(parms->pool, 4);
287 ap_table_set(dc->tSettings, arg1, arg2);
291 /********************************************************************************/
292 // Apache ShibTarget subclass(es) here.
294 class ShibTargetApache : public AbstractSPRequest
297 mutable string m_body;
298 mutable bool m_gotBody;
299 mutable vector<string> m_certs;
300 set<string> m_allhttp;
304 shib_dir_config* m_dc;
305 shib_server_config* m_sc;
306 shib_request_config* m_rc;
308 ShibTargetApache(request_rec* req, bool handler) : m_handler(handler), m_gotBody(false) {
309 m_sc = (shib_server_config*)ap_get_module_config(req->server->module_config, &mod_shib);
310 m_dc = (shib_dir_config*)ap_get_module_config(req->per_dir_config, &mod_shib);
311 m_rc = (shib_request_config*)ap_get_module_config(req->request_config, &mod_shib);
314 virtual ~ShibTargetApache() {}
316 const char* getScheme() const {
317 return m_sc->szScheme ? m_sc->szScheme : ap_http_method(m_req);
319 const char* getHostname() const {
320 return ap_get_server_name(m_req);
322 int getPort() const {
323 return ap_get_server_port(m_req);
325 const char* getRequestURI() const {
326 return m_req->unparsed_uri;
328 const char* getMethod() const {
329 return m_req->method;
331 string getContentType() const {
332 const char* type = ap_table_get(m_req->headers_in, "Content-Type");
333 return type ? type : "";
335 long getContentLength() const {
336 return m_gotBody ? m_body.length() : m_req->remaining;
338 string getRemoteAddr() const {
339 return m_req->connection->remote_ip;
341 void log(SPLogLevel level, const string& msg) const {
342 AbstractSPRequest::log(level,msg);
345 (level == SPDebug ? APLOG_DEBUG :
346 (level == SPInfo ? APLOG_INFO :
347 (level == SPWarn ? APLOG_WARNING :
348 (level == SPError ? APLOG_ERR : APLOG_CRIT))))|APLOG_NOERRNO,
353 const char* getQueryString() const { return m_req->args; }
354 const char* getRequestBody() const {
355 if (m_gotBody || m_req->method_number==M_GET)
356 return m_body.c_str();
357 #ifdef SHIB_APACHE_13
358 // Read the posted data
359 if (ap_setup_client_block(m_req, REQUEST_CHUNKED_DECHUNK) != OK) {
361 log(SPError, "Apache function (setup_client_block) failed while reading request body.");
362 return m_body.c_str();
364 if (!ap_should_client_block(m_req)) {
366 log(SPError, "Apache function (should_client_block) failed while reading request body.");
367 return m_body.c_str();
369 if (m_req->remaining > 1024*1024)
370 throw opensaml::SecurityPolicyException("Blocked request body larger than 1M size limit.");
373 char buff[HUGE_STRING_LEN];
374 ap_hard_timeout("[mod_shib] getRequestBody", m_req);
375 while ((len=ap_get_client_block(m_req, buff, sizeof(buff))) > 0) {
376 ap_reset_timeout(m_req);
377 m_body.append(buff, len);
379 ap_kill_timeout(m_req);
384 apr_bucket_brigade* bb = apr_brigade_create(m_req->pool, m_req->connection->bucket_alloc);
387 apr_status_t rv = ap_get_brigade(m_req->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN);
388 if (rv != APR_SUCCESS) {
389 log(SPError, "Apache function (ap_get_brigade) failed while reading request body.");
393 for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb); bucket = APR_BUCKET_NEXT(bucket)) {
394 if (APR_BUCKET_IS_EOS(bucket)) {
399 /* We can't do much with this. */
400 if (APR_BUCKET_IS_FLUSH(bucket))
404 apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
406 m_body.append(data, len);
408 apr_brigade_cleanup(bb);
410 apr_brigade_destroy(bb);
413 return m_body.c_str();
415 void clearHeader(const char* rawname, const char* cginame) {
416 if (m_dc->bUseHeaders == 1) {
417 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: hdr\n");
418 if (g_checkSpoofing && ap_is_initial_req(m_req)) {
419 if (m_allhttp.empty()) {
420 // First time, so populate set with "CGI" versions of client-supplied headers.
421 #ifdef SHIB_APACHE_13
422 array_header *hdrs_arr = ap_table_elts(m_req->headers_in);
423 table_entry *hdrs = (table_entry *) hdrs_arr->elts;
425 const apr_array_header_t *hdrs_arr = apr_table_elts(m_req->headers_in);
426 const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
428 for (int i = 0; i < hdrs_arr->nelts; ++i) {
431 string cgiversion("HTTP_");
432 const char* pch = hdrs[i].key;
434 cgiversion += (isalnum(*pch) ? toupper(*pch) : '_');
437 m_allhttp.insert(cgiversion);
441 if (m_allhttp.count(cginame) > 0)
442 throw opensaml::SecurityPolicyException("Attempt to spoof header ($1) was detected.", params(1, rawname));
444 ap_table_unset(m_req->headers_in, rawname);
445 ap_table_set(m_req->headers_in, rawname, g_unsetHeaderValue.c_str());
448 void setHeader(const char* name, const char* value) {
449 if (m_dc->bUseEnvVars != 0) {
451 // this happens on subrequests
452 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_setheader: no_m_rc\n");
453 m_rc = init_request_config(m_req);
456 m_rc->env = ap_make_table(m_req->pool, 10);
457 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_env: %s=%s\n", name, value?value:"Null");
458 ap_table_set(m_rc->env, name, value ? value : "");
460 if (m_dc->bUseHeaders == 1)
461 ap_table_set(m_req->headers_in, name, value);
463 string getHeader(const char* name) const {
464 const char* hdr = ap_table_get(m_req->headers_in, name);
465 return string(hdr ? hdr : "");
467 string getSecureHeader(const char* name) const {
468 if (m_dc->bUseEnvVars != 0) {
470 if (m_rc && m_rc->env)
471 hdr = ap_table_get(m_rc->env, name);
474 return string(hdr ? hdr : "");
476 return getHeader(name);
478 void setRemoteUser(const char* user) {
479 SH_AP_USER(m_req) = user ? ap_pstrdup(m_req->pool, user) : NULL;
481 string getRemoteUser() const {
482 return string(SH_AP_USER(m_req) ? SH_AP_USER(m_req) : "");
484 void setContentType(const char* type) {
485 m_req->content_type = ap_psprintf(m_req->pool, type);
487 void setResponseHeader(const char* name, const char* value) {
488 #ifdef SHIB_DEFERRED_HEADERS
490 // this happens on subrequests
491 m_rc = init_request_config(m_req);
493 ap_table_add(m_rc->hdr_out, name, value);
496 ap_table_add(m_req->err_headers_out, name, value);
498 long sendResponse(istream& in, long status) {
499 ap_send_http_header(m_req);
503 ap_rwrite(buf,in.gcount(),m_req);
505 if (status!=XMLTOOLING_HTTP_STATUS_OK)
506 m_req->status = status;
509 long sendRedirect(const char* url) {
510 ap_table_set(m_req->headers_out, "Location", url);
513 const vector<string>& getClientCertificates() const {
514 if (m_certs.empty()) {
515 const char* cert = ap_table_get(m_req->subprocess_env, "SSL_CLIENT_CERT");
517 m_certs.push_back(cert);
520 cert = ap_table_get(m_req->subprocess_env, ap_psprintf(m_req->pool, "SSL_CLIENT_CERT_CHAIN_%d", i++));
522 m_certs.push_back(cert);
527 long returnDecline(void) { return DECLINED; }
528 long returnOK(void) { return OK; }
531 /********************************************************************************/
534 extern "C" int shib_check_user(request_rec* r)
536 // Short-circuit entirely?
537 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
540 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_check_user(%d): ENTER", (int)getpid());
542 ostringstream threadid;
543 threadid << "[" << getpid() << "] shib_check_user" << '\0';
544 xmltooling::NDC ndc(threadid.str().c_str());
547 ShibTargetApache sta(r,false);
549 // Check user authentication and export information, then set the handler bypass
550 pair<bool,long> res = sta.getServiceProvider().doAuthentication(sta,true);
551 apr_pool_userdata_setn((const void*)42,g_UserDataKey,NULL,r->pool);
552 if (res.first) return res.second;
554 // user auth was okay -- export the assertions now
555 res = sta.getServiceProvider().doExport(sta);
556 if (res.first) return res.second;
558 // export happened successfully.. this user is ok.
561 catch (exception& e) {
562 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an exception: %s", e.what());
567 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an uncaught exception!");
573 extern "C" int shib_handler(request_rec* r)
575 // Short-circuit entirely?
576 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
579 ostringstream threadid;
580 threadid << "[" << getpid() << "] shib_handler" << '\0';
581 xmltooling::NDC ndc(threadid.str().c_str());
583 #ifndef SHIB_APACHE_13
584 // With 2.x, this handler always runs, though last.
585 // We check if shib_check_user ran, because it will detect a handler request
586 // and dispatch it directly.
588 apr_pool_userdata_get(&data,g_UserDataKey,r->pool);
589 if (data==(const void*)42) {
590 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler skipped since check_user ran");
595 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler(%d): ENTER: %s", (int)getpid(), r->handler);
598 ShibTargetApache sta(r,true);
600 pair<bool,long> res = sta.getServiceProvider().doHandler(sta);
601 if (res.first) return res.second;
603 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "doHandler() did not do anything.");
606 catch (exception& e) {
607 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an exception: %s", e.what());
612 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an uncaught exception!");
619 * shib_auth_checker() -- a simple resource manager to
620 * process the .htaccess settings
622 extern "C" int shib_auth_checker(request_rec* r)
624 // Short-circuit entirely?
625 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
628 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_auth_checker(%d): ENTER", (int)getpid());
630 ostringstream threadid;
631 threadid << "[" << getpid() << "] shib_auth_checker" << '\0';
632 xmltooling::NDC ndc(threadid.str().c_str());
635 ShibTargetApache sta(r,false);
637 pair<bool,long> res = sta.getServiceProvider().doAuthorization(sta);
638 if (res.first) return res.second;
643 catch (exception& e) {
644 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an exception: %s", e.what());
649 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an uncaught exception!");
655 // Access control plugin that enforces htaccess rules
656 class htAccessControl : virtual public AccessControl
660 ~htAccessControl() {}
661 Lockable* lock() {return this;}
663 bool authorized(const SPRequest& request, const Session* session) const;
665 bool checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const;
668 AccessControl* htAccessFactory(const xercesc::DOMElement* const & e)
670 return new htAccessControl();
673 class ApacheRequestMapper : public virtual RequestMapper, public virtual PropertySet
676 ApacheRequestMapper(const xercesc::DOMElement* e);
677 ~ApacheRequestMapper() { delete m_mapper; delete m_htaccess; delete m_staKey; delete m_propsKey; }
678 Lockable* lock() { return m_mapper->lock(); }
679 void unlock() { m_staKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
680 Settings getSettings(const SPRequest& request) const;
682 const PropertySet* getParent() const { return NULL; }
683 void setParent(const PropertySet*) {}
684 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
685 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
686 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
687 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
688 pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
689 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:2.0:native:sp:config") const;
690 const xercesc::DOMElement* getElement() const;
693 RequestMapper* m_mapper;
695 ThreadKey* m_propsKey;
696 AccessControl* m_htaccess;
699 RequestMapper* ApacheRequestMapFactory(const xercesc::DOMElement* const & e)
701 return new ApacheRequestMapper(e);
704 ApacheRequestMapper::ApacheRequestMapper(const xercesc::DOMElement* e) : m_mapper(NULL), m_staKey(NULL), m_propsKey(NULL), m_htaccess(NULL)
706 m_mapper=SPConfig::getConfig().RequestMapperManager.newPlugin(XML_REQUEST_MAPPER,e);
707 m_htaccess=new htAccessControl();
708 m_staKey=ThreadKey::create(NULL);
709 m_propsKey=ThreadKey::create(NULL);
712 RequestMapper::Settings ApacheRequestMapper::getSettings(const SPRequest& request) const
714 Settings s=m_mapper->getSettings(request);
715 m_staKey->setData((void*)dynamic_cast<const ShibTargetApache*>(&request));
716 m_propsKey->setData((void*)s.first);
717 return pair<const PropertySet*,AccessControl*>(this,s.second ? s.second : m_htaccess);
720 pair<bool,bool> ApacheRequestMapper::getBool(const char* name, const char* ns) const
722 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
723 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
725 // Override Apache-settable boolean properties.
726 if (name && !strcmp(name,"requireSession") && sta->m_dc->bRequireSession != -1)
727 return make_pair(true, sta->m_dc->bRequireSession==1);
728 else if (name && !strcmp(name,"exportAssertion") && sta->m_dc->bExportAssertion != -1)
729 return make_pair(true, sta->m_dc->bExportAssertion==1);
730 else if (sta->m_dc->tSettings) {
731 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
733 return make_pair(true, !strcmp(prop, "true") || !strcmp(prop, "1") || !strcmp(prop, "On"));
736 return s ? s->getBool(name,ns) : make_pair(false,false);
739 pair<bool,const char*> ApacheRequestMapper::getString(const char* name, const char* ns) const
741 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
742 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
744 // Override Apache-settable string properties.
745 if (name && !strcmp(name,"authType")) {
746 const char *auth_type=ap_auth_type(sta->m_req);
748 // Check for Basic Hijack
749 if (!strcasecmp(auth_type, "basic") && sta->m_dc->bBasicHijack == 1)
750 auth_type = "shibboleth";
751 return make_pair(true,auth_type);
754 else if (name && !strcmp(name,"applicationId") && sta->m_dc->szApplicationId)
755 return pair<bool,const char*>(true,sta->m_dc->szApplicationId);
756 else if (name && !strcmp(name,"requireSessionWith") && sta->m_dc->szRequireWith)
757 return pair<bool,const char*>(true,sta->m_dc->szRequireWith);
758 else if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
759 return pair<bool,const char*>(true,sta->m_dc->szRedirectToSSL);
760 else if (sta->m_dc->tSettings) {
761 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
763 return make_pair(true, prop);
766 return s ? s->getString(name,ns) : pair<bool,const char*>(false,NULL);
769 pair<bool,const XMLCh*> ApacheRequestMapper::getXMLString(const char* name, const char* ns) const
771 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
772 return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,NULL);
775 pair<bool,unsigned int> ApacheRequestMapper::getUnsignedInt(const char* name, const char* ns) const
777 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
778 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
780 // Override Apache-settable int properties.
781 if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
782 return pair<bool,unsigned int>(true, strtol(sta->m_dc->szRedirectToSSL, NULL, 10));
783 else if (sta->m_dc->tSettings) {
784 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
786 return make_pair(true, strtol(prop, NULL, 10));
789 return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
792 pair<bool,int> ApacheRequestMapper::getInt(const char* name, const char* ns) const
794 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
795 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
797 // Override Apache-settable int properties.
798 if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
799 return pair<bool,int>(true,atoi(sta->m_dc->szRedirectToSSL));
800 else if (sta->m_dc->tSettings) {
801 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
803 return make_pair(true, atoi(prop));
806 return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
809 const PropertySet* ApacheRequestMapper::getPropertySet(const char* name, const char* ns) const
811 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
812 return s ? s->getPropertySet(name,ns) : NULL;
815 const xercesc::DOMElement* ApacheRequestMapper::getElement() const
817 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
818 return s ? s->getElement() : NULL;
821 static SH_AP_TABLE* groups_for_user(request_rec* r, const char* user, char* grpfile)
824 SH_AP_TABLE* grps=ap_make_table(r->pool,15);
825 char l[MAX_STRING_LEN];
826 const char *group_name, *ll, *w;
828 #ifdef SHIB_APACHE_13
829 if (!(f=ap_pcfg_openfile(r->pool,grpfile))) {
831 if (ap_pcfg_openfile(&f,r->pool,grpfile) != APR_SUCCESS) {
833 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,SH_AP_R(r),"groups_for_user() could not open group file: %s\n",grpfile);
838 #ifdef SHIB_APACHE_13
839 sp=ap_make_sub_pool(r->pool);
841 if (apr_pool_create(&sp,r->pool) != APR_SUCCESS) {
842 ap_log_rerror(APLOG_MARK,APLOG_ERR,0,r,
843 "groups_for_user() could not create a subpool");
848 while (!(ap_cfg_getline(l,MAX_STRING_LEN,f))) {
849 if ((*l=='#') || (!*l))
854 group_name=ap_getword(sp,&ll,':');
857 w=ap_getword_conf(sp,&ll);
858 if (!strcmp(w,user)) {
859 ap_table_setn(grps,ap_pstrdup(r->pool,group_name),"in");
869 bool htAccessControl::checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const
871 bool caseSensitive = attr->isCaseSensitive();
872 const vector<string>& vals = attr->getSerializedValues();
873 for (vector<string>::const_iterator v=vals.begin(); v!=vals.end(); ++v) {
875 auto_arrayptr<XMLCh> trans(fromUTF8(v->c_str()));
876 if (re->matches(trans.get())) {
877 request.log(SPRequest::SPDebug,
878 string("htAccessControl plugin expecting regexp ") + toMatch + ", got " + *v + ": authorization granted"
883 else if ((caseSensitive && *v == toMatch) || (!caseSensitive && !strcasecmp(v->c_str(), toMatch))) {
884 request.log(SPRequest::SPDebug,
885 string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization granted."
890 request.log(SPRequest::SPDebug,
891 string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization not granted."
898 bool htAccessControl::authorized(const SPRequest& request, const Session* session) const
900 // Make sure the object is our type.
901 const ShibTargetApache* sta=dynamic_cast<const ShibTargetApache*>(&request);
903 throw ConfigurationException("Request wrapper object was not of correct type.");
907 int m=sta->m_req->method_number;
908 bool method_restricted=false;
911 const array_header* reqs_arr=ap_requires(sta->m_req);
915 require_line* reqs=(require_line*)reqs_arr->elts;
917 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE nelts: %d", reqs_arr->nelts);
918 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE all: %d", sta->m_dc->bRequireAll);
920 vector<bool> auth_OK(reqs_arr->nelts,false);
922 #define SHIB_AP_CHECK_IS_OK { \
923 if (sta->m_dc->bRequireAll < 1) \
929 for (int x=0; x<reqs_arr->nelts; x++) {
931 if (!(reqs[x].method_mask & (1 << m)))
933 method_restricted=true;
934 string remote_user = request.getRemoteUser();
936 t = reqs[x].requirement;
937 w = ap_getword_white(sta->m_req->pool, &t);
939 if (!strcasecmp(w,"shibboleth")) {
940 // This is a dummy rule needed because Apache conflates authn and authz.
941 // Without some require rule, AuthType is ignored and no check_user hooks run.
944 else if (!strcmp(w,"valid-user")) {
946 request.log(SPRequest::SPDebug,"htAccessControl plugin accepting valid-user based on active session");
950 request.log(SPRequest::SPError,"htAccessControl plugin rejecting access for valid-user rule, no session is active");
952 else if (!strcmp(w,"user") && !remote_user.empty()) {
955 w=ap_getword_conf(sta->m_req->pool,&t);
963 // To do regex matching, we have to convert from UTF-8.
964 auto_arrayptr<XMLCh> trans(fromUTF8(w));
965 RegularExpression re(trans.get());
966 auto_arrayptr<XMLCh> trans2(fromUTF8(remote_user.c_str()));
967 if (re.matches(trans2.get())) {
968 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting user (") + w + ")");
972 catch (XMLException& ex) {
973 auto_ptr_char tmp(ex.getMessage());
974 request.log(SPRequest::SPError,
975 string("htAccessControl plugin caught exception while parsing regular expression (") + w + "): " + tmp.get());
978 else if (remote_user==w) {
979 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting user (") + w + ")");
984 else if (!strcmp(w,"group")) {
985 SH_AP_TABLE* grpstatus=NULL;
986 if (sta->m_dc->szAuthGrpFile && !remote_user.empty()) {
987 request.log(SPRequest::SPDebug,string("htAccessControl plugin using groups file: ") + sta->m_dc->szAuthGrpFile);
988 grpstatus=groups_for_user(sta->m_req,remote_user.c_str(),sta->m_dc->szAuthGrpFile);
994 w=ap_getword_conf(sta->m_req->pool,&t);
995 if (ap_table_get(grpstatus,w)) {
996 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting group (") + w + ")");
1002 // Map alias in rule to the attribute.
1004 request.log(SPRequest::SPError, "htAccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
1008 // Find the attribute(s) matching the require rule.
1009 pair<multimap<string,const Attribute*>::const_iterator,multimap<string,const Attribute*>::const_iterator> attrs =
1010 session->getIndexedAttributes().equal_range(w);
1011 if (attrs.first == attrs.second) {
1012 request.log(SPRequest::SPWarn, string("htAccessControl rule requires attribute (") + w + "), not found in session");
1018 while (!auth_OK[x] && *t) {
1019 w=ap_getword_conf(sta->m_req->pool,&t);
1026 auto_ptr<RegularExpression> re;
1028 delete re.release();
1029 auto_arrayptr<XMLCh> trans(fromUTF8(w));
1030 auto_ptr<xercesc::RegularExpression> temp(new xercesc::RegularExpression(trans.get()));
1034 for (; !auth_OK[x] && attrs.first!=attrs.second; ++attrs.first) {
1035 if (checkAttribute(request, attrs.first->second, w, regexp ? re.get() : NULL)) {
1036 SHIB_AP_CHECK_IS_OK;
1040 catch (XMLException& ex) {
1041 auto_ptr_char tmp(ex.getMessage());
1042 request.log(SPRequest::SPError,
1043 string("htAccessControl plugin caught exception while parsing regular expression (") + w + "): " + tmp.get()
1050 // check if all require directives are true
1051 bool auth_all_OK = true;
1052 for (int i= 0; i<reqs_arr->nelts; i++) {
1053 auth_all_OK &= auth_OK[i];
1055 if (auth_all_OK || !method_restricted)
1062 // Initial look at a request - create the per-request structure
1063 static int shib_post_read(request_rec *r)
1065 shib_request_config* rc = init_request_config(r);
1067 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_post_read");
1069 #ifdef SHIB_DEFERRED_HEADERS
1070 rc->hdr_out = ap_make_table(r->pool, 5);
1075 // fixups: set environment vars
1077 extern "C" int shib_fixups(request_rec* r)
1079 shib_request_config *rc = (shib_request_config*)ap_get_module_config(r->request_config, &mod_shib);
1080 shib_dir_config *dc = (shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib);
1081 if (dc->bOff==1 || dc->bUseEnvVars==0)
1084 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup(%d): ENTER", (int)getpid());
1086 if (rc==NULL || rc->env==NULL || ap_is_empty_table(rc->env))
1089 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup adding %d vars", ap_table_elts(rc->env)->nelts);
1090 r->subprocess_env = ap_overlay_tables(r->pool, r->subprocess_env, rc->env);
1095 #ifdef SHIB_APACHE_13
1098 * Cleanup the (per-process) pool info.
1100 extern "C" void shib_child_exit(server_rec* s, SH_AP_POOL* p)
1103 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit(%d) dealing with g_Config..", (int)getpid());
1106 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit() done");
1112 * Apache 2.x doesn't allow for per-child cleanup, causes CGI forks to hang.
1114 extern "C" apr_status_t shib_exit(void* data)
1120 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
1126 * shire_child_init()
1127 * Things to do when the child process is initialized.
1128 * (or after the configs are read in apache-2)
1130 #ifdef SHIB_APACHE_13
1131 extern "C" void shib_child_init(server_rec* s, SH_AP_POOL* p)
1133 extern "C" void shib_child_init(apr_pool_t* p, server_rec* s)
1136 // Initialize runtime components.
1138 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init(%d) starting", (int)getpid());
1141 ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() already initialized!");
1145 g_Config=&SPConfig::getConfig();
1146 g_Config->setFeatures(
1147 SPConfig::Listener |
1149 SPConfig::RequestMapping |
1150 SPConfig::InProcess |
1153 if (!g_Config->init(g_szSchemaDir)) {
1154 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize libraries");
1157 g_Config->AccessControlManager.registerFactory(HT_ACCESS_CONTROL,&htAccessFactory);
1158 g_Config->RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&ApacheRequestMapFactory);
1161 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
1162 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
1163 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
1164 auto_ptr_XMLCh src(g_szSHIBConfig);
1165 dummy->setAttributeNS(NULL,path,src.get());
1166 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
1168 g_Config->setServiceProvider(g_Config->ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
1169 g_Config->getServiceProvider()->init();
1171 catch (exception& ex) {
1172 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),ex.what());
1173 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to load configuration");
1177 ServiceProvider* sp=g_Config->getServiceProvider();
1178 xmltooling::Locker locker(sp);
1179 const PropertySet* props=sp->getPropertySet("Local");
1181 pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
1182 if (unsetValue.first)
1183 g_unsetHeaderValue = unsetValue.second;
1184 pair<bool,bool> checkSpoofing=props->getBool("checkSpoofing");
1185 if (checkSpoofing.first && !checkSpoofing.second)
1186 g_checkSpoofing = false;
1189 // Set the cleanup handler
1190 apr_pool_cleanup_register(p, NULL, &shib_exit, apr_pool_cleanup_null);
1192 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() done");
1196 #ifdef SHIB_DEFERRED_HEADERS
1197 static void set_output_filter(request_rec *r)
1199 ap_add_output_filter("SHIB_HEADERS_OUT", NULL, r, r->connection);
1202 static void set_error_filter(request_rec *r)
1204 ap_add_output_filter("SHIB_HEADERS_ERR", NULL, r, r->connection);
1207 static int _table_add(void *v, const char *key, const char *value)
1209 apr_table_addn((apr_table_t*)v, key, value);
1213 static apr_status_t do_output_filter(ap_filter_t *f, apr_bucket_brigade *in)
1215 request_rec *r = f->r;
1216 shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
1219 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);
1220 apr_table_do(_table_add,r->headers_out, rc->hdr_out,NULL);
1221 // can't use overlap call because it will collapse Set-Cookie headers
1222 //apr_table_overlap(r->headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
1225 /* remove ourselves from the filter chain */
1226 ap_remove_output_filter(f);
1228 /* send the data up the stack */
1229 return ap_pass_brigade(f->next,in);
1232 static apr_status_t do_error_filter(ap_filter_t *f, apr_bucket_brigade *in)
1234 request_rec *r = f->r;
1235 shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
1238 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);
1239 apr_table_do(_table_add,r->err_headers_out, rc->hdr_out,NULL);
1240 // can't use overlap call because it will collapse Set-Cookie headers
1241 //apr_table_overlap(r->err_headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
1244 /* remove ourselves from the filter chain */
1245 ap_remove_output_filter(f);
1247 /* send the data up the stack */
1248 return ap_pass_brigade(f->next,in);
1250 #endif // SHIB_DEFERRED_HEADERS
1252 typedef const char* (*config_fn_t)(void);
1254 #ifdef SHIB_APACHE_13
1256 // SHIB Module commands
1258 static command_rec shire_cmds[] = {
1259 {"ShibConfig", (config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
1260 RSRC_CONF, TAKE1, "Path to shibboleth.xml config file"},
1261 {"ShibCatalogs", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1262 RSRC_CONF, TAKE1, "Paths of XML schema catalogs"},
1263 {"ShibSchemaDir", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1264 RSRC_CONF, TAKE1, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"},
1266 {"ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
1267 (void *) XtOffsetOf (shib_server_config, szScheme),
1268 RSRC_CONF, TAKE1, "URL scheme to force into generated URLs for a vhost"},
1270 {"ShibRequestSetting", (config_fn_t)shib_table_set, NULL,
1271 OR_AUTHCFG, TAKE2, "Set arbitrary Shibboleth request property for content"},
1273 {"ShibDisable", (config_fn_t)ap_set_flag_slot,
1274 (void *) XtOffsetOf (shib_dir_config, bOff),
1275 OR_AUTHCFG, FLAG, "Disable all Shib module activity here to save processing effort"},
1276 {"ShibApplicationId", (config_fn_t)ap_set_string_slot,
1277 (void *) XtOffsetOf (shib_dir_config, szApplicationId),
1278 OR_AUTHCFG, TAKE1, "Set Shibboleth applicationId property for content"},
1279 {"ShibBasicHijack", (config_fn_t)ap_set_flag_slot,
1280 (void *) XtOffsetOf (shib_dir_config, bBasicHijack),
1281 OR_AUTHCFG, FLAG, "Respond to AuthType Basic and convert to shibboleth"},
1282 {"ShibRequireSession", (config_fn_t)ap_set_flag_slot,
1283 (void *) XtOffsetOf (shib_dir_config, bRequireSession),
1284 OR_AUTHCFG, FLAG, "Initiates a new session if one does not exist"},
1285 {"ShibRequireSessionWith", (config_fn_t)ap_set_string_slot,
1286 (void *) XtOffsetOf (shib_dir_config, szRequireWith),
1287 OR_AUTHCFG, TAKE1, "Initiates a new session if one does not exist using a specific SessionInitiator"},
1288 {"ShibExportAssertion", (config_fn_t)ap_set_flag_slot,
1289 (void *) XtOffsetOf (shib_dir_config, bExportAssertion),
1290 OR_AUTHCFG, FLAG, "Export SAML attribute assertion(s) to Shib-Attributes header"},
1291 {"ShibRedirectToSSL", (config_fn_t)ap_set_string_slot,
1292 (void *) XtOffsetOf (shib_dir_config, szRedirectToSSL),
1293 OR_AUTHCFG, TAKE1, "Redirect non-SSL requests to designated port" },
1294 {"AuthGroupFile", (config_fn_t)shib_ap_set_file_slot,
1295 (void *) XtOffsetOf (shib_dir_config, szAuthGrpFile),
1296 OR_AUTHCFG, TAKE1, "text file containing group names and member user IDs"},
1297 {"ShibRequireAll", (config_fn_t)ap_set_flag_slot,
1298 (void *) XtOffsetOf (shib_dir_config, bRequireAll),
1299 OR_AUTHCFG, FLAG, "All require directives must match"},
1300 {"ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
1301 (void *) XtOffsetOf (shib_dir_config, bUseEnvVars),
1302 OR_AUTHCFG, FLAG, "Export attributes using environment variables (default)"},
1303 {"ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
1304 (void *) XtOffsetOf (shib_dir_config, bUseHeaders),
1305 OR_AUTHCFG, FLAG, "Export attributes using custom HTTP headers"},
1311 handler_rec shib_handlers[] = {
1312 { "shib-handler", shib_handler },
1316 module MODULE_VAR_EXPORT mod_shib = {
1317 STANDARD_MODULE_STUFF,
1318 NULL, /* initializer */
1319 create_shib_dir_config, /* dir config creater */
1320 merge_shib_dir_config, /* dir merger --- default is to override */
1321 create_shib_server_config, /* server config */
1322 merge_shib_server_config, /* merge server config */
1323 shire_cmds, /* command table */
1324 shib_handlers, /* handlers */
1325 NULL, /* filename translation */
1326 shib_check_user, /* check_user_id */
1327 shib_auth_checker, /* check auth */
1328 NULL, /* check access */
1329 NULL, /* type_checker */
1330 NULL,//shib_fixups, /* fixups */
1332 NULL, /* header parser */
1333 shib_child_init, /* child_init */
1334 shib_child_exit, /* child_exit */
1335 NULL//shib_post_read /* post read-request */
1338 #elif defined(SHIB_APACHE_20) || defined(SHIB_APACHE_22)
1340 extern "C" void shib_register_hooks (apr_pool_t *p)
1342 #ifdef SHIB_DEFERRED_HEADERS
1343 ap_register_output_filter("SHIB_HEADERS_OUT", do_output_filter, NULL, AP_FTYPE_CONTENT_SET);
1344 ap_hook_insert_filter(set_output_filter, NULL, NULL, APR_HOOK_LAST);
1345 ap_register_output_filter("SHIB_HEADERS_ERR", do_error_filter, NULL, AP_FTYPE_CONTENT_SET);
1346 ap_hook_insert_error_filter(set_error_filter, NULL, NULL, APR_HOOK_LAST);
1347 ap_hook_post_read_request(shib_post_read, NULL, NULL, APR_HOOK_MIDDLE);
1349 ap_hook_child_init(shib_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1350 ap_hook_check_user_id(shib_check_user, NULL, NULL, APR_HOOK_MIDDLE);
1351 ap_hook_auth_checker(shib_auth_checker, NULL, NULL, APR_HOOK_FIRST);
1352 ap_hook_handler(shib_handler, NULL, NULL, APR_HOOK_LAST);
1353 ap_hook_fixups(shib_fixups, NULL, NULL, APR_HOOK_MIDDLE);
1356 // SHIB Module commands
1359 static command_rec shib_cmds[] = {
1360 AP_INIT_TAKE1("ShibConfig", (config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
1361 RSRC_CONF, "Path to shibboleth.xml config file"),
1362 AP_INIT_TAKE1("ShibCatalogs", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1363 RSRC_CONF, "Paths of XML schema catalogs"),
1364 AP_INIT_TAKE1("ShibSchemaDir", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1365 RSRC_CONF, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"),
1367 AP_INIT_TAKE1("ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
1368 (void *) offsetof (shib_server_config, szScheme),
1369 RSRC_CONF, "URL scheme to force into generated URLs for a vhost"),
1371 AP_INIT_TAKE2("ShibRequestSetting", (config_fn_t)shib_table_set, NULL,
1372 OR_AUTHCFG, "Set arbitrary Shibboleth request property for content"),
1374 AP_INIT_FLAG("ShibDisable", (config_fn_t)ap_set_flag_slot,
1375 (void *) offsetof (shib_dir_config, bOff),
1376 OR_AUTHCFG, "Disable all Shib module activity here to save processing effort"),
1377 AP_INIT_TAKE1("ShibApplicationId", (config_fn_t)ap_set_string_slot,
1378 (void *) offsetof (shib_dir_config, szApplicationId),
1379 OR_AUTHCFG, "Set Shibboleth applicationId property for content"),
1380 AP_INIT_FLAG("ShibBasicHijack", (config_fn_t)ap_set_flag_slot,
1381 (void *) offsetof (shib_dir_config, bBasicHijack),
1382 OR_AUTHCFG, "Respond to AuthType Basic and convert to shibboleth"),
1383 AP_INIT_FLAG("ShibRequireSession", (config_fn_t)ap_set_flag_slot,
1384 (void *) offsetof (shib_dir_config, bRequireSession),
1385 OR_AUTHCFG, "Initiates a new session if one does not exist"),
1386 AP_INIT_TAKE1("ShibRequireSessionWith", (config_fn_t)ap_set_string_slot,
1387 (void *) offsetof (shib_dir_config, szRequireWith),
1388 OR_AUTHCFG, "Initiates a new session if one does not exist using a specific SessionInitiator"),
1389 AP_INIT_FLAG("ShibExportAssertion", (config_fn_t)ap_set_flag_slot,
1390 (void *) offsetof (shib_dir_config, bExportAssertion),
1391 OR_AUTHCFG, "Export SAML attribute assertion(s) to Shib-Attributes header"),
1392 AP_INIT_TAKE1("ShibRedirectToSSL", (config_fn_t)ap_set_string_slot,
1393 (void *) offsetof (shib_dir_config, szRedirectToSSL),
1394 OR_AUTHCFG, "Redirect non-SSL requests to designated port"),
1395 AP_INIT_TAKE1("AuthGroupFile", (config_fn_t)shib_ap_set_file_slot,
1396 (void *) offsetof (shib_dir_config, szAuthGrpFile),
1397 OR_AUTHCFG, "Text file containing group names and member user IDs"),
1398 AP_INIT_FLAG("ShibRequireAll", (config_fn_t)ap_set_flag_slot,
1399 (void *) offsetof (shib_dir_config, bRequireAll),
1400 OR_AUTHCFG, "All require directives must match"),
1401 AP_INIT_FLAG("ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
1402 (void *) offsetof (shib_dir_config, bUseEnvVars),
1403 OR_AUTHCFG, "Export attributes using environment variables (default)"),
1404 AP_INIT_FLAG("ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
1405 (void *) offsetof (shib_dir_config, bUseHeaders),
1406 OR_AUTHCFG, "Export attributes using custom HTTP headers"),
1411 module AP_MODULE_DECLARE_DATA mod_shib = {
1412 STANDARD20_MODULE_STUFF,
1413 create_shib_dir_config, /* create dir config */
1414 merge_shib_dir_config, /* merge dir config --- default is to override */
1415 create_shib_server_config, /* create server config */
1416 merge_shib_server_config, /* merge server config */
1417 shib_cmds, /* command table */
1418 shib_register_hooks /* register hooks */
1422 #error "unsupported Apache version"