sbuild  1.7.2
chroot.h
1 /* Copyright © 2005-2013 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_CHROOT_CHROOT_H
20 #define SBUILD_CHROOT_CHROOT_H
21 
22 #include <sbuild/chroot/facet/facet.h>
23 #include <sbuild/custom-error.h>
24 #include <sbuild/environment.h>
25 #include <sbuild/format-detail.h>
26 #include <sbuild/keyfile.h>
27 #include <sbuild/regex.h>
28 #include <sbuild/util.h>
29 
30 #include <list>
31 #include <memory>
32 #include <ostream>
33 #include <string>
34 
35 namespace sbuild
36 {
40  namespace chroot
41  {
42 
50  class chroot
51  {
52  public:
55  {
61  };
62 
64  enum verbosity
65  {
69  };
70 
73  {
97  };
98 
101 
103  typedef std::shared_ptr<chroot> ptr;
104 
106  typedef std::shared_ptr<const chroot> const_ptr;
107 
109  typedef std::shared_ptr<facet::facet> facet_ptr;
110 
112  typedef std::list<facet_ptr> facet_list;
113 
115  chroot ();
116 
118  chroot (const chroot& rhs);
119 
121  virtual ~chroot ();
122 
129  static chroot::ptr
130  create (const std::string& type);
131 
138  clone () const;
139 
150  clone_session (const std::string& session_id,
151  const std::string& alias,
152  const std::string& user,
153  bool root) const;
154 
161  clone_source () const;
162 
168  std::string const&
169  get_name () const;
170 
176  void
177  set_name (const std::string& name);
178 
184  std::string const&
185  get_description () const;
186 
192  void
193  set_description (const std::string& description);
194 
200  std::string const&
201  get_mount_location () const;
202 
208  void
209  set_mount_location (const std::string& location);
210 
211  public:
221  std::string
222  get_path () const;
223 
229  string_list const&
230  get_users () const;
231 
237  void
238  set_users (const string_list& users);
239 
245  string_list const&
246  get_groups () const;
247 
253  void
254  set_groups (const string_list& groups);
255 
263  string_list const&
264  get_root_users () const;
265 
273  void
275 
283  string_list const&
284  get_root_groups () const;
285 
293  void
295 
302  string_list const&
303  get_aliases () const;
304 
311  void
313 
319  bool
320  get_preserve_environment () const;
321 
327  void
329 
335  std::string const&
336  get_default_shell () const;
337 
343  void
344  set_default_shell (const std::string& default_shell);
345 
353  regex const&
354  get_environment_filter () const;
355 
363  void
365 
372  bool
373  get_active () const;
374 
380  bool
381  get_original () const;
382 
388  void
389  set_original (bool original);
390 
396  bool
397  get_run_setup_scripts () const;
398 
399  public:
407  std::string const&
408  get_script_config () const;
409 
417  void
418  set_script_config (const std::string& script_config);
419 
427  std::string const&
428  get_profile () const;
429 
437  void
438  set_profile (const std::string& profile);
439 
446  string_list const&
447  get_command_prefix () const;
448 
455  void
457 
463  verbosity
464  get_verbosity () const;
465 
471  const char *
472  get_verbosity_string () const;
473 
479  void
481 
487  void
488  set_verbosity (const std::string& verbosity);
489 
496  std::string const&
497  get_chroot_type () const;
498 
505  void
506  setup_env (environment& env) const;
507 
519  void
520  lock (setup_type type);
521 
535  void
536  unlock (setup_type type,
537  int status);
538 
539  protected:
555  void
556  setup_lock(setup_type type,
557  bool lock,
558  int status);
559 
560  public:
568  template <typename T>
569  std::shared_ptr<T>
570  get_facet ();
571 
579  template <typename T>
580  const std::shared_ptr<const T>
581  get_facet () const;
582 
590  template <typename T>
591  std::shared_ptr<T>
592  get_facet_strict ();
593 
601  template <typename T>
602  const std::shared_ptr<const T>
603  get_facet_strict () const;
604 
610  facet_list&
611  get_facets ();
612 
618  const facet_list&
619  get_facets () const;
620 
628  template <typename T>
629  void
630  add_facet (std::shared_ptr<T> facet,
631  bool copy = false);
632 
642  void
644  bool copy = false);
645 
650  template <typename T>
651  void
652  remove_facet ();
653 
659  template <typename T>
660  void
661  remove_facet (std::shared_ptr<T> facet);
662 
668  template <typename T>
669  void
670  replace_facet (std::shared_ptr<T> facet);
671 
678  list_facets () const;
679 
687  get_session_flags () const;
688 
698  friend std::ostream&
699  operator << (std::ostream& stream,
700  const ptr& rhs)
701  {
702  rhs->print_details(stream);
703  return stream;
704  }
705 
713  friend
714  keyfile const&
716  ptr& rhs)
717  {
718  rhs->set_keyfile(keyfile);
719  return keyfile;
720  }
721 
729  friend
730  keyfile&
732  const ptr& rhs)
733  {
734  rhs->get_keyfile(keyfile);
735  return keyfile;
736  }
737 
743  void
744  get_details (format_detail& detail) const;
745 
753  void
754  print_details (std::ostream& stream) const;
755 
763  void
764  get_keyfile (keyfile& keyfile) const;
765 
766  public:
774  void
775  set_keyfile (const keyfile& keyfile);
776 
783  get_used_keys () const;
784 
785  private:
787  std::string name;
789  std::string description;
803  std::string default_shell;
807  std::string mount_location;
809  bool original;
811  std::string script_config;
813  std::string profile;
818 
820  facet_list facets;
821  };
822 
823  }
824 }
825 
826 namespace sbuild
827 {
828  namespace chroot
829  {
830 
831  template <typename T>
832  std::shared_ptr<T>
833  chroot::get_facet ()
834  {
835  std::shared_ptr<T> ret;
836 
837  for (const auto& facet : facets)
838  {
839  if (ret = std::dynamic_pointer_cast<T>(facet))
840  break;
841  }
842 
843  return ret;
844  }
845 
846  template <typename T>
847  const std::shared_ptr<const T>
848  chroot::get_facet () const
849  {
850  std::shared_ptr<T> ret;
851 
852  for (const auto& facet : facets)
853  {
854  if (ret = std::dynamic_pointer_cast<T>(facet))
855  break;
856  }
857 
858  return std::const_pointer_cast<T>(ret);
859  }
860 
861  template <typename T>
862  std::shared_ptr<T>
863  chroot::get_facet_strict ()
864  {
865  auto ret = get_facet<T>();
866 
867  if (!ret)
868  throw error(type_name<T>(), FACET_ABSENT);
869 
870  return ret;
871  }
872 
873  template <typename T>
874  const std::shared_ptr<const T>
875  chroot::get_facet_strict () const
876  {
877  auto ret = get_facet<T>();
878 
879  if (!ret)
880  throw error(type_name<T>(), FACET_ABSENT);
881 
882  return ret;
883  }
884 
885  template <typename T>
886  void
887  chroot::add_facet (std::shared_ptr<T> facet,
888  bool copy)
889  {
890  facet_ptr new_facet = std::dynamic_pointer_cast<facet::facet>(facet);
891  if (!new_facet)
892  throw error(type_name<T>(), FACET_INVALID);
893 
894  for (const auto& facet : facets)
895  {
896  if (std::dynamic_pointer_cast<T>(facet))
897  throw error(type_name<T>(), FACET_PRESENT);
898  }
899 
900  new_facet->set_chroot(*this, copy);
901  facets.push_back(new_facet);
902  }
903 
904  inline void
905  chroot::add_facet_by_name (facet::facet::ptr facet,
906  bool copy)
907  {
908  for (facet_list::iterator lfacet = facets.begin();
909  lfacet != facets.end();)
910  {
911  facet_list::iterator current = lfacet++;
912  if ((*current)->get_name() == facet->get_name())
913  facets.erase(current);
914  }
915 
916  facet->set_chroot(*this, copy);
917  facets.push_back(facet);
918  }
919 
920  template <typename T>
921  void
922  chroot::remove_facet ()
923  {
924  for (facet_list::iterator facet = facets.begin();
925  facet != facets.end();
926  ++facet)
927  {
928  if (std::dynamic_pointer_cast<T>(*facet))
929  {
930  facets.erase(facet);
931  break;
932  }
933  }
934  }
935 
936  template <typename T>
937  void
938  chroot::remove_facet (std::shared_ptr<T> facet)
939  {
940  remove_facet<T>();
941  }
942 
943  template <typename T>
944  void
945  chroot::replace_facet (std::shared_ptr<T> facet)
946  {
947  remove_facet<T>();
948  add_facet(facet);
949  }
950 
951  }
952 }
953 
954 #endif /* SBUILD_CHROOT_CHROOT_H */
955 
956 /*
957  * Local Variables:
958  * mode:C++
959  * End:
960  */
void add_facet(std::shared_ptr< T > facet, bool copy=false)
Add a chroot facet.
Definition: chroot.h:887
POSIX extended regular expression.
Definition: regex.h:66
std::shared_ptr< const chroot > const_ptr
A shared_ptr to a const chroot object.
Definition: chroot.h:106
void print_details(std::ostream &stream) const
Print detailed information about the chroot to a stream.
Definition: chroot.cc:637
void remove_facet()
Remove a chroot facet.
Definition: chroot.h:922
void set_description(const std::string &description)
Set the description of the chroot.
Definition: chroot.cc:234
chroot::ptr clone_source() const
Create a source chroot.
Definition: chroot.cc:185
void set_preserve_environment(bool preserve_environment)
Set if the environment should be preserved in the chroot.
Definition: chroot.cc:346
bool preserve_environment
Preserve environment?
Definition: chroot.h:801
Directory must have an absolute path.
Definition: chroot.h:81
Debian source builder components.
Definition: ctty.cc:31
Print all messages.
Definition: chroot.h:68
std::string default_shell
Default shell.
Definition: chroot.h:803
void set_environment_filter(const regex &environment_filter)
Set the environment filter of the chroot.
Definition: chroot.cc:370
std::string const & get_default_shell() const
Get default shell.
Definition: chroot.cc:352
void set_root_groups(const string_list &groups)
Set the groups allowed to access the chroot as root.
Definition: chroot.cc:302
bool get_active() const
Get the activity status of the chroot.
string_list const & get_groups() const
Get the groups allowed to access the chroot.
Definition: chroot.cc:272
Attempt to use facet which is not present.
Definition: chroot.h:82
void set_root_users(const string_list &users)
Set the users allowed to access the chroot as root.
Definition: chroot.cc:290
void unlock(setup_type type, int status)
Unlock a chroot during setup.
Definition: chroot.cc:571
Deactivate a chroot.
Definition: chroot.h:58
std::shared_ptr< facet::facet > facet_ptr
A shared pointer to a chroot facet.
Definition: chroot.h:109
Invalid name.
Definition: chroot.h:92
facet_list & get_facets()
Get the list of all chroot facets.
Definition: chroot.cc:507
regex environment_filter
Environment filter regex.
Definition: chroot.h:805
std::string const & get_name() const
Get the name of the chroot.
Definition: chroot.cc:198
string_list const & get_root_users() const
Get the users allowed to access the chroot as root.
Definition: chroot.cc:284
string_list const & get_root_groups() const
Get the groups allowed to access the chroot as root.
Definition: chroot.cc:296
string_list groups
Groups allowed to access the chroot.
Definition: chroot.h:793
bool original
Was the chroot automatically generated?
Definition: chroot.h:809
void set_default_shell(const std::string &default_shell)
Set the default shell.
Definition: chroot.cc:358
std::string const & get_description() const
Get the description of the chroot.
Definition: chroot.cc:228
Failed to unlink session file.
Definition: chroot.h:94
void get_keyfile(keyfile &keyfile) const
Copy the chroot properties into a keyfile.
Definition: chroot.cc:689
Container of environment variables.
Definition: environment.h:38
const char * get_verbosity_string() const
Get the message verbosity as a readable string.
Definition: chroot.cc:461
string_list users
Users allowed to access the chroot.
Definition: chroot.h:791
void set_original(bool original)
Set the originality of the chroot.
Definition: chroot.cc:382
Common chroot data.
Definition: chroot.h:50
void setup_env(environment &env) const
Set environment.
Definition: chroot.cc:538
Print messages (the default).
Definition: chroot.h:67
Advisory locking.
Definition: lock.h:38
Only print essential messages.
Definition: chroot.h:66
string_list list_facets() const
List all registered chroot facets.
Definition: chroot.cc:519
string_list root_groups
Groups allowed to access the chroot as root.
Definition: chroot.h:797
Device must have an absolute path.
Definition: chroot.h:77
verbosity message_verbosity
The message verbosity.
Definition: chroot.h:817
Unknown chroot type.
Definition: chroot.h:76
Location must have an absolute path.
Definition: chroot.h:91
verbosity get_verbosity() const
Get the message verbosity.
Definition: chroot.cc:455
File is not a regular file.
Definition: chroot.h:87
std::shared_ptr< T > get_facet()
Get a chroot facet.
Definition: chroot.h:833
void add_facet_by_name(facet::facet::ptr facet, bool copy=false)
Add a chroot facet by name (rather than type).
Definition: chroot.h:905
chroot::ptr clone_session(const std::string &session_id, const std::string &alias, const std::string &user, bool root) const
Create a session chroot.
Definition: chroot.cc:168
void set_verbosity(verbosity verbosity)
Set the message verbosity.
Definition: chroot.cc:488
Failed to discard lock.
Definition: chroot.h:90
void set_name(const std::string &name)
Set the name of the chroot.
Definition: chroot.cc:204
Message verbosity is invalid.
Definition: chroot.h:96
session_flags
Chroot session properties.
Definition: facet.h:54
std::string name
Chroot name.
Definition: chroot.h:787
std::string const & get_chroot_type() const
Get the type of the chroot.
Definition: chroot.cc:530
void set_keyfile(const keyfile &keyfile)
Set the chroot properties from a keyfile.
Definition: chroot.cc:767
static chroot::ptr create(const std::string &type)
Create a chroot.
Definition: chroot.cc:144
Failed to write session file.
Definition: chroot.h:95
Base class for all facets.
Definition: facet.h:50
Failed to unlock device.
Definition: chroot.h:80
void set_users(const string_list &users)
Set the users allowed to access the chroot.
Definition: chroot.cc:266
std::vector< std::string > string_list
A string vector.
Definition: types.h:38
File must have an absolute path.
Definition: chroot.h:85
File is not a block device.
Definition: chroot.h:79
std::string const & get_profile() const
Get the configuration profile for the chroot.
Definition: chroot.cc:421
void setup_lock(setup_type type, bool lock, int status)
Unlock a chroot during setup.
Definition: chroot.cc:578
std::string profile
Configuration profile for setup scripts (replaces script_config).
Definition: chroot.h:813
Chroot device name not set.
Definition: chroot.h:75
string_list command_prefix
Command prefix.
Definition: chroot.h:815
string_list root_users
Users allowed to access the chroot as root.
Definition: chroot.h:795
string_list get_used_keys() const
Get a list of the keys used during keyfile parsing.
Definition: chroot.cc:654
Custom error.
Definition: custom-error.h:31
void replace_facet(std::shared_ptr< T > facet)
Replace an existing chroot facet with a new facet.
Definition: chroot.h:945
string_list aliases
Alternative names for the chroot.
Definition: chroot.h:799
Reactivate a chroot.
Definition: chroot.h:57
void set_aliases(const string_list &aliases)
Set the aliases of the chroot.
Definition: chroot.cc:314
std::shared_ptr< chroot > ptr
A shared_ptr to a chroot object.
Definition: chroot.h:103
virtual ~chroot()
The destructor.
Definition: chroot.cc:139
std::string description
Chroot description.
Definition: chroot.h:789
Format names and values for output.
Definition: format-detail.h:38
void set_command_prefix(const string_list &command_prefix)
Set the command_prefix for the chroot.
Definition: chroot.cc:449
std::string get_path() const
Get the path to the chroot.
Definition: chroot.cc:254
friend keyfile const & operator>>(const keyfile &keyfile, ptr &rhs)
Chroot initialisation from a keyfile.
Definition: chroot.h:715
void set_profile(const std::string &profile)
Set configuration profile for the chroot.
Definition: chroot.cc:427
Attempt to add facet which is already in use.
Definition: chroot.h:84
facet_list facets
Contained chroot facets.
Definition: chroot.h:820
Configuration file parser.
Definition: keyfile.h:46
Could not set profile from script configuration path.
Definition: chroot.h:93
facet::facet::session_flags get_session_flags() const
Get the session flags of the chroot.
Definition: chroot.cc:586
std::string const & get_script_config() const
Get the script configuration file for the chroot.
Definition: chroot.cc:396
Attempt to add object which is not a facet.
Definition: chroot.h:83
std::string mount_location
Location to mount chroot in the filesystem (if any).
Definition: chroot.h:807
regex const & get_environment_filter() const
Get the environment filter of the chroot.
Definition: chroot.cc:364
void set_groups(const string_list &groups)
Set the users allowed to access the chroot.
Definition: chroot.cc:278
File has write permissions for others.
Definition: chroot.h:89
bool get_run_setup_scripts() const
Check if chroot setup scripts will be run.
Definition: chroot.cc:388
File is not owned by user root.
Definition: chroot.h:88
Activate a chroot.
Definition: chroot.h:56
string_list const & get_aliases() const
Get the aliases of the chroot.
Definition: chroot.cc:308
std::list< facet_ptr > facet_list
A list of chroot facets.
Definition: chroot.h:112
std::shared_ptr< facet > ptr
A shared_ptr to a chroot facet object.
Definition: facet.h:63
bool get_preserve_environment() const
Check if the environment should be preserved in the chroot.
Definition: chroot.cc:340
custom_error< error_code > error
Exception type.
Definition: chroot.h:100
error_code
Error codes.
Definition: chroot.h:72
std::string const & get_mount_location() const
Get the mount location of the chroot.
Definition: chroot.cc:240
chroot()
The constructor.
Definition: chroot.cc:86
Clean up after executing a command.
Definition: chroot.h:60
Failed to acquire lock.
Definition: chroot.h:86
string_list const & get_command_prefix() const
Get the command_prefix for the chroot.
Definition: chroot.cc:443
chroot::ptr clone() const
Copy the chroot.
Definition: chroot.cc:160
std::shared_ptr< T > get_facet_strict()
Get a chroot facet.
Definition: chroot.h:863
std::string script_config
Configuration of the setup and exec scripts.
Definition: chroot.h:811
setup_type
Type of setup to perform.
Definition: chroot.h:54
bool get_original() const
Get the originality of the chroot.
Definition: chroot.cc:376
void lock(setup_type type)
Lock a chroot during setup.
Definition: chroot.cc:565
void get_details(format_detail &detail) const
Get detailed information about the chroot for output.
Definition: chroot.cc:597
void set_script_config(const std::string &script_config)
Set the script configuration file for the chroot.
Definition: chroot.cc:402
Prepare for executing a command.
Definition: chroot.h:59
verbosity
Message verbosity.
Definition: chroot.h:64
friend std::ostream & operator<<(std::ostream &stream, const ptr &rhs)
Print detailed information about the chroot to a stream.
Definition: chroot.h:699
Chroot creation failed.
Definition: chroot.h:74
Failed to lock device.
Definition: chroot.h:78
void set_mount_location(const std::string &location)
Set the mount location of the chroot.
Definition: chroot.cc:246
string_list const & get_users() const
Get the users allowed to access the chroot.
Definition: chroot.cc:260