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.
21 /* shibresponder.cpp - Shibboleth FastCGI Responder/Handler
27 #include "config_win32.h"
29 #define _CRT_NONSTDC_NO_DEPRECATE 1
30 #define _CRT_SECURE_NO_DEPRECATE 1
31 #define _SCL_SECURE_NO_WARNINGS 1
33 #include <shibsp/AbstractSPRequest.h>
34 #include <shibsp/SPConfig.h>
35 #include <shibsp/ServiceProvider.h>
36 #include <xmltooling/unicode.h>
37 #include <xmltooling/XMLToolingConfig.h>
38 #include <xmltooling/util/NDC.h>
39 #include <xmltooling/util/XMLConstants.h>
40 #include <xmltooling/util/XMLHelper.h>
41 #include <xercesc/util/XMLUniDefs.hpp>
47 # include <sys/mman.h>
51 using namespace shibsp;
52 using namespace xmltooling;
53 using namespace xercesc;
56 static const XMLCh path[] = UNICODE_LITERAL_4(p,a,t,h);
57 static const XMLCh validate[] = UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
65 class ShibTargetFCGI : public AbstractSPRequest
69 multimap<string,string> m_headers;
71 string m_scheme,m_hostname;
74 ShibTargetFCGI(FCGX_Request* req, char* post_data, const char* scheme=nullptr, const char* hostname=nullptr, int port=0)
75 : AbstractSPRequest(SHIBSP_LOGCAT".FastCGI"), m_req(req), m_body(post_data) {
77 const char* server_name_str = hostname;
78 if (!server_name_str || !*server_name_str)
79 server_name_str = FCGX_GetParam("SERVER_NAME", req->envp);
80 m_hostname = server_name_str;
84 char* server_port_str = FCGX_GetParam("SERVER_PORT", req->envp);
85 m_port = strtol(server_port_str, &server_port_str, 10);
86 if (*server_port_str) {
87 cerr << "can't parse SERVER_PORT (" << FCGX_GetParam("SERVER_PORT", req->envp) << ")" << endl;
88 throw runtime_error("Unable to determine server port.");
92 const char* server_scheme_str = scheme;
93 if (!server_scheme_str || !*server_scheme_str)
94 server_scheme_str = (m_port == 443 || m_port == 8443) ? "https" : "http";
95 m_scheme = server_scheme_str;
97 setRequestURI(FCGX_GetParam("REQUEST_URI", m_req->envp));
100 ~ShibTargetFCGI() { }
102 const char* getScheme() const {
103 return m_scheme.c_str();
105 const char* getHostname() const {
106 return m_hostname.c_str();
108 int getPort() const {
111 const char* getMethod() const {
112 return FCGX_GetParam("REQUEST_METHOD", m_req->envp);
114 string getContentType() const {
115 const char* s = FCGX_GetParam("CONTENT_TYPE", m_req->envp);
118 long getContentLength() const {
119 const char* s = FCGX_GetParam("CONTENT_LENGTH", m_req->envp);
120 return s ? atol(s) : 0;
122 string getRemoteUser() const {
123 const char* s = FCGX_GetParam("REMOTE_USER", m_req->envp);
126 string getRemoteAddr() const {
127 string ret = AbstractSPRequest::getRemoteAddr();
130 const char* s = FCGX_GetParam("REMOTE_ADDR", m_req->envp);
133 void log(SPLogLevel level, const string& msg) const {
134 AbstractSPRequest::log(level,msg);
135 if (level >= SPError)
136 cerr << "shib: " << msg;
139 string getHeader(const char* name) const {
141 for (; *name; ++name) {
145 hdr += toupper(*name);
147 char* s = FCGX_GetParam(hdr.c_str(), m_req->envp);
151 void setResponseHeader(const char* name, const char* value) {
152 HTTPResponse::setResponseHeader(name, value);
155 m_headers.insert(make_pair(name,value));
157 m_headers.erase(name);
160 const char* getQueryString() const {
161 return FCGX_GetParam("QUERY_STRING", m_req->envp);
164 const char* getRequestBody() const {
168 long sendResponse(istream& in, long status) {
169 string hdr = string("Connection: close\r\n");
170 for (multimap<string,string>::const_iterator i=m_headers.begin(); i!=m_headers.end(); ++i)
171 hdr += i->first + ": " + i->second + "\r\n";
173 const char* codestr="Status: 200 OK";
175 case XMLTOOLING_HTTP_STATUS_ERROR: codestr="Status: 500 Server Error"; break;
176 case XMLTOOLING_HTTP_STATUS_UNAUTHORIZED: codestr="Status: 401 Authorization Required"; break;
177 case XMLTOOLING_HTTP_STATUS_FORBIDDEN: codestr="Status: 403 Forbidden"; break;
178 case XMLTOOLING_HTTP_STATUS_NOTFOUND: codestr="Status: 404 Not Found"; break;
179 case XMLTOOLING_HTTP_STATUS_NOTMODIFIED: codestr="Status: 304 Not Modified"; break;
181 cout << codestr << "\r\n" << hdr << "\r\n";
185 cout.write(buf, in.gcount());
187 return SHIB_RETURN_DONE;
190 long sendRedirect(const char* url) {
191 HTTPResponse::sendRedirect(url);
192 string hdr=string("Status: 302 Please Wait\r\nLocation: ") + url + "\r\n"
193 "Content-Type: text/html\r\n"
194 "Content-Length: 40\r\n"
195 "Expires: 01-Jan-1997 12:00:00 GMT\r\n"
196 "Cache-Control: private,no-store,no-cache\r\n";
197 for (multimap<string,string>::const_iterator i=m_headers.begin(); i!=m_headers.end(); ++i)
198 hdr += i->first + ": " + i->second + "\r\n";
201 cout << hdr << "<HTML><BODY>Redirecting...</BODY></HTML>";
202 return SHIB_RETURN_DONE;
205 long returnDecline() {
206 return SHIB_RETURN_KO;
209 return SHIB_RETURN_OK;
212 const vector<string>& getClientCertificates() const {
213 static vector<string> g_NoCerts;
217 // Not used in the extension.
219 virtual void clearHeader(const char* rawname, const char* cginame) {
220 throw runtime_error("clearHeader not implemented by FastCGI responder.");
223 virtual void setHeader(const char* name, const char* value) {
224 throw runtime_error("setHeader not implemented by FastCGI responder.");
227 virtual void setRemoteUser(const char* user) {
228 throw runtime_error("setRemoteUser not implemented by FastCGI responder.");
232 // Maximum number of bytes allowed to be read from stdin
233 static const unsigned long STDIN_MAX = 1000000;
235 static long gstdin(FCGX_Request* request, char** content)
237 char* clenstr = FCGX_GetParam("CONTENT_LENGTH", request->envp);
238 unsigned long clen = STDIN_MAX;
241 clen = strtol(clenstr, &clenstr, 10);
243 cerr << "can't parse CONTENT_LENGTH (" << FCGX_GetParam("CONTENT_LENGTH", request->envp) << ")" << endl;
247 // *always* put a cap on the amount of data that will be read
248 if (clen > STDIN_MAX)
251 *content = new char[clen];
253 cin.read(*content, clen);
257 // *never* read stdin when CONTENT_LENGTH is missing or unparsable
262 // Chew up any remaining stdin - this shouldn't be necessary
263 // but is because mod_fastcgi doesn't handle it correctly.
265 // ignore() doesn't set the eof bit in some versions of glibc++
266 // so use gcount() instead of eof()...
267 do cin.ignore(1024); while (cin.gcount() == 1024);
272 static void print_ok() {
273 cout << "Status: 200 OK" << "\r\n\r\n";
276 static void print_error(const char* msg) {
277 cout << "Status: 500 Server Error" << "\r\n\r\n" << msg;
282 SPConfig* g_Config=&SPConfig::getConfig();
283 g_Config->setFeatures(
286 SPConfig::RequestMapping |
287 SPConfig::InProcess |
291 if (!g_Config->init()) {
292 cerr << "failed to initialize Shibboleth libraries" << endl;
297 if (!g_Config->instantiate(nullptr, true))
298 throw runtime_error("unknown error");
300 catch (exception& ex) {
302 cerr << "exception while initializing Shibboleth configuration: " << ex.what() << endl;
306 string g_ServerScheme;
310 // Load "authoritative" URL fields.
311 char* var = getenv("SHIBSP_SERVER_NAME");
314 var = getenv("SHIBSP_SERVER_SCHEME");
316 g_ServerScheme = var;
317 var = getenv("SHIBSP_SERVER_PORT");
319 g_ServerPort = atoi(var);
321 streambuf* cin_streambuf = cin.rdbuf();
322 streambuf* cout_streambuf = cout.rdbuf();
323 streambuf* cerr_streambuf = cerr.rdbuf();
325 FCGX_Request request;
328 FCGX_InitRequest(&request, 0, 0);
330 cout << "Shibboleth initialization complete. Starting request loop." << endl;
331 while (FCGX_Accept_r(&request) == 0) {
332 // Note that the default bufsize (0) will cause the use of iostream
333 // methods that require positioning (such as peek(), seek(),
334 // unget() and putback()) to fail (in favour of more efficient IO).
335 fcgi_streambuf cin_fcgi_streambuf(request.in);
336 fcgi_streambuf cout_fcgi_streambuf(request.out);
337 fcgi_streambuf cerr_fcgi_streambuf(request.err);
339 cin.rdbuf(&cin_fcgi_streambuf);
340 cout.rdbuf(&cout_fcgi_streambuf);
341 cerr.rdbuf(&cerr_fcgi_streambuf);
343 // Although FastCGI supports writing before reading,
344 // many http clients (browsers) don't support it (so
345 // the connection deadlocks until a timeout expires!).
347 gstdin(&request, &content);
350 xmltooling::NDC ndc("FastCGI shibresponder");
351 ShibTargetFCGI stf(&request, content, g_ServerScheme.c_str(), g_ServerName.c_str(), g_ServerPort);
353 pair<bool,long> res = stf.getServiceProvider().doHandler(stf);
355 stf.log(SPRequest::SPDebug, "shib: doHandler handled the request");
362 cerr << "shib: doHandler failed to handle the request" << endl;
363 print_error("<html><body>FastCGI Shibboleth responder should only be used for Shibboleth protocol requests.</body></html>");
366 case SHIB_RETURN_DONE:
367 // response already handled
371 cerr << "shib: doHandler returned an unexpected result: " << res.second << endl;
372 print_error("<html><body>FastCGI Shibboleth responder returned an unexpected result.</body></html>");
377 cerr << "shib: doHandler failed to handle request." << endl;
378 print_error("<html><body>FastCGI Shibboleth responder failed to process request.</body></html>");
382 catch (exception& e) {
383 cerr << "shib: FastCGI responder caught an exception: " << e.what() << endl;
384 print_error("<html><body>FastCGI Shibboleth responder caught an exception, check log for details.</body></html>");
389 // If the output streambufs had non-zero bufsizes and
390 // were constructed outside of the accept loop (i.e.
391 // their destructor won't be called here), they would
392 // have to be flushed here.
395 cout << "Request loop ended." << endl;
397 cin.rdbuf(cin_streambuf);
398 cout.rdbuf(cout_streambuf);
399 cerr.rdbuf(cerr_streambuf);