| 1 | package ssh |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "sync" |
| 8 | |
| 9 | "github.com/git-lfs/git-lfs/v3/errors" |
| 10 | "github.com/git-lfs/git-lfs/v3/subprocess" |
| 11 | "github.com/git-lfs/git-lfs/v3/tr" |
| 12 | ) |
| 13 | |
| 14 | type PktlineConnection struct { |
| 15 | r io.ReadCloser |
| 16 | w io.WriteCloser |
| 17 | mu sync.Mutex |
| 18 | cmd *subprocess.Cmd |
| 19 | pl Pktline |
| 20 | } |
| 21 | |
| 22 | func (conn *PktlineConnection) Lock() { |
| 23 | conn.mu.Lock() |
| 24 | } |
| 25 | |
| 26 | func (conn *PktlineConnection) Unlock() { |
| 27 | conn.mu.Unlock() |
| 28 | } |
| 29 | |
| 30 | func (conn *PktlineConnection) Start() error { |
| 31 | conn.Lock() |
| 32 | defer conn.Unlock() |
| 33 | return conn.negotiateVersion() |
| 34 | } |
| 35 | |
| 36 | func (conn *PktlineConnection) End() error { |
| 37 | conn.Lock() |
| 38 | defer conn.Unlock() |
| 39 | err := conn.SendMessage("quit", nil) |
| 40 | if err != nil { |
| 41 | return err |
| 42 | } |
| 43 | _, err = conn.ReadStatus() |
| 44 | conn.r.Close() |
| 45 | conn.w.Close() |
| 46 | conn.cmd.Wait() |
| 47 | return err |
| 48 | } |
| 49 | |
| 50 | func (conn *PktlineConnection) negotiateVersion() error { |
| 51 | pkts, err := conn.pl.ReadPacketList() |
| 52 | if err != nil { |
| 53 | return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to read capabilities)"), err) |
| 54 | } |
| 55 | ok := false |
| 56 | for _, line := range pkts { |
| 57 | if line == "version=1" { |
| 58 | ok = true |
| 59 | } |
| 60 | } |
| 61 | if !ok { |
| 62 | return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (missing version=1)"), nil) |
| 63 | } |
| 64 | err = conn.SendMessage("version 1", nil) |
| 65 | if err != nil { |
| 66 | return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to send version)"), err) |
| 67 | } |
| 68 | status, args, _, err := conn.ReadStatusWithLines() |
| 69 | if err != nil { |
| 70 | return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unable to read status)"), err) |
| 71 | } |
| 72 | if status != 200 { |
| 73 | text := tr.Tr.Get("no error provided") |
| 74 | if len(args) > 0 { |
| 75 | text = tr.Tr.Get("server said: %q", args[0]) |
| 76 | } |
| 77 | return errors.NewProtocolError(tr.Tr.Get("Unable to negotiate version with remote side (unexpected status %d; %s)", status, text), nil) |
| 78 | } |
| 79 | return nil |
| 80 | } |
| 81 | |
| 82 | func (conn *PktlineConnection) SendMessage(command string, args []string) error { |
| 83 | err := conn.pl.WritePacketText(command) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | for _, arg := range args { |
| 88 | err = conn.pl.WritePacketText(arg) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | } |
| 93 | return conn.pl.WriteFlush() |
| 94 | } |
| 95 | |
| 96 | func (conn *PktlineConnection) SendMessageWithLines(command string, args []string, lines []string) error { |
| 97 | err := conn.pl.WritePacketText(command) |
| 98 | if err != nil { |
| 99 | return err |
| 100 | } |
| 101 | for _, arg := range args { |
| 102 | err = conn.pl.WritePacketText(arg) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | } |
| 107 | err = conn.pl.WriteDelim() |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | for _, line := range lines { |
| 112 | err = conn.pl.WritePacketText(line) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | } |
| 117 | return conn.pl.WriteFlush() |
| 118 | } |
| 119 | |
| 120 | func (conn *PktlineConnection) SendMessageWithData(command string, args []string, data io.Reader) error { |
| 121 | err := conn.pl.WritePacketText(command) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | for _, arg := range args { |
| 126 | err = conn.pl.WritePacketText(arg) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | err = conn.pl.WriteDelim() |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | buf := make([]byte, 32768) |
| 136 | for { |
| 137 | n, err := data.Read(buf) |
| 138 | if n > 0 { |
| 139 | err := conn.pl.WritePacket(buf[0:n]) |
| 140 | if err != nil { |
| 141 | return err |
| 142 | } |
| 143 | } |
| 144 | if err != nil { |
| 145 | break |
| 146 | } |
| 147 | } |
| 148 | return conn.pl.WriteFlush() |
| 149 | } |
| 150 | |
| 151 | func (conn *PktlineConnection) ReadStatus() (int, error) { |
| 152 | status := 0 |
| 153 | seenStatus := false |
| 154 | for { |
| 155 | s, pktLen, err := conn.pl.ReadPacketTextWithLength() |
| 156 | if err != nil { |
| 157 | return 0, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err) |
| 158 | } |
| 159 | switch { |
| 160 | case pktLen == 0: |
| 161 | if !seenStatus { |
| 162 | return 0, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil) |
| 163 | } |
| 164 | return status, nil |
| 165 | case !seenStatus: |
| 166 | ok := false |
| 167 | if strings.HasPrefix(s, "status ") { |
| 168 | status, err = strconv.Atoi(s[7:]) |
| 169 | ok = err == nil |
| 170 | } |
| 171 | if !ok { |
| 172 | return 0, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err) |
| 173 | } |
| 174 | seenStatus = true |
| 175 | default: |
| 176 | return 0, errors.NewProtocolError(tr.Tr.Get("unexpected data, got %q", s), err) |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // ReadStatusWithData reads a status, arguments, and any binary data. Note that |
| 182 | // the reader must be fully exhausted before invoking any other read methods. |
| 183 | func (conn *PktlineConnection) ReadStatusWithData() (int, []string, io.Reader, error) { |
| 184 | args := make([]string, 0, 100) |
| 185 | status := 0 |
| 186 | seenStatus := false |
| 187 | for { |
| 188 | s, pktLen, err := conn.pl.ReadPacketTextWithLength() |
| 189 | if err != nil { |
| 190 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err) |
| 191 | } |
| 192 | if pktLen == 0 { |
| 193 | if !seenStatus { |
| 194 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil) |
| 195 | } |
| 196 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("unexpected flush packet"), nil) |
| 197 | } else if !seenStatus { |
| 198 | ok := false |
| 199 | if strings.HasPrefix(s, "status ") { |
| 200 | status, err = strconv.Atoi(s[7:]) |
| 201 | ok = err == nil |
| 202 | } |
| 203 | if !ok { |
| 204 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err) |
| 205 | } |
| 206 | seenStatus = true |
| 207 | } else if pktLen == 1 { |
| 208 | break |
| 209 | } else { |
| 210 | args = append(args, s) |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return status, args, pktlineReader(conn.pl), nil |
| 215 | } |
| 216 | |
| 217 | // ReadStatusWithLines reads a status, arguments, and a set of text lines. |
| 218 | func (conn *PktlineConnection) ReadStatusWithLines() (int, []string, []string, error) { |
| 219 | args := make([]string, 0, 100) |
| 220 | lines := make([]string, 0, 100) |
| 221 | status := 0 |
| 222 | seenDelim := false |
| 223 | seenStatus := false |
| 224 | for { |
| 225 | s, pktLen, err := conn.pl.ReadPacketTextWithLength() |
| 226 | if err != nil { |
| 227 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("error reading packet"), err) |
| 228 | } |
| 229 | switch { |
| 230 | case pktLen == 0: |
| 231 | if !seenStatus { |
| 232 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("no status seen"), nil) |
| 233 | } |
| 234 | return status, args, lines, nil |
| 235 | case seenDelim: |
| 236 | lines = append(lines, s) |
| 237 | case !seenStatus: |
| 238 | ok := false |
| 239 | if strings.HasPrefix(s, "status ") { |
| 240 | status, err = strconv.Atoi(s[7:]) |
| 241 | ok = err == nil |
| 242 | } |
| 243 | if !ok { |
| 244 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("expected status line, got %q", s), err) |
| 245 | } |
| 246 | seenStatus = true |
| 247 | case pktLen == 1: |
| 248 | if seenDelim { |
| 249 | return 0, nil, nil, errors.NewProtocolError(tr.Tr.Get("unexpected delimiter packet"), nil) |
| 250 | } |
| 251 | seenDelim = true |
| 252 | default: |
| 253 | args = append(args, s) |
| 254 | } |
| 255 | } |
| 256 | } |