Files
login/auth_file.go
T
Claude 98755b757b Who sees what, time limits, guests and signup, caller ID rules, comments
Items take users, groups ([groups], or the system's for auth = system),
min_baud, callerids and guest; a caller neither sees nor can choose an
item they may not use.

time_limit limits a call and daily_limit a user's calls in a day (kept
in state_dir/usage under a lock); a selection is hung up at the limit,
and {time_left} / MODEM_TIME_LEFT tell what is left.

[guest] logs in by name without a password, to the guest = yes items
for its own time_limit.  [signup] adds new accounts to the users file
with a bcrypt hash, locked (user:!hash) until the sysop unlocks them
unless pending = no; the file backend refuses locked accounts.

[callerid] allow, deny and anonymous decide which calls the line takes;
[number PATTERN] sections deny a number, log it in as a user, or start
an item at once.

Comments may follow a value after a space; \; and \# escape them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
2026-10-12 11:37:10 +00:00

71 lines
1.6 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"os"
"strings"
)
// fileAuth: a text file of user:password lines (the password hashed as
// checkPassword reads it, ! before it for a locked account; # comments),
// read at each login so that edits take effect at once
type fileAuth struct {
path string
scheme string
}
func newFileAuth(s *section) (authenticator, error) {
a := &fileAuth{
path: s.get("path", "/etc/asterisk/modemmenu.users"),
scheme: strings.ToLower(s.get("hash", "auto")),
}
if _, err := os.Stat(a.path); err != nil {
return nil, fmt.Errorf("[file] path: %v", err)
}
return a, nil
}
func (a *fileAuth) name() string { return "file" }
func (a *fileAuth) authenticate(_ context.Context, user, password string) error {
f, err := os.Open(a.path)
if err != nil {
return err
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
line := strings.TrimSpace(sc.Text())
if line == "" || line[0] == '#' {
continue
}
name, stored, ok := strings.Cut(line, ":")
if !ok || name != user {
continue
}
// a locked account (signup's pending ones): !hash
if strings.HasPrefix(stored, "!") {
burn(password)
return fmt.Errorf("%w: %s is locked", errBadPassword, user)
}
// user:hash[:anything else, e.g. a full name]
if a.scheme != "plain" && !strings.HasPrefix(stored, "{") {
stored, _, _ = strings.Cut(stored, ":")
}
ok, err := checkPassword(stored, password, a.scheme)
if err != nil {
return fmt.Errorf("%s: user %s: %v", a.path, user, err)
}
if !ok {
return errBadPassword
}
return nil
}
if err := sc.Err(); err != nil {
return err
}
return errNoUser
}