@@ -413,6 +413,15 @@ func (db *MySQLDb) LoadData(ctx *sql.Context, buf []byte) (err error) {
413413 ed .PutReplicaSourceInfo (replicaSourceInfo )
414414 }
415415
416+ // Load superusers
417+ for i := 0 ; i < serialMySQLDb .SuperUserLength (); i ++ {
418+ serialUser := new (serial.User )
419+ if ! serialMySQLDb .SuperUser (serialUser , i ) {
420+ continue
421+ }
422+ ed .PutUser (LoadUser (serialUser ))
423+ }
424+
416425 // TODO: fill in other tables when they exist
417426 return
418427}
@@ -508,11 +517,36 @@ func (db *MySQLDb) AddRootAccount() {
508517 db .AddSuperUser (ed , "root" , "localhost" , "" )
509518}
510519
520+ // AddEphemeralSuperUser adds a new temporary superuser account for the specified username, host,
521+ // and password. The superuser account will only exist for the lifetime of the server process; once
522+ // the server is restarted, this superuser account will not be present.
523+ func (db * MySQLDb ) AddEphemeralSuperUser (ed * Editor , username string , host string , password string ) {
524+ db .SetEnabled (true )
525+
526+ if len (password ) > 0 {
527+ hash := sha1 .New ()
528+ hash .Write ([]byte (password ))
529+ s1 := hash .Sum (nil )
530+ hash .Reset ()
531+ hash .Write (s1 )
532+ s2 := hash .Sum (nil )
533+ password = "*" + strings .ToUpper (hex .EncodeToString (s2 ))
534+ }
535+
536+ if _ , ok := ed .GetUser (UserPrimaryKey {
537+ Host : host ,
538+ User : username ,
539+ }); ! ok {
540+ addSuperUser (ed , username , host , password , true )
541+ }
542+ }
543+
511544// AddSuperUser adds the given username and password to the list of accounts. This is a temporary function, which is
512545// meant to replace the "auth.New..." functions while the remaining functions are added.
513546func (db * MySQLDb ) AddSuperUser (ed * Editor , username string , host string , password string ) {
514547 //TODO: remove this function and the called function
515548 db .SetEnabled (true )
549+
516550 if len (password ) > 0 {
517551 hash := sha1 .New ()
518552 hash .Write ([]byte (password ))
@@ -527,7 +561,33 @@ func (db *MySQLDb) AddSuperUser(ed *Editor, username string, host string, passwo
527561 Host : host ,
528562 User : username ,
529563 }); ! ok {
530- addSuperUser (ed , username , host , password )
564+ addSuperUser (ed , username , host , password , false )
565+ }
566+ }
567+
568+ // AddLockedSuperUser adds a new superuser with the specified |username|, |host|, and |password|
569+ // and sets the account to be locked so that it cannot be used to log in.
570+ func (db * MySQLDb ) AddLockedSuperUser (ed * Editor , username string , host string , password string ) {
571+ user := db .GetUser (ed , username , host , false )
572+
573+ // If the user doesn't exist yet, create it and lock it
574+ if user == nil {
575+ db .AddSuperUser (ed , username , host , password )
576+ user = db .GetUser (ed , username , host , false )
577+ if user == nil {
578+ panic ("unable to load newly created superuser: " + username )
579+ }
580+
581+ // Lock the account to prevent it being used to log in
582+ user .Locked = true
583+ ed .PutUser (user )
584+ }
585+
586+ // If the user exists, but isn't a superuser or locked, fix it
587+ if user .IsSuperUser == false || user .Locked == false {
588+ user .IsSuperUser = true
589+ user .Locked = true
590+ ed .PutUser (user )
531591 }
532592}
533593
@@ -803,10 +863,12 @@ func (db *MySQLDb) Persist(ctx *sql.Context, ed *Editor) error {
803863 var users []* User
804864 var superUsers []* User
805865 ed .VisitUsers (func (u * User ) {
806- if ! u .IsSuperUser {
807- users = append (users , u )
808- } else {
809- superUsers = append (superUsers , u )
866+ if ! u .IsEphemeral {
867+ if ! u .IsSuperUser {
868+ users = append (users , u )
869+ } else {
870+ superUsers = append (superUsers , u )
871+ }
810872 }
811873 })
812874 sort .Slice (users , func (i , j int ) bool {
0 commit comments