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 #include "config_win32.h"
26 #include <saml/saml.h>
27 #include <shib/shib.h>
28 #include <shib/shib-threads.h>
29 #include <shib-target/shib-target.h>
53 using namespace shibboleth;
54 using namespace shibtarget;
56 // macros to output text to client
57 #define NET_WRITE(str) \
58 if (IO_ERROR==net_write(sn->csd,str,strlen(str))) return REQ_EXIT
61 ShibTargetConfig* g_Config=NULL;
63 string g_ServerScheme;
66 PlugManager::Factory SunRequestMapFactory;
68 extern "C" NSAPI_PUBLIC void nsapi_shib_exit(void*)
75 extern "C" NSAPI_PUBLIC int nsapi_shib_init(pblock* pb, Session* sn, Request* rq)
77 // Save off a default hostname for this virtual server.
78 char* name=pblock_findval("server-name",pb);
92 pblock_nvinsert("error","unable to determine web server hostname",pb);
97 name=pblock_findval("server-scheme",pb);
101 log_error(LOG_INFORM,"nsapi_shib_init",sn,rq,"nsapi_shib loaded for host (%s)",g_ServerName.c_str());
106 const char* schemadir=pblock_findval("shib-schemas",pb);
108 schemadir=getenv("SHIBSCHEMAS");
110 schemadir=SHIB_SCHEMAS;
111 const char* config=pblock_findval("shib-config",pb);
113 config=getenv("SHIBCONFIG");
116 g_Config=&ShibTargetConfig::getConfig();
117 g_Config->setFeatures(
118 ShibTargetConfig::Listener |
119 ShibTargetConfig::Metadata |
120 ShibTargetConfig::AAP |
121 ShibTargetConfig::RequestMapper |
122 ShibTargetConfig::LocalExtensions |
123 ShibTargetConfig::Logging
125 if (!g_Config->init(schemadir)) {
127 pblock_nvinsert("error","unable to initialize Shibboleth libraries",pb);
131 SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::NativeRequestMapType,&SunRequestMapFactory);
132 // We hijack the legacy type so that 1.2 config files will load this plugin
133 SAMLConfig::getConfig().getPlugMgr().regFactory(shibtarget::XML::LegacyRequestMapType,&SunRequestMapFactory);
135 if (!g_Config->load(config)) {
137 pblock_nvinsert("error","unable to initialize load Shibboleth configuration",pb);
141 daemon_atrestart(nsapi_shib_exit,NULL);
146 pblock_nvinsert("error","caught exception, unable to initialize Shibboleth libraries",pb);
153 /********************************************************************************/
154 // NSAPI Shib Target Subclass
156 class ShibTargetNSAPI : public ShibTarget
159 ShibTargetNSAPI(pblock* pb, Session* sn, Request* rq) {
164 // Get everything but hostname...
165 const char* uri=pblock_findval("uri", rq->reqpb);
166 const char* qstr=pblock_findval("query", rq->reqpb);
167 int port=server_portnum;
168 const char* scheme=security_active ? "https" : "http";
169 const char* host=NULL;
175 url=url + '?' + qstr;
177 #ifdef vs_is_default_vs
178 // This is 6.0 or later, so we can distinguish requests to name-based vhosts.
179 if (!vs_is_default_vs)
180 // The beauty here is, a non-default vhost can *only* be accessed if the client
181 // specified the exact name in the Host header. So we can trust the Host header.
182 host=pblock_findval("host", rq->headers);
185 // In other cases, we're going to rely on the initialization process...
186 host=g_ServerName.c_str();
188 char* content_type = "";
189 request_header("content-type", &content_type, sn, rq);
191 const char *remote_ip = pblock_findval("ip", sn->client);
192 const char *method = pblock_findval("method", rq->reqpb);
194 init(scheme, host, port, url.c_str(), content_type, remote_ip, method);
196 ~ShibTargetNSAPI() {}
198 virtual void log(ShibLogLevel level, const string &msg) {
199 ShibTarget::log(level,msg);
200 if (level==LogLevelError)
201 log_error(LOG_FAILURE, "nsapi_shib", m_sn, m_rq, const_cast<char*>(msg.c_str()));
203 virtual string getCookies(void) const {
204 char *cookies = NULL;
205 if (request_header("cookie", &cookies, m_sn, m_rq) == REQ_ABORTED)
206 throw("error accessing cookie header");
207 return string(cookies ? cookies : "");
209 virtual void setCookie(const string &name, const string &value) {
210 string cookie = name + '=' + value;
211 pblock_nvinsert("Set-Cookie", cookie.c_str(), m_rq->srvhdrs);
213 virtual string getArgs(void) {
214 const char *q = pblock_findval("query", m_rq->reqpb);
215 return string(q ? q : "");
217 virtual string getPostData(void) {
218 char* content_length=NULL;
219 if (request_header("content-length", &content_length, m_sn, m_rq)!=REQ_PROCEED ||
220 atoi(content_length) > 1024*1024) // 1MB?
221 throw FatalProfileException("Blocked too-large a submission to profile endpoint.");
224 int cl=atoi(content_length);
226 while (cl && ch != IO_EOF) {
227 ch=netbuf_getc(m_sn->inbuf);
236 throw FatalProfileException("Error reading profile submission from browser.");
240 virtual void clearHeader(const string &name) {
241 if (name=="REMOTE_USER") {
242 param_free(pblock_remove("auth-user",m_rq->vars));
243 param_free(pblock_remove("remote-user",m_rq->headers));
246 param_free(pblock_remove(name.c_str(), m_rq->headers));
248 virtual void setHeader(const string &name, const string &value) {
249 pblock_nvinsert(name.c_str(), value.c_str() ,m_rq->headers);
251 virtual string getHeader(const string &name) {
253 if (request_header(const_cast<char*>(name.c_str()), &hdr, m_sn, m_rq) != REQ_PROCEED)
255 return string(hdr ? hdr : "");
257 virtual void setRemoteUser(const string &user) {
258 pblock_nvinsert("remote-user", user.c_str(), m_rq->headers);
259 pblock_nvinsert("auth-user", user.c_str(), m_rq->vars);
261 virtual string getRemoteUser(void) {
262 return getHeader("remote-user");
265 virtual void* sendPage(
268 const string& content_type="text/html",
269 const saml::Iterator<header_t>& headers=EMPTY(header_t)
271 param_free(pblock_remove("content-type", m_rq->srvhdrs));
272 pblock_nvinsert("content-type", content_type.c_str(), m_rq->srvhdrs);
273 pblock_nninsert("content-length", msg.length(), m_rq->srvhdrs);
274 pblock_nvinsert("connection","close",m_rq->srvhdrs);
275 while (headers.hasNext()) {
276 const header_t& h=headers.next();
277 pblock_nvinsert(h.first.c_str(), h.second.c_str(), m_rq->srvhdrs);
279 protocol_status(m_sn, m_rq, code, NULL);
280 protocol_start_response(m_sn, m_rq);
281 net_write(m_sn->csd,const_cast<char*>(msg.c_str()),msg.length());
282 return (void*)REQ_EXIT;
284 virtual void* sendRedirect(const string& url) {
285 param_free(pblock_remove("content-type", m_rq->srvhdrs));
286 pblock_nninsert("content-length", 0, m_rq->srvhdrs);
287 pblock_nvinsert("expires", "01-Jan-1997 12:00:00 GMT", m_rq->srvhdrs);
288 pblock_nvinsert("cache-control", "private,no-store,no-cache", m_rq->srvhdrs);
289 pblock_nvinsert("location", url.c_str(), m_rq->srvhdrs);
290 protocol_status(m_sn, m_rq, PROTOCOL_REDIRECT, NULL);
291 protocol_start_response(m_sn, m_rq);
292 return (void*)REQ_ABORTED;
294 virtual void* returnDecline(void) { return (void*)REQ_NOACTION; }
295 virtual void* returnOK(void) { return (void*)REQ_PROCEED; }
302 /********************************************************************************/
304 int WriteClientError(Session* sn, Request* rq, char* func, char* msg)
306 log_error(LOG_FAILURE,func,sn,rq,msg);
307 protocol_status(sn,rq,PROTOCOL_SERVER_ERROR,msg);
312 #define FUNC "shibboleth"
313 extern "C" NSAPI_PUBLIC int nsapi_shib(pblock* pb, Session* sn, Request* rq)
315 ostringstream threadid;
316 threadid << "[" << getpid() << "] nsapi_shib" << '\0';
317 saml::NDC ndc(threadid.str().c_str());
320 ShibTargetNSAPI stn(pb, sn, rq);
322 // Check user authentication
323 pair<bool,void*> res = stn.doCheckAuthN();
324 if (res.first) return (int)res.second;
326 // user authN was okay -- export the assertions now
327 param_free(pblock_remove("auth-user",rq->vars));
328 // This seems to be required in order to eventually set
329 // the auth-user var.
330 pblock_nvinsert("auth-type","shibboleth",rq->vars);
331 res = stn.doExportAssertions();
332 if (res.first) return (int)res.second;
334 // Check the Authorization
335 res = stn.doCheckAuthZ();
336 if (res.first) return (int)res.second;
341 catch (SAMLException& e) {
342 log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
343 return WriteClientError(sn, rq, FUNC, "Shibboleth filter threw an exception, see web server log for error.");
347 return WriteClientError(sn, rq, FUNC, "Shibboleth filter threw an uncaught exception.");
354 #define FUNC "shib_handler"
355 extern "C" NSAPI_PUBLIC int shib_handler(pblock* pb, Session* sn, Request* rq)
357 ostringstream threadid;
358 threadid << "[" << getpid() << "] shib_handler" << '\0';
359 saml::NDC ndc(threadid.str().c_str());
362 ShibTargetNSAPI stn(pb, sn, rq);
364 pair<bool,void*> res = stn.doHandler();
365 if (res.first) return (int)res.second;
367 return WriteClientError(sn, rq, FUNC, "Shibboleth handler did not do anything.");
369 catch (SAMLException& e) {
370 log_error(LOG_FAILURE,FUNC,sn,rq,const_cast<char*>(e.what()));
371 return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an exception, see web server log for error.");
375 return WriteClientError(sn, rq, FUNC, "Shibboleth handler threw an unknown exception.");
381 class SunRequestMapper : public virtual IRequestMapper, public virtual IPropertySet
384 SunRequestMapper(const DOMElement* e);
385 ~SunRequestMapper() { delete m_mapper; delete m_stKey; delete m_propsKey; }
386 void lock() { m_mapper->lock(); }
387 void unlock() { m_stKey->setData(NULL); m_propsKey->setData(NULL); m_mapper->unlock(); }
388 Settings getSettings(ShibTarget* st) const;
390 pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
391 pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
392 pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
393 pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
394 pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
395 const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
396 const DOMElement* getElement() const;
399 IRequestMapper* m_mapper;
401 ThreadKey* m_propsKey;
404 IPlugIn* SunRequestMapFactory(const DOMElement* e)
406 return new SunRequestMapper(e);
409 SunRequestMapper::SunRequestMapper(const DOMElement* e) : m_mapper(NULL), m_stKey(NULL), m_propsKey(NULL)
411 IPlugIn* p=SAMLConfig::getConfig().getPlugMgr().newPlugin(shibtarget::XML::XMLRequestMapType,e);
412 m_mapper=dynamic_cast<IRequestMapper*>(p);
415 throw UnsupportedExtensionException("Embedded request mapper plugin was not of correct type.");
417 m_stKey=ThreadKey::create(NULL);
418 m_propsKey=ThreadKey::create(NULL);
421 IRequestMapper::Settings SunRequestMapper::getSettings(ShibTarget* st) const
423 Settings s=m_mapper->getSettings(st);
424 m_stKey->setData(dynamic_cast<ShibTargetNSAPI*>(st));
425 m_propsKey->setData((void*)s.first);
426 return pair<const IPropertySet*,IAccessControl*>(this,s.second);
429 pair<bool,bool> SunRequestMapper::getBool(const char* name, const char* ns) const
431 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
432 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
433 if (stn && !ns && name) {
434 // Override boolean properties.
435 const char* param=pblock_findval(name,stn->m_pb);
436 if (param && (!strcmp(param,"1") || !strcasecmp(param,"true")))
437 return make_pair(true,true);
439 return s ? s->getBool(name,ns) : make_pair(false,false);
442 pair<bool,const char*> SunRequestMapper::getString(const char* name, const char* ns) const
444 ShibTargetNSAPI* stn=reinterpret_cast<ShibTargetNSAPI*>(m_stKey->getData());
445 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
446 if (stn && !ns && name) {
447 // Override string properties.
448 if (!strcmp(name,"authType"))
449 return pair<bool,const char*>(true,"shibboleth");
451 const char* param=pblock_findval(name,stn->m_pb);
453 return make_pair(true,param);
456 return s ? s->getString(name,ns) : pair<bool,const char*>(false,NULL);
459 pair<bool,const XMLCh*> SunRequestMapper::getXMLString(const char* name, const char* ns) const
461 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
462 return s ? s->getXMLString(name,ns) : pair<bool,const XMLCh*>(false,NULL);
465 pair<bool,unsigned int> SunRequestMapper::getUnsignedInt(const char* name, const char* ns) const
467 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
468 return s ? s->getUnsignedInt(name,ns) : pair<bool,unsigned int>(false,0);
471 pair<bool,int> SunRequestMapper::getInt(const char* name, const char* ns) const
473 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
474 return s ? s->getInt(name,ns) : pair<bool,int>(false,0);
477 const IPropertySet* SunRequestMapper::getPropertySet(const char* name, const char* ns) const
479 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
480 return s ? s->getPropertySet(name,ns) : NULL;
483 const DOMElement* SunRequestMapper::getElement() const
485 const IPropertySet* s=reinterpret_cast<const IPropertySet*>(m_propsKey->getData());
486 return s ? s->getElement() : NULL;