Files
login/session.go
T
Claude da20bbd4ae LDAP groups, AD password changes, the time warning, the last callers
- LDAP groups for the items' groups =: with [ldap] group_base, a group
  is an entry there that group_filter finds (groupOfNames,
  groupOfUniqueNames and posixGroup by default), searched as
  search_bind_dn or as the caller; each answer kept for the call.
- [ldap] password_change = ad: mode = passwd changes an Active
  Directory password (unicodePwd's old value deleted, the new one
  added, quoted UTF-16LE), AD having no Password Modify.
- time_warning (2m) before the time limit, time_warning_message is
  written on the line in the middle of a running selection, unless the
  item says time_warning = no (the sample's PPP does).
- mode = callers: the last callers (time, user, rate; no caller ID),
  each login recorded in state_dir/callers (the latest 200) when there
  is such an item.
- The sample's end has items to copy: ZMODEM uploads (rz -b -R -E),
  a Kermit server (kermit -Y -x), lynx -anonymous, mutt, tin, irssi,
  Gopher and the last callers; it loads with all of them on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RvtV8Dg9kqRaakbTEfyH3f
2026-10-15 19:51:33 +00:00

163 lines
4.0 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
groupCache map[string]bool // the backends' group answers, for the call
}
// 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, "'", `'\''`) + "'"
}