Files
Claude 80d0fda167 Split the code into packages: cmd/modemmenu, internal/*, authd
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
2026-11-06 03:12:02 +00:00

98 lines
2.8 KiB
Go

// Copyright (C) 2026 D-Modem Authors
// SPDX-License-Identifier: AGPL-3.0-only
//go:build pam
package auth
import (
"errors"
"fmt"
"strings"
"github.com/msteinert/pam/v2"
)
const havePAM = true
// pamAuthenticate: the PAM service's auth and account stacks for the
// user, the password the answer to its hidden prompts
func pamAuthenticate(service, user, password string) error {
t, err := pam.StartFunc(service, user, func(s pam.Style, msg string) (string, error) {
switch s {
case pam.PromptEchoOff:
return password, nil
case pam.PromptEchoOn:
return user, nil
case pam.ErrorMsg, pam.TextInfo:
return "", nil
}
return "", fmt.Errorf("unsupported PAM message style %d", s)
})
if err != nil {
return fmt.Errorf("pam %s: %v", service, err)
}
defer t.End()
t.SetItem(pam.Tty, "modem")
if err := t.Authenticate(pam.DisallowNullAuthtok); err != nil {
if errors.Is(err, pam.ErrUserUnknown) {
return ErrNoUser
}
if errors.Is(err, pam.ErrAuth) || errors.Is(err, pam.ErrPermDenied) ||
errors.Is(err, pam.ErrMaxtries) || errors.Is(err, pam.ErrCredInsufficient) {
return ErrBadPassword
}
return fmt.Errorf("pam %s: %v", service, err)
}
if err := t.AcctMgmt(pam.DisallowNullAuthtok); err != nil {
// expired, locked, outside its hours...: refused, and why logged
return fmt.Errorf("%w: pam account %s: %v", ErrBadPassword, user, err)
}
return nil
}
// pamChangePassword: the PAM service's password stack for the user. Its
// hidden prompts get the current password (asked for "current" or "old")
// or the new one; what it has to say goes to the caller (a quality
// module's reasons).
func pamChangePassword(service, user, old, new string, line Messenger) error {
t, err := pam.StartFunc(service, user, func(s pam.Style, msg string) (string, error) {
switch s {
case pam.PromptEchoOff:
m := strings.ToLower(msg)
if strings.Contains(m, "current") || strings.Contains(m, "old") {
return old, nil
}
return new, nil
case pam.PromptEchoOn:
return user, nil
case pam.ErrorMsg, pam.TextInfo:
if line != nil && msg != "" {
line.Write(pamCRLF(strings.TrimRight(msg, "\n")) + "\r\n")
}
return "", nil
}
return "", fmt.Errorf("unsupported PAM message style %d", s)
})
if err != nil {
return fmt.Errorf("pam %s: %v", service, err)
}
defer t.End()
t.SetItem(pam.Tty, "modem")
if err := t.ChangeAuthTok(0); err != nil {
if errors.Is(err, pam.ErrAuthtok) || errors.Is(err, pam.ErrPermDenied) ||
errors.Is(err, pam.ErrAuthtokRecovery) || errors.Is(err, pam.ErrAuth) {
return fmt.Errorf("%w: pam %s: %v", ErrRefused, service, err)
}
return fmt.Errorf("pam %s: %v", service, err)
}
return nil
}
// pamCRLF: a PAM module's message with its line ends as the terminal
// wants them
func pamCRLF(s string) string {
s = strings.ReplaceAll(s, "\r\n", "\n")
return strings.ReplaceAll(s, "\n", "\r\n")
}