diff --git a/Makefile b/Makefile index 45f4d06..6297a7c 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ PREFIX ?= /usr/local +GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=${VERSION} -X `go list`.Commit=${COMMIT}" +VERSION = `git describe --abbrev=0 --tags 2>/dev/null || echo "VERSION"` +COMMIT = `git rev-parse --short HEAD || echo "COMMIT"` build: - go build -v ./cmd/mai + go build ${GOFLAGS} ./cmd/mai clean: rm -f mai install: diff --git a/cmd/mai/flag.go b/cmd/mai/flag.go index dda2529..8e83934 100644 --- a/cmd/mai/flag.go +++ b/cmd/mai/flag.go @@ -7,5 +7,6 @@ import ( func parseFlags() { flag.StringVar(&configfile, "f", "", "Configuration file") + flag.BoolVar(&verbose, "v", false, "Verbose logging") flag.Parse() } diff --git a/cmd/mai/main.go b/cmd/mai/main.go index 82116b9..40bf633 100644 --- a/cmd/mai/main.go +++ b/cmd/mai/main.go @@ -2,13 +2,17 @@ package main import ( "bytes" + "fmt" "net/http" "net/url" "os" "time" "runtime" + "syscall" + "marisa.chaotic.ninja/mai" "marisa.chaotic.ninja/mai/engines" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/limiter" @@ -16,17 +20,22 @@ import ( ) var ( configfile string + verbose bool ) var conf struct { + group string listen string - redisuri string staticpath string tmplpath string + user string } func main() { parseFlags() if configfile != "" { + if verbose { + fmt.Printf("[mai] Reading configuration file") + } readConf(configfile) } @@ -34,6 +43,19 @@ func main() { conf.listen = "127.0.0.1:5000" conf.staticpath = "./static" conf.tmplpath = "./views" + + if conf.user != "" { + if verbose { + fmt.Printf("[mai] Dropping privileges to %s", conf.user) + } + uid, gid, err := usergroupids(conf.user, conf.group) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + syscall.Setuid(uid) + syscall.Setgid(gid) + } engine := html.New(conf.tmplpath, ".html") engine.AddFunc("inc", func(i int) int { return i + 1 }) @@ -265,4 +287,9 @@ func main() { Browse: true, }) app.Listen(conf.listen) + + if verbose { + fmt.Printf("Starting mai %v\n", mai.FullVersion()) + fmt.Printf("Listening on %s", conf.listen) + } } diff --git a/cmd/mai/readconf.go b/cmd/mai/readconf.go index 441b8e4..4fe9248 100644 --- a/cmd/mai/readconf.go +++ b/cmd/mai/readconf.go @@ -11,9 +11,11 @@ func readConf(file string) error { return err } + conf.group = cfg.Section("mai").Key("group").String() conf.listen = cfg.Section("mai").Key("listen").String() conf.staticpath = cfg.Section("mai").Key("static").String() conf.tmplpath = cfg.Section("mai").Key("templates").String() + conf.user = cfg.Section("mai").Key("user").String() return nil } diff --git a/cmd/mai/usergroupids.go b/cmd/mai/usergroupids.go new file mode 100644 index 0000000..dd4b609 --- /dev/null +++ b/cmd/mai/usergroupids.go @@ -0,0 +1,25 @@ +package main + +import ( + "os/user" + "strconv" +) + +func usergroupids(username string, groupname string) (int, int, error) { + u, err := user.Lookup(username) + if err != nil { + return -1, -1, err + } + + uid, _ := strconv.Atoi(u.Uid) + gid, _ := strconv.Atoi(u.Gid) + + if conf.group != "" { + g, err := user.LookupGroup(groupname) + if err != nil { + return uid, -1, err + } + gid, _ = strconv.Atoi(g.Gid) + } + return uid, gid, nil +} diff --git a/doc.go b/doc.go new file mode 100644 index 0000000..ec57bf0 --- /dev/null +++ b/doc.go @@ -0,0 +1,4 @@ +// Package mai is an usable frontend for popular translation engines +// Originally based on SimplyTranslate, and took inspiration from the following projects: +// Partage, PixivFE, etc. +package mai diff --git a/mai.ini.5 b/mai.ini.5 index 7f05709..0f6c194 100644 --- a/mai.ini.5 +++ b/mai.ini.5 @@ -7,6 +7,9 @@ .Sh OPTIONS .Ss [mai] section .Bl -tag -width 11n +.It group +Group ID for dropping privileges to. +If unset, assume the GID of user .It listen HTTP port for the server to listen. Default is "localhost:5000" @@ -16,6 +19,8 @@ Default is "./static" .It templates Directory where the templates are located. Default is "./views" +.It user +User ID for dropping privileges to. .El .Sh ENVIRONMENT .Bl -tag -width 11n diff --git a/version.go b/version.go new file mode 100644 index 0000000..3605571 --- /dev/null +++ b/version.go @@ -0,0 +1,18 @@ +package mai + +import ( + "fmt" +) + +var ( + // Version release version + Version = "0.0.1" + + // Commit will be overwritten automatically by the build system + Commit = "HEAD" +) + +// FullVersion display the full version and build +func FullVersion() string { + return fmt.Sprintf("%s@%s", Version, Commit) +}