sbuild-chroot-file.cc

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  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 2 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, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
00017  *
00018  *********************************************************************/
00019 
00020 #include <config.h>
00021 
00022 #include "sbuild-chroot-file.h"
00023 #include "sbuild-format-detail.h"
00024 #include "sbuild-lock.h"
00025 
00026 #include <cerrno>
00027 #include <cstring>
00028 
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <sys/sysmacros.h>
00032 #include <unistd.h>
00033 
00034 #include <boost/format.hpp>
00035 
00036 using boost::format;
00037 using namespace sbuild;
00038 
00039 chroot_file::chroot_file ():
00040   chroot(),
00041   chroot_source(),
00042   file(),
00043   repack(false)
00044 {
00045   set_run_setup_scripts(true);
00046   set_run_exec_scripts(true);
00047 }
00048 
00049 chroot_file::~chroot_file ()
00050 {
00051 }
00052 
00053 sbuild::chroot::ptr
00054 chroot_file::clone () const
00055 {
00056   return ptr(new chroot_file(*this));
00057 }
00058 
00059 sbuild::chroot::ptr
00060 chroot_file::clone_source () const
00061 {
00062   chroot_file *clone_file = new chroot_file(*this);
00063   ptr clone(clone_file);
00064 
00065   chroot_source::clone_source_setup(clone);
00066   clone_file->repack = true;
00067 
00068   return clone;
00069 }
00070 
00071 std::string const&
00072 chroot_file::get_file () const
00073 {
00074   return this->file;
00075 }
00076 
00077 void
00078 chroot_file::set_file (std::string const& file)
00079 {
00080   if (!is_absname(file))
00081     throw error(file, FILE_ABS);
00082 
00083   this->file = file;
00084 }
00085 
00086 bool
00087 chroot_file::get_file_repack () const
00088 {
00089   return this->repack;
00090 }
00091 
00092 void
00093 chroot_file::set_file_repack (bool repack)
00094 {
00095   this->repack = repack;
00096 }
00097 
00098 std::string const&
00099 chroot_file::get_chroot_type () const
00100 {
00101   static const std::string type("file");
00102 
00103   return type;
00104 }
00105 
00106 void
00107 chroot_file::setup_env (environment& env)
00108 {
00109   chroot::setup_env(env);
00110   chroot_source::setup_env(env);
00111 
00112   env.add("CHROOT_FILE", get_file());
00113   env.add("CHROOT_FILE_REPACK", this->repack);
00114 }
00115 
00116 void
00117 chroot_file::setup_lock (chroot::setup_type type,
00118                          bool               lock,
00119                          int                status)
00120 {
00121   // Check ownership and permissions.
00122   if (type == SETUP_START && lock == true)
00123     {
00124       struct stat statbuf;
00125       if (stat(this->file.c_str(), &statbuf) < 0)
00126         throw error(this->file, FILE_STAT, strerror(errno));
00127 
00128       // NOTE: taken from chroot_config::check_security.
00129       if (statbuf.st_uid != 0)
00130         throw error(this->file, FILE_OWNER);
00131       if (statbuf.st_mode & S_IWOTH)
00132         throw error(this->file, FILE_PERMS);
00133       if (!S_ISREG(statbuf.st_mode))
00134         throw error(this->file, FILE_NOTREG);
00135     }
00136 
00137   /* By default, file chroots do no locking. */
00138   /* Create or unlink session information. */
00139   if ((type == SETUP_START && lock == true) ||
00140       (type == SETUP_STOP && lock == false && status == 0))
00141     {
00142 
00143       bool start = (type == SETUP_START);
00144       setup_session_info(start);
00145     }
00146 }
00147 
00148 sbuild::chroot::session_flags
00149 chroot_file::get_session_flags () const
00150 {
00151   return SESSION_CREATE;
00152 }
00153 
00154 void
00155 chroot_file::get_details (format_detail& detail) const
00156 {
00157   chroot::get_details(detail);
00158   chroot_source::get_details(detail);
00159 
00160   if (!this->file.empty())
00161     detail
00162       .add(_("File"), get_file())
00163       .add(_("File Repack"), this->repack);
00164 }
00165 
00166 void
00167 chroot_file::get_keyfile (keyfile& keyfile) const
00168 {
00169   chroot::get_keyfile(keyfile);
00170   chroot_source::get_keyfile(keyfile);
00171 
00172   keyfile::set_object_value(*this, &chroot_file::get_file,
00173                             keyfile, get_name(), "file");
00174 
00175   if (get_active())
00176   keyfile::set_object_value(*this, &chroot_file::get_file_repack,
00177                             keyfile, get_name(), "file-repack");
00178 }
00179 
00180 void
00181 chroot_file::set_keyfile (keyfile const& keyfile)
00182 {
00183   chroot::set_keyfile(keyfile);
00184   chroot_source::set_keyfile(keyfile);
00185 
00186   keyfile::get_object_value(*this, &chroot_file::set_file,
00187                             keyfile, get_name(), "file",
00188                             keyfile::PRIORITY_REQUIRED);
00189 
00190   keyfile::get_object_value(*this, &chroot_file::set_file_repack,
00191                             keyfile, get_name(), "file-repack",
00192                             get_active() ?
00193                             keyfile::PRIORITY_REQUIRED :
00194                             keyfile::PRIORITY_DISALLOWED);
00195 }

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1