Enable POSIX signatures on Solaris, and fix Unix directory parsing.
[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 # if defined(HAVE_SYS_TYPES_H) && defined(HAVE_DIRENT_H)
41 #  include <dirent.h>
42 #  include <sys/types.h>
43 #  include <sys/stat.h>
44 # else
45 #  error Unsupported directory library headers.
46 # endif
47 #endif
48
49 using namespace opensaml::saml2md;
50 using namespace opensaml;
51 using namespace xmlsignature;
52 using namespace xmltooling::logging;
53 using namespace xmltooling;
54 using namespace std;
55
56 namespace opensaml {
57     namespace saml2md {
58
59         static const XMLCh Chaining[] =             UNICODE_LITERAL_8(C,h,a,i,n,i,n,g);
60         static const XMLCh _MetadataProvider[] =    UNICODE_LITERAL_16(M,e,t,a,d,a,t,a,P,r,o,v,i,d,e,r);
61         static const XMLCh discoveryFeed[] =        UNICODE_LITERAL_13(d,i,s,c,o,v,e,r,y,F,e,e,d);
62         static const XMLCh legacyOrgNames[] =       UNICODE_LITERAL_14(l,e,g,a,c,y,O,r,g,N,a,m,e,s);
63         static const XMLCh path[] =                 UNICODE_LITERAL_4(p,a,t,h);
64         static const XMLCh precedence[] =           UNICODE_LITERAL_10(p,r,e,c,e,d,e,n,c,e);
65         static const XMLCh reloadChanges[] =        UNICODE_LITERAL_13(r,e,l,o,a,d,C,h,a,n,g,e,s);
66         static const XMLCh validate[] =             UNICODE_LITERAL_8(v,a,l,i,d,a,t,e);
67         static const XMLCh _type[] =                UNICODE_LITERAL_4(t,y,p,e);
68         static const XMLCh _XML[] =                 UNICODE_LITERAL_3(X,M,L);
69     
70         MetadataProvider* SAML_DLLLOCAL FolderMetadataProviderFactory(const DOMElement* const & e)
71         {
72             // The goal here is to construct a configuration for a chain of file-based providers
73             // based on the content of the directory we're given.
74
75             auto_ptr_char p(e->getAttributeNS(nullptr, path));
76             if (!p.get() || !*p.get()) {
77                 throw MetadataException("Folder MetadataProvider missing path setting.");
78             }
79
80             string fullname, loc(p.get());
81             XMLToolingConfig::getConfig().getPathResolver()->resolve(loc, PathResolver::XMLTOOLING_CFG_FILE);
82
83             // First we build a new root element of the right type, and copy in the precedence setting.
84             DOMElement* root = e->getOwnerDocument()->createElementNS(nullptr, _MetadataProvider);
85             root->setAttributeNS(nullptr, _type, Chaining);
86             if (e->hasAttributeNS(nullptr, precedence))
87                 root->setAttributeNS(nullptr, precedence, e->getAttributeNS(nullptr, precedence));
88
89             Category& log = Category::getInstance(SAML_LOGCAT".Metadata.Folder");
90             log.info("loading metadata files from folder (%s)", loc.c_str());
91
92 #ifdef WIN32
93             WIN32_FIND_DATA f;
94             fullname = loc + "/*";
95             HANDLE h = FindFirstFile(fullname.c_str(), &f);
96             if (h == INVALID_HANDLE_VALUE) {
97                 if (GetLastError() != ERROR_FILE_NOT_FOUND)
98                     throw MetadataException("Folder MetadataProvider unable to open directory ($1)", params(1, loc.c_str()));
99                 log.warn("no files found in folder (%s)", loc.c_str());
100                 return SAMLConfig::getConfig().MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER, root);
101             }
102             do {
103                 if (f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
104                     if (strcmp(f.cFileName, ".") && strcmp(f.cFileName, ".."))
105                         log.warn("nested folders not supported, skipping (%s)", f.cFileName);
106                     continue;
107                 }
108                 fullname = loc + '/' + f.cFileName;
109                 log.info("will create metadata source from (%s)", fullname.c_str());
110                 auto_ptr_XMLCh entry(fullname.c_str());
111 #else
112             DIR* d = opendir(loc.c_str());
113             if (!d) {
114                 throw MetadataException("Folder MetadataProvider unable to open directory ($1)", params(1, loc.c_str()));
115             }
116             char dir_buf[sizeof(struct dirent) + PATH_MAX];
117             struct dirent* ent = (struct dirent*)dir_buf;
118             struct dirent* entptr = nullptr;
119             while(readdir_r(d, ent, &entptr) == 0 && entptr) {
120                 if (!strcmp(entptr->d_name, ".") || !strcmp(entptr->d_name, ".."))
121                     continue;
122                 fullname = loc + '/' + entptr->d_name;
123                 struct stat stat_buf;
124                 if (stat(fullname.c_str(), &stat_buf) != 0) {
125                     log.warn("unable to access (%s)", entptr->d_name);
126                     continue;
127                 }
128                 else if (S_ISDIR(stat_buf.st_mode)) {
129                     log.warn("nested folders not supported, skipping (%s)", entptr->d_name);
130                     continue;
131                 }
132                 log.info("will create metadata source from (%s)", fullname.c_str());
133                 auto_ptr_XMLCh entry(fullname.c_str());
134 #endif
135                 DOMElement* child = e->getOwnerDocument()->createElementNS(nullptr, _MetadataProvider);
136                 child->setAttributeNS(nullptr, _type, _XML);
137                 child->setAttributeNS(nullptr, path, entry.get());
138                 if (e->hasAttributeNS(nullptr, validate))
139                     child->setAttributeNS(nullptr, validate, e->getAttributeNS(nullptr, validate));
140                 if (e->hasAttributeNS(nullptr, reloadChanges))
141                     child->setAttributeNS(nullptr, reloadChanges, e->getAttributeNS(nullptr, reloadChanges));
142                 if (e->hasAttributeNS(nullptr, discoveryFeed))
143                     child->setAttributeNS(nullptr, discoveryFeed, e->getAttributeNS(nullptr, discoveryFeed));
144                 if (e->hasAttributeNS(nullptr, legacyOrgNames))
145                     child->setAttributeNS(nullptr, legacyOrgNames, e->getAttributeNS(nullptr, legacyOrgNames));
146
147                 DOMElement* filter = XMLHelper::getFirstChildElement(e);
148                 while (filter) {
149                     child->appendChild(filter->cloneNode(true));
150                     filter = XMLHelper::getNextSiblingElement(filter);
151                 }
152                 root->appendChild(child);
153
154 #ifdef WIN32
155             } while (FindNextFile(h, &f));
156             FindClose(h);
157 #else
158             }
159             closedir(d);
160 #endif
161             return SAMLConfig::getConfig().MetadataProviderManager.newPlugin(CHAINING_METADATA_PROVIDER, root);
162         }
163
164     };
165 };