sbuild  1.4.26
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 
00258     bool
00259     is_group_member (std::string const& groupname) const;
00260 
00261   protected:
00265     void
00266     get_chroot_membership (chroot::ptr const& chroot,
00267                            bool&              in_users,
00268                            bool&              in_root_users,
00269                            bool&              in_groups,
00270                            bool&              in_root_groups) const;
00271 
00277     virtual auth::status
00278     get_chroot_auth_status (auth::status status,
00279                             chroot::ptr const& chroot) const;
00280 
00281   public:
00287     virtual sbuild::auth::status
00288     get_auth_status () const;
00289 
00296     void
00297     run ();
00298 
00299   protected:
00307     virtual void
00308     run_impl ();
00309 
00318     virtual string_list
00319     get_login_directories (sbuild::chroot::ptr& session_chroot,
00320                            environment const&   env) const;
00321 
00330     virtual string_list
00331     get_command_directories (sbuild::chroot::ptr& session_chroot,
00332                              environment const&   env) const;
00333 
00341     virtual std::string
00342     get_shell () const;
00343 
00352     virtual void
00353     get_command (chroot::ptr&       session_chroot,
00354                  std::string&       file,
00355                  string_list&       command,
00356                  environment const& env) const;
00357 
00365     virtual void
00366     get_login_command (chroot::ptr& session_chroot,
00367                        std::string& file,
00368                        string_list& command) const;
00369 
00378     virtual void
00379     get_user_command (chroot::ptr&       session_chroot,
00380                       std::string&       file,
00381                       string_list&       command,
00382                       environment const& env) const;
00383 
00384   private:
00397     void
00398     setup_chroot (chroot::ptr&       session_chroot,
00399                   chroot::setup_type setup_type);
00400 
00408     void
00409     run_chroot (chroot::ptr& session_chroot);
00410 
00418     void
00419     run_child (chroot::ptr& session_chroot);
00420 
00429     void
00430     wait_for_child (pid_t pid,
00431                     int&  child_status);
00432 
00438     void
00439     set_sighup_handler ();
00440 
00444     void
00445     clear_sighup_handler ();
00446 
00452     void
00453     set_sigint_handler ();
00454 
00458     void
00459     clear_sigint_handler ();
00460 
00466     void
00467     set_sigterm_handler ();
00468 
00472     void
00473     clear_sigterm_handler ();
00474 
00483     void
00484     set_signal_handler (int                signal,
00485                         struct sigaction  *saved_signal,
00486                         void             (*handler)(int));
00487 
00495     void
00496     clear_signal_handler (int               signal,
00497                           struct sigaction *saved_signal);
00498 
00500     auth::ptr        authstat;
00502     chroot_list      chroots;
00504     int              chroot_status;
00506     bool lock_status;
00508     int              child_status;
00510     operation        session_operation;
00512     std::string      session_id;
00514     bool             force;
00516     struct sigaction saved_sighup_signal;
00518     struct sigaction saved_sigint_signal;
00520     struct sigaction saved_sigterm_signal;
00522     struct termios saved_termios;
00524     bool termios_ok;
00526     std::string verbosity;
00528     bool        preserve_environment;
00529 
00530   protected:
00532     std::string      cwd;
00533   };
00534 
00535 }
00536 
00537 #endif /* SBUILD_SESSION_H */
00538 
00539 /*
00540  * Local Variables:
00541  * mode:C++
00542  * End:
00543  */