@@ -816,6 +816,49 @@ func (s *Server) sftpHandler(logger slog.Logger, session ssh.Session) error {
816816return xerrors .Errorf ("sftp server closed with error: %w" ,err )
817817}
818818
819+ func (s * Server )CommandEnv (ei usershell.EnvInfoer ,addEnv []string ) (shell ,dir string ,env []string ,err error ) {
820+ if ei == nil {
821+ ei = & usershell.SystemEnvInfo {}
822+ }
823+
824+ currentUser ,err := ei .User ()
825+ if err != nil {
826+ return "" ,"" ,nil ,xerrors .Errorf ("get current user: %w" ,err )
827+ }
828+ username := currentUser .Username
829+
830+ shell ,err = ei .Shell (username )
831+ if err != nil {
832+ return "" ,"" ,nil ,xerrors .Errorf ("get user shell: %w" ,err )
833+ }
834+
835+ dir = s .config .WorkingDirectory ()
836+
837+ // If the metadata directory doesn't exist, we run the command
838+ // in the users home directory.
839+ _ ,err = os .Stat (dir )
840+ if dir == "" || err != nil {
841+ // Default to user home if a directory is not set.
842+ homedir ,err := ei .HomeDir ()
843+ if err != nil {
844+ return "" ,"" ,nil ,xerrors .Errorf ("get home dir: %w" ,err )
845+ }
846+ dir = homedir
847+ }
848+ env = append (ei .Environ (),addEnv ... )
849+ // Set login variables (see `man login`).
850+ env = append (env ,fmt .Sprintf ("USER=%s" ,username ))
851+ env = append (env ,fmt .Sprintf ("LOGNAME=%s" ,username ))
852+ env = append (env ,fmt .Sprintf ("SHELL=%s" ,shell ))
853+
854+ env ,err = s .config .UpdateEnv (env )
855+ if err != nil {
856+ return "" ,"" ,nil ,xerrors .Errorf ("apply env: %w" ,err )
857+ }
858+
859+ return shell ,dir ,env ,nil
860+ }
861+
819862// CreateCommand processes raw command input with OpenSSH-like behavior.
820863// If the script provided is empty, it will default to the users shell.
821864// This injects environment variables specified by the user at launch too.
@@ -827,15 +870,10 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
827870if ei == nil {
828871ei = & usershell.SystemEnvInfo {}
829872}
830- currentUser ,err := ei .User ()
831- if err != nil {
832- return nil ,xerrors .Errorf ("get current user: %w" ,err )
833- }
834- username := currentUser .Username
835873
836- shell ,err := ei . Shell ( username )
874+ shell ,dir , env , err := s . CommandEnv ( ei , env )
837875if err != nil {
838- return nil ,xerrors .Errorf ("get user shell : %w" ,err )
876+ return nil ,xerrors .Errorf ("prepare command env : %w" ,err )
839877}
840878
841879// OpenSSH executes all commands with the users current shell.
@@ -893,24 +931,8 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
893931)
894932}
895933cmd := s .Execer .PTYCommandContext (ctx ,modifiedName ,modifiedArgs ... )
896- cmd .Dir = s .config .WorkingDirectory ()
897-
898- // If the metadata directory doesn't exist, we run the command
899- // in the users home directory.
900- _ ,err = os .Stat (cmd .Dir )
901- if cmd .Dir == "" || err != nil {
902- // Default to user home if a directory is not set.
903- homedir ,err := ei .HomeDir ()
904- if err != nil {
905- return nil ,xerrors .Errorf ("get home dir: %w" ,err )
906- }
907- cmd .Dir = homedir
908- }
909- cmd .Env = append (ei .Environ (),env ... )
910- // Set login variables (see `man login`).
911- cmd .Env = append (cmd .Env ,fmt .Sprintf ("USER=%s" ,username ))
912- cmd .Env = append (cmd .Env ,fmt .Sprintf ("LOGNAME=%s" ,username ))
913- cmd .Env = append (cmd .Env ,fmt .Sprintf ("SHELL=%s" ,shell ))
934+ cmd .Dir = dir
935+ cmd .Env = env
914936
915937// Set SSH connection environment variables (these are also set by OpenSSH
916938// and thus expected to be present by SSH clients). Since the agent does
@@ -921,11 +943,6 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string,
921943cmd .Env = append (cmd .Env ,fmt .Sprintf ("SSH_CLIENT=%s %s %s" ,srcAddr ,srcPort ,dstPort ))
922944cmd .Env = append (cmd .Env ,fmt .Sprintf ("SSH_CONNECTION=%s %s %s %s" ,srcAddr ,srcPort ,dstAddr ,dstPort ))
923945
924- cmd .Env ,err = s .config .UpdateEnv (cmd .Env )
925- if err != nil {
926- return nil ,xerrors .Errorf ("apply env: %w" ,err )
927- }
928-
929946return cmd ,nil
930947}
931948