2020-05-03 19:41:09 +02:00
|
|
|
package sbctl
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-05-18 01:33:24 +02:00
|
|
|
"fmt"
|
2024-07-28 20:40:43 +02:00
|
|
|
"path/filepath"
|
2023-02-17 19:19:26 +01:00
|
|
|
|
2024-07-18 22:57:49 +02:00
|
|
|
"github.com/foxboron/sbctl/config"
|
2023-02-17 19:19:26 +01:00
|
|
|
"github.com/foxboron/sbctl/fs"
|
2024-07-28 20:40:43 +02:00
|
|
|
"github.com/foxboron/sbctl/lsm"
|
|
|
|
|
"github.com/landlock-lsm/go-landlock/landlock"
|
2024-07-30 23:30:43 +02:00
|
|
|
|
2024-07-18 22:57:49 +02:00
|
|
|
"github.com/spf13/afero"
|
2020-05-03 19:41:09 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type SigningEntry struct {
|
|
|
|
|
File string `json:"file"`
|
|
|
|
|
OutputFile string `json:"output_file"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type SigningEntries map[string]*SigningEntry
|
|
|
|
|
|
2024-07-18 22:57:49 +02:00
|
|
|
func ReadFileDatabase(vfs afero.Fs, dbpath string) (SigningEntries, error) {
|
|
|
|
|
f, err := ReadOrCreateFile(vfs, dbpath)
|
2020-05-03 19:41:09 +02:00
|
|
|
if err != nil {
|
2021-01-11 00:41:42 -03:00
|
|
|
return nil, err
|
2020-05-03 19:41:09 +02:00
|
|
|
}
|
2020-10-28 20:00:50 -03:00
|
|
|
|
|
|
|
|
files := make(SigningEntries)
|
2022-10-31 22:31:22 +01:00
|
|
|
if len(f) == 0 {
|
|
|
|
|
return files, nil
|
|
|
|
|
}
|
2022-07-03 23:28:42 +02:00
|
|
|
if err = json.Unmarshal(f, &files); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed to parse json: %v", err)
|
|
|
|
|
}
|
2020-10-28 20:00:50 -03:00
|
|
|
|
|
|
|
|
return files, nil
|
2020-05-03 19:41:09 +02:00
|
|
|
}
|
|
|
|
|
|
2024-07-18 22:57:49 +02:00
|
|
|
func WriteFileDatabase(vfs afero.Fs, dbpath string, files SigningEntries) error {
|
2020-05-03 19:41:09 +02:00
|
|
|
data, err := json.MarshalIndent(files, "", " ")
|
|
|
|
|
if err != nil {
|
2021-05-20 00:26:59 +02:00
|
|
|
return err
|
2020-05-03 19:41:09 +02:00
|
|
|
}
|
2024-07-18 22:57:49 +02:00
|
|
|
err = fs.WriteFile(vfs, dbpath, data, 0644)
|
2020-05-03 19:41:09 +02:00
|
|
|
if err != nil {
|
2021-05-20 00:26:59 +02:00
|
|
|
return err
|
2020-05-03 19:41:09 +02:00
|
|
|
}
|
2021-05-20 00:26:59 +02:00
|
|
|
return nil
|
2020-05-03 19:41:09 +02:00
|
|
|
}
|
2021-05-18 01:33:24 +02:00
|
|
|
|
2024-07-18 22:57:49 +02:00
|
|
|
func SigningEntryIter(state *config.State, fn func(s *SigningEntry) error) error {
|
|
|
|
|
files, err := ReadFileDatabase(state.Fs, state.Config.FilesDb)
|
2021-05-18 01:33:24 +02:00
|
|
|
if err != nil {
|
2024-07-18 22:57:49 +02:00
|
|
|
return fmt.Errorf("couldn't open database %v: %w", state.Config.FilesDb, err)
|
2021-05-18 01:33:24 +02:00
|
|
|
}
|
|
|
|
|
for _, s := range files {
|
|
|
|
|
if err := fn(s); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2024-07-28 20:40:43 +02:00
|
|
|
|
|
|
|
|
func LandlockFromFileDatabase(state *config.State) error {
|
|
|
|
|
var llrules []landlock.Rule
|
|
|
|
|
files, err := ReadFileDatabase(state.Fs, state.Config.FilesDb)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, entry := range files {
|
2024-08-01 12:06:26 +02:00
|
|
|
if entry.File == entry.OutputFile {
|
|
|
|
|
// If file is the same as output, set RW+Trunc on file
|
|
|
|
|
llrules = append(llrules,
|
|
|
|
|
lsm.TruncFile(entry.File).IgnoreIfMissing(),
|
|
|
|
|
)
|
|
|
|
|
}
|
2024-07-28 20:40:43 +02:00
|
|
|
if entry.File != entry.OutputFile {
|
2024-08-01 12:06:26 +02:00
|
|
|
// Set input file to RO, ignore if missing so we can bubble a useable
|
|
|
|
|
// error to the user
|
|
|
|
|
llrules = append(llrules, landlock.ROFiles(entry.File).IgnoreIfMissing())
|
|
|
|
|
|
|
|
|
|
// Check if output file exists
|
|
|
|
|
// if it does we set RW on the file directly
|
|
|
|
|
// if it doesnt, we set RW on the directory
|
|
|
|
|
if ok, _ := afero.Exists(state.Fs, entry.OutputFile); ok {
|
|
|
|
|
llrules = append(llrules, lsm.TruncFile(entry.OutputFile))
|
|
|
|
|
} else {
|
|
|
|
|
llrules = append(llrules, landlock.RWDirs(filepath.Dir(entry.OutputFile)))
|
|
|
|
|
}
|
2024-07-28 20:40:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lsm.RestrictAdditionalPaths(llrules...)
|
|
|
|
|
return nil
|
|
|
|
|
}
|