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 bool g_catchAll = false;
94 static const char* g_UserDataKey = "_shib_check_user_";
95 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
96 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
99 /* Apache 2.2.x headers must be accumulated and set in the output filter.
100 Apache 2.0.49+ supports the filter method.
101 Apache 1.3.x and lesser 2.0.x must write the headers directly. */
103 #if (defined(SHIB_APACHE_20) || defined(SHIB_APACHE_22)) && AP_MODULE_MAGIC_AT_LEAST(20020903,6)
104 #define SHIB_DEFERRED_HEADERS
107 /********************************************************************************/
108 // Basic Apache Configuration code.
111 // per-server module configuration structure
112 struct shib_server_config
117 // creates the per-server configuration
118 extern "C" void* create_shib_server_config(SH_AP_POOL* p, server_rec* s)
120 shib_server_config* sc=(shib_server_config*)ap_pcalloc(p,sizeof(shib_server_config));
125 // overrides server configuration in virtual servers
126 extern "C" void* merge_shib_server_config (SH_AP_POOL* p, void* base, void* sub)
128 shib_server_config* sc=(shib_server_config*)ap_pcalloc(p,sizeof(shib_server_config));
129 shib_server_config* parent=(shib_server_config*)base;
130 shib_server_config* child=(shib_server_config*)sub;
133 sc->szScheme=ap_pstrdup(p,child->szScheme);
134 else if (parent->szScheme)
135 sc->szScheme=ap_pstrdup(p,parent->szScheme);
142 // per-dir module configuration structure
143 struct shib_dir_config
145 SH_AP_TABLE* tSettings; // generic table of extensible settings
148 char* szAuthGrpFile; // Auth GroupFile name
149 int bRequireAll; // all "known" require directives must match, otherwise OR logic
150 int bAuthoritative; // allow htaccess plugin to DECLINE when authz fails
152 // Content Configuration
153 char* szApplicationId; // Shib applicationId value
154 char* szRequireWith; // require a session using a specific initiator?
155 char* szRedirectToSSL; // redirect non-SSL requests to SSL port
156 int bOff; // flat-out disable all Shib processing
157 int bBasicHijack; // activate for AuthType Basic?
158 int bRequireSession; // require a session?
159 int bExportAssertion; // export SAML assertion to the environment?
160 int bUseEnvVars; // use environment?
161 int bUseHeaders; // use headers?
164 // creates per-directory config structure
165 extern "C" void* create_shib_dir_config (SH_AP_POOL* p, char* d)
167 shib_dir_config* dc=(shib_dir_config*)ap_pcalloc(p,sizeof(shib_dir_config));
168 dc->tSettings = NULL;
169 dc->szAuthGrpFile = NULL;
170 dc->bRequireAll = -1;
171 dc->bAuthoritative = -1;
172 dc->szApplicationId = NULL;
173 dc->szRequireWith = NULL;
174 dc->szRedirectToSSL = NULL;
176 dc->bBasicHijack = -1;
177 dc->bRequireSession = -1;
178 dc->bExportAssertion = -1;
179 dc->bUseEnvVars = -1;
180 dc->bUseHeaders = -1;
184 // overrides server configuration in directories
185 extern "C" void* merge_shib_dir_config (SH_AP_POOL* p, void* base, void* sub)
187 shib_dir_config* dc=(shib_dir_config*)ap_pcalloc(p,sizeof(shib_dir_config));
188 shib_dir_config* parent=(shib_dir_config*)base;
189 shib_dir_config* child=(shib_dir_config*)sub;
191 // The child supersedes any matching table settings in the parent.
192 dc->tSettings = NULL;
193 if (parent->tSettings)
194 dc->tSettings = ap_copy_table(p, parent->tSettings);
195 if (child->tSettings) {
197 ap_overlap_tables(dc->tSettings, child->tSettings, AP_OVERLAP_TABLES_SET);
199 dc->tSettings = ap_copy_table(p, child->tSettings);
202 if (child->szAuthGrpFile)
203 dc->szAuthGrpFile=ap_pstrdup(p,child->szAuthGrpFile);
204 else if (parent->szAuthGrpFile)
205 dc->szAuthGrpFile=ap_pstrdup(p,parent->szAuthGrpFile);
207 dc->szAuthGrpFile=NULL;
209 if (child->szApplicationId)
210 dc->szApplicationId=ap_pstrdup(p,child->szApplicationId);
211 else if (parent->szApplicationId)
212 dc->szApplicationId=ap_pstrdup(p,parent->szApplicationId);
214 dc->szApplicationId=NULL;
216 if (child->szRequireWith)
217 dc->szRequireWith=ap_pstrdup(p,child->szRequireWith);
218 else if (parent->szRequireWith)
219 dc->szRequireWith=ap_pstrdup(p,parent->szRequireWith);
221 dc->szRequireWith=NULL;
223 if (child->szRedirectToSSL)
224 dc->szRedirectToSSL=ap_pstrdup(p,child->szRedirectToSSL);
225 else if (parent->szRedirectToSSL)
226 dc->szRedirectToSSL=ap_pstrdup(p,parent->szRedirectToSSL);
228 dc->szRedirectToSSL=NULL;
230 dc->bOff=((child->bOff==-1) ? parent->bOff : child->bOff);
231 dc->bBasicHijack=((child->bBasicHijack==-1) ? parent->bBasicHijack : child->bBasicHijack);
232 dc->bRequireSession=((child->bRequireSession==-1) ? parent->bRequireSession : child->bRequireSession);
233 dc->bExportAssertion=((child->bExportAssertion==-1) ? parent->bExportAssertion : child->bExportAssertion);
234 dc->bRequireAll=((child->bRequireAll==-1) ? parent->bRequireAll : child->bRequireAll);
235 dc->bAuthoritative=((child->bAuthoritative==-1) ? parent->bAuthoritative : child->bAuthoritative);
236 dc->bUseEnvVars=((child->bUseEnvVars==-1) ? parent->bUseEnvVars : child->bUseEnvVars);
237 dc->bUseHeaders=((child->bUseHeaders==-1) ? parent->bUseHeaders : child->bUseHeaders);
241 // per-request module structure
242 struct shib_request_config
244 SH_AP_TABLE *env; // environment vars
245 #ifdef SHIB_DEFERRED_HEADERS
246 SH_AP_TABLE *hdr_out; // headers to browser
250 // create a request record
251 static shib_request_config *init_request_config(request_rec *r)
253 shib_request_config* rc=(shib_request_config*)ap_pcalloc(r->pool,sizeof(shib_request_config));
254 ap_set_module_config (r->request_config, &mod_shib, rc);
255 memset(rc, 0, sizeof(shib_request_config));
256 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_init_rc");
260 // generic global slot handlers
261 extern "C" const char* ap_set_global_string_slot(cmd_parms* parms, void*, const char* arg)
263 *((char**)(parms->info))=ap_pstrdup(parms->pool,arg);
267 extern "C" const char* shib_set_server_string_slot(cmd_parms* parms, void*, const char* arg)
269 char* base=(char*)ap_get_module_config(parms->server->module_config,&mod_shib);
270 size_t offset=(size_t)parms->info;
271 *((char**)(base + offset))=ap_pstrdup(parms->pool,arg);
275 extern "C" const char* shib_ap_set_file_slot(cmd_parms* parms,
276 #ifdef SHIB_APACHE_13
277 char* arg1, char* arg2
279 void* arg1, const char* arg2
283 ap_set_file_slot(parms, arg1, arg2);
287 extern "C" const char* shib_table_set(cmd_parms* parms, shib_dir_config* dc, const char* arg1, const char* arg2)
290 dc->tSettings = ap_make_table(parms->pool, 4);
291 ap_table_set(dc->tSettings, arg1, arg2);
295 /********************************************************************************/
296 // Apache ShibTarget subclass(es) here.
298 class ShibTargetApache : public AbstractSPRequest
301 mutable string m_body;
302 mutable bool m_gotBody;
303 mutable vector<string> m_certs;
304 set<string> m_allhttp;
308 shib_dir_config* m_dc;
309 shib_server_config* m_sc;
310 shib_request_config* m_rc;
312 ShibTargetApache(request_rec* req, bool handler) : m_handler(handler), m_gotBody(false) {
313 m_sc = (shib_server_config*)ap_get_module_config(req->server->module_config, &mod_shib);
314 m_dc = (shib_dir_config*)ap_get_module_config(req->per_dir_config, &mod_shib);
315 m_rc = (shib_request_config*)ap_get_module_config(req->request_config, &mod_shib);
318 virtual ~ShibTargetApache() {}
320 const char* getScheme() const {
321 return m_sc->szScheme ? m_sc->szScheme : ap_http_method(m_req);
323 const char* getHostname() const {
324 return ap_get_server_name(m_req);
326 int getPort() const {
327 return ap_get_server_port(m_req);
329 const char* getRequestURI() const {
330 return m_req->unparsed_uri;
332 const char* getMethod() const {
333 return m_req->method;
335 string getContentType() const {
336 const char* type = ap_table_get(m_req->headers_in, "Content-Type");
337 return type ? type : "";
339 long getContentLength() const {
340 return m_gotBody ? m_body.length() : m_req->remaining;
342 string getRemoteAddr() const {
343 return m_req->connection->remote_ip;
345 void log(SPLogLevel level, const string& msg) const {
346 AbstractSPRequest::log(level,msg);
349 (level == SPDebug ? APLOG_DEBUG :
350 (level == SPInfo ? APLOG_INFO :
351 (level == SPWarn ? APLOG_WARNING :
352 (level == SPError ? APLOG_ERR : APLOG_CRIT))))|APLOG_NOERRNO,
357 const char* getQueryString() const { return m_req->args; }
358 const char* getRequestBody() const {
359 if (m_gotBody || m_req->method_number==M_GET)
360 return m_body.c_str();
361 #ifdef SHIB_APACHE_13
362 // Read the posted data
363 if (ap_setup_client_block(m_req, REQUEST_CHUNKED_DECHUNK) != OK) {
365 log(SPError, "Apache function (setup_client_block) failed while reading request body.");
366 return m_body.c_str();
368 if (!ap_should_client_block(m_req)) {
370 log(SPError, "Apache function (should_client_block) failed while reading request body.");
371 return m_body.c_str();
373 if (m_req->remaining > 1024*1024)
374 throw opensaml::SecurityPolicyException("Blocked request body larger than 1M size limit.");
377 char buff[HUGE_STRING_LEN];
378 ap_hard_timeout("[mod_shib] getRequestBody", m_req);
379 while ((len=ap_get_client_block(m_req, buff, sizeof(buff))) > 0) {
380 ap_reset_timeout(m_req);
381 m_body.append(buff, len);
383 ap_kill_timeout(m_req);
388 apr_bucket_brigade* bb = apr_brigade_create(m_req->pool, m_req->connection->bucket_alloc);
391 apr_status_t rv = ap_get_brigade(m_req->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN);
392 if (rv != APR_SUCCESS) {
393 log(SPError, "Apache function (ap_get_brigade) failed while reading request body.");
397 for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb); bucket = APR_BUCKET_NEXT(bucket)) {
398 if (APR_BUCKET_IS_EOS(bucket)) {
403 /* We can't do much with this. */
404 if (APR_BUCKET_IS_FLUSH(bucket))
408 apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
410 m_body.append(data, len);
412 apr_brigade_cleanup(bb);
414 apr_brigade_destroy(bb);
417 return m_body.c_str();
419 void clearHeader(const char* rawname, const char* cginame) {
420 if (m_dc->bUseHeaders == 1) {
421 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_clear_header: hdr\n");
422 if (g_checkSpoofing && ap_is_initial_req(m_req)) {
423 if (m_allhttp.empty()) {
424 // First time, so populate set with "CGI" versions of client-supplied headers.
425 #ifdef SHIB_APACHE_13
426 array_header *hdrs_arr = ap_table_elts(m_req->headers_in);
427 table_entry *hdrs = (table_entry *) hdrs_arr->elts;
429 const apr_array_header_t *hdrs_arr = apr_table_elts(m_req->headers_in);
430 const apr_table_entry_t *hdrs = (const apr_table_entry_t *) hdrs_arr->elts;
432 for (int i = 0; i < hdrs_arr->nelts; ++i) {
435 string cgiversion("HTTP_");
436 const char* pch = hdrs[i].key;
438 cgiversion += (isalnum(*pch) ? toupper(*pch) : '_');
441 m_allhttp.insert(cgiversion);
445 if (m_allhttp.count(cginame) > 0)
446 throw opensaml::SecurityPolicyException("Attempt to spoof header ($1) was detected.", params(1, rawname));
448 ap_table_unset(m_req->headers_in, rawname);
449 ap_table_set(m_req->headers_in, rawname, g_unsetHeaderValue.c_str());
452 void setHeader(const char* name, const char* value) {
453 if (m_dc->bUseEnvVars != 0) {
455 // this happens on subrequests
456 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_setheader: no_m_rc\n");
457 m_rc = init_request_config(m_req);
460 m_rc->env = ap_make_table(m_req->pool, 10);
461 // ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(m_req), "shib_set_env: %s=%s\n", name, value?value:"Null");
462 ap_table_set(m_rc->env, name, value ? value : "");
464 if (m_dc->bUseHeaders == 1)
465 ap_table_set(m_req->headers_in, name, value);
467 string getHeader(const char* name) const {
468 const char* hdr = ap_table_get(m_req->headers_in, name);
469 return string(hdr ? hdr : "");
471 string getSecureHeader(const char* name) const {
472 if (m_dc->bUseEnvVars != 0) {
474 if (m_rc && m_rc->env)
475 hdr = ap_table_get(m_rc->env, name);
478 return string(hdr ? hdr : "");
480 return getHeader(name);
482 void setRemoteUser(const char* user) {
483 SH_AP_USER(m_req) = user ? ap_pstrdup(m_req->pool, user) : NULL;
485 string getRemoteUser() const {
486 return string(SH_AP_USER(m_req) ? SH_AP_USER(m_req) : "");
488 void setContentType(const char* type) {
489 m_req->content_type = ap_psprintf(m_req->pool, type);
491 void setResponseHeader(const char* name, const char* value) {
492 #ifdef SHIB_DEFERRED_HEADERS
494 // this happens on subrequests
495 m_rc = init_request_config(m_req);
497 ap_table_add(m_rc->hdr_out, name, value);
500 ap_table_add(m_req->err_headers_out, name, value);
502 long sendResponse(istream& in, long status) {
503 ap_send_http_header(m_req);
507 ap_rwrite(buf,in.gcount(),m_req);
509 if (status!=XMLTOOLING_HTTP_STATUS_OK)
510 m_req->status = status;
513 long sendRedirect(const char* url) {
514 ap_table_set(m_req->headers_out, "Location", url);
517 const vector<string>& getClientCertificates() const {
518 if (m_certs.empty()) {
519 const char* cert = ap_table_get(m_req->subprocess_env, "SSL_CLIENT_CERT");
521 m_certs.push_back(cert);
524 cert = ap_table_get(m_req->subprocess_env, ap_psprintf(m_req->pool, "SSL_CLIENT_CERT_CHAIN_%d", i++));
526 m_certs.push_back(cert);
531 long returnDecline(void) { return DECLINED; }
532 long returnOK(void) { return OK; }
535 /********************************************************************************/
538 extern "C" int shib_check_user(request_rec* r)
540 // Short-circuit entirely?
541 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
544 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_check_user(%d): ENTER", (int)getpid());
546 ostringstream threadid;
547 threadid << "[" << getpid() << "] shib_check_user" << '\0';
548 xmltooling::NDC ndc(threadid.str().c_str());
551 ShibTargetApache sta(r,false);
553 // Check user authentication and export information, then set the handler bypass
554 pair<bool,long> res = sta.getServiceProvider().doAuthentication(sta,true);
555 apr_pool_userdata_setn((const void*)42,g_UserDataKey,NULL,r->pool);
556 if (res.first) return res.second;
558 // user auth was okay -- export the assertions now
559 res = sta.getServiceProvider().doExport(sta);
560 if (res.first) return res.second;
562 // export happened successfully.. this user is ok.
565 catch (exception& e) {
566 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an exception: %s", e.what());
570 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_check_user threw an unknown exception!");
577 extern "C" int shib_handler(request_rec* r)
579 // Short-circuit entirely?
580 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
583 ostringstream threadid;
584 threadid << "[" << getpid() << "] shib_handler" << '\0';
585 xmltooling::NDC ndc(threadid.str().c_str());
587 #ifndef SHIB_APACHE_13
588 // With 2.x, this handler always runs, though last.
589 // We check if shib_check_user ran, because it will detect a handler request
590 // and dispatch it directly.
592 apr_pool_userdata_get(&data,g_UserDataKey,r->pool);
593 if (data==(const void*)42) {
594 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler skipped since check_user ran");
599 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r),"shib_handler(%d): ENTER: %s", (int)getpid(), r->handler);
602 ShibTargetApache sta(r,true);
604 pair<bool,long> res = sta.getServiceProvider().doHandler(sta);
605 if (res.first) return res.second;
607 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "doHandler() did not do anything.");
610 catch (exception& e) {
611 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an exception: %s", e.what());
615 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_handler threw an unknown exception!");
623 * shib_auth_checker() -- a simple resource manager to
624 * process the .htaccess settings
626 extern "C" int shib_auth_checker(request_rec* r)
628 // Short-circuit entirely?
629 if (((shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib))->bOff==1)
632 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_auth_checker(%d): ENTER", (int)getpid());
634 ostringstream threadid;
635 threadid << "[" << getpid() << "] shib_auth_checker" << '\0';
636 xmltooling::NDC ndc(threadid.str().c_str());
639 ShibTargetApache sta(r,false);
641 pair<bool,long> res = sta.getServiceProvider().doAuthorization(sta);
642 if (res.first) return res.second;
644 // The SP method should always return true, so if we get this far, something unusual happened.
645 // Just let Apache (or some other module) decide what to do.
648 catch (exception& e) {
649 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an exception: %s", e.what());
653 ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, SH_AP_R(r), "shib_auth_checker threw an unknown exception!");
660 // Access control plugin that enforces htaccess rules
661 class htAccessControl : virtual public AccessControl
665 ~htAccessControl() {}
666 Lockable* lock() {return this;}
668 aclresult_t authorized(const SPRequest& request, const Session* session) const;
670 bool checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const;
673 AccessControl* htAccessFactory(const xercesc::DOMElement* const & e)
675 return new htAccessControl();
678 class ApacheRequestMapper : public virtual RequestMapper, public virtual PropertySet
681 ApacheRequestMapper(const xercesc::DOMElement* e);
682 ~ApacheRequestMapper() { delete m_mapper; delete m_htaccess; delete m_staKey; delete m_propsKey; }
683 Lockable* lock() { return m_mapper->lock(); }
684 void unlock() { m_staKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
685 Settings getSettings(const HTTPRequest& request) const;
687 const PropertySet* getParent() const { return NULL; }
688 void setParent(const PropertySet*) {}
689 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
690 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
691 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
692 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
693 pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
694 void getAll(map<string,const char*>& properties) const;
695 const PropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:2.0:native:sp:config") const;
696 const xercesc::DOMElement* getElement() const;
699 RequestMapper* m_mapper;
701 ThreadKey* m_propsKey;
702 AccessControl* m_htaccess;
705 RequestMapper* ApacheRequestMapFactory(const xercesc::DOMElement* const & e)
707 return new ApacheRequestMapper(e);
710 ApacheRequestMapper::ApacheRequestMapper(const xercesc::DOMElement* e) : m_mapper(NULL), m_staKey(NULL), m_propsKey(NULL), m_htaccess(NULL)
712 m_mapper=SPConfig::getConfig().RequestMapperManager.newPlugin(XML_REQUEST_MAPPER,e);
713 m_htaccess=new htAccessControl();
714 m_staKey=ThreadKey::create(NULL);
715 m_propsKey=ThreadKey::create(NULL);
718 RequestMapper::Settings ApacheRequestMapper::getSettings(const HTTPRequest& request) const
720 Settings s=m_mapper->getSettings(request);
721 m_staKey->setData((void*)dynamic_cast<const ShibTargetApache*>(&request));
722 m_propsKey->setData((void*)s.first);
723 return pair<const PropertySet*,AccessControl*>(this,s.second ? s.second : m_htaccess);
726 pair<bool,bool> ApacheRequestMapper::getBool(const char* name, const char* ns) const
728 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
729 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
731 // Override Apache-settable boolean properties.
732 if (name && !strcmp(name,"requireSession") && sta->m_dc->bRequireSession != -1)
733 return make_pair(true, sta->m_dc->bRequireSession==1);
734 else if (name && !strcmp(name,"exportAssertion") && sta->m_dc->bExportAssertion != -1)
735 return make_pair(true, sta->m_dc->bExportAssertion==1);
736 else if (sta->m_dc->tSettings) {
737 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
739 return make_pair(true, !strcmp(prop, "true") || !strcmp(prop, "1") || !strcmp(prop, "On"));
742 return s ? s->getBool(name,ns) : make_pair(false,false);
745 pair<bool,const char*> ApacheRequestMapper::getString(const char* name, const char* ns) const
747 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
748 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
750 // Override Apache-settable string properties.
751 if (name && !strcmp(name,"authType")) {
752 const char *auth_type=ap_auth_type(sta->m_req);
754 // Check for Basic Hijack
755 if (!strcasecmp(auth_type, "basic") && sta->m_dc->bBasicHijack == 1)
756 auth_type = "shibboleth";
757 return make_pair(true,auth_type);
760 else if (name && !strcmp(name,"applicationId") && sta->m_dc->szApplicationId)
761 return pair<bool,const char*>(true,sta->m_dc->szApplicationId);
762 else if (name && !strcmp(name,"requireSessionWith") && sta->m_dc->szRequireWith)
763 return pair<bool,const char*>(true,sta->m_dc->szRequireWith);
764 else if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
765 return pair<bool,const char*>(true,sta->m_dc->szRedirectToSSL);
766 else if (sta->m_dc->tSettings) {
767 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
769 return make_pair(true, prop);
772 return s ? s->getString(name,ns) : pair<bool,const char*>(false,NULL);
775 pair<bool,const XMLCh*> ApacheRequestMapper::getXMLString(const char* name, const char* ns) const
777 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
778 return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,NULL);
781 pair<bool,unsigned int> ApacheRequestMapper::getUnsignedInt(const char* name, const char* ns) const
783 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
784 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
786 // Override Apache-settable int properties.
787 if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
788 return pair<bool,unsigned int>(true, strtol(sta->m_dc->szRedirectToSSL, NULL, 10));
789 else if (sta->m_dc->tSettings) {
790 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
792 return make_pair(true, strtol(prop, NULL, 10));
795 return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
798 pair<bool,int> ApacheRequestMapper::getInt(const char* name, const char* ns) const
800 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
801 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
803 // Override Apache-settable int properties.
804 if (name && !strcmp(name,"redirectToSSL") && sta->m_dc->szRedirectToSSL)
805 return pair<bool,int>(true,atoi(sta->m_dc->szRedirectToSSL));
806 else if (sta->m_dc->tSettings) {
807 const char* prop = ap_table_get(sta->m_dc->tSettings, name);
809 return make_pair(true, atoi(prop));
812 return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
815 void ApacheRequestMapper::getAll(map<string,const char*>& properties) const
817 const ShibTargetApache* sta=reinterpret_cast<const ShibTargetApache*>(m_staKey->getData());
818 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
821 s->getAll(properties);
825 const char* auth_type=ap_auth_type(sta->m_req);
827 // Check for Basic Hijack
828 if (!strcasecmp(auth_type, "basic") && sta->m_dc->bBasicHijack == 1)
829 auth_type = "shibboleth";
830 properties["authType"] = auth_type;
833 if (sta->m_dc->szApplicationId)
834 properties["applicationId"] = sta->m_dc->szApplicationId;
835 if (sta->m_dc->szRequireWith)
836 properties["requireSessionWith"] = sta->m_dc->szRequireWith;
837 if (sta->m_dc->szRedirectToSSL)
838 properties["redirectToSSL"] = sta->m_dc->szRedirectToSSL;
839 if (sta->m_dc->bRequireSession != 0)
840 properties["requireSession"] = (sta->m_dc->bRequireSession==1) ? "true" : "false";
841 if (sta->m_dc->bExportAssertion != 0)
842 properties["exportAssertion"] = (sta->m_dc->bExportAssertion==1) ? "true" : "false";
845 const PropertySet* ApacheRequestMapper::getPropertySet(const char* name, const char* ns) const
847 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
848 return s ? s->getPropertySet(name,ns) : NULL;
851 const xercesc::DOMElement* ApacheRequestMapper::getElement() const
853 const PropertySet* s=reinterpret_cast<const PropertySet*>(m_propsKey->getData());
854 return s ? s->getElement() : NULL;
857 static SH_AP_TABLE* groups_for_user(request_rec* r, const char* user, char* grpfile)
860 SH_AP_TABLE* grps=ap_make_table(r->pool,15);
861 char l[MAX_STRING_LEN];
862 const char *group_name, *ll, *w;
864 #ifdef SHIB_APACHE_13
865 if (!(f=ap_pcfg_openfile(r->pool,grpfile))) {
867 if (ap_pcfg_openfile(&f,r->pool,grpfile) != APR_SUCCESS) {
869 ap_log_rerror(APLOG_MARK,APLOG_DEBUG,SH_AP_R(r),"groups_for_user() could not open group file: %s\n",grpfile);
874 #ifdef SHIB_APACHE_13
875 sp=ap_make_sub_pool(r->pool);
877 if (apr_pool_create(&sp,r->pool) != APR_SUCCESS) {
878 ap_log_rerror(APLOG_MARK,APLOG_ERR,0,r,
879 "groups_for_user() could not create a subpool");
884 while (!(ap_cfg_getline(l,MAX_STRING_LEN,f))) {
885 if ((*l=='#') || (!*l))
890 group_name=ap_getword(sp,&ll,':');
893 w=ap_getword_conf(sp,&ll);
894 if (!strcmp(w,user)) {
895 ap_table_setn(grps,ap_pstrdup(r->pool,group_name),"in");
905 bool htAccessControl::checkAttribute(const SPRequest& request, const Attribute* attr, const char* toMatch, RegularExpression* re) const
907 bool caseSensitive = attr->isCaseSensitive();
908 const vector<string>& vals = attr->getSerializedValues();
909 for (vector<string>::const_iterator v=vals.begin(); v!=vals.end(); ++v) {
911 auto_arrayptr<XMLCh> trans(fromUTF8(v->c_str()));
912 if (re->matches(trans.get())) {
913 request.log(SPRequest::SPDebug,
914 string("htAccessControl plugin expecting regexp ") + toMatch + ", got " + *v + ": authorization granted"
919 else if ((caseSensitive && *v == toMatch) || (!caseSensitive && !strcasecmp(v->c_str(), toMatch))) {
920 request.log(SPRequest::SPDebug,
921 string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization granted."
926 request.log(SPRequest::SPDebug,
927 string("htAccessControl plugin expecting ") + toMatch + ", got " + *v + ": authorization not granted."
934 AccessControl::aclresult_t htAccessControl::authorized(const SPRequest& request, const Session* session) const
936 // Make sure the object is our type.
937 const ShibTargetApache* sta=dynamic_cast<const ShibTargetApache*>(&request);
939 throw ConfigurationException("Request wrapper object was not of correct type.");
943 int m=sta->m_req->method_number;
944 bool method_restricted=false;
947 const array_header* reqs_arr=ap_requires(sta->m_req);
949 return shib_acl_indeterminate; // should never happen
951 require_line* reqs=(require_line*)reqs_arr->elts;
953 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE nelts: %d", reqs_arr->nelts);
954 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(sta->m_req),"REQUIRE all: %d", sta->m_dc->bRequireAll);
956 vector<bool> auth_OK(reqs_arr->nelts,false);
958 #define SHIB_AP_CHECK_IS_OK { \
959 if (sta->m_dc->bRequireAll < 1) \
960 return shib_acl_true; \
965 for (int x=0; x<reqs_arr->nelts; x++) {
967 if (!(reqs[x].method_mask & (1 << m)))
969 method_restricted=true;
970 string remote_user = request.getRemoteUser();
972 t = reqs[x].requirement;
973 w = ap_getword_white(sta->m_req->pool, &t);
975 if (!strcasecmp(w,"shibboleth")) {
976 // This is a dummy rule needed because Apache conflates authn and authz.
977 // Without some require rule, AuthType is ignored and no check_user hooks run.
980 else if (!strcmp(w,"valid-user")) {
982 request.log(SPRequest::SPDebug,"htAccessControl plugin accepting valid-user based on active session");
986 request.log(SPRequest::SPError,"htAccessControl plugin rejecting access for valid-user rule, no session is active");
989 else if (!strcmp(w,"user") && !remote_user.empty()) {
992 w=ap_getword_conf(sta->m_req->pool,&t);
1000 // To do regex matching, we have to convert from UTF-8.
1001 auto_arrayptr<XMLCh> trans(fromUTF8(w));
1002 RegularExpression re(trans.get());
1003 auto_arrayptr<XMLCh> trans2(fromUTF8(remote_user.c_str()));
1004 if (re.matches(trans2.get())) {
1005 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting user (") + w + ")");
1006 SHIB_AP_CHECK_IS_OK;
1009 catch (XMLException& ex) {
1010 auto_ptr_char tmp(ex.getMessage());
1011 request.log(SPRequest::SPError,
1012 string("htAccessControl plugin caught exception while parsing regular expression (") + w + "): " + tmp.get());
1015 else if (remote_user==w) {
1016 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting user (") + w + ")");
1017 SHIB_AP_CHECK_IS_OK;
1021 else if (!strcmp(w,"group")) {
1022 SH_AP_TABLE* grpstatus=NULL;
1023 if (sta->m_dc->szAuthGrpFile && !remote_user.empty()) {
1024 request.log(SPRequest::SPDebug,string("htAccessControl plugin using groups file: ") + sta->m_dc->szAuthGrpFile);
1025 grpstatus=groups_for_user(sta->m_req,remote_user.c_str(),sta->m_dc->szAuthGrpFile);
1031 w=ap_getword_conf(sta->m_req->pool,&t);
1032 if (ap_table_get(grpstatus,w)) {
1033 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting group (") + w + ")");
1034 SHIB_AP_CHECK_IS_OK;
1038 else if (!strcmp(w,"authnContextClassRef")) {
1039 const char* ref = session->getAuthnContextClassRef();
1041 w=ap_getword_conf(sta->m_req->pool,&t);
1042 if (!strcmp(w, ref)) {
1043 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting authnContextClassRef (") + w + ")");
1044 SHIB_AP_CHECK_IS_OK;
1048 else if (!strcmp(w,"authnContextDeclRef")) {
1049 const char* ref = session->getAuthnContextDeclRef();
1051 w=ap_getword_conf(sta->m_req->pool,&t);
1052 if (!strcmp(w, ref)) {
1053 request.log(SPRequest::SPDebug, string("htAccessControl plugin accepting authnContextDeclRef (") + w + ")");
1054 SHIB_AP_CHECK_IS_OK;
1059 // Map alias in rule to the attribute.
1061 request.log(SPRequest::SPError, "htAccessControl plugin not given a valid session to evaluate, are you using lazy sessions?");
1065 // Find the attribute(s) matching the require rule.
1066 pair<multimap<string,const Attribute*>::const_iterator,multimap<string,const Attribute*>::const_iterator> attrs =
1067 session->getIndexedAttributes().equal_range(w);
1068 if (attrs.first == attrs.second) {
1069 request.log(SPRequest::SPWarn, string("htAccessControl rule requires attribute (") + w + "), not found in session");
1075 while (!auth_OK[x] && *t) {
1076 w=ap_getword_conf(sta->m_req->pool,&t);
1083 auto_ptr<RegularExpression> re;
1085 delete re.release();
1086 auto_arrayptr<XMLCh> trans(fromUTF8(w));
1087 auto_ptr<xercesc::RegularExpression> temp(new xercesc::RegularExpression(trans.get()));
1091 for (; !auth_OK[x] && attrs.first!=attrs.second; ++attrs.first) {
1092 if (checkAttribute(request, attrs.first->second, w, regexp ? re.get() : NULL)) {
1093 SHIB_AP_CHECK_IS_OK;
1097 catch (XMLException& ex) {
1098 auto_ptr_char tmp(ex.getMessage());
1099 request.log(SPRequest::SPError,
1100 string("htAccessControl plugin caught exception while parsing regular expression (") + w + "): " + tmp.get()
1107 // If we get here, we either "failed" or we're in require all mode.
1108 bool auth_all_OK = true;
1109 for (int i= 0; i<reqs_arr->nelts; i++) {
1110 auth_all_OK &= auth_OK[i];
1112 if (auth_all_OK || !method_restricted)
1113 return shib_acl_true;
1115 return (sta->m_dc->bAuthoritative != 0) ? shib_acl_false : shib_acl_indeterminate;
1119 // Initial look at a request - create the per-request structure
1120 static int shib_post_read(request_rec *r)
1122 shib_request_config* rc = init_request_config(r);
1124 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_post_read");
1126 #ifdef SHIB_DEFERRED_HEADERS
1127 rc->hdr_out = ap_make_table(r->pool, 5);
1132 // fixups: set environment vars
1134 extern "C" int shib_fixups(request_rec* r)
1136 shib_request_config *rc = (shib_request_config*)ap_get_module_config(r->request_config, &mod_shib);
1137 shib_dir_config *dc = (shib_dir_config*)ap_get_module_config(r->per_dir_config, &mod_shib);
1138 if (dc->bOff==1 || dc->bUseEnvVars==0)
1141 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup(%d): ENTER", (int)getpid());
1143 if (rc==NULL || rc->env==NULL || ap_is_empty_table(rc->env))
1146 ap_log_rerror(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(r), "shib_fixup adding %d vars", ap_table_elts(rc->env)->nelts);
1147 r->subprocess_env = ap_overlay_tables(r->pool, r->subprocess_env, rc->env);
1152 #ifdef SHIB_APACHE_13
1155 * Cleanup the (per-process) pool info.
1157 extern "C" void shib_child_exit(server_rec* s, SH_AP_POOL* p)
1160 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit(%d) dealing with g_Config..", (int)getpid());
1163 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_exit() done");
1169 * Apache 2.x doesn't allow for per-child cleanup, causes CGI forks to hang.
1171 extern "C" apr_status_t shib_exit(void* data)
1177 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,0,NULL,"shib_exit() done");
1183 * shire_child_init()
1184 * Things to do when the child process is initialized.
1185 * (or after the configs are read in apache-2)
1187 #ifdef SHIB_APACHE_13
1188 extern "C" void shib_child_init(server_rec* s, SH_AP_POOL* p)
1190 extern "C" void shib_child_init(apr_pool_t* p, server_rec* s)
1193 // Initialize runtime components.
1195 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init(%d) starting", (int)getpid());
1198 ap_log_error(APLOG_MARK,APLOG_ERR|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() already initialized!");
1202 g_Config=&SPConfig::getConfig();
1203 g_Config->setFeatures(
1204 SPConfig::Listener |
1206 SPConfig::RequestMapping |
1207 SPConfig::InProcess |
1211 if (!g_Config->init(g_szSchemaDir)) {
1212 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to initialize libraries");
1215 g_Config->AccessControlManager.registerFactory(HT_ACCESS_CONTROL,&htAccessFactory);
1216 g_Config->RequestMapperManager.registerFactory(NATIVE_REQUEST_MAPPER,&ApacheRequestMapFactory);
1219 xercesc::DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();
1220 XercesJanitor<xercesc::DOMDocument> docjanitor(dummydoc);
1221 xercesc::DOMElement* dummy = dummydoc->createElementNS(NULL,path);
1222 auto_ptr_XMLCh src(g_szSHIBConfig);
1223 dummy->setAttributeNS(NULL,path,src.get());
1224 dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);
1226 g_Config->setServiceProvider(g_Config->ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));
1227 g_Config->getServiceProvider()->init();
1229 catch (exception& ex) {
1230 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),ex.what());
1231 ap_log_error(APLOG_MARK,APLOG_CRIT|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() failed to load configuration");
1235 ServiceProvider* sp=g_Config->getServiceProvider();
1236 xmltooling::Locker locker(sp);
1237 const PropertySet* props=sp->getPropertySet("Local");
1239 pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
1240 if (unsetValue.first)
1241 g_unsetHeaderValue = unsetValue.second;
1242 pair<bool,bool> flag=props->getBool("checkSpoofing");
1243 g_checkSpoofing = !flag.first || flag.second;
1244 flag=props->getBool("catchAll");
1245 g_catchAll = flag.first && flag.second;
1248 // Set the cleanup handler
1249 apr_pool_cleanup_register(p, NULL, &shib_exit, apr_pool_cleanup_null);
1251 ap_log_error(APLOG_MARK,APLOG_DEBUG|APLOG_NOERRNO,SH_AP_R(s),"shib_child_init() done");
1255 #ifdef SHIB_DEFERRED_HEADERS
1256 static void set_output_filter(request_rec *r)
1258 ap_add_output_filter("SHIB_HEADERS_OUT", NULL, r, r->connection);
1261 static void set_error_filter(request_rec *r)
1263 ap_add_output_filter("SHIB_HEADERS_ERR", NULL, r, r->connection);
1266 static int _table_add(void *v, const char *key, const char *value)
1268 apr_table_addn((apr_table_t*)v, key, value);
1272 static apr_status_t do_output_filter(ap_filter_t *f, apr_bucket_brigade *in)
1274 request_rec *r = f->r;
1275 shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
1278 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);
1279 apr_table_do(_table_add,r->headers_out, rc->hdr_out,NULL);
1280 // can't use overlap call because it will collapse Set-Cookie headers
1281 //apr_table_overlap(r->headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
1284 /* remove ourselves from the filter chain */
1285 ap_remove_output_filter(f);
1287 /* send the data up the stack */
1288 return ap_pass_brigade(f->next,in);
1291 static apr_status_t do_error_filter(ap_filter_t *f, apr_bucket_brigade *in)
1293 request_rec *r = f->r;
1294 shib_request_config *rc = (shib_request_config*) ap_get_module_config(r->request_config, &mod_shib);
1297 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);
1298 apr_table_do(_table_add,r->err_headers_out, rc->hdr_out,NULL);
1299 // can't use overlap call because it will collapse Set-Cookie headers
1300 //apr_table_overlap(r->err_headers_out, rc->hdr_out, APR_OVERLAP_TABLES_MERGE);
1303 /* remove ourselves from the filter chain */
1304 ap_remove_output_filter(f);
1306 /* send the data up the stack */
1307 return ap_pass_brigade(f->next,in);
1309 #endif // SHIB_DEFERRED_HEADERS
1311 typedef const char* (*config_fn_t)(void);
1313 #ifdef SHIB_APACHE_13
1315 // SHIB Module commands
1317 static command_rec shire_cmds[] = {
1318 {"ShibConfig", (config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
1319 RSRC_CONF, TAKE1, "Path to shibboleth.xml config file"},
1320 {"ShibCatalogs", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1321 RSRC_CONF, TAKE1, "Paths of XML schema catalogs"},
1322 {"ShibSchemaDir", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1323 RSRC_CONF, TAKE1, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"},
1325 {"ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
1326 (void *) XtOffsetOf (shib_server_config, szScheme),
1327 RSRC_CONF, TAKE1, "URL scheme to force into generated URLs for a vhost"},
1329 {"ShibRequestSetting", (config_fn_t)shib_table_set, NULL,
1330 OR_AUTHCFG, TAKE2, "Set arbitrary Shibboleth request property for content"},
1332 {"ShibDisable", (config_fn_t)ap_set_flag_slot,
1333 (void *) XtOffsetOf (shib_dir_config, bOff),
1334 OR_AUTHCFG, FLAG, "Disable all Shib module activity here to save processing effort"},
1335 {"ShibApplicationId", (config_fn_t)ap_set_string_slot,
1336 (void *) XtOffsetOf (shib_dir_config, szApplicationId),
1337 OR_AUTHCFG, TAKE1, "Set Shibboleth applicationId property for content"},
1338 {"ShibBasicHijack", (config_fn_t)ap_set_flag_slot,
1339 (void *) XtOffsetOf (shib_dir_config, bBasicHijack),
1340 OR_AUTHCFG, FLAG, "Respond to AuthType Basic and convert to shibboleth"},
1341 {"ShibRequireSession", (config_fn_t)ap_set_flag_slot,
1342 (void *) XtOffsetOf (shib_dir_config, bRequireSession),
1343 OR_AUTHCFG, FLAG, "Initiates a new session if one does not exist"},
1344 {"ShibRequireSessionWith", (config_fn_t)ap_set_string_slot,
1345 (void *) XtOffsetOf (shib_dir_config, szRequireWith),
1346 OR_AUTHCFG, TAKE1, "Initiates a new session if one does not exist using a specific SessionInitiator"},
1347 {"ShibExportAssertion", (config_fn_t)ap_set_flag_slot,
1348 (void *) XtOffsetOf (shib_dir_config, bExportAssertion),
1349 OR_AUTHCFG, FLAG, "Export SAML attribute assertion(s) to Shib-Attributes header"},
1350 {"ShibRedirectToSSL", (config_fn_t)ap_set_string_slot,
1351 (void *) XtOffsetOf (shib_dir_config, szRedirectToSSL),
1352 OR_AUTHCFG, TAKE1, "Redirect non-SSL requests to designated port" },
1353 {"AuthGroupFile", (config_fn_t)shib_ap_set_file_slot,
1354 (void *) XtOffsetOf (shib_dir_config, szAuthGrpFile),
1355 OR_AUTHCFG, TAKE1, "text file containing group names and member user IDs"},
1356 {"ShibRequireAll", (config_fn_t)ap_set_flag_slot,
1357 (void *) XtOffsetOf (shib_dir_config, bRequireAll),
1358 OR_AUTHCFG, FLAG, "All require directives must match"},
1359 {"AuthzShibAuthoritative", (config_fn_t)ap_set_flag_slot,
1360 (void *) XtOffsetOf (shib_dir_config, bAuthoritative),
1361 OR_AUTHCFG, FLAG, "Allow failed mod_shib htaccess authorization to fall through to other modules"},
1362 {"ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
1363 (void *) XtOffsetOf (shib_dir_config, bUseEnvVars),
1364 OR_AUTHCFG, FLAG, "Export attributes using environment variables (default)"},
1365 {"ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
1366 (void *) XtOffsetOf (shib_dir_config, bUseHeaders),
1367 OR_AUTHCFG, FLAG, "Export attributes using custom HTTP headers"},
1373 handler_rec shib_handlers[] = {
1374 { "shib-handler", shib_handler },
1378 module MODULE_VAR_EXPORT mod_shib = {
1379 STANDARD_MODULE_STUFF,
1380 NULL, /* initializer */
1381 create_shib_dir_config, /* dir config creater */
1382 merge_shib_dir_config, /* dir merger --- default is to override */
1383 create_shib_server_config, /* server config */
1384 merge_shib_server_config, /* merge server config */
1385 shire_cmds, /* command table */
1386 shib_handlers, /* handlers */
1387 NULL, /* filename translation */
1388 shib_check_user, /* check_user_id */
1389 shib_auth_checker, /* check auth */
1390 NULL, /* check access */
1391 NULL, /* type_checker */
1392 shib_fixups, /* fixups */
1394 NULL, /* header parser */
1395 shib_child_init, /* child_init */
1396 shib_child_exit, /* child_exit */
1397 shib_post_read /* post read-request */
1400 #elif defined(SHIB_APACHE_20) || defined(SHIB_APACHE_22)
1402 extern "C" void shib_register_hooks (apr_pool_t *p)
1404 #ifdef SHIB_DEFERRED_HEADERS
1405 ap_register_output_filter("SHIB_HEADERS_OUT", do_output_filter, NULL, AP_FTYPE_CONTENT_SET);
1406 ap_hook_insert_filter(set_output_filter, NULL, NULL, APR_HOOK_LAST);
1407 ap_register_output_filter("SHIB_HEADERS_ERR", do_error_filter, NULL, AP_FTYPE_CONTENT_SET);
1408 ap_hook_insert_error_filter(set_error_filter, NULL, NULL, APR_HOOK_LAST);
1409 ap_hook_post_read_request(shib_post_read, NULL, NULL, APR_HOOK_MIDDLE);
1411 ap_hook_child_init(shib_child_init, NULL, NULL, APR_HOOK_MIDDLE);
1412 ap_hook_check_user_id(shib_check_user, NULL, NULL, APR_HOOK_MIDDLE);
1413 ap_hook_auth_checker(shib_auth_checker, NULL, NULL, APR_HOOK_FIRST);
1414 ap_hook_handler(shib_handler, NULL, NULL, APR_HOOK_LAST);
1415 ap_hook_fixups(shib_fixups, NULL, NULL, APR_HOOK_MIDDLE);
1418 // SHIB Module commands
1421 static command_rec shib_cmds[] = {
1422 AP_INIT_TAKE1("ShibConfig", (config_fn_t)ap_set_global_string_slot, &g_szSHIBConfig,
1423 RSRC_CONF, "Path to shibboleth.xml config file"),
1424 AP_INIT_TAKE1("ShibCatalogs", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1425 RSRC_CONF, "Paths of XML schema catalogs"),
1426 AP_INIT_TAKE1("ShibSchemaDir", (config_fn_t)ap_set_global_string_slot, &g_szSchemaDir,
1427 RSRC_CONF, "Paths of XML schema catalogs (deprecated in favor of ShibCatalogs)"),
1429 AP_INIT_TAKE1("ShibURLScheme", (config_fn_t)shib_set_server_string_slot,
1430 (void *) offsetof (shib_server_config, szScheme),
1431 RSRC_CONF, "URL scheme to force into generated URLs for a vhost"),
1433 AP_INIT_TAKE2("ShibRequestSetting", (config_fn_t)shib_table_set, NULL,
1434 OR_AUTHCFG, "Set arbitrary Shibboleth request property for content"),
1436 AP_INIT_FLAG("ShibDisable", (config_fn_t)ap_set_flag_slot,
1437 (void *) offsetof (shib_dir_config, bOff),
1438 OR_AUTHCFG, "Disable all Shib module activity here to save processing effort"),
1439 AP_INIT_TAKE1("ShibApplicationId", (config_fn_t)ap_set_string_slot,
1440 (void *) offsetof (shib_dir_config, szApplicationId),
1441 OR_AUTHCFG, "Set Shibboleth applicationId property for content"),
1442 AP_INIT_FLAG("ShibBasicHijack", (config_fn_t)ap_set_flag_slot,
1443 (void *) offsetof (shib_dir_config, bBasicHijack),
1444 OR_AUTHCFG, "Respond to AuthType Basic and convert to shibboleth"),
1445 AP_INIT_FLAG("ShibRequireSession", (config_fn_t)ap_set_flag_slot,
1446 (void *) offsetof (shib_dir_config, bRequireSession),
1447 OR_AUTHCFG, "Initiates a new session if one does not exist"),
1448 AP_INIT_TAKE1("ShibRequireSessionWith", (config_fn_t)ap_set_string_slot,
1449 (void *) offsetof (shib_dir_config, szRequireWith),
1450 OR_AUTHCFG, "Initiates a new session if one does not exist using a specific SessionInitiator"),
1451 AP_INIT_FLAG("ShibExportAssertion", (config_fn_t)ap_set_flag_slot,
1452 (void *) offsetof (shib_dir_config, bExportAssertion),
1453 OR_AUTHCFG, "Export SAML attribute assertion(s) to Shib-Attributes header"),
1454 AP_INIT_TAKE1("ShibRedirectToSSL", (config_fn_t)ap_set_string_slot,
1455 (void *) offsetof (shib_dir_config, szRedirectToSSL),
1456 OR_AUTHCFG, "Redirect non-SSL requests to designated port"),
1457 AP_INIT_TAKE1("AuthGroupFile", (config_fn_t)shib_ap_set_file_slot,
1458 (void *) offsetof (shib_dir_config, szAuthGrpFile),
1459 OR_AUTHCFG, "Text file containing group names and member user IDs"),
1460 AP_INIT_FLAG("ShibRequireAll", (config_fn_t)ap_set_flag_slot,
1461 (void *) offsetof (shib_dir_config, bRequireAll),
1462 OR_AUTHCFG, "All require directives must match"),
1463 AP_INIT_FLAG("AuthzShibAuthoritative", (config_fn_t)ap_set_flag_slot,
1464 (void *) offsetof (shib_dir_config, bAuthoritative),
1465 OR_AUTHCFG, "Allow failed mod_shib htaccess authorization to fall through to other modules"),
1466 AP_INIT_FLAG("ShibUseEnvironment", (config_fn_t)ap_set_flag_slot,
1467 (void *) offsetof (shib_dir_config, bUseEnvVars),
1468 OR_AUTHCFG, "Export attributes using environment variables (default)"),
1469 AP_INIT_FLAG("ShibUseHeaders", (config_fn_t)ap_set_flag_slot,
1470 (void *) offsetof (shib_dir_config, bUseHeaders),
1471 OR_AUTHCFG, "Export attributes using custom HTTP headers"),
1476 module AP_MODULE_DECLARE_DATA mod_shib = {
1477 STANDARD20_MODULE_STUFF,
1478 create_shib_dir_config, /* create dir config */
1479 merge_shib_dir_config, /* merge dir config --- default is to override */
1480 create_shib_server_config, /* create server config */
1481 merge_shib_server_config, /* merge server config */
1482 shib_cmds, /* command table */
1483 shib_register_hooks /* register hooks */
1487 #error "unsupported Apache version"