sbuild-session.h

00001 /* Copyright © 2005-2007  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 3 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, see
00015  * <http://www.gnu.org/licenses/>.
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:
00050     typedef std::vector<chroot::ptr> chroot_list;
00051 
00053     enum operation
00054       {
00055         OPERATION_AUTOMATIC, 
00056         OPERATION_BEGIN,     
00057         OPERATION_RECOVER,   
00058         OPERATION_END,       
00059         OPERATION_RUN        
00060       };
00061 
00063     enum error_code
00064       {
00065         CHDIR,          
00066         CHDIR_FB,       
00067         CHILD_CORE,     
00068         CHILD_FAIL,     
00069         CHILD_FORK,     
00070         CHILD_SIGNAL,   
00071         CHILD_WAIT,     
00072         CHROOT,         
00073         CHROOT_ALIAS,   
00074         CHROOT_LOCK,    
00075         CHROOT_NOTFOUND,
00076         CHROOT_SETUP,   
00077         CHROOT_UNLOCK,  
00078         COMMAND_ABS,    
00079         EXEC,           
00080         GROUP_GET_SUP,  
00081         GROUP_GET_SUPC, 
00082         GROUP_SET,      
00083         GROUP_SET_SUP,  
00084         GROUP_UNKNOWN,  
00085         PAM,            
00086         ROOT_DROP,      
00087         SET_SESSION_ID, 
00088         SHELL,          
00089         SHELL_FB,       
00090         SIGNAL_CATCH,   
00091         SIGNAL_SET,     
00092         USER_SET,       
00093         USER_SWITCH     
00094       };
00095 
00097     typedef custom_error<error_code> error;
00098 
00100     typedef std::tr1::shared_ptr<session> ptr;
00101 
00109     session (std::string const& service,
00110              operation          operation,
00111              chroot_list const& chroots);
00112 
00114     virtual ~session ();
00115 
00121     auth::ptr const&
00122     get_auth () const;
00123 
00129     void
00130     set_auth (auth::ptr& auth);
00131 
00137     chroot_list const&
00138     get_chroots () const;
00139 
00145     void
00146     set_chroots (chroot_list const& chroots);
00147 
00153     operation
00154     get_operation () const;
00155 
00161     void
00162     set_operation (operation operation);
00163 
00170     std::string const&
00171     get_session_id () const;
00172 
00179     void
00180     set_session_id (std::string const& session_id);
00181 
00187     std::string const&
00188     get_verbosity () const;
00189 
00196     void
00197     set_verbosity (std::string const& verbosity);
00198 
00204     bool
00205     get_preserve_environment () const;
00206 
00212     void
00213     set_preserve_environment (bool preserve_environment);
00214 
00220     bool
00221     get_force () const;
00222 
00228     void
00229     set_force (bool force);
00230 
00234     void
00235     save_termios ();
00236 
00240     void
00241     restore_termios ();
00242 
00249     int
00250     get_child_status () const;
00251 
00252   protected:
00256     void
00257     get_chroot_membership (chroot::ptr const& chroot,
00258                            bool&              in_users,
00259                            bool&              in_root_users,
00260                            bool&              in_groups,
00261                            bool&              in_root_groups) const;
00262 
00268     virtual auth::status
00269     get_chroot_auth_status (auth::status status,
00270                             chroot::ptr const& chroot) const;
00271 
00272   public:
00278     virtual sbuild::auth::status
00279     get_auth_status () const;
00280 
00287     void
00288     run ();
00289 
00290   protected:
00298     virtual void
00299     run_impl ();
00300 
00309     virtual string_list
00310     get_login_directories (sbuild::chroot::ptr& session_chroot,
00311                            environment const&   env) const;
00312 
00321     virtual string_list
00322     get_command_directories (sbuild::chroot::ptr& session_chroot,
00323                              environment const&   env) const;
00324 
00332     virtual std::string
00333     get_shell () const;
00334 
00343     virtual void
00344     get_command (chroot::ptr&       session_chroot,
00345                  std::string&       file,
00346                  string_list&       command,
00347                  environment const& env) const;
00348 
00356     virtual void
00357     get_login_command (chroot::ptr& session_chroot,
00358                        std::string& file,
00359                        string_list& command) const;
00360 
00369     virtual void
00370     get_user_command (chroot::ptr&       session_chroot,
00371                       std::string&       file,
00372                       string_list&       command,
00373                       environment const& env) const;
00374 
00375   private:
00388     void
00389     setup_chroot (chroot::ptr&       session_chroot,
00390                   chroot::setup_type setup_type);
00391 
00399     void
00400     run_chroot (chroot::ptr& session_chroot);
00401 
00409     void
00410     run_child (chroot::ptr& session_chroot);
00411 
00420     void
00421     wait_for_child (pid_t pid,
00422                     int&  child_status);
00423 
00429     void
00430     set_sighup_handler ();
00431 
00435     void
00436     clear_sighup_handler ();
00437 
00443     void
00444     set_sigint_handler ();
00445 
00449     void
00450     clear_sigint_handler ();
00451 
00457     void
00458     set_sigterm_handler ();
00459 
00463     void
00464     clear_sigterm_handler ();
00465 
00474     void
00475     set_signal_handler (int                signal,
00476                         struct sigaction  *saved_signal,
00477                         void             (*handler)(int));
00478 
00486     void
00487     clear_signal_handler (int               signal,
00488                           struct sigaction *saved_signal);
00489 
00491     auth::ptr        authstat;
00493     chroot_list      chroots;
00495     int              chroot_status;
00497     bool lock_status;
00499     int              child_status;
00501     operation        session_operation;
00503     std::string      session_id;
00505     bool             force;
00507     struct sigaction saved_sighup_signal;
00509     struct sigaction saved_sigint_signal;
00511     struct sigaction saved_sigterm_signal;
00513     struct termios saved_termios;
00515     bool termios_ok;
00517     std::string verbosity;
00519     bool        preserve_environment;
00520 
00521   protected:
00523     std::string      cwd;
00524   };
00525 
00526 }
00527 
00528 #endif /* SBUILD_SESSION_H */
00529 
00530 /*
00531  * Local Variables:
00532  * mode:C++
00533  * End:
00534  */