Files
login/session.go
T
Claude 98755b757b Who sees what, time limits, guests and signup, caller ID rules, comments
Items take users, groups ([groups], or the system's for auth = system),
min_baud, callerids and guest; a caller neither sees nor can choose an
item they may not use.

time_limit limits a call and daily_limit a user's calls in a day (kept
in state_dir/usage under a lock); a selection is hung up at the limit,
and {time_left} / MODEM_TIME_LEFT tell what is left.

[guest] logs in by name without a password, to the guest = yes items
for its own time_limit.  [signup] adds new accounts to the users file
with a bcrypt hash, locked (user:!hash) until the sysop unlocks them
unless pending = no; the file backend refuses locked accounts.

[callerid] allow, deny and anonymous decide which calls the line takes;
[number PATTERN] sections deny a number, log it in as a user, or start
an item at once.

Comments may follow a value after a space; \; and \# escape them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
2026-10-12 11:37:10 +00:00

161 lines
3.9 KiB
Go

package main
import (
"encoding/json"
"os"
"sort"
"strconv"
"strings"
"time"
)
// session is what is known of the call and the caller, handed to the
// selection in its environment, its command line and its session file
type session struct {
User string `json:"user"`
Password string `json:"password,omitempty"`
Auth string `json:"auth"`
Dialed string `json:"dialed"`
CallerID string `json:"callerid"`
Baud string `json:"baud"`
Rate string `json:"rate"`
TxRate string `json:"tx_rate"`
RxRate string `json:"rx_rate"`
Channel string `json:"channel"`
Profile string `json:"profile"`
Connected time.Time `json:"connected"`
Selection string `json:"selection"`
TimeLeft string `json:"time_left"` // whole minutes; "": no limit
}
// newSession from app_dmodem's environment (DMODEM_RATE "tx/rx",
// DMODEM_CALLERID, DMODEM_DIALED, DMODEM_CHANNEL, DMODEM_PROFILE), what the
// command line overrides of it
func newSession(rate, dialed, callerID string, now time.Time) *session {
s := &session{
Dialed: dialed,
CallerID: callerID,
Channel: os.Getenv("DMODEM_CHANNEL"),
Profile: os.Getenv("DMODEM_PROFILE"),
Connected: now,
}
s.Rate = rate
tx, rx, ok := strings.Cut(rate, "/")
if !ok {
rx = tx
}
if _, err := strconv.Atoi(tx); err == nil {
s.TxRate, s.RxRate = tx, rx
// the speed the caller's modem reports: what reaches it
s.Baud = tx
}
return s
}
// vars: the placeholders of templates and the environment, by name
func (s *session) vars(withPassword bool) map[string]string {
v := map[string]string{
"user": s.User,
"auth": s.Auth,
"dialed": s.Dialed,
"callerid": s.CallerID,
"baud": s.Baud,
"rate": s.Rate,
"tx_rate": s.TxRate,
"rx_rate": s.RxRate,
"channel": s.Channel,
"profile": s.Profile,
"selection": s.Selection,
"time_left": s.TimeLeft,
"date": s.Connected.Format("2006-01-02"),
"time": s.Connected.Format("15:04:05"),
"datetime": s.Connected.Format(time.RFC3339),
"epoch": strconv.FormatInt(s.Connected.Unix(), 10),
"password": "",
}
if withPassword {
v["password"] = s.Password
}
return v
}
// environ: MODEM_<NAME> for every placeholder
func (s *session) environ(withPassword bool) []string {
v := s.vars(withPassword)
names := make([]string, 0, len(v))
for k := range v {
names = append(names, k)
}
sort.Strings(names)
env := make([]string, 0, len(v))
for _, k := range names {
if k == "password" && !withPassword {
continue
}
env = append(env, "MODEM_"+strings.ToUpper(k)+"="+v[k])
}
return env
}
// writeFile: the session as JSON in a file only its owner reads
func (s *session) writeFile(withPassword bool, uid, gid int) (string, error) {
c := *s
if !withPassword {
c.Password = ""
}
b, err := json.MarshalIndent(&c, "", " ")
if err != nil {
return "", err
}
f, err := os.CreateTemp("", "modemmenu-*.json")
if err != nil {
return "", err
}
if _, err = f.Write(append(b, '\n')); err == nil && uid >= 0 {
err = f.Chown(uid, gid)
}
if cerr := f.Close(); err == nil {
err = cerr
}
if err != nil {
os.Remove(f.Name())
return "", err
}
return f.Name(), nil
}
// expand replaces {name} by its value, quote() of it (nil: as is);
// unknown names stay as they are
func expand(t string, v map[string]string, quote func(string) string) string {
var b strings.Builder
for {
i := strings.IndexByte(t, '{')
if i < 0 {
break
}
j := strings.IndexByte(t[i:], '}')
if j < 0 {
break
}
name := t[i+1 : i+j]
val, ok := v[strings.ToLower(name)]
b.WriteString(t[:i])
switch {
case !ok:
b.WriteString(t[i : i+j+1])
case quote != nil:
b.WriteString(quote(val))
default:
b.WriteString(val)
}
t = t[i+j+1:]
}
b.WriteString(t)
return b.String()
}
// shellQuote: a value as one word of /bin/sh
func shellQuote(s string) string {
return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'"
}