sessionHandler handles a single SSH session (shell or exec).
(sess ssh.Session)
| 65 | |
| 66 | // sessionHandler handles a single SSH session (shell or exec). |
| 67 | func sessionHandler(sess ssh.Session) { |
| 68 | u, err := user.Current() |
| 69 | if err != nil { |
| 70 | fmt.Fprintf(sess.Stderr(), "failed to get current user: %v\r\n", err) |
| 71 | sess.Exit(1) |
| 72 | return |
| 73 | } |
| 74 | |
| 75 | shell := loginShell(u) |
| 76 | rawCmd := sess.RawCommand() |
| 77 | |
| 78 | var args []string |
| 79 | if rawCmd == "" { |
| 80 | args = []string{shell, "-l"} |
| 81 | } else { |
| 82 | args = []string{shell, "-c", rawCmd} |
| 83 | } |
| 84 | |
| 85 | cmd := exec.Command(args[0], args[1:]...) |
| 86 | cmd.Dir = u.HomeDir |
| 87 | |
| 88 | cmd.Env = []string{ |
| 89 | "SHELL=" + shell, |
| 90 | "USER=" + u.Username, |
| 91 | "HOME=" + u.HomeDir, |
| 92 | "PATH=" + defaultPath(u), |
| 93 | } |
| 94 | for _, env := range sess.Environ() { |
| 95 | if acceptEnvPair(env) { |
| 96 | cmd.Env = append(cmd.Env, env) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | ptyReq, winCh, isPTY := sess.Pty() |
| 101 | if isPTY { |
| 102 | sess.DisablePTYEmulation() |
| 103 | runWithPTY(sess, cmd, ptyReq, winCh) |
| 104 | } else { |
| 105 | runWithPipes(sess, cmd) |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // runWithPTY runs cmd attached to a pseudo-terminal. |
| 110 | func runWithPTY(sess ssh.Session, cmd *exec.Cmd, ptyReq ssh.Pty, winCh <-chan ssh.Window) { |
nothing calls this directly
no test coverage detected