func main() { readCommandLineArgs() var err error // s := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", App.DBUser, App.DBName) lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open: Error = %v\n", err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping: Error = %v\n", err) } sha := sha512.Sum512([]byte(App.password)) passhash := fmt.Sprintf("%x", sha) update, err := App.db.Prepare("update people set passhash=?") if nil != err { fmt.Printf("error = %v\n", err) os.Exit(1) } _, err = update.Exec(passhash) if nil != err { fmt.Printf("error = %v\n", err) } else { fmt.Printf("password for all users has been set to \"%s\"\n", App.password) } }
func main() { readCommandLineArgs() // s := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", App.DBUser, App.DBName) // s := "<awsdbusername>:<password>@tcp(<rdsinstancename>:3306)/accord" var err error lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) os.Exit(1) } readAccessRoles() if App.DumpRoles { for i := 0; i < len(App.Roles); i++ { fmt.Printf("%d - %s\n", App.Roles[i].RID, App.Roles[i].Name) } os.Exit(0) } App.RID = 0 for i := 0; i < len(App.Roles); i++ { if App.Roles[i].Name == App.rname { App.RID = App.Roles[i].RID } } if 0 == App.RID { fmt.Printf("Could not find role named: %s\n", App.rname) os.Exit(0) } uid := 0 err = App.db.QueryRow("select uid from people where username=?", App.username).Scan(&uid) if 0 == uid { fmt.Printf("Database %s does not have a user with username = %s\n", App.DBName, App.username) os.Exit(1) } update, err := App.db.Prepare("update people set RID=? where uid=?") if nil != err { fmt.Printf("error = %v\n", err) os.Exit(1) } _, err = update.Exec(App.RID, uid) if nil != err { fmt.Printf("error = %v\n", err) } else { fmt.Printf("password for uid %d has been updated to %s\n", uid, App.rname) } }
func main() { readCommandLineArgs() var err error // s := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", App.DBUser, App.DBName) lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open: Error = %v\n", err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping: Error = %v\n", err) } readAccessRoles() if App.DumpRoles { for i := 0; i < len(App.Roles); i++ { fmt.Printf("%d - %s\n", App.Roles[i].RID, App.Roles[i].Name) } os.Exit(0) } App.RID = 0 for i := 0; i < len(App.Roles); i++ { if App.Roles[i].Name == App.rname { App.RID = App.Roles[i].RID } } if 0 == App.RID { fmt.Printf("Could not find role named: %s\n", App.rname) os.Exit(0) } getUsername() sha := sha512.Sum512([]byte(App.passwd)) passhash := fmt.Sprintf("%x", sha) stmt, err := App.db.Prepare("INSERT INTO people (UserName,passhash,FirstName,LastName,RID) VALUES(?,?,?,?,?)") if nil != err { fmt.Printf("error = %v\n", err) os.Exit(1) } _, err = stmt.Exec(App.username, passhash, App.firstname, App.lastname, App.RID) if nil != err { fmt.Printf("error = %v\n", err) } else { fmt.Printf("Added user to database %s: username: %s, access role: %s\n", App.DBName, App.username, App.rname) } }
func main() { readCommandLineArgs() if App.TestUsers > 100 && !App.UpdateDBOnly { fmt.Printf("Maximum users per simulation is 100. You specified %d. Please reduce user count.\n", App.TestUsers) os.Exit(1) } var err error // s := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", App.DBUser, App.DBName) lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open: Error = %v\n", err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping: Error = %v\n", err) } buildPreparedStatements() readAccessRoles() loadNames() loadMaps() App.Peeps = make([]*personDetail, 0) initProfiles() loadUsers() loadCompanyList() if App.TestUsers > len(App.Peeps) { for i := 0; i < App.TestUsers-len(App.Peeps); i++ { var v personDetail createUser(&v) } App.Peeps = make([]*personDetail, 0) loadUsers() } if !App.UpdateDBOnly { executeSimulation() } }
func main() { readCommandLineArgs() var err error // s := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", App.DBUser, App.DBName) // App.db, err = sql.Open("mysql", s) lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) } if len(App.fname) > 0 && len(App.lname) > 0 { getUserName() if App.usernameonly { fmt.Printf("%s\n", App.user) os.Exit(0) } } if len(App.password) == 0 { App.password = lib.RandPasswordStringRunes(8) } err = lib.UpdateUserPassword(App.user, App.password, App.db) if nil != err { fmt.Printf("error = %v\n", err) os.Exit(1) } fmt.Printf("%s\nusername: %s\npassword: %s\nOK\n", getRealName(), App.user, App.password) }
func main() { readCommandLineArgs() if "" == App.username { fmt.Printf("You must supply the username using the -n option\n") os.Exit(2) } if 0 == App.UID { fmt.Printf("You must supply the uid of the user for whom you wish to change the username using the -u option\n") os.Exit(2) } var err error // s := fmt.Sprintf("ec2-user:@/%s?charset=utf8&parseTime=True", App.DBName) lib.ReadConfig() s := lib.GetSQLOpenString(App.DBUser, App.DBName) App.db, err = sql.Open("mysql", s) if nil != err { fmt.Printf("sql.Open for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) } defer App.db.Close() err = App.db.Ping() if nil != err { fmt.Printf("App.db.Ping for database=%s, dbuser=%s: Error = %v\n", App.DBName, App.DBUser, err) } var uid int err = App.db.QueryRow("select uid from people where username=?", App.username).Scan(&uid) switch { case err == sql.ErrNoRows: fmt.Printf("username = %s is available for use in database %s\n", App.username, App.DBName) case err != nil: fmt.Printf("error with QueryRow selecting username: %s, error = %v\n", App.username, err) os.Exit(1) default: fmt.Printf("username %s is already being used in database %s. UID = %d\n", App.username, App.DBName, uid) os.Exit(2) } update, err := App.db.Prepare("update people set username=? where uid=?") if nil != err { fmt.Printf("error = %v\n", err) os.Exit(1) } c, err := update.Exec(App.username, App.UID) if nil != err { switch err { case sql.ErrNoRows: fmt.Printf("Database %s does not contain a user with uid=%d\n", App.DBName, App.UID) os.Exit(1) default: fmt.Printf("error = %v\n", err) os.Exit(1) } } else { n, _ := c.RowsAffected() if n == 0 { fmt.Printf("Database %s does not contain a user with uid=%d\n", App.DBName, App.UID) os.Exit(1) } fmt.Printf("uid %d now has username %s\n", App.UID, App.username) } }
func main() { rand.Seed(time.Now().UnixNano()) // We need this for generating random passwords, probably other random things too. //============================= // Hardcoded defaults... //============================= Phonebook.ReqMem = make(chan int) Phonebook.ReqMemAck = make(chan int) Phonebook.ReqSessionMem = make(chan int) Phonebook.ReqSessionMemAck = make(chan int) Phonebook.ReqCountersMem = make(chan int) Phonebook.ReqCountersMemAck = make(chan int) Phonebook.Roles = make([]Role, 0) Phonebook.SessionTimeout = 120 // minutes Phonebook.SessionCleanupTime = 10 // minutes //============================================== // There may be some command line overrides... //============================================== readCommandLineArgs() //============================================== // Now open the logfile and the database... //============================================== var err error Phonebook.LogFile, err = os.OpenFile("Phonebook.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) lib.Errcheck(err) defer Phonebook.LogFile.Close() log.SetOutput(Phonebook.LogFile) ulog("*** Accord PHONEBOOK ***\n") //============================================== // And the database... //============================================== // dbopenparms := fmt.Sprintf("%s:@/%s?charset=utf8&parseTime=True", Phonebook.DBUser, Phonebook.DBName) lib.ReadConfig() dbopenparms := lib.GetSQLOpenString(Phonebook.DBUser, Phonebook.DBName) db, err := sql.Open("mysql", dbopenparms) lib.Errcheck(err) defer db.Close() err = db.Ping() if nil != err { ulog("db.Ping: Error = %v\n", err) s := fmt.Sprintf("Could not establish database connection to db: %s, dbuser: %s\n", Phonebook.DBName, Phonebook.DBUser) ulog(s) fmt.Println(s) os.Exit(2) } ulog("MySQL database opened with \"%s\"\n", dbopenparms) Phonebook.db = db buildPreparedStatements() //============================================== // Load some of the database info... //============================================== loadMaps() readAccessRoles() if Phonebook.Debug { dumpAccessRoles() } //============================================== // On with the show... //============================================== initUI() go Dispatcher() go CounterDispatcher() go UpdateCounters() go SessionDispatcher() go SessionCleanup() initHTTP() sessionInit() ulog("Phonebook initiating HTTP service on port %d\n", Phonebook.Port) err = http.ListenAndServe(fmt.Sprintf(":%d", Phonebook.Port), nil) if nil != err { fmt.Printf("*** Error on http.ListenAndServe: %v\n", err) ulog("*** Error on http.ListenAndServe: %v\n", err) os.Exit(1) } }