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-config.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:
00050 enum operation
00051 {
00052 OPERATION_AUTOMATIC,
00053 OPERATION_BEGIN,
00054 OPERATION_RECOVER,
00055 OPERATION_END,
00056 OPERATION_RUN
00057 };
00058
00060 enum error_code
00061 {
00062 CHDIR,
00063 CHDIR_FB,
00064 CHILD_CORE,
00065 CHILD_FAIL,
00066 CHILD_FORK,
00067 CHILD_SIGNAL,
00068 CHILD_WAIT,
00069 CHROOT,
00070 CHROOT_ALIAS,
00071 CHROOT_LOCK,
00072 CHROOT_NOTFOUND,
00073 CHROOT_SETUP,
00074 CHROOT_UNLOCK,
00075 COMMAND_ABS,
00076 EXEC,
00077 GROUP_GET_SUP,
00078 GROUP_GET_SUPC,
00079 GROUP_SET,
00080 GROUP_SET_SUP,
00081 GROUP_UNKNOWN,
00082 PAM,
00083 ROOT_DROP,
00084 SET_SESSION_ID,
00085 SHELL,
00086 SHELL_FB,
00087 SIGNAL_CATCH,
00088 SIGNAL_SET,
00089 USER_SET,
00090 USER_SWITCH
00091 };
00092
00094 typedef custom_error<error_code> error;
00095
00097 typedef std::tr1::shared_ptr<chroot_config> config_ptr;
00098
00100 typedef std::tr1::shared_ptr<session> ptr;
00101
00110 session (std::string const& service,
00111 config_ptr& config,
00112 operation operation,
00113 string_list const& chroots);
00114
00116 virtual ~session ();
00117
00123 auth::ptr const&
00124 get_auth () const;
00125
00131 void
00132 set_auth (auth::ptr& auth);
00133
00139 config_ptr const&
00140 get_config () const;
00141
00147 void
00148 set_config (config_ptr& config);
00149
00155 string_list const&
00156 get_chroots () const;
00157
00163 void
00164 set_chroots (string_list const& chroots);
00165
00171 operation
00172 get_operation () const;
00173
00179 void
00180 set_operation (operation operation);
00181
00188 std::string const&
00189 get_session_id () const;
00190
00197 void
00198 set_session_id (std::string const& session_id);
00199
00205 std::string const&
00206 get_verbosity () const;
00207
00214 void
00215 set_verbosity (std::string const& verbosity);
00216
00222 bool
00223 get_preserve_environment () const;
00224
00230 void
00231 set_preserve_environment (bool preserve_environment);
00232
00238 bool
00239 get_force () const;
00240
00246 void
00247 set_force (bool force);
00248
00252 void
00253 save_termios ();
00254
00258 void
00259 restore_termios ();
00260
00267 int
00268 get_child_status () const;
00269
00270 protected:
00274 void
00275 get_chroot_membership (chroot::ptr const& chroot,
00276 bool& in_users,
00277 bool& in_root_users,
00278 bool& in_groups,
00279 bool& in_root_groups) const;
00280
00286 virtual auth::status
00287 get_chroot_auth_status (auth::status status,
00288 chroot::ptr const& chroot) const;
00289
00290 public:
00296 virtual sbuild::auth::status
00297 get_auth_status () const;
00298
00305 void
00306 run ();
00307
00308 protected:
00316 virtual void
00317 run_impl ();
00318
00328 virtual string_list
00329 get_login_directories (sbuild::chroot::ptr& session_chroot,
00330 environment const& env) const;
00331
00341 virtual string_list
00342 get_command_directories (sbuild::chroot::ptr& session_chroot,
00343 environment const& env) const;
00344
00352 virtual std::string
00353 get_shell () const;
00354
00364 virtual void
00365 get_command (chroot::ptr& session_chroot,
00366 std::string& file,
00367 string_list& command,
00368 environment const& env) const;
00369
00378 virtual void
00379 get_login_command (chroot::ptr& session_chroot,
00380 std::string& file,
00381 string_list& command) const;
00382
00392 virtual void
00393 get_user_command (chroot::ptr& session_chroot,
00394 std::string& file,
00395 string_list& command,
00396 environment const& env) const;
00397
00398 private:
00412 void
00413 setup_chroot (chroot::ptr& session_chroot,
00414 chroot::setup_type setup_type);
00415
00424 void
00425 run_chroot (chroot::ptr& session_chroot);
00426
00435 void
00436 run_child (chroot::ptr& session_chroot);
00437
00446 void
00447 wait_for_child (pid_t pid,
00448 int& child_status);
00449
00455 void
00456 set_sighup_handler ();
00457
00461 void
00462 clear_sighup_handler ();
00463
00469 void
00470 set_sigint_handler ();
00471
00475 void
00476 clear_sigint_handler ();
00477
00483 void
00484 set_sigterm_handler ();
00485
00489 void
00490 clear_sigterm_handler ();
00491
00500 void
00501 set_signal_handler (int signal,
00502 struct sigaction *saved_signal,
00503 void (*handler)(int));
00504
00512 void
00513 clear_signal_handler (int signal,
00514 struct sigaction *saved_signal);
00515
00517 auth::ptr authstat;
00519 config_ptr config;
00521 string_list chroots;
00523 int chroot_status;
00525 bool lock_status;
00527 int child_status;
00529 operation session_operation;
00531 std::string session_id;
00533 bool force;
00535 struct sigaction saved_sighup_signal;
00537 struct sigaction saved_sigint_signal;
00539 struct sigaction saved_sigterm_signal;
00541 struct termios saved_termios;
00543 bool termios_ok;
00545 std::string verbosity;
00547 bool preserve_environment;
00548
00549 protected:
00551 std::string cwd;
00552 };
00553
00554 }
00555
00556 #endif
00557
00558
00559
00560
00561
00562