https://issues.shibboleth.net/jira/browse/CPPOST-69
[shibboleth/cpp-opensaml.git] / saml / saml2 / metadata / impl / FolderMetadataProvider.cpp
1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20
21 /**
22  * FolderMetadataProvider.cpp
23  * 
24  * MetadataProvider that loads all files in a directory.
25  */
26
27 #include "internal.h"
28 #include "exceptions.h"
29 #include "saml2/metadata/Metadata.h"
30 #include "saml2/metadata/MetadataProvider.h"
31
32 #include <memory>
33 #include <xercesc/util/XMLUniDefs.hpp>
34 #include <xmltooling/logging.h>
35 #include <xmltooling/XMLToolingConfig.h>
36 #include <xmltooling/util/PathResolver.h>
37 #include <xmltooling/util/XMLHelper.h>
38
39 #ifndef WIN32
40 # ifdef HAVE_SYS_TYPES_H && HAVE_DIRENT_H
41 #  include <sys/types.h>
42 #  include <dirent.h>
43 # else
44 #  #error Unsupported directory library headers.
45 # endif
46 #endif
47
48 using namespace opensaml::saml2md;
49 using namespace opensaml;
50 using namespace xmlsignature;
51 using namespace xmltooling::logging;
52 using namespace xmltooling;
53 using namespace std;
54
55 namespace opensaml {
56     namespace saml2md {
57
58         static const XMLCh Chaining[] =             UNICODE_LITERAL_8(C,h,a,i,n,i,n,g);
59         static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
60         static const XMLCh discoveryFeed[] =        UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
61         static const XMLCh legacyOrgNames[] =       UNICODE_LITERAL_14(l,e,g,a,c,y,O,r,g,N,a,m,e,s);
62         static const XMLCh path[] =                 UNICODE_LITERAL_4(p,a,t,h);
63         static const XMLCh precedence[] =           UNICODE_LITERAL_10(p,r,e,c,e,d,e,n,c,e);
64         static const XMLCh reloadChanges[] =        UNICODE_LITERAL_13(r,e,l,o,a,d,C,h,a,n,g,e,s);
65         static const XMLCh validate[] =             UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
66         static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
67         static const XMLCh _XML[] =                 UNICODE_LITERAL_3(X,M,L);
68     
69         MetadataProvider* SAML_DLLLOCAL FolderMetadataProviderFactory(const DOMElement* const & e)
70         {
71             // The goal here is to construct a configuration for a chain of file-based providers
72             // based on the content of the directory we're given.
73
74             auto_ptr_char p(e->getAttributeNS(nullptr, path));
75             if (!p.get() || !*p.get()) {
76                 throw MetadataException("Folder MetadataProvider missing path setting.");
77             }
78
79             string fullname, loc(p.get());
80             XMLToolingConfig::getConfig().getPathResolver()->resolve(loc, PathResolver::XMLTOOLING_CFG_FILE);
81
82             // First we build a new root element of the right type, and copy in the precedence setting.
83             DOMElement* root = e->getOwnerDocument()->createElementNS(nullptr, _MetadataProvider);
84             root->setAttributeNS(nullptr, _type, Chaining);
85             if (e->hasAttributeNS(nullptr, precedence))
86                 root->setAttributeNS(nullptr, precedence, e->getAttributeNS(nullptr, precedence));
87
88             Category& log = Category::getInstance(SAML_LOGCAT".Metadata.Folder");
89             log.info("loading metadata files from folder (%s)", loc.c_str());
90
91 #ifdef WIN32
92             WIN32_FIND_DATA f;
93             fullname = loc + "/*";
94             HANDLE h = FindFirstFile(fullname.c_str(), &f);
95             if (h == INVALID_HANDLE_VALUE) {
96                 if (GetLastError() != ERROR_FILE_NOT_FOUND)
97                     throw MetadataException("Folder MetadataProvider unable to open directory ($1)", params(1, loc.c_str()));
98                 log.warn("no files found in folder (%s)", loc.c_str());
99                 return SAMLConfig::getConfig().MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER, root);
100             }
101             do {
102                 if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
103                     log.warn("nested folders not supported, skipping (%s)", f.cFileName);
104                     continue;
105                 }
106                 fullname = loc + '/' + f.cFileName;
107                 log.info("will create metadata source from (%s)", fullname.c_str());
108                 auto_ptr_XMLCh entry(fullname.c_str());
109 #else
110             DIR* d = opendir(loc.c_str());
111             if (!d) {
112                 throw MetadataException("Folder MetadataProvider unable to open directory ($1)", params(1, loc.c_str()));
113             }
114             struct dirent* ent;
115             while(readdir_r(d, ent, &ent) == 0 && ent) {
116                 if (ent->d_type & DT_DIR) {
117                     if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
118                         log.warn("nested folders not supported, skipping (%s)", ent->d_name);
119                     continue;
120                 }
121                 fullname = loc + '/' + ent->d_name;
122                 log.info("will create metadata source from (%s)", fullname.c_str());
123                 auto_ptr_XMLCh entry(fullname.c_str());
124 #endif
125                 DOMElement* child = e->getOwnerDocument()->createElementNS(nullptr, _MetadataProvider);
126                 child->setAttributeNS(nullptr, _type, _XML);
127                 child->setAttributeNS(nullptr, path, entry.get());
128                 if (e->hasAttributeNS(nullptr, validate))
129                     child->setAttributeNS(nullptr, validate, e->getAttributeNS(nullptr, validate));
130                 if (e->hasAttributeNS(nullptr, reloadChanges))
131                     child->setAttributeNS(nullptr, reloadChanges, e->getAttributeNS(nullptr, reloadChanges));
132                 if (e->hasAttributeNS(nullptr, discoveryFeed))
133                     child->setAttributeNS(nullptr, discoveryFeed, e->getAttributeNS(nullptr, discoveryFeed));
134                 if (e->hasAttributeNS(nullptr, legacyOrgNames))
135                     child->setAttributeNS(nullptr, legacyOrgNames, e->getAttributeNS(nullptr, legacyOrgNames));
136
137                 DOMElement* filter = XMLHelper::getFirstChildElement(e);
138                 while (filter) {
139                     child->appendChild(filter->cloneNode(true));
140                     filter = XMLHelper::getNextSiblingElement(filter);
141                 }
142                 root->appendChild(child);
143
144 #ifdef WIN32
145             } while (FindNextFile(h, &f));
146             FindClose(h);
147 #else
148             }
149             closedir(d);
150 #endif
151             return SAMLConfig::getConfig().MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER, root);
152         }
153
154     };
155 };