Missing header.
[shibboleth/cpp-sp.git] / shib-target / internal.h
1 /*
2  *  Copyright 2001-2005 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /* internal.h - internally visible declarations
18
19    Scott Cantor
20    6/29/03
21
22    $History:$
23 */
24
25 #ifndef __shibtarget_internal_h__
26 #define __shibtarget_internal_h__
27
28 #ifdef WIN32
29 # define SHIBTARGET_EXPORTS __declspec(dllexport)
30 #endif
31
32 // eventually we might be able to support autoconf via cygwin...
33 #if defined (_MSC_VER) || defined(__BORLANDC__)
34 # include "config_win32.h"
35 #else
36 # include "config.h"
37 #endif
38
39 #include "shib-target.h"
40 #include "hresult.h"
41
42 #if defined(HAVE_LOG4SHIB)
43 # include <log4shib/Category.hh>
44 # include <log4shib/CategoryStream.hh>
45 # include <log4shib/FixedContextCategory.hh>
46 namespace shibtarget {
47     namespace logging = log4shib;
48 };
49 #elif defined(HAVE_LOG4CPP)
50 # include <log4cpp/Category.hh>
51 # include <log4cpp/CategoryStream.hh>
52 # include <log4cpp/FixedContextCategory.hh>
53 namespace shibtarget {
54     namespace logging = log4cpp;
55 };
56 #else
57 # error "Supported logging library not available."
58 #endif
59
60 #define SHIBT_L(s) shibtarget::XML::Literals::s
61 #define SHIBT_L_QNAME(p,s) shibtarget::XML::Literals::p##_##s
62 #define SHIBT_LOGCAT "shibtarget"
63 #define SHIBTRAN_LOGCAT "Shibboleth-TRANSACTION"
64
65 // Controls default logging level of console tools and other situations
66 // where full shibboleth.xml-based logging isn't used.
67 #define SHIB_LOGGING "WARN"
68
69 namespace shibtarget {
70
71     class RPCHandlePool;
72     class RPCListener : public virtual IListener
73     {
74     public:
75         RPCListener(const DOMElement* e);
76         ~RPCListener();
77
78         void sessionNew(
79             const IApplication* application,
80             int supported_profiles,
81             const char* recipient,
82             const char* packet,
83             const char* ip,
84             std::string& target,
85             std::string& cookie,
86             std::string& provider_id
87             ) const;
88     
89         void sessionGet(
90             const IApplication* application,
91             const char* cookie,
92             const char* ip,
93             ISessionCacheEntry** pentry
94             ) const;
95     
96         void sessionEnd(
97             const IApplication* application,
98             const char* cookie
99         ) const;
100         
101         void ping(int& i) const;
102
103         // Implemented by socket-specific subclasses. Return type must be ONC CLIENT*
104         virtual void* getClientHandle(ShibSocket& s, u_long program, u_long version) const=0;
105
106     protected:
107         logging::Category* log;
108     
109     private:
110         mutable RPCHandlePool* m_rpcpool;
111     };
112
113     // Generic class, which handles the IPropertySet configuration interface.
114     // Most of the basic configuration details are exposed via this interface.
115     // This implementation extracts the XML tree structure and caches it in a map
116     // with the attributes stored in the various possible formats they might be fetched.
117     // Elements are treated as nested IPropertySets.
118     // The "trick" to this is to pass in an "exclude list" using a DOMNodeFilter. Nested
119     // property sets are extracted by running a TreeWalker againt the filter for the
120     // immediate children. The filter should skip any excluded elements that will be
121     // processed separately.
122     class XMLPropertySet : public virtual IPropertySet
123     {
124     public:
125         XMLPropertySet() {}
126         ~XMLPropertySet();
127
128         std::pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
129         std::pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
130         std::pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
131         std::pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
132         std::pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
133         const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
134         const DOMElement* getElement() const {return m_root;}
135     
136         void load(
137             const DOMElement* e,    // root element of property set
138             logging::Category& log, // log object for tracing
139             DOMNodeFilter* filter,  // control what subelements to include
140             const std::map<std::string,std::string>* remapper=NULL   // on the fly property renaming for legacy support
141             );
142
143     private:
144         const DOMElement* m_root;
145         std::map<std::string,std::pair<char*,const XMLCh*> > m_map;
146         std::map<std::string,IPropertySet*> m_nested;
147     };
148
149     // ST-aware class that maps SAML artifacts to appropriate binding information
150     class STArtifactMapper : public virtual saml::SAMLBrowserProfile::ArtifactMapper
151     {
152     public:
153         STArtifactMapper(const IApplication* application) : m_app(application) {}
154         virtual ~STArtifactMapper() {}
155         saml::SAMLResponse* resolve(saml::SAMLRequest* request);
156     
157     private:
158         const IApplication* m_app;
159     };
160
161     // Error template class
162     class ShibMLPPriv;
163     class ShibMLP {
164     public:
165         ShibMLP();
166         ~ShibMLP();
167
168         void insert (const std::string& key, const std::string& value);
169         void insert (const std::string& key, const char* value) {
170           std::string v = value;
171           insert (key, v);
172         }
173         void insert (const char* key, const std::string& value) {
174           std::string k = key;
175           insert (k, value);
176         }
177         void insert (const char* key, const char* value) {
178           std::string k = key, v = value;
179           insert(k,v);
180         }
181         void insert (saml::SAMLException& e);
182
183         void clear () { m_map.clear(); }
184
185         const char* run (std::istream& s, const IPropertySet* props=NULL, std::string* output=NULL);
186         const char* run (const std::string& input, const IPropertySet* props=NULL, std::string* output=NULL);
187         const char* run (const char* input, const IPropertySet* props=NULL, std::string* output=NULL) {
188             std::string i = input;
189             return run(i,props,output);
190         }
191
192     private:
193         ShibMLPPriv *m_priv;
194         std::map<std::string,std::string> m_map;
195         std::string m_generated;
196     };
197     
198     class STConfig : public ShibTargetConfig
199     {
200     public:
201         STConfig() : m_tranLog(NULL), m_tranLogLock(NULL) {}
202         ~STConfig() {}
203         
204         bool init(const char* schemadir);
205         bool load(const char* config);
206         void shutdown();
207         
208         logging::Category& getTransactionLog() { m_tranLogLock->lock(); return *m_tranLog; }
209         void releaseTransactionLog() { m_tranLogLock->unlock();}
210     private:
211         logging::FixedContextCategory* m_tranLog;
212         shibboleth::Mutex* m_tranLogLock;
213         static IConfig* ShibTargetConfigFactory(const DOMElement* e);
214     };
215 }
216
217 #endif