2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * AbstractSPRequest.cpp
24 * Abstract base for SPRequest implementations.
28 #include "exceptions.h"
29 #include "AbstractSPRequest.h"
30 #include "Application.h"
31 #include "GSSRequest.h"
32 #include "ServiceProvider.h"
33 #include "SessionCache.h"
34 #include "util/CGIParser.h"
36 #include <boost/lexical_cast.hpp>
38 using namespace shibsp;
39 using namespace opensaml;
40 using namespace xmltooling;
43 SPRequest::SPRequest()
47 SPRequest::~SPRequest()
51 string SPRequest::getSecureHeader(const char* name) const
53 return getHeader(name);
56 void SPRequest::setAuthType(const char* authtype)
60 #ifdef SHIBSP_HAVE_GSSAPI
61 GSSRequest::GSSRequest()
65 GSSRequest::~GSSRequest()
70 AbstractSPRequest::AbstractSPRequest(const char* category)
71 : m_sp(SPConfig::getConfig().getServiceProvider()),
72 m_mapper(nullptr), m_app(nullptr), m_sessionTried(false), m_session(nullptr),
73 m_log(&Category::getInstance(category))
78 AbstractSPRequest::~AbstractSPRequest()
88 const ServiceProvider& AbstractSPRequest::getServiceProvider() const
93 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
96 // Map request to application and content settings.
97 m_mapper = m_sp->getRequestMapper();
99 m_settings = m_mapper->getSettings(*this);
101 if (reinterpret_cast<Category*>(m_log)->isDebugEnabled()) {
102 reinterpret_cast<Category*>(m_log)->debug(
103 "mapped %s to %s", getRequestURL(), m_settings.first->getString("applicationId").second
110 const Application& AbstractSPRequest::getApplication() const
113 // Now find the application from the URL settings
114 m_app = m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
116 throw ConfigurationException("Unable to map non-default applicationId to an ApplicationOverride, check configuration.");
121 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache)
123 // Only attempt this once.
124 if (cache && m_sessionTried)
127 m_sessionTried = true;
129 // Need address checking and timeout settings.
130 time_t timeout = 3600;
131 if (checkTimeout || !ignoreAddress) {
132 const PropertySet* props = getApplication().getPropertySet("Sessions");
135 pair<bool,unsigned int> p = props->getUnsignedInt("timeout");
139 pair<bool,bool> pcheck = props->getBool("consistentAddress");
141 ignoreAddress = !pcheck.second;
145 // The cache will either silently pass a session or nullptr back, or throw an exception out.
146 Session* session = getServiceProvider().getSessionCache()->find(
147 getApplication(), *this, (ignoreAddress ? nullptr : getRemoteAddr().c_str()), (checkTimeout ? &timeout : nullptr)
154 static char _x2c(const char *what)
158 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
160 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
164 void AbstractSPRequest::setRequestURI(const char* uri)
166 // Fix for bug 574, secadv 20061002
167 // Unescape URI up to query string delimiter by looking for %XX escapes.
168 // Adapted from Apache's util.c, ap_unescape_url function.
175 else if (*uri != '%') {
180 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
181 throw ConfigurationException("Bad request, contained unsupported encoded characters.");
190 const char* AbstractSPRequest::getRequestURI() const
192 return m_uri.c_str();
195 const char* AbstractSPRequest::getRequestURL() const
198 // Compute the full target URL
199 int port = getPort();
200 const char* scheme = getScheme();
201 m_url = string(scheme) + "://" + getHostname();
202 if (!isDefaultPort())
203 m_url += ":" + boost::lexical_cast<string>(port);
206 return m_url.c_str();
209 string AbstractSPRequest::getRemoteAddr() const
211 pair<bool,const char*> addr = getRequestSettings().first->getString("REMOTE_ADDR");
212 return addr.first ? getHeader(addr.second) : "";
215 const char* AbstractSPRequest::getParameter(const char* name) const
218 m_parser.reset(new CGIParser(*this));
220 pair<CGIParser::walker,CGIParser::walker> bounds = m_parser->getParameters(name);
221 return (bounds.first==bounds.second) ? nullptr : bounds.first->second;
224 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
227 m_parser.reset(new CGIParser(*this));
229 pair<CGIParser::walker,CGIParser::walker> bounds = m_parser->getParameters(name);
230 while (bounds.first != bounds.second) {
231 values.push_back(bounds.first->second);
234 return values.size();
237 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
240 resource = getRequestURL();
242 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(), resource))
243 return m_handlerURL.c_str();
245 // Check for relative URL.
246 string stackresource;
247 if (resource && *resource == '/') {
248 // Compute a URL to the root of the site and point resource at constructed string.
249 int port = getPort();
250 const char* scheme = getScheme();
251 stackresource = string(scheme) + "://" + getHostname();
252 if (!isDefaultPort())
253 stackresource += ":" + boost::lexical_cast<string>(port);
254 stackresource += resource;
255 resource = stackresource.c_str();
258 #ifdef HAVE_STRCASECMP
259 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
261 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
263 throw ConfigurationException("Target resource was not an absolute URL.");
265 bool ssl_only = true;
266 const char* handler = nullptr;
267 const PropertySet* props = getApplication().getPropertySet("Sessions");
269 pair<bool,bool> p = props->getBool("handlerSSL");
272 pair<bool,const char*> p2 = props->getString("handlerURL");
278 handler = "/Shibboleth.sso";
280 else if (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)) {
281 throw ConfigurationException(
282 "Invalid handlerURL property ($1) in <Sessions> element for Application ($2)",
283 params(2, handler ? handler : "null", m_app->getId())
287 // The "handlerURL" property can be in one of three formats:
289 // 1) a full URI: http://host/foo/bar
290 // 2) a hostless URI: http:///foo/bar
291 // 3) a relative path: /foo/bar
293 // # Protocol Host Path
294 // 1 handler handler handler
295 // 2 handler resource handler
296 // 3 resource resource handler
298 // note: if ssl_only is true, make sure the protocol is https
300 const char* path = nullptr;
302 // Decide whether to use the handler or the resource for the "protocol"
304 if (*handler != '/') {
312 // break apart the "protocol" string into protocol, host, and "the rest"
313 const char* colon = strchr(prot, ':');
315 const char* slash = strchr(colon, '/');
319 // Compute the actual protocol and store in member.
321 m_handlerURL.assign("https://");
323 m_handlerURL.assign(prot, colon-prot);
325 // create the "host" from either the colon/slash or from the target string
326 // If prot == handler then we're in either #1 or #2, else #3.
327 // If slash == colon then we're in #2.
328 if (prot != handler || slash == colon) {
329 colon = strchr(resource, ':');
330 colon += 3; // Get past the ://
331 slash = strchr(colon, '/');
333 string host(colon, (slash ? slash-colon : strlen(colon)));
335 // Build the handler URL
336 m_handlerURL += host + path;
337 return m_handlerURL.c_str();
340 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
342 reinterpret_cast<Category*>(m_log)->log(
343 (level == SPDebug ? Priority::DEBUG :
344 (level == SPInfo ? Priority::INFO :
345 (level == SPWarn ? Priority::WARN :
346 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
351 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
353 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
354 (level == SPDebug ? Priority::DEBUG :
355 (level == SPInfo ? Priority::INFO :
356 (level == SPWarn ? Priority::WARN :
357 (level == SPError ? Priority::ERROR : Priority::CRIT))))