Fix backslashes in SHIBSP_PREFIX variable by manually creating it during the script...
[shibboleth/sp.git] / shibsp / AbstractSPRequest.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * AbstractSPRequest.cpp
19  * 
20  * Abstract base for SPRequest implementations  
21  */
22
23 #include "internal.h"
24 #include "AbstractSPRequest.h"
25 #include "Application.h"
26 #include "ServiceProvider.h"
27 #include "SessionCache.h"
28
29 using namespace shibsp;
30 using namespace opensaml;
31 using namespace xmltooling;
32 using namespace std;
33
34 AbstractSPRequest::AbstractSPRequest(const char* category)
35     : m_sp(NULL), m_mapper(NULL), m_app(NULL), m_sessionTried(false), m_session(NULL),
36         m_log(&Category::getInstance(category)), m_parser(NULL)
37 {
38     m_sp=SPConfig::getConfig().getServiceProvider();
39     m_sp->lock();
40 }
41
42 AbstractSPRequest::~AbstractSPRequest()
43 {
44     if (m_session)
45         m_session->unlock();
46     if (m_mapper)
47         m_mapper->unlock();
48     if (m_sp)
49         m_sp->unlock();
50     delete m_parser;
51 }
52
53 RequestMapper::Settings AbstractSPRequest::getRequestSettings() const
54 {
55     if (m_mapper)
56         return m_settings;
57
58     // Map request to application and content settings.
59     m_mapper=m_sp->getRequestMapper();
60     m_mapper->lock();
61     return m_settings = m_mapper->getSettings(*this);
62
63 }
64
65 const Application& AbstractSPRequest::getApplication() const
66 {
67     if (!m_app) {
68         // Now find the application from the URL settings
69         m_app=m_sp->getApplication(getRequestSettings().first->getString("applicationId").second);
70         if (!m_app)
71             throw ConfigurationException("Unable to map request to application settings, check configuration.");
72     }    
73     return *m_app;
74 }
75
76 Session* AbstractSPRequest::getSession(bool checkTimeout, bool ignoreAddress, bool cache) const
77 {
78     // Only attempt this once.
79     if (cache && m_sessionTried)
80         return m_session;
81     else if (cache)
82         m_sessionTried = true;
83
84     // Need address checking and timeout settings.
85     time_t timeout=0;
86     if (checkTimeout || !ignoreAddress) {
87         const PropertySet* props=getApplication().getPropertySet("Sessions");
88         if (props) {
89             if (checkTimeout) {
90                 pair<bool,unsigned int> p=props->getUnsignedInt("timeout");
91                 if (p.first)
92                     timeout = p.second;
93             }
94             pair<bool,bool> pcheck=props->getBool("consistentAddress");
95             if (pcheck.first)
96                 ignoreAddress = !pcheck.second;
97         }
98     }
99
100     // The cache will either silently pass a session or NULL back, or throw an exception out.
101     Session* session = getServiceProvider().getSessionCache()->find(
102         getApplication(), *this, ignoreAddress ? NULL : getRemoteAddr().c_str(), checkTimeout ? &timeout : NULL
103         );
104     if (cache)
105         m_session = session;
106     return session;
107 }
108
109 static char _x2c(const char *what)
110 {
111     register char digit;
112
113     digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
114     digit *= 16;
115     digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
116     return(digit);
117 }
118
119 void AbstractSPRequest::setRequestURI(const char* uri)
120 {
121     // Fix for bug 574, secadv 20061002
122     // Unescape URI up to query string delimiter by looking for %XX escapes.
123     // Adapted from Apache's util.c, ap_unescape_url function.
124     if (uri) {
125         while (*uri) {
126             if (*uri == '?') {
127                 m_uri += uri;
128                 break;
129             }
130             else if (*uri == ';') {
131                 // If this is Java being stupid, skip everything up to the query string, if any.
132                 if (!strncmp(uri, ";jsessionid=", 12)) {
133                     if (uri = strchr(uri, '?'))
134                         m_uri += uri;
135                     break;
136                 }
137                 else {
138                     m_uri += *uri;
139                 }
140             }
141             else if (*uri != '%') {
142                 m_uri += *uri;
143             }
144             else {
145                 ++uri;
146                 if (!isxdigit(*uri) || !isxdigit(*(uri+1)))
147                     throw ConfigurationException("Bad request, contained unsupported encoded characters.");
148                 m_uri += _x2c(uri);
149                 ++uri;
150             }
151             ++uri;
152         }
153     }
154 }
155
156 const char* AbstractSPRequest::getRequestURL() const
157 {
158     if (m_url.empty()) {
159         // Compute the full target URL
160         int port = getPort();
161         const char* scheme = getScheme();
162         m_url = string(scheme) + "://" + getHostname();
163         if ((!strcmp(scheme,"http") && port!=80) || (!strcmp(scheme,"https") && port!=443)) { 
164             ostringstream portstr;
165             portstr << port;
166             m_url += ":" + portstr.str();
167         }
168         m_url += m_uri;
169     }
170     return m_url.c_str();
171 }
172
173 const char* AbstractSPRequest::getParameter(const char* name) const
174 {
175     if (!m_parser)
176         m_parser=new CGIParser(*this);
177     
178     pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
179     return (bounds.first==bounds.second) ? NULL : bounds.first->second;
180 }
181
182 vector<const char*>::size_type AbstractSPRequest::getParameters(const char* name, vector<const char*>& values) const
183 {
184     if (!m_parser)
185         m_parser=new CGIParser(*this);
186
187     pair<CGIParser::walker,CGIParser::walker> bounds=m_parser->getParameters(name);
188     while (bounds.first!=bounds.second) {
189         values.push_back(bounds.first->second);
190         ++bounds.first;
191     }
192     return values.size();
193 }
194
195 const char* AbstractSPRequest::getHandlerURL(const char* resource) const
196 {
197     if (!resource)
198         resource = getRequestURL();
199
200     if (!m_handlerURL.empty() && resource && !strcmp(getRequestURL(),resource))
201         return m_handlerURL.c_str();
202         
203 #ifdef HAVE_STRCASECMP
204     if (!resource || (strncasecmp(resource,"http://",7) && strncasecmp(resource,"https://",8)))
205 #else
206     if (!resource || (strnicmp(resource,"http://",7) && strnicmp(resource,"https://",8)))
207 #endif
208         throw ConfigurationException("Target resource was not an absolute URL.");
209
210     bool ssl_only=false;
211     const char* handler=NULL;
212     const PropertySet* props=m_app->getPropertySet("Sessions");
213     if (props) {
214         pair<bool,bool> p=props->getBool("handlerSSL");
215         if (p.first)
216             ssl_only=p.second;
217         pair<bool,const char*> p2=props->getString("handlerURL");
218         if (p2.first)
219             handler=p2.second;
220     }
221     
222     // Should never happen...
223     if (!handler || (*handler!='/' && strncmp(handler,"http:",5) && strncmp(handler,"https:",6)))
224         throw ConfigurationException(
225             "Invalid handlerURL property ($1) in Application ($2)",
226             params(2, handler ? handler : "null", m_app->getId())
227             );
228
229     // The "handlerURL" property can be in one of three formats:
230     //
231     // 1) a full URI:       http://host/foo/bar
232     // 2) a hostless URI:   http:///foo/bar
233     // 3) a relative path:  /foo/bar
234     //
235     // #  Protocol  Host        Path
236     // 1  handler   handler     handler
237     // 2  handler   resource    handler
238     // 3  resource  resource    handler
239     //
240     // note: if ssl_only is true, make sure the protocol is https
241
242     const char* path = NULL;
243
244     // Decide whether to use the handler or the resource for the "protocol"
245     const char* prot;
246     if (*handler != '/') {
247         prot = handler;
248     }
249     else {
250         prot = resource;
251         path = handler;
252     }
253
254     // break apart the "protocol" string into protocol, host, and "the rest"
255     const char* colon=strchr(prot,':');
256     colon += 3;
257     const char* slash=strchr(colon,'/');
258     if (!path)
259         path = slash;
260
261     // Compute the actual protocol and store in member.
262     if (ssl_only)
263         m_handlerURL.assign("https://");
264     else
265         m_handlerURL.assign(prot, colon-prot);
266
267     // create the "host" from either the colon/slash or from the target string
268     // If prot == handler then we're in either #1 or #2, else #3.
269     // If slash == colon then we're in #2.
270     if (prot != handler || slash == colon) {
271         colon = strchr(resource, ':');
272         colon += 3;      // Get past the ://
273         slash = strchr(colon, '/');
274     }
275     string host(colon, (slash ? slash-colon : strlen(colon)));
276
277     // Build the handler URL
278     m_handlerURL += host + path;
279     return m_handlerURL.c_str();
280 }
281
282 void AbstractSPRequest::log(SPLogLevel level, const std::string& msg) const
283 {
284     reinterpret_cast<Category*>(m_log)->log(
285         (level == SPDebug ? Priority::DEBUG :
286         (level == SPInfo ? Priority::INFO :
287         (level == SPWarn ? Priority::WARN :
288         (level == SPError ? Priority::ERROR : Priority::CRIT)))),
289         msg
290         );
291 }
292
293 bool AbstractSPRequest::isPriorityEnabled(SPLogLevel level) const
294 {
295     return reinterpret_cast<Category*>(m_log)->isPriorityEnabled(
296         (level == SPDebug ? Priority::DEBUG :
297         (level == SPInfo ? Priority::INFO :
298         (level == SPWarn ? Priority::WARN :
299         (level == SPError ? Priority::ERROR : Priority::CRIT))))
300         );
301 }