Remove xmlproviders from build, deleted old AAP interface.
[shibboleth/cpp-sp.git] / shib-target / shib-target.h
index 3123c93..6297b2d 100644 (file)
@@ -1,50 +1,17 @@
 /*
- * The Shibboleth License, Version 1.
- * Copyright (c) 2002
- * University Corporation for Advanced Internet Development, Inc.
- * All rights reserved
+ *  Copyright 2001-2007 Internet2
  *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
+ *     http://www.apache.org/licenses/LICENSE-2.0
  *
- * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution, if any, must include
- * the following acknowledgment: "This product includes software developed by
- * the University Corporation for Advanced Internet Development
- * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
- * may appear in the software itself, if and wherever such third-party
- * acknowledgments normally appear.
- *
- * Neither the name of Shibboleth nor the names of its contributors, nor
- * Internet2, nor the University Corporation for Advanced Internet Development,
- * Inc., nor UCAID may be used to endorse or promote products derived from this
- * software without specific prior written permission. For written permission,
- * please contact shibboleth@shibboleth.org
- *
- * Products derived from this software may not be called Shibboleth, Internet2,
- * UCAID, or the University Corporation for Advanced Internet Development, nor
- * may Shibboleth appear in their name, without prior written permission of the
- * University Corporation for Advanced Internet Development.
- *
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
- * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
- * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
- * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
  */
 
 /*
 #ifndef SHIB_TARGET_H
 #define SHIB_TARGET_H
 
+// New headers
+#include <shibsp/Application.h>
+#include <shibsp/Handler.h>
+#include <shibsp/RequestMapper.h>
+#include <shibsp/ServiceProvider.h>
+#include <shibsp/SessionCache.h>
+#include <shibsp/remoting/ListenerService.h>
+
+// Old headers
 #include <saml/saml.h>
 #include <shib/shib.h>
-#include <shib/shib-threads.h>
 
 #ifdef WIN32
 # ifndef SHIBTARGET_EXPORTS
 #  define SHIBTARGET_EXPORTS __declspec(dllimport)
 # endif
+# define SHIB_SCHEMAS "/opt/shibboleth-sp/share/xml/shibboleth"
+# define SHIB_CONFIG "/opt/shibboleth-sp/etc/shibboleth/shibboleth.xml"
 #else
 # include <shib-target/shib-paths.h>
 # define SHIBTARGET_EXPORTS
 #endif
 
-#include <shib-target/shibrpc.h>
-
 namespace shibtarget {
   
-  class SHIBTARGET_EXPORTS ShibTargetException : public std::exception
-  {
-  public:
-    explicit ShibTargetException() : m_code(SHIBRPC_OK) {}
-    explicit ShibTargetException(ShibRpcStatus code, const char* msg, const shibboleth::IProvider* provider);
-    explicit ShibTargetException(ShibRpcStatus code, const char* msg, const shibboleth::IProviderRole* role=NULL);
-    
-    virtual ~ShibTargetException() throw () {}
-    virtual ShibRpcStatus which() const throw () { return m_code; }
-    virtual const char* what() const throw () { return m_msg.c_str(); }
-    virtual const char* syswho() const throw() { return m_providerId.c_str(); }
-    virtual const char* where() const throw () { return m_errorURL.c_str(); }
-    virtual const char* who() const throw () { return m_contact.c_str(); }
-    virtual const char* how() const throw () { return m_email.c_str(); }
-
-  private:
-    ShibRpcStatus m_code;
-    std::string m_msg;
-    std::string m_providerId;
-    std::string m_errorURL;
-    std::string m_contact;
-    std::string m_email;
-  };
-
-  class RPCErrorPriv;
-  class SHIBTARGET_EXPORTS RPCError
-  {
-  public:
-    RPCError();
-    RPCError(ShibRpcError* e);
-    RPCError(int s, const char* st);
-    RPCError(ShibTargetException &exp);
-    ~RPCError();
-
-    bool isError();
-    bool isRetryable();
-
-    // Return a set of strings that correspond to the error properties
-    const char* getType();
-    const char* getText();
-    const char* getDesc();
-    const char* getProviderId();
-    const char* getErrorURL();
-    const char* getContactName();
-    const char* getContactEmail();
-    int getCode();
-
-  private:
-    RPCErrorPriv* m_priv;
-  };
-
     // Abstract APIs for access to configuration information
     
-    struct SHIBTARGET_EXPORTS IPropertySet
-    {
-        virtual std::pair<bool,bool> getBool(const char* name, const char* ns=NULL) const=0;
-        virtual std::pair<bool,const char*> getString(const char* name, const char* ns=NULL) const=0;
-        virtual std::pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const=0;
-        virtual std::pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const=0;
-        virtual std::pair<bool,int> getInt(const char* name, const char* ns=NULL) const=0;
-        virtual const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const=0;
-        virtual const DOMElement* getElement() const=0;
-        virtual ~IPropertySet() {}
-    };
-
-    struct SHIBTARGET_EXPORTS IListener : public virtual shibboleth::IPlugIn
+    /**
+     * Interface to Shibboleth Applications, which exposes most of the functionality
+     * required to process web requests or security protocol messages for resources
+     * associated with them.
+     * 
+     * Applications are implementation-specific, but generally correspond to collections
+     * of resources related to one another in logical ways, such as a virtual host or
+     * a Java servlet context. Most complex configuration data is associated with an
+     * Application. Implementations should always expose an application named "default"
+     * as a last resort.
+     */
+    struct SHIBTARGET_EXPORTS IApplication : public virtual shibsp::Application,
+        public virtual shibboleth::ShibBrowserProfile::ITokenValidator
     {
-#ifdef WIN32
-        typedef SOCKET ShibSocket;
-#else
-        typedef int ShibSocket;
-#endif
-        virtual bool create(ShibSocket& s) const=0;
-        virtual bool bind(ShibSocket& s, bool force=false) const=0;
-        virtual bool connect(ShibSocket& s) const=0;
-        virtual bool close(ShibSocket& s) const=0;
-        virtual bool accept(ShibSocket& listener, ShibSocket& s) const=0;
-        virtual CLIENT* getClientHandle(ShibSocket& s, u_long program, u_long version) const=0;
-        virtual ~IListener() {}
-    };
+        // caller is borrowing object, must use within scope of config lock
+        virtual const saml::SAMLBrowserProfile* getBrowserProfile() const=0;
+        virtual const saml::SAMLBinding* getBinding(const XMLCh* binding) const=0;
+
+        // caller is given ownership of object, must use and delete within scope of config lock
+        virtual saml::SAMLBrowserProfile::ArtifactMapper* getArtifactMapper() const=0;
+
+        // general token validation based on conditions, signatures, etc.
+        virtual void validateToken(
+            saml::SAMLAssertion* token,
+            time_t t=0,
+            const opensaml::saml2md::RoleDescriptor* role=NULL,
+            const xmltooling::TrustEngine* trust=NULL
+            ) const=0;
 
