File: config.go

package info (click to toggle)
molly-brown 0.0~git20230820.2068c3b-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: sh: 67; makefile: 4
file content (243 lines) | stat: -rw-r--r-- 6,410 bytes parent folder | download
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main

import (
	"errors"
	"github.com/BurntSushi/toml"
	"log"
	"os"
	"path/filepath"
	"strings"
)

type SysConfig struct {
	Port                  int
	Hostname              string
	CertPath              string
	KeyPath               string
	AccessLog             string
	ErrorLog              string
	DocBase               string
	HomeDocBase           string
	CGIPaths              []string
	SCGIPaths             map[string]string
	ReadMollyFiles        bool
	AllowTLS12            bool
	RateLimitEnable       bool
	RateLimitAverage      int
	RateLimitSoft         int
	RateLimitHard         int
}

type UserConfig struct {
	GeminiExt             string
	DefaultLang           string
	DefaultEncoding       string
	TempRedirects         map[string]string
	PermRedirects         map[string]string
	MimeOverrides         map[string]string
	CertificateZones      map[string][]string
	DirectoryListing      bool
	DirectorySort         string
	DirectorySubdirsFirst bool
	DirectoryReverse      bool
	DirectoryTitles       bool
}

func getConfig(filename string) (SysConfig, UserConfig, error) {

	var sysConfig SysConfig
	var userConfig UserConfig

	// Defaults
	sysConfig.Port = 1965
	sysConfig.Hostname = "localhost"
	sysConfig.CertPath = "cert.pem"
	sysConfig.KeyPath = "key.pem"
	sysConfig.AccessLog = "access.log"
	sysConfig.ErrorLog = ""
	sysConfig.DocBase = "/var/gemini/"
	sysConfig.HomeDocBase = "users"
	sysConfig.CGIPaths = make([]string, 0)
	sysConfig.SCGIPaths = make(map[string]string)
	sysConfig.ReadMollyFiles = false
	sysConfig.AllowTLS12 = true
	sysConfig.RateLimitEnable = false
	sysConfig.RateLimitAverage = 1
	sysConfig.RateLimitSoft = 10
	sysConfig.RateLimitHard = 50

	userConfig.GeminiExt = "gmi"
	userConfig.DefaultLang = ""
	userConfig.DefaultEncoding = ""
	userConfig.TempRedirects = make(map[string]string)
	userConfig.PermRedirects = make(map[string]string)
	userConfig.DirectoryListing = true
	userConfig.DirectorySort = "Name"
	userConfig.DirectorySubdirsFirst = false

	// Return defaults if no filename given
	if filename == "" {
		return sysConfig, userConfig, nil
	}

	// Attempt to overwrite defaults from file
	sysConfig, err := readSysConfig(filename, sysConfig)
	if err != nil {
		return sysConfig, userConfig, err
	}
	userConfig, err = readUserConfig(filename, userConfig, true)
	if err != nil {
		return sysConfig, userConfig, err
	}
	return sysConfig, userConfig, nil
}

func readSysConfig(filename string, config SysConfig) (SysConfig, error) {

	_, err := toml.DecodeFile(filename, &config)
	if err != nil {
		return config, err
	}

	// Force hostname to lowercase
	config.Hostname = strings.ToLower(config.Hostname)

	// Absolutise paths
	config.DocBase, err = filepath.Abs(config.DocBase)
	if err != nil {
		return config, err
	}
	config.CertPath, err = filepath.Abs(config.CertPath)
	if err != nil {
		return config, err
	}
	config.KeyPath, err = filepath.Abs(config.KeyPath)
	if err != nil {
		return config, err
	}
	if config.AccessLog != "" && config.AccessLog != "-" {
		config.AccessLog, err = filepath.Abs(config.AccessLog)
		if err != nil {
			return config, err
		}
	}
	if config.ErrorLog != "" {
		config.ErrorLog, err = filepath.Abs(config.ErrorLog)
		if err != nil {
			return config, err
		}
	}

	// Absolutise CGI paths
	for index, cgiPath := range config.CGIPaths {
		if !filepath.IsAbs(cgiPath) {
			config.CGIPaths[index] = filepath.Join(config.DocBase, cgiPath)
		}
	}

	// Expand CGI paths
	var cgiPaths []string
	for _, cgiPath := range config.CGIPaths {
		expandedPaths, err := filepath.Glob(cgiPath)
		if err != nil {
			return config, errors.New("Error expanding CGI path glob " + cgiPath + ": " + err.Error())
		}
		cgiPaths = append(cgiPaths, expandedPaths...)
	}
	config.CGIPaths = cgiPaths

	// Absolutise SCGI paths
	for index, scgiPath := range config.SCGIPaths {
		config.SCGIPaths[index], err = filepath.Abs( scgiPath)
		if err != nil {
			return config, err
		}
	}

	return config, nil
}

func readUserConfig(filename string, config UserConfig, requireValid bool) (UserConfig, error) {

	_, err := toml.DecodeFile(filename, &config)
	if err != nil {
		return config, err
	}

	// Validate pseudo-enums
	if requireValid {
		switch config.DirectorySort {
			case "Name", "Size", "Time":
			default:
				return config, errors.New("Invalid DirectorySort value.")
		}
	}

	// Validate redirects
	for key, value := range config.TempRedirects {
		if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
			if requireValid {
				return config, errors.New("Invalid cross-protocol redirect to " + value)
			} else {
				log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + filename)
				delete(config.TempRedirects, key)
			}
		}
	}
	for key, value := range config.PermRedirects {
		if strings.Contains(value, "://") && !strings.HasPrefix(value, "gemini://") {
			if requireValid {
				return config, errors.New("Invalid cross-protocol redirect to " + value)
			} else {
				log.Println("Ignoring cross-protocol redirect to " + value + " in .molly file " + filename)
				delete(config.PermRedirects, key)
			}
		}
	}

	return config, nil
}

func parseMollyFiles(path string, docBase string, config UserConfig) UserConfig {
	// Replace config variables which use pointers with new ones,
	// so that changes made here aren't reflected everywhere.
	config.TempRedirects = make(map[string]string)
	config.PermRedirects = make(map[string]string)
	config.MimeOverrides = make(map[string]string)
	config.CertificateZones = make(map[string][]string)

	// Build list of directories to check
	var dirs []string
	dirs = append(dirs, path)
	for {
		if path == filepath.Clean(docBase) {
			break
		}
		subpath := filepath.Dir(path)
		dirs = append(dirs, subpath)
		path = subpath
	}
	// Parse files in reverse order
	for i := len(dirs) - 1; i >= 0; i-- {
		dir := dirs[i]
		// Break out of the loop if a directory doesn't exist
		_, err := os.Stat(dir)
		if os.IsNotExist(err) {
			break
		}
		// Construct path for a .molly file in this dir
		mollyPath := filepath.Join(dir, ".molly")
		_, err = os.Stat(mollyPath)
		if err != nil {
			continue
		}
		// If the file exists and we can read it, try to parse it
		config, err = readUserConfig(mollyPath, config, false)
		if err != nil {
			log.Println("Error parsing .molly file " + mollyPath + ": " + err.Error())
			continue
		}
	}

	return config
}