2 * shib-target.h -- top-level header file for the SHIB Common Target Library
4 * Created by: Derek Atkins <derek@ihtfp.com>
12 #include <shib-target/shibrpc.h>
21 typedef SOCKET ShibSocket;
22 typedef u_short ShibSockName;
23 #define SHIB_SHAR_SOCKET 12345 /* shar portnumber */
27 typedef int ShibSocket;
28 typedef char * ShibSockName;
29 #define SHIB_SHAR_SOCKET "/tmp/shar-socket"
35 /* Create an RPC Client handle for the _connected_ socket sock, attaching
36 * the RPC program and version.
38 * returns a CLIENT on success, or NULL on error. The caller can
39 * call clnt_pcreateerror ("<string>") to output an error message from
42 CLIENT * shibrpc_client_create (ShibSocket sock, u_long program, u_long version);
46 /* Create a new socket and put it into sock.
48 * Returns 0 on success, non-zero on error
50 int shib_sock_create (ShibSocket *sock);
53 * bind the socket s to the "port" name.
55 * Returns 0 on success; non-zero on error.
57 * SIDE EFFECT: On error, the socket is closed!
59 int shib_sock_bind (ShibSocket s, ShibSockName name);
62 * connect the socket s to the "port" name on the local host.
64 * Returns 0 on success; non-zero on error.
66 int shib_sock_connect (ShibSocket s, ShibSockName name);
69 * accept a connection. Returns 0 on success, non-zero on failure.
71 int shib_sock_accept (ShibSocket listener, ShibSocket* s);
76 void shib_sock_close (ShibSocket s, ShibSockName name);
80 /* application names */
81 #define SHIBTARGET_GENERAL "general"
82 #define SHIBTARGET_SHAR "shar"
83 #define SHIBTARGET_SHIRE "shire"
84 #define SHIBTARGET_RM "rm"
85 #define SHIBTARGET_POLICIES "policies"
87 /* configuration tags */
88 #define SHIBTARGET_TAG_LOGGER "logger"
89 #define SHIBTARGET_TAG_SCHEMAS "schemadir"
90 #define SHIBTARGET_TAG_CERTFILE "certfile"
91 #define SHIBTARGET_TAG_KEYFILE "keyfile"
92 #define SHIBTARGET_TAG_KEYPASS "keypass"
93 #define SHIBTARGET_TAG_CALIST "calist"
95 #define SHIBTARGET_TAG_SITES "sitesFile"
96 #define SHIBTARGET_TAG_SITESCERT "sitesCertFile"
97 #define SHIBTARGET_TAG_SITESREFRESH "sitesRefresh"
99 #define SHIBTARGET_TAG_CACHETYPE "cacheType"
100 #define SHIBTARGET_TAG_CACHECLEAN "cacheClean"
101 #define SHIBTARGET_TAG_CACHETIMEOUT "cacheTimeout"
103 #define SHIBTARGET_TAG_REQATTRS "requestAttributes"
105 /* initialize and finalize the target library (return 0 on success, 1 on failure) */
106 int shib_target_initialize (const char* application, const char* ini_file);
107 void shib_target_finalize (void);
108 ShibSockName shib_target_sockname(void);
115 #include <saml/saml.h>
116 #include <shib/shib.h>
118 namespace shibtarget {
123 Resource(const char* resource_url);
124 Resource(std::string resource_url);
127 const char* getResource() const;
128 const char* getURL() const;
129 bool equals(Resource*) const;
130 saml::Iterator<saml::SAMLAttribute*> getDesignators() const;
133 ResourcePriv *m_priv;
140 virtual saml::Iterator<saml::SAMLAssertion*> getAssertions(Resource& resource) = 0;
141 virtual void preFetch(Resource& resource, int prefetch_window) = 0;
143 virtual bool isSessionValid(time_t lifetime, time_t timeout) = 0;
144 virtual const char* getClientAddress() = 0;
145 virtual void release() = 0;
151 virtual ~CCache() = 0;
153 virtual saml::SAMLBinding* getBinding(const XMLCh* bindingProt) = 0;
155 // insert() the Auth Statement into the CCache.
157 // Make sure you do not hold any open CCacheEntry objects before
158 // you call this method.
160 virtual void insert(const char* key, saml::SAMLAuthenticationStatement *s,
161 const char *client_addr) = 0;
163 // find() a CCacheEntry in the CCache for the given key.
165 // This returns a LOCKED cache entry. You should release() it
166 // when you are done using it.
168 // Note that you MUST NOT call any other CCache methods while you
169 // are holding this CCacheEntry!
171 virtual CCacheEntry* find(const char* key) = 0;
173 // remove() a key from the CCache
175 // NOTE: If you previously executed a find(), make sure you
176 // "release()" the CCacheEntry before you try to remove it!
178 virtual void remove(const char* key) = 0;
180 // create a CCache instance of the provided type. A NULL type
181 // implies that it should create the default cache type.
183 static CCache* getInstance(const char* type);
186 extern CCache* g_shibTargetCCache;
188 class RPCHandleInternal;
192 RPCHandle(ShibSockName shar, u_long program, u_long version);
195 CLIENT * connect(void); /* locks the HANDLE and returns the CLIENT */
196 void release(void); /* unlocks the HANDLE */
197 void disconnect(void); /* disconnects */
200 RPCHandleInternal *m_priv;
203 class ShibTargetException : public std::exception
206 explicit ShibTargetException() { m_code = SHIBRPC_OK; m_msg=""; }
207 explicit ShibTargetException(ShibRpcStatus code, const char* msg) { m_code = code; if (msg) m_msg=msg; }
208 explicit ShibTargetException(ShibRpcStatus code, const std::string& msg) : m_msg(msg) { m_code=code; }
209 virtual ~ShibTargetException() throw () {}
210 virtual const char* what() const throw () { return (m_msg.c_str()); }
211 virtual ShibRpcStatus which() const throw () { return (m_code); }
214 ShibRpcStatus m_code;
222 RPCError() { init(0,""); }
223 RPCError(int s, char const* st) { init(s,st); }
224 RPCError(ShibTargetException &exp) { init(exp.which(), exp.what()); }
227 bool isError() { return (status != 0); }
230 // Return a string that corresponds to the "status"
231 const char* toString();
234 std::string error_msg;
235 saml::SAMLException* m_except;
238 void init(int code, char const* msg);
239 RPCErrorPriv* m_priv;
254 SHIRE(RPCHandle *rpc, SHIREConfig config, std::string shire_url);
257 RPCError* sessionIsValid(const char* cookie, const char* ip, const char* url);
258 RPCError* sessionCreate(const char* post, const char* ip,
259 std::string &cookie);
274 RM(RPCHandle *rpc, RMConfig config);
277 RPCError* getAssertions(const char* cookie, const char* ip,
279 std::vector<saml::SAMLAssertion*> &assertions);
280 static void serialize(saml::SAMLAssertion &assertion, std::string &result);
281 static saml::Iterator<saml::SAMLAttribute*> getAttributes(saml::SAMLAssertion &assertion);
289 ShibINI (std::string& file, bool case_sensitive = true) { init(file,case_sensitive); }
290 ShibINI (const char *file, bool case_sensitive = true) {
291 std::string f = file;
292 init(f, case_sensitive);
298 const std::string get (const std::string& header, const std::string& tag);
299 const std::string get (const char* header, const char* tag) {
300 std::string h = header, t = tag;
304 const std::string operator() (const std::string& header, const std::string& tag) {
305 return get(header,tag);
307 const std::string operator() (const char* header, const char* tag) {
308 std::string h = header, t = tag;
312 bool exists(const std::string& header);
313 bool exists(const std::string& header, const std::string& tag);
315 bool exists(const char* header) {
316 std::string s = header;
319 bool exists(const char* header, const char* tag) {
320 std::string h = header, t = tag;
324 // Special method to look for a tag in one header and maybe in the
325 // 'SHIBTARGET_GENERAL' header
326 bool get_tag(std::string& header, std::string& tag, bool try_general,
327 std::string* result);
329 bool get_tag(std::string& header, const char* tag, bool try_general,
330 std::string* result) {
332 return get_tag (header,t,try_general,result);
335 bool get_tag(const char* header, const char* tag, bool try_general,
336 std::string* result) {
337 std::string h = header, t = tag;
338 return get_tag (h,t,try_general,result);
341 // Dump out the inifile to the output stream
342 void dump(std::ostream& os);
346 // The begin() functions reset the iterator and return the first element
347 // (or 0 if there are no elements.)
348 // The next() functions return the next element, or 0 if there are no
352 // for (const foo* current = begin(); current; current = next()) {
356 // NOTE: Holding an Iterator will lock the INI file and cause it to
357 // stop updating itself. You should destroy the iterator as soon as
358 // you are done with it.
360 // ALSO NOTE: the string* returned from the Iterator is only valid
361 // while you hold the iterator. You should copy the de-reference
362 // of the pointer to your own copy if you want to keep the string.
366 virtual const std::string* begin() = 0;
367 virtual const std::string* next() = 0;
370 Iterator* header_iterator();
371 Iterator* tag_iterator(const std::string& header);
373 static bool boolean(std::string& value);
377 void init(std::string& file, bool case_sensitive);
386 void insert (const std::string& key, const std::string& value) { m_map[key] = value; }
387 void insert (const std::string& key, const char* value) {
388 std::string v = value;
391 void insert (const char* key, const std::string& value) {
395 void insert (const char* key, const char* value) {
396 std::string k = key, v = value;
399 void insert (RPCError& e);
401 void clear () { m_map.clear(); }
403 std::string run (std::istream& s) const;
404 std::string run (const std::string& input) const;
405 std::string run (const char* input) const {
406 std::string i = input;
412 std::map<std::string,std::string> m_map;
415 class ShibTargetConfig
418 static void preinit();
419 static ShibTargetConfig& init(const char* app_name, const char* inifile);
420 static ShibTargetConfig& getConfig();
421 virtual void shutdown() = 0;
422 virtual ~ShibTargetConfig();
423 virtual ShibINI& getINI() = 0;
424 virtual saml::Iterator<const XMLCh*> getPolicies() = 0;
426 ShibSockName m_SocketName;
432 #endif /* SHIB_TARGET_H */