dchroot-session.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2007  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software: you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation, either version 3 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>.
00016  *
00017  *********************************************************************/
00018 
00019 #include <config.h>
00020 
00021 #include "dchroot-session.h"
00022 
00023 #include <cassert>
00024 #include <cerrno>
00025 #include <cstdlib>
00026 #include <cstring>
00027 #include <iostream>
00028 #include <memory>
00029 
00030 #include <unistd.h>
00031 
00032 #include <syslog.h>
00033 
00034 #include <boost/format.hpp>
00035 
00036 using std::cout;
00037 using std::endl;
00038 using sbuild::_;
00039 using boost::format;
00040 using namespace dchroot;
00041 
00042 session::session (std::string const&         service,
00043                   config_ptr&                config,
00044                   operation                  operation,
00045                   sbuild::string_list const& chroots,
00046                   bool                       compat):
00047   session_base(service, config, operation, chroots, compat)
00048 {
00049 }
00050 
00051 session::~session ()
00052 {
00053 }
00054 
00055 sbuild::auth::status
00056 session::get_chroot_auth_status (sbuild::auth::status status,
00057                                  sbuild::chroot::ptr const& chroot) const
00058 {
00059   if (get_compat() == true)
00060     status = change_auth(status, auth::STATUS_NONE);
00061   else
00062     status = change_auth(status,
00063                          sbuild::session::get_chroot_auth_status(status,
00064                                                                  chroot));
00065 
00066   return status;
00067 }
00068 
00069 sbuild::string_list
00070 session::get_login_directories () const
00071 {
00072   sbuild::string_list ret;
00073 
00074   std::string const& wd(get_wd());
00075   if (!wd.empty())
00076     {
00077       // Set specified working directory.
00078       ret.push_back(wd);
00079     }
00080   else
00081     {
00082       // Set current working directory only if preserving environment.
00083       // Only change to home if not preserving the environment.
00084       if (!get_environment().empty())
00085         ret.push_back(this->sbuild::session::cwd);
00086       else
00087         ret.push_back(get_home());
00088 
00089       // Final fallback to root.
00090       if (std::find(ret.begin(), ret.end(), "/") == ret.end())
00091         ret.push_back("/");
00092     }
00093 
00094   return ret;
00095 }
00096 
00097 void
00098 session::get_user_command (sbuild::chroot::ptr& session_chroot,
00099                            std::string&         file,
00100                            sbuild::string_list& command) const
00101 {
00102   std::string programstring = sbuild::string_list_to_string(command, " ");
00103 
00104   command.clear();
00105   command.push_back(get_shell());
00106   command.push_back("-c");
00107   command.push_back(programstring);
00108 
00109   file = command[0];
00110 
00111   sbuild::log_debug(sbuild::DEBUG_NOTICE) << "file=" << file << endl;
00112 
00113   std::string commandstring = sbuild::string_list_to_string(command, " ");
00114   sbuild::log_debug(sbuild::DEBUG_NOTICE)
00115     << format("Running command: %1%") % commandstring << endl;
00116   if (get_uid() == 0 || get_ruid() != get_uid())
00117     syslog(LOG_USER|LOG_NOTICE, "[%s chroot] (%s->%s) Running command: \"%s\"",
00118            session_chroot->get_name().c_str(), get_ruser().c_str(), get_user().c_str(), commandstring.c_str());
00119 
00120   if (get_verbosity() != auth::VERBOSITY_QUIET)
00121     {
00122       std::string format_string;
00123       // TRANSLATORS: %1% = chroot name
00124       // TRANSLATORS: %2% = command
00125       format_string = (_("[%1% chroot] Running command: \"%2%\""));
00126 
00127       format fmt(format_string);
00128       fmt % session_chroot->get_name()
00129         % programstring;
00130       sbuild::log_info() << fmt << endl;
00131     }
00132 }

Generated on Mon Jan 21 00:38:28 2008 for schroot by  doxygen 1.5.4