mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
dmodem-login no longer reads modemmenu's default paths (the configuration, the users file, the state directory and the PAM service), and the pppd plugin no longer accepts modemmenu-socket. docs/rename.md now says what to rename on an old installation. The bbs CI job no longer rewrites the old module path, because bbs now uses github.com/D-Modem/login. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B82eRTD8BSptdpaUWGcs65
80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
// Copyright (C) 2026 D-Modem Authors
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
package auth
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/D-Modem/login/internal/config"
|
|
)
|
|
|
|
// File: 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 File struct {
|
|
path string
|
|
scheme string
|
|
}
|
|
|
|
// NewFile: the [file] backend, its users file checked to exist
|
|
func NewFile(s *config.Section) (Authenticator, error) {
|
|
a := &File{
|
|
path: s.Get("path", "/etc/asterisk/dmodem-login.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
|
|
}
|
|
|
|
// Name: the backend's Name, for {auth} and the log
|
|
func (a *File) Name() string { return "file" }
|
|
|
|
// Authenticate: the user's line in the users file and its hash
|
|
// checked; a locked entry (!) is refused
|
|
func (a *File) 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
|
|
}
|