mirror of
https://github.com/D-Modem/login.git
synced 2026-11-28 20:38:06 -08:00
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
287 lines
8.0 KiB
Go
287 lines
8.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Who may use what: groups of users, the access rules of the items, the
|
|
// guest account, signup, caller ID rules and time limits.
|
|
|
|
// itemAccess: an item's rules; all that are set must pass
|
|
type itemAccess struct {
|
|
users []string // these users ...
|
|
groups []string // ... or the members of these groups (either set: only them)
|
|
minBaud int // the connect rate at least this (an unknown rate does not pass)
|
|
callerIDs []string // the caller ID matching one of these patterns
|
|
guest bool // guests see it (they see nothing else)
|
|
}
|
|
|
|
func loadItemAccess(name string, sec *section) (itemAccess, error) {
|
|
var a itemAccess
|
|
var err error
|
|
a.users = fields(sec.get("users", ""))
|
|
a.groups = fields(sec.get("groups", ""))
|
|
a.callerIDs = fields(sec.get("callerids", ""))
|
|
if a.minBaud, err = sec.int("min_baud", 0); err != nil {
|
|
return a, err
|
|
}
|
|
if a.guest, err = sec.bool("guest", false); err != nil {
|
|
return a, err
|
|
}
|
|
for _, p := range a.callerIDs {
|
|
if _, err := path.Match(p, ""); err != nil {
|
|
return a, fmt.Errorf("[item %s] callerids: %q: %v", name, p, err)
|
|
}
|
|
}
|
|
return a, nil
|
|
}
|
|
|
|
// guestSettings: [guest], a login without a password
|
|
type guestSettings struct {
|
|
enabled bool
|
|
name string
|
|
timeLimit time.Duration
|
|
}
|
|
|
|
// signupSettings: [signup], new accounts in the users file
|
|
type signupSettings struct {
|
|
enabled bool
|
|
name string // typed at the login prompt
|
|
file string
|
|
pending bool // new accounts locked until the sysop unlocks them
|
|
minPassword int
|
|
message string
|
|
}
|
|
|
|
// numberRule: a [number PATTERN] section
|
|
type numberRule struct {
|
|
pattern string
|
|
deny bool
|
|
user string // logged in as, without a password
|
|
item string // started at once after the login
|
|
}
|
|
|
|
// callerIDSettings: [callerid] and the [number ...] sections
|
|
type callerIDSettings struct {
|
|
allow []string
|
|
deny []string
|
|
anonymous bool // a call without caller ID is taken
|
|
denyMessage string
|
|
numbers []numberRule
|
|
}
|
|
|
|
// limitSettings: time limits, [general]
|
|
type limitSettings struct {
|
|
timeLimit time.Duration // a call, from the login
|
|
dailyLimit time.Duration // a user's calls in a day
|
|
stateDir string // where the daily use is kept
|
|
}
|
|
|
|
func loadAccess(c *ini, s *settings) error {
|
|
var err error
|
|
g := c.section("general")
|
|
if s.limits.timeLimit, err = g.duration("time_limit", 0); err != nil {
|
|
return err
|
|
}
|
|
if s.limits.dailyLimit, err = g.duration("daily_limit", 0); err != nil {
|
|
return err
|
|
}
|
|
s.limits.stateDir = g.get("state_dir", "/var/lib/modemmenu")
|
|
if s.limits.dailyLimit > 0 && !strings.HasPrefix(s.limits.stateDir, "/") {
|
|
return fmt.Errorf("[general] state_dir: %q (an absolute path)", s.limits.stateDir)
|
|
}
|
|
|
|
// [groups]: name = its members
|
|
s.groups = map[string][]string{}
|
|
gs := c.section("groups")
|
|
for _, k := range gs.keys {
|
|
s.groups[k] = fields(gs.get(k, ""))
|
|
}
|
|
|
|
gu := c.section("guest")
|
|
if s.guest.enabled, err = gu.bool("enabled", false); err != nil {
|
|
return err
|
|
}
|
|
s.guest.name = gu.get("name", "guest")
|
|
if s.guest.timeLimit, err = gu.duration("time_limit", 15*time.Minute); err != nil {
|
|
return err
|
|
}
|
|
|
|
su := c.section("signup")
|
|
if s.signup.enabled, err = su.bool("enabled", false); err != nil {
|
|
return err
|
|
}
|
|
s.signup.name = su.get("name", "new")
|
|
s.signup.file = su.get("file", c.section("file").get("path", "/etc/asterisk/modemmenu.users"))
|
|
if s.signup.pending, err = su.bool("pending", true); err != nil {
|
|
return err
|
|
}
|
|
if s.signup.minPassword, err = su.int("min_password", 8); err != nil {
|
|
return err
|
|
}
|
|
s.signup.message = unescape(su.get("message", `\r\nNew account. Choose a user name and a password.\r\n`))
|
|
if s.signup.enabled && !contains(s.auth, "file") {
|
|
return fmt.Errorf("[signup] enabled: new accounts go in the users file, and [general] auth has no file")
|
|
}
|
|
if s.guest.enabled && s.signup.enabled && strings.EqualFold(s.guest.name, s.signup.name) {
|
|
return fmt.Errorf("[guest] name and [signup] name are both %q", s.guest.name)
|
|
}
|
|
|
|
cs := c.section("callerid")
|
|
s.callerID.allow = fields(cs.get("allow", ""))
|
|
s.callerID.deny = fields(cs.get("deny", ""))
|
|
if s.callerID.anonymous, err = cs.bool("anonymous", true); err != nil {
|
|
return err
|
|
}
|
|
s.callerID.denyMessage = unescape(cs.get("deny_message", `\r\nThis line does not take your call.\r\n`))
|
|
for _, p := range append(append([]string{}, s.callerID.allow...), s.callerID.deny...) {
|
|
if _, err := path.Match(p, ""); err != nil {
|
|
return fmt.Errorf("[callerid] %q: %v", p, err)
|
|
}
|
|
}
|
|
for _, sec := range c.sections {
|
|
p, ok := strings.CutPrefix(sec.name, "number ")
|
|
if !ok {
|
|
continue
|
|
}
|
|
if _, err := path.Match(p, ""); err != nil {
|
|
return fmt.Errorf("[%s]: %v", sec.name, err)
|
|
}
|
|
r := numberRule{pattern: p, user: sec.get("user", ""), item: sec.get("item", "")}
|
|
if r.deny, err = sec.bool("deny", false); err != nil {
|
|
return err
|
|
}
|
|
s.callerID.numbers = append(s.callerID.numbers, r)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkAccess after the items are loaded: what refers to them exists
|
|
func checkAccess(s *settings) error {
|
|
for _, r := range s.callerID.numbers {
|
|
if r.item != "" && s.selects(strings.ToUpper(r.item)) == nil {
|
|
return fmt.Errorf("[number %s] item: no item %q", r.pattern, r.item)
|
|
}
|
|
if r.user != "" && !s.userPattern.MatchString(r.user) {
|
|
return fmt.Errorf("[number %s] user: %q does not match user_pattern", r.pattern, r.user)
|
|
}
|
|
}
|
|
for _, it := range s.items {
|
|
for _, g := range it.access.groups {
|
|
if _, ok := s.groups[g]; !ok && !contains(s.auth, "system") && !contains(s.auth, "pam") {
|
|
return fmt.Errorf("[item %s] groups: no %s in [groups]", it.name, g)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// matchNumber: a caller ID against patterns (* and ? as in file names);
|
|
// "anonymous" matches a call without one
|
|
func matchNumber(patterns []string, number string) bool {
|
|
for _, p := range patterns {
|
|
if number == "" {
|
|
if p == "anonymous" {
|
|
return true
|
|
}
|
|
continue
|
|
}
|
|
if ok, _ := path.Match(p, number); ok {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// callerAllowed: whether the line takes the call, by [callerid] and the
|
|
// [number ...] rules
|
|
func (s *settings) callerAllowed(number string) bool {
|
|
cid := s.callerID
|
|
if number == "" && !cid.anonymous {
|
|
return false
|
|
}
|
|
if r := s.numberRule(number); r != nil && r.deny {
|
|
return false
|
|
}
|
|
if matchNumber(cid.deny, number) {
|
|
return false
|
|
}
|
|
if len(cid.allow) > 0 && number != "" && !matchNumber(cid.allow, number) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// numberRule: the first [number ...] whose pattern the caller ID matches
|
|
func (s *settings) numberRule(number string) *numberRule {
|
|
if number == "" {
|
|
return nil
|
|
}
|
|
for i, r := range s.callerID.numbers {
|
|
if ok, _ := path.Match(r.pattern, number); ok {
|
|
return &s.callerID.numbers[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// inGroup: the session's user is a member of group, by [groups], or for a
|
|
// system account (auth = system) by the system's groups
|
|
func (s *settings) inGroup(sess *session, group string) bool {
|
|
if contains(s.groups[group], sess.User) {
|
|
return true
|
|
}
|
|
if sess.Auth != "system" && sess.Auth != "upstream:system" {
|
|
return false
|
|
}
|
|
acct, err := lookupNSS(sess.User)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
in, err := inGroupsNSS(acct, []string{group})
|
|
return err == nil && in
|
|
}
|
|
|
|
// allows: whether the session may see and use the item
|
|
func (s *settings) allows(it *menuItem, sess *session) bool {
|
|
a := it.access
|
|
if sess.Auth == "guest" {
|
|
if !a.guest {
|
|
return false
|
|
}
|
|
} else if len(a.users) > 0 || len(a.groups) > 0 {
|
|
ok := contains(a.users, sess.User)
|
|
for _, g := range a.groups {
|
|
ok = ok || s.inGroup(sess, g)
|
|
}
|
|
if !ok {
|
|
return false
|
|
}
|
|
}
|
|
if a.minBaud > 0 {
|
|
baud, err := strconv.Atoi(sess.Baud)
|
|
if err != nil || baud < a.minBaud {
|
|
return false
|
|
}
|
|
}
|
|
if len(a.callerIDs) > 0 && !matchNumber(a.callerIDs, sess.CallerID) {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// visible: the items the session may use, in menu order
|
|
func (s *settings) visible(sess *session) []*menuItem {
|
|
var items []*menuItem
|
|
for _, it := range s.items {
|
|
if s.allows(it, sess) {
|
|
items = append(items, it)
|
|
}
|
|
}
|
|
return items
|
|
}
|