diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21361cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +multiserver_converter diff --git a/converter.go b/converter.go new file mode 100644 index 0000000..15c3871 --- /dev/null +++ b/converter.go @@ -0,0 +1,90 @@ +/* +multiserver_converter is a utility designed to convert MT authentication +databases to the multiserver authentication database scheme + +Usage: + multiserver_converter +where in is the path to the MT auth database +and out is the desired path to the newly created database +*/ +package main + +import ( + "database/sql" + "errors" + "fmt" + "os" + + _ "github.com/mattn/go-sqlite3" +) + +func convertDB(in, out string) error { + db, err := sql.Open("sqlite3", in) + if err != nil { + return err + } + defer db.Close() + + db2, err := sql.Open("sqlite3", out) + if err != nil { + return err + } + defer db2.Close() + + if _, err := db2.Exec(` + CREATE TABLE auth ( + name VARCHAR(32) NOT NULL, + password VARCHAR(512) NOT NULL + ); + CREATE TABLE privileges ( + name VARCHAR(32) NOT NULL, + privileges VARCHAR(1024) + ); + CREATE TABLE ban ( + addr VARCHAR(39) NOT NULL, + name VARCHAR(32) NOT NULL + );`); err != nil { + return err + } + + result := db.QueryRow("SELECT name, password FROM auth;") + + for { + var name, password string + + err := result.Scan(&name, &password) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + break + } + + return err + } + + _, err = db2.Exec(`INSERT INTO auth ( + name, password + ) VALUES ( + ?, + ? + );`, name, password) + if err != nil { + return err + } + } + + return nil +} + +func main() { + if len(os.Args) != 3 { + fmt.Println("Usage: multiserver_converter ") + os.Exit(1) + } + + if err := convertDB(os.Args[1], os.Args[2]); err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Println("Database converted successfully") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6abfaef --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/HimbeerserverDE/multiserver/multiserver_converter + +go 1.16 + +require github.com/mattn/go-sqlite3 v1.14.7 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..96ff824 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.7 h1:fxWBnXkxfM6sRiuH3bqJ4CfzZojMOLVc0UTsTglEghA= +github.com/mattn/go-sqlite3 v1.14.7/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=