This commit is contained in:
Viktor Liu
2025-07-02 21:31:57 +02:00
parent 0d5408baec
commit 5970591d24
8 changed files with 59 additions and 189 deletions

View File

@@ -6,7 +6,6 @@ import (
"io"
"os"
"os/exec"
"os/user"
"runtime"
"time"
@@ -150,44 +149,6 @@ func (s *Server) handleCommandIO(logger *log.Entry, stdinPipe io.WriteCloser, se
}
}
// createPtyCommandWithPrivileges creates the exec.Cmd for Pty execution respecting privilege check results
func (s *Server) createPtyCommandWithPrivileges(cmd []string, privilegeResult PrivilegeCheckResult, ptyReq ssh.Pty, session ssh.Session) (*exec.Cmd, error) {
localUser := privilegeResult.User
if privilegeResult.RequiresUserSwitching {
return s.createPtyUserSwitchCommand(cmd, localUser, ptyReq, session)
}
// No user switching needed - create direct Pty command
shell := getUserShell(localUser.Uid)
rawCmd := session.RawCommand()
args := s.getShellCommandArgs(shell, rawCmd)
execCmd := exec.CommandContext(session.Context(), args[0], args[1:]...)
execCmd.Dir = localUser.HomeDir
execCmd.Env = s.preparePtyEnv(localUser, ptyReq, session)
return execCmd, nil
}
// preparePtyEnv prepares environment variables for Pty execution
func (s *Server) preparePtyEnv(localUser *user.User, ptyReq ssh.Pty, session ssh.Session) []string {
termType := ptyReq.Term
if termType == "" {
termType = "xterm-256color"
}
env := prepareUserEnv(localUser, getUserShell(localUser.Uid))
env = append(env, prepareSSHEnv(session)...)
env = append(env, fmt.Sprintf("TERM=%s", termType))
for _, v := range session.Environ() {
if acceptEnv(v) {
env = append(env, v)
}
}
return env
}
// waitForCommandCleanup waits for command completion with session disconnect handling
func (s *Server) waitForCommandCleanup(logger *log.Entry, session ssh.Session, execCmd *exec.Cmd) bool {
ctx := session.Context()