Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Config.cc

Go to the documentation of this file.
00001 // $Id: Config.cc 1772 2011-03-10 12:45:21Z tho $
00002 //
00003 // Copyright (C) 2008
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #include "Config.hh"
00027 #include "Config.ih"
00028 
00029 // Custom includes
00030 #include <senf/Utils/membind.hh>
00031 
00032 //#include "Config.mpp"
00033 #define prefix_
00034 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00035 
00036 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00037 // senf::console::detail::RestrictedExecutor
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 // senf::console::ConfigBundle
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     // It is valid to add additional sources at the end while parsing ...
00130     for (Sources::const_iterator i (sources_.begin()); i != sources_.end(); ++i)
00131         (*i)->parse(executor_);
00132 }
00133 
00134 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00135 // senf::console::detail::RestrictedExecutor::RestrictGuard
00136 
00137 prefix_ senf::console::detail::RestrictedExecutor::RestrictGuard::
00138 RestrictGuard(RestrictedExecutor & executor)
00139     : executor_ (executor)
00140 {
00141     // This MUST BE root() not chroot() since restriction does NOT follow symlinks.
00142     // Therefore, if chroot() is a directory of symlinks, restricting to it will
00143     // execute NOTHING.
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 // senf::console::detail::ConfigSource
00163 
00164 prefix_ senf::console::detail::ConfigSource::~ConfigSource()
00165 {}
00166 
00167 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00168 #undef prefix_
00169 //#include "Config.mpp"
00170 
00171 
00172 // Local Variables:
00173 // mode: c++
00174 // fill-column: 100
00175 // comment-column: 40
00176 // c-file-style: "senf"
00177 // indent-tabs-mode: nil
00178 // ispell-local-dictionary: "american"
00179 // compile-command: "scons -u test"
00180 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research