Ignore . and .. on Windows side, fix VS make
[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                     if (strcmp(f.cFileName, ".") && strcmp(f.cFileName, ".."))
104                         log.warn("nested folders not supported, skipping (%s)", f.cFileName);
105                     continue;
106                 }
107                 fullname = loc + '/' + f.cFileName;
108                 log.info("will create metadata source from (%s)", fullname.c_str());
109                 auto_ptr_XMLCh entry(fullname.c_str());
110 #else
111             DIR* d = opendir(loc.c_str());
112             if (!d) {
113                 throw MetadataException("Folder MetadataProvider unable to open directory ($1)", params(1, loc.c_str()));
114             }
115             struct dirent* ent;
116             while(readdir_r(d, ent, &ent) == 0 && ent) {
117                 if (ent->d_type & DT_DIR) {
118                     if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
119                         log.warn("nested folders not supported, skipping (%s)", ent->d_name);
120                     continue;
121                 }
122                 fullname = loc + '/' + ent->d_name;
123                 log.info("will create metadata source from (%s)", fullname.c_str());
124                 auto_ptr_XMLCh entry(fullname.c_str());
125 #endif
126                 DOMElement* child = e->getOwnerDocument()->createElementNS(nullptr, _MetadataProvider);
127                 child->setAttributeNS(nullptr, _type, _XML);
128                 child->setAttributeNS(nullptr, path, entry.get());
129                 if (e->hasAttributeNS(nullptr, validate))
130                     child->setAttributeNS(nullptr, validate, e->getAttributeNS(nullptr, validate));
131                 if (e->hasAttributeNS(nullptr, reloadChanges))
132                     child->setAttributeNS(nullptr, reloadChanges, e->getAttributeNS(nullptr, reloadChanges));
133                 if (e->hasAttributeNS(nullptr, discoveryFeed))
134                     child->setAttributeNS(nullptr, discoveryFeed, e->getAttributeNS(nullptr, discoveryFeed));
135                 if (e->hasAttributeNS(nullptr, legacyOrgNames))
136                     child->setAttributeNS(nullptr, legacyOrgNames, e->getAttributeNS(nullptr, legacyOrgNames));
137
138                 DOMElement* filter = XMLHelper::getFirstChildElement(e);
139                 while (filter) {
140                     child->appendChild(filter->cloneNode(true));
141                     filter = XMLHelper::getNextSiblingElement(filter);
142                 }
143                 root->appendChild(child);
144
145 #ifdef WIN32
146             } while (FindNextFile(h, &f));
147             FindClose(h);
148 #else
149             }
150             closedir(d);
151 #endif
152             return SAMLConfig::getConfig().MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER, root);
153         }
154
155     };
156 };