This commit is contained in:
mik2k2 2021-07-06 12:57:51 +02:00
parent efab623a6a
commit 253302a057
3 changed files with 26 additions and 26 deletions

12
auth.go
View File

@ -1,16 +1,16 @@
package sshchat
import (
"bufio"
"crypto/sha256"
"crypto/subtle"
"encoding/csv"
"errors"
"fmt"
"net"
"os"
"strings"
"time"
"os"
"bufio"
"github.com/shazow/ssh-chat/set"
"github.com/shazow/ssh-chat/sshd"
@ -59,8 +59,8 @@ type Auth struct {
banned *set.Set
whitelist *set.Set
ops *set.Set
opFile string
whitelistFile string
opFile string
whitelistFile string
}
// NewAuth creates a new empty Auth.
@ -169,7 +169,7 @@ func (a *Auth) IsOp(key ssh.PublicKey) bool {
// LoadOpsFromFile reads a file in authorized_keys format and makes public keys operators
func (a *Auth) LoadOpsFromFile(path string) error {
a.opFile = path
return fromFile(path, func(key ssh.PublicKey){a.Op(key, 0)})
return fromFile(path, func(key ssh.PublicKey) { a.Op(key, 0) })
}
// Whitelist will set a public key as a whitelisted user.
@ -194,7 +194,7 @@ func (a *Auth) Whitelist(key ssh.PublicKey, d time.Duration) {
// LoadWhitelistFromFile reads a file in authorized_keys format and whitelists public keys
func (a *Auth) LoadWhitelistFromFile(path string) error {
a.whitelistFile = path
return fromFile(path, func(key ssh.PublicKey){a.Whitelist(key, 0)})
return fromFile(path, func(key ssh.PublicKey) { a.Whitelist(key, 0) })
}
// Ban will set a public key as banned.

30
host.go
View File

@ -13,10 +13,10 @@ import (
"github.com/shazow/rateio"
"github.com/shazow/ssh-chat/chat"
"github.com/shazow/ssh-chat/set"
"github.com/shazow/ssh-chat/chat/message"
"github.com/shazow/ssh-chat/internal/humantime"
"github.com/shazow/ssh-chat/internal/sanitize"
"github.com/shazow/ssh-chat/set"
"github.com/shazow/ssh-chat/sshd"
)
@ -711,12 +711,12 @@ func (h *Host) InitCommands(c *chat.Commands) {
// TODO: "panic" (?) command for (import + on + reverify)?
// TODO: "print" command with a format for saving to the whitelist file?
// -> hard because the whitelist set only saves fingerprints
Op: true,
Prefix: "/whitelist",
Op: true,
Prefix: "/whitelist",
PrefixHelp: "COMMAND [ARGS...]",
Help: "Manipulate the whitelist or whitelist state. See /whitelist help for subcommands",
Help: "Manipulate the whitelist or whitelist state. See /whitelist help for subcommands",
Handler: func(room *chat.Room, msg message.CommandMsg) error {
if !room.IsOp(msg.From()){
if !room.IsOp(msg.From()) {
return errors.New("must be op")
}
@ -727,14 +727,14 @@ func (h *Host) InitCommands(c *chat.Commands) {
// send exactly one message to preserve order
replyLines := []string{}
sendMsg := func(content string, formatting ...interface{}){
sendMsg := func(content string, formatting ...interface{}) {
replyLines = append(replyLines, fmt.Sprintf(content, formatting...))
}
forConnectedUsers := func(cmd func(*chat.Member, ssh.PublicKey) error)error{
return h.Members.Each(func(key string, item set.Item) error{
forConnectedUsers := func(cmd func(*chat.Member, ssh.PublicKey) error) error {
return h.Members.Each(func(key string, item set.Item) error {
v := item.Value()
if v == nil { // expired between Each and here
if v == nil { // expired between Each and here
return nil
}
user := v.(*chat.Member)
@ -748,14 +748,14 @@ func (h *Host) InitCommands(c *chat.Commands) {
invalidKeys := []string{}
noKeyUsers := []string{}
var keyType string
for _, v := range args[1:]{
for _, v := range args[1:] {
switch {
case keyType != "":
pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(keyType + " " + v))
if err == nil {
cmd(pk)
} else {
invalidKeys = append(invalidKeys, keyType + " " + v)
invalidKeys = append(invalidKeys, keyType+" "+v)
}
keyType = ""
case strings.HasPrefix(v, "ssh-"):
@ -800,9 +800,9 @@ func (h *Host) InitCommands(c *chat.Commands) {
case "off":
h.auth.WhitelistMode = false
case "add":
forPubkeyUser(func(pk ssh.PublicKey){h.auth.Whitelist(pk, 0)})
forPubkeyUser(func(pk ssh.PublicKey) { h.auth.Whitelist(pk, 0) })
case "remove":
forPubkeyUser(func(pk ssh.PublicKey){h.auth.Whitelist(pk, 1)})
forPubkeyUser(func(pk ssh.PublicKey) { h.auth.Whitelist(pk, 1) })
case "import":
var since time.Duration
var err error
@ -843,7 +843,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
sendMsg("whitelist is disabled, so nobody will be kicked")
break
}
forConnectedUsers(func(user *chat.Member, pk ssh.PublicKey)error{
forConnectedUsers(func(user *chat.Member, pk ssh.PublicKey) error {
if !h.auth.IsOp(pk) && h.auth.CheckPublicKey(pk) != nil { // TODO: why doesn't CheckPublicKey do this?
user.Close() // TODO: some message anywhere?
}
@ -860,7 +860,7 @@ func (h *Host) InitCommands(c *chat.Commands) {
// TODO: this can probably be optimized
h.auth.whitelist.Each(func(key string, item set.Item) error {
keyFP := item.Key()
if forConnectedUsers(func (user *chat.Member, pk ssh.PublicKey) error {
if forConnectedUsers(func(user *chat.Member, pk ssh.PublicKey) error {
if pk != nil && sshd.Fingerprint(pk) == keyFP {
whitelistedUsers = append(whitelistedUsers, user.Name())
return errors.New("not an actual error, but exit early because we found the key")

View File

@ -49,7 +49,7 @@ func TestStripPrompt(t *testing.T) {
},
{
Input: "[foo] \x1b[6D\x1b[K-> From your friendly system.\r",
Want: "From your friendly system.\r",
Want: "From your friendly system.\r",
},
}
@ -201,12 +201,12 @@ func TestHostWhitelistCommand(t *testing.T) {
go host.Serve()
users := make(chan *message.User)
host.OnUserJoined = func(u *message.User){
host.OnUserJoined = func(u *message.User) {
users <- u
}
sshd.ConnectShell(s.Addr().String(), "foo", func(r io.Reader, w io.WriteCloser) error {
<- users
<-users
m, ok := host.MemberByID("foo")
if !ok {
t.Fatal("can't get member foo")
@ -216,7 +216,7 @@ func TestHostWhitelistCommand(t *testing.T) {
scanner.Scan() // Joined
// <- messages
assertLineEq := func(expected string){
assertLineEq := func(expected string) {
if !scanner.Scan() {
t.Error("no line available")
}
@ -224,7 +224,7 @@ func TestHostWhitelistCommand(t *testing.T) {
t.Errorf("expected %q, got %q", expected, actual)
}
}
sendCmd := func(cmd string, formatting ...interface{}){
sendCmd := func(cmd string, formatting ...interface{}) {
host.HandleMsg(message.ParseInput(fmt.Sprintf(cmd, formatting...), m.User))
}