diff --git a/main.go b/main.go index 95739638..69e6497f 100644 --- a/main.go +++ b/main.go @@ -5,23 +5,34 @@ import ( "flag" "fmt" "mtui/app" + "mtui/auth" "mtui/events" "mtui/jobs" "mtui/web" "net/http" "os" "os/signal" + "strings" "syscall" + dbauth "github.com/minetest-go/mtdb/auth" + "github.com/sirupsen/logrus" ) -func main() { +var version_flag = flag.Bool("version", false, "show the current version and exit") +var help_flag = flag.Bool("help", false, "shows all options") +var create_admin = flag.String("create_admin", "", "creates a new admin-user (format: 'user:pass')") - version_ptr := flag.Bool("version", false, "show the current version and exit") +func main() { flag.Parse() - if *version_ptr { + if *help_flag { + flag.PrintDefaults() + return + } + + if *version_flag { v := app.Version if v == "" { v = "DEV" @@ -49,6 +60,47 @@ func main() { panic(err) } + if *create_admin != "" { + parts := strings.Split(*create_admin, ":") + if len(parts) != 2 { + panic("invalid format") + } + username := parts[0] + password := parts[1] + salt, verifier, err := auth.CreateAuth(username, password) + if err != nil { + panic(err) + } + + str := auth.CreateDBPassword(salt, verifier) + entry, err := a.DBContext.Auth.GetByUsername(username) + if err != nil { + panic(err) + } + + entry = &dbauth.AuthEntry{ + Name: username, + Password: str, + } + err = a.DBContext.Auth.Create(entry) + if err != nil { + panic(err) + } + + for _, priv := range []string{"server", "interact", "privs"} { + err = a.DBContext.Privs.Create(&dbauth.PrivilegeEntry{ + ID: *entry.ID, + Privilege: priv, + }) + + if err != nil { + panic(err) + } + } + fmt.Printf("Created a new admin-user '%s'\n", username) + return + } + logrus.WithFields(logrus.Fields{ "version": app.Version, }).Info("Starting mtui")