Baby steps. First replacement of old code, link to new libs.
[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 #include <saml/base.h>
29
30 #ifdef WIN32
31 # define SHIBTARGET_EXPORTS __declspec(dllexport)
32 #endif
33
34 // eventually we might be able to support autoconf via cygwin...
35 #if defined (_MSC_VER) || defined(__BORLANDC__)
36 # include "config_win32.h"
37 #else
38 # include "config.h"
39 #endif
40
41 #include "shib-target.h"
42 #include "hresult.h"
43
44 #include <log4cpp/Category.hh>
45 #include <log4cpp/FixedContextCategory.hh>
46
47 #define SHIBT_L(s) shibtarget::XML::Literals::s
48 #define SHIBT_L_QNAME(p,s) shibtarget::XML::Literals::p##_##s
49 #define SHIBT_LOGCAT "shibtarget"
50 #define SHIBTRAN_LOGCAT "Shibboleth-TRANSACTION"
51
52 // Controls default logging level of console tools and other situations
53 // where full shibboleth.xml-based logging isn't used.
54 #define SHIB_LOGGING "WARN"
55
56 namespace shibtarget {
57     // Generic class, which handles the IPropertySet configuration interface.
58     // Most of the basic configuration details are exposed via this interface.
59     // This implementation extracts the XML tree structure and caches it in a map
60     // with the attributes stored in the various possible formats they might be fetched.
61     // Elements are treated as nested IPropertySets.
62     // The "trick" to this is to pass in an "exclude list" using a DOMNodeFilter. Nested
63     // property sets are extracted by running a TreeWalker againt the filter for the
64     // immediate children. The filter should skip any excluded elements that will be
65     // processed separately.
66     class XMLPropertySet : public virtual IPropertySet
67     {
68     public:
69         XMLPropertySet() {}
70         ~XMLPropertySet();
71
72         std::pair<bool,bool> getBool(const char* name, const char* ns=NULL) const;
73         std::pair<bool,const char*> getString(const char* name, const char* ns=NULL) const;
74         std::pair<bool,const XMLCh*> getXMLString(const char* name, const char* ns=NULL) const;
75         std::pair<bool,unsigned int> getUnsignedInt(const char* name, const char* ns=NULL) const;
76         std::pair<bool,int> getInt(const char* name, const char* ns=NULL) const;
77         const IPropertySet* getPropertySet(const char* name, const char* ns="urn:mace:shibboleth:target:config:1.0") const;
78         const DOMElement* getElement() const {return m_root;}
79     
80         void load(
81             const DOMElement* e,    // root element of property set
82             log4cpp::Category& log, // log object for tracing
83             DOMNodeFilter* filter,  // control what subelements to include
84             const std::map<std::string,std::string>* remapper=NULL   // on the fly property renaming for legacy support
85             );
86
87     private:
88         const DOMElement* m_root;
89         std::map<std::string,std::pair<char*,const XMLCh*> > m_map;
90         std::map<std::string,IPropertySet*> m_nested;
91     };
92
93     // ST-aware class that maps SAML artifacts to appropriate binding information
94     class STArtifactMapper : public virtual saml::SAMLBrowserProfile::ArtifactMapper
95     {
96     public:
97         STArtifactMapper(const IApplication* application) : m_app(application) {}
98         virtual ~STArtifactMapper() {}
99         saml::SAMLResponse* resolve(saml::SAMLRequest* request);
100     
101     private:
102         const IApplication* m_app;
103     };
104
105     // Error template class
106     class ShibMLPPriv;
107     class ShibMLP {
108     public:
109         ShibMLP();
110         ~ShibMLP();
111
112         void insert (const std::string& key, const std::string& value);
113         void insert (const std::string& key, const char* value) {
114           std::string v = value;
115           insert (key, v);
116         }
117         void insert (const char* key, const std::string& value) {
118           std::string k = key;
119           insert (k, value);
120         }
121         void insert (const char* key, const char* value) {
122           std::string k = key, v = value;
123           insert(k,v);
124         }
125         void insert (saml::SAMLException& e);
126
127         void clear () { m_map.clear(); }
128
129         const char* run (std::istream& s, const IPropertySet* props=NULL, std::string* output=NULL);
130         const char* run (const std::string& input, const IPropertySet* props=NULL, std::string* output=NULL);
131         const char* run (const char* input, const IPropertySet* props=NULL, std::string* output=NULL) {
132             std::string i = input;
133             return run(i,props,output);
134         }
135
136     private:
137         ShibMLPPriv *m_priv;
138         std::map<std::string,std::string> m_map;
139         std::string m_generated;
140     };
141     
142     class STConfig : public ShibTargetConfig
143     {
144     public:
145         STConfig() : m_tranLog(NULL), m_tranLogLock(NULL) {}
146         ~STConfig() {}
147         
148         bool init(const char* schemadir);
149         bool load(const char* config);
150         void shutdown();
151
152         log4cpp::Category& getTransactionLog() { m_tranLogLock->lock(); return *m_tranLog; }
153         void releaseTransactionLog() { m_tranLogLock->unlock();}
154     private:
155         log4cpp::FixedContextCategory* m_tranLog;
156         shibboleth::Mutex* m_tranLogLock;
157         static IConfig* ShibTargetConfigFactory(const DOMElement* e);
158     };
159 }
160
161 #endif