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
|
// Package accesscontrol provides a middleware that allows you to restrict the commands the user can execute.
package accesscontrol
import (
"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
)
// Middleware will exit 1 connections trying to execute commands that are not allowed.
// If no allowed commands are provided, no commands will be allowed.
func Middleware(cmds ...string) wish.Middleware {
return func(sh ssh.Handler) ssh.Handler {
return func(s ssh.Session) {
if len(s.Command()) == 0 {
sh(s)
return
}
for _, cmd := range cmds {
if s.Command()[0] == cmd {
sh(s)
return
}
}
s.Exit(1)
}
}
}
|