add dovecot auth
This commit is contained in:
parent
10c51a2189
commit
e0c93cc4a5
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// Config of warehost webserver
|
||||
type Config struct {
|
||||
Database string `yaml:"database"`
|
||||
}
|
||||
|
||||
// ReadConfigFile reads a config models by path to a yml file
|
||||
func ReadConfigFile(path string) *Config {
|
||||
config := &Config{}
|
||||
file, _ := ioutil.ReadFile(path)
|
||||
err := yaml.Unmarshal(file, &config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return config
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable"
|
|
@ -0,0 +1,56 @@
|
|||
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)
|
||||
}
|
||||
}
|
Reference in New Issue