mail-gateway
diff --git a/apps/maddy/auth/.gitignore b/apps/maddy/auth/.gitignore
new file mode 100644
index 0000000..cd8d050
--- /dev/null
+++ b/apps/maddy/auth/.gitignore
@@ -0,0 +1 @@
+auth-smtp
diff --git a/apps/maddy/auth/Dockerfile b/apps/maddy/auth/Dockerfile
new file mode 100644
index 0000000..3b1168d
--- /dev/null
+++ b/apps/maddy/auth/Dockerfile
@@ -0,0 +1,3 @@
+FROM giolekva/maddy:v0.4.4
+
+COPY auth-smtp /usr/bin/auth-smtp
diff --git a/apps/maddy/auth/Makefile b/apps/maddy/auth/Makefile
new file mode 100644
index 0000000..7bd9ea3
--- /dev/null
+++ b/apps/maddy/auth/Makefile
@@ -0,0 +1,17 @@
+clean:
+	rm -f auth-smtp
+
+build: clean
+	go build -o auth-smtp *.go
+
+image: build
+	docker build --tag=giolekva/maddy-auth-smtp:v0.4.4 . --platform=linux/arm64
+
+push: image
+	docker push giolekva/maddy-auth-smtp:v0.4.4
+
+push_arm64: export GOOS=linux
+push_arm64: export GOARCH=arm64
+push_arm64: export CGO_ENABLED=0
+push_arm64: export GO111MODULE=on
+push_arm64: push
diff --git a/apps/maddy/auth/go.mod b/apps/maddy/auth/go.mod
new file mode 100644
index 0000000..820d651
--- /dev/null
+++ b/apps/maddy/auth/go.mod
@@ -0,0 +1,8 @@
+module github.com/giolekva/pcloud/apps/maddy/auth
+
+go 1.16
+
+require (
+	github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac
+	github.com/emersion/go-smtp v0.15.0
+)
diff --git a/apps/maddy/auth/go.sum b/apps/maddy/auth/go.sum
new file mode 100644
index 0000000..7d3f0d2
--- /dev/null
+++ b/apps/maddy/auth/go.sum
@@ -0,0 +1,5 @@
+github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
+github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac h1:tn/OQ2PmwQ0XFVgAHfjlLyqMewry25Rz7jWnVoh4Ggs=
+github.com/emersion/go-sasl v0.0.0-20211008083017-0b9dcfb154ac/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
+github.com/emersion/go-smtp v0.15.0 h1:3+hMGMGrqP/lqd7qoxZc1hTU8LY8gHV9RFGWlqSDmP8=
+github.com/emersion/go-smtp v0.15.0/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
diff --git a/apps/maddy/auth/main.go b/apps/maddy/auth/main.go
new file mode 100644
index 0000000..ab71fdb
--- /dev/null
+++ b/apps/maddy/auth/main.go
@@ -0,0 +1,55 @@
+package main
+
+import (
+	"bufio"
+	"crypto/tls"
+	"fmt"
+	"os"
+
+	"github.com/emersion/go-sasl"
+	"github.com/emersion/go-smtp"
+)
+
+var smtpServers = []string{
+	"maddy.app-maddy.svc.cluster.local:587",
+	"maddy.shveli-app-maddy.svc.cluster.local:587",
+}
+
+func auth(server, username, password string) (bool, error) {
+	c, err := smtp.Dial(server)
+	if err != nil {
+		return false, err
+	}
+	if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
+		return false, err
+	}
+	if err := c.Auth(sasl.NewPlainClient(username, username, password)); err != nil {
+		return false, err
+	}
+	return true, nil
+}
+
+func main() {
+	inp := bufio.NewReader(os.Stdin)
+	username, err := inp.ReadString('\n')
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Could not read username")
+		os.Exit(2)
+	}
+	username = username[:len(username)-1]
+	password, err := inp.ReadString('\n')
+	if err != nil {
+		fmt.Fprintln(os.Stderr, "Could not read password")
+		os.Exit(2)
+	}
+	password = password[:len(password)-1]
+	for _, s := range smtpServers {
+		if ok, _ := auth(s, username, password); ok {
+			os.Exit(0)
+			// } else if err != nil {
+			// 	fmt.Println(os.Stderr, err.Error())
+			// 	os.Exit(2)
+		}
+	}
+	os.Exit(1)
+}