https://issues.shibboleth.net/jira/browse/SSPCPP-129
[shibboleth/cpp-sp.git] / fastcgi / shibauthorizer.cpp
index 284793a..db51ad1 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
@@ -67,7 +67,8 @@ class ShibTargetFCGIAuth : public AbstractSPRequest
 public:\r
     map<string,string> m_request_headers;\r
 \r
-    ShibTargetFCGIAuth(FCGX_Request* req, const char* scheme=NULL, const char* hostname=NULL, int port=0) : m_req(req) {\r
+    ShibTargetFCGIAuth(FCGX_Request* req, const char* scheme=NULL, const char* hostname=NULL, int port=0)\r
+            : AbstractSPRequest(SHIBSP_LOGCAT".FastCGI"), m_req(req) {\r
         const char* server_name_str = hostname;\r
         if (!server_name_str || !*server_name_str)\r
             server_name_str = FCGX_GetParam("SERVER_NAME", req->envp);\r
@@ -87,6 +88,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
     ~ShibTargetFCGIAuth() { }\r
@@ -100,9 +103,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
@@ -133,11 +133,20 @@ public:
             m_request_headers.erase(name);\r
     }\r
     virtual string getHeader(const char* name) const {\r
+        // Look in the local map first.\r
         map<string,string>::const_iterator i = m_request_headers.find(name);\r
         if (i != m_request_headers.end())\r
             return i->second;\r
-        else\r
-            return "";\r
+        // Nothing set locally, so try the request.\r
+        string hdr("HTTP_");\r
+        for (; *name; ++name) {\r
+            if (*name=='-')\r
+                hdr += '_';\r
+            else\r
+                hdr += toupper(*name);\r
+        }\r
+        char* s = FCGX_GetParam(hdr.c_str(), m_req->envp);\r
+        return s ? s : "";\r
     }\r
     void setRemoteUser(const char* user) {\r
         if (user)\r
@@ -169,7 +178,7 @@ public:
     const char* getRequestBody() const {\r
         throw runtime_error("getRequestBody not implemented by FastCGI authorizer.");\r
     }\r
\r
+\r
     long sendResponse(istream& in, long status) {\r
         string hdr = string("Connection: close\r\n");\r
         for (multimap<string,string>::const_iterator i=m_response_headers.begin(); i!=m_response_headers.end(); ++i)\r
@@ -206,7 +215,7 @@ public:
         return SHIB_RETURN_DONE;\r
     }\r
 \r
-    long returnDecline() { \r
+    long returnDecline() {\r
         return SHIB_RETURN_KO;\r
     }\r
 \r
@@ -236,16 +245,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
@@ -255,21 +254,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
@@ -299,7 +291,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
     {\r
@@ -315,7 +307,7 @@ int main(void)
         try {\r
             xmltooling::NDC ndc("FastCGI shibauthorizer");\r
             ShibTargetFCGIAuth sta(&request, g_ServerScheme.c_str(), g_ServerName.c_str(), g_ServerPort);\r
-          \r
+\r
             pair<bool,long> res = sta.getServiceProvider().doAuthentication(sta);\r
             if (res.first) {\r
 #ifdef _DEBUG\r
@@ -325,21 +317,21 @@ int main(void)
                     case SHIB_RETURN_OK:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
-              \r
+\r
                     case SHIB_RETURN_KO:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
 \r
                     case SHIB_RETURN_DONE:\r
                         continue;\r
-              \r
+\r
                     default:\r
                         cerr << "shib: doAuthentication returned an unexpected result: " << res.second << endl;\r
                         print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");\r
                         continue;\r
                 }\r
             }\r
-          \r
+\r
             res = sta.getServiceProvider().doExport(sta);\r
             if (res.first) {\r
 #ifdef _DEBUG\r
@@ -349,14 +341,14 @@ int main(void)
                     case SHIB_RETURN_OK:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
-              \r
+\r
                     case SHIB_RETURN_KO:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
 \r
                     case SHIB_RETURN_DONE:\r
                         continue;\r
-              \r
+\r
                     default:\r
                         cerr << "shib: doExport returned an unexpected result: " << res.second << endl;\r
                         print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");\r
@@ -373,14 +365,14 @@ int main(void)
                     case SHIB_RETURN_OK:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
-              \r
+\r
                     case SHIB_RETURN_KO:\r
                         print_ok(sta.m_request_headers);\r
                         continue;\r
 \r
                     case SHIB_RETURN_DONE:\r
                         continue;\r
-              \r
+\r
                     default:\r
                         cerr << "shib: doAuthorization returned an unexpected result: " << res.second << endl;\r
                         print_error("<html><body>FastCGI Shibboleth authorizer returned an unexpected result.</body></html>");\r
@@ -389,7 +381,7 @@ int main(void)
             }\r
 \r
             print_ok(sta.m_request_headers);\r
-          \r
+\r
         }\r
         catch (exception& e) {\r
             cerr << "shib: FastCGI authorizer caught an exception: " << e.what() << endl;\r
@@ -408,6 +400,6 @@ int main(void)
 \r
     if (g_Config)\r
         g_Config->term();\r
\r
+\r
     return 0;\r
 }\r