Home

ugit @dcc55ceb0417ba524c49801b3a8d8460bbf28e18 - refs - log -
-
https://git.jolheiser.com/ugit.git
The code powering this h*ckin' site
ugit / internal / ssh / wish.go
- raw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package ssh

import (
	"context"
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"text/tabwriter"

	"go.jolheiser.com/ugit/internal/git"

	"github.com/charmbracelet/log"
	"github.com/charmbracelet/ssh"
	"github.com/charmbracelet/wish"
)

// ErrSystemMalfunction represents a general system error returned to clients.
var ErrSystemMalfunction = errors.New("something went wrong")

// ErrInvalidRepo represents an attempt to access a non-existent repo.
var ErrInvalidRepo = errors.New("invalid repo")

// Hooks is an interface that allows for custom authorization
// implementations and post push/fetch notifications. Prior to git access,
// AuthRepo will be called with the ssh.Session public key and the repo name.
// Implementers return the appropriate AccessLevel.
type Hooks interface {
	Push(string, ssh.PublicKey)
	Fetch(string, ssh.PublicKey)
}

// Session wraps sn ssh.Session to implement git.ReadWriteContexter
type Session struct {
	s ssh.Session
}

// Read implements io.Reader
func (s Session) Read(p []byte) (n int, err error) {
	return s.s.Read(p)
}

// Write implements io.Writer
func (s Session) Write(p []byte) (n int, err error) {
	return s.s.Write(p)
}

// Close implements io.Closer
func (s Session) Close() error {
	return nil
}

// Context returns an interface context.Context
func (s Session) Context() context.Context {
	return s.s.Context()
}

// Middleware adds Git server functionality to the ssh.Server. Repos are stored
// in the specified repo directory. The provided Hooks implementation will be
// checked for access on a per repo basis for a ssh.Session public key.
// Hooks.Push and Hooks.Fetch will be called on successful completion of
// their commands.
func Middleware(repoDir string, cloneURL string, port int, gh Hooks) wish.Middleware {
	return func(sh ssh.Handler) ssh.Handler {
		return func(s ssh.Session) {
			sess := Session{s: s}
			cmd := s.Command()

			// Git operations
			if len(cmd) == 2 {
				gc := cmd[0]
				// repo should be in the form of "repo.git" or "user/repo.git"
				repo := strings.TrimSuffix(strings.TrimPrefix(cmd[1], "/"), "/")
				repo = filepath.Clean(repo)
				if n := strings.Count(repo, "/"); n > 1 {
					Fatal(s, ErrInvalidRepo)
					return
				}
				pk := s.PublicKey()
				switch gc {
				case "git-receive-pack":
					if err := gitPack(sess, gc, repoDir, repo); err != nil {
						Fatal(s, ErrSystemMalfunction)
					}
					gh.Push(repo, pk)
					return
				case "git-upload-archive", "git-upload-pack":
					if err := gitPack(sess, gc, repoDir, repo); err != nil {
						if errors.Is(err, ErrInvalidRepo) {
							Fatal(s, ErrInvalidRepo)
						}
						log.Error("unknown git error", "error", err)
						Fatal(s, ErrSystemMalfunction)
					}
					gh.Fetch(repo, pk)
					return
				}
			}

			// Repo list
			if len(cmd) == 0 {
				des, err := os.ReadDir(repoDir)
				if err != nil && err != fs.ErrNotExist {
					log.Error("invalid repository", "error", err)
				}
				tw := tabwriter.NewWriter(s, 0, 0, 1, ' ', 0)
				for _, de := range des {
					if filepath.Ext(de.Name()) != ".git" {
						continue
					}
					repo, err := git.NewRepo(repoDir, de.Name())
					visibility := "❓"
					if err == nil {
						visibility = "🔓"
						if repo.Meta.Private {
							visibility = "🔒"
						}
					}
					fmt.Fprintf(tw, "%[1]s\t%[3]s\t%[2]s/%[1]s.git\n", strings.TrimSuffix(de.Name(), ".git"), cloneURL, visibility)
				}
				tw.Flush()
			}
			sh(s)
		}
	}
}

func gitPack(s Session, gitCmd string, repoDir string, repoName string) error {
	rp := filepath.Join(repoDir, repoName)
	protocol, err := git.NewProtocol(rp)
	if err != nil {
		return err
	}
	switch gitCmd {
	case "git-upload-pack":
		exists, err := git.PathExists(rp)
		if !exists {
			return ErrInvalidRepo
		}
		if err != nil {
			return err
		}
		return protocol.SSHUploadPack(s)
	case "git-receive-pack":
		err := git.EnsureRepo(repoDir, repoName)
		if err != nil {
			return err
		}
		repo, err := git.NewRepo(repoDir, repoName)
		if err != nil {
			return err
		}
		err = protocol.SSHReceivePack(s, repo)
		if err != nil {
			return err
		}
		_, err = repo.DefaultBranch()
		if err != nil {
			return err
		}
		// Needed for git dumb http server
		return git.UpdateServerInfo(rp)
	default:
		return fmt.Errorf("unknown git command: %s", gitCmd)
	}
}

// Fatal prints to the session's STDOUT as a git response and exit 1.
func Fatal(s ssh.Session, v ...interface{}) {
	msg := fmt.Sprint(v...)
	// hex length includes 4 byte length prefix and ending newline
	pktLine := fmt.Sprintf("%04x%s\n", len(msg)+5, msg)
	_, _ = wish.WriteString(s, pktLine)
	s.Exit(1) // nolint: errcheck
}