Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for server passwords #55

Merged
merged 3 commits into from
Apr 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions cmd/grumble/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ func (server *Server) RootChannel() *Channel {
return root
}

// Set password as the new SuperUser password
func (server *Server) SetSuperUserPassword(password string) {
func (server *Server) setConfigPassword(key, password string) {
saltBytes := make([]byte, 24)
_, err := rand.Read(saltBytes)
if err != nil {
Expand All @@ -190,7 +189,6 @@ func (server *Server) SetSuperUserPassword(password string) {
digest := hex.EncodeToString(hasher.Sum(nil))

// Could be racy, but shouldn't really matter...
key := "SuperUserPassword"
val := "sha1$" + salt + "$" + digest
server.cfg.Set(key, val)

Expand All @@ -199,9 +197,18 @@ func (server *Server) SetSuperUserPassword(password string) {
}
}

// CheckSuperUserPassword checks whether password matches the set SuperUser password.
func (server *Server) CheckSuperUserPassword(password string) bool {
parts := strings.Split(server.cfg.StringValue("SuperUserPassword"), "$")
// SetSuperUserPassword sets password as the new SuperUser password
func (server *Server) SetSuperUserPassword(password string) {
server.setConfigPassword("SuperUserPassword", password)
}

// SetServerPassword sets password as the new Server password
func (server *Server) SetServerPassword(password string) {
server.setConfigPassword("ServerPassword", password)
}

func (server *Server) checkConfigPassword(key, password string) bool {
parts := strings.Split(server.cfg.StringValue(key), "$")
if len(parts) != 3 {
return false
}
Expand Down Expand Up @@ -239,6 +246,20 @@ func (server *Server) CheckSuperUserPassword(password string) bool {
return false
}

// CheckSuperUserPassword checks whether password matches the set SuperUser password.
func (server *Server) CheckSuperUserPassword(password string) bool {
return server.checkConfigPassword("SuperUserPassword", password)
}

// CheckServerPassword checks whether password matches the set Server password.
func (server *Server) CheckServerPassword(password string) bool {
return server.checkConfigPassword("ServerPassword", password)
}

func (server *Server) hasServerPassword() bool {
return server.cfg.StringValue("ServerPassword") != ""
}

// Called by the server to initiate a new client connection.
func (server *Server) handleIncomingClient(conn net.Conn) (err error) {
client := new(Client)
Expand Down Expand Up @@ -518,6 +539,13 @@ func (server *Server) handleAuthenticate(client *Client, msg *Message) {
}
}

if client.user == nil && server.hasServerPassword() {
if auth.Password == nil || !server.CheckServerPassword(*auth.Password) {
client.RejectAuth(mumbleproto.Reject_WrongServerPW, "Invalid server password")
return
}
}

// Setup the cryptstate for the client.
err = client.crypt.GenerateKey(client.CryptoMode)
if err != nil {
Expand Down