-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilter-auth.go
118 lines (95 loc) · 1.98 KB
/
filter-auth.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
package main
import (
"bufio"
"fmt"
"os"
"strings"
"log"
)
type session struct {
id string
rdns string
ip string
user string
}
var version string
var sessions = make(map[string]*session)
func linkConnect(s *session, params []string) {
if len(params) != 4 {
log.Fatal("invalid input, shouldn't happen")
}
s.rdns = params[0]
if strings.HasPrefix(params[2], "unix:") {
s.ip = "127.0.0.1"
} else if params[2][0] == '[' {
s.ip = strings.Split(strings.Split(params[2], "]")[0], "[")[1]
} else {
s.ip = strings.Split(params[2], ":")[0]
}
}
func linkAuth(s *session, params []string) {
if len(params) < 2 {
log.Fatal("invalid input, shouldn't happen")
}
var res string
if version < "0.7" {
res = params[len(params) - 1]
s.user = strings.Join(params[0:len(params)-1], "|")
} else {
res = params[0]
s.user = strings.Join(params[1:], "|")
}
if res != "pass" {
fmt.Fprintf(os.Stderr, "failed authentication from user=%s address=%s host=%s\n", s.user, s.ip, s.rdns)
return
}
}
func linkDisconnect(s *session, params []string) {
if len(params) != 0 {
log.Fatal("invalid input, shouldn't happen")
}
delete(sessions, s.id)
}
func main() {
input := bufio.NewScanner(os.Stdin)
reporters := map[string]func(*session, []string) {
"link-connect": linkConnect,
"link-disconnect": linkDisconnect,
"link-auth": linkAuth,
}
for {
if !input.Scan() {
os.Exit(0)
}
if input.Text() == "config|ready" {
for k := range reporters {
fmt.Printf("register|report|smtp-in|%s\n", k)
}
fmt.Println("register|ready")
break
}
}
for {
if !input.Scan() {
os.Exit(0)
}
bits := strings.Split(input.Text(), "|")
if len(bits) < 6 {
os.Exit(1)
}
version = bits[1]
if bits[0] == "report" {
if bits[4] == "link-connect" {
sessions[bits[5]] = &session{ id : bits[5] }
}
s := sessions[bits[5]]
if v, ok := reporters[bits[4]]; ok {
v(s, bits[6:])
} else {
os.Exit(1)
}
} else {
os.Exit(1)
}
}
}