mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
From claude/beautiful-pasteur-oz81no (af30cbd), brought onto this
branch without the build output it had committed (pbx-modem, 15 MB, now
in .gitignore).
[authd] listen: a unix socket where a program on this machine (the BBS)
asks modemmenu's backends (file, ldap, sql, system) for a user's
password, so that telnet, SSH and web logins there take the same one.
One JSON line each way: {"user","password"} -> {"ok","auth"} or
{"ok": false, "error": "denied"}. The backends are asked as at the
login prompt, user_pattern kept, a refusal fail_delay late.
Who may ask: socket_mode (none for others: refused), socket_group, and
allow_users (the peer's uid, SO_PEERCRED); TCP is refused. -serve
answers it with its sessions; -authd alone
(modemmenu-authd.service.sample).
With this branch's features: settings carry both [authd] and the access
sections; a signup's locked account is refused on the socket too (now
tested), and a password changed from the menu holds there at once.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
109 lines
3.2 KiB
Go
109 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func authSettings(t *testing.T, extra string) (*settings, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
users := filepath.Join(dir, "users")
|
|
// pending: a signup the sysop has not unlocked
|
|
os.WriteFile(users, []byte("alice:{PLAIN}secret\npending:!{PLAIN}secret\n"), 0o600)
|
|
sock := filepath.Join(dir, "auth.sock")
|
|
p := filepath.Join(dir, "c.conf")
|
|
os.WriteFile(p, []byte(fmt.Sprintf("[general]\nauth = file\nfail_delay = 0\n[file]\npath = %s\n[authd]\nlisten = %s\nsocket_mode = 0600\n%s"+
|
|
"[item x]\nkey = X\ncommand = true\n", users, sock, extra)), 0o600)
|
|
s, err := loadSettings(p)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s, sock
|
|
}
|
|
|
|
func ask(t *testing.T, sock, body string) authReply {
|
|
t.Helper()
|
|
c, err := net.Dial("unix", sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
c.Write([]byte(body + "\n"))
|
|
var r authReply
|
|
if err := json.NewDecoder(c).Decode(&r); err != nil {
|
|
t.Fatalf("%s: %v", body, err)
|
|
}
|
|
return r
|
|
}
|
|
|
|
// TestAuthd: the auth socket answers by the backends, user_pattern kept.
|
|
func TestAuthd(t *testing.T) {
|
|
s, sock := authSettings(t, "")
|
|
ln, err := listenAuth(s.authd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
go serveAuth(s, ln)
|
|
if fi, err := os.Stat(sock); err != nil || fi.Mode().Perm() != 0o600 {
|
|
t.Fatalf("socket %v %v", fi, err)
|
|
}
|
|
for body, want := range map[string]authReply{
|
|
`{"user":"alice","password":"secret"}`: {OK: true, Auth: "file"},
|
|
`{"user":"alice","password":"wrong"}`: {Error: "denied"},
|
|
`{"user":"bob","password":"secret"}`: {Error: "denied"},
|
|
`{"user":"pending","password":"secret"}`: {Error: "denied"},
|
|
`{"user":"../etc","password":"secret"}`: {Error: "denied"},
|
|
`{"user":"","password":"secret"}`: {Error: "bad request"},
|
|
`not json`: {Error: "bad request"},
|
|
`{"user":"alice","password":"secret"`: {Error: "bad request"},
|
|
} {
|
|
if got := ask(t, sock, body); got != want {
|
|
t.Errorf("%s: %+v, want %+v", body, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestAuthdAllowUsers: a peer not among allow_users is refused before it
|
|
// is heard.
|
|
func TestAuthdAllowUsers(t *testing.T) {
|
|
if os.Getuid() == 65534 {
|
|
t.Skip("running as nobody")
|
|
}
|
|
s, sock := authSettings(t, "allow_users = nobody\n")
|
|
ln, err := listenAuth(s.authd)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer ln.Close()
|
|
go serveAuth(s, ln)
|
|
if got := ask(t, sock, `{"user":"alice","password":"secret"}`); got.OK || got.Error != "refused" {
|
|
t.Errorf("%+v", got)
|
|
}
|
|
}
|
|
|
|
func TestAuthdSettings(t *testing.T) {
|
|
for extra, want := range map[string]string{
|
|
"listen = tcp:127.0.0.1:9\n": "a unix socket only",
|
|
"socket_mode = 0666\n": "lets anyone ask",
|
|
"allow_users = no-such-user\n": "allow_users",
|
|
"max_clients = 0\n": "max_clients",
|
|
} {
|
|
dir := t.TempDir()
|
|
users := filepath.Join(dir, "users")
|
|
os.WriteFile(users, nil, 0o600)
|
|
p := filepath.Join(dir, "c.conf")
|
|
os.WriteFile(p, []byte("[general]\nauth = file\n[file]\npath = "+users+"\n[authd]\nlisten = /run/x.sock\n"+extra+
|
|
"[item x]\nkey = X\ncommand = true\n"), 0o600)
|
|
if _, err := loadSettings(p); err == nil || !strings.Contains(err.Error(), want) {
|
|
t.Errorf("%q: %v", extra, err)
|
|
}
|
|
}
|
|
}
|