mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
auth = system is now PAM alone (pam_service): the /etc/shadow reader and its yescrypt and crypt dependencies are gone. The uid, root and group rules stay, by the name service. motd and motd_file (read at each login) show after the welcome, before the menu. The menu reads a line: an item's key (now one or more characters), its name or an alias selects it, in any case (p, ppp, dialup); ? shows the menu again, an unknown choice says so. [menu] hangup takes several words (G goodbye bye logoff); hotkeys = yes keeps one-key selection for one-character keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// systemAuth: the accounts of the system, by PAM: the auth and account
|
|
// stacks of pam_service, in a build with -tags pam. Only accounts with a
|
|
// uid of at least min_uid (not root, unless allow_root), in one of groups
|
|
// if it is set, may log in.
|
|
type systemAuth struct {
|
|
service string
|
|
minUID int
|
|
allowRoot bool
|
|
groups []string
|
|
// the name service and PAM, replaced in tests
|
|
lookup func(string) (*account, error)
|
|
inGroups func(*account, []string) (bool, error)
|
|
pam func(service, user, password string) error
|
|
}
|
|
|
|
// account: a user of the name service
|
|
type account struct {
|
|
name string
|
|
uid, gid int
|
|
}
|
|
|
|
func newSystemAuth(s *section) (authenticator, error) {
|
|
var err error
|
|
a := &systemAuth{
|
|
service: s.get("pam_service", "modemmenu"),
|
|
groups: fields(s.get("groups", "")),
|
|
lookup: lookupNSS,
|
|
inGroups: inGroupsNSS,
|
|
pam: pamAuthenticate,
|
|
}
|
|
if a.minUID, err = s.int("min_uid", 1000); err != nil {
|
|
return nil, err
|
|
}
|
|
if a.allowRoot, err = s.bool("allow_root", false); err != nil {
|
|
return nil, err
|
|
}
|
|
if !havePAM {
|
|
return nil, fmt.Errorf("[general] auth = system: this modemmenu is built without PAM (make with libpam's headers, or go build -tags pam)")
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
func (a *systemAuth) name() string { return "system" }
|
|
|
|
func (a *systemAuth) authenticate(_ context.Context, user, password string) error {
|
|
acct, err := a.lookup(user)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := a.allowed(acct); err != nil {
|
|
return err
|
|
}
|
|
return a.pam(a.service, user, password)
|
|
}
|
|
|
|
// allowed: an account that may dial in. The others are not this
|
|
// backend's users: the next one is asked
|
|
func (a *systemAuth) allowed(acct *account) error {
|
|
if acct.uid == 0 && !a.allowRoot {
|
|
return fmt.Errorf("%w: %s is root", errNoUser, acct.name)
|
|
}
|
|
if acct.uid != 0 && acct.uid < a.minUID {
|
|
return fmt.Errorf("%w: %s has uid %d < min_uid %d", errNoUser, acct.name, acct.uid, a.minUID)
|
|
}
|
|
if len(a.groups) == 0 {
|
|
return nil
|
|
}
|
|
in, err := a.inGroups(acct, a.groups)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !in {
|
|
return fmt.Errorf("%w: %s is in none of %s", errNoUser, acct.name, strings.Join(a.groups, ", "))
|
|
}
|
|
return nil
|
|
}
|