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.
50 /* XMLApplicationMapper.cpp - an XML-based config file for mapping URLs to application names
60 #include <sys/types.h>
63 #include <log4cpp/Category.hh>
64 #include <xercesc/framework/URLInputSource.hpp>
66 namespace shibtarget {
68 class XMLApplicationMapperImpl : public ReloadableXMLFileImpl
71 XMLApplicationMapperImpl(const char* pathname);
72 ~XMLApplicationMapperImpl();
76 Override(const XMLCh* AppID) : m_XMLChAppID((AppID && *AppID) ? AppID : NULL),
77 m_AppID((AppID && *AppID) ? AppID : NULL) {}
79 const Override* locate(const char* path) const;
80 auto_ptr_char m_AppID;
81 const XMLCh* m_XMLChAppID;
82 map<string,Override*> m_map;
85 const Override* findOverride(const char* vhost, const char* path) const;
87 map<string,Override*> m_map;
88 map<string,Override*> m_extras;
93 Override* buildOverride(const XMLCh* appID, DOMElement* root, Category& log);
97 ReloadableXMLFileImpl* XMLApplicationMapper::newImplementation(const char* pathname) const
99 return new XMLApplicationMapperImpl(pathname);
102 XMLApplicationMapperImpl::Override::~Override()
104 for (map<string,Override*>::iterator i=m_map.begin(); i!=m_map.end(); i++)
108 XMLApplicationMapperImpl::XMLApplicationMapperImpl(const char* pathname) : ReloadableXMLFileImpl(pathname)
110 NDC ndc("XMLApplicationMapperImpl");
111 log=&Category::getInstance("shibtarget.XMLApplicationMapper");
115 DOMElement* e = m_doc->getDocumentElement();
116 if (XMLString::compareString(shibtarget::XML::APPMAP_NS,e->getNamespaceURI()) ||
117 XMLString::compareString(shibtarget::XML::Literals::ApplicationMap,e->getLocalName()))
119 log->error("Construction requires a valid app mapping file: (appmap:ApplicationMap as root element)");
120 throw MetadataException("Construction requires a valid app mapping file: (appmap:ApplicationMap as root element)");
123 // Loop over the Host elements.
124 DOMNodeList* nlist = e->getElementsByTagNameNS(shibtarget::XML::APPMAP_NS,shibtarget::XML::Literals::Host);
125 for (int i=0; nlist && i<nlist->getLength(); i++)
127 DOMElement* host=static_cast<DOMElement*>(nlist->item(i));
128 const XMLCh* scheme=host->getAttributeNS(NULL,shibtarget::XML::Literals::Scheme);
129 const XMLCh* port=host->getAttributeNS(NULL,shibtarget::XML::Literals::Port);
130 auto_ptr_XMLCh name(host->getAttributeNS(NULL,shibboleth::XML::Literals::Name));
132 if (!name.get() || !*(name.get()))
134 log->warn("Skipping Host element (%d) with empty Name attribute",i);
137 XMLString::lowerCase(const_cast<XMLCh*>(name.get()));
139 Override* o=buildOverride(host->getAttributeNS(NULL,shibtarget::XML::Literals::ApplicationID),host,*log);
141 auto_ptr_char s(scheme);
142 auto_ptr_char n(name.get());
143 auto_ptr_char p(port);
144 string url((s.get() && *(s.get())) ? s.get() : "http");
145 url=url + "://" + n.get();
146 if (p.get()==NULL || *(p.get())=='\0')
148 // First store a port-less version.
149 if (m_map.count(url))
151 log->warn("Skipping duplicate Host element (%s)",url.c_str());
157 // Now append the default port.
158 // XXX Use getservbyname instead?
159 if (s.get()==NULL || !strcmp(s.get(),"http"))
161 else if (!strcmp(s.get(),"https"))
163 else if (!strcmp(s.get(),"ftp"))
165 else if (!strcmp(s.get(),"ldap"))
167 else if (!strcmp(s.get(),"ldaps"))
174 url=url + ':' + p.get();
175 if (m_map.count(url))
177 log->warn("Skipping duplicate Host element (%s)",url.c_str());
183 log->debug("Added <Host> mapping for %s",url.c_str());
186 catch (SAMLException& e)
188 log->errorStream() << "Error while parsing app mapping configuration: " << e.what() << CategoryStream::ENDLINE;
189 for (map<string,Override*>::iterator i=m_map.begin(); i!=m_map.end(); i++)
198 log->error("Unexpected error while parsing app mapping configuration");
199 for (map<string,Override*>::iterator i=m_map.begin(); i!=m_map.end(); i++)
208 XMLApplicationMapperImpl::~XMLApplicationMapperImpl()
210 for (map<string,Override*>::iterator i=m_map.begin(); i!=m_map.end(); i++)
214 XMLApplicationMapperImpl::Override* XMLApplicationMapperImpl::buildOverride(const XMLCh* appID, DOMElement* root, Category& log)
216 Override* o=new Override(appID);
217 DOMNodeList* nlist = root->getElementsByTagNameNS(shibtarget::XML::APPMAP_NS,shibtarget::XML::Literals::Path);
218 for (int i=0; nlist && i<nlist->getLength(); i++)
220 DOMElement* path=static_cast<DOMElement*>(nlist->item(i));
221 auto_ptr_XMLCh name(path->getAttributeNS(NULL,shibboleth::XML::Literals::Name));
222 if (!name.get() || !*(name.get()))
224 log.warn("Skipping Path element (%d) with empty Name attribute",i);
227 XMLString::lowerCase(const_cast<XMLCh*>(name.get()));
229 auto_ptr_char n(name.get());
230 o->m_map[n.get()]=buildOverride(path->getAttributeNS(NULL,shibtarget::XML::Literals::ApplicationID),path,log);
235 const XMLApplicationMapperImpl::Override* XMLApplicationMapperImpl::findOverride(const char* vhost, const char* path) const
237 const Override* o=NULL;
238 map<string,Override*>::const_iterator i=m_map.find(vhost);
243 i=m_extras.find(vhost);
244 if (i!=m_extras.end())
250 const Override* o2=o->locate(path);
257 const XMLApplicationMapperImpl::Override* XMLApplicationMapperImpl::Override::locate(const char* path) const
259 char* dup=strdup(path);
260 char* sep=strchr(path,'?');
263 for (char* pch=dup; *pch; pch++)
267 const Override* o=this;
268 const Override* specifier=((m_XMLChAppID && *m_XMLChAppID) ? this : NULL);
272 const char* token=strtok_r(dup,"/",&pos);
274 const char* token=strtok(dup,"/");
278 map<string,Override*>::const_iterator i=o->m_map.find(token);
279 if (i==o->m_map.end())
282 if (o->m_XMLChAppID && *(o->m_XMLChAppID))
285 token=strtok_r(NULL,"/",&pos);
287 token=strtok(NULL,"/");
295 const char* split_url(const char* url, string& vhost)
297 const char* path=NULL;
298 char* slash=strchr(url,'/');
301 slash=strchr(slash,'/');
304 path=strchr(slash,'/');
306 vhost.append(url,path-url);
314 const char* XMLApplicationMapper::getApplicationFromURL(const char* url) const
317 const char* path=split_url(url,vhost);
319 XMLApplicationMapperImpl* impl=dynamic_cast<XMLApplicationMapperImpl*>(getImplementation());
320 const XMLApplicationMapperImpl::Override* o=impl->findOverride(vhost.c_str(), path);
322 if (impl->log->isDebugEnabled())
324 saml::NDC ndc("getApplicationFromURL");
325 impl->log->debug("mapped %s to %s", url, o ? o->m_AppID.get() : "default application ID");
328 return o ? o->m_AppID.get() : "";
331 const XMLCh* XMLApplicationMapper::getXMLChApplicationFromURL(const char* url) const
334 const char* path=split_url(url,vhost);
336 XMLApplicationMapperImpl* impl=dynamic_cast<XMLApplicationMapperImpl*>(getImplementation());
337 const XMLApplicationMapperImpl::Override* o=impl->findOverride(vhost.c_str(), path);
339 if (impl->log->isDebugEnabled())
341 saml::NDC ndc("getXMLChApplicationFromURL");
342 impl->log->debug("mapped %s to %s", url, o ? o->m_AppID.get() : "default application ID");
345 return o ? o->m_XMLChAppID : &chNull;
348 const char* XMLApplicationMapper::getApplicationFromParsedURL(
349 const char* scheme, const char* hostname, unsigned int port, const char* path
353 string vhost(scheme);
354 vhost=vhost + "://" + hostname + ':';
356 _snprintf(buf,20,"%u",port);
358 snprintf(buf,20,"%u",port);
362 XMLApplicationMapperImpl* impl=dynamic_cast<XMLApplicationMapperImpl*>(getImplementation());
363 const XMLApplicationMapperImpl::Override* o=impl->findOverride(vhost.c_str(), path);
365 if (impl->log->isDebugEnabled())
367 saml::NDC ndc("getApplicationFromParsedURL");
368 impl->log->debug("mapped %s%s to %s", vhost.c_str(), path ? path : "", o ? o->m_AppID.get() : "default application ID");
371 return o ? o->m_AppID.get() : "";
374 const XMLCh* XMLApplicationMapper::getXMLChApplicationFromParsedURL(
375 const char* scheme, const char* hostname, unsigned int port, const char* path
379 string vhost(scheme);
380 vhost=vhost + "://" + hostname + ':';
382 _snprintf(buf,20,"%u",port);
384 snprintf(buf,20,"%u",port);
388 XMLApplicationMapperImpl* impl=dynamic_cast<XMLApplicationMapperImpl*>(getImplementation());
389 const XMLApplicationMapperImpl::Override* o=impl->findOverride(vhost.c_str(), path);
391 if (impl->log->isDebugEnabled())
393 saml::NDC ndc("getXMLChApplicationFromParsedURL");
394 impl->log->debug("mapped %s%s to %s", vhost.c_str(), path ? path : "", o ? o->m_AppID.get() : "default application ID");
397 return o ? o->m_XMLChAppID : &chNull;