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

fix sqlite import #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ Grumble is an implementation of a server for the Mumble voice chat system. It is
Compiling Grumble from source
=============================

You must have a Go 1 environment installed to build Grumble. Those are available at:
## Dependencies

https://golang.org/dl/
- You must have a Go 1 environment installed to build Grumble. Those are available at: https://golang.org/dl/
- Grumble requires sqlite3 library

## Build

Once Go is installed, you should set up a GOPATH to avoid clobbering your Go environment's root directory with third party packages.

Expand Down
77 changes: 47 additions & 30 deletions cmd/grumble/freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"
)

type parentsMap map[uint32]uint32

// Freeze a server to disk and closes the log file.
// This must be called from within the Server's synchronous handler.
func (server *Server) FreezeToFile() error {
Expand Down Expand Up @@ -261,6 +263,8 @@ func (c *Channel) Unfreeze(fc *freezer.Channel) {
continue
}
g := acl.Group{}
g.Add = make(map[int]bool)
g.Remove = make(map[int]bool)
if fgrp.Inherit != nil {
g.Inherit = *fgrp.Inherit
}
Expand Down Expand Up @@ -430,7 +434,7 @@ func NewServerFromFrozen(name string) (s *Server, err error) {
// Add all channels, but don't hook up parent/child relationships
// until after we've walked the log file. No need to make it harder
// than it really is.
parents := make(map[uint32]uint32)
parents := make(parentsMap)
for _, fc := range fs.Channels {
// The frozen channel must contain an Id and a Name,
// since the server's frozen channels are guaranteed to
Expand Down Expand Up @@ -487,8 +491,48 @@ func NewServerFromFrozen(name string) (s *Server, err error) {
}
}

// Attempt to walk the stored log file
// Attempt to walk the stored log file (if exists)
logFile, err := os.Open(logFn)
if err == nil {
parents, err = s.walkLogFile(parents, logFile)
if err != nil {
return nil, err
}
} else if !os.IsNotExist(err) {
return nil, err
}

// Hook up children with their parents
for chanId, parentId := range parents {
childChan, exists := s.Channels[int(chanId)]
if !exists {
return nil, errors.New("Non-existant child channel")
}
parentChan, exists := s.Channels[int(parentId)]
if !exists {
return nil, errors.New("Non-existant parent channel")
}
parentChan.AddChild(childChan)
}

// Hook up all channel links
for _, channel := range s.Channels {
if len(channel.Links) > 0 {
links := channel.Links
channel.Links = make(map[int]*Channel)
for chanId, _ := range links {
targetChannel := s.Channels[chanId]
if targetChannel != nil {
s.LinkChannels(channel, targetChannel)
}
}
}
}

return s, nil
}

func (s *Server) walkLogFile(parents parentsMap, logFile *os.File) (parentsMap, error) {
walker, err := freezer.NewReaderWalker(logFile)
if err != nil {
return nil, err
Expand Down Expand Up @@ -649,34 +693,7 @@ func NewServerFromFrozen(name string) (s *Server, err error) {
}
}

// Hook up children with their parents
for chanId, parentId := range parents {
childChan, exists := s.Channels[int(chanId)]
if !exists {
return nil, errors.New("Non-existant child channel")
}
parentChan, exists := s.Channels[int(parentId)]
if !exists {
return nil, errors.New("Non-existant parent channel")
}
parentChan.AddChild(childChan)
}

// Hook up all channel links
for _, channel := range s.Channels {
if len(channel.Links) > 0 {
links := channel.Links
channel.Links = make(map[int]*Channel)
for chanId, _ := range links {
targetChannel := s.Channels[chanId]
if targetChannel != nil {
s.LinkChannels(channel, targetChannel)
}
}
}
}

return s, nil
return parents, nil
}

// Update the datastore with the user's current state.
Expand Down
39 changes: 21 additions & 18 deletions cmd/grumble/murmurdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"database/sql"
"errors"
"log"
"mumble.info/grumble/pkg/acl"
"mumble.info/grumble/pkg/ban"
"net"
"os"
"path/filepath"
"strconv"

_ "github.com/mattn/go-sqlite3"
"mumble.info/grumble/pkg/acl"
"mumble.info/grumble/pkg/ban"
)

const (
Expand All @@ -39,7 +41,7 @@ const SQLiteSupport = true

// Import the structure of an existing Murmur SQLite database.
func MurmurImport(filename string) (err error) {
db, err := sql.Open("sqlite", filename)
db, err := sql.Open("sqlite3", filename)
if err != nil {
panic(err.Error())
}
Expand Down Expand Up @@ -67,7 +69,7 @@ func MurmurImport(filename string) (err error) {
return err
}

err = os.Mkdir(filepath.Join(Args.DataDir, strconv.FormatInt(sid, 10)), 0750)
err = os.MkdirAll(filepath.Join(Args.DataDir, "servers", strconv.FormatInt(sid, 10)), 0750)
if err != nil {
return err
}
Expand Down Expand Up @@ -187,33 +189,34 @@ func populateChannelACLFromDatabase(server *Server, c *Channel, db *sql.DB) erro

for rows.Next() {
var (
UserId string
Group string
ApplyHere bool
ApplySub bool
Allow int64
Deny int64
UserId sql.NullString
Group sql.NullString
ApplyHere sql.NullBool
ApplySub sql.NullBool
Allow sql.NullInt64
Deny sql.NullInt64
)

if err := rows.Scan(&UserId, &Group, &ApplyHere, &ApplySub, &Allow, &Deny); err != nil {
return err
}

aclEntry := acl.ACL{}
aclEntry.ApplyHere = ApplyHere
aclEntry.ApplySubs = ApplySub
if len(UserId) > 0 {
aclEntry.UserId, err = strconv.Atoi(UserId)
aclEntry.ApplyHere = ApplyHere.Bool
aclEntry.ApplySubs = ApplySub.Bool
if UserId.Valid {
aclEntry.UserId, err = strconv.Atoi(UserId.String)
if err != nil {
return err
}
} else if len(Group) > 0 {
aclEntry.Group = Group
} else if Group.Valid {
aclEntry.Group = Group.String
} else {
return errors.New("Invalid ACL: Neither Group or UserId specified")
}

aclEntry.Deny = acl.Permission(Deny)
aclEntry.Allow = acl.Permission(Allow)
aclEntry.Deny = acl.Permission(Deny.Int64)
aclEntry.Allow = acl.Permission(Allow.Int64)
c.ACL.ACLs = append(c.ACL.ACLs, aclEntry)
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/grumble/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1452,9 +1452,9 @@ func (server *Server) Start() (err error) {
// Set sensible timeouts, in case no reverse proxy is in front of Grumble.
// Non-conforming (or malicious) clients may otherwise block indefinitely and cause
// file descriptors (or handles, depending on your OS) to leak and/or be exhausted
ReadTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 2 * time.Minute,
IdleTimeout: 2 * time.Minute,
}
go func() {
err := server.webhttp.ListenAndServeTLS("", "")
Expand Down