-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/pocka/legit/config"
"github.com/pocka/legit/core"
"github.com/pocka/legit/git/exe"
"github.com/pocka/legit/pages"
)
var additionalAccessDirs string
func main() {
var cfg string
var host string
var port uint
var scanPath string
var compileTemplatesOnRequest bool
var repoDiff string
var staticDirOverride string
var templatesDirOverride string
flag.StringVar(&cfg, "config", "", "path to config file")
flag.StringVar(&host, "server.host", "", "override server.host config")
flag.UintVar(&port, "server.port", 0, "override server.port config")
flag.StringVar(&scanPath, "repo.scanPath", "", "override repo.scanPath config")
flag.StringVar(&repoDiff, "repo.diff", "", "override repo.diff config")
flag.BoolVar(&compileTemplatesOnRequest, "compileTemplatesOnRequest", false, "override compileTemplatesOnRequest config")
flag.StringVar(&staticDirOverride, "dirs.static", "", "override dirs.static config")
flag.StringVar(&templatesDirOverride, "dirs.templates", "", "override dirs.templates config")
flag.Parse()
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
c := config.NewWithDefaults()
if cfg != "" {
if err := c.ReadFromFile(cfg); err != nil {
log.Fatalf("Unable to read config file: %s", err)
}
}
if port > 0 {
c.Server.Port = port
}
if host != "" {
c.Server.Host = host
}
if scanPath != "" {
c.Repo.ScanPath = scanPath
}
if repoDiff != "" {
c.Repo.Diff = repoDiff
}
if compileTemplatesOnRequest {
c.CompileTemplatesOnRequest = true
}
if staticDirOverride != "" {
resolved, err := filepath.Abs(staticDirOverride)
if err != nil {
log.Fatalf("Cannot resolve -dirs.static")
}
c.Dirs.Static = resolved
}
if templatesDirOverride != "" {
resolved, err := filepath.Abs(templatesDirOverride)
if err != nil {
log.Fatalf("Cannot resolve -dirs.templates")
}
c.Dirs.Templates = resolved
}
if err := c.Resolve(cwd); err != nil {
log.Fatal(err)
}
fsAllowList := make([]filesystemAccess, 2, 10)
fsAllowList[0] = filesystemAccess{
path: c.Repo.ScanPath,
isDir: true,
read: true,
}
// os.exec.Cmd use /dev/null. Without this, git operations fail with
// "open /dev/null: permission denied" under Landlock. Unveil does not
// error on access to /dev/null.
// https://rohitpaulk.com/articles/cmd-run-dev-null
fsAllowList[1] = filesystemAccess{
path: "/dev/null",
read: true,
write: true,
}
if path, err := exec.LookPath(exe.GitBin()); err != nil {
log.Printf("Unable to find git binary, git operations will fail: %s", err)
} else {
fsAllowList = append(fsAllowList, filesystemAccess{
path: path,
read: true,
execute: true,
})
}
if c.Dirs.Static != "" {
fsAllowList = append(fsAllowList, filesystemAccess{
path: c.Dirs.Static,
isDir: true,
read: true,
})
}
if c.Dirs.Templates != "" {
fsAllowList = append(fsAllowList, filesystemAccess{
path: c.Dirs.Templates,
isDir: true,
read: true,
})
}
if additionalAccessDirs != "" {
for path := range strings.SplitSeq(additionalAccessDirs, ",") {
path := strings.Trim(path, " ")
fsAllowList = append(fsAllowList, filesystemAccess{
path: path,
isDir: true,
read: true,
})
}
}
if err := restrictFileAccessTo(fsAllowList...); err != nil {
log.Fatalf("Unable to restrict filesystem access: %s", err)
}
core, err := core.New(c)
if err != nil {
log.Fatal(err)
}
addr := fmt.Sprintf("%s:%d", c.Server.Host, c.Server.Port)
log.Println("starting server on", addr)
log.Fatal(http.ListenAndServe(addr, pages.New(core)))
}