https://issues.shibboleth.net/jira/browse/SSPCPP-130
[shibboleth/cpp-sp.git] / fastcgi / shibresponder.cpp
index 4cab35d..5df6dcd 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
  *  Copyright 2001-2007 Internet2\r
- * \r
+ *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
@@ -36,6 +36,7 @@
 #include <xmltooling/util/XMLHelper.h>\r
 #include <xercesc/util/XMLUniDefs.hpp>\r
 \r
+#include <stdexcept>\r
 #include <stdlib.h>\r
 #ifdef HAVE_UNISTD_H\r
 # include <unistd.h>\r
@@ -67,7 +68,7 @@ class ShibTargetFCGI : public AbstractSPRequest
 \r
 public:\r
     ShibTargetFCGI(FCGX_Request* req, char* post_data, const char* scheme=NULL, const char* hostname=NULL, int port=0)\r
-        : m_req(req), m_body(post_data) {\r
+        : AbstractSPRequest(SHIBSP_LOGCAT".FastCGI"), m_req(req), m_body(post_data) {\r
 \r
         const char* server_name_str = hostname;\r
         if (!server_name_str || !*server_name_str)\r
@@ -80,7 +81,7 @@ public:
             m_port = strtol(server_port_str, &server_port_str, 10);\r
             if (*server_port_str) {\r
                 cerr << "can't parse SERVER_PORT (" << FCGX_GetParam("SERVER_PORT", req->envp) << ")" << endl;\r
-                throw exception("Unable to determine server port.");\r
+                throw runtime_error("Unable to determine server port.");\r
             }\r
         }\r
 \r
@@ -88,6 +89,8 @@ public:
         if (!server_scheme_str || !*server_scheme_str)\r
             server_scheme_str = (m_port == 443 || m_port == 8443) ? "https" : "http";\r
         m_scheme = server_scheme_str;\r
+\r
+        setRequestURI(FCGX_GetParam("REQUEST_URI", m_req->envp));\r
     }\r
 \r
     ~ShibTargetFCGI() { }\r
@@ -101,9 +104,6 @@ public:
     int getPort() const {\r
         return m_port;\r
     }\r
-    const char* getRequestURI() const {\r
-        return FCGX_GetParam("REQUEST_URI", m_req->envp);\r
-    }\r
     const char* getMethod() const {\r
         return FCGX_GetParam("REQUEST_METHOD", m_req->envp);\r
     }\r
@@ -164,9 +164,10 @@ public:
 \r
         const char* codestr="Status: 200 OK";\r
         switch (status) {\r
-            case XMLTOOLING_HTTP_STATUS_ERROR:    codestr="Status: 500 Server Error"; break;\r
-            case XMLTOOLING_HTTP_STATUS_FORBIDDEN:codestr="Status: 403 Forbidden"; break;\r
-            case XMLTOOLING_HTTP_STATUS_NOTFOUND: codestr="Status: 404 Not Found"; break;\r
+            case XMLTOOLING_HTTP_STATUS_ERROR:          codestr="Status: 500 Server Error"; break;\r
+            case XMLTOOLING_HTTP_STATUS_UNAUTHORIZED:   codestr="Status: 401 Authorization Required"; break;\r
+            case XMLTOOLING_HTTP_STATUS_FORBIDDEN:      codestr="Status: 403 Forbidden"; break;\r
+            case XMLTOOLING_HTTP_STATUS_NOTFOUND:       codestr="Status: 404 Not Found"; break;\r
         }\r
         cout << codestr << "\r\n" << hdr << "\r\n";\r
         char buf[1024];\r
@@ -206,15 +207,15 @@ public:
     // Not used in the extension.\r
 \r
     virtual void clearHeader(const char* rawname, const char* cginame) {\r
-        throw exception("clearHeader not implemented by FastCGI responder.");\r
+        throw runtime_error("clearHeader not implemented by FastCGI responder.");\r
     }\r
-  \r
+\r
     virtual void setHeader(const char* name, const char* value) {\r
-        throw exception("setHeader not implemented by FastCGI responder.");\r
+        throw runtime_error("setHeader not implemented by FastCGI responder.");\r
     }\r
 \r
     virtual void setRemoteUser(const char* user) {\r
-        throw exception("setRemoteUser not implemented by FastCGI responder.");\r
+        throw runtime_error("setRemoteUser not implemented by FastCGI responder.");\r
     }\r
 };\r
 \r
