-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrcpt-verify.go
189 lines (163 loc) · 4.01 KB
/
rcpt-verify.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
package main
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import "fmt"
import "io/ioutil"
import "log"
import "os"
import "strings"
const configdir = "/etc/qbox"
func main() {
// No SMTPRCPTTO, we can't do anything
if !env_defined("SMTPRCPTTO") {
fmt.Println()
os.Exit(0)
}
// Relayclients send to remote, non-checkable addresses
if env_defined("RELAYCLIENT") {
fmt.Println()
os.Exit(0)
}
// Split recipient in user and domain part
var smtprcptto string = os.Getenv("SMTPRCPTTO")
addrparts := strings.Split(smtprcptto, "@")
if len(addrparts) < 2 {
fmt.Println("E451 Invalid address")
os.Exit(0)
}
user, domain := addrparts[0], addrparts[1]
// 20220403: Support extensions
user = remove_extension(user)
_ = verify_recipient(smtprcptto, user, domain)
os.Exit(0)
}
func verify_recipient (smtprcptto string, user string, domain string) (bool) {
// Read config files
var dbserver string = "127.0.0.1"
if fileExists(configdir + "/dbserver") {
buf, err := ioutil.ReadFile(configdir + "/dbserver")
if err == nil {
dbserver = string(buf)
}
}
var dbuser string = "qbox"
if fileExists(configdir + "/dbuser") {
buf, err := ioutil.ReadFile(configdir + "/dbuser")
if err == nil {
dbuser = string(buf)
}
}
var dbpass string
if fileExists(configdir + "/dbpass") {
buf, err := ioutil.ReadFile(configdir + "/dbpass")
if err == nil {
dbpass = string(buf)
}
}
// Initialize DB
db, err := sql.Open("mysql", dbuser+":"+dbpass+"@tcp("+dbserver+")/qbox")
if err == nil {
err = db.Ping()
if err != nil {
internal_error()
log.Fatal(err)
}
} else {
internal_error()
log.Fatal(err)
}
defer db.Close()
// Query DB for domain rewrite
var rewrite string
stmt1, err := db.Prepare("SELECT rewrite FROM domains WHERE domain = ? AND rewrite != ''")
if err != nil {
internal_error()
log.Fatal(err)
}
rows1, err := stmt1.Query(domain)
if err != nil {
internal_error()
log.Fatal(err)
}
defer stmt1.Close()
for rows1.Next() {
err := rows1.Scan(&rewrite)
if err != nil {
internal_error()
log.Fatal(err)
}
}
if len(rewrite) > 0 {
fmt.Fprintf(os.Stderr, "%d Domain %s is rewritten to %s for %s\n", os.Getppid(), domain, rewrite, smtprcptto)
domain = rewrite
}
// Query DB for user
stmt2, err := db.Prepare("SELECT DISTINCT COUNT(passwd.homedir) FROM passwd INNER JOIN mapping ON passwd.uid = mapping.uid WHERE domain = ? AND (user = ? OR user = '*')")
if err != nil {
internal_error()
log.Fatal(err)
}
defer stmt2.Close()
var ucount int
rows2, err := stmt2.Query(domain, user)
if err != nil {
internal_error()
log.Fatal(err)
}
for rows2.Next() {
err := rows2.Scan(&ucount)
if err != nil {
internal_error()
log.Fatal(err)
}
}
if ucount > 0 {
// Recipient found
fmt.Fprintf(os.Stderr, "%d Found mapping for %s\n", os.Getppid(), smtprcptto)
fmt.Println()
return true
}
// The user was not found, check if the domain is even in the system
stmt3, err := db.Prepare("SELECT COUNT(domain) FROM domains WHERE domain = ?")
if err != nil {
internal_error()
log.Fatal(err)
}
var dcount int
err = stmt3.QueryRow(domain).Scan(&dcount)
if err != nil {
internal_error()
log.Fatal(err)
}
if dcount > 0 {
fmt.Fprintf(os.Stderr, "%d User %s not found in database\n", os.Getppid(), smtprcptto)
fmt.Fprintf(os.Stdout, "E550 User unknown [%s]\n", smtprcptto)
} else {
fmt.Fprintf(os.Stderr, "%d Domain %s not found in table DOMAINS\n", os.Getppid(), domain)
fmt.Fprintf(os.Stdout, "E521 Domain unknown [%s]\n", domain)
}
// os.Exit(0)
return false
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func env_defined(key string) bool {
value, exists := os.LookupEnv(key)
_ = value
return exists
}
func internal_error() {
fmt.Println("E451 Recipient verification falied")
}
func remove_extension (s string) (string) {
// https://stackoverflow.com/a/29581738
if idx := strings.Index(s, "+"); idx != -1 {
return s[:idx]
}
return s
}