sbuild  1.7.2
session.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_SESSION_H
20 #define SBUILD_SESSION_H
21 
22 #include <sbuild/auth/auth.h>
23 #include <sbuild/chroot/chroot.h>
24 #include <sbuild/custom-error.h>
25 
26 #include <string>
27 
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <termios.h>
31 #include <unistd.h>
32 
33 namespace sbuild
34 {
35 
46  class session
47  {
48  public:
50  {
52  std::string alias;
55  };
56 
58  typedef std::vector<chroot_list_entry> chroot_list;
59 
61  enum operation
62  {
68  };
69 
72  {
87  EXEC,
93  PAM,
102  };
103 
106 
108  typedef std::shared_ptr<session> ptr;
109 
117  session (const std::string& service,
119  const chroot_list& chroots);
120 
122  virtual ~session ();
123 
129  auth::auth::ptr const&
130  get_auth () const;
131 
137  void
138  set_auth (auth::auth::ptr& auth);
139 
145  chroot_list const&
146  get_chroots () const;
147 
153  void
154  set_chroots (const chroot_list& chroots);
155 
161  operation
162  get_operation () const;
163 
169  void
171 
178  std::string const&
179  get_session_id () const;
180 
187  void
188  set_session_id (const std::string& session_id);
189 
195  std::string const&
196  get_verbosity () const;
197 
204  void
205  set_verbosity (const std::string& verbosity);
206 
212  bool
213  get_preserve_environment () const;
214 
220  void
222 
228  std::string const&
229  get_shell_override () const;
230 
236  void
237  set_shell_override (const std::string& shell);
238 
244  string_map const&
245  get_user_options () const;
246 
252  void
254 
260  bool
261  get_force () const;
262 
268  void
269  set_force (bool force);
270 
274  void
275  save_termios ();
276 
280  void
281  restore_termios ();
282 
289  int
290  get_child_status () const;
291 
298  bool
299  is_group_member (const std::string& groupname) const;
300 
301  protected:
305  void
307  bool& in_users,
308  bool& in_root_users,
309  bool& in_groups,
310  bool& in_root_groups) const;
311 
317  virtual auth::auth::status
319  const chroot::chroot::ptr& chroot) const;
320 
321  public:
327  virtual auth::auth::status
328  get_auth_status () const;
329 
336  void
337  run ();
338 
339  protected:
347  virtual void
348  run_impl ();
349 
358  virtual string_list
360  const environment& env) const;
361 
370  virtual string_list
372  const environment& env) const;
373 
382  virtual string_list
383  get_shells (chroot::chroot::ptr& session_chroot) const;
384 
393  virtual std::string
394  get_shell (chroot::chroot::ptr& session_chroot) const;
395 
404  virtual void
405  get_command (chroot::chroot::ptr& session_chroot,
406  std::string& file,
407  string_list& command,
408  environment& env) const;
409 
418  virtual void
419  get_login_command (chroot::chroot::ptr& session_chroot,
420  std::string& file,
421  string_list& command,
422  environment& env) const;
423 
432  virtual void
433  get_user_command (chroot::chroot::ptr& session_chroot,
434  std::string& file,
435  string_list& command,
436  const environment& env) const;
437 
438  private:
451  void
452  setup_chroot (chroot::chroot::ptr& session_chroot,
453  chroot::chroot::setup_type setup_type);
454 
462  void
463  run_chroot (chroot::chroot::ptr& session_chroot);
464 
472  void
473  run_child (chroot::chroot::ptr& session_chroot);
474 
483  void
484  wait_for_child (pid_t pid,
485  int& child_status);
486 
492  void
494 
498  void
500 
506  void
508 
512  void
514 
520  void
522 
526  void
528 
537  void
538  set_signal_handler (int signal,
539  struct sigaction *saved_signal,
540  void (*handler)(int));
541 
549  void
550  clear_signal_handler (int signal,
551  struct sigaction *saved_signal);
552 
556  chroot_list chroots;
566  std::string session_id;
568  bool force;
570  struct sigaction saved_sighup_signal;
572  struct sigaction saved_sigint_signal;
574  struct sigaction saved_sigterm_signal;
576  struct termios saved_termios;
580  std::string verbosity;
584  std::string shell;
587 
588  protected:
590  std::string cwd;
591  };
592 
593 }
594 
595 #endif /* SBUILD_SESSION_H */
596 
597 /*
598  * Local Variables:
599  * mode:C++
600  * End:
601  */
void clear_sigterm_handler()
Restore the state of SIGTERM prior to setting the handler.
Definition: session.cc:1570
void set_sigint_handler()
Set the SIGINT handler.
Definition: session.cc:1552
Command must have an absolute path.
Definition: session.h:86
void set_auth(auth::auth::ptr &auth)
Set the authentication state associated with this session.
Definition: session.cc:231
void save_termios()
Save terminal state.
Definition: session.cc:333
void run_child(chroot::chroot::ptr &session_chroot)
Run a command or login shell as a child process in the specified chroot.
Definition: session.cc:1266
Child exited abnormally (reason unknown)
Definition: session.h:76
std::string alias
Name used to initially identify the chroot.
Definition: session.h:52
Run a command in an existing session.
Definition: session.h:67
int child_status
The child exit status.
Definition: session.h:562
void set_sigterm_handler()
Set the SIGTERM handler.
Definition: session.cc:1564
Debian source builder components.
Definition: ctty.cc:31
struct sigaction saved_sigint_signal
Signal saved while sigint handler is set.
Definition: session.h:572
Recover an existing (but inactive) session.
Definition: session.h:65
session(const std::string &service, operation operation, const chroot_list &chroots)
The constructor.
Definition: session.cc:190
End a session.
Definition: session.h:66
virtual std::string get_shell(chroot::chroot::ptr &session_chroot) const
Get the shell to run.
Definition: session.cc:896
Failed to lock chroot.
Definition: session.h:82
Shell not available.
Definition: session.h:96
std::string const & get_shell_override() const
Get user-specified login shell.
Definition: session.cc:297
PAM error.
Definition: session.h:93
auth::auth::ptr authstat
Authentication state.
Definition: session.h:554
bool force
The session force status.
Definition: session.h:568
struct sigaction saved_sighup_signal
Signal saved while sighup handler is set.
Definition: session.h:570
Session handler.
Definition: session.h:46
void set_shell_override(const std::string &shell)
Set user-specified login shell.
Definition: session.cc:303
Failed to get supplementary groups.
Definition: session.h:88
std::map< std::string, std::string > string_map
A string map.
Definition: types.h:44
No chroot found matching alias.
Definition: session.h:81
Child terminated by signal.
Definition: session.h:78
std::string verbosity
Message verbosity.
Definition: session.h:580
Container of environment variables.
Definition: environment.h:38
virtual void run_impl()
Run a session.
Definition: session.cc:607
bool get_preserve_environment() const
Check if the environment should be preserved in the chroot.
Definition: session.cc:285
Failed to get supplementary group count.
Definition: session.h:89
Begin, end and run a session automatically.
Definition: session.h:63
Chroot does not support setting a session ID.
Definition: session.h:95
void set_signal_handler(int signal, struct sigaction *saved_signal, void(*handler)(int))
Set a signal handler.
Definition: session.cc:1576
bool get_force() const
Get the force status of this session.
Definition: session.cc:321
chroot_list const & get_chroots() const
Get the chroots to use in this session.
Definition: session.cc:237
virtual void get_command(chroot::chroot::ptr &session_chroot, std::string &file, string_list &command, environment &env) const
Get the command to run.
Definition: session.cc:927
Failed to execute.
Definition: session.h:87
Caught signal.
Definition: session.h:98
bool lock_status
Lock status for locks acquired during chroot setup.
Definition: session.h:560
Chroot not found.
Definition: session.h:83
error_code
Error codes.
Definition: session.h:71
auth::auth::ptr const & get_auth() const
Get the authentication state associated with this session.
Definition: session.cc:225
void setup_chroot(chroot::chroot::ptr &session_chroot, chroot::chroot::setup_type setup_type)
Setup a chroot.
Definition: session.cc:1071
string_map user_options
User-defined options.
Definition: session.h:586
virtual string_list get_command_directories(chroot::chroot::ptr &session_chroot, const environment &env) const
Get a list of directories to change to when running a command Multiple directories are used as fallba...
Definition: session.cc:835
Failed to change to directory.
Definition: session.h:73
operation get_operation() const
Get the operation this session will perform.
Definition: session.cc:249
std::shared_ptr< session > ptr
A shared_ptr to a session object.
Definition: session.h:108
std::string const & get_verbosity() const
Get the message verbosity.
Definition: session.cc:273
bool termios_ok
Are the saved terminal settings valid?
Definition: session.h:578
void clear_sigint_handler()
Restore the state of SIGINT prior to setting the handler.
Definition: session.cc:1558
Failed to set user.
Definition: session.h:100
virtual ~session()
The destructor.
Definition: session.cc:220
std::vector< std::string > string_list
A string vector.
Definition: types.h:38
Failed to drop root permissions.
Definition: session.h:94
struct sigaction saved_sigterm_signal
Signal saved while sigterm handler is set.
Definition: session.h:574
void set_operation(operation operation)
Set the operation this session will perform.
Definition: session.cc:255
virtual auth::auth::status get_chroot_auth_status(auth::auth::status status, const chroot::chroot::ptr &chroot) const
Check if authentication is required for a single chroot, taking users, groups, root-users and root-gr...
Definition: session.cc:482
Custom error.
Definition: custom-error.h:31
Failed to fork child.
Definition: session.h:77
void set_force(bool force)
Set the force status of this session.
Definition: session.cc:327
void set_preserve_environment(bool preserve_environment)
Set if the environment should be preserved in the chroot.
Definition: session.cc:291
Failed to set supplementary groups.
Definition: session.h:91
void set_verbosity(const std::string &verbosity)
Set the message verbosity.
Definition: session.cc:279
std::string cwd
Current working directory.
Definition: session.h:590
std::shared_ptr< chroot > ptr
A shared_ptr to a chroot object.
Definition: chroot.h:103
void clear_sighup_handler()
Restore the state of SIGHUP prior to setting the handler.
Definition: session.cc:1546
void set_sighup_handler()
Set the SIGHUP handler.
Definition: session.cc:1540
void run_chroot(chroot::chroot::ptr &session_chroot)
Run command or login shell in the specified chroot.
Definition: session.cc:1503
Definition: session.h:49
custom_error< error_code > error
Exception type.
Definition: session.h:105
Wait for child failed.
Definition: session.h:79
operation session_operation
The session operation to perform.
Definition: session.h:564
string_map const & get_user_options() const
Get user options.
Definition: session.cc:309
virtual string_list get_shells(chroot::chroot::ptr &session_chroot) const
Get a list of candidate shells to run.
Definition: session.cc:852
Failed to change root to directory.
Definition: session.h:80
Failed to set group.
Definition: session.h:90
std::string shell
Login shell.
Definition: session.h:584
chroot_list chroots
The chroots to run the session operation in.
Definition: session.h:556
std::string session_id
The session identifier.
Definition: session.h:566
std::shared_ptr< auth > ptr
A shared_ptr to a auth object.
Definition: auth.h:102
virtual void get_user_command(chroot::chroot::ptr &session_chroot, std::string &file, string_list &command, const environment &env) const
Get the command to run a user command.
Definition: session.cc:1027
void restore_termios()
Restore terminal state.
Definition: session.cc:355
std::string const & get_session_id() const
Get the session identifier.
Definition: session.cc:261
void wait_for_child(pid_t pid, int &child_status)
Wait for a child process to complete, and check its exit status.
Definition: session.cc:1427
Falling back to directory.
Definition: session.h:74
std::vector< chroot_list_entry > chroot_list
A list of chroots.
Definition: session.h:58
virtual void get_login_command(chroot::chroot::ptr &session_chroot, std::string &file, string_list &command, environment &env) const
Get the command to run a login shell.
Definition: session.cc:941
Failed to unlock chroot.
Definition: session.h:85
status
Authentication status.
Definition: auth.h:78
Child dumped core.
Definition: session.h:75
chroot::chroot::ptr chroot
Pointer to chroot object.
Definition: session.h:54
Falling back to shell.
Definition: session.h:97
bool is_group_member(const std::string &groupname) const
Check group membership.
Definition: session.cc:385
virtual string_list get_login_directories(chroot::chroot::ptr &session_chroot, const environment &env) const
Get a list of directories to change to when running a login shell.
Definition: session.cc:800
Begin a session.
Definition: session.h:64
int get_child_status() const
Get the exit (wait) status of the last child process to run in this session.
Definition: session.cc:373
void set_user_options(const string_map &user_options)
Set user options.
Definition: session.cc:315
User switching is not permitted.
Definition: session.h:101
void run()
Run a session.
Definition: session.cc:552
Setup failed.
Definition: session.h:84
Failed to set signal handler.
Definition: session.h:99
virtual auth::auth::status get_auth_status() const
Check if authentication is required, taking users, groups, root-users and root-groups membership of a...
Definition: session.cc:527
int chroot_status
The current chroot status.
Definition: session.h:558
void get_chroot_membership(const chroot::chroot::ptr &chroot, bool &in_users, bool &in_root_users, bool &in_groups, bool &in_root_groups) const
Get the chroot authentication properties the user is included in.
Definition: session.cc:433
setup_type
Type of setup to perform.
Definition: chroot.h:54
void set_chroots(const chroot_list &chroots)
Set the chroots to use in this session.
Definition: session.cc:243
void clear_signal_handler(int signal, struct sigaction *saved_signal)
Restore the state of the signal prior to setting the handler.
Definition: session.cc:1590
operation
Session operations.
Definition: session.h:61
Group not found.
Definition: session.h:92
void set_session_id(const std::string &session_id)
Set the session identifier.
Definition: session.cc:267
bool preserve_environment
Preserve environment?
Definition: session.h:582
struct termios saved_termios
Saved terminal settings.
Definition: session.h:576