Files
Claude 9d5cdfa76e Signup: make the new account in each login backend
A signup made the account in the users file only. Now it makes the
account in each backend of [general] auth that is set up to make
accounts: the users file, SQL ([sql] insert_query), LDAP ([ldap] add_dn,
added as add_bind_dn, the password set by Password Modify or unicodePwd
for AD) and system accounts ([system] create_command, with a useradd
sample script). [signup] backends limits the list.

Before the account is made, each backend checks that the name is free.
If one backend fails, the accounts already made are taken back
(delete_query, entry delete, remove_command). With pending = yes, each
backend must lock the account, or dmodem-login does not start.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B82eRRfuh3h6uNuHtaugux
2026-11-24 16:56:35 +00:00

83 lines
2.1 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" }
// Scheme: hash, the scheme of the stored passwords (auto: by prefix)
func (a *File) Scheme() string { return a.scheme }
// 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
}