2019-11-06 10:08:44 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-27 12:52:29 -04:00
|
|
|
"errors"
|
2023-02-10 11:19:21 -05:00
|
|
|
"github.com/BurntSushi/toml"
|
2020-07-01 17:05:49 -04:00
|
|
|
"log"
|
|
|
|
"os"
|
2020-07-01 10:18:49 -04:00
|
|
|
"path/filepath"
|
2023-01-28 13:16:11 -05:00
|
|
|
"strings"
|
2019-11-06 10:08:44 -05:00
|
|
|
)
|
|
|
|
|
2023-02-25 05:29:13 -05:00
|
|
|
type SysConfig struct {
|
2023-02-10 11:19:21 -05:00
|
|
|
Port int
|
|
|
|
Hostname string
|
|
|
|
CertPath string
|
|
|
|
KeyPath string
|
|
|
|
AccessLog string
|
|
|
|
ErrorLog string
|
2023-02-25 05:29:13 -05:00
|
|
|
DocBase string
|
|
|
|
HomeDocBase string
|
2023-02-10 11:19:21 -05:00
|
|
|
CGIPaths []string
|
|
|
|
SCGIPaths map[string]string
|
2023-02-25 05:29:13 -05:00
|
|
|
ReadMollyFiles bool
|
2023-02-23 13:31:16 -05:00
|
|
|
AllowTLS12 bool
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:29:13 -05:00
|
|
|
type UserConfig struct {
|
2023-02-10 11:19:21 -05:00
|
|
|
GeminiExt string
|
2023-02-25 05:29:13 -05:00
|
|
|
DefaultLang string
|
|
|
|
DefaultEncoding string
|
2023-02-10 11:19:21 -05:00
|
|
|
TempRedirects map[string]string
|
|
|
|
PermRedirects map[string]string
|
|
|
|
MimeOverrides map[string]string
|
|
|
|
CertificateZones map[string][]string
|
|
|
|
DirectorySort string
|
2023-02-05 08:35:29 -05:00
|
|
|
DirectorySubdirsFirst bool
|
2023-02-10 11:19:21 -05:00
|
|
|
DirectoryReverse bool
|
|
|
|
DirectoryTitles bool
|
2020-06-10 14:40:13 -04:00
|
|
|
}
|
|
|
|
|
2023-02-25 05:29:13 -05:00
|
|
|
func getConfig(filename string) (SysConfig, UserConfig, error) {
|
2019-11-06 10:08:44 -05:00
|
|
|
|
2023-02-25 05:29:13 -05:00
|
|
|
var sysConfig SysConfig
|
|
|
|
var userConfig UserConfig
|
2019-11-06 10:08:44 -05:00
|
|
|
|
|
|
|
// Defaults
|
2023-02-25 05:29:13 -05:00
|
|
|
sysConfig.Port = 1965
|
|
|
|
sysConfig.Hostname = "localhost"
|
|
|
|
sysConfig.CertPath = "cert.pem"
|
|
|
|
sysConfig.KeyPath = "key.pem"
|
|
|
|
sysConfig.AccessLog = "access.log"
|
|
|
|
sysConfig.ErrorLog = ""
|
|
|
|
sysConfig.DocBase = "/var/gemini/"
|
|
|
|
sysConfig.HomeDocBase = "users"
|
|
|
|
sysConfig.CGIPaths = make([]string, 0)
|
|
|
|
sysConfig.SCGIPaths = make(map[string]string)
|
|
|
|
sysConfig.ReadMollyFiles = false
|
|
|
|
sysConfig.AllowTLS12 = true
|
|
|
|
|
|
|
|
userConfig.GeminiExt = "gmi"
|
|
|
|
userConfig.DefaultLang = ""
|
|
|
|
userConfig.DefaultEncoding = ""
|
|
|
|
userConfig.TempRedirects = make(map[string]string)
|
|
|
|
userConfig.PermRedirects = make(map[string]string)
|
|
|
|
userConfig.DirectorySort = "Name"
|
|
|
|
userConfig.DirectorySubdirsFirst = false
|
2019-11-06 10:08:44 -05:00
|
|
|
|
|
|
|
// Return defaults if no filename given
|
|
|
|
if filename == "" {
|
2023-02-25 05:29:13 -05:00
|
|
|
return sysConfig, userConfig, nil
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to overwrite defaults from file
|
2023-02-26 13:42:30 -05:00
|
|
|
sysConfig, err := readSysConfig(filename, sysConfig)
|
2019-11-06 10:08:44 -05:00
|
|
|
if err != nil {
|
2023-02-25 05:29:13 -05:00
|
|
|
return sysConfig, userConfig, err
|
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
userConfig, err = readUserConfig(filename, userConfig, true)
|
2023-02-25 05:29:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return sysConfig, userConfig, err
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
return sysConfig, userConfig, nil
|
|
|
|
}
|
2020-06-27 12:52:29 -04:00
|
|
|
|
2023-02-26 13:42:30 -05:00
|
|
|
func readSysConfig(filename string, config SysConfig) (SysConfig, error) {
|
2023-01-28 13:22:31 -05:00
|
|
|
|
2023-02-26 13:42:30 -05:00
|
|
|
_, err := toml.DecodeFile(filename, &config)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
2020-06-27 12:52:29 -04:00
|
|
|
}
|
|
|
|
|
2023-02-26 13:42:30 -05:00
|
|
|
// Force hostname to lowercase
|
|
|
|
config.Hostname = strings.ToLower(config.Hostname)
|
|
|
|
|
2023-02-23 12:49:15 -05:00
|
|
|
// Absolutise paths
|
2023-02-26 13:42:30 -05:00
|
|
|
config.DocBase, err = filepath.Abs(config.DocBase)
|
2023-02-23 12:49:15 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
2023-02-23 12:49:15 -05:00
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
config.CertPath, err = filepath.Abs(config.CertPath)
|
2023-02-23 12:49:15 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
2023-02-23 12:49:15 -05:00
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
config.KeyPath, err = filepath.Abs(config.KeyPath)
|
2023-02-23 12:49:15 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
2023-02-23 12:49:15 -05:00
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
if config.AccessLog != "" && config.AccessLog != "-" {
|
|
|
|
config.AccessLog, err = filepath.Abs(config.AccessLog)
|
2023-02-15 15:15:14 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
2023-02-15 15:15:14 -05:00
|
|
|
}
|
2023-02-15 15:10:22 -05:00
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
if config.ErrorLog != "" {
|
|
|
|
config.ErrorLog, err = filepath.Abs(config.ErrorLog)
|
2023-02-08 14:32:17 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
2023-02-08 14:32:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-05 10:54:00 -05:00
|
|
|
// Absolutise CGI paths
|
2023-02-26 13:42:30 -05:00
|
|
|
for index, cgiPath := range config.CGIPaths {
|
2023-02-10 11:19:21 -05:00
|
|
|
if !filepath.IsAbs(cgiPath) {
|
2023-02-26 13:42:30 -05:00
|
|
|
config.CGIPaths[index] = filepath.Join(config.DocBase, cgiPath)
|
2023-02-05 10:54:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 10:18:49 -04:00
|
|
|
// Expand CGI paths
|
|
|
|
var cgiPaths []string
|
2023-02-26 13:42:30 -05:00
|
|
|
for _, cgiPath := range config.CGIPaths {
|
2020-07-01 10:18:49 -04:00
|
|
|
expandedPaths, err := filepath.Glob(cgiPath)
|
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, errors.New("Error expanding CGI path glob " + cgiPath + ": " + err.Error())
|
2020-07-01 10:18:49 -04:00
|
|
|
}
|
|
|
|
cgiPaths = append(cgiPaths, expandedPaths...)
|
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
config.CGIPaths = cgiPaths
|
2020-07-01 10:18:49 -04:00
|
|
|
|
2023-02-08 14:32:17 -05:00
|
|
|
// Absolutise SCGI paths
|
2023-02-26 13:42:30 -05:00
|
|
|
for index, scgiPath := range config.SCGIPaths {
|
|
|
|
config.SCGIPaths[index], err = filepath.Abs( scgiPath)
|
2023-02-23 12:49:15 -05:00
|
|
|
if err != nil {
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readUserConfig(filename string, config UserConfig, requireValid bool) (UserConfig, error) {
|
|
|
|
|
|
|
|
_, err := toml.DecodeFile(filename, &config)
|
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate pseudo-enums
|
|
|
|
if requireValid {
|
|
|
|
switch config.DirectorySort {
|
|
|
|
case "Name", "Size", "Time":
|
|
|
|
default:
|
|
|
|
return config, errors.New("Invalid DirectorySort value.")
|
2023-02-08 14:32:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-28 13:16:11 -05:00
|
|
|
// Validate redirects
|
2023-02-26 13:42:30 -05:00
|
|
|
for key, value := range config.TempRedirects {
|
2023-01-28 13:16:11 -05:00
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
2023-02-26 13:42:30 -05:00
|
|
|
if requireValid {
|
|
|
|
return config, errors.New("Invalid cross-protocol redirect to " + value)
|
|
|
|
} else {
|
|
|
|
log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + filename)
|
|
|
|
delete(config.TempRedirects, key)
|
|
|
|
}
|
2023-01-28 13:16:11 -05:00
|
|
|
}
|
|
|
|
}
|
2023-02-26 13:42:30 -05:00
|
|
|
for key, value := range config.PermRedirects {
|
2023-01-28 13:16:11 -05:00
|
|
|
if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
|
2023-02-26 13:42:30 -05:00
|
|
|
if requireValid {
|
|
|
|
return config, errors.New("Invalid cross-protocol redirect to " + value)
|
|
|
|
} else {
|
|
|
|
log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + filename)
|
|
|
|
delete(config.PermRedirects, key)
|
|
|
|
}
|
2023-01-28 13:16:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-26 13:42:30 -05:00
|
|
|
return config, nil
|
2019-11-06 10:08:44 -05:00
|
|
|
}
|
2020-07-01 17:05:49 -04:00
|
|
|
|
2023-02-25 05:29:13 -05:00
|
|
|
func parseMollyFiles(path string, docBase string, config UserConfig) UserConfig {
|
2020-07-06 13:08:03 -04:00
|
|
|
// Replace config variables which use pointers with new ones,
|
|
|
|
// so that changes made here aren't reflected everywhere.
|
2023-02-25 05:29:13 -05:00
|
|
|
config.TempRedirects = make(map[string]string)
|
|
|
|
config.PermRedirects = make(map[string]string)
|
|
|
|
config.MimeOverrides = make(map[string]string)
|
|
|
|
config.CertificateZones = make(map[string][]string)
|
|
|
|
|
2020-07-01 17:05:49 -04:00
|
|
|
// Build list of directories to check
|
|
|
|
var dirs []string
|
|
|
|
dirs = append(dirs, path)
|
|
|
|
for {
|
2023-02-25 05:29:13 -05:00
|
|
|
if path == filepath.Clean(docBase) {
|
2020-07-01 17:05:49 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
subpath := filepath.Dir(path)
|
|
|
|
dirs = append(dirs, subpath)
|
|
|
|
path = subpath
|
|
|
|
}
|
|
|
|
// Parse files in reverse order
|
|
|
|
for i := len(dirs) - 1; i >= 0; i-- {
|
|
|
|
dir := dirs[i]
|
|
|
|
// Break out of the loop if a directory doesn't exist
|
|
|
|
_, err := os.Stat(dir)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Construct path for a .molly file in this dir
|
|
|
|
mollyPath := filepath.Join(dir, ".molly")
|
|
|
|
_, err = os.Stat(mollyPath)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// If the file exists and we can read it, try to parse it
|
2023-02-26 13:42:30 -05:00
|
|
|
newConfig, err = readUserConfig(mollyPath, config, false)
|
2020-07-01 17:05:49 -04:00
|
|
|
if err != nil {
|
2023-02-19 09:04:34 -05:00
|
|
|
log.Println("Error parsing .molly file " + mollyPath + ": " + err.Error())
|
2020-07-01 17:05:49 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-02-25 05:29:13 -05:00
|
|
|
|
|
|
|
return config
|
2020-07-01 17:05:49 -04:00
|
|
|
}
|