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
210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"os/user"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/D-Modem/menu/authd"
|
|
"github.com/D-Modem/menu/internal/auth"
|
|
"github.com/D-Modem/menu/internal/config"
|
|
"golang.org/x/sys/unix"
|
|
)
|
|
|
|
// The auth socket: modemmenu's backends (file, ldap, sql, system) asked by
|
|
// a program on this machine for a user's password, so that the BBS's
|
|
// other logins (telnet, SSH, the web) take the same one. One request a
|
|
// connection, a JSON line each way:
|
|
//
|
|
// {"user": "alice", "password": "..."}
|
|
// {"ok": true, "auth": "ldap"} or {"ok": false, "error": "denied"}
|
|
//
|
|
// The socket's mode and group say who may ask; allow_users narrows it to
|
|
// those users (by the peer's uid). A refusal comes after fail_delay, as at
|
|
// the login prompt; user_pattern is kept.
|
|
|
|
// authdSettings: [authd]
|
|
type authdSettings struct {
|
|
listen string
|
|
socketMode os.FileMode
|
|
socketGroup string
|
|
allowUIDs map[uint32]bool // nil: whoever can connect
|
|
maxClients int
|
|
}
|
|
|
|
// loadAuthd: [authd], the auth socket; nil when it has no listen
|
|
func loadAuthd(sec *config.Section) (*authdSettings, error) {
|
|
if sec.Get("listen", "") == "" {
|
|
return nil, nil
|
|
}
|
|
a := &authdSettings{listen: sec.Get("listen", ""), socketGroup: sec.Get("socket_group", "")}
|
|
if !strings.HasPrefix(a.listen, "/") {
|
|
return nil, fmt.Errorf("[authd] listen: %q (an absolute path: a unix socket only, passwords cross it)", a.listen)
|
|
}
|
|
mode, err := strconv.ParseUint(sec.Get("socket_mode", "0660"), 8, 32)
|
|
if err != nil || mode > 0o777 {
|
|
return nil, fmt.Errorf("[authd] socket_mode: %q (octal, e.g. 0660)", sec.Get("socket_mode", ""))
|
|
}
|
|
if mode&0o007 != 0 {
|
|
return nil, fmt.Errorf("[authd] socket_mode: %04o lets anyone ask for passwords", mode)
|
|
}
|
|
a.socketMode = os.FileMode(mode)
|
|
if names := config.Fields(sec.Get("allow_users", "")); len(names) > 0 {
|
|
a.allowUIDs = map[uint32]bool{}
|
|
for _, n := range names {
|
|
u, err := user.Lookup(n)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("[authd] allow_users: %s: %v", n, err)
|
|
}
|
|
uid, _ := strconv.ParseUint(u.Uid, 10, 32)
|
|
a.allowUIDs[uint32(uid)] = true
|
|
}
|
|
}
|
|
if a.maxClients, err = sec.Int("max_clients", 16); err != nil {
|
|
return nil, err
|
|
}
|
|
if a.maxClients < 1 {
|
|
return nil, fmt.Errorf("[authd] max_clients: %d", a.maxClients)
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// runAuthd: modemmenu -authd, the auth socket alone, until SIGTERM.
|
|
func runAuthd(s *settings) int {
|
|
if s.authd == nil {
|
|
fmt.Fprintln(os.Stderr, "modemmenu: -authd: no [authd] listen")
|
|
return 2
|
|
}
|
|
ln, err := listenAuth(s.authd)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "modemmenu:", err)
|
|
return 1
|
|
}
|
|
ch := make(chan os.Signal, 1)
|
|
signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT, syscall.SIGHUP)
|
|
go func() {
|
|
sig := <-ch
|
|
logf("%v: stopping", sig)
|
|
ln.Close()
|
|
}()
|
|
serveAuth(s, ln)
|
|
return 0
|
|
}
|
|
|
|
// listenAuth: the auth socket, listening
|
|
func listenAuth(a *authdSettings) (net.Listener, error) {
|
|
ln, err := listen(&serverSettings{listen: a.listen, socketMode: a.socketMode, socketGroup: a.socketGroup})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("[authd] %v", err)
|
|
}
|
|
logf("auth socket on %s", a.listen)
|
|
return ln, nil
|
|
}
|
|
|
|
// serveAuth answers on ln until it is closed.
|
|
func serveAuth(s *settings, ln net.Listener) {
|
|
slots := make(chan struct{}, s.authd.maxClients)
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
if errors.Is(err, net.ErrClosed) {
|
|
return
|
|
}
|
|
logf("auth accept: %v", err)
|
|
time.Sleep(100 * time.Millisecond)
|
|
continue
|
|
}
|
|
select {
|
|
case slots <- struct{}{}:
|
|
default:
|
|
logf("auth: %d requests at once already, refused", s.authd.maxClients)
|
|
conn.Close()
|
|
continue
|
|
}
|
|
go func() {
|
|
defer func() { <-slots }()
|
|
answerAuth(s, conn)
|
|
}()
|
|
}
|
|
}
|
|
|
|
// peerUID is the uid of a unix socket's peer.
|
|
func peerUID(conn net.Conn) (uint32, bool) {
|
|
uc, ok := conn.(*net.UnixConn)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
raw, err := uc.SyscallConn()
|
|
if err != nil {
|
|
return 0, false
|
|
}
|
|
var cred *unix.Ucred
|
|
raw.Control(func(fd uintptr) {
|
|
cred, _ = unix.GetsockoptUcred(int(fd), unix.SOL_SOCKET, unix.SO_PEERCRED)
|
|
})
|
|
if cred == nil {
|
|
return 0, false
|
|
}
|
|
return cred.Uid, true
|
|
}
|
|
|
|
// answerAuth: one request on the auth socket: who asks (allow_users),
|
|
// the credentials checked by the backends, the answer as JSON
|
|
func answerAuth(s *settings, conn net.Conn) {
|
|
defer conn.Close()
|
|
who := peer(conn)
|
|
reply := func(r authd.Reply) {
|
|
b, _ := json.Marshal(r)
|
|
conn.Write(append(b, '\n'))
|
|
}
|
|
if s.authd.allowUIDs != nil {
|
|
if uid, ok := peerUID(conn); !ok || !s.authd.allowUIDs[uid] {
|
|
logf("auth request from %s: not an allowed user", who)
|
|
reply(authd.Reply{Error: "refused"})
|
|
return
|
|
}
|
|
}
|
|
// one line, soon: a client that says less is not waited for
|
|
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
|
|
line, err := bufio.NewReaderSize(io.LimitReader(conn, authd.MaxRequest), authd.MaxRequest).ReadSlice('\n')
|
|
conn.SetDeadline(time.Now().Add(time.Minute))
|
|
var req authd.Request
|
|
if err != nil || json.Unmarshal(line, &req) != nil || req.User == "" {
|
|
logf("auth request from %s: bad request", who)
|
|
reply(authd.Reply{Error: "bad request"})
|
|
return
|
|
}
|
|
var via string
|
|
if s.userPattern.MatchString(req.User) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
via, err = auth.Authenticate(ctx, s.backends, req.User, req.Password, logf)
|
|
cancel()
|
|
} else {
|
|
// not a name we take: the same answer, after the same time
|
|
auth.Burn(req.Password)
|
|
err = auth.ErrNoUser
|
|
}
|
|
if err != nil {
|
|
logf("auth request from %s: user=%q denied (%v)", who, req.User, err)
|
|
time.Sleep(s.failDelay)
|
|
reply(authd.Reply{Error: "denied"})
|
|
return
|
|
}
|
|
logf("auth request from %s: user=%s auth=%s", who, req.User, via)
|
|
reply(authd.Reply{OK: true, Auth: via})
|
|
}
|