-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostmapweb.go
577 lines (533 loc) · 14.9 KB
/
postmapweb.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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package main
import (
"bufio"
"bytes"
"embed"
"encoding/json"
"errors"
"flag"
"html/template"
"io"
"log"
"net/http"
"net/mail"
"os"
"os/exec"
"os/signal"
"runtime/pprof"
"strings"
"sync"
"syscall"
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/ssh/terminal"
"github.com/labstack/echo/v4"
mw "github.com/labstack/echo/v4/middleware"
)
type Domain struct {
Name string
MapFile string
PassHash string
Script string
}
type Config struct {
Domains []Domain
}
var conf Config
//go:embed templates/view.html templates/view.js templates/error.html
var assets embed.FS
//go:embed handsontable static
var staticAssets embed.FS
func View(c echo.Context) error {
domain := c.Get("domain").(Domain)
return c.Render(http.StatusOK, "view", struct {
Domain string
}{domain.Name})
}
func JS(c echo.Context) error {
domain := c.Get("domain").(Domain)
a := readMapFile(domain.MapFile, nil)
b := make([][]string, 0)
for i := 0; i < len(a); i++ {
if strings.Contains(a[i].Email, "@"+domain.Name) {
b = append(b, []string{a[i].Email, a[i].Target})
}
}
if len(b) == 0 {
b = append(b, []string{"@" + domain.Name, "nobody"})
}
tmpl := c.Echo().Renderer.(Renderer).Template("js")
var w bytes.Buffer
err := tmpl.Execute(&w, struct {
Aliases [][]string
Domain string
}{b, domain.Name})
if err != nil {
return err
}
if w.Len() < len("<script></script>") {
return errors.New("Truncated JS template output")
}
if string(w.Bytes()[:len("<script>")]) != "<script>" {
return errors.New("unexpected JS template output")
}
return c.Blob(200, "application/javascript", w.Bytes()[len("<script>"):w.Len()-len("</script>")])
}
type ChangeRequest struct {
Op string
Alias string
Target string
}
func new_line(label string, fw *bufio.Writer, email string, dest string) {
pad := 40 - len(email)
if pad <= 0 {
pad = 1
}
fw.WriteString(email + strings.Repeat(" ", pad) + dest + "\n")
//log.Println(label, "new_line", email, dest)
}
func validate(target string, local map[string]bool) bool {
if is_spam(target) {
return true
}
for _, email := range strings.Split(target, ",") {
email = strings.TrimSpace(email)
// RFC 5322 parsing/validation of email addresses, accept no inferior
// regex-based substitute
_, err := mail.ParseAddress(email)
switch {
case err == nil: // valid RFC 5322 address
continue
case local[email]: // valid pre-existing local alias
continue
default:
aliases := make([]string, len(local))
i := 0
for alias := range local {
aliases[i] = alias
i++
}
log.Println("attempted alias target", email, "is neither a valid email nor one of the recognized aliases:", strings.Join(aliases, ", "))
return false
}
}
return true
}
var rewrite_lock sync.Mutex
func is_spam(e string) bool {
return e == "spam" || e == "SPAM" || strings.HasPrefix(e, "550 ")
}
func Change(c echo.Context) error {
var changes []ChangeRequest
domain := c.Get("domain").(Domain)
if c.FormValue("changes") == "" && c.FormValue("user") != "" && c.FormValue("dest") != "" {
// quick entry form
changes = []ChangeRequest{
{
"add",
strings.TrimSpace(c.FormValue("user")) + "@" + domain.Name,
strings.TrimSpace(c.FormValue("dest")),
},
}
} else {
err := json.Unmarshal([]byte(c.FormValue("changes")), &changes)
if err != nil {
return err
}
}
log.Println("Received changes", changes)
// serialize map file rewrites
rewrite_lock.Lock()
defer func() {
rewrite_lock.Unlock()
}()
// find all existing local addresses, but exclude command delivery
a := readMapFile(domain.MapFile, nil)
local := make(map[string]bool)
for i := 0; i < len(a); i++ {
if strings.Contains(a[i].Email, "@"+domain.Name) && !strings.ContainsAny(a[i].Target, "@|") {
for _, dest := range strings.Split(a[i].Target, ",") {
local[strings.TrimSpace(dest)] = true
}
}
}
// dedupe and normalize changes
remap := make(map[string]string)
for _, change := range changes {
address, err := mail.ParseAddress(change.Alias)
if err != nil || !strings.HasSuffix(address.Address, "@"+domain.Name) {
if change.Alias == "@"+domain.Name {
address = &mail.Address{Address: change.Alias}
} else {
log.Println("invalid alias:", change.Alias)
return c.Render(http.StatusBadRequest, "error", struct {
Error string
}{"invalid alias: " + change.Alias})
}
}
switch change.Op {
case "remove":
remap[address.Address] = ""
case "add":
if validate(change.Target, local) {
remap[address.Address] = change.Target
} else {
return c.Render(http.StatusBadRequest, "error", struct {
Error string
}{"invalid email address list for " + address.Address + ": " + change.Target})
}
default:
log.Fatal("unexpected change request: ", change)
}
}
// rewrite the map file atomically
tmp_file := domain.MapFile + ".web.new"
f, err := os.OpenFile(tmp_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal("could not rewrite map file ", tmp_file, " due to ", err)
}
fw := bufio.NewWriter(f)
// recipient of "spam" goes to its own file
spam, err := os.OpenFile(tmp_file+".spam", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal("could not rewrite spam map file ", tmp_file+".spam", " due to ", err)
}
sw := bufio.NewWriter(spam)
//log.Println("Preparing to rewrite map files")
readMapFile(domain.MapFile, func(line string, email string) {
//log.Printf("RLMF \"%s\" \"%s\" \"%s\"\n", line, email, remap[email])
if email == "" {
fw.WriteString(line + "\n")
} else {
dest, ok := remap[email]
if ok {
switch {
case dest == "":
break
case dest == "spam":
new_line("SPAM", sw, email, "550 Stop spamming me")
case dest == "SPAM":
new_line("SPAM", sw, email, "550 Stop spamming me")
case strings.HasPrefix(dest, "550 "):
new_line("SPAM", sw, email, dest)
default:
new_line("GOOD", fw, email, dest)
}
// remove any further references
remap[email] = ""
} else {
fields := strings.Fields(line)
dest = strings.Join(fields[1:len(fields)], " ")
if is_spam(dest) {
sw.WriteString(line + "\n")
} else {
fw.WriteString(line + "\n")
}
}
}
})
for email, dest := range remap {
if dest != "" {
if is_spam(dest) {
if !strings.HasPrefix(dest, "550 ") {
dest = "550 Stop spamming me"
}
new_line("SPAM", sw, email, dest)
} else {
new_line("GOOD", fw, email, dest)
}
}
}
fw.Flush()
sw.Flush()
f.Close()
spam.Close()
err = os.Rename(domain.MapFile, domain.MapFile+".old")
if err != nil {
return err
}
err = os.Rename(domain.MapFile+".spam", domain.MapFile+".spam.old")
err = os.Rename(tmp_file, domain.MapFile)
if err != nil {
os.Rename(domain.MapFile+".old", domain.MapFile)
return err
}
err = os.Rename(tmp_file+".spam", domain.MapFile+".spam")
if err != nil {
os.Rename(domain.MapFile+".spam.old", domain.MapFile+".spam")
return err
}
cmd := exec.Command("postmap", domain.MapFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println("error running postmap on map file:", err)
os.Rename(domain.MapFile, domain.MapFile+".bad")
os.Rename(domain.MapFile+".old", domain.MapFile)
return err
}
cmd = exec.Command("postmap", domain.MapFile+".spam")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println("error running postmap on spam file:", err)
os.Rename(domain.MapFile+".spam", domain.MapFile+".spam.bad")
os.Rename(domain.MapFile+".spam.old", domain.MapFile+".spam")
}
cmd = exec.Command("postfix", "reload")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println("postfix reload failed:", err)
}
// optional script hook
if domain.Script != "" {
cmd = exec.Command(domain.Script, domain.Name)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Println("script", domain.Script, "failed:", err)
}
}
// HTTP 303 is specifically for POST/Redirect/GET
// see: https://en.wikipedia.org/wiki/Post/Redirect/Get
c.Response().Header().Set("Location", "/")
return c.HTML(303, "<script>document.location.href = \"/\";</script>")
}
func readConf(conf_file string) Config {
conf_stat, err := os.Stat(conf_file)
if err != nil {
log.Println("could not stat conf file", conf_file, "due to", err, "- assuming an empty config file")
return Config{}
}
conf_size := int(conf_stat.Size())
f, err := os.Open(conf_file)
if err != nil {
log.Fatal("could not open conf file ", conf_file, " due to ", err)
}
conf_data := make([]byte, conf_size)
bytes_read, err := f.Read(conf_data)
if err != nil || bytes_read < conf_size {
log.Fatal("could not read conf file ", conf_file, " error: ", err, " read ", bytes_read, " bytes ")
}
var conf Config
err = json.Unmarshal(conf_data, &conf)
if err != nil || bytes_read < conf_size {
log.Fatal("could not decode conf file ", conf_file, " due to ", err)
}
if *verbose {
log.Println("config file:", conf)
}
return conf
}
func updateConf(conf Config, conf_file string, domain string, virtual string, password []byte) {
pass_hash, err := bcrypt.GenerateFromPassword(password, bcrypt.DefaultCost)
if err != nil {
log.Fatal("Could not hash the password: ", err)
}
// if the domain already exists in the config file,
// change the password and map file
existing := false
for i, d := range conf.Domains {
if d.Name == domain {
existing = true
conf.Domains[i].MapFile = virtual
conf.Domains[i].PassHash = string(pass_hash)
}
}
if !existing {
conf.Domains = append(conf.Domains, Domain{domain, virtual, string(pass_hash), ""})
}
conf_json, err := json.MarshalIndent(conf, "", " ")
if err != nil {
log.Fatal("Could not marshal the config: ", err)
}
f, err := os.OpenFile(conf_file, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
log.Fatal("could not write conf file ", conf_file, " due to ", err)
}
nbytes, err := f.Write(conf_json)
if err != nil || nbytes < len(conf_json) {
log.Fatal("could not write conf file ", conf_file, " due to ", err)
}
err = f.Close()
if err != nil {
log.Fatal("could not finish writing conf file ", conf_file, " due to ", err)
}
}
type Alias struct {
Email string
Target string
}
func readMapFile(map_file string, hook func(line string, email string)) []Alias {
good, err := readSingleMapFile(map_file, hook)
if err != nil {
log.Fatal("could not open map file ", map_file, " due to ", err)
}
spam, err := readSingleMapFile(map_file+".spam", hook)
if err == nil {
good = append(good, spam...)
}
return good
}
func readSingleMapFile(map_file string, hook func(line string, email string)) ([]Alias, error) {
f, err := os.Open(map_file)
if err != nil {
return nil, err
}
aliases := make([]Alias, 0)
scanner := bufio.NewScanner(bufio.NewReader(f))
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") {
if hook != nil {
hook(line, "")
}
continue
}
fields := strings.Fields(line)
if strings.HasSuffix(map_file, ".spam") {
aliases = append(aliases, Alias{fields[0], strings.Join(fields[1:len(fields)], " ")})
if hook != nil {
hook(line, fields[0])
}
} else {
// there could be a comment after the map entry
if len(fields) >= 2 && strings.ContainsRune(fields[0], '@') {
if strings.ContainsRune(fields[0], '@') {
aliases = append(aliases, Alias{fields[0], fields[1]})
}
if hook != nil {
hook(line, fields[0])
}
} else {
if hook != nil {
hook(line, "")
}
}
}
}
return aliases, nil
}
type Renderer struct {
templates map[string]*template.Template
}
func (r *Renderer) Load() {
r.templates = map[string]*template.Template{
"view": r.parse("view.html", "view"),
"js": r.parse("view.js", "js"),
"error": r.parse("error.html", "error"),
}
}
func (r Renderer) Template(name string) *template.Template {
return r.templates[name]
}
func (r *Renderer) parse(filename string, name string) *template.Template {
templateBytes, err := assets.ReadFile("templates/" + filename)
if err != nil {
log.Fatal(err)
}
templateString := string(templateBytes)
if strings.HasSuffix(filename, ".js") {
// recipe from: https://damienradtke.com/post/go-js-template/
templateString = "<script>" + templateString + "</script>"
}
tmpl, err := template.New(name).Parse(templateString)
if err != nil {
log.Fatal(err)
}
return tmpl
}
func (r Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
tmpl := r.templates[name]
return tmpl.Execute(w, data)
}
var verbose *bool
func main() {
// command-line args parsing
conf_file := flag.String("c", "/etc/postfix/postmapweb.json", "config file to use")
verbose = flag.Bool("v", false, "verbose logging")
domain := flag.String("d", "", "add domain user")
virtual := flag.String("m", "/etc/postfix/virtual", "virtual domain map to use with -d")
cl_password := flag.String("w", "", "password to use with -d (insecure!)")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to file")
port := flag.String("p", "localhost:8080", "host address and port to bind to")
flag.Parse()
conf = readConf(*conf_file)
if *domain != "" {
password := []byte(*cl_password)
if len(password) == 0 {
os.Stdout.Write([]byte("Enter password for " + *domain + ":"))
password1, err := terminal.ReadPassword(0)
if err != nil {
log.Fatal("password error: ", err)
}
os.Stdout.Write([]byte("\nConfirm password:"))
password2, err := terminal.ReadPassword(0)
os.Stdout.Write([]byte("\n"))
if err != nil {
log.Fatal("password error: ", err)
}
if string(password1) != string(password2) {
log.Fatal("the passwords do not match")
}
password = password1
}
//log.Println("read password:", "\"" + string(password) + "\"")
updateConf(conf, *conf_file, *domain, *virtual, password)
log.Println("updated conf", *conf_file)
return
}
// profiler
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
log.Println("starting CPU profile")
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP)
go func() {
s := <-sigChan
log.Println("stopping CPU profile due to", s)
pprof.StopCPUProfile()
}()
}
// serve go:embed embedded assets
assetHandler := http.FileServer(http.FS(staticAssets))
// Echo instance
e := echo.New()
e.HideBanner = true
r := Renderer{}
r.Load()
e.Renderer = r
// Middleware
if *verbose {
e.Use(mw.Logger())
}
e.Use(mw.Recover())
e.Use(SecurityHeaders)
e.Use(BasicAuth)
//e.Use(mw.Gzip())
//e.Favicon("img/favicon.ico")
// embedded static assets
e.GET("/handsontable/*", func(c echo.Context) error {
assetHandler.ServeHTTP(c.Response().Writer, c.Request())
return nil
})
e.GET("/static/*", func(c echo.Context) error {
assetHandler.ServeHTTP(c.Response().Writer, c.Request())
return nil
})
e.GET("/", View)
e.GET("/view.js", JS)
e.POST("/", Change)
// Start server
log.Println("starting postmapweb on", *port)
e.Start(*port)
}