-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
251 lines (225 loc) · 6.59 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package db
/*
Sliver Implant Framework
Copyright (C) 2019 Bishop Fox
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import (
"errors"
"fmt"
"os"
"path"
"sync"
"github.com/bishopfox/sliver/server/assets"
"github.com/bishopfox/sliver/server/log"
"github.com/dgraph-io/badger"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
const (
dbDirName = "db"
rootDBDirName = "root"
bucketsDirName = "buckets"
)
var (
rootDB = getRootDB()
dbLog = log.NamedLogger("db", "")
dbCache = &map[string]*Bucket{}
dbCacheMutex = &sync.Mutex{}
)
// Bucket - Badger database and namespaced logger
type Bucket struct {
db *badger.DB
Log *logrus.Entry
}
// These are just pass-thru functions to prevent external callers from
// trying to manually manage db transcations
// Set - Set a key/value (simplified API)
func (b *Bucket) Set(key string, value []byte) error {
return b.db.Update(func(txn *badger.Txn) error {
return txn.Set([]byte(key), value)
})
}
// Get - Get a value for a given key (simplified API)
func (b *Bucket) Get(key string) ([]byte, error) {
var value []byte
err := b.db.View(func(txn *badger.Txn) error {
item, err := txn.Get([]byte(key))
if err != nil {
return err
}
value, err = item.ValueCopy(nil)
return err
})
return value, err
}
// Delete - Delete a key/value
func (b *Bucket) Delete(key string) error {
return b.db.Update(func(txn *badger.Txn) error {
return txn.Delete([]byte(key))
})
}
// List - Returns a list of keys filtered by prefix. Note that modify this list will not affect the database.
func (b *Bucket) List(prefix string) ([]string, error) {
keyPrefix := []byte(prefix)
keys := []string{}
err := b.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchValues = false
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(keyPrefix); it.ValidForPrefix(keyPrefix); it.Next() {
item := it.Item()
keys = append(keys, string(item.Key()))
}
return nil
})
return keys, err
}
// Map - Returns a map of key/values filtered by prefix. Note that modify this map will not affect the database.
func (b *Bucket) Map(prefix string) (map[string][]byte, error) {
keyPrefix := []byte(prefix)
bucketMap := map[string][]byte{}
err := b.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(keyPrefix); it.ValidForPrefix(keyPrefix); it.Next() {
item := it.Item()
value, err := item.ValueCopy(nil)
if err != nil {
continue
}
bucketMap[string(item.Key())] = value
}
return nil
})
return bucketMap, err
}
// Ptr to the root databasea that maps bucket Names <-> UUIDs
func getRootDB() *badger.DB {
rootDir := assets.GetRootAppDir()
dbDir := path.Join(rootDir, dbDirName, rootDBDirName)
if _, err := os.Stat(dbDir); os.IsNotExist(err) {
os.MkdirAll(dbDir, os.ModePerm)
}
opts := badger.DefaultOptions
opts.Dir = dbDir
opts.ValueDir = dbDir
opts.Logger = log.NamedLogger("db", "root")
db, err := badger.Open(opts)
if err != nil {
dbLog.Fatal(err)
}
return db
}
// GetBucket returns a namespaced database, names are mapped to directoires
// thru the rootDB which stores Name<->UUID pairs, this allows us to support
// bucket names with arbitrary string values
func GetBucket(name string) (*Bucket, error) {
if len(name) == 0 {
return nil, errors.New("Invalid bucket name")
}
rootDir := assets.GetRootAppDir()
txn := rootDB.NewTransaction(true)
defer txn.Discard()
var bucketUUID string
item, err := txn.Get([]byte(name))
if err == badger.ErrKeyNotFound {
dbLog.Infof("No bucket for name %#v", name)
id := uuid.New()
txn.Set([]byte(name), []byte(id.String()))
if err := txn.Commit(); err != nil {
dbLog.Debugf("Failed to create bucket %#v, %v", name, err)
return nil, err
}
dbLog.Infof("Created new bucket with name %#v (%s)", name, id.String())
bucketUUID = id.String()
} else if err != nil {
dbLog.Debugf("rootDB error %v", err)
return nil, err
} else {
val, _ := item.ValueCopy(nil)
bucketUUID = string(val)
// dbLog.Debugf("Using bucket %#v (%s)", name, bucketUUID)
}
// We can only call open() once on each directory so we save references
// to buckets when we open them.
dbCacheMutex.Lock()
defer dbCacheMutex.Unlock()
if bucket, ok := (*dbCache)[bucketUUID]; ok {
return bucket, nil
}
// No open handle to database, open/create the bucket
bucketDir := path.Join(rootDir, dbDirName, bucketsDirName, bucketUUID)
if _, err := os.Stat(bucketDir); os.IsNotExist(err) {
os.MkdirAll(bucketDir, os.ModePerm)
}
dbLog.Debugf("Loading db from %s", bucketDir)
opts := badger.DefaultOptions
opts.Dir = bucketDir
opts.ValueDir = bucketDir
logger := log.NamedLogger("db", name)
opts.Logger = logger
db, err := badger.Open(opts)
if err != nil {
dbLog.Errorf("Failed to open db %s", err)
return nil, err
}
bucket := &Bucket{
db: db,
Log: logger,
}
(*dbCache)[bucketUUID] = bucket
return bucket, nil
}
// DeleteBucket - Deletes a bucket from the filesystem and rootDB
func DeleteBucket(name string) error {
if len(name) == 0 {
return errors.New("Invalid bucket name")
}
rootDir := assets.GetRootAppDir()
txn := rootDB.NewTransaction(true)
defer txn.Discard()
var bucketUUID string
item, err := txn.Get([]byte(name))
if err == badger.ErrKeyNotFound {
return nil
} else if err != nil {
dbLog.Debugf("rootDB error %v", err)
return err
} else {
val, _ := item.ValueCopy(nil)
bucketUUID = string(val)
if len(bucketUUID) == 0 {
err = fmt.Errorf("Invalid bucket uuid %#v", bucketUUID)
dbLog.Error(err)
return err
}
}
dbLog.Debugf("Delete bucket %#v (%s)", name, bucketUUID)
txn.Delete([]byte(name))
if err := txn.Commit(); err != nil {
dbLog.Debugf("Failed to delete bucket %#v %v", name, err)
return err
}
bucketDir := path.Join(rootDir, dbDirName, bucketsDirName, bucketUUID)
if _, err := os.Stat(bucketDir); os.IsNotExist(err) {
return nil
}
dbLog.Debugf("Removing bucket dir %s", bucketDir)
err = os.RemoveAll(bucketDir)
if err != nil {
return err
}
return nil
}