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 using namespace shibsp;
37 using namespace opensaml;
38 using namespace xmltooling;
41 SPRequest::SPRequest()
45 SPRequest::~SPRequest()
49 string SPRequest::getSecureHeader(const char* name) const
51 return getHeader(name);
54 void SPRequest::setAuthType(const char* authtype)
58 #ifdef SHIBSP_HAVE_GSSAPI
59 GSSRequest::GSSRequest()
63 GSSRequest::~GSSRequest()
68 AbstractSPRequest::AbstractSPRequest(const char* category)
69 : m_sp(nullptr), m_mapper(nullptr), m_app(nullptr), m_sessionTried(false), m_session(nullptr),
70 m_log(&Category::getInstance(category)), m_parser(nullptr)
72 m_sp=SPConfig::getConfig().getServiceProvider();
76 AbstractSPRequest::~AbstractSPRequest()
87 const ServiceProvider& AbstractSPRequest::getServiceProvider() const
92 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
95 // Map request to application and content settings.
96 m_mapper=m_sp->getRequestMapper();
98 m_settings = m_mapper->getSettings(*this);
100 if (reinterpret_cast<Category*>(m_log)->isDebugEnabled()) {
101 reinterpret_cast<Category*>(m_log)->debug(
102 "mapped %s to %s", getRequestURL(), m_settings.first->getString("applicationId").second
109 const Application& AbstractSPRequest::getApplication() const
112 // Now find the application from the URL settings
113 m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
115 throw ConfigurationException("Unable to map non-default applicationId to an ApplicationOverride, check configuration.");
120 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache)
122 // Only attempt this once.
123 if (cache && m_sessionTried)
126 m_sessionTried = true;
128 // Need address checking and timeout settings.
130 if (checkTimeout || !ignoreAddress) {
131 const PropertySet* props=getApplication().getPropertySet("Sessions");
134 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
138 pair<bool,bool> pcheck=props->getBool("consistentAddress");
140 ignoreAddress = !pcheck.second;
144 // The cache will either silently pass a session or nullptr back, or throw an exception out.
145 Session* session = getServiceProvider().getSessionCache()->find(
146 getApplication(), *this, ignoreAddress ? nullptr : getRemoteAddr().c_str(), checkTimeout ? &timeout : nullptr
153 static char _x2c(const char *what)
157 digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
159 digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
163 void AbstractSPRequest::setRequestURI(const char* uri)
165 // Fix for bug 574, secadv 20061002
166 // Unescape URI up to query string delimiter by looking for %XX escapes.
167 // Adapted from Apache's util.c, ap_unescape_url function.
174 else if (*uri != '%') {
179 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
180 throw ConfigurationException("Bad request, contained unsupported encoded characters.");
189 const char* AbstractSPRequest::getRequestURI() const
191 return m_uri.c_str();
194 const char* AbstractSPRequest::getRequestURL() const
197 // Compute the full target URL
198 int port = getPort();
199 const char* scheme = getScheme();
200 m_url = string(scheme) + "://" + getHostname();
201 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
202 ostringstream portstr;
204 m_url += ":" + portstr.str();
208 return m_url.c_str();
211 string AbstractSPRequest::getRemoteAddr() const
213 pair<bool,const char*> addr = getRequestSettings().first->getString("REMOTE_ADDR");
214 return addr.first ? getHeader(addr.second) : "";
217 const char* AbstractSPRequest::getParameter(const char* name) const
220 m_parser=new CGIParser(*this);
222 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
223 return (bounds.first==bounds.second) ? nullptr : bounds.first->second;
226 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
229 m_parser=new CGIParser(*this);
231 pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
232 while (bounds.first!=bounds.second) {
233 values.push_back(bounds.first->second);
236 return values.size();
239 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
242 resource = getRequestURL();
244 if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
245 return m_handlerURL.c_str();
247 string stackresource;
248 if (resource && *resource == '/') {
249 // Compute a URL to the root of the site and point resource at constructed string.
250 int port = getPort();
251 const char* scheme = getScheme();
252 stackresource = string(scheme) + "://" + getHostname();
253 if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) {
254 ostringstream portstr;
256 stackresource += ":" + portstr.str();
258 stackresource += resource;
259 resource = stackresource.c_str();
262 #ifdef HAVE_STRCASECMP
263 if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
265 if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
267 throw ConfigurationException("Target resource was not an absolute URL.");
270 const char* handler=nullptr;
271 const PropertySet* props=getApplication().getPropertySet("Sessions");
273 pair<bool,bool> p=props->getBool("handlerSSL");
276 pair<bool,const char*> p2=props->getString("handlerURL");
282 handler = "/Shibboleth.sso";
284 else if (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)) {
285 throw ConfigurationException(
286 "Invalid handlerURL property ($1) in <Sessions> element for Application ($2)",
287 params(2, handler ? handler : "null", m_app->getId())
291 // The "handlerURL" property can be in one of three formats:
293 // 1) a full URI: http://host/foo/bar
294 // 2) a hostless URI: http:///foo/bar
295 // 3) a relative path: /foo/bar
297 // # Protocol Host Path
298 // 1 handler handler handler
299 // 2 handler resource handler
300 // 3 resource resource handler
302 // note: if ssl_only is true, make sure the protocol is https
304 const char* path = nullptr;
306 // Decide whether to use the handler or the resource for the "protocol"
308 if (*handler != '/') {
316 // break apart the "protocol" string into protocol, host, and "the rest"
317 const char* colon=strchr(prot,':');
319 const char* slash=strchr(colon,'/');
323 // Compute the actual protocol and store in member.
325 m_handlerURL.assign("https://");
327 m_handlerURL.assign(prot, colon-prot);
329 // create the "host" from either the colon/slash or from the target string
330 // If prot == handler then we're in either #1 or #2, else #3.
331 // If slash == colon then we're in #2.
332 if (prot != handler || slash == colon) {
333 colon = strchr(resource, ':');
334 colon += 3; // Get past the ://
335 slash = strchr(colon, '/');
337 string host(colon, (slash ? slash-colon : strlen(colon)));
339 // Build the handler URL
340 m_handlerURL += host + path;
341 return m_handlerURL.c_str();
344 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
346 reinterpret_cast<Category*>(m_log)->log(
347 (level == SPDebug ? Priority::DEBUG :
348 (level == SPInfo ? Priority::INFO :
349 (level == SPWarn ? Priority::WARN :
350 (level == SPError ? Priority::ERROR : Priority::CRIT)))),
355 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
357 return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
358 (level == SPDebug ? Priority::DEBUG :
359 (level == SPInfo ? Priority::INFO :
360 (level == SPWarn ? Priority::WARN :
361 (level == SPError ? Priority::ERROR : Priority::CRIT))))