2 * Copyright 2001-2005 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.
17 /* nsapi_shib.cpp - Shibboleth NSAPI filter
23 #if defined (_MSC_VER) || defined(__BORLANDC__)
24 # include "config_win32.h"
31 #include <saml/saml.h>
32 #include <shib/shib.h>
33 #include <shib/shib-threads.h>
34 #include <shib-target/shib-target.h>
58 using namespace shibboleth;
59 using namespace shibtarget;
61 // macros to output text to client
62 #define NET_WRITE(str) \
63 if (IO_ERROR==net_write(sn->csd,str,strlen(str))) return REQ_EXIT
66 ShibTargetConfig* g_Config=NULL;
68 string g_ServerScheme;
69 string g_unsetHeaderValue;
72 PlugManager::Factory SunRequestMapFactory;
74 extern "C" NSAPI_PUBLIC void nsapi_shib_exit(void*)
81 extern "C" NSAPI_PUBLIC int nsapi_shib_init(pblock* pb, Session* sn, Request* rq)
83 // Save off a default hostname for this virtual server.
84 char* name=pblock_findval("server-name",pb);
98 pblock_nvinsert("error","unable to determine web server hostname",pb);
103 name=pblock_findval("server-scheme",pb);
107 log_error(LOG_INFORM,"nsapi_shib_init",sn,rq,"nsapi_shib loaded for host (%s)",g_ServerName.c_str());
112 const char* schemadir=pblock_findval("shib-schemas",pb);
114 schemadir=getenv("SHIBSCHEMAS");
116 schemadir=SHIB_SCHEMAS;
117 const char* config=pblock_findval("shib-config",pb);
119 config=getenv("SHIBCONFIG");
122 g_Config=&ShibTargetConfig::getConfig();
123 g_Config->setFeatures(
124 ShibTargetConfig::Listener |
125 ShibTargetConfig::Metadata |
126 ShibTargetConfig::AAP |
127 ShibTargetConfig::RequestMapper |
128 ShibTargetConfig::LocalExtensions |
129 ShibTargetConfig::Logging
131 if (!g_Config->init(schemadir)) {
133 pblock_nvinsert("error","unable to initialize Shibboleth libraries",pb);
137 SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&SunRequestMapFactory);
138 // We hijack the legacy type so that 1.2 config files will load this plugin
139 SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&SunRequestMapFactory);
141 if (!g_Config->load(config)) {
143 pblock_nvinsert("error","unable to initialize load Shibboleth configuration",pb);
147 daemon_atrestart(nsapi_shib_exit,NULL);
149 IConfig* conf=g_Config->getINI();
151 const IPropertySet* props=conf->getPropertySet("Local");
153 pair<bool,const char*> unsetValue=props->getString("unsetHeaderValue");
154 if (unsetValue.first)
155 g_unsetHeaderValue = unsetValue.second;
161 pblock_nvinsert("error","caught exception, unable to initialize Shibboleth libraries",pb);
168 /********************************************************************************/
169 // NSAPI Shib Target Subclass
171 class ShibTargetNSAPI : public ShibTarget
174 ShibTargetNSAPI(pblock* pb, Session* sn, Request* rq) {
179 // Get everything but hostname...
180 const char* uri=pblock_findval("uri", rq->reqpb);
181 const char* qstr=pblock_findval("query", rq->reqpb);
182 int port=server_portnum;
183 const char* scheme=security_active ? "https" : "http";
184 const char* host=NULL;
190 url=url + '?' + qstr;
192 #ifdef vs_is_default_vs
193 // This is 6.0 or later, so we can distinguish requests to name-based vhosts.
194 if (!vs_is_default_vs)
195 // The beauty here is, a non-default vhost can *only* be accessed if the client
196 // specified the exact name in the Host header. So we can trust the Host header.
197 host=pblock_findval("host", rq->headers);
200 // In other cases, we're going to rely on the initialization process...
201 host=g_ServerName.c_str();
203 char* content_type = "";
204 request_header("content-type", &content_type, sn, rq);
206 const char *remote_ip = pblock_findval("ip", sn->client);
207 const char *method = pblock_findval("method", rq->reqpb);
209 init(scheme, host, port, url.c_str(), content_type, remote_ip, method);
211 ~ShibTargetNSAPI() {}
213 virtual void log(ShibLogLevel level, const string &msg) {
214 ShibTarget::log(level,msg);
215 if (level==LogLevelError)
216 log_error(LOG_FAILURE, "nsapi_shib", m_sn, m_rq, const_cast<char*>(msg.c_str()));
218 virtual string getCookies(void) const {
219 char *cookies = NULL;
220 if (request_header("cookie", &cookies, m_sn, m_rq) == REQ_ABORTED)
221 throw("error accessing cookie header");
222 return string(cookies ? cookies : "");
224 virtual void setCookie(const string &name, const string &value) {
225 string cookie = name + '=' + value;
226 pblock_nvinsert("Set-Cookie", cookie.c_str(), m_rq->srvhdrs);
228 virtual string getArgs(void) {
229 const char *q = pblock_findval("query", m_rq->reqpb);
230 return string(q ? q : "");
232 virtual string getPostData(void) {
233 char* content_length=NULL;
234 if (request_header("content-length", &content_length, m_sn, m_rq)!=REQ_PROCEED ||
235 atoi(content_length) > 1024*1024) // 1MB?
236 throw FatalProfileException("Blocked too-large a submission to profile endpoint.");
239 int cl=atoi(content_length);
241 while (cl && ch != IO_EOF) {
242 ch=netbuf_getc(m_sn->inbuf);
251 throw FatalProfileException("Error reading profile submission from browser.");
255 virtual void clearHeader(const string &name) {
256 if (name=="REMOTE_USER") {
257 param_free(pblock_remove("auth-user",m_rq->vars));
258 param_free(pblock_remove("remote-user",m_rq->headers));
261 param_free(pblock_remove(name.c_str(), m_rq->headers));
262 pblock_nvinsert(name.c_str(), g_unsetHeaderValue.c_str() ,m_rq->headers);
265 virtual void setHeader(const string &name, const string &value) {
266 param_free(pblock_remove(name.c_str(), m_rq->headers));
267 pblock_nvinsert(name.c_str(), value.c_str() ,m_rq->headers);
269 virtual string getHeader(const string &name) {
271 if (request_header(const_cast<char*>(name.c_str()), &hdr, m_sn, m_rq) != REQ_PROCEED)
273 return string(hdr ? hdr : "");
275 virtual void setRemoteUser(const string &user) {
276 pblock_nvinsert("remote-user", user.c_str(), m_rq->headers);
277 pblock_nvinsert("auth-user", user.c_str(), m_rq->vars);
279 virtual string getRemoteUser(void) {
280 return getHeader("remote-user");
283 virtual void* sendPage(
286 const string& content_type="text/html",
287 const saml::Iterator<header_t>& headers=EMPTY(header_t)
289 param_free(pblock_remove("content-type", m_rq->srvhdrs));
290 pblock_nvinsert("content-type", content_type.c_str(), m_rq->srvhdrs);
291 pblock_nninsert("content-length", msg.length(), m_rq->srvhdrs);
292 pblock_nvinsert("connection","close",m_rq->srvhdrs);
293 while (headers.hasNext()) {
294 const header_t& h=headers.next();
295 pblock_nvinsert(h.first.c_str(), h.second.c_str(), m_rq->srvhdrs);
297 protocol_status(m_sn, m_rq, code, NULL);
298 protocol_start_response(m_sn, m_rq);
299 net_write(m_sn->csd,const_cast<char*>(msg.c_str()),msg.length());
300 return (void*)REQ_EXIT;
302 virtual void* sendRedirect(const string& url) {
303 param_free(pblock_remove("content-type", m_rq->srvhdrs));
304 pblock_nninsert("content-length", 0, m_rq->srvhdrs);
305 pblock_nvinsert("expires", "01-Jan-1997 12:00:00 GMT", m_rq->srvhdrs);
306 pblock_nvinsert("cache-control", "private,no-store,no-cache", m_rq->srvhdrs);
307 pblock_nvinsert("location", url.c_str(), m_rq->srvhdrs);
308 pblock_nvinsert("connection","close",m_rq->srvhdrs);
309 protocol_status(m_sn, m_rq, PROTOCOL_REDIRECT, NULL);
310 protocol_start_response(m_sn, m_rq);
311 return (void*)REQ_ABORTED;
313 virtual void* returnDecline(void) { return (void*)REQ_NOACTION; }
314 virtual void* returnOK(void) { return (void*)REQ_PROCEED; }
321 /********************************************************************************/
323 int WriteClientError(Session* sn, Request* rq, char* func, char* msg)
325 log_error(LOG_FAILURE,func,sn,rq,msg);
326 protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,msg);
331 #define FUNC "shibboleth"
332 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, Session* sn, Request* rq)
334 ostringstream threadid;
335 threadid << "[" << getpid() << "] nsapi_shib" << '\0';
336 saml::NDC ndc(threadid.str().c_str());
339 ShibTargetNSAPI stn(pb, sn, rq);
341 // Check user authentication
342 pair<bool,void*> res = stn.doCheckAuthN();
343 if (res.first) return (int)res.second;
345 // user authN was okay -- export the assertions now
346 param_free(pblock_remove("auth-user",rq->vars));
347 // This seems to be required in order to eventually set
348 // the auth-user var.
349 pblock_nvinsert("auth-type","shibboleth",rq->vars);
350 res = stn.doExportAssertions();
351 if (res.first) return (int)res.second;
353 // Check the Authorization
354 res = stn.doCheckAuthZ();
355 if (res.first) return (int)res.second;
360 catch (SAMLException& e) {
361 log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
362 return WriteClientError(sn, rq, FUNC, "Shibboleth filter threw an exception, see web server log for error.");
366 return WriteClientError(sn, rq, FUNC, "Shibboleth filter threw an uncaught exception.");
373 #define FUNC "shib_handler"
374 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, Session* sn, Request* rq)
376 ostringstream threadid;
377 threadid << "[" << getpid() << "] shib_handler" << '\0';
378 saml::NDC ndc(threadid.str().c_str());
381 ShibTargetNSAPI stn(pb, sn, rq);
383 pair<bool,void*> res = stn.doHandler();
384 if (res.first) return (int)res.second;
386 return WriteClientError(sn, rq, FUNC, "Shibboleth handler did not do anything.");
388 catch (SAMLException& e) {
389 log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
390 return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an exception, see web server log for error.");
394 return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an unknown exception.");
400 class SunRequestMapper : public virtual IRequestMapper, public virtual IPropertySet
403 SunRequestMapper(const DOMElement* e);
404 ~SunRequestMapper() { delete m_mapper; delete m_stKey; delete m_propsKey; }
405 void lock() { m_mapper->lock(); }
406 void unlock() { m_stKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
407 Settings getSettings(ShibTarget* st) const;
409 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
410 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
411 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
412 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
413 pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
414 const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
415 const DOMElement* getElement() const;
418 IRequestMapper* m_mapper;
420 ThreadKey* m_propsKey;
423 IPlugIn* SunRequestMapFactory(const DOMElement* e)
425 return new SunRequestMapper(e);
428 SunRequestMapper::SunRequestMapper(const DOMElement* e) : m_mapper(NULL), m_stKey(NULL), m_propsKey(NULL)
430 IPlugIn* p=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::XMLRequestMapType,e);
431 m_mapper=dynamic_cast<IRequestMapper*>(p);
434 throw UnsupportedExtensionException("Embedded request mapper plugin was not of correct type.");
436 m_stKey=ThreadKey::create(NULL);
437 m_propsKey=ThreadKey::create(NULL);
440 IRequestMapper::Settings SunRequestMapper::getSettings(ShibTarget* st) const
442 Settings s=m_mapper->getSettings(st);
443 m_stKey->setData(dynamic_cast<ShibTargetNSAPI*>(st));
444 m_propsKey->setData((void*)s.first);
445 return pair<const IPropertySet*,IAccessControl*>(this,s.second);
448 pair<bool,bool> SunRequestMapper::getBool(const char* name, const char* ns) const
450 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
451 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
452 if (stn && !ns && name) {
453 // Override boolean properties.
454 const char* param=pblock_findval(name,stn->m_pb);
455 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
456 return make_pair(true,true);
458 return s ? s->getBool(name,ns) : make_pair(false,false);
461 pair<bool,const char*> SunRequestMapper::getString(const char* name, const char* ns) const
463 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
464 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
465 if (stn && !ns && name) {
466 // Override string properties.
467 if (!strcmp(name,"authType"))
468 return pair<bool,const char*>(true,"shibboleth");
470 const char* param=pblock_findval(name,stn->m_pb);
472 return make_pair(true,param);
475 return s ? s->getString(name,ns) : pair<bool,const char*>(false,NULL);
478 pair<bool,const XMLCh*> SunRequestMapper::getXMLString(const char* name, const char* ns) const
480 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
481 return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,NULL);
484 pair<bool,unsigned int> SunRequestMapper::getUnsignedInt(const char* name, const char* ns) const
486 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
487 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
488 if (stn && !ns && name) {
489 // Override int properties.
490 const char* param=pblock_findval(name,stn->m_pb);
492 return pair<bool,unsigned int>(true,strtol(param,NULL,10));
494 return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
497 pair<bool,int> SunRequestMapper::getInt(const char* name, const char* ns) const
499 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
500 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
501 if (stn && !ns && name) {
502 // Override int properties.
503 const char* param=pblock_findval(name,stn->m_pb);
505 return pair<bool,int>(true,atoi(param));
507 return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
510 const IPropertySet* SunRequestMapper::getPropertySet(const char* name, const char* ns) const
512 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
513 return s ? s->getPropertySet(name,ns) : NULL;
516 const DOMElement* SunRequestMapper::getElement() const
518 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
519 return s ? s->getElement() : NULL;