-    struct SHIBTARGET_EXPORTS IAccessControl : public virtual shibboleth::ILockable, public virtual shibboleth::IPlugIn
-    {
-        virtual bool authorized(const saml::Iterator<saml::SAMLAssertion*>& creds) const=0;
-        virtual ~IAccessControl() {}
+        virtual ~IApplication() {}
     };
 
-    struct SHIBTARGET_EXPORTS IRequestMapper : public virtual shibboleth::ILockable, public virtual shibboleth::IPlugIn
-    {
-        typedef std::pair<const IPropertySet*,IAccessControl*> Settings;
-        virtual Settings getSettingsFromURL(const char* url) const=0;
-        virtual Settings getSettingsFromParsedURL(
-            const char* scheme, const char* hostname, unsigned int port, const char* path=NULL
-            ) const=0;
-        virtual ~IRequestMapper() {}
-    };
-    
-    struct SHIBTARGET_EXPORTS IApplication : public virtual IPropertySet
+    /**
+     * OpenSAML binding hook
+     *
+     * Instead of wrapping the binding to deal with mutual authentication, we
+     * just use the HTTP hook functionality offered by OpenSAML. The hook will
+     * register "itself" as a globalCtx pointer with the SAML binding and the caller
+     * will declare and pass the embedded struct as callCtx for use by the hook.
+     */
+    class ShibHTTPHook : virtual public saml::SAMLSOAPHTTPBinding::HTTPHook
     {
-        virtual const char* getId() const=0;
-        virtual saml::Iterator<saml::SAMLAttributeDesignator*> getAttributeDesignators() const=0;
-        virtual saml::Iterator<shibboleth::IAAP*> getAAPProviders() const=0;
-        virtual saml::Iterator<shibboleth::IMetadata*> getMetadataProviders() const=0;
-        virtual saml::Iterator<shibboleth::ITrust*> getTrustProviders() const=0;
-        virtual saml::Iterator<shibboleth::IRevocation*> getRevocationProviders() const=0;
-        virtual saml::Iterator<const XMLCh*> getAudiences() const=0;
-        virtual const char* getTLSCred(const shibboleth::IProvider* provider) const=0;
-        virtual const char* getSigningCred(const shibboleth::IProvider* provider) const=0;
-        virtual ~IApplication() {}
+    public:
+        ShibHTTPHook(const xmltooling::TrustEngine* trust) : m_trust(trust) {}
+        virtual ~ShibHTTPHook() {}
+        
+        // Only hook we need here is for outgoing connection to server.
+        virtual bool outgoing(saml::HTTPClient* conn, void* globalCtx=NULL, void* callCtx=NULL);
+
+        // Client declares a context object and pass as callCtx to send() method.
+        class ShibHTTPHookCallContext {
+        public:
+            ShibHTTPHookCallContext(const shibsp::PropertySet* credUse, const opensaml::saml2md::RoleDescriptor* role)
+                : m_credUse(credUse), m_role(role), m_hook(NULL), m_authenticated(false) {}
+            const ShibHTTPHook* getHook() {return m_hook;}
+            const shibsp::PropertySet* getCredentialUse() {return m_credUse;}
+            const opensaml::saml2md::RoleDescriptor* getRoleDescriptor() {return m_role;}
+            bool isAuthenticated() const {return m_authenticated;}
+            void setAuthenticated() {m_authenticated=true;}
+            
+        private:
+            const shibsp::PropertySet* m_credUse;
+            const opensaml::saml2md::RoleDescriptor* m_role;
+            ShibHTTPHook* m_hook;
+            bool m_authenticated;
+            friend class ShibHTTPHook;
+        };
+        
+        const xmltooling::TrustEngine* getTrustEngine() const {return m_trust;}
+    private:
+        const xmltooling::TrustEngine* m_trust;
     };
 
