mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 12:28:07 -08:00
An exec item with dropfile = door32.sys door.sys writes DOOR32.SYS (11
lines) and DOOR.SYS (the 52-line GAP format) before its program starts
and removes them after: the user, the rate, the caller ID, the security
level ([dropfile] security, or the item's), the time left (minutes, and
seconds in DOOR.SYS), the emulation and the node. Each door run takes
the first free node of max_nodes (a lock in state_dir/nodes, held while
it runs), each user keeps a number given once (state_dir/usernumbers).
The files go in dropfile_dir ({node} in it) or a new directory; commands
get {node}, {dropfile_dir}, MODEM_NODE and MODEM_DROPFILE_DIR. The
password is not written. [dropfile] has the BBS's name, the sysop and
the comm type, handle and port.
MODEM_SECONDS_LEFT joins MODEM_TIME_LEFT ({seconds_left}) in the
environment, the session file and the header. A modemmenu -serve keeps
the header's time left as a limit of its own, so that going from one
menu to another gives no new allowance.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
198 lines
4.6 KiB
Go
198 lines
4.6 KiB
Go
// Copyright (C) 2026 Neo-Desktop
|
|
// 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.
|
|
|
|
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
|
|
}
|
|
|
|
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()
|
|
)
|
|
|
|
func setAtExit(f func()) {
|
|
atExitMu.Lock()
|
|
atExit = f
|
|
atExitMu.Unlock()
|
|
}
|
|
|
|
func runAtExit() {
|
|
atExitMu.Lock()
|
|
f := atExit
|
|
atExitMu.Unlock()
|
|
if f != nil {
|
|
f()
|
|
}
|
|
}
|