2014-01-09 06:42:05 +00:00
|
|
|
package models
|
2013-12-12 06:27:43 +00:00
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
import (
|
2016-01-13 02:46:17 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2019-03-28 03:48:31 +00:00
|
|
|
"time"
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2016-01-19 03:13:32 +00:00
|
|
|
"bitbucket.org/liamstask/goose/lib/goose"
|
|
|
|
|
2018-05-04 00:07:41 +00:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Blank import needed to import mysql
|
2016-01-10 17:03:17 +00:00
|
|
|
"github.com/gophish/gophish/config"
|
2018-05-04 00:07:41 +00:00
|
|
|
log "github.com/gophish/gophish/logger"
|
2016-01-13 02:46:17 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2015-02-07 02:24:10 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3" // Blank import needed to import sqlite3
|
2014-03-25 03:31:33 +00:00
|
|
|
)
|
2014-01-13 03:36:26 +00:00
|
|
|
|
2016-03-09 04:37:55 +00:00
|
|
|
var db *gorm.DB
|
2018-12-15 21:42:32 +00:00
|
|
|
var conf *config.Config
|
2015-02-07 02:24:10 +00:00
|
|
|
|
2019-03-28 03:48:31 +00:00
|
|
|
const MaxDatabaseConnectionAttempts int = 10
|
|
|
|
|
2014-03-28 04:31:51 +00:00
|
|
|
const (
|
2018-12-16 03:38:51 +00:00
|
|
|
CampaignInProgress string = "In progress"
|
|
|
|
CampaignQueued string = "Queued"
|
|
|
|
CampaignCreated string = "Created"
|
|
|
|
CampaignEmailsSent string = "Emails Sent"
|
|
|
|
CampaignComplete string = "Completed"
|
|
|
|
EventSent string = "Email Sent"
|
|
|
|
EventSendingError string = "Error Sending Email"
|
|
|
|
EventOpened string = "Email Opened"
|
|
|
|
EventClicked string = "Clicked Link"
|
|
|
|
EventDataSubmit string = "Submitted Data"
|
|
|
|
EventReported string = "Email Reported"
|
|
|
|
EventProxyRequest string = "Proxied request"
|
|
|
|
StatusSuccess string = "Success"
|
|
|
|
StatusQueued string = "Queued"
|
|
|
|
StatusSending string = "Sending"
|
|
|
|
StatusUnknown string = "Unknown"
|
|
|
|
StatusScheduled string = "Scheduled"
|
|
|
|
StatusRetry string = "Retrying"
|
|
|
|
Error string = "Error"
|
2014-03-28 04:31:51 +00:00
|
|
|
)
|
|
|
|
|
2014-03-26 19:50:16 +00:00
|
|
|
// Flash is used to hold flash information for use in templates.
|
|
|
|
type Flash struct {
|
|
|
|
Type string
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2015-02-07 02:24:10 +00:00
|
|
|
// Response contains the attributes found in an API response
|
2014-06-02 03:30:23 +00:00
|
|
|
type Response struct {
|
2014-06-02 04:38:21 +00:00
|
|
|
Message string `json:"message"`
|
|
|
|
Success bool `json:"success"`
|
|
|
|
Data interface{} `json:"data"`
|
2014-06-02 03:30:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-13 02:46:17 +00:00
|
|
|
// Copy of auth.GenerateSecureKey to prevent cyclic import with auth library
|
|
|
|
func generateSecureKey() string {
|
|
|
|
k := make([]byte, 32)
|
|
|
|
io.ReadFull(rand.Reader, k)
|
|
|
|
return fmt.Sprintf("%x", k)
|
|
|
|
}
|
|
|
|
|
2016-11-19 16:37:22 +00:00
|
|
|
func chooseDBDriver(name, openStr string) goose.DBDriver {
|
|
|
|
d := goose.DBDriver{Name: name, OpenStr: openStr}
|
|
|
|
|
|
|
|
switch name {
|
|
|
|
case "mysql":
|
|
|
|
d.Import = "github.com/go-sql-driver/mysql"
|
|
|
|
d.Dialect = &goose.MySqlDialect{}
|
|
|
|
|
|
|
|
// Default database is sqlite3
|
|
|
|
default:
|
|
|
|
d.Import = "github.com/mattn/go-sqlite3"
|
|
|
|
d.Dialect = &goose.Sqlite3Dialect{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
2014-03-25 03:31:33 +00:00
|
|
|
// Setup initializes the Conn object
|
|
|
|
// It also populates the Gophish Config object
|
2018-12-15 21:42:32 +00:00
|
|
|
func Setup(c *config.Config) error {
|
|
|
|
// Setup the package-scoped config
|
|
|
|
conf = c
|
2016-01-19 03:13:32 +00:00
|
|
|
// Setup the goose configuration
|
|
|
|
migrateConf := &goose.DBConf{
|
2018-12-15 21:42:32 +00:00
|
|
|
MigrationsDir: conf.MigrationsPath,
|
2016-01-19 03:13:32 +00:00
|
|
|
Env: "production",
|
2018-12-15 21:42:32 +00:00
|
|
|
Driver: chooseDBDriver(conf.DBName, conf.DBPath),
|
2016-01-19 03:13:32 +00:00
|
|
|
}
|
|
|
|
// Get the latest possible migration
|
|
|
|
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-01-19 03:13:32 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Open our database connection
|
2019-03-28 03:48:31 +00:00
|
|
|
i := 0
|
|
|
|
for {
|
|
|
|
db, err = gorm.Open(conf.DBName, conf.DBPath)
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil && i >= MaxDatabaseConnectionAttempts {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
i += 1
|
|
|
|
log.Warn("waiting for database to be up...")
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
}
|
2014-03-28 04:31:51 +00:00
|
|
|
db.LogMode(false)
|
2018-05-04 00:07:41 +00:00
|
|
|
db.SetLogger(log.Logger)
|
2016-08-03 04:28:22 +00:00
|
|
|
db.DB().SetMaxOpenConns(1)
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2014-03-26 19:50:16 +00:00
|
|
|
return err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2016-01-19 03:13:32 +00:00
|
|
|
// Migrate up to the latest version
|
|
|
|
err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB())
|
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-01-19 03:13:32 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-02-09 01:04:59 +00:00
|
|
|
// Create the admin user if it doesn't exist
|
|
|
|
var userCount int64
|
|
|
|
db.Model(&User{}).Count(&userCount)
|
2019-02-20 02:33:50 +00:00
|
|
|
adminRole, err := GetRoleBySlug(RoleAdmin)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
2018-02-09 01:04:59 +00:00
|
|
|
if userCount == 0 {
|
2015-02-07 02:24:10 +00:00
|
|
|
initUser := User{
|
2014-03-25 03:31:33 +00:00
|
|
|
Username: "admin",
|
|
|
|
Hash: "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS", //gophish
|
2019-02-20 02:33:50 +00:00
|
|
|
Role: adminRole,
|
|
|
|
RoleID: adminRole.ID,
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2016-01-13 02:46:17 +00:00
|
|
|
initUser.ApiKey = generateSecureKey()
|
2015-02-07 02:24:10 +00:00
|
|
|
err = db.Save(&initUser).Error
|
2014-03-26 04:53:51 +00:00
|
|
|
if err != nil {
|
2018-05-04 00:07:41 +00:00
|
|
|
log.Error(err)
|
2016-01-19 03:13:32 +00:00
|
|
|
return err
|
2014-03-26 04:53:51 +00:00
|
|
|
}
|
2014-03-25 03:31:33 +00:00
|
|
|
}
|
2014-03-25 03:38:59 +00:00
|
|
|
return nil
|
2013-12-12 07:00:22 +00:00
|
|
|
}
|