-        struct SHIBTARGET_EXPORTS ISessionCacheEntry : public virtual shibboleth::ILockable
+    /**
+     * Interface to a cached user session.
+     * 
+     * Cache entries provide implementations with access to the raw SAML information they
+     * need to publish or provide access to the data for applications to use. All creation
+     * or access to entries is through the ISessionCache interface, and callers must unlock
+     * the entry when finished using it, rather than explicitly freeing them.
+     */
+    struct SHIBTARGET_EXPORTS ISessionCacheEntry : public virtual saml::ILockable
     {
-        virtual bool isValid(time_t lifetime, time_t timeout) const=0;
         virtual const char* getClientAddress() const=0;
-        virtual const char* getSerializedStatement() const=0;
-        virtual const saml::SAMLAuthenticationStatement* getStatement() const=0;
-        virtual void preFetch(int prefetch_window)=0;
-        virtual saml::Iterator<saml::SAMLAssertion*> getAssertions()=0;
+        virtual const char* getProviderId() const=0;
+        virtual std::pair<const char*,const saml::SAMLSubject*> getSubject(bool xml=true, bool obj=false) const=0;
+        virtual const char* getAuthnContext() const=0;
+        virtual std::pair<const char*,const saml::SAMLResponse*> getTokens(bool xml=true, bool obj=false) const=0;
+        virtual std::pair<const char*,const saml::SAMLResponse*> getFilteredTokens(bool xml=true, bool obj=false) const=0;
         virtual ~ISessionCacheEntry() {}
     };
 
