sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0

first pure-ftp-auth + db of host

This commit is contained in:
Martin Geno 2016-09-02 21:32:56 +02:00
parent 78ea10dc19
commit 3a3af67885
8 changed files with 182 additions and 4 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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")
}

View File

@ -7,6 +7,7 @@ import (
"gopkg.in/yaml.v2"
)
// Config of warehost webserver
type Config struct {
Address string `yaml:"address"`
Port string `yaml:"port"`

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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{})
}