Files
login/cmd/modemmenu/limits.go
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

203 lines
4.9 KiB
Go

// Copyright (C) 2026 D-Modem Authors
// SPDX-License-Identifier: AGPL-3.0-only
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"golang.org/x/sys/unix"
)
// errTimeLimit: the session's time is up
var errTimeLimit = errors.New("time limit")
// A user's use of the day is kept in state_dir/usage/USER: the date and
// the seconds used, "2026-10-12 1830". Concurrent calls of a user (or
// sessions of modemmenu -serve) add to it under a lock.
// usagePath: the file of a user's usage today in state_dir
func usagePath(dir, user string) string {
return filepath.Join(dir, "usage", url.PathEscape(user))
}
// usedToday: what user has used of day (YYYY-MM-DD)
func usedToday(dir, user, day string) (time.Duration, error) {
f, err := os.Open(usagePath(dir, user))
if errors.Is(err, os.ErrNotExist) {
return 0, nil
}
if err != nil {
return 0, err
}
defer f.Close()
unix.Flock(int(f.Fd()), unix.LOCK_SH)
b := make([]byte, 64)
n, _ := f.Read(b)
return parseUsage(string(b[:n]), day), nil
}
// parseUsage: the usage a file records for day ("YYYY-MM-DD seconds"),
// 0 for another day
func parseUsage(s, day string) time.Duration {
d, secs, ok := strings.Cut(strings.TrimSpace(s), " ")
if !ok || d != day {
return 0
}
n, err := strconv.ParseInt(secs, 10, 64)
if err != nil || n < 0 {
return 0
}
return time.Duration(n) * time.Second
}
// addUsage: used more of day for user
func addUsage(dir, user, day string, used time.Duration) error {
p := usagePath(dir, user)
if err := os.MkdirAll(filepath.Dir(p), 0o700); err != nil {
return err
}
f, err := os.OpenFile(p, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return err
}
defer f.Close()
if err := unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil {
return err
}
b := make([]byte, 64)
n, _ := f.ReadAt(b, 0)
total := parseUsage(string(b[:n]), day) + used
line := fmt.Sprintf("%s %d\n", day, int64(total/time.Second))
if err := f.Truncate(0); err != nil {
return err
}
_, err = f.WriteAt([]byte(line), 0)
return err
}
// limiter: the time limits of a logged-in session
type limiter struct {
s *settings
sess *session
start time.Time
day string
deadline time.Time // zero: none
record sync.Once
}
// newLimiter at the login: the session's deadline, from time_limit (the
// guest's for a guest) and what is left of daily_limit; errTimeLimit when
// nothing is left today
func newLimiter(s *settings, sess *session, now time.Time) (*limiter, error) {
l := &limiter{s: s, sess: sess, start: now, day: now.Format("2006-01-02")}
limit := s.limits.timeLimit
if sess.Auth == "guest" {
limit = s.guest.timeLimit
}
if limit > 0 {
l.deadline = now.Add(limit)
}
// an upstream menu's limit (its session header) holds here too
if u := sess.upstreamDeadline; !u.IsZero() && (l.deadline.IsZero() || u.Before(l.deadline)) {
l.deadline = u
}
if l.daily() {
used, err := usedToday(s.limits.stateDir, sess.User, l.day)
if err != nil {
// no record: no limit is better than no login
logf("daily_limit: %v", err)
return l, nil
}
left := s.limits.dailyLimit - used
if left <= 0 {
return l, errTimeLimit
}
if d := now.Add(left); l.deadline.IsZero() || d.Before(l.deadline) {
l.deadline = d
}
}
return l, nil
}
// daily: whether the session counts towards a daily limit (guests share
// one name: they have time_limit only)
func (l *limiter) daily() bool {
return l.s.limits.dailyLimit > 0 && l.sess.Auth != "guest"
}
// left: the time left, and whether there is a limit at all
func (l *limiter) left(now time.Time) (time.Duration, bool) {
if l.deadline.IsZero() {
return 0, false
}
return l.deadline.Sub(now), true
}
// timeLeft for {time_left}: whole minutes, "" without a limit
func (l *limiter) timeLeft(now time.Time) string {
left, ok := l.left(now)
if !ok {
return ""
}
if left < 0 {
left = 0
}
return strconv.Itoa(int(left / time.Minute))
}
// mark: the session's {time_left} and {seconds_left} as of now
func (l *limiter) mark(sess *session, now time.Time) {
sess.TimeLeft, sess.SecondsLeft = l.timeLeft(now), ""
if left, ok := l.left(now); ok {
if left < 0 {
left = 0
}
sess.SecondsLeft = strconv.Itoa(int(left / time.Second))
}
}
// done: the session's time into the day's use, once (the session's end,
// or the hangup signal, whichever comes first)
func (l *limiter) done() {
l.record.Do(func() {
if !l.daily() {
return
}
if err := addUsage(l.s.limits.stateDir, l.sess.User, l.day, time.Since(l.start)); err != nil {
logf("daily_limit: %v", err)
}
})
}
// atExit: what the hangup signal does before the process ends
var (
atExitMu sync.Mutex
atExit func()
)
// setAtExit: f to run when modemmenu is hung up (the usage recorded)
func setAtExit(f func()) {
atExitMu.Lock()
atExit = f
atExitMu.Unlock()
}
// runAtExit: the function setAtExit gave, if any
func runAtExit() {
atExitMu.Lock()
f := atExit
atExitMu.Unlock()
if f != nil {
f()
}
}