@@ -268,16 +269,6 @@ static void print_error(const char* msg) {
 \r
 int main(void)\r
 {\r
-    const char* schemadir=getenv("SHIBSP_SCHEMAS");\r
-    if (!schemadir)\r
-        schemadir=SHIBSP_SCHEMAS;\r
-    const char* config=getenv("SHIBSP_CONFIG");\r
-    if (!config)\r
-        config=SHIBSP_CONFIG;\r
-\r
-    cerr << "SHIBSP_CONFIG = " << config << endl\r
-         << "SHIBSP_SCHEMAS = " << schemadir << endl;\r
-\r
     SPConfig* g_Config=&SPConfig::getConfig();\r
     g_Config->setFeatures(\r
         SPConfig::Listener |\r
@@ -287,21 +278,14 @@ int main(void)
         SPConfig::Logging |\r
         SPConfig::Handlers\r
         );\r
-    if (!g_Config->init(schemadir)) {\r
+    if (!g_Config->init()) {\r
         cerr << "failed to initialize Shibboleth libraries" << endl;\r
         exit(1);\r
     }\r
 \r
     try {\r
-        DOMDocument* dummydoc=XMLToolingConfig::getConfig().getParser().newDocument();\r
-        XercesJanitor<DOMDocument> docjanitor(dummydoc);\r
-        DOMElement* dummy = dummydoc->createElementNS(NULL,path);\r
-        auto_ptr_XMLCh src(config);\r
-        dummy->setAttributeNS(NULL,path,src.get());\r
-        dummy->setAttributeNS(NULL,validate,xmlconstants::XML_ONE);\r
-\r
-        g_Config->setServiceProvider(g_Config->ServiceProviderManager.newPlugin(XML_SERVICE_PROVIDER,dummy));\r
-        g_Config->getServiceProvider()->init();\r
+        if (!g_Config->instantiate(NULL, true))\r
+            throw exception("unknown error");\r
     }\r
     catch (exception& ex) {\r
         g_Config->term();\r
@@ -332,7 +316,7 @@ int main(void)
 \r
     FCGX_Init();\r
     FCGX_InitRequest(&request, 0, 0);\r
-    \r
+\r
     cout << "Shibboleth initialization complete. Starting request loop." << endl;\r
     while (FCGX_Accept_r(&request) == 0) {\r
         // Note that the default bufsize (0) will cause the use of iostream\r
@@ -355,17 +339,15 @@ int main(void)
         try {\r
             xmltooling::NDC ndc("FastCGI shibresponder");\r
             ShibTargetFCGI stf(&request, content, g_ServerScheme.c_str(), g_ServerName.c_str(), g_ServerPort);\r
-          \r
+\r
             pair<bool,long> res = stf.getServiceProvider().doHandler(stf);\r
             if (res.first) {\r
-#ifdef _DEBUG\r
-                cerr << "shib: doHandler handled the request" << endl;\r
-#endif\r
+                stf.log(SPRequest::SPDebug, "shib: doHandler handled the request");\r
                 switch(res.second) {\r
                     case SHIB_RETURN_OK:\r
                         print_ok();\r
                         break;\r
-              \r
+\r
                     case SHIB_RETURN_KO:\r
                         cerr << "shib: doHandler failed to handle the request" << endl;\r
                         print_error("<html><body>FastCGI Shibboleth responder should only be used for Shibboleth protocol requests.</body></html>");\r
@@ -374,7 +356,7 @@ int main(void)
                     case SHIB_RETURN_DONE:\r
                         // response already handled\r
                         break;\r
-              \r
+\r
                     default:\r
                         cerr << "shib: doHandler returned an unexpected result: " << res.second << endl;\r
                         print_error("<html><body>FastCGI Shibboleth responder returned an unexpected result.</body></html>");\r
@@ -384,8 +366,8 @@ int main(void)
             else {\r
                 cerr << "shib: doHandler failed to handle request." << endl;\r
                 print_error("<html><body>FastCGI Shibboleth responder failed to process request.</body></html>");\r
-            }          \r
-          \r
+            }\r
+\r
         }\r
         catch (exception& e) {\r
             cerr << "shib: FastCGI responder caught an exception: " << e.what() << endl;\r
@@ -408,6 +390,6 @@ int main(void)
 \r
     if (g_Config)\r
         g_Config->term();\r
\r
+\r
     return 0;\r
 }\r