/
util.go
194 lines (169 loc) · 4.34 KB
/
util.go
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package config
import (
"encoding/json"
"flag"
"fmt"
"io/fs"
"os"
"os/user"
"path"
"runtime"
"strconv"
"time"
"github.com/gochan-org/gochan/pkg/gcutil"
)
const (
GC_DIR_MODE fs.FileMode = 0775
GC_FILE_MODE fs.FileMode = 0664
)
var (
uid int
gid int
)
// MissingField represents a field missing from the configuration file
type MissingField struct {
Name string
Critical bool
Description string
}
// InvalidValueError represents a GochanConfig field with a bad value
type InvalidValueError struct {
Field string
Value any
Details string
}
func (iv *InvalidValueError) Error() string {
str := fmt.Sprintf("invalid %s value: %#v", iv.Field, iv.Value)
if iv.Details != "" {
str += " - " + iv.Details
}
return str
}
// GetUser returns the IDs of the user and group gochan should be acting as
// when creating files. If they are 0, it is using the current user
func GetUser() (int, int) {
return uid, gid
}
func TakeOwnership(fp string) (err error) {
if runtime.GOOS == "windows" || fp == "" || cfg.Username == "" {
// Chown returns an error in Windows so skip it, also skip if Username isn't set
// because otherwise it'll think we want to switch to uid and gid 0 (root)
return nil
}
return os.Chown(fp, uid, gid)
}
func TakeOwnershipOfFile(f *os.File) error {
if runtime.GOOS == "windows" || f == nil || cfg.Username == "" {
// Chown returns an error in Windows so skip it, also skip if Username isn't set
// because otherwise it'll think we want to switch to uid and gid 0 (root)
return nil
}
return f.Chown(uid, gid)
}
// InitConfig loads and parses gochan.json on startup and verifies its contents
func InitConfig(versionStr string) {
cfg = defaultGochanConfig
if flag.Lookup("test.v") != nil {
// create a dummy config for testing if we're using go test
cfg = defaultGochanConfig
cfg.ListenIP = "127.0.0.1"
cfg.Port = 8080
cfg.UseFastCGI = true
cfg.testing = true
cfg.TemplateDir = "templates"
cfg.DBtype = "sqlite3"
cfg.DBhost = "./testdata/gochantest.db"
cfg.DBname = "gochan"
cfg.DBusername = "gochan"
cfg.SiteDomain = "127.0.0.1"
cfg.RandomSeed = "test"
cfg.Version = ParseVersion(versionStr)
cfg.SiteSlogan = "Gochan testing"
cfg.Verbose = true
cfg.Captcha.OnlyNeededForThreads = true
cfg.Cooldowns = BoardCooldowns{0, 0, 0}
cfg.BanColors = []string{
"admin:#0000A0",
"somemod:blue",
}
return
}
cfgPath = gcutil.FindResource(
"gochan.json",
"/usr/local/etc/gochan/gochan.json",
"/etc/gochan/gochan.json")
if cfgPath == "" {
fmt.Println("gochan.json not found")
os.Exit(1)
}
cfgBytes, err := os.ReadFile(cfgPath)
if err != nil {
fmt.Printf("Error reading %s: %s\n", cfgPath, err.Error())
os.Exit(1)
}
if err = json.Unmarshal(cfgBytes, cfg); err != nil {
fmt.Printf("Error parsing %s: %s", cfgPath, err.Error())
os.Exit(1)
}
cfg.jsonLocation = cfgPath
if err = cfg.ValidateValues(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if runtime.GOOS != "windows" {
var gcUser *user.User
if cfg.Username != "" {
gcUser, err = user.Lookup(cfg.Username)
} else {
gcUser, err = user.Current()
}
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if uid, err = strconv.Atoi(gcUser.Uid); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if gid, err = strconv.Atoi(gcUser.Gid); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
if _, err = os.Stat(cfg.DocumentRoot); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if _, err = os.Stat(cfg.TemplateDir); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if _, err = os.Stat(cfg.LogDir); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
cfg.LogDir = gcutil.FindResource(cfg.LogDir, "log", "/var/log/gochan/")
if cfg.Port == 0 {
cfg.Port = 80
}
if len(cfg.FirstPage) == 0 {
cfg.FirstPage = []string{"index.html", "1.html", "firstrun.html"}
}
if cfg.WebRoot == "" {
cfg.WebRoot = "/"
}
if cfg.WebRoot[0] != '/' {
cfg.WebRoot = "/" + cfg.WebRoot
}
if cfg.WebRoot[len(cfg.WebRoot)-1] != '/' {
cfg.WebRoot += "/"
}
_, zoneOffset := time.Now().Zone()
cfg.TimeZone = zoneOffset / 60 / 60
cfg.Version = ParseVersion(versionStr)
cfg.Version.Normalize()
}
// WebPath returns an absolute path, starting at the web root (which is "/" by default)
func WebPath(part ...string) string {
return path.Join(cfg.WebRoot, path.Join(part...))
}