00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SBUILD_SESSION_H
00020 #define SBUILD_SESSION_H
00021
00022 #include <sbuild/sbuild-auth.h>
00023 #include <sbuild/sbuild-chroot.h>
00024 #include <sbuild/sbuild-custom-error.h>
00025
00026 #include <string>
00027
00028 #include <signal.h>
00029 #include <sys/types.h>
00030 #include <termios.h>
00031 #include <unistd.h>
00032
00033 namespace sbuild
00034 {
00035
00046 class session
00047 {
00048 public:
00049 struct chroot_list_entry
00050 {
00052 std::string alias;
00054 sbuild::chroot::ptr chroot;
00055 };
00056
00058 typedef std::vector<chroot_list_entry> chroot_list;
00059
00061 enum operation
00062 {
00063 OPERATION_AUTOMATIC,
00064 OPERATION_BEGIN,
00065 OPERATION_RECOVER,
00066 OPERATION_END,
00067 OPERATION_RUN
00068 };
00069
00071 enum error_code
00072 {
00073 CHDIR,
00074 CHDIR_FB,
00075 CHILD_CORE,
00076 CHILD_FAIL,
00077 CHILD_FORK,
00078 CHILD_SIGNAL,
00079 CHILD_WAIT,
00080 CHROOT,
00081 CHROOT_ALIAS,
00082 CHROOT_LOCK,
00083 CHROOT_NOTFOUND,
00084 CHROOT_SETUP,
00085 CHROOT_UNLOCK,
00086 COMMAND_ABS,
00087 EXEC,
00088 GROUP_GET_SUP,
00089 GROUP_GET_SUPC,
00090 GROUP_SET,
00091 GROUP_SET_SUP,
00092 GROUP_UNKNOWN,
00093 PAM,
00094 ROOT_DROP,
00095 SET_SESSION_ID,
00096 SHELL,
00097 SHELL_FB,
00098 SIGNAL_CATCH,
00099 SIGNAL_SET,
00100 USER_SET,
00101 USER_SWITCH
00102 };
00103
00105 typedef custom_error<error_code> error;
00106
00108 typedef std::shared_ptr<session> ptr;
00109
00117 session (std::string const& service,
00118 operation operation,
00119 chroot_list const& chroots);
00120
00122 virtual ~session ();
00123
00129 auth::ptr const&
00130 get_auth () const;
00131
00137 void
00138 set_auth (auth::ptr& auth);
00139
00145 chroot_list const&
00146 get_chroots () const;
00147
00153 void
00154 set_chroots (chroot_list const& chroots);
00155
00161 operation
00162 get_operation () const;
00163
00169 void
00170 set_operation (operation operation);
00171
00178 std::string const&
00179 get_session_id () const;
00180
00187 void
00188 set_session_id (std::string const& session_id);
00189
00195 std::string const&
00196 get_verbosity () const;
00197
00204 void
00205 set_verbosity (std::string const& verbosity);
00206
00212 bool
00213 get_preserve_environment () const;
00214
00220 void
00221 set_preserve_environment (bool preserve_environment);
00222
00228 std::string const&
00229 get_shell_override () const;
00230
00236 void
00237 set_shell_override (std::string const& shell);
00238
00244 string_map const&
00245 get_user_options () const;
00246
00252 void
00253 set_user_options (string_map const& user_options);
00254
00260 bool
00261 get_force () const;
00262
00268 void
00269 set_force (bool force);
00270
00274 void
00275 save_termios ();
00276
00280 void
00281 restore_termios ();
00282
00289 int
00290 get_child_status () const;
00291
00298 bool
00299 is_group_member (std::string const& groupname) const;
00300
00301 protected:
00305 void
00306 get_chroot_membership (chroot::ptr const& chroot,
00307 bool& in_users,
00308 bool& in_root_users,
00309 bool& in_groups,
00310 bool& in_root_groups) const;
00311
00317 virtual auth::status
00318 get_chroot_auth_status (auth::status status,
00319 chroot::ptr const& chroot) const;
00320
00321 public:
00327 virtual sbuild::auth::status
00328 get_auth_status () const;
00329
00336 void
00337 run ();
00338
00339 protected:
00347 virtual void
00348 run_impl ();
00349
00358 virtual string_list
00359 get_login_directories (sbuild::chroot::ptr& session_chroot,
00360 environment const& env) const;
00361
00370 virtual string_list
00371 get_command_directories (sbuild::chroot::ptr& session_chroot,
00372 environment const& env) const;
00373
00382 virtual string_list
00383 get_shells (sbuild::chroot::ptr& session_chroot) const;
00384
00393 virtual std::string
00394 get_shell (sbuild::chroot::ptr& session_chroot) const;
00395
00404 virtual void
00405 get_command (chroot::ptr& session_chroot,
00406 std::string& file,
00407 string_list& command,
00408 environment& env) const;
00409
00418 virtual void
00419 get_login_command (chroot::ptr& session_chroot,
00420 std::string& file,
00421 string_list& command,
00422 environment& env) const;
00423
00432 virtual void
00433 get_user_command (chroot::ptr& session_chroot,
00434 std::string& file,
00435 string_list& command,
00436 environment const& env) const;
00437
00438 private:
00451 void
00452 setup_chroot (chroot::ptr& session_chroot,
00453 chroot::setup_type setup_type);
00454
00462 void
00463 run_chroot (chroot::ptr& session_chroot);
00464
00472 void
00473 run_child (chroot::ptr& session_chroot);
00474
00483 void
00484 wait_for_child (pid_t pid,
00485 int& child_status);
00486
00492 void
00493 set_sighup_handler ();
00494
00498 void
00499 clear_sighup_handler ();
00500
00506 void
00507 set_sigint_handler ();
00508
00512 void
00513 clear_sigint_handler ();
00514
00520 void
00521 set_sigterm_handler ();
00522
00526 void
00527 clear_sigterm_handler ();
00528
00537 void
00538 set_signal_handler (int signal,
00539 struct sigaction *saved_signal,
00540 void (*handler)(int));
00541
00549 void
00550 clear_signal_handler (int signal,
00551 struct sigaction *saved_signal);
00552
00554 auth::ptr authstat;
00556 chroot_list chroots;
00558 int chroot_status;
00560 bool lock_status;
00562 int child_status;
00564 operation session_operation;
00566 std::string session_id;
00568 bool force;
00570 struct sigaction saved_sighup_signal;
00572 struct sigaction saved_sigint_signal;
00574 struct sigaction saved_sigterm_signal;
00576 struct termios saved_termios;
00578 bool termios_ok;
00580 std::string verbosity;
00582 bool preserve_environment;
00584 std::string shell;
00586 string_map user_options;
00587
00588 protected:
00590 std::string cwd;
00591 };
00592
00593 }
00594
00595 #endif
00596
00597
00598
00599
00600
00601