2024-03-26 00:34:14 +01:00
|
|
|
// apparmor.d - Full set of apparmor profiles
|
|
|
|
// Copyright (C) 2021-2024 Alexandre Pujol <alexandre@pujol.io>
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
|
|
|
|
package prepare
|
|
|
|
|
|
|
|
import (
|
2024-10-23 16:47:49 +02:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-04-28 01:36:16 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/paths"
|
2024-10-02 17:22:46 +02:00
|
|
|
"github.com/roddhjav/apparmor.d/pkg/prebuild"
|
2024-03-26 00:34:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Ignore struct {
|
2024-10-02 17:22:46 +02:00
|
|
|
prebuild.Base
|
2024-03-26 00:34:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RegisterTask(&Ignore{
|
2024-10-02 17:22:46 +02:00
|
|
|
Base: prebuild.Base{
|
2024-03-26 00:34:14 +01:00
|
|
|
Keyword: "ignore",
|
|
|
|
Msg: "Ignore profiles and files from:",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p Ignore) Apply() ([]string, error) {
|
|
|
|
res := []string{}
|
2024-10-02 17:22:46 +02:00
|
|
|
for _, name := range []string{"main", prebuild.Distribution} {
|
|
|
|
for _, ignore := range prebuild.Ignore.Read(name) {
|
2024-10-23 16:47:49 +02:00
|
|
|
// Ignore file from share/
|
|
|
|
path := prebuild.Root.Join(ignore)
|
|
|
|
if path.Exist() {
|
|
|
|
if err := path.RemoveAll(); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore file from apparmor.d/
|
|
|
|
profile := strings.TrimPrefix(ignore, prebuild.Src+"/")
|
|
|
|
if strings.HasPrefix(ignore, prebuild.Src) {
|
|
|
|
path = prebuild.RootApparmord.Join(profile)
|
|
|
|
}
|
|
|
|
if path.Exist() {
|
|
|
|
if err := path.RemoveAll(); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
files, err := prebuild.RootApparmord.ReadDirRecursiveFiltered(nil, paths.FilterNames(profile))
|
2024-03-26 00:34:14 +01:00
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
2024-10-23 16:47:49 +02:00
|
|
|
if len(files) == 0 {
|
|
|
|
return res, fmt.Errorf("%s.ignore: no files found for '%s'", name, profile)
|
|
|
|
}
|
2024-03-26 00:34:14 +01:00
|
|
|
for _, path := range files {
|
|
|
|
if err := path.RemoveAll(); err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
}
|
2024-10-23 16:47:49 +02:00
|
|
|
|
2024-03-26 00:34:14 +01:00
|
|
|
}
|
|
|
|
}
|
2024-10-02 17:22:46 +02:00
|
|
|
res = append(res, prebuild.IgnoreDir.Join(name+".ignore").String())
|
2024-03-26 00:34:14 +01:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|