sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/cmd/warehost-auth-dovecot/main.go

57 lines
1017 B
Go
Raw Normal View History

2016-11-24 23:58:03 +01:00
package main
import (
"bufio"
"os"
"os/exec"
"database/sql"
_ "github.com/lib/pq"
libpassword "dev.sum7.eu/sum7/warehost/lib/password"
)
var (
configFile string
username string
password string
execCmd string
config *Config
db *sql.DB
err error
)
func main() {
configFile = os.Args[1]
execCmd = os.Args[2]
pipe := os.NewFile(uintptr(3), "/dev/fd/3")
defer pipe.Close()
in := bufio.NewReader(pipe)
data, _ := in.ReadBytes(0)
username = string(data[:len(data)-1])
data, _ = in.ReadBytes(0)
password = string(data[:len(data)-1])
config = ReadConfigFile(configFile)
db, err = sql.Open("postgres", config.Database)
if err != nil {
os.Exit(1)
}
defer db.Close()
var realPassword string
err = db.QueryRow("select password from login where mail = $1", username).Scan(&realPassword)
if err != nil {
os.Exit(1)
}
output, _ := libpassword.Validate(realPassword, password)
if output {
exec.Command("bash", "-c", execCmd).Run()
} else {
os.Exit(1)
}
}