00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00026 #include "Config.hh"
00027 #include "Config.ih"
00028
00029
00030 #include <senf/Utils/membind.hh>
00031
00032
00033 #define prefix_
00034
00035
00036
00037
00038
00039 prefix_ senf::console::detail::RestrictedExecutor::RestrictedExecutor(DirectoryNode & root)
00040 {
00041 executor_
00042 .chroot(root)
00043 .policy(senf::membind(&RestrictedExecutor::policyCallback, this));
00044 }
00045
00046 prefix_ void
00047 senf::console::detail::RestrictedExecutor::execute(std::ostream & output,
00048 ParseCommandInfo const & command)
00049 {
00050 executor_.execute(output, command);
00051 }
00052
00053 prefix_ void
00054 senf::console::detail::RestrictedExecutor::operator()(std::ostream & output,
00055 ParseCommandInfo const & command)
00056 {
00057 execute(output, command);
00058 }
00059
00060 prefix_ senf::console::GenericNode &
00061 senf::console::detail::RestrictedExecutor::getNode(ParseCommandInfo const & command)
00062 {
00063 return executor_.getNode(command);
00064 }
00065
00066 prefix_ bool senf::console::detail::RestrictedExecutor::parsed(GenericNode & node)
00067 const
00068 {
00069 ParsedNodes::const_iterator i (parsedNodes_.begin());
00070 ParsedNodes::const_iterator const i_end (parsedNodes_.end());
00071 for (; i != i_end; ++i)
00072 if ( ! i->expired() && node.isChildOf(*(i->lock())) )
00073 return true;
00074 return false;
00075 }
00076
00077 prefix_ void senf::console::detail::RestrictedExecutor::policyCallback(DirectoryNode & dir,
00078 std::string const & name)
00079 {
00080 if (dir.hasChild(name)) {
00081 GenericNode & item (dir.get(name));
00082 if (restrict_ && ! item.isChildOf(*restrict_) && ! item.isDirectory())
00083 throw Executor::IgnoreCommandException();
00084 if (parsed(item))
00085 throw Executor::IgnoreCommandException();
00086 }
00087 else if (restrict_ && ! dir.isChildOf(*restrict_))
00088 throw Executor::IgnoreCommandException();
00089 }
00090
00091 namespace {
00092 struct RemoveNodesFn
00093 {
00094 RemoveNodesFn(senf::console::DirectoryNode::ptr newNode) : newNode_ (newNode) {}
00095
00096 bool operator()(senf::console::DirectoryNode::weak_ptr node) const
00097 { return node.expired() || node.lock()->isChildOf(*newNode_); }
00098
00099 senf::console::DirectoryNode::ptr newNode_;
00100 };
00101 }
00102
00103 prefix_ void
00104 senf::console::detail::RestrictedExecutor::insertParsedNode(DirectoryNode & node)
00105 {
00106 parsedNodes_.erase(
00107 std::remove_if(parsedNodes_.begin(), parsedNodes_.end(), RemoveNodesFn(node.thisptr())),
00108 parsedNodes_.end());
00109 parsedNodes_.push_back(node.thisptr());
00110 }
00111
00112
00113
00114
00115 prefix_ void senf::console::ConfigBundle::parse()
00116 {
00117 detail::RestrictedExecutor::RestrictGuard guard (executor_);
00118 parseInternal();
00119 }
00120
00121 prefix_ void senf::console::ConfigBundle::parse(DirectoryNode & restrict)
00122 {
00123 detail::RestrictedExecutor::RestrictGuard guard (executor_, restrict);
00124 parseInternal();
00125 }
00126
00127 prefix_ void senf::console::ConfigBundle::parseInternal()
00128 {
00129
00130 for (Sources::const_iterator i (sources_.begin()); i != sources_.end(); ++i)
00131 (*i)->parse(executor_);
00132 }
00133
00134
00135
00136
00137 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::
00138 RestrictGuard(RestrictedExecutor & executor)
00139 : executor_ (executor)
00140 {
00141
00142
00143
00144 executor_.restrict_ = console::root().thisptr();
00145 }
00146
00147 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::
00148 RestrictGuard(RestrictedExecutor & executor, DirectoryNode & restrict)
00149 : executor_ (executor)
00150 {
00151 executor_.restrict_ = restrict.thisptr();
00152 }
00153
00154 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::~RestrictGuard()
00155 {
00156 if (! std::uncaught_exception())
00157 executor_.insertParsedNode( *executor_.restrict_ );
00158 executor_.restrict_ = console::root().thisptr();
00159 }
00160
00161
00162
00163
00164 prefix_ senf::console::detail::ConfigSource::~ConfigSource()
00165 {}
00166
00167
00168 #undef prefix_
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180