all repos

mugit @ da1546430020daf5b14c456278e45471dff4d259

🐮 git server that your cow will love

mugit/internal/ssh/ssh.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
ssh: dont write slog to output, 1 month ago
1
package ssh
2
3
import (
4
	"context"
5
	"errors"
6
	"fmt"
7
	"io"
8
	"strings"
9
10
	"olexsmir.xyz/mugit/internal/config"
11
	"olexsmir.xyz/mugit/internal/git"
12
13
	gossh "golang.org/x/crypto/ssh"
14
)
15
16
type Shell struct {
17
	cfg *config.Config
18
19
	keys []gossh.PublicKey
20
}
21
22
func NewShell(cfg *config.Config) (*Shell, error) {
23
	parsedKeys := make([]gossh.PublicKey, len(cfg.SSH.Keys))
24
	for i, key := range cfg.SSH.Keys {
25
		pkey, _, _, _, err := gossh.ParseAuthorizedKey([]byte(key))
26
		if err != nil {
27
			return nil, err
28
		}
29
		parsedKeys[i] = pkey
30
	}
31
32
	return &Shell{
33
		cfg:  cfg,
34
		keys: parsedKeys,
35
	}, nil
36
}
37
38
var validCommands = map[string]bool{
39
	"git-upload-pack":    true,
40
	"git-upload-archive": true,
41
	"git-receive-pack":   true,
42
}
43
44
func (s *Shell) HandleCommand(ctx context.Context, cmd string, stdin io.Reader, stdout, stderr io.Writer) error {
45
	gitCmd, repoName, err := s.parseCommand(cmd)
46
	if err != nil {
47
		return s.replyWithGitError(stderr, "access denied: invalid command", err)
48
	}
49
50
	if !validCommands[gitCmd] {
51
		msg := "access denied: invalid git command"
52
		return s.replyWithGitError(stderr, msg, errors.New(msg))
53
	}
54
55
	repoPath, err := git.ResolvePath(s.cfg.Repo.Dir, git.ResolveName(repoName))
56
	if err != nil {
57
		return s.replyWithGitError(stderr, "access denied", err)
58
	}
59
60
	repo, err := git.Open(repoPath, "")
61
	if err != nil {
62
		return s.replyWithGitError(stderr, "repository not found", err)
63
	}
64
65
	switch gitCmd {
66
	case "git-upload-pack":
67
		err = repo.UploadPack(ctx, false, "", stdin, stdout)
68
	case "git-upload-archive":
69
		err = repo.UploadArchive(ctx, stdin, stdout)
70
	case "git-receive-pack":
71
		err = repo.ReceivePack(ctx, stdin, stdout, stderr)
72
	default:
73
		msg := "access denied: invalid git command"
74
		return s.replyWithGitError(stderr, msg, errors.New(msg))
75
	}
76
77
	if err != nil {
78
		return err
79
	}
80
81
	return nil
82
}
83
84
func (s *Shell) AuthorizedKeys(executablePath string) string {
85
	var out strings.Builder
86
	for _, key := range s.cfg.SSH.Keys {
87
		fmt.Fprintf(&out, `command="%s shell",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty %s`+"\n",
88
			executablePath, key)
89
	}
90
	return out.String()
91
}
92
93
func (s *Shell) parseCommand(cmd string) (gitCmd, repoName string, err error) {
94
	cmdParts := strings.Fields(cmd)
95
	if len(cmdParts) < 2 {
96
		return "", "", fmt.Errorf("invalid command: expected 'git-cmd repo', got %q", cmd)
97
	}
98
99
	gitCmd = cmdParts[0]
100
	repoName = strings.Trim(cmdParts[1], "'\"")
101
	if repoName == "" {
102
		return "", "", fmt.Errorf("invalid command: empty repository name")
103
	}
104
105
	return gitCmd, repoName, nil
106
}
107
108
func (s *Shell) replyWithGitError(stderr io.Writer, msg string, cause error) error {
109
	if _, err := fmt.Fprintf(stderr, "fatal: %s\n", msg); err != nil {
110
		return err
111
	}
112
113
	return cause
114
}