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 /* shibresponder.cpp - Shibboleth FastCGI Responder/Handler
\r
23 #include <saml/saml.h>
\r
24 #include <shib-target/shib-target.h>
\r
27 #ifdef HAVE_UNISTD_H
\r
28 # include <unistd.h>
\r
29 # include <sys/mman.h>
\r
33 using namespace shibtarget;
\r
34 using namespace saml;
\r
35 using namespace std;
\r
43 class ShibTargetFCGI : public ShibTarget
\r
45 FCGX_Request* m_req;
\r
48 map<string, string> m_headers;
\r
51 ShibTargetFCGI(FCGX_Request* req, char* post_data, const char* scheme=NULL, const char* hostname=NULL, int port=0)
\r
52 : m_req(req), m_body(post_data) {
\r
54 const char* server_name_str = hostname;
\r
55 if (!server_name_str || !*server_name_str)
\r
56 server_name_str = FCGX_GetParam("SERVER_NAME", req->envp);
\r
58 int server_port = port;
\r
60 char* server_port_str = FCGX_GetParam("SERVER_PORT", req->envp);
\r
61 server_port = strtol(server_port_str, &server_port_str, 10);
\r
62 if (*server_port_str) {
\r
63 cerr << "can't parse SERVER_PORT (" << FCGX_GetParam("SERVER_PORT", req->envp) << ")" << endl;
\r
64 throw SAMLException("Unable to determine server port.");
\r
68 const char* server_scheme_str = scheme;
\r
69 if (!server_scheme_str || !*server_scheme_str)
\r
70 server_scheme_str = (server_port == 443 || server_port == 8443) ? "https" : "http";
\r
72 const char* request_uri_str = FCGX_GetParam("REQUEST_URI", req->envp);
\r
73 const char* content_type_str = FCGX_GetParam("CONTENT_TYPE", req->envp);
\r
74 const char* remote_addr_str = FCGX_GetParam("REMOTE_ADDR", req->envp);
\r
75 const char* request_method_str = FCGX_GetParam("REQUEST_METHOD", req->envp);
\r
78 cerr << "server_name = " << server_name_str << endl
\r
79 << "server_port = " << server_port << endl
\r
80 << "request_uri_str = " << request_uri_str << endl
\r
81 << "content_type = " << content_type_str << endl
\r
82 << "remote_address = " << remote_addr_str << endl
\r
83 << "request_method = " << request_method_str << endl;
\r
86 init(server_scheme_str,
\r
90 content_type_str ? content_type_str : "",
\r
96 ~ShibTargetFCGI() { }
\r
98 virtual void log(ShibLogLevel level, const string& msg) {
\r
99 ShibTarget::log(level,msg);
\r
101 if (level == LogLevelError)
\r
102 cerr << "shib: " << msg;
\r
105 virtual string getCookies(void) const {
\r
106 char * cookie = FCGX_GetParam("HTTP_COOKIE", m_req->envp);
\r
107 return cookie ? cookie : "";
\r
110 virtual void setCookie(const string& name, const string& value) {
\r
111 m_cookie += "Set-Cookie: " + name + "=" + value + "\r\n";
\r
114 virtual string getArgs(void) {
\r
115 char * args = FCGX_GetParam("QUERY_STRING", m_req->envp);
\r
116 return args ? args : "";
\r
119 virtual string getPostData(void) {
\r
120 return m_body ? m_body : "";
\r
123 virtual void clearHeader(const string &name) {
\r
124 throw SAMLException("clearHeader not implemented by FastCGI responder.");
\r
127 virtual void setHeader(const string &name, const string &value) {
\r
128 throw SAMLException("setHeader not implemented by FastCGI responder.");
\r
131 virtual string getHeader(const string &name) {
\r
132 throw SAMLException("getHeader not implemented by FastCGI responder.");
\r
135 virtual void setRemoteUser(const string &user) {
\r
136 throw SAMLException("setRemoteUser not implemented by FastCGI responder.");
\r
139 virtual string getRemoteUser(void) {
\r
140 throw SAMLException("getRemoteUser not implemented by FastCGI responder.");
\r
143 virtual void* sendPage(
\r
146 const string& content_type="text/html",
\r
147 const Iterator<header_t>& headers=EMPTY(header_t)) {
\r
149 string hdr = string ("Connection: close\r\nContent-type: ") + content_type + "\r\n" + m_cookie;
\r
150 while (headers.hasNext()) {
\r
151 const header_t& h=headers.next();
\r
152 hdr += h.first + ": " + h.second + "\r\n";
\r
155 const char* codestr="Status: 200 OK";
\r
157 case 500: codestr="Status: 500 Server Error"; break;
\r
158 case 403: codestr="Status: 403 Forbidden"; break;
\r
159 case 404: codestr="Status: 404 Not Found"; break;
\r
162 cout << codestr << "\r\n" << hdr << m_cookie << "\r\n" << msg;
\r
163 return (void*)SHIB_RETURN_DONE;
\r
166 virtual void* sendRedirect(const string& url) {
\r
167 cout << "Status: 302 Please Wait" << "\r\n" << "Location: " << url << "\r\n" << m_cookie << "\r\n"
\r
168 << "<HTML><BODY>Redirecting...</BODY></HTML>";
\r
169 return (void*)SHIB_RETURN_DONE;
\r
172 virtual void* returnDecline(void) {
\r
173 return (void*)SHIB_RETURN_KO;
\r
176 virtual void* returnOK(void) {
\r
177 return (void*)SHIB_RETURN_OK;
\r
181 // Maximum number of bytes allowed to be read from stdin
\r
182 static const unsigned long STDIN_MAX = 1000000;
\r
184 static long gstdin(FCGX_Request* request, char** content)
\r
186 char* clenstr = FCGX_GetParam("CONTENT_LENGTH", request->envp);
\r
187 unsigned long clen = STDIN_MAX;
\r
190 clen = strtol(clenstr, &clenstr, 10);
\r
192 cerr << "can't parse CONTENT_LENGTH (" << FCGX_GetParam("CONTENT_LENGTH", request->envp) << ")" << endl;
\r
196 // *always* put a cap on the amount of data that will be read
\r
197 if (clen > STDIN_MAX)
\r
200 *content = new char[clen];
\r
202 cin.read(*content, clen);
\r
203 clen = cin.gcount();
\r
206 // *never* read stdin when CONTENT_LENGTH is missing or unparsable
\r
211 // Chew up any remaining stdin - this shouldn't be necessary
\r
212 // but is because mod_fastcgi doesn't handle it correctly.
\r
214 // ignore() doesn't set the eof bit in some versions of glibc++
\r
215 // so use gcount() instead of eof()...
\r
216 do cin.ignore(1024); while (cin.gcount() == 1024);
\r
221 static void print_ok() {
\r
222 cout << "Status: 200 OK" << "\r\n\r\n";
\r
225 static void print_error(const char* msg) {
\r
226 cout << "Status: 500 Server Error" << "\r\n\r\n" << msg;
\r
231 char* shib_config = getenv("SHIB_CONFIG");
\r
232 char* shib_schema = getenv("SHIB_SCHEMA");
\r
233 if ((shib_config == NULL) || (shib_schema == NULL)) {
\r
234 cerr << "SHIB_CONFIG or SHIB_SCHEMA not set." << endl;
\r
237 cerr << "SHIB_CONFIG = " << shib_config << endl
\r
238 << "SHIB_SCHEMA = " << shib_schema << endl;
\r
240 string g_ServerScheme;
\r
241 string g_ServerName;
\r
242 int g_ServerPort=0;
\r
243 ShibTargetConfig* g_Config;
\r
246 g_Config = &ShibTargetConfig::getConfig();
\r
247 g_Config->setFeatures(
\r
248 ShibTargetConfig::Listener |
\r
249 ShibTargetConfig::Metadata |
\r
250 ShibTargetConfig::RequestMapper |
\r
251 ShibTargetConfig::LocalExtensions |
\r
252 ShibTargetConfig::Logging
\r
254 if (!g_Config->init(shib_schema)) {
\r
255 cerr << "failed to initialize Shibboleth libraries" << endl;
\r
259 if (!g_Config->load(shib_config)) {
\r
260 cerr << "failed to load Shibboleth configuration" << endl;
\r
265 cerr << "exception while initializing Shibboleth configuration" << endl;
\r
269 // Load "authoritative" URL fields.
\r
270 char* var = getenv("SHIBSP_SERVER_NAME");
\r
272 g_ServerName = var;
\r
273 var = getenv("SHIBSP_SERVER_SCHEME");
\r
275 g_ServerScheme = var;
\r
276 var = getenv("SHIBSP_SERVER_PORT");
\r
278 g_ServerPort = atoi(var);
\r
280 streambuf* cin_streambuf = cin.rdbuf();
\r
281 streambuf* cout_streambuf = cout.rdbuf();
\r
282 streambuf* cerr_streambuf = cerr.rdbuf();
\r
284 FCGX_Request request;
\r
287 FCGX_InitRequest(&request, 0, 0);
\r
289 cout << "Shibboleth initialization complete. Starting request loop." << endl;
\r
290 while (FCGX_Accept_r(&request) == 0) {
\r
291 // Note that the default bufsize (0) will cause the use of iostream
\r
292 // methods that require positioning (such as peek(), seek(),
\r
293 // unget() and putback()) to fail (in favour of more efficient IO).
\r
294 fcgi_streambuf cin_fcgi_streambuf(request.in);
\r
295 fcgi_streambuf cout_fcgi_streambuf(request.out);
\r
296 fcgi_streambuf cerr_fcgi_streambuf(request.err);
\r
298 cin.rdbuf(&cin_fcgi_streambuf);
\r
299 cout.rdbuf(&cout_fcgi_streambuf);
\r
300 cerr.rdbuf(&cerr_fcgi_streambuf);
\r
302 // Although FastCGI supports writing before reading,
\r
303 // many http clients (browsers) don't support it (so
\r
304 // the connection deadlocks until a timeout expires!).
\r
306 gstdin(&request, &content);
\r
309 saml::NDC ndc("FastCGI shibresponder");
\r
310 ShibTargetFCGI stf(&request, content, g_ServerScheme.c_str(), g_ServerName.c_str(), g_ServerPort);
\r
312 pair<bool,void*> res = stf.doHandler();
\r
315 cerr << "shib: doHandler handled the request" << endl;
\r
317 switch((long)res.second) {
\r
318 case SHIB_RETURN_OK:
\r
322 case SHIB_RETURN_KO:
\r
323 cerr << "shib: doHandler failed to handle the request" << endl;
\r
324 print_error("<html><body>FastCGI Shibboleth responder should only be used for Shibboleth protocol requests.</body></html>");
\r
327 case SHIB_RETURN_DONE:
\r
328 // response already handled
\r
332 cerr << "shib: doHandler returned an unexpected result: " << (int)res.second << endl;
\r
333 print_error("<html><body>FastCGI Shibboleth responder returned an unexpected result.</body></html>");
\r
338 cerr << "shib: doHandler failed to handle request." << endl;
\r
339 print_error("<html><body>FastCGI Shibboleth responder failed to process request.</body></html>");
\r
343 catch (SAMLException& e) {
\r
344 cerr << "shib: FastCGI responder caught an exception: " << e.what() << endl;
\r
345 print_error("<html><body>FastCGI Shibboleth responder caught an exception, check log for details.</body></html>");
\r
350 // If the output streambufs had non-zero bufsizes and
\r
351 // were constructed outside of the accept loop (i.e.
\r
352 // their destructor won't be called here), they would
\r
353 // have to be flushed here.
\r
356 cout << "Request loop ended." << endl;
\r
358 cin.rdbuf(cin_streambuf);
\r
359 cout.rdbuf(cout_streambuf);
\r
360 cerr.rdbuf(cerr_streambuf);
\r
363 g_Config->shutdown();
\r