示例#1
0
func Initialise() {
	logging.Info("data", "Initialise()")
	trackingSetup()

	dbConn, err := sql.Open("postgres", "postgres://"+config.All().Database.Username+
		":"+config.All().Database.Password+
		"@"+config.All().Database.Address+
		"/"+config.All().Database.Name+
		"?sslmode=require")
	if err != nil {
		logging.Error("data", "Error opening DB connection")
		logging.Error("data", "Error: ", err)
		tracking_notifyFault(err)
	}

	DB, err = gorm.Open("postgres", dbConn)
	DB.LogMode(true)

	if err != nil {
		logging.Error("data", "Error launching DB engine")
		logging.Error("data", "Error: ", err)
	}

	checkStructures(DB)

	//make sure that objects in the config BaseObjects are
	//existing, creating them if nessesary.
	for _, usr := range config.All().BaseObjects.AdminUsers {

		tmp := user.User{}

		DB.Where(&user.User{Username: usr.Username}).First(&tmp)

		if tmp.Username != usr.Username { //if the user was not found
			logging.Info("data", "Creating admin user: "******"data", "Could not create admin. ", err)
			}

			DB.Create(&user.User{Username: usr.Username,
				Permissions: []user.Permission{user.Permission{Name: user.PERM_ADMIN}},
				AuthMethods: []user.AuthenticationMethod{authMethod},
			})
		}
	}

	logging.Info("data", "Initialisation finished.")
}
示例#2
0
func SqlQueryServer(ws *websocket.Conn) {

	dbConn, err := sql.Open("postgres", "postgres://"+config.All().Database.Username+
		":"+config.All().Database.Password+
		"@"+config.All().Database.Address+
		"/"+config.All().Database.Name+
		"?sslmode=require")
	if err != nil {
		logging.Error("data-websock", "DB error: ", err.Error())
		return
	}
	defer dbConn.Close()

	for {
		var data struct {
			Type  string
			Query string
		}
		err := websocket.JSON.Receive(ws, &data)
		if err != nil {
			logging.Warning("data-websock", "Recieve error: ", err.Error())
			return
		}

		//process message
		switch data.Type {
		case "query":
			logging.Info("data-websock", "Got Query: ", data.Query)
			doQuery(data.Query, ws, dbConn)
		}
	}
}
示例#3
0
func HashPassword(password string) (string, error) {
	bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		logging.Error("auth", "Error hashing password", err)
		return "", err
	}

	return b64.StdEncoding.EncodeToString(bytes), nil
}
示例#4
0
文件: io.go 项目: twitchyliquid64/CNC
func loadTLS(keyPath, certPath string) (*tls.Config, error) {
	tlsConfig := new(tls.Config)

	tlsConfig.PreferServerCipherSuites = true
	tlsConfig.CipherSuites = []uint16{
		tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
		tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
		tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
		tls.TLS_RSA_WITH_AES_256_CBC_SHA,
		tls.TLS_RSA_WITH_AES_128_CBC_SHA,
		tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA}

	cert, err := tls.LoadX509KeyPair(certPath, keyPath)
	if err != nil {
		logging.Error("config", "Error loading tls certificate and key files.")
		logging.Error("config", err.Error())
		return nil, err
	}

	tlsConfig.Certificates = []tls.Certificate{cert}
	tlsConfig.BuildNameToCertificate()

	return tlsConfig, nil
}
示例#5
0
func Load(fpath string) error {
	if _, err := os.Stat(fpath); os.IsNotExist(err) {
		logging.Error("config", "Configuration file not found. If you need an example, use example-config.json (cp example-config.json config.json)")
		return err
	}

	conf, err := readConfig(fpath)
	if err == nil {
		gConfig = conf
	} else {
		logging.Error("config", "config.Load() error:", err)
		return err
	}

	tls, err := loadTLS(gConfig.TLS.PrivateKey, gConfig.TLS.Cert)
	if err == nil {
		gTls = tls
	} else {
		logging.Error("config", "config.Load() error:", err)
		return err
	}

	return nil
}
示例#6
0
func upgrade_v3(db gorm.DB) {
	// Hash the passwords
	logging.Info("Migrating 2 => 3")

	var unhashed []user.AuthenticationMethod
	db.Where("method_type = ?", user.AUTH_PASSWD).Find(&unhashed)

	for i := 0; i < len(unhashed); i++ {
		hashed, err := user.HashedPasswordAuth(unhashed[i].Value)
		if err != nil {
			logging.Error("data", "Could not transform password. ID ", unhashed[i].ID)
		} else {
			hashed.UserID = unhashed[i].UserID

			db.Create(&hashed)
			db.Delete(&unhashed[i])
		}
	}
}
示例#7
0
//Checks if a usename and password pair are valid, returning true and the user
//if it is.
func CheckAuthByPassword(username, password string, db gorm.DB) (bool, User) {
	success, tmp := GetByUsername(username, db)

	if !success {
		return false, tmp
	}

	LoadAuthMethods(&tmp, db)

	for _, authmethod := range tmp.AuthMethods {
		if authmethod.MethodType == AUTH_HASHPW {
			hashedPassword, err := b64.StdEncoding.DecodeString(authmethod.Value)

			if err != nil {
				logging.Error("auth", "Corrupted password in db. ID: ", authmethod.ID)
			} else if bcrypt.CompareHashAndPassword(hashedPassword, []byte(password)) == nil {
				return true, tmp
			}
		}
	}

	return false, User{}
}
示例#8
0
func (s *Server) handleEvent(e Event) {
	defer func(event Event) {
		err := recover()
		if err != nil {
			logging.Error("signaller", "Recovered from error when handling event: ", event, "Error: ", err)
			debug.PrintStack()
		}
	}(e)

	switch e.event {
	case connected:
		//Client connected
		e.client.reply(rplMOTD, s.motd)
		logging.Info("signaller", "[EVENT] Client connected: ", e)
	case disconnected:
		//Client disconnected
		logging.Info("signaller", "[EVENT] Client disconnected: ", e)
		for channel := range e.client.channelMap {
			e.client.partChannel(channel, "Client disconnected")
		}
		delete(s.clientMap, e.client.key) //test this?
	case command:
		//Client send a command
		fields := strings.Fields(e.input)
		if len(fields) < 1 {
			return
		}

		if strings.HasPrefix(fields[0], ":") {
			fields = fields[1:]
		}
		command := strings.ToUpper(fields[0])
		args := fields[1:]

		s.handleCommand(e.client, command, args)
	}
}