2 * Copyright 2001-2007 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 /* shibauthorizer.cpp - Shibboleth FastCGI Authorizer
\r
23 #include "config_win32.h"
\r
25 #define _CRT_NONSTDC_NO_DEPRECATE 1
\r
26 #define _CRT_SECURE_NO_DEPRECATE 1
\r
27 #define _SCL_SECURE_NO_WARNINGS 1
\r
29 #include <shibsp/AbstractSPRequest.h>
\r
30 #include <shibsp/SPConfig.h>
\r
31 #include <shibsp/ServiceProvider.h>
\r
32 #include <xmltooling/unicode.h>
\r
33 #include <xmltooling/XMLToolingConfig.h>
\r
34 #include <xmltooling/util/NDC.h>
\r
35 #include <xmltooling/util/XMLConstants.h>
\r
36 #include <xmltooling/util/XMLHelper.h>
\r
37 #include <xercesc/util/XMLUniDefs.hpp>
\r
39 #include <stdexcept>
\r
41 #ifdef HAVE_UNISTD_H
\r
42 # include <unistd.h>
\r
43 # include <sys/mman.h>
\r
47 using namespace shibsp;
\r
48 using namespace xmltooling;
\r
49 using namespace xercesc;
\r
50 using namespace std;
\r
52 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
\r
53 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
\r
61 class ShibTargetFCGIAuth : public AbstractSPRequest
\r
63 FCGX_Request* m_req;
\r
65 string m_scheme,m_hostname;
\r
66 set<string> m_cleared_headers;
\r
67 multimap<string,string> m_response_headers;
\r
69 map<string,string> m_request_headers;
\r
71 ShibTargetFCGIAuth(FCGX_Request* req, const char* scheme=NULL, const char* hostname=NULL, int port=0)
\r
72 : AbstractSPRequest(SHIBSP_LOGCAT".FastCGI"), m_req(req) {
\r
73 const char* server_name_str = hostname;
\r
74 if (!server_name_str || !*server_name_str)
\r
75 server_name_str = FCGX_GetParam("SERVER_NAME", req->envp);
\r
76 m_hostname = server_name_str;
\r
80 char* server_port_str = FCGX_GetParam("SERVER_PORT", req->envp);
\r
81 m_port = strtol(server_port_str, &server_port_str, 10);
\r
82 if (*server_port_str) {
\r
83 cerr << "can't parse SERVER_PORT (" << FCGX_GetParam("SERVER_PORT", req->envp) << ")" << endl;
\r
84 throw runtime_error("Unable to determine server port.");
\r
88 const char* server_scheme_str = scheme;
\r
89 if (!server_scheme_str || !*server_scheme_str)
\r
90 server_scheme_str = (m_port == 443 || m_port == 8443) ? "https" : "http";
\r
91 m_scheme = server_scheme_str;
\r
93 setRequestURI(FCGX_GetParam("REQUEST_URI", m_req->envp));
\r
96 ~ShibTargetFCGIAuth() { }
\r
98 const char* getScheme() const {
\r
99 return m_scheme.c_str();
\r
101 const char* getHostname() const {
\r
102 return m_hostname.c_str();
\r
104 int getPort() const {
\r
107 const char* getMethod() const {
\r
108 return FCGX_GetParam("REQUEST_METHOD", m_req->envp);
\r
110 string getContentType() const {
\r
111 const char* s = FCGX_GetParam("CONTENT_TYPE", m_req->envp);
\r
114 long getContentLength() const {
\r
115 const char* s = FCGX_GetParam("CONTENT_LENGTH", m_req->envp);
\r
116 return s ? atol(s) : 0;
\r
118 string getRemoteAddr() const {
\r
119 const char* s = FCGX_GetParam("REMOTE_ADDR", m_req->envp);
\r
122 void log(SPLogLevel level, const string& msg) const {
\r
123 AbstractSPRequest::log(level,msg);
\r
124 if (level >= SPError)
\r
125 cerr << "shib: " << msg;
\r
127 void clearHeader(const char* rawname, const char* cginame) {
\r
128 // Need to save off the name to prevent access to the header later.
\r
129 m_cleared_headers.insert(rawname);
\r
131 void setHeader(const char* name, const char* value) {
\r
133 m_request_headers[name] = value;
\r
135 m_request_headers.erase(name);
\r
137 virtual string getHeader(const char* name) const {
\r
138 // Look in the local map first.
\r
139 map<string,string>::const_iterator i = m_request_headers.find(name);
\r
140 if (i != m_request_headers.end())
\r
142 // If not in the local set, see if it's a "controlled" header by
\r
143 // checking the cleared list.
\r
144 if (m_cleared_headers.count(name) > 0)
\r
146 // Nothing set locally and it's safe, so try the request.
\r
147 string hdr("HTTP_");
\r
148 for (; *name; ++name) {
\r
152 hdr += toupper(*name);
\r
154 char* s = FCGX_GetParam(hdr.c_str(), m_req->envp);
\r
157 void setRemoteUser(const char* user) {
\r
159 m_request_headers["REMOTE_USER"] = user;
\r
161 m_request_headers.erase("REMOTE_USER");
\r
163 string getRemoteUser() const {
\r
164 map<string,string>::const_iterator i = m_request_headers.find("REMOTE_USER");
\r
165 if (i != m_request_headers.end())
\r
168 char* remote_user = FCGX_GetParam("REMOTE_USER", m_req->envp);
\r
170 return remote_user;
\r
174 void setResponseHeader(const char* name, const char* value) {
\r
177 m_response_headers.insert(make_pair(name,value));
\r
179 m_response_headers.erase(name);
\r
181 const char* getQueryString() const {
\r
182 return FCGX_GetParam("QUERY_STRING", m_req->envp);
\r
184 const char* getRequestBody() const {
\r
185 throw runtime_error("getRequestBody not implemented by FastCGI authorizer.");
\r
188 long sendResponse(istream& in, long status) {
\r
189 string hdr = string("Connection: close\r\n");
\r
190 for (multimap<string,string>::const_iterator i=m_response_headers.begin(); i!=m_response_headers.end(); ++i)
\r
191 hdr += i->first + ": " + i->second + "\r\n";
\r
193 // We can't return 200 OK here or else the filter is bypassed
\r
194 // so custom Shib errors will get turned into a generic page.
\r
195 const char* codestr="Status: 500 Server Error";
\r
197 case XMLTOOLING_HTTP_STATUS_UNAUTHORIZED: codestr="Status: 401 Authorization Required"; break;
\r
198 case XMLTOOLING_HTTP_STATUS_FORBIDDEN: codestr="Status: 403 Forbidden"; break;
\r
199 case XMLTOOLING_HTTP_STATUS_NOTFOUND: codestr="Status: 404 Not Found"; break;
\r
201 cout << codestr << "\r\n" << hdr << "\r\n";
\r
205 cout.write(buf, in.gcount());
\r
207 return SHIB_RETURN_DONE;
\r
210 long sendRedirect(const char* url) {
\r
211 string hdr=string("Status: 302 Please Wait\r\nLocation: ") + url + "\r\n"
\r
212 "Content-Type: text/html\r\n"
\r
213 "Content-Length: 40\r\n"
\r
214 "Expires: 01-Jan-1997 12:00:00 GMT\r\n"
\r
215 "Cache-Control: private,no-store,no-cache\r\n";
\r
216 for (multimap<string,string>::const_iterator i=m_response_headers.begin(); i!=m_response_headers.end(); ++i)
\r
217 hdr += i->first + ": " + i->second + "\r\n";
\r
220 cout << hdr << "<HTML><BODY>Redirecting...</BODY></HTML>";
\r
221 return SHIB_RETURN_DONE;
\r
224 long returnDecline() {
\r
225 return SHIB_RETURN_KO;
\r
229 return SHIB_RETURN_OK;
\r
232 const vector<string>& getClientCertificates() const {
\r
233 static vector<string> g_NoCerts;
\r
238 static void print_ok(const map<string,string>& headers)
\r
240 cout << "Status: 200 OK" << "\r\n";
\r
241 for (map<string,string>::const_iterator iter = headers.begin(); iter != headers.end(); iter++) {
\r
242 cout << "Variable-" << iter->first << ": " << iter->second << "\r\n";
\r
247 static void print_error(const char* msg)
\r
249 cout << "Status: 500 Server Error" << "\r\n\r\n" << msg;
\r
254 SPConfig* g_Config=&SPConfig::getConfig();
\r
255 g_Config->setFeatures(
\r
256 SPConfig::Listener |
\r
257 SPConfig::Caching |
\r
258 SPConfig::RequestMapping |
\r
259 SPConfig::InProcess |
\r
260 SPConfig::Logging |
\r
263 if (!g_Config->init()) {
\r
264 cerr << "failed to initialize Shibboleth libraries" << endl;
\r
269 if (!g_Config->instantiate(NULL, true))
\r
270 throw exception("unknown error");
\r
272 catch (exception& ex) {
\r
274 cerr << "exception while initializing Shibboleth configuration: " << ex.what() << endl;
\r
278 string g_ServerScheme;
\r
279 string g_ServerName;
\r
280 int g_ServerPort=0;
\r
282 // Load "authoritative" URL fields.
\r
283 char* var = getenv("SHIBSP_SERVER_NAME");
\r
285 g_ServerName = var;
\r
286 var = getenv("SHIBSP_SERVER_SCHEME");
\r
288 g_ServerScheme = var;
\r
289 var = getenv("SHIBSP_SERVER_PORT");
\r
291 g_ServerPort = atoi(var);
\r
293 streambuf* cout_streambuf = cout.rdbuf();
\r
294 streambuf* cerr_streambuf = cerr.rdbuf();
\r
296 FCGX_Request request;
\r
299 FCGX_InitRequest(&request, 0, 0);
\r
301 cout << "Shibboleth initialization complete. Starting request loop." << endl;
\r
302 while (FCGX_Accept_r(&request) == 0)
\r
304 // Note that the default bufsize (0) will cause the use of iostream
\r
305 // methods that require positioning (such as peek(), seek(),
\r
306 // unget() and putback()) to fail (in favour of more efficient IO).
\r
307 fcgi_streambuf cout_fcgi_streambuf(request.out);
\r
308 fcgi_streambuf cerr_fcgi_streambuf(request.err);
\r
310 cout.rdbuf(&cout_fcgi_streambuf);
\r
311 cerr.rdbuf(&cerr_fcgi_streambuf);
\r
314 xmltooling::NDC ndc("FastCGI shibauthorizer");
\r
315 ShibTargetFCGIAuth sta(&request, g_ServerScheme.c_str(), g_ServerName.c_str(), g_ServerPort);
\r
317 pair<bool,long> res = sta.getServiceProvider().doAuthentication(sta);
\r
320 cerr << "shib: doAuthentication handled the request" << endl;
\r
322 switch(res.second) {
\r
323 case SHIB_RETURN_OK:
\r
324 print_ok(sta.m_request_headers);
\r
327 case SHIB_RETURN_KO:
\r
328 print_ok(sta.m_request_headers);
\r
331 case SHIB_RETURN_DONE:
\r
335 cerr << "shib: doAuthentication returned an unexpected result: " << res.second << endl;
\r
336 print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");
\r
341 res = sta.getServiceProvider().doExport(sta);
\r
344 cerr << "shib: doExport handled request" << endl;
\r
346 switch(res.second) {
\r
347 case SHIB_RETURN_OK:
\r
348 print_ok(sta.m_request_headers);
\r
351 case SHIB_RETURN_KO:
\r
352 print_ok(sta.m_request_headers);
\r
355 case SHIB_RETURN_DONE:
\r
359 cerr << "shib: doExport returned an unexpected result: " << res.second << endl;
\r
360 print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");
\r
365 res = sta.getServiceProvider().doAuthorization(sta);
\r
368 cerr << "shib: doAuthorization handled request" << endl;
\r
370 switch(res.second) {
\r
371 case SHIB_RETURN_OK:
\r
372 print_ok(sta.m_request_headers);
\r
375 case SHIB_RETURN_KO:
\r
376 print_ok(sta.m_request_headers);
\r
379 case SHIB_RETURN_DONE:
\r
383 cerr << "shib: doAuthorization returned an unexpected result: " << res.second << endl;
\r
384 print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");
\r
389 print_ok(sta.m_request_headers);
\r
392 catch (exception& e) {
\r
393 cerr << "shib: FastCGI authorizer caught an exception: " << e.what() << endl;
\r
394 print_error("<html><body>FastCGI Shibboleth authorizer caught an exception, check log for details.</body></html>");
\r
397 // If the output streambufs had non-zero bufsizes and
\r
398 // were constructed outside of the accept loop (i.e.
\r
399 // their destructor won't be called here), they would
\r
400 // have to be flushed here.
\r
402 cout << "Request loop ended." << endl;
\r
404 cout.rdbuf(cout_streambuf);
\r
405 cerr.rdbuf(cerr_streambuf);
\r