Remove old Sun makefile
[shibboleth/cpp-sp.git] / shib-target / shib-target.h
1 /*
2  * shib-common.h -- top-level header file for the SHIB Common Target Library
3  *
4  * Created by:  Derek Atkins <derek@ihtfp.com>
5  *
6  * $Id$
7  */
8
9 #ifndef SHIB_COMMON_H
10 #define SHIB_COMMON_H
11
12 #include "shibrpc.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 #ifdef WIN32
19
20 #error "Need to define ShibSocket"
21 typedef in_port_t ShibSockName;
22 #define SHIB_SHAR_SOCKET 12345  /* shar portnumber */
23
24 #else  /* UNIX */
25
26 typedef int ShibSocket;
27 typedef char * ShibSockName;
28 #define SHIB_SHAR_SOCKET "/tmp/shar-socket"
29
30 #endif
31
32 /* shib-rpcutil.c */
33
34 /* Create an RPC Client handle for the _connected_ socket sock, attaching
35  * the RPC program and version.
36  *
37  * returns a CLIENT on success, or NULL on error.  The caller can
38  * call clnt_pcreateerror ("<string>") to output an error message from
39  * the RPC library.
40  */
41 CLIENT * shibrpc_client_create (ShibSocket sock, u_long program, u_long version);
42
43
44 /* shib-sock.c */
45
46 /* Create a new socket and put it into sock.
47  *
48  * Returns 0 on success, non-zero on error 
49  */
50 int shib_sock_create (ShibSocket *sock);
51
52 /*
53  * bind the socket s to the "port" name.
54  *
55  * Returns 0 on success; non-zero on error.
56  *
57  * SIDE EFFECT: On error, the socket is closed!
58  */
59 int shib_sock_bind (ShibSocket s, ShibSockName name);
60
61 /*
62  * connect the socket s to the "port" name on the local host.
63  *
64  * Returns 0 on success; non-zero on error.
65  */
66 int shib_sock_connect (ShibSocket s, ShibSockName name);
67
68
69 /* shib-target.cpp */
70
71 /* application names */
72 #define SHIBTARGET_GENERAL      "general"
73 #define SHIBTARGET_SHAR         "shar"
74 #define SHIBTARGET_SHIRE        "shire"
75 #define SHIBTARGET_RM           "rm"
76
77 /* configuration tags */
78 #define SHIBTARGET_TAG_LOGGER   "logger"
79 #define SHIBTARGET_TAG_SCHEMAS  "schemadir"
80 #define SHIBTARGET_TAG_CERTFILE "certfile"
81 #define SHIBTARGET_TAG_KEYFILE  "keyfile"
82 #define SHIBTARGET_TAG_KEYPASS  "keypass"
83 #define SHIBTARGET_TAG_CALIST   "calist"
84
85 #define SHIBTARGET_TAG_SITES    "sitesFile"
86
87 /* initialize and finalize the target library (return 0 on success, 1 on failure) */
88 int shib_target_initialize (const char* application, const char* ini_file);
89 void shib_target_finalize (void);
90
91 #ifdef __cplusplus
92 }
93
94
95 // SAML Runtime
96 #include <saml/saml.h>
97 #include <shib/shib.h>
98
99 namespace shibtarget {
100   class ResourcePriv;
101   class Resource
102   {
103   public:
104     Resource(const char* resource_url);
105     Resource(std::string resource_url);
106     ~Resource();
107
108     const char* getResource();
109     const char* getURL();
110     bool equals(Resource*);
111
112   private:
113     ResourcePriv *m_priv;
114   };
115
116
117   class CCache;
118   class CCacheEntry
119   {
120   public:
121     virtual ~CCacheEntry();
122
123     virtual saml::Iterator<saml::SAMLAssertion*> getAssertions(Resource& resource) = 0;
124     virtual bool isSessionValid(time_t lifetime, time_t timeout) = 0;
125     virtual const char* getClientAddress() = 0;
126
127     static CCacheEntry* getInstance(saml::SAMLAuthenticationStatement *s,
128                                     const char *client_addr);
129
130     friend class CCache;
131   protected:
132     // this should only be called by CCache::insert()
133     virtual void setCache(CCache* cache) = 0;
134   };
135     
136   class CCache
137   {
138   public:
139     virtual ~CCache();
140
141     virtual saml::SAMLBinding* getBinding(const XMLCh* bindingProt) = 0;
142     virtual CCacheEntry* find(const char* key) = 0;
143     virtual void insert(const char* key, CCacheEntry* entry) = 0;
144     virtual void remove(const char* key) = 0;
145     
146     static CCache* getInstance();
147
148   protected:
149     // special function to call over to CCacheEntry::setCache()
150     void setCache(CCacheEntry* entry);
151   };    
152
153   extern CCache* g_shibTargetCCache;
154
155   class RPCHandleInternal;
156   class RPCHandle
157   {
158   public:
159     RPCHandle(ShibSockName shar, u_long program, u_long version);
160     ~RPCHandle();
161
162     CLIENT *    connect(void);
163     void        disconnect(void);
164
165   private:
166     RPCHandleInternal *m_priv;
167   };
168
169   class ShibTargetException : public std::exception
170   {
171   public:
172     explicit ShibTargetException() { m_code = SHIBRPC_OK; m_msg=""; }
173     explicit ShibTargetException(ShibRpcStatus code, const char* msg) { m_code = code; if (msg) m_msg=msg; }
174     explicit ShibTargetException(ShibRpcStatus code, const std::string& msg) : m_msg(msg) { m_code=code; }
175     virtual ~ShibTargetException() throw () {}
176     virtual const char* what() const throw () { return (m_msg.c_str()); }
177     virtual ShibRpcStatus which() const throw () { return (m_code); }
178
179   private:
180     ShibRpcStatus       m_code;
181     std::string         m_msg;
182   };
183
184   class RPCErrorPriv;
185   class RPCError
186   {
187   public:
188     RPCError() { init(0,""); }
189     RPCError(int s, char const* st) { init(s,st); }
190     RPCError(ShibTargetException &exp) { init(exp.which(), exp.what()); }
191     ~RPCError();
192
193     bool        isError() { return (status != 0); }
194     bool        isRetryable();
195
196     // Return a string that corresponds to the "status"
197     const char* toString();
198
199     int         status;
200     std::string error_msg;
201     saml::SAMLException* m_except;
202
203   private:
204     void init(int code, char const* msg);
205     RPCErrorPriv* m_priv;
206   };
207
208   class SHIREConfig
209   {
210   public:
211     bool        checkIPAddress;
212     time_t      lifetime;
213     time_t      timeout;
214   };
215
216   class SHIREPriv;
217   class SHIRE
218   {
219   public:
220     SHIRE(RPCHandle *rpc, SHIREConfig config, std::string shire_url);
221     ~SHIRE();
222
223     RPCError* sessionIsValid(const char* cookie, const char* ip);
224     RPCError* sessionCreate(const char* post, const char* ip,
225                              std::string &cookie);
226   private:
227     SHIREPriv *m_priv;
228   };
229
230   class RMConfig
231   {
232   public:
233     bool        checkIPAddress;
234   };
235
236   class RMPriv;
237   class RM
238   {
239   public:
240     RM(RPCHandle *rpc, RMConfig config);
241     ~RM();
242
243     RPCError* getAssertions(const char* cookie, const char* ip,
244                             const char* url,
245                             std::vector<saml::SAMLAssertion*> &assertions);
246     static void serialize(saml::SAMLAssertion &assertion, std::string &result);
247     static saml::Iterator<saml::SAMLAttribute*> getAttributes(saml::SAMLAssertion &assertion);
248   private:
249     RMPriv *m_priv;
250   };
251
252   class ShibINIPriv;
253   class ShibINI {
254   public:
255     ShibINI (std::string& file, bool case_sensitive = true) { init(file,case_sensitive); }
256     ShibINI (const char *file, bool case_sensitive = true) {
257       std::string f = file;
258       init(f, case_sensitive);
259     }
260     ~ShibINI ();
261
262     void refresh(void);
263
264     const std::string& get (const std::string& header, const std::string& tag);
265     const std::string& get (const char* header, const char* tag) {
266       std::string h = header, t = tag;
267       return get(h,t);
268     }
269
270     const std::string& operator() (const std::string& header, const std::string& tag)  {
271       return get(header,tag);
272     }
273     const std::string& operator() (const char* header, const char* tag) {
274       std::string h = header, t = tag;
275       return get(h,t);
276     }
277
278     bool exists(const std::string& header);
279     bool exists(const std::string& header, const std::string& tag);
280
281     bool exists(const char* header) {
282       std::string s = header;
283       return exists(s);
284     }
285     bool exists(const char* header, const char* tag) {
286       std::string h = header, t = tag;
287       return exists(h,t);
288     }
289
290     // Special method to look for a tag in one header and maybe in the
291     // 'SHIBTARGET_GENERAL' header
292     bool get_tag(std::string& header, std::string& tag, bool try_general,
293                  std::string* result);
294
295     bool get_tag(std::string& header, const char* tag, bool try_general,
296                  std::string* result) {
297       std::string t = tag;
298       return get_tag (header,t,try_general,result);
299     }
300
301     bool get_tag(const char* header, const char* tag, bool try_general,
302                  std::string* result) {
303       std::string h = header, t = tag;
304       return get_tag (h,t,try_general,result);
305     }
306
307     // Dump out the inifile to the output stream
308     void dump(std::ostream& os);
309
310     // Iterators
311
312     // The begin() functions reset the iterator and return the first element
313     // (or 0 if there are no elements.)
314     // The next() functions return the next element, or 0 if there are no
315     // elements left.
316     //
317     // Example:
318     // for (const foo* current = begin(); current; current = next()) {
319     //   ...
320     // }
321
322     class Iterator {
323     public:
324       virtual const std::string* begin() = 0;
325       virtual const std::string* next() = 0;
326     };
327
328     Iterator* header_iterator();
329     Iterator* tag_iterator(const std::string& header);
330
331     static bool boolean(std::string& value);
332
333   private:
334     ShibINIPriv *m_priv;
335     void init(std::string& file, bool case_sensitive);
336   };
337
338   class ShibMLPPriv;
339   class ShibMLP {
340   public:
341     ShibMLP();
342     ~ShibMLP();
343
344     void insert (const std::string& key, const std::string& value) { m_map[key] = value; }
345     void insert (const std::string& key, const char* value) {
346       std::string v = value;
347       insert (key, v);
348     }
349     void insert (const char* key, const std::string& value) {
350       std::string k = key;
351       insert (k, value);
352     }
353     void insert (const char* key, const char* value) {
354       std::string k = key, v = value;
355       insert(k,v);
356     }
357     void insert (RPCError& e);
358
359     void clear () { m_map.clear(); }
360
361     std::string run (std::istream& s) const;
362     std::string run (const std::string& input) const;
363     std::string run (const char* input) const {
364       std::string i = input;
365       return run(i);
366     }
367
368   private:
369     ShibMLPPriv *m_priv;
370     std::map<std::string,std::string> m_map;
371   };
372
373   class ShibTargetConfig
374   {
375   public:
376     static ShibTargetConfig& init(const char* app_name, const char* inifile);
377     virtual void shutdown() = 0;
378     virtual ShibINI& getINI() = 0;
379   };
380
381 } // namespace
382 #endif
383
384 #endif /* SHIB_COMMON_H */