mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
All of modemmenu was one package main in the repository's root. It is
now one module with the program and the pieces it is made of apart:
cmd/modemmenu/ the program (the login, the menu, access, limits,
the selections, -serve, AutoPPP, drop files, the
auth socket's server)
internal/config/ modemmenu.conf's sections and keys, {name} templates
internal/term/ the caller's line, terminal detection, PETSCII
internal/auth/ the backends and password changes
internal/telnet/ the telnet client of mode = telnet items
authd/ the auth socket's protocol and a client, for a BBS
to import (bbs's internal/menuauth is a copy of it)
samples/ the .conf, .users, .pam and .service samples
make builds ./cmd/modemmenu and installs the samples from samples/; the
docs, confref and the site's reference follow. The tests are where
their code is, none lost. TestSampleHasEveryKey now reads the keys of
internal/ too (and the exported Get, Bool...: it would have found none).
.gitignore's "modemmenu" ignored cmd/modemmenu/ as well: it is
/modemmenu now.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRbjf74j8oq9R3PXhA
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/D-Modem/menu/authd"
|
|
)
|
|
|
|
// authSettings: settings with an auth socket in a test directory, and
|
|
// the socket's path
|
|
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
|
|
}
|
|
|
|
// ask: one request on the auth socket, its answer
|
|
func ask(t *testing.T, sock, body string) authd.Reply {
|
|
t.Helper()
|
|
c, err := net.Dial("unix", sock)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer c.Close()
|
|
c.Write([]byte(body + "\n"))
|
|
var r authd.Reply
|
|
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]authd.Reply{
|
|
`{"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)
|
|
}
|
|
}
|
|
|
|
// TestAuthdSettings: [authd] settings that are refused
|
|
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)
|
|
}
|
|
}
|
|
}
|