2014-01-09 23:18:49 +00:00
package db
import (
"database/sql"
2014-02-05 16:57:53 +00:00
"errors"
2014-01-09 23:18:49 +00:00
"fmt"
"os"
2014-01-31 04:46:25 +00:00
"time"
2014-01-09 23:18:49 +00:00
2014-01-30 21:08:14 +00:00
"github.com/coopernurse/gorp"
2014-01-09 23:18:49 +00:00
"github.com/jordan-wright/gophish/config"
2014-01-30 21:08:14 +00:00
"github.com/jordan-wright/gophish/models"
2014-01-09 23:18:49 +00:00
_ "github.com/mattn/go-sqlite3"
)
2014-01-30 21:08:14 +00:00
var Conn * gorp . DbMap
var DB * sql . DB
var err error
2014-02-05 16:57:53 +00:00
var ErrUsernameTaken = errors . New ( "Username already taken" )
2014-01-09 23:18:49 +00:00
// Setup initializes the Conn object
// It also populates the Gophish Config object
2014-01-30 21:08:14 +00:00
func Setup ( ) error {
DB , err := sql . Open ( "sqlite3" , config . Conf . DBPath )
Conn = & gorp . DbMap { Db : DB , Dialect : gorp . SqliteDialect { } }
2014-01-09 23:18:49 +00:00
//If the file already exists, delete it and recreate it
2014-01-30 21:08:14 +00:00
_ , err = os . Stat ( config . Conf . DBPath )
Conn . AddTableWithName ( models . User { } , "users" ) . SetKeys ( true , "Id" )
Conn . AddTableWithName ( models . Campaign { } , "campaigns" ) . SetKeys ( true , "Id" )
2014-02-01 22:35:16 +00:00
Conn . AddTableWithName ( models . Group { } , "groups" ) . SetKeys ( true , "Id" )
2014-01-09 23:18:49 +00:00
if err != nil {
2014-01-30 21:08:14 +00:00
fmt . Println ( "Database not found, recreating..." )
2014-01-13 04:39:40 +00:00
createTablesSQL := [ ] string {
//Create tables
2014-02-05 00:39:01 +00:00
` CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT NOT NULL, hash VARCHAR(60) NOT NULL, api_key VARCHAR(32), UNIQUE(username), UNIQUE(api_key)); ` ,
2014-02-01 22:35:16 +00:00
` CREATE TABLE campaigns (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, created_date TIMESTAMP NOT NULL, completed_date TIMESTAMP, template TEXT, status TEXT NOT NULL, uid INTEGER, FOREIGN KEY (uid) REFERENCES users(id)); ` ,
2014-02-05 03:53:11 +00:00
` CREATE TABLE targets (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, UNIQUE(email)); ` ,
2014-02-04 21:23:09 +00:00
` CREATE TABLE groups (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, modified_date TIMESTAMP NOT NULL); ` ,
` CREATE TABLE user_groups (uid INTEGER NOT NULL, gid INTEGER NOT NULL, FOREIGN KEY (uid) REFERENCES users(id), FOREIGN KEY (gid) REFERENCES groups(id), UNIQUE(uid, gid)) ` ,
` CREATE TABLE group_targets (gid INTEGER NOT NULL, tid INTEGER NOT NULL, FOREIGN KEY (gid) REFERENCES groups(id), FOREIGN KEY (tid) REFERENCES targets(id), UNIQUE(gid, tid)); ` ,
2014-01-13 04:39:40 +00:00
}
fmt . Println ( "Creating db at " + config . Conf . DBPath )
//Create the tables needed
for _ , stmt := range createTablesSQL {
2014-01-30 21:08:14 +00:00
_ , err = DB . Exec ( stmt )
2014-01-13 04:39:40 +00:00
if err != nil {
return err
}
}
//Create the default user
2014-01-30 21:08:14 +00:00
init_user := models . User {
2014-01-31 05:11:06 +00:00
Username : "admin" ,
2014-02-02 22:37:36 +00:00
Hash : "$2a$10$IYkPp0.QsM81lYYPrQx6W.U6oQGw7wMpozrKhKAHUBVL4mkm/EvAS" ,
2014-01-30 21:08:14 +00:00
APIKey : "12345678901234567890123456789012" ,
2014-01-13 04:39:40 +00:00
}
2014-01-30 21:08:14 +00:00
Conn . Insert ( & init_user )
2014-01-13 03:46:51 +00:00
if err != nil {
2014-01-30 21:08:14 +00:00
fmt . Println ( err )
2014-01-13 03:46:51 +00:00
}
2014-01-31 04:46:25 +00:00
c := models . Campaign {
Name : "Test Campaigns" ,
CreatedDate : time . Now ( ) . UTC ( ) ,
CompletedDate : time . Now ( ) . UTC ( ) ,
Template : "test template" ,
Status : "In progress" ,
Uid : init_user . Id ,
}
Conn . Insert ( & c )
2014-01-09 23:18:49 +00:00
}
return nil
}
2014-02-05 16:57:53 +00:00
// API Functions (GET, POST, PUT, DELETE)
// GetUser returns the user that the given id corresponds to. If no user is found, an
// error is thrown.
func GetUser ( id int64 ) ( models . User , error ) {
u := models . User { }
err := Conn . SelectOne ( & u , "SELECT * FROM Users WHERE id=?" , id )
if err != nil {
return u , err
}
return u , nil
}
// GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an
// error is thrown.
func GetUserByAPIKey ( key [ ] byte ) ( models . User , error ) {
u := models . User { }
err := Conn . SelectOne ( & u , "SELECT id, username, api_key FROM Users WHERE apikey=?" , key )
if err != nil {
return u , err
}
return u , nil
}
// GetUserByAPIKey returns the user that the given API Key corresponds to. If no user is found, an
// error is thrown.
func GetUserByUsername ( username string ) ( models . User , error ) {
u := models . User { }
err := Conn . SelectOne ( & u , "SELECT * FROM Users WHERE username=?" , username )
if err != sql . ErrNoRows {
return u , ErrUsernameTaken
} else if err != nil {
return u , err
}
return u , nil
}
func PutUser ( u * models . User ) error {
_ , err := Conn . Update ( u )
return err
}
func GetCampaigns ( key interface { } ) ( [ ] models . Campaign , error ) {
cs := [ ] models . Campaign { }
_ , err := Conn . Select ( & cs , "SELECT c.id, name, created_date, completed_date, status, template FROM campaigns c, users u WHERE c.uid=u.id AND u.api_key=?" , key )
return cs , err
}
func GetCampaign ( id int64 ) {
}