Debian Codegraph @

sshd/session.go

raw
1 package sshd
2
3 import (
4 "fmt"
5 "log/slog"
6 "sort"
7 "strings"
8
9 "github.com/anmitsu/go-shlex"
10 "github.com/armon/go-radix"
11 "golang.org/x/crypto/ssh"
12 "golang.org/x/term"
13 )
14
15 type session struct {
16 l *slog.Logger
17 c *ssh.ServerConn
18 term *term.Terminal
19 commands *radix.Tree
20 cancel func()
21 }
22
23 func NewSession(commands *radix.Tree, conn *ssh.ServerConn, chans <-chan ssh.NewChannel, cancel func(), l *slog.Logger) *session {
24 s := &session{
25 commands: radix.NewFromMap(commands.ToMap()),
26 l: l,
27 c: conn,
28 cancel: cancel,
29 }
30
31 s.commands.Insert("logout", &Command{
32 Name: "logout",
33 ShortDescription: "Ends the current session",
34 Callback: func(a any, args []string, w StringWriter) error {
35 s.Close()
36 return nil
37 },
38 })
39
40 go s.handleChannels(chans)
41 return s
42 }
43
44 func (s *session) handleChannels(chans <-chan ssh.NewChannel) {
45 defer s.Close()
46 for newChannel := range chans {
47 if newChannel.ChannelType() != "session" {
48 s.l.Error("unknown channel type", "sshChannelType", newChannel.ChannelType())
49 newChannel.Reject(ssh.UnknownChannelType, "unknown channel type")
50 continue
51 }
52
53 channel, requests, err := newChannel.Accept()
54 if err != nil {
55 s.l.Warn("could not accept channel", "error", err)
56 continue
57 }
58
59 go s.handleRequests(requests, channel)
60 }
61 }
62
63 func (s *session) handleRequests(in <-chan *ssh.Request, channel ssh.Channel) {
64 for req := range in {
65 var err error
66 switch req.Type {
67 case "shell":
68 if s.term == nil {
69 s.term = s.createTerm(channel)
70 err = req.Reply(true, nil)
71 } else {
72 err = req.Reply(false, nil)
73 }
74
75 case "pty-req":
76 err = req.Reply(true, nil)
77
78 case "window-change":
79 err = req.Reply(true, nil)
80
81 case "exec":
82 var payload = struct{ Value string }{}
83 cErr := ssh.Unmarshal(req.Payload, &payload)
84 if cErr != nil {
85 req.Reply(false, nil)
86 return
87 }
88
89 req.Reply(true, nil)
90 s.dispatchCommand(payload.Value, &stringWriter{channel})
91
92 status := struct{ Status uint32 }{uint32(0)}
93 channel.SendRequest("exit-status", false, ssh.Marshal(status))
94 channel.Close()
95 return
96
97 default:
98 s.l.Debug("Rejected unknown request", "sshRequest", req.Type)
99 err = req.Reply(false, nil)
100 }
101
102 if err != nil {
103 s.l.Info("Error handling ssh session requests", "error", err)
104 return
105 }
106 }
107 }
108
109 func (s *session) createTerm(channel ssh.Channel) *term.Terminal {
110 term := term.NewTerminal(channel, s.c.User()+"@nebula > ")
111 term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
112 // key 9 is tab
113 if key == 9 {
114 cmds := matchCommand(s.commands, line)
115 if len(cmds) == 1 {
116 return cmds[0] + " ", len(cmds[0]) + 1, true
117 }
118
119 sort.Strings(cmds)
120 term.Write([]byte(strings.Join(cmds, "\n") + "\n\n"))
121 }
122
123 return "", 0, false
124 }
125
126 go s.handleInput()
127 return term
128 }
129
130 func (s *session) handleInput() {
131 w := &stringWriter{w: s.term}
132 for {
133 line, err := s.term.ReadLine()
134 if err != nil {
135 break
136 }
137
138 s.dispatchCommand(line, w)
139 }
140 }
141
142 func (s *session) dispatchCommand(line string, w StringWriter) {
143 args, err := shlex.Split(line, true)
144 if err != nil {
145 return
146 }
147
148 if len(args) == 0 {
149 dumpCommands(s.commands, w)
150 return
151 }
152
153 c, err := lookupCommand(s.commands, args[0])
154 if err != nil {
155 return
156 }
157
158 if c == nil {
159 err := w.WriteLine(fmt.Sprintf("did not understand: %s", line))
160 _ = err
161
162 dumpCommands(s.commands, w)
163 return
164 }
165
166 if checkHelpArgs(args) {
167 s.dispatchCommand(fmt.Sprintf("%s %s", "help", c.Name), w)
168 return
169 }
170
171 _ = execCommand(c, args[1:], w)
172 }
173
174 func (s *session) Close() {
175 s.c.Close()
176 s.cancel()
177 }