From 3a3af67885b160f3b9f029473caa2415fca6f990 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Fri, 2 Sep 2016 21:32:56 +0200 Subject: [PATCH] first pure-ftp-auth + db of host --- cmd/warehost-host/pure-ftp-auth/config.go | 32 +++++++++ .../pure-ftp-auth/config.yml.example | 8 +++ cmd/warehost-host/pure-ftp-auth/main.go | 69 +++++++++++++++++++ cmd/warehost-web/config.go | 1 + cmd/warehost/config.yml.example | 2 +- config/config.go | 3 +- lib/log/main.go | 5 ++ modul/host/models.go | 66 +++++++++++++++++- 8 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 cmd/warehost-host/pure-ftp-auth/config.go create mode 100644 cmd/warehost-host/pure-ftp-auth/config.yml.example create mode 100644 cmd/warehost-host/pure-ftp-auth/main.go diff --git a/cmd/warehost-host/pure-ftp-auth/config.go b/cmd/warehost-host/pure-ftp-auth/config.go new file mode 100644 index 0000000..e977cc6 --- /dev/null +++ b/cmd/warehost-host/pure-ftp-auth/config.go @@ -0,0 +1,32 @@ +package main + +import ( + "io/ioutil" + "log" + + "gopkg.in/yaml.v2" +) + +// Config of pure-ftp-auth +type Config struct { + Path string `yaml:"path"` + Userid string `yaml:"uid"` + Groupid string `yaml:"gid"` + Quote string `yaml:"quote"` + Database string `yaml:"database"` + Log struct { + Path string `yaml:"path"` + } `yaml:"log"` + DatabaseDebug bool `yaml:"databasedebug"` +} + +// 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 +} diff --git a/cmd/warehost-host/pure-ftp-auth/config.yml.example b/cmd/warehost-host/pure-ftp-auth/config.yml.example new file mode 100644 index 0000000..f3d9f88 --- /dev/null +++ b/cmd/warehost-host/pure-ftp-auth/config.yml.example @@ -0,0 +1,8 @@ +--- +uid: 33 +gid: 33 +path: ../../ftp/ +database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable" +log: + path: test.log +databasedebug: false diff --git a/cmd/warehost-host/pure-ftp-auth/main.go b/cmd/warehost-host/pure-ftp-auth/main.go new file mode 100644 index 0000000..69d6b92 --- /dev/null +++ b/cmd/warehost-host/pure-ftp-auth/main.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "os" + "path" + + "github.com/jinzhu/gorm" + _ "github.com/jinzhu/gorm/dialects/postgres" + + log "dev.sum7.de/sum7/warehost/lib/log" + libpassword "dev.sum7.de/sum7/warehost/lib/password" + system "dev.sum7.de/sum7/warehost/system" +) + +var ( + configFile string + config *Config + dbconnection *gorm.DB +) + +func main() { + var err error + configFile = os.Getenv("CONFIGFILE") + if len(configFile) > 0 { + configFile = "config.yml" + } + + config = ReadConfigFile(configFile) + log.NewSilenceLogger(config.Log.Path) + + // Main Databaseconnection + dbconnection, err = gorm.Open("postgres", config.Database) + if err != nil { + log.Log.Fatal("database connection: ", err) + } + defer dbconnection.Close() + dbconnection.SingularTable(true) + dbconnection.LogMode(config.DatabaseDebug) + + username := os.Getenv("AUTHD_ACCOUNT") + password := os.Getenv("AUTHD_PASSWORD") + logger := log.Log.WithField("user", username) + login := system.Login{Username: username} + dbconnection.Where("mail = ?", username).First(&login) + if login.ID <= 0 { + logger.Warn("user not found") + fmt.Println("auth_ok:-1") + } else if login.Active { + output, _ := libpassword.Validate(login.Password, password) + if output { + logger.Info("logged in") + fmt.Println("auth_ok:-1") + fmt.Printf("uid:%s\n", config.Userid) + fmt.Printf("gid:%s\n", config.Groupid) + fmt.Printf("dir:%s\n", path.Join(config.Path, fmt.Sprintf("%d", login.ID))) + if len(config.Quote) > 0 && !login.Superadmin { + fmt.Printf("user_quote_size:%s\n", config.Quote) + } + } else { + logger.Warn("wrong password") + fmt.Println("auth_ok:-1") + } + } else { + logger.Warn("not active") + fmt.Println("auth_ok:-1") + } + fmt.Println("end") +} diff --git a/cmd/warehost-web/config.go b/cmd/warehost-web/config.go index d04a164..58721ea 100644 --- a/cmd/warehost-web/config.go +++ b/cmd/warehost-web/config.go @@ -7,6 +7,7 @@ import ( "gopkg.in/yaml.v2" ) +// Config of warehost webserver type Config struct { Address string `yaml:"address"` Port string `yaml:"port"` diff --git a/cmd/warehost/config.yml.example b/cmd/warehost/config.yml.example index 32f39c2..4f2d6ad 100644 --- a/cmd/warehost/config.yml.example +++ b/cmd/warehost/config.yml.example @@ -7,7 +7,7 @@ log: path: test.log webroot: ./webroot/build database: "host=localhost user=warehost dbname=warehost password=hallo sslmode=disable" -databasedebug: false +databasedebug: true modules: web: enabled: true diff --git a/config/config.go b/config/config.go index af60a25..d52d1a2 100644 --- a/config/config.go +++ b/config/config.go @@ -1,10 +1,9 @@ package config import ( + "gopkg.in/yaml.v2" "io/ioutil" "log" - - "gopkg.in/yaml.v2" ) // Config is the struct of the api diff --git a/lib/log/main.go b/lib/log/main.go index f47a451..1ff0a00 100644 --- a/lib/log/main.go +++ b/lib/log/main.go @@ -13,6 +13,11 @@ type ModulLog struct { log *log.Entry } +func NewSilenceLogger(path string) *log.Logger { + Log = NewLogger(path) + //Log.Out = nil + return Log +} func NewLogger(path string) *log.Logger { if Log != nil { return Log diff --git a/modul/host/models.go b/modul/host/models.go index 5f39119..19e8149 100644 --- a/modul/host/models.go +++ b/modul/host/models.go @@ -4,7 +4,71 @@ import ( "github.com/jinzhu/gorm" ) +// Profil struct +type Profil struct { + ID int64 + LoginID int64 `sql:"type:bigint NOT NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` + Reseller bool `sql:"default:false;column:reseller" json:"reseller"` +} + +// TableName of struct +func (Profil) TableName() string { return "host_profil" } + +// Domain struct +type Domain struct { + ID int64 + ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"profil"` + FQDN string `sql:"type:varchar(255);column:fqdn" json:"fqdn"` + Code string `sql:"type:varchar(255);column:code" json:"code"` + Active bool `sql:"default:false;column:active" json:"active"` + Mail bool `sql:"default:false;column:mail" json:"mail"` + Web bool `sql:"default:false;column:web" json:"web"` +} + +// TableName of struct +func (Domain) TableName() string { return "host_domain" } + +// Web struct +type Web struct { + ID int64 + DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"domain"` + Subdomain string `sql:"type:varchar(255);column:subdomain" json:"subdomain"` + PHP bool `sql:"default:false;column:php" json:"php"` + SSL bool `sql:"default:true;column:ssl" json:"ssl"` + SSLRedirect bool `sql:"default:false;column:sslredirect" json:"sslredirect"` + Redirect string `sql:"type:varchar(255);column:redirect" json:"redirect"` + Proxy string `sql:"type:varchar(255);column:proxy" json:"proxy"` + FTP []int64 `sql:"type:bigint[];column:ftp" json:"ftp"` + HTTPAccess []int64 `sql:"type:bigint[];column:httpaccess" json:"httpaccess"` +} + +// TableName of struct +func (Web) TableName() string { return "host_web" } + +// Mail struct +type Mail struct { + ID int64 + DomainID int64 `sql:"type:bigint NOT NULL REFERENCES host_domain(id) ON UPDATE CASCADE ON DELETE CASCADE;column:domain" json:"domain"` + Name string `sql:"type:varchar(255);column:name" json:"name"` + Forward string `sql:"type:varchar(255)[];column:forward" json:"forward"` + LoginID int64 `sql:"type:bigint NOT NULL REFERENCES login(id) ON UPDATE CASCADE ON DELETE CASCADE;column:login" json:"login"` +} + +// TableName of struct +func (Mail) TableName() string { return "host_mail" } + +// Database struct +type Database struct { + ID int64 + ProfilID int64 `sql:"type:bigint NOT NULL REFERENCES host_profil(id) ON UPDATE CASCADE ON DELETE CASCADE;column:profil" json:"profil"` + Password string `sql:"type:varchar(255);column:password" json:"password"` + Comment string `sql:"type:varchar(255);column:comment" json:"comment"` +} + +// TableName of struct +func (Database) TableName() string { return "host_database" } + // SyncModels to verify the database schema func SyncModels(dbconnection *gorm.DB) { - dbconnection.AutoMigrate() + dbconnection.AutoMigrate(&Profil{}, &Domain{}, &Web{}, &Mail{}, &Database{}) }