2 * The Shibboleth License, Version 1.
4 * University Corporation for Advanced Internet Development, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution, if any, must include
17 * the following acknowledgment: "This product includes software developed by
18 * the University Corporation for Advanced Internet Development
19 * <http://www.ucaid.edu>Internet2 Project. Alternately, this acknowledegement
20 * may appear in the software itself, if and wherever such third-party
21 * acknowledgments normally appear.
23 * Neither the name of Shibboleth nor the names of its contributors, nor
24 * Internet2, nor the University Corporation for Advanced Internet Development,
25 * Inc., nor UCAID may be used to endorse or promote products derived from this
26 * software without specific prior written permission. For written permission,
27 * please contact shibboleth@shibboleth.org
29 * Products derived from this software may not be called Shibboleth, Internet2,
30 * UCAID, or the University Corporation for Advanced Internet Development, nor
31 * may Shibboleth appear in their name, without prior written permission of the
32 * University Corporation for Advanced Internet Development.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
36 * AND WITH ALL FAULTS. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
38 * PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED AND THE ENTIRE RISK
39 * OF SATISFACTORY QUALITY, PERFORMANCE, ACCURACY, AND EFFORT IS WITH LICENSEE.
40 * IN NO EVENT SHALL THE COPYRIGHT OWNER, CONTRIBUTORS OR THE UNIVERSITY
41 * CORPORATION FOR ADVANCED INTERNET DEVELOPMENT, INC. BE LIABLE FOR ANY DIRECT,
42 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 * shib-target.h -- top-level header file for the SHIB Common Target Library
53 * Created by: Derek Atkins <derek@ihtfp.com>
62 # include <saml/saml.h>
63 # include <shib/shib.h>
64 # include <shib/shib-threads.h>
68 # ifndef SHIBTARGET_EXPORTS
69 # define SHIBTARGET_EXPORTS __declspec(dllimport)
72 # define SHIBTARGET_EXPORTS
75 #include <shib-target/shibrpc.h>
84 typedef SOCKET ShibSocket;
85 #define SHIB_SHAR_SOCKET "127.0.0.1:12345" /* TCP host:port */
89 typedef int ShibSocket;
90 #define SHIB_SHAR_SOCKET "/tmp/shar-socket" /* Unix domain socket */
96 /* Create an RPC Client handle for the _connected_ socket sock, attaching
97 * the RPC program and version.
99 * returns a CLIENT on success, or NULL on error. The caller can
100 * call clnt_pcreateerror ("<string>") to output an error message from
103 SHIBTARGET_EXPORTS CLIENT * shibrpc_client_create (ShibSocket sock, u_long program, u_long version);
107 /* Create a new socket and put it into sock.
109 * Returns 0 on success, non-zero on error
111 SHIBTARGET_EXPORTS int shib_sock_create (ShibSocket *sock);
114 * bind the socket s to the "port" name.
116 * Returns 0 on success; non-zero on error.
118 * SIDE EFFECT: On error, the socket is closed!
120 SHIBTARGET_EXPORTS int shib_sock_bind (ShibSocket s, const char* name);
123 * connect the socket s to the "port" name on the local host.
125 * Returns 0 on success; non-zero on error.
127 SHIBTARGET_EXPORTS int shib_sock_connect (ShibSocket s, const char* name);
130 * accept a connection. Returns 0 on success, non-zero on failure.
132 SHIBTARGET_EXPORTS int shib_sock_accept (ShibSocket listener, ShibSocket* s);
137 SHIBTARGET_EXPORTS void shib_sock_close (ShibSocket s, const char* name);
139 /* shib-target.cpp */
141 /* application names */
142 #define SHIBTARGET_GENERAL "general"
143 #define SHIBTARGET_SHAR "shar"
144 #define SHIBTARGET_SHIRE "shire"
145 #define SHIBTARGET_RM "rm"
146 #define SHIBTARGET_POLICIES "policies"
148 /* configuration tags */
149 #define SHIBTARGET_TAG_LOGGER "logger"
150 #define SHIBTARGET_TAG_SCHEMAS "schemadir"
151 #define SHIBTARGET_TAG_CERTFILE "certfile"
152 #define SHIBTARGET_TAG_KEYFILE "keyfile"
153 #define SHIBTARGET_TAG_KEYPASS "keypass"
154 #define SHIBTARGET_TAG_CALIST "calist"
156 #define SHIBTARGET_TAG_AATIMEOUT "AATimeout"
157 #define SHIBTARGET_TAG_AACONNECTTO "AAConnectTimeout"
158 #define SHIBTARGET_TAG_SAMLCOMPAT "SAMLCompat"
160 #define SHIBTARGET_TAG_METADATA "metadata"
161 #define SHIBTARGET_TAG_REVOCATION "revocation"
162 #define SHIBTARGET_TAG_TRUST "trust"
163 #define SHIBTARGET_TAG_CREDS "credentials"
164 #define SHIBTARGET_TAG_AAP "aap"
165 #define SHIBTARGET_TAG_APPMAPPER "applicationMap"
167 #define SHIBTARGET_TAG_CACHETYPE "cacheType"
168 #define SHIBTARGET_TAG_CACHECLEAN "cacheClean"
169 #define SHIBTARGET_TAG_CACHETIMEOUT "cacheTimeout"
171 #define SHIBTARGET_TAG_REQATTRS "requestAttributes"
173 /* initialize and finalize the target library (return 0 on success, 1 on failure) */
174 SHIBTARGET_EXPORTS int shib_target_initialize (const char* application, const char* ini_file);
175 SHIBTARGET_EXPORTS void shib_target_finalize (void);
177 /* access socket specifics from C code */
178 SHIBTARGET_EXPORTS const char* shib_target_sockname(void);
179 SHIBTARGET_EXPORTS const char* shib_target_sockacl(unsigned int index);
185 namespace shibtarget {
187 class SHIBTARGET_EXPORTS ShibTargetException : public std::exception
190 explicit ShibTargetException() : m_origin(NULL), m_code(SHIBRPC_OK) {}
191 explicit ShibTargetException(ShibRpcStatus code, const char* msg, const XMLCh* origin = NULL)
192 : m_code(code), m_origin(XMLString::replicate(origin)) { if (msg) m_msg=msg; }
193 explicit ShibTargetException(ShibRpcStatus code, const std::string& msg, const XMLCh* origin = NULL)
194 : m_code(code), m_msg(msg), m_origin(XMLString::replicate(origin)) {}
195 ShibTargetException(const ShibTargetException& src)
196 : m_code(src.m_code), m_msg(src.m_msg), m_origin(XMLString::replicate(src.m_origin)) {}
198 virtual ~ShibTargetException() throw () { if (m_origin) XMLString::release(&m_origin); }
199 virtual const char* what() const throw () { return (m_msg.c_str()); }
200 virtual ShibRpcStatus which() const throw () { return (m_code); }
201 virtual const XMLCh* where() const throw () { return m_origin; }
204 ShibRpcStatus m_code;
210 class SHIBTARGET_EXPORTS RPCError
213 RPCError() { init(0, "", NULL); }
214 RPCError(ShibRpcError* error);
215 RPCError(int s, char const* st, const XMLCh* orig = NULL) { init (s,st,orig); }
216 RPCError(ShibTargetException &exp) { init(exp.which(), exp.what(), exp.where()); }
222 // Return a set of strings that corresponds to the type, text, and desc
223 const char* getType();
224 const char* getText();
225 const char* getDesc();
226 std::string getOriginErrorURL();
227 std::string getOriginContactName();
228 std::string getOriginContactEmail();
232 void init(int stat, char const* msg, const XMLCh* origin);
233 RPCErrorPriv* m_priv;
236 // The ShibTargetError is used by the high-level SHIRE and RM methods
237 // to notify the handlers of high-level errors.
240 class SHIBTARGET_EXPORTS ShibMLP {
245 void insert (const std::string& key, const std::string& value);
246 void insert (const std::string& key, const char* value) {
247 std::string v = value;
250 void insert (const char* key, const std::string& value) {
254 void insert (const char* key, const char* value) {
255 std::string k = key, v = value;
258 void insert (RPCError& e);
260 void clear () { m_map.clear(); }
262 std::string run (std::istream& s) const;
263 std::string run (const std::string& input) const;
264 std::string run (const char* input) const {
265 std::string i = input;
271 std::map<std::string,std::string> m_map;
274 class SHIBTARGET_EXPORTS ShibTargetResponse
277 // What to do with the response
285 // How to log the message, if any. (NONE implies no log_msg)
298 std::string log_msg; // message to log if log_level != NONE
299 std::string redirect_to; // where to redirect if status == REDIRECT
300 ShibMLP mlp; // MLP information if has_mlp == true
303 class SHIBTARGET_EXPORTS ShibTargetError : public std::exception
306 explicit ShibTargetError(ShibTargetResponse *resp = NULL) { m_resp = resp; }
307 virtual ~ShibTargetError() throw () { if (m_resp) delete m_resp; }
308 virtual const ShibTargetResponse* getError() { return m_resp; }
311 ShibTargetResponse *m_resp;
314 class SHIBTARGET_EXPORTS SHIREConfig
323 class SHIBTARGET_EXPORTS ShibINI;
324 class SHIBTARGET_EXPORTS SHIRE
327 SHIRE(SHIREConfig config, const char* shire_url);
330 RPCError* sessionIsValid(const char* cookie, const char* ip, const char* application_id);
331 RPCError* sessionCreate(const char* post, const char* ip, const char* application_id, std::string &cookie);
337 class SHIBTARGET_EXPORTS RMConfig
344 class SHIBTARGET_EXPORTS RM
350 RPCError* getAssertions(const char* cookie, const char* ip, const char* application_id,
351 std::vector<saml::SAMLAssertion*> &assertions,
352 saml::SAMLAuthenticationStatement **statement = NULL);
353 static void serialize(saml::SAMLAssertion &assertion, std::string &result);
359 class SHIBTARGET_EXPORTS ShibINI {
361 ShibINI (std::string& file, bool case_sensitive = true) { init(file,case_sensitive); }
362 ShibINI (const char *file, bool case_sensitive = true) {
363 std::string f = file;
364 init(f, case_sensitive);
370 const std::string get (const std::string& header, const std::string& tag);
371 const std::string get (const char* header, const char* tag) {
372 std::string h = header, t = tag;
376 const std::string operator() (const std::string& header, const std::string& tag) {
377 return get(header,tag);
379 const std::string operator() (const char* header, const char* tag) {
380 std::string h = header, t = tag;
384 bool exists(const std::string& header);
385 bool exists(const std::string& header, const std::string& tag);
387 bool exists(const char* header) {
388 std::string s = header;
391 bool exists(const char* header, const char* tag) {
392 std::string h = header, t = tag;
396 // Special method to look for a tag in one header and maybe in the
397 // 'SHIBTARGET_GENERAL' header
398 bool get_tag(std::string& header, std::string& tag, bool try_general,
399 std::string* result);
401 bool get_tag(std::string& header, const char* tag, bool try_general,
402 std::string* result) {
404 return get_tag (header,t,try_general,result);
407 bool get_tag(const char* header, const char* tag, bool try_general,
408 std::string* result) {
409 std::string h = header, t = tag;
410 return get_tag (h,t,try_general,result);
413 // Dump out the inifile to the output stream
414 void dump(std::ostream& os);
418 // The begin() functions reset the iterator and return the first element
419 // (or 0 if there are no elements.)
420 // The next() functions return the next element, or 0 if there are no
424 // for (const foo* current = begin(); current; current = next()) {
428 // NOTE: Holding an Iterator will lock the INI file and cause it to
429 // stop updating itself. You should destroy the iterator as soon as
430 // you are done with it.
432 // ALSO NOTE: the string* returned from the Iterator is only valid
433 // while you hold the iterator. You should copy the de-reference
434 // of the pointer to your own copy if you want to keep the string.
436 class SHIBTARGET_EXPORTS Iterator {
438 virtual ~Iterator() {}
439 virtual const std::string* begin() = 0;
440 virtual const std::string* next() = 0;
443 Iterator* header_iterator();
444 Iterator* tag_iterator(const std::string& header);
446 static bool boolean(std::string& value);
450 void init(std::string& file, bool case_sensitive);
453 // Abstract API to map URLs to application names
454 struct SHIBTARGET_EXPORTS IApplicationMapper : public virtual shibboleth::ILockable
456 virtual const char* getApplicationFromURL(const char* url) const=0;
457 virtual const XMLCh* getXMLChApplicationFromURL(const char* url) const=0;
458 virtual const char* getApplicationFromParsedURL(
459 const char* scheme, const char* hostname, unsigned int port, const char* path=NULL
461 virtual const XMLCh* getXMLChApplicationFromParsedURL(
462 const char* scheme, const char* hostname, unsigned int port, const char* path=NULL
464 virtual ~IApplicationMapper() {}
467 // A helper class to wrap the lock/unlock sequence.
468 class SHIBTARGET_EXPORTS ApplicationMapper
472 ~ApplicationMapper() {if (m_mapper) m_mapper->unlock();}
473 const IApplicationMapper* operator->() const {return m_mapper;}
474 operator const IApplicationMapper*() const {return m_mapper;}
477 ApplicationMapper(const ApplicationMapper&);
478 void operator=(const ApplicationMapper&);
479 IApplicationMapper* m_mapper;
482 class SHIBTARGET_EXPORTS ShibTargetConfig
485 static void preinit();
486 static ShibTargetConfig& init(const char* app_name, const char* inifile);
487 static ShibTargetConfig& getConfig();
488 virtual void init() = 0;
489 virtual void shutdown() = 0;
490 virtual shibtarget::ShibINI& getINI() const = 0;
491 virtual IApplicationMapper* getApplicationMapper() const = 0;
492 virtual saml::Iterator<shibboleth::IMetadata*> getMetadataProviders() const = 0;
493 virtual saml::Iterator<shibboleth::IRevocation*> getRevocationProviders() const = 0;
494 virtual saml::Iterator<shibboleth::ITrust*> getTrustProviders() const = 0;
495 virtual saml::Iterator<shibboleth::ICredentials*> getCredentialProviders() const = 0;
496 virtual saml::Iterator<shibboleth::IAAP*> getAAPProviders() const = 0;
497 virtual ~ShibTargetConfig() {}
503 #endif /* SHIB_TARGET_H */