c5669e2487c684a1bc2dbecefa0b5a2a7f15ba1b
[shibboleth/cpp-sp.git] / shibsp / handler / impl / SecuredHandler.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  * SecuredHandler.cpp
23  *
24  * Pluggable runtime functionality that is protected by simple access control.
25  */
26
27 #include "internal.h"
28 #include "SPRequest.h"
29 #include "handler/SecuredHandler.h"
30
31 #include <boost/bind.hpp>
32 #include <boost/algorithm/string.hpp>
33
34 using namespace shibsp;
35 using namespace xmltooling;
36 using namespace boost;
37 using namespace std;
38
39 namespace {
40     class SHIBSP_DLLLOCAL Blocker : public DOMNodeFilter
41     {
42     public:
43 #ifdef SHIBSP_XERCESC_SHORT_ACCEPTNODE
44         short
45 #else
46         FilterAction
47 #endif
48         acceptNode(const DOMNode* node) const {
49             return FILTER_REJECT;
50         }
51     };
52
53     static Blocker g_Blocker;
54 };
55
56 SecuredHandler::SecuredHandler(
57     const DOMElement* e,
58     Category& log,
59     const char* aclProperty,
60     const char* defaultACL,
61     DOMNodeFilter* filter,
62     const map<string,string>* remapper
63     ) : AbstractHandler(e, log, filter ? filter : &g_Blocker, remapper)
64 {
65     if (SPConfig::getConfig().isEnabled(SPConfig::InProcess)) {
66         pair<bool,const char*> acl = getString(aclProperty);
67         if (!acl.first && defaultACL) {
68             m_log.info("installing default ACL (%s)", defaultACL);
69             acl.first = true;
70             acl.second = defaultACL;
71         }
72         if (acl.first) {
73             string aclbuf(acl.second);
74             vector<string> aclarray;
75             split(aclarray, aclbuf, is_space(), algorithm::token_compress_on);
76             for_each(aclarray.begin(), aclarray.end(), boost::bind(&SecuredHandler::parseACL, this, _1));
77             if (m_acl.empty()) {
78                 m_log.warn("invalid CIDR range(s) in handler's acl property, allowing 127.0.0.1 and ::1 as a fall back");
79                 m_acl.push_back(IPRange::parseCIDRBlock("127.0.0.1"));
80                 m_acl.push_back(IPRange::parseCIDRBlock("::1"));
81             }
82         }
83     }
84 }
85
86 SecuredHandler::~SecuredHandler()
87 {
88 }
89
90 void SecuredHandler::parseACL(const string& acl)
91 {
92     try {
93         m_acl.push_back(IPRange::parseCIDRBlock(acl.c_str()));
94     }
95     catch (std::exception& ex) {
96         m_log.error("invalid CIDR block (%s): %s", acl.c_str(), ex.what());
97     }
98 }
99
100 pair<bool,long> SecuredHandler::run(SPRequest& request, bool isHandler) const
101 {
102     SPConfig& conf = SPConfig::getConfig();
103     if (conf.isEnabled(SPConfig::InProcess) && !m_acl.empty()) {
104         static bool (IPRange::* contains)(const char*) const = &IPRange::contains;
105         if (find_if(m_acl.begin(), m_acl.end(), boost::bind(contains, _1, request.getRemoteAddr().c_str())) == m_acl.end()) {
106             request.log(SPRequest::SPWarn, string("handler request blocked from invalid address (") + request.getRemoteAddr() + ')');
107             istringstream msg("Access Denied");
108             return make_pair(true, request.sendResponse(msg, HTTPResponse::XMLTOOLING_HTTP_STATUS_FORBIDDEN));
109         }
110     }
111     return make_pair(false, 0L);
112 }