Files
login/authd/client.go
T
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

57 lines
1.4 KiB
Go

// Copyright (C) 2026 D-Modem Authors
// SPDX-License-Identifier: AGPL-3.0-only
package authd
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"net"
"time"
)
// Client asks the socket at Socket.
type Client struct {
Socket string
Timeout time.Duration // for an answer (modemmenu waits fail_delay before a no); 0: 15s
}
// Check asks whether pw is user's password: true and the backend that
// said so, false if it is not; an error if modemmenu could not be asked
// or would not answer (refused: not a client it answers).
func (c *Client) Check(user, pw string) (bool, string, error) {
timeout := c.Timeout
if timeout <= 0 {
timeout = 15 * time.Second
}
conn, err := net.DialTimeout("unix", c.Socket, timeout)
if err != nil {
return false, "", err
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(timeout))
b, _ := json.Marshal(Request{User: user, Password: pw})
if _, err := conn.Write(append(b, '\n')); err != nil {
return false, "", err
}
line, err := bufio.NewReader(conn).ReadSlice('\n')
if err != nil {
return false, "", fmt.Errorf("no answer: %w", err)
}
var r Reply
if err := json.Unmarshal(line, &r); err != nil {
return false, "", fmt.Errorf("an answer not understood: %q", line)
}
switch {
case r.OK:
return true, r.Auth, nil
case r.Error == "denied":
return false, "", nil
case r.Error != "":
return false, "", errors.New(r.Error)
}
return false, "", fmt.Errorf("an answer not understood: %q", line)
}