-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathdovecot_sasl_test.go
206 lines (169 loc) · 4.62 KB
/
dovecot_sasl_test.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
//go:build integration && (darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris)
// +build integration
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
/*
Maddy Mail Server - Composable all-in-one email server.
Copyright © 2019-2020 Max Mazurov <[email protected]>, Maddy Mail Server contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// only posix systems ^
package tests_test
import (
"bufio"
"errors"
"flag"
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
"syscall"
"testing"
"time"
"github.com/foxcpp/maddy/tests"
)
var DovecotExecutable string
func init() {
flag.StringVar(&DovecotExecutable, "integration.dovecot", "dovecot", "path to dovecot executable for interop tests")
}
const dovecotConf = `base_dir = $ROOT/run/
state_dir = $ROOT/lib/
log_path = /dev/stderr
ssl = no
default_internal_user = $USER
default_internal_group = $GROUP
default_login_user = $USER
passdb {
driver = passwd-file
args = $ROOT/passwd
}
userdb {
driver = passwd-file
args = $ROOT/passwd
}
service auth {
unix_listener auth {
mode = 0666
}
}
# Dovecot refuses to start without protocols, so we need to give it one.
protocols = imap
service imap-login {
chroot =
inet_listener imap {
address = 127.0.0.1
port = 0
}
}
service anvil {
chroot =
}
# Turn on debugging information, to help troubleshooting issues.
auth_verbose = yes
auth_debug = yes
auth_debug_passwords = yes
auth_verbose_passwords = yes
mail_debug = yes
`
const dovecotPasswd = `tester:{plain}123456:1000:1000::/home/user`
func runDovecot(t *testing.T) (string, *exec.Cmd) {
dovecotExec, err := exec.LookPath(DovecotExecutable)
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
t.Skip("No Dovecot executable found, skipping interop. tests")
}
t.Fatal(err)
}
tempDir := t.TempDir()
curUser, err := user.Current()
if err != nil {
t.Fatal(err)
}
curGroup, err := user.LookupGroupId(curUser.Gid)
if err != nil {
t.Fatal(err)
}
dovecotConf := strings.NewReplacer(
"$ROOT", tempDir,
"$USER", curUser.Username,
"$GROUP", curGroup.Name).Replace(dovecotConf)
err = ioutil.WriteFile(filepath.Join(tempDir, "dovecot.conf"), []byte(dovecotConf), os.ModePerm)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(filepath.Join(tempDir, "passwd"), []byte(dovecotPasswd), os.ModePerm)
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(dovecotExec, "-F", "-c", filepath.Join(tempDir, "dovecot.conf"))
stderr, err := cmd.StderrPipe()
if err != nil {
t.Fatal(err)
}
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
ready := make(chan struct{}, 1)
go func() {
scnr := bufio.NewScanner(stderr)
for scnr.Scan() {
line := scnr.Text()
// One of messages printed near completing initialization.
if strings.Contains(line, "starting up for imap") {
time.Sleep(500*time.Millisecond)
ready <- struct{}{}
}
t.Log("dovecot:", line)
}
if err := scnr.Err(); err != nil {
t.Log("stderr I/O error:", err)
}
}()
<-ready
return tempDir, cmd
}
func cleanDovecot(t *testing.T, tempDir string, cmd *exec.Cmd) {
cmd.Process.Signal(syscall.SIGTERM)
if !t.Failed() {
os.RemoveAll(tempDir)
} else {
t.Log("Dovecot directory is not deleted:", tempDir)
}
}
func TestDovecotSASLClient(tt *testing.T) {
tt.Parallel()
dovecotDir, cmd := runDovecot(tt)
defer cleanDovecot(tt, dovecotDir, cmd)
t := tests.NewT(tt)
t.DNS(nil)
t.Port("smtp")
t.Env("DOVECOT_SASL_SOCK=" + filepath.Join(dovecotDir, "run", "auth-client"))
t.Config(`
smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {
hostname mx.maddy.test
tls off
auth dovecot_sasl unix://{env:DOVECOT_SASL_SOCK}
deliver_to dummy
}`)
t.Run(1)
defer t.Close()
c := t.Conn("smtp")
defer c.Close()
c.SMTPNegotation("localhost", nil, nil)
c.Writeln("AUTH PLAIN AHRlc3QAMTIzNDU2") // 0x00 test 0x00 123456 (invalid user)
c.ExpectPattern("535 *")
c.Writeln("AUTH PLAIN AHRlc3RlcgAxMjM0NQ==") // 0x00 tester 0x00 12345 (invalid password)
c.ExpectPattern("535 *")
c.Writeln("AUTH PLAIN AHRlc3RlcgAxMjM0NTY=") // 0x00 tester 0x00 123456
c.ExpectPattern("235 *")
}