mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
File areas hold files: records in the database, contents in files_dir under the record's number, so no name a user gives becomes a path. Callers browse them from the text BBS (F) and on 3270s (8): the files' size, uploader, downloads, SHA-256 and where to fetch them; uploaders and sysops edit descriptions and delete. [ftp] runs an FTP server (fclairamb/ftpserverlib) whose file system is the areas: / lists them, an area its files, nothing else exists. Users log in with their BBS name and password, through the BBS's own check (fail delay, throttling by address). They download anything, upload new files into areas that take them (names checked, max_file_size and a daily upload_quota, written to a temporary file and hashed before they are recorded), and delete their own; overwriting, appending, resumed uploads, renames, directories, SITE and MFMT are refused. Active mode is off, data connections must come from the control connection's address, and tls = required (the default) refuses USER before AUTH TLS; implicit, optional and no exist, the last two with a warning. anonymous = yes lets anonymous download. max_clients and max_per_ip limit sessions. bbs filearea add|del|list and bbs file add|list|del look after them; bbs init makes an Uploads area. Go 1.24 is needed now (the library's). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
106 lines
3.3 KiB
Go
106 lines
3.3 KiB
Go
package server
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Neo-Desktop/bbs-sec/internal/config"
|
|
"github.com/Neo-Desktop/bbs-sec/internal/ftp"
|
|
)
|
|
|
|
// parseSize reads a size: a number of bytes, or with K, M or G.
|
|
func parseSize(s *config.Section, key string, def int64) (int64, error) {
|
|
v := strings.ToUpper(strings.TrimSpace(s.String(key, "")))
|
|
if v == "" {
|
|
return def, nil
|
|
}
|
|
mult := int64(1)
|
|
switch {
|
|
case strings.HasSuffix(v, "K"):
|
|
mult, v = 1<<10, strings.TrimSuffix(v, "K")
|
|
case strings.HasSuffix(v, "M"):
|
|
mult, v = 1<<20, strings.TrimSuffix(v, "M")
|
|
case strings.HasSuffix(v, "G"):
|
|
mult, v = 1<<30, strings.TrimSuffix(v, "G")
|
|
}
|
|
n, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil || n < 0 || n > (1<<42)/mult {
|
|
return 0, fmt.Errorf("%s: %q is not a size (bytes, or with K, M, G)", key, s.String(key, ""))
|
|
}
|
|
return n * mult, nil
|
|
}
|
|
|
|
// files reads the file areas' options of [general].
|
|
func (c *Config) files(g *config.Section) error {
|
|
c.FilesDir = g.String("files_dir", c.FilesDir)
|
|
var err error
|
|
if c.MaxFileSize, err = parseSize(g, "max_file_size", 64<<20); err != nil {
|
|
return err
|
|
}
|
|
if c.MaxFileSize == 0 {
|
|
return errors.New("max_file_size: 0 takes no files")
|
|
}
|
|
if c.UploadQuota, err = parseSize(g, "upload_quota", 256<<20); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ftpSection reads [ftp].
|
|
func (c *Config) ftpSection(f *config.Section) error {
|
|
if f == nil {
|
|
return nil
|
|
}
|
|
set := &ftp.Settings{Address: f.String("address", ":2121"), Banner: c.Settings.Name}
|
|
if _, _, err := net.SplitHostPort(set.Address); err != nil {
|
|
return fmt.Errorf("address: %v", err)
|
|
}
|
|
set.PublicHost = f.String("public_host", "")
|
|
if set.PublicHost != "" && net.ParseIP(set.PublicHost) == nil {
|
|
return errors.New("public_host: an IP address (the library takes no names)")
|
|
}
|
|
if r := f.String("passive_ports", ""); r != "" {
|
|
lo, hi, ok := strings.Cut(r, "-")
|
|
a, err1 := strconv.Atoi(strings.TrimSpace(lo))
|
|
b, err2 := strconv.Atoi(strings.TrimSpace(hi))
|
|
if !ok || err1 != nil || err2 != nil || a < 1024 || b > 65535 || a > b {
|
|
return fmt.Errorf("passive_ports: %q is not a range like 50000-50100", r)
|
|
}
|
|
set.PassiveMin, set.PassiveMax = a, b
|
|
}
|
|
set.TLS = strings.ToLower(f.String("tls", ftp.TLSRequired))
|
|
switch set.TLS {
|
|
case ftp.TLSNo:
|
|
c.Warnings = append(c.Warnings, "[ftp] tls = no: passwords go over the network in the clear")
|
|
case ftp.TLSOptional:
|
|
c.Warnings = append(c.Warnings, "[ftp] tls = optional: clients that do not ask for TLS send passwords in the clear")
|
|
case ftp.TLSRequired, ftp.TLSImplicit:
|
|
default:
|
|
return fmt.Errorf("tls: %q is not required, implicit, optional or no", set.TLS)
|
|
}
|
|
set.CertFile = f.String("cert_file", "")
|
|
set.KeyFile = f.String("key_file", "")
|
|
if set.TLS != ftp.TLSNo && (set.CertFile == "" || set.KeyFile == "") {
|
|
return errors.New("cert_file and key_file are needed for TLS (or tls = no)")
|
|
}
|
|
var err error
|
|
if set.Anonymous, err = f.Bool("anonymous", false); err != nil {
|
|
return err
|
|
}
|
|
if set.MaxClients, err = f.Int("max_clients", 32, 1, 10000); err != nil {
|
|
return err
|
|
}
|
|
if set.MaxPerIP, err = f.Int("max_per_ip", 4, 0, 1000); err != nil {
|
|
return err
|
|
}
|
|
if set.IdleTimeout, err = f.Seconds("idle_timeout", 300, 30, 86400); err != nil {
|
|
return err
|
|
}
|
|
c.FTPAdvertise = f.String("advertise", set.Address)
|
|
c.FTP = set
|
|
return nil
|
|
}
|