summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.go')
-rw-r--r--config.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..f89ca6a
--- /dev/null
+++ b/config.go
@@ -0,0 +1,70 @@
+package main
+
+import (
+ "context"
+ "os"
+ "os/signal"
+ "os/user"
+ "strconv"
+ "syscall"
+
+ "tildegit.org/tjp/sliderule/logging"
+)
+
+type config struct {
+ hostname string
+
+ geminiRoot string
+
+ tlsKeyFile string
+ tlsCertFile string
+}
+
+func configure() config {
+ return config{
+ hostname: os.Getenv("HOST_NAME"),
+ geminiRoot: os.Getenv("GEMINI_ROOT"),
+ tlsKeyFile: os.Getenv("TLS_KEY_FILE"),
+ tlsCertFile: os.Getenv("TLS_CERT_FILE"),
+ }
+}
+
+func dropPrivileges() (bool, error) {
+ me, err := user.Current()
+ if err != nil {
+ return false, err
+ }
+
+ if me.Uid != "0" {
+ return false, nil
+ }
+
+ nobody, err := user.Lookup("nobody")
+ if err != nil {
+ return false, err
+ }
+ uid, err := strconv.Atoi(nobody.Uid)
+ if err != nil {
+ return false, err
+ }
+
+ if err := syscall.Setuid(uid); err != nil {
+ return false, err
+ }
+ return true, nil
+}
+
+func serverContext() (context.Context, logging.Logger, logging.Logger, logging.Logger, logging.Logger) {
+ debug, info, warn, err := logging.DefaultLoggers()
+ ctx := signals(context.Background())
+ ctx = context.WithValue(ctx, "debuglog", debug)
+ ctx = context.WithValue(ctx, "infolog", info)
+ ctx = context.WithValue(ctx, "warnlog", warn)
+ ctx = context.WithValue(ctx, "errorlog", err)
+ return ctx, debug, info, warn, err
+}
+
+func signals(ctx context.Context) context.Context {
+ ctx, _ = signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGHUP)
+ return ctx
+}