2024-09-21 01:32:39 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-09-21 19:49:11 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2024-09-21 01:32:39 +02:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"path"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/db"
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
|
|
"git.defalsify.org/vise.git/logging"
|
|
|
|
"git.defalsify.org/vise.git/resource"
|
2024-09-21 19:49:11 +02:00
|
|
|
"git.defalsify.org/vise.git/state"
|
2024-09-21 01:32:39 +02:00
|
|
|
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/handlers"
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
wg sync.WaitGroup
|
|
|
|
keyStore db.Db
|
|
|
|
logg = logging.NewVanilla()
|
|
|
|
scriptDir = path.Join("services", "registration")
|
|
|
|
)
|
|
|
|
|
|
|
|
type auther struct {
|
|
|
|
Ctx context.Context
|
2024-09-21 19:49:11 +02:00
|
|
|
auth map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuther(ctx context.Context) *auther {
|
|
|
|
return &auther{
|
|
|
|
Ctx: ctx,
|
|
|
|
auth: make(map[string]string),
|
|
|
|
}
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func(a *auther) Check(conn ssh.ConnMetadata, pubKey ssh.PublicKey) (*ssh.Permissions, error) {
|
2024-09-21 19:49:11 +02:00
|
|
|
keyStore.SetLanguage(nil)
|
2024-09-21 01:32:39 +02:00
|
|
|
keyStore.SetPrefix(storage.DATATYPE_CUSTOM)
|
|
|
|
k := append([]byte{0x01}, pubKey.Marshal()...)
|
|
|
|
v, err := keyStore.Get(a.Ctx, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-21 19:49:11 +02:00
|
|
|
ka := hex.EncodeToString(conn.SessionID())
|
|
|
|
va := string(v)
|
|
|
|
a.auth[ka] = va
|
|
|
|
fmt.Fprintf(os.Stderr, "connect: %s -> %s\n", ka, v)
|
2024-09-21 01:32:39 +02:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
func(a *auther) FromConn(c *ssh.ServerConn) (string, error) {
|
|
|
|
if c == nil {
|
|
|
|
return "", errors.New("nil server conn")
|
|
|
|
}
|
|
|
|
if c.Conn == nil {
|
|
|
|
return "", errors.New("nil underlying conn")
|
|
|
|
}
|
|
|
|
return a.Get(c.Conn.SessionID())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func(a *auther) Get(k []byte) (string, error) {
|
|
|
|
ka := hex.EncodeToString(k)
|
|
|
|
v, ok := a.auth[ka]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.New("not found")
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
2024-09-21 19:49:11 +02:00
|
|
|
return v, nil
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-21 22:32:02 +02:00
|
|
|
//func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, mss *storage.MenuStorageService, lhs *handlers.LocalHandlerService) error {
|
|
|
|
func serve(ctx context.Context, sessionId string, ch ssh.NewChannel, en engine.Engine) error {
|
2024-09-21 01:32:39 +02:00
|
|
|
if ch == nil {
|
2024-09-21 19:49:11 +02:00
|
|
|
return errors.New("nil channel")
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
if ch.ChannelType() != "session" {
|
|
|
|
ch.Reject(ssh.UnknownChannelType, "that is not the channel you are looking for")
|
2024-09-21 19:49:11 +02:00
|
|
|
return errors.New("not a session")
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
channel, requests, err := ch.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer channel.Close()
|
|
|
|
wg.Add(1)
|
|
|
|
go func(reqIn <-chan *ssh.Request) {
|
|
|
|
defer wg.Done()
|
|
|
|
for req := range reqIn {
|
|
|
|
req.Reply(req.Type == "shell", nil)
|
|
|
|
}
|
|
|
|
_ = requests
|
|
|
|
}(requests)
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
cont, err := en.Exec(ctx, []byte{})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initial engine exec err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var input [state.INPUT_LIMIT]byte
|
|
|
|
for cont {
|
|
|
|
c, err := en.Flush(ctx, channel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("flush err: %v", err)
|
|
|
|
}
|
|
|
|
_, err = channel.Write([]byte{0x0a})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("newline err: %v", err)
|
|
|
|
}
|
|
|
|
c, err = channel.Read(input[:])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read input fail: %v", err)
|
|
|
|
}
|
|
|
|
logg.TraceCtxf(ctx, "input read", "c", c, "input", input[:c-1])
|
|
|
|
cont, err = en.Exec(ctx, input[:c-1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("engine exec err: %v", err)
|
|
|
|
}
|
|
|
|
logg.TraceCtxf(ctx, "exec cont", "cont", cont, "en", en)
|
|
|
|
_ = c
|
|
|
|
}
|
|
|
|
c, err := en.Flush(ctx, channel)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("last flush err: %v", err)
|
|
|
|
}
|
|
|
|
_ = c
|
|
|
|
return nil
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-21 22:32:02 +02:00
|
|
|
type sshRunner struct {
|
|
|
|
Ctx context.Context
|
|
|
|
Cfg engine.Config
|
|
|
|
FlagFile string
|
|
|
|
DbDir string
|
|
|
|
ResourceDir string
|
|
|
|
Debug bool
|
2024-09-22 03:57:10 +02:00
|
|
|
KeyFile string
|
|
|
|
Host string
|
|
|
|
Port uint
|
2024-09-21 22:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func(s *sshRunner) GetEngine(sessionId string) (engine.Engine, func(), error) {
|
|
|
|
ctx := s.Ctx
|
|
|
|
menuStorageService := storage.NewMenuStorageService(s.DbDir, s.ResourceDir)
|
|
|
|
|
|
|
|
err := menuStorageService.EnsureDbDir()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rs, err := menuStorageService.GetResource(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pe, err := menuStorageService.GetPersister(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
userdatastore, err := menuStorageService.GetUserdataDb(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
dbResource, ok := rs.(*resource.DbResource)
|
|
|
|
if !ok {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
lhs, err := handlers.NewLocalHandlerService(s.FlagFile, true, dbResource, s.Cfg, rs)
|
|
|
|
lhs.SetDataStore(&userdatastore)
|
|
|
|
lhs.SetPersister(pe)
|
|
|
|
lhs.Cfg.SessionId = sessionId
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hl, err := lhs.GetHandler()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
en := lhs.GetEngine()
|
|
|
|
en = en.WithFirst(hl.Init)
|
|
|
|
if s.Debug {
|
|
|
|
en = en.WithDebug(nil)
|
|
|
|
}
|
|
|
|
// TODO: this is getting very hacky!
|
|
|
|
closer := func() {
|
|
|
|
err := menuStorageService.Close()
|
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(ctx, "menu storage service cleanup fail", "err", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return en, closer, nil
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:56:20 +02:00
|
|
|
// adapted example from crypto/ssh package, NewServerConn doc
|
2024-09-22 03:57:10 +02:00
|
|
|
func(s *sshRunner) Run(ctx context.Context) {
|
2024-09-21 01:32:39 +02:00
|
|
|
running := true
|
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
// TODO: waitgroup should probably not be global
|
2024-09-21 01:32:39 +02:00
|
|
|
defer wg.Wait()
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
auth := NewAuther(ctx)
|
2024-09-21 01:32:39 +02:00
|
|
|
cfg := ssh.ServerConfig{
|
|
|
|
PublicKeyCallback: auth.Check,
|
|
|
|
}
|
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
privateBytes, err := os.ReadFile(s.KeyFile)
|
2024-09-21 01:32:39 +02:00
|
|
|
if err != nil {
|
2024-09-21 22:32:02 +02:00
|
|
|
logg.ErrorCtxf(ctx, "Failed to load private key", "err", err)
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
private, err := ssh.ParsePrivateKey(privateBytes)
|
|
|
|
if err != nil {
|
2024-09-21 22:32:02 +02:00
|
|
|
logg.ErrorCtxf(ctx, "Failed to parse private key", "err", err)
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
cfg.AddHostKey(private)
|
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
lst, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Host, s.Port))
|
2024-09-21 01:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for running {
|
|
|
|
conn, err := lst.Accept()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-09-21 19:49:11 +02:00
|
|
|
|
2024-09-21 01:32:39 +02:00
|
|
|
go func(conn net.Conn) {
|
|
|
|
defer conn.Close()
|
|
|
|
for true {
|
|
|
|
srvConn, nC, rC, err := ssh.NewServerConn(conn, &cfg)
|
|
|
|
if err != nil {
|
2024-09-21 19:49:11 +02:00
|
|
|
logg.InfoCtxf(ctx, "rejected client", "err", err)
|
|
|
|
return
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
2024-09-21 19:49:11 +02:00
|
|
|
logg.DebugCtxf(ctx, "ssh client connected", "conn", srvConn)
|
2024-09-21 01:32:39 +02:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
ssh.DiscardRequests(rC)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2024-09-21 19:49:11 +02:00
|
|
|
|
|
|
|
sessionId, err := auth.FromConn(srvConn)
|
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(ctx, "Cannot find authentication")
|
|
|
|
return
|
|
|
|
}
|
2024-09-21 22:32:02 +02:00
|
|
|
en, closer, err := s.GetEngine(sessionId)
|
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(ctx, "engine won't start", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err := en.Finish()
|
|
|
|
if err != nil {
|
|
|
|
logg.ErrorCtxf(ctx, "engine won't stop", "err", err)
|
|
|
|
}
|
|
|
|
closer()
|
|
|
|
}()
|
2024-09-21 01:32:39 +02:00
|
|
|
for ch := range nC {
|
2024-09-21 22:32:02 +02:00
|
|
|
err = serve(ctx, sessionId, ch, en)
|
2024-09-21 19:49:11 +02:00
|
|
|
logg.ErrorCtxf(ctx, "ssh server finish", "err", err)
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
// TODO: This is test code, move to external tool for adding and removing keys
|
2024-09-21 01:32:39 +02:00
|
|
|
func sshLoadKeys(ctx context.Context, dbDir string) error {
|
|
|
|
keyStoreFile := path.Join(dbDir, "ssh_authorized_keys.gdbm")
|
2024-09-22 03:21:56 +02:00
|
|
|
keyStore = storage.NewThreadGdbmDb()
|
|
|
|
err := keyStore.Connect(ctx, keyStoreFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-21 01:32:39 +02:00
|
|
|
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte("ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCu5rYCxMBsVAL1TEkMQgmElAYEZj5zYDdyHjUxZ6qzHBOZD9GAzdxx9GyQDx2vdYm3329tLH/69ky1YA3nUz8SnJGBD6hC5XrqwN6zo9R9oOHAKTwiPGhey2NTVmheP+9XNHukBnOlkkWOQlpDDvMbWOztaZOWDaA8OIeP0t6qzFqLyelyg65lxzM3BKd7bCmmfzl/64BcP1MotAmB9DUxmY0Wb4Q2hYZfNYBx50Z4xthTgKV+Xoo8CbTduKotIz6hluQGvWdtxlCJQEiZ2f4RYY87JSA6/BAH2fhxuLHMXRpzocJNqARqCWpdcTGSg7bzxbKvTFH9OU4wZtr9ie40OR4zsc1lOBZL0rnp8GLkG8ZmeBQrgEDlmR9TTlz4okgtL+c5TCS37rjZYVjmtGwihws0EL9+wyv2dSQibirklC4wK5eWHKXl5vab19qzw/qRLdoRBK40DxbRKggxA7gqSsKrmrf+z7CuLIz/kxF+169FBLbh1MfBOGdx1awm6aU= lash@furioso"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
k := append([]byte{0x01}, pubKey.Marshal()...)
|
|
|
|
keyStore.SetPrefix(storage.DATATYPE_CUSTOM)
|
|
|
|
return keyStore.Put(ctx, k, []byte("+25113243546"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var dbDir string
|
|
|
|
var resourceDir string
|
|
|
|
var size uint
|
|
|
|
var engineDebug bool
|
|
|
|
var stateDebug bool
|
|
|
|
var host string
|
|
|
|
var port uint
|
|
|
|
flag.StringVar(&dbDir, "dbdir", ".state", "database dir to read from")
|
|
|
|
flag.StringVar(&resourceDir, "resourcedir", path.Join("services", "registration"), "resource dir")
|
|
|
|
flag.BoolVar(&engineDebug, "engine-debug", false, "use engine debug output")
|
|
|
|
flag.BoolVar(&stateDebug, "state-debug", false, "use engine debug output")
|
|
|
|
flag.UintVar(&size, "s", 160, "max size of output")
|
|
|
|
flag.StringVar(&host, "h", "127.0.0.1", "http host")
|
2024-09-22 03:57:10 +02:00
|
|
|
flag.UintVar(&port, "p", 7122, "http port")
|
2024-09-21 01:32:39 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
sshKeyFile := flag.Arg(0)
|
|
|
|
_, err := os.Stat(sshKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "cannot open ssh server private key file: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
logg.Infof("start command", "dbdir", dbDir, "resourcedir", resourceDir, "outputsize", size, "keyfile", sshKeyFile, "host", host, "port", port)
|
2024-09-21 01:32:39 +02:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
pfp := path.Join(scriptDir, "pp.csv")
|
|
|
|
|
|
|
|
cfg := engine.Config{
|
|
|
|
Root: "root",
|
|
|
|
OutputSize: uint32(size),
|
|
|
|
FlagCount: uint32(16),
|
|
|
|
}
|
|
|
|
if stateDebug {
|
|
|
|
cfg.StateDebug = true
|
|
|
|
}
|
|
|
|
if engineDebug {
|
|
|
|
cfg.EngineDebug = true
|
|
|
|
}
|
2024-09-21 19:49:11 +02:00
|
|
|
|
2024-09-22 03:57:10 +02:00
|
|
|
err = sshLoadKeys(ctx, dbDir)
|
2024-09-21 01:32:39 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:32:02 +02:00
|
|
|
runner := &sshRunner{
|
|
|
|
Cfg: cfg,
|
|
|
|
Debug: engineDebug,
|
|
|
|
FlagFile: pfp,
|
|
|
|
DbDir: dbDir,
|
|
|
|
ResourceDir: resourceDir,
|
2024-09-22 03:57:10 +02:00
|
|
|
KeyFile: sshKeyFile,
|
|
|
|
Host: host,
|
|
|
|
Port: port,
|
2024-09-21 22:32:02 +02:00
|
|
|
}
|
|
|
|
runner.Run(ctx)
|
2024-09-21 01:32:39 +02:00
|
|
|
}
|