-    struct SHIBTARGET_EXPORTS ISessionCache : public virtual shibboleth::IPlugIn
+    /**
+     * Interface to a sink for session cache events.
+     *
+     * All caches support registration of a backing store that can be informed
+     * of significant events in the lifecycle of a cache entry.
+     */
+    struct SHIBTARGET_EXPORTS ISessionCacheStore
     {
-        virtual void thread_init()=0;
-        virtual void thread_end()=0;
-        virtual std::string generateKey() const=0;
-        virtual void insert(
+        virtual HRESULT onCreate(
             const char* key,
             const IApplication* application,
-            saml::SAMLAuthenticationStatement *s,
-            const char* client_addr,
-            saml::SAMLResponse* r=NULL
+            const ISessionCacheEntry* entry,
+            int majorVersion,
+            int minorVersion,
+            time_t created
             )=0;
-        virtual ISessionCacheEntry* find(const char* key)=0;
-        virtual void remove(const char* key)=0;
-        virtual ~ISessionCache() {}
-    };
-
-    struct SHIBTARGET_EXPORTS IConfig : public virtual shibboleth::ILockable, public virtual IPropertySet, public virtual shibboleth::IPlugIn
-    {
-        virtual const IListener* getListener() const=0;
-        virtual ISessionCache* getSessionCache() const=0;
-        virtual IRequestMapper* getRequestMapper() const=0;
-        virtual const IApplication* getApplication(const char* applicationId) const=0;
-        virtual saml::Iterator<shibboleth::ICredentials*> getCredentialsProviders() const=0;
-        virtual ~IConfig() {}
+        virtual HRESULT onRead(
+            const char* key,
+            std::string& applicationId,
+            std::string& clientAddress,
+            std::string& providerId,
+            std::string& subject,
+            std::string& authnContext,
+            std::string& tokens,
+            int& majorVersion,
+            int& minorVersion,
+            time_t& created,
+            time_t& accessed
+            )=0;
+        virtual HRESULT onRead(const char* key, time_t& accessed)=0;
+        virtual HRESULT onRead(const char* key, std::string& tokens)=0;
+        virtual HRESULT onUpdate(const char* key, const char* tokens=NULL, time_t lastAccess=0)=0;
+        virtual HRESULT onDelete(const char* key)=0;
+        virtual ~ISessionCacheStore() {}
     };
 
-    class SHIBTARGET_EXPORTS ShibTargetConfig
+    /**
+     * Interface to the session cache.
+     * 
+     * The session cache abstracts a persistent (meaning across requests) cache of
+     * instances of the ISessionCacheEntry interface. Creation of new entries and entry
+     * lookup are confined to this interface to enable implementations to flexibly
+     * remote and/or optimize calls by implementing custom versions of the
+     * ISessionCacheEntry interface as required.
+     */
+    struct SHIBTARGET_EXPORTS ISessionCache : virtual public shibsp::SessionCache
     {
-    public:
-        ShibTargetConfig() : m_ini(NULL), m_features(0) {}
-        virtual ~ShibTargetConfig() {}
+        virtual std::string insert(
+            const IApplication* application,
+            const opensaml::saml2md::RoleDescriptor* source,
+            const char* client_addr,
+            const saml::SAMLSubject* subject,
+            const char* authnContext,
+            const saml::SAMLResponse* tokens
+            )=0;
+        virtual ISessionCacheEntry* find(
+            const char* key, const IApplication* application, const char* client_addr
+            )=0;
+        virtual void remove(
+            const char* key, const IApplication* application, const char* client_addr
+            )=0;
 
-        virtual bool init(const char* schemadir, const char* config) = 0;
-        virtual void shutdown() = 0;
+        virtual bool setBackingStore(ISessionCacheStore* store)=0;
+        virtual ~ISessionCache() {}
+    };
 
-        enum components_t {
-            Listener = 1,
-            SessionCache = 2,
-            Metadata = 4,
-            Trust = 8,
-            Credentials = 16,
-            AAP = 32,
-            RequestMapper = 64,
-            SHARExtensions = 128,
-            SHIREExtensions = 256
-        };
-        void setFeatures(long enabled) {m_features = enabled;}
-        bool isEnabled(components_t feature) {return (m_features & feature)>0;}
-        virtual IConfig* getINI() const {return m_ini;}
+    #define MEMORY_SESSIONCACHE "edu.internet2.middleware.shibboleth.sp.provider.MemorySessionCacheProvider"
+    #define MYSQL_SESSIONCACHE  "edu.internet2.middleware.shibboleth.sp.provider.MySQLSessionCacheProvider"
+    #define ODBC_SESSIONCACHE   "edu.internet2.middleware.shibboleth.sp.provider.ODBCSessionCacheProvider"
 
-        static const XMLCh SHIBTARGET_NS[];
-        static ShibTargetConfig& getConfig();
+    #define MYSQL_REPLAYCACHE   "edu.internet2.middleware.shibboleth.sp.provider.MySQLReplayCacheProvider"
+    #define ODBC_REPLAYCACHE    "edu.internet2.middleware.shibboleth.sp.provider.ODBCReplayCacheProvider"
 
-    protected:
-        IConfig* m_ini;
-        
-    private:
-        unsigned long m_features;
-    };
 
