2019-11-06 17:08:44 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"flag"
|
2023-02-05 15:36:18 +01:00
|
|
|
"fmt"
|
2019-11-06 17:08:44 +02:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-02-08 19:56:27 +01:00
|
|
|
"os/signal"
|
2020-05-21 22:50:33 +02:00
|
|
|
"strconv"
|
2023-02-08 19:56:27 +01:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
2019-11-06 17:08:44 +02:00
|
|
|
)
|
|
|
|
|
2023-02-05 15:36:18 +01:00
|
|
|
var VERSION = "0.0.0"
|
|
|
|
|
2019-11-06 17:08:44 +02:00
|
|
|
func main() {
|
|
|
|
var conf_file string
|
2023-02-05 15:36:18 +01:00
|
|
|
var version bool
|
2019-11-06 17:08:44 +02:00
|
|
|
|
2023-02-05 15:36:18 +01:00
|
|
|
// Parse args
|
2019-11-06 17:08:44 +02:00
|
|
|
flag.StringVar(&conf_file, "c", "", "Path to config file")
|
2023-02-05 15:36:18 +01:00
|
|
|
flag.BoolVar(&version, "v", false, "Print version and exit")
|
2019-11-06 17:08:44 +02:00
|
|
|
flag.Parse()
|
2023-02-05 15:36:18 +01:00
|
|
|
|
|
|
|
// If requested, print version and exit
|
|
|
|
if version {
|
|
|
|
fmt.Println("Molly Brown version", VERSION)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read config
|
2019-11-06 17:08:44 +02:00
|
|
|
if conf_file == "" {
|
|
|
|
_, err := os.Stat("/etc/molly.conf")
|
2020-07-01 19:56:43 +02:00
|
|
|
if err == nil {
|
2019-11-06 17:08:44 +02:00
|
|
|
conf_file = "/etc/molly.conf"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config, err := getConfig(conf_file)
|
|
|
|
if err != nil {
|
2020-06-08 20:02:29 +02:00
|
|
|
log.Fatal(err)
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|
|
|
|
|
2023-02-19 14:40:54 +01:00
|
|
|
// Run server and exit
|
|
|
|
os.Exit(do_main(config))
|
|
|
|
}
|
|
|
|
|
|
|
|
func do_main(config Config) int {
|
|
|
|
|
2023-02-15 21:10:22 +01:00
|
|
|
// If we are running as root, find the UID of the "nobody" user, before a
|
|
|
|
// chroot() possibly stops seeing /etc/passwd
|
2023-02-19 14:40:54 +01:00
|
|
|
privInfo, err := getUserInfo(config)
|
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Exiting due to failure to apply security restrictions.")
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
|
|
|
}
|
2023-02-15 21:10:22 +01:00
|
|
|
|
|
|
|
// Chroot, if asked
|
|
|
|
if config.ChrootDir != "" {
|
|
|
|
err := syscall.Chroot(config.ChrootDir)
|
|
|
|
if err != nil {
|
2023-02-19 14:40:54 +01:00
|
|
|
log.Println("Could not chroot to " + config.ChrootDir + ": " + err.Error())
|
|
|
|
return 1
|
2023-02-15 21:10:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-28 18:34:50 +02:00
|
|
|
// Open log files
|
2023-02-19 15:04:34 +01:00
|
|
|
if config.ErrorLog != "" {
|
|
|
|
errorLogFile, err := os.OpenFile(config.ErrorLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
2021-01-24 17:09:47 +01:00
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error opening error log file: " + err.Error())
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2021-01-24 17:09:47 +01:00
|
|
|
}
|
|
|
|
defer errorLogFile.Close()
|
2023-02-19 15:04:34 +01:00
|
|
|
log.SetOutput(errorLogFile)
|
2019-11-06 18:38:41 +02:00
|
|
|
}
|
2023-02-19 15:04:34 +01:00
|
|
|
log.SetFlags(log.Ldate|log.Ltime)
|
2020-07-01 19:57:39 +02:00
|
|
|
|
2021-01-24 17:09:47 +01:00
|
|
|
var accessLogFile *os.File
|
|
|
|
if config.AccessLog == "-" {
|
|
|
|
accessLogFile = os.Stdout
|
2023-02-15 21:10:22 +01:00
|
|
|
} else if config.AccessLog != "" {
|
2021-01-24 17:09:47 +01:00
|
|
|
accessLogFile, err = os.OpenFile(config.AccessLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error opening access log file: " + err.Error())
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2021-01-24 17:09:47 +01:00
|
|
|
}
|
|
|
|
defer accessLogFile.Close()
|
2020-06-28 18:34:50 +02:00
|
|
|
}
|
2019-11-06 18:38:41 +02:00
|
|
|
|
2019-11-06 17:08:44 +02:00
|
|
|
// Read TLS files, create TLS config
|
2023-02-07 19:23:35 +01:00
|
|
|
// Check key file permissions first
|
|
|
|
info, err := os.Stat(config.KeyPath)
|
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error opening TLS key file: " + err.Error())
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2023-02-07 19:23:35 +01:00
|
|
|
}
|
|
|
|
if uint64(info.Mode().Perm())&0444 == 0444 {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Refusing to use world-readable TLS key file " + config.KeyPath)
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2023-02-07 19:23:35 +01:00
|
|
|
}
|
2019-11-06 17:08:44 +02:00
|
|
|
cert, err := tls.LoadX509KeyPair(config.CertPath, config.KeyPath)
|
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error loading TLS keypair: " + err.Error())
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|
2023-02-19 15:17:45 +01:00
|
|
|
var tlscfg tls.Config
|
|
|
|
tlscfg.Certificates = []tls.Certificate{cert}
|
|
|
|
tlscfg.MinVersion = tls.VersionTLS12
|
|
|
|
if len(config.CertificateZones) > 0 {
|
|
|
|
tlscfg.ClientAuth = tls.RequestClientCert
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 21:10:22 +01:00
|
|
|
// Try to chdir to /, so we don't block any mountpoints
|
|
|
|
// But if we can't for some reason it's no big deal
|
|
|
|
err = os.Chdir("/")
|
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Could not change working directory to /: " + err.Error())
|
2023-02-15 21:10:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply security restrictions
|
2023-02-19 15:04:34 +01:00
|
|
|
err = enableSecurityRestrictions(config, privInfo)
|
2023-02-19 14:40:54 +01:00
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Exiting due to failure to apply security restrictions.")
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
|
|
|
}
|
2023-02-10 16:16:57 +01:00
|
|
|
|
2019-11-06 17:08:44 +02:00
|
|
|
// Create TLS listener
|
2023-02-19 15:17:45 +01:00
|
|
|
listener, err := tls.Listen("tcp", ":"+strconv.Itoa(config.Port), &tlscfg)
|
2019-11-06 17:08:44 +02:00
|
|
|
if err != nil {
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error creating TLS listener: " + err.Error())
|
2023-02-19 14:40:54 +01:00
|
|
|
return 1
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
2020-06-28 18:34:50 +02:00
|
|
|
// Start log handling routines
|
2023-02-15 21:10:22 +01:00
|
|
|
var accessLogEntries chan LogEntry
|
|
|
|
if config.AccessLog == "" {
|
|
|
|
accessLogEntries = nil
|
|
|
|
} else {
|
|
|
|
accessLogEntries := make(chan LogEntry, 10)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
entry := <-accessLogEntries
|
|
|
|
writeLogEntry(accessLogFile, entry)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-09-14 22:21:05 -04:00
|
|
|
|
2023-02-08 19:56:27 +01:00
|
|
|
// Start listening for signals
|
|
|
|
shutdown := make(chan struct{})
|
|
|
|
sigterm := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigterm, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-sigterm
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Caught SIGTERM. Waiting for handlers to finish...")
|
2023-02-08 19:56:27 +01:00
|
|
|
close(shutdown)
|
|
|
|
listener.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Infinite serve loop (SIGTERM breaks out)
|
|
|
|
running := true
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for running {
|
2019-11-06 17:08:44 +02:00
|
|
|
conn, err := listener.Accept()
|
2023-02-08 19:56:27 +01:00
|
|
|
if err == nil {
|
|
|
|
wg.Add(1)
|
2023-02-19 15:04:34 +01:00
|
|
|
go handleGeminiRequest(conn, config, accessLogEntries, &wg)
|
2023-02-08 19:56:27 +01:00
|
|
|
} else {
|
|
|
|
select {
|
2023-02-10 17:19:21 +01:00
|
|
|
case <-shutdown:
|
|
|
|
running = false
|
|
|
|
default:
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Error accepting connection: " + err.Error())
|
2023-02-08 19:56:27 +01:00
|
|
|
}
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|
|
|
|
}
|
2023-02-08 19:56:27 +01:00
|
|
|
// Wait for still-running handler Go routines to finish
|
|
|
|
wg.Wait()
|
2023-02-19 15:04:34 +01:00
|
|
|
log.Println("Exiting.")
|
2019-11-06 17:08:44 +02:00
|
|
|
|
2023-02-19 14:40:54 +01:00
|
|
|
// Exit successfully
|
|
|
|
return 0
|
2019-11-06 17:08:44 +02:00
|
|
|
}
|