-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
364 lines (345 loc) · 9.15 KB
/
services.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
services.go- Contains mostly everything to do with
service/file/directory objects
*/
package main
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io/fs"
"io/ioutil"
"os"
"syscall"
)
// File object
type ServiceObject struct {
Mode fs.FileMode // File permissions
Name string
Owner int // UID
Group int // GID
Path string
Checksum string
Backup []byte // Contents of the file are stored in memory
isDir bool
}
type Directory struct {
Name string `json:"name"` // These tags are added let us use JSON unmarshal
Path string
isRecursive bool
files []*ServiceObject // Store pointers instead of actual variables to aid with making changes
}
type Service struct {
Name string `json:"name"`
locations []*ServiceObject // We store the objects in this array as well, to allow for easier iterating
Binary *ServiceObject `json:"binary"`
Service *ServiceObject `json:"service"`
Config *ServiceObject `json:"config"`
}
type Services struct {
Services []Service `json:"services"`
Files []ServiceObject `json:"other_files"`
Directories []Directory `json:"directories"`
}
// Check to see if the permissions for a file have been modified
func (a *ServiceObject) CheckPerms() bool {
stat, _ := os.Stat(a.Path)
if stat.Mode() != a.Mode {
if config.outputEnabled {
fmt.Printf("\nPermissions for %s have been modified. Restoring...\n", a.Name)
}
return false
}
// Also check uid and gid
inf := stat.Sys().(*syscall.Stat_t)
if int(inf.Uid) != a.Owner || int(inf.Gid) != a.Group {
if config.outputEnabled {
fmt.Printf("Permissions for %s have been modified. Restoring...\n", a.Name)
}
return false
}
return true
}
// Check to see if file has been deleted or modified
func (a *ServiceObject) CheckFile() bool {
if a.isDir {
return FileExists(a.Path)
}
// Get the SHA checksum of the file's current state
// and compare it to the one stored in memory
sha, err := a.GetSHA()
if err {
return false
}
if sha != a.Checksum {
return false
}
return true
}
func (a *Service) Init() bool {
var err bool
// Initialize the locations array
a.locations = []*ServiceObject{
a.Binary,
a.Service,
a.Config,
}
for _, location := range a.locations {
// Make sure that the file exists
// TODO should I delete this?
if !FileExists(location.Path) {
Warnf("Filepath error while importing %s. Skipping...\n", a.Name)
return false
}
// If it does, get the SHA (from backup or from current state if no backup / disabled)
location.Checksum, err = location.GetBackupSHA()
}
if err {
Warnf("Filepath error while importing %s. Skipping...\n", a.Name)
return false
}
return true
}
// Initialize a file object
func (a *ServiceObject) InitSO() bool {
var err bool
a.Checksum, err = a.GetBackupSHA()
if err {
Warnf("Filepath error while importing %s. Skipping...\n", a.Name)
return false
}
return true
}
// Add a directory and all of its files & subfolders recursively
func AddDir(path string, files []*ServiceObject) []*ServiceObject {
// Iterate through all items in the directory
items, _ := ioutil.ReadDir(path)
for _, item := range items {
// Get the path of the current item
subPath := ConcatenatePath(path, item.Name())
if item.IsDir() {
// Create a file object for the directory
newDir := &ServiceObject{
Name: subPath,
Path: subPath,
isDir: true,
}
// Make sure directory successfully inits first
if newDir.InitSO() {
newDir.InitBackup()
files = append(files, newDir)
// Recusively add all files and items in the subdirectory
files = append(files, AddDir(subPath, []*ServiceObject{})...)
}
} else {
// If the item is a file, just add it to the files array
newFile := &ServiceObject{
Name: subPath,
Path: subPath,
isDir: false,
}
if newFile.InitSO() {
newFile.InitBackup()
files = append(files, newFile)
}
}
}
return files
}
// Initialize a directory object
func (a *Directory) InitDir() bool {
if FileExists(a.Path) {
// Create the ServiceObject for the top directory
topDir := &ServiceObject{
Name: a.Path,
Path: a.Path,
isDir: true,
}
// Make sure the top directory inits
if topDir.InitSO() {
topDir.InitBackup()
}
// Add all files recursively
a.files = AddDir(a.Path, []*ServiceObject{topDir})
return true
}
Warnf("Directory %s doesn't exist. Skipping...", a.Path)
return false
}
// Get the SHA-256 checksum for a file,
// either from encrypted backup (if enabled)
// or from the original file
func (a *ServiceObject) GetBackupSHA() (string, bool) {
// Set local path variable
var path string = a.Path
// Grab the path for the backup folder
filename := GetConfigName(a.Path)
doEncrypt := false
// Check if the backup path exists
if FileExists(filename) && config.loadFromConfig {
// If it does, update the path and set to decrypt
path = filename
doEncrypt = true
}
f, err := os.Open(path)
if err != nil {
return "ERR", true
}
defer f.Close()
read, err := ioutil.ReadAll(f)
if err != nil {
return "ERR", true
}
// If we're reading from a backup, decrypt it
if config.doEncryption && doEncrypt {
read = decrypt(read, config.key)
}
// Return the sha256 in string format
sha := sha256.Sum256(read)
ret := hex.EncodeToString(sha[:])
return ret, false
}
// Get the SHA256 checksum of a file object
func (a *ServiceObject) GetSHA() (string, bool) {
f, err := os.Open(a.Path)
if err != nil {
return "ERR", true
}
defer f.Close()
read, _ := ioutil.ReadAll(f)
sha := sha256.Sum256(read)
ret := hex.EncodeToString(sha[:])
return ret, false
}
// Remove the backup file when freeing a file
func (a *ServiceObject) FreeBackup() {
filename := GetConfigName(a.Path)
if FileExists(filename) {
os.Remove(filename)
}
}
// Initialize the backup for a file
func (a *ServiceObject) InitBackup() {
// Get the file backup path
filename := GetConfigName(a.Path)
var path string = a.Path
doEncrypt := false
// Check to see if we're loading from a backup
if FileExists(filename) && config.loadFromConfig {
// If so, update the path
doEncrypt = true
path = filename
}
// Get permissions, owner, etc.
stat, _ := os.Stat(path)
inf := stat.Sys().(*syscall.Stat_t)
a.Owner = int(inf.Uid)
a.Group = int(inf.Gid)
a.Mode = stat.Mode()
// If the file isn't a directory, read and store the file's contents
if !a.isDir {
f, _ := os.Open(path)
a.Backup, _ = ioutil.ReadAll(f)
if doEncrypt {
a.Backup = decrypt(a.Backup, config.key)
}
defer f.Close()
}
if config.doBackup {
// Write to the backup file
cnfPath := GetConfigName(a.Path)
if a.isDir {
cnfPath = cnfPath + "._."
} else if config.doEncryption {
writeFile(cnfPath, encrypt(a.Backup, config.key))
} else {
writeFile(cnfPath, a.Backup)
}
// Set the backup's permisssions to match the file
os.Chmod(cnfPath, a.Mode)
os.Chown(cnfPath, a.Owner, a.Group)
}
}
// Initialize the backup folder
func InitConfigFolder() {
// Grab the encryption key from the user
if config.doEncryption {
Warnf("Please input the encryption/decryption key to use: ")
key := GetInput()
// Make it a key and convert it into a bytes[] object
config.key = GetPass(key)
}
// Set loadFromConfig
if FileExists(config.backupLocation) && config.loadFromConfig {
Warnf("Detected backup folder (%s). Load from backup? [y/n]: ", config.backupLocation)
if GetInput() == "y" {
config.loadFromConfig = true
} else {
config.loadFromConfig = false
}
} else if config.doBackup && config.loadFromConfig {
// If backup directory doesn't exist, create it
err := os.Mkdir(config.backupLocation, 0755)
if err != nil {
Errorf("Could not create config directory (%s)\n", config.backupLocation)
}
}
}
// Restore the backup of a file/folder
func (e *ServiceObject) writeBackup() bool {
if e.isDir {
// Each file object is scanned directly, so once we restore
// the directory, the files and directories below it will
// automatically be restored as well
if !FileExists(e.Path) {
err := os.Mkdir(e.Path, e.Mode)
if err != nil {
if config.outputEnabled {
Warnf("Error: Could not restore directory %s", e.Name)
}
return false
}
}
// We don't really need this but better safe than sorry idk
os.Chmod(e.Path, e.Mode)
os.Chown(e.Path, e.Owner, e.Group)
return true
}
// Check to see if the file was deleted or just modified
if !FileExists(e.Path) {
if config.outputEnabled {
fmt.Printf("File %s was deleted. Restoring...\n", e.Path)
}
// See if file is immutable
} else if IsImmutable(e.Path) {
if config.outputEnabled {
fmt.Printf("File %s is immutable. Removing immutable flag...\n", e.Path)
}
RemoveImmutable(e.Path)
}
// Restore the backup
ret := writeFile(e.Path, e.Backup)
if ret {
err := os.Chmod(e.Path, e.Mode)
if err != nil {
if config.outputEnabled {
fmt.Printf("Error setting permissions for %s", e.Path)
}
return false
}
os.Chown(e.Path, e.Owner, e.Group)
}
return ret
}
// Revert the permissions of a file back to its backup
func (e *ServiceObject) WritePerms() bool {
err := os.Chmod(e.Path, e.Mode)
if err != nil {
return false
}
err = os.Chown(e.Path, e.Owner, e.Group)
if err != nil {
return false
}
return true
}