-    class CgiParse;
-    class SHIBTARGET_EXPORTS SHIRE
+    class SHIBTARGET_EXPORTS ShibTargetConfig
     {
     public:
-        SHIRE(const IApplication* app) : m_app(app), m_parser(NULL) {}
-        ~SHIRE();
-    
-        // Find the default assertion consumer service for the resource
-        const char* getShireURL(const char* resource);
-        
-        // Generate a Shib 1.x AuthnRequest redirect URL for the resource
-        const char* getAuthnRequest(const char* resource);
-        
-        // Process a lazy session setup request and turn it into an AuthnRequest
-        const char* getLazyAuthnRequest(const char* query_string);
-        
-        // Process a POST profile submission, and return (SAMLResponse,TARGET) pair.
-        std::pair<const char*,const char*> getFormSubmission(const char* post);
+        ShibTargetConfig() {}
+        virtual ~ShibTargetConfig() {}
         
-        RPCError* sessionCreate(const char* response, const char* ip, std::string &cookie);
-        RPCError* sessionIsValid(const char* session_id, const char* ip);
-    
-    private:
-        const IApplication* m_app;
-        std::string m_shireURL;
-        std::string m_authnRequest;
-        CgiParse* m_parser;
-    };
-
-    class SHIBTARGET_EXPORTS RM
-    {
-    public:
-        RM(const IApplication* app) : m_app(app) {}
-        ~RM() {}
-    
-        RPCError* getAssertions(
-            const char* cookie,
-            const char* ip,
-            std::vector<saml::SAMLAssertion*>& assertions,
-            saml::SAMLAuthenticationStatement **statement = NULL
-            );
-        static void serialize(saml::SAMLAssertion &assertion, std::string &result);
-    
-    private:
-        const IApplication* m_app;
-    };
-
-    class ShibMLPPriv;
-    class SHIBTARGET_EXPORTS ShibMLP {
-    public:
-        ShibMLP(const IApplication* app=NULL);
-        ~ShibMLP();
-
-        void insert (const std::string& key, const std::string& value);
-        void insert (const std::string& key, const char* value) {
-          std::string v = value;
-          insert (key, v);
-        }
-        void insert (const char* key, const std::string& value) {
-          std::string k = key;
-          insert (k, value);
-        }
-        void insert (const char* key, const char* value) {
-          std::string k = key, v = value;
-          insert(k,v);
-        }
-        void insert (RPCError& e);
-
-        void clear () { m_map.clear(); }
-
-        const char* run (std::istream& s);
-        const char* run (const std::string& input);
-        const char* run (const char* input) {
-            std::string i = input;
-            return run(i);
-        }
-
-    private:
-        ShibMLPPriv *m_priv;
-        std::map<std::string,std::string> m_map;
-        std::string m_generated;
-    };
-
-  class SHIBTARGET_EXPORTS ShibTargetResponse
-  {
-  public:
-    // What to do with the response
-    enum ReturnValue {
-      OK = 0,
-      DECLINED,
-      REDIRECT,
-      INTERNAL_ERROR
-    };
+        virtual bool init(const char* schemadir) = 0;
+        virtual bool load(const char* config) = 0;
+        virtual void shutdown() = 0;
 
-    // How to log the message, if any.  (NONE implies no log_msg)
-    enum LogLevel {
-      NONE = 0,
-      DEBUG,
-      INFO,
-      ERR,
-      CRIT
+        static ShibTargetConfig& getConfig();
     };
 
-    ReturnValue        status;
-    LogLevel   log_level;
-    bool       has_mlp;
-
-    std::string        log_msg;        // message to log if log_level != NONE
-    std::string        redirect_to;    // where to redirect if status == REDIRECT
-    ShibMLP    mlp;            // MLP information if has_mlp == true
-  };
-
-  // The ShibTargetError is used by the high-level SHIRE and RM methods
-  // to notify the handlers of high-level errors.
-
-  class SHIBTARGET_EXPORTS ShibTargetError : public std::exception
-  {
-  public:
-    explicit ShibTargetError(ShibTargetResponse *resp = NULL) { m_resp = resp; }
-    virtual ~ShibTargetError() throw () { if (m_resp) delete m_resp; }
-    virtual const ShibTargetResponse* getError() { return m_resp; }
-
-  private:
-    ShibTargetResponse *m_resp;
-  };
 }
 
 #endif /* SHIB_TARGET_H */